blob: 9d7b063d0c30f4c362346340da7b3ad1a1e3365e [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?
Reid Spencer309080a2006-09-28 19:28:24 +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 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000419 return ConstantFP::get(Ty, D.ConstPoolFP);
Misha Brukman8b477072005-05-10 22:02:28 +0000420
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000421 case ValID::ConstNullVal: // Is it a null value?
Reid Spencer309080a2006-09-28 19:28:24 +0000422 if (!isa<PointerType>(Ty)) {
Reid Spencerde1d05f2007-02-05 10:16:10 +0000423 GenerateError("Cannot create a a non pointer null");
Reid Spencer309080a2006-09-28 19:28:24 +0000424 return 0;
425 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000426 return ConstantPointerNull::get(cast<PointerType>(Ty));
Misha Brukman8b477072005-05-10 22:02:28 +0000427
Chris Lattner4ff31492004-10-16 18:17:13 +0000428 case ValID::ConstUndefVal: // Is it an undef value?
429 return UndefValue::get(Ty);
Misha Brukman8b477072005-05-10 22:02:28 +0000430
Chris Lattner8c7bda52005-12-21 17:53:02 +0000431 case ValID::ConstZeroVal: // Is it a zero value?
432 return Constant::getNullValue(Ty);
433
Chris Lattner7f325cd2002-08-16 21:14:40 +0000434 case ValID::ConstantVal: // Fully resolved constant?
Reid Spencer309080a2006-09-28 19:28:24 +0000435 if (D.ConstantValue->getType() != Ty) {
Reid Spencerde1d05f2007-02-05 10:16:10 +0000436 GenerateError("Constant expression type different from required type");
Reid Spencer309080a2006-09-28 19:28:24 +0000437 return 0;
438 }
Chris Lattner7f325cd2002-08-16 21:14:40 +0000439 return D.ConstantValue;
440
Chris Lattnera02d6032006-01-25 22:26:43 +0000441 case ValID::InlineAsmVal: { // Inline asm expression
442 const PointerType *PTy = dyn_cast<PointerType>(Ty);
443 const FunctionType *FTy =
444 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
Reid Spencer309080a2006-09-28 19:28:24 +0000445 if (!FTy || !InlineAsm::Verify(FTy, D.IAD->Constraints)) {
Reid Spencerde1d05f2007-02-05 10:16:10 +0000446 GenerateError("Invalid type for asm constraint string");
Reid Spencer309080a2006-09-28 19:28:24 +0000447 return 0;
448 }
Chris Lattnera02d6032006-01-25 22:26:43 +0000449 InlineAsm *IA = InlineAsm::get(FTy, D.IAD->AsmString, D.IAD->Constraints,
450 D.IAD->HasSideEffects);
451 D.destroy(); // Free InlineAsmDescriptor.
452 return IA;
453 }
Chris Lattneraa534bf2001-09-07 16:35:17 +0000454 default:
Reid Spencer9894d6c2007-02-05 17:01:20 +0000455 assert(0 && "Unhandled case!");
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000456 return 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000457 } // End of switch
458
Reid Spencer9894d6c2007-02-05 17:01:20 +0000459 assert(0 && "Unhandled case!");
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000460 return 0;
461}
462
Reid Spencerfe65ae82007-03-19 18:39:36 +0000463// getVal - This function is identical to getExistingVal, except that if a
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000464// value is not already defined, it "improvises" by creating a placeholder var
465// that looks and acts just like the requested variable. When the value is
466// defined later, all uses of the placeholder variable are replaced with the
467// real thing.
468//
Chris Lattner95230b02004-07-14 01:33:11 +0000469static Value *getVal(const Type *Ty, const ValID &ID) {
Reid Spencer309080a2006-09-28 19:28:24 +0000470 if (Ty == Type::LabelTy) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000471 GenerateError("Cannot use a basic block here");
Reid Spencer309080a2006-09-28 19:28:24 +0000472 return 0;
473 }
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000474
Chris Lattner95230b02004-07-14 01:33:11 +0000475 // See if the value has already been defined.
Reid Spencerfe65ae82007-03-19 18:39:36 +0000476 Value *V = getExistingVal(Ty, ID);
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000477 if (V) return V;
Reid Spencer309080a2006-09-28 19:28:24 +0000478 if (TriggerError) return 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000479
Reid Spencer309080a2006-09-28 19:28:24 +0000480 if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty)) {
Reid Spencerde1d05f2007-02-05 10:16:10 +0000481 GenerateError("Invalid use of a composite type");
Reid Spencer309080a2006-09-28 19:28:24 +0000482 return 0;
483 }
Chris Lattnerdcb82f52005-03-23 01:29:26 +0000484
Chris Lattner2f7c9632001-06-06 20:29:01 +0000485 // If we reached here, we referenced either a symbol that we don't know about
486 // or an id number that hasn't been read yet. We may be referencing something
487 // forward, so just create an entry to be resolved later and get to it...
488 //
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000489 switch (ID.Type) {
490 case ValID::GlobalName:
Reid Spencerb19636e2007-04-28 14:13:42 +0000491 case ValID::GlobalID: {
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000492 const PointerType *PTy = dyn_cast<PointerType>(Ty);
493 if (!PTy) {
494 GenerateError("Invalid type for reference to global" );
495 return 0;
496 }
497 const Type* ElTy = PTy->getElementType();
498 if (const FunctionType *FTy = dyn_cast<FunctionType>(ElTy))
499 V = new Function(FTy, GlobalValue::ExternalLinkage);
500 else
501 V = new GlobalVariable(ElTy, false, GlobalValue::ExternalLinkage);
502 break;
Reid Spencerb19636e2007-04-28 14:13:42 +0000503 }
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000504 default:
505 V = new Argument(Ty);
506 }
507
Chris Lattnerd9c9c492004-07-13 08:28:21 +0000508 // Remember where this forward reference came from. FIXME, shouldn't we try
509 // to recycle these things??
Chris Lattner95230b02004-07-14 01:33:11 +0000510 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
Chris Lattnerd9c9c492004-07-13 08:28:21 +0000511 llvmAsmlineno)));
512
Chris Lattner57698e22002-03-26 18:01:55 +0000513 if (inFunctionScope())
Chris Lattnerd9c9c492004-07-13 08:28:21 +0000514 InsertValue(V, CurFun.LateResolveValues);
Misha Brukman8b477072005-05-10 22:02:28 +0000515 else
Chris Lattnerd9c9c492004-07-13 08:28:21 +0000516 InsertValue(V, CurModule.LateResolveValues);
517 return V;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000518}
519
Reid Spencerfe65ae82007-03-19 18:39:36 +0000520/// defineBBVal - This is a definition of a new basic block with the specified
521/// identifier which must be the same as CurFun.NextValNum, if its numeric.
522static BasicBlock *defineBBVal(const ValID &ID) {
Reid Spencer9894d6c2007-02-05 17:01:20 +0000523 assert(inFunctionScope() && "Can't get basic block at global scope!");
Chris Lattner95230b02004-07-14 01:33:11 +0000524
Chris Lattner4accae92004-07-14 06:28:35 +0000525 BasicBlock *BB = 0;
Chris Lattner95230b02004-07-14 01:33:11 +0000526
Reid Spencerfe65ae82007-03-19 18:39:36 +0000527 // First, see if this was forward referenced
Chris Lattner95230b02004-07-14 01:33:11 +0000528
Reid Spencerfe65ae82007-03-19 18:39:36 +0000529 std::map<ValID, BasicBlock*>::iterator BBI = CurFun.BBForwardRefs.find(ID);
530 if (BBI != CurFun.BBForwardRefs.end()) {
531 BB = BBI->second;
Chris Lattner4accae92004-07-14 06:28:35 +0000532 // The forward declaration could have been inserted anywhere in the
533 // function: insert it into the correct place now.
534 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
535 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
Reid Spencerfe65ae82007-03-19 18:39:36 +0000536
Reid Spencerd05bc522007-03-20 01:13:00 +0000537 // We're about to erase the entry, save the key so we can clean it up.
538 ValID Tmp = BBI->first;
539
Reid Spencerfe65ae82007-03-19 18:39:36 +0000540 // Erase the forward ref from the map as its no longer "forward"
541 CurFun.BBForwardRefs.erase(ID);
542
Reid Spencerd05bc522007-03-20 01:13:00 +0000543 // The key has been removed from the map but so we don't want to leave
544 // strdup'd memory around so destroy it too.
545 Tmp.destroy();
546
Reid Spencerfe65ae82007-03-19 18:39:36 +0000547 // If its a numbered definition, bump the number and set the BB value.
548 if (ID.Type == ValID::LocalID) {
549 assert(ID.Num == CurFun.NextValNum && "Invalid new block number");
550 InsertValue(BB);
551 }
552
553 ID.destroy();
554 return BB;
555 }
556
557 // We haven't seen this BB before and its first mention is a definition.
558 // Just create it and return it.
Reid Spencer3b208cc2007-05-22 18:52:21 +0000559 std::string Name (ID.Type == ValID::LocalName ? ID.getName() : "");
Reid Spencerfe65ae82007-03-19 18:39:36 +0000560 BB = new BasicBlock(Name, CurFun.CurrentFunction);
561 if (ID.Type == ValID::LocalID) {
562 assert(ID.Num == CurFun.NextValNum && "Invalid new block number");
563 InsertValue(BB);
Chris Lattner4accae92004-07-14 06:28:35 +0000564 }
Reid Spencerfe65ae82007-03-19 18:39:36 +0000565
566 ID.destroy(); // Free strdup'd memory
567 return BB;
568}
569
570/// getBBVal - get an existing BB value or create a forward reference for it.
571///
572static BasicBlock *getBBVal(const ValID &ID) {
573 assert(inFunctionScope() && "Can't get basic block at global scope!");
574
575 BasicBlock *BB = 0;
576
577 std::map<ValID, BasicBlock*>::iterator BBI = CurFun.BBForwardRefs.find(ID);
578 if (BBI != CurFun.BBForwardRefs.end()) {
579 BB = BBI->second;
580 } if (ID.Type == ValID::LocalName) {
Reid Spencer3b208cc2007-05-22 18:52:21 +0000581 std::string Name = ID.getName();
Reid Spencerfe65ae82007-03-19 18:39:36 +0000582 Value *N = CurFun.CurrentFunction->getValueSymbolTable().lookup(Name);
583 if (N)
584 if (N->getType()->getTypeID() == Type::LabelTyID)
585 BB = cast<BasicBlock>(N);
586 else
587 GenerateError("Reference to label '" + Name + "' is actually of type '"+
588 N->getType()->getDescription() + "'");
589 } else if (ID.Type == ValID::LocalID) {
590 if (ID.Num < CurFun.NextValNum && ID.Num < CurFun.Values.size()) {
591 if (CurFun.Values[ID.Num]->getType()->getTypeID() == Type::LabelTyID)
592 BB = cast<BasicBlock>(CurFun.Values[ID.Num]);
593 else
594 GenerateError("Reference to label '%" + utostr(ID.Num) +
595 "' is actually of type '"+
596 CurFun.Values[ID.Num]->getType()->getDescription() + "'");
597 }
598 } else {
599 GenerateError("Illegal label reference " + ID.getName());
600 return 0;
601 }
602
603 // If its already been defined, return it now.
604 if (BB) {
605 ID.destroy(); // Free strdup'd memory.
606 return BB;
607 }
608
609 // Otherwise, this block has not been seen before, create it.
610 std::string Name;
611 if (ID.Type == ValID::LocalName)
Reid Spencer3b208cc2007-05-22 18:52:21 +0000612 Name = ID.getName();
Reid Spencerfe65ae82007-03-19 18:39:36 +0000613 BB = new BasicBlock(Name, CurFun.CurrentFunction);
614
615 // Insert it in the forward refs map.
616 CurFun.BBForwardRefs[ID] = BB;
617
Chris Lattner95230b02004-07-14 01:33:11 +0000618 return BB;
619}
620
Chris Lattner2f7c9632001-06-06 20:29:01 +0000621
622//===----------------------------------------------------------------------===//
623// Code to handle forward references in instructions
624//===----------------------------------------------------------------------===//
625//
626// This code handles the late binding needed with statements that reference
627// values not defined yet... for example, a forward branch, or the PHI node for
628// a loop body.
629//
Chris Lattner61877742003-10-16 20:12:13 +0000630// This keeps a table (CurFun.LateResolveValues) of all such forward references
Chris Lattner2f7c9632001-06-06 20:29:01 +0000631// and back patchs after we are done.
632//
633
Misha Brukman8b477072005-05-10 22:02:28 +0000634// ResolveDefinitions - If we could not resolve some defs at parsing
635// time (forward branches, phi functions for loops, etc...) resolve the
Chris Lattner2f7c9632001-06-06 20:29:01 +0000636// defs now...
637//
Misha Brukman8b477072005-05-10 22:02:28 +0000638static void
Reid Spencerfe65ae82007-03-19 18:39:36 +0000639ResolveDefinitions(ValueList &LateResolvers, ValueList *FutureLateResolvers) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000640 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Reid Spencerfe65ae82007-03-19 18:39:36 +0000641 while (!LateResolvers.empty()) {
642 Value *V = LateResolvers.back();
643 LateResolvers.pop_back();
Chris Lattnerd9c9c492004-07-13 08:28:21 +0000644
Reid Spencerfe65ae82007-03-19 18:39:36 +0000645 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
646 CurModule.PlaceHolderInfo.find(V);
647 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
Chris Lattnerd9c9c492004-07-13 08:28:21 +0000648
Reid Spencerfe65ae82007-03-19 18:39:36 +0000649 ValID &DID = PHI->second.first;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000650
Reid Spencerfe65ae82007-03-19 18:39:36 +0000651 Value *TheRealValue = getExistingVal(V->getType(), DID);
652 if (TriggerError)
653 return;
654 if (TheRealValue) {
655 V->replaceAllUsesWith(TheRealValue);
656 delete V;
657 CurModule.PlaceHolderInfo.erase(PHI);
658 } else if (FutureLateResolvers) {
659 // Functions have their unresolved items forwarded to the module late
660 // resolver table
661 InsertValue(V, *FutureLateResolvers);
662 } else {
663 if (DID.Type == ValID::LocalName || DID.Type == ValID::GlobalName) {
664 GenerateError("Reference to an invalid definition: '" +DID.getName()+
665 "' of type '" + V->getType()->getDescription() + "'",
666 PHI->second.second);
Reid Spencer309080a2006-09-28 19:28:24 +0000667 return;
Chris Lattner322e49a2001-10-16 19:54:17 +0000668 } else {
Reid Spencerfe65ae82007-03-19 18:39:36 +0000669 GenerateError("Reference to an invalid definition: #" +
670 itostr(DID.Num) + " of type '" +
671 V->getType()->getDescription() + "'",
672 PHI->second.second);
673 return;
Chris Lattneraa534bf2001-09-07 16:35:17 +0000674 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000675 }
676 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000677 LateResolvers.clear();
678}
679
Chris Lattnerd50fa5a2001-10-22 05:56:09 +0000680// ResolveTypeTo - A brand new type was just declared. This means that (if
681// name is not null) things referencing Name can be resolved. Otherwise, things
682// refering to the number can be resolved. Do this now.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000683//
Reid Spencer3b208cc2007-05-22 18:52:21 +0000684static void ResolveTypeTo(std::string *Name, const Type *ToTy) {
Chris Lattnera5f4b772005-03-21 06:27:42 +0000685 ValID D;
Reid Spencer3b208cc2007-05-22 18:52:21 +0000686 if (Name)
687 D = ValID::createLocalName(*Name);
688 else
689 D = ValID::createLocalID(CurModule.Types.size());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000690
Reid Spencerd4701792006-11-28 07:28:14 +0000691 std::map<ValID, PATypeHolder>::iterator I =
Chris Lattnera5f4b772005-03-21 06:27:42 +0000692 CurModule.LateResolveTypes.find(D);
693 if (I != CurModule.LateResolveTypes.end()) {
Reid Spencerd4701792006-11-28 07:28:14 +0000694 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattnera5f4b772005-03-21 06:27:42 +0000695 CurModule.LateResolveTypes.erase(I);
Chris Lattneraa534bf2001-09-07 16:35:17 +0000696 }
697}
698
Chris Lattner7fb14f52001-09-18 04:00:54 +0000699// setValueName - Set the specified value to the name given. The name may be
700// null potentially, in which case this is a noop. The string passed in is
Chris Lattner2ed776b2004-07-13 07:59:27 +0000701// assumed to be a malloc'd string buffer, and is free'd by this function.
702//
Reid Spencer3b208cc2007-05-22 18:52:21 +0000703static void setValueName(Value *V, std::string *NameStr) {
Reid Spencer791b8ef2007-01-26 08:04:51 +0000704 if (!NameStr) return;
Reid Spencer3b208cc2007-05-22 18:52:21 +0000705 std::string Name(*NameStr); // Copy string
706 delete NameStr; // Free old string
Chris Lattner30b35cf2004-07-13 08:12:39 +0000707
Reid Spencer791b8ef2007-01-26 08:04:51 +0000708 if (V->getType() == Type::VoidTy) {
Reid Spencerde1d05f2007-02-05 10:16:10 +0000709 GenerateError("Can't assign name '" + Name+"' to value with void type");
Reid Spencer791b8ef2007-01-26 08:04:51 +0000710 return;
Chris Lattner2ed776b2004-07-13 07:59:27 +0000711 }
Reid Spencer791b8ef2007-01-26 08:04:51 +0000712
Reid Spencer9894d6c2007-02-05 17:01:20 +0000713 assert(inFunctionScope() && "Must be in function scope!");
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000714 ValueSymbolTable &ST = CurFun.CurrentFunction->getValueSymbolTable();
715 if (ST.lookup(Name)) {
Reid Spencer791b8ef2007-01-26 08:04:51 +0000716 GenerateError("Redefinition of value '" + Name + "' of type '" +
Reid Spencerde1d05f2007-02-05 10:16:10 +0000717 V->getType()->getDescription() + "'");
Reid Spencer791b8ef2007-01-26 08:04:51 +0000718 return;
719 }
720
721 // Set the name.
722 V->setName(Name);
Chris Lattner2ed776b2004-07-13 07:59:27 +0000723}
724
Chris Lattner42dd4742004-07-14 08:23:52 +0000725/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
726/// this is a declaration, otherwise it is a definition.
Chris Lattnerbce7ca8a2005-11-12 18:21:21 +0000727static GlobalVariable *
Reid Spencer3b208cc2007-05-22 18:52:21 +0000728ParseGlobalVariable(std::string *NameStr,
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000729 GlobalValue::LinkageTypes Linkage,
730 GlobalValue::VisibilityTypes Visibility,
Chris Lattnerbce7ca8a2005-11-12 18:21:21 +0000731 bool isConstantGlobal, const Type *Ty,
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +0000732 Constant *Initializer, bool IsThreadLocal) {
Reid Spencer309080a2006-09-28 19:28:24 +0000733 if (isa<FunctionType>(Ty)) {
Reid Spencerde1d05f2007-02-05 10:16:10 +0000734 GenerateError("Cannot declare global vars of function type");
Reid Spencer309080a2006-09-28 19:28:24 +0000735 return 0;
736 }
Chris Lattnere875d482004-07-14 20:42:57 +0000737
Misha Brukman8b477072005-05-10 22:02:28 +0000738 const PointerType *PTy = PointerType::get(Ty);
Chris Lattner8abe1a12004-07-14 23:03:46 +0000739
Chris Lattnere875d482004-07-14 20:42:57 +0000740 std::string Name;
741 if (NameStr) {
Reid Spencer3b208cc2007-05-22 18:52:21 +0000742 Name = *NameStr; // Copy string
743 delete NameStr; // Free old string
Chris Lattner42dd4742004-07-14 08:23:52 +0000744 }
Chris Lattnere875d482004-07-14 20:42:57 +0000745
Chris Lattner4c9210e2004-07-14 21:44:00 +0000746 // See if this global value was forward referenced. If so, recycle the
747 // object.
Misha Brukman8b477072005-05-10 22:02:28 +0000748 ValID ID;
Chris Lattner4c9210e2004-07-14 21:44:00 +0000749 if (!Name.empty()) {
Reid Spencer3b208cc2007-05-22 18:52:21 +0000750 ID = ValID::createGlobalName(Name);
Chris Lattnere875d482004-07-14 20:42:57 +0000751 } else {
Reid Spencerfe65ae82007-03-19 18:39:36 +0000752 ID = ValID::createGlobalID(CurModule.Values.size());
Chris Lattner4c9210e2004-07-14 21:44:00 +0000753 }
754
Reid Spencer78517652007-04-28 16:06:50 +0000755 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
Misha Brukman8b477072005-05-10 22:02:28 +0000756 // Move the global to the end of the list, from whereever it was
Chris Lattner4c9210e2004-07-14 21:44:00 +0000757 // previously inserted.
758 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
759 CurModule.CurrentModule->getGlobalList().remove(GV);
760 CurModule.CurrentModule->getGlobalList().push_back(GV);
761 GV->setInitializer(Initializer);
762 GV->setLinkage(Linkage);
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000763 GV->setVisibility(Visibility);
Chris Lattner4c9210e2004-07-14 21:44:00 +0000764 GV->setConstant(isConstantGlobal);
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +0000765 GV->setThreadLocal(IsThreadLocal);
Chris Lattnerfe050242004-07-16 01:18:09 +0000766 InsertValue(GV, CurModule.Values);
Chris Lattnerbce7ca8a2005-11-12 18:21:21 +0000767 return GV;
Chris Lattnere875d482004-07-14 20:42:57 +0000768 }
Chris Lattner8abe1a12004-07-14 23:03:46 +0000769
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000770 // If this global has a name
Chris Lattner8abe1a12004-07-14 23:03:46 +0000771 if (!Name.empty()) {
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000772 // if the global we're parsing has an initializer (is a definition) and
773 // has external linkage.
774 if (Initializer && Linkage != GlobalValue::InternalLinkage)
775 // If there is already a global with external linkage with this name
776 if (CurModule.CurrentModule->getGlobalVariable(Name, false)) {
777 // If we allow this GVar to get created, it will be renamed in the
778 // symbol table because it conflicts with an existing GVar. We can't
779 // allow redefinition of GVars whose linking indicates that their name
780 // must stay the same. Issue the error.
781 GenerateError("Redefinition of global variable named '" + Name +
782 "' of type '" + Ty->getDescription() + "'");
783 return 0;
784 }
Chris Lattner8abe1a12004-07-14 23:03:46 +0000785 }
786
787 // Otherwise there is no existing GV to use, create one now.
Chris Lattnerfe050242004-07-16 01:18:09 +0000788 GlobalVariable *GV =
Misha Brukman8b477072005-05-10 22:02:28 +0000789 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +0000790 CurModule.CurrentModule, IsThreadLocal);
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000791 GV->setVisibility(Visibility);
Chris Lattnerfe050242004-07-16 01:18:09 +0000792 InsertValue(GV, CurModule.Values);
Chris Lattnerbce7ca8a2005-11-12 18:21:21 +0000793 return GV;
Chris Lattner42dd4742004-07-14 08:23:52 +0000794}
Chris Lattner2ed776b2004-07-13 07:59:27 +0000795
Reid Spencer5b4413c2004-05-26 21:48:31 +0000796// setTypeName - Set the specified type to the name given. The name may be
797// null potentially, in which case this is a noop. The string passed in is
798// assumed to be a malloc'd string buffer, and is freed by this function.
799//
800// This function returns true if the type has already been defined, but is
801// allowed to be redefined in the specified context. If the name is a new name
802// for the type plane, it is inserted and false is returned.
Reid Spencer3b208cc2007-05-22 18:52:21 +0000803static bool setTypeName(const Type *T, std::string *NameStr) {
Reid Spencer9894d6c2007-02-05 17:01:20 +0000804 assert(!inFunctionScope() && "Can't give types function-local names!");
Reid Spencer5b4413c2004-05-26 21:48:31 +0000805 if (NameStr == 0) return false;
Misha Brukman8b477072005-05-10 22:02:28 +0000806
Reid Spencer3b208cc2007-05-22 18:52:21 +0000807 std::string Name(*NameStr); // Copy string
808 delete NameStr; // Free old string
Reid Spencer5b4413c2004-05-26 21:48:31 +0000809
810 // We don't allow assigning names to void type
Reid Spencer309080a2006-09-28 19:28:24 +0000811 if (T == Type::VoidTy) {
Reid Spencerde1d05f2007-02-05 10:16:10 +0000812 GenerateError("Can't assign name '" + Name + "' to the void type");
Reid Spencer309080a2006-09-28 19:28:24 +0000813 return false;
814 }
Reid Spencer5b4413c2004-05-26 21:48:31 +0000815
Chris Lattner8abe1a12004-07-14 23:03:46 +0000816 // Set the type name, checking for conflicts as we do so.
817 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
Reid Spencer5b4413c2004-05-26 21:48:31 +0000818
Chris Lattner8abe1a12004-07-14 23:03:46 +0000819 if (AlreadyExists) { // Inserting a name that is already defined???
820 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
Reid Spencer9894d6c2007-02-05 17:01:20 +0000821 assert(Existing && "Conflict but no matching type?!");
Chris Lattner8abe1a12004-07-14 23:03:46 +0000822
Reid Spencer5b4413c2004-05-26 21:48:31 +0000823 // There is only one case where this is allowed: when we are refining an
824 // opaque type. In this case, Existing will be an opaque type.
825 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
826 // We ARE replacing an opaque type!
Chris Lattner85a351f2004-07-13 08:10:10 +0000827 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
Reid Spencer5b4413c2004-05-26 21:48:31 +0000828 return true;
829 }
830
831 // Otherwise, this is an attempt to redefine a type. That's okay if
832 // the redefinition is identical to the original. This will be so if
833 // Existing and T point to the same Type object. In this one case we
834 // allow the equivalent redefinition.
835 if (Existing == T) return true; // Yes, it's equal.
836
837 // Any other kind of (non-equivalent) redefinition is an error.
Reid Spencer6c435f02007-01-05 21:50:38 +0000838 GenerateError("Redefinition of type named '" + Name + "' of type '" +
Reid Spencerde1d05f2007-02-05 10:16:10 +0000839 T->getDescription() + "'");
Reid Spencer5b4413c2004-05-26 21:48:31 +0000840 }
841
Reid Spencer5b4413c2004-05-26 21:48:31 +0000842 return false;
843}
Chris Lattnerd9c40e32001-07-09 19:38:36 +0000844
Chris Lattneraa534bf2001-09-07 16:35:17 +0000845//===----------------------------------------------------------------------===//
846// Code for handling upreferences in type names...
Chris Lattnerd9c40e32001-07-09 19:38:36 +0000847//
Chris Lattnerd9c40e32001-07-09 19:38:36 +0000848
Chris Lattner3cb41672004-02-09 03:19:29 +0000849// TypeContains - Returns true if Ty directly contains E in it.
Chris Lattneraa534bf2001-09-07 16:35:17 +0000850//
851static bool TypeContains(const Type *Ty, const Type *E) {
Misha Brukman8b477072005-05-10 22:02:28 +0000852 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
Chris Lattner4270bcb2004-12-08 16:13:53 +0000853 E) != Ty->subtype_end();
Chris Lattner3cb41672004-02-09 03:19:29 +0000854}
855
856namespace {
857 struct UpRefRecord {
858 // NestingLevel - The number of nesting levels that need to be popped before
859 // this type is resolved.
860 unsigned NestingLevel;
Misha Brukman8b477072005-05-10 22:02:28 +0000861
Chris Lattner3cb41672004-02-09 03:19:29 +0000862 // LastContainedTy - This is the type at the current binding level for the
863 // type. Every time we reduce the nesting level, this gets updated.
864 const Type *LastContainedTy;
865
866 // UpRefTy - This is the actual opaque type that the upreference is
867 // represented with.
868 OpaqueType *UpRefTy;
869
870 UpRefRecord(unsigned NL, OpaqueType *URTy)
871 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
872 };
Chris Lattneraa534bf2001-09-07 16:35:17 +0000873}
Chris Lattner1caf0bb2001-07-20 19:15:08 +0000874
Chris Lattnerfd2a5142003-12-31 02:18:11 +0000875// UpRefs - A list of the outstanding upreferences that need to be resolved.
Chris Lattner3cb41672004-02-09 03:19:29 +0000876static std::vector<UpRefRecord> UpRefs;
Chris Lattneraa534bf2001-09-07 16:35:17 +0000877
Chris Lattnerfd2a5142003-12-31 02:18:11 +0000878/// HandleUpRefs - Every time we finish a new layer of types, this function is
879/// called. It loops through the UpRefs vector, which is a list of the
880/// currently active types. For each type, if the up reference is contained in
881/// the newly completed type, we decrement the level count. When the level
882/// count reaches zero, the upreferenced type is the type that is passed in:
883/// thus we can complete the cycle.
884///
Chris Lattner30752bd92002-04-04 19:23:55 +0000885static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner682e17c2006-08-18 17:34:24 +0000886 // If Ty isn't abstract, or if there are no up-references in it, then there is
887 // nothing to resolve here.
888 if (!ty->isAbstract() || UpRefs.empty()) return ty;
889
Chris Lattner30752bd92002-04-04 19:23:55 +0000890 PATypeHolder Ty(ty);
Misha Brukman8b477072005-05-10 22:02:28 +0000891 UR_OUT("Type '" << Ty->getDescription() <<
Chris Lattnerf29b2312001-11-02 07:46:26 +0000892 "' newly formed. Resolving upreferences.\n" <<
893 UpRefs.size() << " upreferences active!\n");
Chris Lattneredd45002004-02-09 18:53:54 +0000894
895 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
896 // to zero), we resolve them all together before we resolve them to Ty. At
897 // the end of the loop, if there is anything to resolve to Ty, it will be in
898 // this variable.
899 OpaqueType *TypeToResolve = 0;
900
Chris Lattnerfd2a5142003-12-31 02:18:11 +0000901 for (unsigned i = 0; i != UpRefs.size(); ++i) {
Misha Brukman8b477072005-05-10 22:02:28 +0000902 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
903 << UpRefs[i].second->getDescription() << ") = "
Reid Spencerce6adaf2004-07-18 00:08:11 +0000904 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
Chris Lattner3cb41672004-02-09 03:19:29 +0000905 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
906 // Decrement level of upreference
907 unsigned Level = --UpRefs[i].NestingLevel;
908 UpRefs[i].LastContainedTy = Ty;
Chris Lattnerfd2a5142003-12-31 02:18:11 +0000909 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
Misha Brukman8b477072005-05-10 22:02:28 +0000910 if (Level == 0) { // Upreference should be resolved!
Chris Lattneredd45002004-02-09 18:53:54 +0000911 if (!TypeToResolve) {
912 TypeToResolve = UpRefs[i].UpRefTy;
913 } else {
914 UR_OUT(" * Resolving upreference for "
915 << UpRefs[i].second->getDescription() << "\n";
916 std::string OldName = UpRefs[i].UpRefTy->getDescription());
917 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
918 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
919 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
920 }
Reid Spencerce6adaf2004-07-18 00:08:11 +0000921 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
Chris Lattnerfd2a5142003-12-31 02:18:11 +0000922 --i; // Do not skip the next element...
Chris Lattneraa534bf2001-09-07 16:35:17 +0000923 }
924 }
Chris Lattnerd9c40e32001-07-09 19:38:36 +0000925 }
Chris Lattnerfd2a5142003-12-31 02:18:11 +0000926
Chris Lattneredd45002004-02-09 18:53:54 +0000927 if (TypeToResolve) {
928 UR_OUT(" * Resolving upreference for "
929 << UpRefs[i].second->getDescription() << "\n";
Chris Lattner7289e622004-02-09 21:03:38 +0000930 std::string OldName = TypeToResolve->getDescription());
Chris Lattneredd45002004-02-09 18:53:54 +0000931 TypeToResolve->refineAbstractTypeTo(Ty);
932 }
933
Chris Lattnerd9c40e32001-07-09 19:38:36 +0000934 return Ty;
935}
936
Chris Lattner2f7c9632001-06-06 20:29:01 +0000937//===----------------------------------------------------------------------===//
938// RunVMAsmParser - Define an interface to this parser
939//===----------------------------------------------------------------------===//
940//
Reid Spencerfa35bb32006-12-31 05:40:12 +0000941static Module* RunParser(Module * M);
942
Chris Lattner88357932004-07-14 06:39:48 +0000943Module *llvm::RunVMAsmParser(const std::string &Filename, FILE *F) {
Chris Lattner416a0d42005-05-20 03:25:47 +0000944 set_scan_file(F);
945
Chris Lattnerf2d1e792001-07-22 18:36:00 +0000946 CurFilename = Filename;
Chris Lattner416a0d42005-05-20 03:25:47 +0000947 return RunParser(new Module(CurFilename));
948}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000949
Chris Lattner416a0d42005-05-20 03:25:47 +0000950Module *llvm::RunVMAsmParser(const char * AsmString, Module * M) {
951 set_scan_string(AsmString);
Chris Lattner6789a0b2003-11-26 07:24:58 +0000952
Chris Lattner416a0d42005-05-20 03:25:47 +0000953 CurFilename = "from_memory";
954 if (M == NULL) {
955 return RunParser(new Module (CurFilename));
956 } else {
957 return RunParser(M);
958 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000959}
960
961%}
962
963%union {
Brian Gaeke960707c2003-11-11 22:41:34 +0000964 llvm::Module *ModuleVal;
965 llvm::Function *FunctionVal;
Brian Gaeke960707c2003-11-11 22:41:34 +0000966 llvm::BasicBlock *BasicBlockVal;
967 llvm::TerminatorInst *TermInstVal;
968 llvm::Instruction *InstVal;
Reid Spencer812724e2006-12-03 05:45:44 +0000969 llvm::Constant *ConstVal;
Chris Lattneraa534bf2001-09-07 16:35:17 +0000970
Reid Spencer812724e2006-12-03 05:45:44 +0000971 const llvm::Type *PrimType;
Reid Spencerfa35bb32006-12-31 05:40:12 +0000972 std::list<llvm::PATypeHolder> *TypeList;
Reid Spencer812724e2006-12-03 05:45:44 +0000973 llvm::PATypeHolder *TypeVal;
974 llvm::Value *ValueVal;
Reid Spencer812724e2006-12-03 05:45:44 +0000975 std::vector<llvm::Value*> *ValueList;
Reid Spencerfa35bb32006-12-31 05:40:12 +0000976 llvm::ArgListType *ArgList;
977 llvm::TypeWithAttrs TypeWithAttrs;
978 llvm::TypeWithAttrsList *TypeWithAttrsList;
979 llvm::ValueRefList *ValueRefList;
980
Misha Brukman8b477072005-05-10 22:02:28 +0000981 // Represent the RHS of PHI node
Reid Spencer812724e2006-12-03 05:45:44 +0000982 std::list<std::pair<llvm::Value*,
983 llvm::BasicBlock*> > *PHIList;
Brian Gaeke960707c2003-11-11 22:41:34 +0000984 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
Reid Spencer812724e2006-12-03 05:45:44 +0000985 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000986
Brian Gaeke960707c2003-11-11 22:41:34 +0000987 llvm::GlobalValue::LinkageTypes Linkage;
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000988 llvm::GlobalValue::VisibilityTypes Visibility;
Reid Spencer71b79e32007-04-09 06:17:21 +0000989 uint16_t ParamAttrs;
Reid Spencer9875fc12007-02-28 02:23:44 +0000990 llvm::APInt *APIntVal;
Chris Lattneraa534bf2001-09-07 16:35:17 +0000991 int64_t SInt64Val;
992 uint64_t UInt64Val;
993 int SIntVal;
994 unsigned UIntVal;
995 double FPVal;
Chris Lattner7fb14f52001-09-18 04:00:54 +0000996 bool BoolVal;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000997
Reid Spencer3b208cc2007-05-22 18:52:21 +0000998 std::string *StrVal; // This memory must be deleted
999 llvm::ValID ValIDVal;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001000
Reid Spencer812724e2006-12-03 05:45:44 +00001001 llvm::Instruction::BinaryOps BinaryOpVal;
1002 llvm::Instruction::TermOps TermOpVal;
1003 llvm::Instruction::MemoryOps MemOpVal;
1004 llvm::Instruction::CastOps CastOpVal;
1005 llvm::Instruction::OtherOps OtherOpVal;
Reid Spencer812724e2006-12-03 05:45:44 +00001006 llvm::ICmpInst::Predicate IPredicate;
1007 llvm::FCmpInst::Predicate FPredicate;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001008}
1009
Reid Spencerfa35bb32006-12-31 05:40:12 +00001010%type <ModuleVal> Module
Chris Lattner57698e22002-03-26 18:01:55 +00001011%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner2f7c9632001-06-06 20:29:01 +00001012%type <BasicBlockVal> BasicBlock InstructionList
1013%type <TermInstVal> BBTerminatorInst
1014%type <InstVal> Inst InstVal MemoryInst
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +00001015%type <ConstVal> ConstVal ConstExpr AliaseeRef
Chris Lattner476148f2001-11-26 16:54:11 +00001016%type <ConstVector> ConstVector
Chris Lattnerae234e12002-04-09 19:41:42 +00001017%type <ArgList> ArgList ArgListH
Chris Lattner931ef3b2001-06-11 15:04:20 +00001018%type <PHIList> PHIList
Reid Spencerfa35bb32006-12-31 05:40:12 +00001019%type <ValueRefList> ValueRefList // For call param lists & GEP indices
1020%type <ValueList> IndexList // For GEP indices
1021%type <TypeList> TypeListI
1022%type <TypeWithAttrsList> ArgTypeList ArgTypeListI
Reid Spencer136a91c2007-01-05 17:06:19 +00001023%type <TypeWithAttrs> ArgType
Chris Lattner2f7c9632001-06-06 20:29:01 +00001024%type <JumpTable> JumpTable
Chris Lattner379a8d22003-04-16 20:28:45 +00001025%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +00001026%type <BoolVal> ThreadLocal // 'thread_local' or not
Chris Lattner8b1680e2003-09-08 18:20:29 +00001027%type <BoolVal> OptVolatile // 'volatile' or not
Chris Lattner06038452005-05-06 05:51:46 +00001028%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
Chris Lattnera02d6032006-01-25 22:26:43 +00001029%type <BoolVal> OptSideEffect // 'sideeffect' or not.
Reid Spencerfa35bb32006-12-31 05:40:12 +00001030%type <Linkage> GVInternalLinkage GVExternalLinkage
1031%type <Linkage> FunctionDefineLinkage FunctionDeclareLinkage
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001032%type <Linkage> AliasLinkage
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001033%type <Visibility> GVVisibilityStyle
Chris Lattner2f7c9632001-06-06 20:29:01 +00001034
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001035// ValueRef - Unresolved reference to a definition or BB
1036%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattner252afba2001-07-26 16:29:15 +00001037%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner2f7c9632001-06-06 20:29:01 +00001038// Tokens and types for handling constant integer values
1039//
1040// ESINT64VAL - A negative number within long long range
1041%token <SInt64Val> ESINT64VAL
1042
1043// EUINT64VAL - A positive number within uns. long long range
1044%token <UInt64Val> EUINT64VAL
Chris Lattner2f7c9632001-06-06 20:29:01 +00001045
Reid Spencer9875fc12007-02-28 02:23:44 +00001046// ESAPINTVAL - A negative number with arbitrary precision
1047%token <APIntVal> ESAPINTVAL
1048
1049// EUAPINTVAL - A positive number with arbitrary precision
1050%token <APIntVal> EUAPINTVAL
1051
Reid Spencer791b8ef2007-01-26 08:04:51 +00001052%token <UIntVal> LOCALVAL_ID GLOBALVAL_ID // %123 @123
Chris Lattner212f70d2001-07-15 00:17:01 +00001053%token <FPVal> FPVAL // Float or Double constant
Chris Lattner2f7c9632001-06-06 20:29:01 +00001054
1055// Built in types...
Reid Spencer136a91c2007-01-05 17:06:19 +00001056%type <TypeVal> Types ResultTypes
Reid Spencerfa35bb32006-12-31 05:40:12 +00001057%type <PrimType> IntType FPType PrimType // Classifications
Reid Spencer502d64e2007-01-13 05:00:20 +00001058%token <PrimType> VOID INTTYPE
Dale Johannesenff4c3be2007-08-03 01:03:46 +00001059%token <PrimType> FLOAT DOUBLE X86_FP80 FP128 PPC_FP128 LABEL
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00001060%token TYPE
Chris Lattner2f7c9632001-06-06 20:29:01 +00001061
Reid Spencer3b208cc2007-05-22 18:52:21 +00001062
Reid Spenceraba0cc72007-05-19 07:21:26 +00001063%token<StrVal> LOCALVAR GLOBALVAR LABELSTR
1064%token<StrVal> STRINGCONSTANT ATSTRINGCONSTANT PCTSTRINGCONSTANT
Reid Spencer791b8ef2007-01-26 08:04:51 +00001065%type <StrVal> LocalName OptLocalName OptLocalAssign
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001066%type <StrVal> GlobalName OptGlobalAssign GlobalAssign
Chris Lattnerbce7ca8a2005-11-12 18:21:21 +00001067%type <StrVal> OptSection SectionString
Chris Lattner2f7c9632001-06-06 20:29:01 +00001068
Reid Spencer3b208cc2007-05-22 18:52:21 +00001069%type <UIntVal> OptAlign OptCAlign
1070
Reid Spencerbef90fe2007-04-09 01:55:42 +00001071%token ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001072%token DECLARE DEFINE GLOBAL CONSTANT SECTION ALIAS VOLATILE THREAD_LOCAL
Reid Spencerfa35bb32006-12-31 05:40:12 +00001073%token TO DOTDOTDOT NULL_TOK UNDEF INTERNAL LINKONCE WEAK APPENDING
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00001074%token DLLIMPORT DLLEXPORT EXTERN_WEAK
Reid Spencer791b8ef2007-01-26 08:04:51 +00001075%token OPAQUE EXTERNAL TARGET TRIPLE ALIGN
Chris Lattnera02d6032006-01-25 22:26:43 +00001076%token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
Anton Korobeynikov037c8672007-01-28 13:31:35 +00001077%token CC_TOK CCC_TOK FASTCC_TOK COLDCC_TOK X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
Chris Lattnerfd3d29a2006-10-22 06:07:41 +00001078%token DATALAYOUT
Chris Lattner53bdd312005-05-06 20:27:19 +00001079%type <UIntVal> OptCallingConv
Reid Spencer136a91c2007-01-05 17:06:19 +00001080%type <ParamAttrs> OptParamAttrs ParamAttr
1081%type <ParamAttrs> OptFuncAttrs FuncAttr
Chris Lattner2f7c9632001-06-06 20:29:01 +00001082
Misha Brukman8b477072005-05-10 22:02:28 +00001083// Basic Block Terminating Operators
Chris Lattner4ff31492004-10-16 18:17:13 +00001084%token <TermOpVal> RET BR SWITCH INVOKE UNWIND UNREACHABLE
Chris Lattner2f7c9632001-06-06 20:29:01 +00001085
Misha Brukman8b477072005-05-10 22:02:28 +00001086// Binary Operators
Reid Spencer266e42b2006-12-23 06:05:41 +00001087%type <BinaryOpVal> ArithmeticOps LogicalOps // Binops Subcatagories
Reid Spencer7eb55b32006-11-02 01:53:59 +00001088%token <BinaryOpVal> ADD SUB MUL UDIV SDIV FDIV UREM SREM FREM AND OR XOR
Reid Spencer2341c222007-02-02 02:16:23 +00001089%token <BinaryOpVal> SHL LSHR ASHR
1090
Reid Spencer812724e2006-12-03 05:45:44 +00001091%token <OtherOpVal> ICMP FCMP
Reid Spencer812724e2006-12-03 05:45:44 +00001092%type <IPredicate> IPredicates
Reid Spencer812724e2006-12-03 05:45:44 +00001093%type <FPredicate> FPredicates
Reid Spencercfc7f5d2006-12-03 06:58:07 +00001094%token EQ NE SLT SGT SLE SGE ULT UGT ULE UGE
1095%token OEQ ONE OLT OGT OLE OGE ORD UNO UEQ UNE
Chris Lattner2f7c9632001-06-06 20:29:01 +00001096
1097// Memory Instructions
Vikram S. Adve7064eaf2002-07-14 22:59:28 +00001098%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner2f7c9632001-06-06 20:29:01 +00001099
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001100// Cast Operators
1101%type <CastOpVal> CastOps
1102%token <CastOpVal> TRUNC ZEXT SEXT FPTRUNC FPEXT BITCAST
1103%token <CastOpVal> UITOFP SITOFP FPTOUI FPTOSI INTTOPTR PTRTOINT
1104
Chris Lattnerd8bebcd2001-07-08 21:10:27 +00001105// Other Operators
Reid Spencer2341c222007-02-02 02:16:23 +00001106%token <OtherOpVal> PHI_TOK SELECT VAARG
Chris Lattner8ec3c2e2006-04-08 01:18:35 +00001107%token <OtherOpVal> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
Chris Lattner06038452005-05-06 05:51:46 +00001108
Reid Spencer136a91c2007-01-05 17:06:19 +00001109// Function Attributes
Duncan Sands644f9172007-07-27 12:58:54 +00001110%token SIGNEXT ZEROEXT NORETURN INREG SRET NOUNWIND NOALIAS BYVAL NEST
Chris Lattnerd8bebcd2001-07-08 21:10:27 +00001111
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001112// Visibility Styles
Anton Korobeynikov39f3cff2007-04-29 18:35:00 +00001113%token DEFAULT HIDDEN PROTECTED
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001114
Chris Lattner2f7c9632001-06-06 20:29:01 +00001115%start Module
1116%%
1117
Chris Lattner2f7c9632001-06-06 20:29:01 +00001118
Misha Brukman8b477072005-05-10 22:02:28 +00001119// Operations that are notably excluded from this list include:
Chris Lattner2f7c9632001-06-06 20:29:01 +00001120// RET, BR, & SWITCH because they end basic blocks and are treated specially.
1121//
Reid Spencer7eb55b32006-11-02 01:53:59 +00001122ArithmeticOps: ADD | SUB | MUL | UDIV | SDIV | FDIV | UREM | SREM | FREM;
Reid Spencer2341c222007-02-02 02:16:23 +00001123LogicalOps : SHL | LSHR | ASHR | AND | OR | XOR;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001124CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | BITCAST |
1125 UITOFP | SITOFP | FPTOUI | FPTOSI | INTTOPTR | PTRTOINT;
Reid Spencer2341c222007-02-02 02:16:23 +00001126
Reid Spencercfc7f5d2006-12-03 06:58:07 +00001127IPredicates
Reid Spencerf3490692006-12-04 05:20:06 +00001128 : EQ { $$ = ICmpInst::ICMP_EQ; } | NE { $$ = ICmpInst::ICMP_NE; }
Reid Spencercfc7f5d2006-12-03 06:58:07 +00001129 | SLT { $$ = ICmpInst::ICMP_SLT; } | SGT { $$ = ICmpInst::ICMP_SGT; }
1130 | SLE { $$ = ICmpInst::ICMP_SLE; } | SGE { $$ = ICmpInst::ICMP_SGE; }
1131 | ULT { $$ = ICmpInst::ICMP_ULT; } | UGT { $$ = ICmpInst::ICMP_UGT; }
1132 | ULE { $$ = ICmpInst::ICMP_ULE; } | UGE { $$ = ICmpInst::ICMP_UGE; }
1133 ;
1134
1135FPredicates
1136 : OEQ { $$ = FCmpInst::FCMP_OEQ; } | ONE { $$ = FCmpInst::FCMP_ONE; }
1137 | OLT { $$ = FCmpInst::FCMP_OLT; } | OGT { $$ = FCmpInst::FCMP_OGT; }
1138 | OLE { $$ = FCmpInst::FCMP_OLE; } | OGE { $$ = FCmpInst::FCMP_OGE; }
1139 | ORD { $$ = FCmpInst::FCMP_ORD; } | UNO { $$ = FCmpInst::FCMP_UNO; }
1140 | UEQ { $$ = FCmpInst::FCMP_UEQ; } | UNE { $$ = FCmpInst::FCMP_UNE; }
1141 | ULT { $$ = FCmpInst::FCMP_ULT; } | UGT { $$ = FCmpInst::FCMP_UGT; }
1142 | ULE { $$ = FCmpInst::FCMP_ULE; } | UGE { $$ = FCmpInst::FCMP_UGE; }
1143 | TRUETOK { $$ = FCmpInst::FCMP_TRUE; }
1144 | FALSETOK { $$ = FCmpInst::FCMP_FALSE; }
1145 ;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001146
Chris Lattner56f73d42001-07-14 06:10:16 +00001147// These are some types that allow classification if we only want a particular
1148// thing... for example, only a signed, unsigned, or integral type.
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001149IntType : INTTYPE;
Dale Johannesenff4c3be2007-08-03 01:03:46 +00001150FPType : FLOAT | DOUBLE | PPC_FP128 | FP128 | X86_FP80;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001151
Reid Spencer3b208cc2007-05-22 18:52:21 +00001152LocalName : LOCALVAR | STRINGCONSTANT | PCTSTRINGCONSTANT ;
Reid Spencer791b8ef2007-01-26 08:04:51 +00001153OptLocalName : LocalName | /*empty*/ { $$ = 0; };
1154
1155/// OptLocalAssign - Value producing statements have an optional assignment
1156/// component.
1157OptLocalAssign : LocalName '=' {
1158 $$ = $1;
1159 CHECK_FOR_ERROR
1160 }
1161 | /*empty*/ {
1162 $$ = 0;
1163 CHECK_FOR_ERROR
1164 };
1165
Reid Spencer3b208cc2007-05-22 18:52:21 +00001166GlobalName : GLOBALVAR | ATSTRINGCONSTANT ;
Reid Spencer791b8ef2007-01-26 08:04:51 +00001167
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001168OptGlobalAssign : GlobalAssign
Misha Brukman8b477072005-05-10 22:02:28 +00001169 | /*empty*/ {
1170 $$ = 0;
Reid Spencer713eedc2006-08-18 08:43:06 +00001171 CHECK_FOR_ERROR
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001172 };
Chris Lattner2f7c9632001-06-06 20:29:01 +00001173
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001174GlobalAssign : GlobalName '=' {
1175 $$ = $1;
1176 CHECK_FOR_ERROR
Anton Korobeynikov5e4e8f52007-04-25 18:07:40 +00001177 };
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001178
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00001179GVInternalLinkage
1180 : INTERNAL { $$ = GlobalValue::InternalLinkage; }
1181 | WEAK { $$ = GlobalValue::WeakLinkage; }
1182 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1183 | APPENDING { $$ = GlobalValue::AppendingLinkage; }
1184 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
1185 ;
1186
1187GVExternalLinkage
1188 : DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1189 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
1190 | EXTERNAL { $$ = GlobalValue::ExternalLinkage; }
1191 ;
1192
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001193GVVisibilityStyle
Anton Korobeynikov39f3cff2007-04-29 18:35:00 +00001194 : /*empty*/ { $$ = GlobalValue::DefaultVisibility; }
1195 | DEFAULT { $$ = GlobalValue::DefaultVisibility; }
1196 | HIDDEN { $$ = GlobalValue::HiddenVisibility; }
1197 | PROTECTED { $$ = GlobalValue::ProtectedVisibility; }
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001198 ;
1199
Reid Spencerfa35bb32006-12-31 05:40:12 +00001200FunctionDeclareLinkage
1201 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1202 | DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1203 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00001204 ;
1205
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001206FunctionDefineLinkage
Reid Spencerfa35bb32006-12-31 05:40:12 +00001207 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1208 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00001209 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1210 | WEAK { $$ = GlobalValue::WeakLinkage; }
1211 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00001212 ;
Chris Lattneraa534bf2001-09-07 16:35:17 +00001213
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001214AliasLinkage
1215 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1216 | WEAK { $$ = GlobalValue::WeakLinkage; }
1217 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
1218 ;
1219
Anton Korobeynikov6f7072c2006-09-17 20:25:45 +00001220OptCallingConv : /*empty*/ { $$ = CallingConv::C; } |
1221 CCC_TOK { $$ = CallingConv::C; } |
Anton Korobeynikov6f7072c2006-09-17 20:25:45 +00001222 FASTCC_TOK { $$ = CallingConv::Fast; } |
1223 COLDCC_TOK { $$ = CallingConv::Cold; } |
1224 X86_STDCALLCC_TOK { $$ = CallingConv::X86_StdCall; } |
1225 X86_FASTCALLCC_TOK { $$ = CallingConv::X86_FastCall; } |
1226 CC_TOK EUINT64VAL {
Chris Lattner53bdd312005-05-06 20:27:19 +00001227 if ((unsigned)$2 != $2)
Reid Spencerde1d05f2007-02-05 10:16:10 +00001228 GEN_ERROR("Calling conv too large");
Chris Lattner53bdd312005-05-06 20:27:19 +00001229 $$ = $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00001230 CHECK_FOR_ERROR
Chris Lattner53bdd312005-05-06 20:27:19 +00001231 };
1232
Reid Spencer314e1cb2007-07-19 23:13:04 +00001233ParamAttr : ZEROEXT { $$ = ParamAttr::ZExt; }
Reid Spencerd45d07a2007-07-31 02:57:37 +00001234 | ZEXT { $$ = ParamAttr::ZExt; }
Reid Spencer314e1cb2007-07-19 23:13:04 +00001235 | SIGNEXT { $$ = ParamAttr::SExt; }
Reid Spencerd45d07a2007-07-31 02:57:37 +00001236 | SEXT { $$ = ParamAttr::SExt; }
Zhou Sheng2444a9a2007-06-05 05:28:26 +00001237 | INREG { $$ = ParamAttr::InReg; }
1238 | SRET { $$ = ParamAttr::StructRet; }
1239 | NOALIAS { $$ = ParamAttr::NoAlias; }
Duncan Sands644f9172007-07-27 12:58:54 +00001240 | BYVAL { $$ = ParamAttr::ByVal; }
1241 | NEST { $$ = ParamAttr::Nest; }
Reid Spencerfa35bb32006-12-31 05:40:12 +00001242 ;
1243
Reid Spencera472f662007-04-11 02:44:20 +00001244OptParamAttrs : /* empty */ { $$ = ParamAttr::None; }
Reid Spencer136a91c2007-01-05 17:06:19 +00001245 | OptParamAttrs ParamAttr {
Reid Spencer71b79e32007-04-09 06:17:21 +00001246 $$ = $1 | $2;
Reid Spencerfa35bb32006-12-31 05:40:12 +00001247 }
1248 ;
1249
Reid Spencera472f662007-04-11 02:44:20 +00001250FuncAttr : NORETURN { $$ = ParamAttr::NoReturn; }
1251 | NOUNWIND { $$ = ParamAttr::NoUnwind; }
Reid Spencer314e1cb2007-07-19 23:13:04 +00001252 | ZEROEXT { $$ = ParamAttr::ZExt; }
1253 | SIGNEXT { $$ = ParamAttr::SExt; }
Reid Spencer136a91c2007-01-05 17:06:19 +00001254 ;
1255
Reid Spencera472f662007-04-11 02:44:20 +00001256OptFuncAttrs : /* empty */ { $$ = ParamAttr::None; }
Reid Spencer136a91c2007-01-05 17:06:19 +00001257 | OptFuncAttrs FuncAttr {
Reid Spencer71b79e32007-04-09 06:17:21 +00001258 $$ = $1 | $2;
Reid Spencer136a91c2007-01-05 17:06:19 +00001259 }
Reid Spencerfa35bb32006-12-31 05:40:12 +00001260 ;
1261
Chris Lattnerc7de8362005-11-06 06:34:12 +00001262// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1263// a comma before it.
Chris Lattnerd57ed892005-11-06 06:46:28 +00001264OptAlign : /*empty*/ { $$ = 0; } |
Chris Lattnerbce7ca8a2005-11-12 18:21:21 +00001265 ALIGN EUINT64VAL {
1266 $$ = $2;
1267 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencerde1d05f2007-02-05 10:16:10 +00001268 GEN_ERROR("Alignment must be a power of two");
Reid Spencer713eedc2006-08-18 08:43:06 +00001269 CHECK_FOR_ERROR
Chris Lattnerbce7ca8a2005-11-12 18:21:21 +00001270};
Chris Lattnerc7de8362005-11-06 06:34:12 +00001271OptCAlign : /*empty*/ { $$ = 0; } |
Chris Lattnerbce7ca8a2005-11-12 18:21:21 +00001272 ',' ALIGN EUINT64VAL {
1273 $$ = $3;
1274 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencerde1d05f2007-02-05 10:16:10 +00001275 GEN_ERROR("Alignment must be a power of two");
Reid Spencer713eedc2006-08-18 08:43:06 +00001276 CHECK_FOR_ERROR
Chris Lattnerbce7ca8a2005-11-12 18:21:21 +00001277};
1278
Chris Lattnerc7de8362005-11-06 06:34:12 +00001279
Chris Lattner71b936c2005-11-12 00:11:10 +00001280SectionString : SECTION STRINGCONSTANT {
Reid Spencer3b208cc2007-05-22 18:52:21 +00001281 for (unsigned i = 0, e = $2->length(); i != e; ++i)
1282 if ((*$2)[i] == '"' || (*$2)[i] == '\\')
Reid Spencerde1d05f2007-02-05 10:16:10 +00001283 GEN_ERROR("Invalid character in section name");
Chris Lattner71b936c2005-11-12 00:11:10 +00001284 $$ = $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00001285 CHECK_FOR_ERROR
Chris Lattner71b936c2005-11-12 00:11:10 +00001286};
1287
1288OptSection : /*empty*/ { $$ = 0; } |
1289 SectionString { $$ = $1; };
Chris Lattner71b936c2005-11-12 00:11:10 +00001290
Chris Lattnerbce7ca8a2005-11-12 18:21:21 +00001291// GlobalVarAttributes - Used to pass the attributes string on a global. CurGV
1292// is set to be the global we are processing.
1293//
1294GlobalVarAttributes : /* empty */ {} |
1295 ',' GlobalVarAttribute GlobalVarAttributes {};
1296GlobalVarAttribute : SectionString {
Reid Spencer3b208cc2007-05-22 18:52:21 +00001297 CurGV->setSection(*$1);
1298 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001299 CHECK_FOR_ERROR
Chris Lattnerbce7ca8a2005-11-12 18:21:21 +00001300 }
1301 | ALIGN EUINT64VAL {
1302 if ($2 != 0 && !isPowerOf2_32($2))
Reid Spencerde1d05f2007-02-05 10:16:10 +00001303 GEN_ERROR("Alignment must be a power of two");
Chris Lattnerbce7ca8a2005-11-12 18:21:21 +00001304 CurGV->setAlignment($2);
Reid Spencer713eedc2006-08-18 08:43:06 +00001305 CHECK_FOR_ERROR
Chris Lattnerbce7ca8a2005-11-12 18:21:21 +00001306 };
Chris Lattner71b936c2005-11-12 00:11:10 +00001307
Chris Lattneraa534bf2001-09-07 16:35:17 +00001308//===----------------------------------------------------------------------===//
1309// Types includes all predefined types... except void, because it can only be
Reid Spencerfa35bb32006-12-31 05:40:12 +00001310// used in specific contexts (function returning void for example).
Chris Lattneraa534bf2001-09-07 16:35:17 +00001311
1312// Derived types are added later...
1313//
Dale Johannesenff4c3be2007-08-03 01:03:46 +00001314PrimType : INTTYPE | FLOAT | DOUBLE | PPC_FP128 | FP128 | X86_FP80 | LABEL ;
Reid Spencerfa35bb32006-12-31 05:40:12 +00001315
1316Types
1317 : OPAQUE {
Reid Spencer812724e2006-12-03 05:45:44 +00001318 $$ = new PATypeHolder(OpaqueType::get());
Reid Spencer713eedc2006-08-18 08:43:06 +00001319 CHECK_FOR_ERROR
Chris Lattner30752bd92002-04-04 19:23:55 +00001320 }
1321 | PrimType {
Reid Spencer812724e2006-12-03 05:45:44 +00001322 $$ = new PATypeHolder($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00001323 CHECK_FOR_ERROR
Reid Spencerfa35bb32006-12-31 05:40:12 +00001324 }
1325 | Types '*' { // Pointer type?
1326 if (*$1 == Type::LabelTy)
1327 GEN_ERROR("Cannot form a pointer to a basic block");
1328 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
1329 delete $1;
1330 CHECK_FOR_ERROR
1331 }
1332 | SymbolicValueRef { // Named types are also simple types...
1333 const Type* tmp = getTypeVal($1);
1334 CHECK_FOR_ERROR
1335 $$ = new PATypeHolder(tmp);
1336 }
1337 | '\\' EUINT64VAL { // Type UpReference
Reid Spencerde1d05f2007-02-05 10:16:10 +00001338 if ($2 > (uint64_t)~0U) GEN_ERROR("Value out of range");
Chris Lattneraa534bf2001-09-07 16:35:17 +00001339 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
Chris Lattner3cb41672004-02-09 03:19:29 +00001340 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Reid Spencer812724e2006-12-03 05:45:44 +00001341 $$ = new PATypeHolder(OT);
Chris Lattneraa534bf2001-09-07 16:35:17 +00001342 UR_OUT("New Upreference!\n");
Reid Spencer713eedc2006-08-18 08:43:06 +00001343 CHECK_FOR_ERROR
Chris Lattneraa534bf2001-09-07 16:35:17 +00001344 }
Reid Spencer78517652007-04-28 16:06:50 +00001345 | Types '(' ArgTypeListI ')' OptFuncAttrs {
Chris Lattner89da8a32003-04-22 18:42:41 +00001346 std::vector<const Type*> Params;
Reid Spencer78517652007-04-28 16:06:50 +00001347 ParamAttrsVector Attrs;
1348 if ($5 != ParamAttr::None) {
1349 ParamAttrsWithIndex X; X.index = 0; X.attrs = $5;
1350 Attrs.push_back(X);
1351 }
Reid Spencer71b79e32007-04-09 06:17:21 +00001352 unsigned index = 1;
1353 TypeWithAttrsList::iterator I = $3->begin(), E = $3->end();
1354 for (; I != E; ++I, ++index) {
Reid Spencerd05bc522007-03-20 01:13:00 +00001355 const Type *Ty = I->Ty->get();
Reid Spencerd05bc522007-03-20 01:13:00 +00001356 Params.push_back(Ty);
Reid Spencer78517652007-04-28 16:06:50 +00001357 if (Ty != Type::VoidTy)
1358 if (I->Attrs != ParamAttr::None) {
1359 ParamAttrsWithIndex X; X.index = index; X.attrs = I->Attrs;
1360 Attrs.push_back(X);
1361 }
Reid Spencerfa35bb32006-12-31 05:40:12 +00001362 }
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001363 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1364 if (isVarArg) Params.pop_back();
1365
Reid Spencer78517652007-04-28 16:06:50 +00001366 ParamAttrsList *ActualAttrs = 0;
1367 if (!Attrs.empty())
1368 ActualAttrs = ParamAttrsList::get(Attrs);
1369 FunctionType *FT = FunctionType::get(*$1, Params, isVarArg, ActualAttrs);
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001370 delete $3; // Delete the argument list
Reid Spencerfa35bb32006-12-31 05:40:12 +00001371 delete $1; // Delete the return type handle
1372 $$ = new PATypeHolder(HandleUpRefs(FT));
Reid Spencer713eedc2006-08-18 08:43:06 +00001373 CHECK_FOR_ERROR
Chris Lattneraa534bf2001-09-07 16:35:17 +00001374 }
Reid Spencer78517652007-04-28 16:06:50 +00001375 | VOID '(' ArgTypeListI ')' OptFuncAttrs {
Reid Spencerfa35bb32006-12-31 05:40:12 +00001376 std::vector<const Type*> Params;
Reid Spencer78517652007-04-28 16:06:50 +00001377 ParamAttrsVector Attrs;
1378 if ($5 != ParamAttr::None) {
1379 ParamAttrsWithIndex X; X.index = 0; X.attrs = $5;
1380 Attrs.push_back(X);
1381 }
Reid Spencer71b79e32007-04-09 06:17:21 +00001382 TypeWithAttrsList::iterator I = $3->begin(), E = $3->end();
1383 unsigned index = 1;
1384 for ( ; I != E; ++I, ++index) {
Reid Spencerd05bc522007-03-20 01:13:00 +00001385 const Type* Ty = I->Ty->get();
Reid Spencerd05bc522007-03-20 01:13:00 +00001386 Params.push_back(Ty);
Reid Spencer78517652007-04-28 16:06:50 +00001387 if (Ty != Type::VoidTy)
1388 if (I->Attrs != ParamAttr::None) {
1389 ParamAttrsWithIndex X; X.index = index; X.attrs = I->Attrs;
1390 Attrs.push_back(X);
1391 }
Reid Spencerfa35bb32006-12-31 05:40:12 +00001392 }
1393 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1394 if (isVarArg) Params.pop_back();
1395
Reid Spencer78517652007-04-28 16:06:50 +00001396 ParamAttrsList *ActualAttrs = 0;
1397 if (!Attrs.empty())
1398 ActualAttrs = ParamAttrsList::get(Attrs);
1399
1400 FunctionType *FT = FunctionType::get($1, Params, isVarArg, ActualAttrs);
Reid Spencer136a91c2007-01-05 17:06:19 +00001401 delete $3; // Delete the argument list
Reid Spencerfa35bb32006-12-31 05:40:12 +00001402 $$ = new PATypeHolder(HandleUpRefs(FT));
1403 CHECK_FOR_ERROR
1404 }
1405
1406 | '[' EUINT64VAL 'x' Types ']' { // Sized array type?
Reid Spencer812724e2006-12-03 05:45:44 +00001407 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
1408 delete $4;
Reid Spencer713eedc2006-08-18 08:43:06 +00001409 CHECK_FOR_ERROR
Chris Lattneraa534bf2001-09-07 16:35:17 +00001410 }
Reid Spencer09575ba2007-02-15 03:39:18 +00001411 | '<' EUINT64VAL 'x' Types '>' { // Vector type?
Reid Spencer812724e2006-12-03 05:45:44 +00001412 const llvm::Type* ElemTy = $4->get();
1413 if ((unsigned)$2 != $2)
1414 GEN_ERROR("Unsigned result not equal to signed result");
Chris Lattner03c49532007-01-15 02:27:26 +00001415 if (!ElemTy->isFloatingPoint() && !ElemTy->isInteger())
Reid Spencerd84d35b2007-02-15 02:26:10 +00001416 GEN_ERROR("Element type of a VectorType must be primitive");
Reid Spencer812724e2006-12-03 05:45:44 +00001417 if (!isPowerOf2_32($2))
Reid Spencerde1d05f2007-02-05 10:16:10 +00001418 GEN_ERROR("Vector length should be a power of 2");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001419 $$ = new PATypeHolder(HandleUpRefs(VectorType::get(*$4, (unsigned)$2)));
Reid Spencer812724e2006-12-03 05:45:44 +00001420 delete $4;
1421 CHECK_FOR_ERROR
Brian Gaeke02209042004-08-20 06:00:58 +00001422 }
Chris Lattnere63ddd8df2001-10-03 03:19:33 +00001423 | '{' TypeListI '}' { // Structure type?
Chris Lattner89da8a32003-04-22 18:42:41 +00001424 std::vector<const Type*> Elements;
Reid Spencer812724e2006-12-03 05:45:44 +00001425 for (std::list<llvm::PATypeHolder>::iterator I = $2->begin(),
Chris Lattner06b6c4b2005-02-22 23:13:58 +00001426 E = $2->end(); I != E; ++I)
Reid Spencer812724e2006-12-03 05:45:44 +00001427 Elements.push_back(*I);
Misha Brukman8b477072005-05-10 22:02:28 +00001428
Reid Spencer812724e2006-12-03 05:45:44 +00001429 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnere63ddd8df2001-10-03 03:19:33 +00001430 delete $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00001431 CHECK_FOR_ERROR
Chris Lattnere63ddd8df2001-10-03 03:19:33 +00001432 }
1433 | '{' '}' { // Empty structure type?
Reid Spencer812724e2006-12-03 05:45:44 +00001434 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Reid Spencer713eedc2006-08-18 08:43:06 +00001435 CHECK_FOR_ERROR
Chris Lattnere63ddd8df2001-10-03 03:19:33 +00001436 }
Andrew Lenharthdcb3c972006-12-08 18:06:16 +00001437 | '<' '{' TypeListI '}' '>' {
1438 std::vector<const Type*> Elements;
1439 for (std::list<llvm::PATypeHolder>::iterator I = $3->begin(),
1440 E = $3->end(); I != E; ++I)
1441 Elements.push_back(*I);
1442
1443 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements, true)));
1444 delete $3;
1445 CHECK_FOR_ERROR
1446 }
1447 | '<' '{' '}' '>' { // Empty structure type?
1448 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>(), true));
1449 CHECK_FOR_ERROR
1450 }
Reid Spencerfa35bb32006-12-31 05:40:12 +00001451 ;
1452
1453ArgType
Reid Spencer78517652007-04-28 16:06:50 +00001454 : Types OptParamAttrs {
Reid Spencerfa35bb32006-12-31 05:40:12 +00001455 $$.Ty = $1;
Reid Spencer78517652007-04-28 16:06:50 +00001456 $$.Attrs = $2;
Reid Spencerfa35bb32006-12-31 05:40:12 +00001457 }
1458 ;
1459
Reid Spencer136a91c2007-01-05 17:06:19 +00001460ResultTypes
1461 : Types {
Reid Spencerfa35bb32006-12-31 05:40:12 +00001462 if (!UpRefs.empty())
1463 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1464 if (!(*$1)->isFirstClassType())
Reid Spencerde1d05f2007-02-05 10:16:10 +00001465 GEN_ERROR("LLVM functions cannot return aggregate types");
Reid Spencer136a91c2007-01-05 17:06:19 +00001466 $$ = $1;
Reid Spencerfa35bb32006-12-31 05:40:12 +00001467 }
Reid Spencer136a91c2007-01-05 17:06:19 +00001468 | VOID {
1469 $$ = new PATypeHolder(Type::VoidTy);
Reid Spencerfa35bb32006-12-31 05:40:12 +00001470 }
1471 ;
1472
1473ArgTypeList : ArgType {
1474 $$ = new TypeWithAttrsList();
1475 $$->push_back($1);
1476 CHECK_FOR_ERROR
1477 }
1478 | ArgTypeList ',' ArgType {
1479 ($$=$1)->push_back($3);
1480 CHECK_FOR_ERROR
1481 }
1482 ;
1483
1484ArgTypeListI
1485 : ArgTypeList
1486 | ArgTypeList ',' DOTDOTDOT {
1487 $$=$1;
Reid Spencera472f662007-04-11 02:44:20 +00001488 TypeWithAttrs TWA; TWA.Attrs = ParamAttr::None;
Reid Spencerfa35bb32006-12-31 05:40:12 +00001489 TWA.Ty = new PATypeHolder(Type::VoidTy);
1490 $$->push_back(TWA);
1491 CHECK_FOR_ERROR
1492 }
1493 | DOTDOTDOT {
1494 $$ = new TypeWithAttrsList;
Reid Spencera472f662007-04-11 02:44:20 +00001495 TypeWithAttrs TWA; TWA.Attrs = ParamAttr::None;
Reid Spencerfa35bb32006-12-31 05:40:12 +00001496 TWA.Ty = new PATypeHolder(Type::VoidTy);
1497 $$->push_back(TWA);
1498 CHECK_FOR_ERROR
1499 }
1500 | /*empty*/ {
1501 $$ = new TypeWithAttrsList();
Reid Spencer713eedc2006-08-18 08:43:06 +00001502 CHECK_FOR_ERROR
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001503 };
Chris Lattneraa534bf2001-09-07 16:35:17 +00001504
Chris Lattner113f4f42002-06-25 16:13:24 +00001505// TypeList - Used for struct declarations and as a basis for function type
Chris Lattneraa534bf2001-09-07 16:35:17 +00001506// declaration type lists
1507//
Reid Spencerfa35bb32006-12-31 05:40:12 +00001508TypeListI : Types {
Reid Spencer812724e2006-12-03 05:45:44 +00001509 $$ = new std::list<PATypeHolder>();
Reid Spencerd05bc522007-03-20 01:13:00 +00001510 $$->push_back(*$1);
1511 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001512 CHECK_FOR_ERROR
Chris Lattneraa534bf2001-09-07 16:35:17 +00001513 }
Reid Spencerfa35bb32006-12-31 05:40:12 +00001514 | TypeListI ',' Types {
Reid Spencerd05bc522007-03-20 01:13:00 +00001515 ($$=$1)->push_back(*$3);
1516 delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00001517 CHECK_FOR_ERROR
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001518 };
Chris Lattneraa534bf2001-09-07 16:35:17 +00001519
Chris Lattner56f73d42001-07-14 06:10:16 +00001520// ConstVal - The various declarations that go into the constant pool. This
Chris Lattner7f325cd2002-08-16 21:14:40 +00001521// production is used ONLY to represent constants that show up AFTER a 'const',
1522// 'constant' or 'global' token at global scope. Constants that can be inlined
1523// into other expressions (such as integers and constexprs) are handled by the
1524// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattner56f73d42001-07-14 06:10:16 +00001525//
Chris Lattnere63ddd8df2001-10-03 03:19:33 +00001526ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencerfa35bb32006-12-31 05:40:12 +00001527 if (!UpRefs.empty())
1528 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer812724e2006-12-03 05:45:44 +00001529 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnere63ddd8df2001-10-03 03:19:33 +00001530 if (ATy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001531 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerde1d05f2007-02-05 10:16:10 +00001532 (*$1)->getDescription() + "'");
Chris Lattneraa534bf2001-09-07 16:35:17 +00001533 const Type *ETy = ATy->getElementType();
1534 int NumElements = ATy->getNumElements();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001535
Chris Lattneraa534bf2001-09-07 16:35:17 +00001536 // Verify that we have the correct size...
1537 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer713eedc2006-08-18 08:43:06 +00001538 GEN_ERROR("Type mismatch: constant sized array initialized with " +
Reid Spencerce6adaf2004-07-18 00:08:11 +00001539 utostr($3->size()) + " arguments, but has size of " +
Reid Spencerde1d05f2007-02-05 10:16:10 +00001540 itostr(NumElements) + "");
Chris Lattner2f7c9632001-06-06 20:29:01 +00001541
Chris Lattneraa534bf2001-09-07 16:35:17 +00001542 // Verify all elements are correct type!
1543 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencer812724e2006-12-03 05:45:44 +00001544 if (ETy != (*$3)[i]->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00001545 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Reid Spencerce6adaf2004-07-18 00:08:11 +00001546 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencer812724e2006-12-03 05:45:44 +00001547 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner2f7c9632001-06-06 20:29:01 +00001548 }
1549
Reid Spencer812724e2006-12-03 05:45:44 +00001550 $$ = ConstantArray::get(ATy, *$3);
1551 delete $1; delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00001552 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00001553 }
Chris Lattnere63ddd8df2001-10-03 03:19:33 +00001554 | Types '[' ']' {
Reid Spencerfa35bb32006-12-31 05:40:12 +00001555 if (!UpRefs.empty())
1556 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer812724e2006-12-03 05:45:44 +00001557 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnere63ddd8df2001-10-03 03:19:33 +00001558 if (ATy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001559 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerde1d05f2007-02-05 10:16:10 +00001560 (*$1)->getDescription() + "'");
Chris Lattnere63ddd8df2001-10-03 03:19:33 +00001561
1562 int NumElements = ATy->getNumElements();
Chris Lattneraa534bf2001-09-07 16:35:17 +00001563 if (NumElements != -1 && NumElements != 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001564 GEN_ERROR("Type mismatch: constant sized array initialized with 0"
Reid Spencerde1d05f2007-02-05 10:16:10 +00001565 " arguments, but has size of " + itostr(NumElements) +"");
Reid Spencer812724e2006-12-03 05:45:44 +00001566 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
1567 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001568 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00001569 }
Chris Lattnere63ddd8df2001-10-03 03:19:33 +00001570 | Types 'c' STRINGCONSTANT {
Reid Spencerfa35bb32006-12-31 05:40:12 +00001571 if (!UpRefs.empty())
1572 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer812724e2006-12-03 05:45:44 +00001573 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnere63ddd8df2001-10-03 03:19:33 +00001574 if (ATy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001575 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerde1d05f2007-02-05 10:16:10 +00001576 (*$1)->getDescription() + "'");
Chris Lattnere63ddd8df2001-10-03 03:19:33 +00001577
Chris Lattneraa534bf2001-09-07 16:35:17 +00001578 int NumElements = ATy->getNumElements();
1579 const Type *ETy = ATy->getElementType();
Reid Spencer3b208cc2007-05-22 18:52:21 +00001580 if (NumElements != -1 && NumElements != int($3->length()))
Reid Spencer713eedc2006-08-18 08:43:06 +00001581 GEN_ERROR("Can't build string constant of size " +
Reid Spencer3b208cc2007-05-22 18:52:21 +00001582 itostr((int)($3->length())) +
Reid Spencerde1d05f2007-02-05 10:16:10 +00001583 " when array has size " + itostr(NumElements) + "");
Chris Lattner89da8a32003-04-22 18:42:41 +00001584 std::vector<Constant*> Vals;
Reid Spencerfa35bb32006-12-31 05:40:12 +00001585 if (ETy == Type::Int8Ty) {
Reid Spencer3b208cc2007-05-22 18:52:21 +00001586 for (unsigned i = 0; i < $3->length(); ++i)
1587 Vals.push_back(ConstantInt::get(ETy, (*$3)[i]));
Chris Lattner26e50dc2001-07-28 17:48:55 +00001588 } else {
Reid Spencer3b208cc2007-05-22 18:52:21 +00001589 delete $3;
Reid Spencerde1d05f2007-02-05 10:16:10 +00001590 GEN_ERROR("Cannot build string arrays of non byte sized elements");
Chris Lattner26e50dc2001-07-28 17:48:55 +00001591 }
Reid Spencer3b208cc2007-05-22 18:52:21 +00001592 delete $3;
Reid Spencer812724e2006-12-03 05:45:44 +00001593 $$ = ConstantArray::get(ATy, Vals);
1594 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001595 CHECK_FOR_ERROR
Chris Lattner26e50dc2001-07-28 17:48:55 +00001596 }
Brian Gaeke02209042004-08-20 06:00:58 +00001597 | Types '<' ConstVector '>' { // Nonempty unsized arr
Reid Spencerfa35bb32006-12-31 05:40:12 +00001598 if (!UpRefs.empty())
1599 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencerd84d35b2007-02-15 02:26:10 +00001600 const VectorType *PTy = dyn_cast<VectorType>($1->get());
Brian Gaeke02209042004-08-20 06:00:58 +00001601 if (PTy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001602 GEN_ERROR("Cannot make packed constant with type: '" +
Reid Spencerde1d05f2007-02-05 10:16:10 +00001603 (*$1)->getDescription() + "'");
Brian Gaeke02209042004-08-20 06:00:58 +00001604 const Type *ETy = PTy->getElementType();
1605 int NumElements = PTy->getNumElements();
1606
1607 // Verify that we have the correct size...
1608 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer713eedc2006-08-18 08:43:06 +00001609 GEN_ERROR("Type mismatch: constant sized packed initialized with " +
Brian Gaeke02209042004-08-20 06:00:58 +00001610 utostr($3->size()) + " arguments, but has size of " +
Reid Spencerde1d05f2007-02-05 10:16:10 +00001611 itostr(NumElements) + "");
Brian Gaeke02209042004-08-20 06:00:58 +00001612
1613 // Verify all elements are correct type!
1614 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencer812724e2006-12-03 05:45:44 +00001615 if (ETy != (*$3)[i]->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00001616 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Brian Gaeke02209042004-08-20 06:00:58 +00001617 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencer812724e2006-12-03 05:45:44 +00001618 (*$3)[i]->getType()->getDescription() + "'.");
Brian Gaeke02209042004-08-20 06:00:58 +00001619 }
1620
Reid Spencerd84d35b2007-02-15 02:26:10 +00001621 $$ = ConstantVector::get(PTy, *$3);
Reid Spencer812724e2006-12-03 05:45:44 +00001622 delete $1; delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00001623 CHECK_FOR_ERROR
Brian Gaeke02209042004-08-20 06:00:58 +00001624 }
Chris Lattnere63ddd8df2001-10-03 03:19:33 +00001625 | Types '{' ConstVector '}' {
Reid Spencer812724e2006-12-03 05:45:44 +00001626 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnere63ddd8df2001-10-03 03:19:33 +00001627 if (STy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001628 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerde1d05f2007-02-05 10:16:10 +00001629 (*$1)->getDescription() + "'");
Chris Lattner4b1d1062003-04-15 16:09:31 +00001630
Chris Lattner5bb23152003-05-21 16:06:56 +00001631 if ($3->size() != STy->getNumContainedTypes())
Reid Spencerde1d05f2007-02-05 10:16:10 +00001632 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattner5bb23152003-05-21 16:06:56 +00001633
Chris Lattner4b1d1062003-04-15 16:09:31 +00001634 // Check to ensure that constants are compatible with the type initializer!
1635 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Reid Spencer812724e2006-12-03 05:45:44 +00001636 if ((*$3)[i]->getType() != STy->getElementType(i))
Reid Spencer713eedc2006-08-18 08:43:06 +00001637 GEN_ERROR("Expected type '" +
Chris Lattnerac6db752004-02-09 04:37:31 +00001638 STy->getElementType(i)->getDescription() +
Chris Lattner4b1d1062003-04-15 16:09:31 +00001639 "' for element #" + utostr(i) +
Reid Spencerde1d05f2007-02-05 10:16:10 +00001640 " of structure initializer");
Chris Lattner4b1d1062003-04-15 16:09:31 +00001641
Andrew Lenharthe3c5a4c2007-01-08 18:16:47 +00001642 // Check to ensure that Type is not packed
1643 if (STy->isPacked())
Chris Lattnereee35bf2007-04-26 05:30:35 +00001644 GEN_ERROR("Unpacked Initializer to vector type '" +
1645 STy->getDescription() + "'");
Andrew Lenharthe3c5a4c2007-01-08 18:16:47 +00001646
Reid Spencer812724e2006-12-03 05:45:44 +00001647 $$ = ConstantStruct::get(STy, *$3);
1648 delete $1; delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00001649 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00001650 }
Chris Lattner5bb23152003-05-21 16:06:56 +00001651 | Types '{' '}' {
Reid Spencerfa35bb32006-12-31 05:40:12 +00001652 if (!UpRefs.empty())
1653 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer812724e2006-12-03 05:45:44 +00001654 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattner5bb23152003-05-21 16:06:56 +00001655 if (STy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001656 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerde1d05f2007-02-05 10:16:10 +00001657 (*$1)->getDescription() + "'");
Chris Lattner5bb23152003-05-21 16:06:56 +00001658
1659 if (STy->getNumContainedTypes() != 0)
Reid Spencerde1d05f2007-02-05 10:16:10 +00001660 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattner5bb23152003-05-21 16:06:56 +00001661
Andrew Lenharthe3c5a4c2007-01-08 18:16:47 +00001662 // Check to ensure that Type is not packed
1663 if (STy->isPacked())
Chris Lattnereee35bf2007-04-26 05:30:35 +00001664 GEN_ERROR("Unpacked Initializer to vector type '" +
1665 STy->getDescription() + "'");
Andrew Lenharthe3c5a4c2007-01-08 18:16:47 +00001666
1667 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1668 delete $1;
1669 CHECK_FOR_ERROR
1670 }
1671 | Types '<' '{' ConstVector '}' '>' {
1672 const StructType *STy = dyn_cast<StructType>($1->get());
1673 if (STy == 0)
1674 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerde1d05f2007-02-05 10:16:10 +00001675 (*$1)->getDescription() + "'");
Andrew Lenharthe3c5a4c2007-01-08 18:16:47 +00001676
1677 if ($4->size() != STy->getNumContainedTypes())
Reid Spencerde1d05f2007-02-05 10:16:10 +00001678 GEN_ERROR("Illegal number of initializers for structure type");
Andrew Lenharthe3c5a4c2007-01-08 18:16:47 +00001679
1680 // Check to ensure that constants are compatible with the type initializer!
1681 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1682 if ((*$4)[i]->getType() != STy->getElementType(i))
1683 GEN_ERROR("Expected type '" +
1684 STy->getElementType(i)->getDescription() +
1685 "' for element #" + utostr(i) +
Reid Spencerde1d05f2007-02-05 10:16:10 +00001686 " of structure initializer");
Andrew Lenharthe3c5a4c2007-01-08 18:16:47 +00001687
1688 // Check to ensure that Type is packed
1689 if (!STy->isPacked())
Reid Spencer09575ba2007-02-15 03:39:18 +00001690 GEN_ERROR("Vector initializer to non-vector type '" +
1691 STy->getDescription() + "'");
Andrew Lenharthe3c5a4c2007-01-08 18:16:47 +00001692
1693 $$ = ConstantStruct::get(STy, *$4);
1694 delete $1; delete $4;
1695 CHECK_FOR_ERROR
1696 }
1697 | Types '<' '{' '}' '>' {
1698 if (!UpRefs.empty())
1699 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1700 const StructType *STy = dyn_cast<StructType>($1->get());
1701 if (STy == 0)
1702 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerde1d05f2007-02-05 10:16:10 +00001703 (*$1)->getDescription() + "'");
Andrew Lenharthe3c5a4c2007-01-08 18:16:47 +00001704
1705 if (STy->getNumContainedTypes() != 0)
Reid Spencerde1d05f2007-02-05 10:16:10 +00001706 GEN_ERROR("Illegal number of initializers for structure type");
Andrew Lenharthe3c5a4c2007-01-08 18:16:47 +00001707
1708 // Check to ensure that Type is packed
1709 if (!STy->isPacked())
Reid Spencer09575ba2007-02-15 03:39:18 +00001710 GEN_ERROR("Vector initializer to non-vector type '" +
1711 STy->getDescription() + "'");
Andrew Lenharthe3c5a4c2007-01-08 18:16:47 +00001712
Reid Spencer812724e2006-12-03 05:45:44 +00001713 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1714 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001715 CHECK_FOR_ERROR
Chris Lattner5bb23152003-05-21 16:06:56 +00001716 }
Chris Lattnere63ddd8df2001-10-03 03:19:33 +00001717 | Types NULL_TOK {
Reid Spencerfa35bb32006-12-31 05:40:12 +00001718 if (!UpRefs.empty())
1719 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer812724e2006-12-03 05:45:44 +00001720 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnere63ddd8df2001-10-03 03:19:33 +00001721 if (PTy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001722 GEN_ERROR("Cannot make null pointer constant with type: '" +
Reid Spencerde1d05f2007-02-05 10:16:10 +00001723 (*$1)->getDescription() + "'");
Chris Lattnere63ddd8df2001-10-03 03:19:33 +00001724
Reid Spencer812724e2006-12-03 05:45:44 +00001725 $$ = ConstantPointerNull::get(PTy);
1726 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001727 CHECK_FOR_ERROR
Chris Lattner571a6b02001-10-03 01:49:25 +00001728 }
Chris Lattner4ff31492004-10-16 18:17:13 +00001729 | Types UNDEF {
Reid Spencerfa35bb32006-12-31 05:40:12 +00001730 if (!UpRefs.empty())
1731 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer812724e2006-12-03 05:45:44 +00001732 $$ = UndefValue::get($1->get());
1733 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001734 CHECK_FOR_ERROR
Chris Lattner4ff31492004-10-16 18:17:13 +00001735 }
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001736 | Types SymbolicValueRef {
Reid Spencerfa35bb32006-12-31 05:40:12 +00001737 if (!UpRefs.empty())
1738 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer812724e2006-12-03 05:45:44 +00001739 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattner60e0dd72001-10-03 06:12:09 +00001740 if (Ty == 0)
Reid Spencerde1d05f2007-02-05 10:16:10 +00001741 GEN_ERROR("Global const reference must be a pointer type");
Chris Lattner60e0dd72001-10-03 06:12:09 +00001742
Chris Lattner61643a02002-08-15 17:58:33 +00001743 // ConstExprs can exist in the body of a function, thus creating
Reid Spencerce6adaf2004-07-18 00:08:11 +00001744 // GlobalValues whenever they refer to a variable. Because we are in
Reid Spencerfe65ae82007-03-19 18:39:36 +00001745 // the context of a function, getExistingVal will search the functions
Chris Lattner61643a02002-08-15 17:58:33 +00001746 // symbol table instead of the module symbol table for the global symbol,
1747 // which throws things all off. To get around this, we just tell
Reid Spencerfe65ae82007-03-19 18:39:36 +00001748 // getExistingVal that we are at global scope here.
Chris Lattner61643a02002-08-15 17:58:33 +00001749 //
Chris Lattner61877742003-10-16 20:12:13 +00001750 Function *SavedCurFn = CurFun.CurrentFunction;
1751 CurFun.CurrentFunction = 0;
Chris Lattner61643a02002-08-15 17:58:33 +00001752
Reid Spencerfe65ae82007-03-19 18:39:36 +00001753 Value *V = getExistingVal(Ty, $2);
Reid Spencer309080a2006-09-28 19:28:24 +00001754 CHECK_FOR_ERROR
Chris Lattner60e0dd72001-10-03 06:12:09 +00001755
Chris Lattner61877742003-10-16 20:12:13 +00001756 CurFun.CurrentFunction = SavedCurFn;
Chris Lattner61643a02002-08-15 17:58:33 +00001757
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001758 // If this is an initializer for a constant pointer, which is referencing a
1759 // (currently) undefined variable, create a stub now that shall be replaced
1760 // in the future with the right type of variable.
1761 //
1762 if (V == 0) {
Reid Spencer9894d6c2007-02-05 17:01:20 +00001763 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001764 const PointerType *PT = cast<PointerType>(Ty);
1765
1766 // First check to see if the forward references value is already created!
1767 PerModuleInfo::GlobalRefsType::iterator I =
Reid Spencerce6adaf2004-07-18 00:08:11 +00001768 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001769
1770 if (I != CurModule.GlobalRefs.end()) {
Reid Spencerce6adaf2004-07-18 00:08:11 +00001771 V = I->second; // Placeholder already exists, use it...
Chris Lattnere9703012003-12-23 20:05:15 +00001772 $2.destroy();
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001773 } else {
Chris Lattner4c9210e2004-07-14 21:44:00 +00001774 std::string Name;
Reid Spencer791b8ef2007-01-26 08:04:51 +00001775 if ($2.Type == ValID::GlobalName)
Reid Spencer3b208cc2007-05-22 18:52:21 +00001776 Name = $2.getName();
Reid Spencer791b8ef2007-01-26 08:04:51 +00001777 else if ($2.Type != ValID::GlobalID)
1778 GEN_ERROR("Invalid reference to global");
Chris Lattner4c9210e2004-07-14 21:44:00 +00001779
Reid Spencerce6adaf2004-07-18 00:08:11 +00001780 // Create the forward referenced global.
Chris Lattner8abe1a12004-07-14 23:03:46 +00001781 GlobalValue *GV;
1782 if (const FunctionType *FTy =
1783 dyn_cast<FunctionType>(PT->getElementType())) {
Chris Lattnereee35bf2007-04-26 05:30:35 +00001784 GV = new Function(FTy, GlobalValue::ExternalWeakLinkage, Name,
Chris Lattner8abe1a12004-07-14 23:03:46 +00001785 CurModule.CurrentModule);
1786 } else {
Reid Spencerce6adaf2004-07-18 00:08:11 +00001787 GV = new GlobalVariable(PT->getElementType(), false,
Chris Lattnereee35bf2007-04-26 05:30:35 +00001788 GlobalValue::ExternalWeakLinkage, 0,
Chris Lattner8abe1a12004-07-14 23:03:46 +00001789 Name, CurModule.CurrentModule);
1790 }
Chris Lattner4c9210e2004-07-14 21:44:00 +00001791
Reid Spencerce6adaf2004-07-18 00:08:11 +00001792 // Keep track of the fact that we have a forward ref to recycle it
1793 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1794 V = GV;
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001795 }
Chris Lattner60e0dd72001-10-03 06:12:09 +00001796 }
1797
Reid Spencer812724e2006-12-03 05:45:44 +00001798 $$ = cast<GlobalValue>(V);
1799 delete $1; // Free the type handle
Reid Spencer713eedc2006-08-18 08:43:06 +00001800 CHECK_FOR_ERROR
Vikram S. Adve7064eaf2002-07-14 22:59:28 +00001801 }
Chris Lattner7f325cd2002-08-16 21:14:40 +00001802 | Types ConstExpr {
Reid Spencerfa35bb32006-12-31 05:40:12 +00001803 if (!UpRefs.empty())
1804 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer812724e2006-12-03 05:45:44 +00001805 if ($1->get() != $2->getType())
Reid Spencer4eda71e2007-01-04 00:05:48 +00001806 GEN_ERROR("Mismatched types for constant expression: " +
1807 (*$1)->getDescription() + " and " + $2->getType()->getDescription());
Chris Lattner7f325cd2002-08-16 21:14:40 +00001808 $$ = $2;
Reid Spencer812724e2006-12-03 05:45:44 +00001809 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001810 CHECK_FOR_ERROR
Chris Lattner79694012003-06-28 20:01:34 +00001811 }
1812 | Types ZEROINITIALIZER {
Reid Spencerfa35bb32006-12-31 05:40:12 +00001813 if (!UpRefs.empty())
1814 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer812724e2006-12-03 05:45:44 +00001815 const Type *Ty = $1->get();
Chris Lattneraef74b52004-11-28 16:45:45 +00001816 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
Reid Spencerde1d05f2007-02-05 10:16:10 +00001817 GEN_ERROR("Cannot create a null initialized value of this type");
Reid Spencer812724e2006-12-03 05:45:44 +00001818 $$ = Constant::getNullValue(Ty);
1819 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001820 CHECK_FOR_ERROR
Reid Spencer812724e2006-12-03 05:45:44 +00001821 }
Reid Spencerfa35bb32006-12-31 05:40:12 +00001822 | IntType ESINT64VAL { // integral constants
Reid Spencer3f46fba2006-12-20 17:20:09 +00001823 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencerde1d05f2007-02-05 10:16:10 +00001824 GEN_ERROR("Constant value doesn't fit in type");
Reid Spencer363fd462007-03-19 20:40:22 +00001825 $$ = ConstantInt::get($1, $2, true);
Reid Spencer9875fc12007-02-28 02:23:44 +00001826 CHECK_FOR_ERROR
1827 }
1828 | IntType ESAPINTVAL { // arbitrary precision integer constants
1829 uint32_t BitWidth = cast<IntegerType>($1)->getBitWidth();
1830 if ($2->getBitWidth() > BitWidth) {
1831 GEN_ERROR("Constant value does not fit in type");
Reid Spencer666ea0d2007-03-01 19:32:01 +00001832 }
1833 $2->sextOrTrunc(BitWidth);
1834 $$ = ConstantInt::get(*$2);
Reid Spencer9875fc12007-02-28 02:23:44 +00001835 delete $2;
Reid Spencer3f46fba2006-12-20 17:20:09 +00001836 CHECK_FOR_ERROR
1837 }
Reid Spencerfa35bb32006-12-31 05:40:12 +00001838 | IntType EUINT64VAL { // integral constants
Reid Spencer3f46fba2006-12-20 17:20:09 +00001839 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencerde1d05f2007-02-05 10:16:10 +00001840 GEN_ERROR("Constant value doesn't fit in type");
Reid Spencer363fd462007-03-19 20:40:22 +00001841 $$ = ConstantInt::get($1, $2, false);
Reid Spencer9875fc12007-02-28 02:23:44 +00001842 CHECK_FOR_ERROR
1843 }
1844 | IntType EUAPINTVAL { // arbitrary precision integer constants
1845 uint32_t BitWidth = cast<IntegerType>($1)->getBitWidth();
1846 if ($2->getBitWidth() > BitWidth) {
1847 GEN_ERROR("Constant value does not fit in type");
Reid Spencer666ea0d2007-03-01 19:32:01 +00001848 }
1849 $2->zextOrTrunc(BitWidth);
1850 $$ = ConstantInt::get(*$2);
Reid Spencer9875fc12007-02-28 02:23:44 +00001851 delete $2;
Reid Spencer3f46fba2006-12-20 17:20:09 +00001852 CHECK_FOR_ERROR
1853 }
Reid Spencer502d64e2007-01-13 05:00:20 +00001854 | INTTYPE TRUETOK { // Boolean constants
1855 assert(cast<IntegerType>($1)->getBitWidth() == 1 && "Not Bool?");
Zhou Sheng75b871f2007-01-11 12:24:14 +00001856 $$ = ConstantInt::getTrue();
Reid Spencer713eedc2006-08-18 08:43:06 +00001857 CHECK_FOR_ERROR
Chris Lattner7f1c98d2002-08-15 18:17:28 +00001858 }
Reid Spencer502d64e2007-01-13 05:00:20 +00001859 | INTTYPE FALSETOK { // Boolean constants
1860 assert(cast<IntegerType>($1)->getBitWidth() == 1 && "Not Bool?");
Zhou Sheng75b871f2007-01-11 12:24:14 +00001861 $$ = ConstantInt::getFalse();
Reid Spencer713eedc2006-08-18 08:43:06 +00001862 CHECK_FOR_ERROR
Chris Lattner7f1c98d2002-08-15 18:17:28 +00001863 }
1864 | FPType FPVAL { // Float & Double constants
Reid Spencer812724e2006-12-03 05:45:44 +00001865 if (!ConstantFP::isValueValidForType($1, $2))
Reid Spencerde1d05f2007-02-05 10:16:10 +00001866 GEN_ERROR("Floating point constant invalid for type");
Reid Spencer812724e2006-12-03 05:45:44 +00001867 $$ = ConstantFP::get($1, $2);
Reid Spencer713eedc2006-08-18 08:43:06 +00001868 CHECK_FOR_ERROR
Chris Lattner7f1c98d2002-08-15 18:17:28 +00001869 };
1870
Chris Lattner2f7c9632001-06-06 20:29:01 +00001871
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001872ConstExpr: CastOps '(' ConstVal TO Types ')' {
Reid Spencerfa35bb32006-12-31 05:40:12 +00001873 if (!UpRefs.empty())
1874 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencer812724e2006-12-03 05:45:44 +00001875 Constant *Val = $3;
Reid Spencer082a77f2007-01-17 02:47:33 +00001876 const Type *DestTy = $5->get();
1877 if (!CastInst::castIsValid($1, $3, DestTy))
1878 GEN_ERROR("invalid cast opcode for cast from '" +
1879 Val->getType()->getDescription() + "' to '" +
Reid Spencerde1d05f2007-02-05 10:16:10 +00001880 DestTy->getDescription() + "'");
Reid Spencer082a77f2007-01-17 02:47:33 +00001881 $$ = ConstantExpr::getCast($1, $3, DestTy);
Reid Spencer812724e2006-12-03 05:45:44 +00001882 delete $5;
Vikram S. Adve7064eaf2002-07-14 22:59:28 +00001883 }
Chris Lattner7f325cd2002-08-16 21:14:40 +00001884 | GETELEMENTPTR '(' ConstVal IndexList ')' {
Reid Spencer812724e2006-12-03 05:45:44 +00001885 if (!isa<PointerType>($3->getType()))
Reid Spencerde1d05f2007-02-05 10:16:10 +00001886 GEN_ERROR("GetElementPtr requires a pointer operand");
Chris Lattner3cd8c562002-07-30 18:54:25 +00001887
Reid Spencer812724e2006-12-03 05:45:44 +00001888 const Type *IdxTy =
Chris Lattner04a2d762007-02-13 00:57:40 +00001889 GetElementPtrInst::getIndexedType($3->getType(), &(*$4)[0], $4->size(),
1890 true);
Reid Spencer812724e2006-12-03 05:45:44 +00001891 if (!IdxTy)
Reid Spencerde1d05f2007-02-05 10:16:10 +00001892 GEN_ERROR("Index list invalid for constant getelementptr");
Reid Spencer812724e2006-12-03 05:45:44 +00001893
Chris Lattner10aad382007-01-31 04:43:46 +00001894 SmallVector<Constant*, 8> IdxVec;
Reid Spencer812724e2006-12-03 05:45:44 +00001895 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1896 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattner980ddf52002-07-18 00:14:27 +00001897 IdxVec.push_back(C);
1898 else
Reid Spencerde1d05f2007-02-05 10:16:10 +00001899 GEN_ERROR("Indices to constant getelementptr must be constants");
Chris Lattner980ddf52002-07-18 00:14:27 +00001900
Chris Lattner7f325cd2002-08-16 21:14:40 +00001901 delete $4;
Chris Lattner980ddf52002-07-18 00:14:27 +00001902
Chris Lattner10aad382007-01-31 04:43:46 +00001903 $$ = ConstantExpr::getGetElementPtr($3, &IdxVec[0], IdxVec.size());
Reid Spencer713eedc2006-08-18 08:43:06 +00001904 CHECK_FOR_ERROR
Vikram S. Adve7064eaf2002-07-14 22:59:28 +00001905 }
Chris Lattner6536f0c2004-03-12 05:51:36 +00001906 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer542964f2007-01-11 18:21:29 +00001907 if ($3->getType() != Type::Int1Ty)
Reid Spencerde1d05f2007-02-05 10:16:10 +00001908 GEN_ERROR("Select condition must be of boolean type");
Reid Spencer812724e2006-12-03 05:45:44 +00001909 if ($5->getType() != $7->getType())
Reid Spencerde1d05f2007-02-05 10:16:10 +00001910 GEN_ERROR("Select operand types must match");
Reid Spencer812724e2006-12-03 05:45:44 +00001911 $$ = ConstantExpr::getSelect($3, $5, $7);
Reid Spencer713eedc2006-08-18 08:43:06 +00001912 CHECK_FOR_ERROR
Chris Lattner6536f0c2004-03-12 05:51:36 +00001913 }
Chris Lattnerfca28332004-08-17 17:26:41 +00001914 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer812724e2006-12-03 05:45:44 +00001915 if ($3->getType() != $5->getType())
Reid Spencerde1d05f2007-02-05 10:16:10 +00001916 GEN_ERROR("Binary operator types must match");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001917 CHECK_FOR_ERROR;
Reid Spencer5bcee9a2006-12-05 19:15:41 +00001918 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattnerfca28332004-08-17 17:26:41 +00001919 }
1920 | LogicalOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer812724e2006-12-03 05:45:44 +00001921 if ($3->getType() != $5->getType())
Reid Spencerde1d05f2007-02-05 10:16:10 +00001922 GEN_ERROR("Logical operator types must match");
Chris Lattner03c49532007-01-15 02:27:26 +00001923 if (!$3->getType()->isInteger()) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001924 if (Instruction::isShift($1) || !isa<VectorType>($3->getType()) ||
1925 !cast<VectorType>($3->getType())->getElementType()->isInteger())
Reid Spencerde1d05f2007-02-05 10:16:10 +00001926 GEN_ERROR("Logical operator requires integral operands");
Chris Lattnera5d70e92005-12-21 18:31:29 +00001927 }
Reid Spencer812724e2006-12-03 05:45:44 +00001928 $$ = ConstantExpr::get($1, $3, $5);
Reid Spencer713eedc2006-08-18 08:43:06 +00001929 CHECK_FOR_ERROR
Chris Lattnerfca28332004-08-17 17:26:41 +00001930 }
Reid Spencerf3490692006-12-04 05:20:06 +00001931 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
1932 if ($4->getType() != $6->getType())
Reid Spencerde1d05f2007-02-05 10:16:10 +00001933 GEN_ERROR("icmp operand types must match");
Reid Spencerf3490692006-12-04 05:20:06 +00001934 $$ = ConstantExpr::getICmp($2, $4, $6);
Reid Spencer812724e2006-12-03 05:45:44 +00001935 }
Reid Spencerf3490692006-12-04 05:20:06 +00001936 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
1937 if ($4->getType() != $6->getType())
Reid Spencerde1d05f2007-02-05 10:16:10 +00001938 GEN_ERROR("fcmp operand types must match");
Reid Spencerf3490692006-12-04 05:20:06 +00001939 $$ = ConstantExpr::getFCmp($2, $4, $6);
Reid Spencer812724e2006-12-03 05:45:44 +00001940 }
Robert Bocchinoaa1cf542006-01-10 19:04:32 +00001941 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencer812724e2006-12-03 05:45:44 +00001942 if (!ExtractElementInst::isValidOperands($3, $5))
Reid Spencerde1d05f2007-02-05 10:16:10 +00001943 GEN_ERROR("Invalid extractelement operands");
Reid Spencer812724e2006-12-03 05:45:44 +00001944 $$ = ConstantExpr::getExtractElement($3, $5);
Reid Spencer713eedc2006-08-18 08:43:06 +00001945 CHECK_FOR_ERROR
Chris Lattner8c32ad02006-04-08 03:53:34 +00001946 }
1947 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer812724e2006-12-03 05:45:44 +00001948 if (!InsertElementInst::isValidOperands($3, $5, $7))
Reid Spencerde1d05f2007-02-05 10:16:10 +00001949 GEN_ERROR("Invalid insertelement operands");
Reid Spencer812724e2006-12-03 05:45:44 +00001950 $$ = ConstantExpr::getInsertElement($3, $5, $7);
Reid Spencer713eedc2006-08-18 08:43:06 +00001951 CHECK_FOR_ERROR
Chris Lattner8c32ad02006-04-08 03:53:34 +00001952 }
1953 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer812724e2006-12-03 05:45:44 +00001954 if (!ShuffleVectorInst::isValidOperands($3, $5, $7))
Reid Spencerde1d05f2007-02-05 10:16:10 +00001955 GEN_ERROR("Invalid shufflevector operands");
Reid Spencer812724e2006-12-03 05:45:44 +00001956 $$ = ConstantExpr::getShuffleVector($3, $5, $7);
Reid Spencer713eedc2006-08-18 08:43:06 +00001957 CHECK_FOR_ERROR
Chris Lattnerdb3b2022002-08-14 17:12:33 +00001958 };
Vikram S. Adve7064eaf2002-07-14 22:59:28 +00001959
Chris Lattner8c32ad02006-04-08 03:53:34 +00001960
Misha Brukman7fdaab42003-07-14 17:20:40 +00001961// ConstVector - A list of comma separated constants.
Chris Lattner2f7c9632001-06-06 20:29:01 +00001962ConstVector : ConstVector ',' ConstVal {
Chris Lattneraa534bf2001-09-07 16:35:17 +00001963 ($$ = $1)->push_back($3);
Reid Spencer713eedc2006-08-18 08:43:06 +00001964 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00001965 }
1966 | ConstVal {
Reid Spencer812724e2006-12-03 05:45:44 +00001967 $$ = new std::vector<Constant*>();
Chris Lattneraa534bf2001-09-07 16:35:17 +00001968 $$->push_back($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00001969 CHECK_FOR_ERROR
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001970 };
Chris Lattner2f7c9632001-06-06 20:29:01 +00001971
Chris Lattner252afba2001-07-26 16:29:15 +00001972
Chris Lattner7fb14f52001-09-18 04:00:54 +00001973// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001974GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner7fb14f52001-09-18 04:00:54 +00001975
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +00001976// ThreadLocal
1977ThreadLocal : THREAD_LOCAL { $$ = true; } | { $$ = false; };
1978
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +00001979// AliaseeRef - Match either GlobalValue or bitcast to GlobalValue.
1980AliaseeRef : ResultTypes SymbolicValueRef {
1981 const Type* VTy = $1->get();
1982 Value *V = getVal(VTy, $2);
1983 GlobalValue* Aliasee = dyn_cast<GlobalValue>(V);
1984 if (!Aliasee)
1985 GEN_ERROR("Aliases can be created only to global values");
1986
1987 $$ = Aliasee;
1988 CHECK_FOR_ERROR
1989 delete $1;
1990 }
1991 | BITCAST '(' AliaseeRef TO Types ')' {
1992 Constant *Val = $3;
1993 const Type *DestTy = $5->get();
1994 if (!CastInst::castIsValid($1, $3, DestTy))
1995 GEN_ERROR("invalid cast opcode for cast from '" +
1996 Val->getType()->getDescription() + "' to '" +
1997 DestTy->getDescription() + "'");
1998
1999 $$ = ConstantExpr::getCast($1, $3, DestTy);
2000 CHECK_FOR_ERROR
2001 delete $5;
2002 };
Chris Lattner2f7c9632001-06-06 20:29:01 +00002003
Chris Lattner7b804d62002-05-02 19:11:13 +00002004//===----------------------------------------------------------------------===//
2005// Rules to match Modules
2006//===----------------------------------------------------------------------===//
2007
2008// Module rule: Capture the result of parsing the whole file into a result
2009// variable...
2010//
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00002011Module
2012 : DefinitionList {
2013 $$ = ParserResult = CurModule.CurrentModule;
2014 CurModule.ModuleDone();
2015 CHECK_FOR_ERROR;
2016 }
2017 | /*empty*/ {
2018 $$ = ParserResult = CurModule.CurrentModule;
2019 CurModule.ModuleDone();
2020 CHECK_FOR_ERROR;
2021 }
2022 ;
Chris Lattner7b804d62002-05-02 19:11:13 +00002023
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00002024DefinitionList
2025 : Definition
2026 | DefinitionList Definition
2027 ;
2028
2029Definition
Jeff Cohen5d956e42007-01-21 19:19:31 +00002030 : DEFINE { CurFun.isDeclare = false; } Function {
Chris Lattner61877742003-10-16 20:12:13 +00002031 CurFun.FunctionDone();
Reid Spencer713eedc2006-08-18 08:43:06 +00002032 CHECK_FOR_ERROR
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00002033 }
2034 | DECLARE { CurFun.isDeclare = true; } FunctionProto {
Reid Spencer713eedc2006-08-18 08:43:06 +00002035 CHECK_FOR_ERROR
Chris Lattner7b804d62002-05-02 19:11:13 +00002036 }
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00002037 | MODULE ASM_TOK AsmBlock {
Reid Spencer713eedc2006-08-18 08:43:06 +00002038 CHECK_FOR_ERROR
Chris Lattnerf09741f2006-01-23 23:05:15 +00002039 }
Reid Spencer791b8ef2007-01-26 08:04:51 +00002040 | OptLocalAssign TYPE Types {
Reid Spencerfa35bb32006-12-31 05:40:12 +00002041 if (!UpRefs.empty())
2042 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattnerd50fa5a2001-10-22 05:56:09 +00002043 // Eagerly resolve types. This is not an optimization, this is a
2044 // requirement that is due to the fact that we could have this:
2045 //
2046 // %list = type { %list * }
2047 // %list = type { %list * } ; repeated type decl
2048 //
2049 // If types are not resolved eagerly, then the two types will not be
2050 // determined to be the same type!
2051 //
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00002052 ResolveTypeTo($1, *$3);
Chris Lattnerd50fa5a2001-10-22 05:56:09 +00002053
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00002054 if (!setTypeName(*$3, $1) && !$1) {
Reid Spencer309080a2006-09-28 19:28:24 +00002055 CHECK_FOR_ERROR
Chris Lattner85a351f2004-07-13 08:10:10 +00002056 // If this is a named type that is not a redefinition, add it to the slot
2057 // table.
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00002058 CurModule.Types.push_back(*$3);
Chris Lattneraa534bf2001-09-07 16:35:17 +00002059 }
Reid Spencer812724e2006-12-03 05:45:44 +00002060
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00002061 delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00002062 CHECK_FOR_ERROR
Chris Lattneraa534bf2001-09-07 16:35:17 +00002063 }
Reid Spencer791b8ef2007-01-26 08:04:51 +00002064 | OptLocalAssign TYPE VOID {
Reid Spencerfa35bb32006-12-31 05:40:12 +00002065 ResolveTypeTo($1, $3);
2066
2067 if (!setTypeName($3, $1) && !$1) {
2068 CHECK_FOR_ERROR
2069 // If this is a named type that is not a redefinition, add it to the slot
2070 // table.
2071 CurModule.Types.push_back($3);
2072 }
2073 CHECK_FOR_ERROR
2074 }
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +00002075 | OptGlobalAssign GVVisibilityStyle ThreadLocal GlobalType ConstVal {
Reid Spencer791b8ef2007-01-26 08:04:51 +00002076 /* "Externally Visible" Linkage */
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002077 if ($5 == 0)
Reid Spencerde1d05f2007-02-05 10:16:10 +00002078 GEN_ERROR("Global value initializer is not a constant");
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +00002079 CurGV = ParseGlobalVariable($1, GlobalValue::ExternalLinkage,
2080 $2, $4, $5->getType(), $5, $3);
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00002081 CHECK_FOR_ERROR
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002082 } GlobalVarAttributes {
2083 CurGV = 0;
2084 }
Chris Lattnereee35bf2007-04-26 05:30:35 +00002085 | OptGlobalAssign GVInternalLinkage GVVisibilityStyle ThreadLocal GlobalType
2086 ConstVal {
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +00002087 if ($6 == 0)
2088 GEN_ERROR("Global value initializer is not a constant");
2089 CurGV = ParseGlobalVariable($1, $2, $3, $5, $6->getType(), $6, $4);
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002090 CHECK_FOR_ERROR
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +00002091 } GlobalVarAttributes {
2092 CurGV = 0;
2093 }
Chris Lattnereee35bf2007-04-26 05:30:35 +00002094 | OptGlobalAssign GVExternalLinkage GVVisibilityStyle ThreadLocal GlobalType
2095 Types {
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +00002096 if (!UpRefs.empty())
2097 GEN_ERROR("Invalid upreference in type: " + (*$6)->getDescription());
2098 CurGV = ParseGlobalVariable($1, $2, $3, $5, *$6, 0, $4);
2099 CHECK_FOR_ERROR
2100 delete $6;
Reid Spencer309080a2006-09-28 19:28:24 +00002101 } GlobalVarAttributes {
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00002102 CurGV = 0;
2103 CHECK_FOR_ERROR
2104 }
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +00002105 | OptGlobalAssign GVVisibilityStyle ALIAS AliasLinkage AliaseeRef {
Reid Spencer3b208cc2007-05-22 18:52:21 +00002106 std::string Name;
2107 if ($1) {
2108 Name = *$1;
2109 delete $1;
2110 }
Anton Korobeynikova97b6942007-04-25 14:27:10 +00002111 if (Name.empty())
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +00002112 GEN_ERROR("Alias name cannot be empty");
2113
2114 Constant* Aliasee = $5;
2115 if (Aliasee == 0)
Reid Spencer3b208cc2007-05-22 18:52:21 +00002116 GEN_ERROR(std::string("Invalid aliasee for alias: ") + Name);
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +00002117
2118 GlobalAlias* GA = new GlobalAlias(Aliasee->getType(), $4, Name, Aliasee,
2119 CurModule.CurrentModule);
2120 GA->setVisibility($2);
2121 InsertValue(GA, CurModule.Values);
Anton Korobeynikova97b6942007-04-25 14:27:10 +00002122 CHECK_FOR_ERROR
Anton Korobeynikova97b6942007-04-25 14:27:10 +00002123 }
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00002124 | TARGET TargetDefinition {
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00002125 CHECK_FOR_ERROR
2126 }
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00002127 | DEPLIBS '=' LibrariesDefinition {
Reid Spencer713eedc2006-08-18 08:43:06 +00002128 CHECK_FOR_ERROR
Chris Lattner56f73d42001-07-14 06:10:16 +00002129 }
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00002130 ;
Chris Lattner2f7c9632001-06-06 20:29:01 +00002131
2132
Chris Lattnerf09741f2006-01-23 23:05:15 +00002133AsmBlock : STRINGCONSTANT {
Chris Lattner8ebd2162006-01-24 04:14:29 +00002134 const std::string &AsmSoFar = CurModule.CurrentModule->getModuleInlineAsm();
Chris Lattnerf09741f2006-01-23 23:05:15 +00002135 if (AsmSoFar.empty())
Reid Spencer3b208cc2007-05-22 18:52:21 +00002136 CurModule.CurrentModule->setModuleInlineAsm(*$1);
Chris Lattnerf09741f2006-01-23 23:05:15 +00002137 else
Reid Spencer3b208cc2007-05-22 18:52:21 +00002138 CurModule.CurrentModule->setModuleInlineAsm(AsmSoFar+"\n"+*$1);
2139 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002140 CHECK_FOR_ERROR
Chris Lattnerf09741f2006-01-23 23:05:15 +00002141};
Chris Lattner20126132003-04-22 19:07:06 +00002142
Reid Spencer791b8ef2007-01-26 08:04:51 +00002143TargetDefinition : TRIPLE '=' STRINGCONSTANT {
Reid Spencer3b208cc2007-05-22 18:52:21 +00002144 CurModule.CurrentModule->setTargetTriple(*$3);
2145 delete $3;
John Criswell6af0b122006-10-24 19:09:48 +00002146 }
Chris Lattnerfd3d29a2006-10-22 06:07:41 +00002147 | DATALAYOUT '=' STRINGCONSTANT {
Reid Spencer3b208cc2007-05-22 18:52:21 +00002148 CurModule.CurrentModule->setDataLayout(*$3);
2149 delete $3;
Owen Andersone2237542006-10-18 02:21:12 +00002150 };
Chris Lattner20126132003-04-22 19:07:06 +00002151
Reid Spencer62c6da92004-07-25 21:30:51 +00002152LibrariesDefinition : '[' LibList ']';
Reid Spencera24de0d2004-07-25 17:58:28 +00002153
2154LibList : LibList ',' STRINGCONSTANT {
Reid Spencer3b208cc2007-05-22 18:52:21 +00002155 CurModule.CurrentModule->addLibrary(*$3);
2156 delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00002157 CHECK_FOR_ERROR
Reid Spencera24de0d2004-07-25 17:58:28 +00002158 }
2159 | STRINGCONSTANT {
Reid Spencer3b208cc2007-05-22 18:52:21 +00002160 CurModule.CurrentModule->addLibrary(*$1);
2161 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002162 CHECK_FOR_ERROR
Reid Spencera24de0d2004-07-25 17:58:28 +00002163 }
2164 | /* empty: end of list */ {
Reid Spencer713eedc2006-08-18 08:43:06 +00002165 CHECK_FOR_ERROR
Reid Spencera24de0d2004-07-25 17:58:28 +00002166 }
2167 ;
Chris Lattner20126132003-04-22 19:07:06 +00002168
Chris Lattner2f7c9632001-06-06 20:29:01 +00002169//===----------------------------------------------------------------------===//
Chris Lattner57698e22002-03-26 18:01:55 +00002170// Rules to match Function Headers
Chris Lattner2f7c9632001-06-06 20:29:01 +00002171//===----------------------------------------------------------------------===//
2172
Reid Spencer791b8ef2007-01-26 08:04:51 +00002173ArgListH : ArgListH ',' Types OptParamAttrs OptLocalName {
Reid Spencerfa35bb32006-12-31 05:40:12 +00002174 if (!UpRefs.empty())
2175 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
2176 if (*$3 == Type::VoidTy)
Reid Spencerde1d05f2007-02-05 10:16:10 +00002177 GEN_ERROR("void typed arguments are invalid");
Reid Spencerfa35bb32006-12-31 05:40:12 +00002178 ArgListEntry E; E.Attrs = $4; E.Ty = $3; E.Name = $5;
Chris Lattner149376d2002-10-13 20:57:00 +00002179 $$ = $1;
Reid Spencerfa35bb32006-12-31 05:40:12 +00002180 $1->push_back(E);
Reid Spencer713eedc2006-08-18 08:43:06 +00002181 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00002182 }
Reid Spencer791b8ef2007-01-26 08:04:51 +00002183 | Types OptParamAttrs OptLocalName {
Reid Spencerfa35bb32006-12-31 05:40:12 +00002184 if (!UpRefs.empty())
2185 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2186 if (*$1 == Type::VoidTy)
Reid Spencerde1d05f2007-02-05 10:16:10 +00002187 GEN_ERROR("void typed arguments are invalid");
Reid Spencerfa35bb32006-12-31 05:40:12 +00002188 ArgListEntry E; E.Attrs = $2; E.Ty = $1; E.Name = $3;
2189 $$ = new ArgListType;
2190 $$->push_back(E);
Reid Spencer713eedc2006-08-18 08:43:06 +00002191 CHECK_FOR_ERROR
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00002192 };
Chris Lattner2f7c9632001-06-06 20:29:01 +00002193
2194ArgList : ArgListH {
2195 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002196 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00002197 }
Chris Lattner149376d2002-10-13 20:57:00 +00002198 | ArgListH ',' DOTDOTDOT {
2199 $$ = $1;
Reid Spencerfa35bb32006-12-31 05:40:12 +00002200 struct ArgListEntry E;
2201 E.Ty = new PATypeHolder(Type::VoidTy);
2202 E.Name = 0;
Reid Spencera472f662007-04-11 02:44:20 +00002203 E.Attrs = ParamAttr::None;
Reid Spencerfa35bb32006-12-31 05:40:12 +00002204 $$->push_back(E);
Reid Spencer713eedc2006-08-18 08:43:06 +00002205 CHECK_FOR_ERROR
Chris Lattner149376d2002-10-13 20:57:00 +00002206 }
2207 | DOTDOTDOT {
Reid Spencerfa35bb32006-12-31 05:40:12 +00002208 $$ = new ArgListType;
2209 struct ArgListEntry E;
2210 E.Ty = new PATypeHolder(Type::VoidTy);
2211 E.Name = 0;
Reid Spencera472f662007-04-11 02:44:20 +00002212 E.Attrs = ParamAttr::None;
Reid Spencerfa35bb32006-12-31 05:40:12 +00002213 $$->push_back(E);
Reid Spencer713eedc2006-08-18 08:43:06 +00002214 CHECK_FOR_ERROR
Chris Lattner149376d2002-10-13 20:57:00 +00002215 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00002216 | /* empty */ {
2217 $$ = 0;
Reid Spencer713eedc2006-08-18 08:43:06 +00002218 CHECK_FOR_ERROR
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00002219 };
Chris Lattner2f7c9632001-06-06 20:29:01 +00002220
Reid Spencer791b8ef2007-01-26 08:04:51 +00002221FunctionHeaderH : OptCallingConv ResultTypes GlobalName '(' ArgList ')'
Reid Spencer136a91c2007-01-05 17:06:19 +00002222 OptFuncAttrs OptSection OptAlign {
Reid Spencer3b208cc2007-05-22 18:52:21 +00002223 std::string FunctionName(*$3);
2224 delete $3; // Free strdup'd memory!
Chris Lattner841d8b92001-11-26 18:54:16 +00002225
Reid Spencer6130ae02007-01-02 21:53:43 +00002226 // Check the function result for abstractness if this is a define. We should
2227 // have no abstract types at this point
Reid Spencer136a91c2007-01-05 17:06:19 +00002228 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved($2))
2229 GEN_ERROR("Reference to abstract result: "+ $2->get()->getDescription());
Reid Spencer6130ae02007-01-02 21:53:43 +00002230
Chris Lattner89da8a32003-04-22 18:42:41 +00002231 std::vector<const Type*> ParamTypeList;
Reid Spencer4388f0b2007-04-22 05:46:44 +00002232 ParamAttrsVector Attrs;
2233 if ($7 != ParamAttr::None) {
2234 ParamAttrsWithIndex PAWI; PAWI.index = 0; PAWI.attrs = $7;
2235 Attrs.push_back(PAWI);
2236 }
Chris Lattner53bdd312005-05-06 20:27:19 +00002237 if ($5) { // If there are arguments...
Reid Spencer71b79e32007-04-09 06:17:21 +00002238 unsigned index = 1;
2239 for (ArgListType::iterator I = $5->begin(); I != $5->end(); ++I, ++index) {
Reid Spencerfa35bb32006-12-31 05:40:12 +00002240 const Type* Ty = I->Ty->get();
Reid Spencer6130ae02007-01-02 21:53:43 +00002241 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved(I->Ty))
2242 GEN_ERROR("Reference to abstract argument: " + Ty->getDescription());
Reid Spencerfa35bb32006-12-31 05:40:12 +00002243 ParamTypeList.push_back(Ty);
2244 if (Ty != Type::VoidTy)
Reid Spencer4388f0b2007-04-22 05:46:44 +00002245 if (I->Attrs != ParamAttr::None) {
2246 ParamAttrsWithIndex PAWI; PAWI.index = index; PAWI.attrs = I->Attrs;
2247 Attrs.push_back(PAWI);
2248 }
Reid Spencerfa35bb32006-12-31 05:40:12 +00002249 }
Chris Lattner19613292002-10-15 21:41:14 +00002250 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00002251
Chris Lattnerf26e0d92001-10-13 06:41:08 +00002252 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
2253 if (isVarArg) ParamTypeList.pop_back();
2254
Reid Spencer4388f0b2007-04-22 05:46:44 +00002255 ParamAttrsList *PAL = 0;
2256 if (!Attrs.empty())
2257 PAL = ParamAttrsList::get(Attrs);
Reid Spencer71b79e32007-04-09 06:17:21 +00002258
Reid Spencer78517652007-04-28 16:06:50 +00002259 FunctionType *FT = FunctionType::get(*$2, ParamTypeList, isVarArg, PAL);
Chris Lattner19613292002-10-15 21:41:14 +00002260 const PointerType *PFT = PointerType::get(FT);
Reid Spencer136a91c2007-01-05 17:06:19 +00002261 delete $2;
Chris Lattner2f7c9632001-06-06 20:29:01 +00002262
Chris Lattner8abe1a12004-07-14 23:03:46 +00002263 ValID ID;
2264 if (!FunctionName.empty()) {
Reid Spencer791b8ef2007-01-26 08:04:51 +00002265 ID = ValID::createGlobalName((char*)FunctionName.c_str());
Chris Lattner8abe1a12004-07-14 23:03:46 +00002266 } else {
Reid Spencerfe65ae82007-03-19 18:39:36 +00002267 ID = ValID::createGlobalID(CurModule.Values.size());
Chris Lattner8abe1a12004-07-14 23:03:46 +00002268 }
2269
Chris Lattner91393ee2004-07-14 19:33:47 +00002270 Function *Fn = 0;
Chris Lattner8abe1a12004-07-14 23:03:46 +00002271 // See if this function was forward referenced. If so, recycle the object.
Reid Spencer78517652007-04-28 16:06:50 +00002272 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
Chris Lattner8abe1a12004-07-14 23:03:46 +00002273 // Move the function to the end of the list, from whereever it was
2274 // previously inserted.
2275 Fn = cast<Function>(FWRef);
2276 CurModule.CurrentModule->getFunctionList().remove(Fn);
2277 CurModule.CurrentModule->getFunctionList().push_back(Fn);
2278 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002279 (Fn = CurModule.CurrentModule->getFunction(FunctionName))) {
Reid Spencer78517652007-04-28 16:06:50 +00002280 if (Fn->getFunctionType() != FT) {
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002281 // The existing function doesn't have the same type. This is an overload
2282 // error.
2283 GEN_ERROR("Overload of function '" + FunctionName + "' not permitted.");
2284 } else if (!CurFun.isDeclare && !Fn->isDeclaration()) {
2285 // Neither the existing or the current function is a declaration and they
2286 // have the same name and same type. Clearly this is a redefinition.
Reid Spencerde1d05f2007-02-05 10:16:10 +00002287 GEN_ERROR("Redefinition of function '" + FunctionName + "'");
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002288 } if (Fn->isDeclaration()) {
2289 // Make sure to strip off any argument names so we can't get conflicts.
Chris Lattner531f9e92005-03-15 04:54:21 +00002290 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
Chris Lattner8abe1a12004-07-14 23:03:46 +00002291 AI != AE; ++AI)
2292 AI->setName("");
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002293 }
Chris Lattner91393ee2004-07-14 19:33:47 +00002294 } else { // Not already defined?
Chris Lattnereee35bf2007-04-26 05:30:35 +00002295 Fn = new Function(FT, GlobalValue::ExternalWeakLinkage, FunctionName,
Chris Lattner91393ee2004-07-14 19:33:47 +00002296 CurModule.CurrentModule);
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00002297
Chris Lattner91393ee2004-07-14 19:33:47 +00002298 InsertValue(Fn, CurModule.Values);
Chris Lattner91393ee2004-07-14 19:33:47 +00002299 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00002300
Chris Lattner61877742003-10-16 20:12:13 +00002301 CurFun.FunctionStart(Fn);
Anton Korobeynikov0ab01ff2006-09-17 13:06:18 +00002302
2303 if (CurFun.isDeclare) {
2304 // If we have declaration, always overwrite linkage. This will allow us to
2305 // correctly handle cases, when pointer to function is passed as argument to
2306 // another function.
2307 Fn->setLinkage(CurFun.Linkage);
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002308 Fn->setVisibility(CurFun.Visibility);
Anton Korobeynikov0ab01ff2006-09-17 13:06:18 +00002309 }
Chris Lattner53bdd312005-05-06 20:27:19 +00002310 Fn->setCallingConv($1);
Reid Spencer136a91c2007-01-05 17:06:19 +00002311 Fn->setAlignment($9);
2312 if ($8) {
Reid Spencer3b208cc2007-05-22 18:52:21 +00002313 Fn->setSection(*$8);
2314 delete $8;
Chris Lattner71b936c2005-11-12 00:11:10 +00002315 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00002316
Chris Lattner113f4f42002-06-25 16:13:24 +00002317 // Add all of the arguments we parsed to the function...
Chris Lattner53bdd312005-05-06 20:27:19 +00002318 if ($5) { // Is null if empty...
Chris Lattner149376d2002-10-13 20:57:00 +00002319 if (isVarArg) { // Nuke the last entry
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002320 assert($5->back().Ty->get() == Type::VoidTy && $5->back().Name == 0 &&
Reid Spencer9894d6c2007-02-05 17:01:20 +00002321 "Not a varargs marker!");
Reid Spencerfa35bb32006-12-31 05:40:12 +00002322 delete $5->back().Ty;
Chris Lattner53bdd312005-05-06 20:27:19 +00002323 $5->pop_back(); // Delete the last entry
Chris Lattner149376d2002-10-13 20:57:00 +00002324 }
Chris Lattner531f9e92005-03-15 04:54:21 +00002325 Function::arg_iterator ArgIt = Fn->arg_begin();
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002326 Function::arg_iterator ArgEnd = Fn->arg_end();
Reid Spencerfa35bb32006-12-31 05:40:12 +00002327 unsigned Idx = 1;
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002328 for (ArgListType::iterator I = $5->begin();
2329 I != $5->end() && ArgIt != ArgEnd; ++I, ++ArgIt) {
Reid Spencerfa35bb32006-12-31 05:40:12 +00002330 delete I->Ty; // Delete the typeholder...
Reid Spencer3b208cc2007-05-22 18:52:21 +00002331 setValueName(ArgIt, I->Name); // Insert arg into symtab...
Reid Spencer309080a2006-09-28 19:28:24 +00002332 CHECK_FOR_ERROR
Chris Lattner149376d2002-10-13 20:57:00 +00002333 InsertValue(ArgIt);
Reid Spencerfa35bb32006-12-31 05:40:12 +00002334 Idx++;
Chris Lattner2f7c9632001-06-06 20:29:01 +00002335 }
Reid Spencer812724e2006-12-03 05:45:44 +00002336
Chris Lattner53bdd312005-05-06 20:27:19 +00002337 delete $5; // We're now done with the argument list
Chris Lattner2f7c9632001-06-06 20:29:01 +00002338 }
Reid Spencer713eedc2006-08-18 08:43:06 +00002339 CHECK_FOR_ERROR
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00002340};
Chris Lattner2f7c9632001-06-06 20:29:01 +00002341
Chris Lattner1e1a9b42002-05-03 18:23:48 +00002342BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
2343
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002344FunctionHeader : FunctionDefineLinkage GVVisibilityStyle FunctionHeaderH BEGIN {
Chris Lattner61877742003-10-16 20:12:13 +00002345 $$ = CurFun.CurrentFunction;
Chris Lattneraa534bf2001-09-07 16:35:17 +00002346
Chris Lattner379a8d22003-04-16 20:28:45 +00002347 // Make sure that we keep track of the linkage type even if there was a
2348 // previous "declare".
2349 $$->setLinkage($1);
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002350 $$->setVisibility($2);
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00002351};
Chris Lattner2f7c9632001-06-06 20:29:01 +00002352
Chris Lattner1e1a9b42002-05-03 18:23:48 +00002353END : ENDTOK | '}'; // Allow end of '}' to end a function
2354
Chris Lattner57698e22002-03-26 18:01:55 +00002355Function : BasicBlockList END {
Chris Lattner2f7c9632001-06-06 20:29:01 +00002356 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002357 CHECK_FOR_ERROR
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00002358};
Chris Lattner2f7c9632001-06-06 20:29:01 +00002359
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002360FunctionProto : FunctionDeclareLinkage GVVisibilityStyle FunctionHeaderH {
Reid Spencerfa35bb32006-12-31 05:40:12 +00002361 CurFun.CurrentFunction->setLinkage($1);
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002362 CurFun.CurrentFunction->setVisibility($2);
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00002363 $$ = CurFun.CurrentFunction;
2364 CurFun.FunctionDone();
2365 CHECK_FOR_ERROR
2366 };
Chris Lattner2f7c9632001-06-06 20:29:01 +00002367
2368//===----------------------------------------------------------------------===//
2369// Rules to match Basic Blocks
2370//===----------------------------------------------------------------------===//
2371
Chris Lattnera02d6032006-01-25 22:26:43 +00002372OptSideEffect : /* empty */ {
2373 $$ = false;
Reid Spencer713eedc2006-08-18 08:43:06 +00002374 CHECK_FOR_ERROR
Chris Lattnera02d6032006-01-25 22:26:43 +00002375 }
2376 | SIDEEFFECT {
2377 $$ = true;
Reid Spencer713eedc2006-08-18 08:43:06 +00002378 CHECK_FOR_ERROR
Chris Lattnera02d6032006-01-25 22:26:43 +00002379 };
2380
Chris Lattner2f7c9632001-06-06 20:29:01 +00002381ConstValueRef : ESINT64VAL { // A reference to a direct constant
2382 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002383 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00002384 }
2385 | EUINT64VAL {
2386 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002387 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00002388 }
Chris Lattner212f70d2001-07-15 00:17:01 +00002389 | FPVAL { // Perhaps it's an FP constant?
2390 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002391 CHECK_FOR_ERROR
Chris Lattner212f70d2001-07-15 00:17:01 +00002392 }
Chris Lattnerc0ba90e2004-03-31 03:49:47 +00002393 | TRUETOK {
Zhou Sheng75b871f2007-01-11 12:24:14 +00002394 $$ = ValID::create(ConstantInt::getTrue());
Reid Spencer713eedc2006-08-18 08:43:06 +00002395 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00002396 }
Chris Lattnerc0ba90e2004-03-31 03:49:47 +00002397 | FALSETOK {
Zhou Sheng75b871f2007-01-11 12:24:14 +00002398 $$ = ValID::create(ConstantInt::getFalse());
Reid Spencer713eedc2006-08-18 08:43:06 +00002399 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00002400 }
Chris Lattnerfbdec252001-09-30 22:46:54 +00002401 | NULL_TOK {
2402 $$ = ValID::createNull();
Reid Spencer713eedc2006-08-18 08:43:06 +00002403 CHECK_FOR_ERROR
Vikram S. Adve7064eaf2002-07-14 22:59:28 +00002404 }
Chris Lattner4ff31492004-10-16 18:17:13 +00002405 | UNDEF {
2406 $$ = ValID::createUndef();
Reid Spencer713eedc2006-08-18 08:43:06 +00002407 CHECK_FOR_ERROR
Chris Lattner4ff31492004-10-16 18:17:13 +00002408 }
Chris Lattner8c7bda52005-12-21 17:53:02 +00002409 | ZEROINITIALIZER { // A vector zero constant.
2410 $$ = ValID::createZeroInit();
Reid Spencer713eedc2006-08-18 08:43:06 +00002411 CHECK_FOR_ERROR
Chris Lattner8c7bda52005-12-21 17:53:02 +00002412 }
Brian Gaeke02209042004-08-20 06:00:58 +00002413 | '<' ConstVector '>' { // Nonempty unsized packed vector
Reid Spencer812724e2006-12-03 05:45:44 +00002414 const Type *ETy = (*$2)[0]->getType();
Brian Gaeke02209042004-08-20 06:00:58 +00002415 int NumElements = $2->size();
2416
Reid Spencerd84d35b2007-02-15 02:26:10 +00002417 VectorType* pt = VectorType::get(ETy, NumElements);
Brian Gaeke02209042004-08-20 06:00:58 +00002418 PATypeHolder* PTy = new PATypeHolder(
Reid Spencer812724e2006-12-03 05:45:44 +00002419 HandleUpRefs(
Reid Spencerd84d35b2007-02-15 02:26:10 +00002420 VectorType::get(
Reid Spencer812724e2006-12-03 05:45:44 +00002421 ETy,
2422 NumElements)
2423 )
2424 );
Brian Gaeke02209042004-08-20 06:00:58 +00002425
2426 // Verify all elements are correct type!
2427 for (unsigned i = 0; i < $2->size(); i++) {
Reid Spencer812724e2006-12-03 05:45:44 +00002428 if (ETy != (*$2)[i]->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00002429 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Brian Gaeke02209042004-08-20 06:00:58 +00002430 ETy->getDescription() +"' as required!\nIt is of type '" +
Reid Spencer812724e2006-12-03 05:45:44 +00002431 (*$2)[i]->getType()->getDescription() + "'.");
Brian Gaeke02209042004-08-20 06:00:58 +00002432 }
2433
Reid Spencerd84d35b2007-02-15 02:26:10 +00002434 $$ = ValID::create(ConstantVector::get(pt, *$2));
Brian Gaeke02209042004-08-20 06:00:58 +00002435 delete PTy; delete $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00002436 CHECK_FOR_ERROR
Brian Gaeke02209042004-08-20 06:00:58 +00002437 }
Chris Lattner7f325cd2002-08-16 21:14:40 +00002438 | ConstExpr {
Reid Spencer812724e2006-12-03 05:45:44 +00002439 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002440 CHECK_FOR_ERROR
Chris Lattnera02d6032006-01-25 22:26:43 +00002441 }
2442 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
Reid Spencer3b208cc2007-05-22 18:52:21 +00002443 $$ = ValID::createInlineAsm(*$3, *$5, $2);
2444 delete $3;
2445 delete $5;
Reid Spencer713eedc2006-08-18 08:43:06 +00002446 CHECK_FOR_ERROR
Chris Lattner7f325cd2002-08-16 21:14:40 +00002447 };
Chris Lattnerfbdec252001-09-30 22:46:54 +00002448
Chris Lattnerf26e0d92001-10-13 06:41:08 +00002449// SymbolicValueRef - Reference to one of two ways of symbolically refering to
2450// another value.
2451//
Reid Spencer791b8ef2007-01-26 08:04:51 +00002452SymbolicValueRef : LOCALVAL_ID { // Is it an integer reference...?
2453 $$ = ValID::createLocalID($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002454 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00002455 }
Reid Spencer791b8ef2007-01-26 08:04:51 +00002456 | GLOBALVAL_ID {
2457 $$ = ValID::createGlobalID($1);
2458 CHECK_FOR_ERROR
2459 }
2460 | LocalName { // Is it a named reference...?
Reid Spencer3b208cc2007-05-22 18:52:21 +00002461 $$ = ValID::createLocalName(*$1);
2462 delete $1;
Reid Spencer791b8ef2007-01-26 08:04:51 +00002463 CHECK_FOR_ERROR
2464 }
2465 | GlobalName { // Is it a named reference...?
Reid Spencer3b208cc2007-05-22 18:52:21 +00002466 $$ = ValID::createGlobalName(*$1);
2467 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002468 CHECK_FOR_ERROR
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00002469 };
Chris Lattnerf26e0d92001-10-13 06:41:08 +00002470
2471// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00002472ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattnerf26e0d92001-10-13 06:41:08 +00002473
Chris Lattner2f7c9632001-06-06 20:29:01 +00002474
Chris Lattner252afba2001-07-26 16:29:15 +00002475// ResolvedVal - a <type> <value> pair. This is used only in cases where the
2476// type immediately preceeds the value reference, and allows complex constant
2477// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattner571a6b02001-10-03 01:49:25 +00002478ResolvedVal : Types ValueRef {
Reid Spencerfa35bb32006-12-31 05:40:12 +00002479 if (!UpRefs.empty())
2480 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2481 $$ = getVal(*$1, $2);
2482 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002483 CHECK_FOR_ERROR
Reid Spencerfa35bb32006-12-31 05:40:12 +00002484 }
2485 ;
Chris Lattner42b5a8a2001-07-25 22:47:46 +00002486
Chris Lattner2f7c9632001-06-06 20:29:01 +00002487BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner2ed776b2004-07-13 07:59:27 +00002488 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002489 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00002490 }
Chris Lattner113f4f42002-06-25 16:13:24 +00002491 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
Chris Lattner2ed776b2004-07-13 07:59:27 +00002492 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002493 CHECK_FOR_ERROR
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00002494 };
Chris Lattner2f7c9632001-06-06 20:29:01 +00002495
2496
2497// Basic blocks are terminated by branching instructions:
2498// br, br/cc, switch, ret
2499//
Reid Spencer791b8ef2007-01-26 08:04:51 +00002500BasicBlock : InstructionList OptLocalAssign BBTerminatorInst {
Chris Lattner2ed776b2004-07-13 07:59:27 +00002501 setValueName($3, $2);
Reid Spencer309080a2006-09-28 19:28:24 +00002502 CHECK_FOR_ERROR
Chris Lattnerf26e0d92001-10-13 06:41:08 +00002503 InsertValue($3);
Chris Lattnerf26e0d92001-10-13 06:41:08 +00002504 $1->getInstList().push_back($3);
Chris Lattner2f7c9632001-06-06 20:29:01 +00002505 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002506 CHECK_FOR_ERROR
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00002507 };
Chris Lattner2f7c9632001-06-06 20:29:01 +00002508
2509InstructionList : InstructionList Inst {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002510 if (CastInst *CI1 = dyn_cast<CastInst>($2))
2511 if (CastInst *CI2 = dyn_cast<CastInst>(CI1->getOperand(0)))
2512 if (CI2->getParent() == 0)
2513 $1->getInstList().push_back(CI2);
Chris Lattner2f7c9632001-06-06 20:29:01 +00002514 $1->getInstList().push_back($2);
2515 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002516 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00002517 }
Reid Spencerfe65ae82007-03-19 18:39:36 +00002518 | /* empty */ { // Empty space between instruction lists
2519 $$ = defineBBVal(ValID::createLocalID(CurFun.NextValNum));
Reid Spencer713eedc2006-08-18 08:43:06 +00002520 CHECK_FOR_ERROR
Chris Lattnere84a2ba2004-07-13 08:39:15 +00002521 }
Reid Spencerfe65ae82007-03-19 18:39:36 +00002522 | LABELSTR { // Labelled (named) basic block
Reid Spencer3b208cc2007-05-22 18:52:21 +00002523 $$ = defineBBVal(ValID::createLocalName(*$1));
2524 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002525 CHECK_FOR_ERROR
Reid Spencer3b208cc2007-05-22 18:52:21 +00002526
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00002527 };
Chris Lattner2f7c9632001-06-06 20:29:01 +00002528
Chris Lattner252afba2001-07-26 16:29:15 +00002529BBTerminatorInst : RET ResolvedVal { // Return with a result...
Reid Spencer812724e2006-12-03 05:45:44 +00002530 $$ = new ReturnInst($2);
Reid Spencer713eedc2006-08-18 08:43:06 +00002531 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00002532 }
Reid Spencerfe65ae82007-03-19 18:39:36 +00002533 | RET VOID { // Return with no result...
Chris Lattner2f7c9632001-06-06 20:29:01 +00002534 $$ = new ReturnInst();
Reid Spencer713eedc2006-08-18 08:43:06 +00002535 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00002536 }
Reid Spencerfe65ae82007-03-19 18:39:36 +00002537 | BR LABEL ValueRef { // Unconditional Branch...
Reid Spencer309080a2006-09-28 19:28:24 +00002538 BasicBlock* tmpBB = getBBVal($3);
Reid Spencer713eedc2006-08-18 08:43:06 +00002539 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002540 $$ = new BranchInst(tmpBB);
Reid Spencerfe65ae82007-03-19 18:39:36 +00002541 } // Conditional Branch...
Reid Spencer502d64e2007-01-13 05:00:20 +00002542 | BR INTTYPE ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
2543 assert(cast<IntegerType>($2)->getBitWidth() == 1 && "Not Bool?");
Reid Spencer309080a2006-09-28 19:28:24 +00002544 BasicBlock* tmpBBA = getBBVal($6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002545 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002546 BasicBlock* tmpBBB = getBBVal($9);
2547 CHECK_FOR_ERROR
Reid Spencer542964f2007-01-11 18:21:29 +00002548 Value* tmpVal = getVal(Type::Int1Ty, $3);
Reid Spencer309080a2006-09-28 19:28:24 +00002549 CHECK_FOR_ERROR
2550 $$ = new BranchInst(tmpBBA, tmpBBB, tmpVal);
Chris Lattner2f7c9632001-06-06 20:29:01 +00002551 }
2552 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencer812724e2006-12-03 05:45:44 +00002553 Value* tmpVal = getVal($2, $3);
Reid Spencer309080a2006-09-28 19:28:24 +00002554 CHECK_FOR_ERROR
2555 BasicBlock* tmpBB = getBBVal($6);
2556 CHECK_FOR_ERROR
2557 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, $8->size());
Chris Lattner2f7c9632001-06-06 20:29:01 +00002558 $$ = S;
2559
Chris Lattner89da8a32003-04-22 18:42:41 +00002560 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
Chris Lattnerae234e12002-04-09 19:41:42 +00002561 E = $8->end();
Chris Lattnerfc824c12005-02-24 05:25:17 +00002562 for (; I != E; ++I) {
2563 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
2564 S->addCase(CI, I->second);
2565 else
Reid Spencerde1d05f2007-02-05 10:16:10 +00002566 GEN_ERROR("Switch case is constant, but not a simple integer");
Chris Lattnerfc824c12005-02-24 05:25:17 +00002567 }
Chris Lattnerca96cee2004-04-17 23:49:15 +00002568 delete $8;
Reid Spencer713eedc2006-08-18 08:43:06 +00002569 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00002570 }
Chris Lattner9f3648b2003-05-15 21:30:00 +00002571 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencer812724e2006-12-03 05:45:44 +00002572 Value* tmpVal = getVal($2, $3);
Reid Spencer309080a2006-09-28 19:28:24 +00002573 CHECK_FOR_ERROR
2574 BasicBlock* tmpBB = getBBVal($6);
2575 CHECK_FOR_ERROR
2576 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, 0);
Chris Lattner9f3648b2003-05-15 21:30:00 +00002577 $$ = S;
Reid Spencer713eedc2006-08-18 08:43:06 +00002578 CHECK_FOR_ERROR
Chris Lattner9f3648b2003-05-15 21:30:00 +00002579 }
Reid Spencer136a91c2007-01-05 17:06:19 +00002580 | INVOKE OptCallingConv ResultTypes ValueRef '(' ValueRefList ')' OptFuncAttrs
Chris Lattner53bdd312005-05-06 20:27:19 +00002581 TO LABEL ValueRef UNWIND LABEL ValueRef {
Chris Lattnerf26e0d92001-10-13 06:41:08 +00002582
Reid Spencerfa35bb32006-12-31 05:40:12 +00002583 // Handle the short syntax
2584 const PointerType *PFTy = 0;
2585 const FunctionType *Ty = 0;
Reid Spencer136a91c2007-01-05 17:06:19 +00002586 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattner19613292002-10-15 21:41:14 +00002587 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattnerf26e0d92001-10-13 06:41:08 +00002588 // Pull out the types of all of the arguments...
Chris Lattner89da8a32003-04-22 18:42:41 +00002589 std::vector<const Type*> ParamTypes;
Reid Spencer78517652007-04-28 16:06:50 +00002590 ParamAttrsVector Attrs;
2591 if ($8 != ParamAttr::None) {
Chris Lattnercf40d502007-05-04 04:01:07 +00002592 ParamAttrsWithIndex PAWI; PAWI.index = 0; PAWI.attrs = $8;
Reid Spencer78517652007-04-28 16:06:50 +00002593 Attrs.push_back(PAWI);
2594 }
Reid Spencer71b79e32007-04-09 06:17:21 +00002595 ValueRefList::iterator I = $6->begin(), E = $6->end();
Reid Spencer78517652007-04-28 16:06:50 +00002596 unsigned index = 1;
2597 for (; I != E; ++I, ++index) {
Reid Spencerfa35bb32006-12-31 05:40:12 +00002598 const Type *Ty = I->Val->getType();
2599 if (Ty == Type::VoidTy)
2600 GEN_ERROR("Short call syntax cannot be used with varargs");
2601 ParamTypes.push_back(Ty);
Reid Spencer78517652007-04-28 16:06:50 +00002602 if (I->Attrs != ParamAttr::None) {
2603 ParamAttrsWithIndex PAWI; PAWI.index = index; PAWI.attrs = I->Attrs;
2604 Attrs.push_back(PAWI);
2605 }
Chris Lattnerf26e0d92001-10-13 06:41:08 +00002606 }
Reid Spencer78517652007-04-28 16:06:50 +00002607
2608 ParamAttrsList *PAL = 0;
2609 if (!Attrs.empty())
2610 PAL = ParamAttrsList::get(Attrs);
2611 Ty = FunctionType::get($3->get(), ParamTypes, false, PAL);
Chris Lattner19613292002-10-15 21:41:14 +00002612 PFTy = PointerType::get(Ty);
Chris Lattnerf26e0d92001-10-13 06:41:08 +00002613 }
Chris Lattnerf26e0d92001-10-13 06:41:08 +00002614
Reid Spencerd05bc522007-03-20 01:13:00 +00002615 delete $3;
2616
Chris Lattner53bdd312005-05-06 20:27:19 +00002617 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer309080a2006-09-28 19:28:24 +00002618 CHECK_FOR_ERROR
Reid Spencer136a91c2007-01-05 17:06:19 +00002619 BasicBlock *Normal = getBBVal($11);
Reid Spencer309080a2006-09-28 19:28:24 +00002620 CHECK_FOR_ERROR
Reid Spencer136a91c2007-01-05 17:06:19 +00002621 BasicBlock *Except = getBBVal($14);
Reid Spencer309080a2006-09-28 19:28:24 +00002622 CHECK_FOR_ERROR
Chris Lattnerf26e0d92001-10-13 06:41:08 +00002623
Reid Spencerfa35bb32006-12-31 05:40:12 +00002624 // Check the arguments
2625 ValueList Args;
2626 if ($6->empty()) { // Has no arguments?
2627 // Make sure no arguments is a good thing!
2628 if (Ty->getNumParams() != 0)
2629 GEN_ERROR("No arguments passed to a function that "
Reid Spencerde1d05f2007-02-05 10:16:10 +00002630 "expects arguments");
Chris Lattnerf26e0d92001-10-13 06:41:08 +00002631 } else { // Has arguments?
Chris Lattner57698e22002-03-26 18:01:55 +00002632 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattnerf26e0d92001-10-13 06:41:08 +00002633 // correctly!
Chris Lattnerfa829be2004-02-09 04:14:01 +00002634 FunctionType::param_iterator I = Ty->param_begin();
2635 FunctionType::param_iterator E = Ty->param_end();
Reid Spencerfa35bb32006-12-31 05:40:12 +00002636 ValueRefList::iterator ArgI = $6->begin(), ArgE = $6->end();
Reid Spencer78517652007-04-28 16:06:50 +00002637
Reid Spencerfa35bb32006-12-31 05:40:12 +00002638 for (; ArgI != ArgE && I != E; ++ArgI, ++I) {
2639 if (ArgI->Val->getType() != *I)
2640 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencerde1d05f2007-02-05 10:16:10 +00002641 (*I)->getDescription() + "'");
Reid Spencerfa35bb32006-12-31 05:40:12 +00002642 Args.push_back(ArgI->Val);
2643 }
Reid Spencer812724e2006-12-03 05:45:44 +00002644
Reid Spencerfa35bb32006-12-31 05:40:12 +00002645 if (Ty->isVarArg()) {
2646 if (I == E)
2647 for (; ArgI != ArgE; ++ArgI)
2648 Args.push_back(ArgI->Val); // push the remaining varargs
2649 } else if (I != E || ArgI != ArgE)
Reid Spencerde1d05f2007-02-05 10:16:10 +00002650 GEN_ERROR("Invalid number of parameters detected");
Chris Lattnerf26e0d92001-10-13 06:41:08 +00002651 }
Reid Spencerfa35bb32006-12-31 05:40:12 +00002652
2653 // Create the InvokeInst
Chris Lattnerd8028242007-02-13 05:53:56 +00002654 InvokeInst *II = new InvokeInst(V, Normal, Except, &Args[0], Args.size());
Reid Spencerfa35bb32006-12-31 05:40:12 +00002655 II->setCallingConv($2);
2656 $$ = II;
Chris Lattner53bdd312005-05-06 20:27:19 +00002657 delete $6;
Reid Spencer713eedc2006-08-18 08:43:06 +00002658 CHECK_FOR_ERROR
Chris Lattner9c58cf62003-09-08 18:54:55 +00002659 }
2660 | UNWIND {
2661 $$ = new UnwindInst();
Reid Spencer713eedc2006-08-18 08:43:06 +00002662 CHECK_FOR_ERROR
Chris Lattner4ff31492004-10-16 18:17:13 +00002663 }
2664 | UNREACHABLE {
2665 $$ = new UnreachableInst();
Reid Spencer713eedc2006-08-18 08:43:06 +00002666 CHECK_FOR_ERROR
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00002667 };
Chris Lattnerf26e0d92001-10-13 06:41:08 +00002668
2669
Chris Lattner2f7c9632001-06-06 20:29:01 +00002670
2671JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
2672 $$ = $1;
Reid Spencerfe65ae82007-03-19 18:39:36 +00002673 Constant *V = cast<Constant>(getExistingVal($2, $3));
Reid Spencer309080a2006-09-28 19:28:24 +00002674 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00002675 if (V == 0)
Reid Spencerde1d05f2007-02-05 10:16:10 +00002676 GEN_ERROR("May only switch on a constant pool value");
Chris Lattner2f7c9632001-06-06 20:29:01 +00002677
Reid Spencer309080a2006-09-28 19:28:24 +00002678 BasicBlock* tmpBB = getBBVal($6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002679 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002680 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner2f7c9632001-06-06 20:29:01 +00002681 }
2682 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner89da8a32003-04-22 18:42:41 +00002683 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Reid Spencerfe65ae82007-03-19 18:39:36 +00002684 Constant *V = cast<Constant>(getExistingVal($1, $2));
Reid Spencer309080a2006-09-28 19:28:24 +00002685 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00002686
2687 if (V == 0)
Reid Spencerde1d05f2007-02-05 10:16:10 +00002688 GEN_ERROR("May only switch on a constant pool value");
Chris Lattner2f7c9632001-06-06 20:29:01 +00002689
Reid Spencer309080a2006-09-28 19:28:24 +00002690 BasicBlock* tmpBB = getBBVal($5);
Reid Spencer713eedc2006-08-18 08:43:06 +00002691 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002692 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00002693 };
Chris Lattner2f7c9632001-06-06 20:29:01 +00002694
Reid Spencer791b8ef2007-01-26 08:04:51 +00002695Inst : OptLocalAssign InstVal {
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002696 // Is this definition named?? if so, assign the name...
2697 setValueName($2, $1);
2698 CHECK_FOR_ERROR
2699 InsertValue($2);
2700 $$ = $2;
2701 CHECK_FOR_ERROR
2702 };
2703
Chris Lattner2f7c9632001-06-06 20:29:01 +00002704
Chris Lattner931ef3b2001-06-11 15:04:20 +00002705PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Reid Spencerfa35bb32006-12-31 05:40:12 +00002706 if (!UpRefs.empty())
2707 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Chris Lattner89da8a32003-04-22 18:42:41 +00002708 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Reid Spencer812724e2006-12-03 05:45:44 +00002709 Value* tmpVal = getVal(*$1, $3);
Reid Spencer713eedc2006-08-18 08:43:06 +00002710 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002711 BasicBlock* tmpBB = getBBVal($5);
2712 CHECK_FOR_ERROR
2713 $$->push_back(std::make_pair(tmpVal, tmpBB));
Reid Spencer812724e2006-12-03 05:45:44 +00002714 delete $1;
Chris Lattner931ef3b2001-06-11 15:04:20 +00002715 }
2716 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
2717 $$ = $1;
Reid Spencer309080a2006-09-28 19:28:24 +00002718 Value* tmpVal = getVal($1->front().first->getType(), $4);
Reid Spencer713eedc2006-08-18 08:43:06 +00002719 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002720 BasicBlock* tmpBB = getBBVal($6);
2721 CHECK_FOR_ERROR
2722 $1->push_back(std::make_pair(tmpVal, tmpBB));
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00002723 };
Chris Lattner931ef3b2001-06-11 15:04:20 +00002724
2725
Reid Spencerfa35bb32006-12-31 05:40:12 +00002726ValueRefList : Types ValueRef OptParamAttrs {
2727 if (!UpRefs.empty())
2728 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2729 // Used for call and invoke instructions
2730 $$ = new ValueRefList();
2731 ValueRefListEntry E; E.Attrs = $3; E.Val = getVal($1->get(), $2);
2732 $$->push_back(E);
Reid Spencerd05bc522007-03-20 01:13:00 +00002733 delete $1;
Chris Lattner2f7c9632001-06-06 20:29:01 +00002734 }
Reid Spencerfa35bb32006-12-31 05:40:12 +00002735 | ValueRefList ',' Types ValueRef OptParamAttrs {
2736 if (!UpRefs.empty())
2737 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattner2f7c9632001-06-06 20:29:01 +00002738 $$ = $1;
Reid Spencerfa35bb32006-12-31 05:40:12 +00002739 ValueRefListEntry E; E.Attrs = $5; E.Val = getVal($3->get(), $4);
2740 $$->push_back(E);
Reid Spencerd05bc522007-03-20 01:13:00 +00002741 delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00002742 CHECK_FOR_ERROR
Reid Spencerfa35bb32006-12-31 05:40:12 +00002743 }
2744 | /*empty*/ { $$ = new ValueRefList(); };
Chris Lattner2f7c9632001-06-06 20:29:01 +00002745
Reid Spencerfa35bb32006-12-31 05:40:12 +00002746IndexList // Used for gep instructions and constant expressions
Reid Spencerd6979032006-12-31 21:46:36 +00002747 : /*empty*/ { $$ = new std::vector<Value*>(); }
Reid Spencerfa35bb32006-12-31 05:40:12 +00002748 | IndexList ',' ResolvedVal {
2749 $$ = $1;
2750 $$->push_back($3);
2751 CHECK_FOR_ERROR
2752 }
Reid Spencer6829c252006-12-31 21:25:25 +00002753 ;
Chris Lattner2f7c9632001-06-06 20:29:01 +00002754
Chris Lattner06038452005-05-06 05:51:46 +00002755OptTailCall : TAIL CALL {
2756 $$ = true;
Reid Spencer713eedc2006-08-18 08:43:06 +00002757 CHECK_FOR_ERROR
Chris Lattner06038452005-05-06 05:51:46 +00002758 }
2759 | CALL {
2760 $$ = false;
Reid Spencer713eedc2006-08-18 08:43:06 +00002761 CHECK_FOR_ERROR
Chris Lattner06038452005-05-06 05:51:46 +00002762 };
2763
Chris Lattnerc0686fe2002-09-10 19:57:26 +00002764InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencerfa35bb32006-12-31 05:40:12 +00002765 if (!UpRefs.empty())
2766 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Chris Lattner03c49532007-01-15 02:27:26 +00002767 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint() &&
Reid Spencerd84d35b2007-02-15 02:26:10 +00002768 !isa<VectorType>((*$2).get()))
Reid Spencer713eedc2006-08-18 08:43:06 +00002769 GEN_ERROR(
Reid Spencerde1d05f2007-02-05 10:16:10 +00002770 "Arithmetic operator requires integer, FP, or packed operands");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002771 if (isa<VectorType>((*$2).get()) &&
Reid Spencer812724e2006-12-03 05:45:44 +00002772 ($1 == Instruction::URem ||
2773 $1 == Instruction::SRem ||
2774 $1 == Instruction::FRem))
Reid Spencer09575ba2007-02-15 03:39:18 +00002775 GEN_ERROR("Remainder not supported on vector types");
Reid Spencer812724e2006-12-03 05:45:44 +00002776 Value* val1 = getVal(*$2, $3);
Reid Spencer309080a2006-09-28 19:28:24 +00002777 CHECK_FOR_ERROR
Reid Spencer812724e2006-12-03 05:45:44 +00002778 Value* val2 = getVal(*$2, $5);
Reid Spencer309080a2006-09-28 19:28:24 +00002779 CHECK_FOR_ERROR
Reid Spencer812724e2006-12-03 05:45:44 +00002780 $$ = BinaryOperator::create($1, val1, val2);
Chris Lattnerc0686fe2002-09-10 19:57:26 +00002781 if ($$ == 0)
Reid Spencerde1d05f2007-02-05 10:16:10 +00002782 GEN_ERROR("binary operator returned null");
Reid Spencer812724e2006-12-03 05:45:44 +00002783 delete $2;
Chris Lattnerc0686fe2002-09-10 19:57:26 +00002784 }
2785 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencerfa35bb32006-12-31 05:40:12 +00002786 if (!UpRefs.empty())
2787 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Chris Lattner03c49532007-01-15 02:27:26 +00002788 if (!(*$2)->isInteger()) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00002789 if (Instruction::isShift($1) || !isa<VectorType>($2->get()) ||
2790 !cast<VectorType>($2->get())->getElementType()->isInteger())
Reid Spencerde1d05f2007-02-05 10:16:10 +00002791 GEN_ERROR("Logical operator requires integral operands");
Chris Lattnera5d70e92005-12-21 18:31:29 +00002792 }
Reid Spencer812724e2006-12-03 05:45:44 +00002793 Value* tmpVal1 = getVal(*$2, $3);
Reid Spencer309080a2006-09-28 19:28:24 +00002794 CHECK_FOR_ERROR
Reid Spencer812724e2006-12-03 05:45:44 +00002795 Value* tmpVal2 = getVal(*$2, $5);
Reid Spencer309080a2006-09-28 19:28:24 +00002796 CHECK_FOR_ERROR
Reid Spencer812724e2006-12-03 05:45:44 +00002797 $$ = BinaryOperator::create($1, tmpVal1, tmpVal2);
Chris Lattnerc0686fe2002-09-10 19:57:26 +00002798 if ($$ == 0)
Reid Spencerde1d05f2007-02-05 10:16:10 +00002799 GEN_ERROR("binary operator returned null");
Reid Spencer812724e2006-12-03 05:45:44 +00002800 delete $2;
Chris Lattnerc0686fe2002-09-10 19:57:26 +00002801 }
Reid Spencer812724e2006-12-03 05:45:44 +00002802 | ICMP IPredicates Types ValueRef ',' ValueRef {
Reid Spencerfa35bb32006-12-31 05:40:12 +00002803 if (!UpRefs.empty())
2804 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencerd84d35b2007-02-15 02:26:10 +00002805 if (isa<VectorType>((*$3).get()))
Reid Spencer09575ba2007-02-15 03:39:18 +00002806 GEN_ERROR("Vector types not supported by icmp instruction");
Reid Spencer812724e2006-12-03 05:45:44 +00002807 Value* tmpVal1 = getVal(*$3, $4);
2808 CHECK_FOR_ERROR
2809 Value* tmpVal2 = getVal(*$3, $6);
2810 CHECK_FOR_ERROR
2811 $$ = CmpInst::create($1, $2, tmpVal1, tmpVal2);
2812 if ($$ == 0)
Reid Spencerde1d05f2007-02-05 10:16:10 +00002813 GEN_ERROR("icmp operator returned null");
Reid Spencerd05bc522007-03-20 01:13:00 +00002814 delete $3;
Reid Spencer812724e2006-12-03 05:45:44 +00002815 }
2816 | FCMP FPredicates Types ValueRef ',' ValueRef {
Reid Spencerfa35bb32006-12-31 05:40:12 +00002817 if (!UpRefs.empty())
2818 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencerd84d35b2007-02-15 02:26:10 +00002819 if (isa<VectorType>((*$3).get()))
Reid Spencer09575ba2007-02-15 03:39:18 +00002820 GEN_ERROR("Vector types not supported by fcmp instruction");
Reid Spencer812724e2006-12-03 05:45:44 +00002821 Value* tmpVal1 = getVal(*$3, $4);
2822 CHECK_FOR_ERROR
2823 Value* tmpVal2 = getVal(*$3, $6);
2824 CHECK_FOR_ERROR
2825 $$ = CmpInst::create($1, $2, tmpVal1, tmpVal2);
2826 if ($$ == 0)
Reid Spencerde1d05f2007-02-05 10:16:10 +00002827 GEN_ERROR("fcmp operator returned null");
Reid Spencerd05bc522007-03-20 01:13:00 +00002828 delete $3;
Chris Lattner2f7c9632001-06-06 20:29:01 +00002829 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002830 | CastOps ResolvedVal TO Types {
Reid Spencerfa35bb32006-12-31 05:40:12 +00002831 if (!UpRefs.empty())
2832 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencer812724e2006-12-03 05:45:44 +00002833 Value* Val = $2;
Reid Spencer082a77f2007-01-17 02:47:33 +00002834 const Type* DestTy = $4->get();
2835 if (!CastInst::castIsValid($1, Val, DestTy))
2836 GEN_ERROR("invalid cast opcode for cast from '" +
2837 Val->getType()->getDescription() + "' to '" +
Reid Spencerde1d05f2007-02-05 10:16:10 +00002838 DestTy->getDescription() + "'");
Reid Spencer082a77f2007-01-17 02:47:33 +00002839 $$ = CastInst::create($1, Val, DestTy);
Reid Spencer812724e2006-12-03 05:45:44 +00002840 delete $4;
Chris Lattnera6821822001-07-08 04:57:15 +00002841 }
Chris Lattner6536f0c2004-03-12 05:51:36 +00002842 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer542964f2007-01-11 18:21:29 +00002843 if ($2->getType() != Type::Int1Ty)
Reid Spencerde1d05f2007-02-05 10:16:10 +00002844 GEN_ERROR("select condition must be boolean");
Reid Spencer812724e2006-12-03 05:45:44 +00002845 if ($4->getType() != $6->getType())
Reid Spencerde1d05f2007-02-05 10:16:10 +00002846 GEN_ERROR("select value types should match");
Reid Spencer812724e2006-12-03 05:45:44 +00002847 $$ = new SelectInst($2, $4, $6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002848 CHECK_FOR_ERROR
Chris Lattner6536f0c2004-03-12 05:51:36 +00002849 }
Chris Lattner0079e7d2003-10-18 05:53:13 +00002850 | VAARG ResolvedVal ',' Types {
Reid Spencerfa35bb32006-12-31 05:40:12 +00002851 if (!UpRefs.empty())
2852 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencer812724e2006-12-03 05:45:44 +00002853 $$ = new VAArgInst($2, *$4);
2854 delete $4;
Reid Spencer713eedc2006-08-18 08:43:06 +00002855 CHECK_FOR_ERROR
Chris Lattner0079e7d2003-10-18 05:53:13 +00002856 }
Robert Bocchinoaa1cf542006-01-10 19:04:32 +00002857 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencer812724e2006-12-03 05:45:44 +00002858 if (!ExtractElementInst::isValidOperands($2, $4))
Reid Spencerde1d05f2007-02-05 10:16:10 +00002859 GEN_ERROR("Invalid extractelement operands");
Reid Spencer812724e2006-12-03 05:45:44 +00002860 $$ = new ExtractElementInst($2, $4);
Reid Spencer713eedc2006-08-18 08:43:06 +00002861 CHECK_FOR_ERROR
Robert Bocchinoaa1cf542006-01-10 19:04:32 +00002862 }
Robert Bocchinofdf9e412006-01-17 20:06:25 +00002863 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer812724e2006-12-03 05:45:44 +00002864 if (!InsertElementInst::isValidOperands($2, $4, $6))
Reid Spencerde1d05f2007-02-05 10:16:10 +00002865 GEN_ERROR("Invalid insertelement operands");
Reid Spencer812724e2006-12-03 05:45:44 +00002866 $$ = new InsertElementInst($2, $4, $6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002867 CHECK_FOR_ERROR
Robert Bocchinofdf9e412006-01-17 20:06:25 +00002868 }
Chris Lattner8ec3c2e2006-04-08 01:18:35 +00002869 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer812724e2006-12-03 05:45:44 +00002870 if (!ShuffleVectorInst::isValidOperands($2, $4, $6))
Reid Spencerde1d05f2007-02-05 10:16:10 +00002871 GEN_ERROR("Invalid shufflevector operands");
Reid Spencer812724e2006-12-03 05:45:44 +00002872 $$ = new ShuffleVectorInst($2, $4, $6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002873 CHECK_FOR_ERROR
Chris Lattner8ec3c2e2006-04-08 01:18:35 +00002874 }
Chris Lattnerb94550e2003-10-19 21:34:28 +00002875 | PHI_TOK PHIList {
Chris Lattner931ef3b2001-06-11 15:04:20 +00002876 const Type *Ty = $2->front().first->getType();
Chris Lattner0fc43a62003-10-30 01:38:18 +00002877 if (!Ty->isFirstClassType())
Reid Spencerde1d05f2007-02-05 10:16:10 +00002878 GEN_ERROR("PHI node operands must be of first class type");
Chris Lattner931ef3b2001-06-11 15:04:20 +00002879 $$ = new PHINode(Ty);
Chris Lattner2c089492005-01-29 00:35:55 +00002880 ((PHINode*)$$)->reserveOperandSpace($2->size());
Chris Lattner2f7c9632001-06-06 20:29:01 +00002881 while ($2->begin() != $2->end()) {
Chris Lattner931ef3b2001-06-11 15:04:20 +00002882 if ($2->front().first->getType() != Ty)
Reid Spencerde1d05f2007-02-05 10:16:10 +00002883 GEN_ERROR("All elements of a PHI node must be of the same type");
Chris Lattnerda558102001-10-02 03:41:24 +00002884 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner2f7c9632001-06-06 20:29:01 +00002885 $2->pop_front();
2886 }
2887 delete $2; // Free the list...
Reid Spencer713eedc2006-08-18 08:43:06 +00002888 CHECK_FOR_ERROR
Chris Lattner06038452005-05-06 05:51:46 +00002889 }
Reid Spencer136a91c2007-01-05 17:06:19 +00002890 | OptTailCall OptCallingConv ResultTypes ValueRef '(' ValueRefList ')'
2891 OptFuncAttrs {
Reid Spencerfa35bb32006-12-31 05:40:12 +00002892
2893 // Handle the short syntax
Bill Wendlingad753612006-11-12 11:10:39 +00002894 const PointerType *PFTy = 0;
2895 const FunctionType *Ty = 0;
Reid Spencer136a91c2007-01-05 17:06:19 +00002896 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattner19613292002-10-15 21:41:14 +00002897 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner42b5a8a2001-07-25 22:47:46 +00002898 // Pull out the types of all of the arguments...
Chris Lattner89da8a32003-04-22 18:42:41 +00002899 std::vector<const Type*> ParamTypes;
Reid Spencer78517652007-04-28 16:06:50 +00002900 ParamAttrsVector Attrs;
2901 if ($8 != ParamAttr::None) {
2902 ParamAttrsWithIndex PAWI; PAWI.index = 0; PAWI.attrs = $8;
2903 Attrs.push_back(PAWI);
2904 }
2905 unsigned index = 1;
Reid Spencer71b79e32007-04-09 06:17:21 +00002906 ValueRefList::iterator I = $6->begin(), E = $6->end();
Reid Spencer78517652007-04-28 16:06:50 +00002907 for (; I != E; ++I, ++index) {
Reid Spencerfa35bb32006-12-31 05:40:12 +00002908 const Type *Ty = I->Val->getType();
2909 if (Ty == Type::VoidTy)
2910 GEN_ERROR("Short call syntax cannot be used with varargs");
2911 ParamTypes.push_back(Ty);
Reid Spencer78517652007-04-28 16:06:50 +00002912 if (I->Attrs != ParamAttr::None) {
2913 ParamAttrsWithIndex PAWI; PAWI.index = index; PAWI.attrs = I->Attrs;
2914 Attrs.push_back(PAWI);
2915 }
Chris Lattner7fac0702001-10-03 14:53:21 +00002916 }
Reid Spencer78517652007-04-28 16:06:50 +00002917
2918 ParamAttrsList *PAL = 0;
2919 if (!Attrs.empty())
2920 PAL = ParamAttrsList::get(Attrs);
2921
2922 Ty = FunctionType::get($3->get(), ParamTypes, false, PAL);
Chris Lattner19613292002-10-15 21:41:14 +00002923 PFTy = PointerType::get(Ty);
Chris Lattner42b5a8a2001-07-25 22:47:46 +00002924 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00002925
Chris Lattner53bdd312005-05-06 20:27:19 +00002926 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer309080a2006-09-28 19:28:24 +00002927 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00002928
Reid Spencer94bae692007-04-16 06:55:42 +00002929 // Check for call to invalid intrinsic to avoid crashing later.
2930 if (Function *theF = dyn_cast<Function>(V)) {
Reid Spencer9c9741e2007-04-16 22:01:57 +00002931 if (theF->hasName() && (theF->getValueName()->getKeyLength() >= 5) &&
Reid Spencer4339f7d2007-04-16 20:31:06 +00002932 (0 == strncmp(theF->getValueName()->getKeyData(), "llvm.", 5)) &&
2933 !theF->getIntrinsicID(true))
Reid Spencer94bae692007-04-16 06:55:42 +00002934 GEN_ERROR("Call to invalid LLVM intrinsic function '" +
2935 theF->getName() + "'");
2936 }
2937
Reid Spencerfa35bb32006-12-31 05:40:12 +00002938 // Check the arguments
2939 ValueList Args;
2940 if ($6->empty()) { // Has no arguments?
Chris Lattner91e08322002-07-25 20:52:56 +00002941 // Make sure no arguments is a good thing!
2942 if (Ty->getNumParams() != 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00002943 GEN_ERROR("No arguments passed to a function that "
Reid Spencerde1d05f2007-02-05 10:16:10 +00002944 "expects arguments");
Chris Lattner42b5a8a2001-07-25 22:47:46 +00002945 } else { // Has arguments?
Chris Lattner57698e22002-03-26 18:01:55 +00002946 // Loop through FunctionType's arguments and ensure they are specified
Reid Spencer78517652007-04-28 16:06:50 +00002947 // correctly!
2948 //
Chris Lattnerfa829be2004-02-09 04:14:01 +00002949 FunctionType::param_iterator I = Ty->param_begin();
2950 FunctionType::param_iterator E = Ty->param_end();
Reid Spencerfa35bb32006-12-31 05:40:12 +00002951 ValueRefList::iterator ArgI = $6->begin(), ArgE = $6->end();
Reid Spencer78517652007-04-28 16:06:50 +00002952
Reid Spencerfa35bb32006-12-31 05:40:12 +00002953 for (; ArgI != ArgE && I != E; ++ArgI, ++I) {
2954 if (ArgI->Val->getType() != *I)
2955 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencerde1d05f2007-02-05 10:16:10 +00002956 (*I)->getDescription() + "'");
Reid Spencerfa35bb32006-12-31 05:40:12 +00002957 Args.push_back(ArgI->Val);
2958 }
2959 if (Ty->isVarArg()) {
2960 if (I == E)
2961 for (; ArgI != ArgE; ++ArgI)
2962 Args.push_back(ArgI->Val); // push the remaining varargs
2963 } else if (I != E || ArgI != ArgE)
Reid Spencerde1d05f2007-02-05 10:16:10 +00002964 GEN_ERROR("Invalid number of parameters detected");
Chris Lattner42b5a8a2001-07-25 22:47:46 +00002965 }
Reid Spencerfa35bb32006-12-31 05:40:12 +00002966 // Create the call node
David Greene17a5dfe2007-08-01 03:43:44 +00002967 CallInst *CI = new CallInst(V, Args.begin(), Args.end());
Reid Spencerfa35bb32006-12-31 05:40:12 +00002968 CI->setTailCall($1);
2969 CI->setCallingConv($2);
2970 $$ = CI;
Chris Lattner53bdd312005-05-06 20:27:19 +00002971 delete $6;
Reid Spencer791b8ef2007-01-26 08:04:51 +00002972 delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00002973 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00002974 }
2975 | MemoryInst {
2976 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002977 CHECK_FOR_ERROR
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00002978 };
Chris Lattner2f7c9632001-06-06 20:29:01 +00002979
Chris Lattner8b1680e2003-09-08 18:20:29 +00002980OptVolatile : VOLATILE {
2981 $$ = true;
Reid Spencer713eedc2006-08-18 08:43:06 +00002982 CHECK_FOR_ERROR
Chris Lattner8b1680e2003-09-08 18:20:29 +00002983 }
2984 | /* empty */ {
2985 $$ = false;
Reid Spencer713eedc2006-08-18 08:43:06 +00002986 CHECK_FOR_ERROR
Chris Lattner8b1680e2003-09-08 18:20:29 +00002987 };
2988
Chris Lattnerd8bebcd2001-07-08 21:10:27 +00002989
Chris Lattner06038452005-05-06 05:51:46 +00002990
Chris Lattnerc7de8362005-11-06 06:34:12 +00002991MemoryInst : MALLOC Types OptCAlign {
Reid Spencerfa35bb32006-12-31 05:40:12 +00002992 if (!UpRefs.empty())
2993 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer812724e2006-12-03 05:45:44 +00002994 $$ = new MallocInst(*$2, 0, $3);
2995 delete $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00002996 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00002997 }
Reid Spencer7a9c62b2007-01-12 07:05:14 +00002998 | MALLOC Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencerfa35bb32006-12-31 05:40:12 +00002999 if (!UpRefs.empty())
3000 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer812724e2006-12-03 05:45:44 +00003001 Value* tmpVal = getVal($4, $5);
Reid Spencer713eedc2006-08-18 08:43:06 +00003002 CHECK_FOR_ERROR
Reid Spencer812724e2006-12-03 05:45:44 +00003003 $$ = new MallocInst(*$2, tmpVal, $6);
3004 delete $2;
Nate Begeman848622f2005-11-05 09:21:28 +00003005 }
Chris Lattnerc7de8362005-11-06 06:34:12 +00003006 | ALLOCA Types OptCAlign {
Reid Spencerfa35bb32006-12-31 05:40:12 +00003007 if (!UpRefs.empty())
3008 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer812724e2006-12-03 05:45:44 +00003009 $$ = new AllocaInst(*$2, 0, $3);
3010 delete $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00003011 CHECK_FOR_ERROR
Nate Begeman848622f2005-11-05 09:21:28 +00003012 }
Reid Spencer7a9c62b2007-01-12 07:05:14 +00003013 | ALLOCA Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencerfa35bb32006-12-31 05:40:12 +00003014 if (!UpRefs.empty())
3015 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer812724e2006-12-03 05:45:44 +00003016 Value* tmpVal = getVal($4, $5);
Reid Spencer713eedc2006-08-18 08:43:06 +00003017 CHECK_FOR_ERROR
Reid Spencer812724e2006-12-03 05:45:44 +00003018 $$ = new AllocaInst(*$2, tmpVal, $6);
3019 delete $2;
Nate Begeman848622f2005-11-05 09:21:28 +00003020 }
Chris Lattner252afba2001-07-26 16:29:15 +00003021 | FREE ResolvedVal {
Reid Spencer812724e2006-12-03 05:45:44 +00003022 if (!isa<PointerType>($2->getType()))
Reid Spencer713eedc2006-08-18 08:43:06 +00003023 GEN_ERROR("Trying to free nonpointer type " +
Reid Spencerde1d05f2007-02-05 10:16:10 +00003024 $2->getType()->getDescription() + "");
Reid Spencer812724e2006-12-03 05:45:44 +00003025 $$ = new FreeInst($2);
Reid Spencer713eedc2006-08-18 08:43:06 +00003026 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00003027 }
3028
Christopher Lamb84485702007-04-22 19:24:39 +00003029 | OptVolatile LOAD Types ValueRef OptCAlign {
Reid Spencerfa35bb32006-12-31 05:40:12 +00003030 if (!UpRefs.empty())
3031 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencer812724e2006-12-03 05:45:44 +00003032 if (!isa<PointerType>($3->get()))
Reid Spencer713eedc2006-08-18 08:43:06 +00003033 GEN_ERROR("Can't load from nonpointer type: " +
Reid Spencer812724e2006-12-03 05:45:44 +00003034 (*$3)->getDescription());
3035 if (!cast<PointerType>($3->get())->getElementType()->isFirstClassType())
Reid Spencer713eedc2006-08-18 08:43:06 +00003036 GEN_ERROR("Can't load from pointer of non-first-class type: " +
Reid Spencer812724e2006-12-03 05:45:44 +00003037 (*$3)->getDescription());
3038 Value* tmpVal = getVal(*$3, $4);
Reid Spencer713eedc2006-08-18 08:43:06 +00003039 CHECK_FOR_ERROR
Christopher Lamb84485702007-04-22 19:24:39 +00003040 $$ = new LoadInst(tmpVal, "", $1, $5);
Reid Spencer812724e2006-12-03 05:45:44 +00003041 delete $3;
Chris Lattnerd8bebcd2001-07-08 21:10:27 +00003042 }
Christopher Lamb84485702007-04-22 19:24:39 +00003043 | OptVolatile STORE ResolvedVal ',' Types ValueRef OptCAlign {
Reid Spencerfa35bb32006-12-31 05:40:12 +00003044 if (!UpRefs.empty())
3045 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencer812724e2006-12-03 05:45:44 +00003046 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner667ded92003-09-01 16:31:28 +00003047 if (!PT)
Reid Spencer713eedc2006-08-18 08:43:06 +00003048 GEN_ERROR("Can't store to a nonpointer type: " +
Reid Spencer812724e2006-12-03 05:45:44 +00003049 (*$5)->getDescription());
Chris Lattner667ded92003-09-01 16:31:28 +00003050 const Type *ElTy = PT->getElementType();
Reid Spencer812724e2006-12-03 05:45:44 +00003051 if (ElTy != $3->getType())
3052 GEN_ERROR("Can't store '" + $3->getType()->getDescription() +
Reid Spencerde1d05f2007-02-05 10:16:10 +00003053 "' into space of type '" + ElTy->getDescription() + "'");
Chris Lattner319c47a2002-08-21 23:51:21 +00003054
Reid Spencer812724e2006-12-03 05:45:44 +00003055 Value* tmpVal = getVal(*$5, $6);
Reid Spencer713eedc2006-08-18 08:43:06 +00003056 CHECK_FOR_ERROR
Christopher Lamb84485702007-04-22 19:24:39 +00003057 $$ = new StoreInst($3, tmpVal, $1, $7);
Reid Spencer812724e2006-12-03 05:45:44 +00003058 delete $5;
Chris Lattner62ecb4a2001-07-08 23:22:50 +00003059 }
Chris Lattner476148f2001-11-26 16:54:11 +00003060 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencerfa35bb32006-12-31 05:40:12 +00003061 if (!UpRefs.empty())
3062 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer812724e2006-12-03 05:45:44 +00003063 if (!isa<PointerType>($2->get()))
Reid Spencerde1d05f2007-02-05 10:16:10 +00003064 GEN_ERROR("getelementptr insn requires pointer operand");
Chris Lattnerfd9fbe12004-04-05 01:30:04 +00003065
Chris Lattner04a2d762007-02-13 00:57:40 +00003066 if (!GetElementPtrInst::getIndexedType(*$2, &(*$4)[0], $4->size(), true))
Reid Spencer713eedc2006-08-18 08:43:06 +00003067 GEN_ERROR("Invalid getelementptr indices for type '" +
Reid Spencerde1d05f2007-02-05 10:16:10 +00003068 (*$2)->getDescription()+ "'");
Reid Spencer812724e2006-12-03 05:45:44 +00003069 Value* tmpVal = getVal(*$2, $3);
Reid Spencer713eedc2006-08-18 08:43:06 +00003070 CHECK_FOR_ERROR
Chris Lattner04a2d762007-02-13 00:57:40 +00003071 $$ = new GetElementPtrInst(tmpVal, &(*$4)[0], $4->size());
Reid Spencer812724e2006-12-03 05:45:44 +00003072 delete $2;
Reid Spencer309080a2006-09-28 19:28:24 +00003073 delete $4;
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00003074 };
Chris Lattnerd8bebcd2001-07-08 21:10:27 +00003075
Brian Gaeke960707c2003-11-11 22:41:34 +00003076
Chris Lattner2f7c9632001-06-06 20:29:01 +00003077%%
Reid Spencer713eedc2006-08-18 08:43:06 +00003078
Reid Spencerfa35bb32006-12-31 05:40:12 +00003079// common code from the two 'RunVMAsmParser' functions
3080static Module* RunParser(Module * M) {
3081
3082 llvmAsmlineno = 1; // Reset the current line number...
3083 CurModule.CurrentModule = M;
3084#if YYDEBUG
3085 yydebug = Debug;
3086#endif
3087
3088 // Check to make sure the parser succeeded
3089 if (yyparse()) {
3090 if (ParserResult)
3091 delete ParserResult;
3092 return 0;
3093 }
3094
Reid Spencercf2ccbf2007-03-30 01:37:13 +00003095 // Emit an error if there are any unresolved types left.
3096 if (!CurModule.LateResolveTypes.empty()) {
3097 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
3098 if (DID.Type == ValID::LocalName) {
3099 GenerateError("Undefined type remains at eof: '"+DID.getName() + "'");
3100 } else {
3101 GenerateError("Undefined type remains at eof: #" + itostr(DID.Num));
3102 }
3103 if (ParserResult)
3104 delete ParserResult;
3105 return 0;
3106 }
3107
3108 // Emit an error if there are any unresolved values left.
3109 if (!CurModule.LateResolveValues.empty()) {
3110 Value *V = CurModule.LateResolveValues.back();
3111 std::map<Value*, std::pair<ValID, int> >::iterator I =
3112 CurModule.PlaceHolderInfo.find(V);
3113
3114 if (I != CurModule.PlaceHolderInfo.end()) {
3115 ValID &DID = I->second.first;
3116 if (DID.Type == ValID::LocalName) {
3117 GenerateError("Undefined value remains at eof: "+DID.getName() + "'");
3118 } else {
3119 GenerateError("Undefined value remains at eof: #" + itostr(DID.Num));
3120 }
3121 if (ParserResult)
3122 delete ParserResult;
3123 return 0;
3124 }
3125 }
3126
Reid Spencerfa35bb32006-12-31 05:40:12 +00003127 // Check to make sure that parsing produced a result
3128 if (!ParserResult)
3129 return 0;
3130
3131 // Reset ParserResult variable while saving its value for the result.
3132 Module *Result = ParserResult;
3133 ParserResult = 0;
3134
3135 return Result;
3136}
3137
Reid Spencer713eedc2006-08-18 08:43:06 +00003138void llvm::GenerateError(const std::string &message, int LineNo) {
3139 if (LineNo == -1) LineNo = llvmAsmlineno;
3140 // TODO: column number in exception
3141 if (TheParseError)
3142 TheParseError->setError(CurFilename, message, LineNo);
3143 TriggerError = 1;
3144}
3145
Chris Lattnera6821822001-07-08 04:57:15 +00003146int yyerror(const char *ErrorMsg) {
Chris Lattner89da8a32003-04-22 18:42:41 +00003147 std::string where
3148 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Vikram S. Adve7064eaf2002-07-14 22:59:28 +00003149 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00003150 std::string errMsg = where + "error: " + std::string(ErrorMsg);
3151 if (yychar != YYEMPTY && yychar != 0)
3152 errMsg += " while reading token: '" + std::string(llvmAsmtext, llvmAsmleng)+
3153 "'";
Reid Spencer713eedc2006-08-18 08:43:06 +00003154 GenerateError(errMsg);
Chris Lattner2f7c9632001-06-06 20:29:01 +00003155 return 0;
3156}