blob: 6364b29bcd77d53f850fd7361ed77945c0ae99b4 [file] [log] [blame]
Chris Lattner44d2c352003-10-13 03:32:08 +00001//===-- llvmAsmParser.y - Parser for llvm assembly files --------*- C++ -*-===//
Misha Brukman8b477072005-05-10 22:02:28 +00002//
John Criswell29265fe2003-10-21 15:17:13 +00003// 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.
Misha Brukman8b477072005-05-10 22:02:28 +00007//
John Criswell29265fe2003-10-21 15:17:13 +00008//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
10// This file implements the bison parser for LLVM assembly languages files.
11//
Chris Lattner44d2c352003-10-13 03:32:08 +000012//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +000013
Chris Lattner2f7c9632001-06-06 20:29:01 +000014%{
15#include "ParserInternals.h"
Chris Lattner53bdd312005-05-06 20:27:19 +000016#include "llvm/CallingConv.h"
Chris Lattnera02d6032006-01-25 22:26:43 +000017#include "llvm/InlineAsm.h"
Alkis Evlogimenosfd7a2d42004-07-29 12:17:34 +000018#include "llvm/Instructions.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000019#include "llvm/Module.h"
Reid Spencer3aaaa0b2007-02-05 20:47:22 +000020#include "llvm/ValueSymbolTable.h"
Chandler Carruth7132e002007-08-04 01:51:18 +000021#include "llvm/AutoUpgrade.h"
Chris Lattnerfd9fbe12004-04-05 01:30:04 +000022#include "llvm/Support/GetElementPtrTypeIterator.h"
Reid Spencerfa35bb32006-12-31 05:40:12 +000023#include "llvm/Support/CommandLine.h"
Chris Lattner10aad382007-01-31 04:43:46 +000024#include "llvm/ADT/SmallVector.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000025#include "llvm/ADT/STLExtras.h"
Chris Lattnerd57ed892005-11-06 06:46:28 +000026#include "llvm/Support/MathExtras.h"
Bill Wendling6c1740f2006-11-28 22:47:12 +000027#include "llvm/Support/Streams.h"
Reid Spencer832fa922004-07-04 12:19:05 +000028#include <algorithm>
Chris Lattner2f7c9632001-06-06 20:29:01 +000029#include <list>
Chris Lattner988278c2007-02-11 21:39:35 +000030#include <map>
Chris Lattner3cd8c562002-07-30 18:54:25 +000031#include <utility>
Reid Spencerfa35bb32006-12-31 05:40:12 +000032#ifndef NDEBUG
33#define YYDEBUG 1
34#endif
Chris Lattner2f7c9632001-06-06 20:29:01 +000035
Reid Spencerb50974a2006-08-18 17:32:55 +000036// The following is a gross hack. In order to rid the libAsmParser library of
37// exceptions, we have to have a way of getting the yyparse function to go into
38// an error situation. So, whenever we want an error to occur, the GenerateError
39// function (see bottom of file) sets TriggerError. Then, at the end of each
40// production in the grammer we use CHECK_FOR_ERROR which will invoke YYERROR
41// (a goto) to put YACC in error state. Furthermore, several calls to
42// GenerateError are made from inside productions and they must simulate the
43// previous exception behavior by exiting the production immediately. We have
44// replaced these with the GEN_ERROR macro which calls GeneratError and then
45// immediately invokes YYERROR. This would be so much cleaner if it was a
46// recursive descent parser.
Reid Spencer713eedc2006-08-18 08:43:06 +000047static bool TriggerError = false;
Reid Spencerff359002006-10-09 17:36:59 +000048#define CHECK_FOR_ERROR { if (TriggerError) { TriggerError = false; YYABORT; } }
Reid Spencer713eedc2006-08-18 08:43:06 +000049#define GEN_ERROR(msg) { GenerateError(msg); YYERROR; }
50
Chris Lattner322e49a2001-10-16 19:54:17 +000051int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
Chris Lattnera6821822001-07-08 04:57:15 +000052int yylex(); // declaration" of xxx warnings.
Chris Lattner2f7c9632001-06-06 20:29:01 +000053int yyparse();
54
Brian Gaeke960707c2003-11-11 22:41:34 +000055namespace llvm {
Chris Lattner88357932004-07-14 06:39:48 +000056 std::string CurFilename;
Reid Spencerfa35bb32006-12-31 05:40:12 +000057#if YYDEBUG
58static cl::opt<bool>
59Debug("debug-yacc", cl::desc("Print yacc debug state changes"),
60 cl::Hidden, cl::init(false));
61#endif
Chris Lattner88357932004-07-14 06:39:48 +000062}
63using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000064
Chris Lattner2f7c9632001-06-06 20:29:01 +000065static Module *ParserResult;
Chris Lattner2f7c9632001-06-06 20:29:01 +000066
Chris Lattneraa534bf2001-09-07 16:35:17 +000067// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
68// relating to upreferences in the input stream.
69//
70//#define DEBUG_UPREFS 1
71#ifdef DEBUG_UPREFS
Bill Wendlingf3baad32006-12-07 01:30:32 +000072#define UR_OUT(X) cerr << X
Chris Lattneraa534bf2001-09-07 16:35:17 +000073#else
74#define UR_OUT(X)
75#endif
76
Vikram S. Adve7064eaf2002-07-14 22:59:28 +000077#define YYERROR_VERBOSE 1
78
Chris Lattnerbce7ca8a2005-11-12 18:21:21 +000079static GlobalVariable *CurGV;
Andrew Lenharth9144ec42005-06-18 18:34:52 +000080
81
Chris Lattner113f4f42002-06-25 16:13:24 +000082// This contains info used when building the body of a function. It is
83// destroyed when the function is completed.
Chris Lattner2f7c9632001-06-06 20:29:01 +000084//
Chris Lattner89da8a32003-04-22 18:42:41 +000085typedef std::vector<Value *> ValueList; // Numbered defs
Reid Spencerfa35bb32006-12-31 05:40:12 +000086
Misha Brukman8b477072005-05-10 22:02:28 +000087static void
Reid Spencerfe65ae82007-03-19 18:39:36 +000088ResolveDefinitions(ValueList &LateResolvers, ValueList *FutureLateResolvers=0);
Chris Lattner2f7c9632001-06-06 20:29:01 +000089
90static struct PerModuleInfo {
91 Module *CurrentModule;
Reid Spencerfe65ae82007-03-19 18:39:36 +000092 ValueList Values; // Module level numbered definitions
93 ValueList LateResolveValues;
Reid Spencerd4701792006-11-28 07:28:14 +000094 std::vector<PATypeHolder> Types;
95 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner2f7c9632001-06-06 20:29:01 +000096
Chris Lattnerd9c9c492004-07-13 08:28:21 +000097 /// PlaceHolderInfo - When temporary placeholder objects are created, remember
Reid Spenceredcf47e2006-05-29 02:34:34 +000098 /// how they were referenced and on which line of the input they came from so
Chris Lattnere84a2ba2004-07-13 08:39:15 +000099 /// that we can resolve them later and print error messages as appropriate.
Chris Lattnerd9c9c492004-07-13 08:28:21 +0000100 std::map<Value*, std::pair<ValID, int> > PlaceHolderInfo;
101
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000102 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
103 // references to global values. Global values may be referenced before they
104 // are defined, and if so, the temporary object that they represent is held
Reid Spencerce6adaf2004-07-18 00:08:11 +0000105 // here. This is used for forward references of GlobalValues.
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000106 //
Chris Lattner89da8a32003-04-22 18:42:41 +0000107 typedef std::map<std::pair<const PointerType *,
Chris Lattner75998c02004-03-08 06:09:57 +0000108 ValID>, GlobalValue*> GlobalRefsType;
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000109 GlobalRefsType GlobalRefs;
110
Chris Lattner2f7c9632001-06-06 20:29:01 +0000111 void ModuleDone() {
Chris Lattner113f4f42002-06-25 16:13:24 +0000112 // If we could not resolve some functions at function compilation time
113 // (calls to functions before they are defined), resolve them now... Types
114 // are resolved when the constant pool has been completely parsed.
Chris Lattneraa534bf2001-09-07 16:35:17 +0000115 //
Chris Lattner2f7c9632001-06-06 20:29:01 +0000116 ResolveDefinitions(LateResolveValues);
Reid Spencer309080a2006-09-28 19:28:24 +0000117 if (TriggerError)
118 return;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000119
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000120 // Check to make sure that all global value forward references have been
121 // resolved!
122 //
123 if (!GlobalRefs.empty()) {
Chris Lattner89da8a32003-04-22 18:42:41 +0000124 std::string UndefinedReferences = "Unresolved global references exist:\n";
Misha Brukman8b477072005-05-10 22:02:28 +0000125
Chris Lattner1a8ea8a2002-03-11 22:12:39 +0000126 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
127 I != E; ++I) {
128 UndefinedReferences += " " + I->first.first->getDescription() + " " +
129 I->first.second.getName() + "\n";
130 }
Reid Spencer713eedc2006-08-18 08:43:06 +0000131 GenerateError(UndefinedReferences);
Reid Spencer309080a2006-09-28 19:28:24 +0000132 return;
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000133 }
134
Chandler Carruth7132e002007-08-04 01:51:18 +0000135 // Look for intrinsic functions and CallInst that need to be upgraded
136 for (Module::iterator FI = CurrentModule->begin(),
137 FE = CurrentModule->end(); FI != FE; )
138 UpgradeCallsToIntrinsic(FI++); // must be post-increment, as we remove
139
Chris Lattner113f4f42002-06-25 16:13:24 +0000140 Values.clear(); // Clear out function local definitions
Chris Lattneraa534bf2001-09-07 16:35:17 +0000141 Types.clear();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000142 CurrentModule = 0;
143 }
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000144
Chris Lattner4c9210e2004-07-14 21:44:00 +0000145 // GetForwardRefForGlobal - Check to see if there is a forward reference
146 // for this global. If so, remove it from the GlobalRefs map and return it.
147 // If not, just return null.
Reid Spencer78517652007-04-28 16:06:50 +0000148 GlobalValue *GetForwardRefForGlobal(const PointerType *PTy, ValID ID) {
Chris Lattner4c9210e2004-07-14 21:44:00 +0000149 // Check to see if there is a forward reference to this global variable...
150 // if there is, eliminate it and patch the reference to use the new def'n.
151 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(PTy, ID));
152 GlobalValue *Ret = 0;
153 if (I != GlobalRefs.end()) {
154 Ret = I->second;
Reid Spencer78517652007-04-28 16:06:50 +0000155 GlobalRefs.erase(I);
Chris Lattner4c9210e2004-07-14 21:44:00 +0000156 }
157 return Ret;
158 }
Reid Spencer6130ae02007-01-02 21:53:43 +0000159
160 bool TypeIsUnresolved(PATypeHolder* PATy) {
161 // If it isn't abstract, its resolved
162 const Type* Ty = PATy->get();
163 if (!Ty->isAbstract())
164 return false;
165 // Traverse the type looking for abstract types. If it isn't abstract then
166 // we don't need to traverse that leg of the type.
167 std::vector<const Type*> WorkList, SeenList;
168 WorkList.push_back(Ty);
169 while (!WorkList.empty()) {
170 const Type* Ty = WorkList.back();
171 SeenList.push_back(Ty);
172 WorkList.pop_back();
173 if (const OpaqueType* OpTy = dyn_cast<OpaqueType>(Ty)) {
174 // Check to see if this is an unresolved type
175 std::map<ValID, PATypeHolder>::iterator I = LateResolveTypes.begin();
176 std::map<ValID, PATypeHolder>::iterator E = LateResolveTypes.end();
177 for ( ; I != E; ++I) {
178 if (I->second.get() == OpTy)
179 return true;
180 }
181 } else if (const SequentialType* SeqTy = dyn_cast<SequentialType>(Ty)) {
182 const Type* TheTy = SeqTy->getElementType();
183 if (TheTy->isAbstract() && TheTy != Ty) {
184 std::vector<const Type*>::iterator I = SeenList.begin(),
185 E = SeenList.end();
186 for ( ; I != E; ++I)
187 if (*I == TheTy)
188 break;
189 if (I == E)
190 WorkList.push_back(TheTy);
191 }
192 } else if (const StructType* StrTy = dyn_cast<StructType>(Ty)) {
193 for (unsigned i = 0; i < StrTy->getNumElements(); ++i) {
194 const Type* TheTy = StrTy->getElementType(i);
195 if (TheTy->isAbstract() && TheTy != Ty) {
196 std::vector<const Type*>::iterator I = SeenList.begin(),
197 E = SeenList.end();
198 for ( ; I != E; ++I)
199 if (*I == TheTy)
200 break;
201 if (I == E)
202 WorkList.push_back(TheTy);
203 }
204 }
205 }
206 }
207 return false;
208 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000209} CurModule;
210
Chris Lattner57698e22002-03-26 18:01:55 +0000211static struct PerFunctionInfo {
Chris Lattner113f4f42002-06-25 16:13:24 +0000212 Function *CurrentFunction; // Pointer to current function being created
Chris Lattner2f7c9632001-06-06 20:29:01 +0000213
Reid Spencerfe65ae82007-03-19 18:39:36 +0000214 ValueList Values; // Keep track of #'d definitions
215 unsigned NextValNum;
216 ValueList LateResolveValues;
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000217 bool isDeclare; // Is this function a forward declararation?
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000218 GlobalValue::LinkageTypes Linkage; // Linkage for forward declaration.
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000219 GlobalValue::VisibilityTypes Visibility;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000220
Chris Lattner4accae92004-07-14 06:28:35 +0000221 /// BBForwardRefs - When we see forward references to basic blocks, keep
222 /// track of them here.
Reid Spencerfe65ae82007-03-19 18:39:36 +0000223 std::map<ValID, BasicBlock*> BBForwardRefs;
Chris Lattner4accae92004-07-14 06:28:35 +0000224
Chris Lattner57698e22002-03-26 18:01:55 +0000225 inline PerFunctionInfo() {
226 CurrentFunction = 0;
Chris Lattner17f729e2001-07-15 06:35:53 +0000227 isDeclare = false;
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000228 Linkage = GlobalValue::ExternalLinkage;
229 Visibility = GlobalValue::DefaultVisibility;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000230 }
231
Chris Lattner57698e22002-03-26 18:01:55 +0000232 inline void FunctionStart(Function *M) {
233 CurrentFunction = M;
Reid Spencerfe65ae82007-03-19 18:39:36 +0000234 NextValNum = 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000235 }
236
Chris Lattner57698e22002-03-26 18:01:55 +0000237 void FunctionDone() {
Chris Lattner4accae92004-07-14 06:28:35 +0000238 // Any forward referenced blocks left?
Reid Spencer309080a2006-09-28 19:28:24 +0000239 if (!BBForwardRefs.empty()) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000240 GenerateError("Undefined reference to label " +
Reid Spencerfe65ae82007-03-19 18:39:36 +0000241 BBForwardRefs.begin()->second->getName());
Reid Spencer309080a2006-09-28 19:28:24 +0000242 return;
243 }
Chris Lattner4accae92004-07-14 06:28:35 +0000244
245 // Resolve all forward references now.
Chris Lattner322e49a2001-10-16 19:54:17 +0000246 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000247
Chris Lattner113f4f42002-06-25 16:13:24 +0000248 Values.clear(); // Clear out function local definitions
Reid Spencerfe65ae82007-03-19 18:39:36 +0000249 BBForwardRefs.clear();
Chris Lattner57698e22002-03-26 18:01:55 +0000250 CurrentFunction = 0;
Chris Lattner17f729e2001-07-15 06:35:53 +0000251 isDeclare = false;
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000252 Linkage = GlobalValue::ExternalLinkage;
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000253 Visibility = GlobalValue::DefaultVisibility;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000254 }
Chris Lattner61877742003-10-16 20:12:13 +0000255} CurFun; // Info for the current function...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000256
Chris Lattner61877742003-10-16 20:12:13 +0000257static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
Chris Lattnerfb3e5492001-10-03 15:39:04 +0000258
Chris Lattner2f7c9632001-06-06 20:29:01 +0000259
260//===----------------------------------------------------------------------===//
261// Code to handle definitions of all the types
262//===----------------------------------------------------------------------===//
263
Reid Spencerfe65ae82007-03-19 18:39:36 +0000264static void InsertValue(Value *V, ValueList &ValueTab = CurFun.Values) {
265 // Things that have names or are void typed don't get slot numbers
266 if (V->hasName() || (V->getType() == Type::VoidTy))
267 return;
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000268
Reid Spencerfe65ae82007-03-19 18:39:36 +0000269 // In the case of function values, we have to allow for the forward reference
270 // of basic blocks, which are included in the numbering. Consequently, we keep
271 // track of the next insertion location with NextValNum. When a BB gets
272 // inserted, it could change the size of the CurFun.Values vector.
273 if (&ValueTab == &CurFun.Values) {
274 if (ValueTab.size() <= CurFun.NextValNum)
275 ValueTab.resize(CurFun.NextValNum+1);
276 ValueTab[CurFun.NextValNum++] = V;
277 return;
278 }
279 // For all other lists, its okay to just tack it on the back of the vector.
280 ValueTab.push_back(V);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000281}
282
Chris Lattneraa534bf2001-09-07 16:35:17 +0000283static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000284 switch (D.Type) {
Reid Spencer791b8ef2007-01-26 08:04:51 +0000285 case ValID::LocalID: // Is it a numbered definition?
Chris Lattneraa534bf2001-09-07 16:35:17 +0000286 // Module constants occupy the lowest numbered slots...
Reid Spencer791b8ef2007-01-26 08:04:51 +0000287 if (D.Num < CurModule.Types.size())
288 return CurModule.Types[D.Num];
Chris Lattnerc27b1d72001-10-20 09:32:59 +0000289 break;
Reid Spencer791b8ef2007-01-26 08:04:51 +0000290 case ValID::LocalName: // Is it a named definition?
Reid Spencer3b208cc2007-05-22 18:52:21 +0000291 if (const Type *N = CurModule.CurrentModule->getTypeByName(D.getName())) {
Chris Lattner8abe1a12004-07-14 23:03:46 +0000292 D.destroy(); // Free old strdup'd memory...
293 return N;
Chris Lattner98cf1f52002-11-20 18:36:02 +0000294 }
Chris Lattner8abe1a12004-07-14 23:03:46 +0000295 break;
Chris Lattneraa534bf2001-09-07 16:35:17 +0000296 default:
Reid Spencerde1d05f2007-02-05 10:16:10 +0000297 GenerateError("Internal parser error: Invalid symbol type reference");
Reid Spencer309080a2006-09-28 19:28:24 +0000298 return 0;
Chris Lattneraa534bf2001-09-07 16:35:17 +0000299 }
300
301 // If we reached here, we referenced either a symbol that we don't know about
302 // or an id number that hasn't been read yet. We may be referencing something
303 // forward, so just create an entry to be resolved later and get to it...
304 //
305 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
306
Chris Lattnera5f4b772005-03-21 06:27:42 +0000307
308 if (inFunctionScope()) {
Reid Spencer791b8ef2007-01-26 08:04:51 +0000309 if (D.Type == ValID::LocalName) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000310 GenerateError("Reference to an undefined type: '" + D.getName() + "'");
Reid Spencer309080a2006-09-28 19:28:24 +0000311 return 0;
312 } else {
Reid Spencer791b8ef2007-01-26 08:04:51 +0000313 GenerateError("Reference to an undefined type: #" + utostr(D.Num));
Reid Spencer309080a2006-09-28 19:28:24 +0000314 return 0;
315 }
Chris Lattnerd50fa5a2001-10-22 05:56:09 +0000316 }
Chris Lattneraa534bf2001-09-07 16:35:17 +0000317
Reid Spencerd4701792006-11-28 07:28:14 +0000318 std::map<ValID, PATypeHolder>::iterator I =CurModule.LateResolveTypes.find(D);
Chris Lattnera5f4b772005-03-21 06:27:42 +0000319 if (I != CurModule.LateResolveTypes.end())
Reid Spencerd4701792006-11-28 07:28:14 +0000320 return I->second;
Chris Lattnera5f4b772005-03-21 06:27:42 +0000321
Reid Spencerd4701792006-11-28 07:28:14 +0000322 Type *Typ = OpaqueType::get();
323 CurModule.LateResolveTypes.insert(std::make_pair(D, Typ));
324 return Typ;
Reid Spencer812724e2006-12-03 05:45:44 +0000325 }
Chris Lattneraa534bf2001-09-07 16:35:17 +0000326
Reid Spencerfe65ae82007-03-19 18:39:36 +0000327// getExistingVal - Look up the value specified by the provided type and
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000328// the provided ValID. If the value exists and has already been defined, return
329// it. Otherwise return null.
330//
Reid Spencerfe65ae82007-03-19 18:39:36 +0000331static Value *getExistingVal(const Type *Ty, const ValID &D) {
Reid Spencer309080a2006-09-28 19:28:24 +0000332 if (isa<FunctionType>(Ty)) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000333 GenerateError("Functions are not values and "
Chris Lattner57698e22002-03-26 18:01:55 +0000334 "must be referenced as pointers");
Reid Spencer309080a2006-09-28 19:28:24 +0000335 return 0;
336 }
Chris Lattner322e49a2001-10-16 19:54:17 +0000337
Chris Lattneraa534bf2001-09-07 16:35:17 +0000338 switch (D.Type) {
Reid Spencer791b8ef2007-01-26 08:04:51 +0000339 case ValID::LocalID: { // Is it a numbered definition?
Reid Spencer791b8ef2007-01-26 08:04:51 +0000340 // Check that the number is within bounds.
Reid Spencerfe65ae82007-03-19 18:39:36 +0000341 if (D.Num >= CurFun.Values.size())
342 return 0;
343 Value *Result = CurFun.Values[D.Num];
344 if (Ty != Result->getType()) {
345 GenerateError("Numbered value (%" + utostr(D.Num) + ") of type '" +
346 Result->getType()->getDescription() + "' does not match "
347 "expected type, '" + Ty->getDescription() + "'");
348 return 0;
349 }
350 return Result;
Reid Spencer791b8ef2007-01-26 08:04:51 +0000351 }
352 case ValID::GlobalID: { // Is it a numbered definition?
Reid Spencerfe65ae82007-03-19 18:39:36 +0000353 if (D.Num >= CurModule.Values.size())
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000354 return 0;
Reid Spencerfe65ae82007-03-19 18:39:36 +0000355 Value *Result = CurModule.Values[D.Num];
356 if (Ty != Result->getType()) {
357 GenerateError("Numbered value (@" + utostr(D.Num) + ") of type '" +
358 Result->getType()->getDescription() + "' does not match "
359 "expected type, '" + Ty->getDescription() + "'");
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000360 return 0;
Reid Spencerfe65ae82007-03-19 18:39:36 +0000361 }
362 return Result;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000363 }
Reid Spencer791b8ef2007-01-26 08:04:51 +0000364
365 case ValID::LocalName: { // Is it a named definition?
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000366 if (!inFunctionScope())
367 return 0;
368 ValueSymbolTable &SymTab = CurFun.CurrentFunction->getValueSymbolTable();
Reid Spencer3b208cc2007-05-22 18:52:21 +0000369 Value *N = SymTab.lookup(D.getName());
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000370 if (N == 0)
371 return 0;
372 if (N->getType() != Ty)
373 return 0;
Reid Spencer791b8ef2007-01-26 08:04:51 +0000374
375 D.destroy(); // Free old strdup'd memory...
376 return N;
377 }
378 case ValID::GlobalName: { // Is it a named definition?
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000379 ValueSymbolTable &SymTab = CurModule.CurrentModule->getValueSymbolTable();
Reid Spencer3b208cc2007-05-22 18:52:21 +0000380 Value *N = SymTab.lookup(D.getName());
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000381 if (N == 0)
382 return 0;
383 if (N->getType() != Ty)
384 return 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000385
386 D.destroy(); // Free old strdup'd memory...
387 return N;
388 }
389
Misha Brukman8b477072005-05-10 22:02:28 +0000390 // Check to make sure that "Ty" is an integral type, and that our
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000391 // value will fit into the specified type...
392 case ValID::ConstSIntVal: // Is it a constant pool reference??
Reid Spencere0fc4df2006-10-20 07:07:24 +0000393 if (!ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000394 GenerateError("Signed integral constant '" +
Misha Brukman8b477072005-05-10 22:02:28 +0000395 itostr(D.ConstPool64) + "' is invalid for type '" +
Reid Spencerde1d05f2007-02-05 10:16:10 +0000396 Ty->getDescription() + "'");
Reid Spencer309080a2006-09-28 19:28:24 +0000397 return 0;
398 }
Reid Spencer363fd462007-03-19 20:40:22 +0000399 return ConstantInt::get(Ty, D.ConstPool64, true);
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000400
401 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Reid Spencere0fc4df2006-10-20 07:07:24 +0000402 if (!ConstantInt::isValueValidForType(Ty, D.UConstPool64)) {
403 if (!ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000404 GenerateError("Integral constant '" + utostr(D.UConstPool64) +
Reid Spencerde1d05f2007-02-05 10:16:10 +0000405 "' is invalid or out of range");
Reid Spencer309080a2006-09-28 19:28:24 +0000406 return 0;
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000407 } else { // This is really a signed reference. Transmogrify.
Reid Spencer363fd462007-03-19 20:40:22 +0000408 return ConstantInt::get(Ty, D.ConstPool64, true);
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000409 }
410 } else {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000411 return ConstantInt::get(Ty, D.UConstPool64);
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000412 }
413
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000414 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Dale Johannesenbed9dc42007-09-06 18:13:44 +0000415 if (!ConstantFP::isValueValidForType(Ty, *D.ConstPoolFP)) {
Reid Spencerde1d05f2007-02-05 10:16:10 +0000416 GenerateError("FP constant invalid for type");
Reid Spencer309080a2006-09-28 19:28:24 +0000417 return 0;
418 }
Dale Johannesenbed9dc42007-09-06 18:13:44 +0000419 // Lexer has no type info, so builds all FP constants as double.
420 // Fix this here.
421 if (Ty==Type::FloatTy)
422 D.ConstPoolFP->convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven);
423 return ConstantFP::get(Ty, *D.ConstPoolFP);
Misha Brukman8b477072005-05-10 22:02:28 +0000424
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000425 case ValID::ConstNullVal: // Is it a null value?
Reid Spencer309080a2006-09-28 19:28:24 +0000426 if (!isa<PointerType>(Ty)) {
Reid Spencerde1d05f2007-02-05 10:16:10 +0000427 GenerateError("Cannot create a a non pointer null");
Reid Spencer309080a2006-09-28 19:28:24 +0000428 return 0;
429 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000430 return ConstantPointerNull::get(cast<PointerType>(Ty));
Misha Brukman8b477072005-05-10 22:02:28 +0000431
Chris Lattner4ff31492004-10-16 18:17:13 +0000432 case ValID::ConstUndefVal: // Is it an undef value?
433 return UndefValue::get(Ty);
Misha Brukman8b477072005-05-10 22:02:28 +0000434
Chris Lattner8c7bda52005-12-21 17:53:02 +0000435 case ValID::ConstZeroVal: // Is it a zero value?
436 return Constant::getNullValue(Ty);
437
Chris Lattner7f325cd2002-08-16 21:14:40 +0000438 case ValID::ConstantVal: // Fully resolved constant?
Reid Spencer309080a2006-09-28 19:28:24 +0000439 if (D.ConstantValue->getType() != Ty) {
Reid Spencerde1d05f2007-02-05 10:16:10 +0000440 GenerateError("Constant expression type different from required type");
Reid Spencer309080a2006-09-28 19:28:24 +0000441 return 0;
442 }
Chris Lattner7f325cd2002-08-16 21:14:40 +0000443 return D.ConstantValue;
444
Chris Lattnera02d6032006-01-25 22:26:43 +0000445 case ValID::InlineAsmVal: { // Inline asm expression
446 const PointerType *PTy = dyn_cast<PointerType>(Ty);
447 const FunctionType *FTy =
448 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
Reid Spencer309080a2006-09-28 19:28:24 +0000449 if (!FTy || !InlineAsm::Verify(FTy, D.IAD->Constraints)) {
Reid Spencerde1d05f2007-02-05 10:16:10 +0000450 GenerateError("Invalid type for asm constraint string");
Reid Spencer309080a2006-09-28 19:28:24 +0000451 return 0;
452 }
Chris Lattnera02d6032006-01-25 22:26:43 +0000453 InlineAsm *IA = InlineAsm::get(FTy, D.IAD->AsmString, D.IAD->Constraints,
454 D.IAD->HasSideEffects);
455 D.destroy(); // Free InlineAsmDescriptor.
456 return IA;
457 }
Chris Lattneraa534bf2001-09-07 16:35:17 +0000458 default:
Reid Spencer9894d6c2007-02-05 17:01:20 +0000459 assert(0 && "Unhandled case!");
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000460 return 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000461 } // End of switch
462
Reid Spencer9894d6c2007-02-05 17:01:20 +0000463 assert(0 && "Unhandled case!");
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000464 return 0;
465}
466
Reid Spencerfe65ae82007-03-19 18:39:36 +0000467// getVal - This function is identical to getExistingVal, except that if a
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000468// value is not already defined, it "improvises" by creating a placeholder var
469// that looks and acts just like the requested variable. When the value is
470// defined later, all uses of the placeholder variable are replaced with the
471// real thing.
472//
Chris Lattner95230b02004-07-14 01:33:11 +0000473static Value *getVal(const Type *Ty, const ValID &ID) {
Reid Spencer309080a2006-09-28 19:28:24 +0000474 if (Ty == Type::LabelTy) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000475 GenerateError("Cannot use a basic block here");
Reid Spencer309080a2006-09-28 19:28:24 +0000476 return 0;
477 }
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000478
Chris Lattner95230b02004-07-14 01:33:11 +0000479 // See if the value has already been defined.
Reid Spencerfe65ae82007-03-19 18:39:36 +0000480 Value *V = getExistingVal(Ty, ID);
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000481 if (V) return V;
Reid Spencer309080a2006-09-28 19:28:24 +0000482 if (TriggerError) return 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000483
Reid Spencer309080a2006-09-28 19:28:24 +0000484 if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty)) {
Reid Spencerde1d05f2007-02-05 10:16:10 +0000485 GenerateError("Invalid use of a composite type");
Reid Spencer309080a2006-09-28 19:28:24 +0000486 return 0;
487 }
Chris Lattnerdcb82f52005-03-23 01:29:26 +0000488
Chris Lattner2f7c9632001-06-06 20:29:01 +0000489 // If we reached here, we referenced either a symbol that we don't know about
490 // or an id number that hasn't been read yet. We may be referencing something
491 // forward, so just create an entry to be resolved later and get to it...
492 //
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000493 switch (ID.Type) {
494 case ValID::GlobalName:
Reid Spencerb19636e2007-04-28 14:13:42 +0000495 case ValID::GlobalID: {
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000496 const PointerType *PTy = dyn_cast<PointerType>(Ty);
497 if (!PTy) {
498 GenerateError("Invalid type for reference to global" );
499 return 0;
500 }
501 const Type* ElTy = PTy->getElementType();
502 if (const FunctionType *FTy = dyn_cast<FunctionType>(ElTy))
503 V = new Function(FTy, GlobalValue::ExternalLinkage);
504 else
505 V = new GlobalVariable(ElTy, false, GlobalValue::ExternalLinkage);
506 break;
Reid Spencerb19636e2007-04-28 14:13:42 +0000507 }
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000508 default:
509 V = new Argument(Ty);
510 }
511
Chris Lattnerd9c9c492004-07-13 08:28:21 +0000512 // Remember where this forward reference came from. FIXME, shouldn't we try
513 // to recycle these things??
Chris Lattner95230b02004-07-14 01:33:11 +0000514 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
Chris Lattnerd9c9c492004-07-13 08:28:21 +0000515 llvmAsmlineno)));
516
Chris Lattner57698e22002-03-26 18:01:55 +0000517 if (inFunctionScope())
Chris Lattnerd9c9c492004-07-13 08:28:21 +0000518 InsertValue(V, CurFun.LateResolveValues);
Misha Brukman8b477072005-05-10 22:02:28 +0000519 else
Chris Lattnerd9c9c492004-07-13 08:28:21 +0000520 InsertValue(V, CurModule.LateResolveValues);
521 return V;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000522}
523
Reid Spencerfe65ae82007-03-19 18:39:36 +0000524/// defineBBVal - This is a definition of a new basic block with the specified
525/// identifier which must be the same as CurFun.NextValNum, if its numeric.
526static BasicBlock *defineBBVal(const ValID &ID) {
Reid Spencer9894d6c2007-02-05 17:01:20 +0000527 assert(inFunctionScope() && "Can't get basic block at global scope!");
Chris Lattner95230b02004-07-14 01:33:11 +0000528
Chris Lattner4accae92004-07-14 06:28:35 +0000529 BasicBlock *BB = 0;
Chris Lattner95230b02004-07-14 01:33:11 +0000530
Reid Spencerfe65ae82007-03-19 18:39:36 +0000531 // First, see if this was forward referenced
Chris Lattner95230b02004-07-14 01:33:11 +0000532
Reid Spencerfe65ae82007-03-19 18:39:36 +0000533 std::map<ValID, BasicBlock*>::iterator BBI = CurFun.BBForwardRefs.find(ID);
534 if (BBI != CurFun.BBForwardRefs.end()) {
535 BB = BBI->second;
Chris Lattner4accae92004-07-14 06:28:35 +0000536 // The forward declaration could have been inserted anywhere in the
537 // function: insert it into the correct place now.
538 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
539 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
Reid Spencerfe65ae82007-03-19 18:39:36 +0000540
Reid Spencerd05bc522007-03-20 01:13:00 +0000541 // We're about to erase the entry, save the key so we can clean it up.
542 ValID Tmp = BBI->first;
543
Reid Spencerfe65ae82007-03-19 18:39:36 +0000544 // Erase the forward ref from the map as its no longer "forward"
545 CurFun.BBForwardRefs.erase(ID);
546
Reid Spencerd05bc522007-03-20 01:13:00 +0000547 // The key has been removed from the map but so we don't want to leave
548 // strdup'd memory around so destroy it too.
549 Tmp.destroy();
550
Reid Spencerfe65ae82007-03-19 18:39:36 +0000551 // If its a numbered definition, bump the number and set the BB value.
552 if (ID.Type == ValID::LocalID) {
553 assert(ID.Num == CurFun.NextValNum && "Invalid new block number");
554 InsertValue(BB);
555 }
556
557 ID.destroy();
558 return BB;
559 }
560
561 // We haven't seen this BB before and its first mention is a definition.
562 // Just create it and return it.
Reid Spencer3b208cc2007-05-22 18:52:21 +0000563 std::string Name (ID.Type == ValID::LocalName ? ID.getName() : "");
Reid Spencerfe65ae82007-03-19 18:39:36 +0000564 BB = new BasicBlock(Name, CurFun.CurrentFunction);
565 if (ID.Type == ValID::LocalID) {
566 assert(ID.Num == CurFun.NextValNum && "Invalid new block number");
567 InsertValue(BB);
Chris Lattner4accae92004-07-14 06:28:35 +0000568 }
Reid Spencerfe65ae82007-03-19 18:39:36 +0000569
570 ID.destroy(); // Free strdup'd memory
571 return BB;
572}
573
574/// getBBVal - get an existing BB value or create a forward reference for it.
575///
576static BasicBlock *getBBVal(const ValID &ID) {
577 assert(inFunctionScope() && "Can't get basic block at global scope!");
578
579 BasicBlock *BB = 0;
580
581 std::map<ValID, BasicBlock*>::iterator BBI = CurFun.BBForwardRefs.find(ID);
582 if (BBI != CurFun.BBForwardRefs.end()) {
583 BB = BBI->second;
584 } if (ID.Type == ValID::LocalName) {
Reid Spencer3b208cc2007-05-22 18:52:21 +0000585 std::string Name = ID.getName();
Reid Spencerfe65ae82007-03-19 18:39:36 +0000586 Value *N = CurFun.CurrentFunction->getValueSymbolTable().lookup(Name);
587 if (N)
588 if (N->getType()->getTypeID() == Type::LabelTyID)
589 BB = cast<BasicBlock>(N);
590 else
591 GenerateError("Reference to label '" + Name + "' is actually of type '"+
592 N->getType()->getDescription() + "'");
593 } else if (ID.Type == ValID::LocalID) {
594 if (ID.Num < CurFun.NextValNum && ID.Num < CurFun.Values.size()) {
595 if (CurFun.Values[ID.Num]->getType()->getTypeID() == Type::LabelTyID)
596 BB = cast<BasicBlock>(CurFun.Values[ID.Num]);
597 else
598 GenerateError("Reference to label '%" + utostr(ID.Num) +
599 "' is actually of type '"+
600 CurFun.Values[ID.Num]->getType()->getDescription() + "'");
601 }
602 } else {
603 GenerateError("Illegal label reference " + ID.getName());
604 return 0;
605 }
606
607 // If its already been defined, return it now.
608 if (BB) {
609 ID.destroy(); // Free strdup'd memory.
610 return BB;
611 }
612
613 // Otherwise, this block has not been seen before, create it.
614 std::string Name;
615 if (ID.Type == ValID::LocalName)
Reid Spencer3b208cc2007-05-22 18:52:21 +0000616 Name = ID.getName();
Reid Spencerfe65ae82007-03-19 18:39:36 +0000617 BB = new BasicBlock(Name, CurFun.CurrentFunction);
618
619 // Insert it in the forward refs map.
620 CurFun.BBForwardRefs[ID] = BB;
621
Chris Lattner95230b02004-07-14 01:33:11 +0000622 return BB;
623}
624
Chris Lattner2f7c9632001-06-06 20:29:01 +0000625
626//===----------------------------------------------------------------------===//
627// Code to handle forward references in instructions
628//===----------------------------------------------------------------------===//
629//
630// This code handles the late binding needed with statements that reference
631// values not defined yet... for example, a forward branch, or the PHI node for
632// a loop body.
633//
Chris Lattner61877742003-10-16 20:12:13 +0000634// This keeps a table (CurFun.LateResolveValues) of all such forward references
Chris Lattner2f7c9632001-06-06 20:29:01 +0000635// and back patchs after we are done.
636//
637
Misha Brukman8b477072005-05-10 22:02:28 +0000638// ResolveDefinitions - If we could not resolve some defs at parsing
639// time (forward branches, phi functions for loops, etc...) resolve the
Chris Lattner2f7c9632001-06-06 20:29:01 +0000640// defs now...
641//
Misha Brukman8b477072005-05-10 22:02:28 +0000642static void
Reid Spencerfe65ae82007-03-19 18:39:36 +0000643ResolveDefinitions(ValueList &LateResolvers, ValueList *FutureLateResolvers) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000644 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Reid Spencerfe65ae82007-03-19 18:39:36 +0000645 while (!LateResolvers.empty()) {
646 Value *V = LateResolvers.back();
647 LateResolvers.pop_back();
Chris Lattnerd9c9c492004-07-13 08:28:21 +0000648
Reid Spencerfe65ae82007-03-19 18:39:36 +0000649 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
650 CurModule.PlaceHolderInfo.find(V);
651 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
Chris Lattnerd9c9c492004-07-13 08:28:21 +0000652
Reid Spencerfe65ae82007-03-19 18:39:36 +0000653 ValID &DID = PHI->second.first;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000654
Reid Spencerfe65ae82007-03-19 18:39:36 +0000655 Value *TheRealValue = getExistingVal(V->getType(), DID);
656 if (TriggerError)
657 return;
658 if (TheRealValue) {
659 V->replaceAllUsesWith(TheRealValue);
660 delete V;
661 CurModule.PlaceHolderInfo.erase(PHI);
662 } else if (FutureLateResolvers) {
663 // Functions have their unresolved items forwarded to the module late
664 // resolver table
665 InsertValue(V, *FutureLateResolvers);
666 } else {
667 if (DID.Type == ValID::LocalName || DID.Type == ValID::GlobalName) {
668 GenerateError("Reference to an invalid definition: '" +DID.getName()+
669 "' of type '" + V->getType()->getDescription() + "'",
670 PHI->second.second);
Reid Spencer309080a2006-09-28 19:28:24 +0000671 return;
Chris Lattner322e49a2001-10-16 19:54:17 +0000672 } else {
Reid Spencerfe65ae82007-03-19 18:39:36 +0000673 GenerateError("Reference to an invalid definition: #" +
674 itostr(DID.Num) + " of type '" +
675 V->getType()->getDescription() + "'",
676 PHI->second.second);
677 return;
Chris Lattneraa534bf2001-09-07 16:35:17 +0000678 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000679 }
680 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000681 LateResolvers.clear();
682}
683
Chris Lattnerd50fa5a2001-10-22 05:56:09 +0000684// ResolveTypeTo - A brand new type was just declared. This means that (if
685// name is not null) things referencing Name can be resolved. Otherwise, things
686// refering to the number can be resolved. Do this now.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000687//
Reid Spencer3b208cc2007-05-22 18:52:21 +0000688static void ResolveTypeTo(std::string *Name, const Type *ToTy) {
Chris Lattnera5f4b772005-03-21 06:27:42 +0000689 ValID D;
Reid Spencer3b208cc2007-05-22 18:52:21 +0000690 if (Name)
691 D = ValID::createLocalName(*Name);
692 else
693 D = ValID::createLocalID(CurModule.Types.size());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000694
Reid Spencerd4701792006-11-28 07:28:14 +0000695 std::map<ValID, PATypeHolder>::iterator I =
Chris Lattnera5f4b772005-03-21 06:27:42 +0000696 CurModule.LateResolveTypes.find(D);
697 if (I != CurModule.LateResolveTypes.end()) {
Reid Spencerd4701792006-11-28 07:28:14 +0000698 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattnera5f4b772005-03-21 06:27:42 +0000699 CurModule.LateResolveTypes.erase(I);
Chris Lattneraa534bf2001-09-07 16:35:17 +0000700 }
701}
702
Chris Lattner7fb14f52001-09-18 04:00:54 +0000703// setValueName - Set the specified value to the name given. The name may be
704// null potentially, in which case this is a noop. The string passed in is
Chris Lattner2ed776b2004-07-13 07:59:27 +0000705// assumed to be a malloc'd string buffer, and is free'd by this function.
706//
Reid Spencer3b208cc2007-05-22 18:52:21 +0000707static void setValueName(Value *V, std::string *NameStr) {
Reid Spencer791b8ef2007-01-26 08:04:51 +0000708 if (!NameStr) return;
Reid Spencer3b208cc2007-05-22 18:52:21 +0000709 std::string Name(*NameStr); // Copy string
710 delete NameStr; // Free old string
Chris Lattner30b35cf2004-07-13 08:12:39 +0000711
Reid Spencer791b8ef2007-01-26 08:04:51 +0000712 if (V->getType() == Type::VoidTy) {
Reid Spencerde1d05f2007-02-05 10:16:10 +0000713 GenerateError("Can't assign name '" + Name+"' to value with void type");
Reid Spencer791b8ef2007-01-26 08:04:51 +0000714 return;
Chris Lattner2ed776b2004-07-13 07:59:27 +0000715 }
Reid Spencer791b8ef2007-01-26 08:04:51 +0000716
Reid Spencer9894d6c2007-02-05 17:01:20 +0000717 assert(inFunctionScope() && "Must be in function scope!");
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000718 ValueSymbolTable &ST = CurFun.CurrentFunction->getValueSymbolTable();
719 if (ST.lookup(Name)) {
Reid Spencer791b8ef2007-01-26 08:04:51 +0000720 GenerateError("Redefinition of value '" + Name + "' of type '" +
Reid Spencerde1d05f2007-02-05 10:16:10 +0000721 V->getType()->getDescription() + "'");
Reid Spencer791b8ef2007-01-26 08:04:51 +0000722 return;
723 }
724
725 // Set the name.
726 V->setName(Name);
Chris Lattner2ed776b2004-07-13 07:59:27 +0000727}
728
Chris Lattner42dd4742004-07-14 08:23:52 +0000729/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
730/// this is a declaration, otherwise it is a definition.
Chris Lattnerbce7ca8a2005-11-12 18:21:21 +0000731static GlobalVariable *
Reid Spencer3b208cc2007-05-22 18:52:21 +0000732ParseGlobalVariable(std::string *NameStr,
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000733 GlobalValue::LinkageTypes Linkage,
734 GlobalValue::VisibilityTypes Visibility,
Chris Lattnerbce7ca8a2005-11-12 18:21:21 +0000735 bool isConstantGlobal, const Type *Ty,
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +0000736 Constant *Initializer, bool IsThreadLocal) {
Reid Spencer309080a2006-09-28 19:28:24 +0000737 if (isa<FunctionType>(Ty)) {
Reid Spencerde1d05f2007-02-05 10:16:10 +0000738 GenerateError("Cannot declare global vars of function type");
Reid Spencer309080a2006-09-28 19:28:24 +0000739 return 0;
740 }
Chris Lattnere875d482004-07-14 20:42:57 +0000741
Misha Brukman8b477072005-05-10 22:02:28 +0000742 const PointerType *PTy = PointerType::get(Ty);
Chris Lattner8abe1a12004-07-14 23:03:46 +0000743
Chris Lattnere875d482004-07-14 20:42:57 +0000744 std::string Name;
745 if (NameStr) {
Reid Spencer3b208cc2007-05-22 18:52:21 +0000746 Name = *NameStr; // Copy string
747 delete NameStr; // Free old string
Chris Lattner42dd4742004-07-14 08:23:52 +0000748 }
Chris Lattnere875d482004-07-14 20:42:57 +0000749
Chris Lattner4c9210e2004-07-14 21:44:00 +0000750 // See if this global value was forward referenced. If so, recycle the
751 // object.
Misha Brukman8b477072005-05-10 22:02:28 +0000752 ValID ID;
Chris Lattner4c9210e2004-07-14 21:44:00 +0000753 if (!Name.empty()) {
Reid Spencer3b208cc2007-05-22 18:52:21 +0000754 ID = ValID::createGlobalName(Name);
Chris Lattnere875d482004-07-14 20:42:57 +0000755 } else {
Reid Spencerfe65ae82007-03-19 18:39:36 +0000756 ID = ValID::createGlobalID(CurModule.Values.size());
Chris Lattner4c9210e2004-07-14 21:44:00 +0000757 }
758
Reid Spencer78517652007-04-28 16:06:50 +0000759 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
Misha Brukman8b477072005-05-10 22:02:28 +0000760 // Move the global to the end of the list, from whereever it was
Chris Lattner4c9210e2004-07-14 21:44:00 +0000761 // previously inserted.
762 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
763 CurModule.CurrentModule->getGlobalList().remove(GV);
764 CurModule.CurrentModule->getGlobalList().push_back(GV);
765 GV->setInitializer(Initializer);
766 GV->setLinkage(Linkage);
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000767 GV->setVisibility(Visibility);
Chris Lattner4c9210e2004-07-14 21:44:00 +0000768 GV->setConstant(isConstantGlobal);
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +0000769 GV->setThreadLocal(IsThreadLocal);
Chris Lattnerfe050242004-07-16 01:18:09 +0000770 InsertValue(GV, CurModule.Values);
Chris Lattnerbce7ca8a2005-11-12 18:21:21 +0000771 return GV;
Chris Lattnere875d482004-07-14 20:42:57 +0000772 }
Chris Lattner8abe1a12004-07-14 23:03:46 +0000773
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000774 // If this global has a name
Chris Lattner8abe1a12004-07-14 23:03:46 +0000775 if (!Name.empty()) {
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000776 // if the global we're parsing has an initializer (is a definition) and
777 // has external linkage.
778 if (Initializer && Linkage != GlobalValue::InternalLinkage)
779 // If there is already a global with external linkage with this name
780 if (CurModule.CurrentModule->getGlobalVariable(Name, false)) {
781 // If we allow this GVar to get created, it will be renamed in the
782 // symbol table because it conflicts with an existing GVar. We can't
783 // allow redefinition of GVars whose linking indicates that their name
784 // must stay the same. Issue the error.
785 GenerateError("Redefinition of global variable named '" + Name +
786 "' of type '" + Ty->getDescription() + "'");
787 return 0;
788 }
Chris Lattner8abe1a12004-07-14 23:03:46 +0000789 }
790
791 // Otherwise there is no existing GV to use, create one now.
Chris Lattnerfe050242004-07-16 01:18:09 +0000792 GlobalVariable *GV =
Misha Brukman8b477072005-05-10 22:02:28 +0000793 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +0000794 CurModule.CurrentModule, IsThreadLocal);
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000795 GV->setVisibility(Visibility);
Chris Lattnerfe050242004-07-16 01:18:09 +0000796 InsertValue(GV, CurModule.Values);
Chris Lattnerbce7ca8a2005-11-12 18:21:21 +0000797 return GV;
Chris Lattner42dd4742004-07-14 08:23:52 +0000798}
Chris Lattner2ed776b2004-07-13 07:59:27 +0000799
Reid Spencer5b4413c2004-05-26 21:48:31 +0000800// setTypeName - Set the specified type to the name given. The name may be
801// null potentially, in which case this is a noop. The string passed in is
802// assumed to be a malloc'd string buffer, and is freed by this function.
803//
804// This function returns true if the type has already been defined, but is
805// allowed to be redefined in the specified context. If the name is a new name
806// for the type plane, it is inserted and false is returned.
Reid Spencer3b208cc2007-05-22 18:52:21 +0000807static bool setTypeName(const Type *T, std::string *NameStr) {
Reid Spencer9894d6c2007-02-05 17:01:20 +0000808 assert(!inFunctionScope() && "Can't give types function-local names!");
Reid Spencer5b4413c2004-05-26 21:48:31 +0000809 if (NameStr == 0) return false;
Misha Brukman8b477072005-05-10 22:02:28 +0000810
Reid Spencer3b208cc2007-05-22 18:52:21 +0000811 std::string Name(*NameStr); // Copy string
812 delete NameStr; // Free old string
Reid Spencer5b4413c2004-05-26 21:48:31 +0000813
814 // We don't allow assigning names to void type
Reid Spencer309080a2006-09-28 19:28:24 +0000815 if (T == Type::VoidTy) {
Reid Spencerde1d05f2007-02-05 10:16:10 +0000816 GenerateError("Can't assign name '" + Name + "' to the void type");
Reid Spencer309080a2006-09-28 19:28:24 +0000817 return false;
818 }
Reid Spencer5b4413c2004-05-26 21:48:31 +0000819
Chris Lattner8abe1a12004-07-14 23:03:46 +0000820 // Set the type name, checking for conflicts as we do so.
821 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
Reid Spencer5b4413c2004-05-26 21:48:31 +0000822
Chris Lattner8abe1a12004-07-14 23:03:46 +0000823 if (AlreadyExists) { // Inserting a name that is already defined???
824 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
Reid Spencer9894d6c2007-02-05 17:01:20 +0000825 assert(Existing && "Conflict but no matching type?!");
Chris Lattner8abe1a12004-07-14 23:03:46 +0000826
Reid Spencer5b4413c2004-05-26 21:48:31 +0000827 // There is only one case where this is allowed: when we are refining an
828 // opaque type. In this case, Existing will be an opaque type.
829 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
830 // We ARE replacing an opaque type!
Chris Lattner85a351f2004-07-13 08:10:10 +0000831 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
Reid Spencer5b4413c2004-05-26 21:48:31 +0000832 return true;
833 }
834
835 // Otherwise, this is an attempt to redefine a type. That's okay if
836 // the redefinition is identical to the original. This will be so if
837 // Existing and T point to the same Type object. In this one case we
838 // allow the equivalent redefinition.
839 if (Existing == T) return true; // Yes, it's equal.
840
841 // Any other kind of (non-equivalent) redefinition is an error.
Reid Spencer6c435f02007-01-05 21:50:38 +0000842 GenerateError("Redefinition of type named '" + Name + "' of type '" +
Reid Spencerde1d05f2007-02-05 10:16:10 +0000843 T->getDescription() + "'");
Reid Spencer5b4413c2004-05-26 21:48:31 +0000844 }
845
Reid Spencer5b4413c2004-05-26 21:48:31 +0000846 return false;
847}
Chris Lattnerd9c40e32001-07-09 19:38:36 +0000848
Chris Lattneraa534bf2001-09-07 16:35:17 +0000849//===----------------------------------------------------------------------===//
850// Code for handling upreferences in type names...
Chris Lattnerd9c40e32001-07-09 19:38:36 +0000851//
Chris Lattnerd9c40e32001-07-09 19:38:36 +0000852
Chris Lattner3cb41672004-02-09 03:19:29 +0000853// TypeContains - Returns true if Ty directly contains E in it.
Chris Lattneraa534bf2001-09-07 16:35:17 +0000854//
855static bool TypeContains(const Type *Ty, const Type *E) {
Misha Brukman8b477072005-05-10 22:02:28 +0000856 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
Chris Lattner4270bcb2004-12-08 16:13:53 +0000857 E) != Ty->subtype_end();
Chris Lattner3cb41672004-02-09 03:19:29 +0000858}
859
860namespace {
861 struct UpRefRecord {
862 // NestingLevel - The number of nesting levels that need to be popped before
863 // this type is resolved.
864 unsigned NestingLevel;
Misha Brukman8b477072005-05-10 22:02:28 +0000865
Chris Lattner3cb41672004-02-09 03:19:29 +0000866 // LastContainedTy - This is the type at the current binding level for the
867 // type. Every time we reduce the nesting level, this gets updated.
868 const Type *LastContainedTy;
869
870 // UpRefTy - This is the actual opaque type that the upreference is
871 // represented with.
872 OpaqueType *UpRefTy;
873
874 UpRefRecord(unsigned NL, OpaqueType *URTy)
875 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
876 };
Chris Lattneraa534bf2001-09-07 16:35:17 +0000877}
Chris Lattner1caf0bb2001-07-20 19:15:08 +0000878
Chris Lattnerfd2a5142003-12-31 02:18:11 +0000879// UpRefs - A list of the outstanding upreferences that need to be resolved.
Chris Lattner3cb41672004-02-09 03:19:29 +0000880static std::vector<UpRefRecord> UpRefs;
Chris Lattneraa534bf2001-09-07 16:35:17 +0000881
Chris Lattnerfd2a5142003-12-31 02:18:11 +0000882/// HandleUpRefs - Every time we finish a new layer of types, this function is
883/// called. It loops through the UpRefs vector, which is a list of the
884/// currently active types. For each type, if the up reference is contained in
885/// the newly completed type, we decrement the level count. When the level
886/// count reaches zero, the upreferenced type is the type that is passed in:
887/// thus we can complete the cycle.
888///
Chris Lattner30752bd92002-04-04 19:23:55 +0000889static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner682e17c2006-08-18 17:34:24 +0000890 // If Ty isn't abstract, or if there are no up-references in it, then there is
891 // nothing to resolve here.
892 if (!ty->isAbstract() || UpRefs.empty()) return ty;
893
Chris Lattner30752bd92002-04-04 19:23:55 +0000894 PATypeHolder Ty(ty);
Misha Brukman8b477072005-05-10 22:02:28 +0000895 UR_OUT("Type '" << Ty->getDescription() <<
Chris Lattnerf29b2312001-11-02 07:46:26 +0000896 "' newly formed. Resolving upreferences.\n" <<
897 UpRefs.size() << " upreferences active!\n");
Chris Lattneredd45002004-02-09 18:53:54 +0000898
899 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
900 // to zero), we resolve them all together before we resolve them to Ty. At
901 // the end of the loop, if there is anything to resolve to Ty, it will be in
902 // this variable.
903 OpaqueType *TypeToResolve = 0;
904
Chris Lattnerfd2a5142003-12-31 02:18:11 +0000905 for (unsigned i = 0; i != UpRefs.size(); ++i) {
Misha Brukman8b477072005-05-10 22:02:28 +0000906 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
907 << UpRefs[i].second->getDescription() << ") = "
Reid Spencerce6adaf2004-07-18 00:08:11 +0000908 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
Chris Lattner3cb41672004-02-09 03:19:29 +0000909 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
910 // Decrement level of upreference
911 unsigned Level = --UpRefs[i].NestingLevel;
912 UpRefs[i].LastContainedTy = Ty;
Chris Lattnerfd2a5142003-12-31 02:18:11 +0000913 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
Misha Brukman8b477072005-05-10 22:02:28 +0000914 if (Level == 0) { // Upreference should be resolved!
Chris Lattneredd45002004-02-09 18:53:54 +0000915 if (!TypeToResolve) {
916 TypeToResolve = UpRefs[i].UpRefTy;
917 } else {
918 UR_OUT(" * Resolving upreference for "
919 << UpRefs[i].second->getDescription() << "\n";
920 std::string OldName = UpRefs[i].UpRefTy->getDescription());
921 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
922 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
923 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
924 }
Reid Spencerce6adaf2004-07-18 00:08:11 +0000925 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
Chris Lattnerfd2a5142003-12-31 02:18:11 +0000926 --i; // Do not skip the next element...
Chris Lattneraa534bf2001-09-07 16:35:17 +0000927 }
928 }
Chris Lattnerd9c40e32001-07-09 19:38:36 +0000929 }
Chris Lattnerfd2a5142003-12-31 02:18:11 +0000930
Chris Lattneredd45002004-02-09 18:53:54 +0000931 if (TypeToResolve) {
932 UR_OUT(" * Resolving upreference for "
933 << UpRefs[i].second->getDescription() << "\n";
Chris Lattner7289e622004-02-09 21:03:38 +0000934 std::string OldName = TypeToResolve->getDescription());
Chris Lattneredd45002004-02-09 18:53:54 +0000935 TypeToResolve->refineAbstractTypeTo(Ty);
936 }
937
Chris Lattnerd9c40e32001-07-09 19:38:36 +0000938 return Ty;
939}
940
Chris Lattner2f7c9632001-06-06 20:29:01 +0000941//===----------------------------------------------------------------------===//
942// RunVMAsmParser - Define an interface to this parser
943//===----------------------------------------------------------------------===//
944//
Reid Spencerfa35bb32006-12-31 05:40:12 +0000945static Module* RunParser(Module * M);
946
Chris Lattner88357932004-07-14 06:39:48 +0000947Module *llvm::RunVMAsmParser(const std::string &Filename, FILE *F) {
Chris Lattner416a0d42005-05-20 03:25:47 +0000948 set_scan_file(F);
949
Chris Lattnerf2d1e792001-07-22 18:36:00 +0000950 CurFilename = Filename;
Chris Lattner416a0d42005-05-20 03:25:47 +0000951 return RunParser(new Module(CurFilename));
952}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000953
Chris Lattner416a0d42005-05-20 03:25:47 +0000954Module *llvm::RunVMAsmParser(const char * AsmString, Module * M) {
955 set_scan_string(AsmString);
Chris Lattner6789a0b2003-11-26 07:24:58 +0000956
Chris Lattner416a0d42005-05-20 03:25:47 +0000957 CurFilename = "from_memory";
958 if (M == NULL) {
959 return RunParser(new Module (CurFilename));
960 } else {
961 return RunParser(M);
962 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000963}
964
965%}
966
967%union {
Brian Gaeke960707c2003-11-11 22:41:34 +0000968 llvm::Module *ModuleVal;
969 llvm::Function *FunctionVal;
Brian Gaeke960707c2003-11-11 22:41:34 +0000970 llvm::BasicBlock *BasicBlockVal;
971 llvm::TerminatorInst *TermInstVal;
972 llvm::Instruction *InstVal;
Reid Spencer812724e2006-12-03 05:45:44 +0000973 llvm::Constant *ConstVal;
Chris Lattneraa534bf2001-09-07 16:35:17 +0000974
Reid Spencer812724e2006-12-03 05:45:44 +0000975 const llvm::Type *PrimType;
Reid Spencerfa35bb32006-12-31 05:40:12 +0000976 std::list<llvm::PATypeHolder> *TypeList;
Reid Spencer812724e2006-12-03 05:45:44 +0000977 llvm::PATypeHolder *TypeVal;
978 llvm::Value *ValueVal;
Reid Spencer812724e2006-12-03 05:45:44 +0000979 std::vector<llvm::Value*> *ValueList;
Reid Spencerfa35bb32006-12-31 05:40:12 +0000980 llvm::ArgListType *ArgList;
981 llvm::TypeWithAttrs TypeWithAttrs;
982 llvm::TypeWithAttrsList *TypeWithAttrsList;
983 llvm::ValueRefList *ValueRefList;
984
Misha Brukman8b477072005-05-10 22:02:28 +0000985 // Represent the RHS of PHI node
Reid Spencer812724e2006-12-03 05:45:44 +0000986 std::list<std::pair<llvm::Value*,
987 llvm::BasicBlock*> > *PHIList;
Brian Gaeke960707c2003-11-11 22:41:34 +0000988 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
Reid Spencer812724e2006-12-03 05:45:44 +0000989 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000990
Brian Gaeke960707c2003-11-11 22:41:34 +0000991 llvm::GlobalValue::LinkageTypes Linkage;
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000992 llvm::GlobalValue::VisibilityTypes Visibility;
Reid Spencer71b79e32007-04-09 06:17:21 +0000993 uint16_t ParamAttrs;
Reid Spencer9875fc12007-02-28 02:23:44 +0000994 llvm::APInt *APIntVal;
Chris Lattneraa534bf2001-09-07 16:35:17 +0000995 int64_t SInt64Val;
996 uint64_t UInt64Val;
997 int SIntVal;
998 unsigned UIntVal;
Dale Johannesenbed9dc42007-09-06 18:13:44 +0000999 llvm::APFloat *FPVal;
Chris Lattner7fb14f52001-09-18 04:00:54 +00001000 bool BoolVal;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001001
Reid Spencer3b208cc2007-05-22 18:52:21 +00001002 std::string *StrVal; // This memory must be deleted
1003 llvm::ValID ValIDVal;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001004
Reid Spencer812724e2006-12-03 05:45:44 +00001005 llvm::Instruction::BinaryOps BinaryOpVal;
1006 llvm::Instruction::TermOps TermOpVal;
1007 llvm::Instruction::MemoryOps MemOpVal;
1008 llvm::Instruction::CastOps CastOpVal;
1009 llvm::Instruction::OtherOps OtherOpVal;
Reid Spencer812724e2006-12-03 05:45:44 +00001010 llvm::ICmpInst::Predicate IPredicate;
1011 llvm::FCmpInst::Predicate FPredicate;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001012}
1013
Reid Spencerfa35bb32006-12-31 05:40:12 +00001014%type <ModuleVal> Module
Chris Lattner57698e22002-03-26 18:01:55 +00001015%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner2f7c9632001-06-06 20:29:01 +00001016%type <BasicBlockVal> BasicBlock InstructionList
1017%type <TermInstVal> BBTerminatorInst
1018%type <InstVal> Inst InstVal MemoryInst
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +00001019%type <ConstVal> ConstVal ConstExpr AliaseeRef
Chris Lattner476148f2001-11-26 16:54:11 +00001020%type <ConstVector> ConstVector
Chris Lattnerae234e12002-04-09 19:41:42 +00001021%type <ArgList> ArgList ArgListH
Chris Lattner931ef3b2001-06-11 15:04:20 +00001022%type <PHIList> PHIList
Reid Spencerfa35bb32006-12-31 05:40:12 +00001023%type <ValueRefList> ValueRefList // For call param lists & GEP indices
1024%type <ValueList> IndexList // For GEP indices
1025%type <TypeList> TypeListI
1026%type <TypeWithAttrsList> ArgTypeList ArgTypeListI
Reid Spencer136a91c2007-01-05 17:06:19 +00001027%type <TypeWithAttrs> ArgType
Chris Lattner2f7c9632001-06-06 20:29:01 +00001028%type <JumpTable> JumpTable
Chris Lattner379a8d22003-04-16 20:28:45 +00001029%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +00001030%type <BoolVal> ThreadLocal // 'thread_local' or not
Chris Lattner8b1680e2003-09-08 18:20:29 +00001031%type <BoolVal> OptVolatile // 'volatile' or not
Chris Lattner06038452005-05-06 05:51:46 +00001032%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
Chris Lattnera02d6032006-01-25 22:26:43 +00001033%type <BoolVal> OptSideEffect // 'sideeffect' or not.
Reid Spencerfa35bb32006-12-31 05:40:12 +00001034%type <Linkage> GVInternalLinkage GVExternalLinkage
1035%type <Linkage> FunctionDefineLinkage FunctionDeclareLinkage
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001036%type <Linkage> AliasLinkage
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001037%type <Visibility> GVVisibilityStyle
Chris Lattner2f7c9632001-06-06 20:29:01 +00001038
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001039// ValueRef - Unresolved reference to a definition or BB
1040%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattner252afba2001-07-26 16:29:15 +00001041%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner2f7c9632001-06-06 20:29:01 +00001042// Tokens and types for handling constant integer values
1043//
1044// ESINT64VAL - A negative number within long long range
1045%token <SInt64Val> ESINT64VAL
1046
1047// EUINT64VAL - A positive number within uns. long long range
1048%token <UInt64Val> EUINT64VAL
Chris Lattner2f7c9632001-06-06 20:29:01 +00001049
Reid Spencer9875fc12007-02-28 02:23:44 +00001050// ESAPINTVAL - A negative number with arbitrary precision
1051%token <APIntVal> ESAPINTVAL
1052
1053// EUAPINTVAL - A positive number with arbitrary precision
1054%token <APIntVal> EUAPINTVAL
1055
Reid Spencer791b8ef2007-01-26 08:04:51 +00001056%token <UIntVal> LOCALVAL_ID GLOBALVAL_ID // %123 @123
Chris Lattner212f70d2001-07-15 00:17:01 +00001057%token <FPVal> FPVAL // Float or Double constant
Chris Lattner2f7c9632001-06-06 20:29:01 +00001058
1059// Built in types...
Reid Spencer136a91c2007-01-05 17:06:19 +00001060%type <TypeVal> Types ResultTypes
Reid Spencerfa35bb32006-12-31 05:40:12 +00001061%type <PrimType> IntType FPType PrimType // Classifications
Reid Spencer502d64e2007-01-13 05:00:20 +00001062%token <PrimType> VOID INTTYPE
Dale Johannesenff4c3be2007-08-03 01:03:46 +00001063%token <PrimType> FLOAT DOUBLE X86_FP80 FP128 PPC_FP128 LABEL
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00001064%token TYPE
Chris Lattner2f7c9632001-06-06 20:29:01 +00001065
Reid Spencer3b208cc2007-05-22 18:52:21 +00001066
Reid Spenceraba0cc72007-05-19 07:21:26 +00001067%token<StrVal> LOCALVAR GLOBALVAR LABELSTR
1068%token<StrVal> STRINGCONSTANT ATSTRINGCONSTANT PCTSTRINGCONSTANT
Reid Spencer791b8ef2007-01-26 08:04:51 +00001069%type <StrVal> LocalName OptLocalName OptLocalAssign
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001070%type <StrVal> GlobalName OptGlobalAssign GlobalAssign
Chris Lattnerbce7ca8a2005-11-12 18:21:21 +00001071%type <StrVal> OptSection SectionString
Chris Lattner2f7c9632001-06-06 20:29:01 +00001072
Reid Spencer3b208cc2007-05-22 18:52:21 +00001073%type <UIntVal> OptAlign OptCAlign
1074
Reid Spencerbef90fe2007-04-09 01:55:42 +00001075%token ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001076%token DECLARE DEFINE GLOBAL CONSTANT SECTION ALIAS VOLATILE THREAD_LOCAL
Reid Spencerfa35bb32006-12-31 05:40:12 +00001077%token TO DOTDOTDOT NULL_TOK UNDEF INTERNAL LINKONCE WEAK APPENDING
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00001078%token DLLIMPORT DLLEXPORT EXTERN_WEAK
Reid Spencer791b8ef2007-01-26 08:04:51 +00001079%token OPAQUE EXTERNAL TARGET TRIPLE ALIGN
Chris Lattnera02d6032006-01-25 22:26:43 +00001080%token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
Anton Korobeynikov037c8672007-01-28 13:31:35 +00001081%token CC_TOK CCC_TOK FASTCC_TOK COLDCC_TOK X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
Chris Lattnerfd3d29a2006-10-22 06:07:41 +00001082%token DATALAYOUT
Chris Lattner53bdd312005-05-06 20:27:19 +00001083%type <UIntVal> OptCallingConv
Reid Spencer136a91c2007-01-05 17:06:19 +00001084%type <ParamAttrs> OptParamAttrs ParamAttr
1085%type <ParamAttrs> OptFuncAttrs FuncAttr
Chris Lattner2f7c9632001-06-06 20:29:01 +00001086
Misha Brukman8b477072005-05-10 22:02:28 +00001087// Basic Block Terminating Operators
Chris Lattner4ff31492004-10-16 18:17:13 +00001088%token <TermOpVal> RET BR SWITCH INVOKE UNWIND UNREACHABLE
Chris Lattner2f7c9632001-06-06 20:29:01 +00001089
Misha Brukman8b477072005-05-10 22:02:28 +00001090// Binary Operators
Reid Spencer266e42b2006-12-23 06:05:41 +00001091%type <BinaryOpVal> ArithmeticOps LogicalOps // Binops Subcatagories
Reid Spencer7eb55b32006-11-02 01:53:59 +00001092%token <BinaryOpVal> ADD SUB MUL UDIV SDIV FDIV UREM SREM FREM AND OR XOR
Reid Spencer2341c222007-02-02 02:16:23 +00001093%token <BinaryOpVal> SHL LSHR ASHR
1094
Reid Spencer812724e2006-12-03 05:45:44 +00001095%token <OtherOpVal> ICMP FCMP
Reid Spencer812724e2006-12-03 05:45:44 +00001096%type <IPredicate> IPredicates
Reid Spencer812724e2006-12-03 05:45:44 +00001097%type <FPredicate> FPredicates
Reid Spencercfc7f5d2006-12-03 06:58:07 +00001098%token EQ NE SLT SGT SLE SGE ULT UGT ULE UGE
1099%token OEQ ONE OLT OGT OLE OGE ORD UNO UEQ UNE
Chris Lattner2f7c9632001-06-06 20:29:01 +00001100
1101// Memory Instructions
Vikram S. Adve7064eaf2002-07-14 22:59:28 +00001102%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner2f7c9632001-06-06 20:29:01 +00001103
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001104// Cast Operators
1105%type <CastOpVal> CastOps
1106%token <CastOpVal> TRUNC ZEXT SEXT FPTRUNC FPEXT BITCAST
1107%token <CastOpVal> UITOFP SITOFP FPTOUI FPTOSI INTTOPTR PTRTOINT
1108
Chris Lattnerd8bebcd2001-07-08 21:10:27 +00001109// Other Operators
Reid Spencer2341c222007-02-02 02:16:23 +00001110%token <OtherOpVal> PHI_TOK SELECT VAARG
Chris Lattner8ec3c2e2006-04-08 01:18:35 +00001111%token <OtherOpVal> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
Chris Lattner06038452005-05-06 05:51:46 +00001112
Reid Spencer136a91c2007-01-05 17:06:19 +00001113// Function Attributes
Duncan Sands644f9172007-07-27 12:58:54 +00001114%token SIGNEXT ZEROEXT NORETURN INREG SRET NOUNWIND NOALIAS BYVAL NEST
Chris Lattnerd8bebcd2001-07-08 21:10:27 +00001115
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001116// Visibility Styles
Anton Korobeynikov39f3cff2007-04-29 18:35:00 +00001117%token DEFAULT HIDDEN PROTECTED
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001118
Chris Lattner2f7c9632001-06-06 20:29:01 +00001119%start Module
1120%%
1121
Chris Lattner2f7c9632001-06-06 20:29:01 +00001122
Misha Brukman8b477072005-05-10 22:02:28 +00001123// Operations that are notably excluded from this list include:
Chris Lattner2f7c9632001-06-06 20:29:01 +00001124// RET, BR, & SWITCH because they end basic blocks and are treated specially.
1125//
Reid Spencer7eb55b32006-11-02 01:53:59 +00001126ArithmeticOps: ADD | SUB | MUL | UDIV | SDIV | FDIV | UREM | SREM | FREM;
Reid Spencer2341c222007-02-02 02:16:23 +00001127LogicalOps : SHL | LSHR | ASHR | AND | OR | XOR;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001128CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | BITCAST |
1129 UITOFP | SITOFP | FPTOUI | FPTOSI | INTTOPTR | PTRTOINT;
Reid Spencer2341c222007-02-02 02:16:23 +00001130
Reid Spencercfc7f5d2006-12-03 06:58:07 +00001131IPredicates
Reid Spencerf3490692006-12-04 05:20:06 +00001132 : EQ { $$ = ICmpInst::ICMP_EQ; } | NE { $$ = ICmpInst::ICMP_NE; }
Reid Spencercfc7f5d2006-12-03 06:58:07 +00001133 | SLT { $$ = ICmpInst::ICMP_SLT; } | SGT { $$ = ICmpInst::ICMP_SGT; }
1134 | SLE { $$ = ICmpInst::ICMP_SLE; } | SGE { $$ = ICmpInst::ICMP_SGE; }
1135 | ULT { $$ = ICmpInst::ICMP_ULT; } | UGT { $$ = ICmpInst::ICMP_UGT; }
1136 | ULE { $$ = ICmpInst::ICMP_ULE; } | UGE { $$ = ICmpInst::ICMP_UGE; }
1137 ;
1138
1139FPredicates
1140 : OEQ { $$ = FCmpInst::FCMP_OEQ; } | ONE { $$ = FCmpInst::FCMP_ONE; }
1141 | OLT { $$ = FCmpInst::FCMP_OLT; } | OGT { $$ = FCmpInst::FCMP_OGT; }
1142 | OLE { $$ = FCmpInst::FCMP_OLE; } | OGE { $$ = FCmpInst::FCMP_OGE; }
1143 | ORD { $$ = FCmpInst::FCMP_ORD; } | UNO { $$ = FCmpInst::FCMP_UNO; }
1144 | UEQ { $$ = FCmpInst::FCMP_UEQ; } | UNE { $$ = FCmpInst::FCMP_UNE; }
1145 | ULT { $$ = FCmpInst::FCMP_ULT; } | UGT { $$ = FCmpInst::FCMP_UGT; }
1146 | ULE { $$ = FCmpInst::FCMP_ULE; } | UGE { $$ = FCmpInst::FCMP_UGE; }
1147 | TRUETOK { $$ = FCmpInst::FCMP_TRUE; }
1148 | FALSETOK { $$ = FCmpInst::FCMP_FALSE; }
1149 ;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001150
Chris Lattner56f73d42001-07-14 06:10:16 +00001151// These are some types that allow classification if we only want a particular
1152// thing... for example, only a signed, unsigned, or integral type.
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001153IntType : INTTYPE;
Dale Johannesenff4c3be2007-08-03 01:03:46 +00001154FPType : FLOAT | DOUBLE | PPC_FP128 | FP128 | X86_FP80;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001155
Reid Spencer3b208cc2007-05-22 18:52:21 +00001156LocalName : LOCALVAR | STRINGCONSTANT | PCTSTRINGCONSTANT ;
Reid Spencer791b8ef2007-01-26 08:04:51 +00001157OptLocalName : LocalName | /*empty*/ { $$ = 0; };
1158
1159/// OptLocalAssign - Value producing statements have an optional assignment
1160/// component.
1161OptLocalAssign : LocalName '=' {
1162 $$ = $1;
1163 CHECK_FOR_ERROR
1164 }
1165 | /*empty*/ {
1166 $$ = 0;
1167 CHECK_FOR_ERROR
1168 };
1169
Reid Spencer3b208cc2007-05-22 18:52:21 +00001170GlobalName : GLOBALVAR | ATSTRINGCONSTANT ;
Reid Spencer791b8ef2007-01-26 08:04:51 +00001171
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001172OptGlobalAssign : GlobalAssign
Misha Brukman8b477072005-05-10 22:02:28 +00001173 | /*empty*/ {
1174 $$ = 0;
Reid Spencer713eedc2006-08-18 08:43:06 +00001175 CHECK_FOR_ERROR
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001176 };
Chris Lattner2f7c9632001-06-06 20:29:01 +00001177
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001178GlobalAssign : GlobalName '=' {
1179 $$ = $1;
1180 CHECK_FOR_ERROR
Anton Korobeynikov5e4e8f52007-04-25 18:07:40 +00001181 };
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001182
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00001183GVInternalLinkage
1184 : INTERNAL { $$ = GlobalValue::InternalLinkage; }
1185 | WEAK { $$ = GlobalValue::WeakLinkage; }
1186 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1187 | APPENDING { $$ = GlobalValue::AppendingLinkage; }
1188 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
1189 ;
1190
1191GVExternalLinkage
1192 : DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1193 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
1194 | EXTERNAL { $$ = GlobalValue::ExternalLinkage; }
1195 ;
1196
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001197GVVisibilityStyle
Anton Korobeynikov39f3cff2007-04-29 18:35:00 +00001198 : /*empty*/ { $$ = GlobalValue::DefaultVisibility; }
1199 | DEFAULT { $$ = GlobalValue::DefaultVisibility; }
1200 | HIDDEN { $$ = GlobalValue::HiddenVisibility; }
1201 | PROTECTED { $$ = GlobalValue::ProtectedVisibility; }
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001202 ;
1203
Reid Spencerfa35bb32006-12-31 05:40:12 +00001204FunctionDeclareLinkage
1205 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1206 | DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1207 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00001208 ;
1209
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001210FunctionDefineLinkage
Reid Spencerfa35bb32006-12-31 05:40:12 +00001211 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1212 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00001213 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1214 | WEAK { $$ = GlobalValue::WeakLinkage; }
1215 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00001216 ;
Chris Lattneraa534bf2001-09-07 16:35:17 +00001217
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001218AliasLinkage
1219 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1220 | WEAK { $$ = GlobalValue::WeakLinkage; }
1221 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
1222 ;
1223
Anton Korobeynikov6f7072c2006-09-17 20:25:45 +00001224OptCallingConv : /*empty*/ { $$ = CallingConv::C; } |
1225 CCC_TOK { $$ = CallingConv::C; } |
Anton Korobeynikov6f7072c2006-09-17 20:25:45 +00001226 FASTCC_TOK { $$ = CallingConv::Fast; } |
1227 COLDCC_TOK { $$ = CallingConv::Cold; } |
1228 X86_STDCALLCC_TOK { $$ = CallingConv::X86_StdCall; } |
1229 X86_FASTCALLCC_TOK { $$ = CallingConv::X86_FastCall; } |
1230 CC_TOK EUINT64VAL {
Chris Lattner53bdd312005-05-06 20:27:19 +00001231 if ((unsigned)$2 != $2)
Reid Spencerde1d05f2007-02-05 10:16:10 +00001232 GEN_ERROR("Calling conv too large");
Chris Lattner53bdd312005-05-06 20:27:19 +00001233 $$ = $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00001234 CHECK_FOR_ERROR
Chris Lattner53bdd312005-05-06 20:27:19 +00001235 };
1236
Reid Spencer314e1cb2007-07-19 23:13:04 +00001237ParamAttr : ZEROEXT { $$ = ParamAttr::ZExt; }
Reid Spencerd45d07a2007-07-31 02:57:37 +00001238 | ZEXT { $$ = ParamAttr::ZExt; }
Reid Spencer314e1cb2007-07-19 23:13:04 +00001239 | SIGNEXT { $$ = ParamAttr::SExt; }
Reid Spencerd45d07a2007-07-31 02:57:37 +00001240 | SEXT { $$ = ParamAttr::SExt; }
Zhou Sheng2444a9a2007-06-05 05:28:26 +00001241 | INREG { $$ = ParamAttr::InReg; }
1242 | SRET { $$ = ParamAttr::StructRet; }
1243 | NOALIAS { $$ = ParamAttr::NoAlias; }
Duncan Sands644f9172007-07-27 12:58:54 +00001244 | BYVAL { $$ = ParamAttr::ByVal; }
1245 | NEST { $$ = ParamAttr::Nest; }
Reid Spencerfa35bb32006-12-31 05:40:12 +00001246 ;
1247
Reid Spencera472f662007-04-11 02:44:20 +00001248OptParamAttrs : /* empty */ { $$ = ParamAttr::None; }
Reid Spencer136a91c2007-01-05 17:06:19 +00001249 | OptParamAttrs ParamAttr {
Reid Spencer71b79e32007-04-09 06:17:21 +00001250 $$ = $1 | $2;
Reid Spencerfa35bb32006-12-31 05:40:12 +00001251 }
1252 ;
1253
Reid Spencera472f662007-04-11 02:44:20 +00001254FuncAttr : NORETURN { $$ = ParamAttr::NoReturn; }
1255 | NOUNWIND { $$ = ParamAttr::NoUnwind; }
Reid Spencer314e1cb2007-07-19 23:13:04 +00001256 | ZEROEXT { $$ = ParamAttr::ZExt; }
1257 | SIGNEXT { $$ = ParamAttr::SExt; }
Reid Spencer136a91c2007-01-05 17:06:19 +00001258 ;
1259
Reid Spencera472f662007-04-11 02:44:20 +00001260OptFuncAttrs : /* empty */ { $$ = ParamAttr::None; }
Reid Spencer136a91c2007-01-05 17:06:19 +00001261 | OptFuncAttrs FuncAttr {
Reid Spencer71b79e32007-04-09 06:17:21 +00001262 $$ = $1 | $2;
Reid Spencer136a91c2007-01-05 17:06:19 +00001263 }
Reid Spencerfa35bb32006-12-31 05:40:12 +00001264 ;
1265
Chris Lattnerc7de8362005-11-06 06:34:12 +00001266// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1267// a comma before it.
Chris Lattnerd57ed892005-11-06 06:46:28 +00001268OptAlign : /*empty*/ { $$ = 0; } |
Chris Lattnerbce7ca8a2005-11-12 18:21:21 +00001269 ALIGN EUINT64VAL {
1270 $$ = $2;
1271 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencerde1d05f2007-02-05 10:16:10 +00001272 GEN_ERROR("Alignment must be a power of two");
Reid Spencer713eedc2006-08-18 08:43:06 +00001273 CHECK_FOR_ERROR
Chris Lattnerbce7ca8a2005-11-12 18:21:21 +00001274};
Chris Lattnerc7de8362005-11-06 06:34:12 +00001275OptCAlign : /*empty*/ { $$ = 0; } |
Chris Lattnerbce7ca8a2005-11-12 18:21:21 +00001276 ',' ALIGN EUINT64VAL {
1277 $$ = $3;
1278 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencerde1d05f2007-02-05 10:16:10 +00001279 GEN_ERROR("Alignment must be a power of two");
Reid Spencer713eedc2006-08-18 08:43:06 +00001280 CHECK_FOR_ERROR
Chris Lattnerbce7ca8a2005-11-12 18:21:21 +00001281};
1282
Chris Lattnerc7de8362005-11-06 06:34:12 +00001283
Chris Lattner71b936c2005-11-12 00:11:10 +00001284SectionString : SECTION STRINGCONSTANT {
Reid Spencer3b208cc2007-05-22 18:52:21 +00001285 for (unsigned i = 0, e = $2->length(); i != e; ++i)
1286 if ((*$2)[i] == '"' || (*$2)[i] == '\\')
Reid Spencerde1d05f2007-02-05 10:16:10 +00001287 GEN_ERROR("Invalid character in section name");
Chris Lattner71b936c2005-11-12 00:11:10 +00001288 $$ = $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00001289 CHECK_FOR_ERROR
Chris Lattner71b936c2005-11-12 00:11:10 +00001290};
1291
1292OptSection : /*empty*/ { $$ = 0; } |
1293 SectionString { $$ = $1; };
Chris Lattner71b936c2005-11-12 00:11:10 +00001294
Chris Lattnerbce7ca8a2005-11-12 18:21:21 +00001295// GlobalVarAttributes - Used to pass the attributes string on a global. CurGV
1296// is set to be the global we are processing.
1297//
1298GlobalVarAttributes : /* empty */ {} |
1299 ',' GlobalVarAttribute GlobalVarAttributes {};
1300GlobalVarAttribute : SectionString {
Reid Spencer3b208cc2007-05-22 18:52:21 +00001301 CurGV->setSection(*$1);
1302 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001303 CHECK_FOR_ERROR
Chris Lattnerbce7ca8a2005-11-12 18:21:21 +00001304 }
1305 | ALIGN EUINT64VAL {
1306 if ($2 != 0 && !isPowerOf2_32($2))
Reid Spencerde1d05f2007-02-05 10:16:10 +00001307 GEN_ERROR("Alignment must be a power of two");
Chris Lattnerbce7ca8a2005-11-12 18:21:21 +00001308 CurGV->setAlignment($2);
Reid Spencer713eedc2006-08-18 08:43:06 +00001309 CHECK_FOR_ERROR
Chris Lattnerbce7ca8a2005-11-12 18:21:21 +00001310 };
Chris Lattner71b936c2005-11-12 00:11:10 +00001311
Chris Lattneraa534bf2001-09-07 16:35:17 +00001312//===----------------------------------------------------------------------===//
1313// Types includes all predefined types... except void, because it can only be
Reid Spencerfa35bb32006-12-31 05:40:12 +00001314// used in specific contexts (function returning void for example).
Chris Lattneraa534bf2001-09-07 16:35:17 +00001315
1316// Derived types are added later...
1317//
Dale Johannesenff4c3be2007-08-03 01:03:46 +00001318PrimType : INTTYPE | FLOAT | DOUBLE | PPC_FP128 | FP128 | X86_FP80 | LABEL ;
Reid Spencerfa35bb32006-12-31 05:40:12 +00001319
1320Types
1321 : OPAQUE {
Reid Spencer812724e2006-12-03 05:45:44 +00001322 $$ = new PATypeHolder(OpaqueType::get());
Reid Spencer713eedc2006-08-18 08:43:06 +00001323 CHECK_FOR_ERROR
Chris Lattner30752bd92002-04-04 19:23:55 +00001324 }
1325 | PrimType {
Reid Spencer812724e2006-12-03 05:45:44 +00001326 $$ = new PATypeHolder($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00001327 CHECK_FOR_ERROR
Reid Spencerfa35bb32006-12-31 05:40:12 +00001328 }
1329 | Types '*' { // Pointer type?
1330 if (*$1 == Type::LabelTy)
1331 GEN_ERROR("Cannot form a pointer to a basic block");
1332 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
1333 delete $1;
1334 CHECK_FOR_ERROR
1335 }
1336 | SymbolicValueRef { // Named types are also simple types...
1337 const Type* tmp = getTypeVal($1);
1338 CHECK_FOR_ERROR
1339 $$ = new PATypeHolder(tmp);
1340 }
1341 | '\\' EUINT64VAL { // Type UpReference
Reid Spencerde1d05f2007-02-05 10:16:10 +00001342 if ($2 > (uint64_t)~0U) GEN_ERROR("Value out of range");
Chris Lattneraa534bf2001-09-07 16:35:17 +00001343 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
Chris Lattner3cb41672004-02-09 03:19:29 +00001344 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Reid Spencer812724e2006-12-03 05:45:44 +00001345 $$ = new PATypeHolder(OT);
Chris Lattneraa534bf2001-09-07 16:35:17 +00001346 UR_OUT("New Upreference!\n");
Reid Spencer713eedc2006-08-18 08:43:06 +00001347 CHECK_FOR_ERROR
Chris Lattneraa534bf2001-09-07 16:35:17 +00001348 }
Reid Spencer78517652007-04-28 16:06:50 +00001349 | Types '(' ArgTypeListI ')' OptFuncAttrs {
Chris Lattner89da8a32003-04-22 18:42:41 +00001350 std::vector<const Type*> Params;
Reid Spencer78517652007-04-28 16:06:50 +00001351 ParamAttrsVector Attrs;
1352 if ($5 != ParamAttr::None) {
1353 ParamAttrsWithIndex X; X.index = 0; X.attrs = $5;
1354 Attrs.push_back(X);
1355 }
Reid Spencer71b79e32007-04-09 06:17:21 +00001356 unsigned index = 1;
1357 TypeWithAttrsList::iterator I = $3->begin(), E = $3->end();
1358 for (; I != E; ++I, ++index) {
Reid Spencerd05bc522007-03-20 01:13:00 +00001359 const Type *Ty = I->Ty->get();
Reid Spencerd05bc522007-03-20 01:13:00 +00001360 Params.push_back(Ty);
Reid Spencer78517652007-04-28 16:06:50 +00001361 if (Ty != Type::VoidTy)
1362 if (I->Attrs != ParamAttr::None) {
1363 ParamAttrsWithIndex X; X.index = index; X.attrs = I->Attrs;
1364 Attrs.push_back(X);
1365 }
Reid Spencerfa35bb32006-12-31 05:40:12 +00001366 }
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001367 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1368 if (isVarArg) Params.pop_back();
1369
Reid Spencer78517652007-04-28 16:06:50 +00001370 ParamAttrsList *ActualAttrs = 0;
1371 if (!Attrs.empty())
1372 ActualAttrs = ParamAttrsList::get(Attrs);
1373 FunctionType *FT = FunctionType::get(*$1, Params, isVarArg, ActualAttrs);
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001374 delete $3; // Delete the argument list
Reid Spencerfa35bb32006-12-31 05:40:12 +00001375 delete $1; // Delete the return type handle
1376 $$ = new PATypeHolder(HandleUpRefs(FT));
Reid Spencer713eedc2006-08-18 08:43:06 +00001377 CHECK_FOR_ERROR
Chris Lattneraa534bf2001-09-07 16:35:17 +00001378 }
Reid Spencer78517652007-04-28 16:06:50 +00001379 | VOID '(' ArgTypeListI ')' OptFuncAttrs {
Reid Spencerfa35bb32006-12-31 05:40:12 +00001380 std::vector<const Type*> Params;
Reid Spencer78517652007-04-28 16:06:50 +00001381 ParamAttrsVector Attrs;
1382 if ($5 != ParamAttr::None) {
1383 ParamAttrsWithIndex X; X.index = 0; X.attrs = $5;
1384 Attrs.push_back(X);
1385 }
Reid Spencer71b79e32007-04-09 06:17:21 +00001386 TypeWithAttrsList::iterator I = $3->begin(), E = $3->end();
1387 unsigned index = 1;
1388 for ( ; I != E; ++I, ++index) {
Reid Spencerd05bc522007-03-20 01:13:00 +00001389 const Type* Ty = I->Ty->get();
Reid Spencerd05bc522007-03-20 01:13:00 +00001390 Params.push_back(Ty);
Reid Spencer78517652007-04-28 16:06:50 +00001391 if (Ty != Type::VoidTy)
1392 if (I->Attrs != ParamAttr::None) {
1393 ParamAttrsWithIndex X; X.index = index; X.attrs = I->Attrs;
1394 Attrs.push_back(X);
1395 }
Reid Spencerfa35bb32006-12-31 05:40:12 +00001396 }
1397 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1398 if (isVarArg) Params.pop_back();
1399
Reid Spencer78517652007-04-28 16:06:50 +00001400 ParamAttrsList *ActualAttrs = 0;
1401 if (!Attrs.empty())
1402 ActualAttrs = ParamAttrsList::get(Attrs);
1403
1404 FunctionType *FT = FunctionType::get($1, Params, isVarArg, ActualAttrs);
Reid Spencer136a91c2007-01-05 17:06:19 +00001405 delete $3; // Delete the argument list
Reid Spencerfa35bb32006-12-31 05:40:12 +00001406 $$ = new PATypeHolder(HandleUpRefs(FT));
1407 CHECK_FOR_ERROR
1408 }
1409
1410 | '[' EUINT64VAL 'x' Types ']' { // Sized array type?
Reid Spencer812724e2006-12-03 05:45:44 +00001411 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
1412 delete $4;
Reid Spencer713eedc2006-08-18 08:43:06 +00001413 CHECK_FOR_ERROR
Chris Lattneraa534bf2001-09-07 16:35:17 +00001414 }
Reid Spencer09575ba2007-02-15 03:39:18 +00001415 | '<' EUINT64VAL 'x' Types '>' { // Vector type?
Reid Spencer812724e2006-12-03 05:45:44 +00001416 const llvm::Type* ElemTy = $4->get();
1417 if ((unsigned)$2 != $2)
1418 GEN_ERROR("Unsigned result not equal to signed result");
Chris Lattner03c49532007-01-15 02:27:26 +00001419 if (!ElemTy->isFloatingPoint() && !ElemTy->isInteger())
Reid Spencerd84d35b2007-02-15 02:26:10 +00001420 GEN_ERROR("Element type of a VectorType must be primitive");
Reid Spencer812724e2006-12-03 05:45:44 +00001421 if (!isPowerOf2_32($2))
Reid Spencerde1d05f2007-02-05 10:16:10 +00001422 GEN_ERROR("Vector length should be a power of 2");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001423 $$ = new PATypeHolder(HandleUpRefs(VectorType::get(*$4, (unsigned)$2)));
Reid Spencer812724e2006-12-03 05:45:44 +00001424 delete $4;
1425 CHECK_FOR_ERROR
Brian Gaeke02209042004-08-20 06:00:58 +00001426 }
Chris Lattnere63ddd8df2001-10-03 03:19:33 +00001427 | '{' TypeListI '}' { // Structure type?
Chris Lattner89da8a32003-04-22 18:42:41 +00001428 std::vector<const Type*> Elements;
Reid Spencer812724e2006-12-03 05:45:44 +00001429 for (std::list<llvm::PATypeHolder>::iterator I = $2->begin(),
Chris Lattner06b6c4b2005-02-22 23:13:58 +00001430 E = $2->end(); I != E; ++I)
Reid Spencer812724e2006-12-03 05:45:44 +00001431 Elements.push_back(*I);
Misha Brukman8b477072005-05-10 22:02:28 +00001432
Reid Spencer812724e2006-12-03 05:45:44 +00001433 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnere63ddd8df2001-10-03 03:19:33 +00001434 delete $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00001435 CHECK_FOR_ERROR
Chris Lattnere63ddd8df2001-10-03 03:19:33 +00001436 }
1437 | '{' '}' { // Empty structure type?
Reid Spencer812724e2006-12-03 05:45:44 +00001438 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Reid Spencer713eedc2006-08-18 08:43:06 +00001439 CHECK_FOR_ERROR
Chris Lattnere63ddd8df2001-10-03 03:19:33 +00001440 }
Andrew Lenharthdcb3c972006-12-08 18:06:16 +00001441 | '<' '{' TypeListI '}' '>' {
1442 std::vector<const Type*> Elements;
1443 for (std::list<llvm::PATypeHolder>::iterator I = $3->begin(),
1444 E = $3->end(); I != E; ++I)
1445 Elements.push_back(*I);
1446
1447 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements, true)));
1448 delete $3;
1449 CHECK_FOR_ERROR
1450 }
1451 | '<' '{' '}' '>' { // Empty structure type?
1452 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>(), true));
1453 CHECK_FOR_ERROR
1454 }
Reid Spencerfa35bb32006-12-31 05:40:12 +00001455 ;
1456
1457ArgType
Reid Spencer78517652007-04-28 16:06:50 +00001458 : Types OptParamAttrs {
Reid Spencerfa35bb32006-12-31 05:40:12 +00001459 $$.Ty = $1;
Reid Spencer78517652007-04-28 16:06:50 +00001460 $$.Attrs = $2;
Reid Spencerfa35bb32006-12-31 05:40:12 +00001461 }
1462 ;
1463
Reid Spencer136a91c2007-01-05 17:06:19 +00001464ResultTypes
1465 : Types {
Reid Spencerfa35bb32006-12-31 05:40:12 +00001466 if (!UpRefs.empty())
1467 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1468 if (!(*$1)->isFirstClassType())
Reid Spencerde1d05f2007-02-05 10:16:10 +00001469 GEN_ERROR("LLVM functions cannot return aggregate types");
Reid Spencer136a91c2007-01-05 17:06:19 +00001470 $$ = $1;
Reid Spencerfa35bb32006-12-31 05:40:12 +00001471 }
Reid Spencer136a91c2007-01-05 17:06:19 +00001472 | VOID {
1473 $$ = new PATypeHolder(Type::VoidTy);
Reid Spencerfa35bb32006-12-31 05:40:12 +00001474 }
1475 ;
1476
1477ArgTypeList : ArgType {
1478 $$ = new TypeWithAttrsList();
1479 $$->push_back($1);
1480 CHECK_FOR_ERROR
1481 }
1482 | ArgTypeList ',' ArgType {
1483 ($$=$1)->push_back($3);
1484 CHECK_FOR_ERROR
1485 }
1486 ;
1487
1488ArgTypeListI
1489 : ArgTypeList
1490 | ArgTypeList ',' DOTDOTDOT {
1491 $$=$1;
Reid Spencera472f662007-04-11 02:44:20 +00001492 TypeWithAttrs TWA; TWA.Attrs = ParamAttr::None;
Reid Spencerfa35bb32006-12-31 05:40:12 +00001493 TWA.Ty = new PATypeHolder(Type::VoidTy);
1494 $$->push_back(TWA);
1495 CHECK_FOR_ERROR
1496 }
1497 | DOTDOTDOT {
1498 $$ = new TypeWithAttrsList;
Reid Spencera472f662007-04-11 02:44:20 +00001499 TypeWithAttrs TWA; TWA.Attrs = ParamAttr::None;
Reid Spencerfa35bb32006-12-31 05:40:12 +00001500 TWA.Ty = new PATypeHolder(Type::VoidTy);
1501 $$->push_back(TWA);
1502 CHECK_FOR_ERROR
1503 }
1504 | /*empty*/ {
1505 $$ = new TypeWithAttrsList();
Reid Spencer713eedc2006-08-18 08:43:06 +00001506 CHECK_FOR_ERROR
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001507 };
Chris Lattneraa534bf2001-09-07 16:35:17 +00001508
Chris Lattner113f4f42002-06-25 16:13:24 +00001509// TypeList - Used for struct declarations and as a basis for function type
Chris Lattneraa534bf2001-09-07 16:35:17 +00001510// declaration type lists
1511//
Reid Spencerfa35bb32006-12-31 05:40:12 +00001512TypeListI : Types {
Reid Spencer812724e2006-12-03 05:45:44 +00001513 $$ = new std::list<PATypeHolder>();
Reid Spencerd05bc522007-03-20 01:13:00 +00001514 $$->push_back(*$1);
1515 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001516 CHECK_FOR_ERROR
Chris Lattneraa534bf2001-09-07 16:35:17 +00001517 }
Reid Spencerfa35bb32006-12-31 05:40:12 +00001518 | TypeListI ',' Types {
Reid Spencerd05bc522007-03-20 01:13:00 +00001519 ($$=$1)->push_back(*$3);
1520 delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00001521 CHECK_FOR_ERROR
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001522 };
Chris Lattneraa534bf2001-09-07 16:35:17 +00001523
Chris Lattner56f73d42001-07-14 06:10:16 +00001524// ConstVal - The various declarations that go into the constant pool. This
Chris Lattner7f325cd2002-08-16 21:14:40 +00001525// production is used ONLY to represent constants that show up AFTER a 'const',
1526// 'constant' or 'global' token at global scope. Constants that can be inlined
1527// into other expressions (such as integers and constexprs) are handled by the
1528// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattner56f73d42001-07-14 06:10:16 +00001529//
Chris Lattnere63ddd8df2001-10-03 03:19:33 +00001530ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencerfa35bb32006-12-31 05:40:12 +00001531 if (!UpRefs.empty())
1532 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer812724e2006-12-03 05:45:44 +00001533 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnere63ddd8df2001-10-03 03:19:33 +00001534 if (ATy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001535 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerde1d05f2007-02-05 10:16:10 +00001536 (*$1)->getDescription() + "'");
Chris Lattneraa534bf2001-09-07 16:35:17 +00001537 const Type *ETy = ATy->getElementType();
1538 int NumElements = ATy->getNumElements();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001539
Chris Lattneraa534bf2001-09-07 16:35:17 +00001540 // Verify that we have the correct size...
1541 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer713eedc2006-08-18 08:43:06 +00001542 GEN_ERROR("Type mismatch: constant sized array initialized with " +
Reid Spencerce6adaf2004-07-18 00:08:11 +00001543 utostr($3->size()) + " arguments, but has size of " +
Reid Spencerde1d05f2007-02-05 10:16:10 +00001544 itostr(NumElements) + "");
Chris Lattner2f7c9632001-06-06 20:29:01 +00001545
Chris Lattneraa534bf2001-09-07 16:35:17 +00001546 // Verify all elements are correct type!
1547 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencer812724e2006-12-03 05:45:44 +00001548 if (ETy != (*$3)[i]->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00001549 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Reid Spencerce6adaf2004-07-18 00:08:11 +00001550 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencer812724e2006-12-03 05:45:44 +00001551 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner2f7c9632001-06-06 20:29:01 +00001552 }
1553
Reid Spencer812724e2006-12-03 05:45:44 +00001554 $$ = ConstantArray::get(ATy, *$3);
1555 delete $1; delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00001556 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00001557 }
Chris Lattnere63ddd8df2001-10-03 03:19:33 +00001558 | Types '[' ']' {
Reid Spencerfa35bb32006-12-31 05:40:12 +00001559 if (!UpRefs.empty())
1560 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer812724e2006-12-03 05:45:44 +00001561 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnere63ddd8df2001-10-03 03:19:33 +00001562 if (ATy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001563 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerde1d05f2007-02-05 10:16:10 +00001564 (*$1)->getDescription() + "'");
Chris Lattnere63ddd8df2001-10-03 03:19:33 +00001565
1566 int NumElements = ATy->getNumElements();
Chris Lattneraa534bf2001-09-07 16:35:17 +00001567 if (NumElements != -1 && NumElements != 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001568 GEN_ERROR("Type mismatch: constant sized array initialized with 0"
Reid Spencerde1d05f2007-02-05 10:16:10 +00001569 " arguments, but has size of " + itostr(NumElements) +"");
Reid Spencer812724e2006-12-03 05:45:44 +00001570 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
1571 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001572 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00001573 }
Chris Lattnere63ddd8df2001-10-03 03:19:33 +00001574 | Types 'c' STRINGCONSTANT {
Reid Spencerfa35bb32006-12-31 05:40:12 +00001575 if (!UpRefs.empty())
1576 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer812724e2006-12-03 05:45:44 +00001577 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnere63ddd8df2001-10-03 03:19:33 +00001578 if (ATy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001579 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerde1d05f2007-02-05 10:16:10 +00001580 (*$1)->getDescription() + "'");
Chris Lattnere63ddd8df2001-10-03 03:19:33 +00001581
Chris Lattneraa534bf2001-09-07 16:35:17 +00001582 int NumElements = ATy->getNumElements();
1583 const Type *ETy = ATy->getElementType();
Reid Spencer3b208cc2007-05-22 18:52:21 +00001584 if (NumElements != -1 && NumElements != int($3->length()))
Reid Spencer713eedc2006-08-18 08:43:06 +00001585 GEN_ERROR("Can't build string constant of size " +
Reid Spencer3b208cc2007-05-22 18:52:21 +00001586 itostr((int)($3->length())) +
Reid Spencerde1d05f2007-02-05 10:16:10 +00001587 " when array has size " + itostr(NumElements) + "");
Chris Lattner89da8a32003-04-22 18:42:41 +00001588 std::vector<Constant*> Vals;
Reid Spencerfa35bb32006-12-31 05:40:12 +00001589 if (ETy == Type::Int8Ty) {
Reid Spencer3b208cc2007-05-22 18:52:21 +00001590 for (unsigned i = 0; i < $3->length(); ++i)
1591 Vals.push_back(ConstantInt::get(ETy, (*$3)[i]));
Chris Lattner26e50dc2001-07-28 17:48:55 +00001592 } else {
Reid Spencer3b208cc2007-05-22 18:52:21 +00001593 delete $3;
Reid Spencerde1d05f2007-02-05 10:16:10 +00001594 GEN_ERROR("Cannot build string arrays of non byte sized elements");
Chris Lattner26e50dc2001-07-28 17:48:55 +00001595 }
Reid Spencer3b208cc2007-05-22 18:52:21 +00001596 delete $3;
Reid Spencer812724e2006-12-03 05:45:44 +00001597 $$ = ConstantArray::get(ATy, Vals);
1598 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001599 CHECK_FOR_ERROR
Chris Lattner26e50dc2001-07-28 17:48:55 +00001600 }
Brian Gaeke02209042004-08-20 06:00:58 +00001601 | Types '<' ConstVector '>' { // Nonempty unsized arr
Reid Spencerfa35bb32006-12-31 05:40:12 +00001602 if (!UpRefs.empty())
1603 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencerd84d35b2007-02-15 02:26:10 +00001604 const VectorType *PTy = dyn_cast<VectorType>($1->get());
Brian Gaeke02209042004-08-20 06:00:58 +00001605 if (PTy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001606 GEN_ERROR("Cannot make packed constant with type: '" +
Reid Spencerde1d05f2007-02-05 10:16:10 +00001607 (*$1)->getDescription() + "'");
Brian Gaeke02209042004-08-20 06:00:58 +00001608 const Type *ETy = PTy->getElementType();
1609 int NumElements = PTy->getNumElements();
1610
1611 // Verify that we have the correct size...
1612 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer713eedc2006-08-18 08:43:06 +00001613 GEN_ERROR("Type mismatch: constant sized packed initialized with " +
Brian Gaeke02209042004-08-20 06:00:58 +00001614 utostr($3->size()) + " arguments, but has size of " +
Reid Spencerde1d05f2007-02-05 10:16:10 +00001615 itostr(NumElements) + "");
Brian Gaeke02209042004-08-20 06:00:58 +00001616
1617 // Verify all elements are correct type!
1618 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencer812724e2006-12-03 05:45:44 +00001619 if (ETy != (*$3)[i]->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00001620 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Brian Gaeke02209042004-08-20 06:00:58 +00001621 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencer812724e2006-12-03 05:45:44 +00001622 (*$3)[i]->getType()->getDescription() + "'.");
Brian Gaeke02209042004-08-20 06:00:58 +00001623 }
1624
Reid Spencerd84d35b2007-02-15 02:26:10 +00001625 $$ = ConstantVector::get(PTy, *$3);
Reid Spencer812724e2006-12-03 05:45:44 +00001626 delete $1; delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00001627 CHECK_FOR_ERROR
Brian Gaeke02209042004-08-20 06:00:58 +00001628 }
Chris Lattnere63ddd8df2001-10-03 03:19:33 +00001629 | Types '{' ConstVector '}' {
Reid Spencer812724e2006-12-03 05:45:44 +00001630 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnere63ddd8df2001-10-03 03:19:33 +00001631 if (STy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001632 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerde1d05f2007-02-05 10:16:10 +00001633 (*$1)->getDescription() + "'");
Chris Lattner4b1d1062003-04-15 16:09:31 +00001634
Chris Lattner5bb23152003-05-21 16:06:56 +00001635 if ($3->size() != STy->getNumContainedTypes())
Reid Spencerde1d05f2007-02-05 10:16:10 +00001636 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattner5bb23152003-05-21 16:06:56 +00001637
Chris Lattner4b1d1062003-04-15 16:09:31 +00001638 // Check to ensure that constants are compatible with the type initializer!
1639 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Reid Spencer812724e2006-12-03 05:45:44 +00001640 if ((*$3)[i]->getType() != STy->getElementType(i))
Reid Spencer713eedc2006-08-18 08:43:06 +00001641 GEN_ERROR("Expected type '" +
Chris Lattnerac6db752004-02-09 04:37:31 +00001642 STy->getElementType(i)->getDescription() +
Chris Lattner4b1d1062003-04-15 16:09:31 +00001643 "' for element #" + utostr(i) +
Reid Spencerde1d05f2007-02-05 10:16:10 +00001644 " of structure initializer");
Chris Lattner4b1d1062003-04-15 16:09:31 +00001645
Andrew Lenharthe3c5a4c2007-01-08 18:16:47 +00001646 // Check to ensure that Type is not packed
1647 if (STy->isPacked())
Chris Lattnereee35bf2007-04-26 05:30:35 +00001648 GEN_ERROR("Unpacked Initializer to vector type '" +
1649 STy->getDescription() + "'");
Andrew Lenharthe3c5a4c2007-01-08 18:16:47 +00001650
Reid Spencer812724e2006-12-03 05:45:44 +00001651 $$ = ConstantStruct::get(STy, *$3);
1652 delete $1; delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00001653 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00001654 }
Chris Lattner5bb23152003-05-21 16:06:56 +00001655 | Types '{' '}' {
Reid Spencerfa35bb32006-12-31 05:40:12 +00001656 if (!UpRefs.empty())
1657 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer812724e2006-12-03 05:45:44 +00001658 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattner5bb23152003-05-21 16:06:56 +00001659 if (STy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001660 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerde1d05f2007-02-05 10:16:10 +00001661 (*$1)->getDescription() + "'");
Chris Lattner5bb23152003-05-21 16:06:56 +00001662
1663 if (STy->getNumContainedTypes() != 0)
Reid Spencerde1d05f2007-02-05 10:16:10 +00001664 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattner5bb23152003-05-21 16:06:56 +00001665
Andrew Lenharthe3c5a4c2007-01-08 18:16:47 +00001666 // Check to ensure that Type is not packed
1667 if (STy->isPacked())
Chris Lattnereee35bf2007-04-26 05:30:35 +00001668 GEN_ERROR("Unpacked Initializer to vector type '" +
1669 STy->getDescription() + "'");
Andrew Lenharthe3c5a4c2007-01-08 18:16:47 +00001670
1671 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1672 delete $1;
1673 CHECK_FOR_ERROR
1674 }
1675 | Types '<' '{' ConstVector '}' '>' {
1676 const StructType *STy = dyn_cast<StructType>($1->get());
1677 if (STy == 0)
1678 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerde1d05f2007-02-05 10:16:10 +00001679 (*$1)->getDescription() + "'");
Andrew Lenharthe3c5a4c2007-01-08 18:16:47 +00001680
1681 if ($4->size() != STy->getNumContainedTypes())
Reid Spencerde1d05f2007-02-05 10:16:10 +00001682 GEN_ERROR("Illegal number of initializers for structure type");
Andrew Lenharthe3c5a4c2007-01-08 18:16:47 +00001683
1684 // Check to ensure that constants are compatible with the type initializer!
1685 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1686 if ((*$4)[i]->getType() != STy->getElementType(i))
1687 GEN_ERROR("Expected type '" +
1688 STy->getElementType(i)->getDescription() +
1689 "' for element #" + utostr(i) +
Reid Spencerde1d05f2007-02-05 10:16:10 +00001690 " of structure initializer");
Andrew Lenharthe3c5a4c2007-01-08 18:16:47 +00001691
1692 // Check to ensure that Type is packed
1693 if (!STy->isPacked())
Reid Spencer09575ba2007-02-15 03:39:18 +00001694 GEN_ERROR("Vector initializer to non-vector type '" +
1695 STy->getDescription() + "'");
Andrew Lenharthe3c5a4c2007-01-08 18:16:47 +00001696
1697 $$ = ConstantStruct::get(STy, *$4);
1698 delete $1; delete $4;
1699 CHECK_FOR_ERROR
1700 }
1701 | Types '<' '{' '}' '>' {
1702 if (!UpRefs.empty())
1703 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1704 const StructType *STy = dyn_cast<StructType>($1->get());
1705 if (STy == 0)
1706 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerde1d05f2007-02-05 10:16:10 +00001707 (*$1)->getDescription() + "'");
Andrew Lenharthe3c5a4c2007-01-08 18:16:47 +00001708
1709 if (STy->getNumContainedTypes() != 0)
Reid Spencerde1d05f2007-02-05 10:16:10 +00001710 GEN_ERROR("Illegal number of initializers for structure type");
Andrew Lenharthe3c5a4c2007-01-08 18:16:47 +00001711
1712 // Check to ensure that Type is packed
1713 if (!STy->isPacked())
Reid Spencer09575ba2007-02-15 03:39:18 +00001714 GEN_ERROR("Vector initializer to non-vector type '" +
1715 STy->getDescription() + "'");
Andrew Lenharthe3c5a4c2007-01-08 18:16:47 +00001716
Reid Spencer812724e2006-12-03 05:45:44 +00001717 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1718 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001719 CHECK_FOR_ERROR
Chris Lattner5bb23152003-05-21 16:06:56 +00001720 }
Chris Lattnere63ddd8df2001-10-03 03:19:33 +00001721 | Types NULL_TOK {
Reid Spencerfa35bb32006-12-31 05:40:12 +00001722 if (!UpRefs.empty())
1723 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer812724e2006-12-03 05:45:44 +00001724 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnere63ddd8df2001-10-03 03:19:33 +00001725 if (PTy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001726 GEN_ERROR("Cannot make null pointer constant with type: '" +
Reid Spencerde1d05f2007-02-05 10:16:10 +00001727 (*$1)->getDescription() + "'");
Chris Lattnere63ddd8df2001-10-03 03:19:33 +00001728
Reid Spencer812724e2006-12-03 05:45:44 +00001729 $$ = ConstantPointerNull::get(PTy);
1730 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001731 CHECK_FOR_ERROR
Chris Lattner571a6b02001-10-03 01:49:25 +00001732 }
Chris Lattner4ff31492004-10-16 18:17:13 +00001733 | Types UNDEF {
Reid Spencerfa35bb32006-12-31 05:40:12 +00001734 if (!UpRefs.empty())
1735 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer812724e2006-12-03 05:45:44 +00001736 $$ = UndefValue::get($1->get());
1737 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001738 CHECK_FOR_ERROR
Chris Lattner4ff31492004-10-16 18:17:13 +00001739 }
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001740 | Types SymbolicValueRef {
Reid Spencerfa35bb32006-12-31 05:40:12 +00001741 if (!UpRefs.empty())
1742 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer812724e2006-12-03 05:45:44 +00001743 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattner60e0dd72001-10-03 06:12:09 +00001744 if (Ty == 0)
Reid Spencerde1d05f2007-02-05 10:16:10 +00001745 GEN_ERROR("Global const reference must be a pointer type");
Chris Lattner60e0dd72001-10-03 06:12:09 +00001746
Chris Lattner61643a02002-08-15 17:58:33 +00001747 // ConstExprs can exist in the body of a function, thus creating
Reid Spencerce6adaf2004-07-18 00:08:11 +00001748 // GlobalValues whenever they refer to a variable. Because we are in
Reid Spencerfe65ae82007-03-19 18:39:36 +00001749 // the context of a function, getExistingVal will search the functions
Chris Lattner61643a02002-08-15 17:58:33 +00001750 // symbol table instead of the module symbol table for the global symbol,
1751 // which throws things all off. To get around this, we just tell
Reid Spencerfe65ae82007-03-19 18:39:36 +00001752 // getExistingVal that we are at global scope here.
Chris Lattner61643a02002-08-15 17:58:33 +00001753 //
Chris Lattner61877742003-10-16 20:12:13 +00001754 Function *SavedCurFn = CurFun.CurrentFunction;
1755 CurFun.CurrentFunction = 0;
Chris Lattner61643a02002-08-15 17:58:33 +00001756
Reid Spencerfe65ae82007-03-19 18:39:36 +00001757 Value *V = getExistingVal(Ty, $2);
Reid Spencer309080a2006-09-28 19:28:24 +00001758 CHECK_FOR_ERROR
Chris Lattner60e0dd72001-10-03 06:12:09 +00001759
Chris Lattner61877742003-10-16 20:12:13 +00001760 CurFun.CurrentFunction = SavedCurFn;
Chris Lattner61643a02002-08-15 17:58:33 +00001761
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001762 // If this is an initializer for a constant pointer, which is referencing a
1763 // (currently) undefined variable, create a stub now that shall be replaced
1764 // in the future with the right type of variable.
1765 //
1766 if (V == 0) {
Reid Spencer9894d6c2007-02-05 17:01:20 +00001767 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001768 const PointerType *PT = cast<PointerType>(Ty);
1769
1770 // First check to see if the forward references value is already created!
1771 PerModuleInfo::GlobalRefsType::iterator I =
Reid Spencerce6adaf2004-07-18 00:08:11 +00001772 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001773
1774 if (I != CurModule.GlobalRefs.end()) {
Reid Spencerce6adaf2004-07-18 00:08:11 +00001775 V = I->second; // Placeholder already exists, use it...
Chris Lattnere9703012003-12-23 20:05:15 +00001776 $2.destroy();
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001777 } else {
Chris Lattner4c9210e2004-07-14 21:44:00 +00001778 std::string Name;
Reid Spencer791b8ef2007-01-26 08:04:51 +00001779 if ($2.Type == ValID::GlobalName)
Reid Spencer3b208cc2007-05-22 18:52:21 +00001780 Name = $2.getName();
Reid Spencer791b8ef2007-01-26 08:04:51 +00001781 else if ($2.Type != ValID::GlobalID)
1782 GEN_ERROR("Invalid reference to global");
Chris Lattner4c9210e2004-07-14 21:44:00 +00001783
Reid Spencerce6adaf2004-07-18 00:08:11 +00001784 // Create the forward referenced global.
Chris Lattner8abe1a12004-07-14 23:03:46 +00001785 GlobalValue *GV;
1786 if (const FunctionType *FTy =
1787 dyn_cast<FunctionType>(PT->getElementType())) {
Chris Lattnereee35bf2007-04-26 05:30:35 +00001788 GV = new Function(FTy, GlobalValue::ExternalWeakLinkage, Name,
Chris Lattner8abe1a12004-07-14 23:03:46 +00001789 CurModule.CurrentModule);
1790 } else {
Reid Spencerce6adaf2004-07-18 00:08:11 +00001791 GV = new GlobalVariable(PT->getElementType(), false,
Chris Lattnereee35bf2007-04-26 05:30:35 +00001792 GlobalValue::ExternalWeakLinkage, 0,
Chris Lattner8abe1a12004-07-14 23:03:46 +00001793 Name, CurModule.CurrentModule);
1794 }
Chris Lattner4c9210e2004-07-14 21:44:00 +00001795
Reid Spencerce6adaf2004-07-18 00:08:11 +00001796 // Keep track of the fact that we have a forward ref to recycle it
1797 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1798 V = GV;
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001799 }
Chris Lattner60e0dd72001-10-03 06:12:09 +00001800 }
1801
Reid Spencer812724e2006-12-03 05:45:44 +00001802 $$ = cast<GlobalValue>(V);
1803 delete $1; // Free the type handle
Reid Spencer713eedc2006-08-18 08:43:06 +00001804 CHECK_FOR_ERROR
Vikram S. Adve7064eaf2002-07-14 22:59:28 +00001805 }
Chris Lattner7f325cd2002-08-16 21:14:40 +00001806 | Types ConstExpr {
Reid Spencerfa35bb32006-12-31 05:40:12 +00001807 if (!UpRefs.empty())
1808 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer812724e2006-12-03 05:45:44 +00001809 if ($1->get() != $2->getType())
Reid Spencer4eda71e2007-01-04 00:05:48 +00001810 GEN_ERROR("Mismatched types for constant expression: " +
1811 (*$1)->getDescription() + " and " + $2->getType()->getDescription());
Chris Lattner7f325cd2002-08-16 21:14:40 +00001812 $$ = $2;
Reid Spencer812724e2006-12-03 05:45:44 +00001813 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001814 CHECK_FOR_ERROR
Chris Lattner79694012003-06-28 20:01:34 +00001815 }
1816 | Types ZEROINITIALIZER {
Reid Spencerfa35bb32006-12-31 05:40:12 +00001817 if (!UpRefs.empty())
1818 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer812724e2006-12-03 05:45:44 +00001819 const Type *Ty = $1->get();
Chris Lattneraef74b52004-11-28 16:45:45 +00001820 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
Reid Spencerde1d05f2007-02-05 10:16:10 +00001821 GEN_ERROR("Cannot create a null initialized value of this type");
Reid Spencer812724e2006-12-03 05:45:44 +00001822 $$ = Constant::getNullValue(Ty);
1823 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001824 CHECK_FOR_ERROR
Reid Spencer812724e2006-12-03 05:45:44 +00001825 }
Reid Spencerfa35bb32006-12-31 05:40:12 +00001826 | IntType ESINT64VAL { // integral constants
Reid Spencer3f46fba2006-12-20 17:20:09 +00001827 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencerde1d05f2007-02-05 10:16:10 +00001828 GEN_ERROR("Constant value doesn't fit in type");
Reid Spencer363fd462007-03-19 20:40:22 +00001829 $$ = ConstantInt::get($1, $2, true);
Reid Spencer9875fc12007-02-28 02:23:44 +00001830 CHECK_FOR_ERROR
1831 }
1832 | IntType ESAPINTVAL { // arbitrary precision integer constants
1833 uint32_t BitWidth = cast<IntegerType>($1)->getBitWidth();
1834 if ($2->getBitWidth() > BitWidth) {
1835 GEN_ERROR("Constant value does not fit in type");
Reid Spencer666ea0d2007-03-01 19:32:01 +00001836 }
1837 $2->sextOrTrunc(BitWidth);
1838 $$ = ConstantInt::get(*$2);
Reid Spencer9875fc12007-02-28 02:23:44 +00001839 delete $2;
Reid Spencer3f46fba2006-12-20 17:20:09 +00001840 CHECK_FOR_ERROR
1841 }
Reid Spencerfa35bb32006-12-31 05:40:12 +00001842 | IntType EUINT64VAL { // integral constants
Reid Spencer3f46fba2006-12-20 17:20:09 +00001843 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencerde1d05f2007-02-05 10:16:10 +00001844 GEN_ERROR("Constant value doesn't fit in type");
Reid Spencer363fd462007-03-19 20:40:22 +00001845 $$ = ConstantInt::get($1, $2, false);
Reid Spencer9875fc12007-02-28 02:23:44 +00001846 CHECK_FOR_ERROR
1847 }
1848 | IntType EUAPINTVAL { // arbitrary precision integer constants
1849 uint32_t BitWidth = cast<IntegerType>($1)->getBitWidth();
1850 if ($2->getBitWidth() > BitWidth) {
1851 GEN_ERROR("Constant value does not fit in type");
Reid Spencer666ea0d2007-03-01 19:32:01 +00001852 }
1853 $2->zextOrTrunc(BitWidth);
1854 $$ = ConstantInt::get(*$2);
Reid Spencer9875fc12007-02-28 02:23:44 +00001855 delete $2;
Reid Spencer3f46fba2006-12-20 17:20:09 +00001856 CHECK_FOR_ERROR
1857 }
Reid Spencer502d64e2007-01-13 05:00:20 +00001858 | INTTYPE TRUETOK { // Boolean constants
1859 assert(cast<IntegerType>($1)->getBitWidth() == 1 && "Not Bool?");
Zhou Sheng75b871f2007-01-11 12:24:14 +00001860 $$ = ConstantInt::getTrue();
Reid Spencer713eedc2006-08-18 08:43:06 +00001861 CHECK_FOR_ERROR
Chris Lattner7f1c98d2002-08-15 18:17:28 +00001862 }
Reid Spencer502d64e2007-01-13 05:00:20 +00001863 | INTTYPE FALSETOK { // Boolean constants
1864 assert(cast<IntegerType>($1)->getBitWidth() == 1 && "Not Bool?");
Zhou Sheng75b871f2007-01-11 12:24:14 +00001865 $$ = ConstantInt::getFalse();
Reid Spencer713eedc2006-08-18 08:43:06 +00001866 CHECK_FOR_ERROR
Chris Lattner7f1c98d2002-08-15 18:17:28 +00001867 }
1868 | FPType FPVAL { // Float & Double constants
Dale Johannesenbed9dc42007-09-06 18:13:44 +00001869 if (!ConstantFP::isValueValidForType($1, *$2))
Reid Spencerde1d05f2007-02-05 10:16:10 +00001870 GEN_ERROR("Floating point constant invalid for type");
Dale Johannesenbed9dc42007-09-06 18:13:44 +00001871 // Lexer has no type info, so builds all FP constants as double.
1872 // Fix this here.
1873 if ($1==Type::FloatTy)
1874 $2->convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven);
1875 $$ = ConstantFP::get($1, *$2);
Dale Johannesen9e700862007-09-07 21:07:57 +00001876 delete $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00001877 CHECK_FOR_ERROR
Chris Lattner7f1c98d2002-08-15 18:17:28 +00001878 };
1879
Chris Lattner2f7c9632001-06-06 20:29:01 +00001880
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001881ConstExpr: CastOps '(' ConstVal TO Types ')' {
Reid Spencerfa35bb32006-12-31 05:40:12 +00001882 if (!UpRefs.empty())
1883 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencer812724e2006-12-03 05:45:44 +00001884 Constant *Val = $3;
Reid Spencer082a77f2007-01-17 02:47:33 +00001885 const Type *DestTy = $5->get();
1886 if (!CastInst::castIsValid($1, $3, DestTy))
1887 GEN_ERROR("invalid cast opcode for cast from '" +
1888 Val->getType()->getDescription() + "' to '" +
Reid Spencerde1d05f2007-02-05 10:16:10 +00001889 DestTy->getDescription() + "'");
Reid Spencer082a77f2007-01-17 02:47:33 +00001890 $$ = ConstantExpr::getCast($1, $3, DestTy);
Reid Spencer812724e2006-12-03 05:45:44 +00001891 delete $5;
Vikram S. Adve7064eaf2002-07-14 22:59:28 +00001892 }
Chris Lattner7f325cd2002-08-16 21:14:40 +00001893 | GETELEMENTPTR '(' ConstVal IndexList ')' {
Reid Spencer812724e2006-12-03 05:45:44 +00001894 if (!isa<PointerType>($3->getType()))
Reid Spencerde1d05f2007-02-05 10:16:10 +00001895 GEN_ERROR("GetElementPtr requires a pointer operand");
Chris Lattner3cd8c562002-07-30 18:54:25 +00001896
Reid Spencer812724e2006-12-03 05:45:44 +00001897 const Type *IdxTy =
David Greenec656cbb2007-09-04 15:46:09 +00001898 GetElementPtrInst::getIndexedType($3->getType(), $4->begin(), $4->end(),
Chris Lattner04a2d762007-02-13 00:57:40 +00001899 true);
Reid Spencer812724e2006-12-03 05:45:44 +00001900 if (!IdxTy)
Reid Spencerde1d05f2007-02-05 10:16:10 +00001901 GEN_ERROR("Index list invalid for constant getelementptr");
Reid Spencer812724e2006-12-03 05:45:44 +00001902
Chris Lattner10aad382007-01-31 04:43:46 +00001903 SmallVector<Constant*, 8> IdxVec;
Reid Spencer812724e2006-12-03 05:45:44 +00001904 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1905 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattner980ddf52002-07-18 00:14:27 +00001906 IdxVec.push_back(C);
1907 else
Reid Spencerde1d05f2007-02-05 10:16:10 +00001908 GEN_ERROR("Indices to constant getelementptr must be constants");
Chris Lattner980ddf52002-07-18 00:14:27 +00001909
Chris Lattner7f325cd2002-08-16 21:14:40 +00001910 delete $4;
Chris Lattner980ddf52002-07-18 00:14:27 +00001911
Chris Lattner10aad382007-01-31 04:43:46 +00001912 $$ = ConstantExpr::getGetElementPtr($3, &IdxVec[0], IdxVec.size());
Reid Spencer713eedc2006-08-18 08:43:06 +00001913 CHECK_FOR_ERROR
Vikram S. Adve7064eaf2002-07-14 22:59:28 +00001914 }
Chris Lattner6536f0c2004-03-12 05:51:36 +00001915 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer542964f2007-01-11 18:21:29 +00001916 if ($3->getType() != Type::Int1Ty)
Reid Spencerde1d05f2007-02-05 10:16:10 +00001917 GEN_ERROR("Select condition must be of boolean type");
Reid Spencer812724e2006-12-03 05:45:44 +00001918 if ($5->getType() != $7->getType())
Reid Spencerde1d05f2007-02-05 10:16:10 +00001919 GEN_ERROR("Select operand types must match");
Reid Spencer812724e2006-12-03 05:45:44 +00001920 $$ = ConstantExpr::getSelect($3, $5, $7);
Reid Spencer713eedc2006-08-18 08:43:06 +00001921 CHECK_FOR_ERROR
Chris Lattner6536f0c2004-03-12 05:51:36 +00001922 }
Chris Lattnerfca28332004-08-17 17:26:41 +00001923 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer812724e2006-12-03 05:45:44 +00001924 if ($3->getType() != $5->getType())
Reid Spencerde1d05f2007-02-05 10:16:10 +00001925 GEN_ERROR("Binary operator types must match");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001926 CHECK_FOR_ERROR;
Reid Spencer5bcee9a2006-12-05 19:15:41 +00001927 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattnerfca28332004-08-17 17:26:41 +00001928 }
1929 | LogicalOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer812724e2006-12-03 05:45:44 +00001930 if ($3->getType() != $5->getType())
Reid Spencerde1d05f2007-02-05 10:16:10 +00001931 GEN_ERROR("Logical operator types must match");
Chris Lattner03c49532007-01-15 02:27:26 +00001932 if (!$3->getType()->isInteger()) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001933 if (Instruction::isShift($1) || !isa<VectorType>($3->getType()) ||
1934 !cast<VectorType>($3->getType())->getElementType()->isInteger())
Reid Spencerde1d05f2007-02-05 10:16:10 +00001935 GEN_ERROR("Logical operator requires integral operands");
Chris Lattnera5d70e92005-12-21 18:31:29 +00001936 }
Reid Spencer812724e2006-12-03 05:45:44 +00001937 $$ = ConstantExpr::get($1, $3, $5);
Reid Spencer713eedc2006-08-18 08:43:06 +00001938 CHECK_FOR_ERROR
Chris Lattnerfca28332004-08-17 17:26:41 +00001939 }
Reid Spencerf3490692006-12-04 05:20:06 +00001940 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
1941 if ($4->getType() != $6->getType())
Reid Spencerde1d05f2007-02-05 10:16:10 +00001942 GEN_ERROR("icmp operand types must match");
Reid Spencerf3490692006-12-04 05:20:06 +00001943 $$ = ConstantExpr::getICmp($2, $4, $6);
Reid Spencer812724e2006-12-03 05:45:44 +00001944 }
Reid Spencerf3490692006-12-04 05:20:06 +00001945 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
1946 if ($4->getType() != $6->getType())
Reid Spencerde1d05f2007-02-05 10:16:10 +00001947 GEN_ERROR("fcmp operand types must match");
Reid Spencerf3490692006-12-04 05:20:06 +00001948 $$ = ConstantExpr::getFCmp($2, $4, $6);
Reid Spencer812724e2006-12-03 05:45:44 +00001949 }
Robert Bocchinoaa1cf542006-01-10 19:04:32 +00001950 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencer812724e2006-12-03 05:45:44 +00001951 if (!ExtractElementInst::isValidOperands($3, $5))
Reid Spencerde1d05f2007-02-05 10:16:10 +00001952 GEN_ERROR("Invalid extractelement operands");
Reid Spencer812724e2006-12-03 05:45:44 +00001953 $$ = ConstantExpr::getExtractElement($3, $5);
Reid Spencer713eedc2006-08-18 08:43:06 +00001954 CHECK_FOR_ERROR
Chris Lattner8c32ad02006-04-08 03:53:34 +00001955 }
1956 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer812724e2006-12-03 05:45:44 +00001957 if (!InsertElementInst::isValidOperands($3, $5, $7))
Reid Spencerde1d05f2007-02-05 10:16:10 +00001958 GEN_ERROR("Invalid insertelement operands");
Reid Spencer812724e2006-12-03 05:45:44 +00001959 $$ = ConstantExpr::getInsertElement($3, $5, $7);
Reid Spencer713eedc2006-08-18 08:43:06 +00001960 CHECK_FOR_ERROR
Chris Lattner8c32ad02006-04-08 03:53:34 +00001961 }
1962 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer812724e2006-12-03 05:45:44 +00001963 if (!ShuffleVectorInst::isValidOperands($3, $5, $7))
Reid Spencerde1d05f2007-02-05 10:16:10 +00001964 GEN_ERROR("Invalid shufflevector operands");
Reid Spencer812724e2006-12-03 05:45:44 +00001965 $$ = ConstantExpr::getShuffleVector($3, $5, $7);
Reid Spencer713eedc2006-08-18 08:43:06 +00001966 CHECK_FOR_ERROR
Chris Lattnerdb3b2022002-08-14 17:12:33 +00001967 };
Vikram S. Adve7064eaf2002-07-14 22:59:28 +00001968
Chris Lattner8c32ad02006-04-08 03:53:34 +00001969
Misha Brukman7fdaab42003-07-14 17:20:40 +00001970// ConstVector - A list of comma separated constants.
Chris Lattner2f7c9632001-06-06 20:29:01 +00001971ConstVector : ConstVector ',' ConstVal {
Chris Lattneraa534bf2001-09-07 16:35:17 +00001972 ($$ = $1)->push_back($3);
Reid Spencer713eedc2006-08-18 08:43:06 +00001973 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00001974 }
1975 | ConstVal {
Reid Spencer812724e2006-12-03 05:45:44 +00001976 $$ = new std::vector<Constant*>();
Chris Lattneraa534bf2001-09-07 16:35:17 +00001977 $$->push_back($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00001978 CHECK_FOR_ERROR
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001979 };
Chris Lattner2f7c9632001-06-06 20:29:01 +00001980
Chris Lattner252afba2001-07-26 16:29:15 +00001981
Chris Lattner7fb14f52001-09-18 04:00:54 +00001982// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001983GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner7fb14f52001-09-18 04:00:54 +00001984
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +00001985// ThreadLocal
1986ThreadLocal : THREAD_LOCAL { $$ = true; } | { $$ = false; };
1987
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +00001988// AliaseeRef - Match either GlobalValue or bitcast to GlobalValue.
1989AliaseeRef : ResultTypes SymbolicValueRef {
1990 const Type* VTy = $1->get();
1991 Value *V = getVal(VTy, $2);
Chris Lattner6a5a2622007-08-06 21:00:37 +00001992 CHECK_FOR_ERROR
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +00001993 GlobalValue* Aliasee = dyn_cast<GlobalValue>(V);
1994 if (!Aliasee)
1995 GEN_ERROR("Aliases can be created only to global values");
1996
1997 $$ = Aliasee;
1998 CHECK_FOR_ERROR
1999 delete $1;
2000 }
2001 | BITCAST '(' AliaseeRef TO Types ')' {
2002 Constant *Val = $3;
2003 const Type *DestTy = $5->get();
2004 if (!CastInst::castIsValid($1, $3, DestTy))
2005 GEN_ERROR("invalid cast opcode for cast from '" +
2006 Val->getType()->getDescription() + "' to '" +
2007 DestTy->getDescription() + "'");
2008
2009 $$ = ConstantExpr::getCast($1, $3, DestTy);
2010 CHECK_FOR_ERROR
2011 delete $5;
2012 };
Chris Lattner2f7c9632001-06-06 20:29:01 +00002013
Chris Lattner7b804d62002-05-02 19:11:13 +00002014//===----------------------------------------------------------------------===//
2015// Rules to match Modules
2016//===----------------------------------------------------------------------===//
2017
2018// Module rule: Capture the result of parsing the whole file into a result
2019// variable...
2020//
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00002021Module
2022 : DefinitionList {
2023 $$ = ParserResult = CurModule.CurrentModule;
2024 CurModule.ModuleDone();
2025 CHECK_FOR_ERROR;
2026 }
2027 | /*empty*/ {
2028 $$ = ParserResult = CurModule.CurrentModule;
2029 CurModule.ModuleDone();
2030 CHECK_FOR_ERROR;
2031 }
2032 ;
Chris Lattner7b804d62002-05-02 19:11:13 +00002033
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00002034DefinitionList
2035 : Definition
2036 | DefinitionList Definition
2037 ;
2038
2039Definition
Jeff Cohen5d956e42007-01-21 19:19:31 +00002040 : DEFINE { CurFun.isDeclare = false; } Function {
Chris Lattner61877742003-10-16 20:12:13 +00002041 CurFun.FunctionDone();
Reid Spencer713eedc2006-08-18 08:43:06 +00002042 CHECK_FOR_ERROR
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00002043 }
2044 | DECLARE { CurFun.isDeclare = true; } FunctionProto {
Reid Spencer713eedc2006-08-18 08:43:06 +00002045 CHECK_FOR_ERROR
Chris Lattner7b804d62002-05-02 19:11:13 +00002046 }
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00002047 | MODULE ASM_TOK AsmBlock {
Reid Spencer713eedc2006-08-18 08:43:06 +00002048 CHECK_FOR_ERROR
Chris Lattnerf09741f2006-01-23 23:05:15 +00002049 }
Reid Spencer791b8ef2007-01-26 08:04:51 +00002050 | OptLocalAssign TYPE Types {
Reid Spencerfa35bb32006-12-31 05:40:12 +00002051 if (!UpRefs.empty())
2052 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattnerd50fa5a2001-10-22 05:56:09 +00002053 // Eagerly resolve types. This is not an optimization, this is a
2054 // requirement that is due to the fact that we could have this:
2055 //
2056 // %list = type { %list * }
2057 // %list = type { %list * } ; repeated type decl
2058 //
2059 // If types are not resolved eagerly, then the two types will not be
2060 // determined to be the same type!
2061 //
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00002062 ResolveTypeTo($1, *$3);
Chris Lattnerd50fa5a2001-10-22 05:56:09 +00002063
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00002064 if (!setTypeName(*$3, $1) && !$1) {
Reid Spencer309080a2006-09-28 19:28:24 +00002065 CHECK_FOR_ERROR
Chris Lattner85a351f2004-07-13 08:10:10 +00002066 // If this is a named type that is not a redefinition, add it to the slot
2067 // table.
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00002068 CurModule.Types.push_back(*$3);
Chris Lattneraa534bf2001-09-07 16:35:17 +00002069 }
Reid Spencer812724e2006-12-03 05:45:44 +00002070
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00002071 delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00002072 CHECK_FOR_ERROR
Chris Lattneraa534bf2001-09-07 16:35:17 +00002073 }
Reid Spencer791b8ef2007-01-26 08:04:51 +00002074 | OptLocalAssign TYPE VOID {
Reid Spencerfa35bb32006-12-31 05:40:12 +00002075 ResolveTypeTo($1, $3);
2076
2077 if (!setTypeName($3, $1) && !$1) {
2078 CHECK_FOR_ERROR
2079 // If this is a named type that is not a redefinition, add it to the slot
2080 // table.
2081 CurModule.Types.push_back($3);
2082 }
2083 CHECK_FOR_ERROR
2084 }
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +00002085 | OptGlobalAssign GVVisibilityStyle ThreadLocal GlobalType ConstVal {
Reid Spencer791b8ef2007-01-26 08:04:51 +00002086 /* "Externally Visible" Linkage */
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002087 if ($5 == 0)
Reid Spencerde1d05f2007-02-05 10:16:10 +00002088 GEN_ERROR("Global value initializer is not a constant");
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +00002089 CurGV = ParseGlobalVariable($1, GlobalValue::ExternalLinkage,
2090 $2, $4, $5->getType(), $5, $3);
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00002091 CHECK_FOR_ERROR
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002092 } GlobalVarAttributes {
2093 CurGV = 0;
2094 }
Chris Lattnereee35bf2007-04-26 05:30:35 +00002095 | OptGlobalAssign GVInternalLinkage GVVisibilityStyle ThreadLocal GlobalType
2096 ConstVal {
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +00002097 if ($6 == 0)
2098 GEN_ERROR("Global value initializer is not a constant");
2099 CurGV = ParseGlobalVariable($1, $2, $3, $5, $6->getType(), $6, $4);
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002100 CHECK_FOR_ERROR
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +00002101 } GlobalVarAttributes {
2102 CurGV = 0;
2103 }
Chris Lattnereee35bf2007-04-26 05:30:35 +00002104 | OptGlobalAssign GVExternalLinkage GVVisibilityStyle ThreadLocal GlobalType
2105 Types {
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +00002106 if (!UpRefs.empty())
2107 GEN_ERROR("Invalid upreference in type: " + (*$6)->getDescription());
2108 CurGV = ParseGlobalVariable($1, $2, $3, $5, *$6, 0, $4);
2109 CHECK_FOR_ERROR
2110 delete $6;
Reid Spencer309080a2006-09-28 19:28:24 +00002111 } GlobalVarAttributes {
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00002112 CurGV = 0;
2113 CHECK_FOR_ERROR
2114 }
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +00002115 | OptGlobalAssign GVVisibilityStyle ALIAS AliasLinkage AliaseeRef {
Reid Spencer3b208cc2007-05-22 18:52:21 +00002116 std::string Name;
2117 if ($1) {
2118 Name = *$1;
2119 delete $1;
2120 }
Anton Korobeynikova97b6942007-04-25 14:27:10 +00002121 if (Name.empty())
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +00002122 GEN_ERROR("Alias name cannot be empty");
2123
2124 Constant* Aliasee = $5;
2125 if (Aliasee == 0)
Reid Spencer3b208cc2007-05-22 18:52:21 +00002126 GEN_ERROR(std::string("Invalid aliasee for alias: ") + Name);
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +00002127
2128 GlobalAlias* GA = new GlobalAlias(Aliasee->getType(), $4, Name, Aliasee,
2129 CurModule.CurrentModule);
2130 GA->setVisibility($2);
2131 InsertValue(GA, CurModule.Values);
Chris Lattnerdae70d42007-09-10 23:23:53 +00002132
2133
2134 // If there was a forward reference of this alias, resolve it now.
2135
2136 ValID ID;
2137 if (!Name.empty())
2138 ID = ValID::createGlobalName(Name);
2139 else
2140 ID = ValID::createGlobalID(CurModule.Values.size()-1);
2141
2142 if (GlobalValue *FWGV =
2143 CurModule.GetForwardRefForGlobal(GA->getType(), ID)) {
2144 // Replace uses of the fwdref with the actual alias.
2145 FWGV->replaceAllUsesWith(GA);
2146 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(FWGV))
2147 GV->eraseFromParent();
2148 else
2149 cast<Function>(FWGV)->eraseFromParent();
2150 }
2151 ID.destroy();
2152
Anton Korobeynikova97b6942007-04-25 14:27:10 +00002153 CHECK_FOR_ERROR
Anton Korobeynikova97b6942007-04-25 14:27:10 +00002154 }
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00002155 | TARGET TargetDefinition {
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00002156 CHECK_FOR_ERROR
2157 }
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00002158 | DEPLIBS '=' LibrariesDefinition {
Reid Spencer713eedc2006-08-18 08:43:06 +00002159 CHECK_FOR_ERROR
Chris Lattner56f73d42001-07-14 06:10:16 +00002160 }
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00002161 ;
Chris Lattner2f7c9632001-06-06 20:29:01 +00002162
2163
Chris Lattnerf09741f2006-01-23 23:05:15 +00002164AsmBlock : STRINGCONSTANT {
Chris Lattner8ebd2162006-01-24 04:14:29 +00002165 const std::string &AsmSoFar = CurModule.CurrentModule->getModuleInlineAsm();
Chris Lattnerf09741f2006-01-23 23:05:15 +00002166 if (AsmSoFar.empty())
Reid Spencer3b208cc2007-05-22 18:52:21 +00002167 CurModule.CurrentModule->setModuleInlineAsm(*$1);
Chris Lattnerf09741f2006-01-23 23:05:15 +00002168 else
Reid Spencer3b208cc2007-05-22 18:52:21 +00002169 CurModule.CurrentModule->setModuleInlineAsm(AsmSoFar+"\n"+*$1);
2170 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002171 CHECK_FOR_ERROR
Chris Lattnerf09741f2006-01-23 23:05:15 +00002172};
Chris Lattner20126132003-04-22 19:07:06 +00002173
Reid Spencer791b8ef2007-01-26 08:04:51 +00002174TargetDefinition : TRIPLE '=' STRINGCONSTANT {
Reid Spencer3b208cc2007-05-22 18:52:21 +00002175 CurModule.CurrentModule->setTargetTriple(*$3);
2176 delete $3;
John Criswell6af0b122006-10-24 19:09:48 +00002177 }
Chris Lattnerfd3d29a2006-10-22 06:07:41 +00002178 | DATALAYOUT '=' STRINGCONSTANT {
Reid Spencer3b208cc2007-05-22 18:52:21 +00002179 CurModule.CurrentModule->setDataLayout(*$3);
2180 delete $3;
Owen Andersone2237542006-10-18 02:21:12 +00002181 };
Chris Lattner20126132003-04-22 19:07:06 +00002182
Reid Spencer62c6da92004-07-25 21:30:51 +00002183LibrariesDefinition : '[' LibList ']';
Reid Spencera24de0d2004-07-25 17:58:28 +00002184
2185LibList : LibList ',' STRINGCONSTANT {
Reid Spencer3b208cc2007-05-22 18:52:21 +00002186 CurModule.CurrentModule->addLibrary(*$3);
2187 delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00002188 CHECK_FOR_ERROR
Reid Spencera24de0d2004-07-25 17:58:28 +00002189 }
2190 | STRINGCONSTANT {
Reid Spencer3b208cc2007-05-22 18:52:21 +00002191 CurModule.CurrentModule->addLibrary(*$1);
2192 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002193 CHECK_FOR_ERROR
Reid Spencera24de0d2004-07-25 17:58:28 +00002194 }
2195 | /* empty: end of list */ {
Reid Spencer713eedc2006-08-18 08:43:06 +00002196 CHECK_FOR_ERROR
Reid Spencera24de0d2004-07-25 17:58:28 +00002197 }
2198 ;
Chris Lattner20126132003-04-22 19:07:06 +00002199
Chris Lattner2f7c9632001-06-06 20:29:01 +00002200//===----------------------------------------------------------------------===//
Chris Lattner57698e22002-03-26 18:01:55 +00002201// Rules to match Function Headers
Chris Lattner2f7c9632001-06-06 20:29:01 +00002202//===----------------------------------------------------------------------===//
2203
Reid Spencer791b8ef2007-01-26 08:04:51 +00002204ArgListH : ArgListH ',' Types OptParamAttrs OptLocalName {
Reid Spencerfa35bb32006-12-31 05:40:12 +00002205 if (!UpRefs.empty())
2206 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
2207 if (*$3 == Type::VoidTy)
Reid Spencerde1d05f2007-02-05 10:16:10 +00002208 GEN_ERROR("void typed arguments are invalid");
Reid Spencerfa35bb32006-12-31 05:40:12 +00002209 ArgListEntry E; E.Attrs = $4; E.Ty = $3; E.Name = $5;
Chris Lattner149376d2002-10-13 20:57:00 +00002210 $$ = $1;
Reid Spencerfa35bb32006-12-31 05:40:12 +00002211 $1->push_back(E);
Reid Spencer713eedc2006-08-18 08:43:06 +00002212 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00002213 }
Reid Spencer791b8ef2007-01-26 08:04:51 +00002214 | Types OptParamAttrs OptLocalName {
Reid Spencerfa35bb32006-12-31 05:40:12 +00002215 if (!UpRefs.empty())
2216 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2217 if (*$1 == Type::VoidTy)
Reid Spencerde1d05f2007-02-05 10:16:10 +00002218 GEN_ERROR("void typed arguments are invalid");
Reid Spencerfa35bb32006-12-31 05:40:12 +00002219 ArgListEntry E; E.Attrs = $2; E.Ty = $1; E.Name = $3;
2220 $$ = new ArgListType;
2221 $$->push_back(E);
Reid Spencer713eedc2006-08-18 08:43:06 +00002222 CHECK_FOR_ERROR
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00002223 };
Chris Lattner2f7c9632001-06-06 20:29:01 +00002224
2225ArgList : ArgListH {
2226 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002227 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00002228 }
Chris Lattner149376d2002-10-13 20:57:00 +00002229 | ArgListH ',' DOTDOTDOT {
2230 $$ = $1;
Reid Spencerfa35bb32006-12-31 05:40:12 +00002231 struct ArgListEntry E;
2232 E.Ty = new PATypeHolder(Type::VoidTy);
2233 E.Name = 0;
Reid Spencera472f662007-04-11 02:44:20 +00002234 E.Attrs = ParamAttr::None;
Reid Spencerfa35bb32006-12-31 05:40:12 +00002235 $$->push_back(E);
Reid Spencer713eedc2006-08-18 08:43:06 +00002236 CHECK_FOR_ERROR
Chris Lattner149376d2002-10-13 20:57:00 +00002237 }
2238 | DOTDOTDOT {
Reid Spencerfa35bb32006-12-31 05:40:12 +00002239 $$ = new ArgListType;
2240 struct ArgListEntry E;
2241 E.Ty = new PATypeHolder(Type::VoidTy);
2242 E.Name = 0;
Reid Spencera472f662007-04-11 02:44:20 +00002243 E.Attrs = ParamAttr::None;
Reid Spencerfa35bb32006-12-31 05:40:12 +00002244 $$->push_back(E);
Reid Spencer713eedc2006-08-18 08:43:06 +00002245 CHECK_FOR_ERROR
Chris Lattner149376d2002-10-13 20:57:00 +00002246 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00002247 | /* empty */ {
2248 $$ = 0;
Reid Spencer713eedc2006-08-18 08:43:06 +00002249 CHECK_FOR_ERROR
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00002250 };
Chris Lattner2f7c9632001-06-06 20:29:01 +00002251
Reid Spencer791b8ef2007-01-26 08:04:51 +00002252FunctionHeaderH : OptCallingConv ResultTypes GlobalName '(' ArgList ')'
Reid Spencer136a91c2007-01-05 17:06:19 +00002253 OptFuncAttrs OptSection OptAlign {
Reid Spencer3b208cc2007-05-22 18:52:21 +00002254 std::string FunctionName(*$3);
2255 delete $3; // Free strdup'd memory!
Chris Lattner841d8b92001-11-26 18:54:16 +00002256
Reid Spencer6130ae02007-01-02 21:53:43 +00002257 // Check the function result for abstractness if this is a define. We should
2258 // have no abstract types at this point
Reid Spencer136a91c2007-01-05 17:06:19 +00002259 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved($2))
2260 GEN_ERROR("Reference to abstract result: "+ $2->get()->getDescription());
Reid Spencer6130ae02007-01-02 21:53:43 +00002261
Chris Lattner89da8a32003-04-22 18:42:41 +00002262 std::vector<const Type*> ParamTypeList;
Reid Spencer4388f0b2007-04-22 05:46:44 +00002263 ParamAttrsVector Attrs;
2264 if ($7 != ParamAttr::None) {
2265 ParamAttrsWithIndex PAWI; PAWI.index = 0; PAWI.attrs = $7;
2266 Attrs.push_back(PAWI);
2267 }
Chris Lattner53bdd312005-05-06 20:27:19 +00002268 if ($5) { // If there are arguments...
Reid Spencer71b79e32007-04-09 06:17:21 +00002269 unsigned index = 1;
2270 for (ArgListType::iterator I = $5->begin(); I != $5->end(); ++I, ++index) {
Reid Spencerfa35bb32006-12-31 05:40:12 +00002271 const Type* Ty = I->Ty->get();
Reid Spencer6130ae02007-01-02 21:53:43 +00002272 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved(I->Ty))
2273 GEN_ERROR("Reference to abstract argument: " + Ty->getDescription());
Reid Spencerfa35bb32006-12-31 05:40:12 +00002274 ParamTypeList.push_back(Ty);
2275 if (Ty != Type::VoidTy)
Reid Spencer4388f0b2007-04-22 05:46:44 +00002276 if (I->Attrs != ParamAttr::None) {
2277 ParamAttrsWithIndex PAWI; PAWI.index = index; PAWI.attrs = I->Attrs;
2278 Attrs.push_back(PAWI);
2279 }
Reid Spencerfa35bb32006-12-31 05:40:12 +00002280 }
Chris Lattner19613292002-10-15 21:41:14 +00002281 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00002282
Chris Lattnerf26e0d92001-10-13 06:41:08 +00002283 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
2284 if (isVarArg) ParamTypeList.pop_back();
2285
Reid Spencer4388f0b2007-04-22 05:46:44 +00002286 ParamAttrsList *PAL = 0;
2287 if (!Attrs.empty())
2288 PAL = ParamAttrsList::get(Attrs);
Reid Spencer71b79e32007-04-09 06:17:21 +00002289
Reid Spencer78517652007-04-28 16:06:50 +00002290 FunctionType *FT = FunctionType::get(*$2, ParamTypeList, isVarArg, PAL);
Chris Lattner19613292002-10-15 21:41:14 +00002291 const PointerType *PFT = PointerType::get(FT);
Reid Spencer136a91c2007-01-05 17:06:19 +00002292 delete $2;
Chris Lattner2f7c9632001-06-06 20:29:01 +00002293
Chris Lattner8abe1a12004-07-14 23:03:46 +00002294 ValID ID;
2295 if (!FunctionName.empty()) {
Reid Spencer791b8ef2007-01-26 08:04:51 +00002296 ID = ValID::createGlobalName((char*)FunctionName.c_str());
Chris Lattner8abe1a12004-07-14 23:03:46 +00002297 } else {
Reid Spencerfe65ae82007-03-19 18:39:36 +00002298 ID = ValID::createGlobalID(CurModule.Values.size());
Chris Lattner8abe1a12004-07-14 23:03:46 +00002299 }
2300
Chris Lattner91393ee2004-07-14 19:33:47 +00002301 Function *Fn = 0;
Chris Lattner8abe1a12004-07-14 23:03:46 +00002302 // See if this function was forward referenced. If so, recycle the object.
Reid Spencer78517652007-04-28 16:06:50 +00002303 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
Chris Lattner8abe1a12004-07-14 23:03:46 +00002304 // Move the function to the end of the list, from whereever it was
2305 // previously inserted.
2306 Fn = cast<Function>(FWRef);
2307 CurModule.CurrentModule->getFunctionList().remove(Fn);
2308 CurModule.CurrentModule->getFunctionList().push_back(Fn);
2309 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002310 (Fn = CurModule.CurrentModule->getFunction(FunctionName))) {
Reid Spencer78517652007-04-28 16:06:50 +00002311 if (Fn->getFunctionType() != FT) {
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002312 // The existing function doesn't have the same type. This is an overload
2313 // error.
2314 GEN_ERROR("Overload of function '" + FunctionName + "' not permitted.");
2315 } else if (!CurFun.isDeclare && !Fn->isDeclaration()) {
2316 // Neither the existing or the current function is a declaration and they
2317 // have the same name and same type. Clearly this is a redefinition.
Reid Spencerde1d05f2007-02-05 10:16:10 +00002318 GEN_ERROR("Redefinition of function '" + FunctionName + "'");
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002319 } if (Fn->isDeclaration()) {
2320 // Make sure to strip off any argument names so we can't get conflicts.
Chris Lattner531f9e92005-03-15 04:54:21 +00002321 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
Chris Lattner8abe1a12004-07-14 23:03:46 +00002322 AI != AE; ++AI)
2323 AI->setName("");
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002324 }
Chris Lattner91393ee2004-07-14 19:33:47 +00002325 } else { // Not already defined?
Chris Lattnereee35bf2007-04-26 05:30:35 +00002326 Fn = new Function(FT, GlobalValue::ExternalWeakLinkage, FunctionName,
Chris Lattner91393ee2004-07-14 19:33:47 +00002327 CurModule.CurrentModule);
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00002328
Chris Lattner91393ee2004-07-14 19:33:47 +00002329 InsertValue(Fn, CurModule.Values);
Chris Lattner91393ee2004-07-14 19:33:47 +00002330 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00002331
Chris Lattner61877742003-10-16 20:12:13 +00002332 CurFun.FunctionStart(Fn);
Anton Korobeynikov0ab01ff2006-09-17 13:06:18 +00002333
2334 if (CurFun.isDeclare) {
2335 // If we have declaration, always overwrite linkage. This will allow us to
2336 // correctly handle cases, when pointer to function is passed as argument to
2337 // another function.
2338 Fn->setLinkage(CurFun.Linkage);
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002339 Fn->setVisibility(CurFun.Visibility);
Anton Korobeynikov0ab01ff2006-09-17 13:06:18 +00002340 }
Chris Lattner53bdd312005-05-06 20:27:19 +00002341 Fn->setCallingConv($1);
Reid Spencer136a91c2007-01-05 17:06:19 +00002342 Fn->setAlignment($9);
2343 if ($8) {
Reid Spencer3b208cc2007-05-22 18:52:21 +00002344 Fn->setSection(*$8);
2345 delete $8;
Chris Lattner71b936c2005-11-12 00:11:10 +00002346 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00002347
Chris Lattner113f4f42002-06-25 16:13:24 +00002348 // Add all of the arguments we parsed to the function...
Chris Lattner53bdd312005-05-06 20:27:19 +00002349 if ($5) { // Is null if empty...
Chris Lattner149376d2002-10-13 20:57:00 +00002350 if (isVarArg) { // Nuke the last entry
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002351 assert($5->back().Ty->get() == Type::VoidTy && $5->back().Name == 0 &&
Reid Spencer9894d6c2007-02-05 17:01:20 +00002352 "Not a varargs marker!");
Reid Spencerfa35bb32006-12-31 05:40:12 +00002353 delete $5->back().Ty;
Chris Lattner53bdd312005-05-06 20:27:19 +00002354 $5->pop_back(); // Delete the last entry
Chris Lattner149376d2002-10-13 20:57:00 +00002355 }
Chris Lattner531f9e92005-03-15 04:54:21 +00002356 Function::arg_iterator ArgIt = Fn->arg_begin();
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002357 Function::arg_iterator ArgEnd = Fn->arg_end();
Reid Spencerfa35bb32006-12-31 05:40:12 +00002358 unsigned Idx = 1;
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002359 for (ArgListType::iterator I = $5->begin();
2360 I != $5->end() && ArgIt != ArgEnd; ++I, ++ArgIt) {
Reid Spencerfa35bb32006-12-31 05:40:12 +00002361 delete I->Ty; // Delete the typeholder...
Reid Spencer3b208cc2007-05-22 18:52:21 +00002362 setValueName(ArgIt, I->Name); // Insert arg into symtab...
Reid Spencer309080a2006-09-28 19:28:24 +00002363 CHECK_FOR_ERROR
Chris Lattner149376d2002-10-13 20:57:00 +00002364 InsertValue(ArgIt);
Reid Spencerfa35bb32006-12-31 05:40:12 +00002365 Idx++;
Chris Lattner2f7c9632001-06-06 20:29:01 +00002366 }
Reid Spencer812724e2006-12-03 05:45:44 +00002367
Chris Lattner53bdd312005-05-06 20:27:19 +00002368 delete $5; // We're now done with the argument list
Chris Lattner2f7c9632001-06-06 20:29:01 +00002369 }
Reid Spencer713eedc2006-08-18 08:43:06 +00002370 CHECK_FOR_ERROR
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00002371};
Chris Lattner2f7c9632001-06-06 20:29:01 +00002372
Chris Lattner1e1a9b42002-05-03 18:23:48 +00002373BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
2374
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002375FunctionHeader : FunctionDefineLinkage GVVisibilityStyle FunctionHeaderH BEGIN {
Chris Lattner61877742003-10-16 20:12:13 +00002376 $$ = CurFun.CurrentFunction;
Chris Lattneraa534bf2001-09-07 16:35:17 +00002377
Chris Lattner379a8d22003-04-16 20:28:45 +00002378 // Make sure that we keep track of the linkage type even if there was a
2379 // previous "declare".
2380 $$->setLinkage($1);
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002381 $$->setVisibility($2);
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00002382};
Chris Lattner2f7c9632001-06-06 20:29:01 +00002383
Chris Lattner1e1a9b42002-05-03 18:23:48 +00002384END : ENDTOK | '}'; // Allow end of '}' to end a function
2385
Chris Lattner57698e22002-03-26 18:01:55 +00002386Function : BasicBlockList END {
Chris Lattner2f7c9632001-06-06 20:29:01 +00002387 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002388 CHECK_FOR_ERROR
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00002389};
Chris Lattner2f7c9632001-06-06 20:29:01 +00002390
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002391FunctionProto : FunctionDeclareLinkage GVVisibilityStyle FunctionHeaderH {
Reid Spencerfa35bb32006-12-31 05:40:12 +00002392 CurFun.CurrentFunction->setLinkage($1);
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002393 CurFun.CurrentFunction->setVisibility($2);
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00002394 $$ = CurFun.CurrentFunction;
2395 CurFun.FunctionDone();
2396 CHECK_FOR_ERROR
2397 };
Chris Lattner2f7c9632001-06-06 20:29:01 +00002398
2399//===----------------------------------------------------------------------===//
2400// Rules to match Basic Blocks
2401//===----------------------------------------------------------------------===//
2402
Chris Lattnera02d6032006-01-25 22:26:43 +00002403OptSideEffect : /* empty */ {
2404 $$ = false;
Reid Spencer713eedc2006-08-18 08:43:06 +00002405 CHECK_FOR_ERROR
Chris Lattnera02d6032006-01-25 22:26:43 +00002406 }
2407 | SIDEEFFECT {
2408 $$ = true;
Reid Spencer713eedc2006-08-18 08:43:06 +00002409 CHECK_FOR_ERROR
Chris Lattnera02d6032006-01-25 22:26:43 +00002410 };
2411
Chris Lattner2f7c9632001-06-06 20:29:01 +00002412ConstValueRef : ESINT64VAL { // A reference to a direct constant
2413 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002414 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00002415 }
2416 | EUINT64VAL {
2417 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002418 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00002419 }
Chris Lattner212f70d2001-07-15 00:17:01 +00002420 | FPVAL { // Perhaps it's an FP constant?
2421 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002422 CHECK_FOR_ERROR
Chris Lattner212f70d2001-07-15 00:17:01 +00002423 }
Chris Lattnerc0ba90e2004-03-31 03:49:47 +00002424 | TRUETOK {
Zhou Sheng75b871f2007-01-11 12:24:14 +00002425 $$ = ValID::create(ConstantInt::getTrue());
Reid Spencer713eedc2006-08-18 08:43:06 +00002426 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00002427 }
Chris Lattnerc0ba90e2004-03-31 03:49:47 +00002428 | FALSETOK {
Zhou Sheng75b871f2007-01-11 12:24:14 +00002429 $$ = ValID::create(ConstantInt::getFalse());
Reid Spencer713eedc2006-08-18 08:43:06 +00002430 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00002431 }
Chris Lattnerfbdec252001-09-30 22:46:54 +00002432 | NULL_TOK {
2433 $$ = ValID::createNull();
Reid Spencer713eedc2006-08-18 08:43:06 +00002434 CHECK_FOR_ERROR
Vikram S. Adve7064eaf2002-07-14 22:59:28 +00002435 }
Chris Lattner4ff31492004-10-16 18:17:13 +00002436 | UNDEF {
2437 $$ = ValID::createUndef();
Reid Spencer713eedc2006-08-18 08:43:06 +00002438 CHECK_FOR_ERROR
Chris Lattner4ff31492004-10-16 18:17:13 +00002439 }
Chris Lattner8c7bda52005-12-21 17:53:02 +00002440 | ZEROINITIALIZER { // A vector zero constant.
2441 $$ = ValID::createZeroInit();
Reid Spencer713eedc2006-08-18 08:43:06 +00002442 CHECK_FOR_ERROR
Chris Lattner8c7bda52005-12-21 17:53:02 +00002443 }
Brian Gaeke02209042004-08-20 06:00:58 +00002444 | '<' ConstVector '>' { // Nonempty unsized packed vector
Reid Spencer812724e2006-12-03 05:45:44 +00002445 const Type *ETy = (*$2)[0]->getType();
Brian Gaeke02209042004-08-20 06:00:58 +00002446 int NumElements = $2->size();
2447
Reid Spencerd84d35b2007-02-15 02:26:10 +00002448 VectorType* pt = VectorType::get(ETy, NumElements);
Brian Gaeke02209042004-08-20 06:00:58 +00002449 PATypeHolder* PTy = new PATypeHolder(
Reid Spencer812724e2006-12-03 05:45:44 +00002450 HandleUpRefs(
Reid Spencerd84d35b2007-02-15 02:26:10 +00002451 VectorType::get(
Reid Spencer812724e2006-12-03 05:45:44 +00002452 ETy,
2453 NumElements)
2454 )
2455 );
Brian Gaeke02209042004-08-20 06:00:58 +00002456
2457 // Verify all elements are correct type!
2458 for (unsigned i = 0; i < $2->size(); i++) {
Reid Spencer812724e2006-12-03 05:45:44 +00002459 if (ETy != (*$2)[i]->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00002460 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Brian Gaeke02209042004-08-20 06:00:58 +00002461 ETy->getDescription() +"' as required!\nIt is of type '" +
Reid Spencer812724e2006-12-03 05:45:44 +00002462 (*$2)[i]->getType()->getDescription() + "'.");
Brian Gaeke02209042004-08-20 06:00:58 +00002463 }
2464
Reid Spencerd84d35b2007-02-15 02:26:10 +00002465 $$ = ValID::create(ConstantVector::get(pt, *$2));
Brian Gaeke02209042004-08-20 06:00:58 +00002466 delete PTy; delete $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00002467 CHECK_FOR_ERROR
Brian Gaeke02209042004-08-20 06:00:58 +00002468 }
Chris Lattner7f325cd2002-08-16 21:14:40 +00002469 | ConstExpr {
Reid Spencer812724e2006-12-03 05:45:44 +00002470 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002471 CHECK_FOR_ERROR
Chris Lattnera02d6032006-01-25 22:26:43 +00002472 }
2473 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
Reid Spencer3b208cc2007-05-22 18:52:21 +00002474 $$ = ValID::createInlineAsm(*$3, *$5, $2);
2475 delete $3;
2476 delete $5;
Reid Spencer713eedc2006-08-18 08:43:06 +00002477 CHECK_FOR_ERROR
Chris Lattner7f325cd2002-08-16 21:14:40 +00002478 };
Chris Lattnerfbdec252001-09-30 22:46:54 +00002479
Chris Lattnerf26e0d92001-10-13 06:41:08 +00002480// SymbolicValueRef - Reference to one of two ways of symbolically refering to
2481// another value.
2482//
Reid Spencer791b8ef2007-01-26 08:04:51 +00002483SymbolicValueRef : LOCALVAL_ID { // Is it an integer reference...?
2484 $$ = ValID::createLocalID($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002485 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00002486 }
Reid Spencer791b8ef2007-01-26 08:04:51 +00002487 | GLOBALVAL_ID {
2488 $$ = ValID::createGlobalID($1);
2489 CHECK_FOR_ERROR
2490 }
2491 | LocalName { // Is it a named reference...?
Reid Spencer3b208cc2007-05-22 18:52:21 +00002492 $$ = ValID::createLocalName(*$1);
2493 delete $1;
Reid Spencer791b8ef2007-01-26 08:04:51 +00002494 CHECK_FOR_ERROR
2495 }
2496 | GlobalName { // Is it a named reference...?
Reid Spencer3b208cc2007-05-22 18:52:21 +00002497 $$ = ValID::createGlobalName(*$1);
2498 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002499 CHECK_FOR_ERROR
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00002500 };
Chris Lattnerf26e0d92001-10-13 06:41:08 +00002501
2502// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00002503ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattnerf26e0d92001-10-13 06:41:08 +00002504
Chris Lattner2f7c9632001-06-06 20:29:01 +00002505
Chris Lattner252afba2001-07-26 16:29:15 +00002506// ResolvedVal - a <type> <value> pair. This is used only in cases where the
2507// type immediately preceeds the value reference, and allows complex constant
2508// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattner571a6b02001-10-03 01:49:25 +00002509ResolvedVal : Types ValueRef {
Reid Spencerfa35bb32006-12-31 05:40:12 +00002510 if (!UpRefs.empty())
2511 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2512 $$ = getVal(*$1, $2);
2513 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002514 CHECK_FOR_ERROR
Reid Spencerfa35bb32006-12-31 05:40:12 +00002515 }
2516 ;
Chris Lattner42b5a8a2001-07-25 22:47:46 +00002517
Chris Lattner2f7c9632001-06-06 20:29:01 +00002518BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner2ed776b2004-07-13 07:59:27 +00002519 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002520 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00002521 }
Chris Lattner113f4f42002-06-25 16:13:24 +00002522 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
Chris Lattner2ed776b2004-07-13 07:59:27 +00002523 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002524 CHECK_FOR_ERROR
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00002525 };
Chris Lattner2f7c9632001-06-06 20:29:01 +00002526
2527
2528// Basic blocks are terminated by branching instructions:
2529// br, br/cc, switch, ret
2530//
Reid Spencer791b8ef2007-01-26 08:04:51 +00002531BasicBlock : InstructionList OptLocalAssign BBTerminatorInst {
Chris Lattner2ed776b2004-07-13 07:59:27 +00002532 setValueName($3, $2);
Reid Spencer309080a2006-09-28 19:28:24 +00002533 CHECK_FOR_ERROR
Chris Lattnerf26e0d92001-10-13 06:41:08 +00002534 InsertValue($3);
Chris Lattnerf26e0d92001-10-13 06:41:08 +00002535 $1->getInstList().push_back($3);
Chris Lattner2f7c9632001-06-06 20:29:01 +00002536 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002537 CHECK_FOR_ERROR
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00002538 };
Chris Lattner2f7c9632001-06-06 20:29:01 +00002539
2540InstructionList : InstructionList Inst {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002541 if (CastInst *CI1 = dyn_cast<CastInst>($2))
2542 if (CastInst *CI2 = dyn_cast<CastInst>(CI1->getOperand(0)))
2543 if (CI2->getParent() == 0)
2544 $1->getInstList().push_back(CI2);
Chris Lattner2f7c9632001-06-06 20:29:01 +00002545 $1->getInstList().push_back($2);
2546 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002547 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00002548 }
Reid Spencerfe65ae82007-03-19 18:39:36 +00002549 | /* empty */ { // Empty space between instruction lists
2550 $$ = defineBBVal(ValID::createLocalID(CurFun.NextValNum));
Reid Spencer713eedc2006-08-18 08:43:06 +00002551 CHECK_FOR_ERROR
Chris Lattnere84a2ba2004-07-13 08:39:15 +00002552 }
Reid Spencerfe65ae82007-03-19 18:39:36 +00002553 | LABELSTR { // Labelled (named) basic block
Reid Spencer3b208cc2007-05-22 18:52:21 +00002554 $$ = defineBBVal(ValID::createLocalName(*$1));
2555 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002556 CHECK_FOR_ERROR
Reid Spencer3b208cc2007-05-22 18:52:21 +00002557
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00002558 };
Chris Lattner2f7c9632001-06-06 20:29:01 +00002559
Chris Lattner252afba2001-07-26 16:29:15 +00002560BBTerminatorInst : RET ResolvedVal { // Return with a result...
Reid Spencer812724e2006-12-03 05:45:44 +00002561 $$ = new ReturnInst($2);
Reid Spencer713eedc2006-08-18 08:43:06 +00002562 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00002563 }
Reid Spencerfe65ae82007-03-19 18:39:36 +00002564 | RET VOID { // Return with no result...
Chris Lattner2f7c9632001-06-06 20:29:01 +00002565 $$ = new ReturnInst();
Reid Spencer713eedc2006-08-18 08:43:06 +00002566 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00002567 }
Reid Spencerfe65ae82007-03-19 18:39:36 +00002568 | BR LABEL ValueRef { // Unconditional Branch...
Reid Spencer309080a2006-09-28 19:28:24 +00002569 BasicBlock* tmpBB = getBBVal($3);
Reid Spencer713eedc2006-08-18 08:43:06 +00002570 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002571 $$ = new BranchInst(tmpBB);
Reid Spencerfe65ae82007-03-19 18:39:36 +00002572 } // Conditional Branch...
Reid Spencer502d64e2007-01-13 05:00:20 +00002573 | BR INTTYPE ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
2574 assert(cast<IntegerType>($2)->getBitWidth() == 1 && "Not Bool?");
Reid Spencer309080a2006-09-28 19:28:24 +00002575 BasicBlock* tmpBBA = getBBVal($6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002576 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002577 BasicBlock* tmpBBB = getBBVal($9);
2578 CHECK_FOR_ERROR
Reid Spencer542964f2007-01-11 18:21:29 +00002579 Value* tmpVal = getVal(Type::Int1Ty, $3);
Reid Spencer309080a2006-09-28 19:28:24 +00002580 CHECK_FOR_ERROR
2581 $$ = new BranchInst(tmpBBA, tmpBBB, tmpVal);
Chris Lattner2f7c9632001-06-06 20:29:01 +00002582 }
2583 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencer812724e2006-12-03 05:45:44 +00002584 Value* tmpVal = getVal($2, $3);
Reid Spencer309080a2006-09-28 19:28:24 +00002585 CHECK_FOR_ERROR
2586 BasicBlock* tmpBB = getBBVal($6);
2587 CHECK_FOR_ERROR
2588 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, $8->size());
Chris Lattner2f7c9632001-06-06 20:29:01 +00002589 $$ = S;
2590
Chris Lattner89da8a32003-04-22 18:42:41 +00002591 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
Chris Lattnerae234e12002-04-09 19:41:42 +00002592 E = $8->end();
Chris Lattnerfc824c12005-02-24 05:25:17 +00002593 for (; I != E; ++I) {
2594 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
2595 S->addCase(CI, I->second);
2596 else
Reid Spencerde1d05f2007-02-05 10:16:10 +00002597 GEN_ERROR("Switch case is constant, but not a simple integer");
Chris Lattnerfc824c12005-02-24 05:25:17 +00002598 }
Chris Lattnerca96cee2004-04-17 23:49:15 +00002599 delete $8;
Reid Spencer713eedc2006-08-18 08:43:06 +00002600 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00002601 }
Chris Lattner9f3648b2003-05-15 21:30:00 +00002602 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencer812724e2006-12-03 05:45:44 +00002603 Value* tmpVal = getVal($2, $3);
Reid Spencer309080a2006-09-28 19:28:24 +00002604 CHECK_FOR_ERROR
2605 BasicBlock* tmpBB = getBBVal($6);
2606 CHECK_FOR_ERROR
2607 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, 0);
Chris Lattner9f3648b2003-05-15 21:30:00 +00002608 $$ = S;
Reid Spencer713eedc2006-08-18 08:43:06 +00002609 CHECK_FOR_ERROR
Chris Lattner9f3648b2003-05-15 21:30:00 +00002610 }
Reid Spencer136a91c2007-01-05 17:06:19 +00002611 | INVOKE OptCallingConv ResultTypes ValueRef '(' ValueRefList ')' OptFuncAttrs
Chris Lattner53bdd312005-05-06 20:27:19 +00002612 TO LABEL ValueRef UNWIND LABEL ValueRef {
Chris Lattnerf26e0d92001-10-13 06:41:08 +00002613
Reid Spencerfa35bb32006-12-31 05:40:12 +00002614 // Handle the short syntax
2615 const PointerType *PFTy = 0;
2616 const FunctionType *Ty = 0;
Reid Spencer136a91c2007-01-05 17:06:19 +00002617 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattner19613292002-10-15 21:41:14 +00002618 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattnerf26e0d92001-10-13 06:41:08 +00002619 // Pull out the types of all of the arguments...
Chris Lattner89da8a32003-04-22 18:42:41 +00002620 std::vector<const Type*> ParamTypes;
Reid Spencer78517652007-04-28 16:06:50 +00002621 ParamAttrsVector Attrs;
2622 if ($8 != ParamAttr::None) {
Chris Lattnercf40d502007-05-04 04:01:07 +00002623 ParamAttrsWithIndex PAWI; PAWI.index = 0; PAWI.attrs = $8;
Reid Spencer78517652007-04-28 16:06:50 +00002624 Attrs.push_back(PAWI);
2625 }
Reid Spencer71b79e32007-04-09 06:17:21 +00002626 ValueRefList::iterator I = $6->begin(), E = $6->end();
Reid Spencer78517652007-04-28 16:06:50 +00002627 unsigned index = 1;
2628 for (; I != E; ++I, ++index) {
Reid Spencerfa35bb32006-12-31 05:40:12 +00002629 const Type *Ty = I->Val->getType();
2630 if (Ty == Type::VoidTy)
2631 GEN_ERROR("Short call syntax cannot be used with varargs");
2632 ParamTypes.push_back(Ty);
Reid Spencer78517652007-04-28 16:06:50 +00002633 if (I->Attrs != ParamAttr::None) {
2634 ParamAttrsWithIndex PAWI; PAWI.index = index; PAWI.attrs = I->Attrs;
2635 Attrs.push_back(PAWI);
2636 }
Chris Lattnerf26e0d92001-10-13 06:41:08 +00002637 }
Reid Spencer78517652007-04-28 16:06:50 +00002638
2639 ParamAttrsList *PAL = 0;
2640 if (!Attrs.empty())
2641 PAL = ParamAttrsList::get(Attrs);
2642 Ty = FunctionType::get($3->get(), ParamTypes, false, PAL);
Chris Lattner19613292002-10-15 21:41:14 +00002643 PFTy = PointerType::get(Ty);
Chris Lattnerf26e0d92001-10-13 06:41:08 +00002644 }
Chris Lattnerf26e0d92001-10-13 06:41:08 +00002645
Reid Spencerd05bc522007-03-20 01:13:00 +00002646 delete $3;
2647
Chris Lattner53bdd312005-05-06 20:27:19 +00002648 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer309080a2006-09-28 19:28:24 +00002649 CHECK_FOR_ERROR
Reid Spencer136a91c2007-01-05 17:06:19 +00002650 BasicBlock *Normal = getBBVal($11);
Reid Spencer309080a2006-09-28 19:28:24 +00002651 CHECK_FOR_ERROR
Reid Spencer136a91c2007-01-05 17:06:19 +00002652 BasicBlock *Except = getBBVal($14);
Reid Spencer309080a2006-09-28 19:28:24 +00002653 CHECK_FOR_ERROR
Chris Lattnerf26e0d92001-10-13 06:41:08 +00002654
Reid Spencerfa35bb32006-12-31 05:40:12 +00002655 // Check the arguments
2656 ValueList Args;
2657 if ($6->empty()) { // Has no arguments?
2658 // Make sure no arguments is a good thing!
2659 if (Ty->getNumParams() != 0)
2660 GEN_ERROR("No arguments passed to a function that "
Reid Spencerde1d05f2007-02-05 10:16:10 +00002661 "expects arguments");
Chris Lattnerf26e0d92001-10-13 06:41:08 +00002662 } else { // Has arguments?
Chris Lattner57698e22002-03-26 18:01:55 +00002663 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattnerf26e0d92001-10-13 06:41:08 +00002664 // correctly!
Chris Lattnerfa829be2004-02-09 04:14:01 +00002665 FunctionType::param_iterator I = Ty->param_begin();
2666 FunctionType::param_iterator E = Ty->param_end();
Reid Spencerfa35bb32006-12-31 05:40:12 +00002667 ValueRefList::iterator ArgI = $6->begin(), ArgE = $6->end();
Reid Spencer78517652007-04-28 16:06:50 +00002668
Reid Spencerfa35bb32006-12-31 05:40:12 +00002669 for (; ArgI != ArgE && I != E; ++ArgI, ++I) {
2670 if (ArgI->Val->getType() != *I)
2671 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencerde1d05f2007-02-05 10:16:10 +00002672 (*I)->getDescription() + "'");
Reid Spencerfa35bb32006-12-31 05:40:12 +00002673 Args.push_back(ArgI->Val);
2674 }
Reid Spencer812724e2006-12-03 05:45:44 +00002675
Reid Spencerfa35bb32006-12-31 05:40:12 +00002676 if (Ty->isVarArg()) {
2677 if (I == E)
2678 for (; ArgI != ArgE; ++ArgI)
2679 Args.push_back(ArgI->Val); // push the remaining varargs
2680 } else if (I != E || ArgI != ArgE)
Reid Spencerde1d05f2007-02-05 10:16:10 +00002681 GEN_ERROR("Invalid number of parameters detected");
Chris Lattnerf26e0d92001-10-13 06:41:08 +00002682 }
Reid Spencerfa35bb32006-12-31 05:40:12 +00002683
2684 // Create the InvokeInst
David Greene703623d2007-08-27 19:04:21 +00002685 InvokeInst *II = new InvokeInst(V, Normal, Except, Args.begin(), Args.end());
Reid Spencerfa35bb32006-12-31 05:40:12 +00002686 II->setCallingConv($2);
2687 $$ = II;
Chris Lattner53bdd312005-05-06 20:27:19 +00002688 delete $6;
Reid Spencer713eedc2006-08-18 08:43:06 +00002689 CHECK_FOR_ERROR
Chris Lattner9c58cf62003-09-08 18:54:55 +00002690 }
2691 | UNWIND {
2692 $$ = new UnwindInst();
Reid Spencer713eedc2006-08-18 08:43:06 +00002693 CHECK_FOR_ERROR
Chris Lattner4ff31492004-10-16 18:17:13 +00002694 }
2695 | UNREACHABLE {
2696 $$ = new UnreachableInst();
Reid Spencer713eedc2006-08-18 08:43:06 +00002697 CHECK_FOR_ERROR
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00002698 };
Chris Lattnerf26e0d92001-10-13 06:41:08 +00002699
2700
Chris Lattner2f7c9632001-06-06 20:29:01 +00002701
2702JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
2703 $$ = $1;
Reid Spencerfe65ae82007-03-19 18:39:36 +00002704 Constant *V = cast<Constant>(getExistingVal($2, $3));
Reid Spencer309080a2006-09-28 19:28:24 +00002705 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00002706 if (V == 0)
Reid Spencerde1d05f2007-02-05 10:16:10 +00002707 GEN_ERROR("May only switch on a constant pool value");
Chris Lattner2f7c9632001-06-06 20:29:01 +00002708
Reid Spencer309080a2006-09-28 19:28:24 +00002709 BasicBlock* tmpBB = getBBVal($6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002710 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002711 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner2f7c9632001-06-06 20:29:01 +00002712 }
2713 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner89da8a32003-04-22 18:42:41 +00002714 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Reid Spencerfe65ae82007-03-19 18:39:36 +00002715 Constant *V = cast<Constant>(getExistingVal($1, $2));
Reid Spencer309080a2006-09-28 19:28:24 +00002716 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00002717
2718 if (V == 0)
Reid Spencerde1d05f2007-02-05 10:16:10 +00002719 GEN_ERROR("May only switch on a constant pool value");
Chris Lattner2f7c9632001-06-06 20:29:01 +00002720
Reid Spencer309080a2006-09-28 19:28:24 +00002721 BasicBlock* tmpBB = getBBVal($5);
Reid Spencer713eedc2006-08-18 08:43:06 +00002722 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002723 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00002724 };
Chris Lattner2f7c9632001-06-06 20:29:01 +00002725
Reid Spencer791b8ef2007-01-26 08:04:51 +00002726Inst : OptLocalAssign InstVal {
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002727 // Is this definition named?? if so, assign the name...
2728 setValueName($2, $1);
2729 CHECK_FOR_ERROR
2730 InsertValue($2);
2731 $$ = $2;
2732 CHECK_FOR_ERROR
2733 };
2734
Chris Lattner2f7c9632001-06-06 20:29:01 +00002735
Chris Lattner931ef3b2001-06-11 15:04:20 +00002736PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Reid Spencerfa35bb32006-12-31 05:40:12 +00002737 if (!UpRefs.empty())
2738 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Chris Lattner89da8a32003-04-22 18:42:41 +00002739 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Reid Spencer812724e2006-12-03 05:45:44 +00002740 Value* tmpVal = getVal(*$1, $3);
Reid Spencer713eedc2006-08-18 08:43:06 +00002741 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002742 BasicBlock* tmpBB = getBBVal($5);
2743 CHECK_FOR_ERROR
2744 $$->push_back(std::make_pair(tmpVal, tmpBB));
Reid Spencer812724e2006-12-03 05:45:44 +00002745 delete $1;
Chris Lattner931ef3b2001-06-11 15:04:20 +00002746 }
2747 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
2748 $$ = $1;
Reid Spencer309080a2006-09-28 19:28:24 +00002749 Value* tmpVal = getVal($1->front().first->getType(), $4);
Reid Spencer713eedc2006-08-18 08:43:06 +00002750 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002751 BasicBlock* tmpBB = getBBVal($6);
2752 CHECK_FOR_ERROR
2753 $1->push_back(std::make_pair(tmpVal, tmpBB));
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00002754 };
Chris Lattner931ef3b2001-06-11 15:04:20 +00002755
2756
Reid Spencerfa35bb32006-12-31 05:40:12 +00002757ValueRefList : Types ValueRef OptParamAttrs {
2758 if (!UpRefs.empty())
2759 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2760 // Used for call and invoke instructions
2761 $$ = new ValueRefList();
2762 ValueRefListEntry E; E.Attrs = $3; E.Val = getVal($1->get(), $2);
2763 $$->push_back(E);
Reid Spencerd05bc522007-03-20 01:13:00 +00002764 delete $1;
Chris Lattner2f7c9632001-06-06 20:29:01 +00002765 }
Reid Spencerfa35bb32006-12-31 05:40:12 +00002766 | ValueRefList ',' Types ValueRef OptParamAttrs {
2767 if (!UpRefs.empty())
2768 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattner2f7c9632001-06-06 20:29:01 +00002769 $$ = $1;
Reid Spencerfa35bb32006-12-31 05:40:12 +00002770 ValueRefListEntry E; E.Attrs = $5; E.Val = getVal($3->get(), $4);
2771 $$->push_back(E);
Reid Spencerd05bc522007-03-20 01:13:00 +00002772 delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00002773 CHECK_FOR_ERROR
Reid Spencerfa35bb32006-12-31 05:40:12 +00002774 }
2775 | /*empty*/ { $$ = new ValueRefList(); };
Chris Lattner2f7c9632001-06-06 20:29:01 +00002776
Reid Spencerfa35bb32006-12-31 05:40:12 +00002777IndexList // Used for gep instructions and constant expressions
Reid Spencerd6979032006-12-31 21:46:36 +00002778 : /*empty*/ { $$ = new std::vector<Value*>(); }
Reid Spencerfa35bb32006-12-31 05:40:12 +00002779 | IndexList ',' ResolvedVal {
2780 $$ = $1;
2781 $$->push_back($3);
2782 CHECK_FOR_ERROR
2783 }
Reid Spencer6829c252006-12-31 21:25:25 +00002784 ;
Chris Lattner2f7c9632001-06-06 20:29:01 +00002785
Chris Lattner06038452005-05-06 05:51:46 +00002786OptTailCall : TAIL CALL {
2787 $$ = true;
Reid Spencer713eedc2006-08-18 08:43:06 +00002788 CHECK_FOR_ERROR
Chris Lattner06038452005-05-06 05:51:46 +00002789 }
2790 | CALL {
2791 $$ = false;
Reid Spencer713eedc2006-08-18 08:43:06 +00002792 CHECK_FOR_ERROR
Chris Lattner06038452005-05-06 05:51:46 +00002793 };
2794
Chris Lattnerc0686fe2002-09-10 19:57:26 +00002795InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencerfa35bb32006-12-31 05:40:12 +00002796 if (!UpRefs.empty())
2797 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Chris Lattner03c49532007-01-15 02:27:26 +00002798 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint() &&
Reid Spencerd84d35b2007-02-15 02:26:10 +00002799 !isa<VectorType>((*$2).get()))
Reid Spencer713eedc2006-08-18 08:43:06 +00002800 GEN_ERROR(
Reid Spencerde1d05f2007-02-05 10:16:10 +00002801 "Arithmetic operator requires integer, FP, or packed operands");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002802 if (isa<VectorType>((*$2).get()) &&
Reid Spencer812724e2006-12-03 05:45:44 +00002803 ($1 == Instruction::URem ||
2804 $1 == Instruction::SRem ||
2805 $1 == Instruction::FRem))
Reid Spencer09575ba2007-02-15 03:39:18 +00002806 GEN_ERROR("Remainder not supported on vector types");
Reid Spencer812724e2006-12-03 05:45:44 +00002807 Value* val1 = getVal(*$2, $3);
Reid Spencer309080a2006-09-28 19:28:24 +00002808 CHECK_FOR_ERROR
Reid Spencer812724e2006-12-03 05:45:44 +00002809 Value* val2 = getVal(*$2, $5);
Reid Spencer309080a2006-09-28 19:28:24 +00002810 CHECK_FOR_ERROR
Reid Spencer812724e2006-12-03 05:45:44 +00002811 $$ = BinaryOperator::create($1, val1, val2);
Chris Lattnerc0686fe2002-09-10 19:57:26 +00002812 if ($$ == 0)
Reid Spencerde1d05f2007-02-05 10:16:10 +00002813 GEN_ERROR("binary operator returned null");
Reid Spencer812724e2006-12-03 05:45:44 +00002814 delete $2;
Chris Lattnerc0686fe2002-09-10 19:57:26 +00002815 }
2816 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencerfa35bb32006-12-31 05:40:12 +00002817 if (!UpRefs.empty())
2818 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Chris Lattner03c49532007-01-15 02:27:26 +00002819 if (!(*$2)->isInteger()) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00002820 if (Instruction::isShift($1) || !isa<VectorType>($2->get()) ||
2821 !cast<VectorType>($2->get())->getElementType()->isInteger())
Reid Spencerde1d05f2007-02-05 10:16:10 +00002822 GEN_ERROR("Logical operator requires integral operands");
Chris Lattnera5d70e92005-12-21 18:31:29 +00002823 }
Reid Spencer812724e2006-12-03 05:45:44 +00002824 Value* tmpVal1 = getVal(*$2, $3);
Reid Spencer309080a2006-09-28 19:28:24 +00002825 CHECK_FOR_ERROR
Reid Spencer812724e2006-12-03 05:45:44 +00002826 Value* tmpVal2 = getVal(*$2, $5);
Reid Spencer309080a2006-09-28 19:28:24 +00002827 CHECK_FOR_ERROR
Reid Spencer812724e2006-12-03 05:45:44 +00002828 $$ = BinaryOperator::create($1, tmpVal1, tmpVal2);
Chris Lattnerc0686fe2002-09-10 19:57:26 +00002829 if ($$ == 0)
Reid Spencerde1d05f2007-02-05 10:16:10 +00002830 GEN_ERROR("binary operator returned null");
Reid Spencer812724e2006-12-03 05:45:44 +00002831 delete $2;
Chris Lattnerc0686fe2002-09-10 19:57:26 +00002832 }
Reid Spencer812724e2006-12-03 05:45:44 +00002833 | ICMP IPredicates Types ValueRef ',' ValueRef {
Reid Spencerfa35bb32006-12-31 05:40:12 +00002834 if (!UpRefs.empty())
2835 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencerd84d35b2007-02-15 02:26:10 +00002836 if (isa<VectorType>((*$3).get()))
Reid Spencer09575ba2007-02-15 03:39:18 +00002837 GEN_ERROR("Vector types not supported by icmp instruction");
Reid Spencer812724e2006-12-03 05:45:44 +00002838 Value* tmpVal1 = getVal(*$3, $4);
2839 CHECK_FOR_ERROR
2840 Value* tmpVal2 = getVal(*$3, $6);
2841 CHECK_FOR_ERROR
2842 $$ = CmpInst::create($1, $2, tmpVal1, tmpVal2);
2843 if ($$ == 0)
Reid Spencerde1d05f2007-02-05 10:16:10 +00002844 GEN_ERROR("icmp operator returned null");
Reid Spencerd05bc522007-03-20 01:13:00 +00002845 delete $3;
Reid Spencer812724e2006-12-03 05:45:44 +00002846 }
2847 | FCMP FPredicates Types ValueRef ',' ValueRef {
Reid Spencerfa35bb32006-12-31 05:40:12 +00002848 if (!UpRefs.empty())
2849 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencerd84d35b2007-02-15 02:26:10 +00002850 if (isa<VectorType>((*$3).get()))
Reid Spencer09575ba2007-02-15 03:39:18 +00002851 GEN_ERROR("Vector types not supported by fcmp instruction");
Reid Spencer812724e2006-12-03 05:45:44 +00002852 Value* tmpVal1 = getVal(*$3, $4);
2853 CHECK_FOR_ERROR
2854 Value* tmpVal2 = getVal(*$3, $6);
2855 CHECK_FOR_ERROR
2856 $$ = CmpInst::create($1, $2, tmpVal1, tmpVal2);
2857 if ($$ == 0)
Reid Spencerde1d05f2007-02-05 10:16:10 +00002858 GEN_ERROR("fcmp operator returned null");
Reid Spencerd05bc522007-03-20 01:13:00 +00002859 delete $3;
Chris Lattner2f7c9632001-06-06 20:29:01 +00002860 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002861 | CastOps ResolvedVal TO Types {
Reid Spencerfa35bb32006-12-31 05:40:12 +00002862 if (!UpRefs.empty())
2863 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencer812724e2006-12-03 05:45:44 +00002864 Value* Val = $2;
Reid Spencer082a77f2007-01-17 02:47:33 +00002865 const Type* DestTy = $4->get();
2866 if (!CastInst::castIsValid($1, Val, DestTy))
2867 GEN_ERROR("invalid cast opcode for cast from '" +
2868 Val->getType()->getDescription() + "' to '" +
Reid Spencerde1d05f2007-02-05 10:16:10 +00002869 DestTy->getDescription() + "'");
Reid Spencer082a77f2007-01-17 02:47:33 +00002870 $$ = CastInst::create($1, Val, DestTy);
Reid Spencer812724e2006-12-03 05:45:44 +00002871 delete $4;
Chris Lattnera6821822001-07-08 04:57:15 +00002872 }
Chris Lattner6536f0c2004-03-12 05:51:36 +00002873 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer542964f2007-01-11 18:21:29 +00002874 if ($2->getType() != Type::Int1Ty)
Reid Spencerde1d05f2007-02-05 10:16:10 +00002875 GEN_ERROR("select condition must be boolean");
Reid Spencer812724e2006-12-03 05:45:44 +00002876 if ($4->getType() != $6->getType())
Reid Spencerde1d05f2007-02-05 10:16:10 +00002877 GEN_ERROR("select value types should match");
Reid Spencer812724e2006-12-03 05:45:44 +00002878 $$ = new SelectInst($2, $4, $6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002879 CHECK_FOR_ERROR
Chris Lattner6536f0c2004-03-12 05:51:36 +00002880 }
Chris Lattner0079e7d2003-10-18 05:53:13 +00002881 | VAARG ResolvedVal ',' Types {
Reid Spencerfa35bb32006-12-31 05:40:12 +00002882 if (!UpRefs.empty())
2883 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencer812724e2006-12-03 05:45:44 +00002884 $$ = new VAArgInst($2, *$4);
2885 delete $4;
Reid Spencer713eedc2006-08-18 08:43:06 +00002886 CHECK_FOR_ERROR
Chris Lattner0079e7d2003-10-18 05:53:13 +00002887 }
Robert Bocchinoaa1cf542006-01-10 19:04:32 +00002888 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencer812724e2006-12-03 05:45:44 +00002889 if (!ExtractElementInst::isValidOperands($2, $4))
Reid Spencerde1d05f2007-02-05 10:16:10 +00002890 GEN_ERROR("Invalid extractelement operands");
Reid Spencer812724e2006-12-03 05:45:44 +00002891 $$ = new ExtractElementInst($2, $4);
Reid Spencer713eedc2006-08-18 08:43:06 +00002892 CHECK_FOR_ERROR
Robert Bocchinoaa1cf542006-01-10 19:04:32 +00002893 }
Robert Bocchinofdf9e412006-01-17 20:06:25 +00002894 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer812724e2006-12-03 05:45:44 +00002895 if (!InsertElementInst::isValidOperands($2, $4, $6))
Reid Spencerde1d05f2007-02-05 10:16:10 +00002896 GEN_ERROR("Invalid insertelement operands");
Reid Spencer812724e2006-12-03 05:45:44 +00002897 $$ = new InsertElementInst($2, $4, $6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002898 CHECK_FOR_ERROR
Robert Bocchinofdf9e412006-01-17 20:06:25 +00002899 }
Chris Lattner8ec3c2e2006-04-08 01:18:35 +00002900 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer812724e2006-12-03 05:45:44 +00002901 if (!ShuffleVectorInst::isValidOperands($2, $4, $6))
Reid Spencerde1d05f2007-02-05 10:16:10 +00002902 GEN_ERROR("Invalid shufflevector operands");
Reid Spencer812724e2006-12-03 05:45:44 +00002903 $$ = new ShuffleVectorInst($2, $4, $6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002904 CHECK_FOR_ERROR
Chris Lattner8ec3c2e2006-04-08 01:18:35 +00002905 }
Chris Lattnerb94550e2003-10-19 21:34:28 +00002906 | PHI_TOK PHIList {
Chris Lattner931ef3b2001-06-11 15:04:20 +00002907 const Type *Ty = $2->front().first->getType();
Chris Lattner0fc43a62003-10-30 01:38:18 +00002908 if (!Ty->isFirstClassType())
Reid Spencerde1d05f2007-02-05 10:16:10 +00002909 GEN_ERROR("PHI node operands must be of first class type");
Chris Lattner931ef3b2001-06-11 15:04:20 +00002910 $$ = new PHINode(Ty);
Chris Lattner2c089492005-01-29 00:35:55 +00002911 ((PHINode*)$$)->reserveOperandSpace($2->size());
Chris Lattner2f7c9632001-06-06 20:29:01 +00002912 while ($2->begin() != $2->end()) {
Chris Lattner931ef3b2001-06-11 15:04:20 +00002913 if ($2->front().first->getType() != Ty)
Reid Spencerde1d05f2007-02-05 10:16:10 +00002914 GEN_ERROR("All elements of a PHI node must be of the same type");
Chris Lattnerda558102001-10-02 03:41:24 +00002915 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner2f7c9632001-06-06 20:29:01 +00002916 $2->pop_front();
2917 }
2918 delete $2; // Free the list...
Reid Spencer713eedc2006-08-18 08:43:06 +00002919 CHECK_FOR_ERROR
Chris Lattner06038452005-05-06 05:51:46 +00002920 }
Reid Spencer136a91c2007-01-05 17:06:19 +00002921 | OptTailCall OptCallingConv ResultTypes ValueRef '(' ValueRefList ')'
2922 OptFuncAttrs {
Reid Spencerfa35bb32006-12-31 05:40:12 +00002923
2924 // Handle the short syntax
Bill Wendlingad753612006-11-12 11:10:39 +00002925 const PointerType *PFTy = 0;
2926 const FunctionType *Ty = 0;
Reid Spencer136a91c2007-01-05 17:06:19 +00002927 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattner19613292002-10-15 21:41:14 +00002928 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner42b5a8a2001-07-25 22:47:46 +00002929 // Pull out the types of all of the arguments...
Chris Lattner89da8a32003-04-22 18:42:41 +00002930 std::vector<const Type*> ParamTypes;
Reid Spencer78517652007-04-28 16:06:50 +00002931 ParamAttrsVector Attrs;
2932 if ($8 != ParamAttr::None) {
2933 ParamAttrsWithIndex PAWI; PAWI.index = 0; PAWI.attrs = $8;
2934 Attrs.push_back(PAWI);
2935 }
2936 unsigned index = 1;
Reid Spencer71b79e32007-04-09 06:17:21 +00002937 ValueRefList::iterator I = $6->begin(), E = $6->end();
Reid Spencer78517652007-04-28 16:06:50 +00002938 for (; I != E; ++I, ++index) {
Reid Spencerfa35bb32006-12-31 05:40:12 +00002939 const Type *Ty = I->Val->getType();
2940 if (Ty == Type::VoidTy)
2941 GEN_ERROR("Short call syntax cannot be used with varargs");
2942 ParamTypes.push_back(Ty);
Reid Spencer78517652007-04-28 16:06:50 +00002943 if (I->Attrs != ParamAttr::None) {
2944 ParamAttrsWithIndex PAWI; PAWI.index = index; PAWI.attrs = I->Attrs;
2945 Attrs.push_back(PAWI);
2946 }
Chris Lattner7fac0702001-10-03 14:53:21 +00002947 }
Reid Spencer78517652007-04-28 16:06:50 +00002948
2949 ParamAttrsList *PAL = 0;
2950 if (!Attrs.empty())
2951 PAL = ParamAttrsList::get(Attrs);
2952
2953 Ty = FunctionType::get($3->get(), ParamTypes, false, PAL);
Chris Lattner19613292002-10-15 21:41:14 +00002954 PFTy = PointerType::get(Ty);
Chris Lattner42b5a8a2001-07-25 22:47:46 +00002955 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00002956
Chris Lattner53bdd312005-05-06 20:27:19 +00002957 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer309080a2006-09-28 19:28:24 +00002958 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00002959
Reid Spencer94bae692007-04-16 06:55:42 +00002960 // Check for call to invalid intrinsic to avoid crashing later.
2961 if (Function *theF = dyn_cast<Function>(V)) {
Reid Spencer9c9741e2007-04-16 22:01:57 +00002962 if (theF->hasName() && (theF->getValueName()->getKeyLength() >= 5) &&
Reid Spencer4339f7d2007-04-16 20:31:06 +00002963 (0 == strncmp(theF->getValueName()->getKeyData(), "llvm.", 5)) &&
2964 !theF->getIntrinsicID(true))
Reid Spencer94bae692007-04-16 06:55:42 +00002965 GEN_ERROR("Call to invalid LLVM intrinsic function '" +
2966 theF->getName() + "'");
2967 }
2968
Reid Spencerfa35bb32006-12-31 05:40:12 +00002969 // Check the arguments
2970 ValueList Args;
2971 if ($6->empty()) { // Has no arguments?
Chris Lattner91e08322002-07-25 20:52:56 +00002972 // Make sure no arguments is a good thing!
2973 if (Ty->getNumParams() != 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00002974 GEN_ERROR("No arguments passed to a function that "
Reid Spencerde1d05f2007-02-05 10:16:10 +00002975 "expects arguments");
Chris Lattner42b5a8a2001-07-25 22:47:46 +00002976 } else { // Has arguments?
Chris Lattner57698e22002-03-26 18:01:55 +00002977 // Loop through FunctionType's arguments and ensure they are specified
Reid Spencer78517652007-04-28 16:06:50 +00002978 // correctly!
2979 //
Chris Lattnerfa829be2004-02-09 04:14:01 +00002980 FunctionType::param_iterator I = Ty->param_begin();
2981 FunctionType::param_iterator E = Ty->param_end();
Reid Spencerfa35bb32006-12-31 05:40:12 +00002982 ValueRefList::iterator ArgI = $6->begin(), ArgE = $6->end();
Reid Spencer78517652007-04-28 16:06:50 +00002983
Reid Spencerfa35bb32006-12-31 05:40:12 +00002984 for (; ArgI != ArgE && I != E; ++ArgI, ++I) {
2985 if (ArgI->Val->getType() != *I)
2986 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencerde1d05f2007-02-05 10:16:10 +00002987 (*I)->getDescription() + "'");
Reid Spencerfa35bb32006-12-31 05:40:12 +00002988 Args.push_back(ArgI->Val);
2989 }
2990 if (Ty->isVarArg()) {
2991 if (I == E)
2992 for (; ArgI != ArgE; ++ArgI)
2993 Args.push_back(ArgI->Val); // push the remaining varargs
2994 } else if (I != E || ArgI != ArgE)
Reid Spencerde1d05f2007-02-05 10:16:10 +00002995 GEN_ERROR("Invalid number of parameters detected");
Chris Lattner42b5a8a2001-07-25 22:47:46 +00002996 }
Reid Spencerfa35bb32006-12-31 05:40:12 +00002997 // Create the call node
David Greene17a5dfe2007-08-01 03:43:44 +00002998 CallInst *CI = new CallInst(V, Args.begin(), Args.end());
Reid Spencerfa35bb32006-12-31 05:40:12 +00002999 CI->setTailCall($1);
3000 CI->setCallingConv($2);
3001 $$ = CI;
Chris Lattner53bdd312005-05-06 20:27:19 +00003002 delete $6;
Reid Spencer791b8ef2007-01-26 08:04:51 +00003003 delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00003004 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00003005 }
3006 | MemoryInst {
3007 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00003008 CHECK_FOR_ERROR
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00003009 };
Chris Lattner2f7c9632001-06-06 20:29:01 +00003010
Chris Lattner8b1680e2003-09-08 18:20:29 +00003011OptVolatile : VOLATILE {
3012 $$ = true;
Reid Spencer713eedc2006-08-18 08:43:06 +00003013 CHECK_FOR_ERROR
Chris Lattner8b1680e2003-09-08 18:20:29 +00003014 }
3015 | /* empty */ {
3016 $$ = false;
Reid Spencer713eedc2006-08-18 08:43:06 +00003017 CHECK_FOR_ERROR
Chris Lattner8b1680e2003-09-08 18:20:29 +00003018 };
3019
Chris Lattnerd8bebcd2001-07-08 21:10:27 +00003020
Chris Lattner06038452005-05-06 05:51:46 +00003021
Chris Lattnerc7de8362005-11-06 06:34:12 +00003022MemoryInst : MALLOC Types OptCAlign {
Reid Spencerfa35bb32006-12-31 05:40:12 +00003023 if (!UpRefs.empty())
3024 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer812724e2006-12-03 05:45:44 +00003025 $$ = new MallocInst(*$2, 0, $3);
3026 delete $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00003027 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00003028 }
Reid Spencer7a9c62b2007-01-12 07:05:14 +00003029 | MALLOC Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencerfa35bb32006-12-31 05:40:12 +00003030 if (!UpRefs.empty())
3031 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer812724e2006-12-03 05:45:44 +00003032 Value* tmpVal = getVal($4, $5);
Reid Spencer713eedc2006-08-18 08:43:06 +00003033 CHECK_FOR_ERROR
Reid Spencer812724e2006-12-03 05:45:44 +00003034 $$ = new MallocInst(*$2, tmpVal, $6);
3035 delete $2;
Nate Begeman848622f2005-11-05 09:21:28 +00003036 }
Chris Lattnerc7de8362005-11-06 06:34:12 +00003037 | ALLOCA Types OptCAlign {
Reid Spencerfa35bb32006-12-31 05:40:12 +00003038 if (!UpRefs.empty())
3039 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer812724e2006-12-03 05:45:44 +00003040 $$ = new AllocaInst(*$2, 0, $3);
3041 delete $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00003042 CHECK_FOR_ERROR
Nate Begeman848622f2005-11-05 09:21:28 +00003043 }
Reid Spencer7a9c62b2007-01-12 07:05:14 +00003044 | ALLOCA Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencerfa35bb32006-12-31 05:40:12 +00003045 if (!UpRefs.empty())
3046 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer812724e2006-12-03 05:45:44 +00003047 Value* tmpVal = getVal($4, $5);
Reid Spencer713eedc2006-08-18 08:43:06 +00003048 CHECK_FOR_ERROR
Reid Spencer812724e2006-12-03 05:45:44 +00003049 $$ = new AllocaInst(*$2, tmpVal, $6);
3050 delete $2;
Nate Begeman848622f2005-11-05 09:21:28 +00003051 }
Chris Lattner252afba2001-07-26 16:29:15 +00003052 | FREE ResolvedVal {
Reid Spencer812724e2006-12-03 05:45:44 +00003053 if (!isa<PointerType>($2->getType()))
Reid Spencer713eedc2006-08-18 08:43:06 +00003054 GEN_ERROR("Trying to free nonpointer type " +
Reid Spencerde1d05f2007-02-05 10:16:10 +00003055 $2->getType()->getDescription() + "");
Reid Spencer812724e2006-12-03 05:45:44 +00003056 $$ = new FreeInst($2);
Reid Spencer713eedc2006-08-18 08:43:06 +00003057 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00003058 }
3059
Christopher Lamb84485702007-04-22 19:24:39 +00003060 | OptVolatile LOAD Types ValueRef OptCAlign {
Reid Spencerfa35bb32006-12-31 05:40:12 +00003061 if (!UpRefs.empty())
3062 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencer812724e2006-12-03 05:45:44 +00003063 if (!isa<PointerType>($3->get()))
Reid Spencer713eedc2006-08-18 08:43:06 +00003064 GEN_ERROR("Can't load from nonpointer type: " +
Reid Spencer812724e2006-12-03 05:45:44 +00003065 (*$3)->getDescription());
3066 if (!cast<PointerType>($3->get())->getElementType()->isFirstClassType())
Reid Spencer713eedc2006-08-18 08:43:06 +00003067 GEN_ERROR("Can't load from pointer of non-first-class type: " +
Reid Spencer812724e2006-12-03 05:45:44 +00003068 (*$3)->getDescription());
3069 Value* tmpVal = getVal(*$3, $4);
Reid Spencer713eedc2006-08-18 08:43:06 +00003070 CHECK_FOR_ERROR
Christopher Lamb84485702007-04-22 19:24:39 +00003071 $$ = new LoadInst(tmpVal, "", $1, $5);
Reid Spencer812724e2006-12-03 05:45:44 +00003072 delete $3;
Chris Lattnerd8bebcd2001-07-08 21:10:27 +00003073 }
Christopher Lamb84485702007-04-22 19:24:39 +00003074 | OptVolatile STORE ResolvedVal ',' Types ValueRef OptCAlign {
Reid Spencerfa35bb32006-12-31 05:40:12 +00003075 if (!UpRefs.empty())
3076 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencer812724e2006-12-03 05:45:44 +00003077 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner667ded92003-09-01 16:31:28 +00003078 if (!PT)
Reid Spencer713eedc2006-08-18 08:43:06 +00003079 GEN_ERROR("Can't store to a nonpointer type: " +
Reid Spencer812724e2006-12-03 05:45:44 +00003080 (*$5)->getDescription());
Chris Lattner667ded92003-09-01 16:31:28 +00003081 const Type *ElTy = PT->getElementType();
Reid Spencer812724e2006-12-03 05:45:44 +00003082 if (ElTy != $3->getType())
3083 GEN_ERROR("Can't store '" + $3->getType()->getDescription() +
Reid Spencerde1d05f2007-02-05 10:16:10 +00003084 "' into space of type '" + ElTy->getDescription() + "'");
Chris Lattner319c47a2002-08-21 23:51:21 +00003085
Reid Spencer812724e2006-12-03 05:45:44 +00003086 Value* tmpVal = getVal(*$5, $6);
Reid Spencer713eedc2006-08-18 08:43:06 +00003087 CHECK_FOR_ERROR
Christopher Lamb84485702007-04-22 19:24:39 +00003088 $$ = new StoreInst($3, tmpVal, $1, $7);
Reid Spencer812724e2006-12-03 05:45:44 +00003089 delete $5;
Chris Lattner62ecb4a2001-07-08 23:22:50 +00003090 }
Chris Lattner476148f2001-11-26 16:54:11 +00003091 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencerfa35bb32006-12-31 05:40:12 +00003092 if (!UpRefs.empty())
3093 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer812724e2006-12-03 05:45:44 +00003094 if (!isa<PointerType>($2->get()))
Reid Spencerde1d05f2007-02-05 10:16:10 +00003095 GEN_ERROR("getelementptr insn requires pointer operand");
Chris Lattnerfd9fbe12004-04-05 01:30:04 +00003096
David Greenec656cbb2007-09-04 15:46:09 +00003097 if (!GetElementPtrInst::getIndexedType(*$2, $4->begin(), $4->end(), true))
Reid Spencer713eedc2006-08-18 08:43:06 +00003098 GEN_ERROR("Invalid getelementptr indices for type '" +
Reid Spencerde1d05f2007-02-05 10:16:10 +00003099 (*$2)->getDescription()+ "'");
Reid Spencer812724e2006-12-03 05:45:44 +00003100 Value* tmpVal = getVal(*$2, $3);
Reid Spencer713eedc2006-08-18 08:43:06 +00003101 CHECK_FOR_ERROR
David Greenec656cbb2007-09-04 15:46:09 +00003102 $$ = new GetElementPtrInst(tmpVal, $4->begin(), $4->end());
Reid Spencer812724e2006-12-03 05:45:44 +00003103 delete $2;
Reid Spencer309080a2006-09-28 19:28:24 +00003104 delete $4;
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00003105 };
Chris Lattnerd8bebcd2001-07-08 21:10:27 +00003106
Brian Gaeke960707c2003-11-11 22:41:34 +00003107
Chris Lattner2f7c9632001-06-06 20:29:01 +00003108%%
Reid Spencer713eedc2006-08-18 08:43:06 +00003109
Reid Spencerfa35bb32006-12-31 05:40:12 +00003110// common code from the two 'RunVMAsmParser' functions
3111static Module* RunParser(Module * M) {
3112
3113 llvmAsmlineno = 1; // Reset the current line number...
3114 CurModule.CurrentModule = M;
3115#if YYDEBUG
3116 yydebug = Debug;
3117#endif
3118
3119 // Check to make sure the parser succeeded
3120 if (yyparse()) {
3121 if (ParserResult)
3122 delete ParserResult;
3123 return 0;
3124 }
3125
Reid Spencercf2ccbf2007-03-30 01:37:13 +00003126 // Emit an error if there are any unresolved types left.
3127 if (!CurModule.LateResolveTypes.empty()) {
3128 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
3129 if (DID.Type == ValID::LocalName) {
3130 GenerateError("Undefined type remains at eof: '"+DID.getName() + "'");
3131 } else {
3132 GenerateError("Undefined type remains at eof: #" + itostr(DID.Num));
3133 }
3134 if (ParserResult)
3135 delete ParserResult;
3136 return 0;
3137 }
3138
3139 // Emit an error if there are any unresolved values left.
3140 if (!CurModule.LateResolveValues.empty()) {
3141 Value *V = CurModule.LateResolveValues.back();
3142 std::map<Value*, std::pair<ValID, int> >::iterator I =
3143 CurModule.PlaceHolderInfo.find(V);
3144
3145 if (I != CurModule.PlaceHolderInfo.end()) {
3146 ValID &DID = I->second.first;
3147 if (DID.Type == ValID::LocalName) {
3148 GenerateError("Undefined value remains at eof: "+DID.getName() + "'");
3149 } else {
3150 GenerateError("Undefined value remains at eof: #" + itostr(DID.Num));
3151 }
3152 if (ParserResult)
3153 delete ParserResult;
3154 return 0;
3155 }
3156 }
3157
Reid Spencerfa35bb32006-12-31 05:40:12 +00003158 // Check to make sure that parsing produced a result
3159 if (!ParserResult)
3160 return 0;
3161
3162 // Reset ParserResult variable while saving its value for the result.
3163 Module *Result = ParserResult;
3164 ParserResult = 0;
3165
3166 return Result;
3167}
3168
Reid Spencer713eedc2006-08-18 08:43:06 +00003169void llvm::GenerateError(const std::string &message, int LineNo) {
3170 if (LineNo == -1) LineNo = llvmAsmlineno;
3171 // TODO: column number in exception
3172 if (TheParseError)
3173 TheParseError->setError(CurFilename, message, LineNo);
3174 TriggerError = 1;
3175}
3176
Chris Lattnera6821822001-07-08 04:57:15 +00003177int yyerror(const char *ErrorMsg) {
Chris Lattner89da8a32003-04-22 18:42:41 +00003178 std::string where
3179 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Vikram S. Adve7064eaf2002-07-14 22:59:28 +00003180 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00003181 std::string errMsg = where + "error: " + std::string(ErrorMsg);
3182 if (yychar != YYEMPTY && yychar != 0)
3183 errMsg += " while reading token: '" + std::string(llvmAsmtext, llvmAsmleng)+
3184 "'";
Reid Spencer713eedc2006-08-18 08:43:06 +00003185 GenerateError(errMsg);
Chris Lattner2f7c9632001-06-06 20:29:01 +00003186 return 0;
3187}