blob: 8d7891b00264fd8846d84508de5107f0de4cd5b0 [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 Lattner00950542001-06-06 20:29:01 +00009#include "llvm/SymbolTable.h"
10#include "llvm/Module.h"
Chris Lattner00950542001-06-06 20:29:01 +000011#include "llvm/iTerminators.h"
12#include "llvm/iMemory.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000013#include "llvm/iPHINode.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000014#include "Support/STLExtras.h"
15#include "Support/DepthFirstIterator.h"
Chris Lattner00950542001-06-06 20:29:01 +000016#include <list>
Chris Lattnerc188eeb2002-07-30 18:54:25 +000017#include <utility>
Chris Lattner30c89792001-09-07 16:35:17 +000018#include <algorithm>
Chris Lattner697954c2002-01-20 22:54:45 +000019using std::list;
20using std::vector;
21using std::pair;
22using std::map;
23using std::pair;
24using std::make_pair;
Chris Lattner697954c2002-01-20 22:54:45 +000025using std::string;
Chris Lattner00950542001-06-06 20:29:01 +000026
Chris Lattner386a3b72001-10-16 19:54:17 +000027int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
Chris Lattner09083092001-07-08 04:57:15 +000028int yylex(); // declaration" of xxx warnings.
Chris Lattner00950542001-06-06 20:29:01 +000029int yyparse();
30
31static Module *ParserResult;
Chris Lattnera2850432001-07-22 18:36:00 +000032string CurFilename;
Chris Lattner00950542001-06-06 20:29:01 +000033
Chris Lattner30c89792001-09-07 16:35:17 +000034// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
35// relating to upreferences in the input stream.
36//
37//#define DEBUG_UPREFS 1
38#ifdef DEBUG_UPREFS
Chris Lattner699f1eb2002-08-14 17:12:33 +000039#define UR_OUT(X) std::cerr << X
Chris Lattner30c89792001-09-07 16:35:17 +000040#else
41#define UR_OUT(X)
42#endif
43
Vikram S. Adved3f7eb02002-07-14 22:59:28 +000044#define YYERROR_VERBOSE 1
45
Chris Lattner7e708292002-06-25 16:13:24 +000046// This contains info used when building the body of a function. It is
47// destroyed when the function is completed.
Chris Lattner00950542001-06-06 20:29:01 +000048//
49typedef vector<Value *> ValueList; // Numbered defs
Chris Lattner386a3b72001-10-16 19:54:17 +000050static void ResolveDefinitions(vector<ValueList> &LateResolvers,
51 vector<ValueList> *FutureLateResolvers = 0);
Chris Lattner00950542001-06-06 20:29:01 +000052
53static struct PerModuleInfo {
54 Module *CurrentModule;
Chris Lattner30c89792001-09-07 16:35:17 +000055 vector<ValueList> Values; // Module level numbered definitions
56 vector<ValueList> LateResolveValues;
Chris Lattner8b88b3b2002-04-04 19:23:55 +000057 vector<PATypeHolder> Types;
58 map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner00950542001-06-06 20:29:01 +000059
Chris Lattner2079fde2001-10-13 06:41:08 +000060 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
61 // references to global values. Global values may be referenced before they
62 // are defined, and if so, the temporary object that they represent is held
Chris Lattnere9bb2df2001-12-03 22:26:30 +000063 // here. This is used for forward references of ConstantPointerRefs.
Chris Lattner2079fde2001-10-13 06:41:08 +000064 //
65 typedef map<pair<const PointerType *, ValID>, GlobalVariable*> GlobalRefsType;
66 GlobalRefsType GlobalRefs;
67
Chris Lattner00950542001-06-06 20:29:01 +000068 void ModuleDone() {
Chris Lattner7e708292002-06-25 16:13:24 +000069 // If we could not resolve some functions at function compilation time
70 // (calls to functions before they are defined), resolve them now... Types
71 // are resolved when the constant pool has been completely parsed.
Chris Lattner30c89792001-09-07 16:35:17 +000072 //
Chris Lattner00950542001-06-06 20:29:01 +000073 ResolveDefinitions(LateResolveValues);
74
Chris Lattner2079fde2001-10-13 06:41:08 +000075 // Check to make sure that all global value forward references have been
76 // resolved!
77 //
78 if (!GlobalRefs.empty()) {
Chris Lattner749ce032002-03-11 22:12:39 +000079 string UndefinedReferences = "Unresolved global references exist:\n";
80
81 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
82 I != E; ++I) {
83 UndefinedReferences += " " + I->first.first->getDescription() + " " +
84 I->first.second.getName() + "\n";
85 }
86 ThrowException(UndefinedReferences);
Chris Lattner2079fde2001-10-13 06:41:08 +000087 }
88
Chris Lattner7e708292002-06-25 16:13:24 +000089 Values.clear(); // Clear out function local definitions
Chris Lattner30c89792001-09-07 16:35:17 +000090 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +000091 CurrentModule = 0;
92 }
Chris Lattner2079fde2001-10-13 06:41:08 +000093
94
Vikram S. Adved3f7eb02002-07-14 22:59:28 +000095 // DeclareNewGlobalValue - Called every time a new GV has been defined. This
Chris Lattner2079fde2001-10-13 06:41:08 +000096 // is used to remove things from the forward declaration map, resolving them
97 // to the correct thing as needed.
98 //
99 void DeclareNewGlobalValue(GlobalValue *GV, ValID D) {
100 // Check to see if there is a forward reference to this global variable...
101 // if there is, eliminate it and patch the reference to use the new def'n.
102 GlobalRefsType::iterator I = GlobalRefs.find(make_pair(GV->getType(), D));
103
104 if (I != GlobalRefs.end()) {
105 GlobalVariable *OldGV = I->second; // Get the placeholder...
106 I->first.second.destroy(); // Free string memory if neccesary
107
108 // Loop over all of the uses of the GlobalValue. The only thing they are
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000109 // allowed to be is ConstantPointerRef's.
Chris Lattner2079fde2001-10-13 06:41:08 +0000110 assert(OldGV->use_size() == 1 && "Only one reference should exist!");
111 while (!OldGV->use_empty()) {
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000112 User *U = OldGV->use_back(); // Must be a ConstantPointerRef...
113 ConstantPointerRef *CPR = cast<ConstantPointerRef>(U);
114 assert(CPR->getValue() == OldGV && "Something isn't happy");
115
116 // Change the const pool reference to point to the real global variable
117 // now. This should drop a use from the OldGV.
118 CPR->mutateReferences(OldGV, GV);
Chris Lattner2079fde2001-10-13 06:41:08 +0000119 }
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000120
121 // Remove OldGV from the module...
Chris Lattner2079fde2001-10-13 06:41:08 +0000122 CurrentModule->getGlobalList().remove(OldGV);
123 delete OldGV; // Delete the old placeholder
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000124
Chris Lattner2079fde2001-10-13 06:41:08 +0000125 // Remove the map entry for the global now that it has been created...
126 GlobalRefs.erase(I);
127 }
128 }
129
Chris Lattner00950542001-06-06 20:29:01 +0000130} CurModule;
131
Chris Lattner79df7c02002-03-26 18:01:55 +0000132static struct PerFunctionInfo {
Chris Lattner7e708292002-06-25 16:13:24 +0000133 Function *CurrentFunction; // Pointer to current function being created
Chris Lattner00950542001-06-06 20:29:01 +0000134
Chris Lattnere1815642001-07-15 06:35:53 +0000135 vector<ValueList> Values; // Keep track of numbered definitions
Chris Lattner00950542001-06-06 20:29:01 +0000136 vector<ValueList> LateResolveValues;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000137 vector<PATypeHolder> Types;
138 map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner7e708292002-06-25 16:13:24 +0000139 bool isDeclare; // Is this function a forward declararation?
Chris Lattner00950542001-06-06 20:29:01 +0000140
Chris Lattner79df7c02002-03-26 18:01:55 +0000141 inline PerFunctionInfo() {
142 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000143 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000144 }
145
Chris Lattner79df7c02002-03-26 18:01:55 +0000146 inline ~PerFunctionInfo() {}
Chris Lattner00950542001-06-06 20:29:01 +0000147
Chris Lattner79df7c02002-03-26 18:01:55 +0000148 inline void FunctionStart(Function *M) {
149 CurrentFunction = M;
Chris Lattner00950542001-06-06 20:29:01 +0000150 }
151
Chris Lattner79df7c02002-03-26 18:01:55 +0000152 void FunctionDone() {
Chris Lattner00950542001-06-06 20:29:01 +0000153 // If we could not resolve some blocks at parsing time (forward branches)
154 // resolve the branches now...
Chris Lattner386a3b72001-10-16 19:54:17 +0000155 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000156
Chris Lattner7e708292002-06-25 16:13:24 +0000157 Values.clear(); // Clear out function local definitions
Chris Lattner30c89792001-09-07 16:35:17 +0000158 Types.clear();
Chris Lattner79df7c02002-03-26 18:01:55 +0000159 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000160 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000161 }
Chris Lattner7e708292002-06-25 16:13:24 +0000162} CurMeth; // Info for the current function...
Chris Lattner00950542001-06-06 20:29:01 +0000163
Chris Lattner79df7c02002-03-26 18:01:55 +0000164static bool inFunctionScope() { return CurMeth.CurrentFunction != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000165
Chris Lattner00950542001-06-06 20:29:01 +0000166
167//===----------------------------------------------------------------------===//
168// Code to handle definitions of all the types
169//===----------------------------------------------------------------------===//
170
Chris Lattner2079fde2001-10-13 06:41:08 +0000171static int InsertValue(Value *D, vector<ValueList> &ValueTab = CurMeth.Values) {
172 if (D->hasName()) return -1; // Is this a numbered definition?
173
174 // Yes, insert the value into the value table...
175 unsigned type = D->getType()->getUniqueID();
176 if (ValueTab.size() <= type)
177 ValueTab.resize(type+1, ValueList());
178 //printf("Values[%d][%d] = %d\n", type, ValueTab[type].size(), D);
179 ValueTab[type].push_back(D);
180 return ValueTab[type].size()-1;
Chris Lattner00950542001-06-06 20:29:01 +0000181}
182
Chris Lattner30c89792001-09-07 16:35:17 +0000183// TODO: FIXME when Type are not const
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000184static void InsertType(const Type *Ty, vector<PATypeHolder> &Types) {
Chris Lattner30c89792001-09-07 16:35:17 +0000185 Types.push_back(Ty);
186}
187
188static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000189 switch (D.Type) {
Chris Lattnerf8dff732002-07-18 05:18:37 +0000190 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000191 unsigned Num = (unsigned)D.Num;
192
193 // Module constants occupy the lowest numbered slots...
194 if (Num < CurModule.Types.size())
195 return CurModule.Types[Num];
196
197 Num -= CurModule.Types.size();
198
199 // Check that the number is within bounds...
200 if (Num <= CurMeth.Types.size())
201 return CurMeth.Types[Num];
Chris Lattner42c9e772001-10-20 09:32:59 +0000202 break;
Chris Lattner30c89792001-09-07 16:35:17 +0000203 }
Chris Lattnerf8dff732002-07-18 05:18:37 +0000204 case ValID::NameVal: { // Is it a named definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000205 string Name(D.Name);
206 SymbolTable *SymTab = 0;
Chris Lattner79df7c02002-03-26 18:01:55 +0000207 if (inFunctionScope()) SymTab = CurMeth.CurrentFunction->getSymbolTable();
Chris Lattner30c89792001-09-07 16:35:17 +0000208 Value *N = SymTab ? SymTab->lookup(Type::TypeTy, Name) : 0;
209
210 if (N == 0) {
Chris Lattner7e708292002-06-25 16:13:24 +0000211 // Symbol table doesn't automatically chain yet... because the function
Chris Lattner30c89792001-09-07 16:35:17 +0000212 // hasn't been added to the module...
213 //
214 SymTab = CurModule.CurrentModule->getSymbolTable();
215 if (SymTab)
216 N = SymTab->lookup(Type::TypeTy, Name);
217 if (N == 0) break;
218 }
219
220 D.destroy(); // Free old strdup'd memory...
Chris Lattnercfe26c92001-10-01 18:26:53 +0000221 return cast<const Type>(N);
Chris Lattner30c89792001-09-07 16:35:17 +0000222 }
223 default:
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000224 ThrowException("Internal parser error: Invalid symbol type reference!");
Chris Lattner30c89792001-09-07 16:35:17 +0000225 }
226
227 // If we reached here, we referenced either a symbol that we don't know about
228 // or an id number that hasn't been read yet. We may be referencing something
229 // forward, so just create an entry to be resolved later and get to it...
230 //
231 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
232
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000233 map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner4a42e902001-10-22 05:56:09 +0000234 CurMeth.LateResolveTypes : CurModule.LateResolveTypes;
235
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000236 map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000237 if (I != LateResolver.end()) {
238 return I->second;
239 }
Chris Lattner30c89792001-09-07 16:35:17 +0000240
Chris Lattner82269592001-10-22 06:01:08 +0000241 Type *Typ = OpaqueType::get();
Chris Lattner4a42e902001-10-22 05:56:09 +0000242 LateResolver.insert(make_pair(D, Typ));
Chris Lattner30c89792001-09-07 16:35:17 +0000243 return Typ;
244}
245
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000246static Value *lookupInSymbolTable(const Type *Ty, const string &Name) {
247 SymbolTable *SymTab =
Chris Lattner9705a152002-05-02 19:27:42 +0000248 inFunctionScope() ? CurMeth.CurrentFunction->getSymbolTable() :
249 CurModule.CurrentModule->getSymbolTable();
Chris Lattner924025e2002-04-29 18:25:33 +0000250 return SymTab ? SymTab->lookup(Ty, Name) : 0;
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000251}
252
Chris Lattner2079fde2001-10-13 06:41:08 +0000253// getValNonImprovising - Look up the value specified by the provided type and
254// the provided ValID. If the value exists and has already been defined, return
255// it. Otherwise return null.
256//
257static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000258 if (isa<FunctionType>(Ty))
259 ThrowException("Functions are not values and "
260 "must be referenced as pointers");
Chris Lattner386a3b72001-10-16 19:54:17 +0000261
Chris Lattner30c89792001-09-07 16:35:17 +0000262 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000263 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000264 unsigned type = Ty->getUniqueID();
Chris Lattner00950542001-06-06 20:29:01 +0000265 unsigned Num = (unsigned)D.Num;
266
267 // Module constants occupy the lowest numbered slots...
268 if (type < CurModule.Values.size()) {
269 if (Num < CurModule.Values[type].size())
270 return CurModule.Values[type][Num];
271
272 Num -= CurModule.Values[type].size();
273 }
274
275 // Make sure that our type is within bounds
Chris Lattner2079fde2001-10-13 06:41:08 +0000276 if (CurMeth.Values.size() <= type) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000277
278 // Check that the number is within bounds...
Chris Lattner2079fde2001-10-13 06:41:08 +0000279 if (CurMeth.Values[type].size() <= Num) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000280
281 return CurMeth.Values[type][Num];
282 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000283
Chris Lattner1a1cb112001-09-30 22:46:54 +0000284 case ValID::NameVal: { // Is it a named definition?
Chris Lattner2079fde2001-10-13 06:41:08 +0000285 Value *N = lookupInSymbolTable(Ty, string(D.Name));
286 if (N == 0) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000287
288 D.destroy(); // Free old strdup'd memory...
289 return N;
290 }
291
Chris Lattner2079fde2001-10-13 06:41:08 +0000292 // Check to make sure that "Ty" is an integral type, and that our
293 // value will fit into the specified type...
294 case ValID::ConstSIntVal: // Is it a constant pool reference??
Chris Lattnerd78700d2002-08-16 21:14:40 +0000295 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64))
296 ThrowException("Signed integral constant '" +
297 itostr(D.ConstPool64) + "' is invalid for type '" +
298 Ty->getDescription() + "'!");
299 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000300
301 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000302 if (!ConstantUInt::isValueValidForType(Ty, D.UConstPool64)) {
303 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64)) {
Chris Lattnerf8dff732002-07-18 05:18:37 +0000304 ThrowException("Integral constant '" + utostr(D.UConstPool64) +
305 "' is invalid or out of range!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000306 } else { // This is really a signed reference. Transmogrify.
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000307 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000308 }
309 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000310 return ConstantUInt::get(Ty, D.UConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000311 }
312
Chris Lattner2079fde2001-10-13 06:41:08 +0000313 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000314 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
Chris Lattner2079fde2001-10-13 06:41:08 +0000315 ThrowException("FP constant invalid for type!!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000316 return ConstantFP::get(Ty, D.ConstPoolFP);
Chris Lattner2079fde2001-10-13 06:41:08 +0000317
318 case ValID::ConstNullVal: // Is it a null value?
Chris Lattner9b625032002-05-06 16:15:30 +0000319 if (!isa<PointerType>(Ty))
Chris Lattner2079fde2001-10-13 06:41:08 +0000320 ThrowException("Cannot create a a non pointer null!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000321 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner2079fde2001-10-13 06:41:08 +0000322
Chris Lattnerd78700d2002-08-16 21:14:40 +0000323 case ValID::ConstantVal: // Fully resolved constant?
324 if (D.ConstantValue->getType() != Ty)
325 ThrowException("Constant expression type different from required type!");
326 return D.ConstantValue;
327
Chris Lattner30c89792001-09-07 16:35:17 +0000328 default:
329 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000330 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000331 } // End of switch
332
Chris Lattner2079fde2001-10-13 06:41:08 +0000333 assert(0 && "Unhandled case!");
334 return 0;
335}
336
337
338// getVal - This function is identical to getValNonImprovising, except that if a
339// value is not already defined, it "improvises" by creating a placeholder var
340// that looks and acts just like the requested variable. When the value is
341// defined later, all uses of the placeholder variable are replaced with the
342// real thing.
343//
344static Value *getVal(const Type *Ty, const ValID &D) {
345 assert(Ty != Type::TypeTy && "Should use getTypeVal for types!");
346
347 // See if the value has already been defined...
348 Value *V = getValNonImprovising(Ty, D);
349 if (V) return V;
Chris Lattner00950542001-06-06 20:29:01 +0000350
351 // If we reached here, we referenced either a symbol that we don't know about
352 // or an id number that hasn't been read yet. We may be referencing something
353 // forward, so just create an entry to be resolved later and get to it...
354 //
Chris Lattner00950542001-06-06 20:29:01 +0000355 Value *d = 0;
Chris Lattner30c89792001-09-07 16:35:17 +0000356 switch (Ty->getPrimitiveID()) {
357 case Type::LabelTyID: d = new BBPlaceHolder(Ty, D); break;
Chris Lattner30c89792001-09-07 16:35:17 +0000358 default: d = new ValuePlaceHolder(Ty, D); break;
Chris Lattner00950542001-06-06 20:29:01 +0000359 }
360
361 assert(d != 0 && "How did we not make something?");
Chris Lattner79df7c02002-03-26 18:01:55 +0000362 if (inFunctionScope())
Chris Lattner386a3b72001-10-16 19:54:17 +0000363 InsertValue(d, CurMeth.LateResolveValues);
364 else
365 InsertValue(d, CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000366 return d;
367}
368
369
370//===----------------------------------------------------------------------===//
371// Code to handle forward references in instructions
372//===----------------------------------------------------------------------===//
373//
374// This code handles the late binding needed with statements that reference
375// values not defined yet... for example, a forward branch, or the PHI node for
376// a loop body.
377//
378// This keeps a table (CurMeth.LateResolveValues) of all such forward references
379// and back patchs after we are done.
380//
381
382// ResolveDefinitions - If we could not resolve some defs at parsing
383// time (forward branches, phi functions for loops, etc...) resolve the
384// defs now...
385//
Chris Lattner386a3b72001-10-16 19:54:17 +0000386static void ResolveDefinitions(vector<ValueList> &LateResolvers,
Chris Lattnerbcafcce2002-07-25 06:17:42 +0000387 vector<ValueList> *FutureLateResolvers) {
Chris Lattner00950542001-06-06 20:29:01 +0000388 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
389 for (unsigned ty = 0; ty < LateResolvers.size(); ty++) {
390 while (!LateResolvers[ty].empty()) {
391 Value *V = LateResolvers[ty].back();
Chris Lattner386a3b72001-10-16 19:54:17 +0000392 assert(!isa<Type>(V) && "Types should be in LateResolveTypes!");
393
Chris Lattner00950542001-06-06 20:29:01 +0000394 LateResolvers[ty].pop_back();
395 ValID &DID = getValIDFromPlaceHolder(V);
396
Chris Lattner2079fde2001-10-13 06:41:08 +0000397 Value *TheRealValue = getValNonImprovising(Type::getUniqueIDType(ty),DID);
Chris Lattner386a3b72001-10-16 19:54:17 +0000398 if (TheRealValue) {
399 V->replaceAllUsesWith(TheRealValue);
400 delete V;
401 } else if (FutureLateResolvers) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000402 // Functions have their unresolved items forwarded to the module late
Chris Lattner386a3b72001-10-16 19:54:17 +0000403 // resolver table
404 InsertValue(V, *FutureLateResolvers);
405 } else {
Chris Lattner9705a152002-05-02 19:27:42 +0000406 if (DID.Type == ValID::NameVal)
Chris Lattner30c89792001-09-07 16:35:17 +0000407 ThrowException("Reference to an invalid definition: '" +DID.getName()+
408 "' of type '" + V->getType()->getDescription() + "'",
409 getLineNumFromPlaceHolder(V));
410 else
411 ThrowException("Reference to an invalid definition: #" +
412 itostr(DID.Num) + " of type '" +
413 V->getType()->getDescription() + "'",
414 getLineNumFromPlaceHolder(V));
415 }
Chris Lattner00950542001-06-06 20:29:01 +0000416 }
417 }
418
419 LateResolvers.clear();
420}
421
Chris Lattner4a42e902001-10-22 05:56:09 +0000422// ResolveTypeTo - A brand new type was just declared. This means that (if
423// name is not null) things referencing Name can be resolved. Otherwise, things
424// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000425//
Chris Lattner4a42e902001-10-22 05:56:09 +0000426static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000427 vector<PATypeHolder> &Types = inFunctionScope() ?
Chris Lattner4a42e902001-10-22 05:56:09 +0000428 CurMeth.Types : CurModule.Types;
Chris Lattner00950542001-06-06 20:29:01 +0000429
Chris Lattner4a42e902001-10-22 05:56:09 +0000430 ValID D;
431 if (Name) D = ValID::create(Name);
432 else D = ValID::create((int)Types.size());
Chris Lattner30c89792001-09-07 16:35:17 +0000433
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000434 map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner4a42e902001-10-22 05:56:09 +0000435 CurMeth.LateResolveTypes : CurModule.LateResolveTypes;
436
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000437 map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000438 if (I != LateResolver.end()) {
Chris Lattner51727be2002-06-04 21:58:56 +0000439 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattner4a42e902001-10-22 05:56:09 +0000440 LateResolver.erase(I);
441 }
442}
443
444// ResolveTypes - At this point, all types should be resolved. Any that aren't
445// are errors.
446//
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000447static void ResolveTypes(map<ValID, PATypeHolder> &LateResolveTypes) {
Chris Lattner4a42e902001-10-22 05:56:09 +0000448 if (!LateResolveTypes.empty()) {
Chris Lattner82269592001-10-22 06:01:08 +0000449 const ValID &DID = LateResolveTypes.begin()->first;
Chris Lattner4a42e902001-10-22 05:56:09 +0000450
451 if (DID.Type == ValID::NameVal)
Chris Lattner82269592001-10-22 06:01:08 +0000452 ThrowException("Reference to an invalid type: '" +DID.getName() + "'");
Chris Lattner4a42e902001-10-22 05:56:09 +0000453 else
Chris Lattner82269592001-10-22 06:01:08 +0000454 ThrowException("Reference to an invalid type: #" + itostr(DID.Num));
Chris Lattner30c89792001-09-07 16:35:17 +0000455 }
456}
457
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000458
Chris Lattner1781aca2001-09-18 04:00:54 +0000459// setValueName - Set the specified value to the name given. The name may be
460// null potentially, in which case this is a noop. The string passed in is
461// assumed to be a malloc'd string buffer, and is freed by this function.
462//
Chris Lattnerb7474512001-10-03 15:39:04 +0000463// This function returns true if the value has already been defined, but is
464// allowed to be redefined in the specified context. If the name is a new name
465// for the typeplane, false is returned.
466//
467static bool setValueName(Value *V, char *NameStr) {
468 if (NameStr == 0) return false;
Chris Lattner386a3b72001-10-16 19:54:17 +0000469
Chris Lattner1781aca2001-09-18 04:00:54 +0000470 string Name(NameStr); // Copy string
471 free(NameStr); // Free old string
472
Chris Lattner2079fde2001-10-13 06:41:08 +0000473 if (V->getType() == Type::VoidTy)
474 ThrowException("Can't assign name '" + Name +
475 "' to a null valued instruction!");
476
Chris Lattner79df7c02002-03-26 18:01:55 +0000477 SymbolTable *ST = inFunctionScope() ?
478 CurMeth.CurrentFunction->getSymbolTableSure() :
Chris Lattner30c89792001-09-07 16:35:17 +0000479 CurModule.CurrentModule->getSymbolTableSure();
480
481 Value *Existing = ST->lookup(V->getType(), Name);
482 if (Existing) { // Inserting a name that is already defined???
483 // There is only one case where this is allowed: when we are refining an
484 // opaque type. In this case, Existing will be an opaque type.
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000485 if (const Type *Ty = dyn_cast<const Type>(Existing)) {
Chris Lattner51727be2002-06-04 21:58:56 +0000486 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Ty)) {
Chris Lattner30c89792001-09-07 16:35:17 +0000487 // We ARE replacing an opaque type!
Chris Lattner51727be2002-06-04 21:58:56 +0000488 ((OpaqueType*)OpTy)->refineAbstractTypeTo(cast<Type>(V));
Chris Lattnerb7474512001-10-03 15:39:04 +0000489 return true;
Chris Lattner30c89792001-09-07 16:35:17 +0000490 }
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000491 }
Chris Lattner30c89792001-09-07 16:35:17 +0000492
Chris Lattner9636a912001-10-01 16:18:37 +0000493 // Otherwise, we are a simple redefinition of a value, check to see if it
494 // is defined the same as the old one...
495 if (const Type *Ty = dyn_cast<const Type>(Existing)) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000496 if (Ty == cast<const Type>(V)) return true; // Yes, it's equal.
Chris Lattner699f1eb2002-08-14 17:12:33 +0000497 // std::cerr << "Type: " << Ty->getDescription() << " != "
Chris Lattnerb7474512001-10-03 15:39:04 +0000498 // << cast<const Type>(V)->getDescription() << "!\n";
499 } else if (GlobalVariable *EGV = dyn_cast<GlobalVariable>(Existing)) {
Chris Lattner43efcbf2001-10-03 19:35:57 +0000500 // We are allowed to redefine a global variable in two circumstances:
501 // 1. If at least one of the globals is uninitialized or
502 // 2. If both initializers have the same value.
503 //
504 // This can only be done if the const'ness of the vars is the same.
505 //
Chris Lattner89219832001-10-03 19:35:04 +0000506 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
507 if (EGV->isConstant() == GV->isConstant() &&
508 (!EGV->hasInitializer() || !GV->hasInitializer() ||
509 EGV->getInitializer() == GV->getInitializer())) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000510
Chris Lattner89219832001-10-03 19:35:04 +0000511 // Make sure the existing global version gets the initializer!
512 if (GV->hasInitializer() && !EGV->hasInitializer())
513 EGV->setInitializer(GV->getInitializer());
514
Chris Lattner2079fde2001-10-13 06:41:08 +0000515 delete GV; // Destroy the duplicate!
Chris Lattner89219832001-10-03 19:35:04 +0000516 return true; // They are equivalent!
517 }
Chris Lattnerb7474512001-10-03 15:39:04 +0000518 }
Chris Lattner9636a912001-10-01 16:18:37 +0000519 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000520 ThrowException("Redefinition of value named '" + Name + "' in the '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000521 V->getType()->getDescription() + "' type plane!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000522 }
Chris Lattner00950542001-06-06 20:29:01 +0000523
Chris Lattner30c89792001-09-07 16:35:17 +0000524 V->setName(Name, ST);
Chris Lattnerb7474512001-10-03 15:39:04 +0000525 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000526}
527
Chris Lattner8896eda2001-07-09 19:38:36 +0000528
Chris Lattner30c89792001-09-07 16:35:17 +0000529//===----------------------------------------------------------------------===//
530// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000531//
Chris Lattner8896eda2001-07-09 19:38:36 +0000532
Chris Lattner30c89792001-09-07 16:35:17 +0000533// TypeContains - Returns true if Ty contains E in it.
534//
535static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattner3ff43872001-09-28 22:56:31 +0000536 return find(df_begin(Ty), df_end(Ty), E) != df_end(Ty);
Chris Lattner30c89792001-09-07 16:35:17 +0000537}
Chris Lattner698b56e2001-07-20 19:15:08 +0000538
Chris Lattner30c89792001-09-07 16:35:17 +0000539
540static vector<pair<unsigned, OpaqueType *> > UpRefs;
541
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000542static PATypeHolder HandleUpRefs(const Type *ty) {
543 PATypeHolder Ty(ty);
Chris Lattner5084d032001-11-02 07:46:26 +0000544 UR_OUT("Type '" << ty->getDescription() <<
545 "' newly formed. Resolving upreferences.\n" <<
546 UpRefs.size() << " upreferences active!\n");
Chris Lattner30c89792001-09-07 16:35:17 +0000547 for (unsigned i = 0; i < UpRefs.size(); ) {
Chris Lattner5084d032001-11-02 07:46:26 +0000548 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
Chris Lattner30c89792001-09-07 16:35:17 +0000549 << UpRefs[i].second->getDescription() << ") = "
Chris Lattner5084d032001-11-02 07:46:26 +0000550 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << endl);
Chris Lattner30c89792001-09-07 16:35:17 +0000551 if (TypeContains(Ty, UpRefs[i].second)) {
552 unsigned Level = --UpRefs[i].first; // Decrement level of upreference
Chris Lattner5084d032001-11-02 07:46:26 +0000553 UR_OUT(" Uplevel Ref Level = " << Level << endl);
Chris Lattner30c89792001-09-07 16:35:17 +0000554 if (Level == 0) { // Upreference should be resolved!
Chris Lattner5084d032001-11-02 07:46:26 +0000555 UR_OUT(" * Resolving upreference for "
556 << UpRefs[i].second->getDescription() << endl;
Chris Lattner30c89792001-09-07 16:35:17 +0000557 string OldName = UpRefs[i].second->getDescription());
558 UpRefs[i].second->refineAbstractTypeTo(Ty);
559 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
Chris Lattner5084d032001-11-02 07:46:26 +0000560 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
Chris Lattner30c89792001-09-07 16:35:17 +0000561 << (const void*)Ty << ", " << Ty->getDescription() << endl);
562 continue;
563 }
564 }
565
566 ++i; // Otherwise, no resolve, move on...
Chris Lattner8896eda2001-07-09 19:38:36 +0000567 }
Chris Lattner30c89792001-09-07 16:35:17 +0000568 // FIXME: TODO: this should return the updated type
Chris Lattner8896eda2001-07-09 19:38:36 +0000569 return Ty;
570}
571
Chris Lattner30c89792001-09-07 16:35:17 +0000572
Chris Lattner00950542001-06-06 20:29:01 +0000573//===----------------------------------------------------------------------===//
574// RunVMAsmParser - Define an interface to this parser
575//===----------------------------------------------------------------------===//
576//
Chris Lattnera2850432001-07-22 18:36:00 +0000577Module *RunVMAsmParser(const string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000578 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000579 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000580 llvmAsmlineno = 1; // Reset the current line number...
581
582 CurModule.CurrentModule = new Module(); // Allocate a new module to read
583 yyparse(); // Parse the file.
584 Module *Result = ParserResult;
Chris Lattner00950542001-06-06 20:29:01 +0000585 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
586 ParserResult = 0;
587
588 return Result;
589}
590
591%}
592
593%union {
Chris Lattner30c89792001-09-07 16:35:17 +0000594 Module *ModuleVal;
Chris Lattner79df7c02002-03-26 18:01:55 +0000595 Function *FunctionVal;
Chris Lattner46748042002-04-09 19:41:42 +0000596 std::pair<Argument*, char*> *ArgVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000597 BasicBlock *BasicBlockVal;
598 TerminatorInst *TermInstVal;
599 Instruction *InstVal;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000600 Constant *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000601
Chris Lattner30c89792001-09-07 16:35:17 +0000602 const Type *PrimType;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000603 PATypeHolder *TypeVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000604 Value *ValueVal;
605
Chris Lattner46748042002-04-09 19:41:42 +0000606 std::list<std::pair<Argument*,char*> > *ArgList;
Chris Lattner697954c2002-01-20 22:54:45 +0000607 std::vector<Value*> *ValueList;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000608 std::list<PATypeHolder> *TypeList;
Chris Lattner697954c2002-01-20 22:54:45 +0000609 std::list<std::pair<Value*,
610 BasicBlock*> > *PHIList; // Represent the RHS of PHI node
Chris Lattner46748042002-04-09 19:41:42 +0000611 std::vector<std::pair<Constant*, BasicBlock*> > *JumpTable;
Chris Lattner697954c2002-01-20 22:54:45 +0000612 std::vector<Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000613
Chris Lattner30c89792001-09-07 16:35:17 +0000614 int64_t SInt64Val;
615 uint64_t UInt64Val;
616 int SIntVal;
617 unsigned UIntVal;
618 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000619 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000620
Chris Lattner30c89792001-09-07 16:35:17 +0000621 char *StrVal; // This memory is strdup'd!
622 ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000623
Chris Lattner30c89792001-09-07 16:35:17 +0000624 Instruction::BinaryOps BinaryOpVal;
625 Instruction::TermOps TermOpVal;
626 Instruction::MemoryOps MemOpVal;
627 Instruction::OtherOps OtherOpVal;
Chris Lattner00950542001-06-06 20:29:01 +0000628}
629
Chris Lattner79df7c02002-03-26 18:01:55 +0000630%type <ModuleVal> Module FunctionList
631%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000632%type <BasicBlockVal> BasicBlock InstructionList
633%type <TermInstVal> BBTerminatorInst
634%type <InstVal> Inst InstVal MemoryInst
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000635%type <ConstVal> ConstVal ConstExpr
Chris Lattner6cdb0112001-11-26 16:54:11 +0000636%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +0000637%type <ArgList> ArgList ArgListH
638%type <ArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000639%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000640%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +0000641%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +0000642%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000643%type <JumpTable> JumpTable
Chris Lattnerdda71962001-11-26 18:54:16 +0000644%type <BoolVal> GlobalType OptInternal // GLOBAL or CONSTANT? Intern?
Chris Lattner00950542001-06-06 20:29:01 +0000645
Chris Lattner2079fde2001-10-13 06:41:08 +0000646// ValueRef - Unresolved reference to a definition or BB
647%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000648%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000649// Tokens and types for handling constant integer values
650//
651// ESINT64VAL - A negative number within long long range
652%token <SInt64Val> ESINT64VAL
653
654// EUINT64VAL - A positive number within uns. long long range
655%token <UInt64Val> EUINT64VAL
656%type <SInt64Val> EINT64VAL
657
658%token <SIntVal> SINTVAL // Signed 32 bit ints...
659%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
660%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000661%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000662
663// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000664%type <TypeVal> Types TypesV UpRTypes UpRTypesV
665%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
Chris Lattner30c89792001-09-07 16:35:17 +0000666%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
667%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000668
669%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
Chris Lattner8ebccb72002-05-22 22:33:00 +0000670%type <StrVal> OptVAR_ID OptAssign FuncName
Chris Lattner00950542001-06-06 20:29:01 +0000671
672
Chris Lattner9b02cc32002-05-03 18:23:48 +0000673%token IMPLEMENTATION TRUE FALSE BEGINTOK ENDTOK DECLARE GLOBAL CONSTANT UNINIT
Chris Lattnerd78700d2002-08-16 21:14:40 +0000674%token TO EXCEPT DOTDOTDOT NULL_TOK CONST INTERNAL OPAQUE NOT
Chris Lattner00950542001-06-06 20:29:01 +0000675
676// Basic Block Terminating Operators
677%token <TermOpVal> RET BR SWITCH
678
Chris Lattner00950542001-06-06 20:29:01 +0000679// Binary Operators
680%type <BinaryOpVal> BinaryOps // all the binary operators
Chris Lattner42c9e772001-10-20 09:32:59 +0000681%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +0000682%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000683
684// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000685%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000686
Chris Lattner027dcc52001-07-08 21:10:27 +0000687// Other Operators
688%type <OtherOpVal> ShiftOps
Chris Lattner2079fde2001-10-13 06:41:08 +0000689%token <OtherOpVal> PHI CALL INVOKE CAST SHL SHR
Chris Lattner027dcc52001-07-08 21:10:27 +0000690
Chris Lattner00950542001-06-06 20:29:01 +0000691%start Module
692%%
693
694// Handle constant integer size restriction and conversion...
695//
696
Chris Lattner51727be2002-06-04 21:58:56 +0000697INTVAL : SINTVAL;
Chris Lattner00950542001-06-06 20:29:01 +0000698INTVAL : UINTVAL {
699 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
700 ThrowException("Value too large for type!");
701 $$ = (int32_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000702};
Chris Lattner00950542001-06-06 20:29:01 +0000703
704
Chris Lattner51727be2002-06-04 21:58:56 +0000705EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
Chris Lattner00950542001-06-06 20:29:01 +0000706EINT64VAL : EUINT64VAL {
707 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
708 ThrowException("Value too large for type!");
709 $$ = (int64_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000710};
Chris Lattner00950542001-06-06 20:29:01 +0000711
Chris Lattner00950542001-06-06 20:29:01 +0000712// Operations that are notably excluded from this list include:
713// RET, BR, & SWITCH because they end basic blocks and are treated specially.
714//
Chris Lattner51727be2002-06-04 21:58:56 +0000715BinaryOps : ADD | SUB | MUL | DIV | REM | AND | OR | XOR;
716BinaryOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
717ShiftOps : SHL | SHR;
Chris Lattner00950542001-06-06 20:29:01 +0000718
Chris Lattnere98dda62001-07-14 06:10:16 +0000719// These are some types that allow classification if we only want a particular
720// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner51727be2002-06-04 21:58:56 +0000721SIntType : LONG | INT | SHORT | SBYTE;
722UIntType : ULONG | UINT | USHORT | UBYTE;
723IntType : SIntType | UIntType;
724FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +0000725
Chris Lattnere98dda62001-07-14 06:10:16 +0000726// OptAssign - Value producing statements have an optional assignment component
Chris Lattner00950542001-06-06 20:29:01 +0000727OptAssign : VAR_ID '=' {
728 $$ = $1;
729 }
730 | /*empty*/ {
731 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +0000732 };
Chris Lattner00950542001-06-06 20:29:01 +0000733
Chris Lattner51727be2002-06-04 21:58:56 +0000734OptInternal : INTERNAL { $$ = true; } | /*empty*/ { $$ = false; };
Chris Lattner30c89792001-09-07 16:35:17 +0000735
736//===----------------------------------------------------------------------===//
737// Types includes all predefined types... except void, because it can only be
Chris Lattner7e708292002-06-25 16:13:24 +0000738// used in specific contexts (function returning void for example). To have
Chris Lattner30c89792001-09-07 16:35:17 +0000739// access to it, a user must explicitly use TypesV.
740//
741
742// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner51727be2002-06-04 21:58:56 +0000743TypesV : Types | VOID { $$ = new PATypeHolder($1); };
744UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattner30c89792001-09-07 16:35:17 +0000745
746Types : UpRTypes {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000747 if (UpRefs.size())
748 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
749 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000750 };
Chris Lattner30c89792001-09-07 16:35:17 +0000751
752
753// Derived types are added later...
754//
Chris Lattner51727be2002-06-04 21:58:56 +0000755PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
756PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000757UpRTypes : OPAQUE {
758 $$ = new PATypeHolder(OpaqueType::get());
759 }
760 | PrimType {
761 $$ = new PATypeHolder($1);
Chris Lattner51727be2002-06-04 21:58:56 +0000762 };
Chris Lattnerd78700d2002-08-16 21:14:40 +0000763UpRTypes : SymbolicValueRef { // Named types are also simple types...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000764 $$ = new PATypeHolder(getTypeVal($1));
Chris Lattner51727be2002-06-04 21:58:56 +0000765};
Chris Lattner30c89792001-09-07 16:35:17 +0000766
Chris Lattner30c89792001-09-07 16:35:17 +0000767// Include derived types in the Types production.
768//
769UpRTypes : '\\' EUINT64VAL { // Type UpReference
770 if ($2 > (uint64_t)INT64_MAX) ThrowException("Value out of range!");
771 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
772 UpRefs.push_back(make_pair((unsigned)$2, OT)); // Add to vector...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000773 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +0000774 UR_OUT("New Upreference!\n");
775 }
Chris Lattner79df7c02002-03-26 18:01:55 +0000776 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner30c89792001-09-07 16:35:17 +0000777 vector<const Type*> Params;
Chris Lattner697954c2002-01-20 22:54:45 +0000778 mapto($3->begin(), $3->end(), std::back_inserter(Params),
779 std::mem_fun_ref(&PATypeHandle<Type>::get));
Chris Lattner2079fde2001-10-13 06:41:08 +0000780 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
781 if (isVarArg) Params.pop_back();
782
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000783 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +0000784 delete $3; // Delete the argument list
785 delete $1; // Delete the old type handle
786 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000787 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000788 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000789 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +0000790 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000791 | '{' TypeListI '}' { // Structure type?
792 vector<const Type*> Elements;
Chris Lattner697954c2002-01-20 22:54:45 +0000793 mapto($2->begin(), $2->end(), std::back_inserter(Elements),
794 std::mem_fun_ref(&PATypeHandle<Type>::get));
Chris Lattner30c89792001-09-07 16:35:17 +0000795
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000796 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000797 delete $2;
798 }
799 | '{' '}' { // Empty structure type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000800 $$ = new PATypeHolder(StructType::get(vector<const Type*>()));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000801 }
802 | UpRTypes '*' { // Pointer type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000803 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000804 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000805 };
Chris Lattner30c89792001-09-07 16:35:17 +0000806
Chris Lattner7e708292002-06-25 16:13:24 +0000807// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +0000808// declaration type lists
809//
810TypeListI : UpRTypes {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000811 $$ = new list<PATypeHolder>();
Chris Lattner30c89792001-09-07 16:35:17 +0000812 $$->push_back(*$1); delete $1;
813 }
814 | TypeListI ',' UpRTypes {
815 ($$=$1)->push_back(*$3); delete $3;
Chris Lattner51727be2002-06-04 21:58:56 +0000816 };
Chris Lattner30c89792001-09-07 16:35:17 +0000817
Chris Lattner7e708292002-06-25 16:13:24 +0000818// ArgTypeList - List of types for a function type declaration...
Chris Lattner30c89792001-09-07 16:35:17 +0000819ArgTypeListI : TypeListI
820 | TypeListI ',' DOTDOTDOT {
821 ($$=$1)->push_back(Type::VoidTy);
822 }
823 | DOTDOTDOT {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000824 ($$ = new list<PATypeHolder>())->push_back(Type::VoidTy);
Chris Lattner30c89792001-09-07 16:35:17 +0000825 }
826 | /*empty*/ {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000827 $$ = new list<PATypeHolder>();
Chris Lattner51727be2002-06-04 21:58:56 +0000828 };
Chris Lattner30c89792001-09-07 16:35:17 +0000829
Chris Lattnere98dda62001-07-14 06:10:16 +0000830// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +0000831// production is used ONLY to represent constants that show up AFTER a 'const',
832// 'constant' or 'global' token at global scope. Constants that can be inlined
833// into other expressions (such as integers and constexprs) are handled by the
834// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +0000835//
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000836ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
837 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
838 if (ATy == 0)
839 ThrowException("Cannot make array constant with type: '" +
840 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +0000841 const Type *ETy = ATy->getElementType();
842 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +0000843
Chris Lattner30c89792001-09-07 16:35:17 +0000844 // Verify that we have the correct size...
845 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +0000846 ThrowException("Type mismatch: constant sized array initialized with " +
Chris Lattner30c89792001-09-07 16:35:17 +0000847 utostr($3->size()) + " arguments, but has size of " +
848 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +0000849
Chris Lattner30c89792001-09-07 16:35:17 +0000850 // Verify all elements are correct type!
851 for (unsigned i = 0; i < $3->size(); i++) {
852 if (ETy != (*$3)[i]->getType())
Chris Lattner00950542001-06-06 20:29:01 +0000853 ThrowException("Element #" + utostr(i) + " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +0000854 ETy->getDescription() +"' as required!\nIt is of type '"+
855 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +0000856 }
857
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000858 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +0000859 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000860 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000861 | Types '[' ']' {
862 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
863 if (ATy == 0)
864 ThrowException("Cannot make array constant with type: '" +
865 (*$1)->getDescription() + "'!");
866
867 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +0000868 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +0000869 ThrowException("Type mismatch: constant sized array initialized with 0"
Chris Lattner30c89792001-09-07 16:35:17 +0000870 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000871 $$ = ConstantArray::get(ATy, vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +0000872 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +0000873 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000874 | Types 'c' STRINGCONSTANT {
875 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
876 if (ATy == 0)
877 ThrowException("Cannot make array constant with type: '" +
878 (*$1)->getDescription() + "'!");
879
Chris Lattner30c89792001-09-07 16:35:17 +0000880 int NumElements = ATy->getNumElements();
881 const Type *ETy = ATy->getElementType();
882 char *EndStr = UnEscapeLexed($3, true);
883 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +0000884 ThrowException("Can't build string constant of size " +
Chris Lattner30c89792001-09-07 16:35:17 +0000885 itostr((int)(EndStr-$3)) +
886 " when array has size " + itostr(NumElements) + "!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000887 vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +0000888 if (ETy == Type::SByteTy) {
889 for (char *C = $3; C != EndStr; ++C)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000890 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +0000891 } else if (ETy == Type::UByteTy) {
892 for (char *C = $3; C != EndStr; ++C)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000893 Vals.push_back(ConstantUInt::get(ETy, *C));
Chris Lattner93750fa2001-07-28 17:48:55 +0000894 } else {
Chris Lattner30c89792001-09-07 16:35:17 +0000895 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +0000896 ThrowException("Cannot build string arrays of non byte sized elements!");
897 }
Chris Lattner30c89792001-09-07 16:35:17 +0000898 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000899 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +0000900 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +0000901 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000902 | Types '{' ConstVector '}' {
903 const StructType *STy = dyn_cast<const StructType>($1->get());
904 if (STy == 0)
905 ThrowException("Cannot make struct constant with type: '" +
906 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +0000907 // FIXME: TODO: Check to see that the constants are compatible with the type
908 // initializer!
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000909 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +0000910 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000911 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000912 | Types NULL_TOK {
913 const PointerType *PTy = dyn_cast<const PointerType>($1->get());
914 if (PTy == 0)
915 ThrowException("Cannot make null pointer constant with type: '" +
916 (*$1)->getDescription() + "'!");
917
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000918 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000919 delete $1;
920 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000921 | Types SymbolicValueRef {
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000922 const PointerType *Ty = dyn_cast<const PointerType>($1->get());
923 if (Ty == 0)
924 ThrowException("Global const reference must be a pointer type!");
925
Chris Lattner3101c252002-08-15 17:58:33 +0000926 // ConstExprs can exist in the body of a function, thus creating
927 // ConstantPointerRefs whenever they refer to a variable. Because we are in
928 // the context of a function, getValNonImprovising will search the functions
929 // symbol table instead of the module symbol table for the global symbol,
930 // which throws things all off. To get around this, we just tell
931 // getValNonImprovising that we are at global scope here.
932 //
933 Function *SavedCurFn = CurMeth.CurrentFunction;
934 CurMeth.CurrentFunction = 0;
935
Chris Lattner2079fde2001-10-13 06:41:08 +0000936 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000937
Chris Lattner3101c252002-08-15 17:58:33 +0000938 CurMeth.CurrentFunction = SavedCurFn;
939
940
Chris Lattner2079fde2001-10-13 06:41:08 +0000941 // If this is an initializer for a constant pointer, which is referencing a
942 // (currently) undefined variable, create a stub now that shall be replaced
943 // in the future with the right type of variable.
944 //
945 if (V == 0) {
946 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
947 const PointerType *PT = cast<PointerType>(Ty);
948
949 // First check to see if the forward references value is already created!
950 PerModuleInfo::GlobalRefsType::iterator I =
951 CurModule.GlobalRefs.find(make_pair(PT, $2));
952
953 if (I != CurModule.GlobalRefs.end()) {
954 V = I->second; // Placeholder already exists, use it...
955 } else {
956 // TODO: Include line number info by creating a subclass of
957 // TODO: GlobalVariable here that includes the said information!
958
959 // Create a placeholder for the global variable reference...
Chris Lattner7a176752001-12-04 00:03:30 +0000960 GlobalVariable *GV = new GlobalVariable(PT->getElementType(),
961 false, true);
Chris Lattner2079fde2001-10-13 06:41:08 +0000962 // Keep track of the fact that we have a forward ref to recycle it
963 CurModule.GlobalRefs.insert(make_pair(make_pair(PT, $2), GV));
964
965 // Must temporarily push this value into the module table...
966 CurModule.CurrentModule->getGlobalList().push_back(GV);
967 V = GV;
968 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000969 }
970
Chris Lattner2079fde2001-10-13 06:41:08 +0000971 GlobalValue *GV = cast<GlobalValue>(V);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000972 $$ = ConstantPointerRef::get(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +0000973 delete $1; // Free the type handle
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000974 }
Chris Lattnerd78700d2002-08-16 21:14:40 +0000975 | Types ConstExpr {
976 if ($1->get() != $2->getType())
977 ThrowException("Mismatched types for constant expression!");
978 $$ = $2;
979 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000980 };
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000981
Chris Lattnerd05e3592002-08-15 18:17:28 +0000982ConstVal : SIntType EINT64VAL { // integral constants
983 if (!ConstantSInt::isValueValidForType($1, $2))
984 ThrowException("Constant value doesn't fit in type!");
985 $$ = ConstantSInt::get($1, $2);
986 }
987 | UIntType EUINT64VAL { // integral constants
988 if (!ConstantUInt::isValueValidForType($1, $2))
989 ThrowException("Constant value doesn't fit in type!");
990 $$ = ConstantUInt::get($1, $2);
991 }
992 | BOOL TRUE { // Boolean constants
993 $$ = ConstantBool::True;
994 }
995 | BOOL FALSE { // Boolean constants
996 $$ = ConstantBool::False;
997 }
998 | FPType FPVAL { // Float & Double constants
999 $$ = ConstantFP::get($1, $2);
1000 };
1001
Chris Lattner00950542001-06-06 20:29:01 +00001002
Chris Lattnerd78700d2002-08-16 21:14:40 +00001003ConstExpr: CAST '(' ConstVal TO Types ')' {
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001004 $$ = ConstantExpr::getCast($3, $5->get());
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001005 delete $5;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001006 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001007 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1008 if (!isa<PointerType>($3->getType()))
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001009 ThrowException("GetElementPtr requires a pointer operand!");
1010
1011 const Type *IdxTy =
Chris Lattnerd78700d2002-08-16 21:14:40 +00001012 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001013 if (!IdxTy)
1014 ThrowException("Index list invalid for constant getelementptr!");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001015
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001016 vector<Constant*> IdxVec;
Chris Lattnerd78700d2002-08-16 21:14:40 +00001017 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1018 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001019 IdxVec.push_back(C);
1020 else
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001021 ThrowException("Indices to constant getelementptr must be constants!");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001022
Chris Lattnerd78700d2002-08-16 21:14:40 +00001023 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001024
Chris Lattnerd78700d2002-08-16 21:14:40 +00001025 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001026 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001027 | BinaryOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001028 if ($3->getType() != $5->getType())
1029 ThrowException("Binary operator types must match!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001030 $$ = ConstantExpr::get($1, $3, $5);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001031 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001032 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001033 if ($5->getType() != Type::UByteTy)
1034 ThrowException("Shift count for shift constant must be unsigned byte!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001035 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattner699f1eb2002-08-14 17:12:33 +00001036 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001037
1038
Chris Lattnere98dda62001-07-14 06:10:16 +00001039// ConstVector - A list of comma seperated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001040ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001041 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001042 }
1043 | ConstVal {
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001044 $$ = new vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001045 $$->push_back($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001046 };
Chris Lattner00950542001-06-06 20:29:01 +00001047
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001048
Chris Lattner1781aca2001-09-18 04:00:54 +00001049// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001050GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001051
Chris Lattner00950542001-06-06 20:29:01 +00001052
Chris Lattner0e73ce62002-05-02 19:11:13 +00001053//===----------------------------------------------------------------------===//
1054// Rules to match Modules
1055//===----------------------------------------------------------------------===//
1056
1057// Module rule: Capture the result of parsing the whole file into a result
1058// variable...
1059//
1060Module : FunctionList {
1061 $$ = ParserResult = $1;
1062 CurModule.ModuleDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001063};
Chris Lattner0e73ce62002-05-02 19:11:13 +00001064
Chris Lattner7e708292002-06-25 16:13:24 +00001065// FunctionList - A list of functions, preceeded by a constant pool.
Chris Lattner0e73ce62002-05-02 19:11:13 +00001066//
1067FunctionList : FunctionList Function {
1068 $$ = $1;
1069 assert($2->getParent() == 0 && "Function already in module!");
1070 $1->getFunctionList().push_back($2);
1071 CurMeth.FunctionDone();
1072 }
1073 | FunctionList FunctionProto {
1074 $$ = $1;
1075 }
1076 | FunctionList IMPLEMENTATION {
1077 $$ = $1;
1078 }
1079 | ConstPool {
1080 $$ = CurModule.CurrentModule;
1081 // Resolve circular types before we parse the body of the module
1082 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001083 };
Chris Lattner0e73ce62002-05-02 19:11:13 +00001084
Chris Lattnere98dda62001-07-14 06:10:16 +00001085// ConstPool - Constants with optional names assigned to them.
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001086ConstPool : ConstPool OptAssign CONST ConstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001087 if (setValueName($4, $2)) { assert(0 && "No redefinitions allowed!"); }
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001088 InsertValue($4);
Chris Lattner00950542001-06-06 20:29:01 +00001089 }
Chris Lattner30c89792001-09-07 16:35:17 +00001090 | ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattner4a42e902001-10-22 05:56:09 +00001091 // Eagerly resolve types. This is not an optimization, this is a
1092 // requirement that is due to the fact that we could have this:
1093 //
1094 // %list = type { %list * }
1095 // %list = type { %list * } ; repeated type decl
1096 //
1097 // If types are not resolved eagerly, then the two types will not be
1098 // determined to be the same type!
1099 //
1100 ResolveTypeTo($2, $4->get());
1101
Chris Lattner1781aca2001-09-18 04:00:54 +00001102 // TODO: FIXME when Type are not const
Chris Lattnerb7474512001-10-03 15:39:04 +00001103 if (!setValueName(const_cast<Type*>($4->get()), $2)) {
1104 // If this is not a redefinition of a type...
1105 if (!$2) {
1106 InsertType($4->get(),
Chris Lattner79df7c02002-03-26 18:01:55 +00001107 inFunctionScope() ? CurMeth.Types : CurModule.Types);
Chris Lattnerb7474512001-10-03 15:39:04 +00001108 }
Chris Lattner30c89792001-09-07 16:35:17 +00001109 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001110
1111 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001112 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001113 | ConstPool FunctionProto { // Function prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001114 }
Chris Lattnerdda71962001-11-26 18:54:16 +00001115 | ConstPool OptAssign OptInternal GlobalType ConstVal {
1116 const Type *Ty = $5->getType();
Chris Lattner1781aca2001-09-18 04:00:54 +00001117 // Global declarations appear in Constant Pool
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001118 Constant *Initializer = $5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001119 if (Initializer == 0)
1120 ThrowException("Global value initializer is not a constant!");
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001121
Chris Lattnerdda71962001-11-26 18:54:16 +00001122 GlobalVariable *GV = new GlobalVariable(Ty, $4, $3, Initializer);
Chris Lattnerb7474512001-10-03 15:39:04 +00001123 if (!setValueName(GV, $2)) { // If not redefining...
1124 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001125 int Slot = InsertValue(GV, CurModule.Values);
1126
1127 if (Slot != -1) {
1128 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1129 } else {
1130 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1131 (char*)GV->getName().c_str()));
1132 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001133 }
Chris Lattner1781aca2001-09-18 04:00:54 +00001134 }
Chris Lattnerdda71962001-11-26 18:54:16 +00001135 | ConstPool OptAssign OptInternal UNINIT GlobalType Types {
1136 const Type *Ty = *$6;
Chris Lattner1781aca2001-09-18 04:00:54 +00001137 // Global declarations appear in Constant Pool
Chris Lattnerdda71962001-11-26 18:54:16 +00001138 GlobalVariable *GV = new GlobalVariable(Ty, $5, $3);
Chris Lattnerb7474512001-10-03 15:39:04 +00001139 if (!setValueName(GV, $2)) { // If not redefining...
1140 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001141 int Slot = InsertValue(GV, CurModule.Values);
1142
1143 if (Slot != -1) {
1144 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1145 } else {
1146 assert(GV->hasName() && "Not named and not numbered!?");
1147 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1148 (char*)GV->getName().c_str()));
1149 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001150 }
Chris Lattner09c07532002-03-31 07:16:49 +00001151 delete $6;
Chris Lattnere98dda62001-07-14 06:10:16 +00001152 }
Chris Lattner00950542001-06-06 20:29:01 +00001153 | /* empty: end of list */ {
Chris Lattner51727be2002-06-04 21:58:56 +00001154 };
Chris Lattner00950542001-06-06 20:29:01 +00001155
1156
1157//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00001158// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00001159//===----------------------------------------------------------------------===//
1160
Chris Lattner51727be2002-06-04 21:58:56 +00001161OptVAR_ID : VAR_ID | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001162
1163ArgVal : Types OptVAR_ID {
Chris Lattner46748042002-04-09 19:41:42 +00001164 $$ = new pair<Argument*, char*>(new Argument(*$1), $2);
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001165 delete $1; // Delete the type handle..
Chris Lattner51727be2002-06-04 21:58:56 +00001166};
Chris Lattner00950542001-06-06 20:29:01 +00001167
1168ArgListH : ArgVal ',' ArgListH {
1169 $$ = $3;
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001170 $3->push_front(*$1);
1171 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001172 }
1173 | ArgVal {
Chris Lattner46748042002-04-09 19:41:42 +00001174 $$ = new list<pair<Argument*,char*> >();
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001175 $$->push_front(*$1);
1176 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001177 }
Chris Lattner8b81bf52001-07-25 22:47:46 +00001178 | DOTDOTDOT {
Chris Lattner46748042002-04-09 19:41:42 +00001179 $$ = new list<pair<Argument*, char*> >();
1180 $$->push_front(pair<Argument*,char*>(new Argument(Type::VoidTy), 0));
Chris Lattner51727be2002-06-04 21:58:56 +00001181 };
Chris Lattner00950542001-06-06 20:29:01 +00001182
1183ArgList : ArgListH {
1184 $$ = $1;
1185 }
1186 | /* empty */ {
1187 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001188 };
Chris Lattner00950542001-06-06 20:29:01 +00001189
Chris Lattner8ebccb72002-05-22 22:33:00 +00001190FuncName : VAR_ID | STRINGCONSTANT;
1191
1192FunctionHeaderH : OptInternal TypesV FuncName '(' ArgList ')' {
Chris Lattnerdda71962001-11-26 18:54:16 +00001193 UnEscapeLexed($3);
Chris Lattner79df7c02002-03-26 18:01:55 +00001194 string FunctionName($3);
Chris Lattnerdda71962001-11-26 18:54:16 +00001195
Chris Lattner30c89792001-09-07 16:35:17 +00001196 vector<const Type*> ParamTypeList;
Chris Lattnerdda71962001-11-26 18:54:16 +00001197 if ($5)
Chris Lattner46748042002-04-09 19:41:42 +00001198 for (list<pair<Argument*,char*> >::iterator I = $5->begin();
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001199 I != $5->end(); ++I)
1200 ParamTypeList.push_back(I->first->getType());
Chris Lattner00950542001-06-06 20:29:01 +00001201
Chris Lattner2079fde2001-10-13 06:41:08 +00001202 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1203 if (isVarArg) ParamTypeList.pop_back();
1204
Chris Lattner79df7c02002-03-26 18:01:55 +00001205 const FunctionType *MT = FunctionType::get(*$2, ParamTypeList, isVarArg);
Chris Lattneref9c23f2001-10-03 14:53:21 +00001206 const PointerType *PMT = PointerType::get(MT);
Chris Lattnerdda71962001-11-26 18:54:16 +00001207 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001208
Chris Lattner79df7c02002-03-26 18:01:55 +00001209 Function *M = 0;
Chris Lattnere1815642001-07-15 06:35:53 +00001210 if (SymbolTable *ST = CurModule.CurrentModule->getSymbolTable()) {
Chris Lattner79df7c02002-03-26 18:01:55 +00001211 // Is the function already in symtab?
1212 if (Value *V = ST->lookup(PMT, FunctionName)) {
1213 M = cast<Function>(V);
Chris Lattner00950542001-06-06 20:29:01 +00001214
Chris Lattnere1815642001-07-15 06:35:53 +00001215 // Yes it is. If this is the case, either we need to be a forward decl,
1216 // or it needs to be.
1217 if (!CurMeth.isDeclare && !M->isExternal())
Chris Lattner7e708292002-06-25 16:13:24 +00001218 ThrowException("Redefinition of function '" + FunctionName + "'!");
Chris Lattner34538142002-03-08 19:11:42 +00001219
Chris Lattner5659dd12002-07-15 00:10:33 +00001220 // Make sure that we keep track of the internal marker, even if there was
1221 // a previous "declare".
1222 if ($1)
1223 M->setInternalLinkage(true);
1224
Chris Lattner7e708292002-06-25 16:13:24 +00001225 // If we found a preexisting function prototype, remove it from the
1226 // module, so that we don't get spurious conflicts with global & local
1227 // variables.
Chris Lattner34538142002-03-08 19:11:42 +00001228 //
Chris Lattner79df7c02002-03-26 18:01:55 +00001229 CurModule.CurrentModule->getFunctionList().remove(M);
Chris Lattnere1815642001-07-15 06:35:53 +00001230 }
1231 }
1232
1233 if (M == 0) { // Not already defined?
Chris Lattner79df7c02002-03-26 18:01:55 +00001234 M = new Function(MT, $1, FunctionName);
Chris Lattnere1815642001-07-15 06:35:53 +00001235 InsertValue(M, CurModule.Values);
Chris Lattnerdda71962001-11-26 18:54:16 +00001236 CurModule.DeclareNewGlobalValue(M, ValID::create($3));
Chris Lattnere1815642001-07-15 06:35:53 +00001237 }
Chris Lattnerdda71962001-11-26 18:54:16 +00001238 free($3); // Free strdup'd memory!
Chris Lattner00950542001-06-06 20:29:01 +00001239
Chris Lattner79df7c02002-03-26 18:01:55 +00001240 CurMeth.FunctionStart(M);
Chris Lattner00950542001-06-06 20:29:01 +00001241
Chris Lattner7e708292002-06-25 16:13:24 +00001242 // Add all of the arguments we parsed to the function...
Chris Lattnerdda71962001-11-26 18:54:16 +00001243 if ($5 && !CurMeth.isDeclare) { // Is null if empty...
Chris Lattner46748042002-04-09 19:41:42 +00001244 for (list<pair<Argument*, char*> >::iterator I = $5->begin();
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001245 I != $5->end(); ++I) {
1246 if (setValueName(I->first, I->second)) { // Insert into symtab...
1247 assert(0 && "No arg redef allowed!");
1248 }
1249
1250 InsertValue(I->first);
Chris Lattner7e708292002-06-25 16:13:24 +00001251 M->getArgumentList().push_back(I->first);
Chris Lattner00950542001-06-06 20:29:01 +00001252 }
Chris Lattnerdda71962001-11-26 18:54:16 +00001253 delete $5; // We're now done with the argument list
Chris Lattner9176fe42002-03-08 18:57:56 +00001254 } else if ($5) {
1255 // If we are a declaration, we should free the memory for the argument list!
Chris Lattner46748042002-04-09 19:41:42 +00001256 for (list<pair<Argument*, char*> >::iterator I = $5->begin(), E = $5->end();
1257 I != E; ++I) {
Chris Lattner9176fe42002-03-08 18:57:56 +00001258 if (I->second) free(I->second); // Free the memory for the name...
Chris Lattner09c07532002-03-31 07:16:49 +00001259 delete I->first; // Free the unused function argument
1260 }
Chris Lattner9176fe42002-03-08 18:57:56 +00001261 delete $5; // Free the memory for the list itself
Chris Lattner00950542001-06-06 20:29:01 +00001262 }
Chris Lattner51727be2002-06-04 21:58:56 +00001263};
Chris Lattner00950542001-06-06 20:29:01 +00001264
Chris Lattner9b02cc32002-05-03 18:23:48 +00001265BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1266
1267FunctionHeader : FunctionHeaderH BEGIN {
Chris Lattner79df7c02002-03-26 18:01:55 +00001268 $$ = CurMeth.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00001269
Chris Lattner7e708292002-06-25 16:13:24 +00001270 // Resolve circular types before we parse the body of the function.
Chris Lattner30c89792001-09-07 16:35:17 +00001271 ResolveTypes(CurMeth.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001272};
Chris Lattner00950542001-06-06 20:29:01 +00001273
Chris Lattner9b02cc32002-05-03 18:23:48 +00001274END : ENDTOK | '}'; // Allow end of '}' to end a function
1275
Chris Lattner79df7c02002-03-26 18:01:55 +00001276Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00001277 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001278};
Chris Lattner00950542001-06-06 20:29:01 +00001279
Chris Lattner79df7c02002-03-26 18:01:55 +00001280FunctionProto : DECLARE { CurMeth.isDeclare = true; } FunctionHeaderH {
1281 $$ = CurMeth.CurrentFunction;
1282 assert($$->getParent() == 0 && "Function already in module!");
1283 CurModule.CurrentModule->getFunctionList().push_back($$);
1284 CurMeth.FunctionDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001285};
Chris Lattner00950542001-06-06 20:29:01 +00001286
1287//===----------------------------------------------------------------------===//
1288// Rules to match Basic Blocks
1289//===----------------------------------------------------------------------===//
1290
1291ConstValueRef : ESINT64VAL { // A reference to a direct constant
1292 $$ = ValID::create($1);
1293 }
1294 | EUINT64VAL {
1295 $$ = ValID::create($1);
1296 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001297 | FPVAL { // Perhaps it's an FP constant?
1298 $$ = ValID::create($1);
1299 }
Chris Lattner00950542001-06-06 20:29:01 +00001300 | TRUE {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001301 $$ = ValID::create(ConstantBool::True);
Chris Lattner00950542001-06-06 20:29:01 +00001302 }
1303 | FALSE {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001304 $$ = ValID::create(ConstantBool::False);
Chris Lattner00950542001-06-06 20:29:01 +00001305 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001306 | NULL_TOK {
1307 $$ = ValID::createNull();
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001308 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001309 | ConstExpr {
1310 $$ = ValID::create($1);
1311 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00001312
Chris Lattner2079fde2001-10-13 06:41:08 +00001313// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1314// another value.
1315//
1316SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001317 $$ = ValID::create($1);
1318 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001319 | VAR_ID { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001320 $$ = ValID::create($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001321 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001322
1323// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00001324ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00001325
Chris Lattner00950542001-06-06 20:29:01 +00001326
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001327// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1328// type immediately preceeds the value reference, and allows complex constant
1329// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001330ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001331 $$ = getVal(*$1, $2); delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001332 };
Chris Lattner8b81bf52001-07-25 22:47:46 +00001333
Chris Lattner00950542001-06-06 20:29:01 +00001334BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner7e708292002-06-25 16:13:24 +00001335 ($$ = $1)->getBasicBlockList().push_back($2);
Chris Lattner00950542001-06-06 20:29:01 +00001336 }
Chris Lattner7e708292002-06-25 16:13:24 +00001337 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
1338 ($$ = $1)->getBasicBlockList().push_back($2);
Chris Lattner51727be2002-06-04 21:58:56 +00001339 };
Chris Lattner00950542001-06-06 20:29:01 +00001340
1341
1342// Basic blocks are terminated by branching instructions:
1343// br, br/cc, switch, ret
1344//
Chris Lattner2079fde2001-10-13 06:41:08 +00001345BasicBlock : InstructionList OptAssign BBTerminatorInst {
1346 if (setValueName($3, $2)) { assert(0 && "No redefn allowed!"); }
1347 InsertValue($3);
1348
1349 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001350 InsertValue($1);
1351 $$ = $1;
1352 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001353 | LABELSTR InstructionList OptAssign BBTerminatorInst {
1354 if (setValueName($4, $3)) { assert(0 && "No redefn allowed!"); }
1355 InsertValue($4);
1356
1357 $2->getInstList().push_back($4);
Chris Lattnerb7474512001-10-03 15:39:04 +00001358 if (setValueName($2, $1)) { assert(0 && "No label redef allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001359
1360 InsertValue($2);
1361 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001362 };
Chris Lattner00950542001-06-06 20:29:01 +00001363
1364InstructionList : InstructionList Inst {
1365 $1->getInstList().push_back($2);
1366 $$ = $1;
1367 }
1368 | /* empty */ {
1369 $$ = new BasicBlock();
Chris Lattner51727be2002-06-04 21:58:56 +00001370 };
Chris Lattner00950542001-06-06 20:29:01 +00001371
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001372BBTerminatorInst : RET ResolvedVal { // Return with a result...
1373 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001374 }
1375 | RET VOID { // Return with no result...
1376 $$ = new ReturnInst();
1377 }
1378 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner9636a912001-10-01 16:18:37 +00001379 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $3)));
Chris Lattner00950542001-06-06 20:29:01 +00001380 } // Conditional Branch...
1381 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner9636a912001-10-01 16:18:37 +00001382 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $6)),
1383 cast<BasicBlock>(getVal(Type::LabelTy, $9)),
Chris Lattner00950542001-06-06 20:29:01 +00001384 getVal(Type::BoolTy, $3));
1385 }
1386 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1387 SwitchInst *S = new SwitchInst(getVal($2, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001388 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
Chris Lattner00950542001-06-06 20:29:01 +00001389 $$ = S;
1390
Chris Lattner46748042002-04-09 19:41:42 +00001391 vector<pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
1392 E = $8->end();
1393 for (; I != E; ++I)
Chris Lattner00950542001-06-06 20:29:01 +00001394 S->dest_push_back(I->first, I->second);
1395 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001396 | INVOKE TypesV ValueRef '(' ValueRefListE ')' TO ResolvedVal
1397 EXCEPT ResolvedVal {
1398 const PointerType *PMTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001399 const FunctionType *Ty;
Chris Lattner2079fde2001-10-13 06:41:08 +00001400
1401 if (!(PMTy = dyn_cast<PointerType>($2->get())) ||
Chris Lattner79df7c02002-03-26 18:01:55 +00001402 !(Ty = dyn_cast<FunctionType>(PMTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00001403 // Pull out the types of all of the arguments...
1404 vector<const Type*> ParamTypes;
1405 if ($5) {
Chris Lattner6cdb0112001-11-26 16:54:11 +00001406 for (vector<Value*>::iterator I = $5->begin(), E = $5->end(); I!=E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00001407 ParamTypes.push_back((*I)->getType());
1408 }
1409
1410 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1411 if (isVarArg) ParamTypes.pop_back();
1412
Chris Lattner79df7c02002-03-26 18:01:55 +00001413 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattner2079fde2001-10-13 06:41:08 +00001414 PMTy = PointerType::get(Ty);
1415 }
1416 delete $2;
1417
Chris Lattner7e708292002-06-25 16:13:24 +00001418 Value *V = getVal(PMTy, $3); // Get the function we're calling...
Chris Lattner2079fde2001-10-13 06:41:08 +00001419
1420 BasicBlock *Normal = dyn_cast<BasicBlock>($8);
1421 BasicBlock *Except = dyn_cast<BasicBlock>($10);
1422
1423 if (Normal == 0 || Except == 0)
1424 ThrowException("Invoke instruction without label destinations!");
1425
1426 // Create the call node...
1427 if (!$5) { // Has no arguments?
Chris Lattner386a3b72001-10-16 19:54:17 +00001428 $$ = new InvokeInst(V, Normal, Except, vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00001429 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001430 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00001431 // correctly!
1432 //
Chris Lattner79df7c02002-03-26 18:01:55 +00001433 FunctionType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
1434 FunctionType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
Chris Lattner6cdb0112001-11-26 16:54:11 +00001435 vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00001436
1437 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1438 if ((*ArgI)->getType() != *I)
1439 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001440 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001441
1442 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1443 ThrowException("Invalid number of parameters detected!");
1444
Chris Lattner6cdb0112001-11-26 16:54:11 +00001445 $$ = new InvokeInst(V, Normal, Except, *$5);
Chris Lattner2079fde2001-10-13 06:41:08 +00001446 }
1447 delete $5;
Chris Lattner51727be2002-06-04 21:58:56 +00001448 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001449
1450
Chris Lattner00950542001-06-06 20:29:01 +00001451
1452JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1453 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001454 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001455 if (V == 0)
1456 ThrowException("May only switch on a constant pool value!");
1457
Chris Lattner9636a912001-10-01 16:18:37 +00001458 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($5, $6))));
Chris Lattner00950542001-06-06 20:29:01 +00001459 }
1460 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner46748042002-04-09 19:41:42 +00001461 $$ = new vector<pair<Constant*, BasicBlock*> >();
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001462 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00001463
1464 if (V == 0)
1465 ThrowException("May only switch on a constant pool value!");
1466
Chris Lattner9636a912001-10-01 16:18:37 +00001467 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($4, $5))));
Chris Lattner51727be2002-06-04 21:58:56 +00001468 };
Chris Lattner00950542001-06-06 20:29:01 +00001469
1470Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001471 // Is this definition named?? if so, assign the name...
1472 if (setValueName($2, $1)) { assert(0 && "No redefin allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001473 InsertValue($2);
1474 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001475};
Chris Lattner00950542001-06-06 20:29:01 +00001476
Chris Lattnerc24d2082001-06-11 15:04:20 +00001477PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
1478 $$ = new list<pair<Value*, BasicBlock*> >();
Chris Lattner30c89792001-09-07 16:35:17 +00001479 $$->push_back(make_pair(getVal(*$1, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001480 cast<BasicBlock>(getVal(Type::LabelTy, $5))));
Chris Lattner30c89792001-09-07 16:35:17 +00001481 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001482 }
1483 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1484 $$ = $1;
1485 $1->push_back(make_pair(getVal($1->front().first->getType(), $4),
Chris Lattner9636a912001-10-01 16:18:37 +00001486 cast<BasicBlock>(getVal(Type::LabelTy, $6))));
Chris Lattner51727be2002-06-04 21:58:56 +00001487 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00001488
1489
Chris Lattner30c89792001-09-07 16:35:17 +00001490ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner6cdb0112001-11-26 16:54:11 +00001491 $$ = new vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001492 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001493 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001494 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001495 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001496 $1->push_back($3);
Chris Lattner51727be2002-06-04 21:58:56 +00001497 };
Chris Lattner00950542001-06-06 20:29:01 +00001498
1499// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Chris Lattner51727be2002-06-04 21:58:56 +00001500ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001501
1502InstVal : BinaryOps Types ValueRef ',' ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001503 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001504 if ($$ == 0)
1505 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001506 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001507 }
Chris Lattner699f1eb2002-08-14 17:12:33 +00001508 | NOT ResolvedVal {
1509 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
1510 << " Replacing with 'xor'.\n";
1511
1512 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
1513 if (Ones == 0)
1514 ThrowException("Expected integral type for not instruction!");
1515
1516 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
Chris Lattner00950542001-06-06 20:29:01 +00001517 if ($$ == 0)
Chris Lattner699f1eb2002-08-14 17:12:33 +00001518 ThrowException("Could not create a xor instruction!");
Chris Lattner09083092001-07-08 04:57:15 +00001519 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001520 | ShiftOps ResolvedVal ',' ResolvedVal {
1521 if ($4->getType() != Type::UByteTy)
1522 ThrowException("Shift amount must be ubyte!");
1523 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001524 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001525 | CAST ResolvedVal TO Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001526 $$ = new CastInst($2, *$4);
1527 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001528 }
Chris Lattnerc24d2082001-06-11 15:04:20 +00001529 | PHI PHIList {
1530 const Type *Ty = $2->front().first->getType();
1531 $$ = new PHINode(Ty);
Chris Lattner00950542001-06-06 20:29:01 +00001532 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001533 if ($2->front().first->getType() != Ty)
1534 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00001535 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001536 $2->pop_front();
1537 }
1538 delete $2; // Free the list...
1539 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001540 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattneref9c23f2001-10-03 14:53:21 +00001541 const PointerType *PMTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001542 const FunctionType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001543
Chris Lattneref9c23f2001-10-03 14:53:21 +00001544 if (!(PMTy = dyn_cast<PointerType>($2->get())) ||
Chris Lattner79df7c02002-03-26 18:01:55 +00001545 !(Ty = dyn_cast<FunctionType>(PMTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001546 // Pull out the types of all of the arguments...
1547 vector<const Type*> ParamTypes;
Chris Lattneref9c23f2001-10-03 14:53:21 +00001548 if ($5) {
Chris Lattner6cdb0112001-11-26 16:54:11 +00001549 for (vector<Value*>::iterator I = $5->begin(), E = $5->end(); I!=E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00001550 ParamTypes.push_back((*I)->getType());
1551 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001552
1553 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1554 if (isVarArg) ParamTypes.pop_back();
1555
Chris Lattner79df7c02002-03-26 18:01:55 +00001556 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattneref9c23f2001-10-03 14:53:21 +00001557 PMTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001558 }
Chris Lattner30c89792001-09-07 16:35:17 +00001559 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001560
Chris Lattner7e708292002-06-25 16:13:24 +00001561 Value *V = getVal(PMTy, $3); // Get the function we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00001562
Chris Lattner8b81bf52001-07-25 22:47:46 +00001563 // Create the call node...
1564 if (!$5) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00001565 // Make sure no arguments is a good thing!
1566 if (Ty->getNumParams() != 0)
1567 ThrowException("No arguments passed to a function that "
1568 "expects arguments!");
1569
Chris Lattner386a3b72001-10-16 19:54:17 +00001570 $$ = new CallInst(V, vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00001571 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001572 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner00950542001-06-06 20:29:01 +00001573 // correctly!
1574 //
Chris Lattner79df7c02002-03-26 18:01:55 +00001575 FunctionType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
1576 FunctionType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
Chris Lattner6cdb0112001-11-26 16:54:11 +00001577 vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001578
1579 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1580 if ((*ArgI)->getType() != *I)
1581 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001582 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00001583
Chris Lattner8b81bf52001-07-25 22:47:46 +00001584 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner00950542001-06-06 20:29:01 +00001585 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00001586
Chris Lattner6cdb0112001-11-26 16:54:11 +00001587 $$ = new CallInst(V, *$5);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001588 }
1589 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00001590 }
1591 | MemoryInst {
1592 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001593 };
Chris Lattner00950542001-06-06 20:29:01 +00001594
Chris Lattner6cdb0112001-11-26 16:54:11 +00001595
1596// IndexList - List of indices for GEP based instructions...
1597IndexList : ',' ValueRefList {
Chris Lattner027dcc52001-07-08 21:10:27 +00001598 $$ = $2;
1599} | /* empty */ {
Chris Lattner6cdb0112001-11-26 16:54:11 +00001600 $$ = new vector<Value*>();
Chris Lattner51727be2002-06-04 21:58:56 +00001601};
Chris Lattner027dcc52001-07-08 21:10:27 +00001602
Chris Lattner00950542001-06-06 20:29:01 +00001603MemoryInst : MALLOC Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001604 $$ = new MallocInst(PointerType::get(*$2));
1605 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001606 }
1607 | MALLOC Types ',' UINT ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001608 const Type *Ty = PointerType::get(*$2);
Chris Lattner8896eda2001-07-09 19:38:36 +00001609 $$ = new MallocInst(Ty, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001610 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001611 }
1612 | ALLOCA Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001613 $$ = new AllocaInst(PointerType::get(*$2));
1614 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001615 }
1616 | ALLOCA Types ',' UINT ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001617 const Type *Ty = PointerType::get(*$2);
Chris Lattner00950542001-06-06 20:29:01 +00001618 Value *ArrSize = getVal($4, $5);
Chris Lattnerf0d0e9c2001-07-07 08:36:30 +00001619 $$ = new AllocaInst(Ty, ArrSize);
Chris Lattner30c89792001-09-07 16:35:17 +00001620 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001621 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001622 | FREE ResolvedVal {
Chris Lattner9b625032002-05-06 16:15:30 +00001623 if (!isa<PointerType>($2->getType()))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001624 ThrowException("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00001625 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001626 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001627 }
1628
Chris Lattner6cdb0112001-11-26 16:54:11 +00001629 | LOAD Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00001630 if (!isa<PointerType>($2->get()))
Chris Lattner2079fde2001-10-13 06:41:08 +00001631 ThrowException("Can't load from nonpointer type: " +
1632 (*$2)->getDescription());
Chris Lattner30c89792001-09-07 16:35:17 +00001633 if (LoadInst::getIndexedType(*$2, *$4) == 0)
Chris Lattner027dcc52001-07-08 21:10:27 +00001634 ThrowException("Invalid indices for load instruction!");
1635
Chris Lattner30c89792001-09-07 16:35:17 +00001636 $$ = new LoadInst(getVal(*$2, $3), *$4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001637 delete $4; // Free the vector...
Chris Lattner30c89792001-09-07 16:35:17 +00001638 delete $2;
Chris Lattner027dcc52001-07-08 21:10:27 +00001639 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00001640 | STORE ResolvedVal ',' Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00001641 if (!isa<PointerType>($4->get()))
Chris Lattner72e00252001-12-14 16:28:42 +00001642 ThrowException("Can't store to a nonpointer type: " +
1643 (*$4)->getDescription());
Chris Lattner30c89792001-09-07 16:35:17 +00001644 const Type *ElTy = StoreInst::getIndexedType(*$4, *$6);
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001645 if (ElTy == 0)
1646 ThrowException("Can't store into that field list!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001647 if (ElTy != $2->getType())
Chris Lattner72e00252001-12-14 16:28:42 +00001648 ThrowException("Can't store '" + $2->getType()->getDescription() +
1649 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001650 $$ = new StoreInst($2, getVal(*$4, $5), *$6);
1651 delete $4; delete $6;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001652 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00001653 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00001654 if (!isa<PointerType>($2->get()))
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001655 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner30c89792001-09-07 16:35:17 +00001656 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattner72e00252001-12-14 16:28:42 +00001657 ThrowException("Can't get element ptr '" + (*$2)->getDescription()+ "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001658 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
1659 delete $2; delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00001660 };
Chris Lattner027dcc52001-07-08 21:10:27 +00001661
Chris Lattner00950542001-06-06 20:29:01 +00001662%%
Chris Lattner09083092001-07-08 04:57:15 +00001663int yyerror(const char *ErrorMsg) {
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001664 string where = string((CurFilename == "-")? string("<stdin>") : CurFilename)
1665 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
1666 string errMsg = string(ErrorMsg) + string("\n") + where + " while reading ";
1667 if (yychar == YYEMPTY)
1668 errMsg += "end-of-file.";
1669 else
1670 errMsg += "token: '" + string(llvmAsmtext, llvmAsmleng) + "'";
1671 ThrowException(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00001672 return 0;
1673}