blob: 3525567cf2c698ec0a0e33da4cbec75f234328eb [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===-- llvmAsmParser.y - Parser for llvm assembly files --------*- C++ -*-===//
Misha Brukman5a2a3822005-05-10 22:02:28 +00002//
John Criswell856ba762003-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 Brukman5a2a3822005-05-10 22:02:28 +00007//
John Criswell856ba762003-10-21 15:17:13 +00008//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
10// This file implements the bison parser for LLVM assembly languages files.
11//
Chris Lattnercf3056d2003-10-13 03:32:08 +000012//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +000013
Chris Lattner00950542001-06-06 20:29:01 +000014%{
15#include "ParserInternals.h"
Chris Lattnera8e8f162005-05-06 20:27:19 +000016#include "llvm/CallingConv.h"
Chris Lattneraa2c8532006-01-25 22:26:43 +000017#include "llvm/InlineAsm.h"
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000018#include "llvm/Instructions.h"
Chris Lattner00950542001-06-06 20:29:01 +000019#include "llvm/Module.h"
Reid Spenceref9b9a72007-02-05 20:47:22 +000020#include "llvm/ValueSymbolTable.h"
Chandler Carruth69940402007-08-04 01:51:18 +000021#include "llvm/AutoUpgrade.h"
Chris Lattner830b6f92004-04-05 01:30:04 +000022#include "llvm/Support/GetElementPtrTypeIterator.h"
Reid Spencere1553cc2006-12-31 05:40:12 +000023#include "llvm/Support/CommandLine.h"
Chris Lattnere0135402007-01-31 04:43:46 +000024#include "llvm/ADT/SmallVector.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000025#include "llvm/ADT/STLExtras.h"
Chris Lattner87ac9722005-11-06 06:46:28 +000026#include "llvm/Support/MathExtras.h"
Bill Wendling3e3bcbf2006-11-28 22:47:12 +000027#include "llvm/Support/Streams.h"
Reid Spencer8ce1da72004-07-04 12:19:05 +000028#include <algorithm>
Chris Lattner00950542001-06-06 20:29:01 +000029#include <list>
Chris Lattnerc02659f2007-02-11 21:39:35 +000030#include <map>
Chris Lattnerc188eeb2002-07-30 18:54:25 +000031#include <utility>
Reid Spencere1553cc2006-12-31 05:40:12 +000032#ifndef NDEBUG
33#define YYDEBUG 1
34#endif
Chris Lattner00950542001-06-06 20:29:01 +000035
Reid Spencere4f47592006-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 Spencer61c83e02006-08-18 08:43:06 +000047static bool TriggerError = false;
Reid Spencerf63697d2006-10-09 17:36:59 +000048#define CHECK_FOR_ERROR { if (TriggerError) { TriggerError = false; YYABORT; } }
Reid Spencer61c83e02006-08-18 08:43:06 +000049#define GEN_ERROR(msg) { GenerateError(msg); YYERROR; }
50
Chris Lattner386a3b72001-10-16 19:54:17 +000051int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
Chris Lattner09083092001-07-08 04:57:15 +000052int yylex(); // declaration" of xxx warnings.
Chris Lattner00950542001-06-06 20:29:01 +000053int yyparse();
54
Brian Gaeked0fde302003-11-11 22:41:34 +000055namespace llvm {
Chris Lattner86427632004-07-14 06:39:48 +000056 std::string CurFilename;
Reid Spencere1553cc2006-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 Lattner86427632004-07-14 06:39:48 +000062}
63using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000064
Chris Lattner00950542001-06-06 20:29:01 +000065static Module *ParserResult;
Chris Lattner00950542001-06-06 20:29:01 +000066
Chris Lattner30c89792001-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 Wendlinge8156192006-12-07 01:30:32 +000072#define UR_OUT(X) cerr << X
Chris Lattner30c89792001-09-07 16:35:17 +000073#else
74#define UR_OUT(X)
75#endif
76
Vikram S. Adved3f7eb02002-07-14 22:59:28 +000077#define YYERROR_VERBOSE 1
78
Chris Lattnerb2b96672005-11-12 18:21:21 +000079static GlobalVariable *CurGV;
Andrew Lenharth558bc882005-06-18 18:34:52 +000080
81
Chris Lattner7e708292002-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 Lattner00950542001-06-06 20:29:01 +000084//
Chris Lattner9232b992003-04-22 18:42:41 +000085typedef std::vector<Value *> ValueList; // Numbered defs
Reid Spencere1553cc2006-12-31 05:40:12 +000086
Misha Brukman5a2a3822005-05-10 22:02:28 +000087static void
Reid Spencer186a43f2007-03-19 18:39:36 +000088ResolveDefinitions(ValueList &LateResolvers, ValueList *FutureLateResolvers=0);
Chris Lattner00950542001-06-06 20:29:01 +000089
90static struct PerModuleInfo {
91 Module *CurrentModule;
Reid Spencer186a43f2007-03-19 18:39:36 +000092 ValueList Values; // Module level numbered definitions
93 ValueList LateResolveValues;
Reid Spencerb78b9082006-11-28 07:28:14 +000094 std::vector<PATypeHolder> Types;
95 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner00950542001-06-06 20:29:01 +000096
Chris Lattnerbc721ed2004-07-13 08:28:21 +000097 /// PlaceHolderInfo - When temporary placeholder objects are created, remember
Reid Spencerfd20c0a2006-05-29 02:34:34 +000098 /// how they were referenced and on which line of the input they came from so
Chris Lattner22ae2322004-07-13 08:39:15 +000099 /// that we can resolve them later and print error messages as appropriate.
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000100 std::map<Value*, std::pair<ValID, int> > PlaceHolderInfo;
101
Chris Lattner2079fde2001-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 Spencer3271ed52004-07-18 00:08:11 +0000105 // here. This is used for forward references of GlobalValues.
Chris Lattner2079fde2001-10-13 06:41:08 +0000106 //
Chris Lattner9232b992003-04-22 18:42:41 +0000107 typedef std::map<std::pair<const PointerType *,
Chris Lattnerbc207592004-03-08 06:09:57 +0000108 ValID>, GlobalValue*> GlobalRefsType;
Chris Lattner2079fde2001-10-13 06:41:08 +0000109 GlobalRefsType GlobalRefs;
110
Chris Lattner00950542001-06-06 20:29:01 +0000111 void ModuleDone() {
Chris Lattner7e708292002-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 Lattner30c89792001-09-07 16:35:17 +0000115 //
Chris Lattner00950542001-06-06 20:29:01 +0000116 ResolveDefinitions(LateResolveValues);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000117 if (TriggerError)
118 return;
Chris Lattner00950542001-06-06 20:29:01 +0000119
Chris Lattner2079fde2001-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 Lattner9232b992003-04-22 18:42:41 +0000124 std::string UndefinedReferences = "Unresolved global references exist:\n";
Misha Brukman5a2a3822005-05-10 22:02:28 +0000125
Chris Lattner749ce032002-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 Spencer61c83e02006-08-18 08:43:06 +0000131 GenerateError(UndefinedReferences);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000132 return;
Chris Lattner2079fde2001-10-13 06:41:08 +0000133 }
134
Chandler Carruth69940402007-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 Lattner7e708292002-06-25 16:13:24 +0000140 Values.clear(); // Clear out function local definitions
Chris Lattner30c89792001-09-07 16:35:17 +0000141 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +0000142 CurrentModule = 0;
143 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000144
Chris Lattner8a327842004-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 Spencer863dd882007-04-28 16:06:50 +0000148 GlobalValue *GetForwardRefForGlobal(const PointerType *PTy, ValID ID) {
Chris Lattner8a327842004-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 Spencer863dd882007-04-28 16:06:50 +0000155 GlobalRefs.erase(I);
Chris Lattner8a327842004-07-14 21:44:00 +0000156 }
157 return Ret;
158 }
Reid Spencer98b3c5c2007-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 Lattner00950542001-06-06 20:29:01 +0000209} CurModule;
210
Chris Lattner79df7c02002-03-26 18:01:55 +0000211static struct PerFunctionInfo {
Chris Lattner7e708292002-06-25 16:13:24 +0000212 Function *CurrentFunction; // Pointer to current function being created
Chris Lattner00950542001-06-06 20:29:01 +0000213
Reid Spencer186a43f2007-03-19 18:39:36 +0000214 ValueList Values; // Keep track of #'d definitions
215 unsigned NextValNum;
216 ValueList LateResolveValues;
Reid Spenceref9b9a72007-02-05 20:47:22 +0000217 bool isDeclare; // Is this function a forward declararation?
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000218 GlobalValue::LinkageTypes Linkage; // Linkage for forward declaration.
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000219 GlobalValue::VisibilityTypes Visibility;
Chris Lattner00950542001-06-06 20:29:01 +0000220
Chris Lattner2e2ed292004-07-14 06:28:35 +0000221 /// BBForwardRefs - When we see forward references to basic blocks, keep
222 /// track of them here.
Reid Spencer186a43f2007-03-19 18:39:36 +0000223 std::map<ValID, BasicBlock*> BBForwardRefs;
Chris Lattner2e2ed292004-07-14 06:28:35 +0000224
Chris Lattner79df7c02002-03-26 18:01:55 +0000225 inline PerFunctionInfo() {
226 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000227 isDeclare = false;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000228 Linkage = GlobalValue::ExternalLinkage;
229 Visibility = GlobalValue::DefaultVisibility;
Chris Lattner00950542001-06-06 20:29:01 +0000230 }
231
Chris Lattner79df7c02002-03-26 18:01:55 +0000232 inline void FunctionStart(Function *M) {
233 CurrentFunction = M;
Reid Spencer186a43f2007-03-19 18:39:36 +0000234 NextValNum = 0;
Chris Lattner00950542001-06-06 20:29:01 +0000235 }
236
Chris Lattner79df7c02002-03-26 18:01:55 +0000237 void FunctionDone() {
Chris Lattner2e2ed292004-07-14 06:28:35 +0000238 // Any forward referenced blocks left?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000239 if (!BBForwardRefs.empty()) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000240 GenerateError("Undefined reference to label " +
Reid Spencer186a43f2007-03-19 18:39:36 +0000241 BBForwardRefs.begin()->second->getName());
Reid Spencer5b7e7532006-09-28 19:28:24 +0000242 return;
243 }
Chris Lattner2e2ed292004-07-14 06:28:35 +0000244
245 // Resolve all forward references now.
Chris Lattner386a3b72001-10-16 19:54:17 +0000246 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000247
Chris Lattner7e708292002-06-25 16:13:24 +0000248 Values.clear(); // Clear out function local definitions
Reid Spencer186a43f2007-03-19 18:39:36 +0000249 BBForwardRefs.clear();
Chris Lattner79df7c02002-03-26 18:01:55 +0000250 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000251 isDeclare = false;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000252 Linkage = GlobalValue::ExternalLinkage;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000253 Visibility = GlobalValue::DefaultVisibility;
Chris Lattner00950542001-06-06 20:29:01 +0000254 }
Chris Lattner394f2eb2003-10-16 20:12:13 +0000255} CurFun; // Info for the current function...
Chris Lattner00950542001-06-06 20:29:01 +0000256
Chris Lattner394f2eb2003-10-16 20:12:13 +0000257static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000258
Chris Lattner00950542001-06-06 20:29:01 +0000259
260//===----------------------------------------------------------------------===//
261// Code to handle definitions of all the types
262//===----------------------------------------------------------------------===//
263
Reid Spencer186a43f2007-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 Lattner2079fde2001-10-13 06:41:08 +0000268
Reid Spencer186a43f2007-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 Lattner00950542001-06-06 20:29:01 +0000281}
282
Chris Lattner30c89792001-09-07 16:35:17 +0000283static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000284 switch (D.Type) {
Reid Spencerb2d17862007-01-26 08:04:51 +0000285 case ValID::LocalID: // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000286 // Module constants occupy the lowest numbered slots...
Reid Spencerb2d17862007-01-26 08:04:51 +0000287 if (D.Num < CurModule.Types.size())
288 return CurModule.Types[D.Num];
Chris Lattner42c9e772001-10-20 09:32:59 +0000289 break;
Reid Spencerb2d17862007-01-26 08:04:51 +0000290 case ValID::LocalName: // Is it a named definition?
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000291 if (const Type *N = CurModule.CurrentModule->getTypeByName(D.getName())) {
Chris Lattnera09000d2004-07-14 23:03:46 +0000292 D.destroy(); // Free old strdup'd memory...
293 return N;
Chris Lattner6e6026b2002-11-20 18:36:02 +0000294 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000295 break;
Chris Lattner30c89792001-09-07 16:35:17 +0000296 default:
Reid Spencerf4fa5902007-02-05 10:16:10 +0000297 GenerateError("Internal parser error: Invalid symbol type reference");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000298 return 0;
Chris Lattner30c89792001-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 Lattner355ad1f2005-03-21 06:27:42 +0000307
308 if (inFunctionScope()) {
Reid Spencerb2d17862007-01-26 08:04:51 +0000309 if (D.Type == ValID::LocalName) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000310 GenerateError("Reference to an undefined type: '" + D.getName() + "'");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000311 return 0;
312 } else {
Reid Spencerb2d17862007-01-26 08:04:51 +0000313 GenerateError("Reference to an undefined type: #" + utostr(D.Num));
Reid Spencer5b7e7532006-09-28 19:28:24 +0000314 return 0;
315 }
Chris Lattner4a42e902001-10-22 05:56:09 +0000316 }
Chris Lattner30c89792001-09-07 16:35:17 +0000317
Reid Spencerb78b9082006-11-28 07:28:14 +0000318 std::map<ValID, PATypeHolder>::iterator I =CurModule.LateResolveTypes.find(D);
Chris Lattner355ad1f2005-03-21 06:27:42 +0000319 if (I != CurModule.LateResolveTypes.end())
Reid Spencerb78b9082006-11-28 07:28:14 +0000320 return I->second;
Chris Lattner355ad1f2005-03-21 06:27:42 +0000321
Reid Spencerb78b9082006-11-28 07:28:14 +0000322 Type *Typ = OpaqueType::get();
323 CurModule.LateResolveTypes.insert(std::make_pair(D, Typ));
324 return Typ;
Reid Spencer08de34b2006-12-03 05:45:44 +0000325 }
Chris Lattner30c89792001-09-07 16:35:17 +0000326
Reid Spencer186a43f2007-03-19 18:39:36 +0000327// getExistingVal - Look up the value specified by the provided type and
Chris Lattner2079fde2001-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 Spencer186a43f2007-03-19 18:39:36 +0000331static Value *getExistingVal(const Type *Ty, const ValID &D) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000332 if (isa<FunctionType>(Ty)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000333 GenerateError("Functions are not values and "
Chris Lattner79df7c02002-03-26 18:01:55 +0000334 "must be referenced as pointers");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000335 return 0;
336 }
Chris Lattner386a3b72001-10-16 19:54:17 +0000337
Chris Lattner30c89792001-09-07 16:35:17 +0000338 switch (D.Type) {
Reid Spencerb2d17862007-01-26 08:04:51 +0000339 case ValID::LocalID: { // Is it a numbered definition?
Reid Spencerb2d17862007-01-26 08:04:51 +0000340 // Check that the number is within bounds.
Reid Spencer186a43f2007-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 Spencerb2d17862007-01-26 08:04:51 +0000351 }
352 case ValID::GlobalID: { // Is it a numbered definition?
Reid Spencer186a43f2007-03-19 18:39:36 +0000353 if (D.Num >= CurModule.Values.size())
Reid Spenceref9b9a72007-02-05 20:47:22 +0000354 return 0;
Reid Spencer186a43f2007-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 Spenceref9b9a72007-02-05 20:47:22 +0000360 return 0;
Reid Spencer186a43f2007-03-19 18:39:36 +0000361 }
362 return Result;
Chris Lattner00950542001-06-06 20:29:01 +0000363 }
Reid Spencerb2d17862007-01-26 08:04:51 +0000364
365 case ValID::LocalName: { // Is it a named definition?
Reid Spenceref9b9a72007-02-05 20:47:22 +0000366 if (!inFunctionScope())
367 return 0;
368 ValueSymbolTable &SymTab = CurFun.CurrentFunction->getValueSymbolTable();
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000369 Value *N = SymTab.lookup(D.getName());
Reid Spenceref9b9a72007-02-05 20:47:22 +0000370 if (N == 0)
371 return 0;
372 if (N->getType() != Ty)
373 return 0;
Reid Spencerb2d17862007-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 Spenceref9b9a72007-02-05 20:47:22 +0000379 ValueSymbolTable &SymTab = CurModule.CurrentModule->getValueSymbolTable();
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000380 Value *N = SymTab.lookup(D.getName());
Reid Spenceref9b9a72007-02-05 20:47:22 +0000381 if (N == 0)
382 return 0;
383 if (N->getType() != Ty)
384 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000385
386 D.destroy(); // Free old strdup'd memory...
387 return N;
388 }
389
Misha Brukman5a2a3822005-05-10 22:02:28 +0000390 // Check to make sure that "Ty" is an integral type, and that our
Chris Lattner2079fde2001-10-13 06:41:08 +0000391 // value will fit into the specified type...
392 case ValID::ConstSIntVal: // Is it a constant pool reference??
Reid Spencerb83eb642006-10-20 07:07:24 +0000393 if (!ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000394 GenerateError("Signed integral constant '" +
Misha Brukman5a2a3822005-05-10 22:02:28 +0000395 itostr(D.ConstPool64) + "' is invalid for type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +0000396 Ty->getDescription() + "'");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000397 return 0;
398 }
Reid Spencereac65742007-03-19 20:40:22 +0000399 return ConstantInt::get(Ty, D.ConstPool64, true);
Chris Lattner2079fde2001-10-13 06:41:08 +0000400
401 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Reid Spencerb83eb642006-10-20 07:07:24 +0000402 if (!ConstantInt::isValueValidForType(Ty, D.UConstPool64)) {
403 if (!ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000404 GenerateError("Integral constant '" + utostr(D.UConstPool64) +
Reid Spencerf4fa5902007-02-05 10:16:10 +0000405 "' is invalid or out of range");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000406 return 0;
Chris Lattner2079fde2001-10-13 06:41:08 +0000407 } else { // This is really a signed reference. Transmogrify.
Reid Spencereac65742007-03-19 20:40:22 +0000408 return ConstantInt::get(Ty, D.ConstPool64, true);
Chris Lattner2079fde2001-10-13 06:41:08 +0000409 }
410 } else {
Reid Spencerb83eb642006-10-20 07:07:24 +0000411 return ConstantInt::get(Ty, D.UConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000412 }
413
Chris Lattner2079fde2001-10-13 06:41:08 +0000414 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Dale Johannesen43421b32007-09-06 18:13:44 +0000415 if (!ConstantFP::isValueValidForType(Ty, *D.ConstPoolFP)) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000416 GenerateError("FP constant invalid for type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000417 return 0;
418 }
Dale Johannesen3f6eb742007-09-11 18:32:33 +0000419 // Lexer has no type info, so builds all float and double FP constants
420 // as double. Fix this here. Long double does not need this.
421 if (&D.ConstPoolFP->getSemantics() == &APFloat::IEEEdouble &&
422 Ty==Type::FloatTy)
Dale Johannesen43421b32007-09-06 18:13:44 +0000423 D.ConstPoolFP->convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven);
424 return ConstantFP::get(Ty, *D.ConstPoolFP);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000425
Chris Lattner2079fde2001-10-13 06:41:08 +0000426 case ValID::ConstNullVal: // Is it a null value?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000427 if (!isa<PointerType>(Ty)) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000428 GenerateError("Cannot create a a non pointer null");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000429 return 0;
430 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000431 return ConstantPointerNull::get(cast<PointerType>(Ty));
Misha Brukman5a2a3822005-05-10 22:02:28 +0000432
Chris Lattner16710e92004-10-16 18:17:13 +0000433 case ValID::ConstUndefVal: // Is it an undef value?
434 return UndefValue::get(Ty);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000435
Chris Lattnerf1f03df2005-12-21 17:53:02 +0000436 case ValID::ConstZeroVal: // Is it a zero value?
437 return Constant::getNullValue(Ty);
438
Chris Lattnerd78700d2002-08-16 21:14:40 +0000439 case ValID::ConstantVal: // Fully resolved constant?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000440 if (D.ConstantValue->getType() != Ty) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000441 GenerateError("Constant expression type different from required type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000442 return 0;
443 }
Chris Lattnerd78700d2002-08-16 21:14:40 +0000444 return D.ConstantValue;
445
Chris Lattneraa2c8532006-01-25 22:26:43 +0000446 case ValID::InlineAsmVal: { // Inline asm expression
447 const PointerType *PTy = dyn_cast<PointerType>(Ty);
448 const FunctionType *FTy =
449 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
Reid Spencer5b7e7532006-09-28 19:28:24 +0000450 if (!FTy || !InlineAsm::Verify(FTy, D.IAD->Constraints)) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000451 GenerateError("Invalid type for asm constraint string");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000452 return 0;
453 }
Chris Lattneraa2c8532006-01-25 22:26:43 +0000454 InlineAsm *IA = InlineAsm::get(FTy, D.IAD->AsmString, D.IAD->Constraints,
455 D.IAD->HasSideEffects);
456 D.destroy(); // Free InlineAsmDescriptor.
457 return IA;
458 }
Chris Lattner30c89792001-09-07 16:35:17 +0000459 default:
Reid Spencer1755a9a2007-02-05 17:01:20 +0000460 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000461 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000462 } // End of switch
463
Reid Spencer1755a9a2007-02-05 17:01:20 +0000464 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000465 return 0;
466}
467
Reid Spencer186a43f2007-03-19 18:39:36 +0000468// getVal - This function is identical to getExistingVal, except that if a
Chris Lattner2079fde2001-10-13 06:41:08 +0000469// value is not already defined, it "improvises" by creating a placeholder var
470// that looks and acts just like the requested variable. When the value is
471// defined later, all uses of the placeholder variable are replaced with the
472// real thing.
473//
Chris Lattner64116f92004-07-14 01:33:11 +0000474static Value *getVal(const Type *Ty, const ValID &ID) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000475 if (Ty == Type::LabelTy) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000476 GenerateError("Cannot use a basic block here");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000477 return 0;
478 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000479
Chris Lattner64116f92004-07-14 01:33:11 +0000480 // See if the value has already been defined.
Reid Spencer186a43f2007-03-19 18:39:36 +0000481 Value *V = getExistingVal(Ty, ID);
Chris Lattner2079fde2001-10-13 06:41:08 +0000482 if (V) return V;
Reid Spencer5b7e7532006-09-28 19:28:24 +0000483 if (TriggerError) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000484
Reid Spencer5b7e7532006-09-28 19:28:24 +0000485 if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty)) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000486 GenerateError("Invalid use of a composite type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000487 return 0;
488 }
Chris Lattner60cd9552005-03-23 01:29:26 +0000489
Chris Lattner00950542001-06-06 20:29:01 +0000490 // If we reached here, we referenced either a symbol that we don't know about
491 // or an id number that hasn't been read yet. We may be referencing something
492 // forward, so just create an entry to be resolved later and get to it...
493 //
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000494 switch (ID.Type) {
495 case ValID::GlobalName:
Reid Spencer3cb4dda2007-04-28 14:13:42 +0000496 case ValID::GlobalID: {
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000497 const PointerType *PTy = dyn_cast<PointerType>(Ty);
498 if (!PTy) {
499 GenerateError("Invalid type for reference to global" );
500 return 0;
501 }
502 const Type* ElTy = PTy->getElementType();
503 if (const FunctionType *FTy = dyn_cast<FunctionType>(ElTy))
504 V = new Function(FTy, GlobalValue::ExternalLinkage);
505 else
506 V = new GlobalVariable(ElTy, false, GlobalValue::ExternalLinkage);
507 break;
Reid Spencer3cb4dda2007-04-28 14:13:42 +0000508 }
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000509 default:
510 V = new Argument(Ty);
511 }
512
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000513 // Remember where this forward reference came from. FIXME, shouldn't we try
514 // to recycle these things??
Chris Lattner64116f92004-07-14 01:33:11 +0000515 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000516 llvmAsmlineno)));
517
Chris Lattner79df7c02002-03-26 18:01:55 +0000518 if (inFunctionScope())
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000519 InsertValue(V, CurFun.LateResolveValues);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000520 else
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000521 InsertValue(V, CurModule.LateResolveValues);
522 return V;
Chris Lattner00950542001-06-06 20:29:01 +0000523}
524
Reid Spencer186a43f2007-03-19 18:39:36 +0000525/// defineBBVal - This is a definition of a new basic block with the specified
526/// identifier which must be the same as CurFun.NextValNum, if its numeric.
527static BasicBlock *defineBBVal(const ValID &ID) {
Reid Spencer1755a9a2007-02-05 17:01:20 +0000528 assert(inFunctionScope() && "Can't get basic block at global scope!");
Chris Lattner64116f92004-07-14 01:33:11 +0000529
Chris Lattner2e2ed292004-07-14 06:28:35 +0000530 BasicBlock *BB = 0;
Chris Lattner64116f92004-07-14 01:33:11 +0000531
Reid Spencer186a43f2007-03-19 18:39:36 +0000532 // First, see if this was forward referenced
Chris Lattner64116f92004-07-14 01:33:11 +0000533
Reid Spencer186a43f2007-03-19 18:39:36 +0000534 std::map<ValID, BasicBlock*>::iterator BBI = CurFun.BBForwardRefs.find(ID);
535 if (BBI != CurFun.BBForwardRefs.end()) {
536 BB = BBI->second;
Chris Lattner2e2ed292004-07-14 06:28:35 +0000537 // The forward declaration could have been inserted anywhere in the
538 // function: insert it into the correct place now.
539 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
540 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
Reid Spencer186a43f2007-03-19 18:39:36 +0000541
Reid Spencer2c9df212007-03-20 01:13:00 +0000542 // We're about to erase the entry, save the key so we can clean it up.
543 ValID Tmp = BBI->first;
544
Reid Spencer186a43f2007-03-19 18:39:36 +0000545 // Erase the forward ref from the map as its no longer "forward"
546 CurFun.BBForwardRefs.erase(ID);
547
Reid Spencer2c9df212007-03-20 01:13:00 +0000548 // The key has been removed from the map but so we don't want to leave
549 // strdup'd memory around so destroy it too.
550 Tmp.destroy();
551
Reid Spencer186a43f2007-03-19 18:39:36 +0000552 // If its a numbered definition, bump the number and set the BB value.
553 if (ID.Type == ValID::LocalID) {
554 assert(ID.Num == CurFun.NextValNum && "Invalid new block number");
555 InsertValue(BB);
556 }
557
558 ID.destroy();
559 return BB;
560 }
561
562 // We haven't seen this BB before and its first mention is a definition.
563 // Just create it and return it.
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000564 std::string Name (ID.Type == ValID::LocalName ? ID.getName() : "");
Reid Spencer186a43f2007-03-19 18:39:36 +0000565 BB = new BasicBlock(Name, CurFun.CurrentFunction);
566 if (ID.Type == ValID::LocalID) {
567 assert(ID.Num == CurFun.NextValNum && "Invalid new block number");
568 InsertValue(BB);
Chris Lattner2e2ed292004-07-14 06:28:35 +0000569 }
Reid Spencer186a43f2007-03-19 18:39:36 +0000570
571 ID.destroy(); // Free strdup'd memory
572 return BB;
573}
574
575/// getBBVal - get an existing BB value or create a forward reference for it.
576///
577static BasicBlock *getBBVal(const ValID &ID) {
578 assert(inFunctionScope() && "Can't get basic block at global scope!");
579
580 BasicBlock *BB = 0;
581
582 std::map<ValID, BasicBlock*>::iterator BBI = CurFun.BBForwardRefs.find(ID);
583 if (BBI != CurFun.BBForwardRefs.end()) {
584 BB = BBI->second;
585 } if (ID.Type == ValID::LocalName) {
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000586 std::string Name = ID.getName();
Reid Spencer186a43f2007-03-19 18:39:36 +0000587 Value *N = CurFun.CurrentFunction->getValueSymbolTable().lookup(Name);
588 if (N)
589 if (N->getType()->getTypeID() == Type::LabelTyID)
590 BB = cast<BasicBlock>(N);
591 else
592 GenerateError("Reference to label '" + Name + "' is actually of type '"+
593 N->getType()->getDescription() + "'");
594 } else if (ID.Type == ValID::LocalID) {
595 if (ID.Num < CurFun.NextValNum && ID.Num < CurFun.Values.size()) {
596 if (CurFun.Values[ID.Num]->getType()->getTypeID() == Type::LabelTyID)
597 BB = cast<BasicBlock>(CurFun.Values[ID.Num]);
598 else
599 GenerateError("Reference to label '%" + utostr(ID.Num) +
600 "' is actually of type '"+
601 CurFun.Values[ID.Num]->getType()->getDescription() + "'");
602 }
603 } else {
604 GenerateError("Illegal label reference " + ID.getName());
605 return 0;
606 }
607
608 // If its already been defined, return it now.
609 if (BB) {
610 ID.destroy(); // Free strdup'd memory.
611 return BB;
612 }
613
614 // Otherwise, this block has not been seen before, create it.
615 std::string Name;
616 if (ID.Type == ValID::LocalName)
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000617 Name = ID.getName();
Reid Spencer186a43f2007-03-19 18:39:36 +0000618 BB = new BasicBlock(Name, CurFun.CurrentFunction);
619
620 // Insert it in the forward refs map.
621 CurFun.BBForwardRefs[ID] = BB;
622
Chris Lattner64116f92004-07-14 01:33:11 +0000623 return BB;
624}
625
Chris Lattner00950542001-06-06 20:29:01 +0000626
627//===----------------------------------------------------------------------===//
628// Code to handle forward references in instructions
629//===----------------------------------------------------------------------===//
630//
631// This code handles the late binding needed with statements that reference
632// values not defined yet... for example, a forward branch, or the PHI node for
633// a loop body.
634//
Chris Lattner394f2eb2003-10-16 20:12:13 +0000635// This keeps a table (CurFun.LateResolveValues) of all such forward references
Chris Lattner00950542001-06-06 20:29:01 +0000636// and back patchs after we are done.
637//
638
Misha Brukman5a2a3822005-05-10 22:02:28 +0000639// ResolveDefinitions - If we could not resolve some defs at parsing
640// time (forward branches, phi functions for loops, etc...) resolve the
Chris Lattner00950542001-06-06 20:29:01 +0000641// defs now...
642//
Misha Brukman5a2a3822005-05-10 22:02:28 +0000643static void
Reid Spencer186a43f2007-03-19 18:39:36 +0000644ResolveDefinitions(ValueList &LateResolvers, ValueList *FutureLateResolvers) {
Chris Lattner00950542001-06-06 20:29:01 +0000645 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Reid Spencer186a43f2007-03-19 18:39:36 +0000646 while (!LateResolvers.empty()) {
647 Value *V = LateResolvers.back();
648 LateResolvers.pop_back();
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000649
Reid Spencer186a43f2007-03-19 18:39:36 +0000650 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
651 CurModule.PlaceHolderInfo.find(V);
652 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000653
Reid Spencer186a43f2007-03-19 18:39:36 +0000654 ValID &DID = PHI->second.first;
Chris Lattner00950542001-06-06 20:29:01 +0000655
Reid Spencer186a43f2007-03-19 18:39:36 +0000656 Value *TheRealValue = getExistingVal(V->getType(), DID);
657 if (TriggerError)
658 return;
659 if (TheRealValue) {
660 V->replaceAllUsesWith(TheRealValue);
661 delete V;
662 CurModule.PlaceHolderInfo.erase(PHI);
663 } else if (FutureLateResolvers) {
664 // Functions have their unresolved items forwarded to the module late
665 // resolver table
666 InsertValue(V, *FutureLateResolvers);
667 } else {
668 if (DID.Type == ValID::LocalName || DID.Type == ValID::GlobalName) {
669 GenerateError("Reference to an invalid definition: '" +DID.getName()+
670 "' of type '" + V->getType()->getDescription() + "'",
671 PHI->second.second);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000672 return;
Chris Lattner386a3b72001-10-16 19:54:17 +0000673 } else {
Reid Spencer186a43f2007-03-19 18:39:36 +0000674 GenerateError("Reference to an invalid definition: #" +
675 itostr(DID.Num) + " of type '" +
676 V->getType()->getDescription() + "'",
677 PHI->second.second);
678 return;
Chris Lattner30c89792001-09-07 16:35:17 +0000679 }
Chris Lattner00950542001-06-06 20:29:01 +0000680 }
681 }
Chris Lattner00950542001-06-06 20:29:01 +0000682 LateResolvers.clear();
683}
684
Chris Lattner4a42e902001-10-22 05:56:09 +0000685// ResolveTypeTo - A brand new type was just declared. This means that (if
686// name is not null) things referencing Name can be resolved. Otherwise, things
687// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000688//
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000689static void ResolveTypeTo(std::string *Name, const Type *ToTy) {
Chris Lattner355ad1f2005-03-21 06:27:42 +0000690 ValID D;
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000691 if (Name)
692 D = ValID::createLocalName(*Name);
693 else
694 D = ValID::createLocalID(CurModule.Types.size());
Chris Lattner00950542001-06-06 20:29:01 +0000695
Reid Spencerb78b9082006-11-28 07:28:14 +0000696 std::map<ValID, PATypeHolder>::iterator I =
Chris Lattner355ad1f2005-03-21 06:27:42 +0000697 CurModule.LateResolveTypes.find(D);
698 if (I != CurModule.LateResolveTypes.end()) {
Reid Spencerb78b9082006-11-28 07:28:14 +0000699 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattner355ad1f2005-03-21 06:27:42 +0000700 CurModule.LateResolveTypes.erase(I);
Chris Lattner30c89792001-09-07 16:35:17 +0000701 }
702}
703
Chris Lattner1781aca2001-09-18 04:00:54 +0000704// setValueName - Set the specified value to the name given. The name may be
705// null potentially, in which case this is a noop. The string passed in is
Chris Lattner8ab406d2004-07-13 07:59:27 +0000706// assumed to be a malloc'd string buffer, and is free'd by this function.
707//
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000708static void setValueName(Value *V, std::string *NameStr) {
Reid Spencerb2d17862007-01-26 08:04:51 +0000709 if (!NameStr) return;
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000710 std::string Name(*NameStr); // Copy string
711 delete NameStr; // Free old string
Chris Lattnerc9aea522004-07-13 08:12:39 +0000712
Reid Spencerb2d17862007-01-26 08:04:51 +0000713 if (V->getType() == Type::VoidTy) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000714 GenerateError("Can't assign name '" + Name+"' to value with void type");
Reid Spencerb2d17862007-01-26 08:04:51 +0000715 return;
Chris Lattner8ab406d2004-07-13 07:59:27 +0000716 }
Reid Spencerb2d17862007-01-26 08:04:51 +0000717
Reid Spencer1755a9a2007-02-05 17:01:20 +0000718 assert(inFunctionScope() && "Must be in function scope!");
Reid Spenceref9b9a72007-02-05 20:47:22 +0000719 ValueSymbolTable &ST = CurFun.CurrentFunction->getValueSymbolTable();
720 if (ST.lookup(Name)) {
Reid Spencerb2d17862007-01-26 08:04:51 +0000721 GenerateError("Redefinition of value '" + Name + "' of type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +0000722 V->getType()->getDescription() + "'");
Reid Spencerb2d17862007-01-26 08:04:51 +0000723 return;
724 }
725
726 // Set the name.
727 V->setName(Name);
Chris Lattner8ab406d2004-07-13 07:59:27 +0000728}
729
Chris Lattnerd88998d2004-07-14 08:23:52 +0000730/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
731/// this is a declaration, otherwise it is a definition.
Chris Lattnerb2b96672005-11-12 18:21:21 +0000732static GlobalVariable *
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000733ParseGlobalVariable(std::string *NameStr,
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000734 GlobalValue::LinkageTypes Linkage,
735 GlobalValue::VisibilityTypes Visibility,
Chris Lattnerb2b96672005-11-12 18:21:21 +0000736 bool isConstantGlobal, const Type *Ty,
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +0000737 Constant *Initializer, bool IsThreadLocal) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000738 if (isa<FunctionType>(Ty)) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000739 GenerateError("Cannot declare global vars of function type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000740 return 0;
741 }
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000742
Misha Brukman5a2a3822005-05-10 22:02:28 +0000743 const PointerType *PTy = PointerType::get(Ty);
Chris Lattnera09000d2004-07-14 23:03:46 +0000744
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000745 std::string Name;
746 if (NameStr) {
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000747 Name = *NameStr; // Copy string
748 delete NameStr; // Free old string
Chris Lattnerd88998d2004-07-14 08:23:52 +0000749 }
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000750
Chris Lattner8a327842004-07-14 21:44:00 +0000751 // See if this global value was forward referenced. If so, recycle the
752 // object.
Misha Brukman5a2a3822005-05-10 22:02:28 +0000753 ValID ID;
Chris Lattner8a327842004-07-14 21:44:00 +0000754 if (!Name.empty()) {
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000755 ID = ValID::createGlobalName(Name);
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000756 } else {
Reid Spencer186a43f2007-03-19 18:39:36 +0000757 ID = ValID::createGlobalID(CurModule.Values.size());
Chris Lattner8a327842004-07-14 21:44:00 +0000758 }
759
Reid Spencer863dd882007-04-28 16:06:50 +0000760 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000761 // Move the global to the end of the list, from whereever it was
Chris Lattner8a327842004-07-14 21:44:00 +0000762 // previously inserted.
763 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
764 CurModule.CurrentModule->getGlobalList().remove(GV);
765 CurModule.CurrentModule->getGlobalList().push_back(GV);
766 GV->setInitializer(Initializer);
767 GV->setLinkage(Linkage);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000768 GV->setVisibility(Visibility);
Chris Lattner8a327842004-07-14 21:44:00 +0000769 GV->setConstant(isConstantGlobal);
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +0000770 GV->setThreadLocal(IsThreadLocal);
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000771 InsertValue(GV, CurModule.Values);
Chris Lattnerb2b96672005-11-12 18:21:21 +0000772 return GV;
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000773 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000774
Reid Spenceref9b9a72007-02-05 20:47:22 +0000775 // If this global has a name
Chris Lattnera09000d2004-07-14 23:03:46 +0000776 if (!Name.empty()) {
Reid Spenceref9b9a72007-02-05 20:47:22 +0000777 // if the global we're parsing has an initializer (is a definition) and
778 // has external linkage.
779 if (Initializer && Linkage != GlobalValue::InternalLinkage)
780 // If there is already a global with external linkage with this name
781 if (CurModule.CurrentModule->getGlobalVariable(Name, false)) {
782 // If we allow this GVar to get created, it will be renamed in the
783 // symbol table because it conflicts with an existing GVar. We can't
784 // allow redefinition of GVars whose linking indicates that their name
785 // must stay the same. Issue the error.
786 GenerateError("Redefinition of global variable named '" + Name +
787 "' of type '" + Ty->getDescription() + "'");
788 return 0;
789 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000790 }
791
792 // Otherwise there is no existing GV to use, create one now.
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000793 GlobalVariable *GV =
Misha Brukman5a2a3822005-05-10 22:02:28 +0000794 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +0000795 CurModule.CurrentModule, IsThreadLocal);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000796 GV->setVisibility(Visibility);
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000797 InsertValue(GV, CurModule.Values);
Chris Lattnerb2b96672005-11-12 18:21:21 +0000798 return GV;
Chris Lattnerd88998d2004-07-14 08:23:52 +0000799}
Chris Lattner8ab406d2004-07-13 07:59:27 +0000800
Reid Spencer75719bf2004-05-26 21:48:31 +0000801// setTypeName - Set the specified type to the name given. The name may be
802// null potentially, in which case this is a noop. The string passed in is
803// assumed to be a malloc'd string buffer, and is freed by this function.
804//
805// This function returns true if the type has already been defined, but is
806// allowed to be redefined in the specified context. If the name is a new name
807// for the type plane, it is inserted and false is returned.
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000808static bool setTypeName(const Type *T, std::string *NameStr) {
Reid Spencer1755a9a2007-02-05 17:01:20 +0000809 assert(!inFunctionScope() && "Can't give types function-local names!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000810 if (NameStr == 0) return false;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000811
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000812 std::string Name(*NameStr); // Copy string
813 delete NameStr; // Free old string
Reid Spencer75719bf2004-05-26 21:48:31 +0000814
815 // We don't allow assigning names to void type
Reid Spencer5b7e7532006-09-28 19:28:24 +0000816 if (T == Type::VoidTy) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000817 GenerateError("Can't assign name '" + Name + "' to the void type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000818 return false;
819 }
Reid Spencer75719bf2004-05-26 21:48:31 +0000820
Chris Lattnera09000d2004-07-14 23:03:46 +0000821 // Set the type name, checking for conflicts as we do so.
822 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000823
Chris Lattnera09000d2004-07-14 23:03:46 +0000824 if (AlreadyExists) { // Inserting a name that is already defined???
825 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
Reid Spencer1755a9a2007-02-05 17:01:20 +0000826 assert(Existing && "Conflict but no matching type?!");
Chris Lattnera09000d2004-07-14 23:03:46 +0000827
Reid Spencer75719bf2004-05-26 21:48:31 +0000828 // There is only one case where this is allowed: when we are refining an
829 // opaque type. In this case, Existing will be an opaque type.
830 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
831 // We ARE replacing an opaque type!
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000832 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000833 return true;
834 }
835
836 // Otherwise, this is an attempt to redefine a type. That's okay if
837 // the redefinition is identical to the original. This will be so if
838 // Existing and T point to the same Type object. In this one case we
839 // allow the equivalent redefinition.
840 if (Existing == T) return true; // Yes, it's equal.
841
842 // Any other kind of (non-equivalent) redefinition is an error.
Reid Spencer90e2f632007-01-05 21:50:38 +0000843 GenerateError("Redefinition of type named '" + Name + "' of type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +0000844 T->getDescription() + "'");
Reid Spencer75719bf2004-05-26 21:48:31 +0000845 }
846
Reid Spencer75719bf2004-05-26 21:48:31 +0000847 return false;
848}
Chris Lattner8896eda2001-07-09 19:38:36 +0000849
Chris Lattner30c89792001-09-07 16:35:17 +0000850//===----------------------------------------------------------------------===//
851// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000852//
Chris Lattner8896eda2001-07-09 19:38:36 +0000853
Chris Lattner3b073862004-02-09 03:19:29 +0000854// TypeContains - Returns true if Ty directly contains E in it.
Chris Lattner30c89792001-09-07 16:35:17 +0000855//
856static bool TypeContains(const Type *Ty, const Type *E) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000857 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
Chris Lattner6d8dbec2004-12-08 16:13:53 +0000858 E) != Ty->subtype_end();
Chris Lattner3b073862004-02-09 03:19:29 +0000859}
860
861namespace {
862 struct UpRefRecord {
863 // NestingLevel - The number of nesting levels that need to be popped before
864 // this type is resolved.
865 unsigned NestingLevel;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000866
Chris Lattner3b073862004-02-09 03:19:29 +0000867 // LastContainedTy - This is the type at the current binding level for the
868 // type. Every time we reduce the nesting level, this gets updated.
869 const Type *LastContainedTy;
870
871 // UpRefTy - This is the actual opaque type that the upreference is
872 // represented with.
873 OpaqueType *UpRefTy;
874
875 UpRefRecord(unsigned NL, OpaqueType *URTy)
876 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
877 };
Chris Lattner30c89792001-09-07 16:35:17 +0000878}
Chris Lattner698b56e2001-07-20 19:15:08 +0000879
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000880// UpRefs - A list of the outstanding upreferences that need to be resolved.
Chris Lattner3b073862004-02-09 03:19:29 +0000881static std::vector<UpRefRecord> UpRefs;
Chris Lattner30c89792001-09-07 16:35:17 +0000882
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000883/// HandleUpRefs - Every time we finish a new layer of types, this function is
884/// called. It loops through the UpRefs vector, which is a list of the
885/// currently active types. For each type, if the up reference is contained in
886/// the newly completed type, we decrement the level count. When the level
887/// count reaches zero, the upreferenced type is the type that is passed in:
888/// thus we can complete the cycle.
889///
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000890static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner703e92f2006-08-18 17:34:24 +0000891 // If Ty isn't abstract, or if there are no up-references in it, then there is
892 // nothing to resolve here.
893 if (!ty->isAbstract() || UpRefs.empty()) return ty;
894
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000895 PATypeHolder Ty(ty);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000896 UR_OUT("Type '" << Ty->getDescription() <<
Chris Lattner5084d032001-11-02 07:46:26 +0000897 "' newly formed. Resolving upreferences.\n" <<
898 UpRefs.size() << " upreferences active!\n");
Chris Lattner026a8ce2004-02-09 18:53:54 +0000899
900 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
901 // to zero), we resolve them all together before we resolve them to Ty. At
902 // the end of the loop, if there is anything to resolve to Ty, it will be in
903 // this variable.
904 OpaqueType *TypeToResolve = 0;
905
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000906 for (unsigned i = 0; i != UpRefs.size(); ++i) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000907 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
908 << UpRefs[i].second->getDescription() << ") = "
Reid Spencer3271ed52004-07-18 00:08:11 +0000909 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
Chris Lattner3b073862004-02-09 03:19:29 +0000910 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
911 // Decrement level of upreference
912 unsigned Level = --UpRefs[i].NestingLevel;
913 UpRefs[i].LastContainedTy = Ty;
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000914 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
Misha Brukman5a2a3822005-05-10 22:02:28 +0000915 if (Level == 0) { // Upreference should be resolved!
Chris Lattner026a8ce2004-02-09 18:53:54 +0000916 if (!TypeToResolve) {
917 TypeToResolve = UpRefs[i].UpRefTy;
918 } else {
919 UR_OUT(" * Resolving upreference for "
920 << UpRefs[i].second->getDescription() << "\n";
921 std::string OldName = UpRefs[i].UpRefTy->getDescription());
922 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
923 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
924 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
925 }
Reid Spencer3271ed52004-07-18 00:08:11 +0000926 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000927 --i; // Do not skip the next element...
Chris Lattner30c89792001-09-07 16:35:17 +0000928 }
929 }
Chris Lattner8896eda2001-07-09 19:38:36 +0000930 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000931
Chris Lattner026a8ce2004-02-09 18:53:54 +0000932 if (TypeToResolve) {
933 UR_OUT(" * Resolving upreference for "
934 << UpRefs[i].second->getDescription() << "\n";
Chris Lattner16af11d2004-02-09 21:03:38 +0000935 std::string OldName = TypeToResolve->getDescription());
Chris Lattner026a8ce2004-02-09 18:53:54 +0000936 TypeToResolve->refineAbstractTypeTo(Ty);
937 }
938
Chris Lattner8896eda2001-07-09 19:38:36 +0000939 return Ty;
940}
941
Chris Lattner00950542001-06-06 20:29:01 +0000942//===----------------------------------------------------------------------===//
943// RunVMAsmParser - Define an interface to this parser
944//===----------------------------------------------------------------------===//
945//
Reid Spencere1553cc2006-12-31 05:40:12 +0000946static Module* RunParser(Module * M);
947
Chris Lattner86427632004-07-14 06:39:48 +0000948Module *llvm::RunVMAsmParser(const std::string &Filename, FILE *F) {
Chris Lattner6184feb2005-05-20 03:25:47 +0000949 set_scan_file(F);
950
Chris Lattnera2850432001-07-22 18:36:00 +0000951 CurFilename = Filename;
Chris Lattner6184feb2005-05-20 03:25:47 +0000952 return RunParser(new Module(CurFilename));
953}
Chris Lattner00950542001-06-06 20:29:01 +0000954
Chris Lattner6184feb2005-05-20 03:25:47 +0000955Module *llvm::RunVMAsmParser(const char * AsmString, Module * M) {
956 set_scan_string(AsmString);
Chris Lattner42570982003-11-26 07:24:58 +0000957
Chris Lattner6184feb2005-05-20 03:25:47 +0000958 CurFilename = "from_memory";
959 if (M == NULL) {
960 return RunParser(new Module (CurFilename));
961 } else {
962 return RunParser(M);
963 }
Chris Lattner00950542001-06-06 20:29:01 +0000964}
965
966%}
967
968%union {
Brian Gaeked0fde302003-11-11 22:41:34 +0000969 llvm::Module *ModuleVal;
970 llvm::Function *FunctionVal;
Brian Gaeked0fde302003-11-11 22:41:34 +0000971 llvm::BasicBlock *BasicBlockVal;
972 llvm::TerminatorInst *TermInstVal;
973 llvm::Instruction *InstVal;
Reid Spencer08de34b2006-12-03 05:45:44 +0000974 llvm::Constant *ConstVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000975
Reid Spencer08de34b2006-12-03 05:45:44 +0000976 const llvm::Type *PrimType;
Reid Spencere1553cc2006-12-31 05:40:12 +0000977 std::list<llvm::PATypeHolder> *TypeList;
Reid Spencer08de34b2006-12-03 05:45:44 +0000978 llvm::PATypeHolder *TypeVal;
979 llvm::Value *ValueVal;
Reid Spencer08de34b2006-12-03 05:45:44 +0000980 std::vector<llvm::Value*> *ValueList;
Reid Spencere1553cc2006-12-31 05:40:12 +0000981 llvm::ArgListType *ArgList;
982 llvm::TypeWithAttrs TypeWithAttrs;
983 llvm::TypeWithAttrsList *TypeWithAttrsList;
Dale Johanneseneb57ea72007-11-05 21:20:28 +0000984 llvm::ParamList *ParamList;
Reid Spencere1553cc2006-12-31 05:40:12 +0000985
Misha Brukman5a2a3822005-05-10 22:02:28 +0000986 // Represent the RHS of PHI node
Reid Spencer08de34b2006-12-03 05:45:44 +0000987 std::list<std::pair<llvm::Value*,
988 llvm::BasicBlock*> > *PHIList;
Brian Gaeked0fde302003-11-11 22:41:34 +0000989 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
Reid Spencer08de34b2006-12-03 05:45:44 +0000990 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000991
Brian Gaeked0fde302003-11-11 22:41:34 +0000992 llvm::GlobalValue::LinkageTypes Linkage;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000993 llvm::GlobalValue::VisibilityTypes Visibility;
Reid Spencer5694b6e2007-04-09 06:17:21 +0000994 uint16_t ParamAttrs;
Reid Spencerf2310042007-02-28 02:23:44 +0000995 llvm::APInt *APIntVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000996 int64_t SInt64Val;
997 uint64_t UInt64Val;
998 int SIntVal;
999 unsigned UIntVal;
Dale Johannesen43421b32007-09-06 18:13:44 +00001000 llvm::APFloat *FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +00001001 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +00001002
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001003 std::string *StrVal; // This memory must be deleted
1004 llvm::ValID ValIDVal;
Chris Lattner00950542001-06-06 20:29:01 +00001005
Reid Spencer08de34b2006-12-03 05:45:44 +00001006 llvm::Instruction::BinaryOps BinaryOpVal;
1007 llvm::Instruction::TermOps TermOpVal;
1008 llvm::Instruction::MemoryOps MemOpVal;
1009 llvm::Instruction::CastOps CastOpVal;
1010 llvm::Instruction::OtherOps OtherOpVal;
Reid Spencer08de34b2006-12-03 05:45:44 +00001011 llvm::ICmpInst::Predicate IPredicate;
1012 llvm::FCmpInst::Predicate FPredicate;
Chris Lattner00950542001-06-06 20:29:01 +00001013}
1014
Reid Spencere1553cc2006-12-31 05:40:12 +00001015%type <ModuleVal> Module
Chris Lattner79df7c02002-03-26 18:01:55 +00001016%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +00001017%type <BasicBlockVal> BasicBlock InstructionList
1018%type <TermInstVal> BBTerminatorInst
1019%type <InstVal> Inst InstVal MemoryInst
Anton Korobeynikova80e1182007-04-28 13:45:00 +00001020%type <ConstVal> ConstVal ConstExpr AliaseeRef
Chris Lattner6cdb0112001-11-26 16:54:11 +00001021%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +00001022%type <ArgList> ArgList ArgListH
Chris Lattnerc24d2082001-06-11 15:04:20 +00001023%type <PHIList> PHIList
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001024%type <ParamList> ParamList // For call param lists & GEP indices
Reid Spencere1553cc2006-12-31 05:40:12 +00001025%type <ValueList> IndexList // For GEP indices
1026%type <TypeList> TypeListI
1027%type <TypeWithAttrsList> ArgTypeList ArgTypeListI
Reid Spencer2c261782007-01-05 17:06:19 +00001028%type <TypeWithAttrs> ArgType
Chris Lattner00950542001-06-06 20:29:01 +00001029%type <JumpTable> JumpTable
Chris Lattner4ad02e72003-04-16 20:28:45 +00001030%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00001031%type <BoolVal> ThreadLocal // 'thread_local' or not
Chris Lattner15c9c032003-09-08 18:20:29 +00001032%type <BoolVal> OptVolatile // 'volatile' or not
Chris Lattnerddb6db42005-05-06 05:51:46 +00001033%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
Chris Lattneraa2c8532006-01-25 22:26:43 +00001034%type <BoolVal> OptSideEffect // 'sideeffect' or not.
Reid Spencere1553cc2006-12-31 05:40:12 +00001035%type <Linkage> GVInternalLinkage GVExternalLinkage
1036%type <Linkage> FunctionDefineLinkage FunctionDeclareLinkage
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001037%type <Linkage> AliasLinkage
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001038%type <Visibility> GVVisibilityStyle
Chris Lattner00950542001-06-06 20:29:01 +00001039
Chris Lattner2079fde2001-10-13 06:41:08 +00001040// ValueRef - Unresolved reference to a definition or BB
1041%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001042%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +00001043// Tokens and types for handling constant integer values
1044//
1045// ESINT64VAL - A negative number within long long range
1046%token <SInt64Val> ESINT64VAL
1047
1048// EUINT64VAL - A positive number within uns. long long range
1049%token <UInt64Val> EUINT64VAL
Chris Lattner00950542001-06-06 20:29:01 +00001050
Reid Spencerf2310042007-02-28 02:23:44 +00001051// ESAPINTVAL - A negative number with arbitrary precision
1052%token <APIntVal> ESAPINTVAL
1053
1054// EUAPINTVAL - A positive number with arbitrary precision
1055%token <APIntVal> EUAPINTVAL
1056
Reid Spencerb2d17862007-01-26 08:04:51 +00001057%token <UIntVal> LOCALVAL_ID GLOBALVAL_ID // %123 @123
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001058%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +00001059
1060// Built in types...
Reid Spencer2c261782007-01-05 17:06:19 +00001061%type <TypeVal> Types ResultTypes
Reid Spencere1553cc2006-12-31 05:40:12 +00001062%type <PrimType> IntType FPType PrimType // Classifications
Reid Spencer8088e9d2007-01-13 05:00:20 +00001063%token <PrimType> VOID INTTYPE
Dale Johannesen320fc8a2007-08-03 01:03:46 +00001064%token <PrimType> FLOAT DOUBLE X86_FP80 FP128 PPC_FP128 LABEL
Reid Spencerb951bc02006-12-29 20:29:48 +00001065%token TYPE
Chris Lattner00950542001-06-06 20:29:01 +00001066
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001067
Reid Spenceraa7c2f82007-05-19 07:21:26 +00001068%token<StrVal> LOCALVAR GLOBALVAR LABELSTR
1069%token<StrVal> STRINGCONSTANT ATSTRINGCONSTANT PCTSTRINGCONSTANT
Reid Spencerb2d17862007-01-26 08:04:51 +00001070%type <StrVal> LocalName OptLocalName OptLocalAssign
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001071%type <StrVal> GlobalName OptGlobalAssign GlobalAssign
Chris Lattnerb2b96672005-11-12 18:21:21 +00001072%type <StrVal> OptSection SectionString
Chris Lattner00950542001-06-06 20:29:01 +00001073
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001074%type <UIntVal> OptAlign OptCAlign
1075
Reid Spencer744d0362007-04-09 01:55:42 +00001076%token ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001077%token DECLARE DEFINE GLOBAL CONSTANT SECTION ALIAS VOLATILE THREAD_LOCAL
Reid Spencere1553cc2006-12-31 05:40:12 +00001078%token TO DOTDOTDOT NULL_TOK UNDEF INTERNAL LINKONCE WEAK APPENDING
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00001079%token DLLIMPORT DLLEXPORT EXTERN_WEAK
Reid Spencerb2d17862007-01-26 08:04:51 +00001080%token OPAQUE EXTERNAL TARGET TRIPLE ALIGN
Chris Lattneraa2c8532006-01-25 22:26:43 +00001081%token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001082%token CC_TOK CCC_TOK FASTCC_TOK COLDCC_TOK X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
Chris Lattner10b27112006-10-22 06:07:41 +00001083%token DATALAYOUT
Chris Lattnera8e8f162005-05-06 20:27:19 +00001084%type <UIntVal> OptCallingConv
Reid Spencer2c261782007-01-05 17:06:19 +00001085%type <ParamAttrs> OptParamAttrs ParamAttr
1086%type <ParamAttrs> OptFuncAttrs FuncAttr
Chris Lattner00950542001-06-06 20:29:01 +00001087
Misha Brukman5a2a3822005-05-10 22:02:28 +00001088// Basic Block Terminating Operators
Chris Lattner16710e92004-10-16 18:17:13 +00001089%token <TermOpVal> RET BR SWITCH INVOKE UNWIND UNREACHABLE
Chris Lattner00950542001-06-06 20:29:01 +00001090
Misha Brukman5a2a3822005-05-10 22:02:28 +00001091// Binary Operators
Reid Spencere4d87aa2006-12-23 06:05:41 +00001092%type <BinaryOpVal> ArithmeticOps LogicalOps // Binops Subcatagories
Reid Spencer0a783f72006-11-02 01:53:59 +00001093%token <BinaryOpVal> ADD SUB MUL UDIV SDIV FDIV UREM SREM FREM AND OR XOR
Reid Spencer832254e2007-02-02 02:16:23 +00001094%token <BinaryOpVal> SHL LSHR ASHR
1095
Reid Spencer08de34b2006-12-03 05:45:44 +00001096%token <OtherOpVal> ICMP FCMP
Reid Spencer08de34b2006-12-03 05:45:44 +00001097%type <IPredicate> IPredicates
Reid Spencer08de34b2006-12-03 05:45:44 +00001098%type <FPredicate> FPredicates
Reid Spencer9f746f42006-12-03 06:58:07 +00001099%token EQ NE SLT SGT SLE SGE ULT UGT ULE UGE
1100%token OEQ ONE OLT OGT OLE OGE ORD UNO UEQ UNE
Chris Lattner00950542001-06-06 20:29:01 +00001101
1102// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001103%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +00001104
Reid Spencer3da59db2006-11-27 01:05:10 +00001105// Cast Operators
1106%type <CastOpVal> CastOps
1107%token <CastOpVal> TRUNC ZEXT SEXT FPTRUNC FPEXT BITCAST
1108%token <CastOpVal> UITOFP SITOFP FPTOUI FPTOSI INTTOPTR PTRTOINT
1109
Chris Lattner027dcc52001-07-08 21:10:27 +00001110// Other Operators
Reid Spencer832254e2007-02-02 02:16:23 +00001111%token <OtherOpVal> PHI_TOK SELECT VAARG
Chris Lattner4c949082006-04-08 01:18:35 +00001112%token <OtherOpVal> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
Chris Lattnerddb6db42005-05-06 05:51:46 +00001113
Reid Spencer2c261782007-01-05 17:06:19 +00001114// Function Attributes
Duncan Sands36397f52007-07-27 12:58:54 +00001115%token SIGNEXT ZEROEXT NORETURN INREG SRET NOUNWIND NOALIAS BYVAL NEST
Anton Korobeynikov0adb7b42007-11-14 09:52:30 +00001116%token CONST PURE
Chris Lattner027dcc52001-07-08 21:10:27 +00001117
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001118// Visibility Styles
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +00001119%token DEFAULT HIDDEN PROTECTED
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001120
Chris Lattner00950542001-06-06 20:29:01 +00001121%start Module
1122%%
1123
Chris Lattner00950542001-06-06 20:29:01 +00001124
Misha Brukman5a2a3822005-05-10 22:02:28 +00001125// Operations that are notably excluded from this list include:
Chris Lattner00950542001-06-06 20:29:01 +00001126// RET, BR, & SWITCH because they end basic blocks and are treated specially.
1127//
Reid Spencer0a783f72006-11-02 01:53:59 +00001128ArithmeticOps: ADD | SUB | MUL | UDIV | SDIV | FDIV | UREM | SREM | FREM;
Reid Spencer832254e2007-02-02 02:16:23 +00001129LogicalOps : SHL | LSHR | ASHR | AND | OR | XOR;
Reid Spencer3da59db2006-11-27 01:05:10 +00001130CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | BITCAST |
1131 UITOFP | SITOFP | FPTOUI | FPTOSI | INTTOPTR | PTRTOINT;
Reid Spencer832254e2007-02-02 02:16:23 +00001132
Reid Spencer9f746f42006-12-03 06:58:07 +00001133IPredicates
Reid Spencer763ed5e2006-12-04 05:20:06 +00001134 : EQ { $$ = ICmpInst::ICMP_EQ; } | NE { $$ = ICmpInst::ICMP_NE; }
Reid Spencer9f746f42006-12-03 06:58:07 +00001135 | SLT { $$ = ICmpInst::ICMP_SLT; } | SGT { $$ = ICmpInst::ICMP_SGT; }
1136 | SLE { $$ = ICmpInst::ICMP_SLE; } | SGE { $$ = ICmpInst::ICMP_SGE; }
1137 | ULT { $$ = ICmpInst::ICMP_ULT; } | UGT { $$ = ICmpInst::ICMP_UGT; }
1138 | ULE { $$ = ICmpInst::ICMP_ULE; } | UGE { $$ = ICmpInst::ICMP_UGE; }
1139 ;
1140
1141FPredicates
1142 : OEQ { $$ = FCmpInst::FCMP_OEQ; } | ONE { $$ = FCmpInst::FCMP_ONE; }
1143 | OLT { $$ = FCmpInst::FCMP_OLT; } | OGT { $$ = FCmpInst::FCMP_OGT; }
1144 | OLE { $$ = FCmpInst::FCMP_OLE; } | OGE { $$ = FCmpInst::FCMP_OGE; }
1145 | ORD { $$ = FCmpInst::FCMP_ORD; } | UNO { $$ = FCmpInst::FCMP_UNO; }
1146 | UEQ { $$ = FCmpInst::FCMP_UEQ; } | UNE { $$ = FCmpInst::FCMP_UNE; }
1147 | ULT { $$ = FCmpInst::FCMP_ULT; } | UGT { $$ = FCmpInst::FCMP_UGT; }
1148 | ULE { $$ = FCmpInst::FCMP_ULE; } | UGE { $$ = FCmpInst::FCMP_UGE; }
1149 | TRUETOK { $$ = FCmpInst::FCMP_TRUE; }
1150 | FALSETOK { $$ = FCmpInst::FCMP_FALSE; }
1151 ;
Chris Lattner00950542001-06-06 20:29:01 +00001152
Chris Lattnere98dda62001-07-14 06:10:16 +00001153// These are some types that allow classification if we only want a particular
1154// thing... for example, only a signed, unsigned, or integral type.
Reid Spencera54b7cb2007-01-12 07:05:14 +00001155IntType : INTTYPE;
Dale Johannesen320fc8a2007-08-03 01:03:46 +00001156FPType : FLOAT | DOUBLE | PPC_FP128 | FP128 | X86_FP80;
Chris Lattner00950542001-06-06 20:29:01 +00001157
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001158LocalName : LOCALVAR | STRINGCONSTANT | PCTSTRINGCONSTANT ;
Reid Spencerb2d17862007-01-26 08:04:51 +00001159OptLocalName : LocalName | /*empty*/ { $$ = 0; };
1160
1161/// OptLocalAssign - Value producing statements have an optional assignment
1162/// component.
1163OptLocalAssign : LocalName '=' {
1164 $$ = $1;
1165 CHECK_FOR_ERROR
1166 }
1167 | /*empty*/ {
1168 $$ = 0;
1169 CHECK_FOR_ERROR
1170 };
1171
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001172GlobalName : GLOBALVAR | ATSTRINGCONSTANT ;
Reid Spencerb2d17862007-01-26 08:04:51 +00001173
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001174OptGlobalAssign : GlobalAssign
Misha Brukman5a2a3822005-05-10 22:02:28 +00001175 | /*empty*/ {
1176 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00001177 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001178 };
Chris Lattner00950542001-06-06 20:29:01 +00001179
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001180GlobalAssign : GlobalName '=' {
1181 $$ = $1;
1182 CHECK_FOR_ERROR
Anton Korobeynikovc0fabcb2007-04-25 18:07:40 +00001183 };
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001184
Reid Spencerb951bc02006-12-29 20:29:48 +00001185GVInternalLinkage
1186 : INTERNAL { $$ = GlobalValue::InternalLinkage; }
1187 | WEAK { $$ = GlobalValue::WeakLinkage; }
1188 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1189 | APPENDING { $$ = GlobalValue::AppendingLinkage; }
1190 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
1191 ;
1192
1193GVExternalLinkage
1194 : DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1195 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
1196 | EXTERNAL { $$ = GlobalValue::ExternalLinkage; }
1197 ;
1198
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001199GVVisibilityStyle
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +00001200 : /*empty*/ { $$ = GlobalValue::DefaultVisibility; }
1201 | DEFAULT { $$ = GlobalValue::DefaultVisibility; }
1202 | HIDDEN { $$ = GlobalValue::HiddenVisibility; }
1203 | PROTECTED { $$ = GlobalValue::ProtectedVisibility; }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001204 ;
1205
Reid Spencere1553cc2006-12-31 05:40:12 +00001206FunctionDeclareLinkage
1207 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1208 | DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1209 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
Reid Spencerb951bc02006-12-29 20:29:48 +00001210 ;
1211
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001212FunctionDefineLinkage
Reid Spencere1553cc2006-12-31 05:40:12 +00001213 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1214 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
Reid Spencerb951bc02006-12-29 20:29:48 +00001215 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1216 | WEAK { $$ = GlobalValue::WeakLinkage; }
1217 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
Reid Spencerb951bc02006-12-29 20:29:48 +00001218 ;
Chris Lattner30c89792001-09-07 16:35:17 +00001219
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001220AliasLinkage
1221 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1222 | WEAK { $$ = GlobalValue::WeakLinkage; }
1223 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
1224 ;
1225
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00001226OptCallingConv : /*empty*/ { $$ = CallingConv::C; } |
1227 CCC_TOK { $$ = CallingConv::C; } |
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00001228 FASTCC_TOK { $$ = CallingConv::Fast; } |
1229 COLDCC_TOK { $$ = CallingConv::Cold; } |
1230 X86_STDCALLCC_TOK { $$ = CallingConv::X86_StdCall; } |
1231 X86_FASTCALLCC_TOK { $$ = CallingConv::X86_FastCall; } |
1232 CC_TOK EUINT64VAL {
Chris Lattnera8e8f162005-05-06 20:27:19 +00001233 if ((unsigned)$2 != $2)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001234 GEN_ERROR("Calling conv too large");
Chris Lattnera8e8f162005-05-06 20:27:19 +00001235 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001236 CHECK_FOR_ERROR
Chris Lattnera8e8f162005-05-06 20:27:19 +00001237 };
1238
Reid Spencer9445e9a2007-07-19 23:13:04 +00001239ParamAttr : ZEROEXT { $$ = ParamAttr::ZExt; }
Reid Spencer1d3d2302007-07-31 02:57:37 +00001240 | ZEXT { $$ = ParamAttr::ZExt; }
Reid Spencer9445e9a2007-07-19 23:13:04 +00001241 | SIGNEXT { $$ = ParamAttr::SExt; }
Reid Spencer1d3d2302007-07-31 02:57:37 +00001242 | SEXT { $$ = ParamAttr::SExt; }
Zhou Shengfebca342007-06-05 05:28:26 +00001243 | INREG { $$ = ParamAttr::InReg; }
1244 | SRET { $$ = ParamAttr::StructRet; }
1245 | NOALIAS { $$ = ParamAttr::NoAlias; }
Duncan Sands36397f52007-07-27 12:58:54 +00001246 | BYVAL { $$ = ParamAttr::ByVal; }
1247 | NEST { $$ = ParamAttr::Nest; }
Reid Spencere1553cc2006-12-31 05:40:12 +00001248 ;
1249
Reid Spencer18da0722007-04-11 02:44:20 +00001250OptParamAttrs : /* empty */ { $$ = ParamAttr::None; }
Reid Spencer2c261782007-01-05 17:06:19 +00001251 | OptParamAttrs ParamAttr {
Reid Spencer5694b6e2007-04-09 06:17:21 +00001252 $$ = $1 | $2;
Reid Spencere1553cc2006-12-31 05:40:12 +00001253 }
1254 ;
1255
Reid Spencer18da0722007-04-11 02:44:20 +00001256FuncAttr : NORETURN { $$ = ParamAttr::NoReturn; }
1257 | NOUNWIND { $$ = ParamAttr::NoUnwind; }
Reid Spencer9445e9a2007-07-19 23:13:04 +00001258 | ZEROEXT { $$ = ParamAttr::ZExt; }
1259 | SIGNEXT { $$ = ParamAttr::SExt; }
Anton Korobeynikov0adb7b42007-11-14 09:52:30 +00001260 | PURE { $$ = ParamAttr::Pure; }
1261 | CONST { $$ = ParamAttr::Const; }
Reid Spencer2c261782007-01-05 17:06:19 +00001262 ;
1263
Reid Spencer18da0722007-04-11 02:44:20 +00001264OptFuncAttrs : /* empty */ { $$ = ParamAttr::None; }
Reid Spencer2c261782007-01-05 17:06:19 +00001265 | OptFuncAttrs FuncAttr {
Reid Spencer5694b6e2007-04-09 06:17:21 +00001266 $$ = $1 | $2;
Reid Spencer2c261782007-01-05 17:06:19 +00001267 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001268 ;
1269
Chris Lattner66db8e42005-11-06 06:34:12 +00001270// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1271// a comma before it.
Chris Lattner87ac9722005-11-06 06:46:28 +00001272OptAlign : /*empty*/ { $$ = 0; } |
Chris Lattnerb2b96672005-11-12 18:21:21 +00001273 ALIGN EUINT64VAL {
1274 $$ = $2;
1275 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001276 GEN_ERROR("Alignment must be a power of two");
Reid Spencer61c83e02006-08-18 08:43:06 +00001277 CHECK_FOR_ERROR
Chris Lattnerb2b96672005-11-12 18:21:21 +00001278};
Chris Lattner66db8e42005-11-06 06:34:12 +00001279OptCAlign : /*empty*/ { $$ = 0; } |
Chris Lattnerb2b96672005-11-12 18:21:21 +00001280 ',' ALIGN EUINT64VAL {
1281 $$ = $3;
1282 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001283 GEN_ERROR("Alignment must be a power of two");
Reid Spencer61c83e02006-08-18 08:43:06 +00001284 CHECK_FOR_ERROR
Chris Lattnerb2b96672005-11-12 18:21:21 +00001285};
1286
Chris Lattner66db8e42005-11-06 06:34:12 +00001287
Chris Lattner164c3782005-11-12 00:11:10 +00001288SectionString : SECTION STRINGCONSTANT {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001289 for (unsigned i = 0, e = $2->length(); i != e; ++i)
1290 if ((*$2)[i] == '"' || (*$2)[i] == '\\')
Reid Spencerf4fa5902007-02-05 10:16:10 +00001291 GEN_ERROR("Invalid character in section name");
Chris Lattner164c3782005-11-12 00:11:10 +00001292 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001293 CHECK_FOR_ERROR
Chris Lattner164c3782005-11-12 00:11:10 +00001294};
1295
1296OptSection : /*empty*/ { $$ = 0; } |
1297 SectionString { $$ = $1; };
Chris Lattner164c3782005-11-12 00:11:10 +00001298
Chris Lattnerb2b96672005-11-12 18:21:21 +00001299// GlobalVarAttributes - Used to pass the attributes string on a global. CurGV
1300// is set to be the global we are processing.
1301//
1302GlobalVarAttributes : /* empty */ {} |
1303 ',' GlobalVarAttribute GlobalVarAttributes {};
1304GlobalVarAttribute : SectionString {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001305 CurGV->setSection(*$1);
1306 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001307 CHECK_FOR_ERROR
Chris Lattnerb2b96672005-11-12 18:21:21 +00001308 }
1309 | ALIGN EUINT64VAL {
1310 if ($2 != 0 && !isPowerOf2_32($2))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001311 GEN_ERROR("Alignment must be a power of two");
Chris Lattnerb2b96672005-11-12 18:21:21 +00001312 CurGV->setAlignment($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001313 CHECK_FOR_ERROR
Chris Lattnerb2b96672005-11-12 18:21:21 +00001314 };
Chris Lattner164c3782005-11-12 00:11:10 +00001315
Chris Lattner30c89792001-09-07 16:35:17 +00001316//===----------------------------------------------------------------------===//
1317// Types includes all predefined types... except void, because it can only be
Reid Spencere1553cc2006-12-31 05:40:12 +00001318// used in specific contexts (function returning void for example).
Chris Lattner30c89792001-09-07 16:35:17 +00001319
1320// Derived types are added later...
1321//
Dale Johannesen320fc8a2007-08-03 01:03:46 +00001322PrimType : INTTYPE | FLOAT | DOUBLE | PPC_FP128 | FP128 | X86_FP80 | LABEL ;
Reid Spencere1553cc2006-12-31 05:40:12 +00001323
1324Types
1325 : OPAQUE {
Reid Spencer08de34b2006-12-03 05:45:44 +00001326 $$ = new PATypeHolder(OpaqueType::get());
Reid Spencer61c83e02006-08-18 08:43:06 +00001327 CHECK_FOR_ERROR
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001328 }
1329 | PrimType {
Reid Spencer08de34b2006-12-03 05:45:44 +00001330 $$ = new PATypeHolder($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001331 CHECK_FOR_ERROR
Reid Spencere1553cc2006-12-31 05:40:12 +00001332 }
1333 | Types '*' { // Pointer type?
1334 if (*$1 == Type::LabelTy)
1335 GEN_ERROR("Cannot form a pointer to a basic block");
1336 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
1337 delete $1;
1338 CHECK_FOR_ERROR
1339 }
1340 | SymbolicValueRef { // Named types are also simple types...
1341 const Type* tmp = getTypeVal($1);
1342 CHECK_FOR_ERROR
1343 $$ = new PATypeHolder(tmp);
1344 }
1345 | '\\' EUINT64VAL { // Type UpReference
Reid Spencerf4fa5902007-02-05 10:16:10 +00001346 if ($2 > (uint64_t)~0U) GEN_ERROR("Value out of range");
Chris Lattner30c89792001-09-07 16:35:17 +00001347 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
Chris Lattner3b073862004-02-09 03:19:29 +00001348 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Reid Spencer08de34b2006-12-03 05:45:44 +00001349 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +00001350 UR_OUT("New Upreference!\n");
Reid Spencer61c83e02006-08-18 08:43:06 +00001351 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001352 }
Reid Spencer863dd882007-04-28 16:06:50 +00001353 | Types '(' ArgTypeListI ')' OptFuncAttrs {
Chris Lattner9232b992003-04-22 18:42:41 +00001354 std::vector<const Type*> Params;
Reid Spencer863dd882007-04-28 16:06:50 +00001355 ParamAttrsVector Attrs;
1356 if ($5 != ParamAttr::None) {
1357 ParamAttrsWithIndex X; X.index = 0; X.attrs = $5;
1358 Attrs.push_back(X);
1359 }
Reid Spencer5694b6e2007-04-09 06:17:21 +00001360 unsigned index = 1;
1361 TypeWithAttrsList::iterator I = $3->begin(), E = $3->end();
1362 for (; I != E; ++I, ++index) {
Reid Spencer2c9df212007-03-20 01:13:00 +00001363 const Type *Ty = I->Ty->get();
Reid Spencer2c9df212007-03-20 01:13:00 +00001364 Params.push_back(Ty);
Reid Spencer863dd882007-04-28 16:06:50 +00001365 if (Ty != Type::VoidTy)
1366 if (I->Attrs != ParamAttr::None) {
1367 ParamAttrsWithIndex X; X.index = index; X.attrs = I->Attrs;
1368 Attrs.push_back(X);
1369 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001370 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001371 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1372 if (isVarArg) Params.pop_back();
1373
Reid Spencer863dd882007-04-28 16:06:50 +00001374 ParamAttrsList *ActualAttrs = 0;
1375 if (!Attrs.empty())
1376 ActualAttrs = ParamAttrsList::get(Attrs);
1377 FunctionType *FT = FunctionType::get(*$1, Params, isVarArg, ActualAttrs);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001378 delete $3; // Delete the argument list
Reid Spencere1553cc2006-12-31 05:40:12 +00001379 delete $1; // Delete the return type handle
1380 $$ = new PATypeHolder(HandleUpRefs(FT));
Reid Spencer61c83e02006-08-18 08:43:06 +00001381 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001382 }
Reid Spencer863dd882007-04-28 16:06:50 +00001383 | VOID '(' ArgTypeListI ')' OptFuncAttrs {
Reid Spencere1553cc2006-12-31 05:40:12 +00001384 std::vector<const Type*> Params;
Reid Spencer863dd882007-04-28 16:06:50 +00001385 ParamAttrsVector Attrs;
1386 if ($5 != ParamAttr::None) {
1387 ParamAttrsWithIndex X; X.index = 0; X.attrs = $5;
1388 Attrs.push_back(X);
1389 }
Reid Spencer5694b6e2007-04-09 06:17:21 +00001390 TypeWithAttrsList::iterator I = $3->begin(), E = $3->end();
1391 unsigned index = 1;
1392 for ( ; I != E; ++I, ++index) {
Reid Spencer2c9df212007-03-20 01:13:00 +00001393 const Type* Ty = I->Ty->get();
Reid Spencer2c9df212007-03-20 01:13:00 +00001394 Params.push_back(Ty);
Reid Spencer863dd882007-04-28 16:06:50 +00001395 if (Ty != Type::VoidTy)
1396 if (I->Attrs != ParamAttr::None) {
1397 ParamAttrsWithIndex X; X.index = index; X.attrs = I->Attrs;
1398 Attrs.push_back(X);
1399 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001400 }
1401 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1402 if (isVarArg) Params.pop_back();
1403
Reid Spencer863dd882007-04-28 16:06:50 +00001404 ParamAttrsList *ActualAttrs = 0;
1405 if (!Attrs.empty())
1406 ActualAttrs = ParamAttrsList::get(Attrs);
1407
1408 FunctionType *FT = FunctionType::get($1, Params, isVarArg, ActualAttrs);
Reid Spencer2c261782007-01-05 17:06:19 +00001409 delete $3; // Delete the argument list
Reid Spencere1553cc2006-12-31 05:40:12 +00001410 $$ = new PATypeHolder(HandleUpRefs(FT));
1411 CHECK_FOR_ERROR
1412 }
1413
1414 | '[' EUINT64VAL 'x' Types ']' { // Sized array type?
Reid Spencer08de34b2006-12-03 05:45:44 +00001415 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
1416 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00001417 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001418 }
Reid Spencerac9dcb92007-02-15 03:39:18 +00001419 | '<' EUINT64VAL 'x' Types '>' { // Vector type?
Reid Spencer08de34b2006-12-03 05:45:44 +00001420 const llvm::Type* ElemTy = $4->get();
1421 if ((unsigned)$2 != $2)
1422 GEN_ERROR("Unsigned result not equal to signed result");
Chris Lattner42a75512007-01-15 02:27:26 +00001423 if (!ElemTy->isFloatingPoint() && !ElemTy->isInteger())
Reid Spencer9d6565a2007-02-15 02:26:10 +00001424 GEN_ERROR("Element type of a VectorType must be primitive");
Reid Spencer9d6565a2007-02-15 02:26:10 +00001425 $$ = new PATypeHolder(HandleUpRefs(VectorType::get(*$4, (unsigned)$2)));
Reid Spencer08de34b2006-12-03 05:45:44 +00001426 delete $4;
1427 CHECK_FOR_ERROR
Brian Gaeke715c90b2004-08-20 06:00:58 +00001428 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001429 | '{' TypeListI '}' { // Structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001430 std::vector<const Type*> Elements;
Reid Spencer08de34b2006-12-03 05:45:44 +00001431 for (std::list<llvm::PATypeHolder>::iterator I = $2->begin(),
Chris Lattnera08976b2005-02-22 23:13:58 +00001432 E = $2->end(); I != E; ++I)
Reid Spencer08de34b2006-12-03 05:45:44 +00001433 Elements.push_back(*I);
Misha Brukman5a2a3822005-05-10 22:02:28 +00001434
Reid Spencer08de34b2006-12-03 05:45:44 +00001435 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001436 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001437 CHECK_FOR_ERROR
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001438 }
1439 | '{' '}' { // Empty structure type?
Reid Spencer08de34b2006-12-03 05:45:44 +00001440 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Reid Spencer61c83e02006-08-18 08:43:06 +00001441 CHECK_FOR_ERROR
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001442 }
Andrew Lenharth38ecbf12006-12-08 18:06:16 +00001443 | '<' '{' TypeListI '}' '>' {
1444 std::vector<const Type*> Elements;
1445 for (std::list<llvm::PATypeHolder>::iterator I = $3->begin(),
1446 E = $3->end(); I != E; ++I)
1447 Elements.push_back(*I);
1448
1449 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements, true)));
1450 delete $3;
1451 CHECK_FOR_ERROR
1452 }
1453 | '<' '{' '}' '>' { // Empty structure type?
1454 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>(), true));
1455 CHECK_FOR_ERROR
1456 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001457 ;
1458
1459ArgType
Reid Spencer863dd882007-04-28 16:06:50 +00001460 : Types OptParamAttrs {
Reid Spencere1553cc2006-12-31 05:40:12 +00001461 $$.Ty = $1;
Reid Spencer863dd882007-04-28 16:06:50 +00001462 $$.Attrs = $2;
Reid Spencere1553cc2006-12-31 05:40:12 +00001463 }
1464 ;
1465
Reid Spencer2c261782007-01-05 17:06:19 +00001466ResultTypes
1467 : Types {
Reid Spencere1553cc2006-12-31 05:40:12 +00001468 if (!UpRefs.empty())
1469 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1470 if (!(*$1)->isFirstClassType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001471 GEN_ERROR("LLVM functions cannot return aggregate types");
Reid Spencer2c261782007-01-05 17:06:19 +00001472 $$ = $1;
Reid Spencere1553cc2006-12-31 05:40:12 +00001473 }
Reid Spencer2c261782007-01-05 17:06:19 +00001474 | VOID {
1475 $$ = new PATypeHolder(Type::VoidTy);
Reid Spencere1553cc2006-12-31 05:40:12 +00001476 }
1477 ;
1478
1479ArgTypeList : ArgType {
1480 $$ = new TypeWithAttrsList();
1481 $$->push_back($1);
1482 CHECK_FOR_ERROR
1483 }
1484 | ArgTypeList ',' ArgType {
1485 ($$=$1)->push_back($3);
1486 CHECK_FOR_ERROR
1487 }
1488 ;
1489
1490ArgTypeListI
1491 : ArgTypeList
1492 | ArgTypeList ',' DOTDOTDOT {
1493 $$=$1;
Reid Spencer18da0722007-04-11 02:44:20 +00001494 TypeWithAttrs TWA; TWA.Attrs = ParamAttr::None;
Reid Spencere1553cc2006-12-31 05:40:12 +00001495 TWA.Ty = new PATypeHolder(Type::VoidTy);
1496 $$->push_back(TWA);
1497 CHECK_FOR_ERROR
1498 }
1499 | DOTDOTDOT {
1500 $$ = new TypeWithAttrsList;
Reid Spencer18da0722007-04-11 02:44:20 +00001501 TypeWithAttrs TWA; TWA.Attrs = ParamAttr::None;
Reid Spencere1553cc2006-12-31 05:40:12 +00001502 TWA.Ty = new PATypeHolder(Type::VoidTy);
1503 $$->push_back(TWA);
1504 CHECK_FOR_ERROR
1505 }
1506 | /*empty*/ {
1507 $$ = new TypeWithAttrsList();
Reid Spencer61c83e02006-08-18 08:43:06 +00001508 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001509 };
Chris Lattner30c89792001-09-07 16:35:17 +00001510
Chris Lattner7e708292002-06-25 16:13:24 +00001511// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +00001512// declaration type lists
1513//
Reid Spencere1553cc2006-12-31 05:40:12 +00001514TypeListI : Types {
Reid Spencer08de34b2006-12-03 05:45:44 +00001515 $$ = new std::list<PATypeHolder>();
Reid Spencer2c9df212007-03-20 01:13:00 +00001516 $$->push_back(*$1);
1517 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001518 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001519 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001520 | TypeListI ',' Types {
Reid Spencer2c9df212007-03-20 01:13:00 +00001521 ($$=$1)->push_back(*$3);
1522 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001523 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001524 };
Chris Lattner30c89792001-09-07 16:35:17 +00001525
Chris Lattnere98dda62001-07-14 06:10:16 +00001526// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +00001527// production is used ONLY to represent constants that show up AFTER a 'const',
1528// 'constant' or 'global' token at global scope. Constants that can be inlined
1529// into other expressions (such as integers and constexprs) are handled by the
1530// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +00001531//
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001532ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencere1553cc2006-12-31 05:40:12 +00001533 if (!UpRefs.empty())
1534 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001535 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001536 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001537 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001538 (*$1)->getDescription() + "'");
Chris Lattner30c89792001-09-07 16:35:17 +00001539 const Type *ETy = ATy->getElementType();
1540 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +00001541
Chris Lattner30c89792001-09-07 16:35:17 +00001542 // Verify that we have the correct size...
1543 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001544 GEN_ERROR("Type mismatch: constant sized array initialized with " +
Reid Spencer3271ed52004-07-18 00:08:11 +00001545 utostr($3->size()) + " arguments, but has size of " +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001546 itostr(NumElements) + "");
Chris Lattner00950542001-06-06 20:29:01 +00001547
Chris Lattner30c89792001-09-07 16:35:17 +00001548 // Verify all elements are correct type!
1549 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencer08de34b2006-12-03 05:45:44 +00001550 if (ETy != (*$3)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001551 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Reid Spencer3271ed52004-07-18 00:08:11 +00001552 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencer08de34b2006-12-03 05:45:44 +00001553 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +00001554 }
1555
Reid Spencer08de34b2006-12-03 05:45:44 +00001556 $$ = ConstantArray::get(ATy, *$3);
1557 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001558 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001559 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001560 | Types '[' ']' {
Reid Spencere1553cc2006-12-31 05:40:12 +00001561 if (!UpRefs.empty())
1562 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001563 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001564 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001565 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001566 (*$1)->getDescription() + "'");
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001567
1568 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +00001569 if (NumElements != -1 && NumElements != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001570 GEN_ERROR("Type mismatch: constant sized array initialized with 0"
Reid Spencerf4fa5902007-02-05 10:16:10 +00001571 " arguments, but has size of " + itostr(NumElements) +"");
Reid Spencer08de34b2006-12-03 05:45:44 +00001572 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
1573 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001574 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001575 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001576 | Types 'c' STRINGCONSTANT {
Reid Spencere1553cc2006-12-31 05:40:12 +00001577 if (!UpRefs.empty())
1578 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001579 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001580 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001581 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001582 (*$1)->getDescription() + "'");
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001583
Chris Lattner30c89792001-09-07 16:35:17 +00001584 int NumElements = ATy->getNumElements();
1585 const Type *ETy = ATy->getElementType();
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001586 if (NumElements != -1 && NumElements != int($3->length()))
Reid Spencer61c83e02006-08-18 08:43:06 +00001587 GEN_ERROR("Can't build string constant of size " +
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001588 itostr((int)($3->length())) +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001589 " when array has size " + itostr(NumElements) + "");
Chris Lattner9232b992003-04-22 18:42:41 +00001590 std::vector<Constant*> Vals;
Reid Spencere1553cc2006-12-31 05:40:12 +00001591 if (ETy == Type::Int8Ty) {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001592 for (unsigned i = 0; i < $3->length(); ++i)
1593 Vals.push_back(ConstantInt::get(ETy, (*$3)[i]));
Chris Lattner93750fa2001-07-28 17:48:55 +00001594 } else {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001595 delete $3;
Reid Spencerf4fa5902007-02-05 10:16:10 +00001596 GEN_ERROR("Cannot build string arrays of non byte sized elements");
Chris Lattner93750fa2001-07-28 17:48:55 +00001597 }
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001598 delete $3;
Reid Spencer08de34b2006-12-03 05:45:44 +00001599 $$ = ConstantArray::get(ATy, Vals);
1600 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001601 CHECK_FOR_ERROR
Chris Lattner93750fa2001-07-28 17:48:55 +00001602 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00001603 | Types '<' ConstVector '>' { // Nonempty unsized arr
Reid Spencere1553cc2006-12-31 05:40:12 +00001604 if (!UpRefs.empty())
1605 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer9d6565a2007-02-15 02:26:10 +00001606 const VectorType *PTy = dyn_cast<VectorType>($1->get());
Brian Gaeke715c90b2004-08-20 06:00:58 +00001607 if (PTy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001608 GEN_ERROR("Cannot make packed constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001609 (*$1)->getDescription() + "'");
Brian Gaeke715c90b2004-08-20 06:00:58 +00001610 const Type *ETy = PTy->getElementType();
1611 int NumElements = PTy->getNumElements();
1612
1613 // Verify that we have the correct size...
1614 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001615 GEN_ERROR("Type mismatch: constant sized packed initialized with " +
Brian Gaeke715c90b2004-08-20 06:00:58 +00001616 utostr($3->size()) + " arguments, but has size of " +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001617 itostr(NumElements) + "");
Brian Gaeke715c90b2004-08-20 06:00:58 +00001618
1619 // Verify all elements are correct type!
1620 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencer08de34b2006-12-03 05:45:44 +00001621 if (ETy != (*$3)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001622 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Brian Gaeke715c90b2004-08-20 06:00:58 +00001623 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencer08de34b2006-12-03 05:45:44 +00001624 (*$3)[i]->getType()->getDescription() + "'.");
Brian Gaeke715c90b2004-08-20 06:00:58 +00001625 }
1626
Reid Spencer9d6565a2007-02-15 02:26:10 +00001627 $$ = ConstantVector::get(PTy, *$3);
Reid Spencer08de34b2006-12-03 05:45:44 +00001628 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001629 CHECK_FOR_ERROR
Brian Gaeke715c90b2004-08-20 06:00:58 +00001630 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001631 | Types '{' ConstVector '}' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001632 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001633 if (STy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001634 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001635 (*$1)->getDescription() + "'");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001636
Chris Lattnerc6212f12003-05-21 16:06:56 +00001637 if ($3->size() != STy->getNumContainedTypes())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001638 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattnerc6212f12003-05-21 16:06:56 +00001639
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001640 // Check to ensure that constants are compatible with the type initializer!
1641 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Reid Spencer08de34b2006-12-03 05:45:44 +00001642 if ((*$3)[i]->getType() != STy->getElementType(i))
Reid Spencer61c83e02006-08-18 08:43:06 +00001643 GEN_ERROR("Expected type '" +
Chris Lattnerd21cd802004-02-09 04:37:31 +00001644 STy->getElementType(i)->getDescription() +
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001645 "' for element #" + utostr(i) +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001646 " of structure initializer");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001647
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001648 // Check to ensure that Type is not packed
1649 if (STy->isPacked())
Chris Lattner4989b842007-04-26 05:30:35 +00001650 GEN_ERROR("Unpacked Initializer to vector type '" +
1651 STy->getDescription() + "'");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001652
Reid Spencer08de34b2006-12-03 05:45:44 +00001653 $$ = ConstantStruct::get(STy, *$3);
1654 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001655 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001656 }
Chris Lattnerc6212f12003-05-21 16:06:56 +00001657 | Types '{' '}' {
Reid Spencere1553cc2006-12-31 05:40:12 +00001658 if (!UpRefs.empty())
1659 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001660 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerc6212f12003-05-21 16:06:56 +00001661 if (STy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001662 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001663 (*$1)->getDescription() + "'");
Chris Lattnerc6212f12003-05-21 16:06:56 +00001664
1665 if (STy->getNumContainedTypes() != 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001666 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattnerc6212f12003-05-21 16:06:56 +00001667
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001668 // Check to ensure that Type is not packed
1669 if (STy->isPacked())
Chris Lattner4989b842007-04-26 05:30:35 +00001670 GEN_ERROR("Unpacked Initializer to vector type '" +
1671 STy->getDescription() + "'");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001672
1673 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1674 delete $1;
1675 CHECK_FOR_ERROR
1676 }
1677 | Types '<' '{' ConstVector '}' '>' {
1678 const StructType *STy = dyn_cast<StructType>($1->get());
1679 if (STy == 0)
1680 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001681 (*$1)->getDescription() + "'");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001682
1683 if ($4->size() != STy->getNumContainedTypes())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001684 GEN_ERROR("Illegal number of initializers for structure type");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001685
1686 // Check to ensure that constants are compatible with the type initializer!
1687 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1688 if ((*$4)[i]->getType() != STy->getElementType(i))
1689 GEN_ERROR("Expected type '" +
1690 STy->getElementType(i)->getDescription() +
1691 "' for element #" + utostr(i) +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001692 " of structure initializer");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001693
1694 // Check to ensure that Type is packed
1695 if (!STy->isPacked())
Reid Spencerac9dcb92007-02-15 03:39:18 +00001696 GEN_ERROR("Vector initializer to non-vector type '" +
1697 STy->getDescription() + "'");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001698
1699 $$ = ConstantStruct::get(STy, *$4);
1700 delete $1; delete $4;
1701 CHECK_FOR_ERROR
1702 }
1703 | Types '<' '{' '}' '>' {
1704 if (!UpRefs.empty())
1705 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1706 const StructType *STy = dyn_cast<StructType>($1->get());
1707 if (STy == 0)
1708 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001709 (*$1)->getDescription() + "'");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001710
1711 if (STy->getNumContainedTypes() != 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001712 GEN_ERROR("Illegal number of initializers for structure type");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001713
1714 // Check to ensure that Type is packed
1715 if (!STy->isPacked())
Reid Spencerac9dcb92007-02-15 03:39:18 +00001716 GEN_ERROR("Vector initializer to non-vector type '" +
1717 STy->getDescription() + "'");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001718
Reid Spencer08de34b2006-12-03 05:45:44 +00001719 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1720 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001721 CHECK_FOR_ERROR
Chris Lattnerc6212f12003-05-21 16:06:56 +00001722 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001723 | Types NULL_TOK {
Reid Spencere1553cc2006-12-31 05:40:12 +00001724 if (!UpRefs.empty())
1725 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001726 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001727 if (PTy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001728 GEN_ERROR("Cannot make null pointer constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001729 (*$1)->getDescription() + "'");
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001730
Reid Spencer08de34b2006-12-03 05:45:44 +00001731 $$ = ConstantPointerNull::get(PTy);
1732 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001733 CHECK_FOR_ERROR
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001734 }
Chris Lattner16710e92004-10-16 18:17:13 +00001735 | Types UNDEF {
Reid Spencere1553cc2006-12-31 05:40:12 +00001736 if (!UpRefs.empty())
1737 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001738 $$ = UndefValue::get($1->get());
1739 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001740 CHECK_FOR_ERROR
Chris Lattner16710e92004-10-16 18:17:13 +00001741 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001742 | Types SymbolicValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00001743 if (!UpRefs.empty())
1744 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001745 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001746 if (Ty == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001747 GEN_ERROR("Global const reference must be a pointer type");
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001748
Chris Lattner3101c252002-08-15 17:58:33 +00001749 // ConstExprs can exist in the body of a function, thus creating
Reid Spencer3271ed52004-07-18 00:08:11 +00001750 // GlobalValues whenever they refer to a variable. Because we are in
Reid Spencer186a43f2007-03-19 18:39:36 +00001751 // the context of a function, getExistingVal will search the functions
Chris Lattner3101c252002-08-15 17:58:33 +00001752 // symbol table instead of the module symbol table for the global symbol,
1753 // which throws things all off. To get around this, we just tell
Reid Spencer186a43f2007-03-19 18:39:36 +00001754 // getExistingVal that we are at global scope here.
Chris Lattner3101c252002-08-15 17:58:33 +00001755 //
Chris Lattner394f2eb2003-10-16 20:12:13 +00001756 Function *SavedCurFn = CurFun.CurrentFunction;
1757 CurFun.CurrentFunction = 0;
Chris Lattner3101c252002-08-15 17:58:33 +00001758
Reid Spencer186a43f2007-03-19 18:39:36 +00001759 Value *V = getExistingVal(Ty, $2);
Reid Spencer5b7e7532006-09-28 19:28:24 +00001760 CHECK_FOR_ERROR
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001761
Chris Lattner394f2eb2003-10-16 20:12:13 +00001762 CurFun.CurrentFunction = SavedCurFn;
Chris Lattner3101c252002-08-15 17:58:33 +00001763
Chris Lattner2079fde2001-10-13 06:41:08 +00001764 // If this is an initializer for a constant pointer, which is referencing a
1765 // (currently) undefined variable, create a stub now that shall be replaced
1766 // in the future with the right type of variable.
1767 //
1768 if (V == 0) {
Reid Spencer1755a9a2007-02-05 17:01:20 +00001769 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001770 const PointerType *PT = cast<PointerType>(Ty);
1771
1772 // First check to see if the forward references value is already created!
1773 PerModuleInfo::GlobalRefsType::iterator I =
Reid Spencer3271ed52004-07-18 00:08:11 +00001774 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Chris Lattner2079fde2001-10-13 06:41:08 +00001775
1776 if (I != CurModule.GlobalRefs.end()) {
Reid Spencer3271ed52004-07-18 00:08:11 +00001777 V = I->second; // Placeholder already exists, use it...
Chris Lattnera989b232003-12-23 20:05:15 +00001778 $2.destroy();
Chris Lattner2079fde2001-10-13 06:41:08 +00001779 } else {
Chris Lattner8a327842004-07-14 21:44:00 +00001780 std::string Name;
Reid Spencerb2d17862007-01-26 08:04:51 +00001781 if ($2.Type == ValID::GlobalName)
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001782 Name = $2.getName();
Reid Spencerb2d17862007-01-26 08:04:51 +00001783 else if ($2.Type != ValID::GlobalID)
1784 GEN_ERROR("Invalid reference to global");
Chris Lattner8a327842004-07-14 21:44:00 +00001785
Reid Spencer3271ed52004-07-18 00:08:11 +00001786 // Create the forward referenced global.
Chris Lattnera09000d2004-07-14 23:03:46 +00001787 GlobalValue *GV;
1788 if (const FunctionType *FTy =
1789 dyn_cast<FunctionType>(PT->getElementType())) {
Chris Lattner4989b842007-04-26 05:30:35 +00001790 GV = new Function(FTy, GlobalValue::ExternalWeakLinkage, Name,
Chris Lattnera09000d2004-07-14 23:03:46 +00001791 CurModule.CurrentModule);
1792 } else {
Reid Spencer3271ed52004-07-18 00:08:11 +00001793 GV = new GlobalVariable(PT->getElementType(), false,
Chris Lattner4989b842007-04-26 05:30:35 +00001794 GlobalValue::ExternalWeakLinkage, 0,
Chris Lattnera09000d2004-07-14 23:03:46 +00001795 Name, CurModule.CurrentModule);
1796 }
Chris Lattner8a327842004-07-14 21:44:00 +00001797
Reid Spencer3271ed52004-07-18 00:08:11 +00001798 // Keep track of the fact that we have a forward ref to recycle it
1799 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1800 V = GV;
Chris Lattner2079fde2001-10-13 06:41:08 +00001801 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001802 }
1803
Reid Spencer08de34b2006-12-03 05:45:44 +00001804 $$ = cast<GlobalValue>(V);
1805 delete $1; // Free the type handle
Reid Spencer61c83e02006-08-18 08:43:06 +00001806 CHECK_FOR_ERROR
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001807 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001808 | Types ConstExpr {
Reid Spencere1553cc2006-12-31 05:40:12 +00001809 if (!UpRefs.empty())
1810 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001811 if ($1->get() != $2->getType())
Reid Spencerb4fdfdb2007-01-04 00:05:48 +00001812 GEN_ERROR("Mismatched types for constant expression: " +
1813 (*$1)->getDescription() + " and " + $2->getType()->getDescription());
Chris Lattnerd78700d2002-08-16 21:14:40 +00001814 $$ = $2;
Reid Spencer08de34b2006-12-03 05:45:44 +00001815 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001816 CHECK_FOR_ERROR
Chris Lattnerf4600482003-06-28 20:01:34 +00001817 }
1818 | Types ZEROINITIALIZER {
Reid Spencere1553cc2006-12-31 05:40:12 +00001819 if (!UpRefs.empty())
1820 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001821 const Type *Ty = $1->get();
Chris Lattner78915932004-11-28 16:45:45 +00001822 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001823 GEN_ERROR("Cannot create a null initialized value of this type");
Reid Spencer08de34b2006-12-03 05:45:44 +00001824 $$ = Constant::getNullValue(Ty);
1825 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001826 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00001827 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001828 | IntType ESINT64VAL { // integral constants
Reid Spencer9ffad0a2006-12-20 17:20:09 +00001829 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001830 GEN_ERROR("Constant value doesn't fit in type");
Reid Spencereac65742007-03-19 20:40:22 +00001831 $$ = ConstantInt::get($1, $2, true);
Reid Spencerf2310042007-02-28 02:23:44 +00001832 CHECK_FOR_ERROR
1833 }
1834 | IntType ESAPINTVAL { // arbitrary precision integer constants
1835 uint32_t BitWidth = cast<IntegerType>($1)->getBitWidth();
1836 if ($2->getBitWidth() > BitWidth) {
1837 GEN_ERROR("Constant value does not fit in type");
Reid Spenceraf1aacd2007-03-01 19:32:01 +00001838 }
1839 $2->sextOrTrunc(BitWidth);
1840 $$ = ConstantInt::get(*$2);
Reid Spencerf2310042007-02-28 02:23:44 +00001841 delete $2;
Reid Spencer9ffad0a2006-12-20 17:20:09 +00001842 CHECK_FOR_ERROR
1843 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001844 | IntType EUINT64VAL { // integral constants
Reid Spencer9ffad0a2006-12-20 17:20:09 +00001845 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001846 GEN_ERROR("Constant value doesn't fit in type");
Reid Spencereac65742007-03-19 20:40:22 +00001847 $$ = ConstantInt::get($1, $2, false);
Reid Spencerf2310042007-02-28 02:23:44 +00001848 CHECK_FOR_ERROR
1849 }
1850 | IntType EUAPINTVAL { // arbitrary precision integer constants
1851 uint32_t BitWidth = cast<IntegerType>($1)->getBitWidth();
1852 if ($2->getBitWidth() > BitWidth) {
1853 GEN_ERROR("Constant value does not fit in type");
Reid Spenceraf1aacd2007-03-01 19:32:01 +00001854 }
1855 $2->zextOrTrunc(BitWidth);
1856 $$ = ConstantInt::get(*$2);
Reid Spencerf2310042007-02-28 02:23:44 +00001857 delete $2;
Reid Spencer9ffad0a2006-12-20 17:20:09 +00001858 CHECK_FOR_ERROR
1859 }
Reid Spencer8088e9d2007-01-13 05:00:20 +00001860 | INTTYPE TRUETOK { // Boolean constants
1861 assert(cast<IntegerType>($1)->getBitWidth() == 1 && "Not Bool?");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001862 $$ = ConstantInt::getTrue();
Reid Spencer61c83e02006-08-18 08:43:06 +00001863 CHECK_FOR_ERROR
Chris Lattnerd05e3592002-08-15 18:17:28 +00001864 }
Reid Spencer8088e9d2007-01-13 05:00:20 +00001865 | INTTYPE FALSETOK { // Boolean constants
1866 assert(cast<IntegerType>($1)->getBitWidth() == 1 && "Not Bool?");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001867 $$ = ConstantInt::getFalse();
Reid Spencer61c83e02006-08-18 08:43:06 +00001868 CHECK_FOR_ERROR
Chris Lattnerd05e3592002-08-15 18:17:28 +00001869 }
Dale Johannesen9d5f4562007-09-12 03:30:33 +00001870 | FPType FPVAL { // Floating point constants
Dale Johannesen43421b32007-09-06 18:13:44 +00001871 if (!ConstantFP::isValueValidForType($1, *$2))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001872 GEN_ERROR("Floating point constant invalid for type");
Dale Johannesen3f6eb742007-09-11 18:32:33 +00001873 // Lexer has no type info, so builds all float and double FP constants
1874 // as double. Fix this here. Long double is done right.
1875 if (&$2->getSemantics()==&APFloat::IEEEdouble && $1==Type::FloatTy)
Dale Johannesen43421b32007-09-06 18:13:44 +00001876 $2->convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven);
1877 $$ = ConstantFP::get($1, *$2);
Dale Johannesencdd509a2007-09-07 21:07:57 +00001878 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001879 CHECK_FOR_ERROR
Chris Lattnerd05e3592002-08-15 18:17:28 +00001880 };
1881
Chris Lattner00950542001-06-06 20:29:01 +00001882
Reid Spencer3da59db2006-11-27 01:05:10 +00001883ConstExpr: CastOps '(' ConstVal TO Types ')' {
Reid Spencere1553cc2006-12-31 05:40:12 +00001884 if (!UpRefs.empty())
1885 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001886 Constant *Val = $3;
Reid Spencer93947c32007-01-17 02:47:33 +00001887 const Type *DestTy = $5->get();
1888 if (!CastInst::castIsValid($1, $3, DestTy))
1889 GEN_ERROR("invalid cast opcode for cast from '" +
1890 Val->getType()->getDescription() + "' to '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001891 DestTy->getDescription() + "'");
Reid Spencer93947c32007-01-17 02:47:33 +00001892 $$ = ConstantExpr::getCast($1, $3, DestTy);
Reid Spencer08de34b2006-12-03 05:45:44 +00001893 delete $5;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001894 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001895 | GETELEMENTPTR '(' ConstVal IndexList ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001896 if (!isa<PointerType>($3->getType()))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001897 GEN_ERROR("GetElementPtr requires a pointer operand");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001898
Reid Spencer08de34b2006-12-03 05:45:44 +00001899 const Type *IdxTy =
David Greeneb8f74792007-09-04 15:46:09 +00001900 GetElementPtrInst::getIndexedType($3->getType(), $4->begin(), $4->end(),
Chris Lattnerc856c7a2007-02-13 00:57:40 +00001901 true);
Reid Spencer08de34b2006-12-03 05:45:44 +00001902 if (!IdxTy)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001903 GEN_ERROR("Index list invalid for constant getelementptr");
Reid Spencer08de34b2006-12-03 05:45:44 +00001904
Chris Lattnere0135402007-01-31 04:43:46 +00001905 SmallVector<Constant*, 8> IdxVec;
Reid Spencer08de34b2006-12-03 05:45:44 +00001906 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1907 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001908 IdxVec.push_back(C);
1909 else
Reid Spencerf4fa5902007-02-05 10:16:10 +00001910 GEN_ERROR("Indices to constant getelementptr must be constants");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001911
Chris Lattnerd78700d2002-08-16 21:14:40 +00001912 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001913
Chris Lattnere0135402007-01-31 04:43:46 +00001914 $$ = ConstantExpr::getGetElementPtr($3, &IdxVec[0], IdxVec.size());
Reid Spencer61c83e02006-08-18 08:43:06 +00001915 CHECK_FOR_ERROR
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001916 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001917 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer4fe16d62007-01-11 18:21:29 +00001918 if ($3->getType() != Type::Int1Ty)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001919 GEN_ERROR("Select condition must be of boolean type");
Reid Spencer08de34b2006-12-03 05:45:44 +00001920 if ($5->getType() != $7->getType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001921 GEN_ERROR("Select operand types must match");
Reid Spencer08de34b2006-12-03 05:45:44 +00001922 $$ = ConstantExpr::getSelect($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001923 CHECK_FOR_ERROR
Chris Lattner76f0ff32004-03-12 05:51:36 +00001924 }
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001925 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001926 if ($3->getType() != $5->getType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001927 GEN_ERROR("Binary operator types must match");
Reid Spencer1628cec2006-10-26 06:15:43 +00001928 CHECK_FOR_ERROR;
Reid Spencerc6e956e2006-12-05 19:15:41 +00001929 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001930 }
1931 | LogicalOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001932 if ($3->getType() != $5->getType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001933 GEN_ERROR("Logical operator types must match");
Chris Lattner42a75512007-01-15 02:27:26 +00001934 if (!$3->getType()->isInteger()) {
Reid Spencer9d6565a2007-02-15 02:26:10 +00001935 if (Instruction::isShift($1) || !isa<VectorType>($3->getType()) ||
1936 !cast<VectorType>($3->getType())->getElementType()->isInteger())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001937 GEN_ERROR("Logical operator requires integral operands");
Chris Lattner0a017832005-12-21 18:31:29 +00001938 }
Reid Spencer08de34b2006-12-03 05:45:44 +00001939 $$ = ConstantExpr::get($1, $3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001940 CHECK_FOR_ERROR
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001941 }
Reid Spencer763ed5e2006-12-04 05:20:06 +00001942 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
1943 if ($4->getType() != $6->getType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001944 GEN_ERROR("icmp operand types must match");
Reid Spencer763ed5e2006-12-04 05:20:06 +00001945 $$ = ConstantExpr::getICmp($2, $4, $6);
Reid Spencer08de34b2006-12-03 05:45:44 +00001946 }
Reid Spencer763ed5e2006-12-04 05:20:06 +00001947 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
1948 if ($4->getType() != $6->getType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001949 GEN_ERROR("fcmp operand types must match");
Reid Spencer763ed5e2006-12-04 05:20:06 +00001950 $$ = ConstantExpr::getFCmp($2, $4, $6);
Reid Spencer08de34b2006-12-03 05:45:44 +00001951 }
Robert Bocchino9c62b562006-01-10 19:04:32 +00001952 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001953 if (!ExtractElementInst::isValidOperands($3, $5))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001954 GEN_ERROR("Invalid extractelement operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00001955 $$ = ConstantExpr::getExtractElement($3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001956 CHECK_FOR_ERROR
Chris Lattnerca73cd82006-04-08 03:53:34 +00001957 }
1958 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001959 if (!InsertElementInst::isValidOperands($3, $5, $7))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001960 GEN_ERROR("Invalid insertelement operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00001961 $$ = ConstantExpr::getInsertElement($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001962 CHECK_FOR_ERROR
Chris Lattnerca73cd82006-04-08 03:53:34 +00001963 }
1964 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001965 if (!ShuffleVectorInst::isValidOperands($3, $5, $7))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001966 GEN_ERROR("Invalid shufflevector operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00001967 $$ = ConstantExpr::getShuffleVector($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001968 CHECK_FOR_ERROR
Chris Lattner699f1eb2002-08-14 17:12:33 +00001969 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001970
Chris Lattnerca73cd82006-04-08 03:53:34 +00001971
Misha Brukmanbc0e9982003-07-14 17:20:40 +00001972// ConstVector - A list of comma separated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001973ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001974 ($$ = $1)->push_back($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00001975 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001976 }
1977 | ConstVal {
Reid Spencer08de34b2006-12-03 05:45:44 +00001978 $$ = new std::vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001979 $$->push_back($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001980 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001981 };
Chris Lattner00950542001-06-06 20:29:01 +00001982
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001983
Chris Lattner1781aca2001-09-18 04:00:54 +00001984// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001985GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001986
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00001987// ThreadLocal
1988ThreadLocal : THREAD_LOCAL { $$ = true; } | { $$ = false; };
1989
Anton Korobeynikova80e1182007-04-28 13:45:00 +00001990// AliaseeRef - Match either GlobalValue or bitcast to GlobalValue.
1991AliaseeRef : ResultTypes SymbolicValueRef {
1992 const Type* VTy = $1->get();
1993 Value *V = getVal(VTy, $2);
Chris Lattner6df20ef2007-08-06 21:00:37 +00001994 CHECK_FOR_ERROR
Anton Korobeynikova80e1182007-04-28 13:45:00 +00001995 GlobalValue* Aliasee = dyn_cast<GlobalValue>(V);
1996 if (!Aliasee)
1997 GEN_ERROR("Aliases can be created only to global values");
1998
1999 $$ = Aliasee;
2000 CHECK_FOR_ERROR
2001 delete $1;
2002 }
2003 | BITCAST '(' AliaseeRef TO Types ')' {
2004 Constant *Val = $3;
2005 const Type *DestTy = $5->get();
2006 if (!CastInst::castIsValid($1, $3, DestTy))
2007 GEN_ERROR("invalid cast opcode for cast from '" +
2008 Val->getType()->getDescription() + "' to '" +
2009 DestTy->getDescription() + "'");
2010
2011 $$ = ConstantExpr::getCast($1, $3, DestTy);
2012 CHECK_FOR_ERROR
2013 delete $5;
2014 };
Chris Lattner00950542001-06-06 20:29:01 +00002015
Chris Lattner0e73ce62002-05-02 19:11:13 +00002016//===----------------------------------------------------------------------===//
2017// Rules to match Modules
2018//===----------------------------------------------------------------------===//
2019
2020// Module rule: Capture the result of parsing the whole file into a result
2021// variable...
2022//
Reid Spencerb951bc02006-12-29 20:29:48 +00002023Module
2024 : DefinitionList {
2025 $$ = ParserResult = CurModule.CurrentModule;
2026 CurModule.ModuleDone();
2027 CHECK_FOR_ERROR;
2028 }
2029 | /*empty*/ {
2030 $$ = ParserResult = CurModule.CurrentModule;
2031 CurModule.ModuleDone();
2032 CHECK_FOR_ERROR;
2033 }
2034 ;
Chris Lattner0e73ce62002-05-02 19:11:13 +00002035
Reid Spencerb951bc02006-12-29 20:29:48 +00002036DefinitionList
2037 : Definition
2038 | DefinitionList Definition
2039 ;
2040
2041Definition
Jeff Cohen361c3ef2007-01-21 19:19:31 +00002042 : DEFINE { CurFun.isDeclare = false; } Function {
Chris Lattner394f2eb2003-10-16 20:12:13 +00002043 CurFun.FunctionDone();
Reid Spencer61c83e02006-08-18 08:43:06 +00002044 CHECK_FOR_ERROR
Reid Spencerb951bc02006-12-29 20:29:48 +00002045 }
2046 | DECLARE { CurFun.isDeclare = true; } FunctionProto {
Reid Spencer61c83e02006-08-18 08:43:06 +00002047 CHECK_FOR_ERROR
Chris Lattner0e73ce62002-05-02 19:11:13 +00002048 }
Reid Spencerb951bc02006-12-29 20:29:48 +00002049 | MODULE ASM_TOK AsmBlock {
Reid Spencer61c83e02006-08-18 08:43:06 +00002050 CHECK_FOR_ERROR
Chris Lattneree454772006-01-23 23:05:15 +00002051 }
Reid Spencerb2d17862007-01-26 08:04:51 +00002052 | OptLocalAssign TYPE Types {
Reid Spencere1553cc2006-12-31 05:40:12 +00002053 if (!UpRefs.empty())
2054 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattner4a42e902001-10-22 05:56:09 +00002055 // Eagerly resolve types. This is not an optimization, this is a
2056 // requirement that is due to the fact that we could have this:
2057 //
2058 // %list = type { %list * }
2059 // %list = type { %list * } ; repeated type decl
2060 //
2061 // If types are not resolved eagerly, then the two types will not be
2062 // determined to be the same type!
2063 //
Reid Spencerb951bc02006-12-29 20:29:48 +00002064 ResolveTypeTo($1, *$3);
Chris Lattner4a42e902001-10-22 05:56:09 +00002065
Reid Spencerb951bc02006-12-29 20:29:48 +00002066 if (!setTypeName(*$3, $1) && !$1) {
Reid Spencer5b7e7532006-09-28 19:28:24 +00002067 CHECK_FOR_ERROR
Chris Lattner8c89a0a2004-07-13 08:10:10 +00002068 // If this is a named type that is not a redefinition, add it to the slot
2069 // table.
Reid Spencerb951bc02006-12-29 20:29:48 +00002070 CurModule.Types.push_back(*$3);
Chris Lattner30c89792001-09-07 16:35:17 +00002071 }
Reid Spencer08de34b2006-12-03 05:45:44 +00002072
Reid Spencerb951bc02006-12-29 20:29:48 +00002073 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002074 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00002075 }
Reid Spencerb2d17862007-01-26 08:04:51 +00002076 | OptLocalAssign TYPE VOID {
Reid Spencere1553cc2006-12-31 05:40:12 +00002077 ResolveTypeTo($1, $3);
2078
2079 if (!setTypeName($3, $1) && !$1) {
2080 CHECK_FOR_ERROR
2081 // If this is a named type that is not a redefinition, add it to the slot
2082 // table.
2083 CurModule.Types.push_back($3);
2084 }
2085 CHECK_FOR_ERROR
2086 }
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002087 | OptGlobalAssign GVVisibilityStyle ThreadLocal GlobalType ConstVal {
Reid Spencerb2d17862007-01-26 08:04:51 +00002088 /* "Externally Visible" Linkage */
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002089 if ($5 == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002090 GEN_ERROR("Global value initializer is not a constant");
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002091 CurGV = ParseGlobalVariable($1, GlobalValue::ExternalLinkage,
2092 $2, $4, $5->getType(), $5, $3);
Reid Spencerb951bc02006-12-29 20:29:48 +00002093 CHECK_FOR_ERROR
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002094 } GlobalVarAttributes {
2095 CurGV = 0;
2096 }
Chris Lattner4989b842007-04-26 05:30:35 +00002097 | OptGlobalAssign GVInternalLinkage GVVisibilityStyle ThreadLocal GlobalType
2098 ConstVal {
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002099 if ($6 == 0)
2100 GEN_ERROR("Global value initializer is not a constant");
2101 CurGV = ParseGlobalVariable($1, $2, $3, $5, $6->getType(), $6, $4);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002102 CHECK_FOR_ERROR
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002103 } GlobalVarAttributes {
2104 CurGV = 0;
2105 }
Chris Lattner4989b842007-04-26 05:30:35 +00002106 | OptGlobalAssign GVExternalLinkage GVVisibilityStyle ThreadLocal GlobalType
2107 Types {
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002108 if (!UpRefs.empty())
2109 GEN_ERROR("Invalid upreference in type: " + (*$6)->getDescription());
2110 CurGV = ParseGlobalVariable($1, $2, $3, $5, *$6, 0, $4);
2111 CHECK_FOR_ERROR
2112 delete $6;
Reid Spencer5b7e7532006-09-28 19:28:24 +00002113 } GlobalVarAttributes {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002114 CurGV = 0;
2115 CHECK_FOR_ERROR
2116 }
Anton Korobeynikova80e1182007-04-28 13:45:00 +00002117 | OptGlobalAssign GVVisibilityStyle ALIAS AliasLinkage AliaseeRef {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002118 std::string Name;
2119 if ($1) {
2120 Name = *$1;
2121 delete $1;
2122 }
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00002123 if (Name.empty())
Anton Korobeynikova80e1182007-04-28 13:45:00 +00002124 GEN_ERROR("Alias name cannot be empty");
2125
2126 Constant* Aliasee = $5;
2127 if (Aliasee == 0)
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002128 GEN_ERROR(std::string("Invalid aliasee for alias: ") + Name);
Anton Korobeynikova80e1182007-04-28 13:45:00 +00002129
2130 GlobalAlias* GA = new GlobalAlias(Aliasee->getType(), $4, Name, Aliasee,
2131 CurModule.CurrentModule);
2132 GA->setVisibility($2);
2133 InsertValue(GA, CurModule.Values);
Chris Lattnere5434242007-09-10 23:23:53 +00002134
2135
2136 // If there was a forward reference of this alias, resolve it now.
2137
2138 ValID ID;
2139 if (!Name.empty())
2140 ID = ValID::createGlobalName(Name);
2141 else
2142 ID = ValID::createGlobalID(CurModule.Values.size()-1);
2143
2144 if (GlobalValue *FWGV =
2145 CurModule.GetForwardRefForGlobal(GA->getType(), ID)) {
2146 // Replace uses of the fwdref with the actual alias.
2147 FWGV->replaceAllUsesWith(GA);
2148 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(FWGV))
2149 GV->eraseFromParent();
2150 else
2151 cast<Function>(FWGV)->eraseFromParent();
2152 }
2153 ID.destroy();
2154
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00002155 CHECK_FOR_ERROR
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00002156 }
Reid Spencerb951bc02006-12-29 20:29:48 +00002157 | TARGET TargetDefinition {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002158 CHECK_FOR_ERROR
2159 }
Reid Spencerb951bc02006-12-29 20:29:48 +00002160 | DEPLIBS '=' LibrariesDefinition {
Reid Spencer61c83e02006-08-18 08:43:06 +00002161 CHECK_FOR_ERROR
Chris Lattnere98dda62001-07-14 06:10:16 +00002162 }
Reid Spencerb951bc02006-12-29 20:29:48 +00002163 ;
Chris Lattner00950542001-06-06 20:29:01 +00002164
2165
Chris Lattneree454772006-01-23 23:05:15 +00002166AsmBlock : STRINGCONSTANT {
Chris Lattner66316012006-01-24 04:14:29 +00002167 const std::string &AsmSoFar = CurModule.CurrentModule->getModuleInlineAsm();
Chris Lattneree454772006-01-23 23:05:15 +00002168 if (AsmSoFar.empty())
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002169 CurModule.CurrentModule->setModuleInlineAsm(*$1);
Chris Lattneree454772006-01-23 23:05:15 +00002170 else
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002171 CurModule.CurrentModule->setModuleInlineAsm(AsmSoFar+"\n"+*$1);
2172 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002173 CHECK_FOR_ERROR
Chris Lattneree454772006-01-23 23:05:15 +00002174};
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00002175
Reid Spencerb2d17862007-01-26 08:04:51 +00002176TargetDefinition : TRIPLE '=' STRINGCONSTANT {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002177 CurModule.CurrentModule->setTargetTriple(*$3);
2178 delete $3;
John Criswell2f6a8b12006-10-24 19:09:48 +00002179 }
Chris Lattner10b27112006-10-22 06:07:41 +00002180 | DATALAYOUT '=' STRINGCONSTANT {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002181 CurModule.CurrentModule->setDataLayout(*$3);
2182 delete $3;
Owen Andersoncf7ff2b2006-10-18 02:21:12 +00002183 };
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00002184
Reid Spencerfeaf10e2004-07-25 21:30:51 +00002185LibrariesDefinition : '[' LibList ']';
Reid Spencer0be13e72004-07-25 17:58:28 +00002186
2187LibList : LibList ',' STRINGCONSTANT {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002188 CurModule.CurrentModule->addLibrary(*$3);
2189 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002190 CHECK_FOR_ERROR
Reid Spencer0be13e72004-07-25 17:58:28 +00002191 }
2192 | STRINGCONSTANT {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002193 CurModule.CurrentModule->addLibrary(*$1);
2194 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002195 CHECK_FOR_ERROR
Reid Spencer0be13e72004-07-25 17:58:28 +00002196 }
2197 | /* empty: end of list */ {
Reid Spencer61c83e02006-08-18 08:43:06 +00002198 CHECK_FOR_ERROR
Reid Spencer0be13e72004-07-25 17:58:28 +00002199 }
2200 ;
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00002201
Chris Lattner00950542001-06-06 20:29:01 +00002202//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00002203// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00002204//===----------------------------------------------------------------------===//
2205
Reid Spencerb2d17862007-01-26 08:04:51 +00002206ArgListH : ArgListH ',' Types OptParamAttrs OptLocalName {
Reid Spencere1553cc2006-12-31 05:40:12 +00002207 if (!UpRefs.empty())
2208 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
2209 if (*$3 == Type::VoidTy)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002210 GEN_ERROR("void typed arguments are invalid");
Reid Spencere1553cc2006-12-31 05:40:12 +00002211 ArgListEntry E; E.Attrs = $4; E.Ty = $3; E.Name = $5;
Chris Lattner69da5cf2002-10-13 20:57:00 +00002212 $$ = $1;
Reid Spencere1553cc2006-12-31 05:40:12 +00002213 $1->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002214 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002215 }
Reid Spencerb2d17862007-01-26 08:04:51 +00002216 | Types OptParamAttrs OptLocalName {
Reid Spencere1553cc2006-12-31 05:40:12 +00002217 if (!UpRefs.empty())
2218 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2219 if (*$1 == Type::VoidTy)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002220 GEN_ERROR("void typed arguments are invalid");
Reid Spencere1553cc2006-12-31 05:40:12 +00002221 ArgListEntry E; E.Attrs = $2; E.Ty = $1; E.Name = $3;
2222 $$ = new ArgListType;
2223 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002224 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002225 };
Chris Lattner00950542001-06-06 20:29:01 +00002226
2227ArgList : ArgListH {
2228 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002229 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002230 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00002231 | ArgListH ',' DOTDOTDOT {
2232 $$ = $1;
Reid Spencere1553cc2006-12-31 05:40:12 +00002233 struct ArgListEntry E;
2234 E.Ty = new PATypeHolder(Type::VoidTy);
2235 E.Name = 0;
Reid Spencer18da0722007-04-11 02:44:20 +00002236 E.Attrs = ParamAttr::None;
Reid Spencere1553cc2006-12-31 05:40:12 +00002237 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002238 CHECK_FOR_ERROR
Chris Lattner69da5cf2002-10-13 20:57:00 +00002239 }
2240 | DOTDOTDOT {
Reid Spencere1553cc2006-12-31 05:40:12 +00002241 $$ = new ArgListType;
2242 struct ArgListEntry E;
2243 E.Ty = new PATypeHolder(Type::VoidTy);
2244 E.Name = 0;
Reid Spencer18da0722007-04-11 02:44:20 +00002245 E.Attrs = ParamAttr::None;
Reid Spencere1553cc2006-12-31 05:40:12 +00002246 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002247 CHECK_FOR_ERROR
Chris Lattner69da5cf2002-10-13 20:57:00 +00002248 }
Chris Lattner00950542001-06-06 20:29:01 +00002249 | /* empty */ {
2250 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00002251 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002252 };
Chris Lattner00950542001-06-06 20:29:01 +00002253
Reid Spencerb2d17862007-01-26 08:04:51 +00002254FunctionHeaderH : OptCallingConv ResultTypes GlobalName '(' ArgList ')'
Reid Spencer2c261782007-01-05 17:06:19 +00002255 OptFuncAttrs OptSection OptAlign {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002256 std::string FunctionName(*$3);
2257 delete $3; // Free strdup'd memory!
Chris Lattnerdda71962001-11-26 18:54:16 +00002258
Reid Spencer98b3c5c2007-01-02 21:53:43 +00002259 // Check the function result for abstractness if this is a define. We should
2260 // have no abstract types at this point
Reid Spencer2c261782007-01-05 17:06:19 +00002261 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved($2))
2262 GEN_ERROR("Reference to abstract result: "+ $2->get()->getDescription());
Reid Spencer98b3c5c2007-01-02 21:53:43 +00002263
Chris Lattner9232b992003-04-22 18:42:41 +00002264 std::vector<const Type*> ParamTypeList;
Reid Spencer4f859aa2007-04-22 05:46:44 +00002265 ParamAttrsVector Attrs;
2266 if ($7 != ParamAttr::None) {
2267 ParamAttrsWithIndex PAWI; PAWI.index = 0; PAWI.attrs = $7;
2268 Attrs.push_back(PAWI);
2269 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00002270 if ($5) { // If there are arguments...
Reid Spencer5694b6e2007-04-09 06:17:21 +00002271 unsigned index = 1;
2272 for (ArgListType::iterator I = $5->begin(); I != $5->end(); ++I, ++index) {
Reid Spencere1553cc2006-12-31 05:40:12 +00002273 const Type* Ty = I->Ty->get();
Reid Spencer98b3c5c2007-01-02 21:53:43 +00002274 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved(I->Ty))
2275 GEN_ERROR("Reference to abstract argument: " + Ty->getDescription());
Reid Spencere1553cc2006-12-31 05:40:12 +00002276 ParamTypeList.push_back(Ty);
2277 if (Ty != Type::VoidTy)
Reid Spencer4f859aa2007-04-22 05:46:44 +00002278 if (I->Attrs != ParamAttr::None) {
2279 ParamAttrsWithIndex PAWI; PAWI.index = index; PAWI.attrs = I->Attrs;
2280 Attrs.push_back(PAWI);
2281 }
Reid Spencere1553cc2006-12-31 05:40:12 +00002282 }
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002283 }
Chris Lattner00950542001-06-06 20:29:01 +00002284
Chris Lattner2079fde2001-10-13 06:41:08 +00002285 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
2286 if (isVarArg) ParamTypeList.pop_back();
2287
Reid Spencer4f859aa2007-04-22 05:46:44 +00002288 ParamAttrsList *PAL = 0;
2289 if (!Attrs.empty())
2290 PAL = ParamAttrsList::get(Attrs);
Reid Spencer5694b6e2007-04-09 06:17:21 +00002291
Reid Spencer863dd882007-04-28 16:06:50 +00002292 FunctionType *FT = FunctionType::get(*$2, ParamTypeList, isVarArg, PAL);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002293 const PointerType *PFT = PointerType::get(FT);
Reid Spencer2c261782007-01-05 17:06:19 +00002294 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002295
Chris Lattnera09000d2004-07-14 23:03:46 +00002296 ValID ID;
2297 if (!FunctionName.empty()) {
Reid Spencerb2d17862007-01-26 08:04:51 +00002298 ID = ValID::createGlobalName((char*)FunctionName.c_str());
Chris Lattnera09000d2004-07-14 23:03:46 +00002299 } else {
Reid Spencer186a43f2007-03-19 18:39:36 +00002300 ID = ValID::createGlobalID(CurModule.Values.size());
Chris Lattnera09000d2004-07-14 23:03:46 +00002301 }
2302
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00002303 Function *Fn = 0;
Chris Lattnera09000d2004-07-14 23:03:46 +00002304 // See if this function was forward referenced. If so, recycle the object.
Reid Spencer863dd882007-04-28 16:06:50 +00002305 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
Chris Lattnera09000d2004-07-14 23:03:46 +00002306 // Move the function to the end of the list, from whereever it was
2307 // previously inserted.
2308 Fn = cast<Function>(FWRef);
2309 CurModule.CurrentModule->getFunctionList().remove(Fn);
2310 CurModule.CurrentModule->getFunctionList().push_back(Fn);
2311 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
Reid Spenceref9b9a72007-02-05 20:47:22 +00002312 (Fn = CurModule.CurrentModule->getFunction(FunctionName))) {
Reid Spencer863dd882007-04-28 16:06:50 +00002313 if (Fn->getFunctionType() != FT) {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002314 // The existing function doesn't have the same type. This is an overload
2315 // error.
2316 GEN_ERROR("Overload of function '" + FunctionName + "' not permitted.");
2317 } else if (!CurFun.isDeclare && !Fn->isDeclaration()) {
2318 // Neither the existing or the current function is a declaration and they
2319 // have the same name and same type. Clearly this is a redefinition.
Reid Spencerf4fa5902007-02-05 10:16:10 +00002320 GEN_ERROR("Redefinition of function '" + FunctionName + "'");
Reid Spenceref9b9a72007-02-05 20:47:22 +00002321 } if (Fn->isDeclaration()) {
2322 // Make sure to strip off any argument names so we can't get conflicts.
Chris Lattnere4d5c442005-03-15 04:54:21 +00002323 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
Chris Lattnera09000d2004-07-14 23:03:46 +00002324 AI != AE; ++AI)
2325 AI->setName("");
Reid Spenceref9b9a72007-02-05 20:47:22 +00002326 }
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00002327 } else { // Not already defined?
Chris Lattner4989b842007-04-26 05:30:35 +00002328 Fn = new Function(FT, GlobalValue::ExternalWeakLinkage, FunctionName,
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00002329 CurModule.CurrentModule);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002330
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00002331 InsertValue(Fn, CurModule.Values);
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00002332 }
Chris Lattner00950542001-06-06 20:29:01 +00002333
Chris Lattner394f2eb2003-10-16 20:12:13 +00002334 CurFun.FunctionStart(Fn);
Anton Korobeynikov93c2b372006-09-17 13:06:18 +00002335
2336 if (CurFun.isDeclare) {
2337 // If we have declaration, always overwrite linkage. This will allow us to
2338 // correctly handle cases, when pointer to function is passed as argument to
2339 // another function.
2340 Fn->setLinkage(CurFun.Linkage);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002341 Fn->setVisibility(CurFun.Visibility);
Anton Korobeynikov93c2b372006-09-17 13:06:18 +00002342 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00002343 Fn->setCallingConv($1);
Reid Spencer2c261782007-01-05 17:06:19 +00002344 Fn->setAlignment($9);
2345 if ($8) {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002346 Fn->setSection(*$8);
2347 delete $8;
Chris Lattner164c3782005-11-12 00:11:10 +00002348 }
Chris Lattner00950542001-06-06 20:29:01 +00002349
Chris Lattner7e708292002-06-25 16:13:24 +00002350 // Add all of the arguments we parsed to the function...
Chris Lattnera8e8f162005-05-06 20:27:19 +00002351 if ($5) { // Is null if empty...
Chris Lattner69da5cf2002-10-13 20:57:00 +00002352 if (isVarArg) { // Nuke the last entry
Reid Spenceref9b9a72007-02-05 20:47:22 +00002353 assert($5->back().Ty->get() == Type::VoidTy && $5->back().Name == 0 &&
Reid Spencer1755a9a2007-02-05 17:01:20 +00002354 "Not a varargs marker!");
Reid Spencere1553cc2006-12-31 05:40:12 +00002355 delete $5->back().Ty;
Chris Lattnera8e8f162005-05-06 20:27:19 +00002356 $5->pop_back(); // Delete the last entry
Chris Lattner69da5cf2002-10-13 20:57:00 +00002357 }
Chris Lattnere4d5c442005-03-15 04:54:21 +00002358 Function::arg_iterator ArgIt = Fn->arg_begin();
Reid Spenceref9b9a72007-02-05 20:47:22 +00002359 Function::arg_iterator ArgEnd = Fn->arg_end();
Reid Spencere1553cc2006-12-31 05:40:12 +00002360 unsigned Idx = 1;
Reid Spenceref9b9a72007-02-05 20:47:22 +00002361 for (ArgListType::iterator I = $5->begin();
2362 I != $5->end() && ArgIt != ArgEnd; ++I, ++ArgIt) {
Reid Spencere1553cc2006-12-31 05:40:12 +00002363 delete I->Ty; // Delete the typeholder...
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002364 setValueName(ArgIt, I->Name); // Insert arg into symtab...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002365 CHECK_FOR_ERROR
Chris Lattner69da5cf2002-10-13 20:57:00 +00002366 InsertValue(ArgIt);
Reid Spencere1553cc2006-12-31 05:40:12 +00002367 Idx++;
Chris Lattner00950542001-06-06 20:29:01 +00002368 }
Reid Spencer08de34b2006-12-03 05:45:44 +00002369
Chris Lattnera8e8f162005-05-06 20:27:19 +00002370 delete $5; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00002371 }
Reid Spencer61c83e02006-08-18 08:43:06 +00002372 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002373};
Chris Lattner00950542001-06-06 20:29:01 +00002374
Chris Lattner9b02cc32002-05-03 18:23:48 +00002375BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
2376
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002377FunctionHeader : FunctionDefineLinkage GVVisibilityStyle FunctionHeaderH BEGIN {
Chris Lattner394f2eb2003-10-16 20:12:13 +00002378 $$ = CurFun.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00002379
Chris Lattner4ad02e72003-04-16 20:28:45 +00002380 // Make sure that we keep track of the linkage type even if there was a
2381 // previous "declare".
2382 $$->setLinkage($1);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002383 $$->setVisibility($2);
Chris Lattner51727be2002-06-04 21:58:56 +00002384};
Chris Lattner00950542001-06-06 20:29:01 +00002385
Chris Lattner9b02cc32002-05-03 18:23:48 +00002386END : ENDTOK | '}'; // Allow end of '}' to end a function
2387
Chris Lattner79df7c02002-03-26 18:01:55 +00002388Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00002389 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002390 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002391};
Chris Lattner00950542001-06-06 20:29:01 +00002392
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002393FunctionProto : FunctionDeclareLinkage GVVisibilityStyle FunctionHeaderH {
Reid Spencere1553cc2006-12-31 05:40:12 +00002394 CurFun.CurrentFunction->setLinkage($1);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002395 CurFun.CurrentFunction->setVisibility($2);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002396 $$ = CurFun.CurrentFunction;
2397 CurFun.FunctionDone();
2398 CHECK_FOR_ERROR
2399 };
Chris Lattner00950542001-06-06 20:29:01 +00002400
2401//===----------------------------------------------------------------------===//
2402// Rules to match Basic Blocks
2403//===----------------------------------------------------------------------===//
2404
Chris Lattneraa2c8532006-01-25 22:26:43 +00002405OptSideEffect : /* empty */ {
2406 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002407 CHECK_FOR_ERROR
Chris Lattneraa2c8532006-01-25 22:26:43 +00002408 }
2409 | SIDEEFFECT {
2410 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002411 CHECK_FOR_ERROR
Chris Lattneraa2c8532006-01-25 22:26:43 +00002412 };
2413
Chris Lattner00950542001-06-06 20:29:01 +00002414ConstValueRef : ESINT64VAL { // A reference to a direct constant
2415 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002416 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002417 }
2418 | EUINT64VAL {
2419 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002420 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002421 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00002422 | FPVAL { // Perhaps it's an FP constant?
2423 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002424 CHECK_FOR_ERROR
Chris Lattner3d52b2f2001-07-15 00:17:01 +00002425 }
Chris Lattner91ef4602004-03-31 03:49:47 +00002426 | TRUETOK {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00002427 $$ = ValID::create(ConstantInt::getTrue());
Reid Spencer61c83e02006-08-18 08:43:06 +00002428 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002429 }
Chris Lattner91ef4602004-03-31 03:49:47 +00002430 | FALSETOK {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00002431 $$ = ValID::create(ConstantInt::getFalse());
Reid Spencer61c83e02006-08-18 08:43:06 +00002432 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002433 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00002434 | NULL_TOK {
2435 $$ = ValID::createNull();
Reid Spencer61c83e02006-08-18 08:43:06 +00002436 CHECK_FOR_ERROR
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002437 }
Chris Lattner16710e92004-10-16 18:17:13 +00002438 | UNDEF {
2439 $$ = ValID::createUndef();
Reid Spencer61c83e02006-08-18 08:43:06 +00002440 CHECK_FOR_ERROR
Chris Lattner16710e92004-10-16 18:17:13 +00002441 }
Chris Lattnerf1f03df2005-12-21 17:53:02 +00002442 | ZEROINITIALIZER { // A vector zero constant.
2443 $$ = ValID::createZeroInit();
Reid Spencer61c83e02006-08-18 08:43:06 +00002444 CHECK_FOR_ERROR
Chris Lattnerf1f03df2005-12-21 17:53:02 +00002445 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00002446 | '<' ConstVector '>' { // Nonempty unsized packed vector
Reid Spencer08de34b2006-12-03 05:45:44 +00002447 const Type *ETy = (*$2)[0]->getType();
Brian Gaeke715c90b2004-08-20 06:00:58 +00002448 int NumElements = $2->size();
2449
Reid Spencer9d6565a2007-02-15 02:26:10 +00002450 VectorType* pt = VectorType::get(ETy, NumElements);
Brian Gaeke715c90b2004-08-20 06:00:58 +00002451 PATypeHolder* PTy = new PATypeHolder(
Reid Spencer08de34b2006-12-03 05:45:44 +00002452 HandleUpRefs(
Reid Spencer9d6565a2007-02-15 02:26:10 +00002453 VectorType::get(
Reid Spencer08de34b2006-12-03 05:45:44 +00002454 ETy,
2455 NumElements)
2456 )
2457 );
Brian Gaeke715c90b2004-08-20 06:00:58 +00002458
2459 // Verify all elements are correct type!
2460 for (unsigned i = 0; i < $2->size(); i++) {
Reid Spencer08de34b2006-12-03 05:45:44 +00002461 if (ETy != (*$2)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00002462 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Brian Gaeke715c90b2004-08-20 06:00:58 +00002463 ETy->getDescription() +"' as required!\nIt is of type '" +
Reid Spencer08de34b2006-12-03 05:45:44 +00002464 (*$2)[i]->getType()->getDescription() + "'.");
Brian Gaeke715c90b2004-08-20 06:00:58 +00002465 }
2466
Reid Spencer9d6565a2007-02-15 02:26:10 +00002467 $$ = ValID::create(ConstantVector::get(pt, *$2));
Brian Gaeke715c90b2004-08-20 06:00:58 +00002468 delete PTy; delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002469 CHECK_FOR_ERROR
Brian Gaeke715c90b2004-08-20 06:00:58 +00002470 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00002471 | ConstExpr {
Reid Spencer08de34b2006-12-03 05:45:44 +00002472 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002473 CHECK_FOR_ERROR
Chris Lattneraa2c8532006-01-25 22:26:43 +00002474 }
2475 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002476 $$ = ValID::createInlineAsm(*$3, *$5, $2);
2477 delete $3;
2478 delete $5;
Reid Spencer61c83e02006-08-18 08:43:06 +00002479 CHECK_FOR_ERROR
Chris Lattnerd78700d2002-08-16 21:14:40 +00002480 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00002481
Chris Lattner2079fde2001-10-13 06:41:08 +00002482// SymbolicValueRef - Reference to one of two ways of symbolically refering to
2483// another value.
2484//
Reid Spencerb2d17862007-01-26 08:04:51 +00002485SymbolicValueRef : LOCALVAL_ID { // Is it an integer reference...?
2486 $$ = ValID::createLocalID($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002487 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002488 }
Reid Spencerb2d17862007-01-26 08:04:51 +00002489 | GLOBALVAL_ID {
2490 $$ = ValID::createGlobalID($1);
2491 CHECK_FOR_ERROR
2492 }
2493 | LocalName { // Is it a named reference...?
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002494 $$ = ValID::createLocalName(*$1);
2495 delete $1;
Reid Spencerb2d17862007-01-26 08:04:51 +00002496 CHECK_FOR_ERROR
2497 }
2498 | GlobalName { // Is it a named reference...?
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002499 $$ = ValID::createGlobalName(*$1);
2500 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002501 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002502 };
Chris Lattner2079fde2001-10-13 06:41:08 +00002503
2504// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00002505ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00002506
Chris Lattner00950542001-06-06 20:29:01 +00002507
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002508// ResolvedVal - a <type> <value> pair. This is used only in cases where the
2509// type immediately preceeds the value reference, and allows complex constant
2510// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00002511ResolvedVal : Types ValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00002512 if (!UpRefs.empty())
2513 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2514 $$ = getVal(*$1, $2);
2515 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002516 CHECK_FOR_ERROR
Reid Spencere1553cc2006-12-31 05:40:12 +00002517 }
2518 ;
Chris Lattner8b81bf52001-07-25 22:47:46 +00002519
Chris Lattner00950542001-06-06 20:29:01 +00002520BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner8ab406d2004-07-13 07:59:27 +00002521 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002522 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002523 }
Chris Lattner7e708292002-06-25 16:13:24 +00002524 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
Chris Lattner8ab406d2004-07-13 07:59:27 +00002525 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002526 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002527 };
Chris Lattner00950542001-06-06 20:29:01 +00002528
2529
2530// Basic blocks are terminated by branching instructions:
2531// br, br/cc, switch, ret
2532//
Reid Spencerb2d17862007-01-26 08:04:51 +00002533BasicBlock : InstructionList OptLocalAssign BBTerminatorInst {
Chris Lattner8ab406d2004-07-13 07:59:27 +00002534 setValueName($3, $2);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002535 CHECK_FOR_ERROR
Chris Lattner2079fde2001-10-13 06:41:08 +00002536 InsertValue($3);
Chris Lattner2079fde2001-10-13 06:41:08 +00002537 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00002538 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002539 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002540 };
Chris Lattner00950542001-06-06 20:29:01 +00002541
2542InstructionList : InstructionList Inst {
Reid Spencer3da59db2006-11-27 01:05:10 +00002543 if (CastInst *CI1 = dyn_cast<CastInst>($2))
2544 if (CastInst *CI2 = dyn_cast<CastInst>(CI1->getOperand(0)))
2545 if (CI2->getParent() == 0)
2546 $1->getInstList().push_back(CI2);
Chris Lattner00950542001-06-06 20:29:01 +00002547 $1->getInstList().push_back($2);
2548 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002549 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002550 }
Reid Spencer186a43f2007-03-19 18:39:36 +00002551 | /* empty */ { // Empty space between instruction lists
2552 $$ = defineBBVal(ValID::createLocalID(CurFun.NextValNum));
Reid Spencer61c83e02006-08-18 08:43:06 +00002553 CHECK_FOR_ERROR
Chris Lattner22ae2322004-07-13 08:39:15 +00002554 }
Reid Spencer186a43f2007-03-19 18:39:36 +00002555 | LABELSTR { // Labelled (named) basic block
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002556 $$ = defineBBVal(ValID::createLocalName(*$1));
2557 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002558 CHECK_FOR_ERROR
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002559
Chris Lattner51727be2002-06-04 21:58:56 +00002560 };
Chris Lattner00950542001-06-06 20:29:01 +00002561
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002562BBTerminatorInst : RET ResolvedVal { // Return with a result...
Reid Spencer08de34b2006-12-03 05:45:44 +00002563 $$ = new ReturnInst($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00002564 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002565 }
Reid Spencer186a43f2007-03-19 18:39:36 +00002566 | RET VOID { // Return with no result...
Chris Lattner00950542001-06-06 20:29:01 +00002567 $$ = new ReturnInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002568 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002569 }
Reid Spencer186a43f2007-03-19 18:39:36 +00002570 | BR LABEL ValueRef { // Unconditional Branch...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002571 BasicBlock* tmpBB = getBBVal($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002572 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002573 $$ = new BranchInst(tmpBB);
Reid Spencer186a43f2007-03-19 18:39:36 +00002574 } // Conditional Branch...
Reid Spencer8088e9d2007-01-13 05:00:20 +00002575 | BR INTTYPE ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
2576 assert(cast<IntegerType>($2)->getBitWidth() == 1 && "Not Bool?");
Reid Spencer5b7e7532006-09-28 19:28:24 +00002577 BasicBlock* tmpBBA = getBBVal($6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002578 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002579 BasicBlock* tmpBBB = getBBVal($9);
2580 CHECK_FOR_ERROR
Reid Spencer4fe16d62007-01-11 18:21:29 +00002581 Value* tmpVal = getVal(Type::Int1Ty, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002582 CHECK_FOR_ERROR
2583 $$ = new BranchInst(tmpBBA, tmpBBB, tmpVal);
Chris Lattner00950542001-06-06 20:29:01 +00002584 }
2585 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencer08de34b2006-12-03 05:45:44 +00002586 Value* tmpVal = getVal($2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002587 CHECK_FOR_ERROR
2588 BasicBlock* tmpBB = getBBVal($6);
2589 CHECK_FOR_ERROR
2590 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, $8->size());
Chris Lattner00950542001-06-06 20:29:01 +00002591 $$ = S;
2592
Chris Lattner9232b992003-04-22 18:42:41 +00002593 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
Chris Lattner46748042002-04-09 19:41:42 +00002594 E = $8->end();
Chris Lattner69331f52005-02-24 05:25:17 +00002595 for (; I != E; ++I) {
2596 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
2597 S->addCase(CI, I->second);
2598 else
Reid Spencerf4fa5902007-02-05 10:16:10 +00002599 GEN_ERROR("Switch case is constant, but not a simple integer");
Chris Lattner69331f52005-02-24 05:25:17 +00002600 }
Chris Lattnerf57a43d2004-04-17 23:49:15 +00002601 delete $8;
Reid Spencer61c83e02006-08-18 08:43:06 +00002602 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002603 }
Chris Lattner196850c2003-05-15 21:30:00 +00002604 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencer08de34b2006-12-03 05:45:44 +00002605 Value* tmpVal = getVal($2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002606 CHECK_FOR_ERROR
2607 BasicBlock* tmpBB = getBBVal($6);
2608 CHECK_FOR_ERROR
2609 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, 0);
Chris Lattner196850c2003-05-15 21:30:00 +00002610 $$ = S;
Reid Spencer61c83e02006-08-18 08:43:06 +00002611 CHECK_FOR_ERROR
Chris Lattner196850c2003-05-15 21:30:00 +00002612 }
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002613 | INVOKE OptCallingConv ResultTypes ValueRef '(' ParamList ')' OptFuncAttrs
Chris Lattnera8e8f162005-05-06 20:27:19 +00002614 TO LABEL ValueRef UNWIND LABEL ValueRef {
Chris Lattner2079fde2001-10-13 06:41:08 +00002615
Reid Spencere1553cc2006-12-31 05:40:12 +00002616 // Handle the short syntax
2617 const PointerType *PFTy = 0;
2618 const FunctionType *Ty = 0;
Reid Spencer2c261782007-01-05 17:06:19 +00002619 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002620 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00002621 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00002622 std::vector<const Type*> ParamTypes;
Reid Spencer863dd882007-04-28 16:06:50 +00002623 ParamAttrsVector Attrs;
2624 if ($8 != ParamAttr::None) {
Chris Lattnerdd3094a2007-05-04 04:01:07 +00002625 ParamAttrsWithIndex PAWI; PAWI.index = 0; PAWI.attrs = $8;
Reid Spencer863dd882007-04-28 16:06:50 +00002626 Attrs.push_back(PAWI);
2627 }
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002628 ParamList::iterator I = $6->begin(), E = $6->end();
Reid Spencer863dd882007-04-28 16:06:50 +00002629 unsigned index = 1;
2630 for (; I != E; ++I, ++index) {
Reid Spencere1553cc2006-12-31 05:40:12 +00002631 const Type *Ty = I->Val->getType();
2632 if (Ty == Type::VoidTy)
2633 GEN_ERROR("Short call syntax cannot be used with varargs");
2634 ParamTypes.push_back(Ty);
Reid Spencer863dd882007-04-28 16:06:50 +00002635 if (I->Attrs != ParamAttr::None) {
2636 ParamAttrsWithIndex PAWI; PAWI.index = index; PAWI.attrs = I->Attrs;
2637 Attrs.push_back(PAWI);
2638 }
Chris Lattner2079fde2001-10-13 06:41:08 +00002639 }
Reid Spencer863dd882007-04-28 16:06:50 +00002640
2641 ParamAttrsList *PAL = 0;
2642 if (!Attrs.empty())
2643 PAL = ParamAttrsList::get(Attrs);
2644 Ty = FunctionType::get($3->get(), ParamTypes, false, PAL);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002645 PFTy = PointerType::get(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00002646 }
Chris Lattner2079fde2001-10-13 06:41:08 +00002647
Reid Spencer2c9df212007-03-20 01:13:00 +00002648 delete $3;
2649
Chris Lattnera8e8f162005-05-06 20:27:19 +00002650 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002651 CHECK_FOR_ERROR
Reid Spencer2c261782007-01-05 17:06:19 +00002652 BasicBlock *Normal = getBBVal($11);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002653 CHECK_FOR_ERROR
Reid Spencer2c261782007-01-05 17:06:19 +00002654 BasicBlock *Except = getBBVal($14);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002655 CHECK_FOR_ERROR
Chris Lattner2079fde2001-10-13 06:41:08 +00002656
Reid Spencere1553cc2006-12-31 05:40:12 +00002657 // Check the arguments
2658 ValueList Args;
2659 if ($6->empty()) { // Has no arguments?
2660 // Make sure no arguments is a good thing!
2661 if (Ty->getNumParams() != 0)
2662 GEN_ERROR("No arguments passed to a function that "
Reid Spencerf4fa5902007-02-05 10:16:10 +00002663 "expects arguments");
Chris Lattner2079fde2001-10-13 06:41:08 +00002664 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00002665 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00002666 // correctly!
Chris Lattnerd5d89962004-02-09 04:14:01 +00002667 FunctionType::param_iterator I = Ty->param_begin();
2668 FunctionType::param_iterator E = Ty->param_end();
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002669 ParamList::iterator ArgI = $6->begin(), ArgE = $6->end();
Reid Spencer863dd882007-04-28 16:06:50 +00002670
Reid Spencere1553cc2006-12-31 05:40:12 +00002671 for (; ArgI != ArgE && I != E; ++ArgI, ++I) {
2672 if (ArgI->Val->getType() != *I)
2673 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00002674 (*I)->getDescription() + "'");
Reid Spencere1553cc2006-12-31 05:40:12 +00002675 Args.push_back(ArgI->Val);
2676 }
Reid Spencer08de34b2006-12-03 05:45:44 +00002677
Reid Spencere1553cc2006-12-31 05:40:12 +00002678 if (Ty->isVarArg()) {
2679 if (I == E)
2680 for (; ArgI != ArgE; ++ArgI)
2681 Args.push_back(ArgI->Val); // push the remaining varargs
2682 } else if (I != E || ArgI != ArgE)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002683 GEN_ERROR("Invalid number of parameters detected");
Chris Lattner2079fde2001-10-13 06:41:08 +00002684 }
Reid Spencere1553cc2006-12-31 05:40:12 +00002685
2686 // Create the InvokeInst
David Greenef1355a52007-08-27 19:04:21 +00002687 InvokeInst *II = new InvokeInst(V, Normal, Except, Args.begin(), Args.end());
Reid Spencere1553cc2006-12-31 05:40:12 +00002688 II->setCallingConv($2);
2689 $$ = II;
Chris Lattnera8e8f162005-05-06 20:27:19 +00002690 delete $6;
Reid Spencer61c83e02006-08-18 08:43:06 +00002691 CHECK_FOR_ERROR
Chris Lattner36143fc2003-09-08 18:54:55 +00002692 }
2693 | UNWIND {
2694 $$ = new UnwindInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002695 CHECK_FOR_ERROR
Chris Lattner16710e92004-10-16 18:17:13 +00002696 }
2697 | UNREACHABLE {
2698 $$ = new UnreachableInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002699 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002700 };
Chris Lattner2079fde2001-10-13 06:41:08 +00002701
2702
Chris Lattner00950542001-06-06 20:29:01 +00002703
2704JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
2705 $$ = $1;
Reid Spencer186a43f2007-03-19 18:39:36 +00002706 Constant *V = cast<Constant>(getExistingVal($2, $3));
Reid Spencer5b7e7532006-09-28 19:28:24 +00002707 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002708 if (V == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002709 GEN_ERROR("May only switch on a constant pool value");
Chris Lattner00950542001-06-06 20:29:01 +00002710
Reid Spencer5b7e7532006-09-28 19:28:24 +00002711 BasicBlock* tmpBB = getBBVal($6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002712 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002713 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner00950542001-06-06 20:29:01 +00002714 }
2715 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner9232b992003-04-22 18:42:41 +00002716 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Reid Spencer186a43f2007-03-19 18:39:36 +00002717 Constant *V = cast<Constant>(getExistingVal($1, $2));
Reid Spencer5b7e7532006-09-28 19:28:24 +00002718 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002719
2720 if (V == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002721 GEN_ERROR("May only switch on a constant pool value");
Chris Lattner00950542001-06-06 20:29:01 +00002722
Reid Spencer5b7e7532006-09-28 19:28:24 +00002723 BasicBlock* tmpBB = getBBVal($5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002724 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002725 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner51727be2002-06-04 21:58:56 +00002726 };
Chris Lattner00950542001-06-06 20:29:01 +00002727
Reid Spencerb2d17862007-01-26 08:04:51 +00002728Inst : OptLocalAssign InstVal {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002729 // Is this definition named?? if so, assign the name...
2730 setValueName($2, $1);
2731 CHECK_FOR_ERROR
2732 InsertValue($2);
2733 $$ = $2;
2734 CHECK_FOR_ERROR
2735 };
2736
Chris Lattner00950542001-06-06 20:29:01 +00002737
Chris Lattnerc24d2082001-06-11 15:04:20 +00002738PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Reid Spencere1553cc2006-12-31 05:40:12 +00002739 if (!UpRefs.empty())
2740 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Chris Lattner9232b992003-04-22 18:42:41 +00002741 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Reid Spencer08de34b2006-12-03 05:45:44 +00002742 Value* tmpVal = getVal(*$1, $3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002743 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002744 BasicBlock* tmpBB = getBBVal($5);
2745 CHECK_FOR_ERROR
2746 $$->push_back(std::make_pair(tmpVal, tmpBB));
Reid Spencer08de34b2006-12-03 05:45:44 +00002747 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00002748 }
2749 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
2750 $$ = $1;
Reid Spencer5b7e7532006-09-28 19:28:24 +00002751 Value* tmpVal = getVal($1->front().first->getType(), $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00002752 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002753 BasicBlock* tmpBB = getBBVal($6);
2754 CHECK_FOR_ERROR
2755 $1->push_back(std::make_pair(tmpVal, tmpBB));
Chris Lattner51727be2002-06-04 21:58:56 +00002756 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00002757
2758
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002759ParamList : Types ValueRef OptParamAttrs {
Reid Spencere1553cc2006-12-31 05:40:12 +00002760 if (!UpRefs.empty())
2761 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2762 // Used for call and invoke instructions
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002763 $$ = new ParamList();
2764 ParamListEntry E; E.Attrs = $3; E.Val = getVal($1->get(), $2);
Reid Spencere1553cc2006-12-31 05:40:12 +00002765 $$->push_back(E);
Reid Spencer2c9df212007-03-20 01:13:00 +00002766 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00002767 }
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002768 | LABEL ValueRef OptParamAttrs {
2769 // Labels are only valid in ASMs
2770 $$ = new ParamList();
2771 ParamListEntry E; E.Attrs = $3; E.Val = getBBVal($2);
2772 $$->push_back(E);
2773 }
2774 | ParamList ',' Types ValueRef OptParamAttrs {
Reid Spencere1553cc2006-12-31 05:40:12 +00002775 if (!UpRefs.empty())
2776 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattner00950542001-06-06 20:29:01 +00002777 $$ = $1;
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002778 ParamListEntry E; E.Attrs = $5; E.Val = getVal($3->get(), $4);
Reid Spencere1553cc2006-12-31 05:40:12 +00002779 $$->push_back(E);
Reid Spencer2c9df212007-03-20 01:13:00 +00002780 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002781 CHECK_FOR_ERROR
Reid Spencere1553cc2006-12-31 05:40:12 +00002782 }
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002783 | ParamList ',' LABEL ValueRef OptParamAttrs {
2784 $$ = $1;
2785 ParamListEntry E; E.Attrs = $5; E.Val = getBBVal($4);
2786 $$->push_back(E);
2787 CHECK_FOR_ERROR
2788 }
2789 | /*empty*/ { $$ = new ParamList(); };
Chris Lattner00950542001-06-06 20:29:01 +00002790
Reid Spencere1553cc2006-12-31 05:40:12 +00002791IndexList // Used for gep instructions and constant expressions
Reid Spencere03969f2006-12-31 21:46:36 +00002792 : /*empty*/ { $$ = new std::vector<Value*>(); }
Reid Spencere1553cc2006-12-31 05:40:12 +00002793 | IndexList ',' ResolvedVal {
2794 $$ = $1;
2795 $$->push_back($3);
2796 CHECK_FOR_ERROR
2797 }
Reid Spencer71305d72006-12-31 21:25:25 +00002798 ;
Chris Lattner00950542001-06-06 20:29:01 +00002799
Chris Lattnerddb6db42005-05-06 05:51:46 +00002800OptTailCall : TAIL CALL {
2801 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002802 CHECK_FOR_ERROR
Chris Lattnerddb6db42005-05-06 05:51:46 +00002803 }
2804 | CALL {
2805 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002806 CHECK_FOR_ERROR
Chris Lattnerddb6db42005-05-06 05:51:46 +00002807 };
2808
Chris Lattner4a6482b2002-09-10 19:57:26 +00002809InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00002810 if (!UpRefs.empty())
2811 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Chris Lattner42a75512007-01-15 02:27:26 +00002812 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint() &&
Reid Spencer9d6565a2007-02-15 02:26:10 +00002813 !isa<VectorType>((*$2).get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002814 GEN_ERROR(
Reid Spencerf4fa5902007-02-05 10:16:10 +00002815 "Arithmetic operator requires integer, FP, or packed operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00002816 Value* val1 = getVal(*$2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002817 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00002818 Value* val2 = getVal(*$2, $5);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002819 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00002820 $$ = BinaryOperator::create($1, val1, val2);
Chris Lattner4a6482b2002-09-10 19:57:26 +00002821 if ($$ == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002822 GEN_ERROR("binary operator returned null");
Reid Spencer08de34b2006-12-03 05:45:44 +00002823 delete $2;
Chris Lattner4a6482b2002-09-10 19:57:26 +00002824 }
2825 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00002826 if (!UpRefs.empty())
2827 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Chris Lattner42a75512007-01-15 02:27:26 +00002828 if (!(*$2)->isInteger()) {
Reid Spencer9d6565a2007-02-15 02:26:10 +00002829 if (Instruction::isShift($1) || !isa<VectorType>($2->get()) ||
2830 !cast<VectorType>($2->get())->getElementType()->isInteger())
Reid Spencerf4fa5902007-02-05 10:16:10 +00002831 GEN_ERROR("Logical operator requires integral operands");
Chris Lattner0a017832005-12-21 18:31:29 +00002832 }
Reid Spencer08de34b2006-12-03 05:45:44 +00002833 Value* tmpVal1 = getVal(*$2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002834 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00002835 Value* tmpVal2 = getVal(*$2, $5);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002836 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00002837 $$ = BinaryOperator::create($1, tmpVal1, tmpVal2);
Chris Lattner4a6482b2002-09-10 19:57:26 +00002838 if ($$ == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002839 GEN_ERROR("binary operator returned null");
Reid Spencer08de34b2006-12-03 05:45:44 +00002840 delete $2;
Chris Lattner4a6482b2002-09-10 19:57:26 +00002841 }
Reid Spencer08de34b2006-12-03 05:45:44 +00002842 | ICMP IPredicates Types ValueRef ',' ValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00002843 if (!UpRefs.empty())
2844 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencer9d6565a2007-02-15 02:26:10 +00002845 if (isa<VectorType>((*$3).get()))
Reid Spencerac9dcb92007-02-15 03:39:18 +00002846 GEN_ERROR("Vector types not supported by icmp instruction");
Reid Spencer08de34b2006-12-03 05:45:44 +00002847 Value* tmpVal1 = getVal(*$3, $4);
2848 CHECK_FOR_ERROR
2849 Value* tmpVal2 = getVal(*$3, $6);
2850 CHECK_FOR_ERROR
2851 $$ = CmpInst::create($1, $2, tmpVal1, tmpVal2);
2852 if ($$ == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002853 GEN_ERROR("icmp operator returned null");
Reid Spencer2c9df212007-03-20 01:13:00 +00002854 delete $3;
Reid Spencer08de34b2006-12-03 05:45:44 +00002855 }
2856 | FCMP FPredicates Types ValueRef ',' ValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00002857 if (!UpRefs.empty())
2858 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencer9d6565a2007-02-15 02:26:10 +00002859 if (isa<VectorType>((*$3).get()))
Reid Spencerac9dcb92007-02-15 03:39:18 +00002860 GEN_ERROR("Vector types not supported by fcmp instruction");
Reid Spencer08de34b2006-12-03 05:45:44 +00002861 Value* tmpVal1 = getVal(*$3, $4);
2862 CHECK_FOR_ERROR
2863 Value* tmpVal2 = getVal(*$3, $6);
2864 CHECK_FOR_ERROR
2865 $$ = CmpInst::create($1, $2, tmpVal1, tmpVal2);
2866 if ($$ == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002867 GEN_ERROR("fcmp operator returned null");
Reid Spencer2c9df212007-03-20 01:13:00 +00002868 delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00002869 }
Reid Spencer3da59db2006-11-27 01:05:10 +00002870 | CastOps ResolvedVal TO Types {
Reid Spencere1553cc2006-12-31 05:40:12 +00002871 if (!UpRefs.empty())
2872 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00002873 Value* Val = $2;
Reid Spencer93947c32007-01-17 02:47:33 +00002874 const Type* DestTy = $4->get();
2875 if (!CastInst::castIsValid($1, Val, DestTy))
2876 GEN_ERROR("invalid cast opcode for cast from '" +
2877 Val->getType()->getDescription() + "' to '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00002878 DestTy->getDescription() + "'");
Reid Spencer93947c32007-01-17 02:47:33 +00002879 $$ = CastInst::create($1, Val, DestTy);
Reid Spencer08de34b2006-12-03 05:45:44 +00002880 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00002881 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00002882 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer4fe16d62007-01-11 18:21:29 +00002883 if ($2->getType() != Type::Int1Ty)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002884 GEN_ERROR("select condition must be boolean");
Reid Spencer08de34b2006-12-03 05:45:44 +00002885 if ($4->getType() != $6->getType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00002886 GEN_ERROR("select value types should match");
Reid Spencer08de34b2006-12-03 05:45:44 +00002887 $$ = new SelectInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002888 CHECK_FOR_ERROR
Chris Lattner76f0ff32004-03-12 05:51:36 +00002889 }
Chris Lattner99e7ab72003-10-18 05:53:13 +00002890 | VAARG ResolvedVal ',' Types {
Reid Spencere1553cc2006-12-31 05:40:12 +00002891 if (!UpRefs.empty())
2892 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00002893 $$ = new VAArgInst($2, *$4);
2894 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00002895 CHECK_FOR_ERROR
Chris Lattner99e7ab72003-10-18 05:53:13 +00002896 }
Robert Bocchino9c62b562006-01-10 19:04:32 +00002897 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencer08de34b2006-12-03 05:45:44 +00002898 if (!ExtractElementInst::isValidOperands($2, $4))
Reid Spencerf4fa5902007-02-05 10:16:10 +00002899 GEN_ERROR("Invalid extractelement operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00002900 $$ = new ExtractElementInst($2, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00002901 CHECK_FOR_ERROR
Robert Bocchino9c62b562006-01-10 19:04:32 +00002902 }
Robert Bocchino2def1b32006-01-17 20:06:25 +00002903 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer08de34b2006-12-03 05:45:44 +00002904 if (!InsertElementInst::isValidOperands($2, $4, $6))
Reid Spencerf4fa5902007-02-05 10:16:10 +00002905 GEN_ERROR("Invalid insertelement operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00002906 $$ = new InsertElementInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002907 CHECK_FOR_ERROR
Robert Bocchino2def1b32006-01-17 20:06:25 +00002908 }
Chris Lattner4c949082006-04-08 01:18:35 +00002909 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer08de34b2006-12-03 05:45:44 +00002910 if (!ShuffleVectorInst::isValidOperands($2, $4, $6))
Reid Spencerf4fa5902007-02-05 10:16:10 +00002911 GEN_ERROR("Invalid shufflevector operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00002912 $$ = new ShuffleVectorInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002913 CHECK_FOR_ERROR
Chris Lattner4c949082006-04-08 01:18:35 +00002914 }
Chris Lattner3b237fc2003-10-19 21:34:28 +00002915 | PHI_TOK PHIList {
Chris Lattnerc24d2082001-06-11 15:04:20 +00002916 const Type *Ty = $2->front().first->getType();
Chris Lattner8ea8f362003-10-30 01:38:18 +00002917 if (!Ty->isFirstClassType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00002918 GEN_ERROR("PHI node operands must be of first class type");
Chris Lattnerc24d2082001-06-11 15:04:20 +00002919 $$ = new PHINode(Ty);
Chris Lattnerce1df9e2005-01-29 00:35:55 +00002920 ((PHINode*)$$)->reserveOperandSpace($2->size());
Chris Lattner00950542001-06-06 20:29:01 +00002921 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00002922 if ($2->front().first->getType() != Ty)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002923 GEN_ERROR("All elements of a PHI node must be of the same type");
Chris Lattnerb00c5822001-10-02 03:41:24 +00002924 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00002925 $2->pop_front();
2926 }
2927 delete $2; // Free the list...
Reid Spencer61c83e02006-08-18 08:43:06 +00002928 CHECK_FOR_ERROR
Chris Lattnerddb6db42005-05-06 05:51:46 +00002929 }
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002930 | OptTailCall OptCallingConv ResultTypes ValueRef '(' ParamList ')'
Reid Spencer2c261782007-01-05 17:06:19 +00002931 OptFuncAttrs {
Reid Spencere1553cc2006-12-31 05:40:12 +00002932
2933 // Handle the short syntax
Bill Wendlingb39a55a2006-11-12 11:10:39 +00002934 const PointerType *PFTy = 0;
2935 const FunctionType *Ty = 0;
Reid Spencer2c261782007-01-05 17:06:19 +00002936 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002937 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00002938 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00002939 std::vector<const Type*> ParamTypes;
Reid Spencer863dd882007-04-28 16:06:50 +00002940 ParamAttrsVector Attrs;
2941 if ($8 != ParamAttr::None) {
2942 ParamAttrsWithIndex PAWI; PAWI.index = 0; PAWI.attrs = $8;
2943 Attrs.push_back(PAWI);
2944 }
2945 unsigned index = 1;
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002946 ParamList::iterator I = $6->begin(), E = $6->end();
Reid Spencer863dd882007-04-28 16:06:50 +00002947 for (; I != E; ++I, ++index) {
Reid Spencere1553cc2006-12-31 05:40:12 +00002948 const Type *Ty = I->Val->getType();
2949 if (Ty == Type::VoidTy)
2950 GEN_ERROR("Short call syntax cannot be used with varargs");
2951 ParamTypes.push_back(Ty);
Reid Spencer863dd882007-04-28 16:06:50 +00002952 if (I->Attrs != ParamAttr::None) {
2953 ParamAttrsWithIndex PAWI; PAWI.index = index; PAWI.attrs = I->Attrs;
2954 Attrs.push_back(PAWI);
2955 }
Chris Lattneref9c23f2001-10-03 14:53:21 +00002956 }
Reid Spencer863dd882007-04-28 16:06:50 +00002957
2958 ParamAttrsList *PAL = 0;
2959 if (!Attrs.empty())
2960 PAL = ParamAttrsList::get(Attrs);
2961
2962 Ty = FunctionType::get($3->get(), ParamTypes, false, PAL);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002963 PFTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00002964 }
Chris Lattner00950542001-06-06 20:29:01 +00002965
Chris Lattnera8e8f162005-05-06 20:27:19 +00002966 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002967 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002968
Reid Spencerebff55c2007-04-16 06:55:42 +00002969 // Check for call to invalid intrinsic to avoid crashing later.
2970 if (Function *theF = dyn_cast<Function>(V)) {
Reid Spencerce1e8ef2007-04-16 22:01:57 +00002971 if (theF->hasName() && (theF->getValueName()->getKeyLength() >= 5) &&
Reid Spencer43e60732007-04-16 20:31:06 +00002972 (0 == strncmp(theF->getValueName()->getKeyData(), "llvm.", 5)) &&
2973 !theF->getIntrinsicID(true))
Reid Spencerebff55c2007-04-16 06:55:42 +00002974 GEN_ERROR("Call to invalid LLVM intrinsic function '" +
2975 theF->getName() + "'");
2976 }
2977
Reid Spencere1553cc2006-12-31 05:40:12 +00002978 // Check the arguments
2979 ValueList Args;
2980 if ($6->empty()) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00002981 // Make sure no arguments is a good thing!
2982 if (Ty->getNumParams() != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002983 GEN_ERROR("No arguments passed to a function that "
Reid Spencerf4fa5902007-02-05 10:16:10 +00002984 "expects arguments");
Chris Lattner8b81bf52001-07-25 22:47:46 +00002985 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00002986 // Loop through FunctionType's arguments and ensure they are specified
Reid Spencer863dd882007-04-28 16:06:50 +00002987 // correctly!
2988 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00002989 FunctionType::param_iterator I = Ty->param_begin();
2990 FunctionType::param_iterator E = Ty->param_end();
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002991 ParamList::iterator ArgI = $6->begin(), ArgE = $6->end();
Reid Spencer863dd882007-04-28 16:06:50 +00002992
Reid Spencere1553cc2006-12-31 05:40:12 +00002993 for (; ArgI != ArgE && I != E; ++ArgI, ++I) {
2994 if (ArgI->Val->getType() != *I)
2995 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00002996 (*I)->getDescription() + "'");
Reid Spencere1553cc2006-12-31 05:40:12 +00002997 Args.push_back(ArgI->Val);
2998 }
2999 if (Ty->isVarArg()) {
3000 if (I == E)
3001 for (; ArgI != ArgE; ++ArgI)
3002 Args.push_back(ArgI->Val); // push the remaining varargs
3003 } else if (I != E || ArgI != ArgE)
Reid Spencerf4fa5902007-02-05 10:16:10 +00003004 GEN_ERROR("Invalid number of parameters detected");
Chris Lattner8b81bf52001-07-25 22:47:46 +00003005 }
Reid Spencere1553cc2006-12-31 05:40:12 +00003006 // Create the call node
David Greene52eec542007-08-01 03:43:44 +00003007 CallInst *CI = new CallInst(V, Args.begin(), Args.end());
Reid Spencere1553cc2006-12-31 05:40:12 +00003008 CI->setTailCall($1);
3009 CI->setCallingConv($2);
3010 $$ = CI;
Chris Lattnera8e8f162005-05-06 20:27:19 +00003011 delete $6;
Reid Spencerb2d17862007-01-26 08:04:51 +00003012 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00003013 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00003014 }
3015 | MemoryInst {
3016 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00003017 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00003018 };
Chris Lattner00950542001-06-06 20:29:01 +00003019
Chris Lattner15c9c032003-09-08 18:20:29 +00003020OptVolatile : VOLATILE {
3021 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00003022 CHECK_FOR_ERROR
Chris Lattner15c9c032003-09-08 18:20:29 +00003023 }
3024 | /* empty */ {
3025 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00003026 CHECK_FOR_ERROR
Chris Lattner15c9c032003-09-08 18:20:29 +00003027 };
3028
Chris Lattner027dcc52001-07-08 21:10:27 +00003029
Chris Lattnerddb6db42005-05-06 05:51:46 +00003030
Chris Lattner66db8e42005-11-06 06:34:12 +00003031MemoryInst : MALLOC Types OptCAlign {
Reid Spencere1553cc2006-12-31 05:40:12 +00003032 if (!UpRefs.empty())
3033 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00003034 $$ = new MallocInst(*$2, 0, $3);
3035 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00003036 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00003037 }
Reid Spencera54b7cb2007-01-12 07:05:14 +00003038 | MALLOC Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencere1553cc2006-12-31 05:40:12 +00003039 if (!UpRefs.empty())
3040 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00003041 Value* tmpVal = getVal($4, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00003042 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00003043 $$ = new MallocInst(*$2, tmpVal, $6);
3044 delete $2;
Nate Begeman14b05292005-11-05 09:21:28 +00003045 }
Chris Lattner66db8e42005-11-06 06:34:12 +00003046 | ALLOCA Types OptCAlign {
Reid Spencere1553cc2006-12-31 05:40:12 +00003047 if (!UpRefs.empty())
3048 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00003049 $$ = new AllocaInst(*$2, 0, $3);
3050 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00003051 CHECK_FOR_ERROR
Nate Begeman14b05292005-11-05 09:21:28 +00003052 }
Reid Spencera54b7cb2007-01-12 07:05:14 +00003053 | ALLOCA Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencere1553cc2006-12-31 05:40:12 +00003054 if (!UpRefs.empty())
3055 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00003056 Value* tmpVal = getVal($4, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00003057 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00003058 $$ = new AllocaInst(*$2, tmpVal, $6);
3059 delete $2;
Nate Begeman14b05292005-11-05 09:21:28 +00003060 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00003061 | FREE ResolvedVal {
Reid Spencer08de34b2006-12-03 05:45:44 +00003062 if (!isa<PointerType>($2->getType()))
Reid Spencer61c83e02006-08-18 08:43:06 +00003063 GEN_ERROR("Trying to free nonpointer type " +
Reid Spencerf4fa5902007-02-05 10:16:10 +00003064 $2->getType()->getDescription() + "");
Reid Spencer08de34b2006-12-03 05:45:44 +00003065 $$ = new FreeInst($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00003066 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00003067 }
3068
Christopher Lamb43c7f372007-04-22 19:24:39 +00003069 | OptVolatile LOAD Types ValueRef OptCAlign {
Reid Spencere1553cc2006-12-31 05:40:12 +00003070 if (!UpRefs.empty())
3071 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00003072 if (!isa<PointerType>($3->get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00003073 GEN_ERROR("Can't load from nonpointer type: " +
Reid Spencer08de34b2006-12-03 05:45:44 +00003074 (*$3)->getDescription());
3075 if (!cast<PointerType>($3->get())->getElementType()->isFirstClassType())
Reid Spencer61c83e02006-08-18 08:43:06 +00003076 GEN_ERROR("Can't load from pointer of non-first-class type: " +
Reid Spencer08de34b2006-12-03 05:45:44 +00003077 (*$3)->getDescription());
3078 Value* tmpVal = getVal(*$3, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00003079 CHECK_FOR_ERROR
Christopher Lamb43c7f372007-04-22 19:24:39 +00003080 $$ = new LoadInst(tmpVal, "", $1, $5);
Reid Spencer08de34b2006-12-03 05:45:44 +00003081 delete $3;
Chris Lattner027dcc52001-07-08 21:10:27 +00003082 }
Christopher Lamb43c7f372007-04-22 19:24:39 +00003083 | OptVolatile STORE ResolvedVal ',' Types ValueRef OptCAlign {
Reid Spencere1553cc2006-12-31 05:40:12 +00003084 if (!UpRefs.empty())
3085 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00003086 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner8c8418d2003-09-01 16:31:28 +00003087 if (!PT)
Reid Spencer61c83e02006-08-18 08:43:06 +00003088 GEN_ERROR("Can't store to a nonpointer type: " +
Reid Spencer08de34b2006-12-03 05:45:44 +00003089 (*$5)->getDescription());
Chris Lattner8c8418d2003-09-01 16:31:28 +00003090 const Type *ElTy = PT->getElementType();
Reid Spencer08de34b2006-12-03 05:45:44 +00003091 if (ElTy != $3->getType())
3092 GEN_ERROR("Can't store '" + $3->getType()->getDescription() +
Reid Spencerf4fa5902007-02-05 10:16:10 +00003093 "' into space of type '" + ElTy->getDescription() + "'");
Chris Lattner0383cc42002-08-21 23:51:21 +00003094
Reid Spencer08de34b2006-12-03 05:45:44 +00003095 Value* tmpVal = getVal(*$5, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00003096 CHECK_FOR_ERROR
Christopher Lamb43c7f372007-04-22 19:24:39 +00003097 $$ = new StoreInst($3, tmpVal, $1, $7);
Reid Spencer08de34b2006-12-03 05:45:44 +00003098 delete $5;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00003099 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00003100 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencere1553cc2006-12-31 05:40:12 +00003101 if (!UpRefs.empty())
3102 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00003103 if (!isa<PointerType>($2->get()))
Reid Spencerf4fa5902007-02-05 10:16:10 +00003104 GEN_ERROR("getelementptr insn requires pointer operand");
Chris Lattner830b6f92004-04-05 01:30:04 +00003105
David Greeneb8f74792007-09-04 15:46:09 +00003106 if (!GetElementPtrInst::getIndexedType(*$2, $4->begin(), $4->end(), true))
Reid Spencer61c83e02006-08-18 08:43:06 +00003107 GEN_ERROR("Invalid getelementptr indices for type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00003108 (*$2)->getDescription()+ "'");
Reid Spencer08de34b2006-12-03 05:45:44 +00003109 Value* tmpVal = getVal(*$2, $3);
Reid Spencer61c83e02006-08-18 08:43:06 +00003110 CHECK_FOR_ERROR
David Greeneb8f74792007-09-04 15:46:09 +00003111 $$ = new GetElementPtrInst(tmpVal, $4->begin(), $4->end());
Reid Spencer08de34b2006-12-03 05:45:44 +00003112 delete $2;
Reid Spencer5b7e7532006-09-28 19:28:24 +00003113 delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00003114 };
Chris Lattner027dcc52001-07-08 21:10:27 +00003115
Brian Gaeked0fde302003-11-11 22:41:34 +00003116
Chris Lattner00950542001-06-06 20:29:01 +00003117%%
Reid Spencer61c83e02006-08-18 08:43:06 +00003118
Reid Spencere1553cc2006-12-31 05:40:12 +00003119// common code from the two 'RunVMAsmParser' functions
3120static Module* RunParser(Module * M) {
3121
3122 llvmAsmlineno = 1; // Reset the current line number...
3123 CurModule.CurrentModule = M;
3124#if YYDEBUG
3125 yydebug = Debug;
3126#endif
3127
3128 // Check to make sure the parser succeeded
3129 if (yyparse()) {
3130 if (ParserResult)
3131 delete ParserResult;
3132 return 0;
3133 }
3134
Reid Spencercd5bd902007-03-30 01:37:13 +00003135 // Emit an error if there are any unresolved types left.
3136 if (!CurModule.LateResolveTypes.empty()) {
3137 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
3138 if (DID.Type == ValID::LocalName) {
3139 GenerateError("Undefined type remains at eof: '"+DID.getName() + "'");
3140 } else {
3141 GenerateError("Undefined type remains at eof: #" + itostr(DID.Num));
3142 }
3143 if (ParserResult)
3144 delete ParserResult;
3145 return 0;
3146 }
3147
3148 // Emit an error if there are any unresolved values left.
3149 if (!CurModule.LateResolveValues.empty()) {
3150 Value *V = CurModule.LateResolveValues.back();
3151 std::map<Value*, std::pair<ValID, int> >::iterator I =
3152 CurModule.PlaceHolderInfo.find(V);
3153
3154 if (I != CurModule.PlaceHolderInfo.end()) {
3155 ValID &DID = I->second.first;
3156 if (DID.Type == ValID::LocalName) {
3157 GenerateError("Undefined value remains at eof: "+DID.getName() + "'");
3158 } else {
3159 GenerateError("Undefined value remains at eof: #" + itostr(DID.Num));
3160 }
3161 if (ParserResult)
3162 delete ParserResult;
3163 return 0;
3164 }
3165 }
3166
Reid Spencere1553cc2006-12-31 05:40:12 +00003167 // Check to make sure that parsing produced a result
3168 if (!ParserResult)
3169 return 0;
3170
3171 // Reset ParserResult variable while saving its value for the result.
3172 Module *Result = ParserResult;
3173 ParserResult = 0;
3174
3175 return Result;
3176}
3177
Reid Spencer61c83e02006-08-18 08:43:06 +00003178void llvm::GenerateError(const std::string &message, int LineNo) {
3179 if (LineNo == -1) LineNo = llvmAsmlineno;
3180 // TODO: column number in exception
3181 if (TheParseError)
3182 TheParseError->setError(CurFilename, message, LineNo);
3183 TriggerError = 1;
3184}
3185
Chris Lattner09083092001-07-08 04:57:15 +00003186int yyerror(const char *ErrorMsg) {
Chris Lattner9232b992003-04-22 18:42:41 +00003187 std::string where
3188 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00003189 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Reid Spenceref9b9a72007-02-05 20:47:22 +00003190 std::string errMsg = where + "error: " + std::string(ErrorMsg);
3191 if (yychar != YYEMPTY && yychar != 0)
3192 errMsg += " while reading token: '" + std::string(llvmAsmtext, llvmAsmleng)+
3193 "'";
Reid Spencer61c83e02006-08-18 08:43:06 +00003194 GenerateError(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00003195 return 0;
3196}