blob: 698fd7b2c05d978d19ee5a186206df71d44ac3a2 [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
178static Value *getVal(const Type *Ty, const ValID &D,
179 bool DoNotImprovise = false) {
180 assert(Ty != Type::TypeTy && "Should use getTypeVal for types!");
181
182 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000183 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000184 unsigned type = Ty->getUniqueID();
Chris Lattner00950542001-06-06 20:29:01 +0000185 unsigned Num = (unsigned)D.Num;
186
187 // Module constants occupy the lowest numbered slots...
188 if (type < CurModule.Values.size()) {
189 if (Num < CurModule.Values[type].size())
190 return CurModule.Values[type][Num];
191
192 Num -= CurModule.Values[type].size();
193 }
194
195 // Make sure that our type is within bounds
196 if (CurMeth.Values.size() <= type)
197 break;
198
199 // Check that the number is within bounds...
200 if (CurMeth.Values[type].size() <= Num)
201 break;
202
203 return CurMeth.Values[type][Num];
204 }
Chris Lattner1a1cb112001-09-30 22:46:54 +0000205 case ValID::NameVal: { // Is it a named definition?
Chris Lattner00950542001-06-06 20:29:01 +0000206 string Name(D.Name);
207 SymbolTable *SymTab = 0;
208 if (CurMeth.CurrentMethod)
209 SymTab = CurMeth.CurrentMethod->getSymbolTable();
Chris Lattner30c89792001-09-07 16:35:17 +0000210 Value *N = SymTab ? SymTab->lookup(Ty, Name) : 0;
Chris Lattner00950542001-06-06 20:29:01 +0000211
212 if (N == 0) {
Chris Lattner30c89792001-09-07 16:35:17 +0000213 // Symbol table doesn't automatically chain yet... because the method
214 // hasn't been added to the module...
215 //
Chris Lattner00950542001-06-06 20:29:01 +0000216 SymTab = CurModule.CurrentModule->getSymbolTable();
217 if (SymTab)
Chris Lattner30c89792001-09-07 16:35:17 +0000218 N = SymTab->lookup(Ty, Name);
Chris Lattner00950542001-06-06 20:29:01 +0000219 if (N == 0) break;
220 }
221
222 D.destroy(); // Free old strdup'd memory...
223 return N;
224 }
225
Chris Lattner1a1cb112001-09-30 22:46:54 +0000226 case ValID::ConstSIntVal: // Is it a constant pool reference??
227 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
228 case ValID::ConstStringVal: // Is it a string const pool reference?
229 case ValID::ConstFPVal: // Is it a floating point const pool reference?
230 case ValID::ConstNullVal: { // Is it a null value?
Chris Lattner00950542001-06-06 20:29:01 +0000231 ConstPoolVal *CPV = 0;
232
Chris Lattner30c89792001-09-07 16:35:17 +0000233 // Check to make sure that "Ty" is an integral type, and that our
Chris Lattner00950542001-06-06 20:29:01 +0000234 // value will fit into the specified type...
235 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000236 case ValID::ConstSIntVal:
Chris Lattner30c89792001-09-07 16:35:17 +0000237 if (Ty == Type::BoolTy) { // Special handling for boolean data
238 CPV = ConstPoolBool::get(D.ConstPool64 != 0);
Chris Lattner00950542001-06-06 20:29:01 +0000239 } else {
Chris Lattner30c89792001-09-07 16:35:17 +0000240 if (!ConstPoolSInt::isValueValidForType(Ty, D.ConstPool64))
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000241 ThrowException("Symbolic constant pool value '" +
242 itostr(D.ConstPool64) + "' is invalid for type '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000243 Ty->getName() + "'!");
244 CPV = ConstPoolSInt::get(Ty, D.ConstPool64);
Chris Lattner00950542001-06-06 20:29:01 +0000245 }
246 break;
Chris Lattner1a1cb112001-09-30 22:46:54 +0000247 case ValID::ConstUIntVal:
Chris Lattner30c89792001-09-07 16:35:17 +0000248 if (!ConstPoolUInt::isValueValidForType(Ty, D.UConstPool64)) {
249 if (!ConstPoolSInt::isValueValidForType(Ty, D.ConstPool64)) {
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000250 ThrowException("Integral constant pool reference is invalid!");
Chris Lattner00950542001-06-06 20:29:01 +0000251 } else { // This is really a signed reference. Transmogrify.
Chris Lattner30c89792001-09-07 16:35:17 +0000252 CPV = ConstPoolSInt::get(Ty, D.ConstPool64);
Chris Lattner00950542001-06-06 20:29:01 +0000253 }
254 } else {
Chris Lattner30c89792001-09-07 16:35:17 +0000255 CPV = ConstPoolUInt::get(Ty, D.UConstPool64);
Chris Lattner00950542001-06-06 20:29:01 +0000256 }
257 break;
Chris Lattner1a1cb112001-09-30 22:46:54 +0000258 case ValID::ConstStringVal:
Chris Lattner00950542001-06-06 20:29:01 +0000259 cerr << "FIXME: TODO: String constants [sbyte] not implemented yet!\n";
260 abort();
Chris Lattner00950542001-06-06 20:29:01 +0000261 break;
Chris Lattner1a1cb112001-09-30 22:46:54 +0000262 case ValID::ConstFPVal:
Chris Lattner30c89792001-09-07 16:35:17 +0000263 if (!ConstPoolFP::isValueValidForType(Ty, D.ConstPoolFP))
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000264 ThrowException("FP constant invalid for type!!");
Chris Lattner1a1cb112001-09-30 22:46:54 +0000265 CPV = ConstPoolFP::get(Ty, D.ConstPoolFP);
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000266 break;
Chris Lattner1a1cb112001-09-30 22:46:54 +0000267 case ValID::ConstNullVal:
268 if (!Ty->isPointerType())
269 ThrowException("Cannot create a a non pointer null!");
Chris Lattnercfe26c92001-10-01 18:26:53 +0000270 CPV = ConstPoolPointer::getNullPointer(cast<PointerType>(Ty));
Chris Lattner1a1cb112001-09-30 22:46:54 +0000271 break;
272 default:
273 assert(0 && "Unhandled case!");
Chris Lattner00950542001-06-06 20:29:01 +0000274 }
275 assert(CPV && "How did we escape creating a constant??");
Chris Lattner00950542001-06-06 20:29:01 +0000276 return CPV;
277 } // End of case 2,3,4
Chris Lattner30c89792001-09-07 16:35:17 +0000278 default:
279 assert(0 && "Unhandled case!");
Chris Lattner00950542001-06-06 20:29:01 +0000280 } // End of switch
281
282
283 // If we reached here, we referenced either a symbol that we don't know about
284 // or an id number that hasn't been read yet. We may be referencing something
285 // forward, so just create an entry to be resolved later and get to it...
286 //
287 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
288
Chris Lattner00950542001-06-06 20:29:01 +0000289 Value *d = 0;
Chris Lattner30c89792001-09-07 16:35:17 +0000290 vector<ValueList> *LateResolver = (CurMeth.CurrentMethod) ?
291 &CurMeth.LateResolveValues : &CurModule.LateResolveValues;
Chris Lattner93750fa2001-07-28 17:48:55 +0000292
Chris Lattner30c89792001-09-07 16:35:17 +0000293 switch (Ty->getPrimitiveID()) {
294 case Type::LabelTyID: d = new BBPlaceHolder(Ty, D); break;
295 case Type::MethodTyID: d = new MethPlaceHolder(Ty, D);
Chris Lattner93750fa2001-07-28 17:48:55 +0000296 LateResolver = &CurModule.LateResolveValues; break;
Chris Lattner30c89792001-09-07 16:35:17 +0000297 default: d = new ValuePlaceHolder(Ty, D); break;
Chris Lattner00950542001-06-06 20:29:01 +0000298 }
299
300 assert(d != 0 && "How did we not make something?");
Chris Lattner93750fa2001-07-28 17:48:55 +0000301 InsertValue(d, *LateResolver);
Chris Lattner00950542001-06-06 20:29:01 +0000302 return d;
303}
304
305
306//===----------------------------------------------------------------------===//
307// Code to handle forward references in instructions
308//===----------------------------------------------------------------------===//
309//
310// This code handles the late binding needed with statements that reference
311// values not defined yet... for example, a forward branch, or the PHI node for
312// a loop body.
313//
314// This keeps a table (CurMeth.LateResolveValues) of all such forward references
315// and back patchs after we are done.
316//
317
318// ResolveDefinitions - If we could not resolve some defs at parsing
319// time (forward branches, phi functions for loops, etc...) resolve the
320// defs now...
321//
322static void ResolveDefinitions(vector<ValueList> &LateResolvers) {
323 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
324 for (unsigned ty = 0; ty < LateResolvers.size(); ty++) {
325 while (!LateResolvers[ty].empty()) {
326 Value *V = LateResolvers[ty].back();
327 LateResolvers[ty].pop_back();
328 ValID &DID = getValIDFromPlaceHolder(V);
329
330 Value *TheRealValue = getVal(Type::getUniqueIDType(ty), DID, true);
331
Chris Lattner30c89792001-09-07 16:35:17 +0000332 if (TheRealValue == 0) {
333 if (DID.Type == 1)
334 ThrowException("Reference to an invalid definition: '" +DID.getName()+
335 "' of type '" + V->getType()->getDescription() + "'",
336 getLineNumFromPlaceHolder(V));
337 else
338 ThrowException("Reference to an invalid definition: #" +
339 itostr(DID.Num) + " of type '" +
340 V->getType()->getDescription() + "'",
341 getLineNumFromPlaceHolder(V));
342 }
343
Chris Lattnercfe26c92001-10-01 18:26:53 +0000344 assert(!isa<Type>(V) && "Types should be in LateResolveTypes!");
Chris Lattner00950542001-06-06 20:29:01 +0000345
346 V->replaceAllUsesWith(TheRealValue);
Chris Lattner00950542001-06-06 20:29:01 +0000347 delete V;
348 }
349 }
350
351 LateResolvers.clear();
352}
353
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000354// ResolveType - Take a specified unresolved type and resolve it. If there is
355// nothing to resolve it to yet, return true. Otherwise resolve it and return
356// false.
357//
358static bool ResolveType(PATypeHolder<Type> &T) {
359 const Type *Ty = T;
360 ValID &DID = getValIDFromPlaceHolder(Ty);
361
362 const Type *TheRealType = getTypeVal(DID, true);
363 if (TheRealType == 0) return true;
364
365 // Refine the opaque type we had to the new type we are getting.
366 cast<DerivedType>(Ty)->refineAbstractTypeTo(TheRealType);
367 return false;
368}
369
Chris Lattner30c89792001-09-07 16:35:17 +0000370
371// ResolveTypes - This goes through the forward referenced type table and makes
372// sure that all type references are complete. This code is executed after the
373// constant pool of a method or module is completely parsed.
Chris Lattner00950542001-06-06 20:29:01 +0000374//
Chris Lattner30c89792001-09-07 16:35:17 +0000375static void ResolveTypes(vector<PATypeHolder<Type> > &LateResolveTypes) {
376 while (!LateResolveTypes.empty()) {
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000377 if (ResolveType(LateResolveTypes.back())) {
378 const Type *Ty = LateResolveTypes.back();
379 ValID &DID = getValIDFromPlaceHolder(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000380
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000381 if (DID.Type == ValID::NameVal)
Chris Lattner30c89792001-09-07 16:35:17 +0000382 ThrowException("Reference to an invalid type: '" +DID.getName(),
383 getLineNumFromPlaceHolder(Ty));
384 else
385 ThrowException("Reference to an invalid type: #" + itostr(DID.Num),
386 getLineNumFromPlaceHolder(Ty));
Chris Lattner00950542001-06-06 20:29:01 +0000387 }
Chris Lattner30c89792001-09-07 16:35:17 +0000388
Chris Lattner30c89792001-09-07 16:35:17 +0000389 // No need to delete type, refine does that for us.
390 LateResolveTypes.pop_back();
391 }
392}
393
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000394
395// ResolveSomeTypes - This goes through the forward referenced type table and
396// completes references that are now done. This is so that types are
397// immediately resolved to be as concrete as possible. This does not cause
398// thrown exceptions if not everything is resolved.
399//
400static void ResolveSomeTypes(vector<PATypeHolder<Type> > &LateResolveTypes) {
401 for (unsigned i = 0; i < LateResolveTypes.size(); ) {
402 if (ResolveType(LateResolveTypes[i]))
403 ++i; // Type didn't resolve
404 else
405 LateResolveTypes.erase(LateResolveTypes.begin()+i); // Type resolved!
406 }
407}
408
409
Chris Lattner1781aca2001-09-18 04:00:54 +0000410// setValueName - Set the specified value to the name given. The name may be
411// null potentially, in which case this is a noop. The string passed in is
412// assumed to be a malloc'd string buffer, and is freed by this function.
413//
414static void setValueName(Value *V, char *NameStr) {
415 if (NameStr == 0) return;
416 string Name(NameStr); // Copy string
417 free(NameStr); // Free old string
418
Chris Lattner30c89792001-09-07 16:35:17 +0000419 SymbolTable *ST = CurMeth.CurrentMethod ?
420 CurMeth.CurrentMethod->getSymbolTableSure() :
421 CurModule.CurrentModule->getSymbolTableSure();
422
423 Value *Existing = ST->lookup(V->getType(), Name);
424 if (Existing) { // Inserting a name that is already defined???
425 // There is only one case where this is allowed: when we are refining an
426 // opaque type. In this case, Existing will be an opaque type.
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000427 if (const Type *Ty = dyn_cast<const Type>(Existing)) {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000428 if (OpaqueType *OpTy = dyn_cast<OpaqueType>(Ty)) {
Chris Lattner30c89792001-09-07 16:35:17 +0000429 // We ARE replacing an opaque type!
Chris Lattnerb00c5822001-10-02 03:41:24 +0000430 OpTy->refineAbstractTypeTo(cast<Type>(V));
Chris Lattner30c89792001-09-07 16:35:17 +0000431 return;
432 }
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000433 }
Chris Lattner30c89792001-09-07 16:35:17 +0000434
Chris Lattner9636a912001-10-01 16:18:37 +0000435 // Otherwise, we are a simple redefinition of a value, check to see if it
436 // is defined the same as the old one...
437 if (const Type *Ty = dyn_cast<const Type>(Existing)) {
438 if (Ty == cast<const Type>(V)) return; // Yes, it's equal.
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000439 cerr << "Type: " << Ty->getDescription() << " != "
440 << cast<const Type>(V)->getDescription() << "!\n";
Chris Lattner9636a912001-10-01 16:18:37 +0000441 } else {
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000442
Chris Lattner9636a912001-10-01 16:18:37 +0000443 }
Chris Lattner30c89792001-09-07 16:35:17 +0000444 ThrowException("Redefinition of value name '" + Name + "' in the '" +
445 V->getType()->getDescription() + "' type plane!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000446 }
Chris Lattner00950542001-06-06 20:29:01 +0000447
Chris Lattner30c89792001-09-07 16:35:17 +0000448 V->setName(Name, ST);
Chris Lattner00950542001-06-06 20:29:01 +0000449}
450
Chris Lattner8896eda2001-07-09 19:38:36 +0000451
Chris Lattner30c89792001-09-07 16:35:17 +0000452//===----------------------------------------------------------------------===//
453// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000454//
Chris Lattner8896eda2001-07-09 19:38:36 +0000455
Chris Lattner30c89792001-09-07 16:35:17 +0000456// TypeContains - Returns true if Ty contains E in it.
457//
458static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattner3ff43872001-09-28 22:56:31 +0000459 return find(df_begin(Ty), df_end(Ty), E) != df_end(Ty);
Chris Lattner30c89792001-09-07 16:35:17 +0000460}
Chris Lattner698b56e2001-07-20 19:15:08 +0000461
Chris Lattner30c89792001-09-07 16:35:17 +0000462
463static vector<pair<unsigned, OpaqueType *> > UpRefs;
464
465static PATypeHolder<Type> HandleUpRefs(const Type *ty) {
466 PATypeHolder<Type> Ty(ty);
467 UR_OUT(UpRefs.size() << " upreferences active!\n");
468 for (unsigned i = 0; i < UpRefs.size(); ) {
469 UR_OUT("TypeContains(" << Ty->getDescription() << ", "
470 << UpRefs[i].second->getDescription() << ") = "
471 << TypeContains(Ty, UpRefs[i].second) << endl);
472 if (TypeContains(Ty, UpRefs[i].second)) {
473 unsigned Level = --UpRefs[i].first; // Decrement level of upreference
474 UR_OUT("Uplevel Ref Level = " << Level << endl);
475 if (Level == 0) { // Upreference should be resolved!
476 UR_OUT("About to resolve upreference!\n";
477 string OldName = UpRefs[i].second->getDescription());
478 UpRefs[i].second->refineAbstractTypeTo(Ty);
479 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
480 UR_OUT("Type '" << OldName << "' refined upreference to: "
481 << (const void*)Ty << ", " << Ty->getDescription() << endl);
482 continue;
483 }
484 }
485
486 ++i; // Otherwise, no resolve, move on...
Chris Lattner8896eda2001-07-09 19:38:36 +0000487 }
Chris Lattner30c89792001-09-07 16:35:17 +0000488 // FIXME: TODO: this should return the updated type
Chris Lattner8896eda2001-07-09 19:38:36 +0000489 return Ty;
490}
491
Chris Lattner30c89792001-09-07 16:35:17 +0000492template <class TypeTy>
493inline static void TypeDone(PATypeHolder<TypeTy> *Ty) {
494 if (UpRefs.size())
495 ThrowException("Invalid upreference in type: " + (*Ty)->getDescription());
496}
497
498// newTH - Allocate a new type holder for the specified type
499template <class TypeTy>
500inline static PATypeHolder<TypeTy> *newTH(const TypeTy *Ty) {
501 return new PATypeHolder<TypeTy>(Ty);
502}
503template <class TypeTy>
504inline static PATypeHolder<TypeTy> *newTH(const PATypeHolder<TypeTy> &TH) {
505 return new PATypeHolder<TypeTy>(TH);
506}
507
508
509// newTHC - Allocate a new type holder for the specified type that can be
510// casted to a new Type type.
511template <class TypeTy, class OldTy>
512inline static PATypeHolder<TypeTy> *newTHC(const PATypeHolder<OldTy> &Old) {
513 return new PATypeHolder<TypeTy>((const TypeTy*)Old.get());
514}
515
Chris Lattner8896eda2001-07-09 19:38:36 +0000516
Chris Lattner00950542001-06-06 20:29:01 +0000517//===----------------------------------------------------------------------===//
518// RunVMAsmParser - Define an interface to this parser
519//===----------------------------------------------------------------------===//
520//
Chris Lattnera2850432001-07-22 18:36:00 +0000521Module *RunVMAsmParser(const string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000522 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000523 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000524 llvmAsmlineno = 1; // Reset the current line number...
525
526 CurModule.CurrentModule = new Module(); // Allocate a new module to read
527 yyparse(); // Parse the file.
528 Module *Result = ParserResult;
Chris Lattner00950542001-06-06 20:29:01 +0000529 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
530 ParserResult = 0;
531
532 return Result;
533}
534
535%}
536
537%union {
Chris Lattner30c89792001-09-07 16:35:17 +0000538 Module *ModuleVal;
539 Method *MethodVal;
540 MethodArgument *MethArgVal;
541 BasicBlock *BasicBlockVal;
542 TerminatorInst *TermInstVal;
543 Instruction *InstVal;
544 ConstPoolVal *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000545
Chris Lattner30c89792001-09-07 16:35:17 +0000546 const Type *PrimType;
547 PATypeHolder<Type> *TypeVal;
548 PATypeHolder<ArrayType> *ArrayTypeTy;
549 PATypeHolder<StructType> *StructTypeTy;
Chris Lattner1a1cb112001-09-30 22:46:54 +0000550 PATypeHolder<PointerType> *PointerTypeTy;
Chris Lattner30c89792001-09-07 16:35:17 +0000551 Value *ValueVal;
552
553 list<MethodArgument*> *MethodArgList;
554 list<Value*> *ValueList;
555 list<PATypeHolder<Type> > *TypeList;
Chris Lattnerc24d2082001-06-11 15:04:20 +0000556 list<pair<Value*, BasicBlock*> > *PHIList; // Represent the RHS of PHI node
Chris Lattner00950542001-06-06 20:29:01 +0000557 list<pair<ConstPoolVal*, BasicBlock*> > *JumpTable;
Chris Lattner30c89792001-09-07 16:35:17 +0000558 vector<ConstPoolVal*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000559
Chris Lattner30c89792001-09-07 16:35:17 +0000560 int64_t SInt64Val;
561 uint64_t UInt64Val;
562 int SIntVal;
563 unsigned UIntVal;
564 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000565 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000566
Chris Lattner30c89792001-09-07 16:35:17 +0000567 char *StrVal; // This memory is strdup'd!
568 ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000569
Chris Lattner30c89792001-09-07 16:35:17 +0000570 Instruction::UnaryOps UnaryOpVal;
571 Instruction::BinaryOps BinaryOpVal;
572 Instruction::TermOps TermOpVal;
573 Instruction::MemoryOps MemOpVal;
574 Instruction::OtherOps OtherOpVal;
Chris Lattner00950542001-06-06 20:29:01 +0000575}
576
577%type <ModuleVal> Module MethodList
Chris Lattnere1815642001-07-15 06:35:53 +0000578%type <MethodVal> Method MethodProto MethodHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000579%type <BasicBlockVal> BasicBlock InstructionList
580%type <TermInstVal> BBTerminatorInst
581%type <InstVal> Inst InstVal MemoryInst
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000582%type <ConstVal> ConstVal
Chris Lattner027dcc52001-07-08 21:10:27 +0000583%type <ConstVector> ConstVector UByteList
Chris Lattner00950542001-06-06 20:29:01 +0000584%type <MethodArgList> ArgList ArgListH
585%type <MethArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000586%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000587%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner30c89792001-09-07 16:35:17 +0000588%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000589%type <JumpTable> JumpTable
Chris Lattner1781aca2001-09-18 04:00:54 +0000590%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Chris Lattner00950542001-06-06 20:29:01 +0000591
592%type <ValIDVal> ValueRef ConstValueRef // Reference to a definition or BB
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000593%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000594// Tokens and types for handling constant integer values
595//
596// ESINT64VAL - A negative number within long long range
597%token <SInt64Val> ESINT64VAL
598
599// EUINT64VAL - A positive number within uns. long long range
600%token <UInt64Val> EUINT64VAL
601%type <SInt64Val> EINT64VAL
602
603%token <SIntVal> SINTVAL // Signed 32 bit ints...
604%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
605%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000606%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000607
608// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000609%type <TypeVal> Types TypesV UpRTypes UpRTypesV
610%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
611%token <TypeVal> OPAQUE
612%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
613%token <PrimType> FLOAT DOUBLE TYPE LABEL
614%type <ArrayTypeTy> ArrayType ArrayTypeI
615%type <StructTypeTy> StructType StructTypeI
Chris Lattner1a1cb112001-09-30 22:46:54 +0000616%type <PointerTypeTy> PointerType PointerTypeI
Chris Lattner00950542001-06-06 20:29:01 +0000617
618%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
619%type <StrVal> OptVAR_ID OptAssign
620
621
Chris Lattner1781aca2001-09-18 04:00:54 +0000622%token IMPLEMENTATION TRUE FALSE BEGINTOK END DECLARE GLOBAL CONSTANT UNINIT
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000623%token TO DOTDOTDOT STRING NULL_TOK CONST
Chris Lattner00950542001-06-06 20:29:01 +0000624
625// Basic Block Terminating Operators
626%token <TermOpVal> RET BR SWITCH
627
628// Unary Operators
629%type <UnaryOpVal> UnaryOps // all the unary operators
Chris Lattner71496b32001-07-08 19:03:27 +0000630%token <UnaryOpVal> NOT
Chris Lattner00950542001-06-06 20:29:01 +0000631
632// Binary Operators
633%type <BinaryOpVal> BinaryOps // all the binary operators
634%token <BinaryOpVal> ADD SUB MUL DIV REM
Chris Lattner027dcc52001-07-08 21:10:27 +0000635%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000636
637// Memory Instructions
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000638%token <MemoryOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000639
Chris Lattner027dcc52001-07-08 21:10:27 +0000640// Other Operators
641%type <OtherOpVal> ShiftOps
642%token <OtherOpVal> PHI CALL CAST SHL SHR
643
Chris Lattner00950542001-06-06 20:29:01 +0000644%start Module
645%%
646
647// Handle constant integer size restriction and conversion...
648//
649
650INTVAL : SINTVAL
651INTVAL : UINTVAL {
652 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
653 ThrowException("Value too large for type!");
654 $$ = (int32_t)$1;
655}
656
657
658EINT64VAL : ESINT64VAL // These have same type and can't cause problems...
659EINT64VAL : EUINT64VAL {
660 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
661 ThrowException("Value too large for type!");
662 $$ = (int64_t)$1;
663}
664
Chris Lattner00950542001-06-06 20:29:01 +0000665// Operations that are notably excluded from this list include:
666// RET, BR, & SWITCH because they end basic blocks and are treated specially.
667//
Chris Lattner09083092001-07-08 04:57:15 +0000668UnaryOps : NOT
Chris Lattner00950542001-06-06 20:29:01 +0000669BinaryOps : ADD | SUB | MUL | DIV | REM
670BinaryOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE
Chris Lattner027dcc52001-07-08 21:10:27 +0000671ShiftOps : SHL | SHR
Chris Lattner00950542001-06-06 20:29:01 +0000672
Chris Lattnere98dda62001-07-14 06:10:16 +0000673// These are some types that allow classification if we only want a particular
674// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner00950542001-06-06 20:29:01 +0000675SIntType : LONG | INT | SHORT | SBYTE
676UIntType : ULONG | UINT | USHORT | UBYTE
Chris Lattner30c89792001-09-07 16:35:17 +0000677IntType : SIntType | UIntType
678FPType : FLOAT | DOUBLE
Chris Lattner00950542001-06-06 20:29:01 +0000679
Chris Lattnere98dda62001-07-14 06:10:16 +0000680// OptAssign - Value producing statements have an optional assignment component
Chris Lattner00950542001-06-06 20:29:01 +0000681OptAssign : VAR_ID '=' {
682 $$ = $1;
683 }
684 | /*empty*/ {
685 $$ = 0;
686 }
687
Chris Lattner30c89792001-09-07 16:35:17 +0000688
689//===----------------------------------------------------------------------===//
690// Types includes all predefined types... except void, because it can only be
691// used in specific contexts (method returning void for example). To have
692// access to it, a user must explicitly use TypesV.
693//
694
695// TypesV includes all of 'Types', but it also includes the void type.
696TypesV : Types | VOID { $$ = newTH($1); }
697UpRTypesV : UpRTypes | VOID { $$ = newTH($1); }
698
699Types : UpRTypes {
700 TypeDone($$ = $1);
701 }
702
703
704// Derived types are added later...
705//
706PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT
707PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL
708UpRTypes : OPAQUE | PrimType { $$ = newTH($1); }
709UpRTypes : ValueRef { // Named types are also simple types...
710 $$ = newTH(getTypeVal($1));
711}
712
713// ArrayTypeI - Internal version of ArrayType that can have incomplete uprefs
714//
715ArrayTypeI : '[' UpRTypesV ']' { // Unsized array type?
716 $$ = newTHC<ArrayType>(HandleUpRefs(ArrayType::get(*$2)));
717 delete $2;
718 }
719 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
720 $$ = newTHC<ArrayType>(HandleUpRefs(ArrayType::get(*$4, (int)$2)));
721 delete $4;
722 }
723
724StructTypeI : '{' TypeListI '}' { // Structure type?
725 vector<const Type*> Elements;
726 mapto($2->begin(), $2->end(), back_inserter(Elements),
727 mem_fun_ref(&PATypeHandle<Type>::get));
728
729 $$ = newTHC<StructType>(HandleUpRefs(StructType::get(Elements)));
730 delete $2;
731 }
732 | '{' '}' { // Empty structure type?
733 $$ = newTH(StructType::get(vector<const Type*>()));
734 }
735
Chris Lattner1a1cb112001-09-30 22:46:54 +0000736PointerTypeI : UpRTypes '*' { // Pointer type?
737 $$ = newTHC<PointerType>(HandleUpRefs(PointerType::get(*$1)));
738 delete $1; // Delete the type handle
739 }
Chris Lattner30c89792001-09-07 16:35:17 +0000740
741// Include derived types in the Types production.
742//
743UpRTypes : '\\' EUINT64VAL { // Type UpReference
744 if ($2 > (uint64_t)INT64_MAX) ThrowException("Value out of range!");
745 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
746 UpRefs.push_back(make_pair((unsigned)$2, OT)); // Add to vector...
747 $$ = newTH<Type>(OT);
748 UR_OUT("New Upreference!\n");
749 }
750 | UpRTypesV '(' ArgTypeListI ')' { // Method derived type?
751 vector<const Type*> Params;
752 mapto($3->begin(), $3->end(), back_inserter(Params),
753 mem_fun_ref(&PATypeHandle<Type>::get));
754 $$ = newTH(HandleUpRefs(MethodType::get(*$1, Params)));
755 delete $3; // Delete the argument list
756 delete $1; // Delete the old type handle
757 }
758 | ArrayTypeI { // [Un]sized array type?
759 $$ = newTHC<Type>(*$1); delete $1;
760 }
761 | StructTypeI { // Structure type?
762 $$ = newTHC<Type>(*$1); delete $1;
763 }
Chris Lattner1a1cb112001-09-30 22:46:54 +0000764 | PointerTypeI { // Pointer type?
765 $$ = newTHC<Type>(*$1); delete $1;
Chris Lattner30c89792001-09-07 16:35:17 +0000766 }
767
768// Define some helpful top level types that do not allow UpReferences to escape
769//
Chris Lattner1a1cb112001-09-30 22:46:54 +0000770ArrayType : ArrayTypeI { TypeDone($$ = $1); }
771StructType : StructTypeI { TypeDone($$ = $1); }
772PointerType : PointerTypeI { TypeDone($$ = $1); }
Chris Lattner30c89792001-09-07 16:35:17 +0000773
774
775// TypeList - Used for struct declarations and as a basis for method type
776// declaration type lists
777//
778TypeListI : UpRTypes {
779 $$ = new list<PATypeHolder<Type> >();
780 $$->push_back(*$1); delete $1;
781 }
782 | TypeListI ',' UpRTypes {
783 ($$=$1)->push_back(*$3); delete $3;
784 }
785
786// ArgTypeList - List of types for a method type declaration...
787ArgTypeListI : TypeListI
788 | TypeListI ',' DOTDOTDOT {
789 ($$=$1)->push_back(Type::VoidTy);
790 }
791 | DOTDOTDOT {
792 ($$ = new list<PATypeHolder<Type> >())->push_back(Type::VoidTy);
793 }
794 | /*empty*/ {
795 $$ = new list<PATypeHolder<Type> >();
796 }
797
798
Chris Lattnere98dda62001-07-14 06:10:16 +0000799// ConstVal - The various declarations that go into the constant pool. This
800// includes all forward declarations of types, constants, and functions.
801//
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000802ConstVal: ArrayType '[' ConstVector ']' { // Nonempty unsized arr
Chris Lattner30c89792001-09-07 16:35:17 +0000803 const ArrayType *ATy = *$1;
804 const Type *ETy = ATy->getElementType();
805 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +0000806
Chris Lattner30c89792001-09-07 16:35:17 +0000807 // Verify that we have the correct size...
808 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +0000809 ThrowException("Type mismatch: constant sized array initialized with " +
Chris Lattner30c89792001-09-07 16:35:17 +0000810 utostr($3->size()) + " arguments, but has size of " +
811 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +0000812
Chris Lattner30c89792001-09-07 16:35:17 +0000813 // Verify all elements are correct type!
814 for (unsigned i = 0; i < $3->size(); i++) {
815 if (ETy != (*$3)[i]->getType())
Chris Lattner00950542001-06-06 20:29:01 +0000816 ThrowException("Element #" + utostr(i) + " is not of type '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000817 ETy->getName() + "' as required!\nIt is of type '" +
818 (*$3)[i]->getType()->getName() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +0000819 }
820
Chris Lattner30c89792001-09-07 16:35:17 +0000821 $$ = ConstPoolArray::get(ATy, *$3);
822 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000823 }
Chris Lattner30c89792001-09-07 16:35:17 +0000824 | ArrayType '[' ']' {
825 int NumElements = (*$1)->getNumElements();
826 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +0000827 ThrowException("Type mismatch: constant sized array initialized with 0"
Chris Lattner30c89792001-09-07 16:35:17 +0000828 " arguments, but has size of " + itostr(NumElements) +"!");
829 $$ = ConstPoolArray::get((*$1), vector<ConstPoolVal*>());
830 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +0000831 }
Chris Lattner30c89792001-09-07 16:35:17 +0000832 | ArrayType 'c' STRINGCONSTANT {
833 const ArrayType *ATy = *$1;
834 int NumElements = ATy->getNumElements();
835 const Type *ETy = ATy->getElementType();
836 char *EndStr = UnEscapeLexed($3, true);
837 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +0000838 ThrowException("Can't build string constant of size " +
Chris Lattner30c89792001-09-07 16:35:17 +0000839 itostr((int)(EndStr-$3)) +
840 " when array has size " + itostr(NumElements) + "!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000841 vector<ConstPoolVal*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +0000842 if (ETy == Type::SByteTy) {
843 for (char *C = $3; C != EndStr; ++C)
844 Vals.push_back(ConstPoolSInt::get(ETy, *C));
845 } else if (ETy == Type::UByteTy) {
846 for (char *C = $3; C != EndStr; ++C)
847 Vals.push_back(ConstPoolUInt::get(ETy, *C));
Chris Lattner93750fa2001-07-28 17:48:55 +0000848 } else {
Chris Lattner30c89792001-09-07 16:35:17 +0000849 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +0000850 ThrowException("Cannot build string arrays of non byte sized elements!");
851 }
Chris Lattner30c89792001-09-07 16:35:17 +0000852 free($3);
853 $$ = ConstPoolArray::get(ATy, Vals);
854 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +0000855 }
Chris Lattner30c89792001-09-07 16:35:17 +0000856 | StructType '{' ConstVector '}' {
857 // FIXME: TODO: Check to see that the constants are compatible with the type
858 // initializer!
859 $$ = ConstPoolStruct::get(*$1, *$3);
860 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000861 }
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000862 | PointerType NULL_TOK {
863 $$ = ConstPoolPointer::getNullPointer(*$1);
864 delete $1;
865 }
Chris Lattner00950542001-06-06 20:29:01 +0000866/*
867 | Types '*' ConstVal {
868 assert(0);
869 $$ = 0;
870 }
871*/
872
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000873ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000874 if (!ConstPoolSInt::isValueValidForType($1, $2))
875 ThrowException("Constant value doesn't fit in type!");
Chris Lattner30c89792001-09-07 16:35:17 +0000876 $$ = ConstPoolSInt::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000877 }
878 | UIntType EUINT64VAL { // integral constants
879 if (!ConstPoolUInt::isValueValidForType($1, $2))
880 ThrowException("Constant value doesn't fit in type!");
Chris Lattner30c89792001-09-07 16:35:17 +0000881 $$ = ConstPoolUInt::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000882 }
883 | BOOL TRUE { // Boolean constants
Chris Lattner30c89792001-09-07 16:35:17 +0000884 $$ = ConstPoolBool::True;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000885 }
886 | BOOL FALSE { // Boolean constants
Chris Lattner30c89792001-09-07 16:35:17 +0000887 $$ = ConstPoolBool::False;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000888 }
889 | FPType FPVAL { // Float & Double constants
Chris Lattner30c89792001-09-07 16:35:17 +0000890 $$ = ConstPoolFP::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000891 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000892
Chris Lattnere98dda62001-07-14 06:10:16 +0000893// ConstVector - A list of comma seperated constants.
Chris Lattner00950542001-06-06 20:29:01 +0000894ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +0000895 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +0000896 }
897 | ConstVal {
898 $$ = new vector<ConstPoolVal*>();
Chris Lattner30c89792001-09-07 16:35:17 +0000899 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +0000900 }
901
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000902
Chris Lattner1781aca2001-09-18 04:00:54 +0000903// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
904GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; }
905
Chris Lattner00950542001-06-06 20:29:01 +0000906
Chris Lattnere98dda62001-07-14 06:10:16 +0000907// ConstPool - Constants with optional names assigned to them.
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000908ConstPool : ConstPool OptAssign CONST ConstVal {
909 setValueName($4, $2);
910 InsertValue($4);
Chris Lattner00950542001-06-06 20:29:01 +0000911 }
Chris Lattner30c89792001-09-07 16:35:17 +0000912 | ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattner1781aca2001-09-18 04:00:54 +0000913 // TODO: FIXME when Type are not const
914 setValueName(const_cast<Type*>($4->get()), $2);
915
916 if (!$2) {
Chris Lattner30c89792001-09-07 16:35:17 +0000917 InsertType($4->get(),
918 CurMeth.CurrentMethod ? CurMeth.Types : CurModule.Types);
919 }
920 delete $4;
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000921
922 ResolveSomeTypes(CurMeth.CurrentMethod ? CurMeth.LateResolveTypes :
923 CurModule.LateResolveTypes);
Chris Lattner30c89792001-09-07 16:35:17 +0000924 }
925 | ConstPool MethodProto { // Method prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +0000926 }
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000927 | ConstPool OptAssign GlobalType ConstVal {
Chris Lattner1781aca2001-09-18 04:00:54 +0000928 const Type *Ty = $4->getType();
929 // Global declarations appear in Constant Pool
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000930 ConstPoolVal *Initializer = $4;
Chris Lattner1781aca2001-09-18 04:00:54 +0000931 if (Initializer == 0)
932 ThrowException("Global value initializer is not a constant!");
933
934 GlobalVariable *GV = new GlobalVariable(PointerType::get(Ty), $3,
935 Initializer);
936 setValueName(GV, $2);
937
938 CurModule.CurrentModule->getGlobalList().push_back(GV);
939 InsertValue(GV, CurModule.Values);
940 }
941 | ConstPool OptAssign UNINIT GlobalType Types {
942 const Type *Ty = *$5;
943 // Global declarations appear in Constant Pool
Chris Lattnercfe26c92001-10-01 18:26:53 +0000944 if (isa<ArrayType>(Ty) && cast<ArrayType>(Ty)->isUnsized()) {
Chris Lattner1781aca2001-09-18 04:00:54 +0000945 ThrowException("Type '" + Ty->getDescription() +
946 "' is not a sized type!");
Chris Lattner70cc3392001-09-10 07:58:01 +0000947 }
Chris Lattner1781aca2001-09-18 04:00:54 +0000948
949 GlobalVariable *GV = new GlobalVariable(PointerType::get(Ty), $4);
950 setValueName(GV, $2);
951
Chris Lattner70cc3392001-09-10 07:58:01 +0000952 CurModule.CurrentModule->getGlobalList().push_back(GV);
953 InsertValue(GV, CurModule.Values);
Chris Lattnere98dda62001-07-14 06:10:16 +0000954 }
Chris Lattner00950542001-06-06 20:29:01 +0000955 | /* empty: end of list */ {
956 }
957
958
959//===----------------------------------------------------------------------===//
960// Rules to match Modules
961//===----------------------------------------------------------------------===//
962
963// Module rule: Capture the result of parsing the whole file into a result
964// variable...
965//
966Module : MethodList {
967 $$ = ParserResult = $1;
968 CurModule.ModuleDone();
969}
970
Chris Lattnere98dda62001-07-14 06:10:16 +0000971// MethodList - A list of methods, preceeded by a constant pool.
972//
Chris Lattner00950542001-06-06 20:29:01 +0000973MethodList : MethodList Method {
Chris Lattner00950542001-06-06 20:29:01 +0000974 $$ = $1;
Chris Lattnere1815642001-07-15 06:35:53 +0000975 if (!$2->getParent())
976 $1->getMethodList().push_back($2);
977 CurMeth.MethodDone();
Chris Lattner00950542001-06-06 20:29:01 +0000978 }
Chris Lattnere1815642001-07-15 06:35:53 +0000979 | MethodList MethodProto {
980 $$ = $1;
Chris Lattnere1815642001-07-15 06:35:53 +0000981 }
Chris Lattner00950542001-06-06 20:29:01 +0000982 | ConstPool IMPLEMENTATION {
983 $$ = CurModule.CurrentModule;
Chris Lattner30c89792001-09-07 16:35:17 +0000984 // Resolve circular types before we parse the body of the module
985 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner00950542001-06-06 20:29:01 +0000986 }
987
988
989//===----------------------------------------------------------------------===//
990// Rules to match Method Headers
991//===----------------------------------------------------------------------===//
992
993OptVAR_ID : VAR_ID | /*empty*/ { $$ = 0; }
994
995ArgVal : Types OptVAR_ID {
Chris Lattner30c89792001-09-07 16:35:17 +0000996 $$ = new MethodArgument(*$1); delete $1;
Chris Lattner1781aca2001-09-18 04:00:54 +0000997 setValueName($$, $2);
Chris Lattner00950542001-06-06 20:29:01 +0000998}
999
1000ArgListH : ArgVal ',' ArgListH {
1001 $$ = $3;
1002 $3->push_front($1);
1003 }
1004 | ArgVal {
1005 $$ = new list<MethodArgument*>();
1006 $$->push_front($1);
1007 }
Chris Lattner8b81bf52001-07-25 22:47:46 +00001008 | DOTDOTDOT {
1009 $$ = new list<MethodArgument*>();
1010 $$->push_back(new MethodArgument(Type::VoidTy));
1011 }
Chris Lattner00950542001-06-06 20:29:01 +00001012
1013ArgList : ArgListH {
1014 $$ = $1;
1015 }
1016 | /* empty */ {
1017 $$ = 0;
1018 }
1019
1020MethodHeaderH : TypesV STRINGCONSTANT '(' ArgList ')' {
Chris Lattner93750fa2001-07-28 17:48:55 +00001021 UnEscapeLexed($2);
Chris Lattner30c89792001-09-07 16:35:17 +00001022 vector<const Type*> ParamTypeList;
Chris Lattner00950542001-06-06 20:29:01 +00001023 if ($4)
Chris Lattner7fc9fe32001-06-27 23:41:11 +00001024 for (list<MethodArgument*>::iterator I = $4->begin(); I != $4->end(); ++I)
Chris Lattner00950542001-06-06 20:29:01 +00001025 ParamTypeList.push_back((*I)->getType());
1026
Chris Lattner30c89792001-09-07 16:35:17 +00001027 const MethodType *MT = MethodType::get(*$1, ParamTypeList);
1028 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001029
Chris Lattnere1815642001-07-15 06:35:53 +00001030 Method *M = 0;
1031 if (SymbolTable *ST = CurModule.CurrentModule->getSymbolTable()) {
1032 if (Value *V = ST->lookup(MT, $2)) { // Method already in symtab?
Chris Lattner9636a912001-10-01 16:18:37 +00001033 M = cast<Method>(V);
Chris Lattner00950542001-06-06 20:29:01 +00001034
Chris Lattnere1815642001-07-15 06:35:53 +00001035 // Yes it is. If this is the case, either we need to be a forward decl,
1036 // or it needs to be.
1037 if (!CurMeth.isDeclare && !M->isExternal())
1038 ThrowException("Redefinition of method '" + string($2) + "'!");
1039 }
1040 }
1041
1042 if (M == 0) { // Not already defined?
1043 M = new Method(MT, $2);
1044 InsertValue(M, CurModule.Values);
1045 }
1046
1047 free($2); // Free strdup'd memory!
Chris Lattner00950542001-06-06 20:29:01 +00001048
1049 CurMeth.MethodStart(M);
1050
1051 // Add all of the arguments we parsed to the method...
Chris Lattnere1815642001-07-15 06:35:53 +00001052 if ($4 && !CurMeth.isDeclare) { // Is null if empty...
Chris Lattner00950542001-06-06 20:29:01 +00001053 Method::ArgumentListType &ArgList = M->getArgumentList();
1054
Chris Lattner7fc9fe32001-06-27 23:41:11 +00001055 for (list<MethodArgument*>::iterator I = $4->begin(); I != $4->end(); ++I) {
Chris Lattner00950542001-06-06 20:29:01 +00001056 InsertValue(*I);
1057 ArgList.push_back(*I);
1058 }
1059 delete $4; // We're now done with the argument list
1060 }
1061}
1062
1063MethodHeader : MethodHeaderH ConstPool BEGINTOK {
1064 $$ = CurMeth.CurrentMethod;
Chris Lattner30c89792001-09-07 16:35:17 +00001065
1066 // Resolve circular types before we parse the body of the method.
1067 ResolveTypes(CurMeth.LateResolveTypes);
Chris Lattner00950542001-06-06 20:29:01 +00001068}
1069
1070Method : BasicBlockList END {
1071 $$ = $1;
1072}
1073
Chris Lattnere1815642001-07-15 06:35:53 +00001074MethodProto : DECLARE { CurMeth.isDeclare = true; } MethodHeaderH {
1075 $$ = CurMeth.CurrentMethod;
Chris Lattner93750fa2001-07-28 17:48:55 +00001076 if (!$$->getParent())
1077 CurModule.CurrentModule->getMethodList().push_back($$);
1078 CurMeth.MethodDone();
Chris Lattnere1815642001-07-15 06:35:53 +00001079}
Chris Lattner00950542001-06-06 20:29:01 +00001080
1081//===----------------------------------------------------------------------===//
1082// Rules to match Basic Blocks
1083//===----------------------------------------------------------------------===//
1084
1085ConstValueRef : ESINT64VAL { // A reference to a direct constant
1086 $$ = ValID::create($1);
1087 }
1088 | EUINT64VAL {
1089 $$ = ValID::create($1);
1090 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001091 | FPVAL { // Perhaps it's an FP constant?
1092 $$ = ValID::create($1);
1093 }
Chris Lattner00950542001-06-06 20:29:01 +00001094 | TRUE {
1095 $$ = ValID::create((int64_t)1);
1096 }
1097 | FALSE {
1098 $$ = ValID::create((int64_t)0);
1099 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001100 | NULL_TOK {
1101 $$ = ValID::createNull();
1102 }
1103
Chris Lattner93750fa2001-07-28 17:48:55 +00001104/*
Chris Lattner00950542001-06-06 20:29:01 +00001105 | STRINGCONSTANT { // Quoted strings work too... especially for methods
1106 $$ = ValID::create_conststr($1);
1107 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001108*/
Chris Lattner00950542001-06-06 20:29:01 +00001109
1110// ValueRef - A reference to a definition...
1111ValueRef : INTVAL { // Is it an integer reference...?
1112 $$ = ValID::create($1);
1113 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001114 | VAR_ID { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001115 $$ = ValID::create($1);
1116 }
1117 | ConstValueRef {
1118 $$ = $1;
1119 }
1120
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001121// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1122// type immediately preceeds the value reference, and allows complex constant
1123// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001124ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001125 $$ = getVal(*$1, $2); delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +00001126 }
Chris Lattner8b81bf52001-07-25 22:47:46 +00001127
Chris Lattner00950542001-06-06 20:29:01 +00001128
1129BasicBlockList : BasicBlockList BasicBlock {
1130 $1->getBasicBlocks().push_back($2);
1131 $$ = $1;
1132 }
1133 | MethodHeader BasicBlock { // Do not allow methods with 0 basic blocks
1134 $$ = $1; // in them...
1135 $1->getBasicBlocks().push_back($2);
1136 }
1137
1138
1139// Basic blocks are terminated by branching instructions:
1140// br, br/cc, switch, ret
1141//
1142BasicBlock : InstructionList BBTerminatorInst {
1143 $1->getInstList().push_back($2);
1144 InsertValue($1);
1145 $$ = $1;
1146 }
1147 | LABELSTR InstructionList BBTerminatorInst {
1148 $2->getInstList().push_back($3);
Chris Lattner30c89792001-09-07 16:35:17 +00001149 setValueName($2, $1);
Chris Lattner00950542001-06-06 20:29:01 +00001150
1151 InsertValue($2);
1152 $$ = $2;
1153 }
1154
1155InstructionList : InstructionList Inst {
1156 $1->getInstList().push_back($2);
1157 $$ = $1;
1158 }
1159 | /* empty */ {
1160 $$ = new BasicBlock();
1161 }
1162
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001163BBTerminatorInst : RET ResolvedVal { // Return with a result...
1164 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001165 }
1166 | RET VOID { // Return with no result...
1167 $$ = new ReturnInst();
1168 }
1169 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner9636a912001-10-01 16:18:37 +00001170 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $3)));
Chris Lattner00950542001-06-06 20:29:01 +00001171 } // Conditional Branch...
1172 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner9636a912001-10-01 16:18:37 +00001173 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $6)),
1174 cast<BasicBlock>(getVal(Type::LabelTy, $9)),
Chris Lattner00950542001-06-06 20:29:01 +00001175 getVal(Type::BoolTy, $3));
1176 }
1177 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1178 SwitchInst *S = new SwitchInst(getVal($2, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001179 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
Chris Lattner00950542001-06-06 20:29:01 +00001180 $$ = S;
1181
1182 list<pair<ConstPoolVal*, BasicBlock*> >::iterator I = $8->begin(),
1183 end = $8->end();
Chris Lattner7fc9fe32001-06-27 23:41:11 +00001184 for (; I != end; ++I)
Chris Lattner00950542001-06-06 20:29:01 +00001185 S->dest_push_back(I->first, I->second);
1186 }
1187
1188JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1189 $$ = $1;
Chris Lattnercfe26c92001-10-01 18:26:53 +00001190 ConstPoolVal *V = cast<ConstPoolVal>(getVal($2, $3, true));
Chris Lattner00950542001-06-06 20:29:01 +00001191 if (V == 0)
1192 ThrowException("May only switch on a constant pool value!");
1193
Chris Lattner9636a912001-10-01 16:18:37 +00001194 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($5, $6))));
Chris Lattner00950542001-06-06 20:29:01 +00001195 }
1196 | IntType ConstValueRef ',' LABEL ValueRef {
1197 $$ = new list<pair<ConstPoolVal*, BasicBlock*> >();
Chris Lattnercfe26c92001-10-01 18:26:53 +00001198 ConstPoolVal *V = cast<ConstPoolVal>(getVal($1, $2, true));
Chris Lattner00950542001-06-06 20:29:01 +00001199
1200 if (V == 0)
1201 ThrowException("May only switch on a constant pool value!");
1202
Chris Lattner9636a912001-10-01 16:18:37 +00001203 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($4, $5))));
Chris Lattner00950542001-06-06 20:29:01 +00001204 }
1205
1206Inst : OptAssign InstVal {
Chris Lattner1781aca2001-09-18 04:00:54 +00001207 setValueName($2, $1); // Is this definition named?? if so, assign the name...
Chris Lattner00950542001-06-06 20:29:01 +00001208
1209 InsertValue($2);
1210 $$ = $2;
1211}
1212
Chris Lattnerc24d2082001-06-11 15:04:20 +00001213PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
1214 $$ = new list<pair<Value*, BasicBlock*> >();
Chris Lattner30c89792001-09-07 16:35:17 +00001215 $$->push_back(make_pair(getVal(*$1, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001216 cast<BasicBlock>(getVal(Type::LabelTy, $5))));
Chris Lattner30c89792001-09-07 16:35:17 +00001217 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001218 }
1219 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1220 $$ = $1;
1221 $1->push_back(make_pair(getVal($1->front().first->getType(), $4),
Chris Lattner9636a912001-10-01 16:18:37 +00001222 cast<BasicBlock>(getVal(Type::LabelTy, $6))));
Chris Lattnerc24d2082001-06-11 15:04:20 +00001223 }
1224
1225
Chris Lattner30c89792001-09-07 16:35:17 +00001226ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner00950542001-06-06 20:29:01 +00001227 $$ = new list<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001228 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001229 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001230 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001231 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001232 $1->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001233 }
1234
1235// ValueRefListE - Just like ValueRefList, except that it may also be empty!
1236ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; }
1237
1238InstVal : BinaryOps Types ValueRef ',' ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001239 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001240 if ($$ == 0)
1241 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001242 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001243 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001244 | UnaryOps ResolvedVal {
1245 $$ = UnaryOperator::create($1, $2);
Chris Lattner00950542001-06-06 20:29:01 +00001246 if ($$ == 0)
1247 ThrowException("unary operator returned null!");
Chris Lattner09083092001-07-08 04:57:15 +00001248 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001249 | ShiftOps ResolvedVal ',' ResolvedVal {
1250 if ($4->getType() != Type::UByteTy)
1251 ThrowException("Shift amount must be ubyte!");
1252 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001253 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001254 | CAST ResolvedVal TO Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001255 $$ = new CastInst($2, *$4);
1256 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001257 }
Chris Lattnerc24d2082001-06-11 15:04:20 +00001258 | PHI PHIList {
1259 const Type *Ty = $2->front().first->getType();
1260 $$ = new PHINode(Ty);
Chris Lattner00950542001-06-06 20:29:01 +00001261 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001262 if ($2->front().first->getType() != Ty)
1263 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00001264 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001265 $2->pop_front();
1266 }
1267 delete $2; // Free the list...
1268 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001269 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001270 const MethodType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001271
Chris Lattner9636a912001-10-01 16:18:37 +00001272 if (!(Ty = dyn_cast<MethodType>($2->get()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001273 // Pull out the types of all of the arguments...
1274 vector<const Type*> ParamTypes;
1275 for (list<Value*>::iterator I = $5->begin(), E = $5->end(); I != E; ++I)
1276 ParamTypes.push_back((*I)->getType());
Chris Lattner30c89792001-09-07 16:35:17 +00001277 Ty = MethodType::get(*$2, ParamTypes);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001278 }
Chris Lattner30c89792001-09-07 16:35:17 +00001279 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001280
Chris Lattner8b81bf52001-07-25 22:47:46 +00001281 Value *V = getVal(Ty, $3); // Get the method we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00001282
Chris Lattner8b81bf52001-07-25 22:47:46 +00001283 // Create the call node...
1284 if (!$5) { // Has no arguments?
Chris Lattner9636a912001-10-01 16:18:37 +00001285 $$ = new CallInst(cast<Method>(V), vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00001286 } else { // Has arguments?
Chris Lattner00950542001-06-06 20:29:01 +00001287 // Loop through MethodType's arguments and ensure they are specified
1288 // correctly!
1289 //
1290 MethodType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001291 MethodType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
1292 list<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
1293
1294 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1295 if ((*ArgI)->getType() != *I)
1296 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner00950542001-06-06 20:29:01 +00001297 (*I)->getName() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00001298
Chris Lattner8b81bf52001-07-25 22:47:46 +00001299 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner00950542001-06-06 20:29:01 +00001300 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00001301
Chris Lattner9636a912001-10-01 16:18:37 +00001302 $$ = new CallInst(cast<Method>(V),
Chris Lattner8b81bf52001-07-25 22:47:46 +00001303 vector<Value*>($5->begin(), $5->end()));
1304 }
1305 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00001306 }
1307 | MemoryInst {
1308 $$ = $1;
1309 }
1310
Chris Lattner027dcc52001-07-08 21:10:27 +00001311// UByteList - List of ubyte values for load and store instructions
1312UByteList : ',' ConstVector {
1313 $$ = $2;
1314} | /* empty */ {
1315 $$ = new vector<ConstPoolVal*>();
1316}
1317
Chris Lattner00950542001-06-06 20:29:01 +00001318MemoryInst : MALLOC Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001319 $$ = new MallocInst(PointerType::get(*$2));
1320 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001321 }
1322 | MALLOC Types ',' UINT ValueRef {
Chris Lattnerb00c5822001-10-02 03:41:24 +00001323 if (!(*$2)->isArrayType() || cast<const ArrayType>($2->get())->isSized())
Chris Lattner30c89792001-09-07 16:35:17 +00001324 ThrowException("Trying to allocate " + (*$2)->getName() +
Chris Lattner00950542001-06-06 20:29:01 +00001325 " as unsized array!");
Chris Lattner30c89792001-09-07 16:35:17 +00001326 const Type *Ty = PointerType::get(*$2);
Chris Lattner8896eda2001-07-09 19:38:36 +00001327 $$ = new MallocInst(Ty, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001328 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001329 }
1330 | ALLOCA Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001331 $$ = new AllocaInst(PointerType::get(*$2));
1332 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001333 }
1334 | ALLOCA Types ',' UINT ValueRef {
Chris Lattnerb00c5822001-10-02 03:41:24 +00001335 if (!(*$2)->isArrayType() || cast<const ArrayType>($2->get())->isSized())
Chris Lattner30c89792001-09-07 16:35:17 +00001336 ThrowException("Trying to allocate " + (*$2)->getName() +
Chris Lattner00950542001-06-06 20:29:01 +00001337 " as unsized array!");
Chris Lattner30c89792001-09-07 16:35:17 +00001338 const Type *Ty = PointerType::get(*$2);
Chris Lattner00950542001-06-06 20:29:01 +00001339 Value *ArrSize = getVal($4, $5);
Chris Lattnerf0d0e9c2001-07-07 08:36:30 +00001340 $$ = new AllocaInst(Ty, ArrSize);
Chris Lattner30c89792001-09-07 16:35:17 +00001341 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001342 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001343 | FREE ResolvedVal {
1344 if (!$2->getType()->isPointerType())
1345 ThrowException("Trying to free nonpointer type " +
1346 $2->getType()->getName() + "!");
1347 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001348 }
1349
Chris Lattner027dcc52001-07-08 21:10:27 +00001350 | LOAD Types ValueRef UByteList {
Chris Lattner30c89792001-09-07 16:35:17 +00001351 if (!(*$2)->isPointerType())
1352 ThrowException("Can't load from nonpointer type: " + (*$2)->getName());
1353 if (LoadInst::getIndexedType(*$2, *$4) == 0)
Chris Lattner027dcc52001-07-08 21:10:27 +00001354 ThrowException("Invalid indices for load instruction!");
1355
Chris Lattner30c89792001-09-07 16:35:17 +00001356 $$ = new LoadInst(getVal(*$2, $3), *$4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001357 delete $4; // Free the vector...
Chris Lattner30c89792001-09-07 16:35:17 +00001358 delete $2;
Chris Lattner027dcc52001-07-08 21:10:27 +00001359 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001360 | STORE ResolvedVal ',' Types ValueRef UByteList {
Chris Lattner30c89792001-09-07 16:35:17 +00001361 if (!(*$4)->isPointerType())
1362 ThrowException("Can't store to a nonpointer type: " + (*$4)->getName());
1363 const Type *ElTy = StoreInst::getIndexedType(*$4, *$6);
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001364 if (ElTy == 0)
1365 ThrowException("Can't store into that field list!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001366 if (ElTy != $2->getType())
1367 ThrowException("Can't store '" + $2->getType()->getName() +
1368 "' into space of type '" + ElTy->getName() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001369 $$ = new StoreInst($2, getVal(*$4, $5), *$6);
1370 delete $4; delete $6;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001371 }
1372 | GETELEMENTPTR Types ValueRef UByteList {
Chris Lattner30c89792001-09-07 16:35:17 +00001373 if (!(*$2)->isPointerType())
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001374 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner30c89792001-09-07 16:35:17 +00001375 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
1376 ThrowException("Can't get element ptr '" + (*$2)->getName() + "'!");
1377 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
1378 delete $2; delete $4;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001379 }
Chris Lattner027dcc52001-07-08 21:10:27 +00001380
Chris Lattner00950542001-06-06 20:29:01 +00001381%%
Chris Lattner09083092001-07-08 04:57:15 +00001382int yyerror(const char *ErrorMsg) {
Chris Lattner00950542001-06-06 20:29:01 +00001383 ThrowException(string("Parse error: ") + ErrorMsg);
1384 return 0;
1385}