blob: af8e3afee3f6213abce006c314634c24dbac03af [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//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// 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>
Chris Lattner00950542001-06-06 20:29:01 +000032
Reid Spencere4f47592006-08-18 17:32:55 +000033// The following is a gross hack. In order to rid the libAsmParser library of
34// exceptions, we have to have a way of getting the yyparse function to go into
35// an error situation. So, whenever we want an error to occur, the GenerateError
Eric Christopher2a5196f2008-09-24 04:55:49 +000036// function (see bottom of file) sets TriggerError. Then, at the end of each
37// production in the grammer we use CHECK_FOR_ERROR which will invoke YYERROR
38// (a goto) to put YACC in error state. Furthermore, several calls to
Reid Spencere4f47592006-08-18 17:32:55 +000039// GenerateError are made from inside productions and they must simulate the
40// previous exception behavior by exiting the production immediately. We have
41// replaced these with the GEN_ERROR macro which calls GeneratError and then
Eric Christopher2a5196f2008-09-24 04:55:49 +000042// immediately invokes YYERROR. This would be so much cleaner if it was a
Reid Spencere4f47592006-08-18 17:32:55 +000043// recursive descent parser.
Reid Spencer61c83e02006-08-18 08:43:06 +000044static bool TriggerError = false;
Reid Spencerf63697d2006-10-09 17:36:59 +000045#define CHECK_FOR_ERROR { if (TriggerError) { TriggerError = false; YYABORT; } }
Reid Spencer61c83e02006-08-18 08:43:06 +000046#define GEN_ERROR(msg) { GenerateError(msg); YYERROR; }
47
Chris Lattner386a3b72001-10-16 19:54:17 +000048int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
Chris Lattner09083092001-07-08 04:57:15 +000049int yylex(); // declaration" of xxx warnings.
Chris Lattner00950542001-06-06 20:29:01 +000050int yyparse();
Chris Lattner86427632004-07-14 06:39:48 +000051using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000052
Chris Lattner00950542001-06-06 20:29:01 +000053static Module *ParserResult;
Chris Lattner00950542001-06-06 20:29:01 +000054
Chris Lattner30c89792001-09-07 16:35:17 +000055// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
56// relating to upreferences in the input stream.
57//
58//#define DEBUG_UPREFS 1
59#ifdef DEBUG_UPREFS
Bill Wendlinge8156192006-12-07 01:30:32 +000060#define UR_OUT(X) cerr << X
Chris Lattner30c89792001-09-07 16:35:17 +000061#else
62#define UR_OUT(X)
63#endif
64
Vikram S. Adved3f7eb02002-07-14 22:59:28 +000065#define YYERROR_VERBOSE 1
66
Chris Lattnerb2b96672005-11-12 18:21:21 +000067static GlobalVariable *CurGV;
Andrew Lenharth558bc882005-06-18 18:34:52 +000068
69
Chris Lattner7e708292002-06-25 16:13:24 +000070// This contains info used when building the body of a function. It is
71// destroyed when the function is completed.
Chris Lattner00950542001-06-06 20:29:01 +000072//
Chris Lattner9232b992003-04-22 18:42:41 +000073typedef std::vector<Value *> ValueList; // Numbered defs
Reid Spencere1553cc2006-12-31 05:40:12 +000074
Eric Christopher2a5196f2008-09-24 04:55:49 +000075static void
Reid Spencer186a43f2007-03-19 18:39:36 +000076ResolveDefinitions(ValueList &LateResolvers, ValueList *FutureLateResolvers=0);
Chris Lattner00950542001-06-06 20:29:01 +000077
78static struct PerModuleInfo {
79 Module *CurrentModule;
Reid Spencer186a43f2007-03-19 18:39:36 +000080 ValueList Values; // Module level numbered definitions
81 ValueList LateResolveValues;
Reid Spencerb78b9082006-11-28 07:28:14 +000082 std::vector<PATypeHolder> Types;
83 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner00950542001-06-06 20:29:01 +000084
Chris Lattnerbc721ed2004-07-13 08:28:21 +000085 /// PlaceHolderInfo - When temporary placeholder objects are created, remember
Reid Spencerfd20c0a2006-05-29 02:34:34 +000086 /// how they were referenced and on which line of the input they came from so
Chris Lattner22ae2322004-07-13 08:39:15 +000087 /// that we can resolve them later and print error messages as appropriate.
Chris Lattnerbc721ed2004-07-13 08:28:21 +000088 std::map<Value*, std::pair<ValID, int> > PlaceHolderInfo;
89
Chris Lattner2079fde2001-10-13 06:41:08 +000090 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
91 // references to global values. Global values may be referenced before they
92 // are defined, and if so, the temporary object that they represent is held
Reid Spencer3271ed52004-07-18 00:08:11 +000093 // here. This is used for forward references of GlobalValues.
Chris Lattner2079fde2001-10-13 06:41:08 +000094 //
Chris Lattner9232b992003-04-22 18:42:41 +000095 typedef std::map<std::pair<const PointerType *,
Chris Lattnerbc207592004-03-08 06:09:57 +000096 ValID>, GlobalValue*> GlobalRefsType;
Chris Lattner2079fde2001-10-13 06:41:08 +000097 GlobalRefsType GlobalRefs;
98
Chris Lattner00950542001-06-06 20:29:01 +000099 void ModuleDone() {
Chris Lattner7e708292002-06-25 16:13:24 +0000100 // If we could not resolve some functions at function compilation time
101 // (calls to functions before they are defined), resolve them now... Types
102 // are resolved when the constant pool has been completely parsed.
Chris Lattner30c89792001-09-07 16:35:17 +0000103 //
Chris Lattner00950542001-06-06 20:29:01 +0000104 ResolveDefinitions(LateResolveValues);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000105 if (TriggerError)
106 return;
Chris Lattner00950542001-06-06 20:29:01 +0000107
Chris Lattner2079fde2001-10-13 06:41:08 +0000108 // Check to make sure that all global value forward references have been
109 // resolved!
110 //
111 if (!GlobalRefs.empty()) {
Chris Lattner9232b992003-04-22 18:42:41 +0000112 std::string UndefinedReferences = "Unresolved global references exist:\n";
Misha Brukman5a2a3822005-05-10 22:02:28 +0000113
Chris Lattner749ce032002-03-11 22:12:39 +0000114 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
115 I != E; ++I) {
116 UndefinedReferences += " " + I->first.first->getDescription() + " " +
117 I->first.second.getName() + "\n";
118 }
Reid Spencer61c83e02006-08-18 08:43:06 +0000119 GenerateError(UndefinedReferences);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000120 return;
Chris Lattner2079fde2001-10-13 06:41:08 +0000121 }
122
Chandler Carruth69940402007-08-04 01:51:18 +0000123 // Look for intrinsic functions and CallInst that need to be upgraded
124 for (Module::iterator FI = CurrentModule->begin(),
125 FE = CurrentModule->end(); FI != FE; )
126 UpgradeCallsToIntrinsic(FI++); // must be post-increment, as we remove
127
Chris Lattner7e708292002-06-25 16:13:24 +0000128 Values.clear(); // Clear out function local definitions
Chris Lattner30c89792001-09-07 16:35:17 +0000129 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +0000130 CurrentModule = 0;
131 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000132
Chris Lattner8a327842004-07-14 21:44:00 +0000133 // GetForwardRefForGlobal - Check to see if there is a forward reference
134 // for this global. If so, remove it from the GlobalRefs map and return it.
135 // If not, just return null.
Reid Spencer863dd882007-04-28 16:06:50 +0000136 GlobalValue *GetForwardRefForGlobal(const PointerType *PTy, ValID ID) {
Chris Lattner8a327842004-07-14 21:44:00 +0000137 // Check to see if there is a forward reference to this global variable...
138 // if there is, eliminate it and patch the reference to use the new def'n.
139 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(PTy, ID));
140 GlobalValue *Ret = 0;
141 if (I != GlobalRefs.end()) {
142 Ret = I->second;
Nuno Lopesf2bd6502008-10-15 12:04:36 +0000143 I->first.second.destroy();
Reid Spencer863dd882007-04-28 16:06:50 +0000144 GlobalRefs.erase(I);
Chris Lattner8a327842004-07-14 21:44:00 +0000145 }
146 return Ret;
147 }
Reid Spencer98b3c5c2007-01-02 21:53:43 +0000148
149 bool TypeIsUnresolved(PATypeHolder* PATy) {
150 // If it isn't abstract, its resolved
151 const Type* Ty = PATy->get();
152 if (!Ty->isAbstract())
153 return false;
154 // Traverse the type looking for abstract types. If it isn't abstract then
Eric Christopher2a5196f2008-09-24 04:55:49 +0000155 // we don't need to traverse that leg of the type.
Reid Spencer98b3c5c2007-01-02 21:53:43 +0000156 std::vector<const Type*> WorkList, SeenList;
157 WorkList.push_back(Ty);
158 while (!WorkList.empty()) {
159 const Type* Ty = WorkList.back();
160 SeenList.push_back(Ty);
161 WorkList.pop_back();
162 if (const OpaqueType* OpTy = dyn_cast<OpaqueType>(Ty)) {
163 // Check to see if this is an unresolved type
164 std::map<ValID, PATypeHolder>::iterator I = LateResolveTypes.begin();
165 std::map<ValID, PATypeHolder>::iterator E = LateResolveTypes.end();
166 for ( ; I != E; ++I) {
167 if (I->second.get() == OpTy)
168 return true;
169 }
170 } else if (const SequentialType* SeqTy = dyn_cast<SequentialType>(Ty)) {
171 const Type* TheTy = SeqTy->getElementType();
172 if (TheTy->isAbstract() && TheTy != Ty) {
Eric Christopher2a5196f2008-09-24 04:55:49 +0000173 std::vector<const Type*>::iterator I = SeenList.begin(),
Reid Spencer98b3c5c2007-01-02 21:53:43 +0000174 E = SeenList.end();
175 for ( ; I != E; ++I)
176 if (*I == TheTy)
177 break;
178 if (I == E)
179 WorkList.push_back(TheTy);
180 }
181 } else if (const StructType* StrTy = dyn_cast<StructType>(Ty)) {
182 for (unsigned i = 0; i < StrTy->getNumElements(); ++i) {
183 const Type* TheTy = StrTy->getElementType(i);
184 if (TheTy->isAbstract() && TheTy != Ty) {
Eric Christopher2a5196f2008-09-24 04:55:49 +0000185 std::vector<const Type*>::iterator I = SeenList.begin(),
Reid Spencer98b3c5c2007-01-02 21:53:43 +0000186 E = SeenList.end();
187 for ( ; I != E; ++I)
188 if (*I == TheTy)
189 break;
190 if (I == E)
191 WorkList.push_back(TheTy);
192 }
193 }
194 }
195 }
196 return false;
197 }
Chris Lattner00950542001-06-06 20:29:01 +0000198} CurModule;
199
Chris Lattner79df7c02002-03-26 18:01:55 +0000200static struct PerFunctionInfo {
Chris Lattner7e708292002-06-25 16:13:24 +0000201 Function *CurrentFunction; // Pointer to current function being created
Chris Lattner00950542001-06-06 20:29:01 +0000202
Reid Spencer186a43f2007-03-19 18:39:36 +0000203 ValueList Values; // Keep track of #'d definitions
204 unsigned NextValNum;
205 ValueList LateResolveValues;
Reid Spenceref9b9a72007-02-05 20:47:22 +0000206 bool isDeclare; // Is this function a forward declararation?
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000207 GlobalValue::LinkageTypes Linkage; // Linkage for forward declaration.
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000208 GlobalValue::VisibilityTypes Visibility;
Chris Lattner00950542001-06-06 20:29:01 +0000209
Chris Lattner2e2ed292004-07-14 06:28:35 +0000210 /// BBForwardRefs - When we see forward references to basic blocks, keep
211 /// track of them here.
Reid Spencer186a43f2007-03-19 18:39:36 +0000212 std::map<ValID, BasicBlock*> BBForwardRefs;
Chris Lattner2e2ed292004-07-14 06:28:35 +0000213
Chris Lattner79df7c02002-03-26 18:01:55 +0000214 inline PerFunctionInfo() {
215 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000216 isDeclare = false;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000217 Linkage = GlobalValue::ExternalLinkage;
218 Visibility = GlobalValue::DefaultVisibility;
Chris Lattner00950542001-06-06 20:29:01 +0000219 }
220
Chris Lattner79df7c02002-03-26 18:01:55 +0000221 inline void FunctionStart(Function *M) {
222 CurrentFunction = M;
Reid Spencer186a43f2007-03-19 18:39:36 +0000223 NextValNum = 0;
Chris Lattner00950542001-06-06 20:29:01 +0000224 }
225
Chris Lattner79df7c02002-03-26 18:01:55 +0000226 void FunctionDone() {
Chris Lattner2e2ed292004-07-14 06:28:35 +0000227 // Any forward referenced blocks left?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000228 if (!BBForwardRefs.empty()) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000229 GenerateError("Undefined reference to label " +
Reid Spencer186a43f2007-03-19 18:39:36 +0000230 BBForwardRefs.begin()->second->getName());
Reid Spencer5b7e7532006-09-28 19:28:24 +0000231 return;
232 }
Chris Lattner2e2ed292004-07-14 06:28:35 +0000233
234 // Resolve all forward references now.
Chris Lattner386a3b72001-10-16 19:54:17 +0000235 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000236
Chris Lattner7e708292002-06-25 16:13:24 +0000237 Values.clear(); // Clear out function local definitions
Reid Spencer186a43f2007-03-19 18:39:36 +0000238 BBForwardRefs.clear();
Chris Lattner79df7c02002-03-26 18:01:55 +0000239 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000240 isDeclare = false;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000241 Linkage = GlobalValue::ExternalLinkage;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000242 Visibility = GlobalValue::DefaultVisibility;
Chris Lattner00950542001-06-06 20:29:01 +0000243 }
Chris Lattner394f2eb2003-10-16 20:12:13 +0000244} CurFun; // Info for the current function...
Chris Lattner00950542001-06-06 20:29:01 +0000245
Chris Lattner394f2eb2003-10-16 20:12:13 +0000246static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000247
Chris Lattner00950542001-06-06 20:29:01 +0000248
249//===----------------------------------------------------------------------===//
250// Code to handle definitions of all the types
251//===----------------------------------------------------------------------===//
252
Chris Lattner6ac28092008-08-29 17:12:13 +0000253/// InsertValue - Insert a value into the value table. If it is named, this
254/// returns -1, otherwise it returns the slot number for the value.
255static int InsertValue(Value *V, ValueList &ValueTab = CurFun.Values) {
Reid Spencer186a43f2007-03-19 18:39:36 +0000256 // Things that have names or are void typed don't get slot numbers
257 if (V->hasName() || (V->getType() == Type::VoidTy))
Chris Lattner6ac28092008-08-29 17:12:13 +0000258 return -1;
Chris Lattner2079fde2001-10-13 06:41:08 +0000259
Reid Spencer186a43f2007-03-19 18:39:36 +0000260 // In the case of function values, we have to allow for the forward reference
261 // of basic blocks, which are included in the numbering. Consequently, we keep
Eric Christopher2a5196f2008-09-24 04:55:49 +0000262 // track of the next insertion location with NextValNum. When a BB gets
Reid Spencer186a43f2007-03-19 18:39:36 +0000263 // inserted, it could change the size of the CurFun.Values vector.
264 if (&ValueTab == &CurFun.Values) {
265 if (ValueTab.size() <= CurFun.NextValNum)
266 ValueTab.resize(CurFun.NextValNum+1);
267 ValueTab[CurFun.NextValNum++] = V;
Chris Lattner6ac28092008-08-29 17:12:13 +0000268 return CurFun.NextValNum-1;
Eric Christopher2a5196f2008-09-24 04:55:49 +0000269 }
Reid Spencer186a43f2007-03-19 18:39:36 +0000270 // For all other lists, its okay to just tack it on the back of the vector.
271 ValueTab.push_back(V);
Chris Lattner6ac28092008-08-29 17:12:13 +0000272 return ValueTab.size()-1;
Chris Lattner00950542001-06-06 20:29:01 +0000273}
274
Chris Lattner30c89792001-09-07 16:35:17 +0000275static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000276 switch (D.Type) {
Reid Spencerb2d17862007-01-26 08:04:51 +0000277 case ValID::LocalID: // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000278 // Module constants occupy the lowest numbered slots...
Reid Spencerb2d17862007-01-26 08:04:51 +0000279 if (D.Num < CurModule.Types.size())
280 return CurModule.Types[D.Num];
Chris Lattner42c9e772001-10-20 09:32:59 +0000281 break;
Reid Spencerb2d17862007-01-26 08:04:51 +0000282 case ValID::LocalName: // Is it a named definition?
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000283 if (const Type *N = CurModule.CurrentModule->getTypeByName(D.getName())) {
Chris Lattnera09000d2004-07-14 23:03:46 +0000284 D.destroy(); // Free old strdup'd memory...
285 return N;
Chris Lattner6e6026b2002-11-20 18:36:02 +0000286 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000287 break;
Chris Lattner30c89792001-09-07 16:35:17 +0000288 default:
Reid Spencerf4fa5902007-02-05 10:16:10 +0000289 GenerateError("Internal parser error: Invalid symbol type reference");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000290 return 0;
Chris Lattner30c89792001-09-07 16:35:17 +0000291 }
292
293 // If we reached here, we referenced either a symbol that we don't know about
294 // or an id number that hasn't been read yet. We may be referencing something
295 // forward, so just create an entry to be resolved later and get to it...
296 //
297 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
298
Chris Lattner355ad1f2005-03-21 06:27:42 +0000299
300 if (inFunctionScope()) {
Reid Spencerb2d17862007-01-26 08:04:51 +0000301 if (D.Type == ValID::LocalName) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000302 GenerateError("Reference to an undefined type: '" + D.getName() + "'");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000303 return 0;
304 } else {
Reid Spencerb2d17862007-01-26 08:04:51 +0000305 GenerateError("Reference to an undefined type: #" + utostr(D.Num));
Reid Spencer5b7e7532006-09-28 19:28:24 +0000306 return 0;
307 }
Chris Lattner4a42e902001-10-22 05:56:09 +0000308 }
Chris Lattner30c89792001-09-07 16:35:17 +0000309
Reid Spencerb78b9082006-11-28 07:28:14 +0000310 std::map<ValID, PATypeHolder>::iterator I =CurModule.LateResolveTypes.find(D);
Nuno Lopesee87b3b2008-10-15 11:19:34 +0000311 if (I != CurModule.LateResolveTypes.end()) {
312 D.destroy();
Reid Spencerb78b9082006-11-28 07:28:14 +0000313 return I->second;
Nuno Lopesee87b3b2008-10-15 11:19:34 +0000314 }
Chris Lattner355ad1f2005-03-21 06:27:42 +0000315
Reid Spencerb78b9082006-11-28 07:28:14 +0000316 Type *Typ = OpaqueType::get();
317 CurModule.LateResolveTypes.insert(std::make_pair(D, Typ));
318 return Typ;
Reid Spencer08de34b2006-12-03 05:45:44 +0000319 }
Chris Lattner30c89792001-09-07 16:35:17 +0000320
Reid Spencer186a43f2007-03-19 18:39:36 +0000321// getExistingVal - Look up the value specified by the provided type and
Chris Lattner2079fde2001-10-13 06:41:08 +0000322// the provided ValID. If the value exists and has already been defined, return
323// it. Otherwise return null.
324//
Reid Spencer186a43f2007-03-19 18:39:36 +0000325static Value *getExistingVal(const Type *Ty, const ValID &D) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000326 if (isa<FunctionType>(Ty)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000327 GenerateError("Functions are not values and "
Chris Lattner79df7c02002-03-26 18:01:55 +0000328 "must be referenced as pointers");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000329 return 0;
330 }
Chris Lattner386a3b72001-10-16 19:54:17 +0000331
Chris Lattner30c89792001-09-07 16:35:17 +0000332 switch (D.Type) {
Reid Spencerb2d17862007-01-26 08:04:51 +0000333 case ValID::LocalID: { // Is it a numbered definition?
Reid Spencerb2d17862007-01-26 08:04:51 +0000334 // Check that the number is within bounds.
Eric Christopher2a5196f2008-09-24 04:55:49 +0000335 if (D.Num >= CurFun.Values.size())
Reid Spencer186a43f2007-03-19 18:39:36 +0000336 return 0;
337 Value *Result = CurFun.Values[D.Num];
338 if (Ty != Result->getType()) {
339 GenerateError("Numbered value (%" + utostr(D.Num) + ") of type '" +
Eric Christopher2a5196f2008-09-24 04:55:49 +0000340 Result->getType()->getDescription() + "' does not match "
Reid Spencer186a43f2007-03-19 18:39:36 +0000341 "expected type, '" + Ty->getDescription() + "'");
342 return 0;
343 }
344 return Result;
Reid Spencerb2d17862007-01-26 08:04:51 +0000345 }
346 case ValID::GlobalID: { // Is it a numbered definition?
Eric Christopher2a5196f2008-09-24 04:55:49 +0000347 if (D.Num >= CurModule.Values.size())
Reid Spenceref9b9a72007-02-05 20:47:22 +0000348 return 0;
Reid Spencer186a43f2007-03-19 18:39:36 +0000349 Value *Result = CurModule.Values[D.Num];
350 if (Ty != Result->getType()) {
351 GenerateError("Numbered value (@" + utostr(D.Num) + ") of type '" +
Eric Christopher2a5196f2008-09-24 04:55:49 +0000352 Result->getType()->getDescription() + "' does not match "
Reid Spencer186a43f2007-03-19 18:39:36 +0000353 "expected type, '" + Ty->getDescription() + "'");
Reid Spenceref9b9a72007-02-05 20:47:22 +0000354 return 0;
Reid Spencer186a43f2007-03-19 18:39:36 +0000355 }
356 return Result;
Chris Lattner00950542001-06-06 20:29:01 +0000357 }
Eric Christopher2a5196f2008-09-24 04:55:49 +0000358
Reid Spencerb2d17862007-01-26 08:04:51 +0000359 case ValID::LocalName: { // Is it a named definition?
Eric Christopher2a5196f2008-09-24 04:55:49 +0000360 if (!inFunctionScope())
Reid Spenceref9b9a72007-02-05 20:47:22 +0000361 return 0;
362 ValueSymbolTable &SymTab = CurFun.CurrentFunction->getValueSymbolTable();
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000363 Value *N = SymTab.lookup(D.getName());
Eric Christopher2a5196f2008-09-24 04:55:49 +0000364 if (N == 0)
Reid Spenceref9b9a72007-02-05 20:47:22 +0000365 return 0;
366 if (N->getType() != Ty)
367 return 0;
Eric Christopher2a5196f2008-09-24 04:55:49 +0000368
Reid Spencerb2d17862007-01-26 08:04:51 +0000369 D.destroy(); // Free old strdup'd memory...
370 return N;
371 }
372 case ValID::GlobalName: { // Is it a named definition?
Reid Spenceref9b9a72007-02-05 20:47:22 +0000373 ValueSymbolTable &SymTab = CurModule.CurrentModule->getValueSymbolTable();
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000374 Value *N = SymTab.lookup(D.getName());
Eric Christopher2a5196f2008-09-24 04:55:49 +0000375 if (N == 0)
Reid Spenceref9b9a72007-02-05 20:47:22 +0000376 return 0;
377 if (N->getType() != Ty)
378 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000379
380 D.destroy(); // Free old strdup'd memory...
381 return N;
382 }
383
Misha Brukman5a2a3822005-05-10 22:02:28 +0000384 // Check to make sure that "Ty" is an integral type, and that our
Chris Lattner2079fde2001-10-13 06:41:08 +0000385 // value will fit into the specified type...
386 case ValID::ConstSIntVal: // Is it a constant pool reference??
Chris Lattner689e8b22008-02-19 04:36:07 +0000387 if (!isa<IntegerType>(Ty) ||
388 !ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000389 GenerateError("Signed integral constant '" +
Misha Brukman5a2a3822005-05-10 22:02:28 +0000390 itostr(D.ConstPool64) + "' is invalid for type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +0000391 Ty->getDescription() + "'");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000392 return 0;
393 }
Reid Spencereac65742007-03-19 20:40:22 +0000394 return ConstantInt::get(Ty, D.ConstPool64, true);
Chris Lattner2079fde2001-10-13 06:41:08 +0000395
396 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattner689e8b22008-02-19 04:36:07 +0000397 if (isa<IntegerType>(Ty) &&
398 ConstantInt::isValueValidForType(Ty, D.UConstPool64))
Reid Spencerb83eb642006-10-20 07:07:24 +0000399 return ConstantInt::get(Ty, D.UConstPool64);
Chris Lattner689e8b22008-02-19 04:36:07 +0000400
401 if (!isa<IntegerType>(Ty) ||
402 !ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
403 GenerateError("Integral constant '" + utostr(D.UConstPool64) +
404 "' is invalid or out of range for type '" +
405 Ty->getDescription() + "'");
406 return 0;
Chris Lattner2079fde2001-10-13 06:41:08 +0000407 }
Chris Lattner689e8b22008-02-19 04:36:07 +0000408 // This is really a signed reference. Transmogrify.
409 return ConstantInt::get(Ty, D.ConstPool64, true);
Chris Lattner2079fde2001-10-13 06:41:08 +0000410
Chris Lattnerd1599012008-07-11 00:30:06 +0000411 case ValID::ConstAPInt: // Is it an unsigned const pool reference?
412 if (!isa<IntegerType>(Ty)) {
413 GenerateError("Integral constant '" + D.getName() +
414 "' is invalid or out of range for type '" +
415 Ty->getDescription() + "'");
416 return 0;
417 }
Eric Christopher2a5196f2008-09-24 04:55:49 +0000418
Chris Lattnerd1599012008-07-11 00:30:06 +0000419 {
420 APSInt Tmp = *D.ConstPoolInt;
Nuno Lopes91c26202008-11-04 14:26:58 +0000421 D.destroy();
Chris Lattnerd1599012008-07-11 00:30:06 +0000422 Tmp.extOrTrunc(Ty->getPrimitiveSizeInBits());
423 return ConstantInt::get(Tmp);
424 }
Eric Christopher2a5196f2008-09-24 04:55:49 +0000425
Chris Lattner2079fde2001-10-13 06:41:08 +0000426 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattner689e8b22008-02-19 04:36:07 +0000427 if (!Ty->isFloatingPoint() ||
428 !ConstantFP::isValueValidForType(Ty, *D.ConstPoolFP)) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000429 GenerateError("FP constant invalid for type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000430 return 0;
431 }
Eric Christopher2a5196f2008-09-24 04:55:49 +0000432 // Lexer has no type info, so builds all float and double FP constants
Dale Johannesen3f6eb742007-09-11 18:32:33 +0000433 // as double. Fix this here. Long double does not need this.
434 if (&D.ConstPoolFP->getSemantics() == &APFloat::IEEEdouble &&
Dale Johannesen23a98552008-10-09 23:00:39 +0000435 Ty==Type::FloatTy) {
436 bool ignored;
437 D.ConstPoolFP->convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven,
438 &ignored);
439 }
Nuno Lopesb0c53da2008-11-04 14:42:19 +0000440 {
441 ConstantFP *tmp = ConstantFP::get(*D.ConstPoolFP);
442 D.destroy();
443 return tmp;
444 }
Misha Brukman5a2a3822005-05-10 22:02:28 +0000445
Chris Lattner2079fde2001-10-13 06:41:08 +0000446 case ValID::ConstNullVal: // Is it a null value?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000447 if (!isa<PointerType>(Ty)) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000448 GenerateError("Cannot create a a non pointer null");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000449 return 0;
450 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000451 return ConstantPointerNull::get(cast<PointerType>(Ty));
Misha Brukman5a2a3822005-05-10 22:02:28 +0000452
Chris Lattner16710e92004-10-16 18:17:13 +0000453 case ValID::ConstUndefVal: // Is it an undef value?
454 return UndefValue::get(Ty);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000455
Chris Lattnerf1f03df2005-12-21 17:53:02 +0000456 case ValID::ConstZeroVal: // Is it a zero value?
457 return Constant::getNullValue(Ty);
Eric Christopher2a5196f2008-09-24 04:55:49 +0000458
Chris Lattnerd78700d2002-08-16 21:14:40 +0000459 case ValID::ConstantVal: // Fully resolved constant?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000460 if (D.ConstantValue->getType() != Ty) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000461 GenerateError("Constant expression type different from required type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000462 return 0;
463 }
Chris Lattnerd78700d2002-08-16 21:14:40 +0000464 return D.ConstantValue;
465
Chris Lattneraa2c8532006-01-25 22:26:43 +0000466 case ValID::InlineAsmVal: { // Inline asm expression
467 const PointerType *PTy = dyn_cast<PointerType>(Ty);
468 const FunctionType *FTy =
469 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
Reid Spencer5b7e7532006-09-28 19:28:24 +0000470 if (!FTy || !InlineAsm::Verify(FTy, D.IAD->Constraints)) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000471 GenerateError("Invalid type for asm constraint string");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000472 return 0;
473 }
Chris Lattneraa2c8532006-01-25 22:26:43 +0000474 InlineAsm *IA = InlineAsm::get(FTy, D.IAD->AsmString, D.IAD->Constraints,
475 D.IAD->HasSideEffects);
476 D.destroy(); // Free InlineAsmDescriptor.
477 return IA;
478 }
Chris Lattner30c89792001-09-07 16:35:17 +0000479 default:
Reid Spencer1755a9a2007-02-05 17:01:20 +0000480 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000481 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000482 } // End of switch
483
Reid Spencer1755a9a2007-02-05 17:01:20 +0000484 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000485 return 0;
486}
487
Reid Spencer186a43f2007-03-19 18:39:36 +0000488// getVal - This function is identical to getExistingVal, except that if a
Chris Lattner2079fde2001-10-13 06:41:08 +0000489// value is not already defined, it "improvises" by creating a placeholder var
490// that looks and acts just like the requested variable. When the value is
491// defined later, all uses of the placeholder variable are replaced with the
492// real thing.
493//
Chris Lattner64116f92004-07-14 01:33:11 +0000494static Value *getVal(const Type *Ty, const ValID &ID) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000495 if (Ty == Type::LabelTy) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000496 GenerateError("Cannot use a basic block here");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000497 return 0;
498 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000499
Chris Lattner64116f92004-07-14 01:33:11 +0000500 // See if the value has already been defined.
Reid Spencer186a43f2007-03-19 18:39:36 +0000501 Value *V = getExistingVal(Ty, ID);
Chris Lattner2079fde2001-10-13 06:41:08 +0000502 if (V) return V;
Reid Spencer5b7e7532006-09-28 19:28:24 +0000503 if (TriggerError) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000504
Reid Spencer5b7e7532006-09-28 19:28:24 +0000505 if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty)) {
Dan Gohmane4977cf2008-05-23 01:55:30 +0000506 GenerateError("Invalid use of a non-first-class type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000507 return 0;
508 }
Chris Lattner60cd9552005-03-23 01:29:26 +0000509
Chris Lattner00950542001-06-06 20:29:01 +0000510 // If we reached here, we referenced either a symbol that we don't know about
511 // or an id number that hasn't been read yet. We may be referencing something
512 // forward, so just create an entry to be resolved later and get to it...
513 //
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000514 switch (ID.Type) {
515 case ValID::GlobalName:
Reid Spencer3cb4dda2007-04-28 14:13:42 +0000516 case ValID::GlobalID: {
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000517 const PointerType *PTy = dyn_cast<PointerType>(Ty);
518 if (!PTy) {
519 GenerateError("Invalid type for reference to global" );
520 return 0;
521 }
522 const Type* ElTy = PTy->getElementType();
523 if (const FunctionType *FTy = dyn_cast<FunctionType>(ElTy))
Gabor Greif051a9502008-04-06 20:25:17 +0000524 V = Function::Create(FTy, GlobalValue::ExternalLinkage);
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000525 else
Christopher Lambfe63fb92007-12-11 08:59:05 +0000526 V = new GlobalVariable(ElTy, false, GlobalValue::ExternalLinkage, 0, "",
527 (Module*)0, false, PTy->getAddressSpace());
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000528 break;
Reid Spencer3cb4dda2007-04-28 14:13:42 +0000529 }
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000530 default:
531 V = new Argument(Ty);
532 }
Eric Christopher2a5196f2008-09-24 04:55:49 +0000533
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000534 // Remember where this forward reference came from. FIXME, shouldn't we try
535 // to recycle these things??
Chris Lattner64116f92004-07-14 01:33:11 +0000536 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000537 LLLgetLineNo())));
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000538
Chris Lattner79df7c02002-03-26 18:01:55 +0000539 if (inFunctionScope())
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000540 InsertValue(V, CurFun.LateResolveValues);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000541 else
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000542 InsertValue(V, CurModule.LateResolveValues);
543 return V;
Chris Lattner00950542001-06-06 20:29:01 +0000544}
545
Reid Spencer186a43f2007-03-19 18:39:36 +0000546/// defineBBVal - This is a definition of a new basic block with the specified
547/// identifier which must be the same as CurFun.NextValNum, if its numeric.
Nick Lewycky280a6e62008-04-25 16:53:59 +0000548static BasicBlock *defineBBVal(const ValID &ID) {
Reid Spencer1755a9a2007-02-05 17:01:20 +0000549 assert(inFunctionScope() && "Can't get basic block at global scope!");
Chris Lattner64116f92004-07-14 01:33:11 +0000550
Chris Lattner2e2ed292004-07-14 06:28:35 +0000551 BasicBlock *BB = 0;
Chris Lattner64116f92004-07-14 01:33:11 +0000552
Reid Spencer186a43f2007-03-19 18:39:36 +0000553 // First, see if this was forward referenced
Chris Lattner64116f92004-07-14 01:33:11 +0000554
Reid Spencer186a43f2007-03-19 18:39:36 +0000555 std::map<ValID, BasicBlock*>::iterator BBI = CurFun.BBForwardRefs.find(ID);
556 if (BBI != CurFun.BBForwardRefs.end()) {
557 BB = BBI->second;
Chris Lattner2e2ed292004-07-14 06:28:35 +0000558 // The forward declaration could have been inserted anywhere in the
559 // function: insert it into the correct place now.
560 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
561 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
Reid Spencer186a43f2007-03-19 18:39:36 +0000562
Reid Spencer2c9df212007-03-20 01:13:00 +0000563 // We're about to erase the entry, save the key so we can clean it up.
564 ValID Tmp = BBI->first;
565
Reid Spencer186a43f2007-03-19 18:39:36 +0000566 // Erase the forward ref from the map as its no longer "forward"
567 CurFun.BBForwardRefs.erase(ID);
568
Eric Christopher2a5196f2008-09-24 04:55:49 +0000569 // The key has been removed from the map but so we don't want to leave
Reid Spencer2c9df212007-03-20 01:13:00 +0000570 // strdup'd memory around so destroy it too.
571 Tmp.destroy();
572
Reid Spencer186a43f2007-03-19 18:39:36 +0000573 // If its a numbered definition, bump the number and set the BB value.
574 if (ID.Type == ValID::LocalID) {
575 assert(ID.Num == CurFun.NextValNum && "Invalid new block number");
576 InsertValue(BB);
577 }
Eric Christopher2a5196f2008-09-24 04:55:49 +0000578 } else {
579 // We haven't seen this BB before and its first mention is a definition.
Nick Lewyckyfc82fab2008-03-02 02:48:09 +0000580 // Just create it and return it.
581 std::string Name (ID.Type == ValID::LocalName ? ID.getName() : "");
Gabor Greif051a9502008-04-06 20:25:17 +0000582 BB = BasicBlock::Create(Name, CurFun.CurrentFunction);
Nick Lewyckyfc82fab2008-03-02 02:48:09 +0000583 if (ID.Type == ValID::LocalID) {
584 assert(ID.Num == CurFun.NextValNum && "Invalid new block number");
585 InsertValue(BB);
586 }
Chris Lattner2e2ed292004-07-14 06:28:35 +0000587 }
Reid Spencer186a43f2007-03-19 18:39:36 +0000588
Nick Lewyckyfc82fab2008-03-02 02:48:09 +0000589 ID.destroy();
Reid Spencer186a43f2007-03-19 18:39:36 +0000590 return BB;
591}
592
593/// getBBVal - get an existing BB value or create a forward reference for it.
Eric Christopher2a5196f2008-09-24 04:55:49 +0000594///
Reid Spencer186a43f2007-03-19 18:39:36 +0000595static BasicBlock *getBBVal(const ValID &ID) {
596 assert(inFunctionScope() && "Can't get basic block at global scope!");
597
598 BasicBlock *BB = 0;
599
600 std::map<ValID, BasicBlock*>::iterator BBI = CurFun.BBForwardRefs.find(ID);
601 if (BBI != CurFun.BBForwardRefs.end()) {
602 BB = BBI->second;
603 } if (ID.Type == ValID::LocalName) {
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000604 std::string Name = ID.getName();
Reid Spencer186a43f2007-03-19 18:39:36 +0000605 Value *N = CurFun.CurrentFunction->getValueSymbolTable().lookup(Name);
Anton Korobeynikov4aefd6b2008-02-20 12:07:57 +0000606 if (N) {
Reid Spencer186a43f2007-03-19 18:39:36 +0000607 if (N->getType()->getTypeID() == Type::LabelTyID)
608 BB = cast<BasicBlock>(N);
609 else
610 GenerateError("Reference to label '" + Name + "' is actually of type '"+
611 N->getType()->getDescription() + "'");
Anton Korobeynikov4aefd6b2008-02-20 12:07:57 +0000612 }
Reid Spencer186a43f2007-03-19 18:39:36 +0000613 } else if (ID.Type == ValID::LocalID) {
614 if (ID.Num < CurFun.NextValNum && ID.Num < CurFun.Values.size()) {
615 if (CurFun.Values[ID.Num]->getType()->getTypeID() == Type::LabelTyID)
616 BB = cast<BasicBlock>(CurFun.Values[ID.Num]);
617 else
Eric Christopher2a5196f2008-09-24 04:55:49 +0000618 GenerateError("Reference to label '%" + utostr(ID.Num) +
619 "' is actually of type '"+
Reid Spencer186a43f2007-03-19 18:39:36 +0000620 CurFun.Values[ID.Num]->getType()->getDescription() + "'");
621 }
622 } else {
623 GenerateError("Illegal label reference " + ID.getName());
624 return 0;
625 }
626
627 // If its already been defined, return it now.
628 if (BB) {
629 ID.destroy(); // Free strdup'd memory.
630 return BB;
631 }
632
633 // Otherwise, this block has not been seen before, create it.
634 std::string Name;
635 if (ID.Type == ValID::LocalName)
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000636 Name = ID.getName();
Gabor Greif051a9502008-04-06 20:25:17 +0000637 BB = BasicBlock::Create(Name, CurFun.CurrentFunction);
Reid Spencer186a43f2007-03-19 18:39:36 +0000638
639 // Insert it in the forward refs map.
640 CurFun.BBForwardRefs[ID] = BB;
641
Chris Lattner64116f92004-07-14 01:33:11 +0000642 return BB;
643}
644
Chris Lattner00950542001-06-06 20:29:01 +0000645
646//===----------------------------------------------------------------------===//
647// Code to handle forward references in instructions
648//===----------------------------------------------------------------------===//
649//
650// This code handles the late binding needed with statements that reference
651// values not defined yet... for example, a forward branch, or the PHI node for
652// a loop body.
653//
Chris Lattner394f2eb2003-10-16 20:12:13 +0000654// This keeps a table (CurFun.LateResolveValues) of all such forward references
Chris Lattner00950542001-06-06 20:29:01 +0000655// and back patchs after we are done.
656//
657
Misha Brukman5a2a3822005-05-10 22:02:28 +0000658// ResolveDefinitions - If we could not resolve some defs at parsing
659// time (forward branches, phi functions for loops, etc...) resolve the
Chris Lattner00950542001-06-06 20:29:01 +0000660// defs now...
661//
Eric Christopher2a5196f2008-09-24 04:55:49 +0000662static void
Reid Spencer186a43f2007-03-19 18:39:36 +0000663ResolveDefinitions(ValueList &LateResolvers, ValueList *FutureLateResolvers) {
Chris Lattner00950542001-06-06 20:29:01 +0000664 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Reid Spencer186a43f2007-03-19 18:39:36 +0000665 while (!LateResolvers.empty()) {
666 Value *V = LateResolvers.back();
667 LateResolvers.pop_back();
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000668
Reid Spencer186a43f2007-03-19 18:39:36 +0000669 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
670 CurModule.PlaceHolderInfo.find(V);
671 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000672
Reid Spencer186a43f2007-03-19 18:39:36 +0000673 ValID &DID = PHI->second.first;
Chris Lattner00950542001-06-06 20:29:01 +0000674
Reid Spencer186a43f2007-03-19 18:39:36 +0000675 Value *TheRealValue = getExistingVal(V->getType(), DID);
676 if (TriggerError)
677 return;
678 if (TheRealValue) {
679 V->replaceAllUsesWith(TheRealValue);
680 delete V;
681 CurModule.PlaceHolderInfo.erase(PHI);
682 } else if (FutureLateResolvers) {
683 // Functions have their unresolved items forwarded to the module late
684 // resolver table
685 InsertValue(V, *FutureLateResolvers);
686 } else {
687 if (DID.Type == ValID::LocalName || DID.Type == ValID::GlobalName) {
688 GenerateError("Reference to an invalid definition: '" +DID.getName()+
689 "' of type '" + V->getType()->getDescription() + "'",
690 PHI->second.second);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000691 return;
Chris Lattner386a3b72001-10-16 19:54:17 +0000692 } else {
Reid Spencer186a43f2007-03-19 18:39:36 +0000693 GenerateError("Reference to an invalid definition: #" +
694 itostr(DID.Num) + " of type '" +
695 V->getType()->getDescription() + "'",
696 PHI->second.second);
697 return;
Chris Lattner30c89792001-09-07 16:35:17 +0000698 }
Chris Lattner00950542001-06-06 20:29:01 +0000699 }
700 }
Chris Lattner00950542001-06-06 20:29:01 +0000701 LateResolvers.clear();
702}
703
Chris Lattner4a42e902001-10-22 05:56:09 +0000704// ResolveTypeTo - A brand new type was just declared. This means that (if
705// name is not null) things referencing Name can be resolved. Otherwise, things
706// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000707//
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000708static void ResolveTypeTo(std::string *Name, const Type *ToTy) {
Chris Lattner355ad1f2005-03-21 06:27:42 +0000709 ValID D;
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000710 if (Name)
711 D = ValID::createLocalName(*Name);
Eric Christopher2a5196f2008-09-24 04:55:49 +0000712 else
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000713 D = ValID::createLocalID(CurModule.Types.size());
Chris Lattner00950542001-06-06 20:29:01 +0000714
Reid Spencerb78b9082006-11-28 07:28:14 +0000715 std::map<ValID, PATypeHolder>::iterator I =
Chris Lattner355ad1f2005-03-21 06:27:42 +0000716 CurModule.LateResolveTypes.find(D);
717 if (I != CurModule.LateResolveTypes.end()) {
Reid Spencerb78b9082006-11-28 07:28:14 +0000718 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Nuno Lopes0ecaf522008-10-15 11:10:21 +0000719 I->first.destroy();
Chris Lattner355ad1f2005-03-21 06:27:42 +0000720 CurModule.LateResolveTypes.erase(I);
Chris Lattner30c89792001-09-07 16:35:17 +0000721 }
Nuno Lopesaa1aee32008-10-03 15:51:46 +0000722 D.destroy();
Chris Lattner30c89792001-09-07 16:35:17 +0000723}
724
Chris Lattner1781aca2001-09-18 04:00:54 +0000725// setValueName - Set the specified value to the name given. The name may be
726// null potentially, in which case this is a noop. The string passed in is
Chris Lattner8ab406d2004-07-13 07:59:27 +0000727// assumed to be a malloc'd string buffer, and is free'd by this function.
728//
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000729static void setValueName(Value *V, std::string *NameStr) {
Reid Spencerb2d17862007-01-26 08:04:51 +0000730 if (!NameStr) return;
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000731 std::string Name(*NameStr); // Copy string
732 delete NameStr; // Free old string
Chris Lattnerc9aea522004-07-13 08:12:39 +0000733
Reid Spencerb2d17862007-01-26 08:04:51 +0000734 if (V->getType() == Type::VoidTy) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000735 GenerateError("Can't assign name '" + Name+"' to value with void type");
Reid Spencerb2d17862007-01-26 08:04:51 +0000736 return;
Chris Lattner8ab406d2004-07-13 07:59:27 +0000737 }
Reid Spencerb2d17862007-01-26 08:04:51 +0000738
Reid Spencer1755a9a2007-02-05 17:01:20 +0000739 assert(inFunctionScope() && "Must be in function scope!");
Reid Spenceref9b9a72007-02-05 20:47:22 +0000740 ValueSymbolTable &ST = CurFun.CurrentFunction->getValueSymbolTable();
741 if (ST.lookup(Name)) {
Reid Spencerb2d17862007-01-26 08:04:51 +0000742 GenerateError("Redefinition of value '" + Name + "' of type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +0000743 V->getType()->getDescription() + "'");
Reid Spencerb2d17862007-01-26 08:04:51 +0000744 return;
745 }
746
747 // Set the name.
748 V->setName(Name);
Chris Lattner8ab406d2004-07-13 07:59:27 +0000749}
750
Chris Lattnerd88998d2004-07-14 08:23:52 +0000751/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
752/// this is a declaration, otherwise it is a definition.
Chris Lattnerb2b96672005-11-12 18:21:21 +0000753static GlobalVariable *
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000754ParseGlobalVariable(std::string *NameStr,
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000755 GlobalValue::LinkageTypes Linkage,
756 GlobalValue::VisibilityTypes Visibility,
Chris Lattnerb2b96672005-11-12 18:21:21 +0000757 bool isConstantGlobal, const Type *Ty,
Christopher Lambfe63fb92007-12-11 08:59:05 +0000758 Constant *Initializer, bool IsThreadLocal,
759 unsigned AddressSpace = 0) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000760 if (isa<FunctionType>(Ty)) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000761 GenerateError("Cannot declare global vars of function type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000762 return 0;
763 }
Dan Gohman9c531cd2008-05-23 18:23:11 +0000764 if (Ty == Type::LabelTy) {
765 GenerateError("Cannot declare global vars of label type");
766 return 0;
767 }
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000768
Christopher Lambfe63fb92007-12-11 08:59:05 +0000769 const PointerType *PTy = PointerType::get(Ty, AddressSpace);
Chris Lattnera09000d2004-07-14 23:03:46 +0000770
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000771 std::string Name;
772 if (NameStr) {
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000773 Name = *NameStr; // Copy string
774 delete NameStr; // Free old string
Chris Lattnerd88998d2004-07-14 08:23:52 +0000775 }
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000776
Chris Lattner8a327842004-07-14 21:44:00 +0000777 // See if this global value was forward referenced. If so, recycle the
778 // object.
Misha Brukman5a2a3822005-05-10 22:02:28 +0000779 ValID ID;
Chris Lattner8a327842004-07-14 21:44:00 +0000780 if (!Name.empty()) {
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000781 ID = ValID::createGlobalName(Name);
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000782 } else {
Reid Spencer186a43f2007-03-19 18:39:36 +0000783 ID = ValID::createGlobalID(CurModule.Values.size());
Chris Lattner8a327842004-07-14 21:44:00 +0000784 }
785
Reid Spencer863dd882007-04-28 16:06:50 +0000786 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000787 // Move the global to the end of the list, from whereever it was
Chris Lattner8a327842004-07-14 21:44:00 +0000788 // previously inserted.
789 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
790 CurModule.CurrentModule->getGlobalList().remove(GV);
791 CurModule.CurrentModule->getGlobalList().push_back(GV);
792 GV->setInitializer(Initializer);
793 GV->setLinkage(Linkage);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000794 GV->setVisibility(Visibility);
Chris Lattner8a327842004-07-14 21:44:00 +0000795 GV->setConstant(isConstantGlobal);
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +0000796 GV->setThreadLocal(IsThreadLocal);
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000797 InsertValue(GV, CurModule.Values);
Nuno Lopesaa1aee32008-10-03 15:51:46 +0000798 ID.destroy();
Chris Lattnerb2b96672005-11-12 18:21:21 +0000799 return GV;
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000800 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000801
Nuno Lopesaa1aee32008-10-03 15:51:46 +0000802 ID.destroy();
803
Reid Spenceref9b9a72007-02-05 20:47:22 +0000804 // If this global has a name
Chris Lattnera09000d2004-07-14 23:03:46 +0000805 if (!Name.empty()) {
Reid Spenceref9b9a72007-02-05 20:47:22 +0000806 // if the global we're parsing has an initializer (is a definition) and
807 // has external linkage.
808 if (Initializer && Linkage != GlobalValue::InternalLinkage)
809 // If there is already a global with external linkage with this name
810 if (CurModule.CurrentModule->getGlobalVariable(Name, false)) {
811 // If we allow this GVar to get created, it will be renamed in the
812 // symbol table because it conflicts with an existing GVar. We can't
813 // allow redefinition of GVars whose linking indicates that their name
814 // must stay the same. Issue the error.
815 GenerateError("Redefinition of global variable named '" + Name +
816 "' of type '" + Ty->getDescription() + "'");
817 return 0;
818 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000819 }
820
821 // Otherwise there is no existing GV to use, create one now.
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000822 GlobalVariable *GV =
Misha Brukman5a2a3822005-05-10 22:02:28 +0000823 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
Christopher Lambfe63fb92007-12-11 08:59:05 +0000824 CurModule.CurrentModule, IsThreadLocal, AddressSpace);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000825 GV->setVisibility(Visibility);
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000826 InsertValue(GV, CurModule.Values);
Chris Lattnerb2b96672005-11-12 18:21:21 +0000827 return GV;
Chris Lattnerd88998d2004-07-14 08:23:52 +0000828}
Chris Lattner8ab406d2004-07-13 07:59:27 +0000829
Reid Spencer75719bf2004-05-26 21:48:31 +0000830// setTypeName - Set the specified type to the name given. The name may be
831// null potentially, in which case this is a noop. The string passed in is
832// assumed to be a malloc'd string buffer, and is freed by this function.
833//
834// This function returns true if the type has already been defined, but is
835// allowed to be redefined in the specified context. If the name is a new name
836// for the type plane, it is inserted and false is returned.
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000837static bool setTypeName(const Type *T, std::string *NameStr) {
Reid Spencer1755a9a2007-02-05 17:01:20 +0000838 assert(!inFunctionScope() && "Can't give types function-local names!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000839 if (NameStr == 0) return false;
Eric Christopher2a5196f2008-09-24 04:55:49 +0000840
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000841 std::string Name(*NameStr); // Copy string
842 delete NameStr; // Free old string
Reid Spencer75719bf2004-05-26 21:48:31 +0000843
844 // We don't allow assigning names to void type
Reid Spencer5b7e7532006-09-28 19:28:24 +0000845 if (T == Type::VoidTy) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000846 GenerateError("Can't assign name '" + Name + "' to the void type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000847 return false;
848 }
Reid Spencer75719bf2004-05-26 21:48:31 +0000849
Chris Lattnera09000d2004-07-14 23:03:46 +0000850 // Set the type name, checking for conflicts as we do so.
851 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000852
Chris Lattnera09000d2004-07-14 23:03:46 +0000853 if (AlreadyExists) { // Inserting a name that is already defined???
854 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
Reid Spencer1755a9a2007-02-05 17:01:20 +0000855 assert(Existing && "Conflict but no matching type?!");
Chris Lattnera09000d2004-07-14 23:03:46 +0000856
Reid Spencer75719bf2004-05-26 21:48:31 +0000857 // There is only one case where this is allowed: when we are refining an
858 // opaque type. In this case, Existing will be an opaque type.
859 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
860 // We ARE replacing an opaque type!
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000861 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000862 return true;
863 }
864
865 // Otherwise, this is an attempt to redefine a type. That's okay if
866 // the redefinition is identical to the original. This will be so if
867 // Existing and T point to the same Type object. In this one case we
868 // allow the equivalent redefinition.
869 if (Existing == T) return true; // Yes, it's equal.
870
871 // Any other kind of (non-equivalent) redefinition is an error.
Reid Spencer90e2f632007-01-05 21:50:38 +0000872 GenerateError("Redefinition of type named '" + Name + "' of type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +0000873 T->getDescription() + "'");
Reid Spencer75719bf2004-05-26 21:48:31 +0000874 }
875
Reid Spencer75719bf2004-05-26 21:48:31 +0000876 return false;
877}
Chris Lattner8896eda2001-07-09 19:38:36 +0000878
Chris Lattner30c89792001-09-07 16:35:17 +0000879//===----------------------------------------------------------------------===//
880// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000881//
Chris Lattner8896eda2001-07-09 19:38:36 +0000882
Chris Lattner3b073862004-02-09 03:19:29 +0000883// TypeContains - Returns true if Ty directly contains E in it.
Chris Lattner30c89792001-09-07 16:35:17 +0000884//
885static bool TypeContains(const Type *Ty, const Type *E) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000886 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
Chris Lattner6d8dbec2004-12-08 16:13:53 +0000887 E) != Ty->subtype_end();
Chris Lattner3b073862004-02-09 03:19:29 +0000888}
889
890namespace {
891 struct UpRefRecord {
892 // NestingLevel - The number of nesting levels that need to be popped before
893 // this type is resolved.
894 unsigned NestingLevel;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000895
Chris Lattner3b073862004-02-09 03:19:29 +0000896 // LastContainedTy - This is the type at the current binding level for the
897 // type. Every time we reduce the nesting level, this gets updated.
898 const Type *LastContainedTy;
899
900 // UpRefTy - This is the actual opaque type that the upreference is
901 // represented with.
902 OpaqueType *UpRefTy;
903
904 UpRefRecord(unsigned NL, OpaqueType *URTy)
905 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
906 };
Chris Lattner30c89792001-09-07 16:35:17 +0000907}
Chris Lattner698b56e2001-07-20 19:15:08 +0000908
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000909// UpRefs - A list of the outstanding upreferences that need to be resolved.
Chris Lattner3b073862004-02-09 03:19:29 +0000910static std::vector<UpRefRecord> UpRefs;
Chris Lattner30c89792001-09-07 16:35:17 +0000911
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000912/// HandleUpRefs - Every time we finish a new layer of types, this function is
913/// called. It loops through the UpRefs vector, which is a list of the
914/// currently active types. For each type, if the up reference is contained in
915/// the newly completed type, we decrement the level count. When the level
916/// count reaches zero, the upreferenced type is the type that is passed in:
917/// thus we can complete the cycle.
918///
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000919static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner703e92f2006-08-18 17:34:24 +0000920 // If Ty isn't abstract, or if there are no up-references in it, then there is
921 // nothing to resolve here.
922 if (!ty->isAbstract() || UpRefs.empty()) return ty;
Eric Christopher2a5196f2008-09-24 04:55:49 +0000923
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000924 PATypeHolder Ty(ty);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000925 UR_OUT("Type '" << Ty->getDescription() <<
Chris Lattner5084d032001-11-02 07:46:26 +0000926 "' newly formed. Resolving upreferences.\n" <<
927 UpRefs.size() << " upreferences active!\n");
Chris Lattner026a8ce2004-02-09 18:53:54 +0000928
929 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
930 // to zero), we resolve them all together before we resolve them to Ty. At
931 // the end of the loop, if there is anything to resolve to Ty, it will be in
932 // this variable.
933 OpaqueType *TypeToResolve = 0;
934
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000935 for (unsigned i = 0; i != UpRefs.size(); ++i) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000936 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
937 << UpRefs[i].second->getDescription() << ") = "
Reid Spencer3271ed52004-07-18 00:08:11 +0000938 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
Chris Lattner3b073862004-02-09 03:19:29 +0000939 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
940 // Decrement level of upreference
941 unsigned Level = --UpRefs[i].NestingLevel;
942 UpRefs[i].LastContainedTy = Ty;
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000943 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
Misha Brukman5a2a3822005-05-10 22:02:28 +0000944 if (Level == 0) { // Upreference should be resolved!
Chris Lattner026a8ce2004-02-09 18:53:54 +0000945 if (!TypeToResolve) {
946 TypeToResolve = UpRefs[i].UpRefTy;
947 } else {
948 UR_OUT(" * Resolving upreference for "
949 << UpRefs[i].second->getDescription() << "\n";
950 std::string OldName = UpRefs[i].UpRefTy->getDescription());
951 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
952 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
953 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
954 }
Reid Spencer3271ed52004-07-18 00:08:11 +0000955 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000956 --i; // Do not skip the next element...
Chris Lattner30c89792001-09-07 16:35:17 +0000957 }
958 }
Chris Lattner8896eda2001-07-09 19:38:36 +0000959 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000960
Chris Lattner026a8ce2004-02-09 18:53:54 +0000961 if (TypeToResolve) {
962 UR_OUT(" * Resolving upreference for "
963 << UpRefs[i].second->getDescription() << "\n";
Chris Lattner16af11d2004-02-09 21:03:38 +0000964 std::string OldName = TypeToResolve->getDescription());
Chris Lattner026a8ce2004-02-09 18:53:54 +0000965 TypeToResolve->refineAbstractTypeTo(Ty);
966 }
967
Chris Lattner8896eda2001-07-09 19:38:36 +0000968 return Ty;
969}
970
Chris Lattner00950542001-06-06 20:29:01 +0000971//===----------------------------------------------------------------------===//
972// RunVMAsmParser - Define an interface to this parser
973//===----------------------------------------------------------------------===//
974//
Reid Spencere1553cc2006-12-31 05:40:12 +0000975static Module* RunParser(Module * M);
976
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000977Module *llvm::RunVMAsmParser(llvm::MemoryBuffer *MB) {
978 InitLLLexer(MB);
979 Module *M = RunParser(new Module(LLLgetFilename()));
980 FreeLexer();
981 return M;
Chris Lattner00950542001-06-06 20:29:01 +0000982}
983
984%}
985
986%union {
Brian Gaeked0fde302003-11-11 22:41:34 +0000987 llvm::Module *ModuleVal;
988 llvm::Function *FunctionVal;
Brian Gaeked0fde302003-11-11 22:41:34 +0000989 llvm::BasicBlock *BasicBlockVal;
990 llvm::TerminatorInst *TermInstVal;
991 llvm::Instruction *InstVal;
Reid Spencer08de34b2006-12-03 05:45:44 +0000992 llvm::Constant *ConstVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000993
Reid Spencer08de34b2006-12-03 05:45:44 +0000994 const llvm::Type *PrimType;
Reid Spencere1553cc2006-12-31 05:40:12 +0000995 std::list<llvm::PATypeHolder> *TypeList;
Reid Spencer08de34b2006-12-03 05:45:44 +0000996 llvm::PATypeHolder *TypeVal;
997 llvm::Value *ValueVal;
Reid Spencer08de34b2006-12-03 05:45:44 +0000998 std::vector<llvm::Value*> *ValueList;
Dan Gohman81a0c0b2008-05-31 00:58:22 +0000999 std::vector<unsigned> *ConstantList;
Reid Spencere1553cc2006-12-31 05:40:12 +00001000 llvm::ArgListType *ArgList;
1001 llvm::TypeWithAttrs TypeWithAttrs;
1002 llvm::TypeWithAttrsList *TypeWithAttrsList;
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001003 llvm::ParamList *ParamList;
Reid Spencere1553cc2006-12-31 05:40:12 +00001004
Misha Brukman5a2a3822005-05-10 22:02:28 +00001005 // Represent the RHS of PHI node
Reid Spencer08de34b2006-12-03 05:45:44 +00001006 std::list<std::pair<llvm::Value*,
1007 llvm::BasicBlock*> > *PHIList;
Brian Gaeked0fde302003-11-11 22:41:34 +00001008 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
Reid Spencer08de34b2006-12-03 05:45:44 +00001009 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +00001010
Brian Gaeked0fde302003-11-11 22:41:34 +00001011 llvm::GlobalValue::LinkageTypes Linkage;
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001012 llvm::GlobalValue::VisibilityTypes Visibility;
Devang Patel05988662008-09-25 21:00:45 +00001013 llvm::Attributes Attributes;
Reid Spencerf2310042007-02-28 02:23:44 +00001014 llvm::APInt *APIntVal;
Chris Lattner30c89792001-09-07 16:35:17 +00001015 int64_t SInt64Val;
1016 uint64_t UInt64Val;
1017 int SIntVal;
1018 unsigned UIntVal;
Dale Johannesen43421b32007-09-06 18:13:44 +00001019 llvm::APFloat *FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +00001020 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +00001021
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001022 std::string *StrVal; // This memory must be deleted
1023 llvm::ValID ValIDVal;
Chris Lattner00950542001-06-06 20:29:01 +00001024
Reid Spencer08de34b2006-12-03 05:45:44 +00001025 llvm::Instruction::BinaryOps BinaryOpVal;
1026 llvm::Instruction::TermOps TermOpVal;
1027 llvm::Instruction::MemoryOps MemOpVal;
1028 llvm::Instruction::CastOps CastOpVal;
1029 llvm::Instruction::OtherOps OtherOpVal;
Reid Spencer08de34b2006-12-03 05:45:44 +00001030 llvm::ICmpInst::Predicate IPredicate;
1031 llvm::FCmpInst::Predicate FPredicate;
Chris Lattner00950542001-06-06 20:29:01 +00001032}
1033
Eric Christopher2a5196f2008-09-24 04:55:49 +00001034%type <ModuleVal> Module
Chris Lattner79df7c02002-03-26 18:01:55 +00001035%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +00001036%type <BasicBlockVal> BasicBlock InstructionList
1037%type <TermInstVal> BBTerminatorInst
1038%type <InstVal> Inst InstVal MemoryInst
Anton Korobeynikova80e1182007-04-28 13:45:00 +00001039%type <ConstVal> ConstVal ConstExpr AliaseeRef
Chris Lattner6cdb0112001-11-26 16:54:11 +00001040%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +00001041%type <ArgList> ArgList ArgListH
Chris Lattnerc24d2082001-06-11 15:04:20 +00001042%type <PHIList> PHIList
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001043%type <ParamList> ParamList // For call param lists & GEP indices
Reid Spencere1553cc2006-12-31 05:40:12 +00001044%type <ValueList> IndexList // For GEP indices
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001045%type <ConstantList> ConstantIndexList // For insertvalue/extractvalue indices
Eric Christopher2a5196f2008-09-24 04:55:49 +00001046%type <TypeList> TypeListI
Reid Spencere1553cc2006-12-31 05:40:12 +00001047%type <TypeWithAttrsList> ArgTypeList ArgTypeListI
Reid Spencer2c261782007-01-05 17:06:19 +00001048%type <TypeWithAttrs> ArgType
Chris Lattner00950542001-06-06 20:29:01 +00001049%type <JumpTable> JumpTable
Chris Lattner4ad02e72003-04-16 20:28:45 +00001050%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00001051%type <BoolVal> ThreadLocal // 'thread_local' or not
Chris Lattner15c9c032003-09-08 18:20:29 +00001052%type <BoolVal> OptVolatile // 'volatile' or not
Chris Lattnerddb6db42005-05-06 05:51:46 +00001053%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
Chris Lattneraa2c8532006-01-25 22:26:43 +00001054%type <BoolVal> OptSideEffect // 'sideeffect' or not.
Reid Spencere1553cc2006-12-31 05:40:12 +00001055%type <Linkage> GVInternalLinkage GVExternalLinkage
1056%type <Linkage> FunctionDefineLinkage FunctionDeclareLinkage
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001057%type <Linkage> AliasLinkage
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001058%type <Visibility> GVVisibilityStyle
Chris Lattner00950542001-06-06 20:29:01 +00001059
Chris Lattner2079fde2001-10-13 06:41:08 +00001060// ValueRef - Unresolved reference to a definition or BB
1061%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001062%type <ValueVal> ResolvedVal // <type> <valref> pair
Devang Patel5af2f632008-02-20 22:39:45 +00001063%type <ValueList> ReturnedVal
Chris Lattner00950542001-06-06 20:29:01 +00001064// Tokens and types for handling constant integer values
1065//
1066// ESINT64VAL - A negative number within long long range
1067%token <SInt64Val> ESINT64VAL
1068
1069// EUINT64VAL - A positive number within uns. long long range
1070%token <UInt64Val> EUINT64VAL
Chris Lattner00950542001-06-06 20:29:01 +00001071
Eric Christopher2a5196f2008-09-24 04:55:49 +00001072// ESAPINTVAL - A negative number with arbitrary precision
Reid Spencerf2310042007-02-28 02:23:44 +00001073%token <APIntVal> ESAPINTVAL
1074
Eric Christopher2a5196f2008-09-24 04:55:49 +00001075// EUAPINTVAL - A positive number with arbitrary precision
Reid Spencerf2310042007-02-28 02:23:44 +00001076%token <APIntVal> EUAPINTVAL
1077
Reid Spencerb2d17862007-01-26 08:04:51 +00001078%token <UIntVal> LOCALVAL_ID GLOBALVAL_ID // %123 @123
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001079%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +00001080
1081// Built in types...
Reid Spencer2c261782007-01-05 17:06:19 +00001082%type <TypeVal> Types ResultTypes
Chris Lattnerc7e30692008-10-15 06:16:45 +00001083%type <PrimType> PrimType // Classifications
Eric Christopher2a5196f2008-09-24 04:55:49 +00001084%token <PrimType> VOID INTTYPE
Dale Johannesen320fc8a2007-08-03 01:03:46 +00001085%token <PrimType> FLOAT DOUBLE X86_FP80 FP128 PPC_FP128 LABEL
Reid Spencerb951bc02006-12-29 20:29:48 +00001086%token TYPE
Chris Lattner00950542001-06-06 20:29:01 +00001087
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001088
Eric Christopher2a5196f2008-09-24 04:55:49 +00001089%token<StrVal> LOCALVAR GLOBALVAR LABELSTR
Reid Spenceraa7c2f82007-05-19 07:21:26 +00001090%token<StrVal> STRINGCONSTANT ATSTRINGCONSTANT PCTSTRINGCONSTANT
Reid Spencerb2d17862007-01-26 08:04:51 +00001091%type <StrVal> LocalName OptLocalName OptLocalAssign
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001092%type <StrVal> GlobalName OptGlobalAssign GlobalAssign
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00001093%type <StrVal> OptSection SectionString OptGC
Chris Lattner00950542001-06-06 20:29:01 +00001094
Christopher Lambd49e18d2007-12-12 08:44:39 +00001095%type <UIntVal> OptAlign OptCAlign OptAddrSpace
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001096
Reid Spencer744d0362007-04-09 01:55:42 +00001097%token ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001098%token DECLARE DEFINE GLOBAL CONSTANT SECTION ALIAS VOLATILE THREAD_LOCAL
Reid Spencere1553cc2006-12-31 05:40:12 +00001099%token TO DOTDOTDOT NULL_TOK UNDEF INTERNAL LINKONCE WEAK APPENDING
Dale Johannesen40e829f2008-05-14 20:14:09 +00001100%token DLLIMPORT DLLEXPORT EXTERN_WEAK COMMON
Christopher Lambfe63fb92007-12-11 08:59:05 +00001101%token OPAQUE EXTERNAL TARGET TRIPLE ALIGN ADDRSPACE
Chris Lattneraa2c8532006-01-25 22:26:43 +00001102%token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001103%token CC_TOK CCC_TOK FASTCC_TOK COLDCC_TOK X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
Nick Lewycky280a6e62008-04-25 16:53:59 +00001104%token DATALAYOUT
Chris Lattner6ac28092008-08-29 17:12:13 +00001105%type <UIntVal> OptCallingConv LocalNumber
Devang Patel05988662008-09-25 21:00:45 +00001106%type <Attributes> OptAttributes Attribute
1107%type <Attributes> OptFuncAttrs FuncAttr
Devang Patel652203f2008-09-29 20:49:50 +00001108%type <Attributes> OptRetAttrs RetAttr
Chris Lattner00950542001-06-06 20:29:01 +00001109
Misha Brukman5a2a3822005-05-10 22:02:28 +00001110// Basic Block Terminating Operators
Chris Lattner16710e92004-10-16 18:17:13 +00001111%token <TermOpVal> RET BR SWITCH INVOKE UNWIND UNREACHABLE
Chris Lattner00950542001-06-06 20:29:01 +00001112
Misha Brukman5a2a3822005-05-10 22:02:28 +00001113// Binary Operators
Reid Spencere4d87aa2006-12-23 06:05:41 +00001114%type <BinaryOpVal> ArithmeticOps LogicalOps // Binops Subcatagories
Reid Spencer0a783f72006-11-02 01:53:59 +00001115%token <BinaryOpVal> ADD SUB MUL UDIV SDIV FDIV UREM SREM FREM AND OR XOR
Reid Spencer832254e2007-02-02 02:16:23 +00001116%token <BinaryOpVal> SHL LSHR ASHR
1117
Eric Christopher2a5196f2008-09-24 04:55:49 +00001118%token <OtherOpVal> ICMP FCMP VICMP VFCMP
Reid Spencer08de34b2006-12-03 05:45:44 +00001119%type <IPredicate> IPredicates
Reid Spencer08de34b2006-12-03 05:45:44 +00001120%type <FPredicate> FPredicates
Eric Christopher2a5196f2008-09-24 04:55:49 +00001121%token EQ NE SLT SGT SLE SGE ULT UGT ULE UGE
Reid Spencer9f746f42006-12-03 06:58:07 +00001122%token OEQ ONE OLT OGT OLE OGE ORD UNO UEQ UNE
Chris Lattner00950542001-06-06 20:29:01 +00001123
1124// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001125%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +00001126
Reid Spencer3da59db2006-11-27 01:05:10 +00001127// Cast Operators
1128%type <CastOpVal> CastOps
1129%token <CastOpVal> TRUNC ZEXT SEXT FPTRUNC FPEXT BITCAST
1130%token <CastOpVal> UITOFP SITOFP FPTOUI FPTOSI INTTOPTR PTRTOINT
1131
Chris Lattner027dcc52001-07-08 21:10:27 +00001132// Other Operators
Reid Spencer832254e2007-02-02 02:16:23 +00001133%token <OtherOpVal> PHI_TOK SELECT VAARG
Chris Lattner4c949082006-04-08 01:18:35 +00001134%token <OtherOpVal> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
Devang Pateld6ffcf92008-02-19 22:26:37 +00001135%token <OtherOpVal> GETRESULT
Dan Gohmane4977cf2008-05-23 01:55:30 +00001136%token <OtherOpVal> EXTRACTVALUE INSERTVALUE
Chris Lattnerddb6db42005-05-06 05:51:46 +00001137
Reid Spencer2c261782007-01-05 17:06:19 +00001138// Function Attributes
Duncan Sands36397f52007-07-27 12:58:54 +00001139%token SIGNEXT ZEROEXT NORETURN INREG SRET NOUNWIND NOALIAS BYVAL NEST
Bill Wendlinge9e6bdf2008-11-13 01:02:14 +00001140%token READNONE READONLY GC OPTSIZE NOINLINE ALWAYSINLINE SSP SSPREQ
Devang Pateld4980812008-09-02 20:52:40 +00001141
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001142// Visibility Styles
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +00001143%token DEFAULT HIDDEN PROTECTED
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001144
Chris Lattner00950542001-06-06 20:29:01 +00001145%start Module
1146%%
1147
Chris Lattner00950542001-06-06 20:29:01 +00001148
Misha Brukman5a2a3822005-05-10 22:02:28 +00001149// Operations that are notably excluded from this list include:
Chris Lattner00950542001-06-06 20:29:01 +00001150// RET, BR, & SWITCH because they end basic blocks and are treated specially.
1151//
Reid Spencer0a783f72006-11-02 01:53:59 +00001152ArithmeticOps: ADD | SUB | MUL | UDIV | SDIV | FDIV | UREM | SREM | FREM;
Reid Spencer832254e2007-02-02 02:16:23 +00001153LogicalOps : SHL | LSHR | ASHR | AND | OR | XOR;
Eric Christopher2a5196f2008-09-24 04:55:49 +00001154CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | BITCAST |
Reid Spencer3da59db2006-11-27 01:05:10 +00001155 UITOFP | SITOFP | FPTOUI | FPTOSI | INTTOPTR | PTRTOINT;
Reid Spencer832254e2007-02-02 02:16:23 +00001156
Eric Christopher2a5196f2008-09-24 04:55:49 +00001157IPredicates
Reid Spencer763ed5e2006-12-04 05:20:06 +00001158 : EQ { $$ = ICmpInst::ICMP_EQ; } | NE { $$ = ICmpInst::ICMP_NE; }
Reid Spencer9f746f42006-12-03 06:58:07 +00001159 | SLT { $$ = ICmpInst::ICMP_SLT; } | SGT { $$ = ICmpInst::ICMP_SGT; }
1160 | SLE { $$ = ICmpInst::ICMP_SLE; } | SGE { $$ = ICmpInst::ICMP_SGE; }
1161 | ULT { $$ = ICmpInst::ICMP_ULT; } | UGT { $$ = ICmpInst::ICMP_UGT; }
Eric Christopher2a5196f2008-09-24 04:55:49 +00001162 | ULE { $$ = ICmpInst::ICMP_ULE; } | UGE { $$ = ICmpInst::ICMP_UGE; }
Reid Spencer9f746f42006-12-03 06:58:07 +00001163 ;
1164
Eric Christopher2a5196f2008-09-24 04:55:49 +00001165FPredicates
Reid Spencer9f746f42006-12-03 06:58:07 +00001166 : OEQ { $$ = FCmpInst::FCMP_OEQ; } | ONE { $$ = FCmpInst::FCMP_ONE; }
1167 | OLT { $$ = FCmpInst::FCMP_OLT; } | OGT { $$ = FCmpInst::FCMP_OGT; }
1168 | OLE { $$ = FCmpInst::FCMP_OLE; } | OGE { $$ = FCmpInst::FCMP_OGE; }
1169 | ORD { $$ = FCmpInst::FCMP_ORD; } | UNO { $$ = FCmpInst::FCMP_UNO; }
1170 | UEQ { $$ = FCmpInst::FCMP_UEQ; } | UNE { $$ = FCmpInst::FCMP_UNE; }
1171 | ULT { $$ = FCmpInst::FCMP_ULT; } | UGT { $$ = FCmpInst::FCMP_UGT; }
1172 | ULE { $$ = FCmpInst::FCMP_ULE; } | UGE { $$ = FCmpInst::FCMP_UGE; }
1173 | TRUETOK { $$ = FCmpInst::FCMP_TRUE; }
1174 | FALSETOK { $$ = FCmpInst::FCMP_FALSE; }
1175 ;
Chris Lattner00950542001-06-06 20:29:01 +00001176
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001177LocalName : LOCALVAR | STRINGCONSTANT | PCTSTRINGCONSTANT ;
Reid Spencerb2d17862007-01-26 08:04:51 +00001178OptLocalName : LocalName | /*empty*/ { $$ = 0; };
1179
Christopher Lambd49e18d2007-12-12 08:44:39 +00001180OptAddrSpace : ADDRSPACE '(' EUINT64VAL ')' { $$=$3; }
1181 | /*empty*/ { $$=0; };
1182
Reid Spencerb2d17862007-01-26 08:04:51 +00001183/// OptLocalAssign - Value producing statements have an optional assignment
1184/// component.
1185OptLocalAssign : LocalName '=' {
1186 $$ = $1;
1187 CHECK_FOR_ERROR
1188 }
1189 | /*empty*/ {
1190 $$ = 0;
1191 CHECK_FOR_ERROR
1192 };
1193
Chris Lattner6ac28092008-08-29 17:12:13 +00001194LocalNumber : LOCALVAL_ID '=' {
1195 $$ = $1;
1196 CHECK_FOR_ERROR
1197};
1198
1199
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001200GlobalName : GLOBALVAR | ATSTRINGCONSTANT ;
Reid Spencerb2d17862007-01-26 08:04:51 +00001201
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001202OptGlobalAssign : GlobalAssign
Misha Brukman5a2a3822005-05-10 22:02:28 +00001203 | /*empty*/ {
1204 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00001205 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001206 };
Chris Lattner00950542001-06-06 20:29:01 +00001207
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001208GlobalAssign : GlobalName '=' {
1209 $$ = $1;
1210 CHECK_FOR_ERROR
Anton Korobeynikovc0fabcb2007-04-25 18:07:40 +00001211 };
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001212
Eric Christopher2a5196f2008-09-24 04:55:49 +00001213GVInternalLinkage
1214 : INTERNAL { $$ = GlobalValue::InternalLinkage; }
1215 | WEAK { $$ = GlobalValue::WeakLinkage; }
Reid Spencerb951bc02006-12-29 20:29:48 +00001216 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1217 | APPENDING { $$ = GlobalValue::AppendingLinkage; }
Eric Christopher2a5196f2008-09-24 04:55:49 +00001218 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
Dale Johannesen40e829f2008-05-14 20:14:09 +00001219 | COMMON { $$ = GlobalValue::CommonLinkage; }
Reid Spencerb951bc02006-12-29 20:29:48 +00001220 ;
1221
1222GVExternalLinkage
1223 : DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1224 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
1225 | EXTERNAL { $$ = GlobalValue::ExternalLinkage; }
1226 ;
1227
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001228GVVisibilityStyle
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +00001229 : /*empty*/ { $$ = GlobalValue::DefaultVisibility; }
1230 | DEFAULT { $$ = GlobalValue::DefaultVisibility; }
1231 | HIDDEN { $$ = GlobalValue::HiddenVisibility; }
1232 | PROTECTED { $$ = GlobalValue::ProtectedVisibility; }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001233 ;
1234
Reid Spencere1553cc2006-12-31 05:40:12 +00001235FunctionDeclareLinkage
1236 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
Eric Christopher2a5196f2008-09-24 04:55:49 +00001237 | DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
Reid Spencere1553cc2006-12-31 05:40:12 +00001238 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
Reid Spencerb951bc02006-12-29 20:29:48 +00001239 ;
Eric Christopher2a5196f2008-09-24 04:55:49 +00001240
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001241FunctionDefineLinkage
Reid Spencere1553cc2006-12-31 05:40:12 +00001242 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1243 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
Reid Spencerb951bc02006-12-29 20:29:48 +00001244 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1245 | WEAK { $$ = GlobalValue::WeakLinkage; }
Eric Christopher2a5196f2008-09-24 04:55:49 +00001246 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
1247 ;
Chris Lattner30c89792001-09-07 16:35:17 +00001248
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001249AliasLinkage
1250 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1251 | WEAK { $$ = GlobalValue::WeakLinkage; }
1252 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
1253 ;
1254
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00001255OptCallingConv : /*empty*/ { $$ = CallingConv::C; } |
1256 CCC_TOK { $$ = CallingConv::C; } |
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00001257 FASTCC_TOK { $$ = CallingConv::Fast; } |
1258 COLDCC_TOK { $$ = CallingConv::Cold; } |
1259 X86_STDCALLCC_TOK { $$ = CallingConv::X86_StdCall; } |
1260 X86_FASTCALLCC_TOK { $$ = CallingConv::X86_FastCall; } |
1261 CC_TOK EUINT64VAL {
Chris Lattnera8e8f162005-05-06 20:27:19 +00001262 if ((unsigned)$2 != $2)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001263 GEN_ERROR("Calling conv too large");
Chris Lattnera8e8f162005-05-06 20:27:19 +00001264 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001265 CHECK_FOR_ERROR
Chris Lattnera8e8f162005-05-06 20:27:19 +00001266 };
1267
Devang Patel05988662008-09-25 21:00:45 +00001268Attribute : ZEROEXT { $$ = Attribute::ZExt; }
1269 | ZEXT { $$ = Attribute::ZExt; }
1270 | SIGNEXT { $$ = Attribute::SExt; }
1271 | SEXT { $$ = Attribute::SExt; }
1272 | INREG { $$ = Attribute::InReg; }
1273 | SRET { $$ = Attribute::StructRet; }
1274 | NOALIAS { $$ = Attribute::NoAlias; }
1275 | BYVAL { $$ = Attribute::ByVal; }
1276 | NEST { $$ = Attribute::Nest; }
Eric Christopher2a5196f2008-09-24 04:55:49 +00001277 | ALIGN EUINT64VAL { $$ =
Devang Patel05988662008-09-25 21:00:45 +00001278 Attribute::constructAlignmentFromInt($2); }
Reid Spencere1553cc2006-12-31 05:40:12 +00001279 ;
1280
Devang Patel05988662008-09-25 21:00:45 +00001281OptAttributes : /* empty */ { $$ = Attribute::None; }
1282 | OptAttributes Attribute {
Reid Spencer5694b6e2007-04-09 06:17:21 +00001283 $$ = $1 | $2;
Reid Spencere1553cc2006-12-31 05:40:12 +00001284 }
1285 ;
1286
Devang Patel652203f2008-09-29 20:49:50 +00001287RetAttr : INREG { $$ = Attribute::InReg; }
1288 | ZEROEXT { $$ = Attribute::ZExt; }
1289 | SIGNEXT { $$ = Attribute::SExt; }
1290 ;
1291
1292OptRetAttrs : /* empty */ { $$ = Attribute::None; }
1293 | OptRetAttrs RetAttr {
1294 $$ = $1 | $2;
1295 }
1296 ;
1297
1298
Devang Patel05988662008-09-25 21:00:45 +00001299FuncAttr : NORETURN { $$ = Attribute::NoReturn; }
1300 | NOUNWIND { $$ = Attribute::NoUnwind; }
1301 | INREG { $$ = Attribute::InReg; }
1302 | ZEROEXT { $$ = Attribute::ZExt; }
1303 | SIGNEXT { $$ = Attribute::SExt; }
1304 | READNONE { $$ = Attribute::ReadNone; }
1305 | READONLY { $$ = Attribute::ReadOnly; }
Chris Lattnercd168432008-10-08 06:44:36 +00001306 | NOINLINE { $$ = Attribute::NoInline; }
1307 | ALWAYSINLINE { $$ = Attribute::AlwaysInline; }
Bill Wendlinge9e6bdf2008-11-13 01:02:14 +00001308 | OPTSIZE { $$ = Attribute::OptimizeForSize; }
1309 | SSP { $$ = Attribute::StackProtect; }
1310 | SSPREQ { $$ = Attribute::StackProtectReq; }
Reid Spencer2c261782007-01-05 17:06:19 +00001311 ;
1312
Devang Patel05988662008-09-25 21:00:45 +00001313OptFuncAttrs : /* empty */ { $$ = Attribute::None; }
Reid Spencer2c261782007-01-05 17:06:19 +00001314 | OptFuncAttrs FuncAttr {
Reid Spencer5694b6e2007-04-09 06:17:21 +00001315 $$ = $1 | $2;
Reid Spencer2c261782007-01-05 17:06:19 +00001316 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001317 ;
1318
Devang Patel652203f2008-09-29 20:49:50 +00001319
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00001320OptGC : /* empty */ { $$ = 0; }
1321 | GC STRINGCONSTANT {
1322 $$ = $2;
1323 }
1324 ;
1325
Chris Lattner66db8e42005-11-06 06:34:12 +00001326// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1327// a comma before it.
Chris Lattner87ac9722005-11-06 06:46:28 +00001328OptAlign : /*empty*/ { $$ = 0; } |
Chris Lattnerb2b96672005-11-12 18:21:21 +00001329 ALIGN EUINT64VAL {
1330 $$ = $2;
1331 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001332 GEN_ERROR("Alignment must be a power of two");
Reid Spencer61c83e02006-08-18 08:43:06 +00001333 CHECK_FOR_ERROR
Chris Lattnerb2b96672005-11-12 18:21:21 +00001334};
Chris Lattner66db8e42005-11-06 06:34:12 +00001335OptCAlign : /*empty*/ { $$ = 0; } |
Chris Lattnerb2b96672005-11-12 18:21:21 +00001336 ',' ALIGN EUINT64VAL {
1337 $$ = $3;
1338 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001339 GEN_ERROR("Alignment must be a power of two");
Reid Spencer61c83e02006-08-18 08:43:06 +00001340 CHECK_FOR_ERROR
Chris Lattnerb2b96672005-11-12 18:21:21 +00001341};
1342
Chris Lattner66db8e42005-11-06 06:34:12 +00001343
Christopher Lambfe63fb92007-12-11 08:59:05 +00001344
Chris Lattner164c3782005-11-12 00:11:10 +00001345SectionString : SECTION STRINGCONSTANT {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001346 for (unsigned i = 0, e = $2->length(); i != e; ++i)
1347 if ((*$2)[i] == '"' || (*$2)[i] == '\\')
Reid Spencerf4fa5902007-02-05 10:16:10 +00001348 GEN_ERROR("Invalid character in section name");
Chris Lattner164c3782005-11-12 00:11:10 +00001349 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001350 CHECK_FOR_ERROR
Chris Lattner164c3782005-11-12 00:11:10 +00001351};
1352
1353OptSection : /*empty*/ { $$ = 0; } |
1354 SectionString { $$ = $1; };
Chris Lattner164c3782005-11-12 00:11:10 +00001355
Chris Lattnerb2b96672005-11-12 18:21:21 +00001356// GlobalVarAttributes - Used to pass the attributes string on a global. CurGV
1357// is set to be the global we are processing.
1358//
1359GlobalVarAttributes : /* empty */ {} |
1360 ',' GlobalVarAttribute GlobalVarAttributes {};
1361GlobalVarAttribute : SectionString {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001362 CurGV->setSection(*$1);
1363 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001364 CHECK_FOR_ERROR
Eric Christopher2a5196f2008-09-24 04:55:49 +00001365 }
Chris Lattnerb2b96672005-11-12 18:21:21 +00001366 | ALIGN EUINT64VAL {
1367 if ($2 != 0 && !isPowerOf2_32($2))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001368 GEN_ERROR("Alignment must be a power of two");
Chris Lattnerb2b96672005-11-12 18:21:21 +00001369 CurGV->setAlignment($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001370 CHECK_FOR_ERROR
Chris Lattnerb2b96672005-11-12 18:21:21 +00001371 };
Chris Lattner164c3782005-11-12 00:11:10 +00001372
Chris Lattner30c89792001-09-07 16:35:17 +00001373//===----------------------------------------------------------------------===//
1374// Types includes all predefined types... except void, because it can only be
Eric Christopher2a5196f2008-09-24 04:55:49 +00001375// used in specific contexts (function returning void for example).
Chris Lattner30c89792001-09-07 16:35:17 +00001376
1377// Derived types are added later...
1378//
Dale Johannesen320fc8a2007-08-03 01:03:46 +00001379PrimType : INTTYPE | FLOAT | DOUBLE | PPC_FP128 | FP128 | X86_FP80 | LABEL ;
Reid Spencere1553cc2006-12-31 05:40:12 +00001380
Eric Christopher2a5196f2008-09-24 04:55:49 +00001381Types
Reid Spencere1553cc2006-12-31 05:40:12 +00001382 : OPAQUE {
Reid Spencer08de34b2006-12-03 05:45:44 +00001383 $$ = new PATypeHolder(OpaqueType::get());
Reid Spencer61c83e02006-08-18 08:43:06 +00001384 CHECK_FOR_ERROR
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001385 }
1386 | PrimType {
Reid Spencer08de34b2006-12-03 05:45:44 +00001387 $$ = new PATypeHolder($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001388 CHECK_FOR_ERROR
Reid Spencere1553cc2006-12-31 05:40:12 +00001389 }
Christopher Lambd49e18d2007-12-12 08:44:39 +00001390 | Types OptAddrSpace '*' { // Pointer type?
Reid Spencere1553cc2006-12-31 05:40:12 +00001391 if (*$1 == Type::LabelTy)
1392 GEN_ERROR("Cannot form a pointer to a basic block");
Christopher Lambd49e18d2007-12-12 08:44:39 +00001393 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1, $2)));
Christopher Lambfe63fb92007-12-11 08:59:05 +00001394 delete $1;
1395 CHECK_FOR_ERROR
1396 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001397 | SymbolicValueRef { // Named types are also simple types...
1398 const Type* tmp = getTypeVal($1);
1399 CHECK_FOR_ERROR
1400 $$ = new PATypeHolder(tmp);
1401 }
1402 | '\\' EUINT64VAL { // Type UpReference
Reid Spencerf4fa5902007-02-05 10:16:10 +00001403 if ($2 > (uint64_t)~0U) GEN_ERROR("Value out of range");
Chris Lattner30c89792001-09-07 16:35:17 +00001404 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
Chris Lattner3b073862004-02-09 03:19:29 +00001405 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Reid Spencer08de34b2006-12-03 05:45:44 +00001406 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +00001407 UR_OUT("New Upreference!\n");
Reid Spencer61c83e02006-08-18 08:43:06 +00001408 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001409 }
Reid Spencer863dd882007-04-28 16:06:50 +00001410 | Types '(' ArgTypeListI ')' OptFuncAttrs {
Duncan Sandsdc024672007-11-27 13:23:08 +00001411 // Allow but ignore attributes on function types; this permits auto-upgrade.
1412 // FIXME: remove in LLVM 3.0.
Chris Lattnerf7dedf22008-04-23 05:36:58 +00001413 const Type *RetTy = *$1;
1414 if (!FunctionType::isValidReturnType(RetTy))
1415 GEN_ERROR("Invalid result type for LLVM function");
Eric Christopher2a5196f2008-09-24 04:55:49 +00001416
Chris Lattner9232b992003-04-22 18:42:41 +00001417 std::vector<const Type*> Params;
Reid Spencer5694b6e2007-04-09 06:17:21 +00001418 TypeWithAttrsList::iterator I = $3->begin(), E = $3->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00001419 for (; I != E; ++I ) {
Reid Spencer2c9df212007-03-20 01:13:00 +00001420 const Type *Ty = I->Ty->get();
Reid Spencer2c9df212007-03-20 01:13:00 +00001421 Params.push_back(Ty);
Reid Spencere1553cc2006-12-31 05:40:12 +00001422 }
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001423
Chris Lattner2079fde2001-10-13 06:41:08 +00001424 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1425 if (isVarArg) Params.pop_back();
1426
Anton Korobeynikovf8c751a2007-12-03 21:00:45 +00001427 for (unsigned i = 0; i != Params.size(); ++i)
1428 if (!(Params[i]->isFirstClassType() || isa<OpaqueType>(Params[i])))
1429 GEN_ERROR("Function arguments must be value types!");
1430
1431 CHECK_FOR_ERROR
1432
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001433 FunctionType *FT = FunctionType::get(RetTy, Params, isVarArg);
Reid Spencere1553cc2006-12-31 05:40:12 +00001434 delete $1; // Delete the return type handle
Eric Christopher2a5196f2008-09-24 04:55:49 +00001435 $$ = new PATypeHolder(HandleUpRefs(FT));
Nuno Lopesd0ad6772008-10-05 16:49:03 +00001436
1437 // Delete the argument list
1438 for (I = $3->begin() ; I != E; ++I ) {
1439 delete I->Ty;
1440 }
1441 delete $3;
1442
Reid Spencer61c83e02006-08-18 08:43:06 +00001443 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001444 }
Reid Spencer863dd882007-04-28 16:06:50 +00001445 | VOID '(' ArgTypeListI ')' OptFuncAttrs {
Duncan Sandsdc024672007-11-27 13:23:08 +00001446 // Allow but ignore attributes on function types; this permits auto-upgrade.
1447 // FIXME: remove in LLVM 3.0.
Reid Spencere1553cc2006-12-31 05:40:12 +00001448 std::vector<const Type*> Params;
Reid Spencer5694b6e2007-04-09 06:17:21 +00001449 TypeWithAttrsList::iterator I = $3->begin(), E = $3->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00001450 for ( ; I != E; ++I ) {
Reid Spencer2c9df212007-03-20 01:13:00 +00001451 const Type* Ty = I->Ty->get();
Reid Spencer2c9df212007-03-20 01:13:00 +00001452 Params.push_back(Ty);
Reid Spencere1553cc2006-12-31 05:40:12 +00001453 }
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001454
Reid Spencere1553cc2006-12-31 05:40:12 +00001455 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1456 if (isVarArg) Params.pop_back();
1457
Anton Korobeynikovf8c751a2007-12-03 21:00:45 +00001458 for (unsigned i = 0; i != Params.size(); ++i)
1459 if (!(Params[i]->isFirstClassType() || isa<OpaqueType>(Params[i])))
1460 GEN_ERROR("Function arguments must be value types!");
1461
1462 CHECK_FOR_ERROR
1463
Duncan Sandsdc024672007-11-27 13:23:08 +00001464 FunctionType *FT = FunctionType::get($1, Params, isVarArg);
Eric Christopher2a5196f2008-09-24 04:55:49 +00001465 $$ = new PATypeHolder(HandleUpRefs(FT));
Nuno Lopesd0ad6772008-10-05 16:49:03 +00001466
1467 // Delete the argument list
1468 for (I = $3->begin() ; I != E; ++I ) {
1469 delete I->Ty;
1470 }
1471 delete $3;
1472
Reid Spencere1553cc2006-12-31 05:40:12 +00001473 CHECK_FOR_ERROR
1474 }
1475
1476 | '[' EUINT64VAL 'x' Types ']' { // Sized array type?
Dan Gohman80f0f612008-05-23 21:40:55 +00001477 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, $2)));
Reid Spencer08de34b2006-12-03 05:45:44 +00001478 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00001479 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001480 }
Reid Spencerac9dcb92007-02-15 03:39:18 +00001481 | '<' EUINT64VAL 'x' Types '>' { // Vector type?
Reid Spencer08de34b2006-12-03 05:45:44 +00001482 const llvm::Type* ElemTy = $4->get();
1483 if ((unsigned)$2 != $2)
1484 GEN_ERROR("Unsigned result not equal to signed result");
Chris Lattner42a75512007-01-15 02:27:26 +00001485 if (!ElemTy->isFloatingPoint() && !ElemTy->isInteger())
Reid Spencer9d6565a2007-02-15 02:26:10 +00001486 GEN_ERROR("Element type of a VectorType must be primitive");
Reid Spencer9d6565a2007-02-15 02:26:10 +00001487 $$ = new PATypeHolder(HandleUpRefs(VectorType::get(*$4, (unsigned)$2)));
Reid Spencer08de34b2006-12-03 05:45:44 +00001488 delete $4;
1489 CHECK_FOR_ERROR
Brian Gaeke715c90b2004-08-20 06:00:58 +00001490 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001491 | '{' TypeListI '}' { // Structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001492 std::vector<const Type*> Elements;
Reid Spencer08de34b2006-12-03 05:45:44 +00001493 for (std::list<llvm::PATypeHolder>::iterator I = $2->begin(),
Chris Lattnera08976b2005-02-22 23:13:58 +00001494 E = $2->end(); I != E; ++I)
Reid Spencer08de34b2006-12-03 05:45:44 +00001495 Elements.push_back(*I);
Misha Brukman5a2a3822005-05-10 22:02:28 +00001496
Reid Spencer08de34b2006-12-03 05:45:44 +00001497 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001498 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001499 CHECK_FOR_ERROR
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001500 }
1501 | '{' '}' { // Empty structure type?
Reid Spencer08de34b2006-12-03 05:45:44 +00001502 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Reid Spencer61c83e02006-08-18 08:43:06 +00001503 CHECK_FOR_ERROR
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001504 }
Andrew Lenharth38ecbf12006-12-08 18:06:16 +00001505 | '<' '{' TypeListI '}' '>' {
1506 std::vector<const Type*> Elements;
1507 for (std::list<llvm::PATypeHolder>::iterator I = $3->begin(),
1508 E = $3->end(); I != E; ++I)
1509 Elements.push_back(*I);
1510
1511 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements, true)));
1512 delete $3;
1513 CHECK_FOR_ERROR
1514 }
1515 | '<' '{' '}' '>' { // Empty structure type?
1516 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>(), true));
1517 CHECK_FOR_ERROR
1518 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001519 ;
1520
Eric Christopher2a5196f2008-09-24 04:55:49 +00001521ArgType
Devang Patel05988662008-09-25 21:00:45 +00001522 : Types OptAttributes {
Duncan Sandsdc024672007-11-27 13:23:08 +00001523 // Allow but ignore attributes on function types; this permits auto-upgrade.
1524 // FIXME: remove in LLVM 3.0.
Eric Christopher2a5196f2008-09-24 04:55:49 +00001525 $$.Ty = $1;
Devang Patel05988662008-09-25 21:00:45 +00001526 $$.Attrs = Attribute::None;
Reid Spencere1553cc2006-12-31 05:40:12 +00001527 }
1528 ;
1529
Reid Spencer2c261782007-01-05 17:06:19 +00001530ResultTypes
1531 : Types {
Reid Spencere1553cc2006-12-31 05:40:12 +00001532 if (!UpRefs.empty())
1533 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Devang Patel155b8742008-02-23 01:17:17 +00001534 if (!(*$1)->isFirstClassType() && !isa<StructType>($1->get()))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001535 GEN_ERROR("LLVM functions cannot return aggregate types");
Reid Spencer2c261782007-01-05 17:06:19 +00001536 $$ = $1;
Reid Spencere1553cc2006-12-31 05:40:12 +00001537 }
Reid Spencer2c261782007-01-05 17:06:19 +00001538 | VOID {
1539 $$ = new PATypeHolder(Type::VoidTy);
Reid Spencere1553cc2006-12-31 05:40:12 +00001540 }
1541 ;
1542
1543ArgTypeList : ArgType {
1544 $$ = new TypeWithAttrsList();
1545 $$->push_back($1);
1546 CHECK_FOR_ERROR
1547 }
1548 | ArgTypeList ',' ArgType {
1549 ($$=$1)->push_back($3);
1550 CHECK_FOR_ERROR
1551 }
1552 ;
1553
Eric Christopher2a5196f2008-09-24 04:55:49 +00001554ArgTypeListI
Reid Spencere1553cc2006-12-31 05:40:12 +00001555 : ArgTypeList
1556 | ArgTypeList ',' DOTDOTDOT {
1557 $$=$1;
Devang Patel05988662008-09-25 21:00:45 +00001558 TypeWithAttrs TWA; TWA.Attrs = Attribute::None;
Reid Spencere1553cc2006-12-31 05:40:12 +00001559 TWA.Ty = new PATypeHolder(Type::VoidTy);
1560 $$->push_back(TWA);
1561 CHECK_FOR_ERROR
1562 }
1563 | DOTDOTDOT {
1564 $$ = new TypeWithAttrsList;
Devang Patel05988662008-09-25 21:00:45 +00001565 TypeWithAttrs TWA; TWA.Attrs = Attribute::None;
Reid Spencere1553cc2006-12-31 05:40:12 +00001566 TWA.Ty = new PATypeHolder(Type::VoidTy);
1567 $$->push_back(TWA);
1568 CHECK_FOR_ERROR
1569 }
1570 | /*empty*/ {
1571 $$ = new TypeWithAttrsList();
Reid Spencer61c83e02006-08-18 08:43:06 +00001572 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001573 };
Chris Lattner30c89792001-09-07 16:35:17 +00001574
Eric Christopher2a5196f2008-09-24 04:55:49 +00001575// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +00001576// declaration type lists
1577//
Reid Spencere1553cc2006-12-31 05:40:12 +00001578TypeListI : Types {
Reid Spencer08de34b2006-12-03 05:45:44 +00001579 $$ = new std::list<PATypeHolder>();
Eric Christopher2a5196f2008-09-24 04:55:49 +00001580 $$->push_back(*$1);
Reid Spencer2c9df212007-03-20 01:13:00 +00001581 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001582 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001583 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001584 | TypeListI ',' Types {
Eric Christopher2a5196f2008-09-24 04:55:49 +00001585 ($$=$1)->push_back(*$3);
Reid Spencer2c9df212007-03-20 01:13:00 +00001586 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001587 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001588 };
Chris Lattner30c89792001-09-07 16:35:17 +00001589
Chris Lattnere98dda62001-07-14 06:10:16 +00001590// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +00001591// production is used ONLY to represent constants that show up AFTER a 'const',
1592// 'constant' or 'global' token at global scope. Constants that can be inlined
1593// into other expressions (such as integers and constexprs) are handled by the
1594// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +00001595//
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001596ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencere1553cc2006-12-31 05:40:12 +00001597 if (!UpRefs.empty())
1598 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001599 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001600 if (ATy == 0)
Eric Christopher2a5196f2008-09-24 04:55:49 +00001601 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001602 (*$1)->getDescription() + "'");
Chris Lattner30c89792001-09-07 16:35:17 +00001603 const Type *ETy = ATy->getElementType();
Dan Gohmanebb5a972008-06-23 18:40:28 +00001604 uint64_t NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +00001605
Chris Lattner30c89792001-09-07 16:35:17 +00001606 // Verify that we have the correct size...
Dan Gohman46e803b2008-06-24 01:17:52 +00001607 if (NumElements != uint64_t(-1) && NumElements != $3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001608 GEN_ERROR("Type mismatch: constant sized array initialized with " +
Eric Christopher2a5196f2008-09-24 04:55:49 +00001609 utostr($3->size()) + " arguments, but has size of " +
Dan Gohman46e803b2008-06-24 01:17:52 +00001610 utostr(NumElements) + "");
Chris Lattner00950542001-06-06 20:29:01 +00001611
Chris Lattner30c89792001-09-07 16:35:17 +00001612 // Verify all elements are correct type!
1613 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencer08de34b2006-12-03 05:45:44 +00001614 if (ETy != (*$3)[i]->getType())
Eric Christopher2a5196f2008-09-24 04:55:49 +00001615 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Reid Spencer3271ed52004-07-18 00:08:11 +00001616 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencer08de34b2006-12-03 05:45:44 +00001617 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +00001618 }
1619
Reid Spencer08de34b2006-12-03 05:45:44 +00001620 $$ = ConstantArray::get(ATy, *$3);
1621 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001622 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001623 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001624 | Types '[' ']' {
Reid Spencere1553cc2006-12-31 05:40:12 +00001625 if (!UpRefs.empty())
1626 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001627 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001628 if (ATy == 0)
Eric Christopher2a5196f2008-09-24 04:55:49 +00001629 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001630 (*$1)->getDescription() + "'");
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001631
Dan Gohmanebb5a972008-06-23 18:40:28 +00001632 uint64_t NumElements = ATy->getNumElements();
Eric Christopher2a5196f2008-09-24 04:55:49 +00001633 if (NumElements != uint64_t(-1) && NumElements != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001634 GEN_ERROR("Type mismatch: constant sized array initialized with 0"
Dan Gohman46e803b2008-06-24 01:17:52 +00001635 " arguments, but has size of " + utostr(NumElements) +"");
Reid Spencer08de34b2006-12-03 05:45:44 +00001636 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
1637 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001638 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001639 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001640 | Types 'c' STRINGCONSTANT {
Reid Spencere1553cc2006-12-31 05:40:12 +00001641 if (!UpRefs.empty())
1642 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001643 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001644 if (ATy == 0)
Eric Christopher2a5196f2008-09-24 04:55:49 +00001645 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001646 (*$1)->getDescription() + "'");
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001647
Dan Gohmanebb5a972008-06-23 18:40:28 +00001648 uint64_t NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +00001649 const Type *ETy = ATy->getElementType();
Dan Gohman46e803b2008-06-24 01:17:52 +00001650 if (NumElements != uint64_t(-1) && NumElements != $3->length())
Eric Christopher2a5196f2008-09-24 04:55:49 +00001651 GEN_ERROR("Can't build string constant of size " +
Dan Gohman46e803b2008-06-24 01:17:52 +00001652 utostr($3->length()) +
1653 " when array has size " + utostr(NumElements) + "");
Chris Lattner9232b992003-04-22 18:42:41 +00001654 std::vector<Constant*> Vals;
Reid Spencere1553cc2006-12-31 05:40:12 +00001655 if (ETy == Type::Int8Ty) {
Dan Gohman46e803b2008-06-24 01:17:52 +00001656 for (uint64_t i = 0; i < $3->length(); ++i)
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001657 Vals.push_back(ConstantInt::get(ETy, (*$3)[i]));
Chris Lattner93750fa2001-07-28 17:48:55 +00001658 } else {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001659 delete $3;
Reid Spencerf4fa5902007-02-05 10:16:10 +00001660 GEN_ERROR("Cannot build string arrays of non byte sized elements");
Chris Lattner93750fa2001-07-28 17:48:55 +00001661 }
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001662 delete $3;
Reid Spencer08de34b2006-12-03 05:45:44 +00001663 $$ = ConstantArray::get(ATy, Vals);
1664 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001665 CHECK_FOR_ERROR
Chris Lattner93750fa2001-07-28 17:48:55 +00001666 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00001667 | Types '<' ConstVector '>' { // Nonempty unsized arr
Reid Spencere1553cc2006-12-31 05:40:12 +00001668 if (!UpRefs.empty())
1669 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer9d6565a2007-02-15 02:26:10 +00001670 const VectorType *PTy = dyn_cast<VectorType>($1->get());
Brian Gaeke715c90b2004-08-20 06:00:58 +00001671 if (PTy == 0)
Eric Christopher2a5196f2008-09-24 04:55:49 +00001672 GEN_ERROR("Cannot make packed constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001673 (*$1)->getDescription() + "'");
Brian Gaeke715c90b2004-08-20 06:00:58 +00001674 const Type *ETy = PTy->getElementType();
Dan Gohmanebb5a972008-06-23 18:40:28 +00001675 unsigned NumElements = PTy->getNumElements();
Brian Gaeke715c90b2004-08-20 06:00:58 +00001676
1677 // Verify that we have the correct size...
Dan Gohman46e803b2008-06-24 01:17:52 +00001678 if (NumElements != unsigned(-1) && NumElements != (unsigned)$3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001679 GEN_ERROR("Type mismatch: constant sized packed initialized with " +
Eric Christopher2a5196f2008-09-24 04:55:49 +00001680 utostr($3->size()) + " arguments, but has size of " +
Dan Gohman46e803b2008-06-24 01:17:52 +00001681 utostr(NumElements) + "");
Brian Gaeke715c90b2004-08-20 06:00:58 +00001682
1683 // Verify all elements are correct type!
1684 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencer08de34b2006-12-03 05:45:44 +00001685 if (ETy != (*$3)[i]->getType())
Eric Christopher2a5196f2008-09-24 04:55:49 +00001686 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Brian Gaeke715c90b2004-08-20 06:00:58 +00001687 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencer08de34b2006-12-03 05:45:44 +00001688 (*$3)[i]->getType()->getDescription() + "'.");
Brian Gaeke715c90b2004-08-20 06:00:58 +00001689 }
1690
Reid Spencer9d6565a2007-02-15 02:26:10 +00001691 $$ = ConstantVector::get(PTy, *$3);
Reid Spencer08de34b2006-12-03 05:45:44 +00001692 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001693 CHECK_FOR_ERROR
Brian Gaeke715c90b2004-08-20 06:00:58 +00001694 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001695 | Types '{' ConstVector '}' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001696 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001697 if (STy == 0)
Eric Christopher2a5196f2008-09-24 04:55:49 +00001698 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001699 (*$1)->getDescription() + "'");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001700
Chris Lattnerc6212f12003-05-21 16:06:56 +00001701 if ($3->size() != STy->getNumContainedTypes())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001702 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattnerc6212f12003-05-21 16:06:56 +00001703
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001704 // Check to ensure that constants are compatible with the type initializer!
1705 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Reid Spencer08de34b2006-12-03 05:45:44 +00001706 if ((*$3)[i]->getType() != STy->getElementType(i))
Reid Spencer61c83e02006-08-18 08:43:06 +00001707 GEN_ERROR("Expected type '" +
Chris Lattnerd21cd802004-02-09 04:37:31 +00001708 STy->getElementType(i)->getDescription() +
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001709 "' for element #" + utostr(i) +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001710 " of structure initializer");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001711
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001712 // Check to ensure that Type is not packed
1713 if (STy->isPacked())
Chris Lattner4989b842007-04-26 05:30:35 +00001714 GEN_ERROR("Unpacked Initializer to vector type '" +
1715 STy->getDescription() + "'");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001716
Reid Spencer08de34b2006-12-03 05:45:44 +00001717 $$ = ConstantStruct::get(STy, *$3);
1718 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001719 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001720 }
Chris Lattnerc6212f12003-05-21 16:06:56 +00001721 | Types '{' '}' {
Reid Spencere1553cc2006-12-31 05:40:12 +00001722 if (!UpRefs.empty())
1723 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001724 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerc6212f12003-05-21 16:06:56 +00001725 if (STy == 0)
Eric Christopher2a5196f2008-09-24 04:55:49 +00001726 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001727 (*$1)->getDescription() + "'");
Chris Lattnerc6212f12003-05-21 16:06:56 +00001728
1729 if (STy->getNumContainedTypes() != 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001730 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattnerc6212f12003-05-21 16:06:56 +00001731
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001732 // Check to ensure that Type is not packed
1733 if (STy->isPacked())
Chris Lattner4989b842007-04-26 05:30:35 +00001734 GEN_ERROR("Unpacked Initializer to vector type '" +
1735 STy->getDescription() + "'");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001736
1737 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1738 delete $1;
1739 CHECK_FOR_ERROR
1740 }
1741 | Types '<' '{' ConstVector '}' '>' {
1742 const StructType *STy = dyn_cast<StructType>($1->get());
1743 if (STy == 0)
Eric Christopher2a5196f2008-09-24 04:55:49 +00001744 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001745 (*$1)->getDescription() + "'");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001746
1747 if ($4->size() != STy->getNumContainedTypes())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001748 GEN_ERROR("Illegal number of initializers for structure type");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001749
1750 // Check to ensure that constants are compatible with the type initializer!
1751 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1752 if ((*$4)[i]->getType() != STy->getElementType(i))
1753 GEN_ERROR("Expected type '" +
1754 STy->getElementType(i)->getDescription() +
1755 "' for element #" + utostr(i) +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001756 " of structure initializer");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001757
1758 // Check to ensure that Type is packed
1759 if (!STy->isPacked())
Eric Christopher2a5196f2008-09-24 04:55:49 +00001760 GEN_ERROR("Vector initializer to non-vector type '" +
Reid Spencerac9dcb92007-02-15 03:39:18 +00001761 STy->getDescription() + "'");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001762
1763 $$ = ConstantStruct::get(STy, *$4);
1764 delete $1; delete $4;
1765 CHECK_FOR_ERROR
1766 }
1767 | Types '<' '{' '}' '>' {
1768 if (!UpRefs.empty())
1769 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1770 const StructType *STy = dyn_cast<StructType>($1->get());
1771 if (STy == 0)
Eric Christopher2a5196f2008-09-24 04:55:49 +00001772 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001773 (*$1)->getDescription() + "'");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001774
1775 if (STy->getNumContainedTypes() != 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001776 GEN_ERROR("Illegal number of initializers for structure type");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001777
1778 // Check to ensure that Type is packed
1779 if (!STy->isPacked())
Eric Christopher2a5196f2008-09-24 04:55:49 +00001780 GEN_ERROR("Vector initializer to non-vector type '" +
Reid Spencerac9dcb92007-02-15 03:39:18 +00001781 STy->getDescription() + "'");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001782
Reid Spencer08de34b2006-12-03 05:45:44 +00001783 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1784 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001785 CHECK_FOR_ERROR
Chris Lattnerc6212f12003-05-21 16:06:56 +00001786 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001787 | Types NULL_TOK {
Reid Spencere1553cc2006-12-31 05:40:12 +00001788 if (!UpRefs.empty())
1789 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001790 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001791 if (PTy == 0)
Eric Christopher2a5196f2008-09-24 04:55:49 +00001792 GEN_ERROR("Cannot make null pointer constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001793 (*$1)->getDescription() + "'");
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001794
Reid Spencer08de34b2006-12-03 05:45:44 +00001795 $$ = ConstantPointerNull::get(PTy);
1796 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001797 CHECK_FOR_ERROR
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001798 }
Chris Lattner16710e92004-10-16 18:17:13 +00001799 | Types UNDEF {
Reid Spencere1553cc2006-12-31 05:40:12 +00001800 if (!UpRefs.empty())
1801 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001802 $$ = UndefValue::get($1->get());
1803 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001804 CHECK_FOR_ERROR
Chris Lattner16710e92004-10-16 18:17:13 +00001805 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001806 | Types SymbolicValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00001807 if (!UpRefs.empty())
1808 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001809 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001810 if (Ty == 0)
Devang Pateld6ffcf92008-02-19 22:26:37 +00001811 GEN_ERROR("Global const reference must be a pointer type " + (*$1)->getDescription());
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001812
Chris Lattner3101c252002-08-15 17:58:33 +00001813 // ConstExprs can exist in the body of a function, thus creating
Reid Spencer3271ed52004-07-18 00:08:11 +00001814 // GlobalValues whenever they refer to a variable. Because we are in
Reid Spencer186a43f2007-03-19 18:39:36 +00001815 // the context of a function, getExistingVal will search the functions
Chris Lattner3101c252002-08-15 17:58:33 +00001816 // symbol table instead of the module symbol table for the global symbol,
1817 // which throws things all off. To get around this, we just tell
Reid Spencer186a43f2007-03-19 18:39:36 +00001818 // getExistingVal that we are at global scope here.
Chris Lattner3101c252002-08-15 17:58:33 +00001819 //
Chris Lattner394f2eb2003-10-16 20:12:13 +00001820 Function *SavedCurFn = CurFun.CurrentFunction;
1821 CurFun.CurrentFunction = 0;
Chris Lattner3101c252002-08-15 17:58:33 +00001822
Reid Spencer186a43f2007-03-19 18:39:36 +00001823 Value *V = getExistingVal(Ty, $2);
Reid Spencer5b7e7532006-09-28 19:28:24 +00001824 CHECK_FOR_ERROR
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001825
Chris Lattner394f2eb2003-10-16 20:12:13 +00001826 CurFun.CurrentFunction = SavedCurFn;
Chris Lattner3101c252002-08-15 17:58:33 +00001827
Chris Lattner2079fde2001-10-13 06:41:08 +00001828 // If this is an initializer for a constant pointer, which is referencing a
1829 // (currently) undefined variable, create a stub now that shall be replaced
1830 // in the future with the right type of variable.
1831 //
1832 if (V == 0) {
Reid Spencer1755a9a2007-02-05 17:01:20 +00001833 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001834 const PointerType *PT = cast<PointerType>(Ty);
1835
1836 // First check to see if the forward references value is already created!
1837 PerModuleInfo::GlobalRefsType::iterator I =
Reid Spencer3271ed52004-07-18 00:08:11 +00001838 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Eric Christopher2a5196f2008-09-24 04:55:49 +00001839
Chris Lattner2079fde2001-10-13 06:41:08 +00001840 if (I != CurModule.GlobalRefs.end()) {
Reid Spencer3271ed52004-07-18 00:08:11 +00001841 V = I->second; // Placeholder already exists, use it...
Chris Lattnera989b232003-12-23 20:05:15 +00001842 $2.destroy();
Chris Lattner2079fde2001-10-13 06:41:08 +00001843 } else {
Chris Lattner8a327842004-07-14 21:44:00 +00001844 std::string Name;
Reid Spencerb2d17862007-01-26 08:04:51 +00001845 if ($2.Type == ValID::GlobalName)
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001846 Name = $2.getName();
Reid Spencerb2d17862007-01-26 08:04:51 +00001847 else if ($2.Type != ValID::GlobalID)
1848 GEN_ERROR("Invalid reference to global");
Chris Lattner8a327842004-07-14 21:44:00 +00001849
Reid Spencer3271ed52004-07-18 00:08:11 +00001850 // Create the forward referenced global.
Chris Lattnera09000d2004-07-14 23:03:46 +00001851 GlobalValue *GV;
Eric Christopher2a5196f2008-09-24 04:55:49 +00001852 if (const FunctionType *FTy =
Chris Lattnera09000d2004-07-14 23:03:46 +00001853 dyn_cast<FunctionType>(PT->getElementType())) {
Gabor Greif051a9502008-04-06 20:25:17 +00001854 GV = Function::Create(FTy, GlobalValue::ExternalWeakLinkage, Name,
1855 CurModule.CurrentModule);
Chris Lattnera09000d2004-07-14 23:03:46 +00001856 } else {
Reid Spencer3271ed52004-07-18 00:08:11 +00001857 GV = new GlobalVariable(PT->getElementType(), false,
Chris Lattner4989b842007-04-26 05:30:35 +00001858 GlobalValue::ExternalWeakLinkage, 0,
Chris Lattnera09000d2004-07-14 23:03:46 +00001859 Name, CurModule.CurrentModule);
1860 }
Chris Lattner8a327842004-07-14 21:44:00 +00001861
Reid Spencer3271ed52004-07-18 00:08:11 +00001862 // Keep track of the fact that we have a forward ref to recycle it
1863 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1864 V = GV;
Chris Lattner2079fde2001-10-13 06:41:08 +00001865 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001866 }
1867
Reid Spencer08de34b2006-12-03 05:45:44 +00001868 $$ = cast<GlobalValue>(V);
1869 delete $1; // Free the type handle
Reid Spencer61c83e02006-08-18 08:43:06 +00001870 CHECK_FOR_ERROR
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001871 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001872 | Types ConstExpr {
Reid Spencere1553cc2006-12-31 05:40:12 +00001873 if (!UpRefs.empty())
1874 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001875 if ($1->get() != $2->getType())
Eric Christopher2a5196f2008-09-24 04:55:49 +00001876 GEN_ERROR("Mismatched types for constant expression: " +
Reid Spencerb4fdfdb2007-01-04 00:05:48 +00001877 (*$1)->getDescription() + " and " + $2->getType()->getDescription());
Chris Lattnerd78700d2002-08-16 21:14:40 +00001878 $$ = $2;
Reid Spencer08de34b2006-12-03 05:45:44 +00001879 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001880 CHECK_FOR_ERROR
Chris Lattnerf4600482003-06-28 20:01:34 +00001881 }
1882 | Types ZEROINITIALIZER {
Reid Spencere1553cc2006-12-31 05:40:12 +00001883 if (!UpRefs.empty())
1884 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001885 const Type *Ty = $1->get();
Chris Lattner78915932004-11-28 16:45:45 +00001886 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001887 GEN_ERROR("Cannot create a null initialized value of this type");
Reid Spencer08de34b2006-12-03 05:45:44 +00001888 $$ = Constant::getNullValue(Ty);
1889 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001890 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00001891 }
Chris Lattnerc7e30692008-10-15 06:16:45 +00001892 | Types ESINT64VAL { // integral constants
1893 if (IntegerType *IT = dyn_cast<IntegerType>($1->get())) {
1894 if (!ConstantInt::isValueValidForType(IT, $2))
1895 GEN_ERROR("Constant value doesn't fit in type");
1896 $$ = ConstantInt::get(IT, $2, true);
1897 } else {
1898 GEN_ERROR("integer constant must have integer type");
1899 }
1900 delete $1;
Reid Spencerf2310042007-02-28 02:23:44 +00001901 CHECK_FOR_ERROR
1902 }
Chris Lattnerc7e30692008-10-15 06:16:45 +00001903 | Types ESAPINTVAL { // arbitrary precision integer constants
1904 if (IntegerType *IT = dyn_cast<IntegerType>($1->get())) {
1905 if ($2->getBitWidth() > IT->getBitWidth())
1906 GEN_ERROR("Constant value does not fit in type");
1907 $2->sextOrTrunc(IT->getBitWidth());
1908 $$ = ConstantInt::get(*$2);
1909 } else {
1910 GEN_ERROR("integer constant must have integer type");
Reid Spenceraf1aacd2007-03-01 19:32:01 +00001911 }
Chris Lattnerc7e30692008-10-15 06:16:45 +00001912 delete $1;
Reid Spencerf2310042007-02-28 02:23:44 +00001913 delete $2;
Reid Spencer9ffad0a2006-12-20 17:20:09 +00001914 CHECK_FOR_ERROR
1915 }
Chris Lattnerc7e30692008-10-15 06:16:45 +00001916 | Types EUINT64VAL { // integral constants
1917 if (IntegerType *IT = dyn_cast<IntegerType>($1->get())) {
1918 if (!ConstantInt::isValueValidForType(IT, $2))
1919 GEN_ERROR("Constant value doesn't fit in type");
1920 $$ = ConstantInt::get(IT, $2, false);
1921 } else {
1922 GEN_ERROR("integer constant must have integer type");
Eric Christopher2a5196f2008-09-24 04:55:49 +00001923 }
Chris Lattnerc7e30692008-10-15 06:16:45 +00001924 delete $1;
Reid Spencer9ffad0a2006-12-20 17:20:09 +00001925 CHECK_FOR_ERROR
1926 }
Chris Lattnerc7e30692008-10-15 06:16:45 +00001927 | Types EUAPINTVAL { // arbitrary precision integer constants
1928 if (IntegerType *IT = dyn_cast<IntegerType>($1->get())) {
1929 if ($2->getBitWidth() > IT->getBitWidth())
1930 GEN_ERROR("Constant value does not fit in type");
1931 $2->zextOrTrunc(IT->getBitWidth());
1932 $$ = ConstantInt::get(*$2);
1933 } else {
1934 GEN_ERROR("integer constant must have integer type");
1935 }
1936
1937 delete $2;
1938 delete $1;
1939 CHECK_FOR_ERROR
1940 }
1941 | Types TRUETOK { // Boolean constants
1942 if ($1->get() != Type::Int1Ty)
Dan Gohman9c531cd2008-05-23 18:23:11 +00001943 GEN_ERROR("Constant true must have type i1");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001944 $$ = ConstantInt::getTrue();
Chris Lattnerc7e30692008-10-15 06:16:45 +00001945 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001946 CHECK_FOR_ERROR
Chris Lattnerd05e3592002-08-15 18:17:28 +00001947 }
Chris Lattnerc7e30692008-10-15 06:16:45 +00001948 | Types FALSETOK { // Boolean constants
1949 if ($1->get() != Type::Int1Ty)
Dan Gohman9c531cd2008-05-23 18:23:11 +00001950 GEN_ERROR("Constant false must have type i1");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001951 $$ = ConstantInt::getFalse();
Chris Lattnerc7e30692008-10-15 06:16:45 +00001952 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001953 CHECK_FOR_ERROR
Chris Lattnerd05e3592002-08-15 18:17:28 +00001954 }
Chris Lattnerc7e30692008-10-15 06:16:45 +00001955 | Types FPVAL { // Floating point constants
1956 if (!ConstantFP::isValueValidForType($1->get(), *$2))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001957 GEN_ERROR("Floating point constant invalid for type");
Chris Lattnerc7e30692008-10-15 06:16:45 +00001958
Eric Christopher2a5196f2008-09-24 04:55:49 +00001959 // Lexer has no type info, so builds all float and double FP constants
Dale Johannesen3f6eb742007-09-11 18:32:33 +00001960 // as double. Fix this here. Long double is done right.
Chris Lattnerc7e30692008-10-15 06:16:45 +00001961 if (&$2->getSemantics()==&APFloat::IEEEdouble && $1->get()==Type::FloatTy) {
Dale Johannesen23a98552008-10-09 23:00:39 +00001962 bool ignored;
1963 $2->convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven,
1964 &ignored);
1965 }
Chris Lattner02a260a2008-04-20 00:41:09 +00001966 $$ = ConstantFP::get(*$2);
Chris Lattnerc7e30692008-10-15 06:16:45 +00001967 delete $1;
Dale Johannesencdd509a2007-09-07 21:07:57 +00001968 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001969 CHECK_FOR_ERROR
Chris Lattnerd05e3592002-08-15 18:17:28 +00001970 };
1971
Chris Lattner00950542001-06-06 20:29:01 +00001972
Reid Spencer3da59db2006-11-27 01:05:10 +00001973ConstExpr: CastOps '(' ConstVal TO Types ')' {
Reid Spencere1553cc2006-12-31 05:40:12 +00001974 if (!UpRefs.empty())
1975 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001976 Constant *Val = $3;
Reid Spencer93947c32007-01-17 02:47:33 +00001977 const Type *DestTy = $5->get();
1978 if (!CastInst::castIsValid($1, $3, DestTy))
1979 GEN_ERROR("invalid cast opcode for cast from '" +
1980 Val->getType()->getDescription() + "' to '" +
Eric Christopher2a5196f2008-09-24 04:55:49 +00001981 DestTy->getDescription() + "'");
Reid Spencer93947c32007-01-17 02:47:33 +00001982 $$ = ConstantExpr::getCast($1, $3, DestTy);
Reid Spencer08de34b2006-12-03 05:45:44 +00001983 delete $5;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001984 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001985 | GETELEMENTPTR '(' ConstVal IndexList ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001986 if (!isa<PointerType>($3->getType()))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001987 GEN_ERROR("GetElementPtr requires a pointer operand");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001988
Reid Spencer08de34b2006-12-03 05:45:44 +00001989 const Type *IdxTy =
Dan Gohman041e2eb2008-05-15 19:50:34 +00001990 GetElementPtrInst::getIndexedType($3->getType(), $4->begin(), $4->end());
Reid Spencer08de34b2006-12-03 05:45:44 +00001991 if (!IdxTy)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001992 GEN_ERROR("Index list invalid for constant getelementptr");
Reid Spencer08de34b2006-12-03 05:45:44 +00001993
Chris Lattnere0135402007-01-31 04:43:46 +00001994 SmallVector<Constant*, 8> IdxVec;
Reid Spencer08de34b2006-12-03 05:45:44 +00001995 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1996 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001997 IdxVec.push_back(C);
1998 else
Reid Spencerf4fa5902007-02-05 10:16:10 +00001999 GEN_ERROR("Indices to constant getelementptr must be constants");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00002000
Chris Lattnerd78700d2002-08-16 21:14:40 +00002001 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00002002
Chris Lattnere0135402007-01-31 04:43:46 +00002003 $$ = ConstantExpr::getGetElementPtr($3, &IdxVec[0], IdxVec.size());
Reid Spencer61c83e02006-08-18 08:43:06 +00002004 CHECK_FOR_ERROR
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002005 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00002006 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer4fe16d62007-01-11 18:21:29 +00002007 if ($3->getType() != Type::Int1Ty)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002008 GEN_ERROR("Select condition must be of boolean type");
Reid Spencer08de34b2006-12-03 05:45:44 +00002009 if ($5->getType() != $7->getType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00002010 GEN_ERROR("Select operand types must match");
Reid Spencer08de34b2006-12-03 05:45:44 +00002011 $$ = ConstantExpr::getSelect($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00002012 CHECK_FOR_ERROR
Chris Lattner76f0ff32004-03-12 05:51:36 +00002013 }
Chris Lattnerc6d2f152004-08-17 17:26:41 +00002014 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00002015 if ($3->getType() != $5->getType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00002016 GEN_ERROR("Binary operator types must match");
Reid Spencer1628cec2006-10-26 06:15:43 +00002017 CHECK_FOR_ERROR;
Reid Spencerc6e956e2006-12-05 19:15:41 +00002018 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattnerc6d2f152004-08-17 17:26:41 +00002019 }
2020 | LogicalOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00002021 if ($3->getType() != $5->getType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00002022 GEN_ERROR("Logical operator types must match");
Chris Lattner42a75512007-01-15 02:27:26 +00002023 if (!$3->getType()->isInteger()) {
Eric Christopher2a5196f2008-09-24 04:55:49 +00002024 if (!isa<VectorType>($3->getType()) ||
Reid Spencer9d6565a2007-02-15 02:26:10 +00002025 !cast<VectorType>($3->getType())->getElementType()->isInteger())
Reid Spencerf4fa5902007-02-05 10:16:10 +00002026 GEN_ERROR("Logical operator requires integral operands");
Chris Lattner0a017832005-12-21 18:31:29 +00002027 }
Reid Spencer08de34b2006-12-03 05:45:44 +00002028 $$ = ConstantExpr::get($1, $3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002029 CHECK_FOR_ERROR
Chris Lattnerc6d2f152004-08-17 17:26:41 +00002030 }
Reid Spencer763ed5e2006-12-04 05:20:06 +00002031 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
2032 if ($4->getType() != $6->getType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00002033 GEN_ERROR("icmp operand types must match");
Reid Spencer763ed5e2006-12-04 05:20:06 +00002034 $$ = ConstantExpr::getICmp($2, $4, $6);
Reid Spencer08de34b2006-12-03 05:45:44 +00002035 }
Reid Spencer763ed5e2006-12-04 05:20:06 +00002036 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
2037 if ($4->getType() != $6->getType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00002038 GEN_ERROR("fcmp operand types must match");
Reid Spencer763ed5e2006-12-04 05:20:06 +00002039 $$ = ConstantExpr::getFCmp($2, $4, $6);
Reid Spencer08de34b2006-12-03 05:45:44 +00002040 }
Nate Begemanac80ade2008-05-12 19:01:56 +00002041 | VICMP IPredicates '(' ConstVal ',' ConstVal ')' {
2042 if ($4->getType() != $6->getType())
2043 GEN_ERROR("vicmp operand types must match");
2044 $$ = ConstantExpr::getVICmp($2, $4, $6);
2045 }
2046 | VFCMP FPredicates '(' ConstVal ',' ConstVal ')' {
2047 if ($4->getType() != $6->getType())
2048 GEN_ERROR("vfcmp operand types must match");
2049 $$ = ConstantExpr::getVFCmp($2, $4, $6);
2050 }
Robert Bocchino9c62b562006-01-10 19:04:32 +00002051 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00002052 if (!ExtractElementInst::isValidOperands($3, $5))
Reid Spencerf4fa5902007-02-05 10:16:10 +00002053 GEN_ERROR("Invalid extractelement operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00002054 $$ = ConstantExpr::getExtractElement($3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002055 CHECK_FOR_ERROR
Chris Lattnerca73cd82006-04-08 03:53:34 +00002056 }
2057 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00002058 if (!InsertElementInst::isValidOperands($3, $5, $7))
Reid Spencerf4fa5902007-02-05 10:16:10 +00002059 GEN_ERROR("Invalid insertelement operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00002060 $$ = ConstantExpr::getInsertElement($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00002061 CHECK_FOR_ERROR
Chris Lattnerca73cd82006-04-08 03:53:34 +00002062 }
2063 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00002064 if (!ShuffleVectorInst::isValidOperands($3, $5, $7))
Reid Spencerf4fa5902007-02-05 10:16:10 +00002065 GEN_ERROR("Invalid shufflevector operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00002066 $$ = ConstantExpr::getShuffleVector($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00002067 CHECK_FOR_ERROR
Dan Gohmane4977cf2008-05-23 01:55:30 +00002068 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002069 | EXTRACTVALUE '(' ConstVal ConstantIndexList ')' {
Dan Gohmane4977cf2008-05-23 01:55:30 +00002070 if (!isa<StructType>($3->getType()) && !isa<ArrayType>($3->getType()))
2071 GEN_ERROR("ExtractValue requires an aggregate operand");
2072
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002073 $$ = ConstantExpr::getExtractValue($3, &(*$4)[0], $4->size());
Dan Gohmane4977cf2008-05-23 01:55:30 +00002074 delete $4;
Dan Gohmane4977cf2008-05-23 01:55:30 +00002075 CHECK_FOR_ERROR
2076 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002077 | INSERTVALUE '(' ConstVal ',' ConstVal ConstantIndexList ')' {
Dan Gohmane4977cf2008-05-23 01:55:30 +00002078 if (!isa<StructType>($3->getType()) && !isa<ArrayType>($3->getType()))
2079 GEN_ERROR("InsertValue requires an aggregate operand");
2080
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002081 $$ = ConstantExpr::getInsertValue($3, $5, &(*$6)[0], $6->size());
Dan Gohmane4977cf2008-05-23 01:55:30 +00002082 delete $6;
Dan Gohmane4977cf2008-05-23 01:55:30 +00002083 CHECK_FOR_ERROR
Chris Lattner699f1eb2002-08-14 17:12:33 +00002084 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002085
Chris Lattnerca73cd82006-04-08 03:53:34 +00002086
Misha Brukmanbc0e9982003-07-14 17:20:40 +00002087// ConstVector - A list of comma separated constants.
Chris Lattner00950542001-06-06 20:29:01 +00002088ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00002089 ($$ = $1)->push_back($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002090 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002091 }
2092 | ConstVal {
Reid Spencer08de34b2006-12-03 05:45:44 +00002093 $$ = new std::vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00002094 $$->push_back($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002095 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002096 };
Chris Lattner00950542001-06-06 20:29:01 +00002097
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002098
Chris Lattner1781aca2001-09-18 04:00:54 +00002099// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00002100GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00002101
Eric Christopher2a5196f2008-09-24 04:55:49 +00002102// ThreadLocal
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002103ThreadLocal : THREAD_LOCAL { $$ = true; } | { $$ = false; };
2104
Anton Korobeynikova80e1182007-04-28 13:45:00 +00002105// AliaseeRef - Match either GlobalValue or bitcast to GlobalValue.
2106AliaseeRef : ResultTypes SymbolicValueRef {
2107 const Type* VTy = $1->get();
2108 Value *V = getVal(VTy, $2);
Chris Lattner6df20ef2007-08-06 21:00:37 +00002109 CHECK_FOR_ERROR
Anton Korobeynikova80e1182007-04-28 13:45:00 +00002110 GlobalValue* Aliasee = dyn_cast<GlobalValue>(V);
2111 if (!Aliasee)
2112 GEN_ERROR("Aliases can be created only to global values");
2113
2114 $$ = Aliasee;
2115 CHECK_FOR_ERROR
2116 delete $1;
2117 }
2118 | BITCAST '(' AliaseeRef TO Types ')' {
2119 Constant *Val = $3;
2120 const Type *DestTy = $5->get();
2121 if (!CastInst::castIsValid($1, $3, DestTy))
2122 GEN_ERROR("invalid cast opcode for cast from '" +
2123 Val->getType()->getDescription() + "' to '" +
2124 DestTy->getDescription() + "'");
Eric Christopher2a5196f2008-09-24 04:55:49 +00002125
Anton Korobeynikova80e1182007-04-28 13:45:00 +00002126 $$ = ConstantExpr::getCast($1, $3, DestTy);
2127 CHECK_FOR_ERROR
2128 delete $5;
2129 };
Chris Lattner00950542001-06-06 20:29:01 +00002130
Chris Lattner0e73ce62002-05-02 19:11:13 +00002131//===----------------------------------------------------------------------===//
2132// Rules to match Modules
2133//===----------------------------------------------------------------------===//
2134
2135// Module rule: Capture the result of parsing the whole file into a result
2136// variable...
2137//
Eric Christopher2a5196f2008-09-24 04:55:49 +00002138Module
Reid Spencerb951bc02006-12-29 20:29:48 +00002139 : DefinitionList {
2140 $$ = ParserResult = CurModule.CurrentModule;
2141 CurModule.ModuleDone();
2142 CHECK_FOR_ERROR;
2143 }
2144 | /*empty*/ {
2145 $$ = ParserResult = CurModule.CurrentModule;
2146 CurModule.ModuleDone();
2147 CHECK_FOR_ERROR;
2148 }
2149 ;
Chris Lattner0e73ce62002-05-02 19:11:13 +00002150
Reid Spencerb951bc02006-12-29 20:29:48 +00002151DefinitionList
2152 : Definition
2153 | DefinitionList Definition
2154 ;
2155
Eric Christopher2a5196f2008-09-24 04:55:49 +00002156Definition
Jeff Cohen361c3ef2007-01-21 19:19:31 +00002157 : DEFINE { CurFun.isDeclare = false; } Function {
Chris Lattner394f2eb2003-10-16 20:12:13 +00002158 CurFun.FunctionDone();
Reid Spencer61c83e02006-08-18 08:43:06 +00002159 CHECK_FOR_ERROR
Reid Spencerb951bc02006-12-29 20:29:48 +00002160 }
2161 | DECLARE { CurFun.isDeclare = true; } FunctionProto {
Reid Spencer61c83e02006-08-18 08:43:06 +00002162 CHECK_FOR_ERROR
Chris Lattner0e73ce62002-05-02 19:11:13 +00002163 }
Reid Spencerb951bc02006-12-29 20:29:48 +00002164 | MODULE ASM_TOK AsmBlock {
Reid Spencer61c83e02006-08-18 08:43:06 +00002165 CHECK_FOR_ERROR
Eric Christopher2a5196f2008-09-24 04:55:49 +00002166 }
Reid Spencerb2d17862007-01-26 08:04:51 +00002167 | OptLocalAssign TYPE Types {
Reid Spencere1553cc2006-12-31 05:40:12 +00002168 if (!UpRefs.empty())
2169 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattner4a42e902001-10-22 05:56:09 +00002170 // Eagerly resolve types. This is not an optimization, this is a
2171 // requirement that is due to the fact that we could have this:
2172 //
2173 // %list = type { %list * }
2174 // %list = type { %list * } ; repeated type decl
2175 //
2176 // If types are not resolved eagerly, then the two types will not be
2177 // determined to be the same type!
2178 //
Reid Spencerb951bc02006-12-29 20:29:48 +00002179 ResolveTypeTo($1, *$3);
Chris Lattner4a42e902001-10-22 05:56:09 +00002180
Reid Spencerb951bc02006-12-29 20:29:48 +00002181 if (!setTypeName(*$3, $1) && !$1) {
Reid Spencer5b7e7532006-09-28 19:28:24 +00002182 CHECK_FOR_ERROR
Chris Lattner8c89a0a2004-07-13 08:10:10 +00002183 // If this is a named type that is not a redefinition, add it to the slot
2184 // table.
Reid Spencerb951bc02006-12-29 20:29:48 +00002185 CurModule.Types.push_back(*$3);
Chris Lattner30c89792001-09-07 16:35:17 +00002186 }
Reid Spencer08de34b2006-12-03 05:45:44 +00002187
Reid Spencerb951bc02006-12-29 20:29:48 +00002188 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002189 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00002190 }
Reid Spencerb2d17862007-01-26 08:04:51 +00002191 | OptLocalAssign TYPE VOID {
Reid Spencere1553cc2006-12-31 05:40:12 +00002192 ResolveTypeTo($1, $3);
2193
2194 if (!setTypeName($3, $1) && !$1) {
2195 CHECK_FOR_ERROR
2196 // If this is a named type that is not a redefinition, add it to the slot
2197 // table.
2198 CurModule.Types.push_back($3);
2199 }
2200 CHECK_FOR_ERROR
2201 }
Eric Christopher2a5196f2008-09-24 04:55:49 +00002202 | OptGlobalAssign GVVisibilityStyle ThreadLocal GlobalType ConstVal
2203 OptAddrSpace {
Reid Spencerb2d17862007-01-26 08:04:51 +00002204 /* "Externally Visible" Linkage */
Eric Christopher2a5196f2008-09-24 04:55:49 +00002205 if ($5 == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002206 GEN_ERROR("Global value initializer is not a constant");
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002207 CurGV = ParseGlobalVariable($1, GlobalValue::ExternalLinkage,
Christopher Lambd49e18d2007-12-12 08:44:39 +00002208 $2, $4, $5->getType(), $5, $3, $6);
Christopher Lambfe63fb92007-12-11 08:59:05 +00002209 CHECK_FOR_ERROR
2210 } GlobalVarAttributes {
2211 CurGV = 0;
2212 }
Chris Lattner4989b842007-04-26 05:30:35 +00002213 | OptGlobalAssign GVInternalLinkage GVVisibilityStyle ThreadLocal GlobalType
Christopher Lambd49e18d2007-12-12 08:44:39 +00002214 ConstVal OptAddrSpace {
Eric Christopher2a5196f2008-09-24 04:55:49 +00002215 if ($6 == 0)
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002216 GEN_ERROR("Global value initializer is not a constant");
Christopher Lambd49e18d2007-12-12 08:44:39 +00002217 CurGV = ParseGlobalVariable($1, $2, $3, $5, $6->getType(), $6, $4, $7);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002218 CHECK_FOR_ERROR
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002219 } GlobalVarAttributes {
2220 CurGV = 0;
2221 }
Chris Lattner4989b842007-04-26 05:30:35 +00002222 | OptGlobalAssign GVExternalLinkage GVVisibilityStyle ThreadLocal GlobalType
Christopher Lambd49e18d2007-12-12 08:44:39 +00002223 Types OptAddrSpace {
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002224 if (!UpRefs.empty())
2225 GEN_ERROR("Invalid upreference in type: " + (*$6)->getDescription());
Christopher Lambd49e18d2007-12-12 08:44:39 +00002226 CurGV = ParseGlobalVariable($1, $2, $3, $5, *$6, 0, $4, $7);
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002227 CHECK_FOR_ERROR
2228 delete $6;
Reid Spencer5b7e7532006-09-28 19:28:24 +00002229 } GlobalVarAttributes {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002230 CurGV = 0;
2231 CHECK_FOR_ERROR
2232 }
Anton Korobeynikova80e1182007-04-28 13:45:00 +00002233 | OptGlobalAssign GVVisibilityStyle ALIAS AliasLinkage AliaseeRef {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002234 std::string Name;
2235 if ($1) {
2236 Name = *$1;
2237 delete $1;
2238 }
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00002239 if (Name.empty())
Anton Korobeynikova80e1182007-04-28 13:45:00 +00002240 GEN_ERROR("Alias name cannot be empty");
Eric Christopher2a5196f2008-09-24 04:55:49 +00002241
Anton Korobeynikova80e1182007-04-28 13:45:00 +00002242 Constant* Aliasee = $5;
2243 if (Aliasee == 0)
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002244 GEN_ERROR(std::string("Invalid aliasee for alias: ") + Name);
Anton Korobeynikova80e1182007-04-28 13:45:00 +00002245
2246 GlobalAlias* GA = new GlobalAlias(Aliasee->getType(), $4, Name, Aliasee,
2247 CurModule.CurrentModule);
2248 GA->setVisibility($2);
2249 InsertValue(GA, CurModule.Values);
Eric Christopher2a5196f2008-09-24 04:55:49 +00002250
2251
Chris Lattnere5434242007-09-10 23:23:53 +00002252 // If there was a forward reference of this alias, resolve it now.
Eric Christopher2a5196f2008-09-24 04:55:49 +00002253
Chris Lattnere5434242007-09-10 23:23:53 +00002254 ValID ID;
2255 if (!Name.empty())
2256 ID = ValID::createGlobalName(Name);
2257 else
2258 ID = ValID::createGlobalID(CurModule.Values.size()-1);
Eric Christopher2a5196f2008-09-24 04:55:49 +00002259
Chris Lattnere5434242007-09-10 23:23:53 +00002260 if (GlobalValue *FWGV =
2261 CurModule.GetForwardRefForGlobal(GA->getType(), ID)) {
2262 // Replace uses of the fwdref with the actual alias.
2263 FWGV->replaceAllUsesWith(GA);
2264 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(FWGV))
2265 GV->eraseFromParent();
2266 else
2267 cast<Function>(FWGV)->eraseFromParent();
2268 }
2269 ID.destroy();
Eric Christopher2a5196f2008-09-24 04:55:49 +00002270
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00002271 CHECK_FOR_ERROR
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00002272 }
Eric Christopher2a5196f2008-09-24 04:55:49 +00002273 | TARGET TargetDefinition {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002274 CHECK_FOR_ERROR
2275 }
Reid Spencerb951bc02006-12-29 20:29:48 +00002276 | DEPLIBS '=' LibrariesDefinition {
Reid Spencer61c83e02006-08-18 08:43:06 +00002277 CHECK_FOR_ERROR
Chris Lattnere98dda62001-07-14 06:10:16 +00002278 }
Reid Spencerb951bc02006-12-29 20:29:48 +00002279 ;
Chris Lattner00950542001-06-06 20:29:01 +00002280
2281
Chris Lattneree454772006-01-23 23:05:15 +00002282AsmBlock : STRINGCONSTANT {
Chris Lattner66316012006-01-24 04:14:29 +00002283 const std::string &AsmSoFar = CurModule.CurrentModule->getModuleInlineAsm();
Chris Lattneree454772006-01-23 23:05:15 +00002284 if (AsmSoFar.empty())
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002285 CurModule.CurrentModule->setModuleInlineAsm(*$1);
Chris Lattneree454772006-01-23 23:05:15 +00002286 else
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002287 CurModule.CurrentModule->setModuleInlineAsm(AsmSoFar+"\n"+*$1);
2288 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002289 CHECK_FOR_ERROR
Chris Lattneree454772006-01-23 23:05:15 +00002290};
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00002291
Reid Spencerb2d17862007-01-26 08:04:51 +00002292TargetDefinition : TRIPLE '=' STRINGCONSTANT {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002293 CurModule.CurrentModule->setTargetTriple(*$3);
2294 delete $3;
John Criswell2f6a8b12006-10-24 19:09:48 +00002295 }
Chris Lattner10b27112006-10-22 06:07:41 +00002296 | DATALAYOUT '=' STRINGCONSTANT {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002297 CurModule.CurrentModule->setDataLayout(*$3);
2298 delete $3;
Owen Andersoncf7ff2b2006-10-18 02:21:12 +00002299 };
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00002300
Reid Spencerfeaf10e2004-07-25 21:30:51 +00002301LibrariesDefinition : '[' LibList ']';
Reid Spencer0be13e72004-07-25 17:58:28 +00002302
2303LibList : LibList ',' STRINGCONSTANT {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002304 CurModule.CurrentModule->addLibrary(*$3);
2305 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002306 CHECK_FOR_ERROR
Reid Spencer0be13e72004-07-25 17:58:28 +00002307 }
2308 | STRINGCONSTANT {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002309 CurModule.CurrentModule->addLibrary(*$1);
2310 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002311 CHECK_FOR_ERROR
Reid Spencer0be13e72004-07-25 17:58:28 +00002312 }
2313 | /* empty: end of list */ {
Reid Spencer61c83e02006-08-18 08:43:06 +00002314 CHECK_FOR_ERROR
Reid Spencer0be13e72004-07-25 17:58:28 +00002315 }
2316 ;
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00002317
Chris Lattner00950542001-06-06 20:29:01 +00002318//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00002319// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00002320//===----------------------------------------------------------------------===//
2321
Devang Patel05988662008-09-25 21:00:45 +00002322ArgListH : ArgListH ',' Types OptAttributes OptLocalName {
Reid Spencere1553cc2006-12-31 05:40:12 +00002323 if (!UpRefs.empty())
2324 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Dan Gohman9c531cd2008-05-23 18:23:11 +00002325 if (!(*$3)->isFirstClassType())
2326 GEN_ERROR("Argument types must be first-class");
Reid Spencere1553cc2006-12-31 05:40:12 +00002327 ArgListEntry E; E.Attrs = $4; E.Ty = $3; E.Name = $5;
Chris Lattner69da5cf2002-10-13 20:57:00 +00002328 $$ = $1;
Reid Spencere1553cc2006-12-31 05:40:12 +00002329 $1->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002330 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002331 }
Devang Patel05988662008-09-25 21:00:45 +00002332 | Types OptAttributes OptLocalName {
Reid Spencere1553cc2006-12-31 05:40:12 +00002333 if (!UpRefs.empty())
2334 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Dan Gohman9c531cd2008-05-23 18:23:11 +00002335 if (!(*$1)->isFirstClassType())
2336 GEN_ERROR("Argument types must be first-class");
Reid Spencere1553cc2006-12-31 05:40:12 +00002337 ArgListEntry E; E.Attrs = $2; E.Ty = $1; E.Name = $3;
2338 $$ = new ArgListType;
2339 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002340 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002341 };
Chris Lattner00950542001-06-06 20:29:01 +00002342
2343ArgList : ArgListH {
2344 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002345 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002346 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00002347 | ArgListH ',' DOTDOTDOT {
2348 $$ = $1;
Reid Spencere1553cc2006-12-31 05:40:12 +00002349 struct ArgListEntry E;
2350 E.Ty = new PATypeHolder(Type::VoidTy);
2351 E.Name = 0;
Devang Patel05988662008-09-25 21:00:45 +00002352 E.Attrs = Attribute::None;
Reid Spencere1553cc2006-12-31 05:40:12 +00002353 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002354 CHECK_FOR_ERROR
Chris Lattner69da5cf2002-10-13 20:57:00 +00002355 }
2356 | DOTDOTDOT {
Reid Spencere1553cc2006-12-31 05:40:12 +00002357 $$ = new ArgListType;
2358 struct ArgListEntry E;
2359 E.Ty = new PATypeHolder(Type::VoidTy);
2360 E.Name = 0;
Devang Patel05988662008-09-25 21:00:45 +00002361 E.Attrs = Attribute::None;
Reid Spencere1553cc2006-12-31 05:40:12 +00002362 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002363 CHECK_FOR_ERROR
Chris Lattner69da5cf2002-10-13 20:57:00 +00002364 }
Chris Lattner00950542001-06-06 20:29:01 +00002365 | /* empty */ {
2366 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00002367 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002368 };
Chris Lattner00950542001-06-06 20:29:01 +00002369
Devang Patel652203f2008-09-29 20:49:50 +00002370FunctionHeaderH : OptCallingConv OptRetAttrs ResultTypes GlobalName '(' ArgList ')'
Devang Patel2c9c3e72008-09-26 23:51:19 +00002371 OptFuncAttrs OptSection OptAlign OptGC {
Devang Patel652203f2008-09-29 20:49:50 +00002372 std::string FunctionName(*$4);
2373 delete $4; // Free strdup'd memory!
Eric Christopher2a5196f2008-09-24 04:55:49 +00002374
Reid Spencer98b3c5c2007-01-02 21:53:43 +00002375 // Check the function result for abstractness if this is a define. We should
2376 // have no abstract types at this point
Devang Patel652203f2008-09-29 20:49:50 +00002377 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved($3))
2378 GEN_ERROR("Reference to abstract result: "+ $3->get()->getDescription());
Reid Spencer98b3c5c2007-01-02 21:53:43 +00002379
Devang Patel652203f2008-09-29 20:49:50 +00002380 if (!FunctionType::isValidReturnType(*$3))
Chris Lattnerf7dedf22008-04-23 05:36:58 +00002381 GEN_ERROR("Invalid result type for LLVM function");
Eric Christopher2a5196f2008-09-24 04:55:49 +00002382
Chris Lattner9232b992003-04-22 18:42:41 +00002383 std::vector<const Type*> ParamTypeList;
Devang Patel05988662008-09-25 21:00:45 +00002384 SmallVector<AttributeWithIndex, 8> Attrs;
Devang Patel19c87462008-09-26 22:53:05 +00002385 //FIXME : In 3.0, stop accepting zext, sext and inreg as optional function
2386 //attributes.
Devang Patel652203f2008-09-29 20:49:50 +00002387 Attributes RetAttrs = $2;
2388 if ($8 != Attribute::None) {
2389 if ($8 & Attribute::ZExt) {
Devang Patel19c87462008-09-26 22:53:05 +00002390 RetAttrs = RetAttrs | Attribute::ZExt;
Devang Patel652203f2008-09-29 20:49:50 +00002391 $8 = $8 ^ Attribute::ZExt;
Devang Patel19c87462008-09-26 22:53:05 +00002392 }
Devang Patel652203f2008-09-29 20:49:50 +00002393 if ($8 & Attribute::SExt) {
Devang Patel19c87462008-09-26 22:53:05 +00002394 RetAttrs = RetAttrs | Attribute::SExt;
Devang Patel652203f2008-09-29 20:49:50 +00002395 $8 = $8 ^ Attribute::SExt;
Devang Patel19c87462008-09-26 22:53:05 +00002396 }
Devang Patel652203f2008-09-29 20:49:50 +00002397 if ($8 & Attribute::InReg) {
Devang Patel19c87462008-09-26 22:53:05 +00002398 RetAttrs = RetAttrs | Attribute::InReg;
Devang Patel652203f2008-09-29 20:49:50 +00002399 $8 = $8 ^ Attribute::InReg;
Devang Patel19c87462008-09-26 22:53:05 +00002400 }
Devang Patel19c87462008-09-26 22:53:05 +00002401 }
Devang Patel652203f2008-09-29 20:49:50 +00002402 if (RetAttrs != Attribute::None)
2403 Attrs.push_back(AttributeWithIndex::get(0, RetAttrs));
2404 if ($6) { // If there are arguments...
Reid Spencer5694b6e2007-04-09 06:17:21 +00002405 unsigned index = 1;
Devang Patel652203f2008-09-29 20:49:50 +00002406 for (ArgListType::iterator I = $6->begin(); I != $6->end(); ++I, ++index) {
Reid Spencere1553cc2006-12-31 05:40:12 +00002407 const Type* Ty = I->Ty->get();
Reid Spencer98b3c5c2007-01-02 21:53:43 +00002408 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved(I->Ty))
2409 GEN_ERROR("Reference to abstract argument: " + Ty->getDescription());
Reid Spencere1553cc2006-12-31 05:40:12 +00002410 ParamTypeList.push_back(Ty);
Devang Patel05988662008-09-25 21:00:45 +00002411 if (Ty != Type::VoidTy && I->Attrs != Attribute::None)
2412 Attrs.push_back(AttributeWithIndex::get(index, I->Attrs));
Reid Spencere1553cc2006-12-31 05:40:12 +00002413 }
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002414 }
Devang Patel652203f2008-09-29 20:49:50 +00002415 if ($8 != Attribute::None)
2416 Attrs.push_back(AttributeWithIndex::get(~0, $8));
Chris Lattner00950542001-06-06 20:29:01 +00002417
Chris Lattner2079fde2001-10-13 06:41:08 +00002418 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
2419 if (isVarArg) ParamTypeList.pop_back();
2420
Devang Patel05988662008-09-25 21:00:45 +00002421 AttrListPtr PAL;
Reid Spencer4f859aa2007-04-22 05:46:44 +00002422 if (!Attrs.empty())
Devang Patel05988662008-09-25 21:00:45 +00002423 PAL = AttrListPtr::get(Attrs.begin(), Attrs.end());
Reid Spencer5694b6e2007-04-09 06:17:21 +00002424
Devang Patel652203f2008-09-29 20:49:50 +00002425 FunctionType *FT = FunctionType::get(*$3, ParamTypeList, isVarArg);
Christopher Lamb43ad6b32007-12-17 01:12:55 +00002426 const PointerType *PFT = PointerType::getUnqual(FT);
Devang Patel652203f2008-09-29 20:49:50 +00002427 delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00002428
Chris Lattnera09000d2004-07-14 23:03:46 +00002429 ValID ID;
2430 if (!FunctionName.empty()) {
Reid Spencerb2d17862007-01-26 08:04:51 +00002431 ID = ValID::createGlobalName((char*)FunctionName.c_str());
Chris Lattnera09000d2004-07-14 23:03:46 +00002432 } else {
Reid Spencer186a43f2007-03-19 18:39:36 +00002433 ID = ValID::createGlobalID(CurModule.Values.size());
Chris Lattnera09000d2004-07-14 23:03:46 +00002434 }
2435
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00002436 Function *Fn = 0;
Chris Lattnera09000d2004-07-14 23:03:46 +00002437 // See if this function was forward referenced. If so, recycle the object.
Reid Spencer863dd882007-04-28 16:06:50 +00002438 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
Eric Christopher2a5196f2008-09-24 04:55:49 +00002439 // Move the function to the end of the list, from whereever it was
Chris Lattnera09000d2004-07-14 23:03:46 +00002440 // previously inserted.
2441 Fn = cast<Function>(FWRef);
Devang Patel05988662008-09-25 21:00:45 +00002442 assert(Fn->getAttributes().isEmpty() &&
Chris Lattner58d74912008-03-12 17:45:29 +00002443 "Forward reference has parameter attributes!");
Chris Lattnera09000d2004-07-14 23:03:46 +00002444 CurModule.CurrentModule->getFunctionList().remove(Fn);
2445 CurModule.CurrentModule->getFunctionList().push_back(Fn);
2446 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
Reid Spenceref9b9a72007-02-05 20:47:22 +00002447 (Fn = CurModule.CurrentModule->getFunction(FunctionName))) {
Duncan Sandsdc024672007-11-27 13:23:08 +00002448 if (Fn->getFunctionType() != FT ) {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002449 // The existing function doesn't have the same type. This is an overload
2450 // error.
2451 GEN_ERROR("Overload of function '" + FunctionName + "' not permitted.");
Devang Patel05988662008-09-25 21:00:45 +00002452 } else if (Fn->getAttributes() != PAL) {
Duncan Sandsdc024672007-11-27 13:23:08 +00002453 // The existing function doesn't have the same parameter attributes.
2454 // This is an overload error.
2455 GEN_ERROR("Overload of function '" + FunctionName + "' not permitted.");
Reid Spenceref9b9a72007-02-05 20:47:22 +00002456 } else if (!CurFun.isDeclare && !Fn->isDeclaration()) {
2457 // Neither the existing or the current function is a declaration and they
2458 // have the same name and same type. Clearly this is a redefinition.
Reid Spencerf4fa5902007-02-05 10:16:10 +00002459 GEN_ERROR("Redefinition of function '" + FunctionName + "'");
Duncan Sandsdc024672007-11-27 13:23:08 +00002460 } else if (Fn->isDeclaration()) {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002461 // Make sure to strip off any argument names so we can't get conflicts.
Chris Lattnere4d5c442005-03-15 04:54:21 +00002462 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
Chris Lattnera09000d2004-07-14 23:03:46 +00002463 AI != AE; ++AI)
2464 AI->setName("");
Reid Spenceref9b9a72007-02-05 20:47:22 +00002465 }
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00002466 } else { // Not already defined?
Gabor Greif051a9502008-04-06 20:25:17 +00002467 Fn = Function::Create(FT, GlobalValue::ExternalWeakLinkage, FunctionName,
2468 CurModule.CurrentModule);
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00002469 InsertValue(Fn, CurModule.Values);
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00002470 }
Chris Lattner00950542001-06-06 20:29:01 +00002471
Nuno Lopes06a62882008-10-03 15:44:21 +00002472 ID.destroy();
Chris Lattner394f2eb2003-10-16 20:12:13 +00002473 CurFun.FunctionStart(Fn);
Anton Korobeynikov93c2b372006-09-17 13:06:18 +00002474
2475 if (CurFun.isDeclare) {
2476 // If we have declaration, always overwrite linkage. This will allow us to
2477 // correctly handle cases, when pointer to function is passed as argument to
2478 // another function.
2479 Fn->setLinkage(CurFun.Linkage);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002480 Fn->setVisibility(CurFun.Visibility);
Anton Korobeynikov93c2b372006-09-17 13:06:18 +00002481 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00002482 Fn->setCallingConv($1);
Devang Patel05988662008-09-25 21:00:45 +00002483 Fn->setAttributes(PAL);
Devang Patel652203f2008-09-29 20:49:50 +00002484 Fn->setAlignment($10);
2485 if ($9) {
2486 Fn->setSection(*$9);
2487 delete $9;
Chris Lattner164c3782005-11-12 00:11:10 +00002488 }
Devang Patel652203f2008-09-29 20:49:50 +00002489 if ($11) {
2490 Fn->setGC($11->c_str());
2491 delete $11;
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00002492 }
Chris Lattner00950542001-06-06 20:29:01 +00002493
Chris Lattner7e708292002-06-25 16:13:24 +00002494 // Add all of the arguments we parsed to the function...
Devang Patel652203f2008-09-29 20:49:50 +00002495 if ($6) { // Is null if empty...
Chris Lattner69da5cf2002-10-13 20:57:00 +00002496 if (isVarArg) { // Nuke the last entry
Devang Patel652203f2008-09-29 20:49:50 +00002497 assert($6->back().Ty->get() == Type::VoidTy && $6->back().Name == 0 &&
Reid Spencer1755a9a2007-02-05 17:01:20 +00002498 "Not a varargs marker!");
Devang Patel652203f2008-09-29 20:49:50 +00002499 delete $6->back().Ty;
2500 $6->pop_back(); // Delete the last entry
Chris Lattner69da5cf2002-10-13 20:57:00 +00002501 }
Chris Lattnere4d5c442005-03-15 04:54:21 +00002502 Function::arg_iterator ArgIt = Fn->arg_begin();
Reid Spenceref9b9a72007-02-05 20:47:22 +00002503 Function::arg_iterator ArgEnd = Fn->arg_end();
Reid Spencere1553cc2006-12-31 05:40:12 +00002504 unsigned Idx = 1;
Devang Patel652203f2008-09-29 20:49:50 +00002505 for (ArgListType::iterator I = $6->begin();
2506 I != $6->end() && ArgIt != ArgEnd; ++I, ++ArgIt) {
Reid Spencere1553cc2006-12-31 05:40:12 +00002507 delete I->Ty; // Delete the typeholder...
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002508 setValueName(ArgIt, I->Name); // Insert arg into symtab...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002509 CHECK_FOR_ERROR
Chris Lattner69da5cf2002-10-13 20:57:00 +00002510 InsertValue(ArgIt);
Reid Spencere1553cc2006-12-31 05:40:12 +00002511 Idx++;
Chris Lattner00950542001-06-06 20:29:01 +00002512 }
Reid Spencer08de34b2006-12-03 05:45:44 +00002513
Devang Patel652203f2008-09-29 20:49:50 +00002514 delete $6; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00002515 }
Reid Spencer61c83e02006-08-18 08:43:06 +00002516 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002517};
Chris Lattner00950542001-06-06 20:29:01 +00002518
Chris Lattner9b02cc32002-05-03 18:23:48 +00002519BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
2520
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002521FunctionHeader : FunctionDefineLinkage GVVisibilityStyle FunctionHeaderH BEGIN {
Chris Lattner394f2eb2003-10-16 20:12:13 +00002522 $$ = CurFun.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00002523
Chris Lattner4ad02e72003-04-16 20:28:45 +00002524 // Make sure that we keep track of the linkage type even if there was a
2525 // previous "declare".
2526 $$->setLinkage($1);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002527 $$->setVisibility($2);
Chris Lattner51727be2002-06-04 21:58:56 +00002528};
Chris Lattner00950542001-06-06 20:29:01 +00002529
Chris Lattner9b02cc32002-05-03 18:23:48 +00002530END : ENDTOK | '}'; // Allow end of '}' to end a function
2531
Chris Lattner79df7c02002-03-26 18:01:55 +00002532Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00002533 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002534 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002535};
Chris Lattner00950542001-06-06 20:29:01 +00002536
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002537FunctionProto : FunctionDeclareLinkage GVVisibilityStyle FunctionHeaderH {
Reid Spencere1553cc2006-12-31 05:40:12 +00002538 CurFun.CurrentFunction->setLinkage($1);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002539 CurFun.CurrentFunction->setVisibility($2);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002540 $$ = CurFun.CurrentFunction;
2541 CurFun.FunctionDone();
2542 CHECK_FOR_ERROR
2543 };
Chris Lattner00950542001-06-06 20:29:01 +00002544
2545//===----------------------------------------------------------------------===//
2546// Rules to match Basic Blocks
2547//===----------------------------------------------------------------------===//
2548
Chris Lattneraa2c8532006-01-25 22:26:43 +00002549OptSideEffect : /* empty */ {
2550 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002551 CHECK_FOR_ERROR
Chris Lattneraa2c8532006-01-25 22:26:43 +00002552 }
2553 | SIDEEFFECT {
2554 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002555 CHECK_FOR_ERROR
Chris Lattneraa2c8532006-01-25 22:26:43 +00002556 };
2557
Chris Lattner00950542001-06-06 20:29:01 +00002558ConstValueRef : ESINT64VAL { // A reference to a direct constant
2559 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002560 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002561 }
2562 | EUINT64VAL {
2563 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002564 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002565 }
Chris Lattnerd1599012008-07-11 00:30:06 +00002566 | ESAPINTVAL { // arbitrary precision integer constants
2567 $$ = ValID::create(*$1, true);
2568 delete $1;
2569 CHECK_FOR_ERROR
Eric Christopher2a5196f2008-09-24 04:55:49 +00002570 }
Chris Lattnerd1599012008-07-11 00:30:06 +00002571 | EUAPINTVAL { // arbitrary precision integer constants
2572 $$ = ValID::create(*$1, false);
2573 delete $1;
2574 CHECK_FOR_ERROR
2575 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00002576 | FPVAL { // Perhaps it's an FP constant?
2577 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002578 CHECK_FOR_ERROR
Chris Lattner3d52b2f2001-07-15 00:17:01 +00002579 }
Chris Lattner91ef4602004-03-31 03:49:47 +00002580 | TRUETOK {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00002581 $$ = ValID::create(ConstantInt::getTrue());
Reid Spencer61c83e02006-08-18 08:43:06 +00002582 CHECK_FOR_ERROR
Eric Christopher2a5196f2008-09-24 04:55:49 +00002583 }
Chris Lattner91ef4602004-03-31 03:49:47 +00002584 | FALSETOK {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00002585 $$ = ValID::create(ConstantInt::getFalse());
Reid Spencer61c83e02006-08-18 08:43:06 +00002586 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002587 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00002588 | NULL_TOK {
2589 $$ = ValID::createNull();
Reid Spencer61c83e02006-08-18 08:43:06 +00002590 CHECK_FOR_ERROR
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002591 }
Chris Lattner16710e92004-10-16 18:17:13 +00002592 | UNDEF {
2593 $$ = ValID::createUndef();
Reid Spencer61c83e02006-08-18 08:43:06 +00002594 CHECK_FOR_ERROR
Chris Lattner16710e92004-10-16 18:17:13 +00002595 }
Chris Lattnerf1f03df2005-12-21 17:53:02 +00002596 | ZEROINITIALIZER { // A vector zero constant.
2597 $$ = ValID::createZeroInit();
Reid Spencer61c83e02006-08-18 08:43:06 +00002598 CHECK_FOR_ERROR
Chris Lattnerf1f03df2005-12-21 17:53:02 +00002599 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00002600 | '<' ConstVector '>' { // Nonempty unsized packed vector
Reid Spencer08de34b2006-12-03 05:45:44 +00002601 const Type *ETy = (*$2)[0]->getType();
Eric Christopher2a5196f2008-09-24 04:55:49 +00002602 unsigned NumElements = $2->size();
Dan Gohman9c531cd2008-05-23 18:23:11 +00002603
2604 if (!ETy->isInteger() && !ETy->isFloatingPoint())
2605 GEN_ERROR("Invalid vector element type: " + ETy->getDescription());
Eric Christopher2a5196f2008-09-24 04:55:49 +00002606
Reid Spencer9d6565a2007-02-15 02:26:10 +00002607 VectorType* pt = VectorType::get(ETy, NumElements);
Dan Gohmanf910eaa2008-06-09 14:45:02 +00002608 PATypeHolder* PTy = new PATypeHolder(HandleUpRefs(pt));
Eric Christopher2a5196f2008-09-24 04:55:49 +00002609
Brian Gaeke715c90b2004-08-20 06:00:58 +00002610 // Verify all elements are correct type!
2611 for (unsigned i = 0; i < $2->size(); i++) {
Reid Spencer08de34b2006-12-03 05:45:44 +00002612 if (ETy != (*$2)[i]->getType())
Eric Christopher2a5196f2008-09-24 04:55:49 +00002613 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Brian Gaeke715c90b2004-08-20 06:00:58 +00002614 ETy->getDescription() +"' as required!\nIt is of type '" +
Reid Spencer08de34b2006-12-03 05:45:44 +00002615 (*$2)[i]->getType()->getDescription() + "'.");
Brian Gaeke715c90b2004-08-20 06:00:58 +00002616 }
2617
Reid Spencer9d6565a2007-02-15 02:26:10 +00002618 $$ = ValID::create(ConstantVector::get(pt, *$2));
Brian Gaeke715c90b2004-08-20 06:00:58 +00002619 delete PTy; delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002620 CHECK_FOR_ERROR
Brian Gaeke715c90b2004-08-20 06:00:58 +00002621 }
Dan Gohmanf910eaa2008-06-09 14:45:02 +00002622 | '[' ConstVector ']' { // Nonempty unsized arr
2623 const Type *ETy = (*$2)[0]->getType();
Eric Christopher2a5196f2008-09-24 04:55:49 +00002624 uint64_t NumElements = $2->size();
Dan Gohmanf910eaa2008-06-09 14:45:02 +00002625
2626 if (!ETy->isFirstClassType())
2627 GEN_ERROR("Invalid array element type: " + ETy->getDescription());
2628
2629 ArrayType *ATy = ArrayType::get(ETy, NumElements);
2630 PATypeHolder* PTy = new PATypeHolder(HandleUpRefs(ATy));
2631
2632 // Verify all elements are correct type!
2633 for (unsigned i = 0; i < $2->size(); i++) {
2634 if (ETy != (*$2)[i]->getType())
Eric Christopher2a5196f2008-09-24 04:55:49 +00002635 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Dan Gohmanf910eaa2008-06-09 14:45:02 +00002636 ETy->getDescription() +"' as required!\nIt is of type '"+
2637 (*$2)[i]->getType()->getDescription() + "'.");
2638 }
2639
2640 $$ = ValID::create(ConstantArray::get(ATy, *$2));
2641 delete PTy; delete $2;
2642 CHECK_FOR_ERROR
2643 }
2644 | '[' ']' {
Dan Gohmanebb5a972008-06-23 18:40:28 +00002645 // Use undef instead of an array because it's inconvenient to determine
2646 // the element type at this point, there being no elements to examine.
Dan Gohmanf910eaa2008-06-09 14:45:02 +00002647 $$ = ValID::createUndef();
2648 CHECK_FOR_ERROR
2649 }
2650 | 'c' STRINGCONSTANT {
Dan Gohmanebb5a972008-06-23 18:40:28 +00002651 uint64_t NumElements = $2->length();
Dan Gohmanf910eaa2008-06-09 14:45:02 +00002652 const Type *ETy = Type::Int8Ty;
2653
2654 ArrayType *ATy = ArrayType::get(ETy, NumElements);
2655
2656 std::vector<Constant*> Vals;
2657 for (unsigned i = 0; i < $2->length(); ++i)
2658 Vals.push_back(ConstantInt::get(ETy, (*$2)[i]));
2659 delete $2;
2660 $$ = ValID::create(ConstantArray::get(ATy, Vals));
2661 CHECK_FOR_ERROR
2662 }
2663 | '{' ConstVector '}' {
2664 std::vector<const Type*> Elements($2->size());
2665 for (unsigned i = 0, e = $2->size(); i != e; ++i)
2666 Elements[i] = (*$2)[i]->getType();
2667
2668 const StructType *STy = StructType::get(Elements);
2669 PATypeHolder* PTy = new PATypeHolder(HandleUpRefs(STy));
2670
2671 $$ = ValID::create(ConstantStruct::get(STy, *$2));
2672 delete PTy; delete $2;
2673 CHECK_FOR_ERROR
2674 }
2675 | '{' '}' {
2676 const StructType *STy = StructType::get(std::vector<const Type*>());
2677 $$ = ValID::create(ConstantStruct::get(STy, std::vector<Constant*>()));
2678 CHECK_FOR_ERROR
2679 }
2680 | '<' '{' ConstVector '}' '>' {
2681 std::vector<const Type*> Elements($3->size());
2682 for (unsigned i = 0, e = $3->size(); i != e; ++i)
2683 Elements[i] = (*$3)[i]->getType();
2684
2685 const StructType *STy = StructType::get(Elements, /*isPacked=*/true);
2686 PATypeHolder* PTy = new PATypeHolder(HandleUpRefs(STy));
2687
2688 $$ = ValID::create(ConstantStruct::get(STy, *$3));
2689 delete PTy; delete $3;
2690 CHECK_FOR_ERROR
2691 }
2692 | '<' '{' '}' '>' {
2693 const StructType *STy = StructType::get(std::vector<const Type*>(),
2694 /*isPacked=*/true);
2695 $$ = ValID::create(ConstantStruct::get(STy, std::vector<Constant*>()));
2696 CHECK_FOR_ERROR
2697 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00002698 | ConstExpr {
Reid Spencer08de34b2006-12-03 05:45:44 +00002699 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002700 CHECK_FOR_ERROR
Chris Lattneraa2c8532006-01-25 22:26:43 +00002701 }
2702 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002703 $$ = ValID::createInlineAsm(*$3, *$5, $2);
2704 delete $3;
2705 delete $5;
Reid Spencer61c83e02006-08-18 08:43:06 +00002706 CHECK_FOR_ERROR
Chris Lattnerd78700d2002-08-16 21:14:40 +00002707 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00002708
Chris Lattner2079fde2001-10-13 06:41:08 +00002709// SymbolicValueRef - Reference to one of two ways of symbolically refering to
2710// another value.
2711//
Reid Spencerb2d17862007-01-26 08:04:51 +00002712SymbolicValueRef : LOCALVAL_ID { // Is it an integer reference...?
2713 $$ = ValID::createLocalID($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002714 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002715 }
Reid Spencerb2d17862007-01-26 08:04:51 +00002716 | GLOBALVAL_ID {
2717 $$ = ValID::createGlobalID($1);
2718 CHECK_FOR_ERROR
2719 }
2720 | LocalName { // Is it a named reference...?
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002721 $$ = ValID::createLocalName(*$1);
2722 delete $1;
Reid Spencerb2d17862007-01-26 08:04:51 +00002723 CHECK_FOR_ERROR
2724 }
2725 | GlobalName { // Is it a named reference...?
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002726 $$ = ValID::createGlobalName(*$1);
2727 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002728 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002729 };
Chris Lattner2079fde2001-10-13 06:41:08 +00002730
2731// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00002732ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00002733
Chris Lattner00950542001-06-06 20:29:01 +00002734
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002735// ResolvedVal - a <type> <value> pair. This is used only in cases where the
2736// type immediately preceeds the value reference, and allows complex constant
2737// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00002738ResolvedVal : Types ValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00002739 if (!UpRefs.empty())
2740 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Eric Christopher2a5196f2008-09-24 04:55:49 +00002741 $$ = getVal(*$1, $2);
Reid Spencere1553cc2006-12-31 05:40:12 +00002742 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002743 CHECK_FOR_ERROR
Reid Spencere1553cc2006-12-31 05:40:12 +00002744 }
2745 ;
Chris Lattner8b81bf52001-07-25 22:47:46 +00002746
Devang Patel5af2f632008-02-20 22:39:45 +00002747ReturnedVal : ResolvedVal {
2748 $$ = new std::vector<Value *>();
Eric Christopher2a5196f2008-09-24 04:55:49 +00002749 $$->push_back($1);
Devang Patel5af2f632008-02-20 22:39:45 +00002750 CHECK_FOR_ERROR
2751 }
Devang Patel57ef4f42008-02-23 00:35:18 +00002752 | ReturnedVal ',' ResolvedVal {
Eric Christopher2a5196f2008-09-24 04:55:49 +00002753 ($$=$1)->push_back($3);
Devang Patel5af2f632008-02-20 22:39:45 +00002754 CHECK_FOR_ERROR
2755 };
2756
Chris Lattner00950542001-06-06 20:29:01 +00002757BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner8ab406d2004-07-13 07:59:27 +00002758 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002759 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002760 }
Eric Christopher2a5196f2008-09-24 04:55:49 +00002761 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
Chris Lattner8ab406d2004-07-13 07:59:27 +00002762 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002763 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002764 };
Chris Lattner00950542001-06-06 20:29:01 +00002765
2766
Eric Christopher2a5196f2008-09-24 04:55:49 +00002767// Basic blocks are terminated by branching instructions:
Chris Lattner00950542001-06-06 20:29:01 +00002768// br, br/cc, switch, ret
2769//
Chris Lattner6ac28092008-08-29 17:12:13 +00002770BasicBlock : InstructionList OptLocalAssign BBTerminatorInst {
Chris Lattner8ab406d2004-07-13 07:59:27 +00002771 setValueName($3, $2);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002772 CHECK_FOR_ERROR
Chris Lattner2079fde2001-10-13 06:41:08 +00002773 InsertValue($3);
Chris Lattner2079fde2001-10-13 06:41:08 +00002774 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00002775 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002776 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002777 };
Chris Lattner00950542001-06-06 20:29:01 +00002778
Chris Lattner6ac28092008-08-29 17:12:13 +00002779BasicBlock : InstructionList LocalNumber BBTerminatorInst {
2780 CHECK_FOR_ERROR
2781 int ValNum = InsertValue($3);
2782 if (ValNum != (int)$2)
2783 GEN_ERROR("Result value number %" + utostr($2) +
2784 " is incorrect, expected %" + utostr((unsigned)ValNum));
Eric Christopher2a5196f2008-09-24 04:55:49 +00002785
Chris Lattner6ac28092008-08-29 17:12:13 +00002786 $1->getInstList().push_back($3);
2787 $$ = $1;
2788 CHECK_FOR_ERROR
2789};
2790
2791
Chris Lattner00950542001-06-06 20:29:01 +00002792InstructionList : InstructionList Inst {
Reid Spencer3da59db2006-11-27 01:05:10 +00002793 if (CastInst *CI1 = dyn_cast<CastInst>($2))
2794 if (CastInst *CI2 = dyn_cast<CastInst>(CI1->getOperand(0)))
2795 if (CI2->getParent() == 0)
2796 $1->getInstList().push_back(CI2);
Chris Lattner00950542001-06-06 20:29:01 +00002797 $1->getInstList().push_back($2);
2798 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002799 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002800 }
Reid Spencer186a43f2007-03-19 18:39:36 +00002801 | /* empty */ { // Empty space between instruction lists
Nick Lewycky280a6e62008-04-25 16:53:59 +00002802 $$ = defineBBVal(ValID::createLocalID(CurFun.NextValNum));
Reid Spencer61c83e02006-08-18 08:43:06 +00002803 CHECK_FOR_ERROR
Chris Lattner22ae2322004-07-13 08:39:15 +00002804 }
Reid Spencer186a43f2007-03-19 18:39:36 +00002805 | LABELSTR { // Labelled (named) basic block
Nick Lewycky280a6e62008-04-25 16:53:59 +00002806 $$ = defineBBVal(ValID::createLocalName(*$1));
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002807 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002808 CHECK_FOR_ERROR
Nick Lewycky280a6e62008-04-25 16:53:59 +00002809
Chris Lattner51727be2002-06-04 21:58:56 +00002810 };
Chris Lattner00950542001-06-06 20:29:01 +00002811
Eric Christopher2a5196f2008-09-24 04:55:49 +00002812BBTerminatorInst :
Devang Patel5af2f632008-02-20 22:39:45 +00002813 RET ReturnedVal { // Return with a result...
Devang Patel53284d32008-02-26 22:12:58 +00002814 ValueList &VL = *$2;
Devang Patel43a1bb32008-02-26 23:17:50 +00002815 assert(!VL.empty() && "Invalid ret operands!");
Dan Gohmanfc74abf2008-07-23 00:34:11 +00002816 const Type *ReturnType = CurFun.CurrentFunction->getReturnType();
2817 if (VL.size() > 1 ||
2818 (isa<StructType>(ReturnType) &&
2819 (VL.empty() || VL[0]->getType() != ReturnType))) {
2820 Value *RV = UndefValue::get(ReturnType);
2821 for (unsigned i = 0, e = VL.size(); i != e; ++i) {
2822 Instruction *I = InsertValueInst::Create(RV, VL[i], i, "mrv");
2823 ($<BasicBlockVal>-1)->getInstList().push_back(I);
2824 RV = I;
2825 }
2826 $$ = ReturnInst::Create(RV);
2827 } else {
2828 $$ = ReturnInst::Create(VL[0]);
2829 }
Devang Patel5af2f632008-02-20 22:39:45 +00002830 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002831 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002832 }
Reid Spencer186a43f2007-03-19 18:39:36 +00002833 | RET VOID { // Return with no result...
Gabor Greif051a9502008-04-06 20:25:17 +00002834 $$ = ReturnInst::Create();
Reid Spencer61c83e02006-08-18 08:43:06 +00002835 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002836 }
Reid Spencer186a43f2007-03-19 18:39:36 +00002837 | BR LABEL ValueRef { // Unconditional Branch...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002838 BasicBlock* tmpBB = getBBVal($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002839 CHECK_FOR_ERROR
Gabor Greif051a9502008-04-06 20:25:17 +00002840 $$ = BranchInst::Create(tmpBB);
Reid Spencer186a43f2007-03-19 18:39:36 +00002841 } // Conditional Branch...
Eric Christopher2a5196f2008-09-24 04:55:49 +00002842 | BR INTTYPE ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Dan Gohman9c531cd2008-05-23 18:23:11 +00002843 if (cast<IntegerType>($2)->getBitWidth() != 1)
2844 GEN_ERROR("Branch condition must have type i1");
Reid Spencer5b7e7532006-09-28 19:28:24 +00002845 BasicBlock* tmpBBA = getBBVal($6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002846 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002847 BasicBlock* tmpBBB = getBBVal($9);
2848 CHECK_FOR_ERROR
Reid Spencer4fe16d62007-01-11 18:21:29 +00002849 Value* tmpVal = getVal(Type::Int1Ty, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002850 CHECK_FOR_ERROR
Gabor Greif051a9502008-04-06 20:25:17 +00002851 $$ = BranchInst::Create(tmpBBA, tmpBBB, tmpVal);
Chris Lattner00950542001-06-06 20:29:01 +00002852 }
Chris Lattner22b92e32008-10-15 06:03:37 +00002853 | SWITCH INTTYPE ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencer08de34b2006-12-03 05:45:44 +00002854 Value* tmpVal = getVal($2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002855 CHECK_FOR_ERROR
2856 BasicBlock* tmpBB = getBBVal($6);
2857 CHECK_FOR_ERROR
Gabor Greif051a9502008-04-06 20:25:17 +00002858 SwitchInst *S = SwitchInst::Create(tmpVal, tmpBB, $8->size());
Chris Lattner00950542001-06-06 20:29:01 +00002859 $$ = S;
2860
Chris Lattner9232b992003-04-22 18:42:41 +00002861 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
Chris Lattner46748042002-04-09 19:41:42 +00002862 E = $8->end();
Chris Lattner69331f52005-02-24 05:25:17 +00002863 for (; I != E; ++I) {
2864 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
2865 S->addCase(CI, I->second);
2866 else
Reid Spencerf4fa5902007-02-05 10:16:10 +00002867 GEN_ERROR("Switch case is constant, but not a simple integer");
Chris Lattner69331f52005-02-24 05:25:17 +00002868 }
Chris Lattnerf57a43d2004-04-17 23:49:15 +00002869 delete $8;
Reid Spencer61c83e02006-08-18 08:43:06 +00002870 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002871 }
Chris Lattner22b92e32008-10-15 06:03:37 +00002872 | SWITCH INTTYPE ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencer08de34b2006-12-03 05:45:44 +00002873 Value* tmpVal = getVal($2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002874 CHECK_FOR_ERROR
2875 BasicBlock* tmpBB = getBBVal($6);
2876 CHECK_FOR_ERROR
Gabor Greif051a9502008-04-06 20:25:17 +00002877 SwitchInst *S = SwitchInst::Create(tmpVal, tmpBB, 0);
Chris Lattner196850c2003-05-15 21:30:00 +00002878 $$ = S;
Reid Spencer61c83e02006-08-18 08:43:06 +00002879 CHECK_FOR_ERROR
Chris Lattner196850c2003-05-15 21:30:00 +00002880 }
Devang Patel652203f2008-09-29 20:49:50 +00002881 | INVOKE OptCallingConv OptRetAttrs ResultTypes ValueRef '(' ParamList ')'
2882 OptFuncAttrs TO LABEL ValueRef UNWIND LABEL ValueRef {
Chris Lattner2079fde2001-10-13 06:41:08 +00002883
Reid Spencere1553cc2006-12-31 05:40:12 +00002884 // Handle the short syntax
2885 const PointerType *PFTy = 0;
2886 const FunctionType *Ty = 0;
Devang Patel652203f2008-09-29 20:49:50 +00002887 if (!(PFTy = dyn_cast<PointerType>($4->get())) ||
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002888 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00002889 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00002890 std::vector<const Type*> ParamTypes;
Devang Patel652203f2008-09-29 20:49:50 +00002891 ParamList::iterator I = $7->begin(), E = $7->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00002892 for (; I != E; ++I) {
Reid Spencere1553cc2006-12-31 05:40:12 +00002893 const Type *Ty = I->Val->getType();
2894 if (Ty == Type::VoidTy)
2895 GEN_ERROR("Short call syntax cannot be used with varargs");
2896 ParamTypes.push_back(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00002897 }
Eric Christopher2a5196f2008-09-24 04:55:49 +00002898
Devang Patel652203f2008-09-29 20:49:50 +00002899 if (!FunctionType::isValidReturnType(*$4))
Chris Lattnerf7dedf22008-04-23 05:36:58 +00002900 GEN_ERROR("Invalid result type for LLVM function");
2901
Devang Patel652203f2008-09-29 20:49:50 +00002902 Ty = FunctionType::get($4->get(), ParamTypes, false);
Christopher Lamb43ad6b32007-12-17 01:12:55 +00002903 PFTy = PointerType::getUnqual(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00002904 }
Chris Lattner2079fde2001-10-13 06:41:08 +00002905
Devang Patel652203f2008-09-29 20:49:50 +00002906 delete $4;
Reid Spencer2c9df212007-03-20 01:13:00 +00002907
Devang Patel652203f2008-09-29 20:49:50 +00002908 Value *V = getVal(PFTy, $5); // Get the function we're calling...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002909 CHECK_FOR_ERROR
Devang Patel652203f2008-09-29 20:49:50 +00002910 BasicBlock *Normal = getBBVal($12);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002911 CHECK_FOR_ERROR
Devang Patel652203f2008-09-29 20:49:50 +00002912 BasicBlock *Except = getBBVal($15);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002913 CHECK_FOR_ERROR
Chris Lattner2079fde2001-10-13 06:41:08 +00002914
Devang Patel05988662008-09-25 21:00:45 +00002915 SmallVector<AttributeWithIndex, 8> Attrs;
Devang Patel19c87462008-09-26 22:53:05 +00002916 //FIXME : In 3.0, stop accepting zext, sext and inreg as optional function
2917 //attributes.
Devang Patel652203f2008-09-29 20:49:50 +00002918 Attributes RetAttrs = $3;
2919 if ($9 != Attribute::None) {
2920 if ($9 & Attribute::ZExt) {
Devang Patel19c87462008-09-26 22:53:05 +00002921 RetAttrs = RetAttrs | Attribute::ZExt;
Devang Patel652203f2008-09-29 20:49:50 +00002922 $9 = $9 ^ Attribute::ZExt;
Devang Patel19c87462008-09-26 22:53:05 +00002923 }
Devang Patel652203f2008-09-29 20:49:50 +00002924 if ($9 & Attribute::SExt) {
Devang Patel19c87462008-09-26 22:53:05 +00002925 RetAttrs = RetAttrs | Attribute::SExt;
Devang Patel652203f2008-09-29 20:49:50 +00002926 $9 = $9 ^ Attribute::SExt;
Devang Patel19c87462008-09-26 22:53:05 +00002927 }
Devang Patel652203f2008-09-29 20:49:50 +00002928 if ($9 & Attribute::InReg) {
Devang Patel19c87462008-09-26 22:53:05 +00002929 RetAttrs = RetAttrs | Attribute::InReg;
Devang Patel652203f2008-09-29 20:49:50 +00002930 $9 = $9 ^ Attribute::InReg;
Devang Patel19c87462008-09-26 22:53:05 +00002931 }
Devang Patel19c87462008-09-26 22:53:05 +00002932 }
Devang Patel652203f2008-09-29 20:49:50 +00002933 if (RetAttrs != Attribute::None)
2934 Attrs.push_back(AttributeWithIndex::get(0, RetAttrs));
Devang Patel19c87462008-09-26 22:53:05 +00002935
Reid Spencere1553cc2006-12-31 05:40:12 +00002936 // Check the arguments
2937 ValueList Args;
Devang Patel652203f2008-09-29 20:49:50 +00002938 if ($7->empty()) { // Has no arguments?
Reid Spencere1553cc2006-12-31 05:40:12 +00002939 // Make sure no arguments is a good thing!
2940 if (Ty->getNumParams() != 0)
2941 GEN_ERROR("No arguments passed to a function that "
Reid Spencerf4fa5902007-02-05 10:16:10 +00002942 "expects arguments");
Chris Lattner2079fde2001-10-13 06:41:08 +00002943 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00002944 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00002945 // correctly!
Chris Lattnerd5d89962004-02-09 04:14:01 +00002946 FunctionType::param_iterator I = Ty->param_begin();
2947 FunctionType::param_iterator E = Ty->param_end();
Devang Patel652203f2008-09-29 20:49:50 +00002948 ParamList::iterator ArgI = $7->begin(), ArgE = $7->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00002949 unsigned index = 1;
Reid Spencer863dd882007-04-28 16:06:50 +00002950
Duncan Sandsdc024672007-11-27 13:23:08 +00002951 for (; ArgI != ArgE && I != E; ++ArgI, ++I, ++index) {
Reid Spencere1553cc2006-12-31 05:40:12 +00002952 if (ArgI->Val->getType() != *I)
2953 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00002954 (*I)->getDescription() + "'");
Reid Spencere1553cc2006-12-31 05:40:12 +00002955 Args.push_back(ArgI->Val);
Devang Patel05988662008-09-25 21:00:45 +00002956 if (ArgI->Attrs != Attribute::None)
2957 Attrs.push_back(AttributeWithIndex::get(index, ArgI->Attrs));
Reid Spencere1553cc2006-12-31 05:40:12 +00002958 }
Reid Spencer08de34b2006-12-03 05:45:44 +00002959
Reid Spencere1553cc2006-12-31 05:40:12 +00002960 if (Ty->isVarArg()) {
2961 if (I == E)
Duncan Sandseb824702008-01-11 21:23:39 +00002962 for (; ArgI != ArgE; ++ArgI, ++index) {
Reid Spencere1553cc2006-12-31 05:40:12 +00002963 Args.push_back(ArgI->Val); // push the remaining varargs
Devang Patel05988662008-09-25 21:00:45 +00002964 if (ArgI->Attrs != Attribute::None)
2965 Attrs.push_back(AttributeWithIndex::get(index, ArgI->Attrs));
Duncan Sandseb824702008-01-11 21:23:39 +00002966 }
Reid Spencere1553cc2006-12-31 05:40:12 +00002967 } else if (I != E || ArgI != ArgE)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002968 GEN_ERROR("Invalid number of parameters detected");
Chris Lattner2079fde2001-10-13 06:41:08 +00002969 }
Devang Patel652203f2008-09-29 20:49:50 +00002970 if ($9 != Attribute::None)
2971 Attrs.push_back(AttributeWithIndex::get(~0, $9));
Devang Patel05988662008-09-25 21:00:45 +00002972 AttrListPtr PAL;
Duncan Sandsdc024672007-11-27 13:23:08 +00002973 if (!Attrs.empty())
Devang Patel05988662008-09-25 21:00:45 +00002974 PAL = AttrListPtr::get(Attrs.begin(), Attrs.end());
Duncan Sandsdc024672007-11-27 13:23:08 +00002975
Reid Spencere1553cc2006-12-31 05:40:12 +00002976 // Create the InvokeInst
Gabor Greifb1dbcd82008-05-15 10:04:30 +00002977 InvokeInst *II = InvokeInst::Create(V, Normal, Except,
2978 Args.begin(), Args.end());
Reid Spencere1553cc2006-12-31 05:40:12 +00002979 II->setCallingConv($2);
Devang Patel05988662008-09-25 21:00:45 +00002980 II->setAttributes(PAL);
Reid Spencere1553cc2006-12-31 05:40:12 +00002981 $$ = II;
Devang Patel652203f2008-09-29 20:49:50 +00002982 delete $7;
Reid Spencer61c83e02006-08-18 08:43:06 +00002983 CHECK_FOR_ERROR
Chris Lattner36143fc2003-09-08 18:54:55 +00002984 }
2985 | UNWIND {
2986 $$ = new UnwindInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002987 CHECK_FOR_ERROR
Chris Lattner16710e92004-10-16 18:17:13 +00002988 }
2989 | UNREACHABLE {
2990 $$ = new UnreachableInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002991 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002992 };
Chris Lattner2079fde2001-10-13 06:41:08 +00002993
2994
Chris Lattner00950542001-06-06 20:29:01 +00002995
Chris Lattner22b92e32008-10-15 06:03:37 +00002996JumpTable : JumpTable INTTYPE ConstValueRef ',' LABEL ValueRef {
Chris Lattner00950542001-06-06 20:29:01 +00002997 $$ = $1;
Reid Spencer186a43f2007-03-19 18:39:36 +00002998 Constant *V = cast<Constant>(getExistingVal($2, $3));
Reid Spencer5b7e7532006-09-28 19:28:24 +00002999 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00003000 if (V == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00003001 GEN_ERROR("May only switch on a constant pool value");
Chris Lattner00950542001-06-06 20:29:01 +00003002
Reid Spencer5b7e7532006-09-28 19:28:24 +00003003 BasicBlock* tmpBB = getBBVal($6);
Reid Spencer61c83e02006-08-18 08:43:06 +00003004 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00003005 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner00950542001-06-06 20:29:01 +00003006 }
Chris Lattner22b92e32008-10-15 06:03:37 +00003007 | INTTYPE ConstValueRef ',' LABEL ValueRef {
Chris Lattner9232b992003-04-22 18:42:41 +00003008 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Reid Spencer186a43f2007-03-19 18:39:36 +00003009 Constant *V = cast<Constant>(getExistingVal($1, $2));
Reid Spencer5b7e7532006-09-28 19:28:24 +00003010 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00003011
3012 if (V == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00003013 GEN_ERROR("May only switch on a constant pool value");
Chris Lattner00950542001-06-06 20:29:01 +00003014
Reid Spencer5b7e7532006-09-28 19:28:24 +00003015 BasicBlock* tmpBB = getBBVal($5);
Reid Spencer61c83e02006-08-18 08:43:06 +00003016 CHECK_FOR_ERROR
Eric Christopher2a5196f2008-09-24 04:55:49 +00003017 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner51727be2002-06-04 21:58:56 +00003018 };
Chris Lattner00950542001-06-06 20:29:01 +00003019
Reid Spencerb2d17862007-01-26 08:04:51 +00003020Inst : OptLocalAssign InstVal {
Reid Spenceref9b9a72007-02-05 20:47:22 +00003021 // Is this definition named?? if so, assign the name...
3022 setValueName($2, $1);
3023 CHECK_FOR_ERROR
3024 InsertValue($2);
3025 $$ = $2;
3026 CHECK_FOR_ERROR
3027 };
3028
Chris Lattner6ac28092008-08-29 17:12:13 +00003029Inst : LocalNumber InstVal {
3030 CHECK_FOR_ERROR
3031 int ValNum = InsertValue($2);
Eric Christopher2a5196f2008-09-24 04:55:49 +00003032
Chris Lattner6ac28092008-08-29 17:12:13 +00003033 if (ValNum != (int)$1)
3034 GEN_ERROR("Result value number %" + utostr($1) +
3035 " is incorrect, expected %" + utostr((unsigned)ValNum));
3036
3037 $$ = $2;
3038 CHECK_FOR_ERROR
3039 };
3040
Chris Lattner00950542001-06-06 20:29:01 +00003041
Chris Lattnerc24d2082001-06-11 15:04:20 +00003042PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Reid Spencere1553cc2006-12-31 05:40:12 +00003043 if (!UpRefs.empty())
3044 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Chris Lattner9232b992003-04-22 18:42:41 +00003045 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Reid Spencer08de34b2006-12-03 05:45:44 +00003046 Value* tmpVal = getVal(*$1, $3);
Reid Spencer61c83e02006-08-18 08:43:06 +00003047 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00003048 BasicBlock* tmpBB = getBBVal($5);
3049 CHECK_FOR_ERROR
3050 $$->push_back(std::make_pair(tmpVal, tmpBB));
Reid Spencer08de34b2006-12-03 05:45:44 +00003051 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00003052 }
3053 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
3054 $$ = $1;
Reid Spencer5b7e7532006-09-28 19:28:24 +00003055 Value* tmpVal = getVal($1->front().first->getType(), $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00003056 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00003057 BasicBlock* tmpBB = getBBVal($6);
3058 CHECK_FOR_ERROR
3059 $1->push_back(std::make_pair(tmpVal, tmpBB));
Chris Lattner51727be2002-06-04 21:58:56 +00003060 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00003061
3062
Devang Patel05988662008-09-25 21:00:45 +00003063ParamList : Types OptAttributes ValueRef OptAttributes {
3064 // FIXME: Remove trailing OptAttributes in LLVM 3.0, it was a mistake in 2.0
Reid Spencere1553cc2006-12-31 05:40:12 +00003065 if (!UpRefs.empty())
3066 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
3067 // Used for call and invoke instructions
Dale Johanneseneb57ea72007-11-05 21:20:28 +00003068 $$ = new ParamList();
Duncan Sandsdc024672007-11-27 13:23:08 +00003069 ParamListEntry E; E.Attrs = $2 | $4; E.Val = getVal($1->get(), $3);
Reid Spencere1553cc2006-12-31 05:40:12 +00003070 $$->push_back(E);
Reid Spencer2c9df212007-03-20 01:13:00 +00003071 delete $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00003072 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00003073 }
Devang Patel05988662008-09-25 21:00:45 +00003074 | LABEL OptAttributes ValueRef OptAttributes {
3075 // FIXME: Remove trailing OptAttributes in LLVM 3.0, it was a mistake in 2.0
Dale Johanneseneb57ea72007-11-05 21:20:28 +00003076 // Labels are only valid in ASMs
3077 $$ = new ParamList();
Duncan Sandsdc024672007-11-27 13:23:08 +00003078 ParamListEntry E; E.Attrs = $2 | $4; E.Val = getBBVal($3);
Dale Johanneseneb57ea72007-11-05 21:20:28 +00003079 $$->push_back(E);
Duncan Sandsdc024672007-11-27 13:23:08 +00003080 CHECK_FOR_ERROR
Dale Johanneseneb57ea72007-11-05 21:20:28 +00003081 }
Devang Patel05988662008-09-25 21:00:45 +00003082 | ParamList ',' Types OptAttributes ValueRef OptAttributes {
3083 // FIXME: Remove trailing OptAttributes in LLVM 3.0, it was a mistake in 2.0
Reid Spencere1553cc2006-12-31 05:40:12 +00003084 if (!UpRefs.empty())
3085 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattner00950542001-06-06 20:29:01 +00003086 $$ = $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00003087 ParamListEntry E; E.Attrs = $4 | $6; E.Val = getVal($3->get(), $5);
Reid Spencere1553cc2006-12-31 05:40:12 +00003088 $$->push_back(E);
Reid Spencer2c9df212007-03-20 01:13:00 +00003089 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00003090 CHECK_FOR_ERROR
Reid Spencere1553cc2006-12-31 05:40:12 +00003091 }
Devang Patel05988662008-09-25 21:00:45 +00003092 | ParamList ',' LABEL OptAttributes ValueRef OptAttributes {
3093 // FIXME: Remove trailing OptAttributes in LLVM 3.0, it was a mistake in 2.0
Dale Johanneseneb57ea72007-11-05 21:20:28 +00003094 $$ = $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00003095 ParamListEntry E; E.Attrs = $4 | $6; E.Val = getBBVal($5);
Dale Johanneseneb57ea72007-11-05 21:20:28 +00003096 $$->push_back(E);
3097 CHECK_FOR_ERROR
3098 }
3099 | /*empty*/ { $$ = new ParamList(); };
Chris Lattner00950542001-06-06 20:29:01 +00003100
Reid Spencere1553cc2006-12-31 05:40:12 +00003101IndexList // Used for gep instructions and constant expressions
Reid Spencere03969f2006-12-31 21:46:36 +00003102 : /*empty*/ { $$ = new std::vector<Value*>(); }
Reid Spencere1553cc2006-12-31 05:40:12 +00003103 | IndexList ',' ResolvedVal {
3104 $$ = $1;
3105 $$->push_back($3);
3106 CHECK_FOR_ERROR
3107 }
Reid Spencer71305d72006-12-31 21:25:25 +00003108 ;
Chris Lattner00950542001-06-06 20:29:01 +00003109
Dan Gohman81a0c0b2008-05-31 00:58:22 +00003110ConstantIndexList // Used for insertvalue and extractvalue instructions
3111 : ',' EUINT64VAL {
3112 $$ = new std::vector<unsigned>();
3113 if ((unsigned)$2 != $2)
3114 GEN_ERROR("Index " + utostr($2) + " is not valid for insertvalue or extractvalue.");
3115 $$->push_back($2);
3116 }
3117 | ConstantIndexList ',' EUINT64VAL {
3118 $$ = $1;
3119 if ((unsigned)$3 != $3)
3120 GEN_ERROR("Index " + utostr($3) + " is not valid for insertvalue or extractvalue.");
3121 $$->push_back($3);
3122 CHECK_FOR_ERROR
3123 }
3124 ;
3125
Chris Lattnerddb6db42005-05-06 05:51:46 +00003126OptTailCall : TAIL CALL {
3127 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00003128 CHECK_FOR_ERROR
Chris Lattnerddb6db42005-05-06 05:51:46 +00003129 }
3130 | CALL {
3131 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00003132 CHECK_FOR_ERROR
Chris Lattnerddb6db42005-05-06 05:51:46 +00003133 };
3134
Chris Lattner4a6482b2002-09-10 19:57:26 +00003135InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00003136 if (!UpRefs.empty())
3137 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Eric Christopher2a5196f2008-09-24 04:55:49 +00003138 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint() &&
Reid Spencer9d6565a2007-02-15 02:26:10 +00003139 !isa<VectorType>((*$2).get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00003140 GEN_ERROR(
Reid Spencerf4fa5902007-02-05 10:16:10 +00003141 "Arithmetic operator requires integer, FP, or packed operands");
Eric Christopher2a5196f2008-09-24 04:55:49 +00003142 Value* val1 = getVal(*$2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00003143 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00003144 Value* val2 = getVal(*$2, $5);
Reid Spencer5b7e7532006-09-28 19:28:24 +00003145 CHECK_FOR_ERROR
Gabor Greif7cbd8a32008-05-16 19:29:10 +00003146 $$ = BinaryOperator::Create($1, val1, val2);
Chris Lattner4a6482b2002-09-10 19:57:26 +00003147 if ($$ == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00003148 GEN_ERROR("binary operator returned null");
Reid Spencer08de34b2006-12-03 05:45:44 +00003149 delete $2;
Chris Lattner4a6482b2002-09-10 19:57:26 +00003150 }
3151 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00003152 if (!UpRefs.empty())
3153 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Chris Lattner42a75512007-01-15 02:27:26 +00003154 if (!(*$2)->isInteger()) {
Nate Begeman5bc1ea02008-07-29 15:49:41 +00003155 if (!isa<VectorType>($2->get()) ||
Reid Spencer9d6565a2007-02-15 02:26:10 +00003156 !cast<VectorType>($2->get())->getElementType()->isInteger())
Reid Spencerf4fa5902007-02-05 10:16:10 +00003157 GEN_ERROR("Logical operator requires integral operands");
Chris Lattner0a017832005-12-21 18:31:29 +00003158 }
Reid Spencer08de34b2006-12-03 05:45:44 +00003159 Value* tmpVal1 = getVal(*$2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00003160 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00003161 Value* tmpVal2 = getVal(*$2, $5);
Reid Spencer5b7e7532006-09-28 19:28:24 +00003162 CHECK_FOR_ERROR
Gabor Greif7cbd8a32008-05-16 19:29:10 +00003163 $$ = BinaryOperator::Create($1, tmpVal1, tmpVal2);
Chris Lattner4a6482b2002-09-10 19:57:26 +00003164 if ($$ == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00003165 GEN_ERROR("binary operator returned null");
Reid Spencer08de34b2006-12-03 05:45:44 +00003166 delete $2;
Chris Lattner4a6482b2002-09-10 19:57:26 +00003167 }
Reid Spencer08de34b2006-12-03 05:45:44 +00003168 | ICMP IPredicates Types ValueRef ',' ValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00003169 if (!UpRefs.empty())
3170 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00003171 Value* tmpVal1 = getVal(*$3, $4);
3172 CHECK_FOR_ERROR
3173 Value* tmpVal2 = getVal(*$3, $6);
3174 CHECK_FOR_ERROR
Gabor Greif7cbd8a32008-05-16 19:29:10 +00003175 $$ = CmpInst::Create($1, $2, tmpVal1, tmpVal2);
Reid Spencer08de34b2006-12-03 05:45:44 +00003176 if ($$ == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00003177 GEN_ERROR("icmp operator returned null");
Reid Spencer2c9df212007-03-20 01:13:00 +00003178 delete $3;
Reid Spencer08de34b2006-12-03 05:45:44 +00003179 }
3180 | FCMP FPredicates Types ValueRef ',' ValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00003181 if (!UpRefs.empty())
3182 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00003183 Value* tmpVal1 = getVal(*$3, $4);
3184 CHECK_FOR_ERROR
3185 Value* tmpVal2 = getVal(*$3, $6);
3186 CHECK_FOR_ERROR
Gabor Greif7cbd8a32008-05-16 19:29:10 +00003187 $$ = CmpInst::Create($1, $2, tmpVal1, tmpVal2);
Reid Spencer08de34b2006-12-03 05:45:44 +00003188 if ($$ == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00003189 GEN_ERROR("fcmp operator returned null");
Reid Spencer2c9df212007-03-20 01:13:00 +00003190 delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00003191 }
Nate Begemanac80ade2008-05-12 19:01:56 +00003192 | VICMP IPredicates Types ValueRef ',' ValueRef {
3193 if (!UpRefs.empty())
3194 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
3195 if (!isa<VectorType>((*$3).get()))
3196 GEN_ERROR("Scalar types not supported by vicmp instruction");
3197 Value* tmpVal1 = getVal(*$3, $4);
3198 CHECK_FOR_ERROR
3199 Value* tmpVal2 = getVal(*$3, $6);
3200 CHECK_FOR_ERROR
Gabor Greif7cbd8a32008-05-16 19:29:10 +00003201 $$ = CmpInst::Create($1, $2, tmpVal1, tmpVal2);
Nate Begemanac80ade2008-05-12 19:01:56 +00003202 if ($$ == 0)
Dan Gohmanf72fb672008-09-09 01:02:47 +00003203 GEN_ERROR("vicmp operator returned null");
Nate Begemanac80ade2008-05-12 19:01:56 +00003204 delete $3;
3205 }
3206 | VFCMP FPredicates Types ValueRef ',' ValueRef {
3207 if (!UpRefs.empty())
3208 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
3209 if (!isa<VectorType>((*$3).get()))
3210 GEN_ERROR("Scalar types not supported by vfcmp instruction");
3211 Value* tmpVal1 = getVal(*$3, $4);
3212 CHECK_FOR_ERROR
3213 Value* tmpVal2 = getVal(*$3, $6);
3214 CHECK_FOR_ERROR
Gabor Greif7cbd8a32008-05-16 19:29:10 +00003215 $$ = CmpInst::Create($1, $2, tmpVal1, tmpVal2);
Nate Begemanac80ade2008-05-12 19:01:56 +00003216 if ($$ == 0)
Dan Gohmanf72fb672008-09-09 01:02:47 +00003217 GEN_ERROR("vfcmp operator returned null");
Nate Begemanac80ade2008-05-12 19:01:56 +00003218 delete $3;
3219 }
Reid Spencer3da59db2006-11-27 01:05:10 +00003220 | CastOps ResolvedVal TO Types {
Reid Spencere1553cc2006-12-31 05:40:12 +00003221 if (!UpRefs.empty())
3222 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00003223 Value* Val = $2;
Reid Spencer93947c32007-01-17 02:47:33 +00003224 const Type* DestTy = $4->get();
3225 if (!CastInst::castIsValid($1, Val, DestTy))
3226 GEN_ERROR("invalid cast opcode for cast from '" +
3227 Val->getType()->getDescription() + "' to '" +
Eric Christopher2a5196f2008-09-24 04:55:49 +00003228 DestTy->getDescription() + "'");
Gabor Greif7cbd8a32008-05-16 19:29:10 +00003229 $$ = CastInst::Create($1, Val, DestTy);
Reid Spencer08de34b2006-12-03 05:45:44 +00003230 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00003231 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00003232 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Dan Gohmanf72fb672008-09-09 01:02:47 +00003233 if (isa<VectorType>($2->getType())) {
3234 // vector select
3235 if (!isa<VectorType>($4->getType())
3236 || !isa<VectorType>($6->getType()) )
3237 GEN_ERROR("vector select value types must be vector types");
3238 const VectorType* cond_type = cast<VectorType>($2->getType());
3239 const VectorType* select_type = cast<VectorType>($4->getType());
3240 if (cond_type->getElementType() != Type::Int1Ty)
3241 GEN_ERROR("vector select condition element type must be boolean");
3242 if (cond_type->getNumElements() != select_type->getNumElements())
3243 GEN_ERROR("vector select number of elements must be the same");
3244 } else {
3245 if ($2->getType() != Type::Int1Ty)
3246 GEN_ERROR("select condition must be boolean");
3247 }
Reid Spencer08de34b2006-12-03 05:45:44 +00003248 if ($4->getType() != $6->getType())
Dan Gohmanf72fb672008-09-09 01:02:47 +00003249 GEN_ERROR("select value types must match");
Gabor Greif051a9502008-04-06 20:25:17 +00003250 $$ = SelectInst::Create($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00003251 CHECK_FOR_ERROR
Chris Lattner76f0ff32004-03-12 05:51:36 +00003252 }
Chris Lattner99e7ab72003-10-18 05:53:13 +00003253 | VAARG ResolvedVal ',' Types {
Reid Spencere1553cc2006-12-31 05:40:12 +00003254 if (!UpRefs.empty())
3255 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00003256 $$ = new VAArgInst($2, *$4);
3257 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00003258 CHECK_FOR_ERROR
Chris Lattner99e7ab72003-10-18 05:53:13 +00003259 }
Robert Bocchino9c62b562006-01-10 19:04:32 +00003260 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencer08de34b2006-12-03 05:45:44 +00003261 if (!ExtractElementInst::isValidOperands($2, $4))
Reid Spencerf4fa5902007-02-05 10:16:10 +00003262 GEN_ERROR("Invalid extractelement operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00003263 $$ = new ExtractElementInst($2, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00003264 CHECK_FOR_ERROR
Robert Bocchino9c62b562006-01-10 19:04:32 +00003265 }
Robert Bocchino2def1b32006-01-17 20:06:25 +00003266 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer08de34b2006-12-03 05:45:44 +00003267 if (!InsertElementInst::isValidOperands($2, $4, $6))
Reid Spencerf4fa5902007-02-05 10:16:10 +00003268 GEN_ERROR("Invalid insertelement operands");
Gabor Greif051a9502008-04-06 20:25:17 +00003269 $$ = InsertElementInst::Create($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00003270 CHECK_FOR_ERROR
Robert Bocchino2def1b32006-01-17 20:06:25 +00003271 }
Chris Lattner4c949082006-04-08 01:18:35 +00003272 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer08de34b2006-12-03 05:45:44 +00003273 if (!ShuffleVectorInst::isValidOperands($2, $4, $6))
Reid Spencerf4fa5902007-02-05 10:16:10 +00003274 GEN_ERROR("Invalid shufflevector operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00003275 $$ = new ShuffleVectorInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00003276 CHECK_FOR_ERROR
Chris Lattner4c949082006-04-08 01:18:35 +00003277 }
Chris Lattner3b237fc2003-10-19 21:34:28 +00003278 | PHI_TOK PHIList {
Chris Lattnerc24d2082001-06-11 15:04:20 +00003279 const Type *Ty = $2->front().first->getType();
Chris Lattner8ea8f362003-10-30 01:38:18 +00003280 if (!Ty->isFirstClassType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00003281 GEN_ERROR("PHI node operands must be of first class type");
Gabor Greif051a9502008-04-06 20:25:17 +00003282 $$ = PHINode::Create(Ty);
Chris Lattnerce1df9e2005-01-29 00:35:55 +00003283 ((PHINode*)$$)->reserveOperandSpace($2->size());
Chris Lattner00950542001-06-06 20:29:01 +00003284 while ($2->begin() != $2->end()) {
Eric Christopher2a5196f2008-09-24 04:55:49 +00003285 if ($2->front().first->getType() != Ty)
Reid Spencerf4fa5902007-02-05 10:16:10 +00003286 GEN_ERROR("All elements of a PHI node must be of the same type");
Chris Lattnerb00c5822001-10-02 03:41:24 +00003287 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00003288 $2->pop_front();
3289 }
3290 delete $2; // Free the list...
Reid Spencer61c83e02006-08-18 08:43:06 +00003291 CHECK_FOR_ERROR
Chris Lattnerddb6db42005-05-06 05:51:46 +00003292 }
Devang Patel652203f2008-09-29 20:49:50 +00003293 | OptTailCall OptCallingConv OptRetAttrs ResultTypes ValueRef '(' ParamList ')'
Reid Spencer2c261782007-01-05 17:06:19 +00003294 OptFuncAttrs {
Reid Spencere1553cc2006-12-31 05:40:12 +00003295
3296 // Handle the short syntax
Bill Wendlingb39a55a2006-11-12 11:10:39 +00003297 const PointerType *PFTy = 0;
3298 const FunctionType *Ty = 0;
Devang Patel652203f2008-09-29 20:49:50 +00003299 if (!(PFTy = dyn_cast<PointerType>($4->get())) ||
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00003300 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00003301 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00003302 std::vector<const Type*> ParamTypes;
Devang Patel652203f2008-09-29 20:49:50 +00003303 ParamList::iterator I = $7->begin(), E = $7->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00003304 for (; I != E; ++I) {
Reid Spencere1553cc2006-12-31 05:40:12 +00003305 const Type *Ty = I->Val->getType();
3306 if (Ty == Type::VoidTy)
3307 GEN_ERROR("Short call syntax cannot be used with varargs");
3308 ParamTypes.push_back(Ty);
Chris Lattneref9c23f2001-10-03 14:53:21 +00003309 }
Chris Lattnerf7dedf22008-04-23 05:36:58 +00003310
Devang Patel652203f2008-09-29 20:49:50 +00003311 if (!FunctionType::isValidReturnType(*$4))
Chris Lattnerf7dedf22008-04-23 05:36:58 +00003312 GEN_ERROR("Invalid result type for LLVM function");
3313
Devang Patel652203f2008-09-29 20:49:50 +00003314 Ty = FunctionType::get($4->get(), ParamTypes, false);
Christopher Lamb43ad6b32007-12-17 01:12:55 +00003315 PFTy = PointerType::getUnqual(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00003316 }
Chris Lattner00950542001-06-06 20:29:01 +00003317
Devang Patel652203f2008-09-29 20:49:50 +00003318 Value *V = getVal(PFTy, $5); // Get the function we're calling...
Reid Spencer5b7e7532006-09-28 19:28:24 +00003319 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00003320
Reid Spencerebff55c2007-04-16 06:55:42 +00003321 // Check for call to invalid intrinsic to avoid crashing later.
3322 if (Function *theF = dyn_cast<Function>(V)) {
Reid Spencerce1e8ef2007-04-16 22:01:57 +00003323 if (theF->hasName() && (theF->getValueName()->getKeyLength() >= 5) &&
Reid Spencer43e60732007-04-16 20:31:06 +00003324 (0 == strncmp(theF->getValueName()->getKeyData(), "llvm.", 5)) &&
3325 !theF->getIntrinsicID(true))
Reid Spencerebff55c2007-04-16 06:55:42 +00003326 GEN_ERROR("Call to invalid LLVM intrinsic function '" +
3327 theF->getName() + "'");
3328 }
3329
Devang Patel05988662008-09-25 21:00:45 +00003330 // Set up the Attributes for the function
3331 SmallVector<AttributeWithIndex, 8> Attrs;
Devang Patel19c87462008-09-26 22:53:05 +00003332 //FIXME : In 3.0, stop accepting zext, sext and inreg as optional function
3333 //attributes.
Devang Patel652203f2008-09-29 20:49:50 +00003334 Attributes RetAttrs = $3;
3335 if ($9 != Attribute::None) {
3336 if ($9 & Attribute::ZExt) {
Devang Patel19c87462008-09-26 22:53:05 +00003337 RetAttrs = RetAttrs | Attribute::ZExt;
Devang Patel652203f2008-09-29 20:49:50 +00003338 $9 = $9 ^ Attribute::ZExt;
Devang Patel19c87462008-09-26 22:53:05 +00003339 }
Devang Patel652203f2008-09-29 20:49:50 +00003340 if ($9 & Attribute::SExt) {
Devang Patel19c87462008-09-26 22:53:05 +00003341 RetAttrs = RetAttrs | Attribute::SExt;
Devang Patel652203f2008-09-29 20:49:50 +00003342 $9 = $9 ^ Attribute::SExt;
Devang Patel19c87462008-09-26 22:53:05 +00003343 }
Devang Patel652203f2008-09-29 20:49:50 +00003344 if ($9 & Attribute::InReg) {
Devang Patel19c87462008-09-26 22:53:05 +00003345 RetAttrs = RetAttrs | Attribute::InReg;
Devang Patel652203f2008-09-29 20:49:50 +00003346 $9 = $9 ^ Attribute::InReg;
Devang Patel19c87462008-09-26 22:53:05 +00003347 }
Devang Patel19c87462008-09-26 22:53:05 +00003348 }
Devang Patel652203f2008-09-29 20:49:50 +00003349 if (RetAttrs != Attribute::None)
3350 Attrs.push_back(AttributeWithIndex::get(0, RetAttrs));
Devang Patel19c87462008-09-26 22:53:05 +00003351
Eric Christopher2a5196f2008-09-24 04:55:49 +00003352 // Check the arguments
Reid Spencere1553cc2006-12-31 05:40:12 +00003353 ValueList Args;
Devang Patel652203f2008-09-29 20:49:50 +00003354 if ($7->empty()) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00003355 // Make sure no arguments is a good thing!
3356 if (Ty->getNumParams() != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00003357 GEN_ERROR("No arguments passed to a function that "
Reid Spencerf4fa5902007-02-05 10:16:10 +00003358 "expects arguments");
Chris Lattner8b81bf52001-07-25 22:47:46 +00003359 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00003360 // Loop through FunctionType's arguments and ensure they are specified
Duncan Sandsdc024672007-11-27 13:23:08 +00003361 // correctly. Also, gather any parameter attributes.
Chris Lattnerd5d89962004-02-09 04:14:01 +00003362 FunctionType::param_iterator I = Ty->param_begin();
3363 FunctionType::param_iterator E = Ty->param_end();
Devang Patel652203f2008-09-29 20:49:50 +00003364 ParamList::iterator ArgI = $7->begin(), ArgE = $7->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00003365 unsigned index = 1;
Reid Spencer863dd882007-04-28 16:06:50 +00003366
Duncan Sandsdc024672007-11-27 13:23:08 +00003367 for (; ArgI != ArgE && I != E; ++ArgI, ++I, ++index) {
Reid Spencere1553cc2006-12-31 05:40:12 +00003368 if (ArgI->Val->getType() != *I)
3369 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00003370 (*I)->getDescription() + "'");
Reid Spencere1553cc2006-12-31 05:40:12 +00003371 Args.push_back(ArgI->Val);
Devang Patel05988662008-09-25 21:00:45 +00003372 if (ArgI->Attrs != Attribute::None)
3373 Attrs.push_back(AttributeWithIndex::get(index, ArgI->Attrs));
Reid Spencere1553cc2006-12-31 05:40:12 +00003374 }
3375 if (Ty->isVarArg()) {
3376 if (I == E)
Duncan Sandseb824702008-01-11 21:23:39 +00003377 for (; ArgI != ArgE; ++ArgI, ++index) {
Reid Spencere1553cc2006-12-31 05:40:12 +00003378 Args.push_back(ArgI->Val); // push the remaining varargs
Devang Patel05988662008-09-25 21:00:45 +00003379 if (ArgI->Attrs != Attribute::None)
3380 Attrs.push_back(AttributeWithIndex::get(index, ArgI->Attrs));
Duncan Sandseb824702008-01-11 21:23:39 +00003381 }
Reid Spencere1553cc2006-12-31 05:40:12 +00003382 } else if (I != E || ArgI != ArgE)
Reid Spencerf4fa5902007-02-05 10:16:10 +00003383 GEN_ERROR("Invalid number of parameters detected");
Chris Lattner8b81bf52001-07-25 22:47:46 +00003384 }
Devang Patel652203f2008-09-29 20:49:50 +00003385 if ($9 != Attribute::None)
3386 Attrs.push_back(AttributeWithIndex::get(~0, $9));
Duncan Sandsdc024672007-11-27 13:23:08 +00003387
Devang Patel05988662008-09-25 21:00:45 +00003388 // Finish off the Attributes and check them
3389 AttrListPtr PAL;
Duncan Sandsdc024672007-11-27 13:23:08 +00003390 if (!Attrs.empty())
Devang Patel05988662008-09-25 21:00:45 +00003391 PAL = AttrListPtr::get(Attrs.begin(), Attrs.end());
Duncan Sandsdc024672007-11-27 13:23:08 +00003392
Reid Spencere1553cc2006-12-31 05:40:12 +00003393 // Create the call node
Gabor Greif051a9502008-04-06 20:25:17 +00003394 CallInst *CI = CallInst::Create(V, Args.begin(), Args.end());
Reid Spencere1553cc2006-12-31 05:40:12 +00003395 CI->setTailCall($1);
3396 CI->setCallingConv($2);
Devang Patel05988662008-09-25 21:00:45 +00003397 CI->setAttributes(PAL);
Reid Spencere1553cc2006-12-31 05:40:12 +00003398 $$ = CI;
Devang Patel652203f2008-09-29 20:49:50 +00003399 delete $7;
3400 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00003401 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00003402 }
3403 | MemoryInst {
3404 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00003405 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00003406 };
Chris Lattner00950542001-06-06 20:29:01 +00003407
Chris Lattner15c9c032003-09-08 18:20:29 +00003408OptVolatile : VOLATILE {
3409 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00003410 CHECK_FOR_ERROR
Chris Lattner15c9c032003-09-08 18:20:29 +00003411 }
3412 | /* empty */ {
3413 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00003414 CHECK_FOR_ERROR
Chris Lattner15c9c032003-09-08 18:20:29 +00003415 };
3416
Chris Lattner027dcc52001-07-08 21:10:27 +00003417
Chris Lattnerddb6db42005-05-06 05:51:46 +00003418
Chris Lattner66db8e42005-11-06 06:34:12 +00003419MemoryInst : MALLOC Types OptCAlign {
Reid Spencere1553cc2006-12-31 05:40:12 +00003420 if (!UpRefs.empty())
3421 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00003422 $$ = new MallocInst(*$2, 0, $3);
3423 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00003424 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00003425 }
Reid Spencera54b7cb2007-01-12 07:05:14 +00003426 | MALLOC Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencere1553cc2006-12-31 05:40:12 +00003427 if (!UpRefs.empty())
3428 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Dan Gohman9c531cd2008-05-23 18:23:11 +00003429 if ($4 != Type::Int32Ty)
3430 GEN_ERROR("Malloc array size is not a 32-bit integer!");
Reid Spencer08de34b2006-12-03 05:45:44 +00003431 Value* tmpVal = getVal($4, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00003432 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00003433 $$ = new MallocInst(*$2, tmpVal, $6);
3434 delete $2;
Nate Begeman14b05292005-11-05 09:21:28 +00003435 }
Chris Lattner66db8e42005-11-06 06:34:12 +00003436 | ALLOCA Types OptCAlign {
Reid Spencere1553cc2006-12-31 05:40:12 +00003437 if (!UpRefs.empty())
3438 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00003439 $$ = new AllocaInst(*$2, 0, $3);
3440 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00003441 CHECK_FOR_ERROR
Nate Begeman14b05292005-11-05 09:21:28 +00003442 }
Reid Spencera54b7cb2007-01-12 07:05:14 +00003443 | ALLOCA Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencere1553cc2006-12-31 05:40:12 +00003444 if (!UpRefs.empty())
3445 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Dan Gohman9c531cd2008-05-23 18:23:11 +00003446 if ($4 != Type::Int32Ty)
3447 GEN_ERROR("Alloca array size is not a 32-bit integer!");
Reid Spencer08de34b2006-12-03 05:45:44 +00003448 Value* tmpVal = getVal($4, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00003449 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00003450 $$ = new AllocaInst(*$2, tmpVal, $6);
3451 delete $2;
Nate Begeman14b05292005-11-05 09:21:28 +00003452 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00003453 | FREE ResolvedVal {
Reid Spencer08de34b2006-12-03 05:45:44 +00003454 if (!isa<PointerType>($2->getType()))
Eric Christopher2a5196f2008-09-24 04:55:49 +00003455 GEN_ERROR("Trying to free nonpointer type " +
Reid Spencerf4fa5902007-02-05 10:16:10 +00003456 $2->getType()->getDescription() + "");
Reid Spencer08de34b2006-12-03 05:45:44 +00003457 $$ = new FreeInst($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00003458 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00003459 }
3460
Christopher Lamb43c7f372007-04-22 19:24:39 +00003461 | OptVolatile LOAD Types ValueRef OptCAlign {
Reid Spencere1553cc2006-12-31 05:40:12 +00003462 if (!UpRefs.empty())
3463 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00003464 if (!isa<PointerType>($3->get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00003465 GEN_ERROR("Can't load from nonpointer type: " +
Reid Spencer08de34b2006-12-03 05:45:44 +00003466 (*$3)->getDescription());
3467 if (!cast<PointerType>($3->get())->getElementType()->isFirstClassType())
Reid Spencer61c83e02006-08-18 08:43:06 +00003468 GEN_ERROR("Can't load from pointer of non-first-class type: " +
Reid Spencer08de34b2006-12-03 05:45:44 +00003469 (*$3)->getDescription());
3470 Value* tmpVal = getVal(*$3, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00003471 CHECK_FOR_ERROR
Christopher Lamb43c7f372007-04-22 19:24:39 +00003472 $$ = new LoadInst(tmpVal, "", $1, $5);
Reid Spencer08de34b2006-12-03 05:45:44 +00003473 delete $3;
Chris Lattner027dcc52001-07-08 21:10:27 +00003474 }
Christopher Lamb43c7f372007-04-22 19:24:39 +00003475 | OptVolatile STORE ResolvedVal ',' Types ValueRef OptCAlign {
Reid Spencere1553cc2006-12-31 05:40:12 +00003476 if (!UpRefs.empty())
3477 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00003478 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner8c8418d2003-09-01 16:31:28 +00003479 if (!PT)
Reid Spencer61c83e02006-08-18 08:43:06 +00003480 GEN_ERROR("Can't store to a nonpointer type: " +
Reid Spencer08de34b2006-12-03 05:45:44 +00003481 (*$5)->getDescription());
Chris Lattner8c8418d2003-09-01 16:31:28 +00003482 const Type *ElTy = PT->getElementType();
Reid Spencer08de34b2006-12-03 05:45:44 +00003483 if (ElTy != $3->getType())
3484 GEN_ERROR("Can't store '" + $3->getType()->getDescription() +
Reid Spencerf4fa5902007-02-05 10:16:10 +00003485 "' into space of type '" + ElTy->getDescription() + "'");
Chris Lattner0383cc42002-08-21 23:51:21 +00003486
Reid Spencer08de34b2006-12-03 05:45:44 +00003487 Value* tmpVal = getVal(*$5, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00003488 CHECK_FOR_ERROR
Christopher Lamb43c7f372007-04-22 19:24:39 +00003489 $$ = new StoreInst($3, tmpVal, $1, $7);
Reid Spencer08de34b2006-12-03 05:45:44 +00003490 delete $5;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00003491 }
Dan Gohmane4977cf2008-05-23 01:55:30 +00003492 | GETRESULT Types ValueRef ',' EUINT64VAL {
Dan Gohmanfc74abf2008-07-23 00:34:11 +00003493 if (!UpRefs.empty())
3494 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
3495 if (!isa<StructType>($2->get()) && !isa<ArrayType>($2->get()))
3496 GEN_ERROR("getresult insn requires an aggregate operand");
3497 if (!ExtractValueInst::getIndexedType(*$2, $5))
3498 GEN_ERROR("Invalid getresult index for type '" +
3499 (*$2)->getDescription()+ "'");
3500
3501 Value *tmpVal = getVal(*$2, $3);
Devang Pateld6ffcf92008-02-19 22:26:37 +00003502 CHECK_FOR_ERROR
Dan Gohmanfc74abf2008-07-23 00:34:11 +00003503 $$ = ExtractValueInst::Create(tmpVal, $5);
3504 delete $2;
Devang Pateld6ffcf92008-02-19 22:26:37 +00003505 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00003506 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencere1553cc2006-12-31 05:40:12 +00003507 if (!UpRefs.empty())
3508 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00003509 if (!isa<PointerType>($2->get()))
Reid Spencerf4fa5902007-02-05 10:16:10 +00003510 GEN_ERROR("getelementptr insn requires pointer operand");
Chris Lattner830b6f92004-04-05 01:30:04 +00003511
Dan Gohman041e2eb2008-05-15 19:50:34 +00003512 if (!GetElementPtrInst::getIndexedType(*$2, $4->begin(), $4->end()))
Reid Spencer61c83e02006-08-18 08:43:06 +00003513 GEN_ERROR("Invalid getelementptr indices for type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00003514 (*$2)->getDescription()+ "'");
Reid Spencer08de34b2006-12-03 05:45:44 +00003515 Value* tmpVal = getVal(*$2, $3);
Reid Spencer61c83e02006-08-18 08:43:06 +00003516 CHECK_FOR_ERROR
Gabor Greif051a9502008-04-06 20:25:17 +00003517 $$ = GetElementPtrInst::Create(tmpVal, $4->begin(), $4->end());
Eric Christopher2a5196f2008-09-24 04:55:49 +00003518 delete $2;
Reid Spencer5b7e7532006-09-28 19:28:24 +00003519 delete $4;
Dan Gohmane4977cf2008-05-23 01:55:30 +00003520 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +00003521 | EXTRACTVALUE Types ValueRef ConstantIndexList {
Dan Gohmane4977cf2008-05-23 01:55:30 +00003522 if (!UpRefs.empty())
3523 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
3524 if (!isa<StructType>($2->get()) && !isa<ArrayType>($2->get()))
3525 GEN_ERROR("extractvalue insn requires an aggregate operand");
3526
3527 if (!ExtractValueInst::getIndexedType(*$2, $4->begin(), $4->end()))
3528 GEN_ERROR("Invalid extractvalue indices for type '" +
3529 (*$2)->getDescription()+ "'");
3530 Value* tmpVal = getVal(*$2, $3);
3531 CHECK_FOR_ERROR
3532 $$ = ExtractValueInst::Create(tmpVal, $4->begin(), $4->end());
Eric Christopher2a5196f2008-09-24 04:55:49 +00003533 delete $2;
Dan Gohmane4977cf2008-05-23 01:55:30 +00003534 delete $4;
3535 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +00003536 | INSERTVALUE Types ValueRef ',' Types ValueRef ConstantIndexList {
Dan Gohmane4977cf2008-05-23 01:55:30 +00003537 if (!UpRefs.empty())
3538 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
3539 if (!isa<StructType>($2->get()) && !isa<ArrayType>($2->get()))
3540 GEN_ERROR("extractvalue insn requires an aggregate operand");
3541
3542 if (ExtractValueInst::getIndexedType(*$2, $7->begin(), $7->end()) != $5->get())
3543 GEN_ERROR("Invalid insertvalue indices for type '" +
3544 (*$2)->getDescription()+ "'");
3545 Value* aggVal = getVal(*$2, $3);
3546 Value* tmpVal = getVal(*$5, $6);
3547 CHECK_FOR_ERROR
3548 $$ = InsertValueInst::Create(aggVal, tmpVal, $7->begin(), $7->end());
Eric Christopher2a5196f2008-09-24 04:55:49 +00003549 delete $2;
Dan Gohmane4977cf2008-05-23 01:55:30 +00003550 delete $5;
3551 delete $7;
Chris Lattner51727be2002-06-04 21:58:56 +00003552 };
Chris Lattner027dcc52001-07-08 21:10:27 +00003553
Brian Gaeked0fde302003-11-11 22:41:34 +00003554
Chris Lattner00950542001-06-06 20:29:01 +00003555%%
Reid Spencer61c83e02006-08-18 08:43:06 +00003556
Reid Spencere1553cc2006-12-31 05:40:12 +00003557// common code from the two 'RunVMAsmParser' functions
3558static Module* RunParser(Module * M) {
Reid Spencere1553cc2006-12-31 05:40:12 +00003559 CurModule.CurrentModule = M;
Reid Spencere1553cc2006-12-31 05:40:12 +00003560 // Check to make sure the parser succeeded
3561 if (yyparse()) {
3562 if (ParserResult)
3563 delete ParserResult;
3564 return 0;
3565 }
3566
Reid Spencercd5bd902007-03-30 01:37:13 +00003567 // Emit an error if there are any unresolved types left.
3568 if (!CurModule.LateResolveTypes.empty()) {
3569 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
3570 if (DID.Type == ValID::LocalName) {
3571 GenerateError("Undefined type remains at eof: '"+DID.getName() + "'");
3572 } else {
3573 GenerateError("Undefined type remains at eof: #" + itostr(DID.Num));
3574 }
3575 if (ParserResult)
3576 delete ParserResult;
3577 return 0;
3578 }
3579
3580 // Emit an error if there are any unresolved values left.
3581 if (!CurModule.LateResolveValues.empty()) {
3582 Value *V = CurModule.LateResolveValues.back();
3583 std::map<Value*, std::pair<ValID, int> >::iterator I =
3584 CurModule.PlaceHolderInfo.find(V);
3585
3586 if (I != CurModule.PlaceHolderInfo.end()) {
3587 ValID &DID = I->second.first;
3588 if (DID.Type == ValID::LocalName) {
3589 GenerateError("Undefined value remains at eof: "+DID.getName() + "'");
3590 } else {
3591 GenerateError("Undefined value remains at eof: #" + itostr(DID.Num));
3592 }
3593 if (ParserResult)
3594 delete ParserResult;
3595 return 0;
3596 }
3597 }
3598
Reid Spencere1553cc2006-12-31 05:40:12 +00003599 // Check to make sure that parsing produced a result
3600 if (!ParserResult)
3601 return 0;
3602
3603 // Reset ParserResult variable while saving its value for the result.
3604 Module *Result = ParserResult;
3605 ParserResult = 0;
3606
3607 return Result;
3608}
3609
Reid Spencer61c83e02006-08-18 08:43:06 +00003610void llvm::GenerateError(const std::string &message, int LineNo) {
Chris Lattner8e3a8e02007-11-18 08:46:26 +00003611 if (LineNo == -1) LineNo = LLLgetLineNo();
Reid Spencer61c83e02006-08-18 08:43:06 +00003612 // TODO: column number in exception
3613 if (TheParseError)
Chris Lattner8e3a8e02007-11-18 08:46:26 +00003614 TheParseError->setError(LLLgetFilename(), message, LineNo);
Reid Spencer61c83e02006-08-18 08:43:06 +00003615 TriggerError = 1;
3616}
3617
Chris Lattner09083092001-07-08 04:57:15 +00003618int yyerror(const char *ErrorMsg) {
Chris Lattner8e3a8e02007-11-18 08:46:26 +00003619 std::string where = LLLgetFilename() + ":" + utostr(LLLgetLineNo()) + ": ";
Reid Spenceref9b9a72007-02-05 20:47:22 +00003620 std::string errMsg = where + "error: " + std::string(ErrorMsg);
Chris Lattner8e3a8e02007-11-18 08:46:26 +00003621 if (yychar != YYEMPTY && yychar != 0) {
3622 errMsg += " while reading token: '";
Eric Christopher2a5196f2008-09-24 04:55:49 +00003623 errMsg += std::string(LLLgetTokenStart(),
Chris Lattner8e3a8e02007-11-18 08:46:26 +00003624 LLLgetTokenStart()+LLLgetTokenLength()) + "'";
3625 }
Reid Spencer61c83e02006-08-18 08:43:06 +00003626 GenerateError(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00003627 return 0;
3628}