blob: 6f40ca6ac2d1dd4a7a080ed769c32b289439f0de [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===-- llvmAsmParser.y - Parser for llvm assembly files --------*- C++ -*-===//
John Criswell856ba762003-10-21 15:17:13 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
10// This file implements the bison parser for LLVM assembly languages files.
11//
Chris Lattnercf3056d2003-10-13 03:32:08 +000012//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +000013
Chris Lattner00950542001-06-06 20:29:01 +000014%{
15#include "ParserInternals.h"
Chris Lattnera8e8f162005-05-06 20:27:19 +000016#include "llvm/CallingConv.h"
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000017#include "llvm/Instructions.h"
Chris Lattner00950542001-06-06 20:29:01 +000018#include "llvm/Module.h"
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000019#include "llvm/SymbolTable.h"
Chris Lattner830b6f92004-04-05 01:30:04 +000020#include "llvm/Support/GetElementPtrTypeIterator.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000021#include "llvm/ADT/STLExtras.h"
Reid Spencer8ce1da72004-07-04 12:19:05 +000022#include <algorithm>
23#include <iostream>
Chris Lattner00950542001-06-06 20:29:01 +000024#include <list>
Chris Lattnerc188eeb2002-07-30 18:54:25 +000025#include <utility>
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
Brian Gaeked0fde302003-11-11 22:41:34 +000031namespace llvm {
Chris Lattner86427632004-07-14 06:39:48 +000032 std::string CurFilename;
33}
34using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000035
Chris Lattner00950542001-06-06 20:29:01 +000036static Module *ParserResult;
Chris Lattner00950542001-06-06 20:29:01 +000037
Chris Lattner30c89792001-09-07 16:35:17 +000038// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
39// relating to upreferences in the input stream.
40//
41//#define DEBUG_UPREFS 1
42#ifdef DEBUG_UPREFS
Chris Lattner699f1eb2002-08-14 17:12:33 +000043#define UR_OUT(X) std::cerr << X
Chris Lattner30c89792001-09-07 16:35:17 +000044#else
45#define UR_OUT(X)
46#endif
47
Vikram S. Adved3f7eb02002-07-14 22:59:28 +000048#define YYERROR_VERBOSE 1
49
Chris Lattner7e708292002-06-25 16:13:24 +000050// This contains info used when building the body of a function. It is
51// destroyed when the function is completed.
Chris Lattner00950542001-06-06 20:29:01 +000052//
Chris Lattner9232b992003-04-22 18:42:41 +000053typedef std::vector<Value *> ValueList; // Numbered defs
Chris Lattner735f2702004-07-08 22:30:50 +000054static void ResolveDefinitions(std::map<const Type *,ValueList> &LateResolvers,
55 std::map<const Type *,ValueList> *FutureLateResolvers = 0);
Chris Lattner00950542001-06-06 20:29:01 +000056
57static struct PerModuleInfo {
58 Module *CurrentModule;
Chris Lattner735f2702004-07-08 22:30:50 +000059 std::map<const Type *, ValueList> Values; // Module level numbered definitions
60 std::map<const Type *,ValueList> LateResolveValues;
Chris Lattner16af11d2004-02-09 21:03:38 +000061 std::vector<PATypeHolder> Types;
Chris Lattner9232b992003-04-22 18:42:41 +000062 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner00950542001-06-06 20:29:01 +000063
Chris Lattnerbc721ed2004-07-13 08:28:21 +000064 /// PlaceHolderInfo - When temporary placeholder objects are created, remember
65 /// how they were referenced and one which line of the input they came from so
Chris Lattner22ae2322004-07-13 08:39:15 +000066 /// that we can resolve them later and print error messages as appropriate.
Chris Lattnerbc721ed2004-07-13 08:28:21 +000067 std::map<Value*, std::pair<ValID, int> > PlaceHolderInfo;
68
Chris Lattner2079fde2001-10-13 06:41:08 +000069 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
70 // references to global values. Global values may be referenced before they
71 // are defined, and if so, the temporary object that they represent is held
Reid Spencer3271ed52004-07-18 00:08:11 +000072 // here. This is used for forward references of GlobalValues.
Chris Lattner2079fde2001-10-13 06:41:08 +000073 //
Chris Lattner9232b992003-04-22 18:42:41 +000074 typedef std::map<std::pair<const PointerType *,
Chris Lattnerbc207592004-03-08 06:09:57 +000075 ValID>, GlobalValue*> GlobalRefsType;
Chris Lattner2079fde2001-10-13 06:41:08 +000076 GlobalRefsType GlobalRefs;
77
Chris Lattner00950542001-06-06 20:29:01 +000078 void ModuleDone() {
Chris Lattner7e708292002-06-25 16:13:24 +000079 // If we could not resolve some functions at function compilation time
80 // (calls to functions before they are defined), resolve them now... Types
81 // are resolved when the constant pool has been completely parsed.
Chris Lattner30c89792001-09-07 16:35:17 +000082 //
Chris Lattner00950542001-06-06 20:29:01 +000083 ResolveDefinitions(LateResolveValues);
84
Chris Lattner2079fde2001-10-13 06:41:08 +000085 // Check to make sure that all global value forward references have been
86 // resolved!
87 //
88 if (!GlobalRefs.empty()) {
Chris Lattner9232b992003-04-22 18:42:41 +000089 std::string UndefinedReferences = "Unresolved global references exist:\n";
Chris Lattner749ce032002-03-11 22:12:39 +000090
91 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
92 I != E; ++I) {
93 UndefinedReferences += " " + I->first.first->getDescription() + " " +
94 I->first.second.getName() + "\n";
95 }
96 ThrowException(UndefinedReferences);
Chris Lattner2079fde2001-10-13 06:41:08 +000097 }
98
Chris Lattner7e708292002-06-25 16:13:24 +000099 Values.clear(); // Clear out function local definitions
Chris Lattner30c89792001-09-07 16:35:17 +0000100 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +0000101 CurrentModule = 0;
102 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000103
104
Chris Lattner8a327842004-07-14 21:44:00 +0000105 // GetForwardRefForGlobal - Check to see if there is a forward reference
106 // for this global. If so, remove it from the GlobalRefs map and return it.
107 // If not, just return null.
108 GlobalValue *GetForwardRefForGlobal(const PointerType *PTy, ValID ID) {
109 // Check to see if there is a forward reference to this global variable...
110 // if there is, eliminate it and patch the reference to use the new def'n.
111 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(PTy, ID));
112 GlobalValue *Ret = 0;
113 if (I != GlobalRefs.end()) {
114 Ret = I->second;
115 GlobalRefs.erase(I);
116 }
117 return Ret;
118 }
Chris Lattner00950542001-06-06 20:29:01 +0000119} CurModule;
120
Chris Lattner79df7c02002-03-26 18:01:55 +0000121static struct PerFunctionInfo {
Chris Lattner7e708292002-06-25 16:13:24 +0000122 Function *CurrentFunction; // Pointer to current function being created
Chris Lattner00950542001-06-06 20:29:01 +0000123
Chris Lattner735f2702004-07-08 22:30:50 +0000124 std::map<const Type*, ValueList> Values; // Keep track of #'d definitions
125 std::map<const Type*, ValueList> LateResolveValues;
Chris Lattner7e708292002-06-25 16:13:24 +0000126 bool isDeclare; // Is this function a forward declararation?
Chris Lattner00950542001-06-06 20:29:01 +0000127
Chris Lattner2e2ed292004-07-14 06:28:35 +0000128 /// BBForwardRefs - When we see forward references to basic blocks, keep
129 /// track of them here.
130 std::map<BasicBlock*, std::pair<ValID, int> > BBForwardRefs;
131 std::vector<BasicBlock*> NumberedBlocks;
132 unsigned NextBBNum;
133
Chris Lattner79df7c02002-03-26 18:01:55 +0000134 inline PerFunctionInfo() {
135 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000136 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000137 }
138
Chris Lattner79df7c02002-03-26 18:01:55 +0000139 inline void FunctionStart(Function *M) {
140 CurrentFunction = M;
Chris Lattner2e2ed292004-07-14 06:28:35 +0000141 NextBBNum = 0;
Chris Lattner00950542001-06-06 20:29:01 +0000142 }
143
Chris Lattner79df7c02002-03-26 18:01:55 +0000144 void FunctionDone() {
Chris Lattner2e2ed292004-07-14 06:28:35 +0000145 NumberedBlocks.clear();
146
147 // Any forward referenced blocks left?
148 if (!BBForwardRefs.empty())
149 ThrowException("Undefined reference to label " +
Chris Lattner83beacd2005-02-24 04:59:49 +0000150 BBForwardRefs.begin()->first->getName());
Chris Lattner2e2ed292004-07-14 06:28:35 +0000151
152 // Resolve all forward references now.
Chris Lattner386a3b72001-10-16 19:54:17 +0000153 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000154
Chris Lattner7e708292002-06-25 16:13:24 +0000155 Values.clear(); // Clear out function local definitions
Chris Lattner79df7c02002-03-26 18:01:55 +0000156 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000157 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000158 }
Chris Lattner394f2eb2003-10-16 20:12:13 +0000159} CurFun; // Info for the current function...
Chris Lattner00950542001-06-06 20:29:01 +0000160
Chris Lattner394f2eb2003-10-16 20:12:13 +0000161static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000162
Chris Lattner00950542001-06-06 20:29:01 +0000163
164//===----------------------------------------------------------------------===//
165// Code to handle definitions of all the types
166//===----------------------------------------------------------------------===//
167
Chris Lattner735f2702004-07-08 22:30:50 +0000168static int InsertValue(Value *V,
169 std::map<const Type*,ValueList> &ValueTab = CurFun.Values) {
170 if (V->hasName()) return -1; // Is this a numbered definition?
Chris Lattner2079fde2001-10-13 06:41:08 +0000171
172 // Yes, insert the value into the value table...
Chris Lattner735f2702004-07-08 22:30:50 +0000173 ValueList &List = ValueTab[V->getType()];
174 List.push_back(V);
Chris Lattner16af11d2004-02-09 21:03:38 +0000175 return List.size()-1;
Chris Lattner00950542001-06-06 20:29:01 +0000176}
177
Chris Lattner30c89792001-09-07 16:35:17 +0000178static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000179 switch (D.Type) {
Chris Lattnera09000d2004-07-14 23:03:46 +0000180 case ValID::NumberVal: // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000181 // Module constants occupy the lowest numbered slots...
Chris Lattnera09000d2004-07-14 23:03:46 +0000182 if ((unsigned)D.Num < CurModule.Types.size())
183 return CurModule.Types[(unsigned)D.Num];
Chris Lattner42c9e772001-10-20 09:32:59 +0000184 break;
Chris Lattnera09000d2004-07-14 23:03:46 +0000185 case ValID::NameVal: // Is it a named definition?
186 if (const Type *N = CurModule.CurrentModule->getTypeByName(D.Name)) {
187 D.destroy(); // Free old strdup'd memory...
188 return N;
Chris Lattner6e6026b2002-11-20 18:36:02 +0000189 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000190 break;
Chris Lattner30c89792001-09-07 16:35:17 +0000191 default:
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000192 ThrowException("Internal parser error: Invalid symbol type reference!");
Chris Lattner30c89792001-09-07 16:35:17 +0000193 }
194
195 // If we reached here, we referenced either a symbol that we don't know about
196 // or an id number that hasn't been read yet. We may be referencing something
197 // forward, so just create an entry to be resolved later and get to it...
198 //
199 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
200
Chris Lattner355ad1f2005-03-21 06:27:42 +0000201
202 if (inFunctionScope()) {
203 if (D.Type == ValID::NameVal)
204 ThrowException("Reference to an undefined type: '" + D.getName() + "'");
205 else
206 ThrowException("Reference to an undefined type: #" + itostr(D.Num));
Chris Lattner4a42e902001-10-22 05:56:09 +0000207 }
Chris Lattner30c89792001-09-07 16:35:17 +0000208
Chris Lattner355ad1f2005-03-21 06:27:42 +0000209 std::map<ValID, PATypeHolder>::iterator I =CurModule.LateResolveTypes.find(D);
210 if (I != CurModule.LateResolveTypes.end())
211 return I->second;
212
Chris Lattner82269592001-10-22 06:01:08 +0000213 Type *Typ = OpaqueType::get();
Chris Lattner355ad1f2005-03-21 06:27:42 +0000214 CurModule.LateResolveTypes.insert(std::make_pair(D, Typ));
Chris Lattner30c89792001-09-07 16:35:17 +0000215 return Typ;
Chris Lattner355ad1f2005-03-21 06:27:42 +0000216 }
Chris Lattner30c89792001-09-07 16:35:17 +0000217
Chris Lattner9232b992003-04-22 18:42:41 +0000218static Value *lookupInSymbolTable(const Type *Ty, const std::string &Name) {
Chris Lattner6e6026b2002-11-20 18:36:02 +0000219 SymbolTable &SymTab =
Chris Lattner394f2eb2003-10-16 20:12:13 +0000220 inFunctionScope() ? CurFun.CurrentFunction->getSymbolTable() :
Chris Lattner9705a152002-05-02 19:27:42 +0000221 CurModule.CurrentModule->getSymbolTable();
Chris Lattner6e6026b2002-11-20 18:36:02 +0000222 return SymTab.lookup(Ty, Name);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000223}
224
Chris Lattner2079fde2001-10-13 06:41:08 +0000225// getValNonImprovising - Look up the value specified by the provided type and
226// the provided ValID. If the value exists and has already been defined, return
227// it. Otherwise return null.
228//
229static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000230 if (isa<FunctionType>(Ty))
231 ThrowException("Functions are not values and "
232 "must be referenced as pointers");
Chris Lattner386a3b72001-10-16 19:54:17 +0000233
Chris Lattner30c89792001-09-07 16:35:17 +0000234 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000235 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner00950542001-06-06 20:29:01 +0000236 unsigned Num = (unsigned)D.Num;
237
238 // Module constants occupy the lowest numbered slots...
Chris Lattner735f2702004-07-08 22:30:50 +0000239 std::map<const Type*,ValueList>::iterator VI = CurModule.Values.find(Ty);
Chris Lattner16af11d2004-02-09 21:03:38 +0000240 if (VI != CurModule.Values.end()) {
241 if (Num < VI->second.size())
242 return VI->second[Num];
243 Num -= VI->second.size();
Chris Lattner00950542001-06-06 20:29:01 +0000244 }
245
246 // Make sure that our type is within bounds
Chris Lattner735f2702004-07-08 22:30:50 +0000247 VI = CurFun.Values.find(Ty);
Chris Lattner16af11d2004-02-09 21:03:38 +0000248 if (VI == CurFun.Values.end()) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000249
250 // Check that the number is within bounds...
Chris Lattner16af11d2004-02-09 21:03:38 +0000251 if (VI->second.size() <= Num) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000252
Chris Lattner16af11d2004-02-09 21:03:38 +0000253 return VI->second[Num];
Chris Lattner00950542001-06-06 20:29:01 +0000254 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000255
Chris Lattner1a1cb112001-09-30 22:46:54 +0000256 case ValID::NameVal: { // Is it a named definition?
Chris Lattner9232b992003-04-22 18:42:41 +0000257 Value *N = lookupInSymbolTable(Ty, std::string(D.Name));
Chris Lattner2079fde2001-10-13 06:41:08 +0000258 if (N == 0) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000259
260 D.destroy(); // Free old strdup'd memory...
261 return N;
262 }
263
Chris Lattner2079fde2001-10-13 06:41:08 +0000264 // Check to make sure that "Ty" is an integral type, and that our
265 // value will fit into the specified type...
266 case ValID::ConstSIntVal: // Is it a constant pool reference??
Chris Lattnerd78700d2002-08-16 21:14:40 +0000267 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64))
268 ThrowException("Signed integral constant '" +
269 itostr(D.ConstPool64) + "' is invalid for type '" +
270 Ty->getDescription() + "'!");
271 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000272
273 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000274 if (!ConstantUInt::isValueValidForType(Ty, D.UConstPool64)) {
275 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer3271ed52004-07-18 00:08:11 +0000276 ThrowException("Integral constant '" + utostr(D.UConstPool64) +
Chris Lattnerf8dff732002-07-18 05:18:37 +0000277 "' is invalid or out of range!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000278 } else { // This is really a signed reference. Transmogrify.
Reid Spencer3271ed52004-07-18 00:08:11 +0000279 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000280 }
281 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000282 return ConstantUInt::get(Ty, D.UConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000283 }
284
Chris Lattner2079fde2001-10-13 06:41:08 +0000285 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000286 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
Chris Lattner2079fde2001-10-13 06:41:08 +0000287 ThrowException("FP constant invalid for type!!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000288 return ConstantFP::get(Ty, D.ConstPoolFP);
Chris Lattner2079fde2001-10-13 06:41:08 +0000289
290 case ValID::ConstNullVal: // Is it a null value?
Chris Lattner9b625032002-05-06 16:15:30 +0000291 if (!isa<PointerType>(Ty))
Chris Lattner2079fde2001-10-13 06:41:08 +0000292 ThrowException("Cannot create a a non pointer null!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000293 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner2079fde2001-10-13 06:41:08 +0000294
Chris Lattner16710e92004-10-16 18:17:13 +0000295 case ValID::ConstUndefVal: // Is it an undef value?
296 return UndefValue::get(Ty);
297
Chris Lattnerd78700d2002-08-16 21:14:40 +0000298 case ValID::ConstantVal: // Fully resolved constant?
299 if (D.ConstantValue->getType() != Ty)
300 ThrowException("Constant expression type different from required type!");
301 return D.ConstantValue;
302
Chris Lattner30c89792001-09-07 16:35:17 +0000303 default:
304 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000305 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000306 } // End of switch
307
Chris Lattner2079fde2001-10-13 06:41:08 +0000308 assert(0 && "Unhandled case!");
309 return 0;
310}
311
Chris Lattner2079fde2001-10-13 06:41:08 +0000312// getVal - This function is identical to getValNonImprovising, except that if a
313// value is not already defined, it "improvises" by creating a placeholder var
314// that looks and acts just like the requested variable. When the value is
315// defined later, all uses of the placeholder variable are replaced with the
316// real thing.
317//
Chris Lattner64116f92004-07-14 01:33:11 +0000318static Value *getVal(const Type *Ty, const ValID &ID) {
319 if (Ty == Type::LabelTy)
320 ThrowException("Cannot use a basic block here");
Chris Lattner2079fde2001-10-13 06:41:08 +0000321
Chris Lattner64116f92004-07-14 01:33:11 +0000322 // See if the value has already been defined.
323 Value *V = getValNonImprovising(Ty, ID);
Chris Lattner2079fde2001-10-13 06:41:08 +0000324 if (V) return V;
Chris Lattner00950542001-06-06 20:29:01 +0000325
Chris Lattner60cd9552005-03-23 01:29:26 +0000326 if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty))
327 ThrowException("Invalid use of a composite type!");
328
Chris Lattner00950542001-06-06 20:29:01 +0000329 // If we reached here, we referenced either a symbol that we don't know about
330 // or an id number that hasn't been read yet. We may be referencing something
331 // forward, so just create an entry to be resolved later and get to it...
332 //
Chris Lattner64116f92004-07-14 01:33:11 +0000333 V = new Argument(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000334
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000335 // Remember where this forward reference came from. FIXME, shouldn't we try
336 // to recycle these things??
Chris Lattner64116f92004-07-14 01:33:11 +0000337 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000338 llvmAsmlineno)));
339
Chris Lattner79df7c02002-03-26 18:01:55 +0000340 if (inFunctionScope())
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000341 InsertValue(V, CurFun.LateResolveValues);
Chris Lattner386a3b72001-10-16 19:54:17 +0000342 else
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000343 InsertValue(V, CurModule.LateResolveValues);
344 return V;
Chris Lattner00950542001-06-06 20:29:01 +0000345}
346
Chris Lattner2e2ed292004-07-14 06:28:35 +0000347/// getBBVal - This is used for two purposes:
348/// * If isDefinition is true, a new basic block with the specified ID is being
349/// defined.
350/// * If isDefinition is true, this is a reference to a basic block, which may
351/// or may not be a forward reference.
352///
353static BasicBlock *getBBVal(const ValID &ID, bool isDefinition = false) {
Chris Lattner64116f92004-07-14 01:33:11 +0000354 assert(inFunctionScope() && "Can't get basic block at global scope!");
355
Chris Lattner2e2ed292004-07-14 06:28:35 +0000356 std::string Name;
357 BasicBlock *BB = 0;
358 switch (ID.Type) {
359 default: ThrowException("Illegal label reference " + ID.getName());
360 case ValID::NumberVal: // Is it a numbered definition?
361 if (unsigned(ID.Num) >= CurFun.NumberedBlocks.size())
362 CurFun.NumberedBlocks.resize(ID.Num+1);
363 BB = CurFun.NumberedBlocks[ID.Num];
364 break;
365 case ValID::NameVal: // Is it a named definition?
366 Name = ID.Name;
Chris Lattnera09000d2004-07-14 23:03:46 +0000367 if (Value *N = CurFun.CurrentFunction->
368 getSymbolTable().lookup(Type::LabelTy, Name))
Chris Lattner2e2ed292004-07-14 06:28:35 +0000369 BB = cast<BasicBlock>(N);
370 break;
371 }
Chris Lattner64116f92004-07-14 01:33:11 +0000372
Chris Lattner2e2ed292004-07-14 06:28:35 +0000373 // See if the block has already been defined.
374 if (BB) {
375 // If this is the definition of the block, make sure the existing value was
376 // just a forward reference. If it was a forward reference, there will be
377 // an entry for it in the PlaceHolderInfo map.
378 if (isDefinition && !CurFun.BBForwardRefs.erase(BB))
379 // The existing value was a definition, not a forward reference.
380 ThrowException("Redefinition of label " + ID.getName());
Chris Lattner64116f92004-07-14 01:33:11 +0000381
Chris Lattner2e2ed292004-07-14 06:28:35 +0000382 ID.destroy(); // Free strdup'd memory.
383 return BB;
384 }
385
386 // Otherwise this block has not been seen before.
387 BB = new BasicBlock("", CurFun.CurrentFunction);
388 if (ID.Type == ValID::NameVal) {
389 BB->setName(ID.Name);
390 } else {
391 CurFun.NumberedBlocks[ID.Num] = BB;
392 }
393
394 // If this is not a definition, keep track of it so we can use it as a forward
395 // reference.
396 if (!isDefinition) {
397 // Remember where this forward reference came from.
398 CurFun.BBForwardRefs[BB] = std::make_pair(ID, llvmAsmlineno);
399 } else {
400 // The forward declaration could have been inserted anywhere in the
401 // function: insert it into the correct place now.
402 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
403 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
404 }
Chris Lattnere8343a52004-10-26 18:26:14 +0000405 ID.destroy();
Chris Lattner64116f92004-07-14 01:33:11 +0000406 return BB;
407}
408
Chris Lattner00950542001-06-06 20:29:01 +0000409
410//===----------------------------------------------------------------------===//
411// Code to handle forward references in instructions
412//===----------------------------------------------------------------------===//
413//
414// This code handles the late binding needed with statements that reference
415// values not defined yet... for example, a forward branch, or the PHI node for
416// a loop body.
417//
Chris Lattner394f2eb2003-10-16 20:12:13 +0000418// This keeps a table (CurFun.LateResolveValues) of all such forward references
Chris Lattner00950542001-06-06 20:29:01 +0000419// and back patchs after we are done.
420//
421
422// ResolveDefinitions - If we could not resolve some defs at parsing
423// time (forward branches, phi functions for loops, etc...) resolve the
424// defs now...
425//
Chris Lattner735f2702004-07-08 22:30:50 +0000426static void ResolveDefinitions(std::map<const Type*,ValueList> &LateResolvers,
427 std::map<const Type*,ValueList> *FutureLateResolvers) {
Chris Lattner00950542001-06-06 20:29:01 +0000428 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Chris Lattner735f2702004-07-08 22:30:50 +0000429 for (std::map<const Type*,ValueList>::iterator LRI = LateResolvers.begin(),
Chris Lattner16af11d2004-02-09 21:03:38 +0000430 E = LateResolvers.end(); LRI != E; ++LRI) {
431 ValueList &List = LRI->second;
432 while (!List.empty()) {
433 Value *V = List.back();
434 List.pop_back();
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000435
436 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
437 CurModule.PlaceHolderInfo.find(V);
438 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
439
440 ValID &DID = PHI->second.first;
Chris Lattner00950542001-06-06 20:29:01 +0000441
Chris Lattner735f2702004-07-08 22:30:50 +0000442 Value *TheRealValue = getValNonImprovising(LRI->first, DID);
Chris Lattner386a3b72001-10-16 19:54:17 +0000443 if (TheRealValue) {
444 V->replaceAllUsesWith(TheRealValue);
445 delete V;
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000446 CurModule.PlaceHolderInfo.erase(PHI);
Chris Lattner386a3b72001-10-16 19:54:17 +0000447 } else if (FutureLateResolvers) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000448 // Functions have their unresolved items forwarded to the module late
Chris Lattner386a3b72001-10-16 19:54:17 +0000449 // resolver table
450 InsertValue(V, *FutureLateResolvers);
451 } else {
Reid Spencer3271ed52004-07-18 00:08:11 +0000452 if (DID.Type == ValID::NameVal)
453 ThrowException("Reference to an invalid definition: '" +DID.getName()+
454 "' of type '" + V->getType()->getDescription() + "'",
455 PHI->second.second);
456 else
457 ThrowException("Reference to an invalid definition: #" +
458 itostr(DID.Num) + " of type '" +
459 V->getType()->getDescription() + "'",
460 PHI->second.second);
Chris Lattner30c89792001-09-07 16:35:17 +0000461 }
Chris Lattner00950542001-06-06 20:29:01 +0000462 }
463 }
464
465 LateResolvers.clear();
466}
467
Chris Lattner4a42e902001-10-22 05:56:09 +0000468// ResolveTypeTo - A brand new type was just declared. This means that (if
469// name is not null) things referencing Name can be resolved. Otherwise, things
470// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000471//
Chris Lattner4a42e902001-10-22 05:56:09 +0000472static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattner355ad1f2005-03-21 06:27:42 +0000473 ValID D;
474 if (Name) D = ValID::create(Name);
475 else D = ValID::create((int)CurModule.Types.size());
Chris Lattner00950542001-06-06 20:29:01 +0000476
Chris Lattner355ad1f2005-03-21 06:27:42 +0000477 std::map<ValID, PATypeHolder>::iterator I =
478 CurModule.LateResolveTypes.find(D);
479 if (I != CurModule.LateResolveTypes.end()) {
480 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
481 CurModule.LateResolveTypes.erase(I);
Chris Lattner30c89792001-09-07 16:35:17 +0000482 }
483}
484
Chris Lattner1781aca2001-09-18 04:00:54 +0000485// setValueName - Set the specified value to the name given. The name may be
486// null potentially, in which case this is a noop. The string passed in is
Chris Lattner8ab406d2004-07-13 07:59:27 +0000487// assumed to be a malloc'd string buffer, and is free'd by this function.
488//
489static void setValueName(Value *V, char *NameStr) {
490 if (NameStr) {
491 std::string Name(NameStr); // Copy string
492 free(NameStr); // Free old string
Chris Lattnerc9aea522004-07-13 08:12:39 +0000493
494 if (V->getType() == Type::VoidTy)
495 ThrowException("Can't assign name '" + Name+"' to value with void type!");
Chris Lattner8ab406d2004-07-13 07:59:27 +0000496
497 assert(inFunctionScope() && "Must be in function scope!");
498 SymbolTable &ST = CurFun.CurrentFunction->getSymbolTable();
499 if (ST.lookup(V->getType(), Name))
500 ThrowException("Redefinition of value named '" + Name + "' in the '" +
501 V->getType()->getDescription() + "' type plane!");
502
Chris Lattnerc9aea522004-07-13 08:12:39 +0000503 // Set the name.
Chris Lattner262bb9a2005-03-05 19:04:07 +0000504 V->setName(Name);
Chris Lattner8ab406d2004-07-13 07:59:27 +0000505 }
506}
507
Chris Lattnerd88998d2004-07-14 08:23:52 +0000508/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
509/// this is a declaration, otherwise it is a definition.
510static void ParseGlobalVariable(char *NameStr,GlobalValue::LinkageTypes Linkage,
511 bool isConstantGlobal, const Type *Ty,
512 Constant *Initializer) {
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000513 if (isa<FunctionType>(Ty))
514 ThrowException("Cannot declare global vars of function type!");
515
Chris Lattnera09000d2004-07-14 23:03:46 +0000516 const PointerType *PTy = PointerType::get(Ty);
517
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000518 std::string Name;
519 if (NameStr) {
520 Name = NameStr; // Copy string
521 free(NameStr); // Free old string
Chris Lattnerd88998d2004-07-14 08:23:52 +0000522 }
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000523
Chris Lattner8a327842004-07-14 21:44:00 +0000524 // See if this global value was forward referenced. If so, recycle the
525 // object.
526 ValID ID;
527 if (!Name.empty()) {
528 ID = ValID::create((char*)Name.c_str());
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000529 } else {
Chris Lattner8a327842004-07-14 21:44:00 +0000530 ID = ValID::create((int)CurModule.Values[PTy].size());
531 }
532
533 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
534 // Move the global to the end of the list, from whereever it was
535 // previously inserted.
536 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
537 CurModule.CurrentModule->getGlobalList().remove(GV);
538 CurModule.CurrentModule->getGlobalList().push_back(GV);
539 GV->setInitializer(Initializer);
540 GV->setLinkage(Linkage);
541 GV->setConstant(isConstantGlobal);
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000542 InsertValue(GV, CurModule.Values);
Chris Lattnera09000d2004-07-14 23:03:46 +0000543 return;
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000544 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000545
546 // If this global has a name, check to see if there is already a definition
547 // of this global in the module. If so, merge as appropriate. Note that
548 // this is really just a hack around problems in the CFE. :(
549 if (!Name.empty()) {
550 // We are a simple redefinition of a value, check to see if it is defined
551 // the same as the old one.
552 if (GlobalVariable *EGV =
553 CurModule.CurrentModule->getGlobalVariable(Name, Ty)) {
554 // We are allowed to redefine a global variable in two circumstances:
555 // 1. If at least one of the globals is uninitialized or
556 // 2. If both initializers have the same value.
557 //
558 if (!EGV->hasInitializer() || !Initializer ||
559 EGV->getInitializer() == Initializer) {
560
561 // Make sure the existing global version gets the initializer! Make
562 // sure that it also gets marked const if the new version is.
563 if (Initializer && !EGV->hasInitializer())
564 EGV->setInitializer(Initializer);
565 if (isConstantGlobal)
566 EGV->setConstant(true);
567 EGV->setLinkage(Linkage);
568 return;
569 }
570
571 ThrowException("Redefinition of global variable named '" + Name +
572 "' in the '" + Ty->getDescription() + "' type plane!");
573 }
574 }
575
576 // Otherwise there is no existing GV to use, create one now.
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000577 GlobalVariable *GV =
578 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
579 CurModule.CurrentModule);
580 InsertValue(GV, CurModule.Values);
Chris Lattnerd88998d2004-07-14 08:23:52 +0000581}
Chris Lattner8ab406d2004-07-13 07:59:27 +0000582
Reid Spencer75719bf2004-05-26 21:48:31 +0000583// setTypeName - Set the specified type to the name given. The name may be
584// null potentially, in which case this is a noop. The string passed in is
585// assumed to be a malloc'd string buffer, and is freed by this function.
586//
587// This function returns true if the type has already been defined, but is
588// allowed to be redefined in the specified context. If the name is a new name
589// for the type plane, it is inserted and false is returned.
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000590static bool setTypeName(const Type *T, char *NameStr) {
Chris Lattnera09000d2004-07-14 23:03:46 +0000591 assert(!inFunctionScope() && "Can't give types function-local names!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000592 if (NameStr == 0) return false;
593
594 std::string Name(NameStr); // Copy string
595 free(NameStr); // Free old string
596
597 // We don't allow assigning names to void type
598 if (T == Type::VoidTy)
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000599 ThrowException("Can't assign name '" + Name + "' to the void type!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000600
Chris Lattnera09000d2004-07-14 23:03:46 +0000601 // Set the type name, checking for conflicts as we do so.
602 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000603
Chris Lattnera09000d2004-07-14 23:03:46 +0000604 if (AlreadyExists) { // Inserting a name that is already defined???
605 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
606 assert(Existing && "Conflict but no matching type?");
607
Reid Spencer75719bf2004-05-26 21:48:31 +0000608 // There is only one case where this is allowed: when we are refining an
609 // opaque type. In this case, Existing will be an opaque type.
610 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
611 // We ARE replacing an opaque type!
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000612 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000613 return true;
614 }
615
616 // Otherwise, this is an attempt to redefine a type. That's okay if
617 // the redefinition is identical to the original. This will be so if
618 // Existing and T point to the same Type object. In this one case we
619 // allow the equivalent redefinition.
620 if (Existing == T) return true; // Yes, it's equal.
621
622 // Any other kind of (non-equivalent) redefinition is an error.
623 ThrowException("Redefinition of type named '" + Name + "' in the '" +
Reid Spencer3271ed52004-07-18 00:08:11 +0000624 T->getDescription() + "' type plane!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000625 }
626
Reid Spencer75719bf2004-05-26 21:48:31 +0000627 return false;
628}
Chris Lattner8896eda2001-07-09 19:38:36 +0000629
Chris Lattner30c89792001-09-07 16:35:17 +0000630//===----------------------------------------------------------------------===//
631// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000632//
Chris Lattner8896eda2001-07-09 19:38:36 +0000633
Chris Lattner3b073862004-02-09 03:19:29 +0000634// TypeContains - Returns true if Ty directly contains E in it.
Chris Lattner30c89792001-09-07 16:35:17 +0000635//
636static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattner6d8dbec2004-12-08 16:13:53 +0000637 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
638 E) != Ty->subtype_end();
Chris Lattner3b073862004-02-09 03:19:29 +0000639}
640
641namespace {
642 struct UpRefRecord {
643 // NestingLevel - The number of nesting levels that need to be popped before
644 // this type is resolved.
645 unsigned NestingLevel;
646
647 // LastContainedTy - This is the type at the current binding level for the
648 // type. Every time we reduce the nesting level, this gets updated.
649 const Type *LastContainedTy;
650
651 // UpRefTy - This is the actual opaque type that the upreference is
652 // represented with.
653 OpaqueType *UpRefTy;
654
655 UpRefRecord(unsigned NL, OpaqueType *URTy)
656 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
657 };
Chris Lattner30c89792001-09-07 16:35:17 +0000658}
Chris Lattner698b56e2001-07-20 19:15:08 +0000659
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000660// UpRefs - A list of the outstanding upreferences that need to be resolved.
Chris Lattner3b073862004-02-09 03:19:29 +0000661static std::vector<UpRefRecord> UpRefs;
Chris Lattner30c89792001-09-07 16:35:17 +0000662
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000663/// HandleUpRefs - Every time we finish a new layer of types, this function is
664/// called. It loops through the UpRefs vector, which is a list of the
665/// currently active types. For each type, if the up reference is contained in
666/// the newly completed type, we decrement the level count. When the level
667/// count reaches zero, the upreferenced type is the type that is passed in:
668/// thus we can complete the cycle.
669///
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000670static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner2c37c182004-02-09 03:03:10 +0000671 if (!ty->isAbstract()) return ty;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000672 PATypeHolder Ty(ty);
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000673 UR_OUT("Type '" << Ty->getDescription() <<
Chris Lattner5084d032001-11-02 07:46:26 +0000674 "' newly formed. Resolving upreferences.\n" <<
675 UpRefs.size() << " upreferences active!\n");
Chris Lattner026a8ce2004-02-09 18:53:54 +0000676
677 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
678 // to zero), we resolve them all together before we resolve them to Ty. At
679 // the end of the loop, if there is anything to resolve to Ty, it will be in
680 // this variable.
681 OpaqueType *TypeToResolve = 0;
682
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000683 for (unsigned i = 0; i != UpRefs.size(); ++i) {
Chris Lattner5084d032001-11-02 07:46:26 +0000684 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
Reid Spencer3271ed52004-07-18 00:08:11 +0000685 << UpRefs[i].second->getDescription() << ") = "
686 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
Chris Lattner3b073862004-02-09 03:19:29 +0000687 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
688 // Decrement level of upreference
689 unsigned Level = --UpRefs[i].NestingLevel;
690 UpRefs[i].LastContainedTy = Ty;
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000691 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
Chris Lattner30c89792001-09-07 16:35:17 +0000692 if (Level == 0) { // Upreference should be resolved!
Chris Lattner026a8ce2004-02-09 18:53:54 +0000693 if (!TypeToResolve) {
694 TypeToResolve = UpRefs[i].UpRefTy;
695 } else {
696 UR_OUT(" * Resolving upreference for "
697 << UpRefs[i].second->getDescription() << "\n";
698 std::string OldName = UpRefs[i].UpRefTy->getDescription());
699 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
700 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
701 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
702 }
Reid Spencer3271ed52004-07-18 00:08:11 +0000703 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000704 --i; // Do not skip the next element...
Chris Lattner30c89792001-09-07 16:35:17 +0000705 }
706 }
Chris Lattner8896eda2001-07-09 19:38:36 +0000707 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000708
Chris Lattner026a8ce2004-02-09 18:53:54 +0000709 if (TypeToResolve) {
710 UR_OUT(" * Resolving upreference for "
711 << UpRefs[i].second->getDescription() << "\n";
Chris Lattner16af11d2004-02-09 21:03:38 +0000712 std::string OldName = TypeToResolve->getDescription());
Chris Lattner026a8ce2004-02-09 18:53:54 +0000713 TypeToResolve->refineAbstractTypeTo(Ty);
714 }
715
Chris Lattner8896eda2001-07-09 19:38:36 +0000716 return Ty;
717}
718
Chris Lattner30c89792001-09-07 16:35:17 +0000719
Chris Lattner00950542001-06-06 20:29:01 +0000720//===----------------------------------------------------------------------===//
721// RunVMAsmParser - Define an interface to this parser
722//===----------------------------------------------------------------------===//
723//
Chris Lattner86427632004-07-14 06:39:48 +0000724Module *llvm::RunVMAsmParser(const std::string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000725 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000726 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000727 llvmAsmlineno = 1; // Reset the current line number...
728
Chris Lattner75f20532003-04-22 18:02:52 +0000729 // Allocate a new module to read
730 CurModule.CurrentModule = new Module(Filename);
Chris Lattner42570982003-11-26 07:24:58 +0000731
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000732 yyparse(); // Parse the file, potentially throwing exception
Chris Lattner99e7ab72003-10-18 05:53:13 +0000733
Chris Lattner00950542001-06-06 20:29:01 +0000734 Module *Result = ParserResult;
Chris Lattner99e7ab72003-10-18 05:53:13 +0000735
Chris Lattner00950542001-06-06 20:29:01 +0000736 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
737 ParserResult = 0;
738
739 return Result;
740}
741
742%}
743
744%union {
Brian Gaeked0fde302003-11-11 22:41:34 +0000745 llvm::Module *ModuleVal;
746 llvm::Function *FunctionVal;
747 std::pair<llvm::PATypeHolder*, char*> *ArgVal;
748 llvm::BasicBlock *BasicBlockVal;
749 llvm::TerminatorInst *TermInstVal;
750 llvm::Instruction *InstVal;
751 llvm::Constant *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000752
Brian Gaeked0fde302003-11-11 22:41:34 +0000753 const llvm::Type *PrimType;
754 llvm::PATypeHolder *TypeVal;
755 llvm::Value *ValueVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000756
Brian Gaeked0fde302003-11-11 22:41:34 +0000757 std::vector<std::pair<llvm::PATypeHolder*,char*> > *ArgList;
758 std::vector<llvm::Value*> *ValueList;
759 std::list<llvm::PATypeHolder> *TypeList;
760 std::list<std::pair<llvm::Value*,
761 llvm::BasicBlock*> > *PHIList; // Represent the RHS of PHI node
762 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
763 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000764
Brian Gaeked0fde302003-11-11 22:41:34 +0000765 llvm::GlobalValue::LinkageTypes Linkage;
Chris Lattner30c89792001-09-07 16:35:17 +0000766 int64_t SInt64Val;
767 uint64_t UInt64Val;
768 int SIntVal;
769 unsigned UIntVal;
770 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000771 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000772
Chris Lattner30c89792001-09-07 16:35:17 +0000773 char *StrVal; // This memory is strdup'd!
Brian Gaeked0fde302003-11-11 22:41:34 +0000774 llvm::ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000775
Brian Gaeked0fde302003-11-11 22:41:34 +0000776 llvm::Instruction::BinaryOps BinaryOpVal;
777 llvm::Instruction::TermOps TermOpVal;
778 llvm::Instruction::MemoryOps MemOpVal;
779 llvm::Instruction::OtherOps OtherOpVal;
780 llvm::Module::Endianness Endianness;
Chris Lattner00950542001-06-06 20:29:01 +0000781}
782
Chris Lattner79df7c02002-03-26 18:01:55 +0000783%type <ModuleVal> Module FunctionList
784%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000785%type <BasicBlockVal> BasicBlock InstructionList
786%type <TermInstVal> BBTerminatorInst
787%type <InstVal> Inst InstVal MemoryInst
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000788%type <ConstVal> ConstVal ConstExpr
Chris Lattner6cdb0112001-11-26 16:54:11 +0000789%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +0000790%type <ArgList> ArgList ArgListH
791%type <ArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000792%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000793%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +0000794%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +0000795%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000796%type <JumpTable> JumpTable
Chris Lattner4ad02e72003-04-16 20:28:45 +0000797%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Chris Lattner15c9c032003-09-08 18:20:29 +0000798%type <BoolVal> OptVolatile // 'volatile' or not
Chris Lattnerddb6db42005-05-06 05:51:46 +0000799%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
Chris Lattner4ad02e72003-04-16 20:28:45 +0000800%type <Linkage> OptLinkage
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000801%type <Endianness> BigOrLittle
Chris Lattner00950542001-06-06 20:29:01 +0000802
Chris Lattner2079fde2001-10-13 06:41:08 +0000803// ValueRef - Unresolved reference to a definition or BB
804%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000805%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000806// Tokens and types for handling constant integer values
807//
808// ESINT64VAL - A negative number within long long range
809%token <SInt64Val> ESINT64VAL
810
811// EUINT64VAL - A positive number within uns. long long range
812%token <UInt64Val> EUINT64VAL
813%type <SInt64Val> EINT64VAL
814
815%token <SIntVal> SINTVAL // Signed 32 bit ints...
816%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
817%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000818%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000819
820// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000821%type <TypeVal> Types TypesV UpRTypes UpRTypesV
822%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
Chris Lattner30c89792001-09-07 16:35:17 +0000823%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
824%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000825
Chris Lattner6c23f572003-08-22 05:42:10 +0000826%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
827%type <StrVal> Name OptName OptAssign
Chris Lattner00950542001-06-06 20:29:01 +0000828
829
Chris Lattner91ef4602004-03-31 03:49:47 +0000830%token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Chris Lattner15c9c032003-09-08 18:20:29 +0000831%token DECLARE GLOBAL CONSTANT VOLATILE
Chris Lattner16710e92004-10-16 18:17:13 +0000832%token TO DOTDOTDOT NULL_TOK UNDEF CONST INTERNAL LINKONCE WEAK APPENDING
Reid Spencer0be13e72004-07-25 17:58:28 +0000833%token OPAQUE NOT EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG
Chris Lattnerddb6db42005-05-06 05:51:46 +0000834%token DEPLIBS CALL TAIL
Chris Lattnera8e8f162005-05-06 20:27:19 +0000835%token CC_TOK CCC_TOK FASTCC_TOK COLDCC_TOK
836%type <UIntVal> OptCallingConv
Chris Lattner00950542001-06-06 20:29:01 +0000837
838// Basic Block Terminating Operators
Chris Lattner16710e92004-10-16 18:17:13 +0000839%token <TermOpVal> RET BR SWITCH INVOKE UNWIND UNREACHABLE
Chris Lattner00950542001-06-06 20:29:01 +0000840
Chris Lattner00950542001-06-06 20:29:01 +0000841// Binary Operators
Chris Lattner4a6482b2002-09-10 19:57:26 +0000842%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Chris Lattner42c9e772001-10-20 09:32:59 +0000843%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +0000844%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000845
846// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000847%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000848
Chris Lattner027dcc52001-07-08 21:10:27 +0000849// Other Operators
850%type <OtherOpVal> ShiftOps
Chris Lattnerddb6db42005-05-06 05:51:46 +0000851%token <OtherOpVal> PHI_TOK CAST SELECT SHL SHR VAARG VANEXT
852
Chris Lattner027dcc52001-07-08 21:10:27 +0000853
Chris Lattner00950542001-06-06 20:29:01 +0000854%start Module
855%%
856
857// Handle constant integer size restriction and conversion...
858//
Chris Lattner51727be2002-06-04 21:58:56 +0000859INTVAL : SINTVAL;
Chris Lattner00950542001-06-06 20:29:01 +0000860INTVAL : UINTVAL {
861 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
862 ThrowException("Value too large for type!");
863 $$ = (int32_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000864};
Chris Lattner00950542001-06-06 20:29:01 +0000865
866
Chris Lattner51727be2002-06-04 21:58:56 +0000867EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
Chris Lattner00950542001-06-06 20:29:01 +0000868EINT64VAL : EUINT64VAL {
869 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
870 ThrowException("Value too large for type!");
871 $$ = (int64_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000872};
Chris Lattner00950542001-06-06 20:29:01 +0000873
Chris Lattner00950542001-06-06 20:29:01 +0000874// Operations that are notably excluded from this list include:
875// RET, BR, & SWITCH because they end basic blocks and are treated specially.
876//
Chris Lattner4a6482b2002-09-10 19:57:26 +0000877ArithmeticOps: ADD | SUB | MUL | DIV | REM;
878LogicalOps : AND | OR | XOR;
879SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
Chris Lattner4a6482b2002-09-10 19:57:26 +0000880
Chris Lattner51727be2002-06-04 21:58:56 +0000881ShiftOps : SHL | SHR;
Chris Lattner00950542001-06-06 20:29:01 +0000882
Chris Lattnerddb6db42005-05-06 05:51:46 +0000883
884
885
Chris Lattnere98dda62001-07-14 06:10:16 +0000886// These are some types that allow classification if we only want a particular
887// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner51727be2002-06-04 21:58:56 +0000888SIntType : LONG | INT | SHORT | SBYTE;
889UIntType : ULONG | UINT | USHORT | UBYTE;
890IntType : SIntType | UIntType;
891FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +0000892
Chris Lattnere98dda62001-07-14 06:10:16 +0000893// OptAssign - Value producing statements have an optional assignment component
Chris Lattner6c23f572003-08-22 05:42:10 +0000894OptAssign : Name '=' {
Chris Lattner00950542001-06-06 20:29:01 +0000895 $$ = $1;
896 }
897 | /*empty*/ {
898 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +0000899 };
Chris Lattner00950542001-06-06 20:29:01 +0000900
Chris Lattner4ad02e72003-04-16 20:28:45 +0000901OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
902 LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
Chris Lattner72ac148d2003-10-16 18:29:00 +0000903 WEAK { $$ = GlobalValue::WeakLinkage; } |
Chris Lattner4ad02e72003-04-16 20:28:45 +0000904 APPENDING { $$ = GlobalValue::AppendingLinkage; } |
905 /*empty*/ { $$ = GlobalValue::ExternalLinkage; };
Chris Lattner30c89792001-09-07 16:35:17 +0000906
Chris Lattnera8e8f162005-05-06 20:27:19 +0000907OptCallingConv : /*empty*/ { $$ = CallingConv::C; } |
908 CCC_TOK { $$ = CallingConv::C; } |
909 FASTCC_TOK { $$ = CallingConv::Fast; } |
910 COLDCC_TOK { $$ = CallingConv::Cold; } |
911 CC_TOK EUINT64VAL {
912 if ((unsigned)$2 != $2)
913 ThrowException("Calling conv too large!");
914 $$ = $2;
915 };
916
Chris Lattner30c89792001-09-07 16:35:17 +0000917//===----------------------------------------------------------------------===//
918// Types includes all predefined types... except void, because it can only be
Chris Lattner7e708292002-06-25 16:13:24 +0000919// used in specific contexts (function returning void for example). To have
Chris Lattner30c89792001-09-07 16:35:17 +0000920// access to it, a user must explicitly use TypesV.
921//
922
923// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner51727be2002-06-04 21:58:56 +0000924TypesV : Types | VOID { $$ = new PATypeHolder($1); };
925UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattner30c89792001-09-07 16:35:17 +0000926
927Types : UpRTypes {
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000928 if (!UpRefs.empty())
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000929 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
930 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000931 };
Chris Lattner30c89792001-09-07 16:35:17 +0000932
933
934// Derived types are added later...
935//
Chris Lattner51727be2002-06-04 21:58:56 +0000936PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
937PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000938UpRTypes : OPAQUE {
939 $$ = new PATypeHolder(OpaqueType::get());
940 }
941 | PrimType {
942 $$ = new PATypeHolder($1);
Chris Lattner51727be2002-06-04 21:58:56 +0000943 };
Chris Lattnerd78700d2002-08-16 21:14:40 +0000944UpRTypes : SymbolicValueRef { // Named types are also simple types...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000945 $$ = new PATypeHolder(getTypeVal($1));
Chris Lattner51727be2002-06-04 21:58:56 +0000946};
Chris Lattner30c89792001-09-07 16:35:17 +0000947
Chris Lattner30c89792001-09-07 16:35:17 +0000948// Include derived types in the Types production.
949//
950UpRTypes : '\\' EUINT64VAL { // Type UpReference
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000951 if ($2 > (uint64_t)~0U) ThrowException("Value out of range!");
Chris Lattner30c89792001-09-07 16:35:17 +0000952 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
Chris Lattner3b073862004-02-09 03:19:29 +0000953 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000954 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +0000955 UR_OUT("New Upreference!\n");
956 }
Chris Lattner79df7c02002-03-26 18:01:55 +0000957 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner9232b992003-04-22 18:42:41 +0000958 std::vector<const Type*> Params;
Chris Lattnera08976b2005-02-22 23:13:58 +0000959 for (std::list<llvm::PATypeHolder>::iterator I = $3->begin(),
960 E = $3->end(); I != E; ++I)
961 Params.push_back(*I);
Chris Lattner2079fde2001-10-13 06:41:08 +0000962 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
963 if (isVarArg) Params.pop_back();
964
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000965 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +0000966 delete $3; // Delete the argument list
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000967 delete $1; // Delete the return type handle
Chris Lattner30c89792001-09-07 16:35:17 +0000968 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000969 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000970 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000971 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +0000972 }
Brian Gaeke715c90b2004-08-20 06:00:58 +0000973 | '<' EUINT64VAL 'x' UpRTypes '>' { // Packed array type?
974 const llvm::Type* ElemTy = $4->get();
975 if ((unsigned)$2 != $2) {
976 ThrowException("Unsigned result not equal to signed result");
977 }
978 if(!ElemTy->isPrimitiveType()) {
979 ThrowException("Elemental type of a PackedType must be primitive");
980 }
981 $$ = new PATypeHolder(HandleUpRefs(PackedType::get(*$4, (unsigned)$2)));
982 delete $4;
983 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000984 | '{' TypeListI '}' { // Structure type?
Chris Lattner9232b992003-04-22 18:42:41 +0000985 std::vector<const Type*> Elements;
Chris Lattnera08976b2005-02-22 23:13:58 +0000986 for (std::list<llvm::PATypeHolder>::iterator I = $2->begin(),
987 E = $2->end(); I != E; ++I)
988 Elements.push_back(*I);
989
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000990 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000991 delete $2;
992 }
993 | '{' '}' { // Empty structure type?
Chris Lattner9232b992003-04-22 18:42:41 +0000994 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000995 }
996 | UpRTypes '*' { // Pointer type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000997 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000998 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000999 };
Chris Lattner30c89792001-09-07 16:35:17 +00001000
Chris Lattner7e708292002-06-25 16:13:24 +00001001// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +00001002// declaration type lists
1003//
1004TypeListI : UpRTypes {
Chris Lattner9232b992003-04-22 18:42:41 +00001005 $$ = new std::list<PATypeHolder>();
Chris Lattner30c89792001-09-07 16:35:17 +00001006 $$->push_back(*$1); delete $1;
1007 }
1008 | TypeListI ',' UpRTypes {
1009 ($$=$1)->push_back(*$3); delete $3;
Chris Lattner51727be2002-06-04 21:58:56 +00001010 };
Chris Lattner30c89792001-09-07 16:35:17 +00001011
Chris Lattner7e708292002-06-25 16:13:24 +00001012// ArgTypeList - List of types for a function type declaration...
Chris Lattner30c89792001-09-07 16:35:17 +00001013ArgTypeListI : TypeListI
1014 | TypeListI ',' DOTDOTDOT {
1015 ($$=$1)->push_back(Type::VoidTy);
1016 }
1017 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001018 ($$ = new std::list<PATypeHolder>())->push_back(Type::VoidTy);
Chris Lattner30c89792001-09-07 16:35:17 +00001019 }
1020 | /*empty*/ {
Chris Lattner9232b992003-04-22 18:42:41 +00001021 $$ = new std::list<PATypeHolder>();
Chris Lattner51727be2002-06-04 21:58:56 +00001022 };
Chris Lattner30c89792001-09-07 16:35:17 +00001023
Chris Lattnere98dda62001-07-14 06:10:16 +00001024// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +00001025// production is used ONLY to represent constants that show up AFTER a 'const',
1026// 'constant' or 'global' token at global scope. Constants that can be inlined
1027// into other expressions (such as integers and constexprs) are handled by the
1028// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +00001029//
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001030ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Chris Lattner949a3622003-07-23 15:30:06 +00001031 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001032 if (ATy == 0)
1033 ThrowException("Cannot make array constant with type: '" +
1034 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001035 const Type *ETy = ATy->getElementType();
1036 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +00001037
Chris Lattner30c89792001-09-07 16:35:17 +00001038 // Verify that we have the correct size...
1039 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +00001040 ThrowException("Type mismatch: constant sized array initialized with " +
Reid Spencer3271ed52004-07-18 00:08:11 +00001041 utostr($3->size()) + " arguments, but has size of " +
1042 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +00001043
Chris Lattner30c89792001-09-07 16:35:17 +00001044 // Verify all elements are correct type!
1045 for (unsigned i = 0; i < $3->size(); i++) {
1046 if (ETy != (*$3)[i]->getType())
Reid Spencer3271ed52004-07-18 00:08:11 +00001047 ThrowException("Element #" + utostr(i) + " is not of type '" +
1048 ETy->getDescription() +"' as required!\nIt is of type '"+
1049 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +00001050 }
1051
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001052 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001053 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001054 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001055 | Types '[' ']' {
Chris Lattner949a3622003-07-23 15:30:06 +00001056 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001057 if (ATy == 0)
1058 ThrowException("Cannot make array constant with type: '" +
1059 (*$1)->getDescription() + "'!");
1060
1061 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +00001062 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +00001063 ThrowException("Type mismatch: constant sized array initialized with 0"
Reid Spencer3271ed52004-07-18 00:08:11 +00001064 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattner9232b992003-04-22 18:42:41 +00001065 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +00001066 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001067 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001068 | Types 'c' STRINGCONSTANT {
Chris Lattner949a3622003-07-23 15:30:06 +00001069 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001070 if (ATy == 0)
1071 ThrowException("Cannot make array constant with type: '" +
1072 (*$1)->getDescription() + "'!");
1073
Chris Lattner30c89792001-09-07 16:35:17 +00001074 int NumElements = ATy->getNumElements();
1075 const Type *ETy = ATy->getElementType();
1076 char *EndStr = UnEscapeLexed($3, true);
1077 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +00001078 ThrowException("Can't build string constant of size " +
Reid Spencer3271ed52004-07-18 00:08:11 +00001079 itostr((int)(EndStr-$3)) +
1080 " when array has size " + itostr(NumElements) + "!");
Chris Lattner9232b992003-04-22 18:42:41 +00001081 std::vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +00001082 if (ETy == Type::SByteTy) {
1083 for (char *C = $3; C != EndStr; ++C)
Reid Spencer3271ed52004-07-18 00:08:11 +00001084 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +00001085 } else if (ETy == Type::UByteTy) {
1086 for (char *C = $3; C != EndStr; ++C)
Reid Spencer3271ed52004-07-18 00:08:11 +00001087 Vals.push_back(ConstantUInt::get(ETy, (unsigned char)*C));
Chris Lattner93750fa2001-07-28 17:48:55 +00001088 } else {
Chris Lattner30c89792001-09-07 16:35:17 +00001089 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +00001090 ThrowException("Cannot build string arrays of non byte sized elements!");
1091 }
Chris Lattner30c89792001-09-07 16:35:17 +00001092 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001093 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +00001094 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +00001095 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00001096 | Types '<' ConstVector '>' { // Nonempty unsized arr
1097 const PackedType *PTy = dyn_cast<PackedType>($1->get());
1098 if (PTy == 0)
1099 ThrowException("Cannot make packed constant with type: '" +
1100 (*$1)->getDescription() + "'!");
1101 const Type *ETy = PTy->getElementType();
1102 int NumElements = PTy->getNumElements();
1103
1104 // Verify that we have the correct size...
1105 if (NumElements != -1 && NumElements != (int)$3->size())
1106 ThrowException("Type mismatch: constant sized packed initialized with " +
1107 utostr($3->size()) + " arguments, but has size of " +
1108 itostr(NumElements) + "!");
1109
1110 // Verify all elements are correct type!
1111 for (unsigned i = 0; i < $3->size(); i++) {
1112 if (ETy != (*$3)[i]->getType())
1113 ThrowException("Element #" + utostr(i) + " is not of type '" +
1114 ETy->getDescription() +"' as required!\nIt is of type '"+
1115 (*$3)[i]->getType()->getDescription() + "'.");
1116 }
1117
1118 $$ = ConstantPacked::get(PTy, *$3);
1119 delete $1; delete $3;
1120 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001121 | Types '{' ConstVector '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001122 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001123 if (STy == 0)
1124 ThrowException("Cannot make struct constant with type: '" +
1125 (*$1)->getDescription() + "'!");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001126
Chris Lattnerc6212f12003-05-21 16:06:56 +00001127 if ($3->size() != STy->getNumContainedTypes())
1128 ThrowException("Illegal number of initializers for structure type!");
1129
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001130 // Check to ensure that constants are compatible with the type initializer!
1131 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Chris Lattnerd21cd802004-02-09 04:37:31 +00001132 if ((*$3)[i]->getType() != STy->getElementType(i))
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001133 ThrowException("Expected type '" +
Chris Lattnerd21cd802004-02-09 04:37:31 +00001134 STy->getElementType(i)->getDescription() +
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001135 "' for element #" + utostr(i) +
1136 " of structure initializer!");
1137
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001138 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001139 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001140 }
Chris Lattnerc6212f12003-05-21 16:06:56 +00001141 | Types '{' '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001142 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerc6212f12003-05-21 16:06:56 +00001143 if (STy == 0)
1144 ThrowException("Cannot make struct constant with type: '" +
1145 (*$1)->getDescription() + "'!");
1146
1147 if (STy->getNumContainedTypes() != 0)
1148 ThrowException("Illegal number of initializers for structure type!");
1149
1150 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1151 delete $1;
1152 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001153 | Types NULL_TOK {
Chris Lattner949a3622003-07-23 15:30:06 +00001154 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001155 if (PTy == 0)
1156 ThrowException("Cannot make null pointer constant with type: '" +
1157 (*$1)->getDescription() + "'!");
1158
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001159 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001160 delete $1;
1161 }
Chris Lattner16710e92004-10-16 18:17:13 +00001162 | Types UNDEF {
1163 $$ = UndefValue::get($1->get());
1164 delete $1;
1165 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001166 | Types SymbolicValueRef {
Chris Lattner949a3622003-07-23 15:30:06 +00001167 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001168 if (Ty == 0)
1169 ThrowException("Global const reference must be a pointer type!");
1170
Chris Lattner3101c252002-08-15 17:58:33 +00001171 // ConstExprs can exist in the body of a function, thus creating
Reid Spencer3271ed52004-07-18 00:08:11 +00001172 // GlobalValues whenever they refer to a variable. Because we are in
Chris Lattner3101c252002-08-15 17:58:33 +00001173 // the context of a function, getValNonImprovising will search the functions
1174 // symbol table instead of the module symbol table for the global symbol,
1175 // which throws things all off. To get around this, we just tell
1176 // getValNonImprovising that we are at global scope here.
1177 //
Chris Lattner394f2eb2003-10-16 20:12:13 +00001178 Function *SavedCurFn = CurFun.CurrentFunction;
1179 CurFun.CurrentFunction = 0;
Chris Lattner3101c252002-08-15 17:58:33 +00001180
Chris Lattner2079fde2001-10-13 06:41:08 +00001181 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001182
Chris Lattner394f2eb2003-10-16 20:12:13 +00001183 CurFun.CurrentFunction = SavedCurFn;
Chris Lattner3101c252002-08-15 17:58:33 +00001184
Chris Lattner2079fde2001-10-13 06:41:08 +00001185 // If this is an initializer for a constant pointer, which is referencing a
1186 // (currently) undefined variable, create a stub now that shall be replaced
1187 // in the future with the right type of variable.
1188 //
1189 if (V == 0) {
1190 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1191 const PointerType *PT = cast<PointerType>(Ty);
1192
1193 // First check to see if the forward references value is already created!
1194 PerModuleInfo::GlobalRefsType::iterator I =
Reid Spencer3271ed52004-07-18 00:08:11 +00001195 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Chris Lattner2079fde2001-10-13 06:41:08 +00001196
1197 if (I != CurModule.GlobalRefs.end()) {
Reid Spencer3271ed52004-07-18 00:08:11 +00001198 V = I->second; // Placeholder already exists, use it...
Chris Lattnera989b232003-12-23 20:05:15 +00001199 $2.destroy();
Chris Lattner2079fde2001-10-13 06:41:08 +00001200 } else {
Chris Lattner8a327842004-07-14 21:44:00 +00001201 std::string Name;
Chris Lattnera09000d2004-07-14 23:03:46 +00001202 if ($2.Type == ValID::NameVal) Name = $2.Name;
Chris Lattner8a327842004-07-14 21:44:00 +00001203
Reid Spencer3271ed52004-07-18 00:08:11 +00001204 // Create the forward referenced global.
Chris Lattnera09000d2004-07-14 23:03:46 +00001205 GlobalValue *GV;
1206 if (const FunctionType *FTy =
1207 dyn_cast<FunctionType>(PT->getElementType())) {
1208 GV = new Function(FTy, GlobalValue::ExternalLinkage, Name,
1209 CurModule.CurrentModule);
1210 } else {
Reid Spencer3271ed52004-07-18 00:08:11 +00001211 GV = new GlobalVariable(PT->getElementType(), false,
Chris Lattnera09000d2004-07-14 23:03:46 +00001212 GlobalValue::ExternalLinkage, 0,
1213 Name, CurModule.CurrentModule);
1214 }
Chris Lattner8a327842004-07-14 21:44:00 +00001215
Reid Spencer3271ed52004-07-18 00:08:11 +00001216 // Keep track of the fact that we have a forward ref to recycle it
1217 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1218 V = GV;
Chris Lattner2079fde2001-10-13 06:41:08 +00001219 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001220 }
1221
Reid Spencer3271ed52004-07-18 00:08:11 +00001222 $$ = cast<GlobalValue>(V);
Chris Lattner2079fde2001-10-13 06:41:08 +00001223 delete $1; // Free the type handle
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001224 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001225 | Types ConstExpr {
1226 if ($1->get() != $2->getType())
1227 ThrowException("Mismatched types for constant expression!");
1228 $$ = $2;
1229 delete $1;
Chris Lattnerf4600482003-06-28 20:01:34 +00001230 }
1231 | Types ZEROINITIALIZER {
Chris Lattner78915932004-11-28 16:45:45 +00001232 const Type *Ty = $1->get();
1233 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
1234 ThrowException("Cannot create a null initialized value of this type!");
1235 $$ = Constant::getNullValue(Ty);
Chris Lattnerf4600482003-06-28 20:01:34 +00001236 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001237 };
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001238
Chris Lattnere43f40b2002-10-09 00:25:32 +00001239ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001240 if (!ConstantSInt::isValueValidForType($1, $2))
1241 ThrowException("Constant value doesn't fit in type!");
1242 $$ = ConstantSInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001243 }
1244 | UIntType EUINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001245 if (!ConstantUInt::isValueValidForType($1, $2))
1246 ThrowException("Constant value doesn't fit in type!");
1247 $$ = ConstantUInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001248 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001249 | BOOL TRUETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001250 $$ = ConstantBool::True;
1251 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001252 | BOOL FALSETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001253 $$ = ConstantBool::False;
1254 }
1255 | FPType FPVAL { // Float & Double constants
Reid Spencer9f9b3ac2004-12-06 22:18:25 +00001256 if (!ConstantFP::isValueValidForType($1, $2))
1257 ThrowException("Floating point constant invalid for type!!");
Chris Lattnerd05e3592002-08-15 18:17:28 +00001258 $$ = ConstantFP::get($1, $2);
1259 };
1260
Chris Lattner00950542001-06-06 20:29:01 +00001261
Chris Lattnerd78700d2002-08-16 21:14:40 +00001262ConstExpr: CAST '(' ConstVal TO Types ')' {
Chris Lattnerae711a82003-11-21 20:27:35 +00001263 if (!$3->getType()->isFirstClassType())
1264 ThrowException("cast constant expression from a non-primitive type: '" +
1265 $3->getType()->getDescription() + "'!");
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001266 if (!$5->get()->isFirstClassType())
1267 ThrowException("cast constant expression to a non-primitive type: '" +
1268 $5->get()->getDescription() + "'!");
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001269 $$ = ConstantExpr::getCast($3, $5->get());
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001270 delete $5;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001271 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001272 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1273 if (!isa<PointerType>($3->getType()))
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001274 ThrowException("GetElementPtr requires a pointer operand!");
1275
Chris Lattner830b6f92004-04-05 01:30:04 +00001276 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
1277 // indices to uint struct indices for compatibility.
1278 generic_gep_type_iterator<std::vector<Value*>::iterator>
1279 GTI = gep_type_begin($3->getType(), $4->begin(), $4->end()),
1280 GTE = gep_type_end($3->getType(), $4->begin(), $4->end());
1281 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
1282 if (isa<StructType>(*GTI)) // Only change struct indices
1283 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
1284 if (CUI->getType() == Type::UByteTy)
1285 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
1286
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001287 const Type *IdxTy =
Chris Lattnerd78700d2002-08-16 21:14:40 +00001288 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001289 if (!IdxTy)
1290 ThrowException("Index list invalid for constant getelementptr!");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001291
Chris Lattner9232b992003-04-22 18:42:41 +00001292 std::vector<Constant*> IdxVec;
Chris Lattnerd78700d2002-08-16 21:14:40 +00001293 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1294 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001295 IdxVec.push_back(C);
1296 else
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001297 ThrowException("Indices to constant getelementptr must be constants!");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001298
Chris Lattnerd78700d2002-08-16 21:14:40 +00001299 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001300
Chris Lattnerd78700d2002-08-16 21:14:40 +00001301 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001302 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001303 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1304 if ($3->getType() != Type::BoolTy)
1305 ThrowException("Select condition must be of boolean type!");
1306 if ($5->getType() != $7->getType())
1307 ThrowException("Select operand types must match!");
1308 $$ = ConstantExpr::getSelect($3, $5, $7);
1309 }
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001310 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001311 if ($3->getType() != $5->getType())
1312 ThrowException("Binary operator types must match!");
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001313 // HACK: llvm 1.3 and earlier used to emit invalid pointer constant exprs.
1314 // To retain backward compatibility with these early compilers, we emit a
1315 // cast to the appropriate integer type automatically if we are in the
1316 // broken case. See PR424 for more information.
1317 if (!isa<PointerType>($3->getType())) {
1318 $$ = ConstantExpr::get($1, $3, $5);
1319 } else {
Chris Lattner42db1a02004-08-20 18:07:39 +00001320 const Type *IntPtrTy = 0;
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001321 switch (CurModule.CurrentModule->getPointerSize()) {
1322 case Module::Pointer32: IntPtrTy = Type::IntTy; break;
1323 case Module::Pointer64: IntPtrTy = Type::LongTy; break;
1324 default: ThrowException("invalid pointer binary constant expr!");
1325 }
1326 $$ = ConstantExpr::get($1, ConstantExpr::getCast($3, IntPtrTy),
1327 ConstantExpr::getCast($5, IntPtrTy));
1328 $$ = ConstantExpr::getCast($$, $3->getType());
1329 }
1330 }
1331 | LogicalOps '(' ConstVal ',' ConstVal ')' {
1332 if ($3->getType() != $5->getType())
1333 ThrowException("Logical operator types must match!");
1334 if (!$3->getType()->isIntegral())
1335 ThrowException("Logical operands must have integral types!");
1336 $$ = ConstantExpr::get($1, $3, $5);
1337 }
1338 | SetCondOps '(' ConstVal ',' ConstVal ')' {
1339 if ($3->getType() != $5->getType())
1340 ThrowException("setcc operand types must match!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001341 $$ = ConstantExpr::get($1, $3, $5);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001342 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001343 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001344 if ($5->getType() != Type::UByteTy)
1345 ThrowException("Shift count for shift constant must be unsigned byte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001346 if (!$3->getType()->isInteger())
1347 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerb16689b2004-01-12 19:08:43 +00001348 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattner699f1eb2002-08-14 17:12:33 +00001349 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001350
1351
Misha Brukmanbc0e9982003-07-14 17:20:40 +00001352// ConstVector - A list of comma separated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001353ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001354 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001355 }
1356 | ConstVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001357 $$ = new std::vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001358 $$->push_back($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001359 };
Chris Lattner00950542001-06-06 20:29:01 +00001360
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001361
Chris Lattner1781aca2001-09-18 04:00:54 +00001362// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001363GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001364
Chris Lattner00950542001-06-06 20:29:01 +00001365
Chris Lattner0e73ce62002-05-02 19:11:13 +00001366//===----------------------------------------------------------------------===//
1367// Rules to match Modules
1368//===----------------------------------------------------------------------===//
1369
1370// Module rule: Capture the result of parsing the whole file into a result
1371// variable...
1372//
1373Module : FunctionList {
1374 $$ = ParserResult = $1;
1375 CurModule.ModuleDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001376};
Chris Lattner0e73ce62002-05-02 19:11:13 +00001377
Chris Lattner7e708292002-06-25 16:13:24 +00001378// FunctionList - A list of functions, preceeded by a constant pool.
Chris Lattner0e73ce62002-05-02 19:11:13 +00001379//
1380FunctionList : FunctionList Function {
1381 $$ = $1;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001382 CurFun.FunctionDone();
Chris Lattner0e73ce62002-05-02 19:11:13 +00001383 }
1384 | FunctionList FunctionProto {
1385 $$ = $1;
1386 }
1387 | FunctionList IMPLEMENTATION {
1388 $$ = $1;
1389 }
1390 | ConstPool {
1391 $$ = CurModule.CurrentModule;
Chris Lattner355ad1f2005-03-21 06:27:42 +00001392 // Emit an error if there are any unresolved types left.
1393 if (!CurModule.LateResolveTypes.empty()) {
1394 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
1395 if (DID.Type == ValID::NameVal)
1396 ThrowException("Reference to an undefined type: '"+DID.getName() + "'");
1397 else
1398 ThrowException("Reference to an undefined type: #" + itostr(DID.Num));
1399 }
Chris Lattner51727be2002-06-04 21:58:56 +00001400 };
Chris Lattner0e73ce62002-05-02 19:11:13 +00001401
Chris Lattnere98dda62001-07-14 06:10:16 +00001402// ConstPool - Constants with optional names assigned to them.
Chris Lattner355ad1f2005-03-21 06:27:42 +00001403ConstPool : ConstPool OptAssign TYPE TypesV {
Chris Lattner4a42e902001-10-22 05:56:09 +00001404 // Eagerly resolve types. This is not an optimization, this is a
1405 // requirement that is due to the fact that we could have this:
1406 //
1407 // %list = type { %list * }
1408 // %list = type { %list * } ; repeated type decl
1409 //
1410 // If types are not resolved eagerly, then the two types will not be
1411 // determined to be the same type!
1412 //
Chris Lattner8c89a0a2004-07-13 08:10:10 +00001413 ResolveTypeTo($2, *$4);
Chris Lattner4a42e902001-10-22 05:56:09 +00001414
Chris Lattner8c89a0a2004-07-13 08:10:10 +00001415 if (!setTypeName(*$4, $2) && !$2) {
1416 // If this is a named type that is not a redefinition, add it to the slot
1417 // table.
Chris Lattner355ad1f2005-03-21 06:27:42 +00001418 CurModule.Types.push_back(*$4);
Chris Lattner30c89792001-09-07 16:35:17 +00001419 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001420
1421 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001422 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001423 | ConstPool FunctionProto { // Function prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001424 }
Chris Lattner4ad02e72003-04-16 20:28:45 +00001425 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
Chris Lattnerd88998d2004-07-14 08:23:52 +00001426 if ($5 == 0) ThrowException("Global value initializer is not a constant!");
1427 ParseGlobalVariable($2, $3, $4, $5->getType(), $5);
Chris Lattner1781aca2001-09-18 04:00:54 +00001428 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001429 | ConstPool OptAssign EXTERNAL GlobalType Types {
Chris Lattnerd88998d2004-07-14 08:23:52 +00001430 ParseGlobalVariable($2, GlobalValue::ExternalLinkage, $4, *$5, 0);
Chris Lattner1f862af2003-04-16 18:13:57 +00001431 delete $5;
Chris Lattnere98dda62001-07-14 06:10:16 +00001432 }
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001433 | ConstPool TARGET TargetDefinition {
1434 }
Reid Spencer0be13e72004-07-25 17:58:28 +00001435 | ConstPool DEPLIBS '=' LibrariesDefinition {
1436 }
Chris Lattner00950542001-06-06 20:29:01 +00001437 | /* empty: end of list */ {
Chris Lattner51727be2002-06-04 21:58:56 +00001438 };
Chris Lattner00950542001-06-06 20:29:01 +00001439
1440
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001441
1442BigOrLittle : BIG { $$ = Module::BigEndian; };
1443BigOrLittle : LITTLE { $$ = Module::LittleEndian; };
1444
1445TargetDefinition : ENDIAN '=' BigOrLittle {
1446 CurModule.CurrentModule->setEndianness($3);
1447 }
1448 | POINTERSIZE '=' EUINT64VAL {
1449 if ($3 == 32)
1450 CurModule.CurrentModule->setPointerSize(Module::Pointer32);
1451 else if ($3 == 64)
1452 CurModule.CurrentModule->setPointerSize(Module::Pointer64);
1453 else
1454 ThrowException("Invalid pointer size: '" + utostr($3) + "'!");
Reid Spencer0be13e72004-07-25 17:58:28 +00001455 }
1456 | TRIPLE '=' STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001457 CurModule.CurrentModule->setTargetTriple($3);
1458 free($3);
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001459 };
1460
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001461LibrariesDefinition : '[' LibList ']';
Reid Spencer0be13e72004-07-25 17:58:28 +00001462
1463LibList : LibList ',' STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001464 CurModule.CurrentModule->addLibrary($3);
1465 free($3);
Reid Spencer0be13e72004-07-25 17:58:28 +00001466 }
1467 | STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001468 CurModule.CurrentModule->addLibrary($1);
1469 free($1);
Reid Spencer0be13e72004-07-25 17:58:28 +00001470 }
1471 | /* empty: end of list */ {
1472 }
1473 ;
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001474
Chris Lattner00950542001-06-06 20:29:01 +00001475//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00001476// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00001477//===----------------------------------------------------------------------===//
1478
Chris Lattner6c23f572003-08-22 05:42:10 +00001479Name : VAR_ID | STRINGCONSTANT;
1480OptName : Name | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001481
Chris Lattner6c23f572003-08-22 05:42:10 +00001482ArgVal : Types OptName {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001483 if (*$1 == Type::VoidTy)
1484 ThrowException("void typed arguments are invalid!");
Chris Lattner9232b992003-04-22 18:42:41 +00001485 $$ = new std::pair<PATypeHolder*, char*>($1, $2);
Chris Lattner51727be2002-06-04 21:58:56 +00001486};
Chris Lattner00950542001-06-06 20:29:01 +00001487
Chris Lattner69da5cf2002-10-13 20:57:00 +00001488ArgListH : ArgListH ',' ArgVal {
1489 $$ = $1;
1490 $1->push_back(*$3);
1491 delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001492 }
1493 | ArgVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001494 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
Chris Lattner69da5cf2002-10-13 20:57:00 +00001495 $$->push_back(*$1);
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001496 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001497 };
Chris Lattner00950542001-06-06 20:29:01 +00001498
1499ArgList : ArgListH {
1500 $$ = $1;
1501 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001502 | ArgListH ',' DOTDOTDOT {
1503 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001504 $$->push_back(std::pair<PATypeHolder*,
1505 char*>(new PATypeHolder(Type::VoidTy), 0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001506 }
1507 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001508 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1509 $$->push_back(std::make_pair(new PATypeHolder(Type::VoidTy), (char*)0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001510 }
Chris Lattner00950542001-06-06 20:29:01 +00001511 | /* empty */ {
1512 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001513 };
Chris Lattner00950542001-06-06 20:29:01 +00001514
Chris Lattnera8e8f162005-05-06 20:27:19 +00001515FunctionHeaderH : OptCallingConv TypesV Name '(' ArgList ')' {
1516 UnEscapeLexed($3);
1517 std::string FunctionName($3);
1518 free($3); // Free strdup'd memory!
Chris Lattnerdda71962001-11-26 18:54:16 +00001519
Chris Lattnera8e8f162005-05-06 20:27:19 +00001520 if (!(*$2)->isFirstClassType() && *$2 != Type::VoidTy)
Chris Lattnerc282f5a2003-11-21 22:32:23 +00001521 ThrowException("LLVM functions cannot return aggregate types!");
1522
Chris Lattner9232b992003-04-22 18:42:41 +00001523 std::vector<const Type*> ParamTypeList;
Chris Lattnera8e8f162005-05-06 20:27:19 +00001524 if ($5) { // If there are arguments...
1525 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $5->begin();
1526 I != $5->end(); ++I)
Chris Lattner69da5cf2002-10-13 20:57:00 +00001527 ParamTypeList.push_back(I->first->get());
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001528 }
Chris Lattner00950542001-06-06 20:29:01 +00001529
Chris Lattner2079fde2001-10-13 06:41:08 +00001530 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1531 if (isVarArg) ParamTypeList.pop_back();
1532
Chris Lattnera8e8f162005-05-06 20:27:19 +00001533 const FunctionType *FT = FunctionType::get(*$2, ParamTypeList, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001534 const PointerType *PFT = PointerType::get(FT);
Chris Lattnera8e8f162005-05-06 20:27:19 +00001535 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001536
Chris Lattnera09000d2004-07-14 23:03:46 +00001537 ValID ID;
1538 if (!FunctionName.empty()) {
1539 ID = ValID::create((char*)FunctionName.c_str());
1540 } else {
1541 ID = ValID::create((int)CurModule.Values[PFT].size());
1542 }
1543
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001544 Function *Fn = 0;
Chris Lattnera09000d2004-07-14 23:03:46 +00001545 // See if this function was forward referenced. If so, recycle the object.
1546 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
1547 // Move the function to the end of the list, from whereever it was
1548 // previously inserted.
1549 Fn = cast<Function>(FWRef);
1550 CurModule.CurrentModule->getFunctionList().remove(Fn);
1551 CurModule.CurrentModule->getFunctionList().push_back(Fn);
1552 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
1553 (Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
1554 // If this is the case, either we need to be a forward decl, or it needs
1555 // to be.
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001556 if (!CurFun.isDeclare && !Fn->isExternal())
1557 ThrowException("Redefinition of function '" + FunctionName + "'!");
1558
Chris Lattnera09000d2004-07-14 23:03:46 +00001559 // Make sure to strip off any argument names so we can't get conflicts.
1560 if (Fn->isExternal())
Chris Lattnere4d5c442005-03-15 04:54:21 +00001561 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
Chris Lattnera09000d2004-07-14 23:03:46 +00001562 AI != AE; ++AI)
1563 AI->setName("");
Chris Lattner5659dd12002-07-15 00:10:33 +00001564
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001565 } else { // Not already defined?
1566 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName,
1567 CurModule.CurrentModule);
1568 InsertValue(Fn, CurModule.Values);
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001569 }
Chris Lattner00950542001-06-06 20:29:01 +00001570
Chris Lattner394f2eb2003-10-16 20:12:13 +00001571 CurFun.FunctionStart(Fn);
Chris Lattnera8e8f162005-05-06 20:27:19 +00001572 Fn->setCallingConv($1);
Chris Lattner00950542001-06-06 20:29:01 +00001573
Chris Lattner7e708292002-06-25 16:13:24 +00001574 // Add all of the arguments we parsed to the function...
Chris Lattnera8e8f162005-05-06 20:27:19 +00001575 if ($5) { // Is null if empty...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001576 if (isVarArg) { // Nuke the last entry
Chris Lattnera8e8f162005-05-06 20:27:19 +00001577 assert($5->back().first->get() == Type::VoidTy && $5->back().second == 0&&
Chris Lattner69da5cf2002-10-13 20:57:00 +00001578 "Not a varargs marker!");
Chris Lattnera8e8f162005-05-06 20:27:19 +00001579 delete $5->back().first;
1580 $5->pop_back(); // Delete the last entry
Chris Lattner69da5cf2002-10-13 20:57:00 +00001581 }
Chris Lattnere4d5c442005-03-15 04:54:21 +00001582 Function::arg_iterator ArgIt = Fn->arg_begin();
Chris Lattnera8e8f162005-05-06 20:27:19 +00001583 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $5->begin();
1584 I != $5->end(); ++I, ++ArgIt) {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001585 delete I->first; // Delete the typeholder...
1586
Chris Lattner8ab406d2004-07-13 07:59:27 +00001587 setValueName(ArgIt, I->second); // Insert arg into symtab...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001588 InsertValue(ArgIt);
Chris Lattner00950542001-06-06 20:29:01 +00001589 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001590
Chris Lattnera8e8f162005-05-06 20:27:19 +00001591 delete $5; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00001592 }
Chris Lattner51727be2002-06-04 21:58:56 +00001593};
Chris Lattner00950542001-06-06 20:29:01 +00001594
Chris Lattner9b02cc32002-05-03 18:23:48 +00001595BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1596
Chris Lattner4ad02e72003-04-16 20:28:45 +00001597FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
Chris Lattner394f2eb2003-10-16 20:12:13 +00001598 $$ = CurFun.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00001599
Chris Lattner4ad02e72003-04-16 20:28:45 +00001600 // Make sure that we keep track of the linkage type even if there was a
1601 // previous "declare".
1602 $$->setLinkage($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001603};
Chris Lattner00950542001-06-06 20:29:01 +00001604
Chris Lattner9b02cc32002-05-03 18:23:48 +00001605END : ENDTOK | '}'; // Allow end of '}' to end a function
1606
Chris Lattner79df7c02002-03-26 18:01:55 +00001607Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00001608 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001609};
Chris Lattner00950542001-06-06 20:29:01 +00001610
Chris Lattner394f2eb2003-10-16 20:12:13 +00001611FunctionProto : DECLARE { CurFun.isDeclare = true; } FunctionHeaderH {
1612 $$ = CurFun.CurrentFunction;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001613 CurFun.FunctionDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001614};
Chris Lattner00950542001-06-06 20:29:01 +00001615
1616//===----------------------------------------------------------------------===//
1617// Rules to match Basic Blocks
1618//===----------------------------------------------------------------------===//
1619
1620ConstValueRef : ESINT64VAL { // A reference to a direct constant
1621 $$ = ValID::create($1);
1622 }
1623 | EUINT64VAL {
1624 $$ = ValID::create($1);
1625 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001626 | FPVAL { // Perhaps it's an FP constant?
1627 $$ = ValID::create($1);
1628 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001629 | TRUETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001630 $$ = ValID::create(ConstantBool::True);
Chris Lattner00950542001-06-06 20:29:01 +00001631 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001632 | FALSETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001633 $$ = ValID::create(ConstantBool::False);
Chris Lattner00950542001-06-06 20:29:01 +00001634 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001635 | NULL_TOK {
1636 $$ = ValID::createNull();
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001637 }
Chris Lattner16710e92004-10-16 18:17:13 +00001638 | UNDEF {
1639 $$ = ValID::createUndef();
1640 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00001641 | '<' ConstVector '>' { // Nonempty unsized packed vector
1642 const Type *ETy = (*$2)[0]->getType();
1643 int NumElements = $2->size();
1644
1645 PackedType* pt = PackedType::get(ETy, NumElements);
1646 PATypeHolder* PTy = new PATypeHolder(
1647 HandleUpRefs(
1648 PackedType::get(
1649 ETy,
1650 NumElements)
1651 )
1652 );
1653
1654 // Verify all elements are correct type!
1655 for (unsigned i = 0; i < $2->size(); i++) {
1656 if (ETy != (*$2)[i]->getType())
1657 ThrowException("Element #" + utostr(i) + " is not of type '" +
1658 ETy->getDescription() +"' as required!\nIt is of type '" +
1659 (*$2)[i]->getType()->getDescription() + "'.");
1660 }
1661
1662 $$ = ValID::create(ConstantPacked::get(pt, *$2));
1663 delete PTy; delete $2;
1664 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001665 | ConstExpr {
1666 $$ = ValID::create($1);
1667 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00001668
Chris Lattner2079fde2001-10-13 06:41:08 +00001669// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1670// another value.
1671//
1672SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001673 $$ = ValID::create($1);
1674 }
Chris Lattner6c23f572003-08-22 05:42:10 +00001675 | Name { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001676 $$ = ValID::create($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001677 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001678
1679// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00001680ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00001681
Chris Lattner00950542001-06-06 20:29:01 +00001682
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001683// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1684// type immediately preceeds the value reference, and allows complex constant
1685// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001686ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001687 $$ = getVal(*$1, $2); delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001688 };
Chris Lattner8b81bf52001-07-25 22:47:46 +00001689
Chris Lattner00950542001-06-06 20:29:01 +00001690BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001691 $$ = $1;
Chris Lattner00950542001-06-06 20:29:01 +00001692 }
Chris Lattner7e708292002-06-25 16:13:24 +00001693 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
Chris Lattner8ab406d2004-07-13 07:59:27 +00001694 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001695 };
Chris Lattner00950542001-06-06 20:29:01 +00001696
1697
1698// Basic blocks are terminated by branching instructions:
1699// br, br/cc, switch, ret
1700//
Chris Lattner2079fde2001-10-13 06:41:08 +00001701BasicBlock : InstructionList OptAssign BBTerminatorInst {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001702 setValueName($3, $2);
Chris Lattner2079fde2001-10-13 06:41:08 +00001703 InsertValue($3);
1704
1705 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001706 InsertValue($1);
1707 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001708 };
Chris Lattner00950542001-06-06 20:29:01 +00001709
1710InstructionList : InstructionList Inst {
1711 $1->getInstList().push_back($2);
1712 $$ = $1;
1713 }
1714 | /* empty */ {
Chris Lattner1f640252005-05-06 19:49:51 +00001715 $$ = getBBVal(ValID::create((int)CurFun.NextBBNum++), true);
Chris Lattner8d65a062004-07-26 01:40:20 +00001716
1717 // Make sure to move the basic block to the correct location in the
1718 // function, instead of leaving it inserted wherever it was first
1719 // referenced.
Chris Lattnerf924a4c2005-05-06 19:58:35 +00001720 Function::BasicBlockListType &BBL =
1721 CurFun.CurrentFunction->getBasicBlockList();
1722 BBL.splice(BBL.end(), BBL, $$);
Chris Lattner22ae2322004-07-13 08:39:15 +00001723 }
1724 | LABELSTR {
Chris Lattner1f640252005-05-06 19:49:51 +00001725 $$ = getBBVal(ValID::create($1), true);
Chris Lattner8d65a062004-07-26 01:40:20 +00001726
1727 // Make sure to move the basic block to the correct location in the
1728 // function, instead of leaving it inserted wherever it was first
1729 // referenced.
Chris Lattnerf924a4c2005-05-06 19:58:35 +00001730 Function::BasicBlockListType &BBL =
1731 CurFun.CurrentFunction->getBasicBlockList();
1732 BBL.splice(BBL.end(), BBL, $$);
Chris Lattner51727be2002-06-04 21:58:56 +00001733 };
Chris Lattner00950542001-06-06 20:29:01 +00001734
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001735BBTerminatorInst : RET ResolvedVal { // Return with a result...
1736 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001737 }
1738 | RET VOID { // Return with no result...
1739 $$ = new ReturnInst();
1740 }
1741 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner64116f92004-07-14 01:33:11 +00001742 $$ = new BranchInst(getBBVal($3));
Chris Lattner00950542001-06-06 20:29:01 +00001743 } // Conditional Branch...
1744 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner64116f92004-07-14 01:33:11 +00001745 $$ = new BranchInst(getBBVal($6), getBBVal($9), getVal(Type::BoolTy, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001746 }
1747 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Chris Lattnerce1df9e2005-01-29 00:35:55 +00001748 SwitchInst *S = new SwitchInst(getVal($2, $3), getBBVal($6), $8->size());
Chris Lattner00950542001-06-06 20:29:01 +00001749 $$ = S;
1750
Chris Lattner9232b992003-04-22 18:42:41 +00001751 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
Chris Lattner46748042002-04-09 19:41:42 +00001752 E = $8->end();
Chris Lattner69331f52005-02-24 05:25:17 +00001753 for (; I != E; ++I) {
1754 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
1755 S->addCase(CI, I->second);
1756 else
1757 ThrowException("Switch case is constant, but not a simple integer!");
1758 }
Chris Lattnerf57a43d2004-04-17 23:49:15 +00001759 delete $8;
Chris Lattner00950542001-06-06 20:29:01 +00001760 }
Chris Lattner196850c2003-05-15 21:30:00 +00001761 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Chris Lattnerce1df9e2005-01-29 00:35:55 +00001762 SwitchInst *S = new SwitchInst(getVal($2, $3), getBBVal($6), 0);
Chris Lattner196850c2003-05-15 21:30:00 +00001763 $$ = S;
1764 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00001765 | INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
1766 TO LABEL ValueRef UNWIND LABEL ValueRef {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001767 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001768 const FunctionType *Ty;
Chris Lattner2079fde2001-10-13 06:41:08 +00001769
Chris Lattnera8e8f162005-05-06 20:27:19 +00001770 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001771 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00001772 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001773 std::vector<const Type*> ParamTypes;
Chris Lattnera8e8f162005-05-06 20:27:19 +00001774 if ($6) {
1775 for (std::vector<Value*>::iterator I = $6->begin(), E = $6->end();
Chris Lattner9232b992003-04-22 18:42:41 +00001776 I != E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00001777 ParamTypes.push_back((*I)->getType());
1778 }
1779
1780 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1781 if (isVarArg) ParamTypes.pop_back();
1782
Chris Lattnera8e8f162005-05-06 20:27:19 +00001783 Ty = FunctionType::get($3->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001784 PFTy = PointerType::get(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00001785 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001786
Chris Lattnera8e8f162005-05-06 20:27:19 +00001787 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Chris Lattner2079fde2001-10-13 06:41:08 +00001788
Chris Lattnera8e8f162005-05-06 20:27:19 +00001789 BasicBlock *Normal = getBBVal($10);
1790 BasicBlock *Except = getBBVal($13);
Chris Lattner2079fde2001-10-13 06:41:08 +00001791
1792 // Create the call node...
Chris Lattnera8e8f162005-05-06 20:27:19 +00001793 if (!$6) { // Has no arguments?
Chris Lattner9232b992003-04-22 18:42:41 +00001794 $$ = new InvokeInst(V, Normal, Except, std::vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00001795 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001796 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00001797 // correctly!
1798 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00001799 FunctionType::param_iterator I = Ty->param_begin();
1800 FunctionType::param_iterator E = Ty->param_end();
Chris Lattnera8e8f162005-05-06 20:27:19 +00001801 std::vector<Value*>::iterator ArgI = $6->begin(), ArgE = $6->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00001802
1803 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
Reid Spencer3271ed52004-07-18 00:08:11 +00001804 if ((*ArgI)->getType() != *I)
1805 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
1806 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001807
1808 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Reid Spencer3271ed52004-07-18 00:08:11 +00001809 ThrowException("Invalid number of parameters detected!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001810
Chris Lattnera8e8f162005-05-06 20:27:19 +00001811 $$ = new InvokeInst(V, Normal, Except, *$6);
Chris Lattner2079fde2001-10-13 06:41:08 +00001812 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00001813 cast<InvokeInst>($$)->setCallingConv($2);
1814
1815 delete $3;
1816 delete $6;
Chris Lattner36143fc2003-09-08 18:54:55 +00001817 }
1818 | UNWIND {
1819 $$ = new UnwindInst();
Chris Lattner16710e92004-10-16 18:17:13 +00001820 }
1821 | UNREACHABLE {
1822 $$ = new UnreachableInst();
Chris Lattner51727be2002-06-04 21:58:56 +00001823 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001824
1825
Chris Lattner00950542001-06-06 20:29:01 +00001826
1827JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1828 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001829 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001830 if (V == 0)
1831 ThrowException("May only switch on a constant pool value!");
1832
Chris Lattner64116f92004-07-14 01:33:11 +00001833 $$->push_back(std::make_pair(V, getBBVal($6)));
Chris Lattner00950542001-06-06 20:29:01 +00001834 }
1835 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner9232b992003-04-22 18:42:41 +00001836 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001837 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00001838
1839 if (V == 0)
1840 ThrowException("May only switch on a constant pool value!");
1841
Chris Lattner64116f92004-07-14 01:33:11 +00001842 $$->push_back(std::make_pair(V, getBBVal($5)));
Chris Lattner51727be2002-06-04 21:58:56 +00001843 };
Chris Lattner00950542001-06-06 20:29:01 +00001844
1845Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001846 // Is this definition named?? if so, assign the name...
Chris Lattner8ab406d2004-07-13 07:59:27 +00001847 setValueName($2, $1);
Chris Lattner00950542001-06-06 20:29:01 +00001848 InsertValue($2);
1849 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001850};
Chris Lattner00950542001-06-06 20:29:01 +00001851
Chris Lattnerc24d2082001-06-11 15:04:20 +00001852PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Chris Lattner9232b992003-04-22 18:42:41 +00001853 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Chris Lattner64116f92004-07-14 01:33:11 +00001854 $$->push_back(std::make_pair(getVal(*$1, $3), getBBVal($5)));
Chris Lattner30c89792001-09-07 16:35:17 +00001855 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001856 }
1857 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1858 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001859 $1->push_back(std::make_pair(getVal($1->front().first->getType(), $4),
Chris Lattner64116f92004-07-14 01:33:11 +00001860 getBBVal($6)));
Chris Lattner51727be2002-06-04 21:58:56 +00001861 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00001862
1863
Chris Lattner30c89792001-09-07 16:35:17 +00001864ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner9232b992003-04-22 18:42:41 +00001865 $$ = new std::vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001866 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001867 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001868 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001869 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001870 $1->push_back($3);
Chris Lattner51727be2002-06-04 21:58:56 +00001871 };
Chris Lattner00950542001-06-06 20:29:01 +00001872
1873// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Chris Lattner51727be2002-06-04 21:58:56 +00001874ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001875
Chris Lattnerddb6db42005-05-06 05:51:46 +00001876OptTailCall : TAIL CALL {
1877 $$ = true;
1878 }
1879 | CALL {
1880 $$ = false;
1881 };
1882
1883
1884
Chris Lattner4a6482b2002-09-10 19:57:26 +00001885InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Brian Gaeke715c90b2004-08-20 06:00:58 +00001886 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint() &&
1887 !isa<PackedType>((*$2).get()))
1888 ThrowException(
1889 "Arithmetic operator requires integer, FP, or packed operands!");
1890 if(isa<PackedType>((*$2).get()) && $1 == Instruction::Rem) {
1891 ThrowException(
1892 "Rem not supported on packed types!");
1893 }
Chris Lattner4a6482b2002-09-10 19:57:26 +00001894 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1895 if ($$ == 0)
1896 ThrowException("binary operator returned null!");
1897 delete $2;
1898 }
1899 | LogicalOps Types ValueRef ',' ValueRef {
1900 if (!(*$2)->isIntegral())
1901 ThrowException("Logical operator requires integral operands!");
1902 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1903 if ($$ == 0)
1904 ThrowException("binary operator returned null!");
1905 delete $2;
1906 }
1907 | SetCondOps Types ValueRef ',' ValueRef {
Reid Spencer57b6eec2004-08-21 16:11:02 +00001908 if(isa<PackedType>((*$2).get())) {
1909 ThrowException(
1910 "PackedTypes currently not supported in setcc instructions!");
1911 }
Chris Lattner1cff96a2002-09-10 22:37:46 +00001912 $$ = new SetCondInst($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001913 if ($$ == 0)
1914 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001915 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001916 }
Chris Lattner699f1eb2002-08-14 17:12:33 +00001917 | NOT ResolvedVal {
1918 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
1919 << " Replacing with 'xor'.\n";
1920
1921 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
1922 if (Ones == 0)
1923 ThrowException("Expected integral type for not instruction!");
1924
1925 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
Chris Lattner00950542001-06-06 20:29:01 +00001926 if ($$ == 0)
Chris Lattner699f1eb2002-08-14 17:12:33 +00001927 ThrowException("Could not create a xor instruction!");
Chris Lattner09083092001-07-08 04:57:15 +00001928 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001929 | ShiftOps ResolvedVal ',' ResolvedVal {
1930 if ($4->getType() != Type::UByteTy)
1931 ThrowException("Shift amount must be ubyte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001932 if (!$2->getType()->isInteger())
1933 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001934 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001935 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001936 | CAST ResolvedVal TO Types {
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001937 if (!$4->get()->isFirstClassType())
1938 ThrowException("cast instruction to a non-primitive type: '" +
1939 $4->get()->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001940 $$ = new CastInst($2, *$4);
1941 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001942 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001943 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1944 if ($2->getType() != Type::BoolTy)
1945 ThrowException("select condition must be boolean!");
1946 if ($4->getType() != $6->getType())
1947 ThrowException("select value types should match!");
1948 $$ = new SelectInst($2, $4, $6);
1949 }
Chris Lattner99e7ab72003-10-18 05:53:13 +00001950 | VAARG ResolvedVal ',' Types {
1951 $$ = new VAArgInst($2, *$4);
1952 delete $4;
1953 }
1954 | VANEXT ResolvedVal ',' Types {
1955 $$ = new VANextInst($2, *$4);
Chris Lattner8f77dae2003-05-08 02:44:12 +00001956 delete $4;
1957 }
Chris Lattner3b237fc2003-10-19 21:34:28 +00001958 | PHI_TOK PHIList {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001959 const Type *Ty = $2->front().first->getType();
Chris Lattner8ea8f362003-10-30 01:38:18 +00001960 if (!Ty->isFirstClassType())
1961 ThrowException("PHI node operands must be of first class type!");
Chris Lattnerc24d2082001-06-11 15:04:20 +00001962 $$ = new PHINode(Ty);
Chris Lattnerce1df9e2005-01-29 00:35:55 +00001963 ((PHINode*)$$)->reserveOperandSpace($2->size());
Chris Lattner00950542001-06-06 20:29:01 +00001964 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001965 if ($2->front().first->getType() != Ty)
Reid Spencer3271ed52004-07-18 00:08:11 +00001966 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00001967 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001968 $2->pop_front();
1969 }
1970 delete $2; // Free the list...
Chris Lattnerddb6db42005-05-06 05:51:46 +00001971 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00001972 | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001973 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001974 const FunctionType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001975
Chris Lattnera8e8f162005-05-06 20:27:19 +00001976 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001977 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001978 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001979 std::vector<const Type*> ParamTypes;
Chris Lattnera8e8f162005-05-06 20:27:19 +00001980 if ($6) {
1981 for (std::vector<Value*>::iterator I = $6->begin(), E = $6->end();
Chris Lattner9232b992003-04-22 18:42:41 +00001982 I != E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00001983 ParamTypes.push_back((*I)->getType());
1984 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001985
1986 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1987 if (isVarArg) ParamTypes.pop_back();
1988
Chris Lattnera8e8f162005-05-06 20:27:19 +00001989 if (!(*$3)->isFirstClassType() && *$3 != Type::VoidTy)
Chris Lattner595f06c2005-02-01 01:47:42 +00001990 ThrowException("LLVM functions cannot return aggregate types!");
1991
Chris Lattnera8e8f162005-05-06 20:27:19 +00001992 Ty = FunctionType::get($3->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001993 PFTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001994 }
Chris Lattner00950542001-06-06 20:29:01 +00001995
Chris Lattnera8e8f162005-05-06 20:27:19 +00001996 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00001997
Chris Lattner8b81bf52001-07-25 22:47:46 +00001998 // Create the call node...
Chris Lattnera8e8f162005-05-06 20:27:19 +00001999 if (!$6) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00002000 // Make sure no arguments is a good thing!
2001 if (Ty->getNumParams() != 0)
2002 ThrowException("No arguments passed to a function that "
2003 "expects arguments!");
2004
Chris Lattner9232b992003-04-22 18:42:41 +00002005 $$ = new CallInst(V, std::vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00002006 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00002007 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner00950542001-06-06 20:29:01 +00002008 // correctly!
2009 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00002010 FunctionType::param_iterator I = Ty->param_begin();
2011 FunctionType::param_iterator E = Ty->param_end();
Chris Lattnera8e8f162005-05-06 20:27:19 +00002012 std::vector<Value*>::iterator ArgI = $6->begin(), ArgE = $6->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00002013
2014 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
Reid Spencer3271ed52004-07-18 00:08:11 +00002015 if ((*ArgI)->getType() != *I)
2016 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
2017 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00002018
Chris Lattner8b81bf52001-07-25 22:47:46 +00002019 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Reid Spencer3271ed52004-07-18 00:08:11 +00002020 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00002021
Chris Lattnera8e8f162005-05-06 20:27:19 +00002022 $$ = new CallInst(V, *$6);
Chris Lattner8b81bf52001-07-25 22:47:46 +00002023 }
Chris Lattnerddb6db42005-05-06 05:51:46 +00002024 cast<CallInst>($$)->setTailCall($1);
Chris Lattnera8e8f162005-05-06 20:27:19 +00002025 cast<CallInst>($$)->setCallingConv($2);
2026 delete $3;
2027 delete $6;
Chris Lattner00950542001-06-06 20:29:01 +00002028 }
2029 | MemoryInst {
2030 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00002031 };
Chris Lattner00950542001-06-06 20:29:01 +00002032
Chris Lattner6cdb0112001-11-26 16:54:11 +00002033
2034// IndexList - List of indices for GEP based instructions...
2035IndexList : ',' ValueRefList {
Chris Lattner15c9c032003-09-08 18:20:29 +00002036 $$ = $2;
2037 } | /* empty */ {
2038 $$ = new std::vector<Value*>();
2039 };
2040
2041OptVolatile : VOLATILE {
2042 $$ = true;
2043 }
2044 | /* empty */ {
2045 $$ = false;
2046 };
2047
Chris Lattner027dcc52001-07-08 21:10:27 +00002048
Chris Lattnerddb6db42005-05-06 05:51:46 +00002049
Chris Lattner00950542001-06-06 20:29:01 +00002050MemoryInst : MALLOC Types {
Chris Lattner05804b72002-09-13 22:28:45 +00002051 $$ = new MallocInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00002052 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002053 }
2054 | MALLOC Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00002055 $$ = new MallocInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00002056 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002057 }
2058 | ALLOCA Types {
Chris Lattner05804b72002-09-13 22:28:45 +00002059 $$ = new AllocaInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00002060 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002061 }
2062 | ALLOCA Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00002063 $$ = new AllocaInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00002064 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002065 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002066 | FREE ResolvedVal {
Chris Lattner9b625032002-05-06 16:15:30 +00002067 if (!isa<PointerType>($2->getType()))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002068 ThrowException("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00002069 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002070 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00002071 }
2072
Chris Lattner15c9c032003-09-08 18:20:29 +00002073 | OptVolatile LOAD Types ValueRef {
2074 if (!isa<PointerType>($3->get()))
Chris Lattner2079fde2001-10-13 06:41:08 +00002075 ThrowException("Can't load from nonpointer type: " +
Reid Spencer3271ed52004-07-18 00:08:11 +00002076 (*$3)->getDescription());
Chris Lattner1c1bb332004-10-09 02:18:58 +00002077 if (!cast<PointerType>($3->get())->getElementType()->isFirstClassType())
2078 ThrowException("Can't load from pointer of non-first-class type: " +
2079 (*$3)->getDescription());
Chris Lattner79ad13802003-09-08 20:29:46 +00002080 $$ = new LoadInst(getVal(*$3, $4), "", $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002081 delete $3;
Chris Lattner027dcc52001-07-08 21:10:27 +00002082 }
Chris Lattner15c9c032003-09-08 18:20:29 +00002083 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
2084 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002085 if (!PT)
Chris Lattner72e00252001-12-14 16:28:42 +00002086 ThrowException("Can't store to a nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00002087 (*$5)->getDescription());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002088 const Type *ElTy = PT->getElementType();
Chris Lattner15c9c032003-09-08 18:20:29 +00002089 if (ElTy != $3->getType())
2090 ThrowException("Can't store '" + $3->getType()->getDescription() +
Chris Lattner72e00252001-12-14 16:28:42 +00002091 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner0383cc42002-08-21 23:51:21 +00002092
Chris Lattner79ad13802003-09-08 20:29:46 +00002093 $$ = new StoreInst($3, getVal(*$5, $6), $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002094 delete $5;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002095 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00002096 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00002097 if (!isa<PointerType>($2->get()))
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002098 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner830b6f92004-04-05 01:30:04 +00002099
2100 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
2101 // indices to uint struct indices for compatibility.
2102 generic_gep_type_iterator<std::vector<Value*>::iterator>
2103 GTI = gep_type_begin($2->get(), $4->begin(), $4->end()),
2104 GTE = gep_type_end($2->get(), $4->begin(), $4->end());
2105 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
2106 if (isa<StructType>(*GTI)) // Only change struct indices
2107 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
2108 if (CUI->getType() == Type::UByteTy)
2109 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
2110
Chris Lattner30c89792001-09-07 16:35:17 +00002111 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattner830b6f92004-04-05 01:30:04 +00002112 ThrowException("Invalid getelementptr indices for type '" +
2113 (*$2)->getDescription()+ "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00002114 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
2115 delete $2; delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00002116 };
Chris Lattner027dcc52001-07-08 21:10:27 +00002117
Brian Gaeked0fde302003-11-11 22:41:34 +00002118
Chris Lattner00950542001-06-06 20:29:01 +00002119%%
Chris Lattner09083092001-07-08 04:57:15 +00002120int yyerror(const char *ErrorMsg) {
Chris Lattner9232b992003-04-22 18:42:41 +00002121 std::string where
2122 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002123 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Chris Lattner9232b992003-04-22 18:42:41 +00002124 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
Chris Lattnerbc280ab2004-03-30 20:58:25 +00002125 if (yychar == YYEMPTY || yychar == 0)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002126 errMsg += "end-of-file.";
2127 else
Chris Lattner9232b992003-04-22 18:42:41 +00002128 errMsg += "token: '" + std::string(llvmAsmtext, llvmAsmleng) + "'";
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002129 ThrowException(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00002130 return 0;
2131}