blob: b0cdbc7c9bc262f764da036748c09b936918abf7 [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//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// 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??
Chris Lattner689e8b22008-02-19 04:36:07 +0000381 if (!isa<IntegerType>(Ty) ||
382 !ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000383 GenerateError("Signed integral constant '" +
Misha Brukman5a2a3822005-05-10 22:02:28 +0000384 itostr(D.ConstPool64) + "' is invalid for type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +0000385 Ty->getDescription() + "'");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000386 return 0;
387 }
Reid Spencereac65742007-03-19 20:40:22 +0000388 return ConstantInt::get(Ty, D.ConstPool64, true);
Chris Lattner2079fde2001-10-13 06:41:08 +0000389
390 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattner689e8b22008-02-19 04:36:07 +0000391 if (isa<IntegerType>(Ty) &&
392 ConstantInt::isValueValidForType(Ty, D.UConstPool64))
Reid Spencerb83eb642006-10-20 07:07:24 +0000393 return ConstantInt::get(Ty, D.UConstPool64);
Chris Lattner689e8b22008-02-19 04:36:07 +0000394
395 if (!isa<IntegerType>(Ty) ||
396 !ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
397 GenerateError("Integral constant '" + utostr(D.UConstPool64) +
398 "' is invalid or out of range for type '" +
399 Ty->getDescription() + "'");
400 return 0;
Chris Lattner2079fde2001-10-13 06:41:08 +0000401 }
Chris Lattner689e8b22008-02-19 04:36:07 +0000402 // This is really a signed reference. Transmogrify.
403 return ConstantInt::get(Ty, D.ConstPool64, true);
Chris Lattner2079fde2001-10-13 06:41:08 +0000404
Chris Lattner2079fde2001-10-13 06:41:08 +0000405 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattner689e8b22008-02-19 04:36:07 +0000406 if (!Ty->isFloatingPoint() ||
407 !ConstantFP::isValueValidForType(Ty, *D.ConstPoolFP)) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000408 GenerateError("FP constant invalid for type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000409 return 0;
410 }
Dale Johannesen3f6eb742007-09-11 18:32:33 +0000411 // Lexer has no type info, so builds all float and double FP constants
412 // as double. Fix this here. Long double does not need this.
413 if (&D.ConstPoolFP->getSemantics() == &APFloat::IEEEdouble &&
414 Ty==Type::FloatTy)
Dale Johannesen43421b32007-09-06 18:13:44 +0000415 D.ConstPoolFP->convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven);
416 return ConstantFP::get(Ty, *D.ConstPoolFP);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000417
Chris Lattner2079fde2001-10-13 06:41:08 +0000418 case ValID::ConstNullVal: // Is it a null value?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000419 if (!isa<PointerType>(Ty)) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000420 GenerateError("Cannot create a a non pointer null");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000421 return 0;
422 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000423 return ConstantPointerNull::get(cast<PointerType>(Ty));
Misha Brukman5a2a3822005-05-10 22:02:28 +0000424
Chris Lattner16710e92004-10-16 18:17:13 +0000425 case ValID::ConstUndefVal: // Is it an undef value?
426 return UndefValue::get(Ty);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000427
Chris Lattnerf1f03df2005-12-21 17:53:02 +0000428 case ValID::ConstZeroVal: // Is it a zero value?
429 return Constant::getNullValue(Ty);
430
Chris Lattnerd78700d2002-08-16 21:14:40 +0000431 case ValID::ConstantVal: // Fully resolved constant?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000432 if (D.ConstantValue->getType() != Ty) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000433 GenerateError("Constant expression type different from required type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000434 return 0;
435 }
Chris Lattnerd78700d2002-08-16 21:14:40 +0000436 return D.ConstantValue;
437
Chris Lattneraa2c8532006-01-25 22:26:43 +0000438 case ValID::InlineAsmVal: { // Inline asm expression
439 const PointerType *PTy = dyn_cast<PointerType>(Ty);
440 const FunctionType *FTy =
441 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
Reid Spencer5b7e7532006-09-28 19:28:24 +0000442 if (!FTy || !InlineAsm::Verify(FTy, D.IAD->Constraints)) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000443 GenerateError("Invalid type for asm constraint string");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000444 return 0;
445 }
Chris Lattneraa2c8532006-01-25 22:26:43 +0000446 InlineAsm *IA = InlineAsm::get(FTy, D.IAD->AsmString, D.IAD->Constraints,
447 D.IAD->HasSideEffects);
448 D.destroy(); // Free InlineAsmDescriptor.
449 return IA;
450 }
Chris Lattner30c89792001-09-07 16:35:17 +0000451 default:
Reid Spencer1755a9a2007-02-05 17:01:20 +0000452 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000453 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000454 } // End of switch
455
Reid Spencer1755a9a2007-02-05 17:01:20 +0000456 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000457 return 0;
458}
459
Reid Spencer186a43f2007-03-19 18:39:36 +0000460// getVal - This function is identical to getExistingVal, except that if a
Chris Lattner2079fde2001-10-13 06:41:08 +0000461// value is not already defined, it "improvises" by creating a placeholder var
462// that looks and acts just like the requested variable. When the value is
463// defined later, all uses of the placeholder variable are replaced with the
464// real thing.
465//
Chris Lattner64116f92004-07-14 01:33:11 +0000466static Value *getVal(const Type *Ty, const ValID &ID) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000467 if (Ty == Type::LabelTy) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000468 GenerateError("Cannot use a basic block here");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000469 return 0;
470 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000471
Chris Lattner64116f92004-07-14 01:33:11 +0000472 // See if the value has already been defined.
Reid Spencer186a43f2007-03-19 18:39:36 +0000473 Value *V = getExistingVal(Ty, ID);
Chris Lattner2079fde2001-10-13 06:41:08 +0000474 if (V) return V;
Reid Spencer5b7e7532006-09-28 19:28:24 +0000475 if (TriggerError) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000476
Reid Spencer5b7e7532006-09-28 19:28:24 +0000477 if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty)) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000478 GenerateError("Invalid use of a composite type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000479 return 0;
480 }
Chris Lattner60cd9552005-03-23 01:29:26 +0000481
Chris Lattner00950542001-06-06 20:29:01 +0000482 // If we reached here, we referenced either a symbol that we don't know about
483 // or an id number that hasn't been read yet. We may be referencing something
484 // forward, so just create an entry to be resolved later and get to it...
485 //
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000486 switch (ID.Type) {
487 case ValID::GlobalName:
Reid Spencer3cb4dda2007-04-28 14:13:42 +0000488 case ValID::GlobalID: {
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000489 const PointerType *PTy = dyn_cast<PointerType>(Ty);
490 if (!PTy) {
491 GenerateError("Invalid type for reference to global" );
492 return 0;
493 }
494 const Type* ElTy = PTy->getElementType();
495 if (const FunctionType *FTy = dyn_cast<FunctionType>(ElTy))
496 V = new Function(FTy, GlobalValue::ExternalLinkage);
497 else
Christopher Lambfe63fb92007-12-11 08:59:05 +0000498 V = new GlobalVariable(ElTy, false, GlobalValue::ExternalLinkage, 0, "",
499 (Module*)0, false, PTy->getAddressSpace());
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000500 break;
Reid Spencer3cb4dda2007-04-28 14:13:42 +0000501 }
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000502 default:
503 V = new Argument(Ty);
504 }
505
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000506 // Remember where this forward reference came from. FIXME, shouldn't we try
507 // to recycle these things??
Chris Lattner64116f92004-07-14 01:33:11 +0000508 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000509 LLLgetLineNo())));
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000510
Chris Lattner79df7c02002-03-26 18:01:55 +0000511 if (inFunctionScope())
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000512 InsertValue(V, CurFun.LateResolveValues);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000513 else
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000514 InsertValue(V, CurModule.LateResolveValues);
515 return V;
Chris Lattner00950542001-06-06 20:29:01 +0000516}
517
Reid Spencer186a43f2007-03-19 18:39:36 +0000518/// defineBBVal - This is a definition of a new basic block with the specified
519/// identifier which must be the same as CurFun.NextValNum, if its numeric.
520static BasicBlock *defineBBVal(const ValID &ID) {
Reid Spencer1755a9a2007-02-05 17:01:20 +0000521 assert(inFunctionScope() && "Can't get basic block at global scope!");
Chris Lattner64116f92004-07-14 01:33:11 +0000522
Chris Lattner2e2ed292004-07-14 06:28:35 +0000523 BasicBlock *BB = 0;
Chris Lattner64116f92004-07-14 01:33:11 +0000524
Reid Spencer186a43f2007-03-19 18:39:36 +0000525 // First, see if this was forward referenced
Chris Lattner64116f92004-07-14 01:33:11 +0000526
Reid Spencer186a43f2007-03-19 18:39:36 +0000527 std::map<ValID, BasicBlock*>::iterator BBI = CurFun.BBForwardRefs.find(ID);
528 if (BBI != CurFun.BBForwardRefs.end()) {
529 BB = BBI->second;
Chris Lattner2e2ed292004-07-14 06:28:35 +0000530 // The forward declaration could have been inserted anywhere in the
531 // function: insert it into the correct place now.
532 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
533 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
Reid Spencer186a43f2007-03-19 18:39:36 +0000534
Reid Spencer2c9df212007-03-20 01:13:00 +0000535 // We're about to erase the entry, save the key so we can clean it up.
536 ValID Tmp = BBI->first;
537
Reid Spencer186a43f2007-03-19 18:39:36 +0000538 // Erase the forward ref from the map as its no longer "forward"
539 CurFun.BBForwardRefs.erase(ID);
540
Reid Spencer2c9df212007-03-20 01:13:00 +0000541 // The key has been removed from the map but so we don't want to leave
542 // strdup'd memory around so destroy it too.
543 Tmp.destroy();
544
Reid Spencer186a43f2007-03-19 18:39:36 +0000545 // If its a numbered definition, bump the number and set the BB value.
546 if (ID.Type == ValID::LocalID) {
547 assert(ID.Num == CurFun.NextValNum && "Invalid new block number");
548 InsertValue(BB);
549 }
550
551 ID.destroy();
552 return BB;
553 }
554
555 // We haven't seen this BB before and its first mention is a definition.
556 // Just create it and return it.
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000557 std::string Name (ID.Type == ValID::LocalName ? ID.getName() : "");
Reid Spencer186a43f2007-03-19 18:39:36 +0000558 BB = new BasicBlock(Name, CurFun.CurrentFunction);
559 if (ID.Type == ValID::LocalID) {
560 assert(ID.Num == CurFun.NextValNum && "Invalid new block number");
561 InsertValue(BB);
Chris Lattner2e2ed292004-07-14 06:28:35 +0000562 }
Reid Spencer186a43f2007-03-19 18:39:36 +0000563
564 ID.destroy(); // Free strdup'd memory
565 return BB;
566}
567
568/// getBBVal - get an existing BB value or create a forward reference for it.
569///
570static BasicBlock *getBBVal(const ValID &ID) {
571 assert(inFunctionScope() && "Can't get basic block at global scope!");
572
573 BasicBlock *BB = 0;
574
575 std::map<ValID, BasicBlock*>::iterator BBI = CurFun.BBForwardRefs.find(ID);
576 if (BBI != CurFun.BBForwardRefs.end()) {
577 BB = BBI->second;
578 } if (ID.Type == ValID::LocalName) {
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000579 std::string Name = ID.getName();
Reid Spencer186a43f2007-03-19 18:39:36 +0000580 Value *N = CurFun.CurrentFunction->getValueSymbolTable().lookup(Name);
Anton Korobeynikov4aefd6b2008-02-20 12:07:57 +0000581 if (N) {
Reid Spencer186a43f2007-03-19 18:39:36 +0000582 if (N->getType()->getTypeID() == Type::LabelTyID)
583 BB = cast<BasicBlock>(N);
584 else
585 GenerateError("Reference to label '" + Name + "' is actually of type '"+
586 N->getType()->getDescription() + "'");
Anton Korobeynikov4aefd6b2008-02-20 12:07:57 +0000587 }
Reid Spencer186a43f2007-03-19 18:39:36 +0000588 } else if (ID.Type == ValID::LocalID) {
589 if (ID.Num < CurFun.NextValNum && ID.Num < CurFun.Values.size()) {
590 if (CurFun.Values[ID.Num]->getType()->getTypeID() == Type::LabelTyID)
591 BB = cast<BasicBlock>(CurFun.Values[ID.Num]);
592 else
593 GenerateError("Reference to label '%" + utostr(ID.Num) +
594 "' is actually of type '"+
595 CurFun.Values[ID.Num]->getType()->getDescription() + "'");
596 }
597 } else {
598 GenerateError("Illegal label reference " + ID.getName());
599 return 0;
600 }
601
602 // If its already been defined, return it now.
603 if (BB) {
604 ID.destroy(); // Free strdup'd memory.
605 return BB;
606 }
607
608 // Otherwise, this block has not been seen before, create it.
609 std::string Name;
610 if (ID.Type == ValID::LocalName)
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000611 Name = ID.getName();
Reid Spencer186a43f2007-03-19 18:39:36 +0000612 BB = new BasicBlock(Name, CurFun.CurrentFunction);
613
614 // Insert it in the forward refs map.
615 CurFun.BBForwardRefs[ID] = BB;
616
Chris Lattner64116f92004-07-14 01:33:11 +0000617 return BB;
618}
619
Chris Lattner00950542001-06-06 20:29:01 +0000620
621//===----------------------------------------------------------------------===//
622// Code to handle forward references in instructions
623//===----------------------------------------------------------------------===//
624//
625// This code handles the late binding needed with statements that reference
626// values not defined yet... for example, a forward branch, or the PHI node for
627// a loop body.
628//
Chris Lattner394f2eb2003-10-16 20:12:13 +0000629// This keeps a table (CurFun.LateResolveValues) of all such forward references
Chris Lattner00950542001-06-06 20:29:01 +0000630// and back patchs after we are done.
631//
632
Misha Brukman5a2a3822005-05-10 22:02:28 +0000633// ResolveDefinitions - If we could not resolve some defs at parsing
634// time (forward branches, phi functions for loops, etc...) resolve the
Chris Lattner00950542001-06-06 20:29:01 +0000635// defs now...
636//
Misha Brukman5a2a3822005-05-10 22:02:28 +0000637static void
Reid Spencer186a43f2007-03-19 18:39:36 +0000638ResolveDefinitions(ValueList &LateResolvers, ValueList *FutureLateResolvers) {
Chris Lattner00950542001-06-06 20:29:01 +0000639 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Reid Spencer186a43f2007-03-19 18:39:36 +0000640 while (!LateResolvers.empty()) {
641 Value *V = LateResolvers.back();
642 LateResolvers.pop_back();
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000643
Reid Spencer186a43f2007-03-19 18:39:36 +0000644 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
645 CurModule.PlaceHolderInfo.find(V);
646 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000647
Reid Spencer186a43f2007-03-19 18:39:36 +0000648 ValID &DID = PHI->second.first;
Chris Lattner00950542001-06-06 20:29:01 +0000649
Reid Spencer186a43f2007-03-19 18:39:36 +0000650 Value *TheRealValue = getExistingVal(V->getType(), DID);
651 if (TriggerError)
652 return;
653 if (TheRealValue) {
654 V->replaceAllUsesWith(TheRealValue);
655 delete V;
656 CurModule.PlaceHolderInfo.erase(PHI);
657 } else if (FutureLateResolvers) {
658 // Functions have their unresolved items forwarded to the module late
659 // resolver table
660 InsertValue(V, *FutureLateResolvers);
661 } else {
662 if (DID.Type == ValID::LocalName || DID.Type == ValID::GlobalName) {
663 GenerateError("Reference to an invalid definition: '" +DID.getName()+
664 "' of type '" + V->getType()->getDescription() + "'",
665 PHI->second.second);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000666 return;
Chris Lattner386a3b72001-10-16 19:54:17 +0000667 } else {
Reid Spencer186a43f2007-03-19 18:39:36 +0000668 GenerateError("Reference to an invalid definition: #" +
669 itostr(DID.Num) + " of type '" +
670 V->getType()->getDescription() + "'",
671 PHI->second.second);
672 return;
Chris Lattner30c89792001-09-07 16:35:17 +0000673 }
Chris Lattner00950542001-06-06 20:29:01 +0000674 }
675 }
Chris Lattner00950542001-06-06 20:29:01 +0000676 LateResolvers.clear();
677}
678
Chris Lattner4a42e902001-10-22 05:56:09 +0000679// ResolveTypeTo - A brand new type was just declared. This means that (if
680// name is not null) things referencing Name can be resolved. Otherwise, things
681// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000682//
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000683static void ResolveTypeTo(std::string *Name, const Type *ToTy) {
Chris Lattner355ad1f2005-03-21 06:27:42 +0000684 ValID D;
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000685 if (Name)
686 D = ValID::createLocalName(*Name);
687 else
688 D = ValID::createLocalID(CurModule.Types.size());
Chris Lattner00950542001-06-06 20:29:01 +0000689
Reid Spencerb78b9082006-11-28 07:28:14 +0000690 std::map<ValID, PATypeHolder>::iterator I =
Chris Lattner355ad1f2005-03-21 06:27:42 +0000691 CurModule.LateResolveTypes.find(D);
692 if (I != CurModule.LateResolveTypes.end()) {
Reid Spencerb78b9082006-11-28 07:28:14 +0000693 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattner355ad1f2005-03-21 06:27:42 +0000694 CurModule.LateResolveTypes.erase(I);
Chris Lattner30c89792001-09-07 16:35:17 +0000695 }
696}
697
Chris Lattner1781aca2001-09-18 04:00:54 +0000698// setValueName - Set the specified value to the name given. The name may be
699// null potentially, in which case this is a noop. The string passed in is
Chris Lattner8ab406d2004-07-13 07:59:27 +0000700// assumed to be a malloc'd string buffer, and is free'd by this function.
701//
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000702static void setValueName(Value *V, std::string *NameStr) {
Reid Spencerb2d17862007-01-26 08:04:51 +0000703 if (!NameStr) return;
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000704 std::string Name(*NameStr); // Copy string
705 delete NameStr; // Free old string
Chris Lattnerc9aea522004-07-13 08:12:39 +0000706
Reid Spencerb2d17862007-01-26 08:04:51 +0000707 if (V->getType() == Type::VoidTy) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000708 GenerateError("Can't assign name '" + Name+"' to value with void type");
Reid Spencerb2d17862007-01-26 08:04:51 +0000709 return;
Chris Lattner8ab406d2004-07-13 07:59:27 +0000710 }
Reid Spencerb2d17862007-01-26 08:04:51 +0000711
Reid Spencer1755a9a2007-02-05 17:01:20 +0000712 assert(inFunctionScope() && "Must be in function scope!");
Reid Spenceref9b9a72007-02-05 20:47:22 +0000713 ValueSymbolTable &ST = CurFun.CurrentFunction->getValueSymbolTable();
714 if (ST.lookup(Name)) {
Reid Spencerb2d17862007-01-26 08:04:51 +0000715 GenerateError("Redefinition of value '" + Name + "' of type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +0000716 V->getType()->getDescription() + "'");
Reid Spencerb2d17862007-01-26 08:04:51 +0000717 return;
718 }
719
720 // Set the name.
721 V->setName(Name);
Chris Lattner8ab406d2004-07-13 07:59:27 +0000722}
723
Chris Lattnerd88998d2004-07-14 08:23:52 +0000724/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
725/// this is a declaration, otherwise it is a definition.
Chris Lattnerb2b96672005-11-12 18:21:21 +0000726static GlobalVariable *
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000727ParseGlobalVariable(std::string *NameStr,
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000728 GlobalValue::LinkageTypes Linkage,
729 GlobalValue::VisibilityTypes Visibility,
Chris Lattnerb2b96672005-11-12 18:21:21 +0000730 bool isConstantGlobal, const Type *Ty,
Christopher Lambfe63fb92007-12-11 08:59:05 +0000731 Constant *Initializer, bool IsThreadLocal,
732 unsigned AddressSpace = 0) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000733 if (isa<FunctionType>(Ty)) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000734 GenerateError("Cannot declare global vars of function type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000735 return 0;
736 }
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000737
Christopher Lambfe63fb92007-12-11 08:59:05 +0000738 const PointerType *PTy = PointerType::get(Ty, AddressSpace);
Chris Lattnera09000d2004-07-14 23:03:46 +0000739
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000740 std::string Name;
741 if (NameStr) {
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000742 Name = *NameStr; // Copy string
743 delete NameStr; // Free old string
Chris Lattnerd88998d2004-07-14 08:23:52 +0000744 }
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000745
Chris Lattner8a327842004-07-14 21:44:00 +0000746 // See if this global value was forward referenced. If so, recycle the
747 // object.
Misha Brukman5a2a3822005-05-10 22:02:28 +0000748 ValID ID;
Chris Lattner8a327842004-07-14 21:44:00 +0000749 if (!Name.empty()) {
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000750 ID = ValID::createGlobalName(Name);
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000751 } else {
Reid Spencer186a43f2007-03-19 18:39:36 +0000752 ID = ValID::createGlobalID(CurModule.Values.size());
Chris Lattner8a327842004-07-14 21:44:00 +0000753 }
754
Reid Spencer863dd882007-04-28 16:06:50 +0000755 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000756 // Move the global to the end of the list, from whereever it was
Chris Lattner8a327842004-07-14 21:44:00 +0000757 // previously inserted.
758 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
759 CurModule.CurrentModule->getGlobalList().remove(GV);
760 CurModule.CurrentModule->getGlobalList().push_back(GV);
761 GV->setInitializer(Initializer);
762 GV->setLinkage(Linkage);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000763 GV->setVisibility(Visibility);
Chris Lattner8a327842004-07-14 21:44:00 +0000764 GV->setConstant(isConstantGlobal);
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +0000765 GV->setThreadLocal(IsThreadLocal);
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000766 InsertValue(GV, CurModule.Values);
Chris Lattnerb2b96672005-11-12 18:21:21 +0000767 return GV;
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000768 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000769
Reid Spenceref9b9a72007-02-05 20:47:22 +0000770 // If this global has a name
Chris Lattnera09000d2004-07-14 23:03:46 +0000771 if (!Name.empty()) {
Reid Spenceref9b9a72007-02-05 20:47:22 +0000772 // if the global we're parsing has an initializer (is a definition) and
773 // has external linkage.
774 if (Initializer && Linkage != GlobalValue::InternalLinkage)
775 // If there is already a global with external linkage with this name
776 if (CurModule.CurrentModule->getGlobalVariable(Name, false)) {
777 // If we allow this GVar to get created, it will be renamed in the
778 // symbol table because it conflicts with an existing GVar. We can't
779 // allow redefinition of GVars whose linking indicates that their name
780 // must stay the same. Issue the error.
781 GenerateError("Redefinition of global variable named '" + Name +
782 "' of type '" + Ty->getDescription() + "'");
783 return 0;
784 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000785 }
786
787 // Otherwise there is no existing GV to use, create one now.
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000788 GlobalVariable *GV =
Misha Brukman5a2a3822005-05-10 22:02:28 +0000789 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
Christopher Lambfe63fb92007-12-11 08:59:05 +0000790 CurModule.CurrentModule, IsThreadLocal, AddressSpace);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000791 GV->setVisibility(Visibility);
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000792 InsertValue(GV, CurModule.Values);
Chris Lattnerb2b96672005-11-12 18:21:21 +0000793 return GV;
Chris Lattnerd88998d2004-07-14 08:23:52 +0000794}
Chris Lattner8ab406d2004-07-13 07:59:27 +0000795
Reid Spencer75719bf2004-05-26 21:48:31 +0000796// setTypeName - Set the specified type to the name given. The name may be
797// null potentially, in which case this is a noop. The string passed in is
798// assumed to be a malloc'd string buffer, and is freed by this function.
799//
800// This function returns true if the type has already been defined, but is
801// allowed to be redefined in the specified context. If the name is a new name
802// for the type plane, it is inserted and false is returned.
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000803static bool setTypeName(const Type *T, std::string *NameStr) {
Reid Spencer1755a9a2007-02-05 17:01:20 +0000804 assert(!inFunctionScope() && "Can't give types function-local names!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000805 if (NameStr == 0) return false;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000806
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000807 std::string Name(*NameStr); // Copy string
808 delete NameStr; // Free old string
Reid Spencer75719bf2004-05-26 21:48:31 +0000809
810 // We don't allow assigning names to void type
Reid Spencer5b7e7532006-09-28 19:28:24 +0000811 if (T == Type::VoidTy) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000812 GenerateError("Can't assign name '" + Name + "' to the void type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000813 return false;
814 }
Reid Spencer75719bf2004-05-26 21:48:31 +0000815
Chris Lattnera09000d2004-07-14 23:03:46 +0000816 // Set the type name, checking for conflicts as we do so.
817 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000818
Chris Lattnera09000d2004-07-14 23:03:46 +0000819 if (AlreadyExists) { // Inserting a name that is already defined???
820 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
Reid Spencer1755a9a2007-02-05 17:01:20 +0000821 assert(Existing && "Conflict but no matching type?!");
Chris Lattnera09000d2004-07-14 23:03:46 +0000822
Reid Spencer75719bf2004-05-26 21:48:31 +0000823 // There is only one case where this is allowed: when we are refining an
824 // opaque type. In this case, Existing will be an opaque type.
825 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
826 // We ARE replacing an opaque type!
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000827 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000828 return true;
829 }
830
831 // Otherwise, this is an attempt to redefine a type. That's okay if
832 // the redefinition is identical to the original. This will be so if
833 // Existing and T point to the same Type object. In this one case we
834 // allow the equivalent redefinition.
835 if (Existing == T) return true; // Yes, it's equal.
836
837 // Any other kind of (non-equivalent) redefinition is an error.
Reid Spencer90e2f632007-01-05 21:50:38 +0000838 GenerateError("Redefinition of type named '" + Name + "' of type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +0000839 T->getDescription() + "'");
Reid Spencer75719bf2004-05-26 21:48:31 +0000840 }
841
Reid Spencer75719bf2004-05-26 21:48:31 +0000842 return false;
843}
Chris Lattner8896eda2001-07-09 19:38:36 +0000844
Chris Lattner30c89792001-09-07 16:35:17 +0000845//===----------------------------------------------------------------------===//
846// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000847//
Chris Lattner8896eda2001-07-09 19:38:36 +0000848
Chris Lattner3b073862004-02-09 03:19:29 +0000849// TypeContains - Returns true if Ty directly contains E in it.
Chris Lattner30c89792001-09-07 16:35:17 +0000850//
851static bool TypeContains(const Type *Ty, const Type *E) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000852 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
Chris Lattner6d8dbec2004-12-08 16:13:53 +0000853 E) != Ty->subtype_end();
Chris Lattner3b073862004-02-09 03:19:29 +0000854}
855
856namespace {
857 struct UpRefRecord {
858 // NestingLevel - The number of nesting levels that need to be popped before
859 // this type is resolved.
860 unsigned NestingLevel;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000861
Chris Lattner3b073862004-02-09 03:19:29 +0000862 // LastContainedTy - This is the type at the current binding level for the
863 // type. Every time we reduce the nesting level, this gets updated.
864 const Type *LastContainedTy;
865
866 // UpRefTy - This is the actual opaque type that the upreference is
867 // represented with.
868 OpaqueType *UpRefTy;
869
870 UpRefRecord(unsigned NL, OpaqueType *URTy)
871 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
872 };
Chris Lattner30c89792001-09-07 16:35:17 +0000873}
Chris Lattner698b56e2001-07-20 19:15:08 +0000874
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000875// UpRefs - A list of the outstanding upreferences that need to be resolved.
Chris Lattner3b073862004-02-09 03:19:29 +0000876static std::vector<UpRefRecord> UpRefs;
Chris Lattner30c89792001-09-07 16:35:17 +0000877
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000878/// HandleUpRefs - Every time we finish a new layer of types, this function is
879/// called. It loops through the UpRefs vector, which is a list of the
880/// currently active types. For each type, if the up reference is contained in
881/// the newly completed type, we decrement the level count. When the level
882/// count reaches zero, the upreferenced type is the type that is passed in:
883/// thus we can complete the cycle.
884///
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000885static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner703e92f2006-08-18 17:34:24 +0000886 // If Ty isn't abstract, or if there are no up-references in it, then there is
887 // nothing to resolve here.
888 if (!ty->isAbstract() || UpRefs.empty()) return ty;
889
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000890 PATypeHolder Ty(ty);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000891 UR_OUT("Type '" << Ty->getDescription() <<
Chris Lattner5084d032001-11-02 07:46:26 +0000892 "' newly formed. Resolving upreferences.\n" <<
893 UpRefs.size() << " upreferences active!\n");
Chris Lattner026a8ce2004-02-09 18:53:54 +0000894
895 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
896 // to zero), we resolve them all together before we resolve them to Ty. At
897 // the end of the loop, if there is anything to resolve to Ty, it will be in
898 // this variable.
899 OpaqueType *TypeToResolve = 0;
900
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000901 for (unsigned i = 0; i != UpRefs.size(); ++i) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000902 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
903 << UpRefs[i].second->getDescription() << ") = "
Reid Spencer3271ed52004-07-18 00:08:11 +0000904 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
Chris Lattner3b073862004-02-09 03:19:29 +0000905 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
906 // Decrement level of upreference
907 unsigned Level = --UpRefs[i].NestingLevel;
908 UpRefs[i].LastContainedTy = Ty;
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000909 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
Misha Brukman5a2a3822005-05-10 22:02:28 +0000910 if (Level == 0) { // Upreference should be resolved!
Chris Lattner026a8ce2004-02-09 18:53:54 +0000911 if (!TypeToResolve) {
912 TypeToResolve = UpRefs[i].UpRefTy;
913 } else {
914 UR_OUT(" * Resolving upreference for "
915 << UpRefs[i].second->getDescription() << "\n";
916 std::string OldName = UpRefs[i].UpRefTy->getDescription());
917 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
918 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
919 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
920 }
Reid Spencer3271ed52004-07-18 00:08:11 +0000921 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000922 --i; // Do not skip the next element...
Chris Lattner30c89792001-09-07 16:35:17 +0000923 }
924 }
Chris Lattner8896eda2001-07-09 19:38:36 +0000925 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000926
Chris Lattner026a8ce2004-02-09 18:53:54 +0000927 if (TypeToResolve) {
928 UR_OUT(" * Resolving upreference for "
929 << UpRefs[i].second->getDescription() << "\n";
Chris Lattner16af11d2004-02-09 21:03:38 +0000930 std::string OldName = TypeToResolve->getDescription());
Chris Lattner026a8ce2004-02-09 18:53:54 +0000931 TypeToResolve->refineAbstractTypeTo(Ty);
932 }
933
Chris Lattner8896eda2001-07-09 19:38:36 +0000934 return Ty;
935}
936
Chris Lattner00950542001-06-06 20:29:01 +0000937//===----------------------------------------------------------------------===//
938// RunVMAsmParser - Define an interface to this parser
939//===----------------------------------------------------------------------===//
940//
Reid Spencere1553cc2006-12-31 05:40:12 +0000941static Module* RunParser(Module * M);
942
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000943Module *llvm::RunVMAsmParser(llvm::MemoryBuffer *MB) {
944 InitLLLexer(MB);
945 Module *M = RunParser(new Module(LLLgetFilename()));
946 FreeLexer();
947 return M;
Chris Lattner00950542001-06-06 20:29:01 +0000948}
949
950%}
951
952%union {
Brian Gaeked0fde302003-11-11 22:41:34 +0000953 llvm::Module *ModuleVal;
954 llvm::Function *FunctionVal;
Brian Gaeked0fde302003-11-11 22:41:34 +0000955 llvm::BasicBlock *BasicBlockVal;
956 llvm::TerminatorInst *TermInstVal;
957 llvm::Instruction *InstVal;
Reid Spencer08de34b2006-12-03 05:45:44 +0000958 llvm::Constant *ConstVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000959
Reid Spencer08de34b2006-12-03 05:45:44 +0000960 const llvm::Type *PrimType;
Reid Spencere1553cc2006-12-31 05:40:12 +0000961 std::list<llvm::PATypeHolder> *TypeList;
Reid Spencer08de34b2006-12-03 05:45:44 +0000962 llvm::PATypeHolder *TypeVal;
963 llvm::Value *ValueVal;
Reid Spencer08de34b2006-12-03 05:45:44 +0000964 std::vector<llvm::Value*> *ValueList;
Reid Spencere1553cc2006-12-31 05:40:12 +0000965 llvm::ArgListType *ArgList;
966 llvm::TypeWithAttrs TypeWithAttrs;
967 llvm::TypeWithAttrsList *TypeWithAttrsList;
Dale Johanneseneb57ea72007-11-05 21:20:28 +0000968 llvm::ParamList *ParamList;
Reid Spencere1553cc2006-12-31 05:40:12 +0000969
Misha Brukman5a2a3822005-05-10 22:02:28 +0000970 // Represent the RHS of PHI node
Reid Spencer08de34b2006-12-03 05:45:44 +0000971 std::list<std::pair<llvm::Value*,
972 llvm::BasicBlock*> > *PHIList;
Brian Gaeked0fde302003-11-11 22:41:34 +0000973 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
Reid Spencer08de34b2006-12-03 05:45:44 +0000974 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000975
Brian Gaeked0fde302003-11-11 22:41:34 +0000976 llvm::GlobalValue::LinkageTypes Linkage;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000977 llvm::GlobalValue::VisibilityTypes Visibility;
Dale Johannesen0d51e7e2008-02-19 21:38:47 +0000978 llvm::ParameterAttributes ParamAttrs;
Reid Spencerf2310042007-02-28 02:23:44 +0000979 llvm::APInt *APIntVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000980 int64_t SInt64Val;
981 uint64_t UInt64Val;
982 int SIntVal;
983 unsigned UIntVal;
Dale Johannesen43421b32007-09-06 18:13:44 +0000984 llvm::APFloat *FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000985 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000986
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000987 std::string *StrVal; // This memory must be deleted
988 llvm::ValID ValIDVal;
Chris Lattner00950542001-06-06 20:29:01 +0000989
Reid Spencer08de34b2006-12-03 05:45:44 +0000990 llvm::Instruction::BinaryOps BinaryOpVal;
991 llvm::Instruction::TermOps TermOpVal;
992 llvm::Instruction::MemoryOps MemOpVal;
993 llvm::Instruction::CastOps CastOpVal;
994 llvm::Instruction::OtherOps OtherOpVal;
Reid Spencer08de34b2006-12-03 05:45:44 +0000995 llvm::ICmpInst::Predicate IPredicate;
996 llvm::FCmpInst::Predicate FPredicate;
Chris Lattner00950542001-06-06 20:29:01 +0000997}
998
Reid Spencere1553cc2006-12-31 05:40:12 +0000999%type <ModuleVal> Module
Chris Lattner79df7c02002-03-26 18:01:55 +00001000%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +00001001%type <BasicBlockVal> BasicBlock InstructionList
1002%type <TermInstVal> BBTerminatorInst
1003%type <InstVal> Inst InstVal MemoryInst
Anton Korobeynikova80e1182007-04-28 13:45:00 +00001004%type <ConstVal> ConstVal ConstExpr AliaseeRef
Chris Lattner6cdb0112001-11-26 16:54:11 +00001005%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +00001006%type <ArgList> ArgList ArgListH
Chris Lattnerc24d2082001-06-11 15:04:20 +00001007%type <PHIList> PHIList
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001008%type <ParamList> ParamList // For call param lists & GEP indices
Reid Spencere1553cc2006-12-31 05:40:12 +00001009%type <ValueList> IndexList // For GEP indices
1010%type <TypeList> TypeListI
1011%type <TypeWithAttrsList> ArgTypeList ArgTypeListI
Reid Spencer2c261782007-01-05 17:06:19 +00001012%type <TypeWithAttrs> ArgType
Chris Lattner00950542001-06-06 20:29:01 +00001013%type <JumpTable> JumpTable
Chris Lattner4ad02e72003-04-16 20:28:45 +00001014%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00001015%type <BoolVal> ThreadLocal // 'thread_local' or not
Chris Lattner15c9c032003-09-08 18:20:29 +00001016%type <BoolVal> OptVolatile // 'volatile' or not
Chris Lattnerddb6db42005-05-06 05:51:46 +00001017%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
Chris Lattneraa2c8532006-01-25 22:26:43 +00001018%type <BoolVal> OptSideEffect // 'sideeffect' or not.
Reid Spencere1553cc2006-12-31 05:40:12 +00001019%type <Linkage> GVInternalLinkage GVExternalLinkage
1020%type <Linkage> FunctionDefineLinkage FunctionDeclareLinkage
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001021%type <Linkage> AliasLinkage
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001022%type <Visibility> GVVisibilityStyle
Chris Lattner00950542001-06-06 20:29:01 +00001023
Chris Lattner2079fde2001-10-13 06:41:08 +00001024// ValueRef - Unresolved reference to a definition or BB
1025%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001026%type <ValueVal> ResolvedVal // <type> <valref> pair
Devang Patel5af2f632008-02-20 22:39:45 +00001027%type <ValueList> ReturnedVal
Chris Lattner00950542001-06-06 20:29:01 +00001028// Tokens and types for handling constant integer values
1029//
1030// ESINT64VAL - A negative number within long long range
1031%token <SInt64Val> ESINT64VAL
1032
1033// EUINT64VAL - A positive number within uns. long long range
1034%token <UInt64Val> EUINT64VAL
Chris Lattner00950542001-06-06 20:29:01 +00001035
Reid Spencerf2310042007-02-28 02:23:44 +00001036// ESAPINTVAL - A negative number with arbitrary precision
1037%token <APIntVal> ESAPINTVAL
1038
1039// EUAPINTVAL - A positive number with arbitrary precision
1040%token <APIntVal> EUAPINTVAL
1041
Reid Spencerb2d17862007-01-26 08:04:51 +00001042%token <UIntVal> LOCALVAL_ID GLOBALVAL_ID // %123 @123
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001043%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +00001044
1045// Built in types...
Reid Spencer2c261782007-01-05 17:06:19 +00001046%type <TypeVal> Types ResultTypes
Reid Spencere1553cc2006-12-31 05:40:12 +00001047%type <PrimType> IntType FPType PrimType // Classifications
Reid Spencer8088e9d2007-01-13 05:00:20 +00001048%token <PrimType> VOID INTTYPE
Dale Johannesen320fc8a2007-08-03 01:03:46 +00001049%token <PrimType> FLOAT DOUBLE X86_FP80 FP128 PPC_FP128 LABEL
Reid Spencerb951bc02006-12-29 20:29:48 +00001050%token TYPE
Chris Lattner00950542001-06-06 20:29:01 +00001051
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001052
Reid Spenceraa7c2f82007-05-19 07:21:26 +00001053%token<StrVal> LOCALVAR GLOBALVAR LABELSTR
1054%token<StrVal> STRINGCONSTANT ATSTRINGCONSTANT PCTSTRINGCONSTANT
Reid Spencerb2d17862007-01-26 08:04:51 +00001055%type <StrVal> LocalName OptLocalName OptLocalAssign
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001056%type <StrVal> GlobalName OptGlobalAssign GlobalAssign
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00001057%type <StrVal> OptSection SectionString OptGC
Chris Lattner00950542001-06-06 20:29:01 +00001058
Christopher Lambd49e18d2007-12-12 08:44:39 +00001059%type <UIntVal> OptAlign OptCAlign OptAddrSpace
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001060
Reid Spencer744d0362007-04-09 01:55:42 +00001061%token ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001062%token DECLARE DEFINE GLOBAL CONSTANT SECTION ALIAS VOLATILE THREAD_LOCAL
Reid Spencere1553cc2006-12-31 05:40:12 +00001063%token TO DOTDOTDOT NULL_TOK UNDEF INTERNAL LINKONCE WEAK APPENDING
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00001064%token DLLIMPORT DLLEXPORT EXTERN_WEAK
Christopher Lambfe63fb92007-12-11 08:59:05 +00001065%token OPAQUE EXTERNAL TARGET TRIPLE ALIGN ADDRSPACE
Chris Lattneraa2c8532006-01-25 22:26:43 +00001066%token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001067%token CC_TOK CCC_TOK FASTCC_TOK COLDCC_TOK X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
Chris Lattner10b27112006-10-22 06:07:41 +00001068%token DATALAYOUT
Chris Lattnera8e8f162005-05-06 20:27:19 +00001069%type <UIntVal> OptCallingConv
Reid Spencer2c261782007-01-05 17:06:19 +00001070%type <ParamAttrs> OptParamAttrs ParamAttr
1071%type <ParamAttrs> OptFuncAttrs FuncAttr
Chris Lattner00950542001-06-06 20:29:01 +00001072
Misha Brukman5a2a3822005-05-10 22:02:28 +00001073// Basic Block Terminating Operators
Chris Lattner16710e92004-10-16 18:17:13 +00001074%token <TermOpVal> RET BR SWITCH INVOKE UNWIND UNREACHABLE
Chris Lattner00950542001-06-06 20:29:01 +00001075
Misha Brukman5a2a3822005-05-10 22:02:28 +00001076// Binary Operators
Reid Spencere4d87aa2006-12-23 06:05:41 +00001077%type <BinaryOpVal> ArithmeticOps LogicalOps // Binops Subcatagories
Reid Spencer0a783f72006-11-02 01:53:59 +00001078%token <BinaryOpVal> ADD SUB MUL UDIV SDIV FDIV UREM SREM FREM AND OR XOR
Reid Spencer832254e2007-02-02 02:16:23 +00001079%token <BinaryOpVal> SHL LSHR ASHR
1080
Reid Spencer08de34b2006-12-03 05:45:44 +00001081%token <OtherOpVal> ICMP FCMP
Reid Spencer08de34b2006-12-03 05:45:44 +00001082%type <IPredicate> IPredicates
Reid Spencer08de34b2006-12-03 05:45:44 +00001083%type <FPredicate> FPredicates
Reid Spencer9f746f42006-12-03 06:58:07 +00001084%token EQ NE SLT SGT SLE SGE ULT UGT ULE UGE
1085%token OEQ ONE OLT OGT OLE OGE ORD UNO UEQ UNE
Chris Lattner00950542001-06-06 20:29:01 +00001086
1087// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001088%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +00001089
Reid Spencer3da59db2006-11-27 01:05:10 +00001090// Cast Operators
1091%type <CastOpVal> CastOps
1092%token <CastOpVal> TRUNC ZEXT SEXT FPTRUNC FPEXT BITCAST
1093%token <CastOpVal> UITOFP SITOFP FPTOUI FPTOSI INTTOPTR PTRTOINT
1094
Chris Lattner027dcc52001-07-08 21:10:27 +00001095// Other Operators
Reid Spencer832254e2007-02-02 02:16:23 +00001096%token <OtherOpVal> PHI_TOK SELECT VAARG
Chris Lattner4c949082006-04-08 01:18:35 +00001097%token <OtherOpVal> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
Devang Pateld6ffcf92008-02-19 22:26:37 +00001098%token <OtherOpVal> GETRESULT
Chris Lattnerddb6db42005-05-06 05:51:46 +00001099
Reid Spencer2c261782007-01-05 17:06:19 +00001100// Function Attributes
Duncan Sands36397f52007-07-27 12:58:54 +00001101%token SIGNEXT ZEROEXT NORETURN INREG SRET NOUNWIND NOALIAS BYVAL NEST
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00001102%token READNONE READONLY GC
Chris Lattner027dcc52001-07-08 21:10:27 +00001103
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001104// Visibility Styles
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +00001105%token DEFAULT HIDDEN PROTECTED
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001106
Chris Lattner00950542001-06-06 20:29:01 +00001107%start Module
1108%%
1109
Chris Lattner00950542001-06-06 20:29:01 +00001110
Misha Brukman5a2a3822005-05-10 22:02:28 +00001111// Operations that are notably excluded from this list include:
Chris Lattner00950542001-06-06 20:29:01 +00001112// RET, BR, & SWITCH because they end basic blocks and are treated specially.
1113//
Reid Spencer0a783f72006-11-02 01:53:59 +00001114ArithmeticOps: ADD | SUB | MUL | UDIV | SDIV | FDIV | UREM | SREM | FREM;
Reid Spencer832254e2007-02-02 02:16:23 +00001115LogicalOps : SHL | LSHR | ASHR | AND | OR | XOR;
Reid Spencer3da59db2006-11-27 01:05:10 +00001116CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | BITCAST |
1117 UITOFP | SITOFP | FPTOUI | FPTOSI | INTTOPTR | PTRTOINT;
Reid Spencer832254e2007-02-02 02:16:23 +00001118
Reid Spencer9f746f42006-12-03 06:58:07 +00001119IPredicates
Reid Spencer763ed5e2006-12-04 05:20:06 +00001120 : EQ { $$ = ICmpInst::ICMP_EQ; } | NE { $$ = ICmpInst::ICMP_NE; }
Reid Spencer9f746f42006-12-03 06:58:07 +00001121 | SLT { $$ = ICmpInst::ICMP_SLT; } | SGT { $$ = ICmpInst::ICMP_SGT; }
1122 | SLE { $$ = ICmpInst::ICMP_SLE; } | SGE { $$ = ICmpInst::ICMP_SGE; }
1123 | ULT { $$ = ICmpInst::ICMP_ULT; } | UGT { $$ = ICmpInst::ICMP_UGT; }
1124 | ULE { $$ = ICmpInst::ICMP_ULE; } | UGE { $$ = ICmpInst::ICMP_UGE; }
1125 ;
1126
1127FPredicates
1128 : OEQ { $$ = FCmpInst::FCMP_OEQ; } | ONE { $$ = FCmpInst::FCMP_ONE; }
1129 | OLT { $$ = FCmpInst::FCMP_OLT; } | OGT { $$ = FCmpInst::FCMP_OGT; }
1130 | OLE { $$ = FCmpInst::FCMP_OLE; } | OGE { $$ = FCmpInst::FCMP_OGE; }
1131 | ORD { $$ = FCmpInst::FCMP_ORD; } | UNO { $$ = FCmpInst::FCMP_UNO; }
1132 | UEQ { $$ = FCmpInst::FCMP_UEQ; } | UNE { $$ = FCmpInst::FCMP_UNE; }
1133 | ULT { $$ = FCmpInst::FCMP_ULT; } | UGT { $$ = FCmpInst::FCMP_UGT; }
1134 | ULE { $$ = FCmpInst::FCMP_ULE; } | UGE { $$ = FCmpInst::FCMP_UGE; }
1135 | TRUETOK { $$ = FCmpInst::FCMP_TRUE; }
1136 | FALSETOK { $$ = FCmpInst::FCMP_FALSE; }
1137 ;
Chris Lattner00950542001-06-06 20:29:01 +00001138
Chris Lattnere98dda62001-07-14 06:10:16 +00001139// These are some types that allow classification if we only want a particular
1140// thing... for example, only a signed, unsigned, or integral type.
Reid Spencera54b7cb2007-01-12 07:05:14 +00001141IntType : INTTYPE;
Dale Johannesen320fc8a2007-08-03 01:03:46 +00001142FPType : FLOAT | DOUBLE | PPC_FP128 | FP128 | X86_FP80;
Chris Lattner00950542001-06-06 20:29:01 +00001143
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001144LocalName : LOCALVAR | STRINGCONSTANT | PCTSTRINGCONSTANT ;
Reid Spencerb2d17862007-01-26 08:04:51 +00001145OptLocalName : LocalName | /*empty*/ { $$ = 0; };
1146
Christopher Lambd49e18d2007-12-12 08:44:39 +00001147OptAddrSpace : ADDRSPACE '(' EUINT64VAL ')' { $$=$3; }
1148 | /*empty*/ { $$=0; };
1149
Reid Spencerb2d17862007-01-26 08:04:51 +00001150/// OptLocalAssign - Value producing statements have an optional assignment
1151/// component.
1152OptLocalAssign : LocalName '=' {
1153 $$ = $1;
1154 CHECK_FOR_ERROR
1155 }
1156 | /*empty*/ {
1157 $$ = 0;
1158 CHECK_FOR_ERROR
1159 };
1160
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001161GlobalName : GLOBALVAR | ATSTRINGCONSTANT ;
Reid Spencerb2d17862007-01-26 08:04:51 +00001162
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001163OptGlobalAssign : GlobalAssign
Misha Brukman5a2a3822005-05-10 22:02:28 +00001164 | /*empty*/ {
1165 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00001166 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001167 };
Chris Lattner00950542001-06-06 20:29:01 +00001168
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001169GlobalAssign : GlobalName '=' {
1170 $$ = $1;
1171 CHECK_FOR_ERROR
Anton Korobeynikovc0fabcb2007-04-25 18:07:40 +00001172 };
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001173
Reid Spencerb951bc02006-12-29 20:29:48 +00001174GVInternalLinkage
1175 : INTERNAL { $$ = GlobalValue::InternalLinkage; }
1176 | WEAK { $$ = GlobalValue::WeakLinkage; }
1177 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1178 | APPENDING { $$ = GlobalValue::AppendingLinkage; }
1179 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
1180 ;
1181
1182GVExternalLinkage
1183 : DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1184 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
1185 | EXTERNAL { $$ = GlobalValue::ExternalLinkage; }
1186 ;
1187
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001188GVVisibilityStyle
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +00001189 : /*empty*/ { $$ = GlobalValue::DefaultVisibility; }
1190 | DEFAULT { $$ = GlobalValue::DefaultVisibility; }
1191 | HIDDEN { $$ = GlobalValue::HiddenVisibility; }
1192 | PROTECTED { $$ = GlobalValue::ProtectedVisibility; }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001193 ;
1194
Reid Spencere1553cc2006-12-31 05:40:12 +00001195FunctionDeclareLinkage
1196 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1197 | DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1198 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
Reid Spencerb951bc02006-12-29 20:29:48 +00001199 ;
1200
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001201FunctionDefineLinkage
Reid Spencere1553cc2006-12-31 05:40:12 +00001202 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1203 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
Reid Spencerb951bc02006-12-29 20:29:48 +00001204 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1205 | WEAK { $$ = GlobalValue::WeakLinkage; }
1206 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
Reid Spencerb951bc02006-12-29 20:29:48 +00001207 ;
Chris Lattner30c89792001-09-07 16:35:17 +00001208
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001209AliasLinkage
1210 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1211 | WEAK { $$ = GlobalValue::WeakLinkage; }
1212 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
1213 ;
1214
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00001215OptCallingConv : /*empty*/ { $$ = CallingConv::C; } |
1216 CCC_TOK { $$ = CallingConv::C; } |
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00001217 FASTCC_TOK { $$ = CallingConv::Fast; } |
1218 COLDCC_TOK { $$ = CallingConv::Cold; } |
1219 X86_STDCALLCC_TOK { $$ = CallingConv::X86_StdCall; } |
1220 X86_FASTCALLCC_TOK { $$ = CallingConv::X86_FastCall; } |
1221 CC_TOK EUINT64VAL {
Chris Lattnera8e8f162005-05-06 20:27:19 +00001222 if ((unsigned)$2 != $2)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001223 GEN_ERROR("Calling conv too large");
Chris Lattnera8e8f162005-05-06 20:27:19 +00001224 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001225 CHECK_FOR_ERROR
Chris Lattnera8e8f162005-05-06 20:27:19 +00001226 };
1227
Reid Spencer9445e9a2007-07-19 23:13:04 +00001228ParamAttr : ZEROEXT { $$ = ParamAttr::ZExt; }
Reid Spencer1d3d2302007-07-31 02:57:37 +00001229 | ZEXT { $$ = ParamAttr::ZExt; }
Reid Spencer9445e9a2007-07-19 23:13:04 +00001230 | SIGNEXT { $$ = ParamAttr::SExt; }
Reid Spencer1d3d2302007-07-31 02:57:37 +00001231 | SEXT { $$ = ParamAttr::SExt; }
Zhou Shengfebca342007-06-05 05:28:26 +00001232 | INREG { $$ = ParamAttr::InReg; }
1233 | SRET { $$ = ParamAttr::StructRet; }
1234 | NOALIAS { $$ = ParamAttr::NoAlias; }
Duncan Sands36397f52007-07-27 12:58:54 +00001235 | BYVAL { $$ = ParamAttr::ByVal; }
1236 | NEST { $$ = ParamAttr::Nest; }
Dale Johannesen8d1433c2008-02-20 21:15:19 +00001237 | ALIGN EUINT64VAL { $$ = $2 << 16; }
Reid Spencere1553cc2006-12-31 05:40:12 +00001238 ;
1239
Reid Spencer18da0722007-04-11 02:44:20 +00001240OptParamAttrs : /* empty */ { $$ = ParamAttr::None; }
Reid Spencer2c261782007-01-05 17:06:19 +00001241 | OptParamAttrs ParamAttr {
Reid Spencer5694b6e2007-04-09 06:17:21 +00001242 $$ = $1 | $2;
Reid Spencere1553cc2006-12-31 05:40:12 +00001243 }
1244 ;
1245
Reid Spencer18da0722007-04-11 02:44:20 +00001246FuncAttr : NORETURN { $$ = ParamAttr::NoReturn; }
1247 | NOUNWIND { $$ = ParamAttr::NoUnwind; }
Reid Spencer9445e9a2007-07-19 23:13:04 +00001248 | ZEROEXT { $$ = ParamAttr::ZExt; }
1249 | SIGNEXT { $$ = ParamAttr::SExt; }
Duncan Sandsed4a2f12007-11-22 20:23:04 +00001250 | READNONE { $$ = ParamAttr::ReadNone; }
1251 | READONLY { $$ = ParamAttr::ReadOnly; }
Reid Spencer2c261782007-01-05 17:06:19 +00001252 ;
1253
Reid Spencer18da0722007-04-11 02:44:20 +00001254OptFuncAttrs : /* empty */ { $$ = ParamAttr::None; }
Reid Spencer2c261782007-01-05 17:06:19 +00001255 | OptFuncAttrs FuncAttr {
Reid Spencer5694b6e2007-04-09 06:17:21 +00001256 $$ = $1 | $2;
Reid Spencer2c261782007-01-05 17:06:19 +00001257 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001258 ;
1259
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00001260OptGC : /* empty */ { $$ = 0; }
1261 | GC STRINGCONSTANT {
1262 $$ = $2;
1263 }
1264 ;
1265
Chris Lattner66db8e42005-11-06 06:34:12 +00001266// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1267// a comma before it.
Chris Lattner87ac9722005-11-06 06:46:28 +00001268OptAlign : /*empty*/ { $$ = 0; } |
Chris Lattnerb2b96672005-11-12 18:21:21 +00001269 ALIGN EUINT64VAL {
1270 $$ = $2;
1271 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001272 GEN_ERROR("Alignment must be a power of two");
Reid Spencer61c83e02006-08-18 08:43:06 +00001273 CHECK_FOR_ERROR
Chris Lattnerb2b96672005-11-12 18:21:21 +00001274};
Chris Lattner66db8e42005-11-06 06:34:12 +00001275OptCAlign : /*empty*/ { $$ = 0; } |
Chris Lattnerb2b96672005-11-12 18:21:21 +00001276 ',' ALIGN EUINT64VAL {
1277 $$ = $3;
1278 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001279 GEN_ERROR("Alignment must be a power of two");
Reid Spencer61c83e02006-08-18 08:43:06 +00001280 CHECK_FOR_ERROR
Chris Lattnerb2b96672005-11-12 18:21:21 +00001281};
1282
Chris Lattner66db8e42005-11-06 06:34:12 +00001283
Christopher Lambfe63fb92007-12-11 08:59:05 +00001284
Chris Lattner164c3782005-11-12 00:11:10 +00001285SectionString : SECTION STRINGCONSTANT {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001286 for (unsigned i = 0, e = $2->length(); i != e; ++i)
1287 if ((*$2)[i] == '"' || (*$2)[i] == '\\')
Reid Spencerf4fa5902007-02-05 10:16:10 +00001288 GEN_ERROR("Invalid character in section name");
Chris Lattner164c3782005-11-12 00:11:10 +00001289 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001290 CHECK_FOR_ERROR
Chris Lattner164c3782005-11-12 00:11:10 +00001291};
1292
1293OptSection : /*empty*/ { $$ = 0; } |
1294 SectionString { $$ = $1; };
Chris Lattner164c3782005-11-12 00:11:10 +00001295
Chris Lattnerb2b96672005-11-12 18:21:21 +00001296// GlobalVarAttributes - Used to pass the attributes string on a global. CurGV
1297// is set to be the global we are processing.
1298//
1299GlobalVarAttributes : /* empty */ {} |
1300 ',' GlobalVarAttribute GlobalVarAttributes {};
1301GlobalVarAttribute : SectionString {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001302 CurGV->setSection(*$1);
1303 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001304 CHECK_FOR_ERROR
Chris Lattnerb2b96672005-11-12 18:21:21 +00001305 }
1306 | ALIGN EUINT64VAL {
1307 if ($2 != 0 && !isPowerOf2_32($2))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001308 GEN_ERROR("Alignment must be a power of two");
Chris Lattnerb2b96672005-11-12 18:21:21 +00001309 CurGV->setAlignment($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001310 CHECK_FOR_ERROR
Chris Lattnerb2b96672005-11-12 18:21:21 +00001311 };
Chris Lattner164c3782005-11-12 00:11:10 +00001312
Chris Lattner30c89792001-09-07 16:35:17 +00001313//===----------------------------------------------------------------------===//
1314// Types includes all predefined types... except void, because it can only be
Reid Spencere1553cc2006-12-31 05:40:12 +00001315// used in specific contexts (function returning void for example).
Chris Lattner30c89792001-09-07 16:35:17 +00001316
1317// Derived types are added later...
1318//
Dale Johannesen320fc8a2007-08-03 01:03:46 +00001319PrimType : INTTYPE | FLOAT | DOUBLE | PPC_FP128 | FP128 | X86_FP80 | LABEL ;
Reid Spencere1553cc2006-12-31 05:40:12 +00001320
1321Types
1322 : OPAQUE {
Reid Spencer08de34b2006-12-03 05:45:44 +00001323 $$ = new PATypeHolder(OpaqueType::get());
Reid Spencer61c83e02006-08-18 08:43:06 +00001324 CHECK_FOR_ERROR
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001325 }
1326 | PrimType {
Reid Spencer08de34b2006-12-03 05:45:44 +00001327 $$ = new PATypeHolder($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001328 CHECK_FOR_ERROR
Reid Spencere1553cc2006-12-31 05:40:12 +00001329 }
Christopher Lambd49e18d2007-12-12 08:44:39 +00001330 | Types OptAddrSpace '*' { // Pointer type?
Reid Spencere1553cc2006-12-31 05:40:12 +00001331 if (*$1 == Type::LabelTy)
1332 GEN_ERROR("Cannot form a pointer to a basic block");
Christopher Lambd49e18d2007-12-12 08:44:39 +00001333 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1, $2)));
Christopher Lambfe63fb92007-12-11 08:59:05 +00001334 delete $1;
1335 CHECK_FOR_ERROR
1336 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001337 | SymbolicValueRef { // Named types are also simple types...
1338 const Type* tmp = getTypeVal($1);
1339 CHECK_FOR_ERROR
1340 $$ = new PATypeHolder(tmp);
1341 }
1342 | '\\' EUINT64VAL { // Type UpReference
Reid Spencerf4fa5902007-02-05 10:16:10 +00001343 if ($2 > (uint64_t)~0U) GEN_ERROR("Value out of range");
Chris Lattner30c89792001-09-07 16:35:17 +00001344 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
Chris Lattner3b073862004-02-09 03:19:29 +00001345 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Reid Spencer08de34b2006-12-03 05:45:44 +00001346 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +00001347 UR_OUT("New Upreference!\n");
Reid Spencer61c83e02006-08-18 08:43:06 +00001348 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001349 }
Reid Spencer863dd882007-04-28 16:06:50 +00001350 | Types '(' ArgTypeListI ')' OptFuncAttrs {
Duncan Sandsdc024672007-11-27 13:23:08 +00001351 // Allow but ignore attributes on function types; this permits auto-upgrade.
1352 // FIXME: remove in LLVM 3.0.
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001353 const Type* RetTy = *$1;
Anton Korobeynikovf8c751a2007-12-03 21:00:45 +00001354 if (!(RetTy->isFirstClassType() || RetTy == Type::VoidTy ||
1355 isa<OpaqueType>(RetTy)))
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001356 GEN_ERROR("LLVM Functions cannot return aggregates");
1357
Chris Lattner9232b992003-04-22 18:42:41 +00001358 std::vector<const Type*> Params;
Reid Spencer5694b6e2007-04-09 06:17:21 +00001359 TypeWithAttrsList::iterator I = $3->begin(), E = $3->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00001360 for (; I != E; ++I ) {
Reid Spencer2c9df212007-03-20 01:13:00 +00001361 const Type *Ty = I->Ty->get();
Reid Spencer2c9df212007-03-20 01:13:00 +00001362 Params.push_back(Ty);
Reid Spencere1553cc2006-12-31 05:40:12 +00001363 }
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001364
Chris Lattner2079fde2001-10-13 06:41:08 +00001365 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1366 if (isVarArg) Params.pop_back();
1367
Anton Korobeynikovf8c751a2007-12-03 21:00:45 +00001368 for (unsigned i = 0; i != Params.size(); ++i)
1369 if (!(Params[i]->isFirstClassType() || isa<OpaqueType>(Params[i])))
1370 GEN_ERROR("Function arguments must be value types!");
1371
1372 CHECK_FOR_ERROR
1373
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001374 FunctionType *FT = FunctionType::get(RetTy, Params, isVarArg);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001375 delete $3; // Delete the argument list
Reid Spencere1553cc2006-12-31 05:40:12 +00001376 delete $1; // Delete the return type handle
1377 $$ = new PATypeHolder(HandleUpRefs(FT));
Reid Spencer61c83e02006-08-18 08:43:06 +00001378 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001379 }
Reid Spencer863dd882007-04-28 16:06:50 +00001380 | VOID '(' ArgTypeListI ')' OptFuncAttrs {
Duncan Sandsdc024672007-11-27 13:23:08 +00001381 // Allow but ignore attributes on function types; this permits auto-upgrade.
1382 // FIXME: remove in LLVM 3.0.
Reid Spencere1553cc2006-12-31 05:40:12 +00001383 std::vector<const Type*> Params;
Reid Spencer5694b6e2007-04-09 06:17:21 +00001384 TypeWithAttrsList::iterator I = $3->begin(), E = $3->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00001385 for ( ; I != E; ++I ) {
Reid Spencer2c9df212007-03-20 01:13:00 +00001386 const Type* Ty = I->Ty->get();
Reid Spencer2c9df212007-03-20 01:13:00 +00001387 Params.push_back(Ty);
Reid Spencere1553cc2006-12-31 05:40:12 +00001388 }
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001389
Reid Spencere1553cc2006-12-31 05:40:12 +00001390 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1391 if (isVarArg) Params.pop_back();
1392
Anton Korobeynikovf8c751a2007-12-03 21:00:45 +00001393 for (unsigned i = 0; i != Params.size(); ++i)
1394 if (!(Params[i]->isFirstClassType() || isa<OpaqueType>(Params[i])))
1395 GEN_ERROR("Function arguments must be value types!");
1396
1397 CHECK_FOR_ERROR
1398
Duncan Sandsdc024672007-11-27 13:23:08 +00001399 FunctionType *FT = FunctionType::get($1, Params, isVarArg);
Reid Spencer2c261782007-01-05 17:06:19 +00001400 delete $3; // Delete the argument list
Reid Spencere1553cc2006-12-31 05:40:12 +00001401 $$ = new PATypeHolder(HandleUpRefs(FT));
1402 CHECK_FOR_ERROR
1403 }
1404
1405 | '[' EUINT64VAL 'x' Types ']' { // Sized array type?
Reid Spencer08de34b2006-12-03 05:45:44 +00001406 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
1407 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00001408 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001409 }
Reid Spencerac9dcb92007-02-15 03:39:18 +00001410 | '<' EUINT64VAL 'x' Types '>' { // Vector type?
Reid Spencer08de34b2006-12-03 05:45:44 +00001411 const llvm::Type* ElemTy = $4->get();
1412 if ((unsigned)$2 != $2)
1413 GEN_ERROR("Unsigned result not equal to signed result");
Chris Lattner42a75512007-01-15 02:27:26 +00001414 if (!ElemTy->isFloatingPoint() && !ElemTy->isInteger())
Reid Spencer9d6565a2007-02-15 02:26:10 +00001415 GEN_ERROR("Element type of a VectorType must be primitive");
Reid Spencer9d6565a2007-02-15 02:26:10 +00001416 $$ = new PATypeHolder(HandleUpRefs(VectorType::get(*$4, (unsigned)$2)));
Reid Spencer08de34b2006-12-03 05:45:44 +00001417 delete $4;
1418 CHECK_FOR_ERROR
Brian Gaeke715c90b2004-08-20 06:00:58 +00001419 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001420 | '{' TypeListI '}' { // Structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001421 std::vector<const Type*> Elements;
Reid Spencer08de34b2006-12-03 05:45:44 +00001422 for (std::list<llvm::PATypeHolder>::iterator I = $2->begin(),
Chris Lattnera08976b2005-02-22 23:13:58 +00001423 E = $2->end(); I != E; ++I)
Reid Spencer08de34b2006-12-03 05:45:44 +00001424 Elements.push_back(*I);
Misha Brukman5a2a3822005-05-10 22:02:28 +00001425
Reid Spencer08de34b2006-12-03 05:45:44 +00001426 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001427 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001428 CHECK_FOR_ERROR
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001429 }
1430 | '{' '}' { // Empty structure type?
Reid Spencer08de34b2006-12-03 05:45:44 +00001431 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Reid Spencer61c83e02006-08-18 08:43:06 +00001432 CHECK_FOR_ERROR
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001433 }
Andrew Lenharth38ecbf12006-12-08 18:06:16 +00001434 | '<' '{' TypeListI '}' '>' {
1435 std::vector<const Type*> Elements;
1436 for (std::list<llvm::PATypeHolder>::iterator I = $3->begin(),
1437 E = $3->end(); I != E; ++I)
1438 Elements.push_back(*I);
1439
1440 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements, true)));
1441 delete $3;
1442 CHECK_FOR_ERROR
1443 }
1444 | '<' '{' '}' '>' { // Empty structure type?
1445 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>(), true));
1446 CHECK_FOR_ERROR
1447 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001448 ;
1449
1450ArgType
Duncan Sandsdc024672007-11-27 13:23:08 +00001451 : Types OptParamAttrs {
1452 // Allow but ignore attributes on function types; this permits auto-upgrade.
1453 // FIXME: remove in LLVM 3.0.
Reid Spencere1553cc2006-12-31 05:40:12 +00001454 $$.Ty = $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00001455 $$.Attrs = ParamAttr::None;
Reid Spencere1553cc2006-12-31 05:40:12 +00001456 }
1457 ;
1458
Reid Spencer2c261782007-01-05 17:06:19 +00001459ResultTypes
1460 : Types {
Reid Spencere1553cc2006-12-31 05:40:12 +00001461 if (!UpRefs.empty())
1462 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Devang Patel5af2f632008-02-20 22:39:45 +00001463 if (!(*$1)->isFirstClassType() && (*$1)->getTypeID() != Type::StructTyID)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001464 GEN_ERROR("LLVM functions cannot return aggregate types");
Reid Spencer2c261782007-01-05 17:06:19 +00001465 $$ = $1;
Reid Spencere1553cc2006-12-31 05:40:12 +00001466 }
Reid Spencer2c261782007-01-05 17:06:19 +00001467 | VOID {
1468 $$ = new PATypeHolder(Type::VoidTy);
Reid Spencere1553cc2006-12-31 05:40:12 +00001469 }
1470 ;
1471
1472ArgTypeList : ArgType {
1473 $$ = new TypeWithAttrsList();
1474 $$->push_back($1);
1475 CHECK_FOR_ERROR
1476 }
1477 | ArgTypeList ',' ArgType {
1478 ($$=$1)->push_back($3);
1479 CHECK_FOR_ERROR
1480 }
1481 ;
1482
1483ArgTypeListI
1484 : ArgTypeList
1485 | ArgTypeList ',' DOTDOTDOT {
1486 $$=$1;
Reid Spencer18da0722007-04-11 02:44:20 +00001487 TypeWithAttrs TWA; TWA.Attrs = ParamAttr::None;
Reid Spencere1553cc2006-12-31 05:40:12 +00001488 TWA.Ty = new PATypeHolder(Type::VoidTy);
1489 $$->push_back(TWA);
1490 CHECK_FOR_ERROR
1491 }
1492 | DOTDOTDOT {
1493 $$ = new TypeWithAttrsList;
Reid Spencer18da0722007-04-11 02:44:20 +00001494 TypeWithAttrs TWA; TWA.Attrs = ParamAttr::None;
Reid Spencere1553cc2006-12-31 05:40:12 +00001495 TWA.Ty = new PATypeHolder(Type::VoidTy);
1496 $$->push_back(TWA);
1497 CHECK_FOR_ERROR
1498 }
1499 | /*empty*/ {
1500 $$ = new TypeWithAttrsList();
Reid Spencer61c83e02006-08-18 08:43:06 +00001501 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001502 };
Chris Lattner30c89792001-09-07 16:35:17 +00001503
Chris Lattner7e708292002-06-25 16:13:24 +00001504// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +00001505// declaration type lists
1506//
Reid Spencere1553cc2006-12-31 05:40:12 +00001507TypeListI : Types {
Reid Spencer08de34b2006-12-03 05:45:44 +00001508 $$ = new std::list<PATypeHolder>();
Reid Spencer2c9df212007-03-20 01:13:00 +00001509 $$->push_back(*$1);
1510 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001511 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001512 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001513 | TypeListI ',' Types {
Reid Spencer2c9df212007-03-20 01:13:00 +00001514 ($$=$1)->push_back(*$3);
1515 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001516 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001517 };
Chris Lattner30c89792001-09-07 16:35:17 +00001518
Chris Lattnere98dda62001-07-14 06:10:16 +00001519// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +00001520// production is used ONLY to represent constants that show up AFTER a 'const',
1521// 'constant' or 'global' token at global scope. Constants that can be inlined
1522// into other expressions (such as integers and constexprs) are handled by the
1523// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +00001524//
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001525ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencere1553cc2006-12-31 05:40:12 +00001526 if (!UpRefs.empty())
1527 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001528 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001529 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001530 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001531 (*$1)->getDescription() + "'");
Chris Lattner30c89792001-09-07 16:35:17 +00001532 const Type *ETy = ATy->getElementType();
1533 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +00001534
Chris Lattner30c89792001-09-07 16:35:17 +00001535 // Verify that we have the correct size...
1536 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001537 GEN_ERROR("Type mismatch: constant sized array initialized with " +
Reid Spencer3271ed52004-07-18 00:08:11 +00001538 utostr($3->size()) + " arguments, but has size of " +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001539 itostr(NumElements) + "");
Chris Lattner00950542001-06-06 20:29:01 +00001540
Chris Lattner30c89792001-09-07 16:35:17 +00001541 // Verify all elements are correct type!
1542 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencer08de34b2006-12-03 05:45:44 +00001543 if (ETy != (*$3)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001544 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Reid Spencer3271ed52004-07-18 00:08:11 +00001545 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencer08de34b2006-12-03 05:45:44 +00001546 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +00001547 }
1548
Reid Spencer08de34b2006-12-03 05:45:44 +00001549 $$ = ConstantArray::get(ATy, *$3);
1550 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001551 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001552 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001553 | Types '[' ']' {
Reid Spencere1553cc2006-12-31 05:40:12 +00001554 if (!UpRefs.empty())
1555 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001556 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001557 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001558 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001559 (*$1)->getDescription() + "'");
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001560
1561 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +00001562 if (NumElements != -1 && NumElements != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001563 GEN_ERROR("Type mismatch: constant sized array initialized with 0"
Reid Spencerf4fa5902007-02-05 10:16:10 +00001564 " arguments, but has size of " + itostr(NumElements) +"");
Reid Spencer08de34b2006-12-03 05:45:44 +00001565 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
1566 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001567 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001568 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001569 | Types 'c' STRINGCONSTANT {
Reid Spencere1553cc2006-12-31 05:40:12 +00001570 if (!UpRefs.empty())
1571 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001572 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001573 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001574 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001575 (*$1)->getDescription() + "'");
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001576
Chris Lattner30c89792001-09-07 16:35:17 +00001577 int NumElements = ATy->getNumElements();
1578 const Type *ETy = ATy->getElementType();
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001579 if (NumElements != -1 && NumElements != int($3->length()))
Reid Spencer61c83e02006-08-18 08:43:06 +00001580 GEN_ERROR("Can't build string constant of size " +
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001581 itostr((int)($3->length())) +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001582 " when array has size " + itostr(NumElements) + "");
Chris Lattner9232b992003-04-22 18:42:41 +00001583 std::vector<Constant*> Vals;
Reid Spencere1553cc2006-12-31 05:40:12 +00001584 if (ETy == Type::Int8Ty) {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001585 for (unsigned i = 0; i < $3->length(); ++i)
1586 Vals.push_back(ConstantInt::get(ETy, (*$3)[i]));
Chris Lattner93750fa2001-07-28 17:48:55 +00001587 } else {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001588 delete $3;
Reid Spencerf4fa5902007-02-05 10:16:10 +00001589 GEN_ERROR("Cannot build string arrays of non byte sized elements");
Chris Lattner93750fa2001-07-28 17:48:55 +00001590 }
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001591 delete $3;
Reid Spencer08de34b2006-12-03 05:45:44 +00001592 $$ = ConstantArray::get(ATy, Vals);
1593 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001594 CHECK_FOR_ERROR
Chris Lattner93750fa2001-07-28 17:48:55 +00001595 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00001596 | Types '<' ConstVector '>' { // Nonempty unsized arr
Reid Spencere1553cc2006-12-31 05:40:12 +00001597 if (!UpRefs.empty())
1598 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer9d6565a2007-02-15 02:26:10 +00001599 const VectorType *PTy = dyn_cast<VectorType>($1->get());
Brian Gaeke715c90b2004-08-20 06:00:58 +00001600 if (PTy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001601 GEN_ERROR("Cannot make packed constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001602 (*$1)->getDescription() + "'");
Brian Gaeke715c90b2004-08-20 06:00:58 +00001603 const Type *ETy = PTy->getElementType();
1604 int NumElements = PTy->getNumElements();
1605
1606 // Verify that we have the correct size...
1607 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001608 GEN_ERROR("Type mismatch: constant sized packed initialized with " +
Brian Gaeke715c90b2004-08-20 06:00:58 +00001609 utostr($3->size()) + " arguments, but has size of " +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001610 itostr(NumElements) + "");
Brian Gaeke715c90b2004-08-20 06:00:58 +00001611
1612 // Verify all elements are correct type!
1613 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencer08de34b2006-12-03 05:45:44 +00001614 if (ETy != (*$3)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001615 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Brian Gaeke715c90b2004-08-20 06:00:58 +00001616 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencer08de34b2006-12-03 05:45:44 +00001617 (*$3)[i]->getType()->getDescription() + "'.");
Brian Gaeke715c90b2004-08-20 06:00:58 +00001618 }
1619
Reid Spencer9d6565a2007-02-15 02:26:10 +00001620 $$ = ConstantVector::get(PTy, *$3);
Reid Spencer08de34b2006-12-03 05:45:44 +00001621 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001622 CHECK_FOR_ERROR
Brian Gaeke715c90b2004-08-20 06:00:58 +00001623 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001624 | Types '{' ConstVector '}' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001625 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001626 if (STy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001627 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001628 (*$1)->getDescription() + "'");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001629
Chris Lattnerc6212f12003-05-21 16:06:56 +00001630 if ($3->size() != STy->getNumContainedTypes())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001631 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattnerc6212f12003-05-21 16:06:56 +00001632
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001633 // Check to ensure that constants are compatible with the type initializer!
1634 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Reid Spencer08de34b2006-12-03 05:45:44 +00001635 if ((*$3)[i]->getType() != STy->getElementType(i))
Reid Spencer61c83e02006-08-18 08:43:06 +00001636 GEN_ERROR("Expected type '" +
Chris Lattnerd21cd802004-02-09 04:37:31 +00001637 STy->getElementType(i)->getDescription() +
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001638 "' for element #" + utostr(i) +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001639 " of structure initializer");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001640
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001641 // Check to ensure that Type is not packed
1642 if (STy->isPacked())
Chris Lattner4989b842007-04-26 05:30:35 +00001643 GEN_ERROR("Unpacked Initializer to vector type '" +
1644 STy->getDescription() + "'");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001645
Reid Spencer08de34b2006-12-03 05:45:44 +00001646 $$ = ConstantStruct::get(STy, *$3);
1647 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001648 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001649 }
Chris Lattnerc6212f12003-05-21 16:06:56 +00001650 | Types '{' '}' {
Reid Spencere1553cc2006-12-31 05:40:12 +00001651 if (!UpRefs.empty())
1652 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001653 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerc6212f12003-05-21 16:06:56 +00001654 if (STy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001655 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001656 (*$1)->getDescription() + "'");
Chris Lattnerc6212f12003-05-21 16:06:56 +00001657
1658 if (STy->getNumContainedTypes() != 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001659 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattnerc6212f12003-05-21 16:06:56 +00001660
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001661 // Check to ensure that Type is not packed
1662 if (STy->isPacked())
Chris Lattner4989b842007-04-26 05:30:35 +00001663 GEN_ERROR("Unpacked Initializer to vector type '" +
1664 STy->getDescription() + "'");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001665
1666 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1667 delete $1;
1668 CHECK_FOR_ERROR
1669 }
1670 | Types '<' '{' ConstVector '}' '>' {
1671 const StructType *STy = dyn_cast<StructType>($1->get());
1672 if (STy == 0)
1673 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001674 (*$1)->getDescription() + "'");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001675
1676 if ($4->size() != STy->getNumContainedTypes())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001677 GEN_ERROR("Illegal number of initializers for structure type");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001678
1679 // Check to ensure that constants are compatible with the type initializer!
1680 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1681 if ((*$4)[i]->getType() != STy->getElementType(i))
1682 GEN_ERROR("Expected type '" +
1683 STy->getElementType(i)->getDescription() +
1684 "' for element #" + utostr(i) +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001685 " of structure initializer");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001686
1687 // Check to ensure that Type is packed
1688 if (!STy->isPacked())
Reid Spencerac9dcb92007-02-15 03:39:18 +00001689 GEN_ERROR("Vector initializer to non-vector type '" +
1690 STy->getDescription() + "'");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001691
1692 $$ = ConstantStruct::get(STy, *$4);
1693 delete $1; delete $4;
1694 CHECK_FOR_ERROR
1695 }
1696 | Types '<' '{' '}' '>' {
1697 if (!UpRefs.empty())
1698 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1699 const StructType *STy = dyn_cast<StructType>($1->get());
1700 if (STy == 0)
1701 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001702 (*$1)->getDescription() + "'");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001703
1704 if (STy->getNumContainedTypes() != 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001705 GEN_ERROR("Illegal number of initializers for structure type");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001706
1707 // Check to ensure that Type is packed
1708 if (!STy->isPacked())
Reid Spencerac9dcb92007-02-15 03:39:18 +00001709 GEN_ERROR("Vector initializer to non-vector type '" +
1710 STy->getDescription() + "'");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001711
Reid Spencer08de34b2006-12-03 05:45:44 +00001712 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1713 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001714 CHECK_FOR_ERROR
Chris Lattnerc6212f12003-05-21 16:06:56 +00001715 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001716 | Types NULL_TOK {
Reid Spencere1553cc2006-12-31 05:40:12 +00001717 if (!UpRefs.empty())
1718 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001719 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001720 if (PTy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001721 GEN_ERROR("Cannot make null pointer constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001722 (*$1)->getDescription() + "'");
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001723
Reid Spencer08de34b2006-12-03 05:45:44 +00001724 $$ = ConstantPointerNull::get(PTy);
1725 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001726 CHECK_FOR_ERROR
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001727 }
Chris Lattner16710e92004-10-16 18:17:13 +00001728 | Types UNDEF {
Reid Spencere1553cc2006-12-31 05:40:12 +00001729 if (!UpRefs.empty())
1730 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001731 $$ = UndefValue::get($1->get());
1732 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001733 CHECK_FOR_ERROR
Chris Lattner16710e92004-10-16 18:17:13 +00001734 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001735 | Types SymbolicValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00001736 if (!UpRefs.empty())
1737 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001738 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001739 if (Ty == 0)
Devang Pateld6ffcf92008-02-19 22:26:37 +00001740 GEN_ERROR("Global const reference must be a pointer type " + (*$1)->getDescription());
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001741
Chris Lattner3101c252002-08-15 17:58:33 +00001742 // ConstExprs can exist in the body of a function, thus creating
Reid Spencer3271ed52004-07-18 00:08:11 +00001743 // GlobalValues whenever they refer to a variable. Because we are in
Reid Spencer186a43f2007-03-19 18:39:36 +00001744 // the context of a function, getExistingVal will search the functions
Chris Lattner3101c252002-08-15 17:58:33 +00001745 // symbol table instead of the module symbol table for the global symbol,
1746 // which throws things all off. To get around this, we just tell
Reid Spencer186a43f2007-03-19 18:39:36 +00001747 // getExistingVal that we are at global scope here.
Chris Lattner3101c252002-08-15 17:58:33 +00001748 //
Chris Lattner394f2eb2003-10-16 20:12:13 +00001749 Function *SavedCurFn = CurFun.CurrentFunction;
1750 CurFun.CurrentFunction = 0;
Chris Lattner3101c252002-08-15 17:58:33 +00001751
Reid Spencer186a43f2007-03-19 18:39:36 +00001752 Value *V = getExistingVal(Ty, $2);
Reid Spencer5b7e7532006-09-28 19:28:24 +00001753 CHECK_FOR_ERROR
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001754
Chris Lattner394f2eb2003-10-16 20:12:13 +00001755 CurFun.CurrentFunction = SavedCurFn;
Chris Lattner3101c252002-08-15 17:58:33 +00001756
Chris Lattner2079fde2001-10-13 06:41:08 +00001757 // If this is an initializer for a constant pointer, which is referencing a
1758 // (currently) undefined variable, create a stub now that shall be replaced
1759 // in the future with the right type of variable.
1760 //
1761 if (V == 0) {
Reid Spencer1755a9a2007-02-05 17:01:20 +00001762 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001763 const PointerType *PT = cast<PointerType>(Ty);
1764
1765 // First check to see if the forward references value is already created!
1766 PerModuleInfo::GlobalRefsType::iterator I =
Reid Spencer3271ed52004-07-18 00:08:11 +00001767 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Chris Lattner2079fde2001-10-13 06:41:08 +00001768
1769 if (I != CurModule.GlobalRefs.end()) {
Reid Spencer3271ed52004-07-18 00:08:11 +00001770 V = I->second; // Placeholder already exists, use it...
Chris Lattnera989b232003-12-23 20:05:15 +00001771 $2.destroy();
Chris Lattner2079fde2001-10-13 06:41:08 +00001772 } else {
Chris Lattner8a327842004-07-14 21:44:00 +00001773 std::string Name;
Reid Spencerb2d17862007-01-26 08:04:51 +00001774 if ($2.Type == ValID::GlobalName)
Reid Spencer6ecdcc12007-05-22 18:52:21 +00001775 Name = $2.getName();
Reid Spencerb2d17862007-01-26 08:04:51 +00001776 else if ($2.Type != ValID::GlobalID)
1777 GEN_ERROR("Invalid reference to global");
Chris Lattner8a327842004-07-14 21:44:00 +00001778
Reid Spencer3271ed52004-07-18 00:08:11 +00001779 // Create the forward referenced global.
Chris Lattnera09000d2004-07-14 23:03:46 +00001780 GlobalValue *GV;
1781 if (const FunctionType *FTy =
1782 dyn_cast<FunctionType>(PT->getElementType())) {
Chris Lattner4989b842007-04-26 05:30:35 +00001783 GV = new Function(FTy, GlobalValue::ExternalWeakLinkage, Name,
Chris Lattnera09000d2004-07-14 23:03:46 +00001784 CurModule.CurrentModule);
1785 } else {
Reid Spencer3271ed52004-07-18 00:08:11 +00001786 GV = new GlobalVariable(PT->getElementType(), false,
Chris Lattner4989b842007-04-26 05:30:35 +00001787 GlobalValue::ExternalWeakLinkage, 0,
Chris Lattnera09000d2004-07-14 23:03:46 +00001788 Name, CurModule.CurrentModule);
1789 }
Chris Lattner8a327842004-07-14 21:44:00 +00001790
Reid Spencer3271ed52004-07-18 00:08:11 +00001791 // Keep track of the fact that we have a forward ref to recycle it
1792 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1793 V = GV;
Chris Lattner2079fde2001-10-13 06:41:08 +00001794 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001795 }
1796
Reid Spencer08de34b2006-12-03 05:45:44 +00001797 $$ = cast<GlobalValue>(V);
1798 delete $1; // Free the type handle
Reid Spencer61c83e02006-08-18 08:43:06 +00001799 CHECK_FOR_ERROR
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001800 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001801 | Types ConstExpr {
Reid Spencere1553cc2006-12-31 05:40:12 +00001802 if (!UpRefs.empty())
1803 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001804 if ($1->get() != $2->getType())
Reid Spencerb4fdfdb2007-01-04 00:05:48 +00001805 GEN_ERROR("Mismatched types for constant expression: " +
1806 (*$1)->getDescription() + " and " + $2->getType()->getDescription());
Chris Lattnerd78700d2002-08-16 21:14:40 +00001807 $$ = $2;
Reid Spencer08de34b2006-12-03 05:45:44 +00001808 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001809 CHECK_FOR_ERROR
Chris Lattnerf4600482003-06-28 20:01:34 +00001810 }
1811 | Types ZEROINITIALIZER {
Reid Spencere1553cc2006-12-31 05:40:12 +00001812 if (!UpRefs.empty())
1813 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001814 const Type *Ty = $1->get();
Chris Lattner78915932004-11-28 16:45:45 +00001815 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001816 GEN_ERROR("Cannot create a null initialized value of this type");
Reid Spencer08de34b2006-12-03 05:45:44 +00001817 $$ = Constant::getNullValue(Ty);
1818 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001819 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00001820 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001821 | IntType ESINT64VAL { // integral constants
Reid Spencer9ffad0a2006-12-20 17:20:09 +00001822 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001823 GEN_ERROR("Constant value doesn't fit in type");
Reid Spencereac65742007-03-19 20:40:22 +00001824 $$ = ConstantInt::get($1, $2, true);
Reid Spencerf2310042007-02-28 02:23:44 +00001825 CHECK_FOR_ERROR
1826 }
1827 | IntType ESAPINTVAL { // arbitrary precision integer constants
1828 uint32_t BitWidth = cast<IntegerType>($1)->getBitWidth();
1829 if ($2->getBitWidth() > BitWidth) {
1830 GEN_ERROR("Constant value does not fit in type");
Reid Spenceraf1aacd2007-03-01 19:32:01 +00001831 }
1832 $2->sextOrTrunc(BitWidth);
1833 $$ = ConstantInt::get(*$2);
Reid Spencerf2310042007-02-28 02:23:44 +00001834 delete $2;
Reid Spencer9ffad0a2006-12-20 17:20:09 +00001835 CHECK_FOR_ERROR
1836 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001837 | IntType EUINT64VAL { // integral constants
Reid Spencer9ffad0a2006-12-20 17:20:09 +00001838 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001839 GEN_ERROR("Constant value doesn't fit in type");
Reid Spencereac65742007-03-19 20:40:22 +00001840 $$ = ConstantInt::get($1, $2, false);
Reid Spencerf2310042007-02-28 02:23:44 +00001841 CHECK_FOR_ERROR
1842 }
1843 | IntType EUAPINTVAL { // arbitrary precision integer constants
1844 uint32_t BitWidth = cast<IntegerType>($1)->getBitWidth();
1845 if ($2->getBitWidth() > BitWidth) {
1846 GEN_ERROR("Constant value does not fit in type");
Reid Spenceraf1aacd2007-03-01 19:32:01 +00001847 }
1848 $2->zextOrTrunc(BitWidth);
1849 $$ = ConstantInt::get(*$2);
Reid Spencerf2310042007-02-28 02:23:44 +00001850 delete $2;
Reid Spencer9ffad0a2006-12-20 17:20:09 +00001851 CHECK_FOR_ERROR
1852 }
Reid Spencer8088e9d2007-01-13 05:00:20 +00001853 | INTTYPE TRUETOK { // Boolean constants
1854 assert(cast<IntegerType>($1)->getBitWidth() == 1 && "Not Bool?");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001855 $$ = ConstantInt::getTrue();
Reid Spencer61c83e02006-08-18 08:43:06 +00001856 CHECK_FOR_ERROR
Chris Lattnerd05e3592002-08-15 18:17:28 +00001857 }
Reid Spencer8088e9d2007-01-13 05:00:20 +00001858 | INTTYPE FALSETOK { // Boolean constants
1859 assert(cast<IntegerType>($1)->getBitWidth() == 1 && "Not Bool?");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001860 $$ = ConstantInt::getFalse();
Reid Spencer61c83e02006-08-18 08:43:06 +00001861 CHECK_FOR_ERROR
Chris Lattnerd05e3592002-08-15 18:17:28 +00001862 }
Dale Johannesen9d5f4562007-09-12 03:30:33 +00001863 | FPType FPVAL { // Floating point constants
Dale Johannesen43421b32007-09-06 18:13:44 +00001864 if (!ConstantFP::isValueValidForType($1, *$2))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001865 GEN_ERROR("Floating point constant invalid for type");
Dale Johannesen3f6eb742007-09-11 18:32:33 +00001866 // Lexer has no type info, so builds all float and double FP constants
1867 // as double. Fix this here. Long double is done right.
1868 if (&$2->getSemantics()==&APFloat::IEEEdouble && $1==Type::FloatTy)
Dale Johannesen43421b32007-09-06 18:13:44 +00001869 $2->convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven);
1870 $$ = ConstantFP::get($1, *$2);
Dale Johannesencdd509a2007-09-07 21:07:57 +00001871 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001872 CHECK_FOR_ERROR
Chris Lattnerd05e3592002-08-15 18:17:28 +00001873 };
1874
Chris Lattner00950542001-06-06 20:29:01 +00001875
Reid Spencer3da59db2006-11-27 01:05:10 +00001876ConstExpr: CastOps '(' ConstVal TO Types ')' {
Reid Spencere1553cc2006-12-31 05:40:12 +00001877 if (!UpRefs.empty())
1878 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001879 Constant *Val = $3;
Reid Spencer93947c32007-01-17 02:47:33 +00001880 const Type *DestTy = $5->get();
1881 if (!CastInst::castIsValid($1, $3, DestTy))
1882 GEN_ERROR("invalid cast opcode for cast from '" +
1883 Val->getType()->getDescription() + "' to '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001884 DestTy->getDescription() + "'");
Reid Spencer93947c32007-01-17 02:47:33 +00001885 $$ = ConstantExpr::getCast($1, $3, DestTy);
Reid Spencer08de34b2006-12-03 05:45:44 +00001886 delete $5;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001887 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001888 | GETELEMENTPTR '(' ConstVal IndexList ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001889 if (!isa<PointerType>($3->getType()))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001890 GEN_ERROR("GetElementPtr requires a pointer operand");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001891
Reid Spencer08de34b2006-12-03 05:45:44 +00001892 const Type *IdxTy =
David Greeneb8f74792007-09-04 15:46:09 +00001893 GetElementPtrInst::getIndexedType($3->getType(), $4->begin(), $4->end(),
Chris Lattnerc856c7a2007-02-13 00:57:40 +00001894 true);
Reid Spencer08de34b2006-12-03 05:45:44 +00001895 if (!IdxTy)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001896 GEN_ERROR("Index list invalid for constant getelementptr");
Reid Spencer08de34b2006-12-03 05:45:44 +00001897
Chris Lattnere0135402007-01-31 04:43:46 +00001898 SmallVector<Constant*, 8> IdxVec;
Reid Spencer08de34b2006-12-03 05:45:44 +00001899 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1900 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001901 IdxVec.push_back(C);
1902 else
Reid Spencerf4fa5902007-02-05 10:16:10 +00001903 GEN_ERROR("Indices to constant getelementptr must be constants");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001904
Chris Lattnerd78700d2002-08-16 21:14:40 +00001905 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001906
Chris Lattnere0135402007-01-31 04:43:46 +00001907 $$ = ConstantExpr::getGetElementPtr($3, &IdxVec[0], IdxVec.size());
Reid Spencer61c83e02006-08-18 08:43:06 +00001908 CHECK_FOR_ERROR
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001909 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001910 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer4fe16d62007-01-11 18:21:29 +00001911 if ($3->getType() != Type::Int1Ty)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001912 GEN_ERROR("Select condition must be of boolean type");
Reid Spencer08de34b2006-12-03 05:45:44 +00001913 if ($5->getType() != $7->getType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001914 GEN_ERROR("Select operand types must match");
Reid Spencer08de34b2006-12-03 05:45:44 +00001915 $$ = ConstantExpr::getSelect($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001916 CHECK_FOR_ERROR
Chris Lattner76f0ff32004-03-12 05:51:36 +00001917 }
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001918 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001919 if ($3->getType() != $5->getType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001920 GEN_ERROR("Binary operator types must match");
Reid Spencer1628cec2006-10-26 06:15:43 +00001921 CHECK_FOR_ERROR;
Reid Spencerc6e956e2006-12-05 19:15:41 +00001922 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001923 }
1924 | LogicalOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001925 if ($3->getType() != $5->getType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001926 GEN_ERROR("Logical operator types must match");
Chris Lattner42a75512007-01-15 02:27:26 +00001927 if (!$3->getType()->isInteger()) {
Reid Spencer9d6565a2007-02-15 02:26:10 +00001928 if (Instruction::isShift($1) || !isa<VectorType>($3->getType()) ||
1929 !cast<VectorType>($3->getType())->getElementType()->isInteger())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001930 GEN_ERROR("Logical operator requires integral operands");
Chris Lattner0a017832005-12-21 18:31:29 +00001931 }
Reid Spencer08de34b2006-12-03 05:45:44 +00001932 $$ = ConstantExpr::get($1, $3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001933 CHECK_FOR_ERROR
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001934 }
Reid Spencer763ed5e2006-12-04 05:20:06 +00001935 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
1936 if ($4->getType() != $6->getType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001937 GEN_ERROR("icmp operand types must match");
Reid Spencer763ed5e2006-12-04 05:20:06 +00001938 $$ = ConstantExpr::getICmp($2, $4, $6);
Reid Spencer08de34b2006-12-03 05:45:44 +00001939 }
Reid Spencer763ed5e2006-12-04 05:20:06 +00001940 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
1941 if ($4->getType() != $6->getType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001942 GEN_ERROR("fcmp operand types must match");
Reid Spencer763ed5e2006-12-04 05:20:06 +00001943 $$ = ConstantExpr::getFCmp($2, $4, $6);
Reid Spencer08de34b2006-12-03 05:45:44 +00001944 }
Robert Bocchino9c62b562006-01-10 19:04:32 +00001945 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001946 if (!ExtractElementInst::isValidOperands($3, $5))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001947 GEN_ERROR("Invalid extractelement operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00001948 $$ = ConstantExpr::getExtractElement($3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001949 CHECK_FOR_ERROR
Chris Lattnerca73cd82006-04-08 03:53:34 +00001950 }
1951 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001952 if (!InsertElementInst::isValidOperands($3, $5, $7))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001953 GEN_ERROR("Invalid insertelement operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00001954 $$ = ConstantExpr::getInsertElement($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001955 CHECK_FOR_ERROR
Chris Lattnerca73cd82006-04-08 03:53:34 +00001956 }
1957 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001958 if (!ShuffleVectorInst::isValidOperands($3, $5, $7))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001959 GEN_ERROR("Invalid shufflevector operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00001960 $$ = ConstantExpr::getShuffleVector($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001961 CHECK_FOR_ERROR
Chris Lattner699f1eb2002-08-14 17:12:33 +00001962 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001963
Chris Lattnerca73cd82006-04-08 03:53:34 +00001964
Misha Brukmanbc0e9982003-07-14 17:20:40 +00001965// ConstVector - A list of comma separated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001966ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001967 ($$ = $1)->push_back($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00001968 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001969 }
1970 | ConstVal {
Reid Spencer08de34b2006-12-03 05:45:44 +00001971 $$ = new std::vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001972 $$->push_back($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001973 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001974 };
Chris Lattner00950542001-06-06 20:29:01 +00001975
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001976
Chris Lattner1781aca2001-09-18 04:00:54 +00001977// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001978GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001979
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00001980// ThreadLocal
1981ThreadLocal : THREAD_LOCAL { $$ = true; } | { $$ = false; };
1982
Anton Korobeynikova80e1182007-04-28 13:45:00 +00001983// AliaseeRef - Match either GlobalValue or bitcast to GlobalValue.
1984AliaseeRef : ResultTypes SymbolicValueRef {
1985 const Type* VTy = $1->get();
1986 Value *V = getVal(VTy, $2);
Chris Lattner6df20ef2007-08-06 21:00:37 +00001987 CHECK_FOR_ERROR
Anton Korobeynikova80e1182007-04-28 13:45:00 +00001988 GlobalValue* Aliasee = dyn_cast<GlobalValue>(V);
1989 if (!Aliasee)
1990 GEN_ERROR("Aliases can be created only to global values");
1991
1992 $$ = Aliasee;
1993 CHECK_FOR_ERROR
1994 delete $1;
1995 }
1996 | BITCAST '(' AliaseeRef TO Types ')' {
1997 Constant *Val = $3;
1998 const Type *DestTy = $5->get();
1999 if (!CastInst::castIsValid($1, $3, DestTy))
2000 GEN_ERROR("invalid cast opcode for cast from '" +
2001 Val->getType()->getDescription() + "' to '" +
2002 DestTy->getDescription() + "'");
2003
2004 $$ = ConstantExpr::getCast($1, $3, DestTy);
2005 CHECK_FOR_ERROR
2006 delete $5;
2007 };
Chris Lattner00950542001-06-06 20:29:01 +00002008
Chris Lattner0e73ce62002-05-02 19:11:13 +00002009//===----------------------------------------------------------------------===//
2010// Rules to match Modules
2011//===----------------------------------------------------------------------===//
2012
2013// Module rule: Capture the result of parsing the whole file into a result
2014// variable...
2015//
Reid Spencerb951bc02006-12-29 20:29:48 +00002016Module
2017 : DefinitionList {
2018 $$ = ParserResult = CurModule.CurrentModule;
2019 CurModule.ModuleDone();
2020 CHECK_FOR_ERROR;
2021 }
2022 | /*empty*/ {
2023 $$ = ParserResult = CurModule.CurrentModule;
2024 CurModule.ModuleDone();
2025 CHECK_FOR_ERROR;
2026 }
2027 ;
Chris Lattner0e73ce62002-05-02 19:11:13 +00002028
Reid Spencerb951bc02006-12-29 20:29:48 +00002029DefinitionList
2030 : Definition
2031 | DefinitionList Definition
2032 ;
2033
2034Definition
Jeff Cohen361c3ef2007-01-21 19:19:31 +00002035 : DEFINE { CurFun.isDeclare = false; } Function {
Chris Lattner394f2eb2003-10-16 20:12:13 +00002036 CurFun.FunctionDone();
Reid Spencer61c83e02006-08-18 08:43:06 +00002037 CHECK_FOR_ERROR
Reid Spencerb951bc02006-12-29 20:29:48 +00002038 }
2039 | DECLARE { CurFun.isDeclare = true; } FunctionProto {
Reid Spencer61c83e02006-08-18 08:43:06 +00002040 CHECK_FOR_ERROR
Chris Lattner0e73ce62002-05-02 19:11:13 +00002041 }
Reid Spencerb951bc02006-12-29 20:29:48 +00002042 | MODULE ASM_TOK AsmBlock {
Reid Spencer61c83e02006-08-18 08:43:06 +00002043 CHECK_FOR_ERROR
Chris Lattneree454772006-01-23 23:05:15 +00002044 }
Reid Spencerb2d17862007-01-26 08:04:51 +00002045 | OptLocalAssign TYPE Types {
Reid Spencere1553cc2006-12-31 05:40:12 +00002046 if (!UpRefs.empty())
2047 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattner4a42e902001-10-22 05:56:09 +00002048 // Eagerly resolve types. This is not an optimization, this is a
2049 // requirement that is due to the fact that we could have this:
2050 //
2051 // %list = type { %list * }
2052 // %list = type { %list * } ; repeated type decl
2053 //
2054 // If types are not resolved eagerly, then the two types will not be
2055 // determined to be the same type!
2056 //
Reid Spencerb951bc02006-12-29 20:29:48 +00002057 ResolveTypeTo($1, *$3);
Chris Lattner4a42e902001-10-22 05:56:09 +00002058
Reid Spencerb951bc02006-12-29 20:29:48 +00002059 if (!setTypeName(*$3, $1) && !$1) {
Reid Spencer5b7e7532006-09-28 19:28:24 +00002060 CHECK_FOR_ERROR
Chris Lattner8c89a0a2004-07-13 08:10:10 +00002061 // If this is a named type that is not a redefinition, add it to the slot
2062 // table.
Reid Spencerb951bc02006-12-29 20:29:48 +00002063 CurModule.Types.push_back(*$3);
Chris Lattner30c89792001-09-07 16:35:17 +00002064 }
Reid Spencer08de34b2006-12-03 05:45:44 +00002065
Reid Spencerb951bc02006-12-29 20:29:48 +00002066 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002067 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00002068 }
Reid Spencerb2d17862007-01-26 08:04:51 +00002069 | OptLocalAssign TYPE VOID {
Reid Spencere1553cc2006-12-31 05:40:12 +00002070 ResolveTypeTo($1, $3);
2071
2072 if (!setTypeName($3, $1) && !$1) {
2073 CHECK_FOR_ERROR
2074 // If this is a named type that is not a redefinition, add it to the slot
2075 // table.
2076 CurModule.Types.push_back($3);
2077 }
2078 CHECK_FOR_ERROR
2079 }
Christopher Lambd49e18d2007-12-12 08:44:39 +00002080 | OptGlobalAssign GVVisibilityStyle ThreadLocal GlobalType ConstVal
2081 OptAddrSpace {
Reid Spencerb2d17862007-01-26 08:04:51 +00002082 /* "Externally Visible" Linkage */
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002083 if ($5 == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002084 GEN_ERROR("Global value initializer is not a constant");
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002085 CurGV = ParseGlobalVariable($1, GlobalValue::ExternalLinkage,
Christopher Lambd49e18d2007-12-12 08:44:39 +00002086 $2, $4, $5->getType(), $5, $3, $6);
Christopher Lambfe63fb92007-12-11 08:59:05 +00002087 CHECK_FOR_ERROR
2088 } GlobalVarAttributes {
2089 CurGV = 0;
2090 }
Chris Lattner4989b842007-04-26 05:30:35 +00002091 | OptGlobalAssign GVInternalLinkage GVVisibilityStyle ThreadLocal GlobalType
Christopher Lambd49e18d2007-12-12 08:44:39 +00002092 ConstVal OptAddrSpace {
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002093 if ($6 == 0)
2094 GEN_ERROR("Global value initializer is not a constant");
Christopher Lambd49e18d2007-12-12 08:44:39 +00002095 CurGV = ParseGlobalVariable($1, $2, $3, $5, $6->getType(), $6, $4, $7);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002096 CHECK_FOR_ERROR
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002097 } GlobalVarAttributes {
2098 CurGV = 0;
2099 }
Chris Lattner4989b842007-04-26 05:30:35 +00002100 | OptGlobalAssign GVExternalLinkage GVVisibilityStyle ThreadLocal GlobalType
Christopher Lambd49e18d2007-12-12 08:44:39 +00002101 Types OptAddrSpace {
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002102 if (!UpRefs.empty())
2103 GEN_ERROR("Invalid upreference in type: " + (*$6)->getDescription());
Christopher Lambd49e18d2007-12-12 08:44:39 +00002104 CurGV = ParseGlobalVariable($1, $2, $3, $5, *$6, 0, $4, $7);
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002105 CHECK_FOR_ERROR
2106 delete $6;
Reid Spencer5b7e7532006-09-28 19:28:24 +00002107 } GlobalVarAttributes {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002108 CurGV = 0;
2109 CHECK_FOR_ERROR
2110 }
Anton Korobeynikova80e1182007-04-28 13:45:00 +00002111 | OptGlobalAssign GVVisibilityStyle ALIAS AliasLinkage AliaseeRef {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002112 std::string Name;
2113 if ($1) {
2114 Name = *$1;
2115 delete $1;
2116 }
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00002117 if (Name.empty())
Anton Korobeynikova80e1182007-04-28 13:45:00 +00002118 GEN_ERROR("Alias name cannot be empty");
2119
2120 Constant* Aliasee = $5;
2121 if (Aliasee == 0)
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002122 GEN_ERROR(std::string("Invalid aliasee for alias: ") + Name);
Anton Korobeynikova80e1182007-04-28 13:45:00 +00002123
2124 GlobalAlias* GA = new GlobalAlias(Aliasee->getType(), $4, Name, Aliasee,
2125 CurModule.CurrentModule);
2126 GA->setVisibility($2);
2127 InsertValue(GA, CurModule.Values);
Chris Lattnere5434242007-09-10 23:23:53 +00002128
2129
2130 // If there was a forward reference of this alias, resolve it now.
2131
2132 ValID ID;
2133 if (!Name.empty())
2134 ID = ValID::createGlobalName(Name);
2135 else
2136 ID = ValID::createGlobalID(CurModule.Values.size()-1);
2137
2138 if (GlobalValue *FWGV =
2139 CurModule.GetForwardRefForGlobal(GA->getType(), ID)) {
2140 // Replace uses of the fwdref with the actual alias.
2141 FWGV->replaceAllUsesWith(GA);
2142 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(FWGV))
2143 GV->eraseFromParent();
2144 else
2145 cast<Function>(FWGV)->eraseFromParent();
2146 }
2147 ID.destroy();
2148
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00002149 CHECK_FOR_ERROR
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00002150 }
Reid Spencerb951bc02006-12-29 20:29:48 +00002151 | TARGET TargetDefinition {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002152 CHECK_FOR_ERROR
2153 }
Reid Spencerb951bc02006-12-29 20:29:48 +00002154 | DEPLIBS '=' LibrariesDefinition {
Reid Spencer61c83e02006-08-18 08:43:06 +00002155 CHECK_FOR_ERROR
Chris Lattnere98dda62001-07-14 06:10:16 +00002156 }
Reid Spencerb951bc02006-12-29 20:29:48 +00002157 ;
Chris Lattner00950542001-06-06 20:29:01 +00002158
2159
Chris Lattneree454772006-01-23 23:05:15 +00002160AsmBlock : STRINGCONSTANT {
Chris Lattner66316012006-01-24 04:14:29 +00002161 const std::string &AsmSoFar = CurModule.CurrentModule->getModuleInlineAsm();
Chris Lattneree454772006-01-23 23:05:15 +00002162 if (AsmSoFar.empty())
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002163 CurModule.CurrentModule->setModuleInlineAsm(*$1);
Chris Lattneree454772006-01-23 23:05:15 +00002164 else
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002165 CurModule.CurrentModule->setModuleInlineAsm(AsmSoFar+"\n"+*$1);
2166 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002167 CHECK_FOR_ERROR
Chris Lattneree454772006-01-23 23:05:15 +00002168};
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00002169
Reid Spencerb2d17862007-01-26 08:04:51 +00002170TargetDefinition : TRIPLE '=' STRINGCONSTANT {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002171 CurModule.CurrentModule->setTargetTriple(*$3);
2172 delete $3;
John Criswell2f6a8b12006-10-24 19:09:48 +00002173 }
Chris Lattner10b27112006-10-22 06:07:41 +00002174 | DATALAYOUT '=' STRINGCONSTANT {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002175 CurModule.CurrentModule->setDataLayout(*$3);
2176 delete $3;
Owen Andersoncf7ff2b2006-10-18 02:21:12 +00002177 };
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00002178
Reid Spencerfeaf10e2004-07-25 21:30:51 +00002179LibrariesDefinition : '[' LibList ']';
Reid Spencer0be13e72004-07-25 17:58:28 +00002180
2181LibList : LibList ',' STRINGCONSTANT {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002182 CurModule.CurrentModule->addLibrary(*$3);
2183 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002184 CHECK_FOR_ERROR
Reid Spencer0be13e72004-07-25 17:58:28 +00002185 }
2186 | STRINGCONSTANT {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002187 CurModule.CurrentModule->addLibrary(*$1);
2188 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002189 CHECK_FOR_ERROR
Reid Spencer0be13e72004-07-25 17:58:28 +00002190 }
2191 | /* empty: end of list */ {
Reid Spencer61c83e02006-08-18 08:43:06 +00002192 CHECK_FOR_ERROR
Reid Spencer0be13e72004-07-25 17:58:28 +00002193 }
2194 ;
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00002195
Chris Lattner00950542001-06-06 20:29:01 +00002196//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00002197// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00002198//===----------------------------------------------------------------------===//
2199
Reid Spencerb2d17862007-01-26 08:04:51 +00002200ArgListH : ArgListH ',' Types OptParamAttrs OptLocalName {
Reid Spencere1553cc2006-12-31 05:40:12 +00002201 if (!UpRefs.empty())
2202 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
2203 if (*$3 == Type::VoidTy)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002204 GEN_ERROR("void typed arguments are invalid");
Reid Spencere1553cc2006-12-31 05:40:12 +00002205 ArgListEntry E; E.Attrs = $4; E.Ty = $3; E.Name = $5;
Chris Lattner69da5cf2002-10-13 20:57:00 +00002206 $$ = $1;
Reid Spencere1553cc2006-12-31 05:40:12 +00002207 $1->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002208 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002209 }
Reid Spencerb2d17862007-01-26 08:04:51 +00002210 | Types OptParamAttrs OptLocalName {
Reid Spencere1553cc2006-12-31 05:40:12 +00002211 if (!UpRefs.empty())
2212 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2213 if (*$1 == Type::VoidTy)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002214 GEN_ERROR("void typed arguments are invalid");
Reid Spencere1553cc2006-12-31 05:40:12 +00002215 ArgListEntry E; E.Attrs = $2; E.Ty = $1; E.Name = $3;
2216 $$ = new ArgListType;
2217 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002218 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002219 };
Chris Lattner00950542001-06-06 20:29:01 +00002220
2221ArgList : ArgListH {
2222 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002223 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002224 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00002225 | ArgListH ',' DOTDOTDOT {
2226 $$ = $1;
Reid Spencere1553cc2006-12-31 05:40:12 +00002227 struct ArgListEntry E;
2228 E.Ty = new PATypeHolder(Type::VoidTy);
2229 E.Name = 0;
Reid Spencer18da0722007-04-11 02:44:20 +00002230 E.Attrs = ParamAttr::None;
Reid Spencere1553cc2006-12-31 05:40:12 +00002231 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002232 CHECK_FOR_ERROR
Chris Lattner69da5cf2002-10-13 20:57:00 +00002233 }
2234 | DOTDOTDOT {
Reid Spencere1553cc2006-12-31 05:40:12 +00002235 $$ = new ArgListType;
2236 struct ArgListEntry E;
2237 E.Ty = new PATypeHolder(Type::VoidTy);
2238 E.Name = 0;
Reid Spencer18da0722007-04-11 02:44:20 +00002239 E.Attrs = ParamAttr::None;
Reid Spencere1553cc2006-12-31 05:40:12 +00002240 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002241 CHECK_FOR_ERROR
Chris Lattner69da5cf2002-10-13 20:57:00 +00002242 }
Chris Lattner00950542001-06-06 20:29:01 +00002243 | /* empty */ {
2244 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00002245 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002246 };
Chris Lattner00950542001-06-06 20:29:01 +00002247
Reid Spencerb2d17862007-01-26 08:04:51 +00002248FunctionHeaderH : OptCallingConv ResultTypes GlobalName '(' ArgList ')'
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00002249 OptFuncAttrs OptSection OptAlign OptGC {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002250 std::string FunctionName(*$3);
2251 delete $3; // Free strdup'd memory!
Chris Lattnerdda71962001-11-26 18:54:16 +00002252
Reid Spencer98b3c5c2007-01-02 21:53:43 +00002253 // Check the function result for abstractness if this is a define. We should
2254 // have no abstract types at this point
Reid Spencer2c261782007-01-05 17:06:19 +00002255 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved($2))
2256 GEN_ERROR("Reference to abstract result: "+ $2->get()->getDescription());
Reid Spencer98b3c5c2007-01-02 21:53:43 +00002257
Chris Lattner9232b992003-04-22 18:42:41 +00002258 std::vector<const Type*> ParamTypeList;
Reid Spencer4f859aa2007-04-22 05:46:44 +00002259 ParamAttrsVector Attrs;
2260 if ($7 != ParamAttr::None) {
Duncan Sandsdc024672007-11-27 13:23:08 +00002261 ParamAttrsWithIndex PAWI;
2262 PAWI.index = 0;
2263 PAWI.attrs = $7;
Reid Spencer4f859aa2007-04-22 05:46:44 +00002264 Attrs.push_back(PAWI);
2265 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00002266 if ($5) { // If there are arguments...
Reid Spencer5694b6e2007-04-09 06:17:21 +00002267 unsigned index = 1;
2268 for (ArgListType::iterator I = $5->begin(); I != $5->end(); ++I, ++index) {
Reid Spencere1553cc2006-12-31 05:40:12 +00002269 const Type* Ty = I->Ty->get();
Reid Spencer98b3c5c2007-01-02 21:53:43 +00002270 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved(I->Ty))
2271 GEN_ERROR("Reference to abstract argument: " + Ty->getDescription());
Reid Spencere1553cc2006-12-31 05:40:12 +00002272 ParamTypeList.push_back(Ty);
2273 if (Ty != Type::VoidTy)
Reid Spencer4f859aa2007-04-22 05:46:44 +00002274 if (I->Attrs != ParamAttr::None) {
Duncan Sandsdc024672007-11-27 13:23:08 +00002275 ParamAttrsWithIndex PAWI;
2276 PAWI.index = index;
2277 PAWI.attrs = I->Attrs;
Reid Spencer4f859aa2007-04-22 05:46:44 +00002278 Attrs.push_back(PAWI);
2279 }
Reid Spencere1553cc2006-12-31 05:40:12 +00002280 }
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002281 }
Chris Lattner00950542001-06-06 20:29:01 +00002282
Chris Lattner2079fde2001-10-13 06:41:08 +00002283 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
2284 if (isVarArg) ParamTypeList.pop_back();
2285
Duncan Sands757d2432007-11-30 18:19:18 +00002286 const ParamAttrsList *PAL = 0;
Reid Spencer4f859aa2007-04-22 05:46:44 +00002287 if (!Attrs.empty())
2288 PAL = ParamAttrsList::get(Attrs);
Reid Spencer5694b6e2007-04-09 06:17:21 +00002289
Duncan Sandsdc024672007-11-27 13:23:08 +00002290 FunctionType *FT = FunctionType::get(*$2, ParamTypeList, isVarArg);
Christopher Lamb43ad6b32007-12-17 01:12:55 +00002291 const PointerType *PFT = PointerType::getUnqual(FT);
Reid Spencer2c261782007-01-05 17:06:19 +00002292 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002293
Chris Lattnera09000d2004-07-14 23:03:46 +00002294 ValID ID;
2295 if (!FunctionName.empty()) {
Reid Spencerb2d17862007-01-26 08:04:51 +00002296 ID = ValID::createGlobalName((char*)FunctionName.c_str());
Chris Lattnera09000d2004-07-14 23:03:46 +00002297 } else {
Reid Spencer186a43f2007-03-19 18:39:36 +00002298 ID = ValID::createGlobalID(CurModule.Values.size());
Chris Lattnera09000d2004-07-14 23:03:46 +00002299 }
2300
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00002301 Function *Fn = 0;
Chris Lattnera09000d2004-07-14 23:03:46 +00002302 // See if this function was forward referenced. If so, recycle the object.
Reid Spencer863dd882007-04-28 16:06:50 +00002303 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
Chris Lattnera09000d2004-07-14 23:03:46 +00002304 // Move the function to the end of the list, from whereever it was
2305 // previously inserted.
2306 Fn = cast<Function>(FWRef);
Duncan Sandsdc024672007-11-27 13:23:08 +00002307 assert(!Fn->getParamAttrs() && "Forward reference has parameter attributes!");
Chris Lattnera09000d2004-07-14 23:03:46 +00002308 CurModule.CurrentModule->getFunctionList().remove(Fn);
2309 CurModule.CurrentModule->getFunctionList().push_back(Fn);
2310 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
Reid Spenceref9b9a72007-02-05 20:47:22 +00002311 (Fn = CurModule.CurrentModule->getFunction(FunctionName))) {
Duncan Sandsdc024672007-11-27 13:23:08 +00002312 if (Fn->getFunctionType() != FT ) {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002313 // The existing function doesn't have the same type. This is an overload
2314 // error.
2315 GEN_ERROR("Overload of function '" + FunctionName + "' not permitted.");
Duncan Sandsdc024672007-11-27 13:23:08 +00002316 } else if (Fn->getParamAttrs() != PAL) {
2317 // The existing function doesn't have the same parameter attributes.
2318 // This is an overload error.
2319 GEN_ERROR("Overload of function '" + FunctionName + "' not permitted.");
Reid Spenceref9b9a72007-02-05 20:47:22 +00002320 } else if (!CurFun.isDeclare && !Fn->isDeclaration()) {
2321 // Neither the existing or the current function is a declaration and they
2322 // have the same name and same type. Clearly this is a redefinition.
Reid Spencerf4fa5902007-02-05 10:16:10 +00002323 GEN_ERROR("Redefinition of function '" + FunctionName + "'");
Duncan Sandsdc024672007-11-27 13:23:08 +00002324 } else if (Fn->isDeclaration()) {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002325 // Make sure to strip off any argument names so we can't get conflicts.
Chris Lattnere4d5c442005-03-15 04:54:21 +00002326 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
Chris Lattnera09000d2004-07-14 23:03:46 +00002327 AI != AE; ++AI)
2328 AI->setName("");
Reid Spenceref9b9a72007-02-05 20:47:22 +00002329 }
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00002330 } else { // Not already defined?
Chris Lattner4989b842007-04-26 05:30:35 +00002331 Fn = new Function(FT, GlobalValue::ExternalWeakLinkage, FunctionName,
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00002332 CurModule.CurrentModule);
2333 InsertValue(Fn, CurModule.Values);
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00002334 }
Chris Lattner00950542001-06-06 20:29:01 +00002335
Chris Lattner394f2eb2003-10-16 20:12:13 +00002336 CurFun.FunctionStart(Fn);
Anton Korobeynikov93c2b372006-09-17 13:06:18 +00002337
2338 if (CurFun.isDeclare) {
2339 // If we have declaration, always overwrite linkage. This will allow us to
2340 // correctly handle cases, when pointer to function is passed as argument to
2341 // another function.
2342 Fn->setLinkage(CurFun.Linkage);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002343 Fn->setVisibility(CurFun.Visibility);
Anton Korobeynikov93c2b372006-09-17 13:06:18 +00002344 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00002345 Fn->setCallingConv($1);
Duncan Sandsdc024672007-11-27 13:23:08 +00002346 Fn->setParamAttrs(PAL);
Reid Spencer2c261782007-01-05 17:06:19 +00002347 Fn->setAlignment($9);
2348 if ($8) {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002349 Fn->setSection(*$8);
2350 delete $8;
Chris Lattner164c3782005-11-12 00:11:10 +00002351 }
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00002352 if ($10) {
2353 Fn->setCollector($10->c_str());
2354 delete $10;
2355 }
Chris Lattner00950542001-06-06 20:29:01 +00002356
Chris Lattner7e708292002-06-25 16:13:24 +00002357 // Add all of the arguments we parsed to the function...
Chris Lattnera8e8f162005-05-06 20:27:19 +00002358 if ($5) { // Is null if empty...
Chris Lattner69da5cf2002-10-13 20:57:00 +00002359 if (isVarArg) { // Nuke the last entry
Reid Spenceref9b9a72007-02-05 20:47:22 +00002360 assert($5->back().Ty->get() == Type::VoidTy && $5->back().Name == 0 &&
Reid Spencer1755a9a2007-02-05 17:01:20 +00002361 "Not a varargs marker!");
Reid Spencere1553cc2006-12-31 05:40:12 +00002362 delete $5->back().Ty;
Chris Lattnera8e8f162005-05-06 20:27:19 +00002363 $5->pop_back(); // Delete the last entry
Chris Lattner69da5cf2002-10-13 20:57:00 +00002364 }
Chris Lattnere4d5c442005-03-15 04:54:21 +00002365 Function::arg_iterator ArgIt = Fn->arg_begin();
Reid Spenceref9b9a72007-02-05 20:47:22 +00002366 Function::arg_iterator ArgEnd = Fn->arg_end();
Reid Spencere1553cc2006-12-31 05:40:12 +00002367 unsigned Idx = 1;
Reid Spenceref9b9a72007-02-05 20:47:22 +00002368 for (ArgListType::iterator I = $5->begin();
2369 I != $5->end() && ArgIt != ArgEnd; ++I, ++ArgIt) {
Reid Spencere1553cc2006-12-31 05:40:12 +00002370 delete I->Ty; // Delete the typeholder...
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002371 setValueName(ArgIt, I->Name); // Insert arg into symtab...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002372 CHECK_FOR_ERROR
Chris Lattner69da5cf2002-10-13 20:57:00 +00002373 InsertValue(ArgIt);
Reid Spencere1553cc2006-12-31 05:40:12 +00002374 Idx++;
Chris Lattner00950542001-06-06 20:29:01 +00002375 }
Reid Spencer08de34b2006-12-03 05:45:44 +00002376
Chris Lattnera8e8f162005-05-06 20:27:19 +00002377 delete $5; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00002378 }
Reid Spencer61c83e02006-08-18 08:43:06 +00002379 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002380};
Chris Lattner00950542001-06-06 20:29:01 +00002381
Chris Lattner9b02cc32002-05-03 18:23:48 +00002382BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
2383
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002384FunctionHeader : FunctionDefineLinkage GVVisibilityStyle FunctionHeaderH BEGIN {
Chris Lattner394f2eb2003-10-16 20:12:13 +00002385 $$ = CurFun.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00002386
Chris Lattner4ad02e72003-04-16 20:28:45 +00002387 // Make sure that we keep track of the linkage type even if there was a
2388 // previous "declare".
2389 $$->setLinkage($1);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002390 $$->setVisibility($2);
Chris Lattner51727be2002-06-04 21:58:56 +00002391};
Chris Lattner00950542001-06-06 20:29:01 +00002392
Chris Lattner9b02cc32002-05-03 18:23:48 +00002393END : ENDTOK | '}'; // Allow end of '}' to end a function
2394
Chris Lattner79df7c02002-03-26 18:01:55 +00002395Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00002396 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002397 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002398};
Chris Lattner00950542001-06-06 20:29:01 +00002399
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002400FunctionProto : FunctionDeclareLinkage GVVisibilityStyle FunctionHeaderH {
Reid Spencere1553cc2006-12-31 05:40:12 +00002401 CurFun.CurrentFunction->setLinkage($1);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002402 CurFun.CurrentFunction->setVisibility($2);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002403 $$ = CurFun.CurrentFunction;
2404 CurFun.FunctionDone();
2405 CHECK_FOR_ERROR
2406 };
Chris Lattner00950542001-06-06 20:29:01 +00002407
2408//===----------------------------------------------------------------------===//
2409// Rules to match Basic Blocks
2410//===----------------------------------------------------------------------===//
2411
Chris Lattneraa2c8532006-01-25 22:26:43 +00002412OptSideEffect : /* empty */ {
2413 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002414 CHECK_FOR_ERROR
Chris Lattneraa2c8532006-01-25 22:26:43 +00002415 }
2416 | SIDEEFFECT {
2417 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002418 CHECK_FOR_ERROR
Chris Lattneraa2c8532006-01-25 22:26:43 +00002419 };
2420
Chris Lattner00950542001-06-06 20:29:01 +00002421ConstValueRef : ESINT64VAL { // A reference to a direct constant
2422 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002423 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002424 }
2425 | EUINT64VAL {
2426 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002427 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002428 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00002429 | FPVAL { // Perhaps it's an FP constant?
2430 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002431 CHECK_FOR_ERROR
Chris Lattner3d52b2f2001-07-15 00:17:01 +00002432 }
Chris Lattner91ef4602004-03-31 03:49:47 +00002433 | TRUETOK {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00002434 $$ = ValID::create(ConstantInt::getTrue());
Reid Spencer61c83e02006-08-18 08:43:06 +00002435 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002436 }
Chris Lattner91ef4602004-03-31 03:49:47 +00002437 | FALSETOK {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00002438 $$ = ValID::create(ConstantInt::getFalse());
Reid Spencer61c83e02006-08-18 08:43:06 +00002439 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002440 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00002441 | NULL_TOK {
2442 $$ = ValID::createNull();
Reid Spencer61c83e02006-08-18 08:43:06 +00002443 CHECK_FOR_ERROR
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002444 }
Chris Lattner16710e92004-10-16 18:17:13 +00002445 | UNDEF {
2446 $$ = ValID::createUndef();
Reid Spencer61c83e02006-08-18 08:43:06 +00002447 CHECK_FOR_ERROR
Chris Lattner16710e92004-10-16 18:17:13 +00002448 }
Chris Lattnerf1f03df2005-12-21 17:53:02 +00002449 | ZEROINITIALIZER { // A vector zero constant.
2450 $$ = ValID::createZeroInit();
Reid Spencer61c83e02006-08-18 08:43:06 +00002451 CHECK_FOR_ERROR
Chris Lattnerf1f03df2005-12-21 17:53:02 +00002452 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00002453 | '<' ConstVector '>' { // Nonempty unsized packed vector
Reid Spencer08de34b2006-12-03 05:45:44 +00002454 const Type *ETy = (*$2)[0]->getType();
Brian Gaeke715c90b2004-08-20 06:00:58 +00002455 int NumElements = $2->size();
2456
Reid Spencer9d6565a2007-02-15 02:26:10 +00002457 VectorType* pt = VectorType::get(ETy, NumElements);
Brian Gaeke715c90b2004-08-20 06:00:58 +00002458 PATypeHolder* PTy = new PATypeHolder(
Reid Spencer08de34b2006-12-03 05:45:44 +00002459 HandleUpRefs(
Reid Spencer9d6565a2007-02-15 02:26:10 +00002460 VectorType::get(
Reid Spencer08de34b2006-12-03 05:45:44 +00002461 ETy,
2462 NumElements)
2463 )
2464 );
Brian Gaeke715c90b2004-08-20 06:00:58 +00002465
2466 // Verify all elements are correct type!
2467 for (unsigned i = 0; i < $2->size(); i++) {
Reid Spencer08de34b2006-12-03 05:45:44 +00002468 if (ETy != (*$2)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00002469 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Brian Gaeke715c90b2004-08-20 06:00:58 +00002470 ETy->getDescription() +"' as required!\nIt is of type '" +
Reid Spencer08de34b2006-12-03 05:45:44 +00002471 (*$2)[i]->getType()->getDescription() + "'.");
Brian Gaeke715c90b2004-08-20 06:00:58 +00002472 }
2473
Reid Spencer9d6565a2007-02-15 02:26:10 +00002474 $$ = ValID::create(ConstantVector::get(pt, *$2));
Brian Gaeke715c90b2004-08-20 06:00:58 +00002475 delete PTy; delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002476 CHECK_FOR_ERROR
Brian Gaeke715c90b2004-08-20 06:00:58 +00002477 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00002478 | ConstExpr {
Reid Spencer08de34b2006-12-03 05:45:44 +00002479 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002480 CHECK_FOR_ERROR
Chris Lattneraa2c8532006-01-25 22:26:43 +00002481 }
2482 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002483 $$ = ValID::createInlineAsm(*$3, *$5, $2);
2484 delete $3;
2485 delete $5;
Reid Spencer61c83e02006-08-18 08:43:06 +00002486 CHECK_FOR_ERROR
Chris Lattnerd78700d2002-08-16 21:14:40 +00002487 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00002488
Chris Lattner2079fde2001-10-13 06:41:08 +00002489// SymbolicValueRef - Reference to one of two ways of symbolically refering to
2490// another value.
2491//
Reid Spencerb2d17862007-01-26 08:04:51 +00002492SymbolicValueRef : LOCALVAL_ID { // Is it an integer reference...?
2493 $$ = ValID::createLocalID($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002494 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002495 }
Reid Spencerb2d17862007-01-26 08:04:51 +00002496 | GLOBALVAL_ID {
2497 $$ = ValID::createGlobalID($1);
2498 CHECK_FOR_ERROR
2499 }
2500 | LocalName { // Is it a named reference...?
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002501 $$ = ValID::createLocalName(*$1);
2502 delete $1;
Reid Spencerb2d17862007-01-26 08:04:51 +00002503 CHECK_FOR_ERROR
2504 }
2505 | GlobalName { // Is it a named reference...?
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002506 $$ = ValID::createGlobalName(*$1);
2507 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002508 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002509 };
Chris Lattner2079fde2001-10-13 06:41:08 +00002510
2511// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00002512ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00002513
Chris Lattner00950542001-06-06 20:29:01 +00002514
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002515// ResolvedVal - a <type> <value> pair. This is used only in cases where the
2516// type immediately preceeds the value reference, and allows complex constant
2517// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00002518ResolvedVal : Types ValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00002519 if (!UpRefs.empty())
2520 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2521 $$ = getVal(*$1, $2);
2522 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002523 CHECK_FOR_ERROR
Reid Spencere1553cc2006-12-31 05:40:12 +00002524 }
2525 ;
Chris Lattner8b81bf52001-07-25 22:47:46 +00002526
Devang Patel5af2f632008-02-20 22:39:45 +00002527ReturnedVal : ResolvedVal {
2528 $$ = new std::vector<Value *>();
2529 $$->push_back($1);
2530 CHECK_FOR_ERROR
2531 }
2532 | ReturnedVal ',' ConstVal {
2533 ($$=$1)->push_back($3);
2534 CHECK_FOR_ERROR
2535 };
2536
Chris Lattner00950542001-06-06 20:29:01 +00002537BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner8ab406d2004-07-13 07:59:27 +00002538 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002539 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002540 }
Chris Lattner7e708292002-06-25 16:13:24 +00002541 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
Chris Lattner8ab406d2004-07-13 07:59:27 +00002542 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002543 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002544 };
Chris Lattner00950542001-06-06 20:29:01 +00002545
2546
2547// Basic blocks are terminated by branching instructions:
2548// br, br/cc, switch, ret
2549//
Reid Spencerb2d17862007-01-26 08:04:51 +00002550BasicBlock : InstructionList OptLocalAssign BBTerminatorInst {
Chris Lattner8ab406d2004-07-13 07:59:27 +00002551 setValueName($3, $2);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002552 CHECK_FOR_ERROR
Chris Lattner2079fde2001-10-13 06:41:08 +00002553 InsertValue($3);
Chris Lattner2079fde2001-10-13 06:41:08 +00002554 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00002555 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002556 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002557 };
Chris Lattner00950542001-06-06 20:29:01 +00002558
2559InstructionList : InstructionList Inst {
Reid Spencer3da59db2006-11-27 01:05:10 +00002560 if (CastInst *CI1 = dyn_cast<CastInst>($2))
2561 if (CastInst *CI2 = dyn_cast<CastInst>(CI1->getOperand(0)))
2562 if (CI2->getParent() == 0)
2563 $1->getInstList().push_back(CI2);
Chris Lattner00950542001-06-06 20:29:01 +00002564 $1->getInstList().push_back($2);
2565 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002566 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002567 }
Reid Spencer186a43f2007-03-19 18:39:36 +00002568 | /* empty */ { // Empty space between instruction lists
2569 $$ = defineBBVal(ValID::createLocalID(CurFun.NextValNum));
Reid Spencer61c83e02006-08-18 08:43:06 +00002570 CHECK_FOR_ERROR
Chris Lattner22ae2322004-07-13 08:39:15 +00002571 }
Reid Spencer186a43f2007-03-19 18:39:36 +00002572 | LABELSTR { // Labelled (named) basic block
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002573 $$ = defineBBVal(ValID::createLocalName(*$1));
2574 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002575 CHECK_FOR_ERROR
Reid Spencer6ecdcc12007-05-22 18:52:21 +00002576
Chris Lattner51727be2002-06-04 21:58:56 +00002577 };
Chris Lattner00950542001-06-06 20:29:01 +00002578
Devang Patel5af2f632008-02-20 22:39:45 +00002579BBTerminatorInst :
2580 RET ReturnedVal { // Return with a result...
2581 if($2->size() == 1)
2582 $$ = new ReturnInst($2->back());
2583 else {
2584
2585 std::vector<const Type*> Elements;
2586 std::vector<Constant*> Vals;
2587 for (std::vector<Value *>::iterator I = $2->begin(),
2588 E = $2->end(); I != E; ++I) {
2589 Value *V = *I;
2590 Constant *C = cast<Constant>(V);
2591 Elements.push_back(V->getType());
2592 Vals.push_back(C);
2593 }
2594
2595 const StructType *STy = StructType::get(Elements);
2596 PATypeHolder *PTy =
2597 new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
2598
2599 Constant *CS = ConstantStruct::get(STy, Vals); // *$2);
2600 $$ = new ReturnInst(CS);
2601 delete PTy;
2602 }
2603 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002604 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002605 }
Reid Spencer186a43f2007-03-19 18:39:36 +00002606 | RET VOID { // Return with no result...
Chris Lattner00950542001-06-06 20:29:01 +00002607 $$ = new ReturnInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002608 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002609 }
Reid Spencer186a43f2007-03-19 18:39:36 +00002610 | BR LABEL ValueRef { // Unconditional Branch...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002611 BasicBlock* tmpBB = getBBVal($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002612 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002613 $$ = new BranchInst(tmpBB);
Reid Spencer186a43f2007-03-19 18:39:36 +00002614 } // Conditional Branch...
Reid Spencer8088e9d2007-01-13 05:00:20 +00002615 | BR INTTYPE ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
2616 assert(cast<IntegerType>($2)->getBitWidth() == 1 && "Not Bool?");
Reid Spencer5b7e7532006-09-28 19:28:24 +00002617 BasicBlock* tmpBBA = getBBVal($6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002618 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002619 BasicBlock* tmpBBB = getBBVal($9);
2620 CHECK_FOR_ERROR
Reid Spencer4fe16d62007-01-11 18:21:29 +00002621 Value* tmpVal = getVal(Type::Int1Ty, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002622 CHECK_FOR_ERROR
2623 $$ = new BranchInst(tmpBBA, tmpBBB, tmpVal);
Chris Lattner00950542001-06-06 20:29:01 +00002624 }
2625 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencer08de34b2006-12-03 05:45:44 +00002626 Value* tmpVal = getVal($2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002627 CHECK_FOR_ERROR
2628 BasicBlock* tmpBB = getBBVal($6);
2629 CHECK_FOR_ERROR
2630 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, $8->size());
Chris Lattner00950542001-06-06 20:29:01 +00002631 $$ = S;
2632
Chris Lattner9232b992003-04-22 18:42:41 +00002633 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
Chris Lattner46748042002-04-09 19:41:42 +00002634 E = $8->end();
Chris Lattner69331f52005-02-24 05:25:17 +00002635 for (; I != E; ++I) {
2636 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
2637 S->addCase(CI, I->second);
2638 else
Reid Spencerf4fa5902007-02-05 10:16:10 +00002639 GEN_ERROR("Switch case is constant, but not a simple integer");
Chris Lattner69331f52005-02-24 05:25:17 +00002640 }
Chris Lattnerf57a43d2004-04-17 23:49:15 +00002641 delete $8;
Reid Spencer61c83e02006-08-18 08:43:06 +00002642 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002643 }
Chris Lattner196850c2003-05-15 21:30:00 +00002644 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencer08de34b2006-12-03 05:45:44 +00002645 Value* tmpVal = getVal($2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002646 CHECK_FOR_ERROR
2647 BasicBlock* tmpBB = getBBVal($6);
2648 CHECK_FOR_ERROR
2649 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, 0);
Chris Lattner196850c2003-05-15 21:30:00 +00002650 $$ = S;
Reid Spencer61c83e02006-08-18 08:43:06 +00002651 CHECK_FOR_ERROR
Chris Lattner196850c2003-05-15 21:30:00 +00002652 }
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002653 | INVOKE OptCallingConv ResultTypes ValueRef '(' ParamList ')' OptFuncAttrs
Chris Lattnera8e8f162005-05-06 20:27:19 +00002654 TO LABEL ValueRef UNWIND LABEL ValueRef {
Chris Lattner2079fde2001-10-13 06:41:08 +00002655
Reid Spencere1553cc2006-12-31 05:40:12 +00002656 // Handle the short syntax
2657 const PointerType *PFTy = 0;
2658 const FunctionType *Ty = 0;
Reid Spencer2c261782007-01-05 17:06:19 +00002659 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002660 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00002661 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00002662 std::vector<const Type*> ParamTypes;
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002663 ParamList::iterator I = $6->begin(), E = $6->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00002664 for (; I != E; ++I) {
Reid Spencere1553cc2006-12-31 05:40:12 +00002665 const Type *Ty = I->Val->getType();
2666 if (Ty == Type::VoidTy)
2667 GEN_ERROR("Short call syntax cannot be used with varargs");
2668 ParamTypes.push_back(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00002669 }
Duncan Sandsdc024672007-11-27 13:23:08 +00002670 Ty = FunctionType::get($3->get(), ParamTypes, false);
Christopher Lamb43ad6b32007-12-17 01:12:55 +00002671 PFTy = PointerType::getUnqual(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00002672 }
Chris Lattner2079fde2001-10-13 06:41:08 +00002673
Reid Spencer2c9df212007-03-20 01:13:00 +00002674 delete $3;
2675
Chris Lattnera8e8f162005-05-06 20:27:19 +00002676 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002677 CHECK_FOR_ERROR
Reid Spencer2c261782007-01-05 17:06:19 +00002678 BasicBlock *Normal = getBBVal($11);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002679 CHECK_FOR_ERROR
Reid Spencer2c261782007-01-05 17:06:19 +00002680 BasicBlock *Except = getBBVal($14);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002681 CHECK_FOR_ERROR
Chris Lattner2079fde2001-10-13 06:41:08 +00002682
Duncan Sandsdc024672007-11-27 13:23:08 +00002683 ParamAttrsVector Attrs;
2684 if ($8 != ParamAttr::None) {
2685 ParamAttrsWithIndex PAWI; PAWI.index = 0; PAWI.attrs = $8;
2686 Attrs.push_back(PAWI);
2687 }
2688
Reid Spencere1553cc2006-12-31 05:40:12 +00002689 // Check the arguments
2690 ValueList Args;
2691 if ($6->empty()) { // Has no arguments?
2692 // Make sure no arguments is a good thing!
2693 if (Ty->getNumParams() != 0)
2694 GEN_ERROR("No arguments passed to a function that "
Reid Spencerf4fa5902007-02-05 10:16:10 +00002695 "expects arguments");
Chris Lattner2079fde2001-10-13 06:41:08 +00002696 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00002697 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00002698 // correctly!
Chris Lattnerd5d89962004-02-09 04:14:01 +00002699 FunctionType::param_iterator I = Ty->param_begin();
2700 FunctionType::param_iterator E = Ty->param_end();
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002701 ParamList::iterator ArgI = $6->begin(), ArgE = $6->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00002702 unsigned index = 1;
Reid Spencer863dd882007-04-28 16:06:50 +00002703
Duncan Sandsdc024672007-11-27 13:23:08 +00002704 for (; ArgI != ArgE && I != E; ++ArgI, ++I, ++index) {
Reid Spencere1553cc2006-12-31 05:40:12 +00002705 if (ArgI->Val->getType() != *I)
2706 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00002707 (*I)->getDescription() + "'");
Reid Spencere1553cc2006-12-31 05:40:12 +00002708 Args.push_back(ArgI->Val);
Duncan Sandsdc024672007-11-27 13:23:08 +00002709 if (ArgI->Attrs != ParamAttr::None) {
2710 ParamAttrsWithIndex PAWI;
2711 PAWI.index = index;
2712 PAWI.attrs = ArgI->Attrs;
2713 Attrs.push_back(PAWI);
2714 }
Reid Spencere1553cc2006-12-31 05:40:12 +00002715 }
Reid Spencer08de34b2006-12-03 05:45:44 +00002716
Reid Spencere1553cc2006-12-31 05:40:12 +00002717 if (Ty->isVarArg()) {
2718 if (I == E)
Duncan Sandseb824702008-01-11 21:23:39 +00002719 for (; ArgI != ArgE; ++ArgI, ++index) {
Reid Spencere1553cc2006-12-31 05:40:12 +00002720 Args.push_back(ArgI->Val); // push the remaining varargs
Duncan Sandseb824702008-01-11 21:23:39 +00002721 if (ArgI->Attrs != ParamAttr::None) {
2722 ParamAttrsWithIndex PAWI;
2723 PAWI.index = index;
2724 PAWI.attrs = ArgI->Attrs;
2725 Attrs.push_back(PAWI);
2726 }
2727 }
Reid Spencere1553cc2006-12-31 05:40:12 +00002728 } else if (I != E || ArgI != ArgE)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002729 GEN_ERROR("Invalid number of parameters detected");
Chris Lattner2079fde2001-10-13 06:41:08 +00002730 }
Reid Spencere1553cc2006-12-31 05:40:12 +00002731
Duncan Sands757d2432007-11-30 18:19:18 +00002732 const ParamAttrsList *PAL = 0;
Duncan Sandsdc024672007-11-27 13:23:08 +00002733 if (!Attrs.empty())
2734 PAL = ParamAttrsList::get(Attrs);
2735
Reid Spencere1553cc2006-12-31 05:40:12 +00002736 // Create the InvokeInst
David Greenef1355a52007-08-27 19:04:21 +00002737 InvokeInst *II = new InvokeInst(V, Normal, Except, Args.begin(), Args.end());
Reid Spencere1553cc2006-12-31 05:40:12 +00002738 II->setCallingConv($2);
Duncan Sandsdc024672007-11-27 13:23:08 +00002739 II->setParamAttrs(PAL);
Reid Spencere1553cc2006-12-31 05:40:12 +00002740 $$ = II;
Chris Lattnera8e8f162005-05-06 20:27:19 +00002741 delete $6;
Reid Spencer61c83e02006-08-18 08:43:06 +00002742 CHECK_FOR_ERROR
Chris Lattner36143fc2003-09-08 18:54:55 +00002743 }
2744 | UNWIND {
2745 $$ = new UnwindInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002746 CHECK_FOR_ERROR
Chris Lattner16710e92004-10-16 18:17:13 +00002747 }
2748 | UNREACHABLE {
2749 $$ = new UnreachableInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002750 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002751 };
Chris Lattner2079fde2001-10-13 06:41:08 +00002752
2753
Chris Lattner00950542001-06-06 20:29:01 +00002754
2755JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
2756 $$ = $1;
Reid Spencer186a43f2007-03-19 18:39:36 +00002757 Constant *V = cast<Constant>(getExistingVal($2, $3));
Reid Spencer5b7e7532006-09-28 19:28:24 +00002758 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002759 if (V == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002760 GEN_ERROR("May only switch on a constant pool value");
Chris Lattner00950542001-06-06 20:29:01 +00002761
Reid Spencer5b7e7532006-09-28 19:28:24 +00002762 BasicBlock* tmpBB = getBBVal($6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002763 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002764 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner00950542001-06-06 20:29:01 +00002765 }
2766 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner9232b992003-04-22 18:42:41 +00002767 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Reid Spencer186a43f2007-03-19 18:39:36 +00002768 Constant *V = cast<Constant>(getExistingVal($1, $2));
Reid Spencer5b7e7532006-09-28 19:28:24 +00002769 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002770
2771 if (V == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002772 GEN_ERROR("May only switch on a constant pool value");
Chris Lattner00950542001-06-06 20:29:01 +00002773
Reid Spencer5b7e7532006-09-28 19:28:24 +00002774 BasicBlock* tmpBB = getBBVal($5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002775 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002776 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner51727be2002-06-04 21:58:56 +00002777 };
Chris Lattner00950542001-06-06 20:29:01 +00002778
Reid Spencerb2d17862007-01-26 08:04:51 +00002779Inst : OptLocalAssign InstVal {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002780 // Is this definition named?? if so, assign the name...
2781 setValueName($2, $1);
2782 CHECK_FOR_ERROR
2783 InsertValue($2);
2784 $$ = $2;
2785 CHECK_FOR_ERROR
2786 };
2787
Chris Lattner00950542001-06-06 20:29:01 +00002788
Chris Lattnerc24d2082001-06-11 15:04:20 +00002789PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Reid Spencere1553cc2006-12-31 05:40:12 +00002790 if (!UpRefs.empty())
2791 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Chris Lattner9232b992003-04-22 18:42:41 +00002792 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Reid Spencer08de34b2006-12-03 05:45:44 +00002793 Value* tmpVal = getVal(*$1, $3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002794 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002795 BasicBlock* tmpBB = getBBVal($5);
2796 CHECK_FOR_ERROR
2797 $$->push_back(std::make_pair(tmpVal, tmpBB));
Reid Spencer08de34b2006-12-03 05:45:44 +00002798 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00002799 }
2800 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
2801 $$ = $1;
Reid Spencer5b7e7532006-09-28 19:28:24 +00002802 Value* tmpVal = getVal($1->front().first->getType(), $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00002803 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002804 BasicBlock* tmpBB = getBBVal($6);
2805 CHECK_FOR_ERROR
2806 $1->push_back(std::make_pair(tmpVal, tmpBB));
Chris Lattner51727be2002-06-04 21:58:56 +00002807 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00002808
2809
Duncan Sandsdc024672007-11-27 13:23:08 +00002810ParamList : Types OptParamAttrs ValueRef OptParamAttrs {
2811 // FIXME: Remove trailing OptParamAttrs in LLVM 3.0, it was a mistake in 2.0
Reid Spencere1553cc2006-12-31 05:40:12 +00002812 if (!UpRefs.empty())
2813 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2814 // Used for call and invoke instructions
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002815 $$ = new ParamList();
Duncan Sandsdc024672007-11-27 13:23:08 +00002816 ParamListEntry E; E.Attrs = $2 | $4; E.Val = getVal($1->get(), $3);
Reid Spencere1553cc2006-12-31 05:40:12 +00002817 $$->push_back(E);
Reid Spencer2c9df212007-03-20 01:13:00 +00002818 delete $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00002819 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002820 }
Duncan Sandsdc024672007-11-27 13:23:08 +00002821 | LABEL OptParamAttrs ValueRef OptParamAttrs {
2822 // FIXME: Remove trailing OptParamAttrs in LLVM 3.0, it was a mistake in 2.0
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002823 // Labels are only valid in ASMs
2824 $$ = new ParamList();
Duncan Sandsdc024672007-11-27 13:23:08 +00002825 ParamListEntry E; E.Attrs = $2 | $4; E.Val = getBBVal($3);
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002826 $$->push_back(E);
Duncan Sandsdc024672007-11-27 13:23:08 +00002827 CHECK_FOR_ERROR
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002828 }
Duncan Sandsdc024672007-11-27 13:23:08 +00002829 | ParamList ',' Types OptParamAttrs ValueRef OptParamAttrs {
2830 // FIXME: Remove trailing OptParamAttrs in LLVM 3.0, it was a mistake in 2.0
Reid Spencere1553cc2006-12-31 05:40:12 +00002831 if (!UpRefs.empty())
2832 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattner00950542001-06-06 20:29:01 +00002833 $$ = $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00002834 ParamListEntry E; E.Attrs = $4 | $6; E.Val = getVal($3->get(), $5);
Reid Spencere1553cc2006-12-31 05:40:12 +00002835 $$->push_back(E);
Reid Spencer2c9df212007-03-20 01:13:00 +00002836 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002837 CHECK_FOR_ERROR
Reid Spencere1553cc2006-12-31 05:40:12 +00002838 }
Duncan Sandsdc024672007-11-27 13:23:08 +00002839 | ParamList ',' LABEL OptParamAttrs ValueRef OptParamAttrs {
2840 // FIXME: Remove trailing OptParamAttrs in LLVM 3.0, it was a mistake in 2.0
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002841 $$ = $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00002842 ParamListEntry E; E.Attrs = $4 | $6; E.Val = getBBVal($5);
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002843 $$->push_back(E);
2844 CHECK_FOR_ERROR
2845 }
2846 | /*empty*/ { $$ = new ParamList(); };
Chris Lattner00950542001-06-06 20:29:01 +00002847
Reid Spencere1553cc2006-12-31 05:40:12 +00002848IndexList // Used for gep instructions and constant expressions
Reid Spencere03969f2006-12-31 21:46:36 +00002849 : /*empty*/ { $$ = new std::vector<Value*>(); }
Reid Spencere1553cc2006-12-31 05:40:12 +00002850 | IndexList ',' ResolvedVal {
2851 $$ = $1;
2852 $$->push_back($3);
2853 CHECK_FOR_ERROR
2854 }
Reid Spencer71305d72006-12-31 21:25:25 +00002855 ;
Chris Lattner00950542001-06-06 20:29:01 +00002856
Chris Lattnerddb6db42005-05-06 05:51:46 +00002857OptTailCall : TAIL CALL {
2858 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002859 CHECK_FOR_ERROR
Chris Lattnerddb6db42005-05-06 05:51:46 +00002860 }
2861 | CALL {
2862 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002863 CHECK_FOR_ERROR
Chris Lattnerddb6db42005-05-06 05:51:46 +00002864 };
2865
Chris Lattner4a6482b2002-09-10 19:57:26 +00002866InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00002867 if (!UpRefs.empty())
2868 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Chris Lattner42a75512007-01-15 02:27:26 +00002869 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint() &&
Reid Spencer9d6565a2007-02-15 02:26:10 +00002870 !isa<VectorType>((*$2).get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002871 GEN_ERROR(
Reid Spencerf4fa5902007-02-05 10:16:10 +00002872 "Arithmetic operator requires integer, FP, or packed operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00002873 Value* val1 = getVal(*$2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002874 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00002875 Value* val2 = getVal(*$2, $5);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002876 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00002877 $$ = BinaryOperator::create($1, val1, val2);
Chris Lattner4a6482b2002-09-10 19:57:26 +00002878 if ($$ == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002879 GEN_ERROR("binary operator returned null");
Reid Spencer08de34b2006-12-03 05:45:44 +00002880 delete $2;
Chris Lattner4a6482b2002-09-10 19:57:26 +00002881 }
2882 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00002883 if (!UpRefs.empty())
2884 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Chris Lattner42a75512007-01-15 02:27:26 +00002885 if (!(*$2)->isInteger()) {
Reid Spencer9d6565a2007-02-15 02:26:10 +00002886 if (Instruction::isShift($1) || !isa<VectorType>($2->get()) ||
2887 !cast<VectorType>($2->get())->getElementType()->isInteger())
Reid Spencerf4fa5902007-02-05 10:16:10 +00002888 GEN_ERROR("Logical operator requires integral operands");
Chris Lattner0a017832005-12-21 18:31:29 +00002889 }
Reid Spencer08de34b2006-12-03 05:45:44 +00002890 Value* tmpVal1 = getVal(*$2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002891 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00002892 Value* tmpVal2 = getVal(*$2, $5);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002893 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00002894 $$ = BinaryOperator::create($1, tmpVal1, tmpVal2);
Chris Lattner4a6482b2002-09-10 19:57:26 +00002895 if ($$ == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002896 GEN_ERROR("binary operator returned null");
Reid Spencer08de34b2006-12-03 05:45:44 +00002897 delete $2;
Chris Lattner4a6482b2002-09-10 19:57:26 +00002898 }
Reid Spencer08de34b2006-12-03 05:45:44 +00002899 | ICMP IPredicates Types ValueRef ',' ValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00002900 if (!UpRefs.empty())
2901 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencer9d6565a2007-02-15 02:26:10 +00002902 if (isa<VectorType>((*$3).get()))
Reid Spencerac9dcb92007-02-15 03:39:18 +00002903 GEN_ERROR("Vector types not supported by icmp instruction");
Reid Spencer08de34b2006-12-03 05:45:44 +00002904 Value* tmpVal1 = getVal(*$3, $4);
2905 CHECK_FOR_ERROR
2906 Value* tmpVal2 = getVal(*$3, $6);
2907 CHECK_FOR_ERROR
2908 $$ = CmpInst::create($1, $2, tmpVal1, tmpVal2);
2909 if ($$ == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002910 GEN_ERROR("icmp operator returned null");
Reid Spencer2c9df212007-03-20 01:13:00 +00002911 delete $3;
Reid Spencer08de34b2006-12-03 05:45:44 +00002912 }
2913 | FCMP FPredicates Types ValueRef ',' ValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00002914 if (!UpRefs.empty())
2915 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencer9d6565a2007-02-15 02:26:10 +00002916 if (isa<VectorType>((*$3).get()))
Reid Spencerac9dcb92007-02-15 03:39:18 +00002917 GEN_ERROR("Vector types not supported by fcmp instruction");
Reid Spencer08de34b2006-12-03 05:45:44 +00002918 Value* tmpVal1 = getVal(*$3, $4);
2919 CHECK_FOR_ERROR
2920 Value* tmpVal2 = getVal(*$3, $6);
2921 CHECK_FOR_ERROR
2922 $$ = CmpInst::create($1, $2, tmpVal1, tmpVal2);
2923 if ($$ == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002924 GEN_ERROR("fcmp operator returned null");
Reid Spencer2c9df212007-03-20 01:13:00 +00002925 delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00002926 }
Reid Spencer3da59db2006-11-27 01:05:10 +00002927 | CastOps ResolvedVal TO Types {
Reid Spencere1553cc2006-12-31 05:40:12 +00002928 if (!UpRefs.empty())
2929 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00002930 Value* Val = $2;
Reid Spencer93947c32007-01-17 02:47:33 +00002931 const Type* DestTy = $4->get();
2932 if (!CastInst::castIsValid($1, Val, DestTy))
2933 GEN_ERROR("invalid cast opcode for cast from '" +
2934 Val->getType()->getDescription() + "' to '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00002935 DestTy->getDescription() + "'");
Reid Spencer93947c32007-01-17 02:47:33 +00002936 $$ = CastInst::create($1, Val, DestTy);
Reid Spencer08de34b2006-12-03 05:45:44 +00002937 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00002938 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00002939 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer4fe16d62007-01-11 18:21:29 +00002940 if ($2->getType() != Type::Int1Ty)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002941 GEN_ERROR("select condition must be boolean");
Reid Spencer08de34b2006-12-03 05:45:44 +00002942 if ($4->getType() != $6->getType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00002943 GEN_ERROR("select value types should match");
Reid Spencer08de34b2006-12-03 05:45:44 +00002944 $$ = new SelectInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002945 CHECK_FOR_ERROR
Chris Lattner76f0ff32004-03-12 05:51:36 +00002946 }
Chris Lattner99e7ab72003-10-18 05:53:13 +00002947 | VAARG ResolvedVal ',' Types {
Reid Spencere1553cc2006-12-31 05:40:12 +00002948 if (!UpRefs.empty())
2949 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00002950 $$ = new VAArgInst($2, *$4);
2951 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00002952 CHECK_FOR_ERROR
Chris Lattner99e7ab72003-10-18 05:53:13 +00002953 }
Robert Bocchino9c62b562006-01-10 19:04:32 +00002954 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencer08de34b2006-12-03 05:45:44 +00002955 if (!ExtractElementInst::isValidOperands($2, $4))
Reid Spencerf4fa5902007-02-05 10:16:10 +00002956 GEN_ERROR("Invalid extractelement operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00002957 $$ = new ExtractElementInst($2, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00002958 CHECK_FOR_ERROR
Robert Bocchino9c62b562006-01-10 19:04:32 +00002959 }
Robert Bocchino2def1b32006-01-17 20:06:25 +00002960 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer08de34b2006-12-03 05:45:44 +00002961 if (!InsertElementInst::isValidOperands($2, $4, $6))
Reid Spencerf4fa5902007-02-05 10:16:10 +00002962 GEN_ERROR("Invalid insertelement operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00002963 $$ = new InsertElementInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002964 CHECK_FOR_ERROR
Robert Bocchino2def1b32006-01-17 20:06:25 +00002965 }
Chris Lattner4c949082006-04-08 01:18:35 +00002966 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer08de34b2006-12-03 05:45:44 +00002967 if (!ShuffleVectorInst::isValidOperands($2, $4, $6))
Reid Spencerf4fa5902007-02-05 10:16:10 +00002968 GEN_ERROR("Invalid shufflevector operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00002969 $$ = new ShuffleVectorInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002970 CHECK_FOR_ERROR
Chris Lattner4c949082006-04-08 01:18:35 +00002971 }
Chris Lattner3b237fc2003-10-19 21:34:28 +00002972 | PHI_TOK PHIList {
Chris Lattnerc24d2082001-06-11 15:04:20 +00002973 const Type *Ty = $2->front().first->getType();
Chris Lattner8ea8f362003-10-30 01:38:18 +00002974 if (!Ty->isFirstClassType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00002975 GEN_ERROR("PHI node operands must be of first class type");
Chris Lattnerc24d2082001-06-11 15:04:20 +00002976 $$ = new PHINode(Ty);
Chris Lattnerce1df9e2005-01-29 00:35:55 +00002977 ((PHINode*)$$)->reserveOperandSpace($2->size());
Chris Lattner00950542001-06-06 20:29:01 +00002978 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00002979 if ($2->front().first->getType() != Ty)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002980 GEN_ERROR("All elements of a PHI node must be of the same type");
Chris Lattnerb00c5822001-10-02 03:41:24 +00002981 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00002982 $2->pop_front();
2983 }
2984 delete $2; // Free the list...
Reid Spencer61c83e02006-08-18 08:43:06 +00002985 CHECK_FOR_ERROR
Chris Lattnerddb6db42005-05-06 05:51:46 +00002986 }
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002987 | OptTailCall OptCallingConv ResultTypes ValueRef '(' ParamList ')'
Reid Spencer2c261782007-01-05 17:06:19 +00002988 OptFuncAttrs {
Reid Spencere1553cc2006-12-31 05:40:12 +00002989
2990 // Handle the short syntax
Bill Wendlingb39a55a2006-11-12 11:10:39 +00002991 const PointerType *PFTy = 0;
2992 const FunctionType *Ty = 0;
Reid Spencer2c261782007-01-05 17:06:19 +00002993 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002994 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00002995 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00002996 std::vector<const Type*> ParamTypes;
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002997 ParamList::iterator I = $6->begin(), E = $6->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00002998 for (; I != E; ++I) {
Reid Spencere1553cc2006-12-31 05:40:12 +00002999 const Type *Ty = I->Val->getType();
3000 if (Ty == Type::VoidTy)
3001 GEN_ERROR("Short call syntax cannot be used with varargs");
3002 ParamTypes.push_back(Ty);
Chris Lattneref9c23f2001-10-03 14:53:21 +00003003 }
Duncan Sandsdc024672007-11-27 13:23:08 +00003004 Ty = FunctionType::get($3->get(), ParamTypes, false);
Christopher Lamb43ad6b32007-12-17 01:12:55 +00003005 PFTy = PointerType::getUnqual(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00003006 }
Chris Lattner00950542001-06-06 20:29:01 +00003007
Chris Lattnera8e8f162005-05-06 20:27:19 +00003008 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer5b7e7532006-09-28 19:28:24 +00003009 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00003010
Reid Spencerebff55c2007-04-16 06:55:42 +00003011 // Check for call to invalid intrinsic to avoid crashing later.
3012 if (Function *theF = dyn_cast<Function>(V)) {
Reid Spencerce1e8ef2007-04-16 22:01:57 +00003013 if (theF->hasName() && (theF->getValueName()->getKeyLength() >= 5) &&
Reid Spencer43e60732007-04-16 20:31:06 +00003014 (0 == strncmp(theF->getValueName()->getKeyData(), "llvm.", 5)) &&
3015 !theF->getIntrinsicID(true))
Reid Spencerebff55c2007-04-16 06:55:42 +00003016 GEN_ERROR("Call to invalid LLVM intrinsic function '" +
3017 theF->getName() + "'");
3018 }
3019
Duncan Sandsdc024672007-11-27 13:23:08 +00003020 // Set up the ParamAttrs for the function
3021 ParamAttrsVector Attrs;
3022 if ($8 != ParamAttr::None) {
3023 ParamAttrsWithIndex PAWI;
3024 PAWI.index = 0;
3025 PAWI.attrs = $8;
3026 Attrs.push_back(PAWI);
3027 }
Reid Spencere1553cc2006-12-31 05:40:12 +00003028 // Check the arguments
3029 ValueList Args;
3030 if ($6->empty()) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00003031 // Make sure no arguments is a good thing!
3032 if (Ty->getNumParams() != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00003033 GEN_ERROR("No arguments passed to a function that "
Reid Spencerf4fa5902007-02-05 10:16:10 +00003034 "expects arguments");
Chris Lattner8b81bf52001-07-25 22:47:46 +00003035 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00003036 // Loop through FunctionType's arguments and ensure they are specified
Duncan Sandsdc024672007-11-27 13:23:08 +00003037 // correctly. Also, gather any parameter attributes.
Chris Lattnerd5d89962004-02-09 04:14:01 +00003038 FunctionType::param_iterator I = Ty->param_begin();
3039 FunctionType::param_iterator E = Ty->param_end();
Dale Johanneseneb57ea72007-11-05 21:20:28 +00003040 ParamList::iterator ArgI = $6->begin(), ArgE = $6->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00003041 unsigned index = 1;
Reid Spencer863dd882007-04-28 16:06:50 +00003042
Duncan Sandsdc024672007-11-27 13:23:08 +00003043 for (; ArgI != ArgE && I != E; ++ArgI, ++I, ++index) {
Reid Spencere1553cc2006-12-31 05:40:12 +00003044 if (ArgI->Val->getType() != *I)
3045 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00003046 (*I)->getDescription() + "'");
Reid Spencere1553cc2006-12-31 05:40:12 +00003047 Args.push_back(ArgI->Val);
Duncan Sandsdc024672007-11-27 13:23:08 +00003048 if (ArgI->Attrs != ParamAttr::None) {
3049 ParamAttrsWithIndex PAWI;
3050 PAWI.index = index;
3051 PAWI.attrs = ArgI->Attrs;
3052 Attrs.push_back(PAWI);
3053 }
Reid Spencere1553cc2006-12-31 05:40:12 +00003054 }
3055 if (Ty->isVarArg()) {
3056 if (I == E)
Duncan Sandseb824702008-01-11 21:23:39 +00003057 for (; ArgI != ArgE; ++ArgI, ++index) {
Reid Spencere1553cc2006-12-31 05:40:12 +00003058 Args.push_back(ArgI->Val); // push the remaining varargs
Duncan Sandseb824702008-01-11 21:23:39 +00003059 if (ArgI->Attrs != ParamAttr::None) {
3060 ParamAttrsWithIndex PAWI;
3061 PAWI.index = index;
3062 PAWI.attrs = ArgI->Attrs;
3063 Attrs.push_back(PAWI);
3064 }
3065 }
Reid Spencere1553cc2006-12-31 05:40:12 +00003066 } else if (I != E || ArgI != ArgE)
Reid Spencerf4fa5902007-02-05 10:16:10 +00003067 GEN_ERROR("Invalid number of parameters detected");
Chris Lattner8b81bf52001-07-25 22:47:46 +00003068 }
Duncan Sandsdc024672007-11-27 13:23:08 +00003069
3070 // Finish off the ParamAttrs and check them
Duncan Sands757d2432007-11-30 18:19:18 +00003071 const ParamAttrsList *PAL = 0;
Duncan Sandsdc024672007-11-27 13:23:08 +00003072 if (!Attrs.empty())
3073 PAL = ParamAttrsList::get(Attrs);
3074
Reid Spencere1553cc2006-12-31 05:40:12 +00003075 // Create the call node
David Greene52eec542007-08-01 03:43:44 +00003076 CallInst *CI = new CallInst(V, Args.begin(), Args.end());
Reid Spencere1553cc2006-12-31 05:40:12 +00003077 CI->setTailCall($1);
3078 CI->setCallingConv($2);
Duncan Sandsdc024672007-11-27 13:23:08 +00003079 CI->setParamAttrs(PAL);
Reid Spencere1553cc2006-12-31 05:40:12 +00003080 $$ = CI;
Chris Lattnera8e8f162005-05-06 20:27:19 +00003081 delete $6;
Reid Spencerb2d17862007-01-26 08:04:51 +00003082 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00003083 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00003084 }
3085 | MemoryInst {
3086 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00003087 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00003088 };
Chris Lattner00950542001-06-06 20:29:01 +00003089
Chris Lattner15c9c032003-09-08 18:20:29 +00003090OptVolatile : VOLATILE {
3091 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00003092 CHECK_FOR_ERROR
Chris Lattner15c9c032003-09-08 18:20:29 +00003093 }
3094 | /* empty */ {
3095 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00003096 CHECK_FOR_ERROR
Chris Lattner15c9c032003-09-08 18:20:29 +00003097 };
3098
Chris Lattner027dcc52001-07-08 21:10:27 +00003099
Chris Lattnerddb6db42005-05-06 05:51:46 +00003100
Chris Lattner66db8e42005-11-06 06:34:12 +00003101MemoryInst : MALLOC Types OptCAlign {
Reid Spencere1553cc2006-12-31 05:40:12 +00003102 if (!UpRefs.empty())
3103 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00003104 $$ = new MallocInst(*$2, 0, $3);
3105 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00003106 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00003107 }
Reid Spencera54b7cb2007-01-12 07:05:14 +00003108 | MALLOC Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencere1553cc2006-12-31 05:40:12 +00003109 if (!UpRefs.empty())
3110 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00003111 Value* tmpVal = getVal($4, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00003112 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00003113 $$ = new MallocInst(*$2, tmpVal, $6);
3114 delete $2;
Nate Begeman14b05292005-11-05 09:21:28 +00003115 }
Chris Lattner66db8e42005-11-06 06:34:12 +00003116 | ALLOCA Types OptCAlign {
Reid Spencere1553cc2006-12-31 05:40:12 +00003117 if (!UpRefs.empty())
3118 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00003119 $$ = new AllocaInst(*$2, 0, $3);
3120 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00003121 CHECK_FOR_ERROR
Nate Begeman14b05292005-11-05 09:21:28 +00003122 }
Reid Spencera54b7cb2007-01-12 07:05:14 +00003123 | ALLOCA Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencere1553cc2006-12-31 05:40:12 +00003124 if (!UpRefs.empty())
3125 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00003126 Value* tmpVal = getVal($4, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00003127 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00003128 $$ = new AllocaInst(*$2, tmpVal, $6);
3129 delete $2;
Nate Begeman14b05292005-11-05 09:21:28 +00003130 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00003131 | FREE ResolvedVal {
Reid Spencer08de34b2006-12-03 05:45:44 +00003132 if (!isa<PointerType>($2->getType()))
Reid Spencer61c83e02006-08-18 08:43:06 +00003133 GEN_ERROR("Trying to free nonpointer type " +
Reid Spencerf4fa5902007-02-05 10:16:10 +00003134 $2->getType()->getDescription() + "");
Reid Spencer08de34b2006-12-03 05:45:44 +00003135 $$ = new FreeInst($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00003136 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00003137 }
3138
Christopher Lamb43c7f372007-04-22 19:24:39 +00003139 | OptVolatile LOAD Types ValueRef OptCAlign {
Reid Spencere1553cc2006-12-31 05:40:12 +00003140 if (!UpRefs.empty())
3141 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00003142 if (!isa<PointerType>($3->get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00003143 GEN_ERROR("Can't load from nonpointer type: " +
Reid Spencer08de34b2006-12-03 05:45:44 +00003144 (*$3)->getDescription());
3145 if (!cast<PointerType>($3->get())->getElementType()->isFirstClassType())
Reid Spencer61c83e02006-08-18 08:43:06 +00003146 GEN_ERROR("Can't load from pointer of non-first-class type: " +
Reid Spencer08de34b2006-12-03 05:45:44 +00003147 (*$3)->getDescription());
3148 Value* tmpVal = getVal(*$3, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00003149 CHECK_FOR_ERROR
Christopher Lamb43c7f372007-04-22 19:24:39 +00003150 $$ = new LoadInst(tmpVal, "", $1, $5);
Reid Spencer08de34b2006-12-03 05:45:44 +00003151 delete $3;
Chris Lattner027dcc52001-07-08 21:10:27 +00003152 }
Christopher Lamb43c7f372007-04-22 19:24:39 +00003153 | OptVolatile STORE ResolvedVal ',' Types ValueRef OptCAlign {
Reid Spencere1553cc2006-12-31 05:40:12 +00003154 if (!UpRefs.empty())
3155 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00003156 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner8c8418d2003-09-01 16:31:28 +00003157 if (!PT)
Reid Spencer61c83e02006-08-18 08:43:06 +00003158 GEN_ERROR("Can't store to a nonpointer type: " +
Reid Spencer08de34b2006-12-03 05:45:44 +00003159 (*$5)->getDescription());
Chris Lattner8c8418d2003-09-01 16:31:28 +00003160 const Type *ElTy = PT->getElementType();
Reid Spencer08de34b2006-12-03 05:45:44 +00003161 if (ElTy != $3->getType())
3162 GEN_ERROR("Can't store '" + $3->getType()->getDescription() +
Reid Spencerf4fa5902007-02-05 10:16:10 +00003163 "' into space of type '" + ElTy->getDescription() + "'");
Chris Lattner0383cc42002-08-21 23:51:21 +00003164
Reid Spencer08de34b2006-12-03 05:45:44 +00003165 Value* tmpVal = getVal(*$5, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00003166 CHECK_FOR_ERROR
Christopher Lamb43c7f372007-04-22 19:24:39 +00003167 $$ = new StoreInst($3, tmpVal, $1, $7);
Reid Spencer08de34b2006-12-03 05:45:44 +00003168 delete $5;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00003169 }
Devang Patel23755d82008-02-20 19:10:47 +00003170| GETRESULT Types LocalName ',' EUINT64VAL {
Devang Pateld6ffcf92008-02-19 22:26:37 +00003171 ValID TmpVID = ValID::createLocalName(*$3);
3172 Value *TmpVal = getVal($2->get(), TmpVID);
3173 if (!GetResultInst::isValidOperands(TmpVal, $5))
3174 GEN_ERROR("Invalid getresult operands");
3175 $$ = new GetResultInst(TmpVal, $5);
3176 CHECK_FOR_ERROR
3177 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00003178 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencere1553cc2006-12-31 05:40:12 +00003179 if (!UpRefs.empty())
3180 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00003181 if (!isa<PointerType>($2->get()))
Reid Spencerf4fa5902007-02-05 10:16:10 +00003182 GEN_ERROR("getelementptr insn requires pointer operand");
Chris Lattner830b6f92004-04-05 01:30:04 +00003183
David Greeneb8f74792007-09-04 15:46:09 +00003184 if (!GetElementPtrInst::getIndexedType(*$2, $4->begin(), $4->end(), true))
Reid Spencer61c83e02006-08-18 08:43:06 +00003185 GEN_ERROR("Invalid getelementptr indices for type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00003186 (*$2)->getDescription()+ "'");
Reid Spencer08de34b2006-12-03 05:45:44 +00003187 Value* tmpVal = getVal(*$2, $3);
Reid Spencer61c83e02006-08-18 08:43:06 +00003188 CHECK_FOR_ERROR
David Greeneb8f74792007-09-04 15:46:09 +00003189 $$ = new GetElementPtrInst(tmpVal, $4->begin(), $4->end());
Reid Spencer08de34b2006-12-03 05:45:44 +00003190 delete $2;
Reid Spencer5b7e7532006-09-28 19:28:24 +00003191 delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00003192 };
Chris Lattner027dcc52001-07-08 21:10:27 +00003193
Brian Gaeked0fde302003-11-11 22:41:34 +00003194
Chris Lattner00950542001-06-06 20:29:01 +00003195%%
Reid Spencer61c83e02006-08-18 08:43:06 +00003196
Reid Spencere1553cc2006-12-31 05:40:12 +00003197// common code from the two 'RunVMAsmParser' functions
3198static Module* RunParser(Module * M) {
Reid Spencere1553cc2006-12-31 05:40:12 +00003199 CurModule.CurrentModule = M;
Reid Spencere1553cc2006-12-31 05:40:12 +00003200 // Check to make sure the parser succeeded
3201 if (yyparse()) {
3202 if (ParserResult)
3203 delete ParserResult;
3204 return 0;
3205 }
3206
Reid Spencercd5bd902007-03-30 01:37:13 +00003207 // Emit an error if there are any unresolved types left.
3208 if (!CurModule.LateResolveTypes.empty()) {
3209 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
3210 if (DID.Type == ValID::LocalName) {
3211 GenerateError("Undefined type remains at eof: '"+DID.getName() + "'");
3212 } else {
3213 GenerateError("Undefined type remains at eof: #" + itostr(DID.Num));
3214 }
3215 if (ParserResult)
3216 delete ParserResult;
3217 return 0;
3218 }
3219
3220 // Emit an error if there are any unresolved values left.
3221 if (!CurModule.LateResolveValues.empty()) {
3222 Value *V = CurModule.LateResolveValues.back();
3223 std::map<Value*, std::pair<ValID, int> >::iterator I =
3224 CurModule.PlaceHolderInfo.find(V);
3225
3226 if (I != CurModule.PlaceHolderInfo.end()) {
3227 ValID &DID = I->second.first;
3228 if (DID.Type == ValID::LocalName) {
3229 GenerateError("Undefined value remains at eof: "+DID.getName() + "'");
3230 } else {
3231 GenerateError("Undefined value remains at eof: #" + itostr(DID.Num));
3232 }
3233 if (ParserResult)
3234 delete ParserResult;
3235 return 0;
3236 }
3237 }
3238
Reid Spencere1553cc2006-12-31 05:40:12 +00003239 // Check to make sure that parsing produced a result
3240 if (!ParserResult)
3241 return 0;
3242
3243 // Reset ParserResult variable while saving its value for the result.
3244 Module *Result = ParserResult;
3245 ParserResult = 0;
3246
3247 return Result;
3248}
3249
Reid Spencer61c83e02006-08-18 08:43:06 +00003250void llvm::GenerateError(const std::string &message, int LineNo) {
Chris Lattner8e3a8e02007-11-18 08:46:26 +00003251 if (LineNo == -1) LineNo = LLLgetLineNo();
Reid Spencer61c83e02006-08-18 08:43:06 +00003252 // TODO: column number in exception
3253 if (TheParseError)
Chris Lattner8e3a8e02007-11-18 08:46:26 +00003254 TheParseError->setError(LLLgetFilename(), message, LineNo);
Reid Spencer61c83e02006-08-18 08:43:06 +00003255 TriggerError = 1;
3256}
3257
Chris Lattner09083092001-07-08 04:57:15 +00003258int yyerror(const char *ErrorMsg) {
Chris Lattner8e3a8e02007-11-18 08:46:26 +00003259 std::string where = LLLgetFilename() + ":" + utostr(LLLgetLineNo()) + ": ";
Reid Spenceref9b9a72007-02-05 20:47:22 +00003260 std::string errMsg = where + "error: " + std::string(ErrorMsg);
Chris Lattner8e3a8e02007-11-18 08:46:26 +00003261 if (yychar != YYEMPTY && yychar != 0) {
3262 errMsg += " while reading token: '";
3263 errMsg += std::string(LLLgetTokenStart(),
3264 LLLgetTokenStart()+LLLgetTokenLength()) + "'";
3265 }
Reid Spencer61c83e02006-08-18 08:43:06 +00003266 GenerateError(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00003267 return 0;
3268}