blob: ab10cc7d56d528288a979104103733cfb443f6ad [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.
142 GlobalValue *GetForwardRefForGlobal(const PointerType *PTy, ValID ID) {
143 // Check to see if there is a forward reference to this global variable...
144 // if there is, eliminate it and patch the reference to use the new def'n.
145 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(PTy, ID));
146 GlobalValue *Ret = 0;
147 if (I != GlobalRefs.end()) {
148 Ret = I->second;
149 GlobalRefs.erase(I);
150 }
151 return Ret;
152 }
Reid Spencer98b3c5c2007-01-02 21:53:43 +0000153
154 bool TypeIsUnresolved(PATypeHolder* PATy) {
155 // If it isn't abstract, its resolved
156 const Type* Ty = PATy->get();
157 if (!Ty->isAbstract())
158 return false;
159 // Traverse the type looking for abstract types. If it isn't abstract then
160 // we don't need to traverse that leg of the type.
161 std::vector<const Type*> WorkList, SeenList;
162 WorkList.push_back(Ty);
163 while (!WorkList.empty()) {
164 const Type* Ty = WorkList.back();
165 SeenList.push_back(Ty);
166 WorkList.pop_back();
167 if (const OpaqueType* OpTy = dyn_cast<OpaqueType>(Ty)) {
168 // Check to see if this is an unresolved type
169 std::map<ValID, PATypeHolder>::iterator I = LateResolveTypes.begin();
170 std::map<ValID, PATypeHolder>::iterator E = LateResolveTypes.end();
171 for ( ; I != E; ++I) {
172 if (I->second.get() == OpTy)
173 return true;
174 }
175 } else if (const SequentialType* SeqTy = dyn_cast<SequentialType>(Ty)) {
176 const Type* TheTy = SeqTy->getElementType();
177 if (TheTy->isAbstract() && TheTy != Ty) {
178 std::vector<const Type*>::iterator I = SeenList.begin(),
179 E = SeenList.end();
180 for ( ; I != E; ++I)
181 if (*I == TheTy)
182 break;
183 if (I == E)
184 WorkList.push_back(TheTy);
185 }
186 } else if (const StructType* StrTy = dyn_cast<StructType>(Ty)) {
187 for (unsigned i = 0; i < StrTy->getNumElements(); ++i) {
188 const Type* TheTy = StrTy->getElementType(i);
189 if (TheTy->isAbstract() && TheTy != Ty) {
190 std::vector<const Type*>::iterator I = SeenList.begin(),
191 E = SeenList.end();
192 for ( ; I != E; ++I)
193 if (*I == TheTy)
194 break;
195 if (I == E)
196 WorkList.push_back(TheTy);
197 }
198 }
199 }
200 }
201 return false;
202 }
Chris Lattner00950542001-06-06 20:29:01 +0000203} CurModule;
204
Chris Lattner79df7c02002-03-26 18:01:55 +0000205static struct PerFunctionInfo {
Chris Lattner7e708292002-06-25 16:13:24 +0000206 Function *CurrentFunction; // Pointer to current function being created
Chris Lattner00950542001-06-06 20:29:01 +0000207
Reid Spencer186a43f2007-03-19 18:39:36 +0000208 ValueList Values; // Keep track of #'d definitions
209 unsigned NextValNum;
210 ValueList LateResolveValues;
Reid Spenceref9b9a72007-02-05 20:47:22 +0000211 bool isDeclare; // Is this function a forward declararation?
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000212 GlobalValue::LinkageTypes Linkage; // Linkage for forward declaration.
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000213 GlobalValue::VisibilityTypes Visibility;
Chris Lattner00950542001-06-06 20:29:01 +0000214
Chris Lattner2e2ed292004-07-14 06:28:35 +0000215 /// BBForwardRefs - When we see forward references to basic blocks, keep
216 /// track of them here.
Reid Spencer186a43f2007-03-19 18:39:36 +0000217 std::map<ValID, BasicBlock*> BBForwardRefs;
Chris Lattner2e2ed292004-07-14 06:28:35 +0000218
Chris Lattner79df7c02002-03-26 18:01:55 +0000219 inline PerFunctionInfo() {
220 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000221 isDeclare = false;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000222 Linkage = GlobalValue::ExternalLinkage;
223 Visibility = GlobalValue::DefaultVisibility;
Chris Lattner00950542001-06-06 20:29:01 +0000224 }
225
Chris Lattner79df7c02002-03-26 18:01:55 +0000226 inline void FunctionStart(Function *M) {
227 CurrentFunction = M;
Reid Spencer186a43f2007-03-19 18:39:36 +0000228 NextValNum = 0;
Chris Lattner00950542001-06-06 20:29:01 +0000229 }
230
Chris Lattner79df7c02002-03-26 18:01:55 +0000231 void FunctionDone() {
Chris Lattner2e2ed292004-07-14 06:28:35 +0000232 // Any forward referenced blocks left?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000233 if (!BBForwardRefs.empty()) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000234 GenerateError("Undefined reference to label " +
Reid Spencer186a43f2007-03-19 18:39:36 +0000235 BBForwardRefs.begin()->second->getName());
Reid Spencer5b7e7532006-09-28 19:28:24 +0000236 return;
237 }
Chris Lattner2e2ed292004-07-14 06:28:35 +0000238
239 // Resolve all forward references now.
Chris Lattner386a3b72001-10-16 19:54:17 +0000240 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000241
Chris Lattner7e708292002-06-25 16:13:24 +0000242 Values.clear(); // Clear out function local definitions
Reid Spencer186a43f2007-03-19 18:39:36 +0000243 BBForwardRefs.clear();
Chris Lattner79df7c02002-03-26 18:01:55 +0000244 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000245 isDeclare = false;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000246 Linkage = GlobalValue::ExternalLinkage;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000247 Visibility = GlobalValue::DefaultVisibility;
Chris Lattner00950542001-06-06 20:29:01 +0000248 }
Chris Lattner394f2eb2003-10-16 20:12:13 +0000249} CurFun; // Info for the current function...
Chris Lattner00950542001-06-06 20:29:01 +0000250
Chris Lattner394f2eb2003-10-16 20:12:13 +0000251static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000252
Chris Lattner00950542001-06-06 20:29:01 +0000253
254//===----------------------------------------------------------------------===//
255// Code to handle definitions of all the types
256//===----------------------------------------------------------------------===//
257
Reid Spencer186a43f2007-03-19 18:39:36 +0000258static void InsertValue(Value *V, ValueList &ValueTab = CurFun.Values) {
259 // Things that have names or are void typed don't get slot numbers
260 if (V->hasName() || (V->getType() == Type::VoidTy))
261 return;
Chris Lattner2079fde2001-10-13 06:41:08 +0000262
Reid Spencer186a43f2007-03-19 18:39:36 +0000263 // In the case of function values, we have to allow for the forward reference
264 // of basic blocks, which are included in the numbering. Consequently, we keep
265 // track of the next insertion location with NextValNum. When a BB gets
266 // inserted, it could change the size of the CurFun.Values vector.
267 if (&ValueTab == &CurFun.Values) {
268 if (ValueTab.size() <= CurFun.NextValNum)
269 ValueTab.resize(CurFun.NextValNum+1);
270 ValueTab[CurFun.NextValNum++] = V;
271 return;
272 }
273 // For all other lists, its okay to just tack it on the back of the vector.
274 ValueTab.push_back(V);
Chris Lattner00950542001-06-06 20:29:01 +0000275}
276
Chris Lattner30c89792001-09-07 16:35:17 +0000277static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000278 switch (D.Type) {
Reid Spencerb2d17862007-01-26 08:04:51 +0000279 case ValID::LocalID: // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000280 // Module constants occupy the lowest numbered slots...
Reid Spencerb2d17862007-01-26 08:04:51 +0000281 if (D.Num < CurModule.Types.size())
282 return CurModule.Types[D.Num];
Chris Lattner42c9e772001-10-20 09:32:59 +0000283 break;
Reid Spencerb2d17862007-01-26 08:04:51 +0000284 case ValID::LocalName: // Is it a named definition?
Chris Lattnera09000d2004-07-14 23:03:46 +0000285 if (const Type *N = CurModule.CurrentModule->getTypeByName(D.Name)) {
286 D.destroy(); // Free old strdup'd memory...
287 return N;
Chris Lattner6e6026b2002-11-20 18:36:02 +0000288 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000289 break;
Chris Lattner30c89792001-09-07 16:35:17 +0000290 default:
Reid Spencerf4fa5902007-02-05 10:16:10 +0000291 GenerateError("Internal parser error: Invalid symbol type reference");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000292 return 0;
Chris Lattner30c89792001-09-07 16:35:17 +0000293 }
294
295 // If we reached here, we referenced either a symbol that we don't know about
296 // or an id number that hasn't been read yet. We may be referencing something
297 // forward, so just create an entry to be resolved later and get to it...
298 //
299 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
300
Chris Lattner355ad1f2005-03-21 06:27:42 +0000301
302 if (inFunctionScope()) {
Reid Spencerb2d17862007-01-26 08:04:51 +0000303 if (D.Type == ValID::LocalName) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000304 GenerateError("Reference to an undefined type: '" + D.getName() + "'");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000305 return 0;
306 } else {
Reid Spencerb2d17862007-01-26 08:04:51 +0000307 GenerateError("Reference to an undefined type: #" + utostr(D.Num));
Reid Spencer5b7e7532006-09-28 19:28:24 +0000308 return 0;
309 }
Chris Lattner4a42e902001-10-22 05:56:09 +0000310 }
Chris Lattner30c89792001-09-07 16:35:17 +0000311
Reid Spencerb78b9082006-11-28 07:28:14 +0000312 std::map<ValID, PATypeHolder>::iterator I =CurModule.LateResolveTypes.find(D);
Chris Lattner355ad1f2005-03-21 06:27:42 +0000313 if (I != CurModule.LateResolveTypes.end())
Reid Spencerb78b9082006-11-28 07:28:14 +0000314 return I->second;
Chris Lattner355ad1f2005-03-21 06:27:42 +0000315
Reid Spencerb78b9082006-11-28 07:28:14 +0000316 Type *Typ = OpaqueType::get();
317 CurModule.LateResolveTypes.insert(std::make_pair(D, Typ));
318 return Typ;
Reid Spencer08de34b2006-12-03 05:45:44 +0000319 }
Chris Lattner30c89792001-09-07 16:35:17 +0000320
Reid Spencer186a43f2007-03-19 18:39:36 +0000321// getExistingVal - Look up the value specified by the provided type and
Chris Lattner2079fde2001-10-13 06:41:08 +0000322// the provided ValID. If the value exists and has already been defined, return
323// it. Otherwise return null.
324//
Reid Spencer186a43f2007-03-19 18:39:36 +0000325static Value *getExistingVal(const Type *Ty, const ValID &D) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000326 if (isa<FunctionType>(Ty)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000327 GenerateError("Functions are not values and "
Chris Lattner79df7c02002-03-26 18:01:55 +0000328 "must be referenced as pointers");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000329 return 0;
330 }
Chris Lattner386a3b72001-10-16 19:54:17 +0000331
Chris Lattner30c89792001-09-07 16:35:17 +0000332 switch (D.Type) {
Reid Spencerb2d17862007-01-26 08:04:51 +0000333 case ValID::LocalID: { // Is it a numbered definition?
Reid Spencerb2d17862007-01-26 08:04:51 +0000334 // Check that the number is within bounds.
Reid Spencer186a43f2007-03-19 18:39:36 +0000335 if (D.Num >= CurFun.Values.size())
336 return 0;
337 Value *Result = CurFun.Values[D.Num];
338 if (Ty != Result->getType()) {
339 GenerateError("Numbered value (%" + utostr(D.Num) + ") of type '" +
340 Result->getType()->getDescription() + "' does not match "
341 "expected type, '" + Ty->getDescription() + "'");
342 return 0;
343 }
344 return Result;
Reid Spencerb2d17862007-01-26 08:04:51 +0000345 }
346 case ValID::GlobalID: { // Is it a numbered definition?
Reid Spencer186a43f2007-03-19 18:39:36 +0000347 if (D.Num >= CurModule.Values.size())
Reid Spenceref9b9a72007-02-05 20:47:22 +0000348 return 0;
Reid Spencer186a43f2007-03-19 18:39:36 +0000349 Value *Result = CurModule.Values[D.Num];
350 if (Ty != Result->getType()) {
351 GenerateError("Numbered value (@" + utostr(D.Num) + ") of type '" +
352 Result->getType()->getDescription() + "' does not match "
353 "expected type, '" + Ty->getDescription() + "'");
Reid Spenceref9b9a72007-02-05 20:47:22 +0000354 return 0;
Reid Spencer186a43f2007-03-19 18:39:36 +0000355 }
356 return Result;
Chris Lattner00950542001-06-06 20:29:01 +0000357 }
Reid Spencerb2d17862007-01-26 08:04:51 +0000358
359 case ValID::LocalName: { // Is it a named definition?
Reid Spenceref9b9a72007-02-05 20:47:22 +0000360 if (!inFunctionScope())
361 return 0;
362 ValueSymbolTable &SymTab = CurFun.CurrentFunction->getValueSymbolTable();
363 Value *N = SymTab.lookup(D.Name);
364 if (N == 0)
365 return 0;
366 if (N->getType() != Ty)
367 return 0;
Reid Spencerb2d17862007-01-26 08:04:51 +0000368
369 D.destroy(); // Free old strdup'd memory...
370 return N;
371 }
372 case ValID::GlobalName: { // Is it a named definition?
Reid Spenceref9b9a72007-02-05 20:47:22 +0000373 ValueSymbolTable &SymTab = CurModule.CurrentModule->getValueSymbolTable();
374 Value *N = SymTab.lookup(D.Name);
375 if (N == 0)
376 return 0;
377 if (N->getType() != Ty)
378 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000379
380 D.destroy(); // Free old strdup'd memory...
381 return N;
382 }
383
Misha Brukman5a2a3822005-05-10 22:02:28 +0000384 // Check to make sure that "Ty" is an integral type, and that our
Chris Lattner2079fde2001-10-13 06:41:08 +0000385 // value will fit into the specified type...
386 case ValID::ConstSIntVal: // Is it a constant pool reference??
Reid Spencerb83eb642006-10-20 07:07:24 +0000387 if (!ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000388 GenerateError("Signed integral constant '" +
Misha Brukman5a2a3822005-05-10 22:02:28 +0000389 itostr(D.ConstPool64) + "' is invalid for type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +0000390 Ty->getDescription() + "'");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000391 return 0;
392 }
Reid Spencereac65742007-03-19 20:40:22 +0000393 return ConstantInt::get(Ty, D.ConstPool64, true);
Chris Lattner2079fde2001-10-13 06:41:08 +0000394
395 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Reid Spencerb83eb642006-10-20 07:07:24 +0000396 if (!ConstantInt::isValueValidForType(Ty, D.UConstPool64)) {
397 if (!ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000398 GenerateError("Integral constant '" + utostr(D.UConstPool64) +
Reid Spencerf4fa5902007-02-05 10:16:10 +0000399 "' is invalid or out of range");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000400 return 0;
Chris Lattner2079fde2001-10-13 06:41:08 +0000401 } else { // This is really a signed reference. Transmogrify.
Reid Spencereac65742007-03-19 20:40:22 +0000402 return ConstantInt::get(Ty, D.ConstPool64, true);
Chris Lattner2079fde2001-10-13 06:41:08 +0000403 }
404 } else {
Reid Spencerb83eb642006-10-20 07:07:24 +0000405 return ConstantInt::get(Ty, D.UConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000406 }
407
Chris Lattner2079fde2001-10-13 06:41:08 +0000408 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000409 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP)) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000410 GenerateError("FP constant invalid for type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000411 return 0;
412 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000413 return ConstantFP::get(Ty, D.ConstPoolFP);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000414
Chris Lattner2079fde2001-10-13 06:41:08 +0000415 case ValID::ConstNullVal: // Is it a null value?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000416 if (!isa<PointerType>(Ty)) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000417 GenerateError("Cannot create a a non pointer null");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000418 return 0;
419 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000420 return ConstantPointerNull::get(cast<PointerType>(Ty));
Misha Brukman5a2a3822005-05-10 22:02:28 +0000421
Chris Lattner16710e92004-10-16 18:17:13 +0000422 case ValID::ConstUndefVal: // Is it an undef value?
423 return UndefValue::get(Ty);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000424
Chris Lattnerf1f03df2005-12-21 17:53:02 +0000425 case ValID::ConstZeroVal: // Is it a zero value?
426 return Constant::getNullValue(Ty);
427
Chris Lattnerd78700d2002-08-16 21:14:40 +0000428 case ValID::ConstantVal: // Fully resolved constant?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000429 if (D.ConstantValue->getType() != Ty) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000430 GenerateError("Constant expression type different from required type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000431 return 0;
432 }
Chris Lattnerd78700d2002-08-16 21:14:40 +0000433 return D.ConstantValue;
434
Chris Lattneraa2c8532006-01-25 22:26:43 +0000435 case ValID::InlineAsmVal: { // Inline asm expression
436 const PointerType *PTy = dyn_cast<PointerType>(Ty);
437 const FunctionType *FTy =
438 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
Reid Spencer5b7e7532006-09-28 19:28:24 +0000439 if (!FTy || !InlineAsm::Verify(FTy, D.IAD->Constraints)) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000440 GenerateError("Invalid type for asm constraint string");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000441 return 0;
442 }
Chris Lattneraa2c8532006-01-25 22:26:43 +0000443 InlineAsm *IA = InlineAsm::get(FTy, D.IAD->AsmString, D.IAD->Constraints,
444 D.IAD->HasSideEffects);
445 D.destroy(); // Free InlineAsmDescriptor.
446 return IA;
447 }
Chris Lattner30c89792001-09-07 16:35:17 +0000448 default:
Reid Spencer1755a9a2007-02-05 17:01:20 +0000449 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000450 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000451 } // End of switch
452
Reid Spencer1755a9a2007-02-05 17:01:20 +0000453 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000454 return 0;
455}
456
Reid Spencer186a43f2007-03-19 18:39:36 +0000457// getVal - This function is identical to getExistingVal, except that if a
Chris Lattner2079fde2001-10-13 06:41:08 +0000458// value is not already defined, it "improvises" by creating a placeholder var
459// that looks and acts just like the requested variable. When the value is
460// defined later, all uses of the placeholder variable are replaced with the
461// real thing.
462//
Chris Lattner64116f92004-07-14 01:33:11 +0000463static Value *getVal(const Type *Ty, const ValID &ID) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000464 if (Ty == Type::LabelTy) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000465 GenerateError("Cannot use a basic block here");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000466 return 0;
467 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000468
Chris Lattner64116f92004-07-14 01:33:11 +0000469 // See if the value has already been defined.
Reid Spencer186a43f2007-03-19 18:39:36 +0000470 Value *V = getExistingVal(Ty, ID);
Chris Lattner2079fde2001-10-13 06:41:08 +0000471 if (V) return V;
Reid Spencer5b7e7532006-09-28 19:28:24 +0000472 if (TriggerError) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000473
Reid Spencer5b7e7532006-09-28 19:28:24 +0000474 if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty)) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000475 GenerateError("Invalid use of a composite type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000476 return 0;
477 }
Chris Lattner60cd9552005-03-23 01:29:26 +0000478
Chris Lattner00950542001-06-06 20:29:01 +0000479 // If we reached here, we referenced either a symbol that we don't know about
480 // or an id number that hasn't been read yet. We may be referencing something
481 // forward, so just create an entry to be resolved later and get to it...
482 //
Chris Lattner64116f92004-07-14 01:33:11 +0000483 V = new Argument(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000484
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000485 // Remember where this forward reference came from. FIXME, shouldn't we try
486 // to recycle these things??
Chris Lattner64116f92004-07-14 01:33:11 +0000487 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000488 llvmAsmlineno)));
489
Chris Lattner79df7c02002-03-26 18:01:55 +0000490 if (inFunctionScope())
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000491 InsertValue(V, CurFun.LateResolveValues);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000492 else
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000493 InsertValue(V, CurModule.LateResolveValues);
494 return V;
Chris Lattner00950542001-06-06 20:29:01 +0000495}
496
Reid Spencer186a43f2007-03-19 18:39:36 +0000497/// defineBBVal - This is a definition of a new basic block with the specified
498/// identifier which must be the same as CurFun.NextValNum, if its numeric.
499static BasicBlock *defineBBVal(const ValID &ID) {
Reid Spencer1755a9a2007-02-05 17:01:20 +0000500 assert(inFunctionScope() && "Can't get basic block at global scope!");
Chris Lattner64116f92004-07-14 01:33:11 +0000501
Chris Lattner2e2ed292004-07-14 06:28:35 +0000502 BasicBlock *BB = 0;
Chris Lattner64116f92004-07-14 01:33:11 +0000503
Reid Spencer186a43f2007-03-19 18:39:36 +0000504 // First, see if this was forward referenced
Chris Lattner64116f92004-07-14 01:33:11 +0000505
Reid Spencer186a43f2007-03-19 18:39:36 +0000506 std::map<ValID, BasicBlock*>::iterator BBI = CurFun.BBForwardRefs.find(ID);
507 if (BBI != CurFun.BBForwardRefs.end()) {
508 BB = BBI->second;
Chris Lattner2e2ed292004-07-14 06:28:35 +0000509 // The forward declaration could have been inserted anywhere in the
510 // function: insert it into the correct place now.
511 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
512 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
Reid Spencer186a43f2007-03-19 18:39:36 +0000513
Reid Spencer2c9df212007-03-20 01:13:00 +0000514 // We're about to erase the entry, save the key so we can clean it up.
515 ValID Tmp = BBI->first;
516
Reid Spencer186a43f2007-03-19 18:39:36 +0000517 // Erase the forward ref from the map as its no longer "forward"
518 CurFun.BBForwardRefs.erase(ID);
519
Reid Spencer2c9df212007-03-20 01:13:00 +0000520 // The key has been removed from the map but so we don't want to leave
521 // strdup'd memory around so destroy it too.
522 Tmp.destroy();
523
Reid Spencer186a43f2007-03-19 18:39:36 +0000524 // If its a numbered definition, bump the number and set the BB value.
525 if (ID.Type == ValID::LocalID) {
526 assert(ID.Num == CurFun.NextValNum && "Invalid new block number");
527 InsertValue(BB);
528 }
529
530 ID.destroy();
531 return BB;
532 }
533
534 // We haven't seen this BB before and its first mention is a definition.
535 // Just create it and return it.
536 std::string Name (ID.Type == ValID::LocalName ? ID.Name : "");
537 BB = new BasicBlock(Name, CurFun.CurrentFunction);
538 if (ID.Type == ValID::LocalID) {
539 assert(ID.Num == CurFun.NextValNum && "Invalid new block number");
540 InsertValue(BB);
Chris Lattner2e2ed292004-07-14 06:28:35 +0000541 }
Reid Spencer186a43f2007-03-19 18:39:36 +0000542
543 ID.destroy(); // Free strdup'd memory
544 return BB;
545}
546
547/// getBBVal - get an existing BB value or create a forward reference for it.
548///
549static BasicBlock *getBBVal(const ValID &ID) {
550 assert(inFunctionScope() && "Can't get basic block at global scope!");
551
552 BasicBlock *BB = 0;
553
554 std::map<ValID, BasicBlock*>::iterator BBI = CurFun.BBForwardRefs.find(ID);
555 if (BBI != CurFun.BBForwardRefs.end()) {
556 BB = BBI->second;
557 } if (ID.Type == ValID::LocalName) {
558 std::string Name = ID.Name;
559 Value *N = CurFun.CurrentFunction->getValueSymbolTable().lookup(Name);
560 if (N)
561 if (N->getType()->getTypeID() == Type::LabelTyID)
562 BB = cast<BasicBlock>(N);
563 else
564 GenerateError("Reference to label '" + Name + "' is actually of type '"+
565 N->getType()->getDescription() + "'");
566 } else if (ID.Type == ValID::LocalID) {
567 if (ID.Num < CurFun.NextValNum && ID.Num < CurFun.Values.size()) {
568 if (CurFun.Values[ID.Num]->getType()->getTypeID() == Type::LabelTyID)
569 BB = cast<BasicBlock>(CurFun.Values[ID.Num]);
570 else
571 GenerateError("Reference to label '%" + utostr(ID.Num) +
572 "' is actually of type '"+
573 CurFun.Values[ID.Num]->getType()->getDescription() + "'");
574 }
575 } else {
576 GenerateError("Illegal label reference " + ID.getName());
577 return 0;
578 }
579
580 // If its already been defined, return it now.
581 if (BB) {
582 ID.destroy(); // Free strdup'd memory.
583 return BB;
584 }
585
586 // Otherwise, this block has not been seen before, create it.
587 std::string Name;
588 if (ID.Type == ValID::LocalName)
589 Name = ID.Name;
590 BB = new BasicBlock(Name, CurFun.CurrentFunction);
591
592 // Insert it in the forward refs map.
593 CurFun.BBForwardRefs[ID] = BB;
594
Chris Lattner64116f92004-07-14 01:33:11 +0000595 return BB;
596}
597
Chris Lattner00950542001-06-06 20:29:01 +0000598
599//===----------------------------------------------------------------------===//
600// Code to handle forward references in instructions
601//===----------------------------------------------------------------------===//
602//
603// This code handles the late binding needed with statements that reference
604// values not defined yet... for example, a forward branch, or the PHI node for
605// a loop body.
606//
Chris Lattner394f2eb2003-10-16 20:12:13 +0000607// This keeps a table (CurFun.LateResolveValues) of all such forward references
Chris Lattner00950542001-06-06 20:29:01 +0000608// and back patchs after we are done.
609//
610
Misha Brukman5a2a3822005-05-10 22:02:28 +0000611// ResolveDefinitions - If we could not resolve some defs at parsing
612// time (forward branches, phi functions for loops, etc...) resolve the
Chris Lattner00950542001-06-06 20:29:01 +0000613// defs now...
614//
Misha Brukman5a2a3822005-05-10 22:02:28 +0000615static void
Reid Spencer186a43f2007-03-19 18:39:36 +0000616ResolveDefinitions(ValueList &LateResolvers, ValueList *FutureLateResolvers) {
Chris Lattner00950542001-06-06 20:29:01 +0000617 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Reid Spencer186a43f2007-03-19 18:39:36 +0000618 while (!LateResolvers.empty()) {
619 Value *V = LateResolvers.back();
620 LateResolvers.pop_back();
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000621
Reid Spencer186a43f2007-03-19 18:39:36 +0000622 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
623 CurModule.PlaceHolderInfo.find(V);
624 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000625
Reid Spencer186a43f2007-03-19 18:39:36 +0000626 ValID &DID = PHI->second.first;
Chris Lattner00950542001-06-06 20:29:01 +0000627
Reid Spencer186a43f2007-03-19 18:39:36 +0000628 Value *TheRealValue = getExistingVal(V->getType(), DID);
629 if (TriggerError)
630 return;
631 if (TheRealValue) {
632 V->replaceAllUsesWith(TheRealValue);
633 delete V;
634 CurModule.PlaceHolderInfo.erase(PHI);
635 } else if (FutureLateResolvers) {
636 // Functions have their unresolved items forwarded to the module late
637 // resolver table
638 InsertValue(V, *FutureLateResolvers);
639 } else {
640 if (DID.Type == ValID::LocalName || DID.Type == ValID::GlobalName) {
641 GenerateError("Reference to an invalid definition: '" +DID.getName()+
642 "' of type '" + V->getType()->getDescription() + "'",
643 PHI->second.second);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000644 return;
Chris Lattner386a3b72001-10-16 19:54:17 +0000645 } else {
Reid Spencer186a43f2007-03-19 18:39:36 +0000646 GenerateError("Reference to an invalid definition: #" +
647 itostr(DID.Num) + " of type '" +
648 V->getType()->getDescription() + "'",
649 PHI->second.second);
650 return;
Chris Lattner30c89792001-09-07 16:35:17 +0000651 }
Chris Lattner00950542001-06-06 20:29:01 +0000652 }
653 }
Chris Lattner00950542001-06-06 20:29:01 +0000654 LateResolvers.clear();
655}
656
Chris Lattner4a42e902001-10-22 05:56:09 +0000657// ResolveTypeTo - A brand new type was just declared. This means that (if
658// name is not null) things referencing Name can be resolved. Otherwise, things
659// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000660//
Chris Lattner4a42e902001-10-22 05:56:09 +0000661static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattner355ad1f2005-03-21 06:27:42 +0000662 ValID D;
Reid Spencerb2d17862007-01-26 08:04:51 +0000663 if (Name) D = ValID::createLocalName(Name);
664 else D = ValID::createLocalID(CurModule.Types.size());
Chris Lattner00950542001-06-06 20:29:01 +0000665
Reid Spencerb78b9082006-11-28 07:28:14 +0000666 std::map<ValID, PATypeHolder>::iterator I =
Chris Lattner355ad1f2005-03-21 06:27:42 +0000667 CurModule.LateResolveTypes.find(D);
668 if (I != CurModule.LateResolveTypes.end()) {
Reid Spencerb78b9082006-11-28 07:28:14 +0000669 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattner355ad1f2005-03-21 06:27:42 +0000670 CurModule.LateResolveTypes.erase(I);
Chris Lattner30c89792001-09-07 16:35:17 +0000671 }
672}
673
Chris Lattner1781aca2001-09-18 04:00:54 +0000674// setValueName - Set the specified value to the name given. The name may be
675// null potentially, in which case this is a noop. The string passed in is
Chris Lattner8ab406d2004-07-13 07:59:27 +0000676// assumed to be a malloc'd string buffer, and is free'd by this function.
677//
678static void setValueName(Value *V, char *NameStr) {
Reid Spencerb2d17862007-01-26 08:04:51 +0000679 if (!NameStr) return;
680 std::string Name(NameStr); // Copy string
681 free(NameStr); // Free old string
Chris Lattnerc9aea522004-07-13 08:12:39 +0000682
Reid Spencerb2d17862007-01-26 08:04:51 +0000683 if (V->getType() == Type::VoidTy) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000684 GenerateError("Can't assign name '" + Name+"' to value with void type");
Reid Spencerb2d17862007-01-26 08:04:51 +0000685 return;
Chris Lattner8ab406d2004-07-13 07:59:27 +0000686 }
Reid Spencerb2d17862007-01-26 08:04:51 +0000687
Reid Spencer1755a9a2007-02-05 17:01:20 +0000688 assert(inFunctionScope() && "Must be in function scope!");
Reid Spenceref9b9a72007-02-05 20:47:22 +0000689 ValueSymbolTable &ST = CurFun.CurrentFunction->getValueSymbolTable();
690 if (ST.lookup(Name)) {
Reid Spencerb2d17862007-01-26 08:04:51 +0000691 GenerateError("Redefinition of value '" + Name + "' of type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +0000692 V->getType()->getDescription() + "'");
Reid Spencerb2d17862007-01-26 08:04:51 +0000693 return;
694 }
695
696 // Set the name.
697 V->setName(Name);
Chris Lattner8ab406d2004-07-13 07:59:27 +0000698}
699
Chris Lattnerd88998d2004-07-14 08:23:52 +0000700/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
701/// this is a declaration, otherwise it is a definition.
Chris Lattnerb2b96672005-11-12 18:21:21 +0000702static GlobalVariable *
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000703ParseGlobalVariable(char *NameStr,
704 GlobalValue::LinkageTypes Linkage,
705 GlobalValue::VisibilityTypes Visibility,
Chris Lattnerb2b96672005-11-12 18:21:21 +0000706 bool isConstantGlobal, const Type *Ty,
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +0000707 Constant *Initializer, bool IsThreadLocal) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000708 if (isa<FunctionType>(Ty)) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000709 GenerateError("Cannot declare global vars of function type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000710 return 0;
711 }
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000712
Misha Brukman5a2a3822005-05-10 22:02:28 +0000713 const PointerType *PTy = PointerType::get(Ty);
Chris Lattnera09000d2004-07-14 23:03:46 +0000714
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000715 std::string Name;
716 if (NameStr) {
717 Name = NameStr; // Copy string
718 free(NameStr); // Free old string
Chris Lattnerd88998d2004-07-14 08:23:52 +0000719 }
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000720
Chris Lattner8a327842004-07-14 21:44:00 +0000721 // See if this global value was forward referenced. If so, recycle the
722 // object.
Misha Brukman5a2a3822005-05-10 22:02:28 +0000723 ValID ID;
Chris Lattner8a327842004-07-14 21:44:00 +0000724 if (!Name.empty()) {
Reid Spencerb2d17862007-01-26 08:04:51 +0000725 ID = ValID::createGlobalName((char*)Name.c_str());
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000726 } else {
Reid Spencer186a43f2007-03-19 18:39:36 +0000727 ID = ValID::createGlobalID(CurModule.Values.size());
Chris Lattner8a327842004-07-14 21:44:00 +0000728 }
729
730 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000731 // Move the global to the end of the list, from whereever it was
Chris Lattner8a327842004-07-14 21:44:00 +0000732 // previously inserted.
733 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
734 CurModule.CurrentModule->getGlobalList().remove(GV);
735 CurModule.CurrentModule->getGlobalList().push_back(GV);
736 GV->setInitializer(Initializer);
737 GV->setLinkage(Linkage);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000738 GV->setVisibility(Visibility);
Chris Lattner8a327842004-07-14 21:44:00 +0000739 GV->setConstant(isConstantGlobal);
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +0000740 GV->setThreadLocal(IsThreadLocal);
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000741 InsertValue(GV, CurModule.Values);
Chris Lattnerb2b96672005-11-12 18:21:21 +0000742 return GV;
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000743 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000744
Reid Spenceref9b9a72007-02-05 20:47:22 +0000745 // If this global has a name
Chris Lattnera09000d2004-07-14 23:03:46 +0000746 if (!Name.empty()) {
Reid Spenceref9b9a72007-02-05 20:47:22 +0000747 // if the global we're parsing has an initializer (is a definition) and
748 // has external linkage.
749 if (Initializer && Linkage != GlobalValue::InternalLinkage)
750 // If there is already a global with external linkage with this name
751 if (CurModule.CurrentModule->getGlobalVariable(Name, false)) {
752 // If we allow this GVar to get created, it will be renamed in the
753 // symbol table because it conflicts with an existing GVar. We can't
754 // allow redefinition of GVars whose linking indicates that their name
755 // must stay the same. Issue the error.
756 GenerateError("Redefinition of global variable named '" + Name +
757 "' of type '" + Ty->getDescription() + "'");
758 return 0;
759 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000760 }
761
762 // Otherwise there is no existing GV to use, create one now.
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000763 GlobalVariable *GV =
Misha Brukman5a2a3822005-05-10 22:02:28 +0000764 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +0000765 CurModule.CurrentModule, IsThreadLocal);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000766 GV->setVisibility(Visibility);
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000767 InsertValue(GV, CurModule.Values);
Chris Lattnerb2b96672005-11-12 18:21:21 +0000768 return GV;
Chris Lattnerd88998d2004-07-14 08:23:52 +0000769}
Chris Lattner8ab406d2004-07-13 07:59:27 +0000770
Reid Spencer75719bf2004-05-26 21:48:31 +0000771// setTypeName - Set the specified type to the name given. The name may be
772// null potentially, in which case this is a noop. The string passed in is
773// assumed to be a malloc'd string buffer, and is freed by this function.
774//
775// This function returns true if the type has already been defined, but is
776// allowed to be redefined in the specified context. If the name is a new name
777// for the type plane, it is inserted and false is returned.
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000778static bool setTypeName(const Type *T, char *NameStr) {
Reid Spencer1755a9a2007-02-05 17:01:20 +0000779 assert(!inFunctionScope() && "Can't give types function-local names!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000780 if (NameStr == 0) return false;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000781
Reid Spencer75719bf2004-05-26 21:48:31 +0000782 std::string Name(NameStr); // Copy string
783 free(NameStr); // Free old string
784
785 // We don't allow assigning names to void type
Reid Spencer5b7e7532006-09-28 19:28:24 +0000786 if (T == Type::VoidTy) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000787 GenerateError("Can't assign name '" + Name + "' to the void type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000788 return false;
789 }
Reid Spencer75719bf2004-05-26 21:48:31 +0000790
Chris Lattnera09000d2004-07-14 23:03:46 +0000791 // Set the type name, checking for conflicts as we do so.
792 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000793
Chris Lattnera09000d2004-07-14 23:03:46 +0000794 if (AlreadyExists) { // Inserting a name that is already defined???
795 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
Reid Spencer1755a9a2007-02-05 17:01:20 +0000796 assert(Existing && "Conflict but no matching type?!");
Chris Lattnera09000d2004-07-14 23:03:46 +0000797
Reid Spencer75719bf2004-05-26 21:48:31 +0000798 // There is only one case where this is allowed: when we are refining an
799 // opaque type. In this case, Existing will be an opaque type.
800 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
801 // We ARE replacing an opaque type!
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000802 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000803 return true;
804 }
805
806 // Otherwise, this is an attempt to redefine a type. That's okay if
807 // the redefinition is identical to the original. This will be so if
808 // Existing and T point to the same Type object. In this one case we
809 // allow the equivalent redefinition.
810 if (Existing == T) return true; // Yes, it's equal.
811
812 // Any other kind of (non-equivalent) redefinition is an error.
Reid Spencer90e2f632007-01-05 21:50:38 +0000813 GenerateError("Redefinition of type named '" + Name + "' of type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +0000814 T->getDescription() + "'");
Reid Spencer75719bf2004-05-26 21:48:31 +0000815 }
816
Reid Spencer75719bf2004-05-26 21:48:31 +0000817 return false;
818}
Chris Lattner8896eda2001-07-09 19:38:36 +0000819
Chris Lattner30c89792001-09-07 16:35:17 +0000820//===----------------------------------------------------------------------===//
821// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000822//
Chris Lattner8896eda2001-07-09 19:38:36 +0000823
Chris Lattner3b073862004-02-09 03:19:29 +0000824// TypeContains - Returns true if Ty directly contains E in it.
Chris Lattner30c89792001-09-07 16:35:17 +0000825//
826static bool TypeContains(const Type *Ty, const Type *E) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000827 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
Chris Lattner6d8dbec2004-12-08 16:13:53 +0000828 E) != Ty->subtype_end();
Chris Lattner3b073862004-02-09 03:19:29 +0000829}
830
831namespace {
832 struct UpRefRecord {
833 // NestingLevel - The number of nesting levels that need to be popped before
834 // this type is resolved.
835 unsigned NestingLevel;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000836
Chris Lattner3b073862004-02-09 03:19:29 +0000837 // LastContainedTy - This is the type at the current binding level for the
838 // type. Every time we reduce the nesting level, this gets updated.
839 const Type *LastContainedTy;
840
841 // UpRefTy - This is the actual opaque type that the upreference is
842 // represented with.
843 OpaqueType *UpRefTy;
844
845 UpRefRecord(unsigned NL, OpaqueType *URTy)
846 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
847 };
Chris Lattner30c89792001-09-07 16:35:17 +0000848}
Chris Lattner698b56e2001-07-20 19:15:08 +0000849
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000850// UpRefs - A list of the outstanding upreferences that need to be resolved.
Chris Lattner3b073862004-02-09 03:19:29 +0000851static std::vector<UpRefRecord> UpRefs;
Chris Lattner30c89792001-09-07 16:35:17 +0000852
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000853/// HandleUpRefs - Every time we finish a new layer of types, this function is
854/// called. It loops through the UpRefs vector, which is a list of the
855/// currently active types. For each type, if the up reference is contained in
856/// the newly completed type, we decrement the level count. When the level
857/// count reaches zero, the upreferenced type is the type that is passed in:
858/// thus we can complete the cycle.
859///
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000860static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner703e92f2006-08-18 17:34:24 +0000861 // If Ty isn't abstract, or if there are no up-references in it, then there is
862 // nothing to resolve here.
863 if (!ty->isAbstract() || UpRefs.empty()) return ty;
864
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000865 PATypeHolder Ty(ty);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000866 UR_OUT("Type '" << Ty->getDescription() <<
Chris Lattner5084d032001-11-02 07:46:26 +0000867 "' newly formed. Resolving upreferences.\n" <<
868 UpRefs.size() << " upreferences active!\n");
Chris Lattner026a8ce2004-02-09 18:53:54 +0000869
870 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
871 // to zero), we resolve them all together before we resolve them to Ty. At
872 // the end of the loop, if there is anything to resolve to Ty, it will be in
873 // this variable.
874 OpaqueType *TypeToResolve = 0;
875
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000876 for (unsigned i = 0; i != UpRefs.size(); ++i) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000877 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
878 << UpRefs[i].second->getDescription() << ") = "
Reid Spencer3271ed52004-07-18 00:08:11 +0000879 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
Chris Lattner3b073862004-02-09 03:19:29 +0000880 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
881 // Decrement level of upreference
882 unsigned Level = --UpRefs[i].NestingLevel;
883 UpRefs[i].LastContainedTy = Ty;
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000884 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
Misha Brukman5a2a3822005-05-10 22:02:28 +0000885 if (Level == 0) { // Upreference should be resolved!
Chris Lattner026a8ce2004-02-09 18:53:54 +0000886 if (!TypeToResolve) {
887 TypeToResolve = UpRefs[i].UpRefTy;
888 } else {
889 UR_OUT(" * Resolving upreference for "
890 << UpRefs[i].second->getDescription() << "\n";
891 std::string OldName = UpRefs[i].UpRefTy->getDescription());
892 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
893 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
894 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
895 }
Reid Spencer3271ed52004-07-18 00:08:11 +0000896 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000897 --i; // Do not skip the next element...
Chris Lattner30c89792001-09-07 16:35:17 +0000898 }
899 }
Chris Lattner8896eda2001-07-09 19:38:36 +0000900 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000901
Chris Lattner026a8ce2004-02-09 18:53:54 +0000902 if (TypeToResolve) {
903 UR_OUT(" * Resolving upreference for "
904 << UpRefs[i].second->getDescription() << "\n";
Chris Lattner16af11d2004-02-09 21:03:38 +0000905 std::string OldName = TypeToResolve->getDescription());
Chris Lattner026a8ce2004-02-09 18:53:54 +0000906 TypeToResolve->refineAbstractTypeTo(Ty);
907 }
908
Chris Lattner8896eda2001-07-09 19:38:36 +0000909 return Ty;
910}
911
Chris Lattner00950542001-06-06 20:29:01 +0000912//===----------------------------------------------------------------------===//
913// RunVMAsmParser - Define an interface to this parser
914//===----------------------------------------------------------------------===//
915//
Reid Spencere1553cc2006-12-31 05:40:12 +0000916static Module* RunParser(Module * M);
917
Chris Lattner86427632004-07-14 06:39:48 +0000918Module *llvm::RunVMAsmParser(const std::string &Filename, FILE *F) {
Chris Lattner6184feb2005-05-20 03:25:47 +0000919 set_scan_file(F);
920
Chris Lattnera2850432001-07-22 18:36:00 +0000921 CurFilename = Filename;
Chris Lattner6184feb2005-05-20 03:25:47 +0000922 return RunParser(new Module(CurFilename));
923}
Chris Lattner00950542001-06-06 20:29:01 +0000924
Chris Lattner6184feb2005-05-20 03:25:47 +0000925Module *llvm::RunVMAsmParser(const char * AsmString, Module * M) {
926 set_scan_string(AsmString);
Chris Lattner42570982003-11-26 07:24:58 +0000927
Chris Lattner6184feb2005-05-20 03:25:47 +0000928 CurFilename = "from_memory";
929 if (M == NULL) {
930 return RunParser(new Module (CurFilename));
931 } else {
932 return RunParser(M);
933 }
Chris Lattner00950542001-06-06 20:29:01 +0000934}
935
936%}
937
938%union {
Brian Gaeked0fde302003-11-11 22:41:34 +0000939 llvm::Module *ModuleVal;
940 llvm::Function *FunctionVal;
Brian Gaeked0fde302003-11-11 22:41:34 +0000941 llvm::BasicBlock *BasicBlockVal;
942 llvm::TerminatorInst *TermInstVal;
943 llvm::Instruction *InstVal;
Reid Spencer08de34b2006-12-03 05:45:44 +0000944 llvm::Constant *ConstVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000945
Reid Spencer08de34b2006-12-03 05:45:44 +0000946 const llvm::Type *PrimType;
Reid Spencere1553cc2006-12-31 05:40:12 +0000947 std::list<llvm::PATypeHolder> *TypeList;
Reid Spencer08de34b2006-12-03 05:45:44 +0000948 llvm::PATypeHolder *TypeVal;
949 llvm::Value *ValueVal;
Reid Spencer08de34b2006-12-03 05:45:44 +0000950 std::vector<llvm::Value*> *ValueList;
Reid Spencere1553cc2006-12-31 05:40:12 +0000951 llvm::ArgListType *ArgList;
952 llvm::TypeWithAttrs TypeWithAttrs;
953 llvm::TypeWithAttrsList *TypeWithAttrsList;
954 llvm::ValueRefList *ValueRefList;
955
Misha Brukman5a2a3822005-05-10 22:02:28 +0000956 // Represent the RHS of PHI node
Reid Spencer08de34b2006-12-03 05:45:44 +0000957 std::list<std::pair<llvm::Value*,
958 llvm::BasicBlock*> > *PHIList;
Brian Gaeked0fde302003-11-11 22:41:34 +0000959 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
Reid Spencer08de34b2006-12-03 05:45:44 +0000960 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000961
Brian Gaeked0fde302003-11-11 22:41:34 +0000962 llvm::GlobalValue::LinkageTypes Linkage;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000963 llvm::GlobalValue::VisibilityTypes Visibility;
Reid Spencer5694b6e2007-04-09 06:17:21 +0000964 uint16_t ParamAttrs;
Reid Spencerf2310042007-02-28 02:23:44 +0000965 llvm::APInt *APIntVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000966 int64_t SInt64Val;
967 uint64_t UInt64Val;
968 int SIntVal;
969 unsigned UIntVal;
970 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000971 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000972
Chris Lattner30c89792001-09-07 16:35:17 +0000973 char *StrVal; // This memory is strdup'd!
Reid Spencer1628cec2006-10-26 06:15:43 +0000974 llvm::ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000975
Reid Spencer08de34b2006-12-03 05:45:44 +0000976 llvm::Instruction::BinaryOps BinaryOpVal;
977 llvm::Instruction::TermOps TermOpVal;
978 llvm::Instruction::MemoryOps MemOpVal;
979 llvm::Instruction::CastOps CastOpVal;
980 llvm::Instruction::OtherOps OtherOpVal;
Reid Spencer08de34b2006-12-03 05:45:44 +0000981 llvm::ICmpInst::Predicate IPredicate;
982 llvm::FCmpInst::Predicate FPredicate;
Chris Lattner00950542001-06-06 20:29:01 +0000983}
984
Reid Spencere1553cc2006-12-31 05:40:12 +0000985%type <ModuleVal> Module
Chris Lattner79df7c02002-03-26 18:01:55 +0000986%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000987%type <BasicBlockVal> BasicBlock InstructionList
988%type <TermInstVal> BBTerminatorInst
989%type <InstVal> Inst InstVal MemoryInst
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000990%type <ConstVal> ConstVal ConstExpr
Chris Lattner6cdb0112001-11-26 16:54:11 +0000991%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +0000992%type <ArgList> ArgList ArgListH
Chris Lattnerc24d2082001-06-11 15:04:20 +0000993%type <PHIList> PHIList
Reid Spencere1553cc2006-12-31 05:40:12 +0000994%type <ValueRefList> ValueRefList // For call param lists & GEP indices
995%type <ValueList> IndexList // For GEP indices
996%type <TypeList> TypeListI
997%type <TypeWithAttrsList> ArgTypeList ArgTypeListI
Reid Spencer2c261782007-01-05 17:06:19 +0000998%type <TypeWithAttrs> ArgType
Chris Lattner00950542001-06-06 20:29:01 +0000999%type <JumpTable> JumpTable
Chris Lattner4ad02e72003-04-16 20:28:45 +00001000%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00001001%type <BoolVal> ThreadLocal // 'thread_local' or not
Chris Lattner15c9c032003-09-08 18:20:29 +00001002%type <BoolVal> OptVolatile // 'volatile' or not
Chris Lattnerddb6db42005-05-06 05:51:46 +00001003%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
Chris Lattneraa2c8532006-01-25 22:26:43 +00001004%type <BoolVal> OptSideEffect // 'sideeffect' or not.
Reid Spencere1553cc2006-12-31 05:40:12 +00001005%type <Linkage> GVInternalLinkage GVExternalLinkage
1006%type <Linkage> FunctionDefineLinkage FunctionDeclareLinkage
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001007%type <Linkage> AliasLinkage
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001008%type <Visibility> GVVisibilityStyle
Chris Lattner00950542001-06-06 20:29:01 +00001009
Chris Lattner2079fde2001-10-13 06:41:08 +00001010// ValueRef - Unresolved reference to a definition or BB
1011%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001012%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +00001013// Tokens and types for handling constant integer values
1014//
1015// ESINT64VAL - A negative number within long long range
1016%token <SInt64Val> ESINT64VAL
1017
1018// EUINT64VAL - A positive number within uns. long long range
1019%token <UInt64Val> EUINT64VAL
Chris Lattner00950542001-06-06 20:29:01 +00001020
Reid Spencerf2310042007-02-28 02:23:44 +00001021// ESAPINTVAL - A negative number with arbitrary precision
1022%token <APIntVal> ESAPINTVAL
1023
1024// EUAPINTVAL - A positive number with arbitrary precision
1025%token <APIntVal> EUAPINTVAL
1026
Reid Spencerb2d17862007-01-26 08:04:51 +00001027%token <UIntVal> LOCALVAL_ID GLOBALVAL_ID // %123 @123
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001028%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +00001029
1030// Built in types...
Reid Spencer2c261782007-01-05 17:06:19 +00001031%type <TypeVal> Types ResultTypes
Reid Spencere1553cc2006-12-31 05:40:12 +00001032%type <PrimType> IntType FPType PrimType // Classifications
Reid Spencer8088e9d2007-01-13 05:00:20 +00001033%token <PrimType> VOID INTTYPE
Reid Spencerb951bc02006-12-29 20:29:48 +00001034%token <PrimType> FLOAT DOUBLE LABEL
1035%token TYPE
Chris Lattner00950542001-06-06 20:29:01 +00001036
Reid Spencerb2d17862007-01-26 08:04:51 +00001037%token<StrVal> LOCALVAR GLOBALVAR LABELSTR STRINGCONSTANT ATSTRINGCONSTANT
1038%type <StrVal> LocalName OptLocalName OptLocalAssign
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001039%type <StrVal> GlobalName OptGlobalAssign GlobalAssign
Reid Spencerb2d17862007-01-26 08:04:51 +00001040%type <UIntVal> OptAlign OptCAlign
Chris Lattnerb2b96672005-11-12 18:21:21 +00001041%type <StrVal> OptSection SectionString
Chris Lattner00950542001-06-06 20:29:01 +00001042
Reid Spencer744d0362007-04-09 01:55:42 +00001043%token ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001044%token DECLARE DEFINE GLOBAL CONSTANT SECTION ALIAS VOLATILE THREAD_LOCAL
Reid Spencere1553cc2006-12-31 05:40:12 +00001045%token TO DOTDOTDOT NULL_TOK UNDEF INTERNAL LINKONCE WEAK APPENDING
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00001046%token DLLIMPORT DLLEXPORT EXTERN_WEAK
Reid Spencerb2d17862007-01-26 08:04:51 +00001047%token OPAQUE EXTERNAL TARGET TRIPLE ALIGN
Chris Lattneraa2c8532006-01-25 22:26:43 +00001048%token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001049%token CC_TOK CCC_TOK FASTCC_TOK COLDCC_TOK X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
Chris Lattner10b27112006-10-22 06:07:41 +00001050%token DATALAYOUT
Chris Lattnera8e8f162005-05-06 20:27:19 +00001051%type <UIntVal> OptCallingConv
Reid Spencer2c261782007-01-05 17:06:19 +00001052%type <ParamAttrs> OptParamAttrs ParamAttr
1053%type <ParamAttrs> OptFuncAttrs FuncAttr
Chris Lattner00950542001-06-06 20:29:01 +00001054
Misha Brukman5a2a3822005-05-10 22:02:28 +00001055// Basic Block Terminating Operators
Chris Lattner16710e92004-10-16 18:17:13 +00001056%token <TermOpVal> RET BR SWITCH INVOKE UNWIND UNREACHABLE
Chris Lattner00950542001-06-06 20:29:01 +00001057
Misha Brukman5a2a3822005-05-10 22:02:28 +00001058// Binary Operators
Reid Spencere4d87aa2006-12-23 06:05:41 +00001059%type <BinaryOpVal> ArithmeticOps LogicalOps // Binops Subcatagories
Reid Spencer0a783f72006-11-02 01:53:59 +00001060%token <BinaryOpVal> ADD SUB MUL UDIV SDIV FDIV UREM SREM FREM AND OR XOR
Reid Spencer832254e2007-02-02 02:16:23 +00001061%token <BinaryOpVal> SHL LSHR ASHR
1062
Reid Spencer08de34b2006-12-03 05:45:44 +00001063%token <OtherOpVal> ICMP FCMP
Reid Spencer08de34b2006-12-03 05:45:44 +00001064%type <IPredicate> IPredicates
Reid Spencer08de34b2006-12-03 05:45:44 +00001065%type <FPredicate> FPredicates
Reid Spencer9f746f42006-12-03 06:58:07 +00001066%token EQ NE SLT SGT SLE SGE ULT UGT ULE UGE
1067%token OEQ ONE OLT OGT OLE OGE ORD UNO UEQ UNE
Chris Lattner00950542001-06-06 20:29:01 +00001068
1069// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001070%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +00001071
Reid Spencer3da59db2006-11-27 01:05:10 +00001072// Cast Operators
1073%type <CastOpVal> CastOps
1074%token <CastOpVal> TRUNC ZEXT SEXT FPTRUNC FPEXT BITCAST
1075%token <CastOpVal> UITOFP SITOFP FPTOUI FPTOSI INTTOPTR PTRTOINT
1076
Chris Lattner027dcc52001-07-08 21:10:27 +00001077// Other Operators
Reid Spencer832254e2007-02-02 02:16:23 +00001078%token <OtherOpVal> PHI_TOK SELECT VAARG
Chris Lattner4c949082006-04-08 01:18:35 +00001079%token <OtherOpVal> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
Chris Lattnerddb6db42005-05-06 05:51:46 +00001080
Reid Spencer2c261782007-01-05 17:06:19 +00001081// Function Attributes
Reid Spencera39dfd32007-03-22 02:13:23 +00001082%token NORETURN INREG SRET NOUNWIND
Chris Lattner027dcc52001-07-08 21:10:27 +00001083
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001084// Visibility Styles
1085%token DEFAULT HIDDEN
1086
Chris Lattner00950542001-06-06 20:29:01 +00001087%start Module
1088%%
1089
Chris Lattner00950542001-06-06 20:29:01 +00001090
Misha Brukman5a2a3822005-05-10 22:02:28 +00001091// Operations that are notably excluded from this list include:
Chris Lattner00950542001-06-06 20:29:01 +00001092// RET, BR, & SWITCH because they end basic blocks and are treated specially.
1093//
Reid Spencer0a783f72006-11-02 01:53:59 +00001094ArithmeticOps: ADD | SUB | MUL | UDIV | SDIV | FDIV | UREM | SREM | FREM;
Reid Spencer832254e2007-02-02 02:16:23 +00001095LogicalOps : SHL | LSHR | ASHR | AND | OR | XOR;
Reid Spencer3da59db2006-11-27 01:05:10 +00001096CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | BITCAST |
1097 UITOFP | SITOFP | FPTOUI | FPTOSI | INTTOPTR | PTRTOINT;
Reid Spencer832254e2007-02-02 02:16:23 +00001098
Reid Spencer9f746f42006-12-03 06:58:07 +00001099IPredicates
Reid Spencer763ed5e2006-12-04 05:20:06 +00001100 : EQ { $$ = ICmpInst::ICMP_EQ; } | NE { $$ = ICmpInst::ICMP_NE; }
Reid Spencer9f746f42006-12-03 06:58:07 +00001101 | SLT { $$ = ICmpInst::ICMP_SLT; } | SGT { $$ = ICmpInst::ICMP_SGT; }
1102 | SLE { $$ = ICmpInst::ICMP_SLE; } | SGE { $$ = ICmpInst::ICMP_SGE; }
1103 | ULT { $$ = ICmpInst::ICMP_ULT; } | UGT { $$ = ICmpInst::ICMP_UGT; }
1104 | ULE { $$ = ICmpInst::ICMP_ULE; } | UGE { $$ = ICmpInst::ICMP_UGE; }
1105 ;
1106
1107FPredicates
1108 : OEQ { $$ = FCmpInst::FCMP_OEQ; } | ONE { $$ = FCmpInst::FCMP_ONE; }
1109 | OLT { $$ = FCmpInst::FCMP_OLT; } | OGT { $$ = FCmpInst::FCMP_OGT; }
1110 | OLE { $$ = FCmpInst::FCMP_OLE; } | OGE { $$ = FCmpInst::FCMP_OGE; }
1111 | ORD { $$ = FCmpInst::FCMP_ORD; } | UNO { $$ = FCmpInst::FCMP_UNO; }
1112 | UEQ { $$ = FCmpInst::FCMP_UEQ; } | UNE { $$ = FCmpInst::FCMP_UNE; }
1113 | ULT { $$ = FCmpInst::FCMP_ULT; } | UGT { $$ = FCmpInst::FCMP_UGT; }
1114 | ULE { $$ = FCmpInst::FCMP_ULE; } | UGE { $$ = FCmpInst::FCMP_UGE; }
1115 | TRUETOK { $$ = FCmpInst::FCMP_TRUE; }
1116 | FALSETOK { $$ = FCmpInst::FCMP_FALSE; }
1117 ;
Chris Lattner00950542001-06-06 20:29:01 +00001118
Chris Lattnere98dda62001-07-14 06:10:16 +00001119// These are some types that allow classification if we only want a particular
1120// thing... for example, only a signed, unsigned, or integral type.
Reid Spencera54b7cb2007-01-12 07:05:14 +00001121IntType : INTTYPE;
Chris Lattner51727be2002-06-04 21:58:56 +00001122FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +00001123
Reid Spencerb2d17862007-01-26 08:04:51 +00001124LocalName : LOCALVAR | STRINGCONSTANT;
1125OptLocalName : LocalName | /*empty*/ { $$ = 0; };
1126
1127/// OptLocalAssign - Value producing statements have an optional assignment
1128/// component.
1129OptLocalAssign : LocalName '=' {
1130 $$ = $1;
1131 CHECK_FOR_ERROR
1132 }
1133 | /*empty*/ {
1134 $$ = 0;
1135 CHECK_FOR_ERROR
1136 };
1137
1138GlobalName : GLOBALVAR | ATSTRINGCONSTANT;
1139
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001140OptGlobalAssign : GlobalAssign
Misha Brukman5a2a3822005-05-10 22:02:28 +00001141 | /*empty*/ {
1142 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00001143 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001144 };
Chris Lattner00950542001-06-06 20:29:01 +00001145
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001146GlobalAssign : GlobalName '=' {
1147 $$ = $1;
1148 CHECK_FOR_ERROR
Anton Korobeynikovc0fabcb2007-04-25 18:07:40 +00001149 };
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001150
Reid Spencerb951bc02006-12-29 20:29:48 +00001151GVInternalLinkage
1152 : INTERNAL { $$ = GlobalValue::InternalLinkage; }
1153 | WEAK { $$ = GlobalValue::WeakLinkage; }
1154 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1155 | APPENDING { $$ = GlobalValue::AppendingLinkage; }
1156 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
1157 ;
1158
1159GVExternalLinkage
1160 : DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1161 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
1162 | EXTERNAL { $$ = GlobalValue::ExternalLinkage; }
1163 ;
1164
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001165GVVisibilityStyle
1166 : /*empty*/ { $$ = GlobalValue::DefaultVisibility; }
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001167 | DEFAULT { $$ = GlobalValue::DefaultVisibility; }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001168 | HIDDEN { $$ = GlobalValue::HiddenVisibility; }
1169 ;
1170
Reid Spencere1553cc2006-12-31 05:40:12 +00001171FunctionDeclareLinkage
1172 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1173 | DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1174 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
Reid Spencerb951bc02006-12-29 20:29:48 +00001175 ;
1176
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001177FunctionDefineLinkage
Reid Spencere1553cc2006-12-31 05:40:12 +00001178 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1179 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
Reid Spencerb951bc02006-12-29 20:29:48 +00001180 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1181 | WEAK { $$ = GlobalValue::WeakLinkage; }
1182 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
Reid Spencerb951bc02006-12-29 20:29:48 +00001183 ;
Chris Lattner30c89792001-09-07 16:35:17 +00001184
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001185AliasLinkage
1186 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1187 | WEAK { $$ = GlobalValue::WeakLinkage; }
1188 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
1189 ;
1190
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00001191OptCallingConv : /*empty*/ { $$ = CallingConv::C; } |
1192 CCC_TOK { $$ = CallingConv::C; } |
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00001193 FASTCC_TOK { $$ = CallingConv::Fast; } |
1194 COLDCC_TOK { $$ = CallingConv::Cold; } |
1195 X86_STDCALLCC_TOK { $$ = CallingConv::X86_StdCall; } |
1196 X86_FASTCALLCC_TOK { $$ = CallingConv::X86_FastCall; } |
1197 CC_TOK EUINT64VAL {
Chris Lattnera8e8f162005-05-06 20:27:19 +00001198 if ((unsigned)$2 != $2)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001199 GEN_ERROR("Calling conv too large");
Chris Lattnera8e8f162005-05-06 20:27:19 +00001200 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001201 CHECK_FOR_ERROR
Chris Lattnera8e8f162005-05-06 20:27:19 +00001202 };
1203
Reid Spencer18da0722007-04-11 02:44:20 +00001204ParamAttr : ZEXT { $$ = ParamAttr::ZExt; }
1205 | SEXT { $$ = ParamAttr::SExt; }
1206 | INREG { $$ = ParamAttr::InReg; }
1207 | SRET { $$ = ParamAttr::StructRet; }
Reid Spencere1553cc2006-12-31 05:40:12 +00001208 ;
1209
Reid Spencer18da0722007-04-11 02:44:20 +00001210OptParamAttrs : /* empty */ { $$ = ParamAttr::None; }
Reid Spencer2c261782007-01-05 17:06:19 +00001211 | OptParamAttrs ParamAttr {
Reid Spencer5694b6e2007-04-09 06:17:21 +00001212 $$ = $1 | $2;
Reid Spencere1553cc2006-12-31 05:40:12 +00001213 }
1214 ;
1215
Reid Spencer18da0722007-04-11 02:44:20 +00001216FuncAttr : NORETURN { $$ = ParamAttr::NoReturn; }
1217 | NOUNWIND { $$ = ParamAttr::NoUnwind; }
Reid Spencer2c261782007-01-05 17:06:19 +00001218 | ParamAttr
1219 ;
1220
Reid Spencer18da0722007-04-11 02:44:20 +00001221OptFuncAttrs : /* empty */ { $$ = ParamAttr::None; }
Reid Spencer2c261782007-01-05 17:06:19 +00001222 | OptFuncAttrs FuncAttr {
Reid Spencer5694b6e2007-04-09 06:17:21 +00001223 $$ = $1 | $2;
Reid Spencer2c261782007-01-05 17:06:19 +00001224 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001225 ;
1226
Chris Lattner66db8e42005-11-06 06:34:12 +00001227// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1228// a comma before it.
Chris Lattner87ac9722005-11-06 06:46:28 +00001229OptAlign : /*empty*/ { $$ = 0; } |
Chris Lattnerb2b96672005-11-12 18:21:21 +00001230 ALIGN EUINT64VAL {
1231 $$ = $2;
1232 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001233 GEN_ERROR("Alignment must be a power of two");
Reid Spencer61c83e02006-08-18 08:43:06 +00001234 CHECK_FOR_ERROR
Chris Lattnerb2b96672005-11-12 18:21:21 +00001235};
Chris Lattner66db8e42005-11-06 06:34:12 +00001236OptCAlign : /*empty*/ { $$ = 0; } |
Chris Lattnerb2b96672005-11-12 18:21:21 +00001237 ',' ALIGN EUINT64VAL {
1238 $$ = $3;
1239 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001240 GEN_ERROR("Alignment must be a power of two");
Reid Spencer61c83e02006-08-18 08:43:06 +00001241 CHECK_FOR_ERROR
Chris Lattnerb2b96672005-11-12 18:21:21 +00001242};
1243
Chris Lattner66db8e42005-11-06 06:34:12 +00001244
Chris Lattner164c3782005-11-12 00:11:10 +00001245SectionString : SECTION STRINGCONSTANT {
1246 for (unsigned i = 0, e = strlen($2); i != e; ++i)
1247 if ($2[i] == '"' || $2[i] == '\\')
Reid Spencerf4fa5902007-02-05 10:16:10 +00001248 GEN_ERROR("Invalid character in section name");
Chris Lattner164c3782005-11-12 00:11:10 +00001249 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001250 CHECK_FOR_ERROR
Chris Lattner164c3782005-11-12 00:11:10 +00001251};
1252
1253OptSection : /*empty*/ { $$ = 0; } |
1254 SectionString { $$ = $1; };
Chris Lattner164c3782005-11-12 00:11:10 +00001255
Chris Lattnerb2b96672005-11-12 18:21:21 +00001256// GlobalVarAttributes - Used to pass the attributes string on a global. CurGV
1257// is set to be the global we are processing.
1258//
1259GlobalVarAttributes : /* empty */ {} |
1260 ',' GlobalVarAttribute GlobalVarAttributes {};
1261GlobalVarAttribute : SectionString {
1262 CurGV->setSection($1);
1263 free($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001264 CHECK_FOR_ERROR
Chris Lattnerb2b96672005-11-12 18:21:21 +00001265 }
1266 | ALIGN EUINT64VAL {
1267 if ($2 != 0 && !isPowerOf2_32($2))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001268 GEN_ERROR("Alignment must be a power of two");
Chris Lattnerb2b96672005-11-12 18:21:21 +00001269 CurGV->setAlignment($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001270 CHECK_FOR_ERROR
Chris Lattnerb2b96672005-11-12 18:21:21 +00001271 };
Chris Lattner164c3782005-11-12 00:11:10 +00001272
Chris Lattner30c89792001-09-07 16:35:17 +00001273//===----------------------------------------------------------------------===//
1274// Types includes all predefined types... except void, because it can only be
Reid Spencere1553cc2006-12-31 05:40:12 +00001275// used in specific contexts (function returning void for example).
Chris Lattner30c89792001-09-07 16:35:17 +00001276
1277// Derived types are added later...
1278//
Reid Spencer8088e9d2007-01-13 05:00:20 +00001279PrimType : INTTYPE | FLOAT | DOUBLE | LABEL ;
Reid Spencere1553cc2006-12-31 05:40:12 +00001280
1281Types
1282 : OPAQUE {
Reid Spencer08de34b2006-12-03 05:45:44 +00001283 $$ = new PATypeHolder(OpaqueType::get());
Reid Spencer61c83e02006-08-18 08:43:06 +00001284 CHECK_FOR_ERROR
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001285 }
1286 | PrimType {
Reid Spencer08de34b2006-12-03 05:45:44 +00001287 $$ = new PATypeHolder($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001288 CHECK_FOR_ERROR
Reid Spencere1553cc2006-12-31 05:40:12 +00001289 }
1290 | Types '*' { // Pointer type?
1291 if (*$1 == Type::LabelTy)
1292 GEN_ERROR("Cannot form a pointer to a basic block");
1293 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
1294 delete $1;
1295 CHECK_FOR_ERROR
1296 }
1297 | SymbolicValueRef { // Named types are also simple types...
1298 const Type* tmp = getTypeVal($1);
1299 CHECK_FOR_ERROR
1300 $$ = new PATypeHolder(tmp);
1301 }
1302 | '\\' EUINT64VAL { // Type UpReference
Reid Spencerf4fa5902007-02-05 10:16:10 +00001303 if ($2 > (uint64_t)~0U) GEN_ERROR("Value out of range");
Chris Lattner30c89792001-09-07 16:35:17 +00001304 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
Chris Lattner3b073862004-02-09 03:19:29 +00001305 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Reid Spencer08de34b2006-12-03 05:45:44 +00001306 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +00001307 UR_OUT("New Upreference!\n");
Reid Spencer61c83e02006-08-18 08:43:06 +00001308 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001309 }
Reid Spencer2c261782007-01-05 17:06:19 +00001310 | Types '(' ArgTypeListI ')' OptFuncAttrs {
Chris Lattner9232b992003-04-22 18:42:41 +00001311 std::vector<const Type*> Params;
Reid Spencer4f859aa2007-04-22 05:46:44 +00001312 ParamAttrsVector Attrs;
1313 if ($5 != ParamAttr::None) {
1314 ParamAttrsWithIndex X; X.index = 0; X.attrs = $5;
1315 Attrs.push_back(X);
1316 }
Reid Spencer5694b6e2007-04-09 06:17:21 +00001317 unsigned index = 1;
1318 TypeWithAttrsList::iterator I = $3->begin(), E = $3->end();
1319 for (; I != E; ++I, ++index) {
Reid Spencer2c9df212007-03-20 01:13:00 +00001320 const Type *Ty = I->Ty->get();
Reid Spencer2c9df212007-03-20 01:13:00 +00001321 Params.push_back(Ty);
1322 if (Ty != Type::VoidTy)
Reid Spencer4f859aa2007-04-22 05:46:44 +00001323 if (I->Attrs != ParamAttr::None) {
1324 ParamAttrsWithIndex X; X.index = index; X.attrs = I->Attrs;
1325 Attrs.push_back(X);
1326 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001327 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001328 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1329 if (isVarArg) Params.pop_back();
1330
Reid Spencer5694b6e2007-04-09 06:17:21 +00001331 ParamAttrsList *ActualAttrs = 0;
1332 if (!Attrs.empty())
Reid Spencer4f859aa2007-04-22 05:46:44 +00001333 ActualAttrs = ParamAttrsList::get(Attrs);
Reid Spencer5694b6e2007-04-09 06:17:21 +00001334 FunctionType *FT = FunctionType::get(*$1, Params, isVarArg, ActualAttrs);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001335 delete $3; // Delete the argument list
Reid Spencere1553cc2006-12-31 05:40:12 +00001336 delete $1; // Delete the return type handle
1337 $$ = new PATypeHolder(HandleUpRefs(FT));
Reid Spencer61c83e02006-08-18 08:43:06 +00001338 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001339 }
Reid Spencer2c261782007-01-05 17:06:19 +00001340 | VOID '(' ArgTypeListI ')' OptFuncAttrs {
Reid Spencere1553cc2006-12-31 05:40:12 +00001341 std::vector<const Type*> Params;
Reid Spencer4f859aa2007-04-22 05:46:44 +00001342 ParamAttrsVector Attrs;
1343 if ($5 != ParamAttr::None) {
1344 ParamAttrsWithIndex X; X.index = 0; X.attrs = $5;
1345 Attrs.push_back(X);
1346 }
Reid Spencer5694b6e2007-04-09 06:17:21 +00001347 TypeWithAttrsList::iterator I = $3->begin(), E = $3->end();
1348 unsigned index = 1;
1349 for ( ; I != E; ++I, ++index) {
Reid Spencer2c9df212007-03-20 01:13:00 +00001350 const Type* Ty = I->Ty->get();
Reid Spencer2c9df212007-03-20 01:13:00 +00001351 Params.push_back(Ty);
1352 if (Ty != Type::VoidTy)
Reid Spencer4f859aa2007-04-22 05:46:44 +00001353 if (I->Attrs != ParamAttr::None) {
1354 ParamAttrsWithIndex X; X.index = index; X.attrs = I->Attrs;
1355 Attrs.push_back(X);
1356 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001357 }
1358 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1359 if (isVarArg) Params.pop_back();
1360
Reid Spencer5694b6e2007-04-09 06:17:21 +00001361 ParamAttrsList *ActualAttrs = 0;
1362 if (!Attrs.empty())
Reid Spencer4f859aa2007-04-22 05:46:44 +00001363 ActualAttrs = ParamAttrsList::get(Attrs);
Reid Spencer5694b6e2007-04-09 06:17:21 +00001364
1365 FunctionType *FT = FunctionType::get($1, Params, isVarArg, ActualAttrs);
Reid Spencer2c261782007-01-05 17:06:19 +00001366 delete $3; // Delete the argument list
Reid Spencere1553cc2006-12-31 05:40:12 +00001367 $$ = new PATypeHolder(HandleUpRefs(FT));
1368 CHECK_FOR_ERROR
1369 }
1370
1371 | '[' EUINT64VAL 'x' Types ']' { // Sized array type?
Reid Spencer08de34b2006-12-03 05:45:44 +00001372 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
1373 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00001374 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001375 }
Reid Spencerac9dcb92007-02-15 03:39:18 +00001376 | '<' EUINT64VAL 'x' Types '>' { // Vector type?
Reid Spencer08de34b2006-12-03 05:45:44 +00001377 const llvm::Type* ElemTy = $4->get();
1378 if ((unsigned)$2 != $2)
1379 GEN_ERROR("Unsigned result not equal to signed result");
Chris Lattner42a75512007-01-15 02:27:26 +00001380 if (!ElemTy->isFloatingPoint() && !ElemTy->isInteger())
Reid Spencer9d6565a2007-02-15 02:26:10 +00001381 GEN_ERROR("Element type of a VectorType must be primitive");
Reid Spencer08de34b2006-12-03 05:45:44 +00001382 if (!isPowerOf2_32($2))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001383 GEN_ERROR("Vector length should be a power of 2");
Reid Spencer9d6565a2007-02-15 02:26:10 +00001384 $$ = new PATypeHolder(HandleUpRefs(VectorType::get(*$4, (unsigned)$2)));
Reid Spencer08de34b2006-12-03 05:45:44 +00001385 delete $4;
1386 CHECK_FOR_ERROR
Brian Gaeke715c90b2004-08-20 06:00:58 +00001387 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001388 | '{' TypeListI '}' { // Structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001389 std::vector<const Type*> Elements;
Reid Spencer08de34b2006-12-03 05:45:44 +00001390 for (std::list<llvm::PATypeHolder>::iterator I = $2->begin(),
Chris Lattnera08976b2005-02-22 23:13:58 +00001391 E = $2->end(); I != E; ++I)
Reid Spencer08de34b2006-12-03 05:45:44 +00001392 Elements.push_back(*I);
Misha Brukman5a2a3822005-05-10 22:02:28 +00001393
Reid Spencer08de34b2006-12-03 05:45:44 +00001394 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001395 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001396 CHECK_FOR_ERROR
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001397 }
1398 | '{' '}' { // Empty structure type?
Reid Spencer08de34b2006-12-03 05:45:44 +00001399 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Reid Spencer61c83e02006-08-18 08:43:06 +00001400 CHECK_FOR_ERROR
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001401 }
Andrew Lenharth38ecbf12006-12-08 18:06:16 +00001402 | '<' '{' TypeListI '}' '>' {
1403 std::vector<const Type*> Elements;
1404 for (std::list<llvm::PATypeHolder>::iterator I = $3->begin(),
1405 E = $3->end(); I != E; ++I)
1406 Elements.push_back(*I);
1407
1408 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements, true)));
1409 delete $3;
1410 CHECK_FOR_ERROR
1411 }
1412 | '<' '{' '}' '>' { // Empty structure type?
1413 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>(), true));
1414 CHECK_FOR_ERROR
1415 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001416 ;
1417
1418ArgType
1419 : Types OptParamAttrs {
1420 $$.Ty = $1;
1421 $$.Attrs = $2;
1422 }
1423 ;
1424
Reid Spencer2c261782007-01-05 17:06:19 +00001425ResultTypes
1426 : Types {
Reid Spencere1553cc2006-12-31 05:40:12 +00001427 if (!UpRefs.empty())
1428 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1429 if (!(*$1)->isFirstClassType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001430 GEN_ERROR("LLVM functions cannot return aggregate types");
Reid Spencer2c261782007-01-05 17:06:19 +00001431 $$ = $1;
Reid Spencere1553cc2006-12-31 05:40:12 +00001432 }
Reid Spencer2c261782007-01-05 17:06:19 +00001433 | VOID {
1434 $$ = new PATypeHolder(Type::VoidTy);
Reid Spencere1553cc2006-12-31 05:40:12 +00001435 }
1436 ;
1437
1438ArgTypeList : ArgType {
1439 $$ = new TypeWithAttrsList();
1440 $$->push_back($1);
1441 CHECK_FOR_ERROR
1442 }
1443 | ArgTypeList ',' ArgType {
1444 ($$=$1)->push_back($3);
1445 CHECK_FOR_ERROR
1446 }
1447 ;
1448
1449ArgTypeListI
1450 : ArgTypeList
1451 | ArgTypeList ',' DOTDOTDOT {
1452 $$=$1;
Reid Spencer18da0722007-04-11 02:44:20 +00001453 TypeWithAttrs TWA; TWA.Attrs = ParamAttr::None;
Reid Spencere1553cc2006-12-31 05:40:12 +00001454 TWA.Ty = new PATypeHolder(Type::VoidTy);
1455 $$->push_back(TWA);
1456 CHECK_FOR_ERROR
1457 }
1458 | DOTDOTDOT {
1459 $$ = new TypeWithAttrsList;
Reid Spencer18da0722007-04-11 02:44:20 +00001460 TypeWithAttrs TWA; TWA.Attrs = ParamAttr::None;
Reid Spencere1553cc2006-12-31 05:40:12 +00001461 TWA.Ty = new PATypeHolder(Type::VoidTy);
1462 $$->push_back(TWA);
1463 CHECK_FOR_ERROR
1464 }
1465 | /*empty*/ {
1466 $$ = new TypeWithAttrsList();
Reid Spencer61c83e02006-08-18 08:43:06 +00001467 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001468 };
Chris Lattner30c89792001-09-07 16:35:17 +00001469
Chris Lattner7e708292002-06-25 16:13:24 +00001470// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +00001471// declaration type lists
1472//
Reid Spencere1553cc2006-12-31 05:40:12 +00001473TypeListI : Types {
Reid Spencer08de34b2006-12-03 05:45:44 +00001474 $$ = new std::list<PATypeHolder>();
Reid Spencer2c9df212007-03-20 01:13:00 +00001475 $$->push_back(*$1);
1476 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001477 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001478 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001479 | TypeListI ',' Types {
Reid Spencer2c9df212007-03-20 01:13:00 +00001480 ($$=$1)->push_back(*$3);
1481 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001482 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001483 };
Chris Lattner30c89792001-09-07 16:35:17 +00001484
Chris Lattnere98dda62001-07-14 06:10:16 +00001485// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +00001486// production is used ONLY to represent constants that show up AFTER a 'const',
1487// 'constant' or 'global' token at global scope. Constants that can be inlined
1488// into other expressions (such as integers and constexprs) are handled by the
1489// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +00001490//
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001491ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencere1553cc2006-12-31 05:40:12 +00001492 if (!UpRefs.empty())
1493 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001494 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001495 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001496 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001497 (*$1)->getDescription() + "'");
Chris Lattner30c89792001-09-07 16:35:17 +00001498 const Type *ETy = ATy->getElementType();
1499 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +00001500
Chris Lattner30c89792001-09-07 16:35:17 +00001501 // Verify that we have the correct size...
1502 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001503 GEN_ERROR("Type mismatch: constant sized array initialized with " +
Reid Spencer3271ed52004-07-18 00:08:11 +00001504 utostr($3->size()) + " arguments, but has size of " +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001505 itostr(NumElements) + "");
Chris Lattner00950542001-06-06 20:29:01 +00001506
Chris Lattner30c89792001-09-07 16:35:17 +00001507 // Verify all elements are correct type!
1508 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencer08de34b2006-12-03 05:45:44 +00001509 if (ETy != (*$3)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001510 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Reid Spencer3271ed52004-07-18 00:08:11 +00001511 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencer08de34b2006-12-03 05:45:44 +00001512 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +00001513 }
1514
Reid Spencer08de34b2006-12-03 05:45:44 +00001515 $$ = ConstantArray::get(ATy, *$3);
1516 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001517 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001518 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001519 | Types '[' ']' {
Reid Spencere1553cc2006-12-31 05:40:12 +00001520 if (!UpRefs.empty())
1521 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001522 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001523 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001524 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001525 (*$1)->getDescription() + "'");
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001526
1527 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +00001528 if (NumElements != -1 && NumElements != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001529 GEN_ERROR("Type mismatch: constant sized array initialized with 0"
Reid Spencerf4fa5902007-02-05 10:16:10 +00001530 " arguments, but has size of " + itostr(NumElements) +"");
Reid Spencer08de34b2006-12-03 05:45:44 +00001531 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
1532 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001533 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001534 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001535 | Types 'c' STRINGCONSTANT {
Reid Spencere1553cc2006-12-31 05:40:12 +00001536 if (!UpRefs.empty())
1537 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001538 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001539 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001540 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001541 (*$1)->getDescription() + "'");
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001542
Chris Lattner30c89792001-09-07 16:35:17 +00001543 int NumElements = ATy->getNumElements();
1544 const Type *ETy = ATy->getElementType();
1545 char *EndStr = UnEscapeLexed($3, true);
1546 if (NumElements != -1 && NumElements != (EndStr-$3))
Reid Spencer61c83e02006-08-18 08:43:06 +00001547 GEN_ERROR("Can't build string constant of size " +
Reid Spencer3271ed52004-07-18 00:08:11 +00001548 itostr((int)(EndStr-$3)) +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001549 " when array has size " + itostr(NumElements) + "");
Chris Lattner9232b992003-04-22 18:42:41 +00001550 std::vector<Constant*> Vals;
Reid Spencere1553cc2006-12-31 05:40:12 +00001551 if (ETy == Type::Int8Ty) {
Chris Lattneree454772006-01-23 23:05:15 +00001552 for (unsigned char *C = (unsigned char *)$3;
Reid Spencere1553cc2006-12-31 05:40:12 +00001553 C != (unsigned char*)EndStr; ++C)
1554 Vals.push_back(ConstantInt::get(ETy, *C));
Chris Lattner93750fa2001-07-28 17:48:55 +00001555 } else {
Chris Lattner30c89792001-09-07 16:35:17 +00001556 free($3);
Reid Spencerf4fa5902007-02-05 10:16:10 +00001557 GEN_ERROR("Cannot build string arrays of non byte sized elements");
Chris Lattner93750fa2001-07-28 17:48:55 +00001558 }
Chris Lattner30c89792001-09-07 16:35:17 +00001559 free($3);
Reid Spencer08de34b2006-12-03 05:45:44 +00001560 $$ = ConstantArray::get(ATy, Vals);
1561 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001562 CHECK_FOR_ERROR
Chris Lattner93750fa2001-07-28 17:48:55 +00001563 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00001564 | Types '<' ConstVector '>' { // Nonempty unsized arr
Reid Spencere1553cc2006-12-31 05:40:12 +00001565 if (!UpRefs.empty())
1566 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer9d6565a2007-02-15 02:26:10 +00001567 const VectorType *PTy = dyn_cast<VectorType>($1->get());
Brian Gaeke715c90b2004-08-20 06:00:58 +00001568 if (PTy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001569 GEN_ERROR("Cannot make packed constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001570 (*$1)->getDescription() + "'");
Brian Gaeke715c90b2004-08-20 06:00:58 +00001571 const Type *ETy = PTy->getElementType();
1572 int NumElements = PTy->getNumElements();
1573
1574 // Verify that we have the correct size...
1575 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001576 GEN_ERROR("Type mismatch: constant sized packed initialized with " +
Brian Gaeke715c90b2004-08-20 06:00:58 +00001577 utostr($3->size()) + " arguments, but has size of " +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001578 itostr(NumElements) + "");
Brian Gaeke715c90b2004-08-20 06:00:58 +00001579
1580 // Verify all elements are correct type!
1581 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencer08de34b2006-12-03 05:45:44 +00001582 if (ETy != (*$3)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001583 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Brian Gaeke715c90b2004-08-20 06:00:58 +00001584 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencer08de34b2006-12-03 05:45:44 +00001585 (*$3)[i]->getType()->getDescription() + "'.");
Brian Gaeke715c90b2004-08-20 06:00:58 +00001586 }
1587
Reid Spencer9d6565a2007-02-15 02:26:10 +00001588 $$ = ConstantVector::get(PTy, *$3);
Reid Spencer08de34b2006-12-03 05:45:44 +00001589 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001590 CHECK_FOR_ERROR
Brian Gaeke715c90b2004-08-20 06:00:58 +00001591 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001592 | Types '{' ConstVector '}' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001593 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001594 if (STy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001595 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001596 (*$1)->getDescription() + "'");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001597
Chris Lattnerc6212f12003-05-21 16:06:56 +00001598 if ($3->size() != STy->getNumContainedTypes())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001599 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattnerc6212f12003-05-21 16:06:56 +00001600
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001601 // Check to ensure that constants are compatible with the type initializer!
1602 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Reid Spencer08de34b2006-12-03 05:45:44 +00001603 if ((*$3)[i]->getType() != STy->getElementType(i))
Reid Spencer61c83e02006-08-18 08:43:06 +00001604 GEN_ERROR("Expected type '" +
Chris Lattnerd21cd802004-02-09 04:37:31 +00001605 STy->getElementType(i)->getDescription() +
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001606 "' for element #" + utostr(i) +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001607 " of structure initializer");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001608
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001609 // Check to ensure that Type is not packed
1610 if (STy->isPacked())
Reid Spencerac9dcb92007-02-15 03:39:18 +00001611 GEN_ERROR("Unpacked Initializer to vector type '" + STy->getDescription() + "'");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001612
Reid Spencer08de34b2006-12-03 05:45:44 +00001613 $$ = ConstantStruct::get(STy, *$3);
1614 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001615 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001616 }
Chris Lattnerc6212f12003-05-21 16:06:56 +00001617 | Types '{' '}' {
Reid Spencere1553cc2006-12-31 05:40:12 +00001618 if (!UpRefs.empty())
1619 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001620 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerc6212f12003-05-21 16:06:56 +00001621 if (STy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001622 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001623 (*$1)->getDescription() + "'");
Chris Lattnerc6212f12003-05-21 16:06:56 +00001624
1625 if (STy->getNumContainedTypes() != 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001626 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattnerc6212f12003-05-21 16:06:56 +00001627
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001628 // Check to ensure that Type is not packed
1629 if (STy->isPacked())
Reid Spencerac9dcb92007-02-15 03:39:18 +00001630 GEN_ERROR("Unpacked Initializer to vector type '" + STy->getDescription() + "'");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001631
1632 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1633 delete $1;
1634 CHECK_FOR_ERROR
1635 }
1636 | Types '<' '{' ConstVector '}' '>' {
1637 const StructType *STy = dyn_cast<StructType>($1->get());
1638 if (STy == 0)
1639 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001640 (*$1)->getDescription() + "'");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001641
1642 if ($4->size() != STy->getNumContainedTypes())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001643 GEN_ERROR("Illegal number of initializers for structure type");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001644
1645 // Check to ensure that constants are compatible with the type initializer!
1646 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1647 if ((*$4)[i]->getType() != STy->getElementType(i))
1648 GEN_ERROR("Expected type '" +
1649 STy->getElementType(i)->getDescription() +
1650 "' for element #" + utostr(i) +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001651 " of structure initializer");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001652
1653 // Check to ensure that Type is packed
1654 if (!STy->isPacked())
Reid Spencerac9dcb92007-02-15 03:39:18 +00001655 GEN_ERROR("Vector initializer to non-vector type '" +
1656 STy->getDescription() + "'");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001657
1658 $$ = ConstantStruct::get(STy, *$4);
1659 delete $1; delete $4;
1660 CHECK_FOR_ERROR
1661 }
1662 | Types '<' '{' '}' '>' {
1663 if (!UpRefs.empty())
1664 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1665 const StructType *STy = dyn_cast<StructType>($1->get());
1666 if (STy == 0)
1667 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001668 (*$1)->getDescription() + "'");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001669
1670 if (STy->getNumContainedTypes() != 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001671 GEN_ERROR("Illegal number of initializers for structure type");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001672
1673 // Check to ensure that Type is packed
1674 if (!STy->isPacked())
Reid Spencerac9dcb92007-02-15 03:39:18 +00001675 GEN_ERROR("Vector initializer to non-vector type '" +
1676 STy->getDescription() + "'");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001677
Reid Spencer08de34b2006-12-03 05:45:44 +00001678 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1679 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001680 CHECK_FOR_ERROR
Chris Lattnerc6212f12003-05-21 16:06:56 +00001681 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001682 | Types NULL_TOK {
Reid Spencere1553cc2006-12-31 05:40:12 +00001683 if (!UpRefs.empty())
1684 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001685 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001686 if (PTy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001687 GEN_ERROR("Cannot make null pointer constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001688 (*$1)->getDescription() + "'");
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001689
Reid Spencer08de34b2006-12-03 05:45:44 +00001690 $$ = ConstantPointerNull::get(PTy);
1691 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001692 CHECK_FOR_ERROR
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001693 }
Chris Lattner16710e92004-10-16 18:17:13 +00001694 | Types UNDEF {
Reid Spencere1553cc2006-12-31 05:40:12 +00001695 if (!UpRefs.empty())
1696 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001697 $$ = UndefValue::get($1->get());
1698 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001699 CHECK_FOR_ERROR
Chris Lattner16710e92004-10-16 18:17:13 +00001700 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001701 | Types SymbolicValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00001702 if (!UpRefs.empty())
1703 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001704 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001705 if (Ty == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001706 GEN_ERROR("Global const reference must be a pointer type");
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001707
Chris Lattner3101c252002-08-15 17:58:33 +00001708 // ConstExprs can exist in the body of a function, thus creating
Reid Spencer3271ed52004-07-18 00:08:11 +00001709 // GlobalValues whenever they refer to a variable. Because we are in
Reid Spencer186a43f2007-03-19 18:39:36 +00001710 // the context of a function, getExistingVal will search the functions
Chris Lattner3101c252002-08-15 17:58:33 +00001711 // symbol table instead of the module symbol table for the global symbol,
1712 // which throws things all off. To get around this, we just tell
Reid Spencer186a43f2007-03-19 18:39:36 +00001713 // getExistingVal that we are at global scope here.
Chris Lattner3101c252002-08-15 17:58:33 +00001714 //
Chris Lattner394f2eb2003-10-16 20:12:13 +00001715 Function *SavedCurFn = CurFun.CurrentFunction;
1716 CurFun.CurrentFunction = 0;
Chris Lattner3101c252002-08-15 17:58:33 +00001717
Reid Spencer186a43f2007-03-19 18:39:36 +00001718 Value *V = getExistingVal(Ty, $2);
Reid Spencer5b7e7532006-09-28 19:28:24 +00001719 CHECK_FOR_ERROR
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001720
Chris Lattner394f2eb2003-10-16 20:12:13 +00001721 CurFun.CurrentFunction = SavedCurFn;
Chris Lattner3101c252002-08-15 17:58:33 +00001722
Chris Lattner2079fde2001-10-13 06:41:08 +00001723 // If this is an initializer for a constant pointer, which is referencing a
1724 // (currently) undefined variable, create a stub now that shall be replaced
1725 // in the future with the right type of variable.
1726 //
1727 if (V == 0) {
Reid Spencer1755a9a2007-02-05 17:01:20 +00001728 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001729 const PointerType *PT = cast<PointerType>(Ty);
1730
1731 // First check to see if the forward references value is already created!
1732 PerModuleInfo::GlobalRefsType::iterator I =
Reid Spencer3271ed52004-07-18 00:08:11 +00001733 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Chris Lattner2079fde2001-10-13 06:41:08 +00001734
1735 if (I != CurModule.GlobalRefs.end()) {
Reid Spencer3271ed52004-07-18 00:08:11 +00001736 V = I->second; // Placeholder already exists, use it...
Chris Lattnera989b232003-12-23 20:05:15 +00001737 $2.destroy();
Chris Lattner2079fde2001-10-13 06:41:08 +00001738 } else {
Chris Lattner8a327842004-07-14 21:44:00 +00001739 std::string Name;
Reid Spencerb2d17862007-01-26 08:04:51 +00001740 if ($2.Type == ValID::GlobalName)
1741 Name = $2.Name;
1742 else if ($2.Type != ValID::GlobalID)
1743 GEN_ERROR("Invalid reference to global");
Chris Lattner8a327842004-07-14 21:44:00 +00001744
Reid Spencer3271ed52004-07-18 00:08:11 +00001745 // Create the forward referenced global.
Chris Lattnera09000d2004-07-14 23:03:46 +00001746 GlobalValue *GV;
1747 if (const FunctionType *FTy =
1748 dyn_cast<FunctionType>(PT->getElementType())) {
1749 GV = new Function(FTy, GlobalValue::ExternalLinkage, Name,
1750 CurModule.CurrentModule);
1751 } else {
Reid Spencer3271ed52004-07-18 00:08:11 +00001752 GV = new GlobalVariable(PT->getElementType(), false,
Chris Lattnera09000d2004-07-14 23:03:46 +00001753 GlobalValue::ExternalLinkage, 0,
1754 Name, CurModule.CurrentModule);
1755 }
Chris Lattner8a327842004-07-14 21:44:00 +00001756
Reid Spencer3271ed52004-07-18 00:08:11 +00001757 // Keep track of the fact that we have a forward ref to recycle it
1758 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1759 V = GV;
Chris Lattner2079fde2001-10-13 06:41:08 +00001760 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001761 }
1762
Reid Spencer08de34b2006-12-03 05:45:44 +00001763 $$ = cast<GlobalValue>(V);
1764 delete $1; // Free the type handle
Reid Spencer61c83e02006-08-18 08:43:06 +00001765 CHECK_FOR_ERROR
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001766 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001767 | Types ConstExpr {
Reid Spencere1553cc2006-12-31 05:40:12 +00001768 if (!UpRefs.empty())
1769 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001770 if ($1->get() != $2->getType())
Reid Spencerb4fdfdb2007-01-04 00:05:48 +00001771 GEN_ERROR("Mismatched types for constant expression: " +
1772 (*$1)->getDescription() + " and " + $2->getType()->getDescription());
Chris Lattnerd78700d2002-08-16 21:14:40 +00001773 $$ = $2;
Reid Spencer08de34b2006-12-03 05:45:44 +00001774 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001775 CHECK_FOR_ERROR
Chris Lattnerf4600482003-06-28 20:01:34 +00001776 }
1777 | Types ZEROINITIALIZER {
Reid Spencere1553cc2006-12-31 05:40:12 +00001778 if (!UpRefs.empty())
1779 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001780 const Type *Ty = $1->get();
Chris Lattner78915932004-11-28 16:45:45 +00001781 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001782 GEN_ERROR("Cannot create a null initialized value of this type");
Reid Spencer08de34b2006-12-03 05:45:44 +00001783 $$ = Constant::getNullValue(Ty);
1784 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001785 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00001786 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001787 | IntType ESINT64VAL { // integral constants
Reid Spencer9ffad0a2006-12-20 17:20:09 +00001788 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001789 GEN_ERROR("Constant value doesn't fit in type");
Reid Spencereac65742007-03-19 20:40:22 +00001790 $$ = ConstantInt::get($1, $2, true);
Reid Spencerf2310042007-02-28 02:23:44 +00001791 CHECK_FOR_ERROR
1792 }
1793 | IntType ESAPINTVAL { // arbitrary precision integer constants
1794 uint32_t BitWidth = cast<IntegerType>($1)->getBitWidth();
1795 if ($2->getBitWidth() > BitWidth) {
1796 GEN_ERROR("Constant value does not fit in type");
Reid Spenceraf1aacd2007-03-01 19:32:01 +00001797 }
1798 $2->sextOrTrunc(BitWidth);
1799 $$ = ConstantInt::get(*$2);
Reid Spencerf2310042007-02-28 02:23:44 +00001800 delete $2;
Reid Spencer9ffad0a2006-12-20 17:20:09 +00001801 CHECK_FOR_ERROR
1802 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001803 | IntType EUINT64VAL { // integral constants
Reid Spencer9ffad0a2006-12-20 17:20:09 +00001804 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001805 GEN_ERROR("Constant value doesn't fit in type");
Reid Spencereac65742007-03-19 20:40:22 +00001806 $$ = ConstantInt::get($1, $2, false);
Reid Spencerf2310042007-02-28 02:23:44 +00001807 CHECK_FOR_ERROR
1808 }
1809 | IntType EUAPINTVAL { // arbitrary precision integer constants
1810 uint32_t BitWidth = cast<IntegerType>($1)->getBitWidth();
1811 if ($2->getBitWidth() > BitWidth) {
1812 GEN_ERROR("Constant value does not fit in type");
Reid Spenceraf1aacd2007-03-01 19:32:01 +00001813 }
1814 $2->zextOrTrunc(BitWidth);
1815 $$ = ConstantInt::get(*$2);
Reid Spencerf2310042007-02-28 02:23:44 +00001816 delete $2;
Reid Spencer9ffad0a2006-12-20 17:20:09 +00001817 CHECK_FOR_ERROR
1818 }
Reid Spencer8088e9d2007-01-13 05:00:20 +00001819 | INTTYPE TRUETOK { // Boolean constants
1820 assert(cast<IntegerType>($1)->getBitWidth() == 1 && "Not Bool?");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001821 $$ = ConstantInt::getTrue();
Reid Spencer61c83e02006-08-18 08:43:06 +00001822 CHECK_FOR_ERROR
Chris Lattnerd05e3592002-08-15 18:17:28 +00001823 }
Reid Spencer8088e9d2007-01-13 05:00:20 +00001824 | INTTYPE FALSETOK { // Boolean constants
1825 assert(cast<IntegerType>($1)->getBitWidth() == 1 && "Not Bool?");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001826 $$ = ConstantInt::getFalse();
Reid Spencer61c83e02006-08-18 08:43:06 +00001827 CHECK_FOR_ERROR
Chris Lattnerd05e3592002-08-15 18:17:28 +00001828 }
1829 | FPType FPVAL { // Float & Double constants
Reid Spencer08de34b2006-12-03 05:45:44 +00001830 if (!ConstantFP::isValueValidForType($1, $2))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001831 GEN_ERROR("Floating point constant invalid for type");
Reid Spencer08de34b2006-12-03 05:45:44 +00001832 $$ = ConstantFP::get($1, $2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001833 CHECK_FOR_ERROR
Chris Lattnerd05e3592002-08-15 18:17:28 +00001834 };
1835
Chris Lattner00950542001-06-06 20:29:01 +00001836
Reid Spencer3da59db2006-11-27 01:05:10 +00001837ConstExpr: CastOps '(' ConstVal TO Types ')' {
Reid Spencere1553cc2006-12-31 05:40:12 +00001838 if (!UpRefs.empty())
1839 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001840 Constant *Val = $3;
Reid Spencer93947c32007-01-17 02:47:33 +00001841 const Type *DestTy = $5->get();
1842 if (!CastInst::castIsValid($1, $3, DestTy))
1843 GEN_ERROR("invalid cast opcode for cast from '" +
1844 Val->getType()->getDescription() + "' to '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001845 DestTy->getDescription() + "'");
Reid Spencer93947c32007-01-17 02:47:33 +00001846 $$ = ConstantExpr::getCast($1, $3, DestTy);
Reid Spencer08de34b2006-12-03 05:45:44 +00001847 delete $5;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001848 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001849 | GETELEMENTPTR '(' ConstVal IndexList ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001850 if (!isa<PointerType>($3->getType()))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001851 GEN_ERROR("GetElementPtr requires a pointer operand");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001852
Reid Spencer08de34b2006-12-03 05:45:44 +00001853 const Type *IdxTy =
Chris Lattnerc856c7a2007-02-13 00:57:40 +00001854 GetElementPtrInst::getIndexedType($3->getType(), &(*$4)[0], $4->size(),
1855 true);
Reid Spencer08de34b2006-12-03 05:45:44 +00001856 if (!IdxTy)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001857 GEN_ERROR("Index list invalid for constant getelementptr");
Reid Spencer08de34b2006-12-03 05:45:44 +00001858
Chris Lattnere0135402007-01-31 04:43:46 +00001859 SmallVector<Constant*, 8> IdxVec;
Reid Spencer08de34b2006-12-03 05:45:44 +00001860 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1861 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001862 IdxVec.push_back(C);
1863 else
Reid Spencerf4fa5902007-02-05 10:16:10 +00001864 GEN_ERROR("Indices to constant getelementptr must be constants");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001865
Chris Lattnerd78700d2002-08-16 21:14:40 +00001866 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001867
Chris Lattnere0135402007-01-31 04:43:46 +00001868 $$ = ConstantExpr::getGetElementPtr($3, &IdxVec[0], IdxVec.size());
Reid Spencer61c83e02006-08-18 08:43:06 +00001869 CHECK_FOR_ERROR
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001870 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001871 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer4fe16d62007-01-11 18:21:29 +00001872 if ($3->getType() != Type::Int1Ty)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001873 GEN_ERROR("Select condition must be of boolean type");
Reid Spencer08de34b2006-12-03 05:45:44 +00001874 if ($5->getType() != $7->getType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001875 GEN_ERROR("Select operand types must match");
Reid Spencer08de34b2006-12-03 05:45:44 +00001876 $$ = ConstantExpr::getSelect($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001877 CHECK_FOR_ERROR
Chris Lattner76f0ff32004-03-12 05:51:36 +00001878 }
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001879 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001880 if ($3->getType() != $5->getType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001881 GEN_ERROR("Binary operator types must match");
Reid Spencer1628cec2006-10-26 06:15:43 +00001882 CHECK_FOR_ERROR;
Reid Spencerc6e956e2006-12-05 19:15:41 +00001883 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001884 }
1885 | LogicalOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001886 if ($3->getType() != $5->getType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001887 GEN_ERROR("Logical operator types must match");
Chris Lattner42a75512007-01-15 02:27:26 +00001888 if (!$3->getType()->isInteger()) {
Reid Spencer9d6565a2007-02-15 02:26:10 +00001889 if (Instruction::isShift($1) || !isa<VectorType>($3->getType()) ||
1890 !cast<VectorType>($3->getType())->getElementType()->isInteger())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001891 GEN_ERROR("Logical operator requires integral operands");
Chris Lattner0a017832005-12-21 18:31:29 +00001892 }
Reid Spencer08de34b2006-12-03 05:45:44 +00001893 $$ = ConstantExpr::get($1, $3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001894 CHECK_FOR_ERROR
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001895 }
Reid Spencer763ed5e2006-12-04 05:20:06 +00001896 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
1897 if ($4->getType() != $6->getType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001898 GEN_ERROR("icmp operand types must match");
Reid Spencer763ed5e2006-12-04 05:20:06 +00001899 $$ = ConstantExpr::getICmp($2, $4, $6);
Reid Spencer08de34b2006-12-03 05:45:44 +00001900 }
Reid Spencer763ed5e2006-12-04 05:20:06 +00001901 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
1902 if ($4->getType() != $6->getType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001903 GEN_ERROR("fcmp operand types must match");
Reid Spencer763ed5e2006-12-04 05:20:06 +00001904 $$ = ConstantExpr::getFCmp($2, $4, $6);
Reid Spencer08de34b2006-12-03 05:45:44 +00001905 }
Robert Bocchino9c62b562006-01-10 19:04:32 +00001906 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001907 if (!ExtractElementInst::isValidOperands($3, $5))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001908 GEN_ERROR("Invalid extractelement operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00001909 $$ = ConstantExpr::getExtractElement($3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001910 CHECK_FOR_ERROR
Chris Lattnerca73cd82006-04-08 03:53:34 +00001911 }
1912 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001913 if (!InsertElementInst::isValidOperands($3, $5, $7))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001914 GEN_ERROR("Invalid insertelement operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00001915 $$ = ConstantExpr::getInsertElement($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001916 CHECK_FOR_ERROR
Chris Lattnerca73cd82006-04-08 03:53:34 +00001917 }
1918 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001919 if (!ShuffleVectorInst::isValidOperands($3, $5, $7))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001920 GEN_ERROR("Invalid shufflevector operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00001921 $$ = ConstantExpr::getShuffleVector($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001922 CHECK_FOR_ERROR
Chris Lattner699f1eb2002-08-14 17:12:33 +00001923 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001924
Chris Lattnerca73cd82006-04-08 03:53:34 +00001925
Misha Brukmanbc0e9982003-07-14 17:20:40 +00001926// ConstVector - A list of comma separated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001927ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001928 ($$ = $1)->push_back($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00001929 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001930 }
1931 | ConstVal {
Reid Spencer08de34b2006-12-03 05:45:44 +00001932 $$ = new std::vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001933 $$->push_back($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001934 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001935 };
Chris Lattner00950542001-06-06 20:29:01 +00001936
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001937
Chris Lattner1781aca2001-09-18 04:00:54 +00001938// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001939GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001940
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00001941// ThreadLocal
1942ThreadLocal : THREAD_LOCAL { $$ = true; } | { $$ = false; };
1943
Chris Lattner00950542001-06-06 20:29:01 +00001944
Chris Lattner0e73ce62002-05-02 19:11:13 +00001945//===----------------------------------------------------------------------===//
1946// Rules to match Modules
1947//===----------------------------------------------------------------------===//
1948
1949// Module rule: Capture the result of parsing the whole file into a result
1950// variable...
1951//
Reid Spencerb951bc02006-12-29 20:29:48 +00001952Module
1953 : DefinitionList {
1954 $$ = ParserResult = CurModule.CurrentModule;
1955 CurModule.ModuleDone();
1956 CHECK_FOR_ERROR;
1957 }
1958 | /*empty*/ {
1959 $$ = ParserResult = CurModule.CurrentModule;
1960 CurModule.ModuleDone();
1961 CHECK_FOR_ERROR;
1962 }
1963 ;
Chris Lattner0e73ce62002-05-02 19:11:13 +00001964
Reid Spencerb951bc02006-12-29 20:29:48 +00001965DefinitionList
1966 : Definition
1967 | DefinitionList Definition
1968 ;
1969
1970Definition
Jeff Cohen361c3ef2007-01-21 19:19:31 +00001971 : DEFINE { CurFun.isDeclare = false; } Function {
Chris Lattner394f2eb2003-10-16 20:12:13 +00001972 CurFun.FunctionDone();
Reid Spencer61c83e02006-08-18 08:43:06 +00001973 CHECK_FOR_ERROR
Reid Spencerb951bc02006-12-29 20:29:48 +00001974 }
1975 | DECLARE { CurFun.isDeclare = true; } FunctionProto {
Reid Spencer61c83e02006-08-18 08:43:06 +00001976 CHECK_FOR_ERROR
Chris Lattner0e73ce62002-05-02 19:11:13 +00001977 }
Reid Spencerb951bc02006-12-29 20:29:48 +00001978 | MODULE ASM_TOK AsmBlock {
Reid Spencer61c83e02006-08-18 08:43:06 +00001979 CHECK_FOR_ERROR
Chris Lattneree454772006-01-23 23:05:15 +00001980 }
Reid Spencerb2d17862007-01-26 08:04:51 +00001981 | OptLocalAssign TYPE Types {
Reid Spencere1553cc2006-12-31 05:40:12 +00001982 if (!UpRefs.empty())
1983 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattner4a42e902001-10-22 05:56:09 +00001984 // Eagerly resolve types. This is not an optimization, this is a
1985 // requirement that is due to the fact that we could have this:
1986 //
1987 // %list = type { %list * }
1988 // %list = type { %list * } ; repeated type decl
1989 //
1990 // If types are not resolved eagerly, then the two types will not be
1991 // determined to be the same type!
1992 //
Reid Spencerb951bc02006-12-29 20:29:48 +00001993 ResolveTypeTo($1, *$3);
Chris Lattner4a42e902001-10-22 05:56:09 +00001994
Reid Spencerb951bc02006-12-29 20:29:48 +00001995 if (!setTypeName(*$3, $1) && !$1) {
Reid Spencer5b7e7532006-09-28 19:28:24 +00001996 CHECK_FOR_ERROR
Chris Lattner8c89a0a2004-07-13 08:10:10 +00001997 // If this is a named type that is not a redefinition, add it to the slot
1998 // table.
Reid Spencerb951bc02006-12-29 20:29:48 +00001999 CurModule.Types.push_back(*$3);
Chris Lattner30c89792001-09-07 16:35:17 +00002000 }
Reid Spencer08de34b2006-12-03 05:45:44 +00002001
Reid Spencerb951bc02006-12-29 20:29:48 +00002002 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002003 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00002004 }
Reid Spencerb2d17862007-01-26 08:04:51 +00002005 | OptLocalAssign TYPE VOID {
Reid Spencere1553cc2006-12-31 05:40:12 +00002006 ResolveTypeTo($1, $3);
2007
2008 if (!setTypeName($3, $1) && !$1) {
2009 CHECK_FOR_ERROR
2010 // If this is a named type that is not a redefinition, add it to the slot
2011 // table.
2012 CurModule.Types.push_back($3);
2013 }
2014 CHECK_FOR_ERROR
2015 }
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002016 | OptGlobalAssign GVVisibilityStyle ThreadLocal GlobalType ConstVal {
Reid Spencerb2d17862007-01-26 08:04:51 +00002017 /* "Externally Visible" Linkage */
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002018 if ($5 == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002019 GEN_ERROR("Global value initializer is not a constant");
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002020 CurGV = ParseGlobalVariable($1, GlobalValue::ExternalLinkage,
2021 $2, $4, $5->getType(), $5, $3);
Reid Spencerb951bc02006-12-29 20:29:48 +00002022 CHECK_FOR_ERROR
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002023 } GlobalVarAttributes {
2024 CurGV = 0;
2025 }
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002026 | OptGlobalAssign GVInternalLinkage GVVisibilityStyle ThreadLocal GlobalType ConstVal {
2027 if ($6 == 0)
2028 GEN_ERROR("Global value initializer is not a constant");
2029 CurGV = ParseGlobalVariable($1, $2, $3, $5, $6->getType(), $6, $4);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002030 CHECK_FOR_ERROR
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002031 } GlobalVarAttributes {
2032 CurGV = 0;
2033 }
2034 | OptGlobalAssign GVExternalLinkage GVVisibilityStyle ThreadLocal GlobalType Types {
2035 if (!UpRefs.empty())
2036 GEN_ERROR("Invalid upreference in type: " + (*$6)->getDescription());
2037 CurGV = ParseGlobalVariable($1, $2, $3, $5, *$6, 0, $4);
2038 CHECK_FOR_ERROR
2039 delete $6;
Reid Spencer5b7e7532006-09-28 19:28:24 +00002040 } GlobalVarAttributes {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002041 CurGV = 0;
2042 CHECK_FOR_ERROR
2043 }
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00002044 | OptGlobalAssign GVVisibilityStyle ALIAS AliasLinkage ResultTypes SymbolicValueRef {
2045 std::string Name($1);
2046 if (Name.empty())
2047 GEN_ERROR("Alias name cannot be empty")
2048 const PointerType *PFTy = 0;
2049 const FunctionType *Ty = 0;
2050 Value* V = 0;
2051 const Type* VTy = 0;
2052 if (!(PFTy = dyn_cast<PointerType>($5->get())) ||
2053 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
2054 VTy = $5->get();
2055 V = getExistingVal(VTy, $6);
2056 } else {
2057 VTy = PFTy;
2058 V = getExistingVal(PFTy, $6);
2059 }
2060 if (V == 0)
2061 GEN_ERROR(std::string("Invalid aliasee for alias: ") + $1);
Bill Wendling9de13712007-04-25 23:52:02 +00002062 if (GlobalValue* Aliasee = dyn_cast<GlobalValue>(V)) {
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00002063 GlobalAlias* GA = new GlobalAlias(VTy, $4, Name, Aliasee, CurModule.CurrentModule);
2064 GA->setVisibility($2);
2065 InsertValue(GA, CurModule.Values);
2066 } else
2067 GEN_ERROR("Aliases can be created only to global values");
2068 CHECK_FOR_ERROR
2069 delete $5;
2070 }
Reid Spencerb951bc02006-12-29 20:29:48 +00002071 | TARGET TargetDefinition {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002072 CHECK_FOR_ERROR
2073 }
Reid Spencerb951bc02006-12-29 20:29:48 +00002074 | DEPLIBS '=' LibrariesDefinition {
Reid Spencer61c83e02006-08-18 08:43:06 +00002075 CHECK_FOR_ERROR
Chris Lattnere98dda62001-07-14 06:10:16 +00002076 }
Reid Spencerb951bc02006-12-29 20:29:48 +00002077 ;
Chris Lattner00950542001-06-06 20:29:01 +00002078
2079
Chris Lattneree454772006-01-23 23:05:15 +00002080AsmBlock : STRINGCONSTANT {
Chris Lattner66316012006-01-24 04:14:29 +00002081 const std::string &AsmSoFar = CurModule.CurrentModule->getModuleInlineAsm();
Chris Lattneree454772006-01-23 23:05:15 +00002082 char *EndStr = UnEscapeLexed($1, true);
2083 std::string NewAsm($1, EndStr);
2084 free($1);
2085
2086 if (AsmSoFar.empty())
Chris Lattner66316012006-01-24 04:14:29 +00002087 CurModule.CurrentModule->setModuleInlineAsm(NewAsm);
Chris Lattneree454772006-01-23 23:05:15 +00002088 else
Chris Lattner66316012006-01-24 04:14:29 +00002089 CurModule.CurrentModule->setModuleInlineAsm(AsmSoFar+"\n"+NewAsm);
Reid Spencer61c83e02006-08-18 08:43:06 +00002090 CHECK_FOR_ERROR
Chris Lattneree454772006-01-23 23:05:15 +00002091};
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00002092
Reid Spencerb2d17862007-01-26 08:04:51 +00002093TargetDefinition : TRIPLE '=' STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00002094 CurModule.CurrentModule->setTargetTriple($3);
2095 free($3);
John Criswell2f6a8b12006-10-24 19:09:48 +00002096 }
Chris Lattner10b27112006-10-22 06:07:41 +00002097 | DATALAYOUT '=' STRINGCONSTANT {
Owen Andersoncf7ff2b2006-10-18 02:21:12 +00002098 CurModule.CurrentModule->setDataLayout($3);
2099 free($3);
Owen Andersoncf7ff2b2006-10-18 02:21:12 +00002100 };
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00002101
Reid Spencerfeaf10e2004-07-25 21:30:51 +00002102LibrariesDefinition : '[' LibList ']';
Reid Spencer0be13e72004-07-25 17:58:28 +00002103
2104LibList : LibList ',' STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00002105 CurModule.CurrentModule->addLibrary($3);
2106 free($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002107 CHECK_FOR_ERROR
Reid Spencer0be13e72004-07-25 17:58:28 +00002108 }
2109 | STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00002110 CurModule.CurrentModule->addLibrary($1);
2111 free($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002112 CHECK_FOR_ERROR
Reid Spencer0be13e72004-07-25 17:58:28 +00002113 }
2114 | /* empty: end of list */ {
Reid Spencer61c83e02006-08-18 08:43:06 +00002115 CHECK_FOR_ERROR
Reid Spencer0be13e72004-07-25 17:58:28 +00002116 }
2117 ;
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00002118
Chris Lattner00950542001-06-06 20:29:01 +00002119//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00002120// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00002121//===----------------------------------------------------------------------===//
2122
Reid Spencerb2d17862007-01-26 08:04:51 +00002123ArgListH : ArgListH ',' Types OptParamAttrs OptLocalName {
Reid Spencere1553cc2006-12-31 05:40:12 +00002124 if (!UpRefs.empty())
2125 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
2126 if (*$3 == Type::VoidTy)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002127 GEN_ERROR("void typed arguments are invalid");
Reid Spencere1553cc2006-12-31 05:40:12 +00002128 ArgListEntry E; E.Attrs = $4; E.Ty = $3; E.Name = $5;
Chris Lattner69da5cf2002-10-13 20:57:00 +00002129 $$ = $1;
Reid Spencere1553cc2006-12-31 05:40:12 +00002130 $1->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002131 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002132 }
Reid Spencerb2d17862007-01-26 08:04:51 +00002133 | Types OptParamAttrs OptLocalName {
Reid Spencere1553cc2006-12-31 05:40:12 +00002134 if (!UpRefs.empty())
2135 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2136 if (*$1 == 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 = $2; E.Ty = $1; E.Name = $3;
2139 $$ = new ArgListType;
2140 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002141 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002142 };
Chris Lattner00950542001-06-06 20:29:01 +00002143
2144ArgList : ArgListH {
2145 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002146 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002147 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00002148 | ArgListH ',' DOTDOTDOT {
2149 $$ = $1;
Reid Spencere1553cc2006-12-31 05:40:12 +00002150 struct ArgListEntry E;
2151 E.Ty = new PATypeHolder(Type::VoidTy);
2152 E.Name = 0;
Reid Spencer18da0722007-04-11 02:44:20 +00002153 E.Attrs = ParamAttr::None;
Reid Spencere1553cc2006-12-31 05:40:12 +00002154 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002155 CHECK_FOR_ERROR
Chris Lattner69da5cf2002-10-13 20:57:00 +00002156 }
2157 | DOTDOTDOT {
Reid Spencere1553cc2006-12-31 05:40:12 +00002158 $$ = new ArgListType;
2159 struct ArgListEntry E;
2160 E.Ty = new PATypeHolder(Type::VoidTy);
2161 E.Name = 0;
Reid Spencer18da0722007-04-11 02:44:20 +00002162 E.Attrs = ParamAttr::None;
Reid Spencere1553cc2006-12-31 05:40:12 +00002163 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002164 CHECK_FOR_ERROR
Chris Lattner69da5cf2002-10-13 20:57:00 +00002165 }
Chris Lattner00950542001-06-06 20:29:01 +00002166 | /* empty */ {
2167 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00002168 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002169 };
Chris Lattner00950542001-06-06 20:29:01 +00002170
Reid Spencerb2d17862007-01-26 08:04:51 +00002171FunctionHeaderH : OptCallingConv ResultTypes GlobalName '(' ArgList ')'
Reid Spencer2c261782007-01-05 17:06:19 +00002172 OptFuncAttrs OptSection OptAlign {
Chris Lattnera8e8f162005-05-06 20:27:19 +00002173 UnEscapeLexed($3);
2174 std::string FunctionName($3);
2175 free($3); // Free strdup'd memory!
Chris Lattnerdda71962001-11-26 18:54:16 +00002176
Reid Spencer98b3c5c2007-01-02 21:53:43 +00002177 // Check the function result for abstractness if this is a define. We should
2178 // have no abstract types at this point
Reid Spencer2c261782007-01-05 17:06:19 +00002179 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved($2))
2180 GEN_ERROR("Reference to abstract result: "+ $2->get()->getDescription());
Reid Spencer98b3c5c2007-01-02 21:53:43 +00002181
Chris Lattner9232b992003-04-22 18:42:41 +00002182 std::vector<const Type*> ParamTypeList;
Reid Spencer4f859aa2007-04-22 05:46:44 +00002183 ParamAttrsVector Attrs;
2184 if ($7 != ParamAttr::None) {
2185 ParamAttrsWithIndex PAWI; PAWI.index = 0; PAWI.attrs = $7;
2186 Attrs.push_back(PAWI);
2187 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00002188 if ($5) { // If there are arguments...
Reid Spencer5694b6e2007-04-09 06:17:21 +00002189 unsigned index = 1;
2190 for (ArgListType::iterator I = $5->begin(); I != $5->end(); ++I, ++index) {
Reid Spencere1553cc2006-12-31 05:40:12 +00002191 const Type* Ty = I->Ty->get();
Reid Spencer98b3c5c2007-01-02 21:53:43 +00002192 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved(I->Ty))
2193 GEN_ERROR("Reference to abstract argument: " + Ty->getDescription());
Reid Spencere1553cc2006-12-31 05:40:12 +00002194 ParamTypeList.push_back(Ty);
2195 if (Ty != Type::VoidTy)
Reid Spencer4f859aa2007-04-22 05:46:44 +00002196 if (I->Attrs != ParamAttr::None) {
2197 ParamAttrsWithIndex PAWI; PAWI.index = index; PAWI.attrs = I->Attrs;
2198 Attrs.push_back(PAWI);
2199 }
Reid Spencere1553cc2006-12-31 05:40:12 +00002200 }
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002201 }
Chris Lattner00950542001-06-06 20:29:01 +00002202
Chris Lattner2079fde2001-10-13 06:41:08 +00002203 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
2204 if (isVarArg) ParamTypeList.pop_back();
2205
Reid Spencer4f859aa2007-04-22 05:46:44 +00002206 ParamAttrsList *PAL = 0;
2207 if (!Attrs.empty())
2208 PAL = ParamAttrsList::get(Attrs);
Reid Spencer5694b6e2007-04-09 06:17:21 +00002209
Reid Spencer4f859aa2007-04-22 05:46:44 +00002210 FunctionType *FT = FunctionType::get(*$2, ParamTypeList, isVarArg, PAL);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002211 const PointerType *PFT = PointerType::get(FT);
Reid Spencer2c261782007-01-05 17:06:19 +00002212 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002213
Chris Lattnera09000d2004-07-14 23:03:46 +00002214 ValID ID;
2215 if (!FunctionName.empty()) {
Reid Spencerb2d17862007-01-26 08:04:51 +00002216 ID = ValID::createGlobalName((char*)FunctionName.c_str());
Chris Lattnera09000d2004-07-14 23:03:46 +00002217 } else {
Reid Spencer186a43f2007-03-19 18:39:36 +00002218 ID = ValID::createGlobalID(CurModule.Values.size());
Chris Lattnera09000d2004-07-14 23:03:46 +00002219 }
2220
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00002221 Function *Fn = 0;
Chris Lattnera09000d2004-07-14 23:03:46 +00002222 // See if this function was forward referenced. If so, recycle the object.
2223 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
2224 // Move the function to the end of the list, from whereever it was
2225 // previously inserted.
2226 Fn = cast<Function>(FWRef);
2227 CurModule.CurrentModule->getFunctionList().remove(Fn);
2228 CurModule.CurrentModule->getFunctionList().push_back(Fn);
2229 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
Reid Spenceref9b9a72007-02-05 20:47:22 +00002230 (Fn = CurModule.CurrentModule->getFunction(FunctionName))) {
2231 if (Fn->getFunctionType() != FT ) {
2232 // The existing function doesn't have the same type. This is an overload
2233 // error.
2234 GEN_ERROR("Overload of function '" + FunctionName + "' not permitted.");
2235 } else if (!CurFun.isDeclare && !Fn->isDeclaration()) {
2236 // Neither the existing or the current function is a declaration and they
2237 // have the same name and same type. Clearly this is a redefinition.
Reid Spencerf4fa5902007-02-05 10:16:10 +00002238 GEN_ERROR("Redefinition of function '" + FunctionName + "'");
Reid Spenceref9b9a72007-02-05 20:47:22 +00002239 } if (Fn->isDeclaration()) {
2240 // Make sure to strip off any argument names so we can't get conflicts.
Chris Lattnere4d5c442005-03-15 04:54:21 +00002241 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
Chris Lattnera09000d2004-07-14 23:03:46 +00002242 AI != AE; ++AI)
2243 AI->setName("");
Reid Spenceref9b9a72007-02-05 20:47:22 +00002244 }
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00002245 } else { // Not already defined?
2246 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName,
2247 CurModule.CurrentModule);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002248
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00002249 InsertValue(Fn, CurModule.Values);
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00002250 }
Chris Lattner00950542001-06-06 20:29:01 +00002251
Chris Lattner394f2eb2003-10-16 20:12:13 +00002252 CurFun.FunctionStart(Fn);
Anton Korobeynikov93c2b372006-09-17 13:06:18 +00002253
2254 if (CurFun.isDeclare) {
2255 // If we have declaration, always overwrite linkage. This will allow us to
2256 // correctly handle cases, when pointer to function is passed as argument to
2257 // another function.
2258 Fn->setLinkage(CurFun.Linkage);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002259 Fn->setVisibility(CurFun.Visibility);
Anton Korobeynikov93c2b372006-09-17 13:06:18 +00002260 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00002261 Fn->setCallingConv($1);
Reid Spencer2c261782007-01-05 17:06:19 +00002262 Fn->setAlignment($9);
2263 if ($8) {
2264 Fn->setSection($8);
2265 free($8);
Chris Lattner164c3782005-11-12 00:11:10 +00002266 }
Chris Lattner00950542001-06-06 20:29:01 +00002267
Chris Lattner7e708292002-06-25 16:13:24 +00002268 // Add all of the arguments we parsed to the function...
Chris Lattnera8e8f162005-05-06 20:27:19 +00002269 if ($5) { // Is null if empty...
Chris Lattner69da5cf2002-10-13 20:57:00 +00002270 if (isVarArg) { // Nuke the last entry
Reid Spenceref9b9a72007-02-05 20:47:22 +00002271 assert($5->back().Ty->get() == Type::VoidTy && $5->back().Name == 0 &&
Reid Spencer1755a9a2007-02-05 17:01:20 +00002272 "Not a varargs marker!");
Reid Spencere1553cc2006-12-31 05:40:12 +00002273 delete $5->back().Ty;
Chris Lattnera8e8f162005-05-06 20:27:19 +00002274 $5->pop_back(); // Delete the last entry
Chris Lattner69da5cf2002-10-13 20:57:00 +00002275 }
Chris Lattnere4d5c442005-03-15 04:54:21 +00002276 Function::arg_iterator ArgIt = Fn->arg_begin();
Reid Spenceref9b9a72007-02-05 20:47:22 +00002277 Function::arg_iterator ArgEnd = Fn->arg_end();
Reid Spencere1553cc2006-12-31 05:40:12 +00002278 unsigned Idx = 1;
Reid Spenceref9b9a72007-02-05 20:47:22 +00002279 for (ArgListType::iterator I = $5->begin();
2280 I != $5->end() && ArgIt != ArgEnd; ++I, ++ArgIt) {
Reid Spencere1553cc2006-12-31 05:40:12 +00002281 delete I->Ty; // Delete the typeholder...
Reid Spenceref9b9a72007-02-05 20:47:22 +00002282 setValueName(ArgIt, I->Name); // Insert arg into symtab...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002283 CHECK_FOR_ERROR
Chris Lattner69da5cf2002-10-13 20:57:00 +00002284 InsertValue(ArgIt);
Reid Spencere1553cc2006-12-31 05:40:12 +00002285 Idx++;
Chris Lattner00950542001-06-06 20:29:01 +00002286 }
Reid Spencer08de34b2006-12-03 05:45:44 +00002287
Chris Lattnera8e8f162005-05-06 20:27:19 +00002288 delete $5; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00002289 }
Reid Spencer61c83e02006-08-18 08:43:06 +00002290 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002291};
Chris Lattner00950542001-06-06 20:29:01 +00002292
Chris Lattner9b02cc32002-05-03 18:23:48 +00002293BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
2294
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002295FunctionHeader : FunctionDefineLinkage GVVisibilityStyle FunctionHeaderH BEGIN {
Chris Lattner394f2eb2003-10-16 20:12:13 +00002296 $$ = CurFun.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00002297
Chris Lattner4ad02e72003-04-16 20:28:45 +00002298 // Make sure that we keep track of the linkage type even if there was a
2299 // previous "declare".
2300 $$->setLinkage($1);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002301 $$->setVisibility($2);
Chris Lattner51727be2002-06-04 21:58:56 +00002302};
Chris Lattner00950542001-06-06 20:29:01 +00002303
Chris Lattner9b02cc32002-05-03 18:23:48 +00002304END : ENDTOK | '}'; // Allow end of '}' to end a function
2305
Chris Lattner79df7c02002-03-26 18:01:55 +00002306Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00002307 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002308 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002309};
Chris Lattner00950542001-06-06 20:29:01 +00002310
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002311FunctionProto : FunctionDeclareLinkage GVVisibilityStyle FunctionHeaderH {
Reid Spencere1553cc2006-12-31 05:40:12 +00002312 CurFun.CurrentFunction->setLinkage($1);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002313 CurFun.CurrentFunction->setVisibility($2);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002314 $$ = CurFun.CurrentFunction;
2315 CurFun.FunctionDone();
2316 CHECK_FOR_ERROR
2317 };
Chris Lattner00950542001-06-06 20:29:01 +00002318
2319//===----------------------------------------------------------------------===//
2320// Rules to match Basic Blocks
2321//===----------------------------------------------------------------------===//
2322
Chris Lattneraa2c8532006-01-25 22:26:43 +00002323OptSideEffect : /* empty */ {
2324 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002325 CHECK_FOR_ERROR
Chris Lattneraa2c8532006-01-25 22:26:43 +00002326 }
2327 | SIDEEFFECT {
2328 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002329 CHECK_FOR_ERROR
Chris Lattneraa2c8532006-01-25 22:26:43 +00002330 };
2331
Chris Lattner00950542001-06-06 20:29:01 +00002332ConstValueRef : ESINT64VAL { // A reference to a direct constant
2333 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002334 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002335 }
2336 | EUINT64VAL {
2337 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002338 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002339 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00002340 | FPVAL { // Perhaps it's an FP constant?
2341 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002342 CHECK_FOR_ERROR
Chris Lattner3d52b2f2001-07-15 00:17:01 +00002343 }
Chris Lattner91ef4602004-03-31 03:49:47 +00002344 | TRUETOK {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00002345 $$ = ValID::create(ConstantInt::getTrue());
Reid Spencer61c83e02006-08-18 08:43:06 +00002346 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002347 }
Chris Lattner91ef4602004-03-31 03:49:47 +00002348 | FALSETOK {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00002349 $$ = ValID::create(ConstantInt::getFalse());
Reid Spencer61c83e02006-08-18 08:43:06 +00002350 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002351 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00002352 | NULL_TOK {
2353 $$ = ValID::createNull();
Reid Spencer61c83e02006-08-18 08:43:06 +00002354 CHECK_FOR_ERROR
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002355 }
Chris Lattner16710e92004-10-16 18:17:13 +00002356 | UNDEF {
2357 $$ = ValID::createUndef();
Reid Spencer61c83e02006-08-18 08:43:06 +00002358 CHECK_FOR_ERROR
Chris Lattner16710e92004-10-16 18:17:13 +00002359 }
Chris Lattnerf1f03df2005-12-21 17:53:02 +00002360 | ZEROINITIALIZER { // A vector zero constant.
2361 $$ = ValID::createZeroInit();
Reid Spencer61c83e02006-08-18 08:43:06 +00002362 CHECK_FOR_ERROR
Chris Lattnerf1f03df2005-12-21 17:53:02 +00002363 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00002364 | '<' ConstVector '>' { // Nonempty unsized packed vector
Reid Spencer08de34b2006-12-03 05:45:44 +00002365 const Type *ETy = (*$2)[0]->getType();
Brian Gaeke715c90b2004-08-20 06:00:58 +00002366 int NumElements = $2->size();
2367
Reid Spencer9d6565a2007-02-15 02:26:10 +00002368 VectorType* pt = VectorType::get(ETy, NumElements);
Brian Gaeke715c90b2004-08-20 06:00:58 +00002369 PATypeHolder* PTy = new PATypeHolder(
Reid Spencer08de34b2006-12-03 05:45:44 +00002370 HandleUpRefs(
Reid Spencer9d6565a2007-02-15 02:26:10 +00002371 VectorType::get(
Reid Spencer08de34b2006-12-03 05:45:44 +00002372 ETy,
2373 NumElements)
2374 )
2375 );
Brian Gaeke715c90b2004-08-20 06:00:58 +00002376
2377 // Verify all elements are correct type!
2378 for (unsigned i = 0; i < $2->size(); i++) {
Reid Spencer08de34b2006-12-03 05:45:44 +00002379 if (ETy != (*$2)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00002380 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Brian Gaeke715c90b2004-08-20 06:00:58 +00002381 ETy->getDescription() +"' as required!\nIt is of type '" +
Reid Spencer08de34b2006-12-03 05:45:44 +00002382 (*$2)[i]->getType()->getDescription() + "'.");
Brian Gaeke715c90b2004-08-20 06:00:58 +00002383 }
2384
Reid Spencer9d6565a2007-02-15 02:26:10 +00002385 $$ = ValID::create(ConstantVector::get(pt, *$2));
Brian Gaeke715c90b2004-08-20 06:00:58 +00002386 delete PTy; delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002387 CHECK_FOR_ERROR
Brian Gaeke715c90b2004-08-20 06:00:58 +00002388 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00002389 | ConstExpr {
Reid Spencer08de34b2006-12-03 05:45:44 +00002390 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002391 CHECK_FOR_ERROR
Chris Lattneraa2c8532006-01-25 22:26:43 +00002392 }
2393 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
2394 char *End = UnEscapeLexed($3, true);
2395 std::string AsmStr = std::string($3, End);
2396 End = UnEscapeLexed($5, true);
2397 std::string Constraints = std::string($5, End);
2398 $$ = ValID::createInlineAsm(AsmStr, Constraints, $2);
2399 free($3);
2400 free($5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002401 CHECK_FOR_ERROR
Chris Lattnerd78700d2002-08-16 21:14:40 +00002402 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00002403
Chris Lattner2079fde2001-10-13 06:41:08 +00002404// SymbolicValueRef - Reference to one of two ways of symbolically refering to
2405// another value.
2406//
Reid Spencerb2d17862007-01-26 08:04:51 +00002407SymbolicValueRef : LOCALVAL_ID { // Is it an integer reference...?
2408 $$ = ValID::createLocalID($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002409 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002410 }
Reid Spencerb2d17862007-01-26 08:04:51 +00002411 | GLOBALVAL_ID {
2412 $$ = ValID::createGlobalID($1);
2413 CHECK_FOR_ERROR
2414 }
2415 | LocalName { // Is it a named reference...?
2416 $$ = ValID::createLocalName($1);
2417 CHECK_FOR_ERROR
2418 }
2419 | GlobalName { // Is it a named reference...?
2420 $$ = ValID::createGlobalName($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002421 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002422 };
Chris Lattner2079fde2001-10-13 06:41:08 +00002423
2424// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00002425ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00002426
Chris Lattner00950542001-06-06 20:29:01 +00002427
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002428// ResolvedVal - a <type> <value> pair. This is used only in cases where the
2429// type immediately preceeds the value reference, and allows complex constant
2430// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00002431ResolvedVal : Types ValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00002432 if (!UpRefs.empty())
2433 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2434 $$ = getVal(*$1, $2);
2435 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002436 CHECK_FOR_ERROR
Reid Spencere1553cc2006-12-31 05:40:12 +00002437 }
2438 ;
Chris Lattner8b81bf52001-07-25 22:47:46 +00002439
Chris Lattner00950542001-06-06 20:29:01 +00002440BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner8ab406d2004-07-13 07:59:27 +00002441 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002442 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002443 }
Chris Lattner7e708292002-06-25 16:13:24 +00002444 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
Chris Lattner8ab406d2004-07-13 07:59:27 +00002445 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002446 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002447 };
Chris Lattner00950542001-06-06 20:29:01 +00002448
2449
2450// Basic blocks are terminated by branching instructions:
2451// br, br/cc, switch, ret
2452//
Reid Spencerb2d17862007-01-26 08:04:51 +00002453BasicBlock : InstructionList OptLocalAssign BBTerminatorInst {
Chris Lattner8ab406d2004-07-13 07:59:27 +00002454 setValueName($3, $2);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002455 CHECK_FOR_ERROR
Chris Lattner2079fde2001-10-13 06:41:08 +00002456 InsertValue($3);
Chris Lattner2079fde2001-10-13 06:41:08 +00002457 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00002458 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002459 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002460 };
Chris Lattner00950542001-06-06 20:29:01 +00002461
2462InstructionList : InstructionList Inst {
Reid Spencer3da59db2006-11-27 01:05:10 +00002463 if (CastInst *CI1 = dyn_cast<CastInst>($2))
2464 if (CastInst *CI2 = dyn_cast<CastInst>(CI1->getOperand(0)))
2465 if (CI2->getParent() == 0)
2466 $1->getInstList().push_back(CI2);
Chris Lattner00950542001-06-06 20:29:01 +00002467 $1->getInstList().push_back($2);
2468 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002469 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002470 }
Reid Spencer186a43f2007-03-19 18:39:36 +00002471 | /* empty */ { // Empty space between instruction lists
2472 $$ = defineBBVal(ValID::createLocalID(CurFun.NextValNum));
Reid Spencer61c83e02006-08-18 08:43:06 +00002473 CHECK_FOR_ERROR
Chris Lattner22ae2322004-07-13 08:39:15 +00002474 }
Reid Spencer186a43f2007-03-19 18:39:36 +00002475 | LABELSTR { // Labelled (named) basic block
2476 $$ = defineBBVal(ValID::createLocalName($1));
Reid Spencer61c83e02006-08-18 08:43:06 +00002477 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002478 };
Chris Lattner00950542001-06-06 20:29:01 +00002479
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002480BBTerminatorInst : RET ResolvedVal { // Return with a result...
Reid Spencer08de34b2006-12-03 05:45:44 +00002481 $$ = new ReturnInst($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00002482 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002483 }
Reid Spencer186a43f2007-03-19 18:39:36 +00002484 | RET VOID { // Return with no result...
Chris Lattner00950542001-06-06 20:29:01 +00002485 $$ = new ReturnInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002486 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002487 }
Reid Spencer186a43f2007-03-19 18:39:36 +00002488 | BR LABEL ValueRef { // Unconditional Branch...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002489 BasicBlock* tmpBB = getBBVal($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002490 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002491 $$ = new BranchInst(tmpBB);
Reid Spencer186a43f2007-03-19 18:39:36 +00002492 } // Conditional Branch...
Reid Spencer8088e9d2007-01-13 05:00:20 +00002493 | BR INTTYPE ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
2494 assert(cast<IntegerType>($2)->getBitWidth() == 1 && "Not Bool?");
Reid Spencer5b7e7532006-09-28 19:28:24 +00002495 BasicBlock* tmpBBA = getBBVal($6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002496 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002497 BasicBlock* tmpBBB = getBBVal($9);
2498 CHECK_FOR_ERROR
Reid Spencer4fe16d62007-01-11 18:21:29 +00002499 Value* tmpVal = getVal(Type::Int1Ty, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002500 CHECK_FOR_ERROR
2501 $$ = new BranchInst(tmpBBA, tmpBBB, tmpVal);
Chris Lattner00950542001-06-06 20:29:01 +00002502 }
2503 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencer08de34b2006-12-03 05:45:44 +00002504 Value* tmpVal = getVal($2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002505 CHECK_FOR_ERROR
2506 BasicBlock* tmpBB = getBBVal($6);
2507 CHECK_FOR_ERROR
2508 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, $8->size());
Chris Lattner00950542001-06-06 20:29:01 +00002509 $$ = S;
2510
Chris Lattner9232b992003-04-22 18:42:41 +00002511 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
Chris Lattner46748042002-04-09 19:41:42 +00002512 E = $8->end();
Chris Lattner69331f52005-02-24 05:25:17 +00002513 for (; I != E; ++I) {
2514 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
2515 S->addCase(CI, I->second);
2516 else
Reid Spencerf4fa5902007-02-05 10:16:10 +00002517 GEN_ERROR("Switch case is constant, but not a simple integer");
Chris Lattner69331f52005-02-24 05:25:17 +00002518 }
Chris Lattnerf57a43d2004-04-17 23:49:15 +00002519 delete $8;
Reid Spencer61c83e02006-08-18 08:43:06 +00002520 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002521 }
Chris Lattner196850c2003-05-15 21:30:00 +00002522 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencer08de34b2006-12-03 05:45:44 +00002523 Value* tmpVal = getVal($2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002524 CHECK_FOR_ERROR
2525 BasicBlock* tmpBB = getBBVal($6);
2526 CHECK_FOR_ERROR
2527 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, 0);
Chris Lattner196850c2003-05-15 21:30:00 +00002528 $$ = S;
Reid Spencer61c83e02006-08-18 08:43:06 +00002529 CHECK_FOR_ERROR
Chris Lattner196850c2003-05-15 21:30:00 +00002530 }
Reid Spencer2c261782007-01-05 17:06:19 +00002531 | INVOKE OptCallingConv ResultTypes ValueRef '(' ValueRefList ')' OptFuncAttrs
Chris Lattnera8e8f162005-05-06 20:27:19 +00002532 TO LABEL ValueRef UNWIND LABEL ValueRef {
Chris Lattner2079fde2001-10-13 06:41:08 +00002533
Reid Spencere1553cc2006-12-31 05:40:12 +00002534 // Handle the short syntax
2535 const PointerType *PFTy = 0;
2536 const FunctionType *Ty = 0;
Reid Spencer2c261782007-01-05 17:06:19 +00002537 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002538 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00002539 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00002540 std::vector<const Type*> ParamTypes;
Reid Spencer4f859aa2007-04-22 05:46:44 +00002541 ParamAttrsVector Attrs;
2542 if ($8 != ParamAttr::None) {
2543 ParamAttrsWithIndex PAWI; PAWI.index = 0; PAWI.attrs = 8;
2544 Attrs.push_back(PAWI);
2545 }
Reid Spencer5694b6e2007-04-09 06:17:21 +00002546 ValueRefList::iterator I = $6->begin(), E = $6->end();
2547 unsigned index = 1;
2548 for (; I != E; ++I, ++index) {
Reid Spencere1553cc2006-12-31 05:40:12 +00002549 const Type *Ty = I->Val->getType();
2550 if (Ty == Type::VoidTy)
2551 GEN_ERROR("Short call syntax cannot be used with varargs");
2552 ParamTypes.push_back(Ty);
Reid Spencer4f859aa2007-04-22 05:46:44 +00002553 if (I->Attrs != ParamAttr::None) {
2554 ParamAttrsWithIndex PAWI; PAWI.index = index; PAWI.attrs = I->Attrs;
2555 Attrs.push_back(PAWI);
2556 }
Chris Lattner2079fde2001-10-13 06:41:08 +00002557 }
2558
Reid Spencer4f859aa2007-04-22 05:46:44 +00002559 ParamAttrsList *PAL = 0;
2560 if (!Attrs.empty())
2561 PAL = ParamAttrsList::get(Attrs);
2562 Ty = FunctionType::get($3->get(), ParamTypes, false, PAL);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002563 PFTy = PointerType::get(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00002564 }
Chris Lattner2079fde2001-10-13 06:41:08 +00002565
Reid Spencer2c9df212007-03-20 01:13:00 +00002566 delete $3;
2567
Chris Lattnera8e8f162005-05-06 20:27:19 +00002568 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002569 CHECK_FOR_ERROR
Reid Spencer2c261782007-01-05 17:06:19 +00002570 BasicBlock *Normal = getBBVal($11);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002571 CHECK_FOR_ERROR
Reid Spencer2c261782007-01-05 17:06:19 +00002572 BasicBlock *Except = getBBVal($14);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002573 CHECK_FOR_ERROR
Chris Lattner2079fde2001-10-13 06:41:08 +00002574
Reid Spencere1553cc2006-12-31 05:40:12 +00002575 // Check the arguments
2576 ValueList Args;
2577 if ($6->empty()) { // Has no arguments?
2578 // Make sure no arguments is a good thing!
2579 if (Ty->getNumParams() != 0)
2580 GEN_ERROR("No arguments passed to a function that "
Reid Spencerf4fa5902007-02-05 10:16:10 +00002581 "expects arguments");
Chris Lattner2079fde2001-10-13 06:41:08 +00002582 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00002583 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00002584 // correctly!
Chris Lattnerd5d89962004-02-09 04:14:01 +00002585 FunctionType::param_iterator I = Ty->param_begin();
2586 FunctionType::param_iterator E = Ty->param_end();
Reid Spencere1553cc2006-12-31 05:40:12 +00002587 ValueRefList::iterator ArgI = $6->begin(), ArgE = $6->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00002588
Reid Spencere1553cc2006-12-31 05:40:12 +00002589 for (; ArgI != ArgE && I != E; ++ArgI, ++I) {
2590 if (ArgI->Val->getType() != *I)
2591 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00002592 (*I)->getDescription() + "'");
Reid Spencere1553cc2006-12-31 05:40:12 +00002593 Args.push_back(ArgI->Val);
2594 }
Reid Spencer08de34b2006-12-03 05:45:44 +00002595
Reid Spencere1553cc2006-12-31 05:40:12 +00002596 if (Ty->isVarArg()) {
2597 if (I == E)
2598 for (; ArgI != ArgE; ++ArgI)
2599 Args.push_back(ArgI->Val); // push the remaining varargs
2600 } else if (I != E || ArgI != ArgE)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002601 GEN_ERROR("Invalid number of parameters detected");
Chris Lattner2079fde2001-10-13 06:41:08 +00002602 }
Reid Spencere1553cc2006-12-31 05:40:12 +00002603
2604 // Create the InvokeInst
Chris Lattner9d2fda62007-02-13 05:53:56 +00002605 InvokeInst *II = new InvokeInst(V, Normal, Except, &Args[0], Args.size());
Reid Spencere1553cc2006-12-31 05:40:12 +00002606 II->setCallingConv($2);
2607 $$ = II;
Chris Lattnera8e8f162005-05-06 20:27:19 +00002608 delete $6;
Reid Spencer61c83e02006-08-18 08:43:06 +00002609 CHECK_FOR_ERROR
Chris Lattner36143fc2003-09-08 18:54:55 +00002610 }
2611 | UNWIND {
2612 $$ = new UnwindInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002613 CHECK_FOR_ERROR
Chris Lattner16710e92004-10-16 18:17:13 +00002614 }
2615 | UNREACHABLE {
2616 $$ = new UnreachableInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002617 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002618 };
Chris Lattner2079fde2001-10-13 06:41:08 +00002619
2620
Chris Lattner00950542001-06-06 20:29:01 +00002621
2622JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
2623 $$ = $1;
Reid Spencer186a43f2007-03-19 18:39:36 +00002624 Constant *V = cast<Constant>(getExistingVal($2, $3));
Reid Spencer5b7e7532006-09-28 19:28:24 +00002625 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002626 if (V == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002627 GEN_ERROR("May only switch on a constant pool value");
Chris Lattner00950542001-06-06 20:29:01 +00002628
Reid Spencer5b7e7532006-09-28 19:28:24 +00002629 BasicBlock* tmpBB = getBBVal($6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002630 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002631 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner00950542001-06-06 20:29:01 +00002632 }
2633 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner9232b992003-04-22 18:42:41 +00002634 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Reid Spencer186a43f2007-03-19 18:39:36 +00002635 Constant *V = cast<Constant>(getExistingVal($1, $2));
Reid Spencer5b7e7532006-09-28 19:28:24 +00002636 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002637
2638 if (V == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002639 GEN_ERROR("May only switch on a constant pool value");
Chris Lattner00950542001-06-06 20:29:01 +00002640
Reid Spencer5b7e7532006-09-28 19:28:24 +00002641 BasicBlock* tmpBB = getBBVal($5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002642 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002643 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner51727be2002-06-04 21:58:56 +00002644 };
Chris Lattner00950542001-06-06 20:29:01 +00002645
Reid Spencerb2d17862007-01-26 08:04:51 +00002646Inst : OptLocalAssign InstVal {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002647 // Is this definition named?? if so, assign the name...
2648 setValueName($2, $1);
2649 CHECK_FOR_ERROR
2650 InsertValue($2);
2651 $$ = $2;
2652 CHECK_FOR_ERROR
2653 };
2654
Chris Lattner00950542001-06-06 20:29:01 +00002655
Chris Lattnerc24d2082001-06-11 15:04:20 +00002656PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Reid Spencere1553cc2006-12-31 05:40:12 +00002657 if (!UpRefs.empty())
2658 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Chris Lattner9232b992003-04-22 18:42:41 +00002659 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Reid Spencer08de34b2006-12-03 05:45:44 +00002660 Value* tmpVal = getVal(*$1, $3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002661 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002662 BasicBlock* tmpBB = getBBVal($5);
2663 CHECK_FOR_ERROR
2664 $$->push_back(std::make_pair(tmpVal, tmpBB));
Reid Spencer08de34b2006-12-03 05:45:44 +00002665 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00002666 }
2667 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
2668 $$ = $1;
Reid Spencer5b7e7532006-09-28 19:28:24 +00002669 Value* tmpVal = getVal($1->front().first->getType(), $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00002670 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002671 BasicBlock* tmpBB = getBBVal($6);
2672 CHECK_FOR_ERROR
2673 $1->push_back(std::make_pair(tmpVal, tmpBB));
Chris Lattner51727be2002-06-04 21:58:56 +00002674 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00002675
2676
Reid Spencere1553cc2006-12-31 05:40:12 +00002677ValueRefList : Types ValueRef OptParamAttrs {
2678 if (!UpRefs.empty())
2679 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2680 // Used for call and invoke instructions
2681 $$ = new ValueRefList();
2682 ValueRefListEntry E; E.Attrs = $3; E.Val = getVal($1->get(), $2);
2683 $$->push_back(E);
Reid Spencer2c9df212007-03-20 01:13:00 +00002684 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00002685 }
Reid Spencere1553cc2006-12-31 05:40:12 +00002686 | ValueRefList ',' Types ValueRef OptParamAttrs {
2687 if (!UpRefs.empty())
2688 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattner00950542001-06-06 20:29:01 +00002689 $$ = $1;
Reid Spencere1553cc2006-12-31 05:40:12 +00002690 ValueRefListEntry E; E.Attrs = $5; E.Val = getVal($3->get(), $4);
2691 $$->push_back(E);
Reid Spencer2c9df212007-03-20 01:13:00 +00002692 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002693 CHECK_FOR_ERROR
Reid Spencere1553cc2006-12-31 05:40:12 +00002694 }
2695 | /*empty*/ { $$ = new ValueRefList(); };
Chris Lattner00950542001-06-06 20:29:01 +00002696
Reid Spencere1553cc2006-12-31 05:40:12 +00002697IndexList // Used for gep instructions and constant expressions
Reid Spencere03969f2006-12-31 21:46:36 +00002698 : /*empty*/ { $$ = new std::vector<Value*>(); }
Reid Spencere1553cc2006-12-31 05:40:12 +00002699 | IndexList ',' ResolvedVal {
2700 $$ = $1;
2701 $$->push_back($3);
2702 CHECK_FOR_ERROR
2703 }
Reid Spencer71305d72006-12-31 21:25:25 +00002704 ;
Chris Lattner00950542001-06-06 20:29:01 +00002705
Chris Lattnerddb6db42005-05-06 05:51:46 +00002706OptTailCall : TAIL CALL {
2707 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002708 CHECK_FOR_ERROR
Chris Lattnerddb6db42005-05-06 05:51:46 +00002709 }
2710 | CALL {
2711 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002712 CHECK_FOR_ERROR
Chris Lattnerddb6db42005-05-06 05:51:46 +00002713 };
2714
Chris Lattner4a6482b2002-09-10 19:57:26 +00002715InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00002716 if (!UpRefs.empty())
2717 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Chris Lattner42a75512007-01-15 02:27:26 +00002718 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint() &&
Reid Spencer9d6565a2007-02-15 02:26:10 +00002719 !isa<VectorType>((*$2).get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002720 GEN_ERROR(
Reid Spencerf4fa5902007-02-05 10:16:10 +00002721 "Arithmetic operator requires integer, FP, or packed operands");
Reid Spencer9d6565a2007-02-15 02:26:10 +00002722 if (isa<VectorType>((*$2).get()) &&
Reid Spencer08de34b2006-12-03 05:45:44 +00002723 ($1 == Instruction::URem ||
2724 $1 == Instruction::SRem ||
2725 $1 == Instruction::FRem))
Reid Spencerac9dcb92007-02-15 03:39:18 +00002726 GEN_ERROR("Remainder not supported on vector types");
Reid Spencer08de34b2006-12-03 05:45:44 +00002727 Value* val1 = getVal(*$2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002728 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00002729 Value* val2 = getVal(*$2, $5);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002730 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00002731 $$ = BinaryOperator::create($1, val1, val2);
Chris Lattner4a6482b2002-09-10 19:57:26 +00002732 if ($$ == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002733 GEN_ERROR("binary operator returned null");
Reid Spencer08de34b2006-12-03 05:45:44 +00002734 delete $2;
Chris Lattner4a6482b2002-09-10 19:57:26 +00002735 }
2736 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00002737 if (!UpRefs.empty())
2738 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Chris Lattner42a75512007-01-15 02:27:26 +00002739 if (!(*$2)->isInteger()) {
Reid Spencer9d6565a2007-02-15 02:26:10 +00002740 if (Instruction::isShift($1) || !isa<VectorType>($2->get()) ||
2741 !cast<VectorType>($2->get())->getElementType()->isInteger())
Reid Spencerf4fa5902007-02-05 10:16:10 +00002742 GEN_ERROR("Logical operator requires integral operands");
Chris Lattner0a017832005-12-21 18:31:29 +00002743 }
Reid Spencer08de34b2006-12-03 05:45:44 +00002744 Value* tmpVal1 = getVal(*$2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002745 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00002746 Value* tmpVal2 = getVal(*$2, $5);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002747 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00002748 $$ = BinaryOperator::create($1, tmpVal1, tmpVal2);
Chris Lattner4a6482b2002-09-10 19:57:26 +00002749 if ($$ == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002750 GEN_ERROR("binary operator returned null");
Reid Spencer08de34b2006-12-03 05:45:44 +00002751 delete $2;
Chris Lattner4a6482b2002-09-10 19:57:26 +00002752 }
Reid Spencer08de34b2006-12-03 05:45:44 +00002753 | ICMP IPredicates Types ValueRef ',' ValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00002754 if (!UpRefs.empty())
2755 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencer9d6565a2007-02-15 02:26:10 +00002756 if (isa<VectorType>((*$3).get()))
Reid Spencerac9dcb92007-02-15 03:39:18 +00002757 GEN_ERROR("Vector types not supported by icmp instruction");
Reid Spencer08de34b2006-12-03 05:45:44 +00002758 Value* tmpVal1 = getVal(*$3, $4);
2759 CHECK_FOR_ERROR
2760 Value* tmpVal2 = getVal(*$3, $6);
2761 CHECK_FOR_ERROR
2762 $$ = CmpInst::create($1, $2, tmpVal1, tmpVal2);
2763 if ($$ == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002764 GEN_ERROR("icmp operator returned null");
Reid Spencer2c9df212007-03-20 01:13:00 +00002765 delete $3;
Reid Spencer08de34b2006-12-03 05:45:44 +00002766 }
2767 | FCMP FPredicates Types ValueRef ',' ValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00002768 if (!UpRefs.empty())
2769 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencer9d6565a2007-02-15 02:26:10 +00002770 if (isa<VectorType>((*$3).get()))
Reid Spencerac9dcb92007-02-15 03:39:18 +00002771 GEN_ERROR("Vector types not supported by fcmp instruction");
Reid Spencer08de34b2006-12-03 05:45:44 +00002772 Value* tmpVal1 = getVal(*$3, $4);
2773 CHECK_FOR_ERROR
2774 Value* tmpVal2 = getVal(*$3, $6);
2775 CHECK_FOR_ERROR
2776 $$ = CmpInst::create($1, $2, tmpVal1, tmpVal2);
2777 if ($$ == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002778 GEN_ERROR("fcmp operator returned null");
Reid Spencer2c9df212007-03-20 01:13:00 +00002779 delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00002780 }
Reid Spencer3da59db2006-11-27 01:05:10 +00002781 | CastOps ResolvedVal TO Types {
Reid Spencere1553cc2006-12-31 05:40:12 +00002782 if (!UpRefs.empty())
2783 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00002784 Value* Val = $2;
Reid Spencer93947c32007-01-17 02:47:33 +00002785 const Type* DestTy = $4->get();
2786 if (!CastInst::castIsValid($1, Val, DestTy))
2787 GEN_ERROR("invalid cast opcode for cast from '" +
2788 Val->getType()->getDescription() + "' to '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00002789 DestTy->getDescription() + "'");
Reid Spencer93947c32007-01-17 02:47:33 +00002790 $$ = CastInst::create($1, Val, DestTy);
Reid Spencer08de34b2006-12-03 05:45:44 +00002791 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00002792 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00002793 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer4fe16d62007-01-11 18:21:29 +00002794 if ($2->getType() != Type::Int1Ty)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002795 GEN_ERROR("select condition must be boolean");
Reid Spencer08de34b2006-12-03 05:45:44 +00002796 if ($4->getType() != $6->getType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00002797 GEN_ERROR("select value types should match");
Reid Spencer08de34b2006-12-03 05:45:44 +00002798 $$ = new SelectInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002799 CHECK_FOR_ERROR
Chris Lattner76f0ff32004-03-12 05:51:36 +00002800 }
Chris Lattner99e7ab72003-10-18 05:53:13 +00002801 | VAARG ResolvedVal ',' Types {
Reid Spencere1553cc2006-12-31 05:40:12 +00002802 if (!UpRefs.empty())
2803 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00002804 $$ = new VAArgInst($2, *$4);
2805 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00002806 CHECK_FOR_ERROR
Chris Lattner99e7ab72003-10-18 05:53:13 +00002807 }
Robert Bocchino9c62b562006-01-10 19:04:32 +00002808 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencer08de34b2006-12-03 05:45:44 +00002809 if (!ExtractElementInst::isValidOperands($2, $4))
Reid Spencerf4fa5902007-02-05 10:16:10 +00002810 GEN_ERROR("Invalid extractelement operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00002811 $$ = new ExtractElementInst($2, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00002812 CHECK_FOR_ERROR
Robert Bocchino9c62b562006-01-10 19:04:32 +00002813 }
Robert Bocchino2def1b32006-01-17 20:06:25 +00002814 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer08de34b2006-12-03 05:45:44 +00002815 if (!InsertElementInst::isValidOperands($2, $4, $6))
Reid Spencerf4fa5902007-02-05 10:16:10 +00002816 GEN_ERROR("Invalid insertelement operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00002817 $$ = new InsertElementInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002818 CHECK_FOR_ERROR
Robert Bocchino2def1b32006-01-17 20:06:25 +00002819 }
Chris Lattner4c949082006-04-08 01:18:35 +00002820 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer08de34b2006-12-03 05:45:44 +00002821 if (!ShuffleVectorInst::isValidOperands($2, $4, $6))
Reid Spencerf4fa5902007-02-05 10:16:10 +00002822 GEN_ERROR("Invalid shufflevector operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00002823 $$ = new ShuffleVectorInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002824 CHECK_FOR_ERROR
Chris Lattner4c949082006-04-08 01:18:35 +00002825 }
Chris Lattner3b237fc2003-10-19 21:34:28 +00002826 | PHI_TOK PHIList {
Chris Lattnerc24d2082001-06-11 15:04:20 +00002827 const Type *Ty = $2->front().first->getType();
Chris Lattner8ea8f362003-10-30 01:38:18 +00002828 if (!Ty->isFirstClassType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00002829 GEN_ERROR("PHI node operands must be of first class type");
Chris Lattnerc24d2082001-06-11 15:04:20 +00002830 $$ = new PHINode(Ty);
Chris Lattnerce1df9e2005-01-29 00:35:55 +00002831 ((PHINode*)$$)->reserveOperandSpace($2->size());
Chris Lattner00950542001-06-06 20:29:01 +00002832 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00002833 if ($2->front().first->getType() != Ty)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002834 GEN_ERROR("All elements of a PHI node must be of the same type");
Chris Lattnerb00c5822001-10-02 03:41:24 +00002835 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00002836 $2->pop_front();
2837 }
2838 delete $2; // Free the list...
Reid Spencer61c83e02006-08-18 08:43:06 +00002839 CHECK_FOR_ERROR
Chris Lattnerddb6db42005-05-06 05:51:46 +00002840 }
Reid Spencer2c261782007-01-05 17:06:19 +00002841 | OptTailCall OptCallingConv ResultTypes ValueRef '(' ValueRefList ')'
2842 OptFuncAttrs {
Reid Spencere1553cc2006-12-31 05:40:12 +00002843
2844 // Handle the short syntax
Bill Wendlingb39a55a2006-11-12 11:10:39 +00002845 const PointerType *PFTy = 0;
2846 const FunctionType *Ty = 0;
Reid Spencer2c261782007-01-05 17:06:19 +00002847 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002848 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00002849 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00002850 std::vector<const Type*> ParamTypes;
Reid Spencer4f859aa2007-04-22 05:46:44 +00002851 ParamAttrsVector Attrs;
2852 if ($8 != ParamAttr::None) {
2853 ParamAttrsWithIndex PAWI; PAWI.index = 0; PAWI.attrs = $8;
2854 Attrs.push_back(PAWI);
2855 }
Reid Spencer5694b6e2007-04-09 06:17:21 +00002856 unsigned index = 1;
2857 ValueRefList::iterator I = $6->begin(), E = $6->end();
2858 for (; I != E; ++I, ++index) {
Reid Spencere1553cc2006-12-31 05:40:12 +00002859 const Type *Ty = I->Val->getType();
2860 if (Ty == Type::VoidTy)
2861 GEN_ERROR("Short call syntax cannot be used with varargs");
2862 ParamTypes.push_back(Ty);
Reid Spencer4f859aa2007-04-22 05:46:44 +00002863 if (I->Attrs != ParamAttr::None) {
2864 ParamAttrsWithIndex PAWI; PAWI.index = index; PAWI.attrs = I->Attrs;
2865 Attrs.push_back(PAWI);
2866 }
Chris Lattneref9c23f2001-10-03 14:53:21 +00002867 }
Chris Lattner2079fde2001-10-13 06:41:08 +00002868
Reid Spencer4f859aa2007-04-22 05:46:44 +00002869 ParamAttrsList *PAL = 0;
2870 if (!Attrs.empty())
2871 PAL = ParamAttrsList::get(Attrs);
Reid Spencer5694b6e2007-04-09 06:17:21 +00002872
Reid Spencer4f859aa2007-04-22 05:46:44 +00002873 Ty = FunctionType::get($3->get(), ParamTypes, false, PAL);
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 Spencere1553cc2006-12-31 05:40:12 +00002889 // Check the arguments
2890 ValueList Args;
2891 if ($6->empty()) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00002892 // Make sure no arguments is a good thing!
2893 if (Ty->getNumParams() != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002894 GEN_ERROR("No arguments passed to a function that "
Reid Spencerf4fa5902007-02-05 10:16:10 +00002895 "expects arguments");
Chris Lattner8b81bf52001-07-25 22:47:46 +00002896 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00002897 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner00950542001-06-06 20:29:01 +00002898 // correctly!
2899 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00002900 FunctionType::param_iterator I = Ty->param_begin();
2901 FunctionType::param_iterator E = Ty->param_end();
Reid Spencere1553cc2006-12-31 05:40:12 +00002902 ValueRefList::iterator ArgI = $6->begin(), ArgE = $6->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00002903
Reid Spencere1553cc2006-12-31 05:40:12 +00002904 for (; ArgI != ArgE && I != E; ++ArgI, ++I) {
2905 if (ArgI->Val->getType() != *I)
2906 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00002907 (*I)->getDescription() + "'");
Reid Spencere1553cc2006-12-31 05:40:12 +00002908 Args.push_back(ArgI->Val);
2909 }
2910 if (Ty->isVarArg()) {
2911 if (I == E)
2912 for (; ArgI != ArgE; ++ArgI)
2913 Args.push_back(ArgI->Val); // push the remaining varargs
2914 } else if (I != E || ArgI != ArgE)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002915 GEN_ERROR("Invalid number of parameters detected");
Chris Lattner8b81bf52001-07-25 22:47:46 +00002916 }
Reid Spencere1553cc2006-12-31 05:40:12 +00002917 // Create the call node
Chris Lattner9d2fda62007-02-13 05:53:56 +00002918 CallInst *CI = new CallInst(V, &Args[0], Args.size());
Reid Spencere1553cc2006-12-31 05:40:12 +00002919 CI->setTailCall($1);
2920 CI->setCallingConv($2);
2921 $$ = CI;
Chris Lattnera8e8f162005-05-06 20:27:19 +00002922 delete $6;
Reid Spencerb2d17862007-01-26 08:04:51 +00002923 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002924 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002925 }
2926 | MemoryInst {
2927 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002928 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002929 };
Chris Lattner00950542001-06-06 20:29:01 +00002930
Chris Lattner15c9c032003-09-08 18:20:29 +00002931OptVolatile : VOLATILE {
2932 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002933 CHECK_FOR_ERROR
Chris Lattner15c9c032003-09-08 18:20:29 +00002934 }
2935 | /* empty */ {
2936 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002937 CHECK_FOR_ERROR
Chris Lattner15c9c032003-09-08 18:20:29 +00002938 };
2939
Chris Lattner027dcc52001-07-08 21:10:27 +00002940
Chris Lattnerddb6db42005-05-06 05:51:46 +00002941
Chris Lattner66db8e42005-11-06 06:34:12 +00002942MemoryInst : MALLOC Types OptCAlign {
Reid Spencere1553cc2006-12-31 05:40:12 +00002943 if (!UpRefs.empty())
2944 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00002945 $$ = new MallocInst(*$2, 0, $3);
2946 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002947 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002948 }
Reid Spencera54b7cb2007-01-12 07:05:14 +00002949 | MALLOC Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencere1553cc2006-12-31 05:40:12 +00002950 if (!UpRefs.empty())
2951 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00002952 Value* tmpVal = getVal($4, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002953 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00002954 $$ = new MallocInst(*$2, tmpVal, $6);
2955 delete $2;
Nate Begeman14b05292005-11-05 09:21:28 +00002956 }
Chris Lattner66db8e42005-11-06 06:34:12 +00002957 | ALLOCA Types OptCAlign {
Reid Spencere1553cc2006-12-31 05:40:12 +00002958 if (!UpRefs.empty())
2959 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00002960 $$ = new AllocaInst(*$2, 0, $3);
2961 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002962 CHECK_FOR_ERROR
Nate Begeman14b05292005-11-05 09:21:28 +00002963 }
Reid Spencera54b7cb2007-01-12 07:05:14 +00002964 | ALLOCA Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencere1553cc2006-12-31 05:40:12 +00002965 if (!UpRefs.empty())
2966 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00002967 Value* tmpVal = getVal($4, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002968 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00002969 $$ = new AllocaInst(*$2, tmpVal, $6);
2970 delete $2;
Nate Begeman14b05292005-11-05 09:21:28 +00002971 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002972 | FREE ResolvedVal {
Reid Spencer08de34b2006-12-03 05:45:44 +00002973 if (!isa<PointerType>($2->getType()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002974 GEN_ERROR("Trying to free nonpointer type " +
Reid Spencerf4fa5902007-02-05 10:16:10 +00002975 $2->getType()->getDescription() + "");
Reid Spencer08de34b2006-12-03 05:45:44 +00002976 $$ = new FreeInst($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00002977 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002978 }
2979
Christopher Lamb43c7f372007-04-22 19:24:39 +00002980 | OptVolatile LOAD Types ValueRef OptCAlign {
Reid Spencere1553cc2006-12-31 05:40:12 +00002981 if (!UpRefs.empty())
2982 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00002983 if (!isa<PointerType>($3->get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002984 GEN_ERROR("Can't load from nonpointer type: " +
Reid Spencer08de34b2006-12-03 05:45:44 +00002985 (*$3)->getDescription());
2986 if (!cast<PointerType>($3->get())->getElementType()->isFirstClassType())
Reid Spencer61c83e02006-08-18 08:43:06 +00002987 GEN_ERROR("Can't load from pointer of non-first-class type: " +
Reid Spencer08de34b2006-12-03 05:45:44 +00002988 (*$3)->getDescription());
2989 Value* tmpVal = getVal(*$3, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00002990 CHECK_FOR_ERROR
Christopher Lamb43c7f372007-04-22 19:24:39 +00002991 $$ = new LoadInst(tmpVal, "", $1, $5);
Reid Spencer08de34b2006-12-03 05:45:44 +00002992 delete $3;
Chris Lattner027dcc52001-07-08 21:10:27 +00002993 }
Christopher Lamb43c7f372007-04-22 19:24:39 +00002994 | OptVolatile STORE ResolvedVal ',' Types ValueRef OptCAlign {
Reid Spencere1553cc2006-12-31 05:40:12 +00002995 if (!UpRefs.empty())
2996 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00002997 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002998 if (!PT)
Reid Spencer61c83e02006-08-18 08:43:06 +00002999 GEN_ERROR("Can't store to a nonpointer type: " +
Reid Spencer08de34b2006-12-03 05:45:44 +00003000 (*$5)->getDescription());
Chris Lattner8c8418d2003-09-01 16:31:28 +00003001 const Type *ElTy = PT->getElementType();
Reid Spencer08de34b2006-12-03 05:45:44 +00003002 if (ElTy != $3->getType())
3003 GEN_ERROR("Can't store '" + $3->getType()->getDescription() +
Reid Spencerf4fa5902007-02-05 10:16:10 +00003004 "' into space of type '" + ElTy->getDescription() + "'");
Chris Lattner0383cc42002-08-21 23:51:21 +00003005
Reid Spencer08de34b2006-12-03 05:45:44 +00003006 Value* tmpVal = getVal(*$5, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00003007 CHECK_FOR_ERROR
Christopher Lamb43c7f372007-04-22 19:24:39 +00003008 $$ = new StoreInst($3, tmpVal, $1, $7);
Reid Spencer08de34b2006-12-03 05:45:44 +00003009 delete $5;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00003010 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00003011 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencere1553cc2006-12-31 05:40:12 +00003012 if (!UpRefs.empty())
3013 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00003014 if (!isa<PointerType>($2->get()))
Reid Spencerf4fa5902007-02-05 10:16:10 +00003015 GEN_ERROR("getelementptr insn requires pointer operand");
Chris Lattner830b6f92004-04-05 01:30:04 +00003016
Chris Lattnerc856c7a2007-02-13 00:57:40 +00003017 if (!GetElementPtrInst::getIndexedType(*$2, &(*$4)[0], $4->size(), true))
Reid Spencer61c83e02006-08-18 08:43:06 +00003018 GEN_ERROR("Invalid getelementptr indices for type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00003019 (*$2)->getDescription()+ "'");
Reid Spencer08de34b2006-12-03 05:45:44 +00003020 Value* tmpVal = getVal(*$2, $3);
Reid Spencer61c83e02006-08-18 08:43:06 +00003021 CHECK_FOR_ERROR
Chris Lattnerc856c7a2007-02-13 00:57:40 +00003022 $$ = new GetElementPtrInst(tmpVal, &(*$4)[0], $4->size());
Reid Spencer08de34b2006-12-03 05:45:44 +00003023 delete $2;
Reid Spencer5b7e7532006-09-28 19:28:24 +00003024 delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00003025 };
Chris Lattner027dcc52001-07-08 21:10:27 +00003026
Brian Gaeked0fde302003-11-11 22:41:34 +00003027
Chris Lattner00950542001-06-06 20:29:01 +00003028%%
Reid Spencer61c83e02006-08-18 08:43:06 +00003029
Reid Spencere1553cc2006-12-31 05:40:12 +00003030// common code from the two 'RunVMAsmParser' functions
3031static Module* RunParser(Module * M) {
3032
3033 llvmAsmlineno = 1; // Reset the current line number...
3034 CurModule.CurrentModule = M;
3035#if YYDEBUG
3036 yydebug = Debug;
3037#endif
3038
3039 // Check to make sure the parser succeeded
3040 if (yyparse()) {
3041 if (ParserResult)
3042 delete ParserResult;
3043 return 0;
3044 }
3045
Reid Spencercd5bd902007-03-30 01:37:13 +00003046 // Emit an error if there are any unresolved types left.
3047 if (!CurModule.LateResolveTypes.empty()) {
3048 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
3049 if (DID.Type == ValID::LocalName) {
3050 GenerateError("Undefined type remains at eof: '"+DID.getName() + "'");
3051 } else {
3052 GenerateError("Undefined type remains at eof: #" + itostr(DID.Num));
3053 }
3054 if (ParserResult)
3055 delete ParserResult;
3056 return 0;
3057 }
3058
3059 // Emit an error if there are any unresolved values left.
3060 if (!CurModule.LateResolveValues.empty()) {
3061 Value *V = CurModule.LateResolveValues.back();
3062 std::map<Value*, std::pair<ValID, int> >::iterator I =
3063 CurModule.PlaceHolderInfo.find(V);
3064
3065 if (I != CurModule.PlaceHolderInfo.end()) {
3066 ValID &DID = I->second.first;
3067 if (DID.Type == ValID::LocalName) {
3068 GenerateError("Undefined value remains at eof: "+DID.getName() + "'");
3069 } else {
3070 GenerateError("Undefined value remains at eof: #" + itostr(DID.Num));
3071 }
3072 if (ParserResult)
3073 delete ParserResult;
3074 return 0;
3075 }
3076 }
3077
Reid Spencere1553cc2006-12-31 05:40:12 +00003078 // Check to make sure that parsing produced a result
3079 if (!ParserResult)
3080 return 0;
3081
3082 // Reset ParserResult variable while saving its value for the result.
3083 Module *Result = ParserResult;
3084 ParserResult = 0;
3085
3086 return Result;
3087}
3088
Reid Spencer61c83e02006-08-18 08:43:06 +00003089void llvm::GenerateError(const std::string &message, int LineNo) {
3090 if (LineNo == -1) LineNo = llvmAsmlineno;
3091 // TODO: column number in exception
3092 if (TheParseError)
3093 TheParseError->setError(CurFilename, message, LineNo);
3094 TriggerError = 1;
3095}
3096
Chris Lattner09083092001-07-08 04:57:15 +00003097int yyerror(const char *ErrorMsg) {
Chris Lattner9232b992003-04-22 18:42:41 +00003098 std::string where
3099 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00003100 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Reid Spenceref9b9a72007-02-05 20:47:22 +00003101 std::string errMsg = where + "error: " + std::string(ErrorMsg);
3102 if (yychar != YYEMPTY && yychar != 0)
3103 errMsg += " while reading token: '" + std::string(llvmAsmtext, llvmAsmleng)+
3104 "'";
Reid Spencer61c83e02006-08-18 08:43:06 +00003105 GenerateError(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00003106 return 0;
3107}