blob: 1ee6d50b567aa3c1d3bf0f45f1188effb0b7de69 [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
Chris Lattnerb7474512001-10-03 15:39:04 +0000105static bool inMethodScope() { return CurMeth.CurrentMethod != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000106
Chris Lattner00950542001-06-06 20:29:01 +0000107
108//===----------------------------------------------------------------------===//
109// Code to handle definitions of all the types
110//===----------------------------------------------------------------------===//
111
Chris Lattner93750fa2001-07-28 17:48:55 +0000112static void InsertValue(Value *D, vector<ValueList> &ValueTab = CurMeth.Values){
Chris Lattner00950542001-06-06 20:29:01 +0000113 if (!D->hasName()) { // Is this a numbered definition?
114 unsigned type = D->getType()->getUniqueID();
115 if (ValueTab.size() <= type)
116 ValueTab.resize(type+1, ValueList());
117 //printf("Values[%d][%d] = %d\n", type, ValueTab[type].size(), D);
118 ValueTab[type].push_back(D);
119 }
120}
121
Chris Lattner30c89792001-09-07 16:35:17 +0000122// TODO: FIXME when Type are not const
123static void InsertType(const Type *Ty, vector<PATypeHolder<Type> > &Types) {
124 Types.push_back(Ty);
125}
126
127static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000128 switch (D.Type) {
129 case 0: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000130 unsigned Num = (unsigned)D.Num;
131
132 // Module constants occupy the lowest numbered slots...
133 if (Num < CurModule.Types.size())
134 return CurModule.Types[Num];
135
136 Num -= CurModule.Types.size();
137
138 // Check that the number is within bounds...
139 if (Num <= CurMeth.Types.size())
140 return CurMeth.Types[Num];
141 }
142 case 1: { // Is it a named definition?
143 string Name(D.Name);
144 SymbolTable *SymTab = 0;
Chris Lattnerb7474512001-10-03 15:39:04 +0000145 if (inMethodScope()) SymTab = CurMeth.CurrentMethod->getSymbolTable();
Chris Lattner30c89792001-09-07 16:35:17 +0000146 Value *N = SymTab ? SymTab->lookup(Type::TypeTy, Name) : 0;
147
148 if (N == 0) {
149 // Symbol table doesn't automatically chain yet... because the method
150 // hasn't been added to the module...
151 //
152 SymTab = CurModule.CurrentModule->getSymbolTable();
153 if (SymTab)
154 N = SymTab->lookup(Type::TypeTy, Name);
155 if (N == 0) break;
156 }
157
158 D.destroy(); // Free old strdup'd memory...
Chris Lattnercfe26c92001-10-01 18:26:53 +0000159 return cast<const Type>(N);
Chris Lattner30c89792001-09-07 16:35:17 +0000160 }
161 default:
162 ThrowException("Invalid symbol type reference!");
163 }
164
165 // If we reached here, we referenced either a symbol that we don't know about
166 // or an id number that hasn't been read yet. We may be referencing something
167 // forward, so just create an entry to be resolved later and get to it...
168 //
169 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
170
Chris Lattnerb7474512001-10-03 15:39:04 +0000171 vector<PATypeHolder<Type> > *LateResolver = inMethodScope() ?
Chris Lattner30c89792001-09-07 16:35:17 +0000172 &CurMeth.LateResolveTypes : &CurModule.LateResolveTypes;
173
174 Type *Typ = new TypePlaceHolder(Type::TypeTy, D);
175 InsertType(Typ, *LateResolver);
176 return Typ;
177}
178
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000179static Value *lookupInSymbolTable(const Type *Ty, const string &Name) {
180 SymbolTable *SymTab =
Chris Lattnerb7474512001-10-03 15:39:04 +0000181 inMethodScope() ? CurMeth.CurrentMethod->getSymbolTable() : 0;
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000182 Value *N = SymTab ? SymTab->lookup(Ty, Name) : 0;
183
184 if (N == 0) {
185 // Symbol table doesn't automatically chain yet... because the method
186 // hasn't been added to the module...
187 //
188 SymTab = CurModule.CurrentModule->getSymbolTable();
189 if (SymTab)
190 N = SymTab->lookup(Ty, Name);
191 }
192
193 return N;
194}
195
Chris Lattner30c89792001-09-07 16:35:17 +0000196static Value *getVal(const Type *Ty, const ValID &D,
197 bool DoNotImprovise = false) {
198 assert(Ty != Type::TypeTy && "Should use getTypeVal for types!");
199
200 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000201 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000202 unsigned type = Ty->getUniqueID();
Chris Lattner00950542001-06-06 20:29:01 +0000203 unsigned Num = (unsigned)D.Num;
204
205 // Module constants occupy the lowest numbered slots...
206 if (type < CurModule.Values.size()) {
207 if (Num < CurModule.Values[type].size())
208 return CurModule.Values[type][Num];
209
210 Num -= CurModule.Values[type].size();
211 }
212
213 // Make sure that our type is within bounds
214 if (CurMeth.Values.size() <= type)
215 break;
216
217 // Check that the number is within bounds...
218 if (CurMeth.Values[type].size() <= Num)
219 break;
220
221 return CurMeth.Values[type][Num];
222 }
Chris Lattner1a1cb112001-09-30 22:46:54 +0000223 case ValID::NameVal: { // Is it a named definition?
Chris Lattner00950542001-06-06 20:29:01 +0000224 string Name(D.Name);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000225 Value *N = lookupInSymbolTable(Ty, Name);
226 if (N == 0) break;
Chris Lattner00950542001-06-06 20:29:01 +0000227
228 D.destroy(); // Free old strdup'd memory...
229 return N;
230 }
231
Chris Lattner1a1cb112001-09-30 22:46:54 +0000232 case ValID::ConstSIntVal: // Is it a constant pool reference??
233 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
234 case ValID::ConstStringVal: // Is it a string const pool reference?
235 case ValID::ConstFPVal: // Is it a floating point const pool reference?
236 case ValID::ConstNullVal: { // Is it a null value?
Chris Lattner00950542001-06-06 20:29:01 +0000237 ConstPoolVal *CPV = 0;
238
Chris Lattner30c89792001-09-07 16:35:17 +0000239 // Check to make sure that "Ty" is an integral type, and that our
Chris Lattner00950542001-06-06 20:29:01 +0000240 // value will fit into the specified type...
241 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000242 case ValID::ConstSIntVal:
Chris Lattner30c89792001-09-07 16:35:17 +0000243 if (Ty == Type::BoolTy) { // Special handling for boolean data
244 CPV = ConstPoolBool::get(D.ConstPool64 != 0);
Chris Lattner00950542001-06-06 20:29:01 +0000245 } else {
Chris Lattner30c89792001-09-07 16:35:17 +0000246 if (!ConstPoolSInt::isValueValidForType(Ty, D.ConstPool64))
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000247 ThrowException("Symbolic constant pool value '" +
248 itostr(D.ConstPool64) + "' is invalid for type '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000249 Ty->getName() + "'!");
250 CPV = ConstPoolSInt::get(Ty, D.ConstPool64);
Chris Lattner00950542001-06-06 20:29:01 +0000251 }
252 break;
Chris Lattner1a1cb112001-09-30 22:46:54 +0000253 case ValID::ConstUIntVal:
Chris Lattner30c89792001-09-07 16:35:17 +0000254 if (!ConstPoolUInt::isValueValidForType(Ty, D.UConstPool64)) {
255 if (!ConstPoolSInt::isValueValidForType(Ty, D.ConstPool64)) {
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000256 ThrowException("Integral constant pool reference is invalid!");
Chris Lattner00950542001-06-06 20:29:01 +0000257 } else { // This is really a signed reference. Transmogrify.
Chris Lattner30c89792001-09-07 16:35:17 +0000258 CPV = ConstPoolSInt::get(Ty, D.ConstPool64);
Chris Lattner00950542001-06-06 20:29:01 +0000259 }
260 } else {
Chris Lattner30c89792001-09-07 16:35:17 +0000261 CPV = ConstPoolUInt::get(Ty, D.UConstPool64);
Chris Lattner00950542001-06-06 20:29:01 +0000262 }
263 break;
Chris Lattner1a1cb112001-09-30 22:46:54 +0000264 case ValID::ConstStringVal:
Chris Lattner00950542001-06-06 20:29:01 +0000265 cerr << "FIXME: TODO: String constants [sbyte] not implemented yet!\n";
266 abort();
Chris Lattner00950542001-06-06 20:29:01 +0000267 break;
Chris Lattner1a1cb112001-09-30 22:46:54 +0000268 case ValID::ConstFPVal:
Chris Lattner30c89792001-09-07 16:35:17 +0000269 if (!ConstPoolFP::isValueValidForType(Ty, D.ConstPoolFP))
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000270 ThrowException("FP constant invalid for type!!");
Chris Lattner1a1cb112001-09-30 22:46:54 +0000271 CPV = ConstPoolFP::get(Ty, D.ConstPoolFP);
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000272 break;
Chris Lattner1a1cb112001-09-30 22:46:54 +0000273 case ValID::ConstNullVal:
274 if (!Ty->isPointerType())
275 ThrowException("Cannot create a a non pointer null!");
Chris Lattnerb7474512001-10-03 15:39:04 +0000276 CPV = ConstPoolPointer::getNull(cast<PointerType>(Ty));
Chris Lattner1a1cb112001-09-30 22:46:54 +0000277 break;
278 default:
279 assert(0 && "Unhandled case!");
Chris Lattner00950542001-06-06 20:29:01 +0000280 }
281 assert(CPV && "How did we escape creating a constant??");
Chris Lattner00950542001-06-06 20:29:01 +0000282 return CPV;
283 } // End of case 2,3,4
Chris Lattner30c89792001-09-07 16:35:17 +0000284 default:
285 assert(0 && "Unhandled case!");
Chris Lattner00950542001-06-06 20:29:01 +0000286 } // End of switch
287
288
289 // If we reached here, we referenced either a symbol that we don't know about
290 // or an id number that hasn't been read yet. We may be referencing something
291 // forward, so just create an entry to be resolved later and get to it...
292 //
293 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
294
Chris Lattner00950542001-06-06 20:29:01 +0000295 Value *d = 0;
Chris Lattnerb7474512001-10-03 15:39:04 +0000296 vector<ValueList> *LateResolver = inMethodScope() ?
Chris Lattner30c89792001-09-07 16:35:17 +0000297 &CurMeth.LateResolveValues : &CurModule.LateResolveValues;
Chris Lattner93750fa2001-07-28 17:48:55 +0000298
Chris Lattnerb973dd72001-10-03 14:59:05 +0000299 if (isa<MethodType>(Ty))
300 ThrowException("Methods are not values and must be referenced as pointers");
301
Chris Lattneref9c23f2001-10-03 14:53:21 +0000302 if (const PointerType *PTy = dyn_cast<PointerType>(Ty))
303 if (const MethodType *MTy = dyn_cast<MethodType>(PTy->getValueType()))
304 Ty = MTy; // Convert pointer to method to method type
305
Chris Lattner30c89792001-09-07 16:35:17 +0000306 switch (Ty->getPrimitiveID()) {
307 case Type::LabelTyID: d = new BBPlaceHolder(Ty, D); break;
308 case Type::MethodTyID: d = new MethPlaceHolder(Ty, D);
Chris Lattner93750fa2001-07-28 17:48:55 +0000309 LateResolver = &CurModule.LateResolveValues; break;
Chris Lattner30c89792001-09-07 16:35:17 +0000310 default: d = new ValuePlaceHolder(Ty, D); break;
Chris Lattner00950542001-06-06 20:29:01 +0000311 }
312
313 assert(d != 0 && "How did we not make something?");
Chris Lattner93750fa2001-07-28 17:48:55 +0000314 InsertValue(d, *LateResolver);
Chris Lattner00950542001-06-06 20:29:01 +0000315 return d;
316}
317
318
319//===----------------------------------------------------------------------===//
320// Code to handle forward references in instructions
321//===----------------------------------------------------------------------===//
322//
323// This code handles the late binding needed with statements that reference
324// values not defined yet... for example, a forward branch, or the PHI node for
325// a loop body.
326//
327// This keeps a table (CurMeth.LateResolveValues) of all such forward references
328// and back patchs after we are done.
329//
330
331// ResolveDefinitions - If we could not resolve some defs at parsing
332// time (forward branches, phi functions for loops, etc...) resolve the
333// defs now...
334//
335static void ResolveDefinitions(vector<ValueList> &LateResolvers) {
336 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
337 for (unsigned ty = 0; ty < LateResolvers.size(); ty++) {
338 while (!LateResolvers[ty].empty()) {
339 Value *V = LateResolvers[ty].back();
340 LateResolvers[ty].pop_back();
341 ValID &DID = getValIDFromPlaceHolder(V);
342
343 Value *TheRealValue = getVal(Type::getUniqueIDType(ty), DID, true);
344
Chris Lattner30c89792001-09-07 16:35:17 +0000345 if (TheRealValue == 0) {
346 if (DID.Type == 1)
347 ThrowException("Reference to an invalid definition: '" +DID.getName()+
348 "' of type '" + V->getType()->getDescription() + "'",
349 getLineNumFromPlaceHolder(V));
350 else
351 ThrowException("Reference to an invalid definition: #" +
352 itostr(DID.Num) + " of type '" +
353 V->getType()->getDescription() + "'",
354 getLineNumFromPlaceHolder(V));
355 }
356
Chris Lattnercfe26c92001-10-01 18:26:53 +0000357 assert(!isa<Type>(V) && "Types should be in LateResolveTypes!");
Chris Lattner00950542001-06-06 20:29:01 +0000358
359 V->replaceAllUsesWith(TheRealValue);
Chris Lattner00950542001-06-06 20:29:01 +0000360 delete V;
361 }
362 }
363
364 LateResolvers.clear();
365}
366
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000367// ResolveType - Take a specified unresolved type and resolve it. If there is
368// nothing to resolve it to yet, return true. Otherwise resolve it and return
369// false.
370//
371static bool ResolveType(PATypeHolder<Type> &T) {
372 const Type *Ty = T;
373 ValID &DID = getValIDFromPlaceHolder(Ty);
374
375 const Type *TheRealType = getTypeVal(DID, true);
376 if (TheRealType == 0) return true;
377
378 // Refine the opaque type we had to the new type we are getting.
379 cast<DerivedType>(Ty)->refineAbstractTypeTo(TheRealType);
380 return false;
381}
382
Chris Lattner30c89792001-09-07 16:35:17 +0000383
384// ResolveTypes - This goes through the forward referenced type table and makes
385// sure that all type references are complete. This code is executed after the
386// constant pool of a method or module is completely parsed.
Chris Lattner00950542001-06-06 20:29:01 +0000387//
Chris Lattner30c89792001-09-07 16:35:17 +0000388static void ResolveTypes(vector<PATypeHolder<Type> > &LateResolveTypes) {
389 while (!LateResolveTypes.empty()) {
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000390 if (ResolveType(LateResolveTypes.back())) {
391 const Type *Ty = LateResolveTypes.back();
392 ValID &DID = getValIDFromPlaceHolder(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000393
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000394 if (DID.Type == ValID::NameVal)
Chris Lattner30c89792001-09-07 16:35:17 +0000395 ThrowException("Reference to an invalid type: '" +DID.getName(),
396 getLineNumFromPlaceHolder(Ty));
397 else
398 ThrowException("Reference to an invalid type: #" + itostr(DID.Num),
399 getLineNumFromPlaceHolder(Ty));
Chris Lattner00950542001-06-06 20:29:01 +0000400 }
Chris Lattner30c89792001-09-07 16:35:17 +0000401
Chris Lattner30c89792001-09-07 16:35:17 +0000402 // No need to delete type, refine does that for us.
403 LateResolveTypes.pop_back();
404 }
405}
406
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000407
408// ResolveSomeTypes - This goes through the forward referenced type table and
409// completes references that are now done. This is so that types are
410// immediately resolved to be as concrete as possible. This does not cause
411// thrown exceptions if not everything is resolved.
412//
413static void ResolveSomeTypes(vector<PATypeHolder<Type> > &LateResolveTypes) {
414 for (unsigned i = 0; i < LateResolveTypes.size(); ) {
415 if (ResolveType(LateResolveTypes[i]))
416 ++i; // Type didn't resolve
417 else
418 LateResolveTypes.erase(LateResolveTypes.begin()+i); // Type resolved!
419 }
420}
421
422
Chris Lattner1781aca2001-09-18 04:00:54 +0000423// setValueName - Set the specified value to the name given. The name may be
424// null potentially, in which case this is a noop. The string passed in is
425// assumed to be a malloc'd string buffer, and is freed by this function.
426//
Chris Lattnerb7474512001-10-03 15:39:04 +0000427// This function returns true if the value has already been defined, but is
428// allowed to be redefined in the specified context. If the name is a new name
429// for the typeplane, false is returned.
430//
431static bool setValueName(Value *V, char *NameStr) {
432 if (NameStr == 0) return false;
Chris Lattner1781aca2001-09-18 04:00:54 +0000433 string Name(NameStr); // Copy string
434 free(NameStr); // Free old string
435
Chris Lattnerb7474512001-10-03 15:39:04 +0000436 SymbolTable *ST = inMethodScope() ?
Chris Lattner30c89792001-09-07 16:35:17 +0000437 CurMeth.CurrentMethod->getSymbolTableSure() :
438 CurModule.CurrentModule->getSymbolTableSure();
439
440 Value *Existing = ST->lookup(V->getType(), Name);
441 if (Existing) { // Inserting a name that is already defined???
442 // There is only one case where this is allowed: when we are refining an
443 // opaque type. In this case, Existing will be an opaque type.
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000444 if (const Type *Ty = dyn_cast<const Type>(Existing)) {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000445 if (OpaqueType *OpTy = dyn_cast<OpaqueType>(Ty)) {
Chris Lattner30c89792001-09-07 16:35:17 +0000446 // We ARE replacing an opaque type!
Chris Lattnerb00c5822001-10-02 03:41:24 +0000447 OpTy->refineAbstractTypeTo(cast<Type>(V));
Chris Lattnerb7474512001-10-03 15:39:04 +0000448 return true;
Chris Lattner30c89792001-09-07 16:35:17 +0000449 }
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000450 }
Chris Lattner30c89792001-09-07 16:35:17 +0000451
Chris Lattner9636a912001-10-01 16:18:37 +0000452 // Otherwise, we are a simple redefinition of a value, check to see if it
453 // is defined the same as the old one...
454 if (const Type *Ty = dyn_cast<const Type>(Existing)) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000455 if (Ty == cast<const Type>(V)) return true; // Yes, it's equal.
456 // cerr << "Type: " << Ty->getDescription() << " != "
457 // << cast<const Type>(V)->getDescription() << "!\n";
458 } else if (GlobalVariable *EGV = dyn_cast<GlobalVariable>(Existing)) {
Chris Lattner43efcbf2001-10-03 19:35:57 +0000459 // We are allowed to redefine a global variable in two circumstances:
460 // 1. If at least one of the globals is uninitialized or
461 // 2. If both initializers have the same value.
462 //
463 // This can only be done if the const'ness of the vars is the same.
464 //
Chris Lattner89219832001-10-03 19:35:04 +0000465 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
466 if (EGV->isConstant() == GV->isConstant() &&
467 (!EGV->hasInitializer() || !GV->hasInitializer() ||
468 EGV->getInitializer() == GV->getInitializer())) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000469
Chris Lattner89219832001-10-03 19:35:04 +0000470 // Make sure the existing global version gets the initializer!
471 if (GV->hasInitializer() && !EGV->hasInitializer())
472 EGV->setInitializer(GV->getInitializer());
473
474 return true; // They are equivalent!
475 }
Chris Lattnerb7474512001-10-03 15:39:04 +0000476 }
Chris Lattner9636a912001-10-01 16:18:37 +0000477 }
Chris Lattner30c89792001-09-07 16:35:17 +0000478 ThrowException("Redefinition of value name '" + Name + "' in the '" +
479 V->getType()->getDescription() + "' type plane!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000480 }
Chris Lattner00950542001-06-06 20:29:01 +0000481
Chris Lattner30c89792001-09-07 16:35:17 +0000482 V->setName(Name, ST);
Chris Lattnerb7474512001-10-03 15:39:04 +0000483 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000484}
485
Chris Lattner8896eda2001-07-09 19:38:36 +0000486
Chris Lattner30c89792001-09-07 16:35:17 +0000487//===----------------------------------------------------------------------===//
488// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000489//
Chris Lattner8896eda2001-07-09 19:38:36 +0000490
Chris Lattner30c89792001-09-07 16:35:17 +0000491// TypeContains - Returns true if Ty contains E in it.
492//
493static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattner3ff43872001-09-28 22:56:31 +0000494 return find(df_begin(Ty), df_end(Ty), E) != df_end(Ty);
Chris Lattner30c89792001-09-07 16:35:17 +0000495}
Chris Lattner698b56e2001-07-20 19:15:08 +0000496
Chris Lattner30c89792001-09-07 16:35:17 +0000497
498static vector<pair<unsigned, OpaqueType *> > UpRefs;
499
500static PATypeHolder<Type> HandleUpRefs(const Type *ty) {
501 PATypeHolder<Type> Ty(ty);
502 UR_OUT(UpRefs.size() << " upreferences active!\n");
503 for (unsigned i = 0; i < UpRefs.size(); ) {
504 UR_OUT("TypeContains(" << Ty->getDescription() << ", "
505 << UpRefs[i].second->getDescription() << ") = "
506 << TypeContains(Ty, UpRefs[i].second) << endl);
507 if (TypeContains(Ty, UpRefs[i].second)) {
508 unsigned Level = --UpRefs[i].first; // Decrement level of upreference
509 UR_OUT("Uplevel Ref Level = " << Level << endl);
510 if (Level == 0) { // Upreference should be resolved!
511 UR_OUT("About to resolve upreference!\n";
512 string OldName = UpRefs[i].second->getDescription());
513 UpRefs[i].second->refineAbstractTypeTo(Ty);
514 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
515 UR_OUT("Type '" << OldName << "' refined upreference to: "
516 << (const void*)Ty << ", " << Ty->getDescription() << endl);
517 continue;
518 }
519 }
520
521 ++i; // Otherwise, no resolve, move on...
Chris Lattner8896eda2001-07-09 19:38:36 +0000522 }
Chris Lattner30c89792001-09-07 16:35:17 +0000523 // FIXME: TODO: this should return the updated type
Chris Lattner8896eda2001-07-09 19:38:36 +0000524 return Ty;
525}
526
Chris Lattner30c89792001-09-07 16:35:17 +0000527template <class TypeTy>
528inline static void TypeDone(PATypeHolder<TypeTy> *Ty) {
529 if (UpRefs.size())
530 ThrowException("Invalid upreference in type: " + (*Ty)->getDescription());
531}
532
533// newTH - Allocate a new type holder for the specified type
534template <class TypeTy>
535inline static PATypeHolder<TypeTy> *newTH(const TypeTy *Ty) {
536 return new PATypeHolder<TypeTy>(Ty);
537}
538template <class TypeTy>
539inline static PATypeHolder<TypeTy> *newTH(const PATypeHolder<TypeTy> &TH) {
540 return new PATypeHolder<TypeTy>(TH);
541}
542
543
Chris Lattner00950542001-06-06 20:29:01 +0000544//===----------------------------------------------------------------------===//
545// RunVMAsmParser - Define an interface to this parser
546//===----------------------------------------------------------------------===//
547//
Chris Lattnera2850432001-07-22 18:36:00 +0000548Module *RunVMAsmParser(const string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000549 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000550 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000551 llvmAsmlineno = 1; // Reset the current line number...
552
553 CurModule.CurrentModule = new Module(); // Allocate a new module to read
554 yyparse(); // Parse the file.
555 Module *Result = ParserResult;
Chris Lattner00950542001-06-06 20:29:01 +0000556 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
557 ParserResult = 0;
558
559 return Result;
560}
561
562%}
563
564%union {
Chris Lattner30c89792001-09-07 16:35:17 +0000565 Module *ModuleVal;
566 Method *MethodVal;
567 MethodArgument *MethArgVal;
568 BasicBlock *BasicBlockVal;
569 TerminatorInst *TermInstVal;
570 Instruction *InstVal;
571 ConstPoolVal *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000572
Chris Lattner30c89792001-09-07 16:35:17 +0000573 const Type *PrimType;
574 PATypeHolder<Type> *TypeVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000575 Value *ValueVal;
576
577 list<MethodArgument*> *MethodArgList;
578 list<Value*> *ValueList;
579 list<PATypeHolder<Type> > *TypeList;
Chris Lattnerc24d2082001-06-11 15:04:20 +0000580 list<pair<Value*, BasicBlock*> > *PHIList; // Represent the RHS of PHI node
Chris Lattner00950542001-06-06 20:29:01 +0000581 list<pair<ConstPoolVal*, BasicBlock*> > *JumpTable;
Chris Lattner30c89792001-09-07 16:35:17 +0000582 vector<ConstPoolVal*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000583
Chris Lattner30c89792001-09-07 16:35:17 +0000584 int64_t SInt64Val;
585 uint64_t UInt64Val;
586 int SIntVal;
587 unsigned UIntVal;
588 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000589 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000590
Chris Lattner30c89792001-09-07 16:35:17 +0000591 char *StrVal; // This memory is strdup'd!
592 ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000593
Chris Lattner30c89792001-09-07 16:35:17 +0000594 Instruction::UnaryOps UnaryOpVal;
595 Instruction::BinaryOps BinaryOpVal;
596 Instruction::TermOps TermOpVal;
597 Instruction::MemoryOps MemOpVal;
598 Instruction::OtherOps OtherOpVal;
Chris Lattner00950542001-06-06 20:29:01 +0000599}
600
601%type <ModuleVal> Module MethodList
Chris Lattnere1815642001-07-15 06:35:53 +0000602%type <MethodVal> Method MethodProto MethodHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000603%type <BasicBlockVal> BasicBlock InstructionList
604%type <TermInstVal> BBTerminatorInst
605%type <InstVal> Inst InstVal MemoryInst
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000606%type <ConstVal> ConstVal
Chris Lattner027dcc52001-07-08 21:10:27 +0000607%type <ConstVector> ConstVector UByteList
Chris Lattner00950542001-06-06 20:29:01 +0000608%type <MethodArgList> ArgList ArgListH
609%type <MethArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000610%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000611%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner30c89792001-09-07 16:35:17 +0000612%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000613%type <JumpTable> JumpTable
Chris Lattner1781aca2001-09-18 04:00:54 +0000614%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Chris Lattner00950542001-06-06 20:29:01 +0000615
616%type <ValIDVal> ValueRef ConstValueRef // Reference to a definition or BB
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000617%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000618// Tokens and types for handling constant integer values
619//
620// ESINT64VAL - A negative number within long long range
621%token <SInt64Val> ESINT64VAL
622
623// EUINT64VAL - A positive number within uns. long long range
624%token <UInt64Val> EUINT64VAL
625%type <SInt64Val> EINT64VAL
626
627%token <SIntVal> SINTVAL // Signed 32 bit ints...
628%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
629%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000630%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000631
632// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000633%type <TypeVal> Types TypesV UpRTypes UpRTypesV
634%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
635%token <TypeVal> OPAQUE
636%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
637%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000638
639%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
640%type <StrVal> OptVAR_ID OptAssign
641
642
Chris Lattner1781aca2001-09-18 04:00:54 +0000643%token IMPLEMENTATION TRUE FALSE BEGINTOK END DECLARE GLOBAL CONSTANT UNINIT
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000644%token TO DOTDOTDOT STRING NULL_TOK CONST
Chris Lattner00950542001-06-06 20:29:01 +0000645
646// Basic Block Terminating Operators
647%token <TermOpVal> RET BR SWITCH
648
649// Unary Operators
650%type <UnaryOpVal> UnaryOps // all the unary operators
Chris Lattner71496b32001-07-08 19:03:27 +0000651%token <UnaryOpVal> NOT
Chris Lattner00950542001-06-06 20:29:01 +0000652
653// Binary Operators
654%type <BinaryOpVal> BinaryOps // all the binary operators
655%token <BinaryOpVal> ADD SUB MUL DIV REM
Chris Lattner027dcc52001-07-08 21:10:27 +0000656%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000657
658// Memory Instructions
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000659%token <MemoryOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000660
Chris Lattner027dcc52001-07-08 21:10:27 +0000661// Other Operators
662%type <OtherOpVal> ShiftOps
663%token <OtherOpVal> PHI CALL CAST SHL SHR
664
Chris Lattner00950542001-06-06 20:29:01 +0000665%start Module
666%%
667
668// Handle constant integer size restriction and conversion...
669//
670
671INTVAL : SINTVAL
672INTVAL : UINTVAL {
673 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
674 ThrowException("Value too large for type!");
675 $$ = (int32_t)$1;
676}
677
678
679EINT64VAL : ESINT64VAL // These have same type and can't cause problems...
680EINT64VAL : EUINT64VAL {
681 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
682 ThrowException("Value too large for type!");
683 $$ = (int64_t)$1;
684}
685
Chris Lattner00950542001-06-06 20:29:01 +0000686// Operations that are notably excluded from this list include:
687// RET, BR, & SWITCH because they end basic blocks and are treated specially.
688//
Chris Lattner09083092001-07-08 04:57:15 +0000689UnaryOps : NOT
Chris Lattner00950542001-06-06 20:29:01 +0000690BinaryOps : ADD | SUB | MUL | DIV | REM
691BinaryOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE
Chris Lattner027dcc52001-07-08 21:10:27 +0000692ShiftOps : SHL | SHR
Chris Lattner00950542001-06-06 20:29:01 +0000693
Chris Lattnere98dda62001-07-14 06:10:16 +0000694// These are some types that allow classification if we only want a particular
695// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner00950542001-06-06 20:29:01 +0000696SIntType : LONG | INT | SHORT | SBYTE
697UIntType : ULONG | UINT | USHORT | UBYTE
Chris Lattner30c89792001-09-07 16:35:17 +0000698IntType : SIntType | UIntType
699FPType : FLOAT | DOUBLE
Chris Lattner00950542001-06-06 20:29:01 +0000700
Chris Lattnere98dda62001-07-14 06:10:16 +0000701// OptAssign - Value producing statements have an optional assignment component
Chris Lattner00950542001-06-06 20:29:01 +0000702OptAssign : VAR_ID '=' {
703 $$ = $1;
704 }
705 | /*empty*/ {
706 $$ = 0;
707 }
708
Chris Lattner30c89792001-09-07 16:35:17 +0000709
710//===----------------------------------------------------------------------===//
711// Types includes all predefined types... except void, because it can only be
712// used in specific contexts (method returning void for example). To have
713// access to it, a user must explicitly use TypesV.
714//
715
716// TypesV includes all of 'Types', but it also includes the void type.
717TypesV : Types | VOID { $$ = newTH($1); }
718UpRTypesV : UpRTypes | VOID { $$ = newTH($1); }
719
720Types : UpRTypes {
721 TypeDone($$ = $1);
722 }
723
724
725// Derived types are added later...
726//
727PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT
728PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL
729UpRTypes : OPAQUE | PrimType { $$ = newTH($1); }
730UpRTypes : ValueRef { // Named types are also simple types...
731 $$ = newTH(getTypeVal($1));
732}
733
Chris Lattner30c89792001-09-07 16:35:17 +0000734// Include derived types in the Types production.
735//
736UpRTypes : '\\' EUINT64VAL { // Type UpReference
737 if ($2 > (uint64_t)INT64_MAX) ThrowException("Value out of range!");
738 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
739 UpRefs.push_back(make_pair((unsigned)$2, OT)); // Add to vector...
740 $$ = newTH<Type>(OT);
741 UR_OUT("New Upreference!\n");
742 }
743 | UpRTypesV '(' ArgTypeListI ')' { // Method derived type?
744 vector<const Type*> Params;
745 mapto($3->begin(), $3->end(), back_inserter(Params),
746 mem_fun_ref(&PATypeHandle<Type>::get));
747 $$ = newTH(HandleUpRefs(MethodType::get(*$1, Params)));
748 delete $3; // Delete the argument list
749 delete $1; // Delete the old type handle
750 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000751 | '[' UpRTypesV ']' { // Unsized array type?
752 $$ = newTH<Type>(HandleUpRefs(ArrayType::get(*$2)));
753 delete $2;
Chris Lattner30c89792001-09-07 16:35:17 +0000754 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000755 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
756 $$ = newTH<Type>(HandleUpRefs(ArrayType::get(*$4, (int)$2)));
757 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +0000758 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000759 | '{' TypeListI '}' { // Structure type?
760 vector<const Type*> Elements;
761 mapto($2->begin(), $2->end(), back_inserter(Elements),
762 mem_fun_ref(&PATypeHandle<Type>::get));
Chris Lattner30c89792001-09-07 16:35:17 +0000763
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000764 $$ = newTH<Type>(HandleUpRefs(StructType::get(Elements)));
765 delete $2;
766 }
767 | '{' '}' { // Empty structure type?
768 $$ = newTH<Type>(StructType::get(vector<const Type*>()));
769 }
770 | UpRTypes '*' { // Pointer type?
771 $$ = newTH<Type>(HandleUpRefs(PointerType::get(*$1)));
772 delete $1;
773 }
Chris Lattner30c89792001-09-07 16:35:17 +0000774
775// TypeList - Used for struct declarations and as a basis for method type
776// declaration type lists
777//
778TypeListI : UpRTypes {
779 $$ = new list<PATypeHolder<Type> >();
780 $$->push_back(*$1); delete $1;
781 }
782 | TypeListI ',' UpRTypes {
783 ($$=$1)->push_back(*$3); delete $3;
784 }
785
786// ArgTypeList - List of types for a method type declaration...
787ArgTypeListI : TypeListI
788 | TypeListI ',' DOTDOTDOT {
789 ($$=$1)->push_back(Type::VoidTy);
790 }
791 | DOTDOTDOT {
792 ($$ = new list<PATypeHolder<Type> >())->push_back(Type::VoidTy);
793 }
794 | /*empty*/ {
795 $$ = new list<PATypeHolder<Type> >();
796 }
797
798
Chris Lattnere98dda62001-07-14 06:10:16 +0000799// ConstVal - The various declarations that go into the constant pool. This
800// includes all forward declarations of types, constants, and functions.
801//
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000802ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
803 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
804 if (ATy == 0)
805 ThrowException("Cannot make array constant with type: '" +
806 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +0000807 const Type *ETy = ATy->getElementType();
808 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +0000809
Chris Lattner30c89792001-09-07 16:35:17 +0000810 // Verify that we have the correct size...
811 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +0000812 ThrowException("Type mismatch: constant sized array initialized with " +
Chris Lattner30c89792001-09-07 16:35:17 +0000813 utostr($3->size()) + " arguments, but has size of " +
814 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +0000815
Chris Lattner30c89792001-09-07 16:35:17 +0000816 // Verify all elements are correct type!
817 for (unsigned i = 0; i < $3->size(); i++) {
818 if (ETy != (*$3)[i]->getType())
Chris Lattner00950542001-06-06 20:29:01 +0000819 ThrowException("Element #" + utostr(i) + " is not of type '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000820 ETy->getName() + "' as required!\nIt is of type '" +
821 (*$3)[i]->getType()->getName() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +0000822 }
823
Chris Lattner30c89792001-09-07 16:35:17 +0000824 $$ = ConstPoolArray::get(ATy, *$3);
825 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000826 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000827 | Types '[' ']' {
828 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
829 if (ATy == 0)
830 ThrowException("Cannot make array constant with type: '" +
831 (*$1)->getDescription() + "'!");
832
833 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +0000834 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +0000835 ThrowException("Type mismatch: constant sized array initialized with 0"
Chris Lattner30c89792001-09-07 16:35:17 +0000836 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000837 $$ = ConstPoolArray::get(ATy, vector<ConstPoolVal*>());
Chris Lattner30c89792001-09-07 16:35:17 +0000838 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +0000839 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000840 | Types 'c' STRINGCONSTANT {
841 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
842 if (ATy == 0)
843 ThrowException("Cannot make array constant with type: '" +
844 (*$1)->getDescription() + "'!");
845
Chris Lattner30c89792001-09-07 16:35:17 +0000846 int NumElements = ATy->getNumElements();
847 const Type *ETy = ATy->getElementType();
848 char *EndStr = UnEscapeLexed($3, true);
849 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +0000850 ThrowException("Can't build string constant of size " +
Chris Lattner30c89792001-09-07 16:35:17 +0000851 itostr((int)(EndStr-$3)) +
852 " when array has size " + itostr(NumElements) + "!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000853 vector<ConstPoolVal*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +0000854 if (ETy == Type::SByteTy) {
855 for (char *C = $3; C != EndStr; ++C)
856 Vals.push_back(ConstPoolSInt::get(ETy, *C));
857 } else if (ETy == Type::UByteTy) {
858 for (char *C = $3; C != EndStr; ++C)
859 Vals.push_back(ConstPoolUInt::get(ETy, *C));
Chris Lattner93750fa2001-07-28 17:48:55 +0000860 } else {
Chris Lattner30c89792001-09-07 16:35:17 +0000861 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +0000862 ThrowException("Cannot build string arrays of non byte sized elements!");
863 }
Chris Lattner30c89792001-09-07 16:35:17 +0000864 free($3);
865 $$ = ConstPoolArray::get(ATy, Vals);
866 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +0000867 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000868 | Types '{' ConstVector '}' {
869 const StructType *STy = dyn_cast<const StructType>($1->get());
870 if (STy == 0)
871 ThrowException("Cannot make struct constant with type: '" +
872 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +0000873 // FIXME: TODO: Check to see that the constants are compatible with the type
874 // initializer!
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000875 $$ = ConstPoolStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +0000876 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000877 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000878 | Types NULL_TOK {
879 const PointerType *PTy = dyn_cast<const PointerType>($1->get());
880 if (PTy == 0)
881 ThrowException("Cannot make null pointer constant with type: '" +
882 (*$1)->getDescription() + "'!");
883
Chris Lattnerb7474512001-10-03 15:39:04 +0000884 $$ = ConstPoolPointer::getNull(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000885 delete $1;
886 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000887 | Types VAR_ID {
888 string Name($2); free($2); // Change to a responsible mem manager
889 const PointerType *Ty = dyn_cast<const PointerType>($1->get());
890 if (Ty == 0)
891 ThrowException("Global const reference must be a pointer type!");
892
893 Value *N = lookupInSymbolTable(Ty, Name);
894 if (N == 0)
895 ThrowException("Global pointer reference '%" + Name +
896 "' must be defined before use!");
897
898 // TODO FIXME: This should also allow methods... when common baseclass
899 // exists
900 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(N)) {
901 $$ = ConstPoolPointerReference::get(GV);
902 } else {
903 ThrowException("'%" + Name + "' is not a global value reference!");
904 }
905
906 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +0000907 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000908
Chris Lattner00950542001-06-06 20:29:01 +0000909
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000910ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000911 if (!ConstPoolSInt::isValueValidForType($1, $2))
912 ThrowException("Constant value doesn't fit in type!");
Chris Lattner30c89792001-09-07 16:35:17 +0000913 $$ = ConstPoolSInt::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000914 }
915 | UIntType EUINT64VAL { // integral constants
916 if (!ConstPoolUInt::isValueValidForType($1, $2))
917 ThrowException("Constant value doesn't fit in type!");
Chris Lattner30c89792001-09-07 16:35:17 +0000918 $$ = ConstPoolUInt::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000919 }
920 | BOOL TRUE { // Boolean constants
Chris Lattner30c89792001-09-07 16:35:17 +0000921 $$ = ConstPoolBool::True;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000922 }
923 | BOOL FALSE { // Boolean constants
Chris Lattner30c89792001-09-07 16:35:17 +0000924 $$ = ConstPoolBool::False;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000925 }
926 | FPType FPVAL { // Float & Double constants
Chris Lattner30c89792001-09-07 16:35:17 +0000927 $$ = ConstPoolFP::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000928 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000929
Chris Lattnere98dda62001-07-14 06:10:16 +0000930// ConstVector - A list of comma seperated constants.
Chris Lattner00950542001-06-06 20:29:01 +0000931ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +0000932 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +0000933 }
934 | ConstVal {
935 $$ = new vector<ConstPoolVal*>();
Chris Lattner30c89792001-09-07 16:35:17 +0000936 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +0000937 }
938
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000939
Chris Lattner1781aca2001-09-18 04:00:54 +0000940// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
941GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; }
942
Chris Lattner00950542001-06-06 20:29:01 +0000943
Chris Lattnere98dda62001-07-14 06:10:16 +0000944// ConstPool - Constants with optional names assigned to them.
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000945ConstPool : ConstPool OptAssign CONST ConstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +0000946 if (setValueName($4, $2)) { assert(0 && "No redefinitions allowed!"); }
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000947 InsertValue($4);
Chris Lattner00950542001-06-06 20:29:01 +0000948 }
Chris Lattner30c89792001-09-07 16:35:17 +0000949 | ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattner1781aca2001-09-18 04:00:54 +0000950 // TODO: FIXME when Type are not const
Chris Lattnerb7474512001-10-03 15:39:04 +0000951 if (!setValueName(const_cast<Type*>($4->get()), $2)) {
952 // If this is not a redefinition of a type...
953 if (!$2) {
954 InsertType($4->get(),
955 inMethodScope() ? CurMeth.Types : CurModule.Types);
956 }
957 delete $4;
Chris Lattner1781aca2001-09-18 04:00:54 +0000958
Chris Lattnerb7474512001-10-03 15:39:04 +0000959 ResolveSomeTypes(inMethodScope() ? CurMeth.LateResolveTypes :
960 CurModule.LateResolveTypes);
Chris Lattner30c89792001-09-07 16:35:17 +0000961 }
Chris Lattner30c89792001-09-07 16:35:17 +0000962 }
963 | ConstPool MethodProto { // Method prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +0000964 }
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000965 | ConstPool OptAssign GlobalType ConstVal {
Chris Lattner1781aca2001-09-18 04:00:54 +0000966 const Type *Ty = $4->getType();
967 // Global declarations appear in Constant Pool
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000968 ConstPoolVal *Initializer = $4;
Chris Lattner1781aca2001-09-18 04:00:54 +0000969 if (Initializer == 0)
970 ThrowException("Global value initializer is not a constant!");
971
Chris Lattneref9c23f2001-10-03 14:53:21 +0000972 GlobalVariable *GV = new GlobalVariable(Ty, $3, Initializer);
Chris Lattnerb7474512001-10-03 15:39:04 +0000973 if (!setValueName(GV, $2)) { // If not redefining...
974 CurModule.CurrentModule->getGlobalList().push_back(GV);
975 InsertValue(GV, CurModule.Values);
976 }
Chris Lattner1781aca2001-09-18 04:00:54 +0000977 }
978 | ConstPool OptAssign UNINIT GlobalType Types {
979 const Type *Ty = *$5;
980 // Global declarations appear in Constant Pool
Chris Lattnercfe26c92001-10-01 18:26:53 +0000981 if (isa<ArrayType>(Ty) && cast<ArrayType>(Ty)->isUnsized()) {
Chris Lattner1781aca2001-09-18 04:00:54 +0000982 ThrowException("Type '" + Ty->getDescription() +
983 "' is not a sized type!");
Chris Lattner70cc3392001-09-10 07:58:01 +0000984 }
Chris Lattner1781aca2001-09-18 04:00:54 +0000985
Chris Lattneref9c23f2001-10-03 14:53:21 +0000986 GlobalVariable *GV = new GlobalVariable(Ty, $4);
Chris Lattnerb7474512001-10-03 15:39:04 +0000987 if (!setValueName(GV, $2)) { // If not redefining...
988 CurModule.CurrentModule->getGlobalList().push_back(GV);
989 InsertValue(GV, CurModule.Values);
990 }
Chris Lattnere98dda62001-07-14 06:10:16 +0000991 }
Chris Lattner00950542001-06-06 20:29:01 +0000992 | /* empty: end of list */ {
993 }
994
995
996//===----------------------------------------------------------------------===//
997// Rules to match Modules
998//===----------------------------------------------------------------------===//
999
1000// Module rule: Capture the result of parsing the whole file into a result
1001// variable...
1002//
1003Module : MethodList {
1004 $$ = ParserResult = $1;
1005 CurModule.ModuleDone();
1006}
1007
Chris Lattnere98dda62001-07-14 06:10:16 +00001008// MethodList - A list of methods, preceeded by a constant pool.
1009//
Chris Lattner00950542001-06-06 20:29:01 +00001010MethodList : MethodList Method {
Chris Lattner00950542001-06-06 20:29:01 +00001011 $$ = $1;
Chris Lattnere1815642001-07-15 06:35:53 +00001012 if (!$2->getParent())
1013 $1->getMethodList().push_back($2);
1014 CurMeth.MethodDone();
Chris Lattner00950542001-06-06 20:29:01 +00001015 }
Chris Lattnere1815642001-07-15 06:35:53 +00001016 | MethodList MethodProto {
1017 $$ = $1;
Chris Lattnere1815642001-07-15 06:35:53 +00001018 }
Chris Lattner00950542001-06-06 20:29:01 +00001019 | ConstPool IMPLEMENTATION {
1020 $$ = CurModule.CurrentModule;
Chris Lattner30c89792001-09-07 16:35:17 +00001021 // Resolve circular types before we parse the body of the module
1022 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner00950542001-06-06 20:29:01 +00001023 }
1024
1025
1026//===----------------------------------------------------------------------===//
1027// Rules to match Method Headers
1028//===----------------------------------------------------------------------===//
1029
1030OptVAR_ID : VAR_ID | /*empty*/ { $$ = 0; }
1031
1032ArgVal : Types OptVAR_ID {
Chris Lattner30c89792001-09-07 16:35:17 +00001033 $$ = new MethodArgument(*$1); delete $1;
Chris Lattnerb7474512001-10-03 15:39:04 +00001034 if (setValueName($$, $2)) { assert(0 && "No arg redef allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001035}
1036
1037ArgListH : ArgVal ',' ArgListH {
1038 $$ = $3;
1039 $3->push_front($1);
1040 }
1041 | ArgVal {
1042 $$ = new list<MethodArgument*>();
1043 $$->push_front($1);
1044 }
Chris Lattner8b81bf52001-07-25 22:47:46 +00001045 | DOTDOTDOT {
1046 $$ = new list<MethodArgument*>();
1047 $$->push_back(new MethodArgument(Type::VoidTy));
1048 }
Chris Lattner00950542001-06-06 20:29:01 +00001049
1050ArgList : ArgListH {
1051 $$ = $1;
1052 }
1053 | /* empty */ {
1054 $$ = 0;
1055 }
1056
1057MethodHeaderH : TypesV STRINGCONSTANT '(' ArgList ')' {
Chris Lattner93750fa2001-07-28 17:48:55 +00001058 UnEscapeLexed($2);
Chris Lattner30c89792001-09-07 16:35:17 +00001059 vector<const Type*> ParamTypeList;
Chris Lattner00950542001-06-06 20:29:01 +00001060 if ($4)
Chris Lattner7fc9fe32001-06-27 23:41:11 +00001061 for (list<MethodArgument*>::iterator I = $4->begin(); I != $4->end(); ++I)
Chris Lattner00950542001-06-06 20:29:01 +00001062 ParamTypeList.push_back((*I)->getType());
1063
Chris Lattneref9c23f2001-10-03 14:53:21 +00001064 const MethodType *MT = MethodType::get(*$1, ParamTypeList);
1065 const PointerType *PMT = PointerType::get(MT);
Chris Lattner30c89792001-09-07 16:35:17 +00001066 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001067
Chris Lattnere1815642001-07-15 06:35:53 +00001068 Method *M = 0;
1069 if (SymbolTable *ST = CurModule.CurrentModule->getSymbolTable()) {
Chris Lattneref9c23f2001-10-03 14:53:21 +00001070 if (Value *V = ST->lookup(PMT, $2)) { // Method already in symtab?
1071 M = cast<Method>(V);
Chris Lattner00950542001-06-06 20:29:01 +00001072
Chris Lattnere1815642001-07-15 06:35:53 +00001073 // Yes it is. If this is the case, either we need to be a forward decl,
1074 // or it needs to be.
1075 if (!CurMeth.isDeclare && !M->isExternal())
1076 ThrowException("Redefinition of method '" + string($2) + "'!");
1077 }
1078 }
1079
1080 if (M == 0) { // Not already defined?
1081 M = new Method(MT, $2);
1082 InsertValue(M, CurModule.Values);
1083 }
1084
1085 free($2); // Free strdup'd memory!
Chris Lattner00950542001-06-06 20:29:01 +00001086
1087 CurMeth.MethodStart(M);
1088
1089 // Add all of the arguments we parsed to the method...
Chris Lattnere1815642001-07-15 06:35:53 +00001090 if ($4 && !CurMeth.isDeclare) { // Is null if empty...
Chris Lattner00950542001-06-06 20:29:01 +00001091 Method::ArgumentListType &ArgList = M->getArgumentList();
1092
Chris Lattner7fc9fe32001-06-27 23:41:11 +00001093 for (list<MethodArgument*>::iterator I = $4->begin(); I != $4->end(); ++I) {
Chris Lattner00950542001-06-06 20:29:01 +00001094 InsertValue(*I);
1095 ArgList.push_back(*I);
1096 }
1097 delete $4; // We're now done with the argument list
1098 }
1099}
1100
1101MethodHeader : MethodHeaderH ConstPool BEGINTOK {
1102 $$ = CurMeth.CurrentMethod;
Chris Lattner30c89792001-09-07 16:35:17 +00001103
1104 // Resolve circular types before we parse the body of the method.
1105 ResolveTypes(CurMeth.LateResolveTypes);
Chris Lattner00950542001-06-06 20:29:01 +00001106}
1107
1108Method : BasicBlockList END {
1109 $$ = $1;
1110}
1111
Chris Lattnere1815642001-07-15 06:35:53 +00001112MethodProto : DECLARE { CurMeth.isDeclare = true; } MethodHeaderH {
1113 $$ = CurMeth.CurrentMethod;
Chris Lattner93750fa2001-07-28 17:48:55 +00001114 if (!$$->getParent())
1115 CurModule.CurrentModule->getMethodList().push_back($$);
1116 CurMeth.MethodDone();
Chris Lattnere1815642001-07-15 06:35:53 +00001117}
Chris Lattner00950542001-06-06 20:29:01 +00001118
1119//===----------------------------------------------------------------------===//
1120// Rules to match Basic Blocks
1121//===----------------------------------------------------------------------===//
1122
1123ConstValueRef : ESINT64VAL { // A reference to a direct constant
1124 $$ = ValID::create($1);
1125 }
1126 | EUINT64VAL {
1127 $$ = ValID::create($1);
1128 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001129 | FPVAL { // Perhaps it's an FP constant?
1130 $$ = ValID::create($1);
1131 }
Chris Lattner00950542001-06-06 20:29:01 +00001132 | TRUE {
1133 $$ = ValID::create((int64_t)1);
1134 }
1135 | FALSE {
1136 $$ = ValID::create((int64_t)0);
1137 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001138 | NULL_TOK {
1139 $$ = ValID::createNull();
1140 }
1141
Chris Lattner93750fa2001-07-28 17:48:55 +00001142/*
Chris Lattner00950542001-06-06 20:29:01 +00001143 | STRINGCONSTANT { // Quoted strings work too... especially for methods
1144 $$ = ValID::create_conststr($1);
1145 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001146*/
Chris Lattner00950542001-06-06 20:29:01 +00001147
1148// ValueRef - A reference to a definition...
1149ValueRef : INTVAL { // Is it an integer reference...?
1150 $$ = ValID::create($1);
1151 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001152 | VAR_ID { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001153 $$ = ValID::create($1);
1154 }
1155 | ConstValueRef {
1156 $$ = $1;
1157 }
1158
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001159// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1160// type immediately preceeds the value reference, and allows complex constant
1161// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001162ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001163 $$ = getVal(*$1, $2); delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +00001164 }
Chris Lattner8b81bf52001-07-25 22:47:46 +00001165
Chris Lattner00950542001-06-06 20:29:01 +00001166
1167BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner89219832001-10-03 19:35:04 +00001168 ($$ = $1)->getBasicBlocks().push_back($2);
Chris Lattner00950542001-06-06 20:29:01 +00001169 }
1170 | MethodHeader BasicBlock { // Do not allow methods with 0 basic blocks
Chris Lattner89219832001-10-03 19:35:04 +00001171 ($$ = $1)->getBasicBlocks().push_back($2);
Chris Lattner00950542001-06-06 20:29:01 +00001172 }
1173
1174
1175// Basic blocks are terminated by branching instructions:
1176// br, br/cc, switch, ret
1177//
1178BasicBlock : InstructionList BBTerminatorInst {
1179 $1->getInstList().push_back($2);
1180 InsertValue($1);
1181 $$ = $1;
1182 }
1183 | LABELSTR InstructionList BBTerminatorInst {
1184 $2->getInstList().push_back($3);
Chris Lattnerb7474512001-10-03 15:39:04 +00001185 if (setValueName($2, $1)) { assert(0 && "No label redef allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001186
1187 InsertValue($2);
1188 $$ = $2;
1189 }
1190
1191InstructionList : InstructionList Inst {
1192 $1->getInstList().push_back($2);
1193 $$ = $1;
1194 }
1195 | /* empty */ {
1196 $$ = new BasicBlock();
1197 }
1198
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001199BBTerminatorInst : RET ResolvedVal { // Return with a result...
1200 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001201 }
1202 | RET VOID { // Return with no result...
1203 $$ = new ReturnInst();
1204 }
1205 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner9636a912001-10-01 16:18:37 +00001206 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $3)));
Chris Lattner00950542001-06-06 20:29:01 +00001207 } // Conditional Branch...
1208 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner9636a912001-10-01 16:18:37 +00001209 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $6)),
1210 cast<BasicBlock>(getVal(Type::LabelTy, $9)),
Chris Lattner00950542001-06-06 20:29:01 +00001211 getVal(Type::BoolTy, $3));
1212 }
1213 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1214 SwitchInst *S = new SwitchInst(getVal($2, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001215 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
Chris Lattner00950542001-06-06 20:29:01 +00001216 $$ = S;
1217
1218 list<pair<ConstPoolVal*, BasicBlock*> >::iterator I = $8->begin(),
1219 end = $8->end();
Chris Lattner7fc9fe32001-06-27 23:41:11 +00001220 for (; I != end; ++I)
Chris Lattner00950542001-06-06 20:29:01 +00001221 S->dest_push_back(I->first, I->second);
1222 }
1223
1224JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1225 $$ = $1;
Chris Lattnercfe26c92001-10-01 18:26:53 +00001226 ConstPoolVal *V = cast<ConstPoolVal>(getVal($2, $3, true));
Chris Lattner00950542001-06-06 20:29:01 +00001227 if (V == 0)
1228 ThrowException("May only switch on a constant pool value!");
1229
Chris Lattner9636a912001-10-01 16:18:37 +00001230 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($5, $6))));
Chris Lattner00950542001-06-06 20:29:01 +00001231 }
1232 | IntType ConstValueRef ',' LABEL ValueRef {
1233 $$ = new list<pair<ConstPoolVal*, BasicBlock*> >();
Chris Lattnercfe26c92001-10-01 18:26:53 +00001234 ConstPoolVal *V = cast<ConstPoolVal>(getVal($1, $2, true));
Chris Lattner00950542001-06-06 20:29:01 +00001235
1236 if (V == 0)
1237 ThrowException("May only switch on a constant pool value!");
1238
Chris Lattner9636a912001-10-01 16:18:37 +00001239 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($4, $5))));
Chris Lattner00950542001-06-06 20:29:01 +00001240 }
1241
1242Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001243 // Is this definition named?? if so, assign the name...
1244 if (setValueName($2, $1)) { assert(0 && "No redefin allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001245 InsertValue($2);
1246 $$ = $2;
1247}
1248
Chris Lattnerc24d2082001-06-11 15:04:20 +00001249PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
1250 $$ = new list<pair<Value*, BasicBlock*> >();
Chris Lattner30c89792001-09-07 16:35:17 +00001251 $$->push_back(make_pair(getVal(*$1, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001252 cast<BasicBlock>(getVal(Type::LabelTy, $5))));
Chris Lattner30c89792001-09-07 16:35:17 +00001253 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001254 }
1255 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1256 $$ = $1;
1257 $1->push_back(make_pair(getVal($1->front().first->getType(), $4),
Chris Lattner9636a912001-10-01 16:18:37 +00001258 cast<BasicBlock>(getVal(Type::LabelTy, $6))));
Chris Lattnerc24d2082001-06-11 15:04:20 +00001259 }
1260
1261
Chris Lattner30c89792001-09-07 16:35:17 +00001262ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner00950542001-06-06 20:29:01 +00001263 $$ = new list<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001264 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001265 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001266 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001267 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001268 $1->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001269 }
1270
1271// ValueRefListE - Just like ValueRefList, except that it may also be empty!
1272ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; }
1273
1274InstVal : BinaryOps Types ValueRef ',' ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001275 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001276 if ($$ == 0)
1277 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001278 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001279 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001280 | UnaryOps ResolvedVal {
1281 $$ = UnaryOperator::create($1, $2);
Chris Lattner00950542001-06-06 20:29:01 +00001282 if ($$ == 0)
1283 ThrowException("unary operator returned null!");
Chris Lattner09083092001-07-08 04:57:15 +00001284 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001285 | ShiftOps ResolvedVal ',' ResolvedVal {
1286 if ($4->getType() != Type::UByteTy)
1287 ThrowException("Shift amount must be ubyte!");
1288 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001289 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001290 | CAST ResolvedVal TO Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001291 $$ = new CastInst($2, *$4);
1292 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001293 }
Chris Lattnerc24d2082001-06-11 15:04:20 +00001294 | PHI PHIList {
1295 const Type *Ty = $2->front().first->getType();
1296 $$ = new PHINode(Ty);
Chris Lattner00950542001-06-06 20:29:01 +00001297 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001298 if ($2->front().first->getType() != Ty)
1299 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00001300 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001301 $2->pop_front();
1302 }
1303 delete $2; // Free the list...
1304 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001305 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattneref9c23f2001-10-03 14:53:21 +00001306 const PointerType *PMTy;
Chris Lattner8b81bf52001-07-25 22:47:46 +00001307 const MethodType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001308
Chris Lattneref9c23f2001-10-03 14:53:21 +00001309 if (!(PMTy = dyn_cast<PointerType>($2->get())) ||
1310 !(Ty = dyn_cast<MethodType>(PMTy->getValueType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001311 // Pull out the types of all of the arguments...
1312 vector<const Type*> ParamTypes;
Chris Lattneref9c23f2001-10-03 14:53:21 +00001313 if ($5) {
1314 for (list<Value*>::iterator I = $5->begin(), E = $5->end(); I != E; ++I)
1315 ParamTypes.push_back((*I)->getType());
1316 }
1317 Ty = MethodType::get($2->get(), ParamTypes);
1318 PMTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001319 }
Chris Lattner30c89792001-09-07 16:35:17 +00001320 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001321
Chris Lattneref9c23f2001-10-03 14:53:21 +00001322 Value *V = getVal(PMTy, $3); // Get the method we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00001323
Chris Lattner8b81bf52001-07-25 22:47:46 +00001324 // Create the call node...
1325 if (!$5) { // Has no arguments?
Chris Lattner9636a912001-10-01 16:18:37 +00001326 $$ = new CallInst(cast<Method>(V), vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00001327 } else { // Has arguments?
Chris Lattner00950542001-06-06 20:29:01 +00001328 // Loop through MethodType's arguments and ensure they are specified
1329 // correctly!
1330 //
1331 MethodType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001332 MethodType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
1333 list<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
1334
1335 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1336 if ((*ArgI)->getType() != *I)
1337 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner00950542001-06-06 20:29:01 +00001338 (*I)->getName() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00001339
Chris Lattner8b81bf52001-07-25 22:47:46 +00001340 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner00950542001-06-06 20:29:01 +00001341 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00001342
Chris Lattner9636a912001-10-01 16:18:37 +00001343 $$ = new CallInst(cast<Method>(V),
Chris Lattner8b81bf52001-07-25 22:47:46 +00001344 vector<Value*>($5->begin(), $5->end()));
1345 }
1346 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00001347 }
1348 | MemoryInst {
1349 $$ = $1;
1350 }
1351
Chris Lattner027dcc52001-07-08 21:10:27 +00001352// UByteList - List of ubyte values for load and store instructions
1353UByteList : ',' ConstVector {
1354 $$ = $2;
1355} | /* empty */ {
1356 $$ = new vector<ConstPoolVal*>();
1357}
1358
Chris Lattner00950542001-06-06 20:29:01 +00001359MemoryInst : MALLOC Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001360 $$ = new MallocInst(PointerType::get(*$2));
1361 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001362 }
1363 | MALLOC Types ',' UINT ValueRef {
Chris Lattnerb00c5822001-10-02 03:41:24 +00001364 if (!(*$2)->isArrayType() || cast<const ArrayType>($2->get())->isSized())
Chris Lattner30c89792001-09-07 16:35:17 +00001365 ThrowException("Trying to allocate " + (*$2)->getName() +
Chris Lattner00950542001-06-06 20:29:01 +00001366 " as unsized array!");
Chris Lattner30c89792001-09-07 16:35:17 +00001367 const Type *Ty = PointerType::get(*$2);
Chris Lattner8896eda2001-07-09 19:38:36 +00001368 $$ = new MallocInst(Ty, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001369 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001370 }
1371 | ALLOCA Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001372 $$ = new AllocaInst(PointerType::get(*$2));
1373 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001374 }
1375 | ALLOCA Types ',' UINT ValueRef {
Chris Lattnerb00c5822001-10-02 03:41:24 +00001376 if (!(*$2)->isArrayType() || cast<const ArrayType>($2->get())->isSized())
Chris Lattner30c89792001-09-07 16:35:17 +00001377 ThrowException("Trying to allocate " + (*$2)->getName() +
Chris Lattner00950542001-06-06 20:29:01 +00001378 " as unsized array!");
Chris Lattner30c89792001-09-07 16:35:17 +00001379 const Type *Ty = PointerType::get(*$2);
Chris Lattner00950542001-06-06 20:29:01 +00001380 Value *ArrSize = getVal($4, $5);
Chris Lattnerf0d0e9c2001-07-07 08:36:30 +00001381 $$ = new AllocaInst(Ty, ArrSize);
Chris Lattner30c89792001-09-07 16:35:17 +00001382 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001383 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001384 | FREE ResolvedVal {
1385 if (!$2->getType()->isPointerType())
1386 ThrowException("Trying to free nonpointer type " +
1387 $2->getType()->getName() + "!");
1388 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001389 }
1390
Chris Lattner027dcc52001-07-08 21:10:27 +00001391 | LOAD Types ValueRef UByteList {
Chris Lattner30c89792001-09-07 16:35:17 +00001392 if (!(*$2)->isPointerType())
1393 ThrowException("Can't load from nonpointer type: " + (*$2)->getName());
1394 if (LoadInst::getIndexedType(*$2, *$4) == 0)
Chris Lattner027dcc52001-07-08 21:10:27 +00001395 ThrowException("Invalid indices for load instruction!");
1396
Chris Lattner30c89792001-09-07 16:35:17 +00001397 $$ = new LoadInst(getVal(*$2, $3), *$4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001398 delete $4; // Free the vector...
Chris Lattner30c89792001-09-07 16:35:17 +00001399 delete $2;
Chris Lattner027dcc52001-07-08 21:10:27 +00001400 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001401 | STORE ResolvedVal ',' Types ValueRef UByteList {
Chris Lattner30c89792001-09-07 16:35:17 +00001402 if (!(*$4)->isPointerType())
1403 ThrowException("Can't store to a nonpointer type: " + (*$4)->getName());
1404 const Type *ElTy = StoreInst::getIndexedType(*$4, *$6);
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001405 if (ElTy == 0)
1406 ThrowException("Can't store into that field list!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001407 if (ElTy != $2->getType())
1408 ThrowException("Can't store '" + $2->getType()->getName() +
1409 "' into space of type '" + ElTy->getName() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001410 $$ = new StoreInst($2, getVal(*$4, $5), *$6);
1411 delete $4; delete $6;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001412 }
1413 | GETELEMENTPTR Types ValueRef UByteList {
Chris Lattner30c89792001-09-07 16:35:17 +00001414 if (!(*$2)->isPointerType())
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001415 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner30c89792001-09-07 16:35:17 +00001416 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
1417 ThrowException("Can't get element ptr '" + (*$2)->getName() + "'!");
1418 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
1419 delete $2; delete $4;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001420 }
Chris Lattner027dcc52001-07-08 21:10:27 +00001421
Chris Lattner00950542001-06-06 20:29:01 +00001422%%
Chris Lattner09083092001-07-08 04:57:15 +00001423int yyerror(const char *ErrorMsg) {
Chris Lattner00950542001-06-06 20:29:01 +00001424 ThrowException(string("Parse error: ") + ErrorMsg);
1425 return 0;
1426}