blob: cd3a7edb502ef4156a38511613912652404db5d3 [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"
Chandler Carruth69940402007-08-04 01:51:18 +000021#include "llvm/AutoUpgrade.h"
Chris Lattner830b6f92004-04-05 01:30:04 +000022#include "llvm/Support/GetElementPtrTypeIterator.h"
Reid Spencere1553cc2006-12-31 05:40:12 +000023#include "llvm/Support/CommandLine.h"
Chris Lattnere0135402007-01-31 04:43:46 +000024#include "llvm/ADT/SmallVector.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000025#include "llvm/ADT/STLExtras.h"
Chris Lattner87ac9722005-11-06 06:46:28 +000026#include "llvm/Support/MathExtras.h"
Bill Wendling3e3bcbf2006-11-28 22:47:12 +000027#include "llvm/Support/Streams.h"
Reid Spencer8ce1da72004-07-04 12:19:05 +000028#include <algorithm>
Chris Lattner00950542001-06-06 20:29:01 +000029#include <list>
Chris Lattnerc02659f2007-02-11 21:39:35 +000030#include <map>
Chris Lattnerc188eeb2002-07-30 18:54:25 +000031#include <utility>
Chris Lattner00950542001-06-06 20:29:01 +000032
Reid Spencere4f47592006-08-18 17:32:55 +000033// The following is a gross hack. In order to rid the libAsmParser library of
34// exceptions, we have to have a way of getting the yyparse function to go into
35// an error situation. So, whenever we want an error to occur, the GenerateError
36// function (see bottom of file) sets TriggerError. Then, at the end of each
37// production in the grammer we use CHECK_FOR_ERROR which will invoke YYERROR
38// (a goto) to put YACC in error state. Furthermore, several calls to
39// GenerateError are made from inside productions and they must simulate the
40// previous exception behavior by exiting the production immediately. We have
41// replaced these with the GEN_ERROR macro which calls GeneratError and then
42// immediately invokes YYERROR. This would be so much cleaner if it was a
43// recursive descent parser.
Reid Spencer61c83e02006-08-18 08:43:06 +000044static bool TriggerError = false;
Reid Spencerf63697d2006-10-09 17:36:59 +000045#define CHECK_FOR_ERROR { if (TriggerError) { TriggerError = false; YYABORT; } }
Reid Spencer61c83e02006-08-18 08:43:06 +000046#define GEN_ERROR(msg) { GenerateError(msg); YYERROR; }
47
Chris Lattner386a3b72001-10-16 19:54:17 +000048int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
Chris Lattner09083092001-07-08 04:57:15 +000049int yylex(); // declaration" of xxx warnings.
Chris Lattner00950542001-06-06 20:29:01 +000050int yyparse();
Chris Lattner86427632004-07-14 06:39:48 +000051using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000052
Chris Lattner00950542001-06-06 20:29:01 +000053static Module *ParserResult;
Chris Lattner00950542001-06-06 20:29:01 +000054
Chris Lattner30c89792001-09-07 16:35:17 +000055// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
56// relating to upreferences in the input stream.
57//
58//#define DEBUG_UPREFS 1
59#ifdef DEBUG_UPREFS
Bill Wendlinge8156192006-12-07 01:30:32 +000060#define UR_OUT(X) cerr << X
Chris Lattner30c89792001-09-07 16:35:17 +000061#else
62#define UR_OUT(X)
63#endif
64
Vikram S. Adved3f7eb02002-07-14 22:59:28 +000065#define YYERROR_VERBOSE 1
66
Chris Lattnerb2b96672005-11-12 18:21:21 +000067static GlobalVariable *CurGV;
Andrew Lenharth558bc882005-06-18 18:34:52 +000068
69
Chris Lattner7e708292002-06-25 16:13:24 +000070// This contains info used when building the body of a function. It is
71// destroyed when the function is completed.
Chris Lattner00950542001-06-06 20:29:01 +000072//
Chris Lattner9232b992003-04-22 18:42:41 +000073typedef std::vector<Value *> ValueList; // Numbered defs
Reid Spencere1553cc2006-12-31 05:40:12 +000074
Misha Brukman5a2a3822005-05-10 22:02:28 +000075static void
Reid Spencer186a43f2007-03-19 18:39:36 +000076ResolveDefinitions(ValueList &LateResolvers, ValueList *FutureLateResolvers=0);
Chris Lattner00950542001-06-06 20:29:01 +000077
78static struct PerModuleInfo {
79 Module *CurrentModule;
Reid Spencer186a43f2007-03-19 18:39:36 +000080 ValueList Values; // Module level numbered definitions
81 ValueList LateResolveValues;
Reid Spencerb78b9082006-11-28 07:28:14 +000082 std::vector<PATypeHolder> Types;
83 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner00950542001-06-06 20:29:01 +000084
Chris Lattnerbc721ed2004-07-13 08:28:21 +000085 /// PlaceHolderInfo - When temporary placeholder objects are created, remember
Reid Spencerfd20c0a2006-05-29 02:34:34 +000086 /// how they were referenced and on which line of the input they came from so
Chris Lattner22ae2322004-07-13 08:39:15 +000087 /// that we can resolve them later and print error messages as appropriate.
Chris Lattnerbc721ed2004-07-13 08:28:21 +000088 std::map<Value*, std::pair<ValID, int> > PlaceHolderInfo;
89
Chris Lattner2079fde2001-10-13 06:41:08 +000090 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
91 // references to global values. Global values may be referenced before they
92 // are defined, and if so, the temporary object that they represent is held
Reid Spencer3271ed52004-07-18 00:08:11 +000093 // here. This is used for forward references of GlobalValues.
Chris Lattner2079fde2001-10-13 06:41:08 +000094 //
Chris Lattner9232b992003-04-22 18:42:41 +000095 typedef std::map<std::pair<const PointerType *,
Chris Lattnerbc207592004-03-08 06:09:57 +000096 ValID>, GlobalValue*> GlobalRefsType;
Chris Lattner2079fde2001-10-13 06:41:08 +000097 GlobalRefsType GlobalRefs;
98
Chris Lattner00950542001-06-06 20:29:01 +000099 void ModuleDone() {
Chris Lattner7e708292002-06-25 16:13:24 +0000100 // If we could not resolve some functions at function compilation time
101 // (calls to functions before they are defined), resolve them now... Types
102 // are resolved when the constant pool has been completely parsed.
Chris Lattner30c89792001-09-07 16:35:17 +0000103 //
Chris Lattner00950542001-06-06 20:29:01 +0000104 ResolveDefinitions(LateResolveValues);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000105 if (TriggerError)
106 return;
Chris Lattner00950542001-06-06 20:29:01 +0000107
Chris Lattner2079fde2001-10-13 06:41:08 +0000108 // Check to make sure that all global value forward references have been
109 // resolved!
110 //
111 if (!GlobalRefs.empty()) {
Chris Lattner9232b992003-04-22 18:42:41 +0000112 std::string UndefinedReferences = "Unresolved global references exist:\n";
Misha Brukman5a2a3822005-05-10 22:02:28 +0000113
Chris Lattner749ce032002-03-11 22:12:39 +0000114 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
115 I != E; ++I) {
116 UndefinedReferences += " " + I->first.first->getDescription() + " " +
117 I->first.second.getName() + "\n";
118 }
Reid Spencer61c83e02006-08-18 08:43:06 +0000119 GenerateError(UndefinedReferences);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000120 return;
Chris Lattner2079fde2001-10-13 06:41:08 +0000121 }
122
Chandler Carruth69940402007-08-04 01:51:18 +0000123 // Look for intrinsic functions and CallInst that need to be upgraded
124 for (Module::iterator FI = CurrentModule->begin(),
125 FE = CurrentModule->end(); FI != FE; )
126 UpgradeCallsToIntrinsic(FI++); // must be post-increment, as we remove
127
Chris Lattner7e708292002-06-25 16:13:24 +0000128 Values.clear(); // Clear out function local definitions
Chris Lattner30c89792001-09-07 16:35:17 +0000129 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +0000130 CurrentModule = 0;
131 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000132
Chris Lattner8a327842004-07-14 21:44:00 +0000133 // GetForwardRefForGlobal - Check to see if there is a forward reference
134 // for this global. If so, remove it from the GlobalRefs map and return it.
135 // If not, just return null.
Reid Spencer863dd882007-04-28 16:06:50 +0000136 GlobalValue *GetForwardRefForGlobal(const PointerType *PTy, ValID ID) {
Chris Lattner8a327842004-07-14 21:44:00 +0000137 // Check to see if there is a forward reference to this global variable...
138 // if there is, eliminate it and patch the reference to use the new def'n.
139 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(PTy, ID));
140 GlobalValue *Ret = 0;
141 if (I != GlobalRefs.end()) {
142 Ret = I->second;
Reid Spencer863dd882007-04-28 16:06:50 +0000143 GlobalRefs.erase(I);
Chris Lattner8a327842004-07-14 21:44:00 +0000144 }
145 return Ret;
146 }
Reid Spencer98b3c5c2007-01-02 21:53:43 +0000147
148 bool TypeIsUnresolved(PATypeHolder* PATy) {
149 // If it isn't abstract, its resolved
150 const Type* Ty = PATy->get();
151 if (!Ty->isAbstract())
152 return false;
153 // Traverse the type looking for abstract types. If it isn't abstract then
154 // we don't need to traverse that leg of the type.
155 std::vector<const Type*> WorkList, SeenList;
156 WorkList.push_back(Ty);
157 while (!WorkList.empty()) {
158 const Type* Ty = WorkList.back();
159 SeenList.push_back(Ty);
160 WorkList.pop_back();
161 if (const OpaqueType* OpTy = dyn_cast<OpaqueType>(Ty)) {
162 // Check to see if this is an unresolved type
163 std::map<ValID, PATypeHolder>::iterator I = LateResolveTypes.begin();
164 std::map<ValID, PATypeHolder>::iterator E = LateResolveTypes.end();
165 for ( ; I != E; ++I) {
166 if (I->second.get() == OpTy)
167 return true;
168 }
169 } else if (const SequentialType* SeqTy = dyn_cast<SequentialType>(Ty)) {
170 const Type* TheTy = SeqTy->getElementType();
171 if (TheTy->isAbstract() && TheTy != Ty) {
172 std::vector<const Type*>::iterator I = SeenList.begin(),
173 E = SeenList.end();
174 for ( ; I != E; ++I)
175 if (*I == TheTy)
176 break;
177 if (I == E)
178 WorkList.push_back(TheTy);
179 }
180 } else if (const StructType* StrTy = dyn_cast<StructType>(Ty)) {
181 for (unsigned i = 0; i < StrTy->getNumElements(); ++i) {
182 const Type* TheTy = StrTy->getElementType(i);
183 if (TheTy->isAbstract() && TheTy != Ty) {
184 std::vector<const Type*>::iterator I = SeenList.begin(),
185 E = SeenList.end();
186 for ( ; I != E; ++I)
187 if (*I == TheTy)
188 break;
189 if (I == E)
190 WorkList.push_back(TheTy);
191 }
192 }
193 }
194 }
195 return false;
196 }
Chris Lattner00950542001-06-06 20:29:01 +0000197} CurModule;
198
Chris Lattner79df7c02002-03-26 18:01:55 +0000199static struct PerFunctionInfo {
Chris Lattner7e708292002-06-25 16:13:24 +0000200 Function *CurrentFunction; // Pointer to current function being created
Chris Lattner00950542001-06-06 20:29:01 +0000201
Reid Spencer186a43f2007-03-19 18:39:36 +0000202 ValueList Values; // Keep track of #'d definitions
203 unsigned NextValNum;
204 ValueList LateResolveValues;
Reid Spenceref9b9a72007-02-05 20:47:22 +0000205 bool isDeclare; // Is this function a forward declararation?
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000206 GlobalValue::LinkageTypes Linkage; // Linkage for forward declaration.
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000207 GlobalValue::VisibilityTypes Visibility;
Chris Lattner00950542001-06-06 20:29:01 +0000208
Chris Lattner2e2ed292004-07-14 06:28:35 +0000209 /// BBForwardRefs - When we see forward references to basic blocks, keep
210 /// track of them here.
Reid Spencer186a43f2007-03-19 18:39:36 +0000211 std::map<ValID, BasicBlock*> BBForwardRefs;
Chris Lattner2e2ed292004-07-14 06:28:35 +0000212
Chris Lattner79df7c02002-03-26 18:01:55 +0000213 inline PerFunctionInfo() {
214 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000215 isDeclare = false;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000216 Linkage = GlobalValue::ExternalLinkage;
217 Visibility = GlobalValue::DefaultVisibility;
Chris Lattner00950542001-06-06 20:29:01 +0000218 }
219
Chris Lattner79df7c02002-03-26 18:01:55 +0000220 inline void FunctionStart(Function *M) {
221 CurrentFunction = M;
Reid Spencer186a43f2007-03-19 18:39:36 +0000222 NextValNum = 0;
Chris Lattner00950542001-06-06 20:29:01 +0000223 }
224
Chris Lattner79df7c02002-03-26 18:01:55 +0000225 void FunctionDone() {
Chris Lattner2e2ed292004-07-14 06:28:35 +0000226 // Any forward referenced blocks left?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000227 if (!BBForwardRefs.empty()) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000228 GenerateError("Undefined reference to label " +
Reid Spencer186a43f2007-03-19 18:39:36 +0000229 BBForwardRefs.begin()->second->getName());
Reid Spencer5b7e7532006-09-28 19:28:24 +0000230 return;
231 }
Chris Lattner2e2ed292004-07-14 06:28:35 +0000232
233 // Resolve all forward references now.
Chris Lattner386a3b72001-10-16 19:54:17 +0000234 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000235
Chris Lattner7e708292002-06-25 16:13:24 +0000236 Values.clear(); // Clear out function local definitions
Reid Spencer186a43f2007-03-19 18:39:36 +0000237 BBForwardRefs.clear();
Chris Lattner79df7c02002-03-26 18:01:55 +0000238 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000239 isDeclare = false;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000240 Linkage = GlobalValue::ExternalLinkage;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000241 Visibility = GlobalValue::DefaultVisibility;
Chris Lattner00950542001-06-06 20:29:01 +0000242 }
Chris Lattner394f2eb2003-10-16 20:12:13 +0000243} CurFun; // Info for the current function...
Chris Lattner00950542001-06-06 20:29:01 +0000244
Chris Lattner394f2eb2003-10-16 20:12:13 +0000245static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000246
Chris Lattner00950542001-06-06 20:29:01 +0000247
248//===----------------------------------------------------------------------===//
249// Code to handle definitions of all the types
250//===----------------------------------------------------------------------===//
251
Reid Spencer186a43f2007-03-19 18:39:36 +0000252static void InsertValue(Value *V, ValueList &ValueTab = CurFun.Values) {
253 // Things that have names or are void typed don't get slot numbers
254 if (V->hasName() || (V->getType() == Type::VoidTy))
255 return;
Chris Lattner2079fde2001-10-13 06:41:08 +0000256
Reid Spencer186a43f2007-03-19 18:39:36 +0000257 // In the case of function values, we have to allow for the forward reference
258 // of basic blocks, which are included in the numbering. Consequently, we keep
259 // track of the next insertion location with NextValNum. When a BB gets
260 // inserted, it could change the size of the CurFun.Values vector.
261 if (&ValueTab == &CurFun.Values) {
262 if (ValueTab.size() <= CurFun.NextValNum)
263 ValueTab.resize(CurFun.NextValNum+1);
264 ValueTab[CurFun.NextValNum++] = V;
265 return;
266 }
267 // For all other lists, its okay to just tack it on the back of the vector.
268 ValueTab.push_back(V);
Chris Lattner00950542001-06-06 20:29:01 +0000269}
270
Chris Lattner30c89792001-09-07 16:35:17 +0000271static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000272 switch (D.Type) {
Reid Spencerb2d17862007-01-26 08:04:51 +0000273 case ValID::LocalID: // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000274 // Module constants occupy the lowest numbered slots...
Reid Spencerb2d17862007-01-26 08:04:51 +0000275 if (D.Num < CurModule.Types.size())
276 return CurModule.Types[D.Num];
Chris Lattner42c9e772001-10-20 09:32:59 +0000277 break;
Reid Spencerb2d17862007-01-26 08:04:51 +0000278 case ValID::LocalName: // Is it a named definition?
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000279 if (const Type *N = CurModule.CurrentModule->getTypeByName(D.getName())) {
Chris Lattnera09000d2004-07-14 23:03:46 +0000280 D.destroy(); // Free old strdup'd memory...
281 return N;
Chris Lattner6e6026b2002-11-20 18:36:02 +0000282 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000283 break;
Chris Lattner30c89792001-09-07 16:35:17 +0000284 default:
Reid Spencerf4fa5902007-02-05 10:16:10 +0000285 GenerateError("Internal parser error: Invalid symbol type reference");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000286 return 0;
Chris Lattner30c89792001-09-07 16:35:17 +0000287 }
288
289 // If we reached here, we referenced either a symbol that we don't know about
290 // or an id number that hasn't been read yet. We may be referencing something
291 // forward, so just create an entry to be resolved later and get to it...
292 //
293 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
294
Chris Lattner355ad1f2005-03-21 06:27:42 +0000295
296 if (inFunctionScope()) {
Reid Spencerb2d17862007-01-26 08:04:51 +0000297 if (D.Type == ValID::LocalName) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000298 GenerateError("Reference to an undefined type: '" + D.getName() + "'");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000299 return 0;
300 } else {
Reid Spencerb2d17862007-01-26 08:04:51 +0000301 GenerateError("Reference to an undefined type: #" + utostr(D.Num));
Reid Spencer5b7e7532006-09-28 19:28:24 +0000302 return 0;
303 }
Chris Lattner4a42e902001-10-22 05:56:09 +0000304 }
Chris Lattner30c89792001-09-07 16:35:17 +0000305
Reid Spencerb78b9082006-11-28 07:28:14 +0000306 std::map<ValID, PATypeHolder>::iterator I =CurModule.LateResolveTypes.find(D);
Chris Lattner355ad1f2005-03-21 06:27:42 +0000307 if (I != CurModule.LateResolveTypes.end())
Reid Spencerb78b9082006-11-28 07:28:14 +0000308 return I->second;
Chris Lattner355ad1f2005-03-21 06:27:42 +0000309
Reid Spencerb78b9082006-11-28 07:28:14 +0000310 Type *Typ = OpaqueType::get();
311 CurModule.LateResolveTypes.insert(std::make_pair(D, Typ));
312 return Typ;
Reid Spencer08de34b2006-12-03 05:45:44 +0000313 }
Chris Lattner30c89792001-09-07 16:35:17 +0000314
Reid Spencer186a43f2007-03-19 18:39:36 +0000315// getExistingVal - Look up the value specified by the provided type and
Chris Lattner2079fde2001-10-13 06:41:08 +0000316// the provided ValID. If the value exists and has already been defined, return
317// it. Otherwise return null.
318//
Reid Spencer186a43f2007-03-19 18:39:36 +0000319static Value *getExistingVal(const Type *Ty, const ValID &D) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000320 if (isa<FunctionType>(Ty)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000321 GenerateError("Functions are not values and "
Chris Lattner79df7c02002-03-26 18:01:55 +0000322 "must be referenced as pointers");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000323 return 0;
324 }
Chris Lattner386a3b72001-10-16 19:54:17 +0000325
Chris Lattner30c89792001-09-07 16:35:17 +0000326 switch (D.Type) {
Reid Spencerb2d17862007-01-26 08:04:51 +0000327 case ValID::LocalID: { // Is it a numbered definition?
Reid Spencerb2d17862007-01-26 08:04:51 +0000328 // Check that the number is within bounds.
Reid Spencer186a43f2007-03-19 18:39:36 +0000329 if (D.Num >= CurFun.Values.size())
330 return 0;
331 Value *Result = CurFun.Values[D.Num];
332 if (Ty != Result->getType()) {
333 GenerateError("Numbered value (%" + utostr(D.Num) + ") of type '" +
334 Result->getType()->getDescription() + "' does not match "
335 "expected type, '" + Ty->getDescription() + "'");
336 return 0;
337 }
338 return Result;
Reid Spencerb2d17862007-01-26 08:04:51 +0000339 }
340 case ValID::GlobalID: { // Is it a numbered definition?
Reid Spencer186a43f2007-03-19 18:39:36 +0000341 if (D.Num >= CurModule.Values.size())
Reid Spenceref9b9a72007-02-05 20:47:22 +0000342 return 0;
Reid Spencer186a43f2007-03-19 18:39:36 +0000343 Value *Result = CurModule.Values[D.Num];
344 if (Ty != Result->getType()) {
345 GenerateError("Numbered value (@" + utostr(D.Num) + ") of type '" +
346 Result->getType()->getDescription() + "' does not match "
347 "expected type, '" + Ty->getDescription() + "'");
Reid Spenceref9b9a72007-02-05 20:47:22 +0000348 return 0;
Reid Spencer186a43f2007-03-19 18:39:36 +0000349 }
350 return Result;
Chris Lattner00950542001-06-06 20:29:01 +0000351 }
Reid Spencerb2d17862007-01-26 08:04:51 +0000352
353 case ValID::LocalName: { // Is it a named definition?
Reid Spenceref9b9a72007-02-05 20:47:22 +0000354 if (!inFunctionScope())
355 return 0;
356 ValueSymbolTable &SymTab = CurFun.CurrentFunction->getValueSymbolTable();
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000357 Value *N = SymTab.lookup(D.getName());
Reid Spenceref9b9a72007-02-05 20:47:22 +0000358 if (N == 0)
359 return 0;
360 if (N->getType() != Ty)
361 return 0;
Reid Spencerb2d17862007-01-26 08:04:51 +0000362
363 D.destroy(); // Free old strdup'd memory...
364 return N;
365 }
366 case ValID::GlobalName: { // Is it a named definition?
Reid Spenceref9b9a72007-02-05 20:47:22 +0000367 ValueSymbolTable &SymTab = CurModule.CurrentModule->getValueSymbolTable();
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000368 Value *N = SymTab.lookup(D.getName());
Reid Spenceref9b9a72007-02-05 20:47:22 +0000369 if (N == 0)
370 return 0;
371 if (N->getType() != Ty)
372 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000373
374 D.destroy(); // Free old strdup'd memory...
375 return N;
376 }
377
Misha Brukman5a2a3822005-05-10 22:02:28 +0000378 // Check to make sure that "Ty" is an integral type, and that our
Chris Lattner2079fde2001-10-13 06:41:08 +0000379 // value will fit into the specified type...
380 case ValID::ConstSIntVal: // Is it a constant pool reference??
Reid Spencerb83eb642006-10-20 07:07:24 +0000381 if (!ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000382 GenerateError("Signed integral constant '" +
Misha Brukman5a2a3822005-05-10 22:02:28 +0000383 itostr(D.ConstPool64) + "' is invalid for type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +0000384 Ty->getDescription() + "'");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000385 return 0;
386 }
Reid Spencereac65742007-03-19 20:40:22 +0000387 return ConstantInt::get(Ty, D.ConstPool64, true);
Chris Lattner2079fde2001-10-13 06:41:08 +0000388
389 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Reid Spencerb83eb642006-10-20 07:07:24 +0000390 if (!ConstantInt::isValueValidForType(Ty, D.UConstPool64)) {
391 if (!ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000392 GenerateError("Integral constant '" + utostr(D.UConstPool64) +
Reid Spencerf4fa5902007-02-05 10:16:10 +0000393 "' is invalid or out of range");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000394 return 0;
Chris Lattner2079fde2001-10-13 06:41:08 +0000395 } else { // This is really a signed reference. Transmogrify.
Reid Spencereac65742007-03-19 20:40:22 +0000396 return ConstantInt::get(Ty, D.ConstPool64, true);
Chris Lattner2079fde2001-10-13 06:41:08 +0000397 }
398 } else {
Reid Spencerb83eb642006-10-20 07:07:24 +0000399 return ConstantInt::get(Ty, D.UConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000400 }
401
Chris Lattner2079fde2001-10-13 06:41:08 +0000402 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Dale Johannesen43421b32007-09-06 18:13:44 +0000403 if (!ConstantFP::isValueValidForType(Ty, *D.ConstPoolFP)) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000404 GenerateError("FP constant invalid for type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000405 return 0;
406 }
Dale Johannesen3f6eb742007-09-11 18:32:33 +0000407 // Lexer has no type info, so builds all float and double FP constants
408 // as double. Fix this here. Long double does not need this.
409 if (&D.ConstPoolFP->getSemantics() == &APFloat::IEEEdouble &&
410 Ty==Type::FloatTy)
Dale Johannesen43421b32007-09-06 18:13:44 +0000411 D.ConstPoolFP->convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven);
412 return ConstantFP::get(Ty, *D.ConstPoolFP);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000413
Chris Lattner2079fde2001-10-13 06:41:08 +0000414 case ValID::ConstNullVal: // Is it a null value?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000415 if (!isa<PointerType>(Ty)) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000416 GenerateError("Cannot create a a non pointer null");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000417 return 0;
418 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000419 return ConstantPointerNull::get(cast<PointerType>(Ty));
Misha Brukman5a2a3822005-05-10 22:02:28 +0000420
Chris Lattner16710e92004-10-16 18:17:13 +0000421 case ValID::ConstUndefVal: // Is it an undef value?
422 return UndefValue::get(Ty);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000423
Chris Lattnerf1f03df2005-12-21 17:53:02 +0000424 case ValID::ConstZeroVal: // Is it a zero value?
425 return Constant::getNullValue(Ty);
426
Chris Lattnerd78700d2002-08-16 21:14:40 +0000427 case ValID::ConstantVal: // Fully resolved constant?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000428 if (D.ConstantValue->getType() != Ty) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000429 GenerateError("Constant expression type different from required type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000430 return 0;
431 }
Chris Lattnerd78700d2002-08-16 21:14:40 +0000432 return D.ConstantValue;
433
Chris Lattneraa2c8532006-01-25 22:26:43 +0000434 case ValID::InlineAsmVal: { // Inline asm expression
435 const PointerType *PTy = dyn_cast<PointerType>(Ty);
436 const FunctionType *FTy =
437 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
Reid Spencer5b7e7532006-09-28 19:28:24 +0000438 if (!FTy || !InlineAsm::Verify(FTy, D.IAD->Constraints)) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000439 GenerateError("Invalid type for asm constraint string");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000440 return 0;
441 }
Chris Lattneraa2c8532006-01-25 22:26:43 +0000442 InlineAsm *IA = InlineAsm::get(FTy, D.IAD->AsmString, D.IAD->Constraints,
443 D.IAD->HasSideEffects);
444 D.destroy(); // Free InlineAsmDescriptor.
445 return IA;
446 }
Chris Lattner30c89792001-09-07 16:35:17 +0000447 default:
Reid Spencer1755a9a2007-02-05 17:01:20 +0000448 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000449 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000450 } // End of switch
451
Reid Spencer1755a9a2007-02-05 17:01:20 +0000452 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000453 return 0;
454}
455
Reid Spencer186a43f2007-03-19 18:39:36 +0000456// getVal - This function is identical to getExistingVal, except that if a
Chris Lattner2079fde2001-10-13 06:41:08 +0000457// value is not already defined, it "improvises" by creating a placeholder var
458// that looks and acts just like the requested variable. When the value is
459// defined later, all uses of the placeholder variable are replaced with the
460// real thing.
461//
Chris Lattner64116f92004-07-14 01:33:11 +0000462static Value *getVal(const Type *Ty, const ValID &ID) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000463 if (Ty == Type::LabelTy) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000464 GenerateError("Cannot use a basic block here");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000465 return 0;
466 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000467
Chris Lattner64116f92004-07-14 01:33:11 +0000468 // See if the value has already been defined.
Reid Spencer186a43f2007-03-19 18:39:36 +0000469 Value *V = getExistingVal(Ty, ID);
Chris Lattner2079fde2001-10-13 06:41:08 +0000470 if (V) return V;
Reid Spencer5b7e7532006-09-28 19:28:24 +0000471 if (TriggerError) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000472
Reid Spencer5b7e7532006-09-28 19:28:24 +0000473 if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty)) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000474 GenerateError("Invalid use of a composite type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000475 return 0;
476 }
Chris Lattner60cd9552005-03-23 01:29:26 +0000477
Chris Lattner00950542001-06-06 20:29:01 +0000478 // If we reached here, we referenced either a symbol that we don't know about
479 // or an id number that hasn't been read yet. We may be referencing something
480 // forward, so just create an entry to be resolved later and get to it...
481 //
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000482 switch (ID.Type) {
483 case ValID::GlobalName:
Reid Spencer3cb4dda2007-04-28 14:13:42 +0000484 case ValID::GlobalID: {
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000485 const PointerType *PTy = dyn_cast<PointerType>(Ty);
486 if (!PTy) {
487 GenerateError("Invalid type for reference to global" );
488 return 0;
489 }
490 const Type* ElTy = PTy->getElementType();
491 if (const FunctionType *FTy = dyn_cast<FunctionType>(ElTy))
492 V = new Function(FTy, GlobalValue::ExternalLinkage);
493 else
Christopher Lambfe63fb92007-12-11 08:59:05 +0000494 V = new GlobalVariable(ElTy, false, GlobalValue::ExternalLinkage, 0, "",
495 (Module*)0, false, PTy->getAddressSpace());
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000496 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 Lattner8e3a8e02007-11-18 08:46:26 +0000505 LLLgetLineNo())));
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000506
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,
Christopher Lambfe63fb92007-12-11 08:59:05 +0000726 Constant *Initializer, bool IsThreadLocal,
727 unsigned AddressSpace = 0) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000728 if (isa<FunctionType>(Ty)) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000729 GenerateError("Cannot declare global vars of function type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000730 return 0;
731 }
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000732
Christopher Lambfe63fb92007-12-11 08:59:05 +0000733 const PointerType *PTy = PointerType::get(Ty, AddressSpace);
Chris Lattnera09000d2004-07-14 23:03:46 +0000734
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000735 std::string Name;
736 if (NameStr) {
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000737 Name = *NameStr; // Copy string
738 delete NameStr; // Free old string
Chris Lattnerd88998d2004-07-14 08:23:52 +0000739 }
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000740
Chris Lattner8a327842004-07-14 21:44:00 +0000741 // See if this global value was forward referenced. If so, recycle the
742 // object.
Misha Brukman5a2a3822005-05-10 22:02:28 +0000743 ValID ID;
Chris Lattner8a327842004-07-14 21:44:00 +0000744 if (!Name.empty()) {
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000745 ID = ValID::createGlobalName(Name);
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000746 } else {
Reid Spencer186a43f2007-03-19 18:39:36 +0000747 ID = ValID::createGlobalID(CurModule.Values.size());
Chris Lattner8a327842004-07-14 21:44:00 +0000748 }
749
Reid Spencer863dd882007-04-28 16:06:50 +0000750 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000751 // Move the global to the end of the list, from whereever it was
Chris Lattner8a327842004-07-14 21:44:00 +0000752 // previously inserted.
753 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
754 CurModule.CurrentModule->getGlobalList().remove(GV);
755 CurModule.CurrentModule->getGlobalList().push_back(GV);
756 GV->setInitializer(Initializer);
757 GV->setLinkage(Linkage);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000758 GV->setVisibility(Visibility);
Chris Lattner8a327842004-07-14 21:44:00 +0000759 GV->setConstant(isConstantGlobal);
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +0000760 GV->setThreadLocal(IsThreadLocal);
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000761 InsertValue(GV, CurModule.Values);
Chris Lattnerb2b96672005-11-12 18:21:21 +0000762 return GV;
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000763 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000764
Reid Spenceref9b9a72007-02-05 20:47:22 +0000765 // If this global has a name
Chris Lattnera09000d2004-07-14 23:03:46 +0000766 if (!Name.empty()) {
Reid Spenceref9b9a72007-02-05 20:47:22 +0000767 // if the global we're parsing has an initializer (is a definition) and
768 // has external linkage.
769 if (Initializer && Linkage != GlobalValue::InternalLinkage)
770 // If there is already a global with external linkage with this name
771 if (CurModule.CurrentModule->getGlobalVariable(Name, false)) {
772 // If we allow this GVar to get created, it will be renamed in the
773 // symbol table because it conflicts with an existing GVar. We can't
774 // allow redefinition of GVars whose linking indicates that their name
775 // must stay the same. Issue the error.
776 GenerateError("Redefinition of global variable named '" + Name +
777 "' of type '" + Ty->getDescription() + "'");
778 return 0;
779 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000780 }
781
782 // Otherwise there is no existing GV to use, create one now.
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000783 GlobalVariable *GV =
Misha Brukman5a2a3822005-05-10 22:02:28 +0000784 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
Christopher Lambfe63fb92007-12-11 08:59:05 +0000785 CurModule.CurrentModule, IsThreadLocal, AddressSpace);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000786 GV->setVisibility(Visibility);
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000787 InsertValue(GV, CurModule.Values);
Chris Lattnerb2b96672005-11-12 18:21:21 +0000788 return GV;
Chris Lattnerd88998d2004-07-14 08:23:52 +0000789}
Chris Lattner8ab406d2004-07-13 07:59:27 +0000790
Reid Spencer75719bf2004-05-26 21:48:31 +0000791// setTypeName - Set the specified type to the name given. The name may be
792// null potentially, in which case this is a noop. The string passed in is
793// assumed to be a malloc'd string buffer, and is freed by this function.
794//
795// This function returns true if the type has already been defined, but is
796// allowed to be redefined in the specified context. If the name is a new name
797// for the type plane, it is inserted and false is returned.
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000798static bool setTypeName(const Type *T, std::string *NameStr) {
Reid Spencer1755a9a2007-02-05 17:01:20 +0000799 assert(!inFunctionScope() && "Can't give types function-local names!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000800 if (NameStr == 0) return false;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000801
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000802 std::string Name(*NameStr); // Copy string
803 delete NameStr; // Free old string
Reid Spencer75719bf2004-05-26 21:48:31 +0000804
805 // We don't allow assigning names to void type
Reid Spencer5b7e7532006-09-28 19:28:24 +0000806 if (T == Type::VoidTy) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000807 GenerateError("Can't assign name '" + Name + "' to the void type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000808 return false;
809 }
Reid Spencer75719bf2004-05-26 21:48:31 +0000810
Chris Lattnera09000d2004-07-14 23:03:46 +0000811 // Set the type name, checking for conflicts as we do so.
812 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000813
Chris Lattnera09000d2004-07-14 23:03:46 +0000814 if (AlreadyExists) { // Inserting a name that is already defined???
815 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
Reid Spencer1755a9a2007-02-05 17:01:20 +0000816 assert(Existing && "Conflict but no matching type?!");
Chris Lattnera09000d2004-07-14 23:03:46 +0000817
Reid Spencer75719bf2004-05-26 21:48:31 +0000818 // There is only one case where this is allowed: when we are refining an
819 // opaque type. In this case, Existing will be an opaque type.
820 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
821 // We ARE replacing an opaque type!
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000822 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000823 return true;
824 }
825
826 // Otherwise, this is an attempt to redefine a type. That's okay if
827 // the redefinition is identical to the original. This will be so if
828 // Existing and T point to the same Type object. In this one case we
829 // allow the equivalent redefinition.
830 if (Existing == T) return true; // Yes, it's equal.
831
832 // Any other kind of (non-equivalent) redefinition is an error.
Reid Spencer90e2f632007-01-05 21:50:38 +0000833 GenerateError("Redefinition of type named '" + Name + "' of type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +0000834 T->getDescription() + "'");
Reid Spencer75719bf2004-05-26 21:48:31 +0000835 }
836
Reid Spencer75719bf2004-05-26 21:48:31 +0000837 return false;
838}
Chris Lattner8896eda2001-07-09 19:38:36 +0000839
Chris Lattner30c89792001-09-07 16:35:17 +0000840//===----------------------------------------------------------------------===//
841// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000842//
Chris Lattner8896eda2001-07-09 19:38:36 +0000843
Chris Lattner3b073862004-02-09 03:19:29 +0000844// TypeContains - Returns true if Ty directly contains E in it.
Chris Lattner30c89792001-09-07 16:35:17 +0000845//
846static bool TypeContains(const Type *Ty, const Type *E) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000847 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
Chris Lattner6d8dbec2004-12-08 16:13:53 +0000848 E) != Ty->subtype_end();
Chris Lattner3b073862004-02-09 03:19:29 +0000849}
850
851namespace {
852 struct UpRefRecord {
853 // NestingLevel - The number of nesting levels that need to be popped before
854 // this type is resolved.
855 unsigned NestingLevel;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000856
Chris Lattner3b073862004-02-09 03:19:29 +0000857 // LastContainedTy - This is the type at the current binding level for the
858 // type. Every time we reduce the nesting level, this gets updated.
859 const Type *LastContainedTy;
860
861 // UpRefTy - This is the actual opaque type that the upreference is
862 // represented with.
863 OpaqueType *UpRefTy;
864
865 UpRefRecord(unsigned NL, OpaqueType *URTy)
866 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
867 };
Chris Lattner30c89792001-09-07 16:35:17 +0000868}
Chris Lattner698b56e2001-07-20 19:15:08 +0000869
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000870// UpRefs - A list of the outstanding upreferences that need to be resolved.
Chris Lattner3b073862004-02-09 03:19:29 +0000871static std::vector<UpRefRecord> UpRefs;
Chris Lattner30c89792001-09-07 16:35:17 +0000872
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000873/// HandleUpRefs - Every time we finish a new layer of types, this function is
874/// called. It loops through the UpRefs vector, which is a list of the
875/// currently active types. For each type, if the up reference is contained in
876/// the newly completed type, we decrement the level count. When the level
877/// count reaches zero, the upreferenced type is the type that is passed in:
878/// thus we can complete the cycle.
879///
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000880static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner703e92f2006-08-18 17:34:24 +0000881 // If Ty isn't abstract, or if there are no up-references in it, then there is
882 // nothing to resolve here.
883 if (!ty->isAbstract() || UpRefs.empty()) return ty;
884
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000885 PATypeHolder Ty(ty);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000886 UR_OUT("Type '" << Ty->getDescription() <<
Chris Lattner5084d032001-11-02 07:46:26 +0000887 "' newly formed. Resolving upreferences.\n" <<
888 UpRefs.size() << " upreferences active!\n");
Chris Lattner026a8ce2004-02-09 18:53:54 +0000889
890 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
891 // to zero), we resolve them all together before we resolve them to Ty. At
892 // the end of the loop, if there is anything to resolve to Ty, it will be in
893 // this variable.
894 OpaqueType *TypeToResolve = 0;
895
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000896 for (unsigned i = 0; i != UpRefs.size(); ++i) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000897 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
898 << UpRefs[i].second->getDescription() << ") = "
Reid Spencer3271ed52004-07-18 00:08:11 +0000899 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
Chris Lattner3b073862004-02-09 03:19:29 +0000900 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
901 // Decrement level of upreference
902 unsigned Level = --UpRefs[i].NestingLevel;
903 UpRefs[i].LastContainedTy = Ty;
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000904 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
Misha Brukman5a2a3822005-05-10 22:02:28 +0000905 if (Level == 0) { // Upreference should be resolved!
Chris Lattner026a8ce2004-02-09 18:53:54 +0000906 if (!TypeToResolve) {
907 TypeToResolve = UpRefs[i].UpRefTy;
908 } else {
909 UR_OUT(" * Resolving upreference for "
910 << UpRefs[i].second->getDescription() << "\n";
911 std::string OldName = UpRefs[i].UpRefTy->getDescription());
912 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
913 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
914 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
915 }
Reid Spencer3271ed52004-07-18 00:08:11 +0000916 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000917 --i; // Do not skip the next element...
Chris Lattner30c89792001-09-07 16:35:17 +0000918 }
919 }
Chris Lattner8896eda2001-07-09 19:38:36 +0000920 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000921
Chris Lattner026a8ce2004-02-09 18:53:54 +0000922 if (TypeToResolve) {
923 UR_OUT(" * Resolving upreference for "
924 << UpRefs[i].second->getDescription() << "\n";
Chris Lattner16af11d2004-02-09 21:03:38 +0000925 std::string OldName = TypeToResolve->getDescription());
Chris Lattner026a8ce2004-02-09 18:53:54 +0000926 TypeToResolve->refineAbstractTypeTo(Ty);
927 }
928
Chris Lattner8896eda2001-07-09 19:38:36 +0000929 return Ty;
930}
931
Chris Lattner00950542001-06-06 20:29:01 +0000932//===----------------------------------------------------------------------===//
933// RunVMAsmParser - Define an interface to this parser
934//===----------------------------------------------------------------------===//
935//
Reid Spencere1553cc2006-12-31 05:40:12 +0000936static Module* RunParser(Module * M);
937
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000938Module *llvm::RunVMAsmParser(llvm::MemoryBuffer *MB) {
939 InitLLLexer(MB);
940 Module *M = RunParser(new Module(LLLgetFilename()));
941 FreeLexer();
942 return M;
Chris Lattner00950542001-06-06 20:29:01 +0000943}
944
945%}
946
947%union {
Brian Gaeked0fde302003-11-11 22:41:34 +0000948 llvm::Module *ModuleVal;
949 llvm::Function *FunctionVal;
Brian Gaeked0fde302003-11-11 22:41:34 +0000950 llvm::BasicBlock *BasicBlockVal;
951 llvm::TerminatorInst *TermInstVal;
952 llvm::Instruction *InstVal;
Reid Spencer08de34b2006-12-03 05:45:44 +0000953 llvm::Constant *ConstVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000954
Reid Spencer08de34b2006-12-03 05:45:44 +0000955 const llvm::Type *PrimType;
Reid Spencere1553cc2006-12-31 05:40:12 +0000956 std::list<llvm::PATypeHolder> *TypeList;
Reid Spencer08de34b2006-12-03 05:45:44 +0000957 llvm::PATypeHolder *TypeVal;
958 llvm::Value *ValueVal;
Reid Spencer08de34b2006-12-03 05:45:44 +0000959 std::vector<llvm::Value*> *ValueList;
Reid Spencere1553cc2006-12-31 05:40:12 +0000960 llvm::ArgListType *ArgList;
961 llvm::TypeWithAttrs TypeWithAttrs;
962 llvm::TypeWithAttrsList *TypeWithAttrsList;
Dale Johanneseneb57ea72007-11-05 21:20:28 +0000963 llvm::ParamList *ParamList;
Reid Spencere1553cc2006-12-31 05:40:12 +0000964
Misha Brukman5a2a3822005-05-10 22:02:28 +0000965 // Represent the RHS of PHI node
Reid Spencer08de34b2006-12-03 05:45:44 +0000966 std::list<std::pair<llvm::Value*,
967 llvm::BasicBlock*> > *PHIList;
Brian Gaeked0fde302003-11-11 22:41:34 +0000968 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
Reid Spencer08de34b2006-12-03 05:45:44 +0000969 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000970
Brian Gaeked0fde302003-11-11 22:41:34 +0000971 llvm::GlobalValue::LinkageTypes Linkage;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000972 llvm::GlobalValue::VisibilityTypes Visibility;
Reid Spencer5694b6e2007-04-09 06:17:21 +0000973 uint16_t ParamAttrs;
Reid Spencerf2310042007-02-28 02:23:44 +0000974 llvm::APInt *APIntVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000975 int64_t SInt64Val;
976 uint64_t UInt64Val;
977 int SIntVal;
978 unsigned UIntVal;
Dale Johannesen43421b32007-09-06 18:13:44 +0000979 llvm::APFloat *FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000980 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000981
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000982 std::string *StrVal; // This memory must be deleted
983 llvm::ValID ValIDVal;
Chris Lattner00950542001-06-06 20:29:01 +0000984
Reid Spencer08de34b2006-12-03 05:45:44 +0000985 llvm::Instruction::BinaryOps BinaryOpVal;
986 llvm::Instruction::TermOps TermOpVal;
987 llvm::Instruction::MemoryOps MemOpVal;
988 llvm::Instruction::CastOps CastOpVal;
989 llvm::Instruction::OtherOps OtherOpVal;
Reid Spencer08de34b2006-12-03 05:45:44 +0000990 llvm::ICmpInst::Predicate IPredicate;
991 llvm::FCmpInst::Predicate FPredicate;
Chris Lattner00950542001-06-06 20:29:01 +0000992}
993
Reid Spencere1553cc2006-12-31 05:40:12 +0000994%type <ModuleVal> Module
Chris Lattner79df7c02002-03-26 18:01:55 +0000995%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000996%type <BasicBlockVal> BasicBlock InstructionList
997%type <TermInstVal> BBTerminatorInst
998%type <InstVal> Inst InstVal MemoryInst
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000999%type <ConstVal> ConstVal ConstExpr AliaseeRef
Chris Lattner6cdb0112001-11-26 16:54:11 +00001000%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +00001001%type <ArgList> ArgList ArgListH
Chris Lattnerc24d2082001-06-11 15:04:20 +00001002%type <PHIList> PHIList
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001003%type <ParamList> ParamList // For call param lists & GEP indices
Reid Spencere1553cc2006-12-31 05:40:12 +00001004%type <ValueList> IndexList // For GEP indices
1005%type <TypeList> TypeListI
1006%type <TypeWithAttrsList> ArgTypeList ArgTypeListI
Reid Spencer2c261782007-01-05 17:06:19 +00001007%type <TypeWithAttrs> ArgType
Chris Lattner00950542001-06-06 20:29:01 +00001008%type <JumpTable> JumpTable
Chris Lattner4ad02e72003-04-16 20:28:45 +00001009%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00001010%type <BoolVal> ThreadLocal // 'thread_local' or not
Chris Lattner15c9c032003-09-08 18:20:29 +00001011%type <BoolVal> OptVolatile // 'volatile' or not
Chris Lattnerddb6db42005-05-06 05:51:46 +00001012%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
Chris Lattneraa2c8532006-01-25 22:26:43 +00001013%type <BoolVal> OptSideEffect // 'sideeffect' or not.
Reid Spencere1553cc2006-12-31 05:40:12 +00001014%type <Linkage> GVInternalLinkage GVExternalLinkage
1015%type <Linkage> FunctionDefineLinkage FunctionDeclareLinkage
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001016%type <Linkage> AliasLinkage
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001017%type <Visibility> GVVisibilityStyle
Chris Lattner00950542001-06-06 20:29:01 +00001018
Chris Lattner2079fde2001-10-13 06:41:08 +00001019// ValueRef - Unresolved reference to a definition or BB
1020%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001021%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +00001022// Tokens and types for handling constant integer values
1023//
1024// ESINT64VAL - A negative number within long long range
1025%token <SInt64Val> ESINT64VAL
1026
1027// EUINT64VAL - A positive number within uns. long long range
1028%token <UInt64Val> EUINT64VAL
Chris Lattner00950542001-06-06 20:29:01 +00001029
Reid Spencerf2310042007-02-28 02:23:44 +00001030// ESAPINTVAL - A negative number with arbitrary precision
1031%token <APIntVal> ESAPINTVAL
1032
1033// EUAPINTVAL - A positive number with arbitrary precision
1034%token <APIntVal> EUAPINTVAL
1035
Reid Spencerb2d17862007-01-26 08:04:51 +00001036%token <UIntVal> LOCALVAL_ID GLOBALVAL_ID // %123 @123
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001037%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +00001038
1039// Built in types...
Reid Spencer2c261782007-01-05 17:06:19 +00001040%type <TypeVal> Types ResultTypes
Reid Spencere1553cc2006-12-31 05:40:12 +00001041%type <PrimType> IntType FPType PrimType // Classifications
Reid Spencer8088e9d2007-01-13 05:00:20 +00001042%token <PrimType> VOID INTTYPE
Dale Johannesen320fc8a2007-08-03 01:03:46 +00001043%token <PrimType> FLOAT DOUBLE X86_FP80 FP128 PPC_FP128 LABEL
Reid Spencerb951bc02006-12-29 20:29:48 +00001044%token TYPE
Chris Lattner00950542001-06-06 20:29:01 +00001045
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001046
Reid Spenceraa7c2f82007-05-19 07:21:26 +00001047%token<StrVal> LOCALVAR GLOBALVAR LABELSTR
1048%token<StrVal> STRINGCONSTANT ATSTRINGCONSTANT PCTSTRINGCONSTANT
Reid Spencerb2d17862007-01-26 08:04:51 +00001049%type <StrVal> LocalName OptLocalName OptLocalAssign
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001050%type <StrVal> GlobalName OptGlobalAssign GlobalAssign
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00001051%type <StrVal> OptSection SectionString OptGC
Chris Lattner00950542001-06-06 20:29:01 +00001052
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001053%type <UIntVal> OptAlign OptCAlign
1054
Reid Spencer744d0362007-04-09 01:55:42 +00001055%token ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001056%token DECLARE DEFINE GLOBAL CONSTANT SECTION ALIAS VOLATILE THREAD_LOCAL
Reid Spencere1553cc2006-12-31 05:40:12 +00001057%token TO DOTDOTDOT NULL_TOK UNDEF INTERNAL LINKONCE WEAK APPENDING
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00001058%token DLLIMPORT DLLEXPORT EXTERN_WEAK
Christopher Lambfe63fb92007-12-11 08:59:05 +00001059%token OPAQUE EXTERNAL TARGET TRIPLE ALIGN ADDRSPACE
Chris Lattneraa2c8532006-01-25 22:26:43 +00001060%token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001061%token CC_TOK CCC_TOK FASTCC_TOK COLDCC_TOK X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
Chris Lattner10b27112006-10-22 06:07:41 +00001062%token DATALAYOUT
Chris Lattnera8e8f162005-05-06 20:27:19 +00001063%type <UIntVal> OptCallingConv
Reid Spencer2c261782007-01-05 17:06:19 +00001064%type <ParamAttrs> OptParamAttrs ParamAttr
1065%type <ParamAttrs> OptFuncAttrs FuncAttr
Chris Lattner00950542001-06-06 20:29:01 +00001066
Misha Brukman5a2a3822005-05-10 22:02:28 +00001067// Basic Block Terminating Operators
Chris Lattner16710e92004-10-16 18:17:13 +00001068%token <TermOpVal> RET BR SWITCH INVOKE UNWIND UNREACHABLE
Chris Lattner00950542001-06-06 20:29:01 +00001069
Misha Brukman5a2a3822005-05-10 22:02:28 +00001070// Binary Operators
Reid Spencere4d87aa2006-12-23 06:05:41 +00001071%type <BinaryOpVal> ArithmeticOps LogicalOps // Binops Subcatagories
Reid Spencer0a783f72006-11-02 01:53:59 +00001072%token <BinaryOpVal> ADD SUB MUL UDIV SDIV FDIV UREM SREM FREM AND OR XOR
Reid Spencer832254e2007-02-02 02:16:23 +00001073%token <BinaryOpVal> SHL LSHR ASHR
1074
Reid Spencer08de34b2006-12-03 05:45:44 +00001075%token <OtherOpVal> ICMP FCMP
Reid Spencer08de34b2006-12-03 05:45:44 +00001076%type <IPredicate> IPredicates
Reid Spencer08de34b2006-12-03 05:45:44 +00001077%type <FPredicate> FPredicates
Reid Spencer9f746f42006-12-03 06:58:07 +00001078%token EQ NE SLT SGT SLE SGE ULT UGT ULE UGE
1079%token OEQ ONE OLT OGT OLE OGE ORD UNO UEQ UNE
Chris Lattner00950542001-06-06 20:29:01 +00001080
1081// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001082%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +00001083
Reid Spencer3da59db2006-11-27 01:05:10 +00001084// Cast Operators
1085%type <CastOpVal> CastOps
1086%token <CastOpVal> TRUNC ZEXT SEXT FPTRUNC FPEXT BITCAST
1087%token <CastOpVal> UITOFP SITOFP FPTOUI FPTOSI INTTOPTR PTRTOINT
1088
Chris Lattner027dcc52001-07-08 21:10:27 +00001089// Other Operators
Reid Spencer832254e2007-02-02 02:16:23 +00001090%token <OtherOpVal> PHI_TOK SELECT VAARG
Chris Lattner4c949082006-04-08 01:18:35 +00001091%token <OtherOpVal> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
Chris Lattnerddb6db42005-05-06 05:51:46 +00001092
Reid Spencer2c261782007-01-05 17:06:19 +00001093// Function Attributes
Duncan Sands36397f52007-07-27 12:58:54 +00001094%token SIGNEXT ZEROEXT NORETURN INREG SRET NOUNWIND NOALIAS BYVAL NEST
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00001095%token READNONE READONLY GC
Chris Lattner027dcc52001-07-08 21:10:27 +00001096
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001097// Visibility Styles
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +00001098%token DEFAULT HIDDEN PROTECTED
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001099
Chris Lattner00950542001-06-06 20:29:01 +00001100%start Module
1101%%
1102
Chris Lattner00950542001-06-06 20:29:01 +00001103
Misha Brukman5a2a3822005-05-10 22:02:28 +00001104// Operations that are notably excluded from this list include:
Chris Lattner00950542001-06-06 20:29:01 +00001105// RET, BR, & SWITCH because they end basic blocks and are treated specially.
1106//
Reid Spencer0a783f72006-11-02 01:53:59 +00001107ArithmeticOps: ADD | SUB | MUL | UDIV | SDIV | FDIV | UREM | SREM | FREM;
Reid Spencer832254e2007-02-02 02:16:23 +00001108LogicalOps : SHL | LSHR | ASHR | AND | OR | XOR;
Reid Spencer3da59db2006-11-27 01:05:10 +00001109CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | BITCAST |
1110 UITOFP | SITOFP | FPTOUI | FPTOSI | INTTOPTR | PTRTOINT;
Reid Spencer832254e2007-02-02 02:16:23 +00001111
Reid Spencer9f746f42006-12-03 06:58:07 +00001112IPredicates
Reid Spencer763ed5e2006-12-04 05:20:06 +00001113 : EQ { $$ = ICmpInst::ICMP_EQ; } | NE { $$ = ICmpInst::ICMP_NE; }
Reid Spencer9f746f42006-12-03 06:58:07 +00001114 | SLT { $$ = ICmpInst::ICMP_SLT; } | SGT { $$ = ICmpInst::ICMP_SGT; }
1115 | SLE { $$ = ICmpInst::ICMP_SLE; } | SGE { $$ = ICmpInst::ICMP_SGE; }
1116 | ULT { $$ = ICmpInst::ICMP_ULT; } | UGT { $$ = ICmpInst::ICMP_UGT; }
1117 | ULE { $$ = ICmpInst::ICMP_ULE; } | UGE { $$ = ICmpInst::ICMP_UGE; }
1118 ;
1119
1120FPredicates
1121 : OEQ { $$ = FCmpInst::FCMP_OEQ; } | ONE { $$ = FCmpInst::FCMP_ONE; }
1122 | OLT { $$ = FCmpInst::FCMP_OLT; } | OGT { $$ = FCmpInst::FCMP_OGT; }
1123 | OLE { $$ = FCmpInst::FCMP_OLE; } | OGE { $$ = FCmpInst::FCMP_OGE; }
1124 | ORD { $$ = FCmpInst::FCMP_ORD; } | UNO { $$ = FCmpInst::FCMP_UNO; }
1125 | UEQ { $$ = FCmpInst::FCMP_UEQ; } | UNE { $$ = FCmpInst::FCMP_UNE; }
1126 | ULT { $$ = FCmpInst::FCMP_ULT; } | UGT { $$ = FCmpInst::FCMP_UGT; }
1127 | ULE { $$ = FCmpInst::FCMP_ULE; } | UGE { $$ = FCmpInst::FCMP_UGE; }
1128 | TRUETOK { $$ = FCmpInst::FCMP_TRUE; }
1129 | FALSETOK { $$ = FCmpInst::FCMP_FALSE; }
1130 ;
Chris Lattner00950542001-06-06 20:29:01 +00001131
Chris Lattnere98dda62001-07-14 06:10:16 +00001132// These are some types that allow classification if we only want a particular
1133// thing... for example, only a signed, unsigned, or integral type.
Reid Spencera54b7cb2007-01-12 07:05:14 +00001134IntType : INTTYPE;
Dale Johannesen320fc8a2007-08-03 01:03:46 +00001135FPType : FLOAT | DOUBLE | PPC_FP128 | FP128 | X86_FP80;
Chris Lattner00950542001-06-06 20:29:01 +00001136
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001137LocalName : LOCALVAR | STRINGCONSTANT | PCTSTRINGCONSTANT ;
Reid Spencerb2d17862007-01-26 08:04:51 +00001138OptLocalName : LocalName | /*empty*/ { $$ = 0; };
1139
1140/// OptLocalAssign - Value producing statements have an optional assignment
1141/// component.
1142OptLocalAssign : LocalName '=' {
1143 $$ = $1;
1144 CHECK_FOR_ERROR
1145 }
1146 | /*empty*/ {
1147 $$ = 0;
1148 CHECK_FOR_ERROR
1149 };
1150
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001151GlobalName : GLOBALVAR | ATSTRINGCONSTANT ;
Reid Spencerb2d17862007-01-26 08:04:51 +00001152
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001153OptGlobalAssign : GlobalAssign
Misha Brukman5a2a3822005-05-10 22:02:28 +00001154 | /*empty*/ {
1155 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00001156 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001157 };
Chris Lattner00950542001-06-06 20:29:01 +00001158
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001159GlobalAssign : GlobalName '=' {
1160 $$ = $1;
1161 CHECK_FOR_ERROR
Anton Korobeynikovc0fabcb2007-04-25 18:07:40 +00001162 };
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001163
Reid Spencerb951bc02006-12-29 20:29:48 +00001164GVInternalLinkage
1165 : INTERNAL { $$ = GlobalValue::InternalLinkage; }
1166 | WEAK { $$ = GlobalValue::WeakLinkage; }
1167 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1168 | APPENDING { $$ = GlobalValue::AppendingLinkage; }
1169 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
1170 ;
1171
1172GVExternalLinkage
1173 : DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1174 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
1175 | EXTERNAL { $$ = GlobalValue::ExternalLinkage; }
1176 ;
1177
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001178GVVisibilityStyle
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +00001179 : /*empty*/ { $$ = GlobalValue::DefaultVisibility; }
1180 | DEFAULT { $$ = GlobalValue::DefaultVisibility; }
1181 | HIDDEN { $$ = GlobalValue::HiddenVisibility; }
1182 | PROTECTED { $$ = GlobalValue::ProtectedVisibility; }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001183 ;
1184
Reid Spencere1553cc2006-12-31 05:40:12 +00001185FunctionDeclareLinkage
1186 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1187 | DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1188 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
Reid Spencerb951bc02006-12-29 20:29:48 +00001189 ;
1190
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001191FunctionDefineLinkage
Reid Spencere1553cc2006-12-31 05:40:12 +00001192 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1193 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
Reid Spencerb951bc02006-12-29 20:29:48 +00001194 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1195 | WEAK { $$ = GlobalValue::WeakLinkage; }
1196 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
Reid Spencerb951bc02006-12-29 20:29:48 +00001197 ;
Chris Lattner30c89792001-09-07 16:35:17 +00001198
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001199AliasLinkage
1200 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1201 | WEAK { $$ = GlobalValue::WeakLinkage; }
1202 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
1203 ;
1204
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00001205OptCallingConv : /*empty*/ { $$ = CallingConv::C; } |
1206 CCC_TOK { $$ = CallingConv::C; } |
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00001207 FASTCC_TOK { $$ = CallingConv::Fast; } |
1208 COLDCC_TOK { $$ = CallingConv::Cold; } |
1209 X86_STDCALLCC_TOK { $$ = CallingConv::X86_StdCall; } |
1210 X86_FASTCALLCC_TOK { $$ = CallingConv::X86_FastCall; } |
1211 CC_TOK EUINT64VAL {
Chris Lattnera8e8f162005-05-06 20:27:19 +00001212 if ((unsigned)$2 != $2)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001213 GEN_ERROR("Calling conv too large");
Chris Lattnera8e8f162005-05-06 20:27:19 +00001214 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001215 CHECK_FOR_ERROR
Chris Lattnera8e8f162005-05-06 20:27:19 +00001216 };
1217
Reid Spencer9445e9a2007-07-19 23:13:04 +00001218ParamAttr : ZEROEXT { $$ = ParamAttr::ZExt; }
Reid Spencer1d3d2302007-07-31 02:57:37 +00001219 | ZEXT { $$ = ParamAttr::ZExt; }
Reid Spencer9445e9a2007-07-19 23:13:04 +00001220 | SIGNEXT { $$ = ParamAttr::SExt; }
Reid Spencer1d3d2302007-07-31 02:57:37 +00001221 | SEXT { $$ = ParamAttr::SExt; }
Zhou Shengfebca342007-06-05 05:28:26 +00001222 | INREG { $$ = ParamAttr::InReg; }
1223 | SRET { $$ = ParamAttr::StructRet; }
1224 | NOALIAS { $$ = ParamAttr::NoAlias; }
Duncan Sands36397f52007-07-27 12:58:54 +00001225 | BYVAL { $$ = ParamAttr::ByVal; }
1226 | NEST { $$ = ParamAttr::Nest; }
Reid Spencere1553cc2006-12-31 05:40:12 +00001227 ;
1228
Reid Spencer18da0722007-04-11 02:44:20 +00001229OptParamAttrs : /* empty */ { $$ = ParamAttr::None; }
Reid Spencer2c261782007-01-05 17:06:19 +00001230 | OptParamAttrs ParamAttr {
Reid Spencer5694b6e2007-04-09 06:17:21 +00001231 $$ = $1 | $2;
Reid Spencere1553cc2006-12-31 05:40:12 +00001232 }
1233 ;
1234
Reid Spencer18da0722007-04-11 02:44:20 +00001235FuncAttr : NORETURN { $$ = ParamAttr::NoReturn; }
1236 | NOUNWIND { $$ = ParamAttr::NoUnwind; }
Reid Spencer9445e9a2007-07-19 23:13:04 +00001237 | ZEROEXT { $$ = ParamAttr::ZExt; }
1238 | SIGNEXT { $$ = ParamAttr::SExt; }
Duncan Sandsed4a2f12007-11-22 20:23:04 +00001239 | READNONE { $$ = ParamAttr::ReadNone; }
1240 | READONLY { $$ = ParamAttr::ReadOnly; }
Reid Spencer2c261782007-01-05 17:06:19 +00001241 ;
1242
Reid Spencer18da0722007-04-11 02:44:20 +00001243OptFuncAttrs : /* empty */ { $$ = ParamAttr::None; }
Reid Spencer2c261782007-01-05 17:06:19 +00001244 | OptFuncAttrs FuncAttr {
Reid Spencer5694b6e2007-04-09 06:17:21 +00001245 $$ = $1 | $2;
Reid Spencer2c261782007-01-05 17:06:19 +00001246 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001247 ;
1248
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00001249OptGC : /* empty */ { $$ = 0; }
1250 | GC STRINGCONSTANT {
1251 $$ = $2;
1252 }
1253 ;
1254
Chris Lattner66db8e42005-11-06 06:34:12 +00001255// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1256// a comma before it.
Chris Lattner87ac9722005-11-06 06:46:28 +00001257OptAlign : /*empty*/ { $$ = 0; } |
Chris Lattnerb2b96672005-11-12 18:21:21 +00001258 ALIGN EUINT64VAL {
1259 $$ = $2;
1260 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001261 GEN_ERROR("Alignment must be a power of two");
Reid Spencer61c83e02006-08-18 08:43:06 +00001262 CHECK_FOR_ERROR
Chris Lattnerb2b96672005-11-12 18:21:21 +00001263};
Chris Lattner66db8e42005-11-06 06:34:12 +00001264OptCAlign : /*empty*/ { $$ = 0; } |
Chris Lattnerb2b96672005-11-12 18:21:21 +00001265 ',' ALIGN EUINT64VAL {
1266 $$ = $3;
1267 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001268 GEN_ERROR("Alignment must be a power of two");
Reid Spencer61c83e02006-08-18 08:43:06 +00001269 CHECK_FOR_ERROR
Chris Lattnerb2b96672005-11-12 18:21:21 +00001270};
1271
Chris Lattner66db8e42005-11-06 06:34:12 +00001272
Christopher Lambfe63fb92007-12-11 08:59:05 +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//
Dale Johannesen320fc8a2007-08-03 01:03:46 +00001308PrimType : INTTYPE | FLOAT | DOUBLE | PPC_FP128 | FP128 | X86_FP80 | 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 }
Christopher Lambfe63fb92007-12-11 08:59:05 +00001326 | Types ADDRSPACE '(' EUINT64VAL ')' '*' { // Pointer type?
1327 if (*$1 == Type::LabelTy)
1328 GEN_ERROR("Cannot form a pointer to a basic block");
1329 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1, $4)));
1330 delete $1;
1331 CHECK_FOR_ERROR
1332 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001333 | SymbolicValueRef { // Named types are also simple types...
1334 const Type* tmp = getTypeVal($1);
1335 CHECK_FOR_ERROR
1336 $$ = new PATypeHolder(tmp);
1337 }
1338 | '\\' EUINT64VAL { // Type UpReference
Reid Spencerf4fa5902007-02-05 10:16:10 +00001339 if ($2 > (uint64_t)~0U) GEN_ERROR("Value out of range");
Chris Lattner30c89792001-09-07 16:35:17 +00001340 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
Chris Lattner3b073862004-02-09 03:19:29 +00001341 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Reid Spencer08de34b2006-12-03 05:45:44 +00001342 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +00001343 UR_OUT("New Upreference!\n");
Reid Spencer61c83e02006-08-18 08:43:06 +00001344 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001345 }
Reid Spencer863dd882007-04-28 16:06:50 +00001346 | Types '(' ArgTypeListI ')' OptFuncAttrs {
Duncan Sandsdc024672007-11-27 13:23:08 +00001347 // Allow but ignore attributes on function types; this permits auto-upgrade.
1348 // FIXME: remove in LLVM 3.0.
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001349 const Type* RetTy = *$1;
Anton Korobeynikovf8c751a2007-12-03 21:00:45 +00001350 if (!(RetTy->isFirstClassType() || RetTy == Type::VoidTy ||
1351 isa<OpaqueType>(RetTy)))
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001352 GEN_ERROR("LLVM Functions cannot return aggregates");
1353
Chris Lattner9232b992003-04-22 18:42:41 +00001354 std::vector<const Type*> Params;
Reid Spencer5694b6e2007-04-09 06:17:21 +00001355 TypeWithAttrsList::iterator I = $3->begin(), E = $3->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00001356 for (; I != E; ++I ) {
Reid Spencer2c9df212007-03-20 01:13:00 +00001357 const Type *Ty = I->Ty->get();
Reid Spencer2c9df212007-03-20 01:13:00 +00001358 Params.push_back(Ty);
Reid Spencere1553cc2006-12-31 05:40:12 +00001359 }
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001360
Chris Lattner2079fde2001-10-13 06:41:08 +00001361 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1362 if (isVarArg) Params.pop_back();
1363
Anton Korobeynikovf8c751a2007-12-03 21:00:45 +00001364 for (unsigned i = 0; i != Params.size(); ++i)
1365 if (!(Params[i]->isFirstClassType() || isa<OpaqueType>(Params[i])))
1366 GEN_ERROR("Function arguments must be value types!");
1367
1368 CHECK_FOR_ERROR
1369
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001370 FunctionType *FT = FunctionType::get(RetTy, Params, isVarArg);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001371 delete $3; // Delete the argument list
Reid Spencere1553cc2006-12-31 05:40:12 +00001372 delete $1; // Delete the return type handle
1373 $$ = new PATypeHolder(HandleUpRefs(FT));
Reid Spencer61c83e02006-08-18 08:43:06 +00001374 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001375 }
Reid Spencer863dd882007-04-28 16:06:50 +00001376 | VOID '(' ArgTypeListI ')' OptFuncAttrs {
Duncan Sandsdc024672007-11-27 13:23:08 +00001377 // Allow but ignore attributes on function types; this permits auto-upgrade.
1378 // FIXME: remove in LLVM 3.0.
Reid Spencere1553cc2006-12-31 05:40:12 +00001379 std::vector<const Type*> Params;
Reid Spencer5694b6e2007-04-09 06:17:21 +00001380 TypeWithAttrsList::iterator I = $3->begin(), E = $3->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00001381 for ( ; I != E; ++I ) {
Reid Spencer2c9df212007-03-20 01:13:00 +00001382 const Type* Ty = I->Ty->get();
Reid Spencer2c9df212007-03-20 01:13:00 +00001383 Params.push_back(Ty);
Reid Spencere1553cc2006-12-31 05:40:12 +00001384 }
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001385
Reid Spencere1553cc2006-12-31 05:40:12 +00001386 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1387 if (isVarArg) Params.pop_back();
1388
Anton Korobeynikovf8c751a2007-12-03 21:00:45 +00001389 for (unsigned i = 0; i != Params.size(); ++i)
1390 if (!(Params[i]->isFirstClassType() || isa<OpaqueType>(Params[i])))
1391 GEN_ERROR("Function arguments must be value types!");
1392
1393 CHECK_FOR_ERROR
1394
Duncan Sandsdc024672007-11-27 13:23:08 +00001395 FunctionType *FT = FunctionType::get($1, Params, isVarArg);
Reid Spencer2c261782007-01-05 17:06:19 +00001396 delete $3; // Delete the argument list
Reid Spencere1553cc2006-12-31 05:40:12 +00001397 $$ = new PATypeHolder(HandleUpRefs(FT));
1398 CHECK_FOR_ERROR
1399 }
1400
1401 | '[' EUINT64VAL 'x' Types ']' { // Sized array type?
Reid Spencer08de34b2006-12-03 05:45:44 +00001402 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
1403 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00001404 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001405 }
Reid Spencerac9dcb92007-02-15 03:39:18 +00001406 | '<' EUINT64VAL 'x' Types '>' { // Vector type?
Reid Spencer08de34b2006-12-03 05:45:44 +00001407 const llvm::Type* ElemTy = $4->get();
1408 if ((unsigned)$2 != $2)
1409 GEN_ERROR("Unsigned result not equal to signed result");
Chris Lattner42a75512007-01-15 02:27:26 +00001410 if (!ElemTy->isFloatingPoint() && !ElemTy->isInteger())
Reid Spencer9d6565a2007-02-15 02:26:10 +00001411 GEN_ERROR("Element type of a VectorType must be primitive");
Reid Spencer9d6565a2007-02-15 02:26:10 +00001412 $$ = new PATypeHolder(HandleUpRefs(VectorType::get(*$4, (unsigned)$2)));
Reid Spencer08de34b2006-12-03 05:45:44 +00001413 delete $4;
1414 CHECK_FOR_ERROR
Brian Gaeke715c90b2004-08-20 06:00:58 +00001415 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001416 | '{' TypeListI '}' { // Structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001417 std::vector<const Type*> Elements;
Reid Spencer08de34b2006-12-03 05:45:44 +00001418 for (std::list<llvm::PATypeHolder>::iterator I = $2->begin(),
Chris Lattnera08976b2005-02-22 23:13:58 +00001419 E = $2->end(); I != E; ++I)
Reid Spencer08de34b2006-12-03 05:45:44 +00001420 Elements.push_back(*I);
Misha Brukman5a2a3822005-05-10 22:02:28 +00001421
Reid Spencer08de34b2006-12-03 05:45:44 +00001422 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001423 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001424 CHECK_FOR_ERROR
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001425 }
1426 | '{' '}' { // Empty structure type?
Reid Spencer08de34b2006-12-03 05:45:44 +00001427 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Reid Spencer61c83e02006-08-18 08:43:06 +00001428 CHECK_FOR_ERROR
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001429 }
Andrew Lenharth38ecbf12006-12-08 18:06:16 +00001430 | '<' '{' TypeListI '}' '>' {
1431 std::vector<const Type*> Elements;
1432 for (std::list<llvm::PATypeHolder>::iterator I = $3->begin(),
1433 E = $3->end(); I != E; ++I)
1434 Elements.push_back(*I);
1435
1436 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements, true)));
1437 delete $3;
1438 CHECK_FOR_ERROR
1439 }
1440 | '<' '{' '}' '>' { // Empty structure type?
1441 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>(), true));
1442 CHECK_FOR_ERROR
1443 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001444 ;
1445
1446ArgType
Duncan Sandsdc024672007-11-27 13:23:08 +00001447 : Types OptParamAttrs {
1448 // Allow but ignore attributes on function types; this permits auto-upgrade.
1449 // FIXME: remove in LLVM 3.0.
Reid Spencere1553cc2006-12-31 05:40:12 +00001450 $$.Ty = $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00001451 $$.Attrs = ParamAttr::None;
Reid Spencere1553cc2006-12-31 05:40:12 +00001452 }
1453 ;
1454
Reid Spencer2c261782007-01-05 17:06:19 +00001455ResultTypes
1456 : Types {
Reid Spencere1553cc2006-12-31 05:40:12 +00001457 if (!UpRefs.empty())
1458 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1459 if (!(*$1)->isFirstClassType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001460 GEN_ERROR("LLVM functions cannot return aggregate types");
Reid Spencer2c261782007-01-05 17:06:19 +00001461 $$ = $1;
Reid Spencere1553cc2006-12-31 05:40:12 +00001462 }
Reid Spencer2c261782007-01-05 17:06:19 +00001463 | VOID {
1464 $$ = new PATypeHolder(Type::VoidTy);
Reid Spencere1553cc2006-12-31 05:40:12 +00001465 }
1466 ;
1467
1468ArgTypeList : ArgType {
1469 $$ = new TypeWithAttrsList();
1470 $$->push_back($1);
1471 CHECK_FOR_ERROR
1472 }
1473 | ArgTypeList ',' ArgType {
1474 ($$=$1)->push_back($3);
1475 CHECK_FOR_ERROR
1476 }
1477 ;
1478
1479ArgTypeListI
1480 : ArgTypeList
1481 | ArgTypeList ',' DOTDOTDOT {
1482 $$=$1;
Reid Spencer18da0722007-04-11 02:44:20 +00001483 TypeWithAttrs TWA; TWA.Attrs = ParamAttr::None;
Reid Spencere1553cc2006-12-31 05:40:12 +00001484 TWA.Ty = new PATypeHolder(Type::VoidTy);
1485 $$->push_back(TWA);
1486 CHECK_FOR_ERROR
1487 }
1488 | DOTDOTDOT {
1489 $$ = new TypeWithAttrsList;
Reid Spencer18da0722007-04-11 02:44:20 +00001490 TypeWithAttrs TWA; TWA.Attrs = ParamAttr::None;
Reid Spencere1553cc2006-12-31 05:40:12 +00001491 TWA.Ty = new PATypeHolder(Type::VoidTy);
1492 $$->push_back(TWA);
1493 CHECK_FOR_ERROR
1494 }
1495 | /*empty*/ {
1496 $$ = new TypeWithAttrsList();
Reid Spencer61c83e02006-08-18 08:43:06 +00001497 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001498 };
Chris Lattner30c89792001-09-07 16:35:17 +00001499
Chris Lattner7e708292002-06-25 16:13:24 +00001500// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +00001501// declaration type lists
1502//
Reid Spencere1553cc2006-12-31 05:40:12 +00001503TypeListI : Types {
Reid Spencer08de34b2006-12-03 05:45:44 +00001504 $$ = new std::list<PATypeHolder>();
Reid Spencer2c9df212007-03-20 01:13:00 +00001505 $$->push_back(*$1);
1506 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001507 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001508 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001509 | TypeListI ',' Types {
Reid Spencer2c9df212007-03-20 01:13:00 +00001510 ($$=$1)->push_back(*$3);
1511 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001512 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001513 };
Chris Lattner30c89792001-09-07 16:35:17 +00001514
Chris Lattnere98dda62001-07-14 06:10:16 +00001515// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +00001516// production is used ONLY to represent constants that show up AFTER a 'const',
1517// 'constant' or 'global' token at global scope. Constants that can be inlined
1518// into other expressions (such as integers and constexprs) are handled by the
1519// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +00001520//
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001521ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencere1553cc2006-12-31 05:40:12 +00001522 if (!UpRefs.empty())
1523 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001524 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001525 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001526 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001527 (*$1)->getDescription() + "'");
Chris Lattner30c89792001-09-07 16:35:17 +00001528 const Type *ETy = ATy->getElementType();
1529 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +00001530
Chris Lattner30c89792001-09-07 16:35:17 +00001531 // Verify that we have the correct size...
1532 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001533 GEN_ERROR("Type mismatch: constant sized array initialized with " +
Reid Spencer3271ed52004-07-18 00:08:11 +00001534 utostr($3->size()) + " arguments, but has size of " +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001535 itostr(NumElements) + "");
Chris Lattner00950542001-06-06 20:29:01 +00001536
Chris Lattner30c89792001-09-07 16:35:17 +00001537 // Verify all elements are correct type!
1538 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencer08de34b2006-12-03 05:45:44 +00001539 if (ETy != (*$3)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001540 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Reid Spencer3271ed52004-07-18 00:08:11 +00001541 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencer08de34b2006-12-03 05:45:44 +00001542 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +00001543 }
1544
Reid Spencer08de34b2006-12-03 05:45:44 +00001545 $$ = ConstantArray::get(ATy, *$3);
1546 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001547 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001548 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001549 | Types '[' ']' {
Reid Spencere1553cc2006-12-31 05:40:12 +00001550 if (!UpRefs.empty())
1551 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001552 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001553 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001554 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001555 (*$1)->getDescription() + "'");
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001556
1557 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +00001558 if (NumElements != -1 && NumElements != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001559 GEN_ERROR("Type mismatch: constant sized array initialized with 0"
Reid Spencerf4fa5902007-02-05 10:16:10 +00001560 " arguments, but has size of " + itostr(NumElements) +"");
Reid Spencer08de34b2006-12-03 05:45:44 +00001561 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
1562 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001563 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001564 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001565 | Types 'c' STRINGCONSTANT {
Reid Spencere1553cc2006-12-31 05:40:12 +00001566 if (!UpRefs.empty())
1567 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001568 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001569 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001570 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001571 (*$1)->getDescription() + "'");
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001572
Chris Lattner30c89792001-09-07 16:35:17 +00001573 int NumElements = ATy->getNumElements();
1574 const Type *ETy = ATy->getElementType();
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001575 if (NumElements != -1 && NumElements != int($3->length()))
Reid Spencer61c83e02006-08-18 08:43:06 +00001576 GEN_ERROR("Can't build string constant of size " +
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001577 itostr((int)($3->length())) +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001578 " when array has size " + itostr(NumElements) + "");
Chris Lattner9232b992003-04-22 18:42:41 +00001579 std::vector<Constant*> Vals;
Reid Spencere1553cc2006-12-31 05:40:12 +00001580 if (ETy == Type::Int8Ty) {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001581 for (unsigned i = 0; i < $3->length(); ++i)
1582 Vals.push_back(ConstantInt::get(ETy, (*$3)[i]));
Chris Lattner93750fa2001-07-28 17:48:55 +00001583 } else {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001584 delete $3;
Reid Spencerf4fa5902007-02-05 10:16:10 +00001585 GEN_ERROR("Cannot build string arrays of non byte sized elements");
Chris Lattner93750fa2001-07-28 17:48:55 +00001586 }
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001587 delete $3;
Reid Spencer08de34b2006-12-03 05:45:44 +00001588 $$ = ConstantArray::get(ATy, Vals);
1589 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001590 CHECK_FOR_ERROR
Chris Lattner93750fa2001-07-28 17:48:55 +00001591 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00001592 | Types '<' ConstVector '>' { // Nonempty unsized arr
Reid Spencere1553cc2006-12-31 05:40:12 +00001593 if (!UpRefs.empty())
1594 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer9d6565a2007-02-15 02:26:10 +00001595 const VectorType *PTy = dyn_cast<VectorType>($1->get());
Brian Gaeke715c90b2004-08-20 06:00:58 +00001596 if (PTy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001597 GEN_ERROR("Cannot make packed constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001598 (*$1)->getDescription() + "'");
Brian Gaeke715c90b2004-08-20 06:00:58 +00001599 const Type *ETy = PTy->getElementType();
1600 int NumElements = PTy->getNumElements();
1601
1602 // Verify that we have the correct size...
1603 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001604 GEN_ERROR("Type mismatch: constant sized packed initialized with " +
Brian Gaeke715c90b2004-08-20 06:00:58 +00001605 utostr($3->size()) + " arguments, but has size of " +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001606 itostr(NumElements) + "");
Brian Gaeke715c90b2004-08-20 06:00:58 +00001607
1608 // Verify all elements are correct type!
1609 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencer08de34b2006-12-03 05:45:44 +00001610 if (ETy != (*$3)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001611 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Brian Gaeke715c90b2004-08-20 06:00:58 +00001612 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencer08de34b2006-12-03 05:45:44 +00001613 (*$3)[i]->getType()->getDescription() + "'.");
Brian Gaeke715c90b2004-08-20 06:00:58 +00001614 }
1615
Reid Spencer9d6565a2007-02-15 02:26:10 +00001616 $$ = ConstantVector::get(PTy, *$3);
Reid Spencer08de34b2006-12-03 05:45:44 +00001617 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001618 CHECK_FOR_ERROR
Brian Gaeke715c90b2004-08-20 06:00:58 +00001619 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001620 | Types '{' ConstVector '}' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001621 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001622 if (STy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001623 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001624 (*$1)->getDescription() + "'");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001625
Chris Lattnerc6212f12003-05-21 16:06:56 +00001626 if ($3->size() != STy->getNumContainedTypes())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001627 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattnerc6212f12003-05-21 16:06:56 +00001628
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001629 // Check to ensure that constants are compatible with the type initializer!
1630 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Reid Spencer08de34b2006-12-03 05:45:44 +00001631 if ((*$3)[i]->getType() != STy->getElementType(i))
Reid Spencer61c83e02006-08-18 08:43:06 +00001632 GEN_ERROR("Expected type '" +
Chris Lattnerd21cd802004-02-09 04:37:31 +00001633 STy->getElementType(i)->getDescription() +
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001634 "' for element #" + utostr(i) +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001635 " of structure initializer");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001636
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001637 // Check to ensure that Type is not packed
1638 if (STy->isPacked())
Chris Lattner4989b842007-04-26 05:30:35 +00001639 GEN_ERROR("Unpacked Initializer to vector type '" +
1640 STy->getDescription() + "'");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001641
Reid Spencer08de34b2006-12-03 05:45:44 +00001642 $$ = ConstantStruct::get(STy, *$3);
1643 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001644 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001645 }
Chris Lattnerc6212f12003-05-21 16:06:56 +00001646 | Types '{' '}' {
Reid Spencere1553cc2006-12-31 05:40:12 +00001647 if (!UpRefs.empty())
1648 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001649 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerc6212f12003-05-21 16:06:56 +00001650 if (STy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001651 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001652 (*$1)->getDescription() + "'");
Chris Lattnerc6212f12003-05-21 16:06:56 +00001653
1654 if (STy->getNumContainedTypes() != 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001655 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattnerc6212f12003-05-21 16:06:56 +00001656
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001657 // Check to ensure that Type is not packed
1658 if (STy->isPacked())
Chris Lattner4989b842007-04-26 05:30:35 +00001659 GEN_ERROR("Unpacked Initializer to vector type '" +
1660 STy->getDescription() + "'");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001661
1662 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1663 delete $1;
1664 CHECK_FOR_ERROR
1665 }
1666 | Types '<' '{' ConstVector '}' '>' {
1667 const StructType *STy = dyn_cast<StructType>($1->get());
1668 if (STy == 0)
1669 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001670 (*$1)->getDescription() + "'");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001671
1672 if ($4->size() != STy->getNumContainedTypes())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001673 GEN_ERROR("Illegal number of initializers for structure type");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001674
1675 // Check to ensure that constants are compatible with the type initializer!
1676 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1677 if ((*$4)[i]->getType() != STy->getElementType(i))
1678 GEN_ERROR("Expected type '" +
1679 STy->getElementType(i)->getDescription() +
1680 "' for element #" + utostr(i) +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001681 " of structure initializer");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001682
1683 // Check to ensure that Type is packed
1684 if (!STy->isPacked())
Reid Spencerac9dcb92007-02-15 03:39:18 +00001685 GEN_ERROR("Vector initializer to non-vector type '" +
1686 STy->getDescription() + "'");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001687
1688 $$ = ConstantStruct::get(STy, *$4);
1689 delete $1; delete $4;
1690 CHECK_FOR_ERROR
1691 }
1692 | Types '<' '{' '}' '>' {
1693 if (!UpRefs.empty())
1694 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1695 const StructType *STy = dyn_cast<StructType>($1->get());
1696 if (STy == 0)
1697 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001698 (*$1)->getDescription() + "'");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001699
1700 if (STy->getNumContainedTypes() != 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001701 GEN_ERROR("Illegal number of initializers for structure type");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001702
1703 // Check to ensure that Type is packed
1704 if (!STy->isPacked())
Reid Spencerac9dcb92007-02-15 03:39:18 +00001705 GEN_ERROR("Vector initializer to non-vector type '" +
1706 STy->getDescription() + "'");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001707
Reid Spencer08de34b2006-12-03 05:45:44 +00001708 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1709 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001710 CHECK_FOR_ERROR
Chris Lattnerc6212f12003-05-21 16:06:56 +00001711 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001712 | Types NULL_TOK {
Reid Spencere1553cc2006-12-31 05:40:12 +00001713 if (!UpRefs.empty())
1714 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001715 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001716 if (PTy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001717 GEN_ERROR("Cannot make null pointer constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001718 (*$1)->getDescription() + "'");
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001719
Reid Spencer08de34b2006-12-03 05:45:44 +00001720 $$ = ConstantPointerNull::get(PTy);
1721 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001722 CHECK_FOR_ERROR
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001723 }
Chris Lattner16710e92004-10-16 18:17:13 +00001724 | Types UNDEF {
Reid Spencere1553cc2006-12-31 05:40:12 +00001725 if (!UpRefs.empty())
1726 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001727 $$ = UndefValue::get($1->get());
1728 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001729 CHECK_FOR_ERROR
Chris Lattner16710e92004-10-16 18:17:13 +00001730 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001731 | Types SymbolicValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00001732 if (!UpRefs.empty())
1733 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001734 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001735 if (Ty == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001736 GEN_ERROR("Global const reference must be a pointer type");
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001737
Chris Lattner3101c252002-08-15 17:58:33 +00001738 // ConstExprs can exist in the body of a function, thus creating
Reid Spencer3271ed52004-07-18 00:08:11 +00001739 // GlobalValues whenever they refer to a variable. Because we are in
Reid Spencer186a43f2007-03-19 18:39:36 +00001740 // the context of a function, getExistingVal will search the functions
Chris Lattner3101c252002-08-15 17:58:33 +00001741 // symbol table instead of the module symbol table for the global symbol,
1742 // which throws things all off. To get around this, we just tell
Reid Spencer186a43f2007-03-19 18:39:36 +00001743 // getExistingVal that we are at global scope here.
Chris Lattner3101c252002-08-15 17:58:33 +00001744 //
Chris Lattner394f2eb2003-10-16 20:12:13 +00001745 Function *SavedCurFn = CurFun.CurrentFunction;
1746 CurFun.CurrentFunction = 0;
Chris Lattner3101c252002-08-15 17:58:33 +00001747
Reid Spencer186a43f2007-03-19 18:39:36 +00001748 Value *V = getExistingVal(Ty, $2);
Reid Spencer5b7e7532006-09-28 19:28:24 +00001749 CHECK_FOR_ERROR
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001750
Chris Lattner394f2eb2003-10-16 20:12:13 +00001751 CurFun.CurrentFunction = SavedCurFn;
Chris Lattner3101c252002-08-15 17:58:33 +00001752
Chris Lattner2079fde2001-10-13 06:41:08 +00001753 // If this is an initializer for a constant pointer, which is referencing a
1754 // (currently) undefined variable, create a stub now that shall be replaced
1755 // in the future with the right type of variable.
1756 //
1757 if (V == 0) {
Reid Spencer1755a9a2007-02-05 17:01:20 +00001758 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001759 const PointerType *PT = cast<PointerType>(Ty);
1760
1761 // First check to see if the forward references value is already created!
1762 PerModuleInfo::GlobalRefsType::iterator I =
Reid Spencer3271ed52004-07-18 00:08:11 +00001763 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Chris Lattner2079fde2001-10-13 06:41:08 +00001764
1765 if (I != CurModule.GlobalRefs.end()) {
Reid Spencer3271ed52004-07-18 00:08:11 +00001766 V = I->second; // Placeholder already exists, use it...
Chris Lattnera989b232003-12-23 20:05:15 +00001767 $2.destroy();
Chris Lattner2079fde2001-10-13 06:41:08 +00001768 } else {
Chris Lattner8a327842004-07-14 21:44:00 +00001769 std::string Name;
Reid Spencerb2d17862007-01-26 08:04:51 +00001770 if ($2.Type == ValID::GlobalName)
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001771 Name = $2.getName();
Reid Spencerb2d17862007-01-26 08:04:51 +00001772 else if ($2.Type != ValID::GlobalID)
1773 GEN_ERROR("Invalid reference to global");
Chris Lattner8a327842004-07-14 21:44:00 +00001774
Reid Spencer3271ed52004-07-18 00:08:11 +00001775 // Create the forward referenced global.
Chris Lattnera09000d2004-07-14 23:03:46 +00001776 GlobalValue *GV;
1777 if (const FunctionType *FTy =
1778 dyn_cast<FunctionType>(PT->getElementType())) {
Chris Lattner4989b842007-04-26 05:30:35 +00001779 GV = new Function(FTy, GlobalValue::ExternalWeakLinkage, Name,
Chris Lattnera09000d2004-07-14 23:03:46 +00001780 CurModule.CurrentModule);
1781 } else {
Reid Spencer3271ed52004-07-18 00:08:11 +00001782 GV = new GlobalVariable(PT->getElementType(), false,
Chris Lattner4989b842007-04-26 05:30:35 +00001783 GlobalValue::ExternalWeakLinkage, 0,
Chris Lattnera09000d2004-07-14 23:03:46 +00001784 Name, CurModule.CurrentModule);
1785 }
Chris Lattner8a327842004-07-14 21:44:00 +00001786
Reid Spencer3271ed52004-07-18 00:08:11 +00001787 // Keep track of the fact that we have a forward ref to recycle it
1788 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1789 V = GV;
Chris Lattner2079fde2001-10-13 06:41:08 +00001790 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001791 }
1792
Reid Spencer08de34b2006-12-03 05:45:44 +00001793 $$ = cast<GlobalValue>(V);
1794 delete $1; // Free the type handle
Reid Spencer61c83e02006-08-18 08:43:06 +00001795 CHECK_FOR_ERROR
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001796 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001797 | Types ConstExpr {
Reid Spencere1553cc2006-12-31 05:40:12 +00001798 if (!UpRefs.empty())
1799 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001800 if ($1->get() != $2->getType())
Reid Spencerb4fdfdb2007-01-04 00:05:48 +00001801 GEN_ERROR("Mismatched types for constant expression: " +
1802 (*$1)->getDescription() + " and " + $2->getType()->getDescription());
Chris Lattnerd78700d2002-08-16 21:14:40 +00001803 $$ = $2;
Reid Spencer08de34b2006-12-03 05:45:44 +00001804 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001805 CHECK_FOR_ERROR
Chris Lattnerf4600482003-06-28 20:01:34 +00001806 }
1807 | Types ZEROINITIALIZER {
Reid Spencere1553cc2006-12-31 05:40:12 +00001808 if (!UpRefs.empty())
1809 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001810 const Type *Ty = $1->get();
Chris Lattner78915932004-11-28 16:45:45 +00001811 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001812 GEN_ERROR("Cannot create a null initialized value of this type");
Reid Spencer08de34b2006-12-03 05:45:44 +00001813 $$ = Constant::getNullValue(Ty);
1814 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001815 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00001816 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001817 | IntType ESINT64VAL { // integral constants
Reid Spencer9ffad0a2006-12-20 17:20:09 +00001818 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001819 GEN_ERROR("Constant value doesn't fit in type");
Reid Spencereac65742007-03-19 20:40:22 +00001820 $$ = ConstantInt::get($1, $2, true);
Reid Spencerf2310042007-02-28 02:23:44 +00001821 CHECK_FOR_ERROR
1822 }
1823 | IntType ESAPINTVAL { // arbitrary precision integer constants
1824 uint32_t BitWidth = cast<IntegerType>($1)->getBitWidth();
1825 if ($2->getBitWidth() > BitWidth) {
1826 GEN_ERROR("Constant value does not fit in type");
Reid Spenceraf1aacd2007-03-01 19:32:01 +00001827 }
1828 $2->sextOrTrunc(BitWidth);
1829 $$ = ConstantInt::get(*$2);
Reid Spencerf2310042007-02-28 02:23:44 +00001830 delete $2;
Reid Spencer9ffad0a2006-12-20 17:20:09 +00001831 CHECK_FOR_ERROR
1832 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001833 | IntType EUINT64VAL { // integral constants
Reid Spencer9ffad0a2006-12-20 17:20:09 +00001834 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001835 GEN_ERROR("Constant value doesn't fit in type");
Reid Spencereac65742007-03-19 20:40:22 +00001836 $$ = ConstantInt::get($1, $2, false);
Reid Spencerf2310042007-02-28 02:23:44 +00001837 CHECK_FOR_ERROR
1838 }
1839 | IntType EUAPINTVAL { // arbitrary precision integer constants
1840 uint32_t BitWidth = cast<IntegerType>($1)->getBitWidth();
1841 if ($2->getBitWidth() > BitWidth) {
1842 GEN_ERROR("Constant value does not fit in type");
Reid Spenceraf1aacd2007-03-01 19:32:01 +00001843 }
1844 $2->zextOrTrunc(BitWidth);
1845 $$ = ConstantInt::get(*$2);
Reid Spencerf2310042007-02-28 02:23:44 +00001846 delete $2;
Reid Spencer9ffad0a2006-12-20 17:20:09 +00001847 CHECK_FOR_ERROR
1848 }
Reid Spencer8088e9d2007-01-13 05:00:20 +00001849 | INTTYPE TRUETOK { // Boolean constants
1850 assert(cast<IntegerType>($1)->getBitWidth() == 1 && "Not Bool?");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001851 $$ = ConstantInt::getTrue();
Reid Spencer61c83e02006-08-18 08:43:06 +00001852 CHECK_FOR_ERROR
Chris Lattnerd05e3592002-08-15 18:17:28 +00001853 }
Reid Spencer8088e9d2007-01-13 05:00:20 +00001854 | INTTYPE FALSETOK { // Boolean constants
1855 assert(cast<IntegerType>($1)->getBitWidth() == 1 && "Not Bool?");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001856 $$ = ConstantInt::getFalse();
Reid Spencer61c83e02006-08-18 08:43:06 +00001857 CHECK_FOR_ERROR
Chris Lattnerd05e3592002-08-15 18:17:28 +00001858 }
Dale Johannesen9d5f4562007-09-12 03:30:33 +00001859 | FPType FPVAL { // Floating point constants
Dale Johannesen43421b32007-09-06 18:13:44 +00001860 if (!ConstantFP::isValueValidForType($1, *$2))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001861 GEN_ERROR("Floating point constant invalid for type");
Dale Johannesen3f6eb742007-09-11 18:32:33 +00001862 // Lexer has no type info, so builds all float and double FP constants
1863 // as double. Fix this here. Long double is done right.
1864 if (&$2->getSemantics()==&APFloat::IEEEdouble && $1==Type::FloatTy)
Dale Johannesen43421b32007-09-06 18:13:44 +00001865 $2->convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven);
1866 $$ = ConstantFP::get($1, *$2);
Dale Johannesencdd509a2007-09-07 21:07:57 +00001867 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001868 CHECK_FOR_ERROR
Chris Lattnerd05e3592002-08-15 18:17:28 +00001869 };
1870
Chris Lattner00950542001-06-06 20:29:01 +00001871
Reid Spencer3da59db2006-11-27 01:05:10 +00001872ConstExpr: CastOps '(' ConstVal TO Types ')' {
Reid Spencere1553cc2006-12-31 05:40:12 +00001873 if (!UpRefs.empty())
1874 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001875 Constant *Val = $3;
Reid Spencer93947c32007-01-17 02:47:33 +00001876 const Type *DestTy = $5->get();
1877 if (!CastInst::castIsValid($1, $3, DestTy))
1878 GEN_ERROR("invalid cast opcode for cast from '" +
1879 Val->getType()->getDescription() + "' to '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001880 DestTy->getDescription() + "'");
Reid Spencer93947c32007-01-17 02:47:33 +00001881 $$ = ConstantExpr::getCast($1, $3, DestTy);
Reid Spencer08de34b2006-12-03 05:45:44 +00001882 delete $5;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001883 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001884 | GETELEMENTPTR '(' ConstVal IndexList ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001885 if (!isa<PointerType>($3->getType()))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001886 GEN_ERROR("GetElementPtr requires a pointer operand");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001887
Reid Spencer08de34b2006-12-03 05:45:44 +00001888 const Type *IdxTy =
David Greeneb8f74792007-09-04 15:46:09 +00001889 GetElementPtrInst::getIndexedType($3->getType(), $4->begin(), $4->end(),
Chris Lattnerc856c7a2007-02-13 00:57:40 +00001890 true);
Reid Spencer08de34b2006-12-03 05:45:44 +00001891 if (!IdxTy)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001892 GEN_ERROR("Index list invalid for constant getelementptr");
Reid Spencer08de34b2006-12-03 05:45:44 +00001893
Chris Lattnere0135402007-01-31 04:43:46 +00001894 SmallVector<Constant*, 8> IdxVec;
Reid Spencer08de34b2006-12-03 05:45:44 +00001895 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1896 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001897 IdxVec.push_back(C);
1898 else
Reid Spencerf4fa5902007-02-05 10:16:10 +00001899 GEN_ERROR("Indices to constant getelementptr must be constants");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001900
Chris Lattnerd78700d2002-08-16 21:14:40 +00001901 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001902
Chris Lattnere0135402007-01-31 04:43:46 +00001903 $$ = ConstantExpr::getGetElementPtr($3, &IdxVec[0], IdxVec.size());
Reid Spencer61c83e02006-08-18 08:43:06 +00001904 CHECK_FOR_ERROR
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001905 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001906 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer4fe16d62007-01-11 18:21:29 +00001907 if ($3->getType() != Type::Int1Ty)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001908 GEN_ERROR("Select condition must be of boolean type");
Reid Spencer08de34b2006-12-03 05:45:44 +00001909 if ($5->getType() != $7->getType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001910 GEN_ERROR("Select operand types must match");
Reid Spencer08de34b2006-12-03 05:45:44 +00001911 $$ = ConstantExpr::getSelect($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001912 CHECK_FOR_ERROR
Chris Lattner76f0ff32004-03-12 05:51:36 +00001913 }
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001914 | ArithmeticOps '(' 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("Binary operator types must match");
Reid Spencer1628cec2006-10-26 06:15:43 +00001917 CHECK_FOR_ERROR;
Reid Spencerc6e956e2006-12-05 19:15:41 +00001918 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001919 }
1920 | LogicalOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001921 if ($3->getType() != $5->getType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001922 GEN_ERROR("Logical operator types must match");
Chris Lattner42a75512007-01-15 02:27:26 +00001923 if (!$3->getType()->isInteger()) {
Reid Spencer9d6565a2007-02-15 02:26:10 +00001924 if (Instruction::isShift($1) || !isa<VectorType>($3->getType()) ||
1925 !cast<VectorType>($3->getType())->getElementType()->isInteger())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001926 GEN_ERROR("Logical operator requires integral operands");
Chris Lattner0a017832005-12-21 18:31:29 +00001927 }
Reid Spencer08de34b2006-12-03 05:45:44 +00001928 $$ = ConstantExpr::get($1, $3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001929 CHECK_FOR_ERROR
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001930 }
Reid Spencer763ed5e2006-12-04 05:20:06 +00001931 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
1932 if ($4->getType() != $6->getType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001933 GEN_ERROR("icmp operand types must match");
Reid Spencer763ed5e2006-12-04 05:20:06 +00001934 $$ = ConstantExpr::getICmp($2, $4, $6);
Reid Spencer08de34b2006-12-03 05:45:44 +00001935 }
Reid Spencer763ed5e2006-12-04 05:20:06 +00001936 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
1937 if ($4->getType() != $6->getType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001938 GEN_ERROR("fcmp operand types must match");
Reid Spencer763ed5e2006-12-04 05:20:06 +00001939 $$ = ConstantExpr::getFCmp($2, $4, $6);
Reid Spencer08de34b2006-12-03 05:45:44 +00001940 }
Robert Bocchino9c62b562006-01-10 19:04:32 +00001941 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001942 if (!ExtractElementInst::isValidOperands($3, $5))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001943 GEN_ERROR("Invalid extractelement operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00001944 $$ = ConstantExpr::getExtractElement($3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001945 CHECK_FOR_ERROR
Chris Lattnerca73cd82006-04-08 03:53:34 +00001946 }
1947 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001948 if (!InsertElementInst::isValidOperands($3, $5, $7))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001949 GEN_ERROR("Invalid insertelement operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00001950 $$ = ConstantExpr::getInsertElement($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001951 CHECK_FOR_ERROR
Chris Lattnerca73cd82006-04-08 03:53:34 +00001952 }
1953 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001954 if (!ShuffleVectorInst::isValidOperands($3, $5, $7))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001955 GEN_ERROR("Invalid shufflevector operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00001956 $$ = ConstantExpr::getShuffleVector($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001957 CHECK_FOR_ERROR
Chris Lattner699f1eb2002-08-14 17:12:33 +00001958 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001959
Chris Lattnerca73cd82006-04-08 03:53:34 +00001960
Misha Brukmanbc0e9982003-07-14 17:20:40 +00001961// ConstVector - A list of comma separated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001962ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001963 ($$ = $1)->push_back($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00001964 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001965 }
1966 | ConstVal {
Reid Spencer08de34b2006-12-03 05:45:44 +00001967 $$ = new std::vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001968 $$->push_back($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001969 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001970 };
Chris Lattner00950542001-06-06 20:29:01 +00001971
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001972
Chris Lattner1781aca2001-09-18 04:00:54 +00001973// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001974GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001975
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00001976// ThreadLocal
1977ThreadLocal : THREAD_LOCAL { $$ = true; } | { $$ = false; };
1978
Anton Korobeynikova80e1182007-04-28 13:45:00 +00001979// AliaseeRef - Match either GlobalValue or bitcast to GlobalValue.
1980AliaseeRef : ResultTypes SymbolicValueRef {
1981 const Type* VTy = $1->get();
1982 Value *V = getVal(VTy, $2);
Chris Lattner6df20ef2007-08-06 21:00:37 +00001983 CHECK_FOR_ERROR
Anton Korobeynikova80e1182007-04-28 13:45:00 +00001984 GlobalValue* Aliasee = dyn_cast<GlobalValue>(V);
1985 if (!Aliasee)
1986 GEN_ERROR("Aliases can be created only to global values");
1987
1988 $$ = Aliasee;
1989 CHECK_FOR_ERROR
1990 delete $1;
1991 }
1992 | BITCAST '(' AliaseeRef TO Types ')' {
1993 Constant *Val = $3;
1994 const Type *DestTy = $5->get();
1995 if (!CastInst::castIsValid($1, $3, DestTy))
1996 GEN_ERROR("invalid cast opcode for cast from '" +
1997 Val->getType()->getDescription() + "' to '" +
1998 DestTy->getDescription() + "'");
1999
2000 $$ = ConstantExpr::getCast($1, $3, DestTy);
2001 CHECK_FOR_ERROR
2002 delete $5;
2003 };
Chris Lattner00950542001-06-06 20:29:01 +00002004
Chris Lattner0e73ce62002-05-02 19:11:13 +00002005//===----------------------------------------------------------------------===//
2006// Rules to match Modules
2007//===----------------------------------------------------------------------===//
2008
2009// Module rule: Capture the result of parsing the whole file into a result
2010// variable...
2011//
Reid Spencerb951bc02006-12-29 20:29:48 +00002012Module
2013 : DefinitionList {
2014 $$ = ParserResult = CurModule.CurrentModule;
2015 CurModule.ModuleDone();
2016 CHECK_FOR_ERROR;
2017 }
2018 | /*empty*/ {
2019 $$ = ParserResult = CurModule.CurrentModule;
2020 CurModule.ModuleDone();
2021 CHECK_FOR_ERROR;
2022 }
2023 ;
Chris Lattner0e73ce62002-05-02 19:11:13 +00002024
Reid Spencerb951bc02006-12-29 20:29:48 +00002025DefinitionList
2026 : Definition
2027 | DefinitionList Definition
2028 ;
2029
2030Definition
Jeff Cohen361c3ef2007-01-21 19:19:31 +00002031 : DEFINE { CurFun.isDeclare = false; } Function {
Chris Lattner394f2eb2003-10-16 20:12:13 +00002032 CurFun.FunctionDone();
Reid Spencer61c83e02006-08-18 08:43:06 +00002033 CHECK_FOR_ERROR
Reid Spencerb951bc02006-12-29 20:29:48 +00002034 }
2035 | DECLARE { CurFun.isDeclare = true; } FunctionProto {
Reid Spencer61c83e02006-08-18 08:43:06 +00002036 CHECK_FOR_ERROR
Chris Lattner0e73ce62002-05-02 19:11:13 +00002037 }
Reid Spencerb951bc02006-12-29 20:29:48 +00002038 | MODULE ASM_TOK AsmBlock {
Reid Spencer61c83e02006-08-18 08:43:06 +00002039 CHECK_FOR_ERROR
Chris Lattneree454772006-01-23 23:05:15 +00002040 }
Reid Spencerb2d17862007-01-26 08:04:51 +00002041 | OptLocalAssign TYPE Types {
Reid Spencere1553cc2006-12-31 05:40:12 +00002042 if (!UpRefs.empty())
2043 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattner4a42e902001-10-22 05:56:09 +00002044 // Eagerly resolve types. This is not an optimization, this is a
2045 // requirement that is due to the fact that we could have this:
2046 //
2047 // %list = type { %list * }
2048 // %list = type { %list * } ; repeated type decl
2049 //
2050 // If types are not resolved eagerly, then the two types will not be
2051 // determined to be the same type!
2052 //
Reid Spencerb951bc02006-12-29 20:29:48 +00002053 ResolveTypeTo($1, *$3);
Chris Lattner4a42e902001-10-22 05:56:09 +00002054
Reid Spencerb951bc02006-12-29 20:29:48 +00002055 if (!setTypeName(*$3, $1) && !$1) {
Reid Spencer5b7e7532006-09-28 19:28:24 +00002056 CHECK_FOR_ERROR
Chris Lattner8c89a0a2004-07-13 08:10:10 +00002057 // If this is a named type that is not a redefinition, add it to the slot
2058 // table.
Reid Spencerb951bc02006-12-29 20:29:48 +00002059 CurModule.Types.push_back(*$3);
Chris Lattner30c89792001-09-07 16:35:17 +00002060 }
Reid Spencer08de34b2006-12-03 05:45:44 +00002061
Reid Spencerb951bc02006-12-29 20:29:48 +00002062 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002063 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00002064 }
Reid Spencerb2d17862007-01-26 08:04:51 +00002065 | OptLocalAssign TYPE VOID {
Reid Spencere1553cc2006-12-31 05:40:12 +00002066 ResolveTypeTo($1, $3);
2067
2068 if (!setTypeName($3, $1) && !$1) {
2069 CHECK_FOR_ERROR
2070 // If this is a named type that is not a redefinition, add it to the slot
2071 // table.
2072 CurModule.Types.push_back($3);
2073 }
2074 CHECK_FOR_ERROR
2075 }
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002076 | OptGlobalAssign GVVisibilityStyle ThreadLocal GlobalType ConstVal {
Reid Spencerb2d17862007-01-26 08:04:51 +00002077 /* "Externally Visible" Linkage */
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002078 if ($5 == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002079 GEN_ERROR("Global value initializer is not a constant");
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002080 CurGV = ParseGlobalVariable($1, GlobalValue::ExternalLinkage,
2081 $2, $4, $5->getType(), $5, $3);
Reid Spencerb951bc02006-12-29 20:29:48 +00002082 CHECK_FOR_ERROR
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002083 } GlobalVarAttributes {
2084 CurGV = 0;
2085 }
Christopher Lambfe63fb92007-12-11 08:59:05 +00002086 | OptGlobalAssign GVVisibilityStyle ThreadLocal GlobalType ConstVal
2087 ADDRSPACE '(' EUINT64VAL ')' {
2088 /* "Externally Visible" Linkage with address space qualifier */
2089 if ($5 == 0)
2090 GEN_ERROR("Global value initializer is not a constant");
2091 CurGV = ParseGlobalVariable($1, GlobalValue::ExternalLinkage,
2092 $2, $4, $5->getType(), $5, $3, $8);
2093 CHECK_FOR_ERROR
2094 } GlobalVarAttributes {
2095 CurGV = 0;
2096 }
Chris Lattner4989b842007-04-26 05:30:35 +00002097 | OptGlobalAssign GVInternalLinkage GVVisibilityStyle ThreadLocal GlobalType
2098 ConstVal {
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002099 if ($6 == 0)
2100 GEN_ERROR("Global value initializer is not a constant");
2101 CurGV = ParseGlobalVariable($1, $2, $3, $5, $6->getType(), $6, $4);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002102 CHECK_FOR_ERROR
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002103 } GlobalVarAttributes {
2104 CurGV = 0;
2105 }
Chris Lattner4989b842007-04-26 05:30:35 +00002106 | OptGlobalAssign GVExternalLinkage GVVisibilityStyle ThreadLocal GlobalType
2107 Types {
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002108 if (!UpRefs.empty())
2109 GEN_ERROR("Invalid upreference in type: " + (*$6)->getDescription());
2110 CurGV = ParseGlobalVariable($1, $2, $3, $5, *$6, 0, $4);
2111 CHECK_FOR_ERROR
2112 delete $6;
Reid Spencer5b7e7532006-09-28 19:28:24 +00002113 } GlobalVarAttributes {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002114 CurGV = 0;
2115 CHECK_FOR_ERROR
2116 }
Anton Korobeynikova80e1182007-04-28 13:45:00 +00002117 | OptGlobalAssign GVVisibilityStyle ALIAS AliasLinkage AliaseeRef {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002118 std::string Name;
2119 if ($1) {
2120 Name = *$1;
2121 delete $1;
2122 }
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00002123 if (Name.empty())
Anton Korobeynikova80e1182007-04-28 13:45:00 +00002124 GEN_ERROR("Alias name cannot be empty");
2125
2126 Constant* Aliasee = $5;
2127 if (Aliasee == 0)
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002128 GEN_ERROR(std::string("Invalid aliasee for alias: ") + Name);
Anton Korobeynikova80e1182007-04-28 13:45:00 +00002129
2130 GlobalAlias* GA = new GlobalAlias(Aliasee->getType(), $4, Name, Aliasee,
2131 CurModule.CurrentModule);
2132 GA->setVisibility($2);
2133 InsertValue(GA, CurModule.Values);
Chris Lattnere5434242007-09-10 23:23:53 +00002134
2135
2136 // If there was a forward reference of this alias, resolve it now.
2137
2138 ValID ID;
2139 if (!Name.empty())
2140 ID = ValID::createGlobalName(Name);
2141 else
2142 ID = ValID::createGlobalID(CurModule.Values.size()-1);
2143
2144 if (GlobalValue *FWGV =
2145 CurModule.GetForwardRefForGlobal(GA->getType(), ID)) {
2146 // Replace uses of the fwdref with the actual alias.
2147 FWGV->replaceAllUsesWith(GA);
2148 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(FWGV))
2149 GV->eraseFromParent();
2150 else
2151 cast<Function>(FWGV)->eraseFromParent();
2152 }
2153 ID.destroy();
2154
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00002155 CHECK_FOR_ERROR
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00002156 }
Reid Spencerb951bc02006-12-29 20:29:48 +00002157 | TARGET TargetDefinition {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002158 CHECK_FOR_ERROR
2159 }
Reid Spencerb951bc02006-12-29 20:29:48 +00002160 | DEPLIBS '=' LibrariesDefinition {
Reid Spencer61c83e02006-08-18 08:43:06 +00002161 CHECK_FOR_ERROR
Chris Lattnere98dda62001-07-14 06:10:16 +00002162 }
Reid Spencerb951bc02006-12-29 20:29:48 +00002163 ;
Chris Lattner00950542001-06-06 20:29:01 +00002164
2165
Chris Lattneree454772006-01-23 23:05:15 +00002166AsmBlock : STRINGCONSTANT {
Chris Lattner66316012006-01-24 04:14:29 +00002167 const std::string &AsmSoFar = CurModule.CurrentModule->getModuleInlineAsm();
Chris Lattneree454772006-01-23 23:05:15 +00002168 if (AsmSoFar.empty())
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002169 CurModule.CurrentModule->setModuleInlineAsm(*$1);
Chris Lattneree454772006-01-23 23:05:15 +00002170 else
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002171 CurModule.CurrentModule->setModuleInlineAsm(AsmSoFar+"\n"+*$1);
2172 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002173 CHECK_FOR_ERROR
Chris Lattneree454772006-01-23 23:05:15 +00002174};
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00002175
Reid Spencerb2d17862007-01-26 08:04:51 +00002176TargetDefinition : TRIPLE '=' STRINGCONSTANT {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002177 CurModule.CurrentModule->setTargetTriple(*$3);
2178 delete $3;
John Criswell2f6a8b12006-10-24 19:09:48 +00002179 }
Chris Lattner10b27112006-10-22 06:07:41 +00002180 | DATALAYOUT '=' STRINGCONSTANT {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002181 CurModule.CurrentModule->setDataLayout(*$3);
2182 delete $3;
Owen Andersoncf7ff2b2006-10-18 02:21:12 +00002183 };
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00002184
Reid Spencerfeaf10e2004-07-25 21:30:51 +00002185LibrariesDefinition : '[' LibList ']';
Reid Spencer0be13e72004-07-25 17:58:28 +00002186
2187LibList : LibList ',' STRINGCONSTANT {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002188 CurModule.CurrentModule->addLibrary(*$3);
2189 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002190 CHECK_FOR_ERROR
Reid Spencer0be13e72004-07-25 17:58:28 +00002191 }
2192 | STRINGCONSTANT {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002193 CurModule.CurrentModule->addLibrary(*$1);
2194 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002195 CHECK_FOR_ERROR
Reid Spencer0be13e72004-07-25 17:58:28 +00002196 }
2197 | /* empty: end of list */ {
Reid Spencer61c83e02006-08-18 08:43:06 +00002198 CHECK_FOR_ERROR
Reid Spencer0be13e72004-07-25 17:58:28 +00002199 }
2200 ;
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00002201
Chris Lattner00950542001-06-06 20:29:01 +00002202//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00002203// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00002204//===----------------------------------------------------------------------===//
2205
Reid Spencerb2d17862007-01-26 08:04:51 +00002206ArgListH : ArgListH ',' Types OptParamAttrs OptLocalName {
Reid Spencere1553cc2006-12-31 05:40:12 +00002207 if (!UpRefs.empty())
2208 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
2209 if (*$3 == Type::VoidTy)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002210 GEN_ERROR("void typed arguments are invalid");
Reid Spencere1553cc2006-12-31 05:40:12 +00002211 ArgListEntry E; E.Attrs = $4; E.Ty = $3; E.Name = $5;
Chris Lattner69da5cf2002-10-13 20:57:00 +00002212 $$ = $1;
Reid Spencere1553cc2006-12-31 05:40:12 +00002213 $1->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002214 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002215 }
Reid Spencerb2d17862007-01-26 08:04:51 +00002216 | Types OptParamAttrs OptLocalName {
Reid Spencere1553cc2006-12-31 05:40:12 +00002217 if (!UpRefs.empty())
2218 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2219 if (*$1 == Type::VoidTy)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002220 GEN_ERROR("void typed arguments are invalid");
Reid Spencere1553cc2006-12-31 05:40:12 +00002221 ArgListEntry E; E.Attrs = $2; E.Ty = $1; E.Name = $3;
2222 $$ = new ArgListType;
2223 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002224 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002225 };
Chris Lattner00950542001-06-06 20:29:01 +00002226
2227ArgList : ArgListH {
2228 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002229 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002230 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00002231 | ArgListH ',' DOTDOTDOT {
2232 $$ = $1;
Reid Spencere1553cc2006-12-31 05:40:12 +00002233 struct ArgListEntry E;
2234 E.Ty = new PATypeHolder(Type::VoidTy);
2235 E.Name = 0;
Reid Spencer18da0722007-04-11 02:44:20 +00002236 E.Attrs = ParamAttr::None;
Reid Spencere1553cc2006-12-31 05:40:12 +00002237 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002238 CHECK_FOR_ERROR
Chris Lattner69da5cf2002-10-13 20:57:00 +00002239 }
2240 | DOTDOTDOT {
Reid Spencere1553cc2006-12-31 05:40:12 +00002241 $$ = new ArgListType;
2242 struct ArgListEntry E;
2243 E.Ty = new PATypeHolder(Type::VoidTy);
2244 E.Name = 0;
Reid Spencer18da0722007-04-11 02:44:20 +00002245 E.Attrs = ParamAttr::None;
Reid Spencere1553cc2006-12-31 05:40:12 +00002246 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002247 CHECK_FOR_ERROR
Chris Lattner69da5cf2002-10-13 20:57:00 +00002248 }
Chris Lattner00950542001-06-06 20:29:01 +00002249 | /* empty */ {
2250 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00002251 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002252 };
Chris Lattner00950542001-06-06 20:29:01 +00002253
Reid Spencerb2d17862007-01-26 08:04:51 +00002254FunctionHeaderH : OptCallingConv ResultTypes GlobalName '(' ArgList ')'
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00002255 OptFuncAttrs OptSection OptAlign OptGC {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002256 std::string FunctionName(*$3);
2257 delete $3; // Free strdup'd memory!
Chris Lattnerdda71962001-11-26 18:54:16 +00002258
Reid Spencer98b3c5c2007-01-02 21:53:43 +00002259 // Check the function result for abstractness if this is a define. We should
2260 // have no abstract types at this point
Reid Spencer2c261782007-01-05 17:06:19 +00002261 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved($2))
2262 GEN_ERROR("Reference to abstract result: "+ $2->get()->getDescription());
Reid Spencer98b3c5c2007-01-02 21:53:43 +00002263
Chris Lattner9232b992003-04-22 18:42:41 +00002264 std::vector<const Type*> ParamTypeList;
Reid Spencer4f859aa2007-04-22 05:46:44 +00002265 ParamAttrsVector Attrs;
2266 if ($7 != ParamAttr::None) {
Duncan Sandsdc024672007-11-27 13:23:08 +00002267 ParamAttrsWithIndex PAWI;
2268 PAWI.index = 0;
2269 PAWI.attrs = $7;
Reid Spencer4f859aa2007-04-22 05:46:44 +00002270 Attrs.push_back(PAWI);
2271 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00002272 if ($5) { // If there are arguments...
Reid Spencer5694b6e2007-04-09 06:17:21 +00002273 unsigned index = 1;
2274 for (ArgListType::iterator I = $5->begin(); I != $5->end(); ++I, ++index) {
Reid Spencere1553cc2006-12-31 05:40:12 +00002275 const Type* Ty = I->Ty->get();
Reid Spencer98b3c5c2007-01-02 21:53:43 +00002276 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved(I->Ty))
2277 GEN_ERROR("Reference to abstract argument: " + Ty->getDescription());
Reid Spencere1553cc2006-12-31 05:40:12 +00002278 ParamTypeList.push_back(Ty);
2279 if (Ty != Type::VoidTy)
Reid Spencer4f859aa2007-04-22 05:46:44 +00002280 if (I->Attrs != ParamAttr::None) {
Duncan Sandsdc024672007-11-27 13:23:08 +00002281 ParamAttrsWithIndex PAWI;
2282 PAWI.index = index;
2283 PAWI.attrs = I->Attrs;
Reid Spencer4f859aa2007-04-22 05:46:44 +00002284 Attrs.push_back(PAWI);
2285 }
Reid Spencere1553cc2006-12-31 05:40:12 +00002286 }
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002287 }
Chris Lattner00950542001-06-06 20:29:01 +00002288
Chris Lattner2079fde2001-10-13 06:41:08 +00002289 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
2290 if (isVarArg) ParamTypeList.pop_back();
2291
Duncan Sands757d2432007-11-30 18:19:18 +00002292 const ParamAttrsList *PAL = 0;
Reid Spencer4f859aa2007-04-22 05:46:44 +00002293 if (!Attrs.empty())
2294 PAL = ParamAttrsList::get(Attrs);
Reid Spencer5694b6e2007-04-09 06:17:21 +00002295
Duncan Sandsdc024672007-11-27 13:23:08 +00002296 FunctionType *FT = FunctionType::get(*$2, ParamTypeList, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002297 const PointerType *PFT = PointerType::get(FT);
Reid Spencer2c261782007-01-05 17:06:19 +00002298 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002299
Chris Lattnera09000d2004-07-14 23:03:46 +00002300 ValID ID;
2301 if (!FunctionName.empty()) {
Reid Spencerb2d17862007-01-26 08:04:51 +00002302 ID = ValID::createGlobalName((char*)FunctionName.c_str());
Chris Lattnera09000d2004-07-14 23:03:46 +00002303 } else {
Reid Spencer186a43f2007-03-19 18:39:36 +00002304 ID = ValID::createGlobalID(CurModule.Values.size());
Chris Lattnera09000d2004-07-14 23:03:46 +00002305 }
2306
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00002307 Function *Fn = 0;
Chris Lattnera09000d2004-07-14 23:03:46 +00002308 // See if this function was forward referenced. If so, recycle the object.
Reid Spencer863dd882007-04-28 16:06:50 +00002309 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
Chris Lattnera09000d2004-07-14 23:03:46 +00002310 // Move the function to the end of the list, from whereever it was
2311 // previously inserted.
2312 Fn = cast<Function>(FWRef);
Duncan Sandsdc024672007-11-27 13:23:08 +00002313 assert(!Fn->getParamAttrs() && "Forward reference has parameter attributes!");
Chris Lattnera09000d2004-07-14 23:03:46 +00002314 CurModule.CurrentModule->getFunctionList().remove(Fn);
2315 CurModule.CurrentModule->getFunctionList().push_back(Fn);
2316 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
Reid Spenceref9b9a72007-02-05 20:47:22 +00002317 (Fn = CurModule.CurrentModule->getFunction(FunctionName))) {
Duncan Sandsdc024672007-11-27 13:23:08 +00002318 if (Fn->getFunctionType() != FT ) {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002319 // The existing function doesn't have the same type. This is an overload
2320 // error.
2321 GEN_ERROR("Overload of function '" + FunctionName + "' not permitted.");
Duncan Sandsdc024672007-11-27 13:23:08 +00002322 } else if (Fn->getParamAttrs() != PAL) {
2323 // The existing function doesn't have the same parameter attributes.
2324 // This is an overload error.
2325 GEN_ERROR("Overload of function '" + FunctionName + "' not permitted.");
Reid Spenceref9b9a72007-02-05 20:47:22 +00002326 } else if (!CurFun.isDeclare && !Fn->isDeclaration()) {
2327 // Neither the existing or the current function is a declaration and they
2328 // have the same name and same type. Clearly this is a redefinition.
Reid Spencerf4fa5902007-02-05 10:16:10 +00002329 GEN_ERROR("Redefinition of function '" + FunctionName + "'");
Duncan Sandsdc024672007-11-27 13:23:08 +00002330 } else if (Fn->isDeclaration()) {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002331 // Make sure to strip off any argument names so we can't get conflicts.
Chris Lattnere4d5c442005-03-15 04:54:21 +00002332 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
Chris Lattnera09000d2004-07-14 23:03:46 +00002333 AI != AE; ++AI)
2334 AI->setName("");
Reid Spenceref9b9a72007-02-05 20:47:22 +00002335 }
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00002336 } else { // Not already defined?
Chris Lattner4989b842007-04-26 05:30:35 +00002337 Fn = new Function(FT, GlobalValue::ExternalWeakLinkage, FunctionName,
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00002338 CurModule.CurrentModule);
2339 InsertValue(Fn, CurModule.Values);
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00002340 }
Chris Lattner00950542001-06-06 20:29:01 +00002341
Chris Lattner394f2eb2003-10-16 20:12:13 +00002342 CurFun.FunctionStart(Fn);
Anton Korobeynikov93c2b372006-09-17 13:06:18 +00002343
2344 if (CurFun.isDeclare) {
2345 // If we have declaration, always overwrite linkage. This will allow us to
2346 // correctly handle cases, when pointer to function is passed as argument to
2347 // another function.
2348 Fn->setLinkage(CurFun.Linkage);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002349 Fn->setVisibility(CurFun.Visibility);
Anton Korobeynikov93c2b372006-09-17 13:06:18 +00002350 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00002351 Fn->setCallingConv($1);
Duncan Sandsdc024672007-11-27 13:23:08 +00002352 Fn->setParamAttrs(PAL);
Reid Spencer2c261782007-01-05 17:06:19 +00002353 Fn->setAlignment($9);
2354 if ($8) {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002355 Fn->setSection(*$8);
2356 delete $8;
Chris Lattner164c3782005-11-12 00:11:10 +00002357 }
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00002358 if ($10) {
2359 Fn->setCollector($10->c_str());
2360 delete $10;
2361 }
Chris Lattner00950542001-06-06 20:29:01 +00002362
Chris Lattner7e708292002-06-25 16:13:24 +00002363 // Add all of the arguments we parsed to the function...
Chris Lattnera8e8f162005-05-06 20:27:19 +00002364 if ($5) { // Is null if empty...
Chris Lattner69da5cf2002-10-13 20:57:00 +00002365 if (isVarArg) { // Nuke the last entry
Reid Spenceref9b9a72007-02-05 20:47:22 +00002366 assert($5->back().Ty->get() == Type::VoidTy && $5->back().Name == 0 &&
Reid Spencer1755a9a2007-02-05 17:01:20 +00002367 "Not a varargs marker!");
Reid Spencere1553cc2006-12-31 05:40:12 +00002368 delete $5->back().Ty;
Chris Lattnera8e8f162005-05-06 20:27:19 +00002369 $5->pop_back(); // Delete the last entry
Chris Lattner69da5cf2002-10-13 20:57:00 +00002370 }
Chris Lattnere4d5c442005-03-15 04:54:21 +00002371 Function::arg_iterator ArgIt = Fn->arg_begin();
Reid Spenceref9b9a72007-02-05 20:47:22 +00002372 Function::arg_iterator ArgEnd = Fn->arg_end();
Reid Spencere1553cc2006-12-31 05:40:12 +00002373 unsigned Idx = 1;
Reid Spenceref9b9a72007-02-05 20:47:22 +00002374 for (ArgListType::iterator I = $5->begin();
2375 I != $5->end() && ArgIt != ArgEnd; ++I, ++ArgIt) {
Reid Spencere1553cc2006-12-31 05:40:12 +00002376 delete I->Ty; // Delete the typeholder...
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002377 setValueName(ArgIt, I->Name); // Insert arg into symtab...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002378 CHECK_FOR_ERROR
Chris Lattner69da5cf2002-10-13 20:57:00 +00002379 InsertValue(ArgIt);
Reid Spencere1553cc2006-12-31 05:40:12 +00002380 Idx++;
Chris Lattner00950542001-06-06 20:29:01 +00002381 }
Reid Spencer08de34b2006-12-03 05:45:44 +00002382
Chris Lattnera8e8f162005-05-06 20:27:19 +00002383 delete $5; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00002384 }
Reid Spencer61c83e02006-08-18 08:43:06 +00002385 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002386};
Chris Lattner00950542001-06-06 20:29:01 +00002387
Chris Lattner9b02cc32002-05-03 18:23:48 +00002388BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
2389
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002390FunctionHeader : FunctionDefineLinkage GVVisibilityStyle FunctionHeaderH BEGIN {
Chris Lattner394f2eb2003-10-16 20:12:13 +00002391 $$ = CurFun.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00002392
Chris Lattner4ad02e72003-04-16 20:28:45 +00002393 // Make sure that we keep track of the linkage type even if there was a
2394 // previous "declare".
2395 $$->setLinkage($1);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002396 $$->setVisibility($2);
Chris Lattner51727be2002-06-04 21:58:56 +00002397};
Chris Lattner00950542001-06-06 20:29:01 +00002398
Chris Lattner9b02cc32002-05-03 18:23:48 +00002399END : ENDTOK | '}'; // Allow end of '}' to end a function
2400
Chris Lattner79df7c02002-03-26 18:01:55 +00002401Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00002402 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002403 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002404};
Chris Lattner00950542001-06-06 20:29:01 +00002405
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002406FunctionProto : FunctionDeclareLinkage GVVisibilityStyle FunctionHeaderH {
Reid Spencere1553cc2006-12-31 05:40:12 +00002407 CurFun.CurrentFunction->setLinkage($1);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002408 CurFun.CurrentFunction->setVisibility($2);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002409 $$ = CurFun.CurrentFunction;
2410 CurFun.FunctionDone();
2411 CHECK_FOR_ERROR
2412 };
Chris Lattner00950542001-06-06 20:29:01 +00002413
2414//===----------------------------------------------------------------------===//
2415// Rules to match Basic Blocks
2416//===----------------------------------------------------------------------===//
2417
Chris Lattneraa2c8532006-01-25 22:26:43 +00002418OptSideEffect : /* empty */ {
2419 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002420 CHECK_FOR_ERROR
Chris Lattneraa2c8532006-01-25 22:26:43 +00002421 }
2422 | SIDEEFFECT {
2423 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002424 CHECK_FOR_ERROR
Chris Lattneraa2c8532006-01-25 22:26:43 +00002425 };
2426
Chris Lattner00950542001-06-06 20:29:01 +00002427ConstValueRef : ESINT64VAL { // A reference to a direct constant
2428 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002429 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002430 }
2431 | EUINT64VAL {
2432 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002433 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002434 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00002435 | FPVAL { // Perhaps it's an FP constant?
2436 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002437 CHECK_FOR_ERROR
Chris Lattner3d52b2f2001-07-15 00:17:01 +00002438 }
Chris Lattner91ef4602004-03-31 03:49:47 +00002439 | TRUETOK {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00002440 $$ = ValID::create(ConstantInt::getTrue());
Reid Spencer61c83e02006-08-18 08:43:06 +00002441 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002442 }
Chris Lattner91ef4602004-03-31 03:49:47 +00002443 | FALSETOK {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00002444 $$ = ValID::create(ConstantInt::getFalse());
Reid Spencer61c83e02006-08-18 08:43:06 +00002445 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002446 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00002447 | NULL_TOK {
2448 $$ = ValID::createNull();
Reid Spencer61c83e02006-08-18 08:43:06 +00002449 CHECK_FOR_ERROR
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002450 }
Chris Lattner16710e92004-10-16 18:17:13 +00002451 | UNDEF {
2452 $$ = ValID::createUndef();
Reid Spencer61c83e02006-08-18 08:43:06 +00002453 CHECK_FOR_ERROR
Chris Lattner16710e92004-10-16 18:17:13 +00002454 }
Chris Lattnerf1f03df2005-12-21 17:53:02 +00002455 | ZEROINITIALIZER { // A vector zero constant.
2456 $$ = ValID::createZeroInit();
Reid Spencer61c83e02006-08-18 08:43:06 +00002457 CHECK_FOR_ERROR
Chris Lattnerf1f03df2005-12-21 17:53:02 +00002458 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00002459 | '<' ConstVector '>' { // Nonempty unsized packed vector
Reid Spencer08de34b2006-12-03 05:45:44 +00002460 const Type *ETy = (*$2)[0]->getType();
Brian Gaeke715c90b2004-08-20 06:00:58 +00002461 int NumElements = $2->size();
2462
Reid Spencer9d6565a2007-02-15 02:26:10 +00002463 VectorType* pt = VectorType::get(ETy, NumElements);
Brian Gaeke715c90b2004-08-20 06:00:58 +00002464 PATypeHolder* PTy = new PATypeHolder(
Reid Spencer08de34b2006-12-03 05:45:44 +00002465 HandleUpRefs(
Reid Spencer9d6565a2007-02-15 02:26:10 +00002466 VectorType::get(
Reid Spencer08de34b2006-12-03 05:45:44 +00002467 ETy,
2468 NumElements)
2469 )
2470 );
Brian Gaeke715c90b2004-08-20 06:00:58 +00002471
2472 // Verify all elements are correct type!
2473 for (unsigned i = 0; i < $2->size(); i++) {
Reid Spencer08de34b2006-12-03 05:45:44 +00002474 if (ETy != (*$2)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00002475 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Brian Gaeke715c90b2004-08-20 06:00:58 +00002476 ETy->getDescription() +"' as required!\nIt is of type '" +
Reid Spencer08de34b2006-12-03 05:45:44 +00002477 (*$2)[i]->getType()->getDescription() + "'.");
Brian Gaeke715c90b2004-08-20 06:00:58 +00002478 }
2479
Reid Spencer9d6565a2007-02-15 02:26:10 +00002480 $$ = ValID::create(ConstantVector::get(pt, *$2));
Brian Gaeke715c90b2004-08-20 06:00:58 +00002481 delete PTy; delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002482 CHECK_FOR_ERROR
Brian Gaeke715c90b2004-08-20 06:00:58 +00002483 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00002484 | ConstExpr {
Reid Spencer08de34b2006-12-03 05:45:44 +00002485 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002486 CHECK_FOR_ERROR
Chris Lattneraa2c8532006-01-25 22:26:43 +00002487 }
2488 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002489 $$ = ValID::createInlineAsm(*$3, *$5, $2);
2490 delete $3;
2491 delete $5;
Reid Spencer61c83e02006-08-18 08:43:06 +00002492 CHECK_FOR_ERROR
Chris Lattnerd78700d2002-08-16 21:14:40 +00002493 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00002494
Chris Lattner2079fde2001-10-13 06:41:08 +00002495// SymbolicValueRef - Reference to one of two ways of symbolically refering to
2496// another value.
2497//
Reid Spencerb2d17862007-01-26 08:04:51 +00002498SymbolicValueRef : LOCALVAL_ID { // Is it an integer reference...?
2499 $$ = ValID::createLocalID($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002500 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002501 }
Reid Spencerb2d17862007-01-26 08:04:51 +00002502 | GLOBALVAL_ID {
2503 $$ = ValID::createGlobalID($1);
2504 CHECK_FOR_ERROR
2505 }
2506 | LocalName { // Is it a named reference...?
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002507 $$ = ValID::createLocalName(*$1);
2508 delete $1;
Reid Spencerb2d17862007-01-26 08:04:51 +00002509 CHECK_FOR_ERROR
2510 }
2511 | GlobalName { // Is it a named reference...?
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002512 $$ = ValID::createGlobalName(*$1);
2513 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002514 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002515 };
Chris Lattner2079fde2001-10-13 06:41:08 +00002516
2517// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00002518ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00002519
Chris Lattner00950542001-06-06 20:29:01 +00002520
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002521// ResolvedVal - a <type> <value> pair. This is used only in cases where the
2522// type immediately preceeds the value reference, and allows complex constant
2523// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00002524ResolvedVal : Types ValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00002525 if (!UpRefs.empty())
2526 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2527 $$ = getVal(*$1, $2);
2528 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002529 CHECK_FOR_ERROR
Reid Spencere1553cc2006-12-31 05:40:12 +00002530 }
2531 ;
Chris Lattner8b81bf52001-07-25 22:47:46 +00002532
Chris Lattner00950542001-06-06 20:29:01 +00002533BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner8ab406d2004-07-13 07:59:27 +00002534 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002535 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002536 }
Chris Lattner7e708292002-06-25 16:13:24 +00002537 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
Chris Lattner8ab406d2004-07-13 07:59:27 +00002538 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002539 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002540 };
Chris Lattner00950542001-06-06 20:29:01 +00002541
2542
2543// Basic blocks are terminated by branching instructions:
2544// br, br/cc, switch, ret
2545//
Reid Spencerb2d17862007-01-26 08:04:51 +00002546BasicBlock : InstructionList OptLocalAssign BBTerminatorInst {
Chris Lattner8ab406d2004-07-13 07:59:27 +00002547 setValueName($3, $2);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002548 CHECK_FOR_ERROR
Chris Lattner2079fde2001-10-13 06:41:08 +00002549 InsertValue($3);
Chris Lattner2079fde2001-10-13 06:41:08 +00002550 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00002551 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002552 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002553 };
Chris Lattner00950542001-06-06 20:29:01 +00002554
2555InstructionList : InstructionList Inst {
Reid Spencer3da59db2006-11-27 01:05:10 +00002556 if (CastInst *CI1 = dyn_cast<CastInst>($2))
2557 if (CastInst *CI2 = dyn_cast<CastInst>(CI1->getOperand(0)))
2558 if (CI2->getParent() == 0)
2559 $1->getInstList().push_back(CI2);
Chris Lattner00950542001-06-06 20:29:01 +00002560 $1->getInstList().push_back($2);
2561 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002562 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002563 }
Reid Spencer186a43f2007-03-19 18:39:36 +00002564 | /* empty */ { // Empty space between instruction lists
2565 $$ = defineBBVal(ValID::createLocalID(CurFun.NextValNum));
Reid Spencer61c83e02006-08-18 08:43:06 +00002566 CHECK_FOR_ERROR
Chris Lattner22ae2322004-07-13 08:39:15 +00002567 }
Reid Spencer186a43f2007-03-19 18:39:36 +00002568 | LABELSTR { // Labelled (named) basic block
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002569 $$ = defineBBVal(ValID::createLocalName(*$1));
2570 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002571 CHECK_FOR_ERROR
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002572
Chris Lattner51727be2002-06-04 21:58:56 +00002573 };
Chris Lattner00950542001-06-06 20:29:01 +00002574
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002575BBTerminatorInst : RET ResolvedVal { // Return with a result...
Reid Spencer08de34b2006-12-03 05:45:44 +00002576 $$ = new ReturnInst($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00002577 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002578 }
Reid Spencer186a43f2007-03-19 18:39:36 +00002579 | RET VOID { // Return with no result...
Chris Lattner00950542001-06-06 20:29:01 +00002580 $$ = new ReturnInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002581 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002582 }
Reid Spencer186a43f2007-03-19 18:39:36 +00002583 | BR LABEL ValueRef { // Unconditional Branch...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002584 BasicBlock* tmpBB = getBBVal($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002585 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002586 $$ = new BranchInst(tmpBB);
Reid Spencer186a43f2007-03-19 18:39:36 +00002587 } // Conditional Branch...
Reid Spencer8088e9d2007-01-13 05:00:20 +00002588 | BR INTTYPE ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
2589 assert(cast<IntegerType>($2)->getBitWidth() == 1 && "Not Bool?");
Reid Spencer5b7e7532006-09-28 19:28:24 +00002590 BasicBlock* tmpBBA = getBBVal($6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002591 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002592 BasicBlock* tmpBBB = getBBVal($9);
2593 CHECK_FOR_ERROR
Reid Spencer4fe16d62007-01-11 18:21:29 +00002594 Value* tmpVal = getVal(Type::Int1Ty, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002595 CHECK_FOR_ERROR
2596 $$ = new BranchInst(tmpBBA, tmpBBB, tmpVal);
Chris Lattner00950542001-06-06 20:29:01 +00002597 }
2598 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencer08de34b2006-12-03 05:45:44 +00002599 Value* tmpVal = getVal($2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002600 CHECK_FOR_ERROR
2601 BasicBlock* tmpBB = getBBVal($6);
2602 CHECK_FOR_ERROR
2603 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, $8->size());
Chris Lattner00950542001-06-06 20:29:01 +00002604 $$ = S;
2605
Chris Lattner9232b992003-04-22 18:42:41 +00002606 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
Chris Lattner46748042002-04-09 19:41:42 +00002607 E = $8->end();
Chris Lattner69331f52005-02-24 05:25:17 +00002608 for (; I != E; ++I) {
2609 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
2610 S->addCase(CI, I->second);
2611 else
Reid Spencerf4fa5902007-02-05 10:16:10 +00002612 GEN_ERROR("Switch case is constant, but not a simple integer");
Chris Lattner69331f52005-02-24 05:25:17 +00002613 }
Chris Lattnerf57a43d2004-04-17 23:49:15 +00002614 delete $8;
Reid Spencer61c83e02006-08-18 08:43:06 +00002615 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002616 }
Chris Lattner196850c2003-05-15 21:30:00 +00002617 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencer08de34b2006-12-03 05:45:44 +00002618 Value* tmpVal = getVal($2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002619 CHECK_FOR_ERROR
2620 BasicBlock* tmpBB = getBBVal($6);
2621 CHECK_FOR_ERROR
2622 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, 0);
Chris Lattner196850c2003-05-15 21:30:00 +00002623 $$ = S;
Reid Spencer61c83e02006-08-18 08:43:06 +00002624 CHECK_FOR_ERROR
Chris Lattner196850c2003-05-15 21:30:00 +00002625 }
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002626 | INVOKE OptCallingConv ResultTypes ValueRef '(' ParamList ')' OptFuncAttrs
Chris Lattnera8e8f162005-05-06 20:27:19 +00002627 TO LABEL ValueRef UNWIND LABEL ValueRef {
Chris Lattner2079fde2001-10-13 06:41:08 +00002628
Reid Spencere1553cc2006-12-31 05:40:12 +00002629 // Handle the short syntax
2630 const PointerType *PFTy = 0;
2631 const FunctionType *Ty = 0;
Reid Spencer2c261782007-01-05 17:06:19 +00002632 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002633 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00002634 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00002635 std::vector<const Type*> ParamTypes;
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002636 ParamList::iterator I = $6->begin(), E = $6->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00002637 for (; I != E; ++I) {
Reid Spencere1553cc2006-12-31 05:40:12 +00002638 const Type *Ty = I->Val->getType();
2639 if (Ty == Type::VoidTy)
2640 GEN_ERROR("Short call syntax cannot be used with varargs");
2641 ParamTypes.push_back(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00002642 }
Duncan Sandsdc024672007-11-27 13:23:08 +00002643 Ty = FunctionType::get($3->get(), ParamTypes, false);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002644 PFTy = PointerType::get(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00002645 }
Chris Lattner2079fde2001-10-13 06:41:08 +00002646
Reid Spencer2c9df212007-03-20 01:13:00 +00002647 delete $3;
2648
Chris Lattnera8e8f162005-05-06 20:27:19 +00002649 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002650 CHECK_FOR_ERROR
Reid Spencer2c261782007-01-05 17:06:19 +00002651 BasicBlock *Normal = getBBVal($11);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002652 CHECK_FOR_ERROR
Reid Spencer2c261782007-01-05 17:06:19 +00002653 BasicBlock *Except = getBBVal($14);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002654 CHECK_FOR_ERROR
Chris Lattner2079fde2001-10-13 06:41:08 +00002655
Duncan Sandsdc024672007-11-27 13:23:08 +00002656 ParamAttrsVector Attrs;
2657 if ($8 != ParamAttr::None) {
2658 ParamAttrsWithIndex PAWI; PAWI.index = 0; PAWI.attrs = $8;
2659 Attrs.push_back(PAWI);
2660 }
2661
Reid Spencere1553cc2006-12-31 05:40:12 +00002662 // Check the arguments
2663 ValueList Args;
2664 if ($6->empty()) { // Has no arguments?
2665 // Make sure no arguments is a good thing!
2666 if (Ty->getNumParams() != 0)
2667 GEN_ERROR("No arguments passed to a function that "
Reid Spencerf4fa5902007-02-05 10:16:10 +00002668 "expects arguments");
Chris Lattner2079fde2001-10-13 06:41:08 +00002669 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00002670 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00002671 // correctly!
Chris Lattnerd5d89962004-02-09 04:14:01 +00002672 FunctionType::param_iterator I = Ty->param_begin();
2673 FunctionType::param_iterator E = Ty->param_end();
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002674 ParamList::iterator ArgI = $6->begin(), ArgE = $6->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00002675 unsigned index = 1;
Reid Spencer863dd882007-04-28 16:06:50 +00002676
Duncan Sandsdc024672007-11-27 13:23:08 +00002677 for (; ArgI != ArgE && I != E; ++ArgI, ++I, ++index) {
Reid Spencere1553cc2006-12-31 05:40:12 +00002678 if (ArgI->Val->getType() != *I)
2679 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00002680 (*I)->getDescription() + "'");
Reid Spencere1553cc2006-12-31 05:40:12 +00002681 Args.push_back(ArgI->Val);
Duncan Sandsdc024672007-11-27 13:23:08 +00002682 if (ArgI->Attrs != ParamAttr::None) {
2683 ParamAttrsWithIndex PAWI;
2684 PAWI.index = index;
2685 PAWI.attrs = ArgI->Attrs;
2686 Attrs.push_back(PAWI);
2687 }
Reid Spencere1553cc2006-12-31 05:40:12 +00002688 }
Reid Spencer08de34b2006-12-03 05:45:44 +00002689
Reid Spencere1553cc2006-12-31 05:40:12 +00002690 if (Ty->isVarArg()) {
2691 if (I == E)
2692 for (; ArgI != ArgE; ++ArgI)
2693 Args.push_back(ArgI->Val); // push the remaining varargs
2694 } else if (I != E || ArgI != ArgE)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002695 GEN_ERROR("Invalid number of parameters detected");
Chris Lattner2079fde2001-10-13 06:41:08 +00002696 }
Reid Spencere1553cc2006-12-31 05:40:12 +00002697
Duncan Sands757d2432007-11-30 18:19:18 +00002698 const ParamAttrsList *PAL = 0;
Duncan Sandsdc024672007-11-27 13:23:08 +00002699 if (!Attrs.empty())
2700 PAL = ParamAttrsList::get(Attrs);
2701
Reid Spencere1553cc2006-12-31 05:40:12 +00002702 // Create the InvokeInst
David Greenef1355a52007-08-27 19:04:21 +00002703 InvokeInst *II = new InvokeInst(V, Normal, Except, Args.begin(), Args.end());
Reid Spencere1553cc2006-12-31 05:40:12 +00002704 II->setCallingConv($2);
Duncan Sandsdc024672007-11-27 13:23:08 +00002705 II->setParamAttrs(PAL);
Reid Spencere1553cc2006-12-31 05:40:12 +00002706 $$ = II;
Chris Lattnera8e8f162005-05-06 20:27:19 +00002707 delete $6;
Reid Spencer61c83e02006-08-18 08:43:06 +00002708 CHECK_FOR_ERROR
Chris Lattner36143fc2003-09-08 18:54:55 +00002709 }
2710 | UNWIND {
2711 $$ = new UnwindInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002712 CHECK_FOR_ERROR
Chris Lattner16710e92004-10-16 18:17:13 +00002713 }
2714 | UNREACHABLE {
2715 $$ = new UnreachableInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002716 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002717 };
Chris Lattner2079fde2001-10-13 06:41:08 +00002718
2719
Chris Lattner00950542001-06-06 20:29:01 +00002720
2721JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
2722 $$ = $1;
Reid Spencer186a43f2007-03-19 18:39:36 +00002723 Constant *V = cast<Constant>(getExistingVal($2, $3));
Reid Spencer5b7e7532006-09-28 19:28:24 +00002724 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002725 if (V == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002726 GEN_ERROR("May only switch on a constant pool value");
Chris Lattner00950542001-06-06 20:29:01 +00002727
Reid Spencer5b7e7532006-09-28 19:28:24 +00002728 BasicBlock* tmpBB = getBBVal($6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002729 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002730 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner00950542001-06-06 20:29:01 +00002731 }
2732 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner9232b992003-04-22 18:42:41 +00002733 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Reid Spencer186a43f2007-03-19 18:39:36 +00002734 Constant *V = cast<Constant>(getExistingVal($1, $2));
Reid Spencer5b7e7532006-09-28 19:28:24 +00002735 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002736
2737 if (V == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002738 GEN_ERROR("May only switch on a constant pool value");
Chris Lattner00950542001-06-06 20:29:01 +00002739
Reid Spencer5b7e7532006-09-28 19:28:24 +00002740 BasicBlock* tmpBB = getBBVal($5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002741 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002742 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner51727be2002-06-04 21:58:56 +00002743 };
Chris Lattner00950542001-06-06 20:29:01 +00002744
Reid Spencerb2d17862007-01-26 08:04:51 +00002745Inst : OptLocalAssign InstVal {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002746 // Is this definition named?? if so, assign the name...
2747 setValueName($2, $1);
2748 CHECK_FOR_ERROR
2749 InsertValue($2);
2750 $$ = $2;
2751 CHECK_FOR_ERROR
2752 };
2753
Chris Lattner00950542001-06-06 20:29:01 +00002754
Chris Lattnerc24d2082001-06-11 15:04:20 +00002755PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Reid Spencere1553cc2006-12-31 05:40:12 +00002756 if (!UpRefs.empty())
2757 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Chris Lattner9232b992003-04-22 18:42:41 +00002758 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Reid Spencer08de34b2006-12-03 05:45:44 +00002759 Value* tmpVal = getVal(*$1, $3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002760 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002761 BasicBlock* tmpBB = getBBVal($5);
2762 CHECK_FOR_ERROR
2763 $$->push_back(std::make_pair(tmpVal, tmpBB));
Reid Spencer08de34b2006-12-03 05:45:44 +00002764 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00002765 }
2766 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
2767 $$ = $1;
Reid Spencer5b7e7532006-09-28 19:28:24 +00002768 Value* tmpVal = getVal($1->front().first->getType(), $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00002769 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002770 BasicBlock* tmpBB = getBBVal($6);
2771 CHECK_FOR_ERROR
2772 $1->push_back(std::make_pair(tmpVal, tmpBB));
Chris Lattner51727be2002-06-04 21:58:56 +00002773 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00002774
2775
Duncan Sandsdc024672007-11-27 13:23:08 +00002776ParamList : Types OptParamAttrs ValueRef OptParamAttrs {
2777 // FIXME: Remove trailing OptParamAttrs in LLVM 3.0, it was a mistake in 2.0
Reid Spencere1553cc2006-12-31 05:40:12 +00002778 if (!UpRefs.empty())
2779 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2780 // Used for call and invoke instructions
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002781 $$ = new ParamList();
Duncan Sandsdc024672007-11-27 13:23:08 +00002782 ParamListEntry E; E.Attrs = $2 | $4; E.Val = getVal($1->get(), $3);
Reid Spencere1553cc2006-12-31 05:40:12 +00002783 $$->push_back(E);
Reid Spencer2c9df212007-03-20 01:13:00 +00002784 delete $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00002785 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002786 }
Duncan Sandsdc024672007-11-27 13:23:08 +00002787 | LABEL OptParamAttrs ValueRef OptParamAttrs {
2788 // FIXME: Remove trailing OptParamAttrs in LLVM 3.0, it was a mistake in 2.0
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002789 // Labels are only valid in ASMs
2790 $$ = new ParamList();
Duncan Sandsdc024672007-11-27 13:23:08 +00002791 ParamListEntry E; E.Attrs = $2 | $4; E.Val = getBBVal($3);
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002792 $$->push_back(E);
Duncan Sandsdc024672007-11-27 13:23:08 +00002793 CHECK_FOR_ERROR
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002794 }
Duncan Sandsdc024672007-11-27 13:23:08 +00002795 | ParamList ',' Types OptParamAttrs ValueRef OptParamAttrs {
2796 // FIXME: Remove trailing OptParamAttrs in LLVM 3.0, it was a mistake in 2.0
Reid Spencere1553cc2006-12-31 05:40:12 +00002797 if (!UpRefs.empty())
2798 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattner00950542001-06-06 20:29:01 +00002799 $$ = $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00002800 ParamListEntry E; E.Attrs = $4 | $6; E.Val = getVal($3->get(), $5);
Reid Spencere1553cc2006-12-31 05:40:12 +00002801 $$->push_back(E);
Reid Spencer2c9df212007-03-20 01:13:00 +00002802 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002803 CHECK_FOR_ERROR
Reid Spencere1553cc2006-12-31 05:40:12 +00002804 }
Duncan Sandsdc024672007-11-27 13:23:08 +00002805 | ParamList ',' LABEL OptParamAttrs ValueRef OptParamAttrs {
2806 // FIXME: Remove trailing OptParamAttrs in LLVM 3.0, it was a mistake in 2.0
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002807 $$ = $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00002808 ParamListEntry E; E.Attrs = $4 | $6; E.Val = getBBVal($5);
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002809 $$->push_back(E);
2810 CHECK_FOR_ERROR
2811 }
2812 | /*empty*/ { $$ = new ParamList(); };
Chris Lattner00950542001-06-06 20:29:01 +00002813
Reid Spencere1553cc2006-12-31 05:40:12 +00002814IndexList // Used for gep instructions and constant expressions
Reid Spencere03969f2006-12-31 21:46:36 +00002815 : /*empty*/ { $$ = new std::vector<Value*>(); }
Reid Spencere1553cc2006-12-31 05:40:12 +00002816 | IndexList ',' ResolvedVal {
2817 $$ = $1;
2818 $$->push_back($3);
2819 CHECK_FOR_ERROR
2820 }
Reid Spencer71305d72006-12-31 21:25:25 +00002821 ;
Chris Lattner00950542001-06-06 20:29:01 +00002822
Chris Lattnerddb6db42005-05-06 05:51:46 +00002823OptTailCall : TAIL CALL {
2824 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002825 CHECK_FOR_ERROR
Chris Lattnerddb6db42005-05-06 05:51:46 +00002826 }
2827 | CALL {
2828 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002829 CHECK_FOR_ERROR
Chris Lattnerddb6db42005-05-06 05:51:46 +00002830 };
2831
Chris Lattner4a6482b2002-09-10 19:57:26 +00002832InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00002833 if (!UpRefs.empty())
2834 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Chris Lattner42a75512007-01-15 02:27:26 +00002835 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint() &&
Reid Spencer9d6565a2007-02-15 02:26:10 +00002836 !isa<VectorType>((*$2).get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002837 GEN_ERROR(
Reid Spencerf4fa5902007-02-05 10:16:10 +00002838 "Arithmetic operator requires integer, FP, or packed operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00002839 Value* val1 = getVal(*$2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002840 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00002841 Value* val2 = getVal(*$2, $5);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002842 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00002843 $$ = BinaryOperator::create($1, val1, val2);
Chris Lattner4a6482b2002-09-10 19:57:26 +00002844 if ($$ == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002845 GEN_ERROR("binary operator returned null");
Reid Spencer08de34b2006-12-03 05:45:44 +00002846 delete $2;
Chris Lattner4a6482b2002-09-10 19:57:26 +00002847 }
2848 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00002849 if (!UpRefs.empty())
2850 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Chris Lattner42a75512007-01-15 02:27:26 +00002851 if (!(*$2)->isInteger()) {
Reid Spencer9d6565a2007-02-15 02:26:10 +00002852 if (Instruction::isShift($1) || !isa<VectorType>($2->get()) ||
2853 !cast<VectorType>($2->get())->getElementType()->isInteger())
Reid Spencerf4fa5902007-02-05 10:16:10 +00002854 GEN_ERROR("Logical operator requires integral operands");
Chris Lattner0a017832005-12-21 18:31:29 +00002855 }
Reid Spencer08de34b2006-12-03 05:45:44 +00002856 Value* tmpVal1 = getVal(*$2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002857 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00002858 Value* tmpVal2 = getVal(*$2, $5);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002859 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00002860 $$ = BinaryOperator::create($1, tmpVal1, tmpVal2);
Chris Lattner4a6482b2002-09-10 19:57:26 +00002861 if ($$ == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002862 GEN_ERROR("binary operator returned null");
Reid Spencer08de34b2006-12-03 05:45:44 +00002863 delete $2;
Chris Lattner4a6482b2002-09-10 19:57:26 +00002864 }
Reid Spencer08de34b2006-12-03 05:45:44 +00002865 | ICMP IPredicates Types ValueRef ',' ValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00002866 if (!UpRefs.empty())
2867 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencer9d6565a2007-02-15 02:26:10 +00002868 if (isa<VectorType>((*$3).get()))
Reid Spencerac9dcb92007-02-15 03:39:18 +00002869 GEN_ERROR("Vector types not supported by icmp instruction");
Reid Spencer08de34b2006-12-03 05:45:44 +00002870 Value* tmpVal1 = getVal(*$3, $4);
2871 CHECK_FOR_ERROR
2872 Value* tmpVal2 = getVal(*$3, $6);
2873 CHECK_FOR_ERROR
2874 $$ = CmpInst::create($1, $2, tmpVal1, tmpVal2);
2875 if ($$ == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002876 GEN_ERROR("icmp operator returned null");
Reid Spencer2c9df212007-03-20 01:13:00 +00002877 delete $3;
Reid Spencer08de34b2006-12-03 05:45:44 +00002878 }
2879 | FCMP FPredicates Types ValueRef ',' ValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00002880 if (!UpRefs.empty())
2881 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencer9d6565a2007-02-15 02:26:10 +00002882 if (isa<VectorType>((*$3).get()))
Reid Spencerac9dcb92007-02-15 03:39:18 +00002883 GEN_ERROR("Vector types not supported by fcmp instruction");
Reid Spencer08de34b2006-12-03 05:45:44 +00002884 Value* tmpVal1 = getVal(*$3, $4);
2885 CHECK_FOR_ERROR
2886 Value* tmpVal2 = getVal(*$3, $6);
2887 CHECK_FOR_ERROR
2888 $$ = CmpInst::create($1, $2, tmpVal1, tmpVal2);
2889 if ($$ == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002890 GEN_ERROR("fcmp operator returned null");
Reid Spencer2c9df212007-03-20 01:13:00 +00002891 delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00002892 }
Reid Spencer3da59db2006-11-27 01:05:10 +00002893 | CastOps ResolvedVal TO Types {
Reid Spencere1553cc2006-12-31 05:40:12 +00002894 if (!UpRefs.empty())
2895 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00002896 Value* Val = $2;
Reid Spencer93947c32007-01-17 02:47:33 +00002897 const Type* DestTy = $4->get();
2898 if (!CastInst::castIsValid($1, Val, DestTy))
2899 GEN_ERROR("invalid cast opcode for cast from '" +
2900 Val->getType()->getDescription() + "' to '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00002901 DestTy->getDescription() + "'");
Reid Spencer93947c32007-01-17 02:47:33 +00002902 $$ = CastInst::create($1, Val, DestTy);
Reid Spencer08de34b2006-12-03 05:45:44 +00002903 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00002904 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00002905 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer4fe16d62007-01-11 18:21:29 +00002906 if ($2->getType() != Type::Int1Ty)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002907 GEN_ERROR("select condition must be boolean");
Reid Spencer08de34b2006-12-03 05:45:44 +00002908 if ($4->getType() != $6->getType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00002909 GEN_ERROR("select value types should match");
Reid Spencer08de34b2006-12-03 05:45:44 +00002910 $$ = new SelectInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002911 CHECK_FOR_ERROR
Chris Lattner76f0ff32004-03-12 05:51:36 +00002912 }
Chris Lattner99e7ab72003-10-18 05:53:13 +00002913 | VAARG ResolvedVal ',' Types {
Reid Spencere1553cc2006-12-31 05:40:12 +00002914 if (!UpRefs.empty())
2915 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00002916 $$ = new VAArgInst($2, *$4);
2917 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00002918 CHECK_FOR_ERROR
Chris Lattner99e7ab72003-10-18 05:53:13 +00002919 }
Robert Bocchino9c62b562006-01-10 19:04:32 +00002920 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencer08de34b2006-12-03 05:45:44 +00002921 if (!ExtractElementInst::isValidOperands($2, $4))
Reid Spencerf4fa5902007-02-05 10:16:10 +00002922 GEN_ERROR("Invalid extractelement operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00002923 $$ = new ExtractElementInst($2, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00002924 CHECK_FOR_ERROR
Robert Bocchino9c62b562006-01-10 19:04:32 +00002925 }
Robert Bocchino2def1b32006-01-17 20:06:25 +00002926 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer08de34b2006-12-03 05:45:44 +00002927 if (!InsertElementInst::isValidOperands($2, $4, $6))
Reid Spencerf4fa5902007-02-05 10:16:10 +00002928 GEN_ERROR("Invalid insertelement operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00002929 $$ = new InsertElementInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002930 CHECK_FOR_ERROR
Robert Bocchino2def1b32006-01-17 20:06:25 +00002931 }
Chris Lattner4c949082006-04-08 01:18:35 +00002932 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer08de34b2006-12-03 05:45:44 +00002933 if (!ShuffleVectorInst::isValidOperands($2, $4, $6))
Reid Spencerf4fa5902007-02-05 10:16:10 +00002934 GEN_ERROR("Invalid shufflevector operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00002935 $$ = new ShuffleVectorInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002936 CHECK_FOR_ERROR
Chris Lattner4c949082006-04-08 01:18:35 +00002937 }
Chris Lattner3b237fc2003-10-19 21:34:28 +00002938 | PHI_TOK PHIList {
Chris Lattnerc24d2082001-06-11 15:04:20 +00002939 const Type *Ty = $2->front().first->getType();
Chris Lattner8ea8f362003-10-30 01:38:18 +00002940 if (!Ty->isFirstClassType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00002941 GEN_ERROR("PHI node operands must be of first class type");
Chris Lattnerc24d2082001-06-11 15:04:20 +00002942 $$ = new PHINode(Ty);
Chris Lattnerce1df9e2005-01-29 00:35:55 +00002943 ((PHINode*)$$)->reserveOperandSpace($2->size());
Chris Lattner00950542001-06-06 20:29:01 +00002944 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00002945 if ($2->front().first->getType() != Ty)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002946 GEN_ERROR("All elements of a PHI node must be of the same type");
Chris Lattnerb00c5822001-10-02 03:41:24 +00002947 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00002948 $2->pop_front();
2949 }
2950 delete $2; // Free the list...
Reid Spencer61c83e02006-08-18 08:43:06 +00002951 CHECK_FOR_ERROR
Chris Lattnerddb6db42005-05-06 05:51:46 +00002952 }
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002953 | OptTailCall OptCallingConv ResultTypes ValueRef '(' ParamList ')'
Reid Spencer2c261782007-01-05 17:06:19 +00002954 OptFuncAttrs {
Reid Spencere1553cc2006-12-31 05:40:12 +00002955
2956 // Handle the short syntax
Bill Wendlingb39a55a2006-11-12 11:10:39 +00002957 const PointerType *PFTy = 0;
2958 const FunctionType *Ty = 0;
Reid Spencer2c261782007-01-05 17:06:19 +00002959 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002960 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00002961 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00002962 std::vector<const Type*> ParamTypes;
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002963 ParamList::iterator I = $6->begin(), E = $6->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00002964 for (; I != E; ++I) {
Reid Spencere1553cc2006-12-31 05:40:12 +00002965 const Type *Ty = I->Val->getType();
2966 if (Ty == Type::VoidTy)
2967 GEN_ERROR("Short call syntax cannot be used with varargs");
2968 ParamTypes.push_back(Ty);
Chris Lattneref9c23f2001-10-03 14:53:21 +00002969 }
Duncan Sandsdc024672007-11-27 13:23:08 +00002970 Ty = FunctionType::get($3->get(), ParamTypes, false);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002971 PFTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00002972 }
Chris Lattner00950542001-06-06 20:29:01 +00002973
Chris Lattnera8e8f162005-05-06 20:27:19 +00002974 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002975 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002976
Reid Spencerebff55c2007-04-16 06:55:42 +00002977 // Check for call to invalid intrinsic to avoid crashing later.
2978 if (Function *theF = dyn_cast<Function>(V)) {
Reid Spencerce1e8ef2007-04-16 22:01:57 +00002979 if (theF->hasName() && (theF->getValueName()->getKeyLength() >= 5) &&
Reid Spencer43e60732007-04-16 20:31:06 +00002980 (0 == strncmp(theF->getValueName()->getKeyData(), "llvm.", 5)) &&
2981 !theF->getIntrinsicID(true))
Reid Spencerebff55c2007-04-16 06:55:42 +00002982 GEN_ERROR("Call to invalid LLVM intrinsic function '" +
2983 theF->getName() + "'");
2984 }
2985
Duncan Sandsdc024672007-11-27 13:23:08 +00002986 // Set up the ParamAttrs for the function
2987 ParamAttrsVector Attrs;
2988 if ($8 != ParamAttr::None) {
2989 ParamAttrsWithIndex PAWI;
2990 PAWI.index = 0;
2991 PAWI.attrs = $8;
2992 Attrs.push_back(PAWI);
2993 }
Reid Spencere1553cc2006-12-31 05:40:12 +00002994 // Check the arguments
2995 ValueList Args;
2996 if ($6->empty()) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00002997 // Make sure no arguments is a good thing!
2998 if (Ty->getNumParams() != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002999 GEN_ERROR("No arguments passed to a function that "
Reid Spencerf4fa5902007-02-05 10:16:10 +00003000 "expects arguments");
Chris Lattner8b81bf52001-07-25 22:47:46 +00003001 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00003002 // Loop through FunctionType's arguments and ensure they are specified
Duncan Sandsdc024672007-11-27 13:23:08 +00003003 // correctly. Also, gather any parameter attributes.
Chris Lattnerd5d89962004-02-09 04:14:01 +00003004 FunctionType::param_iterator I = Ty->param_begin();
3005 FunctionType::param_iterator E = Ty->param_end();
Dale Johanneseneb57ea72007-11-05 21:20:28 +00003006 ParamList::iterator ArgI = $6->begin(), ArgE = $6->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00003007 unsigned index = 1;
Reid Spencer863dd882007-04-28 16:06:50 +00003008
Duncan Sandsdc024672007-11-27 13:23:08 +00003009 for (; ArgI != ArgE && I != E; ++ArgI, ++I, ++index) {
Reid Spencere1553cc2006-12-31 05:40:12 +00003010 if (ArgI->Val->getType() != *I)
3011 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00003012 (*I)->getDescription() + "'");
Reid Spencere1553cc2006-12-31 05:40:12 +00003013 Args.push_back(ArgI->Val);
Duncan Sandsdc024672007-11-27 13:23:08 +00003014 if (ArgI->Attrs != ParamAttr::None) {
3015 ParamAttrsWithIndex PAWI;
3016 PAWI.index = index;
3017 PAWI.attrs = ArgI->Attrs;
3018 Attrs.push_back(PAWI);
3019 }
Reid Spencere1553cc2006-12-31 05:40:12 +00003020 }
3021 if (Ty->isVarArg()) {
3022 if (I == E)
3023 for (; ArgI != ArgE; ++ArgI)
3024 Args.push_back(ArgI->Val); // push the remaining varargs
3025 } else if (I != E || ArgI != ArgE)
Reid Spencerf4fa5902007-02-05 10:16:10 +00003026 GEN_ERROR("Invalid number of parameters detected");
Chris Lattner8b81bf52001-07-25 22:47:46 +00003027 }
Duncan Sandsdc024672007-11-27 13:23:08 +00003028
3029 // Finish off the ParamAttrs and check them
Duncan Sands757d2432007-11-30 18:19:18 +00003030 const ParamAttrsList *PAL = 0;
Duncan Sandsdc024672007-11-27 13:23:08 +00003031 if (!Attrs.empty())
3032 PAL = ParamAttrsList::get(Attrs);
3033
Reid Spencere1553cc2006-12-31 05:40:12 +00003034 // Create the call node
David Greene52eec542007-08-01 03:43:44 +00003035 CallInst *CI = new CallInst(V, Args.begin(), Args.end());
Reid Spencere1553cc2006-12-31 05:40:12 +00003036 CI->setTailCall($1);
3037 CI->setCallingConv($2);
Duncan Sandsdc024672007-11-27 13:23:08 +00003038 CI->setParamAttrs(PAL);
Reid Spencere1553cc2006-12-31 05:40:12 +00003039 $$ = CI;
Chris Lattnera8e8f162005-05-06 20:27:19 +00003040 delete $6;
Reid Spencerb2d17862007-01-26 08:04:51 +00003041 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00003042 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00003043 }
3044 | MemoryInst {
3045 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00003046 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00003047 };
Chris Lattner00950542001-06-06 20:29:01 +00003048
Chris Lattner15c9c032003-09-08 18:20:29 +00003049OptVolatile : VOLATILE {
3050 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00003051 CHECK_FOR_ERROR
Chris Lattner15c9c032003-09-08 18:20:29 +00003052 }
3053 | /* empty */ {
3054 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00003055 CHECK_FOR_ERROR
Chris Lattner15c9c032003-09-08 18:20:29 +00003056 };
3057
Chris Lattner027dcc52001-07-08 21:10:27 +00003058
Chris Lattnerddb6db42005-05-06 05:51:46 +00003059
Chris Lattner66db8e42005-11-06 06:34:12 +00003060MemoryInst : MALLOC Types OptCAlign {
Reid Spencere1553cc2006-12-31 05:40:12 +00003061 if (!UpRefs.empty())
3062 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00003063 $$ = new MallocInst(*$2, 0, $3);
3064 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00003065 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00003066 }
Reid Spencera54b7cb2007-01-12 07:05:14 +00003067 | MALLOC Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencere1553cc2006-12-31 05:40:12 +00003068 if (!UpRefs.empty())
3069 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00003070 Value* tmpVal = getVal($4, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00003071 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00003072 $$ = new MallocInst(*$2, tmpVal, $6);
3073 delete $2;
Nate Begeman14b05292005-11-05 09:21:28 +00003074 }
Chris Lattner66db8e42005-11-06 06:34:12 +00003075 | ALLOCA Types OptCAlign {
Reid Spencere1553cc2006-12-31 05:40:12 +00003076 if (!UpRefs.empty())
3077 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00003078 $$ = new AllocaInst(*$2, 0, $3);
3079 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00003080 CHECK_FOR_ERROR
Nate Begeman14b05292005-11-05 09:21:28 +00003081 }
Reid Spencera54b7cb2007-01-12 07:05:14 +00003082 | ALLOCA Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencere1553cc2006-12-31 05:40:12 +00003083 if (!UpRefs.empty())
3084 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00003085 Value* tmpVal = getVal($4, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00003086 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00003087 $$ = new AllocaInst(*$2, tmpVal, $6);
3088 delete $2;
Nate Begeman14b05292005-11-05 09:21:28 +00003089 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00003090 | FREE ResolvedVal {
Reid Spencer08de34b2006-12-03 05:45:44 +00003091 if (!isa<PointerType>($2->getType()))
Reid Spencer61c83e02006-08-18 08:43:06 +00003092 GEN_ERROR("Trying to free nonpointer type " +
Reid Spencerf4fa5902007-02-05 10:16:10 +00003093 $2->getType()->getDescription() + "");
Reid Spencer08de34b2006-12-03 05:45:44 +00003094 $$ = new FreeInst($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00003095 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00003096 }
3097
Christopher Lamb43c7f372007-04-22 19:24:39 +00003098 | OptVolatile LOAD Types ValueRef OptCAlign {
Reid Spencere1553cc2006-12-31 05:40:12 +00003099 if (!UpRefs.empty())
3100 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00003101 if (!isa<PointerType>($3->get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00003102 GEN_ERROR("Can't load from nonpointer type: " +
Reid Spencer08de34b2006-12-03 05:45:44 +00003103 (*$3)->getDescription());
3104 if (!cast<PointerType>($3->get())->getElementType()->isFirstClassType())
Reid Spencer61c83e02006-08-18 08:43:06 +00003105 GEN_ERROR("Can't load from pointer of non-first-class type: " +
Reid Spencer08de34b2006-12-03 05:45:44 +00003106 (*$3)->getDescription());
3107 Value* tmpVal = getVal(*$3, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00003108 CHECK_FOR_ERROR
Christopher Lamb43c7f372007-04-22 19:24:39 +00003109 $$ = new LoadInst(tmpVal, "", $1, $5);
Reid Spencer08de34b2006-12-03 05:45:44 +00003110 delete $3;
Chris Lattner027dcc52001-07-08 21:10:27 +00003111 }
Christopher Lamb43c7f372007-04-22 19:24:39 +00003112 | OptVolatile STORE ResolvedVal ',' Types ValueRef OptCAlign {
Reid Spencere1553cc2006-12-31 05:40:12 +00003113 if (!UpRefs.empty())
3114 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00003115 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner8c8418d2003-09-01 16:31:28 +00003116 if (!PT)
Reid Spencer61c83e02006-08-18 08:43:06 +00003117 GEN_ERROR("Can't store to a nonpointer type: " +
Reid Spencer08de34b2006-12-03 05:45:44 +00003118 (*$5)->getDescription());
Chris Lattner8c8418d2003-09-01 16:31:28 +00003119 const Type *ElTy = PT->getElementType();
Reid Spencer08de34b2006-12-03 05:45:44 +00003120 if (ElTy != $3->getType())
3121 GEN_ERROR("Can't store '" + $3->getType()->getDescription() +
Reid Spencerf4fa5902007-02-05 10:16:10 +00003122 "' into space of type '" + ElTy->getDescription() + "'");
Chris Lattner0383cc42002-08-21 23:51:21 +00003123
Reid Spencer08de34b2006-12-03 05:45:44 +00003124 Value* tmpVal = getVal(*$5, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00003125 CHECK_FOR_ERROR
Christopher Lamb43c7f372007-04-22 19:24:39 +00003126 $$ = new StoreInst($3, tmpVal, $1, $7);
Reid Spencer08de34b2006-12-03 05:45:44 +00003127 delete $5;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00003128 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00003129 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencere1553cc2006-12-31 05:40:12 +00003130 if (!UpRefs.empty())
3131 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00003132 if (!isa<PointerType>($2->get()))
Reid Spencerf4fa5902007-02-05 10:16:10 +00003133 GEN_ERROR("getelementptr insn requires pointer operand");
Chris Lattner830b6f92004-04-05 01:30:04 +00003134
David Greeneb8f74792007-09-04 15:46:09 +00003135 if (!GetElementPtrInst::getIndexedType(*$2, $4->begin(), $4->end(), true))
Reid Spencer61c83e02006-08-18 08:43:06 +00003136 GEN_ERROR("Invalid getelementptr indices for type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00003137 (*$2)->getDescription()+ "'");
Reid Spencer08de34b2006-12-03 05:45:44 +00003138 Value* tmpVal = getVal(*$2, $3);
Reid Spencer61c83e02006-08-18 08:43:06 +00003139 CHECK_FOR_ERROR
David Greeneb8f74792007-09-04 15:46:09 +00003140 $$ = new GetElementPtrInst(tmpVal, $4->begin(), $4->end());
Reid Spencer08de34b2006-12-03 05:45:44 +00003141 delete $2;
Reid Spencer5b7e7532006-09-28 19:28:24 +00003142 delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00003143 };
Chris Lattner027dcc52001-07-08 21:10:27 +00003144
Brian Gaeked0fde302003-11-11 22:41:34 +00003145
Chris Lattner00950542001-06-06 20:29:01 +00003146%%
Reid Spencer61c83e02006-08-18 08:43:06 +00003147
Reid Spencere1553cc2006-12-31 05:40:12 +00003148// common code from the two 'RunVMAsmParser' functions
3149static Module* RunParser(Module * M) {
Reid Spencere1553cc2006-12-31 05:40:12 +00003150 CurModule.CurrentModule = M;
Reid Spencere1553cc2006-12-31 05:40:12 +00003151 // Check to make sure the parser succeeded
3152 if (yyparse()) {
3153 if (ParserResult)
3154 delete ParserResult;
3155 return 0;
3156 }
3157
Reid Spencercd5bd902007-03-30 01:37:13 +00003158 // Emit an error if there are any unresolved types left.
3159 if (!CurModule.LateResolveTypes.empty()) {
3160 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
3161 if (DID.Type == ValID::LocalName) {
3162 GenerateError("Undefined type remains at eof: '"+DID.getName() + "'");
3163 } else {
3164 GenerateError("Undefined type remains at eof: #" + itostr(DID.Num));
3165 }
3166 if (ParserResult)
3167 delete ParserResult;
3168 return 0;
3169 }
3170
3171 // Emit an error if there are any unresolved values left.
3172 if (!CurModule.LateResolveValues.empty()) {
3173 Value *V = CurModule.LateResolveValues.back();
3174 std::map<Value*, std::pair<ValID, int> >::iterator I =
3175 CurModule.PlaceHolderInfo.find(V);
3176
3177 if (I != CurModule.PlaceHolderInfo.end()) {
3178 ValID &DID = I->second.first;
3179 if (DID.Type == ValID::LocalName) {
3180 GenerateError("Undefined value remains at eof: "+DID.getName() + "'");
3181 } else {
3182 GenerateError("Undefined value remains at eof: #" + itostr(DID.Num));
3183 }
3184 if (ParserResult)
3185 delete ParserResult;
3186 return 0;
3187 }
3188 }
3189
Reid Spencere1553cc2006-12-31 05:40:12 +00003190 // Check to make sure that parsing produced a result
3191 if (!ParserResult)
3192 return 0;
3193
3194 // Reset ParserResult variable while saving its value for the result.
3195 Module *Result = ParserResult;
3196 ParserResult = 0;
3197
3198 return Result;
3199}
3200
Reid Spencer61c83e02006-08-18 08:43:06 +00003201void llvm::GenerateError(const std::string &message, int LineNo) {
Chris Lattner8e3a8e02007-11-18 08:46:26 +00003202 if (LineNo == -1) LineNo = LLLgetLineNo();
Reid Spencer61c83e02006-08-18 08:43:06 +00003203 // TODO: column number in exception
3204 if (TheParseError)
Chris Lattner8e3a8e02007-11-18 08:46:26 +00003205 TheParseError->setError(LLLgetFilename(), message, LineNo);
Reid Spencer61c83e02006-08-18 08:43:06 +00003206 TriggerError = 1;
3207}
3208
Chris Lattner09083092001-07-08 04:57:15 +00003209int yyerror(const char *ErrorMsg) {
Chris Lattner8e3a8e02007-11-18 08:46:26 +00003210 std::string where = LLLgetFilename() + ":" + utostr(LLLgetLineNo()) + ": ";
Reid Spenceref9b9a72007-02-05 20:47:22 +00003211 std::string errMsg = where + "error: " + std::string(ErrorMsg);
Chris Lattner8e3a8e02007-11-18 08:46:26 +00003212 if (yychar != YYEMPTY && yychar != 0) {
3213 errMsg += " while reading token: '";
3214 errMsg += std::string(LLLgetTokenStart(),
3215 LLLgetTokenStart()+LLLgetTokenLength()) + "'";
3216 }
Reid Spencer61c83e02006-08-18 08:43:06 +00003217 GenerateError(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00003218 return 0;
3219}