blob: e43c2859dbf6534d66b111174f379d9466f48b6d [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;
Reid Spencer863dd882007-04-28 16:06:50 +0000143 GlobalRefs.erase(I);
Chris Lattner8a327842004-07-14 21:44:00 +0000144 }
145 return Ret;
146 }
Reid Spencer98b3c5c2007-01-02 21:53:43 +0000147
148 bool TypeIsUnresolved(PATypeHolder* PATy) {
149 // If it isn't abstract, its resolved
150 const Type* Ty = PATy->get();
151 if (!Ty->isAbstract())
152 return false;
153 // Traverse the type looking for abstract types. If it isn't abstract then
Eric Christopher2a5196f2008-09-24 04:55:49 +0000154 // we don't need to traverse that leg of the type.
Reid Spencer98b3c5c2007-01-02 21:53:43 +0000155 std::vector<const Type*> WorkList, SeenList;
156 WorkList.push_back(Ty);
157 while (!WorkList.empty()) {
158 const Type* Ty = WorkList.back();
159 SeenList.push_back(Ty);
160 WorkList.pop_back();
161 if (const OpaqueType* OpTy = dyn_cast<OpaqueType>(Ty)) {
162 // Check to see if this is an unresolved type
163 std::map<ValID, PATypeHolder>::iterator I = LateResolveTypes.begin();
164 std::map<ValID, PATypeHolder>::iterator E = LateResolveTypes.end();
165 for ( ; I != E; ++I) {
166 if (I->second.get() == OpTy)
167 return true;
168 }
169 } else if (const SequentialType* SeqTy = dyn_cast<SequentialType>(Ty)) {
170 const Type* TheTy = SeqTy->getElementType();
171 if (TheTy->isAbstract() && TheTy != Ty) {
Eric Christopher2a5196f2008-09-24 04:55:49 +0000172 std::vector<const Type*>::iterator I = SeenList.begin(),
Reid Spencer98b3c5c2007-01-02 21:53:43 +0000173 E = SeenList.end();
174 for ( ; I != E; ++I)
175 if (*I == TheTy)
176 break;
177 if (I == E)
178 WorkList.push_back(TheTy);
179 }
180 } else if (const StructType* StrTy = dyn_cast<StructType>(Ty)) {
181 for (unsigned i = 0; i < StrTy->getNumElements(); ++i) {
182 const Type* TheTy = StrTy->getElementType(i);
183 if (TheTy->isAbstract() && TheTy != Ty) {
Eric Christopher2a5196f2008-09-24 04:55:49 +0000184 std::vector<const Type*>::iterator I = SeenList.begin(),
Reid Spencer98b3c5c2007-01-02 21:53:43 +0000185 E = SeenList.end();
186 for ( ; I != E; ++I)
187 if (*I == TheTy)
188 break;
189 if (I == E)
190 WorkList.push_back(TheTy);
191 }
192 }
193 }
194 }
195 return false;
196 }
Chris Lattner00950542001-06-06 20:29:01 +0000197} CurModule;
198
Chris Lattner79df7c02002-03-26 18:01:55 +0000199static struct PerFunctionInfo {
Chris Lattner7e708292002-06-25 16:13:24 +0000200 Function *CurrentFunction; // Pointer to current function being created
Chris Lattner00950542001-06-06 20:29:01 +0000201
Reid Spencer186a43f2007-03-19 18:39:36 +0000202 ValueList Values; // Keep track of #'d definitions
203 unsigned NextValNum;
204 ValueList LateResolveValues;
Reid Spenceref9b9a72007-02-05 20:47:22 +0000205 bool isDeclare; // Is this function a forward declararation?
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000206 GlobalValue::LinkageTypes Linkage; // Linkage for forward declaration.
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000207 GlobalValue::VisibilityTypes Visibility;
Chris Lattner00950542001-06-06 20:29:01 +0000208
Chris Lattner2e2ed292004-07-14 06:28:35 +0000209 /// BBForwardRefs - When we see forward references to basic blocks, keep
210 /// track of them here.
Reid Spencer186a43f2007-03-19 18:39:36 +0000211 std::map<ValID, BasicBlock*> BBForwardRefs;
Chris Lattner2e2ed292004-07-14 06:28:35 +0000212
Chris Lattner79df7c02002-03-26 18:01:55 +0000213 inline PerFunctionInfo() {
214 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000215 isDeclare = false;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000216 Linkage = GlobalValue::ExternalLinkage;
217 Visibility = GlobalValue::DefaultVisibility;
Chris Lattner00950542001-06-06 20:29:01 +0000218 }
219
Chris Lattner79df7c02002-03-26 18:01:55 +0000220 inline void FunctionStart(Function *M) {
221 CurrentFunction = M;
Reid Spencer186a43f2007-03-19 18:39:36 +0000222 NextValNum = 0;
Chris Lattner00950542001-06-06 20:29:01 +0000223 }
224
Chris Lattner79df7c02002-03-26 18:01:55 +0000225 void FunctionDone() {
Chris Lattner2e2ed292004-07-14 06:28:35 +0000226 // Any forward referenced blocks left?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000227 if (!BBForwardRefs.empty()) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000228 GenerateError("Undefined reference to label " +
Reid Spencer186a43f2007-03-19 18:39:36 +0000229 BBForwardRefs.begin()->second->getName());
Reid Spencer5b7e7532006-09-28 19:28:24 +0000230 return;
231 }
Chris Lattner2e2ed292004-07-14 06:28:35 +0000232
233 // Resolve all forward references now.
Chris Lattner386a3b72001-10-16 19:54:17 +0000234 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000235
Chris Lattner7e708292002-06-25 16:13:24 +0000236 Values.clear(); // Clear out function local definitions
Reid Spencer186a43f2007-03-19 18:39:36 +0000237 BBForwardRefs.clear();
Chris Lattner79df7c02002-03-26 18:01:55 +0000238 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000239 isDeclare = false;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000240 Linkage = GlobalValue::ExternalLinkage;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000241 Visibility = GlobalValue::DefaultVisibility;
Chris Lattner00950542001-06-06 20:29:01 +0000242 }
Chris Lattner394f2eb2003-10-16 20:12:13 +0000243} CurFun; // Info for the current function...
Chris Lattner00950542001-06-06 20:29:01 +0000244
Chris Lattner394f2eb2003-10-16 20:12:13 +0000245static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000246
Chris Lattner00950542001-06-06 20:29:01 +0000247
248//===----------------------------------------------------------------------===//
249// Code to handle definitions of all the types
250//===----------------------------------------------------------------------===//
251
Chris Lattner6ac28092008-08-29 17:12:13 +0000252/// InsertValue - Insert a value into the value table. If it is named, this
253/// returns -1, otherwise it returns the slot number for the value.
254static int InsertValue(Value *V, ValueList &ValueTab = CurFun.Values) {
Reid Spencer186a43f2007-03-19 18:39:36 +0000255 // Things that have names or are void typed don't get slot numbers
256 if (V->hasName() || (V->getType() == Type::VoidTy))
Chris Lattner6ac28092008-08-29 17:12:13 +0000257 return -1;
Chris Lattner2079fde2001-10-13 06:41:08 +0000258
Reid Spencer186a43f2007-03-19 18:39:36 +0000259 // In the case of function values, we have to allow for the forward reference
260 // of basic blocks, which are included in the numbering. Consequently, we keep
Eric Christopher2a5196f2008-09-24 04:55:49 +0000261 // track of the next insertion location with NextValNum. When a BB gets
Reid Spencer186a43f2007-03-19 18:39:36 +0000262 // inserted, it could change the size of the CurFun.Values vector.
263 if (&ValueTab == &CurFun.Values) {
264 if (ValueTab.size() <= CurFun.NextValNum)
265 ValueTab.resize(CurFun.NextValNum+1);
266 ValueTab[CurFun.NextValNum++] = V;
Chris Lattner6ac28092008-08-29 17:12:13 +0000267 return CurFun.NextValNum-1;
Eric Christopher2a5196f2008-09-24 04:55:49 +0000268 }
Reid Spencer186a43f2007-03-19 18:39:36 +0000269 // For all other lists, its okay to just tack it on the back of the vector.
270 ValueTab.push_back(V);
Chris Lattner6ac28092008-08-29 17:12:13 +0000271 return ValueTab.size()-1;
Chris Lattner00950542001-06-06 20:29:01 +0000272}
273
Chris Lattner30c89792001-09-07 16:35:17 +0000274static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000275 switch (D.Type) {
Reid Spencerb2d17862007-01-26 08:04:51 +0000276 case ValID::LocalID: // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000277 // Module constants occupy the lowest numbered slots...
Reid Spencerb2d17862007-01-26 08:04:51 +0000278 if (D.Num < CurModule.Types.size())
279 return CurModule.Types[D.Num];
Chris Lattner42c9e772001-10-20 09:32:59 +0000280 break;
Reid Spencerb2d17862007-01-26 08:04:51 +0000281 case ValID::LocalName: // Is it a named definition?
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000282 if (const Type *N = CurModule.CurrentModule->getTypeByName(D.getName())) {
Chris Lattnera09000d2004-07-14 23:03:46 +0000283 D.destroy(); // Free old strdup'd memory...
284 return N;
Chris Lattner6e6026b2002-11-20 18:36:02 +0000285 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000286 break;
Chris Lattner30c89792001-09-07 16:35:17 +0000287 default:
Reid Spencerf4fa5902007-02-05 10:16:10 +0000288 GenerateError("Internal parser error: Invalid symbol type reference");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000289 return 0;
Chris Lattner30c89792001-09-07 16:35:17 +0000290 }
291
292 // If we reached here, we referenced either a symbol that we don't know about
293 // or an id number that hasn't been read yet. We may be referencing something
294 // forward, so just create an entry to be resolved later and get to it...
295 //
296 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
297
Chris Lattner355ad1f2005-03-21 06:27:42 +0000298
299 if (inFunctionScope()) {
Reid Spencerb2d17862007-01-26 08:04:51 +0000300 if (D.Type == ValID::LocalName) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000301 GenerateError("Reference to an undefined type: '" + D.getName() + "'");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000302 return 0;
303 } else {
Reid Spencerb2d17862007-01-26 08:04:51 +0000304 GenerateError("Reference to an undefined type: #" + utostr(D.Num));
Reid Spencer5b7e7532006-09-28 19:28:24 +0000305 return 0;
306 }
Chris Lattner4a42e902001-10-22 05:56:09 +0000307 }
Chris Lattner30c89792001-09-07 16:35:17 +0000308
Reid Spencerb78b9082006-11-28 07:28:14 +0000309 std::map<ValID, PATypeHolder>::iterator I =CurModule.LateResolveTypes.find(D);
Chris Lattner355ad1f2005-03-21 06:27:42 +0000310 if (I != CurModule.LateResolveTypes.end())
Reid Spencerb78b9082006-11-28 07:28:14 +0000311 return I->second;
Chris Lattner355ad1f2005-03-21 06:27:42 +0000312
Reid Spencerb78b9082006-11-28 07:28:14 +0000313 Type *Typ = OpaqueType::get();
314 CurModule.LateResolveTypes.insert(std::make_pair(D, Typ));
315 return Typ;
Reid Spencer08de34b2006-12-03 05:45:44 +0000316 }
Chris Lattner30c89792001-09-07 16:35:17 +0000317
Reid Spencer186a43f2007-03-19 18:39:36 +0000318// getExistingVal - Look up the value specified by the provided type and
Chris Lattner2079fde2001-10-13 06:41:08 +0000319// the provided ValID. If the value exists and has already been defined, return
320// it. Otherwise return null.
321//
Reid Spencer186a43f2007-03-19 18:39:36 +0000322static Value *getExistingVal(const Type *Ty, const ValID &D) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000323 if (isa<FunctionType>(Ty)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000324 GenerateError("Functions are not values and "
Chris Lattner79df7c02002-03-26 18:01:55 +0000325 "must be referenced as pointers");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000326 return 0;
327 }
Chris Lattner386a3b72001-10-16 19:54:17 +0000328
Chris Lattner30c89792001-09-07 16:35:17 +0000329 switch (D.Type) {
Reid Spencerb2d17862007-01-26 08:04:51 +0000330 case ValID::LocalID: { // Is it a numbered definition?
Reid Spencerb2d17862007-01-26 08:04:51 +0000331 // Check that the number is within bounds.
Eric Christopher2a5196f2008-09-24 04:55:49 +0000332 if (D.Num >= CurFun.Values.size())
Reid Spencer186a43f2007-03-19 18:39:36 +0000333 return 0;
334 Value *Result = CurFun.Values[D.Num];
335 if (Ty != Result->getType()) {
336 GenerateError("Numbered value (%" + utostr(D.Num) + ") of type '" +
Eric Christopher2a5196f2008-09-24 04:55:49 +0000337 Result->getType()->getDescription() + "' does not match "
Reid Spencer186a43f2007-03-19 18:39:36 +0000338 "expected type, '" + Ty->getDescription() + "'");
339 return 0;
340 }
341 return Result;
Reid Spencerb2d17862007-01-26 08:04:51 +0000342 }
343 case ValID::GlobalID: { // Is it a numbered definition?
Eric Christopher2a5196f2008-09-24 04:55:49 +0000344 if (D.Num >= CurModule.Values.size())
Reid Spenceref9b9a72007-02-05 20:47:22 +0000345 return 0;
Reid Spencer186a43f2007-03-19 18:39:36 +0000346 Value *Result = CurModule.Values[D.Num];
347 if (Ty != Result->getType()) {
348 GenerateError("Numbered value (@" + utostr(D.Num) + ") of type '" +
Eric Christopher2a5196f2008-09-24 04:55:49 +0000349 Result->getType()->getDescription() + "' does not match "
Reid Spencer186a43f2007-03-19 18:39:36 +0000350 "expected type, '" + Ty->getDescription() + "'");
Reid Spenceref9b9a72007-02-05 20:47:22 +0000351 return 0;
Reid Spencer186a43f2007-03-19 18:39:36 +0000352 }
353 return Result;
Chris Lattner00950542001-06-06 20:29:01 +0000354 }
Eric Christopher2a5196f2008-09-24 04:55:49 +0000355
Reid Spencerb2d17862007-01-26 08:04:51 +0000356 case ValID::LocalName: { // Is it a named definition?
Eric Christopher2a5196f2008-09-24 04:55:49 +0000357 if (!inFunctionScope())
Reid Spenceref9b9a72007-02-05 20:47:22 +0000358 return 0;
359 ValueSymbolTable &SymTab = CurFun.CurrentFunction->getValueSymbolTable();
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000360 Value *N = SymTab.lookup(D.getName());
Eric Christopher2a5196f2008-09-24 04:55:49 +0000361 if (N == 0)
Reid Spenceref9b9a72007-02-05 20:47:22 +0000362 return 0;
363 if (N->getType() != Ty)
364 return 0;
Eric Christopher2a5196f2008-09-24 04:55:49 +0000365
Reid Spencerb2d17862007-01-26 08:04:51 +0000366 D.destroy(); // Free old strdup'd memory...
367 return N;
368 }
369 case ValID::GlobalName: { // Is it a named definition?
Reid Spenceref9b9a72007-02-05 20:47:22 +0000370 ValueSymbolTable &SymTab = CurModule.CurrentModule->getValueSymbolTable();
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000371 Value *N = SymTab.lookup(D.getName());
Eric Christopher2a5196f2008-09-24 04:55:49 +0000372 if (N == 0)
Reid Spenceref9b9a72007-02-05 20:47:22 +0000373 return 0;
374 if (N->getType() != Ty)
375 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000376
377 D.destroy(); // Free old strdup'd memory...
378 return N;
379 }
380
Misha Brukman5a2a3822005-05-10 22:02:28 +0000381 // Check to make sure that "Ty" is an integral type, and that our
Chris Lattner2079fde2001-10-13 06:41:08 +0000382 // value will fit into the specified type...
383 case ValID::ConstSIntVal: // Is it a constant pool reference??
Chris Lattner689e8b22008-02-19 04:36:07 +0000384 if (!isa<IntegerType>(Ty) ||
385 !ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000386 GenerateError("Signed integral constant '" +
Misha Brukman5a2a3822005-05-10 22:02:28 +0000387 itostr(D.ConstPool64) + "' is invalid for type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +0000388 Ty->getDescription() + "'");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000389 return 0;
390 }
Reid Spencereac65742007-03-19 20:40:22 +0000391 return ConstantInt::get(Ty, D.ConstPool64, true);
Chris Lattner2079fde2001-10-13 06:41:08 +0000392
393 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattner689e8b22008-02-19 04:36:07 +0000394 if (isa<IntegerType>(Ty) &&
395 ConstantInt::isValueValidForType(Ty, D.UConstPool64))
Reid Spencerb83eb642006-10-20 07:07:24 +0000396 return ConstantInt::get(Ty, D.UConstPool64);
Chris Lattner689e8b22008-02-19 04:36:07 +0000397
398 if (!isa<IntegerType>(Ty) ||
399 !ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
400 GenerateError("Integral constant '" + utostr(D.UConstPool64) +
401 "' is invalid or out of range for type '" +
402 Ty->getDescription() + "'");
403 return 0;
Chris Lattner2079fde2001-10-13 06:41:08 +0000404 }
Chris Lattner689e8b22008-02-19 04:36:07 +0000405 // This is really a signed reference. Transmogrify.
406 return ConstantInt::get(Ty, D.ConstPool64, true);
Chris Lattner2079fde2001-10-13 06:41:08 +0000407
Chris Lattnerd1599012008-07-11 00:30:06 +0000408 case ValID::ConstAPInt: // Is it an unsigned const pool reference?
409 if (!isa<IntegerType>(Ty)) {
410 GenerateError("Integral constant '" + D.getName() +
411 "' is invalid or out of range for type '" +
412 Ty->getDescription() + "'");
413 return 0;
414 }
Eric Christopher2a5196f2008-09-24 04:55:49 +0000415
Chris Lattnerd1599012008-07-11 00:30:06 +0000416 {
417 APSInt Tmp = *D.ConstPoolInt;
418 Tmp.extOrTrunc(Ty->getPrimitiveSizeInBits());
419 return ConstantInt::get(Tmp);
420 }
Eric Christopher2a5196f2008-09-24 04:55:49 +0000421
Chris Lattner2079fde2001-10-13 06:41:08 +0000422 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattner689e8b22008-02-19 04:36:07 +0000423 if (!Ty->isFloatingPoint() ||
424 !ConstantFP::isValueValidForType(Ty, *D.ConstPoolFP)) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000425 GenerateError("FP constant invalid for type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000426 return 0;
427 }
Eric Christopher2a5196f2008-09-24 04:55:49 +0000428 // Lexer has no type info, so builds all float and double FP constants
Dale Johannesen3f6eb742007-09-11 18:32:33 +0000429 // as double. Fix this here. Long double does not need this.
430 if (&D.ConstPoolFP->getSemantics() == &APFloat::IEEEdouble &&
431 Ty==Type::FloatTy)
Dale Johannesen43421b32007-09-06 18:13:44 +0000432 D.ConstPoolFP->convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven);
Chris Lattner02a260a2008-04-20 00:41:09 +0000433 return ConstantFP::get(*D.ConstPoolFP);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000434
Chris Lattner2079fde2001-10-13 06:41:08 +0000435 case ValID::ConstNullVal: // Is it a null value?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000436 if (!isa<PointerType>(Ty)) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000437 GenerateError("Cannot create a a non pointer null");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000438 return 0;
439 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000440 return ConstantPointerNull::get(cast<PointerType>(Ty));
Misha Brukman5a2a3822005-05-10 22:02:28 +0000441
Chris Lattner16710e92004-10-16 18:17:13 +0000442 case ValID::ConstUndefVal: // Is it an undef value?
443 return UndefValue::get(Ty);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000444
Chris Lattnerf1f03df2005-12-21 17:53:02 +0000445 case ValID::ConstZeroVal: // Is it a zero value?
446 return Constant::getNullValue(Ty);
Eric Christopher2a5196f2008-09-24 04:55:49 +0000447
Chris Lattnerd78700d2002-08-16 21:14:40 +0000448 case ValID::ConstantVal: // Fully resolved constant?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000449 if (D.ConstantValue->getType() != Ty) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000450 GenerateError("Constant expression type different from required type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000451 return 0;
452 }
Chris Lattnerd78700d2002-08-16 21:14:40 +0000453 return D.ConstantValue;
454
Chris Lattneraa2c8532006-01-25 22:26:43 +0000455 case ValID::InlineAsmVal: { // Inline asm expression
456 const PointerType *PTy = dyn_cast<PointerType>(Ty);
457 const FunctionType *FTy =
458 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
Reid Spencer5b7e7532006-09-28 19:28:24 +0000459 if (!FTy || !InlineAsm::Verify(FTy, D.IAD->Constraints)) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000460 GenerateError("Invalid type for asm constraint string");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000461 return 0;
462 }
Chris Lattneraa2c8532006-01-25 22:26:43 +0000463 InlineAsm *IA = InlineAsm::get(FTy, D.IAD->AsmString, D.IAD->Constraints,
464 D.IAD->HasSideEffects);
465 D.destroy(); // Free InlineAsmDescriptor.
466 return IA;
467 }
Chris Lattner30c89792001-09-07 16:35:17 +0000468 default:
Reid Spencer1755a9a2007-02-05 17:01:20 +0000469 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000470 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000471 } // End of switch
472
Reid Spencer1755a9a2007-02-05 17:01:20 +0000473 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000474 return 0;
475}
476
Reid Spencer186a43f2007-03-19 18:39:36 +0000477// getVal - This function is identical to getExistingVal, except that if a
Chris Lattner2079fde2001-10-13 06:41:08 +0000478// value is not already defined, it "improvises" by creating a placeholder var
479// that looks and acts just like the requested variable. When the value is
480// defined later, all uses of the placeholder variable are replaced with the
481// real thing.
482//
Chris Lattner64116f92004-07-14 01:33:11 +0000483static Value *getVal(const Type *Ty, const ValID &ID) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000484 if (Ty == Type::LabelTy) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000485 GenerateError("Cannot use a basic block here");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000486 return 0;
487 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000488
Chris Lattner64116f92004-07-14 01:33:11 +0000489 // See if the value has already been defined.
Reid Spencer186a43f2007-03-19 18:39:36 +0000490 Value *V = getExistingVal(Ty, ID);
Chris Lattner2079fde2001-10-13 06:41:08 +0000491 if (V) return V;
Reid Spencer5b7e7532006-09-28 19:28:24 +0000492 if (TriggerError) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000493
Reid Spencer5b7e7532006-09-28 19:28:24 +0000494 if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty)) {
Dan Gohmane4977cf2008-05-23 01:55:30 +0000495 GenerateError("Invalid use of a non-first-class type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000496 return 0;
497 }
Chris Lattner60cd9552005-03-23 01:29:26 +0000498
Chris Lattner00950542001-06-06 20:29:01 +0000499 // If we reached here, we referenced either a symbol that we don't know about
500 // or an id number that hasn't been read yet. We may be referencing something
501 // forward, so just create an entry to be resolved later and get to it...
502 //
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000503 switch (ID.Type) {
504 case ValID::GlobalName:
Reid Spencer3cb4dda2007-04-28 14:13:42 +0000505 case ValID::GlobalID: {
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000506 const PointerType *PTy = dyn_cast<PointerType>(Ty);
507 if (!PTy) {
508 GenerateError("Invalid type for reference to global" );
509 return 0;
510 }
511 const Type* ElTy = PTy->getElementType();
512 if (const FunctionType *FTy = dyn_cast<FunctionType>(ElTy))
Gabor Greif051a9502008-04-06 20:25:17 +0000513 V = Function::Create(FTy, GlobalValue::ExternalLinkage);
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000514 else
Christopher Lambfe63fb92007-12-11 08:59:05 +0000515 V = new GlobalVariable(ElTy, false, GlobalValue::ExternalLinkage, 0, "",
516 (Module*)0, false, PTy->getAddressSpace());
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000517 break;
Reid Spencer3cb4dda2007-04-28 14:13:42 +0000518 }
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000519 default:
520 V = new Argument(Ty);
521 }
Eric Christopher2a5196f2008-09-24 04:55:49 +0000522
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000523 // Remember where this forward reference came from. FIXME, shouldn't we try
524 // to recycle these things??
Chris Lattner64116f92004-07-14 01:33:11 +0000525 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000526 LLLgetLineNo())));
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000527
Chris Lattner79df7c02002-03-26 18:01:55 +0000528 if (inFunctionScope())
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000529 InsertValue(V, CurFun.LateResolveValues);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000530 else
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000531 InsertValue(V, CurModule.LateResolveValues);
532 return V;
Chris Lattner00950542001-06-06 20:29:01 +0000533}
534
Reid Spencer186a43f2007-03-19 18:39:36 +0000535/// defineBBVal - This is a definition of a new basic block with the specified
536/// identifier which must be the same as CurFun.NextValNum, if its numeric.
Nick Lewycky280a6e62008-04-25 16:53:59 +0000537static BasicBlock *defineBBVal(const ValID &ID) {
Reid Spencer1755a9a2007-02-05 17:01:20 +0000538 assert(inFunctionScope() && "Can't get basic block at global scope!");
Chris Lattner64116f92004-07-14 01:33:11 +0000539
Chris Lattner2e2ed292004-07-14 06:28:35 +0000540 BasicBlock *BB = 0;
Chris Lattner64116f92004-07-14 01:33:11 +0000541
Reid Spencer186a43f2007-03-19 18:39:36 +0000542 // First, see if this was forward referenced
Chris Lattner64116f92004-07-14 01:33:11 +0000543
Reid Spencer186a43f2007-03-19 18:39:36 +0000544 std::map<ValID, BasicBlock*>::iterator BBI = CurFun.BBForwardRefs.find(ID);
545 if (BBI != CurFun.BBForwardRefs.end()) {
546 BB = BBI->second;
Chris Lattner2e2ed292004-07-14 06:28:35 +0000547 // The forward declaration could have been inserted anywhere in the
548 // function: insert it into the correct place now.
549 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
550 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
Reid Spencer186a43f2007-03-19 18:39:36 +0000551
Reid Spencer2c9df212007-03-20 01:13:00 +0000552 // We're about to erase the entry, save the key so we can clean it up.
553 ValID Tmp = BBI->first;
554
Reid Spencer186a43f2007-03-19 18:39:36 +0000555 // Erase the forward ref from the map as its no longer "forward"
556 CurFun.BBForwardRefs.erase(ID);
557
Eric Christopher2a5196f2008-09-24 04:55:49 +0000558 // The key has been removed from the map but so we don't want to leave
Reid Spencer2c9df212007-03-20 01:13:00 +0000559 // strdup'd memory around so destroy it too.
560 Tmp.destroy();
561
Reid Spencer186a43f2007-03-19 18:39:36 +0000562 // If its a numbered definition, bump the number and set the BB value.
563 if (ID.Type == ValID::LocalID) {
564 assert(ID.Num == CurFun.NextValNum && "Invalid new block number");
565 InsertValue(BB);
566 }
Eric Christopher2a5196f2008-09-24 04:55:49 +0000567 } else {
568 // We haven't seen this BB before and its first mention is a definition.
Nick Lewyckyfc82fab2008-03-02 02:48:09 +0000569 // Just create it and return it.
570 std::string Name (ID.Type == ValID::LocalName ? ID.getName() : "");
Gabor Greif051a9502008-04-06 20:25:17 +0000571 BB = BasicBlock::Create(Name, CurFun.CurrentFunction);
Nick Lewyckyfc82fab2008-03-02 02:48:09 +0000572 if (ID.Type == ValID::LocalID) {
573 assert(ID.Num == CurFun.NextValNum && "Invalid new block number");
574 InsertValue(BB);
575 }
Chris Lattner2e2ed292004-07-14 06:28:35 +0000576 }
Reid Spencer186a43f2007-03-19 18:39:36 +0000577
Nick Lewyckyfc82fab2008-03-02 02:48:09 +0000578 ID.destroy();
Reid Spencer186a43f2007-03-19 18:39:36 +0000579 return BB;
580}
581
582/// getBBVal - get an existing BB value or create a forward reference for it.
Eric Christopher2a5196f2008-09-24 04:55:49 +0000583///
Reid Spencer186a43f2007-03-19 18:39:36 +0000584static BasicBlock *getBBVal(const ValID &ID) {
585 assert(inFunctionScope() && "Can't get basic block at global scope!");
586
587 BasicBlock *BB = 0;
588
589 std::map<ValID, BasicBlock*>::iterator BBI = CurFun.BBForwardRefs.find(ID);
590 if (BBI != CurFun.BBForwardRefs.end()) {
591 BB = BBI->second;
592 } if (ID.Type == ValID::LocalName) {
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000593 std::string Name = ID.getName();
Reid Spencer186a43f2007-03-19 18:39:36 +0000594 Value *N = CurFun.CurrentFunction->getValueSymbolTable().lookup(Name);
Anton Korobeynikov4aefd6b2008-02-20 12:07:57 +0000595 if (N) {
Reid Spencer186a43f2007-03-19 18:39:36 +0000596 if (N->getType()->getTypeID() == Type::LabelTyID)
597 BB = cast<BasicBlock>(N);
598 else
599 GenerateError("Reference to label '" + Name + "' is actually of type '"+
600 N->getType()->getDescription() + "'");
Anton Korobeynikov4aefd6b2008-02-20 12:07:57 +0000601 }
Reid Spencer186a43f2007-03-19 18:39:36 +0000602 } else if (ID.Type == ValID::LocalID) {
603 if (ID.Num < CurFun.NextValNum && ID.Num < CurFun.Values.size()) {
604 if (CurFun.Values[ID.Num]->getType()->getTypeID() == Type::LabelTyID)
605 BB = cast<BasicBlock>(CurFun.Values[ID.Num]);
606 else
Eric Christopher2a5196f2008-09-24 04:55:49 +0000607 GenerateError("Reference to label '%" + utostr(ID.Num) +
608 "' is actually of type '"+
Reid Spencer186a43f2007-03-19 18:39:36 +0000609 CurFun.Values[ID.Num]->getType()->getDescription() + "'");
610 }
611 } else {
612 GenerateError("Illegal label reference " + ID.getName());
613 return 0;
614 }
615
616 // If its already been defined, return it now.
617 if (BB) {
618 ID.destroy(); // Free strdup'd memory.
619 return BB;
620 }
621
622 // Otherwise, this block has not been seen before, create it.
623 std::string Name;
624 if (ID.Type == ValID::LocalName)
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000625 Name = ID.getName();
Gabor Greif051a9502008-04-06 20:25:17 +0000626 BB = BasicBlock::Create(Name, CurFun.CurrentFunction);
Reid Spencer186a43f2007-03-19 18:39:36 +0000627
628 // Insert it in the forward refs map.
629 CurFun.BBForwardRefs[ID] = BB;
630
Chris Lattner64116f92004-07-14 01:33:11 +0000631 return BB;
632}
633
Chris Lattner00950542001-06-06 20:29:01 +0000634
635//===----------------------------------------------------------------------===//
636// Code to handle forward references in instructions
637//===----------------------------------------------------------------------===//
638//
639// This code handles the late binding needed with statements that reference
640// values not defined yet... for example, a forward branch, or the PHI node for
641// a loop body.
642//
Chris Lattner394f2eb2003-10-16 20:12:13 +0000643// This keeps a table (CurFun.LateResolveValues) of all such forward references
Chris Lattner00950542001-06-06 20:29:01 +0000644// and back patchs after we are done.
645//
646
Misha Brukman5a2a3822005-05-10 22:02:28 +0000647// ResolveDefinitions - If we could not resolve some defs at parsing
648// time (forward branches, phi functions for loops, etc...) resolve the
Chris Lattner00950542001-06-06 20:29:01 +0000649// defs now...
650//
Eric Christopher2a5196f2008-09-24 04:55:49 +0000651static void
Reid Spencer186a43f2007-03-19 18:39:36 +0000652ResolveDefinitions(ValueList &LateResolvers, ValueList *FutureLateResolvers) {
Chris Lattner00950542001-06-06 20:29:01 +0000653 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Reid Spencer186a43f2007-03-19 18:39:36 +0000654 while (!LateResolvers.empty()) {
655 Value *V = LateResolvers.back();
656 LateResolvers.pop_back();
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000657
Reid Spencer186a43f2007-03-19 18:39:36 +0000658 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
659 CurModule.PlaceHolderInfo.find(V);
660 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000661
Reid Spencer186a43f2007-03-19 18:39:36 +0000662 ValID &DID = PHI->second.first;
Chris Lattner00950542001-06-06 20:29:01 +0000663
Reid Spencer186a43f2007-03-19 18:39:36 +0000664 Value *TheRealValue = getExistingVal(V->getType(), DID);
665 if (TriggerError)
666 return;
667 if (TheRealValue) {
668 V->replaceAllUsesWith(TheRealValue);
669 delete V;
670 CurModule.PlaceHolderInfo.erase(PHI);
671 } else if (FutureLateResolvers) {
672 // Functions have their unresolved items forwarded to the module late
673 // resolver table
674 InsertValue(V, *FutureLateResolvers);
675 } else {
676 if (DID.Type == ValID::LocalName || DID.Type == ValID::GlobalName) {
677 GenerateError("Reference to an invalid definition: '" +DID.getName()+
678 "' of type '" + V->getType()->getDescription() + "'",
679 PHI->second.second);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000680 return;
Chris Lattner386a3b72001-10-16 19:54:17 +0000681 } else {
Reid Spencer186a43f2007-03-19 18:39:36 +0000682 GenerateError("Reference to an invalid definition: #" +
683 itostr(DID.Num) + " of type '" +
684 V->getType()->getDescription() + "'",
685 PHI->second.second);
686 return;
Chris Lattner30c89792001-09-07 16:35:17 +0000687 }
Chris Lattner00950542001-06-06 20:29:01 +0000688 }
689 }
Chris Lattner00950542001-06-06 20:29:01 +0000690 LateResolvers.clear();
691}
692
Chris Lattner4a42e902001-10-22 05:56:09 +0000693// ResolveTypeTo - A brand new type was just declared. This means that (if
694// name is not null) things referencing Name can be resolved. Otherwise, things
695// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000696//
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000697static void ResolveTypeTo(std::string *Name, const Type *ToTy) {
Chris Lattner355ad1f2005-03-21 06:27:42 +0000698 ValID D;
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000699 if (Name)
700 D = ValID::createLocalName(*Name);
Eric Christopher2a5196f2008-09-24 04:55:49 +0000701 else
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000702 D = ValID::createLocalID(CurModule.Types.size());
Chris Lattner00950542001-06-06 20:29:01 +0000703
Reid Spencerb78b9082006-11-28 07:28:14 +0000704 std::map<ValID, PATypeHolder>::iterator I =
Chris Lattner355ad1f2005-03-21 06:27:42 +0000705 CurModule.LateResolveTypes.find(D);
706 if (I != CurModule.LateResolveTypes.end()) {
Reid Spencerb78b9082006-11-28 07:28:14 +0000707 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattner355ad1f2005-03-21 06:27:42 +0000708 CurModule.LateResolveTypes.erase(I);
Chris Lattner30c89792001-09-07 16:35:17 +0000709 }
Nuno Lopesaa1aee32008-10-03 15:51:46 +0000710 D.destroy();
Chris Lattner30c89792001-09-07 16:35:17 +0000711}
712
Chris Lattner1781aca2001-09-18 04:00:54 +0000713// setValueName - Set the specified value to the name given. The name may be
714// null potentially, in which case this is a noop. The string passed in is
Chris Lattner8ab406d2004-07-13 07:59:27 +0000715// assumed to be a malloc'd string buffer, and is free'd by this function.
716//
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000717static void setValueName(Value *V, std::string *NameStr) {
Reid Spencerb2d17862007-01-26 08:04:51 +0000718 if (!NameStr) return;
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000719 std::string Name(*NameStr); // Copy string
720 delete NameStr; // Free old string
Chris Lattnerc9aea522004-07-13 08:12:39 +0000721
Reid Spencerb2d17862007-01-26 08:04:51 +0000722 if (V->getType() == Type::VoidTy) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000723 GenerateError("Can't assign name '" + Name+"' to value with void type");
Reid Spencerb2d17862007-01-26 08:04:51 +0000724 return;
Chris Lattner8ab406d2004-07-13 07:59:27 +0000725 }
Reid Spencerb2d17862007-01-26 08:04:51 +0000726
Reid Spencer1755a9a2007-02-05 17:01:20 +0000727 assert(inFunctionScope() && "Must be in function scope!");
Reid Spenceref9b9a72007-02-05 20:47:22 +0000728 ValueSymbolTable &ST = CurFun.CurrentFunction->getValueSymbolTable();
729 if (ST.lookup(Name)) {
Reid Spencerb2d17862007-01-26 08:04:51 +0000730 GenerateError("Redefinition of value '" + Name + "' of type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +0000731 V->getType()->getDescription() + "'");
Reid Spencerb2d17862007-01-26 08:04:51 +0000732 return;
733 }
734
735 // Set the name.
736 V->setName(Name);
Chris Lattner8ab406d2004-07-13 07:59:27 +0000737}
738
Chris Lattnerd88998d2004-07-14 08:23:52 +0000739/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
740/// this is a declaration, otherwise it is a definition.
Chris Lattnerb2b96672005-11-12 18:21:21 +0000741static GlobalVariable *
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000742ParseGlobalVariable(std::string *NameStr,
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000743 GlobalValue::LinkageTypes Linkage,
744 GlobalValue::VisibilityTypes Visibility,
Chris Lattnerb2b96672005-11-12 18:21:21 +0000745 bool isConstantGlobal, const Type *Ty,
Christopher Lambfe63fb92007-12-11 08:59:05 +0000746 Constant *Initializer, bool IsThreadLocal,
747 unsigned AddressSpace = 0) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000748 if (isa<FunctionType>(Ty)) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000749 GenerateError("Cannot declare global vars of function type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000750 return 0;
751 }
Dan Gohman9c531cd2008-05-23 18:23:11 +0000752 if (Ty == Type::LabelTy) {
753 GenerateError("Cannot declare global vars of label type");
754 return 0;
755 }
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000756
Christopher Lambfe63fb92007-12-11 08:59:05 +0000757 const PointerType *PTy = PointerType::get(Ty, AddressSpace);
Chris Lattnera09000d2004-07-14 23:03:46 +0000758
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000759 std::string Name;
760 if (NameStr) {
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000761 Name = *NameStr; // Copy string
762 delete NameStr; // Free old string
Chris Lattnerd88998d2004-07-14 08:23:52 +0000763 }
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000764
Chris Lattner8a327842004-07-14 21:44:00 +0000765 // See if this global value was forward referenced. If so, recycle the
766 // object.
Misha Brukman5a2a3822005-05-10 22:02:28 +0000767 ValID ID;
Chris Lattner8a327842004-07-14 21:44:00 +0000768 if (!Name.empty()) {
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000769 ID = ValID::createGlobalName(Name);
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000770 } else {
Reid Spencer186a43f2007-03-19 18:39:36 +0000771 ID = ValID::createGlobalID(CurModule.Values.size());
Chris Lattner8a327842004-07-14 21:44:00 +0000772 }
773
Reid Spencer863dd882007-04-28 16:06:50 +0000774 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000775 // Move the global to the end of the list, from whereever it was
Chris Lattner8a327842004-07-14 21:44:00 +0000776 // previously inserted.
777 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
778 CurModule.CurrentModule->getGlobalList().remove(GV);
779 CurModule.CurrentModule->getGlobalList().push_back(GV);
780 GV->setInitializer(Initializer);
781 GV->setLinkage(Linkage);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000782 GV->setVisibility(Visibility);
Chris Lattner8a327842004-07-14 21:44:00 +0000783 GV->setConstant(isConstantGlobal);
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +0000784 GV->setThreadLocal(IsThreadLocal);
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000785 InsertValue(GV, CurModule.Values);
Nuno Lopesaa1aee32008-10-03 15:51:46 +0000786 ID.destroy();
Chris Lattnerb2b96672005-11-12 18:21:21 +0000787 return GV;
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000788 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000789
Nuno Lopesaa1aee32008-10-03 15:51:46 +0000790 ID.destroy();
791
Reid Spenceref9b9a72007-02-05 20:47:22 +0000792 // If this global has a name
Chris Lattnera09000d2004-07-14 23:03:46 +0000793 if (!Name.empty()) {
Reid Spenceref9b9a72007-02-05 20:47:22 +0000794 // if the global we're parsing has an initializer (is a definition) and
795 // has external linkage.
796 if (Initializer && Linkage != GlobalValue::InternalLinkage)
797 // If there is already a global with external linkage with this name
798 if (CurModule.CurrentModule->getGlobalVariable(Name, false)) {
799 // If we allow this GVar to get created, it will be renamed in the
800 // symbol table because it conflicts with an existing GVar. We can't
801 // allow redefinition of GVars whose linking indicates that their name
802 // must stay the same. Issue the error.
803 GenerateError("Redefinition of global variable named '" + Name +
804 "' of type '" + Ty->getDescription() + "'");
805 return 0;
806 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000807 }
808
809 // Otherwise there is no existing GV to use, create one now.
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000810 GlobalVariable *GV =
Misha Brukman5a2a3822005-05-10 22:02:28 +0000811 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
Christopher Lambfe63fb92007-12-11 08:59:05 +0000812 CurModule.CurrentModule, IsThreadLocal, AddressSpace);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000813 GV->setVisibility(Visibility);
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000814 InsertValue(GV, CurModule.Values);
Chris Lattnerb2b96672005-11-12 18:21:21 +0000815 return GV;
Chris Lattnerd88998d2004-07-14 08:23:52 +0000816}
Chris Lattner8ab406d2004-07-13 07:59:27 +0000817
Reid Spencer75719bf2004-05-26 21:48:31 +0000818// setTypeName - Set the specified type to the name given. The name may be
819// null potentially, in which case this is a noop. The string passed in is
820// assumed to be a malloc'd string buffer, and is freed by this function.
821//
822// This function returns true if the type has already been defined, but is
823// allowed to be redefined in the specified context. If the name is a new name
824// for the type plane, it is inserted and false is returned.
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000825static bool setTypeName(const Type *T, std::string *NameStr) {
Reid Spencer1755a9a2007-02-05 17:01:20 +0000826 assert(!inFunctionScope() && "Can't give types function-local names!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000827 if (NameStr == 0) return false;
Eric Christopher2a5196f2008-09-24 04:55:49 +0000828
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000829 std::string Name(*NameStr); // Copy string
830 delete NameStr; // Free old string
Reid Spencer75719bf2004-05-26 21:48:31 +0000831
832 // We don't allow assigning names to void type
Reid Spencer5b7e7532006-09-28 19:28:24 +0000833 if (T == Type::VoidTy) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000834 GenerateError("Can't assign name '" + Name + "' to the void type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000835 return false;
836 }
Reid Spencer75719bf2004-05-26 21:48:31 +0000837
Chris Lattnera09000d2004-07-14 23:03:46 +0000838 // Set the type name, checking for conflicts as we do so.
839 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000840
Chris Lattnera09000d2004-07-14 23:03:46 +0000841 if (AlreadyExists) { // Inserting a name that is already defined???
842 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
Reid Spencer1755a9a2007-02-05 17:01:20 +0000843 assert(Existing && "Conflict but no matching type?!");
Chris Lattnera09000d2004-07-14 23:03:46 +0000844
Reid Spencer75719bf2004-05-26 21:48:31 +0000845 // There is only one case where this is allowed: when we are refining an
846 // opaque type. In this case, Existing will be an opaque type.
847 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
848 // We ARE replacing an opaque type!
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000849 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000850 return true;
851 }
852
853 // Otherwise, this is an attempt to redefine a type. That's okay if
854 // the redefinition is identical to the original. This will be so if
855 // Existing and T point to the same Type object. In this one case we
856 // allow the equivalent redefinition.
857 if (Existing == T) return true; // Yes, it's equal.
858
859 // Any other kind of (non-equivalent) redefinition is an error.
Reid Spencer90e2f632007-01-05 21:50:38 +0000860 GenerateError("Redefinition of type named '" + Name + "' of type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +0000861 T->getDescription() + "'");
Reid Spencer75719bf2004-05-26 21:48:31 +0000862 }
863
Reid Spencer75719bf2004-05-26 21:48:31 +0000864 return false;
865}
Chris Lattner8896eda2001-07-09 19:38:36 +0000866
Chris Lattner30c89792001-09-07 16:35:17 +0000867//===----------------------------------------------------------------------===//
868// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000869//
Chris Lattner8896eda2001-07-09 19:38:36 +0000870
Chris Lattner3b073862004-02-09 03:19:29 +0000871// TypeContains - Returns true if Ty directly contains E in it.
Chris Lattner30c89792001-09-07 16:35:17 +0000872//
873static bool TypeContains(const Type *Ty, const Type *E) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000874 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
Chris Lattner6d8dbec2004-12-08 16:13:53 +0000875 E) != Ty->subtype_end();
Chris Lattner3b073862004-02-09 03:19:29 +0000876}
877
878namespace {
879 struct UpRefRecord {
880 // NestingLevel - The number of nesting levels that need to be popped before
881 // this type is resolved.
882 unsigned NestingLevel;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000883
Chris Lattner3b073862004-02-09 03:19:29 +0000884 // LastContainedTy - This is the type at the current binding level for the
885 // type. Every time we reduce the nesting level, this gets updated.
886 const Type *LastContainedTy;
887
888 // UpRefTy - This is the actual opaque type that the upreference is
889 // represented with.
890 OpaqueType *UpRefTy;
891
892 UpRefRecord(unsigned NL, OpaqueType *URTy)
893 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
894 };
Chris Lattner30c89792001-09-07 16:35:17 +0000895}
Chris Lattner698b56e2001-07-20 19:15:08 +0000896
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000897// UpRefs - A list of the outstanding upreferences that need to be resolved.
Chris Lattner3b073862004-02-09 03:19:29 +0000898static std::vector<UpRefRecord> UpRefs;
Chris Lattner30c89792001-09-07 16:35:17 +0000899
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000900/// HandleUpRefs - Every time we finish a new layer of types, this function is
901/// called. It loops through the UpRefs vector, which is a list of the
902/// currently active types. For each type, if the up reference is contained in
903/// the newly completed type, we decrement the level count. When the level
904/// count reaches zero, the upreferenced type is the type that is passed in:
905/// thus we can complete the cycle.
906///
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000907static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner703e92f2006-08-18 17:34:24 +0000908 // If Ty isn't abstract, or if there are no up-references in it, then there is
909 // nothing to resolve here.
910 if (!ty->isAbstract() || UpRefs.empty()) return ty;
Eric Christopher2a5196f2008-09-24 04:55:49 +0000911
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000912 PATypeHolder Ty(ty);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000913 UR_OUT("Type '" << Ty->getDescription() <<
Chris Lattner5084d032001-11-02 07:46:26 +0000914 "' newly formed. Resolving upreferences.\n" <<
915 UpRefs.size() << " upreferences active!\n");
Chris Lattner026a8ce2004-02-09 18:53:54 +0000916
917 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
918 // to zero), we resolve them all together before we resolve them to Ty. At
919 // the end of the loop, if there is anything to resolve to Ty, it will be in
920 // this variable.
921 OpaqueType *TypeToResolve = 0;
922
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000923 for (unsigned i = 0; i != UpRefs.size(); ++i) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000924 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
925 << UpRefs[i].second->getDescription() << ") = "
Reid Spencer3271ed52004-07-18 00:08:11 +0000926 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
Chris Lattner3b073862004-02-09 03:19:29 +0000927 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
928 // Decrement level of upreference
929 unsigned Level = --UpRefs[i].NestingLevel;
930 UpRefs[i].LastContainedTy = Ty;
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000931 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
Misha Brukman5a2a3822005-05-10 22:02:28 +0000932 if (Level == 0) { // Upreference should be resolved!
Chris Lattner026a8ce2004-02-09 18:53:54 +0000933 if (!TypeToResolve) {
934 TypeToResolve = UpRefs[i].UpRefTy;
935 } else {
936 UR_OUT(" * Resolving upreference for "
937 << UpRefs[i].second->getDescription() << "\n";
938 std::string OldName = UpRefs[i].UpRefTy->getDescription());
939 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
940 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
941 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
942 }
Reid Spencer3271ed52004-07-18 00:08:11 +0000943 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000944 --i; // Do not skip the next element...
Chris Lattner30c89792001-09-07 16:35:17 +0000945 }
946 }
Chris Lattner8896eda2001-07-09 19:38:36 +0000947 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000948
Chris Lattner026a8ce2004-02-09 18:53:54 +0000949 if (TypeToResolve) {
950 UR_OUT(" * Resolving upreference for "
951 << UpRefs[i].second->getDescription() << "\n";
Chris Lattner16af11d2004-02-09 21:03:38 +0000952 std::string OldName = TypeToResolve->getDescription());
Chris Lattner026a8ce2004-02-09 18:53:54 +0000953 TypeToResolve->refineAbstractTypeTo(Ty);
954 }
955
Chris Lattner8896eda2001-07-09 19:38:36 +0000956 return Ty;
957}
958
Chris Lattner00950542001-06-06 20:29:01 +0000959//===----------------------------------------------------------------------===//
960// RunVMAsmParser - Define an interface to this parser
961//===----------------------------------------------------------------------===//
962//
Reid Spencere1553cc2006-12-31 05:40:12 +0000963static Module* RunParser(Module * M);
964
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000965Module *llvm::RunVMAsmParser(llvm::MemoryBuffer *MB) {
966 InitLLLexer(MB);
967 Module *M = RunParser(new Module(LLLgetFilename()));
968 FreeLexer();
969 return M;
Chris Lattner00950542001-06-06 20:29:01 +0000970}
971
972%}
973
974%union {
Brian Gaeked0fde302003-11-11 22:41:34 +0000975 llvm::Module *ModuleVal;
976 llvm::Function *FunctionVal;
Brian Gaeked0fde302003-11-11 22:41:34 +0000977 llvm::BasicBlock *BasicBlockVal;
978 llvm::TerminatorInst *TermInstVal;
979 llvm::Instruction *InstVal;
Reid Spencer08de34b2006-12-03 05:45:44 +0000980 llvm::Constant *ConstVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000981
Reid Spencer08de34b2006-12-03 05:45:44 +0000982 const llvm::Type *PrimType;
Reid Spencere1553cc2006-12-31 05:40:12 +0000983 std::list<llvm::PATypeHolder> *TypeList;
Reid Spencer08de34b2006-12-03 05:45:44 +0000984 llvm::PATypeHolder *TypeVal;
985 llvm::Value *ValueVal;
Reid Spencer08de34b2006-12-03 05:45:44 +0000986 std::vector<llvm::Value*> *ValueList;
Dan Gohman81a0c0b2008-05-31 00:58:22 +0000987 std::vector<unsigned> *ConstantList;
Reid Spencere1553cc2006-12-31 05:40:12 +0000988 llvm::ArgListType *ArgList;
989 llvm::TypeWithAttrs TypeWithAttrs;
990 llvm::TypeWithAttrsList *TypeWithAttrsList;
Dale Johanneseneb57ea72007-11-05 21:20:28 +0000991 llvm::ParamList *ParamList;
Reid Spencere1553cc2006-12-31 05:40:12 +0000992
Misha Brukman5a2a3822005-05-10 22:02:28 +0000993 // Represent the RHS of PHI node
Reid Spencer08de34b2006-12-03 05:45:44 +0000994 std::list<std::pair<llvm::Value*,
995 llvm::BasicBlock*> > *PHIList;
Brian Gaeked0fde302003-11-11 22:41:34 +0000996 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
Reid Spencer08de34b2006-12-03 05:45:44 +0000997 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000998
Brian Gaeked0fde302003-11-11 22:41:34 +0000999 llvm::GlobalValue::LinkageTypes Linkage;
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001000 llvm::GlobalValue::VisibilityTypes Visibility;
Devang Patel05988662008-09-25 21:00:45 +00001001 llvm::Attributes Attributes;
Reid Spencerf2310042007-02-28 02:23:44 +00001002 llvm::APInt *APIntVal;
Chris Lattner30c89792001-09-07 16:35:17 +00001003 int64_t SInt64Val;
1004 uint64_t UInt64Val;
1005 int SIntVal;
1006 unsigned UIntVal;
Dale Johannesen43421b32007-09-06 18:13:44 +00001007 llvm::APFloat *FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +00001008 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +00001009
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001010 std::string *StrVal; // This memory must be deleted
1011 llvm::ValID ValIDVal;
Chris Lattner00950542001-06-06 20:29:01 +00001012
Reid Spencer08de34b2006-12-03 05:45:44 +00001013 llvm::Instruction::BinaryOps BinaryOpVal;
1014 llvm::Instruction::TermOps TermOpVal;
1015 llvm::Instruction::MemoryOps MemOpVal;
1016 llvm::Instruction::CastOps CastOpVal;
1017 llvm::Instruction::OtherOps OtherOpVal;
Reid Spencer08de34b2006-12-03 05:45:44 +00001018 llvm::ICmpInst::Predicate IPredicate;
1019 llvm::FCmpInst::Predicate FPredicate;
Chris Lattner00950542001-06-06 20:29:01 +00001020}
1021
Eric Christopher2a5196f2008-09-24 04:55:49 +00001022%type <ModuleVal> Module
Chris Lattner79df7c02002-03-26 18:01:55 +00001023%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +00001024%type <BasicBlockVal> BasicBlock InstructionList
1025%type <TermInstVal> BBTerminatorInst
1026%type <InstVal> Inst InstVal MemoryInst
Anton Korobeynikova80e1182007-04-28 13:45:00 +00001027%type <ConstVal> ConstVal ConstExpr AliaseeRef
Chris Lattner6cdb0112001-11-26 16:54:11 +00001028%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +00001029%type <ArgList> ArgList ArgListH
Chris Lattnerc24d2082001-06-11 15:04:20 +00001030%type <PHIList> PHIList
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001031%type <ParamList> ParamList // For call param lists & GEP indices
Reid Spencere1553cc2006-12-31 05:40:12 +00001032%type <ValueList> IndexList // For GEP indices
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001033%type <ConstantList> ConstantIndexList // For insertvalue/extractvalue indices
Eric Christopher2a5196f2008-09-24 04:55:49 +00001034%type <TypeList> TypeListI
Reid Spencere1553cc2006-12-31 05:40:12 +00001035%type <TypeWithAttrsList> ArgTypeList ArgTypeListI
Reid Spencer2c261782007-01-05 17:06:19 +00001036%type <TypeWithAttrs> ArgType
Chris Lattner00950542001-06-06 20:29:01 +00001037%type <JumpTable> JumpTable
Chris Lattner4ad02e72003-04-16 20:28:45 +00001038%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00001039%type <BoolVal> ThreadLocal // 'thread_local' or not
Chris Lattner15c9c032003-09-08 18:20:29 +00001040%type <BoolVal> OptVolatile // 'volatile' or not
Chris Lattnerddb6db42005-05-06 05:51:46 +00001041%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
Chris Lattneraa2c8532006-01-25 22:26:43 +00001042%type <BoolVal> OptSideEffect // 'sideeffect' or not.
Reid Spencere1553cc2006-12-31 05:40:12 +00001043%type <Linkage> GVInternalLinkage GVExternalLinkage
1044%type <Linkage> FunctionDefineLinkage FunctionDeclareLinkage
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001045%type <Linkage> AliasLinkage
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001046%type <Visibility> GVVisibilityStyle
Chris Lattner00950542001-06-06 20:29:01 +00001047
Chris Lattner2079fde2001-10-13 06:41:08 +00001048// ValueRef - Unresolved reference to a definition or BB
1049%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001050%type <ValueVal> ResolvedVal // <type> <valref> pair
Devang Patel5af2f632008-02-20 22:39:45 +00001051%type <ValueList> ReturnedVal
Chris Lattner00950542001-06-06 20:29:01 +00001052// Tokens and types for handling constant integer values
1053//
1054// ESINT64VAL - A negative number within long long range
1055%token <SInt64Val> ESINT64VAL
1056
1057// EUINT64VAL - A positive number within uns. long long range
1058%token <UInt64Val> EUINT64VAL
Chris Lattner00950542001-06-06 20:29:01 +00001059
Eric Christopher2a5196f2008-09-24 04:55:49 +00001060// ESAPINTVAL - A negative number with arbitrary precision
Reid Spencerf2310042007-02-28 02:23:44 +00001061%token <APIntVal> ESAPINTVAL
1062
Eric Christopher2a5196f2008-09-24 04:55:49 +00001063// EUAPINTVAL - A positive number with arbitrary precision
Reid Spencerf2310042007-02-28 02:23:44 +00001064%token <APIntVal> EUAPINTVAL
1065
Reid Spencerb2d17862007-01-26 08:04:51 +00001066%token <UIntVal> LOCALVAL_ID GLOBALVAL_ID // %123 @123
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001067%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +00001068
1069// Built in types...
Reid Spencer2c261782007-01-05 17:06:19 +00001070%type <TypeVal> Types ResultTypes
Reid Spencere1553cc2006-12-31 05:40:12 +00001071%type <PrimType> IntType FPType PrimType // Classifications
Eric Christopher2a5196f2008-09-24 04:55:49 +00001072%token <PrimType> VOID INTTYPE
Dale Johannesen320fc8a2007-08-03 01:03:46 +00001073%token <PrimType> FLOAT DOUBLE X86_FP80 FP128 PPC_FP128 LABEL
Reid Spencerb951bc02006-12-29 20:29:48 +00001074%token TYPE
Chris Lattner00950542001-06-06 20:29:01 +00001075
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001076
Eric Christopher2a5196f2008-09-24 04:55:49 +00001077%token<StrVal> LOCALVAR GLOBALVAR LABELSTR
Reid Spenceraa7c2f82007-05-19 07:21:26 +00001078%token<StrVal> STRINGCONSTANT ATSTRINGCONSTANT PCTSTRINGCONSTANT
Reid Spencerb2d17862007-01-26 08:04:51 +00001079%type <StrVal> LocalName OptLocalName OptLocalAssign
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001080%type <StrVal> GlobalName OptGlobalAssign GlobalAssign
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00001081%type <StrVal> OptSection SectionString OptGC
Chris Lattner00950542001-06-06 20:29:01 +00001082
Christopher Lambd49e18d2007-12-12 08:44:39 +00001083%type <UIntVal> OptAlign OptCAlign OptAddrSpace
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001084
Reid Spencer744d0362007-04-09 01:55:42 +00001085%token ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001086%token DECLARE DEFINE GLOBAL CONSTANT SECTION ALIAS VOLATILE THREAD_LOCAL
Reid Spencere1553cc2006-12-31 05:40:12 +00001087%token TO DOTDOTDOT NULL_TOK UNDEF INTERNAL LINKONCE WEAK APPENDING
Dale Johannesen40e829f2008-05-14 20:14:09 +00001088%token DLLIMPORT DLLEXPORT EXTERN_WEAK COMMON
Christopher Lambfe63fb92007-12-11 08:59:05 +00001089%token OPAQUE EXTERNAL TARGET TRIPLE ALIGN ADDRSPACE
Chris Lattneraa2c8532006-01-25 22:26:43 +00001090%token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001091%token CC_TOK CCC_TOK FASTCC_TOK COLDCC_TOK X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
Nick Lewycky280a6e62008-04-25 16:53:59 +00001092%token DATALAYOUT
Chris Lattner6ac28092008-08-29 17:12:13 +00001093%type <UIntVal> OptCallingConv LocalNumber
Devang Patel05988662008-09-25 21:00:45 +00001094%type <Attributes> OptAttributes Attribute
1095%type <Attributes> OptFuncAttrs FuncAttr
Devang Patel652203f2008-09-29 20:49:50 +00001096%type <Attributes> OptRetAttrs RetAttr
Chris Lattner00950542001-06-06 20:29:01 +00001097
Misha Brukman5a2a3822005-05-10 22:02:28 +00001098// Basic Block Terminating Operators
Chris Lattner16710e92004-10-16 18:17:13 +00001099%token <TermOpVal> RET BR SWITCH INVOKE UNWIND UNREACHABLE
Chris Lattner00950542001-06-06 20:29:01 +00001100
Misha Brukman5a2a3822005-05-10 22:02:28 +00001101// Binary Operators
Reid Spencere4d87aa2006-12-23 06:05:41 +00001102%type <BinaryOpVal> ArithmeticOps LogicalOps // Binops Subcatagories
Reid Spencer0a783f72006-11-02 01:53:59 +00001103%token <BinaryOpVal> ADD SUB MUL UDIV SDIV FDIV UREM SREM FREM AND OR XOR
Reid Spencer832254e2007-02-02 02:16:23 +00001104%token <BinaryOpVal> SHL LSHR ASHR
1105
Eric Christopher2a5196f2008-09-24 04:55:49 +00001106%token <OtherOpVal> ICMP FCMP VICMP VFCMP
Reid Spencer08de34b2006-12-03 05:45:44 +00001107%type <IPredicate> IPredicates
Reid Spencer08de34b2006-12-03 05:45:44 +00001108%type <FPredicate> FPredicates
Eric Christopher2a5196f2008-09-24 04:55:49 +00001109%token EQ NE SLT SGT SLE SGE ULT UGT ULE UGE
Reid Spencer9f746f42006-12-03 06:58:07 +00001110%token OEQ ONE OLT OGT OLE OGE ORD UNO UEQ UNE
Chris Lattner00950542001-06-06 20:29:01 +00001111
1112// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001113%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +00001114
Reid Spencer3da59db2006-11-27 01:05:10 +00001115// Cast Operators
1116%type <CastOpVal> CastOps
1117%token <CastOpVal> TRUNC ZEXT SEXT FPTRUNC FPEXT BITCAST
1118%token <CastOpVal> UITOFP SITOFP FPTOUI FPTOSI INTTOPTR PTRTOINT
1119
Chris Lattner027dcc52001-07-08 21:10:27 +00001120// Other Operators
Reid Spencer832254e2007-02-02 02:16:23 +00001121%token <OtherOpVal> PHI_TOK SELECT VAARG
Chris Lattner4c949082006-04-08 01:18:35 +00001122%token <OtherOpVal> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
Devang Pateld6ffcf92008-02-19 22:26:37 +00001123%token <OtherOpVal> GETRESULT
Dan Gohmane4977cf2008-05-23 01:55:30 +00001124%token <OtherOpVal> EXTRACTVALUE INSERTVALUE
Chris Lattnerddb6db42005-05-06 05:51:46 +00001125
Reid Spencer2c261782007-01-05 17:06:19 +00001126// Function Attributes
Duncan Sands36397f52007-07-27 12:58:54 +00001127%token SIGNEXT ZEROEXT NORETURN INREG SRET NOUNWIND NOALIAS BYVAL NEST
Devang Patel2c9c3e72008-09-26 23:51:19 +00001128%token READNONE READONLY GC OPTSIZE NOINLINE ALWAYSINLINE
Devang Pateld4980812008-09-02 20:52:40 +00001129
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001130// Visibility Styles
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +00001131%token DEFAULT HIDDEN PROTECTED
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001132
Chris Lattner00950542001-06-06 20:29:01 +00001133%start Module
1134%%
1135
Chris Lattner00950542001-06-06 20:29:01 +00001136
Misha Brukman5a2a3822005-05-10 22:02:28 +00001137// Operations that are notably excluded from this list include:
Chris Lattner00950542001-06-06 20:29:01 +00001138// RET, BR, & SWITCH because they end basic blocks and are treated specially.
1139//
Reid Spencer0a783f72006-11-02 01:53:59 +00001140ArithmeticOps: ADD | SUB | MUL | UDIV | SDIV | FDIV | UREM | SREM | FREM;
Reid Spencer832254e2007-02-02 02:16:23 +00001141LogicalOps : SHL | LSHR | ASHR | AND | OR | XOR;
Eric Christopher2a5196f2008-09-24 04:55:49 +00001142CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | BITCAST |
Reid Spencer3da59db2006-11-27 01:05:10 +00001143 UITOFP | SITOFP | FPTOUI | FPTOSI | INTTOPTR | PTRTOINT;
Reid Spencer832254e2007-02-02 02:16:23 +00001144
Eric Christopher2a5196f2008-09-24 04:55:49 +00001145IPredicates
Reid Spencer763ed5e2006-12-04 05:20:06 +00001146 : EQ { $$ = ICmpInst::ICMP_EQ; } | NE { $$ = ICmpInst::ICMP_NE; }
Reid Spencer9f746f42006-12-03 06:58:07 +00001147 | SLT { $$ = ICmpInst::ICMP_SLT; } | SGT { $$ = ICmpInst::ICMP_SGT; }
1148 | SLE { $$ = ICmpInst::ICMP_SLE; } | SGE { $$ = ICmpInst::ICMP_SGE; }
1149 | ULT { $$ = ICmpInst::ICMP_ULT; } | UGT { $$ = ICmpInst::ICMP_UGT; }
Eric Christopher2a5196f2008-09-24 04:55:49 +00001150 | ULE { $$ = ICmpInst::ICMP_ULE; } | UGE { $$ = ICmpInst::ICMP_UGE; }
Reid Spencer9f746f42006-12-03 06:58:07 +00001151 ;
1152
Eric Christopher2a5196f2008-09-24 04:55:49 +00001153FPredicates
Reid Spencer9f746f42006-12-03 06:58:07 +00001154 : OEQ { $$ = FCmpInst::FCMP_OEQ; } | ONE { $$ = FCmpInst::FCMP_ONE; }
1155 | OLT { $$ = FCmpInst::FCMP_OLT; } | OGT { $$ = FCmpInst::FCMP_OGT; }
1156 | OLE { $$ = FCmpInst::FCMP_OLE; } | OGE { $$ = FCmpInst::FCMP_OGE; }
1157 | ORD { $$ = FCmpInst::FCMP_ORD; } | UNO { $$ = FCmpInst::FCMP_UNO; }
1158 | UEQ { $$ = FCmpInst::FCMP_UEQ; } | UNE { $$ = FCmpInst::FCMP_UNE; }
1159 | ULT { $$ = FCmpInst::FCMP_ULT; } | UGT { $$ = FCmpInst::FCMP_UGT; }
1160 | ULE { $$ = FCmpInst::FCMP_ULE; } | UGE { $$ = FCmpInst::FCMP_UGE; }
1161 | TRUETOK { $$ = FCmpInst::FCMP_TRUE; }
1162 | FALSETOK { $$ = FCmpInst::FCMP_FALSE; }
1163 ;
Chris Lattner00950542001-06-06 20:29:01 +00001164
Eric Christopher2a5196f2008-09-24 04:55:49 +00001165// These are some types that allow classification if we only want a particular
Chris Lattnere98dda62001-07-14 06:10:16 +00001166// thing... for example, only a signed, unsigned, or integral type.
Reid Spencera54b7cb2007-01-12 07:05:14 +00001167IntType : INTTYPE;
Dale Johannesen320fc8a2007-08-03 01:03:46 +00001168FPType : FLOAT | DOUBLE | PPC_FP128 | FP128 | X86_FP80;
Chris Lattner00950542001-06-06 20:29:01 +00001169
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001170LocalName : LOCALVAR | STRINGCONSTANT | PCTSTRINGCONSTANT ;
Reid Spencerb2d17862007-01-26 08:04:51 +00001171OptLocalName : LocalName | /*empty*/ { $$ = 0; };
1172
Christopher Lambd49e18d2007-12-12 08:44:39 +00001173OptAddrSpace : ADDRSPACE '(' EUINT64VAL ')' { $$=$3; }
1174 | /*empty*/ { $$=0; };
1175
Reid Spencerb2d17862007-01-26 08:04:51 +00001176/// OptLocalAssign - Value producing statements have an optional assignment
1177/// component.
1178OptLocalAssign : LocalName '=' {
1179 $$ = $1;
1180 CHECK_FOR_ERROR
1181 }
1182 | /*empty*/ {
1183 $$ = 0;
1184 CHECK_FOR_ERROR
1185 };
1186
Chris Lattner6ac28092008-08-29 17:12:13 +00001187LocalNumber : LOCALVAL_ID '=' {
1188 $$ = $1;
1189 CHECK_FOR_ERROR
1190};
1191
1192
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001193GlobalName : GLOBALVAR | ATSTRINGCONSTANT ;
Reid Spencerb2d17862007-01-26 08:04:51 +00001194
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001195OptGlobalAssign : GlobalAssign
Misha Brukman5a2a3822005-05-10 22:02:28 +00001196 | /*empty*/ {
1197 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00001198 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001199 };
Chris Lattner00950542001-06-06 20:29:01 +00001200
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001201GlobalAssign : GlobalName '=' {
1202 $$ = $1;
1203 CHECK_FOR_ERROR
Anton Korobeynikovc0fabcb2007-04-25 18:07:40 +00001204 };
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001205
Eric Christopher2a5196f2008-09-24 04:55:49 +00001206GVInternalLinkage
1207 : INTERNAL { $$ = GlobalValue::InternalLinkage; }
1208 | WEAK { $$ = GlobalValue::WeakLinkage; }
Reid Spencerb951bc02006-12-29 20:29:48 +00001209 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1210 | APPENDING { $$ = GlobalValue::AppendingLinkage; }
Eric Christopher2a5196f2008-09-24 04:55:49 +00001211 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
Dale Johannesen40e829f2008-05-14 20:14:09 +00001212 | COMMON { $$ = GlobalValue::CommonLinkage; }
Reid Spencerb951bc02006-12-29 20:29:48 +00001213 ;
1214
1215GVExternalLinkage
1216 : DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1217 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
1218 | EXTERNAL { $$ = GlobalValue::ExternalLinkage; }
1219 ;
1220
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001221GVVisibilityStyle
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +00001222 : /*empty*/ { $$ = GlobalValue::DefaultVisibility; }
1223 | DEFAULT { $$ = GlobalValue::DefaultVisibility; }
1224 | HIDDEN { $$ = GlobalValue::HiddenVisibility; }
1225 | PROTECTED { $$ = GlobalValue::ProtectedVisibility; }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001226 ;
1227
Reid Spencere1553cc2006-12-31 05:40:12 +00001228FunctionDeclareLinkage
1229 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
Eric Christopher2a5196f2008-09-24 04:55:49 +00001230 | DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
Reid Spencere1553cc2006-12-31 05:40:12 +00001231 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
Reid Spencerb951bc02006-12-29 20:29:48 +00001232 ;
Eric Christopher2a5196f2008-09-24 04:55:49 +00001233
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001234FunctionDefineLinkage
Reid Spencere1553cc2006-12-31 05:40:12 +00001235 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1236 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
Reid Spencerb951bc02006-12-29 20:29:48 +00001237 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1238 | WEAK { $$ = GlobalValue::WeakLinkage; }
Eric Christopher2a5196f2008-09-24 04:55:49 +00001239 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
1240 ;
Chris Lattner30c89792001-09-07 16:35:17 +00001241
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001242AliasLinkage
1243 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1244 | WEAK { $$ = GlobalValue::WeakLinkage; }
1245 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
1246 ;
1247
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00001248OptCallingConv : /*empty*/ { $$ = CallingConv::C; } |
1249 CCC_TOK { $$ = CallingConv::C; } |
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00001250 FASTCC_TOK { $$ = CallingConv::Fast; } |
1251 COLDCC_TOK { $$ = CallingConv::Cold; } |
1252 X86_STDCALLCC_TOK { $$ = CallingConv::X86_StdCall; } |
1253 X86_FASTCALLCC_TOK { $$ = CallingConv::X86_FastCall; } |
1254 CC_TOK EUINT64VAL {
Chris Lattnera8e8f162005-05-06 20:27:19 +00001255 if ((unsigned)$2 != $2)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001256 GEN_ERROR("Calling conv too large");
Chris Lattnera8e8f162005-05-06 20:27:19 +00001257 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001258 CHECK_FOR_ERROR
Chris Lattnera8e8f162005-05-06 20:27:19 +00001259 };
1260
Devang Patel05988662008-09-25 21:00:45 +00001261Attribute : ZEROEXT { $$ = Attribute::ZExt; }
1262 | ZEXT { $$ = Attribute::ZExt; }
1263 | SIGNEXT { $$ = Attribute::SExt; }
1264 | SEXT { $$ = Attribute::SExt; }
1265 | INREG { $$ = Attribute::InReg; }
1266 | SRET { $$ = Attribute::StructRet; }
1267 | NOALIAS { $$ = Attribute::NoAlias; }
1268 | BYVAL { $$ = Attribute::ByVal; }
1269 | NEST { $$ = Attribute::Nest; }
Eric Christopher2a5196f2008-09-24 04:55:49 +00001270 | ALIGN EUINT64VAL { $$ =
Devang Patel05988662008-09-25 21:00:45 +00001271 Attribute::constructAlignmentFromInt($2); }
Reid Spencere1553cc2006-12-31 05:40:12 +00001272 ;
1273
Devang Patel05988662008-09-25 21:00:45 +00001274OptAttributes : /* empty */ { $$ = Attribute::None; }
1275 | OptAttributes Attribute {
Reid Spencer5694b6e2007-04-09 06:17:21 +00001276 $$ = $1 | $2;
Reid Spencere1553cc2006-12-31 05:40:12 +00001277 }
1278 ;
1279
Devang Patel652203f2008-09-29 20:49:50 +00001280RetAttr : INREG { $$ = Attribute::InReg; }
1281 | ZEROEXT { $$ = Attribute::ZExt; }
1282 | SIGNEXT { $$ = Attribute::SExt; }
1283 ;
1284
1285OptRetAttrs : /* empty */ { $$ = Attribute::None; }
1286 | OptRetAttrs RetAttr {
1287 $$ = $1 | $2;
1288 }
1289 ;
1290
1291
Devang Patel05988662008-09-25 21:00:45 +00001292FuncAttr : NORETURN { $$ = Attribute::NoReturn; }
1293 | NOUNWIND { $$ = Attribute::NoUnwind; }
1294 | INREG { $$ = Attribute::InReg; }
1295 | ZEROEXT { $$ = Attribute::ZExt; }
1296 | SIGNEXT { $$ = Attribute::SExt; }
1297 | READNONE { $$ = Attribute::ReadNone; }
1298 | READONLY { $$ = Attribute::ReadOnly; }
Devang Patel2c9c3e72008-09-26 23:51:19 +00001299 | NOINLINE { $$ = Attribute::NoInline }
1300 | ALWAYSINLINE { $$ = Attribute::AlwaysInline }
1301 | OPTSIZE { $$ = Attribute::OptimizeForSize }
Reid Spencer2c261782007-01-05 17:06:19 +00001302 ;
1303
Devang Patel05988662008-09-25 21:00:45 +00001304OptFuncAttrs : /* empty */ { $$ = Attribute::None; }
Reid Spencer2c261782007-01-05 17:06:19 +00001305 | OptFuncAttrs FuncAttr {
Reid Spencer5694b6e2007-04-09 06:17:21 +00001306 $$ = $1 | $2;
Reid Spencer2c261782007-01-05 17:06:19 +00001307 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001308 ;
1309
Devang Patel652203f2008-09-29 20:49:50 +00001310
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00001311OptGC : /* empty */ { $$ = 0; }
1312 | GC STRINGCONSTANT {
1313 $$ = $2;
1314 }
1315 ;
1316
Chris Lattner66db8e42005-11-06 06:34:12 +00001317// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1318// a comma before it.
Chris Lattner87ac9722005-11-06 06:46:28 +00001319OptAlign : /*empty*/ { $$ = 0; } |
Chris Lattnerb2b96672005-11-12 18:21:21 +00001320 ALIGN EUINT64VAL {
1321 $$ = $2;
1322 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001323 GEN_ERROR("Alignment must be a power of two");
Reid Spencer61c83e02006-08-18 08:43:06 +00001324 CHECK_FOR_ERROR
Chris Lattnerb2b96672005-11-12 18:21:21 +00001325};
Chris Lattner66db8e42005-11-06 06:34:12 +00001326OptCAlign : /*empty*/ { $$ = 0; } |
Chris Lattnerb2b96672005-11-12 18:21:21 +00001327 ',' ALIGN EUINT64VAL {
1328 $$ = $3;
1329 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001330 GEN_ERROR("Alignment must be a power of two");
Reid Spencer61c83e02006-08-18 08:43:06 +00001331 CHECK_FOR_ERROR
Chris Lattnerb2b96672005-11-12 18:21:21 +00001332};
1333
Chris Lattner66db8e42005-11-06 06:34:12 +00001334
Christopher Lambfe63fb92007-12-11 08:59:05 +00001335
Chris Lattner164c3782005-11-12 00:11:10 +00001336SectionString : SECTION STRINGCONSTANT {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001337 for (unsigned i = 0, e = $2->length(); i != e; ++i)
1338 if ((*$2)[i] == '"' || (*$2)[i] == '\\')
Reid Spencerf4fa5902007-02-05 10:16:10 +00001339 GEN_ERROR("Invalid character in section name");
Chris Lattner164c3782005-11-12 00:11:10 +00001340 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001341 CHECK_FOR_ERROR
Chris Lattner164c3782005-11-12 00:11:10 +00001342};
1343
1344OptSection : /*empty*/ { $$ = 0; } |
1345 SectionString { $$ = $1; };
Chris Lattner164c3782005-11-12 00:11:10 +00001346
Chris Lattnerb2b96672005-11-12 18:21:21 +00001347// GlobalVarAttributes - Used to pass the attributes string on a global. CurGV
1348// is set to be the global we are processing.
1349//
1350GlobalVarAttributes : /* empty */ {} |
1351 ',' GlobalVarAttribute GlobalVarAttributes {};
1352GlobalVarAttribute : SectionString {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001353 CurGV->setSection(*$1);
1354 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001355 CHECK_FOR_ERROR
Eric Christopher2a5196f2008-09-24 04:55:49 +00001356 }
Chris Lattnerb2b96672005-11-12 18:21:21 +00001357 | ALIGN EUINT64VAL {
1358 if ($2 != 0 && !isPowerOf2_32($2))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001359 GEN_ERROR("Alignment must be a power of two");
Chris Lattnerb2b96672005-11-12 18:21:21 +00001360 CurGV->setAlignment($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001361 CHECK_FOR_ERROR
Chris Lattnerb2b96672005-11-12 18:21:21 +00001362 };
Chris Lattner164c3782005-11-12 00:11:10 +00001363
Chris Lattner30c89792001-09-07 16:35:17 +00001364//===----------------------------------------------------------------------===//
1365// Types includes all predefined types... except void, because it can only be
Eric Christopher2a5196f2008-09-24 04:55:49 +00001366// used in specific contexts (function returning void for example).
Chris Lattner30c89792001-09-07 16:35:17 +00001367
1368// Derived types are added later...
1369//
Dale Johannesen320fc8a2007-08-03 01:03:46 +00001370PrimType : INTTYPE | FLOAT | DOUBLE | PPC_FP128 | FP128 | X86_FP80 | LABEL ;
Reid Spencere1553cc2006-12-31 05:40:12 +00001371
Eric Christopher2a5196f2008-09-24 04:55:49 +00001372Types
Reid Spencere1553cc2006-12-31 05:40:12 +00001373 : OPAQUE {
Reid Spencer08de34b2006-12-03 05:45:44 +00001374 $$ = new PATypeHolder(OpaqueType::get());
Reid Spencer61c83e02006-08-18 08:43:06 +00001375 CHECK_FOR_ERROR
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001376 }
1377 | PrimType {
Reid Spencer08de34b2006-12-03 05:45:44 +00001378 $$ = new PATypeHolder($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001379 CHECK_FOR_ERROR
Reid Spencere1553cc2006-12-31 05:40:12 +00001380 }
Christopher Lambd49e18d2007-12-12 08:44:39 +00001381 | Types OptAddrSpace '*' { // Pointer type?
Reid Spencere1553cc2006-12-31 05:40:12 +00001382 if (*$1 == Type::LabelTy)
1383 GEN_ERROR("Cannot form a pointer to a basic block");
Christopher Lambd49e18d2007-12-12 08:44:39 +00001384 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1, $2)));
Christopher Lambfe63fb92007-12-11 08:59:05 +00001385 delete $1;
1386 CHECK_FOR_ERROR
1387 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001388 | SymbolicValueRef { // Named types are also simple types...
1389 const Type* tmp = getTypeVal($1);
1390 CHECK_FOR_ERROR
1391 $$ = new PATypeHolder(tmp);
1392 }
1393 | '\\' EUINT64VAL { // Type UpReference
Reid Spencerf4fa5902007-02-05 10:16:10 +00001394 if ($2 > (uint64_t)~0U) GEN_ERROR("Value out of range");
Chris Lattner30c89792001-09-07 16:35:17 +00001395 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
Chris Lattner3b073862004-02-09 03:19:29 +00001396 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Reid Spencer08de34b2006-12-03 05:45:44 +00001397 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +00001398 UR_OUT("New Upreference!\n");
Reid Spencer61c83e02006-08-18 08:43:06 +00001399 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001400 }
Reid Spencer863dd882007-04-28 16:06:50 +00001401 | Types '(' ArgTypeListI ')' OptFuncAttrs {
Duncan Sandsdc024672007-11-27 13:23:08 +00001402 // Allow but ignore attributes on function types; this permits auto-upgrade.
1403 // FIXME: remove in LLVM 3.0.
Chris Lattnerf7dedf22008-04-23 05:36:58 +00001404 const Type *RetTy = *$1;
1405 if (!FunctionType::isValidReturnType(RetTy))
1406 GEN_ERROR("Invalid result type for LLVM function");
Eric Christopher2a5196f2008-09-24 04:55:49 +00001407
Chris Lattner9232b992003-04-22 18:42:41 +00001408 std::vector<const Type*> Params;
Reid Spencer5694b6e2007-04-09 06:17:21 +00001409 TypeWithAttrsList::iterator I = $3->begin(), E = $3->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00001410 for (; I != E; ++I ) {
Reid Spencer2c9df212007-03-20 01:13:00 +00001411 const Type *Ty = I->Ty->get();
Reid Spencer2c9df212007-03-20 01:13:00 +00001412 Params.push_back(Ty);
Reid Spencere1553cc2006-12-31 05:40:12 +00001413 }
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001414
Chris Lattner2079fde2001-10-13 06:41:08 +00001415 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1416 if (isVarArg) Params.pop_back();
1417
Anton Korobeynikovf8c751a2007-12-03 21:00:45 +00001418 for (unsigned i = 0; i != Params.size(); ++i)
1419 if (!(Params[i]->isFirstClassType() || isa<OpaqueType>(Params[i])))
1420 GEN_ERROR("Function arguments must be value types!");
1421
1422 CHECK_FOR_ERROR
1423
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001424 FunctionType *FT = FunctionType::get(RetTy, Params, isVarArg);
Reid Spencere1553cc2006-12-31 05:40:12 +00001425 delete $1; // Delete the return type handle
Eric Christopher2a5196f2008-09-24 04:55:49 +00001426 $$ = new PATypeHolder(HandleUpRefs(FT));
Nuno Lopesd0ad6772008-10-05 16:49:03 +00001427
1428 // Delete the argument list
1429 for (I = $3->begin() ; I != E; ++I ) {
1430 delete I->Ty;
1431 }
1432 delete $3;
1433
Reid Spencer61c83e02006-08-18 08:43:06 +00001434 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001435 }
Reid Spencer863dd882007-04-28 16:06:50 +00001436 | VOID '(' ArgTypeListI ')' OptFuncAttrs {
Duncan Sandsdc024672007-11-27 13:23:08 +00001437 // Allow but ignore attributes on function types; this permits auto-upgrade.
1438 // FIXME: remove in LLVM 3.0.
Reid Spencere1553cc2006-12-31 05:40:12 +00001439 std::vector<const Type*> Params;
Reid Spencer5694b6e2007-04-09 06:17:21 +00001440 TypeWithAttrsList::iterator I = $3->begin(), E = $3->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00001441 for ( ; I != E; ++I ) {
Reid Spencer2c9df212007-03-20 01:13:00 +00001442 const Type* Ty = I->Ty->get();
Reid Spencer2c9df212007-03-20 01:13:00 +00001443 Params.push_back(Ty);
Reid Spencere1553cc2006-12-31 05:40:12 +00001444 }
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001445
Reid Spencere1553cc2006-12-31 05:40:12 +00001446 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1447 if (isVarArg) Params.pop_back();
1448
Anton Korobeynikovf8c751a2007-12-03 21:00:45 +00001449 for (unsigned i = 0; i != Params.size(); ++i)
1450 if (!(Params[i]->isFirstClassType() || isa<OpaqueType>(Params[i])))
1451 GEN_ERROR("Function arguments must be value types!");
1452
1453 CHECK_FOR_ERROR
1454
Duncan Sandsdc024672007-11-27 13:23:08 +00001455 FunctionType *FT = FunctionType::get($1, Params, isVarArg);
Eric Christopher2a5196f2008-09-24 04:55:49 +00001456 $$ = new PATypeHolder(HandleUpRefs(FT));
Nuno Lopesd0ad6772008-10-05 16:49:03 +00001457
1458 // Delete the argument list
1459 for (I = $3->begin() ; I != E; ++I ) {
1460 delete I->Ty;
1461 }
1462 delete $3;
1463
Reid Spencere1553cc2006-12-31 05:40:12 +00001464 CHECK_FOR_ERROR
1465 }
1466
1467 | '[' EUINT64VAL 'x' Types ']' { // Sized array type?
Dan Gohman80f0f612008-05-23 21:40:55 +00001468 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, $2)));
Reid Spencer08de34b2006-12-03 05:45:44 +00001469 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00001470 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001471 }
Reid Spencerac9dcb92007-02-15 03:39:18 +00001472 | '<' EUINT64VAL 'x' Types '>' { // Vector type?
Reid Spencer08de34b2006-12-03 05:45:44 +00001473 const llvm::Type* ElemTy = $4->get();
1474 if ((unsigned)$2 != $2)
1475 GEN_ERROR("Unsigned result not equal to signed result");
Chris Lattner42a75512007-01-15 02:27:26 +00001476 if (!ElemTy->isFloatingPoint() && !ElemTy->isInteger())
Reid Spencer9d6565a2007-02-15 02:26:10 +00001477 GEN_ERROR("Element type of a VectorType must be primitive");
Reid Spencer9d6565a2007-02-15 02:26:10 +00001478 $$ = new PATypeHolder(HandleUpRefs(VectorType::get(*$4, (unsigned)$2)));
Reid Spencer08de34b2006-12-03 05:45:44 +00001479 delete $4;
1480 CHECK_FOR_ERROR
Brian Gaeke715c90b2004-08-20 06:00:58 +00001481 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001482 | '{' TypeListI '}' { // Structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001483 std::vector<const Type*> Elements;
Reid Spencer08de34b2006-12-03 05:45:44 +00001484 for (std::list<llvm::PATypeHolder>::iterator I = $2->begin(),
Chris Lattnera08976b2005-02-22 23:13:58 +00001485 E = $2->end(); I != E; ++I)
Reid Spencer08de34b2006-12-03 05:45:44 +00001486 Elements.push_back(*I);
Misha Brukman5a2a3822005-05-10 22:02:28 +00001487
Reid Spencer08de34b2006-12-03 05:45:44 +00001488 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001489 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001490 CHECK_FOR_ERROR
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001491 }
1492 | '{' '}' { // Empty structure type?
Reid Spencer08de34b2006-12-03 05:45:44 +00001493 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Reid Spencer61c83e02006-08-18 08:43:06 +00001494 CHECK_FOR_ERROR
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001495 }
Andrew Lenharth38ecbf12006-12-08 18:06:16 +00001496 | '<' '{' TypeListI '}' '>' {
1497 std::vector<const Type*> Elements;
1498 for (std::list<llvm::PATypeHolder>::iterator I = $3->begin(),
1499 E = $3->end(); I != E; ++I)
1500 Elements.push_back(*I);
1501
1502 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements, true)));
1503 delete $3;
1504 CHECK_FOR_ERROR
1505 }
1506 | '<' '{' '}' '>' { // Empty structure type?
1507 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>(), true));
1508 CHECK_FOR_ERROR
1509 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001510 ;
1511
Eric Christopher2a5196f2008-09-24 04:55:49 +00001512ArgType
Devang Patel05988662008-09-25 21:00:45 +00001513 : Types OptAttributes {
Duncan Sandsdc024672007-11-27 13:23:08 +00001514 // Allow but ignore attributes on function types; this permits auto-upgrade.
1515 // FIXME: remove in LLVM 3.0.
Eric Christopher2a5196f2008-09-24 04:55:49 +00001516 $$.Ty = $1;
Devang Patel05988662008-09-25 21:00:45 +00001517 $$.Attrs = Attribute::None;
Reid Spencere1553cc2006-12-31 05:40:12 +00001518 }
1519 ;
1520
Reid Spencer2c261782007-01-05 17:06:19 +00001521ResultTypes
1522 : Types {
Reid Spencere1553cc2006-12-31 05:40:12 +00001523 if (!UpRefs.empty())
1524 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Devang Patel155b8742008-02-23 01:17:17 +00001525 if (!(*$1)->isFirstClassType() && !isa<StructType>($1->get()))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001526 GEN_ERROR("LLVM functions cannot return aggregate types");
Reid Spencer2c261782007-01-05 17:06:19 +00001527 $$ = $1;
Reid Spencere1553cc2006-12-31 05:40:12 +00001528 }
Reid Spencer2c261782007-01-05 17:06:19 +00001529 | VOID {
1530 $$ = new PATypeHolder(Type::VoidTy);
Reid Spencere1553cc2006-12-31 05:40:12 +00001531 }
1532 ;
1533
1534ArgTypeList : ArgType {
1535 $$ = new TypeWithAttrsList();
1536 $$->push_back($1);
1537 CHECK_FOR_ERROR
1538 }
1539 | ArgTypeList ',' ArgType {
1540 ($$=$1)->push_back($3);
1541 CHECK_FOR_ERROR
1542 }
1543 ;
1544
Eric Christopher2a5196f2008-09-24 04:55:49 +00001545ArgTypeListI
Reid Spencere1553cc2006-12-31 05:40:12 +00001546 : ArgTypeList
1547 | ArgTypeList ',' DOTDOTDOT {
1548 $$=$1;
Devang Patel05988662008-09-25 21:00:45 +00001549 TypeWithAttrs TWA; TWA.Attrs = Attribute::None;
Reid Spencere1553cc2006-12-31 05:40:12 +00001550 TWA.Ty = new PATypeHolder(Type::VoidTy);
1551 $$->push_back(TWA);
1552 CHECK_FOR_ERROR
1553 }
1554 | DOTDOTDOT {
1555 $$ = new TypeWithAttrsList;
Devang Patel05988662008-09-25 21:00:45 +00001556 TypeWithAttrs TWA; TWA.Attrs = Attribute::None;
Reid Spencere1553cc2006-12-31 05:40:12 +00001557 TWA.Ty = new PATypeHolder(Type::VoidTy);
1558 $$->push_back(TWA);
1559 CHECK_FOR_ERROR
1560 }
1561 | /*empty*/ {
1562 $$ = new TypeWithAttrsList();
Reid Spencer61c83e02006-08-18 08:43:06 +00001563 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001564 };
Chris Lattner30c89792001-09-07 16:35:17 +00001565
Eric Christopher2a5196f2008-09-24 04:55:49 +00001566// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +00001567// declaration type lists
1568//
Reid Spencere1553cc2006-12-31 05:40:12 +00001569TypeListI : Types {
Reid Spencer08de34b2006-12-03 05:45:44 +00001570 $$ = new std::list<PATypeHolder>();
Eric Christopher2a5196f2008-09-24 04:55:49 +00001571 $$->push_back(*$1);
Reid Spencer2c9df212007-03-20 01:13:00 +00001572 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001573 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001574 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001575 | TypeListI ',' Types {
Eric Christopher2a5196f2008-09-24 04:55:49 +00001576 ($$=$1)->push_back(*$3);
Reid Spencer2c9df212007-03-20 01:13:00 +00001577 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001578 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001579 };
Chris Lattner30c89792001-09-07 16:35:17 +00001580
Chris Lattnere98dda62001-07-14 06:10:16 +00001581// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +00001582// production is used ONLY to represent constants that show up AFTER a 'const',
1583// 'constant' or 'global' token at global scope. Constants that can be inlined
1584// into other expressions (such as integers and constexprs) are handled by the
1585// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +00001586//
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001587ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencere1553cc2006-12-31 05:40:12 +00001588 if (!UpRefs.empty())
1589 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001590 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001591 if (ATy == 0)
Eric Christopher2a5196f2008-09-24 04:55:49 +00001592 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001593 (*$1)->getDescription() + "'");
Chris Lattner30c89792001-09-07 16:35:17 +00001594 const Type *ETy = ATy->getElementType();
Dan Gohmanebb5a972008-06-23 18:40:28 +00001595 uint64_t NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +00001596
Chris Lattner30c89792001-09-07 16:35:17 +00001597 // Verify that we have the correct size...
Dan Gohman46e803b2008-06-24 01:17:52 +00001598 if (NumElements != uint64_t(-1) && NumElements != $3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001599 GEN_ERROR("Type mismatch: constant sized array initialized with " +
Eric Christopher2a5196f2008-09-24 04:55:49 +00001600 utostr($3->size()) + " arguments, but has size of " +
Dan Gohman46e803b2008-06-24 01:17:52 +00001601 utostr(NumElements) + "");
Chris Lattner00950542001-06-06 20:29:01 +00001602
Chris Lattner30c89792001-09-07 16:35:17 +00001603 // Verify all elements are correct type!
1604 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencer08de34b2006-12-03 05:45:44 +00001605 if (ETy != (*$3)[i]->getType())
Eric Christopher2a5196f2008-09-24 04:55:49 +00001606 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Reid Spencer3271ed52004-07-18 00:08:11 +00001607 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencer08de34b2006-12-03 05:45:44 +00001608 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +00001609 }
1610
Reid Spencer08de34b2006-12-03 05:45:44 +00001611 $$ = ConstantArray::get(ATy, *$3);
1612 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001613 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001614 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001615 | Types '[' ']' {
Reid Spencere1553cc2006-12-31 05:40:12 +00001616 if (!UpRefs.empty())
1617 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001618 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001619 if (ATy == 0)
Eric Christopher2a5196f2008-09-24 04:55:49 +00001620 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001621 (*$1)->getDescription() + "'");
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001622
Dan Gohmanebb5a972008-06-23 18:40:28 +00001623 uint64_t NumElements = ATy->getNumElements();
Eric Christopher2a5196f2008-09-24 04:55:49 +00001624 if (NumElements != uint64_t(-1) && NumElements != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001625 GEN_ERROR("Type mismatch: constant sized array initialized with 0"
Dan Gohman46e803b2008-06-24 01:17:52 +00001626 " arguments, but has size of " + utostr(NumElements) +"");
Reid Spencer08de34b2006-12-03 05:45:44 +00001627 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
1628 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001629 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001630 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001631 | Types 'c' STRINGCONSTANT {
Reid Spencere1553cc2006-12-31 05:40:12 +00001632 if (!UpRefs.empty())
1633 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001634 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001635 if (ATy == 0)
Eric Christopher2a5196f2008-09-24 04:55:49 +00001636 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001637 (*$1)->getDescription() + "'");
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001638
Dan Gohmanebb5a972008-06-23 18:40:28 +00001639 uint64_t NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +00001640 const Type *ETy = ATy->getElementType();
Dan Gohman46e803b2008-06-24 01:17:52 +00001641 if (NumElements != uint64_t(-1) && NumElements != $3->length())
Eric Christopher2a5196f2008-09-24 04:55:49 +00001642 GEN_ERROR("Can't build string constant of size " +
Dan Gohman46e803b2008-06-24 01:17:52 +00001643 utostr($3->length()) +
1644 " when array has size " + utostr(NumElements) + "");
Chris Lattner9232b992003-04-22 18:42:41 +00001645 std::vector<Constant*> Vals;
Reid Spencere1553cc2006-12-31 05:40:12 +00001646 if (ETy == Type::Int8Ty) {
Dan Gohman46e803b2008-06-24 01:17:52 +00001647 for (uint64_t i = 0; i < $3->length(); ++i)
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001648 Vals.push_back(ConstantInt::get(ETy, (*$3)[i]));
Chris Lattner93750fa2001-07-28 17:48:55 +00001649 } else {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001650 delete $3;
Reid Spencerf4fa5902007-02-05 10:16:10 +00001651 GEN_ERROR("Cannot build string arrays of non byte sized elements");
Chris Lattner93750fa2001-07-28 17:48:55 +00001652 }
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001653 delete $3;
Reid Spencer08de34b2006-12-03 05:45:44 +00001654 $$ = ConstantArray::get(ATy, Vals);
1655 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001656 CHECK_FOR_ERROR
Chris Lattner93750fa2001-07-28 17:48:55 +00001657 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00001658 | Types '<' ConstVector '>' { // Nonempty unsized arr
Reid Spencere1553cc2006-12-31 05:40:12 +00001659 if (!UpRefs.empty())
1660 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer9d6565a2007-02-15 02:26:10 +00001661 const VectorType *PTy = dyn_cast<VectorType>($1->get());
Brian Gaeke715c90b2004-08-20 06:00:58 +00001662 if (PTy == 0)
Eric Christopher2a5196f2008-09-24 04:55:49 +00001663 GEN_ERROR("Cannot make packed constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001664 (*$1)->getDescription() + "'");
Brian Gaeke715c90b2004-08-20 06:00:58 +00001665 const Type *ETy = PTy->getElementType();
Dan Gohmanebb5a972008-06-23 18:40:28 +00001666 unsigned NumElements = PTy->getNumElements();
Brian Gaeke715c90b2004-08-20 06:00:58 +00001667
1668 // Verify that we have the correct size...
Dan Gohman46e803b2008-06-24 01:17:52 +00001669 if (NumElements != unsigned(-1) && NumElements != (unsigned)$3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001670 GEN_ERROR("Type mismatch: constant sized packed initialized with " +
Eric Christopher2a5196f2008-09-24 04:55:49 +00001671 utostr($3->size()) + " arguments, but has size of " +
Dan Gohman46e803b2008-06-24 01:17:52 +00001672 utostr(NumElements) + "");
Brian Gaeke715c90b2004-08-20 06:00:58 +00001673
1674 // Verify all elements are correct type!
1675 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencer08de34b2006-12-03 05:45:44 +00001676 if (ETy != (*$3)[i]->getType())
Eric Christopher2a5196f2008-09-24 04:55:49 +00001677 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Brian Gaeke715c90b2004-08-20 06:00:58 +00001678 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencer08de34b2006-12-03 05:45:44 +00001679 (*$3)[i]->getType()->getDescription() + "'.");
Brian Gaeke715c90b2004-08-20 06:00:58 +00001680 }
1681
Reid Spencer9d6565a2007-02-15 02:26:10 +00001682 $$ = ConstantVector::get(PTy, *$3);
Reid Spencer08de34b2006-12-03 05:45:44 +00001683 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001684 CHECK_FOR_ERROR
Brian Gaeke715c90b2004-08-20 06:00:58 +00001685 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001686 | Types '{' ConstVector '}' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001687 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001688 if (STy == 0)
Eric Christopher2a5196f2008-09-24 04:55:49 +00001689 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001690 (*$1)->getDescription() + "'");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001691
Chris Lattnerc6212f12003-05-21 16:06:56 +00001692 if ($3->size() != STy->getNumContainedTypes())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001693 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattnerc6212f12003-05-21 16:06:56 +00001694
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001695 // Check to ensure that constants are compatible with the type initializer!
1696 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Reid Spencer08de34b2006-12-03 05:45:44 +00001697 if ((*$3)[i]->getType() != STy->getElementType(i))
Reid Spencer61c83e02006-08-18 08:43:06 +00001698 GEN_ERROR("Expected type '" +
Chris Lattnerd21cd802004-02-09 04:37:31 +00001699 STy->getElementType(i)->getDescription() +
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001700 "' for element #" + utostr(i) +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001701 " of structure initializer");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001702
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001703 // Check to ensure that Type is not packed
1704 if (STy->isPacked())
Chris Lattner4989b842007-04-26 05:30:35 +00001705 GEN_ERROR("Unpacked Initializer to vector type '" +
1706 STy->getDescription() + "'");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001707
Reid Spencer08de34b2006-12-03 05:45:44 +00001708 $$ = ConstantStruct::get(STy, *$3);
1709 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001710 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001711 }
Chris Lattnerc6212f12003-05-21 16:06:56 +00001712 | Types '{' '}' {
Reid Spencere1553cc2006-12-31 05:40:12 +00001713 if (!UpRefs.empty())
1714 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001715 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerc6212f12003-05-21 16:06:56 +00001716 if (STy == 0)
Eric Christopher2a5196f2008-09-24 04:55:49 +00001717 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001718 (*$1)->getDescription() + "'");
Chris Lattnerc6212f12003-05-21 16:06:56 +00001719
1720 if (STy->getNumContainedTypes() != 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001721 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattnerc6212f12003-05-21 16:06:56 +00001722
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001723 // Check to ensure that Type is not packed
1724 if (STy->isPacked())
Chris Lattner4989b842007-04-26 05:30:35 +00001725 GEN_ERROR("Unpacked Initializer to vector type '" +
1726 STy->getDescription() + "'");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001727
1728 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1729 delete $1;
1730 CHECK_FOR_ERROR
1731 }
1732 | Types '<' '{' ConstVector '}' '>' {
1733 const StructType *STy = dyn_cast<StructType>($1->get());
1734 if (STy == 0)
Eric Christopher2a5196f2008-09-24 04:55:49 +00001735 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001736 (*$1)->getDescription() + "'");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001737
1738 if ($4->size() != STy->getNumContainedTypes())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001739 GEN_ERROR("Illegal number of initializers for structure type");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001740
1741 // Check to ensure that constants are compatible with the type initializer!
1742 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1743 if ((*$4)[i]->getType() != STy->getElementType(i))
1744 GEN_ERROR("Expected type '" +
1745 STy->getElementType(i)->getDescription() +
1746 "' for element #" + utostr(i) +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001747 " of structure initializer");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001748
1749 // Check to ensure that Type is packed
1750 if (!STy->isPacked())
Eric Christopher2a5196f2008-09-24 04:55:49 +00001751 GEN_ERROR("Vector initializer to non-vector type '" +
Reid Spencerac9dcb92007-02-15 03:39:18 +00001752 STy->getDescription() + "'");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001753
1754 $$ = ConstantStruct::get(STy, *$4);
1755 delete $1; delete $4;
1756 CHECK_FOR_ERROR
1757 }
1758 | Types '<' '{' '}' '>' {
1759 if (!UpRefs.empty())
1760 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1761 const StructType *STy = dyn_cast<StructType>($1->get());
1762 if (STy == 0)
Eric Christopher2a5196f2008-09-24 04:55:49 +00001763 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001764 (*$1)->getDescription() + "'");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001765
1766 if (STy->getNumContainedTypes() != 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001767 GEN_ERROR("Illegal number of initializers for structure type");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001768
1769 // Check to ensure that Type is packed
1770 if (!STy->isPacked())
Eric Christopher2a5196f2008-09-24 04:55:49 +00001771 GEN_ERROR("Vector initializer to non-vector type '" +
Reid Spencerac9dcb92007-02-15 03:39:18 +00001772 STy->getDescription() + "'");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001773
Reid Spencer08de34b2006-12-03 05:45:44 +00001774 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1775 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001776 CHECK_FOR_ERROR
Chris Lattnerc6212f12003-05-21 16:06:56 +00001777 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001778 | Types NULL_TOK {
Reid Spencere1553cc2006-12-31 05:40:12 +00001779 if (!UpRefs.empty())
1780 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001781 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001782 if (PTy == 0)
Eric Christopher2a5196f2008-09-24 04:55:49 +00001783 GEN_ERROR("Cannot make null pointer constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001784 (*$1)->getDescription() + "'");
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001785
Reid Spencer08de34b2006-12-03 05:45:44 +00001786 $$ = ConstantPointerNull::get(PTy);
1787 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001788 CHECK_FOR_ERROR
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001789 }
Chris Lattner16710e92004-10-16 18:17:13 +00001790 | Types UNDEF {
Reid Spencere1553cc2006-12-31 05:40:12 +00001791 if (!UpRefs.empty())
1792 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001793 $$ = UndefValue::get($1->get());
1794 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001795 CHECK_FOR_ERROR
Chris Lattner16710e92004-10-16 18:17:13 +00001796 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001797 | Types SymbolicValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00001798 if (!UpRefs.empty())
1799 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001800 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001801 if (Ty == 0)
Devang Pateld6ffcf92008-02-19 22:26:37 +00001802 GEN_ERROR("Global const reference must be a pointer type " + (*$1)->getDescription());
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001803
Chris Lattner3101c252002-08-15 17:58:33 +00001804 // ConstExprs can exist in the body of a function, thus creating
Reid Spencer3271ed52004-07-18 00:08:11 +00001805 // GlobalValues whenever they refer to a variable. Because we are in
Reid Spencer186a43f2007-03-19 18:39:36 +00001806 // the context of a function, getExistingVal will search the functions
Chris Lattner3101c252002-08-15 17:58:33 +00001807 // symbol table instead of the module symbol table for the global symbol,
1808 // which throws things all off. To get around this, we just tell
Reid Spencer186a43f2007-03-19 18:39:36 +00001809 // getExistingVal that we are at global scope here.
Chris Lattner3101c252002-08-15 17:58:33 +00001810 //
Chris Lattner394f2eb2003-10-16 20:12:13 +00001811 Function *SavedCurFn = CurFun.CurrentFunction;
1812 CurFun.CurrentFunction = 0;
Chris Lattner3101c252002-08-15 17:58:33 +00001813
Reid Spencer186a43f2007-03-19 18:39:36 +00001814 Value *V = getExistingVal(Ty, $2);
Reid Spencer5b7e7532006-09-28 19:28:24 +00001815 CHECK_FOR_ERROR
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001816
Chris Lattner394f2eb2003-10-16 20:12:13 +00001817 CurFun.CurrentFunction = SavedCurFn;
Chris Lattner3101c252002-08-15 17:58:33 +00001818
Chris Lattner2079fde2001-10-13 06:41:08 +00001819 // If this is an initializer for a constant pointer, which is referencing a
1820 // (currently) undefined variable, create a stub now that shall be replaced
1821 // in the future with the right type of variable.
1822 //
1823 if (V == 0) {
Reid Spencer1755a9a2007-02-05 17:01:20 +00001824 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001825 const PointerType *PT = cast<PointerType>(Ty);
1826
1827 // First check to see if the forward references value is already created!
1828 PerModuleInfo::GlobalRefsType::iterator I =
Reid Spencer3271ed52004-07-18 00:08:11 +00001829 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Eric Christopher2a5196f2008-09-24 04:55:49 +00001830
Chris Lattner2079fde2001-10-13 06:41:08 +00001831 if (I != CurModule.GlobalRefs.end()) {
Reid Spencer3271ed52004-07-18 00:08:11 +00001832 V = I->second; // Placeholder already exists, use it...
Chris Lattnera989b232003-12-23 20:05:15 +00001833 $2.destroy();
Chris Lattner2079fde2001-10-13 06:41:08 +00001834 } else {
Chris Lattner8a327842004-07-14 21:44:00 +00001835 std::string Name;
Reid Spencerb2d17862007-01-26 08:04:51 +00001836 if ($2.Type == ValID::GlobalName)
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001837 Name = $2.getName();
Reid Spencerb2d17862007-01-26 08:04:51 +00001838 else if ($2.Type != ValID::GlobalID)
1839 GEN_ERROR("Invalid reference to global");
Chris Lattner8a327842004-07-14 21:44:00 +00001840
Reid Spencer3271ed52004-07-18 00:08:11 +00001841 // Create the forward referenced global.
Chris Lattnera09000d2004-07-14 23:03:46 +00001842 GlobalValue *GV;
Eric Christopher2a5196f2008-09-24 04:55:49 +00001843 if (const FunctionType *FTy =
Chris Lattnera09000d2004-07-14 23:03:46 +00001844 dyn_cast<FunctionType>(PT->getElementType())) {
Gabor Greif051a9502008-04-06 20:25:17 +00001845 GV = Function::Create(FTy, GlobalValue::ExternalWeakLinkage, Name,
1846 CurModule.CurrentModule);
Chris Lattnera09000d2004-07-14 23:03:46 +00001847 } else {
Reid Spencer3271ed52004-07-18 00:08:11 +00001848 GV = new GlobalVariable(PT->getElementType(), false,
Chris Lattner4989b842007-04-26 05:30:35 +00001849 GlobalValue::ExternalWeakLinkage, 0,
Chris Lattnera09000d2004-07-14 23:03:46 +00001850 Name, CurModule.CurrentModule);
1851 }
Chris Lattner8a327842004-07-14 21:44:00 +00001852
Reid Spencer3271ed52004-07-18 00:08:11 +00001853 // Keep track of the fact that we have a forward ref to recycle it
1854 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1855 V = GV;
Chris Lattner2079fde2001-10-13 06:41:08 +00001856 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001857 }
1858
Reid Spencer08de34b2006-12-03 05:45:44 +00001859 $$ = cast<GlobalValue>(V);
1860 delete $1; // Free the type handle
Reid Spencer61c83e02006-08-18 08:43:06 +00001861 CHECK_FOR_ERROR
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001862 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001863 | Types ConstExpr {
Reid Spencere1553cc2006-12-31 05:40:12 +00001864 if (!UpRefs.empty())
1865 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001866 if ($1->get() != $2->getType())
Eric Christopher2a5196f2008-09-24 04:55:49 +00001867 GEN_ERROR("Mismatched types for constant expression: " +
Reid Spencerb4fdfdb2007-01-04 00:05:48 +00001868 (*$1)->getDescription() + " and " + $2->getType()->getDescription());
Chris Lattnerd78700d2002-08-16 21:14:40 +00001869 $$ = $2;
Reid Spencer08de34b2006-12-03 05:45:44 +00001870 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001871 CHECK_FOR_ERROR
Chris Lattnerf4600482003-06-28 20:01:34 +00001872 }
1873 | Types ZEROINITIALIZER {
Reid Spencere1553cc2006-12-31 05:40:12 +00001874 if (!UpRefs.empty())
1875 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001876 const Type *Ty = $1->get();
Chris Lattner78915932004-11-28 16:45:45 +00001877 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001878 GEN_ERROR("Cannot create a null initialized value of this type");
Reid Spencer08de34b2006-12-03 05:45:44 +00001879 $$ = Constant::getNullValue(Ty);
1880 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001881 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00001882 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001883 | IntType ESINT64VAL { // integral constants
Reid Spencer9ffad0a2006-12-20 17:20:09 +00001884 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001885 GEN_ERROR("Constant value doesn't fit in type");
Reid Spencereac65742007-03-19 20:40:22 +00001886 $$ = ConstantInt::get($1, $2, true);
Reid Spencerf2310042007-02-28 02:23:44 +00001887 CHECK_FOR_ERROR
1888 }
1889 | IntType ESAPINTVAL { // arbitrary precision integer constants
1890 uint32_t BitWidth = cast<IntegerType>($1)->getBitWidth();
1891 if ($2->getBitWidth() > BitWidth) {
1892 GEN_ERROR("Constant value does not fit in type");
Reid Spenceraf1aacd2007-03-01 19:32:01 +00001893 }
1894 $2->sextOrTrunc(BitWidth);
1895 $$ = ConstantInt::get(*$2);
Reid Spencerf2310042007-02-28 02:23:44 +00001896 delete $2;
Reid Spencer9ffad0a2006-12-20 17:20:09 +00001897 CHECK_FOR_ERROR
1898 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001899 | IntType EUINT64VAL { // integral constants
Reid Spencer9ffad0a2006-12-20 17:20:09 +00001900 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001901 GEN_ERROR("Constant value doesn't fit in type");
Reid Spencereac65742007-03-19 20:40:22 +00001902 $$ = ConstantInt::get($1, $2, false);
Reid Spencerf2310042007-02-28 02:23:44 +00001903 CHECK_FOR_ERROR
1904 }
1905 | IntType EUAPINTVAL { // arbitrary precision integer constants
1906 uint32_t BitWidth = cast<IntegerType>($1)->getBitWidth();
1907 if ($2->getBitWidth() > BitWidth) {
1908 GEN_ERROR("Constant value does not fit in type");
Eric Christopher2a5196f2008-09-24 04:55:49 +00001909 }
Reid Spenceraf1aacd2007-03-01 19:32:01 +00001910 $2->zextOrTrunc(BitWidth);
1911 $$ = ConstantInt::get(*$2);
Reid Spencerf2310042007-02-28 02:23:44 +00001912 delete $2;
Reid Spencer9ffad0a2006-12-20 17:20:09 +00001913 CHECK_FOR_ERROR
1914 }
Reid Spencer8088e9d2007-01-13 05:00:20 +00001915 | INTTYPE TRUETOK { // Boolean constants
Dan Gohman9c531cd2008-05-23 18:23:11 +00001916 if (cast<IntegerType>($1)->getBitWidth() != 1)
1917 GEN_ERROR("Constant true must have type i1");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001918 $$ = ConstantInt::getTrue();
Reid Spencer61c83e02006-08-18 08:43:06 +00001919 CHECK_FOR_ERROR
Chris Lattnerd05e3592002-08-15 18:17:28 +00001920 }
Reid Spencer8088e9d2007-01-13 05:00:20 +00001921 | INTTYPE FALSETOK { // Boolean constants
Dan Gohman9c531cd2008-05-23 18:23:11 +00001922 if (cast<IntegerType>($1)->getBitWidth() != 1)
1923 GEN_ERROR("Constant false must have type i1");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001924 $$ = ConstantInt::getFalse();
Reid Spencer61c83e02006-08-18 08:43:06 +00001925 CHECK_FOR_ERROR
Chris Lattnerd05e3592002-08-15 18:17:28 +00001926 }
Dale Johannesen9d5f4562007-09-12 03:30:33 +00001927 | FPType FPVAL { // Floating point constants
Dale Johannesen43421b32007-09-06 18:13:44 +00001928 if (!ConstantFP::isValueValidForType($1, *$2))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001929 GEN_ERROR("Floating point constant invalid for type");
Eric Christopher2a5196f2008-09-24 04:55:49 +00001930 // Lexer has no type info, so builds all float and double FP constants
Dale Johannesen3f6eb742007-09-11 18:32:33 +00001931 // as double. Fix this here. Long double is done right.
1932 if (&$2->getSemantics()==&APFloat::IEEEdouble && $1==Type::FloatTy)
Dale Johannesen43421b32007-09-06 18:13:44 +00001933 $2->convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven);
Chris Lattner02a260a2008-04-20 00:41:09 +00001934 $$ = ConstantFP::get(*$2);
Dale Johannesencdd509a2007-09-07 21:07:57 +00001935 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001936 CHECK_FOR_ERROR
Chris Lattnerd05e3592002-08-15 18:17:28 +00001937 };
1938
Chris Lattner00950542001-06-06 20:29:01 +00001939
Reid Spencer3da59db2006-11-27 01:05:10 +00001940ConstExpr: CastOps '(' ConstVal TO Types ')' {
Reid Spencere1553cc2006-12-31 05:40:12 +00001941 if (!UpRefs.empty())
1942 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001943 Constant *Val = $3;
Reid Spencer93947c32007-01-17 02:47:33 +00001944 const Type *DestTy = $5->get();
1945 if (!CastInst::castIsValid($1, $3, DestTy))
1946 GEN_ERROR("invalid cast opcode for cast from '" +
1947 Val->getType()->getDescription() + "' to '" +
Eric Christopher2a5196f2008-09-24 04:55:49 +00001948 DestTy->getDescription() + "'");
Reid Spencer93947c32007-01-17 02:47:33 +00001949 $$ = ConstantExpr::getCast($1, $3, DestTy);
Reid Spencer08de34b2006-12-03 05:45:44 +00001950 delete $5;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001951 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001952 | GETELEMENTPTR '(' ConstVal IndexList ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001953 if (!isa<PointerType>($3->getType()))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001954 GEN_ERROR("GetElementPtr requires a pointer operand");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001955
Reid Spencer08de34b2006-12-03 05:45:44 +00001956 const Type *IdxTy =
Dan Gohman041e2eb2008-05-15 19:50:34 +00001957 GetElementPtrInst::getIndexedType($3->getType(), $4->begin(), $4->end());
Reid Spencer08de34b2006-12-03 05:45:44 +00001958 if (!IdxTy)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001959 GEN_ERROR("Index list invalid for constant getelementptr");
Reid Spencer08de34b2006-12-03 05:45:44 +00001960
Chris Lattnere0135402007-01-31 04:43:46 +00001961 SmallVector<Constant*, 8> IdxVec;
Reid Spencer08de34b2006-12-03 05:45:44 +00001962 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1963 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001964 IdxVec.push_back(C);
1965 else
Reid Spencerf4fa5902007-02-05 10:16:10 +00001966 GEN_ERROR("Indices to constant getelementptr must be constants");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001967
Chris Lattnerd78700d2002-08-16 21:14:40 +00001968 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001969
Chris Lattnere0135402007-01-31 04:43:46 +00001970 $$ = ConstantExpr::getGetElementPtr($3, &IdxVec[0], IdxVec.size());
Reid Spencer61c83e02006-08-18 08:43:06 +00001971 CHECK_FOR_ERROR
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001972 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001973 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer4fe16d62007-01-11 18:21:29 +00001974 if ($3->getType() != Type::Int1Ty)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001975 GEN_ERROR("Select condition must be of boolean type");
Reid Spencer08de34b2006-12-03 05:45:44 +00001976 if ($5->getType() != $7->getType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001977 GEN_ERROR("Select operand types must match");
Reid Spencer08de34b2006-12-03 05:45:44 +00001978 $$ = ConstantExpr::getSelect($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001979 CHECK_FOR_ERROR
Chris Lattner76f0ff32004-03-12 05:51:36 +00001980 }
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001981 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001982 if ($3->getType() != $5->getType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001983 GEN_ERROR("Binary operator types must match");
Reid Spencer1628cec2006-10-26 06:15:43 +00001984 CHECK_FOR_ERROR;
Reid Spencerc6e956e2006-12-05 19:15:41 +00001985 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001986 }
1987 | LogicalOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001988 if ($3->getType() != $5->getType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001989 GEN_ERROR("Logical operator types must match");
Chris Lattner42a75512007-01-15 02:27:26 +00001990 if (!$3->getType()->isInteger()) {
Eric Christopher2a5196f2008-09-24 04:55:49 +00001991 if (!isa<VectorType>($3->getType()) ||
Reid Spencer9d6565a2007-02-15 02:26:10 +00001992 !cast<VectorType>($3->getType())->getElementType()->isInteger())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001993 GEN_ERROR("Logical operator requires integral operands");
Chris Lattner0a017832005-12-21 18:31:29 +00001994 }
Reid Spencer08de34b2006-12-03 05:45:44 +00001995 $$ = ConstantExpr::get($1, $3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001996 CHECK_FOR_ERROR
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001997 }
Reid Spencer763ed5e2006-12-04 05:20:06 +00001998 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
1999 if ($4->getType() != $6->getType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00002000 GEN_ERROR("icmp operand types must match");
Reid Spencer763ed5e2006-12-04 05:20:06 +00002001 $$ = ConstantExpr::getICmp($2, $4, $6);
Reid Spencer08de34b2006-12-03 05:45:44 +00002002 }
Reid Spencer763ed5e2006-12-04 05:20:06 +00002003 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
2004 if ($4->getType() != $6->getType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00002005 GEN_ERROR("fcmp operand types must match");
Reid Spencer763ed5e2006-12-04 05:20:06 +00002006 $$ = ConstantExpr::getFCmp($2, $4, $6);
Reid Spencer08de34b2006-12-03 05:45:44 +00002007 }
Nate Begemanac80ade2008-05-12 19:01:56 +00002008 | VICMP IPredicates '(' ConstVal ',' ConstVal ')' {
2009 if ($4->getType() != $6->getType())
2010 GEN_ERROR("vicmp operand types must match");
2011 $$ = ConstantExpr::getVICmp($2, $4, $6);
2012 }
2013 | VFCMP FPredicates '(' ConstVal ',' ConstVal ')' {
2014 if ($4->getType() != $6->getType())
2015 GEN_ERROR("vfcmp operand types must match");
2016 $$ = ConstantExpr::getVFCmp($2, $4, $6);
2017 }
Robert Bocchino9c62b562006-01-10 19:04:32 +00002018 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00002019 if (!ExtractElementInst::isValidOperands($3, $5))
Reid Spencerf4fa5902007-02-05 10:16:10 +00002020 GEN_ERROR("Invalid extractelement operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00002021 $$ = ConstantExpr::getExtractElement($3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002022 CHECK_FOR_ERROR
Chris Lattnerca73cd82006-04-08 03:53:34 +00002023 }
2024 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00002025 if (!InsertElementInst::isValidOperands($3, $5, $7))
Reid Spencerf4fa5902007-02-05 10:16:10 +00002026 GEN_ERROR("Invalid insertelement operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00002027 $$ = ConstantExpr::getInsertElement($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00002028 CHECK_FOR_ERROR
Chris Lattnerca73cd82006-04-08 03:53:34 +00002029 }
2030 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00002031 if (!ShuffleVectorInst::isValidOperands($3, $5, $7))
Reid Spencerf4fa5902007-02-05 10:16:10 +00002032 GEN_ERROR("Invalid shufflevector operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00002033 $$ = ConstantExpr::getShuffleVector($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00002034 CHECK_FOR_ERROR
Dan Gohmane4977cf2008-05-23 01:55:30 +00002035 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002036 | EXTRACTVALUE '(' ConstVal ConstantIndexList ')' {
Dan Gohmane4977cf2008-05-23 01:55:30 +00002037 if (!isa<StructType>($3->getType()) && !isa<ArrayType>($3->getType()))
2038 GEN_ERROR("ExtractValue requires an aggregate operand");
2039
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002040 $$ = ConstantExpr::getExtractValue($3, &(*$4)[0], $4->size());
Dan Gohmane4977cf2008-05-23 01:55:30 +00002041 delete $4;
Dan Gohmane4977cf2008-05-23 01:55:30 +00002042 CHECK_FOR_ERROR
2043 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002044 | INSERTVALUE '(' ConstVal ',' ConstVal ConstantIndexList ')' {
Dan Gohmane4977cf2008-05-23 01:55:30 +00002045 if (!isa<StructType>($3->getType()) && !isa<ArrayType>($3->getType()))
2046 GEN_ERROR("InsertValue requires an aggregate operand");
2047
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002048 $$ = ConstantExpr::getInsertValue($3, $5, &(*$6)[0], $6->size());
Dan Gohmane4977cf2008-05-23 01:55:30 +00002049 delete $6;
Dan Gohmane4977cf2008-05-23 01:55:30 +00002050 CHECK_FOR_ERROR
Chris Lattner699f1eb2002-08-14 17:12:33 +00002051 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002052
Chris Lattnerca73cd82006-04-08 03:53:34 +00002053
Misha Brukmanbc0e9982003-07-14 17:20:40 +00002054// ConstVector - A list of comma separated constants.
Chris Lattner00950542001-06-06 20:29:01 +00002055ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00002056 ($$ = $1)->push_back($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002057 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002058 }
2059 | ConstVal {
Reid Spencer08de34b2006-12-03 05:45:44 +00002060 $$ = new std::vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00002061 $$->push_back($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002062 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002063 };
Chris Lattner00950542001-06-06 20:29:01 +00002064
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002065
Chris Lattner1781aca2001-09-18 04:00:54 +00002066// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00002067GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00002068
Eric Christopher2a5196f2008-09-24 04:55:49 +00002069// ThreadLocal
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002070ThreadLocal : THREAD_LOCAL { $$ = true; } | { $$ = false; };
2071
Anton Korobeynikova80e1182007-04-28 13:45:00 +00002072// AliaseeRef - Match either GlobalValue or bitcast to GlobalValue.
2073AliaseeRef : ResultTypes SymbolicValueRef {
2074 const Type* VTy = $1->get();
2075 Value *V = getVal(VTy, $2);
Chris Lattner6df20ef2007-08-06 21:00:37 +00002076 CHECK_FOR_ERROR
Anton Korobeynikova80e1182007-04-28 13:45:00 +00002077 GlobalValue* Aliasee = dyn_cast<GlobalValue>(V);
2078 if (!Aliasee)
2079 GEN_ERROR("Aliases can be created only to global values");
2080
2081 $$ = Aliasee;
2082 CHECK_FOR_ERROR
2083 delete $1;
2084 }
2085 | BITCAST '(' AliaseeRef TO Types ')' {
2086 Constant *Val = $3;
2087 const Type *DestTy = $5->get();
2088 if (!CastInst::castIsValid($1, $3, DestTy))
2089 GEN_ERROR("invalid cast opcode for cast from '" +
2090 Val->getType()->getDescription() + "' to '" +
2091 DestTy->getDescription() + "'");
Eric Christopher2a5196f2008-09-24 04:55:49 +00002092
Anton Korobeynikova80e1182007-04-28 13:45:00 +00002093 $$ = ConstantExpr::getCast($1, $3, DestTy);
2094 CHECK_FOR_ERROR
2095 delete $5;
2096 };
Chris Lattner00950542001-06-06 20:29:01 +00002097
Chris Lattner0e73ce62002-05-02 19:11:13 +00002098//===----------------------------------------------------------------------===//
2099// Rules to match Modules
2100//===----------------------------------------------------------------------===//
2101
2102// Module rule: Capture the result of parsing the whole file into a result
2103// variable...
2104//
Eric Christopher2a5196f2008-09-24 04:55:49 +00002105Module
Reid Spencerb951bc02006-12-29 20:29:48 +00002106 : DefinitionList {
2107 $$ = ParserResult = CurModule.CurrentModule;
2108 CurModule.ModuleDone();
2109 CHECK_FOR_ERROR;
2110 }
2111 | /*empty*/ {
2112 $$ = ParserResult = CurModule.CurrentModule;
2113 CurModule.ModuleDone();
2114 CHECK_FOR_ERROR;
2115 }
2116 ;
Chris Lattner0e73ce62002-05-02 19:11:13 +00002117
Reid Spencerb951bc02006-12-29 20:29:48 +00002118DefinitionList
2119 : Definition
2120 | DefinitionList Definition
2121 ;
2122
Eric Christopher2a5196f2008-09-24 04:55:49 +00002123Definition
Jeff Cohen361c3ef2007-01-21 19:19:31 +00002124 : DEFINE { CurFun.isDeclare = false; } Function {
Chris Lattner394f2eb2003-10-16 20:12:13 +00002125 CurFun.FunctionDone();
Reid Spencer61c83e02006-08-18 08:43:06 +00002126 CHECK_FOR_ERROR
Reid Spencerb951bc02006-12-29 20:29:48 +00002127 }
2128 | DECLARE { CurFun.isDeclare = true; } FunctionProto {
Reid Spencer61c83e02006-08-18 08:43:06 +00002129 CHECK_FOR_ERROR
Chris Lattner0e73ce62002-05-02 19:11:13 +00002130 }
Reid Spencerb951bc02006-12-29 20:29:48 +00002131 | MODULE ASM_TOK AsmBlock {
Reid Spencer61c83e02006-08-18 08:43:06 +00002132 CHECK_FOR_ERROR
Eric Christopher2a5196f2008-09-24 04:55:49 +00002133 }
Reid Spencerb2d17862007-01-26 08:04:51 +00002134 | OptLocalAssign TYPE Types {
Reid Spencere1553cc2006-12-31 05:40:12 +00002135 if (!UpRefs.empty())
2136 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattner4a42e902001-10-22 05:56:09 +00002137 // Eagerly resolve types. This is not an optimization, this is a
2138 // requirement that is due to the fact that we could have this:
2139 //
2140 // %list = type { %list * }
2141 // %list = type { %list * } ; repeated type decl
2142 //
2143 // If types are not resolved eagerly, then the two types will not be
2144 // determined to be the same type!
2145 //
Reid Spencerb951bc02006-12-29 20:29:48 +00002146 ResolveTypeTo($1, *$3);
Chris Lattner4a42e902001-10-22 05:56:09 +00002147
Reid Spencerb951bc02006-12-29 20:29:48 +00002148 if (!setTypeName(*$3, $1) && !$1) {
Reid Spencer5b7e7532006-09-28 19:28:24 +00002149 CHECK_FOR_ERROR
Chris Lattner8c89a0a2004-07-13 08:10:10 +00002150 // If this is a named type that is not a redefinition, add it to the slot
2151 // table.
Reid Spencerb951bc02006-12-29 20:29:48 +00002152 CurModule.Types.push_back(*$3);
Chris Lattner30c89792001-09-07 16:35:17 +00002153 }
Reid Spencer08de34b2006-12-03 05:45:44 +00002154
Reid Spencerb951bc02006-12-29 20:29:48 +00002155 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002156 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00002157 }
Reid Spencerb2d17862007-01-26 08:04:51 +00002158 | OptLocalAssign TYPE VOID {
Reid Spencere1553cc2006-12-31 05:40:12 +00002159 ResolveTypeTo($1, $3);
2160
2161 if (!setTypeName($3, $1) && !$1) {
2162 CHECK_FOR_ERROR
2163 // If this is a named type that is not a redefinition, add it to the slot
2164 // table.
2165 CurModule.Types.push_back($3);
2166 }
2167 CHECK_FOR_ERROR
2168 }
Eric Christopher2a5196f2008-09-24 04:55:49 +00002169 | OptGlobalAssign GVVisibilityStyle ThreadLocal GlobalType ConstVal
2170 OptAddrSpace {
Reid Spencerb2d17862007-01-26 08:04:51 +00002171 /* "Externally Visible" Linkage */
Eric Christopher2a5196f2008-09-24 04:55:49 +00002172 if ($5 == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002173 GEN_ERROR("Global value initializer is not a constant");
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002174 CurGV = ParseGlobalVariable($1, GlobalValue::ExternalLinkage,
Christopher Lambd49e18d2007-12-12 08:44:39 +00002175 $2, $4, $5->getType(), $5, $3, $6);
Christopher Lambfe63fb92007-12-11 08:59:05 +00002176 CHECK_FOR_ERROR
2177 } GlobalVarAttributes {
2178 CurGV = 0;
2179 }
Chris Lattner4989b842007-04-26 05:30:35 +00002180 | OptGlobalAssign GVInternalLinkage GVVisibilityStyle ThreadLocal GlobalType
Christopher Lambd49e18d2007-12-12 08:44:39 +00002181 ConstVal OptAddrSpace {
Eric Christopher2a5196f2008-09-24 04:55:49 +00002182 if ($6 == 0)
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002183 GEN_ERROR("Global value initializer is not a constant");
Christopher Lambd49e18d2007-12-12 08:44:39 +00002184 CurGV = ParseGlobalVariable($1, $2, $3, $5, $6->getType(), $6, $4, $7);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002185 CHECK_FOR_ERROR
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002186 } GlobalVarAttributes {
2187 CurGV = 0;
2188 }
Chris Lattner4989b842007-04-26 05:30:35 +00002189 | OptGlobalAssign GVExternalLinkage GVVisibilityStyle ThreadLocal GlobalType
Christopher Lambd49e18d2007-12-12 08:44:39 +00002190 Types OptAddrSpace {
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002191 if (!UpRefs.empty())
2192 GEN_ERROR("Invalid upreference in type: " + (*$6)->getDescription());
Christopher Lambd49e18d2007-12-12 08:44:39 +00002193 CurGV = ParseGlobalVariable($1, $2, $3, $5, *$6, 0, $4, $7);
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002194 CHECK_FOR_ERROR
2195 delete $6;
Reid Spencer5b7e7532006-09-28 19:28:24 +00002196 } GlobalVarAttributes {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002197 CurGV = 0;
2198 CHECK_FOR_ERROR
2199 }
Anton Korobeynikova80e1182007-04-28 13:45:00 +00002200 | OptGlobalAssign GVVisibilityStyle ALIAS AliasLinkage AliaseeRef {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002201 std::string Name;
2202 if ($1) {
2203 Name = *$1;
2204 delete $1;
2205 }
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00002206 if (Name.empty())
Anton Korobeynikova80e1182007-04-28 13:45:00 +00002207 GEN_ERROR("Alias name cannot be empty");
Eric Christopher2a5196f2008-09-24 04:55:49 +00002208
Anton Korobeynikova80e1182007-04-28 13:45:00 +00002209 Constant* Aliasee = $5;
2210 if (Aliasee == 0)
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002211 GEN_ERROR(std::string("Invalid aliasee for alias: ") + Name);
Anton Korobeynikova80e1182007-04-28 13:45:00 +00002212
2213 GlobalAlias* GA = new GlobalAlias(Aliasee->getType(), $4, Name, Aliasee,
2214 CurModule.CurrentModule);
2215 GA->setVisibility($2);
2216 InsertValue(GA, CurModule.Values);
Eric Christopher2a5196f2008-09-24 04:55:49 +00002217
2218
Chris Lattnere5434242007-09-10 23:23:53 +00002219 // If there was a forward reference of this alias, resolve it now.
Eric Christopher2a5196f2008-09-24 04:55:49 +00002220
Chris Lattnere5434242007-09-10 23:23:53 +00002221 ValID ID;
2222 if (!Name.empty())
2223 ID = ValID::createGlobalName(Name);
2224 else
2225 ID = ValID::createGlobalID(CurModule.Values.size()-1);
Eric Christopher2a5196f2008-09-24 04:55:49 +00002226
Chris Lattnere5434242007-09-10 23:23:53 +00002227 if (GlobalValue *FWGV =
2228 CurModule.GetForwardRefForGlobal(GA->getType(), ID)) {
2229 // Replace uses of the fwdref with the actual alias.
2230 FWGV->replaceAllUsesWith(GA);
2231 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(FWGV))
2232 GV->eraseFromParent();
2233 else
2234 cast<Function>(FWGV)->eraseFromParent();
2235 }
2236 ID.destroy();
Eric Christopher2a5196f2008-09-24 04:55:49 +00002237
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00002238 CHECK_FOR_ERROR
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00002239 }
Eric Christopher2a5196f2008-09-24 04:55:49 +00002240 | TARGET TargetDefinition {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002241 CHECK_FOR_ERROR
2242 }
Reid Spencerb951bc02006-12-29 20:29:48 +00002243 | DEPLIBS '=' LibrariesDefinition {
Reid Spencer61c83e02006-08-18 08:43:06 +00002244 CHECK_FOR_ERROR
Chris Lattnere98dda62001-07-14 06:10:16 +00002245 }
Reid Spencerb951bc02006-12-29 20:29:48 +00002246 ;
Chris Lattner00950542001-06-06 20:29:01 +00002247
2248
Chris Lattneree454772006-01-23 23:05:15 +00002249AsmBlock : STRINGCONSTANT {
Chris Lattner66316012006-01-24 04:14:29 +00002250 const std::string &AsmSoFar = CurModule.CurrentModule->getModuleInlineAsm();
Chris Lattneree454772006-01-23 23:05:15 +00002251 if (AsmSoFar.empty())
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002252 CurModule.CurrentModule->setModuleInlineAsm(*$1);
Chris Lattneree454772006-01-23 23:05:15 +00002253 else
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002254 CurModule.CurrentModule->setModuleInlineAsm(AsmSoFar+"\n"+*$1);
2255 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002256 CHECK_FOR_ERROR
Chris Lattneree454772006-01-23 23:05:15 +00002257};
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00002258
Reid Spencerb2d17862007-01-26 08:04:51 +00002259TargetDefinition : TRIPLE '=' STRINGCONSTANT {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002260 CurModule.CurrentModule->setTargetTriple(*$3);
2261 delete $3;
John Criswell2f6a8b12006-10-24 19:09:48 +00002262 }
Chris Lattner10b27112006-10-22 06:07:41 +00002263 | DATALAYOUT '=' STRINGCONSTANT {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002264 CurModule.CurrentModule->setDataLayout(*$3);
2265 delete $3;
Owen Andersoncf7ff2b2006-10-18 02:21:12 +00002266 };
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00002267
Reid Spencerfeaf10e2004-07-25 21:30:51 +00002268LibrariesDefinition : '[' LibList ']';
Reid Spencer0be13e72004-07-25 17:58:28 +00002269
2270LibList : LibList ',' STRINGCONSTANT {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002271 CurModule.CurrentModule->addLibrary(*$3);
2272 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002273 CHECK_FOR_ERROR
Reid Spencer0be13e72004-07-25 17:58:28 +00002274 }
2275 | STRINGCONSTANT {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002276 CurModule.CurrentModule->addLibrary(*$1);
2277 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002278 CHECK_FOR_ERROR
Reid Spencer0be13e72004-07-25 17:58:28 +00002279 }
2280 | /* empty: end of list */ {
Reid Spencer61c83e02006-08-18 08:43:06 +00002281 CHECK_FOR_ERROR
Reid Spencer0be13e72004-07-25 17:58:28 +00002282 }
2283 ;
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00002284
Chris Lattner00950542001-06-06 20:29:01 +00002285//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00002286// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00002287//===----------------------------------------------------------------------===//
2288
Devang Patel05988662008-09-25 21:00:45 +00002289ArgListH : ArgListH ',' Types OptAttributes OptLocalName {
Reid Spencere1553cc2006-12-31 05:40:12 +00002290 if (!UpRefs.empty())
2291 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Dan Gohman9c531cd2008-05-23 18:23:11 +00002292 if (!(*$3)->isFirstClassType())
2293 GEN_ERROR("Argument types must be first-class");
Reid Spencere1553cc2006-12-31 05:40:12 +00002294 ArgListEntry E; E.Attrs = $4; E.Ty = $3; E.Name = $5;
Chris Lattner69da5cf2002-10-13 20:57:00 +00002295 $$ = $1;
Reid Spencere1553cc2006-12-31 05:40:12 +00002296 $1->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002297 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002298 }
Devang Patel05988662008-09-25 21:00:45 +00002299 | Types OptAttributes OptLocalName {
Reid Spencere1553cc2006-12-31 05:40:12 +00002300 if (!UpRefs.empty())
2301 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Dan Gohman9c531cd2008-05-23 18:23:11 +00002302 if (!(*$1)->isFirstClassType())
2303 GEN_ERROR("Argument types must be first-class");
Reid Spencere1553cc2006-12-31 05:40:12 +00002304 ArgListEntry E; E.Attrs = $2; E.Ty = $1; E.Name = $3;
2305 $$ = new ArgListType;
2306 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002307 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002308 };
Chris Lattner00950542001-06-06 20:29:01 +00002309
2310ArgList : ArgListH {
2311 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002312 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002313 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00002314 | ArgListH ',' DOTDOTDOT {
2315 $$ = $1;
Reid Spencere1553cc2006-12-31 05:40:12 +00002316 struct ArgListEntry E;
2317 E.Ty = new PATypeHolder(Type::VoidTy);
2318 E.Name = 0;
Devang Patel05988662008-09-25 21:00:45 +00002319 E.Attrs = Attribute::None;
Reid Spencere1553cc2006-12-31 05:40:12 +00002320 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002321 CHECK_FOR_ERROR
Chris Lattner69da5cf2002-10-13 20:57:00 +00002322 }
2323 | DOTDOTDOT {
Reid Spencere1553cc2006-12-31 05:40:12 +00002324 $$ = new ArgListType;
2325 struct ArgListEntry E;
2326 E.Ty = new PATypeHolder(Type::VoidTy);
2327 E.Name = 0;
Devang Patel05988662008-09-25 21:00:45 +00002328 E.Attrs = Attribute::None;
Reid Spencere1553cc2006-12-31 05:40:12 +00002329 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002330 CHECK_FOR_ERROR
Chris Lattner69da5cf2002-10-13 20:57:00 +00002331 }
Chris Lattner00950542001-06-06 20:29:01 +00002332 | /* empty */ {
2333 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00002334 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002335 };
Chris Lattner00950542001-06-06 20:29:01 +00002336
Devang Patel652203f2008-09-29 20:49:50 +00002337FunctionHeaderH : OptCallingConv OptRetAttrs ResultTypes GlobalName '(' ArgList ')'
Devang Patel2c9c3e72008-09-26 23:51:19 +00002338 OptFuncAttrs OptSection OptAlign OptGC {
Devang Patel652203f2008-09-29 20:49:50 +00002339 std::string FunctionName(*$4);
2340 delete $4; // Free strdup'd memory!
Eric Christopher2a5196f2008-09-24 04:55:49 +00002341
Reid Spencer98b3c5c2007-01-02 21:53:43 +00002342 // Check the function result for abstractness if this is a define. We should
2343 // have no abstract types at this point
Devang Patel652203f2008-09-29 20:49:50 +00002344 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved($3))
2345 GEN_ERROR("Reference to abstract result: "+ $3->get()->getDescription());
Reid Spencer98b3c5c2007-01-02 21:53:43 +00002346
Devang Patel652203f2008-09-29 20:49:50 +00002347 if (!FunctionType::isValidReturnType(*$3))
Chris Lattnerf7dedf22008-04-23 05:36:58 +00002348 GEN_ERROR("Invalid result type for LLVM function");
Eric Christopher2a5196f2008-09-24 04:55:49 +00002349
Chris Lattner9232b992003-04-22 18:42:41 +00002350 std::vector<const Type*> ParamTypeList;
Devang Patel05988662008-09-25 21:00:45 +00002351 SmallVector<AttributeWithIndex, 8> Attrs;
Devang Patel19c87462008-09-26 22:53:05 +00002352 //FIXME : In 3.0, stop accepting zext, sext and inreg as optional function
2353 //attributes.
Devang Patel652203f2008-09-29 20:49:50 +00002354 Attributes RetAttrs = $2;
2355 if ($8 != Attribute::None) {
2356 if ($8 & Attribute::ZExt) {
Devang Patel19c87462008-09-26 22:53:05 +00002357 RetAttrs = RetAttrs | Attribute::ZExt;
Devang Patel652203f2008-09-29 20:49:50 +00002358 $8 = $8 ^ Attribute::ZExt;
Devang Patel19c87462008-09-26 22:53:05 +00002359 }
Devang Patel652203f2008-09-29 20:49:50 +00002360 if ($8 & Attribute::SExt) {
Devang Patel19c87462008-09-26 22:53:05 +00002361 RetAttrs = RetAttrs | Attribute::SExt;
Devang Patel652203f2008-09-29 20:49:50 +00002362 $8 = $8 ^ Attribute::SExt;
Devang Patel19c87462008-09-26 22:53:05 +00002363 }
Devang Patel652203f2008-09-29 20:49:50 +00002364 if ($8 & Attribute::InReg) {
Devang Patel19c87462008-09-26 22:53:05 +00002365 RetAttrs = RetAttrs | Attribute::InReg;
Devang Patel652203f2008-09-29 20:49:50 +00002366 $8 = $8 ^ Attribute::InReg;
Devang Patel19c87462008-09-26 22:53:05 +00002367 }
Devang Patel19c87462008-09-26 22:53:05 +00002368 }
Devang Patel652203f2008-09-29 20:49:50 +00002369 if (RetAttrs != Attribute::None)
2370 Attrs.push_back(AttributeWithIndex::get(0, RetAttrs));
2371 if ($6) { // If there are arguments...
Reid Spencer5694b6e2007-04-09 06:17:21 +00002372 unsigned index = 1;
Devang Patel652203f2008-09-29 20:49:50 +00002373 for (ArgListType::iterator I = $6->begin(); I != $6->end(); ++I, ++index) {
Reid Spencere1553cc2006-12-31 05:40:12 +00002374 const Type* Ty = I->Ty->get();
Reid Spencer98b3c5c2007-01-02 21:53:43 +00002375 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved(I->Ty))
2376 GEN_ERROR("Reference to abstract argument: " + Ty->getDescription());
Reid Spencere1553cc2006-12-31 05:40:12 +00002377 ParamTypeList.push_back(Ty);
Devang Patel05988662008-09-25 21:00:45 +00002378 if (Ty != Type::VoidTy && I->Attrs != Attribute::None)
2379 Attrs.push_back(AttributeWithIndex::get(index, I->Attrs));
Reid Spencere1553cc2006-12-31 05:40:12 +00002380 }
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002381 }
Devang Patel652203f2008-09-29 20:49:50 +00002382 if ($8 != Attribute::None)
2383 Attrs.push_back(AttributeWithIndex::get(~0, $8));
Chris Lattner00950542001-06-06 20:29:01 +00002384
Chris Lattner2079fde2001-10-13 06:41:08 +00002385 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
2386 if (isVarArg) ParamTypeList.pop_back();
2387
Devang Patel05988662008-09-25 21:00:45 +00002388 AttrListPtr PAL;
Reid Spencer4f859aa2007-04-22 05:46:44 +00002389 if (!Attrs.empty())
Devang Patel05988662008-09-25 21:00:45 +00002390 PAL = AttrListPtr::get(Attrs.begin(), Attrs.end());
Reid Spencer5694b6e2007-04-09 06:17:21 +00002391
Devang Patel652203f2008-09-29 20:49:50 +00002392 FunctionType *FT = FunctionType::get(*$3, ParamTypeList, isVarArg);
Christopher Lamb43ad6b32007-12-17 01:12:55 +00002393 const PointerType *PFT = PointerType::getUnqual(FT);
Devang Patel652203f2008-09-29 20:49:50 +00002394 delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00002395
Chris Lattnera09000d2004-07-14 23:03:46 +00002396 ValID ID;
2397 if (!FunctionName.empty()) {
Reid Spencerb2d17862007-01-26 08:04:51 +00002398 ID = ValID::createGlobalName((char*)FunctionName.c_str());
Chris Lattnera09000d2004-07-14 23:03:46 +00002399 } else {
Reid Spencer186a43f2007-03-19 18:39:36 +00002400 ID = ValID::createGlobalID(CurModule.Values.size());
Chris Lattnera09000d2004-07-14 23:03:46 +00002401 }
2402
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00002403 Function *Fn = 0;
Chris Lattnera09000d2004-07-14 23:03:46 +00002404 // See if this function was forward referenced. If so, recycle the object.
Reid Spencer863dd882007-04-28 16:06:50 +00002405 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
Eric Christopher2a5196f2008-09-24 04:55:49 +00002406 // Move the function to the end of the list, from whereever it was
Chris Lattnera09000d2004-07-14 23:03:46 +00002407 // previously inserted.
2408 Fn = cast<Function>(FWRef);
Devang Patel05988662008-09-25 21:00:45 +00002409 assert(Fn->getAttributes().isEmpty() &&
Chris Lattner58d74912008-03-12 17:45:29 +00002410 "Forward reference has parameter attributes!");
Chris Lattnera09000d2004-07-14 23:03:46 +00002411 CurModule.CurrentModule->getFunctionList().remove(Fn);
2412 CurModule.CurrentModule->getFunctionList().push_back(Fn);
2413 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
Reid Spenceref9b9a72007-02-05 20:47:22 +00002414 (Fn = CurModule.CurrentModule->getFunction(FunctionName))) {
Duncan Sandsdc024672007-11-27 13:23:08 +00002415 if (Fn->getFunctionType() != FT ) {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002416 // The existing function doesn't have the same type. This is an overload
2417 // error.
2418 GEN_ERROR("Overload of function '" + FunctionName + "' not permitted.");
Devang Patel05988662008-09-25 21:00:45 +00002419 } else if (Fn->getAttributes() != PAL) {
Duncan Sandsdc024672007-11-27 13:23:08 +00002420 // The existing function doesn't have the same parameter attributes.
2421 // This is an overload error.
2422 GEN_ERROR("Overload of function '" + FunctionName + "' not permitted.");
Reid Spenceref9b9a72007-02-05 20:47:22 +00002423 } else if (!CurFun.isDeclare && !Fn->isDeclaration()) {
2424 // Neither the existing or the current function is a declaration and they
2425 // have the same name and same type. Clearly this is a redefinition.
Reid Spencerf4fa5902007-02-05 10:16:10 +00002426 GEN_ERROR("Redefinition of function '" + FunctionName + "'");
Duncan Sandsdc024672007-11-27 13:23:08 +00002427 } else if (Fn->isDeclaration()) {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002428 // Make sure to strip off any argument names so we can't get conflicts.
Chris Lattnere4d5c442005-03-15 04:54:21 +00002429 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
Chris Lattnera09000d2004-07-14 23:03:46 +00002430 AI != AE; ++AI)
2431 AI->setName("");
Reid Spenceref9b9a72007-02-05 20:47:22 +00002432 }
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00002433 } else { // Not already defined?
Gabor Greif051a9502008-04-06 20:25:17 +00002434 Fn = Function::Create(FT, GlobalValue::ExternalWeakLinkage, FunctionName,
2435 CurModule.CurrentModule);
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00002436 InsertValue(Fn, CurModule.Values);
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00002437 }
Chris Lattner00950542001-06-06 20:29:01 +00002438
Nuno Lopes06a62882008-10-03 15:44:21 +00002439 ID.destroy();
Chris Lattner394f2eb2003-10-16 20:12:13 +00002440 CurFun.FunctionStart(Fn);
Anton Korobeynikov93c2b372006-09-17 13:06:18 +00002441
2442 if (CurFun.isDeclare) {
2443 // If we have declaration, always overwrite linkage. This will allow us to
2444 // correctly handle cases, when pointer to function is passed as argument to
2445 // another function.
2446 Fn->setLinkage(CurFun.Linkage);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002447 Fn->setVisibility(CurFun.Visibility);
Anton Korobeynikov93c2b372006-09-17 13:06:18 +00002448 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00002449 Fn->setCallingConv($1);
Devang Patel05988662008-09-25 21:00:45 +00002450 Fn->setAttributes(PAL);
Devang Patel652203f2008-09-29 20:49:50 +00002451 Fn->setAlignment($10);
2452 if ($9) {
2453 Fn->setSection(*$9);
2454 delete $9;
Chris Lattner164c3782005-11-12 00:11:10 +00002455 }
Devang Patel652203f2008-09-29 20:49:50 +00002456 if ($11) {
2457 Fn->setGC($11->c_str());
2458 delete $11;
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00002459 }
Chris Lattner00950542001-06-06 20:29:01 +00002460
Chris Lattner7e708292002-06-25 16:13:24 +00002461 // Add all of the arguments we parsed to the function...
Devang Patel652203f2008-09-29 20:49:50 +00002462 if ($6) { // Is null if empty...
Chris Lattner69da5cf2002-10-13 20:57:00 +00002463 if (isVarArg) { // Nuke the last entry
Devang Patel652203f2008-09-29 20:49:50 +00002464 assert($6->back().Ty->get() == Type::VoidTy && $6->back().Name == 0 &&
Reid Spencer1755a9a2007-02-05 17:01:20 +00002465 "Not a varargs marker!");
Devang Patel652203f2008-09-29 20:49:50 +00002466 delete $6->back().Ty;
2467 $6->pop_back(); // Delete the last entry
Chris Lattner69da5cf2002-10-13 20:57:00 +00002468 }
Chris Lattnere4d5c442005-03-15 04:54:21 +00002469 Function::arg_iterator ArgIt = Fn->arg_begin();
Reid Spenceref9b9a72007-02-05 20:47:22 +00002470 Function::arg_iterator ArgEnd = Fn->arg_end();
Reid Spencere1553cc2006-12-31 05:40:12 +00002471 unsigned Idx = 1;
Devang Patel652203f2008-09-29 20:49:50 +00002472 for (ArgListType::iterator I = $6->begin();
2473 I != $6->end() && ArgIt != ArgEnd; ++I, ++ArgIt) {
Reid Spencere1553cc2006-12-31 05:40:12 +00002474 delete I->Ty; // Delete the typeholder...
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002475 setValueName(ArgIt, I->Name); // Insert arg into symtab...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002476 CHECK_FOR_ERROR
Chris Lattner69da5cf2002-10-13 20:57:00 +00002477 InsertValue(ArgIt);
Reid Spencere1553cc2006-12-31 05:40:12 +00002478 Idx++;
Chris Lattner00950542001-06-06 20:29:01 +00002479 }
Reid Spencer08de34b2006-12-03 05:45:44 +00002480
Devang Patel652203f2008-09-29 20:49:50 +00002481 delete $6; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00002482 }
Reid Spencer61c83e02006-08-18 08:43:06 +00002483 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002484};
Chris Lattner00950542001-06-06 20:29:01 +00002485
Chris Lattner9b02cc32002-05-03 18:23:48 +00002486BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
2487
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002488FunctionHeader : FunctionDefineLinkage GVVisibilityStyle FunctionHeaderH BEGIN {
Chris Lattner394f2eb2003-10-16 20:12:13 +00002489 $$ = CurFun.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00002490
Chris Lattner4ad02e72003-04-16 20:28:45 +00002491 // Make sure that we keep track of the linkage type even if there was a
2492 // previous "declare".
2493 $$->setLinkage($1);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002494 $$->setVisibility($2);
Chris Lattner51727be2002-06-04 21:58:56 +00002495};
Chris Lattner00950542001-06-06 20:29:01 +00002496
Chris Lattner9b02cc32002-05-03 18:23:48 +00002497END : ENDTOK | '}'; // Allow end of '}' to end a function
2498
Chris Lattner79df7c02002-03-26 18:01:55 +00002499Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00002500 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002501 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002502};
Chris Lattner00950542001-06-06 20:29:01 +00002503
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002504FunctionProto : FunctionDeclareLinkage GVVisibilityStyle FunctionHeaderH {
Reid Spencere1553cc2006-12-31 05:40:12 +00002505 CurFun.CurrentFunction->setLinkage($1);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002506 CurFun.CurrentFunction->setVisibility($2);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002507 $$ = CurFun.CurrentFunction;
2508 CurFun.FunctionDone();
2509 CHECK_FOR_ERROR
2510 };
Chris Lattner00950542001-06-06 20:29:01 +00002511
2512//===----------------------------------------------------------------------===//
2513// Rules to match Basic Blocks
2514//===----------------------------------------------------------------------===//
2515
Chris Lattneraa2c8532006-01-25 22:26:43 +00002516OptSideEffect : /* empty */ {
2517 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002518 CHECK_FOR_ERROR
Chris Lattneraa2c8532006-01-25 22:26:43 +00002519 }
2520 | SIDEEFFECT {
2521 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002522 CHECK_FOR_ERROR
Chris Lattneraa2c8532006-01-25 22:26:43 +00002523 };
2524
Chris Lattner00950542001-06-06 20:29:01 +00002525ConstValueRef : ESINT64VAL { // A reference to a direct constant
2526 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002527 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002528 }
2529 | EUINT64VAL {
2530 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002531 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002532 }
Chris Lattnerd1599012008-07-11 00:30:06 +00002533 | ESAPINTVAL { // arbitrary precision integer constants
2534 $$ = ValID::create(*$1, true);
2535 delete $1;
2536 CHECK_FOR_ERROR
Eric Christopher2a5196f2008-09-24 04:55:49 +00002537 }
Chris Lattnerd1599012008-07-11 00:30:06 +00002538 | EUAPINTVAL { // arbitrary precision integer constants
2539 $$ = ValID::create(*$1, false);
2540 delete $1;
2541 CHECK_FOR_ERROR
2542 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00002543 | FPVAL { // Perhaps it's an FP constant?
2544 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002545 CHECK_FOR_ERROR
Chris Lattner3d52b2f2001-07-15 00:17:01 +00002546 }
Chris Lattner91ef4602004-03-31 03:49:47 +00002547 | TRUETOK {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00002548 $$ = ValID::create(ConstantInt::getTrue());
Reid Spencer61c83e02006-08-18 08:43:06 +00002549 CHECK_FOR_ERROR
Eric Christopher2a5196f2008-09-24 04:55:49 +00002550 }
Chris Lattner91ef4602004-03-31 03:49:47 +00002551 | FALSETOK {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00002552 $$ = ValID::create(ConstantInt::getFalse());
Reid Spencer61c83e02006-08-18 08:43:06 +00002553 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002554 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00002555 | NULL_TOK {
2556 $$ = ValID::createNull();
Reid Spencer61c83e02006-08-18 08:43:06 +00002557 CHECK_FOR_ERROR
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002558 }
Chris Lattner16710e92004-10-16 18:17:13 +00002559 | UNDEF {
2560 $$ = ValID::createUndef();
Reid Spencer61c83e02006-08-18 08:43:06 +00002561 CHECK_FOR_ERROR
Chris Lattner16710e92004-10-16 18:17:13 +00002562 }
Chris Lattnerf1f03df2005-12-21 17:53:02 +00002563 | ZEROINITIALIZER { // A vector zero constant.
2564 $$ = ValID::createZeroInit();
Reid Spencer61c83e02006-08-18 08:43:06 +00002565 CHECK_FOR_ERROR
Chris Lattnerf1f03df2005-12-21 17:53:02 +00002566 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00002567 | '<' ConstVector '>' { // Nonempty unsized packed vector
Reid Spencer08de34b2006-12-03 05:45:44 +00002568 const Type *ETy = (*$2)[0]->getType();
Eric Christopher2a5196f2008-09-24 04:55:49 +00002569 unsigned NumElements = $2->size();
Dan Gohman9c531cd2008-05-23 18:23:11 +00002570
2571 if (!ETy->isInteger() && !ETy->isFloatingPoint())
2572 GEN_ERROR("Invalid vector element type: " + ETy->getDescription());
Eric Christopher2a5196f2008-09-24 04:55:49 +00002573
Reid Spencer9d6565a2007-02-15 02:26:10 +00002574 VectorType* pt = VectorType::get(ETy, NumElements);
Dan Gohmanf910eaa2008-06-09 14:45:02 +00002575 PATypeHolder* PTy = new PATypeHolder(HandleUpRefs(pt));
Eric Christopher2a5196f2008-09-24 04:55:49 +00002576
Brian Gaeke715c90b2004-08-20 06:00:58 +00002577 // Verify all elements are correct type!
2578 for (unsigned i = 0; i < $2->size(); i++) {
Reid Spencer08de34b2006-12-03 05:45:44 +00002579 if (ETy != (*$2)[i]->getType())
Eric Christopher2a5196f2008-09-24 04:55:49 +00002580 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Brian Gaeke715c90b2004-08-20 06:00:58 +00002581 ETy->getDescription() +"' as required!\nIt is of type '" +
Reid Spencer08de34b2006-12-03 05:45:44 +00002582 (*$2)[i]->getType()->getDescription() + "'.");
Brian Gaeke715c90b2004-08-20 06:00:58 +00002583 }
2584
Reid Spencer9d6565a2007-02-15 02:26:10 +00002585 $$ = ValID::create(ConstantVector::get(pt, *$2));
Brian Gaeke715c90b2004-08-20 06:00:58 +00002586 delete PTy; delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002587 CHECK_FOR_ERROR
Brian Gaeke715c90b2004-08-20 06:00:58 +00002588 }
Dan Gohmanf910eaa2008-06-09 14:45:02 +00002589 | '[' ConstVector ']' { // Nonempty unsized arr
2590 const Type *ETy = (*$2)[0]->getType();
Eric Christopher2a5196f2008-09-24 04:55:49 +00002591 uint64_t NumElements = $2->size();
Dan Gohmanf910eaa2008-06-09 14:45:02 +00002592
2593 if (!ETy->isFirstClassType())
2594 GEN_ERROR("Invalid array element type: " + ETy->getDescription());
2595
2596 ArrayType *ATy = ArrayType::get(ETy, NumElements);
2597 PATypeHolder* PTy = new PATypeHolder(HandleUpRefs(ATy));
2598
2599 // Verify all elements are correct type!
2600 for (unsigned i = 0; i < $2->size(); i++) {
2601 if (ETy != (*$2)[i]->getType())
Eric Christopher2a5196f2008-09-24 04:55:49 +00002602 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Dan Gohmanf910eaa2008-06-09 14:45:02 +00002603 ETy->getDescription() +"' as required!\nIt is of type '"+
2604 (*$2)[i]->getType()->getDescription() + "'.");
2605 }
2606
2607 $$ = ValID::create(ConstantArray::get(ATy, *$2));
2608 delete PTy; delete $2;
2609 CHECK_FOR_ERROR
2610 }
2611 | '[' ']' {
Dan Gohmanebb5a972008-06-23 18:40:28 +00002612 // Use undef instead of an array because it's inconvenient to determine
2613 // the element type at this point, there being no elements to examine.
Dan Gohmanf910eaa2008-06-09 14:45:02 +00002614 $$ = ValID::createUndef();
2615 CHECK_FOR_ERROR
2616 }
2617 | 'c' STRINGCONSTANT {
Dan Gohmanebb5a972008-06-23 18:40:28 +00002618 uint64_t NumElements = $2->length();
Dan Gohmanf910eaa2008-06-09 14:45:02 +00002619 const Type *ETy = Type::Int8Ty;
2620
2621 ArrayType *ATy = ArrayType::get(ETy, NumElements);
2622
2623 std::vector<Constant*> Vals;
2624 for (unsigned i = 0; i < $2->length(); ++i)
2625 Vals.push_back(ConstantInt::get(ETy, (*$2)[i]));
2626 delete $2;
2627 $$ = ValID::create(ConstantArray::get(ATy, Vals));
2628 CHECK_FOR_ERROR
2629 }
2630 | '{' ConstVector '}' {
2631 std::vector<const Type*> Elements($2->size());
2632 for (unsigned i = 0, e = $2->size(); i != e; ++i)
2633 Elements[i] = (*$2)[i]->getType();
2634
2635 const StructType *STy = StructType::get(Elements);
2636 PATypeHolder* PTy = new PATypeHolder(HandleUpRefs(STy));
2637
2638 $$ = ValID::create(ConstantStruct::get(STy, *$2));
2639 delete PTy; delete $2;
2640 CHECK_FOR_ERROR
2641 }
2642 | '{' '}' {
2643 const StructType *STy = StructType::get(std::vector<const Type*>());
2644 $$ = ValID::create(ConstantStruct::get(STy, std::vector<Constant*>()));
2645 CHECK_FOR_ERROR
2646 }
2647 | '<' '{' ConstVector '}' '>' {
2648 std::vector<const Type*> Elements($3->size());
2649 for (unsigned i = 0, e = $3->size(); i != e; ++i)
2650 Elements[i] = (*$3)[i]->getType();
2651
2652 const StructType *STy = StructType::get(Elements, /*isPacked=*/true);
2653 PATypeHolder* PTy = new PATypeHolder(HandleUpRefs(STy));
2654
2655 $$ = ValID::create(ConstantStruct::get(STy, *$3));
2656 delete PTy; delete $3;
2657 CHECK_FOR_ERROR
2658 }
2659 | '<' '{' '}' '>' {
2660 const StructType *STy = StructType::get(std::vector<const Type*>(),
2661 /*isPacked=*/true);
2662 $$ = ValID::create(ConstantStruct::get(STy, std::vector<Constant*>()));
2663 CHECK_FOR_ERROR
2664 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00002665 | ConstExpr {
Reid Spencer08de34b2006-12-03 05:45:44 +00002666 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002667 CHECK_FOR_ERROR
Chris Lattneraa2c8532006-01-25 22:26:43 +00002668 }
2669 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002670 $$ = ValID::createInlineAsm(*$3, *$5, $2);
2671 delete $3;
2672 delete $5;
Reid Spencer61c83e02006-08-18 08:43:06 +00002673 CHECK_FOR_ERROR
Chris Lattnerd78700d2002-08-16 21:14:40 +00002674 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00002675
Chris Lattner2079fde2001-10-13 06:41:08 +00002676// SymbolicValueRef - Reference to one of two ways of symbolically refering to
2677// another value.
2678//
Reid Spencerb2d17862007-01-26 08:04:51 +00002679SymbolicValueRef : LOCALVAL_ID { // Is it an integer reference...?
2680 $$ = ValID::createLocalID($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002681 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002682 }
Reid Spencerb2d17862007-01-26 08:04:51 +00002683 | GLOBALVAL_ID {
2684 $$ = ValID::createGlobalID($1);
2685 CHECK_FOR_ERROR
2686 }
2687 | LocalName { // Is it a named reference...?
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002688 $$ = ValID::createLocalName(*$1);
2689 delete $1;
Reid Spencerb2d17862007-01-26 08:04:51 +00002690 CHECK_FOR_ERROR
2691 }
2692 | GlobalName { // Is it a named reference...?
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002693 $$ = ValID::createGlobalName(*$1);
2694 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002695 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002696 };
Chris Lattner2079fde2001-10-13 06:41:08 +00002697
2698// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00002699ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00002700
Chris Lattner00950542001-06-06 20:29:01 +00002701
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002702// ResolvedVal - a <type> <value> pair. This is used only in cases where the
2703// type immediately preceeds the value reference, and allows complex constant
2704// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00002705ResolvedVal : Types ValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00002706 if (!UpRefs.empty())
2707 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Eric Christopher2a5196f2008-09-24 04:55:49 +00002708 $$ = getVal(*$1, $2);
Reid Spencere1553cc2006-12-31 05:40:12 +00002709 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002710 CHECK_FOR_ERROR
Reid Spencere1553cc2006-12-31 05:40:12 +00002711 }
2712 ;
Chris Lattner8b81bf52001-07-25 22:47:46 +00002713
Devang Patel5af2f632008-02-20 22:39:45 +00002714ReturnedVal : ResolvedVal {
2715 $$ = new std::vector<Value *>();
Eric Christopher2a5196f2008-09-24 04:55:49 +00002716 $$->push_back($1);
Devang Patel5af2f632008-02-20 22:39:45 +00002717 CHECK_FOR_ERROR
2718 }
Devang Patel57ef4f42008-02-23 00:35:18 +00002719 | ReturnedVal ',' ResolvedVal {
Eric Christopher2a5196f2008-09-24 04:55:49 +00002720 ($$=$1)->push_back($3);
Devang Patel5af2f632008-02-20 22:39:45 +00002721 CHECK_FOR_ERROR
2722 };
2723
Chris Lattner00950542001-06-06 20:29:01 +00002724BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner8ab406d2004-07-13 07:59:27 +00002725 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002726 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002727 }
Eric Christopher2a5196f2008-09-24 04:55:49 +00002728 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
Chris Lattner8ab406d2004-07-13 07:59:27 +00002729 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002730 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002731 };
Chris Lattner00950542001-06-06 20:29:01 +00002732
2733
Eric Christopher2a5196f2008-09-24 04:55:49 +00002734// Basic blocks are terminated by branching instructions:
Chris Lattner00950542001-06-06 20:29:01 +00002735// br, br/cc, switch, ret
2736//
Chris Lattner6ac28092008-08-29 17:12:13 +00002737BasicBlock : InstructionList OptLocalAssign BBTerminatorInst {
Chris Lattner8ab406d2004-07-13 07:59:27 +00002738 setValueName($3, $2);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002739 CHECK_FOR_ERROR
Chris Lattner2079fde2001-10-13 06:41:08 +00002740 InsertValue($3);
Chris Lattner2079fde2001-10-13 06:41:08 +00002741 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00002742 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002743 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002744 };
Chris Lattner00950542001-06-06 20:29:01 +00002745
Chris Lattner6ac28092008-08-29 17:12:13 +00002746BasicBlock : InstructionList LocalNumber BBTerminatorInst {
2747 CHECK_FOR_ERROR
2748 int ValNum = InsertValue($3);
2749 if (ValNum != (int)$2)
2750 GEN_ERROR("Result value number %" + utostr($2) +
2751 " is incorrect, expected %" + utostr((unsigned)ValNum));
Eric Christopher2a5196f2008-09-24 04:55:49 +00002752
Chris Lattner6ac28092008-08-29 17:12:13 +00002753 $1->getInstList().push_back($3);
2754 $$ = $1;
2755 CHECK_FOR_ERROR
2756};
2757
2758
Chris Lattner00950542001-06-06 20:29:01 +00002759InstructionList : InstructionList Inst {
Reid Spencer3da59db2006-11-27 01:05:10 +00002760 if (CastInst *CI1 = dyn_cast<CastInst>($2))
2761 if (CastInst *CI2 = dyn_cast<CastInst>(CI1->getOperand(0)))
2762 if (CI2->getParent() == 0)
2763 $1->getInstList().push_back(CI2);
Chris Lattner00950542001-06-06 20:29:01 +00002764 $1->getInstList().push_back($2);
2765 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002766 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002767 }
Reid Spencer186a43f2007-03-19 18:39:36 +00002768 | /* empty */ { // Empty space between instruction lists
Nick Lewycky280a6e62008-04-25 16:53:59 +00002769 $$ = defineBBVal(ValID::createLocalID(CurFun.NextValNum));
Reid Spencer61c83e02006-08-18 08:43:06 +00002770 CHECK_FOR_ERROR
Chris Lattner22ae2322004-07-13 08:39:15 +00002771 }
Reid Spencer186a43f2007-03-19 18:39:36 +00002772 | LABELSTR { // Labelled (named) basic block
Nick Lewycky280a6e62008-04-25 16:53:59 +00002773 $$ = defineBBVal(ValID::createLocalName(*$1));
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002774 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002775 CHECK_FOR_ERROR
Nick Lewycky280a6e62008-04-25 16:53:59 +00002776
Chris Lattner51727be2002-06-04 21:58:56 +00002777 };
Chris Lattner00950542001-06-06 20:29:01 +00002778
Eric Christopher2a5196f2008-09-24 04:55:49 +00002779BBTerminatorInst :
Devang Patel5af2f632008-02-20 22:39:45 +00002780 RET ReturnedVal { // Return with a result...
Devang Patel53284d32008-02-26 22:12:58 +00002781 ValueList &VL = *$2;
Devang Patel43a1bb32008-02-26 23:17:50 +00002782 assert(!VL.empty() && "Invalid ret operands!");
Dan Gohmanfc74abf2008-07-23 00:34:11 +00002783 const Type *ReturnType = CurFun.CurrentFunction->getReturnType();
2784 if (VL.size() > 1 ||
2785 (isa<StructType>(ReturnType) &&
2786 (VL.empty() || VL[0]->getType() != ReturnType))) {
2787 Value *RV = UndefValue::get(ReturnType);
2788 for (unsigned i = 0, e = VL.size(); i != e; ++i) {
2789 Instruction *I = InsertValueInst::Create(RV, VL[i], i, "mrv");
2790 ($<BasicBlockVal>-1)->getInstList().push_back(I);
2791 RV = I;
2792 }
2793 $$ = ReturnInst::Create(RV);
2794 } else {
2795 $$ = ReturnInst::Create(VL[0]);
2796 }
Devang Patel5af2f632008-02-20 22:39:45 +00002797 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002798 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002799 }
Reid Spencer186a43f2007-03-19 18:39:36 +00002800 | RET VOID { // Return with no result...
Gabor Greif051a9502008-04-06 20:25:17 +00002801 $$ = ReturnInst::Create();
Reid Spencer61c83e02006-08-18 08:43:06 +00002802 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002803 }
Reid Spencer186a43f2007-03-19 18:39:36 +00002804 | BR LABEL ValueRef { // Unconditional Branch...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002805 BasicBlock* tmpBB = getBBVal($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002806 CHECK_FOR_ERROR
Gabor Greif051a9502008-04-06 20:25:17 +00002807 $$ = BranchInst::Create(tmpBB);
Reid Spencer186a43f2007-03-19 18:39:36 +00002808 } // Conditional Branch...
Eric Christopher2a5196f2008-09-24 04:55:49 +00002809 | BR INTTYPE ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Dan Gohman9c531cd2008-05-23 18:23:11 +00002810 if (cast<IntegerType>($2)->getBitWidth() != 1)
2811 GEN_ERROR("Branch condition must have type i1");
Reid Spencer5b7e7532006-09-28 19:28:24 +00002812 BasicBlock* tmpBBA = getBBVal($6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002813 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002814 BasicBlock* tmpBBB = getBBVal($9);
2815 CHECK_FOR_ERROR
Reid Spencer4fe16d62007-01-11 18:21:29 +00002816 Value* tmpVal = getVal(Type::Int1Ty, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002817 CHECK_FOR_ERROR
Gabor Greif051a9502008-04-06 20:25:17 +00002818 $$ = BranchInst::Create(tmpBBA, tmpBBB, tmpVal);
Chris Lattner00950542001-06-06 20:29:01 +00002819 }
2820 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencer08de34b2006-12-03 05:45:44 +00002821 Value* tmpVal = getVal($2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002822 CHECK_FOR_ERROR
2823 BasicBlock* tmpBB = getBBVal($6);
2824 CHECK_FOR_ERROR
Gabor Greif051a9502008-04-06 20:25:17 +00002825 SwitchInst *S = SwitchInst::Create(tmpVal, tmpBB, $8->size());
Chris Lattner00950542001-06-06 20:29:01 +00002826 $$ = S;
2827
Chris Lattner9232b992003-04-22 18:42:41 +00002828 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
Chris Lattner46748042002-04-09 19:41:42 +00002829 E = $8->end();
Chris Lattner69331f52005-02-24 05:25:17 +00002830 for (; I != E; ++I) {
2831 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
2832 S->addCase(CI, I->second);
2833 else
Reid Spencerf4fa5902007-02-05 10:16:10 +00002834 GEN_ERROR("Switch case is constant, but not a simple integer");
Chris Lattner69331f52005-02-24 05:25:17 +00002835 }
Chris Lattnerf57a43d2004-04-17 23:49:15 +00002836 delete $8;
Reid Spencer61c83e02006-08-18 08:43:06 +00002837 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002838 }
Chris Lattner196850c2003-05-15 21:30:00 +00002839 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencer08de34b2006-12-03 05:45:44 +00002840 Value* tmpVal = getVal($2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002841 CHECK_FOR_ERROR
2842 BasicBlock* tmpBB = getBBVal($6);
2843 CHECK_FOR_ERROR
Gabor Greif051a9502008-04-06 20:25:17 +00002844 SwitchInst *S = SwitchInst::Create(tmpVal, tmpBB, 0);
Chris Lattner196850c2003-05-15 21:30:00 +00002845 $$ = S;
Reid Spencer61c83e02006-08-18 08:43:06 +00002846 CHECK_FOR_ERROR
Chris Lattner196850c2003-05-15 21:30:00 +00002847 }
Devang Patel652203f2008-09-29 20:49:50 +00002848 | INVOKE OptCallingConv OptRetAttrs ResultTypes ValueRef '(' ParamList ')'
2849 OptFuncAttrs TO LABEL ValueRef UNWIND LABEL ValueRef {
Chris Lattner2079fde2001-10-13 06:41:08 +00002850
Reid Spencere1553cc2006-12-31 05:40:12 +00002851 // Handle the short syntax
2852 const PointerType *PFTy = 0;
2853 const FunctionType *Ty = 0;
Devang Patel652203f2008-09-29 20:49:50 +00002854 if (!(PFTy = dyn_cast<PointerType>($4->get())) ||
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002855 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00002856 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00002857 std::vector<const Type*> ParamTypes;
Devang Patel652203f2008-09-29 20:49:50 +00002858 ParamList::iterator I = $7->begin(), E = $7->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00002859 for (; I != E; ++I) {
Reid Spencere1553cc2006-12-31 05:40:12 +00002860 const Type *Ty = I->Val->getType();
2861 if (Ty == Type::VoidTy)
2862 GEN_ERROR("Short call syntax cannot be used with varargs");
2863 ParamTypes.push_back(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00002864 }
Eric Christopher2a5196f2008-09-24 04:55:49 +00002865
Devang Patel652203f2008-09-29 20:49:50 +00002866 if (!FunctionType::isValidReturnType(*$4))
Chris Lattnerf7dedf22008-04-23 05:36:58 +00002867 GEN_ERROR("Invalid result type for LLVM function");
2868
Devang Patel652203f2008-09-29 20:49:50 +00002869 Ty = FunctionType::get($4->get(), ParamTypes, false);
Christopher Lamb43ad6b32007-12-17 01:12:55 +00002870 PFTy = PointerType::getUnqual(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00002871 }
Chris Lattner2079fde2001-10-13 06:41:08 +00002872
Devang Patel652203f2008-09-29 20:49:50 +00002873 delete $4;
Reid Spencer2c9df212007-03-20 01:13:00 +00002874
Devang Patel652203f2008-09-29 20:49:50 +00002875 Value *V = getVal(PFTy, $5); // Get the function we're calling...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002876 CHECK_FOR_ERROR
Devang Patel652203f2008-09-29 20:49:50 +00002877 BasicBlock *Normal = getBBVal($12);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002878 CHECK_FOR_ERROR
Devang Patel652203f2008-09-29 20:49:50 +00002879 BasicBlock *Except = getBBVal($15);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002880 CHECK_FOR_ERROR
Chris Lattner2079fde2001-10-13 06:41:08 +00002881
Devang Patel05988662008-09-25 21:00:45 +00002882 SmallVector<AttributeWithIndex, 8> Attrs;
Devang Patel19c87462008-09-26 22:53:05 +00002883 //FIXME : In 3.0, stop accepting zext, sext and inreg as optional function
2884 //attributes.
Devang Patel652203f2008-09-29 20:49:50 +00002885 Attributes RetAttrs = $3;
2886 if ($9 != Attribute::None) {
2887 if ($9 & Attribute::ZExt) {
Devang Patel19c87462008-09-26 22:53:05 +00002888 RetAttrs = RetAttrs | Attribute::ZExt;
Devang Patel652203f2008-09-29 20:49:50 +00002889 $9 = $9 ^ Attribute::ZExt;
Devang Patel19c87462008-09-26 22:53:05 +00002890 }
Devang Patel652203f2008-09-29 20:49:50 +00002891 if ($9 & Attribute::SExt) {
Devang Patel19c87462008-09-26 22:53:05 +00002892 RetAttrs = RetAttrs | Attribute::SExt;
Devang Patel652203f2008-09-29 20:49:50 +00002893 $9 = $9 ^ Attribute::SExt;
Devang Patel19c87462008-09-26 22:53:05 +00002894 }
Devang Patel652203f2008-09-29 20:49:50 +00002895 if ($9 & Attribute::InReg) {
Devang Patel19c87462008-09-26 22:53:05 +00002896 RetAttrs = RetAttrs | Attribute::InReg;
Devang Patel652203f2008-09-29 20:49:50 +00002897 $9 = $9 ^ Attribute::InReg;
Devang Patel19c87462008-09-26 22:53:05 +00002898 }
Devang Patel19c87462008-09-26 22:53:05 +00002899 }
Devang Patel652203f2008-09-29 20:49:50 +00002900 if (RetAttrs != Attribute::None)
2901 Attrs.push_back(AttributeWithIndex::get(0, RetAttrs));
Devang Patel19c87462008-09-26 22:53:05 +00002902
Reid Spencere1553cc2006-12-31 05:40:12 +00002903 // Check the arguments
2904 ValueList Args;
Devang Patel652203f2008-09-29 20:49:50 +00002905 if ($7->empty()) { // Has no arguments?
Reid Spencere1553cc2006-12-31 05:40:12 +00002906 // Make sure no arguments is a good thing!
2907 if (Ty->getNumParams() != 0)
2908 GEN_ERROR("No arguments passed to a function that "
Reid Spencerf4fa5902007-02-05 10:16:10 +00002909 "expects arguments");
Chris Lattner2079fde2001-10-13 06:41:08 +00002910 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00002911 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00002912 // correctly!
Chris Lattnerd5d89962004-02-09 04:14:01 +00002913 FunctionType::param_iterator I = Ty->param_begin();
2914 FunctionType::param_iterator E = Ty->param_end();
Devang Patel652203f2008-09-29 20:49:50 +00002915 ParamList::iterator ArgI = $7->begin(), ArgE = $7->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00002916 unsigned index = 1;
Reid Spencer863dd882007-04-28 16:06:50 +00002917
Duncan Sandsdc024672007-11-27 13:23:08 +00002918 for (; ArgI != ArgE && I != E; ++ArgI, ++I, ++index) {
Reid Spencere1553cc2006-12-31 05:40:12 +00002919 if (ArgI->Val->getType() != *I)
2920 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00002921 (*I)->getDescription() + "'");
Reid Spencere1553cc2006-12-31 05:40:12 +00002922 Args.push_back(ArgI->Val);
Devang Patel05988662008-09-25 21:00:45 +00002923 if (ArgI->Attrs != Attribute::None)
2924 Attrs.push_back(AttributeWithIndex::get(index, ArgI->Attrs));
Reid Spencere1553cc2006-12-31 05:40:12 +00002925 }
Reid Spencer08de34b2006-12-03 05:45:44 +00002926
Reid Spencere1553cc2006-12-31 05:40:12 +00002927 if (Ty->isVarArg()) {
2928 if (I == E)
Duncan Sandseb824702008-01-11 21:23:39 +00002929 for (; ArgI != ArgE; ++ArgI, ++index) {
Reid Spencere1553cc2006-12-31 05:40:12 +00002930 Args.push_back(ArgI->Val); // push the remaining varargs
Devang Patel05988662008-09-25 21:00:45 +00002931 if (ArgI->Attrs != Attribute::None)
2932 Attrs.push_back(AttributeWithIndex::get(index, ArgI->Attrs));
Duncan Sandseb824702008-01-11 21:23:39 +00002933 }
Reid Spencere1553cc2006-12-31 05:40:12 +00002934 } else if (I != E || ArgI != ArgE)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002935 GEN_ERROR("Invalid number of parameters detected");
Chris Lattner2079fde2001-10-13 06:41:08 +00002936 }
Devang Patel652203f2008-09-29 20:49:50 +00002937 if ($9 != Attribute::None)
2938 Attrs.push_back(AttributeWithIndex::get(~0, $9));
Devang Patel05988662008-09-25 21:00:45 +00002939 AttrListPtr PAL;
Duncan Sandsdc024672007-11-27 13:23:08 +00002940 if (!Attrs.empty())
Devang Patel05988662008-09-25 21:00:45 +00002941 PAL = AttrListPtr::get(Attrs.begin(), Attrs.end());
Duncan Sandsdc024672007-11-27 13:23:08 +00002942
Reid Spencere1553cc2006-12-31 05:40:12 +00002943 // Create the InvokeInst
Gabor Greifb1dbcd82008-05-15 10:04:30 +00002944 InvokeInst *II = InvokeInst::Create(V, Normal, Except,
2945 Args.begin(), Args.end());
Reid Spencere1553cc2006-12-31 05:40:12 +00002946 II->setCallingConv($2);
Devang Patel05988662008-09-25 21:00:45 +00002947 II->setAttributes(PAL);
Reid Spencere1553cc2006-12-31 05:40:12 +00002948 $$ = II;
Devang Patel652203f2008-09-29 20:49:50 +00002949 delete $7;
Reid Spencer61c83e02006-08-18 08:43:06 +00002950 CHECK_FOR_ERROR
Chris Lattner36143fc2003-09-08 18:54:55 +00002951 }
2952 | UNWIND {
2953 $$ = new UnwindInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002954 CHECK_FOR_ERROR
Chris Lattner16710e92004-10-16 18:17:13 +00002955 }
2956 | UNREACHABLE {
2957 $$ = new UnreachableInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002958 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002959 };
Chris Lattner2079fde2001-10-13 06:41:08 +00002960
2961
Chris Lattner00950542001-06-06 20:29:01 +00002962
2963JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
2964 $$ = $1;
Reid Spencer186a43f2007-03-19 18:39:36 +00002965 Constant *V = cast<Constant>(getExistingVal($2, $3));
Reid Spencer5b7e7532006-09-28 19:28:24 +00002966 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002967 if (V == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002968 GEN_ERROR("May only switch on a constant pool value");
Chris Lattner00950542001-06-06 20:29:01 +00002969
Reid Spencer5b7e7532006-09-28 19:28:24 +00002970 BasicBlock* tmpBB = getBBVal($6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002971 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002972 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner00950542001-06-06 20:29:01 +00002973 }
2974 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner9232b992003-04-22 18:42:41 +00002975 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Reid Spencer186a43f2007-03-19 18:39:36 +00002976 Constant *V = cast<Constant>(getExistingVal($1, $2));
Reid Spencer5b7e7532006-09-28 19:28:24 +00002977 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002978
2979 if (V == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002980 GEN_ERROR("May only switch on a constant pool value");
Chris Lattner00950542001-06-06 20:29:01 +00002981
Reid Spencer5b7e7532006-09-28 19:28:24 +00002982 BasicBlock* tmpBB = getBBVal($5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002983 CHECK_FOR_ERROR
Eric Christopher2a5196f2008-09-24 04:55:49 +00002984 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner51727be2002-06-04 21:58:56 +00002985 };
Chris Lattner00950542001-06-06 20:29:01 +00002986
Reid Spencerb2d17862007-01-26 08:04:51 +00002987Inst : OptLocalAssign InstVal {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002988 // Is this definition named?? if so, assign the name...
2989 setValueName($2, $1);
2990 CHECK_FOR_ERROR
2991 InsertValue($2);
2992 $$ = $2;
2993 CHECK_FOR_ERROR
2994 };
2995
Chris Lattner6ac28092008-08-29 17:12:13 +00002996Inst : LocalNumber InstVal {
2997 CHECK_FOR_ERROR
2998 int ValNum = InsertValue($2);
Eric Christopher2a5196f2008-09-24 04:55:49 +00002999
Chris Lattner6ac28092008-08-29 17:12:13 +00003000 if (ValNum != (int)$1)
3001 GEN_ERROR("Result value number %" + utostr($1) +
3002 " is incorrect, expected %" + utostr((unsigned)ValNum));
3003
3004 $$ = $2;
3005 CHECK_FOR_ERROR
3006 };
3007
Chris Lattner00950542001-06-06 20:29:01 +00003008
Chris Lattnerc24d2082001-06-11 15:04:20 +00003009PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Reid Spencere1553cc2006-12-31 05:40:12 +00003010 if (!UpRefs.empty())
3011 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Chris Lattner9232b992003-04-22 18:42:41 +00003012 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Reid Spencer08de34b2006-12-03 05:45:44 +00003013 Value* tmpVal = getVal(*$1, $3);
Reid Spencer61c83e02006-08-18 08:43:06 +00003014 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00003015 BasicBlock* tmpBB = getBBVal($5);
3016 CHECK_FOR_ERROR
3017 $$->push_back(std::make_pair(tmpVal, tmpBB));
Reid Spencer08de34b2006-12-03 05:45:44 +00003018 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00003019 }
3020 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
3021 $$ = $1;
Reid Spencer5b7e7532006-09-28 19:28:24 +00003022 Value* tmpVal = getVal($1->front().first->getType(), $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00003023 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00003024 BasicBlock* tmpBB = getBBVal($6);
3025 CHECK_FOR_ERROR
3026 $1->push_back(std::make_pair(tmpVal, tmpBB));
Chris Lattner51727be2002-06-04 21:58:56 +00003027 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00003028
3029
Devang Patel05988662008-09-25 21:00:45 +00003030ParamList : Types OptAttributes ValueRef OptAttributes {
3031 // FIXME: Remove trailing OptAttributes in LLVM 3.0, it was a mistake in 2.0
Reid Spencere1553cc2006-12-31 05:40:12 +00003032 if (!UpRefs.empty())
3033 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
3034 // Used for call and invoke instructions
Dale Johanneseneb57ea72007-11-05 21:20:28 +00003035 $$ = new ParamList();
Duncan Sandsdc024672007-11-27 13:23:08 +00003036 ParamListEntry E; E.Attrs = $2 | $4; E.Val = getVal($1->get(), $3);
Reid Spencere1553cc2006-12-31 05:40:12 +00003037 $$->push_back(E);
Reid Spencer2c9df212007-03-20 01:13:00 +00003038 delete $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00003039 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00003040 }
Devang Patel05988662008-09-25 21:00:45 +00003041 | LABEL OptAttributes ValueRef OptAttributes {
3042 // FIXME: Remove trailing OptAttributes in LLVM 3.0, it was a mistake in 2.0
Dale Johanneseneb57ea72007-11-05 21:20:28 +00003043 // Labels are only valid in ASMs
3044 $$ = new ParamList();
Duncan Sandsdc024672007-11-27 13:23:08 +00003045 ParamListEntry E; E.Attrs = $2 | $4; E.Val = getBBVal($3);
Dale Johanneseneb57ea72007-11-05 21:20:28 +00003046 $$->push_back(E);
Duncan Sandsdc024672007-11-27 13:23:08 +00003047 CHECK_FOR_ERROR
Dale Johanneseneb57ea72007-11-05 21:20:28 +00003048 }
Devang Patel05988662008-09-25 21:00:45 +00003049 | ParamList ',' Types OptAttributes ValueRef OptAttributes {
3050 // FIXME: Remove trailing OptAttributes in LLVM 3.0, it was a mistake in 2.0
Reid Spencere1553cc2006-12-31 05:40:12 +00003051 if (!UpRefs.empty())
3052 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattner00950542001-06-06 20:29:01 +00003053 $$ = $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00003054 ParamListEntry E; E.Attrs = $4 | $6; E.Val = getVal($3->get(), $5);
Reid Spencere1553cc2006-12-31 05:40:12 +00003055 $$->push_back(E);
Reid Spencer2c9df212007-03-20 01:13:00 +00003056 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00003057 CHECK_FOR_ERROR
Reid Spencere1553cc2006-12-31 05:40:12 +00003058 }
Devang Patel05988662008-09-25 21:00:45 +00003059 | ParamList ',' LABEL OptAttributes ValueRef OptAttributes {
3060 // FIXME: Remove trailing OptAttributes in LLVM 3.0, it was a mistake in 2.0
Dale Johanneseneb57ea72007-11-05 21:20:28 +00003061 $$ = $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00003062 ParamListEntry E; E.Attrs = $4 | $6; E.Val = getBBVal($5);
Dale Johanneseneb57ea72007-11-05 21:20:28 +00003063 $$->push_back(E);
3064 CHECK_FOR_ERROR
3065 }
3066 | /*empty*/ { $$ = new ParamList(); };
Chris Lattner00950542001-06-06 20:29:01 +00003067
Reid Spencere1553cc2006-12-31 05:40:12 +00003068IndexList // Used for gep instructions and constant expressions
Reid Spencere03969f2006-12-31 21:46:36 +00003069 : /*empty*/ { $$ = new std::vector<Value*>(); }
Reid Spencere1553cc2006-12-31 05:40:12 +00003070 | IndexList ',' ResolvedVal {
3071 $$ = $1;
3072 $$->push_back($3);
3073 CHECK_FOR_ERROR
3074 }
Reid Spencer71305d72006-12-31 21:25:25 +00003075 ;
Chris Lattner00950542001-06-06 20:29:01 +00003076
Dan Gohman81a0c0b2008-05-31 00:58:22 +00003077ConstantIndexList // Used for insertvalue and extractvalue instructions
3078 : ',' EUINT64VAL {
3079 $$ = new std::vector<unsigned>();
3080 if ((unsigned)$2 != $2)
3081 GEN_ERROR("Index " + utostr($2) + " is not valid for insertvalue or extractvalue.");
3082 $$->push_back($2);
3083 }
3084 | ConstantIndexList ',' EUINT64VAL {
3085 $$ = $1;
3086 if ((unsigned)$3 != $3)
3087 GEN_ERROR("Index " + utostr($3) + " is not valid for insertvalue or extractvalue.");
3088 $$->push_back($3);
3089 CHECK_FOR_ERROR
3090 }
3091 ;
3092
Chris Lattnerddb6db42005-05-06 05:51:46 +00003093OptTailCall : TAIL CALL {
3094 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00003095 CHECK_FOR_ERROR
Chris Lattnerddb6db42005-05-06 05:51:46 +00003096 }
3097 | CALL {
3098 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00003099 CHECK_FOR_ERROR
Chris Lattnerddb6db42005-05-06 05:51:46 +00003100 };
3101
Chris Lattner4a6482b2002-09-10 19:57:26 +00003102InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00003103 if (!UpRefs.empty())
3104 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Eric Christopher2a5196f2008-09-24 04:55:49 +00003105 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint() &&
Reid Spencer9d6565a2007-02-15 02:26:10 +00003106 !isa<VectorType>((*$2).get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00003107 GEN_ERROR(
Reid Spencerf4fa5902007-02-05 10:16:10 +00003108 "Arithmetic operator requires integer, FP, or packed operands");
Eric Christopher2a5196f2008-09-24 04:55:49 +00003109 Value* val1 = getVal(*$2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00003110 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00003111 Value* val2 = getVal(*$2, $5);
Reid Spencer5b7e7532006-09-28 19:28:24 +00003112 CHECK_FOR_ERROR
Gabor Greif7cbd8a32008-05-16 19:29:10 +00003113 $$ = BinaryOperator::Create($1, val1, val2);
Chris Lattner4a6482b2002-09-10 19:57:26 +00003114 if ($$ == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00003115 GEN_ERROR("binary operator returned null");
Reid Spencer08de34b2006-12-03 05:45:44 +00003116 delete $2;
Chris Lattner4a6482b2002-09-10 19:57:26 +00003117 }
3118 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00003119 if (!UpRefs.empty())
3120 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Chris Lattner42a75512007-01-15 02:27:26 +00003121 if (!(*$2)->isInteger()) {
Nate Begeman5bc1ea02008-07-29 15:49:41 +00003122 if (!isa<VectorType>($2->get()) ||
Reid Spencer9d6565a2007-02-15 02:26:10 +00003123 !cast<VectorType>($2->get())->getElementType()->isInteger())
Reid Spencerf4fa5902007-02-05 10:16:10 +00003124 GEN_ERROR("Logical operator requires integral operands");
Chris Lattner0a017832005-12-21 18:31:29 +00003125 }
Reid Spencer08de34b2006-12-03 05:45:44 +00003126 Value* tmpVal1 = getVal(*$2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00003127 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00003128 Value* tmpVal2 = getVal(*$2, $5);
Reid Spencer5b7e7532006-09-28 19:28:24 +00003129 CHECK_FOR_ERROR
Gabor Greif7cbd8a32008-05-16 19:29:10 +00003130 $$ = BinaryOperator::Create($1, tmpVal1, tmpVal2);
Chris Lattner4a6482b2002-09-10 19:57:26 +00003131 if ($$ == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00003132 GEN_ERROR("binary operator returned null");
Reid Spencer08de34b2006-12-03 05:45:44 +00003133 delete $2;
Chris Lattner4a6482b2002-09-10 19:57:26 +00003134 }
Reid Spencer08de34b2006-12-03 05:45:44 +00003135 | ICMP IPredicates Types ValueRef ',' ValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00003136 if (!UpRefs.empty())
3137 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00003138 Value* tmpVal1 = getVal(*$3, $4);
3139 CHECK_FOR_ERROR
3140 Value* tmpVal2 = getVal(*$3, $6);
3141 CHECK_FOR_ERROR
Gabor Greif7cbd8a32008-05-16 19:29:10 +00003142 $$ = CmpInst::Create($1, $2, tmpVal1, tmpVal2);
Reid Spencer08de34b2006-12-03 05:45:44 +00003143 if ($$ == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00003144 GEN_ERROR("icmp operator returned null");
Reid Spencer2c9df212007-03-20 01:13:00 +00003145 delete $3;
Reid Spencer08de34b2006-12-03 05:45:44 +00003146 }
3147 | FCMP FPredicates Types ValueRef ',' ValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00003148 if (!UpRefs.empty())
3149 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00003150 Value* tmpVal1 = getVal(*$3, $4);
3151 CHECK_FOR_ERROR
3152 Value* tmpVal2 = getVal(*$3, $6);
3153 CHECK_FOR_ERROR
Gabor Greif7cbd8a32008-05-16 19:29:10 +00003154 $$ = CmpInst::Create($1, $2, tmpVal1, tmpVal2);
Reid Spencer08de34b2006-12-03 05:45:44 +00003155 if ($$ == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00003156 GEN_ERROR("fcmp operator returned null");
Reid Spencer2c9df212007-03-20 01:13:00 +00003157 delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00003158 }
Nate Begemanac80ade2008-05-12 19:01:56 +00003159 | VICMP IPredicates Types ValueRef ',' ValueRef {
3160 if (!UpRefs.empty())
3161 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
3162 if (!isa<VectorType>((*$3).get()))
3163 GEN_ERROR("Scalar types not supported by vicmp instruction");
3164 Value* tmpVal1 = getVal(*$3, $4);
3165 CHECK_FOR_ERROR
3166 Value* tmpVal2 = getVal(*$3, $6);
3167 CHECK_FOR_ERROR
Gabor Greif7cbd8a32008-05-16 19:29:10 +00003168 $$ = CmpInst::Create($1, $2, tmpVal1, tmpVal2);
Nate Begemanac80ade2008-05-12 19:01:56 +00003169 if ($$ == 0)
Dan Gohmanf72fb672008-09-09 01:02:47 +00003170 GEN_ERROR("vicmp operator returned null");
Nate Begemanac80ade2008-05-12 19:01:56 +00003171 delete $3;
3172 }
3173 | VFCMP FPredicates Types ValueRef ',' ValueRef {
3174 if (!UpRefs.empty())
3175 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
3176 if (!isa<VectorType>((*$3).get()))
3177 GEN_ERROR("Scalar types not supported by vfcmp instruction");
3178 Value* tmpVal1 = getVal(*$3, $4);
3179 CHECK_FOR_ERROR
3180 Value* tmpVal2 = getVal(*$3, $6);
3181 CHECK_FOR_ERROR
Gabor Greif7cbd8a32008-05-16 19:29:10 +00003182 $$ = CmpInst::Create($1, $2, tmpVal1, tmpVal2);
Nate Begemanac80ade2008-05-12 19:01:56 +00003183 if ($$ == 0)
Dan Gohmanf72fb672008-09-09 01:02:47 +00003184 GEN_ERROR("vfcmp operator returned null");
Nate Begemanac80ade2008-05-12 19:01:56 +00003185 delete $3;
3186 }
Reid Spencer3da59db2006-11-27 01:05:10 +00003187 | CastOps ResolvedVal TO Types {
Reid Spencere1553cc2006-12-31 05:40:12 +00003188 if (!UpRefs.empty())
3189 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00003190 Value* Val = $2;
Reid Spencer93947c32007-01-17 02:47:33 +00003191 const Type* DestTy = $4->get();
3192 if (!CastInst::castIsValid($1, Val, DestTy))
3193 GEN_ERROR("invalid cast opcode for cast from '" +
3194 Val->getType()->getDescription() + "' to '" +
Eric Christopher2a5196f2008-09-24 04:55:49 +00003195 DestTy->getDescription() + "'");
Gabor Greif7cbd8a32008-05-16 19:29:10 +00003196 $$ = CastInst::Create($1, Val, DestTy);
Reid Spencer08de34b2006-12-03 05:45:44 +00003197 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00003198 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00003199 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Dan Gohmanf72fb672008-09-09 01:02:47 +00003200 if (isa<VectorType>($2->getType())) {
3201 // vector select
3202 if (!isa<VectorType>($4->getType())
3203 || !isa<VectorType>($6->getType()) )
3204 GEN_ERROR("vector select value types must be vector types");
3205 const VectorType* cond_type = cast<VectorType>($2->getType());
3206 const VectorType* select_type = cast<VectorType>($4->getType());
3207 if (cond_type->getElementType() != Type::Int1Ty)
3208 GEN_ERROR("vector select condition element type must be boolean");
3209 if (cond_type->getNumElements() != select_type->getNumElements())
3210 GEN_ERROR("vector select number of elements must be the same");
3211 } else {
3212 if ($2->getType() != Type::Int1Ty)
3213 GEN_ERROR("select condition must be boolean");
3214 }
Reid Spencer08de34b2006-12-03 05:45:44 +00003215 if ($4->getType() != $6->getType())
Dan Gohmanf72fb672008-09-09 01:02:47 +00003216 GEN_ERROR("select value types must match");
Gabor Greif051a9502008-04-06 20:25:17 +00003217 $$ = SelectInst::Create($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00003218 CHECK_FOR_ERROR
Chris Lattner76f0ff32004-03-12 05:51:36 +00003219 }
Chris Lattner99e7ab72003-10-18 05:53:13 +00003220 | VAARG ResolvedVal ',' 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 $$ = new VAArgInst($2, *$4);
3224 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00003225 CHECK_FOR_ERROR
Chris Lattner99e7ab72003-10-18 05:53:13 +00003226 }
Robert Bocchino9c62b562006-01-10 19:04:32 +00003227 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencer08de34b2006-12-03 05:45:44 +00003228 if (!ExtractElementInst::isValidOperands($2, $4))
Reid Spencerf4fa5902007-02-05 10:16:10 +00003229 GEN_ERROR("Invalid extractelement operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00003230 $$ = new ExtractElementInst($2, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00003231 CHECK_FOR_ERROR
Robert Bocchino9c62b562006-01-10 19:04:32 +00003232 }
Robert Bocchino2def1b32006-01-17 20:06:25 +00003233 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer08de34b2006-12-03 05:45:44 +00003234 if (!InsertElementInst::isValidOperands($2, $4, $6))
Reid Spencerf4fa5902007-02-05 10:16:10 +00003235 GEN_ERROR("Invalid insertelement operands");
Gabor Greif051a9502008-04-06 20:25:17 +00003236 $$ = InsertElementInst::Create($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00003237 CHECK_FOR_ERROR
Robert Bocchino2def1b32006-01-17 20:06:25 +00003238 }
Chris Lattner4c949082006-04-08 01:18:35 +00003239 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer08de34b2006-12-03 05:45:44 +00003240 if (!ShuffleVectorInst::isValidOperands($2, $4, $6))
Reid Spencerf4fa5902007-02-05 10:16:10 +00003241 GEN_ERROR("Invalid shufflevector operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00003242 $$ = new ShuffleVectorInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00003243 CHECK_FOR_ERROR
Chris Lattner4c949082006-04-08 01:18:35 +00003244 }
Chris Lattner3b237fc2003-10-19 21:34:28 +00003245 | PHI_TOK PHIList {
Chris Lattnerc24d2082001-06-11 15:04:20 +00003246 const Type *Ty = $2->front().first->getType();
Chris Lattner8ea8f362003-10-30 01:38:18 +00003247 if (!Ty->isFirstClassType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00003248 GEN_ERROR("PHI node operands must be of first class type");
Gabor Greif051a9502008-04-06 20:25:17 +00003249 $$ = PHINode::Create(Ty);
Chris Lattnerce1df9e2005-01-29 00:35:55 +00003250 ((PHINode*)$$)->reserveOperandSpace($2->size());
Chris Lattner00950542001-06-06 20:29:01 +00003251 while ($2->begin() != $2->end()) {
Eric Christopher2a5196f2008-09-24 04:55:49 +00003252 if ($2->front().first->getType() != Ty)
Reid Spencerf4fa5902007-02-05 10:16:10 +00003253 GEN_ERROR("All elements of a PHI node must be of the same type");
Chris Lattnerb00c5822001-10-02 03:41:24 +00003254 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00003255 $2->pop_front();
3256 }
3257 delete $2; // Free the list...
Reid Spencer61c83e02006-08-18 08:43:06 +00003258 CHECK_FOR_ERROR
Chris Lattnerddb6db42005-05-06 05:51:46 +00003259 }
Devang Patel652203f2008-09-29 20:49:50 +00003260 | OptTailCall OptCallingConv OptRetAttrs ResultTypes ValueRef '(' ParamList ')'
Reid Spencer2c261782007-01-05 17:06:19 +00003261 OptFuncAttrs {
Reid Spencere1553cc2006-12-31 05:40:12 +00003262
3263 // Handle the short syntax
Bill Wendlingb39a55a2006-11-12 11:10:39 +00003264 const PointerType *PFTy = 0;
3265 const FunctionType *Ty = 0;
Devang Patel652203f2008-09-29 20:49:50 +00003266 if (!(PFTy = dyn_cast<PointerType>($4->get())) ||
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00003267 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00003268 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00003269 std::vector<const Type*> ParamTypes;
Devang Patel652203f2008-09-29 20:49:50 +00003270 ParamList::iterator I = $7->begin(), E = $7->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00003271 for (; I != E; ++I) {
Reid Spencere1553cc2006-12-31 05:40:12 +00003272 const Type *Ty = I->Val->getType();
3273 if (Ty == Type::VoidTy)
3274 GEN_ERROR("Short call syntax cannot be used with varargs");
3275 ParamTypes.push_back(Ty);
Chris Lattneref9c23f2001-10-03 14:53:21 +00003276 }
Chris Lattnerf7dedf22008-04-23 05:36:58 +00003277
Devang Patel652203f2008-09-29 20:49:50 +00003278 if (!FunctionType::isValidReturnType(*$4))
Chris Lattnerf7dedf22008-04-23 05:36:58 +00003279 GEN_ERROR("Invalid result type for LLVM function");
3280
Devang Patel652203f2008-09-29 20:49:50 +00003281 Ty = FunctionType::get($4->get(), ParamTypes, false);
Christopher Lamb43ad6b32007-12-17 01:12:55 +00003282 PFTy = PointerType::getUnqual(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00003283 }
Chris Lattner00950542001-06-06 20:29:01 +00003284
Devang Patel652203f2008-09-29 20:49:50 +00003285 Value *V = getVal(PFTy, $5); // Get the function we're calling...
Reid Spencer5b7e7532006-09-28 19:28:24 +00003286 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00003287
Reid Spencerebff55c2007-04-16 06:55:42 +00003288 // Check for call to invalid intrinsic to avoid crashing later.
3289 if (Function *theF = dyn_cast<Function>(V)) {
Reid Spencerce1e8ef2007-04-16 22:01:57 +00003290 if (theF->hasName() && (theF->getValueName()->getKeyLength() >= 5) &&
Reid Spencer43e60732007-04-16 20:31:06 +00003291 (0 == strncmp(theF->getValueName()->getKeyData(), "llvm.", 5)) &&
3292 !theF->getIntrinsicID(true))
Reid Spencerebff55c2007-04-16 06:55:42 +00003293 GEN_ERROR("Call to invalid LLVM intrinsic function '" +
3294 theF->getName() + "'");
3295 }
3296
Devang Patel05988662008-09-25 21:00:45 +00003297 // Set up the Attributes for the function
3298 SmallVector<AttributeWithIndex, 8> Attrs;
Devang Patel19c87462008-09-26 22:53:05 +00003299 //FIXME : In 3.0, stop accepting zext, sext and inreg as optional function
3300 //attributes.
Devang Patel652203f2008-09-29 20:49:50 +00003301 Attributes RetAttrs = $3;
3302 if ($9 != Attribute::None) {
3303 if ($9 & Attribute::ZExt) {
Devang Patel19c87462008-09-26 22:53:05 +00003304 RetAttrs = RetAttrs | Attribute::ZExt;
Devang Patel652203f2008-09-29 20:49:50 +00003305 $9 = $9 ^ Attribute::ZExt;
Devang Patel19c87462008-09-26 22:53:05 +00003306 }
Devang Patel652203f2008-09-29 20:49:50 +00003307 if ($9 & Attribute::SExt) {
Devang Patel19c87462008-09-26 22:53:05 +00003308 RetAttrs = RetAttrs | Attribute::SExt;
Devang Patel652203f2008-09-29 20:49:50 +00003309 $9 = $9 ^ Attribute::SExt;
Devang Patel19c87462008-09-26 22:53:05 +00003310 }
Devang Patel652203f2008-09-29 20:49:50 +00003311 if ($9 & Attribute::InReg) {
Devang Patel19c87462008-09-26 22:53:05 +00003312 RetAttrs = RetAttrs | Attribute::InReg;
Devang Patel652203f2008-09-29 20:49:50 +00003313 $9 = $9 ^ Attribute::InReg;
Devang Patel19c87462008-09-26 22:53:05 +00003314 }
Devang Patel19c87462008-09-26 22:53:05 +00003315 }
Devang Patel652203f2008-09-29 20:49:50 +00003316 if (RetAttrs != Attribute::None)
3317 Attrs.push_back(AttributeWithIndex::get(0, RetAttrs));
Devang Patel19c87462008-09-26 22:53:05 +00003318
Eric Christopher2a5196f2008-09-24 04:55:49 +00003319 // Check the arguments
Reid Spencere1553cc2006-12-31 05:40:12 +00003320 ValueList Args;
Devang Patel652203f2008-09-29 20:49:50 +00003321 if ($7->empty()) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00003322 // Make sure no arguments is a good thing!
3323 if (Ty->getNumParams() != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00003324 GEN_ERROR("No arguments passed to a function that "
Reid Spencerf4fa5902007-02-05 10:16:10 +00003325 "expects arguments");
Chris Lattner8b81bf52001-07-25 22:47:46 +00003326 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00003327 // Loop through FunctionType's arguments and ensure they are specified
Duncan Sandsdc024672007-11-27 13:23:08 +00003328 // correctly. Also, gather any parameter attributes.
Chris Lattnerd5d89962004-02-09 04:14:01 +00003329 FunctionType::param_iterator I = Ty->param_begin();
3330 FunctionType::param_iterator E = Ty->param_end();
Devang Patel652203f2008-09-29 20:49:50 +00003331 ParamList::iterator ArgI = $7->begin(), ArgE = $7->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00003332 unsigned index = 1;
Reid Spencer863dd882007-04-28 16:06:50 +00003333
Duncan Sandsdc024672007-11-27 13:23:08 +00003334 for (; ArgI != ArgE && I != E; ++ArgI, ++I, ++index) {
Reid Spencere1553cc2006-12-31 05:40:12 +00003335 if (ArgI->Val->getType() != *I)
3336 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00003337 (*I)->getDescription() + "'");
Reid Spencere1553cc2006-12-31 05:40:12 +00003338 Args.push_back(ArgI->Val);
Devang Patel05988662008-09-25 21:00:45 +00003339 if (ArgI->Attrs != Attribute::None)
3340 Attrs.push_back(AttributeWithIndex::get(index, ArgI->Attrs));
Reid Spencere1553cc2006-12-31 05:40:12 +00003341 }
3342 if (Ty->isVarArg()) {
3343 if (I == E)
Duncan Sandseb824702008-01-11 21:23:39 +00003344 for (; ArgI != ArgE; ++ArgI, ++index) {
Reid Spencere1553cc2006-12-31 05:40:12 +00003345 Args.push_back(ArgI->Val); // push the remaining varargs
Devang Patel05988662008-09-25 21:00:45 +00003346 if (ArgI->Attrs != Attribute::None)
3347 Attrs.push_back(AttributeWithIndex::get(index, ArgI->Attrs));
Duncan Sandseb824702008-01-11 21:23:39 +00003348 }
Reid Spencere1553cc2006-12-31 05:40:12 +00003349 } else if (I != E || ArgI != ArgE)
Reid Spencerf4fa5902007-02-05 10:16:10 +00003350 GEN_ERROR("Invalid number of parameters detected");
Chris Lattner8b81bf52001-07-25 22:47:46 +00003351 }
Devang Patel652203f2008-09-29 20:49:50 +00003352 if ($9 != Attribute::None)
3353 Attrs.push_back(AttributeWithIndex::get(~0, $9));
Duncan Sandsdc024672007-11-27 13:23:08 +00003354
Devang Patel05988662008-09-25 21:00:45 +00003355 // Finish off the Attributes and check them
3356 AttrListPtr PAL;
Duncan Sandsdc024672007-11-27 13:23:08 +00003357 if (!Attrs.empty())
Devang Patel05988662008-09-25 21:00:45 +00003358 PAL = AttrListPtr::get(Attrs.begin(), Attrs.end());
Duncan Sandsdc024672007-11-27 13:23:08 +00003359
Reid Spencere1553cc2006-12-31 05:40:12 +00003360 // Create the call node
Gabor Greif051a9502008-04-06 20:25:17 +00003361 CallInst *CI = CallInst::Create(V, Args.begin(), Args.end());
Reid Spencere1553cc2006-12-31 05:40:12 +00003362 CI->setTailCall($1);
3363 CI->setCallingConv($2);
Devang Patel05988662008-09-25 21:00:45 +00003364 CI->setAttributes(PAL);
Reid Spencere1553cc2006-12-31 05:40:12 +00003365 $$ = CI;
Devang Patel652203f2008-09-29 20:49:50 +00003366 delete $7;
3367 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00003368 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00003369 }
3370 | MemoryInst {
3371 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00003372 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00003373 };
Chris Lattner00950542001-06-06 20:29:01 +00003374
Chris Lattner15c9c032003-09-08 18:20:29 +00003375OptVolatile : VOLATILE {
3376 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00003377 CHECK_FOR_ERROR
Chris Lattner15c9c032003-09-08 18:20:29 +00003378 }
3379 | /* empty */ {
3380 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00003381 CHECK_FOR_ERROR
Chris Lattner15c9c032003-09-08 18:20:29 +00003382 };
3383
Chris Lattner027dcc52001-07-08 21:10:27 +00003384
Chris Lattnerddb6db42005-05-06 05:51:46 +00003385
Chris Lattner66db8e42005-11-06 06:34:12 +00003386MemoryInst : MALLOC Types OptCAlign {
Reid Spencere1553cc2006-12-31 05:40:12 +00003387 if (!UpRefs.empty())
3388 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00003389 $$ = new MallocInst(*$2, 0, $3);
3390 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00003391 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00003392 }
Reid Spencera54b7cb2007-01-12 07:05:14 +00003393 | MALLOC Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencere1553cc2006-12-31 05:40:12 +00003394 if (!UpRefs.empty())
3395 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Dan Gohman9c531cd2008-05-23 18:23:11 +00003396 if ($4 != Type::Int32Ty)
3397 GEN_ERROR("Malloc array size is not a 32-bit integer!");
Reid Spencer08de34b2006-12-03 05:45:44 +00003398 Value* tmpVal = getVal($4, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00003399 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00003400 $$ = new MallocInst(*$2, tmpVal, $6);
3401 delete $2;
Nate Begeman14b05292005-11-05 09:21:28 +00003402 }
Chris Lattner66db8e42005-11-06 06:34:12 +00003403 | ALLOCA Types OptCAlign {
Reid Spencere1553cc2006-12-31 05:40:12 +00003404 if (!UpRefs.empty())
3405 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00003406 $$ = new AllocaInst(*$2, 0, $3);
3407 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00003408 CHECK_FOR_ERROR
Nate Begeman14b05292005-11-05 09:21:28 +00003409 }
Reid Spencera54b7cb2007-01-12 07:05:14 +00003410 | ALLOCA Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencere1553cc2006-12-31 05:40:12 +00003411 if (!UpRefs.empty())
3412 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Dan Gohman9c531cd2008-05-23 18:23:11 +00003413 if ($4 != Type::Int32Ty)
3414 GEN_ERROR("Alloca array size is not a 32-bit integer!");
Reid Spencer08de34b2006-12-03 05:45:44 +00003415 Value* tmpVal = getVal($4, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00003416 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00003417 $$ = new AllocaInst(*$2, tmpVal, $6);
3418 delete $2;
Nate Begeman14b05292005-11-05 09:21:28 +00003419 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00003420 | FREE ResolvedVal {
Reid Spencer08de34b2006-12-03 05:45:44 +00003421 if (!isa<PointerType>($2->getType()))
Eric Christopher2a5196f2008-09-24 04:55:49 +00003422 GEN_ERROR("Trying to free nonpointer type " +
Reid Spencerf4fa5902007-02-05 10:16:10 +00003423 $2->getType()->getDescription() + "");
Reid Spencer08de34b2006-12-03 05:45:44 +00003424 $$ = new FreeInst($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00003425 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00003426 }
3427
Christopher Lamb43c7f372007-04-22 19:24:39 +00003428 | OptVolatile LOAD Types ValueRef OptCAlign {
Reid Spencere1553cc2006-12-31 05:40:12 +00003429 if (!UpRefs.empty())
3430 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00003431 if (!isa<PointerType>($3->get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00003432 GEN_ERROR("Can't load from nonpointer type: " +
Reid Spencer08de34b2006-12-03 05:45:44 +00003433 (*$3)->getDescription());
3434 if (!cast<PointerType>($3->get())->getElementType()->isFirstClassType())
Reid Spencer61c83e02006-08-18 08:43:06 +00003435 GEN_ERROR("Can't load from pointer of non-first-class type: " +
Reid Spencer08de34b2006-12-03 05:45:44 +00003436 (*$3)->getDescription());
3437 Value* tmpVal = getVal(*$3, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00003438 CHECK_FOR_ERROR
Christopher Lamb43c7f372007-04-22 19:24:39 +00003439 $$ = new LoadInst(tmpVal, "", $1, $5);
Reid Spencer08de34b2006-12-03 05:45:44 +00003440 delete $3;
Chris Lattner027dcc52001-07-08 21:10:27 +00003441 }
Christopher Lamb43c7f372007-04-22 19:24:39 +00003442 | OptVolatile STORE ResolvedVal ',' Types ValueRef OptCAlign {
Reid Spencere1553cc2006-12-31 05:40:12 +00003443 if (!UpRefs.empty())
3444 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00003445 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner8c8418d2003-09-01 16:31:28 +00003446 if (!PT)
Reid Spencer61c83e02006-08-18 08:43:06 +00003447 GEN_ERROR("Can't store to a nonpointer type: " +
Reid Spencer08de34b2006-12-03 05:45:44 +00003448 (*$5)->getDescription());
Chris Lattner8c8418d2003-09-01 16:31:28 +00003449 const Type *ElTy = PT->getElementType();
Reid Spencer08de34b2006-12-03 05:45:44 +00003450 if (ElTy != $3->getType())
3451 GEN_ERROR("Can't store '" + $3->getType()->getDescription() +
Reid Spencerf4fa5902007-02-05 10:16:10 +00003452 "' into space of type '" + ElTy->getDescription() + "'");
Chris Lattner0383cc42002-08-21 23:51:21 +00003453
Reid Spencer08de34b2006-12-03 05:45:44 +00003454 Value* tmpVal = getVal(*$5, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00003455 CHECK_FOR_ERROR
Christopher Lamb43c7f372007-04-22 19:24:39 +00003456 $$ = new StoreInst($3, tmpVal, $1, $7);
Reid Spencer08de34b2006-12-03 05:45:44 +00003457 delete $5;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00003458 }
Dan Gohmane4977cf2008-05-23 01:55:30 +00003459 | GETRESULT Types ValueRef ',' EUINT64VAL {
Dan Gohmanfc74abf2008-07-23 00:34:11 +00003460 if (!UpRefs.empty())
3461 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
3462 if (!isa<StructType>($2->get()) && !isa<ArrayType>($2->get()))
3463 GEN_ERROR("getresult insn requires an aggregate operand");
3464 if (!ExtractValueInst::getIndexedType(*$2, $5))
3465 GEN_ERROR("Invalid getresult index for type '" +
3466 (*$2)->getDescription()+ "'");
3467
3468 Value *tmpVal = getVal(*$2, $3);
Devang Pateld6ffcf92008-02-19 22:26:37 +00003469 CHECK_FOR_ERROR
Dan Gohmanfc74abf2008-07-23 00:34:11 +00003470 $$ = ExtractValueInst::Create(tmpVal, $5);
3471 delete $2;
Devang Pateld6ffcf92008-02-19 22:26:37 +00003472 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00003473 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencere1553cc2006-12-31 05:40:12 +00003474 if (!UpRefs.empty())
3475 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00003476 if (!isa<PointerType>($2->get()))
Reid Spencerf4fa5902007-02-05 10:16:10 +00003477 GEN_ERROR("getelementptr insn requires pointer operand");
Chris Lattner830b6f92004-04-05 01:30:04 +00003478
Dan Gohman041e2eb2008-05-15 19:50:34 +00003479 if (!GetElementPtrInst::getIndexedType(*$2, $4->begin(), $4->end()))
Reid Spencer61c83e02006-08-18 08:43:06 +00003480 GEN_ERROR("Invalid getelementptr indices for type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00003481 (*$2)->getDescription()+ "'");
Reid Spencer08de34b2006-12-03 05:45:44 +00003482 Value* tmpVal = getVal(*$2, $3);
Reid Spencer61c83e02006-08-18 08:43:06 +00003483 CHECK_FOR_ERROR
Gabor Greif051a9502008-04-06 20:25:17 +00003484 $$ = GetElementPtrInst::Create(tmpVal, $4->begin(), $4->end());
Eric Christopher2a5196f2008-09-24 04:55:49 +00003485 delete $2;
Reid Spencer5b7e7532006-09-28 19:28:24 +00003486 delete $4;
Dan Gohmane4977cf2008-05-23 01:55:30 +00003487 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +00003488 | EXTRACTVALUE Types ValueRef ConstantIndexList {
Dan Gohmane4977cf2008-05-23 01:55:30 +00003489 if (!UpRefs.empty())
3490 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
3491 if (!isa<StructType>($2->get()) && !isa<ArrayType>($2->get()))
3492 GEN_ERROR("extractvalue insn requires an aggregate operand");
3493
3494 if (!ExtractValueInst::getIndexedType(*$2, $4->begin(), $4->end()))
3495 GEN_ERROR("Invalid extractvalue indices for type '" +
3496 (*$2)->getDescription()+ "'");
3497 Value* tmpVal = getVal(*$2, $3);
3498 CHECK_FOR_ERROR
3499 $$ = ExtractValueInst::Create(tmpVal, $4->begin(), $4->end());
Eric Christopher2a5196f2008-09-24 04:55:49 +00003500 delete $2;
Dan Gohmane4977cf2008-05-23 01:55:30 +00003501 delete $4;
3502 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +00003503 | INSERTVALUE Types ValueRef ',' Types ValueRef ConstantIndexList {
Dan Gohmane4977cf2008-05-23 01:55:30 +00003504 if (!UpRefs.empty())
3505 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
3506 if (!isa<StructType>($2->get()) && !isa<ArrayType>($2->get()))
3507 GEN_ERROR("extractvalue insn requires an aggregate operand");
3508
3509 if (ExtractValueInst::getIndexedType(*$2, $7->begin(), $7->end()) != $5->get())
3510 GEN_ERROR("Invalid insertvalue indices for type '" +
3511 (*$2)->getDescription()+ "'");
3512 Value* aggVal = getVal(*$2, $3);
3513 Value* tmpVal = getVal(*$5, $6);
3514 CHECK_FOR_ERROR
3515 $$ = InsertValueInst::Create(aggVal, tmpVal, $7->begin(), $7->end());
Eric Christopher2a5196f2008-09-24 04:55:49 +00003516 delete $2;
Dan Gohmane4977cf2008-05-23 01:55:30 +00003517 delete $5;
3518 delete $7;
Chris Lattner51727be2002-06-04 21:58:56 +00003519 };
Chris Lattner027dcc52001-07-08 21:10:27 +00003520
Brian Gaeked0fde302003-11-11 22:41:34 +00003521
Chris Lattner00950542001-06-06 20:29:01 +00003522%%
Reid Spencer61c83e02006-08-18 08:43:06 +00003523
Reid Spencere1553cc2006-12-31 05:40:12 +00003524// common code from the two 'RunVMAsmParser' functions
3525static Module* RunParser(Module * M) {
Reid Spencere1553cc2006-12-31 05:40:12 +00003526 CurModule.CurrentModule = M;
Reid Spencere1553cc2006-12-31 05:40:12 +00003527 // Check to make sure the parser succeeded
3528 if (yyparse()) {
3529 if (ParserResult)
3530 delete ParserResult;
3531 return 0;
3532 }
3533
Reid Spencercd5bd902007-03-30 01:37:13 +00003534 // Emit an error if there are any unresolved types left.
3535 if (!CurModule.LateResolveTypes.empty()) {
3536 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
3537 if (DID.Type == ValID::LocalName) {
3538 GenerateError("Undefined type remains at eof: '"+DID.getName() + "'");
3539 } else {
3540 GenerateError("Undefined type remains at eof: #" + itostr(DID.Num));
3541 }
3542 if (ParserResult)
3543 delete ParserResult;
3544 return 0;
3545 }
3546
3547 // Emit an error if there are any unresolved values left.
3548 if (!CurModule.LateResolveValues.empty()) {
3549 Value *V = CurModule.LateResolveValues.back();
3550 std::map<Value*, std::pair<ValID, int> >::iterator I =
3551 CurModule.PlaceHolderInfo.find(V);
3552
3553 if (I != CurModule.PlaceHolderInfo.end()) {
3554 ValID &DID = I->second.first;
3555 if (DID.Type == ValID::LocalName) {
3556 GenerateError("Undefined value remains at eof: "+DID.getName() + "'");
3557 } else {
3558 GenerateError("Undefined value remains at eof: #" + itostr(DID.Num));
3559 }
3560 if (ParserResult)
3561 delete ParserResult;
3562 return 0;
3563 }
3564 }
3565
Reid Spencere1553cc2006-12-31 05:40:12 +00003566 // Check to make sure that parsing produced a result
3567 if (!ParserResult)
3568 return 0;
3569
3570 // Reset ParserResult variable while saving its value for the result.
3571 Module *Result = ParserResult;
3572 ParserResult = 0;
3573
3574 return Result;
3575}
3576
Reid Spencer61c83e02006-08-18 08:43:06 +00003577void llvm::GenerateError(const std::string &message, int LineNo) {
Chris Lattner8e3a8e02007-11-18 08:46:26 +00003578 if (LineNo == -1) LineNo = LLLgetLineNo();
Reid Spencer61c83e02006-08-18 08:43:06 +00003579 // TODO: column number in exception
3580 if (TheParseError)
Chris Lattner8e3a8e02007-11-18 08:46:26 +00003581 TheParseError->setError(LLLgetFilename(), message, LineNo);
Reid Spencer61c83e02006-08-18 08:43:06 +00003582 TriggerError = 1;
3583}
3584
Chris Lattner09083092001-07-08 04:57:15 +00003585int yyerror(const char *ErrorMsg) {
Chris Lattner8e3a8e02007-11-18 08:46:26 +00003586 std::string where = LLLgetFilename() + ":" + utostr(LLLgetLineNo()) + ": ";
Reid Spenceref9b9a72007-02-05 20:47:22 +00003587 std::string errMsg = where + "error: " + std::string(ErrorMsg);
Chris Lattner8e3a8e02007-11-18 08:46:26 +00003588 if (yychar != YYEMPTY && yychar != 0) {
3589 errMsg += " while reading token: '";
Eric Christopher2a5196f2008-09-24 04:55:49 +00003590 errMsg += std::string(LLLgetTokenStart(),
Chris Lattner8e3a8e02007-11-18 08:46:26 +00003591 LLLgetTokenStart()+LLLgetTokenLength()) + "'";
3592 }
Reid Spencer61c83e02006-08-18 08:43:06 +00003593 GenerateError(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00003594 return 0;
3595}