blob: 6b858650b74f69a9f44122a1d8e3e2eaeb406fc1 [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??
295 if (Ty == Type::BoolTy) { // Special handling for boolean data
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000296 return ConstantBool::get(D.ConstPool64 != 0);
Chris Lattner2079fde2001-10-13 06:41:08 +0000297 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000298 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64))
Chris Lattnerf8dff732002-07-18 05:18:37 +0000299 ThrowException("Signed integral constant '" +
Chris Lattner2079fde2001-10-13 06:41:08 +0000300 itostr(D.ConstPool64) + "' is invalid for type '" +
Chris Lattner72e00252001-12-14 16:28:42 +0000301 Ty->getDescription() + "'!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000302 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner00950542001-06-06 20:29:01 +0000303 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000304
305 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000306 if (!ConstantUInt::isValueValidForType(Ty, D.UConstPool64)) {
307 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64)) {
Chris Lattnerf8dff732002-07-18 05:18:37 +0000308 ThrowException("Integral constant '" + utostr(D.UConstPool64) +
309 "' is invalid or out of range!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000310 } else { // This is really a signed reference. Transmogrify.
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000311 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000312 }
313 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000314 return ConstantUInt::get(Ty, D.UConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000315 }
316
Chris Lattner2079fde2001-10-13 06:41:08 +0000317 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000318 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
Chris Lattner2079fde2001-10-13 06:41:08 +0000319 ThrowException("FP constant invalid for type!!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000320 return ConstantFP::get(Ty, D.ConstPoolFP);
Chris Lattner2079fde2001-10-13 06:41:08 +0000321
322 case ValID::ConstNullVal: // Is it a null value?
Chris Lattner9b625032002-05-06 16:15:30 +0000323 if (!isa<PointerType>(Ty))
Chris Lattner2079fde2001-10-13 06:41:08 +0000324 ThrowException("Cannot create a a non pointer null!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000325 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner2079fde2001-10-13 06:41:08 +0000326
Chris Lattner30c89792001-09-07 16:35:17 +0000327 default:
328 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000329 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000330 } // End of switch
331
Chris Lattner2079fde2001-10-13 06:41:08 +0000332 assert(0 && "Unhandled case!");
333 return 0;
334}
335
336
337// getVal - This function is identical to getValNonImprovising, except that if a
338// value is not already defined, it "improvises" by creating a placeholder var
339// that looks and acts just like the requested variable. When the value is
340// defined later, all uses of the placeholder variable are replaced with the
341// real thing.
342//
343static Value *getVal(const Type *Ty, const ValID &D) {
344 assert(Ty != Type::TypeTy && "Should use getTypeVal for types!");
345
346 // See if the value has already been defined...
347 Value *V = getValNonImprovising(Ty, D);
348 if (V) return V;
Chris Lattner00950542001-06-06 20:29:01 +0000349
350 // If we reached here, we referenced either a symbol that we don't know about
351 // or an id number that hasn't been read yet. We may be referencing something
352 // forward, so just create an entry to be resolved later and get to it...
353 //
Chris Lattner00950542001-06-06 20:29:01 +0000354 Value *d = 0;
Chris Lattner30c89792001-09-07 16:35:17 +0000355 switch (Ty->getPrimitiveID()) {
356 case Type::LabelTyID: d = new BBPlaceHolder(Ty, D); break;
Chris Lattner30c89792001-09-07 16:35:17 +0000357 default: d = new ValuePlaceHolder(Ty, D); break;
Chris Lattner00950542001-06-06 20:29:01 +0000358 }
359
360 assert(d != 0 && "How did we not make something?");
Chris Lattner79df7c02002-03-26 18:01:55 +0000361 if (inFunctionScope())
Chris Lattner386a3b72001-10-16 19:54:17 +0000362 InsertValue(d, CurMeth.LateResolveValues);
363 else
364 InsertValue(d, CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000365 return d;
366}
367
368
369//===----------------------------------------------------------------------===//
370// Code to handle forward references in instructions
371//===----------------------------------------------------------------------===//
372//
373// This code handles the late binding needed with statements that reference
374// values not defined yet... for example, a forward branch, or the PHI node for
375// a loop body.
376//
377// This keeps a table (CurMeth.LateResolveValues) of all such forward references
378// and back patchs after we are done.
379//
380
381// ResolveDefinitions - If we could not resolve some defs at parsing
382// time (forward branches, phi functions for loops, etc...) resolve the
383// defs now...
384//
Chris Lattner386a3b72001-10-16 19:54:17 +0000385static void ResolveDefinitions(vector<ValueList> &LateResolvers,
Chris Lattnerbcafcce2002-07-25 06:17:42 +0000386 vector<ValueList> *FutureLateResolvers) {
Chris Lattner00950542001-06-06 20:29:01 +0000387 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
388 for (unsigned ty = 0; ty < LateResolvers.size(); ty++) {
389 while (!LateResolvers[ty].empty()) {
390 Value *V = LateResolvers[ty].back();
Chris Lattner386a3b72001-10-16 19:54:17 +0000391 assert(!isa<Type>(V) && "Types should be in LateResolveTypes!");
392
Chris Lattner00950542001-06-06 20:29:01 +0000393 LateResolvers[ty].pop_back();
394 ValID &DID = getValIDFromPlaceHolder(V);
395
Chris Lattner2079fde2001-10-13 06:41:08 +0000396 Value *TheRealValue = getValNonImprovising(Type::getUniqueIDType(ty),DID);
Chris Lattner386a3b72001-10-16 19:54:17 +0000397 if (TheRealValue) {
398 V->replaceAllUsesWith(TheRealValue);
399 delete V;
400 } else if (FutureLateResolvers) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000401 // Functions have their unresolved items forwarded to the module late
Chris Lattner386a3b72001-10-16 19:54:17 +0000402 // resolver table
403 InsertValue(V, *FutureLateResolvers);
404 } else {
Chris Lattner9705a152002-05-02 19:27:42 +0000405 if (DID.Type == ValID::NameVal)
Chris Lattner30c89792001-09-07 16:35:17 +0000406 ThrowException("Reference to an invalid definition: '" +DID.getName()+
407 "' of type '" + V->getType()->getDescription() + "'",
408 getLineNumFromPlaceHolder(V));
409 else
410 ThrowException("Reference to an invalid definition: #" +
411 itostr(DID.Num) + " of type '" +
412 V->getType()->getDescription() + "'",
413 getLineNumFromPlaceHolder(V));
414 }
Chris Lattner00950542001-06-06 20:29:01 +0000415 }
416 }
417
418 LateResolvers.clear();
419}
420
Chris Lattner4a42e902001-10-22 05:56:09 +0000421// ResolveTypeTo - A brand new type was just declared. This means that (if
422// name is not null) things referencing Name can be resolved. Otherwise, things
423// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000424//
Chris Lattner4a42e902001-10-22 05:56:09 +0000425static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000426 vector<PATypeHolder> &Types = inFunctionScope() ?
Chris Lattner4a42e902001-10-22 05:56:09 +0000427 CurMeth.Types : CurModule.Types;
Chris Lattner00950542001-06-06 20:29:01 +0000428
Chris Lattner4a42e902001-10-22 05:56:09 +0000429 ValID D;
430 if (Name) D = ValID::create(Name);
431 else D = ValID::create((int)Types.size());
Chris Lattner30c89792001-09-07 16:35:17 +0000432
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000433 map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner4a42e902001-10-22 05:56:09 +0000434 CurMeth.LateResolveTypes : CurModule.LateResolveTypes;
435
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000436 map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000437 if (I != LateResolver.end()) {
Chris Lattner51727be2002-06-04 21:58:56 +0000438 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattner4a42e902001-10-22 05:56:09 +0000439 LateResolver.erase(I);
440 }
441}
442
443// ResolveTypes - At this point, all types should be resolved. Any that aren't
444// are errors.
445//
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000446static void ResolveTypes(map<ValID, PATypeHolder> &LateResolveTypes) {
Chris Lattner4a42e902001-10-22 05:56:09 +0000447 if (!LateResolveTypes.empty()) {
Chris Lattner82269592001-10-22 06:01:08 +0000448 const ValID &DID = LateResolveTypes.begin()->first;
Chris Lattner4a42e902001-10-22 05:56:09 +0000449
450 if (DID.Type == ValID::NameVal)
Chris Lattner82269592001-10-22 06:01:08 +0000451 ThrowException("Reference to an invalid type: '" +DID.getName() + "'");
Chris Lattner4a42e902001-10-22 05:56:09 +0000452 else
Chris Lattner82269592001-10-22 06:01:08 +0000453 ThrowException("Reference to an invalid type: #" + itostr(DID.Num));
Chris Lattner30c89792001-09-07 16:35:17 +0000454 }
455}
456
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000457
Chris Lattner1781aca2001-09-18 04:00:54 +0000458// setValueName - Set the specified value to the name given. The name may be
459// null potentially, in which case this is a noop. The string passed in is
460// assumed to be a malloc'd string buffer, and is freed by this function.
461//
Chris Lattnerb7474512001-10-03 15:39:04 +0000462// This function returns true if the value has already been defined, but is
463// allowed to be redefined in the specified context. If the name is a new name
464// for the typeplane, false is returned.
465//
466static bool setValueName(Value *V, char *NameStr) {
467 if (NameStr == 0) return false;
Chris Lattner386a3b72001-10-16 19:54:17 +0000468
Chris Lattner1781aca2001-09-18 04:00:54 +0000469 string Name(NameStr); // Copy string
470 free(NameStr); // Free old string
471
Chris Lattner2079fde2001-10-13 06:41:08 +0000472 if (V->getType() == Type::VoidTy)
473 ThrowException("Can't assign name '" + Name +
474 "' to a null valued instruction!");
475
Chris Lattner79df7c02002-03-26 18:01:55 +0000476 SymbolTable *ST = inFunctionScope() ?
477 CurMeth.CurrentFunction->getSymbolTableSure() :
Chris Lattner30c89792001-09-07 16:35:17 +0000478 CurModule.CurrentModule->getSymbolTableSure();
479
480 Value *Existing = ST->lookup(V->getType(), Name);
481 if (Existing) { // Inserting a name that is already defined???
482 // There is only one case where this is allowed: when we are refining an
483 // opaque type. In this case, Existing will be an opaque type.
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000484 if (const Type *Ty = dyn_cast<const Type>(Existing)) {
Chris Lattner51727be2002-06-04 21:58:56 +0000485 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Ty)) {
Chris Lattner30c89792001-09-07 16:35:17 +0000486 // We ARE replacing an opaque type!
Chris Lattner51727be2002-06-04 21:58:56 +0000487 ((OpaqueType*)OpTy)->refineAbstractTypeTo(cast<Type>(V));
Chris Lattnerb7474512001-10-03 15:39:04 +0000488 return true;
Chris Lattner30c89792001-09-07 16:35:17 +0000489 }
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000490 }
Chris Lattner30c89792001-09-07 16:35:17 +0000491
Chris Lattner9636a912001-10-01 16:18:37 +0000492 // Otherwise, we are a simple redefinition of a value, check to see if it
493 // is defined the same as the old one...
494 if (const Type *Ty = dyn_cast<const Type>(Existing)) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000495 if (Ty == cast<const Type>(V)) return true; // Yes, it's equal.
Chris Lattner699f1eb2002-08-14 17:12:33 +0000496 // std::cerr << "Type: " << Ty->getDescription() << " != "
Chris Lattnerb7474512001-10-03 15:39:04 +0000497 // << cast<const Type>(V)->getDescription() << "!\n";
498 } else if (GlobalVariable *EGV = dyn_cast<GlobalVariable>(Existing)) {
Chris Lattner43efcbf2001-10-03 19:35:57 +0000499 // We are allowed to redefine a global variable in two circumstances:
500 // 1. If at least one of the globals is uninitialized or
501 // 2. If both initializers have the same value.
502 //
503 // This can only be done if the const'ness of the vars is the same.
504 //
Chris Lattner89219832001-10-03 19:35:04 +0000505 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
506 if (EGV->isConstant() == GV->isConstant() &&
507 (!EGV->hasInitializer() || !GV->hasInitializer() ||
508 EGV->getInitializer() == GV->getInitializer())) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000509
Chris Lattner89219832001-10-03 19:35:04 +0000510 // Make sure the existing global version gets the initializer!
511 if (GV->hasInitializer() && !EGV->hasInitializer())
512 EGV->setInitializer(GV->getInitializer());
513
Chris Lattner2079fde2001-10-13 06:41:08 +0000514 delete GV; // Destroy the duplicate!
Chris Lattner89219832001-10-03 19:35:04 +0000515 return true; // They are equivalent!
516 }
Chris Lattnerb7474512001-10-03 15:39:04 +0000517 }
Chris Lattner9636a912001-10-01 16:18:37 +0000518 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000519 ThrowException("Redefinition of value named '" + Name + "' in the '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000520 V->getType()->getDescription() + "' type plane!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000521 }
Chris Lattner00950542001-06-06 20:29:01 +0000522
Chris Lattner30c89792001-09-07 16:35:17 +0000523 V->setName(Name, ST);
Chris Lattnerb7474512001-10-03 15:39:04 +0000524 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000525}
526
Chris Lattner8896eda2001-07-09 19:38:36 +0000527
Chris Lattner30c89792001-09-07 16:35:17 +0000528//===----------------------------------------------------------------------===//
529// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000530//
Chris Lattner8896eda2001-07-09 19:38:36 +0000531
Chris Lattner30c89792001-09-07 16:35:17 +0000532// TypeContains - Returns true if Ty contains E in it.
533//
534static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattner3ff43872001-09-28 22:56:31 +0000535 return find(df_begin(Ty), df_end(Ty), E) != df_end(Ty);
Chris Lattner30c89792001-09-07 16:35:17 +0000536}
Chris Lattner698b56e2001-07-20 19:15:08 +0000537
Chris Lattner30c89792001-09-07 16:35:17 +0000538
539static vector<pair<unsigned, OpaqueType *> > UpRefs;
540
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000541static PATypeHolder HandleUpRefs(const Type *ty) {
542 PATypeHolder Ty(ty);
Chris Lattner5084d032001-11-02 07:46:26 +0000543 UR_OUT("Type '" << ty->getDescription() <<
544 "' newly formed. Resolving upreferences.\n" <<
545 UpRefs.size() << " upreferences active!\n");
Chris Lattner30c89792001-09-07 16:35:17 +0000546 for (unsigned i = 0; i < UpRefs.size(); ) {
Chris Lattner5084d032001-11-02 07:46:26 +0000547 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
Chris Lattner30c89792001-09-07 16:35:17 +0000548 << UpRefs[i].second->getDescription() << ") = "
Chris Lattner5084d032001-11-02 07:46:26 +0000549 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << endl);
Chris Lattner30c89792001-09-07 16:35:17 +0000550 if (TypeContains(Ty, UpRefs[i].second)) {
551 unsigned Level = --UpRefs[i].first; // Decrement level of upreference
Chris Lattner5084d032001-11-02 07:46:26 +0000552 UR_OUT(" Uplevel Ref Level = " << Level << endl);
Chris Lattner30c89792001-09-07 16:35:17 +0000553 if (Level == 0) { // Upreference should be resolved!
Chris Lattner5084d032001-11-02 07:46:26 +0000554 UR_OUT(" * Resolving upreference for "
555 << UpRefs[i].second->getDescription() << endl;
Chris Lattner30c89792001-09-07 16:35:17 +0000556 string OldName = UpRefs[i].second->getDescription());
557 UpRefs[i].second->refineAbstractTypeTo(Ty);
558 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
Chris Lattner5084d032001-11-02 07:46:26 +0000559 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
Chris Lattner30c89792001-09-07 16:35:17 +0000560 << (const void*)Ty << ", " << Ty->getDescription() << endl);
561 continue;
562 }
563 }
564
565 ++i; // Otherwise, no resolve, move on...
Chris Lattner8896eda2001-07-09 19:38:36 +0000566 }
Chris Lattner30c89792001-09-07 16:35:17 +0000567 // FIXME: TODO: this should return the updated type
Chris Lattner8896eda2001-07-09 19:38:36 +0000568 return Ty;
569}
570
Chris Lattner30c89792001-09-07 16:35:17 +0000571
Chris Lattner00950542001-06-06 20:29:01 +0000572//===----------------------------------------------------------------------===//
573// RunVMAsmParser - Define an interface to this parser
574//===----------------------------------------------------------------------===//
575//
Chris Lattnera2850432001-07-22 18:36:00 +0000576Module *RunVMAsmParser(const string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000577 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000578 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000579 llvmAsmlineno = 1; // Reset the current line number...
580
581 CurModule.CurrentModule = new Module(); // Allocate a new module to read
582 yyparse(); // Parse the file.
583 Module *Result = ParserResult;
Chris Lattner00950542001-06-06 20:29:01 +0000584 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
585 ParserResult = 0;
586
587 return Result;
588}
589
590%}
591
592%union {
Chris Lattner30c89792001-09-07 16:35:17 +0000593 Module *ModuleVal;
Chris Lattner79df7c02002-03-26 18:01:55 +0000594 Function *FunctionVal;
Chris Lattner46748042002-04-09 19:41:42 +0000595 std::pair<Argument*, char*> *ArgVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000596 BasicBlock *BasicBlockVal;
597 TerminatorInst *TermInstVal;
598 Instruction *InstVal;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000599 Constant *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000600
Chris Lattner30c89792001-09-07 16:35:17 +0000601 const Type *PrimType;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000602 PATypeHolder *TypeVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000603 Value *ValueVal;
604
Chris Lattner46748042002-04-09 19:41:42 +0000605 std::list<std::pair<Argument*,char*> > *ArgList;
Chris Lattner697954c2002-01-20 22:54:45 +0000606 std::vector<Value*> *ValueList;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000607 std::list<PATypeHolder> *TypeList;
Chris Lattner697954c2002-01-20 22:54:45 +0000608 std::list<std::pair<Value*,
609 BasicBlock*> > *PHIList; // Represent the RHS of PHI node
Chris Lattner46748042002-04-09 19:41:42 +0000610 std::vector<std::pair<Constant*, BasicBlock*> > *JumpTable;
Chris Lattner697954c2002-01-20 22:54:45 +0000611 std::vector<Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000612
Chris Lattner30c89792001-09-07 16:35:17 +0000613 int64_t SInt64Val;
614 uint64_t UInt64Val;
615 int SIntVal;
616 unsigned UIntVal;
617 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000618 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000619
Chris Lattner30c89792001-09-07 16:35:17 +0000620 char *StrVal; // This memory is strdup'd!
621 ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000622
Chris Lattner30c89792001-09-07 16:35:17 +0000623 Instruction::BinaryOps BinaryOpVal;
624 Instruction::TermOps TermOpVal;
625 Instruction::MemoryOps MemOpVal;
626 Instruction::OtherOps OtherOpVal;
Chris Lattner00950542001-06-06 20:29:01 +0000627}
628
Chris Lattner79df7c02002-03-26 18:01:55 +0000629%type <ModuleVal> Module FunctionList
630%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000631%type <BasicBlockVal> BasicBlock InstructionList
632%type <TermInstVal> BBTerminatorInst
633%type <InstVal> Inst InstVal MemoryInst
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000634%type <ConstVal> ConstVal ConstExpr
Chris Lattner6cdb0112001-11-26 16:54:11 +0000635%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +0000636%type <ArgList> ArgList ArgListH
637%type <ArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000638%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000639%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +0000640%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +0000641%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000642%type <JumpTable> JumpTable
Chris Lattnerdda71962001-11-26 18:54:16 +0000643%type <BoolVal> GlobalType OptInternal // GLOBAL or CONSTANT? Intern?
Chris Lattner00950542001-06-06 20:29:01 +0000644
Chris Lattner2079fde2001-10-13 06:41:08 +0000645// ValueRef - Unresolved reference to a definition or BB
646%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000647%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000648// Tokens and types for handling constant integer values
649//
650// ESINT64VAL - A negative number within long long range
651%token <SInt64Val> ESINT64VAL
652
653// EUINT64VAL - A positive number within uns. long long range
654%token <UInt64Val> EUINT64VAL
655%type <SInt64Val> EINT64VAL
656
657%token <SIntVal> SINTVAL // Signed 32 bit ints...
658%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
659%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000660%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000661
662// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000663%type <TypeVal> Types TypesV UpRTypes UpRTypesV
664%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
Chris Lattner30c89792001-09-07 16:35:17 +0000665%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
666%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000667
668%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
Chris Lattner8ebccb72002-05-22 22:33:00 +0000669%type <StrVal> OptVAR_ID OptAssign FuncName
Chris Lattner00950542001-06-06 20:29:01 +0000670
671
Chris Lattner9b02cc32002-05-03 18:23:48 +0000672%token IMPLEMENTATION TRUE FALSE BEGINTOK ENDTOK DECLARE GLOBAL CONSTANT UNINIT
Chris Lattner699f1eb2002-08-14 17:12:33 +0000673%token TO EXCEPT DOTDOTDOT STRING NULL_TOK CONST INTERNAL OPAQUE NOT
Chris Lattner00950542001-06-06 20:29:01 +0000674
675// Basic Block Terminating Operators
676%token <TermOpVal> RET BR SWITCH
677
Chris Lattner00950542001-06-06 20:29:01 +0000678// Binary Operators
679%type <BinaryOpVal> BinaryOps // all the binary operators
Chris Lattner42c9e772001-10-20 09:32:59 +0000680%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +0000681%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000682
683// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000684%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000685
Chris Lattner027dcc52001-07-08 21:10:27 +0000686// Other Operators
687%type <OtherOpVal> ShiftOps
Chris Lattner2079fde2001-10-13 06:41:08 +0000688%token <OtherOpVal> PHI CALL INVOKE CAST SHL SHR
Chris Lattner027dcc52001-07-08 21:10:27 +0000689
Chris Lattner00950542001-06-06 20:29:01 +0000690%start Module
691%%
692
693// Handle constant integer size restriction and conversion...
694//
695
Chris Lattner51727be2002-06-04 21:58:56 +0000696INTVAL : SINTVAL;
Chris Lattner00950542001-06-06 20:29:01 +0000697INTVAL : UINTVAL {
698 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
699 ThrowException("Value too large for type!");
700 $$ = (int32_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000701};
Chris Lattner00950542001-06-06 20:29:01 +0000702
703
Chris Lattner51727be2002-06-04 21:58:56 +0000704EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
Chris Lattner00950542001-06-06 20:29:01 +0000705EINT64VAL : EUINT64VAL {
706 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
707 ThrowException("Value too large for type!");
708 $$ = (int64_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000709};
Chris Lattner00950542001-06-06 20:29:01 +0000710
Chris Lattner00950542001-06-06 20:29:01 +0000711// Operations that are notably excluded from this list include:
712// RET, BR, & SWITCH because they end basic blocks and are treated specially.
713//
Chris Lattner51727be2002-06-04 21:58:56 +0000714BinaryOps : ADD | SUB | MUL | DIV | REM | AND | OR | XOR;
715BinaryOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
716ShiftOps : SHL | SHR;
Chris Lattner00950542001-06-06 20:29:01 +0000717
Chris Lattnere98dda62001-07-14 06:10:16 +0000718// These are some types that allow classification if we only want a particular
719// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner51727be2002-06-04 21:58:56 +0000720SIntType : LONG | INT | SHORT | SBYTE;
721UIntType : ULONG | UINT | USHORT | UBYTE;
722IntType : SIntType | UIntType;
723FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +0000724
Chris Lattnere98dda62001-07-14 06:10:16 +0000725// OptAssign - Value producing statements have an optional assignment component
Chris Lattner00950542001-06-06 20:29:01 +0000726OptAssign : VAR_ID '=' {
727 $$ = $1;
728 }
729 | /*empty*/ {
730 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +0000731 };
Chris Lattner00950542001-06-06 20:29:01 +0000732
Chris Lattner51727be2002-06-04 21:58:56 +0000733OptInternal : INTERNAL { $$ = true; } | /*empty*/ { $$ = false; };
Chris Lattner30c89792001-09-07 16:35:17 +0000734
735//===----------------------------------------------------------------------===//
736// Types includes all predefined types... except void, because it can only be
Chris Lattner7e708292002-06-25 16:13:24 +0000737// used in specific contexts (function returning void for example). To have
Chris Lattner30c89792001-09-07 16:35:17 +0000738// access to it, a user must explicitly use TypesV.
739//
740
741// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner51727be2002-06-04 21:58:56 +0000742TypesV : Types | VOID { $$ = new PATypeHolder($1); };
743UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattner30c89792001-09-07 16:35:17 +0000744
745Types : UpRTypes {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000746 if (UpRefs.size())
747 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
748 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000749 };
Chris Lattner30c89792001-09-07 16:35:17 +0000750
751
752// Derived types are added later...
753//
Chris Lattner51727be2002-06-04 21:58:56 +0000754PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
755PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000756UpRTypes : OPAQUE {
757 $$ = new PATypeHolder(OpaqueType::get());
758 }
759 | PrimType {
760 $$ = new PATypeHolder($1);
Chris Lattner51727be2002-06-04 21:58:56 +0000761 };
Chris Lattner30c89792001-09-07 16:35:17 +0000762UpRTypes : ValueRef { // Named types are also simple types...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000763 $$ = new PATypeHolder(getTypeVal($1));
Chris Lattner51727be2002-06-04 21:58:56 +0000764};
Chris Lattner30c89792001-09-07 16:35:17 +0000765
Chris Lattner30c89792001-09-07 16:35:17 +0000766// Include derived types in the Types production.
767//
768UpRTypes : '\\' EUINT64VAL { // Type UpReference
769 if ($2 > (uint64_t)INT64_MAX) ThrowException("Value out of range!");
770 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
771 UpRefs.push_back(make_pair((unsigned)$2, OT)); // Add to vector...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000772 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +0000773 UR_OUT("New Upreference!\n");
774 }
Chris Lattner79df7c02002-03-26 18:01:55 +0000775 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner30c89792001-09-07 16:35:17 +0000776 vector<const Type*> Params;
Chris Lattner697954c2002-01-20 22:54:45 +0000777 mapto($3->begin(), $3->end(), std::back_inserter(Params),
778 std::mem_fun_ref(&PATypeHandle<Type>::get));
Chris Lattner2079fde2001-10-13 06:41:08 +0000779 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
780 if (isVarArg) Params.pop_back();
781
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000782 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +0000783 delete $3; // Delete the argument list
784 delete $1; // Delete the old type handle
785 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000786 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000787 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000788 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +0000789 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000790 | '{' TypeListI '}' { // Structure type?
791 vector<const Type*> Elements;
Chris Lattner697954c2002-01-20 22:54:45 +0000792 mapto($2->begin(), $2->end(), std::back_inserter(Elements),
793 std::mem_fun_ref(&PATypeHandle<Type>::get));
Chris Lattner30c89792001-09-07 16:35:17 +0000794
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000795 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000796 delete $2;
797 }
798 | '{' '}' { // Empty structure type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000799 $$ = new PATypeHolder(StructType::get(vector<const Type*>()));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000800 }
801 | UpRTypes '*' { // Pointer type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000802 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000803 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000804 };
Chris Lattner30c89792001-09-07 16:35:17 +0000805
Chris Lattner7e708292002-06-25 16:13:24 +0000806// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +0000807// declaration type lists
808//
809TypeListI : UpRTypes {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000810 $$ = new list<PATypeHolder>();
Chris Lattner30c89792001-09-07 16:35:17 +0000811 $$->push_back(*$1); delete $1;
812 }
813 | TypeListI ',' UpRTypes {
814 ($$=$1)->push_back(*$3); delete $3;
Chris Lattner51727be2002-06-04 21:58:56 +0000815 };
Chris Lattner30c89792001-09-07 16:35:17 +0000816
Chris Lattner7e708292002-06-25 16:13:24 +0000817// ArgTypeList - List of types for a function type declaration...
Chris Lattner30c89792001-09-07 16:35:17 +0000818ArgTypeListI : TypeListI
819 | TypeListI ',' DOTDOTDOT {
820 ($$=$1)->push_back(Type::VoidTy);
821 }
822 | DOTDOTDOT {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000823 ($$ = new list<PATypeHolder>())->push_back(Type::VoidTy);
Chris Lattner30c89792001-09-07 16:35:17 +0000824 }
825 | /*empty*/ {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000826 $$ = new list<PATypeHolder>();
Chris Lattner51727be2002-06-04 21:58:56 +0000827 };
Chris Lattner30c89792001-09-07 16:35:17 +0000828
Chris Lattnere98dda62001-07-14 06:10:16 +0000829// ConstVal - The various declarations that go into the constant pool. This
830// includes all forward declarations of types, constants, and functions.
831//
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000832ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
833 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
834 if (ATy == 0)
835 ThrowException("Cannot make array constant with type: '" +
836 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +0000837 const Type *ETy = ATy->getElementType();
838 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +0000839
Chris Lattner30c89792001-09-07 16:35:17 +0000840 // Verify that we have the correct size...
841 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +0000842 ThrowException("Type mismatch: constant sized array initialized with " +
Chris Lattner30c89792001-09-07 16:35:17 +0000843 utostr($3->size()) + " arguments, but has size of " +
844 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +0000845
Chris Lattner30c89792001-09-07 16:35:17 +0000846 // Verify all elements are correct type!
847 for (unsigned i = 0; i < $3->size(); i++) {
848 if (ETy != (*$3)[i]->getType())
Chris Lattner00950542001-06-06 20:29:01 +0000849 ThrowException("Element #" + utostr(i) + " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +0000850 ETy->getDescription() +"' as required!\nIt is of type '"+
851 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +0000852 }
853
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000854 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +0000855 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000856 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000857 | Types '[' ']' {
858 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
859 if (ATy == 0)
860 ThrowException("Cannot make array constant with type: '" +
861 (*$1)->getDescription() + "'!");
862
863 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +0000864 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +0000865 ThrowException("Type mismatch: constant sized array initialized with 0"
Chris Lattner30c89792001-09-07 16:35:17 +0000866 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000867 $$ = ConstantArray::get(ATy, vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +0000868 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +0000869 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000870 | Types 'c' STRINGCONSTANT {
871 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
872 if (ATy == 0)
873 ThrowException("Cannot make array constant with type: '" +
874 (*$1)->getDescription() + "'!");
875
Chris Lattner30c89792001-09-07 16:35:17 +0000876 int NumElements = ATy->getNumElements();
877 const Type *ETy = ATy->getElementType();
878 char *EndStr = UnEscapeLexed($3, true);
879 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +0000880 ThrowException("Can't build string constant of size " +
Chris Lattner30c89792001-09-07 16:35:17 +0000881 itostr((int)(EndStr-$3)) +
882 " when array has size " + itostr(NumElements) + "!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000883 vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +0000884 if (ETy == Type::SByteTy) {
885 for (char *C = $3; C != EndStr; ++C)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000886 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +0000887 } else if (ETy == Type::UByteTy) {
888 for (char *C = $3; C != EndStr; ++C)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000889 Vals.push_back(ConstantUInt::get(ETy, *C));
Chris Lattner93750fa2001-07-28 17:48:55 +0000890 } else {
Chris Lattner30c89792001-09-07 16:35:17 +0000891 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +0000892 ThrowException("Cannot build string arrays of non byte sized elements!");
893 }
Chris Lattner30c89792001-09-07 16:35:17 +0000894 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000895 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +0000896 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +0000897 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000898 | Types '{' ConstVector '}' {
899 const StructType *STy = dyn_cast<const StructType>($1->get());
900 if (STy == 0)
901 ThrowException("Cannot make struct constant with type: '" +
902 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +0000903 // FIXME: TODO: Check to see that the constants are compatible with the type
904 // initializer!
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000905 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +0000906 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000907 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000908 | Types NULL_TOK {
909 const PointerType *PTy = dyn_cast<const PointerType>($1->get());
910 if (PTy == 0)
911 ThrowException("Cannot make null pointer constant with type: '" +
912 (*$1)->getDescription() + "'!");
913
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000914 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000915 delete $1;
916 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000917 | Types SymbolicValueRef {
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000918 const PointerType *Ty = dyn_cast<const PointerType>($1->get());
919 if (Ty == 0)
920 ThrowException("Global const reference must be a pointer type!");
921
Chris Lattner2079fde2001-10-13 06:41:08 +0000922 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000923
Chris Lattner2079fde2001-10-13 06:41:08 +0000924 // If this is an initializer for a constant pointer, which is referencing a
925 // (currently) undefined variable, create a stub now that shall be replaced
926 // in the future with the right type of variable.
927 //
928 if (V == 0) {
929 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
930 const PointerType *PT = cast<PointerType>(Ty);
931
932 // First check to see if the forward references value is already created!
933 PerModuleInfo::GlobalRefsType::iterator I =
934 CurModule.GlobalRefs.find(make_pair(PT, $2));
935
936 if (I != CurModule.GlobalRefs.end()) {
937 V = I->second; // Placeholder already exists, use it...
938 } else {
939 // TODO: Include line number info by creating a subclass of
940 // TODO: GlobalVariable here that includes the said information!
941
942 // Create a placeholder for the global variable reference...
Chris Lattner7a176752001-12-04 00:03:30 +0000943 GlobalVariable *GV = new GlobalVariable(PT->getElementType(),
944 false, true);
Chris Lattner2079fde2001-10-13 06:41:08 +0000945 // Keep track of the fact that we have a forward ref to recycle it
946 CurModule.GlobalRefs.insert(make_pair(make_pair(PT, $2), GV));
947
948 // Must temporarily push this value into the module table...
949 CurModule.CurrentModule->getGlobalList().push_back(GV);
950 V = GV;
951 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000952 }
953
Chris Lattner2079fde2001-10-13 06:41:08 +0000954 GlobalValue *GV = cast<GlobalValue>(V);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000955 $$ = ConstantPointerRef::get(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +0000956 delete $1; // Free the type handle
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000957 }
958 | ConstExpr {
959 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000960 };
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000961
Chris Lattner00950542001-06-06 20:29:01 +0000962
Chris Lattnerf8dff732002-07-18 05:18:37 +0000963// FIXME: ConstExpr::get never return null! Do checking here in the parser.
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000964ConstExpr: Types CAST ConstVal {
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000965 $$ = ConstantExpr::get(Instruction::Cast, $3, $1->get());
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000966 delete $1;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000967 }
968 | Types GETELEMENTPTR '(' ConstVal IndexList ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000969 if (!isa<PointerType>($4->getType()))
970 ThrowException("GetElementPtr requires a pointer operand!");
971
972 const Type *IdxTy =
973 GetElementPtrInst::getIndexedType($4->getType(), *$5, true);
974 if (!IdxTy)
975 ThrowException("Index list invalid for constant getelementptr!");
976 if (PointerType::get(IdxTy) != $1->get())
977 ThrowException("Declared type of constant getelementptr is incorrect!");
978
Chris Lattnercc4b6ec2002-07-18 00:14:27 +0000979 vector<Constant*> IdxVec;
980 for (unsigned i = 0, e = $5->size(); i != e; ++i)
981 if (Constant *C = dyn_cast<Constant>((*$5)[i]))
982 IdxVec.push_back(C);
983 else
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000984 ThrowException("Indices to constant getelementptr must be constants!");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +0000985
986 delete $5;
987
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000988 $$ = ConstantExpr::getGetElementPtr($4, IdxVec);
989 delete $1;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000990 }
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000991 | Types BinaryOps ConstVal ',' ConstVal {
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000992 if ($3->getType() != $5->getType())
993 ThrowException("Binary operator types must match!");
994 if ($1->get() != $3->getType())
995 ThrowException("Return type of binary constant must match arguments!");
996 $$ = ConstantExpr::get($2, $3, $5);
997 delete $1;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000998 }
999 | Types ShiftOps ConstVal ',' ConstVal {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001000 if ($1->get() != $3->getType())
1001 ThrowException("Return type of shift constant must match argument!");
1002 if ($5->getType() != Type::UByteTy)
1003 ThrowException("Shift count for shift constant must be unsigned byte!");
1004
1005 $$ = ConstantExpr::get($2, $3, $5);
1006 delete $1;
Chris Lattner699f1eb2002-08-14 17:12:33 +00001007 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001008
1009
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001010ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001011 if (!ConstantSInt::isValueValidForType($1, $2))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001012 ThrowException("Constant value doesn't fit in type!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001013 $$ = ConstantSInt::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001014 }
1015 | UIntType EUINT64VAL { // integral constants
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001016 if (!ConstantUInt::isValueValidForType($1, $2))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001017 ThrowException("Constant value doesn't fit in type!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001018 $$ = ConstantUInt::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001019 }
1020 | BOOL TRUE { // Boolean constants
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001021 $$ = ConstantBool::True;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001022 }
1023 | BOOL FALSE { // Boolean constants
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001024 $$ = ConstantBool::False;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001025 }
1026 | FPType FPVAL { // Float & Double constants
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001027 $$ = ConstantFP::get($1, $2);
Chris Lattner51727be2002-06-04 21:58:56 +00001028 };
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001029
Chris Lattnere98dda62001-07-14 06:10:16 +00001030// ConstVector - A list of comma seperated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001031ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001032 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001033 }
1034 | ConstVal {
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001035 $$ = new vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001036 $$->push_back($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001037 };
Chris Lattner00950542001-06-06 20:29:01 +00001038
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001039
Chris Lattner1781aca2001-09-18 04:00:54 +00001040// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001041GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001042
Chris Lattner00950542001-06-06 20:29:01 +00001043
Chris Lattner0e73ce62002-05-02 19:11:13 +00001044//===----------------------------------------------------------------------===//
1045// Rules to match Modules
1046//===----------------------------------------------------------------------===//
1047
1048// Module rule: Capture the result of parsing the whole file into a result
1049// variable...
1050//
1051Module : FunctionList {
1052 $$ = ParserResult = $1;
1053 CurModule.ModuleDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001054};
Chris Lattner0e73ce62002-05-02 19:11:13 +00001055
Chris Lattner7e708292002-06-25 16:13:24 +00001056// FunctionList - A list of functions, preceeded by a constant pool.
Chris Lattner0e73ce62002-05-02 19:11:13 +00001057//
1058FunctionList : FunctionList Function {
1059 $$ = $1;
1060 assert($2->getParent() == 0 && "Function already in module!");
1061 $1->getFunctionList().push_back($2);
1062 CurMeth.FunctionDone();
1063 }
1064 | FunctionList FunctionProto {
1065 $$ = $1;
1066 }
1067 | FunctionList IMPLEMENTATION {
1068 $$ = $1;
1069 }
1070 | ConstPool {
1071 $$ = CurModule.CurrentModule;
1072 // Resolve circular types before we parse the body of the module
1073 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001074 };
Chris Lattner0e73ce62002-05-02 19:11:13 +00001075
Chris Lattnere98dda62001-07-14 06:10:16 +00001076// ConstPool - Constants with optional names assigned to them.
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001077ConstPool : ConstPool OptAssign CONST ConstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001078 if (setValueName($4, $2)) { assert(0 && "No redefinitions allowed!"); }
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001079 InsertValue($4);
Chris Lattner00950542001-06-06 20:29:01 +00001080 }
Chris Lattner30c89792001-09-07 16:35:17 +00001081 | ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattner4a42e902001-10-22 05:56:09 +00001082 // Eagerly resolve types. This is not an optimization, this is a
1083 // requirement that is due to the fact that we could have this:
1084 //
1085 // %list = type { %list * }
1086 // %list = type { %list * } ; repeated type decl
1087 //
1088 // If types are not resolved eagerly, then the two types will not be
1089 // determined to be the same type!
1090 //
1091 ResolveTypeTo($2, $4->get());
1092
Chris Lattner1781aca2001-09-18 04:00:54 +00001093 // TODO: FIXME when Type are not const
Chris Lattnerb7474512001-10-03 15:39:04 +00001094 if (!setValueName(const_cast<Type*>($4->get()), $2)) {
1095 // If this is not a redefinition of a type...
1096 if (!$2) {
1097 InsertType($4->get(),
Chris Lattner79df7c02002-03-26 18:01:55 +00001098 inFunctionScope() ? CurMeth.Types : CurModule.Types);
Chris Lattnerb7474512001-10-03 15:39:04 +00001099 }
Chris Lattner30c89792001-09-07 16:35:17 +00001100 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001101
1102 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001103 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001104 | ConstPool FunctionProto { // Function prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001105 }
Chris Lattnerdda71962001-11-26 18:54:16 +00001106 | ConstPool OptAssign OptInternal GlobalType ConstVal {
1107 const Type *Ty = $5->getType();
Chris Lattner1781aca2001-09-18 04:00:54 +00001108 // Global declarations appear in Constant Pool
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001109 Constant *Initializer = $5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001110 if (Initializer == 0)
1111 ThrowException("Global value initializer is not a constant!");
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001112
Chris Lattnerdda71962001-11-26 18:54:16 +00001113 GlobalVariable *GV = new GlobalVariable(Ty, $4, $3, Initializer);
Chris Lattnerb7474512001-10-03 15:39:04 +00001114 if (!setValueName(GV, $2)) { // If not redefining...
1115 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001116 int Slot = InsertValue(GV, CurModule.Values);
1117
1118 if (Slot != -1) {
1119 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1120 } else {
1121 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1122 (char*)GV->getName().c_str()));
1123 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001124 }
Chris Lattner1781aca2001-09-18 04:00:54 +00001125 }
Chris Lattnerdda71962001-11-26 18:54:16 +00001126 | ConstPool OptAssign OptInternal UNINIT GlobalType Types {
1127 const Type *Ty = *$6;
Chris Lattner1781aca2001-09-18 04:00:54 +00001128 // Global declarations appear in Constant Pool
Chris Lattnerdda71962001-11-26 18:54:16 +00001129 GlobalVariable *GV = new GlobalVariable(Ty, $5, $3);
Chris Lattnerb7474512001-10-03 15:39:04 +00001130 if (!setValueName(GV, $2)) { // If not redefining...
1131 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001132 int Slot = InsertValue(GV, CurModule.Values);
1133
1134 if (Slot != -1) {
1135 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1136 } else {
1137 assert(GV->hasName() && "Not named and not numbered!?");
1138 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1139 (char*)GV->getName().c_str()));
1140 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001141 }
Chris Lattner09c07532002-03-31 07:16:49 +00001142 delete $6;
Chris Lattnere98dda62001-07-14 06:10:16 +00001143 }
Chris Lattner00950542001-06-06 20:29:01 +00001144 | /* empty: end of list */ {
Chris Lattner51727be2002-06-04 21:58:56 +00001145 };
Chris Lattner00950542001-06-06 20:29:01 +00001146
1147
1148//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00001149// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00001150//===----------------------------------------------------------------------===//
1151
Chris Lattner51727be2002-06-04 21:58:56 +00001152OptVAR_ID : VAR_ID | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001153
1154ArgVal : Types OptVAR_ID {
Chris Lattner46748042002-04-09 19:41:42 +00001155 $$ = new pair<Argument*, char*>(new Argument(*$1), $2);
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001156 delete $1; // Delete the type handle..
Chris Lattner51727be2002-06-04 21:58:56 +00001157};
Chris Lattner00950542001-06-06 20:29:01 +00001158
1159ArgListH : ArgVal ',' ArgListH {
1160 $$ = $3;
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001161 $3->push_front(*$1);
1162 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001163 }
1164 | ArgVal {
Chris Lattner46748042002-04-09 19:41:42 +00001165 $$ = new list<pair<Argument*,char*> >();
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001166 $$->push_front(*$1);
1167 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001168 }
Chris Lattner8b81bf52001-07-25 22:47:46 +00001169 | DOTDOTDOT {
Chris Lattner46748042002-04-09 19:41:42 +00001170 $$ = new list<pair<Argument*, char*> >();
1171 $$->push_front(pair<Argument*,char*>(new Argument(Type::VoidTy), 0));
Chris Lattner51727be2002-06-04 21:58:56 +00001172 };
Chris Lattner00950542001-06-06 20:29:01 +00001173
1174ArgList : ArgListH {
1175 $$ = $1;
1176 }
1177 | /* empty */ {
1178 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001179 };
Chris Lattner00950542001-06-06 20:29:01 +00001180
Chris Lattner8ebccb72002-05-22 22:33:00 +00001181FuncName : VAR_ID | STRINGCONSTANT;
1182
1183FunctionHeaderH : OptInternal TypesV FuncName '(' ArgList ')' {
Chris Lattnerdda71962001-11-26 18:54:16 +00001184 UnEscapeLexed($3);
Chris Lattner79df7c02002-03-26 18:01:55 +00001185 string FunctionName($3);
Chris Lattnerdda71962001-11-26 18:54:16 +00001186
Chris Lattner30c89792001-09-07 16:35:17 +00001187 vector<const Type*> ParamTypeList;
Chris Lattnerdda71962001-11-26 18:54:16 +00001188 if ($5)
Chris Lattner46748042002-04-09 19:41:42 +00001189 for (list<pair<Argument*,char*> >::iterator I = $5->begin();
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001190 I != $5->end(); ++I)
1191 ParamTypeList.push_back(I->first->getType());
Chris Lattner00950542001-06-06 20:29:01 +00001192
Chris Lattner2079fde2001-10-13 06:41:08 +00001193 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1194 if (isVarArg) ParamTypeList.pop_back();
1195
Chris Lattner79df7c02002-03-26 18:01:55 +00001196 const FunctionType *MT = FunctionType::get(*$2, ParamTypeList, isVarArg);
Chris Lattneref9c23f2001-10-03 14:53:21 +00001197 const PointerType *PMT = PointerType::get(MT);
Chris Lattnerdda71962001-11-26 18:54:16 +00001198 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001199
Chris Lattner79df7c02002-03-26 18:01:55 +00001200 Function *M = 0;
Chris Lattnere1815642001-07-15 06:35:53 +00001201 if (SymbolTable *ST = CurModule.CurrentModule->getSymbolTable()) {
Chris Lattner79df7c02002-03-26 18:01:55 +00001202 // Is the function already in symtab?
1203 if (Value *V = ST->lookup(PMT, FunctionName)) {
1204 M = cast<Function>(V);
Chris Lattner00950542001-06-06 20:29:01 +00001205
Chris Lattnere1815642001-07-15 06:35:53 +00001206 // Yes it is. If this is the case, either we need to be a forward decl,
1207 // or it needs to be.
1208 if (!CurMeth.isDeclare && !M->isExternal())
Chris Lattner7e708292002-06-25 16:13:24 +00001209 ThrowException("Redefinition of function '" + FunctionName + "'!");
Chris Lattner34538142002-03-08 19:11:42 +00001210
Chris Lattner5659dd12002-07-15 00:10:33 +00001211 // Make sure that we keep track of the internal marker, even if there was
1212 // a previous "declare".
1213 if ($1)
1214 M->setInternalLinkage(true);
1215
Chris Lattner7e708292002-06-25 16:13:24 +00001216 // If we found a preexisting function prototype, remove it from the
1217 // module, so that we don't get spurious conflicts with global & local
1218 // variables.
Chris Lattner34538142002-03-08 19:11:42 +00001219 //
Chris Lattner79df7c02002-03-26 18:01:55 +00001220 CurModule.CurrentModule->getFunctionList().remove(M);
Chris Lattnere1815642001-07-15 06:35:53 +00001221 }
1222 }
1223
1224 if (M == 0) { // Not already defined?
Chris Lattner79df7c02002-03-26 18:01:55 +00001225 M = new Function(MT, $1, FunctionName);
Chris Lattnere1815642001-07-15 06:35:53 +00001226 InsertValue(M, CurModule.Values);
Chris Lattnerdda71962001-11-26 18:54:16 +00001227 CurModule.DeclareNewGlobalValue(M, ValID::create($3));
Chris Lattnere1815642001-07-15 06:35:53 +00001228 }
Chris Lattnerdda71962001-11-26 18:54:16 +00001229 free($3); // Free strdup'd memory!
Chris Lattner00950542001-06-06 20:29:01 +00001230
Chris Lattner79df7c02002-03-26 18:01:55 +00001231 CurMeth.FunctionStart(M);
Chris Lattner00950542001-06-06 20:29:01 +00001232
Chris Lattner7e708292002-06-25 16:13:24 +00001233 // Add all of the arguments we parsed to the function...
Chris Lattnerdda71962001-11-26 18:54:16 +00001234 if ($5 && !CurMeth.isDeclare) { // Is null if empty...
Chris Lattner46748042002-04-09 19:41:42 +00001235 for (list<pair<Argument*, char*> >::iterator I = $5->begin();
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001236 I != $5->end(); ++I) {
1237 if (setValueName(I->first, I->second)) { // Insert into symtab...
1238 assert(0 && "No arg redef allowed!");
1239 }
1240
1241 InsertValue(I->first);
Chris Lattner7e708292002-06-25 16:13:24 +00001242 M->getArgumentList().push_back(I->first);
Chris Lattner00950542001-06-06 20:29:01 +00001243 }
Chris Lattnerdda71962001-11-26 18:54:16 +00001244 delete $5; // We're now done with the argument list
Chris Lattner9176fe42002-03-08 18:57:56 +00001245 } else if ($5) {
1246 // If we are a declaration, we should free the memory for the argument list!
Chris Lattner46748042002-04-09 19:41:42 +00001247 for (list<pair<Argument*, char*> >::iterator I = $5->begin(), E = $5->end();
1248 I != E; ++I) {
Chris Lattner9176fe42002-03-08 18:57:56 +00001249 if (I->second) free(I->second); // Free the memory for the name...
Chris Lattner09c07532002-03-31 07:16:49 +00001250 delete I->first; // Free the unused function argument
1251 }
Chris Lattner9176fe42002-03-08 18:57:56 +00001252 delete $5; // Free the memory for the list itself
Chris Lattner00950542001-06-06 20:29:01 +00001253 }
Chris Lattner51727be2002-06-04 21:58:56 +00001254};
Chris Lattner00950542001-06-06 20:29:01 +00001255
Chris Lattner9b02cc32002-05-03 18:23:48 +00001256BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1257
1258FunctionHeader : FunctionHeaderH BEGIN {
Chris Lattner79df7c02002-03-26 18:01:55 +00001259 $$ = CurMeth.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00001260
Chris Lattner7e708292002-06-25 16:13:24 +00001261 // Resolve circular types before we parse the body of the function.
Chris Lattner30c89792001-09-07 16:35:17 +00001262 ResolveTypes(CurMeth.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001263};
Chris Lattner00950542001-06-06 20:29:01 +00001264
Chris Lattner9b02cc32002-05-03 18:23:48 +00001265END : ENDTOK | '}'; // Allow end of '}' to end a function
1266
Chris Lattner79df7c02002-03-26 18:01:55 +00001267Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00001268 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001269};
Chris Lattner00950542001-06-06 20:29:01 +00001270
Chris Lattner79df7c02002-03-26 18:01:55 +00001271FunctionProto : DECLARE { CurMeth.isDeclare = true; } FunctionHeaderH {
1272 $$ = CurMeth.CurrentFunction;
1273 assert($$->getParent() == 0 && "Function already in module!");
1274 CurModule.CurrentModule->getFunctionList().push_back($$);
1275 CurMeth.FunctionDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001276};
Chris Lattner00950542001-06-06 20:29:01 +00001277
1278//===----------------------------------------------------------------------===//
1279// Rules to match Basic Blocks
1280//===----------------------------------------------------------------------===//
1281
1282ConstValueRef : ESINT64VAL { // A reference to a direct constant
1283 $$ = ValID::create($1);
1284 }
1285 | EUINT64VAL {
1286 $$ = ValID::create($1);
1287 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001288 | FPVAL { // Perhaps it's an FP constant?
1289 $$ = ValID::create($1);
1290 }
Chris Lattner00950542001-06-06 20:29:01 +00001291 | TRUE {
1292 $$ = ValID::create((int64_t)1);
1293 }
1294 | FALSE {
1295 $$ = ValID::create((int64_t)0);
1296 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001297 | NULL_TOK {
1298 $$ = ValID::createNull();
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001299 }
1300 ;
Chris Lattner1a1cb112001-09-30 22:46:54 +00001301
Chris Lattner2079fde2001-10-13 06:41:08 +00001302// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1303// another value.
1304//
1305SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001306 $$ = ValID::create($1);
1307 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001308 | VAR_ID { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001309 $$ = ValID::create($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001310 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001311
1312// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00001313ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00001314
Chris Lattner00950542001-06-06 20:29:01 +00001315
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001316// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1317// type immediately preceeds the value reference, and allows complex constant
1318// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001319ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001320 $$ = getVal(*$1, $2); delete $1;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001321 }
1322 | ConstExpr {
1323 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001324 };
Chris Lattner8b81bf52001-07-25 22:47:46 +00001325
Chris Lattner00950542001-06-06 20:29:01 +00001326BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner7e708292002-06-25 16:13:24 +00001327 ($$ = $1)->getBasicBlockList().push_back($2);
Chris Lattner00950542001-06-06 20:29:01 +00001328 }
Chris Lattner7e708292002-06-25 16:13:24 +00001329 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
1330 ($$ = $1)->getBasicBlockList().push_back($2);
Chris Lattner51727be2002-06-04 21:58:56 +00001331 };
Chris Lattner00950542001-06-06 20:29:01 +00001332
1333
1334// Basic blocks are terminated by branching instructions:
1335// br, br/cc, switch, ret
1336//
Chris Lattner2079fde2001-10-13 06:41:08 +00001337BasicBlock : InstructionList OptAssign BBTerminatorInst {
1338 if (setValueName($3, $2)) { assert(0 && "No redefn allowed!"); }
1339 InsertValue($3);
1340
1341 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001342 InsertValue($1);
1343 $$ = $1;
1344 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001345 | LABELSTR InstructionList OptAssign BBTerminatorInst {
1346 if (setValueName($4, $3)) { assert(0 && "No redefn allowed!"); }
1347 InsertValue($4);
1348
1349 $2->getInstList().push_back($4);
Chris Lattnerb7474512001-10-03 15:39:04 +00001350 if (setValueName($2, $1)) { assert(0 && "No label redef allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001351
1352 InsertValue($2);
1353 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001354 };
Chris Lattner00950542001-06-06 20:29:01 +00001355
1356InstructionList : InstructionList Inst {
1357 $1->getInstList().push_back($2);
1358 $$ = $1;
1359 }
1360 | /* empty */ {
1361 $$ = new BasicBlock();
Chris Lattner51727be2002-06-04 21:58:56 +00001362 };
Chris Lattner00950542001-06-06 20:29:01 +00001363
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001364BBTerminatorInst : RET ResolvedVal { // Return with a result...
1365 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001366 }
1367 | RET VOID { // Return with no result...
1368 $$ = new ReturnInst();
1369 }
1370 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner9636a912001-10-01 16:18:37 +00001371 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $3)));
Chris Lattner00950542001-06-06 20:29:01 +00001372 } // Conditional Branch...
1373 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner9636a912001-10-01 16:18:37 +00001374 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $6)),
1375 cast<BasicBlock>(getVal(Type::LabelTy, $9)),
Chris Lattner00950542001-06-06 20:29:01 +00001376 getVal(Type::BoolTy, $3));
1377 }
1378 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1379 SwitchInst *S = new SwitchInst(getVal($2, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001380 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
Chris Lattner00950542001-06-06 20:29:01 +00001381 $$ = S;
1382
Chris Lattner46748042002-04-09 19:41:42 +00001383 vector<pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
1384 E = $8->end();
1385 for (; I != E; ++I)
Chris Lattner00950542001-06-06 20:29:01 +00001386 S->dest_push_back(I->first, I->second);
1387 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001388 | INVOKE TypesV ValueRef '(' ValueRefListE ')' TO ResolvedVal
1389 EXCEPT ResolvedVal {
1390 const PointerType *PMTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001391 const FunctionType *Ty;
Chris Lattner2079fde2001-10-13 06:41:08 +00001392
1393 if (!(PMTy = dyn_cast<PointerType>($2->get())) ||
Chris Lattner79df7c02002-03-26 18:01:55 +00001394 !(Ty = dyn_cast<FunctionType>(PMTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00001395 // Pull out the types of all of the arguments...
1396 vector<const Type*> ParamTypes;
1397 if ($5) {
Chris Lattner6cdb0112001-11-26 16:54:11 +00001398 for (vector<Value*>::iterator I = $5->begin(), E = $5->end(); I!=E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00001399 ParamTypes.push_back((*I)->getType());
1400 }
1401
1402 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1403 if (isVarArg) ParamTypes.pop_back();
1404
Chris Lattner79df7c02002-03-26 18:01:55 +00001405 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattner2079fde2001-10-13 06:41:08 +00001406 PMTy = PointerType::get(Ty);
1407 }
1408 delete $2;
1409
Chris Lattner7e708292002-06-25 16:13:24 +00001410 Value *V = getVal(PMTy, $3); // Get the function we're calling...
Chris Lattner2079fde2001-10-13 06:41:08 +00001411
1412 BasicBlock *Normal = dyn_cast<BasicBlock>($8);
1413 BasicBlock *Except = dyn_cast<BasicBlock>($10);
1414
1415 if (Normal == 0 || Except == 0)
1416 ThrowException("Invoke instruction without label destinations!");
1417
1418 // Create the call node...
1419 if (!$5) { // Has no arguments?
Chris Lattner386a3b72001-10-16 19:54:17 +00001420 $$ = new InvokeInst(V, Normal, Except, vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00001421 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001422 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00001423 // correctly!
1424 //
Chris Lattner79df7c02002-03-26 18:01:55 +00001425 FunctionType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
1426 FunctionType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
Chris Lattner6cdb0112001-11-26 16:54:11 +00001427 vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00001428
1429 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1430 if ((*ArgI)->getType() != *I)
1431 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001432 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001433
1434 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1435 ThrowException("Invalid number of parameters detected!");
1436
Chris Lattner6cdb0112001-11-26 16:54:11 +00001437 $$ = new InvokeInst(V, Normal, Except, *$5);
Chris Lattner2079fde2001-10-13 06:41:08 +00001438 }
1439 delete $5;
Chris Lattner51727be2002-06-04 21:58:56 +00001440 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001441
1442
Chris Lattner00950542001-06-06 20:29:01 +00001443
1444JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1445 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001446 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001447 if (V == 0)
1448 ThrowException("May only switch on a constant pool value!");
1449
Chris Lattner9636a912001-10-01 16:18:37 +00001450 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($5, $6))));
Chris Lattner00950542001-06-06 20:29:01 +00001451 }
1452 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner46748042002-04-09 19:41:42 +00001453 $$ = new vector<pair<Constant*, BasicBlock*> >();
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001454 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00001455
1456 if (V == 0)
1457 ThrowException("May only switch on a constant pool value!");
1458
Chris Lattner9636a912001-10-01 16:18:37 +00001459 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($4, $5))));
Chris Lattner51727be2002-06-04 21:58:56 +00001460 };
Chris Lattner00950542001-06-06 20:29:01 +00001461
1462Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001463 // Is this definition named?? if so, assign the name...
1464 if (setValueName($2, $1)) { assert(0 && "No redefin allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001465 InsertValue($2);
1466 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001467};
Chris Lattner00950542001-06-06 20:29:01 +00001468
Chris Lattnerc24d2082001-06-11 15:04:20 +00001469PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
1470 $$ = new list<pair<Value*, BasicBlock*> >();
Chris Lattner30c89792001-09-07 16:35:17 +00001471 $$->push_back(make_pair(getVal(*$1, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001472 cast<BasicBlock>(getVal(Type::LabelTy, $5))));
Chris Lattner30c89792001-09-07 16:35:17 +00001473 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001474 }
1475 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1476 $$ = $1;
1477 $1->push_back(make_pair(getVal($1->front().first->getType(), $4),
Chris Lattner9636a912001-10-01 16:18:37 +00001478 cast<BasicBlock>(getVal(Type::LabelTy, $6))));
Chris Lattner51727be2002-06-04 21:58:56 +00001479 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00001480
1481
Chris Lattner30c89792001-09-07 16:35:17 +00001482ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner6cdb0112001-11-26 16:54:11 +00001483 $$ = new vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001484 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001485 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001486 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001487 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001488 $1->push_back($3);
Chris Lattner51727be2002-06-04 21:58:56 +00001489 };
Chris Lattner00950542001-06-06 20:29:01 +00001490
1491// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Chris Lattner51727be2002-06-04 21:58:56 +00001492ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001493
1494InstVal : BinaryOps Types ValueRef ',' ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001495 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001496 if ($$ == 0)
1497 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001498 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001499 }
Chris Lattner699f1eb2002-08-14 17:12:33 +00001500 | NOT ResolvedVal {
1501 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
1502 << " Replacing with 'xor'.\n";
1503
1504 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
1505 if (Ones == 0)
1506 ThrowException("Expected integral type for not instruction!");
1507
1508 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
Chris Lattner00950542001-06-06 20:29:01 +00001509 if ($$ == 0)
Chris Lattner699f1eb2002-08-14 17:12:33 +00001510 ThrowException("Could not create a xor instruction!");
Chris Lattner09083092001-07-08 04:57:15 +00001511 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001512 | ShiftOps ResolvedVal ',' ResolvedVal {
1513 if ($4->getType() != Type::UByteTy)
1514 ThrowException("Shift amount must be ubyte!");
1515 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001516 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001517 | CAST ResolvedVal TO Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001518 $$ = new CastInst($2, *$4);
1519 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001520 }
Chris Lattnerc24d2082001-06-11 15:04:20 +00001521 | PHI PHIList {
1522 const Type *Ty = $2->front().first->getType();
1523 $$ = new PHINode(Ty);
Chris Lattner00950542001-06-06 20:29:01 +00001524 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001525 if ($2->front().first->getType() != Ty)
1526 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00001527 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001528 $2->pop_front();
1529 }
1530 delete $2; // Free the list...
1531 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001532 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattneref9c23f2001-10-03 14:53:21 +00001533 const PointerType *PMTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001534 const FunctionType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001535
Chris Lattneref9c23f2001-10-03 14:53:21 +00001536 if (!(PMTy = dyn_cast<PointerType>($2->get())) ||
Chris Lattner79df7c02002-03-26 18:01:55 +00001537 !(Ty = dyn_cast<FunctionType>(PMTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001538 // Pull out the types of all of the arguments...
1539 vector<const Type*> ParamTypes;
Chris Lattneref9c23f2001-10-03 14:53:21 +00001540 if ($5) {
Chris Lattner6cdb0112001-11-26 16:54:11 +00001541 for (vector<Value*>::iterator I = $5->begin(), E = $5->end(); I!=E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00001542 ParamTypes.push_back((*I)->getType());
1543 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001544
1545 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1546 if (isVarArg) ParamTypes.pop_back();
1547
Chris Lattner79df7c02002-03-26 18:01:55 +00001548 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattneref9c23f2001-10-03 14:53:21 +00001549 PMTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001550 }
Chris Lattner30c89792001-09-07 16:35:17 +00001551 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001552
Chris Lattner7e708292002-06-25 16:13:24 +00001553 Value *V = getVal(PMTy, $3); // Get the function we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00001554
Chris Lattner8b81bf52001-07-25 22:47:46 +00001555 // Create the call node...
1556 if (!$5) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00001557 // Make sure no arguments is a good thing!
1558 if (Ty->getNumParams() != 0)
1559 ThrowException("No arguments passed to a function that "
1560 "expects arguments!");
1561
Chris Lattner386a3b72001-10-16 19:54:17 +00001562 $$ = new CallInst(V, vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00001563 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001564 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner00950542001-06-06 20:29:01 +00001565 // correctly!
1566 //
Chris Lattner79df7c02002-03-26 18:01:55 +00001567 FunctionType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
1568 FunctionType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
Chris Lattner6cdb0112001-11-26 16:54:11 +00001569 vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001570
1571 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1572 if ((*ArgI)->getType() != *I)
1573 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001574 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00001575
Chris Lattner8b81bf52001-07-25 22:47:46 +00001576 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner00950542001-06-06 20:29:01 +00001577 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00001578
Chris Lattner6cdb0112001-11-26 16:54:11 +00001579 $$ = new CallInst(V, *$5);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001580 }
1581 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00001582 }
1583 | MemoryInst {
1584 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001585 };
Chris Lattner00950542001-06-06 20:29:01 +00001586
Chris Lattner6cdb0112001-11-26 16:54:11 +00001587
1588// IndexList - List of indices for GEP based instructions...
1589IndexList : ',' ValueRefList {
Chris Lattner027dcc52001-07-08 21:10:27 +00001590 $$ = $2;
1591} | /* empty */ {
Chris Lattner6cdb0112001-11-26 16:54:11 +00001592 $$ = new vector<Value*>();
Chris Lattner51727be2002-06-04 21:58:56 +00001593};
Chris Lattner027dcc52001-07-08 21:10:27 +00001594
Chris Lattner00950542001-06-06 20:29:01 +00001595MemoryInst : MALLOC Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001596 $$ = new MallocInst(PointerType::get(*$2));
1597 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001598 }
1599 | MALLOC Types ',' UINT ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001600 const Type *Ty = PointerType::get(*$2);
Chris Lattner8896eda2001-07-09 19:38:36 +00001601 $$ = new MallocInst(Ty, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001602 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001603 }
1604 | ALLOCA Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001605 $$ = new AllocaInst(PointerType::get(*$2));
1606 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001607 }
1608 | ALLOCA Types ',' UINT ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001609 const Type *Ty = PointerType::get(*$2);
Chris Lattner00950542001-06-06 20:29:01 +00001610 Value *ArrSize = getVal($4, $5);
Chris Lattnerf0d0e9c2001-07-07 08:36:30 +00001611 $$ = new AllocaInst(Ty, ArrSize);
Chris Lattner30c89792001-09-07 16:35:17 +00001612 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001613 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001614 | FREE ResolvedVal {
Chris Lattner9b625032002-05-06 16:15:30 +00001615 if (!isa<PointerType>($2->getType()))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001616 ThrowException("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00001617 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001618 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001619 }
1620
Chris Lattner6cdb0112001-11-26 16:54:11 +00001621 | LOAD Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00001622 if (!isa<PointerType>($2->get()))
Chris Lattner2079fde2001-10-13 06:41:08 +00001623 ThrowException("Can't load from nonpointer type: " +
1624 (*$2)->getDescription());
Chris Lattner30c89792001-09-07 16:35:17 +00001625 if (LoadInst::getIndexedType(*$2, *$4) == 0)
Chris Lattner027dcc52001-07-08 21:10:27 +00001626 ThrowException("Invalid indices for load instruction!");
1627
Chris Lattner30c89792001-09-07 16:35:17 +00001628 $$ = new LoadInst(getVal(*$2, $3), *$4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001629 delete $4; // Free the vector...
Chris Lattner30c89792001-09-07 16:35:17 +00001630 delete $2;
Chris Lattner027dcc52001-07-08 21:10:27 +00001631 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00001632 | STORE ResolvedVal ',' Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00001633 if (!isa<PointerType>($4->get()))
Chris Lattner72e00252001-12-14 16:28:42 +00001634 ThrowException("Can't store to a nonpointer type: " +
1635 (*$4)->getDescription());
Chris Lattner30c89792001-09-07 16:35:17 +00001636 const Type *ElTy = StoreInst::getIndexedType(*$4, *$6);
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001637 if (ElTy == 0)
1638 ThrowException("Can't store into that field list!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001639 if (ElTy != $2->getType())
Chris Lattner72e00252001-12-14 16:28:42 +00001640 ThrowException("Can't store '" + $2->getType()->getDescription() +
1641 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001642 $$ = new StoreInst($2, getVal(*$4, $5), *$6);
1643 delete $4; delete $6;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001644 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00001645 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00001646 if (!isa<PointerType>($2->get()))
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001647 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner30c89792001-09-07 16:35:17 +00001648 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattner72e00252001-12-14 16:28:42 +00001649 ThrowException("Can't get element ptr '" + (*$2)->getDescription()+ "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001650 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
1651 delete $2; delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00001652 };
Chris Lattner027dcc52001-07-08 21:10:27 +00001653
Chris Lattner00950542001-06-06 20:29:01 +00001654%%
Chris Lattner09083092001-07-08 04:57:15 +00001655int yyerror(const char *ErrorMsg) {
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001656 string where = string((CurFilename == "-")? string("<stdin>") : CurFilename)
1657 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
1658 string errMsg = string(ErrorMsg) + string("\n") + where + " while reading ";
1659 if (yychar == YYEMPTY)
1660 errMsg += "end-of-file.";
1661 else
1662 errMsg += "token: '" + string(llvmAsmtext, llvmAsmleng) + "'";
1663 ThrowException(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00001664 return 0;
1665}