blob: 6edd2797c2aa8c8a56bbd239a00879a47bc1d66d [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 Lattner89219832001-10-03 19:35:04 +0000459 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
460 if (EGV->isConstant() == GV->isConstant() &&
461 (!EGV->hasInitializer() || !GV->hasInitializer() ||
462 EGV->getInitializer() == GV->getInitializer())) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000463
Chris Lattner89219832001-10-03 19:35:04 +0000464 // Make sure the existing global version gets the initializer!
465 if (GV->hasInitializer() && !EGV->hasInitializer())
466 EGV->setInitializer(GV->getInitializer());
467
468 return true; // They are equivalent!
469 }
Chris Lattnerb7474512001-10-03 15:39:04 +0000470 }
Chris Lattner9636a912001-10-01 16:18:37 +0000471 }
Chris Lattner30c89792001-09-07 16:35:17 +0000472 ThrowException("Redefinition of value name '" + Name + "' in the '" +
473 V->getType()->getDescription() + "' type plane!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000474 }
Chris Lattner00950542001-06-06 20:29:01 +0000475
Chris Lattner30c89792001-09-07 16:35:17 +0000476 V->setName(Name, ST);
Chris Lattnerb7474512001-10-03 15:39:04 +0000477 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000478}
479
Chris Lattner8896eda2001-07-09 19:38:36 +0000480
Chris Lattner30c89792001-09-07 16:35:17 +0000481//===----------------------------------------------------------------------===//
482// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000483//
Chris Lattner8896eda2001-07-09 19:38:36 +0000484
Chris Lattner30c89792001-09-07 16:35:17 +0000485// TypeContains - Returns true if Ty contains E in it.
486//
487static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattner3ff43872001-09-28 22:56:31 +0000488 return find(df_begin(Ty), df_end(Ty), E) != df_end(Ty);
Chris Lattner30c89792001-09-07 16:35:17 +0000489}
Chris Lattner698b56e2001-07-20 19:15:08 +0000490
Chris Lattner30c89792001-09-07 16:35:17 +0000491
492static vector<pair<unsigned, OpaqueType *> > UpRefs;
493
494static PATypeHolder<Type> HandleUpRefs(const Type *ty) {
495 PATypeHolder<Type> Ty(ty);
496 UR_OUT(UpRefs.size() << " upreferences active!\n");
497 for (unsigned i = 0; i < UpRefs.size(); ) {
498 UR_OUT("TypeContains(" << Ty->getDescription() << ", "
499 << UpRefs[i].second->getDescription() << ") = "
500 << TypeContains(Ty, UpRefs[i].second) << endl);
501 if (TypeContains(Ty, UpRefs[i].second)) {
502 unsigned Level = --UpRefs[i].first; // Decrement level of upreference
503 UR_OUT("Uplevel Ref Level = " << Level << endl);
504 if (Level == 0) { // Upreference should be resolved!
505 UR_OUT("About to resolve upreference!\n";
506 string OldName = UpRefs[i].second->getDescription());
507 UpRefs[i].second->refineAbstractTypeTo(Ty);
508 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
509 UR_OUT("Type '" << OldName << "' refined upreference to: "
510 << (const void*)Ty << ", " << Ty->getDescription() << endl);
511 continue;
512 }
513 }
514
515 ++i; // Otherwise, no resolve, move on...
Chris Lattner8896eda2001-07-09 19:38:36 +0000516 }
Chris Lattner30c89792001-09-07 16:35:17 +0000517 // FIXME: TODO: this should return the updated type
Chris Lattner8896eda2001-07-09 19:38:36 +0000518 return Ty;
519}
520
Chris Lattner30c89792001-09-07 16:35:17 +0000521template <class TypeTy>
522inline static void TypeDone(PATypeHolder<TypeTy> *Ty) {
523 if (UpRefs.size())
524 ThrowException("Invalid upreference in type: " + (*Ty)->getDescription());
525}
526
527// newTH - Allocate a new type holder for the specified type
528template <class TypeTy>
529inline static PATypeHolder<TypeTy> *newTH(const TypeTy *Ty) {
530 return new PATypeHolder<TypeTy>(Ty);
531}
532template <class TypeTy>
533inline static PATypeHolder<TypeTy> *newTH(const PATypeHolder<TypeTy> &TH) {
534 return new PATypeHolder<TypeTy>(TH);
535}
536
537
Chris Lattner00950542001-06-06 20:29:01 +0000538//===----------------------------------------------------------------------===//
539// RunVMAsmParser - Define an interface to this parser
540//===----------------------------------------------------------------------===//
541//
Chris Lattnera2850432001-07-22 18:36:00 +0000542Module *RunVMAsmParser(const string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000543 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000544 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000545 llvmAsmlineno = 1; // Reset the current line number...
546
547 CurModule.CurrentModule = new Module(); // Allocate a new module to read
548 yyparse(); // Parse the file.
549 Module *Result = ParserResult;
Chris Lattner00950542001-06-06 20:29:01 +0000550 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
551 ParserResult = 0;
552
553 return Result;
554}
555
556%}
557
558%union {
Chris Lattner30c89792001-09-07 16:35:17 +0000559 Module *ModuleVal;
560 Method *MethodVal;
561 MethodArgument *MethArgVal;
562 BasicBlock *BasicBlockVal;
563 TerminatorInst *TermInstVal;
564 Instruction *InstVal;
565 ConstPoolVal *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000566
Chris Lattner30c89792001-09-07 16:35:17 +0000567 const Type *PrimType;
568 PATypeHolder<Type> *TypeVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000569 Value *ValueVal;
570
571 list<MethodArgument*> *MethodArgList;
572 list<Value*> *ValueList;
573 list<PATypeHolder<Type> > *TypeList;
Chris Lattnerc24d2082001-06-11 15:04:20 +0000574 list<pair<Value*, BasicBlock*> > *PHIList; // Represent the RHS of PHI node
Chris Lattner00950542001-06-06 20:29:01 +0000575 list<pair<ConstPoolVal*, BasicBlock*> > *JumpTable;
Chris Lattner30c89792001-09-07 16:35:17 +0000576 vector<ConstPoolVal*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000577
Chris Lattner30c89792001-09-07 16:35:17 +0000578 int64_t SInt64Val;
579 uint64_t UInt64Val;
580 int SIntVal;
581 unsigned UIntVal;
582 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000583 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000584
Chris Lattner30c89792001-09-07 16:35:17 +0000585 char *StrVal; // This memory is strdup'd!
586 ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000587
Chris Lattner30c89792001-09-07 16:35:17 +0000588 Instruction::UnaryOps UnaryOpVal;
589 Instruction::BinaryOps BinaryOpVal;
590 Instruction::TermOps TermOpVal;
591 Instruction::MemoryOps MemOpVal;
592 Instruction::OtherOps OtherOpVal;
Chris Lattner00950542001-06-06 20:29:01 +0000593}
594
595%type <ModuleVal> Module MethodList
Chris Lattnere1815642001-07-15 06:35:53 +0000596%type <MethodVal> Method MethodProto MethodHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000597%type <BasicBlockVal> BasicBlock InstructionList
598%type <TermInstVal> BBTerminatorInst
599%type <InstVal> Inst InstVal MemoryInst
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000600%type <ConstVal> ConstVal
Chris Lattner027dcc52001-07-08 21:10:27 +0000601%type <ConstVector> ConstVector UByteList
Chris Lattner00950542001-06-06 20:29:01 +0000602%type <MethodArgList> ArgList ArgListH
603%type <MethArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000604%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000605%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner30c89792001-09-07 16:35:17 +0000606%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000607%type <JumpTable> JumpTable
Chris Lattner1781aca2001-09-18 04:00:54 +0000608%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Chris Lattner00950542001-06-06 20:29:01 +0000609
610%type <ValIDVal> ValueRef ConstValueRef // Reference to a definition or BB
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000611%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000612// Tokens and types for handling constant integer values
613//
614// ESINT64VAL - A negative number within long long range
615%token <SInt64Val> ESINT64VAL
616
617// EUINT64VAL - A positive number within uns. long long range
618%token <UInt64Val> EUINT64VAL
619%type <SInt64Val> EINT64VAL
620
621%token <SIntVal> SINTVAL // Signed 32 bit ints...
622%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
623%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000624%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000625
626// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000627%type <TypeVal> Types TypesV UpRTypes UpRTypesV
628%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
629%token <TypeVal> OPAQUE
630%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
631%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000632
633%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
634%type <StrVal> OptVAR_ID OptAssign
635
636
Chris Lattner1781aca2001-09-18 04:00:54 +0000637%token IMPLEMENTATION TRUE FALSE BEGINTOK END DECLARE GLOBAL CONSTANT UNINIT
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000638%token TO DOTDOTDOT STRING NULL_TOK CONST
Chris Lattner00950542001-06-06 20:29:01 +0000639
640// Basic Block Terminating Operators
641%token <TermOpVal> RET BR SWITCH
642
643// Unary Operators
644%type <UnaryOpVal> UnaryOps // all the unary operators
Chris Lattner71496b32001-07-08 19:03:27 +0000645%token <UnaryOpVal> NOT
Chris Lattner00950542001-06-06 20:29:01 +0000646
647// Binary Operators
648%type <BinaryOpVal> BinaryOps // all the binary operators
649%token <BinaryOpVal> ADD SUB MUL DIV REM
Chris Lattner027dcc52001-07-08 21:10:27 +0000650%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000651
652// Memory Instructions
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000653%token <MemoryOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000654
Chris Lattner027dcc52001-07-08 21:10:27 +0000655// Other Operators
656%type <OtherOpVal> ShiftOps
657%token <OtherOpVal> PHI CALL CAST SHL SHR
658
Chris Lattner00950542001-06-06 20:29:01 +0000659%start Module
660%%
661
662// Handle constant integer size restriction and conversion...
663//
664
665INTVAL : SINTVAL
666INTVAL : UINTVAL {
667 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
668 ThrowException("Value too large for type!");
669 $$ = (int32_t)$1;
670}
671
672
673EINT64VAL : ESINT64VAL // These have same type and can't cause problems...
674EINT64VAL : EUINT64VAL {
675 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
676 ThrowException("Value too large for type!");
677 $$ = (int64_t)$1;
678}
679
Chris Lattner00950542001-06-06 20:29:01 +0000680// Operations that are notably excluded from this list include:
681// RET, BR, & SWITCH because they end basic blocks and are treated specially.
682//
Chris Lattner09083092001-07-08 04:57:15 +0000683UnaryOps : NOT
Chris Lattner00950542001-06-06 20:29:01 +0000684BinaryOps : ADD | SUB | MUL | DIV | REM
685BinaryOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE
Chris Lattner027dcc52001-07-08 21:10:27 +0000686ShiftOps : SHL | SHR
Chris Lattner00950542001-06-06 20:29:01 +0000687
Chris Lattnere98dda62001-07-14 06:10:16 +0000688// These are some types that allow classification if we only want a particular
689// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner00950542001-06-06 20:29:01 +0000690SIntType : LONG | INT | SHORT | SBYTE
691UIntType : ULONG | UINT | USHORT | UBYTE
Chris Lattner30c89792001-09-07 16:35:17 +0000692IntType : SIntType | UIntType
693FPType : FLOAT | DOUBLE
Chris Lattner00950542001-06-06 20:29:01 +0000694
Chris Lattnere98dda62001-07-14 06:10:16 +0000695// OptAssign - Value producing statements have an optional assignment component
Chris Lattner00950542001-06-06 20:29:01 +0000696OptAssign : VAR_ID '=' {
697 $$ = $1;
698 }
699 | /*empty*/ {
700 $$ = 0;
701 }
702
Chris Lattner30c89792001-09-07 16:35:17 +0000703
704//===----------------------------------------------------------------------===//
705// Types includes all predefined types... except void, because it can only be
706// used in specific contexts (method returning void for example). To have
707// access to it, a user must explicitly use TypesV.
708//
709
710// TypesV includes all of 'Types', but it also includes the void type.
711TypesV : Types | VOID { $$ = newTH($1); }
712UpRTypesV : UpRTypes | VOID { $$ = newTH($1); }
713
714Types : UpRTypes {
715 TypeDone($$ = $1);
716 }
717
718
719// Derived types are added later...
720//
721PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT
722PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL
723UpRTypes : OPAQUE | PrimType { $$ = newTH($1); }
724UpRTypes : ValueRef { // Named types are also simple types...
725 $$ = newTH(getTypeVal($1));
726}
727
Chris Lattner30c89792001-09-07 16:35:17 +0000728// Include derived types in the Types production.
729//
730UpRTypes : '\\' EUINT64VAL { // Type UpReference
731 if ($2 > (uint64_t)INT64_MAX) ThrowException("Value out of range!");
732 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
733 UpRefs.push_back(make_pair((unsigned)$2, OT)); // Add to vector...
734 $$ = newTH<Type>(OT);
735 UR_OUT("New Upreference!\n");
736 }
737 | UpRTypesV '(' ArgTypeListI ')' { // Method derived type?
738 vector<const Type*> Params;
739 mapto($3->begin(), $3->end(), back_inserter(Params),
740 mem_fun_ref(&PATypeHandle<Type>::get));
741 $$ = newTH(HandleUpRefs(MethodType::get(*$1, Params)));
742 delete $3; // Delete the argument list
743 delete $1; // Delete the old type handle
744 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000745 | '[' UpRTypesV ']' { // Unsized array type?
746 $$ = newTH<Type>(HandleUpRefs(ArrayType::get(*$2)));
747 delete $2;
Chris Lattner30c89792001-09-07 16:35:17 +0000748 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000749 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
750 $$ = newTH<Type>(HandleUpRefs(ArrayType::get(*$4, (int)$2)));
751 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +0000752 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000753 | '{' TypeListI '}' { // Structure type?
754 vector<const Type*> Elements;
755 mapto($2->begin(), $2->end(), back_inserter(Elements),
756 mem_fun_ref(&PATypeHandle<Type>::get));
Chris Lattner30c89792001-09-07 16:35:17 +0000757
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000758 $$ = newTH<Type>(HandleUpRefs(StructType::get(Elements)));
759 delete $2;
760 }
761 | '{' '}' { // Empty structure type?
762 $$ = newTH<Type>(StructType::get(vector<const Type*>()));
763 }
764 | UpRTypes '*' { // Pointer type?
765 $$ = newTH<Type>(HandleUpRefs(PointerType::get(*$1)));
766 delete $1;
767 }
Chris Lattner30c89792001-09-07 16:35:17 +0000768
769// TypeList - Used for struct declarations and as a basis for method type
770// declaration type lists
771//
772TypeListI : UpRTypes {
773 $$ = new list<PATypeHolder<Type> >();
774 $$->push_back(*$1); delete $1;
775 }
776 | TypeListI ',' UpRTypes {
777 ($$=$1)->push_back(*$3); delete $3;
778 }
779
780// ArgTypeList - List of types for a method type declaration...
781ArgTypeListI : TypeListI
782 | TypeListI ',' DOTDOTDOT {
783 ($$=$1)->push_back(Type::VoidTy);
784 }
785 | DOTDOTDOT {
786 ($$ = new list<PATypeHolder<Type> >())->push_back(Type::VoidTy);
787 }
788 | /*empty*/ {
789 $$ = new list<PATypeHolder<Type> >();
790 }
791
792
Chris Lattnere98dda62001-07-14 06:10:16 +0000793// ConstVal - The various declarations that go into the constant pool. This
794// includes all forward declarations of types, constants, and functions.
795//
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000796ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
797 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
798 if (ATy == 0)
799 ThrowException("Cannot make array constant with type: '" +
800 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +0000801 const Type *ETy = ATy->getElementType();
802 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +0000803
Chris Lattner30c89792001-09-07 16:35:17 +0000804 // Verify that we have the correct size...
805 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +0000806 ThrowException("Type mismatch: constant sized array initialized with " +
Chris Lattner30c89792001-09-07 16:35:17 +0000807 utostr($3->size()) + " arguments, but has size of " +
808 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +0000809
Chris Lattner30c89792001-09-07 16:35:17 +0000810 // Verify all elements are correct type!
811 for (unsigned i = 0; i < $3->size(); i++) {
812 if (ETy != (*$3)[i]->getType())
Chris Lattner00950542001-06-06 20:29:01 +0000813 ThrowException("Element #" + utostr(i) + " is not of type '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000814 ETy->getName() + "' as required!\nIt is of type '" +
815 (*$3)[i]->getType()->getName() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +0000816 }
817
Chris Lattner30c89792001-09-07 16:35:17 +0000818 $$ = ConstPoolArray::get(ATy, *$3);
819 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000820 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000821 | Types '[' ']' {
822 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
823 if (ATy == 0)
824 ThrowException("Cannot make array constant with type: '" +
825 (*$1)->getDescription() + "'!");
826
827 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +0000828 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +0000829 ThrowException("Type mismatch: constant sized array initialized with 0"
Chris Lattner30c89792001-09-07 16:35:17 +0000830 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000831 $$ = ConstPoolArray::get(ATy, vector<ConstPoolVal*>());
Chris Lattner30c89792001-09-07 16:35:17 +0000832 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +0000833 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000834 | Types 'c' STRINGCONSTANT {
835 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
836 if (ATy == 0)
837 ThrowException("Cannot make array constant with type: '" +
838 (*$1)->getDescription() + "'!");
839
Chris Lattner30c89792001-09-07 16:35:17 +0000840 int NumElements = ATy->getNumElements();
841 const Type *ETy = ATy->getElementType();
842 char *EndStr = UnEscapeLexed($3, true);
843 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +0000844 ThrowException("Can't build string constant of size " +
Chris Lattner30c89792001-09-07 16:35:17 +0000845 itostr((int)(EndStr-$3)) +
846 " when array has size " + itostr(NumElements) + "!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000847 vector<ConstPoolVal*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +0000848 if (ETy == Type::SByteTy) {
849 for (char *C = $3; C != EndStr; ++C)
850 Vals.push_back(ConstPoolSInt::get(ETy, *C));
851 } else if (ETy == Type::UByteTy) {
852 for (char *C = $3; C != EndStr; ++C)
853 Vals.push_back(ConstPoolUInt::get(ETy, *C));
Chris Lattner93750fa2001-07-28 17:48:55 +0000854 } else {
Chris Lattner30c89792001-09-07 16:35:17 +0000855 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +0000856 ThrowException("Cannot build string arrays of non byte sized elements!");
857 }
Chris Lattner30c89792001-09-07 16:35:17 +0000858 free($3);
859 $$ = ConstPoolArray::get(ATy, Vals);
860 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +0000861 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000862 | Types '{' ConstVector '}' {
863 const StructType *STy = dyn_cast<const StructType>($1->get());
864 if (STy == 0)
865 ThrowException("Cannot make struct constant with type: '" +
866 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +0000867 // FIXME: TODO: Check to see that the constants are compatible with the type
868 // initializer!
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000869 $$ = ConstPoolStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +0000870 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000871 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000872 | Types NULL_TOK {
873 const PointerType *PTy = dyn_cast<const PointerType>($1->get());
874 if (PTy == 0)
875 ThrowException("Cannot make null pointer constant with type: '" +
876 (*$1)->getDescription() + "'!");
877
Chris Lattnerb7474512001-10-03 15:39:04 +0000878 $$ = ConstPoolPointer::getNull(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000879 delete $1;
880 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000881 | Types VAR_ID {
882 string Name($2); free($2); // Change to a responsible mem manager
883 const PointerType *Ty = dyn_cast<const PointerType>($1->get());
884 if (Ty == 0)
885 ThrowException("Global const reference must be a pointer type!");
886
887 Value *N = lookupInSymbolTable(Ty, Name);
888 if (N == 0)
889 ThrowException("Global pointer reference '%" + Name +
890 "' must be defined before use!");
891
892 // TODO FIXME: This should also allow methods... when common baseclass
893 // exists
894 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(N)) {
895 $$ = ConstPoolPointerReference::get(GV);
896 } else {
897 ThrowException("'%" + Name + "' is not a global value reference!");
898 }
899
900 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +0000901 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000902
Chris Lattner00950542001-06-06 20:29:01 +0000903
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000904ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000905 if (!ConstPoolSInt::isValueValidForType($1, $2))
906 ThrowException("Constant value doesn't fit in type!");
Chris Lattner30c89792001-09-07 16:35:17 +0000907 $$ = ConstPoolSInt::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000908 }
909 | UIntType EUINT64VAL { // integral constants
910 if (!ConstPoolUInt::isValueValidForType($1, $2))
911 ThrowException("Constant value doesn't fit in type!");
Chris Lattner30c89792001-09-07 16:35:17 +0000912 $$ = ConstPoolUInt::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000913 }
914 | BOOL TRUE { // Boolean constants
Chris Lattner30c89792001-09-07 16:35:17 +0000915 $$ = ConstPoolBool::True;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000916 }
917 | BOOL FALSE { // Boolean constants
Chris Lattner30c89792001-09-07 16:35:17 +0000918 $$ = ConstPoolBool::False;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000919 }
920 | FPType FPVAL { // Float & Double constants
Chris Lattner30c89792001-09-07 16:35:17 +0000921 $$ = ConstPoolFP::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000922 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000923
Chris Lattnere98dda62001-07-14 06:10:16 +0000924// ConstVector - A list of comma seperated constants.
Chris Lattner00950542001-06-06 20:29:01 +0000925ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +0000926 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +0000927 }
928 | ConstVal {
929 $$ = new vector<ConstPoolVal*>();
Chris Lattner30c89792001-09-07 16:35:17 +0000930 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +0000931 }
932
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000933
Chris Lattner1781aca2001-09-18 04:00:54 +0000934// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
935GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; }
936
Chris Lattner00950542001-06-06 20:29:01 +0000937
Chris Lattnere98dda62001-07-14 06:10:16 +0000938// ConstPool - Constants with optional names assigned to them.
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000939ConstPool : ConstPool OptAssign CONST ConstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +0000940 if (setValueName($4, $2)) { assert(0 && "No redefinitions allowed!"); }
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000941 InsertValue($4);
Chris Lattner00950542001-06-06 20:29:01 +0000942 }
Chris Lattner30c89792001-09-07 16:35:17 +0000943 | ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattner1781aca2001-09-18 04:00:54 +0000944 // TODO: FIXME when Type are not const
Chris Lattnerb7474512001-10-03 15:39:04 +0000945 if (!setValueName(const_cast<Type*>($4->get()), $2)) {
946 // If this is not a redefinition of a type...
947 if (!$2) {
948 InsertType($4->get(),
949 inMethodScope() ? CurMeth.Types : CurModule.Types);
950 }
951 delete $4;
Chris Lattner1781aca2001-09-18 04:00:54 +0000952
Chris Lattnerb7474512001-10-03 15:39:04 +0000953 ResolveSomeTypes(inMethodScope() ? CurMeth.LateResolveTypes :
954 CurModule.LateResolveTypes);
Chris Lattner30c89792001-09-07 16:35:17 +0000955 }
Chris Lattner30c89792001-09-07 16:35:17 +0000956 }
957 | ConstPool MethodProto { // Method prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +0000958 }
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000959 | ConstPool OptAssign GlobalType ConstVal {
Chris Lattner1781aca2001-09-18 04:00:54 +0000960 const Type *Ty = $4->getType();
961 // Global declarations appear in Constant Pool
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000962 ConstPoolVal *Initializer = $4;
Chris Lattner1781aca2001-09-18 04:00:54 +0000963 if (Initializer == 0)
964 ThrowException("Global value initializer is not a constant!");
965
Chris Lattneref9c23f2001-10-03 14:53:21 +0000966 GlobalVariable *GV = new GlobalVariable(Ty, $3, Initializer);
Chris Lattnerb7474512001-10-03 15:39:04 +0000967 if (!setValueName(GV, $2)) { // If not redefining...
968 CurModule.CurrentModule->getGlobalList().push_back(GV);
969 InsertValue(GV, CurModule.Values);
970 }
Chris Lattner1781aca2001-09-18 04:00:54 +0000971 }
972 | ConstPool OptAssign UNINIT GlobalType Types {
973 const Type *Ty = *$5;
974 // Global declarations appear in Constant Pool
Chris Lattnercfe26c92001-10-01 18:26:53 +0000975 if (isa<ArrayType>(Ty) && cast<ArrayType>(Ty)->isUnsized()) {
Chris Lattner1781aca2001-09-18 04:00:54 +0000976 ThrowException("Type '" + Ty->getDescription() +
977 "' is not a sized type!");
Chris Lattner70cc3392001-09-10 07:58:01 +0000978 }
Chris Lattner1781aca2001-09-18 04:00:54 +0000979
Chris Lattneref9c23f2001-10-03 14:53:21 +0000980 GlobalVariable *GV = new GlobalVariable(Ty, $4);
Chris Lattnerb7474512001-10-03 15:39:04 +0000981 if (!setValueName(GV, $2)) { // If not redefining...
982 CurModule.CurrentModule->getGlobalList().push_back(GV);
983 InsertValue(GV, CurModule.Values);
984 }
Chris Lattnere98dda62001-07-14 06:10:16 +0000985 }
Chris Lattner00950542001-06-06 20:29:01 +0000986 | /* empty: end of list */ {
987 }
988
989
990//===----------------------------------------------------------------------===//
991// Rules to match Modules
992//===----------------------------------------------------------------------===//
993
994// Module rule: Capture the result of parsing the whole file into a result
995// variable...
996//
997Module : MethodList {
998 $$ = ParserResult = $1;
999 CurModule.ModuleDone();
1000}
1001
Chris Lattnere98dda62001-07-14 06:10:16 +00001002// MethodList - A list of methods, preceeded by a constant pool.
1003//
Chris Lattner00950542001-06-06 20:29:01 +00001004MethodList : MethodList Method {
Chris Lattner00950542001-06-06 20:29:01 +00001005 $$ = $1;
Chris Lattnere1815642001-07-15 06:35:53 +00001006 if (!$2->getParent())
1007 $1->getMethodList().push_back($2);
1008 CurMeth.MethodDone();
Chris Lattner00950542001-06-06 20:29:01 +00001009 }
Chris Lattnere1815642001-07-15 06:35:53 +00001010 | MethodList MethodProto {
1011 $$ = $1;
Chris Lattnere1815642001-07-15 06:35:53 +00001012 }
Chris Lattner00950542001-06-06 20:29:01 +00001013 | ConstPool IMPLEMENTATION {
1014 $$ = CurModule.CurrentModule;
Chris Lattner30c89792001-09-07 16:35:17 +00001015 // Resolve circular types before we parse the body of the module
1016 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner00950542001-06-06 20:29:01 +00001017 }
1018
1019
1020//===----------------------------------------------------------------------===//
1021// Rules to match Method Headers
1022//===----------------------------------------------------------------------===//
1023
1024OptVAR_ID : VAR_ID | /*empty*/ { $$ = 0; }
1025
1026ArgVal : Types OptVAR_ID {
Chris Lattner30c89792001-09-07 16:35:17 +00001027 $$ = new MethodArgument(*$1); delete $1;
Chris Lattnerb7474512001-10-03 15:39:04 +00001028 if (setValueName($$, $2)) { assert(0 && "No arg redef allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001029}
1030
1031ArgListH : ArgVal ',' ArgListH {
1032 $$ = $3;
1033 $3->push_front($1);
1034 }
1035 | ArgVal {
1036 $$ = new list<MethodArgument*>();
1037 $$->push_front($1);
1038 }
Chris Lattner8b81bf52001-07-25 22:47:46 +00001039 | DOTDOTDOT {
1040 $$ = new list<MethodArgument*>();
1041 $$->push_back(new MethodArgument(Type::VoidTy));
1042 }
Chris Lattner00950542001-06-06 20:29:01 +00001043
1044ArgList : ArgListH {
1045 $$ = $1;
1046 }
1047 | /* empty */ {
1048 $$ = 0;
1049 }
1050
1051MethodHeaderH : TypesV STRINGCONSTANT '(' ArgList ')' {
Chris Lattner93750fa2001-07-28 17:48:55 +00001052 UnEscapeLexed($2);
Chris Lattner30c89792001-09-07 16:35:17 +00001053 vector<const Type*> ParamTypeList;
Chris Lattner00950542001-06-06 20:29:01 +00001054 if ($4)
Chris Lattner7fc9fe32001-06-27 23:41:11 +00001055 for (list<MethodArgument*>::iterator I = $4->begin(); I != $4->end(); ++I)
Chris Lattner00950542001-06-06 20:29:01 +00001056 ParamTypeList.push_back((*I)->getType());
1057
Chris Lattneref9c23f2001-10-03 14:53:21 +00001058 const MethodType *MT = MethodType::get(*$1, ParamTypeList);
1059 const PointerType *PMT = PointerType::get(MT);
Chris Lattner30c89792001-09-07 16:35:17 +00001060 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001061
Chris Lattnere1815642001-07-15 06:35:53 +00001062 Method *M = 0;
1063 if (SymbolTable *ST = CurModule.CurrentModule->getSymbolTable()) {
Chris Lattneref9c23f2001-10-03 14:53:21 +00001064 if (Value *V = ST->lookup(PMT, $2)) { // Method already in symtab?
1065 M = cast<Method>(V);
Chris Lattner00950542001-06-06 20:29:01 +00001066
Chris Lattnere1815642001-07-15 06:35:53 +00001067 // Yes it is. If this is the case, either we need to be a forward decl,
1068 // or it needs to be.
1069 if (!CurMeth.isDeclare && !M->isExternal())
1070 ThrowException("Redefinition of method '" + string($2) + "'!");
1071 }
1072 }
1073
1074 if (M == 0) { // Not already defined?
1075 M = new Method(MT, $2);
1076 InsertValue(M, CurModule.Values);
1077 }
1078
1079 free($2); // Free strdup'd memory!
Chris Lattner00950542001-06-06 20:29:01 +00001080
1081 CurMeth.MethodStart(M);
1082
1083 // Add all of the arguments we parsed to the method...
Chris Lattnere1815642001-07-15 06:35:53 +00001084 if ($4 && !CurMeth.isDeclare) { // Is null if empty...
Chris Lattner00950542001-06-06 20:29:01 +00001085 Method::ArgumentListType &ArgList = M->getArgumentList();
1086
Chris Lattner7fc9fe32001-06-27 23:41:11 +00001087 for (list<MethodArgument*>::iterator I = $4->begin(); I != $4->end(); ++I) {
Chris Lattner00950542001-06-06 20:29:01 +00001088 InsertValue(*I);
1089 ArgList.push_back(*I);
1090 }
1091 delete $4; // We're now done with the argument list
1092 }
1093}
1094
1095MethodHeader : MethodHeaderH ConstPool BEGINTOK {
1096 $$ = CurMeth.CurrentMethod;
Chris Lattner30c89792001-09-07 16:35:17 +00001097
1098 // Resolve circular types before we parse the body of the method.
1099 ResolveTypes(CurMeth.LateResolveTypes);
Chris Lattner00950542001-06-06 20:29:01 +00001100}
1101
1102Method : BasicBlockList END {
1103 $$ = $1;
1104}
1105
Chris Lattnere1815642001-07-15 06:35:53 +00001106MethodProto : DECLARE { CurMeth.isDeclare = true; } MethodHeaderH {
1107 $$ = CurMeth.CurrentMethod;
Chris Lattner93750fa2001-07-28 17:48:55 +00001108 if (!$$->getParent())
1109 CurModule.CurrentModule->getMethodList().push_back($$);
1110 CurMeth.MethodDone();
Chris Lattnere1815642001-07-15 06:35:53 +00001111}
Chris Lattner00950542001-06-06 20:29:01 +00001112
1113//===----------------------------------------------------------------------===//
1114// Rules to match Basic Blocks
1115//===----------------------------------------------------------------------===//
1116
1117ConstValueRef : ESINT64VAL { // A reference to a direct constant
1118 $$ = ValID::create($1);
1119 }
1120 | EUINT64VAL {
1121 $$ = ValID::create($1);
1122 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001123 | FPVAL { // Perhaps it's an FP constant?
1124 $$ = ValID::create($1);
1125 }
Chris Lattner00950542001-06-06 20:29:01 +00001126 | TRUE {
1127 $$ = ValID::create((int64_t)1);
1128 }
1129 | FALSE {
1130 $$ = ValID::create((int64_t)0);
1131 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001132 | NULL_TOK {
1133 $$ = ValID::createNull();
1134 }
1135
Chris Lattner93750fa2001-07-28 17:48:55 +00001136/*
Chris Lattner00950542001-06-06 20:29:01 +00001137 | STRINGCONSTANT { // Quoted strings work too... especially for methods
1138 $$ = ValID::create_conststr($1);
1139 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001140*/
Chris Lattner00950542001-06-06 20:29:01 +00001141
1142// ValueRef - A reference to a definition...
1143ValueRef : INTVAL { // Is it an integer reference...?
1144 $$ = ValID::create($1);
1145 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001146 | VAR_ID { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001147 $$ = ValID::create($1);
1148 }
1149 | ConstValueRef {
1150 $$ = $1;
1151 }
1152
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001153// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1154// type immediately preceeds the value reference, and allows complex constant
1155// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001156ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001157 $$ = getVal(*$1, $2); delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +00001158 }
Chris Lattner8b81bf52001-07-25 22:47:46 +00001159
Chris Lattner00950542001-06-06 20:29:01 +00001160
1161BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner89219832001-10-03 19:35:04 +00001162 ($$ = $1)->getBasicBlocks().push_back($2);
Chris Lattner00950542001-06-06 20:29:01 +00001163 }
1164 | MethodHeader BasicBlock { // Do not allow methods with 0 basic blocks
Chris Lattner89219832001-10-03 19:35:04 +00001165 ($$ = $1)->getBasicBlocks().push_back($2);
Chris Lattner00950542001-06-06 20:29:01 +00001166 }
1167
1168
1169// Basic blocks are terminated by branching instructions:
1170// br, br/cc, switch, ret
1171//
1172BasicBlock : InstructionList BBTerminatorInst {
1173 $1->getInstList().push_back($2);
1174 InsertValue($1);
1175 $$ = $1;
1176 }
1177 | LABELSTR InstructionList BBTerminatorInst {
1178 $2->getInstList().push_back($3);
Chris Lattnerb7474512001-10-03 15:39:04 +00001179 if (setValueName($2, $1)) { assert(0 && "No label redef allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001180
1181 InsertValue($2);
1182 $$ = $2;
1183 }
1184
1185InstructionList : InstructionList Inst {
1186 $1->getInstList().push_back($2);
1187 $$ = $1;
1188 }
1189 | /* empty */ {
1190 $$ = new BasicBlock();
1191 }
1192
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001193BBTerminatorInst : RET ResolvedVal { // Return with a result...
1194 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001195 }
1196 | RET VOID { // Return with no result...
1197 $$ = new ReturnInst();
1198 }
1199 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner9636a912001-10-01 16:18:37 +00001200 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $3)));
Chris Lattner00950542001-06-06 20:29:01 +00001201 } // Conditional Branch...
1202 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner9636a912001-10-01 16:18:37 +00001203 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $6)),
1204 cast<BasicBlock>(getVal(Type::LabelTy, $9)),
Chris Lattner00950542001-06-06 20:29:01 +00001205 getVal(Type::BoolTy, $3));
1206 }
1207 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1208 SwitchInst *S = new SwitchInst(getVal($2, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001209 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
Chris Lattner00950542001-06-06 20:29:01 +00001210 $$ = S;
1211
1212 list<pair<ConstPoolVal*, BasicBlock*> >::iterator I = $8->begin(),
1213 end = $8->end();
Chris Lattner7fc9fe32001-06-27 23:41:11 +00001214 for (; I != end; ++I)
Chris Lattner00950542001-06-06 20:29:01 +00001215 S->dest_push_back(I->first, I->second);
1216 }
1217
1218JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1219 $$ = $1;
Chris Lattnercfe26c92001-10-01 18:26:53 +00001220 ConstPoolVal *V = cast<ConstPoolVal>(getVal($2, $3, true));
Chris Lattner00950542001-06-06 20:29:01 +00001221 if (V == 0)
1222 ThrowException("May only switch on a constant pool value!");
1223
Chris Lattner9636a912001-10-01 16:18:37 +00001224 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($5, $6))));
Chris Lattner00950542001-06-06 20:29:01 +00001225 }
1226 | IntType ConstValueRef ',' LABEL ValueRef {
1227 $$ = new list<pair<ConstPoolVal*, BasicBlock*> >();
Chris Lattnercfe26c92001-10-01 18:26:53 +00001228 ConstPoolVal *V = cast<ConstPoolVal>(getVal($1, $2, true));
Chris Lattner00950542001-06-06 20:29:01 +00001229
1230 if (V == 0)
1231 ThrowException("May only switch on a constant pool value!");
1232
Chris Lattner9636a912001-10-01 16:18:37 +00001233 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($4, $5))));
Chris Lattner00950542001-06-06 20:29:01 +00001234 }
1235
1236Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001237 // Is this definition named?? if so, assign the name...
1238 if (setValueName($2, $1)) { assert(0 && "No redefin allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001239 InsertValue($2);
1240 $$ = $2;
1241}
1242
Chris Lattnerc24d2082001-06-11 15:04:20 +00001243PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
1244 $$ = new list<pair<Value*, BasicBlock*> >();
Chris Lattner30c89792001-09-07 16:35:17 +00001245 $$->push_back(make_pair(getVal(*$1, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001246 cast<BasicBlock>(getVal(Type::LabelTy, $5))));
Chris Lattner30c89792001-09-07 16:35:17 +00001247 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001248 }
1249 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1250 $$ = $1;
1251 $1->push_back(make_pair(getVal($1->front().first->getType(), $4),
Chris Lattner9636a912001-10-01 16:18:37 +00001252 cast<BasicBlock>(getVal(Type::LabelTy, $6))));
Chris Lattnerc24d2082001-06-11 15:04:20 +00001253 }
1254
1255
Chris Lattner30c89792001-09-07 16:35:17 +00001256ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner00950542001-06-06 20:29:01 +00001257 $$ = new list<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001258 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001259 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001260 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001261 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001262 $1->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001263 }
1264
1265// ValueRefListE - Just like ValueRefList, except that it may also be empty!
1266ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; }
1267
1268InstVal : BinaryOps Types ValueRef ',' ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001269 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001270 if ($$ == 0)
1271 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001272 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001273 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001274 | UnaryOps ResolvedVal {
1275 $$ = UnaryOperator::create($1, $2);
Chris Lattner00950542001-06-06 20:29:01 +00001276 if ($$ == 0)
1277 ThrowException("unary operator returned null!");
Chris Lattner09083092001-07-08 04:57:15 +00001278 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001279 | ShiftOps ResolvedVal ',' ResolvedVal {
1280 if ($4->getType() != Type::UByteTy)
1281 ThrowException("Shift amount must be ubyte!");
1282 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001283 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001284 | CAST ResolvedVal TO Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001285 $$ = new CastInst($2, *$4);
1286 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001287 }
Chris Lattnerc24d2082001-06-11 15:04:20 +00001288 | PHI PHIList {
1289 const Type *Ty = $2->front().first->getType();
1290 $$ = new PHINode(Ty);
Chris Lattner00950542001-06-06 20:29:01 +00001291 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001292 if ($2->front().first->getType() != Ty)
1293 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00001294 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001295 $2->pop_front();
1296 }
1297 delete $2; // Free the list...
1298 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001299 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattneref9c23f2001-10-03 14:53:21 +00001300 const PointerType *PMTy;
Chris Lattner8b81bf52001-07-25 22:47:46 +00001301 const MethodType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001302
Chris Lattneref9c23f2001-10-03 14:53:21 +00001303 if (!(PMTy = dyn_cast<PointerType>($2->get())) ||
1304 !(Ty = dyn_cast<MethodType>(PMTy->getValueType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001305 // Pull out the types of all of the arguments...
1306 vector<const Type*> ParamTypes;
Chris Lattneref9c23f2001-10-03 14:53:21 +00001307 if ($5) {
1308 for (list<Value*>::iterator I = $5->begin(), E = $5->end(); I != E; ++I)
1309 ParamTypes.push_back((*I)->getType());
1310 }
1311 Ty = MethodType::get($2->get(), ParamTypes);
1312 PMTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001313 }
Chris Lattner30c89792001-09-07 16:35:17 +00001314 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001315
Chris Lattneref9c23f2001-10-03 14:53:21 +00001316 Value *V = getVal(PMTy, $3); // Get the method we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00001317
Chris Lattner8b81bf52001-07-25 22:47:46 +00001318 // Create the call node...
1319 if (!$5) { // Has no arguments?
Chris Lattner9636a912001-10-01 16:18:37 +00001320 $$ = new CallInst(cast<Method>(V), vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00001321 } else { // Has arguments?
Chris Lattner00950542001-06-06 20:29:01 +00001322 // Loop through MethodType's arguments and ensure they are specified
1323 // correctly!
1324 //
1325 MethodType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001326 MethodType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
1327 list<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
1328
1329 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1330 if ((*ArgI)->getType() != *I)
1331 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner00950542001-06-06 20:29:01 +00001332 (*I)->getName() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00001333
Chris Lattner8b81bf52001-07-25 22:47:46 +00001334 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner00950542001-06-06 20:29:01 +00001335 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00001336
Chris Lattner9636a912001-10-01 16:18:37 +00001337 $$ = new CallInst(cast<Method>(V),
Chris Lattner8b81bf52001-07-25 22:47:46 +00001338 vector<Value*>($5->begin(), $5->end()));
1339 }
1340 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00001341 }
1342 | MemoryInst {
1343 $$ = $1;
1344 }
1345
Chris Lattner027dcc52001-07-08 21:10:27 +00001346// UByteList - List of ubyte values for load and store instructions
1347UByteList : ',' ConstVector {
1348 $$ = $2;
1349} | /* empty */ {
1350 $$ = new vector<ConstPoolVal*>();
1351}
1352
Chris Lattner00950542001-06-06 20:29:01 +00001353MemoryInst : MALLOC Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001354 $$ = new MallocInst(PointerType::get(*$2));
1355 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001356 }
1357 | MALLOC Types ',' UINT ValueRef {
Chris Lattnerb00c5822001-10-02 03:41:24 +00001358 if (!(*$2)->isArrayType() || cast<const ArrayType>($2->get())->isSized())
Chris Lattner30c89792001-09-07 16:35:17 +00001359 ThrowException("Trying to allocate " + (*$2)->getName() +
Chris Lattner00950542001-06-06 20:29:01 +00001360 " as unsized array!");
Chris Lattner30c89792001-09-07 16:35:17 +00001361 const Type *Ty = PointerType::get(*$2);
Chris Lattner8896eda2001-07-09 19:38:36 +00001362 $$ = new MallocInst(Ty, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001363 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001364 }
1365 | ALLOCA Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001366 $$ = new AllocaInst(PointerType::get(*$2));
1367 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001368 }
1369 | ALLOCA Types ',' UINT ValueRef {
Chris Lattnerb00c5822001-10-02 03:41:24 +00001370 if (!(*$2)->isArrayType() || cast<const ArrayType>($2->get())->isSized())
Chris Lattner30c89792001-09-07 16:35:17 +00001371 ThrowException("Trying to allocate " + (*$2)->getName() +
Chris Lattner00950542001-06-06 20:29:01 +00001372 " as unsized array!");
Chris Lattner30c89792001-09-07 16:35:17 +00001373 const Type *Ty = PointerType::get(*$2);
Chris Lattner00950542001-06-06 20:29:01 +00001374 Value *ArrSize = getVal($4, $5);
Chris Lattnerf0d0e9c2001-07-07 08:36:30 +00001375 $$ = new AllocaInst(Ty, ArrSize);
Chris Lattner30c89792001-09-07 16:35:17 +00001376 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001377 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001378 | FREE ResolvedVal {
1379 if (!$2->getType()->isPointerType())
1380 ThrowException("Trying to free nonpointer type " +
1381 $2->getType()->getName() + "!");
1382 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001383 }
1384
Chris Lattner027dcc52001-07-08 21:10:27 +00001385 | LOAD Types ValueRef UByteList {
Chris Lattner30c89792001-09-07 16:35:17 +00001386 if (!(*$2)->isPointerType())
1387 ThrowException("Can't load from nonpointer type: " + (*$2)->getName());
1388 if (LoadInst::getIndexedType(*$2, *$4) == 0)
Chris Lattner027dcc52001-07-08 21:10:27 +00001389 ThrowException("Invalid indices for load instruction!");
1390
Chris Lattner30c89792001-09-07 16:35:17 +00001391 $$ = new LoadInst(getVal(*$2, $3), *$4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001392 delete $4; // Free the vector...
Chris Lattner30c89792001-09-07 16:35:17 +00001393 delete $2;
Chris Lattner027dcc52001-07-08 21:10:27 +00001394 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001395 | STORE ResolvedVal ',' Types ValueRef UByteList {
Chris Lattner30c89792001-09-07 16:35:17 +00001396 if (!(*$4)->isPointerType())
1397 ThrowException("Can't store to a nonpointer type: " + (*$4)->getName());
1398 const Type *ElTy = StoreInst::getIndexedType(*$4, *$6);
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001399 if (ElTy == 0)
1400 ThrowException("Can't store into that field list!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001401 if (ElTy != $2->getType())
1402 ThrowException("Can't store '" + $2->getType()->getName() +
1403 "' into space of type '" + ElTy->getName() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001404 $$ = new StoreInst($2, getVal(*$4, $5), *$6);
1405 delete $4; delete $6;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001406 }
1407 | GETELEMENTPTR Types ValueRef UByteList {
Chris Lattner30c89792001-09-07 16:35:17 +00001408 if (!(*$2)->isPointerType())
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001409 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner30c89792001-09-07 16:35:17 +00001410 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
1411 ThrowException("Can't get element ptr '" + (*$2)->getName() + "'!");
1412 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
1413 delete $2; delete $4;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001414 }
Chris Lattner027dcc52001-07-08 21:10:27 +00001415
Chris Lattner00950542001-06-06 20:29:01 +00001416%%
Chris Lattner09083092001-07-08 04:57:15 +00001417int yyerror(const char *ErrorMsg) {
Chris Lattner00950542001-06-06 20:29:01 +00001418 ThrowException(string("Parse error: ") + ErrorMsg);
1419 return 0;
1420}