blob: 3c0bb8f101d4cf4e1b82e9626206f2ef14d23ec0 [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
Chris Lattner00950542001-06-06 20:29:01 +00007%{
8#include "ParserInternals.h"
Chris Lattner70cc3392001-09-10 07:58:01 +00009#include "llvm/Assembly/Parser.h"
Chris Lattner00950542001-06-06 20:29:01 +000010#include "llvm/SymbolTable.h"
11#include "llvm/Module.h"
Chris Lattner70cc3392001-09-10 07:58:01 +000012#include "llvm/GlobalVariable.h"
Chris Lattner79df7c02002-03-26 18:01:55 +000013#include "llvm/Function.h"
Chris Lattner70cc3392001-09-10 07:58:01 +000014#include "llvm/BasicBlock.h"
Chris Lattner00950542001-06-06 20:29:01 +000015#include "llvm/DerivedTypes.h"
Chris Lattner00950542001-06-06 20:29:01 +000016#include "llvm/iTerminators.h"
17#include "llvm/iMemory.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000018#include "llvm/iPHINode.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000019#include "Support/STLExtras.h"
20#include "Support/DepthFirstIterator.h"
Chris Lattner00950542001-06-06 20:29:01 +000021#include <list>
22#include <utility> // Get definition of pair class
Chris Lattner30c89792001-09-07 16:35:17 +000023#include <algorithm>
Chris Lattner00950542001-06-06 20:29:01 +000024#include <stdio.h> // This embarasment is due to our flex lexer...
Chris Lattner697954c2002-01-20 22:54:45 +000025#include <iostream>
26using std::list;
27using std::vector;
28using std::pair;
29using std::map;
30using std::pair;
31using std::make_pair;
32using std::cerr;
33using std::string;
Chris Lattner00950542001-06-06 20:29:01 +000034
Chris Lattner386a3b72001-10-16 19:54:17 +000035int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
Chris Lattner09083092001-07-08 04:57:15 +000036int yylex(); // declaration" of xxx warnings.
Chris Lattner00950542001-06-06 20:29:01 +000037int yyparse();
38
39static Module *ParserResult;
Chris Lattnera2850432001-07-22 18:36:00 +000040string CurFilename;
Chris Lattner00950542001-06-06 20:29:01 +000041
Chris Lattner30c89792001-09-07 16:35:17 +000042// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
43// relating to upreferences in the input stream.
44//
45//#define DEBUG_UPREFS 1
46#ifdef DEBUG_UPREFS
47#define UR_OUT(X) cerr << X
48#else
49#define UR_OUT(X)
50#endif
51
Chris Lattner00950542001-06-06 20:29:01 +000052// This contains info used when building the body of a method. It is destroyed
53// when the method is completed.
54//
55typedef vector<Value *> ValueList; // Numbered defs
Chris Lattner386a3b72001-10-16 19:54:17 +000056static void ResolveDefinitions(vector<ValueList> &LateResolvers,
57 vector<ValueList> *FutureLateResolvers = 0);
Chris Lattner00950542001-06-06 20:29:01 +000058
59static struct PerModuleInfo {
60 Module *CurrentModule;
Chris Lattner30c89792001-09-07 16:35:17 +000061 vector<ValueList> Values; // Module level numbered definitions
62 vector<ValueList> LateResolveValues;
Chris Lattner4a42e902001-10-22 05:56:09 +000063 vector<PATypeHolder<Type> > Types;
64 map<ValID, PATypeHolder<Type> > LateResolveTypes;
Chris Lattner00950542001-06-06 20:29:01 +000065
Chris Lattner2079fde2001-10-13 06:41:08 +000066 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
67 // references to global values. Global values may be referenced before they
68 // are defined, and if so, the temporary object that they represent is held
Chris Lattnere9bb2df2001-12-03 22:26:30 +000069 // here. This is used for forward references of ConstantPointerRefs.
Chris Lattner2079fde2001-10-13 06:41:08 +000070 //
71 typedef map<pair<const PointerType *, ValID>, GlobalVariable*> GlobalRefsType;
72 GlobalRefsType GlobalRefs;
73
Chris Lattner00950542001-06-06 20:29:01 +000074 void ModuleDone() {
Chris Lattner30c89792001-09-07 16:35:17 +000075 // If we could not resolve some methods at method compilation time (calls to
76 // methods before they are defined), resolve them now... Types are resolved
77 // when the constant pool has been completely parsed.
78 //
Chris Lattner00950542001-06-06 20:29:01 +000079 ResolveDefinitions(LateResolveValues);
80
Chris Lattner2079fde2001-10-13 06:41:08 +000081 // Check to make sure that all global value forward references have been
82 // resolved!
83 //
84 if (!GlobalRefs.empty()) {
Chris Lattner749ce032002-03-11 22:12:39 +000085 string UndefinedReferences = "Unresolved global references exist:\n";
86
87 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
88 I != E; ++I) {
89 UndefinedReferences += " " + I->first.first->getDescription() + " " +
90 I->first.second.getName() + "\n";
91 }
92 ThrowException(UndefinedReferences);
Chris Lattner2079fde2001-10-13 06:41:08 +000093 }
94
Chris Lattner00950542001-06-06 20:29:01 +000095 Values.clear(); // Clear out method local definitions
Chris Lattner30c89792001-09-07 16:35:17 +000096 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +000097 CurrentModule = 0;
98 }
Chris Lattner2079fde2001-10-13 06:41:08 +000099
100
101 // DeclareNewGlobalValue - Called every type a new GV has been defined. This
102 // is used to remove things from the forward declaration map, resolving them
103 // to the correct thing as needed.
104 //
105 void DeclareNewGlobalValue(GlobalValue *GV, ValID D) {
106 // Check to see if there is a forward reference to this global variable...
107 // if there is, eliminate it and patch the reference to use the new def'n.
108 GlobalRefsType::iterator I = GlobalRefs.find(make_pair(GV->getType(), D));
109
110 if (I != GlobalRefs.end()) {
111 GlobalVariable *OldGV = I->second; // Get the placeholder...
112 I->first.second.destroy(); // Free string memory if neccesary
113
114 // Loop over all of the uses of the GlobalValue. The only thing they are
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000115 // allowed to be at this point is ConstantPointerRef's.
Chris Lattner2079fde2001-10-13 06:41:08 +0000116 assert(OldGV->use_size() == 1 && "Only one reference should exist!");
117 while (!OldGV->use_empty()) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000118 User *U = OldGV->use_back(); // Must be a ConstantPointerRef...
119 ConstantPointerRef *CPPR = cast<ConstantPointerRef>(U);
Chris Lattner2079fde2001-10-13 06:41:08 +0000120 assert(CPPR->getValue() == OldGV && "Something isn't happy");
121
122 // Change the const pool reference to point to the real global variable
123 // now. This should drop a use from the OldGV.
124 CPPR->mutateReference(GV);
125 }
126
127 // Remove GV from the module...
128 CurrentModule->getGlobalList().remove(OldGV);
129 delete OldGV; // Delete the old placeholder
130
131 // Remove the map entry for the global now that it has been created...
132 GlobalRefs.erase(I);
133 }
134 }
135
Chris Lattner00950542001-06-06 20:29:01 +0000136} CurModule;
137
Chris Lattner79df7c02002-03-26 18:01:55 +0000138static struct PerFunctionInfo {
139 Function *CurrentFunction; // Pointer to current method being created
Chris Lattner00950542001-06-06 20:29:01 +0000140
Chris Lattnere1815642001-07-15 06:35:53 +0000141 vector<ValueList> Values; // Keep track of numbered definitions
Chris Lattner00950542001-06-06 20:29:01 +0000142 vector<ValueList> LateResolveValues;
Chris Lattner4a42e902001-10-22 05:56:09 +0000143 vector<PATypeHolder<Type> > Types;
144 map<ValID, PATypeHolder<Type> > LateResolveTypes;
Chris Lattnere1815642001-07-15 06:35:53 +0000145 bool isDeclare; // Is this method a forward declararation?
Chris Lattner00950542001-06-06 20:29:01 +0000146
Chris Lattner79df7c02002-03-26 18:01:55 +0000147 inline PerFunctionInfo() {
148 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000149 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000150 }
151
Chris Lattner79df7c02002-03-26 18:01:55 +0000152 inline ~PerFunctionInfo() {}
Chris Lattner00950542001-06-06 20:29:01 +0000153
Chris Lattner79df7c02002-03-26 18:01:55 +0000154 inline void FunctionStart(Function *M) {
155 CurrentFunction = M;
Chris Lattner00950542001-06-06 20:29:01 +0000156 }
157
Chris Lattner79df7c02002-03-26 18:01:55 +0000158 void FunctionDone() {
Chris Lattner00950542001-06-06 20:29:01 +0000159 // If we could not resolve some blocks at parsing time (forward branches)
160 // resolve the branches now...
Chris Lattner386a3b72001-10-16 19:54:17 +0000161 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000162
163 Values.clear(); // Clear out method local definitions
Chris Lattner30c89792001-09-07 16:35:17 +0000164 Types.clear();
Chris Lattner79df7c02002-03-26 18:01:55 +0000165 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000166 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000167 }
168} CurMeth; // Info for the current method...
169
Chris Lattner79df7c02002-03-26 18:01:55 +0000170static bool inFunctionScope() { return CurMeth.CurrentFunction != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000171
Chris Lattner00950542001-06-06 20:29:01 +0000172
173//===----------------------------------------------------------------------===//
174// Code to handle definitions of all the types
175//===----------------------------------------------------------------------===//
176
Chris Lattner2079fde2001-10-13 06:41:08 +0000177static int InsertValue(Value *D, vector<ValueList> &ValueTab = CurMeth.Values) {
178 if (D->hasName()) return -1; // Is this a numbered definition?
179
180 // Yes, insert the value into the value table...
181 unsigned type = D->getType()->getUniqueID();
182 if (ValueTab.size() <= type)
183 ValueTab.resize(type+1, ValueList());
184 //printf("Values[%d][%d] = %d\n", type, ValueTab[type].size(), D);
185 ValueTab[type].push_back(D);
186 return ValueTab[type].size()-1;
Chris Lattner00950542001-06-06 20:29:01 +0000187}
188
Chris Lattner30c89792001-09-07 16:35:17 +0000189// TODO: FIXME when Type are not const
190static void InsertType(const Type *Ty, vector<PATypeHolder<Type> > &Types) {
191 Types.push_back(Ty);
192}
193
194static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000195 switch (D.Type) {
196 case 0: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000197 unsigned Num = (unsigned)D.Num;
198
199 // Module constants occupy the lowest numbered slots...
200 if (Num < CurModule.Types.size())
201 return CurModule.Types[Num];
202
203 Num -= CurModule.Types.size();
204
205 // Check that the number is within bounds...
206 if (Num <= CurMeth.Types.size())
207 return CurMeth.Types[Num];
Chris Lattner42c9e772001-10-20 09:32:59 +0000208 break;
Chris Lattner30c89792001-09-07 16:35:17 +0000209 }
210 case 1: { // Is it a named definition?
211 string Name(D.Name);
212 SymbolTable *SymTab = 0;
Chris Lattner79df7c02002-03-26 18:01:55 +0000213 if (inFunctionScope()) SymTab = CurMeth.CurrentFunction->getSymbolTable();
Chris Lattner30c89792001-09-07 16:35:17 +0000214 Value *N = SymTab ? SymTab->lookup(Type::TypeTy, Name) : 0;
215
216 if (N == 0) {
217 // Symbol table doesn't automatically chain yet... because the method
218 // hasn't been added to the module...
219 //
220 SymTab = CurModule.CurrentModule->getSymbolTable();
221 if (SymTab)
222 N = SymTab->lookup(Type::TypeTy, Name);
223 if (N == 0) break;
224 }
225
226 D.destroy(); // Free old strdup'd memory...
Chris Lattnercfe26c92001-10-01 18:26:53 +0000227 return cast<const Type>(N);
Chris Lattner30c89792001-09-07 16:35:17 +0000228 }
229 default:
230 ThrowException("Invalid symbol type reference!");
231 }
232
233 // If we reached here, we referenced either a symbol that we don't know about
234 // or an id number that hasn't been read yet. We may be referencing something
235 // forward, so just create an entry to be resolved later and get to it...
236 //
237 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
238
Chris Lattner79df7c02002-03-26 18:01:55 +0000239 map<ValID, PATypeHolder<Type> > &LateResolver = inFunctionScope() ?
Chris Lattner4a42e902001-10-22 05:56:09 +0000240 CurMeth.LateResolveTypes : CurModule.LateResolveTypes;
241
242 map<ValID, PATypeHolder<Type> >::iterator I = LateResolver.find(D);
243 if (I != LateResolver.end()) {
244 return I->second;
245 }
Chris Lattner30c89792001-09-07 16:35:17 +0000246
Chris Lattner82269592001-10-22 06:01:08 +0000247 Type *Typ = OpaqueType::get();
Chris Lattner4a42e902001-10-22 05:56:09 +0000248 LateResolver.insert(make_pair(D, Typ));
Chris Lattner30c89792001-09-07 16:35:17 +0000249 return Typ;
250}
251
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000252static Value *lookupInSymbolTable(const Type *Ty, const string &Name) {
253 SymbolTable *SymTab =
Chris Lattner79df7c02002-03-26 18:01:55 +0000254 inFunctionScope() ? CurMeth.CurrentFunction->getSymbolTable() : 0;
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000255 Value *N = SymTab ? SymTab->lookup(Ty, Name) : 0;
256
257 if (N == 0) {
258 // Symbol table doesn't automatically chain yet... because the method
259 // hasn't been added to the module...
260 //
261 SymTab = CurModule.CurrentModule->getSymbolTable();
262 if (SymTab)
263 N = SymTab->lookup(Ty, Name);
264 }
265
266 return N;
267}
268
Chris Lattner2079fde2001-10-13 06:41:08 +0000269// getValNonImprovising - Look up the value specified by the provided type and
270// the provided ValID. If the value exists and has already been defined, return
271// it. Otherwise return null.
272//
273static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000274 if (isa<FunctionType>(Ty))
275 ThrowException("Functions are not values and "
276 "must be referenced as pointers");
Chris Lattner386a3b72001-10-16 19:54:17 +0000277
Chris Lattner30c89792001-09-07 16:35:17 +0000278 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000279 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000280 unsigned type = Ty->getUniqueID();
Chris Lattner00950542001-06-06 20:29:01 +0000281 unsigned Num = (unsigned)D.Num;
282
283 // Module constants occupy the lowest numbered slots...
284 if (type < CurModule.Values.size()) {
285 if (Num < CurModule.Values[type].size())
286 return CurModule.Values[type][Num];
287
288 Num -= CurModule.Values[type].size();
289 }
290
291 // Make sure that our type is within bounds
Chris Lattner2079fde2001-10-13 06:41:08 +0000292 if (CurMeth.Values.size() <= type) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000293
294 // Check that the number is within bounds...
Chris Lattner2079fde2001-10-13 06:41:08 +0000295 if (CurMeth.Values[type].size() <= Num) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000296
297 return CurMeth.Values[type][Num];
298 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000299
Chris Lattner1a1cb112001-09-30 22:46:54 +0000300 case ValID::NameVal: { // Is it a named definition?
Chris Lattner2079fde2001-10-13 06:41:08 +0000301 Value *N = lookupInSymbolTable(Ty, string(D.Name));
302 if (N == 0) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000303
304 D.destroy(); // Free old strdup'd memory...
305 return N;
306 }
307
Chris Lattner2079fde2001-10-13 06:41:08 +0000308 // Check to make sure that "Ty" is an integral type, and that our
309 // value will fit into the specified type...
310 case ValID::ConstSIntVal: // Is it a constant pool reference??
311 if (Ty == Type::BoolTy) { // Special handling for boolean data
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000312 return ConstantBool::get(D.ConstPool64 != 0);
Chris Lattner2079fde2001-10-13 06:41:08 +0000313 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000314 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64))
Chris Lattner2079fde2001-10-13 06:41:08 +0000315 ThrowException("Symbolic constant pool value '" +
316 itostr(D.ConstPool64) + "' is invalid for type '" +
Chris Lattner72e00252001-12-14 16:28:42 +0000317 Ty->getDescription() + "'!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000318 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner00950542001-06-06 20:29:01 +0000319 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000320
321 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000322 if (!ConstantUInt::isValueValidForType(Ty, D.UConstPool64)) {
323 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64)) {
Chris Lattner2079fde2001-10-13 06:41:08 +0000324 ThrowException("Integral constant pool reference is invalid!");
325 } else { // This is really a signed reference. Transmogrify.
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000326 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000327 }
328 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000329 return ConstantUInt::get(Ty, D.UConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000330 }
331
332 case ValID::ConstStringVal: // Is it a string const pool reference?
333 cerr << "FIXME: TODO: String constants [sbyte] not implemented yet!\n";
334 abort();
335 return 0;
336
337 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000338 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
Chris Lattner2079fde2001-10-13 06:41:08 +0000339 ThrowException("FP constant invalid for type!!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000340 return ConstantFP::get(Ty, D.ConstPoolFP);
Chris Lattner2079fde2001-10-13 06:41:08 +0000341
342 case ValID::ConstNullVal: // Is it a null value?
343 if (!Ty->isPointerType())
344 ThrowException("Cannot create a a non pointer null!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000345 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner2079fde2001-10-13 06:41:08 +0000346
Chris Lattner30c89792001-09-07 16:35:17 +0000347 default:
348 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000349 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000350 } // End of switch
351
Chris Lattner2079fde2001-10-13 06:41:08 +0000352 assert(0 && "Unhandled case!");
353 return 0;
354}
355
356
357// getVal - This function is identical to getValNonImprovising, except that if a
358// value is not already defined, it "improvises" by creating a placeholder var
359// that looks and acts just like the requested variable. When the value is
360// defined later, all uses of the placeholder variable are replaced with the
361// real thing.
362//
363static Value *getVal(const Type *Ty, const ValID &D) {
364 assert(Ty != Type::TypeTy && "Should use getTypeVal for types!");
365
366 // See if the value has already been defined...
367 Value *V = getValNonImprovising(Ty, D);
368 if (V) return V;
Chris Lattner00950542001-06-06 20:29:01 +0000369
370 // If we reached here, we referenced either a symbol that we don't know about
371 // or an id number that hasn't been read yet. We may be referencing something
372 // forward, so just create an entry to be resolved later and get to it...
373 //
Chris Lattner00950542001-06-06 20:29:01 +0000374 Value *d = 0;
Chris Lattner30c89792001-09-07 16:35:17 +0000375 switch (Ty->getPrimitiveID()) {
376 case Type::LabelTyID: d = new BBPlaceHolder(Ty, D); break;
Chris Lattner30c89792001-09-07 16:35:17 +0000377 default: d = new ValuePlaceHolder(Ty, D); break;
Chris Lattner00950542001-06-06 20:29:01 +0000378 }
379
380 assert(d != 0 && "How did we not make something?");
Chris Lattner79df7c02002-03-26 18:01:55 +0000381 if (inFunctionScope())
Chris Lattner386a3b72001-10-16 19:54:17 +0000382 InsertValue(d, CurMeth.LateResolveValues);
383 else
384 InsertValue(d, CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000385 return d;
386}
387
388
389//===----------------------------------------------------------------------===//
390// Code to handle forward references in instructions
391//===----------------------------------------------------------------------===//
392//
393// This code handles the late binding needed with statements that reference
394// values not defined yet... for example, a forward branch, or the PHI node for
395// a loop body.
396//
397// This keeps a table (CurMeth.LateResolveValues) of all such forward references
398// and back patchs after we are done.
399//
400
401// ResolveDefinitions - If we could not resolve some defs at parsing
402// time (forward branches, phi functions for loops, etc...) resolve the
403// defs now...
404//
Chris Lattner386a3b72001-10-16 19:54:17 +0000405static void ResolveDefinitions(vector<ValueList> &LateResolvers,
406 vector<ValueList> *FutureLateResolvers = 0) {
Chris Lattner00950542001-06-06 20:29:01 +0000407 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
408 for (unsigned ty = 0; ty < LateResolvers.size(); ty++) {
409 while (!LateResolvers[ty].empty()) {
410 Value *V = LateResolvers[ty].back();
Chris Lattner386a3b72001-10-16 19:54:17 +0000411 assert(!isa<Type>(V) && "Types should be in LateResolveTypes!");
412
Chris Lattner00950542001-06-06 20:29:01 +0000413 LateResolvers[ty].pop_back();
414 ValID &DID = getValIDFromPlaceHolder(V);
415
Chris Lattner2079fde2001-10-13 06:41:08 +0000416 Value *TheRealValue = getValNonImprovising(Type::getUniqueIDType(ty),DID);
Chris Lattner386a3b72001-10-16 19:54:17 +0000417 if (TheRealValue) {
418 V->replaceAllUsesWith(TheRealValue);
419 delete V;
420 } else if (FutureLateResolvers) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000421 // Functions have their unresolved items forwarded to the module late
Chris Lattner386a3b72001-10-16 19:54:17 +0000422 // resolver table
423 InsertValue(V, *FutureLateResolvers);
424 } else {
Chris Lattner30c89792001-09-07 16:35:17 +0000425 if (DID.Type == 1)
426 ThrowException("Reference to an invalid definition: '" +DID.getName()+
427 "' of type '" + V->getType()->getDescription() + "'",
428 getLineNumFromPlaceHolder(V));
429 else
430 ThrowException("Reference to an invalid definition: #" +
431 itostr(DID.Num) + " of type '" +
432 V->getType()->getDescription() + "'",
433 getLineNumFromPlaceHolder(V));
434 }
Chris Lattner00950542001-06-06 20:29:01 +0000435 }
436 }
437
438 LateResolvers.clear();
439}
440
Chris Lattner4a42e902001-10-22 05:56:09 +0000441// ResolveTypeTo - A brand new type was just declared. This means that (if
442// name is not null) things referencing Name can be resolved. Otherwise, things
443// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000444//
Chris Lattner4a42e902001-10-22 05:56:09 +0000445static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000446 vector<PATypeHolder<Type> > &Types = inFunctionScope() ?
Chris Lattner4a42e902001-10-22 05:56:09 +0000447 CurMeth.Types : CurModule.Types;
Chris Lattner00950542001-06-06 20:29:01 +0000448
Chris Lattner4a42e902001-10-22 05:56:09 +0000449 ValID D;
450 if (Name) D = ValID::create(Name);
451 else D = ValID::create((int)Types.size());
Chris Lattner30c89792001-09-07 16:35:17 +0000452
Chris Lattner79df7c02002-03-26 18:01:55 +0000453 map<ValID, PATypeHolder<Type> > &LateResolver = inFunctionScope() ?
Chris Lattner4a42e902001-10-22 05:56:09 +0000454 CurMeth.LateResolveTypes : CurModule.LateResolveTypes;
455
456 map<ValID, PATypeHolder<Type> >::iterator I = LateResolver.find(D);
457 if (I != LateResolver.end()) {
458 cast<DerivedType>(I->second.get())->refineAbstractTypeTo(ToTy);
459 LateResolver.erase(I);
460 }
461}
462
463// ResolveTypes - At this point, all types should be resolved. Any that aren't
464// are errors.
465//
466static void ResolveTypes(map<ValID, PATypeHolder<Type> > &LateResolveTypes) {
467 if (!LateResolveTypes.empty()) {
Chris Lattner82269592001-10-22 06:01:08 +0000468 const ValID &DID = LateResolveTypes.begin()->first;
Chris Lattner4a42e902001-10-22 05:56:09 +0000469
470 if (DID.Type == ValID::NameVal)
Chris Lattner82269592001-10-22 06:01:08 +0000471 ThrowException("Reference to an invalid type: '" +DID.getName() + "'");
Chris Lattner4a42e902001-10-22 05:56:09 +0000472 else
Chris Lattner82269592001-10-22 06:01:08 +0000473 ThrowException("Reference to an invalid type: #" + itostr(DID.Num));
Chris Lattner30c89792001-09-07 16:35:17 +0000474 }
475}
476
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000477
Chris Lattner1781aca2001-09-18 04:00:54 +0000478// setValueName - Set the specified value to the name given. The name may be
479// null potentially, in which case this is a noop. The string passed in is
480// assumed to be a malloc'd string buffer, and is freed by this function.
481//
Chris Lattnerb7474512001-10-03 15:39:04 +0000482// This function returns true if the value has already been defined, but is
483// allowed to be redefined in the specified context. If the name is a new name
484// for the typeplane, false is returned.
485//
486static bool setValueName(Value *V, char *NameStr) {
487 if (NameStr == 0) return false;
Chris Lattner386a3b72001-10-16 19:54:17 +0000488
Chris Lattner1781aca2001-09-18 04:00:54 +0000489 string Name(NameStr); // Copy string
490 free(NameStr); // Free old string
491
Chris Lattner2079fde2001-10-13 06:41:08 +0000492 if (V->getType() == Type::VoidTy)
493 ThrowException("Can't assign name '" + Name +
494 "' to a null valued instruction!");
495
Chris Lattner79df7c02002-03-26 18:01:55 +0000496 SymbolTable *ST = inFunctionScope() ?
497 CurMeth.CurrentFunction->getSymbolTableSure() :
Chris Lattner30c89792001-09-07 16:35:17 +0000498 CurModule.CurrentModule->getSymbolTableSure();
499
500 Value *Existing = ST->lookup(V->getType(), Name);
501 if (Existing) { // Inserting a name that is already defined???
502 // There is only one case where this is allowed: when we are refining an
503 // opaque type. In this case, Existing will be an opaque type.
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000504 if (const Type *Ty = dyn_cast<const Type>(Existing)) {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000505 if (OpaqueType *OpTy = dyn_cast<OpaqueType>(Ty)) {
Chris Lattner30c89792001-09-07 16:35:17 +0000506 // We ARE replacing an opaque type!
Chris Lattnerb00c5822001-10-02 03:41:24 +0000507 OpTy->refineAbstractTypeTo(cast<Type>(V));
Chris Lattnerb7474512001-10-03 15:39:04 +0000508 return true;
Chris Lattner30c89792001-09-07 16:35:17 +0000509 }
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000510 }
Chris Lattner30c89792001-09-07 16:35:17 +0000511
Chris Lattner9636a912001-10-01 16:18:37 +0000512 // Otherwise, we are a simple redefinition of a value, check to see if it
513 // is defined the same as the old one...
514 if (const Type *Ty = dyn_cast<const Type>(Existing)) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000515 if (Ty == cast<const Type>(V)) return true; // Yes, it's equal.
516 // cerr << "Type: " << Ty->getDescription() << " != "
517 // << cast<const Type>(V)->getDescription() << "!\n";
518 } else if (GlobalVariable *EGV = dyn_cast<GlobalVariable>(Existing)) {
Chris Lattner43efcbf2001-10-03 19:35:57 +0000519 // We are allowed to redefine a global variable in two circumstances:
520 // 1. If at least one of the globals is uninitialized or
521 // 2. If both initializers have the same value.
522 //
523 // This can only be done if the const'ness of the vars is the same.
524 //
Chris Lattner89219832001-10-03 19:35:04 +0000525 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
526 if (EGV->isConstant() == GV->isConstant() &&
527 (!EGV->hasInitializer() || !GV->hasInitializer() ||
528 EGV->getInitializer() == GV->getInitializer())) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000529
Chris Lattner89219832001-10-03 19:35:04 +0000530 // Make sure the existing global version gets the initializer!
531 if (GV->hasInitializer() && !EGV->hasInitializer())
532 EGV->setInitializer(GV->getInitializer());
533
Chris Lattner2079fde2001-10-13 06:41:08 +0000534 delete GV; // Destroy the duplicate!
Chris Lattner89219832001-10-03 19:35:04 +0000535 return true; // They are equivalent!
536 }
Chris Lattnerb7474512001-10-03 15:39:04 +0000537 }
Chris Lattner9636a912001-10-01 16:18:37 +0000538 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000539 ThrowException("Redefinition of value named '" + Name + "' in the '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000540 V->getType()->getDescription() + "' type plane!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000541 }
Chris Lattner00950542001-06-06 20:29:01 +0000542
Chris Lattner30c89792001-09-07 16:35:17 +0000543 V->setName(Name, ST);
Chris Lattnerb7474512001-10-03 15:39:04 +0000544 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000545}
546
Chris Lattner8896eda2001-07-09 19:38:36 +0000547
Chris Lattner30c89792001-09-07 16:35:17 +0000548//===----------------------------------------------------------------------===//
549// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000550//
Chris Lattner8896eda2001-07-09 19:38:36 +0000551
Chris Lattner30c89792001-09-07 16:35:17 +0000552// TypeContains - Returns true if Ty contains E in it.
553//
554static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattner3ff43872001-09-28 22:56:31 +0000555 return find(df_begin(Ty), df_end(Ty), E) != df_end(Ty);
Chris Lattner30c89792001-09-07 16:35:17 +0000556}
Chris Lattner698b56e2001-07-20 19:15:08 +0000557
Chris Lattner30c89792001-09-07 16:35:17 +0000558
559static vector<pair<unsigned, OpaqueType *> > UpRefs;
560
561static PATypeHolder<Type> HandleUpRefs(const Type *ty) {
562 PATypeHolder<Type> Ty(ty);
Chris Lattner5084d032001-11-02 07:46:26 +0000563 UR_OUT("Type '" << ty->getDescription() <<
564 "' newly formed. Resolving upreferences.\n" <<
565 UpRefs.size() << " upreferences active!\n");
Chris Lattner30c89792001-09-07 16:35:17 +0000566 for (unsigned i = 0; i < UpRefs.size(); ) {
Chris Lattner5084d032001-11-02 07:46:26 +0000567 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
Chris Lattner30c89792001-09-07 16:35:17 +0000568 << UpRefs[i].second->getDescription() << ") = "
Chris Lattner5084d032001-11-02 07:46:26 +0000569 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << endl);
Chris Lattner30c89792001-09-07 16:35:17 +0000570 if (TypeContains(Ty, UpRefs[i].second)) {
571 unsigned Level = --UpRefs[i].first; // Decrement level of upreference
Chris Lattner5084d032001-11-02 07:46:26 +0000572 UR_OUT(" Uplevel Ref Level = " << Level << endl);
Chris Lattner30c89792001-09-07 16:35:17 +0000573 if (Level == 0) { // Upreference should be resolved!
Chris Lattner5084d032001-11-02 07:46:26 +0000574 UR_OUT(" * Resolving upreference for "
575 << UpRefs[i].second->getDescription() << endl;
Chris Lattner30c89792001-09-07 16:35:17 +0000576 string OldName = UpRefs[i].second->getDescription());
577 UpRefs[i].second->refineAbstractTypeTo(Ty);
578 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
Chris Lattner5084d032001-11-02 07:46:26 +0000579 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
Chris Lattner30c89792001-09-07 16:35:17 +0000580 << (const void*)Ty << ", " << Ty->getDescription() << endl);
581 continue;
582 }
583 }
584
585 ++i; // Otherwise, no resolve, move on...
Chris Lattner8896eda2001-07-09 19:38:36 +0000586 }
Chris Lattner30c89792001-09-07 16:35:17 +0000587 // FIXME: TODO: this should return the updated type
Chris Lattner8896eda2001-07-09 19:38:36 +0000588 return Ty;
589}
590
Chris Lattner30c89792001-09-07 16:35:17 +0000591template <class TypeTy>
592inline static void TypeDone(PATypeHolder<TypeTy> *Ty) {
593 if (UpRefs.size())
594 ThrowException("Invalid upreference in type: " + (*Ty)->getDescription());
595}
596
597// newTH - Allocate a new type holder for the specified type
598template <class TypeTy>
599inline static PATypeHolder<TypeTy> *newTH(const TypeTy *Ty) {
600 return new PATypeHolder<TypeTy>(Ty);
601}
602template <class TypeTy>
603inline static PATypeHolder<TypeTy> *newTH(const PATypeHolder<TypeTy> &TH) {
604 return new PATypeHolder<TypeTy>(TH);
605}
606
607
Chris Lattner00950542001-06-06 20:29:01 +0000608//===----------------------------------------------------------------------===//
609// RunVMAsmParser - Define an interface to this parser
610//===----------------------------------------------------------------------===//
611//
Chris Lattnera2850432001-07-22 18:36:00 +0000612Module *RunVMAsmParser(const string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000613 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000614 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000615 llvmAsmlineno = 1; // Reset the current line number...
616
617 CurModule.CurrentModule = new Module(); // Allocate a new module to read
618 yyparse(); // Parse the file.
619 Module *Result = ParserResult;
Chris Lattner00950542001-06-06 20:29:01 +0000620 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
621 ParserResult = 0;
622
623 return Result;
624}
625
626%}
627
628%union {
Chris Lattner30c89792001-09-07 16:35:17 +0000629 Module *ModuleVal;
Chris Lattner79df7c02002-03-26 18:01:55 +0000630 Function *FunctionVal;
631 std::pair<FunctionArgument*,char*> *MethArgVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000632 BasicBlock *BasicBlockVal;
633 TerminatorInst *TermInstVal;
634 Instruction *InstVal;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000635 Constant *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000636
Chris Lattner30c89792001-09-07 16:35:17 +0000637 const Type *PrimType;
638 PATypeHolder<Type> *TypeVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000639 Value *ValueVal;
640
Chris Lattner79df7c02002-03-26 18:01:55 +0000641 std::list<std::pair<FunctionArgument*,char*> > *FunctionArgList;
Chris Lattner697954c2002-01-20 22:54:45 +0000642 std::vector<Value*> *ValueList;
643 std::list<PATypeHolder<Type> > *TypeList;
644 std::list<std::pair<Value*,
645 BasicBlock*> > *PHIList; // Represent the RHS of PHI node
646 std::list<std::pair<Constant*, BasicBlock*> > *JumpTable;
647 std::vector<Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000648
Chris Lattner30c89792001-09-07 16:35:17 +0000649 int64_t SInt64Val;
650 uint64_t UInt64Val;
651 int SIntVal;
652 unsigned UIntVal;
653 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000654 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000655
Chris Lattner30c89792001-09-07 16:35:17 +0000656 char *StrVal; // This memory is strdup'd!
657 ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000658
Chris Lattner30c89792001-09-07 16:35:17 +0000659 Instruction::UnaryOps UnaryOpVal;
660 Instruction::BinaryOps BinaryOpVal;
661 Instruction::TermOps TermOpVal;
662 Instruction::MemoryOps MemOpVal;
663 Instruction::OtherOps OtherOpVal;
Chris Lattner00950542001-06-06 20:29:01 +0000664}
665
Chris Lattner79df7c02002-03-26 18:01:55 +0000666%type <ModuleVal> Module FunctionList
667%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000668%type <BasicBlockVal> BasicBlock InstructionList
669%type <TermInstVal> BBTerminatorInst
670%type <InstVal> Inst InstVal MemoryInst
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000671%type <ConstVal> ConstVal
Chris Lattner6cdb0112001-11-26 16:54:11 +0000672%type <ConstVector> ConstVector
Chris Lattner79df7c02002-03-26 18:01:55 +0000673%type <FunctionArgList> ArgList ArgListH
Chris Lattner00950542001-06-06 20:29:01 +0000674%type <MethArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000675%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000676%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +0000677%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +0000678%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000679%type <JumpTable> JumpTable
Chris Lattnerdda71962001-11-26 18:54:16 +0000680%type <BoolVal> GlobalType OptInternal // GLOBAL or CONSTANT? Intern?
Chris Lattner00950542001-06-06 20:29:01 +0000681
Chris Lattner2079fde2001-10-13 06:41:08 +0000682// ValueRef - Unresolved reference to a definition or BB
683%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000684%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000685// Tokens and types for handling constant integer values
686//
687// ESINT64VAL - A negative number within long long range
688%token <SInt64Val> ESINT64VAL
689
690// EUINT64VAL - A positive number within uns. long long range
691%token <UInt64Val> EUINT64VAL
692%type <SInt64Val> EINT64VAL
693
694%token <SIntVal> SINTVAL // Signed 32 bit ints...
695%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
696%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000697%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000698
699// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000700%type <TypeVal> Types TypesV UpRTypes UpRTypesV
701%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
702%token <TypeVal> OPAQUE
703%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
704%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000705
706%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
707%type <StrVal> OptVAR_ID OptAssign
708
709
Chris Lattner1781aca2001-09-18 04:00:54 +0000710%token IMPLEMENTATION TRUE FALSE BEGINTOK END DECLARE GLOBAL CONSTANT UNINIT
Chris Lattnerdda71962001-11-26 18:54:16 +0000711%token TO EXCEPT DOTDOTDOT STRING NULL_TOK CONST INTERNAL
Chris Lattner00950542001-06-06 20:29:01 +0000712
713// Basic Block Terminating Operators
714%token <TermOpVal> RET BR SWITCH
715
716// Unary Operators
717%type <UnaryOpVal> UnaryOps // all the unary operators
Chris Lattner71496b32001-07-08 19:03:27 +0000718%token <UnaryOpVal> NOT
Chris Lattner00950542001-06-06 20:29:01 +0000719
720// Binary Operators
721%type <BinaryOpVal> BinaryOps // all the binary operators
Chris Lattner42c9e772001-10-20 09:32:59 +0000722%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +0000723%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000724
725// Memory Instructions
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000726%token <MemoryOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000727
Chris Lattner027dcc52001-07-08 21:10:27 +0000728// Other Operators
729%type <OtherOpVal> ShiftOps
Chris Lattner2079fde2001-10-13 06:41:08 +0000730%token <OtherOpVal> PHI CALL INVOKE CAST SHL SHR
Chris Lattner027dcc52001-07-08 21:10:27 +0000731
Chris Lattner00950542001-06-06 20:29:01 +0000732%start Module
733%%
734
735// Handle constant integer size restriction and conversion...
736//
737
738INTVAL : SINTVAL
739INTVAL : UINTVAL {
740 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
741 ThrowException("Value too large for type!");
742 $$ = (int32_t)$1;
743}
744
745
746EINT64VAL : ESINT64VAL // These have same type and can't cause problems...
747EINT64VAL : EUINT64VAL {
748 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
749 ThrowException("Value too large for type!");
750 $$ = (int64_t)$1;
751}
752
Chris Lattner00950542001-06-06 20:29:01 +0000753// Operations that are notably excluded from this list include:
754// RET, BR, & SWITCH because they end basic blocks and are treated specially.
755//
Chris Lattner09083092001-07-08 04:57:15 +0000756UnaryOps : NOT
Chris Lattner42c9e772001-10-20 09:32:59 +0000757BinaryOps : ADD | SUB | MUL | DIV | REM | AND | OR | XOR
Chris Lattner00950542001-06-06 20:29:01 +0000758BinaryOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE
Chris Lattner027dcc52001-07-08 21:10:27 +0000759ShiftOps : SHL | SHR
Chris Lattner00950542001-06-06 20:29:01 +0000760
Chris Lattnere98dda62001-07-14 06:10:16 +0000761// These are some types that allow classification if we only want a particular
762// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner00950542001-06-06 20:29:01 +0000763SIntType : LONG | INT | SHORT | SBYTE
764UIntType : ULONG | UINT | USHORT | UBYTE
Chris Lattner30c89792001-09-07 16:35:17 +0000765IntType : SIntType | UIntType
766FPType : FLOAT | DOUBLE
Chris Lattner00950542001-06-06 20:29:01 +0000767
Chris Lattnere98dda62001-07-14 06:10:16 +0000768// OptAssign - Value producing statements have an optional assignment component
Chris Lattner00950542001-06-06 20:29:01 +0000769OptAssign : VAR_ID '=' {
770 $$ = $1;
771 }
772 | /*empty*/ {
773 $$ = 0;
774 }
775
Chris Lattnerdda71962001-11-26 18:54:16 +0000776OptInternal : INTERNAL { $$ = true; } | /*empty*/ { $$ = false; }
Chris Lattner30c89792001-09-07 16:35:17 +0000777
778//===----------------------------------------------------------------------===//
779// Types includes all predefined types... except void, because it can only be
780// used in specific contexts (method returning void for example). To have
781// access to it, a user must explicitly use TypesV.
782//
783
784// TypesV includes all of 'Types', but it also includes the void type.
785TypesV : Types | VOID { $$ = newTH($1); }
786UpRTypesV : UpRTypes | VOID { $$ = newTH($1); }
787
788Types : UpRTypes {
789 TypeDone($$ = $1);
790 }
791
792
793// Derived types are added later...
794//
795PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT
796PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL
797UpRTypes : OPAQUE | PrimType { $$ = newTH($1); }
798UpRTypes : ValueRef { // Named types are also simple types...
799 $$ = newTH(getTypeVal($1));
800}
801
Chris Lattner30c89792001-09-07 16:35:17 +0000802// Include derived types in the Types production.
803//
804UpRTypes : '\\' EUINT64VAL { // Type UpReference
805 if ($2 > (uint64_t)INT64_MAX) ThrowException("Value out of range!");
806 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
807 UpRefs.push_back(make_pair((unsigned)$2, OT)); // Add to vector...
808 $$ = newTH<Type>(OT);
809 UR_OUT("New Upreference!\n");
810 }
Chris Lattner79df7c02002-03-26 18:01:55 +0000811 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner30c89792001-09-07 16:35:17 +0000812 vector<const Type*> Params;
Chris Lattner697954c2002-01-20 22:54:45 +0000813 mapto($3->begin(), $3->end(), std::back_inserter(Params),
814 std::mem_fun_ref(&PATypeHandle<Type>::get));
Chris Lattner2079fde2001-10-13 06:41:08 +0000815 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
816 if (isVarArg) Params.pop_back();
817
Chris Lattner79df7c02002-03-26 18:01:55 +0000818 $$ = newTH(HandleUpRefs(FunctionType::get(*$1, Params, isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +0000819 delete $3; // Delete the argument list
820 delete $1; // Delete the old type handle
821 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000822 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner72e00252001-12-14 16:28:42 +0000823 $$ = newTH<Type>(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000824 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +0000825 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000826 | '{' TypeListI '}' { // Structure type?
827 vector<const Type*> Elements;
Chris Lattner697954c2002-01-20 22:54:45 +0000828 mapto($2->begin(), $2->end(), std::back_inserter(Elements),
829 std::mem_fun_ref(&PATypeHandle<Type>::get));
Chris Lattner30c89792001-09-07 16:35:17 +0000830
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000831 $$ = newTH<Type>(HandleUpRefs(StructType::get(Elements)));
832 delete $2;
833 }
834 | '{' '}' { // Empty structure type?
835 $$ = newTH<Type>(StructType::get(vector<const Type*>()));
836 }
837 | UpRTypes '*' { // Pointer type?
838 $$ = newTH<Type>(HandleUpRefs(PointerType::get(*$1)));
839 delete $1;
840 }
Chris Lattner30c89792001-09-07 16:35:17 +0000841
842// TypeList - Used for struct declarations and as a basis for method type
843// declaration type lists
844//
845TypeListI : UpRTypes {
846 $$ = new list<PATypeHolder<Type> >();
847 $$->push_back(*$1); delete $1;
848 }
849 | TypeListI ',' UpRTypes {
850 ($$=$1)->push_back(*$3); delete $3;
851 }
852
853// ArgTypeList - List of types for a method type declaration...
854ArgTypeListI : TypeListI
855 | TypeListI ',' DOTDOTDOT {
856 ($$=$1)->push_back(Type::VoidTy);
857 }
858 | DOTDOTDOT {
859 ($$ = new list<PATypeHolder<Type> >())->push_back(Type::VoidTy);
860 }
861 | /*empty*/ {
862 $$ = new list<PATypeHolder<Type> >();
863 }
864
865
Chris Lattnere98dda62001-07-14 06:10:16 +0000866// ConstVal - The various declarations that go into the constant pool. This
867// includes all forward declarations of types, constants, and functions.
868//
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000869ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
870 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
871 if (ATy == 0)
872 ThrowException("Cannot make array constant with type: '" +
873 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +0000874 const Type *ETy = ATy->getElementType();
875 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +0000876
Chris Lattner30c89792001-09-07 16:35:17 +0000877 // Verify that we have the correct size...
878 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +0000879 ThrowException("Type mismatch: constant sized array initialized with " +
Chris Lattner30c89792001-09-07 16:35:17 +0000880 utostr($3->size()) + " arguments, but has size of " +
881 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +0000882
Chris Lattner30c89792001-09-07 16:35:17 +0000883 // Verify all elements are correct type!
884 for (unsigned i = 0; i < $3->size(); i++) {
885 if (ETy != (*$3)[i]->getType())
Chris Lattner00950542001-06-06 20:29:01 +0000886 ThrowException("Element #" + utostr(i) + " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +0000887 ETy->getDescription() +"' as required!\nIt is of type '"+
888 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +0000889 }
890
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000891 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +0000892 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000893 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000894 | Types '[' ']' {
895 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
896 if (ATy == 0)
897 ThrowException("Cannot make array constant with type: '" +
898 (*$1)->getDescription() + "'!");
899
900 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +0000901 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +0000902 ThrowException("Type mismatch: constant sized array initialized with 0"
Chris Lattner30c89792001-09-07 16:35:17 +0000903 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000904 $$ = ConstantArray::get(ATy, vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +0000905 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +0000906 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000907 | Types 'c' STRINGCONSTANT {
908 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
909 if (ATy == 0)
910 ThrowException("Cannot make array constant with type: '" +
911 (*$1)->getDescription() + "'!");
912
Chris Lattner30c89792001-09-07 16:35:17 +0000913 int NumElements = ATy->getNumElements();
914 const Type *ETy = ATy->getElementType();
915 char *EndStr = UnEscapeLexed($3, true);
916 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +0000917 ThrowException("Can't build string constant of size " +
Chris Lattner30c89792001-09-07 16:35:17 +0000918 itostr((int)(EndStr-$3)) +
919 " when array has size " + itostr(NumElements) + "!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000920 vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +0000921 if (ETy == Type::SByteTy) {
922 for (char *C = $3; C != EndStr; ++C)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000923 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +0000924 } else if (ETy == Type::UByteTy) {
925 for (char *C = $3; C != EndStr; ++C)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000926 Vals.push_back(ConstantUInt::get(ETy, *C));
Chris Lattner93750fa2001-07-28 17:48:55 +0000927 } else {
Chris Lattner30c89792001-09-07 16:35:17 +0000928 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +0000929 ThrowException("Cannot build string arrays of non byte sized elements!");
930 }
Chris Lattner30c89792001-09-07 16:35:17 +0000931 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000932 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +0000933 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +0000934 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000935 | Types '{' ConstVector '}' {
936 const StructType *STy = dyn_cast<const StructType>($1->get());
937 if (STy == 0)
938 ThrowException("Cannot make struct constant with type: '" +
939 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +0000940 // FIXME: TODO: Check to see that the constants are compatible with the type
941 // initializer!
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000942 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +0000943 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000944 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000945 | Types NULL_TOK {
946 const PointerType *PTy = dyn_cast<const PointerType>($1->get());
947 if (PTy == 0)
948 ThrowException("Cannot make null pointer constant with type: '" +
949 (*$1)->getDescription() + "'!");
950
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000951 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000952 delete $1;
953 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000954 | Types SymbolicValueRef {
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000955 const PointerType *Ty = dyn_cast<const PointerType>($1->get());
956 if (Ty == 0)
957 ThrowException("Global const reference must be a pointer type!");
958
Chris Lattner2079fde2001-10-13 06:41:08 +0000959 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000960
Chris Lattner2079fde2001-10-13 06:41:08 +0000961 // If this is an initializer for a constant pointer, which is referencing a
962 // (currently) undefined variable, create a stub now that shall be replaced
963 // in the future with the right type of variable.
964 //
965 if (V == 0) {
966 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
967 const PointerType *PT = cast<PointerType>(Ty);
968
969 // First check to see if the forward references value is already created!
970 PerModuleInfo::GlobalRefsType::iterator I =
971 CurModule.GlobalRefs.find(make_pair(PT, $2));
972
973 if (I != CurModule.GlobalRefs.end()) {
974 V = I->second; // Placeholder already exists, use it...
975 } else {
976 // TODO: Include line number info by creating a subclass of
977 // TODO: GlobalVariable here that includes the said information!
978
979 // Create a placeholder for the global variable reference...
Chris Lattner7a176752001-12-04 00:03:30 +0000980 GlobalVariable *GV = new GlobalVariable(PT->getElementType(),
981 false, true);
Chris Lattner2079fde2001-10-13 06:41:08 +0000982 // Keep track of the fact that we have a forward ref to recycle it
983 CurModule.GlobalRefs.insert(make_pair(make_pair(PT, $2), GV));
984
985 // Must temporarily push this value into the module table...
986 CurModule.CurrentModule->getGlobalList().push_back(GV);
987 V = GV;
988 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000989 }
990
Chris Lattner2079fde2001-10-13 06:41:08 +0000991 GlobalValue *GV = cast<GlobalValue>(V);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000992 $$ = ConstantPointerRef::get(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +0000993 delete $1; // Free the type handle
Chris Lattner00950542001-06-06 20:29:01 +0000994 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000995
Chris Lattner00950542001-06-06 20:29:01 +0000996
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000997ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000998 if (!ConstantSInt::isValueValidForType($1, $2))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000999 ThrowException("Constant value doesn't fit in type!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001000 $$ = ConstantSInt::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001001 }
1002 | UIntType EUINT64VAL { // integral constants
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001003 if (!ConstantUInt::isValueValidForType($1, $2))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001004 ThrowException("Constant value doesn't fit in type!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001005 $$ = ConstantUInt::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001006 }
1007 | BOOL TRUE { // Boolean constants
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001008 $$ = ConstantBool::True;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001009 }
1010 | BOOL FALSE { // Boolean constants
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001011 $$ = ConstantBool::False;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001012 }
1013 | FPType FPVAL { // Float & Double constants
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001014 $$ = ConstantFP::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001015 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001016
Chris Lattnere98dda62001-07-14 06:10:16 +00001017// ConstVector - A list of comma seperated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001018ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001019 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001020 }
1021 | ConstVal {
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001022 $$ = new vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001023 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001024 }
1025
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001026
Chris Lattner1781aca2001-09-18 04:00:54 +00001027// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
1028GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; }
1029
Chris Lattner00950542001-06-06 20:29:01 +00001030
Chris Lattnere98dda62001-07-14 06:10:16 +00001031// ConstPool - Constants with optional names assigned to them.
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001032ConstPool : ConstPool OptAssign CONST ConstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001033 if (setValueName($4, $2)) { assert(0 && "No redefinitions allowed!"); }
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001034 InsertValue($4);
Chris Lattner00950542001-06-06 20:29:01 +00001035 }
Chris Lattner30c89792001-09-07 16:35:17 +00001036 | ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattner4a42e902001-10-22 05:56:09 +00001037 // Eagerly resolve types. This is not an optimization, this is a
1038 // requirement that is due to the fact that we could have this:
1039 //
1040 // %list = type { %list * }
1041 // %list = type { %list * } ; repeated type decl
1042 //
1043 // If types are not resolved eagerly, then the two types will not be
1044 // determined to be the same type!
1045 //
1046 ResolveTypeTo($2, $4->get());
1047
Chris Lattner1781aca2001-09-18 04:00:54 +00001048 // TODO: FIXME when Type are not const
Chris Lattnerb7474512001-10-03 15:39:04 +00001049 if (!setValueName(const_cast<Type*>($4->get()), $2)) {
1050 // If this is not a redefinition of a type...
1051 if (!$2) {
1052 InsertType($4->get(),
Chris Lattner79df7c02002-03-26 18:01:55 +00001053 inFunctionScope() ? CurMeth.Types : CurModule.Types);
Chris Lattnerb7474512001-10-03 15:39:04 +00001054 }
Chris Lattner30c89792001-09-07 16:35:17 +00001055 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001056
1057 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001058 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001059 | ConstPool FunctionProto { // Function prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001060 }
Chris Lattnerdda71962001-11-26 18:54:16 +00001061 | ConstPool OptAssign OptInternal GlobalType ConstVal {
1062 const Type *Ty = $5->getType();
Chris Lattner1781aca2001-09-18 04:00:54 +00001063 // Global declarations appear in Constant Pool
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001064 Constant *Initializer = $5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001065 if (Initializer == 0)
1066 ThrowException("Global value initializer is not a constant!");
1067
Chris Lattnerdda71962001-11-26 18:54:16 +00001068 GlobalVariable *GV = new GlobalVariable(Ty, $4, $3, Initializer);
Chris Lattnerb7474512001-10-03 15:39:04 +00001069 if (!setValueName(GV, $2)) { // If not redefining...
1070 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001071 int Slot = InsertValue(GV, CurModule.Values);
1072
1073 if (Slot != -1) {
1074 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1075 } else {
1076 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1077 (char*)GV->getName().c_str()));
1078 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001079 }
Chris Lattner1781aca2001-09-18 04:00:54 +00001080 }
Chris Lattnerdda71962001-11-26 18:54:16 +00001081 | ConstPool OptAssign OptInternal UNINIT GlobalType Types {
1082 const Type *Ty = *$6;
Chris Lattner1781aca2001-09-18 04:00:54 +00001083 // Global declarations appear in Constant Pool
Chris Lattnerdda71962001-11-26 18:54:16 +00001084 GlobalVariable *GV = new GlobalVariable(Ty, $5, $3);
Chris Lattnerb7474512001-10-03 15:39:04 +00001085 if (!setValueName(GV, $2)) { // If not redefining...
1086 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001087 int Slot = InsertValue(GV, CurModule.Values);
1088
1089 if (Slot != -1) {
1090 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1091 } else {
1092 assert(GV->hasName() && "Not named and not numbered!?");
1093 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1094 (char*)GV->getName().c_str()));
1095 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001096 }
Chris Lattner09c07532002-03-31 07:16:49 +00001097 delete $6;
Chris Lattnere98dda62001-07-14 06:10:16 +00001098 }
Chris Lattner00950542001-06-06 20:29:01 +00001099 | /* empty: end of list */ {
1100 }
1101
1102
1103//===----------------------------------------------------------------------===//
1104// Rules to match Modules
1105//===----------------------------------------------------------------------===//
1106
1107// Module rule: Capture the result of parsing the whole file into a result
1108// variable...
1109//
Chris Lattner79df7c02002-03-26 18:01:55 +00001110Module : FunctionList {
Chris Lattner00950542001-06-06 20:29:01 +00001111 $$ = ParserResult = $1;
1112 CurModule.ModuleDone();
1113}
1114
Chris Lattner79df7c02002-03-26 18:01:55 +00001115// FunctionList - A list of methods, preceeded by a constant pool.
Chris Lattnere98dda62001-07-14 06:10:16 +00001116//
Chris Lattner79df7c02002-03-26 18:01:55 +00001117FunctionList : FunctionList Function {
Chris Lattner00950542001-06-06 20:29:01 +00001118 $$ = $1;
Chris Lattner79df7c02002-03-26 18:01:55 +00001119 assert($2->getParent() == 0 && "Function already in module!");
1120 $1->getFunctionList().push_back($2);
1121 CurMeth.FunctionDone();
Chris Lattner00950542001-06-06 20:29:01 +00001122 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001123 | FunctionList FunctionProto {
Chris Lattnere1815642001-07-15 06:35:53 +00001124 $$ = $1;
Chris Lattnere1815642001-07-15 06:35:53 +00001125 }
Chris Lattner00950542001-06-06 20:29:01 +00001126 | ConstPool IMPLEMENTATION {
1127 $$ = CurModule.CurrentModule;
Chris Lattner30c89792001-09-07 16:35:17 +00001128 // Resolve circular types before we parse the body of the module
1129 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner00950542001-06-06 20:29:01 +00001130 }
1131
1132
1133//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00001134// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00001135//===----------------------------------------------------------------------===//
1136
1137OptVAR_ID : VAR_ID | /*empty*/ { $$ = 0; }
1138
1139ArgVal : Types OptVAR_ID {
Chris Lattner79df7c02002-03-26 18:01:55 +00001140 $$ = new pair<FunctionArgument*,char*>(new FunctionArgument(*$1), $2);
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001141 delete $1; // Delete the type handle..
Chris Lattner00950542001-06-06 20:29:01 +00001142}
1143
1144ArgListH : ArgVal ',' ArgListH {
1145 $$ = $3;
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001146 $3->push_front(*$1);
1147 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001148 }
1149 | ArgVal {
Chris Lattner79df7c02002-03-26 18:01:55 +00001150 $$ = new list<pair<FunctionArgument*,char*> >();
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001151 $$->push_front(*$1);
1152 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001153 }
Chris Lattner8b81bf52001-07-25 22:47:46 +00001154 | DOTDOTDOT {
Chris Lattner79df7c02002-03-26 18:01:55 +00001155 $$ = new list<pair<FunctionArgument*, char*> >();
1156 $$->push_front(pair<FunctionArgument*,char*>(
1157 new FunctionArgument(Type::VoidTy), 0));
Chris Lattner8b81bf52001-07-25 22:47:46 +00001158 }
Chris Lattner00950542001-06-06 20:29:01 +00001159
1160ArgList : ArgListH {
1161 $$ = $1;
1162 }
1163 | /* empty */ {
1164 $$ = 0;
1165 }
1166
Chris Lattner79df7c02002-03-26 18:01:55 +00001167FunctionHeaderH : OptInternal TypesV STRINGCONSTANT '(' ArgList ')' {
Chris Lattnerdda71962001-11-26 18:54:16 +00001168 UnEscapeLexed($3);
Chris Lattner79df7c02002-03-26 18:01:55 +00001169 string FunctionName($3);
Chris Lattnerdda71962001-11-26 18:54:16 +00001170
Chris Lattner30c89792001-09-07 16:35:17 +00001171 vector<const Type*> ParamTypeList;
Chris Lattnerdda71962001-11-26 18:54:16 +00001172 if ($5)
Chris Lattner79df7c02002-03-26 18:01:55 +00001173 for (list<pair<FunctionArgument*,char*> >::iterator I = $5->begin();
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001174 I != $5->end(); ++I)
1175 ParamTypeList.push_back(I->first->getType());
Chris Lattner00950542001-06-06 20:29:01 +00001176
Chris Lattner2079fde2001-10-13 06:41:08 +00001177 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1178 if (isVarArg) ParamTypeList.pop_back();
1179
Chris Lattner79df7c02002-03-26 18:01:55 +00001180 const FunctionType *MT = FunctionType::get(*$2, ParamTypeList, isVarArg);
Chris Lattneref9c23f2001-10-03 14:53:21 +00001181 const PointerType *PMT = PointerType::get(MT);
Chris Lattnerdda71962001-11-26 18:54:16 +00001182 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001183
Chris Lattner79df7c02002-03-26 18:01:55 +00001184 Function *M = 0;
Chris Lattnere1815642001-07-15 06:35:53 +00001185 if (SymbolTable *ST = CurModule.CurrentModule->getSymbolTable()) {
Chris Lattner79df7c02002-03-26 18:01:55 +00001186 // Is the function already in symtab?
1187 if (Value *V = ST->lookup(PMT, FunctionName)) {
1188 M = cast<Function>(V);
Chris Lattner00950542001-06-06 20:29:01 +00001189
Chris Lattnere1815642001-07-15 06:35:53 +00001190 // Yes it is. If this is the case, either we need to be a forward decl,
1191 // or it needs to be.
1192 if (!CurMeth.isDeclare && !M->isExternal())
Chris Lattner79df7c02002-03-26 18:01:55 +00001193 ThrowException("Redefinition of method '" + FunctionName + "'!");
Chris Lattner34538142002-03-08 19:11:42 +00001194
1195 // If we found a preexisting method prototype, remove it from the module,
1196 // so that we don't get spurious conflicts with global & local variables.
1197 //
Chris Lattner79df7c02002-03-26 18:01:55 +00001198 CurModule.CurrentModule->getFunctionList().remove(M);
Chris Lattnere1815642001-07-15 06:35:53 +00001199 }
1200 }
1201
1202 if (M == 0) { // Not already defined?
Chris Lattner79df7c02002-03-26 18:01:55 +00001203 M = new Function(MT, $1, FunctionName);
Chris Lattnere1815642001-07-15 06:35:53 +00001204 InsertValue(M, CurModule.Values);
Chris Lattnerdda71962001-11-26 18:54:16 +00001205 CurModule.DeclareNewGlobalValue(M, ValID::create($3));
Chris Lattnere1815642001-07-15 06:35:53 +00001206 }
Chris Lattnerdda71962001-11-26 18:54:16 +00001207 free($3); // Free strdup'd memory!
Chris Lattner00950542001-06-06 20:29:01 +00001208
Chris Lattner79df7c02002-03-26 18:01:55 +00001209 CurMeth.FunctionStart(M);
Chris Lattner00950542001-06-06 20:29:01 +00001210
1211 // Add all of the arguments we parsed to the method...
Chris Lattnerdda71962001-11-26 18:54:16 +00001212 if ($5 && !CurMeth.isDeclare) { // Is null if empty...
Chris Lattner79df7c02002-03-26 18:01:55 +00001213 Function::ArgumentListType &ArgList = M->getArgumentList();
Chris Lattner00950542001-06-06 20:29:01 +00001214
Chris Lattner79df7c02002-03-26 18:01:55 +00001215 for (list<pair<FunctionArgument*, char*> >::iterator I = $5->begin();
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001216 I != $5->end(); ++I) {
1217 if (setValueName(I->first, I->second)) { // Insert into symtab...
1218 assert(0 && "No arg redef allowed!");
1219 }
1220
1221 InsertValue(I->first);
1222 ArgList.push_back(I->first);
Chris Lattner00950542001-06-06 20:29:01 +00001223 }
Chris Lattnerdda71962001-11-26 18:54:16 +00001224 delete $5; // We're now done with the argument list
Chris Lattner9176fe42002-03-08 18:57:56 +00001225 } else if ($5) {
1226 // If we are a declaration, we should free the memory for the argument list!
Chris Lattner79df7c02002-03-26 18:01:55 +00001227 for (list<pair<FunctionArgument*, char*> >::iterator I = $5->begin();
Chris Lattner09c07532002-03-31 07:16:49 +00001228 I != $5->end(); ++I) {
Chris Lattner9176fe42002-03-08 18:57:56 +00001229 if (I->second) free(I->second); // Free the memory for the name...
Chris Lattner09c07532002-03-31 07:16:49 +00001230 delete I->first; // Free the unused function argument
1231 }
Chris Lattner9176fe42002-03-08 18:57:56 +00001232 delete $5; // Free the memory for the list itself
Chris Lattner00950542001-06-06 20:29:01 +00001233 }
1234}
1235
Chris Lattner79df7c02002-03-26 18:01:55 +00001236FunctionHeader : FunctionHeaderH ConstPool BEGINTOK {
1237 $$ = CurMeth.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00001238
1239 // Resolve circular types before we parse the body of the method.
1240 ResolveTypes(CurMeth.LateResolveTypes);
Chris Lattner00950542001-06-06 20:29:01 +00001241}
1242
Chris Lattner79df7c02002-03-26 18:01:55 +00001243Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00001244 $$ = $1;
1245}
1246
Chris Lattner79df7c02002-03-26 18:01:55 +00001247FunctionProto : DECLARE { CurMeth.isDeclare = true; } FunctionHeaderH {
1248 $$ = CurMeth.CurrentFunction;
1249 assert($$->getParent() == 0 && "Function already in module!");
1250 CurModule.CurrentModule->getFunctionList().push_back($$);
1251 CurMeth.FunctionDone();
Chris Lattnere1815642001-07-15 06:35:53 +00001252}
Chris Lattner00950542001-06-06 20:29:01 +00001253
1254//===----------------------------------------------------------------------===//
1255// Rules to match Basic Blocks
1256//===----------------------------------------------------------------------===//
1257
1258ConstValueRef : ESINT64VAL { // A reference to a direct constant
1259 $$ = ValID::create($1);
1260 }
1261 | EUINT64VAL {
1262 $$ = ValID::create($1);
1263 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001264 | FPVAL { // Perhaps it's an FP constant?
1265 $$ = ValID::create($1);
1266 }
Chris Lattner00950542001-06-06 20:29:01 +00001267 | TRUE {
1268 $$ = ValID::create((int64_t)1);
1269 }
1270 | FALSE {
1271 $$ = ValID::create((int64_t)0);
1272 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001273 | NULL_TOK {
1274 $$ = ValID::createNull();
1275 }
1276
Chris Lattner93750fa2001-07-28 17:48:55 +00001277/*
Chris Lattner00950542001-06-06 20:29:01 +00001278 | STRINGCONSTANT { // Quoted strings work too... especially for methods
1279 $$ = ValID::create_conststr($1);
1280 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001281*/
Chris Lattner00950542001-06-06 20:29:01 +00001282
Chris Lattner2079fde2001-10-13 06:41:08 +00001283// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1284// another value.
1285//
1286SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001287 $$ = ValID::create($1);
1288 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001289 | VAR_ID { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001290 $$ = ValID::create($1);
1291 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001292
1293// ValueRef - A reference to a definition... either constant or symbolic
1294ValueRef : SymbolicValueRef | ConstValueRef
1295
Chris Lattner00950542001-06-06 20:29:01 +00001296
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001297// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1298// type immediately preceeds the value reference, and allows complex constant
1299// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001300ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001301 $$ = getVal(*$1, $2); delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +00001302 }
Chris Lattner8b81bf52001-07-25 22:47:46 +00001303
Chris Lattner00950542001-06-06 20:29:01 +00001304
1305BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner89219832001-10-03 19:35:04 +00001306 ($$ = $1)->getBasicBlocks().push_back($2);
Chris Lattner00950542001-06-06 20:29:01 +00001307 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001308 | FunctionHeader BasicBlock { // Do not allow methods with 0 basic blocks
Chris Lattner89219832001-10-03 19:35:04 +00001309 ($$ = $1)->getBasicBlocks().push_back($2);
Chris Lattner00950542001-06-06 20:29:01 +00001310 }
1311
1312
1313// Basic blocks are terminated by branching instructions:
1314// br, br/cc, switch, ret
1315//
Chris Lattner2079fde2001-10-13 06:41:08 +00001316BasicBlock : InstructionList OptAssign BBTerminatorInst {
1317 if (setValueName($3, $2)) { assert(0 && "No redefn allowed!"); }
1318 InsertValue($3);
1319
1320 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001321 InsertValue($1);
1322 $$ = $1;
1323 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001324 | LABELSTR InstructionList OptAssign BBTerminatorInst {
1325 if (setValueName($4, $3)) { assert(0 && "No redefn allowed!"); }
1326 InsertValue($4);
1327
1328 $2->getInstList().push_back($4);
Chris Lattnerb7474512001-10-03 15:39:04 +00001329 if (setValueName($2, $1)) { assert(0 && "No label redef allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001330
1331 InsertValue($2);
1332 $$ = $2;
1333 }
1334
1335InstructionList : InstructionList Inst {
1336 $1->getInstList().push_back($2);
1337 $$ = $1;
1338 }
1339 | /* empty */ {
1340 $$ = new BasicBlock();
1341 }
1342
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001343BBTerminatorInst : RET ResolvedVal { // Return with a result...
1344 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001345 }
1346 | RET VOID { // Return with no result...
1347 $$ = new ReturnInst();
1348 }
1349 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner9636a912001-10-01 16:18:37 +00001350 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $3)));
Chris Lattner00950542001-06-06 20:29:01 +00001351 } // Conditional Branch...
1352 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner9636a912001-10-01 16:18:37 +00001353 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $6)),
1354 cast<BasicBlock>(getVal(Type::LabelTy, $9)),
Chris Lattner00950542001-06-06 20:29:01 +00001355 getVal(Type::BoolTy, $3));
1356 }
1357 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1358 SwitchInst *S = new SwitchInst(getVal($2, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001359 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
Chris Lattner00950542001-06-06 20:29:01 +00001360 $$ = S;
1361
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001362 list<pair<Constant*, BasicBlock*> >::iterator I = $8->begin(),
Chris Lattner00950542001-06-06 20:29:01 +00001363 end = $8->end();
Chris Lattner7fc9fe32001-06-27 23:41:11 +00001364 for (; I != end; ++I)
Chris Lattner00950542001-06-06 20:29:01 +00001365 S->dest_push_back(I->first, I->second);
1366 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001367 | INVOKE TypesV ValueRef '(' ValueRefListE ')' TO ResolvedVal
1368 EXCEPT ResolvedVal {
1369 const PointerType *PMTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001370 const FunctionType *Ty;
Chris Lattner2079fde2001-10-13 06:41:08 +00001371
1372 if (!(PMTy = dyn_cast<PointerType>($2->get())) ||
Chris Lattner79df7c02002-03-26 18:01:55 +00001373 !(Ty = dyn_cast<FunctionType>(PMTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00001374 // Pull out the types of all of the arguments...
1375 vector<const Type*> ParamTypes;
1376 if ($5) {
Chris Lattner6cdb0112001-11-26 16:54:11 +00001377 for (vector<Value*>::iterator I = $5->begin(), E = $5->end(); I!=E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00001378 ParamTypes.push_back((*I)->getType());
1379 }
1380
1381 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1382 if (isVarArg) ParamTypes.pop_back();
1383
Chris Lattner79df7c02002-03-26 18:01:55 +00001384 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattner2079fde2001-10-13 06:41:08 +00001385 PMTy = PointerType::get(Ty);
1386 }
1387 delete $2;
1388
1389 Value *V = getVal(PMTy, $3); // Get the method we're calling...
1390
1391 BasicBlock *Normal = dyn_cast<BasicBlock>($8);
1392 BasicBlock *Except = dyn_cast<BasicBlock>($10);
1393
1394 if (Normal == 0 || Except == 0)
1395 ThrowException("Invoke instruction without label destinations!");
1396
1397 // Create the call node...
1398 if (!$5) { // Has no arguments?
Chris Lattner386a3b72001-10-16 19:54:17 +00001399 $$ = new InvokeInst(V, Normal, Except, vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00001400 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001401 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00001402 // correctly!
1403 //
Chris Lattner79df7c02002-03-26 18:01:55 +00001404 FunctionType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
1405 FunctionType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
Chris Lattner6cdb0112001-11-26 16:54:11 +00001406 vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00001407
1408 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1409 if ((*ArgI)->getType() != *I)
1410 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001411 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001412
1413 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1414 ThrowException("Invalid number of parameters detected!");
1415
Chris Lattner6cdb0112001-11-26 16:54:11 +00001416 $$ = new InvokeInst(V, Normal, Except, *$5);
Chris Lattner2079fde2001-10-13 06:41:08 +00001417 }
1418 delete $5;
1419 }
1420
1421
Chris Lattner00950542001-06-06 20:29:01 +00001422
1423JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1424 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001425 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001426 if (V == 0)
1427 ThrowException("May only switch on a constant pool value!");
1428
Chris Lattner9636a912001-10-01 16:18:37 +00001429 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($5, $6))));
Chris Lattner00950542001-06-06 20:29:01 +00001430 }
1431 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001432 $$ = new list<pair<Constant*, BasicBlock*> >();
1433 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00001434
1435 if (V == 0)
1436 ThrowException("May only switch on a constant pool value!");
1437
Chris Lattner9636a912001-10-01 16:18:37 +00001438 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($4, $5))));
Chris Lattner00950542001-06-06 20:29:01 +00001439 }
1440
1441Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001442 // Is this definition named?? if so, assign the name...
1443 if (setValueName($2, $1)) { assert(0 && "No redefin allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001444 InsertValue($2);
1445 $$ = $2;
1446}
1447
Chris Lattnerc24d2082001-06-11 15:04:20 +00001448PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
1449 $$ = new list<pair<Value*, BasicBlock*> >();
Chris Lattner30c89792001-09-07 16:35:17 +00001450 $$->push_back(make_pair(getVal(*$1, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001451 cast<BasicBlock>(getVal(Type::LabelTy, $5))));
Chris Lattner30c89792001-09-07 16:35:17 +00001452 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001453 }
1454 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1455 $$ = $1;
1456 $1->push_back(make_pair(getVal($1->front().first->getType(), $4),
Chris Lattner9636a912001-10-01 16:18:37 +00001457 cast<BasicBlock>(getVal(Type::LabelTy, $6))));
Chris Lattnerc24d2082001-06-11 15:04:20 +00001458 }
1459
1460
Chris Lattner30c89792001-09-07 16:35:17 +00001461ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner6cdb0112001-11-26 16:54:11 +00001462 $$ = new vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001463 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001464 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001465 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001466 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001467 $1->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001468 }
1469
1470// ValueRefListE - Just like ValueRefList, except that it may also be empty!
1471ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; }
1472
1473InstVal : BinaryOps Types ValueRef ',' ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001474 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001475 if ($$ == 0)
1476 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001477 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001478 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001479 | UnaryOps ResolvedVal {
1480 $$ = UnaryOperator::create($1, $2);
Chris Lattner00950542001-06-06 20:29:01 +00001481 if ($$ == 0)
1482 ThrowException("unary operator returned null!");
Chris Lattner09083092001-07-08 04:57:15 +00001483 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001484 | ShiftOps ResolvedVal ',' ResolvedVal {
1485 if ($4->getType() != Type::UByteTy)
1486 ThrowException("Shift amount must be ubyte!");
1487 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001488 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001489 | CAST ResolvedVal TO Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001490 $$ = new CastInst($2, *$4);
1491 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001492 }
Chris Lattnerc24d2082001-06-11 15:04:20 +00001493 | PHI PHIList {
1494 const Type *Ty = $2->front().first->getType();
1495 $$ = new PHINode(Ty);
Chris Lattner00950542001-06-06 20:29:01 +00001496 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001497 if ($2->front().first->getType() != Ty)
1498 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00001499 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001500 $2->pop_front();
1501 }
1502 delete $2; // Free the list...
1503 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001504 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattneref9c23f2001-10-03 14:53:21 +00001505 const PointerType *PMTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001506 const FunctionType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001507
Chris Lattneref9c23f2001-10-03 14:53:21 +00001508 if (!(PMTy = dyn_cast<PointerType>($2->get())) ||
Chris Lattner79df7c02002-03-26 18:01:55 +00001509 !(Ty = dyn_cast<FunctionType>(PMTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001510 // Pull out the types of all of the arguments...
1511 vector<const Type*> ParamTypes;
Chris Lattneref9c23f2001-10-03 14:53:21 +00001512 if ($5) {
Chris Lattner6cdb0112001-11-26 16:54:11 +00001513 for (vector<Value*>::iterator I = $5->begin(), E = $5->end(); I!=E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00001514 ParamTypes.push_back((*I)->getType());
1515 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001516
1517 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1518 if (isVarArg) ParamTypes.pop_back();
1519
Chris Lattner79df7c02002-03-26 18:01:55 +00001520 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattneref9c23f2001-10-03 14:53:21 +00001521 PMTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001522 }
Chris Lattner30c89792001-09-07 16:35:17 +00001523 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001524
Chris Lattneref9c23f2001-10-03 14:53:21 +00001525 Value *V = getVal(PMTy, $3); // Get the method we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00001526
Chris Lattner8b81bf52001-07-25 22:47:46 +00001527 // Create the call node...
1528 if (!$5) { // Has no arguments?
Chris Lattner386a3b72001-10-16 19:54:17 +00001529 $$ = new CallInst(V, vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00001530 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001531 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner00950542001-06-06 20:29:01 +00001532 // correctly!
1533 //
Chris Lattner79df7c02002-03-26 18:01:55 +00001534 FunctionType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
1535 FunctionType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
Chris Lattner6cdb0112001-11-26 16:54:11 +00001536 vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001537
1538 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1539 if ((*ArgI)->getType() != *I)
1540 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001541 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00001542
Chris Lattner8b81bf52001-07-25 22:47:46 +00001543 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner00950542001-06-06 20:29:01 +00001544 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00001545
Chris Lattner6cdb0112001-11-26 16:54:11 +00001546 $$ = new CallInst(V, *$5);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001547 }
1548 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00001549 }
1550 | MemoryInst {
1551 $$ = $1;
1552 }
1553
Chris Lattner6cdb0112001-11-26 16:54:11 +00001554
1555// IndexList - List of indices for GEP based instructions...
1556IndexList : ',' ValueRefList {
Chris Lattner027dcc52001-07-08 21:10:27 +00001557 $$ = $2;
1558} | /* empty */ {
Chris Lattner6cdb0112001-11-26 16:54:11 +00001559 $$ = new vector<Value*>();
Chris Lattner027dcc52001-07-08 21:10:27 +00001560}
1561
Chris Lattner00950542001-06-06 20:29:01 +00001562MemoryInst : MALLOC Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001563 $$ = new MallocInst(PointerType::get(*$2));
1564 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001565 }
1566 | MALLOC Types ',' UINT ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001567 const Type *Ty = PointerType::get(*$2);
Chris Lattner8896eda2001-07-09 19:38:36 +00001568 $$ = new MallocInst(Ty, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001569 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001570 }
1571 | ALLOCA Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001572 $$ = new AllocaInst(PointerType::get(*$2));
1573 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001574 }
1575 | ALLOCA Types ',' UINT ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001576 const Type *Ty = PointerType::get(*$2);
Chris Lattner00950542001-06-06 20:29:01 +00001577 Value *ArrSize = getVal($4, $5);
Chris Lattnerf0d0e9c2001-07-07 08:36:30 +00001578 $$ = new AllocaInst(Ty, ArrSize);
Chris Lattner30c89792001-09-07 16:35:17 +00001579 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001580 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001581 | FREE ResolvedVal {
1582 if (!$2->getType()->isPointerType())
1583 ThrowException("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00001584 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001585 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001586 }
1587
Chris Lattner6cdb0112001-11-26 16:54:11 +00001588 | LOAD Types ValueRef IndexList {
Chris Lattner30c89792001-09-07 16:35:17 +00001589 if (!(*$2)->isPointerType())
Chris Lattner2079fde2001-10-13 06:41:08 +00001590 ThrowException("Can't load from nonpointer type: " +
1591 (*$2)->getDescription());
Chris Lattner30c89792001-09-07 16:35:17 +00001592 if (LoadInst::getIndexedType(*$2, *$4) == 0)
Chris Lattner027dcc52001-07-08 21:10:27 +00001593 ThrowException("Invalid indices for load instruction!");
1594
Chris Lattner30c89792001-09-07 16:35:17 +00001595 $$ = new LoadInst(getVal(*$2, $3), *$4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001596 delete $4; // Free the vector...
Chris Lattner30c89792001-09-07 16:35:17 +00001597 delete $2;
Chris Lattner027dcc52001-07-08 21:10:27 +00001598 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00001599 | STORE ResolvedVal ',' Types ValueRef IndexList {
Chris Lattner30c89792001-09-07 16:35:17 +00001600 if (!(*$4)->isPointerType())
Chris Lattner72e00252001-12-14 16:28:42 +00001601 ThrowException("Can't store to a nonpointer type: " +
1602 (*$4)->getDescription());
Chris Lattner30c89792001-09-07 16:35:17 +00001603 const Type *ElTy = StoreInst::getIndexedType(*$4, *$6);
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001604 if (ElTy == 0)
1605 ThrowException("Can't store into that field list!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001606 if (ElTy != $2->getType())
Chris Lattner72e00252001-12-14 16:28:42 +00001607 ThrowException("Can't store '" + $2->getType()->getDescription() +
1608 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001609 $$ = new StoreInst($2, getVal(*$4, $5), *$6);
1610 delete $4; delete $6;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001611 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00001612 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner30c89792001-09-07 16:35:17 +00001613 if (!(*$2)->isPointerType())
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001614 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner30c89792001-09-07 16:35:17 +00001615 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattner72e00252001-12-14 16:28:42 +00001616 ThrowException("Can't get element ptr '" + (*$2)->getDescription()+ "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001617 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
1618 delete $2; delete $4;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001619 }
Chris Lattner027dcc52001-07-08 21:10:27 +00001620
Chris Lattner00950542001-06-06 20:29:01 +00001621%%
Chris Lattner09083092001-07-08 04:57:15 +00001622int yyerror(const char *ErrorMsg) {
Chris Lattner00950542001-06-06 20:29:01 +00001623 ThrowException(string("Parse error: ") + ErrorMsg);
1624 return 0;
1625}