blob: 28eb339aa405fa2c2ef44060d095e07ef0ff3000 [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===-- llvmAsmParser.y - Parser for llvm assembly files --------*- C++ -*-===//
Misha Brukman5a2a3822005-05-10 22:02:28 +00002//
John Criswell856ba762003-10-21 15:17:13 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman5a2a3822005-05-10 22:02:28 +00007//
John Criswell856ba762003-10-21 15:17:13 +00008//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
10// This file implements the bison parser for LLVM assembly languages files.
11//
Chris Lattnercf3056d2003-10-13 03:32:08 +000012//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +000013
Chris Lattner00950542001-06-06 20:29:01 +000014%{
15#include "ParserInternals.h"
Chris Lattnera8e8f162005-05-06 20:27:19 +000016#include "llvm/CallingConv.h"
Chris Lattneraa2c8532006-01-25 22:26:43 +000017#include "llvm/InlineAsm.h"
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000018#include "llvm/Instructions.h"
Chris Lattner00950542001-06-06 20:29:01 +000019#include "llvm/Module.h"
Reid Spenceref9b9a72007-02-05 20:47:22 +000020#include "llvm/ValueSymbolTable.h"
Chris Lattner830b6f92004-04-05 01:30:04 +000021#include "llvm/Support/GetElementPtrTypeIterator.h"
Reid Spencere1553cc2006-12-31 05:40:12 +000022#include "llvm/Support/CommandLine.h"
Chris Lattnere0135402007-01-31 04:43:46 +000023#include "llvm/ADT/SmallVector.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000024#include "llvm/ADT/STLExtras.h"
Chris Lattner87ac9722005-11-06 06:46:28 +000025#include "llvm/Support/MathExtras.h"
Bill Wendling3e3bcbf2006-11-28 22:47:12 +000026#include "llvm/Support/Streams.h"
Reid Spencer8ce1da72004-07-04 12:19:05 +000027#include <algorithm>
Chris Lattner00950542001-06-06 20:29:01 +000028#include <list>
Chris Lattnerc02659f2007-02-11 21:39:35 +000029#include <map>
Chris Lattnerc188eeb2002-07-30 18:54:25 +000030#include <utility>
Reid Spencere1553cc2006-12-31 05:40:12 +000031#ifndef NDEBUG
32#define YYDEBUG 1
33#endif
Chris Lattner00950542001-06-06 20:29:01 +000034
Reid Spencere4f47592006-08-18 17:32:55 +000035// The following is a gross hack. In order to rid the libAsmParser library of
36// exceptions, we have to have a way of getting the yyparse function to go into
37// an error situation. So, whenever we want an error to occur, the GenerateError
38// function (see bottom of file) sets TriggerError. Then, at the end of each
39// production in the grammer we use CHECK_FOR_ERROR which will invoke YYERROR
40// (a goto) to put YACC in error state. Furthermore, several calls to
41// GenerateError are made from inside productions and they must simulate the
42// previous exception behavior by exiting the production immediately. We have
43// replaced these with the GEN_ERROR macro which calls GeneratError and then
44// immediately invokes YYERROR. This would be so much cleaner if it was a
45// recursive descent parser.
Reid Spencer61c83e02006-08-18 08:43:06 +000046static bool TriggerError = false;
Reid Spencerf63697d2006-10-09 17:36:59 +000047#define CHECK_FOR_ERROR { if (TriggerError) { TriggerError = false; YYABORT; } }
Reid Spencer61c83e02006-08-18 08:43:06 +000048#define GEN_ERROR(msg) { GenerateError(msg); YYERROR; }
49
Chris Lattner386a3b72001-10-16 19:54:17 +000050int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
Chris Lattner09083092001-07-08 04:57:15 +000051int yylex(); // declaration" of xxx warnings.
Chris Lattner00950542001-06-06 20:29:01 +000052int yyparse();
53
Brian Gaeked0fde302003-11-11 22:41:34 +000054namespace llvm {
Chris Lattner86427632004-07-14 06:39:48 +000055 std::string CurFilename;
Reid Spencere1553cc2006-12-31 05:40:12 +000056#if YYDEBUG
57static cl::opt<bool>
58Debug("debug-yacc", cl::desc("Print yacc debug state changes"),
59 cl::Hidden, cl::init(false));
60#endif
Chris Lattner86427632004-07-14 06:39:48 +000061}
62using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000063
Chris Lattner00950542001-06-06 20:29:01 +000064static Module *ParserResult;
Chris Lattner00950542001-06-06 20:29:01 +000065
Chris Lattner30c89792001-09-07 16:35:17 +000066// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
67// relating to upreferences in the input stream.
68//
69//#define DEBUG_UPREFS 1
70#ifdef DEBUG_UPREFS
Bill Wendlinge8156192006-12-07 01:30:32 +000071#define UR_OUT(X) cerr << X
Chris Lattner30c89792001-09-07 16:35:17 +000072#else
73#define UR_OUT(X)
74#endif
75
Vikram S. Adved3f7eb02002-07-14 22:59:28 +000076#define YYERROR_VERBOSE 1
77
Chris Lattnerb2b96672005-11-12 18:21:21 +000078static GlobalVariable *CurGV;
Andrew Lenharth558bc882005-06-18 18:34:52 +000079
80
Chris Lattner7e708292002-06-25 16:13:24 +000081// This contains info used when building the body of a function. It is
82// destroyed when the function is completed.
Chris Lattner00950542001-06-06 20:29:01 +000083//
Chris Lattner9232b992003-04-22 18:42:41 +000084typedef std::vector<Value *> ValueList; // Numbered defs
Reid Spencere1553cc2006-12-31 05:40:12 +000085
Misha Brukman5a2a3822005-05-10 22:02:28 +000086static void
Reid Spencer186a43f2007-03-19 18:39:36 +000087ResolveDefinitions(ValueList &LateResolvers, ValueList *FutureLateResolvers=0);
Chris Lattner00950542001-06-06 20:29:01 +000088
89static struct PerModuleInfo {
90 Module *CurrentModule;
Reid Spencer186a43f2007-03-19 18:39:36 +000091 ValueList Values; // Module level numbered definitions
92 ValueList LateResolveValues;
Reid Spencerb78b9082006-11-28 07:28:14 +000093 std::vector<PATypeHolder> Types;
94 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner00950542001-06-06 20:29:01 +000095
Chris Lattnerbc721ed2004-07-13 08:28:21 +000096 /// PlaceHolderInfo - When temporary placeholder objects are created, remember
Reid Spencerfd20c0a2006-05-29 02:34:34 +000097 /// how they were referenced and on which line of the input they came from so
Chris Lattner22ae2322004-07-13 08:39:15 +000098 /// that we can resolve them later and print error messages as appropriate.
Chris Lattnerbc721ed2004-07-13 08:28:21 +000099 std::map<Value*, std::pair<ValID, int> > PlaceHolderInfo;
100
Chris Lattner2079fde2001-10-13 06:41:08 +0000101 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
102 // references to global values. Global values may be referenced before they
103 // are defined, and if so, the temporary object that they represent is held
Reid Spencer3271ed52004-07-18 00:08:11 +0000104 // here. This is used for forward references of GlobalValues.
Chris Lattner2079fde2001-10-13 06:41:08 +0000105 //
Chris Lattner9232b992003-04-22 18:42:41 +0000106 typedef std::map<std::pair<const PointerType *,
Chris Lattnerbc207592004-03-08 06:09:57 +0000107 ValID>, GlobalValue*> GlobalRefsType;
Chris Lattner2079fde2001-10-13 06:41:08 +0000108 GlobalRefsType GlobalRefs;
109
Chris Lattner00950542001-06-06 20:29:01 +0000110 void ModuleDone() {
Chris Lattner7e708292002-06-25 16:13:24 +0000111 // If we could not resolve some functions at function compilation time
112 // (calls to functions before they are defined), resolve them now... Types
113 // are resolved when the constant pool has been completely parsed.
Chris Lattner30c89792001-09-07 16:35:17 +0000114 //
Chris Lattner00950542001-06-06 20:29:01 +0000115 ResolveDefinitions(LateResolveValues);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000116 if (TriggerError)
117 return;
Chris Lattner00950542001-06-06 20:29:01 +0000118
Chris Lattner2079fde2001-10-13 06:41:08 +0000119 // Check to make sure that all global value forward references have been
120 // resolved!
121 //
122 if (!GlobalRefs.empty()) {
Chris Lattner9232b992003-04-22 18:42:41 +0000123 std::string UndefinedReferences = "Unresolved global references exist:\n";
Misha Brukman5a2a3822005-05-10 22:02:28 +0000124
Chris Lattner749ce032002-03-11 22:12:39 +0000125 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
126 I != E; ++I) {
127 UndefinedReferences += " " + I->first.first->getDescription() + " " +
128 I->first.second.getName() + "\n";
129 }
Reid Spencer61c83e02006-08-18 08:43:06 +0000130 GenerateError(UndefinedReferences);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000131 return;
Chris Lattner2079fde2001-10-13 06:41:08 +0000132 }
133
Chris Lattner7e708292002-06-25 16:13:24 +0000134 Values.clear(); // Clear out function local definitions
Chris Lattner30c89792001-09-07 16:35:17 +0000135 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +0000136 CurrentModule = 0;
137 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000138
Chris Lattner8a327842004-07-14 21:44:00 +0000139 // GetForwardRefForGlobal - Check to see if there is a forward reference
140 // for this global. If so, remove it from the GlobalRefs map and return it.
141 // If not, just return null.
Reid Spencer863dd882007-04-28 16:06:50 +0000142 GlobalValue *GetForwardRefForGlobal(const PointerType *PTy, ValID ID) {
Chris Lattner8a327842004-07-14 21:44:00 +0000143 // Check to see if there is a forward reference to this global variable...
144 // if there is, eliminate it and patch the reference to use the new def'n.
145 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(PTy, ID));
146 GlobalValue *Ret = 0;
147 if (I != GlobalRefs.end()) {
148 Ret = I->second;
Reid Spencer863dd882007-04-28 16:06:50 +0000149 GlobalRefs.erase(I);
Chris Lattner8a327842004-07-14 21:44:00 +0000150 }
151 return Ret;
152 }
Reid Spencer98b3c5c2007-01-02 21:53:43 +0000153
154 bool TypeIsUnresolved(PATypeHolder* PATy) {
155 // If it isn't abstract, its resolved
156 const Type* Ty = PATy->get();
157 if (!Ty->isAbstract())
158 return false;
159 // Traverse the type looking for abstract types. If it isn't abstract then
160 // we don't need to traverse that leg of the type.
161 std::vector<const Type*> WorkList, SeenList;
162 WorkList.push_back(Ty);
163 while (!WorkList.empty()) {
164 const Type* Ty = WorkList.back();
165 SeenList.push_back(Ty);
166 WorkList.pop_back();
167 if (const OpaqueType* OpTy = dyn_cast<OpaqueType>(Ty)) {
168 // Check to see if this is an unresolved type
169 std::map<ValID, PATypeHolder>::iterator I = LateResolveTypes.begin();
170 std::map<ValID, PATypeHolder>::iterator E = LateResolveTypes.end();
171 for ( ; I != E; ++I) {
172 if (I->second.get() == OpTy)
173 return true;
174 }
175 } else if (const SequentialType* SeqTy = dyn_cast<SequentialType>(Ty)) {
176 const Type* TheTy = SeqTy->getElementType();
177 if (TheTy->isAbstract() && TheTy != Ty) {
178 std::vector<const Type*>::iterator I = SeenList.begin(),
179 E = SeenList.end();
180 for ( ; I != E; ++I)
181 if (*I == TheTy)
182 break;
183 if (I == E)
184 WorkList.push_back(TheTy);
185 }
186 } else if (const StructType* StrTy = dyn_cast<StructType>(Ty)) {
187 for (unsigned i = 0; i < StrTy->getNumElements(); ++i) {
188 const Type* TheTy = StrTy->getElementType(i);
189 if (TheTy->isAbstract() && TheTy != Ty) {
190 std::vector<const Type*>::iterator I = SeenList.begin(),
191 E = SeenList.end();
192 for ( ; I != E; ++I)
193 if (*I == TheTy)
194 break;
195 if (I == E)
196 WorkList.push_back(TheTy);
197 }
198 }
199 }
200 }
201 return false;
202 }
Chris Lattner00950542001-06-06 20:29:01 +0000203} CurModule;
204
Chris Lattner79df7c02002-03-26 18:01:55 +0000205static struct PerFunctionInfo {
Chris Lattner7e708292002-06-25 16:13:24 +0000206 Function *CurrentFunction; // Pointer to current function being created
Chris Lattner00950542001-06-06 20:29:01 +0000207
Reid Spencer186a43f2007-03-19 18:39:36 +0000208 ValueList Values; // Keep track of #'d definitions
209 unsigned NextValNum;
210 ValueList LateResolveValues;
Reid Spenceref9b9a72007-02-05 20:47:22 +0000211 bool isDeclare; // Is this function a forward declararation?
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000212 GlobalValue::LinkageTypes Linkage; // Linkage for forward declaration.
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000213 GlobalValue::VisibilityTypes Visibility;
Chris Lattner00950542001-06-06 20:29:01 +0000214
Chris Lattner2e2ed292004-07-14 06:28:35 +0000215 /// BBForwardRefs - When we see forward references to basic blocks, keep
216 /// track of them here.
Reid Spencer186a43f2007-03-19 18:39:36 +0000217 std::map<ValID, BasicBlock*> BBForwardRefs;
Chris Lattner2e2ed292004-07-14 06:28:35 +0000218
Chris Lattner79df7c02002-03-26 18:01:55 +0000219 inline PerFunctionInfo() {
220 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000221 isDeclare = false;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000222 Linkage = GlobalValue::ExternalLinkage;
223 Visibility = GlobalValue::DefaultVisibility;
Chris Lattner00950542001-06-06 20:29:01 +0000224 }
225
Chris Lattner79df7c02002-03-26 18:01:55 +0000226 inline void FunctionStart(Function *M) {
227 CurrentFunction = M;
Reid Spencer186a43f2007-03-19 18:39:36 +0000228 NextValNum = 0;
Chris Lattner00950542001-06-06 20:29:01 +0000229 }
230
Chris Lattner79df7c02002-03-26 18:01:55 +0000231 void FunctionDone() {
Chris Lattner2e2ed292004-07-14 06:28:35 +0000232 // Any forward referenced blocks left?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000233 if (!BBForwardRefs.empty()) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000234 GenerateError("Undefined reference to label " +
Reid Spencer186a43f2007-03-19 18:39:36 +0000235 BBForwardRefs.begin()->second->getName());
Reid Spencer5b7e7532006-09-28 19:28:24 +0000236 return;
237 }
Chris Lattner2e2ed292004-07-14 06:28:35 +0000238
239 // Resolve all forward references now.
Chris Lattner386a3b72001-10-16 19:54:17 +0000240 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000241
Chris Lattner7e708292002-06-25 16:13:24 +0000242 Values.clear(); // Clear out function local definitions
Reid Spencer186a43f2007-03-19 18:39:36 +0000243 BBForwardRefs.clear();
Chris Lattner79df7c02002-03-26 18:01:55 +0000244 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000245 isDeclare = false;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000246 Linkage = GlobalValue::ExternalLinkage;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000247 Visibility = GlobalValue::DefaultVisibility;
Chris Lattner00950542001-06-06 20:29:01 +0000248 }
Chris Lattner394f2eb2003-10-16 20:12:13 +0000249} CurFun; // Info for the current function...
Chris Lattner00950542001-06-06 20:29:01 +0000250
Chris Lattner394f2eb2003-10-16 20:12:13 +0000251static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000252
Chris Lattner00950542001-06-06 20:29:01 +0000253
254//===----------------------------------------------------------------------===//
255// Code to handle definitions of all the types
256//===----------------------------------------------------------------------===//
257
Reid Spencer186a43f2007-03-19 18:39:36 +0000258static void InsertValue(Value *V, ValueList &ValueTab = CurFun.Values) {
259 // Things that have names or are void typed don't get slot numbers
260 if (V->hasName() || (V->getType() == Type::VoidTy))
261 return;
Chris Lattner2079fde2001-10-13 06:41:08 +0000262
Reid Spencer186a43f2007-03-19 18:39:36 +0000263 // In the case of function values, we have to allow for the forward reference
264 // of basic blocks, which are included in the numbering. Consequently, we keep
265 // track of the next insertion location with NextValNum. When a BB gets
266 // inserted, it could change the size of the CurFun.Values vector.
267 if (&ValueTab == &CurFun.Values) {
268 if (ValueTab.size() <= CurFun.NextValNum)
269 ValueTab.resize(CurFun.NextValNum+1);
270 ValueTab[CurFun.NextValNum++] = V;
271 return;
272 }
273 // For all other lists, its okay to just tack it on the back of the vector.
274 ValueTab.push_back(V);
Chris Lattner00950542001-06-06 20:29:01 +0000275}
276
Chris Lattner30c89792001-09-07 16:35:17 +0000277static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000278 switch (D.Type) {
Reid Spencerb2d17862007-01-26 08:04:51 +0000279 case ValID::LocalID: // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000280 // Module constants occupy the lowest numbered slots...
Reid Spencerb2d17862007-01-26 08:04:51 +0000281 if (D.Num < CurModule.Types.size())
282 return CurModule.Types[D.Num];
Chris Lattner42c9e772001-10-20 09:32:59 +0000283 break;
Reid Spencerb2d17862007-01-26 08:04:51 +0000284 case ValID::LocalName: // Is it a named definition?
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000285 if (const Type *N = CurModule.CurrentModule->getTypeByName(D.getName())) {
Chris Lattnera09000d2004-07-14 23:03:46 +0000286 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();
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000363 Value *N = SymTab.lookup(D.getName());
Reid Spenceref9b9a72007-02-05 20:47:22 +0000364 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();
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000374 Value *N = SymTab.lookup(D.getName());
Reid Spenceref9b9a72007-02-05 20:47:22 +0000375 if (N == 0)
376 return 0;
377 if (N->getType() != Ty)
378 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000379
380 D.destroy(); // Free old strdup'd memory...
381 return N;
382 }
383
Misha Brukman5a2a3822005-05-10 22:02:28 +0000384 // Check to make sure that "Ty" is an integral type, and that our
Chris Lattner2079fde2001-10-13 06:41:08 +0000385 // value will fit into the specified type...
386 case ValID::ConstSIntVal: // Is it a constant pool reference??
Reid Spencerb83eb642006-10-20 07:07:24 +0000387 if (!ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000388 GenerateError("Signed integral constant '" +
Misha Brukman5a2a3822005-05-10 22:02:28 +0000389 itostr(D.ConstPool64) + "' is invalid for type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +0000390 Ty->getDescription() + "'");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000391 return 0;
392 }
Reid Spencereac65742007-03-19 20:40:22 +0000393 return ConstantInt::get(Ty, D.ConstPool64, true);
Chris Lattner2079fde2001-10-13 06:41:08 +0000394
395 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Reid Spencerb83eb642006-10-20 07:07:24 +0000396 if (!ConstantInt::isValueValidForType(Ty, D.UConstPool64)) {
397 if (!ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000398 GenerateError("Integral constant '" + utostr(D.UConstPool64) +
Reid Spencerf4fa5902007-02-05 10:16:10 +0000399 "' is invalid or out of range");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000400 return 0;
Chris Lattner2079fde2001-10-13 06:41:08 +0000401 } else { // This is really a signed reference. Transmogrify.
Reid Spencereac65742007-03-19 20:40:22 +0000402 return ConstantInt::get(Ty, D.ConstPool64, true);
Chris Lattner2079fde2001-10-13 06:41:08 +0000403 }
404 } else {
Reid Spencerb83eb642006-10-20 07:07:24 +0000405 return ConstantInt::get(Ty, D.UConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000406 }
407
Chris Lattner2079fde2001-10-13 06:41:08 +0000408 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000409 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP)) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000410 GenerateError("FP constant invalid for type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000411 return 0;
412 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000413 return ConstantFP::get(Ty, D.ConstPoolFP);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000414
Chris Lattner2079fde2001-10-13 06:41:08 +0000415 case ValID::ConstNullVal: // Is it a null value?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000416 if (!isa<PointerType>(Ty)) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000417 GenerateError("Cannot create a a non pointer null");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000418 return 0;
419 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000420 return ConstantPointerNull::get(cast<PointerType>(Ty));
Misha Brukman5a2a3822005-05-10 22:02:28 +0000421
Chris Lattner16710e92004-10-16 18:17:13 +0000422 case ValID::ConstUndefVal: // Is it an undef value?
423 return UndefValue::get(Ty);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000424
Chris Lattnerf1f03df2005-12-21 17:53:02 +0000425 case ValID::ConstZeroVal: // Is it a zero value?
426 return Constant::getNullValue(Ty);
427
Chris Lattnerd78700d2002-08-16 21:14:40 +0000428 case ValID::ConstantVal: // Fully resolved constant?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000429 if (D.ConstantValue->getType() != Ty) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000430 GenerateError("Constant expression type different from required type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000431 return 0;
432 }
Chris Lattnerd78700d2002-08-16 21:14:40 +0000433 return D.ConstantValue;
434
Chris Lattneraa2c8532006-01-25 22:26:43 +0000435 case ValID::InlineAsmVal: { // Inline asm expression
436 const PointerType *PTy = dyn_cast<PointerType>(Ty);
437 const FunctionType *FTy =
438 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
Reid Spencer5b7e7532006-09-28 19:28:24 +0000439 if (!FTy || !InlineAsm::Verify(FTy, D.IAD->Constraints)) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000440 GenerateError("Invalid type for asm constraint string");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000441 return 0;
442 }
Chris Lattneraa2c8532006-01-25 22:26:43 +0000443 InlineAsm *IA = InlineAsm::get(FTy, D.IAD->AsmString, D.IAD->Constraints,
444 D.IAD->HasSideEffects);
445 D.destroy(); // Free InlineAsmDescriptor.
446 return IA;
447 }
Chris Lattner30c89792001-09-07 16:35:17 +0000448 default:
Reid Spencer1755a9a2007-02-05 17:01:20 +0000449 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000450 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000451 } // End of switch
452
Reid Spencer1755a9a2007-02-05 17:01:20 +0000453 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000454 return 0;
455}
456
Reid Spencer186a43f2007-03-19 18:39:36 +0000457// getVal - This function is identical to getExistingVal, except that if a
Chris Lattner2079fde2001-10-13 06:41:08 +0000458// value is not already defined, it "improvises" by creating a placeholder var
459// that looks and acts just like the requested variable. When the value is
460// defined later, all uses of the placeholder variable are replaced with the
461// real thing.
462//
Chris Lattner64116f92004-07-14 01:33:11 +0000463static Value *getVal(const Type *Ty, const ValID &ID) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000464 if (Ty == Type::LabelTy) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000465 GenerateError("Cannot use a basic block here");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000466 return 0;
467 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000468
Chris Lattner64116f92004-07-14 01:33:11 +0000469 // See if the value has already been defined.
Reid Spencer186a43f2007-03-19 18:39:36 +0000470 Value *V = getExistingVal(Ty, ID);
Chris Lattner2079fde2001-10-13 06:41:08 +0000471 if (V) return V;
Reid Spencer5b7e7532006-09-28 19:28:24 +0000472 if (TriggerError) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000473
Reid Spencer5b7e7532006-09-28 19:28:24 +0000474 if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty)) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000475 GenerateError("Invalid use of a composite type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000476 return 0;
477 }
Chris Lattner60cd9552005-03-23 01:29:26 +0000478
Chris Lattner00950542001-06-06 20:29:01 +0000479 // If we reached here, we referenced either a symbol that we don't know about
480 // or an id number that hasn't been read yet. We may be referencing something
481 // forward, so just create an entry to be resolved later and get to it...
482 //
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000483 switch (ID.Type) {
484 case ValID::GlobalName:
Reid Spencer3cb4dda2007-04-28 14:13:42 +0000485 case ValID::GlobalID: {
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000486 const PointerType *PTy = dyn_cast<PointerType>(Ty);
487 if (!PTy) {
488 GenerateError("Invalid type for reference to global" );
489 return 0;
490 }
491 const Type* ElTy = PTy->getElementType();
492 if (const FunctionType *FTy = dyn_cast<FunctionType>(ElTy))
493 V = new Function(FTy, GlobalValue::ExternalLinkage);
494 else
495 V = new GlobalVariable(ElTy, false, GlobalValue::ExternalLinkage);
496 break;
Reid Spencer3cb4dda2007-04-28 14:13:42 +0000497 }
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000498 default:
499 V = new Argument(Ty);
500 }
501
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000502 // Remember where this forward reference came from. FIXME, shouldn't we try
503 // to recycle these things??
Chris Lattner64116f92004-07-14 01:33:11 +0000504 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000505 llvmAsmlineno)));
506
Chris Lattner79df7c02002-03-26 18:01:55 +0000507 if (inFunctionScope())
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000508 InsertValue(V, CurFun.LateResolveValues);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000509 else
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000510 InsertValue(V, CurModule.LateResolveValues);
511 return V;
Chris Lattner00950542001-06-06 20:29:01 +0000512}
513
Reid Spencer186a43f2007-03-19 18:39:36 +0000514/// defineBBVal - This is a definition of a new basic block with the specified
515/// identifier which must be the same as CurFun.NextValNum, if its numeric.
516static BasicBlock *defineBBVal(const ValID &ID) {
Reid Spencer1755a9a2007-02-05 17:01:20 +0000517 assert(inFunctionScope() && "Can't get basic block at global scope!");
Chris Lattner64116f92004-07-14 01:33:11 +0000518
Chris Lattner2e2ed292004-07-14 06:28:35 +0000519 BasicBlock *BB = 0;
Chris Lattner64116f92004-07-14 01:33:11 +0000520
Reid Spencer186a43f2007-03-19 18:39:36 +0000521 // First, see if this was forward referenced
Chris Lattner64116f92004-07-14 01:33:11 +0000522
Reid Spencer186a43f2007-03-19 18:39:36 +0000523 std::map<ValID, BasicBlock*>::iterator BBI = CurFun.BBForwardRefs.find(ID);
524 if (BBI != CurFun.BBForwardRefs.end()) {
525 BB = BBI->second;
Chris Lattner2e2ed292004-07-14 06:28:35 +0000526 // The forward declaration could have been inserted anywhere in the
527 // function: insert it into the correct place now.
528 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
529 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
Reid Spencer186a43f2007-03-19 18:39:36 +0000530
Reid Spencer2c9df212007-03-20 01:13:00 +0000531 // We're about to erase the entry, save the key so we can clean it up.
532 ValID Tmp = BBI->first;
533
Reid Spencer186a43f2007-03-19 18:39:36 +0000534 // Erase the forward ref from the map as its no longer "forward"
535 CurFun.BBForwardRefs.erase(ID);
536
Reid Spencer2c9df212007-03-20 01:13:00 +0000537 // The key has been removed from the map but so we don't want to leave
538 // strdup'd memory around so destroy it too.
539 Tmp.destroy();
540
Reid Spencer186a43f2007-03-19 18:39:36 +0000541 // If its a numbered definition, bump the number and set the BB value.
542 if (ID.Type == ValID::LocalID) {
543 assert(ID.Num == CurFun.NextValNum && "Invalid new block number");
544 InsertValue(BB);
545 }
546
547 ID.destroy();
548 return BB;
549 }
550
551 // We haven't seen this BB before and its first mention is a definition.
552 // Just create it and return it.
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000553 std::string Name (ID.Type == ValID::LocalName ? ID.getName() : "");
Reid Spencer186a43f2007-03-19 18:39:36 +0000554 BB = new BasicBlock(Name, CurFun.CurrentFunction);
555 if (ID.Type == ValID::LocalID) {
556 assert(ID.Num == CurFun.NextValNum && "Invalid new block number");
557 InsertValue(BB);
Chris Lattner2e2ed292004-07-14 06:28:35 +0000558 }
Reid Spencer186a43f2007-03-19 18:39:36 +0000559
560 ID.destroy(); // Free strdup'd memory
561 return BB;
562}
563
564/// getBBVal - get an existing BB value or create a forward reference for it.
565///
566static BasicBlock *getBBVal(const ValID &ID) {
567 assert(inFunctionScope() && "Can't get basic block at global scope!");
568
569 BasicBlock *BB = 0;
570
571 std::map<ValID, BasicBlock*>::iterator BBI = CurFun.BBForwardRefs.find(ID);
572 if (BBI != CurFun.BBForwardRefs.end()) {
573 BB = BBI->second;
574 } if (ID.Type == ValID::LocalName) {
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000575 std::string Name = ID.getName();
Reid Spencer186a43f2007-03-19 18:39:36 +0000576 Value *N = CurFun.CurrentFunction->getValueSymbolTable().lookup(Name);
577 if (N)
578 if (N->getType()->getTypeID() == Type::LabelTyID)
579 BB = cast<BasicBlock>(N);
580 else
581 GenerateError("Reference to label '" + Name + "' is actually of type '"+
582 N->getType()->getDescription() + "'");
583 } else if (ID.Type == ValID::LocalID) {
584 if (ID.Num < CurFun.NextValNum && ID.Num < CurFun.Values.size()) {
585 if (CurFun.Values[ID.Num]->getType()->getTypeID() == Type::LabelTyID)
586 BB = cast<BasicBlock>(CurFun.Values[ID.Num]);
587 else
588 GenerateError("Reference to label '%" + utostr(ID.Num) +
589 "' is actually of type '"+
590 CurFun.Values[ID.Num]->getType()->getDescription() + "'");
591 }
592 } else {
593 GenerateError("Illegal label reference " + ID.getName());
594 return 0;
595 }
596
597 // If its already been defined, return it now.
598 if (BB) {
599 ID.destroy(); // Free strdup'd memory.
600 return BB;
601 }
602
603 // Otherwise, this block has not been seen before, create it.
604 std::string Name;
605 if (ID.Type == ValID::LocalName)
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000606 Name = ID.getName();
Reid Spencer186a43f2007-03-19 18:39:36 +0000607 BB = new BasicBlock(Name, CurFun.CurrentFunction);
608
609 // Insert it in the forward refs map.
610 CurFun.BBForwardRefs[ID] = BB;
611
Chris Lattner64116f92004-07-14 01:33:11 +0000612 return BB;
613}
614
Chris Lattner00950542001-06-06 20:29:01 +0000615
616//===----------------------------------------------------------------------===//
617// Code to handle forward references in instructions
618//===----------------------------------------------------------------------===//
619//
620// This code handles the late binding needed with statements that reference
621// values not defined yet... for example, a forward branch, or the PHI node for
622// a loop body.
623//
Chris Lattner394f2eb2003-10-16 20:12:13 +0000624// This keeps a table (CurFun.LateResolveValues) of all such forward references
Chris Lattner00950542001-06-06 20:29:01 +0000625// and back patchs after we are done.
626//
627
Misha Brukman5a2a3822005-05-10 22:02:28 +0000628// ResolveDefinitions - If we could not resolve some defs at parsing
629// time (forward branches, phi functions for loops, etc...) resolve the
Chris Lattner00950542001-06-06 20:29:01 +0000630// defs now...
631//
Misha Brukman5a2a3822005-05-10 22:02:28 +0000632static void
Reid Spencer186a43f2007-03-19 18:39:36 +0000633ResolveDefinitions(ValueList &LateResolvers, ValueList *FutureLateResolvers) {
Chris Lattner00950542001-06-06 20:29:01 +0000634 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Reid Spencer186a43f2007-03-19 18:39:36 +0000635 while (!LateResolvers.empty()) {
636 Value *V = LateResolvers.back();
637 LateResolvers.pop_back();
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000638
Reid Spencer186a43f2007-03-19 18:39:36 +0000639 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
640 CurModule.PlaceHolderInfo.find(V);
641 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000642
Reid Spencer186a43f2007-03-19 18:39:36 +0000643 ValID &DID = PHI->second.first;
Chris Lattner00950542001-06-06 20:29:01 +0000644
Reid Spencer186a43f2007-03-19 18:39:36 +0000645 Value *TheRealValue = getExistingVal(V->getType(), DID);
646 if (TriggerError)
647 return;
648 if (TheRealValue) {
649 V->replaceAllUsesWith(TheRealValue);
650 delete V;
651 CurModule.PlaceHolderInfo.erase(PHI);
652 } else if (FutureLateResolvers) {
653 // Functions have their unresolved items forwarded to the module late
654 // resolver table
655 InsertValue(V, *FutureLateResolvers);
656 } else {
657 if (DID.Type == ValID::LocalName || DID.Type == ValID::GlobalName) {
658 GenerateError("Reference to an invalid definition: '" +DID.getName()+
659 "' of type '" + V->getType()->getDescription() + "'",
660 PHI->second.second);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000661 return;
Chris Lattner386a3b72001-10-16 19:54:17 +0000662 } else {
Reid Spencer186a43f2007-03-19 18:39:36 +0000663 GenerateError("Reference to an invalid definition: #" +
664 itostr(DID.Num) + " of type '" +
665 V->getType()->getDescription() + "'",
666 PHI->second.second);
667 return;
Chris Lattner30c89792001-09-07 16:35:17 +0000668 }
Chris Lattner00950542001-06-06 20:29:01 +0000669 }
670 }
Chris Lattner00950542001-06-06 20:29:01 +0000671 LateResolvers.clear();
672}
673
Chris Lattner4a42e902001-10-22 05:56:09 +0000674// ResolveTypeTo - A brand new type was just declared. This means that (if
675// name is not null) things referencing Name can be resolved. Otherwise, things
676// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000677//
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000678static void ResolveTypeTo(std::string *Name, const Type *ToTy) {
Chris Lattner355ad1f2005-03-21 06:27:42 +0000679 ValID D;
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000680 if (Name)
681 D = ValID::createLocalName(*Name);
682 else
683 D = ValID::createLocalID(CurModule.Types.size());
Chris Lattner00950542001-06-06 20:29:01 +0000684
Reid Spencerb78b9082006-11-28 07:28:14 +0000685 std::map<ValID, PATypeHolder>::iterator I =
Chris Lattner355ad1f2005-03-21 06:27:42 +0000686 CurModule.LateResolveTypes.find(D);
687 if (I != CurModule.LateResolveTypes.end()) {
Reid Spencerb78b9082006-11-28 07:28:14 +0000688 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattner355ad1f2005-03-21 06:27:42 +0000689 CurModule.LateResolveTypes.erase(I);
Chris Lattner30c89792001-09-07 16:35:17 +0000690 }
691}
692
Chris Lattner1781aca2001-09-18 04:00:54 +0000693// setValueName - Set the specified value to the name given. The name may be
694// null potentially, in which case this is a noop. The string passed in is
Chris Lattner8ab406d2004-07-13 07:59:27 +0000695// assumed to be a malloc'd string buffer, and is free'd by this function.
696//
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000697static void setValueName(Value *V, std::string *NameStr) {
Reid Spencerb2d17862007-01-26 08:04:51 +0000698 if (!NameStr) return;
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000699 std::string Name(*NameStr); // Copy string
700 delete NameStr; // Free old string
Chris Lattnerc9aea522004-07-13 08:12:39 +0000701
Reid Spencerb2d17862007-01-26 08:04:51 +0000702 if (V->getType() == Type::VoidTy) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000703 GenerateError("Can't assign name '" + Name+"' to value with void type");
Reid Spencerb2d17862007-01-26 08:04:51 +0000704 return;
Chris Lattner8ab406d2004-07-13 07:59:27 +0000705 }
Reid Spencerb2d17862007-01-26 08:04:51 +0000706
Reid Spencer1755a9a2007-02-05 17:01:20 +0000707 assert(inFunctionScope() && "Must be in function scope!");
Reid Spenceref9b9a72007-02-05 20:47:22 +0000708 ValueSymbolTable &ST = CurFun.CurrentFunction->getValueSymbolTable();
709 if (ST.lookup(Name)) {
Reid Spencerb2d17862007-01-26 08:04:51 +0000710 GenerateError("Redefinition of value '" + Name + "' of type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +0000711 V->getType()->getDescription() + "'");
Reid Spencerb2d17862007-01-26 08:04:51 +0000712 return;
713 }
714
715 // Set the name.
716 V->setName(Name);
Chris Lattner8ab406d2004-07-13 07:59:27 +0000717}
718
Chris Lattnerd88998d2004-07-14 08:23:52 +0000719/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
720/// this is a declaration, otherwise it is a definition.
Chris Lattnerb2b96672005-11-12 18:21:21 +0000721static GlobalVariable *
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000722ParseGlobalVariable(std::string *NameStr,
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000723 GlobalValue::LinkageTypes Linkage,
724 GlobalValue::VisibilityTypes Visibility,
Chris Lattnerb2b96672005-11-12 18:21:21 +0000725 bool isConstantGlobal, const Type *Ty,
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +0000726 Constant *Initializer, bool IsThreadLocal) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000727 if (isa<FunctionType>(Ty)) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000728 GenerateError("Cannot declare global vars of function type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000729 return 0;
730 }
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000731
Misha Brukman5a2a3822005-05-10 22:02:28 +0000732 const PointerType *PTy = PointerType::get(Ty);
Chris Lattnera09000d2004-07-14 23:03:46 +0000733
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000734 std::string Name;
735 if (NameStr) {
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000736 Name = *NameStr; // Copy string
737 delete NameStr; // Free old string
Chris Lattnerd88998d2004-07-14 08:23:52 +0000738 }
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000739
Chris Lattner8a327842004-07-14 21:44:00 +0000740 // See if this global value was forward referenced. If so, recycle the
741 // object.
Misha Brukman5a2a3822005-05-10 22:02:28 +0000742 ValID ID;
Chris Lattner8a327842004-07-14 21:44:00 +0000743 if (!Name.empty()) {
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000744 ID = ValID::createGlobalName(Name);
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000745 } else {
Reid Spencer186a43f2007-03-19 18:39:36 +0000746 ID = ValID::createGlobalID(CurModule.Values.size());
Chris Lattner8a327842004-07-14 21:44:00 +0000747 }
748
Reid Spencer863dd882007-04-28 16:06:50 +0000749 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000750 // Move the global to the end of the list, from whereever it was
Chris Lattner8a327842004-07-14 21:44:00 +0000751 // previously inserted.
752 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
753 CurModule.CurrentModule->getGlobalList().remove(GV);
754 CurModule.CurrentModule->getGlobalList().push_back(GV);
755 GV->setInitializer(Initializer);
756 GV->setLinkage(Linkage);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000757 GV->setVisibility(Visibility);
Chris Lattner8a327842004-07-14 21:44:00 +0000758 GV->setConstant(isConstantGlobal);
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +0000759 GV->setThreadLocal(IsThreadLocal);
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000760 InsertValue(GV, CurModule.Values);
Chris Lattnerb2b96672005-11-12 18:21:21 +0000761 return GV;
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000762 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000763
Reid Spenceref9b9a72007-02-05 20:47:22 +0000764 // If this global has a name
Chris Lattnera09000d2004-07-14 23:03:46 +0000765 if (!Name.empty()) {
Reid Spenceref9b9a72007-02-05 20:47:22 +0000766 // if the global we're parsing has an initializer (is a definition) and
767 // has external linkage.
768 if (Initializer && Linkage != GlobalValue::InternalLinkage)
769 // If there is already a global with external linkage with this name
770 if (CurModule.CurrentModule->getGlobalVariable(Name, false)) {
771 // If we allow this GVar to get created, it will be renamed in the
772 // symbol table because it conflicts with an existing GVar. We can't
773 // allow redefinition of GVars whose linking indicates that their name
774 // must stay the same. Issue the error.
775 GenerateError("Redefinition of global variable named '" + Name +
776 "' of type '" + Ty->getDescription() + "'");
777 return 0;
778 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000779 }
780
781 // Otherwise there is no existing GV to use, create one now.
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000782 GlobalVariable *GV =
Misha Brukman5a2a3822005-05-10 22:02:28 +0000783 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +0000784 CurModule.CurrentModule, IsThreadLocal);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000785 GV->setVisibility(Visibility);
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000786 InsertValue(GV, CurModule.Values);
Chris Lattnerb2b96672005-11-12 18:21:21 +0000787 return GV;
Chris Lattnerd88998d2004-07-14 08:23:52 +0000788}
Chris Lattner8ab406d2004-07-13 07:59:27 +0000789
Reid Spencer75719bf2004-05-26 21:48:31 +0000790// setTypeName - Set the specified type to the name given. The name may be
791// null potentially, in which case this is a noop. The string passed in is
792// assumed to be a malloc'd string buffer, and is freed by this function.
793//
794// This function returns true if the type has already been defined, but is
795// allowed to be redefined in the specified context. If the name is a new name
796// for the type plane, it is inserted and false is returned.
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000797static bool setTypeName(const Type *T, std::string *NameStr) {
Reid Spencer1755a9a2007-02-05 17:01:20 +0000798 assert(!inFunctionScope() && "Can't give types function-local names!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000799 if (NameStr == 0) return false;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000800
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000801 std::string Name(*NameStr); // Copy string
802 delete NameStr; // Free old string
Reid Spencer75719bf2004-05-26 21:48:31 +0000803
804 // We don't allow assigning names to void type
Reid Spencer5b7e7532006-09-28 19:28:24 +0000805 if (T == Type::VoidTy) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000806 GenerateError("Can't assign name '" + Name + "' to the void type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000807 return false;
808 }
Reid Spencer75719bf2004-05-26 21:48:31 +0000809
Chris Lattnera09000d2004-07-14 23:03:46 +0000810 // Set the type name, checking for conflicts as we do so.
811 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000812
Chris Lattnera09000d2004-07-14 23:03:46 +0000813 if (AlreadyExists) { // Inserting a name that is already defined???
814 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
Reid Spencer1755a9a2007-02-05 17:01:20 +0000815 assert(Existing && "Conflict but no matching type?!");
Chris Lattnera09000d2004-07-14 23:03:46 +0000816
Reid Spencer75719bf2004-05-26 21:48:31 +0000817 // There is only one case where this is allowed: when we are refining an
818 // opaque type. In this case, Existing will be an opaque type.
819 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
820 // We ARE replacing an opaque type!
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000821 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000822 return true;
823 }
824
825 // Otherwise, this is an attempt to redefine a type. That's okay if
826 // the redefinition is identical to the original. This will be so if
827 // Existing and T point to the same Type object. In this one case we
828 // allow the equivalent redefinition.
829 if (Existing == T) return true; // Yes, it's equal.
830
831 // Any other kind of (non-equivalent) redefinition is an error.
Reid Spencer90e2f632007-01-05 21:50:38 +0000832 GenerateError("Redefinition of type named '" + Name + "' of type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +0000833 T->getDescription() + "'");
Reid Spencer75719bf2004-05-26 21:48:31 +0000834 }
835
Reid Spencer75719bf2004-05-26 21:48:31 +0000836 return false;
837}
Chris Lattner8896eda2001-07-09 19:38:36 +0000838
Chris Lattner30c89792001-09-07 16:35:17 +0000839//===----------------------------------------------------------------------===//
840// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000841//
Chris Lattner8896eda2001-07-09 19:38:36 +0000842
Chris Lattner3b073862004-02-09 03:19:29 +0000843// TypeContains - Returns true if Ty directly contains E in it.
Chris Lattner30c89792001-09-07 16:35:17 +0000844//
845static bool TypeContains(const Type *Ty, const Type *E) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000846 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
Chris Lattner6d8dbec2004-12-08 16:13:53 +0000847 E) != Ty->subtype_end();
Chris Lattner3b073862004-02-09 03:19:29 +0000848}
849
850namespace {
851 struct UpRefRecord {
852 // NestingLevel - The number of nesting levels that need to be popped before
853 // this type is resolved.
854 unsigned NestingLevel;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000855
Chris Lattner3b073862004-02-09 03:19:29 +0000856 // LastContainedTy - This is the type at the current binding level for the
857 // type. Every time we reduce the nesting level, this gets updated.
858 const Type *LastContainedTy;
859
860 // UpRefTy - This is the actual opaque type that the upreference is
861 // represented with.
862 OpaqueType *UpRefTy;
863
864 UpRefRecord(unsigned NL, OpaqueType *URTy)
865 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
866 };
Chris Lattner30c89792001-09-07 16:35:17 +0000867}
Chris Lattner698b56e2001-07-20 19:15:08 +0000868
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000869// UpRefs - A list of the outstanding upreferences that need to be resolved.
Chris Lattner3b073862004-02-09 03:19:29 +0000870static std::vector<UpRefRecord> UpRefs;
Chris Lattner30c89792001-09-07 16:35:17 +0000871
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000872/// HandleUpRefs - Every time we finish a new layer of types, this function is
873/// called. It loops through the UpRefs vector, which is a list of the
874/// currently active types. For each type, if the up reference is contained in
875/// the newly completed type, we decrement the level count. When the level
876/// count reaches zero, the upreferenced type is the type that is passed in:
877/// thus we can complete the cycle.
878///
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000879static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner703e92f2006-08-18 17:34:24 +0000880 // If Ty isn't abstract, or if there are no up-references in it, then there is
881 // nothing to resolve here.
882 if (!ty->isAbstract() || UpRefs.empty()) return ty;
883
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000884 PATypeHolder Ty(ty);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000885 UR_OUT("Type '" << Ty->getDescription() <<
Chris Lattner5084d032001-11-02 07:46:26 +0000886 "' newly formed. Resolving upreferences.\n" <<
887 UpRefs.size() << " upreferences active!\n");
Chris Lattner026a8ce2004-02-09 18:53:54 +0000888
889 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
890 // to zero), we resolve them all together before we resolve them to Ty. At
891 // the end of the loop, if there is anything to resolve to Ty, it will be in
892 // this variable.
893 OpaqueType *TypeToResolve = 0;
894
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000895 for (unsigned i = 0; i != UpRefs.size(); ++i) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000896 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
897 << UpRefs[i].second->getDescription() << ") = "
Reid Spencer3271ed52004-07-18 00:08:11 +0000898 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
Chris Lattner3b073862004-02-09 03:19:29 +0000899 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
900 // Decrement level of upreference
901 unsigned Level = --UpRefs[i].NestingLevel;
902 UpRefs[i].LastContainedTy = Ty;
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000903 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
Misha Brukman5a2a3822005-05-10 22:02:28 +0000904 if (Level == 0) { // Upreference should be resolved!
Chris Lattner026a8ce2004-02-09 18:53:54 +0000905 if (!TypeToResolve) {
906 TypeToResolve = UpRefs[i].UpRefTy;
907 } else {
908 UR_OUT(" * Resolving upreference for "
909 << UpRefs[i].second->getDescription() << "\n";
910 std::string OldName = UpRefs[i].UpRefTy->getDescription());
911 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
912 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
913 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
914 }
Reid Spencer3271ed52004-07-18 00:08:11 +0000915 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000916 --i; // Do not skip the next element...
Chris Lattner30c89792001-09-07 16:35:17 +0000917 }
918 }
Chris Lattner8896eda2001-07-09 19:38:36 +0000919 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000920
Chris Lattner026a8ce2004-02-09 18:53:54 +0000921 if (TypeToResolve) {
922 UR_OUT(" * Resolving upreference for "
923 << UpRefs[i].second->getDescription() << "\n";
Chris Lattner16af11d2004-02-09 21:03:38 +0000924 std::string OldName = TypeToResolve->getDescription());
Chris Lattner026a8ce2004-02-09 18:53:54 +0000925 TypeToResolve->refineAbstractTypeTo(Ty);
926 }
927
Chris Lattner8896eda2001-07-09 19:38:36 +0000928 return Ty;
929}
930
Chris Lattner00950542001-06-06 20:29:01 +0000931//===----------------------------------------------------------------------===//
932// RunVMAsmParser - Define an interface to this parser
933//===----------------------------------------------------------------------===//
934//
Reid Spencere1553cc2006-12-31 05:40:12 +0000935static Module* RunParser(Module * M);
936
Chris Lattner86427632004-07-14 06:39:48 +0000937Module *llvm::RunVMAsmParser(const std::string &Filename, FILE *F) {
Chris Lattner6184feb2005-05-20 03:25:47 +0000938 set_scan_file(F);
939
Chris Lattnera2850432001-07-22 18:36:00 +0000940 CurFilename = Filename;
Chris Lattner6184feb2005-05-20 03:25:47 +0000941 return RunParser(new Module(CurFilename));
942}
Chris Lattner00950542001-06-06 20:29:01 +0000943
Chris Lattner6184feb2005-05-20 03:25:47 +0000944Module *llvm::RunVMAsmParser(const char * AsmString, Module * M) {
945 set_scan_string(AsmString);
Chris Lattner42570982003-11-26 07:24:58 +0000946
Chris Lattner6184feb2005-05-20 03:25:47 +0000947 CurFilename = "from_memory";
948 if (M == NULL) {
949 return RunParser(new Module (CurFilename));
950 } else {
951 return RunParser(M);
952 }
Chris Lattner00950542001-06-06 20:29:01 +0000953}
954
955%}
956
957%union {
Brian Gaeked0fde302003-11-11 22:41:34 +0000958 llvm::Module *ModuleVal;
959 llvm::Function *FunctionVal;
Brian Gaeked0fde302003-11-11 22:41:34 +0000960 llvm::BasicBlock *BasicBlockVal;
961 llvm::TerminatorInst *TermInstVal;
962 llvm::Instruction *InstVal;
Reid Spencer08de34b2006-12-03 05:45:44 +0000963 llvm::Constant *ConstVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000964
Reid Spencer08de34b2006-12-03 05:45:44 +0000965 const llvm::Type *PrimType;
Reid Spencere1553cc2006-12-31 05:40:12 +0000966 std::list<llvm::PATypeHolder> *TypeList;
Reid Spencer08de34b2006-12-03 05:45:44 +0000967 llvm::PATypeHolder *TypeVal;
968 llvm::Value *ValueVal;
Reid Spencer08de34b2006-12-03 05:45:44 +0000969 std::vector<llvm::Value*> *ValueList;
Reid Spencere1553cc2006-12-31 05:40:12 +0000970 llvm::ArgListType *ArgList;
971 llvm::TypeWithAttrs TypeWithAttrs;
972 llvm::TypeWithAttrsList *TypeWithAttrsList;
973 llvm::ValueRefList *ValueRefList;
974
Misha Brukman5a2a3822005-05-10 22:02:28 +0000975 // Represent the RHS of PHI node
Reid Spencer08de34b2006-12-03 05:45:44 +0000976 std::list<std::pair<llvm::Value*,
977 llvm::BasicBlock*> > *PHIList;
Brian Gaeked0fde302003-11-11 22:41:34 +0000978 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
Reid Spencer08de34b2006-12-03 05:45:44 +0000979 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000980
Brian Gaeked0fde302003-11-11 22:41:34 +0000981 llvm::GlobalValue::LinkageTypes Linkage;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000982 llvm::GlobalValue::VisibilityTypes Visibility;
Reid Spencer5694b6e2007-04-09 06:17:21 +0000983 uint16_t ParamAttrs;
Reid Spencerf2310042007-02-28 02:23:44 +0000984 llvm::APInt *APIntVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000985 int64_t SInt64Val;
986 uint64_t UInt64Val;
987 int SIntVal;
988 unsigned UIntVal;
989 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000990 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000991
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000992 std::string *StrVal; // This memory must be deleted
993 llvm::ValID ValIDVal;
Chris Lattner00950542001-06-06 20:29:01 +0000994
Reid Spencer08de34b2006-12-03 05:45:44 +0000995 llvm::Instruction::BinaryOps BinaryOpVal;
996 llvm::Instruction::TermOps TermOpVal;
997 llvm::Instruction::MemoryOps MemOpVal;
998 llvm::Instruction::CastOps CastOpVal;
999 llvm::Instruction::OtherOps OtherOpVal;
Reid Spencer08de34b2006-12-03 05:45:44 +00001000 llvm::ICmpInst::Predicate IPredicate;
1001 llvm::FCmpInst::Predicate FPredicate;
Chris Lattner00950542001-06-06 20:29:01 +00001002}
1003
Reid Spencere1553cc2006-12-31 05:40:12 +00001004%type <ModuleVal> Module
Chris Lattner79df7c02002-03-26 18:01:55 +00001005%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +00001006%type <BasicBlockVal> BasicBlock InstructionList
1007%type <TermInstVal> BBTerminatorInst
1008%type <InstVal> Inst InstVal MemoryInst
Anton Korobeynikova80e1182007-04-28 13:45:00 +00001009%type <ConstVal> ConstVal ConstExpr AliaseeRef
Chris Lattner6cdb0112001-11-26 16:54:11 +00001010%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +00001011%type <ArgList> ArgList ArgListH
Chris Lattnerc24d2082001-06-11 15:04:20 +00001012%type <PHIList> PHIList
Reid Spencere1553cc2006-12-31 05:40:12 +00001013%type <ValueRefList> ValueRefList // For call param lists & GEP indices
1014%type <ValueList> IndexList // For GEP indices
1015%type <TypeList> TypeListI
1016%type <TypeWithAttrsList> ArgTypeList ArgTypeListI
Reid Spencer2c261782007-01-05 17:06:19 +00001017%type <TypeWithAttrs> ArgType
Chris Lattner00950542001-06-06 20:29:01 +00001018%type <JumpTable> JumpTable
Chris Lattner4ad02e72003-04-16 20:28:45 +00001019%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00001020%type <BoolVal> ThreadLocal // 'thread_local' or not
Chris Lattner15c9c032003-09-08 18:20:29 +00001021%type <BoolVal> OptVolatile // 'volatile' or not
Chris Lattnerddb6db42005-05-06 05:51:46 +00001022%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
Chris Lattneraa2c8532006-01-25 22:26:43 +00001023%type <BoolVal> OptSideEffect // 'sideeffect' or not.
Reid Spencere1553cc2006-12-31 05:40:12 +00001024%type <Linkage> GVInternalLinkage GVExternalLinkage
1025%type <Linkage> FunctionDefineLinkage FunctionDeclareLinkage
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001026%type <Linkage> AliasLinkage
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001027%type <Visibility> GVVisibilityStyle
Chris Lattner00950542001-06-06 20:29:01 +00001028
Chris Lattner2079fde2001-10-13 06:41:08 +00001029// ValueRef - Unresolved reference to a definition or BB
1030%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001031%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +00001032// Tokens and types for handling constant integer values
1033//
1034// ESINT64VAL - A negative number within long long range
1035%token <SInt64Val> ESINT64VAL
1036
1037// EUINT64VAL - A positive number within uns. long long range
1038%token <UInt64Val> EUINT64VAL
Chris Lattner00950542001-06-06 20:29:01 +00001039
Reid Spencerf2310042007-02-28 02:23:44 +00001040// ESAPINTVAL - A negative number with arbitrary precision
1041%token <APIntVal> ESAPINTVAL
1042
1043// EUAPINTVAL - A positive number with arbitrary precision
1044%token <APIntVal> EUAPINTVAL
1045
Reid Spencerb2d17862007-01-26 08:04:51 +00001046%token <UIntVal> LOCALVAL_ID GLOBALVAL_ID // %123 @123
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001047%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +00001048
1049// Built in types...
Reid Spencer2c261782007-01-05 17:06:19 +00001050%type <TypeVal> Types ResultTypes
Reid Spencere1553cc2006-12-31 05:40:12 +00001051%type <PrimType> IntType FPType PrimType // Classifications
Reid Spencer8088e9d2007-01-13 05:00:20 +00001052%token <PrimType> VOID INTTYPE
Reid Spencerb951bc02006-12-29 20:29:48 +00001053%token <PrimType> FLOAT DOUBLE LABEL
1054%token TYPE
Chris Lattner00950542001-06-06 20:29:01 +00001055
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001056
Reid Spenceraa7c2f82007-05-19 07:21:26 +00001057%token<StrVal> LOCALVAR GLOBALVAR LABELSTR
1058%token<StrVal> STRINGCONSTANT ATSTRINGCONSTANT PCTSTRINGCONSTANT
Reid Spencerb2d17862007-01-26 08:04:51 +00001059%type <StrVal> LocalName OptLocalName OptLocalAssign
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001060%type <StrVal> GlobalName OptGlobalAssign GlobalAssign
Chris Lattnerb2b96672005-11-12 18:21:21 +00001061%type <StrVal> OptSection SectionString
Chris Lattner00950542001-06-06 20:29:01 +00001062
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001063%type <UIntVal> OptAlign OptCAlign
1064
Reid Spencer744d0362007-04-09 01:55:42 +00001065%token ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001066%token DECLARE DEFINE GLOBAL CONSTANT SECTION ALIAS VOLATILE THREAD_LOCAL
Reid Spencere1553cc2006-12-31 05:40:12 +00001067%token TO DOTDOTDOT NULL_TOK UNDEF INTERNAL LINKONCE WEAK APPENDING
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00001068%token DLLIMPORT DLLEXPORT EXTERN_WEAK
Reid Spencerb2d17862007-01-26 08:04:51 +00001069%token OPAQUE EXTERNAL TARGET TRIPLE ALIGN
Chris Lattneraa2c8532006-01-25 22:26:43 +00001070%token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001071%token CC_TOK CCC_TOK FASTCC_TOK COLDCC_TOK X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
Chris Lattner10b27112006-10-22 06:07:41 +00001072%token DATALAYOUT
Chris Lattnera8e8f162005-05-06 20:27:19 +00001073%type <UIntVal> OptCallingConv
Reid Spencer2c261782007-01-05 17:06:19 +00001074%type <ParamAttrs> OptParamAttrs ParamAttr
1075%type <ParamAttrs> OptFuncAttrs FuncAttr
Chris Lattner00950542001-06-06 20:29:01 +00001076
Misha Brukman5a2a3822005-05-10 22:02:28 +00001077// Basic Block Terminating Operators
Chris Lattner16710e92004-10-16 18:17:13 +00001078%token <TermOpVal> RET BR SWITCH INVOKE UNWIND UNREACHABLE
Chris Lattner00950542001-06-06 20:29:01 +00001079
Misha Brukman5a2a3822005-05-10 22:02:28 +00001080// Binary Operators
Reid Spencere4d87aa2006-12-23 06:05:41 +00001081%type <BinaryOpVal> ArithmeticOps LogicalOps // Binops Subcatagories
Reid Spencer0a783f72006-11-02 01:53:59 +00001082%token <BinaryOpVal> ADD SUB MUL UDIV SDIV FDIV UREM SREM FREM AND OR XOR
Reid Spencer832254e2007-02-02 02:16:23 +00001083%token <BinaryOpVal> SHL LSHR ASHR
1084
Reid Spencer08de34b2006-12-03 05:45:44 +00001085%token <OtherOpVal> ICMP FCMP
Reid Spencer08de34b2006-12-03 05:45:44 +00001086%type <IPredicate> IPredicates
Reid Spencer08de34b2006-12-03 05:45:44 +00001087%type <FPredicate> FPredicates
Reid Spencer9f746f42006-12-03 06:58:07 +00001088%token EQ NE SLT SGT SLE SGE ULT UGT ULE UGE
1089%token OEQ ONE OLT OGT OLE OGE ORD UNO UEQ UNE
Chris Lattner00950542001-06-06 20:29:01 +00001090
1091// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001092%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +00001093
Reid Spencer3da59db2006-11-27 01:05:10 +00001094// Cast Operators
1095%type <CastOpVal> CastOps
1096%token <CastOpVal> TRUNC ZEXT SEXT FPTRUNC FPEXT BITCAST
1097%token <CastOpVal> UITOFP SITOFP FPTOUI FPTOSI INTTOPTR PTRTOINT
1098
Chris Lattner027dcc52001-07-08 21:10:27 +00001099// Other Operators
Reid Spencer832254e2007-02-02 02:16:23 +00001100%token <OtherOpVal> PHI_TOK SELECT VAARG
Chris Lattner4c949082006-04-08 01:18:35 +00001101%token <OtherOpVal> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
Chris Lattnerddb6db42005-05-06 05:51:46 +00001102
Reid Spencer2c261782007-01-05 17:06:19 +00001103// Function Attributes
Duncan Sands36397f52007-07-27 12:58:54 +00001104%token SIGNEXT ZEROEXT NORETURN INREG SRET NOUNWIND NOALIAS BYVAL NEST
Chris Lattner027dcc52001-07-08 21:10:27 +00001105
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001106// Visibility Styles
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +00001107%token DEFAULT HIDDEN PROTECTED
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001108
Chris Lattner00950542001-06-06 20:29:01 +00001109%start Module
1110%%
1111
Chris Lattner00950542001-06-06 20:29:01 +00001112
Misha Brukman5a2a3822005-05-10 22:02:28 +00001113// Operations that are notably excluded from this list include:
Chris Lattner00950542001-06-06 20:29:01 +00001114// RET, BR, & SWITCH because they end basic blocks and are treated specially.
1115//
Reid Spencer0a783f72006-11-02 01:53:59 +00001116ArithmeticOps: ADD | SUB | MUL | UDIV | SDIV | FDIV | UREM | SREM | FREM;
Reid Spencer832254e2007-02-02 02:16:23 +00001117LogicalOps : SHL | LSHR | ASHR | AND | OR | XOR;
Reid Spencer3da59db2006-11-27 01:05:10 +00001118CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | BITCAST |
1119 UITOFP | SITOFP | FPTOUI | FPTOSI | INTTOPTR | PTRTOINT;
Reid Spencer832254e2007-02-02 02:16:23 +00001120
Reid Spencer9f746f42006-12-03 06:58:07 +00001121IPredicates
Reid Spencer763ed5e2006-12-04 05:20:06 +00001122 : EQ { $$ = ICmpInst::ICMP_EQ; } | NE { $$ = ICmpInst::ICMP_NE; }
Reid Spencer9f746f42006-12-03 06:58:07 +00001123 | SLT { $$ = ICmpInst::ICMP_SLT; } | SGT { $$ = ICmpInst::ICMP_SGT; }
1124 | SLE { $$ = ICmpInst::ICMP_SLE; } | SGE { $$ = ICmpInst::ICMP_SGE; }
1125 | ULT { $$ = ICmpInst::ICMP_ULT; } | UGT { $$ = ICmpInst::ICMP_UGT; }
1126 | ULE { $$ = ICmpInst::ICMP_ULE; } | UGE { $$ = ICmpInst::ICMP_UGE; }
1127 ;
1128
1129FPredicates
1130 : OEQ { $$ = FCmpInst::FCMP_OEQ; } | ONE { $$ = FCmpInst::FCMP_ONE; }
1131 | OLT { $$ = FCmpInst::FCMP_OLT; } | OGT { $$ = FCmpInst::FCMP_OGT; }
1132 | OLE { $$ = FCmpInst::FCMP_OLE; } | OGE { $$ = FCmpInst::FCMP_OGE; }
1133 | ORD { $$ = FCmpInst::FCMP_ORD; } | UNO { $$ = FCmpInst::FCMP_UNO; }
1134 | UEQ { $$ = FCmpInst::FCMP_UEQ; } | UNE { $$ = FCmpInst::FCMP_UNE; }
1135 | ULT { $$ = FCmpInst::FCMP_ULT; } | UGT { $$ = FCmpInst::FCMP_UGT; }
1136 | ULE { $$ = FCmpInst::FCMP_ULE; } | UGE { $$ = FCmpInst::FCMP_UGE; }
1137 | TRUETOK { $$ = FCmpInst::FCMP_TRUE; }
1138 | FALSETOK { $$ = FCmpInst::FCMP_FALSE; }
1139 ;
Chris Lattner00950542001-06-06 20:29:01 +00001140
Chris Lattnere98dda62001-07-14 06:10:16 +00001141// These are some types that allow classification if we only want a particular
1142// thing... for example, only a signed, unsigned, or integral type.
Reid Spencera54b7cb2007-01-12 07:05:14 +00001143IntType : INTTYPE;
Chris Lattner51727be2002-06-04 21:58:56 +00001144FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +00001145
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001146LocalName : LOCALVAR | STRINGCONSTANT | PCTSTRINGCONSTANT ;
Reid Spencerb2d17862007-01-26 08:04:51 +00001147OptLocalName : LocalName | /*empty*/ { $$ = 0; };
1148
1149/// OptLocalAssign - Value producing statements have an optional assignment
1150/// component.
1151OptLocalAssign : LocalName '=' {
1152 $$ = $1;
1153 CHECK_FOR_ERROR
1154 }
1155 | /*empty*/ {
1156 $$ = 0;
1157 CHECK_FOR_ERROR
1158 };
1159
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001160GlobalName : GLOBALVAR | ATSTRINGCONSTANT ;
Reid Spencerb2d17862007-01-26 08:04:51 +00001161
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001162OptGlobalAssign : GlobalAssign
Misha Brukman5a2a3822005-05-10 22:02:28 +00001163 | /*empty*/ {
1164 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00001165 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001166 };
Chris Lattner00950542001-06-06 20:29:01 +00001167
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001168GlobalAssign : GlobalName '=' {
1169 $$ = $1;
1170 CHECK_FOR_ERROR
Anton Korobeynikovc0fabcb2007-04-25 18:07:40 +00001171 };
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001172
Reid Spencerb951bc02006-12-29 20:29:48 +00001173GVInternalLinkage
1174 : INTERNAL { $$ = GlobalValue::InternalLinkage; }
1175 | WEAK { $$ = GlobalValue::WeakLinkage; }
1176 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1177 | APPENDING { $$ = GlobalValue::AppendingLinkage; }
1178 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
1179 ;
1180
1181GVExternalLinkage
1182 : DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1183 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
1184 | EXTERNAL { $$ = GlobalValue::ExternalLinkage; }
1185 ;
1186
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001187GVVisibilityStyle
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +00001188 : /*empty*/ { $$ = GlobalValue::DefaultVisibility; }
1189 | DEFAULT { $$ = GlobalValue::DefaultVisibility; }
1190 | HIDDEN { $$ = GlobalValue::HiddenVisibility; }
1191 | PROTECTED { $$ = GlobalValue::ProtectedVisibility; }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001192 ;
1193
Reid Spencere1553cc2006-12-31 05:40:12 +00001194FunctionDeclareLinkage
1195 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1196 | DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1197 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
Reid Spencerb951bc02006-12-29 20:29:48 +00001198 ;
1199
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001200FunctionDefineLinkage
Reid Spencere1553cc2006-12-31 05:40:12 +00001201 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1202 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
Reid Spencerb951bc02006-12-29 20:29:48 +00001203 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1204 | WEAK { $$ = GlobalValue::WeakLinkage; }
1205 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
Reid Spencerb951bc02006-12-29 20:29:48 +00001206 ;
Chris Lattner30c89792001-09-07 16:35:17 +00001207
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001208AliasLinkage
1209 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1210 | WEAK { $$ = GlobalValue::WeakLinkage; }
1211 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
1212 ;
1213
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00001214OptCallingConv : /*empty*/ { $$ = CallingConv::C; } |
1215 CCC_TOK { $$ = CallingConv::C; } |
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00001216 FASTCC_TOK { $$ = CallingConv::Fast; } |
1217 COLDCC_TOK { $$ = CallingConv::Cold; } |
1218 X86_STDCALLCC_TOK { $$ = CallingConv::X86_StdCall; } |
1219 X86_FASTCALLCC_TOK { $$ = CallingConv::X86_FastCall; } |
1220 CC_TOK EUINT64VAL {
Chris Lattnera8e8f162005-05-06 20:27:19 +00001221 if ((unsigned)$2 != $2)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001222 GEN_ERROR("Calling conv too large");
Chris Lattnera8e8f162005-05-06 20:27:19 +00001223 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001224 CHECK_FOR_ERROR
Chris Lattnera8e8f162005-05-06 20:27:19 +00001225 };
1226
Reid Spencer9445e9a2007-07-19 23:13:04 +00001227ParamAttr : ZEROEXT { $$ = ParamAttr::ZExt; }
Reid Spencer1d3d2302007-07-31 02:57:37 +00001228 | ZEXT { $$ = ParamAttr::ZExt; }
Reid Spencer9445e9a2007-07-19 23:13:04 +00001229 | SIGNEXT { $$ = ParamAttr::SExt; }
Reid Spencer1d3d2302007-07-31 02:57:37 +00001230 | SEXT { $$ = ParamAttr::SExt; }
Zhou Shengfebca342007-06-05 05:28:26 +00001231 | INREG { $$ = ParamAttr::InReg; }
1232 | SRET { $$ = ParamAttr::StructRet; }
1233 | NOALIAS { $$ = ParamAttr::NoAlias; }
Duncan Sands36397f52007-07-27 12:58:54 +00001234 | BYVAL { $$ = ParamAttr::ByVal; }
1235 | NEST { $$ = ParamAttr::Nest; }
Reid Spencere1553cc2006-12-31 05:40:12 +00001236 ;
1237
Reid Spencer18da0722007-04-11 02:44:20 +00001238OptParamAttrs : /* empty */ { $$ = ParamAttr::None; }
Reid Spencer2c261782007-01-05 17:06:19 +00001239 | OptParamAttrs ParamAttr {
Reid Spencer5694b6e2007-04-09 06:17:21 +00001240 $$ = $1 | $2;
Reid Spencere1553cc2006-12-31 05:40:12 +00001241 }
1242 ;
1243
Reid Spencer18da0722007-04-11 02:44:20 +00001244FuncAttr : NORETURN { $$ = ParamAttr::NoReturn; }
1245 | NOUNWIND { $$ = ParamAttr::NoUnwind; }
Reid Spencer9445e9a2007-07-19 23:13:04 +00001246 | ZEROEXT { $$ = ParamAttr::ZExt; }
1247 | SIGNEXT { $$ = ParamAttr::SExt; }
Reid Spencer2c261782007-01-05 17:06:19 +00001248 ;
1249
Reid Spencer18da0722007-04-11 02:44:20 +00001250OptFuncAttrs : /* empty */ { $$ = ParamAttr::None; }
Reid Spencer2c261782007-01-05 17:06:19 +00001251 | OptFuncAttrs FuncAttr {
Reid Spencer5694b6e2007-04-09 06:17:21 +00001252 $$ = $1 | $2;
Reid Spencer2c261782007-01-05 17:06:19 +00001253 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001254 ;
1255
Chris Lattner66db8e42005-11-06 06:34:12 +00001256// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1257// a comma before it.
Chris Lattner87ac9722005-11-06 06:46:28 +00001258OptAlign : /*empty*/ { $$ = 0; } |
Chris Lattnerb2b96672005-11-12 18:21:21 +00001259 ALIGN EUINT64VAL {
1260 $$ = $2;
1261 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001262 GEN_ERROR("Alignment must be a power of two");
Reid Spencer61c83e02006-08-18 08:43:06 +00001263 CHECK_FOR_ERROR
Chris Lattnerb2b96672005-11-12 18:21:21 +00001264};
Chris Lattner66db8e42005-11-06 06:34:12 +00001265OptCAlign : /*empty*/ { $$ = 0; } |
Chris Lattnerb2b96672005-11-12 18:21:21 +00001266 ',' ALIGN EUINT64VAL {
1267 $$ = $3;
1268 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001269 GEN_ERROR("Alignment must be a power of two");
Reid Spencer61c83e02006-08-18 08:43:06 +00001270 CHECK_FOR_ERROR
Chris Lattnerb2b96672005-11-12 18:21:21 +00001271};
1272
Chris Lattner66db8e42005-11-06 06:34:12 +00001273
Chris Lattner164c3782005-11-12 00:11:10 +00001274SectionString : SECTION STRINGCONSTANT {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001275 for (unsigned i = 0, e = $2->length(); i != e; ++i)
1276 if ((*$2)[i] == '"' || (*$2)[i] == '\\')
Reid Spencerf4fa5902007-02-05 10:16:10 +00001277 GEN_ERROR("Invalid character in section name");
Chris Lattner164c3782005-11-12 00:11:10 +00001278 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001279 CHECK_FOR_ERROR
Chris Lattner164c3782005-11-12 00:11:10 +00001280};
1281
1282OptSection : /*empty*/ { $$ = 0; } |
1283 SectionString { $$ = $1; };
Chris Lattner164c3782005-11-12 00:11:10 +00001284
Chris Lattnerb2b96672005-11-12 18:21:21 +00001285// GlobalVarAttributes - Used to pass the attributes string on a global. CurGV
1286// is set to be the global we are processing.
1287//
1288GlobalVarAttributes : /* empty */ {} |
1289 ',' GlobalVarAttribute GlobalVarAttributes {};
1290GlobalVarAttribute : SectionString {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001291 CurGV->setSection(*$1);
1292 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001293 CHECK_FOR_ERROR
Chris Lattnerb2b96672005-11-12 18:21:21 +00001294 }
1295 | ALIGN EUINT64VAL {
1296 if ($2 != 0 && !isPowerOf2_32($2))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001297 GEN_ERROR("Alignment must be a power of two");
Chris Lattnerb2b96672005-11-12 18:21:21 +00001298 CurGV->setAlignment($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001299 CHECK_FOR_ERROR
Chris Lattnerb2b96672005-11-12 18:21:21 +00001300 };
Chris Lattner164c3782005-11-12 00:11:10 +00001301
Chris Lattner30c89792001-09-07 16:35:17 +00001302//===----------------------------------------------------------------------===//
1303// Types includes all predefined types... except void, because it can only be
Reid Spencere1553cc2006-12-31 05:40:12 +00001304// used in specific contexts (function returning void for example).
Chris Lattner30c89792001-09-07 16:35:17 +00001305
1306// Derived types are added later...
1307//
Reid Spencer8088e9d2007-01-13 05:00:20 +00001308PrimType : INTTYPE | FLOAT | DOUBLE | LABEL ;
Reid Spencere1553cc2006-12-31 05:40:12 +00001309
1310Types
1311 : OPAQUE {
Reid Spencer08de34b2006-12-03 05:45:44 +00001312 $$ = new PATypeHolder(OpaqueType::get());
Reid Spencer61c83e02006-08-18 08:43:06 +00001313 CHECK_FOR_ERROR
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001314 }
1315 | PrimType {
Reid Spencer08de34b2006-12-03 05:45:44 +00001316 $$ = new PATypeHolder($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001317 CHECK_FOR_ERROR
Reid Spencere1553cc2006-12-31 05:40:12 +00001318 }
1319 | Types '*' { // Pointer type?
1320 if (*$1 == Type::LabelTy)
1321 GEN_ERROR("Cannot form a pointer to a basic block");
1322 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
1323 delete $1;
1324 CHECK_FOR_ERROR
1325 }
1326 | SymbolicValueRef { // Named types are also simple types...
1327 const Type* tmp = getTypeVal($1);
1328 CHECK_FOR_ERROR
1329 $$ = new PATypeHolder(tmp);
1330 }
1331 | '\\' EUINT64VAL { // Type UpReference
Reid Spencerf4fa5902007-02-05 10:16:10 +00001332 if ($2 > (uint64_t)~0U) GEN_ERROR("Value out of range");
Chris Lattner30c89792001-09-07 16:35:17 +00001333 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
Chris Lattner3b073862004-02-09 03:19:29 +00001334 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Reid Spencer08de34b2006-12-03 05:45:44 +00001335 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +00001336 UR_OUT("New Upreference!\n");
Reid Spencer61c83e02006-08-18 08:43:06 +00001337 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001338 }
Reid Spencer863dd882007-04-28 16:06:50 +00001339 | Types '(' ArgTypeListI ')' OptFuncAttrs {
Chris Lattner9232b992003-04-22 18:42:41 +00001340 std::vector<const Type*> Params;
Reid Spencer863dd882007-04-28 16:06:50 +00001341 ParamAttrsVector Attrs;
1342 if ($5 != ParamAttr::None) {
1343 ParamAttrsWithIndex X; X.index = 0; X.attrs = $5;
1344 Attrs.push_back(X);
1345 }
Reid Spencer5694b6e2007-04-09 06:17:21 +00001346 unsigned index = 1;
1347 TypeWithAttrsList::iterator I = $3->begin(), E = $3->end();
1348 for (; I != E; ++I, ++index) {
Reid Spencer2c9df212007-03-20 01:13:00 +00001349 const Type *Ty = I->Ty->get();
Reid Spencer2c9df212007-03-20 01:13:00 +00001350 Params.push_back(Ty);
Reid Spencer863dd882007-04-28 16:06:50 +00001351 if (Ty != Type::VoidTy)
1352 if (I->Attrs != ParamAttr::None) {
1353 ParamAttrsWithIndex X; X.index = index; X.attrs = I->Attrs;
1354 Attrs.push_back(X);
1355 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001356 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001357 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1358 if (isVarArg) Params.pop_back();
1359
Reid Spencer863dd882007-04-28 16:06:50 +00001360 ParamAttrsList *ActualAttrs = 0;
1361 if (!Attrs.empty())
1362 ActualAttrs = ParamAttrsList::get(Attrs);
1363 FunctionType *FT = FunctionType::get(*$1, Params, isVarArg, ActualAttrs);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001364 delete $3; // Delete the argument list
Reid Spencere1553cc2006-12-31 05:40:12 +00001365 delete $1; // Delete the return type handle
1366 $$ = new PATypeHolder(HandleUpRefs(FT));
Reid Spencer61c83e02006-08-18 08:43:06 +00001367 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001368 }
Reid Spencer863dd882007-04-28 16:06:50 +00001369 | VOID '(' ArgTypeListI ')' OptFuncAttrs {
Reid Spencere1553cc2006-12-31 05:40:12 +00001370 std::vector<const Type*> Params;
Reid Spencer863dd882007-04-28 16:06:50 +00001371 ParamAttrsVector Attrs;
1372 if ($5 != ParamAttr::None) {
1373 ParamAttrsWithIndex X; X.index = 0; X.attrs = $5;
1374 Attrs.push_back(X);
1375 }
Reid Spencer5694b6e2007-04-09 06:17:21 +00001376 TypeWithAttrsList::iterator I = $3->begin(), E = $3->end();
1377 unsigned index = 1;
1378 for ( ; I != E; ++I, ++index) {
Reid Spencer2c9df212007-03-20 01:13:00 +00001379 const Type* Ty = I->Ty->get();
Reid Spencer2c9df212007-03-20 01:13:00 +00001380 Params.push_back(Ty);
Reid Spencer863dd882007-04-28 16:06:50 +00001381 if (Ty != Type::VoidTy)
1382 if (I->Attrs != ParamAttr::None) {
1383 ParamAttrsWithIndex X; X.index = index; X.attrs = I->Attrs;
1384 Attrs.push_back(X);
1385 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001386 }
1387 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1388 if (isVarArg) Params.pop_back();
1389
Reid Spencer863dd882007-04-28 16:06:50 +00001390 ParamAttrsList *ActualAttrs = 0;
1391 if (!Attrs.empty())
1392 ActualAttrs = ParamAttrsList::get(Attrs);
1393
1394 FunctionType *FT = FunctionType::get($1, Params, isVarArg, ActualAttrs);
Reid Spencer2c261782007-01-05 17:06:19 +00001395 delete $3; // Delete the argument list
Reid Spencere1553cc2006-12-31 05:40:12 +00001396 $$ = new PATypeHolder(HandleUpRefs(FT));
1397 CHECK_FOR_ERROR
1398 }
1399
1400 | '[' EUINT64VAL 'x' Types ']' { // Sized array type?
Reid Spencer08de34b2006-12-03 05:45:44 +00001401 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
1402 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00001403 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001404 }
Reid Spencerac9dcb92007-02-15 03:39:18 +00001405 | '<' EUINT64VAL 'x' Types '>' { // Vector type?
Reid Spencer08de34b2006-12-03 05:45:44 +00001406 const llvm::Type* ElemTy = $4->get();
1407 if ((unsigned)$2 != $2)
1408 GEN_ERROR("Unsigned result not equal to signed result");
Chris Lattner42a75512007-01-15 02:27:26 +00001409 if (!ElemTy->isFloatingPoint() && !ElemTy->isInteger())
Reid Spencer9d6565a2007-02-15 02:26:10 +00001410 GEN_ERROR("Element type of a VectorType must be primitive");
Reid Spencer08de34b2006-12-03 05:45:44 +00001411 if (!isPowerOf2_32($2))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001412 GEN_ERROR("Vector length should be a power of 2");
Reid Spencer9d6565a2007-02-15 02:26:10 +00001413 $$ = new PATypeHolder(HandleUpRefs(VectorType::get(*$4, (unsigned)$2)));
Reid Spencer08de34b2006-12-03 05:45:44 +00001414 delete $4;
1415 CHECK_FOR_ERROR
Brian Gaeke715c90b2004-08-20 06:00:58 +00001416 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001417 | '{' TypeListI '}' { // Structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001418 std::vector<const Type*> Elements;
Reid Spencer08de34b2006-12-03 05:45:44 +00001419 for (std::list<llvm::PATypeHolder>::iterator I = $2->begin(),
Chris Lattnera08976b2005-02-22 23:13:58 +00001420 E = $2->end(); I != E; ++I)
Reid Spencer08de34b2006-12-03 05:45:44 +00001421 Elements.push_back(*I);
Misha Brukman5a2a3822005-05-10 22:02:28 +00001422
Reid Spencer08de34b2006-12-03 05:45:44 +00001423 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001424 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001425 CHECK_FOR_ERROR
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001426 }
1427 | '{' '}' { // Empty structure type?
Reid Spencer08de34b2006-12-03 05:45:44 +00001428 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Reid Spencer61c83e02006-08-18 08:43:06 +00001429 CHECK_FOR_ERROR
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001430 }
Andrew Lenharth38ecbf12006-12-08 18:06:16 +00001431 | '<' '{' TypeListI '}' '>' {
1432 std::vector<const Type*> Elements;
1433 for (std::list<llvm::PATypeHolder>::iterator I = $3->begin(),
1434 E = $3->end(); I != E; ++I)
1435 Elements.push_back(*I);
1436
1437 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements, true)));
1438 delete $3;
1439 CHECK_FOR_ERROR
1440 }
1441 | '<' '{' '}' '>' { // Empty structure type?
1442 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>(), true));
1443 CHECK_FOR_ERROR
1444 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001445 ;
1446
1447ArgType
Reid Spencer863dd882007-04-28 16:06:50 +00001448 : Types OptParamAttrs {
Reid Spencere1553cc2006-12-31 05:40:12 +00001449 $$.Ty = $1;
Reid Spencer863dd882007-04-28 16:06:50 +00001450 $$.Attrs = $2;
Reid Spencere1553cc2006-12-31 05:40:12 +00001451 }
1452 ;
1453
Reid Spencer2c261782007-01-05 17:06:19 +00001454ResultTypes
1455 : Types {
Reid Spencere1553cc2006-12-31 05:40:12 +00001456 if (!UpRefs.empty())
1457 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1458 if (!(*$1)->isFirstClassType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001459 GEN_ERROR("LLVM functions cannot return aggregate types");
Reid Spencer2c261782007-01-05 17:06:19 +00001460 $$ = $1;
Reid Spencere1553cc2006-12-31 05:40:12 +00001461 }
Reid Spencer2c261782007-01-05 17:06:19 +00001462 | VOID {
1463 $$ = new PATypeHolder(Type::VoidTy);
Reid Spencere1553cc2006-12-31 05:40:12 +00001464 }
1465 ;
1466
1467ArgTypeList : ArgType {
1468 $$ = new TypeWithAttrsList();
1469 $$->push_back($1);
1470 CHECK_FOR_ERROR
1471 }
1472 | ArgTypeList ',' ArgType {
1473 ($$=$1)->push_back($3);
1474 CHECK_FOR_ERROR
1475 }
1476 ;
1477
1478ArgTypeListI
1479 : ArgTypeList
1480 | ArgTypeList ',' DOTDOTDOT {
1481 $$=$1;
Reid Spencer18da0722007-04-11 02:44:20 +00001482 TypeWithAttrs TWA; TWA.Attrs = ParamAttr::None;
Reid Spencere1553cc2006-12-31 05:40:12 +00001483 TWA.Ty = new PATypeHolder(Type::VoidTy);
1484 $$->push_back(TWA);
1485 CHECK_FOR_ERROR
1486 }
1487 | DOTDOTDOT {
1488 $$ = new TypeWithAttrsList;
Reid Spencer18da0722007-04-11 02:44:20 +00001489 TypeWithAttrs TWA; TWA.Attrs = ParamAttr::None;
Reid Spencere1553cc2006-12-31 05:40:12 +00001490 TWA.Ty = new PATypeHolder(Type::VoidTy);
1491 $$->push_back(TWA);
1492 CHECK_FOR_ERROR
1493 }
1494 | /*empty*/ {
1495 $$ = new TypeWithAttrsList();
Reid Spencer61c83e02006-08-18 08:43:06 +00001496 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001497 };
Chris Lattner30c89792001-09-07 16:35:17 +00001498
Chris Lattner7e708292002-06-25 16:13:24 +00001499// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +00001500// declaration type lists
1501//
Reid Spencere1553cc2006-12-31 05:40:12 +00001502TypeListI : Types {
Reid Spencer08de34b2006-12-03 05:45:44 +00001503 $$ = new std::list<PATypeHolder>();
Reid Spencer2c9df212007-03-20 01:13:00 +00001504 $$->push_back(*$1);
1505 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001506 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001507 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001508 | TypeListI ',' Types {
Reid Spencer2c9df212007-03-20 01:13:00 +00001509 ($$=$1)->push_back(*$3);
1510 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001511 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001512 };
Chris Lattner30c89792001-09-07 16:35:17 +00001513
Chris Lattnere98dda62001-07-14 06:10:16 +00001514// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +00001515// production is used ONLY to represent constants that show up AFTER a 'const',
1516// 'constant' or 'global' token at global scope. Constants that can be inlined
1517// into other expressions (such as integers and constexprs) are handled by the
1518// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +00001519//
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001520ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencere1553cc2006-12-31 05:40:12 +00001521 if (!UpRefs.empty())
1522 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001523 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001524 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001525 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001526 (*$1)->getDescription() + "'");
Chris Lattner30c89792001-09-07 16:35:17 +00001527 const Type *ETy = ATy->getElementType();
1528 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +00001529
Chris Lattner30c89792001-09-07 16:35:17 +00001530 // Verify that we have the correct size...
1531 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001532 GEN_ERROR("Type mismatch: constant sized array initialized with " +
Reid Spencer3271ed52004-07-18 00:08:11 +00001533 utostr($3->size()) + " arguments, but has size of " +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001534 itostr(NumElements) + "");
Chris Lattner00950542001-06-06 20:29:01 +00001535
Chris Lattner30c89792001-09-07 16:35:17 +00001536 // Verify all elements are correct type!
1537 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencer08de34b2006-12-03 05:45:44 +00001538 if (ETy != (*$3)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001539 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Reid Spencer3271ed52004-07-18 00:08:11 +00001540 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencer08de34b2006-12-03 05:45:44 +00001541 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +00001542 }
1543
Reid Spencer08de34b2006-12-03 05:45:44 +00001544 $$ = ConstantArray::get(ATy, *$3);
1545 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001546 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001547 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001548 | Types '[' ']' {
Reid Spencere1553cc2006-12-31 05:40:12 +00001549 if (!UpRefs.empty())
1550 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001551 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001552 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001553 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001554 (*$1)->getDescription() + "'");
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001555
1556 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +00001557 if (NumElements != -1 && NumElements != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001558 GEN_ERROR("Type mismatch: constant sized array initialized with 0"
Reid Spencerf4fa5902007-02-05 10:16:10 +00001559 " arguments, but has size of " + itostr(NumElements) +"");
Reid Spencer08de34b2006-12-03 05:45:44 +00001560 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
1561 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001562 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001563 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001564 | Types 'c' STRINGCONSTANT {
Reid Spencere1553cc2006-12-31 05:40:12 +00001565 if (!UpRefs.empty())
1566 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001567 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001568 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001569 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001570 (*$1)->getDescription() + "'");
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001571
Chris Lattner30c89792001-09-07 16:35:17 +00001572 int NumElements = ATy->getNumElements();
1573 const Type *ETy = ATy->getElementType();
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001574 if (NumElements != -1 && NumElements != int($3->length()))
Reid Spencer61c83e02006-08-18 08:43:06 +00001575 GEN_ERROR("Can't build string constant of size " +
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001576 itostr((int)($3->length())) +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001577 " when array has size " + itostr(NumElements) + "");
Chris Lattner9232b992003-04-22 18:42:41 +00001578 std::vector<Constant*> Vals;
Reid Spencere1553cc2006-12-31 05:40:12 +00001579 if (ETy == Type::Int8Ty) {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001580 for (unsigned i = 0; i < $3->length(); ++i)
1581 Vals.push_back(ConstantInt::get(ETy, (*$3)[i]));
Chris Lattner93750fa2001-07-28 17:48:55 +00001582 } else {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001583 delete $3;
Reid Spencerf4fa5902007-02-05 10:16:10 +00001584 GEN_ERROR("Cannot build string arrays of non byte sized elements");
Chris Lattner93750fa2001-07-28 17:48:55 +00001585 }
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001586 delete $3;
Reid Spencer08de34b2006-12-03 05:45:44 +00001587 $$ = ConstantArray::get(ATy, Vals);
1588 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001589 CHECK_FOR_ERROR
Chris Lattner93750fa2001-07-28 17:48:55 +00001590 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00001591 | Types '<' ConstVector '>' { // Nonempty unsized arr
Reid Spencere1553cc2006-12-31 05:40:12 +00001592 if (!UpRefs.empty())
1593 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer9d6565a2007-02-15 02:26:10 +00001594 const VectorType *PTy = dyn_cast<VectorType>($1->get());
Brian Gaeke715c90b2004-08-20 06:00:58 +00001595 if (PTy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001596 GEN_ERROR("Cannot make packed constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001597 (*$1)->getDescription() + "'");
Brian Gaeke715c90b2004-08-20 06:00:58 +00001598 const Type *ETy = PTy->getElementType();
1599 int NumElements = PTy->getNumElements();
1600
1601 // Verify that we have the correct size...
1602 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001603 GEN_ERROR("Type mismatch: constant sized packed initialized with " +
Brian Gaeke715c90b2004-08-20 06:00:58 +00001604 utostr($3->size()) + " arguments, but has size of " +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001605 itostr(NumElements) + "");
Brian Gaeke715c90b2004-08-20 06:00:58 +00001606
1607 // Verify all elements are correct type!
1608 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencer08de34b2006-12-03 05:45:44 +00001609 if (ETy != (*$3)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001610 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Brian Gaeke715c90b2004-08-20 06:00:58 +00001611 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencer08de34b2006-12-03 05:45:44 +00001612 (*$3)[i]->getType()->getDescription() + "'.");
Brian Gaeke715c90b2004-08-20 06:00:58 +00001613 }
1614
Reid Spencer9d6565a2007-02-15 02:26:10 +00001615 $$ = ConstantVector::get(PTy, *$3);
Reid Spencer08de34b2006-12-03 05:45:44 +00001616 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001617 CHECK_FOR_ERROR
Brian Gaeke715c90b2004-08-20 06:00:58 +00001618 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001619 | Types '{' ConstVector '}' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001620 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +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 Lattneraf76d0e2003-04-15 16:09:31 +00001624
Chris Lattnerc6212f12003-05-21 16:06:56 +00001625 if ($3->size() != STy->getNumContainedTypes())
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
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001628 // Check to ensure that constants are compatible with the type initializer!
1629 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Reid Spencer08de34b2006-12-03 05:45:44 +00001630 if ((*$3)[i]->getType() != STy->getElementType(i))
Reid Spencer61c83e02006-08-18 08:43:06 +00001631 GEN_ERROR("Expected type '" +
Chris Lattnerd21cd802004-02-09 04:37:31 +00001632 STy->getElementType(i)->getDescription() +
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001633 "' for element #" + utostr(i) +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001634 " of structure initializer");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001635
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001636 // Check to ensure that Type is not packed
1637 if (STy->isPacked())
Chris Lattner4989b842007-04-26 05:30:35 +00001638 GEN_ERROR("Unpacked Initializer to vector type '" +
1639 STy->getDescription() + "'");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001640
Reid Spencer08de34b2006-12-03 05:45:44 +00001641 $$ = ConstantStruct::get(STy, *$3);
1642 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001643 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001644 }
Chris Lattnerc6212f12003-05-21 16:06:56 +00001645 | Types '{' '}' {
Reid Spencere1553cc2006-12-31 05:40:12 +00001646 if (!UpRefs.empty())
1647 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001648 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerc6212f12003-05-21 16:06:56 +00001649 if (STy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001650 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001651 (*$1)->getDescription() + "'");
Chris Lattnerc6212f12003-05-21 16:06:56 +00001652
1653 if (STy->getNumContainedTypes() != 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001654 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattnerc6212f12003-05-21 16:06:56 +00001655
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001656 // Check to ensure that Type is not packed
1657 if (STy->isPacked())
Chris Lattner4989b842007-04-26 05:30:35 +00001658 GEN_ERROR("Unpacked Initializer to vector type '" +
1659 STy->getDescription() + "'");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001660
1661 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1662 delete $1;
1663 CHECK_FOR_ERROR
1664 }
1665 | Types '<' '{' ConstVector '}' '>' {
1666 const StructType *STy = dyn_cast<StructType>($1->get());
1667 if (STy == 0)
1668 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001669 (*$1)->getDescription() + "'");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001670
1671 if ($4->size() != STy->getNumContainedTypes())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001672 GEN_ERROR("Illegal number of initializers for structure type");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001673
1674 // Check to ensure that constants are compatible with the type initializer!
1675 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1676 if ((*$4)[i]->getType() != STy->getElementType(i))
1677 GEN_ERROR("Expected type '" +
1678 STy->getElementType(i)->getDescription() +
1679 "' for element #" + utostr(i) +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001680 " of structure initializer");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001681
1682 // Check to ensure that Type is packed
1683 if (!STy->isPacked())
Reid Spencerac9dcb92007-02-15 03:39:18 +00001684 GEN_ERROR("Vector initializer to non-vector type '" +
1685 STy->getDescription() + "'");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001686
1687 $$ = ConstantStruct::get(STy, *$4);
1688 delete $1; delete $4;
1689 CHECK_FOR_ERROR
1690 }
1691 | Types '<' '{' '}' '>' {
1692 if (!UpRefs.empty())
1693 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1694 const StructType *STy = dyn_cast<StructType>($1->get());
1695 if (STy == 0)
1696 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001697 (*$1)->getDescription() + "'");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001698
1699 if (STy->getNumContainedTypes() != 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001700 GEN_ERROR("Illegal number of initializers for structure type");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001701
1702 // Check to ensure that Type is packed
1703 if (!STy->isPacked())
Reid Spencerac9dcb92007-02-15 03:39:18 +00001704 GEN_ERROR("Vector initializer to non-vector type '" +
1705 STy->getDescription() + "'");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001706
Reid Spencer08de34b2006-12-03 05:45:44 +00001707 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1708 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001709 CHECK_FOR_ERROR
Chris Lattnerc6212f12003-05-21 16:06:56 +00001710 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001711 | Types NULL_TOK {
Reid Spencere1553cc2006-12-31 05:40:12 +00001712 if (!UpRefs.empty())
1713 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001714 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001715 if (PTy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001716 GEN_ERROR("Cannot make null pointer constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001717 (*$1)->getDescription() + "'");
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001718
Reid Spencer08de34b2006-12-03 05:45:44 +00001719 $$ = ConstantPointerNull::get(PTy);
1720 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001721 CHECK_FOR_ERROR
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001722 }
Chris Lattner16710e92004-10-16 18:17:13 +00001723 | Types UNDEF {
Reid Spencere1553cc2006-12-31 05:40:12 +00001724 if (!UpRefs.empty())
1725 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001726 $$ = UndefValue::get($1->get());
1727 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001728 CHECK_FOR_ERROR
Chris Lattner16710e92004-10-16 18:17:13 +00001729 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001730 | Types SymbolicValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00001731 if (!UpRefs.empty())
1732 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001733 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001734 if (Ty == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001735 GEN_ERROR("Global const reference must be a pointer type");
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001736
Chris Lattner3101c252002-08-15 17:58:33 +00001737 // ConstExprs can exist in the body of a function, thus creating
Reid Spencer3271ed52004-07-18 00:08:11 +00001738 // GlobalValues whenever they refer to a variable. Because we are in
Reid Spencer186a43f2007-03-19 18:39:36 +00001739 // the context of a function, getExistingVal will search the functions
Chris Lattner3101c252002-08-15 17:58:33 +00001740 // symbol table instead of the module symbol table for the global symbol,
1741 // which throws things all off. To get around this, we just tell
Reid Spencer186a43f2007-03-19 18:39:36 +00001742 // getExistingVal that we are at global scope here.
Chris Lattner3101c252002-08-15 17:58:33 +00001743 //
Chris Lattner394f2eb2003-10-16 20:12:13 +00001744 Function *SavedCurFn = CurFun.CurrentFunction;
1745 CurFun.CurrentFunction = 0;
Chris Lattner3101c252002-08-15 17:58:33 +00001746
Reid Spencer186a43f2007-03-19 18:39:36 +00001747 Value *V = getExistingVal(Ty, $2);
Reid Spencer5b7e7532006-09-28 19:28:24 +00001748 CHECK_FOR_ERROR
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001749
Chris Lattner394f2eb2003-10-16 20:12:13 +00001750 CurFun.CurrentFunction = SavedCurFn;
Chris Lattner3101c252002-08-15 17:58:33 +00001751
Chris Lattner2079fde2001-10-13 06:41:08 +00001752 // If this is an initializer for a constant pointer, which is referencing a
1753 // (currently) undefined variable, create a stub now that shall be replaced
1754 // in the future with the right type of variable.
1755 //
1756 if (V == 0) {
Reid Spencer1755a9a2007-02-05 17:01:20 +00001757 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001758 const PointerType *PT = cast<PointerType>(Ty);
1759
1760 // First check to see if the forward references value is already created!
1761 PerModuleInfo::GlobalRefsType::iterator I =
Reid Spencer3271ed52004-07-18 00:08:11 +00001762 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Chris Lattner2079fde2001-10-13 06:41:08 +00001763
1764 if (I != CurModule.GlobalRefs.end()) {
Reid Spencer3271ed52004-07-18 00:08:11 +00001765 V = I->second; // Placeholder already exists, use it...
Chris Lattnera989b232003-12-23 20:05:15 +00001766 $2.destroy();
Chris Lattner2079fde2001-10-13 06:41:08 +00001767 } else {
Chris Lattner8a327842004-07-14 21:44:00 +00001768 std::string Name;
Reid Spencerb2d17862007-01-26 08:04:51 +00001769 if ($2.Type == ValID::GlobalName)
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001770 Name = $2.getName();
Reid Spencerb2d17862007-01-26 08:04:51 +00001771 else if ($2.Type != ValID::GlobalID)
1772 GEN_ERROR("Invalid reference to global");
Chris Lattner8a327842004-07-14 21:44:00 +00001773
Reid Spencer3271ed52004-07-18 00:08:11 +00001774 // Create the forward referenced global.
Chris Lattnera09000d2004-07-14 23:03:46 +00001775 GlobalValue *GV;
1776 if (const FunctionType *FTy =
1777 dyn_cast<FunctionType>(PT->getElementType())) {
Chris Lattner4989b842007-04-26 05:30:35 +00001778 GV = new Function(FTy, GlobalValue::ExternalWeakLinkage, Name,
Chris Lattnera09000d2004-07-14 23:03:46 +00001779 CurModule.CurrentModule);
1780 } else {
Reid Spencer3271ed52004-07-18 00:08:11 +00001781 GV = new GlobalVariable(PT->getElementType(), false,
Chris Lattner4989b842007-04-26 05:30:35 +00001782 GlobalValue::ExternalWeakLinkage, 0,
Chris Lattnera09000d2004-07-14 23:03:46 +00001783 Name, CurModule.CurrentModule);
1784 }
Chris Lattner8a327842004-07-14 21:44:00 +00001785
Reid Spencer3271ed52004-07-18 00:08:11 +00001786 // Keep track of the fact that we have a forward ref to recycle it
1787 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1788 V = GV;
Chris Lattner2079fde2001-10-13 06:41:08 +00001789 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001790 }
1791
Reid Spencer08de34b2006-12-03 05:45:44 +00001792 $$ = cast<GlobalValue>(V);
1793 delete $1; // Free the type handle
Reid Spencer61c83e02006-08-18 08:43:06 +00001794 CHECK_FOR_ERROR
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001795 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001796 | Types ConstExpr {
Reid Spencere1553cc2006-12-31 05:40:12 +00001797 if (!UpRefs.empty())
1798 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001799 if ($1->get() != $2->getType())
Reid Spencerb4fdfdb2007-01-04 00:05:48 +00001800 GEN_ERROR("Mismatched types for constant expression: " +
1801 (*$1)->getDescription() + " and " + $2->getType()->getDescription());
Chris Lattnerd78700d2002-08-16 21:14:40 +00001802 $$ = $2;
Reid Spencer08de34b2006-12-03 05:45:44 +00001803 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001804 CHECK_FOR_ERROR
Chris Lattnerf4600482003-06-28 20:01:34 +00001805 }
1806 | Types ZEROINITIALIZER {
Reid Spencere1553cc2006-12-31 05:40:12 +00001807 if (!UpRefs.empty())
1808 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001809 const Type *Ty = $1->get();
Chris Lattner78915932004-11-28 16:45:45 +00001810 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001811 GEN_ERROR("Cannot create a null initialized value of this type");
Reid Spencer08de34b2006-12-03 05:45:44 +00001812 $$ = Constant::getNullValue(Ty);
1813 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001814 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00001815 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001816 | IntType ESINT64VAL { // integral constants
Reid Spencer9ffad0a2006-12-20 17:20:09 +00001817 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001818 GEN_ERROR("Constant value doesn't fit in type");
Reid Spencereac65742007-03-19 20:40:22 +00001819 $$ = ConstantInt::get($1, $2, true);
Reid Spencerf2310042007-02-28 02:23:44 +00001820 CHECK_FOR_ERROR
1821 }
1822 | IntType ESAPINTVAL { // arbitrary precision integer constants
1823 uint32_t BitWidth = cast<IntegerType>($1)->getBitWidth();
1824 if ($2->getBitWidth() > BitWidth) {
1825 GEN_ERROR("Constant value does not fit in type");
Reid Spenceraf1aacd2007-03-01 19:32:01 +00001826 }
1827 $2->sextOrTrunc(BitWidth);
1828 $$ = ConstantInt::get(*$2);
Reid Spencerf2310042007-02-28 02:23:44 +00001829 delete $2;
Reid Spencer9ffad0a2006-12-20 17:20:09 +00001830 CHECK_FOR_ERROR
1831 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001832 | IntType EUINT64VAL { // integral constants
Reid Spencer9ffad0a2006-12-20 17:20:09 +00001833 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001834 GEN_ERROR("Constant value doesn't fit in type");
Reid Spencereac65742007-03-19 20:40:22 +00001835 $$ = ConstantInt::get($1, $2, false);
Reid Spencerf2310042007-02-28 02:23:44 +00001836 CHECK_FOR_ERROR
1837 }
1838 | IntType EUAPINTVAL { // arbitrary precision integer constants
1839 uint32_t BitWidth = cast<IntegerType>($1)->getBitWidth();
1840 if ($2->getBitWidth() > BitWidth) {
1841 GEN_ERROR("Constant value does not fit in type");
Reid Spenceraf1aacd2007-03-01 19:32:01 +00001842 }
1843 $2->zextOrTrunc(BitWidth);
1844 $$ = ConstantInt::get(*$2);
Reid Spencerf2310042007-02-28 02:23:44 +00001845 delete $2;
Reid Spencer9ffad0a2006-12-20 17:20:09 +00001846 CHECK_FOR_ERROR
1847 }
Reid Spencer8088e9d2007-01-13 05:00:20 +00001848 | INTTYPE TRUETOK { // Boolean constants
1849 assert(cast<IntegerType>($1)->getBitWidth() == 1 && "Not Bool?");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001850 $$ = ConstantInt::getTrue();
Reid Spencer61c83e02006-08-18 08:43:06 +00001851 CHECK_FOR_ERROR
Chris Lattnerd05e3592002-08-15 18:17:28 +00001852 }
Reid Spencer8088e9d2007-01-13 05:00:20 +00001853 | INTTYPE FALSETOK { // Boolean constants
1854 assert(cast<IntegerType>($1)->getBitWidth() == 1 && "Not Bool?");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001855 $$ = ConstantInt::getFalse();
Reid Spencer61c83e02006-08-18 08:43:06 +00001856 CHECK_FOR_ERROR
Chris Lattnerd05e3592002-08-15 18:17:28 +00001857 }
1858 | FPType FPVAL { // Float & Double constants
Reid Spencer08de34b2006-12-03 05:45:44 +00001859 if (!ConstantFP::isValueValidForType($1, $2))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001860 GEN_ERROR("Floating point constant invalid for type");
Reid Spencer08de34b2006-12-03 05:45:44 +00001861 $$ = ConstantFP::get($1, $2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001862 CHECK_FOR_ERROR
Chris Lattnerd05e3592002-08-15 18:17:28 +00001863 };
1864
Chris Lattner00950542001-06-06 20:29:01 +00001865
Reid Spencer3da59db2006-11-27 01:05:10 +00001866ConstExpr: CastOps '(' ConstVal TO Types ')' {
Reid Spencere1553cc2006-12-31 05:40:12 +00001867 if (!UpRefs.empty())
1868 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001869 Constant *Val = $3;
Reid Spencer93947c32007-01-17 02:47:33 +00001870 const Type *DestTy = $5->get();
1871 if (!CastInst::castIsValid($1, $3, DestTy))
1872 GEN_ERROR("invalid cast opcode for cast from '" +
1873 Val->getType()->getDescription() + "' to '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001874 DestTy->getDescription() + "'");
Reid Spencer93947c32007-01-17 02:47:33 +00001875 $$ = ConstantExpr::getCast($1, $3, DestTy);
Reid Spencer08de34b2006-12-03 05:45:44 +00001876 delete $5;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001877 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001878 | GETELEMENTPTR '(' ConstVal IndexList ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001879 if (!isa<PointerType>($3->getType()))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001880 GEN_ERROR("GetElementPtr requires a pointer operand");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001881
Reid Spencer08de34b2006-12-03 05:45:44 +00001882 const Type *IdxTy =
Chris Lattnerc856c7a2007-02-13 00:57:40 +00001883 GetElementPtrInst::getIndexedType($3->getType(), &(*$4)[0], $4->size(),
1884 true);
Reid Spencer08de34b2006-12-03 05:45:44 +00001885 if (!IdxTy)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001886 GEN_ERROR("Index list invalid for constant getelementptr");
Reid Spencer08de34b2006-12-03 05:45:44 +00001887
Chris Lattnere0135402007-01-31 04:43:46 +00001888 SmallVector<Constant*, 8> IdxVec;
Reid Spencer08de34b2006-12-03 05:45:44 +00001889 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1890 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001891 IdxVec.push_back(C);
1892 else
Reid Spencerf4fa5902007-02-05 10:16:10 +00001893 GEN_ERROR("Indices to constant getelementptr must be constants");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001894
Chris Lattnerd78700d2002-08-16 21:14:40 +00001895 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001896
Chris Lattnere0135402007-01-31 04:43:46 +00001897 $$ = ConstantExpr::getGetElementPtr($3, &IdxVec[0], IdxVec.size());
Reid Spencer61c83e02006-08-18 08:43:06 +00001898 CHECK_FOR_ERROR
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001899 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001900 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer4fe16d62007-01-11 18:21:29 +00001901 if ($3->getType() != Type::Int1Ty)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001902 GEN_ERROR("Select condition must be of boolean type");
Reid Spencer08de34b2006-12-03 05:45:44 +00001903 if ($5->getType() != $7->getType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001904 GEN_ERROR("Select operand types must match");
Reid Spencer08de34b2006-12-03 05:45:44 +00001905 $$ = ConstantExpr::getSelect($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001906 CHECK_FOR_ERROR
Chris Lattner76f0ff32004-03-12 05:51:36 +00001907 }
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001908 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001909 if ($3->getType() != $5->getType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001910 GEN_ERROR("Binary operator types must match");
Reid Spencer1628cec2006-10-26 06:15:43 +00001911 CHECK_FOR_ERROR;
Reid Spencerc6e956e2006-12-05 19:15:41 +00001912 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001913 }
1914 | LogicalOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001915 if ($3->getType() != $5->getType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001916 GEN_ERROR("Logical operator types must match");
Chris Lattner42a75512007-01-15 02:27:26 +00001917 if (!$3->getType()->isInteger()) {
Reid Spencer9d6565a2007-02-15 02:26:10 +00001918 if (Instruction::isShift($1) || !isa<VectorType>($3->getType()) ||
1919 !cast<VectorType>($3->getType())->getElementType()->isInteger())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001920 GEN_ERROR("Logical operator requires integral operands");
Chris Lattner0a017832005-12-21 18:31:29 +00001921 }
Reid Spencer08de34b2006-12-03 05:45:44 +00001922 $$ = ConstantExpr::get($1, $3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001923 CHECK_FOR_ERROR
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001924 }
Reid Spencer763ed5e2006-12-04 05:20:06 +00001925 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
1926 if ($4->getType() != $6->getType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001927 GEN_ERROR("icmp operand types must match");
Reid Spencer763ed5e2006-12-04 05:20:06 +00001928 $$ = ConstantExpr::getICmp($2, $4, $6);
Reid Spencer08de34b2006-12-03 05:45:44 +00001929 }
Reid Spencer763ed5e2006-12-04 05:20:06 +00001930 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
1931 if ($4->getType() != $6->getType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001932 GEN_ERROR("fcmp operand types must match");
Reid Spencer763ed5e2006-12-04 05:20:06 +00001933 $$ = ConstantExpr::getFCmp($2, $4, $6);
Reid Spencer08de34b2006-12-03 05:45:44 +00001934 }
Robert Bocchino9c62b562006-01-10 19:04:32 +00001935 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001936 if (!ExtractElementInst::isValidOperands($3, $5))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001937 GEN_ERROR("Invalid extractelement operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00001938 $$ = ConstantExpr::getExtractElement($3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001939 CHECK_FOR_ERROR
Chris Lattnerca73cd82006-04-08 03:53:34 +00001940 }
1941 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001942 if (!InsertElementInst::isValidOperands($3, $5, $7))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001943 GEN_ERROR("Invalid insertelement operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00001944 $$ = ConstantExpr::getInsertElement($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001945 CHECK_FOR_ERROR
Chris Lattnerca73cd82006-04-08 03:53:34 +00001946 }
1947 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001948 if (!ShuffleVectorInst::isValidOperands($3, $5, $7))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001949 GEN_ERROR("Invalid shufflevector operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00001950 $$ = ConstantExpr::getShuffleVector($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001951 CHECK_FOR_ERROR
Chris Lattner699f1eb2002-08-14 17:12:33 +00001952 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001953
Chris Lattnerca73cd82006-04-08 03:53:34 +00001954
Misha Brukmanbc0e9982003-07-14 17:20:40 +00001955// ConstVector - A list of comma separated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001956ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001957 ($$ = $1)->push_back($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00001958 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001959 }
1960 | ConstVal {
Reid Spencer08de34b2006-12-03 05:45:44 +00001961 $$ = new std::vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001962 $$->push_back($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001963 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001964 };
Chris Lattner00950542001-06-06 20:29:01 +00001965
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001966
Chris Lattner1781aca2001-09-18 04:00:54 +00001967// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001968GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001969
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00001970// ThreadLocal
1971ThreadLocal : THREAD_LOCAL { $$ = true; } | { $$ = false; };
1972
Anton Korobeynikova80e1182007-04-28 13:45:00 +00001973// AliaseeRef - Match either GlobalValue or bitcast to GlobalValue.
1974AliaseeRef : ResultTypes SymbolicValueRef {
1975 const Type* VTy = $1->get();
1976 Value *V = getVal(VTy, $2);
1977 GlobalValue* Aliasee = dyn_cast<GlobalValue>(V);
1978 if (!Aliasee)
1979 GEN_ERROR("Aliases can be created only to global values");
1980
1981 $$ = Aliasee;
1982 CHECK_FOR_ERROR
1983 delete $1;
1984 }
1985 | BITCAST '(' AliaseeRef TO Types ')' {
1986 Constant *Val = $3;
1987 const Type *DestTy = $5->get();
1988 if (!CastInst::castIsValid($1, $3, DestTy))
1989 GEN_ERROR("invalid cast opcode for cast from '" +
1990 Val->getType()->getDescription() + "' to '" +
1991 DestTy->getDescription() + "'");
1992
1993 $$ = ConstantExpr::getCast($1, $3, DestTy);
1994 CHECK_FOR_ERROR
1995 delete $5;
1996 };
Chris Lattner00950542001-06-06 20:29:01 +00001997
Chris Lattner0e73ce62002-05-02 19:11:13 +00001998//===----------------------------------------------------------------------===//
1999// Rules to match Modules
2000//===----------------------------------------------------------------------===//
2001
2002// Module rule: Capture the result of parsing the whole file into a result
2003// variable...
2004//
Reid Spencerb951bc02006-12-29 20:29:48 +00002005Module
2006 : DefinitionList {
2007 $$ = ParserResult = CurModule.CurrentModule;
2008 CurModule.ModuleDone();
2009 CHECK_FOR_ERROR;
2010 }
2011 | /*empty*/ {
2012 $$ = ParserResult = CurModule.CurrentModule;
2013 CurModule.ModuleDone();
2014 CHECK_FOR_ERROR;
2015 }
2016 ;
Chris Lattner0e73ce62002-05-02 19:11:13 +00002017
Reid Spencerb951bc02006-12-29 20:29:48 +00002018DefinitionList
2019 : Definition
2020 | DefinitionList Definition
2021 ;
2022
2023Definition
Jeff Cohen361c3ef2007-01-21 19:19:31 +00002024 : DEFINE { CurFun.isDeclare = false; } Function {
Chris Lattner394f2eb2003-10-16 20:12:13 +00002025 CurFun.FunctionDone();
Reid Spencer61c83e02006-08-18 08:43:06 +00002026 CHECK_FOR_ERROR
Reid Spencerb951bc02006-12-29 20:29:48 +00002027 }
2028 | DECLARE { CurFun.isDeclare = true; } FunctionProto {
Reid Spencer61c83e02006-08-18 08:43:06 +00002029 CHECK_FOR_ERROR
Chris Lattner0e73ce62002-05-02 19:11:13 +00002030 }
Reid Spencerb951bc02006-12-29 20:29:48 +00002031 | MODULE ASM_TOK AsmBlock {
Reid Spencer61c83e02006-08-18 08:43:06 +00002032 CHECK_FOR_ERROR
Chris Lattneree454772006-01-23 23:05:15 +00002033 }
Reid Spencerb2d17862007-01-26 08:04:51 +00002034 | OptLocalAssign TYPE Types {
Reid Spencere1553cc2006-12-31 05:40:12 +00002035 if (!UpRefs.empty())
2036 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattner4a42e902001-10-22 05:56:09 +00002037 // Eagerly resolve types. This is not an optimization, this is a
2038 // requirement that is due to the fact that we could have this:
2039 //
2040 // %list = type { %list * }
2041 // %list = type { %list * } ; repeated type decl
2042 //
2043 // If types are not resolved eagerly, then the two types will not be
2044 // determined to be the same type!
2045 //
Reid Spencerb951bc02006-12-29 20:29:48 +00002046 ResolveTypeTo($1, *$3);
Chris Lattner4a42e902001-10-22 05:56:09 +00002047
Reid Spencerb951bc02006-12-29 20:29:48 +00002048 if (!setTypeName(*$3, $1) && !$1) {
Reid Spencer5b7e7532006-09-28 19:28:24 +00002049 CHECK_FOR_ERROR
Chris Lattner8c89a0a2004-07-13 08:10:10 +00002050 // If this is a named type that is not a redefinition, add it to the slot
2051 // table.
Reid Spencerb951bc02006-12-29 20:29:48 +00002052 CurModule.Types.push_back(*$3);
Chris Lattner30c89792001-09-07 16:35:17 +00002053 }
Reid Spencer08de34b2006-12-03 05:45:44 +00002054
Reid Spencerb951bc02006-12-29 20:29:48 +00002055 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002056 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00002057 }
Reid Spencerb2d17862007-01-26 08:04:51 +00002058 | OptLocalAssign TYPE VOID {
Reid Spencere1553cc2006-12-31 05:40:12 +00002059 ResolveTypeTo($1, $3);
2060
2061 if (!setTypeName($3, $1) && !$1) {
2062 CHECK_FOR_ERROR
2063 // If this is a named type that is not a redefinition, add it to the slot
2064 // table.
2065 CurModule.Types.push_back($3);
2066 }
2067 CHECK_FOR_ERROR
2068 }
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002069 | OptGlobalAssign GVVisibilityStyle ThreadLocal GlobalType ConstVal {
Reid Spencerb2d17862007-01-26 08:04:51 +00002070 /* "Externally Visible" Linkage */
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002071 if ($5 == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002072 GEN_ERROR("Global value initializer is not a constant");
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002073 CurGV = ParseGlobalVariable($1, GlobalValue::ExternalLinkage,
2074 $2, $4, $5->getType(), $5, $3);
Reid Spencerb951bc02006-12-29 20:29:48 +00002075 CHECK_FOR_ERROR
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002076 } GlobalVarAttributes {
2077 CurGV = 0;
2078 }
Chris Lattner4989b842007-04-26 05:30:35 +00002079 | OptGlobalAssign GVInternalLinkage GVVisibilityStyle ThreadLocal GlobalType
2080 ConstVal {
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002081 if ($6 == 0)
2082 GEN_ERROR("Global value initializer is not a constant");
2083 CurGV = ParseGlobalVariable($1, $2, $3, $5, $6->getType(), $6, $4);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002084 CHECK_FOR_ERROR
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002085 } GlobalVarAttributes {
2086 CurGV = 0;
2087 }
Chris Lattner4989b842007-04-26 05:30:35 +00002088 | OptGlobalAssign GVExternalLinkage GVVisibilityStyle ThreadLocal GlobalType
2089 Types {
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002090 if (!UpRefs.empty())
2091 GEN_ERROR("Invalid upreference in type: " + (*$6)->getDescription());
2092 CurGV = ParseGlobalVariable($1, $2, $3, $5, *$6, 0, $4);
2093 CHECK_FOR_ERROR
2094 delete $6;
Reid Spencer5b7e7532006-09-28 19:28:24 +00002095 } GlobalVarAttributes {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002096 CurGV = 0;
2097 CHECK_FOR_ERROR
2098 }
Anton Korobeynikova80e1182007-04-28 13:45:00 +00002099 | OptGlobalAssign GVVisibilityStyle ALIAS AliasLinkage AliaseeRef {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002100 std::string Name;
2101 if ($1) {
2102 Name = *$1;
2103 delete $1;
2104 }
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00002105 if (Name.empty())
Anton Korobeynikova80e1182007-04-28 13:45:00 +00002106 GEN_ERROR("Alias name cannot be empty");
2107
2108 Constant* Aliasee = $5;
2109 if (Aliasee == 0)
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002110 GEN_ERROR(std::string("Invalid aliasee for alias: ") + Name);
Anton Korobeynikova80e1182007-04-28 13:45:00 +00002111
2112 GlobalAlias* GA = new GlobalAlias(Aliasee->getType(), $4, Name, Aliasee,
2113 CurModule.CurrentModule);
2114 GA->setVisibility($2);
2115 InsertValue(GA, CurModule.Values);
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00002116 CHECK_FOR_ERROR
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00002117 }
Reid Spencerb951bc02006-12-29 20:29:48 +00002118 | TARGET TargetDefinition {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002119 CHECK_FOR_ERROR
2120 }
Reid Spencerb951bc02006-12-29 20:29:48 +00002121 | DEPLIBS '=' LibrariesDefinition {
Reid Spencer61c83e02006-08-18 08:43:06 +00002122 CHECK_FOR_ERROR
Chris Lattnere98dda62001-07-14 06:10:16 +00002123 }
Reid Spencerb951bc02006-12-29 20:29:48 +00002124 ;
Chris Lattner00950542001-06-06 20:29:01 +00002125
2126
Chris Lattneree454772006-01-23 23:05:15 +00002127AsmBlock : STRINGCONSTANT {
Chris Lattner66316012006-01-24 04:14:29 +00002128 const std::string &AsmSoFar = CurModule.CurrentModule->getModuleInlineAsm();
Chris Lattneree454772006-01-23 23:05:15 +00002129 if (AsmSoFar.empty())
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002130 CurModule.CurrentModule->setModuleInlineAsm(*$1);
Chris Lattneree454772006-01-23 23:05:15 +00002131 else
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002132 CurModule.CurrentModule->setModuleInlineAsm(AsmSoFar+"\n"+*$1);
2133 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002134 CHECK_FOR_ERROR
Chris Lattneree454772006-01-23 23:05:15 +00002135};
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00002136
Reid Spencerb2d17862007-01-26 08:04:51 +00002137TargetDefinition : TRIPLE '=' STRINGCONSTANT {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002138 CurModule.CurrentModule->setTargetTriple(*$3);
2139 delete $3;
John Criswell2f6a8b12006-10-24 19:09:48 +00002140 }
Chris Lattner10b27112006-10-22 06:07:41 +00002141 | DATALAYOUT '=' STRINGCONSTANT {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002142 CurModule.CurrentModule->setDataLayout(*$3);
2143 delete $3;
Owen Andersoncf7ff2b2006-10-18 02:21:12 +00002144 };
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00002145
Reid Spencerfeaf10e2004-07-25 21:30:51 +00002146LibrariesDefinition : '[' LibList ']';
Reid Spencer0be13e72004-07-25 17:58:28 +00002147
2148LibList : LibList ',' STRINGCONSTANT {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002149 CurModule.CurrentModule->addLibrary(*$3);
2150 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002151 CHECK_FOR_ERROR
Reid Spencer0be13e72004-07-25 17:58:28 +00002152 }
2153 | STRINGCONSTANT {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002154 CurModule.CurrentModule->addLibrary(*$1);
2155 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002156 CHECK_FOR_ERROR
Reid Spencer0be13e72004-07-25 17:58:28 +00002157 }
2158 | /* empty: end of list */ {
Reid Spencer61c83e02006-08-18 08:43:06 +00002159 CHECK_FOR_ERROR
Reid Spencer0be13e72004-07-25 17:58:28 +00002160 }
2161 ;
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00002162
Chris Lattner00950542001-06-06 20:29:01 +00002163//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00002164// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00002165//===----------------------------------------------------------------------===//
2166
Reid Spencerb2d17862007-01-26 08:04:51 +00002167ArgListH : ArgListH ',' Types OptParamAttrs OptLocalName {
Reid Spencere1553cc2006-12-31 05:40:12 +00002168 if (!UpRefs.empty())
2169 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
2170 if (*$3 == Type::VoidTy)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002171 GEN_ERROR("void typed arguments are invalid");
Reid Spencere1553cc2006-12-31 05:40:12 +00002172 ArgListEntry E; E.Attrs = $4; E.Ty = $3; E.Name = $5;
Chris Lattner69da5cf2002-10-13 20:57:00 +00002173 $$ = $1;
Reid Spencere1553cc2006-12-31 05:40:12 +00002174 $1->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002175 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002176 }
Reid Spencerb2d17862007-01-26 08:04:51 +00002177 | Types OptParamAttrs OptLocalName {
Reid Spencere1553cc2006-12-31 05:40:12 +00002178 if (!UpRefs.empty())
2179 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2180 if (*$1 == Type::VoidTy)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002181 GEN_ERROR("void typed arguments are invalid");
Reid Spencere1553cc2006-12-31 05:40:12 +00002182 ArgListEntry E; E.Attrs = $2; E.Ty = $1; E.Name = $3;
2183 $$ = new ArgListType;
2184 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002185 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002186 };
Chris Lattner00950542001-06-06 20:29:01 +00002187
2188ArgList : ArgListH {
2189 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002190 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002191 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00002192 | ArgListH ',' DOTDOTDOT {
2193 $$ = $1;
Reid Spencere1553cc2006-12-31 05:40:12 +00002194 struct ArgListEntry E;
2195 E.Ty = new PATypeHolder(Type::VoidTy);
2196 E.Name = 0;
Reid Spencer18da0722007-04-11 02:44:20 +00002197 E.Attrs = ParamAttr::None;
Reid Spencere1553cc2006-12-31 05:40:12 +00002198 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002199 CHECK_FOR_ERROR
Chris Lattner69da5cf2002-10-13 20:57:00 +00002200 }
2201 | DOTDOTDOT {
Reid Spencere1553cc2006-12-31 05:40:12 +00002202 $$ = new ArgListType;
2203 struct ArgListEntry E;
2204 E.Ty = new PATypeHolder(Type::VoidTy);
2205 E.Name = 0;
Reid Spencer18da0722007-04-11 02:44:20 +00002206 E.Attrs = ParamAttr::None;
Reid Spencere1553cc2006-12-31 05:40:12 +00002207 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002208 CHECK_FOR_ERROR
Chris Lattner69da5cf2002-10-13 20:57:00 +00002209 }
Chris Lattner00950542001-06-06 20:29:01 +00002210 | /* empty */ {
2211 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00002212 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002213 };
Chris Lattner00950542001-06-06 20:29:01 +00002214
Reid Spencerb2d17862007-01-26 08:04:51 +00002215FunctionHeaderH : OptCallingConv ResultTypes GlobalName '(' ArgList ')'
Reid Spencer2c261782007-01-05 17:06:19 +00002216 OptFuncAttrs OptSection OptAlign {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002217 std::string FunctionName(*$3);
2218 delete $3; // Free strdup'd memory!
Chris Lattnerdda71962001-11-26 18:54:16 +00002219
Reid Spencer98b3c5c2007-01-02 21:53:43 +00002220 // Check the function result for abstractness if this is a define. We should
2221 // have no abstract types at this point
Reid Spencer2c261782007-01-05 17:06:19 +00002222 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved($2))
2223 GEN_ERROR("Reference to abstract result: "+ $2->get()->getDescription());
Reid Spencer98b3c5c2007-01-02 21:53:43 +00002224
Chris Lattner9232b992003-04-22 18:42:41 +00002225 std::vector<const Type*> ParamTypeList;
Reid Spencer4f859aa2007-04-22 05:46:44 +00002226 ParamAttrsVector Attrs;
2227 if ($7 != ParamAttr::None) {
2228 ParamAttrsWithIndex PAWI; PAWI.index = 0; PAWI.attrs = $7;
2229 Attrs.push_back(PAWI);
2230 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00002231 if ($5) { // If there are arguments...
Reid Spencer5694b6e2007-04-09 06:17:21 +00002232 unsigned index = 1;
2233 for (ArgListType::iterator I = $5->begin(); I != $5->end(); ++I, ++index) {
Reid Spencere1553cc2006-12-31 05:40:12 +00002234 const Type* Ty = I->Ty->get();
Reid Spencer98b3c5c2007-01-02 21:53:43 +00002235 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved(I->Ty))
2236 GEN_ERROR("Reference to abstract argument: " + Ty->getDescription());
Reid Spencere1553cc2006-12-31 05:40:12 +00002237 ParamTypeList.push_back(Ty);
2238 if (Ty != Type::VoidTy)
Reid Spencer4f859aa2007-04-22 05:46:44 +00002239 if (I->Attrs != ParamAttr::None) {
2240 ParamAttrsWithIndex PAWI; PAWI.index = index; PAWI.attrs = I->Attrs;
2241 Attrs.push_back(PAWI);
2242 }
Reid Spencere1553cc2006-12-31 05:40:12 +00002243 }
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002244 }
Chris Lattner00950542001-06-06 20:29:01 +00002245
Chris Lattner2079fde2001-10-13 06:41:08 +00002246 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
2247 if (isVarArg) ParamTypeList.pop_back();
2248
Reid Spencer4f859aa2007-04-22 05:46:44 +00002249 ParamAttrsList *PAL = 0;
2250 if (!Attrs.empty())
2251 PAL = ParamAttrsList::get(Attrs);
Reid Spencer5694b6e2007-04-09 06:17:21 +00002252
Reid Spencer863dd882007-04-28 16:06:50 +00002253 FunctionType *FT = FunctionType::get(*$2, ParamTypeList, isVarArg, PAL);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002254 const PointerType *PFT = PointerType::get(FT);
Reid Spencer2c261782007-01-05 17:06:19 +00002255 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002256
Chris Lattnera09000d2004-07-14 23:03:46 +00002257 ValID ID;
2258 if (!FunctionName.empty()) {
Reid Spencerb2d17862007-01-26 08:04:51 +00002259 ID = ValID::createGlobalName((char*)FunctionName.c_str());
Chris Lattnera09000d2004-07-14 23:03:46 +00002260 } else {
Reid Spencer186a43f2007-03-19 18:39:36 +00002261 ID = ValID::createGlobalID(CurModule.Values.size());
Chris Lattnera09000d2004-07-14 23:03:46 +00002262 }
2263
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00002264 Function *Fn = 0;
Chris Lattnera09000d2004-07-14 23:03:46 +00002265 // See if this function was forward referenced. If so, recycle the object.
Reid Spencer863dd882007-04-28 16:06:50 +00002266 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
Chris Lattnera09000d2004-07-14 23:03:46 +00002267 // Move the function to the end of the list, from whereever it was
2268 // previously inserted.
2269 Fn = cast<Function>(FWRef);
2270 CurModule.CurrentModule->getFunctionList().remove(Fn);
2271 CurModule.CurrentModule->getFunctionList().push_back(Fn);
2272 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
Reid Spenceref9b9a72007-02-05 20:47:22 +00002273 (Fn = CurModule.CurrentModule->getFunction(FunctionName))) {
Reid Spencer863dd882007-04-28 16:06:50 +00002274 if (Fn->getFunctionType() != FT) {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002275 // The existing function doesn't have the same type. This is an overload
2276 // error.
2277 GEN_ERROR("Overload of function '" + FunctionName + "' not permitted.");
2278 } else if (!CurFun.isDeclare && !Fn->isDeclaration()) {
2279 // Neither the existing or the current function is a declaration and they
2280 // have the same name and same type. Clearly this is a redefinition.
Reid Spencerf4fa5902007-02-05 10:16:10 +00002281 GEN_ERROR("Redefinition of function '" + FunctionName + "'");
Reid Spenceref9b9a72007-02-05 20:47:22 +00002282 } if (Fn->isDeclaration()) {
2283 // Make sure to strip off any argument names so we can't get conflicts.
Chris Lattnere4d5c442005-03-15 04:54:21 +00002284 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
Chris Lattnera09000d2004-07-14 23:03:46 +00002285 AI != AE; ++AI)
2286 AI->setName("");
Reid Spenceref9b9a72007-02-05 20:47:22 +00002287 }
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00002288 } else { // Not already defined?
Chris Lattner4989b842007-04-26 05:30:35 +00002289 Fn = new Function(FT, GlobalValue::ExternalWeakLinkage, FunctionName,
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00002290 CurModule.CurrentModule);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002291
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00002292 InsertValue(Fn, CurModule.Values);
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00002293 }
Chris Lattner00950542001-06-06 20:29:01 +00002294
Chris Lattner394f2eb2003-10-16 20:12:13 +00002295 CurFun.FunctionStart(Fn);
Anton Korobeynikov93c2b372006-09-17 13:06:18 +00002296
2297 if (CurFun.isDeclare) {
2298 // If we have declaration, always overwrite linkage. This will allow us to
2299 // correctly handle cases, when pointer to function is passed as argument to
2300 // another function.
2301 Fn->setLinkage(CurFun.Linkage);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002302 Fn->setVisibility(CurFun.Visibility);
Anton Korobeynikov93c2b372006-09-17 13:06:18 +00002303 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00002304 Fn->setCallingConv($1);
Reid Spencer2c261782007-01-05 17:06:19 +00002305 Fn->setAlignment($9);
2306 if ($8) {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002307 Fn->setSection(*$8);
2308 delete $8;
Chris Lattner164c3782005-11-12 00:11:10 +00002309 }
Chris Lattner00950542001-06-06 20:29:01 +00002310
Chris Lattner7e708292002-06-25 16:13:24 +00002311 // Add all of the arguments we parsed to the function...
Chris Lattnera8e8f162005-05-06 20:27:19 +00002312 if ($5) { // Is null if empty...
Chris Lattner69da5cf2002-10-13 20:57:00 +00002313 if (isVarArg) { // Nuke the last entry
Reid Spenceref9b9a72007-02-05 20:47:22 +00002314 assert($5->back().Ty->get() == Type::VoidTy && $5->back().Name == 0 &&
Reid Spencer1755a9a2007-02-05 17:01:20 +00002315 "Not a varargs marker!");
Reid Spencere1553cc2006-12-31 05:40:12 +00002316 delete $5->back().Ty;
Chris Lattnera8e8f162005-05-06 20:27:19 +00002317 $5->pop_back(); // Delete the last entry
Chris Lattner69da5cf2002-10-13 20:57:00 +00002318 }
Chris Lattnere4d5c442005-03-15 04:54:21 +00002319 Function::arg_iterator ArgIt = Fn->arg_begin();
Reid Spenceref9b9a72007-02-05 20:47:22 +00002320 Function::arg_iterator ArgEnd = Fn->arg_end();
Reid Spencere1553cc2006-12-31 05:40:12 +00002321 unsigned Idx = 1;
Reid Spenceref9b9a72007-02-05 20:47:22 +00002322 for (ArgListType::iterator I = $5->begin();
2323 I != $5->end() && ArgIt != ArgEnd; ++I, ++ArgIt) {
Reid Spencere1553cc2006-12-31 05:40:12 +00002324 delete I->Ty; // Delete the typeholder...
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002325 setValueName(ArgIt, I->Name); // Insert arg into symtab...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002326 CHECK_FOR_ERROR
Chris Lattner69da5cf2002-10-13 20:57:00 +00002327 InsertValue(ArgIt);
Reid Spencere1553cc2006-12-31 05:40:12 +00002328 Idx++;
Chris Lattner00950542001-06-06 20:29:01 +00002329 }
Reid Spencer08de34b2006-12-03 05:45:44 +00002330
Chris Lattnera8e8f162005-05-06 20:27:19 +00002331 delete $5; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00002332 }
Reid Spencer61c83e02006-08-18 08:43:06 +00002333 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002334};
Chris Lattner00950542001-06-06 20:29:01 +00002335
Chris Lattner9b02cc32002-05-03 18:23:48 +00002336BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
2337
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002338FunctionHeader : FunctionDefineLinkage GVVisibilityStyle FunctionHeaderH BEGIN {
Chris Lattner394f2eb2003-10-16 20:12:13 +00002339 $$ = CurFun.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00002340
Chris Lattner4ad02e72003-04-16 20:28:45 +00002341 // Make sure that we keep track of the linkage type even if there was a
2342 // previous "declare".
2343 $$->setLinkage($1);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002344 $$->setVisibility($2);
Chris Lattner51727be2002-06-04 21:58:56 +00002345};
Chris Lattner00950542001-06-06 20:29:01 +00002346
Chris Lattner9b02cc32002-05-03 18:23:48 +00002347END : ENDTOK | '}'; // Allow end of '}' to end a function
2348
Chris Lattner79df7c02002-03-26 18:01:55 +00002349Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00002350 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002351 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002352};
Chris Lattner00950542001-06-06 20:29:01 +00002353
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002354FunctionProto : FunctionDeclareLinkage GVVisibilityStyle FunctionHeaderH {
Reid Spencere1553cc2006-12-31 05:40:12 +00002355 CurFun.CurrentFunction->setLinkage($1);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002356 CurFun.CurrentFunction->setVisibility($2);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002357 $$ = CurFun.CurrentFunction;
2358 CurFun.FunctionDone();
2359 CHECK_FOR_ERROR
2360 };
Chris Lattner00950542001-06-06 20:29:01 +00002361
2362//===----------------------------------------------------------------------===//
2363// Rules to match Basic Blocks
2364//===----------------------------------------------------------------------===//
2365
Chris Lattneraa2c8532006-01-25 22:26:43 +00002366OptSideEffect : /* empty */ {
2367 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002368 CHECK_FOR_ERROR
Chris Lattneraa2c8532006-01-25 22:26:43 +00002369 }
2370 | SIDEEFFECT {
2371 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002372 CHECK_FOR_ERROR
Chris Lattneraa2c8532006-01-25 22:26:43 +00002373 };
2374
Chris Lattner00950542001-06-06 20:29:01 +00002375ConstValueRef : ESINT64VAL { // A reference to a direct constant
2376 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002377 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002378 }
2379 | EUINT64VAL {
2380 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002381 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002382 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00002383 | FPVAL { // Perhaps it's an FP constant?
2384 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002385 CHECK_FOR_ERROR
Chris Lattner3d52b2f2001-07-15 00:17:01 +00002386 }
Chris Lattner91ef4602004-03-31 03:49:47 +00002387 | TRUETOK {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00002388 $$ = ValID::create(ConstantInt::getTrue());
Reid Spencer61c83e02006-08-18 08:43:06 +00002389 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002390 }
Chris Lattner91ef4602004-03-31 03:49:47 +00002391 | FALSETOK {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00002392 $$ = ValID::create(ConstantInt::getFalse());
Reid Spencer61c83e02006-08-18 08:43:06 +00002393 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002394 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00002395 | NULL_TOK {
2396 $$ = ValID::createNull();
Reid Spencer61c83e02006-08-18 08:43:06 +00002397 CHECK_FOR_ERROR
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002398 }
Chris Lattner16710e92004-10-16 18:17:13 +00002399 | UNDEF {
2400 $$ = ValID::createUndef();
Reid Spencer61c83e02006-08-18 08:43:06 +00002401 CHECK_FOR_ERROR
Chris Lattner16710e92004-10-16 18:17:13 +00002402 }
Chris Lattnerf1f03df2005-12-21 17:53:02 +00002403 | ZEROINITIALIZER { // A vector zero constant.
2404 $$ = ValID::createZeroInit();
Reid Spencer61c83e02006-08-18 08:43:06 +00002405 CHECK_FOR_ERROR
Chris Lattnerf1f03df2005-12-21 17:53:02 +00002406 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00002407 | '<' ConstVector '>' { // Nonempty unsized packed vector
Reid Spencer08de34b2006-12-03 05:45:44 +00002408 const Type *ETy = (*$2)[0]->getType();
Brian Gaeke715c90b2004-08-20 06:00:58 +00002409 int NumElements = $2->size();
2410
Reid Spencer9d6565a2007-02-15 02:26:10 +00002411 VectorType* pt = VectorType::get(ETy, NumElements);
Brian Gaeke715c90b2004-08-20 06:00:58 +00002412 PATypeHolder* PTy = new PATypeHolder(
Reid Spencer08de34b2006-12-03 05:45:44 +00002413 HandleUpRefs(
Reid Spencer9d6565a2007-02-15 02:26:10 +00002414 VectorType::get(
Reid Spencer08de34b2006-12-03 05:45:44 +00002415 ETy,
2416 NumElements)
2417 )
2418 );
Brian Gaeke715c90b2004-08-20 06:00:58 +00002419
2420 // Verify all elements are correct type!
2421 for (unsigned i = 0; i < $2->size(); i++) {
Reid Spencer08de34b2006-12-03 05:45:44 +00002422 if (ETy != (*$2)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00002423 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Brian Gaeke715c90b2004-08-20 06:00:58 +00002424 ETy->getDescription() +"' as required!\nIt is of type '" +
Reid Spencer08de34b2006-12-03 05:45:44 +00002425 (*$2)[i]->getType()->getDescription() + "'.");
Brian Gaeke715c90b2004-08-20 06:00:58 +00002426 }
2427
Reid Spencer9d6565a2007-02-15 02:26:10 +00002428 $$ = ValID::create(ConstantVector::get(pt, *$2));
Brian Gaeke715c90b2004-08-20 06:00:58 +00002429 delete PTy; delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002430 CHECK_FOR_ERROR
Brian Gaeke715c90b2004-08-20 06:00:58 +00002431 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00002432 | ConstExpr {
Reid Spencer08de34b2006-12-03 05:45:44 +00002433 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002434 CHECK_FOR_ERROR
Chris Lattneraa2c8532006-01-25 22:26:43 +00002435 }
2436 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002437 $$ = ValID::createInlineAsm(*$3, *$5, $2);
2438 delete $3;
2439 delete $5;
Reid Spencer61c83e02006-08-18 08:43:06 +00002440 CHECK_FOR_ERROR
Chris Lattnerd78700d2002-08-16 21:14:40 +00002441 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00002442
Chris Lattner2079fde2001-10-13 06:41:08 +00002443// SymbolicValueRef - Reference to one of two ways of symbolically refering to
2444// another value.
2445//
Reid Spencerb2d17862007-01-26 08:04:51 +00002446SymbolicValueRef : LOCALVAL_ID { // Is it an integer reference...?
2447 $$ = ValID::createLocalID($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002448 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002449 }
Reid Spencerb2d17862007-01-26 08:04:51 +00002450 | GLOBALVAL_ID {
2451 $$ = ValID::createGlobalID($1);
2452 CHECK_FOR_ERROR
2453 }
2454 | LocalName { // Is it a named reference...?
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002455 $$ = ValID::createLocalName(*$1);
2456 delete $1;
Reid Spencerb2d17862007-01-26 08:04:51 +00002457 CHECK_FOR_ERROR
2458 }
2459 | GlobalName { // Is it a named reference...?
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002460 $$ = ValID::createGlobalName(*$1);
2461 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002462 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002463 };
Chris Lattner2079fde2001-10-13 06:41:08 +00002464
2465// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00002466ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00002467
Chris Lattner00950542001-06-06 20:29:01 +00002468
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002469// ResolvedVal - a <type> <value> pair. This is used only in cases where the
2470// type immediately preceeds the value reference, and allows complex constant
2471// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00002472ResolvedVal : Types ValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00002473 if (!UpRefs.empty())
2474 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2475 $$ = getVal(*$1, $2);
2476 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002477 CHECK_FOR_ERROR
Reid Spencere1553cc2006-12-31 05:40:12 +00002478 }
2479 ;
Chris Lattner8b81bf52001-07-25 22:47:46 +00002480
Chris Lattner00950542001-06-06 20:29:01 +00002481BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner8ab406d2004-07-13 07:59:27 +00002482 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002483 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002484 }
Chris Lattner7e708292002-06-25 16:13:24 +00002485 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
Chris Lattner8ab406d2004-07-13 07:59:27 +00002486 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002487 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002488 };
Chris Lattner00950542001-06-06 20:29:01 +00002489
2490
2491// Basic blocks are terminated by branching instructions:
2492// br, br/cc, switch, ret
2493//
Reid Spencerb2d17862007-01-26 08:04:51 +00002494BasicBlock : InstructionList OptLocalAssign BBTerminatorInst {
Chris Lattner8ab406d2004-07-13 07:59:27 +00002495 setValueName($3, $2);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002496 CHECK_FOR_ERROR
Chris Lattner2079fde2001-10-13 06:41:08 +00002497 InsertValue($3);
Chris Lattner2079fde2001-10-13 06:41:08 +00002498 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00002499 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002500 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002501 };
Chris Lattner00950542001-06-06 20:29:01 +00002502
2503InstructionList : InstructionList Inst {
Reid Spencer3da59db2006-11-27 01:05:10 +00002504 if (CastInst *CI1 = dyn_cast<CastInst>($2))
2505 if (CastInst *CI2 = dyn_cast<CastInst>(CI1->getOperand(0)))
2506 if (CI2->getParent() == 0)
2507 $1->getInstList().push_back(CI2);
Chris Lattner00950542001-06-06 20:29:01 +00002508 $1->getInstList().push_back($2);
2509 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002510 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002511 }
Reid Spencer186a43f2007-03-19 18:39:36 +00002512 | /* empty */ { // Empty space between instruction lists
2513 $$ = defineBBVal(ValID::createLocalID(CurFun.NextValNum));
Reid Spencer61c83e02006-08-18 08:43:06 +00002514 CHECK_FOR_ERROR
Chris Lattner22ae2322004-07-13 08:39:15 +00002515 }
Reid Spencer186a43f2007-03-19 18:39:36 +00002516 | LABELSTR { // Labelled (named) basic block
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002517 $$ = defineBBVal(ValID::createLocalName(*$1));
2518 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002519 CHECK_FOR_ERROR
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002520
Chris Lattner51727be2002-06-04 21:58:56 +00002521 };
Chris Lattner00950542001-06-06 20:29:01 +00002522
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002523BBTerminatorInst : RET ResolvedVal { // Return with a result...
Reid Spencer08de34b2006-12-03 05:45:44 +00002524 $$ = new ReturnInst($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00002525 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002526 }
Reid Spencer186a43f2007-03-19 18:39:36 +00002527 | RET VOID { // Return with no result...
Chris Lattner00950542001-06-06 20:29:01 +00002528 $$ = new ReturnInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002529 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002530 }
Reid Spencer186a43f2007-03-19 18:39:36 +00002531 | BR LABEL ValueRef { // Unconditional Branch...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002532 BasicBlock* tmpBB = getBBVal($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002533 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002534 $$ = new BranchInst(tmpBB);
Reid Spencer186a43f2007-03-19 18:39:36 +00002535 } // Conditional Branch...
Reid Spencer8088e9d2007-01-13 05:00:20 +00002536 | BR INTTYPE ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
2537 assert(cast<IntegerType>($2)->getBitWidth() == 1 && "Not Bool?");
Reid Spencer5b7e7532006-09-28 19:28:24 +00002538 BasicBlock* tmpBBA = getBBVal($6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002539 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002540 BasicBlock* tmpBBB = getBBVal($9);
2541 CHECK_FOR_ERROR
Reid Spencer4fe16d62007-01-11 18:21:29 +00002542 Value* tmpVal = getVal(Type::Int1Ty, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002543 CHECK_FOR_ERROR
2544 $$ = new BranchInst(tmpBBA, tmpBBB, tmpVal);
Chris Lattner00950542001-06-06 20:29:01 +00002545 }
2546 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencer08de34b2006-12-03 05:45:44 +00002547 Value* tmpVal = getVal($2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002548 CHECK_FOR_ERROR
2549 BasicBlock* tmpBB = getBBVal($6);
2550 CHECK_FOR_ERROR
2551 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, $8->size());
Chris Lattner00950542001-06-06 20:29:01 +00002552 $$ = S;
2553
Chris Lattner9232b992003-04-22 18:42:41 +00002554 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
Chris Lattner46748042002-04-09 19:41:42 +00002555 E = $8->end();
Chris Lattner69331f52005-02-24 05:25:17 +00002556 for (; I != E; ++I) {
2557 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
2558 S->addCase(CI, I->second);
2559 else
Reid Spencerf4fa5902007-02-05 10:16:10 +00002560 GEN_ERROR("Switch case is constant, but not a simple integer");
Chris Lattner69331f52005-02-24 05:25:17 +00002561 }
Chris Lattnerf57a43d2004-04-17 23:49:15 +00002562 delete $8;
Reid Spencer61c83e02006-08-18 08:43:06 +00002563 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002564 }
Chris Lattner196850c2003-05-15 21:30:00 +00002565 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencer08de34b2006-12-03 05:45:44 +00002566 Value* tmpVal = getVal($2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002567 CHECK_FOR_ERROR
2568 BasicBlock* tmpBB = getBBVal($6);
2569 CHECK_FOR_ERROR
2570 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, 0);
Chris Lattner196850c2003-05-15 21:30:00 +00002571 $$ = S;
Reid Spencer61c83e02006-08-18 08:43:06 +00002572 CHECK_FOR_ERROR
Chris Lattner196850c2003-05-15 21:30:00 +00002573 }
Reid Spencer2c261782007-01-05 17:06:19 +00002574 | INVOKE OptCallingConv ResultTypes ValueRef '(' ValueRefList ')' OptFuncAttrs
Chris Lattnera8e8f162005-05-06 20:27:19 +00002575 TO LABEL ValueRef UNWIND LABEL ValueRef {
Chris Lattner2079fde2001-10-13 06:41:08 +00002576
Reid Spencere1553cc2006-12-31 05:40:12 +00002577 // Handle the short syntax
2578 const PointerType *PFTy = 0;
2579 const FunctionType *Ty = 0;
Reid Spencer2c261782007-01-05 17:06:19 +00002580 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002581 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00002582 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00002583 std::vector<const Type*> ParamTypes;
Reid Spencer863dd882007-04-28 16:06:50 +00002584 ParamAttrsVector Attrs;
2585 if ($8 != ParamAttr::None) {
Chris Lattnerdd3094a2007-05-04 04:01:07 +00002586 ParamAttrsWithIndex PAWI; PAWI.index = 0; PAWI.attrs = $8;
Reid Spencer863dd882007-04-28 16:06:50 +00002587 Attrs.push_back(PAWI);
2588 }
Reid Spencer5694b6e2007-04-09 06:17:21 +00002589 ValueRefList::iterator I = $6->begin(), E = $6->end();
Reid Spencer863dd882007-04-28 16:06:50 +00002590 unsigned index = 1;
2591 for (; I != E; ++I, ++index) {
Reid Spencere1553cc2006-12-31 05:40:12 +00002592 const Type *Ty = I->Val->getType();
2593 if (Ty == Type::VoidTy)
2594 GEN_ERROR("Short call syntax cannot be used with varargs");
2595 ParamTypes.push_back(Ty);
Reid Spencer863dd882007-04-28 16:06:50 +00002596 if (I->Attrs != ParamAttr::None) {
2597 ParamAttrsWithIndex PAWI; PAWI.index = index; PAWI.attrs = I->Attrs;
2598 Attrs.push_back(PAWI);
2599 }
Chris Lattner2079fde2001-10-13 06:41:08 +00002600 }
Reid Spencer863dd882007-04-28 16:06:50 +00002601
2602 ParamAttrsList *PAL = 0;
2603 if (!Attrs.empty())
2604 PAL = ParamAttrsList::get(Attrs);
2605 Ty = FunctionType::get($3->get(), ParamTypes, false, PAL);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002606 PFTy = PointerType::get(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00002607 }
Chris Lattner2079fde2001-10-13 06:41:08 +00002608
Reid Spencer2c9df212007-03-20 01:13:00 +00002609 delete $3;
2610
Chris Lattnera8e8f162005-05-06 20:27:19 +00002611 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002612 CHECK_FOR_ERROR
Reid Spencer2c261782007-01-05 17:06:19 +00002613 BasicBlock *Normal = getBBVal($11);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002614 CHECK_FOR_ERROR
Reid Spencer2c261782007-01-05 17:06:19 +00002615 BasicBlock *Except = getBBVal($14);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002616 CHECK_FOR_ERROR
Chris Lattner2079fde2001-10-13 06:41:08 +00002617
Reid Spencere1553cc2006-12-31 05:40:12 +00002618 // Check the arguments
2619 ValueList Args;
2620 if ($6->empty()) { // Has no arguments?
2621 // Make sure no arguments is a good thing!
2622 if (Ty->getNumParams() != 0)
2623 GEN_ERROR("No arguments passed to a function that "
Reid Spencerf4fa5902007-02-05 10:16:10 +00002624 "expects arguments");
Chris Lattner2079fde2001-10-13 06:41:08 +00002625 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00002626 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00002627 // correctly!
Chris Lattnerd5d89962004-02-09 04:14:01 +00002628 FunctionType::param_iterator I = Ty->param_begin();
2629 FunctionType::param_iterator E = Ty->param_end();
Reid Spencere1553cc2006-12-31 05:40:12 +00002630 ValueRefList::iterator ArgI = $6->begin(), ArgE = $6->end();
Reid Spencer863dd882007-04-28 16:06:50 +00002631
Reid Spencere1553cc2006-12-31 05:40:12 +00002632 for (; ArgI != ArgE && I != E; ++ArgI, ++I) {
2633 if (ArgI->Val->getType() != *I)
2634 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00002635 (*I)->getDescription() + "'");
Reid Spencere1553cc2006-12-31 05:40:12 +00002636 Args.push_back(ArgI->Val);
2637 }
Reid Spencer08de34b2006-12-03 05:45:44 +00002638
Reid Spencere1553cc2006-12-31 05:40:12 +00002639 if (Ty->isVarArg()) {
2640 if (I == E)
2641 for (; ArgI != ArgE; ++ArgI)
2642 Args.push_back(ArgI->Val); // push the remaining varargs
2643 } else if (I != E || ArgI != ArgE)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002644 GEN_ERROR("Invalid number of parameters detected");
Chris Lattner2079fde2001-10-13 06:41:08 +00002645 }
Reid Spencere1553cc2006-12-31 05:40:12 +00002646
2647 // Create the InvokeInst
Chris Lattner9d2fda62007-02-13 05:53:56 +00002648 InvokeInst *II = new InvokeInst(V, Normal, Except, &Args[0], Args.size());
Reid Spencere1553cc2006-12-31 05:40:12 +00002649 II->setCallingConv($2);
2650 $$ = II;
Chris Lattnera8e8f162005-05-06 20:27:19 +00002651 delete $6;
Reid Spencer61c83e02006-08-18 08:43:06 +00002652 CHECK_FOR_ERROR
Chris Lattner36143fc2003-09-08 18:54:55 +00002653 }
2654 | UNWIND {
2655 $$ = new UnwindInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002656 CHECK_FOR_ERROR
Chris Lattner16710e92004-10-16 18:17:13 +00002657 }
2658 | UNREACHABLE {
2659 $$ = new UnreachableInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002660 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002661 };
Chris Lattner2079fde2001-10-13 06:41:08 +00002662
2663
Chris Lattner00950542001-06-06 20:29:01 +00002664
2665JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
2666 $$ = $1;
Reid Spencer186a43f2007-03-19 18:39:36 +00002667 Constant *V = cast<Constant>(getExistingVal($2, $3));
Reid Spencer5b7e7532006-09-28 19:28:24 +00002668 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002669 if (V == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002670 GEN_ERROR("May only switch on a constant pool value");
Chris Lattner00950542001-06-06 20:29:01 +00002671
Reid Spencer5b7e7532006-09-28 19:28:24 +00002672 BasicBlock* tmpBB = getBBVal($6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002673 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002674 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner00950542001-06-06 20:29:01 +00002675 }
2676 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner9232b992003-04-22 18:42:41 +00002677 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Reid Spencer186a43f2007-03-19 18:39:36 +00002678 Constant *V = cast<Constant>(getExistingVal($1, $2));
Reid Spencer5b7e7532006-09-28 19:28:24 +00002679 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002680
2681 if (V == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002682 GEN_ERROR("May only switch on a constant pool value");
Chris Lattner00950542001-06-06 20:29:01 +00002683
Reid Spencer5b7e7532006-09-28 19:28:24 +00002684 BasicBlock* tmpBB = getBBVal($5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002685 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002686 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner51727be2002-06-04 21:58:56 +00002687 };
Chris Lattner00950542001-06-06 20:29:01 +00002688
Reid Spencerb2d17862007-01-26 08:04:51 +00002689Inst : OptLocalAssign InstVal {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002690 // Is this definition named?? if so, assign the name...
2691 setValueName($2, $1);
2692 CHECK_FOR_ERROR
2693 InsertValue($2);
2694 $$ = $2;
2695 CHECK_FOR_ERROR
2696 };
2697
Chris Lattner00950542001-06-06 20:29:01 +00002698
Chris Lattnerc24d2082001-06-11 15:04:20 +00002699PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Reid Spencere1553cc2006-12-31 05:40:12 +00002700 if (!UpRefs.empty())
2701 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Chris Lattner9232b992003-04-22 18:42:41 +00002702 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Reid Spencer08de34b2006-12-03 05:45:44 +00002703 Value* tmpVal = getVal(*$1, $3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002704 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002705 BasicBlock* tmpBB = getBBVal($5);
2706 CHECK_FOR_ERROR
2707 $$->push_back(std::make_pair(tmpVal, tmpBB));
Reid Spencer08de34b2006-12-03 05:45:44 +00002708 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00002709 }
2710 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
2711 $$ = $1;
Reid Spencer5b7e7532006-09-28 19:28:24 +00002712 Value* tmpVal = getVal($1->front().first->getType(), $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00002713 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002714 BasicBlock* tmpBB = getBBVal($6);
2715 CHECK_FOR_ERROR
2716 $1->push_back(std::make_pair(tmpVal, tmpBB));
Chris Lattner51727be2002-06-04 21:58:56 +00002717 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00002718
2719
Reid Spencere1553cc2006-12-31 05:40:12 +00002720ValueRefList : Types ValueRef OptParamAttrs {
2721 if (!UpRefs.empty())
2722 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2723 // Used for call and invoke instructions
2724 $$ = new ValueRefList();
2725 ValueRefListEntry E; E.Attrs = $3; E.Val = getVal($1->get(), $2);
2726 $$->push_back(E);
Reid Spencer2c9df212007-03-20 01:13:00 +00002727 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00002728 }
Reid Spencere1553cc2006-12-31 05:40:12 +00002729 | ValueRefList ',' Types ValueRef OptParamAttrs {
2730 if (!UpRefs.empty())
2731 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattner00950542001-06-06 20:29:01 +00002732 $$ = $1;
Reid Spencere1553cc2006-12-31 05:40:12 +00002733 ValueRefListEntry E; E.Attrs = $5; E.Val = getVal($3->get(), $4);
2734 $$->push_back(E);
Reid Spencer2c9df212007-03-20 01:13:00 +00002735 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002736 CHECK_FOR_ERROR
Reid Spencere1553cc2006-12-31 05:40:12 +00002737 }
2738 | /*empty*/ { $$ = new ValueRefList(); };
Chris Lattner00950542001-06-06 20:29:01 +00002739
Reid Spencere1553cc2006-12-31 05:40:12 +00002740IndexList // Used for gep instructions and constant expressions
Reid Spencere03969f2006-12-31 21:46:36 +00002741 : /*empty*/ { $$ = new std::vector<Value*>(); }
Reid Spencere1553cc2006-12-31 05:40:12 +00002742 | IndexList ',' ResolvedVal {
2743 $$ = $1;
2744 $$->push_back($3);
2745 CHECK_FOR_ERROR
2746 }
Reid Spencer71305d72006-12-31 21:25:25 +00002747 ;
Chris Lattner00950542001-06-06 20:29:01 +00002748
Chris Lattnerddb6db42005-05-06 05:51:46 +00002749OptTailCall : TAIL CALL {
2750 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002751 CHECK_FOR_ERROR
Chris Lattnerddb6db42005-05-06 05:51:46 +00002752 }
2753 | CALL {
2754 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002755 CHECK_FOR_ERROR
Chris Lattnerddb6db42005-05-06 05:51:46 +00002756 };
2757
Chris Lattner4a6482b2002-09-10 19:57:26 +00002758InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00002759 if (!UpRefs.empty())
2760 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Chris Lattner42a75512007-01-15 02:27:26 +00002761 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint() &&
Reid Spencer9d6565a2007-02-15 02:26:10 +00002762 !isa<VectorType>((*$2).get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002763 GEN_ERROR(
Reid Spencerf4fa5902007-02-05 10:16:10 +00002764 "Arithmetic operator requires integer, FP, or packed operands");
Reid Spencer9d6565a2007-02-15 02:26:10 +00002765 if (isa<VectorType>((*$2).get()) &&
Reid Spencer08de34b2006-12-03 05:45:44 +00002766 ($1 == Instruction::URem ||
2767 $1 == Instruction::SRem ||
2768 $1 == Instruction::FRem))
Reid Spencerac9dcb92007-02-15 03:39:18 +00002769 GEN_ERROR("Remainder not supported on vector types");
Reid Spencer08de34b2006-12-03 05:45:44 +00002770 Value* val1 = getVal(*$2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002771 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00002772 Value* val2 = getVal(*$2, $5);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002773 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00002774 $$ = BinaryOperator::create($1, val1, val2);
Chris Lattner4a6482b2002-09-10 19:57:26 +00002775 if ($$ == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002776 GEN_ERROR("binary operator returned null");
Reid Spencer08de34b2006-12-03 05:45:44 +00002777 delete $2;
Chris Lattner4a6482b2002-09-10 19:57:26 +00002778 }
2779 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00002780 if (!UpRefs.empty())
2781 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Chris Lattner42a75512007-01-15 02:27:26 +00002782 if (!(*$2)->isInteger()) {
Reid Spencer9d6565a2007-02-15 02:26:10 +00002783 if (Instruction::isShift($1) || !isa<VectorType>($2->get()) ||
2784 !cast<VectorType>($2->get())->getElementType()->isInteger())
Reid Spencerf4fa5902007-02-05 10:16:10 +00002785 GEN_ERROR("Logical operator requires integral operands");
Chris Lattner0a017832005-12-21 18:31:29 +00002786 }
Reid Spencer08de34b2006-12-03 05:45:44 +00002787 Value* tmpVal1 = getVal(*$2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002788 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00002789 Value* tmpVal2 = getVal(*$2, $5);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002790 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00002791 $$ = BinaryOperator::create($1, tmpVal1, tmpVal2);
Chris Lattner4a6482b2002-09-10 19:57:26 +00002792 if ($$ == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002793 GEN_ERROR("binary operator returned null");
Reid Spencer08de34b2006-12-03 05:45:44 +00002794 delete $2;
Chris Lattner4a6482b2002-09-10 19:57:26 +00002795 }
Reid Spencer08de34b2006-12-03 05:45:44 +00002796 | ICMP IPredicates Types ValueRef ',' ValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00002797 if (!UpRefs.empty())
2798 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencer9d6565a2007-02-15 02:26:10 +00002799 if (isa<VectorType>((*$3).get()))
Reid Spencerac9dcb92007-02-15 03:39:18 +00002800 GEN_ERROR("Vector types not supported by icmp instruction");
Reid Spencer08de34b2006-12-03 05:45:44 +00002801 Value* tmpVal1 = getVal(*$3, $4);
2802 CHECK_FOR_ERROR
2803 Value* tmpVal2 = getVal(*$3, $6);
2804 CHECK_FOR_ERROR
2805 $$ = CmpInst::create($1, $2, tmpVal1, tmpVal2);
2806 if ($$ == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002807 GEN_ERROR("icmp operator returned null");
Reid Spencer2c9df212007-03-20 01:13:00 +00002808 delete $3;
Reid Spencer08de34b2006-12-03 05:45:44 +00002809 }
2810 | FCMP FPredicates Types ValueRef ',' ValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00002811 if (!UpRefs.empty())
2812 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencer9d6565a2007-02-15 02:26:10 +00002813 if (isa<VectorType>((*$3).get()))
Reid Spencerac9dcb92007-02-15 03:39:18 +00002814 GEN_ERROR("Vector types not supported by fcmp instruction");
Reid Spencer08de34b2006-12-03 05:45:44 +00002815 Value* tmpVal1 = getVal(*$3, $4);
2816 CHECK_FOR_ERROR
2817 Value* tmpVal2 = getVal(*$3, $6);
2818 CHECK_FOR_ERROR
2819 $$ = CmpInst::create($1, $2, tmpVal1, tmpVal2);
2820 if ($$ == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002821 GEN_ERROR("fcmp operator returned null");
Reid Spencer2c9df212007-03-20 01:13:00 +00002822 delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00002823 }
Reid Spencer3da59db2006-11-27 01:05:10 +00002824 | CastOps ResolvedVal TO Types {
Reid Spencere1553cc2006-12-31 05:40:12 +00002825 if (!UpRefs.empty())
2826 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00002827 Value* Val = $2;
Reid Spencer93947c32007-01-17 02:47:33 +00002828 const Type* DestTy = $4->get();
2829 if (!CastInst::castIsValid($1, Val, DestTy))
2830 GEN_ERROR("invalid cast opcode for cast from '" +
2831 Val->getType()->getDescription() + "' to '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00002832 DestTy->getDescription() + "'");
Reid Spencer93947c32007-01-17 02:47:33 +00002833 $$ = CastInst::create($1, Val, DestTy);
Reid Spencer08de34b2006-12-03 05:45:44 +00002834 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00002835 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00002836 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer4fe16d62007-01-11 18:21:29 +00002837 if ($2->getType() != Type::Int1Ty)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002838 GEN_ERROR("select condition must be boolean");
Reid Spencer08de34b2006-12-03 05:45:44 +00002839 if ($4->getType() != $6->getType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00002840 GEN_ERROR("select value types should match");
Reid Spencer08de34b2006-12-03 05:45:44 +00002841 $$ = new SelectInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002842 CHECK_FOR_ERROR
Chris Lattner76f0ff32004-03-12 05:51:36 +00002843 }
Chris Lattner99e7ab72003-10-18 05:53:13 +00002844 | VAARG ResolvedVal ',' Types {
Reid Spencere1553cc2006-12-31 05:40:12 +00002845 if (!UpRefs.empty())
2846 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00002847 $$ = new VAArgInst($2, *$4);
2848 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00002849 CHECK_FOR_ERROR
Chris Lattner99e7ab72003-10-18 05:53:13 +00002850 }
Robert Bocchino9c62b562006-01-10 19:04:32 +00002851 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencer08de34b2006-12-03 05:45:44 +00002852 if (!ExtractElementInst::isValidOperands($2, $4))
Reid Spencerf4fa5902007-02-05 10:16:10 +00002853 GEN_ERROR("Invalid extractelement operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00002854 $$ = new ExtractElementInst($2, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00002855 CHECK_FOR_ERROR
Robert Bocchino9c62b562006-01-10 19:04:32 +00002856 }
Robert Bocchino2def1b32006-01-17 20:06:25 +00002857 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer08de34b2006-12-03 05:45:44 +00002858 if (!InsertElementInst::isValidOperands($2, $4, $6))
Reid Spencerf4fa5902007-02-05 10:16:10 +00002859 GEN_ERROR("Invalid insertelement operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00002860 $$ = new InsertElementInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002861 CHECK_FOR_ERROR
Robert Bocchino2def1b32006-01-17 20:06:25 +00002862 }
Chris Lattner4c949082006-04-08 01:18:35 +00002863 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer08de34b2006-12-03 05:45:44 +00002864 if (!ShuffleVectorInst::isValidOperands($2, $4, $6))
Reid Spencerf4fa5902007-02-05 10:16:10 +00002865 GEN_ERROR("Invalid shufflevector operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00002866 $$ = new ShuffleVectorInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002867 CHECK_FOR_ERROR
Chris Lattner4c949082006-04-08 01:18:35 +00002868 }
Chris Lattner3b237fc2003-10-19 21:34:28 +00002869 | PHI_TOK PHIList {
Chris Lattnerc24d2082001-06-11 15:04:20 +00002870 const Type *Ty = $2->front().first->getType();
Chris Lattner8ea8f362003-10-30 01:38:18 +00002871 if (!Ty->isFirstClassType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00002872 GEN_ERROR("PHI node operands must be of first class type");
Chris Lattnerc24d2082001-06-11 15:04:20 +00002873 $$ = new PHINode(Ty);
Chris Lattnerce1df9e2005-01-29 00:35:55 +00002874 ((PHINode*)$$)->reserveOperandSpace($2->size());
Chris Lattner00950542001-06-06 20:29:01 +00002875 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00002876 if ($2->front().first->getType() != Ty)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002877 GEN_ERROR("All elements of a PHI node must be of the same type");
Chris Lattnerb00c5822001-10-02 03:41:24 +00002878 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00002879 $2->pop_front();
2880 }
2881 delete $2; // Free the list...
Reid Spencer61c83e02006-08-18 08:43:06 +00002882 CHECK_FOR_ERROR
Chris Lattnerddb6db42005-05-06 05:51:46 +00002883 }
Reid Spencer2c261782007-01-05 17:06:19 +00002884 | OptTailCall OptCallingConv ResultTypes ValueRef '(' ValueRefList ')'
2885 OptFuncAttrs {
Reid Spencere1553cc2006-12-31 05:40:12 +00002886
2887 // Handle the short syntax
Bill Wendlingb39a55a2006-11-12 11:10:39 +00002888 const PointerType *PFTy = 0;
2889 const FunctionType *Ty = 0;
Reid Spencer2c261782007-01-05 17:06:19 +00002890 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002891 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00002892 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00002893 std::vector<const Type*> ParamTypes;
Reid Spencer863dd882007-04-28 16:06:50 +00002894 ParamAttrsVector Attrs;
2895 if ($8 != ParamAttr::None) {
2896 ParamAttrsWithIndex PAWI; PAWI.index = 0; PAWI.attrs = $8;
2897 Attrs.push_back(PAWI);
2898 }
2899 unsigned index = 1;
Reid Spencer5694b6e2007-04-09 06:17:21 +00002900 ValueRefList::iterator I = $6->begin(), E = $6->end();
Reid Spencer863dd882007-04-28 16:06:50 +00002901 for (; I != E; ++I, ++index) {
Reid Spencere1553cc2006-12-31 05:40:12 +00002902 const Type *Ty = I->Val->getType();
2903 if (Ty == Type::VoidTy)
2904 GEN_ERROR("Short call syntax cannot be used with varargs");
2905 ParamTypes.push_back(Ty);
Reid Spencer863dd882007-04-28 16:06:50 +00002906 if (I->Attrs != ParamAttr::None) {
2907 ParamAttrsWithIndex PAWI; PAWI.index = index; PAWI.attrs = I->Attrs;
2908 Attrs.push_back(PAWI);
2909 }
Chris Lattneref9c23f2001-10-03 14:53:21 +00002910 }
Reid Spencer863dd882007-04-28 16:06:50 +00002911
2912 ParamAttrsList *PAL = 0;
2913 if (!Attrs.empty())
2914 PAL = ParamAttrsList::get(Attrs);
2915
2916 Ty = FunctionType::get($3->get(), ParamTypes, false, PAL);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002917 PFTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00002918 }
Chris Lattner00950542001-06-06 20:29:01 +00002919
Chris Lattnera8e8f162005-05-06 20:27:19 +00002920 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002921 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002922
Reid Spencerebff55c2007-04-16 06:55:42 +00002923 // Check for call to invalid intrinsic to avoid crashing later.
2924 if (Function *theF = dyn_cast<Function>(V)) {
Reid Spencerce1e8ef2007-04-16 22:01:57 +00002925 if (theF->hasName() && (theF->getValueName()->getKeyLength() >= 5) &&
Reid Spencer43e60732007-04-16 20:31:06 +00002926 (0 == strncmp(theF->getValueName()->getKeyData(), "llvm.", 5)) &&
2927 !theF->getIntrinsicID(true))
Reid Spencerebff55c2007-04-16 06:55:42 +00002928 GEN_ERROR("Call to invalid LLVM intrinsic function '" +
2929 theF->getName() + "'");
2930 }
2931
Reid Spencere1553cc2006-12-31 05:40:12 +00002932 // Check the arguments
2933 ValueList Args;
2934 if ($6->empty()) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00002935 // Make sure no arguments is a good thing!
2936 if (Ty->getNumParams() != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002937 GEN_ERROR("No arguments passed to a function that "
Reid Spencerf4fa5902007-02-05 10:16:10 +00002938 "expects arguments");
Chris Lattner8b81bf52001-07-25 22:47:46 +00002939 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00002940 // Loop through FunctionType's arguments and ensure they are specified
Reid Spencer863dd882007-04-28 16:06:50 +00002941 // correctly!
2942 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00002943 FunctionType::param_iterator I = Ty->param_begin();
2944 FunctionType::param_iterator E = Ty->param_end();
Reid Spencere1553cc2006-12-31 05:40:12 +00002945 ValueRefList::iterator ArgI = $6->begin(), ArgE = $6->end();
Reid Spencer863dd882007-04-28 16:06:50 +00002946
Reid Spencere1553cc2006-12-31 05:40:12 +00002947 for (; ArgI != ArgE && I != E; ++ArgI, ++I) {
2948 if (ArgI->Val->getType() != *I)
2949 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00002950 (*I)->getDescription() + "'");
Reid Spencere1553cc2006-12-31 05:40:12 +00002951 Args.push_back(ArgI->Val);
2952 }
2953 if (Ty->isVarArg()) {
2954 if (I == E)
2955 for (; ArgI != ArgE; ++ArgI)
2956 Args.push_back(ArgI->Val); // push the remaining varargs
2957 } else if (I != E || ArgI != ArgE)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002958 GEN_ERROR("Invalid number of parameters detected");
Chris Lattner8b81bf52001-07-25 22:47:46 +00002959 }
Reid Spencere1553cc2006-12-31 05:40:12 +00002960 // Create the call node
David Greene52eec542007-08-01 03:43:44 +00002961 CallInst *CI = new CallInst(V, Args.begin(), Args.end());
Reid Spencere1553cc2006-12-31 05:40:12 +00002962 CI->setTailCall($1);
2963 CI->setCallingConv($2);
2964 $$ = CI;
Chris Lattnera8e8f162005-05-06 20:27:19 +00002965 delete $6;
Reid Spencerb2d17862007-01-26 08:04:51 +00002966 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002967 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002968 }
2969 | MemoryInst {
2970 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002971 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002972 };
Chris Lattner00950542001-06-06 20:29:01 +00002973
Chris Lattner15c9c032003-09-08 18:20:29 +00002974OptVolatile : VOLATILE {
2975 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002976 CHECK_FOR_ERROR
Chris Lattner15c9c032003-09-08 18:20:29 +00002977 }
2978 | /* empty */ {
2979 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002980 CHECK_FOR_ERROR
Chris Lattner15c9c032003-09-08 18:20:29 +00002981 };
2982
Chris Lattner027dcc52001-07-08 21:10:27 +00002983
Chris Lattnerddb6db42005-05-06 05:51:46 +00002984
Chris Lattner66db8e42005-11-06 06:34:12 +00002985MemoryInst : MALLOC Types OptCAlign {
Reid Spencere1553cc2006-12-31 05:40:12 +00002986 if (!UpRefs.empty())
2987 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00002988 $$ = new MallocInst(*$2, 0, $3);
2989 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002990 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002991 }
Reid Spencera54b7cb2007-01-12 07:05:14 +00002992 | MALLOC Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencere1553cc2006-12-31 05:40:12 +00002993 if (!UpRefs.empty())
2994 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00002995 Value* tmpVal = getVal($4, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002996 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00002997 $$ = new MallocInst(*$2, tmpVal, $6);
2998 delete $2;
Nate Begeman14b05292005-11-05 09:21:28 +00002999 }
Chris Lattner66db8e42005-11-06 06:34:12 +00003000 | ALLOCA Types OptCAlign {
Reid Spencere1553cc2006-12-31 05:40:12 +00003001 if (!UpRefs.empty())
3002 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00003003 $$ = new AllocaInst(*$2, 0, $3);
3004 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00003005 CHECK_FOR_ERROR
Nate Begeman14b05292005-11-05 09:21:28 +00003006 }
Reid Spencera54b7cb2007-01-12 07:05:14 +00003007 | ALLOCA Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencere1553cc2006-12-31 05:40:12 +00003008 if (!UpRefs.empty())
3009 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00003010 Value* tmpVal = getVal($4, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00003011 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00003012 $$ = new AllocaInst(*$2, tmpVal, $6);
3013 delete $2;
Nate Begeman14b05292005-11-05 09:21:28 +00003014 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00003015 | FREE ResolvedVal {
Reid Spencer08de34b2006-12-03 05:45:44 +00003016 if (!isa<PointerType>($2->getType()))
Reid Spencer61c83e02006-08-18 08:43:06 +00003017 GEN_ERROR("Trying to free nonpointer type " +
Reid Spencerf4fa5902007-02-05 10:16:10 +00003018 $2->getType()->getDescription() + "");
Reid Spencer08de34b2006-12-03 05:45:44 +00003019 $$ = new FreeInst($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00003020 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00003021 }
3022
Christopher Lamb43c7f372007-04-22 19:24:39 +00003023 | OptVolatile LOAD Types ValueRef OptCAlign {
Reid Spencere1553cc2006-12-31 05:40:12 +00003024 if (!UpRefs.empty())
3025 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00003026 if (!isa<PointerType>($3->get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00003027 GEN_ERROR("Can't load from nonpointer type: " +
Reid Spencer08de34b2006-12-03 05:45:44 +00003028 (*$3)->getDescription());
3029 if (!cast<PointerType>($3->get())->getElementType()->isFirstClassType())
Reid Spencer61c83e02006-08-18 08:43:06 +00003030 GEN_ERROR("Can't load from pointer of non-first-class type: " +
Reid Spencer08de34b2006-12-03 05:45:44 +00003031 (*$3)->getDescription());
3032 Value* tmpVal = getVal(*$3, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00003033 CHECK_FOR_ERROR
Christopher Lamb43c7f372007-04-22 19:24:39 +00003034 $$ = new LoadInst(tmpVal, "", $1, $5);
Reid Spencer08de34b2006-12-03 05:45:44 +00003035 delete $3;
Chris Lattner027dcc52001-07-08 21:10:27 +00003036 }
Christopher Lamb43c7f372007-04-22 19:24:39 +00003037 | OptVolatile STORE ResolvedVal ',' Types ValueRef OptCAlign {
Reid Spencere1553cc2006-12-31 05:40:12 +00003038 if (!UpRefs.empty())
3039 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00003040 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner8c8418d2003-09-01 16:31:28 +00003041 if (!PT)
Reid Spencer61c83e02006-08-18 08:43:06 +00003042 GEN_ERROR("Can't store to a nonpointer type: " +
Reid Spencer08de34b2006-12-03 05:45:44 +00003043 (*$5)->getDescription());
Chris Lattner8c8418d2003-09-01 16:31:28 +00003044 const Type *ElTy = PT->getElementType();
Reid Spencer08de34b2006-12-03 05:45:44 +00003045 if (ElTy != $3->getType())
3046 GEN_ERROR("Can't store '" + $3->getType()->getDescription() +
Reid Spencerf4fa5902007-02-05 10:16:10 +00003047 "' into space of type '" + ElTy->getDescription() + "'");
Chris Lattner0383cc42002-08-21 23:51:21 +00003048
Reid Spencer08de34b2006-12-03 05:45:44 +00003049 Value* tmpVal = getVal(*$5, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00003050 CHECK_FOR_ERROR
Christopher Lamb43c7f372007-04-22 19:24:39 +00003051 $$ = new StoreInst($3, tmpVal, $1, $7);
Reid Spencer08de34b2006-12-03 05:45:44 +00003052 delete $5;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00003053 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00003054 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencere1553cc2006-12-31 05:40:12 +00003055 if (!UpRefs.empty())
3056 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00003057 if (!isa<PointerType>($2->get()))
Reid Spencerf4fa5902007-02-05 10:16:10 +00003058 GEN_ERROR("getelementptr insn requires pointer operand");
Chris Lattner830b6f92004-04-05 01:30:04 +00003059
Chris Lattnerc856c7a2007-02-13 00:57:40 +00003060 if (!GetElementPtrInst::getIndexedType(*$2, &(*$4)[0], $4->size(), true))
Reid Spencer61c83e02006-08-18 08:43:06 +00003061 GEN_ERROR("Invalid getelementptr indices for type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00003062 (*$2)->getDescription()+ "'");
Reid Spencer08de34b2006-12-03 05:45:44 +00003063 Value* tmpVal = getVal(*$2, $3);
Reid Spencer61c83e02006-08-18 08:43:06 +00003064 CHECK_FOR_ERROR
Chris Lattnerc856c7a2007-02-13 00:57:40 +00003065 $$ = new GetElementPtrInst(tmpVal, &(*$4)[0], $4->size());
Reid Spencer08de34b2006-12-03 05:45:44 +00003066 delete $2;
Reid Spencer5b7e7532006-09-28 19:28:24 +00003067 delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00003068 };
Chris Lattner027dcc52001-07-08 21:10:27 +00003069
Brian Gaeked0fde302003-11-11 22:41:34 +00003070
Chris Lattner00950542001-06-06 20:29:01 +00003071%%
Reid Spencer61c83e02006-08-18 08:43:06 +00003072
Reid Spencere1553cc2006-12-31 05:40:12 +00003073// common code from the two 'RunVMAsmParser' functions
3074static Module* RunParser(Module * M) {
3075
3076 llvmAsmlineno = 1; // Reset the current line number...
3077 CurModule.CurrentModule = M;
3078#if YYDEBUG
3079 yydebug = Debug;
3080#endif
3081
3082 // Check to make sure the parser succeeded
3083 if (yyparse()) {
3084 if (ParserResult)
3085 delete ParserResult;
3086 return 0;
3087 }
3088
Reid Spencercd5bd902007-03-30 01:37:13 +00003089 // Emit an error if there are any unresolved types left.
3090 if (!CurModule.LateResolveTypes.empty()) {
3091 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
3092 if (DID.Type == ValID::LocalName) {
3093 GenerateError("Undefined type remains at eof: '"+DID.getName() + "'");
3094 } else {
3095 GenerateError("Undefined type remains at eof: #" + itostr(DID.Num));
3096 }
3097 if (ParserResult)
3098 delete ParserResult;
3099 return 0;
3100 }
3101
3102 // Emit an error if there are any unresolved values left.
3103 if (!CurModule.LateResolveValues.empty()) {
3104 Value *V = CurModule.LateResolveValues.back();
3105 std::map<Value*, std::pair<ValID, int> >::iterator I =
3106 CurModule.PlaceHolderInfo.find(V);
3107
3108 if (I != CurModule.PlaceHolderInfo.end()) {
3109 ValID &DID = I->second.first;
3110 if (DID.Type == ValID::LocalName) {
3111 GenerateError("Undefined value remains at eof: "+DID.getName() + "'");
3112 } else {
3113 GenerateError("Undefined value remains at eof: #" + itostr(DID.Num));
3114 }
3115 if (ParserResult)
3116 delete ParserResult;
3117 return 0;
3118 }
3119 }
3120
Reid Spencere1553cc2006-12-31 05:40:12 +00003121 // Check to make sure that parsing produced a result
3122 if (!ParserResult)
3123 return 0;
3124
3125 // Reset ParserResult variable while saving its value for the result.
3126 Module *Result = ParserResult;
3127 ParserResult = 0;
3128
3129 return Result;
3130}
3131
Reid Spencer61c83e02006-08-18 08:43:06 +00003132void llvm::GenerateError(const std::string &message, int LineNo) {
3133 if (LineNo == -1) LineNo = llvmAsmlineno;
3134 // TODO: column number in exception
3135 if (TheParseError)
3136 TheParseError->setError(CurFilename, message, LineNo);
3137 TriggerError = 1;
3138}
3139
Chris Lattner09083092001-07-08 04:57:15 +00003140int yyerror(const char *ErrorMsg) {
Chris Lattner9232b992003-04-22 18:42:41 +00003141 std::string where
3142 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00003143 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Reid Spenceref9b9a72007-02-05 20:47:22 +00003144 std::string errMsg = where + "error: " + std::string(ErrorMsg);
3145 if (yychar != YYEMPTY && yychar != 0)
3146 errMsg += " while reading token: '" + std::string(llvmAsmtext, llvmAsmleng)+
3147 "'";
Reid Spencer61c83e02006-08-18 08:43:06 +00003148 GenerateError(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00003149 return 0;
3150}