blob: c47b6fc6ea9062d2e65b7dbd2f78c8326c339c22 [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===-- llvmAsmParser.y - Parser for llvm assembly files --------*- C++ -*-===//
Misha Brukman5a2a3822005-05-10 22:02:28 +00002//
John Criswell856ba762003-10-21 15:17:13 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman5a2a3822005-05-10 22:02:28 +00007//
John Criswell856ba762003-10-21 15:17:13 +00008//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
10// This file implements the bison parser for LLVM assembly languages files.
11//
Chris Lattnercf3056d2003-10-13 03:32:08 +000012//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +000013
Chris Lattner00950542001-06-06 20:29:01 +000014%{
15#include "ParserInternals.h"
Chris Lattnera8e8f162005-05-06 20:27:19 +000016#include "llvm/CallingConv.h"
Chris Lattneraa2c8532006-01-25 22:26:43 +000017#include "llvm/InlineAsm.h"
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000018#include "llvm/Instructions.h"
Chris Lattner00950542001-06-06 20:29:01 +000019#include "llvm/Module.h"
Reid Spenceref9b9a72007-02-05 20:47:22 +000020#include "llvm/ValueSymbolTable.h"
Chris Lattner830b6f92004-04-05 01:30:04 +000021#include "llvm/Support/GetElementPtrTypeIterator.h"
Reid Spencere1553cc2006-12-31 05:40:12 +000022#include "llvm/Support/CommandLine.h"
Chris Lattnere0135402007-01-31 04:43:46 +000023#include "llvm/ADT/SmallVector.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000024#include "llvm/ADT/STLExtras.h"
Chris Lattner87ac9722005-11-06 06:46:28 +000025#include "llvm/Support/MathExtras.h"
Bill Wendling3e3bcbf2006-11-28 22:47:12 +000026#include "llvm/Support/Streams.h"
Reid Spencer8ce1da72004-07-04 12:19:05 +000027#include <algorithm>
Chris Lattner00950542001-06-06 20:29:01 +000028#include <list>
Chris Lattnerc02659f2007-02-11 21:39:35 +000029#include <map>
Chris Lattnerc188eeb2002-07-30 18:54:25 +000030#include <utility>
Reid Spencere1553cc2006-12-31 05:40:12 +000031#ifndef NDEBUG
32#define YYDEBUG 1
33#endif
Chris Lattner00950542001-06-06 20:29:01 +000034
Reid Spencere4f47592006-08-18 17:32:55 +000035// The following is a gross hack. In order to rid the libAsmParser library of
36// exceptions, we have to have a way of getting the yyparse function to go into
37// an error situation. So, whenever we want an error to occur, the GenerateError
38// function (see bottom of file) sets TriggerError. Then, at the end of each
39// production in the grammer we use CHECK_FOR_ERROR which will invoke YYERROR
40// (a goto) to put YACC in error state. Furthermore, several calls to
41// GenerateError are made from inside productions and they must simulate the
42// previous exception behavior by exiting the production immediately. We have
43// replaced these with the GEN_ERROR macro which calls GeneratError and then
44// immediately invokes YYERROR. This would be so much cleaner if it was a
45// recursive descent parser.
Reid Spencer61c83e02006-08-18 08:43:06 +000046static bool TriggerError = false;
Reid Spencerf63697d2006-10-09 17:36:59 +000047#define CHECK_FOR_ERROR { if (TriggerError) { TriggerError = false; YYABORT; } }
Reid Spencer61c83e02006-08-18 08:43:06 +000048#define GEN_ERROR(msg) { GenerateError(msg); YYERROR; }
49
Chris Lattner386a3b72001-10-16 19:54:17 +000050int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
Chris Lattner09083092001-07-08 04:57:15 +000051int yylex(); // declaration" of xxx warnings.
Chris Lattner00950542001-06-06 20:29:01 +000052int yyparse();
53
Brian Gaeked0fde302003-11-11 22:41:34 +000054namespace llvm {
Chris Lattner86427632004-07-14 06:39:48 +000055 std::string CurFilename;
Reid Spencere1553cc2006-12-31 05:40:12 +000056#if YYDEBUG
57static cl::opt<bool>
58Debug("debug-yacc", cl::desc("Print yacc debug state changes"),
59 cl::Hidden, cl::init(false));
60#endif
Chris Lattner86427632004-07-14 06:39:48 +000061}
62using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000063
Chris Lattner00950542001-06-06 20:29:01 +000064static Module *ParserResult;
Chris Lattner00950542001-06-06 20:29:01 +000065
Chris Lattner30c89792001-09-07 16:35:17 +000066// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
67// relating to upreferences in the input stream.
68//
69//#define DEBUG_UPREFS 1
70#ifdef DEBUG_UPREFS
Bill Wendlinge8156192006-12-07 01:30:32 +000071#define UR_OUT(X) cerr << X
Chris Lattner30c89792001-09-07 16:35:17 +000072#else
73#define UR_OUT(X)
74#endif
75
Vikram S. Adved3f7eb02002-07-14 22:59:28 +000076#define YYERROR_VERBOSE 1
77
Chris Lattnerb2b96672005-11-12 18:21:21 +000078static GlobalVariable *CurGV;
Andrew Lenharth558bc882005-06-18 18:34:52 +000079
80
Chris Lattner7e708292002-06-25 16:13:24 +000081// This contains info used when building the body of a function. It is
82// destroyed when the function is completed.
Chris Lattner00950542001-06-06 20:29:01 +000083//
Chris Lattner9232b992003-04-22 18:42:41 +000084typedef std::vector<Value *> ValueList; // Numbered defs
Reid Spencere1553cc2006-12-31 05:40:12 +000085
Misha Brukman5a2a3822005-05-10 22:02:28 +000086static void
Reid Spencer186a43f2007-03-19 18:39:36 +000087ResolveDefinitions(ValueList &LateResolvers, ValueList *FutureLateResolvers=0);
Chris Lattner00950542001-06-06 20:29:01 +000088
89static struct PerModuleInfo {
90 Module *CurrentModule;
Reid Spencer186a43f2007-03-19 18:39:36 +000091 ValueList Values; // Module level numbered definitions
92 ValueList LateResolveValues;
Reid Spencerb78b9082006-11-28 07:28:14 +000093 std::vector<PATypeHolder> Types;
94 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner00950542001-06-06 20:29:01 +000095
Chris Lattnerbc721ed2004-07-13 08:28:21 +000096 /// PlaceHolderInfo - When temporary placeholder objects are created, remember
Reid Spencerfd20c0a2006-05-29 02:34:34 +000097 /// how they were referenced and on which line of the input they came from so
Chris Lattner22ae2322004-07-13 08:39:15 +000098 /// that we can resolve them later and print error messages as appropriate.
Chris Lattnerbc721ed2004-07-13 08:28:21 +000099 std::map<Value*, std::pair<ValID, int> > PlaceHolderInfo;
100
Chris Lattner2079fde2001-10-13 06:41:08 +0000101 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
102 // references to global values. Global values may be referenced before they
103 // are defined, and if so, the temporary object that they represent is held
Reid Spencer3271ed52004-07-18 00:08:11 +0000104 // here. This is used for forward references of GlobalValues.
Chris Lattner2079fde2001-10-13 06:41:08 +0000105 //
Chris Lattner9232b992003-04-22 18:42:41 +0000106 typedef std::map<std::pair<const PointerType *,
Chris Lattnerbc207592004-03-08 06:09:57 +0000107 ValID>, GlobalValue*> GlobalRefsType;
Chris Lattner2079fde2001-10-13 06:41:08 +0000108 GlobalRefsType GlobalRefs;
109
Chris Lattner00950542001-06-06 20:29:01 +0000110 void ModuleDone() {
Chris Lattner7e708292002-06-25 16:13:24 +0000111 // If we could not resolve some functions at function compilation time
112 // (calls to functions before they are defined), resolve them now... Types
113 // are resolved when the constant pool has been completely parsed.
Chris Lattner30c89792001-09-07 16:35:17 +0000114 //
Chris Lattner00950542001-06-06 20:29:01 +0000115 ResolveDefinitions(LateResolveValues);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000116 if (TriggerError)
117 return;
Chris Lattner00950542001-06-06 20:29:01 +0000118
Chris Lattner2079fde2001-10-13 06:41:08 +0000119 // Check to make sure that all global value forward references have been
120 // resolved!
121 //
122 if (!GlobalRefs.empty()) {
Chris Lattner9232b992003-04-22 18:42:41 +0000123 std::string UndefinedReferences = "Unresolved global references exist:\n";
Misha Brukman5a2a3822005-05-10 22:02:28 +0000124
Chris Lattner749ce032002-03-11 22:12:39 +0000125 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
126 I != E; ++I) {
127 UndefinedReferences += " " + I->first.first->getDescription() + " " +
128 I->first.second.getName() + "\n";
129 }
Reid Spencer61c83e02006-08-18 08:43:06 +0000130 GenerateError(UndefinedReferences);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000131 return;
Chris Lattner2079fde2001-10-13 06:41:08 +0000132 }
133
Chris Lattner7e708292002-06-25 16:13:24 +0000134 Values.clear(); // Clear out function local definitions
Chris Lattner30c89792001-09-07 16:35:17 +0000135 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +0000136 CurrentModule = 0;
137 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000138
Chris Lattner8a327842004-07-14 21:44:00 +0000139 // GetForwardRefForGlobal - Check to see if there is a forward reference
140 // for this global. If so, remove it from the GlobalRefs map and return it.
141 // If not, just return null.
Reid Spencer863dd882007-04-28 16:06:50 +0000142 GlobalValue *GetForwardRefForGlobal(const PointerType *PTy, ValID ID) {
Chris Lattner8a327842004-07-14 21:44:00 +0000143 // Check to see if there is a forward reference to this global variable...
144 // if there is, eliminate it and patch the reference to use the new def'n.
145 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(PTy, ID));
146 GlobalValue *Ret = 0;
147 if (I != GlobalRefs.end()) {
148 Ret = I->second;
Reid Spencer863dd882007-04-28 16:06:50 +0000149 GlobalRefs.erase(I);
Chris Lattner8a327842004-07-14 21:44:00 +0000150 }
151 return Ret;
152 }
Reid Spencer98b3c5c2007-01-02 21:53:43 +0000153
154 bool TypeIsUnresolved(PATypeHolder* PATy) {
155 // If it isn't abstract, its resolved
156 const Type* Ty = PATy->get();
157 if (!Ty->isAbstract())
158 return false;
159 // Traverse the type looking for abstract types. If it isn't abstract then
160 // we don't need to traverse that leg of the type.
161 std::vector<const Type*> WorkList, SeenList;
162 WorkList.push_back(Ty);
163 while (!WorkList.empty()) {
164 const Type* Ty = WorkList.back();
165 SeenList.push_back(Ty);
166 WorkList.pop_back();
167 if (const OpaqueType* OpTy = dyn_cast<OpaqueType>(Ty)) {
168 // Check to see if this is an unresolved type
169 std::map<ValID, PATypeHolder>::iterator I = LateResolveTypes.begin();
170 std::map<ValID, PATypeHolder>::iterator E = LateResolveTypes.end();
171 for ( ; I != E; ++I) {
172 if (I->second.get() == OpTy)
173 return true;
174 }
175 } else if (const SequentialType* SeqTy = dyn_cast<SequentialType>(Ty)) {
176 const Type* TheTy = SeqTy->getElementType();
177 if (TheTy->isAbstract() && TheTy != Ty) {
178 std::vector<const Type*>::iterator I = SeenList.begin(),
179 E = SeenList.end();
180 for ( ; I != E; ++I)
181 if (*I == TheTy)
182 break;
183 if (I == E)
184 WorkList.push_back(TheTy);
185 }
186 } else if (const StructType* StrTy = dyn_cast<StructType>(Ty)) {
187 for (unsigned i = 0; i < StrTy->getNumElements(); ++i) {
188 const Type* TheTy = StrTy->getElementType(i);
189 if (TheTy->isAbstract() && TheTy != Ty) {
190 std::vector<const Type*>::iterator I = SeenList.begin(),
191 E = SeenList.end();
192 for ( ; I != E; ++I)
193 if (*I == TheTy)
194 break;
195 if (I == E)
196 WorkList.push_back(TheTy);
197 }
198 }
199 }
200 }
201 return false;
202 }
Chris Lattner00950542001-06-06 20:29:01 +0000203} CurModule;
204
Chris Lattner79df7c02002-03-26 18:01:55 +0000205static struct PerFunctionInfo {
Chris Lattner7e708292002-06-25 16:13:24 +0000206 Function *CurrentFunction; // Pointer to current function being created
Chris Lattner00950542001-06-06 20:29:01 +0000207
Reid Spencer186a43f2007-03-19 18:39:36 +0000208 ValueList Values; // Keep track of #'d definitions
209 unsigned NextValNum;
210 ValueList LateResolveValues;
Reid Spenceref9b9a72007-02-05 20:47:22 +0000211 bool isDeclare; // Is this function a forward declararation?
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000212 GlobalValue::LinkageTypes Linkage; // Linkage for forward declaration.
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000213 GlobalValue::VisibilityTypes Visibility;
Chris Lattner00950542001-06-06 20:29:01 +0000214
Chris Lattner2e2ed292004-07-14 06:28:35 +0000215 /// BBForwardRefs - When we see forward references to basic blocks, keep
216 /// track of them here.
Reid Spencer186a43f2007-03-19 18:39:36 +0000217 std::map<ValID, BasicBlock*> BBForwardRefs;
Chris Lattner2e2ed292004-07-14 06:28:35 +0000218
Chris Lattner79df7c02002-03-26 18:01:55 +0000219 inline PerFunctionInfo() {
220 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000221 isDeclare = false;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000222 Linkage = GlobalValue::ExternalLinkage;
223 Visibility = GlobalValue::DefaultVisibility;
Chris Lattner00950542001-06-06 20:29:01 +0000224 }
225
Chris Lattner79df7c02002-03-26 18:01:55 +0000226 inline void FunctionStart(Function *M) {
227 CurrentFunction = M;
Reid Spencer186a43f2007-03-19 18:39:36 +0000228 NextValNum = 0;
Chris Lattner00950542001-06-06 20:29:01 +0000229 }
230
Chris Lattner79df7c02002-03-26 18:01:55 +0000231 void FunctionDone() {
Chris Lattner2e2ed292004-07-14 06:28:35 +0000232 // Any forward referenced blocks left?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000233 if (!BBForwardRefs.empty()) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000234 GenerateError("Undefined reference to label " +
Reid Spencer186a43f2007-03-19 18:39:36 +0000235 BBForwardRefs.begin()->second->getName());
Reid Spencer5b7e7532006-09-28 19:28:24 +0000236 return;
237 }
Chris Lattner2e2ed292004-07-14 06:28:35 +0000238
239 // Resolve all forward references now.
Chris Lattner386a3b72001-10-16 19:54:17 +0000240 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000241
Chris Lattner7e708292002-06-25 16:13:24 +0000242 Values.clear(); // Clear out function local definitions
Reid Spencer186a43f2007-03-19 18:39:36 +0000243 BBForwardRefs.clear();
Chris Lattner79df7c02002-03-26 18:01:55 +0000244 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000245 isDeclare = false;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000246 Linkage = GlobalValue::ExternalLinkage;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000247 Visibility = GlobalValue::DefaultVisibility;
Chris Lattner00950542001-06-06 20:29:01 +0000248 }
Chris Lattner394f2eb2003-10-16 20:12:13 +0000249} CurFun; // Info for the current function...
Chris Lattner00950542001-06-06 20:29:01 +0000250
Chris Lattner394f2eb2003-10-16 20:12:13 +0000251static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000252
Chris Lattner00950542001-06-06 20:29:01 +0000253
254//===----------------------------------------------------------------------===//
255// Code to handle definitions of all the types
256//===----------------------------------------------------------------------===//
257
Reid Spencer186a43f2007-03-19 18:39:36 +0000258static void InsertValue(Value *V, ValueList &ValueTab = CurFun.Values) {
259 // Things that have names or are void typed don't get slot numbers
260 if (V->hasName() || (V->getType() == Type::VoidTy))
261 return;
Chris Lattner2079fde2001-10-13 06:41:08 +0000262
Reid Spencer186a43f2007-03-19 18:39:36 +0000263 // In the case of function values, we have to allow for the forward reference
264 // of basic blocks, which are included in the numbering. Consequently, we keep
265 // track of the next insertion location with NextValNum. When a BB gets
266 // inserted, it could change the size of the CurFun.Values vector.
267 if (&ValueTab == &CurFun.Values) {
268 if (ValueTab.size() <= CurFun.NextValNum)
269 ValueTab.resize(CurFun.NextValNum+1);
270 ValueTab[CurFun.NextValNum++] = V;
271 return;
272 }
273 // For all other lists, its okay to just tack it on the back of the vector.
274 ValueTab.push_back(V);
Chris Lattner00950542001-06-06 20:29:01 +0000275}
276
Chris Lattner30c89792001-09-07 16:35:17 +0000277static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000278 switch (D.Type) {
Reid Spencerb2d17862007-01-26 08:04:51 +0000279 case ValID::LocalID: // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000280 // Module constants occupy the lowest numbered slots...
Reid Spencerb2d17862007-01-26 08:04:51 +0000281 if (D.Num < CurModule.Types.size())
282 return CurModule.Types[D.Num];
Chris Lattner42c9e772001-10-20 09:32:59 +0000283 break;
Reid Spencerb2d17862007-01-26 08:04:51 +0000284 case ValID::LocalName: // Is it a named definition?
Chris Lattnera09000d2004-07-14 23:03:46 +0000285 if (const Type *N = CurModule.CurrentModule->getTypeByName(D.Name)) {
286 D.destroy(); // Free old strdup'd memory...
287 return N;
Chris Lattner6e6026b2002-11-20 18:36:02 +0000288 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000289 break;
Chris Lattner30c89792001-09-07 16:35:17 +0000290 default:
Reid Spencerf4fa5902007-02-05 10:16:10 +0000291 GenerateError("Internal parser error: Invalid symbol type reference");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000292 return 0;
Chris Lattner30c89792001-09-07 16:35:17 +0000293 }
294
295 // If we reached here, we referenced either a symbol that we don't know about
296 // or an id number that hasn't been read yet. We may be referencing something
297 // forward, so just create an entry to be resolved later and get to it...
298 //
299 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
300
Chris Lattner355ad1f2005-03-21 06:27:42 +0000301
302 if (inFunctionScope()) {
Reid Spencerb2d17862007-01-26 08:04:51 +0000303 if (D.Type == ValID::LocalName) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000304 GenerateError("Reference to an undefined type: '" + D.getName() + "'");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000305 return 0;
306 } else {
Reid Spencerb2d17862007-01-26 08:04:51 +0000307 GenerateError("Reference to an undefined type: #" + utostr(D.Num));
Reid Spencer5b7e7532006-09-28 19:28:24 +0000308 return 0;
309 }
Chris Lattner4a42e902001-10-22 05:56:09 +0000310 }
Chris Lattner30c89792001-09-07 16:35:17 +0000311
Reid Spencerb78b9082006-11-28 07:28:14 +0000312 std::map<ValID, PATypeHolder>::iterator I =CurModule.LateResolveTypes.find(D);
Chris Lattner355ad1f2005-03-21 06:27:42 +0000313 if (I != CurModule.LateResolveTypes.end())
Reid Spencerb78b9082006-11-28 07:28:14 +0000314 return I->second;
Chris Lattner355ad1f2005-03-21 06:27:42 +0000315
Reid Spencerb78b9082006-11-28 07:28:14 +0000316 Type *Typ = OpaqueType::get();
317 CurModule.LateResolveTypes.insert(std::make_pair(D, Typ));
318 return Typ;
Reid Spencer08de34b2006-12-03 05:45:44 +0000319 }
Chris Lattner30c89792001-09-07 16:35:17 +0000320
Reid Spencer186a43f2007-03-19 18:39:36 +0000321// getExistingVal - Look up the value specified by the provided type and
Chris Lattner2079fde2001-10-13 06:41:08 +0000322// the provided ValID. If the value exists and has already been defined, return
323// it. Otherwise return null.
324//
Reid Spencer186a43f2007-03-19 18:39:36 +0000325static Value *getExistingVal(const Type *Ty, const ValID &D) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000326 if (isa<FunctionType>(Ty)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000327 GenerateError("Functions are not values and "
Chris Lattner79df7c02002-03-26 18:01:55 +0000328 "must be referenced as pointers");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000329 return 0;
330 }
Chris Lattner386a3b72001-10-16 19:54:17 +0000331
Chris Lattner30c89792001-09-07 16:35:17 +0000332 switch (D.Type) {
Reid Spencerb2d17862007-01-26 08:04:51 +0000333 case ValID::LocalID: { // Is it a numbered definition?
Reid Spencerb2d17862007-01-26 08:04:51 +0000334 // Check that the number is within bounds.
Reid Spencer186a43f2007-03-19 18:39:36 +0000335 if (D.Num >= CurFun.Values.size())
336 return 0;
337 Value *Result = CurFun.Values[D.Num];
338 if (Ty != Result->getType()) {
339 GenerateError("Numbered value (%" + utostr(D.Num) + ") of type '" +
340 Result->getType()->getDescription() + "' does not match "
341 "expected type, '" + Ty->getDescription() + "'");
342 return 0;
343 }
344 return Result;
Reid Spencerb2d17862007-01-26 08:04:51 +0000345 }
346 case ValID::GlobalID: { // Is it a numbered definition?
Reid Spencer186a43f2007-03-19 18:39:36 +0000347 if (D.Num >= CurModule.Values.size())
Reid Spenceref9b9a72007-02-05 20:47:22 +0000348 return 0;
Reid Spencer186a43f2007-03-19 18:39:36 +0000349 Value *Result = CurModule.Values[D.Num];
350 if (Ty != Result->getType()) {
351 GenerateError("Numbered value (@" + utostr(D.Num) + ") of type '" +
352 Result->getType()->getDescription() + "' does not match "
353 "expected type, '" + Ty->getDescription() + "'");
Reid Spenceref9b9a72007-02-05 20:47:22 +0000354 return 0;
Reid Spencer186a43f2007-03-19 18:39:36 +0000355 }
356 return Result;
Chris Lattner00950542001-06-06 20:29:01 +0000357 }
Reid Spencerb2d17862007-01-26 08:04:51 +0000358
359 case ValID::LocalName: { // Is it a named definition?
Reid Spenceref9b9a72007-02-05 20:47:22 +0000360 if (!inFunctionScope())
361 return 0;
362 ValueSymbolTable &SymTab = CurFun.CurrentFunction->getValueSymbolTable();
363 Value *N = SymTab.lookup(D.Name);
364 if (N == 0)
365 return 0;
366 if (N->getType() != Ty)
367 return 0;
Reid Spencerb2d17862007-01-26 08:04:51 +0000368
369 D.destroy(); // Free old strdup'd memory...
370 return N;
371 }
372 case ValID::GlobalName: { // Is it a named definition?
Reid Spenceref9b9a72007-02-05 20:47:22 +0000373 ValueSymbolTable &SymTab = CurModule.CurrentModule->getValueSymbolTable();
374 Value *N = SymTab.lookup(D.Name);
375 if (N == 0)
376 return 0;
377 if (N->getType() != Ty)
378 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000379
380 D.destroy(); // Free old strdup'd memory...
381 return N;
382 }
383
Misha Brukman5a2a3822005-05-10 22:02:28 +0000384 // Check to make sure that "Ty" is an integral type, and that our
Chris Lattner2079fde2001-10-13 06:41:08 +0000385 // value will fit into the specified type...
386 case ValID::ConstSIntVal: // Is it a constant pool reference??
Reid Spencerb83eb642006-10-20 07:07:24 +0000387 if (!ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000388 GenerateError("Signed integral constant '" +
Misha Brukman5a2a3822005-05-10 22:02:28 +0000389 itostr(D.ConstPool64) + "' is invalid for type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +0000390 Ty->getDescription() + "'");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000391 return 0;
392 }
Reid Spencereac65742007-03-19 20:40:22 +0000393 return ConstantInt::get(Ty, D.ConstPool64, true);
Chris Lattner2079fde2001-10-13 06:41:08 +0000394
395 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Reid Spencerb83eb642006-10-20 07:07:24 +0000396 if (!ConstantInt::isValueValidForType(Ty, D.UConstPool64)) {
397 if (!ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000398 GenerateError("Integral constant '" + utostr(D.UConstPool64) +
Reid Spencerf4fa5902007-02-05 10:16:10 +0000399 "' is invalid or out of range");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000400 return 0;
Chris Lattner2079fde2001-10-13 06:41:08 +0000401 } else { // This is really a signed reference. Transmogrify.
Reid Spencereac65742007-03-19 20:40:22 +0000402 return ConstantInt::get(Ty, D.ConstPool64, true);
Chris Lattner2079fde2001-10-13 06:41:08 +0000403 }
404 } else {
Reid Spencerb83eb642006-10-20 07:07:24 +0000405 return ConstantInt::get(Ty, D.UConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000406 }
407
Chris Lattner2079fde2001-10-13 06:41:08 +0000408 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000409 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP)) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000410 GenerateError("FP constant invalid for type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000411 return 0;
412 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000413 return ConstantFP::get(Ty, D.ConstPoolFP);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000414
Chris Lattner2079fde2001-10-13 06:41:08 +0000415 case ValID::ConstNullVal: // Is it a null value?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000416 if (!isa<PointerType>(Ty)) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000417 GenerateError("Cannot create a a non pointer null");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000418 return 0;
419 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000420 return ConstantPointerNull::get(cast<PointerType>(Ty));
Misha Brukman5a2a3822005-05-10 22:02:28 +0000421
Chris Lattner16710e92004-10-16 18:17:13 +0000422 case ValID::ConstUndefVal: // Is it an undef value?
423 return UndefValue::get(Ty);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000424
Chris Lattnerf1f03df2005-12-21 17:53:02 +0000425 case ValID::ConstZeroVal: // Is it a zero value?
426 return Constant::getNullValue(Ty);
427
Chris Lattnerd78700d2002-08-16 21:14:40 +0000428 case ValID::ConstantVal: // Fully resolved constant?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000429 if (D.ConstantValue->getType() != Ty) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000430 GenerateError("Constant expression type different from required type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000431 return 0;
432 }
Chris Lattnerd78700d2002-08-16 21:14:40 +0000433 return D.ConstantValue;
434
Chris Lattneraa2c8532006-01-25 22:26:43 +0000435 case ValID::InlineAsmVal: { // Inline asm expression
436 const PointerType *PTy = dyn_cast<PointerType>(Ty);
437 const FunctionType *FTy =
438 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
Reid Spencer5b7e7532006-09-28 19:28:24 +0000439 if (!FTy || !InlineAsm::Verify(FTy, D.IAD->Constraints)) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000440 GenerateError("Invalid type for asm constraint string");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000441 return 0;
442 }
Chris Lattneraa2c8532006-01-25 22:26:43 +0000443 InlineAsm *IA = InlineAsm::get(FTy, D.IAD->AsmString, D.IAD->Constraints,
444 D.IAD->HasSideEffects);
445 D.destroy(); // Free InlineAsmDescriptor.
446 return IA;
447 }
Chris Lattner30c89792001-09-07 16:35:17 +0000448 default:
Reid Spencer1755a9a2007-02-05 17:01:20 +0000449 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000450 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000451 } // End of switch
452
Reid Spencer1755a9a2007-02-05 17:01:20 +0000453 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000454 return 0;
455}
456
Reid Spencer186a43f2007-03-19 18:39:36 +0000457// getVal - This function is identical to getExistingVal, except that if a
Chris Lattner2079fde2001-10-13 06:41:08 +0000458// value is not already defined, it "improvises" by creating a placeholder var
459// that looks and acts just like the requested variable. When the value is
460// defined later, all uses of the placeholder variable are replaced with the
461// real thing.
462//
Chris Lattner64116f92004-07-14 01:33:11 +0000463static Value *getVal(const Type *Ty, const ValID &ID) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000464 if (Ty == Type::LabelTy) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000465 GenerateError("Cannot use a basic block here");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000466 return 0;
467 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000468
Chris Lattner64116f92004-07-14 01:33:11 +0000469 // See if the value has already been defined.
Reid Spencer186a43f2007-03-19 18:39:36 +0000470 Value *V = getExistingVal(Ty, ID);
Chris Lattner2079fde2001-10-13 06:41:08 +0000471 if (V) return V;
Reid Spencer5b7e7532006-09-28 19:28:24 +0000472 if (TriggerError) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000473
Reid Spencer5b7e7532006-09-28 19:28:24 +0000474 if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty)) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000475 GenerateError("Invalid use of a composite type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000476 return 0;
477 }
Chris Lattner60cd9552005-03-23 01:29:26 +0000478
Chris Lattner00950542001-06-06 20:29:01 +0000479 // If we reached here, we referenced either a symbol that we don't know about
480 // or an id number that hasn't been read yet. We may be referencing something
481 // forward, so just create an entry to be resolved later and get to it...
482 //
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000483 switch (ID.Type) {
484 case ValID::GlobalName:
Reid Spencer3cb4dda2007-04-28 14:13:42 +0000485 case ValID::GlobalID: {
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000486 const PointerType *PTy = dyn_cast<PointerType>(Ty);
487 if (!PTy) {
488 GenerateError("Invalid type for reference to global" );
489 return 0;
490 }
491 const Type* ElTy = PTy->getElementType();
492 if (const FunctionType *FTy = dyn_cast<FunctionType>(ElTy))
493 V = new Function(FTy, GlobalValue::ExternalLinkage);
494 else
495 V = new GlobalVariable(ElTy, false, GlobalValue::ExternalLinkage);
496 break;
Reid Spencer3cb4dda2007-04-28 14:13:42 +0000497 }
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000498 default:
499 V = new Argument(Ty);
500 }
501
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000502 // Remember where this forward reference came from. FIXME, shouldn't we try
503 // to recycle these things??
Chris Lattner64116f92004-07-14 01:33:11 +0000504 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000505 llvmAsmlineno)));
506
Chris Lattner79df7c02002-03-26 18:01:55 +0000507 if (inFunctionScope())
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000508 InsertValue(V, CurFun.LateResolveValues);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000509 else
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000510 InsertValue(V, CurModule.LateResolveValues);
511 return V;
Chris Lattner00950542001-06-06 20:29:01 +0000512}
513
Reid Spencer186a43f2007-03-19 18:39:36 +0000514/// defineBBVal - This is a definition of a new basic block with the specified
515/// identifier which must be the same as CurFun.NextValNum, if its numeric.
516static BasicBlock *defineBBVal(const ValID &ID) {
Reid Spencer1755a9a2007-02-05 17:01:20 +0000517 assert(inFunctionScope() && "Can't get basic block at global scope!");
Chris Lattner64116f92004-07-14 01:33:11 +0000518
Chris Lattner2e2ed292004-07-14 06:28:35 +0000519 BasicBlock *BB = 0;
Chris Lattner64116f92004-07-14 01:33:11 +0000520
Reid Spencer186a43f2007-03-19 18:39:36 +0000521 // First, see if this was forward referenced
Chris Lattner64116f92004-07-14 01:33:11 +0000522
Reid Spencer186a43f2007-03-19 18:39:36 +0000523 std::map<ValID, BasicBlock*>::iterator BBI = CurFun.BBForwardRefs.find(ID);
524 if (BBI != CurFun.BBForwardRefs.end()) {
525 BB = BBI->second;
Chris Lattner2e2ed292004-07-14 06:28:35 +0000526 // The forward declaration could have been inserted anywhere in the
527 // function: insert it into the correct place now.
528 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
529 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
Reid Spencer186a43f2007-03-19 18:39:36 +0000530
Reid Spencer2c9df212007-03-20 01:13:00 +0000531 // We're about to erase the entry, save the key so we can clean it up.
532 ValID Tmp = BBI->first;
533
Reid Spencer186a43f2007-03-19 18:39:36 +0000534 // Erase the forward ref from the map as its no longer "forward"
535 CurFun.BBForwardRefs.erase(ID);
536
Reid Spencer2c9df212007-03-20 01:13:00 +0000537 // The key has been removed from the map but so we don't want to leave
538 // strdup'd memory around so destroy it too.
539 Tmp.destroy();
540
Reid Spencer186a43f2007-03-19 18:39:36 +0000541 // If its a numbered definition, bump the number and set the BB value.
542 if (ID.Type == ValID::LocalID) {
543 assert(ID.Num == CurFun.NextValNum && "Invalid new block number");
544 InsertValue(BB);
545 }
546
547 ID.destroy();
548 return BB;
549 }
550
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.Name : "");
554 BB = new BasicBlock(Name, CurFun.CurrentFunction);
555 if (ID.Type == ValID::LocalID) {
556 assert(ID.Num == CurFun.NextValNum && "Invalid new block number");
557 InsertValue(BB);
Chris Lattner2e2ed292004-07-14 06:28:35 +0000558 }
Reid Spencer186a43f2007-03-19 18:39:36 +0000559
560 ID.destroy(); // Free strdup'd memory
561 return BB;
562}
563
564/// getBBVal - get an existing BB value or create a forward reference for it.
565///
566static BasicBlock *getBBVal(const ValID &ID) {
567 assert(inFunctionScope() && "Can't get basic block at global scope!");
568
569 BasicBlock *BB = 0;
570
571 std::map<ValID, BasicBlock*>::iterator BBI = CurFun.BBForwardRefs.find(ID);
572 if (BBI != CurFun.BBForwardRefs.end()) {
573 BB = BBI->second;
574 } if (ID.Type == ValID::LocalName) {
575 std::string Name = ID.Name;
576 Value *N = CurFun.CurrentFunction->getValueSymbolTable().lookup(Name);
577 if (N)
578 if (N->getType()->getTypeID() == Type::LabelTyID)
579 BB = cast<BasicBlock>(N);
580 else
581 GenerateError("Reference to label '" + Name + "' is actually of type '"+
582 N->getType()->getDescription() + "'");
583 } else if (ID.Type == ValID::LocalID) {
584 if (ID.Num < CurFun.NextValNum && ID.Num < CurFun.Values.size()) {
585 if (CurFun.Values[ID.Num]->getType()->getTypeID() == Type::LabelTyID)
586 BB = cast<BasicBlock>(CurFun.Values[ID.Num]);
587 else
588 GenerateError("Reference to label '%" + utostr(ID.Num) +
589 "' is actually of type '"+
590 CurFun.Values[ID.Num]->getType()->getDescription() + "'");
591 }
592 } else {
593 GenerateError("Illegal label reference " + ID.getName());
594 return 0;
595 }
596
597 // If its already been defined, return it now.
598 if (BB) {
599 ID.destroy(); // Free strdup'd memory.
600 return BB;
601 }
602
603 // Otherwise, this block has not been seen before, create it.
604 std::string Name;
605 if (ID.Type == ValID::LocalName)
606 Name = ID.Name;
607 BB = new BasicBlock(Name, CurFun.CurrentFunction);
608
609 // Insert it in the forward refs map.
610 CurFun.BBForwardRefs[ID] = BB;
611
Chris Lattner64116f92004-07-14 01:33:11 +0000612 return BB;
613}
614
Chris Lattner00950542001-06-06 20:29:01 +0000615
616//===----------------------------------------------------------------------===//
617// Code to handle forward references in instructions
618//===----------------------------------------------------------------------===//
619//
620// This code handles the late binding needed with statements that reference
621// values not defined yet... for example, a forward branch, or the PHI node for
622// a loop body.
623//
Chris Lattner394f2eb2003-10-16 20:12:13 +0000624// This keeps a table (CurFun.LateResolveValues) of all such forward references
Chris Lattner00950542001-06-06 20:29:01 +0000625// and back patchs after we are done.
626//
627
Misha Brukman5a2a3822005-05-10 22:02:28 +0000628// ResolveDefinitions - If we could not resolve some defs at parsing
629// time (forward branches, phi functions for loops, etc...) resolve the
Chris Lattner00950542001-06-06 20:29:01 +0000630// defs now...
631//
Misha Brukman5a2a3822005-05-10 22:02:28 +0000632static void
Reid Spencer186a43f2007-03-19 18:39:36 +0000633ResolveDefinitions(ValueList &LateResolvers, ValueList *FutureLateResolvers) {
Chris Lattner00950542001-06-06 20:29:01 +0000634 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Reid Spencer186a43f2007-03-19 18:39:36 +0000635 while (!LateResolvers.empty()) {
636 Value *V = LateResolvers.back();
637 LateResolvers.pop_back();
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000638
Reid Spencer186a43f2007-03-19 18:39:36 +0000639 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
640 CurModule.PlaceHolderInfo.find(V);
641 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000642
Reid Spencer186a43f2007-03-19 18:39:36 +0000643 ValID &DID = PHI->second.first;
Chris Lattner00950542001-06-06 20:29:01 +0000644
Reid Spencer186a43f2007-03-19 18:39:36 +0000645 Value *TheRealValue = getExistingVal(V->getType(), DID);
646 if (TriggerError)
647 return;
648 if (TheRealValue) {
649 V->replaceAllUsesWith(TheRealValue);
650 delete V;
651 CurModule.PlaceHolderInfo.erase(PHI);
652 } else if (FutureLateResolvers) {
653 // Functions have their unresolved items forwarded to the module late
654 // resolver table
655 InsertValue(V, *FutureLateResolvers);
656 } else {
657 if (DID.Type == ValID::LocalName || DID.Type == ValID::GlobalName) {
658 GenerateError("Reference to an invalid definition: '" +DID.getName()+
659 "' of type '" + V->getType()->getDescription() + "'",
660 PHI->second.second);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000661 return;
Chris Lattner386a3b72001-10-16 19:54:17 +0000662 } else {
Reid Spencer186a43f2007-03-19 18:39:36 +0000663 GenerateError("Reference to an invalid definition: #" +
664 itostr(DID.Num) + " of type '" +
665 V->getType()->getDescription() + "'",
666 PHI->second.second);
667 return;
Chris Lattner30c89792001-09-07 16:35:17 +0000668 }
Chris Lattner00950542001-06-06 20:29:01 +0000669 }
670 }
Chris Lattner00950542001-06-06 20:29:01 +0000671 LateResolvers.clear();
672}
673
Chris Lattner4a42e902001-10-22 05:56:09 +0000674// ResolveTypeTo - A brand new type was just declared. This means that (if
675// name is not null) things referencing Name can be resolved. Otherwise, things
676// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000677//
Chris Lattner4a42e902001-10-22 05:56:09 +0000678static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattner355ad1f2005-03-21 06:27:42 +0000679 ValID D;
Reid Spencerb2d17862007-01-26 08:04:51 +0000680 if (Name) D = ValID::createLocalName(Name);
681 else D = ValID::createLocalID(CurModule.Types.size());
Chris Lattner00950542001-06-06 20:29:01 +0000682
Reid Spencerb78b9082006-11-28 07:28:14 +0000683 std::map<ValID, PATypeHolder>::iterator I =
Chris Lattner355ad1f2005-03-21 06:27:42 +0000684 CurModule.LateResolveTypes.find(D);
685 if (I != CurModule.LateResolveTypes.end()) {
Reid Spencerb78b9082006-11-28 07:28:14 +0000686 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattner355ad1f2005-03-21 06:27:42 +0000687 CurModule.LateResolveTypes.erase(I);
Chris Lattner30c89792001-09-07 16:35:17 +0000688 }
689}
690
Chris Lattner1781aca2001-09-18 04:00:54 +0000691// setValueName - Set the specified value to the name given. The name may be
692// null potentially, in which case this is a noop. The string passed in is
Chris Lattner8ab406d2004-07-13 07:59:27 +0000693// assumed to be a malloc'd string buffer, and is free'd by this function.
694//
695static void setValueName(Value *V, char *NameStr) {
Reid Spencerb2d17862007-01-26 08:04:51 +0000696 if (!NameStr) return;
697 std::string Name(NameStr); // Copy string
698 free(NameStr); // Free old string
Chris Lattnerc9aea522004-07-13 08:12:39 +0000699
Reid Spencerb2d17862007-01-26 08:04:51 +0000700 if (V->getType() == Type::VoidTy) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000701 GenerateError("Can't assign name '" + Name+"' to value with void type");
Reid Spencerb2d17862007-01-26 08:04:51 +0000702 return;
Chris Lattner8ab406d2004-07-13 07:59:27 +0000703 }
Reid Spencerb2d17862007-01-26 08:04:51 +0000704
Reid Spencer1755a9a2007-02-05 17:01:20 +0000705 assert(inFunctionScope() && "Must be in function scope!");
Reid Spenceref9b9a72007-02-05 20:47:22 +0000706 ValueSymbolTable &ST = CurFun.CurrentFunction->getValueSymbolTable();
707 if (ST.lookup(Name)) {
Reid Spencerb2d17862007-01-26 08:04:51 +0000708 GenerateError("Redefinition of value '" + Name + "' of type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +0000709 V->getType()->getDescription() + "'");
Reid Spencerb2d17862007-01-26 08:04:51 +0000710 return;
711 }
712
713 // Set the name.
714 V->setName(Name);
Chris Lattner8ab406d2004-07-13 07:59:27 +0000715}
716
Chris Lattnerd88998d2004-07-14 08:23:52 +0000717/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
718/// this is a declaration, otherwise it is a definition.
Chris Lattnerb2b96672005-11-12 18:21:21 +0000719static GlobalVariable *
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000720ParseGlobalVariable(char *NameStr,
721 GlobalValue::LinkageTypes Linkage,
722 GlobalValue::VisibilityTypes Visibility,
Chris Lattnerb2b96672005-11-12 18:21:21 +0000723 bool isConstantGlobal, const Type *Ty,
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +0000724 Constant *Initializer, bool IsThreadLocal) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000725 if (isa<FunctionType>(Ty)) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000726 GenerateError("Cannot declare global vars of function type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000727 return 0;
728 }
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000729
Misha Brukman5a2a3822005-05-10 22:02:28 +0000730 const PointerType *PTy = PointerType::get(Ty);
Chris Lattnera09000d2004-07-14 23:03:46 +0000731
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000732 std::string Name;
733 if (NameStr) {
734 Name = NameStr; // Copy string
735 free(NameStr); // Free old string
Chris Lattnerd88998d2004-07-14 08:23:52 +0000736 }
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000737
Chris Lattner8a327842004-07-14 21:44:00 +0000738 // See if this global value was forward referenced. If so, recycle the
739 // object.
Misha Brukman5a2a3822005-05-10 22:02:28 +0000740 ValID ID;
Chris Lattner8a327842004-07-14 21:44:00 +0000741 if (!Name.empty()) {
Reid Spencerb2d17862007-01-26 08:04:51 +0000742 ID = ValID::createGlobalName((char*)Name.c_str());
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000743 } else {
Reid Spencer186a43f2007-03-19 18:39:36 +0000744 ID = ValID::createGlobalID(CurModule.Values.size());
Chris Lattner8a327842004-07-14 21:44:00 +0000745 }
746
Reid Spencer863dd882007-04-28 16:06:50 +0000747 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000748 // Move the global to the end of the list, from whereever it was
Chris Lattner8a327842004-07-14 21:44:00 +0000749 // previously inserted.
750 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
751 CurModule.CurrentModule->getGlobalList().remove(GV);
752 CurModule.CurrentModule->getGlobalList().push_back(GV);
753 GV->setInitializer(Initializer);
754 GV->setLinkage(Linkage);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000755 GV->setVisibility(Visibility);
Chris Lattner8a327842004-07-14 21:44:00 +0000756 GV->setConstant(isConstantGlobal);
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +0000757 GV->setThreadLocal(IsThreadLocal);
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000758 InsertValue(GV, CurModule.Values);
Chris Lattnerb2b96672005-11-12 18:21:21 +0000759 return GV;
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000760 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000761
Reid Spenceref9b9a72007-02-05 20:47:22 +0000762 // If this global has a name
Chris Lattnera09000d2004-07-14 23:03:46 +0000763 if (!Name.empty()) {
Reid Spenceref9b9a72007-02-05 20:47:22 +0000764 // if the global we're parsing has an initializer (is a definition) and
765 // has external linkage.
766 if (Initializer && Linkage != GlobalValue::InternalLinkage)
767 // If there is already a global with external linkage with this name
768 if (CurModule.CurrentModule->getGlobalVariable(Name, false)) {
769 // If we allow this GVar to get created, it will be renamed in the
770 // symbol table because it conflicts with an existing GVar. We can't
771 // allow redefinition of GVars whose linking indicates that their name
772 // must stay the same. Issue the error.
773 GenerateError("Redefinition of global variable named '" + Name +
774 "' of type '" + Ty->getDescription() + "'");
775 return 0;
776 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000777 }
778
779 // Otherwise there is no existing GV to use, create one now.
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000780 GlobalVariable *GV =
Misha Brukman5a2a3822005-05-10 22:02:28 +0000781 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +0000782 CurModule.CurrentModule, IsThreadLocal);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000783 GV->setVisibility(Visibility);
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000784 InsertValue(GV, CurModule.Values);
Chris Lattnerb2b96672005-11-12 18:21:21 +0000785 return GV;
Chris Lattnerd88998d2004-07-14 08:23:52 +0000786}
Chris Lattner8ab406d2004-07-13 07:59:27 +0000787
Reid Spencer75719bf2004-05-26 21:48:31 +0000788// setTypeName - Set the specified type to the name given. The name may be
789// null potentially, in which case this is a noop. The string passed in is
790// assumed to be a malloc'd string buffer, and is freed by this function.
791//
792// This function returns true if the type has already been defined, but is
793// allowed to be redefined in the specified context. If the name is a new name
794// for the type plane, it is inserted and false is returned.
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000795static bool setTypeName(const Type *T, char *NameStr) {
Reid Spencer1755a9a2007-02-05 17:01:20 +0000796 assert(!inFunctionScope() && "Can't give types function-local names!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000797 if (NameStr == 0) return false;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000798
Reid Spencer75719bf2004-05-26 21:48:31 +0000799 std::string Name(NameStr); // Copy string
800 free(NameStr); // Free old string
801
802 // We don't allow assigning names to void type
Reid Spencer5b7e7532006-09-28 19:28:24 +0000803 if (T == Type::VoidTy) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000804 GenerateError("Can't assign name '" + Name + "' to the void type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000805 return false;
806 }
Reid Spencer75719bf2004-05-26 21:48:31 +0000807
Chris Lattnera09000d2004-07-14 23:03:46 +0000808 // Set the type name, checking for conflicts as we do so.
809 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000810
Chris Lattnera09000d2004-07-14 23:03:46 +0000811 if (AlreadyExists) { // Inserting a name that is already defined???
812 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
Reid Spencer1755a9a2007-02-05 17:01:20 +0000813 assert(Existing && "Conflict but no matching type?!");
Chris Lattnera09000d2004-07-14 23:03:46 +0000814
Reid Spencer75719bf2004-05-26 21:48:31 +0000815 // There is only one case where this is allowed: when we are refining an
816 // opaque type. In this case, Existing will be an opaque type.
817 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
818 // We ARE replacing an opaque type!
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000819 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000820 return true;
821 }
822
823 // Otherwise, this is an attempt to redefine a type. That's okay if
824 // the redefinition is identical to the original. This will be so if
825 // Existing and T point to the same Type object. In this one case we
826 // allow the equivalent redefinition.
827 if (Existing == T) return true; // Yes, it's equal.
828
829 // Any other kind of (non-equivalent) redefinition is an error.
Reid Spencer90e2f632007-01-05 21:50:38 +0000830 GenerateError("Redefinition of type named '" + Name + "' of type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +0000831 T->getDescription() + "'");
Reid Spencer75719bf2004-05-26 21:48:31 +0000832 }
833
Reid Spencer75719bf2004-05-26 21:48:31 +0000834 return false;
835}
Chris Lattner8896eda2001-07-09 19:38:36 +0000836
Chris Lattner30c89792001-09-07 16:35:17 +0000837//===----------------------------------------------------------------------===//
838// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000839//
Chris Lattner8896eda2001-07-09 19:38:36 +0000840
Chris Lattner3b073862004-02-09 03:19:29 +0000841// TypeContains - Returns true if Ty directly contains E in it.
Chris Lattner30c89792001-09-07 16:35:17 +0000842//
843static bool TypeContains(const Type *Ty, const Type *E) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000844 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
Chris Lattner6d8dbec2004-12-08 16:13:53 +0000845 E) != Ty->subtype_end();
Chris Lattner3b073862004-02-09 03:19:29 +0000846}
847
848namespace {
849 struct UpRefRecord {
850 // NestingLevel - The number of nesting levels that need to be popped before
851 // this type is resolved.
852 unsigned NestingLevel;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000853
Chris Lattner3b073862004-02-09 03:19:29 +0000854 // LastContainedTy - This is the type at the current binding level for the
855 // type. Every time we reduce the nesting level, this gets updated.
856 const Type *LastContainedTy;
857
858 // UpRefTy - This is the actual opaque type that the upreference is
859 // represented with.
860 OpaqueType *UpRefTy;
861
862 UpRefRecord(unsigned NL, OpaqueType *URTy)
863 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
864 };
Chris Lattner30c89792001-09-07 16:35:17 +0000865}
Chris Lattner698b56e2001-07-20 19:15:08 +0000866
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000867// UpRefs - A list of the outstanding upreferences that need to be resolved.
Chris Lattner3b073862004-02-09 03:19:29 +0000868static std::vector<UpRefRecord> UpRefs;
Chris Lattner30c89792001-09-07 16:35:17 +0000869
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000870/// HandleUpRefs - Every time we finish a new layer of types, this function is
871/// called. It loops through the UpRefs vector, which is a list of the
872/// currently active types. For each type, if the up reference is contained in
873/// the newly completed type, we decrement the level count. When the level
874/// count reaches zero, the upreferenced type is the type that is passed in:
875/// thus we can complete the cycle.
876///
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000877static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner703e92f2006-08-18 17:34:24 +0000878 // If Ty isn't abstract, or if there are no up-references in it, then there is
879 // nothing to resolve here.
880 if (!ty->isAbstract() || UpRefs.empty()) return ty;
881
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000882 PATypeHolder Ty(ty);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000883 UR_OUT("Type '" << Ty->getDescription() <<
Chris Lattner5084d032001-11-02 07:46:26 +0000884 "' newly formed. Resolving upreferences.\n" <<
885 UpRefs.size() << " upreferences active!\n");
Chris Lattner026a8ce2004-02-09 18:53:54 +0000886
887 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
888 // to zero), we resolve them all together before we resolve them to Ty. At
889 // the end of the loop, if there is anything to resolve to Ty, it will be in
890 // this variable.
891 OpaqueType *TypeToResolve = 0;
892
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000893 for (unsigned i = 0; i != UpRefs.size(); ++i) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000894 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
895 << UpRefs[i].second->getDescription() << ") = "
Reid Spencer3271ed52004-07-18 00:08:11 +0000896 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
Chris Lattner3b073862004-02-09 03:19:29 +0000897 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
898 // Decrement level of upreference
899 unsigned Level = --UpRefs[i].NestingLevel;
900 UpRefs[i].LastContainedTy = Ty;
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000901 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
Misha Brukman5a2a3822005-05-10 22:02:28 +0000902 if (Level == 0) { // Upreference should be resolved!
Chris Lattner026a8ce2004-02-09 18:53:54 +0000903 if (!TypeToResolve) {
904 TypeToResolve = UpRefs[i].UpRefTy;
905 } else {
906 UR_OUT(" * Resolving upreference for "
907 << UpRefs[i].second->getDescription() << "\n";
908 std::string OldName = UpRefs[i].UpRefTy->getDescription());
909 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
910 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
911 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
912 }
Reid Spencer3271ed52004-07-18 00:08:11 +0000913 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000914 --i; // Do not skip the next element...
Chris Lattner30c89792001-09-07 16:35:17 +0000915 }
916 }
Chris Lattner8896eda2001-07-09 19:38:36 +0000917 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000918
Chris Lattner026a8ce2004-02-09 18:53:54 +0000919 if (TypeToResolve) {
920 UR_OUT(" * Resolving upreference for "
921 << UpRefs[i].second->getDescription() << "\n";
Chris Lattner16af11d2004-02-09 21:03:38 +0000922 std::string OldName = TypeToResolve->getDescription());
Chris Lattner026a8ce2004-02-09 18:53:54 +0000923 TypeToResolve->refineAbstractTypeTo(Ty);
924 }
925
Chris Lattner8896eda2001-07-09 19:38:36 +0000926 return Ty;
927}
928
Chris Lattner00950542001-06-06 20:29:01 +0000929//===----------------------------------------------------------------------===//
930// RunVMAsmParser - Define an interface to this parser
931//===----------------------------------------------------------------------===//
932//
Reid Spencere1553cc2006-12-31 05:40:12 +0000933static Module* RunParser(Module * M);
934
Chris Lattner86427632004-07-14 06:39:48 +0000935Module *llvm::RunVMAsmParser(const std::string &Filename, FILE *F) {
Chris Lattner6184feb2005-05-20 03:25:47 +0000936 set_scan_file(F);
937
Chris Lattnera2850432001-07-22 18:36:00 +0000938 CurFilename = Filename;
Chris Lattner6184feb2005-05-20 03:25:47 +0000939 return RunParser(new Module(CurFilename));
940}
Chris Lattner00950542001-06-06 20:29:01 +0000941
Chris Lattner6184feb2005-05-20 03:25:47 +0000942Module *llvm::RunVMAsmParser(const char * AsmString, Module * M) {
943 set_scan_string(AsmString);
Chris Lattner42570982003-11-26 07:24:58 +0000944
Chris Lattner6184feb2005-05-20 03:25:47 +0000945 CurFilename = "from_memory";
946 if (M == NULL) {
947 return RunParser(new Module (CurFilename));
948 } else {
949 return RunParser(M);
950 }
Chris Lattner00950542001-06-06 20:29:01 +0000951}
952
953%}
954
955%union {
Brian Gaeked0fde302003-11-11 22:41:34 +0000956 llvm::Module *ModuleVal;
957 llvm::Function *FunctionVal;
Brian Gaeked0fde302003-11-11 22:41:34 +0000958 llvm::BasicBlock *BasicBlockVal;
959 llvm::TerminatorInst *TermInstVal;
960 llvm::Instruction *InstVal;
Reid Spencer08de34b2006-12-03 05:45:44 +0000961 llvm::Constant *ConstVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000962
Reid Spencer08de34b2006-12-03 05:45:44 +0000963 const llvm::Type *PrimType;
Reid Spencere1553cc2006-12-31 05:40:12 +0000964 std::list<llvm::PATypeHolder> *TypeList;
Reid Spencer08de34b2006-12-03 05:45:44 +0000965 llvm::PATypeHolder *TypeVal;
966 llvm::Value *ValueVal;
Reid Spencer08de34b2006-12-03 05:45:44 +0000967 std::vector<llvm::Value*> *ValueList;
Reid Spencere1553cc2006-12-31 05:40:12 +0000968 llvm::ArgListType *ArgList;
969 llvm::TypeWithAttrs TypeWithAttrs;
970 llvm::TypeWithAttrsList *TypeWithAttrsList;
971 llvm::ValueRefList *ValueRefList;
972
Misha Brukman5a2a3822005-05-10 22:02:28 +0000973 // Represent the RHS of PHI node
Reid Spencer08de34b2006-12-03 05:45:44 +0000974 std::list<std::pair<llvm::Value*,
975 llvm::BasicBlock*> > *PHIList;
Brian Gaeked0fde302003-11-11 22:41:34 +0000976 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
Reid Spencer08de34b2006-12-03 05:45:44 +0000977 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000978
Brian Gaeked0fde302003-11-11 22:41:34 +0000979 llvm::GlobalValue::LinkageTypes Linkage;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000980 llvm::GlobalValue::VisibilityTypes Visibility;
Reid Spencer5694b6e2007-04-09 06:17:21 +0000981 uint16_t ParamAttrs;
Reid Spencerf2310042007-02-28 02:23:44 +0000982 llvm::APInt *APIntVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000983 int64_t SInt64Val;
984 uint64_t UInt64Val;
985 int SIntVal;
986 unsigned UIntVal;
987 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000988 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000989
Chris Lattner30c89792001-09-07 16:35:17 +0000990 char *StrVal; // This memory is strdup'd!
Reid Spencer1628cec2006-10-26 06:15:43 +0000991 llvm::ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000992
Reid Spencer08de34b2006-12-03 05:45:44 +0000993 llvm::Instruction::BinaryOps BinaryOpVal;
994 llvm::Instruction::TermOps TermOpVal;
995 llvm::Instruction::MemoryOps MemOpVal;
996 llvm::Instruction::CastOps CastOpVal;
997 llvm::Instruction::OtherOps OtherOpVal;
Reid Spencer08de34b2006-12-03 05:45:44 +0000998 llvm::ICmpInst::Predicate IPredicate;
999 llvm::FCmpInst::Predicate FPredicate;
Chris Lattner00950542001-06-06 20:29:01 +00001000}
1001
Reid Spencere1553cc2006-12-31 05:40:12 +00001002%type <ModuleVal> Module
Chris Lattner79df7c02002-03-26 18:01:55 +00001003%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +00001004%type <BasicBlockVal> BasicBlock InstructionList
1005%type <TermInstVal> BBTerminatorInst
1006%type <InstVal> Inst InstVal MemoryInst
Anton Korobeynikova80e1182007-04-28 13:45:00 +00001007%type <ConstVal> ConstVal ConstExpr AliaseeRef
Chris Lattner6cdb0112001-11-26 16:54:11 +00001008%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +00001009%type <ArgList> ArgList ArgListH
Chris Lattnerc24d2082001-06-11 15:04:20 +00001010%type <PHIList> PHIList
Reid Spencere1553cc2006-12-31 05:40:12 +00001011%type <ValueRefList> ValueRefList // For call param lists & GEP indices
1012%type <ValueList> IndexList // For GEP indices
1013%type <TypeList> TypeListI
1014%type <TypeWithAttrsList> ArgTypeList ArgTypeListI
Reid Spencer2c261782007-01-05 17:06:19 +00001015%type <TypeWithAttrs> ArgType
Chris Lattner00950542001-06-06 20:29:01 +00001016%type <JumpTable> JumpTable
Chris Lattner4ad02e72003-04-16 20:28:45 +00001017%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00001018%type <BoolVal> ThreadLocal // 'thread_local' or not
Chris Lattner15c9c032003-09-08 18:20:29 +00001019%type <BoolVal> OptVolatile // 'volatile' or not
Chris Lattnerddb6db42005-05-06 05:51:46 +00001020%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
Chris Lattneraa2c8532006-01-25 22:26:43 +00001021%type <BoolVal> OptSideEffect // 'sideeffect' or not.
Reid Spencere1553cc2006-12-31 05:40:12 +00001022%type <Linkage> GVInternalLinkage GVExternalLinkage
1023%type <Linkage> FunctionDefineLinkage FunctionDeclareLinkage
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001024%type <Linkage> AliasLinkage
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001025%type <Visibility> GVVisibilityStyle
Chris Lattner00950542001-06-06 20:29:01 +00001026
Chris Lattner2079fde2001-10-13 06:41:08 +00001027// ValueRef - Unresolved reference to a definition or BB
1028%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001029%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +00001030// Tokens and types for handling constant integer values
1031//
1032// ESINT64VAL - A negative number within long long range
1033%token <SInt64Val> ESINT64VAL
1034
1035// EUINT64VAL - A positive number within uns. long long range
1036%token <UInt64Val> EUINT64VAL
Chris Lattner00950542001-06-06 20:29:01 +00001037
Reid Spencerf2310042007-02-28 02:23:44 +00001038// ESAPINTVAL - A negative number with arbitrary precision
1039%token <APIntVal> ESAPINTVAL
1040
1041// EUAPINTVAL - A positive number with arbitrary precision
1042%token <APIntVal> EUAPINTVAL
1043
Reid Spencerb2d17862007-01-26 08:04:51 +00001044%token <UIntVal> LOCALVAL_ID GLOBALVAL_ID // %123 @123
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001045%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +00001046
1047// Built in types...
Reid Spencer2c261782007-01-05 17:06:19 +00001048%type <TypeVal> Types ResultTypes
Reid Spencere1553cc2006-12-31 05:40:12 +00001049%type <PrimType> IntType FPType PrimType // Classifications
Reid Spencer8088e9d2007-01-13 05:00:20 +00001050%token <PrimType> VOID INTTYPE
Reid Spencerb951bc02006-12-29 20:29:48 +00001051%token <PrimType> FLOAT DOUBLE LABEL
1052%token TYPE
Chris Lattner00950542001-06-06 20:29:01 +00001053
Reid Spencerb2d17862007-01-26 08:04:51 +00001054%token<StrVal> LOCALVAR GLOBALVAR LABELSTR STRINGCONSTANT ATSTRINGCONSTANT
1055%type <StrVal> LocalName OptLocalName OptLocalAssign
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001056%type <StrVal> GlobalName OptGlobalAssign GlobalAssign
Reid Spencerb2d17862007-01-26 08:04:51 +00001057%type <UIntVal> OptAlign OptCAlign
Chris Lattnerb2b96672005-11-12 18:21:21 +00001058%type <StrVal> OptSection SectionString
Chris Lattner00950542001-06-06 20:29:01 +00001059
Reid Spencer744d0362007-04-09 01:55:42 +00001060%token ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001061%token DECLARE DEFINE GLOBAL CONSTANT SECTION ALIAS VOLATILE THREAD_LOCAL
Reid Spencere1553cc2006-12-31 05:40:12 +00001062%token TO DOTDOTDOT NULL_TOK UNDEF INTERNAL LINKONCE WEAK APPENDING
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00001063%token DLLIMPORT DLLEXPORT EXTERN_WEAK
Reid Spencerb2d17862007-01-26 08:04:51 +00001064%token OPAQUE EXTERNAL TARGET TRIPLE ALIGN
Chris Lattneraa2c8532006-01-25 22:26:43 +00001065%token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001066%token CC_TOK CCC_TOK FASTCC_TOK COLDCC_TOK X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
Chris Lattner10b27112006-10-22 06:07:41 +00001067%token DATALAYOUT
Chris Lattnera8e8f162005-05-06 20:27:19 +00001068%type <UIntVal> OptCallingConv
Reid Spencer2c261782007-01-05 17:06:19 +00001069%type <ParamAttrs> OptParamAttrs ParamAttr
1070%type <ParamAttrs> OptFuncAttrs FuncAttr
Chris Lattner00950542001-06-06 20:29:01 +00001071
Misha Brukman5a2a3822005-05-10 22:02:28 +00001072// Basic Block Terminating Operators
Chris Lattner16710e92004-10-16 18:17:13 +00001073%token <TermOpVal> RET BR SWITCH INVOKE UNWIND UNREACHABLE
Chris Lattner00950542001-06-06 20:29:01 +00001074
Misha Brukman5a2a3822005-05-10 22:02:28 +00001075// Binary Operators
Reid Spencere4d87aa2006-12-23 06:05:41 +00001076%type <BinaryOpVal> ArithmeticOps LogicalOps // Binops Subcatagories
Reid Spencer0a783f72006-11-02 01:53:59 +00001077%token <BinaryOpVal> ADD SUB MUL UDIV SDIV FDIV UREM SREM FREM AND OR XOR
Reid Spencer832254e2007-02-02 02:16:23 +00001078%token <BinaryOpVal> SHL LSHR ASHR
1079
Reid Spencer08de34b2006-12-03 05:45:44 +00001080%token <OtherOpVal> ICMP FCMP
Reid Spencer08de34b2006-12-03 05:45:44 +00001081%type <IPredicate> IPredicates
Reid Spencer08de34b2006-12-03 05:45:44 +00001082%type <FPredicate> FPredicates
Reid Spencer9f746f42006-12-03 06:58:07 +00001083%token EQ NE SLT SGT SLE SGE ULT UGT ULE UGE
1084%token OEQ ONE OLT OGT OLE OGE ORD UNO UEQ UNE
Chris Lattner00950542001-06-06 20:29:01 +00001085
1086// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001087%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +00001088
Reid Spencer3da59db2006-11-27 01:05:10 +00001089// Cast Operators
1090%type <CastOpVal> CastOps
1091%token <CastOpVal> TRUNC ZEXT SEXT FPTRUNC FPEXT BITCAST
1092%token <CastOpVal> UITOFP SITOFP FPTOUI FPTOSI INTTOPTR PTRTOINT
1093
Chris Lattner027dcc52001-07-08 21:10:27 +00001094// Other Operators
Reid Spencer832254e2007-02-02 02:16:23 +00001095%token <OtherOpVal> PHI_TOK SELECT VAARG
Chris Lattner4c949082006-04-08 01:18:35 +00001096%token <OtherOpVal> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
Chris Lattnerddb6db42005-05-06 05:51:46 +00001097
Reid Spencer2c261782007-01-05 17:06:19 +00001098// Function Attributes
Reid Spencera39dfd32007-03-22 02:13:23 +00001099%token NORETURN INREG SRET NOUNWIND
Chris Lattner027dcc52001-07-08 21:10:27 +00001100
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001101// Visibility Styles
1102%token DEFAULT HIDDEN
1103
Chris Lattner00950542001-06-06 20:29:01 +00001104%start Module
1105%%
1106
Chris Lattner00950542001-06-06 20:29:01 +00001107
Misha Brukman5a2a3822005-05-10 22:02:28 +00001108// Operations that are notably excluded from this list include:
Chris Lattner00950542001-06-06 20:29:01 +00001109// RET, BR, & SWITCH because they end basic blocks and are treated specially.
1110//
Reid Spencer0a783f72006-11-02 01:53:59 +00001111ArithmeticOps: ADD | SUB | MUL | UDIV | SDIV | FDIV | UREM | SREM | FREM;
Reid Spencer832254e2007-02-02 02:16:23 +00001112LogicalOps : SHL | LSHR | ASHR | AND | OR | XOR;
Reid Spencer3da59db2006-11-27 01:05:10 +00001113CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | BITCAST |
1114 UITOFP | SITOFP | FPTOUI | FPTOSI | INTTOPTR | PTRTOINT;
Reid Spencer832254e2007-02-02 02:16:23 +00001115
Reid Spencer9f746f42006-12-03 06:58:07 +00001116IPredicates
Reid Spencer763ed5e2006-12-04 05:20:06 +00001117 : EQ { $$ = ICmpInst::ICMP_EQ; } | NE { $$ = ICmpInst::ICMP_NE; }
Reid Spencer9f746f42006-12-03 06:58:07 +00001118 | SLT { $$ = ICmpInst::ICMP_SLT; } | SGT { $$ = ICmpInst::ICMP_SGT; }
1119 | SLE { $$ = ICmpInst::ICMP_SLE; } | SGE { $$ = ICmpInst::ICMP_SGE; }
1120 | ULT { $$ = ICmpInst::ICMP_ULT; } | UGT { $$ = ICmpInst::ICMP_UGT; }
1121 | ULE { $$ = ICmpInst::ICMP_ULE; } | UGE { $$ = ICmpInst::ICMP_UGE; }
1122 ;
1123
1124FPredicates
1125 : OEQ { $$ = FCmpInst::FCMP_OEQ; } | ONE { $$ = FCmpInst::FCMP_ONE; }
1126 | OLT { $$ = FCmpInst::FCMP_OLT; } | OGT { $$ = FCmpInst::FCMP_OGT; }
1127 | OLE { $$ = FCmpInst::FCMP_OLE; } | OGE { $$ = FCmpInst::FCMP_OGE; }
1128 | ORD { $$ = FCmpInst::FCMP_ORD; } | UNO { $$ = FCmpInst::FCMP_UNO; }
1129 | UEQ { $$ = FCmpInst::FCMP_UEQ; } | UNE { $$ = FCmpInst::FCMP_UNE; }
1130 | ULT { $$ = FCmpInst::FCMP_ULT; } | UGT { $$ = FCmpInst::FCMP_UGT; }
1131 | ULE { $$ = FCmpInst::FCMP_ULE; } | UGE { $$ = FCmpInst::FCMP_UGE; }
1132 | TRUETOK { $$ = FCmpInst::FCMP_TRUE; }
1133 | FALSETOK { $$ = FCmpInst::FCMP_FALSE; }
1134 ;
Chris Lattner00950542001-06-06 20:29:01 +00001135
Chris Lattnere98dda62001-07-14 06:10:16 +00001136// These are some types that allow classification if we only want a particular
1137// thing... for example, only a signed, unsigned, or integral type.
Reid Spencera54b7cb2007-01-12 07:05:14 +00001138IntType : INTTYPE;
Chris Lattner51727be2002-06-04 21:58:56 +00001139FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +00001140
Reid Spencerb2d17862007-01-26 08:04:51 +00001141LocalName : LOCALVAR | STRINGCONSTANT;
1142OptLocalName : LocalName | /*empty*/ { $$ = 0; };
1143
1144/// OptLocalAssign - Value producing statements have an optional assignment
1145/// component.
1146OptLocalAssign : LocalName '=' {
1147 $$ = $1;
1148 CHECK_FOR_ERROR
1149 }
1150 | /*empty*/ {
1151 $$ = 0;
1152 CHECK_FOR_ERROR
1153 };
1154
1155GlobalName : GLOBALVAR | ATSTRINGCONSTANT;
1156
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001157OptGlobalAssign : GlobalAssign
Misha Brukman5a2a3822005-05-10 22:02:28 +00001158 | /*empty*/ {
1159 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00001160 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001161 };
Chris Lattner00950542001-06-06 20:29:01 +00001162
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001163GlobalAssign : GlobalName '=' {
1164 $$ = $1;
1165 CHECK_FOR_ERROR
Anton Korobeynikovc0fabcb2007-04-25 18:07:40 +00001166 };
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001167
Reid Spencerb951bc02006-12-29 20:29:48 +00001168GVInternalLinkage
1169 : INTERNAL { $$ = GlobalValue::InternalLinkage; }
1170 | WEAK { $$ = GlobalValue::WeakLinkage; }
1171 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1172 | APPENDING { $$ = GlobalValue::AppendingLinkage; }
1173 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
1174 ;
1175
1176GVExternalLinkage
1177 : DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1178 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
1179 | EXTERNAL { $$ = GlobalValue::ExternalLinkage; }
1180 ;
1181
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001182GVVisibilityStyle
1183 : /*empty*/ { $$ = GlobalValue::DefaultVisibility; }
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001184 | DEFAULT { $$ = GlobalValue::DefaultVisibility; }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001185 | HIDDEN { $$ = GlobalValue::HiddenVisibility; }
1186 ;
1187
Reid Spencere1553cc2006-12-31 05:40:12 +00001188FunctionDeclareLinkage
1189 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1190 | DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1191 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
Reid Spencerb951bc02006-12-29 20:29:48 +00001192 ;
1193
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001194FunctionDefineLinkage
Reid Spencere1553cc2006-12-31 05:40:12 +00001195 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1196 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
Reid Spencerb951bc02006-12-29 20:29:48 +00001197 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1198 | WEAK { $$ = GlobalValue::WeakLinkage; }
1199 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
Reid Spencerb951bc02006-12-29 20:29:48 +00001200 ;
Chris Lattner30c89792001-09-07 16:35:17 +00001201
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001202AliasLinkage
1203 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1204 | WEAK { $$ = GlobalValue::WeakLinkage; }
1205 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
1206 ;
1207
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00001208OptCallingConv : /*empty*/ { $$ = CallingConv::C; } |
1209 CCC_TOK { $$ = CallingConv::C; } |
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00001210 FASTCC_TOK { $$ = CallingConv::Fast; } |
1211 COLDCC_TOK { $$ = CallingConv::Cold; } |
1212 X86_STDCALLCC_TOK { $$ = CallingConv::X86_StdCall; } |
1213 X86_FASTCALLCC_TOK { $$ = CallingConv::X86_FastCall; } |
1214 CC_TOK EUINT64VAL {
Chris Lattnera8e8f162005-05-06 20:27:19 +00001215 if ((unsigned)$2 != $2)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001216 GEN_ERROR("Calling conv too large");
Chris Lattnera8e8f162005-05-06 20:27:19 +00001217 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001218 CHECK_FOR_ERROR
Chris Lattnera8e8f162005-05-06 20:27:19 +00001219 };
1220
Reid Spencer18da0722007-04-11 02:44:20 +00001221ParamAttr : ZEXT { $$ = ParamAttr::ZExt; }
1222 | SEXT { $$ = ParamAttr::SExt; }
1223 | INREG { $$ = ParamAttr::InReg; }
1224 | SRET { $$ = ParamAttr::StructRet; }
Reid Spencere1553cc2006-12-31 05:40:12 +00001225 ;
1226
Reid Spencer18da0722007-04-11 02:44:20 +00001227OptParamAttrs : /* empty */ { $$ = ParamAttr::None; }
Reid Spencer2c261782007-01-05 17:06:19 +00001228 | OptParamAttrs ParamAttr {
Reid Spencer5694b6e2007-04-09 06:17:21 +00001229 $$ = $1 | $2;
Reid Spencere1553cc2006-12-31 05:40:12 +00001230 }
1231 ;
1232
Reid Spencer18da0722007-04-11 02:44:20 +00001233FuncAttr : NORETURN { $$ = ParamAttr::NoReturn; }
1234 | NOUNWIND { $$ = ParamAttr::NoUnwind; }
Reid Spencer2c261782007-01-05 17:06:19 +00001235 | ParamAttr
1236 ;
1237
Reid Spencer18da0722007-04-11 02:44:20 +00001238OptFuncAttrs : /* empty */ { $$ = ParamAttr::None; }
Reid Spencer2c261782007-01-05 17:06:19 +00001239 | OptFuncAttrs FuncAttr {
Reid Spencer5694b6e2007-04-09 06:17:21 +00001240 $$ = $1 | $2;
Reid Spencer2c261782007-01-05 17:06:19 +00001241 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001242 ;
1243
Chris Lattner66db8e42005-11-06 06:34:12 +00001244// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1245// a comma before it.
Chris Lattner87ac9722005-11-06 06:46:28 +00001246OptAlign : /*empty*/ { $$ = 0; } |
Chris Lattnerb2b96672005-11-12 18:21:21 +00001247 ALIGN EUINT64VAL {
1248 $$ = $2;
1249 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001250 GEN_ERROR("Alignment must be a power of two");
Reid Spencer61c83e02006-08-18 08:43:06 +00001251 CHECK_FOR_ERROR
Chris Lattnerb2b96672005-11-12 18:21:21 +00001252};
Chris Lattner66db8e42005-11-06 06:34:12 +00001253OptCAlign : /*empty*/ { $$ = 0; } |
Chris Lattnerb2b96672005-11-12 18:21:21 +00001254 ',' ALIGN EUINT64VAL {
1255 $$ = $3;
1256 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001257 GEN_ERROR("Alignment must be a power of two");
Reid Spencer61c83e02006-08-18 08:43:06 +00001258 CHECK_FOR_ERROR
Chris Lattnerb2b96672005-11-12 18:21:21 +00001259};
1260
Chris Lattner66db8e42005-11-06 06:34:12 +00001261
Chris Lattner164c3782005-11-12 00:11:10 +00001262SectionString : SECTION STRINGCONSTANT {
1263 for (unsigned i = 0, e = strlen($2); i != e; ++i)
1264 if ($2[i] == '"' || $2[i] == '\\')
Reid Spencerf4fa5902007-02-05 10:16:10 +00001265 GEN_ERROR("Invalid character in section name");
Chris Lattner164c3782005-11-12 00:11:10 +00001266 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001267 CHECK_FOR_ERROR
Chris Lattner164c3782005-11-12 00:11:10 +00001268};
1269
1270OptSection : /*empty*/ { $$ = 0; } |
1271 SectionString { $$ = $1; };
Chris Lattner164c3782005-11-12 00:11:10 +00001272
Chris Lattnerb2b96672005-11-12 18:21:21 +00001273// GlobalVarAttributes - Used to pass the attributes string on a global. CurGV
1274// is set to be the global we are processing.
1275//
1276GlobalVarAttributes : /* empty */ {} |
1277 ',' GlobalVarAttribute GlobalVarAttributes {};
1278GlobalVarAttribute : SectionString {
1279 CurGV->setSection($1);
1280 free($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001281 CHECK_FOR_ERROR
Chris Lattnerb2b96672005-11-12 18:21:21 +00001282 }
1283 | ALIGN EUINT64VAL {
1284 if ($2 != 0 && !isPowerOf2_32($2))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001285 GEN_ERROR("Alignment must be a power of two");
Chris Lattnerb2b96672005-11-12 18:21:21 +00001286 CurGV->setAlignment($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001287 CHECK_FOR_ERROR
Chris Lattnerb2b96672005-11-12 18:21:21 +00001288 };
Chris Lattner164c3782005-11-12 00:11:10 +00001289
Chris Lattner30c89792001-09-07 16:35:17 +00001290//===----------------------------------------------------------------------===//
1291// Types includes all predefined types... except void, because it can only be
Reid Spencere1553cc2006-12-31 05:40:12 +00001292// used in specific contexts (function returning void for example).
Chris Lattner30c89792001-09-07 16:35:17 +00001293
1294// Derived types are added later...
1295//
Reid Spencer8088e9d2007-01-13 05:00:20 +00001296PrimType : INTTYPE | FLOAT | DOUBLE | LABEL ;
Reid Spencere1553cc2006-12-31 05:40:12 +00001297
1298Types
1299 : OPAQUE {
Reid Spencer08de34b2006-12-03 05:45:44 +00001300 $$ = new PATypeHolder(OpaqueType::get());
Reid Spencer61c83e02006-08-18 08:43:06 +00001301 CHECK_FOR_ERROR
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001302 }
1303 | PrimType {
Reid Spencer08de34b2006-12-03 05:45:44 +00001304 $$ = new PATypeHolder($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001305 CHECK_FOR_ERROR
Reid Spencere1553cc2006-12-31 05:40:12 +00001306 }
1307 | Types '*' { // Pointer type?
1308 if (*$1 == Type::LabelTy)
1309 GEN_ERROR("Cannot form a pointer to a basic block");
1310 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
1311 delete $1;
1312 CHECK_FOR_ERROR
1313 }
1314 | SymbolicValueRef { // Named types are also simple types...
1315 const Type* tmp = getTypeVal($1);
1316 CHECK_FOR_ERROR
1317 $$ = new PATypeHolder(tmp);
1318 }
1319 | '\\' EUINT64VAL { // Type UpReference
Reid Spencerf4fa5902007-02-05 10:16:10 +00001320 if ($2 > (uint64_t)~0U) GEN_ERROR("Value out of range");
Chris Lattner30c89792001-09-07 16:35:17 +00001321 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
Chris Lattner3b073862004-02-09 03:19:29 +00001322 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Reid Spencer08de34b2006-12-03 05:45:44 +00001323 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +00001324 UR_OUT("New Upreference!\n");
Reid Spencer61c83e02006-08-18 08:43:06 +00001325 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001326 }
Reid Spencer863dd882007-04-28 16:06:50 +00001327 | Types '(' ArgTypeListI ')' OptFuncAttrs {
Chris Lattner9232b992003-04-22 18:42:41 +00001328 std::vector<const Type*> Params;
Reid Spencer863dd882007-04-28 16:06:50 +00001329 ParamAttrsVector Attrs;
1330 if ($5 != ParamAttr::None) {
1331 ParamAttrsWithIndex X; X.index = 0; X.attrs = $5;
1332 Attrs.push_back(X);
1333 }
Reid Spencer5694b6e2007-04-09 06:17:21 +00001334 unsigned index = 1;
1335 TypeWithAttrsList::iterator I = $3->begin(), E = $3->end();
1336 for (; I != E; ++I, ++index) {
Reid Spencer2c9df212007-03-20 01:13:00 +00001337 const Type *Ty = I->Ty->get();
Reid Spencer2c9df212007-03-20 01:13:00 +00001338 Params.push_back(Ty);
Reid Spencer863dd882007-04-28 16:06:50 +00001339 if (Ty != Type::VoidTy)
1340 if (I->Attrs != ParamAttr::None) {
1341 ParamAttrsWithIndex X; X.index = index; X.attrs = I->Attrs;
1342 Attrs.push_back(X);
1343 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001344 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001345 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1346 if (isVarArg) Params.pop_back();
1347
Reid Spencer863dd882007-04-28 16:06:50 +00001348 ParamAttrsList *ActualAttrs = 0;
1349 if (!Attrs.empty())
1350 ActualAttrs = ParamAttrsList::get(Attrs);
1351 FunctionType *FT = FunctionType::get(*$1, Params, isVarArg, ActualAttrs);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001352 delete $3; // Delete the argument list
Reid Spencere1553cc2006-12-31 05:40:12 +00001353 delete $1; // Delete the return type handle
1354 $$ = new PATypeHolder(HandleUpRefs(FT));
Reid Spencer61c83e02006-08-18 08:43:06 +00001355 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001356 }
Reid Spencer863dd882007-04-28 16:06:50 +00001357 | VOID '(' ArgTypeListI ')' OptFuncAttrs {
Reid Spencere1553cc2006-12-31 05:40:12 +00001358 std::vector<const Type*> Params;
Reid Spencer863dd882007-04-28 16:06:50 +00001359 ParamAttrsVector Attrs;
1360 if ($5 != ParamAttr::None) {
1361 ParamAttrsWithIndex X; X.index = 0; X.attrs = $5;
1362 Attrs.push_back(X);
1363 }
Reid Spencer5694b6e2007-04-09 06:17:21 +00001364 TypeWithAttrsList::iterator I = $3->begin(), E = $3->end();
1365 unsigned index = 1;
1366 for ( ; I != E; ++I, ++index) {
Reid Spencer2c9df212007-03-20 01:13:00 +00001367 const Type* Ty = I->Ty->get();
Reid Spencer2c9df212007-03-20 01:13:00 +00001368 Params.push_back(Ty);
Reid Spencer863dd882007-04-28 16:06:50 +00001369 if (Ty != Type::VoidTy)
1370 if (I->Attrs != ParamAttr::None) {
1371 ParamAttrsWithIndex X; X.index = index; X.attrs = I->Attrs;
1372 Attrs.push_back(X);
1373 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001374 }
1375 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1376 if (isVarArg) Params.pop_back();
1377
Reid Spencer863dd882007-04-28 16:06:50 +00001378 ParamAttrsList *ActualAttrs = 0;
1379 if (!Attrs.empty())
1380 ActualAttrs = ParamAttrsList::get(Attrs);
1381
1382 FunctionType *FT = FunctionType::get($1, Params, isVarArg, ActualAttrs);
Reid Spencer2c261782007-01-05 17:06:19 +00001383 delete $3; // Delete the argument list
Reid Spencere1553cc2006-12-31 05:40:12 +00001384 $$ = new PATypeHolder(HandleUpRefs(FT));
1385 CHECK_FOR_ERROR
1386 }
1387
1388 | '[' EUINT64VAL 'x' Types ']' { // Sized array type?
Reid Spencer08de34b2006-12-03 05:45:44 +00001389 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
1390 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00001391 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001392 }
Reid Spencerac9dcb92007-02-15 03:39:18 +00001393 | '<' EUINT64VAL 'x' Types '>' { // Vector type?
Reid Spencer08de34b2006-12-03 05:45:44 +00001394 const llvm::Type* ElemTy = $4->get();
1395 if ((unsigned)$2 != $2)
1396 GEN_ERROR("Unsigned result not equal to signed result");
Chris Lattner42a75512007-01-15 02:27:26 +00001397 if (!ElemTy->isFloatingPoint() && !ElemTy->isInteger())
Reid Spencer9d6565a2007-02-15 02:26:10 +00001398 GEN_ERROR("Element type of a VectorType must be primitive");
Reid Spencer08de34b2006-12-03 05:45:44 +00001399 if (!isPowerOf2_32($2))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001400 GEN_ERROR("Vector length should be a power of 2");
Reid Spencer9d6565a2007-02-15 02:26:10 +00001401 $$ = new PATypeHolder(HandleUpRefs(VectorType::get(*$4, (unsigned)$2)));
Reid Spencer08de34b2006-12-03 05:45:44 +00001402 delete $4;
1403 CHECK_FOR_ERROR
Brian Gaeke715c90b2004-08-20 06:00:58 +00001404 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001405 | '{' TypeListI '}' { // Structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001406 std::vector<const Type*> Elements;
Reid Spencer08de34b2006-12-03 05:45:44 +00001407 for (std::list<llvm::PATypeHolder>::iterator I = $2->begin(),
Chris Lattnera08976b2005-02-22 23:13:58 +00001408 E = $2->end(); I != E; ++I)
Reid Spencer08de34b2006-12-03 05:45:44 +00001409 Elements.push_back(*I);
Misha Brukman5a2a3822005-05-10 22:02:28 +00001410
Reid Spencer08de34b2006-12-03 05:45:44 +00001411 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001412 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001413 CHECK_FOR_ERROR
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001414 }
1415 | '{' '}' { // Empty structure type?
Reid Spencer08de34b2006-12-03 05:45:44 +00001416 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Reid Spencer61c83e02006-08-18 08:43:06 +00001417 CHECK_FOR_ERROR
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001418 }
Andrew Lenharth38ecbf12006-12-08 18:06:16 +00001419 | '<' '{' TypeListI '}' '>' {
1420 std::vector<const Type*> Elements;
1421 for (std::list<llvm::PATypeHolder>::iterator I = $3->begin(),
1422 E = $3->end(); I != E; ++I)
1423 Elements.push_back(*I);
1424
1425 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements, true)));
1426 delete $3;
1427 CHECK_FOR_ERROR
1428 }
1429 | '<' '{' '}' '>' { // Empty structure type?
1430 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>(), true));
1431 CHECK_FOR_ERROR
1432 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001433 ;
1434
1435ArgType
Reid Spencer863dd882007-04-28 16:06:50 +00001436 : Types OptParamAttrs {
Reid Spencere1553cc2006-12-31 05:40:12 +00001437 $$.Ty = $1;
Reid Spencer863dd882007-04-28 16:06:50 +00001438 $$.Attrs = $2;
Reid Spencere1553cc2006-12-31 05:40:12 +00001439 }
1440 ;
1441
Reid Spencer2c261782007-01-05 17:06:19 +00001442ResultTypes
1443 : Types {
Reid Spencere1553cc2006-12-31 05:40:12 +00001444 if (!UpRefs.empty())
1445 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1446 if (!(*$1)->isFirstClassType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001447 GEN_ERROR("LLVM functions cannot return aggregate types");
Reid Spencer2c261782007-01-05 17:06:19 +00001448 $$ = $1;
Reid Spencere1553cc2006-12-31 05:40:12 +00001449 }
Reid Spencer2c261782007-01-05 17:06:19 +00001450 | VOID {
1451 $$ = new PATypeHolder(Type::VoidTy);
Reid Spencere1553cc2006-12-31 05:40:12 +00001452 }
1453 ;
1454
1455ArgTypeList : ArgType {
1456 $$ = new TypeWithAttrsList();
1457 $$->push_back($1);
1458 CHECK_FOR_ERROR
1459 }
1460 | ArgTypeList ',' ArgType {
1461 ($$=$1)->push_back($3);
1462 CHECK_FOR_ERROR
1463 }
1464 ;
1465
1466ArgTypeListI
1467 : ArgTypeList
1468 | ArgTypeList ',' DOTDOTDOT {
1469 $$=$1;
Reid Spencer18da0722007-04-11 02:44:20 +00001470 TypeWithAttrs TWA; TWA.Attrs = ParamAttr::None;
Reid Spencere1553cc2006-12-31 05:40:12 +00001471 TWA.Ty = new PATypeHolder(Type::VoidTy);
1472 $$->push_back(TWA);
1473 CHECK_FOR_ERROR
1474 }
1475 | DOTDOTDOT {
1476 $$ = new TypeWithAttrsList;
Reid Spencer18da0722007-04-11 02:44:20 +00001477 TypeWithAttrs TWA; TWA.Attrs = ParamAttr::None;
Reid Spencere1553cc2006-12-31 05:40:12 +00001478 TWA.Ty = new PATypeHolder(Type::VoidTy);
1479 $$->push_back(TWA);
1480 CHECK_FOR_ERROR
1481 }
1482 | /*empty*/ {
1483 $$ = new TypeWithAttrsList();
Reid Spencer61c83e02006-08-18 08:43:06 +00001484 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001485 };
Chris Lattner30c89792001-09-07 16:35:17 +00001486
Chris Lattner7e708292002-06-25 16:13:24 +00001487// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +00001488// declaration type lists
1489//
Reid Spencere1553cc2006-12-31 05:40:12 +00001490TypeListI : Types {
Reid Spencer08de34b2006-12-03 05:45:44 +00001491 $$ = new std::list<PATypeHolder>();
Reid Spencer2c9df212007-03-20 01:13:00 +00001492 $$->push_back(*$1);
1493 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001494 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001495 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001496 | TypeListI ',' Types {
Reid Spencer2c9df212007-03-20 01:13:00 +00001497 ($$=$1)->push_back(*$3);
1498 delete $3;
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 Lattnere98dda62001-07-14 06:10:16 +00001502// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +00001503// production is used ONLY to represent constants that show up AFTER a 'const',
1504// 'constant' or 'global' token at global scope. Constants that can be inlined
1505// into other expressions (such as integers and constexprs) are handled by the
1506// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +00001507//
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001508ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencere1553cc2006-12-31 05:40:12 +00001509 if (!UpRefs.empty())
1510 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001511 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001512 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001513 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001514 (*$1)->getDescription() + "'");
Chris Lattner30c89792001-09-07 16:35:17 +00001515 const Type *ETy = ATy->getElementType();
1516 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +00001517
Chris Lattner30c89792001-09-07 16:35:17 +00001518 // Verify that we have the correct size...
1519 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001520 GEN_ERROR("Type mismatch: constant sized array initialized with " +
Reid Spencer3271ed52004-07-18 00:08:11 +00001521 utostr($3->size()) + " arguments, but has size of " +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001522 itostr(NumElements) + "");
Chris Lattner00950542001-06-06 20:29:01 +00001523
Chris Lattner30c89792001-09-07 16:35:17 +00001524 // Verify all elements are correct type!
1525 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencer08de34b2006-12-03 05:45:44 +00001526 if (ETy != (*$3)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001527 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Reid Spencer3271ed52004-07-18 00:08:11 +00001528 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencer08de34b2006-12-03 05:45:44 +00001529 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +00001530 }
1531
Reid Spencer08de34b2006-12-03 05:45:44 +00001532 $$ = ConstantArray::get(ATy, *$3);
1533 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001534 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001535 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001536 | Types '[' ']' {
Reid Spencere1553cc2006-12-31 05:40:12 +00001537 if (!UpRefs.empty())
1538 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001539 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001540 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001541 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001542 (*$1)->getDescription() + "'");
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001543
1544 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +00001545 if (NumElements != -1 && NumElements != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001546 GEN_ERROR("Type mismatch: constant sized array initialized with 0"
Reid Spencerf4fa5902007-02-05 10:16:10 +00001547 " arguments, but has size of " + itostr(NumElements) +"");
Reid Spencer08de34b2006-12-03 05:45:44 +00001548 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
1549 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001550 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001551 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001552 | Types 'c' STRINGCONSTANT {
Reid Spencere1553cc2006-12-31 05:40:12 +00001553 if (!UpRefs.empty())
1554 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001555 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001556 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001557 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001558 (*$1)->getDescription() + "'");
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001559
Chris Lattner30c89792001-09-07 16:35:17 +00001560 int NumElements = ATy->getNumElements();
1561 const Type *ETy = ATy->getElementType();
1562 char *EndStr = UnEscapeLexed($3, true);
1563 if (NumElements != -1 && NumElements != (EndStr-$3))
Reid Spencer61c83e02006-08-18 08:43:06 +00001564 GEN_ERROR("Can't build string constant of size " +
Reid Spencer3271ed52004-07-18 00:08:11 +00001565 itostr((int)(EndStr-$3)) +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001566 " when array has size " + itostr(NumElements) + "");
Chris Lattner9232b992003-04-22 18:42:41 +00001567 std::vector<Constant*> Vals;
Reid Spencere1553cc2006-12-31 05:40:12 +00001568 if (ETy == Type::Int8Ty) {
Chris Lattneree454772006-01-23 23:05:15 +00001569 for (unsigned char *C = (unsigned char *)$3;
Reid Spencere1553cc2006-12-31 05:40:12 +00001570 C != (unsigned char*)EndStr; ++C)
1571 Vals.push_back(ConstantInt::get(ETy, *C));
Chris Lattner93750fa2001-07-28 17:48:55 +00001572 } else {
Chris Lattner30c89792001-09-07 16:35:17 +00001573 free($3);
Reid Spencerf4fa5902007-02-05 10:16:10 +00001574 GEN_ERROR("Cannot build string arrays of non byte sized elements");
Chris Lattner93750fa2001-07-28 17:48:55 +00001575 }
Chris Lattner30c89792001-09-07 16:35:17 +00001576 free($3);
Reid Spencer08de34b2006-12-03 05:45:44 +00001577 $$ = ConstantArray::get(ATy, Vals);
1578 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001579 CHECK_FOR_ERROR
Chris Lattner93750fa2001-07-28 17:48:55 +00001580 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00001581 | Types '<' ConstVector '>' { // Nonempty unsized arr
Reid Spencere1553cc2006-12-31 05:40:12 +00001582 if (!UpRefs.empty())
1583 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer9d6565a2007-02-15 02:26:10 +00001584 const VectorType *PTy = dyn_cast<VectorType>($1->get());
Brian Gaeke715c90b2004-08-20 06:00:58 +00001585 if (PTy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001586 GEN_ERROR("Cannot make packed constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001587 (*$1)->getDescription() + "'");
Brian Gaeke715c90b2004-08-20 06:00:58 +00001588 const Type *ETy = PTy->getElementType();
1589 int NumElements = PTy->getNumElements();
1590
1591 // Verify that we have the correct size...
1592 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001593 GEN_ERROR("Type mismatch: constant sized packed initialized with " +
Brian Gaeke715c90b2004-08-20 06:00:58 +00001594 utostr($3->size()) + " arguments, but has size of " +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001595 itostr(NumElements) + "");
Brian Gaeke715c90b2004-08-20 06:00:58 +00001596
1597 // Verify all elements are correct type!
1598 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencer08de34b2006-12-03 05:45:44 +00001599 if (ETy != (*$3)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001600 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Brian Gaeke715c90b2004-08-20 06:00:58 +00001601 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencer08de34b2006-12-03 05:45:44 +00001602 (*$3)[i]->getType()->getDescription() + "'.");
Brian Gaeke715c90b2004-08-20 06:00:58 +00001603 }
1604
Reid Spencer9d6565a2007-02-15 02:26:10 +00001605 $$ = ConstantVector::get(PTy, *$3);
Reid Spencer08de34b2006-12-03 05:45:44 +00001606 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001607 CHECK_FOR_ERROR
Brian Gaeke715c90b2004-08-20 06:00:58 +00001608 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001609 | Types '{' ConstVector '}' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001610 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001611 if (STy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001612 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001613 (*$1)->getDescription() + "'");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001614
Chris Lattnerc6212f12003-05-21 16:06:56 +00001615 if ($3->size() != STy->getNumContainedTypes())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001616 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattnerc6212f12003-05-21 16:06:56 +00001617
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001618 // Check to ensure that constants are compatible with the type initializer!
1619 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Reid Spencer08de34b2006-12-03 05:45:44 +00001620 if ((*$3)[i]->getType() != STy->getElementType(i))
Reid Spencer61c83e02006-08-18 08:43:06 +00001621 GEN_ERROR("Expected type '" +
Chris Lattnerd21cd802004-02-09 04:37:31 +00001622 STy->getElementType(i)->getDescription() +
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001623 "' for element #" + utostr(i) +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001624 " of structure initializer");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001625
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001626 // Check to ensure that Type is not packed
1627 if (STy->isPacked())
Chris Lattner4989b842007-04-26 05:30:35 +00001628 GEN_ERROR("Unpacked Initializer to vector type '" +
1629 STy->getDescription() + "'");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001630
Reid Spencer08de34b2006-12-03 05:45:44 +00001631 $$ = ConstantStruct::get(STy, *$3);
1632 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001633 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001634 }
Chris Lattnerc6212f12003-05-21 16:06:56 +00001635 | Types '{' '}' {
Reid Spencere1553cc2006-12-31 05:40:12 +00001636 if (!UpRefs.empty())
1637 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001638 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerc6212f12003-05-21 16:06:56 +00001639 if (STy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001640 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001641 (*$1)->getDescription() + "'");
Chris Lattnerc6212f12003-05-21 16:06:56 +00001642
1643 if (STy->getNumContainedTypes() != 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001644 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattnerc6212f12003-05-21 16:06:56 +00001645
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001646 // Check to ensure that Type is not packed
1647 if (STy->isPacked())
Chris Lattner4989b842007-04-26 05:30:35 +00001648 GEN_ERROR("Unpacked Initializer to vector type '" +
1649 STy->getDescription() + "'");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001650
1651 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1652 delete $1;
1653 CHECK_FOR_ERROR
1654 }
1655 | Types '<' '{' ConstVector '}' '>' {
1656 const StructType *STy = dyn_cast<StructType>($1->get());
1657 if (STy == 0)
1658 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001659 (*$1)->getDescription() + "'");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001660
1661 if ($4->size() != STy->getNumContainedTypes())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001662 GEN_ERROR("Illegal number of initializers for structure type");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001663
1664 // Check to ensure that constants are compatible with the type initializer!
1665 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1666 if ((*$4)[i]->getType() != STy->getElementType(i))
1667 GEN_ERROR("Expected type '" +
1668 STy->getElementType(i)->getDescription() +
1669 "' for element #" + utostr(i) +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001670 " of structure initializer");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001671
1672 // Check to ensure that Type is packed
1673 if (!STy->isPacked())
Reid Spencerac9dcb92007-02-15 03:39:18 +00001674 GEN_ERROR("Vector initializer to non-vector type '" +
1675 STy->getDescription() + "'");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001676
1677 $$ = ConstantStruct::get(STy, *$4);
1678 delete $1; delete $4;
1679 CHECK_FOR_ERROR
1680 }
1681 | Types '<' '{' '}' '>' {
1682 if (!UpRefs.empty())
1683 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1684 const StructType *STy = dyn_cast<StructType>($1->get());
1685 if (STy == 0)
1686 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001687 (*$1)->getDescription() + "'");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001688
1689 if (STy->getNumContainedTypes() != 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001690 GEN_ERROR("Illegal number of initializers for structure type");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001691
1692 // Check to ensure that Type is packed
1693 if (!STy->isPacked())
Reid Spencerac9dcb92007-02-15 03:39:18 +00001694 GEN_ERROR("Vector initializer to non-vector type '" +
1695 STy->getDescription() + "'");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001696
Reid Spencer08de34b2006-12-03 05:45:44 +00001697 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1698 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001699 CHECK_FOR_ERROR
Chris Lattnerc6212f12003-05-21 16:06:56 +00001700 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001701 | Types NULL_TOK {
Reid Spencere1553cc2006-12-31 05:40:12 +00001702 if (!UpRefs.empty())
1703 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001704 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001705 if (PTy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001706 GEN_ERROR("Cannot make null pointer constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001707 (*$1)->getDescription() + "'");
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001708
Reid Spencer08de34b2006-12-03 05:45:44 +00001709 $$ = ConstantPointerNull::get(PTy);
1710 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001711 CHECK_FOR_ERROR
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001712 }
Chris Lattner16710e92004-10-16 18:17:13 +00001713 | Types UNDEF {
Reid Spencere1553cc2006-12-31 05:40:12 +00001714 if (!UpRefs.empty())
1715 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001716 $$ = UndefValue::get($1->get());
1717 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001718 CHECK_FOR_ERROR
Chris Lattner16710e92004-10-16 18:17:13 +00001719 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001720 | Types SymbolicValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00001721 if (!UpRefs.empty())
1722 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001723 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001724 if (Ty == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001725 GEN_ERROR("Global const reference must be a pointer type");
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001726
Chris Lattner3101c252002-08-15 17:58:33 +00001727 // ConstExprs can exist in the body of a function, thus creating
Reid Spencer3271ed52004-07-18 00:08:11 +00001728 // GlobalValues whenever they refer to a variable. Because we are in
Reid Spencer186a43f2007-03-19 18:39:36 +00001729 // the context of a function, getExistingVal will search the functions
Chris Lattner3101c252002-08-15 17:58:33 +00001730 // symbol table instead of the module symbol table for the global symbol,
1731 // which throws things all off. To get around this, we just tell
Reid Spencer186a43f2007-03-19 18:39:36 +00001732 // getExistingVal that we are at global scope here.
Chris Lattner3101c252002-08-15 17:58:33 +00001733 //
Chris Lattner394f2eb2003-10-16 20:12:13 +00001734 Function *SavedCurFn = CurFun.CurrentFunction;
1735 CurFun.CurrentFunction = 0;
Chris Lattner3101c252002-08-15 17:58:33 +00001736
Reid Spencer186a43f2007-03-19 18:39:36 +00001737 Value *V = getExistingVal(Ty, $2);
Reid Spencer5b7e7532006-09-28 19:28:24 +00001738 CHECK_FOR_ERROR
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001739
Chris Lattner394f2eb2003-10-16 20:12:13 +00001740 CurFun.CurrentFunction = SavedCurFn;
Chris Lattner3101c252002-08-15 17:58:33 +00001741
Chris Lattner2079fde2001-10-13 06:41:08 +00001742 // If this is an initializer for a constant pointer, which is referencing a
1743 // (currently) undefined variable, create a stub now that shall be replaced
1744 // in the future with the right type of variable.
1745 //
1746 if (V == 0) {
Reid Spencer1755a9a2007-02-05 17:01:20 +00001747 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001748 const PointerType *PT = cast<PointerType>(Ty);
1749
1750 // First check to see if the forward references value is already created!
1751 PerModuleInfo::GlobalRefsType::iterator I =
Reid Spencer3271ed52004-07-18 00:08:11 +00001752 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Chris Lattner2079fde2001-10-13 06:41:08 +00001753
1754 if (I != CurModule.GlobalRefs.end()) {
Reid Spencer3271ed52004-07-18 00:08:11 +00001755 V = I->second; // Placeholder already exists, use it...
Chris Lattnera989b232003-12-23 20:05:15 +00001756 $2.destroy();
Chris Lattner2079fde2001-10-13 06:41:08 +00001757 } else {
Chris Lattner8a327842004-07-14 21:44:00 +00001758 std::string Name;
Reid Spencerb2d17862007-01-26 08:04:51 +00001759 if ($2.Type == ValID::GlobalName)
1760 Name = $2.Name;
1761 else if ($2.Type != ValID::GlobalID)
1762 GEN_ERROR("Invalid reference to global");
Chris Lattner8a327842004-07-14 21:44:00 +00001763
Reid Spencer3271ed52004-07-18 00:08:11 +00001764 // Create the forward referenced global.
Chris Lattnera09000d2004-07-14 23:03:46 +00001765 GlobalValue *GV;
1766 if (const FunctionType *FTy =
1767 dyn_cast<FunctionType>(PT->getElementType())) {
Chris Lattner4989b842007-04-26 05:30:35 +00001768 GV = new Function(FTy, GlobalValue::ExternalWeakLinkage, Name,
Chris Lattnera09000d2004-07-14 23:03:46 +00001769 CurModule.CurrentModule);
1770 } else {
Reid Spencer3271ed52004-07-18 00:08:11 +00001771 GV = new GlobalVariable(PT->getElementType(), false,
Chris Lattner4989b842007-04-26 05:30:35 +00001772 GlobalValue::ExternalWeakLinkage, 0,
Chris Lattnera09000d2004-07-14 23:03:46 +00001773 Name, CurModule.CurrentModule);
1774 }
Chris Lattner8a327842004-07-14 21:44:00 +00001775
Reid Spencer3271ed52004-07-18 00:08:11 +00001776 // Keep track of the fact that we have a forward ref to recycle it
1777 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1778 V = GV;
Chris Lattner2079fde2001-10-13 06:41:08 +00001779 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001780 }
1781
Reid Spencer08de34b2006-12-03 05:45:44 +00001782 $$ = cast<GlobalValue>(V);
1783 delete $1; // Free the type handle
Reid Spencer61c83e02006-08-18 08:43:06 +00001784 CHECK_FOR_ERROR
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001785 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001786 | Types ConstExpr {
Reid Spencere1553cc2006-12-31 05:40:12 +00001787 if (!UpRefs.empty())
1788 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001789 if ($1->get() != $2->getType())
Reid Spencerb4fdfdb2007-01-04 00:05:48 +00001790 GEN_ERROR("Mismatched types for constant expression: " +
1791 (*$1)->getDescription() + " and " + $2->getType()->getDescription());
Chris Lattnerd78700d2002-08-16 21:14:40 +00001792 $$ = $2;
Reid Spencer08de34b2006-12-03 05:45:44 +00001793 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001794 CHECK_FOR_ERROR
Chris Lattnerf4600482003-06-28 20:01:34 +00001795 }
1796 | Types ZEROINITIALIZER {
Reid Spencere1553cc2006-12-31 05:40:12 +00001797 if (!UpRefs.empty())
1798 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001799 const Type *Ty = $1->get();
Chris Lattner78915932004-11-28 16:45:45 +00001800 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001801 GEN_ERROR("Cannot create a null initialized value of this type");
Reid Spencer08de34b2006-12-03 05:45:44 +00001802 $$ = Constant::getNullValue(Ty);
1803 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001804 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00001805 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001806 | IntType ESINT64VAL { // integral constants
Reid Spencer9ffad0a2006-12-20 17:20:09 +00001807 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001808 GEN_ERROR("Constant value doesn't fit in type");
Reid Spencereac65742007-03-19 20:40:22 +00001809 $$ = ConstantInt::get($1, $2, true);
Reid Spencerf2310042007-02-28 02:23:44 +00001810 CHECK_FOR_ERROR
1811 }
1812 | IntType ESAPINTVAL { // arbitrary precision integer constants
1813 uint32_t BitWidth = cast<IntegerType>($1)->getBitWidth();
1814 if ($2->getBitWidth() > BitWidth) {
1815 GEN_ERROR("Constant value does not fit in type");
Reid Spenceraf1aacd2007-03-01 19:32:01 +00001816 }
1817 $2->sextOrTrunc(BitWidth);
1818 $$ = ConstantInt::get(*$2);
Reid Spencerf2310042007-02-28 02:23:44 +00001819 delete $2;
Reid Spencer9ffad0a2006-12-20 17:20:09 +00001820 CHECK_FOR_ERROR
1821 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001822 | IntType EUINT64VAL { // integral constants
Reid Spencer9ffad0a2006-12-20 17:20:09 +00001823 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001824 GEN_ERROR("Constant value doesn't fit in type");
Reid Spencereac65742007-03-19 20:40:22 +00001825 $$ = ConstantInt::get($1, $2, false);
Reid Spencerf2310042007-02-28 02:23:44 +00001826 CHECK_FOR_ERROR
1827 }
1828 | IntType EUAPINTVAL { // arbitrary precision integer constants
1829 uint32_t BitWidth = cast<IntegerType>($1)->getBitWidth();
1830 if ($2->getBitWidth() > BitWidth) {
1831 GEN_ERROR("Constant value does not fit in type");
Reid Spenceraf1aacd2007-03-01 19:32:01 +00001832 }
1833 $2->zextOrTrunc(BitWidth);
1834 $$ = ConstantInt::get(*$2);
Reid Spencerf2310042007-02-28 02:23:44 +00001835 delete $2;
Reid Spencer9ffad0a2006-12-20 17:20:09 +00001836 CHECK_FOR_ERROR
1837 }
Reid Spencer8088e9d2007-01-13 05:00:20 +00001838 | INTTYPE TRUETOK { // Boolean constants
1839 assert(cast<IntegerType>($1)->getBitWidth() == 1 && "Not Bool?");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001840 $$ = ConstantInt::getTrue();
Reid Spencer61c83e02006-08-18 08:43:06 +00001841 CHECK_FOR_ERROR
Chris Lattnerd05e3592002-08-15 18:17:28 +00001842 }
Reid Spencer8088e9d2007-01-13 05:00:20 +00001843 | INTTYPE FALSETOK { // Boolean constants
1844 assert(cast<IntegerType>($1)->getBitWidth() == 1 && "Not Bool?");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001845 $$ = ConstantInt::getFalse();
Reid Spencer61c83e02006-08-18 08:43:06 +00001846 CHECK_FOR_ERROR
Chris Lattnerd05e3592002-08-15 18:17:28 +00001847 }
1848 | FPType FPVAL { // Float & Double constants
Reid Spencer08de34b2006-12-03 05:45:44 +00001849 if (!ConstantFP::isValueValidForType($1, $2))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001850 GEN_ERROR("Floating point constant invalid for type");
Reid Spencer08de34b2006-12-03 05:45:44 +00001851 $$ = ConstantFP::get($1, $2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001852 CHECK_FOR_ERROR
Chris Lattnerd05e3592002-08-15 18:17:28 +00001853 };
1854
Chris Lattner00950542001-06-06 20:29:01 +00001855
Reid Spencer3da59db2006-11-27 01:05:10 +00001856ConstExpr: CastOps '(' ConstVal TO Types ')' {
Reid Spencere1553cc2006-12-31 05:40:12 +00001857 if (!UpRefs.empty())
1858 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001859 Constant *Val = $3;
Reid Spencer93947c32007-01-17 02:47:33 +00001860 const Type *DestTy = $5->get();
1861 if (!CastInst::castIsValid($1, $3, DestTy))
1862 GEN_ERROR("invalid cast opcode for cast from '" +
1863 Val->getType()->getDescription() + "' to '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001864 DestTy->getDescription() + "'");
Reid Spencer93947c32007-01-17 02:47:33 +00001865 $$ = ConstantExpr::getCast($1, $3, DestTy);
Reid Spencer08de34b2006-12-03 05:45:44 +00001866 delete $5;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001867 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001868 | GETELEMENTPTR '(' ConstVal IndexList ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001869 if (!isa<PointerType>($3->getType()))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001870 GEN_ERROR("GetElementPtr requires a pointer operand");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001871
Reid Spencer08de34b2006-12-03 05:45:44 +00001872 const Type *IdxTy =
Chris Lattnerc856c7a2007-02-13 00:57:40 +00001873 GetElementPtrInst::getIndexedType($3->getType(), &(*$4)[0], $4->size(),
1874 true);
Reid Spencer08de34b2006-12-03 05:45:44 +00001875 if (!IdxTy)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001876 GEN_ERROR("Index list invalid for constant getelementptr");
Reid Spencer08de34b2006-12-03 05:45:44 +00001877
Chris Lattnere0135402007-01-31 04:43:46 +00001878 SmallVector<Constant*, 8> IdxVec;
Reid Spencer08de34b2006-12-03 05:45:44 +00001879 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1880 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001881 IdxVec.push_back(C);
1882 else
Reid Spencerf4fa5902007-02-05 10:16:10 +00001883 GEN_ERROR("Indices to constant getelementptr must be constants");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001884
Chris Lattnerd78700d2002-08-16 21:14:40 +00001885 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001886
Chris Lattnere0135402007-01-31 04:43:46 +00001887 $$ = ConstantExpr::getGetElementPtr($3, &IdxVec[0], IdxVec.size());
Reid Spencer61c83e02006-08-18 08:43:06 +00001888 CHECK_FOR_ERROR
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001889 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001890 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer4fe16d62007-01-11 18:21:29 +00001891 if ($3->getType() != Type::Int1Ty)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001892 GEN_ERROR("Select condition must be of boolean type");
Reid Spencer08de34b2006-12-03 05:45:44 +00001893 if ($5->getType() != $7->getType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001894 GEN_ERROR("Select operand types must match");
Reid Spencer08de34b2006-12-03 05:45:44 +00001895 $$ = ConstantExpr::getSelect($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001896 CHECK_FOR_ERROR
Chris Lattner76f0ff32004-03-12 05:51:36 +00001897 }
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001898 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001899 if ($3->getType() != $5->getType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001900 GEN_ERROR("Binary operator types must match");
Reid Spencer1628cec2006-10-26 06:15:43 +00001901 CHECK_FOR_ERROR;
Reid Spencerc6e956e2006-12-05 19:15:41 +00001902 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001903 }
1904 | LogicalOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001905 if ($3->getType() != $5->getType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001906 GEN_ERROR("Logical operator types must match");
Chris Lattner42a75512007-01-15 02:27:26 +00001907 if (!$3->getType()->isInteger()) {
Reid Spencer9d6565a2007-02-15 02:26:10 +00001908 if (Instruction::isShift($1) || !isa<VectorType>($3->getType()) ||
1909 !cast<VectorType>($3->getType())->getElementType()->isInteger())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001910 GEN_ERROR("Logical operator requires integral operands");
Chris Lattner0a017832005-12-21 18:31:29 +00001911 }
Reid Spencer08de34b2006-12-03 05:45:44 +00001912 $$ = ConstantExpr::get($1, $3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001913 CHECK_FOR_ERROR
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001914 }
Reid Spencer763ed5e2006-12-04 05:20:06 +00001915 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
1916 if ($4->getType() != $6->getType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001917 GEN_ERROR("icmp operand types must match");
Reid Spencer763ed5e2006-12-04 05:20:06 +00001918 $$ = ConstantExpr::getICmp($2, $4, $6);
Reid Spencer08de34b2006-12-03 05:45:44 +00001919 }
Reid Spencer763ed5e2006-12-04 05:20:06 +00001920 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
1921 if ($4->getType() != $6->getType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001922 GEN_ERROR("fcmp operand types must match");
Reid Spencer763ed5e2006-12-04 05:20:06 +00001923 $$ = ConstantExpr::getFCmp($2, $4, $6);
Reid Spencer08de34b2006-12-03 05:45:44 +00001924 }
Robert Bocchino9c62b562006-01-10 19:04:32 +00001925 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001926 if (!ExtractElementInst::isValidOperands($3, $5))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001927 GEN_ERROR("Invalid extractelement operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00001928 $$ = ConstantExpr::getExtractElement($3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001929 CHECK_FOR_ERROR
Chris Lattnerca73cd82006-04-08 03:53:34 +00001930 }
1931 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001932 if (!InsertElementInst::isValidOperands($3, $5, $7))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001933 GEN_ERROR("Invalid insertelement operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00001934 $$ = ConstantExpr::getInsertElement($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001935 CHECK_FOR_ERROR
Chris Lattnerca73cd82006-04-08 03:53:34 +00001936 }
1937 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001938 if (!ShuffleVectorInst::isValidOperands($3, $5, $7))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001939 GEN_ERROR("Invalid shufflevector operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00001940 $$ = ConstantExpr::getShuffleVector($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001941 CHECK_FOR_ERROR
Chris Lattner699f1eb2002-08-14 17:12:33 +00001942 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001943
Chris Lattnerca73cd82006-04-08 03:53:34 +00001944
Misha Brukmanbc0e9982003-07-14 17:20:40 +00001945// ConstVector - A list of comma separated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001946ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001947 ($$ = $1)->push_back($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00001948 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001949 }
1950 | ConstVal {
Reid Spencer08de34b2006-12-03 05:45:44 +00001951 $$ = new std::vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001952 $$->push_back($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001953 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001954 };
Chris Lattner00950542001-06-06 20:29:01 +00001955
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001956
Chris Lattner1781aca2001-09-18 04:00:54 +00001957// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001958GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001959
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00001960// ThreadLocal
1961ThreadLocal : THREAD_LOCAL { $$ = true; } | { $$ = false; };
1962
Anton Korobeynikova80e1182007-04-28 13:45:00 +00001963// AliaseeRef - Match either GlobalValue or bitcast to GlobalValue.
1964AliaseeRef : ResultTypes SymbolicValueRef {
1965 const Type* VTy = $1->get();
1966 Value *V = getVal(VTy, $2);
1967 GlobalValue* Aliasee = dyn_cast<GlobalValue>(V);
1968 if (!Aliasee)
1969 GEN_ERROR("Aliases can be created only to global values");
1970
1971 $$ = Aliasee;
1972 CHECK_FOR_ERROR
1973 delete $1;
1974 }
1975 | BITCAST '(' AliaseeRef TO Types ')' {
1976 Constant *Val = $3;
1977 const Type *DestTy = $5->get();
1978 if (!CastInst::castIsValid($1, $3, DestTy))
1979 GEN_ERROR("invalid cast opcode for cast from '" +
1980 Val->getType()->getDescription() + "' to '" +
1981 DestTy->getDescription() + "'");
1982
1983 $$ = ConstantExpr::getCast($1, $3, DestTy);
1984 CHECK_FOR_ERROR
1985 delete $5;
1986 };
Chris Lattner00950542001-06-06 20:29:01 +00001987
Chris Lattner0e73ce62002-05-02 19:11:13 +00001988//===----------------------------------------------------------------------===//
1989// Rules to match Modules
1990//===----------------------------------------------------------------------===//
1991
1992// Module rule: Capture the result of parsing the whole file into a result
1993// variable...
1994//
Reid Spencerb951bc02006-12-29 20:29:48 +00001995Module
1996 : DefinitionList {
1997 $$ = ParserResult = CurModule.CurrentModule;
1998 CurModule.ModuleDone();
1999 CHECK_FOR_ERROR;
2000 }
2001 | /*empty*/ {
2002 $$ = ParserResult = CurModule.CurrentModule;
2003 CurModule.ModuleDone();
2004 CHECK_FOR_ERROR;
2005 }
2006 ;
Chris Lattner0e73ce62002-05-02 19:11:13 +00002007
Reid Spencerb951bc02006-12-29 20:29:48 +00002008DefinitionList
2009 : Definition
2010 | DefinitionList Definition
2011 ;
2012
2013Definition
Jeff Cohen361c3ef2007-01-21 19:19:31 +00002014 : DEFINE { CurFun.isDeclare = false; } Function {
Chris Lattner394f2eb2003-10-16 20:12:13 +00002015 CurFun.FunctionDone();
Reid Spencer61c83e02006-08-18 08:43:06 +00002016 CHECK_FOR_ERROR
Reid Spencerb951bc02006-12-29 20:29:48 +00002017 }
2018 | DECLARE { CurFun.isDeclare = true; } FunctionProto {
Reid Spencer61c83e02006-08-18 08:43:06 +00002019 CHECK_FOR_ERROR
Chris Lattner0e73ce62002-05-02 19:11:13 +00002020 }
Reid Spencerb951bc02006-12-29 20:29:48 +00002021 | MODULE ASM_TOK AsmBlock {
Reid Spencer61c83e02006-08-18 08:43:06 +00002022 CHECK_FOR_ERROR
Chris Lattneree454772006-01-23 23:05:15 +00002023 }
Reid Spencerb2d17862007-01-26 08:04:51 +00002024 | OptLocalAssign TYPE Types {
Reid Spencere1553cc2006-12-31 05:40:12 +00002025 if (!UpRefs.empty())
2026 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattner4a42e902001-10-22 05:56:09 +00002027 // Eagerly resolve types. This is not an optimization, this is a
2028 // requirement that is due to the fact that we could have this:
2029 //
2030 // %list = type { %list * }
2031 // %list = type { %list * } ; repeated type decl
2032 //
2033 // If types are not resolved eagerly, then the two types will not be
2034 // determined to be the same type!
2035 //
Reid Spencerb951bc02006-12-29 20:29:48 +00002036 ResolveTypeTo($1, *$3);
Chris Lattner4a42e902001-10-22 05:56:09 +00002037
Reid Spencerb951bc02006-12-29 20:29:48 +00002038 if (!setTypeName(*$3, $1) && !$1) {
Reid Spencer5b7e7532006-09-28 19:28:24 +00002039 CHECK_FOR_ERROR
Chris Lattner8c89a0a2004-07-13 08:10:10 +00002040 // If this is a named type that is not a redefinition, add it to the slot
2041 // table.
Reid Spencerb951bc02006-12-29 20:29:48 +00002042 CurModule.Types.push_back(*$3);
Chris Lattner30c89792001-09-07 16:35:17 +00002043 }
Reid Spencer08de34b2006-12-03 05:45:44 +00002044
Reid Spencerb951bc02006-12-29 20:29:48 +00002045 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002046 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00002047 }
Reid Spencerb2d17862007-01-26 08:04:51 +00002048 | OptLocalAssign TYPE VOID {
Reid Spencere1553cc2006-12-31 05:40:12 +00002049 ResolveTypeTo($1, $3);
2050
2051 if (!setTypeName($3, $1) && !$1) {
2052 CHECK_FOR_ERROR
2053 // If this is a named type that is not a redefinition, add it to the slot
2054 // table.
2055 CurModule.Types.push_back($3);
2056 }
2057 CHECK_FOR_ERROR
2058 }
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002059 | OptGlobalAssign GVVisibilityStyle ThreadLocal GlobalType ConstVal {
Reid Spencerb2d17862007-01-26 08:04:51 +00002060 /* "Externally Visible" Linkage */
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002061 if ($5 == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002062 GEN_ERROR("Global value initializer is not a constant");
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002063 CurGV = ParseGlobalVariable($1, GlobalValue::ExternalLinkage,
2064 $2, $4, $5->getType(), $5, $3);
Reid Spencerb951bc02006-12-29 20:29:48 +00002065 CHECK_FOR_ERROR
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002066 } GlobalVarAttributes {
2067 CurGV = 0;
2068 }
Chris Lattner4989b842007-04-26 05:30:35 +00002069 | OptGlobalAssign GVInternalLinkage GVVisibilityStyle ThreadLocal GlobalType
2070 ConstVal {
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002071 if ($6 == 0)
2072 GEN_ERROR("Global value initializer is not a constant");
2073 CurGV = ParseGlobalVariable($1, $2, $3, $5, $6->getType(), $6, $4);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002074 CHECK_FOR_ERROR
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002075 } GlobalVarAttributes {
2076 CurGV = 0;
2077 }
Chris Lattner4989b842007-04-26 05:30:35 +00002078 | OptGlobalAssign GVExternalLinkage GVVisibilityStyle ThreadLocal GlobalType
2079 Types {
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002080 if (!UpRefs.empty())
2081 GEN_ERROR("Invalid upreference in type: " + (*$6)->getDescription());
2082 CurGV = ParseGlobalVariable($1, $2, $3, $5, *$6, 0, $4);
2083 CHECK_FOR_ERROR
2084 delete $6;
Reid Spencer5b7e7532006-09-28 19:28:24 +00002085 } GlobalVarAttributes {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002086 CurGV = 0;
2087 CHECK_FOR_ERROR
2088 }
Anton Korobeynikova80e1182007-04-28 13:45:00 +00002089 | OptGlobalAssign GVVisibilityStyle ALIAS AliasLinkage AliaseeRef {
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00002090 std::string Name($1);
2091 if (Name.empty())
Anton Korobeynikova80e1182007-04-28 13:45:00 +00002092 GEN_ERROR("Alias name cannot be empty");
2093
2094 Constant* Aliasee = $5;
2095 if (Aliasee == 0)
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00002096 GEN_ERROR(std::string("Invalid aliasee for alias: ") + $1);
Anton Korobeynikova80e1182007-04-28 13:45:00 +00002097
2098 GlobalAlias* GA = new GlobalAlias(Aliasee->getType(), $4, Name, Aliasee,
2099 CurModule.CurrentModule);
2100 GA->setVisibility($2);
2101 InsertValue(GA, CurModule.Values);
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00002102 CHECK_FOR_ERROR
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00002103 }
Reid Spencerb951bc02006-12-29 20:29:48 +00002104 | TARGET TargetDefinition {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002105 CHECK_FOR_ERROR
2106 }
Reid Spencerb951bc02006-12-29 20:29:48 +00002107 | DEPLIBS '=' LibrariesDefinition {
Reid Spencer61c83e02006-08-18 08:43:06 +00002108 CHECK_FOR_ERROR
Chris Lattnere98dda62001-07-14 06:10:16 +00002109 }
Reid Spencerb951bc02006-12-29 20:29:48 +00002110 ;
Chris Lattner00950542001-06-06 20:29:01 +00002111
2112
Chris Lattneree454772006-01-23 23:05:15 +00002113AsmBlock : STRINGCONSTANT {
Chris Lattner66316012006-01-24 04:14:29 +00002114 const std::string &AsmSoFar = CurModule.CurrentModule->getModuleInlineAsm();
Chris Lattneree454772006-01-23 23:05:15 +00002115 char *EndStr = UnEscapeLexed($1, true);
2116 std::string NewAsm($1, EndStr);
2117 free($1);
2118
2119 if (AsmSoFar.empty())
Chris Lattner66316012006-01-24 04:14:29 +00002120 CurModule.CurrentModule->setModuleInlineAsm(NewAsm);
Chris Lattneree454772006-01-23 23:05:15 +00002121 else
Chris Lattner66316012006-01-24 04:14:29 +00002122 CurModule.CurrentModule->setModuleInlineAsm(AsmSoFar+"\n"+NewAsm);
Reid Spencer61c83e02006-08-18 08:43:06 +00002123 CHECK_FOR_ERROR
Chris Lattneree454772006-01-23 23:05:15 +00002124};
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00002125
Reid Spencerb2d17862007-01-26 08:04:51 +00002126TargetDefinition : TRIPLE '=' STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00002127 CurModule.CurrentModule->setTargetTriple($3);
2128 free($3);
John Criswell2f6a8b12006-10-24 19:09:48 +00002129 }
Chris Lattner10b27112006-10-22 06:07:41 +00002130 | DATALAYOUT '=' STRINGCONSTANT {
Owen Andersoncf7ff2b2006-10-18 02:21:12 +00002131 CurModule.CurrentModule->setDataLayout($3);
2132 free($3);
Owen Andersoncf7ff2b2006-10-18 02:21:12 +00002133 };
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00002134
Reid Spencerfeaf10e2004-07-25 21:30:51 +00002135LibrariesDefinition : '[' LibList ']';
Reid Spencer0be13e72004-07-25 17:58:28 +00002136
2137LibList : LibList ',' STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00002138 CurModule.CurrentModule->addLibrary($3);
2139 free($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002140 CHECK_FOR_ERROR
Reid Spencer0be13e72004-07-25 17:58:28 +00002141 }
2142 | STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00002143 CurModule.CurrentModule->addLibrary($1);
2144 free($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002145 CHECK_FOR_ERROR
Reid Spencer0be13e72004-07-25 17:58:28 +00002146 }
2147 | /* empty: end of list */ {
Reid Spencer61c83e02006-08-18 08:43:06 +00002148 CHECK_FOR_ERROR
Reid Spencer0be13e72004-07-25 17:58:28 +00002149 }
2150 ;
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00002151
Chris Lattner00950542001-06-06 20:29:01 +00002152//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00002153// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00002154//===----------------------------------------------------------------------===//
2155
Reid Spencerb2d17862007-01-26 08:04:51 +00002156ArgListH : ArgListH ',' Types OptParamAttrs OptLocalName {
Reid Spencere1553cc2006-12-31 05:40:12 +00002157 if (!UpRefs.empty())
2158 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
2159 if (*$3 == Type::VoidTy)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002160 GEN_ERROR("void typed arguments are invalid");
Reid Spencere1553cc2006-12-31 05:40:12 +00002161 ArgListEntry E; E.Attrs = $4; E.Ty = $3; E.Name = $5;
Chris Lattner69da5cf2002-10-13 20:57:00 +00002162 $$ = $1;
Reid Spencere1553cc2006-12-31 05:40:12 +00002163 $1->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002164 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002165 }
Reid Spencerb2d17862007-01-26 08:04:51 +00002166 | Types OptParamAttrs OptLocalName {
Reid Spencere1553cc2006-12-31 05:40:12 +00002167 if (!UpRefs.empty())
2168 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2169 if (*$1 == Type::VoidTy)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002170 GEN_ERROR("void typed arguments are invalid");
Reid Spencere1553cc2006-12-31 05:40:12 +00002171 ArgListEntry E; E.Attrs = $2; E.Ty = $1; E.Name = $3;
2172 $$ = new ArgListType;
2173 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002174 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002175 };
Chris Lattner00950542001-06-06 20:29:01 +00002176
2177ArgList : ArgListH {
2178 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002179 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002180 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00002181 | ArgListH ',' DOTDOTDOT {
2182 $$ = $1;
Reid Spencere1553cc2006-12-31 05:40:12 +00002183 struct ArgListEntry E;
2184 E.Ty = new PATypeHolder(Type::VoidTy);
2185 E.Name = 0;
Reid Spencer18da0722007-04-11 02:44:20 +00002186 E.Attrs = ParamAttr::None;
Reid Spencere1553cc2006-12-31 05:40:12 +00002187 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002188 CHECK_FOR_ERROR
Chris Lattner69da5cf2002-10-13 20:57:00 +00002189 }
2190 | DOTDOTDOT {
Reid Spencere1553cc2006-12-31 05:40:12 +00002191 $$ = new ArgListType;
2192 struct ArgListEntry E;
2193 E.Ty = new PATypeHolder(Type::VoidTy);
2194 E.Name = 0;
Reid Spencer18da0722007-04-11 02:44:20 +00002195 E.Attrs = ParamAttr::None;
Reid Spencere1553cc2006-12-31 05:40:12 +00002196 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002197 CHECK_FOR_ERROR
Chris Lattner69da5cf2002-10-13 20:57:00 +00002198 }
Chris Lattner00950542001-06-06 20:29:01 +00002199 | /* empty */ {
2200 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00002201 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002202 };
Chris Lattner00950542001-06-06 20:29:01 +00002203
Reid Spencerb2d17862007-01-26 08:04:51 +00002204FunctionHeaderH : OptCallingConv ResultTypes GlobalName '(' ArgList ')'
Reid Spencer2c261782007-01-05 17:06:19 +00002205 OptFuncAttrs OptSection OptAlign {
Chris Lattnera8e8f162005-05-06 20:27:19 +00002206 UnEscapeLexed($3);
2207 std::string FunctionName($3);
2208 free($3); // Free strdup'd memory!
Chris Lattnerdda71962001-11-26 18:54:16 +00002209
Reid Spencer98b3c5c2007-01-02 21:53:43 +00002210 // Check the function result for abstractness if this is a define. We should
2211 // have no abstract types at this point
Reid Spencer2c261782007-01-05 17:06:19 +00002212 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved($2))
2213 GEN_ERROR("Reference to abstract result: "+ $2->get()->getDescription());
Reid Spencer98b3c5c2007-01-02 21:53:43 +00002214
Chris Lattner9232b992003-04-22 18:42:41 +00002215 std::vector<const Type*> ParamTypeList;
Reid Spencer4f859aa2007-04-22 05:46:44 +00002216 ParamAttrsVector Attrs;
2217 if ($7 != ParamAttr::None) {
2218 ParamAttrsWithIndex PAWI; PAWI.index = 0; PAWI.attrs = $7;
2219 Attrs.push_back(PAWI);
2220 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00002221 if ($5) { // If there are arguments...
Reid Spencer5694b6e2007-04-09 06:17:21 +00002222 unsigned index = 1;
2223 for (ArgListType::iterator I = $5->begin(); I != $5->end(); ++I, ++index) {
Reid Spencere1553cc2006-12-31 05:40:12 +00002224 const Type* Ty = I->Ty->get();
Reid Spencer98b3c5c2007-01-02 21:53:43 +00002225 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved(I->Ty))
2226 GEN_ERROR("Reference to abstract argument: " + Ty->getDescription());
Reid Spencere1553cc2006-12-31 05:40:12 +00002227 ParamTypeList.push_back(Ty);
2228 if (Ty != Type::VoidTy)
Reid Spencer4f859aa2007-04-22 05:46:44 +00002229 if (I->Attrs != ParamAttr::None) {
2230 ParamAttrsWithIndex PAWI; PAWI.index = index; PAWI.attrs = I->Attrs;
2231 Attrs.push_back(PAWI);
2232 }
Reid Spencere1553cc2006-12-31 05:40:12 +00002233 }
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002234 }
Chris Lattner00950542001-06-06 20:29:01 +00002235
Chris Lattner2079fde2001-10-13 06:41:08 +00002236 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
2237 if (isVarArg) ParamTypeList.pop_back();
2238
Reid Spencer4f859aa2007-04-22 05:46:44 +00002239 ParamAttrsList *PAL = 0;
2240 if (!Attrs.empty())
2241 PAL = ParamAttrsList::get(Attrs);
Reid Spencer5694b6e2007-04-09 06:17:21 +00002242
Reid Spencer863dd882007-04-28 16:06:50 +00002243 FunctionType *FT = FunctionType::get(*$2, ParamTypeList, isVarArg, PAL);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002244 const PointerType *PFT = PointerType::get(FT);
Reid Spencer2c261782007-01-05 17:06:19 +00002245 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002246
Chris Lattnera09000d2004-07-14 23:03:46 +00002247 ValID ID;
2248 if (!FunctionName.empty()) {
Reid Spencerb2d17862007-01-26 08:04:51 +00002249 ID = ValID::createGlobalName((char*)FunctionName.c_str());
Chris Lattnera09000d2004-07-14 23:03:46 +00002250 } else {
Reid Spencer186a43f2007-03-19 18:39:36 +00002251 ID = ValID::createGlobalID(CurModule.Values.size());
Chris Lattnera09000d2004-07-14 23:03:46 +00002252 }
2253
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00002254 Function *Fn = 0;
Chris Lattnera09000d2004-07-14 23:03:46 +00002255 // See if this function was forward referenced. If so, recycle the object.
Reid Spencer863dd882007-04-28 16:06:50 +00002256 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
Chris Lattnera09000d2004-07-14 23:03:46 +00002257 // Move the function to the end of the list, from whereever it was
2258 // previously inserted.
2259 Fn = cast<Function>(FWRef);
2260 CurModule.CurrentModule->getFunctionList().remove(Fn);
2261 CurModule.CurrentModule->getFunctionList().push_back(Fn);
2262 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
Reid Spenceref9b9a72007-02-05 20:47:22 +00002263 (Fn = CurModule.CurrentModule->getFunction(FunctionName))) {
Reid Spencer863dd882007-04-28 16:06:50 +00002264 if (Fn->getFunctionType() != FT) {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002265 // The existing function doesn't have the same type. This is an overload
2266 // error.
2267 GEN_ERROR("Overload of function '" + FunctionName + "' not permitted.");
2268 } else if (!CurFun.isDeclare && !Fn->isDeclaration()) {
2269 // Neither the existing or the current function is a declaration and they
2270 // have the same name and same type. Clearly this is a redefinition.
Reid Spencerf4fa5902007-02-05 10:16:10 +00002271 GEN_ERROR("Redefinition of function '" + FunctionName + "'");
Reid Spenceref9b9a72007-02-05 20:47:22 +00002272 } if (Fn->isDeclaration()) {
2273 // Make sure to strip off any argument names so we can't get conflicts.
Chris Lattnere4d5c442005-03-15 04:54:21 +00002274 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
Chris Lattnera09000d2004-07-14 23:03:46 +00002275 AI != AE; ++AI)
2276 AI->setName("");
Reid Spenceref9b9a72007-02-05 20:47:22 +00002277 }
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00002278 } else { // Not already defined?
Chris Lattner4989b842007-04-26 05:30:35 +00002279 Fn = new Function(FT, GlobalValue::ExternalWeakLinkage, FunctionName,
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00002280 CurModule.CurrentModule);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002281
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00002282 InsertValue(Fn, CurModule.Values);
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00002283 }
Chris Lattner00950542001-06-06 20:29:01 +00002284
Chris Lattner394f2eb2003-10-16 20:12:13 +00002285 CurFun.FunctionStart(Fn);
Anton Korobeynikov93c2b372006-09-17 13:06:18 +00002286
2287 if (CurFun.isDeclare) {
2288 // If we have declaration, always overwrite linkage. This will allow us to
2289 // correctly handle cases, when pointer to function is passed as argument to
2290 // another function.
2291 Fn->setLinkage(CurFun.Linkage);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002292 Fn->setVisibility(CurFun.Visibility);
Anton Korobeynikov93c2b372006-09-17 13:06:18 +00002293 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00002294 Fn->setCallingConv($1);
Reid Spencer2c261782007-01-05 17:06:19 +00002295 Fn->setAlignment($9);
2296 if ($8) {
2297 Fn->setSection($8);
2298 free($8);
Chris Lattner164c3782005-11-12 00:11:10 +00002299 }
Chris Lattner00950542001-06-06 20:29:01 +00002300
Chris Lattner7e708292002-06-25 16:13:24 +00002301 // Add all of the arguments we parsed to the function...
Chris Lattnera8e8f162005-05-06 20:27:19 +00002302 if ($5) { // Is null if empty...
Chris Lattner69da5cf2002-10-13 20:57:00 +00002303 if (isVarArg) { // Nuke the last entry
Reid Spenceref9b9a72007-02-05 20:47:22 +00002304 assert($5->back().Ty->get() == Type::VoidTy && $5->back().Name == 0 &&
Reid Spencer1755a9a2007-02-05 17:01:20 +00002305 "Not a varargs marker!");
Reid Spencere1553cc2006-12-31 05:40:12 +00002306 delete $5->back().Ty;
Chris Lattnera8e8f162005-05-06 20:27:19 +00002307 $5->pop_back(); // Delete the last entry
Chris Lattner69da5cf2002-10-13 20:57:00 +00002308 }
Chris Lattnere4d5c442005-03-15 04:54:21 +00002309 Function::arg_iterator ArgIt = Fn->arg_begin();
Reid Spenceref9b9a72007-02-05 20:47:22 +00002310 Function::arg_iterator ArgEnd = Fn->arg_end();
Reid Spencere1553cc2006-12-31 05:40:12 +00002311 unsigned Idx = 1;
Reid Spenceref9b9a72007-02-05 20:47:22 +00002312 for (ArgListType::iterator I = $5->begin();
2313 I != $5->end() && ArgIt != ArgEnd; ++I, ++ArgIt) {
Reid Spencere1553cc2006-12-31 05:40:12 +00002314 delete I->Ty; // Delete the typeholder...
Reid Spenceref9b9a72007-02-05 20:47:22 +00002315 setValueName(ArgIt, I->Name); // Insert arg into symtab...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002316 CHECK_FOR_ERROR
Chris Lattner69da5cf2002-10-13 20:57:00 +00002317 InsertValue(ArgIt);
Reid Spencere1553cc2006-12-31 05:40:12 +00002318 Idx++;
Chris Lattner00950542001-06-06 20:29:01 +00002319 }
Reid Spencer08de34b2006-12-03 05:45:44 +00002320
Chris Lattnera8e8f162005-05-06 20:27:19 +00002321 delete $5; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00002322 }
Reid Spencer61c83e02006-08-18 08:43:06 +00002323 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002324};
Chris Lattner00950542001-06-06 20:29:01 +00002325
Chris Lattner9b02cc32002-05-03 18:23:48 +00002326BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
2327
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002328FunctionHeader : FunctionDefineLinkage GVVisibilityStyle FunctionHeaderH BEGIN {
Chris Lattner394f2eb2003-10-16 20:12:13 +00002329 $$ = CurFun.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00002330
Chris Lattner4ad02e72003-04-16 20:28:45 +00002331 // Make sure that we keep track of the linkage type even if there was a
2332 // previous "declare".
2333 $$->setLinkage($1);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002334 $$->setVisibility($2);
Chris Lattner51727be2002-06-04 21:58:56 +00002335};
Chris Lattner00950542001-06-06 20:29:01 +00002336
Chris Lattner9b02cc32002-05-03 18:23:48 +00002337END : ENDTOK | '}'; // Allow end of '}' to end a function
2338
Chris Lattner79df7c02002-03-26 18:01:55 +00002339Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00002340 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002341 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002342};
Chris Lattner00950542001-06-06 20:29:01 +00002343
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002344FunctionProto : FunctionDeclareLinkage GVVisibilityStyle FunctionHeaderH {
Reid Spencere1553cc2006-12-31 05:40:12 +00002345 CurFun.CurrentFunction->setLinkage($1);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002346 CurFun.CurrentFunction->setVisibility($2);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002347 $$ = CurFun.CurrentFunction;
2348 CurFun.FunctionDone();
2349 CHECK_FOR_ERROR
2350 };
Chris Lattner00950542001-06-06 20:29:01 +00002351
2352//===----------------------------------------------------------------------===//
2353// Rules to match Basic Blocks
2354//===----------------------------------------------------------------------===//
2355
Chris Lattneraa2c8532006-01-25 22:26:43 +00002356OptSideEffect : /* empty */ {
2357 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002358 CHECK_FOR_ERROR
Chris Lattneraa2c8532006-01-25 22:26:43 +00002359 }
2360 | SIDEEFFECT {
2361 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002362 CHECK_FOR_ERROR
Chris Lattneraa2c8532006-01-25 22:26:43 +00002363 };
2364
Chris Lattner00950542001-06-06 20:29:01 +00002365ConstValueRef : ESINT64VAL { // A reference to a direct constant
2366 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002367 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002368 }
2369 | EUINT64VAL {
2370 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002371 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002372 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00002373 | FPVAL { // Perhaps it's an FP constant?
2374 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002375 CHECK_FOR_ERROR
Chris Lattner3d52b2f2001-07-15 00:17:01 +00002376 }
Chris Lattner91ef4602004-03-31 03:49:47 +00002377 | TRUETOK {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00002378 $$ = ValID::create(ConstantInt::getTrue());
Reid Spencer61c83e02006-08-18 08:43:06 +00002379 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002380 }
Chris Lattner91ef4602004-03-31 03:49:47 +00002381 | FALSETOK {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00002382 $$ = ValID::create(ConstantInt::getFalse());
Reid Spencer61c83e02006-08-18 08:43:06 +00002383 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002384 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00002385 | NULL_TOK {
2386 $$ = ValID::createNull();
Reid Spencer61c83e02006-08-18 08:43:06 +00002387 CHECK_FOR_ERROR
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002388 }
Chris Lattner16710e92004-10-16 18:17:13 +00002389 | UNDEF {
2390 $$ = ValID::createUndef();
Reid Spencer61c83e02006-08-18 08:43:06 +00002391 CHECK_FOR_ERROR
Chris Lattner16710e92004-10-16 18:17:13 +00002392 }
Chris Lattnerf1f03df2005-12-21 17:53:02 +00002393 | ZEROINITIALIZER { // A vector zero constant.
2394 $$ = ValID::createZeroInit();
Reid Spencer61c83e02006-08-18 08:43:06 +00002395 CHECK_FOR_ERROR
Chris Lattnerf1f03df2005-12-21 17:53:02 +00002396 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00002397 | '<' ConstVector '>' { // Nonempty unsized packed vector
Reid Spencer08de34b2006-12-03 05:45:44 +00002398 const Type *ETy = (*$2)[0]->getType();
Brian Gaeke715c90b2004-08-20 06:00:58 +00002399 int NumElements = $2->size();
2400
Reid Spencer9d6565a2007-02-15 02:26:10 +00002401 VectorType* pt = VectorType::get(ETy, NumElements);
Brian Gaeke715c90b2004-08-20 06:00:58 +00002402 PATypeHolder* PTy = new PATypeHolder(
Reid Spencer08de34b2006-12-03 05:45:44 +00002403 HandleUpRefs(
Reid Spencer9d6565a2007-02-15 02:26:10 +00002404 VectorType::get(
Reid Spencer08de34b2006-12-03 05:45:44 +00002405 ETy,
2406 NumElements)
2407 )
2408 );
Brian Gaeke715c90b2004-08-20 06:00:58 +00002409
2410 // Verify all elements are correct type!
2411 for (unsigned i = 0; i < $2->size(); i++) {
Reid Spencer08de34b2006-12-03 05:45:44 +00002412 if (ETy != (*$2)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00002413 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Brian Gaeke715c90b2004-08-20 06:00:58 +00002414 ETy->getDescription() +"' as required!\nIt is of type '" +
Reid Spencer08de34b2006-12-03 05:45:44 +00002415 (*$2)[i]->getType()->getDescription() + "'.");
Brian Gaeke715c90b2004-08-20 06:00:58 +00002416 }
2417
Reid Spencer9d6565a2007-02-15 02:26:10 +00002418 $$ = ValID::create(ConstantVector::get(pt, *$2));
Brian Gaeke715c90b2004-08-20 06:00:58 +00002419 delete PTy; delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002420 CHECK_FOR_ERROR
Brian Gaeke715c90b2004-08-20 06:00:58 +00002421 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00002422 | ConstExpr {
Reid Spencer08de34b2006-12-03 05:45:44 +00002423 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002424 CHECK_FOR_ERROR
Chris Lattneraa2c8532006-01-25 22:26:43 +00002425 }
2426 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
2427 char *End = UnEscapeLexed($3, true);
2428 std::string AsmStr = std::string($3, End);
2429 End = UnEscapeLexed($5, true);
2430 std::string Constraints = std::string($5, End);
2431 $$ = ValID::createInlineAsm(AsmStr, Constraints, $2);
2432 free($3);
2433 free($5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002434 CHECK_FOR_ERROR
Chris Lattnerd78700d2002-08-16 21:14:40 +00002435 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00002436
Chris Lattner2079fde2001-10-13 06:41:08 +00002437// SymbolicValueRef - Reference to one of two ways of symbolically refering to
2438// another value.
2439//
Reid Spencerb2d17862007-01-26 08:04:51 +00002440SymbolicValueRef : LOCALVAL_ID { // Is it an integer reference...?
2441 $$ = ValID::createLocalID($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002442 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002443 }
Reid Spencerb2d17862007-01-26 08:04:51 +00002444 | GLOBALVAL_ID {
2445 $$ = ValID::createGlobalID($1);
2446 CHECK_FOR_ERROR
2447 }
2448 | LocalName { // Is it a named reference...?
2449 $$ = ValID::createLocalName($1);
2450 CHECK_FOR_ERROR
2451 }
2452 | GlobalName { // Is it a named reference...?
2453 $$ = ValID::createGlobalName($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002454 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002455 };
Chris Lattner2079fde2001-10-13 06:41:08 +00002456
2457// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00002458ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00002459
Chris Lattner00950542001-06-06 20:29:01 +00002460
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002461// ResolvedVal - a <type> <value> pair. This is used only in cases where the
2462// type immediately preceeds the value reference, and allows complex constant
2463// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00002464ResolvedVal : Types ValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00002465 if (!UpRefs.empty())
2466 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2467 $$ = getVal(*$1, $2);
2468 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002469 CHECK_FOR_ERROR
Reid Spencere1553cc2006-12-31 05:40:12 +00002470 }
2471 ;
Chris Lattner8b81bf52001-07-25 22:47:46 +00002472
Chris Lattner00950542001-06-06 20:29:01 +00002473BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner8ab406d2004-07-13 07:59:27 +00002474 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002475 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002476 }
Chris Lattner7e708292002-06-25 16:13:24 +00002477 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
Chris Lattner8ab406d2004-07-13 07:59:27 +00002478 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002479 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002480 };
Chris Lattner00950542001-06-06 20:29:01 +00002481
2482
2483// Basic blocks are terminated by branching instructions:
2484// br, br/cc, switch, ret
2485//
Reid Spencerb2d17862007-01-26 08:04:51 +00002486BasicBlock : InstructionList OptLocalAssign BBTerminatorInst {
Chris Lattner8ab406d2004-07-13 07:59:27 +00002487 setValueName($3, $2);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002488 CHECK_FOR_ERROR
Chris Lattner2079fde2001-10-13 06:41:08 +00002489 InsertValue($3);
Chris Lattner2079fde2001-10-13 06:41:08 +00002490 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00002491 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002492 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002493 };
Chris Lattner00950542001-06-06 20:29:01 +00002494
2495InstructionList : InstructionList Inst {
Reid Spencer3da59db2006-11-27 01:05:10 +00002496 if (CastInst *CI1 = dyn_cast<CastInst>($2))
2497 if (CastInst *CI2 = dyn_cast<CastInst>(CI1->getOperand(0)))
2498 if (CI2->getParent() == 0)
2499 $1->getInstList().push_back(CI2);
Chris Lattner00950542001-06-06 20:29:01 +00002500 $1->getInstList().push_back($2);
2501 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002502 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002503 }
Reid Spencer186a43f2007-03-19 18:39:36 +00002504 | /* empty */ { // Empty space between instruction lists
2505 $$ = defineBBVal(ValID::createLocalID(CurFun.NextValNum));
Reid Spencer61c83e02006-08-18 08:43:06 +00002506 CHECK_FOR_ERROR
Chris Lattner22ae2322004-07-13 08:39:15 +00002507 }
Reid Spencer186a43f2007-03-19 18:39:36 +00002508 | LABELSTR { // Labelled (named) basic block
2509 $$ = defineBBVal(ValID::createLocalName($1));
Reid Spencer61c83e02006-08-18 08:43:06 +00002510 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002511 };
Chris Lattner00950542001-06-06 20:29:01 +00002512
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002513BBTerminatorInst : RET ResolvedVal { // Return with a result...
Reid Spencer08de34b2006-12-03 05:45:44 +00002514 $$ = new ReturnInst($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00002515 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002516 }
Reid Spencer186a43f2007-03-19 18:39:36 +00002517 | RET VOID { // Return with no result...
Chris Lattner00950542001-06-06 20:29:01 +00002518 $$ = new ReturnInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002519 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002520 }
Reid Spencer186a43f2007-03-19 18:39:36 +00002521 | BR LABEL ValueRef { // Unconditional Branch...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002522 BasicBlock* tmpBB = getBBVal($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002523 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002524 $$ = new BranchInst(tmpBB);
Reid Spencer186a43f2007-03-19 18:39:36 +00002525 } // Conditional Branch...
Reid Spencer8088e9d2007-01-13 05:00:20 +00002526 | BR INTTYPE ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
2527 assert(cast<IntegerType>($2)->getBitWidth() == 1 && "Not Bool?");
Reid Spencer5b7e7532006-09-28 19:28:24 +00002528 BasicBlock* tmpBBA = getBBVal($6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002529 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002530 BasicBlock* tmpBBB = getBBVal($9);
2531 CHECK_FOR_ERROR
Reid Spencer4fe16d62007-01-11 18:21:29 +00002532 Value* tmpVal = getVal(Type::Int1Ty, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002533 CHECK_FOR_ERROR
2534 $$ = new BranchInst(tmpBBA, tmpBBB, tmpVal);
Chris Lattner00950542001-06-06 20:29:01 +00002535 }
2536 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencer08de34b2006-12-03 05:45:44 +00002537 Value* tmpVal = getVal($2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002538 CHECK_FOR_ERROR
2539 BasicBlock* tmpBB = getBBVal($6);
2540 CHECK_FOR_ERROR
2541 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, $8->size());
Chris Lattner00950542001-06-06 20:29:01 +00002542 $$ = S;
2543
Chris Lattner9232b992003-04-22 18:42:41 +00002544 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
Chris Lattner46748042002-04-09 19:41:42 +00002545 E = $8->end();
Chris Lattner69331f52005-02-24 05:25:17 +00002546 for (; I != E; ++I) {
2547 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
2548 S->addCase(CI, I->second);
2549 else
Reid Spencerf4fa5902007-02-05 10:16:10 +00002550 GEN_ERROR("Switch case is constant, but not a simple integer");
Chris Lattner69331f52005-02-24 05:25:17 +00002551 }
Chris Lattnerf57a43d2004-04-17 23:49:15 +00002552 delete $8;
Reid Spencer61c83e02006-08-18 08:43:06 +00002553 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002554 }
Chris Lattner196850c2003-05-15 21:30:00 +00002555 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencer08de34b2006-12-03 05:45:44 +00002556 Value* tmpVal = getVal($2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002557 CHECK_FOR_ERROR
2558 BasicBlock* tmpBB = getBBVal($6);
2559 CHECK_FOR_ERROR
2560 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, 0);
Chris Lattner196850c2003-05-15 21:30:00 +00002561 $$ = S;
Reid Spencer61c83e02006-08-18 08:43:06 +00002562 CHECK_FOR_ERROR
Chris Lattner196850c2003-05-15 21:30:00 +00002563 }
Reid Spencer2c261782007-01-05 17:06:19 +00002564 | INVOKE OptCallingConv ResultTypes ValueRef '(' ValueRefList ')' OptFuncAttrs
Chris Lattnera8e8f162005-05-06 20:27:19 +00002565 TO LABEL ValueRef UNWIND LABEL ValueRef {
Chris Lattner2079fde2001-10-13 06:41:08 +00002566
Reid Spencere1553cc2006-12-31 05:40:12 +00002567 // Handle the short syntax
2568 const PointerType *PFTy = 0;
2569 const FunctionType *Ty = 0;
Reid Spencer2c261782007-01-05 17:06:19 +00002570 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002571 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00002572 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00002573 std::vector<const Type*> ParamTypes;
Reid Spencer863dd882007-04-28 16:06:50 +00002574 ParamAttrsVector Attrs;
2575 if ($8 != ParamAttr::None) {
2576 ParamAttrsWithIndex PAWI; PAWI.index = 0; PAWI.attrs = 8;
2577 Attrs.push_back(PAWI);
2578 }
Reid Spencer5694b6e2007-04-09 06:17:21 +00002579 ValueRefList::iterator I = $6->begin(), E = $6->end();
Reid Spencer863dd882007-04-28 16:06:50 +00002580 unsigned index = 1;
2581 for (; I != E; ++I, ++index) {
Reid Spencere1553cc2006-12-31 05:40:12 +00002582 const Type *Ty = I->Val->getType();
2583 if (Ty == Type::VoidTy)
2584 GEN_ERROR("Short call syntax cannot be used with varargs");
2585 ParamTypes.push_back(Ty);
Reid Spencer863dd882007-04-28 16:06:50 +00002586 if (I->Attrs != ParamAttr::None) {
2587 ParamAttrsWithIndex PAWI; PAWI.index = index; PAWI.attrs = I->Attrs;
2588 Attrs.push_back(PAWI);
2589 }
Chris Lattner2079fde2001-10-13 06:41:08 +00002590 }
Reid Spencer863dd882007-04-28 16:06:50 +00002591
2592 ParamAttrsList *PAL = 0;
2593 if (!Attrs.empty())
2594 PAL = ParamAttrsList::get(Attrs);
2595 Ty = FunctionType::get($3->get(), ParamTypes, false, PAL);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002596 PFTy = PointerType::get(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00002597 }
Chris Lattner2079fde2001-10-13 06:41:08 +00002598
Reid Spencer2c9df212007-03-20 01:13:00 +00002599 delete $3;
2600
Chris Lattnera8e8f162005-05-06 20:27:19 +00002601 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002602 CHECK_FOR_ERROR
Reid Spencer2c261782007-01-05 17:06:19 +00002603 BasicBlock *Normal = getBBVal($11);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002604 CHECK_FOR_ERROR
Reid Spencer2c261782007-01-05 17:06:19 +00002605 BasicBlock *Except = getBBVal($14);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002606 CHECK_FOR_ERROR
Chris Lattner2079fde2001-10-13 06:41:08 +00002607
Reid Spencere1553cc2006-12-31 05:40:12 +00002608 // Check the arguments
2609 ValueList Args;
2610 if ($6->empty()) { // Has no arguments?
2611 // Make sure no arguments is a good thing!
2612 if (Ty->getNumParams() != 0)
2613 GEN_ERROR("No arguments passed to a function that "
Reid Spencerf4fa5902007-02-05 10:16:10 +00002614 "expects arguments");
Chris Lattner2079fde2001-10-13 06:41:08 +00002615 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00002616 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00002617 // correctly!
Chris Lattnerd5d89962004-02-09 04:14:01 +00002618 FunctionType::param_iterator I = Ty->param_begin();
2619 FunctionType::param_iterator E = Ty->param_end();
Reid Spencere1553cc2006-12-31 05:40:12 +00002620 ValueRefList::iterator ArgI = $6->begin(), ArgE = $6->end();
Reid Spencer863dd882007-04-28 16:06:50 +00002621
Reid Spencere1553cc2006-12-31 05:40:12 +00002622 for (; ArgI != ArgE && I != E; ++ArgI, ++I) {
2623 if (ArgI->Val->getType() != *I)
2624 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00002625 (*I)->getDescription() + "'");
Reid Spencere1553cc2006-12-31 05:40:12 +00002626 Args.push_back(ArgI->Val);
2627 }
Reid Spencer08de34b2006-12-03 05:45:44 +00002628
Reid Spencere1553cc2006-12-31 05:40:12 +00002629 if (Ty->isVarArg()) {
2630 if (I == E)
2631 for (; ArgI != ArgE; ++ArgI)
2632 Args.push_back(ArgI->Val); // push the remaining varargs
2633 } else if (I != E || ArgI != ArgE)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002634 GEN_ERROR("Invalid number of parameters detected");
Chris Lattner2079fde2001-10-13 06:41:08 +00002635 }
Reid Spencere1553cc2006-12-31 05:40:12 +00002636
2637 // Create the InvokeInst
Chris Lattner9d2fda62007-02-13 05:53:56 +00002638 InvokeInst *II = new InvokeInst(V, Normal, Except, &Args[0], Args.size());
Reid Spencere1553cc2006-12-31 05:40:12 +00002639 II->setCallingConv($2);
2640 $$ = II;
Chris Lattnera8e8f162005-05-06 20:27:19 +00002641 delete $6;
Reid Spencer61c83e02006-08-18 08:43:06 +00002642 CHECK_FOR_ERROR
Chris Lattner36143fc2003-09-08 18:54:55 +00002643 }
2644 | UNWIND {
2645 $$ = new UnwindInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002646 CHECK_FOR_ERROR
Chris Lattner16710e92004-10-16 18:17:13 +00002647 }
2648 | UNREACHABLE {
2649 $$ = new UnreachableInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002650 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002651 };
Chris Lattner2079fde2001-10-13 06:41:08 +00002652
2653
Chris Lattner00950542001-06-06 20:29:01 +00002654
2655JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
2656 $$ = $1;
Reid Spencer186a43f2007-03-19 18:39:36 +00002657 Constant *V = cast<Constant>(getExistingVal($2, $3));
Reid Spencer5b7e7532006-09-28 19:28:24 +00002658 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002659 if (V == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002660 GEN_ERROR("May only switch on a constant pool value");
Chris Lattner00950542001-06-06 20:29:01 +00002661
Reid Spencer5b7e7532006-09-28 19:28:24 +00002662 BasicBlock* tmpBB = getBBVal($6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002663 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002664 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner00950542001-06-06 20:29:01 +00002665 }
2666 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner9232b992003-04-22 18:42:41 +00002667 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Reid Spencer186a43f2007-03-19 18:39:36 +00002668 Constant *V = cast<Constant>(getExistingVal($1, $2));
Reid Spencer5b7e7532006-09-28 19:28:24 +00002669 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002670
2671 if (V == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002672 GEN_ERROR("May only switch on a constant pool value");
Chris Lattner00950542001-06-06 20:29:01 +00002673
Reid Spencer5b7e7532006-09-28 19:28:24 +00002674 BasicBlock* tmpBB = getBBVal($5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002675 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002676 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner51727be2002-06-04 21:58:56 +00002677 };
Chris Lattner00950542001-06-06 20:29:01 +00002678
Reid Spencerb2d17862007-01-26 08:04:51 +00002679Inst : OptLocalAssign InstVal {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002680 // Is this definition named?? if so, assign the name...
2681 setValueName($2, $1);
2682 CHECK_FOR_ERROR
2683 InsertValue($2);
2684 $$ = $2;
2685 CHECK_FOR_ERROR
2686 };
2687
Chris Lattner00950542001-06-06 20:29:01 +00002688
Chris Lattnerc24d2082001-06-11 15:04:20 +00002689PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Reid Spencere1553cc2006-12-31 05:40:12 +00002690 if (!UpRefs.empty())
2691 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Chris Lattner9232b992003-04-22 18:42:41 +00002692 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Reid Spencer08de34b2006-12-03 05:45:44 +00002693 Value* tmpVal = getVal(*$1, $3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002694 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002695 BasicBlock* tmpBB = getBBVal($5);
2696 CHECK_FOR_ERROR
2697 $$->push_back(std::make_pair(tmpVal, tmpBB));
Reid Spencer08de34b2006-12-03 05:45:44 +00002698 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00002699 }
2700 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
2701 $$ = $1;
Reid Spencer5b7e7532006-09-28 19:28:24 +00002702 Value* tmpVal = getVal($1->front().first->getType(), $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00002703 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002704 BasicBlock* tmpBB = getBBVal($6);
2705 CHECK_FOR_ERROR
2706 $1->push_back(std::make_pair(tmpVal, tmpBB));
Chris Lattner51727be2002-06-04 21:58:56 +00002707 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00002708
2709
Reid Spencere1553cc2006-12-31 05:40:12 +00002710ValueRefList : Types ValueRef OptParamAttrs {
2711 if (!UpRefs.empty())
2712 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2713 // Used for call and invoke instructions
2714 $$ = new ValueRefList();
2715 ValueRefListEntry E; E.Attrs = $3; E.Val = getVal($1->get(), $2);
2716 $$->push_back(E);
Reid Spencer2c9df212007-03-20 01:13:00 +00002717 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00002718 }
Reid Spencere1553cc2006-12-31 05:40:12 +00002719 | ValueRefList ',' Types ValueRef OptParamAttrs {
2720 if (!UpRefs.empty())
2721 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattner00950542001-06-06 20:29:01 +00002722 $$ = $1;
Reid Spencere1553cc2006-12-31 05:40:12 +00002723 ValueRefListEntry E; E.Attrs = $5; E.Val = getVal($3->get(), $4);
2724 $$->push_back(E);
Reid Spencer2c9df212007-03-20 01:13:00 +00002725 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002726 CHECK_FOR_ERROR
Reid Spencere1553cc2006-12-31 05:40:12 +00002727 }
2728 | /*empty*/ { $$ = new ValueRefList(); };
Chris Lattner00950542001-06-06 20:29:01 +00002729
Reid Spencere1553cc2006-12-31 05:40:12 +00002730IndexList // Used for gep instructions and constant expressions
Reid Spencere03969f2006-12-31 21:46:36 +00002731 : /*empty*/ { $$ = new std::vector<Value*>(); }
Reid Spencere1553cc2006-12-31 05:40:12 +00002732 | IndexList ',' ResolvedVal {
2733 $$ = $1;
2734 $$->push_back($3);
2735 CHECK_FOR_ERROR
2736 }
Reid Spencer71305d72006-12-31 21:25:25 +00002737 ;
Chris Lattner00950542001-06-06 20:29:01 +00002738
Chris Lattnerddb6db42005-05-06 05:51:46 +00002739OptTailCall : TAIL CALL {
2740 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002741 CHECK_FOR_ERROR
Chris Lattnerddb6db42005-05-06 05:51:46 +00002742 }
2743 | CALL {
2744 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002745 CHECK_FOR_ERROR
Chris Lattnerddb6db42005-05-06 05:51:46 +00002746 };
2747
Chris Lattner4a6482b2002-09-10 19:57:26 +00002748InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00002749 if (!UpRefs.empty())
2750 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Chris Lattner42a75512007-01-15 02:27:26 +00002751 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint() &&
Reid Spencer9d6565a2007-02-15 02:26:10 +00002752 !isa<VectorType>((*$2).get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002753 GEN_ERROR(
Reid Spencerf4fa5902007-02-05 10:16:10 +00002754 "Arithmetic operator requires integer, FP, or packed operands");
Reid Spencer9d6565a2007-02-15 02:26:10 +00002755 if (isa<VectorType>((*$2).get()) &&
Reid Spencer08de34b2006-12-03 05:45:44 +00002756 ($1 == Instruction::URem ||
2757 $1 == Instruction::SRem ||
2758 $1 == Instruction::FRem))
Reid Spencerac9dcb92007-02-15 03:39:18 +00002759 GEN_ERROR("Remainder not supported on vector types");
Reid Spencer08de34b2006-12-03 05:45:44 +00002760 Value* val1 = getVal(*$2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002761 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00002762 Value* val2 = getVal(*$2, $5);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002763 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00002764 $$ = BinaryOperator::create($1, val1, val2);
Chris Lattner4a6482b2002-09-10 19:57:26 +00002765 if ($$ == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002766 GEN_ERROR("binary operator returned null");
Reid Spencer08de34b2006-12-03 05:45:44 +00002767 delete $2;
Chris Lattner4a6482b2002-09-10 19:57:26 +00002768 }
2769 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00002770 if (!UpRefs.empty())
2771 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Chris Lattner42a75512007-01-15 02:27:26 +00002772 if (!(*$2)->isInteger()) {
Reid Spencer9d6565a2007-02-15 02:26:10 +00002773 if (Instruction::isShift($1) || !isa<VectorType>($2->get()) ||
2774 !cast<VectorType>($2->get())->getElementType()->isInteger())
Reid Spencerf4fa5902007-02-05 10:16:10 +00002775 GEN_ERROR("Logical operator requires integral operands");
Chris Lattner0a017832005-12-21 18:31:29 +00002776 }
Reid Spencer08de34b2006-12-03 05:45:44 +00002777 Value* tmpVal1 = getVal(*$2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002778 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00002779 Value* tmpVal2 = getVal(*$2, $5);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002780 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00002781 $$ = BinaryOperator::create($1, tmpVal1, tmpVal2);
Chris Lattner4a6482b2002-09-10 19:57:26 +00002782 if ($$ == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002783 GEN_ERROR("binary operator returned null");
Reid Spencer08de34b2006-12-03 05:45:44 +00002784 delete $2;
Chris Lattner4a6482b2002-09-10 19:57:26 +00002785 }
Reid Spencer08de34b2006-12-03 05:45:44 +00002786 | ICMP IPredicates Types ValueRef ',' ValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00002787 if (!UpRefs.empty())
2788 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencer9d6565a2007-02-15 02:26:10 +00002789 if (isa<VectorType>((*$3).get()))
Reid Spencerac9dcb92007-02-15 03:39:18 +00002790 GEN_ERROR("Vector types not supported by icmp instruction");
Reid Spencer08de34b2006-12-03 05:45:44 +00002791 Value* tmpVal1 = getVal(*$3, $4);
2792 CHECK_FOR_ERROR
2793 Value* tmpVal2 = getVal(*$3, $6);
2794 CHECK_FOR_ERROR
2795 $$ = CmpInst::create($1, $2, tmpVal1, tmpVal2);
2796 if ($$ == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002797 GEN_ERROR("icmp operator returned null");
Reid Spencer2c9df212007-03-20 01:13:00 +00002798 delete $3;
Reid Spencer08de34b2006-12-03 05:45:44 +00002799 }
2800 | FCMP FPredicates Types ValueRef ',' ValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00002801 if (!UpRefs.empty())
2802 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencer9d6565a2007-02-15 02:26:10 +00002803 if (isa<VectorType>((*$3).get()))
Reid Spencerac9dcb92007-02-15 03:39:18 +00002804 GEN_ERROR("Vector types not supported by fcmp instruction");
Reid Spencer08de34b2006-12-03 05:45:44 +00002805 Value* tmpVal1 = getVal(*$3, $4);
2806 CHECK_FOR_ERROR
2807 Value* tmpVal2 = getVal(*$3, $6);
2808 CHECK_FOR_ERROR
2809 $$ = CmpInst::create($1, $2, tmpVal1, tmpVal2);
2810 if ($$ == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002811 GEN_ERROR("fcmp operator returned null");
Reid Spencer2c9df212007-03-20 01:13:00 +00002812 delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00002813 }
Reid Spencer3da59db2006-11-27 01:05:10 +00002814 | CastOps ResolvedVal TO Types {
Reid Spencere1553cc2006-12-31 05:40:12 +00002815 if (!UpRefs.empty())
2816 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00002817 Value* Val = $2;
Reid Spencer93947c32007-01-17 02:47:33 +00002818 const Type* DestTy = $4->get();
2819 if (!CastInst::castIsValid($1, Val, DestTy))
2820 GEN_ERROR("invalid cast opcode for cast from '" +
2821 Val->getType()->getDescription() + "' to '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00002822 DestTy->getDescription() + "'");
Reid Spencer93947c32007-01-17 02:47:33 +00002823 $$ = CastInst::create($1, Val, DestTy);
Reid Spencer08de34b2006-12-03 05:45:44 +00002824 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00002825 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00002826 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer4fe16d62007-01-11 18:21:29 +00002827 if ($2->getType() != Type::Int1Ty)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002828 GEN_ERROR("select condition must be boolean");
Reid Spencer08de34b2006-12-03 05:45:44 +00002829 if ($4->getType() != $6->getType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00002830 GEN_ERROR("select value types should match");
Reid Spencer08de34b2006-12-03 05:45:44 +00002831 $$ = new SelectInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002832 CHECK_FOR_ERROR
Chris Lattner76f0ff32004-03-12 05:51:36 +00002833 }
Chris Lattner99e7ab72003-10-18 05:53:13 +00002834 | VAARG ResolvedVal ',' Types {
Reid Spencere1553cc2006-12-31 05:40:12 +00002835 if (!UpRefs.empty())
2836 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00002837 $$ = new VAArgInst($2, *$4);
2838 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00002839 CHECK_FOR_ERROR
Chris Lattner99e7ab72003-10-18 05:53:13 +00002840 }
Robert Bocchino9c62b562006-01-10 19:04:32 +00002841 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencer08de34b2006-12-03 05:45:44 +00002842 if (!ExtractElementInst::isValidOperands($2, $4))
Reid Spencerf4fa5902007-02-05 10:16:10 +00002843 GEN_ERROR("Invalid extractelement operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00002844 $$ = new ExtractElementInst($2, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00002845 CHECK_FOR_ERROR
Robert Bocchino9c62b562006-01-10 19:04:32 +00002846 }
Robert Bocchino2def1b32006-01-17 20:06:25 +00002847 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer08de34b2006-12-03 05:45:44 +00002848 if (!InsertElementInst::isValidOperands($2, $4, $6))
Reid Spencerf4fa5902007-02-05 10:16:10 +00002849 GEN_ERROR("Invalid insertelement operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00002850 $$ = new InsertElementInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002851 CHECK_FOR_ERROR
Robert Bocchino2def1b32006-01-17 20:06:25 +00002852 }
Chris Lattner4c949082006-04-08 01:18:35 +00002853 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer08de34b2006-12-03 05:45:44 +00002854 if (!ShuffleVectorInst::isValidOperands($2, $4, $6))
Reid Spencerf4fa5902007-02-05 10:16:10 +00002855 GEN_ERROR("Invalid shufflevector operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00002856 $$ = new ShuffleVectorInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002857 CHECK_FOR_ERROR
Chris Lattner4c949082006-04-08 01:18:35 +00002858 }
Chris Lattner3b237fc2003-10-19 21:34:28 +00002859 | PHI_TOK PHIList {
Chris Lattnerc24d2082001-06-11 15:04:20 +00002860 const Type *Ty = $2->front().first->getType();
Chris Lattner8ea8f362003-10-30 01:38:18 +00002861 if (!Ty->isFirstClassType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00002862 GEN_ERROR("PHI node operands must be of first class type");
Chris Lattnerc24d2082001-06-11 15:04:20 +00002863 $$ = new PHINode(Ty);
Chris Lattnerce1df9e2005-01-29 00:35:55 +00002864 ((PHINode*)$$)->reserveOperandSpace($2->size());
Chris Lattner00950542001-06-06 20:29:01 +00002865 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00002866 if ($2->front().first->getType() != Ty)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002867 GEN_ERROR("All elements of a PHI node must be of the same type");
Chris Lattnerb00c5822001-10-02 03:41:24 +00002868 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00002869 $2->pop_front();
2870 }
2871 delete $2; // Free the list...
Reid Spencer61c83e02006-08-18 08:43:06 +00002872 CHECK_FOR_ERROR
Chris Lattnerddb6db42005-05-06 05:51:46 +00002873 }
Reid Spencer2c261782007-01-05 17:06:19 +00002874 | OptTailCall OptCallingConv ResultTypes ValueRef '(' ValueRefList ')'
2875 OptFuncAttrs {
Reid Spencere1553cc2006-12-31 05:40:12 +00002876
2877 // Handle the short syntax
Bill Wendlingb39a55a2006-11-12 11:10:39 +00002878 const PointerType *PFTy = 0;
2879 const FunctionType *Ty = 0;
Reid Spencer2c261782007-01-05 17:06:19 +00002880 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002881 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00002882 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00002883 std::vector<const Type*> ParamTypes;
Reid Spencer863dd882007-04-28 16:06:50 +00002884 ParamAttrsVector Attrs;
2885 if ($8 != ParamAttr::None) {
2886 ParamAttrsWithIndex PAWI; PAWI.index = 0; PAWI.attrs = $8;
2887 Attrs.push_back(PAWI);
2888 }
2889 unsigned index = 1;
Reid Spencer5694b6e2007-04-09 06:17:21 +00002890 ValueRefList::iterator I = $6->begin(), E = $6->end();
Reid Spencer863dd882007-04-28 16:06:50 +00002891 for (; I != E; ++I, ++index) {
Reid Spencere1553cc2006-12-31 05:40:12 +00002892 const Type *Ty = I->Val->getType();
2893 if (Ty == Type::VoidTy)
2894 GEN_ERROR("Short call syntax cannot be used with varargs");
2895 ParamTypes.push_back(Ty);
Reid Spencer863dd882007-04-28 16:06:50 +00002896 if (I->Attrs != ParamAttr::None) {
2897 ParamAttrsWithIndex PAWI; PAWI.index = index; PAWI.attrs = I->Attrs;
2898 Attrs.push_back(PAWI);
2899 }
Chris Lattneref9c23f2001-10-03 14:53:21 +00002900 }
Reid Spencer863dd882007-04-28 16:06:50 +00002901
2902 ParamAttrsList *PAL = 0;
2903 if (!Attrs.empty())
2904 PAL = ParamAttrsList::get(Attrs);
2905
2906 Ty = FunctionType::get($3->get(), ParamTypes, false, PAL);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002907 PFTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00002908 }
Chris Lattner00950542001-06-06 20:29:01 +00002909
Chris Lattnera8e8f162005-05-06 20:27:19 +00002910 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002911 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002912
Reid Spencerebff55c2007-04-16 06:55:42 +00002913 // Check for call to invalid intrinsic to avoid crashing later.
2914 if (Function *theF = dyn_cast<Function>(V)) {
Reid Spencerce1e8ef2007-04-16 22:01:57 +00002915 if (theF->hasName() && (theF->getValueName()->getKeyLength() >= 5) &&
Reid Spencer43e60732007-04-16 20:31:06 +00002916 (0 == strncmp(theF->getValueName()->getKeyData(), "llvm.", 5)) &&
2917 !theF->getIntrinsicID(true))
Reid Spencerebff55c2007-04-16 06:55:42 +00002918 GEN_ERROR("Call to invalid LLVM intrinsic function '" +
2919 theF->getName() + "'");
2920 }
2921
Reid Spencere1553cc2006-12-31 05:40:12 +00002922 // Check the arguments
2923 ValueList Args;
2924 if ($6->empty()) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00002925 // Make sure no arguments is a good thing!
2926 if (Ty->getNumParams() != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002927 GEN_ERROR("No arguments passed to a function that "
Reid Spencerf4fa5902007-02-05 10:16:10 +00002928 "expects arguments");
Chris Lattner8b81bf52001-07-25 22:47:46 +00002929 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00002930 // Loop through FunctionType's arguments and ensure they are specified
Reid Spencer863dd882007-04-28 16:06:50 +00002931 // correctly!
2932 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00002933 FunctionType::param_iterator I = Ty->param_begin();
2934 FunctionType::param_iterator E = Ty->param_end();
Reid Spencere1553cc2006-12-31 05:40:12 +00002935 ValueRefList::iterator ArgI = $6->begin(), ArgE = $6->end();
Reid Spencer863dd882007-04-28 16:06:50 +00002936
Reid Spencere1553cc2006-12-31 05:40:12 +00002937 for (; ArgI != ArgE && I != E; ++ArgI, ++I) {
2938 if (ArgI->Val->getType() != *I)
2939 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00002940 (*I)->getDescription() + "'");
Reid Spencere1553cc2006-12-31 05:40:12 +00002941 Args.push_back(ArgI->Val);
2942 }
2943 if (Ty->isVarArg()) {
2944 if (I == E)
2945 for (; ArgI != ArgE; ++ArgI)
2946 Args.push_back(ArgI->Val); // push the remaining varargs
2947 } else if (I != E || ArgI != ArgE)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002948 GEN_ERROR("Invalid number of parameters detected");
Chris Lattner8b81bf52001-07-25 22:47:46 +00002949 }
Reid Spencere1553cc2006-12-31 05:40:12 +00002950 // Create the call node
Chris Lattner9d2fda62007-02-13 05:53:56 +00002951 CallInst *CI = new CallInst(V, &Args[0], Args.size());
Reid Spencere1553cc2006-12-31 05:40:12 +00002952 CI->setTailCall($1);
2953 CI->setCallingConv($2);
2954 $$ = CI;
Chris Lattnera8e8f162005-05-06 20:27:19 +00002955 delete $6;
Reid Spencerb2d17862007-01-26 08:04:51 +00002956 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002957 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002958 }
2959 | MemoryInst {
2960 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002961 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002962 };
Chris Lattner00950542001-06-06 20:29:01 +00002963
Chris Lattner15c9c032003-09-08 18:20:29 +00002964OptVolatile : VOLATILE {
2965 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002966 CHECK_FOR_ERROR
Chris Lattner15c9c032003-09-08 18:20:29 +00002967 }
2968 | /* empty */ {
2969 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002970 CHECK_FOR_ERROR
Chris Lattner15c9c032003-09-08 18:20:29 +00002971 };
2972
Chris Lattner027dcc52001-07-08 21:10:27 +00002973
Chris Lattnerddb6db42005-05-06 05:51:46 +00002974
Chris Lattner66db8e42005-11-06 06:34:12 +00002975MemoryInst : MALLOC Types OptCAlign {
Reid Spencere1553cc2006-12-31 05:40:12 +00002976 if (!UpRefs.empty())
2977 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00002978 $$ = new MallocInst(*$2, 0, $3);
2979 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002980 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002981 }
Reid Spencera54b7cb2007-01-12 07:05:14 +00002982 | MALLOC Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencere1553cc2006-12-31 05:40:12 +00002983 if (!UpRefs.empty())
2984 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00002985 Value* tmpVal = getVal($4, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002986 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00002987 $$ = new MallocInst(*$2, tmpVal, $6);
2988 delete $2;
Nate Begeman14b05292005-11-05 09:21:28 +00002989 }
Chris Lattner66db8e42005-11-06 06:34:12 +00002990 | ALLOCA Types OptCAlign {
Reid Spencere1553cc2006-12-31 05:40:12 +00002991 if (!UpRefs.empty())
2992 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00002993 $$ = new AllocaInst(*$2, 0, $3);
2994 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002995 CHECK_FOR_ERROR
Nate Begeman14b05292005-11-05 09:21:28 +00002996 }
Reid Spencera54b7cb2007-01-12 07:05:14 +00002997 | ALLOCA Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencere1553cc2006-12-31 05:40:12 +00002998 if (!UpRefs.empty())
2999 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00003000 Value* tmpVal = getVal($4, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00003001 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00003002 $$ = new AllocaInst(*$2, tmpVal, $6);
3003 delete $2;
Nate Begeman14b05292005-11-05 09:21:28 +00003004 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00003005 | FREE ResolvedVal {
Reid Spencer08de34b2006-12-03 05:45:44 +00003006 if (!isa<PointerType>($2->getType()))
Reid Spencer61c83e02006-08-18 08:43:06 +00003007 GEN_ERROR("Trying to free nonpointer type " +
Reid Spencerf4fa5902007-02-05 10:16:10 +00003008 $2->getType()->getDescription() + "");
Reid Spencer08de34b2006-12-03 05:45:44 +00003009 $$ = new FreeInst($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00003010 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00003011 }
3012
Christopher Lamb43c7f372007-04-22 19:24:39 +00003013 | OptVolatile LOAD Types ValueRef OptCAlign {
Reid Spencere1553cc2006-12-31 05:40:12 +00003014 if (!UpRefs.empty())
3015 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00003016 if (!isa<PointerType>($3->get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00003017 GEN_ERROR("Can't load from nonpointer type: " +
Reid Spencer08de34b2006-12-03 05:45:44 +00003018 (*$3)->getDescription());
3019 if (!cast<PointerType>($3->get())->getElementType()->isFirstClassType())
Reid Spencer61c83e02006-08-18 08:43:06 +00003020 GEN_ERROR("Can't load from pointer of non-first-class type: " +
Reid Spencer08de34b2006-12-03 05:45:44 +00003021 (*$3)->getDescription());
3022 Value* tmpVal = getVal(*$3, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00003023 CHECK_FOR_ERROR
Christopher Lamb43c7f372007-04-22 19:24:39 +00003024 $$ = new LoadInst(tmpVal, "", $1, $5);
Reid Spencer08de34b2006-12-03 05:45:44 +00003025 delete $3;
Chris Lattner027dcc52001-07-08 21:10:27 +00003026 }
Christopher Lamb43c7f372007-04-22 19:24:39 +00003027 | OptVolatile STORE ResolvedVal ',' Types ValueRef OptCAlign {
Reid Spencere1553cc2006-12-31 05:40:12 +00003028 if (!UpRefs.empty())
3029 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00003030 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner8c8418d2003-09-01 16:31:28 +00003031 if (!PT)
Reid Spencer61c83e02006-08-18 08:43:06 +00003032 GEN_ERROR("Can't store to a nonpointer type: " +
Reid Spencer08de34b2006-12-03 05:45:44 +00003033 (*$5)->getDescription());
Chris Lattner8c8418d2003-09-01 16:31:28 +00003034 const Type *ElTy = PT->getElementType();
Reid Spencer08de34b2006-12-03 05:45:44 +00003035 if (ElTy != $3->getType())
3036 GEN_ERROR("Can't store '" + $3->getType()->getDescription() +
Reid Spencerf4fa5902007-02-05 10:16:10 +00003037 "' into space of type '" + ElTy->getDescription() + "'");
Chris Lattner0383cc42002-08-21 23:51:21 +00003038
Reid Spencer08de34b2006-12-03 05:45:44 +00003039 Value* tmpVal = getVal(*$5, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00003040 CHECK_FOR_ERROR
Christopher Lamb43c7f372007-04-22 19:24:39 +00003041 $$ = new StoreInst($3, tmpVal, $1, $7);
Reid Spencer08de34b2006-12-03 05:45:44 +00003042 delete $5;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00003043 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00003044 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencere1553cc2006-12-31 05:40:12 +00003045 if (!UpRefs.empty())
3046 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00003047 if (!isa<PointerType>($2->get()))
Reid Spencerf4fa5902007-02-05 10:16:10 +00003048 GEN_ERROR("getelementptr insn requires pointer operand");
Chris Lattner830b6f92004-04-05 01:30:04 +00003049
Chris Lattnerc856c7a2007-02-13 00:57:40 +00003050 if (!GetElementPtrInst::getIndexedType(*$2, &(*$4)[0], $4->size(), true))
Reid Spencer61c83e02006-08-18 08:43:06 +00003051 GEN_ERROR("Invalid getelementptr indices for type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00003052 (*$2)->getDescription()+ "'");
Reid Spencer08de34b2006-12-03 05:45:44 +00003053 Value* tmpVal = getVal(*$2, $3);
Reid Spencer61c83e02006-08-18 08:43:06 +00003054 CHECK_FOR_ERROR
Chris Lattnerc856c7a2007-02-13 00:57:40 +00003055 $$ = new GetElementPtrInst(tmpVal, &(*$4)[0], $4->size());
Reid Spencer08de34b2006-12-03 05:45:44 +00003056 delete $2;
Reid Spencer5b7e7532006-09-28 19:28:24 +00003057 delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00003058 };
Chris Lattner027dcc52001-07-08 21:10:27 +00003059
Brian Gaeked0fde302003-11-11 22:41:34 +00003060
Chris Lattner00950542001-06-06 20:29:01 +00003061%%
Reid Spencer61c83e02006-08-18 08:43:06 +00003062
Reid Spencere1553cc2006-12-31 05:40:12 +00003063// common code from the two 'RunVMAsmParser' functions
3064static Module* RunParser(Module * M) {
3065
3066 llvmAsmlineno = 1; // Reset the current line number...
3067 CurModule.CurrentModule = M;
3068#if YYDEBUG
3069 yydebug = Debug;
3070#endif
3071
3072 // Check to make sure the parser succeeded
3073 if (yyparse()) {
3074 if (ParserResult)
3075 delete ParserResult;
3076 return 0;
3077 }
3078
Reid Spencercd5bd902007-03-30 01:37:13 +00003079 // Emit an error if there are any unresolved types left.
3080 if (!CurModule.LateResolveTypes.empty()) {
3081 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
3082 if (DID.Type == ValID::LocalName) {
3083 GenerateError("Undefined type remains at eof: '"+DID.getName() + "'");
3084 } else {
3085 GenerateError("Undefined type remains at eof: #" + itostr(DID.Num));
3086 }
3087 if (ParserResult)
3088 delete ParserResult;
3089 return 0;
3090 }
3091
3092 // Emit an error if there are any unresolved values left.
3093 if (!CurModule.LateResolveValues.empty()) {
3094 Value *V = CurModule.LateResolveValues.back();
3095 std::map<Value*, std::pair<ValID, int> >::iterator I =
3096 CurModule.PlaceHolderInfo.find(V);
3097
3098 if (I != CurModule.PlaceHolderInfo.end()) {
3099 ValID &DID = I->second.first;
3100 if (DID.Type == ValID::LocalName) {
3101 GenerateError("Undefined value remains at eof: "+DID.getName() + "'");
3102 } else {
3103 GenerateError("Undefined value remains at eof: #" + itostr(DID.Num));
3104 }
3105 if (ParserResult)
3106 delete ParserResult;
3107 return 0;
3108 }
3109 }
3110
Reid Spencere1553cc2006-12-31 05:40:12 +00003111 // Check to make sure that parsing produced a result
3112 if (!ParserResult)
3113 return 0;
3114
3115 // Reset ParserResult variable while saving its value for the result.
3116 Module *Result = ParserResult;
3117 ParserResult = 0;
3118
3119 return Result;
3120}
3121
Reid Spencer61c83e02006-08-18 08:43:06 +00003122void llvm::GenerateError(const std::string &message, int LineNo) {
3123 if (LineNo == -1) LineNo = llvmAsmlineno;
3124 // TODO: column number in exception
3125 if (TheParseError)
3126 TheParseError->setError(CurFilename, message, LineNo);
3127 TriggerError = 1;
3128}
3129
Chris Lattner09083092001-07-08 04:57:15 +00003130int yyerror(const char *ErrorMsg) {
Chris Lattner9232b992003-04-22 18:42:41 +00003131 std::string where
3132 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00003133 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Reid Spenceref9b9a72007-02-05 20:47:22 +00003134 std::string errMsg = where + "error: " + std::string(ErrorMsg);
3135 if (yychar != YYEMPTY && yychar != 0)
3136 errMsg += " while reading token: '" + std::string(llvmAsmtext, llvmAsmleng)+
3137 "'";
Reid Spencer61c83e02006-08-18 08:43:06 +00003138 GenerateError(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00003139 return 0;
3140}