blob: e6222b6cbff0fd8cd0914478ca8f1e843c5b5229 [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 Spencer3cb4dda2007-04-28 14:13:42 +0000142 GlobalValue *GetForwardRefForGlobal(const PointerType *PTy, ParamAttrsList *PAL,
143 ValID ID) {
Chris Lattner8a327842004-07-14 21:44:00 +0000144 // Check to see if there is a forward reference to this global variable...
145 // if there is, eliminate it and patch the reference to use the new def'n.
146 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(PTy, ID));
147 GlobalValue *Ret = 0;
148 if (I != GlobalRefs.end()) {
149 Ret = I->second;
Reid Spencer3cb4dda2007-04-28 14:13:42 +0000150 if (PAL && isa<Function>(Ret))
151 Ret = cast<Function>(Ret)->getParamAttrs() == PAL ? Ret : 0;
152 if (Ret)
153 GlobalRefs.erase(I);
Chris Lattner8a327842004-07-14 21:44:00 +0000154 }
155 return Ret;
156 }
Reid Spencer98b3c5c2007-01-02 21:53:43 +0000157
158 bool TypeIsUnresolved(PATypeHolder* PATy) {
159 // If it isn't abstract, its resolved
160 const Type* Ty = PATy->get();
161 if (!Ty->isAbstract())
162 return false;
163 // Traverse the type looking for abstract types. If it isn't abstract then
164 // we don't need to traverse that leg of the type.
165 std::vector<const Type*> WorkList, SeenList;
166 WorkList.push_back(Ty);
167 while (!WorkList.empty()) {
168 const Type* Ty = WorkList.back();
169 SeenList.push_back(Ty);
170 WorkList.pop_back();
171 if (const OpaqueType* OpTy = dyn_cast<OpaqueType>(Ty)) {
172 // Check to see if this is an unresolved type
173 std::map<ValID, PATypeHolder>::iterator I = LateResolveTypes.begin();
174 std::map<ValID, PATypeHolder>::iterator E = LateResolveTypes.end();
175 for ( ; I != E; ++I) {
176 if (I->second.get() == OpTy)
177 return true;
178 }
179 } else if (const SequentialType* SeqTy = dyn_cast<SequentialType>(Ty)) {
180 const Type* TheTy = SeqTy->getElementType();
181 if (TheTy->isAbstract() && TheTy != Ty) {
182 std::vector<const Type*>::iterator I = SeenList.begin(),
183 E = SeenList.end();
184 for ( ; I != E; ++I)
185 if (*I == TheTy)
186 break;
187 if (I == E)
188 WorkList.push_back(TheTy);
189 }
190 } else if (const StructType* StrTy = dyn_cast<StructType>(Ty)) {
191 for (unsigned i = 0; i < StrTy->getNumElements(); ++i) {
192 const Type* TheTy = StrTy->getElementType(i);
193 if (TheTy->isAbstract() && TheTy != Ty) {
194 std::vector<const Type*>::iterator I = SeenList.begin(),
195 E = SeenList.end();
196 for ( ; I != E; ++I)
197 if (*I == TheTy)
198 break;
199 if (I == E)
200 WorkList.push_back(TheTy);
201 }
202 }
203 }
204 }
205 return false;
206 }
Chris Lattner00950542001-06-06 20:29:01 +0000207} CurModule;
208
Chris Lattner79df7c02002-03-26 18:01:55 +0000209static struct PerFunctionInfo {
Chris Lattner7e708292002-06-25 16:13:24 +0000210 Function *CurrentFunction; // Pointer to current function being created
Chris Lattner00950542001-06-06 20:29:01 +0000211
Reid Spencer186a43f2007-03-19 18:39:36 +0000212 ValueList Values; // Keep track of #'d definitions
213 unsigned NextValNum;
214 ValueList LateResolveValues;
Reid Spenceref9b9a72007-02-05 20:47:22 +0000215 bool isDeclare; // Is this function a forward declararation?
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000216 GlobalValue::LinkageTypes Linkage; // Linkage for forward declaration.
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000217 GlobalValue::VisibilityTypes Visibility;
Chris Lattner00950542001-06-06 20:29:01 +0000218
Chris Lattner2e2ed292004-07-14 06:28:35 +0000219 /// BBForwardRefs - When we see forward references to basic blocks, keep
220 /// track of them here.
Reid Spencer186a43f2007-03-19 18:39:36 +0000221 std::map<ValID, BasicBlock*> BBForwardRefs;
Chris Lattner2e2ed292004-07-14 06:28:35 +0000222
Chris Lattner79df7c02002-03-26 18:01:55 +0000223 inline PerFunctionInfo() {
224 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000225 isDeclare = false;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000226 Linkage = GlobalValue::ExternalLinkage;
227 Visibility = GlobalValue::DefaultVisibility;
Chris Lattner00950542001-06-06 20:29:01 +0000228 }
229
Chris Lattner79df7c02002-03-26 18:01:55 +0000230 inline void FunctionStart(Function *M) {
231 CurrentFunction = M;
Reid Spencer186a43f2007-03-19 18:39:36 +0000232 NextValNum = 0;
Chris Lattner00950542001-06-06 20:29:01 +0000233 }
234
Chris Lattner79df7c02002-03-26 18:01:55 +0000235 void FunctionDone() {
Chris Lattner2e2ed292004-07-14 06:28:35 +0000236 // Any forward referenced blocks left?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000237 if (!BBForwardRefs.empty()) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000238 GenerateError("Undefined reference to label " +
Reid Spencer186a43f2007-03-19 18:39:36 +0000239 BBForwardRefs.begin()->second->getName());
Reid Spencer5b7e7532006-09-28 19:28:24 +0000240 return;
241 }
Chris Lattner2e2ed292004-07-14 06:28:35 +0000242
243 // Resolve all forward references now.
Chris Lattner386a3b72001-10-16 19:54:17 +0000244 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000245
Chris Lattner7e708292002-06-25 16:13:24 +0000246 Values.clear(); // Clear out function local definitions
Reid Spencer186a43f2007-03-19 18:39:36 +0000247 BBForwardRefs.clear();
Chris Lattner79df7c02002-03-26 18:01:55 +0000248 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000249 isDeclare = false;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000250 Linkage = GlobalValue::ExternalLinkage;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000251 Visibility = GlobalValue::DefaultVisibility;
Chris Lattner00950542001-06-06 20:29:01 +0000252 }
Chris Lattner394f2eb2003-10-16 20:12:13 +0000253} CurFun; // Info for the current function...
Chris Lattner00950542001-06-06 20:29:01 +0000254
Chris Lattner394f2eb2003-10-16 20:12:13 +0000255static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000256
Chris Lattner00950542001-06-06 20:29:01 +0000257
258//===----------------------------------------------------------------------===//
259// Code to handle definitions of all the types
260//===----------------------------------------------------------------------===//
261
Reid Spencer186a43f2007-03-19 18:39:36 +0000262static void InsertValue(Value *V, ValueList &ValueTab = CurFun.Values) {
263 // Things that have names or are void typed don't get slot numbers
264 if (V->hasName() || (V->getType() == Type::VoidTy))
265 return;
Chris Lattner2079fde2001-10-13 06:41:08 +0000266
Reid Spencer186a43f2007-03-19 18:39:36 +0000267 // In the case of function values, we have to allow for the forward reference
268 // of basic blocks, which are included in the numbering. Consequently, we keep
269 // track of the next insertion location with NextValNum. When a BB gets
270 // inserted, it could change the size of the CurFun.Values vector.
271 if (&ValueTab == &CurFun.Values) {
272 if (ValueTab.size() <= CurFun.NextValNum)
273 ValueTab.resize(CurFun.NextValNum+1);
274 ValueTab[CurFun.NextValNum++] = V;
275 return;
276 }
277 // For all other lists, its okay to just tack it on the back of the vector.
278 ValueTab.push_back(V);
Chris Lattner00950542001-06-06 20:29:01 +0000279}
280
Chris Lattner30c89792001-09-07 16:35:17 +0000281static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000282 switch (D.Type) {
Reid Spencerb2d17862007-01-26 08:04:51 +0000283 case ValID::LocalID: // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000284 // Module constants occupy the lowest numbered slots...
Reid Spencerb2d17862007-01-26 08:04:51 +0000285 if (D.Num < CurModule.Types.size())
286 return CurModule.Types[D.Num];
Chris Lattner42c9e772001-10-20 09:32:59 +0000287 break;
Reid Spencerb2d17862007-01-26 08:04:51 +0000288 case ValID::LocalName: // Is it a named definition?
Chris Lattnera09000d2004-07-14 23:03:46 +0000289 if (const Type *N = CurModule.CurrentModule->getTypeByName(D.Name)) {
290 D.destroy(); // Free old strdup'd memory...
291 return N;
Chris Lattner6e6026b2002-11-20 18:36:02 +0000292 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000293 break;
Chris Lattner30c89792001-09-07 16:35:17 +0000294 default:
Reid Spencerf4fa5902007-02-05 10:16:10 +0000295 GenerateError("Internal parser error: Invalid symbol type reference");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000296 return 0;
Chris Lattner30c89792001-09-07 16:35:17 +0000297 }
298
299 // If we reached here, we referenced either a symbol that we don't know about
300 // or an id number that hasn't been read yet. We may be referencing something
301 // forward, so just create an entry to be resolved later and get to it...
302 //
303 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
304
Chris Lattner355ad1f2005-03-21 06:27:42 +0000305
306 if (inFunctionScope()) {
Reid Spencerb2d17862007-01-26 08:04:51 +0000307 if (D.Type == ValID::LocalName) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000308 GenerateError("Reference to an undefined type: '" + D.getName() + "'");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000309 return 0;
310 } else {
Reid Spencerb2d17862007-01-26 08:04:51 +0000311 GenerateError("Reference to an undefined type: #" + utostr(D.Num));
Reid Spencer5b7e7532006-09-28 19:28:24 +0000312 return 0;
313 }
Chris Lattner4a42e902001-10-22 05:56:09 +0000314 }
Chris Lattner30c89792001-09-07 16:35:17 +0000315
Reid Spencerb78b9082006-11-28 07:28:14 +0000316 std::map<ValID, PATypeHolder>::iterator I =CurModule.LateResolveTypes.find(D);
Chris Lattner355ad1f2005-03-21 06:27:42 +0000317 if (I != CurModule.LateResolveTypes.end())
Reid Spencerb78b9082006-11-28 07:28:14 +0000318 return I->second;
Chris Lattner355ad1f2005-03-21 06:27:42 +0000319
Reid Spencerb78b9082006-11-28 07:28:14 +0000320 Type *Typ = OpaqueType::get();
321 CurModule.LateResolveTypes.insert(std::make_pair(D, Typ));
322 return Typ;
Reid Spencer08de34b2006-12-03 05:45:44 +0000323 }
Chris Lattner30c89792001-09-07 16:35:17 +0000324
Reid Spencer186a43f2007-03-19 18:39:36 +0000325// getExistingVal - Look up the value specified by the provided type and
Chris Lattner2079fde2001-10-13 06:41:08 +0000326// the provided ValID. If the value exists and has already been defined, return
327// it. Otherwise return null.
328//
Reid Spencer186a43f2007-03-19 18:39:36 +0000329static Value *getExistingVal(const Type *Ty, const ValID &D) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000330 if (isa<FunctionType>(Ty)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000331 GenerateError("Functions are not values and "
Chris Lattner79df7c02002-03-26 18:01:55 +0000332 "must be referenced as pointers");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000333 return 0;
334 }
Chris Lattner386a3b72001-10-16 19:54:17 +0000335
Chris Lattner30c89792001-09-07 16:35:17 +0000336 switch (D.Type) {
Reid Spencerb2d17862007-01-26 08:04:51 +0000337 case ValID::LocalID: { // Is it a numbered definition?
Reid Spencerb2d17862007-01-26 08:04:51 +0000338 // Check that the number is within bounds.
Reid Spencer186a43f2007-03-19 18:39:36 +0000339 if (D.Num >= CurFun.Values.size())
340 return 0;
341 Value *Result = CurFun.Values[D.Num];
342 if (Ty != Result->getType()) {
343 GenerateError("Numbered value (%" + utostr(D.Num) + ") of type '" +
344 Result->getType()->getDescription() + "' does not match "
345 "expected type, '" + Ty->getDescription() + "'");
346 return 0;
347 }
348 return Result;
Reid Spencerb2d17862007-01-26 08:04:51 +0000349 }
350 case ValID::GlobalID: { // Is it a numbered definition?
Reid Spencer186a43f2007-03-19 18:39:36 +0000351 if (D.Num >= CurModule.Values.size())
Reid Spenceref9b9a72007-02-05 20:47:22 +0000352 return 0;
Reid Spencer186a43f2007-03-19 18:39:36 +0000353 Value *Result = CurModule.Values[D.Num];
354 if (Ty != Result->getType()) {
355 GenerateError("Numbered value (@" + utostr(D.Num) + ") of type '" +
356 Result->getType()->getDescription() + "' does not match "
357 "expected type, '" + Ty->getDescription() + "'");
Reid Spenceref9b9a72007-02-05 20:47:22 +0000358 return 0;
Reid Spencer186a43f2007-03-19 18:39:36 +0000359 }
360 return Result;
Chris Lattner00950542001-06-06 20:29:01 +0000361 }
Reid Spencerb2d17862007-01-26 08:04:51 +0000362
363 case ValID::LocalName: { // Is it a named definition?
Reid Spenceref9b9a72007-02-05 20:47:22 +0000364 if (!inFunctionScope())
365 return 0;
366 ValueSymbolTable &SymTab = CurFun.CurrentFunction->getValueSymbolTable();
367 Value *N = SymTab.lookup(D.Name);
368 if (N == 0)
369 return 0;
370 if (N->getType() != Ty)
371 return 0;
Reid Spencerb2d17862007-01-26 08:04:51 +0000372
373 D.destroy(); // Free old strdup'd memory...
374 return N;
375 }
376 case ValID::GlobalName: { // Is it a named definition?
Reid Spenceref9b9a72007-02-05 20:47:22 +0000377 ValueSymbolTable &SymTab = CurModule.CurrentModule->getValueSymbolTable();
378 Value *N = SymTab.lookup(D.Name);
379 if (N == 0)
380 return 0;
381 if (N->getType() != Ty)
382 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000383
384 D.destroy(); // Free old strdup'd memory...
385 return N;
386 }
387
Misha Brukman5a2a3822005-05-10 22:02:28 +0000388 // Check to make sure that "Ty" is an integral type, and that our
Chris Lattner2079fde2001-10-13 06:41:08 +0000389 // value will fit into the specified type...
390 case ValID::ConstSIntVal: // Is it a constant pool reference??
Reid Spencerb83eb642006-10-20 07:07:24 +0000391 if (!ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000392 GenerateError("Signed integral constant '" +
Misha Brukman5a2a3822005-05-10 22:02:28 +0000393 itostr(D.ConstPool64) + "' is invalid for type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +0000394 Ty->getDescription() + "'");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000395 return 0;
396 }
Reid Spencereac65742007-03-19 20:40:22 +0000397 return ConstantInt::get(Ty, D.ConstPool64, true);
Chris Lattner2079fde2001-10-13 06:41:08 +0000398
399 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Reid Spencerb83eb642006-10-20 07:07:24 +0000400 if (!ConstantInt::isValueValidForType(Ty, D.UConstPool64)) {
401 if (!ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000402 GenerateError("Integral constant '" + utostr(D.UConstPool64) +
Reid Spencerf4fa5902007-02-05 10:16:10 +0000403 "' is invalid or out of range");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000404 return 0;
Chris Lattner2079fde2001-10-13 06:41:08 +0000405 } else { // This is really a signed reference. Transmogrify.
Reid Spencereac65742007-03-19 20:40:22 +0000406 return ConstantInt::get(Ty, D.ConstPool64, true);
Chris Lattner2079fde2001-10-13 06:41:08 +0000407 }
408 } else {
Reid Spencerb83eb642006-10-20 07:07:24 +0000409 return ConstantInt::get(Ty, D.UConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000410 }
411
Chris Lattner2079fde2001-10-13 06:41:08 +0000412 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000413 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP)) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000414 GenerateError("FP constant invalid for type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000415 return 0;
416 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000417 return ConstantFP::get(Ty, D.ConstPoolFP);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000418
Chris Lattner2079fde2001-10-13 06:41:08 +0000419 case ValID::ConstNullVal: // Is it a null value?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000420 if (!isa<PointerType>(Ty)) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000421 GenerateError("Cannot create a a non pointer null");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000422 return 0;
423 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000424 return ConstantPointerNull::get(cast<PointerType>(Ty));
Misha Brukman5a2a3822005-05-10 22:02:28 +0000425
Chris Lattner16710e92004-10-16 18:17:13 +0000426 case ValID::ConstUndefVal: // Is it an undef value?
427 return UndefValue::get(Ty);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000428
Chris Lattnerf1f03df2005-12-21 17:53:02 +0000429 case ValID::ConstZeroVal: // Is it a zero value?
430 return Constant::getNullValue(Ty);
431
Chris Lattnerd78700d2002-08-16 21:14:40 +0000432 case ValID::ConstantVal: // Fully resolved constant?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000433 if (D.ConstantValue->getType() != Ty) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000434 GenerateError("Constant expression type different from required type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000435 return 0;
436 }
Chris Lattnerd78700d2002-08-16 21:14:40 +0000437 return D.ConstantValue;
438
Chris Lattneraa2c8532006-01-25 22:26:43 +0000439 case ValID::InlineAsmVal: { // Inline asm expression
440 const PointerType *PTy = dyn_cast<PointerType>(Ty);
441 const FunctionType *FTy =
442 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
Reid Spencer5b7e7532006-09-28 19:28:24 +0000443 if (!FTy || !InlineAsm::Verify(FTy, D.IAD->Constraints)) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000444 GenerateError("Invalid type for asm constraint string");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000445 return 0;
446 }
Chris Lattneraa2c8532006-01-25 22:26:43 +0000447 InlineAsm *IA = InlineAsm::get(FTy, D.IAD->AsmString, D.IAD->Constraints,
448 D.IAD->HasSideEffects);
449 D.destroy(); // Free InlineAsmDescriptor.
450 return IA;
451 }
Chris Lattner30c89792001-09-07 16:35:17 +0000452 default:
Reid Spencer1755a9a2007-02-05 17:01:20 +0000453 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000454 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000455 } // End of switch
456
Reid Spencer1755a9a2007-02-05 17:01:20 +0000457 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000458 return 0;
459}
460
Reid Spencer186a43f2007-03-19 18:39:36 +0000461// getVal - This function is identical to getExistingVal, except that if a
Chris Lattner2079fde2001-10-13 06:41:08 +0000462// value is not already defined, it "improvises" by creating a placeholder var
463// that looks and acts just like the requested variable. When the value is
464// defined later, all uses of the placeholder variable are replaced with the
465// real thing.
466//
Chris Lattner64116f92004-07-14 01:33:11 +0000467static Value *getVal(const Type *Ty, const ValID &ID) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000468 if (Ty == Type::LabelTy) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000469 GenerateError("Cannot use a basic block here");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000470 return 0;
471 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000472
Chris Lattner64116f92004-07-14 01:33:11 +0000473 // See if the value has already been defined.
Reid Spencer186a43f2007-03-19 18:39:36 +0000474 Value *V = getExistingVal(Ty, ID);
Chris Lattner2079fde2001-10-13 06:41:08 +0000475 if (V) return V;
Reid Spencer5b7e7532006-09-28 19:28:24 +0000476 if (TriggerError) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000477
Reid Spencer5b7e7532006-09-28 19:28:24 +0000478 if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty)) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000479 GenerateError("Invalid use of a composite type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000480 return 0;
481 }
Chris Lattner60cd9552005-03-23 01:29:26 +0000482
Chris Lattner00950542001-06-06 20:29:01 +0000483 // If we reached here, we referenced either a symbol that we don't know about
484 // or an id number that hasn't been read yet. We may be referencing something
485 // forward, so just create an entry to be resolved later and get to it...
486 //
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000487 switch (ID.Type) {
488 case ValID::GlobalName:
Reid Spencer3cb4dda2007-04-28 14:13:42 +0000489 case ValID::GlobalID: {
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000490 const PointerType *PTy = dyn_cast<PointerType>(Ty);
491 if (!PTy) {
492 GenerateError("Invalid type for reference to global" );
493 return 0;
494 }
495 const Type* ElTy = PTy->getElementType();
496 if (const FunctionType *FTy = dyn_cast<FunctionType>(ElTy))
497 V = new Function(FTy, GlobalValue::ExternalLinkage);
498 else
499 V = new GlobalVariable(ElTy, false, GlobalValue::ExternalLinkage);
500 break;
Reid Spencer3cb4dda2007-04-28 14:13:42 +0000501 }
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000502 default:
503 V = new Argument(Ty);
504 }
505
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000506 // Remember where this forward reference came from. FIXME, shouldn't we try
507 // to recycle these things??
Chris Lattner64116f92004-07-14 01:33:11 +0000508 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000509 llvmAsmlineno)));
510
Chris Lattner79df7c02002-03-26 18:01:55 +0000511 if (inFunctionScope())
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000512 InsertValue(V, CurFun.LateResolveValues);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000513 else
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000514 InsertValue(V, CurModule.LateResolveValues);
515 return V;
Chris Lattner00950542001-06-06 20:29:01 +0000516}
517
Reid Spencer186a43f2007-03-19 18:39:36 +0000518/// defineBBVal - This is a definition of a new basic block with the specified
519/// identifier which must be the same as CurFun.NextValNum, if its numeric.
520static BasicBlock *defineBBVal(const ValID &ID) {
Reid Spencer1755a9a2007-02-05 17:01:20 +0000521 assert(inFunctionScope() && "Can't get basic block at global scope!");
Chris Lattner64116f92004-07-14 01:33:11 +0000522
Chris Lattner2e2ed292004-07-14 06:28:35 +0000523 BasicBlock *BB = 0;
Chris Lattner64116f92004-07-14 01:33:11 +0000524
Reid Spencer186a43f2007-03-19 18:39:36 +0000525 // First, see if this was forward referenced
Chris Lattner64116f92004-07-14 01:33:11 +0000526
Reid Spencer186a43f2007-03-19 18:39:36 +0000527 std::map<ValID, BasicBlock*>::iterator BBI = CurFun.BBForwardRefs.find(ID);
528 if (BBI != CurFun.BBForwardRefs.end()) {
529 BB = BBI->second;
Chris Lattner2e2ed292004-07-14 06:28:35 +0000530 // The forward declaration could have been inserted anywhere in the
531 // function: insert it into the correct place now.
532 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
533 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
Reid Spencer186a43f2007-03-19 18:39:36 +0000534
Reid Spencer2c9df212007-03-20 01:13:00 +0000535 // We're about to erase the entry, save the key so we can clean it up.
536 ValID Tmp = BBI->first;
537
Reid Spencer186a43f2007-03-19 18:39:36 +0000538 // Erase the forward ref from the map as its no longer "forward"
539 CurFun.BBForwardRefs.erase(ID);
540
Reid Spencer2c9df212007-03-20 01:13:00 +0000541 // The key has been removed from the map but so we don't want to leave
542 // strdup'd memory around so destroy it too.
543 Tmp.destroy();
544
Reid Spencer186a43f2007-03-19 18:39:36 +0000545 // If its a numbered definition, bump the number and set the BB value.
546 if (ID.Type == ValID::LocalID) {
547 assert(ID.Num == CurFun.NextValNum && "Invalid new block number");
548 InsertValue(BB);
549 }
550
551 ID.destroy();
552 return BB;
553 }
554
555 // We haven't seen this BB before and its first mention is a definition.
556 // Just create it and return it.
557 std::string Name (ID.Type == ValID::LocalName ? ID.Name : "");
558 BB = new BasicBlock(Name, CurFun.CurrentFunction);
559 if (ID.Type == ValID::LocalID) {
560 assert(ID.Num == CurFun.NextValNum && "Invalid new block number");
561 InsertValue(BB);
Chris Lattner2e2ed292004-07-14 06:28:35 +0000562 }
Reid Spencer186a43f2007-03-19 18:39:36 +0000563
564 ID.destroy(); // Free strdup'd memory
565 return BB;
566}
567
568/// getBBVal - get an existing BB value or create a forward reference for it.
569///
570static BasicBlock *getBBVal(const ValID &ID) {
571 assert(inFunctionScope() && "Can't get basic block at global scope!");
572
573 BasicBlock *BB = 0;
574
575 std::map<ValID, BasicBlock*>::iterator BBI = CurFun.BBForwardRefs.find(ID);
576 if (BBI != CurFun.BBForwardRefs.end()) {
577 BB = BBI->second;
578 } if (ID.Type == ValID::LocalName) {
579 std::string Name = ID.Name;
580 Value *N = CurFun.CurrentFunction->getValueSymbolTable().lookup(Name);
581 if (N)
582 if (N->getType()->getTypeID() == Type::LabelTyID)
583 BB = cast<BasicBlock>(N);
584 else
585 GenerateError("Reference to label '" + Name + "' is actually of type '"+
586 N->getType()->getDescription() + "'");
587 } else if (ID.Type == ValID::LocalID) {
588 if (ID.Num < CurFun.NextValNum && ID.Num < CurFun.Values.size()) {
589 if (CurFun.Values[ID.Num]->getType()->getTypeID() == Type::LabelTyID)
590 BB = cast<BasicBlock>(CurFun.Values[ID.Num]);
591 else
592 GenerateError("Reference to label '%" + utostr(ID.Num) +
593 "' is actually of type '"+
594 CurFun.Values[ID.Num]->getType()->getDescription() + "'");
595 }
596 } else {
597 GenerateError("Illegal label reference " + ID.getName());
598 return 0;
599 }
600
601 // If its already been defined, return it now.
602 if (BB) {
603 ID.destroy(); // Free strdup'd memory.
604 return BB;
605 }
606
607 // Otherwise, this block has not been seen before, create it.
608 std::string Name;
609 if (ID.Type == ValID::LocalName)
610 Name = ID.Name;
611 BB = new BasicBlock(Name, CurFun.CurrentFunction);
612
613 // Insert it in the forward refs map.
614 CurFun.BBForwardRefs[ID] = BB;
615
Chris Lattner64116f92004-07-14 01:33:11 +0000616 return BB;
617}
618
Chris Lattner00950542001-06-06 20:29:01 +0000619
620//===----------------------------------------------------------------------===//
621// Code to handle forward references in instructions
622//===----------------------------------------------------------------------===//
623//
624// This code handles the late binding needed with statements that reference
625// values not defined yet... for example, a forward branch, or the PHI node for
626// a loop body.
627//
Chris Lattner394f2eb2003-10-16 20:12:13 +0000628// This keeps a table (CurFun.LateResolveValues) of all such forward references
Chris Lattner00950542001-06-06 20:29:01 +0000629// and back patchs after we are done.
630//
631
Misha Brukman5a2a3822005-05-10 22:02:28 +0000632// ResolveDefinitions - If we could not resolve some defs at parsing
633// time (forward branches, phi functions for loops, etc...) resolve the
Chris Lattner00950542001-06-06 20:29:01 +0000634// defs now...
635//
Misha Brukman5a2a3822005-05-10 22:02:28 +0000636static void
Reid Spencer186a43f2007-03-19 18:39:36 +0000637ResolveDefinitions(ValueList &LateResolvers, ValueList *FutureLateResolvers) {
Chris Lattner00950542001-06-06 20:29:01 +0000638 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Reid Spencer186a43f2007-03-19 18:39:36 +0000639 while (!LateResolvers.empty()) {
640 Value *V = LateResolvers.back();
641 LateResolvers.pop_back();
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000642
Reid Spencer186a43f2007-03-19 18:39:36 +0000643 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
644 CurModule.PlaceHolderInfo.find(V);
645 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000646
Reid Spencer186a43f2007-03-19 18:39:36 +0000647 ValID &DID = PHI->second.first;
Chris Lattner00950542001-06-06 20:29:01 +0000648
Reid Spencer186a43f2007-03-19 18:39:36 +0000649 Value *TheRealValue = getExistingVal(V->getType(), DID);
650 if (TriggerError)
651 return;
652 if (TheRealValue) {
653 V->replaceAllUsesWith(TheRealValue);
654 delete V;
655 CurModule.PlaceHolderInfo.erase(PHI);
656 } else if (FutureLateResolvers) {
657 // Functions have their unresolved items forwarded to the module late
658 // resolver table
659 InsertValue(V, *FutureLateResolvers);
660 } else {
661 if (DID.Type == ValID::LocalName || DID.Type == ValID::GlobalName) {
662 GenerateError("Reference to an invalid definition: '" +DID.getName()+
663 "' of type '" + V->getType()->getDescription() + "'",
664 PHI->second.second);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000665 return;
Chris Lattner386a3b72001-10-16 19:54:17 +0000666 } else {
Reid Spencer186a43f2007-03-19 18:39:36 +0000667 GenerateError("Reference to an invalid definition: #" +
668 itostr(DID.Num) + " of type '" +
669 V->getType()->getDescription() + "'",
670 PHI->second.second);
671 return;
Chris Lattner30c89792001-09-07 16:35:17 +0000672 }
Chris Lattner00950542001-06-06 20:29:01 +0000673 }
674 }
Chris Lattner00950542001-06-06 20:29:01 +0000675 LateResolvers.clear();
676}
677
Chris Lattner4a42e902001-10-22 05:56:09 +0000678// ResolveTypeTo - A brand new type was just declared. This means that (if
679// name is not null) things referencing Name can be resolved. Otherwise, things
680// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000681//
Chris Lattner4a42e902001-10-22 05:56:09 +0000682static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattner355ad1f2005-03-21 06:27:42 +0000683 ValID D;
Reid Spencerb2d17862007-01-26 08:04:51 +0000684 if (Name) D = ValID::createLocalName(Name);
685 else D = ValID::createLocalID(CurModule.Types.size());
Chris Lattner00950542001-06-06 20:29:01 +0000686
Reid Spencerb78b9082006-11-28 07:28:14 +0000687 std::map<ValID, PATypeHolder>::iterator I =
Chris Lattner355ad1f2005-03-21 06:27:42 +0000688 CurModule.LateResolveTypes.find(D);
689 if (I != CurModule.LateResolveTypes.end()) {
Reid Spencerb78b9082006-11-28 07:28:14 +0000690 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattner355ad1f2005-03-21 06:27:42 +0000691 CurModule.LateResolveTypes.erase(I);
Chris Lattner30c89792001-09-07 16:35:17 +0000692 }
693}
694
Chris Lattner1781aca2001-09-18 04:00:54 +0000695// setValueName - Set the specified value to the name given. The name may be
696// null potentially, in which case this is a noop. The string passed in is
Chris Lattner8ab406d2004-07-13 07:59:27 +0000697// assumed to be a malloc'd string buffer, and is free'd by this function.
698//
699static void setValueName(Value *V, char *NameStr) {
Reid Spencerb2d17862007-01-26 08:04:51 +0000700 if (!NameStr) return;
701 std::string Name(NameStr); // Copy string
702 free(NameStr); // Free old string
Chris Lattnerc9aea522004-07-13 08:12:39 +0000703
Reid Spencerb2d17862007-01-26 08:04:51 +0000704 if (V->getType() == Type::VoidTy) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000705 GenerateError("Can't assign name '" + Name+"' to value with void type");
Reid Spencerb2d17862007-01-26 08:04:51 +0000706 return;
Chris Lattner8ab406d2004-07-13 07:59:27 +0000707 }
Reid Spencerb2d17862007-01-26 08:04:51 +0000708
Reid Spencer1755a9a2007-02-05 17:01:20 +0000709 assert(inFunctionScope() && "Must be in function scope!");
Reid Spenceref9b9a72007-02-05 20:47:22 +0000710 ValueSymbolTable &ST = CurFun.CurrentFunction->getValueSymbolTable();
711 if (ST.lookup(Name)) {
Reid Spencerb2d17862007-01-26 08:04:51 +0000712 GenerateError("Redefinition of value '" + Name + "' of type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +0000713 V->getType()->getDescription() + "'");
Reid Spencerb2d17862007-01-26 08:04:51 +0000714 return;
715 }
716
717 // Set the name.
718 V->setName(Name);
Chris Lattner8ab406d2004-07-13 07:59:27 +0000719}
720
Chris Lattnerd88998d2004-07-14 08:23:52 +0000721/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
722/// this is a declaration, otherwise it is a definition.
Chris Lattnerb2b96672005-11-12 18:21:21 +0000723static GlobalVariable *
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000724ParseGlobalVariable(char *NameStr,
725 GlobalValue::LinkageTypes Linkage,
726 GlobalValue::VisibilityTypes Visibility,
Chris Lattnerb2b96672005-11-12 18:21:21 +0000727 bool isConstantGlobal, const Type *Ty,
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +0000728 Constant *Initializer, bool IsThreadLocal) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000729 if (isa<FunctionType>(Ty)) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000730 GenerateError("Cannot declare global vars of function type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000731 return 0;
732 }
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000733
Misha Brukman5a2a3822005-05-10 22:02:28 +0000734 const PointerType *PTy = PointerType::get(Ty);
Chris Lattnera09000d2004-07-14 23:03:46 +0000735
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000736 std::string Name;
737 if (NameStr) {
738 Name = NameStr; // Copy string
739 free(NameStr); // Free old string
Chris Lattnerd88998d2004-07-14 08:23:52 +0000740 }
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000741
Chris Lattner8a327842004-07-14 21:44:00 +0000742 // See if this global value was forward referenced. If so, recycle the
743 // object.
Misha Brukman5a2a3822005-05-10 22:02:28 +0000744 ValID ID;
Chris Lattner8a327842004-07-14 21:44:00 +0000745 if (!Name.empty()) {
Reid Spencerb2d17862007-01-26 08:04:51 +0000746 ID = ValID::createGlobalName((char*)Name.c_str());
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000747 } else {
Reid Spencer186a43f2007-03-19 18:39:36 +0000748 ID = ValID::createGlobalID(CurModule.Values.size());
Chris Lattner8a327842004-07-14 21:44:00 +0000749 }
750
Reid Spencer3cb4dda2007-04-28 14:13:42 +0000751 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, 0, ID)) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000752 // Move the global to the end of the list, from whereever it was
Chris Lattner8a327842004-07-14 21:44:00 +0000753 // previously inserted.
754 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
755 CurModule.CurrentModule->getGlobalList().remove(GV);
756 CurModule.CurrentModule->getGlobalList().push_back(GV);
757 GV->setInitializer(Initializer);
758 GV->setLinkage(Linkage);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000759 GV->setVisibility(Visibility);
Chris Lattner8a327842004-07-14 21:44:00 +0000760 GV->setConstant(isConstantGlobal);
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +0000761 GV->setThreadLocal(IsThreadLocal);
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000762 InsertValue(GV, CurModule.Values);
Chris Lattnerb2b96672005-11-12 18:21:21 +0000763 return GV;
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000764 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000765
Reid Spenceref9b9a72007-02-05 20:47:22 +0000766 // If this global has a name
Chris Lattnera09000d2004-07-14 23:03:46 +0000767 if (!Name.empty()) {
Reid Spenceref9b9a72007-02-05 20:47:22 +0000768 // if the global we're parsing has an initializer (is a definition) and
769 // has external linkage.
770 if (Initializer && Linkage != GlobalValue::InternalLinkage)
771 // If there is already a global with external linkage with this name
772 if (CurModule.CurrentModule->getGlobalVariable(Name, false)) {
773 // If we allow this GVar to get created, it will be renamed in the
774 // symbol table because it conflicts with an existing GVar. We can't
775 // allow redefinition of GVars whose linking indicates that their name
776 // must stay the same. Issue the error.
777 GenerateError("Redefinition of global variable named '" + Name +
778 "' of type '" + Ty->getDescription() + "'");
779 return 0;
780 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000781 }
782
783 // Otherwise there is no existing GV to use, create one now.
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000784 GlobalVariable *GV =
Misha Brukman5a2a3822005-05-10 22:02:28 +0000785 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +0000786 CurModule.CurrentModule, IsThreadLocal);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000787 GV->setVisibility(Visibility);
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000788 InsertValue(GV, CurModule.Values);
Chris Lattnerb2b96672005-11-12 18:21:21 +0000789 return GV;
Chris Lattnerd88998d2004-07-14 08:23:52 +0000790}
Chris Lattner8ab406d2004-07-13 07:59:27 +0000791
Reid Spencer75719bf2004-05-26 21:48:31 +0000792// setTypeName - Set the specified type to the name given. The name may be
793// null potentially, in which case this is a noop. The string passed in is
794// assumed to be a malloc'd string buffer, and is freed by this function.
795//
796// This function returns true if the type has already been defined, but is
797// allowed to be redefined in the specified context. If the name is a new name
798// for the type plane, it is inserted and false is returned.
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000799static bool setTypeName(const Type *T, char *NameStr) {
Reid Spencer1755a9a2007-02-05 17:01:20 +0000800 assert(!inFunctionScope() && "Can't give types function-local names!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000801 if (NameStr == 0) return false;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000802
Reid Spencer75719bf2004-05-26 21:48:31 +0000803 std::string Name(NameStr); // Copy string
804 free(NameStr); // Free old string
805
806 // We don't allow assigning names to void type
Reid Spencer5b7e7532006-09-28 19:28:24 +0000807 if (T == Type::VoidTy) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000808 GenerateError("Can't assign name '" + Name + "' to the void type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000809 return false;
810 }
Reid Spencer75719bf2004-05-26 21:48:31 +0000811
Chris Lattnera09000d2004-07-14 23:03:46 +0000812 // Set the type name, checking for conflicts as we do so.
813 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000814
Chris Lattnera09000d2004-07-14 23:03:46 +0000815 if (AlreadyExists) { // Inserting a name that is already defined???
816 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
Reid Spencer1755a9a2007-02-05 17:01:20 +0000817 assert(Existing && "Conflict but no matching type?!");
Chris Lattnera09000d2004-07-14 23:03:46 +0000818
Reid Spencer75719bf2004-05-26 21:48:31 +0000819 // There is only one case where this is allowed: when we are refining an
820 // opaque type. In this case, Existing will be an opaque type.
821 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
822 // We ARE replacing an opaque type!
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000823 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000824 return true;
825 }
826
827 // Otherwise, this is an attempt to redefine a type. That's okay if
828 // the redefinition is identical to the original. This will be so if
829 // Existing and T point to the same Type object. In this one case we
830 // allow the equivalent redefinition.
831 if (Existing == T) return true; // Yes, it's equal.
832
833 // Any other kind of (non-equivalent) redefinition is an error.
Reid Spencer90e2f632007-01-05 21:50:38 +0000834 GenerateError("Redefinition of type named '" + Name + "' of type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +0000835 T->getDescription() + "'");
Reid Spencer75719bf2004-05-26 21:48:31 +0000836 }
837
Reid Spencer75719bf2004-05-26 21:48:31 +0000838 return false;
839}
Chris Lattner8896eda2001-07-09 19:38:36 +0000840
Chris Lattner30c89792001-09-07 16:35:17 +0000841//===----------------------------------------------------------------------===//
842// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000843//
Chris Lattner8896eda2001-07-09 19:38:36 +0000844
Chris Lattner3b073862004-02-09 03:19:29 +0000845// TypeContains - Returns true if Ty directly contains E in it.
Chris Lattner30c89792001-09-07 16:35:17 +0000846//
847static bool TypeContains(const Type *Ty, const Type *E) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000848 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
Chris Lattner6d8dbec2004-12-08 16:13:53 +0000849 E) != Ty->subtype_end();
Chris Lattner3b073862004-02-09 03:19:29 +0000850}
851
852namespace {
853 struct UpRefRecord {
854 // NestingLevel - The number of nesting levels that need to be popped before
855 // this type is resolved.
856 unsigned NestingLevel;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000857
Chris Lattner3b073862004-02-09 03:19:29 +0000858 // LastContainedTy - This is the type at the current binding level for the
859 // type. Every time we reduce the nesting level, this gets updated.
860 const Type *LastContainedTy;
861
862 // UpRefTy - This is the actual opaque type that the upreference is
863 // represented with.
864 OpaqueType *UpRefTy;
865
866 UpRefRecord(unsigned NL, OpaqueType *URTy)
867 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
868 };
Chris Lattner30c89792001-09-07 16:35:17 +0000869}
Chris Lattner698b56e2001-07-20 19:15:08 +0000870
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000871// UpRefs - A list of the outstanding upreferences that need to be resolved.
Chris Lattner3b073862004-02-09 03:19:29 +0000872static std::vector<UpRefRecord> UpRefs;
Chris Lattner30c89792001-09-07 16:35:17 +0000873
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000874/// HandleUpRefs - Every time we finish a new layer of types, this function is
875/// called. It loops through the UpRefs vector, which is a list of the
876/// currently active types. For each type, if the up reference is contained in
877/// the newly completed type, we decrement the level count. When the level
878/// count reaches zero, the upreferenced type is the type that is passed in:
879/// thus we can complete the cycle.
880///
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000881static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner703e92f2006-08-18 17:34:24 +0000882 // If Ty isn't abstract, or if there are no up-references in it, then there is
883 // nothing to resolve here.
884 if (!ty->isAbstract() || UpRefs.empty()) return ty;
885
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000886 PATypeHolder Ty(ty);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000887 UR_OUT("Type '" << Ty->getDescription() <<
Chris Lattner5084d032001-11-02 07:46:26 +0000888 "' newly formed. Resolving upreferences.\n" <<
889 UpRefs.size() << " upreferences active!\n");
Chris Lattner026a8ce2004-02-09 18:53:54 +0000890
891 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
892 // to zero), we resolve them all together before we resolve them to Ty. At
893 // the end of the loop, if there is anything to resolve to Ty, it will be in
894 // this variable.
895 OpaqueType *TypeToResolve = 0;
896
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000897 for (unsigned i = 0; i != UpRefs.size(); ++i) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000898 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
899 << UpRefs[i].second->getDescription() << ") = "
Reid Spencer3271ed52004-07-18 00:08:11 +0000900 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
Chris Lattner3b073862004-02-09 03:19:29 +0000901 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
902 // Decrement level of upreference
903 unsigned Level = --UpRefs[i].NestingLevel;
904 UpRefs[i].LastContainedTy = Ty;
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000905 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
Misha Brukman5a2a3822005-05-10 22:02:28 +0000906 if (Level == 0) { // Upreference should be resolved!
Chris Lattner026a8ce2004-02-09 18:53:54 +0000907 if (!TypeToResolve) {
908 TypeToResolve = UpRefs[i].UpRefTy;
909 } else {
910 UR_OUT(" * Resolving upreference for "
911 << UpRefs[i].second->getDescription() << "\n";
912 std::string OldName = UpRefs[i].UpRefTy->getDescription());
913 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
914 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
915 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
916 }
Reid Spencer3271ed52004-07-18 00:08:11 +0000917 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000918 --i; // Do not skip the next element...
Chris Lattner30c89792001-09-07 16:35:17 +0000919 }
920 }
Chris Lattner8896eda2001-07-09 19:38:36 +0000921 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000922
Chris Lattner026a8ce2004-02-09 18:53:54 +0000923 if (TypeToResolve) {
924 UR_OUT(" * Resolving upreference for "
925 << UpRefs[i].second->getDescription() << "\n";
Chris Lattner16af11d2004-02-09 21:03:38 +0000926 std::string OldName = TypeToResolve->getDescription());
Chris Lattner026a8ce2004-02-09 18:53:54 +0000927 TypeToResolve->refineAbstractTypeTo(Ty);
928 }
929
Chris Lattner8896eda2001-07-09 19:38:36 +0000930 return Ty;
931}
932
Chris Lattner00950542001-06-06 20:29:01 +0000933//===----------------------------------------------------------------------===//
934// RunVMAsmParser - Define an interface to this parser
935//===----------------------------------------------------------------------===//
936//
Reid Spencere1553cc2006-12-31 05:40:12 +0000937static Module* RunParser(Module * M);
938
Chris Lattner86427632004-07-14 06:39:48 +0000939Module *llvm::RunVMAsmParser(const std::string &Filename, FILE *F) {
Chris Lattner6184feb2005-05-20 03:25:47 +0000940 set_scan_file(F);
941
Chris Lattnera2850432001-07-22 18:36:00 +0000942 CurFilename = Filename;
Chris Lattner6184feb2005-05-20 03:25:47 +0000943 return RunParser(new Module(CurFilename));
944}
Chris Lattner00950542001-06-06 20:29:01 +0000945
Chris Lattner6184feb2005-05-20 03:25:47 +0000946Module *llvm::RunVMAsmParser(const char * AsmString, Module * M) {
947 set_scan_string(AsmString);
Chris Lattner42570982003-11-26 07:24:58 +0000948
Chris Lattner6184feb2005-05-20 03:25:47 +0000949 CurFilename = "from_memory";
950 if (M == NULL) {
951 return RunParser(new Module (CurFilename));
952 } else {
953 return RunParser(M);
954 }
Chris Lattner00950542001-06-06 20:29:01 +0000955}
956
957%}
958
959%union {
Brian Gaeked0fde302003-11-11 22:41:34 +0000960 llvm::Module *ModuleVal;
961 llvm::Function *FunctionVal;
Brian Gaeked0fde302003-11-11 22:41:34 +0000962 llvm::BasicBlock *BasicBlockVal;
963 llvm::TerminatorInst *TermInstVal;
964 llvm::Instruction *InstVal;
Reid Spencer08de34b2006-12-03 05:45:44 +0000965 llvm::Constant *ConstVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000966
Reid Spencer08de34b2006-12-03 05:45:44 +0000967 const llvm::Type *PrimType;
Reid Spencere1553cc2006-12-31 05:40:12 +0000968 std::list<llvm::PATypeHolder> *TypeList;
Reid Spencer08de34b2006-12-03 05:45:44 +0000969 llvm::PATypeHolder *TypeVal;
970 llvm::Value *ValueVal;
Reid Spencer08de34b2006-12-03 05:45:44 +0000971 std::vector<llvm::Value*> *ValueList;
Reid Spencere1553cc2006-12-31 05:40:12 +0000972 llvm::ArgListType *ArgList;
973 llvm::TypeWithAttrs TypeWithAttrs;
974 llvm::TypeWithAttrsList *TypeWithAttrsList;
975 llvm::ValueRefList *ValueRefList;
976
Misha Brukman5a2a3822005-05-10 22:02:28 +0000977 // Represent the RHS of PHI node
Reid Spencer08de34b2006-12-03 05:45:44 +0000978 std::list<std::pair<llvm::Value*,
979 llvm::BasicBlock*> > *PHIList;
Brian Gaeked0fde302003-11-11 22:41:34 +0000980 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
Reid Spencer08de34b2006-12-03 05:45:44 +0000981 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000982
Brian Gaeked0fde302003-11-11 22:41:34 +0000983 llvm::GlobalValue::LinkageTypes Linkage;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000984 llvm::GlobalValue::VisibilityTypes Visibility;
Reid Spencer5694b6e2007-04-09 06:17:21 +0000985 uint16_t ParamAttrs;
Reid Spencerf2310042007-02-28 02:23:44 +0000986 llvm::APInt *APIntVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000987 int64_t SInt64Val;
988 uint64_t UInt64Val;
989 int SIntVal;
990 unsigned UIntVal;
991 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000992 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000993
Chris Lattner30c89792001-09-07 16:35:17 +0000994 char *StrVal; // This memory is strdup'd!
Reid Spencer1628cec2006-10-26 06:15:43 +0000995 llvm::ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000996
Reid Spencer08de34b2006-12-03 05:45:44 +0000997 llvm::Instruction::BinaryOps BinaryOpVal;
998 llvm::Instruction::TermOps TermOpVal;
999 llvm::Instruction::MemoryOps MemOpVal;
1000 llvm::Instruction::CastOps CastOpVal;
1001 llvm::Instruction::OtherOps OtherOpVal;
Reid Spencer08de34b2006-12-03 05:45:44 +00001002 llvm::ICmpInst::Predicate IPredicate;
1003 llvm::FCmpInst::Predicate FPredicate;
Chris Lattner00950542001-06-06 20:29:01 +00001004}
1005
Reid Spencere1553cc2006-12-31 05:40:12 +00001006%type <ModuleVal> Module
Chris Lattner79df7c02002-03-26 18:01:55 +00001007%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +00001008%type <BasicBlockVal> BasicBlock InstructionList
1009%type <TermInstVal> BBTerminatorInst
1010%type <InstVal> Inst InstVal MemoryInst
Anton Korobeynikova80e1182007-04-28 13:45:00 +00001011%type <ConstVal> ConstVal ConstExpr AliaseeRef
Chris Lattner6cdb0112001-11-26 16:54:11 +00001012%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +00001013%type <ArgList> ArgList ArgListH
Chris Lattnerc24d2082001-06-11 15:04:20 +00001014%type <PHIList> PHIList
Reid Spencere1553cc2006-12-31 05:40:12 +00001015%type <ValueRefList> ValueRefList // For call param lists & GEP indices
1016%type <ValueList> IndexList // For GEP indices
1017%type <TypeList> TypeListI
1018%type <TypeWithAttrsList> ArgTypeList ArgTypeListI
Reid Spencer2c261782007-01-05 17:06:19 +00001019%type <TypeWithAttrs> ArgType
Chris Lattner00950542001-06-06 20:29:01 +00001020%type <JumpTable> JumpTable
Chris Lattner4ad02e72003-04-16 20:28:45 +00001021%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00001022%type <BoolVal> ThreadLocal // 'thread_local' or not
Chris Lattner15c9c032003-09-08 18:20:29 +00001023%type <BoolVal> OptVolatile // 'volatile' or not
Chris Lattnerddb6db42005-05-06 05:51:46 +00001024%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
Chris Lattneraa2c8532006-01-25 22:26:43 +00001025%type <BoolVal> OptSideEffect // 'sideeffect' or not.
Reid Spencere1553cc2006-12-31 05:40:12 +00001026%type <Linkage> GVInternalLinkage GVExternalLinkage
1027%type <Linkage> FunctionDefineLinkage FunctionDeclareLinkage
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001028%type <Linkage> AliasLinkage
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001029%type <Visibility> GVVisibilityStyle
Chris Lattner00950542001-06-06 20:29:01 +00001030
Chris Lattner2079fde2001-10-13 06:41:08 +00001031// ValueRef - Unresolved reference to a definition or BB
1032%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001033%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +00001034// Tokens and types for handling constant integer values
1035//
1036// ESINT64VAL - A negative number within long long range
1037%token <SInt64Val> ESINT64VAL
1038
1039// EUINT64VAL - A positive number within uns. long long range
1040%token <UInt64Val> EUINT64VAL
Chris Lattner00950542001-06-06 20:29:01 +00001041
Reid Spencerf2310042007-02-28 02:23:44 +00001042// ESAPINTVAL - A negative number with arbitrary precision
1043%token <APIntVal> ESAPINTVAL
1044
1045// EUAPINTVAL - A positive number with arbitrary precision
1046%token <APIntVal> EUAPINTVAL
1047
Reid Spencerb2d17862007-01-26 08:04:51 +00001048%token <UIntVal> LOCALVAL_ID GLOBALVAL_ID // %123 @123
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001049%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +00001050
1051// Built in types...
Reid Spencer2c261782007-01-05 17:06:19 +00001052%type <TypeVal> Types ResultTypes
Reid Spencere1553cc2006-12-31 05:40:12 +00001053%type <PrimType> IntType FPType PrimType // Classifications
Reid Spencer8088e9d2007-01-13 05:00:20 +00001054%token <PrimType> VOID INTTYPE
Reid Spencerb951bc02006-12-29 20:29:48 +00001055%token <PrimType> FLOAT DOUBLE LABEL
1056%token TYPE
Chris Lattner00950542001-06-06 20:29:01 +00001057
Reid Spencerb2d17862007-01-26 08:04:51 +00001058%token<StrVal> LOCALVAR GLOBALVAR LABELSTR STRINGCONSTANT ATSTRINGCONSTANT
1059%type <StrVal> LocalName OptLocalName OptLocalAssign
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001060%type <StrVal> GlobalName OptGlobalAssign GlobalAssign
Reid Spencerb2d17862007-01-26 08:04:51 +00001061%type <UIntVal> OptAlign OptCAlign
Chris Lattnerb2b96672005-11-12 18:21:21 +00001062%type <StrVal> OptSection SectionString
Chris Lattner00950542001-06-06 20:29:01 +00001063
Reid Spencer744d0362007-04-09 01:55:42 +00001064%token ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001065%token DECLARE DEFINE GLOBAL CONSTANT SECTION ALIAS VOLATILE THREAD_LOCAL
Reid Spencere1553cc2006-12-31 05:40:12 +00001066%token TO DOTDOTDOT NULL_TOK UNDEF INTERNAL LINKONCE WEAK APPENDING
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00001067%token DLLIMPORT DLLEXPORT EXTERN_WEAK
Reid Spencerb2d17862007-01-26 08:04:51 +00001068%token OPAQUE EXTERNAL TARGET TRIPLE ALIGN
Chris Lattneraa2c8532006-01-25 22:26:43 +00001069%token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001070%token CC_TOK CCC_TOK FASTCC_TOK COLDCC_TOK X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
Chris Lattner10b27112006-10-22 06:07:41 +00001071%token DATALAYOUT
Chris Lattnera8e8f162005-05-06 20:27:19 +00001072%type <UIntVal> OptCallingConv
Reid Spencer2c261782007-01-05 17:06:19 +00001073%type <ParamAttrs> OptParamAttrs ParamAttr
1074%type <ParamAttrs> OptFuncAttrs FuncAttr
Chris Lattner00950542001-06-06 20:29:01 +00001075
Misha Brukman5a2a3822005-05-10 22:02:28 +00001076// Basic Block Terminating Operators
Chris Lattner16710e92004-10-16 18:17:13 +00001077%token <TermOpVal> RET BR SWITCH INVOKE UNWIND UNREACHABLE
Chris Lattner00950542001-06-06 20:29:01 +00001078
Misha Brukman5a2a3822005-05-10 22:02:28 +00001079// Binary Operators
Reid Spencere4d87aa2006-12-23 06:05:41 +00001080%type <BinaryOpVal> ArithmeticOps LogicalOps // Binops Subcatagories
Reid Spencer0a783f72006-11-02 01:53:59 +00001081%token <BinaryOpVal> ADD SUB MUL UDIV SDIV FDIV UREM SREM FREM AND OR XOR
Reid Spencer832254e2007-02-02 02:16:23 +00001082%token <BinaryOpVal> SHL LSHR ASHR
1083
Reid Spencer08de34b2006-12-03 05:45:44 +00001084%token <OtherOpVal> ICMP FCMP
Reid Spencer08de34b2006-12-03 05:45:44 +00001085%type <IPredicate> IPredicates
Reid Spencer08de34b2006-12-03 05:45:44 +00001086%type <FPredicate> FPredicates
Reid Spencer9f746f42006-12-03 06:58:07 +00001087%token EQ NE SLT SGT SLE SGE ULT UGT ULE UGE
1088%token OEQ ONE OLT OGT OLE OGE ORD UNO UEQ UNE
Chris Lattner00950542001-06-06 20:29:01 +00001089
1090// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001091%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +00001092
Reid Spencer3da59db2006-11-27 01:05:10 +00001093// Cast Operators
1094%type <CastOpVal> CastOps
1095%token <CastOpVal> TRUNC ZEXT SEXT FPTRUNC FPEXT BITCAST
1096%token <CastOpVal> UITOFP SITOFP FPTOUI FPTOSI INTTOPTR PTRTOINT
1097
Chris Lattner027dcc52001-07-08 21:10:27 +00001098// Other Operators
Reid Spencer832254e2007-02-02 02:16:23 +00001099%token <OtherOpVal> PHI_TOK SELECT VAARG
Chris Lattner4c949082006-04-08 01:18:35 +00001100%token <OtherOpVal> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
Chris Lattnerddb6db42005-05-06 05:51:46 +00001101
Reid Spencer2c261782007-01-05 17:06:19 +00001102// Function Attributes
Reid Spencera39dfd32007-03-22 02:13:23 +00001103%token NORETURN INREG SRET NOUNWIND
Chris Lattner027dcc52001-07-08 21:10:27 +00001104
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001105// Visibility Styles
1106%token DEFAULT HIDDEN
1107
Chris Lattner00950542001-06-06 20:29:01 +00001108%start Module
1109%%
1110
Chris Lattner00950542001-06-06 20:29:01 +00001111
Misha Brukman5a2a3822005-05-10 22:02:28 +00001112// Operations that are notably excluded from this list include:
Chris Lattner00950542001-06-06 20:29:01 +00001113// RET, BR, & SWITCH because they end basic blocks and are treated specially.
1114//
Reid Spencer0a783f72006-11-02 01:53:59 +00001115ArithmeticOps: ADD | SUB | MUL | UDIV | SDIV | FDIV | UREM | SREM | FREM;
Reid Spencer832254e2007-02-02 02:16:23 +00001116LogicalOps : SHL | LSHR | ASHR | AND | OR | XOR;
Reid Spencer3da59db2006-11-27 01:05:10 +00001117CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | BITCAST |
1118 UITOFP | SITOFP | FPTOUI | FPTOSI | INTTOPTR | PTRTOINT;
Reid Spencer832254e2007-02-02 02:16:23 +00001119
Reid Spencer9f746f42006-12-03 06:58:07 +00001120IPredicates
Reid Spencer763ed5e2006-12-04 05:20:06 +00001121 : EQ { $$ = ICmpInst::ICMP_EQ; } | NE { $$ = ICmpInst::ICMP_NE; }
Reid Spencer9f746f42006-12-03 06:58:07 +00001122 | SLT { $$ = ICmpInst::ICMP_SLT; } | SGT { $$ = ICmpInst::ICMP_SGT; }
1123 | SLE { $$ = ICmpInst::ICMP_SLE; } | SGE { $$ = ICmpInst::ICMP_SGE; }
1124 | ULT { $$ = ICmpInst::ICMP_ULT; } | UGT { $$ = ICmpInst::ICMP_UGT; }
1125 | ULE { $$ = ICmpInst::ICMP_ULE; } | UGE { $$ = ICmpInst::ICMP_UGE; }
1126 ;
1127
1128FPredicates
1129 : OEQ { $$ = FCmpInst::FCMP_OEQ; } | ONE { $$ = FCmpInst::FCMP_ONE; }
1130 | OLT { $$ = FCmpInst::FCMP_OLT; } | OGT { $$ = FCmpInst::FCMP_OGT; }
1131 | OLE { $$ = FCmpInst::FCMP_OLE; } | OGE { $$ = FCmpInst::FCMP_OGE; }
1132 | ORD { $$ = FCmpInst::FCMP_ORD; } | UNO { $$ = FCmpInst::FCMP_UNO; }
1133 | UEQ { $$ = FCmpInst::FCMP_UEQ; } | UNE { $$ = FCmpInst::FCMP_UNE; }
1134 | ULT { $$ = FCmpInst::FCMP_ULT; } | UGT { $$ = FCmpInst::FCMP_UGT; }
1135 | ULE { $$ = FCmpInst::FCMP_ULE; } | UGE { $$ = FCmpInst::FCMP_UGE; }
1136 | TRUETOK { $$ = FCmpInst::FCMP_TRUE; }
1137 | FALSETOK { $$ = FCmpInst::FCMP_FALSE; }
1138 ;
Chris Lattner00950542001-06-06 20:29:01 +00001139
Chris Lattnere98dda62001-07-14 06:10:16 +00001140// These are some types that allow classification if we only want a particular
1141// thing... for example, only a signed, unsigned, or integral type.
Reid Spencera54b7cb2007-01-12 07:05:14 +00001142IntType : INTTYPE;
Chris Lattner51727be2002-06-04 21:58:56 +00001143FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +00001144
Reid Spencerb2d17862007-01-26 08:04:51 +00001145LocalName : LOCALVAR | STRINGCONSTANT;
1146OptLocalName : LocalName | /*empty*/ { $$ = 0; };
1147
1148/// OptLocalAssign - Value producing statements have an optional assignment
1149/// component.
1150OptLocalAssign : LocalName '=' {
1151 $$ = $1;
1152 CHECK_FOR_ERROR
1153 }
1154 | /*empty*/ {
1155 $$ = 0;
1156 CHECK_FOR_ERROR
1157 };
1158
1159GlobalName : GLOBALVAR | ATSTRINGCONSTANT;
1160
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001161OptGlobalAssign : GlobalAssign
Misha Brukman5a2a3822005-05-10 22:02:28 +00001162 | /*empty*/ {
1163 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00001164 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001165 };
Chris Lattner00950542001-06-06 20:29:01 +00001166
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001167GlobalAssign : GlobalName '=' {
1168 $$ = $1;
1169 CHECK_FOR_ERROR
Anton Korobeynikovc0fabcb2007-04-25 18:07:40 +00001170 };
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001171
Reid Spencerb951bc02006-12-29 20:29:48 +00001172GVInternalLinkage
1173 : INTERNAL { $$ = GlobalValue::InternalLinkage; }
1174 | WEAK { $$ = GlobalValue::WeakLinkage; }
1175 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1176 | APPENDING { $$ = GlobalValue::AppendingLinkage; }
1177 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
1178 ;
1179
1180GVExternalLinkage
1181 : DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1182 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
1183 | EXTERNAL { $$ = GlobalValue::ExternalLinkage; }
1184 ;
1185
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001186GVVisibilityStyle
1187 : /*empty*/ { $$ = GlobalValue::DefaultVisibility; }
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001188 | DEFAULT { $$ = GlobalValue::DefaultVisibility; }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001189 | HIDDEN { $$ = GlobalValue::HiddenVisibility; }
1190 ;
1191
Reid Spencere1553cc2006-12-31 05:40:12 +00001192FunctionDeclareLinkage
1193 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1194 | DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1195 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
Reid Spencerb951bc02006-12-29 20:29:48 +00001196 ;
1197
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001198FunctionDefineLinkage
Reid Spencere1553cc2006-12-31 05:40:12 +00001199 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1200 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
Reid Spencerb951bc02006-12-29 20:29:48 +00001201 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1202 | WEAK { $$ = GlobalValue::WeakLinkage; }
1203 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
Reid Spencerb951bc02006-12-29 20:29:48 +00001204 ;
Chris Lattner30c89792001-09-07 16:35:17 +00001205
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001206AliasLinkage
1207 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1208 | WEAK { $$ = GlobalValue::WeakLinkage; }
1209 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
1210 ;
1211
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00001212OptCallingConv : /*empty*/ { $$ = CallingConv::C; } |
1213 CCC_TOK { $$ = CallingConv::C; } |
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00001214 FASTCC_TOK { $$ = CallingConv::Fast; } |
1215 COLDCC_TOK { $$ = CallingConv::Cold; } |
1216 X86_STDCALLCC_TOK { $$ = CallingConv::X86_StdCall; } |
1217 X86_FASTCALLCC_TOK { $$ = CallingConv::X86_FastCall; } |
1218 CC_TOK EUINT64VAL {
Chris Lattnera8e8f162005-05-06 20:27:19 +00001219 if ((unsigned)$2 != $2)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001220 GEN_ERROR("Calling conv too large");
Chris Lattnera8e8f162005-05-06 20:27:19 +00001221 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001222 CHECK_FOR_ERROR
Chris Lattnera8e8f162005-05-06 20:27:19 +00001223 };
1224
Reid Spencer18da0722007-04-11 02:44:20 +00001225ParamAttr : ZEXT { $$ = ParamAttr::ZExt; }
1226 | SEXT { $$ = ParamAttr::SExt; }
1227 | INREG { $$ = ParamAttr::InReg; }
1228 | SRET { $$ = ParamAttr::StructRet; }
Reid Spencere1553cc2006-12-31 05:40:12 +00001229 ;
1230
Reid Spencer18da0722007-04-11 02:44:20 +00001231OptParamAttrs : /* empty */ { $$ = ParamAttr::None; }
Reid Spencer2c261782007-01-05 17:06:19 +00001232 | OptParamAttrs ParamAttr {
Reid Spencer5694b6e2007-04-09 06:17:21 +00001233 $$ = $1 | $2;
Reid Spencere1553cc2006-12-31 05:40:12 +00001234 }
1235 ;
1236
Reid Spencer18da0722007-04-11 02:44:20 +00001237FuncAttr : NORETURN { $$ = ParamAttr::NoReturn; }
1238 | NOUNWIND { $$ = ParamAttr::NoUnwind; }
Reid Spencer2c261782007-01-05 17:06:19 +00001239 | ParamAttr
1240 ;
1241
Reid Spencer18da0722007-04-11 02:44:20 +00001242OptFuncAttrs : /* empty */ { $$ = ParamAttr::None; }
Reid Spencer2c261782007-01-05 17:06:19 +00001243 | OptFuncAttrs FuncAttr {
Reid Spencer5694b6e2007-04-09 06:17:21 +00001244 $$ = $1 | $2;
Reid Spencer2c261782007-01-05 17:06:19 +00001245 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001246 ;
1247
Chris Lattner66db8e42005-11-06 06:34:12 +00001248// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1249// a comma before it.
Chris Lattner87ac9722005-11-06 06:46:28 +00001250OptAlign : /*empty*/ { $$ = 0; } |
Chris Lattnerb2b96672005-11-12 18:21:21 +00001251 ALIGN EUINT64VAL {
1252 $$ = $2;
1253 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001254 GEN_ERROR("Alignment must be a power of two");
Reid Spencer61c83e02006-08-18 08:43:06 +00001255 CHECK_FOR_ERROR
Chris Lattnerb2b96672005-11-12 18:21:21 +00001256};
Chris Lattner66db8e42005-11-06 06:34:12 +00001257OptCAlign : /*empty*/ { $$ = 0; } |
Chris Lattnerb2b96672005-11-12 18:21:21 +00001258 ',' ALIGN EUINT64VAL {
1259 $$ = $3;
1260 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001261 GEN_ERROR("Alignment must be a power of two");
Reid Spencer61c83e02006-08-18 08:43:06 +00001262 CHECK_FOR_ERROR
Chris Lattnerb2b96672005-11-12 18:21:21 +00001263};
1264
Chris Lattner66db8e42005-11-06 06:34:12 +00001265
Chris Lattner164c3782005-11-12 00:11:10 +00001266SectionString : SECTION STRINGCONSTANT {
1267 for (unsigned i = 0, e = strlen($2); i != e; ++i)
1268 if ($2[i] == '"' || $2[i] == '\\')
Reid Spencerf4fa5902007-02-05 10:16:10 +00001269 GEN_ERROR("Invalid character in section name");
Chris Lattner164c3782005-11-12 00:11:10 +00001270 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001271 CHECK_FOR_ERROR
Chris Lattner164c3782005-11-12 00:11:10 +00001272};
1273
1274OptSection : /*empty*/ { $$ = 0; } |
1275 SectionString { $$ = $1; };
Chris Lattner164c3782005-11-12 00:11:10 +00001276
Chris Lattnerb2b96672005-11-12 18:21:21 +00001277// GlobalVarAttributes - Used to pass the attributes string on a global. CurGV
1278// is set to be the global we are processing.
1279//
1280GlobalVarAttributes : /* empty */ {} |
1281 ',' GlobalVarAttribute GlobalVarAttributes {};
1282GlobalVarAttribute : SectionString {
1283 CurGV->setSection($1);
1284 free($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001285 CHECK_FOR_ERROR
Chris Lattnerb2b96672005-11-12 18:21:21 +00001286 }
1287 | ALIGN EUINT64VAL {
1288 if ($2 != 0 && !isPowerOf2_32($2))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001289 GEN_ERROR("Alignment must be a power of two");
Chris Lattnerb2b96672005-11-12 18:21:21 +00001290 CurGV->setAlignment($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001291 CHECK_FOR_ERROR
Chris Lattnerb2b96672005-11-12 18:21:21 +00001292 };
Chris Lattner164c3782005-11-12 00:11:10 +00001293
Chris Lattner30c89792001-09-07 16:35:17 +00001294//===----------------------------------------------------------------------===//
1295// Types includes all predefined types... except void, because it can only be
Reid Spencere1553cc2006-12-31 05:40:12 +00001296// used in specific contexts (function returning void for example).
Chris Lattner30c89792001-09-07 16:35:17 +00001297
1298// Derived types are added later...
1299//
Reid Spencer8088e9d2007-01-13 05:00:20 +00001300PrimType : INTTYPE | FLOAT | DOUBLE | LABEL ;
Reid Spencere1553cc2006-12-31 05:40:12 +00001301
1302Types
1303 : OPAQUE {
Reid Spencer08de34b2006-12-03 05:45:44 +00001304 $$ = new PATypeHolder(OpaqueType::get());
Reid Spencer61c83e02006-08-18 08:43:06 +00001305 CHECK_FOR_ERROR
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001306 }
1307 | PrimType {
Reid Spencer08de34b2006-12-03 05:45:44 +00001308 $$ = new PATypeHolder($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001309 CHECK_FOR_ERROR
Reid Spencere1553cc2006-12-31 05:40:12 +00001310 }
1311 | Types '*' { // Pointer type?
1312 if (*$1 == Type::LabelTy)
1313 GEN_ERROR("Cannot form a pointer to a basic block");
1314 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
1315 delete $1;
1316 CHECK_FOR_ERROR
1317 }
1318 | SymbolicValueRef { // Named types are also simple types...
1319 const Type* tmp = getTypeVal($1);
1320 CHECK_FOR_ERROR
1321 $$ = new PATypeHolder(tmp);
1322 }
1323 | '\\' EUINT64VAL { // Type UpReference
Reid Spencerf4fa5902007-02-05 10:16:10 +00001324 if ($2 > (uint64_t)~0U) GEN_ERROR("Value out of range");
Chris Lattner30c89792001-09-07 16:35:17 +00001325 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
Chris Lattner3b073862004-02-09 03:19:29 +00001326 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Reid Spencer08de34b2006-12-03 05:45:44 +00001327 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +00001328 UR_OUT("New Upreference!\n");
Reid Spencer61c83e02006-08-18 08:43:06 +00001329 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001330 }
Reid Spencer3cb4dda2007-04-28 14:13:42 +00001331 | Types '(' ArgTypeListI ')' {
Chris Lattner9232b992003-04-22 18:42:41 +00001332 std::vector<const Type*> Params;
Reid Spencer5694b6e2007-04-09 06:17:21 +00001333 unsigned index = 1;
1334 TypeWithAttrsList::iterator I = $3->begin(), E = $3->end();
1335 for (; I != E; ++I, ++index) {
Reid Spencer2c9df212007-03-20 01:13:00 +00001336 const Type *Ty = I->Ty->get();
Reid Spencer2c9df212007-03-20 01:13:00 +00001337 Params.push_back(Ty);
Reid Spencere1553cc2006-12-31 05:40:12 +00001338 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001339 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1340 if (isVarArg) Params.pop_back();
1341
Reid Spencer3cb4dda2007-04-28 14:13:42 +00001342 FunctionType *FT = FunctionType::get(*$1, Params, isVarArg);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001343 delete $3; // Delete the argument list
Reid Spencere1553cc2006-12-31 05:40:12 +00001344 delete $1; // Delete the return type handle
1345 $$ = new PATypeHolder(HandleUpRefs(FT));
Reid Spencer61c83e02006-08-18 08:43:06 +00001346 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001347 }
Reid Spencer3cb4dda2007-04-28 14:13:42 +00001348 | VOID '(' ArgTypeListI ')' {
Reid Spencere1553cc2006-12-31 05:40:12 +00001349 std::vector<const Type*> Params;
Reid Spencer5694b6e2007-04-09 06:17:21 +00001350 TypeWithAttrsList::iterator I = $3->begin(), E = $3->end();
1351 unsigned index = 1;
1352 for ( ; I != E; ++I, ++index) {
Reid Spencer2c9df212007-03-20 01:13:00 +00001353 const Type* Ty = I->Ty->get();
Reid Spencer2c9df212007-03-20 01:13:00 +00001354 Params.push_back(Ty);
Reid Spencere1553cc2006-12-31 05:40:12 +00001355 }
1356 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1357 if (isVarArg) Params.pop_back();
1358
Reid Spencer3cb4dda2007-04-28 14:13:42 +00001359 FunctionType *FT = FunctionType::get($1, Params, isVarArg);
Reid Spencer2c261782007-01-05 17:06:19 +00001360 delete $3; // Delete the argument list
Reid Spencere1553cc2006-12-31 05:40:12 +00001361 $$ = new PATypeHolder(HandleUpRefs(FT));
1362 CHECK_FOR_ERROR
1363 }
1364
1365 | '[' EUINT64VAL 'x' Types ']' { // Sized array type?
Reid Spencer08de34b2006-12-03 05:45:44 +00001366 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
1367 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00001368 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001369 }
Reid Spencerac9dcb92007-02-15 03:39:18 +00001370 | '<' EUINT64VAL 'x' Types '>' { // Vector type?
Reid Spencer08de34b2006-12-03 05:45:44 +00001371 const llvm::Type* ElemTy = $4->get();
1372 if ((unsigned)$2 != $2)
1373 GEN_ERROR("Unsigned result not equal to signed result");
Chris Lattner42a75512007-01-15 02:27:26 +00001374 if (!ElemTy->isFloatingPoint() && !ElemTy->isInteger())
Reid Spencer9d6565a2007-02-15 02:26:10 +00001375 GEN_ERROR("Element type of a VectorType must be primitive");
Reid Spencer08de34b2006-12-03 05:45:44 +00001376 if (!isPowerOf2_32($2))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001377 GEN_ERROR("Vector length should be a power of 2");
Reid Spencer9d6565a2007-02-15 02:26:10 +00001378 $$ = new PATypeHolder(HandleUpRefs(VectorType::get(*$4, (unsigned)$2)));
Reid Spencer08de34b2006-12-03 05:45:44 +00001379 delete $4;
1380 CHECK_FOR_ERROR
Brian Gaeke715c90b2004-08-20 06:00:58 +00001381 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001382 | '{' TypeListI '}' { // Structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001383 std::vector<const Type*> Elements;
Reid Spencer08de34b2006-12-03 05:45:44 +00001384 for (std::list<llvm::PATypeHolder>::iterator I = $2->begin(),
Chris Lattnera08976b2005-02-22 23:13:58 +00001385 E = $2->end(); I != E; ++I)
Reid Spencer08de34b2006-12-03 05:45:44 +00001386 Elements.push_back(*I);
Misha Brukman5a2a3822005-05-10 22:02:28 +00001387
Reid Spencer08de34b2006-12-03 05:45:44 +00001388 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001389 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001390 CHECK_FOR_ERROR
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001391 }
1392 | '{' '}' { // Empty structure type?
Reid Spencer08de34b2006-12-03 05:45:44 +00001393 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Reid Spencer61c83e02006-08-18 08:43:06 +00001394 CHECK_FOR_ERROR
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001395 }
Andrew Lenharth38ecbf12006-12-08 18:06:16 +00001396 | '<' '{' TypeListI '}' '>' {
1397 std::vector<const Type*> Elements;
1398 for (std::list<llvm::PATypeHolder>::iterator I = $3->begin(),
1399 E = $3->end(); I != E; ++I)
1400 Elements.push_back(*I);
1401
1402 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements, true)));
1403 delete $3;
1404 CHECK_FOR_ERROR
1405 }
1406 | '<' '{' '}' '>' { // Empty structure type?
1407 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>(), true));
1408 CHECK_FOR_ERROR
1409 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001410 ;
1411
1412ArgType
Reid Spencer3cb4dda2007-04-28 14:13:42 +00001413 : Types {
Reid Spencere1553cc2006-12-31 05:40:12 +00001414 $$.Ty = $1;
Reid Spencer3cb4dda2007-04-28 14:13:42 +00001415 $$.Attrs = ParamAttr::None;
Reid Spencere1553cc2006-12-31 05:40:12 +00001416 }
1417 ;
1418
Reid Spencer2c261782007-01-05 17:06:19 +00001419ResultTypes
1420 : Types {
Reid Spencere1553cc2006-12-31 05:40:12 +00001421 if (!UpRefs.empty())
1422 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1423 if (!(*$1)->isFirstClassType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001424 GEN_ERROR("LLVM functions cannot return aggregate types");
Reid Spencer2c261782007-01-05 17:06:19 +00001425 $$ = $1;
Reid Spencere1553cc2006-12-31 05:40:12 +00001426 }
Reid Spencer2c261782007-01-05 17:06:19 +00001427 | VOID {
1428 $$ = new PATypeHolder(Type::VoidTy);
Reid Spencere1553cc2006-12-31 05:40:12 +00001429 }
1430 ;
1431
1432ArgTypeList : ArgType {
1433 $$ = new TypeWithAttrsList();
1434 $$->push_back($1);
1435 CHECK_FOR_ERROR
1436 }
1437 | ArgTypeList ',' ArgType {
1438 ($$=$1)->push_back($3);
1439 CHECK_FOR_ERROR
1440 }
1441 ;
1442
1443ArgTypeListI
1444 : ArgTypeList
1445 | ArgTypeList ',' DOTDOTDOT {
1446 $$=$1;
Reid Spencer18da0722007-04-11 02:44:20 +00001447 TypeWithAttrs TWA; TWA.Attrs = ParamAttr::None;
Reid Spencere1553cc2006-12-31 05:40:12 +00001448 TWA.Ty = new PATypeHolder(Type::VoidTy);
1449 $$->push_back(TWA);
1450 CHECK_FOR_ERROR
1451 }
1452 | DOTDOTDOT {
1453 $$ = new TypeWithAttrsList;
Reid Spencer18da0722007-04-11 02:44:20 +00001454 TypeWithAttrs TWA; TWA.Attrs = ParamAttr::None;
Reid Spencere1553cc2006-12-31 05:40:12 +00001455 TWA.Ty = new PATypeHolder(Type::VoidTy);
1456 $$->push_back(TWA);
1457 CHECK_FOR_ERROR
1458 }
1459 | /*empty*/ {
1460 $$ = new TypeWithAttrsList();
Reid Spencer61c83e02006-08-18 08:43:06 +00001461 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001462 };
Chris Lattner30c89792001-09-07 16:35:17 +00001463
Chris Lattner7e708292002-06-25 16:13:24 +00001464// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +00001465// declaration type lists
1466//
Reid Spencere1553cc2006-12-31 05:40:12 +00001467TypeListI : Types {
Reid Spencer08de34b2006-12-03 05:45:44 +00001468 $$ = new std::list<PATypeHolder>();
Reid Spencer2c9df212007-03-20 01:13:00 +00001469 $$->push_back(*$1);
1470 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001471 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001472 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001473 | TypeListI ',' Types {
Reid Spencer2c9df212007-03-20 01:13:00 +00001474 ($$=$1)->push_back(*$3);
1475 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001476 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001477 };
Chris Lattner30c89792001-09-07 16:35:17 +00001478
Chris Lattnere98dda62001-07-14 06:10:16 +00001479// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +00001480// production is used ONLY to represent constants that show up AFTER a 'const',
1481// 'constant' or 'global' token at global scope. Constants that can be inlined
1482// into other expressions (such as integers and constexprs) are handled by the
1483// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +00001484//
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001485ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencere1553cc2006-12-31 05:40:12 +00001486 if (!UpRefs.empty())
1487 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001488 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001489 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001490 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001491 (*$1)->getDescription() + "'");
Chris Lattner30c89792001-09-07 16:35:17 +00001492 const Type *ETy = ATy->getElementType();
1493 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +00001494
Chris Lattner30c89792001-09-07 16:35:17 +00001495 // Verify that we have the correct size...
1496 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001497 GEN_ERROR("Type mismatch: constant sized array initialized with " +
Reid Spencer3271ed52004-07-18 00:08:11 +00001498 utostr($3->size()) + " arguments, but has size of " +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001499 itostr(NumElements) + "");
Chris Lattner00950542001-06-06 20:29:01 +00001500
Chris Lattner30c89792001-09-07 16:35:17 +00001501 // Verify all elements are correct type!
1502 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencer08de34b2006-12-03 05:45:44 +00001503 if (ETy != (*$3)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001504 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Reid Spencer3271ed52004-07-18 00:08:11 +00001505 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencer08de34b2006-12-03 05:45:44 +00001506 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +00001507 }
1508
Reid Spencer08de34b2006-12-03 05:45:44 +00001509 $$ = ConstantArray::get(ATy, *$3);
1510 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001511 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001512 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001513 | Types '[' ']' {
Reid Spencere1553cc2006-12-31 05:40:12 +00001514 if (!UpRefs.empty())
1515 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001516 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001517 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001518 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001519 (*$1)->getDescription() + "'");
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001520
1521 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +00001522 if (NumElements != -1 && NumElements != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001523 GEN_ERROR("Type mismatch: constant sized array initialized with 0"
Reid Spencerf4fa5902007-02-05 10:16:10 +00001524 " arguments, but has size of " + itostr(NumElements) +"");
Reid Spencer08de34b2006-12-03 05:45:44 +00001525 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
1526 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001527 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001528 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001529 | Types 'c' STRINGCONSTANT {
Reid Spencere1553cc2006-12-31 05:40:12 +00001530 if (!UpRefs.empty())
1531 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001532 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001533 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001534 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001535 (*$1)->getDescription() + "'");
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001536
Chris Lattner30c89792001-09-07 16:35:17 +00001537 int NumElements = ATy->getNumElements();
1538 const Type *ETy = ATy->getElementType();
1539 char *EndStr = UnEscapeLexed($3, true);
1540 if (NumElements != -1 && NumElements != (EndStr-$3))
Reid Spencer61c83e02006-08-18 08:43:06 +00001541 GEN_ERROR("Can't build string constant of size " +
Reid Spencer3271ed52004-07-18 00:08:11 +00001542 itostr((int)(EndStr-$3)) +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001543 " when array has size " + itostr(NumElements) + "");
Chris Lattner9232b992003-04-22 18:42:41 +00001544 std::vector<Constant*> Vals;
Reid Spencere1553cc2006-12-31 05:40:12 +00001545 if (ETy == Type::Int8Ty) {
Chris Lattneree454772006-01-23 23:05:15 +00001546 for (unsigned char *C = (unsigned char *)$3;
Reid Spencere1553cc2006-12-31 05:40:12 +00001547 C != (unsigned char*)EndStr; ++C)
1548 Vals.push_back(ConstantInt::get(ETy, *C));
Chris Lattner93750fa2001-07-28 17:48:55 +00001549 } else {
Chris Lattner30c89792001-09-07 16:35:17 +00001550 free($3);
Reid Spencerf4fa5902007-02-05 10:16:10 +00001551 GEN_ERROR("Cannot build string arrays of non byte sized elements");
Chris Lattner93750fa2001-07-28 17:48:55 +00001552 }
Chris Lattner30c89792001-09-07 16:35:17 +00001553 free($3);
Reid Spencer08de34b2006-12-03 05:45:44 +00001554 $$ = ConstantArray::get(ATy, Vals);
1555 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001556 CHECK_FOR_ERROR
Chris Lattner93750fa2001-07-28 17:48:55 +00001557 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00001558 | Types '<' ConstVector '>' { // Nonempty unsized arr
Reid Spencere1553cc2006-12-31 05:40:12 +00001559 if (!UpRefs.empty())
1560 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer9d6565a2007-02-15 02:26:10 +00001561 const VectorType *PTy = dyn_cast<VectorType>($1->get());
Brian Gaeke715c90b2004-08-20 06:00:58 +00001562 if (PTy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001563 GEN_ERROR("Cannot make packed constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001564 (*$1)->getDescription() + "'");
Brian Gaeke715c90b2004-08-20 06:00:58 +00001565 const Type *ETy = PTy->getElementType();
1566 int NumElements = PTy->getNumElements();
1567
1568 // Verify that we have the correct size...
1569 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001570 GEN_ERROR("Type mismatch: constant sized packed initialized with " +
Brian Gaeke715c90b2004-08-20 06:00:58 +00001571 utostr($3->size()) + " arguments, but has size of " +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001572 itostr(NumElements) + "");
Brian Gaeke715c90b2004-08-20 06:00:58 +00001573
1574 // Verify all elements are correct type!
1575 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencer08de34b2006-12-03 05:45:44 +00001576 if (ETy != (*$3)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001577 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Brian Gaeke715c90b2004-08-20 06:00:58 +00001578 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencer08de34b2006-12-03 05:45:44 +00001579 (*$3)[i]->getType()->getDescription() + "'.");
Brian Gaeke715c90b2004-08-20 06:00:58 +00001580 }
1581
Reid Spencer9d6565a2007-02-15 02:26:10 +00001582 $$ = ConstantVector::get(PTy, *$3);
Reid Spencer08de34b2006-12-03 05:45:44 +00001583 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001584 CHECK_FOR_ERROR
Brian Gaeke715c90b2004-08-20 06:00:58 +00001585 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001586 | Types '{' ConstVector '}' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001587 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001588 if (STy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001589 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001590 (*$1)->getDescription() + "'");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001591
Chris Lattnerc6212f12003-05-21 16:06:56 +00001592 if ($3->size() != STy->getNumContainedTypes())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001593 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattnerc6212f12003-05-21 16:06:56 +00001594
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001595 // Check to ensure that constants are compatible with the type initializer!
1596 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Reid Spencer08de34b2006-12-03 05:45:44 +00001597 if ((*$3)[i]->getType() != STy->getElementType(i))
Reid Spencer61c83e02006-08-18 08:43:06 +00001598 GEN_ERROR("Expected type '" +
Chris Lattnerd21cd802004-02-09 04:37:31 +00001599 STy->getElementType(i)->getDescription() +
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001600 "' for element #" + utostr(i) +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001601 " of structure initializer");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001602
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001603 // Check to ensure that Type is not packed
1604 if (STy->isPacked())
Chris Lattner4989b842007-04-26 05:30:35 +00001605 GEN_ERROR("Unpacked Initializer to vector type '" +
1606 STy->getDescription() + "'");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001607
Reid Spencer08de34b2006-12-03 05:45:44 +00001608 $$ = ConstantStruct::get(STy, *$3);
1609 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001610 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001611 }
Chris Lattnerc6212f12003-05-21 16:06:56 +00001612 | Types '{' '}' {
Reid Spencere1553cc2006-12-31 05:40:12 +00001613 if (!UpRefs.empty())
1614 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001615 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerc6212f12003-05-21 16:06:56 +00001616 if (STy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001617 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001618 (*$1)->getDescription() + "'");
Chris Lattnerc6212f12003-05-21 16:06:56 +00001619
1620 if (STy->getNumContainedTypes() != 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001621 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattnerc6212f12003-05-21 16:06:56 +00001622
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001623 // Check to ensure that Type is not packed
1624 if (STy->isPacked())
Chris Lattner4989b842007-04-26 05:30:35 +00001625 GEN_ERROR("Unpacked Initializer to vector type '" +
1626 STy->getDescription() + "'");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001627
1628 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1629 delete $1;
1630 CHECK_FOR_ERROR
1631 }
1632 | Types '<' '{' ConstVector '}' '>' {
1633 const StructType *STy = dyn_cast<StructType>($1->get());
1634 if (STy == 0)
1635 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001636 (*$1)->getDescription() + "'");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001637
1638 if ($4->size() != STy->getNumContainedTypes())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001639 GEN_ERROR("Illegal number of initializers for structure type");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001640
1641 // Check to ensure that constants are compatible with the type initializer!
1642 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1643 if ((*$4)[i]->getType() != STy->getElementType(i))
1644 GEN_ERROR("Expected type '" +
1645 STy->getElementType(i)->getDescription() +
1646 "' for element #" + utostr(i) +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001647 " of structure initializer");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001648
1649 // Check to ensure that Type is packed
1650 if (!STy->isPacked())
Reid Spencerac9dcb92007-02-15 03:39:18 +00001651 GEN_ERROR("Vector initializer to non-vector type '" +
1652 STy->getDescription() + "'");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001653
1654 $$ = ConstantStruct::get(STy, *$4);
1655 delete $1; delete $4;
1656 CHECK_FOR_ERROR
1657 }
1658 | Types '<' '{' '}' '>' {
1659 if (!UpRefs.empty())
1660 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1661 const StructType *STy = dyn_cast<StructType>($1->get());
1662 if (STy == 0)
1663 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001664 (*$1)->getDescription() + "'");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001665
1666 if (STy->getNumContainedTypes() != 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001667 GEN_ERROR("Illegal number of initializers for structure type");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001668
1669 // Check to ensure that Type is packed
1670 if (!STy->isPacked())
Reid Spencerac9dcb92007-02-15 03:39:18 +00001671 GEN_ERROR("Vector initializer to non-vector type '" +
1672 STy->getDescription() + "'");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001673
Reid Spencer08de34b2006-12-03 05:45:44 +00001674 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1675 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001676 CHECK_FOR_ERROR
Chris Lattnerc6212f12003-05-21 16:06:56 +00001677 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001678 | Types NULL_TOK {
Reid Spencere1553cc2006-12-31 05:40:12 +00001679 if (!UpRefs.empty())
1680 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001681 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001682 if (PTy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001683 GEN_ERROR("Cannot make null pointer constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001684 (*$1)->getDescription() + "'");
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001685
Reid Spencer08de34b2006-12-03 05:45:44 +00001686 $$ = ConstantPointerNull::get(PTy);
1687 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001688 CHECK_FOR_ERROR
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001689 }
Chris Lattner16710e92004-10-16 18:17:13 +00001690 | Types UNDEF {
Reid Spencere1553cc2006-12-31 05:40:12 +00001691 if (!UpRefs.empty())
1692 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001693 $$ = UndefValue::get($1->get());
1694 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001695 CHECK_FOR_ERROR
Chris Lattner16710e92004-10-16 18:17:13 +00001696 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001697 | Types SymbolicValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00001698 if (!UpRefs.empty())
1699 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001700 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001701 if (Ty == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001702 GEN_ERROR("Global const reference must be a pointer type");
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001703
Chris Lattner3101c252002-08-15 17:58:33 +00001704 // ConstExprs can exist in the body of a function, thus creating
Reid Spencer3271ed52004-07-18 00:08:11 +00001705 // GlobalValues whenever they refer to a variable. Because we are in
Reid Spencer186a43f2007-03-19 18:39:36 +00001706 // the context of a function, getExistingVal will search the functions
Chris Lattner3101c252002-08-15 17:58:33 +00001707 // symbol table instead of the module symbol table for the global symbol,
1708 // which throws things all off. To get around this, we just tell
Reid Spencer186a43f2007-03-19 18:39:36 +00001709 // getExistingVal that we are at global scope here.
Chris Lattner3101c252002-08-15 17:58:33 +00001710 //
Chris Lattner394f2eb2003-10-16 20:12:13 +00001711 Function *SavedCurFn = CurFun.CurrentFunction;
1712 CurFun.CurrentFunction = 0;
Chris Lattner3101c252002-08-15 17:58:33 +00001713
Reid Spencer186a43f2007-03-19 18:39:36 +00001714 Value *V = getExistingVal(Ty, $2);
Reid Spencer5b7e7532006-09-28 19:28:24 +00001715 CHECK_FOR_ERROR
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001716
Chris Lattner394f2eb2003-10-16 20:12:13 +00001717 CurFun.CurrentFunction = SavedCurFn;
Chris Lattner3101c252002-08-15 17:58:33 +00001718
Chris Lattner2079fde2001-10-13 06:41:08 +00001719 // If this is an initializer for a constant pointer, which is referencing a
1720 // (currently) undefined variable, create a stub now that shall be replaced
1721 // in the future with the right type of variable.
1722 //
1723 if (V == 0) {
Reid Spencer1755a9a2007-02-05 17:01:20 +00001724 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001725 const PointerType *PT = cast<PointerType>(Ty);
1726
1727 // First check to see if the forward references value is already created!
1728 PerModuleInfo::GlobalRefsType::iterator I =
Reid Spencer3271ed52004-07-18 00:08:11 +00001729 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Chris Lattner2079fde2001-10-13 06:41:08 +00001730
1731 if (I != CurModule.GlobalRefs.end()) {
Reid Spencer3271ed52004-07-18 00:08:11 +00001732 V = I->second; // Placeholder already exists, use it...
Chris Lattnera989b232003-12-23 20:05:15 +00001733 $2.destroy();
Chris Lattner2079fde2001-10-13 06:41:08 +00001734 } else {
Chris Lattner8a327842004-07-14 21:44:00 +00001735 std::string Name;
Reid Spencerb2d17862007-01-26 08:04:51 +00001736 if ($2.Type == ValID::GlobalName)
1737 Name = $2.Name;
1738 else if ($2.Type != ValID::GlobalID)
1739 GEN_ERROR("Invalid reference to global");
Chris Lattner8a327842004-07-14 21:44:00 +00001740
Reid Spencer3271ed52004-07-18 00:08:11 +00001741 // Create the forward referenced global.
Chris Lattnera09000d2004-07-14 23:03:46 +00001742 GlobalValue *GV;
1743 if (const FunctionType *FTy =
1744 dyn_cast<FunctionType>(PT->getElementType())) {
Chris Lattner4989b842007-04-26 05:30:35 +00001745 GV = new Function(FTy, GlobalValue::ExternalWeakLinkage, Name,
Chris Lattnera09000d2004-07-14 23:03:46 +00001746 CurModule.CurrentModule);
1747 } else {
Reid Spencer3271ed52004-07-18 00:08:11 +00001748 GV = new GlobalVariable(PT->getElementType(), false,
Chris Lattner4989b842007-04-26 05:30:35 +00001749 GlobalValue::ExternalWeakLinkage, 0,
Chris Lattnera09000d2004-07-14 23:03:46 +00001750 Name, CurModule.CurrentModule);
1751 }
Chris Lattner8a327842004-07-14 21:44:00 +00001752
Reid Spencer3271ed52004-07-18 00:08:11 +00001753 // Keep track of the fact that we have a forward ref to recycle it
1754 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1755 V = GV;
Chris Lattner2079fde2001-10-13 06:41:08 +00001756 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001757 }
1758
Reid Spencer08de34b2006-12-03 05:45:44 +00001759 $$ = cast<GlobalValue>(V);
1760 delete $1; // Free the type handle
Reid Spencer61c83e02006-08-18 08:43:06 +00001761 CHECK_FOR_ERROR
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001762 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001763 | Types ConstExpr {
Reid Spencere1553cc2006-12-31 05:40:12 +00001764 if (!UpRefs.empty())
1765 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001766 if ($1->get() != $2->getType())
Reid Spencerb4fdfdb2007-01-04 00:05:48 +00001767 GEN_ERROR("Mismatched types for constant expression: " +
1768 (*$1)->getDescription() + " and " + $2->getType()->getDescription());
Chris Lattnerd78700d2002-08-16 21:14:40 +00001769 $$ = $2;
Reid Spencer08de34b2006-12-03 05:45:44 +00001770 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001771 CHECK_FOR_ERROR
Chris Lattnerf4600482003-06-28 20:01:34 +00001772 }
1773 | Types ZEROINITIALIZER {
Reid Spencere1553cc2006-12-31 05:40:12 +00001774 if (!UpRefs.empty())
1775 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001776 const Type *Ty = $1->get();
Chris Lattner78915932004-11-28 16:45:45 +00001777 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001778 GEN_ERROR("Cannot create a null initialized value of this type");
Reid Spencer08de34b2006-12-03 05:45:44 +00001779 $$ = Constant::getNullValue(Ty);
1780 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001781 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00001782 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001783 | IntType ESINT64VAL { // integral constants
Reid Spencer9ffad0a2006-12-20 17:20:09 +00001784 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001785 GEN_ERROR("Constant value doesn't fit in type");
Reid Spencereac65742007-03-19 20:40:22 +00001786 $$ = ConstantInt::get($1, $2, true);
Reid Spencerf2310042007-02-28 02:23:44 +00001787 CHECK_FOR_ERROR
1788 }
1789 | IntType ESAPINTVAL { // arbitrary precision integer constants
1790 uint32_t BitWidth = cast<IntegerType>($1)->getBitWidth();
1791 if ($2->getBitWidth() > BitWidth) {
1792 GEN_ERROR("Constant value does not fit in type");
Reid Spenceraf1aacd2007-03-01 19:32:01 +00001793 }
1794 $2->sextOrTrunc(BitWidth);
1795 $$ = ConstantInt::get(*$2);
Reid Spencerf2310042007-02-28 02:23:44 +00001796 delete $2;
Reid Spencer9ffad0a2006-12-20 17:20:09 +00001797 CHECK_FOR_ERROR
1798 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001799 | IntType EUINT64VAL { // integral constants
Reid Spencer9ffad0a2006-12-20 17:20:09 +00001800 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001801 GEN_ERROR("Constant value doesn't fit in type");
Reid Spencereac65742007-03-19 20:40:22 +00001802 $$ = ConstantInt::get($1, $2, false);
Reid Spencerf2310042007-02-28 02:23:44 +00001803 CHECK_FOR_ERROR
1804 }
1805 | IntType EUAPINTVAL { // arbitrary precision integer constants
1806 uint32_t BitWidth = cast<IntegerType>($1)->getBitWidth();
1807 if ($2->getBitWidth() > BitWidth) {
1808 GEN_ERROR("Constant value does not fit in type");
Reid Spenceraf1aacd2007-03-01 19:32:01 +00001809 }
1810 $2->zextOrTrunc(BitWidth);
1811 $$ = ConstantInt::get(*$2);
Reid Spencerf2310042007-02-28 02:23:44 +00001812 delete $2;
Reid Spencer9ffad0a2006-12-20 17:20:09 +00001813 CHECK_FOR_ERROR
1814 }
Reid Spencer8088e9d2007-01-13 05:00:20 +00001815 | INTTYPE TRUETOK { // Boolean constants
1816 assert(cast<IntegerType>($1)->getBitWidth() == 1 && "Not Bool?");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001817 $$ = ConstantInt::getTrue();
Reid Spencer61c83e02006-08-18 08:43:06 +00001818 CHECK_FOR_ERROR
Chris Lattnerd05e3592002-08-15 18:17:28 +00001819 }
Reid Spencer8088e9d2007-01-13 05:00:20 +00001820 | INTTYPE FALSETOK { // Boolean constants
1821 assert(cast<IntegerType>($1)->getBitWidth() == 1 && "Not Bool?");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001822 $$ = ConstantInt::getFalse();
Reid Spencer61c83e02006-08-18 08:43:06 +00001823 CHECK_FOR_ERROR
Chris Lattnerd05e3592002-08-15 18:17:28 +00001824 }
1825 | FPType FPVAL { // Float & Double constants
Reid Spencer08de34b2006-12-03 05:45:44 +00001826 if (!ConstantFP::isValueValidForType($1, $2))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001827 GEN_ERROR("Floating point constant invalid for type");
Reid Spencer08de34b2006-12-03 05:45:44 +00001828 $$ = ConstantFP::get($1, $2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001829 CHECK_FOR_ERROR
Chris Lattnerd05e3592002-08-15 18:17:28 +00001830 };
1831
Chris Lattner00950542001-06-06 20:29:01 +00001832
Reid Spencer3da59db2006-11-27 01:05:10 +00001833ConstExpr: CastOps '(' ConstVal TO Types ')' {
Reid Spencere1553cc2006-12-31 05:40:12 +00001834 if (!UpRefs.empty())
1835 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001836 Constant *Val = $3;
Reid Spencer93947c32007-01-17 02:47:33 +00001837 const Type *DestTy = $5->get();
1838 if (!CastInst::castIsValid($1, $3, DestTy))
1839 GEN_ERROR("invalid cast opcode for cast from '" +
1840 Val->getType()->getDescription() + "' to '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001841 DestTy->getDescription() + "'");
Reid Spencer93947c32007-01-17 02:47:33 +00001842 $$ = ConstantExpr::getCast($1, $3, DestTy);
Reid Spencer08de34b2006-12-03 05:45:44 +00001843 delete $5;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001844 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001845 | GETELEMENTPTR '(' ConstVal IndexList ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001846 if (!isa<PointerType>($3->getType()))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001847 GEN_ERROR("GetElementPtr requires a pointer operand");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001848
Reid Spencer08de34b2006-12-03 05:45:44 +00001849 const Type *IdxTy =
Chris Lattnerc856c7a2007-02-13 00:57:40 +00001850 GetElementPtrInst::getIndexedType($3->getType(), &(*$4)[0], $4->size(),
1851 true);
Reid Spencer08de34b2006-12-03 05:45:44 +00001852 if (!IdxTy)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001853 GEN_ERROR("Index list invalid for constant getelementptr");
Reid Spencer08de34b2006-12-03 05:45:44 +00001854
Chris Lattnere0135402007-01-31 04:43:46 +00001855 SmallVector<Constant*, 8> IdxVec;
Reid Spencer08de34b2006-12-03 05:45:44 +00001856 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1857 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001858 IdxVec.push_back(C);
1859 else
Reid Spencerf4fa5902007-02-05 10:16:10 +00001860 GEN_ERROR("Indices to constant getelementptr must be constants");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001861
Chris Lattnerd78700d2002-08-16 21:14:40 +00001862 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001863
Chris Lattnere0135402007-01-31 04:43:46 +00001864 $$ = ConstantExpr::getGetElementPtr($3, &IdxVec[0], IdxVec.size());
Reid Spencer61c83e02006-08-18 08:43:06 +00001865 CHECK_FOR_ERROR
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001866 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001867 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer4fe16d62007-01-11 18:21:29 +00001868 if ($3->getType() != Type::Int1Ty)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001869 GEN_ERROR("Select condition must be of boolean type");
Reid Spencer08de34b2006-12-03 05:45:44 +00001870 if ($5->getType() != $7->getType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001871 GEN_ERROR("Select operand types must match");
Reid Spencer08de34b2006-12-03 05:45:44 +00001872 $$ = ConstantExpr::getSelect($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001873 CHECK_FOR_ERROR
Chris Lattner76f0ff32004-03-12 05:51:36 +00001874 }
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001875 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001876 if ($3->getType() != $5->getType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001877 GEN_ERROR("Binary operator types must match");
Reid Spencer1628cec2006-10-26 06:15:43 +00001878 CHECK_FOR_ERROR;
Reid Spencerc6e956e2006-12-05 19:15:41 +00001879 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001880 }
1881 | LogicalOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001882 if ($3->getType() != $5->getType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001883 GEN_ERROR("Logical operator types must match");
Chris Lattner42a75512007-01-15 02:27:26 +00001884 if (!$3->getType()->isInteger()) {
Reid Spencer9d6565a2007-02-15 02:26:10 +00001885 if (Instruction::isShift($1) || !isa<VectorType>($3->getType()) ||
1886 !cast<VectorType>($3->getType())->getElementType()->isInteger())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001887 GEN_ERROR("Logical operator requires integral operands");
Chris Lattner0a017832005-12-21 18:31:29 +00001888 }
Reid Spencer08de34b2006-12-03 05:45:44 +00001889 $$ = ConstantExpr::get($1, $3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001890 CHECK_FOR_ERROR
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001891 }
Reid Spencer763ed5e2006-12-04 05:20:06 +00001892 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
1893 if ($4->getType() != $6->getType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001894 GEN_ERROR("icmp operand types must match");
Reid Spencer763ed5e2006-12-04 05:20:06 +00001895 $$ = ConstantExpr::getICmp($2, $4, $6);
Reid Spencer08de34b2006-12-03 05:45:44 +00001896 }
Reid Spencer763ed5e2006-12-04 05:20:06 +00001897 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
1898 if ($4->getType() != $6->getType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001899 GEN_ERROR("fcmp operand types must match");
Reid Spencer763ed5e2006-12-04 05:20:06 +00001900 $$ = ConstantExpr::getFCmp($2, $4, $6);
Reid Spencer08de34b2006-12-03 05:45:44 +00001901 }
Robert Bocchino9c62b562006-01-10 19:04:32 +00001902 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001903 if (!ExtractElementInst::isValidOperands($3, $5))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001904 GEN_ERROR("Invalid extractelement operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00001905 $$ = ConstantExpr::getExtractElement($3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001906 CHECK_FOR_ERROR
Chris Lattnerca73cd82006-04-08 03:53:34 +00001907 }
1908 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001909 if (!InsertElementInst::isValidOperands($3, $5, $7))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001910 GEN_ERROR("Invalid insertelement operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00001911 $$ = ConstantExpr::getInsertElement($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001912 CHECK_FOR_ERROR
Chris Lattnerca73cd82006-04-08 03:53:34 +00001913 }
1914 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001915 if (!ShuffleVectorInst::isValidOperands($3, $5, $7))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001916 GEN_ERROR("Invalid shufflevector operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00001917 $$ = ConstantExpr::getShuffleVector($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001918 CHECK_FOR_ERROR
Chris Lattner699f1eb2002-08-14 17:12:33 +00001919 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001920
Chris Lattnerca73cd82006-04-08 03:53:34 +00001921
Misha Brukmanbc0e9982003-07-14 17:20:40 +00001922// ConstVector - A list of comma separated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001923ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001924 ($$ = $1)->push_back($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00001925 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001926 }
1927 | ConstVal {
Reid Spencer08de34b2006-12-03 05:45:44 +00001928 $$ = new std::vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001929 $$->push_back($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001930 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001931 };
Chris Lattner00950542001-06-06 20:29:01 +00001932
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001933
Chris Lattner1781aca2001-09-18 04:00:54 +00001934// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001935GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001936
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00001937// ThreadLocal
1938ThreadLocal : THREAD_LOCAL { $$ = true; } | { $$ = false; };
1939
Anton Korobeynikova80e1182007-04-28 13:45:00 +00001940// AliaseeRef - Match either GlobalValue or bitcast to GlobalValue.
1941AliaseeRef : ResultTypes SymbolicValueRef {
1942 const Type* VTy = $1->get();
1943 Value *V = getVal(VTy, $2);
1944 GlobalValue* Aliasee = dyn_cast<GlobalValue>(V);
1945 if (!Aliasee)
1946 GEN_ERROR("Aliases can be created only to global values");
1947
1948 $$ = Aliasee;
1949 CHECK_FOR_ERROR
1950 delete $1;
1951 }
1952 | BITCAST '(' AliaseeRef TO Types ')' {
1953 Constant *Val = $3;
1954 const Type *DestTy = $5->get();
1955 if (!CastInst::castIsValid($1, $3, DestTy))
1956 GEN_ERROR("invalid cast opcode for cast from '" +
1957 Val->getType()->getDescription() + "' to '" +
1958 DestTy->getDescription() + "'");
1959
1960 $$ = ConstantExpr::getCast($1, $3, DestTy);
1961 CHECK_FOR_ERROR
1962 delete $5;
1963 };
Chris Lattner00950542001-06-06 20:29:01 +00001964
Chris Lattner0e73ce62002-05-02 19:11:13 +00001965//===----------------------------------------------------------------------===//
1966// Rules to match Modules
1967//===----------------------------------------------------------------------===//
1968
1969// Module rule: Capture the result of parsing the whole file into a result
1970// variable...
1971//
Reid Spencerb951bc02006-12-29 20:29:48 +00001972Module
1973 : DefinitionList {
1974 $$ = ParserResult = CurModule.CurrentModule;
1975 CurModule.ModuleDone();
1976 CHECK_FOR_ERROR;
1977 }
1978 | /*empty*/ {
1979 $$ = ParserResult = CurModule.CurrentModule;
1980 CurModule.ModuleDone();
1981 CHECK_FOR_ERROR;
1982 }
1983 ;
Chris Lattner0e73ce62002-05-02 19:11:13 +00001984
Reid Spencerb951bc02006-12-29 20:29:48 +00001985DefinitionList
1986 : Definition
1987 | DefinitionList Definition
1988 ;
1989
1990Definition
Jeff Cohen361c3ef2007-01-21 19:19:31 +00001991 : DEFINE { CurFun.isDeclare = false; } Function {
Chris Lattner394f2eb2003-10-16 20:12:13 +00001992 CurFun.FunctionDone();
Reid Spencer61c83e02006-08-18 08:43:06 +00001993 CHECK_FOR_ERROR
Reid Spencerb951bc02006-12-29 20:29:48 +00001994 }
1995 | DECLARE { CurFun.isDeclare = true; } FunctionProto {
Reid Spencer61c83e02006-08-18 08:43:06 +00001996 CHECK_FOR_ERROR
Chris Lattner0e73ce62002-05-02 19:11:13 +00001997 }
Reid Spencerb951bc02006-12-29 20:29:48 +00001998 | MODULE ASM_TOK AsmBlock {
Reid Spencer61c83e02006-08-18 08:43:06 +00001999 CHECK_FOR_ERROR
Chris Lattneree454772006-01-23 23:05:15 +00002000 }
Reid Spencerb2d17862007-01-26 08:04:51 +00002001 | OptLocalAssign TYPE Types {
Reid Spencere1553cc2006-12-31 05:40:12 +00002002 if (!UpRefs.empty())
2003 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattner4a42e902001-10-22 05:56:09 +00002004 // Eagerly resolve types. This is not an optimization, this is a
2005 // requirement that is due to the fact that we could have this:
2006 //
2007 // %list = type { %list * }
2008 // %list = type { %list * } ; repeated type decl
2009 //
2010 // If types are not resolved eagerly, then the two types will not be
2011 // determined to be the same type!
2012 //
Reid Spencerb951bc02006-12-29 20:29:48 +00002013 ResolveTypeTo($1, *$3);
Chris Lattner4a42e902001-10-22 05:56:09 +00002014
Reid Spencerb951bc02006-12-29 20:29:48 +00002015 if (!setTypeName(*$3, $1) && !$1) {
Reid Spencer5b7e7532006-09-28 19:28:24 +00002016 CHECK_FOR_ERROR
Chris Lattner8c89a0a2004-07-13 08:10:10 +00002017 // If this is a named type that is not a redefinition, add it to the slot
2018 // table.
Reid Spencerb951bc02006-12-29 20:29:48 +00002019 CurModule.Types.push_back(*$3);
Chris Lattner30c89792001-09-07 16:35:17 +00002020 }
Reid Spencer08de34b2006-12-03 05:45:44 +00002021
Reid Spencerb951bc02006-12-29 20:29:48 +00002022 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002023 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00002024 }
Reid Spencerb2d17862007-01-26 08:04:51 +00002025 | OptLocalAssign TYPE VOID {
Reid Spencere1553cc2006-12-31 05:40:12 +00002026 ResolveTypeTo($1, $3);
2027
2028 if (!setTypeName($3, $1) && !$1) {
2029 CHECK_FOR_ERROR
2030 // If this is a named type that is not a redefinition, add it to the slot
2031 // table.
2032 CurModule.Types.push_back($3);
2033 }
2034 CHECK_FOR_ERROR
2035 }
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002036 | OptGlobalAssign GVVisibilityStyle ThreadLocal GlobalType ConstVal {
Reid Spencerb2d17862007-01-26 08:04:51 +00002037 /* "Externally Visible" Linkage */
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002038 if ($5 == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002039 GEN_ERROR("Global value initializer is not a constant");
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002040 CurGV = ParseGlobalVariable($1, GlobalValue::ExternalLinkage,
2041 $2, $4, $5->getType(), $5, $3);
Reid Spencerb951bc02006-12-29 20:29:48 +00002042 CHECK_FOR_ERROR
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002043 } GlobalVarAttributes {
2044 CurGV = 0;
2045 }
Chris Lattner4989b842007-04-26 05:30:35 +00002046 | OptGlobalAssign GVInternalLinkage GVVisibilityStyle ThreadLocal GlobalType
2047 ConstVal {
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002048 if ($6 == 0)
2049 GEN_ERROR("Global value initializer is not a constant");
2050 CurGV = ParseGlobalVariable($1, $2, $3, $5, $6->getType(), $6, $4);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002051 CHECK_FOR_ERROR
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002052 } GlobalVarAttributes {
2053 CurGV = 0;
2054 }
Chris Lattner4989b842007-04-26 05:30:35 +00002055 | OptGlobalAssign GVExternalLinkage GVVisibilityStyle ThreadLocal GlobalType
2056 Types {
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002057 if (!UpRefs.empty())
2058 GEN_ERROR("Invalid upreference in type: " + (*$6)->getDescription());
2059 CurGV = ParseGlobalVariable($1, $2, $3, $5, *$6, 0, $4);
2060 CHECK_FOR_ERROR
2061 delete $6;
Reid Spencer5b7e7532006-09-28 19:28:24 +00002062 } GlobalVarAttributes {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002063 CurGV = 0;
2064 CHECK_FOR_ERROR
2065 }
Anton Korobeynikova80e1182007-04-28 13:45:00 +00002066 | OptGlobalAssign GVVisibilityStyle ALIAS AliasLinkage AliaseeRef {
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00002067 std::string Name($1);
2068 if (Name.empty())
Anton Korobeynikova80e1182007-04-28 13:45:00 +00002069 GEN_ERROR("Alias name cannot be empty");
2070
2071 Constant* Aliasee = $5;
2072 if (Aliasee == 0)
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00002073 GEN_ERROR(std::string("Invalid aliasee for alias: ") + $1);
Anton Korobeynikova80e1182007-04-28 13:45:00 +00002074
2075 GlobalAlias* GA = new GlobalAlias(Aliasee->getType(), $4, Name, Aliasee,
2076 CurModule.CurrentModule);
2077 GA->setVisibility($2);
2078 InsertValue(GA, CurModule.Values);
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00002079 CHECK_FOR_ERROR
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00002080 }
Reid Spencerb951bc02006-12-29 20:29:48 +00002081 | TARGET TargetDefinition {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002082 CHECK_FOR_ERROR
2083 }
Reid Spencerb951bc02006-12-29 20:29:48 +00002084 | DEPLIBS '=' LibrariesDefinition {
Reid Spencer61c83e02006-08-18 08:43:06 +00002085 CHECK_FOR_ERROR
Chris Lattnere98dda62001-07-14 06:10:16 +00002086 }
Reid Spencerb951bc02006-12-29 20:29:48 +00002087 ;
Chris Lattner00950542001-06-06 20:29:01 +00002088
2089
Chris Lattneree454772006-01-23 23:05:15 +00002090AsmBlock : STRINGCONSTANT {
Chris Lattner66316012006-01-24 04:14:29 +00002091 const std::string &AsmSoFar = CurModule.CurrentModule->getModuleInlineAsm();
Chris Lattneree454772006-01-23 23:05:15 +00002092 char *EndStr = UnEscapeLexed($1, true);
2093 std::string NewAsm($1, EndStr);
2094 free($1);
2095
2096 if (AsmSoFar.empty())
Chris Lattner66316012006-01-24 04:14:29 +00002097 CurModule.CurrentModule->setModuleInlineAsm(NewAsm);
Chris Lattneree454772006-01-23 23:05:15 +00002098 else
Chris Lattner66316012006-01-24 04:14:29 +00002099 CurModule.CurrentModule->setModuleInlineAsm(AsmSoFar+"\n"+NewAsm);
Reid Spencer61c83e02006-08-18 08:43:06 +00002100 CHECK_FOR_ERROR
Chris Lattneree454772006-01-23 23:05:15 +00002101};
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00002102
Reid Spencerb2d17862007-01-26 08:04:51 +00002103TargetDefinition : TRIPLE '=' STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00002104 CurModule.CurrentModule->setTargetTriple($3);
2105 free($3);
John Criswell2f6a8b12006-10-24 19:09:48 +00002106 }
Chris Lattner10b27112006-10-22 06:07:41 +00002107 | DATALAYOUT '=' STRINGCONSTANT {
Owen Andersoncf7ff2b2006-10-18 02:21:12 +00002108 CurModule.CurrentModule->setDataLayout($3);
2109 free($3);
Owen Andersoncf7ff2b2006-10-18 02:21:12 +00002110 };
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00002111
Reid Spencerfeaf10e2004-07-25 21:30:51 +00002112LibrariesDefinition : '[' LibList ']';
Reid Spencer0be13e72004-07-25 17:58:28 +00002113
2114LibList : LibList ',' STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00002115 CurModule.CurrentModule->addLibrary($3);
2116 free($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002117 CHECK_FOR_ERROR
Reid Spencer0be13e72004-07-25 17:58:28 +00002118 }
2119 | STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00002120 CurModule.CurrentModule->addLibrary($1);
2121 free($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002122 CHECK_FOR_ERROR
Reid Spencer0be13e72004-07-25 17:58:28 +00002123 }
2124 | /* empty: end of list */ {
Reid Spencer61c83e02006-08-18 08:43:06 +00002125 CHECK_FOR_ERROR
Reid Spencer0be13e72004-07-25 17:58:28 +00002126 }
2127 ;
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00002128
Chris Lattner00950542001-06-06 20:29:01 +00002129//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00002130// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00002131//===----------------------------------------------------------------------===//
2132
Reid Spencerb2d17862007-01-26 08:04:51 +00002133ArgListH : ArgListH ',' Types OptParamAttrs OptLocalName {
Reid Spencere1553cc2006-12-31 05:40:12 +00002134 if (!UpRefs.empty())
2135 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
2136 if (*$3 == Type::VoidTy)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002137 GEN_ERROR("void typed arguments are invalid");
Reid Spencere1553cc2006-12-31 05:40:12 +00002138 ArgListEntry E; E.Attrs = $4; E.Ty = $3; E.Name = $5;
Chris Lattner69da5cf2002-10-13 20:57:00 +00002139 $$ = $1;
Reid Spencere1553cc2006-12-31 05:40:12 +00002140 $1->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002141 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002142 }
Reid Spencerb2d17862007-01-26 08:04:51 +00002143 | Types OptParamAttrs OptLocalName {
Reid Spencere1553cc2006-12-31 05:40:12 +00002144 if (!UpRefs.empty())
2145 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2146 if (*$1 == Type::VoidTy)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002147 GEN_ERROR("void typed arguments are invalid");
Reid Spencere1553cc2006-12-31 05:40:12 +00002148 ArgListEntry E; E.Attrs = $2; E.Ty = $1; E.Name = $3;
2149 $$ = new ArgListType;
2150 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002151 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002152 };
Chris Lattner00950542001-06-06 20:29:01 +00002153
2154ArgList : ArgListH {
2155 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002156 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002157 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00002158 | ArgListH ',' DOTDOTDOT {
2159 $$ = $1;
Reid Spencere1553cc2006-12-31 05:40:12 +00002160 struct ArgListEntry E;
2161 E.Ty = new PATypeHolder(Type::VoidTy);
2162 E.Name = 0;
Reid Spencer18da0722007-04-11 02:44:20 +00002163 E.Attrs = ParamAttr::None;
Reid Spencere1553cc2006-12-31 05:40:12 +00002164 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002165 CHECK_FOR_ERROR
Chris Lattner69da5cf2002-10-13 20:57:00 +00002166 }
2167 | DOTDOTDOT {
Reid Spencere1553cc2006-12-31 05:40:12 +00002168 $$ = new ArgListType;
2169 struct ArgListEntry E;
2170 E.Ty = new PATypeHolder(Type::VoidTy);
2171 E.Name = 0;
Reid Spencer18da0722007-04-11 02:44:20 +00002172 E.Attrs = ParamAttr::None;
Reid Spencere1553cc2006-12-31 05:40:12 +00002173 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002174 CHECK_FOR_ERROR
Chris Lattner69da5cf2002-10-13 20:57:00 +00002175 }
Chris Lattner00950542001-06-06 20:29:01 +00002176 | /* empty */ {
2177 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00002178 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002179 };
Chris Lattner00950542001-06-06 20:29:01 +00002180
Reid Spencerb2d17862007-01-26 08:04:51 +00002181FunctionHeaderH : OptCallingConv ResultTypes GlobalName '(' ArgList ')'
Reid Spencer2c261782007-01-05 17:06:19 +00002182 OptFuncAttrs OptSection OptAlign {
Chris Lattnera8e8f162005-05-06 20:27:19 +00002183 UnEscapeLexed($3);
2184 std::string FunctionName($3);
2185 free($3); // Free strdup'd memory!
Chris Lattnerdda71962001-11-26 18:54:16 +00002186
Reid Spencer98b3c5c2007-01-02 21:53:43 +00002187 // Check the function result for abstractness if this is a define. We should
2188 // have no abstract types at this point
Reid Spencer2c261782007-01-05 17:06:19 +00002189 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved($2))
2190 GEN_ERROR("Reference to abstract result: "+ $2->get()->getDescription());
Reid Spencer98b3c5c2007-01-02 21:53:43 +00002191
Chris Lattner9232b992003-04-22 18:42:41 +00002192 std::vector<const Type*> ParamTypeList;
Reid Spencer4f859aa2007-04-22 05:46:44 +00002193 ParamAttrsVector Attrs;
2194 if ($7 != ParamAttr::None) {
2195 ParamAttrsWithIndex PAWI; PAWI.index = 0; PAWI.attrs = $7;
2196 Attrs.push_back(PAWI);
2197 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00002198 if ($5) { // If there are arguments...
Reid Spencer5694b6e2007-04-09 06:17:21 +00002199 unsigned index = 1;
2200 for (ArgListType::iterator I = $5->begin(); I != $5->end(); ++I, ++index) {
Reid Spencere1553cc2006-12-31 05:40:12 +00002201 const Type* Ty = I->Ty->get();
Reid Spencer98b3c5c2007-01-02 21:53:43 +00002202 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved(I->Ty))
2203 GEN_ERROR("Reference to abstract argument: " + Ty->getDescription());
Reid Spencere1553cc2006-12-31 05:40:12 +00002204 ParamTypeList.push_back(Ty);
2205 if (Ty != Type::VoidTy)
Reid Spencer4f859aa2007-04-22 05:46:44 +00002206 if (I->Attrs != ParamAttr::None) {
2207 ParamAttrsWithIndex PAWI; PAWI.index = index; PAWI.attrs = I->Attrs;
2208 Attrs.push_back(PAWI);
2209 }
Reid Spencere1553cc2006-12-31 05:40:12 +00002210 }
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002211 }
Chris Lattner00950542001-06-06 20:29:01 +00002212
Chris Lattner2079fde2001-10-13 06:41:08 +00002213 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
2214 if (isVarArg) ParamTypeList.pop_back();
2215
Reid Spencer4f859aa2007-04-22 05:46:44 +00002216 ParamAttrsList *PAL = 0;
2217 if (!Attrs.empty())
2218 PAL = ParamAttrsList::get(Attrs);
Reid Spencer5694b6e2007-04-09 06:17:21 +00002219
Reid Spencer3cb4dda2007-04-28 14:13:42 +00002220 FunctionType *FT = FunctionType::get(*$2, ParamTypeList, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002221 const PointerType *PFT = PointerType::get(FT);
Reid Spencer2c261782007-01-05 17:06:19 +00002222 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002223
Chris Lattnera09000d2004-07-14 23:03:46 +00002224 ValID ID;
2225 if (!FunctionName.empty()) {
Reid Spencerb2d17862007-01-26 08:04:51 +00002226 ID = ValID::createGlobalName((char*)FunctionName.c_str());
Chris Lattnera09000d2004-07-14 23:03:46 +00002227 } else {
Reid Spencer186a43f2007-03-19 18:39:36 +00002228 ID = ValID::createGlobalID(CurModule.Values.size());
Chris Lattnera09000d2004-07-14 23:03:46 +00002229 }
2230
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00002231 Function *Fn = 0;
Chris Lattnera09000d2004-07-14 23:03:46 +00002232 // See if this function was forward referenced. If so, recycle the object.
Reid Spencer3cb4dda2007-04-28 14:13:42 +00002233 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, PAL, ID)) {
Chris Lattnera09000d2004-07-14 23:03:46 +00002234 // Move the function to the end of the list, from whereever it was
2235 // previously inserted.
2236 Fn = cast<Function>(FWRef);
2237 CurModule.CurrentModule->getFunctionList().remove(Fn);
2238 CurModule.CurrentModule->getFunctionList().push_back(Fn);
2239 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
Reid Spenceref9b9a72007-02-05 20:47:22 +00002240 (Fn = CurModule.CurrentModule->getFunction(FunctionName))) {
Reid Spencer3cb4dda2007-04-28 14:13:42 +00002241 if (Fn->getFunctionType() != FT || PAL != Fn->getParamAttrs()) {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002242 // The existing function doesn't have the same type. This is an overload
2243 // error.
2244 GEN_ERROR("Overload of function '" + FunctionName + "' not permitted.");
2245 } else if (!CurFun.isDeclare && !Fn->isDeclaration()) {
2246 // Neither the existing or the current function is a declaration and they
2247 // have the same name and same type. Clearly this is a redefinition.
Reid Spencerf4fa5902007-02-05 10:16:10 +00002248 GEN_ERROR("Redefinition of function '" + FunctionName + "'");
Reid Spenceref9b9a72007-02-05 20:47:22 +00002249 } if (Fn->isDeclaration()) {
2250 // Make sure to strip off any argument names so we can't get conflicts.
Chris Lattnere4d5c442005-03-15 04:54:21 +00002251 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
Chris Lattnera09000d2004-07-14 23:03:46 +00002252 AI != AE; ++AI)
2253 AI->setName("");
Reid Spenceref9b9a72007-02-05 20:47:22 +00002254 }
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00002255 } else { // Not already defined?
Chris Lattner4989b842007-04-26 05:30:35 +00002256 Fn = new Function(FT, GlobalValue::ExternalWeakLinkage, FunctionName,
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00002257 CurModule.CurrentModule);
Reid Spencer3cb4dda2007-04-28 14:13:42 +00002258 Fn->setParamAttrs(PAL);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002259
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00002260 InsertValue(Fn, CurModule.Values);
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00002261 }
Chris Lattner00950542001-06-06 20:29:01 +00002262
Chris Lattner394f2eb2003-10-16 20:12:13 +00002263 CurFun.FunctionStart(Fn);
Anton Korobeynikov93c2b372006-09-17 13:06:18 +00002264
2265 if (CurFun.isDeclare) {
2266 // If we have declaration, always overwrite linkage. This will allow us to
2267 // correctly handle cases, when pointer to function is passed as argument to
2268 // another function.
2269 Fn->setLinkage(CurFun.Linkage);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002270 Fn->setVisibility(CurFun.Visibility);
Anton Korobeynikov93c2b372006-09-17 13:06:18 +00002271 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00002272 Fn->setCallingConv($1);
Reid Spencer2c261782007-01-05 17:06:19 +00002273 Fn->setAlignment($9);
2274 if ($8) {
2275 Fn->setSection($8);
2276 free($8);
Chris Lattner164c3782005-11-12 00:11:10 +00002277 }
Chris Lattner00950542001-06-06 20:29:01 +00002278
Chris Lattner7e708292002-06-25 16:13:24 +00002279 // Add all of the arguments we parsed to the function...
Chris Lattnera8e8f162005-05-06 20:27:19 +00002280 if ($5) { // Is null if empty...
Chris Lattner69da5cf2002-10-13 20:57:00 +00002281 if (isVarArg) { // Nuke the last entry
Reid Spenceref9b9a72007-02-05 20:47:22 +00002282 assert($5->back().Ty->get() == Type::VoidTy && $5->back().Name == 0 &&
Reid Spencer1755a9a2007-02-05 17:01:20 +00002283 "Not a varargs marker!");
Reid Spencere1553cc2006-12-31 05:40:12 +00002284 delete $5->back().Ty;
Chris Lattnera8e8f162005-05-06 20:27:19 +00002285 $5->pop_back(); // Delete the last entry
Chris Lattner69da5cf2002-10-13 20:57:00 +00002286 }
Chris Lattnere4d5c442005-03-15 04:54:21 +00002287 Function::arg_iterator ArgIt = Fn->arg_begin();
Reid Spenceref9b9a72007-02-05 20:47:22 +00002288 Function::arg_iterator ArgEnd = Fn->arg_end();
Reid Spencere1553cc2006-12-31 05:40:12 +00002289 unsigned Idx = 1;
Reid Spenceref9b9a72007-02-05 20:47:22 +00002290 for (ArgListType::iterator I = $5->begin();
2291 I != $5->end() && ArgIt != ArgEnd; ++I, ++ArgIt) {
Reid Spencere1553cc2006-12-31 05:40:12 +00002292 delete I->Ty; // Delete the typeholder...
Reid Spenceref9b9a72007-02-05 20:47:22 +00002293 setValueName(ArgIt, I->Name); // Insert arg into symtab...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002294 CHECK_FOR_ERROR
Chris Lattner69da5cf2002-10-13 20:57:00 +00002295 InsertValue(ArgIt);
Reid Spencere1553cc2006-12-31 05:40:12 +00002296 Idx++;
Chris Lattner00950542001-06-06 20:29:01 +00002297 }
Reid Spencer08de34b2006-12-03 05:45:44 +00002298
Chris Lattnera8e8f162005-05-06 20:27:19 +00002299 delete $5; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00002300 }
Reid Spencer61c83e02006-08-18 08:43:06 +00002301 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002302};
Chris Lattner00950542001-06-06 20:29:01 +00002303
Chris Lattner9b02cc32002-05-03 18:23:48 +00002304BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
2305
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002306FunctionHeader : FunctionDefineLinkage GVVisibilityStyle FunctionHeaderH BEGIN {
Chris Lattner394f2eb2003-10-16 20:12:13 +00002307 $$ = CurFun.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00002308
Chris Lattner4ad02e72003-04-16 20:28:45 +00002309 // Make sure that we keep track of the linkage type even if there was a
2310 // previous "declare".
2311 $$->setLinkage($1);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002312 $$->setVisibility($2);
Chris Lattner51727be2002-06-04 21:58:56 +00002313};
Chris Lattner00950542001-06-06 20:29:01 +00002314
Chris Lattner9b02cc32002-05-03 18:23:48 +00002315END : ENDTOK | '}'; // Allow end of '}' to end a function
2316
Chris Lattner79df7c02002-03-26 18:01:55 +00002317Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00002318 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002319 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002320};
Chris Lattner00950542001-06-06 20:29:01 +00002321
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002322FunctionProto : FunctionDeclareLinkage GVVisibilityStyle FunctionHeaderH {
Reid Spencere1553cc2006-12-31 05:40:12 +00002323 CurFun.CurrentFunction->setLinkage($1);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002324 CurFun.CurrentFunction->setVisibility($2);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002325 $$ = CurFun.CurrentFunction;
2326 CurFun.FunctionDone();
2327 CHECK_FOR_ERROR
2328 };
Chris Lattner00950542001-06-06 20:29:01 +00002329
2330//===----------------------------------------------------------------------===//
2331// Rules to match Basic Blocks
2332//===----------------------------------------------------------------------===//
2333
Chris Lattneraa2c8532006-01-25 22:26:43 +00002334OptSideEffect : /* empty */ {
2335 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002336 CHECK_FOR_ERROR
Chris Lattneraa2c8532006-01-25 22:26:43 +00002337 }
2338 | SIDEEFFECT {
2339 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002340 CHECK_FOR_ERROR
Chris Lattneraa2c8532006-01-25 22:26:43 +00002341 };
2342
Chris Lattner00950542001-06-06 20:29:01 +00002343ConstValueRef : ESINT64VAL { // A reference to a direct constant
2344 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002345 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002346 }
2347 | EUINT64VAL {
2348 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002349 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002350 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00002351 | FPVAL { // Perhaps it's an FP constant?
2352 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002353 CHECK_FOR_ERROR
Chris Lattner3d52b2f2001-07-15 00:17:01 +00002354 }
Chris Lattner91ef4602004-03-31 03:49:47 +00002355 | TRUETOK {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00002356 $$ = ValID::create(ConstantInt::getTrue());
Reid Spencer61c83e02006-08-18 08:43:06 +00002357 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002358 }
Chris Lattner91ef4602004-03-31 03:49:47 +00002359 | FALSETOK {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00002360 $$ = ValID::create(ConstantInt::getFalse());
Reid Spencer61c83e02006-08-18 08:43:06 +00002361 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002362 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00002363 | NULL_TOK {
2364 $$ = ValID::createNull();
Reid Spencer61c83e02006-08-18 08:43:06 +00002365 CHECK_FOR_ERROR
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002366 }
Chris Lattner16710e92004-10-16 18:17:13 +00002367 | UNDEF {
2368 $$ = ValID::createUndef();
Reid Spencer61c83e02006-08-18 08:43:06 +00002369 CHECK_FOR_ERROR
Chris Lattner16710e92004-10-16 18:17:13 +00002370 }
Chris Lattnerf1f03df2005-12-21 17:53:02 +00002371 | ZEROINITIALIZER { // A vector zero constant.
2372 $$ = ValID::createZeroInit();
Reid Spencer61c83e02006-08-18 08:43:06 +00002373 CHECK_FOR_ERROR
Chris Lattnerf1f03df2005-12-21 17:53:02 +00002374 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00002375 | '<' ConstVector '>' { // Nonempty unsized packed vector
Reid Spencer08de34b2006-12-03 05:45:44 +00002376 const Type *ETy = (*$2)[0]->getType();
Brian Gaeke715c90b2004-08-20 06:00:58 +00002377 int NumElements = $2->size();
2378
Reid Spencer9d6565a2007-02-15 02:26:10 +00002379 VectorType* pt = VectorType::get(ETy, NumElements);
Brian Gaeke715c90b2004-08-20 06:00:58 +00002380 PATypeHolder* PTy = new PATypeHolder(
Reid Spencer08de34b2006-12-03 05:45:44 +00002381 HandleUpRefs(
Reid Spencer9d6565a2007-02-15 02:26:10 +00002382 VectorType::get(
Reid Spencer08de34b2006-12-03 05:45:44 +00002383 ETy,
2384 NumElements)
2385 )
2386 );
Brian Gaeke715c90b2004-08-20 06:00:58 +00002387
2388 // Verify all elements are correct type!
2389 for (unsigned i = 0; i < $2->size(); i++) {
Reid Spencer08de34b2006-12-03 05:45:44 +00002390 if (ETy != (*$2)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00002391 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Brian Gaeke715c90b2004-08-20 06:00:58 +00002392 ETy->getDescription() +"' as required!\nIt is of type '" +
Reid Spencer08de34b2006-12-03 05:45:44 +00002393 (*$2)[i]->getType()->getDescription() + "'.");
Brian Gaeke715c90b2004-08-20 06:00:58 +00002394 }
2395
Reid Spencer9d6565a2007-02-15 02:26:10 +00002396 $$ = ValID::create(ConstantVector::get(pt, *$2));
Brian Gaeke715c90b2004-08-20 06:00:58 +00002397 delete PTy; delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002398 CHECK_FOR_ERROR
Brian Gaeke715c90b2004-08-20 06:00:58 +00002399 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00002400 | ConstExpr {
Reid Spencer08de34b2006-12-03 05:45:44 +00002401 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002402 CHECK_FOR_ERROR
Chris Lattneraa2c8532006-01-25 22:26:43 +00002403 }
2404 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
2405 char *End = UnEscapeLexed($3, true);
2406 std::string AsmStr = std::string($3, End);
2407 End = UnEscapeLexed($5, true);
2408 std::string Constraints = std::string($5, End);
2409 $$ = ValID::createInlineAsm(AsmStr, Constraints, $2);
2410 free($3);
2411 free($5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002412 CHECK_FOR_ERROR
Chris Lattnerd78700d2002-08-16 21:14:40 +00002413 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00002414
Chris Lattner2079fde2001-10-13 06:41:08 +00002415// SymbolicValueRef - Reference to one of two ways of symbolically refering to
2416// another value.
2417//
Reid Spencerb2d17862007-01-26 08:04:51 +00002418SymbolicValueRef : LOCALVAL_ID { // Is it an integer reference...?
2419 $$ = ValID::createLocalID($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002420 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002421 }
Reid Spencerb2d17862007-01-26 08:04:51 +00002422 | GLOBALVAL_ID {
2423 $$ = ValID::createGlobalID($1);
2424 CHECK_FOR_ERROR
2425 }
2426 | LocalName { // Is it a named reference...?
2427 $$ = ValID::createLocalName($1);
2428 CHECK_FOR_ERROR
2429 }
2430 | GlobalName { // Is it a named reference...?
2431 $$ = ValID::createGlobalName($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002432 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002433 };
Chris Lattner2079fde2001-10-13 06:41:08 +00002434
2435// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00002436ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00002437
Chris Lattner00950542001-06-06 20:29:01 +00002438
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002439// ResolvedVal - a <type> <value> pair. This is used only in cases where the
2440// type immediately preceeds the value reference, and allows complex constant
2441// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00002442ResolvedVal : Types ValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00002443 if (!UpRefs.empty())
2444 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2445 $$ = getVal(*$1, $2);
2446 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002447 CHECK_FOR_ERROR
Reid Spencere1553cc2006-12-31 05:40:12 +00002448 }
2449 ;
Chris Lattner8b81bf52001-07-25 22:47:46 +00002450
Chris Lattner00950542001-06-06 20:29:01 +00002451BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner8ab406d2004-07-13 07:59:27 +00002452 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002453 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002454 }
Chris Lattner7e708292002-06-25 16:13:24 +00002455 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
Chris Lattner8ab406d2004-07-13 07:59:27 +00002456 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002457 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002458 };
Chris Lattner00950542001-06-06 20:29:01 +00002459
2460
2461// Basic blocks are terminated by branching instructions:
2462// br, br/cc, switch, ret
2463//
Reid Spencerb2d17862007-01-26 08:04:51 +00002464BasicBlock : InstructionList OptLocalAssign BBTerminatorInst {
Chris Lattner8ab406d2004-07-13 07:59:27 +00002465 setValueName($3, $2);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002466 CHECK_FOR_ERROR
Chris Lattner2079fde2001-10-13 06:41:08 +00002467 InsertValue($3);
Chris Lattner2079fde2001-10-13 06:41:08 +00002468 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00002469 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002470 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002471 };
Chris Lattner00950542001-06-06 20:29:01 +00002472
2473InstructionList : InstructionList Inst {
Reid Spencer3da59db2006-11-27 01:05:10 +00002474 if (CastInst *CI1 = dyn_cast<CastInst>($2))
2475 if (CastInst *CI2 = dyn_cast<CastInst>(CI1->getOperand(0)))
2476 if (CI2->getParent() == 0)
2477 $1->getInstList().push_back(CI2);
Chris Lattner00950542001-06-06 20:29:01 +00002478 $1->getInstList().push_back($2);
2479 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002480 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002481 }
Reid Spencer186a43f2007-03-19 18:39:36 +00002482 | /* empty */ { // Empty space between instruction lists
2483 $$ = defineBBVal(ValID::createLocalID(CurFun.NextValNum));
Reid Spencer61c83e02006-08-18 08:43:06 +00002484 CHECK_FOR_ERROR
Chris Lattner22ae2322004-07-13 08:39:15 +00002485 }
Reid Spencer186a43f2007-03-19 18:39:36 +00002486 | LABELSTR { // Labelled (named) basic block
2487 $$ = defineBBVal(ValID::createLocalName($1));
Reid Spencer61c83e02006-08-18 08:43:06 +00002488 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002489 };
Chris Lattner00950542001-06-06 20:29:01 +00002490
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002491BBTerminatorInst : RET ResolvedVal { // Return with a result...
Reid Spencer08de34b2006-12-03 05:45:44 +00002492 $$ = new ReturnInst($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00002493 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002494 }
Reid Spencer186a43f2007-03-19 18:39:36 +00002495 | RET VOID { // Return with no result...
Chris Lattner00950542001-06-06 20:29:01 +00002496 $$ = new ReturnInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002497 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002498 }
Reid Spencer186a43f2007-03-19 18:39:36 +00002499 | BR LABEL ValueRef { // Unconditional Branch...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002500 BasicBlock* tmpBB = getBBVal($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002501 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002502 $$ = new BranchInst(tmpBB);
Reid Spencer186a43f2007-03-19 18:39:36 +00002503 } // Conditional Branch...
Reid Spencer8088e9d2007-01-13 05:00:20 +00002504 | BR INTTYPE ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
2505 assert(cast<IntegerType>($2)->getBitWidth() == 1 && "Not Bool?");
Reid Spencer5b7e7532006-09-28 19:28:24 +00002506 BasicBlock* tmpBBA = getBBVal($6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002507 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002508 BasicBlock* tmpBBB = getBBVal($9);
2509 CHECK_FOR_ERROR
Reid Spencer4fe16d62007-01-11 18:21:29 +00002510 Value* tmpVal = getVal(Type::Int1Ty, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002511 CHECK_FOR_ERROR
2512 $$ = new BranchInst(tmpBBA, tmpBBB, tmpVal);
Chris Lattner00950542001-06-06 20:29:01 +00002513 }
2514 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencer08de34b2006-12-03 05:45:44 +00002515 Value* tmpVal = getVal($2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002516 CHECK_FOR_ERROR
2517 BasicBlock* tmpBB = getBBVal($6);
2518 CHECK_FOR_ERROR
2519 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, $8->size());
Chris Lattner00950542001-06-06 20:29:01 +00002520 $$ = S;
2521
Chris Lattner9232b992003-04-22 18:42:41 +00002522 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
Chris Lattner46748042002-04-09 19:41:42 +00002523 E = $8->end();
Chris Lattner69331f52005-02-24 05:25:17 +00002524 for (; I != E; ++I) {
2525 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
2526 S->addCase(CI, I->second);
2527 else
Reid Spencerf4fa5902007-02-05 10:16:10 +00002528 GEN_ERROR("Switch case is constant, but not a simple integer");
Chris Lattner69331f52005-02-24 05:25:17 +00002529 }
Chris Lattnerf57a43d2004-04-17 23:49:15 +00002530 delete $8;
Reid Spencer61c83e02006-08-18 08:43:06 +00002531 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002532 }
Chris Lattner196850c2003-05-15 21:30:00 +00002533 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencer08de34b2006-12-03 05:45:44 +00002534 Value* tmpVal = getVal($2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002535 CHECK_FOR_ERROR
2536 BasicBlock* tmpBB = getBBVal($6);
2537 CHECK_FOR_ERROR
2538 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, 0);
Chris Lattner196850c2003-05-15 21:30:00 +00002539 $$ = S;
Reid Spencer61c83e02006-08-18 08:43:06 +00002540 CHECK_FOR_ERROR
Chris Lattner196850c2003-05-15 21:30:00 +00002541 }
Reid Spencer2c261782007-01-05 17:06:19 +00002542 | INVOKE OptCallingConv ResultTypes ValueRef '(' ValueRefList ')' OptFuncAttrs
Chris Lattnera8e8f162005-05-06 20:27:19 +00002543 TO LABEL ValueRef UNWIND LABEL ValueRef {
Chris Lattner2079fde2001-10-13 06:41:08 +00002544
Reid Spencere1553cc2006-12-31 05:40:12 +00002545 // Handle the short syntax
2546 const PointerType *PFTy = 0;
2547 const FunctionType *Ty = 0;
Reid Spencer2c261782007-01-05 17:06:19 +00002548 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002549 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00002550 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00002551 std::vector<const Type*> ParamTypes;
Reid Spencer5694b6e2007-04-09 06:17:21 +00002552 ValueRefList::iterator I = $6->begin(), E = $6->end();
Reid Spencer3cb4dda2007-04-28 14:13:42 +00002553 for (; I != E; ++I) {
Reid Spencere1553cc2006-12-31 05:40:12 +00002554 const Type *Ty = I->Val->getType();
2555 if (Ty == Type::VoidTy)
2556 GEN_ERROR("Short call syntax cannot be used with varargs");
2557 ParamTypes.push_back(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00002558 }
Reid Spencer3cb4dda2007-04-28 14:13:42 +00002559 Ty = FunctionType::get($3->get(), ParamTypes, false);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002560 PFTy = PointerType::get(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00002561 }
Chris Lattner2079fde2001-10-13 06:41:08 +00002562
Reid Spencer2c9df212007-03-20 01:13:00 +00002563 delete $3;
2564
Chris Lattnera8e8f162005-05-06 20:27:19 +00002565 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002566 CHECK_FOR_ERROR
Reid Spencer2c261782007-01-05 17:06:19 +00002567 BasicBlock *Normal = getBBVal($11);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002568 CHECK_FOR_ERROR
Reid Spencer2c261782007-01-05 17:06:19 +00002569 BasicBlock *Except = getBBVal($14);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002570 CHECK_FOR_ERROR
Chris Lattner2079fde2001-10-13 06:41:08 +00002571
Reid Spencer3cb4dda2007-04-28 14:13:42 +00002572 ParamAttrsVector Attrs;
2573 if ($8 != ParamAttr::None) {
2574 ParamAttrsWithIndex PAWI; PAWI.index = 0; PAWI.attrs = 8;
2575 Attrs.push_back(PAWI);
2576 }
2577
Reid Spencere1553cc2006-12-31 05:40:12 +00002578 // Check the arguments
2579 ValueList Args;
2580 if ($6->empty()) { // Has no arguments?
2581 // Make sure no arguments is a good thing!
2582 if (Ty->getNumParams() != 0)
2583 GEN_ERROR("No arguments passed to a function that "
Reid Spencerf4fa5902007-02-05 10:16:10 +00002584 "expects arguments");
Chris Lattner2079fde2001-10-13 06:41:08 +00002585 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00002586 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00002587 // correctly!
Chris Lattnerd5d89962004-02-09 04:14:01 +00002588 FunctionType::param_iterator I = Ty->param_begin();
2589 FunctionType::param_iterator E = Ty->param_end();
Reid Spencere1553cc2006-12-31 05:40:12 +00002590 ValueRefList::iterator ArgI = $6->begin(), ArgE = $6->end();
Reid Spencer3cb4dda2007-04-28 14:13:42 +00002591 unsigned index = 1;
Reid Spencere1553cc2006-12-31 05:40:12 +00002592 for (; ArgI != ArgE && I != E; ++ArgI, ++I) {
2593 if (ArgI->Val->getType() != *I)
2594 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00002595 (*I)->getDescription() + "'");
Reid Spencere1553cc2006-12-31 05:40:12 +00002596 Args.push_back(ArgI->Val);
Reid Spencer3cb4dda2007-04-28 14:13:42 +00002597 if (ArgI->Attrs != ParamAttr::None) {
2598 ParamAttrsWithIndex PAWI; PAWI.index = index; PAWI.attrs = ArgI->Attrs;
2599 Attrs.push_back(PAWI);
2600 }
Reid Spencere1553cc2006-12-31 05:40:12 +00002601 }
Reid Spencer08de34b2006-12-03 05:45:44 +00002602
Reid Spencere1553cc2006-12-31 05:40:12 +00002603 if (Ty->isVarArg()) {
2604 if (I == E)
2605 for (; ArgI != ArgE; ++ArgI)
2606 Args.push_back(ArgI->Val); // push the remaining varargs
2607 } else if (I != E || ArgI != ArgE)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002608 GEN_ERROR("Invalid number of parameters detected");
Chris Lattner2079fde2001-10-13 06:41:08 +00002609 }
Reid Spencere1553cc2006-12-31 05:40:12 +00002610
Reid Spencer3cb4dda2007-04-28 14:13:42 +00002611 ParamAttrsList *PAL = 0;
2612 if (!Attrs.empty())
2613 PAL = ParamAttrsList::get(Attrs);
2614 if (isa<Function>(V))
2615 if (PAL != cast<Function>(V)->getParamAttrs())
2616 GEN_ERROR("Invalid parameter attributes for function invoked");
2617
Reid Spencere1553cc2006-12-31 05:40:12 +00002618 // Create the InvokeInst
Chris Lattner9d2fda62007-02-13 05:53:56 +00002619 InvokeInst *II = new InvokeInst(V, Normal, Except, &Args[0], Args.size());
Reid Spencere1553cc2006-12-31 05:40:12 +00002620 II->setCallingConv($2);
Reid Spencer3cb4dda2007-04-28 14:13:42 +00002621 II->setParamAttrs(PAL);
Reid Spencere1553cc2006-12-31 05:40:12 +00002622 $$ = II;
Chris Lattnera8e8f162005-05-06 20:27:19 +00002623 delete $6;
Reid Spencer61c83e02006-08-18 08:43:06 +00002624 CHECK_FOR_ERROR
Chris Lattner36143fc2003-09-08 18:54:55 +00002625 }
2626 | UNWIND {
2627 $$ = new UnwindInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002628 CHECK_FOR_ERROR
Chris Lattner16710e92004-10-16 18:17:13 +00002629 }
2630 | UNREACHABLE {
2631 $$ = new UnreachableInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002632 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002633 };
Chris Lattner2079fde2001-10-13 06:41:08 +00002634
2635
Chris Lattner00950542001-06-06 20:29:01 +00002636
2637JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
2638 $$ = $1;
Reid Spencer186a43f2007-03-19 18:39:36 +00002639 Constant *V = cast<Constant>(getExistingVal($2, $3));
Reid Spencer5b7e7532006-09-28 19:28:24 +00002640 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002641 if (V == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002642 GEN_ERROR("May only switch on a constant pool value");
Chris Lattner00950542001-06-06 20:29:01 +00002643
Reid Spencer5b7e7532006-09-28 19:28:24 +00002644 BasicBlock* tmpBB = getBBVal($6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002645 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002646 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner00950542001-06-06 20:29:01 +00002647 }
2648 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner9232b992003-04-22 18:42:41 +00002649 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Reid Spencer186a43f2007-03-19 18:39:36 +00002650 Constant *V = cast<Constant>(getExistingVal($1, $2));
Reid Spencer5b7e7532006-09-28 19:28:24 +00002651 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002652
2653 if (V == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002654 GEN_ERROR("May only switch on a constant pool value");
Chris Lattner00950542001-06-06 20:29:01 +00002655
Reid Spencer5b7e7532006-09-28 19:28:24 +00002656 BasicBlock* tmpBB = getBBVal($5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002657 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002658 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner51727be2002-06-04 21:58:56 +00002659 };
Chris Lattner00950542001-06-06 20:29:01 +00002660
Reid Spencerb2d17862007-01-26 08:04:51 +00002661Inst : OptLocalAssign InstVal {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002662 // Is this definition named?? if so, assign the name...
2663 setValueName($2, $1);
2664 CHECK_FOR_ERROR
2665 InsertValue($2);
2666 $$ = $2;
2667 CHECK_FOR_ERROR
2668 };
2669
Chris Lattner00950542001-06-06 20:29:01 +00002670
Chris Lattnerc24d2082001-06-11 15:04:20 +00002671PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Reid Spencere1553cc2006-12-31 05:40:12 +00002672 if (!UpRefs.empty())
2673 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Chris Lattner9232b992003-04-22 18:42:41 +00002674 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Reid Spencer08de34b2006-12-03 05:45:44 +00002675 Value* tmpVal = getVal(*$1, $3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002676 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002677 BasicBlock* tmpBB = getBBVal($5);
2678 CHECK_FOR_ERROR
2679 $$->push_back(std::make_pair(tmpVal, tmpBB));
Reid Spencer08de34b2006-12-03 05:45:44 +00002680 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00002681 }
2682 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
2683 $$ = $1;
Reid Spencer5b7e7532006-09-28 19:28:24 +00002684 Value* tmpVal = getVal($1->front().first->getType(), $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00002685 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002686 BasicBlock* tmpBB = getBBVal($6);
2687 CHECK_FOR_ERROR
2688 $1->push_back(std::make_pair(tmpVal, tmpBB));
Chris Lattner51727be2002-06-04 21:58:56 +00002689 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00002690
2691
Reid Spencere1553cc2006-12-31 05:40:12 +00002692ValueRefList : Types ValueRef OptParamAttrs {
2693 if (!UpRefs.empty())
2694 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2695 // Used for call and invoke instructions
2696 $$ = new ValueRefList();
2697 ValueRefListEntry E; E.Attrs = $3; E.Val = getVal($1->get(), $2);
2698 $$->push_back(E);
Reid Spencer2c9df212007-03-20 01:13:00 +00002699 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00002700 }
Reid Spencere1553cc2006-12-31 05:40:12 +00002701 | ValueRefList ',' Types ValueRef OptParamAttrs {
2702 if (!UpRefs.empty())
2703 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattner00950542001-06-06 20:29:01 +00002704 $$ = $1;
Reid Spencere1553cc2006-12-31 05:40:12 +00002705 ValueRefListEntry E; E.Attrs = $5; E.Val = getVal($3->get(), $4);
2706 $$->push_back(E);
Reid Spencer2c9df212007-03-20 01:13:00 +00002707 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002708 CHECK_FOR_ERROR
Reid Spencere1553cc2006-12-31 05:40:12 +00002709 }
2710 | /*empty*/ { $$ = new ValueRefList(); };
Chris Lattner00950542001-06-06 20:29:01 +00002711
Reid Spencere1553cc2006-12-31 05:40:12 +00002712IndexList // Used for gep instructions and constant expressions
Reid Spencere03969f2006-12-31 21:46:36 +00002713 : /*empty*/ { $$ = new std::vector<Value*>(); }
Reid Spencere1553cc2006-12-31 05:40:12 +00002714 | IndexList ',' ResolvedVal {
2715 $$ = $1;
2716 $$->push_back($3);
2717 CHECK_FOR_ERROR
2718 }
Reid Spencer71305d72006-12-31 21:25:25 +00002719 ;
Chris Lattner00950542001-06-06 20:29:01 +00002720
Chris Lattnerddb6db42005-05-06 05:51:46 +00002721OptTailCall : TAIL CALL {
2722 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002723 CHECK_FOR_ERROR
Chris Lattnerddb6db42005-05-06 05:51:46 +00002724 }
2725 | CALL {
2726 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002727 CHECK_FOR_ERROR
Chris Lattnerddb6db42005-05-06 05:51:46 +00002728 };
2729
Chris Lattner4a6482b2002-09-10 19:57:26 +00002730InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00002731 if (!UpRefs.empty())
2732 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Chris Lattner42a75512007-01-15 02:27:26 +00002733 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint() &&
Reid Spencer9d6565a2007-02-15 02:26:10 +00002734 !isa<VectorType>((*$2).get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002735 GEN_ERROR(
Reid Spencerf4fa5902007-02-05 10:16:10 +00002736 "Arithmetic operator requires integer, FP, or packed operands");
Reid Spencer9d6565a2007-02-15 02:26:10 +00002737 if (isa<VectorType>((*$2).get()) &&
Reid Spencer08de34b2006-12-03 05:45:44 +00002738 ($1 == Instruction::URem ||
2739 $1 == Instruction::SRem ||
2740 $1 == Instruction::FRem))
Reid Spencerac9dcb92007-02-15 03:39:18 +00002741 GEN_ERROR("Remainder not supported on vector types");
Reid Spencer08de34b2006-12-03 05:45:44 +00002742 Value* val1 = getVal(*$2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002743 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00002744 Value* val2 = getVal(*$2, $5);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002745 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00002746 $$ = BinaryOperator::create($1, val1, val2);
Chris Lattner4a6482b2002-09-10 19:57:26 +00002747 if ($$ == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002748 GEN_ERROR("binary operator returned null");
Reid Spencer08de34b2006-12-03 05:45:44 +00002749 delete $2;
Chris Lattner4a6482b2002-09-10 19:57:26 +00002750 }
2751 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00002752 if (!UpRefs.empty())
2753 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Chris Lattner42a75512007-01-15 02:27:26 +00002754 if (!(*$2)->isInteger()) {
Reid Spencer9d6565a2007-02-15 02:26:10 +00002755 if (Instruction::isShift($1) || !isa<VectorType>($2->get()) ||
2756 !cast<VectorType>($2->get())->getElementType()->isInteger())
Reid Spencerf4fa5902007-02-05 10:16:10 +00002757 GEN_ERROR("Logical operator requires integral operands");
Chris Lattner0a017832005-12-21 18:31:29 +00002758 }
Reid Spencer08de34b2006-12-03 05:45:44 +00002759 Value* tmpVal1 = getVal(*$2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002760 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00002761 Value* tmpVal2 = getVal(*$2, $5);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002762 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00002763 $$ = BinaryOperator::create($1, tmpVal1, tmpVal2);
Chris Lattner4a6482b2002-09-10 19:57:26 +00002764 if ($$ == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002765 GEN_ERROR("binary operator returned null");
Reid Spencer08de34b2006-12-03 05:45:44 +00002766 delete $2;
Chris Lattner4a6482b2002-09-10 19:57:26 +00002767 }
Reid Spencer08de34b2006-12-03 05:45:44 +00002768 | ICMP IPredicates Types ValueRef ',' ValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00002769 if (!UpRefs.empty())
2770 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencer9d6565a2007-02-15 02:26:10 +00002771 if (isa<VectorType>((*$3).get()))
Reid Spencerac9dcb92007-02-15 03:39:18 +00002772 GEN_ERROR("Vector types not supported by icmp instruction");
Reid Spencer08de34b2006-12-03 05:45:44 +00002773 Value* tmpVal1 = getVal(*$3, $4);
2774 CHECK_FOR_ERROR
2775 Value* tmpVal2 = getVal(*$3, $6);
2776 CHECK_FOR_ERROR
2777 $$ = CmpInst::create($1, $2, tmpVal1, tmpVal2);
2778 if ($$ == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002779 GEN_ERROR("icmp operator returned null");
Reid Spencer2c9df212007-03-20 01:13:00 +00002780 delete $3;
Reid Spencer08de34b2006-12-03 05:45:44 +00002781 }
2782 | FCMP FPredicates Types ValueRef ',' ValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00002783 if (!UpRefs.empty())
2784 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencer9d6565a2007-02-15 02:26:10 +00002785 if (isa<VectorType>((*$3).get()))
Reid Spencerac9dcb92007-02-15 03:39:18 +00002786 GEN_ERROR("Vector types not supported by fcmp instruction");
Reid Spencer08de34b2006-12-03 05:45:44 +00002787 Value* tmpVal1 = getVal(*$3, $4);
2788 CHECK_FOR_ERROR
2789 Value* tmpVal2 = getVal(*$3, $6);
2790 CHECK_FOR_ERROR
2791 $$ = CmpInst::create($1, $2, tmpVal1, tmpVal2);
2792 if ($$ == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002793 GEN_ERROR("fcmp operator returned null");
Reid Spencer2c9df212007-03-20 01:13:00 +00002794 delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00002795 }
Reid Spencer3da59db2006-11-27 01:05:10 +00002796 | CastOps ResolvedVal TO Types {
Reid Spencere1553cc2006-12-31 05:40:12 +00002797 if (!UpRefs.empty())
2798 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00002799 Value* Val = $2;
Reid Spencer93947c32007-01-17 02:47:33 +00002800 const Type* DestTy = $4->get();
2801 if (!CastInst::castIsValid($1, Val, DestTy))
2802 GEN_ERROR("invalid cast opcode for cast from '" +
2803 Val->getType()->getDescription() + "' to '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00002804 DestTy->getDescription() + "'");
Reid Spencer93947c32007-01-17 02:47:33 +00002805 $$ = CastInst::create($1, Val, DestTy);
Reid Spencer08de34b2006-12-03 05:45:44 +00002806 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00002807 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00002808 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer4fe16d62007-01-11 18:21:29 +00002809 if ($2->getType() != Type::Int1Ty)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002810 GEN_ERROR("select condition must be boolean");
Reid Spencer08de34b2006-12-03 05:45:44 +00002811 if ($4->getType() != $6->getType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00002812 GEN_ERROR("select value types should match");
Reid Spencer08de34b2006-12-03 05:45:44 +00002813 $$ = new SelectInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002814 CHECK_FOR_ERROR
Chris Lattner76f0ff32004-03-12 05:51:36 +00002815 }
Chris Lattner99e7ab72003-10-18 05:53:13 +00002816 | VAARG ResolvedVal ',' Types {
Reid Spencere1553cc2006-12-31 05:40:12 +00002817 if (!UpRefs.empty())
2818 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00002819 $$ = new VAArgInst($2, *$4);
2820 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00002821 CHECK_FOR_ERROR
Chris Lattner99e7ab72003-10-18 05:53:13 +00002822 }
Robert Bocchino9c62b562006-01-10 19:04:32 +00002823 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencer08de34b2006-12-03 05:45:44 +00002824 if (!ExtractElementInst::isValidOperands($2, $4))
Reid Spencerf4fa5902007-02-05 10:16:10 +00002825 GEN_ERROR("Invalid extractelement operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00002826 $$ = new ExtractElementInst($2, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00002827 CHECK_FOR_ERROR
Robert Bocchino9c62b562006-01-10 19:04:32 +00002828 }
Robert Bocchino2def1b32006-01-17 20:06:25 +00002829 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer08de34b2006-12-03 05:45:44 +00002830 if (!InsertElementInst::isValidOperands($2, $4, $6))
Reid Spencerf4fa5902007-02-05 10:16:10 +00002831 GEN_ERROR("Invalid insertelement operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00002832 $$ = new InsertElementInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002833 CHECK_FOR_ERROR
Robert Bocchino2def1b32006-01-17 20:06:25 +00002834 }
Chris Lattner4c949082006-04-08 01:18:35 +00002835 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer08de34b2006-12-03 05:45:44 +00002836 if (!ShuffleVectorInst::isValidOperands($2, $4, $6))
Reid Spencerf4fa5902007-02-05 10:16:10 +00002837 GEN_ERROR("Invalid shufflevector operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00002838 $$ = new ShuffleVectorInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002839 CHECK_FOR_ERROR
Chris Lattner4c949082006-04-08 01:18:35 +00002840 }
Chris Lattner3b237fc2003-10-19 21:34:28 +00002841 | PHI_TOK PHIList {
Chris Lattnerc24d2082001-06-11 15:04:20 +00002842 const Type *Ty = $2->front().first->getType();
Chris Lattner8ea8f362003-10-30 01:38:18 +00002843 if (!Ty->isFirstClassType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00002844 GEN_ERROR("PHI node operands must be of first class type");
Chris Lattnerc24d2082001-06-11 15:04:20 +00002845 $$ = new PHINode(Ty);
Chris Lattnerce1df9e2005-01-29 00:35:55 +00002846 ((PHINode*)$$)->reserveOperandSpace($2->size());
Chris Lattner00950542001-06-06 20:29:01 +00002847 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00002848 if ($2->front().first->getType() != Ty)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002849 GEN_ERROR("All elements of a PHI node must be of the same type");
Chris Lattnerb00c5822001-10-02 03:41:24 +00002850 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00002851 $2->pop_front();
2852 }
2853 delete $2; // Free the list...
Reid Spencer61c83e02006-08-18 08:43:06 +00002854 CHECK_FOR_ERROR
Chris Lattnerddb6db42005-05-06 05:51:46 +00002855 }
Reid Spencer2c261782007-01-05 17:06:19 +00002856 | OptTailCall OptCallingConv ResultTypes ValueRef '(' ValueRefList ')'
2857 OptFuncAttrs {
Reid Spencere1553cc2006-12-31 05:40:12 +00002858
2859 // Handle the short syntax
Bill Wendlingb39a55a2006-11-12 11:10:39 +00002860 const PointerType *PFTy = 0;
2861 const FunctionType *Ty = 0;
Reid Spencer2c261782007-01-05 17:06:19 +00002862 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002863 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00002864 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00002865 std::vector<const Type*> ParamTypes;
Reid Spencer5694b6e2007-04-09 06:17:21 +00002866 ValueRefList::iterator I = $6->begin(), E = $6->end();
Reid Spencer3cb4dda2007-04-28 14:13:42 +00002867 for (; I != E; ++I) {
Reid Spencere1553cc2006-12-31 05:40:12 +00002868 const Type *Ty = I->Val->getType();
2869 if (Ty == Type::VoidTy)
2870 GEN_ERROR("Short call syntax cannot be used with varargs");
2871 ParamTypes.push_back(Ty);
Chris Lattneref9c23f2001-10-03 14:53:21 +00002872 }
Reid Spencer3cb4dda2007-04-28 14:13:42 +00002873 Ty = FunctionType::get($3->get(), ParamTypes, false);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002874 PFTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00002875 }
Chris Lattner00950542001-06-06 20:29:01 +00002876
Chris Lattnera8e8f162005-05-06 20:27:19 +00002877 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002878 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002879
Reid Spencerebff55c2007-04-16 06:55:42 +00002880 // Check for call to invalid intrinsic to avoid crashing later.
2881 if (Function *theF = dyn_cast<Function>(V)) {
Reid Spencerce1e8ef2007-04-16 22:01:57 +00002882 if (theF->hasName() && (theF->getValueName()->getKeyLength() >= 5) &&
Reid Spencer43e60732007-04-16 20:31:06 +00002883 (0 == strncmp(theF->getValueName()->getKeyData(), "llvm.", 5)) &&
2884 !theF->getIntrinsicID(true))
Reid Spencerebff55c2007-04-16 06:55:42 +00002885 GEN_ERROR("Call to invalid LLVM intrinsic function '" +
2886 theF->getName() + "'");
2887 }
2888
Reid Spencer3cb4dda2007-04-28 14:13:42 +00002889 // Set up the ParamAttrs for the function
2890 ParamAttrsVector Attrs;
2891 if ($8 != ParamAttr::None) {
2892 ParamAttrsWithIndex PAWI; PAWI.index = 0; PAWI.attrs = $8;
2893 Attrs.push_back(PAWI);
2894 }
Reid Spencere1553cc2006-12-31 05:40:12 +00002895 // Check the arguments
2896 ValueList Args;
2897 if ($6->empty()) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00002898 // Make sure no arguments is a good thing!
2899 if (Ty->getNumParams() != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002900 GEN_ERROR("No arguments passed to a function that "
Reid Spencerf4fa5902007-02-05 10:16:10 +00002901 "expects arguments");
Chris Lattner8b81bf52001-07-25 22:47:46 +00002902 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00002903 // Loop through FunctionType's arguments and ensure they are specified
Reid Spencer3cb4dda2007-04-28 14:13:42 +00002904 // correctly. Also, gather any parameter attributes.
Chris Lattnerd5d89962004-02-09 04:14:01 +00002905 FunctionType::param_iterator I = Ty->param_begin();
2906 FunctionType::param_iterator E = Ty->param_end();
Reid Spencere1553cc2006-12-31 05:40:12 +00002907 ValueRefList::iterator ArgI = $6->begin(), ArgE = $6->end();
Reid Spencer3cb4dda2007-04-28 14:13:42 +00002908 unsigned index = 1;
Reid Spencere1553cc2006-12-31 05:40:12 +00002909 for (; ArgI != ArgE && I != E; ++ArgI, ++I) {
2910 if (ArgI->Val->getType() != *I)
2911 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00002912 (*I)->getDescription() + "'");
Reid Spencere1553cc2006-12-31 05:40:12 +00002913 Args.push_back(ArgI->Val);
Reid Spencer3cb4dda2007-04-28 14:13:42 +00002914 if (ArgI->Attrs != ParamAttr::None) {
2915 ParamAttrsWithIndex PAWI; PAWI.index = index; PAWI.attrs = ArgI->Attrs;
2916 Attrs.push_back(PAWI);
2917 }
Reid Spencere1553cc2006-12-31 05:40:12 +00002918 }
2919 if (Ty->isVarArg()) {
2920 if (I == E)
2921 for (; ArgI != ArgE; ++ArgI)
2922 Args.push_back(ArgI->Val); // push the remaining varargs
2923 } else if (I != E || ArgI != ArgE)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002924 GEN_ERROR("Invalid number of parameters detected");
Chris Lattner8b81bf52001-07-25 22:47:46 +00002925 }
Reid Spencer3cb4dda2007-04-28 14:13:42 +00002926
2927 // Finish off the ParamAttrs and check them
2928 ParamAttrsList *PAL = 0;
2929 if (!Attrs.empty())
2930 PAL = ParamAttrsList::get(Attrs);
2931 if (isa<Function>(V))
2932 if (PAL != cast<Function>(V)->getParamAttrs())
2933 GEN_ERROR("Invalid parameter attributes for function called");
2934
Reid Spencere1553cc2006-12-31 05:40:12 +00002935 // Create the call node
Chris Lattner9d2fda62007-02-13 05:53:56 +00002936 CallInst *CI = new CallInst(V, &Args[0], Args.size());
Reid Spencere1553cc2006-12-31 05:40:12 +00002937 CI->setTailCall($1);
2938 CI->setCallingConv($2);
Reid Spencer3cb4dda2007-04-28 14:13:42 +00002939 CI->setParamAttrs(PAL);
Reid Spencere1553cc2006-12-31 05:40:12 +00002940 $$ = CI;
Chris Lattnera8e8f162005-05-06 20:27:19 +00002941 delete $6;
Reid Spencerb2d17862007-01-26 08:04:51 +00002942 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002943 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002944 }
2945 | MemoryInst {
2946 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002947 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002948 };
Chris Lattner00950542001-06-06 20:29:01 +00002949
Chris Lattner15c9c032003-09-08 18:20:29 +00002950OptVolatile : VOLATILE {
2951 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002952 CHECK_FOR_ERROR
Chris Lattner15c9c032003-09-08 18:20:29 +00002953 }
2954 | /* empty */ {
2955 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002956 CHECK_FOR_ERROR
Chris Lattner15c9c032003-09-08 18:20:29 +00002957 };
2958
Chris Lattner027dcc52001-07-08 21:10:27 +00002959
Chris Lattnerddb6db42005-05-06 05:51:46 +00002960
Chris Lattner66db8e42005-11-06 06:34:12 +00002961MemoryInst : MALLOC Types OptCAlign {
Reid Spencere1553cc2006-12-31 05:40:12 +00002962 if (!UpRefs.empty())
2963 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00002964 $$ = new MallocInst(*$2, 0, $3);
2965 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002966 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002967 }
Reid Spencera54b7cb2007-01-12 07:05:14 +00002968 | MALLOC Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencere1553cc2006-12-31 05:40:12 +00002969 if (!UpRefs.empty())
2970 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00002971 Value* tmpVal = getVal($4, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002972 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00002973 $$ = new MallocInst(*$2, tmpVal, $6);
2974 delete $2;
Nate Begeman14b05292005-11-05 09:21:28 +00002975 }
Chris Lattner66db8e42005-11-06 06:34:12 +00002976 | ALLOCA Types OptCAlign {
Reid Spencere1553cc2006-12-31 05:40:12 +00002977 if (!UpRefs.empty())
2978 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00002979 $$ = new AllocaInst(*$2, 0, $3);
2980 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002981 CHECK_FOR_ERROR
Nate Begeman14b05292005-11-05 09:21:28 +00002982 }
Reid Spencera54b7cb2007-01-12 07:05:14 +00002983 | ALLOCA Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencere1553cc2006-12-31 05:40:12 +00002984 if (!UpRefs.empty())
2985 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00002986 Value* tmpVal = getVal($4, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002987 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00002988 $$ = new AllocaInst(*$2, tmpVal, $6);
2989 delete $2;
Nate Begeman14b05292005-11-05 09:21:28 +00002990 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002991 | FREE ResolvedVal {
Reid Spencer08de34b2006-12-03 05:45:44 +00002992 if (!isa<PointerType>($2->getType()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002993 GEN_ERROR("Trying to free nonpointer type " +
Reid Spencerf4fa5902007-02-05 10:16:10 +00002994 $2->getType()->getDescription() + "");
Reid Spencer08de34b2006-12-03 05:45:44 +00002995 $$ = new FreeInst($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00002996 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002997 }
2998
Christopher Lamb43c7f372007-04-22 19:24:39 +00002999 | OptVolatile LOAD Types ValueRef OptCAlign {
Reid Spencere1553cc2006-12-31 05:40:12 +00003000 if (!UpRefs.empty())
3001 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00003002 if (!isa<PointerType>($3->get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00003003 GEN_ERROR("Can't load from nonpointer type: " +
Reid Spencer08de34b2006-12-03 05:45:44 +00003004 (*$3)->getDescription());
3005 if (!cast<PointerType>($3->get())->getElementType()->isFirstClassType())
Reid Spencer61c83e02006-08-18 08:43:06 +00003006 GEN_ERROR("Can't load from pointer of non-first-class type: " +
Reid Spencer08de34b2006-12-03 05:45:44 +00003007 (*$3)->getDescription());
3008 Value* tmpVal = getVal(*$3, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00003009 CHECK_FOR_ERROR
Christopher Lamb43c7f372007-04-22 19:24:39 +00003010 $$ = new LoadInst(tmpVal, "", $1, $5);
Reid Spencer08de34b2006-12-03 05:45:44 +00003011 delete $3;
Chris Lattner027dcc52001-07-08 21:10:27 +00003012 }
Christopher Lamb43c7f372007-04-22 19:24:39 +00003013 | OptVolatile STORE ResolvedVal ',' Types ValueRef OptCAlign {
Reid Spencere1553cc2006-12-31 05:40:12 +00003014 if (!UpRefs.empty())
3015 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00003016 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner8c8418d2003-09-01 16:31:28 +00003017 if (!PT)
Reid Spencer61c83e02006-08-18 08:43:06 +00003018 GEN_ERROR("Can't store to a nonpointer type: " +
Reid Spencer08de34b2006-12-03 05:45:44 +00003019 (*$5)->getDescription());
Chris Lattner8c8418d2003-09-01 16:31:28 +00003020 const Type *ElTy = PT->getElementType();
Reid Spencer08de34b2006-12-03 05:45:44 +00003021 if (ElTy != $3->getType())
3022 GEN_ERROR("Can't store '" + $3->getType()->getDescription() +
Reid Spencerf4fa5902007-02-05 10:16:10 +00003023 "' into space of type '" + ElTy->getDescription() + "'");
Chris Lattner0383cc42002-08-21 23:51:21 +00003024
Reid Spencer08de34b2006-12-03 05:45:44 +00003025 Value* tmpVal = getVal(*$5, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00003026 CHECK_FOR_ERROR
Christopher Lamb43c7f372007-04-22 19:24:39 +00003027 $$ = new StoreInst($3, tmpVal, $1, $7);
Reid Spencer08de34b2006-12-03 05:45:44 +00003028 delete $5;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00003029 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00003030 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencere1553cc2006-12-31 05:40:12 +00003031 if (!UpRefs.empty())
3032 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00003033 if (!isa<PointerType>($2->get()))
Reid Spencerf4fa5902007-02-05 10:16:10 +00003034 GEN_ERROR("getelementptr insn requires pointer operand");
Chris Lattner830b6f92004-04-05 01:30:04 +00003035
Chris Lattnerc856c7a2007-02-13 00:57:40 +00003036 if (!GetElementPtrInst::getIndexedType(*$2, &(*$4)[0], $4->size(), true))
Reid Spencer61c83e02006-08-18 08:43:06 +00003037 GEN_ERROR("Invalid getelementptr indices for type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00003038 (*$2)->getDescription()+ "'");
Reid Spencer08de34b2006-12-03 05:45:44 +00003039 Value* tmpVal = getVal(*$2, $3);
Reid Spencer61c83e02006-08-18 08:43:06 +00003040 CHECK_FOR_ERROR
Chris Lattnerc856c7a2007-02-13 00:57:40 +00003041 $$ = new GetElementPtrInst(tmpVal, &(*$4)[0], $4->size());
Reid Spencer08de34b2006-12-03 05:45:44 +00003042 delete $2;
Reid Spencer5b7e7532006-09-28 19:28:24 +00003043 delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00003044 };
Chris Lattner027dcc52001-07-08 21:10:27 +00003045
Brian Gaeked0fde302003-11-11 22:41:34 +00003046
Chris Lattner00950542001-06-06 20:29:01 +00003047%%
Reid Spencer61c83e02006-08-18 08:43:06 +00003048
Reid Spencere1553cc2006-12-31 05:40:12 +00003049// common code from the two 'RunVMAsmParser' functions
3050static Module* RunParser(Module * M) {
3051
3052 llvmAsmlineno = 1; // Reset the current line number...
3053 CurModule.CurrentModule = M;
3054#if YYDEBUG
3055 yydebug = Debug;
3056#endif
3057
3058 // Check to make sure the parser succeeded
3059 if (yyparse()) {
3060 if (ParserResult)
3061 delete ParserResult;
3062 return 0;
3063 }
3064
Reid Spencercd5bd902007-03-30 01:37:13 +00003065 // Emit an error if there are any unresolved types left.
3066 if (!CurModule.LateResolveTypes.empty()) {
3067 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
3068 if (DID.Type == ValID::LocalName) {
3069 GenerateError("Undefined type remains at eof: '"+DID.getName() + "'");
3070 } else {
3071 GenerateError("Undefined type remains at eof: #" + itostr(DID.Num));
3072 }
3073 if (ParserResult)
3074 delete ParserResult;
3075 return 0;
3076 }
3077
3078 // Emit an error if there are any unresolved values left.
3079 if (!CurModule.LateResolveValues.empty()) {
3080 Value *V = CurModule.LateResolveValues.back();
3081 std::map<Value*, std::pair<ValID, int> >::iterator I =
3082 CurModule.PlaceHolderInfo.find(V);
3083
3084 if (I != CurModule.PlaceHolderInfo.end()) {
3085 ValID &DID = I->second.first;
3086 if (DID.Type == ValID::LocalName) {
3087 GenerateError("Undefined value remains at eof: "+DID.getName() + "'");
3088 } else {
3089 GenerateError("Undefined value remains at eof: #" + itostr(DID.Num));
3090 }
3091 if (ParserResult)
3092 delete ParserResult;
3093 return 0;
3094 }
3095 }
3096
Reid Spencere1553cc2006-12-31 05:40:12 +00003097 // Check to make sure that parsing produced a result
3098 if (!ParserResult)
3099 return 0;
3100
3101 // Reset ParserResult variable while saving its value for the result.
3102 Module *Result = ParserResult;
3103 ParserResult = 0;
3104
3105 return Result;
3106}
3107
Reid Spencer61c83e02006-08-18 08:43:06 +00003108void llvm::GenerateError(const std::string &message, int LineNo) {
3109 if (LineNo == -1) LineNo = llvmAsmlineno;
3110 // TODO: column number in exception
3111 if (TheParseError)
3112 TheParseError->setError(CurFilename, message, LineNo);
3113 TriggerError = 1;
3114}
3115
Chris Lattner09083092001-07-08 04:57:15 +00003116int yyerror(const char *ErrorMsg) {
Chris Lattner9232b992003-04-22 18:42:41 +00003117 std::string where
3118 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00003119 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Reid Spenceref9b9a72007-02-05 20:47:22 +00003120 std::string errMsg = where + "error: " + std::string(ErrorMsg);
3121 if (yychar != YYEMPTY && yychar != 0)
3122 errMsg += " while reading token: '" + std::string(llvmAsmtext, llvmAsmleng)+
3123 "'";
Reid Spencer61c83e02006-08-18 08:43:06 +00003124 GenerateError(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00003125 return 0;
3126}