blob: a0c96733a0e76347cf4f3c8a59c2eb633f8f050e [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 Lattner30c89792001-09-07 16:35:17 +0000354
355// ResolveTypes - This goes through the forward referenced type table and makes
356// sure that all type references are complete. This code is executed after the
357// constant pool of a method or module is completely parsed.
Chris Lattner00950542001-06-06 20:29:01 +0000358//
Chris Lattner30c89792001-09-07 16:35:17 +0000359static void ResolveTypes(vector<PATypeHolder<Type> > &LateResolveTypes) {
360 while (!LateResolveTypes.empty()) {
361 const Type *Ty = LateResolveTypes.back();
362 ValID &DID = getValIDFromPlaceHolder(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000363
Chris Lattner30c89792001-09-07 16:35:17 +0000364 const Type *TheRealType = getTypeVal(DID, true);
365 if (TheRealType == 0) {
366 if (DID.Type == 1)
367 ThrowException("Reference to an invalid type: '" +DID.getName(),
368 getLineNumFromPlaceHolder(Ty));
369 else
370 ThrowException("Reference to an invalid type: #" + itostr(DID.Num),
371 getLineNumFromPlaceHolder(Ty));
Chris Lattner00950542001-06-06 20:29:01 +0000372 }
Chris Lattner30c89792001-09-07 16:35:17 +0000373
Chris Lattner30c89792001-09-07 16:35:17 +0000374 // Refine the opaque type we had to the new type we are getting.
Chris Lattnercfe26c92001-10-01 18:26:53 +0000375 cast<DerivedType>(Ty)->refineAbstractTypeTo(TheRealType);
Chris Lattner30c89792001-09-07 16:35:17 +0000376
377 // No need to delete type, refine does that for us.
378 LateResolveTypes.pop_back();
379 }
380}
381
Chris Lattner1781aca2001-09-18 04:00:54 +0000382// setValueName - Set the specified value to the name given. The name may be
383// null potentially, in which case this is a noop. The string passed in is
384// assumed to be a malloc'd string buffer, and is freed by this function.
385//
386static void setValueName(Value *V, char *NameStr) {
387 if (NameStr == 0) return;
388 string Name(NameStr); // Copy string
389 free(NameStr); // Free old string
390
Chris Lattner30c89792001-09-07 16:35:17 +0000391 SymbolTable *ST = CurMeth.CurrentMethod ?
392 CurMeth.CurrentMethod->getSymbolTableSure() :
393 CurModule.CurrentModule->getSymbolTableSure();
394
395 Value *Existing = ST->lookup(V->getType(), Name);
396 if (Existing) { // Inserting a name that is already defined???
397 // There is only one case where this is allowed: when we are refining an
398 // opaque type. In this case, Existing will be an opaque type.
Chris Lattnercfe26c92001-10-01 18:26:53 +0000399 if (const Type *Ty = cast<const Type>(Existing))
Chris Lattnerb00c5822001-10-02 03:41:24 +0000400 if (OpaqueType *OpTy = dyn_cast<OpaqueType>(Ty)) {
Chris Lattner30c89792001-09-07 16:35:17 +0000401 // We ARE replacing an opaque type!
Chris Lattnerb00c5822001-10-02 03:41:24 +0000402 OpTy->refineAbstractTypeTo(cast<Type>(V));
Chris Lattner30c89792001-09-07 16:35:17 +0000403 return;
404 }
405
Chris Lattner9636a912001-10-01 16:18:37 +0000406 // Otherwise, we are a simple redefinition of a value, check to see if it
407 // is defined the same as the old one...
408 if (const Type *Ty = dyn_cast<const Type>(Existing)) {
409 if (Ty == cast<const Type>(V)) return; // Yes, it's equal.
410 } else {
411
412 }
Chris Lattner30c89792001-09-07 16:35:17 +0000413 ThrowException("Redefinition of value name '" + Name + "' in the '" +
414 V->getType()->getDescription() + "' type plane!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000415 }
Chris Lattner00950542001-06-06 20:29:01 +0000416
Chris Lattner30c89792001-09-07 16:35:17 +0000417 V->setName(Name, ST);
Chris Lattner00950542001-06-06 20:29:01 +0000418}
419
Chris Lattner8896eda2001-07-09 19:38:36 +0000420
Chris Lattner30c89792001-09-07 16:35:17 +0000421//===----------------------------------------------------------------------===//
422// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000423//
Chris Lattner8896eda2001-07-09 19:38:36 +0000424
Chris Lattner30c89792001-09-07 16:35:17 +0000425// TypeContains - Returns true if Ty contains E in it.
426//
427static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattner3ff43872001-09-28 22:56:31 +0000428 return find(df_begin(Ty), df_end(Ty), E) != df_end(Ty);
Chris Lattner30c89792001-09-07 16:35:17 +0000429}
Chris Lattner698b56e2001-07-20 19:15:08 +0000430
Chris Lattner30c89792001-09-07 16:35:17 +0000431
432static vector<pair<unsigned, OpaqueType *> > UpRefs;
433
434static PATypeHolder<Type> HandleUpRefs(const Type *ty) {
435 PATypeHolder<Type> Ty(ty);
436 UR_OUT(UpRefs.size() << " upreferences active!\n");
437 for (unsigned i = 0; i < UpRefs.size(); ) {
438 UR_OUT("TypeContains(" << Ty->getDescription() << ", "
439 << UpRefs[i].second->getDescription() << ") = "
440 << TypeContains(Ty, UpRefs[i].second) << endl);
441 if (TypeContains(Ty, UpRefs[i].second)) {
442 unsigned Level = --UpRefs[i].first; // Decrement level of upreference
443 UR_OUT("Uplevel Ref Level = " << Level << endl);
444 if (Level == 0) { // Upreference should be resolved!
445 UR_OUT("About to resolve upreference!\n";
446 string OldName = UpRefs[i].second->getDescription());
447 UpRefs[i].second->refineAbstractTypeTo(Ty);
448 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
449 UR_OUT("Type '" << OldName << "' refined upreference to: "
450 << (const void*)Ty << ", " << Ty->getDescription() << endl);
451 continue;
452 }
453 }
454
455 ++i; // Otherwise, no resolve, move on...
Chris Lattner8896eda2001-07-09 19:38:36 +0000456 }
Chris Lattner30c89792001-09-07 16:35:17 +0000457 // FIXME: TODO: this should return the updated type
Chris Lattner8896eda2001-07-09 19:38:36 +0000458 return Ty;
459}
460
Chris Lattner30c89792001-09-07 16:35:17 +0000461template <class TypeTy>
462inline static void TypeDone(PATypeHolder<TypeTy> *Ty) {
463 if (UpRefs.size())
464 ThrowException("Invalid upreference in type: " + (*Ty)->getDescription());
465}
466
467// newTH - Allocate a new type holder for the specified type
468template <class TypeTy>
469inline static PATypeHolder<TypeTy> *newTH(const TypeTy *Ty) {
470 return new PATypeHolder<TypeTy>(Ty);
471}
472template <class TypeTy>
473inline static PATypeHolder<TypeTy> *newTH(const PATypeHolder<TypeTy> &TH) {
474 return new PATypeHolder<TypeTy>(TH);
475}
476
477
478// newTHC - Allocate a new type holder for the specified type that can be
479// casted to a new Type type.
480template <class TypeTy, class OldTy>
481inline static PATypeHolder<TypeTy> *newTHC(const PATypeHolder<OldTy> &Old) {
482 return new PATypeHolder<TypeTy>((const TypeTy*)Old.get());
483}
484
Chris Lattner8896eda2001-07-09 19:38:36 +0000485
Chris Lattner00950542001-06-06 20:29:01 +0000486//===----------------------------------------------------------------------===//
487// RunVMAsmParser - Define an interface to this parser
488//===----------------------------------------------------------------------===//
489//
Chris Lattnera2850432001-07-22 18:36:00 +0000490Module *RunVMAsmParser(const string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000491 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000492 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000493 llvmAsmlineno = 1; // Reset the current line number...
494
495 CurModule.CurrentModule = new Module(); // Allocate a new module to read
496 yyparse(); // Parse the file.
497 Module *Result = ParserResult;
Chris Lattner00950542001-06-06 20:29:01 +0000498 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
499 ParserResult = 0;
500
501 return Result;
502}
503
504%}
505
506%union {
Chris Lattner30c89792001-09-07 16:35:17 +0000507 Module *ModuleVal;
508 Method *MethodVal;
509 MethodArgument *MethArgVal;
510 BasicBlock *BasicBlockVal;
511 TerminatorInst *TermInstVal;
512 Instruction *InstVal;
513 ConstPoolVal *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000514
Chris Lattner30c89792001-09-07 16:35:17 +0000515 const Type *PrimType;
516 PATypeHolder<Type> *TypeVal;
517 PATypeHolder<ArrayType> *ArrayTypeTy;
518 PATypeHolder<StructType> *StructTypeTy;
Chris Lattner1a1cb112001-09-30 22:46:54 +0000519 PATypeHolder<PointerType> *PointerTypeTy;
Chris Lattner30c89792001-09-07 16:35:17 +0000520 Value *ValueVal;
521
522 list<MethodArgument*> *MethodArgList;
523 list<Value*> *ValueList;
524 list<PATypeHolder<Type> > *TypeList;
Chris Lattnerc24d2082001-06-11 15:04:20 +0000525 list<pair<Value*, BasicBlock*> > *PHIList; // Represent the RHS of PHI node
Chris Lattner00950542001-06-06 20:29:01 +0000526 list<pair<ConstPoolVal*, BasicBlock*> > *JumpTable;
Chris Lattner30c89792001-09-07 16:35:17 +0000527 vector<ConstPoolVal*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000528
Chris Lattner30c89792001-09-07 16:35:17 +0000529 int64_t SInt64Val;
530 uint64_t UInt64Val;
531 int SIntVal;
532 unsigned UIntVal;
533 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000534 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000535
Chris Lattner30c89792001-09-07 16:35:17 +0000536 char *StrVal; // This memory is strdup'd!
537 ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000538
Chris Lattner30c89792001-09-07 16:35:17 +0000539 Instruction::UnaryOps UnaryOpVal;
540 Instruction::BinaryOps BinaryOpVal;
541 Instruction::TermOps TermOpVal;
542 Instruction::MemoryOps MemOpVal;
543 Instruction::OtherOps OtherOpVal;
Chris Lattner00950542001-06-06 20:29:01 +0000544}
545
546%type <ModuleVal> Module MethodList
Chris Lattnere1815642001-07-15 06:35:53 +0000547%type <MethodVal> Method MethodProto MethodHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000548%type <BasicBlockVal> BasicBlock InstructionList
549%type <TermInstVal> BBTerminatorInst
550%type <InstVal> Inst InstVal MemoryInst
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000551%type <ConstVal> ConstVal ExtendedConstVal
Chris Lattner027dcc52001-07-08 21:10:27 +0000552%type <ConstVector> ConstVector UByteList
Chris Lattner00950542001-06-06 20:29:01 +0000553%type <MethodArgList> ArgList ArgListH
554%type <MethArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000555%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000556%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner30c89792001-09-07 16:35:17 +0000557%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000558%type <JumpTable> JumpTable
Chris Lattner1781aca2001-09-18 04:00:54 +0000559%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Chris Lattner00950542001-06-06 20:29:01 +0000560
561%type <ValIDVal> ValueRef ConstValueRef // Reference to a definition or BB
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000562%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000563// Tokens and types for handling constant integer values
564//
565// ESINT64VAL - A negative number within long long range
566%token <SInt64Val> ESINT64VAL
567
568// EUINT64VAL - A positive number within uns. long long range
569%token <UInt64Val> EUINT64VAL
570%type <SInt64Val> EINT64VAL
571
572%token <SIntVal> SINTVAL // Signed 32 bit ints...
573%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
574%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000575%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000576
577// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000578%type <TypeVal> Types TypesV UpRTypes UpRTypesV
579%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
580%token <TypeVal> OPAQUE
581%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
582%token <PrimType> FLOAT DOUBLE TYPE LABEL
583%type <ArrayTypeTy> ArrayType ArrayTypeI
584%type <StructTypeTy> StructType StructTypeI
Chris Lattner1a1cb112001-09-30 22:46:54 +0000585%type <PointerTypeTy> PointerType PointerTypeI
Chris Lattner00950542001-06-06 20:29:01 +0000586
587%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
588%type <StrVal> OptVAR_ID OptAssign
589
590
Chris Lattner1781aca2001-09-18 04:00:54 +0000591%token IMPLEMENTATION TRUE FALSE BEGINTOK END DECLARE GLOBAL CONSTANT UNINIT
Chris Lattner1a1cb112001-09-30 22:46:54 +0000592%token TO DOTDOTDOT STRING NULL_TOK
Chris Lattner00950542001-06-06 20:29:01 +0000593
594// Basic Block Terminating Operators
595%token <TermOpVal> RET BR SWITCH
596
597// Unary Operators
598%type <UnaryOpVal> UnaryOps // all the unary operators
Chris Lattner71496b32001-07-08 19:03:27 +0000599%token <UnaryOpVal> NOT
Chris Lattner00950542001-06-06 20:29:01 +0000600
601// Binary Operators
602%type <BinaryOpVal> BinaryOps // all the binary operators
603%token <BinaryOpVal> ADD SUB MUL DIV REM
Chris Lattner027dcc52001-07-08 21:10:27 +0000604%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000605
606// Memory Instructions
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000607%token <MemoryOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000608
Chris Lattner027dcc52001-07-08 21:10:27 +0000609// Other Operators
610%type <OtherOpVal> ShiftOps
611%token <OtherOpVal> PHI CALL CAST SHL SHR
612
Chris Lattner00950542001-06-06 20:29:01 +0000613%start Module
614%%
615
616// Handle constant integer size restriction and conversion...
617//
618
619INTVAL : SINTVAL
620INTVAL : UINTVAL {
621 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
622 ThrowException("Value too large for type!");
623 $$ = (int32_t)$1;
624}
625
626
627EINT64VAL : ESINT64VAL // These have same type and can't cause problems...
628EINT64VAL : EUINT64VAL {
629 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
630 ThrowException("Value too large for type!");
631 $$ = (int64_t)$1;
632}
633
Chris Lattner00950542001-06-06 20:29:01 +0000634// Operations that are notably excluded from this list include:
635// RET, BR, & SWITCH because they end basic blocks and are treated specially.
636//
Chris Lattner09083092001-07-08 04:57:15 +0000637UnaryOps : NOT
Chris Lattner00950542001-06-06 20:29:01 +0000638BinaryOps : ADD | SUB | MUL | DIV | REM
639BinaryOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE
Chris Lattner027dcc52001-07-08 21:10:27 +0000640ShiftOps : SHL | SHR
Chris Lattner00950542001-06-06 20:29:01 +0000641
Chris Lattnere98dda62001-07-14 06:10:16 +0000642// These are some types that allow classification if we only want a particular
643// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner00950542001-06-06 20:29:01 +0000644SIntType : LONG | INT | SHORT | SBYTE
645UIntType : ULONG | UINT | USHORT | UBYTE
Chris Lattner30c89792001-09-07 16:35:17 +0000646IntType : SIntType | UIntType
647FPType : FLOAT | DOUBLE
Chris Lattner00950542001-06-06 20:29:01 +0000648
Chris Lattnere98dda62001-07-14 06:10:16 +0000649// OptAssign - Value producing statements have an optional assignment component
Chris Lattner00950542001-06-06 20:29:01 +0000650OptAssign : VAR_ID '=' {
651 $$ = $1;
652 }
653 | /*empty*/ {
654 $$ = 0;
655 }
656
Chris Lattner30c89792001-09-07 16:35:17 +0000657
658//===----------------------------------------------------------------------===//
659// Types includes all predefined types... except void, because it can only be
660// used in specific contexts (method returning void for example). To have
661// access to it, a user must explicitly use TypesV.
662//
663
664// TypesV includes all of 'Types', but it also includes the void type.
665TypesV : Types | VOID { $$ = newTH($1); }
666UpRTypesV : UpRTypes | VOID { $$ = newTH($1); }
667
668Types : UpRTypes {
669 TypeDone($$ = $1);
670 }
671
672
673// Derived types are added later...
674//
675PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT
676PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL
677UpRTypes : OPAQUE | PrimType { $$ = newTH($1); }
678UpRTypes : ValueRef { // Named types are also simple types...
679 $$ = newTH(getTypeVal($1));
680}
681
682// ArrayTypeI - Internal version of ArrayType that can have incomplete uprefs
683//
684ArrayTypeI : '[' UpRTypesV ']' { // Unsized array type?
685 $$ = newTHC<ArrayType>(HandleUpRefs(ArrayType::get(*$2)));
686 delete $2;
687 }
688 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
689 $$ = newTHC<ArrayType>(HandleUpRefs(ArrayType::get(*$4, (int)$2)));
690 delete $4;
691 }
692
693StructTypeI : '{' TypeListI '}' { // Structure type?
694 vector<const Type*> Elements;
695 mapto($2->begin(), $2->end(), back_inserter(Elements),
696 mem_fun_ref(&PATypeHandle<Type>::get));
697
698 $$ = newTHC<StructType>(HandleUpRefs(StructType::get(Elements)));
699 delete $2;
700 }
701 | '{' '}' { // Empty structure type?
702 $$ = newTH(StructType::get(vector<const Type*>()));
703 }
704
Chris Lattner1a1cb112001-09-30 22:46:54 +0000705PointerTypeI : UpRTypes '*' { // Pointer type?
706 $$ = newTHC<PointerType>(HandleUpRefs(PointerType::get(*$1)));
707 delete $1; // Delete the type handle
708 }
Chris Lattner30c89792001-09-07 16:35:17 +0000709
710// Include derived types in the Types production.
711//
712UpRTypes : '\\' EUINT64VAL { // Type UpReference
713 if ($2 > (uint64_t)INT64_MAX) ThrowException("Value out of range!");
714 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
715 UpRefs.push_back(make_pair((unsigned)$2, OT)); // Add to vector...
716 $$ = newTH<Type>(OT);
717 UR_OUT("New Upreference!\n");
718 }
719 | UpRTypesV '(' ArgTypeListI ')' { // Method derived type?
720 vector<const Type*> Params;
721 mapto($3->begin(), $3->end(), back_inserter(Params),
722 mem_fun_ref(&PATypeHandle<Type>::get));
723 $$ = newTH(HandleUpRefs(MethodType::get(*$1, Params)));
724 delete $3; // Delete the argument list
725 delete $1; // Delete the old type handle
726 }
727 | ArrayTypeI { // [Un]sized array type?
728 $$ = newTHC<Type>(*$1); delete $1;
729 }
730 | StructTypeI { // Structure type?
731 $$ = newTHC<Type>(*$1); delete $1;
732 }
Chris Lattner1a1cb112001-09-30 22:46:54 +0000733 | PointerTypeI { // Pointer type?
734 $$ = newTHC<Type>(*$1); delete $1;
Chris Lattner30c89792001-09-07 16:35:17 +0000735 }
736
737// Define some helpful top level types that do not allow UpReferences to escape
738//
Chris Lattner1a1cb112001-09-30 22:46:54 +0000739ArrayType : ArrayTypeI { TypeDone($$ = $1); }
740StructType : StructTypeI { TypeDone($$ = $1); }
741PointerType : PointerTypeI { TypeDone($$ = $1); }
Chris Lattner30c89792001-09-07 16:35:17 +0000742
743
744// TypeList - Used for struct declarations and as a basis for method type
745// declaration type lists
746//
747TypeListI : UpRTypes {
748 $$ = new list<PATypeHolder<Type> >();
749 $$->push_back(*$1); delete $1;
750 }
751 | TypeListI ',' UpRTypes {
752 ($$=$1)->push_back(*$3); delete $3;
753 }
754
755// ArgTypeList - List of types for a method type declaration...
756ArgTypeListI : TypeListI
757 | TypeListI ',' DOTDOTDOT {
758 ($$=$1)->push_back(Type::VoidTy);
759 }
760 | DOTDOTDOT {
761 ($$ = new list<PATypeHolder<Type> >())->push_back(Type::VoidTy);
762 }
763 | /*empty*/ {
764 $$ = new list<PATypeHolder<Type> >();
765 }
766
767
Chris Lattnere98dda62001-07-14 06:10:16 +0000768// ConstVal - The various declarations that go into the constant pool. This
769// includes all forward declarations of types, constants, and functions.
770//
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000771// This is broken into two sections: ExtendedConstVal and ConstVal
772//
Chris Lattner30c89792001-09-07 16:35:17 +0000773ExtendedConstVal: ArrayType '[' ConstVector ']' { // Nonempty unsized arr
774 const ArrayType *ATy = *$1;
775 const Type *ETy = ATy->getElementType();
776 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +0000777
Chris Lattner30c89792001-09-07 16:35:17 +0000778 // Verify that we have the correct size...
779 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +0000780 ThrowException("Type mismatch: constant sized array initialized with " +
Chris Lattner30c89792001-09-07 16:35:17 +0000781 utostr($3->size()) + " arguments, but has size of " +
782 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +0000783
Chris Lattner30c89792001-09-07 16:35:17 +0000784 // Verify all elements are correct type!
785 for (unsigned i = 0; i < $3->size(); i++) {
786 if (ETy != (*$3)[i]->getType())
Chris Lattner00950542001-06-06 20:29:01 +0000787 ThrowException("Element #" + utostr(i) + " is not of type '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000788 ETy->getName() + "' as required!\nIt is of type '" +
789 (*$3)[i]->getType()->getName() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +0000790 }
791
Chris Lattner30c89792001-09-07 16:35:17 +0000792 $$ = ConstPoolArray::get(ATy, *$3);
793 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000794 }
Chris Lattner30c89792001-09-07 16:35:17 +0000795 | ArrayType '[' ']' {
796 int NumElements = (*$1)->getNumElements();
797 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +0000798 ThrowException("Type mismatch: constant sized array initialized with 0"
Chris Lattner30c89792001-09-07 16:35:17 +0000799 " arguments, but has size of " + itostr(NumElements) +"!");
800 $$ = ConstPoolArray::get((*$1), vector<ConstPoolVal*>());
801 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +0000802 }
Chris Lattner30c89792001-09-07 16:35:17 +0000803 | ArrayType 'c' STRINGCONSTANT {
804 const ArrayType *ATy = *$1;
805 int NumElements = ATy->getNumElements();
806 const Type *ETy = ATy->getElementType();
807 char *EndStr = UnEscapeLexed($3, true);
808 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +0000809 ThrowException("Can't build string constant of size " +
Chris Lattner30c89792001-09-07 16:35:17 +0000810 itostr((int)(EndStr-$3)) +
811 " when array has size " + itostr(NumElements) + "!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000812 vector<ConstPoolVal*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +0000813 if (ETy == Type::SByteTy) {
814 for (char *C = $3; C != EndStr; ++C)
815 Vals.push_back(ConstPoolSInt::get(ETy, *C));
816 } else if (ETy == Type::UByteTy) {
817 for (char *C = $3; C != EndStr; ++C)
818 Vals.push_back(ConstPoolUInt::get(ETy, *C));
Chris Lattner93750fa2001-07-28 17:48:55 +0000819 } else {
Chris Lattner30c89792001-09-07 16:35:17 +0000820 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +0000821 ThrowException("Cannot build string arrays of non byte sized elements!");
822 }
Chris Lattner30c89792001-09-07 16:35:17 +0000823 free($3);
824 $$ = ConstPoolArray::get(ATy, Vals);
825 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +0000826 }
Chris Lattner30c89792001-09-07 16:35:17 +0000827 | StructType '{' ConstVector '}' {
828 // FIXME: TODO: Check to see that the constants are compatible with the type
829 // initializer!
830 $$ = ConstPoolStruct::get(*$1, *$3);
831 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000832 }
833/*
834 | Types '*' ConstVal {
835 assert(0);
836 $$ = 0;
837 }
838*/
839
Chris Lattner93750fa2001-07-28 17:48:55 +0000840ConstVal : ExtendedConstVal {
841 $$ = $1;
842 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000843 | SIntType EINT64VAL { // integral constants
844 if (!ConstPoolSInt::isValueValidForType($1, $2))
845 ThrowException("Constant value doesn't fit in type!");
Chris Lattner30c89792001-09-07 16:35:17 +0000846 $$ = ConstPoolSInt::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000847 }
848 | UIntType EUINT64VAL { // integral constants
849 if (!ConstPoolUInt::isValueValidForType($1, $2))
850 ThrowException("Constant value doesn't fit in type!");
Chris Lattner30c89792001-09-07 16:35:17 +0000851 $$ = ConstPoolUInt::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000852 }
853 | BOOL TRUE { // Boolean constants
Chris Lattner30c89792001-09-07 16:35:17 +0000854 $$ = ConstPoolBool::True;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000855 }
856 | BOOL FALSE { // Boolean constants
Chris Lattner30c89792001-09-07 16:35:17 +0000857 $$ = ConstPoolBool::False;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000858 }
859 | FPType FPVAL { // Float & Double constants
Chris Lattner30c89792001-09-07 16:35:17 +0000860 $$ = ConstPoolFP::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000861 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000862
Chris Lattnere98dda62001-07-14 06:10:16 +0000863// ConstVector - A list of comma seperated constants.
Chris Lattner00950542001-06-06 20:29:01 +0000864ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +0000865 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +0000866 }
867 | ConstVal {
868 $$ = new vector<ConstPoolVal*>();
Chris Lattner30c89792001-09-07 16:35:17 +0000869 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +0000870 }
871
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000872
Chris Lattner1781aca2001-09-18 04:00:54 +0000873// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
874GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; }
875
Chris Lattner00950542001-06-06 20:29:01 +0000876
Chris Lattnere98dda62001-07-14 06:10:16 +0000877// ConstPool - Constants with optional names assigned to them.
Chris Lattner00950542001-06-06 20:29:01 +0000878ConstPool : ConstPool OptAssign ConstVal {
Chris Lattner1781aca2001-09-18 04:00:54 +0000879 setValueName($3, $2);
Chris Lattner30c89792001-09-07 16:35:17 +0000880 InsertValue($3);
Chris Lattner00950542001-06-06 20:29:01 +0000881 }
Chris Lattner30c89792001-09-07 16:35:17 +0000882 | ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattner1781aca2001-09-18 04:00:54 +0000883 // TODO: FIXME when Type are not const
884 setValueName(const_cast<Type*>($4->get()), $2);
885
886 if (!$2) {
Chris Lattner30c89792001-09-07 16:35:17 +0000887 InsertType($4->get(),
888 CurMeth.CurrentMethod ? CurMeth.Types : CurModule.Types);
889 }
890 delete $4;
891 }
892 | ConstPool MethodProto { // Method prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +0000893 }
Chris Lattner1781aca2001-09-18 04:00:54 +0000894 | ConstPool OptAssign GlobalType ResolvedVal {
895 const Type *Ty = $4->getType();
896 // Global declarations appear in Constant Pool
Chris Lattnercfe26c92001-10-01 18:26:53 +0000897 ConstPoolVal *Initializer = cast<ConstPoolVal>($4);
Chris Lattner1781aca2001-09-18 04:00:54 +0000898 if (Initializer == 0)
899 ThrowException("Global value initializer is not a constant!");
900
901 GlobalVariable *GV = new GlobalVariable(PointerType::get(Ty), $3,
902 Initializer);
903 setValueName(GV, $2);
904
905 CurModule.CurrentModule->getGlobalList().push_back(GV);
906 InsertValue(GV, CurModule.Values);
907 }
908 | ConstPool OptAssign UNINIT GlobalType Types {
909 const Type *Ty = *$5;
910 // Global declarations appear in Constant Pool
Chris Lattnercfe26c92001-10-01 18:26:53 +0000911 if (isa<ArrayType>(Ty) && cast<ArrayType>(Ty)->isUnsized()) {
Chris Lattner1781aca2001-09-18 04:00:54 +0000912 ThrowException("Type '" + Ty->getDescription() +
913 "' is not a sized type!");
Chris Lattner70cc3392001-09-10 07:58:01 +0000914 }
Chris Lattner1781aca2001-09-18 04:00:54 +0000915
916 GlobalVariable *GV = new GlobalVariable(PointerType::get(Ty), $4);
917 setValueName(GV, $2);
918
Chris Lattner70cc3392001-09-10 07:58:01 +0000919 CurModule.CurrentModule->getGlobalList().push_back(GV);
920 InsertValue(GV, CurModule.Values);
Chris Lattnere98dda62001-07-14 06:10:16 +0000921 }
Chris Lattner00950542001-06-06 20:29:01 +0000922 | /* empty: end of list */ {
923 }
924
925
926//===----------------------------------------------------------------------===//
927// Rules to match Modules
928//===----------------------------------------------------------------------===//
929
930// Module rule: Capture the result of parsing the whole file into a result
931// variable...
932//
933Module : MethodList {
934 $$ = ParserResult = $1;
935 CurModule.ModuleDone();
936}
937
Chris Lattnere98dda62001-07-14 06:10:16 +0000938// MethodList - A list of methods, preceeded by a constant pool.
939//
Chris Lattner00950542001-06-06 20:29:01 +0000940MethodList : MethodList Method {
Chris Lattner00950542001-06-06 20:29:01 +0000941 $$ = $1;
Chris Lattnere1815642001-07-15 06:35:53 +0000942 if (!$2->getParent())
943 $1->getMethodList().push_back($2);
944 CurMeth.MethodDone();
Chris Lattner00950542001-06-06 20:29:01 +0000945 }
Chris Lattnere1815642001-07-15 06:35:53 +0000946 | MethodList MethodProto {
947 $$ = $1;
Chris Lattnere1815642001-07-15 06:35:53 +0000948 }
Chris Lattner00950542001-06-06 20:29:01 +0000949 | ConstPool IMPLEMENTATION {
950 $$ = CurModule.CurrentModule;
Chris Lattner30c89792001-09-07 16:35:17 +0000951 // Resolve circular types before we parse the body of the module
952 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner00950542001-06-06 20:29:01 +0000953 }
954
955
956//===----------------------------------------------------------------------===//
957// Rules to match Method Headers
958//===----------------------------------------------------------------------===//
959
960OptVAR_ID : VAR_ID | /*empty*/ { $$ = 0; }
961
962ArgVal : Types OptVAR_ID {
Chris Lattner30c89792001-09-07 16:35:17 +0000963 $$ = new MethodArgument(*$1); delete $1;
Chris Lattner1781aca2001-09-18 04:00:54 +0000964 setValueName($$, $2);
Chris Lattner00950542001-06-06 20:29:01 +0000965}
966
967ArgListH : ArgVal ',' ArgListH {
968 $$ = $3;
969 $3->push_front($1);
970 }
971 | ArgVal {
972 $$ = new list<MethodArgument*>();
973 $$->push_front($1);
974 }
Chris Lattner8b81bf52001-07-25 22:47:46 +0000975 | DOTDOTDOT {
976 $$ = new list<MethodArgument*>();
977 $$->push_back(new MethodArgument(Type::VoidTy));
978 }
Chris Lattner00950542001-06-06 20:29:01 +0000979
980ArgList : ArgListH {
981 $$ = $1;
982 }
983 | /* empty */ {
984 $$ = 0;
985 }
986
987MethodHeaderH : TypesV STRINGCONSTANT '(' ArgList ')' {
Chris Lattner93750fa2001-07-28 17:48:55 +0000988 UnEscapeLexed($2);
Chris Lattner30c89792001-09-07 16:35:17 +0000989 vector<const Type*> ParamTypeList;
Chris Lattner00950542001-06-06 20:29:01 +0000990 if ($4)
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000991 for (list<MethodArgument*>::iterator I = $4->begin(); I != $4->end(); ++I)
Chris Lattner00950542001-06-06 20:29:01 +0000992 ParamTypeList.push_back((*I)->getType());
993
Chris Lattner30c89792001-09-07 16:35:17 +0000994 const MethodType *MT = MethodType::get(*$1, ParamTypeList);
995 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +0000996
Chris Lattnere1815642001-07-15 06:35:53 +0000997 Method *M = 0;
998 if (SymbolTable *ST = CurModule.CurrentModule->getSymbolTable()) {
999 if (Value *V = ST->lookup(MT, $2)) { // Method already in symtab?
Chris Lattner9636a912001-10-01 16:18:37 +00001000 M = cast<Method>(V);
Chris Lattner00950542001-06-06 20:29:01 +00001001
Chris Lattnere1815642001-07-15 06:35:53 +00001002 // Yes it is. If this is the case, either we need to be a forward decl,
1003 // or it needs to be.
1004 if (!CurMeth.isDeclare && !M->isExternal())
1005 ThrowException("Redefinition of method '" + string($2) + "'!");
1006 }
1007 }
1008
1009 if (M == 0) { // Not already defined?
1010 M = new Method(MT, $2);
1011 InsertValue(M, CurModule.Values);
1012 }
1013
1014 free($2); // Free strdup'd memory!
Chris Lattner00950542001-06-06 20:29:01 +00001015
1016 CurMeth.MethodStart(M);
1017
1018 // Add all of the arguments we parsed to the method...
Chris Lattnere1815642001-07-15 06:35:53 +00001019 if ($4 && !CurMeth.isDeclare) { // Is null if empty...
Chris Lattner00950542001-06-06 20:29:01 +00001020 Method::ArgumentListType &ArgList = M->getArgumentList();
1021
Chris Lattner7fc9fe32001-06-27 23:41:11 +00001022 for (list<MethodArgument*>::iterator I = $4->begin(); I != $4->end(); ++I) {
Chris Lattner00950542001-06-06 20:29:01 +00001023 InsertValue(*I);
1024 ArgList.push_back(*I);
1025 }
1026 delete $4; // We're now done with the argument list
1027 }
1028}
1029
1030MethodHeader : MethodHeaderH ConstPool BEGINTOK {
1031 $$ = CurMeth.CurrentMethod;
Chris Lattner30c89792001-09-07 16:35:17 +00001032
1033 // Resolve circular types before we parse the body of the method.
1034 ResolveTypes(CurMeth.LateResolveTypes);
Chris Lattner00950542001-06-06 20:29:01 +00001035}
1036
1037Method : BasicBlockList END {
1038 $$ = $1;
1039}
1040
Chris Lattnere1815642001-07-15 06:35:53 +00001041MethodProto : DECLARE { CurMeth.isDeclare = true; } MethodHeaderH {
1042 $$ = CurMeth.CurrentMethod;
Chris Lattner93750fa2001-07-28 17:48:55 +00001043 if (!$$->getParent())
1044 CurModule.CurrentModule->getMethodList().push_back($$);
1045 CurMeth.MethodDone();
Chris Lattnere1815642001-07-15 06:35:53 +00001046}
Chris Lattner00950542001-06-06 20:29:01 +00001047
1048//===----------------------------------------------------------------------===//
1049// Rules to match Basic Blocks
1050//===----------------------------------------------------------------------===//
1051
1052ConstValueRef : ESINT64VAL { // A reference to a direct constant
1053 $$ = ValID::create($1);
1054 }
1055 | EUINT64VAL {
1056 $$ = ValID::create($1);
1057 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001058 | FPVAL { // Perhaps it's an FP constant?
1059 $$ = ValID::create($1);
1060 }
Chris Lattner00950542001-06-06 20:29:01 +00001061 | TRUE {
1062 $$ = ValID::create((int64_t)1);
1063 }
1064 | FALSE {
1065 $$ = ValID::create((int64_t)0);
1066 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001067 | NULL_TOK {
1068 $$ = ValID::createNull();
1069 }
1070
Chris Lattner93750fa2001-07-28 17:48:55 +00001071/*
Chris Lattner00950542001-06-06 20:29:01 +00001072 | STRINGCONSTANT { // Quoted strings work too... especially for methods
1073 $$ = ValID::create_conststr($1);
1074 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001075*/
Chris Lattner00950542001-06-06 20:29:01 +00001076
1077// ValueRef - A reference to a definition...
1078ValueRef : INTVAL { // Is it an integer reference...?
1079 $$ = ValID::create($1);
1080 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001081 | VAR_ID { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001082 $$ = ValID::create($1);
1083 }
1084 | ConstValueRef {
1085 $$ = $1;
1086 }
1087
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001088// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1089// type immediately preceeds the value reference, and allows complex constant
1090// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
1091ResolvedVal : ExtendedConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001092 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001093 }
1094 | Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001095 $$ = getVal(*$1, $2); delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +00001096 }
Chris Lattner8b81bf52001-07-25 22:47:46 +00001097
Chris Lattner00950542001-06-06 20:29:01 +00001098
1099BasicBlockList : BasicBlockList BasicBlock {
1100 $1->getBasicBlocks().push_back($2);
1101 $$ = $1;
1102 }
1103 | MethodHeader BasicBlock { // Do not allow methods with 0 basic blocks
1104 $$ = $1; // in them...
1105 $1->getBasicBlocks().push_back($2);
1106 }
1107
1108
1109// Basic blocks are terminated by branching instructions:
1110// br, br/cc, switch, ret
1111//
1112BasicBlock : InstructionList BBTerminatorInst {
1113 $1->getInstList().push_back($2);
1114 InsertValue($1);
1115 $$ = $1;
1116 }
1117 | LABELSTR InstructionList BBTerminatorInst {
1118 $2->getInstList().push_back($3);
Chris Lattner30c89792001-09-07 16:35:17 +00001119 setValueName($2, $1);
Chris Lattner00950542001-06-06 20:29:01 +00001120
1121 InsertValue($2);
1122 $$ = $2;
1123 }
1124
1125InstructionList : InstructionList Inst {
1126 $1->getInstList().push_back($2);
1127 $$ = $1;
1128 }
1129 | /* empty */ {
1130 $$ = new BasicBlock();
1131 }
1132
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001133BBTerminatorInst : RET ResolvedVal { // Return with a result...
1134 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001135 }
1136 | RET VOID { // Return with no result...
1137 $$ = new ReturnInst();
1138 }
1139 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner9636a912001-10-01 16:18:37 +00001140 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $3)));
Chris Lattner00950542001-06-06 20:29:01 +00001141 } // Conditional Branch...
1142 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner9636a912001-10-01 16:18:37 +00001143 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $6)),
1144 cast<BasicBlock>(getVal(Type::LabelTy, $9)),
Chris Lattner00950542001-06-06 20:29:01 +00001145 getVal(Type::BoolTy, $3));
1146 }
1147 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1148 SwitchInst *S = new SwitchInst(getVal($2, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001149 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
Chris Lattner00950542001-06-06 20:29:01 +00001150 $$ = S;
1151
1152 list<pair<ConstPoolVal*, BasicBlock*> >::iterator I = $8->begin(),
1153 end = $8->end();
Chris Lattner7fc9fe32001-06-27 23:41:11 +00001154 for (; I != end; ++I)
Chris Lattner00950542001-06-06 20:29:01 +00001155 S->dest_push_back(I->first, I->second);
1156 }
1157
1158JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1159 $$ = $1;
Chris Lattnercfe26c92001-10-01 18:26:53 +00001160 ConstPoolVal *V = cast<ConstPoolVal>(getVal($2, $3, true));
Chris Lattner00950542001-06-06 20:29:01 +00001161 if (V == 0)
1162 ThrowException("May only switch on a constant pool value!");
1163
Chris Lattner9636a912001-10-01 16:18:37 +00001164 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($5, $6))));
Chris Lattner00950542001-06-06 20:29:01 +00001165 }
1166 | IntType ConstValueRef ',' LABEL ValueRef {
1167 $$ = new list<pair<ConstPoolVal*, BasicBlock*> >();
Chris Lattnercfe26c92001-10-01 18:26:53 +00001168 ConstPoolVal *V = cast<ConstPoolVal>(getVal($1, $2, true));
Chris Lattner00950542001-06-06 20:29:01 +00001169
1170 if (V == 0)
1171 ThrowException("May only switch on a constant pool value!");
1172
Chris Lattner9636a912001-10-01 16:18:37 +00001173 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($4, $5))));
Chris Lattner00950542001-06-06 20:29:01 +00001174 }
1175
1176Inst : OptAssign InstVal {
Chris Lattner1781aca2001-09-18 04:00:54 +00001177 setValueName($2, $1); // Is this definition named?? if so, assign the name...
Chris Lattner00950542001-06-06 20:29:01 +00001178
1179 InsertValue($2);
1180 $$ = $2;
1181}
1182
Chris Lattnerc24d2082001-06-11 15:04:20 +00001183PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
1184 $$ = new list<pair<Value*, BasicBlock*> >();
Chris Lattner30c89792001-09-07 16:35:17 +00001185 $$->push_back(make_pair(getVal(*$1, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001186 cast<BasicBlock>(getVal(Type::LabelTy, $5))));
Chris Lattner30c89792001-09-07 16:35:17 +00001187 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001188 }
1189 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1190 $$ = $1;
1191 $1->push_back(make_pair(getVal($1->front().first->getType(), $4),
Chris Lattner9636a912001-10-01 16:18:37 +00001192 cast<BasicBlock>(getVal(Type::LabelTy, $6))));
Chris Lattnerc24d2082001-06-11 15:04:20 +00001193 }
1194
1195
Chris Lattner30c89792001-09-07 16:35:17 +00001196ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner00950542001-06-06 20:29:01 +00001197 $$ = new list<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001198 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001199 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001200 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001201 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001202 $1->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001203 }
1204
1205// ValueRefListE - Just like ValueRefList, except that it may also be empty!
1206ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; }
1207
1208InstVal : BinaryOps Types ValueRef ',' ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001209 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001210 if ($$ == 0)
1211 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001212 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001213 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001214 | UnaryOps ResolvedVal {
1215 $$ = UnaryOperator::create($1, $2);
Chris Lattner00950542001-06-06 20:29:01 +00001216 if ($$ == 0)
1217 ThrowException("unary operator returned null!");
Chris Lattner09083092001-07-08 04:57:15 +00001218 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001219 | ShiftOps ResolvedVal ',' ResolvedVal {
1220 if ($4->getType() != Type::UByteTy)
1221 ThrowException("Shift amount must be ubyte!");
1222 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001223 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001224 | CAST ResolvedVal TO Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001225 $$ = new CastInst($2, *$4);
1226 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001227 }
Chris Lattnerc24d2082001-06-11 15:04:20 +00001228 | PHI PHIList {
1229 const Type *Ty = $2->front().first->getType();
1230 $$ = new PHINode(Ty);
Chris Lattner00950542001-06-06 20:29:01 +00001231 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001232 if ($2->front().first->getType() != Ty)
1233 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00001234 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001235 $2->pop_front();
1236 }
1237 delete $2; // Free the list...
1238 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001239 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001240 const MethodType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001241
Chris Lattner9636a912001-10-01 16:18:37 +00001242 if (!(Ty = dyn_cast<MethodType>($2->get()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001243 // Pull out the types of all of the arguments...
1244 vector<const Type*> ParamTypes;
1245 for (list<Value*>::iterator I = $5->begin(), E = $5->end(); I != E; ++I)
1246 ParamTypes.push_back((*I)->getType());
Chris Lattner30c89792001-09-07 16:35:17 +00001247 Ty = MethodType::get(*$2, ParamTypes);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001248 }
Chris Lattner30c89792001-09-07 16:35:17 +00001249 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001250
Chris Lattner8b81bf52001-07-25 22:47:46 +00001251 Value *V = getVal(Ty, $3); // Get the method we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00001252
Chris Lattner8b81bf52001-07-25 22:47:46 +00001253 // Create the call node...
1254 if (!$5) { // Has no arguments?
Chris Lattner9636a912001-10-01 16:18:37 +00001255 $$ = new CallInst(cast<Method>(V), vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00001256 } else { // Has arguments?
Chris Lattner00950542001-06-06 20:29:01 +00001257 // Loop through MethodType's arguments and ensure they are specified
1258 // correctly!
1259 //
1260 MethodType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001261 MethodType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
1262 list<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
1263
1264 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1265 if ((*ArgI)->getType() != *I)
1266 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner00950542001-06-06 20:29:01 +00001267 (*I)->getName() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00001268
Chris Lattner8b81bf52001-07-25 22:47:46 +00001269 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner00950542001-06-06 20:29:01 +00001270 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00001271
Chris Lattner9636a912001-10-01 16:18:37 +00001272 $$ = new CallInst(cast<Method>(V),
Chris Lattner8b81bf52001-07-25 22:47:46 +00001273 vector<Value*>($5->begin(), $5->end()));
1274 }
1275 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00001276 }
1277 | MemoryInst {
1278 $$ = $1;
1279 }
1280
Chris Lattner027dcc52001-07-08 21:10:27 +00001281// UByteList - List of ubyte values for load and store instructions
1282UByteList : ',' ConstVector {
1283 $$ = $2;
1284} | /* empty */ {
1285 $$ = new vector<ConstPoolVal*>();
1286}
1287
Chris Lattner00950542001-06-06 20:29:01 +00001288MemoryInst : MALLOC Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001289 $$ = new MallocInst(PointerType::get(*$2));
1290 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001291 }
1292 | MALLOC Types ',' UINT ValueRef {
Chris Lattnerb00c5822001-10-02 03:41:24 +00001293 if (!(*$2)->isArrayType() || cast<const ArrayType>($2->get())->isSized())
Chris Lattner30c89792001-09-07 16:35:17 +00001294 ThrowException("Trying to allocate " + (*$2)->getName() +
Chris Lattner00950542001-06-06 20:29:01 +00001295 " as unsized array!");
Chris Lattner30c89792001-09-07 16:35:17 +00001296 const Type *Ty = PointerType::get(*$2);
Chris Lattner8896eda2001-07-09 19:38:36 +00001297 $$ = new MallocInst(Ty, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001298 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001299 }
1300 | ALLOCA Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001301 $$ = new AllocaInst(PointerType::get(*$2));
1302 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001303 }
1304 | ALLOCA Types ',' UINT ValueRef {
Chris Lattnerb00c5822001-10-02 03:41:24 +00001305 if (!(*$2)->isArrayType() || cast<const ArrayType>($2->get())->isSized())
Chris Lattner30c89792001-09-07 16:35:17 +00001306 ThrowException("Trying to allocate " + (*$2)->getName() +
Chris Lattner00950542001-06-06 20:29:01 +00001307 " as unsized array!");
Chris Lattner30c89792001-09-07 16:35:17 +00001308 const Type *Ty = PointerType::get(*$2);
Chris Lattner00950542001-06-06 20:29:01 +00001309 Value *ArrSize = getVal($4, $5);
Chris Lattnerf0d0e9c2001-07-07 08:36:30 +00001310 $$ = new AllocaInst(Ty, ArrSize);
Chris Lattner30c89792001-09-07 16:35:17 +00001311 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001312 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001313 | FREE ResolvedVal {
1314 if (!$2->getType()->isPointerType())
1315 ThrowException("Trying to free nonpointer type " +
1316 $2->getType()->getName() + "!");
1317 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001318 }
1319
Chris Lattner027dcc52001-07-08 21:10:27 +00001320 | LOAD Types ValueRef UByteList {
Chris Lattner30c89792001-09-07 16:35:17 +00001321 if (!(*$2)->isPointerType())
1322 ThrowException("Can't load from nonpointer type: " + (*$2)->getName());
1323 if (LoadInst::getIndexedType(*$2, *$4) == 0)
Chris Lattner027dcc52001-07-08 21:10:27 +00001324 ThrowException("Invalid indices for load instruction!");
1325
Chris Lattner30c89792001-09-07 16:35:17 +00001326 $$ = new LoadInst(getVal(*$2, $3), *$4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001327 delete $4; // Free the vector...
Chris Lattner30c89792001-09-07 16:35:17 +00001328 delete $2;
Chris Lattner027dcc52001-07-08 21:10:27 +00001329 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001330 | STORE ResolvedVal ',' Types ValueRef UByteList {
Chris Lattner30c89792001-09-07 16:35:17 +00001331 if (!(*$4)->isPointerType())
1332 ThrowException("Can't store to a nonpointer type: " + (*$4)->getName());
1333 const Type *ElTy = StoreInst::getIndexedType(*$4, *$6);
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001334 if (ElTy == 0)
1335 ThrowException("Can't store into that field list!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001336 if (ElTy != $2->getType())
1337 ThrowException("Can't store '" + $2->getType()->getName() +
1338 "' into space of type '" + ElTy->getName() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001339 $$ = new StoreInst($2, getVal(*$4, $5), *$6);
1340 delete $4; delete $6;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001341 }
1342 | GETELEMENTPTR Types ValueRef UByteList {
Chris Lattner30c89792001-09-07 16:35:17 +00001343 if (!(*$2)->isPointerType())
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001344 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner30c89792001-09-07 16:35:17 +00001345 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
1346 ThrowException("Can't get element ptr '" + (*$2)->getName() + "'!");
1347 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
1348 delete $2; delete $4;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001349 }
Chris Lattner027dcc52001-07-08 21:10:27 +00001350
Chris Lattner00950542001-06-06 20:29:01 +00001351%%
Chris Lattner09083092001-07-08 04:57:15 +00001352int yyerror(const char *ErrorMsg) {
Chris Lattner00950542001-06-06 20:29:01 +00001353 ThrowException(string("Parse error: ") + ErrorMsg);
1354 return 0;
1355}