blob: 5771318f6bf9aeff1e57845e23011bb558bbe358 [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
Chris Lattner00950542001-06-06 20:29:01 +0000509//===----------------------------------------------------------------------===//
510// RunVMAsmParser - Define an interface to this parser
511//===----------------------------------------------------------------------===//
512//
Chris Lattnera2850432001-07-22 18:36:00 +0000513Module *RunVMAsmParser(const string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000514 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000515 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000516 llvmAsmlineno = 1; // Reset the current line number...
517
518 CurModule.CurrentModule = new Module(); // Allocate a new module to read
519 yyparse(); // Parse the file.
520 Module *Result = ParserResult;
Chris Lattner00950542001-06-06 20:29:01 +0000521 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
522 ParserResult = 0;
523
524 return Result;
525}
526
527%}
528
529%union {
Chris Lattner30c89792001-09-07 16:35:17 +0000530 Module *ModuleVal;
531 Method *MethodVal;
532 MethodArgument *MethArgVal;
533 BasicBlock *BasicBlockVal;
534 TerminatorInst *TermInstVal;
535 Instruction *InstVal;
536 ConstPoolVal *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000537
Chris Lattner30c89792001-09-07 16:35:17 +0000538 const Type *PrimType;
539 PATypeHolder<Type> *TypeVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000540 Value *ValueVal;
541
542 list<MethodArgument*> *MethodArgList;
543 list<Value*> *ValueList;
544 list<PATypeHolder<Type> > *TypeList;
Chris Lattnerc24d2082001-06-11 15:04:20 +0000545 list<pair<Value*, BasicBlock*> > *PHIList; // Represent the RHS of PHI node
Chris Lattner00950542001-06-06 20:29:01 +0000546 list<pair<ConstPoolVal*, BasicBlock*> > *JumpTable;
Chris Lattner30c89792001-09-07 16:35:17 +0000547 vector<ConstPoolVal*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000548
Chris Lattner30c89792001-09-07 16:35:17 +0000549 int64_t SInt64Val;
550 uint64_t UInt64Val;
551 int SIntVal;
552 unsigned UIntVal;
553 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000554 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000555
Chris Lattner30c89792001-09-07 16:35:17 +0000556 char *StrVal; // This memory is strdup'd!
557 ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000558
Chris Lattner30c89792001-09-07 16:35:17 +0000559 Instruction::UnaryOps UnaryOpVal;
560 Instruction::BinaryOps BinaryOpVal;
561 Instruction::TermOps TermOpVal;
562 Instruction::MemoryOps MemOpVal;
563 Instruction::OtherOps OtherOpVal;
Chris Lattner00950542001-06-06 20:29:01 +0000564}
565
566%type <ModuleVal> Module MethodList
Chris Lattnere1815642001-07-15 06:35:53 +0000567%type <MethodVal> Method MethodProto MethodHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000568%type <BasicBlockVal> BasicBlock InstructionList
569%type <TermInstVal> BBTerminatorInst
570%type <InstVal> Inst InstVal MemoryInst
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000571%type <ConstVal> ConstVal
Chris Lattner027dcc52001-07-08 21:10:27 +0000572%type <ConstVector> ConstVector UByteList
Chris Lattner00950542001-06-06 20:29:01 +0000573%type <MethodArgList> ArgList ArgListH
574%type <MethArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000575%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000576%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner30c89792001-09-07 16:35:17 +0000577%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000578%type <JumpTable> JumpTable
Chris Lattner1781aca2001-09-18 04:00:54 +0000579%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Chris Lattner00950542001-06-06 20:29:01 +0000580
581%type <ValIDVal> ValueRef ConstValueRef // Reference to a definition or BB
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000582%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000583// Tokens and types for handling constant integer values
584//
585// ESINT64VAL - A negative number within long long range
586%token <SInt64Val> ESINT64VAL
587
588// EUINT64VAL - A positive number within uns. long long range
589%token <UInt64Val> EUINT64VAL
590%type <SInt64Val> EINT64VAL
591
592%token <SIntVal> SINTVAL // Signed 32 bit ints...
593%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
594%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000595%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000596
597// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000598%type <TypeVal> Types TypesV UpRTypes UpRTypesV
599%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
600%token <TypeVal> OPAQUE
601%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
602%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000603
604%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
605%type <StrVal> OptVAR_ID OptAssign
606
607
Chris Lattner1781aca2001-09-18 04:00:54 +0000608%token IMPLEMENTATION TRUE FALSE BEGINTOK END DECLARE GLOBAL CONSTANT UNINIT
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000609%token TO DOTDOTDOT STRING NULL_TOK CONST
Chris Lattner00950542001-06-06 20:29:01 +0000610
611// Basic Block Terminating Operators
612%token <TermOpVal> RET BR SWITCH
613
614// Unary Operators
615%type <UnaryOpVal> UnaryOps // all the unary operators
Chris Lattner71496b32001-07-08 19:03:27 +0000616%token <UnaryOpVal> NOT
Chris Lattner00950542001-06-06 20:29:01 +0000617
618// Binary Operators
619%type <BinaryOpVal> BinaryOps // all the binary operators
620%token <BinaryOpVal> ADD SUB MUL DIV REM
Chris Lattner027dcc52001-07-08 21:10:27 +0000621%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000622
623// Memory Instructions
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000624%token <MemoryOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000625
Chris Lattner027dcc52001-07-08 21:10:27 +0000626// Other Operators
627%type <OtherOpVal> ShiftOps
628%token <OtherOpVal> PHI CALL CAST SHL SHR
629
Chris Lattner00950542001-06-06 20:29:01 +0000630%start Module
631%%
632
633// Handle constant integer size restriction and conversion...
634//
635
636INTVAL : SINTVAL
637INTVAL : UINTVAL {
638 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
639 ThrowException("Value too large for type!");
640 $$ = (int32_t)$1;
641}
642
643
644EINT64VAL : ESINT64VAL // These have same type and can't cause problems...
645EINT64VAL : EUINT64VAL {
646 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
647 ThrowException("Value too large for type!");
648 $$ = (int64_t)$1;
649}
650
Chris Lattner00950542001-06-06 20:29:01 +0000651// Operations that are notably excluded from this list include:
652// RET, BR, & SWITCH because they end basic blocks and are treated specially.
653//
Chris Lattner09083092001-07-08 04:57:15 +0000654UnaryOps : NOT
Chris Lattner00950542001-06-06 20:29:01 +0000655BinaryOps : ADD | SUB | MUL | DIV | REM
656BinaryOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE
Chris Lattner027dcc52001-07-08 21:10:27 +0000657ShiftOps : SHL | SHR
Chris Lattner00950542001-06-06 20:29:01 +0000658
Chris Lattnere98dda62001-07-14 06:10:16 +0000659// These are some types that allow classification if we only want a particular
660// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner00950542001-06-06 20:29:01 +0000661SIntType : LONG | INT | SHORT | SBYTE
662UIntType : ULONG | UINT | USHORT | UBYTE
Chris Lattner30c89792001-09-07 16:35:17 +0000663IntType : SIntType | UIntType
664FPType : FLOAT | DOUBLE
Chris Lattner00950542001-06-06 20:29:01 +0000665
Chris Lattnere98dda62001-07-14 06:10:16 +0000666// OptAssign - Value producing statements have an optional assignment component
Chris Lattner00950542001-06-06 20:29:01 +0000667OptAssign : VAR_ID '=' {
668 $$ = $1;
669 }
670 | /*empty*/ {
671 $$ = 0;
672 }
673
Chris Lattner30c89792001-09-07 16:35:17 +0000674
675//===----------------------------------------------------------------------===//
676// Types includes all predefined types... except void, because it can only be
677// used in specific contexts (method returning void for example). To have
678// access to it, a user must explicitly use TypesV.
679//
680
681// TypesV includes all of 'Types', but it also includes the void type.
682TypesV : Types | VOID { $$ = newTH($1); }
683UpRTypesV : UpRTypes | VOID { $$ = newTH($1); }
684
685Types : UpRTypes {
686 TypeDone($$ = $1);
687 }
688
689
690// Derived types are added later...
691//
692PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT
693PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL
694UpRTypes : OPAQUE | PrimType { $$ = newTH($1); }
695UpRTypes : ValueRef { // Named types are also simple types...
696 $$ = newTH(getTypeVal($1));
697}
698
Chris Lattner30c89792001-09-07 16:35:17 +0000699// Include derived types in the Types production.
700//
701UpRTypes : '\\' EUINT64VAL { // Type UpReference
702 if ($2 > (uint64_t)INT64_MAX) ThrowException("Value out of range!");
703 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
704 UpRefs.push_back(make_pair((unsigned)$2, OT)); // Add to vector...
705 $$ = newTH<Type>(OT);
706 UR_OUT("New Upreference!\n");
707 }
708 | UpRTypesV '(' ArgTypeListI ')' { // Method derived type?
709 vector<const Type*> Params;
710 mapto($3->begin(), $3->end(), back_inserter(Params),
711 mem_fun_ref(&PATypeHandle<Type>::get));
712 $$ = newTH(HandleUpRefs(MethodType::get(*$1, Params)));
713 delete $3; // Delete the argument list
714 delete $1; // Delete the old type handle
715 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000716 | '[' UpRTypesV ']' { // Unsized array type?
717 $$ = newTH<Type>(HandleUpRefs(ArrayType::get(*$2)));
718 delete $2;
Chris Lattner30c89792001-09-07 16:35:17 +0000719 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000720 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
721 $$ = newTH<Type>(HandleUpRefs(ArrayType::get(*$4, (int)$2)));
722 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +0000723 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000724 | '{' TypeListI '}' { // Structure type?
725 vector<const Type*> Elements;
726 mapto($2->begin(), $2->end(), back_inserter(Elements),
727 mem_fun_ref(&PATypeHandle<Type>::get));
Chris Lattner30c89792001-09-07 16:35:17 +0000728
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000729 $$ = newTH<Type>(HandleUpRefs(StructType::get(Elements)));
730 delete $2;
731 }
732 | '{' '}' { // Empty structure type?
733 $$ = newTH<Type>(StructType::get(vector<const Type*>()));
734 }
735 | UpRTypes '*' { // Pointer type?
736 $$ = newTH<Type>(HandleUpRefs(PointerType::get(*$1)));
737 delete $1;
738 }
Chris Lattner30c89792001-09-07 16:35:17 +0000739
740// TypeList - Used for struct declarations and as a basis for method type
741// declaration type lists
742//
743TypeListI : UpRTypes {
744 $$ = new list<PATypeHolder<Type> >();
745 $$->push_back(*$1); delete $1;
746 }
747 | TypeListI ',' UpRTypes {
748 ($$=$1)->push_back(*$3); delete $3;
749 }
750
751// ArgTypeList - List of types for a method type declaration...
752ArgTypeListI : TypeListI
753 | TypeListI ',' DOTDOTDOT {
754 ($$=$1)->push_back(Type::VoidTy);
755 }
756 | DOTDOTDOT {
757 ($$ = new list<PATypeHolder<Type> >())->push_back(Type::VoidTy);
758 }
759 | /*empty*/ {
760 $$ = new list<PATypeHolder<Type> >();
761 }
762
763
Chris Lattnere98dda62001-07-14 06:10:16 +0000764// ConstVal - The various declarations that go into the constant pool. This
765// includes all forward declarations of types, constants, and functions.
766//
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000767ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
768 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
769 if (ATy == 0)
770 ThrowException("Cannot make array constant with type: '" +
771 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +0000772 const Type *ETy = ATy->getElementType();
773 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +0000774
Chris Lattner30c89792001-09-07 16:35:17 +0000775 // Verify that we have the correct size...
776 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +0000777 ThrowException("Type mismatch: constant sized array initialized with " +
Chris Lattner30c89792001-09-07 16:35:17 +0000778 utostr($3->size()) + " arguments, but has size of " +
779 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +0000780
Chris Lattner30c89792001-09-07 16:35:17 +0000781 // Verify all elements are correct type!
782 for (unsigned i = 0; i < $3->size(); i++) {
783 if (ETy != (*$3)[i]->getType())
Chris Lattner00950542001-06-06 20:29:01 +0000784 ThrowException("Element #" + utostr(i) + " is not of type '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000785 ETy->getName() + "' as required!\nIt is of type '" +
786 (*$3)[i]->getType()->getName() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +0000787 }
788
Chris Lattner30c89792001-09-07 16:35:17 +0000789 $$ = ConstPoolArray::get(ATy, *$3);
790 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000791 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000792 | Types '[' ']' {
793 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
794 if (ATy == 0)
795 ThrowException("Cannot make array constant with type: '" +
796 (*$1)->getDescription() + "'!");
797
798 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +0000799 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +0000800 ThrowException("Type mismatch: constant sized array initialized with 0"
Chris Lattner30c89792001-09-07 16:35:17 +0000801 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000802 $$ = ConstPoolArray::get(ATy, vector<ConstPoolVal*>());
Chris Lattner30c89792001-09-07 16:35:17 +0000803 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +0000804 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000805 | Types 'c' STRINGCONSTANT {
806 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
807 if (ATy == 0)
808 ThrowException("Cannot make array constant with type: '" +
809 (*$1)->getDescription() + "'!");
810
Chris Lattner30c89792001-09-07 16:35:17 +0000811 int NumElements = ATy->getNumElements();
812 const Type *ETy = ATy->getElementType();
813 char *EndStr = UnEscapeLexed($3, true);
814 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +0000815 ThrowException("Can't build string constant of size " +
Chris Lattner30c89792001-09-07 16:35:17 +0000816 itostr((int)(EndStr-$3)) +
817 " when array has size " + itostr(NumElements) + "!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000818 vector<ConstPoolVal*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +0000819 if (ETy == Type::SByteTy) {
820 for (char *C = $3; C != EndStr; ++C)
821 Vals.push_back(ConstPoolSInt::get(ETy, *C));
822 } else if (ETy == Type::UByteTy) {
823 for (char *C = $3; C != EndStr; ++C)
824 Vals.push_back(ConstPoolUInt::get(ETy, *C));
Chris Lattner93750fa2001-07-28 17:48:55 +0000825 } else {
Chris Lattner30c89792001-09-07 16:35:17 +0000826 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +0000827 ThrowException("Cannot build string arrays of non byte sized elements!");
828 }
Chris Lattner30c89792001-09-07 16:35:17 +0000829 free($3);
830 $$ = ConstPoolArray::get(ATy, Vals);
831 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +0000832 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000833 | Types '{' ConstVector '}' {
834 const StructType *STy = dyn_cast<const StructType>($1->get());
835 if (STy == 0)
836 ThrowException("Cannot make struct constant with type: '" +
837 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +0000838 // FIXME: TODO: Check to see that the constants are compatible with the type
839 // initializer!
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000840 $$ = ConstPoolStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +0000841 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000842 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000843 | Types NULL_TOK {
844 const PointerType *PTy = dyn_cast<const PointerType>($1->get());
845 if (PTy == 0)
846 ThrowException("Cannot make null pointer constant with type: '" +
847 (*$1)->getDescription() + "'!");
848
849 $$ = ConstPoolPointer::getNullPointer(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000850 delete $1;
851 }
Chris Lattner00950542001-06-06 20:29:01 +0000852/*
853 | Types '*' ConstVal {
854 assert(0);
855 $$ = 0;
856 }
857*/
858
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000859ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000860 if (!ConstPoolSInt::isValueValidForType($1, $2))
861 ThrowException("Constant value doesn't fit in type!");
Chris Lattner30c89792001-09-07 16:35:17 +0000862 $$ = ConstPoolSInt::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000863 }
864 | UIntType EUINT64VAL { // integral constants
865 if (!ConstPoolUInt::isValueValidForType($1, $2))
866 ThrowException("Constant value doesn't fit in type!");
Chris Lattner30c89792001-09-07 16:35:17 +0000867 $$ = ConstPoolUInt::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000868 }
869 | BOOL TRUE { // Boolean constants
Chris Lattner30c89792001-09-07 16:35:17 +0000870 $$ = ConstPoolBool::True;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000871 }
872 | BOOL FALSE { // Boolean constants
Chris Lattner30c89792001-09-07 16:35:17 +0000873 $$ = ConstPoolBool::False;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000874 }
875 | FPType FPVAL { // Float & Double constants
Chris Lattner30c89792001-09-07 16:35:17 +0000876 $$ = ConstPoolFP::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000877 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000878
Chris Lattnere98dda62001-07-14 06:10:16 +0000879// ConstVector - A list of comma seperated constants.
Chris Lattner00950542001-06-06 20:29:01 +0000880ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +0000881 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +0000882 }
883 | ConstVal {
884 $$ = new vector<ConstPoolVal*>();
Chris Lattner30c89792001-09-07 16:35:17 +0000885 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +0000886 }
887
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000888
Chris Lattner1781aca2001-09-18 04:00:54 +0000889// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
890GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; }
891
Chris Lattner00950542001-06-06 20:29:01 +0000892
Chris Lattnere98dda62001-07-14 06:10:16 +0000893// ConstPool - Constants with optional names assigned to them.
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000894ConstPool : ConstPool OptAssign CONST ConstVal {
895 setValueName($4, $2);
896 InsertValue($4);
Chris Lattner00950542001-06-06 20:29:01 +0000897 }
Chris Lattner30c89792001-09-07 16:35:17 +0000898 | ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattner1781aca2001-09-18 04:00:54 +0000899 // TODO: FIXME when Type are not const
900 setValueName(const_cast<Type*>($4->get()), $2);
901
902 if (!$2) {
Chris Lattner30c89792001-09-07 16:35:17 +0000903 InsertType($4->get(),
904 CurMeth.CurrentMethod ? CurMeth.Types : CurModule.Types);
905 }
906 delete $4;
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000907
908 ResolveSomeTypes(CurMeth.CurrentMethod ? CurMeth.LateResolveTypes :
909 CurModule.LateResolveTypes);
Chris Lattner30c89792001-09-07 16:35:17 +0000910 }
911 | ConstPool MethodProto { // Method prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +0000912 }
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000913 | ConstPool OptAssign GlobalType ConstVal {
Chris Lattner1781aca2001-09-18 04:00:54 +0000914 const Type *Ty = $4->getType();
915 // Global declarations appear in Constant Pool
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000916 ConstPoolVal *Initializer = $4;
Chris Lattner1781aca2001-09-18 04:00:54 +0000917 if (Initializer == 0)
918 ThrowException("Global value initializer is not a constant!");
919
920 GlobalVariable *GV = new GlobalVariable(PointerType::get(Ty), $3,
921 Initializer);
922 setValueName(GV, $2);
923
924 CurModule.CurrentModule->getGlobalList().push_back(GV);
925 InsertValue(GV, CurModule.Values);
926 }
927 | ConstPool OptAssign UNINIT GlobalType Types {
928 const Type *Ty = *$5;
929 // Global declarations appear in Constant Pool
Chris Lattnercfe26c92001-10-01 18:26:53 +0000930 if (isa<ArrayType>(Ty) && cast<ArrayType>(Ty)->isUnsized()) {
Chris Lattner1781aca2001-09-18 04:00:54 +0000931 ThrowException("Type '" + Ty->getDescription() +
932 "' is not a sized type!");
Chris Lattner70cc3392001-09-10 07:58:01 +0000933 }
Chris Lattner1781aca2001-09-18 04:00:54 +0000934
935 GlobalVariable *GV = new GlobalVariable(PointerType::get(Ty), $4);
936 setValueName(GV, $2);
937
Chris Lattner70cc3392001-09-10 07:58:01 +0000938 CurModule.CurrentModule->getGlobalList().push_back(GV);
939 InsertValue(GV, CurModule.Values);
Chris Lattnere98dda62001-07-14 06:10:16 +0000940 }
Chris Lattner00950542001-06-06 20:29:01 +0000941 | /* empty: end of list */ {
942 }
943
944
945//===----------------------------------------------------------------------===//
946// Rules to match Modules
947//===----------------------------------------------------------------------===//
948
949// Module rule: Capture the result of parsing the whole file into a result
950// variable...
951//
952Module : MethodList {
953 $$ = ParserResult = $1;
954 CurModule.ModuleDone();
955}
956
Chris Lattnere98dda62001-07-14 06:10:16 +0000957// MethodList - A list of methods, preceeded by a constant pool.
958//
Chris Lattner00950542001-06-06 20:29:01 +0000959MethodList : MethodList Method {
Chris Lattner00950542001-06-06 20:29:01 +0000960 $$ = $1;
Chris Lattnere1815642001-07-15 06:35:53 +0000961 if (!$2->getParent())
962 $1->getMethodList().push_back($2);
963 CurMeth.MethodDone();
Chris Lattner00950542001-06-06 20:29:01 +0000964 }
Chris Lattnere1815642001-07-15 06:35:53 +0000965 | MethodList MethodProto {
966 $$ = $1;
Chris Lattnere1815642001-07-15 06:35:53 +0000967 }
Chris Lattner00950542001-06-06 20:29:01 +0000968 | ConstPool IMPLEMENTATION {
969 $$ = CurModule.CurrentModule;
Chris Lattner30c89792001-09-07 16:35:17 +0000970 // Resolve circular types before we parse the body of the module
971 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner00950542001-06-06 20:29:01 +0000972 }
973
974
975//===----------------------------------------------------------------------===//
976// Rules to match Method Headers
977//===----------------------------------------------------------------------===//
978
979OptVAR_ID : VAR_ID | /*empty*/ { $$ = 0; }
980
981ArgVal : Types OptVAR_ID {
Chris Lattner30c89792001-09-07 16:35:17 +0000982 $$ = new MethodArgument(*$1); delete $1;
Chris Lattner1781aca2001-09-18 04:00:54 +0000983 setValueName($$, $2);
Chris Lattner00950542001-06-06 20:29:01 +0000984}
985
986ArgListH : ArgVal ',' ArgListH {
987 $$ = $3;
988 $3->push_front($1);
989 }
990 | ArgVal {
991 $$ = new list<MethodArgument*>();
992 $$->push_front($1);
993 }
Chris Lattner8b81bf52001-07-25 22:47:46 +0000994 | DOTDOTDOT {
995 $$ = new list<MethodArgument*>();
996 $$->push_back(new MethodArgument(Type::VoidTy));
997 }
Chris Lattner00950542001-06-06 20:29:01 +0000998
999ArgList : ArgListH {
1000 $$ = $1;
1001 }
1002 | /* empty */ {
1003 $$ = 0;
1004 }
1005
1006MethodHeaderH : TypesV STRINGCONSTANT '(' ArgList ')' {
Chris Lattner93750fa2001-07-28 17:48:55 +00001007 UnEscapeLexed($2);
Chris Lattner30c89792001-09-07 16:35:17 +00001008 vector<const Type*> ParamTypeList;
Chris Lattner00950542001-06-06 20:29:01 +00001009 if ($4)
Chris Lattner7fc9fe32001-06-27 23:41:11 +00001010 for (list<MethodArgument*>::iterator I = $4->begin(); I != $4->end(); ++I)
Chris Lattner00950542001-06-06 20:29:01 +00001011 ParamTypeList.push_back((*I)->getType());
1012
Chris Lattner30c89792001-09-07 16:35:17 +00001013 const MethodType *MT = MethodType::get(*$1, ParamTypeList);
1014 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001015
Chris Lattnere1815642001-07-15 06:35:53 +00001016 Method *M = 0;
1017 if (SymbolTable *ST = CurModule.CurrentModule->getSymbolTable()) {
1018 if (Value *V = ST->lookup(MT, $2)) { // Method already in symtab?
Chris Lattner9636a912001-10-01 16:18:37 +00001019 M = cast<Method>(V);
Chris Lattner00950542001-06-06 20:29:01 +00001020
Chris Lattnere1815642001-07-15 06:35:53 +00001021 // Yes it is. If this is the case, either we need to be a forward decl,
1022 // or it needs to be.
1023 if (!CurMeth.isDeclare && !M->isExternal())
1024 ThrowException("Redefinition of method '" + string($2) + "'!");
1025 }
1026 }
1027
1028 if (M == 0) { // Not already defined?
1029 M = new Method(MT, $2);
1030 InsertValue(M, CurModule.Values);
1031 }
1032
1033 free($2); // Free strdup'd memory!
Chris Lattner00950542001-06-06 20:29:01 +00001034
1035 CurMeth.MethodStart(M);
1036
1037 // Add all of the arguments we parsed to the method...
Chris Lattnere1815642001-07-15 06:35:53 +00001038 if ($4 && !CurMeth.isDeclare) { // Is null if empty...
Chris Lattner00950542001-06-06 20:29:01 +00001039 Method::ArgumentListType &ArgList = M->getArgumentList();
1040
Chris Lattner7fc9fe32001-06-27 23:41:11 +00001041 for (list<MethodArgument*>::iterator I = $4->begin(); I != $4->end(); ++I) {
Chris Lattner00950542001-06-06 20:29:01 +00001042 InsertValue(*I);
1043 ArgList.push_back(*I);
1044 }
1045 delete $4; // We're now done with the argument list
1046 }
1047}
1048
1049MethodHeader : MethodHeaderH ConstPool BEGINTOK {
1050 $$ = CurMeth.CurrentMethod;
Chris Lattner30c89792001-09-07 16:35:17 +00001051
1052 // Resolve circular types before we parse the body of the method.
1053 ResolveTypes(CurMeth.LateResolveTypes);
Chris Lattner00950542001-06-06 20:29:01 +00001054}
1055
1056Method : BasicBlockList END {
1057 $$ = $1;
1058}
1059
Chris Lattnere1815642001-07-15 06:35:53 +00001060MethodProto : DECLARE { CurMeth.isDeclare = true; } MethodHeaderH {
1061 $$ = CurMeth.CurrentMethod;
Chris Lattner93750fa2001-07-28 17:48:55 +00001062 if (!$$->getParent())
1063 CurModule.CurrentModule->getMethodList().push_back($$);
1064 CurMeth.MethodDone();
Chris Lattnere1815642001-07-15 06:35:53 +00001065}
Chris Lattner00950542001-06-06 20:29:01 +00001066
1067//===----------------------------------------------------------------------===//
1068// Rules to match Basic Blocks
1069//===----------------------------------------------------------------------===//
1070
1071ConstValueRef : ESINT64VAL { // A reference to a direct constant
1072 $$ = ValID::create($1);
1073 }
1074 | EUINT64VAL {
1075 $$ = ValID::create($1);
1076 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001077 | FPVAL { // Perhaps it's an FP constant?
1078 $$ = ValID::create($1);
1079 }
Chris Lattner00950542001-06-06 20:29:01 +00001080 | TRUE {
1081 $$ = ValID::create((int64_t)1);
1082 }
1083 | FALSE {
1084 $$ = ValID::create((int64_t)0);
1085 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001086 | NULL_TOK {
1087 $$ = ValID::createNull();
1088 }
1089
Chris Lattner93750fa2001-07-28 17:48:55 +00001090/*
Chris Lattner00950542001-06-06 20:29:01 +00001091 | STRINGCONSTANT { // Quoted strings work too... especially for methods
1092 $$ = ValID::create_conststr($1);
1093 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001094*/
Chris Lattner00950542001-06-06 20:29:01 +00001095
1096// ValueRef - A reference to a definition...
1097ValueRef : INTVAL { // Is it an integer reference...?
1098 $$ = ValID::create($1);
1099 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001100 | VAR_ID { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001101 $$ = ValID::create($1);
1102 }
1103 | ConstValueRef {
1104 $$ = $1;
1105 }
1106
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001107// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1108// type immediately preceeds the value reference, and allows complex constant
1109// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001110ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001111 $$ = getVal(*$1, $2); delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +00001112 }
Chris Lattner8b81bf52001-07-25 22:47:46 +00001113
Chris Lattner00950542001-06-06 20:29:01 +00001114
1115BasicBlockList : BasicBlockList BasicBlock {
1116 $1->getBasicBlocks().push_back($2);
1117 $$ = $1;
1118 }
1119 | MethodHeader BasicBlock { // Do not allow methods with 0 basic blocks
1120 $$ = $1; // in them...
1121 $1->getBasicBlocks().push_back($2);
1122 }
1123
1124
1125// Basic blocks are terminated by branching instructions:
1126// br, br/cc, switch, ret
1127//
1128BasicBlock : InstructionList BBTerminatorInst {
1129 $1->getInstList().push_back($2);
1130 InsertValue($1);
1131 $$ = $1;
1132 }
1133 | LABELSTR InstructionList BBTerminatorInst {
1134 $2->getInstList().push_back($3);
Chris Lattner30c89792001-09-07 16:35:17 +00001135 setValueName($2, $1);
Chris Lattner00950542001-06-06 20:29:01 +00001136
1137 InsertValue($2);
1138 $$ = $2;
1139 }
1140
1141InstructionList : InstructionList Inst {
1142 $1->getInstList().push_back($2);
1143 $$ = $1;
1144 }
1145 | /* empty */ {
1146 $$ = new BasicBlock();
1147 }
1148
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001149BBTerminatorInst : RET ResolvedVal { // Return with a result...
1150 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001151 }
1152 | RET VOID { // Return with no result...
1153 $$ = new ReturnInst();
1154 }
1155 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner9636a912001-10-01 16:18:37 +00001156 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $3)));
Chris Lattner00950542001-06-06 20:29:01 +00001157 } // Conditional Branch...
1158 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner9636a912001-10-01 16:18:37 +00001159 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $6)),
1160 cast<BasicBlock>(getVal(Type::LabelTy, $9)),
Chris Lattner00950542001-06-06 20:29:01 +00001161 getVal(Type::BoolTy, $3));
1162 }
1163 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1164 SwitchInst *S = new SwitchInst(getVal($2, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001165 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
Chris Lattner00950542001-06-06 20:29:01 +00001166 $$ = S;
1167
1168 list<pair<ConstPoolVal*, BasicBlock*> >::iterator I = $8->begin(),
1169 end = $8->end();
Chris Lattner7fc9fe32001-06-27 23:41:11 +00001170 for (; I != end; ++I)
Chris Lattner00950542001-06-06 20:29:01 +00001171 S->dest_push_back(I->first, I->second);
1172 }
1173
1174JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1175 $$ = $1;
Chris Lattnercfe26c92001-10-01 18:26:53 +00001176 ConstPoolVal *V = cast<ConstPoolVal>(getVal($2, $3, true));
Chris Lattner00950542001-06-06 20:29:01 +00001177 if (V == 0)
1178 ThrowException("May only switch on a constant pool value!");
1179
Chris Lattner9636a912001-10-01 16:18:37 +00001180 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($5, $6))));
Chris Lattner00950542001-06-06 20:29:01 +00001181 }
1182 | IntType ConstValueRef ',' LABEL ValueRef {
1183 $$ = new list<pair<ConstPoolVal*, BasicBlock*> >();
Chris Lattnercfe26c92001-10-01 18:26:53 +00001184 ConstPoolVal *V = cast<ConstPoolVal>(getVal($1, $2, true));
Chris Lattner00950542001-06-06 20:29:01 +00001185
1186 if (V == 0)
1187 ThrowException("May only switch on a constant pool value!");
1188
Chris Lattner9636a912001-10-01 16:18:37 +00001189 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($4, $5))));
Chris Lattner00950542001-06-06 20:29:01 +00001190 }
1191
1192Inst : OptAssign InstVal {
Chris Lattner1781aca2001-09-18 04:00:54 +00001193 setValueName($2, $1); // Is this definition named?? if so, assign the name...
Chris Lattner00950542001-06-06 20:29:01 +00001194
1195 InsertValue($2);
1196 $$ = $2;
1197}
1198
Chris Lattnerc24d2082001-06-11 15:04:20 +00001199PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
1200 $$ = new list<pair<Value*, BasicBlock*> >();
Chris Lattner30c89792001-09-07 16:35:17 +00001201 $$->push_back(make_pair(getVal(*$1, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001202 cast<BasicBlock>(getVal(Type::LabelTy, $5))));
Chris Lattner30c89792001-09-07 16:35:17 +00001203 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001204 }
1205 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1206 $$ = $1;
1207 $1->push_back(make_pair(getVal($1->front().first->getType(), $4),
Chris Lattner9636a912001-10-01 16:18:37 +00001208 cast<BasicBlock>(getVal(Type::LabelTy, $6))));
Chris Lattnerc24d2082001-06-11 15:04:20 +00001209 }
1210
1211
Chris Lattner30c89792001-09-07 16:35:17 +00001212ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner00950542001-06-06 20:29:01 +00001213 $$ = new list<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001214 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001215 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001216 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001217 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001218 $1->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001219 }
1220
1221// ValueRefListE - Just like ValueRefList, except that it may also be empty!
1222ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; }
1223
1224InstVal : BinaryOps Types ValueRef ',' ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001225 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001226 if ($$ == 0)
1227 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001228 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001229 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001230 | UnaryOps ResolvedVal {
1231 $$ = UnaryOperator::create($1, $2);
Chris Lattner00950542001-06-06 20:29:01 +00001232 if ($$ == 0)
1233 ThrowException("unary operator returned null!");
Chris Lattner09083092001-07-08 04:57:15 +00001234 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001235 | ShiftOps ResolvedVal ',' ResolvedVal {
1236 if ($4->getType() != Type::UByteTy)
1237 ThrowException("Shift amount must be ubyte!");
1238 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001239 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001240 | CAST ResolvedVal TO Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001241 $$ = new CastInst($2, *$4);
1242 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001243 }
Chris Lattnerc24d2082001-06-11 15:04:20 +00001244 | PHI PHIList {
1245 const Type *Ty = $2->front().first->getType();
1246 $$ = new PHINode(Ty);
Chris Lattner00950542001-06-06 20:29:01 +00001247 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001248 if ($2->front().first->getType() != Ty)
1249 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00001250 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001251 $2->pop_front();
1252 }
1253 delete $2; // Free the list...
1254 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001255 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001256 const MethodType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001257
Chris Lattner9636a912001-10-01 16:18:37 +00001258 if (!(Ty = dyn_cast<MethodType>($2->get()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001259 // Pull out the types of all of the arguments...
1260 vector<const Type*> ParamTypes;
1261 for (list<Value*>::iterator I = $5->begin(), E = $5->end(); I != E; ++I)
1262 ParamTypes.push_back((*I)->getType());
Chris Lattner30c89792001-09-07 16:35:17 +00001263 Ty = MethodType::get(*$2, ParamTypes);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001264 }
Chris Lattner30c89792001-09-07 16:35:17 +00001265 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001266
Chris Lattner8b81bf52001-07-25 22:47:46 +00001267 Value *V = getVal(Ty, $3); // Get the method we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00001268
Chris Lattner8b81bf52001-07-25 22:47:46 +00001269 // Create the call node...
1270 if (!$5) { // Has no arguments?
Chris Lattner9636a912001-10-01 16:18:37 +00001271 $$ = new CallInst(cast<Method>(V), vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00001272 } else { // Has arguments?
Chris Lattner00950542001-06-06 20:29:01 +00001273 // Loop through MethodType's arguments and ensure they are specified
1274 // correctly!
1275 //
1276 MethodType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001277 MethodType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
1278 list<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
1279
1280 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1281 if ((*ArgI)->getType() != *I)
1282 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner00950542001-06-06 20:29:01 +00001283 (*I)->getName() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00001284
Chris Lattner8b81bf52001-07-25 22:47:46 +00001285 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner00950542001-06-06 20:29:01 +00001286 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00001287
Chris Lattner9636a912001-10-01 16:18:37 +00001288 $$ = new CallInst(cast<Method>(V),
Chris Lattner8b81bf52001-07-25 22:47:46 +00001289 vector<Value*>($5->begin(), $5->end()));
1290 }
1291 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00001292 }
1293 | MemoryInst {
1294 $$ = $1;
1295 }
1296
Chris Lattner027dcc52001-07-08 21:10:27 +00001297// UByteList - List of ubyte values for load and store instructions
1298UByteList : ',' ConstVector {
1299 $$ = $2;
1300} | /* empty */ {
1301 $$ = new vector<ConstPoolVal*>();
1302}
1303
Chris Lattner00950542001-06-06 20:29:01 +00001304MemoryInst : MALLOC Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001305 $$ = new MallocInst(PointerType::get(*$2));
1306 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001307 }
1308 | MALLOC Types ',' UINT ValueRef {
Chris Lattnerb00c5822001-10-02 03:41:24 +00001309 if (!(*$2)->isArrayType() || cast<const ArrayType>($2->get())->isSized())
Chris Lattner30c89792001-09-07 16:35:17 +00001310 ThrowException("Trying to allocate " + (*$2)->getName() +
Chris Lattner00950542001-06-06 20:29:01 +00001311 " as unsized array!");
Chris Lattner30c89792001-09-07 16:35:17 +00001312 const Type *Ty = PointerType::get(*$2);
Chris Lattner8896eda2001-07-09 19:38:36 +00001313 $$ = new MallocInst(Ty, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001314 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001315 }
1316 | ALLOCA Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001317 $$ = new AllocaInst(PointerType::get(*$2));
1318 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001319 }
1320 | ALLOCA Types ',' UINT ValueRef {
Chris Lattnerb00c5822001-10-02 03:41:24 +00001321 if (!(*$2)->isArrayType() || cast<const ArrayType>($2->get())->isSized())
Chris Lattner30c89792001-09-07 16:35:17 +00001322 ThrowException("Trying to allocate " + (*$2)->getName() +
Chris Lattner00950542001-06-06 20:29:01 +00001323 " as unsized array!");
Chris Lattner30c89792001-09-07 16:35:17 +00001324 const Type *Ty = PointerType::get(*$2);
Chris Lattner00950542001-06-06 20:29:01 +00001325 Value *ArrSize = getVal($4, $5);
Chris Lattnerf0d0e9c2001-07-07 08:36:30 +00001326 $$ = new AllocaInst(Ty, ArrSize);
Chris Lattner30c89792001-09-07 16:35:17 +00001327 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001328 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001329 | FREE ResolvedVal {
1330 if (!$2->getType()->isPointerType())
1331 ThrowException("Trying to free nonpointer type " +
1332 $2->getType()->getName() + "!");
1333 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001334 }
1335
Chris Lattner027dcc52001-07-08 21:10:27 +00001336 | LOAD Types ValueRef UByteList {
Chris Lattner30c89792001-09-07 16:35:17 +00001337 if (!(*$2)->isPointerType())
1338 ThrowException("Can't load from nonpointer type: " + (*$2)->getName());
1339 if (LoadInst::getIndexedType(*$2, *$4) == 0)
Chris Lattner027dcc52001-07-08 21:10:27 +00001340 ThrowException("Invalid indices for load instruction!");
1341
Chris Lattner30c89792001-09-07 16:35:17 +00001342 $$ = new LoadInst(getVal(*$2, $3), *$4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001343 delete $4; // Free the vector...
Chris Lattner30c89792001-09-07 16:35:17 +00001344 delete $2;
Chris Lattner027dcc52001-07-08 21:10:27 +00001345 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001346 | STORE ResolvedVal ',' Types ValueRef UByteList {
Chris Lattner30c89792001-09-07 16:35:17 +00001347 if (!(*$4)->isPointerType())
1348 ThrowException("Can't store to a nonpointer type: " + (*$4)->getName());
1349 const Type *ElTy = StoreInst::getIndexedType(*$4, *$6);
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001350 if (ElTy == 0)
1351 ThrowException("Can't store into that field list!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001352 if (ElTy != $2->getType())
1353 ThrowException("Can't store '" + $2->getType()->getName() +
1354 "' into space of type '" + ElTy->getName() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001355 $$ = new StoreInst($2, getVal(*$4, $5), *$6);
1356 delete $4; delete $6;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001357 }
1358 | GETELEMENTPTR Types ValueRef UByteList {
Chris Lattner30c89792001-09-07 16:35:17 +00001359 if (!(*$2)->isPointerType())
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001360 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner30c89792001-09-07 16:35:17 +00001361 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
1362 ThrowException("Can't get element ptr '" + (*$2)->getName() + "'!");
1363 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
1364 delete $2; delete $4;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001365 }
Chris Lattner027dcc52001-07-08 21:10:27 +00001366
Chris Lattner00950542001-06-06 20:29:01 +00001367%%
Chris Lattner09083092001-07-08 04:57:15 +00001368int yyerror(const char *ErrorMsg) {
Chris Lattner00950542001-06-06 20:29:01 +00001369 ThrowException(string("Parse error: ") + ErrorMsg);
1370 return 0;
1371}