blob: 35dad259f6b01ef02703216a3fd5de1ae251be64 [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
36// 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
39// 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
42// immediately invokes YYERROR. This would be so much cleaner if it was a
43// 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
Misha Brukman5a2a3822005-05-10 22:02:28 +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
154 // we don't need to traverse that leg of the type.
155 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) {
172 std::vector<const Type*>::iterator I = SeenList.begin(),
173 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) {
184 std::vector<const Type*>::iterator I = SeenList.begin(),
185 E = SeenList.end();
186 for ( ; I != E; ++I)
187 if (*I == TheTy)
188 break;
189 if (I == E)
190 WorkList.push_back(TheTy);
191 }
192 }
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
Reid Spencer186a43f2007-03-19 18:39:36 +0000252static void InsertValue(Value *V, ValueList &ValueTab = CurFun.Values) {
253 // Things that have names or are void typed don't get slot numbers
254 if (V->hasName() || (V->getType() == Type::VoidTy))
255 return;
Chris Lattner2079fde2001-10-13 06:41:08 +0000256
Reid Spencer186a43f2007-03-19 18:39:36 +0000257 // In the case of function values, we have to allow for the forward reference
258 // of basic blocks, which are included in the numbering. Consequently, we keep
259 // track of the next insertion location with NextValNum. When a BB gets
260 // inserted, it could change the size of the CurFun.Values vector.
261 if (&ValueTab == &CurFun.Values) {
262 if (ValueTab.size() <= CurFun.NextValNum)
263 ValueTab.resize(CurFun.NextValNum+1);
264 ValueTab[CurFun.NextValNum++] = V;
265 return;
266 }
267 // For all other lists, its okay to just tack it on the back of the vector.
268 ValueTab.push_back(V);
Chris Lattner00950542001-06-06 20:29:01 +0000269}
270
Chris Lattner30c89792001-09-07 16:35:17 +0000271static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000272 switch (D.Type) {
Reid Spencerb2d17862007-01-26 08:04:51 +0000273 case ValID::LocalID: // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000274 // Module constants occupy the lowest numbered slots...
Reid Spencerb2d17862007-01-26 08:04:51 +0000275 if (D.Num < CurModule.Types.size())
276 return CurModule.Types[D.Num];
Chris Lattner42c9e772001-10-20 09:32:59 +0000277 break;
Reid Spencerb2d17862007-01-26 08:04:51 +0000278 case ValID::LocalName: // Is it a named definition?
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000279 if (const Type *N = CurModule.CurrentModule->getTypeByName(D.getName())) {
Chris Lattnera09000d2004-07-14 23:03:46 +0000280 D.destroy(); // Free old strdup'd memory...
281 return N;
Chris Lattner6e6026b2002-11-20 18:36:02 +0000282 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000283 break;
Chris Lattner30c89792001-09-07 16:35:17 +0000284 default:
Reid Spencerf4fa5902007-02-05 10:16:10 +0000285 GenerateError("Internal parser error: Invalid symbol type reference");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000286 return 0;
Chris Lattner30c89792001-09-07 16:35:17 +0000287 }
288
289 // If we reached here, we referenced either a symbol that we don't know about
290 // or an id number that hasn't been read yet. We may be referencing something
291 // forward, so just create an entry to be resolved later and get to it...
292 //
293 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
294
Chris Lattner355ad1f2005-03-21 06:27:42 +0000295
296 if (inFunctionScope()) {
Reid Spencerb2d17862007-01-26 08:04:51 +0000297 if (D.Type == ValID::LocalName) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000298 GenerateError("Reference to an undefined type: '" + D.getName() + "'");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000299 return 0;
300 } else {
Reid Spencerb2d17862007-01-26 08:04:51 +0000301 GenerateError("Reference to an undefined type: #" + utostr(D.Num));
Reid Spencer5b7e7532006-09-28 19:28:24 +0000302 return 0;
303 }
Chris Lattner4a42e902001-10-22 05:56:09 +0000304 }
Chris Lattner30c89792001-09-07 16:35:17 +0000305
Reid Spencerb78b9082006-11-28 07:28:14 +0000306 std::map<ValID, PATypeHolder>::iterator I =CurModule.LateResolveTypes.find(D);
Chris Lattner355ad1f2005-03-21 06:27:42 +0000307 if (I != CurModule.LateResolveTypes.end())
Reid Spencerb78b9082006-11-28 07:28:14 +0000308 return I->second;
Chris Lattner355ad1f2005-03-21 06:27:42 +0000309
Reid Spencerb78b9082006-11-28 07:28:14 +0000310 Type *Typ = OpaqueType::get();
311 CurModule.LateResolveTypes.insert(std::make_pair(D, Typ));
312 return Typ;
Reid Spencer08de34b2006-12-03 05:45:44 +0000313 }
Chris Lattner30c89792001-09-07 16:35:17 +0000314
Reid Spencer186a43f2007-03-19 18:39:36 +0000315// getExistingVal - Look up the value specified by the provided type and
Chris Lattner2079fde2001-10-13 06:41:08 +0000316// the provided ValID. If the value exists and has already been defined, return
317// it. Otherwise return null.
318//
Reid Spencer186a43f2007-03-19 18:39:36 +0000319static Value *getExistingVal(const Type *Ty, const ValID &D) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000320 if (isa<FunctionType>(Ty)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000321 GenerateError("Functions are not values and "
Chris Lattner79df7c02002-03-26 18:01:55 +0000322 "must be referenced as pointers");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000323 return 0;
324 }
Chris Lattner386a3b72001-10-16 19:54:17 +0000325
Chris Lattner30c89792001-09-07 16:35:17 +0000326 switch (D.Type) {
Reid Spencerb2d17862007-01-26 08:04:51 +0000327 case ValID::LocalID: { // Is it a numbered definition?
Reid Spencerb2d17862007-01-26 08:04:51 +0000328 // Check that the number is within bounds.
Reid Spencer186a43f2007-03-19 18:39:36 +0000329 if (D.Num >= CurFun.Values.size())
330 return 0;
331 Value *Result = CurFun.Values[D.Num];
332 if (Ty != Result->getType()) {
333 GenerateError("Numbered value (%" + utostr(D.Num) + ") of type '" +
334 Result->getType()->getDescription() + "' does not match "
335 "expected type, '" + Ty->getDescription() + "'");
336 return 0;
337 }
338 return Result;
Reid Spencerb2d17862007-01-26 08:04:51 +0000339 }
340 case ValID::GlobalID: { // Is it a numbered definition?
Reid Spencer186a43f2007-03-19 18:39:36 +0000341 if (D.Num >= CurModule.Values.size())
Reid Spenceref9b9a72007-02-05 20:47:22 +0000342 return 0;
Reid Spencer186a43f2007-03-19 18:39:36 +0000343 Value *Result = CurModule.Values[D.Num];
344 if (Ty != Result->getType()) {
345 GenerateError("Numbered value (@" + utostr(D.Num) + ") of type '" +
346 Result->getType()->getDescription() + "' does not match "
347 "expected type, '" + Ty->getDescription() + "'");
Reid Spenceref9b9a72007-02-05 20:47:22 +0000348 return 0;
Reid Spencer186a43f2007-03-19 18:39:36 +0000349 }
350 return Result;
Chris Lattner00950542001-06-06 20:29:01 +0000351 }
Reid Spencerb2d17862007-01-26 08:04:51 +0000352
353 case ValID::LocalName: { // Is it a named definition?
Reid Spenceref9b9a72007-02-05 20:47:22 +0000354 if (!inFunctionScope())
355 return 0;
356 ValueSymbolTable &SymTab = CurFun.CurrentFunction->getValueSymbolTable();
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000357 Value *N = SymTab.lookup(D.getName());
Reid Spenceref9b9a72007-02-05 20:47:22 +0000358 if (N == 0)
359 return 0;
360 if (N->getType() != Ty)
361 return 0;
Reid Spencerb2d17862007-01-26 08:04:51 +0000362
363 D.destroy(); // Free old strdup'd memory...
364 return N;
365 }
366 case ValID::GlobalName: { // Is it a named definition?
Reid Spenceref9b9a72007-02-05 20:47:22 +0000367 ValueSymbolTable &SymTab = CurModule.CurrentModule->getValueSymbolTable();
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000368 Value *N = SymTab.lookup(D.getName());
Reid Spenceref9b9a72007-02-05 20:47:22 +0000369 if (N == 0)
370 return 0;
371 if (N->getType() != Ty)
372 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000373
374 D.destroy(); // Free old strdup'd memory...
375 return N;
376 }
377
Misha Brukman5a2a3822005-05-10 22:02:28 +0000378 // Check to make sure that "Ty" is an integral type, and that our
Chris Lattner2079fde2001-10-13 06:41:08 +0000379 // value will fit into the specified type...
380 case ValID::ConstSIntVal: // Is it a constant pool reference??
Chris Lattner689e8b22008-02-19 04:36:07 +0000381 if (!isa<IntegerType>(Ty) ||
382 !ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000383 GenerateError("Signed integral constant '" +
Misha Brukman5a2a3822005-05-10 22:02:28 +0000384 itostr(D.ConstPool64) + "' is invalid for type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +0000385 Ty->getDescription() + "'");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000386 return 0;
387 }
Reid Spencereac65742007-03-19 20:40:22 +0000388 return ConstantInt::get(Ty, D.ConstPool64, true);
Chris Lattner2079fde2001-10-13 06:41:08 +0000389
390 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattner689e8b22008-02-19 04:36:07 +0000391 if (isa<IntegerType>(Ty) &&
392 ConstantInt::isValueValidForType(Ty, D.UConstPool64))
Reid Spencerb83eb642006-10-20 07:07:24 +0000393 return ConstantInt::get(Ty, D.UConstPool64);
Chris Lattner689e8b22008-02-19 04:36:07 +0000394
395 if (!isa<IntegerType>(Ty) ||
396 !ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
397 GenerateError("Integral constant '" + utostr(D.UConstPool64) +
398 "' is invalid or out of range for type '" +
399 Ty->getDescription() + "'");
400 return 0;
Chris Lattner2079fde2001-10-13 06:41:08 +0000401 }
Chris Lattner689e8b22008-02-19 04:36:07 +0000402 // This is really a signed reference. Transmogrify.
403 return ConstantInt::get(Ty, D.ConstPool64, true);
Chris Lattner2079fde2001-10-13 06:41:08 +0000404
Chris Lattner2079fde2001-10-13 06:41:08 +0000405 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattner689e8b22008-02-19 04:36:07 +0000406 if (!Ty->isFloatingPoint() ||
407 !ConstantFP::isValueValidForType(Ty, *D.ConstPoolFP)) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000408 GenerateError("FP constant invalid for type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000409 return 0;
410 }
Chris Lattner02a260a2008-04-20 00:41:09 +0000411 // Lexer has no type info, so builds all float and double FP constants
Dale Johannesen3f6eb742007-09-11 18:32:33 +0000412 // as double. Fix this here. Long double does not need this.
413 if (&D.ConstPoolFP->getSemantics() == &APFloat::IEEEdouble &&
414 Ty==Type::FloatTy)
Dale Johannesen43421b32007-09-06 18:13:44 +0000415 D.ConstPoolFP->convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven);
Chris Lattner02a260a2008-04-20 00:41:09 +0000416 return ConstantFP::get(*D.ConstPoolFP);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000417
Chris Lattner2079fde2001-10-13 06:41:08 +0000418 case ValID::ConstNullVal: // Is it a null value?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000419 if (!isa<PointerType>(Ty)) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000420 GenerateError("Cannot create a a non pointer null");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000421 return 0;
422 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000423 return ConstantPointerNull::get(cast<PointerType>(Ty));
Misha Brukman5a2a3822005-05-10 22:02:28 +0000424
Chris Lattner16710e92004-10-16 18:17:13 +0000425 case ValID::ConstUndefVal: // Is it an undef value?
426 return UndefValue::get(Ty);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000427
Chris Lattnerf1f03df2005-12-21 17:53:02 +0000428 case ValID::ConstZeroVal: // Is it a zero value?
429 return Constant::getNullValue(Ty);
430
Chris Lattnerd78700d2002-08-16 21:14:40 +0000431 case ValID::ConstantVal: // Fully resolved constant?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000432 if (D.ConstantValue->getType() != Ty) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000433 GenerateError("Constant expression type different from required type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000434 return 0;
435 }
Chris Lattnerd78700d2002-08-16 21:14:40 +0000436 return D.ConstantValue;
437
Chris Lattneraa2c8532006-01-25 22:26:43 +0000438 case ValID::InlineAsmVal: { // Inline asm expression
439 const PointerType *PTy = dyn_cast<PointerType>(Ty);
440 const FunctionType *FTy =
441 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
Reid Spencer5b7e7532006-09-28 19:28:24 +0000442 if (!FTy || !InlineAsm::Verify(FTy, D.IAD->Constraints)) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000443 GenerateError("Invalid type for asm constraint string");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000444 return 0;
445 }
Chris Lattneraa2c8532006-01-25 22:26:43 +0000446 InlineAsm *IA = InlineAsm::get(FTy, D.IAD->AsmString, D.IAD->Constraints,
447 D.IAD->HasSideEffects);
448 D.destroy(); // Free InlineAsmDescriptor.
449 return IA;
450 }
Chris Lattner30c89792001-09-07 16:35:17 +0000451 default:
Reid Spencer1755a9a2007-02-05 17:01:20 +0000452 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000453 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000454 } // End of switch
455
Reid Spencer1755a9a2007-02-05 17:01:20 +0000456 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000457 return 0;
458}
459
Reid Spencer186a43f2007-03-19 18:39:36 +0000460// getVal - This function is identical to getExistingVal, except that if a
Chris Lattner2079fde2001-10-13 06:41:08 +0000461// value is not already defined, it "improvises" by creating a placeholder var
462// that looks and acts just like the requested variable. When the value is
463// defined later, all uses of the placeholder variable are replaced with the
464// real thing.
465//
Chris Lattner64116f92004-07-14 01:33:11 +0000466static Value *getVal(const Type *Ty, const ValID &ID) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000467 if (Ty == Type::LabelTy) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000468 GenerateError("Cannot use a basic block here");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000469 return 0;
470 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000471
Chris Lattner64116f92004-07-14 01:33:11 +0000472 // See if the value has already been defined.
Reid Spencer186a43f2007-03-19 18:39:36 +0000473 Value *V = getExistingVal(Ty, ID);
Chris Lattner2079fde2001-10-13 06:41:08 +0000474 if (V) return V;
Reid Spencer5b7e7532006-09-28 19:28:24 +0000475 if (TriggerError) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000476
Reid Spencer5b7e7532006-09-28 19:28:24 +0000477 if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty)) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000478 GenerateError("Invalid use of a composite type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000479 return 0;
480 }
Chris Lattner60cd9552005-03-23 01:29:26 +0000481
Chris Lattner00950542001-06-06 20:29:01 +0000482 // If we reached here, we referenced either a symbol that we don't know about
483 // or an id number that hasn't been read yet. We may be referencing something
484 // forward, so just create an entry to be resolved later and get to it...
485 //
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000486 switch (ID.Type) {
487 case ValID::GlobalName:
Reid Spencer3cb4dda2007-04-28 14:13:42 +0000488 case ValID::GlobalID: {
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000489 const PointerType *PTy = dyn_cast<PointerType>(Ty);
490 if (!PTy) {
491 GenerateError("Invalid type for reference to global" );
492 return 0;
493 }
494 const Type* ElTy = PTy->getElementType();
495 if (const FunctionType *FTy = dyn_cast<FunctionType>(ElTy))
Gabor Greif051a9502008-04-06 20:25:17 +0000496 V = Function::Create(FTy, GlobalValue::ExternalLinkage);
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000497 else
Christopher Lambfe63fb92007-12-11 08:59:05 +0000498 V = new GlobalVariable(ElTy, false, GlobalValue::ExternalLinkage, 0, "",
499 (Module*)0, false, PTy->getAddressSpace());
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000500 break;
Reid Spencer3cb4dda2007-04-28 14:13:42 +0000501 }
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000502 default:
503 V = new Argument(Ty);
504 }
505
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000506 // Remember where this forward reference came from. FIXME, shouldn't we try
507 // to recycle these things??
Chris Lattner64116f92004-07-14 01:33:11 +0000508 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000509 LLLgetLineNo())));
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000510
Chris Lattner79df7c02002-03-26 18:01:55 +0000511 if (inFunctionScope())
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000512 InsertValue(V, CurFun.LateResolveValues);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000513 else
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000514 InsertValue(V, CurModule.LateResolveValues);
515 return V;
Chris Lattner00950542001-06-06 20:29:01 +0000516}
517
Reid Spencer186a43f2007-03-19 18:39:36 +0000518/// defineBBVal - This is a definition of a new basic block with the specified
519/// identifier which must be the same as CurFun.NextValNum, if its numeric.
Nick Lewyckyfc82fab2008-03-02 02:48:09 +0000520static BasicBlock *defineBBVal(const ValID &ID, BasicBlock *unwindDest) {
Reid Spencer1755a9a2007-02-05 17:01:20 +0000521 assert(inFunctionScope() && "Can't get basic block at global scope!");
Chris Lattner64116f92004-07-14 01:33:11 +0000522
Chris Lattner2e2ed292004-07-14 06:28:35 +0000523 BasicBlock *BB = 0;
Chris Lattner64116f92004-07-14 01:33:11 +0000524
Reid Spencer186a43f2007-03-19 18:39:36 +0000525 // First, see if this was forward referenced
Chris Lattner64116f92004-07-14 01:33:11 +0000526
Reid Spencer186a43f2007-03-19 18:39:36 +0000527 std::map<ValID, BasicBlock*>::iterator BBI = CurFun.BBForwardRefs.find(ID);
528 if (BBI != CurFun.BBForwardRefs.end()) {
529 BB = BBI->second;
Chris Lattner2e2ed292004-07-14 06:28:35 +0000530 // The forward declaration could have been inserted anywhere in the
531 // function: insert it into the correct place now.
532 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
533 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
Reid Spencer186a43f2007-03-19 18:39:36 +0000534
Reid Spencer2c9df212007-03-20 01:13:00 +0000535 // We're about to erase the entry, save the key so we can clean it up.
536 ValID Tmp = BBI->first;
537
Reid Spencer186a43f2007-03-19 18:39:36 +0000538 // Erase the forward ref from the map as its no longer "forward"
539 CurFun.BBForwardRefs.erase(ID);
540
Reid Spencer2c9df212007-03-20 01:13:00 +0000541 // The key has been removed from the map but so we don't want to leave
542 // strdup'd memory around so destroy it too.
543 Tmp.destroy();
544
Reid Spencer186a43f2007-03-19 18:39:36 +0000545 // If its a numbered definition, bump the number and set the BB value.
546 if (ID.Type == ValID::LocalID) {
547 assert(ID.Num == CurFun.NextValNum && "Invalid new block number");
548 InsertValue(BB);
549 }
Nick Lewyckyfc82fab2008-03-02 02:48:09 +0000550 } else {
551 // We haven't seen this BB before and its first mention is a definition.
552 // Just create it and return it.
553 std::string Name (ID.Type == ValID::LocalName ? ID.getName() : "");
Gabor Greif051a9502008-04-06 20:25:17 +0000554 BB = BasicBlock::Create(Name, CurFun.CurrentFunction);
Nick Lewyckyfc82fab2008-03-02 02:48:09 +0000555 if (ID.Type == ValID::LocalID) {
556 assert(ID.Num == CurFun.NextValNum && "Invalid new block number");
557 InsertValue(BB);
558 }
Chris Lattner2e2ed292004-07-14 06:28:35 +0000559 }
Reid Spencer186a43f2007-03-19 18:39:36 +0000560
Nick Lewyckyfc82fab2008-03-02 02:48:09 +0000561 ID.destroy();
562 BB->setUnwindDest(unwindDest);
Reid Spencer186a43f2007-03-19 18:39:36 +0000563 return BB;
564}
565
566/// getBBVal - get an existing BB value or create a forward reference for it.
567///
568static BasicBlock *getBBVal(const ValID &ID) {
569 assert(inFunctionScope() && "Can't get basic block at global scope!");
570
571 BasicBlock *BB = 0;
572
573 std::map<ValID, BasicBlock*>::iterator BBI = CurFun.BBForwardRefs.find(ID);
574 if (BBI != CurFun.BBForwardRefs.end()) {
575 BB = BBI->second;
576 } if (ID.Type == ValID::LocalName) {
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000577 std::string Name = ID.getName();
Reid Spencer186a43f2007-03-19 18:39:36 +0000578 Value *N = CurFun.CurrentFunction->getValueSymbolTable().lookup(Name);
Anton Korobeynikov4aefd6b2008-02-20 12:07:57 +0000579 if (N) {
Reid Spencer186a43f2007-03-19 18:39:36 +0000580 if (N->getType()->getTypeID() == Type::LabelTyID)
581 BB = cast<BasicBlock>(N);
582 else
583 GenerateError("Reference to label '" + Name + "' is actually of type '"+
584 N->getType()->getDescription() + "'");
Anton Korobeynikov4aefd6b2008-02-20 12:07:57 +0000585 }
Reid Spencer186a43f2007-03-19 18:39:36 +0000586 } else if (ID.Type == ValID::LocalID) {
587 if (ID.Num < CurFun.NextValNum && ID.Num < CurFun.Values.size()) {
588 if (CurFun.Values[ID.Num]->getType()->getTypeID() == Type::LabelTyID)
589 BB = cast<BasicBlock>(CurFun.Values[ID.Num]);
590 else
591 GenerateError("Reference to label '%" + utostr(ID.Num) +
592 "' is actually of type '"+
593 CurFun.Values[ID.Num]->getType()->getDescription() + "'");
594 }
595 } else {
596 GenerateError("Illegal label reference " + ID.getName());
597 return 0;
598 }
599
600 // If its already been defined, return it now.
601 if (BB) {
602 ID.destroy(); // Free strdup'd memory.
603 return BB;
604 }
605
606 // Otherwise, this block has not been seen before, create it.
607 std::string Name;
608 if (ID.Type == ValID::LocalName)
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000609 Name = ID.getName();
Gabor Greif051a9502008-04-06 20:25:17 +0000610 BB = BasicBlock::Create(Name, CurFun.CurrentFunction);
Reid Spencer186a43f2007-03-19 18:39:36 +0000611
612 // Insert it in the forward refs map.
613 CurFun.BBForwardRefs[ID] = BB;
614
Chris Lattner64116f92004-07-14 01:33:11 +0000615 return BB;
616}
617
Chris Lattner00950542001-06-06 20:29:01 +0000618
619//===----------------------------------------------------------------------===//
620// Code to handle forward references in instructions
621//===----------------------------------------------------------------------===//
622//
623// This code handles the late binding needed with statements that reference
624// values not defined yet... for example, a forward branch, or the PHI node for
625// a loop body.
626//
Chris Lattner394f2eb2003-10-16 20:12:13 +0000627// This keeps a table (CurFun.LateResolveValues) of all such forward references
Chris Lattner00950542001-06-06 20:29:01 +0000628// and back patchs after we are done.
629//
630
Misha Brukman5a2a3822005-05-10 22:02:28 +0000631// ResolveDefinitions - If we could not resolve some defs at parsing
632// time (forward branches, phi functions for loops, etc...) resolve the
Chris Lattner00950542001-06-06 20:29:01 +0000633// defs now...
634//
Misha Brukman5a2a3822005-05-10 22:02:28 +0000635static void
Reid Spencer186a43f2007-03-19 18:39:36 +0000636ResolveDefinitions(ValueList &LateResolvers, ValueList *FutureLateResolvers) {
Chris Lattner00950542001-06-06 20:29:01 +0000637 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Reid Spencer186a43f2007-03-19 18:39:36 +0000638 while (!LateResolvers.empty()) {
639 Value *V = LateResolvers.back();
640 LateResolvers.pop_back();
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000641
Reid Spencer186a43f2007-03-19 18:39:36 +0000642 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
643 CurModule.PlaceHolderInfo.find(V);
644 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000645
Reid Spencer186a43f2007-03-19 18:39:36 +0000646 ValID &DID = PHI->second.first;
Chris Lattner00950542001-06-06 20:29:01 +0000647
Reid Spencer186a43f2007-03-19 18:39:36 +0000648 Value *TheRealValue = getExistingVal(V->getType(), DID);
649 if (TriggerError)
650 return;
651 if (TheRealValue) {
652 V->replaceAllUsesWith(TheRealValue);
653 delete V;
654 CurModule.PlaceHolderInfo.erase(PHI);
655 } else if (FutureLateResolvers) {
656 // Functions have their unresolved items forwarded to the module late
657 // resolver table
658 InsertValue(V, *FutureLateResolvers);
659 } else {
660 if (DID.Type == ValID::LocalName || DID.Type == ValID::GlobalName) {
661 GenerateError("Reference to an invalid definition: '" +DID.getName()+
662 "' of type '" + V->getType()->getDescription() + "'",
663 PHI->second.second);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000664 return;
Chris Lattner386a3b72001-10-16 19:54:17 +0000665 } else {
Reid Spencer186a43f2007-03-19 18:39:36 +0000666 GenerateError("Reference to an invalid definition: #" +
667 itostr(DID.Num) + " of type '" +
668 V->getType()->getDescription() + "'",
669 PHI->second.second);
670 return;
Chris Lattner30c89792001-09-07 16:35:17 +0000671 }
Chris Lattner00950542001-06-06 20:29:01 +0000672 }
673 }
Chris Lattner00950542001-06-06 20:29:01 +0000674 LateResolvers.clear();
675}
676
Chris Lattner4a42e902001-10-22 05:56:09 +0000677// ResolveTypeTo - A brand new type was just declared. This means that (if
678// name is not null) things referencing Name can be resolved. Otherwise, things
679// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000680//
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000681static void ResolveTypeTo(std::string *Name, const Type *ToTy) {
Chris Lattner355ad1f2005-03-21 06:27:42 +0000682 ValID D;
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000683 if (Name)
684 D = ValID::createLocalName(*Name);
685 else
686 D = ValID::createLocalID(CurModule.Types.size());
Chris Lattner00950542001-06-06 20:29:01 +0000687
Reid Spencerb78b9082006-11-28 07:28:14 +0000688 std::map<ValID, PATypeHolder>::iterator I =
Chris Lattner355ad1f2005-03-21 06:27:42 +0000689 CurModule.LateResolveTypes.find(D);
690 if (I != CurModule.LateResolveTypes.end()) {
Reid Spencerb78b9082006-11-28 07:28:14 +0000691 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattner355ad1f2005-03-21 06:27:42 +0000692 CurModule.LateResolveTypes.erase(I);
Chris Lattner30c89792001-09-07 16:35:17 +0000693 }
694}
695
Chris Lattner1781aca2001-09-18 04:00:54 +0000696// setValueName - Set the specified value to the name given. The name may be
697// null potentially, in which case this is a noop. The string passed in is
Chris Lattner8ab406d2004-07-13 07:59:27 +0000698// assumed to be a malloc'd string buffer, and is free'd by this function.
699//
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000700static void setValueName(Value *V, std::string *NameStr) {
Reid Spencerb2d17862007-01-26 08:04:51 +0000701 if (!NameStr) return;
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000702 std::string Name(*NameStr); // Copy string
703 delete NameStr; // Free old string
Chris Lattnerc9aea522004-07-13 08:12:39 +0000704
Reid Spencerb2d17862007-01-26 08:04:51 +0000705 if (V->getType() == Type::VoidTy) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000706 GenerateError("Can't assign name '" + Name+"' to value with void type");
Reid Spencerb2d17862007-01-26 08:04:51 +0000707 return;
Chris Lattner8ab406d2004-07-13 07:59:27 +0000708 }
Reid Spencerb2d17862007-01-26 08:04:51 +0000709
Reid Spencer1755a9a2007-02-05 17:01:20 +0000710 assert(inFunctionScope() && "Must be in function scope!");
Reid Spenceref9b9a72007-02-05 20:47:22 +0000711 ValueSymbolTable &ST = CurFun.CurrentFunction->getValueSymbolTable();
712 if (ST.lookup(Name)) {
Reid Spencerb2d17862007-01-26 08:04:51 +0000713 GenerateError("Redefinition of value '" + Name + "' of type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +0000714 V->getType()->getDescription() + "'");
Reid Spencerb2d17862007-01-26 08:04:51 +0000715 return;
716 }
717
718 // Set the name.
719 V->setName(Name);
Chris Lattner8ab406d2004-07-13 07:59:27 +0000720}
721
Chris Lattnerd88998d2004-07-14 08:23:52 +0000722/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
723/// this is a declaration, otherwise it is a definition.
Chris Lattnerb2b96672005-11-12 18:21:21 +0000724static GlobalVariable *
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000725ParseGlobalVariable(std::string *NameStr,
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000726 GlobalValue::LinkageTypes Linkage,
727 GlobalValue::VisibilityTypes Visibility,
Chris Lattnerb2b96672005-11-12 18:21:21 +0000728 bool isConstantGlobal, const Type *Ty,
Christopher Lambfe63fb92007-12-11 08:59:05 +0000729 Constant *Initializer, bool IsThreadLocal,
730 unsigned AddressSpace = 0) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000731 if (isa<FunctionType>(Ty)) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000732 GenerateError("Cannot declare global vars of function type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000733 return 0;
734 }
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000735
Christopher Lambfe63fb92007-12-11 08:59:05 +0000736 const PointerType *PTy = PointerType::get(Ty, AddressSpace);
Chris Lattnera09000d2004-07-14 23:03:46 +0000737
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000738 std::string Name;
739 if (NameStr) {
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000740 Name = *NameStr; // Copy string
741 delete NameStr; // Free old string
Chris Lattnerd88998d2004-07-14 08:23:52 +0000742 }
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000743
Chris Lattner8a327842004-07-14 21:44:00 +0000744 // See if this global value was forward referenced. If so, recycle the
745 // object.
Misha Brukman5a2a3822005-05-10 22:02:28 +0000746 ValID ID;
Chris Lattner8a327842004-07-14 21:44:00 +0000747 if (!Name.empty()) {
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000748 ID = ValID::createGlobalName(Name);
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000749 } else {
Reid Spencer186a43f2007-03-19 18:39:36 +0000750 ID = ValID::createGlobalID(CurModule.Values.size());
Chris Lattner8a327842004-07-14 21:44:00 +0000751 }
752
Reid Spencer863dd882007-04-28 16:06:50 +0000753 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000754 // Move the global to the end of the list, from whereever it was
Chris Lattner8a327842004-07-14 21:44:00 +0000755 // previously inserted.
756 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
757 CurModule.CurrentModule->getGlobalList().remove(GV);
758 CurModule.CurrentModule->getGlobalList().push_back(GV);
759 GV->setInitializer(Initializer);
760 GV->setLinkage(Linkage);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000761 GV->setVisibility(Visibility);
Chris Lattner8a327842004-07-14 21:44:00 +0000762 GV->setConstant(isConstantGlobal);
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +0000763 GV->setThreadLocal(IsThreadLocal);
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000764 InsertValue(GV, CurModule.Values);
Chris Lattnerb2b96672005-11-12 18:21:21 +0000765 return GV;
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000766 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000767
Reid Spenceref9b9a72007-02-05 20:47:22 +0000768 // If this global has a name
Chris Lattnera09000d2004-07-14 23:03:46 +0000769 if (!Name.empty()) {
Reid Spenceref9b9a72007-02-05 20:47:22 +0000770 // if the global we're parsing has an initializer (is a definition) and
771 // has external linkage.
772 if (Initializer && Linkage != GlobalValue::InternalLinkage)
773 // If there is already a global with external linkage with this name
774 if (CurModule.CurrentModule->getGlobalVariable(Name, false)) {
775 // If we allow this GVar to get created, it will be renamed in the
776 // symbol table because it conflicts with an existing GVar. We can't
777 // allow redefinition of GVars whose linking indicates that their name
778 // must stay the same. Issue the error.
779 GenerateError("Redefinition of global variable named '" + Name +
780 "' of type '" + Ty->getDescription() + "'");
781 return 0;
782 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000783 }
784
785 // Otherwise there is no existing GV to use, create one now.
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000786 GlobalVariable *GV =
Misha Brukman5a2a3822005-05-10 22:02:28 +0000787 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
Christopher Lambfe63fb92007-12-11 08:59:05 +0000788 CurModule.CurrentModule, IsThreadLocal, AddressSpace);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000789 GV->setVisibility(Visibility);
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000790 InsertValue(GV, CurModule.Values);
Chris Lattnerb2b96672005-11-12 18:21:21 +0000791 return GV;
Chris Lattnerd88998d2004-07-14 08:23:52 +0000792}
Chris Lattner8ab406d2004-07-13 07:59:27 +0000793
Reid Spencer75719bf2004-05-26 21:48:31 +0000794// setTypeName - Set the specified type to the name given. The name may be
795// null potentially, in which case this is a noop. The string passed in is
796// assumed to be a malloc'd string buffer, and is freed by this function.
797//
798// This function returns true if the type has already been defined, but is
799// allowed to be redefined in the specified context. If the name is a new name
800// for the type plane, it is inserted and false is returned.
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000801static bool setTypeName(const Type *T, std::string *NameStr) {
Reid Spencer1755a9a2007-02-05 17:01:20 +0000802 assert(!inFunctionScope() && "Can't give types function-local names!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000803 if (NameStr == 0) return false;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000804
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000805 std::string Name(*NameStr); // Copy string
806 delete NameStr; // Free old string
Reid Spencer75719bf2004-05-26 21:48:31 +0000807
808 // We don't allow assigning names to void type
Reid Spencer5b7e7532006-09-28 19:28:24 +0000809 if (T == Type::VoidTy) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000810 GenerateError("Can't assign name '" + Name + "' to the void type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000811 return false;
812 }
Reid Spencer75719bf2004-05-26 21:48:31 +0000813
Chris Lattnera09000d2004-07-14 23:03:46 +0000814 // Set the type name, checking for conflicts as we do so.
815 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000816
Chris Lattnera09000d2004-07-14 23:03:46 +0000817 if (AlreadyExists) { // Inserting a name that is already defined???
818 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
Reid Spencer1755a9a2007-02-05 17:01:20 +0000819 assert(Existing && "Conflict but no matching type?!");
Chris Lattnera09000d2004-07-14 23:03:46 +0000820
Reid Spencer75719bf2004-05-26 21:48:31 +0000821 // There is only one case where this is allowed: when we are refining an
822 // opaque type. In this case, Existing will be an opaque type.
823 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
824 // We ARE replacing an opaque type!
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000825 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000826 return true;
827 }
828
829 // Otherwise, this is an attempt to redefine a type. That's okay if
830 // the redefinition is identical to the original. This will be so if
831 // Existing and T point to the same Type object. In this one case we
832 // allow the equivalent redefinition.
833 if (Existing == T) return true; // Yes, it's equal.
834
835 // Any other kind of (non-equivalent) redefinition is an error.
Reid Spencer90e2f632007-01-05 21:50:38 +0000836 GenerateError("Redefinition of type named '" + Name + "' of type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +0000837 T->getDescription() + "'");
Reid Spencer75719bf2004-05-26 21:48:31 +0000838 }
839
Reid Spencer75719bf2004-05-26 21:48:31 +0000840 return false;
841}
Chris Lattner8896eda2001-07-09 19:38:36 +0000842
Chris Lattner30c89792001-09-07 16:35:17 +0000843//===----------------------------------------------------------------------===//
844// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000845//
Chris Lattner8896eda2001-07-09 19:38:36 +0000846
Chris Lattner3b073862004-02-09 03:19:29 +0000847// TypeContains - Returns true if Ty directly contains E in it.
Chris Lattner30c89792001-09-07 16:35:17 +0000848//
849static bool TypeContains(const Type *Ty, const Type *E) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000850 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
Chris Lattner6d8dbec2004-12-08 16:13:53 +0000851 E) != Ty->subtype_end();
Chris Lattner3b073862004-02-09 03:19:29 +0000852}
853
854namespace {
855 struct UpRefRecord {
856 // NestingLevel - The number of nesting levels that need to be popped before
857 // this type is resolved.
858 unsigned NestingLevel;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000859
Chris Lattner3b073862004-02-09 03:19:29 +0000860 // LastContainedTy - This is the type at the current binding level for the
861 // type. Every time we reduce the nesting level, this gets updated.
862 const Type *LastContainedTy;
863
864 // UpRefTy - This is the actual opaque type that the upreference is
865 // represented with.
866 OpaqueType *UpRefTy;
867
868 UpRefRecord(unsigned NL, OpaqueType *URTy)
869 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
870 };
Chris Lattner30c89792001-09-07 16:35:17 +0000871}
Chris Lattner698b56e2001-07-20 19:15:08 +0000872
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000873// UpRefs - A list of the outstanding upreferences that need to be resolved.
Chris Lattner3b073862004-02-09 03:19:29 +0000874static std::vector<UpRefRecord> UpRefs;
Chris Lattner30c89792001-09-07 16:35:17 +0000875
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000876/// HandleUpRefs - Every time we finish a new layer of types, this function is
877/// called. It loops through the UpRefs vector, which is a list of the
878/// currently active types. For each type, if the up reference is contained in
879/// the newly completed type, we decrement the level count. When the level
880/// count reaches zero, the upreferenced type is the type that is passed in:
881/// thus we can complete the cycle.
882///
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000883static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner703e92f2006-08-18 17:34:24 +0000884 // If Ty isn't abstract, or if there are no up-references in it, then there is
885 // nothing to resolve here.
886 if (!ty->isAbstract() || UpRefs.empty()) return ty;
887
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000888 PATypeHolder Ty(ty);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000889 UR_OUT("Type '" << Ty->getDescription() <<
Chris Lattner5084d032001-11-02 07:46:26 +0000890 "' newly formed. Resolving upreferences.\n" <<
891 UpRefs.size() << " upreferences active!\n");
Chris Lattner026a8ce2004-02-09 18:53:54 +0000892
893 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
894 // to zero), we resolve them all together before we resolve them to Ty. At
895 // the end of the loop, if there is anything to resolve to Ty, it will be in
896 // this variable.
897 OpaqueType *TypeToResolve = 0;
898
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000899 for (unsigned i = 0; i != UpRefs.size(); ++i) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000900 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
901 << UpRefs[i].second->getDescription() << ") = "
Reid Spencer3271ed52004-07-18 00:08:11 +0000902 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
Chris Lattner3b073862004-02-09 03:19:29 +0000903 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
904 // Decrement level of upreference
905 unsigned Level = --UpRefs[i].NestingLevel;
906 UpRefs[i].LastContainedTy = Ty;
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000907 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
Misha Brukman5a2a3822005-05-10 22:02:28 +0000908 if (Level == 0) { // Upreference should be resolved!
Chris Lattner026a8ce2004-02-09 18:53:54 +0000909 if (!TypeToResolve) {
910 TypeToResolve = UpRefs[i].UpRefTy;
911 } else {
912 UR_OUT(" * Resolving upreference for "
913 << UpRefs[i].second->getDescription() << "\n";
914 std::string OldName = UpRefs[i].UpRefTy->getDescription());
915 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
916 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
917 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
918 }
Reid Spencer3271ed52004-07-18 00:08:11 +0000919 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000920 --i; // Do not skip the next element...
Chris Lattner30c89792001-09-07 16:35:17 +0000921 }
922 }
Chris Lattner8896eda2001-07-09 19:38:36 +0000923 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000924
Chris Lattner026a8ce2004-02-09 18:53:54 +0000925 if (TypeToResolve) {
926 UR_OUT(" * Resolving upreference for "
927 << UpRefs[i].second->getDescription() << "\n";
Chris Lattner16af11d2004-02-09 21:03:38 +0000928 std::string OldName = TypeToResolve->getDescription());
Chris Lattner026a8ce2004-02-09 18:53:54 +0000929 TypeToResolve->refineAbstractTypeTo(Ty);
930 }
931
Chris Lattner8896eda2001-07-09 19:38:36 +0000932 return Ty;
933}
934
Chris Lattner00950542001-06-06 20:29:01 +0000935//===----------------------------------------------------------------------===//
936// RunVMAsmParser - Define an interface to this parser
937//===----------------------------------------------------------------------===//
938//
Reid Spencere1553cc2006-12-31 05:40:12 +0000939static Module* RunParser(Module * M);
940
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000941Module *llvm::RunVMAsmParser(llvm::MemoryBuffer *MB) {
942 InitLLLexer(MB);
943 Module *M = RunParser(new Module(LLLgetFilename()));
944 FreeLexer();
945 return M;
Chris Lattner00950542001-06-06 20:29:01 +0000946}
947
948%}
949
950%union {
Brian Gaeked0fde302003-11-11 22:41:34 +0000951 llvm::Module *ModuleVal;
952 llvm::Function *FunctionVal;
Brian Gaeked0fde302003-11-11 22:41:34 +0000953 llvm::BasicBlock *BasicBlockVal;
954 llvm::TerminatorInst *TermInstVal;
955 llvm::Instruction *InstVal;
Reid Spencer08de34b2006-12-03 05:45:44 +0000956 llvm::Constant *ConstVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000957
Reid Spencer08de34b2006-12-03 05:45:44 +0000958 const llvm::Type *PrimType;
Reid Spencere1553cc2006-12-31 05:40:12 +0000959 std::list<llvm::PATypeHolder> *TypeList;
Reid Spencer08de34b2006-12-03 05:45:44 +0000960 llvm::PATypeHolder *TypeVal;
961 llvm::Value *ValueVal;
Reid Spencer08de34b2006-12-03 05:45:44 +0000962 std::vector<llvm::Value*> *ValueList;
Reid Spencere1553cc2006-12-31 05:40:12 +0000963 llvm::ArgListType *ArgList;
964 llvm::TypeWithAttrs TypeWithAttrs;
965 llvm::TypeWithAttrsList *TypeWithAttrsList;
Dale Johanneseneb57ea72007-11-05 21:20:28 +0000966 llvm::ParamList *ParamList;
Reid Spencere1553cc2006-12-31 05:40:12 +0000967
Misha Brukman5a2a3822005-05-10 22:02:28 +0000968 // Represent the RHS of PHI node
Reid Spencer08de34b2006-12-03 05:45:44 +0000969 std::list<std::pair<llvm::Value*,
970 llvm::BasicBlock*> > *PHIList;
Brian Gaeked0fde302003-11-11 22:41:34 +0000971 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
Reid Spencer08de34b2006-12-03 05:45:44 +0000972 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000973
Brian Gaeked0fde302003-11-11 22:41:34 +0000974 llvm::GlobalValue::LinkageTypes Linkage;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000975 llvm::GlobalValue::VisibilityTypes Visibility;
Dale Johannesen0d51e7e2008-02-19 21:38:47 +0000976 llvm::ParameterAttributes ParamAttrs;
Reid Spencerf2310042007-02-28 02:23:44 +0000977 llvm::APInt *APIntVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000978 int64_t SInt64Val;
979 uint64_t UInt64Val;
980 int SIntVal;
981 unsigned UIntVal;
Dale Johannesen43421b32007-09-06 18:13:44 +0000982 llvm::APFloat *FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000983 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000984
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000985 std::string *StrVal; // This memory must be deleted
986 llvm::ValID ValIDVal;
Chris Lattner00950542001-06-06 20:29:01 +0000987
Reid Spencer08de34b2006-12-03 05:45:44 +0000988 llvm::Instruction::BinaryOps BinaryOpVal;
989 llvm::Instruction::TermOps TermOpVal;
990 llvm::Instruction::MemoryOps MemOpVal;
991 llvm::Instruction::CastOps CastOpVal;
992 llvm::Instruction::OtherOps OtherOpVal;
Reid Spencer08de34b2006-12-03 05:45:44 +0000993 llvm::ICmpInst::Predicate IPredicate;
994 llvm::FCmpInst::Predicate FPredicate;
Chris Lattner00950542001-06-06 20:29:01 +0000995}
996
Reid Spencere1553cc2006-12-31 05:40:12 +0000997%type <ModuleVal> Module
Chris Lattner79df7c02002-03-26 18:01:55 +0000998%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000999%type <BasicBlockVal> BasicBlock InstructionList
1000%type <TermInstVal> BBTerminatorInst
1001%type <InstVal> Inst InstVal MemoryInst
Anton Korobeynikova80e1182007-04-28 13:45:00 +00001002%type <ConstVal> ConstVal ConstExpr AliaseeRef
Chris Lattner6cdb0112001-11-26 16:54:11 +00001003%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +00001004%type <ArgList> ArgList ArgListH
Chris Lattnerc24d2082001-06-11 15:04:20 +00001005%type <PHIList> PHIList
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001006%type <ParamList> ParamList // For call param lists & GEP indices
Reid Spencere1553cc2006-12-31 05:40:12 +00001007%type <ValueList> IndexList // For GEP indices
1008%type <TypeList> TypeListI
1009%type <TypeWithAttrsList> ArgTypeList ArgTypeListI
Reid Spencer2c261782007-01-05 17:06:19 +00001010%type <TypeWithAttrs> ArgType
Chris Lattner00950542001-06-06 20:29:01 +00001011%type <JumpTable> JumpTable
Chris Lattner4ad02e72003-04-16 20:28:45 +00001012%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00001013%type <BoolVal> ThreadLocal // 'thread_local' or not
Chris Lattner15c9c032003-09-08 18:20:29 +00001014%type <BoolVal> OptVolatile // 'volatile' or not
Chris Lattnerddb6db42005-05-06 05:51:46 +00001015%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
Chris Lattneraa2c8532006-01-25 22:26:43 +00001016%type <BoolVal> OptSideEffect // 'sideeffect' or not.
Reid Spencere1553cc2006-12-31 05:40:12 +00001017%type <Linkage> GVInternalLinkage GVExternalLinkage
1018%type <Linkage> FunctionDefineLinkage FunctionDeclareLinkage
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001019%type <Linkage> AliasLinkage
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001020%type <Visibility> GVVisibilityStyle
Chris Lattner00950542001-06-06 20:29:01 +00001021
Chris Lattner2079fde2001-10-13 06:41:08 +00001022// ValueRef - Unresolved reference to a definition or BB
1023%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001024%type <ValueVal> ResolvedVal // <type> <valref> pair
Devang Patel5af2f632008-02-20 22:39:45 +00001025%type <ValueList> ReturnedVal
Chris Lattner00950542001-06-06 20:29:01 +00001026// Tokens and types for handling constant integer values
1027//
1028// ESINT64VAL - A negative number within long long range
1029%token <SInt64Val> ESINT64VAL
1030
1031// EUINT64VAL - A positive number within uns. long long range
1032%token <UInt64Val> EUINT64VAL
Chris Lattner00950542001-06-06 20:29:01 +00001033
Reid Spencerf2310042007-02-28 02:23:44 +00001034// ESAPINTVAL - A negative number with arbitrary precision
1035%token <APIntVal> ESAPINTVAL
1036
1037// EUAPINTVAL - A positive number with arbitrary precision
1038%token <APIntVal> EUAPINTVAL
1039
Reid Spencerb2d17862007-01-26 08:04:51 +00001040%token <UIntVal> LOCALVAL_ID GLOBALVAL_ID // %123 @123
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001041%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +00001042
1043// Built in types...
Reid Spencer2c261782007-01-05 17:06:19 +00001044%type <TypeVal> Types ResultTypes
Reid Spencere1553cc2006-12-31 05:40:12 +00001045%type <PrimType> IntType FPType PrimType // Classifications
Reid Spencer8088e9d2007-01-13 05:00:20 +00001046%token <PrimType> VOID INTTYPE
Dale Johannesen320fc8a2007-08-03 01:03:46 +00001047%token <PrimType> FLOAT DOUBLE X86_FP80 FP128 PPC_FP128 LABEL
Reid Spencerb951bc02006-12-29 20:29:48 +00001048%token TYPE
Chris Lattner00950542001-06-06 20:29:01 +00001049
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001050
Reid Spenceraa7c2f82007-05-19 07:21:26 +00001051%token<StrVal> LOCALVAR GLOBALVAR LABELSTR
1052%token<StrVal> STRINGCONSTANT ATSTRINGCONSTANT PCTSTRINGCONSTANT
Reid Spencerb2d17862007-01-26 08:04:51 +00001053%type <StrVal> LocalName OptLocalName OptLocalAssign
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001054%type <StrVal> GlobalName OptGlobalAssign GlobalAssign
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00001055%type <StrVal> OptSection SectionString OptGC
Chris Lattner00950542001-06-06 20:29:01 +00001056
Christopher Lambd49e18d2007-12-12 08:44:39 +00001057%type <UIntVal> OptAlign OptCAlign OptAddrSpace
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001058
Reid Spencer744d0362007-04-09 01:55:42 +00001059%token ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001060%token DECLARE DEFINE GLOBAL CONSTANT SECTION ALIAS VOLATILE THREAD_LOCAL
Reid Spencere1553cc2006-12-31 05:40:12 +00001061%token TO DOTDOTDOT NULL_TOK UNDEF INTERNAL LINKONCE WEAK APPENDING
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00001062%token DLLIMPORT DLLEXPORT EXTERN_WEAK
Christopher Lambfe63fb92007-12-11 08:59:05 +00001063%token OPAQUE EXTERNAL TARGET TRIPLE ALIGN ADDRSPACE
Chris Lattneraa2c8532006-01-25 22:26:43 +00001064%token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001065%token CC_TOK CCC_TOK FASTCC_TOK COLDCC_TOK X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
Nick Lewycky9be3c972008-03-10 02:20:00 +00001066%token DATALAYOUT UNWINDS
Chris Lattnera8e8f162005-05-06 20:27:19 +00001067%type <UIntVal> OptCallingConv
Reid Spencer2c261782007-01-05 17:06:19 +00001068%type <ParamAttrs> OptParamAttrs ParamAttr
1069%type <ParamAttrs> OptFuncAttrs FuncAttr
Chris Lattner00950542001-06-06 20:29:01 +00001070
Misha Brukman5a2a3822005-05-10 22:02:28 +00001071// Basic Block Terminating Operators
Chris Lattner16710e92004-10-16 18:17:13 +00001072%token <TermOpVal> RET BR SWITCH INVOKE UNWIND UNREACHABLE
Chris Lattner00950542001-06-06 20:29:01 +00001073
Misha Brukman5a2a3822005-05-10 22:02:28 +00001074// Binary Operators
Reid Spencere4d87aa2006-12-23 06:05:41 +00001075%type <BinaryOpVal> ArithmeticOps LogicalOps // Binops Subcatagories
Reid Spencer0a783f72006-11-02 01:53:59 +00001076%token <BinaryOpVal> ADD SUB MUL UDIV SDIV FDIV UREM SREM FREM AND OR XOR
Reid Spencer832254e2007-02-02 02:16:23 +00001077%token <BinaryOpVal> SHL LSHR ASHR
1078
Reid Spencer08de34b2006-12-03 05:45:44 +00001079%token <OtherOpVal> ICMP FCMP
Reid Spencer08de34b2006-12-03 05:45:44 +00001080%type <IPredicate> IPredicates
Reid Spencer08de34b2006-12-03 05:45:44 +00001081%type <FPredicate> FPredicates
Reid Spencer9f746f42006-12-03 06:58:07 +00001082%token EQ NE SLT SGT SLE SGE ULT UGT ULE UGE
1083%token OEQ ONE OLT OGT OLE OGE ORD UNO UEQ UNE
Chris Lattner00950542001-06-06 20:29:01 +00001084
1085// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001086%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +00001087
Reid Spencer3da59db2006-11-27 01:05:10 +00001088// Cast Operators
1089%type <CastOpVal> CastOps
1090%token <CastOpVal> TRUNC ZEXT SEXT FPTRUNC FPEXT BITCAST
1091%token <CastOpVal> UITOFP SITOFP FPTOUI FPTOSI INTTOPTR PTRTOINT
1092
Chris Lattner027dcc52001-07-08 21:10:27 +00001093// Other Operators
Reid Spencer832254e2007-02-02 02:16:23 +00001094%token <OtherOpVal> PHI_TOK SELECT VAARG
Chris Lattner4c949082006-04-08 01:18:35 +00001095%token <OtherOpVal> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
Devang Pateld6ffcf92008-02-19 22:26:37 +00001096%token <OtherOpVal> GETRESULT
Chris Lattnerddb6db42005-05-06 05:51:46 +00001097
Reid Spencer2c261782007-01-05 17:06:19 +00001098// Function Attributes
Duncan Sands36397f52007-07-27 12:58:54 +00001099%token SIGNEXT ZEROEXT NORETURN INREG SRET NOUNWIND NOALIAS BYVAL NEST
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00001100%token READNONE READONLY GC
Chris Lattner027dcc52001-07-08 21:10:27 +00001101
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001102// Visibility Styles
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +00001103%token DEFAULT HIDDEN PROTECTED
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001104
Chris Lattner00950542001-06-06 20:29:01 +00001105%start Module
1106%%
1107
Chris Lattner00950542001-06-06 20:29:01 +00001108
Misha Brukman5a2a3822005-05-10 22:02:28 +00001109// Operations that are notably excluded from this list include:
Chris Lattner00950542001-06-06 20:29:01 +00001110// RET, BR, & SWITCH because they end basic blocks and are treated specially.
1111//
Reid Spencer0a783f72006-11-02 01:53:59 +00001112ArithmeticOps: ADD | SUB | MUL | UDIV | SDIV | FDIV | UREM | SREM | FREM;
Reid Spencer832254e2007-02-02 02:16:23 +00001113LogicalOps : SHL | LSHR | ASHR | AND | OR | XOR;
Reid Spencer3da59db2006-11-27 01:05:10 +00001114CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | BITCAST |
1115 UITOFP | SITOFP | FPTOUI | FPTOSI | INTTOPTR | PTRTOINT;
Reid Spencer832254e2007-02-02 02:16:23 +00001116
Reid Spencer9f746f42006-12-03 06:58:07 +00001117IPredicates
Reid Spencer763ed5e2006-12-04 05:20:06 +00001118 : EQ { $$ = ICmpInst::ICMP_EQ; } | NE { $$ = ICmpInst::ICMP_NE; }
Reid Spencer9f746f42006-12-03 06:58:07 +00001119 | SLT { $$ = ICmpInst::ICMP_SLT; } | SGT { $$ = ICmpInst::ICMP_SGT; }
1120 | SLE { $$ = ICmpInst::ICMP_SLE; } | SGE { $$ = ICmpInst::ICMP_SGE; }
1121 | ULT { $$ = ICmpInst::ICMP_ULT; } | UGT { $$ = ICmpInst::ICMP_UGT; }
1122 | ULE { $$ = ICmpInst::ICMP_ULE; } | UGE { $$ = ICmpInst::ICMP_UGE; }
1123 ;
1124
1125FPredicates
1126 : OEQ { $$ = FCmpInst::FCMP_OEQ; } | ONE { $$ = FCmpInst::FCMP_ONE; }
1127 | OLT { $$ = FCmpInst::FCMP_OLT; } | OGT { $$ = FCmpInst::FCMP_OGT; }
1128 | OLE { $$ = FCmpInst::FCMP_OLE; } | OGE { $$ = FCmpInst::FCMP_OGE; }
1129 | ORD { $$ = FCmpInst::FCMP_ORD; } | UNO { $$ = FCmpInst::FCMP_UNO; }
1130 | UEQ { $$ = FCmpInst::FCMP_UEQ; } | UNE { $$ = FCmpInst::FCMP_UNE; }
1131 | ULT { $$ = FCmpInst::FCMP_ULT; } | UGT { $$ = FCmpInst::FCMP_UGT; }
1132 | ULE { $$ = FCmpInst::FCMP_ULE; } | UGE { $$ = FCmpInst::FCMP_UGE; }
1133 | TRUETOK { $$ = FCmpInst::FCMP_TRUE; }
1134 | FALSETOK { $$ = FCmpInst::FCMP_FALSE; }
1135 ;
Chris Lattner00950542001-06-06 20:29:01 +00001136
Chris Lattnere98dda62001-07-14 06:10:16 +00001137// These are some types that allow classification if we only want a particular
1138// thing... for example, only a signed, unsigned, or integral type.
Reid Spencera54b7cb2007-01-12 07:05:14 +00001139IntType : INTTYPE;
Dale Johannesen320fc8a2007-08-03 01:03:46 +00001140FPType : FLOAT | DOUBLE | PPC_FP128 | FP128 | X86_FP80;
Chris Lattner00950542001-06-06 20:29:01 +00001141
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001142LocalName : LOCALVAR | STRINGCONSTANT | PCTSTRINGCONSTANT ;
Reid Spencerb2d17862007-01-26 08:04:51 +00001143OptLocalName : LocalName | /*empty*/ { $$ = 0; };
1144
Christopher Lambd49e18d2007-12-12 08:44:39 +00001145OptAddrSpace : ADDRSPACE '(' EUINT64VAL ')' { $$=$3; }
1146 | /*empty*/ { $$=0; };
1147
Reid Spencerb2d17862007-01-26 08:04:51 +00001148/// OptLocalAssign - Value producing statements have an optional assignment
1149/// component.
1150OptLocalAssign : LocalName '=' {
1151 $$ = $1;
1152 CHECK_FOR_ERROR
1153 }
1154 | /*empty*/ {
1155 $$ = 0;
1156 CHECK_FOR_ERROR
1157 };
1158
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001159GlobalName : GLOBALVAR | ATSTRINGCONSTANT ;
Reid Spencerb2d17862007-01-26 08:04:51 +00001160
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001161OptGlobalAssign : GlobalAssign
Misha Brukman5a2a3822005-05-10 22:02:28 +00001162 | /*empty*/ {
1163 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00001164 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001165 };
Chris Lattner00950542001-06-06 20:29:01 +00001166
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001167GlobalAssign : GlobalName '=' {
1168 $$ = $1;
1169 CHECK_FOR_ERROR
Anton Korobeynikovc0fabcb2007-04-25 18:07:40 +00001170 };
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001171
Reid Spencerb951bc02006-12-29 20:29:48 +00001172GVInternalLinkage
1173 : INTERNAL { $$ = GlobalValue::InternalLinkage; }
1174 | WEAK { $$ = GlobalValue::WeakLinkage; }
1175 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1176 | APPENDING { $$ = GlobalValue::AppendingLinkage; }
1177 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
1178 ;
1179
1180GVExternalLinkage
1181 : DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1182 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
1183 | EXTERNAL { $$ = GlobalValue::ExternalLinkage; }
1184 ;
1185
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001186GVVisibilityStyle
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +00001187 : /*empty*/ { $$ = GlobalValue::DefaultVisibility; }
1188 | DEFAULT { $$ = GlobalValue::DefaultVisibility; }
1189 | HIDDEN { $$ = GlobalValue::HiddenVisibility; }
1190 | PROTECTED { $$ = GlobalValue::ProtectedVisibility; }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001191 ;
1192
Reid Spencere1553cc2006-12-31 05:40:12 +00001193FunctionDeclareLinkage
1194 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1195 | DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1196 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
Reid Spencerb951bc02006-12-29 20:29:48 +00001197 ;
1198
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001199FunctionDefineLinkage
Reid Spencere1553cc2006-12-31 05:40:12 +00001200 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1201 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
Reid Spencerb951bc02006-12-29 20:29:48 +00001202 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1203 | WEAK { $$ = GlobalValue::WeakLinkage; }
1204 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
Reid Spencerb951bc02006-12-29 20:29:48 +00001205 ;
Chris Lattner30c89792001-09-07 16:35:17 +00001206
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001207AliasLinkage
1208 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1209 | WEAK { $$ = GlobalValue::WeakLinkage; }
1210 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
1211 ;
1212
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00001213OptCallingConv : /*empty*/ { $$ = CallingConv::C; } |
1214 CCC_TOK { $$ = CallingConv::C; } |
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00001215 FASTCC_TOK { $$ = CallingConv::Fast; } |
1216 COLDCC_TOK { $$ = CallingConv::Cold; } |
1217 X86_STDCALLCC_TOK { $$ = CallingConv::X86_StdCall; } |
1218 X86_FASTCALLCC_TOK { $$ = CallingConv::X86_FastCall; } |
1219 CC_TOK EUINT64VAL {
Chris Lattnera8e8f162005-05-06 20:27:19 +00001220 if ((unsigned)$2 != $2)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001221 GEN_ERROR("Calling conv too large");
Chris Lattnera8e8f162005-05-06 20:27:19 +00001222 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001223 CHECK_FOR_ERROR
Chris Lattnera8e8f162005-05-06 20:27:19 +00001224 };
1225
Reid Spencer9445e9a2007-07-19 23:13:04 +00001226ParamAttr : ZEROEXT { $$ = ParamAttr::ZExt; }
Reid Spencer1d3d2302007-07-31 02:57:37 +00001227 | ZEXT { $$ = ParamAttr::ZExt; }
Reid Spencer9445e9a2007-07-19 23:13:04 +00001228 | SIGNEXT { $$ = ParamAttr::SExt; }
Reid Spencer1d3d2302007-07-31 02:57:37 +00001229 | SEXT { $$ = ParamAttr::SExt; }
Zhou Shengfebca342007-06-05 05:28:26 +00001230 | INREG { $$ = ParamAttr::InReg; }
1231 | SRET { $$ = ParamAttr::StructRet; }
1232 | NOALIAS { $$ = ParamAttr::NoAlias; }
Duncan Sands36397f52007-07-27 12:58:54 +00001233 | BYVAL { $$ = ParamAttr::ByVal; }
1234 | NEST { $$ = ParamAttr::Nest; }
Dale Johannesen08e78b12008-02-22 17:49:45 +00001235 | ALIGN EUINT64VAL { $$ =
1236 ParamAttr::constructAlignmentFromInt($2); }
Reid Spencere1553cc2006-12-31 05:40:12 +00001237 ;
1238
Reid Spencer18da0722007-04-11 02:44:20 +00001239OptParamAttrs : /* empty */ { $$ = ParamAttr::None; }
Reid Spencer2c261782007-01-05 17:06:19 +00001240 | OptParamAttrs ParamAttr {
Reid Spencer5694b6e2007-04-09 06:17:21 +00001241 $$ = $1 | $2;
Reid Spencere1553cc2006-12-31 05:40:12 +00001242 }
1243 ;
1244
Reid Spencer18da0722007-04-11 02:44:20 +00001245FuncAttr : NORETURN { $$ = ParamAttr::NoReturn; }
1246 | NOUNWIND { $$ = ParamAttr::NoUnwind; }
Reid Spencer9445e9a2007-07-19 23:13:04 +00001247 | ZEROEXT { $$ = ParamAttr::ZExt; }
1248 | SIGNEXT { $$ = ParamAttr::SExt; }
Duncan Sandsed4a2f12007-11-22 20:23:04 +00001249 | READNONE { $$ = ParamAttr::ReadNone; }
1250 | READONLY { $$ = ParamAttr::ReadOnly; }
Reid Spencer2c261782007-01-05 17:06:19 +00001251 ;
1252
Reid Spencer18da0722007-04-11 02:44:20 +00001253OptFuncAttrs : /* empty */ { $$ = ParamAttr::None; }
Reid Spencer2c261782007-01-05 17:06:19 +00001254 | OptFuncAttrs FuncAttr {
Reid Spencer5694b6e2007-04-09 06:17:21 +00001255 $$ = $1 | $2;
Reid Spencer2c261782007-01-05 17:06:19 +00001256 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001257 ;
1258
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00001259OptGC : /* empty */ { $$ = 0; }
1260 | GC STRINGCONSTANT {
1261 $$ = $2;
1262 }
1263 ;
1264
Chris Lattner66db8e42005-11-06 06:34:12 +00001265// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1266// a comma before it.
Chris Lattner87ac9722005-11-06 06:46:28 +00001267OptAlign : /*empty*/ { $$ = 0; } |
Chris Lattnerb2b96672005-11-12 18:21:21 +00001268 ALIGN EUINT64VAL {
1269 $$ = $2;
1270 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001271 GEN_ERROR("Alignment must be a power of two");
Reid Spencer61c83e02006-08-18 08:43:06 +00001272 CHECK_FOR_ERROR
Chris Lattnerb2b96672005-11-12 18:21:21 +00001273};
Chris Lattner66db8e42005-11-06 06:34:12 +00001274OptCAlign : /*empty*/ { $$ = 0; } |
Chris Lattnerb2b96672005-11-12 18:21:21 +00001275 ',' ALIGN EUINT64VAL {
1276 $$ = $3;
1277 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001278 GEN_ERROR("Alignment must be a power of two");
Reid Spencer61c83e02006-08-18 08:43:06 +00001279 CHECK_FOR_ERROR
Chris Lattnerb2b96672005-11-12 18:21:21 +00001280};
1281
Chris Lattner66db8e42005-11-06 06:34:12 +00001282
Christopher Lambfe63fb92007-12-11 08:59:05 +00001283
Chris Lattner164c3782005-11-12 00:11:10 +00001284SectionString : SECTION STRINGCONSTANT {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001285 for (unsigned i = 0, e = $2->length(); i != e; ++i)
1286 if ((*$2)[i] == '"' || (*$2)[i] == '\\')
Reid Spencerf4fa5902007-02-05 10:16:10 +00001287 GEN_ERROR("Invalid character in section name");
Chris Lattner164c3782005-11-12 00:11:10 +00001288 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001289 CHECK_FOR_ERROR
Chris Lattner164c3782005-11-12 00:11:10 +00001290};
1291
1292OptSection : /*empty*/ { $$ = 0; } |
1293 SectionString { $$ = $1; };
Chris Lattner164c3782005-11-12 00:11:10 +00001294
Chris Lattnerb2b96672005-11-12 18:21:21 +00001295// GlobalVarAttributes - Used to pass the attributes string on a global. CurGV
1296// is set to be the global we are processing.
1297//
1298GlobalVarAttributes : /* empty */ {} |
1299 ',' GlobalVarAttribute GlobalVarAttributes {};
1300GlobalVarAttribute : SectionString {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001301 CurGV->setSection(*$1);
1302 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001303 CHECK_FOR_ERROR
Chris Lattnerb2b96672005-11-12 18:21:21 +00001304 }
1305 | ALIGN EUINT64VAL {
1306 if ($2 != 0 && !isPowerOf2_32($2))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001307 GEN_ERROR("Alignment must be a power of two");
Chris Lattnerb2b96672005-11-12 18:21:21 +00001308 CurGV->setAlignment($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001309 CHECK_FOR_ERROR
Chris Lattnerb2b96672005-11-12 18:21:21 +00001310 };
Chris Lattner164c3782005-11-12 00:11:10 +00001311
Chris Lattner30c89792001-09-07 16:35:17 +00001312//===----------------------------------------------------------------------===//
1313// Types includes all predefined types... except void, because it can only be
Reid Spencere1553cc2006-12-31 05:40:12 +00001314// used in specific contexts (function returning void for example).
Chris Lattner30c89792001-09-07 16:35:17 +00001315
1316// Derived types are added later...
1317//
Dale Johannesen320fc8a2007-08-03 01:03:46 +00001318PrimType : INTTYPE | FLOAT | DOUBLE | PPC_FP128 | FP128 | X86_FP80 | LABEL ;
Reid Spencere1553cc2006-12-31 05:40:12 +00001319
1320Types
1321 : OPAQUE {
Reid Spencer08de34b2006-12-03 05:45:44 +00001322 $$ = new PATypeHolder(OpaqueType::get());
Reid Spencer61c83e02006-08-18 08:43:06 +00001323 CHECK_FOR_ERROR
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001324 }
1325 | PrimType {
Reid Spencer08de34b2006-12-03 05:45:44 +00001326 $$ = new PATypeHolder($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001327 CHECK_FOR_ERROR
Reid Spencere1553cc2006-12-31 05:40:12 +00001328 }
Christopher Lambd49e18d2007-12-12 08:44:39 +00001329 | Types OptAddrSpace '*' { // Pointer type?
Reid Spencere1553cc2006-12-31 05:40:12 +00001330 if (*$1 == Type::LabelTy)
1331 GEN_ERROR("Cannot form a pointer to a basic block");
Christopher Lambd49e18d2007-12-12 08:44:39 +00001332 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1, $2)));
Christopher Lambfe63fb92007-12-11 08:59:05 +00001333 delete $1;
1334 CHECK_FOR_ERROR
1335 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001336 | SymbolicValueRef { // Named types are also simple types...
1337 const Type* tmp = getTypeVal($1);
1338 CHECK_FOR_ERROR
1339 $$ = new PATypeHolder(tmp);
1340 }
1341 | '\\' EUINT64VAL { // Type UpReference
Reid Spencerf4fa5902007-02-05 10:16:10 +00001342 if ($2 > (uint64_t)~0U) GEN_ERROR("Value out of range");
Chris Lattner30c89792001-09-07 16:35:17 +00001343 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
Chris Lattner3b073862004-02-09 03:19:29 +00001344 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Reid Spencer08de34b2006-12-03 05:45:44 +00001345 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +00001346 UR_OUT("New Upreference!\n");
Reid Spencer61c83e02006-08-18 08:43:06 +00001347 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001348 }
Reid Spencer863dd882007-04-28 16:06:50 +00001349 | Types '(' ArgTypeListI ')' OptFuncAttrs {
Duncan Sandsdc024672007-11-27 13:23:08 +00001350 // Allow but ignore attributes on function types; this permits auto-upgrade.
1351 // FIXME: remove in LLVM 3.0.
Chris Lattnerf7dedf22008-04-23 05:36:58 +00001352 const Type *RetTy = *$1;
1353 if (!FunctionType::isValidReturnType(RetTy))
1354 GEN_ERROR("Invalid result type for LLVM function");
1355
Chris Lattner9232b992003-04-22 18:42:41 +00001356 std::vector<const Type*> Params;
Reid Spencer5694b6e2007-04-09 06:17:21 +00001357 TypeWithAttrsList::iterator I = $3->begin(), E = $3->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00001358 for (; I != E; ++I ) {
Reid Spencer2c9df212007-03-20 01:13:00 +00001359 const Type *Ty = I->Ty->get();
Reid Spencer2c9df212007-03-20 01:13:00 +00001360 Params.push_back(Ty);
Reid Spencere1553cc2006-12-31 05:40:12 +00001361 }
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001362
Chris Lattner2079fde2001-10-13 06:41:08 +00001363 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1364 if (isVarArg) Params.pop_back();
1365
Anton Korobeynikovf8c751a2007-12-03 21:00:45 +00001366 for (unsigned i = 0; i != Params.size(); ++i)
1367 if (!(Params[i]->isFirstClassType() || isa<OpaqueType>(Params[i])))
1368 GEN_ERROR("Function arguments must be value types!");
1369
1370 CHECK_FOR_ERROR
1371
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001372 FunctionType *FT = FunctionType::get(RetTy, Params, isVarArg);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001373 delete $3; // Delete the argument list
Reid Spencere1553cc2006-12-31 05:40:12 +00001374 delete $1; // Delete the return type handle
1375 $$ = new PATypeHolder(HandleUpRefs(FT));
Reid Spencer61c83e02006-08-18 08:43:06 +00001376 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001377 }
Reid Spencer863dd882007-04-28 16:06:50 +00001378 | VOID '(' ArgTypeListI ')' OptFuncAttrs {
Duncan Sandsdc024672007-11-27 13:23:08 +00001379 // Allow but ignore attributes on function types; this permits auto-upgrade.
1380 // FIXME: remove in LLVM 3.0.
Reid Spencere1553cc2006-12-31 05:40:12 +00001381 std::vector<const Type*> Params;
Reid Spencer5694b6e2007-04-09 06:17:21 +00001382 TypeWithAttrsList::iterator I = $3->begin(), E = $3->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00001383 for ( ; I != E; ++I ) {
Reid Spencer2c9df212007-03-20 01:13:00 +00001384 const Type* Ty = I->Ty->get();
Reid Spencer2c9df212007-03-20 01:13:00 +00001385 Params.push_back(Ty);
Reid Spencere1553cc2006-12-31 05:40:12 +00001386 }
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001387
Reid Spencere1553cc2006-12-31 05:40:12 +00001388 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1389 if (isVarArg) Params.pop_back();
1390
Anton Korobeynikovf8c751a2007-12-03 21:00:45 +00001391 for (unsigned i = 0; i != Params.size(); ++i)
1392 if (!(Params[i]->isFirstClassType() || isa<OpaqueType>(Params[i])))
1393 GEN_ERROR("Function arguments must be value types!");
1394
1395 CHECK_FOR_ERROR
1396
Duncan Sandsdc024672007-11-27 13:23:08 +00001397 FunctionType *FT = FunctionType::get($1, Params, isVarArg);
Reid Spencer2c261782007-01-05 17:06:19 +00001398 delete $3; // Delete the argument list
Reid Spencere1553cc2006-12-31 05:40:12 +00001399 $$ = new PATypeHolder(HandleUpRefs(FT));
1400 CHECK_FOR_ERROR
1401 }
1402
1403 | '[' EUINT64VAL 'x' Types ']' { // Sized array type?
Reid Spencer08de34b2006-12-03 05:45:44 +00001404 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
1405 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00001406 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001407 }
Reid Spencerac9dcb92007-02-15 03:39:18 +00001408 | '<' EUINT64VAL 'x' Types '>' { // Vector type?
Reid Spencer08de34b2006-12-03 05:45:44 +00001409 const llvm::Type* ElemTy = $4->get();
1410 if ((unsigned)$2 != $2)
1411 GEN_ERROR("Unsigned result not equal to signed result");
Chris Lattner42a75512007-01-15 02:27:26 +00001412 if (!ElemTy->isFloatingPoint() && !ElemTy->isInteger())
Reid Spencer9d6565a2007-02-15 02:26:10 +00001413 GEN_ERROR("Element type of a VectorType must be primitive");
Reid Spencer9d6565a2007-02-15 02:26:10 +00001414 $$ = new PATypeHolder(HandleUpRefs(VectorType::get(*$4, (unsigned)$2)));
Reid Spencer08de34b2006-12-03 05:45:44 +00001415 delete $4;
1416 CHECK_FOR_ERROR
Brian Gaeke715c90b2004-08-20 06:00:58 +00001417 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001418 | '{' TypeListI '}' { // Structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001419 std::vector<const Type*> Elements;
Reid Spencer08de34b2006-12-03 05:45:44 +00001420 for (std::list<llvm::PATypeHolder>::iterator I = $2->begin(),
Chris Lattnera08976b2005-02-22 23:13:58 +00001421 E = $2->end(); I != E; ++I)
Reid Spencer08de34b2006-12-03 05:45:44 +00001422 Elements.push_back(*I);
Misha Brukman5a2a3822005-05-10 22:02:28 +00001423
Reid Spencer08de34b2006-12-03 05:45:44 +00001424 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001425 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001426 CHECK_FOR_ERROR
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001427 }
1428 | '{' '}' { // Empty structure type?
Reid Spencer08de34b2006-12-03 05:45:44 +00001429 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Reid Spencer61c83e02006-08-18 08:43:06 +00001430 CHECK_FOR_ERROR
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001431 }
Andrew Lenharth38ecbf12006-12-08 18:06:16 +00001432 | '<' '{' TypeListI '}' '>' {
1433 std::vector<const Type*> Elements;
1434 for (std::list<llvm::PATypeHolder>::iterator I = $3->begin(),
1435 E = $3->end(); I != E; ++I)
1436 Elements.push_back(*I);
1437
1438 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements, true)));
1439 delete $3;
1440 CHECK_FOR_ERROR
1441 }
1442 | '<' '{' '}' '>' { // Empty structure type?
1443 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>(), true));
1444 CHECK_FOR_ERROR
1445 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001446 ;
1447
1448ArgType
Duncan Sandsdc024672007-11-27 13:23:08 +00001449 : Types OptParamAttrs {
1450 // Allow but ignore attributes on function types; this permits auto-upgrade.
1451 // FIXME: remove in LLVM 3.0.
Reid Spencere1553cc2006-12-31 05:40:12 +00001452 $$.Ty = $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00001453 $$.Attrs = ParamAttr::None;
Reid Spencere1553cc2006-12-31 05:40:12 +00001454 }
1455 ;
1456
Reid Spencer2c261782007-01-05 17:06:19 +00001457ResultTypes
1458 : Types {
Reid Spencere1553cc2006-12-31 05:40:12 +00001459 if (!UpRefs.empty())
1460 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Devang Patel155b8742008-02-23 01:17:17 +00001461 if (!(*$1)->isFirstClassType() && !isa<StructType>($1->get()))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001462 GEN_ERROR("LLVM functions cannot return aggregate types");
Reid Spencer2c261782007-01-05 17:06:19 +00001463 $$ = $1;
Reid Spencere1553cc2006-12-31 05:40:12 +00001464 }
Reid Spencer2c261782007-01-05 17:06:19 +00001465 | VOID {
1466 $$ = new PATypeHolder(Type::VoidTy);
Reid Spencere1553cc2006-12-31 05:40:12 +00001467 }
1468 ;
1469
1470ArgTypeList : ArgType {
1471 $$ = new TypeWithAttrsList();
1472 $$->push_back($1);
1473 CHECK_FOR_ERROR
1474 }
1475 | ArgTypeList ',' ArgType {
1476 ($$=$1)->push_back($3);
1477 CHECK_FOR_ERROR
1478 }
1479 ;
1480
1481ArgTypeListI
1482 : ArgTypeList
1483 | ArgTypeList ',' DOTDOTDOT {
1484 $$=$1;
Reid Spencer18da0722007-04-11 02:44:20 +00001485 TypeWithAttrs TWA; TWA.Attrs = ParamAttr::None;
Reid Spencere1553cc2006-12-31 05:40:12 +00001486 TWA.Ty = new PATypeHolder(Type::VoidTy);
1487 $$->push_back(TWA);
1488 CHECK_FOR_ERROR
1489 }
1490 | DOTDOTDOT {
1491 $$ = new TypeWithAttrsList;
Reid Spencer18da0722007-04-11 02:44:20 +00001492 TypeWithAttrs TWA; TWA.Attrs = ParamAttr::None;
Reid Spencere1553cc2006-12-31 05:40:12 +00001493 TWA.Ty = new PATypeHolder(Type::VoidTy);
1494 $$->push_back(TWA);
1495 CHECK_FOR_ERROR
1496 }
1497 | /*empty*/ {
1498 $$ = new TypeWithAttrsList();
Reid Spencer61c83e02006-08-18 08:43:06 +00001499 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001500 };
Chris Lattner30c89792001-09-07 16:35:17 +00001501
Chris Lattner7e708292002-06-25 16:13:24 +00001502// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +00001503// declaration type lists
1504//
Reid Spencere1553cc2006-12-31 05:40:12 +00001505TypeListI : Types {
Reid Spencer08de34b2006-12-03 05:45:44 +00001506 $$ = new std::list<PATypeHolder>();
Reid Spencer2c9df212007-03-20 01:13:00 +00001507 $$->push_back(*$1);
1508 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001509 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001510 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001511 | TypeListI ',' Types {
Reid Spencer2c9df212007-03-20 01:13:00 +00001512 ($$=$1)->push_back(*$3);
1513 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001514 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001515 };
Chris Lattner30c89792001-09-07 16:35:17 +00001516
Chris Lattnere98dda62001-07-14 06:10:16 +00001517// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +00001518// production is used ONLY to represent constants that show up AFTER a 'const',
1519// 'constant' or 'global' token at global scope. Constants that can be inlined
1520// into other expressions (such as integers and constexprs) are handled by the
1521// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +00001522//
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001523ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencere1553cc2006-12-31 05:40:12 +00001524 if (!UpRefs.empty())
1525 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001526 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001527 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001528 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001529 (*$1)->getDescription() + "'");
Chris Lattner30c89792001-09-07 16:35:17 +00001530 const Type *ETy = ATy->getElementType();
1531 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +00001532
Chris Lattner30c89792001-09-07 16:35:17 +00001533 // Verify that we have the correct size...
1534 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001535 GEN_ERROR("Type mismatch: constant sized array initialized with " +
Reid Spencer3271ed52004-07-18 00:08:11 +00001536 utostr($3->size()) + " arguments, but has size of " +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001537 itostr(NumElements) + "");
Chris Lattner00950542001-06-06 20:29:01 +00001538
Chris Lattner30c89792001-09-07 16:35:17 +00001539 // Verify all elements are correct type!
1540 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencer08de34b2006-12-03 05:45:44 +00001541 if (ETy != (*$3)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001542 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Reid Spencer3271ed52004-07-18 00:08:11 +00001543 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencer08de34b2006-12-03 05:45:44 +00001544 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +00001545 }
1546
Reid Spencer08de34b2006-12-03 05:45:44 +00001547 $$ = ConstantArray::get(ATy, *$3);
1548 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001549 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001550 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001551 | Types '[' ']' {
Reid Spencere1553cc2006-12-31 05:40:12 +00001552 if (!UpRefs.empty())
1553 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001554 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001555 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001556 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001557 (*$1)->getDescription() + "'");
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001558
1559 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +00001560 if (NumElements != -1 && NumElements != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001561 GEN_ERROR("Type mismatch: constant sized array initialized with 0"
Reid Spencerf4fa5902007-02-05 10:16:10 +00001562 " arguments, but has size of " + itostr(NumElements) +"");
Reid Spencer08de34b2006-12-03 05:45:44 +00001563 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
1564 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001565 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001566 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001567 | Types 'c' STRINGCONSTANT {
Reid Spencere1553cc2006-12-31 05:40:12 +00001568 if (!UpRefs.empty())
1569 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001570 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001571 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001572 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001573 (*$1)->getDescription() + "'");
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001574
Chris Lattner30c89792001-09-07 16:35:17 +00001575 int NumElements = ATy->getNumElements();
1576 const Type *ETy = ATy->getElementType();
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001577 if (NumElements != -1 && NumElements != int($3->length()))
Reid Spencer61c83e02006-08-18 08:43:06 +00001578 GEN_ERROR("Can't build string constant of size " +
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001579 itostr((int)($3->length())) +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001580 " when array has size " + itostr(NumElements) + "");
Chris Lattner9232b992003-04-22 18:42:41 +00001581 std::vector<Constant*> Vals;
Reid Spencere1553cc2006-12-31 05:40:12 +00001582 if (ETy == Type::Int8Ty) {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001583 for (unsigned i = 0; i < $3->length(); ++i)
1584 Vals.push_back(ConstantInt::get(ETy, (*$3)[i]));
Chris Lattner93750fa2001-07-28 17:48:55 +00001585 } else {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001586 delete $3;
Reid Spencerf4fa5902007-02-05 10:16:10 +00001587 GEN_ERROR("Cannot build string arrays of non byte sized elements");
Chris Lattner93750fa2001-07-28 17:48:55 +00001588 }
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001589 delete $3;
Reid Spencer08de34b2006-12-03 05:45:44 +00001590 $$ = ConstantArray::get(ATy, Vals);
1591 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001592 CHECK_FOR_ERROR
Chris Lattner93750fa2001-07-28 17:48:55 +00001593 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00001594 | Types '<' ConstVector '>' { // Nonempty unsized arr
Reid Spencere1553cc2006-12-31 05:40:12 +00001595 if (!UpRefs.empty())
1596 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer9d6565a2007-02-15 02:26:10 +00001597 const VectorType *PTy = dyn_cast<VectorType>($1->get());
Brian Gaeke715c90b2004-08-20 06:00:58 +00001598 if (PTy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001599 GEN_ERROR("Cannot make packed constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001600 (*$1)->getDescription() + "'");
Brian Gaeke715c90b2004-08-20 06:00:58 +00001601 const Type *ETy = PTy->getElementType();
1602 int NumElements = PTy->getNumElements();
1603
1604 // Verify that we have the correct size...
1605 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001606 GEN_ERROR("Type mismatch: constant sized packed initialized with " +
Brian Gaeke715c90b2004-08-20 06:00:58 +00001607 utostr($3->size()) + " arguments, but has size of " +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001608 itostr(NumElements) + "");
Brian Gaeke715c90b2004-08-20 06:00:58 +00001609
1610 // Verify all elements are correct type!
1611 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencer08de34b2006-12-03 05:45:44 +00001612 if (ETy != (*$3)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001613 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Brian Gaeke715c90b2004-08-20 06:00:58 +00001614 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencer08de34b2006-12-03 05:45:44 +00001615 (*$3)[i]->getType()->getDescription() + "'.");
Brian Gaeke715c90b2004-08-20 06:00:58 +00001616 }
1617
Reid Spencer9d6565a2007-02-15 02:26:10 +00001618 $$ = ConstantVector::get(PTy, *$3);
Reid Spencer08de34b2006-12-03 05:45:44 +00001619 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001620 CHECK_FOR_ERROR
Brian Gaeke715c90b2004-08-20 06:00:58 +00001621 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001622 | Types '{' ConstVector '}' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001623 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001624 if (STy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001625 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001626 (*$1)->getDescription() + "'");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001627
Chris Lattnerc6212f12003-05-21 16:06:56 +00001628 if ($3->size() != STy->getNumContainedTypes())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001629 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattnerc6212f12003-05-21 16:06:56 +00001630
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001631 // Check to ensure that constants are compatible with the type initializer!
1632 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Reid Spencer08de34b2006-12-03 05:45:44 +00001633 if ((*$3)[i]->getType() != STy->getElementType(i))
Reid Spencer61c83e02006-08-18 08:43:06 +00001634 GEN_ERROR("Expected type '" +
Chris Lattnerd21cd802004-02-09 04:37:31 +00001635 STy->getElementType(i)->getDescription() +
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001636 "' for element #" + utostr(i) +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001637 " of structure initializer");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001638
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001639 // Check to ensure that Type is not packed
1640 if (STy->isPacked())
Chris Lattner4989b842007-04-26 05:30:35 +00001641 GEN_ERROR("Unpacked Initializer to vector type '" +
1642 STy->getDescription() + "'");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001643
Reid Spencer08de34b2006-12-03 05:45:44 +00001644 $$ = ConstantStruct::get(STy, *$3);
1645 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001646 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001647 }
Chris Lattnerc6212f12003-05-21 16:06:56 +00001648 | Types '{' '}' {
Reid Spencere1553cc2006-12-31 05:40:12 +00001649 if (!UpRefs.empty())
1650 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001651 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerc6212f12003-05-21 16:06:56 +00001652 if (STy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001653 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001654 (*$1)->getDescription() + "'");
Chris Lattnerc6212f12003-05-21 16:06:56 +00001655
1656 if (STy->getNumContainedTypes() != 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001657 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattnerc6212f12003-05-21 16:06:56 +00001658
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001659 // Check to ensure that Type is not packed
1660 if (STy->isPacked())
Chris Lattner4989b842007-04-26 05:30:35 +00001661 GEN_ERROR("Unpacked Initializer to vector type '" +
1662 STy->getDescription() + "'");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001663
1664 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1665 delete $1;
1666 CHECK_FOR_ERROR
1667 }
1668 | Types '<' '{' ConstVector '}' '>' {
1669 const StructType *STy = dyn_cast<StructType>($1->get());
1670 if (STy == 0)
1671 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001672 (*$1)->getDescription() + "'");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001673
1674 if ($4->size() != STy->getNumContainedTypes())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001675 GEN_ERROR("Illegal number of initializers for structure type");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001676
1677 // Check to ensure that constants are compatible with the type initializer!
1678 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1679 if ((*$4)[i]->getType() != STy->getElementType(i))
1680 GEN_ERROR("Expected type '" +
1681 STy->getElementType(i)->getDescription() +
1682 "' for element #" + utostr(i) +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001683 " of structure initializer");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001684
1685 // Check to ensure that Type is packed
1686 if (!STy->isPacked())
Reid Spencerac9dcb92007-02-15 03:39:18 +00001687 GEN_ERROR("Vector initializer to non-vector type '" +
1688 STy->getDescription() + "'");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001689
1690 $$ = ConstantStruct::get(STy, *$4);
1691 delete $1; delete $4;
1692 CHECK_FOR_ERROR
1693 }
1694 | Types '<' '{' '}' '>' {
1695 if (!UpRefs.empty())
1696 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1697 const StructType *STy = dyn_cast<StructType>($1->get());
1698 if (STy == 0)
1699 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001700 (*$1)->getDescription() + "'");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001701
1702 if (STy->getNumContainedTypes() != 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001703 GEN_ERROR("Illegal number of initializers for structure type");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001704
1705 // Check to ensure that Type is packed
1706 if (!STy->isPacked())
Reid Spencerac9dcb92007-02-15 03:39:18 +00001707 GEN_ERROR("Vector initializer to non-vector type '" +
1708 STy->getDescription() + "'");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001709
Reid Spencer08de34b2006-12-03 05:45:44 +00001710 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1711 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001712 CHECK_FOR_ERROR
Chris Lattnerc6212f12003-05-21 16:06:56 +00001713 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001714 | Types NULL_TOK {
Reid Spencere1553cc2006-12-31 05:40:12 +00001715 if (!UpRefs.empty())
1716 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001717 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001718 if (PTy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001719 GEN_ERROR("Cannot make null pointer constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001720 (*$1)->getDescription() + "'");
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001721
Reid Spencer08de34b2006-12-03 05:45:44 +00001722 $$ = ConstantPointerNull::get(PTy);
1723 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001724 CHECK_FOR_ERROR
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001725 }
Chris Lattner16710e92004-10-16 18:17:13 +00001726 | Types UNDEF {
Reid Spencere1553cc2006-12-31 05:40:12 +00001727 if (!UpRefs.empty())
1728 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001729 $$ = UndefValue::get($1->get());
1730 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001731 CHECK_FOR_ERROR
Chris Lattner16710e92004-10-16 18:17:13 +00001732 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001733 | Types SymbolicValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00001734 if (!UpRefs.empty())
1735 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001736 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001737 if (Ty == 0)
Devang Pateld6ffcf92008-02-19 22:26:37 +00001738 GEN_ERROR("Global const reference must be a pointer type " + (*$1)->getDescription());
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001739
Chris Lattner3101c252002-08-15 17:58:33 +00001740 // ConstExprs can exist in the body of a function, thus creating
Reid Spencer3271ed52004-07-18 00:08:11 +00001741 // GlobalValues whenever they refer to a variable. Because we are in
Reid Spencer186a43f2007-03-19 18:39:36 +00001742 // the context of a function, getExistingVal will search the functions
Chris Lattner3101c252002-08-15 17:58:33 +00001743 // symbol table instead of the module symbol table for the global symbol,
1744 // which throws things all off. To get around this, we just tell
Reid Spencer186a43f2007-03-19 18:39:36 +00001745 // getExistingVal that we are at global scope here.
Chris Lattner3101c252002-08-15 17:58:33 +00001746 //
Chris Lattner394f2eb2003-10-16 20:12:13 +00001747 Function *SavedCurFn = CurFun.CurrentFunction;
1748 CurFun.CurrentFunction = 0;
Chris Lattner3101c252002-08-15 17:58:33 +00001749
Reid Spencer186a43f2007-03-19 18:39:36 +00001750 Value *V = getExistingVal(Ty, $2);
Reid Spencer5b7e7532006-09-28 19:28:24 +00001751 CHECK_FOR_ERROR
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001752
Chris Lattner394f2eb2003-10-16 20:12:13 +00001753 CurFun.CurrentFunction = SavedCurFn;
Chris Lattner3101c252002-08-15 17:58:33 +00001754
Chris Lattner2079fde2001-10-13 06:41:08 +00001755 // If this is an initializer for a constant pointer, which is referencing a
1756 // (currently) undefined variable, create a stub now that shall be replaced
1757 // in the future with the right type of variable.
1758 //
1759 if (V == 0) {
Reid Spencer1755a9a2007-02-05 17:01:20 +00001760 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001761 const PointerType *PT = cast<PointerType>(Ty);
1762
1763 // First check to see if the forward references value is already created!
1764 PerModuleInfo::GlobalRefsType::iterator I =
Reid Spencer3271ed52004-07-18 00:08:11 +00001765 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Chris Lattner2079fde2001-10-13 06:41:08 +00001766
1767 if (I != CurModule.GlobalRefs.end()) {
Reid Spencer3271ed52004-07-18 00:08:11 +00001768 V = I->second; // Placeholder already exists, use it...
Chris Lattnera989b232003-12-23 20:05:15 +00001769 $2.destroy();
Chris Lattner2079fde2001-10-13 06:41:08 +00001770 } else {
Chris Lattner8a327842004-07-14 21:44:00 +00001771 std::string Name;
Reid Spencerb2d17862007-01-26 08:04:51 +00001772 if ($2.Type == ValID::GlobalName)
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001773 Name = $2.getName();
Reid Spencerb2d17862007-01-26 08:04:51 +00001774 else if ($2.Type != ValID::GlobalID)
1775 GEN_ERROR("Invalid reference to global");
Chris Lattner8a327842004-07-14 21:44:00 +00001776
Reid Spencer3271ed52004-07-18 00:08:11 +00001777 // Create the forward referenced global.
Chris Lattnera09000d2004-07-14 23:03:46 +00001778 GlobalValue *GV;
1779 if (const FunctionType *FTy =
1780 dyn_cast<FunctionType>(PT->getElementType())) {
Gabor Greif051a9502008-04-06 20:25:17 +00001781 GV = Function::Create(FTy, GlobalValue::ExternalWeakLinkage, Name,
1782 CurModule.CurrentModule);
Chris Lattnera09000d2004-07-14 23:03:46 +00001783 } else {
Reid Spencer3271ed52004-07-18 00:08:11 +00001784 GV = new GlobalVariable(PT->getElementType(), false,
Chris Lattner4989b842007-04-26 05:30:35 +00001785 GlobalValue::ExternalWeakLinkage, 0,
Chris Lattnera09000d2004-07-14 23:03:46 +00001786 Name, CurModule.CurrentModule);
1787 }
Chris Lattner8a327842004-07-14 21:44:00 +00001788
Reid Spencer3271ed52004-07-18 00:08:11 +00001789 // Keep track of the fact that we have a forward ref to recycle it
1790 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1791 V = GV;
Chris Lattner2079fde2001-10-13 06:41:08 +00001792 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001793 }
1794
Reid Spencer08de34b2006-12-03 05:45:44 +00001795 $$ = cast<GlobalValue>(V);
1796 delete $1; // Free the type handle
Reid Spencer61c83e02006-08-18 08:43:06 +00001797 CHECK_FOR_ERROR
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001798 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001799 | Types ConstExpr {
Reid Spencere1553cc2006-12-31 05:40:12 +00001800 if (!UpRefs.empty())
1801 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001802 if ($1->get() != $2->getType())
Reid Spencerb4fdfdb2007-01-04 00:05:48 +00001803 GEN_ERROR("Mismatched types for constant expression: " +
1804 (*$1)->getDescription() + " and " + $2->getType()->getDescription());
Chris Lattnerd78700d2002-08-16 21:14:40 +00001805 $$ = $2;
Reid Spencer08de34b2006-12-03 05:45:44 +00001806 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001807 CHECK_FOR_ERROR
Chris Lattnerf4600482003-06-28 20:01:34 +00001808 }
1809 | Types ZEROINITIALIZER {
Reid Spencere1553cc2006-12-31 05:40:12 +00001810 if (!UpRefs.empty())
1811 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001812 const Type *Ty = $1->get();
Chris Lattner78915932004-11-28 16:45:45 +00001813 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001814 GEN_ERROR("Cannot create a null initialized value of this type");
Reid Spencer08de34b2006-12-03 05:45:44 +00001815 $$ = Constant::getNullValue(Ty);
1816 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001817 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00001818 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001819 | IntType ESINT64VAL { // integral constants
Reid Spencer9ffad0a2006-12-20 17:20:09 +00001820 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001821 GEN_ERROR("Constant value doesn't fit in type");
Reid Spencereac65742007-03-19 20:40:22 +00001822 $$ = ConstantInt::get($1, $2, true);
Reid Spencerf2310042007-02-28 02:23:44 +00001823 CHECK_FOR_ERROR
1824 }
1825 | IntType ESAPINTVAL { // arbitrary precision integer constants
1826 uint32_t BitWidth = cast<IntegerType>($1)->getBitWidth();
1827 if ($2->getBitWidth() > BitWidth) {
1828 GEN_ERROR("Constant value does not fit in type");
Reid Spenceraf1aacd2007-03-01 19:32:01 +00001829 }
1830 $2->sextOrTrunc(BitWidth);
1831 $$ = ConstantInt::get(*$2);
Reid Spencerf2310042007-02-28 02:23:44 +00001832 delete $2;
Reid Spencer9ffad0a2006-12-20 17:20:09 +00001833 CHECK_FOR_ERROR
1834 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001835 | IntType EUINT64VAL { // integral constants
Reid Spencer9ffad0a2006-12-20 17:20:09 +00001836 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001837 GEN_ERROR("Constant value doesn't fit in type");
Reid Spencereac65742007-03-19 20:40:22 +00001838 $$ = ConstantInt::get($1, $2, false);
Reid Spencerf2310042007-02-28 02:23:44 +00001839 CHECK_FOR_ERROR
1840 }
1841 | IntType EUAPINTVAL { // arbitrary precision integer constants
1842 uint32_t BitWidth = cast<IntegerType>($1)->getBitWidth();
1843 if ($2->getBitWidth() > BitWidth) {
1844 GEN_ERROR("Constant value does not fit in type");
Reid Spenceraf1aacd2007-03-01 19:32:01 +00001845 }
1846 $2->zextOrTrunc(BitWidth);
1847 $$ = ConstantInt::get(*$2);
Reid Spencerf2310042007-02-28 02:23:44 +00001848 delete $2;
Reid Spencer9ffad0a2006-12-20 17:20:09 +00001849 CHECK_FOR_ERROR
1850 }
Reid Spencer8088e9d2007-01-13 05:00:20 +00001851 | INTTYPE TRUETOK { // Boolean constants
1852 assert(cast<IntegerType>($1)->getBitWidth() == 1 && "Not Bool?");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001853 $$ = ConstantInt::getTrue();
Reid Spencer61c83e02006-08-18 08:43:06 +00001854 CHECK_FOR_ERROR
Chris Lattnerd05e3592002-08-15 18:17:28 +00001855 }
Reid Spencer8088e9d2007-01-13 05:00:20 +00001856 | INTTYPE FALSETOK { // Boolean constants
1857 assert(cast<IntegerType>($1)->getBitWidth() == 1 && "Not Bool?");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001858 $$ = ConstantInt::getFalse();
Reid Spencer61c83e02006-08-18 08:43:06 +00001859 CHECK_FOR_ERROR
Chris Lattnerd05e3592002-08-15 18:17:28 +00001860 }
Dale Johannesen9d5f4562007-09-12 03:30:33 +00001861 | FPType FPVAL { // Floating point constants
Dale Johannesen43421b32007-09-06 18:13:44 +00001862 if (!ConstantFP::isValueValidForType($1, *$2))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001863 GEN_ERROR("Floating point constant invalid for type");
Dale Johannesen3f6eb742007-09-11 18:32:33 +00001864 // Lexer has no type info, so builds all float and double FP constants
1865 // as double. Fix this here. Long double is done right.
1866 if (&$2->getSemantics()==&APFloat::IEEEdouble && $1==Type::FloatTy)
Dale Johannesen43421b32007-09-06 18:13:44 +00001867 $2->convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven);
Chris Lattner02a260a2008-04-20 00:41:09 +00001868 $$ = ConstantFP::get(*$2);
Dale Johannesencdd509a2007-09-07 21:07:57 +00001869 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001870 CHECK_FOR_ERROR
Chris Lattnerd05e3592002-08-15 18:17:28 +00001871 };
1872
Chris Lattner00950542001-06-06 20:29:01 +00001873
Reid Spencer3da59db2006-11-27 01:05:10 +00001874ConstExpr: CastOps '(' ConstVal TO Types ')' {
Reid Spencere1553cc2006-12-31 05:40:12 +00001875 if (!UpRefs.empty())
1876 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001877 Constant *Val = $3;
Reid Spencer93947c32007-01-17 02:47:33 +00001878 const Type *DestTy = $5->get();
1879 if (!CastInst::castIsValid($1, $3, DestTy))
1880 GEN_ERROR("invalid cast opcode for cast from '" +
1881 Val->getType()->getDescription() + "' to '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001882 DestTy->getDescription() + "'");
Reid Spencer93947c32007-01-17 02:47:33 +00001883 $$ = ConstantExpr::getCast($1, $3, DestTy);
Reid Spencer08de34b2006-12-03 05:45:44 +00001884 delete $5;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001885 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001886 | GETELEMENTPTR '(' ConstVal IndexList ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001887 if (!isa<PointerType>($3->getType()))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001888 GEN_ERROR("GetElementPtr requires a pointer operand");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001889
Reid Spencer08de34b2006-12-03 05:45:44 +00001890 const Type *IdxTy =
David Greeneb8f74792007-09-04 15:46:09 +00001891 GetElementPtrInst::getIndexedType($3->getType(), $4->begin(), $4->end(),
Chris Lattnerc856c7a2007-02-13 00:57:40 +00001892 true);
Reid Spencer08de34b2006-12-03 05:45:44 +00001893 if (!IdxTy)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001894 GEN_ERROR("Index list invalid for constant getelementptr");
Reid Spencer08de34b2006-12-03 05:45:44 +00001895
Chris Lattnere0135402007-01-31 04:43:46 +00001896 SmallVector<Constant*, 8> IdxVec;
Reid Spencer08de34b2006-12-03 05:45:44 +00001897 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1898 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001899 IdxVec.push_back(C);
1900 else
Reid Spencerf4fa5902007-02-05 10:16:10 +00001901 GEN_ERROR("Indices to constant getelementptr must be constants");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001902
Chris Lattnerd78700d2002-08-16 21:14:40 +00001903 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001904
Chris Lattnere0135402007-01-31 04:43:46 +00001905 $$ = ConstantExpr::getGetElementPtr($3, &IdxVec[0], IdxVec.size());
Reid Spencer61c83e02006-08-18 08:43:06 +00001906 CHECK_FOR_ERROR
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001907 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001908 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer4fe16d62007-01-11 18:21:29 +00001909 if ($3->getType() != Type::Int1Ty)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001910 GEN_ERROR("Select condition must be of boolean type");
Reid Spencer08de34b2006-12-03 05:45:44 +00001911 if ($5->getType() != $7->getType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001912 GEN_ERROR("Select operand types must match");
Reid Spencer08de34b2006-12-03 05:45:44 +00001913 $$ = ConstantExpr::getSelect($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001914 CHECK_FOR_ERROR
Chris Lattner76f0ff32004-03-12 05:51:36 +00001915 }
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001916 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001917 if ($3->getType() != $5->getType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001918 GEN_ERROR("Binary operator types must match");
Reid Spencer1628cec2006-10-26 06:15:43 +00001919 CHECK_FOR_ERROR;
Reid Spencerc6e956e2006-12-05 19:15:41 +00001920 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001921 }
1922 | LogicalOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001923 if ($3->getType() != $5->getType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001924 GEN_ERROR("Logical operator types must match");
Chris Lattner42a75512007-01-15 02:27:26 +00001925 if (!$3->getType()->isInteger()) {
Reid Spencer9d6565a2007-02-15 02:26:10 +00001926 if (Instruction::isShift($1) || !isa<VectorType>($3->getType()) ||
1927 !cast<VectorType>($3->getType())->getElementType()->isInteger())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001928 GEN_ERROR("Logical operator requires integral operands");
Chris Lattner0a017832005-12-21 18:31:29 +00001929 }
Reid Spencer08de34b2006-12-03 05:45:44 +00001930 $$ = ConstantExpr::get($1, $3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001931 CHECK_FOR_ERROR
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001932 }
Reid Spencer763ed5e2006-12-04 05:20:06 +00001933 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
1934 if ($4->getType() != $6->getType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001935 GEN_ERROR("icmp operand types must match");
Reid Spencer763ed5e2006-12-04 05:20:06 +00001936 $$ = ConstantExpr::getICmp($2, $4, $6);
Reid Spencer08de34b2006-12-03 05:45:44 +00001937 }
Reid Spencer763ed5e2006-12-04 05:20:06 +00001938 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
1939 if ($4->getType() != $6->getType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001940 GEN_ERROR("fcmp operand types must match");
Reid Spencer763ed5e2006-12-04 05:20:06 +00001941 $$ = ConstantExpr::getFCmp($2, $4, $6);
Reid Spencer08de34b2006-12-03 05:45:44 +00001942 }
Robert Bocchino9c62b562006-01-10 19:04:32 +00001943 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001944 if (!ExtractElementInst::isValidOperands($3, $5))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001945 GEN_ERROR("Invalid extractelement operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00001946 $$ = ConstantExpr::getExtractElement($3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001947 CHECK_FOR_ERROR
Chris Lattnerca73cd82006-04-08 03:53:34 +00001948 }
1949 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001950 if (!InsertElementInst::isValidOperands($3, $5, $7))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001951 GEN_ERROR("Invalid insertelement operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00001952 $$ = ConstantExpr::getInsertElement($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001953 CHECK_FOR_ERROR
Chris Lattnerca73cd82006-04-08 03:53:34 +00001954 }
1955 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001956 if (!ShuffleVectorInst::isValidOperands($3, $5, $7))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001957 GEN_ERROR("Invalid shufflevector operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00001958 $$ = ConstantExpr::getShuffleVector($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001959 CHECK_FOR_ERROR
Chris Lattner699f1eb2002-08-14 17:12:33 +00001960 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001961
Chris Lattnerca73cd82006-04-08 03:53:34 +00001962
Misha Brukmanbc0e9982003-07-14 17:20:40 +00001963// ConstVector - A list of comma separated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001964ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001965 ($$ = $1)->push_back($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00001966 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001967 }
1968 | ConstVal {
Reid Spencer08de34b2006-12-03 05:45:44 +00001969 $$ = new std::vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001970 $$->push_back($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001971 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001972 };
Chris Lattner00950542001-06-06 20:29:01 +00001973
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001974
Chris Lattner1781aca2001-09-18 04:00:54 +00001975// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001976GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001977
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00001978// ThreadLocal
1979ThreadLocal : THREAD_LOCAL { $$ = true; } | { $$ = false; };
1980
Anton Korobeynikova80e1182007-04-28 13:45:00 +00001981// AliaseeRef - Match either GlobalValue or bitcast to GlobalValue.
1982AliaseeRef : ResultTypes SymbolicValueRef {
1983 const Type* VTy = $1->get();
1984 Value *V = getVal(VTy, $2);
Chris Lattner6df20ef2007-08-06 21:00:37 +00001985 CHECK_FOR_ERROR
Anton Korobeynikova80e1182007-04-28 13:45:00 +00001986 GlobalValue* Aliasee = dyn_cast<GlobalValue>(V);
1987 if (!Aliasee)
1988 GEN_ERROR("Aliases can be created only to global values");
1989
1990 $$ = Aliasee;
1991 CHECK_FOR_ERROR
1992 delete $1;
1993 }
1994 | BITCAST '(' AliaseeRef TO Types ')' {
1995 Constant *Val = $3;
1996 const Type *DestTy = $5->get();
1997 if (!CastInst::castIsValid($1, $3, DestTy))
1998 GEN_ERROR("invalid cast opcode for cast from '" +
1999 Val->getType()->getDescription() + "' to '" +
2000 DestTy->getDescription() + "'");
2001
2002 $$ = ConstantExpr::getCast($1, $3, DestTy);
2003 CHECK_FOR_ERROR
2004 delete $5;
2005 };
Chris Lattner00950542001-06-06 20:29:01 +00002006
Chris Lattner0e73ce62002-05-02 19:11:13 +00002007//===----------------------------------------------------------------------===//
2008// Rules to match Modules
2009//===----------------------------------------------------------------------===//
2010
2011// Module rule: Capture the result of parsing the whole file into a result
2012// variable...
2013//
Reid Spencerb951bc02006-12-29 20:29:48 +00002014Module
2015 : DefinitionList {
2016 $$ = ParserResult = CurModule.CurrentModule;
2017 CurModule.ModuleDone();
2018 CHECK_FOR_ERROR;
2019 }
2020 | /*empty*/ {
2021 $$ = ParserResult = CurModule.CurrentModule;
2022 CurModule.ModuleDone();
2023 CHECK_FOR_ERROR;
2024 }
2025 ;
Chris Lattner0e73ce62002-05-02 19:11:13 +00002026
Reid Spencerb951bc02006-12-29 20:29:48 +00002027DefinitionList
2028 : Definition
2029 | DefinitionList Definition
2030 ;
2031
2032Definition
Jeff Cohen361c3ef2007-01-21 19:19:31 +00002033 : DEFINE { CurFun.isDeclare = false; } Function {
Chris Lattner394f2eb2003-10-16 20:12:13 +00002034 CurFun.FunctionDone();
Reid Spencer61c83e02006-08-18 08:43:06 +00002035 CHECK_FOR_ERROR
Reid Spencerb951bc02006-12-29 20:29:48 +00002036 }
2037 | DECLARE { CurFun.isDeclare = true; } FunctionProto {
Reid Spencer61c83e02006-08-18 08:43:06 +00002038 CHECK_FOR_ERROR
Chris Lattner0e73ce62002-05-02 19:11:13 +00002039 }
Reid Spencerb951bc02006-12-29 20:29:48 +00002040 | MODULE ASM_TOK AsmBlock {
Reid Spencer61c83e02006-08-18 08:43:06 +00002041 CHECK_FOR_ERROR
Chris Lattneree454772006-01-23 23:05:15 +00002042 }
Reid Spencerb2d17862007-01-26 08:04:51 +00002043 | OptLocalAssign TYPE Types {
Reid Spencere1553cc2006-12-31 05:40:12 +00002044 if (!UpRefs.empty())
2045 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattner4a42e902001-10-22 05:56:09 +00002046 // Eagerly resolve types. This is not an optimization, this is a
2047 // requirement that is due to the fact that we could have this:
2048 //
2049 // %list = type { %list * }
2050 // %list = type { %list * } ; repeated type decl
2051 //
2052 // If types are not resolved eagerly, then the two types will not be
2053 // determined to be the same type!
2054 //
Reid Spencerb951bc02006-12-29 20:29:48 +00002055 ResolveTypeTo($1, *$3);
Chris Lattner4a42e902001-10-22 05:56:09 +00002056
Reid Spencerb951bc02006-12-29 20:29:48 +00002057 if (!setTypeName(*$3, $1) && !$1) {
Reid Spencer5b7e7532006-09-28 19:28:24 +00002058 CHECK_FOR_ERROR
Chris Lattner8c89a0a2004-07-13 08:10:10 +00002059 // If this is a named type that is not a redefinition, add it to the slot
2060 // table.
Reid Spencerb951bc02006-12-29 20:29:48 +00002061 CurModule.Types.push_back(*$3);
Chris Lattner30c89792001-09-07 16:35:17 +00002062 }
Reid Spencer08de34b2006-12-03 05:45:44 +00002063
Reid Spencerb951bc02006-12-29 20:29:48 +00002064 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002065 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00002066 }
Reid Spencerb2d17862007-01-26 08:04:51 +00002067 | OptLocalAssign TYPE VOID {
Reid Spencere1553cc2006-12-31 05:40:12 +00002068 ResolveTypeTo($1, $3);
2069
2070 if (!setTypeName($3, $1) && !$1) {
2071 CHECK_FOR_ERROR
2072 // If this is a named type that is not a redefinition, add it to the slot
2073 // table.
2074 CurModule.Types.push_back($3);
2075 }
2076 CHECK_FOR_ERROR
2077 }
Christopher Lambd49e18d2007-12-12 08:44:39 +00002078 | OptGlobalAssign GVVisibilityStyle ThreadLocal GlobalType ConstVal
2079 OptAddrSpace {
Reid Spencerb2d17862007-01-26 08:04:51 +00002080 /* "Externally Visible" Linkage */
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002081 if ($5 == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002082 GEN_ERROR("Global value initializer is not a constant");
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002083 CurGV = ParseGlobalVariable($1, GlobalValue::ExternalLinkage,
Christopher Lambd49e18d2007-12-12 08:44:39 +00002084 $2, $4, $5->getType(), $5, $3, $6);
Christopher Lambfe63fb92007-12-11 08:59:05 +00002085 CHECK_FOR_ERROR
2086 } GlobalVarAttributes {
2087 CurGV = 0;
2088 }
Chris Lattner4989b842007-04-26 05:30:35 +00002089 | OptGlobalAssign GVInternalLinkage GVVisibilityStyle ThreadLocal GlobalType
Christopher Lambd49e18d2007-12-12 08:44:39 +00002090 ConstVal OptAddrSpace {
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002091 if ($6 == 0)
2092 GEN_ERROR("Global value initializer is not a constant");
Christopher Lambd49e18d2007-12-12 08:44:39 +00002093 CurGV = ParseGlobalVariable($1, $2, $3, $5, $6->getType(), $6, $4, $7);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002094 CHECK_FOR_ERROR
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002095 } GlobalVarAttributes {
2096 CurGV = 0;
2097 }
Chris Lattner4989b842007-04-26 05:30:35 +00002098 | OptGlobalAssign GVExternalLinkage GVVisibilityStyle ThreadLocal GlobalType
Christopher Lambd49e18d2007-12-12 08:44:39 +00002099 Types OptAddrSpace {
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002100 if (!UpRefs.empty())
2101 GEN_ERROR("Invalid upreference in type: " + (*$6)->getDescription());
Christopher Lambd49e18d2007-12-12 08:44:39 +00002102 CurGV = ParseGlobalVariable($1, $2, $3, $5, *$6, 0, $4, $7);
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002103 CHECK_FOR_ERROR
2104 delete $6;
Reid Spencer5b7e7532006-09-28 19:28:24 +00002105 } GlobalVarAttributes {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002106 CurGV = 0;
2107 CHECK_FOR_ERROR
2108 }
Anton Korobeynikova80e1182007-04-28 13:45:00 +00002109 | OptGlobalAssign GVVisibilityStyle ALIAS AliasLinkage AliaseeRef {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002110 std::string Name;
2111 if ($1) {
2112 Name = *$1;
2113 delete $1;
2114 }
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00002115 if (Name.empty())
Anton Korobeynikova80e1182007-04-28 13:45:00 +00002116 GEN_ERROR("Alias name cannot be empty");
2117
2118 Constant* Aliasee = $5;
2119 if (Aliasee == 0)
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002120 GEN_ERROR(std::string("Invalid aliasee for alias: ") + Name);
Anton Korobeynikova80e1182007-04-28 13:45:00 +00002121
2122 GlobalAlias* GA = new GlobalAlias(Aliasee->getType(), $4, Name, Aliasee,
2123 CurModule.CurrentModule);
2124 GA->setVisibility($2);
2125 InsertValue(GA, CurModule.Values);
Chris Lattnere5434242007-09-10 23:23:53 +00002126
2127
2128 // If there was a forward reference of this alias, resolve it now.
2129
2130 ValID ID;
2131 if (!Name.empty())
2132 ID = ValID::createGlobalName(Name);
2133 else
2134 ID = ValID::createGlobalID(CurModule.Values.size()-1);
2135
2136 if (GlobalValue *FWGV =
2137 CurModule.GetForwardRefForGlobal(GA->getType(), ID)) {
2138 // Replace uses of the fwdref with the actual alias.
2139 FWGV->replaceAllUsesWith(GA);
2140 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(FWGV))
2141 GV->eraseFromParent();
2142 else
2143 cast<Function>(FWGV)->eraseFromParent();
2144 }
2145 ID.destroy();
2146
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00002147 CHECK_FOR_ERROR
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00002148 }
Reid Spencerb951bc02006-12-29 20:29:48 +00002149 | TARGET TargetDefinition {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002150 CHECK_FOR_ERROR
2151 }
Reid Spencerb951bc02006-12-29 20:29:48 +00002152 | DEPLIBS '=' LibrariesDefinition {
Reid Spencer61c83e02006-08-18 08:43:06 +00002153 CHECK_FOR_ERROR
Chris Lattnere98dda62001-07-14 06:10:16 +00002154 }
Reid Spencerb951bc02006-12-29 20:29:48 +00002155 ;
Chris Lattner00950542001-06-06 20:29:01 +00002156
2157
Chris Lattneree454772006-01-23 23:05:15 +00002158AsmBlock : STRINGCONSTANT {
Chris Lattner66316012006-01-24 04:14:29 +00002159 const std::string &AsmSoFar = CurModule.CurrentModule->getModuleInlineAsm();
Chris Lattneree454772006-01-23 23:05:15 +00002160 if (AsmSoFar.empty())
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002161 CurModule.CurrentModule->setModuleInlineAsm(*$1);
Chris Lattneree454772006-01-23 23:05:15 +00002162 else
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002163 CurModule.CurrentModule->setModuleInlineAsm(AsmSoFar+"\n"+*$1);
2164 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002165 CHECK_FOR_ERROR
Chris Lattneree454772006-01-23 23:05:15 +00002166};
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00002167
Reid Spencerb2d17862007-01-26 08:04:51 +00002168TargetDefinition : TRIPLE '=' STRINGCONSTANT {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002169 CurModule.CurrentModule->setTargetTriple(*$3);
2170 delete $3;
John Criswell2f6a8b12006-10-24 19:09:48 +00002171 }
Chris Lattner10b27112006-10-22 06:07:41 +00002172 | DATALAYOUT '=' STRINGCONSTANT {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002173 CurModule.CurrentModule->setDataLayout(*$3);
2174 delete $3;
Owen Andersoncf7ff2b2006-10-18 02:21:12 +00002175 };
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00002176
Reid Spencerfeaf10e2004-07-25 21:30:51 +00002177LibrariesDefinition : '[' LibList ']';
Reid Spencer0be13e72004-07-25 17:58:28 +00002178
2179LibList : LibList ',' STRINGCONSTANT {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002180 CurModule.CurrentModule->addLibrary(*$3);
2181 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002182 CHECK_FOR_ERROR
Reid Spencer0be13e72004-07-25 17:58:28 +00002183 }
2184 | STRINGCONSTANT {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002185 CurModule.CurrentModule->addLibrary(*$1);
2186 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002187 CHECK_FOR_ERROR
Reid Spencer0be13e72004-07-25 17:58:28 +00002188 }
2189 | /* empty: end of list */ {
Reid Spencer61c83e02006-08-18 08:43:06 +00002190 CHECK_FOR_ERROR
Reid Spencer0be13e72004-07-25 17:58:28 +00002191 }
2192 ;
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00002193
Chris Lattner00950542001-06-06 20:29:01 +00002194//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00002195// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00002196//===----------------------------------------------------------------------===//
2197
Reid Spencerb2d17862007-01-26 08:04:51 +00002198ArgListH : ArgListH ',' Types OptParamAttrs OptLocalName {
Reid Spencere1553cc2006-12-31 05:40:12 +00002199 if (!UpRefs.empty())
2200 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
2201 if (*$3 == Type::VoidTy)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002202 GEN_ERROR("void typed arguments are invalid");
Reid Spencere1553cc2006-12-31 05:40:12 +00002203 ArgListEntry E; E.Attrs = $4; E.Ty = $3; E.Name = $5;
Chris Lattner69da5cf2002-10-13 20:57:00 +00002204 $$ = $1;
Reid Spencere1553cc2006-12-31 05:40:12 +00002205 $1->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002206 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002207 }
Reid Spencerb2d17862007-01-26 08:04:51 +00002208 | Types OptParamAttrs OptLocalName {
Reid Spencere1553cc2006-12-31 05:40:12 +00002209 if (!UpRefs.empty())
2210 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2211 if (*$1 == Type::VoidTy)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002212 GEN_ERROR("void typed arguments are invalid");
Reid Spencere1553cc2006-12-31 05:40:12 +00002213 ArgListEntry E; E.Attrs = $2; E.Ty = $1; E.Name = $3;
2214 $$ = new ArgListType;
2215 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002216 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002217 };
Chris Lattner00950542001-06-06 20:29:01 +00002218
2219ArgList : ArgListH {
2220 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002221 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002222 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00002223 | ArgListH ',' DOTDOTDOT {
2224 $$ = $1;
Reid Spencere1553cc2006-12-31 05:40:12 +00002225 struct ArgListEntry E;
2226 E.Ty = new PATypeHolder(Type::VoidTy);
2227 E.Name = 0;
Reid Spencer18da0722007-04-11 02:44:20 +00002228 E.Attrs = ParamAttr::None;
Reid Spencere1553cc2006-12-31 05:40:12 +00002229 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002230 CHECK_FOR_ERROR
Chris Lattner69da5cf2002-10-13 20:57:00 +00002231 }
2232 | DOTDOTDOT {
Reid Spencere1553cc2006-12-31 05:40:12 +00002233 $$ = new ArgListType;
2234 struct ArgListEntry E;
2235 E.Ty = new PATypeHolder(Type::VoidTy);
2236 E.Name = 0;
Reid Spencer18da0722007-04-11 02:44:20 +00002237 E.Attrs = ParamAttr::None;
Reid Spencere1553cc2006-12-31 05:40:12 +00002238 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002239 CHECK_FOR_ERROR
Chris Lattner69da5cf2002-10-13 20:57:00 +00002240 }
Chris Lattner00950542001-06-06 20:29:01 +00002241 | /* empty */ {
2242 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00002243 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002244 };
Chris Lattner00950542001-06-06 20:29:01 +00002245
Reid Spencerb2d17862007-01-26 08:04:51 +00002246FunctionHeaderH : OptCallingConv ResultTypes GlobalName '(' ArgList ')'
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00002247 OptFuncAttrs OptSection OptAlign OptGC {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002248 std::string FunctionName(*$3);
2249 delete $3; // Free strdup'd memory!
Chris Lattnerdda71962001-11-26 18:54:16 +00002250
Reid Spencer98b3c5c2007-01-02 21:53:43 +00002251 // Check the function result for abstractness if this is a define. We should
2252 // have no abstract types at this point
Reid Spencer2c261782007-01-05 17:06:19 +00002253 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved($2))
2254 GEN_ERROR("Reference to abstract result: "+ $2->get()->getDescription());
Reid Spencer98b3c5c2007-01-02 21:53:43 +00002255
Chris Lattnerf7dedf22008-04-23 05:36:58 +00002256 if (!FunctionType::isValidReturnType(*$2))
2257 GEN_ERROR("Invalid result type for LLVM function");
2258
Chris Lattner9232b992003-04-22 18:42:41 +00002259 std::vector<const Type*> ParamTypeList;
Chris Lattner58d74912008-03-12 17:45:29 +00002260 SmallVector<ParamAttrsWithIndex, 8> Attrs;
2261 if ($7 != ParamAttr::None)
2262 Attrs.push_back(ParamAttrsWithIndex::get(0, $7));
Chris Lattnera8e8f162005-05-06 20:27:19 +00002263 if ($5) { // If there are arguments...
Reid Spencer5694b6e2007-04-09 06:17:21 +00002264 unsigned index = 1;
2265 for (ArgListType::iterator I = $5->begin(); I != $5->end(); ++I, ++index) {
Reid Spencere1553cc2006-12-31 05:40:12 +00002266 const Type* Ty = I->Ty->get();
Reid Spencer98b3c5c2007-01-02 21:53:43 +00002267 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved(I->Ty))
2268 GEN_ERROR("Reference to abstract argument: " + Ty->getDescription());
Reid Spencere1553cc2006-12-31 05:40:12 +00002269 ParamTypeList.push_back(Ty);
Chris Lattner58d74912008-03-12 17:45:29 +00002270 if (Ty != Type::VoidTy && I->Attrs != ParamAttr::None)
2271 Attrs.push_back(ParamAttrsWithIndex::get(index, I->Attrs));
Reid Spencere1553cc2006-12-31 05:40:12 +00002272 }
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002273 }
Chris Lattner00950542001-06-06 20:29:01 +00002274
Chris Lattner2079fde2001-10-13 06:41:08 +00002275 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
2276 if (isVarArg) ParamTypeList.pop_back();
2277
Chris Lattner58d74912008-03-12 17:45:29 +00002278 PAListPtr PAL;
Reid Spencer4f859aa2007-04-22 05:46:44 +00002279 if (!Attrs.empty())
Chris Lattner58d74912008-03-12 17:45:29 +00002280 PAL = PAListPtr::get(Attrs.begin(), Attrs.end());
Reid Spencer5694b6e2007-04-09 06:17:21 +00002281
Duncan Sandsdc024672007-11-27 13:23:08 +00002282 FunctionType *FT = FunctionType::get(*$2, ParamTypeList, isVarArg);
Christopher Lamb43ad6b32007-12-17 01:12:55 +00002283 const PointerType *PFT = PointerType::getUnqual(FT);
Reid Spencer2c261782007-01-05 17:06:19 +00002284 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002285
Chris Lattnera09000d2004-07-14 23:03:46 +00002286 ValID ID;
2287 if (!FunctionName.empty()) {
Reid Spencerb2d17862007-01-26 08:04:51 +00002288 ID = ValID::createGlobalName((char*)FunctionName.c_str());
Chris Lattnera09000d2004-07-14 23:03:46 +00002289 } else {
Reid Spencer186a43f2007-03-19 18:39:36 +00002290 ID = ValID::createGlobalID(CurModule.Values.size());
Chris Lattnera09000d2004-07-14 23:03:46 +00002291 }
2292
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00002293 Function *Fn = 0;
Chris Lattnera09000d2004-07-14 23:03:46 +00002294 // See if this function was forward referenced. If so, recycle the object.
Reid Spencer863dd882007-04-28 16:06:50 +00002295 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
Chris Lattnera09000d2004-07-14 23:03:46 +00002296 // Move the function to the end of the list, from whereever it was
2297 // previously inserted.
2298 Fn = cast<Function>(FWRef);
Chris Lattner58d74912008-03-12 17:45:29 +00002299 assert(Fn->getParamAttrs().isEmpty() &&
2300 "Forward reference has parameter attributes!");
Chris Lattnera09000d2004-07-14 23:03:46 +00002301 CurModule.CurrentModule->getFunctionList().remove(Fn);
2302 CurModule.CurrentModule->getFunctionList().push_back(Fn);
2303 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
Reid Spenceref9b9a72007-02-05 20:47:22 +00002304 (Fn = CurModule.CurrentModule->getFunction(FunctionName))) {
Duncan Sandsdc024672007-11-27 13:23:08 +00002305 if (Fn->getFunctionType() != FT ) {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002306 // The existing function doesn't have the same type. This is an overload
2307 // error.
2308 GEN_ERROR("Overload of function '" + FunctionName + "' not permitted.");
Duncan Sandsdc024672007-11-27 13:23:08 +00002309 } else if (Fn->getParamAttrs() != PAL) {
2310 // The existing function doesn't have the same parameter attributes.
2311 // This is an overload error.
2312 GEN_ERROR("Overload of function '" + FunctionName + "' not permitted.");
Reid Spenceref9b9a72007-02-05 20:47:22 +00002313 } else if (!CurFun.isDeclare && !Fn->isDeclaration()) {
2314 // Neither the existing or the current function is a declaration and they
2315 // have the same name and same type. Clearly this is a redefinition.
Reid Spencerf4fa5902007-02-05 10:16:10 +00002316 GEN_ERROR("Redefinition of function '" + FunctionName + "'");
Duncan Sandsdc024672007-11-27 13:23:08 +00002317 } else if (Fn->isDeclaration()) {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002318 // Make sure to strip off any argument names so we can't get conflicts.
Chris Lattnere4d5c442005-03-15 04:54:21 +00002319 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
Chris Lattnera09000d2004-07-14 23:03:46 +00002320 AI != AE; ++AI)
2321 AI->setName("");
Reid Spenceref9b9a72007-02-05 20:47:22 +00002322 }
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00002323 } else { // Not already defined?
Gabor Greif051a9502008-04-06 20:25:17 +00002324 Fn = Function::Create(FT, GlobalValue::ExternalWeakLinkage, FunctionName,
2325 CurModule.CurrentModule);
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00002326 InsertValue(Fn, CurModule.Values);
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00002327 }
Chris Lattner00950542001-06-06 20:29:01 +00002328
Chris Lattner394f2eb2003-10-16 20:12:13 +00002329 CurFun.FunctionStart(Fn);
Anton Korobeynikov93c2b372006-09-17 13:06:18 +00002330
2331 if (CurFun.isDeclare) {
2332 // If we have declaration, always overwrite linkage. This will allow us to
2333 // correctly handle cases, when pointer to function is passed as argument to
2334 // another function.
2335 Fn->setLinkage(CurFun.Linkage);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002336 Fn->setVisibility(CurFun.Visibility);
Anton Korobeynikov93c2b372006-09-17 13:06:18 +00002337 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00002338 Fn->setCallingConv($1);
Duncan Sandsdc024672007-11-27 13:23:08 +00002339 Fn->setParamAttrs(PAL);
Reid Spencer2c261782007-01-05 17:06:19 +00002340 Fn->setAlignment($9);
2341 if ($8) {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002342 Fn->setSection(*$8);
2343 delete $8;
Chris Lattner164c3782005-11-12 00:11:10 +00002344 }
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00002345 if ($10) {
2346 Fn->setCollector($10->c_str());
2347 delete $10;
2348 }
Chris Lattner00950542001-06-06 20:29:01 +00002349
Chris Lattner7e708292002-06-25 16:13:24 +00002350 // Add all of the arguments we parsed to the function...
Chris Lattnera8e8f162005-05-06 20:27:19 +00002351 if ($5) { // Is null if empty...
Chris Lattner69da5cf2002-10-13 20:57:00 +00002352 if (isVarArg) { // Nuke the last entry
Reid Spenceref9b9a72007-02-05 20:47:22 +00002353 assert($5->back().Ty->get() == Type::VoidTy && $5->back().Name == 0 &&
Reid Spencer1755a9a2007-02-05 17:01:20 +00002354 "Not a varargs marker!");
Reid Spencere1553cc2006-12-31 05:40:12 +00002355 delete $5->back().Ty;
Chris Lattnera8e8f162005-05-06 20:27:19 +00002356 $5->pop_back(); // Delete the last entry
Chris Lattner69da5cf2002-10-13 20:57:00 +00002357 }
Chris Lattnere4d5c442005-03-15 04:54:21 +00002358 Function::arg_iterator ArgIt = Fn->arg_begin();
Reid Spenceref9b9a72007-02-05 20:47:22 +00002359 Function::arg_iterator ArgEnd = Fn->arg_end();
Reid Spencere1553cc2006-12-31 05:40:12 +00002360 unsigned Idx = 1;
Reid Spenceref9b9a72007-02-05 20:47:22 +00002361 for (ArgListType::iterator I = $5->begin();
2362 I != $5->end() && ArgIt != ArgEnd; ++I, ++ArgIt) {
Reid Spencere1553cc2006-12-31 05:40:12 +00002363 delete I->Ty; // Delete the typeholder...
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002364 setValueName(ArgIt, I->Name); // Insert arg into symtab...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002365 CHECK_FOR_ERROR
Chris Lattner69da5cf2002-10-13 20:57:00 +00002366 InsertValue(ArgIt);
Reid Spencere1553cc2006-12-31 05:40:12 +00002367 Idx++;
Chris Lattner00950542001-06-06 20:29:01 +00002368 }
Reid Spencer08de34b2006-12-03 05:45:44 +00002369
Chris Lattnera8e8f162005-05-06 20:27:19 +00002370 delete $5; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00002371 }
Reid Spencer61c83e02006-08-18 08:43:06 +00002372 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002373};
Chris Lattner00950542001-06-06 20:29:01 +00002374
Chris Lattner9b02cc32002-05-03 18:23:48 +00002375BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
2376
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002377FunctionHeader : FunctionDefineLinkage GVVisibilityStyle FunctionHeaderH BEGIN {
Chris Lattner394f2eb2003-10-16 20:12:13 +00002378 $$ = CurFun.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00002379
Chris Lattner4ad02e72003-04-16 20:28:45 +00002380 // Make sure that we keep track of the linkage type even if there was a
2381 // previous "declare".
2382 $$->setLinkage($1);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002383 $$->setVisibility($2);
Chris Lattner51727be2002-06-04 21:58:56 +00002384};
Chris Lattner00950542001-06-06 20:29:01 +00002385
Chris Lattner9b02cc32002-05-03 18:23:48 +00002386END : ENDTOK | '}'; // Allow end of '}' to end a function
2387
Chris Lattner79df7c02002-03-26 18:01:55 +00002388Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00002389 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002390 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002391};
Chris Lattner00950542001-06-06 20:29:01 +00002392
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002393FunctionProto : FunctionDeclareLinkage GVVisibilityStyle FunctionHeaderH {
Reid Spencere1553cc2006-12-31 05:40:12 +00002394 CurFun.CurrentFunction->setLinkage($1);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002395 CurFun.CurrentFunction->setVisibility($2);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002396 $$ = CurFun.CurrentFunction;
2397 CurFun.FunctionDone();
2398 CHECK_FOR_ERROR
2399 };
Chris Lattner00950542001-06-06 20:29:01 +00002400
2401//===----------------------------------------------------------------------===//
2402// Rules to match Basic Blocks
2403//===----------------------------------------------------------------------===//
2404
Chris Lattneraa2c8532006-01-25 22:26:43 +00002405OptSideEffect : /* empty */ {
2406 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002407 CHECK_FOR_ERROR
Chris Lattneraa2c8532006-01-25 22:26:43 +00002408 }
2409 | SIDEEFFECT {
2410 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002411 CHECK_FOR_ERROR
Chris Lattneraa2c8532006-01-25 22:26:43 +00002412 };
2413
Chris Lattner00950542001-06-06 20:29:01 +00002414ConstValueRef : ESINT64VAL { // A reference to a direct constant
2415 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002416 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002417 }
2418 | EUINT64VAL {
2419 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002420 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002421 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00002422 | FPVAL { // Perhaps it's an FP constant?
2423 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002424 CHECK_FOR_ERROR
Chris Lattner3d52b2f2001-07-15 00:17:01 +00002425 }
Chris Lattner91ef4602004-03-31 03:49:47 +00002426 | TRUETOK {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00002427 $$ = ValID::create(ConstantInt::getTrue());
Reid Spencer61c83e02006-08-18 08:43:06 +00002428 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002429 }
Chris Lattner91ef4602004-03-31 03:49:47 +00002430 | FALSETOK {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00002431 $$ = ValID::create(ConstantInt::getFalse());
Reid Spencer61c83e02006-08-18 08:43:06 +00002432 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002433 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00002434 | NULL_TOK {
2435 $$ = ValID::createNull();
Reid Spencer61c83e02006-08-18 08:43:06 +00002436 CHECK_FOR_ERROR
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002437 }
Chris Lattner16710e92004-10-16 18:17:13 +00002438 | UNDEF {
2439 $$ = ValID::createUndef();
Reid Spencer61c83e02006-08-18 08:43:06 +00002440 CHECK_FOR_ERROR
Chris Lattner16710e92004-10-16 18:17:13 +00002441 }
Chris Lattnerf1f03df2005-12-21 17:53:02 +00002442 | ZEROINITIALIZER { // A vector zero constant.
2443 $$ = ValID::createZeroInit();
Reid Spencer61c83e02006-08-18 08:43:06 +00002444 CHECK_FOR_ERROR
Chris Lattnerf1f03df2005-12-21 17:53:02 +00002445 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00002446 | '<' ConstVector '>' { // Nonempty unsized packed vector
Reid Spencer08de34b2006-12-03 05:45:44 +00002447 const Type *ETy = (*$2)[0]->getType();
Brian Gaeke715c90b2004-08-20 06:00:58 +00002448 int NumElements = $2->size();
2449
Reid Spencer9d6565a2007-02-15 02:26:10 +00002450 VectorType* pt = VectorType::get(ETy, NumElements);
Brian Gaeke715c90b2004-08-20 06:00:58 +00002451 PATypeHolder* PTy = new PATypeHolder(
Reid Spencer08de34b2006-12-03 05:45:44 +00002452 HandleUpRefs(
Reid Spencer9d6565a2007-02-15 02:26:10 +00002453 VectorType::get(
Reid Spencer08de34b2006-12-03 05:45:44 +00002454 ETy,
2455 NumElements)
2456 )
2457 );
Brian Gaeke715c90b2004-08-20 06:00:58 +00002458
2459 // Verify all elements are correct type!
2460 for (unsigned i = 0; i < $2->size(); i++) {
Reid Spencer08de34b2006-12-03 05:45:44 +00002461 if (ETy != (*$2)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00002462 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Brian Gaeke715c90b2004-08-20 06:00:58 +00002463 ETy->getDescription() +"' as required!\nIt is of type '" +
Reid Spencer08de34b2006-12-03 05:45:44 +00002464 (*$2)[i]->getType()->getDescription() + "'.");
Brian Gaeke715c90b2004-08-20 06:00:58 +00002465 }
2466
Reid Spencer9d6565a2007-02-15 02:26:10 +00002467 $$ = ValID::create(ConstantVector::get(pt, *$2));
Brian Gaeke715c90b2004-08-20 06:00:58 +00002468 delete PTy; delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002469 CHECK_FOR_ERROR
Brian Gaeke715c90b2004-08-20 06:00:58 +00002470 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00002471 | ConstExpr {
Reid Spencer08de34b2006-12-03 05:45:44 +00002472 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002473 CHECK_FOR_ERROR
Chris Lattneraa2c8532006-01-25 22:26:43 +00002474 }
2475 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002476 $$ = ValID::createInlineAsm(*$3, *$5, $2);
2477 delete $3;
2478 delete $5;
Reid Spencer61c83e02006-08-18 08:43:06 +00002479 CHECK_FOR_ERROR
Chris Lattnerd78700d2002-08-16 21:14:40 +00002480 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00002481
Chris Lattner2079fde2001-10-13 06:41:08 +00002482// SymbolicValueRef - Reference to one of two ways of symbolically refering to
2483// another value.
2484//
Reid Spencerb2d17862007-01-26 08:04:51 +00002485SymbolicValueRef : LOCALVAL_ID { // Is it an integer reference...?
2486 $$ = ValID::createLocalID($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002487 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002488 }
Reid Spencerb2d17862007-01-26 08:04:51 +00002489 | GLOBALVAL_ID {
2490 $$ = ValID::createGlobalID($1);
2491 CHECK_FOR_ERROR
2492 }
2493 | LocalName { // Is it a named reference...?
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002494 $$ = ValID::createLocalName(*$1);
2495 delete $1;
Reid Spencerb2d17862007-01-26 08:04:51 +00002496 CHECK_FOR_ERROR
2497 }
2498 | GlobalName { // Is it a named reference...?
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002499 $$ = ValID::createGlobalName(*$1);
2500 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002501 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002502 };
Chris Lattner2079fde2001-10-13 06:41:08 +00002503
2504// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00002505ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00002506
Chris Lattner00950542001-06-06 20:29:01 +00002507
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002508// ResolvedVal - a <type> <value> pair. This is used only in cases where the
2509// type immediately preceeds the value reference, and allows complex constant
2510// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00002511ResolvedVal : Types ValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00002512 if (!UpRefs.empty())
2513 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2514 $$ = getVal(*$1, $2);
2515 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002516 CHECK_FOR_ERROR
Reid Spencere1553cc2006-12-31 05:40:12 +00002517 }
2518 ;
Chris Lattner8b81bf52001-07-25 22:47:46 +00002519
Devang Patel5af2f632008-02-20 22:39:45 +00002520ReturnedVal : ResolvedVal {
2521 $$ = new std::vector<Value *>();
2522 $$->push_back($1);
2523 CHECK_FOR_ERROR
2524 }
Devang Patel57ef4f42008-02-23 00:35:18 +00002525 | ReturnedVal ',' ResolvedVal {
Devang Patel5af2f632008-02-20 22:39:45 +00002526 ($$=$1)->push_back($3);
2527 CHECK_FOR_ERROR
2528 };
2529
Chris Lattner00950542001-06-06 20:29:01 +00002530BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner8ab406d2004-07-13 07:59:27 +00002531 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002532 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002533 }
Chris Lattner7e708292002-06-25 16:13:24 +00002534 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
Chris Lattner8ab406d2004-07-13 07:59:27 +00002535 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002536 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002537 };
Chris Lattner00950542001-06-06 20:29:01 +00002538
2539
2540// Basic blocks are terminated by branching instructions:
2541// br, br/cc, switch, ret
2542//
Reid Spencerb2d17862007-01-26 08:04:51 +00002543BasicBlock : InstructionList OptLocalAssign BBTerminatorInst {
Chris Lattner8ab406d2004-07-13 07:59:27 +00002544 setValueName($3, $2);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002545 CHECK_FOR_ERROR
Chris Lattner2079fde2001-10-13 06:41:08 +00002546 InsertValue($3);
Chris Lattner2079fde2001-10-13 06:41:08 +00002547 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00002548 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002549 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002550 };
Chris Lattner00950542001-06-06 20:29:01 +00002551
2552InstructionList : InstructionList Inst {
Reid Spencer3da59db2006-11-27 01:05:10 +00002553 if (CastInst *CI1 = dyn_cast<CastInst>($2))
2554 if (CastInst *CI2 = dyn_cast<CastInst>(CI1->getOperand(0)))
2555 if (CI2->getParent() == 0)
2556 $1->getInstList().push_back(CI2);
Chris Lattner00950542001-06-06 20:29:01 +00002557 $1->getInstList().push_back($2);
2558 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002559 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002560 }
Reid Spencer186a43f2007-03-19 18:39:36 +00002561 | /* empty */ { // Empty space between instruction lists
Nick Lewyckyfc82fab2008-03-02 02:48:09 +00002562 $$ = defineBBVal(ValID::createLocalID(CurFun.NextValNum), 0);
2563 CHECK_FOR_ERROR
2564 }
Nick Lewycky9be3c972008-03-10 02:20:00 +00002565 | UNWINDS TO ValueRef { // Only the unwind to block
2566 $$ = defineBBVal(ValID::createLocalID(CurFun.NextValNum), getBBVal($3));
Reid Spencer61c83e02006-08-18 08:43:06 +00002567 CHECK_FOR_ERROR
Chris Lattner22ae2322004-07-13 08:39:15 +00002568 }
Reid Spencer186a43f2007-03-19 18:39:36 +00002569 | LABELSTR { // Labelled (named) basic block
Nick Lewyckyfc82fab2008-03-02 02:48:09 +00002570 $$ = defineBBVal(ValID::createLocalName(*$1), 0);
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002571 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002572 CHECK_FOR_ERROR
Nick Lewyckyfc82fab2008-03-02 02:48:09 +00002573 }
Nick Lewycky9be3c972008-03-10 02:20:00 +00002574 | LABELSTR UNWINDS TO ValueRef {
2575 $$ = defineBBVal(ValID::createLocalName(*$1), getBBVal($4));
Nick Lewyckyfc82fab2008-03-02 02:48:09 +00002576 delete $1;
2577 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002578 };
Chris Lattner00950542001-06-06 20:29:01 +00002579
Devang Patel5af2f632008-02-20 22:39:45 +00002580BBTerminatorInst :
2581 RET ReturnedVal { // Return with a result...
Devang Patel53284d32008-02-26 22:12:58 +00002582 ValueList &VL = *$2;
Devang Patel43a1bb32008-02-26 23:17:50 +00002583 assert(!VL.empty() && "Invalid ret operands!");
Gabor Greif051a9502008-04-06 20:25:17 +00002584 $$ = ReturnInst::Create(&VL[0], VL.size());
Devang Patel5af2f632008-02-20 22:39:45 +00002585 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002586 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002587 }
Reid Spencer186a43f2007-03-19 18:39:36 +00002588 | RET VOID { // Return with no result...
Gabor Greif051a9502008-04-06 20:25:17 +00002589 $$ = ReturnInst::Create();
Reid Spencer61c83e02006-08-18 08:43:06 +00002590 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002591 }
Reid Spencer186a43f2007-03-19 18:39:36 +00002592 | BR LABEL ValueRef { // Unconditional Branch...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002593 BasicBlock* tmpBB = getBBVal($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002594 CHECK_FOR_ERROR
Gabor Greif051a9502008-04-06 20:25:17 +00002595 $$ = BranchInst::Create(tmpBB);
Reid Spencer186a43f2007-03-19 18:39:36 +00002596 } // Conditional Branch...
Reid Spencer8088e9d2007-01-13 05:00:20 +00002597 | BR INTTYPE ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
2598 assert(cast<IntegerType>($2)->getBitWidth() == 1 && "Not Bool?");
Reid Spencer5b7e7532006-09-28 19:28:24 +00002599 BasicBlock* tmpBBA = getBBVal($6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002600 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002601 BasicBlock* tmpBBB = getBBVal($9);
2602 CHECK_FOR_ERROR
Reid Spencer4fe16d62007-01-11 18:21:29 +00002603 Value* tmpVal = getVal(Type::Int1Ty, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002604 CHECK_FOR_ERROR
Gabor Greif051a9502008-04-06 20:25:17 +00002605 $$ = BranchInst::Create(tmpBBA, tmpBBB, tmpVal);
Chris Lattner00950542001-06-06 20:29:01 +00002606 }
2607 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencer08de34b2006-12-03 05:45:44 +00002608 Value* tmpVal = getVal($2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002609 CHECK_FOR_ERROR
2610 BasicBlock* tmpBB = getBBVal($6);
2611 CHECK_FOR_ERROR
Gabor Greif051a9502008-04-06 20:25:17 +00002612 SwitchInst *S = SwitchInst::Create(tmpVal, tmpBB, $8->size());
Chris Lattner00950542001-06-06 20:29:01 +00002613 $$ = S;
2614
Chris Lattner9232b992003-04-22 18:42:41 +00002615 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
Chris Lattner46748042002-04-09 19:41:42 +00002616 E = $8->end();
Chris Lattner69331f52005-02-24 05:25:17 +00002617 for (; I != E; ++I) {
2618 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
2619 S->addCase(CI, I->second);
2620 else
Reid Spencerf4fa5902007-02-05 10:16:10 +00002621 GEN_ERROR("Switch case is constant, but not a simple integer");
Chris Lattner69331f52005-02-24 05:25:17 +00002622 }
Chris Lattnerf57a43d2004-04-17 23:49:15 +00002623 delete $8;
Reid Spencer61c83e02006-08-18 08:43:06 +00002624 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002625 }
Chris Lattner196850c2003-05-15 21:30:00 +00002626 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencer08de34b2006-12-03 05:45:44 +00002627 Value* tmpVal = getVal($2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002628 CHECK_FOR_ERROR
2629 BasicBlock* tmpBB = getBBVal($6);
2630 CHECK_FOR_ERROR
Gabor Greif051a9502008-04-06 20:25:17 +00002631 SwitchInst *S = SwitchInst::Create(tmpVal, tmpBB, 0);
Chris Lattner196850c2003-05-15 21:30:00 +00002632 $$ = S;
Reid Spencer61c83e02006-08-18 08:43:06 +00002633 CHECK_FOR_ERROR
Chris Lattner196850c2003-05-15 21:30:00 +00002634 }
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002635 | INVOKE OptCallingConv ResultTypes ValueRef '(' ParamList ')' OptFuncAttrs
Chris Lattnera8e8f162005-05-06 20:27:19 +00002636 TO LABEL ValueRef UNWIND LABEL ValueRef {
Chris Lattner2079fde2001-10-13 06:41:08 +00002637
Reid Spencere1553cc2006-12-31 05:40:12 +00002638 // Handle the short syntax
2639 const PointerType *PFTy = 0;
2640 const FunctionType *Ty = 0;
Reid Spencer2c261782007-01-05 17:06:19 +00002641 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002642 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00002643 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00002644 std::vector<const Type*> ParamTypes;
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002645 ParamList::iterator I = $6->begin(), E = $6->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00002646 for (; I != E; ++I) {
Reid Spencere1553cc2006-12-31 05:40:12 +00002647 const Type *Ty = I->Val->getType();
2648 if (Ty == Type::VoidTy)
2649 GEN_ERROR("Short call syntax cannot be used with varargs");
2650 ParamTypes.push_back(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00002651 }
Chris Lattnerf7dedf22008-04-23 05:36:58 +00002652
2653 if (!FunctionType::isValidReturnType(*$3))
2654 GEN_ERROR("Invalid result type for LLVM function");
2655
Duncan Sandsdc024672007-11-27 13:23:08 +00002656 Ty = FunctionType::get($3->get(), ParamTypes, false);
Christopher Lamb43ad6b32007-12-17 01:12:55 +00002657 PFTy = PointerType::getUnqual(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00002658 }
Chris Lattner2079fde2001-10-13 06:41:08 +00002659
Reid Spencer2c9df212007-03-20 01:13:00 +00002660 delete $3;
2661
Chris Lattnera8e8f162005-05-06 20:27:19 +00002662 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002663 CHECK_FOR_ERROR
Reid Spencer2c261782007-01-05 17:06:19 +00002664 BasicBlock *Normal = getBBVal($11);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002665 CHECK_FOR_ERROR
Reid Spencer2c261782007-01-05 17:06:19 +00002666 BasicBlock *Except = getBBVal($14);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002667 CHECK_FOR_ERROR
Chris Lattner2079fde2001-10-13 06:41:08 +00002668
Chris Lattner58d74912008-03-12 17:45:29 +00002669 SmallVector<ParamAttrsWithIndex, 8> Attrs;
2670 if ($8 != ParamAttr::None)
2671 Attrs.push_back(ParamAttrsWithIndex::get(0, $8));
Duncan Sandsdc024672007-11-27 13:23:08 +00002672
Reid Spencere1553cc2006-12-31 05:40:12 +00002673 // Check the arguments
2674 ValueList Args;
2675 if ($6->empty()) { // Has no arguments?
2676 // Make sure no arguments is a good thing!
2677 if (Ty->getNumParams() != 0)
2678 GEN_ERROR("No arguments passed to a function that "
Reid Spencerf4fa5902007-02-05 10:16:10 +00002679 "expects arguments");
Chris Lattner2079fde2001-10-13 06:41:08 +00002680 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00002681 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00002682 // correctly!
Chris Lattnerd5d89962004-02-09 04:14:01 +00002683 FunctionType::param_iterator I = Ty->param_begin();
2684 FunctionType::param_iterator E = Ty->param_end();
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002685 ParamList::iterator ArgI = $6->begin(), ArgE = $6->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00002686 unsigned index = 1;
Reid Spencer863dd882007-04-28 16:06:50 +00002687
Duncan Sandsdc024672007-11-27 13:23:08 +00002688 for (; ArgI != ArgE && I != E; ++ArgI, ++I, ++index) {
Reid Spencere1553cc2006-12-31 05:40:12 +00002689 if (ArgI->Val->getType() != *I)
2690 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00002691 (*I)->getDescription() + "'");
Reid Spencere1553cc2006-12-31 05:40:12 +00002692 Args.push_back(ArgI->Val);
Chris Lattner58d74912008-03-12 17:45:29 +00002693 if (ArgI->Attrs != ParamAttr::None)
2694 Attrs.push_back(ParamAttrsWithIndex::get(index, ArgI->Attrs));
Reid Spencere1553cc2006-12-31 05:40:12 +00002695 }
Reid Spencer08de34b2006-12-03 05:45:44 +00002696
Reid Spencere1553cc2006-12-31 05:40:12 +00002697 if (Ty->isVarArg()) {
2698 if (I == E)
Duncan Sandseb824702008-01-11 21:23:39 +00002699 for (; ArgI != ArgE; ++ArgI, ++index) {
Reid Spencere1553cc2006-12-31 05:40:12 +00002700 Args.push_back(ArgI->Val); // push the remaining varargs
Chris Lattner58d74912008-03-12 17:45:29 +00002701 if (ArgI->Attrs != ParamAttr::None)
2702 Attrs.push_back(ParamAttrsWithIndex::get(index, ArgI->Attrs));
Duncan Sandseb824702008-01-11 21:23:39 +00002703 }
Reid Spencere1553cc2006-12-31 05:40:12 +00002704 } else if (I != E || ArgI != ArgE)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002705 GEN_ERROR("Invalid number of parameters detected");
Chris Lattner2079fde2001-10-13 06:41:08 +00002706 }
Reid Spencere1553cc2006-12-31 05:40:12 +00002707
Chris Lattner58d74912008-03-12 17:45:29 +00002708 PAListPtr PAL;
Duncan Sandsdc024672007-11-27 13:23:08 +00002709 if (!Attrs.empty())
Chris Lattner58d74912008-03-12 17:45:29 +00002710 PAL = PAListPtr::get(Attrs.begin(), Attrs.end());
Duncan Sandsdc024672007-11-27 13:23:08 +00002711
Reid Spencere1553cc2006-12-31 05:40:12 +00002712 // Create the InvokeInst
Gabor Greif051a9502008-04-06 20:25:17 +00002713 InvokeInst *II = InvokeInst::Create(V, Normal, Except, Args.begin(),Args.end());
Reid Spencere1553cc2006-12-31 05:40:12 +00002714 II->setCallingConv($2);
Duncan Sandsdc024672007-11-27 13:23:08 +00002715 II->setParamAttrs(PAL);
Reid Spencere1553cc2006-12-31 05:40:12 +00002716 $$ = II;
Chris Lattnera8e8f162005-05-06 20:27:19 +00002717 delete $6;
Reid Spencer61c83e02006-08-18 08:43:06 +00002718 CHECK_FOR_ERROR
Chris Lattner36143fc2003-09-08 18:54:55 +00002719 }
2720 | UNWIND {
2721 $$ = new UnwindInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002722 CHECK_FOR_ERROR
Chris Lattner16710e92004-10-16 18:17:13 +00002723 }
2724 | UNREACHABLE {
2725 $$ = new UnreachableInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002726 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002727 };
Chris Lattner2079fde2001-10-13 06:41:08 +00002728
2729
Chris Lattner00950542001-06-06 20:29:01 +00002730
2731JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
2732 $$ = $1;
Reid Spencer186a43f2007-03-19 18:39:36 +00002733 Constant *V = cast<Constant>(getExistingVal($2, $3));
Reid Spencer5b7e7532006-09-28 19:28:24 +00002734 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002735 if (V == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002736 GEN_ERROR("May only switch on a constant pool value");
Chris Lattner00950542001-06-06 20:29:01 +00002737
Reid Spencer5b7e7532006-09-28 19:28:24 +00002738 BasicBlock* tmpBB = getBBVal($6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002739 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002740 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner00950542001-06-06 20:29:01 +00002741 }
2742 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner9232b992003-04-22 18:42:41 +00002743 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Reid Spencer186a43f2007-03-19 18:39:36 +00002744 Constant *V = cast<Constant>(getExistingVal($1, $2));
Reid Spencer5b7e7532006-09-28 19:28:24 +00002745 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002746
2747 if (V == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002748 GEN_ERROR("May only switch on a constant pool value");
Chris Lattner00950542001-06-06 20:29:01 +00002749
Reid Spencer5b7e7532006-09-28 19:28:24 +00002750 BasicBlock* tmpBB = getBBVal($5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002751 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002752 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner51727be2002-06-04 21:58:56 +00002753 };
Chris Lattner00950542001-06-06 20:29:01 +00002754
Reid Spencerb2d17862007-01-26 08:04:51 +00002755Inst : OptLocalAssign InstVal {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002756 // Is this definition named?? if so, assign the name...
2757 setValueName($2, $1);
2758 CHECK_FOR_ERROR
2759 InsertValue($2);
2760 $$ = $2;
2761 CHECK_FOR_ERROR
2762 };
2763
Chris Lattner00950542001-06-06 20:29:01 +00002764
Chris Lattnerc24d2082001-06-11 15:04:20 +00002765PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Reid Spencere1553cc2006-12-31 05:40:12 +00002766 if (!UpRefs.empty())
2767 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Chris Lattner9232b992003-04-22 18:42:41 +00002768 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Reid Spencer08de34b2006-12-03 05:45:44 +00002769 Value* tmpVal = getVal(*$1, $3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002770 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002771 BasicBlock* tmpBB = getBBVal($5);
2772 CHECK_FOR_ERROR
2773 $$->push_back(std::make_pair(tmpVal, tmpBB));
Reid Spencer08de34b2006-12-03 05:45:44 +00002774 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00002775 }
2776 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
2777 $$ = $1;
Reid Spencer5b7e7532006-09-28 19:28:24 +00002778 Value* tmpVal = getVal($1->front().first->getType(), $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00002779 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002780 BasicBlock* tmpBB = getBBVal($6);
2781 CHECK_FOR_ERROR
2782 $1->push_back(std::make_pair(tmpVal, tmpBB));
Chris Lattner51727be2002-06-04 21:58:56 +00002783 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00002784
2785
Duncan Sandsdc024672007-11-27 13:23:08 +00002786ParamList : Types OptParamAttrs ValueRef OptParamAttrs {
2787 // FIXME: Remove trailing OptParamAttrs in LLVM 3.0, it was a mistake in 2.0
Reid Spencere1553cc2006-12-31 05:40:12 +00002788 if (!UpRefs.empty())
2789 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2790 // Used for call and invoke instructions
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002791 $$ = new ParamList();
Duncan Sandsdc024672007-11-27 13:23:08 +00002792 ParamListEntry E; E.Attrs = $2 | $4; E.Val = getVal($1->get(), $3);
Reid Spencere1553cc2006-12-31 05:40:12 +00002793 $$->push_back(E);
Reid Spencer2c9df212007-03-20 01:13:00 +00002794 delete $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00002795 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002796 }
Duncan Sandsdc024672007-11-27 13:23:08 +00002797 | LABEL OptParamAttrs ValueRef OptParamAttrs {
2798 // FIXME: Remove trailing OptParamAttrs in LLVM 3.0, it was a mistake in 2.0
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002799 // Labels are only valid in ASMs
2800 $$ = new ParamList();
Duncan Sandsdc024672007-11-27 13:23:08 +00002801 ParamListEntry E; E.Attrs = $2 | $4; E.Val = getBBVal($3);
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002802 $$->push_back(E);
Duncan Sandsdc024672007-11-27 13:23:08 +00002803 CHECK_FOR_ERROR
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002804 }
Duncan Sandsdc024672007-11-27 13:23:08 +00002805 | ParamList ',' Types OptParamAttrs ValueRef OptParamAttrs {
2806 // FIXME: Remove trailing OptParamAttrs in LLVM 3.0, it was a mistake in 2.0
Reid Spencere1553cc2006-12-31 05:40:12 +00002807 if (!UpRefs.empty())
2808 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattner00950542001-06-06 20:29:01 +00002809 $$ = $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00002810 ParamListEntry E; E.Attrs = $4 | $6; E.Val = getVal($3->get(), $5);
Reid Spencere1553cc2006-12-31 05:40:12 +00002811 $$->push_back(E);
Reid Spencer2c9df212007-03-20 01:13:00 +00002812 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002813 CHECK_FOR_ERROR
Reid Spencere1553cc2006-12-31 05:40:12 +00002814 }
Duncan Sandsdc024672007-11-27 13:23:08 +00002815 | ParamList ',' LABEL OptParamAttrs ValueRef OptParamAttrs {
2816 // FIXME: Remove trailing OptParamAttrs in LLVM 3.0, it was a mistake in 2.0
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002817 $$ = $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00002818 ParamListEntry E; E.Attrs = $4 | $6; E.Val = getBBVal($5);
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002819 $$->push_back(E);
2820 CHECK_FOR_ERROR
2821 }
2822 | /*empty*/ { $$ = new ParamList(); };
Chris Lattner00950542001-06-06 20:29:01 +00002823
Reid Spencere1553cc2006-12-31 05:40:12 +00002824IndexList // Used for gep instructions and constant expressions
Reid Spencere03969f2006-12-31 21:46:36 +00002825 : /*empty*/ { $$ = new std::vector<Value*>(); }
Reid Spencere1553cc2006-12-31 05:40:12 +00002826 | IndexList ',' ResolvedVal {
2827 $$ = $1;
2828 $$->push_back($3);
2829 CHECK_FOR_ERROR
2830 }
Reid Spencer71305d72006-12-31 21:25:25 +00002831 ;
Chris Lattner00950542001-06-06 20:29:01 +00002832
Chris Lattnerddb6db42005-05-06 05:51:46 +00002833OptTailCall : TAIL CALL {
2834 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002835 CHECK_FOR_ERROR
Chris Lattnerddb6db42005-05-06 05:51:46 +00002836 }
2837 | CALL {
2838 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002839 CHECK_FOR_ERROR
Chris Lattnerddb6db42005-05-06 05:51:46 +00002840 };
2841
Chris Lattner4a6482b2002-09-10 19:57:26 +00002842InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00002843 if (!UpRefs.empty())
2844 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Chris Lattner42a75512007-01-15 02:27:26 +00002845 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint() &&
Reid Spencer9d6565a2007-02-15 02:26:10 +00002846 !isa<VectorType>((*$2).get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002847 GEN_ERROR(
Reid Spencerf4fa5902007-02-05 10:16:10 +00002848 "Arithmetic operator requires integer, FP, or packed operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00002849 Value* val1 = getVal(*$2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002850 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00002851 Value* val2 = getVal(*$2, $5);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002852 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00002853 $$ = BinaryOperator::create($1, val1, val2);
Chris Lattner4a6482b2002-09-10 19:57:26 +00002854 if ($$ == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002855 GEN_ERROR("binary operator returned null");
Reid Spencer08de34b2006-12-03 05:45:44 +00002856 delete $2;
Chris Lattner4a6482b2002-09-10 19:57:26 +00002857 }
2858 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00002859 if (!UpRefs.empty())
2860 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Chris Lattner42a75512007-01-15 02:27:26 +00002861 if (!(*$2)->isInteger()) {
Reid Spencer9d6565a2007-02-15 02:26:10 +00002862 if (Instruction::isShift($1) || !isa<VectorType>($2->get()) ||
2863 !cast<VectorType>($2->get())->getElementType()->isInteger())
Reid Spencerf4fa5902007-02-05 10:16:10 +00002864 GEN_ERROR("Logical operator requires integral operands");
Chris Lattner0a017832005-12-21 18:31:29 +00002865 }
Reid Spencer08de34b2006-12-03 05:45:44 +00002866 Value* tmpVal1 = getVal(*$2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002867 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00002868 Value* tmpVal2 = getVal(*$2, $5);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002869 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00002870 $$ = BinaryOperator::create($1, tmpVal1, tmpVal2);
Chris Lattner4a6482b2002-09-10 19:57:26 +00002871 if ($$ == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002872 GEN_ERROR("binary operator returned null");
Reid Spencer08de34b2006-12-03 05:45:44 +00002873 delete $2;
Chris Lattner4a6482b2002-09-10 19:57:26 +00002874 }
Reid Spencer08de34b2006-12-03 05:45:44 +00002875 | ICMP IPredicates Types ValueRef ',' ValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00002876 if (!UpRefs.empty())
2877 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencer9d6565a2007-02-15 02:26:10 +00002878 if (isa<VectorType>((*$3).get()))
Reid Spencerac9dcb92007-02-15 03:39:18 +00002879 GEN_ERROR("Vector types not supported by icmp instruction");
Reid Spencer08de34b2006-12-03 05:45:44 +00002880 Value* tmpVal1 = getVal(*$3, $4);
2881 CHECK_FOR_ERROR
2882 Value* tmpVal2 = getVal(*$3, $6);
2883 CHECK_FOR_ERROR
2884 $$ = CmpInst::create($1, $2, tmpVal1, tmpVal2);
2885 if ($$ == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002886 GEN_ERROR("icmp operator returned null");
Reid Spencer2c9df212007-03-20 01:13:00 +00002887 delete $3;
Reid Spencer08de34b2006-12-03 05:45:44 +00002888 }
2889 | FCMP FPredicates Types ValueRef ',' ValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00002890 if (!UpRefs.empty())
2891 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencer9d6565a2007-02-15 02:26:10 +00002892 if (isa<VectorType>((*$3).get()))
Reid Spencerac9dcb92007-02-15 03:39:18 +00002893 GEN_ERROR("Vector types not supported by fcmp instruction");
Reid Spencer08de34b2006-12-03 05:45:44 +00002894 Value* tmpVal1 = getVal(*$3, $4);
2895 CHECK_FOR_ERROR
2896 Value* tmpVal2 = getVal(*$3, $6);
2897 CHECK_FOR_ERROR
2898 $$ = CmpInst::create($1, $2, tmpVal1, tmpVal2);
2899 if ($$ == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002900 GEN_ERROR("fcmp operator returned null");
Reid Spencer2c9df212007-03-20 01:13:00 +00002901 delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00002902 }
Reid Spencer3da59db2006-11-27 01:05:10 +00002903 | CastOps ResolvedVal TO Types {
Reid Spencere1553cc2006-12-31 05:40:12 +00002904 if (!UpRefs.empty())
2905 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00002906 Value* Val = $2;
Reid Spencer93947c32007-01-17 02:47:33 +00002907 const Type* DestTy = $4->get();
2908 if (!CastInst::castIsValid($1, Val, DestTy))
2909 GEN_ERROR("invalid cast opcode for cast from '" +
2910 Val->getType()->getDescription() + "' to '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00002911 DestTy->getDescription() + "'");
Reid Spencer93947c32007-01-17 02:47:33 +00002912 $$ = CastInst::create($1, Val, DestTy);
Reid Spencer08de34b2006-12-03 05:45:44 +00002913 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00002914 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00002915 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer4fe16d62007-01-11 18:21:29 +00002916 if ($2->getType() != Type::Int1Ty)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002917 GEN_ERROR("select condition must be boolean");
Reid Spencer08de34b2006-12-03 05:45:44 +00002918 if ($4->getType() != $6->getType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00002919 GEN_ERROR("select value types should match");
Gabor Greif051a9502008-04-06 20:25:17 +00002920 $$ = SelectInst::Create($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002921 CHECK_FOR_ERROR
Chris Lattner76f0ff32004-03-12 05:51:36 +00002922 }
Chris Lattner99e7ab72003-10-18 05:53:13 +00002923 | VAARG ResolvedVal ',' Types {
Reid Spencere1553cc2006-12-31 05:40:12 +00002924 if (!UpRefs.empty())
2925 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00002926 $$ = new VAArgInst($2, *$4);
2927 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00002928 CHECK_FOR_ERROR
Chris Lattner99e7ab72003-10-18 05:53:13 +00002929 }
Robert Bocchino9c62b562006-01-10 19:04:32 +00002930 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencer08de34b2006-12-03 05:45:44 +00002931 if (!ExtractElementInst::isValidOperands($2, $4))
Reid Spencerf4fa5902007-02-05 10:16:10 +00002932 GEN_ERROR("Invalid extractelement operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00002933 $$ = new ExtractElementInst($2, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00002934 CHECK_FOR_ERROR
Robert Bocchino9c62b562006-01-10 19:04:32 +00002935 }
Robert Bocchino2def1b32006-01-17 20:06:25 +00002936 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer08de34b2006-12-03 05:45:44 +00002937 if (!InsertElementInst::isValidOperands($2, $4, $6))
Reid Spencerf4fa5902007-02-05 10:16:10 +00002938 GEN_ERROR("Invalid insertelement operands");
Gabor Greif051a9502008-04-06 20:25:17 +00002939 $$ = InsertElementInst::Create($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002940 CHECK_FOR_ERROR
Robert Bocchino2def1b32006-01-17 20:06:25 +00002941 }
Chris Lattner4c949082006-04-08 01:18:35 +00002942 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer08de34b2006-12-03 05:45:44 +00002943 if (!ShuffleVectorInst::isValidOperands($2, $4, $6))
Reid Spencerf4fa5902007-02-05 10:16:10 +00002944 GEN_ERROR("Invalid shufflevector operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00002945 $$ = new ShuffleVectorInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002946 CHECK_FOR_ERROR
Chris Lattner4c949082006-04-08 01:18:35 +00002947 }
Chris Lattner3b237fc2003-10-19 21:34:28 +00002948 | PHI_TOK PHIList {
Chris Lattnerc24d2082001-06-11 15:04:20 +00002949 const Type *Ty = $2->front().first->getType();
Chris Lattner8ea8f362003-10-30 01:38:18 +00002950 if (!Ty->isFirstClassType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00002951 GEN_ERROR("PHI node operands must be of first class type");
Gabor Greif051a9502008-04-06 20:25:17 +00002952 $$ = PHINode::Create(Ty);
Chris Lattnerce1df9e2005-01-29 00:35:55 +00002953 ((PHINode*)$$)->reserveOperandSpace($2->size());
Chris Lattner00950542001-06-06 20:29:01 +00002954 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00002955 if ($2->front().first->getType() != Ty)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002956 GEN_ERROR("All elements of a PHI node must be of the same type");
Chris Lattnerb00c5822001-10-02 03:41:24 +00002957 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00002958 $2->pop_front();
2959 }
2960 delete $2; // Free the list...
Reid Spencer61c83e02006-08-18 08:43:06 +00002961 CHECK_FOR_ERROR
Chris Lattnerddb6db42005-05-06 05:51:46 +00002962 }
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002963 | OptTailCall OptCallingConv ResultTypes ValueRef '(' ParamList ')'
Reid Spencer2c261782007-01-05 17:06:19 +00002964 OptFuncAttrs {
Reid Spencere1553cc2006-12-31 05:40:12 +00002965
2966 // Handle the short syntax
Bill Wendlingb39a55a2006-11-12 11:10:39 +00002967 const PointerType *PFTy = 0;
2968 const FunctionType *Ty = 0;
Reid Spencer2c261782007-01-05 17:06:19 +00002969 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002970 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00002971 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00002972 std::vector<const Type*> ParamTypes;
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002973 ParamList::iterator I = $6->begin(), E = $6->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00002974 for (; I != E; ++I) {
Reid Spencere1553cc2006-12-31 05:40:12 +00002975 const Type *Ty = I->Val->getType();
2976 if (Ty == Type::VoidTy)
2977 GEN_ERROR("Short call syntax cannot be used with varargs");
2978 ParamTypes.push_back(Ty);
Chris Lattneref9c23f2001-10-03 14:53:21 +00002979 }
Chris Lattnerf7dedf22008-04-23 05:36:58 +00002980
2981 if (!FunctionType::isValidReturnType(*$3))
2982 GEN_ERROR("Invalid result type for LLVM function");
2983
Duncan Sandsdc024672007-11-27 13:23:08 +00002984 Ty = FunctionType::get($3->get(), ParamTypes, false);
Christopher Lamb43ad6b32007-12-17 01:12:55 +00002985 PFTy = PointerType::getUnqual(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00002986 }
Chris Lattner00950542001-06-06 20:29:01 +00002987
Chris Lattnera8e8f162005-05-06 20:27:19 +00002988 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002989 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002990
Reid Spencerebff55c2007-04-16 06:55:42 +00002991 // Check for call to invalid intrinsic to avoid crashing later.
2992 if (Function *theF = dyn_cast<Function>(V)) {
Reid Spencerce1e8ef2007-04-16 22:01:57 +00002993 if (theF->hasName() && (theF->getValueName()->getKeyLength() >= 5) &&
Reid Spencer43e60732007-04-16 20:31:06 +00002994 (0 == strncmp(theF->getValueName()->getKeyData(), "llvm.", 5)) &&
2995 !theF->getIntrinsicID(true))
Reid Spencerebff55c2007-04-16 06:55:42 +00002996 GEN_ERROR("Call to invalid LLVM intrinsic function '" +
2997 theF->getName() + "'");
2998 }
2999
Duncan Sandsdc024672007-11-27 13:23:08 +00003000 // Set up the ParamAttrs for the function
Chris Lattner58d74912008-03-12 17:45:29 +00003001 SmallVector<ParamAttrsWithIndex, 8> Attrs;
3002 if ($8 != ParamAttr::None)
3003 Attrs.push_back(ParamAttrsWithIndex::get(0, $8));
Reid Spencere1553cc2006-12-31 05:40:12 +00003004 // Check the arguments
3005 ValueList Args;
3006 if ($6->empty()) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00003007 // Make sure no arguments is a good thing!
3008 if (Ty->getNumParams() != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00003009 GEN_ERROR("No arguments passed to a function that "
Reid Spencerf4fa5902007-02-05 10:16:10 +00003010 "expects arguments");
Chris Lattner8b81bf52001-07-25 22:47:46 +00003011 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00003012 // Loop through FunctionType's arguments and ensure they are specified
Duncan Sandsdc024672007-11-27 13:23:08 +00003013 // correctly. Also, gather any parameter attributes.
Chris Lattnerd5d89962004-02-09 04:14:01 +00003014 FunctionType::param_iterator I = Ty->param_begin();
3015 FunctionType::param_iterator E = Ty->param_end();
Dale Johanneseneb57ea72007-11-05 21:20:28 +00003016 ParamList::iterator ArgI = $6->begin(), ArgE = $6->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00003017 unsigned index = 1;
Reid Spencer863dd882007-04-28 16:06:50 +00003018
Duncan Sandsdc024672007-11-27 13:23:08 +00003019 for (; ArgI != ArgE && I != E; ++ArgI, ++I, ++index) {
Reid Spencere1553cc2006-12-31 05:40:12 +00003020 if (ArgI->Val->getType() != *I)
3021 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00003022 (*I)->getDescription() + "'");
Reid Spencere1553cc2006-12-31 05:40:12 +00003023 Args.push_back(ArgI->Val);
Chris Lattner58d74912008-03-12 17:45:29 +00003024 if (ArgI->Attrs != ParamAttr::None)
3025 Attrs.push_back(ParamAttrsWithIndex::get(index, ArgI->Attrs));
Reid Spencere1553cc2006-12-31 05:40:12 +00003026 }
3027 if (Ty->isVarArg()) {
3028 if (I == E)
Duncan Sandseb824702008-01-11 21:23:39 +00003029 for (; ArgI != ArgE; ++ArgI, ++index) {
Reid Spencere1553cc2006-12-31 05:40:12 +00003030 Args.push_back(ArgI->Val); // push the remaining varargs
Chris Lattner58d74912008-03-12 17:45:29 +00003031 if (ArgI->Attrs != ParamAttr::None)
3032 Attrs.push_back(ParamAttrsWithIndex::get(index, ArgI->Attrs));
Duncan Sandseb824702008-01-11 21:23:39 +00003033 }
Reid Spencere1553cc2006-12-31 05:40:12 +00003034 } else if (I != E || ArgI != ArgE)
Reid Spencerf4fa5902007-02-05 10:16:10 +00003035 GEN_ERROR("Invalid number of parameters detected");
Chris Lattner8b81bf52001-07-25 22:47:46 +00003036 }
Duncan Sandsdc024672007-11-27 13:23:08 +00003037
3038 // Finish off the ParamAttrs and check them
Chris Lattner58d74912008-03-12 17:45:29 +00003039 PAListPtr PAL;
Duncan Sandsdc024672007-11-27 13:23:08 +00003040 if (!Attrs.empty())
Chris Lattner58d74912008-03-12 17:45:29 +00003041 PAL = PAListPtr::get(Attrs.begin(), Attrs.end());
Duncan Sandsdc024672007-11-27 13:23:08 +00003042
Reid Spencere1553cc2006-12-31 05:40:12 +00003043 // Create the call node
Gabor Greif051a9502008-04-06 20:25:17 +00003044 CallInst *CI = CallInst::Create(V, Args.begin(), Args.end());
Reid Spencere1553cc2006-12-31 05:40:12 +00003045 CI->setTailCall($1);
3046 CI->setCallingConv($2);
Duncan Sandsdc024672007-11-27 13:23:08 +00003047 CI->setParamAttrs(PAL);
Reid Spencere1553cc2006-12-31 05:40:12 +00003048 $$ = CI;
Chris Lattnera8e8f162005-05-06 20:27:19 +00003049 delete $6;
Reid Spencerb2d17862007-01-26 08:04:51 +00003050 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00003051 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00003052 }
3053 | MemoryInst {
3054 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00003055 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00003056 };
Chris Lattner00950542001-06-06 20:29:01 +00003057
Chris Lattner15c9c032003-09-08 18:20:29 +00003058OptVolatile : VOLATILE {
3059 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00003060 CHECK_FOR_ERROR
Chris Lattner15c9c032003-09-08 18:20:29 +00003061 }
3062 | /* empty */ {
3063 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00003064 CHECK_FOR_ERROR
Chris Lattner15c9c032003-09-08 18:20:29 +00003065 };
3066
Chris Lattner027dcc52001-07-08 21:10:27 +00003067
Chris Lattnerddb6db42005-05-06 05:51:46 +00003068
Chris Lattner66db8e42005-11-06 06:34:12 +00003069MemoryInst : MALLOC Types OptCAlign {
Reid Spencere1553cc2006-12-31 05:40:12 +00003070 if (!UpRefs.empty())
3071 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00003072 $$ = new MallocInst(*$2, 0, $3);
3073 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00003074 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00003075 }
Reid Spencera54b7cb2007-01-12 07:05:14 +00003076 | MALLOC Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencere1553cc2006-12-31 05:40:12 +00003077 if (!UpRefs.empty())
3078 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00003079 Value* tmpVal = getVal($4, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00003080 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00003081 $$ = new MallocInst(*$2, tmpVal, $6);
3082 delete $2;
Nate Begeman14b05292005-11-05 09:21:28 +00003083 }
Chris Lattner66db8e42005-11-06 06:34:12 +00003084 | ALLOCA Types OptCAlign {
Reid Spencere1553cc2006-12-31 05:40:12 +00003085 if (!UpRefs.empty())
3086 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00003087 $$ = new AllocaInst(*$2, 0, $3);
3088 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00003089 CHECK_FOR_ERROR
Nate Begeman14b05292005-11-05 09:21:28 +00003090 }
Reid Spencera54b7cb2007-01-12 07:05:14 +00003091 | ALLOCA Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencere1553cc2006-12-31 05:40:12 +00003092 if (!UpRefs.empty())
3093 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00003094 Value* tmpVal = getVal($4, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00003095 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00003096 $$ = new AllocaInst(*$2, tmpVal, $6);
3097 delete $2;
Nate Begeman14b05292005-11-05 09:21:28 +00003098 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00003099 | FREE ResolvedVal {
Reid Spencer08de34b2006-12-03 05:45:44 +00003100 if (!isa<PointerType>($2->getType()))
Reid Spencer61c83e02006-08-18 08:43:06 +00003101 GEN_ERROR("Trying to free nonpointer type " +
Reid Spencerf4fa5902007-02-05 10:16:10 +00003102 $2->getType()->getDescription() + "");
Reid Spencer08de34b2006-12-03 05:45:44 +00003103 $$ = new FreeInst($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00003104 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00003105 }
3106
Christopher Lamb43c7f372007-04-22 19:24:39 +00003107 | OptVolatile LOAD Types ValueRef OptCAlign {
Reid Spencere1553cc2006-12-31 05:40:12 +00003108 if (!UpRefs.empty())
3109 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00003110 if (!isa<PointerType>($3->get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00003111 GEN_ERROR("Can't load from nonpointer type: " +
Reid Spencer08de34b2006-12-03 05:45:44 +00003112 (*$3)->getDescription());
3113 if (!cast<PointerType>($3->get())->getElementType()->isFirstClassType())
Reid Spencer61c83e02006-08-18 08:43:06 +00003114 GEN_ERROR("Can't load from pointer of non-first-class type: " +
Reid Spencer08de34b2006-12-03 05:45:44 +00003115 (*$3)->getDescription());
3116 Value* tmpVal = getVal(*$3, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00003117 CHECK_FOR_ERROR
Christopher Lamb43c7f372007-04-22 19:24:39 +00003118 $$ = new LoadInst(tmpVal, "", $1, $5);
Reid Spencer08de34b2006-12-03 05:45:44 +00003119 delete $3;
Chris Lattner027dcc52001-07-08 21:10:27 +00003120 }
Christopher Lamb43c7f372007-04-22 19:24:39 +00003121 | OptVolatile STORE ResolvedVal ',' Types ValueRef OptCAlign {
Reid Spencere1553cc2006-12-31 05:40:12 +00003122 if (!UpRefs.empty())
3123 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00003124 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner8c8418d2003-09-01 16:31:28 +00003125 if (!PT)
Reid Spencer61c83e02006-08-18 08:43:06 +00003126 GEN_ERROR("Can't store to a nonpointer type: " +
Reid Spencer08de34b2006-12-03 05:45:44 +00003127 (*$5)->getDescription());
Chris Lattner8c8418d2003-09-01 16:31:28 +00003128 const Type *ElTy = PT->getElementType();
Reid Spencer08de34b2006-12-03 05:45:44 +00003129 if (ElTy != $3->getType())
3130 GEN_ERROR("Can't store '" + $3->getType()->getDescription() +
Reid Spencerf4fa5902007-02-05 10:16:10 +00003131 "' into space of type '" + ElTy->getDescription() + "'");
Chris Lattner0383cc42002-08-21 23:51:21 +00003132
Reid Spencer08de34b2006-12-03 05:45:44 +00003133 Value* tmpVal = getVal(*$5, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00003134 CHECK_FOR_ERROR
Christopher Lamb43c7f372007-04-22 19:24:39 +00003135 $$ = new StoreInst($3, tmpVal, $1, $7);
Reid Spencer08de34b2006-12-03 05:45:44 +00003136 delete $5;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00003137 }
Devang Patel1b76c752008-02-22 19:31:15 +00003138| GETRESULT Types SymbolicValueRef ',' EUINT64VAL {
3139 Value *TmpVal = getVal($2->get(), $3);
Devang Pateld6ffcf92008-02-19 22:26:37 +00003140 if (!GetResultInst::isValidOperands(TmpVal, $5))
3141 GEN_ERROR("Invalid getresult operands");
3142 $$ = new GetResultInst(TmpVal, $5);
Devang Patel57ef4f42008-02-23 00:35:18 +00003143 delete $2;
Devang Pateld6ffcf92008-02-19 22:26:37 +00003144 CHECK_FOR_ERROR
3145 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00003146 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencere1553cc2006-12-31 05:40:12 +00003147 if (!UpRefs.empty())
3148 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00003149 if (!isa<PointerType>($2->get()))
Reid Spencerf4fa5902007-02-05 10:16:10 +00003150 GEN_ERROR("getelementptr insn requires pointer operand");
Chris Lattner830b6f92004-04-05 01:30:04 +00003151
David Greeneb8f74792007-09-04 15:46:09 +00003152 if (!GetElementPtrInst::getIndexedType(*$2, $4->begin(), $4->end(), true))
Reid Spencer61c83e02006-08-18 08:43:06 +00003153 GEN_ERROR("Invalid getelementptr indices for type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00003154 (*$2)->getDescription()+ "'");
Reid Spencer08de34b2006-12-03 05:45:44 +00003155 Value* tmpVal = getVal(*$2, $3);
Reid Spencer61c83e02006-08-18 08:43:06 +00003156 CHECK_FOR_ERROR
Gabor Greif051a9502008-04-06 20:25:17 +00003157 $$ = GetElementPtrInst::Create(tmpVal, $4->begin(), $4->end());
Reid Spencer08de34b2006-12-03 05:45:44 +00003158 delete $2;
Reid Spencer5b7e7532006-09-28 19:28:24 +00003159 delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00003160 };
Chris Lattner027dcc52001-07-08 21:10:27 +00003161
Brian Gaeked0fde302003-11-11 22:41:34 +00003162
Chris Lattner00950542001-06-06 20:29:01 +00003163%%
Reid Spencer61c83e02006-08-18 08:43:06 +00003164
Reid Spencere1553cc2006-12-31 05:40:12 +00003165// common code from the two 'RunVMAsmParser' functions
3166static Module* RunParser(Module * M) {
Reid Spencere1553cc2006-12-31 05:40:12 +00003167 CurModule.CurrentModule = M;
Reid Spencere1553cc2006-12-31 05:40:12 +00003168 // Check to make sure the parser succeeded
3169 if (yyparse()) {
3170 if (ParserResult)
3171 delete ParserResult;
3172 return 0;
3173 }
3174
Reid Spencercd5bd902007-03-30 01:37:13 +00003175 // Emit an error if there are any unresolved types left.
3176 if (!CurModule.LateResolveTypes.empty()) {
3177 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
3178 if (DID.Type == ValID::LocalName) {
3179 GenerateError("Undefined type remains at eof: '"+DID.getName() + "'");
3180 } else {
3181 GenerateError("Undefined type remains at eof: #" + itostr(DID.Num));
3182 }
3183 if (ParserResult)
3184 delete ParserResult;
3185 return 0;
3186 }
3187
3188 // Emit an error if there are any unresolved values left.
3189 if (!CurModule.LateResolveValues.empty()) {
3190 Value *V = CurModule.LateResolveValues.back();
3191 std::map<Value*, std::pair<ValID, int> >::iterator I =
3192 CurModule.PlaceHolderInfo.find(V);
3193
3194 if (I != CurModule.PlaceHolderInfo.end()) {
3195 ValID &DID = I->second.first;
3196 if (DID.Type == ValID::LocalName) {
3197 GenerateError("Undefined value remains at eof: "+DID.getName() + "'");
3198 } else {
3199 GenerateError("Undefined value remains at eof: #" + itostr(DID.Num));
3200 }
3201 if (ParserResult)
3202 delete ParserResult;
3203 return 0;
3204 }
3205 }
3206
Reid Spencere1553cc2006-12-31 05:40:12 +00003207 // Check to make sure that parsing produced a result
3208 if (!ParserResult)
3209 return 0;
3210
3211 // Reset ParserResult variable while saving its value for the result.
3212 Module *Result = ParserResult;
3213 ParserResult = 0;
3214
3215 return Result;
3216}
3217
Reid Spencer61c83e02006-08-18 08:43:06 +00003218void llvm::GenerateError(const std::string &message, int LineNo) {
Chris Lattner8e3a8e02007-11-18 08:46:26 +00003219 if (LineNo == -1) LineNo = LLLgetLineNo();
Reid Spencer61c83e02006-08-18 08:43:06 +00003220 // TODO: column number in exception
3221 if (TheParseError)
Chris Lattner8e3a8e02007-11-18 08:46:26 +00003222 TheParseError->setError(LLLgetFilename(), message, LineNo);
Reid Spencer61c83e02006-08-18 08:43:06 +00003223 TriggerError = 1;
3224}
3225
Chris Lattner09083092001-07-08 04:57:15 +00003226int yyerror(const char *ErrorMsg) {
Chris Lattner8e3a8e02007-11-18 08:46:26 +00003227 std::string where = LLLgetFilename() + ":" + utostr(LLLgetLineNo()) + ": ";
Reid Spenceref9b9a72007-02-05 20:47:22 +00003228 std::string errMsg = where + "error: " + std::string(ErrorMsg);
Chris Lattner8e3a8e02007-11-18 08:46:26 +00003229 if (yychar != YYEMPTY && yychar != 0) {
3230 errMsg += " while reading token: '";
3231 errMsg += std::string(LLLgetTokenStart(),
3232 LLLgetTokenStart()+LLLgetTokenLength()) + "'";
3233 }
Reid Spencer61c83e02006-08-18 08:43:06 +00003234 GenerateError(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00003235 return 0;
3236}