blob: 6852df7dc6b93d8dc87fc5c7b54d47798f080732 [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 Lattner30c89792001-09-07 16:35:17 +0000298 switch (Ty->getPrimitiveID()) {
299 case Type::LabelTyID: d = new BBPlaceHolder(Ty, D); break;
300 case Type::MethodTyID: d = new MethPlaceHolder(Ty, D);
Chris Lattner93750fa2001-07-28 17:48:55 +0000301 LateResolver = &CurModule.LateResolveValues; break;
Chris Lattner30c89792001-09-07 16:35:17 +0000302 default: d = new ValuePlaceHolder(Ty, D); break;
Chris Lattner00950542001-06-06 20:29:01 +0000303 }
304
305 assert(d != 0 && "How did we not make something?");
Chris Lattner93750fa2001-07-28 17:48:55 +0000306 InsertValue(d, *LateResolver);
Chris Lattner00950542001-06-06 20:29:01 +0000307 return d;
308}
309
310
311//===----------------------------------------------------------------------===//
312// Code to handle forward references in instructions
313//===----------------------------------------------------------------------===//
314//
315// This code handles the late binding needed with statements that reference
316// values not defined yet... for example, a forward branch, or the PHI node for
317// a loop body.
318//
319// This keeps a table (CurMeth.LateResolveValues) of all such forward references
320// and back patchs after we are done.
321//
322
323// ResolveDefinitions - If we could not resolve some defs at parsing
324// time (forward branches, phi functions for loops, etc...) resolve the
325// defs now...
326//
327static void ResolveDefinitions(vector<ValueList> &LateResolvers) {
328 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
329 for (unsigned ty = 0; ty < LateResolvers.size(); ty++) {
330 while (!LateResolvers[ty].empty()) {
331 Value *V = LateResolvers[ty].back();
332 LateResolvers[ty].pop_back();
333 ValID &DID = getValIDFromPlaceHolder(V);
334
335 Value *TheRealValue = getVal(Type::getUniqueIDType(ty), DID, true);
336
Chris Lattner30c89792001-09-07 16:35:17 +0000337 if (TheRealValue == 0) {
338 if (DID.Type == 1)
339 ThrowException("Reference to an invalid definition: '" +DID.getName()+
340 "' of type '" + V->getType()->getDescription() + "'",
341 getLineNumFromPlaceHolder(V));
342 else
343 ThrowException("Reference to an invalid definition: #" +
344 itostr(DID.Num) + " of type '" +
345 V->getType()->getDescription() + "'",
346 getLineNumFromPlaceHolder(V));
347 }
348
Chris Lattnercfe26c92001-10-01 18:26:53 +0000349 assert(!isa<Type>(V) && "Types should be in LateResolveTypes!");
Chris Lattner00950542001-06-06 20:29:01 +0000350
351 V->replaceAllUsesWith(TheRealValue);
Chris Lattner00950542001-06-06 20:29:01 +0000352 delete V;
353 }
354 }
355
356 LateResolvers.clear();
357}
358
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000359// ResolveType - Take a specified unresolved type and resolve it. If there is
360// nothing to resolve it to yet, return true. Otherwise resolve it and return
361// false.
362//
363static bool ResolveType(PATypeHolder<Type> &T) {
364 const Type *Ty = T;
365 ValID &DID = getValIDFromPlaceHolder(Ty);
366
367 const Type *TheRealType = getTypeVal(DID, true);
368 if (TheRealType == 0) return true;
369
370 // Refine the opaque type we had to the new type we are getting.
371 cast<DerivedType>(Ty)->refineAbstractTypeTo(TheRealType);
372 return false;
373}
374
Chris Lattner30c89792001-09-07 16:35:17 +0000375
376// ResolveTypes - This goes through the forward referenced type table and makes
377// sure that all type references are complete. This code is executed after the
378// constant pool of a method or module is completely parsed.
Chris Lattner00950542001-06-06 20:29:01 +0000379//
Chris Lattner30c89792001-09-07 16:35:17 +0000380static void ResolveTypes(vector<PATypeHolder<Type> > &LateResolveTypes) {
381 while (!LateResolveTypes.empty()) {
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000382 if (ResolveType(LateResolveTypes.back())) {
383 const Type *Ty = LateResolveTypes.back();
384 ValID &DID = getValIDFromPlaceHolder(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000385
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000386 if (DID.Type == ValID::NameVal)
Chris Lattner30c89792001-09-07 16:35:17 +0000387 ThrowException("Reference to an invalid type: '" +DID.getName(),
388 getLineNumFromPlaceHolder(Ty));
389 else
390 ThrowException("Reference to an invalid type: #" + itostr(DID.Num),
391 getLineNumFromPlaceHolder(Ty));
Chris Lattner00950542001-06-06 20:29:01 +0000392 }
Chris Lattner30c89792001-09-07 16:35:17 +0000393
Chris Lattner30c89792001-09-07 16:35:17 +0000394 // No need to delete type, refine does that for us.
395 LateResolveTypes.pop_back();
396 }
397}
398
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000399
400// ResolveSomeTypes - This goes through the forward referenced type table and
401// completes references that are now done. This is so that types are
402// immediately resolved to be as concrete as possible. This does not cause
403// thrown exceptions if not everything is resolved.
404//
405static void ResolveSomeTypes(vector<PATypeHolder<Type> > &LateResolveTypes) {
406 for (unsigned i = 0; i < LateResolveTypes.size(); ) {
407 if (ResolveType(LateResolveTypes[i]))
408 ++i; // Type didn't resolve
409 else
410 LateResolveTypes.erase(LateResolveTypes.begin()+i); // Type resolved!
411 }
412}
413
414
Chris Lattner1781aca2001-09-18 04:00:54 +0000415// setValueName - Set the specified value to the name given. The name may be
416// null potentially, in which case this is a noop. The string passed in is
417// assumed to be a malloc'd string buffer, and is freed by this function.
418//
419static void setValueName(Value *V, char *NameStr) {
420 if (NameStr == 0) return;
421 string Name(NameStr); // Copy string
422 free(NameStr); // Free old string
423
Chris Lattner30c89792001-09-07 16:35:17 +0000424 SymbolTable *ST = CurMeth.CurrentMethod ?
425 CurMeth.CurrentMethod->getSymbolTableSure() :
426 CurModule.CurrentModule->getSymbolTableSure();
427
428 Value *Existing = ST->lookup(V->getType(), Name);
429 if (Existing) { // Inserting a name that is already defined???
430 // There is only one case where this is allowed: when we are refining an
431 // opaque type. In this case, Existing will be an opaque type.
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000432 if (const Type *Ty = dyn_cast<const Type>(Existing)) {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000433 if (OpaqueType *OpTy = dyn_cast<OpaqueType>(Ty)) {
Chris Lattner30c89792001-09-07 16:35:17 +0000434 // We ARE replacing an opaque type!
Chris Lattnerb00c5822001-10-02 03:41:24 +0000435 OpTy->refineAbstractTypeTo(cast<Type>(V));
Chris Lattner30c89792001-09-07 16:35:17 +0000436 return;
437 }
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000438 }
Chris Lattner30c89792001-09-07 16:35:17 +0000439
Chris Lattner9636a912001-10-01 16:18:37 +0000440 // Otherwise, we are a simple redefinition of a value, check to see if it
441 // is defined the same as the old one...
442 if (const Type *Ty = dyn_cast<const Type>(Existing)) {
443 if (Ty == cast<const Type>(V)) return; // Yes, it's equal.
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000444 cerr << "Type: " << Ty->getDescription() << " != "
445 << cast<const Type>(V)->getDescription() << "!\n";
Chris Lattner9636a912001-10-01 16:18:37 +0000446 } else {
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000447
Chris Lattner9636a912001-10-01 16:18:37 +0000448 }
Chris Lattner30c89792001-09-07 16:35:17 +0000449 ThrowException("Redefinition of value name '" + Name + "' in the '" +
450 V->getType()->getDescription() + "' type plane!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000451 }
Chris Lattner00950542001-06-06 20:29:01 +0000452
Chris Lattner30c89792001-09-07 16:35:17 +0000453 V->setName(Name, ST);
Chris Lattner00950542001-06-06 20:29:01 +0000454}
455
Chris Lattner8896eda2001-07-09 19:38:36 +0000456
Chris Lattner30c89792001-09-07 16:35:17 +0000457//===----------------------------------------------------------------------===//
458// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000459//
Chris Lattner8896eda2001-07-09 19:38:36 +0000460
Chris Lattner30c89792001-09-07 16:35:17 +0000461// TypeContains - Returns true if Ty contains E in it.
462//
463static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattner3ff43872001-09-28 22:56:31 +0000464 return find(df_begin(Ty), df_end(Ty), E) != df_end(Ty);
Chris Lattner30c89792001-09-07 16:35:17 +0000465}
Chris Lattner698b56e2001-07-20 19:15:08 +0000466
Chris Lattner30c89792001-09-07 16:35:17 +0000467
468static vector<pair<unsigned, OpaqueType *> > UpRefs;
469
470static PATypeHolder<Type> HandleUpRefs(const Type *ty) {
471 PATypeHolder<Type> Ty(ty);
472 UR_OUT(UpRefs.size() << " upreferences active!\n");
473 for (unsigned i = 0; i < UpRefs.size(); ) {
474 UR_OUT("TypeContains(" << Ty->getDescription() << ", "
475 << UpRefs[i].second->getDescription() << ") = "
476 << TypeContains(Ty, UpRefs[i].second) << endl);
477 if (TypeContains(Ty, UpRefs[i].second)) {
478 unsigned Level = --UpRefs[i].first; // Decrement level of upreference
479 UR_OUT("Uplevel Ref Level = " << Level << endl);
480 if (Level == 0) { // Upreference should be resolved!
481 UR_OUT("About to resolve upreference!\n";
482 string OldName = UpRefs[i].second->getDescription());
483 UpRefs[i].second->refineAbstractTypeTo(Ty);
484 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
485 UR_OUT("Type '" << OldName << "' refined upreference to: "
486 << (const void*)Ty << ", " << Ty->getDescription() << endl);
487 continue;
488 }
489 }
490
491 ++i; // Otherwise, no resolve, move on...
Chris Lattner8896eda2001-07-09 19:38:36 +0000492 }
Chris Lattner30c89792001-09-07 16:35:17 +0000493 // FIXME: TODO: this should return the updated type
Chris Lattner8896eda2001-07-09 19:38:36 +0000494 return Ty;
495}
496
Chris Lattner30c89792001-09-07 16:35:17 +0000497template <class TypeTy>
498inline static void TypeDone(PATypeHolder<TypeTy> *Ty) {
499 if (UpRefs.size())
500 ThrowException("Invalid upreference in type: " + (*Ty)->getDescription());
501}
502
503// newTH - Allocate a new type holder for the specified type
504template <class TypeTy>
505inline static PATypeHolder<TypeTy> *newTH(const TypeTy *Ty) {
506 return new PATypeHolder<TypeTy>(Ty);
507}
508template <class TypeTy>
509inline static PATypeHolder<TypeTy> *newTH(const PATypeHolder<TypeTy> &TH) {
510 return new PATypeHolder<TypeTy>(TH);
511}
512
513
Chris Lattner00950542001-06-06 20:29:01 +0000514//===----------------------------------------------------------------------===//
515// RunVMAsmParser - Define an interface to this parser
516//===----------------------------------------------------------------------===//
517//
Chris Lattnera2850432001-07-22 18:36:00 +0000518Module *RunVMAsmParser(const string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000519 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000520 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000521 llvmAsmlineno = 1; // Reset the current line number...
522
523 CurModule.CurrentModule = new Module(); // Allocate a new module to read
524 yyparse(); // Parse the file.
525 Module *Result = ParserResult;
Chris Lattner00950542001-06-06 20:29:01 +0000526 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
527 ParserResult = 0;
528
529 return Result;
530}
531
532%}
533
534%union {
Chris Lattner30c89792001-09-07 16:35:17 +0000535 Module *ModuleVal;
536 Method *MethodVal;
537 MethodArgument *MethArgVal;
538 BasicBlock *BasicBlockVal;
539 TerminatorInst *TermInstVal;
540 Instruction *InstVal;
541 ConstPoolVal *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000542
Chris Lattner30c89792001-09-07 16:35:17 +0000543 const Type *PrimType;
544 PATypeHolder<Type> *TypeVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000545 Value *ValueVal;
546
547 list<MethodArgument*> *MethodArgList;
548 list<Value*> *ValueList;
549 list<PATypeHolder<Type> > *TypeList;
Chris Lattnerc24d2082001-06-11 15:04:20 +0000550 list<pair<Value*, BasicBlock*> > *PHIList; // Represent the RHS of PHI node
Chris Lattner00950542001-06-06 20:29:01 +0000551 list<pair<ConstPoolVal*, BasicBlock*> > *JumpTable;
Chris Lattner30c89792001-09-07 16:35:17 +0000552 vector<ConstPoolVal*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000553
Chris Lattner30c89792001-09-07 16:35:17 +0000554 int64_t SInt64Val;
555 uint64_t UInt64Val;
556 int SIntVal;
557 unsigned UIntVal;
558 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000559 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000560
Chris Lattner30c89792001-09-07 16:35:17 +0000561 char *StrVal; // This memory is strdup'd!
562 ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000563
Chris Lattner30c89792001-09-07 16:35:17 +0000564 Instruction::UnaryOps UnaryOpVal;
565 Instruction::BinaryOps BinaryOpVal;
566 Instruction::TermOps TermOpVal;
567 Instruction::MemoryOps MemOpVal;
568 Instruction::OtherOps OtherOpVal;
Chris Lattner00950542001-06-06 20:29:01 +0000569}
570
571%type <ModuleVal> Module MethodList
Chris Lattnere1815642001-07-15 06:35:53 +0000572%type <MethodVal> Method MethodProto MethodHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000573%type <BasicBlockVal> BasicBlock InstructionList
574%type <TermInstVal> BBTerminatorInst
575%type <InstVal> Inst InstVal MemoryInst
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000576%type <ConstVal> ConstVal
Chris Lattner027dcc52001-07-08 21:10:27 +0000577%type <ConstVector> ConstVector UByteList
Chris Lattner00950542001-06-06 20:29:01 +0000578%type <MethodArgList> ArgList ArgListH
579%type <MethArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000580%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000581%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner30c89792001-09-07 16:35:17 +0000582%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000583%type <JumpTable> JumpTable
Chris Lattner1781aca2001-09-18 04:00:54 +0000584%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Chris Lattner00950542001-06-06 20:29:01 +0000585
586%type <ValIDVal> ValueRef ConstValueRef // Reference to a definition or BB
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000587%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000588// Tokens and types for handling constant integer values
589//
590// ESINT64VAL - A negative number within long long range
591%token <SInt64Val> ESINT64VAL
592
593// EUINT64VAL - A positive number within uns. long long range
594%token <UInt64Val> EUINT64VAL
595%type <SInt64Val> EINT64VAL
596
597%token <SIntVal> SINTVAL // Signed 32 bit ints...
598%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
599%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000600%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000601
602// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000603%type <TypeVal> Types TypesV UpRTypes UpRTypesV
604%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
605%token <TypeVal> OPAQUE
606%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
607%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000608
609%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
610%type <StrVal> OptVAR_ID OptAssign
611
612
Chris Lattner1781aca2001-09-18 04:00:54 +0000613%token IMPLEMENTATION TRUE FALSE BEGINTOK END DECLARE GLOBAL CONSTANT UNINIT
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000614%token TO DOTDOTDOT STRING NULL_TOK CONST
Chris Lattner00950542001-06-06 20:29:01 +0000615
616// Basic Block Terminating Operators
617%token <TermOpVal> RET BR SWITCH
618
619// Unary Operators
620%type <UnaryOpVal> UnaryOps // all the unary operators
Chris Lattner71496b32001-07-08 19:03:27 +0000621%token <UnaryOpVal> NOT
Chris Lattner00950542001-06-06 20:29:01 +0000622
623// Binary Operators
624%type <BinaryOpVal> BinaryOps // all the binary operators
625%token <BinaryOpVal> ADD SUB MUL DIV REM
Chris Lattner027dcc52001-07-08 21:10:27 +0000626%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000627
628// Memory Instructions
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000629%token <MemoryOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000630
Chris Lattner027dcc52001-07-08 21:10:27 +0000631// Other Operators
632%type <OtherOpVal> ShiftOps
633%token <OtherOpVal> PHI CALL CAST SHL SHR
634
Chris Lattner00950542001-06-06 20:29:01 +0000635%start Module
636%%
637
638// Handle constant integer size restriction and conversion...
639//
640
641INTVAL : SINTVAL
642INTVAL : UINTVAL {
643 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
644 ThrowException("Value too large for type!");
645 $$ = (int32_t)$1;
646}
647
648
649EINT64VAL : ESINT64VAL // These have same type and can't cause problems...
650EINT64VAL : EUINT64VAL {
651 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
652 ThrowException("Value too large for type!");
653 $$ = (int64_t)$1;
654}
655
Chris Lattner00950542001-06-06 20:29:01 +0000656// Operations that are notably excluded from this list include:
657// RET, BR, & SWITCH because they end basic blocks and are treated specially.
658//
Chris Lattner09083092001-07-08 04:57:15 +0000659UnaryOps : NOT
Chris Lattner00950542001-06-06 20:29:01 +0000660BinaryOps : ADD | SUB | MUL | DIV | REM
661BinaryOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE
Chris Lattner027dcc52001-07-08 21:10:27 +0000662ShiftOps : SHL | SHR
Chris Lattner00950542001-06-06 20:29:01 +0000663
Chris Lattnere98dda62001-07-14 06:10:16 +0000664// These are some types that allow classification if we only want a particular
665// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner00950542001-06-06 20:29:01 +0000666SIntType : LONG | INT | SHORT | SBYTE
667UIntType : ULONG | UINT | USHORT | UBYTE
Chris Lattner30c89792001-09-07 16:35:17 +0000668IntType : SIntType | UIntType
669FPType : FLOAT | DOUBLE
Chris Lattner00950542001-06-06 20:29:01 +0000670
Chris Lattnere98dda62001-07-14 06:10:16 +0000671// OptAssign - Value producing statements have an optional assignment component
Chris Lattner00950542001-06-06 20:29:01 +0000672OptAssign : VAR_ID '=' {
673 $$ = $1;
674 }
675 | /*empty*/ {
676 $$ = 0;
677 }
678
Chris Lattner30c89792001-09-07 16:35:17 +0000679
680//===----------------------------------------------------------------------===//
681// Types includes all predefined types... except void, because it can only be
682// used in specific contexts (method returning void for example). To have
683// access to it, a user must explicitly use TypesV.
684//
685
686// TypesV includes all of 'Types', but it also includes the void type.
687TypesV : Types | VOID { $$ = newTH($1); }
688UpRTypesV : UpRTypes | VOID { $$ = newTH($1); }
689
690Types : UpRTypes {
691 TypeDone($$ = $1);
692 }
693
694
695// Derived types are added later...
696//
697PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT
698PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL
699UpRTypes : OPAQUE | PrimType { $$ = newTH($1); }
700UpRTypes : ValueRef { // Named types are also simple types...
701 $$ = newTH(getTypeVal($1));
702}
703
Chris Lattner30c89792001-09-07 16:35:17 +0000704// Include derived types in the Types production.
705//
706UpRTypes : '\\' EUINT64VAL { // Type UpReference
707 if ($2 > (uint64_t)INT64_MAX) ThrowException("Value out of range!");
708 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
709 UpRefs.push_back(make_pair((unsigned)$2, OT)); // Add to vector...
710 $$ = newTH<Type>(OT);
711 UR_OUT("New Upreference!\n");
712 }
713 | UpRTypesV '(' ArgTypeListI ')' { // Method derived type?
714 vector<const Type*> Params;
715 mapto($3->begin(), $3->end(), back_inserter(Params),
716 mem_fun_ref(&PATypeHandle<Type>::get));
717 $$ = newTH(HandleUpRefs(MethodType::get(*$1, Params)));
718 delete $3; // Delete the argument list
719 delete $1; // Delete the old type handle
720 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000721 | '[' UpRTypesV ']' { // Unsized array type?
722 $$ = newTH<Type>(HandleUpRefs(ArrayType::get(*$2)));
723 delete $2;
Chris Lattner30c89792001-09-07 16:35:17 +0000724 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000725 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
726 $$ = newTH<Type>(HandleUpRefs(ArrayType::get(*$4, (int)$2)));
727 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +0000728 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000729 | '{' TypeListI '}' { // Structure type?
730 vector<const Type*> Elements;
731 mapto($2->begin(), $2->end(), back_inserter(Elements),
732 mem_fun_ref(&PATypeHandle<Type>::get));
Chris Lattner30c89792001-09-07 16:35:17 +0000733
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000734 $$ = newTH<Type>(HandleUpRefs(StructType::get(Elements)));
735 delete $2;
736 }
737 | '{' '}' { // Empty structure type?
738 $$ = newTH<Type>(StructType::get(vector<const Type*>()));
739 }
740 | UpRTypes '*' { // Pointer type?
741 $$ = newTH<Type>(HandleUpRefs(PointerType::get(*$1)));
742 delete $1;
743 }
Chris Lattner30c89792001-09-07 16:35:17 +0000744
745// TypeList - Used for struct declarations and as a basis for method type
746// declaration type lists
747//
748TypeListI : UpRTypes {
749 $$ = new list<PATypeHolder<Type> >();
750 $$->push_back(*$1); delete $1;
751 }
752 | TypeListI ',' UpRTypes {
753 ($$=$1)->push_back(*$3); delete $3;
754 }
755
756// ArgTypeList - List of types for a method type declaration...
757ArgTypeListI : TypeListI
758 | TypeListI ',' DOTDOTDOT {
759 ($$=$1)->push_back(Type::VoidTy);
760 }
761 | DOTDOTDOT {
762 ($$ = new list<PATypeHolder<Type> >())->push_back(Type::VoidTy);
763 }
764 | /*empty*/ {
765 $$ = new list<PATypeHolder<Type> >();
766 }
767
768
Chris Lattnere98dda62001-07-14 06:10:16 +0000769// ConstVal - The various declarations that go into the constant pool. This
770// includes all forward declarations of types, constants, and functions.
771//
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000772ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
773 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
774 if (ATy == 0)
775 ThrowException("Cannot make array constant with type: '" +
776 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +0000777 const Type *ETy = ATy->getElementType();
778 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +0000779
Chris Lattner30c89792001-09-07 16:35:17 +0000780 // Verify that we have the correct size...
781 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +0000782 ThrowException("Type mismatch: constant sized array initialized with " +
Chris Lattner30c89792001-09-07 16:35:17 +0000783 utostr($3->size()) + " arguments, but has size of " +
784 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +0000785
Chris Lattner30c89792001-09-07 16:35:17 +0000786 // Verify all elements are correct type!
787 for (unsigned i = 0; i < $3->size(); i++) {
788 if (ETy != (*$3)[i]->getType())
Chris Lattner00950542001-06-06 20:29:01 +0000789 ThrowException("Element #" + utostr(i) + " is not of type '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000790 ETy->getName() + "' as required!\nIt is of type '" +
791 (*$3)[i]->getType()->getName() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +0000792 }
793
Chris Lattner30c89792001-09-07 16:35:17 +0000794 $$ = ConstPoolArray::get(ATy, *$3);
795 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000796 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000797 | Types '[' ']' {
798 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
799 if (ATy == 0)
800 ThrowException("Cannot make array constant with type: '" +
801 (*$1)->getDescription() + "'!");
802
803 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +0000804 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +0000805 ThrowException("Type mismatch: constant sized array initialized with 0"
Chris Lattner30c89792001-09-07 16:35:17 +0000806 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000807 $$ = ConstPoolArray::get(ATy, vector<ConstPoolVal*>());
Chris Lattner30c89792001-09-07 16:35:17 +0000808 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +0000809 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000810 | Types 'c' STRINGCONSTANT {
811 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
812 if (ATy == 0)
813 ThrowException("Cannot make array constant with type: '" +
814 (*$1)->getDescription() + "'!");
815
Chris Lattner30c89792001-09-07 16:35:17 +0000816 int NumElements = ATy->getNumElements();
817 const Type *ETy = ATy->getElementType();
818 char *EndStr = UnEscapeLexed($3, true);
819 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +0000820 ThrowException("Can't build string constant of size " +
Chris Lattner30c89792001-09-07 16:35:17 +0000821 itostr((int)(EndStr-$3)) +
822 " when array has size " + itostr(NumElements) + "!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000823 vector<ConstPoolVal*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +0000824 if (ETy == Type::SByteTy) {
825 for (char *C = $3; C != EndStr; ++C)
826 Vals.push_back(ConstPoolSInt::get(ETy, *C));
827 } else if (ETy == Type::UByteTy) {
828 for (char *C = $3; C != EndStr; ++C)
829 Vals.push_back(ConstPoolUInt::get(ETy, *C));
Chris Lattner93750fa2001-07-28 17:48:55 +0000830 } else {
Chris Lattner30c89792001-09-07 16:35:17 +0000831 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +0000832 ThrowException("Cannot build string arrays of non byte sized elements!");
833 }
Chris Lattner30c89792001-09-07 16:35:17 +0000834 free($3);
835 $$ = ConstPoolArray::get(ATy, Vals);
836 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +0000837 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000838 | Types '{' ConstVector '}' {
839 const StructType *STy = dyn_cast<const StructType>($1->get());
840 if (STy == 0)
841 ThrowException("Cannot make struct constant with type: '" +
842 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +0000843 // FIXME: TODO: Check to see that the constants are compatible with the type
844 // initializer!
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000845 $$ = ConstPoolStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +0000846 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000847 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000848 | Types NULL_TOK {
849 const PointerType *PTy = dyn_cast<const PointerType>($1->get());
850 if (PTy == 0)
851 ThrowException("Cannot make null pointer constant with type: '" +
852 (*$1)->getDescription() + "'!");
853
854 $$ = ConstPoolPointer::getNullPointer(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000855 delete $1;
856 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000857 | Types VAR_ID {
858 string Name($2); free($2); // Change to a responsible mem manager
859 const PointerType *Ty = dyn_cast<const PointerType>($1->get());
860 if (Ty == 0)
861 ThrowException("Global const reference must be a pointer type!");
862
863 Value *N = lookupInSymbolTable(Ty, Name);
864 if (N == 0)
865 ThrowException("Global pointer reference '%" + Name +
866 "' must be defined before use!");
867
868 // TODO FIXME: This should also allow methods... when common baseclass
869 // exists
870 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(N)) {
871 $$ = ConstPoolPointerReference::get(GV);
872 } else {
873 ThrowException("'%" + Name + "' is not a global value reference!");
874 }
875
876 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +0000877 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000878
Chris Lattner00950542001-06-06 20:29:01 +0000879
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000880ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000881 if (!ConstPoolSInt::isValueValidForType($1, $2))
882 ThrowException("Constant value doesn't fit in type!");
Chris Lattner30c89792001-09-07 16:35:17 +0000883 $$ = ConstPoolSInt::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000884 }
885 | UIntType EUINT64VAL { // integral constants
886 if (!ConstPoolUInt::isValueValidForType($1, $2))
887 ThrowException("Constant value doesn't fit in type!");
Chris Lattner30c89792001-09-07 16:35:17 +0000888 $$ = ConstPoolUInt::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000889 }
890 | BOOL TRUE { // Boolean constants
Chris Lattner30c89792001-09-07 16:35:17 +0000891 $$ = ConstPoolBool::True;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000892 }
893 | BOOL FALSE { // Boolean constants
Chris Lattner30c89792001-09-07 16:35:17 +0000894 $$ = ConstPoolBool::False;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000895 }
896 | FPType FPVAL { // Float & Double constants
Chris Lattner30c89792001-09-07 16:35:17 +0000897 $$ = ConstPoolFP::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000898 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000899
Chris Lattnere98dda62001-07-14 06:10:16 +0000900// ConstVector - A list of comma seperated constants.
Chris Lattner00950542001-06-06 20:29:01 +0000901ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +0000902 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +0000903 }
904 | ConstVal {
905 $$ = new vector<ConstPoolVal*>();
Chris Lattner30c89792001-09-07 16:35:17 +0000906 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +0000907 }
908
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000909
Chris Lattner1781aca2001-09-18 04:00:54 +0000910// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
911GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; }
912
Chris Lattner00950542001-06-06 20:29:01 +0000913
Chris Lattnere98dda62001-07-14 06:10:16 +0000914// ConstPool - Constants with optional names assigned to them.
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000915ConstPool : ConstPool OptAssign CONST ConstVal {
916 setValueName($4, $2);
917 InsertValue($4);
Chris Lattner00950542001-06-06 20:29:01 +0000918 }
Chris Lattner30c89792001-09-07 16:35:17 +0000919 | ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattner1781aca2001-09-18 04:00:54 +0000920 // TODO: FIXME when Type are not const
921 setValueName(const_cast<Type*>($4->get()), $2);
922
923 if (!$2) {
Chris Lattner30c89792001-09-07 16:35:17 +0000924 InsertType($4->get(),
925 CurMeth.CurrentMethod ? CurMeth.Types : CurModule.Types);
926 }
927 delete $4;
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000928
929 ResolveSomeTypes(CurMeth.CurrentMethod ? CurMeth.LateResolveTypes :
930 CurModule.LateResolveTypes);
Chris Lattner30c89792001-09-07 16:35:17 +0000931 }
932 | ConstPool MethodProto { // Method prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +0000933 }
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000934 | ConstPool OptAssign GlobalType ConstVal {
Chris Lattner1781aca2001-09-18 04:00:54 +0000935 const Type *Ty = $4->getType();
936 // Global declarations appear in Constant Pool
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000937 ConstPoolVal *Initializer = $4;
Chris Lattner1781aca2001-09-18 04:00:54 +0000938 if (Initializer == 0)
939 ThrowException("Global value initializer is not a constant!");
940
941 GlobalVariable *GV = new GlobalVariable(PointerType::get(Ty), $3,
942 Initializer);
943 setValueName(GV, $2);
944
945 CurModule.CurrentModule->getGlobalList().push_back(GV);
946 InsertValue(GV, CurModule.Values);
947 }
948 | ConstPool OptAssign UNINIT GlobalType Types {
949 const Type *Ty = *$5;
950 // Global declarations appear in Constant Pool
Chris Lattnercfe26c92001-10-01 18:26:53 +0000951 if (isa<ArrayType>(Ty) && cast<ArrayType>(Ty)->isUnsized()) {
Chris Lattner1781aca2001-09-18 04:00:54 +0000952 ThrowException("Type '" + Ty->getDescription() +
953 "' is not a sized type!");
Chris Lattner70cc3392001-09-10 07:58:01 +0000954 }
Chris Lattner1781aca2001-09-18 04:00:54 +0000955
956 GlobalVariable *GV = new GlobalVariable(PointerType::get(Ty), $4);
957 setValueName(GV, $2);
958
Chris Lattner70cc3392001-09-10 07:58:01 +0000959 CurModule.CurrentModule->getGlobalList().push_back(GV);
960 InsertValue(GV, CurModule.Values);
Chris Lattnere98dda62001-07-14 06:10:16 +0000961 }
Chris Lattner00950542001-06-06 20:29:01 +0000962 | /* empty: end of list */ {
963 }
964
965
966//===----------------------------------------------------------------------===//
967// Rules to match Modules
968//===----------------------------------------------------------------------===//
969
970// Module rule: Capture the result of parsing the whole file into a result
971// variable...
972//
973Module : MethodList {
974 $$ = ParserResult = $1;
975 CurModule.ModuleDone();
976}
977
Chris Lattnere98dda62001-07-14 06:10:16 +0000978// MethodList - A list of methods, preceeded by a constant pool.
979//
Chris Lattner00950542001-06-06 20:29:01 +0000980MethodList : MethodList Method {
Chris Lattner00950542001-06-06 20:29:01 +0000981 $$ = $1;
Chris Lattnere1815642001-07-15 06:35:53 +0000982 if (!$2->getParent())
983 $1->getMethodList().push_back($2);
984 CurMeth.MethodDone();
Chris Lattner00950542001-06-06 20:29:01 +0000985 }
Chris Lattnere1815642001-07-15 06:35:53 +0000986 | MethodList MethodProto {
987 $$ = $1;
Chris Lattnere1815642001-07-15 06:35:53 +0000988 }
Chris Lattner00950542001-06-06 20:29:01 +0000989 | ConstPool IMPLEMENTATION {
990 $$ = CurModule.CurrentModule;
Chris Lattner30c89792001-09-07 16:35:17 +0000991 // Resolve circular types before we parse the body of the module
992 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner00950542001-06-06 20:29:01 +0000993 }
994
995
996//===----------------------------------------------------------------------===//
997// Rules to match Method Headers
998//===----------------------------------------------------------------------===//
999
1000OptVAR_ID : VAR_ID | /*empty*/ { $$ = 0; }
1001
1002ArgVal : Types OptVAR_ID {
Chris Lattner30c89792001-09-07 16:35:17 +00001003 $$ = new MethodArgument(*$1); delete $1;
Chris Lattner1781aca2001-09-18 04:00:54 +00001004 setValueName($$, $2);
Chris Lattner00950542001-06-06 20:29:01 +00001005}
1006
1007ArgListH : ArgVal ',' ArgListH {
1008 $$ = $3;
1009 $3->push_front($1);
1010 }
1011 | ArgVal {
1012 $$ = new list<MethodArgument*>();
1013 $$->push_front($1);
1014 }
Chris Lattner8b81bf52001-07-25 22:47:46 +00001015 | DOTDOTDOT {
1016 $$ = new list<MethodArgument*>();
1017 $$->push_back(new MethodArgument(Type::VoidTy));
1018 }
Chris Lattner00950542001-06-06 20:29:01 +00001019
1020ArgList : ArgListH {
1021 $$ = $1;
1022 }
1023 | /* empty */ {
1024 $$ = 0;
1025 }
1026
1027MethodHeaderH : TypesV STRINGCONSTANT '(' ArgList ')' {
Chris Lattner93750fa2001-07-28 17:48:55 +00001028 UnEscapeLexed($2);
Chris Lattner30c89792001-09-07 16:35:17 +00001029 vector<const Type*> ParamTypeList;
Chris Lattner00950542001-06-06 20:29:01 +00001030 if ($4)
Chris Lattner7fc9fe32001-06-27 23:41:11 +00001031 for (list<MethodArgument*>::iterator I = $4->begin(); I != $4->end(); ++I)
Chris Lattner00950542001-06-06 20:29:01 +00001032 ParamTypeList.push_back((*I)->getType());
1033
Chris Lattner30c89792001-09-07 16:35:17 +00001034 const MethodType *MT = MethodType::get(*$1, ParamTypeList);
1035 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001036
Chris Lattnere1815642001-07-15 06:35:53 +00001037 Method *M = 0;
1038 if (SymbolTable *ST = CurModule.CurrentModule->getSymbolTable()) {
1039 if (Value *V = ST->lookup(MT, $2)) { // Method already in symtab?
Chris Lattner9636a912001-10-01 16:18:37 +00001040 M = cast<Method>(V);
Chris Lattner00950542001-06-06 20:29:01 +00001041
Chris Lattnere1815642001-07-15 06:35:53 +00001042 // Yes it is. If this is the case, either we need to be a forward decl,
1043 // or it needs to be.
1044 if (!CurMeth.isDeclare && !M->isExternal())
1045 ThrowException("Redefinition of method '" + string($2) + "'!");
1046 }
1047 }
1048
1049 if (M == 0) { // Not already defined?
1050 M = new Method(MT, $2);
1051 InsertValue(M, CurModule.Values);
1052 }
1053
1054 free($2); // Free strdup'd memory!
Chris Lattner00950542001-06-06 20:29:01 +00001055
1056 CurMeth.MethodStart(M);
1057
1058 // Add all of the arguments we parsed to the method...
Chris Lattnere1815642001-07-15 06:35:53 +00001059 if ($4 && !CurMeth.isDeclare) { // Is null if empty...
Chris Lattner00950542001-06-06 20:29:01 +00001060 Method::ArgumentListType &ArgList = M->getArgumentList();
1061
Chris Lattner7fc9fe32001-06-27 23:41:11 +00001062 for (list<MethodArgument*>::iterator I = $4->begin(); I != $4->end(); ++I) {
Chris Lattner00950542001-06-06 20:29:01 +00001063 InsertValue(*I);
1064 ArgList.push_back(*I);
1065 }
1066 delete $4; // We're now done with the argument list
1067 }
1068}
1069
1070MethodHeader : MethodHeaderH ConstPool BEGINTOK {
1071 $$ = CurMeth.CurrentMethod;
Chris Lattner30c89792001-09-07 16:35:17 +00001072
1073 // Resolve circular types before we parse the body of the method.
1074 ResolveTypes(CurMeth.LateResolveTypes);
Chris Lattner00950542001-06-06 20:29:01 +00001075}
1076
1077Method : BasicBlockList END {
1078 $$ = $1;
1079}
1080
Chris Lattnere1815642001-07-15 06:35:53 +00001081MethodProto : DECLARE { CurMeth.isDeclare = true; } MethodHeaderH {
1082 $$ = CurMeth.CurrentMethod;
Chris Lattner93750fa2001-07-28 17:48:55 +00001083 if (!$$->getParent())
1084 CurModule.CurrentModule->getMethodList().push_back($$);
1085 CurMeth.MethodDone();
Chris Lattnere1815642001-07-15 06:35:53 +00001086}
Chris Lattner00950542001-06-06 20:29:01 +00001087
1088//===----------------------------------------------------------------------===//
1089// Rules to match Basic Blocks
1090//===----------------------------------------------------------------------===//
1091
1092ConstValueRef : ESINT64VAL { // A reference to a direct constant
1093 $$ = ValID::create($1);
1094 }
1095 | EUINT64VAL {
1096 $$ = ValID::create($1);
1097 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001098 | FPVAL { // Perhaps it's an FP constant?
1099 $$ = ValID::create($1);
1100 }
Chris Lattner00950542001-06-06 20:29:01 +00001101 | TRUE {
1102 $$ = ValID::create((int64_t)1);
1103 }
1104 | FALSE {
1105 $$ = ValID::create((int64_t)0);
1106 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001107 | NULL_TOK {
1108 $$ = ValID::createNull();
1109 }
1110
Chris Lattner93750fa2001-07-28 17:48:55 +00001111/*
Chris Lattner00950542001-06-06 20:29:01 +00001112 | STRINGCONSTANT { // Quoted strings work too... especially for methods
1113 $$ = ValID::create_conststr($1);
1114 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001115*/
Chris Lattner00950542001-06-06 20:29:01 +00001116
1117// ValueRef - A reference to a definition...
1118ValueRef : INTVAL { // Is it an integer reference...?
1119 $$ = ValID::create($1);
1120 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001121 | VAR_ID { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001122 $$ = ValID::create($1);
1123 }
1124 | ConstValueRef {
1125 $$ = $1;
1126 }
1127
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001128// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1129// type immediately preceeds the value reference, and allows complex constant
1130// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001131ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001132 $$ = getVal(*$1, $2); delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +00001133 }
Chris Lattner8b81bf52001-07-25 22:47:46 +00001134
Chris Lattner00950542001-06-06 20:29:01 +00001135
1136BasicBlockList : BasicBlockList BasicBlock {
1137 $1->getBasicBlocks().push_back($2);
1138 $$ = $1;
1139 }
1140 | MethodHeader BasicBlock { // Do not allow methods with 0 basic blocks
1141 $$ = $1; // in them...
1142 $1->getBasicBlocks().push_back($2);
1143 }
1144
1145
1146// Basic blocks are terminated by branching instructions:
1147// br, br/cc, switch, ret
1148//
1149BasicBlock : InstructionList BBTerminatorInst {
1150 $1->getInstList().push_back($2);
1151 InsertValue($1);
1152 $$ = $1;
1153 }
1154 | LABELSTR InstructionList BBTerminatorInst {
1155 $2->getInstList().push_back($3);
Chris Lattner30c89792001-09-07 16:35:17 +00001156 setValueName($2, $1);
Chris Lattner00950542001-06-06 20:29:01 +00001157
1158 InsertValue($2);
1159 $$ = $2;
1160 }
1161
1162InstructionList : InstructionList Inst {
1163 $1->getInstList().push_back($2);
1164 $$ = $1;
1165 }
1166 | /* empty */ {
1167 $$ = new BasicBlock();
1168 }
1169
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001170BBTerminatorInst : RET ResolvedVal { // Return with a result...
1171 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001172 }
1173 | RET VOID { // Return with no result...
1174 $$ = new ReturnInst();
1175 }
1176 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner9636a912001-10-01 16:18:37 +00001177 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $3)));
Chris Lattner00950542001-06-06 20:29:01 +00001178 } // Conditional Branch...
1179 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner9636a912001-10-01 16:18:37 +00001180 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $6)),
1181 cast<BasicBlock>(getVal(Type::LabelTy, $9)),
Chris Lattner00950542001-06-06 20:29:01 +00001182 getVal(Type::BoolTy, $3));
1183 }
1184 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1185 SwitchInst *S = new SwitchInst(getVal($2, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001186 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
Chris Lattner00950542001-06-06 20:29:01 +00001187 $$ = S;
1188
1189 list<pair<ConstPoolVal*, BasicBlock*> >::iterator I = $8->begin(),
1190 end = $8->end();
Chris Lattner7fc9fe32001-06-27 23:41:11 +00001191 for (; I != end; ++I)
Chris Lattner00950542001-06-06 20:29:01 +00001192 S->dest_push_back(I->first, I->second);
1193 }
1194
1195JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1196 $$ = $1;
Chris Lattnercfe26c92001-10-01 18:26:53 +00001197 ConstPoolVal *V = cast<ConstPoolVal>(getVal($2, $3, true));
Chris Lattner00950542001-06-06 20:29:01 +00001198 if (V == 0)
1199 ThrowException("May only switch on a constant pool value!");
1200
Chris Lattner9636a912001-10-01 16:18:37 +00001201 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($5, $6))));
Chris Lattner00950542001-06-06 20:29:01 +00001202 }
1203 | IntType ConstValueRef ',' LABEL ValueRef {
1204 $$ = new list<pair<ConstPoolVal*, BasicBlock*> >();
Chris Lattnercfe26c92001-10-01 18:26:53 +00001205 ConstPoolVal *V = cast<ConstPoolVal>(getVal($1, $2, true));
Chris Lattner00950542001-06-06 20:29:01 +00001206
1207 if (V == 0)
1208 ThrowException("May only switch on a constant pool value!");
1209
Chris Lattner9636a912001-10-01 16:18:37 +00001210 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($4, $5))));
Chris Lattner00950542001-06-06 20:29:01 +00001211 }
1212
1213Inst : OptAssign InstVal {
Chris Lattner1781aca2001-09-18 04:00:54 +00001214 setValueName($2, $1); // Is this definition named?? if so, assign the name...
Chris Lattner00950542001-06-06 20:29:01 +00001215
1216 InsertValue($2);
1217 $$ = $2;
1218}
1219
Chris Lattnerc24d2082001-06-11 15:04:20 +00001220PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
1221 $$ = new list<pair<Value*, BasicBlock*> >();
Chris Lattner30c89792001-09-07 16:35:17 +00001222 $$->push_back(make_pair(getVal(*$1, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001223 cast<BasicBlock>(getVal(Type::LabelTy, $5))));
Chris Lattner30c89792001-09-07 16:35:17 +00001224 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001225 }
1226 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1227 $$ = $1;
1228 $1->push_back(make_pair(getVal($1->front().first->getType(), $4),
Chris Lattner9636a912001-10-01 16:18:37 +00001229 cast<BasicBlock>(getVal(Type::LabelTy, $6))));
Chris Lattnerc24d2082001-06-11 15:04:20 +00001230 }
1231
1232
Chris Lattner30c89792001-09-07 16:35:17 +00001233ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner00950542001-06-06 20:29:01 +00001234 $$ = new list<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001235 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001236 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001237 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001238 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001239 $1->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001240 }
1241
1242// ValueRefListE - Just like ValueRefList, except that it may also be empty!
1243ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; }
1244
1245InstVal : BinaryOps Types ValueRef ',' ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001246 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001247 if ($$ == 0)
1248 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001249 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001250 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001251 | UnaryOps ResolvedVal {
1252 $$ = UnaryOperator::create($1, $2);
Chris Lattner00950542001-06-06 20:29:01 +00001253 if ($$ == 0)
1254 ThrowException("unary operator returned null!");
Chris Lattner09083092001-07-08 04:57:15 +00001255 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001256 | ShiftOps ResolvedVal ',' ResolvedVal {
1257 if ($4->getType() != Type::UByteTy)
1258 ThrowException("Shift amount must be ubyte!");
1259 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001260 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001261 | CAST ResolvedVal TO Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001262 $$ = new CastInst($2, *$4);
1263 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001264 }
Chris Lattnerc24d2082001-06-11 15:04:20 +00001265 | PHI PHIList {
1266 const Type *Ty = $2->front().first->getType();
1267 $$ = new PHINode(Ty);
Chris Lattner00950542001-06-06 20:29:01 +00001268 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001269 if ($2->front().first->getType() != Ty)
1270 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00001271 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001272 $2->pop_front();
1273 }
1274 delete $2; // Free the list...
1275 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001276 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001277 const MethodType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001278
Chris Lattner9636a912001-10-01 16:18:37 +00001279 if (!(Ty = dyn_cast<MethodType>($2->get()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001280 // Pull out the types of all of the arguments...
1281 vector<const Type*> ParamTypes;
1282 for (list<Value*>::iterator I = $5->begin(), E = $5->end(); I != E; ++I)
1283 ParamTypes.push_back((*I)->getType());
Chris Lattner30c89792001-09-07 16:35:17 +00001284 Ty = MethodType::get(*$2, ParamTypes);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001285 }
Chris Lattner30c89792001-09-07 16:35:17 +00001286 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001287
Chris Lattner8b81bf52001-07-25 22:47:46 +00001288 Value *V = getVal(Ty, $3); // Get the method we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00001289
Chris Lattner8b81bf52001-07-25 22:47:46 +00001290 // Create the call node...
1291 if (!$5) { // Has no arguments?
Chris Lattner9636a912001-10-01 16:18:37 +00001292 $$ = new CallInst(cast<Method>(V), vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00001293 } else { // Has arguments?
Chris Lattner00950542001-06-06 20:29:01 +00001294 // Loop through MethodType's arguments and ensure they are specified
1295 // correctly!
1296 //
1297 MethodType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001298 MethodType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
1299 list<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
1300
1301 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1302 if ((*ArgI)->getType() != *I)
1303 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner00950542001-06-06 20:29:01 +00001304 (*I)->getName() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00001305
Chris Lattner8b81bf52001-07-25 22:47:46 +00001306 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner00950542001-06-06 20:29:01 +00001307 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00001308
Chris Lattner9636a912001-10-01 16:18:37 +00001309 $$ = new CallInst(cast<Method>(V),
Chris Lattner8b81bf52001-07-25 22:47:46 +00001310 vector<Value*>($5->begin(), $5->end()));
1311 }
1312 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00001313 }
1314 | MemoryInst {
1315 $$ = $1;
1316 }
1317
Chris Lattner027dcc52001-07-08 21:10:27 +00001318// UByteList - List of ubyte values for load and store instructions
1319UByteList : ',' ConstVector {
1320 $$ = $2;
1321} | /* empty */ {
1322 $$ = new vector<ConstPoolVal*>();
1323}
1324
Chris Lattner00950542001-06-06 20:29:01 +00001325MemoryInst : MALLOC Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001326 $$ = new MallocInst(PointerType::get(*$2));
1327 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001328 }
1329 | MALLOC Types ',' UINT ValueRef {
Chris Lattnerb00c5822001-10-02 03:41:24 +00001330 if (!(*$2)->isArrayType() || cast<const ArrayType>($2->get())->isSized())
Chris Lattner30c89792001-09-07 16:35:17 +00001331 ThrowException("Trying to allocate " + (*$2)->getName() +
Chris Lattner00950542001-06-06 20:29:01 +00001332 " as unsized array!");
Chris Lattner30c89792001-09-07 16:35:17 +00001333 const Type *Ty = PointerType::get(*$2);
Chris Lattner8896eda2001-07-09 19:38:36 +00001334 $$ = new MallocInst(Ty, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001335 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001336 }
1337 | ALLOCA Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001338 $$ = new AllocaInst(PointerType::get(*$2));
1339 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001340 }
1341 | ALLOCA 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 Lattner00950542001-06-06 20:29:01 +00001346 Value *ArrSize = getVal($4, $5);
Chris Lattnerf0d0e9c2001-07-07 08:36:30 +00001347 $$ = new AllocaInst(Ty, ArrSize);
Chris Lattner30c89792001-09-07 16:35:17 +00001348 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001349 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001350 | FREE ResolvedVal {
1351 if (!$2->getType()->isPointerType())
1352 ThrowException("Trying to free nonpointer type " +
1353 $2->getType()->getName() + "!");
1354 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001355 }
1356
Chris Lattner027dcc52001-07-08 21:10:27 +00001357 | LOAD Types ValueRef UByteList {
Chris Lattner30c89792001-09-07 16:35:17 +00001358 if (!(*$2)->isPointerType())
1359 ThrowException("Can't load from nonpointer type: " + (*$2)->getName());
1360 if (LoadInst::getIndexedType(*$2, *$4) == 0)
Chris Lattner027dcc52001-07-08 21:10:27 +00001361 ThrowException("Invalid indices for load instruction!");
1362
Chris Lattner30c89792001-09-07 16:35:17 +00001363 $$ = new LoadInst(getVal(*$2, $3), *$4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001364 delete $4; // Free the vector...
Chris Lattner30c89792001-09-07 16:35:17 +00001365 delete $2;
Chris Lattner027dcc52001-07-08 21:10:27 +00001366 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001367 | STORE ResolvedVal ',' Types ValueRef UByteList {
Chris Lattner30c89792001-09-07 16:35:17 +00001368 if (!(*$4)->isPointerType())
1369 ThrowException("Can't store to a nonpointer type: " + (*$4)->getName());
1370 const Type *ElTy = StoreInst::getIndexedType(*$4, *$6);
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001371 if (ElTy == 0)
1372 ThrowException("Can't store into that field list!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001373 if (ElTy != $2->getType())
1374 ThrowException("Can't store '" + $2->getType()->getName() +
1375 "' into space of type '" + ElTy->getName() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001376 $$ = new StoreInst($2, getVal(*$4, $5), *$6);
1377 delete $4; delete $6;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001378 }
1379 | GETELEMENTPTR Types ValueRef UByteList {
Chris Lattner30c89792001-09-07 16:35:17 +00001380 if (!(*$2)->isPointerType())
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001381 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner30c89792001-09-07 16:35:17 +00001382 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
1383 ThrowException("Can't get element ptr '" + (*$2)->getName() + "'!");
1384 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
1385 delete $2; delete $4;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001386 }
Chris Lattner027dcc52001-07-08 21:10:27 +00001387
Chris Lattner00950542001-06-06 20:29:01 +00001388%%
Chris Lattner09083092001-07-08 04:57:15 +00001389int yyerror(const char *ErrorMsg) {
Chris Lattner00950542001-06-06 20:29:01 +00001390 ThrowException(string("Parse error: ") + ErrorMsg);
1391 return 0;
1392}