blob: 54694d712cb0030eee43d5768a63c13bc1235ed1 [file] [log] [blame]
Chris Lattner58af2a12006-02-15 07:22:58 +00001//===-- llvmAsmParser.y - Parser for llvm assembly files --------*- C++ -*-===//
2//
3// 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.
Chris Lattner58af2a12006-02-15 07:22:58 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the bison parser for LLVM assembly languages files.
11//
12//===----------------------------------------------------------------------===//
13
14%{
15#include "ParserInternals.h"
16#include "llvm/CallingConv.h"
17#include "llvm/InlineAsm.h"
18#include "llvm/Instructions.h"
19#include "llvm/Module.h"
Reid Spenceref9b9a72007-02-05 20:47:22 +000020#include "llvm/ValueSymbolTable.h"
Chandler Carruth02202192007-08-04 01:56:21 +000021#include "llvm/AutoUpgrade.h"
Chris Lattner58af2a12006-02-15 07:22:58 +000022#include "llvm/Support/GetElementPtrTypeIterator.h"
Reid Spencer14310612006-12-31 05:40:51 +000023#include "llvm/Support/CommandLine.h"
Chris Lattnerf7469af2007-01-31 04:44:08 +000024#include "llvm/ADT/SmallVector.h"
Chris Lattner58af2a12006-02-15 07:22:58 +000025#include "llvm/ADT/STLExtras.h"
26#include "llvm/Support/MathExtras.h"
Reid Spencer481169e2006-12-01 00:33:46 +000027#include "llvm/Support/Streams.h"
Chris Lattner58af2a12006-02-15 07:22:58 +000028#include <algorithm>
Chris Lattner58af2a12006-02-15 07:22:58 +000029#include <list>
Chris Lattner8adde282007-02-11 21:40:10 +000030#include <map>
Chris Lattner58af2a12006-02-15 07:22:58 +000031#include <utility>
32
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
Eric Christopher2a5196f2008-09-24 04:55:49 +000036// 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
Reid Spencere4f47592006-08-18 17:32:55 +000039// 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
Eric Christopher2a5196f2008-09-24 04:55:49 +000042// immediately invokes YYERROR. This would be so much cleaner if it was a
Reid Spencere4f47592006-08-18 17:32:55 +000043// 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 Lattner58af2a12006-02-15 07:22:58 +000048int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
49int yylex(); // declaration" of xxx warnings.
50int yyparse();
Chris Lattner58af2a12006-02-15 07:22:58 +000051using namespace llvm;
52
53static Module *ParserResult;
54
55// 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 Lattner58af2a12006-02-15 07:22:58 +000061#else
62#define UR_OUT(X)
63#endif
64
65#define YYERROR_VERBOSE 1
66
Chris Lattner58af2a12006-02-15 07:22:58 +000067static GlobalVariable *CurGV;
68
69
70// This contains info used when building the body of a function. It is
71// destroyed when the function is completed.
72//
73typedef std::vector<Value *> ValueList; // Numbered defs
Reid Spencer14310612006-12-31 05:40:51 +000074
Eric Christopher2a5196f2008-09-24 04:55:49 +000075static void
Reid Spencer93c40032007-03-19 18:40:50 +000076ResolveDefinitions(ValueList &LateResolvers, ValueList *FutureLateResolvers=0);
Chris Lattner58af2a12006-02-15 07:22:58 +000077
78static struct PerModuleInfo {
79 Module *CurrentModule;
Reid Spencer93c40032007-03-19 18:40:50 +000080 ValueList Values; // Module level numbered definitions
81 ValueList LateResolveValues;
Reid Spencer861d9d62006-11-28 07:29:44 +000082 std::vector<PATypeHolder> Types;
83 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner58af2a12006-02-15 07:22:58 +000084
85 /// PlaceHolderInfo - When temporary placeholder objects are created, remember
Chris Lattner0ad19702006-06-21 16:53:00 +000086 /// how they were referenced and on which line of the input they came from so
Chris Lattner58af2a12006-02-15 07:22:58 +000087 /// that we can resolve them later and print error messages as appropriate.
88 std::map<Value*, std::pair<ValID, int> > PlaceHolderInfo;
89
90 // 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
93 // here. This is used for forward references of GlobalValues.
94 //
95 typedef std::map<std::pair<const PointerType *,
96 ValID>, GlobalValue*> GlobalRefsType;
97 GlobalRefsType GlobalRefs;
98
99 void ModuleDone() {
100 // 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.
103 //
104 ResolveDefinitions(LateResolveValues);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000105 if (TriggerError)
106 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000107
108 // Check to make sure that all global value forward references have been
109 // resolved!
110 //
111 if (!GlobalRefs.empty()) {
112 std::string UndefinedReferences = "Unresolved global references exist:\n";
113
114 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 Lattner58af2a12006-02-15 07:22:58 +0000121 }
122
Chandler Carruth02202192007-08-04 01:56:21 +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 Lattner58af2a12006-02-15 07:22:58 +0000128 Values.clear(); // Clear out function local definitions
129 Types.clear();
130 CurrentModule = 0;
131 }
132
133 // 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.
136 GlobalValue *GetForwardRefForGlobal(const PointerType *PTy, ValID ID) {
137 // 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;
143 GlobalRefs.erase(I);
144 }
145 return Ret;
146 }
Reid Spencer8c8a2dc2007-01-02 21:54:12 +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
Eric Christopher2a5196f2008-09-24 04:55:49 +0000154 // we don't need to traverse that leg of the type.
Reid Spencer8c8a2dc2007-01-02 21:54:12 +0000155 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) {
Eric Christopher2a5196f2008-09-24 04:55:49 +0000172 std::vector<const Type*>::iterator I = SeenList.begin(),
Reid Spencer8c8a2dc2007-01-02 21:54:12 +0000173 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) {
Eric Christopher2a5196f2008-09-24 04:55:49 +0000184 std::vector<const Type*>::iterator I = SeenList.begin(),
Reid Spencer8c8a2dc2007-01-02 21:54:12 +0000185 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 Lattner58af2a12006-02-15 07:22:58 +0000197} CurModule;
198
199static struct PerFunctionInfo {
200 Function *CurrentFunction; // Pointer to current function being created
201
Reid Spencer93c40032007-03-19 18:40:50 +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 Lattner58af2a12006-02-15 07:22:58 +0000208
209 /// BBForwardRefs - When we see forward references to basic blocks, keep
210 /// track of them here.
Reid Spencer93c40032007-03-19 18:40:50 +0000211 std::map<ValID, BasicBlock*> BBForwardRefs;
Chris Lattner58af2a12006-02-15 07:22:58 +0000212
213 inline PerFunctionInfo() {
214 CurrentFunction = 0;
215 isDeclare = false;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000216 Linkage = GlobalValue::ExternalLinkage;
217 Visibility = GlobalValue::DefaultVisibility;
Chris Lattner58af2a12006-02-15 07:22:58 +0000218 }
219
220 inline void FunctionStart(Function *M) {
221 CurrentFunction = M;
Reid Spencer93c40032007-03-19 18:40:50 +0000222 NextValNum = 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000223 }
224
225 void FunctionDone() {
Chris Lattner58af2a12006-02-15 07:22:58 +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 Spencer93c40032007-03-19 18:40:50 +0000229 BBForwardRefs.begin()->second->getName());
Reid Spencer5b7e7532006-09-28 19:28:24 +0000230 return;
231 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000232
233 // Resolve all forward references now.
234 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
235
236 Values.clear(); // Clear out function local definitions
Reid Spencer93c40032007-03-19 18:40:50 +0000237 BBForwardRefs.clear();
Chris Lattner58af2a12006-02-15 07:22:58 +0000238 CurrentFunction = 0;
239 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 Lattner58af2a12006-02-15 07:22:58 +0000242 }
243} CurFun; // Info for the current function...
244
245static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
246
247
248//===----------------------------------------------------------------------===//
249// Code to handle definitions of all the types
250//===----------------------------------------------------------------------===//
251
Chris Lattner15bd0952008-08-29 17:20:18 +0000252/// InsertValue - Insert a value into the value table. If it is named, this
253/// returns -1, otherwise it returns the slot number for the value.
254static int InsertValue(Value *V, ValueList &ValueTab = CurFun.Values) {
Reid Spencer93c40032007-03-19 18:40:50 +0000255 // Things that have names or are void typed don't get slot numbers
256 if (V->hasName() || (V->getType() == Type::VoidTy))
Chris Lattner15bd0952008-08-29 17:20:18 +0000257 return -1;
Chris Lattner58af2a12006-02-15 07:22:58 +0000258
Reid Spencer93c40032007-03-19 18:40:50 +0000259 // In the case of function values, we have to allow for the forward reference
260 // of basic blocks, which are included in the numbering. Consequently, we keep
Eric Christopher2a5196f2008-09-24 04:55:49 +0000261 // track of the next insertion location with NextValNum. When a BB gets
Reid Spencer93c40032007-03-19 18:40:50 +0000262 // inserted, it could change the size of the CurFun.Values vector.
263 if (&ValueTab == &CurFun.Values) {
264 if (ValueTab.size() <= CurFun.NextValNum)
265 ValueTab.resize(CurFun.NextValNum+1);
266 ValueTab[CurFun.NextValNum++] = V;
Chris Lattner15bd0952008-08-29 17:20:18 +0000267 return CurFun.NextValNum-1;
Eric Christopher2a5196f2008-09-24 04:55:49 +0000268 }
Reid Spencer93c40032007-03-19 18:40:50 +0000269 // For all other lists, its okay to just tack it on the back of the vector.
270 ValueTab.push_back(V);
Chris Lattner15bd0952008-08-29 17:20:18 +0000271 return ValueTab.size()-1;
Chris Lattner58af2a12006-02-15 07:22:58 +0000272}
273
274static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
275 switch (D.Type) {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000276 case ValID::LocalID: // Is it a numbered definition?
Chris Lattner58af2a12006-02-15 07:22:58 +0000277 // Module constants occupy the lowest numbered slots...
Reid Spencer41dff5e2007-01-26 08:05:27 +0000278 if (D.Num < CurModule.Types.size())
279 return CurModule.Types[D.Num];
Chris Lattner58af2a12006-02-15 07:22:58 +0000280 break;
Reid Spencer41dff5e2007-01-26 08:05:27 +0000281 case ValID::LocalName: // Is it a named definition?
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000282 if (const Type *N = CurModule.CurrentModule->getTypeByName(D.getName())) {
Chris Lattner58af2a12006-02-15 07:22:58 +0000283 D.destroy(); // Free old strdup'd memory...
284 return N;
285 }
286 break;
287 default:
Reid Spencerb5334b02007-02-05 10:18:06 +0000288 GenerateError("Internal parser error: Invalid symbol type reference");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000289 return 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000290 }
291
292 // If we reached here, we referenced either a symbol that we don't know about
293 // or an id number that hasn't been read yet. We may be referencing something
294 // forward, so just create an entry to be resolved later and get to it...
295 //
296 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
297
298
299 if (inFunctionScope()) {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000300 if (D.Type == ValID::LocalName) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000301 GenerateError("Reference to an undefined type: '" + D.getName() + "'");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000302 return 0;
303 } else {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000304 GenerateError("Reference to an undefined type: #" + utostr(D.Num));
Reid Spencer5b7e7532006-09-28 19:28:24 +0000305 return 0;
306 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000307 }
308
Reid Spencer861d9d62006-11-28 07:29:44 +0000309 std::map<ValID, PATypeHolder>::iterator I =CurModule.LateResolveTypes.find(D);
Nuno Lopesf05ff662008-10-15 11:20:21 +0000310 if (I != CurModule.LateResolveTypes.end()) {
311 D.destroy();
Reid Spencer861d9d62006-11-28 07:29:44 +0000312 return I->second;
Nuno Lopesf05ff662008-10-15 11:20:21 +0000313 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000314
Reid Spencer861d9d62006-11-28 07:29:44 +0000315 Type *Typ = OpaqueType::get();
316 CurModule.LateResolveTypes.insert(std::make_pair(D, Typ));
317 return Typ;
Reid Spencera132e042006-12-03 05:46:11 +0000318 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000319
Reid Spencer93c40032007-03-19 18:40:50 +0000320// getExistingVal - Look up the value specified by the provided type and
Chris Lattner58af2a12006-02-15 07:22:58 +0000321// the provided ValID. If the value exists and has already been defined, return
322// it. Otherwise return null.
323//
Reid Spencer93c40032007-03-19 18:40:50 +0000324static Value *getExistingVal(const Type *Ty, const ValID &D) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000325 if (isa<FunctionType>(Ty)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000326 GenerateError("Functions are not values and "
Chris Lattner58af2a12006-02-15 07:22:58 +0000327 "must be referenced as pointers");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000328 return 0;
329 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000330
331 switch (D.Type) {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000332 case ValID::LocalID: { // Is it a numbered definition?
Reid Spencer41dff5e2007-01-26 08:05:27 +0000333 // Check that the number is within bounds.
Eric Christopher2a5196f2008-09-24 04:55:49 +0000334 if (D.Num >= CurFun.Values.size())
Reid Spencer93c40032007-03-19 18:40:50 +0000335 return 0;
336 Value *Result = CurFun.Values[D.Num];
337 if (Ty != Result->getType()) {
338 GenerateError("Numbered value (%" + utostr(D.Num) + ") of type '" +
Eric Christopher2a5196f2008-09-24 04:55:49 +0000339 Result->getType()->getDescription() + "' does not match "
Reid Spencer93c40032007-03-19 18:40:50 +0000340 "expected type, '" + Ty->getDescription() + "'");
341 return 0;
342 }
343 return Result;
Reid Spencer41dff5e2007-01-26 08:05:27 +0000344 }
345 case ValID::GlobalID: { // Is it a numbered definition?
Eric Christopher2a5196f2008-09-24 04:55:49 +0000346 if (D.Num >= CurModule.Values.size())
Reid Spenceref9b9a72007-02-05 20:47:22 +0000347 return 0;
Reid Spencer93c40032007-03-19 18:40:50 +0000348 Value *Result = CurModule.Values[D.Num];
349 if (Ty != Result->getType()) {
350 GenerateError("Numbered value (@" + utostr(D.Num) + ") of type '" +
Eric Christopher2a5196f2008-09-24 04:55:49 +0000351 Result->getType()->getDescription() + "' does not match "
Reid Spencer93c40032007-03-19 18:40:50 +0000352 "expected type, '" + Ty->getDescription() + "'");
Reid Spenceref9b9a72007-02-05 20:47:22 +0000353 return 0;
Reid Spencer93c40032007-03-19 18:40:50 +0000354 }
355 return Result;
Chris Lattner58af2a12006-02-15 07:22:58 +0000356 }
Eric Christopher2a5196f2008-09-24 04:55:49 +0000357
Reid Spencer41dff5e2007-01-26 08:05:27 +0000358 case ValID::LocalName: { // Is it a named definition?
Eric Christopher2a5196f2008-09-24 04:55:49 +0000359 if (!inFunctionScope())
Reid Spenceref9b9a72007-02-05 20:47:22 +0000360 return 0;
361 ValueSymbolTable &SymTab = CurFun.CurrentFunction->getValueSymbolTable();
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000362 Value *N = SymTab.lookup(D.getName());
Eric Christopher2a5196f2008-09-24 04:55:49 +0000363 if (N == 0)
Reid Spenceref9b9a72007-02-05 20:47:22 +0000364 return 0;
365 if (N->getType() != Ty)
366 return 0;
Eric Christopher2a5196f2008-09-24 04:55:49 +0000367
Reid Spencer41dff5e2007-01-26 08:05:27 +0000368 D.destroy(); // Free old strdup'd memory...
369 return N;
370 }
371 case ValID::GlobalName: { // Is it a named definition?
Reid Spenceref9b9a72007-02-05 20:47:22 +0000372 ValueSymbolTable &SymTab = CurModule.CurrentModule->getValueSymbolTable();
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000373 Value *N = SymTab.lookup(D.getName());
Eric Christopher2a5196f2008-09-24 04:55:49 +0000374 if (N == 0)
Reid Spenceref9b9a72007-02-05 20:47:22 +0000375 return 0;
376 if (N->getType() != Ty)
377 return 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000378
379 D.destroy(); // Free old strdup'd memory...
380 return N;
381 }
382
383 // Check to make sure that "Ty" is an integral type, and that our
384 // value will fit into the specified type...
385 case ValID::ConstSIntVal: // Is it a constant pool reference??
Chris Lattner38905612008-02-19 04:36:25 +0000386 if (!isa<IntegerType>(Ty) ||
387 !ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000388 GenerateError("Signed integral constant '" +
Chris Lattner58af2a12006-02-15 07:22:58 +0000389 itostr(D.ConstPool64) + "' is invalid for type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +0000390 Ty->getDescription() + "'");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000391 return 0;
392 }
Reid Spencer49d273e2007-03-19 20:40:51 +0000393 return ConstantInt::get(Ty, D.ConstPool64, true);
Chris Lattner58af2a12006-02-15 07:22:58 +0000394
395 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattner38905612008-02-19 04:36:25 +0000396 if (isa<IntegerType>(Ty) &&
397 ConstantInt::isValueValidForType(Ty, D.UConstPool64))
Reid Spencerb83eb642006-10-20 07:07:24 +0000398 return ConstantInt::get(Ty, D.UConstPool64);
Chris Lattner38905612008-02-19 04:36:25 +0000399
400 if (!isa<IntegerType>(Ty) ||
401 !ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
402 GenerateError("Integral constant '" + utostr(D.UConstPool64) +
403 "' is invalid or out of range for type '" +
404 Ty->getDescription() + "'");
405 return 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000406 }
Chris Lattner38905612008-02-19 04:36:25 +0000407 // This is really a signed reference. Transmogrify.
408 return ConstantInt::get(Ty, D.ConstPool64, true);
Chris Lattner58af2a12006-02-15 07:22:58 +0000409
Chris Lattner1913b942008-07-11 00:30:39 +0000410 case ValID::ConstAPInt: // Is it an unsigned const pool reference?
411 if (!isa<IntegerType>(Ty)) {
412 GenerateError("Integral constant '" + D.getName() +
413 "' is invalid or out of range for type '" +
414 Ty->getDescription() + "'");
415 return 0;
416 }
Eric Christopher2a5196f2008-09-24 04:55:49 +0000417
Chris Lattner1913b942008-07-11 00:30:39 +0000418 {
419 APSInt Tmp = *D.ConstPoolInt;
420 Tmp.extOrTrunc(Ty->getPrimitiveSizeInBits());
421 return ConstantInt::get(Tmp);
422 }
Eric Christopher2a5196f2008-09-24 04:55:49 +0000423
Chris Lattner58af2a12006-02-15 07:22:58 +0000424 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattner38905612008-02-19 04:36:25 +0000425 if (!Ty->isFloatingPoint() ||
426 !ConstantFP::isValueValidForType(Ty, *D.ConstPoolFP)) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000427 GenerateError("FP constant invalid for type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000428 return 0;
429 }
Eric Christopher2a5196f2008-09-24 04:55:49 +0000430 // Lexer has no type info, so builds all float and double FP constants
Dale Johannesenc72cd7e2007-09-11 18:33:39 +0000431 // as double. Fix this here. Long double does not need this.
432 if (&D.ConstPoolFP->getSemantics() == &APFloat::IEEEdouble &&
Dale Johannesen236bbd42008-10-09 23:01:34 +0000433 Ty==Type::FloatTy) {
434 bool ignored;
435 D.ConstPoolFP->convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven,
436 &ignored);
437 }
Chris Lattnerd8eb63f2008-04-20 00:41:19 +0000438 return ConstantFP::get(*D.ConstPoolFP);
Chris Lattner58af2a12006-02-15 07:22:58 +0000439
440 case ValID::ConstNullVal: // Is it a null value?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000441 if (!isa<PointerType>(Ty)) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000442 GenerateError("Cannot create a a non pointer null");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000443 return 0;
444 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000445 return ConstantPointerNull::get(cast<PointerType>(Ty));
446
447 case ValID::ConstUndefVal: // Is it an undef value?
448 return UndefValue::get(Ty);
449
450 case ValID::ConstZeroVal: // Is it a zero value?
451 return Constant::getNullValue(Ty);
Eric Christopher2a5196f2008-09-24 04:55:49 +0000452
Chris Lattner58af2a12006-02-15 07:22:58 +0000453 case ValID::ConstantVal: // Fully resolved constant?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000454 if (D.ConstantValue->getType() != Ty) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000455 GenerateError("Constant expression type different from required type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000456 return 0;
457 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000458 return D.ConstantValue;
459
460 case ValID::InlineAsmVal: { // Inline asm expression
461 const PointerType *PTy = dyn_cast<PointerType>(Ty);
462 const FunctionType *FTy =
463 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
Reid Spencer5b7e7532006-09-28 19:28:24 +0000464 if (!FTy || !InlineAsm::Verify(FTy, D.IAD->Constraints)) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000465 GenerateError("Invalid type for asm constraint string");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000466 return 0;
467 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000468 InlineAsm *IA = InlineAsm::get(FTy, D.IAD->AsmString, D.IAD->Constraints,
469 D.IAD->HasSideEffects);
470 D.destroy(); // Free InlineAsmDescriptor.
471 return IA;
472 }
473 default:
Reid Spencera9720f52007-02-05 17:04:00 +0000474 assert(0 && "Unhandled case!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000475 return 0;
476 } // End of switch
477
Reid Spencera9720f52007-02-05 17:04:00 +0000478 assert(0 && "Unhandled case!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000479 return 0;
480}
481
Reid Spencer93c40032007-03-19 18:40:50 +0000482// getVal - This function is identical to getExistingVal, except that if a
Chris Lattner58af2a12006-02-15 07:22:58 +0000483// value is not already defined, it "improvises" by creating a placeholder var
484// that looks and acts just like the requested variable. When the value is
485// defined later, all uses of the placeholder variable are replaced with the
486// real thing.
487//
488static Value *getVal(const Type *Ty, const ValID &ID) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000489 if (Ty == Type::LabelTy) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000490 GenerateError("Cannot use a basic block here");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000491 return 0;
492 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000493
494 // See if the value has already been defined.
Reid Spencer93c40032007-03-19 18:40:50 +0000495 Value *V = getExistingVal(Ty, ID);
Chris Lattner58af2a12006-02-15 07:22:58 +0000496 if (V) return V;
Reid Spencer5b7e7532006-09-28 19:28:24 +0000497 if (TriggerError) return 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000498
Reid Spencer5b7e7532006-09-28 19:28:24 +0000499 if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty)) {
Dan Gohmane4977cf2008-05-23 01:55:30 +0000500 GenerateError("Invalid use of a non-first-class type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000501 return 0;
502 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000503
504 // If we reached here, we referenced either a symbol that we don't know about
505 // or an id number that hasn't been read yet. We may be referencing something
506 // forward, so just create an entry to be resolved later and get to it...
507 //
Anton Korobeynikov38e09802007-04-28 13:48:45 +0000508 switch (ID.Type) {
509 case ValID::GlobalName:
Reid Spencer9c9b63a2007-04-28 16:07:31 +0000510 case ValID::GlobalID: {
Anton Korobeynikov38e09802007-04-28 13:48:45 +0000511 const PointerType *PTy = dyn_cast<PointerType>(Ty);
512 if (!PTy) {
513 GenerateError("Invalid type for reference to global" );
514 return 0;
515 }
516 const Type* ElTy = PTy->getElementType();
517 if (const FunctionType *FTy = dyn_cast<FunctionType>(ElTy))
Gabor Greife64d2482008-04-06 23:07:54 +0000518 V = Function::Create(FTy, GlobalValue::ExternalLinkage);
Anton Korobeynikov38e09802007-04-28 13:48:45 +0000519 else
Christopher Lamba8ed9bf2007-12-11 09:02:08 +0000520 V = new GlobalVariable(ElTy, false, GlobalValue::ExternalLinkage, 0, "",
521 (Module*)0, false, PTy->getAddressSpace());
Anton Korobeynikov38e09802007-04-28 13:48:45 +0000522 break;
Reid Spencer9c9b63a2007-04-28 16:07:31 +0000523 }
Anton Korobeynikov38e09802007-04-28 13:48:45 +0000524 default:
525 V = new Argument(Ty);
526 }
Eric Christopher2a5196f2008-09-24 04:55:49 +0000527
Chris Lattner58af2a12006-02-15 07:22:58 +0000528 // Remember where this forward reference came from. FIXME, shouldn't we try
529 // to recycle these things??
530 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
Duncan Sandsdc024672007-11-27 13:23:08 +0000531 LLLgetLineNo())));
Chris Lattner58af2a12006-02-15 07:22:58 +0000532
533 if (inFunctionScope())
534 InsertValue(V, CurFun.LateResolveValues);
535 else
536 InsertValue(V, CurModule.LateResolveValues);
537 return V;
538}
539
Reid Spencer93c40032007-03-19 18:40:50 +0000540/// defineBBVal - This is a definition of a new basic block with the specified
541/// identifier which must be the same as CurFun.NextValNum, if its numeric.
Nick Lewycky280a6e62008-04-25 16:53:59 +0000542static BasicBlock *defineBBVal(const ValID &ID) {
Reid Spencera9720f52007-02-05 17:04:00 +0000543 assert(inFunctionScope() && "Can't get basic block at global scope!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000544
Chris Lattner58af2a12006-02-15 07:22:58 +0000545 BasicBlock *BB = 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000546
Reid Spencer93c40032007-03-19 18:40:50 +0000547 // First, see if this was forward referenced
Chris Lattner58af2a12006-02-15 07:22:58 +0000548
Reid Spencer93c40032007-03-19 18:40:50 +0000549 std::map<ValID, BasicBlock*>::iterator BBI = CurFun.BBForwardRefs.find(ID);
550 if (BBI != CurFun.BBForwardRefs.end()) {
551 BB = BBI->second;
Chris Lattner58af2a12006-02-15 07:22:58 +0000552 // The forward declaration could have been inserted anywhere in the
553 // function: insert it into the correct place now.
554 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
555 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
Reid Spencer93c40032007-03-19 18:40:50 +0000556
Reid Spencer66728ef2007-03-20 01:13:36 +0000557 // We're about to erase the entry, save the key so we can clean it up.
558 ValID Tmp = BBI->first;
559
Reid Spencer93c40032007-03-19 18:40:50 +0000560 // Erase the forward ref from the map as its no longer "forward"
561 CurFun.BBForwardRefs.erase(ID);
562
Eric Christopher2a5196f2008-09-24 04:55:49 +0000563 // The key has been removed from the map but so we don't want to leave
Reid Spencer66728ef2007-03-20 01:13:36 +0000564 // strdup'd memory around so destroy it too.
565 Tmp.destroy();
566
Reid Spencer93c40032007-03-19 18:40:50 +0000567 // If its a numbered definition, bump the number and set the BB value.
568 if (ID.Type == ValID::LocalID) {
569 assert(ID.Num == CurFun.NextValNum && "Invalid new block number");
570 InsertValue(BB);
571 }
Eric Christopher2a5196f2008-09-24 04:55:49 +0000572 } else {
573 // We haven't seen this BB before and its first mention is a definition.
Devang Patel67909432008-03-03 18:58:47 +0000574 // Just create it and return it.
575 std::string Name (ID.Type == ValID::LocalName ? ID.getName() : "");
Gabor Greife64d2482008-04-06 23:07:54 +0000576 BB = BasicBlock::Create(Name, CurFun.CurrentFunction);
Devang Patel67909432008-03-03 18:58:47 +0000577 if (ID.Type == ValID::LocalID) {
578 assert(ID.Num == CurFun.NextValNum && "Invalid new block number");
579 InsertValue(BB);
580 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000581 }
Reid Spencer93c40032007-03-19 18:40:50 +0000582
Devang Patel67909432008-03-03 18:58:47 +0000583 ID.destroy();
Reid Spencer93c40032007-03-19 18:40:50 +0000584 return BB;
585}
586
587/// getBBVal - get an existing BB value or create a forward reference for it.
Eric Christopher2a5196f2008-09-24 04:55:49 +0000588///
Reid Spencer93c40032007-03-19 18:40:50 +0000589static BasicBlock *getBBVal(const ValID &ID) {
590 assert(inFunctionScope() && "Can't get basic block at global scope!");
591
592 BasicBlock *BB = 0;
593
594 std::map<ValID, BasicBlock*>::iterator BBI = CurFun.BBForwardRefs.find(ID);
595 if (BBI != CurFun.BBForwardRefs.end()) {
596 BB = BBI->second;
597 } if (ID.Type == ValID::LocalName) {
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000598 std::string Name = ID.getName();
Reid Spencer93c40032007-03-19 18:40:50 +0000599 Value *N = CurFun.CurrentFunction->getValueSymbolTable().lookup(Name);
Anton Korobeynikov4aefd6b2008-02-20 12:07:57 +0000600 if (N) {
Reid Spencer93c40032007-03-19 18:40:50 +0000601 if (N->getType()->getTypeID() == Type::LabelTyID)
602 BB = cast<BasicBlock>(N);
603 else
604 GenerateError("Reference to label '" + Name + "' is actually of type '"+
605 N->getType()->getDescription() + "'");
Anton Korobeynikov4aefd6b2008-02-20 12:07:57 +0000606 }
Reid Spencer93c40032007-03-19 18:40:50 +0000607 } else if (ID.Type == ValID::LocalID) {
608 if (ID.Num < CurFun.NextValNum && ID.Num < CurFun.Values.size()) {
609 if (CurFun.Values[ID.Num]->getType()->getTypeID() == Type::LabelTyID)
610 BB = cast<BasicBlock>(CurFun.Values[ID.Num]);
611 else
Eric Christopher2a5196f2008-09-24 04:55:49 +0000612 GenerateError("Reference to label '%" + utostr(ID.Num) +
613 "' is actually of type '"+
Reid Spencer93c40032007-03-19 18:40:50 +0000614 CurFun.Values[ID.Num]->getType()->getDescription() + "'");
615 }
616 } else {
617 GenerateError("Illegal label reference " + ID.getName());
618 return 0;
619 }
620
621 // If its already been defined, return it now.
622 if (BB) {
623 ID.destroy(); // Free strdup'd memory.
624 return BB;
625 }
626
627 // Otherwise, this block has not been seen before, create it.
628 std::string Name;
629 if (ID.Type == ValID::LocalName)
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000630 Name = ID.getName();
Gabor Greife64d2482008-04-06 23:07:54 +0000631 BB = BasicBlock::Create(Name, CurFun.CurrentFunction);
Reid Spencer93c40032007-03-19 18:40:50 +0000632
633 // Insert it in the forward refs map.
634 CurFun.BBForwardRefs[ID] = BB;
635
Chris Lattner58af2a12006-02-15 07:22:58 +0000636 return BB;
637}
638
639
640//===----------------------------------------------------------------------===//
641// Code to handle forward references in instructions
642//===----------------------------------------------------------------------===//
643//
644// This code handles the late binding needed with statements that reference
645// values not defined yet... for example, a forward branch, or the PHI node for
646// a loop body.
647//
648// This keeps a table (CurFun.LateResolveValues) of all such forward references
649// and back patchs after we are done.
650//
651
652// ResolveDefinitions - If we could not resolve some defs at parsing
653// time (forward branches, phi functions for loops, etc...) resolve the
654// defs now...
655//
Eric Christopher2a5196f2008-09-24 04:55:49 +0000656static void
Reid Spencer93c40032007-03-19 18:40:50 +0000657ResolveDefinitions(ValueList &LateResolvers, ValueList *FutureLateResolvers) {
Chris Lattner58af2a12006-02-15 07:22:58 +0000658 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Reid Spencer93c40032007-03-19 18:40:50 +0000659 while (!LateResolvers.empty()) {
660 Value *V = LateResolvers.back();
661 LateResolvers.pop_back();
Chris Lattner58af2a12006-02-15 07:22:58 +0000662
Reid Spencer93c40032007-03-19 18:40:50 +0000663 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
664 CurModule.PlaceHolderInfo.find(V);
665 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000666
Reid Spencer93c40032007-03-19 18:40:50 +0000667 ValID &DID = PHI->second.first;
Chris Lattner58af2a12006-02-15 07:22:58 +0000668
Reid Spencer93c40032007-03-19 18:40:50 +0000669 Value *TheRealValue = getExistingVal(V->getType(), DID);
670 if (TriggerError)
671 return;
672 if (TheRealValue) {
673 V->replaceAllUsesWith(TheRealValue);
674 delete V;
675 CurModule.PlaceHolderInfo.erase(PHI);
676 } else if (FutureLateResolvers) {
677 // Functions have their unresolved items forwarded to the module late
678 // resolver table
679 InsertValue(V, *FutureLateResolvers);
680 } else {
681 if (DID.Type == ValID::LocalName || DID.Type == ValID::GlobalName) {
682 GenerateError("Reference to an invalid definition: '" +DID.getName()+
683 "' of type '" + V->getType()->getDescription() + "'",
684 PHI->second.second);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000685 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000686 } else {
Reid Spencer93c40032007-03-19 18:40:50 +0000687 GenerateError("Reference to an invalid definition: #" +
688 itostr(DID.Num) + " of type '" +
689 V->getType()->getDescription() + "'",
690 PHI->second.second);
691 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000692 }
693 }
694 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000695 LateResolvers.clear();
696}
697
698// ResolveTypeTo - A brand new type was just declared. This means that (if
699// name is not null) things referencing Name can be resolved. Otherwise, things
700// refering to the number can be resolved. Do this now.
701//
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000702static void ResolveTypeTo(std::string *Name, const Type *ToTy) {
Chris Lattner58af2a12006-02-15 07:22:58 +0000703 ValID D;
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000704 if (Name)
705 D = ValID::createLocalName(*Name);
Eric Christopher2a5196f2008-09-24 04:55:49 +0000706 else
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000707 D = ValID::createLocalID(CurModule.Types.size());
Chris Lattner58af2a12006-02-15 07:22:58 +0000708
Reid Spencer861d9d62006-11-28 07:29:44 +0000709 std::map<ValID, PATypeHolder>::iterator I =
Chris Lattner58af2a12006-02-15 07:22:58 +0000710 CurModule.LateResolveTypes.find(D);
711 if (I != CurModule.LateResolveTypes.end()) {
Reid Spencer861d9d62006-11-28 07:29:44 +0000712 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Nuno Lopes6ec8a252008-10-15 11:11:12 +0000713 I->first.destroy();
Chris Lattner58af2a12006-02-15 07:22:58 +0000714 CurModule.LateResolveTypes.erase(I);
715 }
Nuno Lopes191dfb92008-10-03 15:52:39 +0000716 D.destroy();
Chris Lattner58af2a12006-02-15 07:22:58 +0000717}
718
719// setValueName - Set the specified value to the name given. The name may be
720// null potentially, in which case this is a noop. The string passed in is
721// assumed to be a malloc'd string buffer, and is free'd by this function.
722//
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000723static void setValueName(Value *V, std::string *NameStr) {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000724 if (!NameStr) return;
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000725 std::string Name(*NameStr); // Copy string
726 delete NameStr; // Free old string
Chris Lattner58af2a12006-02-15 07:22:58 +0000727
Reid Spencer41dff5e2007-01-26 08:05:27 +0000728 if (V->getType() == Type::VoidTy) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000729 GenerateError("Can't assign name '" + Name+"' to value with void type");
Reid Spencer41dff5e2007-01-26 08:05:27 +0000730 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000731 }
Reid Spencer41dff5e2007-01-26 08:05:27 +0000732
Reid Spencera9720f52007-02-05 17:04:00 +0000733 assert(inFunctionScope() && "Must be in function scope!");
Reid Spenceref9b9a72007-02-05 20:47:22 +0000734 ValueSymbolTable &ST = CurFun.CurrentFunction->getValueSymbolTable();
735 if (ST.lookup(Name)) {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000736 GenerateError("Redefinition of value '" + Name + "' of type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +0000737 V->getType()->getDescription() + "'");
Reid Spencer41dff5e2007-01-26 08:05:27 +0000738 return;
739 }
740
741 // Set the name.
742 V->setName(Name);
Chris Lattner58af2a12006-02-15 07:22:58 +0000743}
744
745/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
746/// this is a declaration, otherwise it is a definition.
747static GlobalVariable *
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000748ParseGlobalVariable(std::string *NameStr,
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000749 GlobalValue::LinkageTypes Linkage,
750 GlobalValue::VisibilityTypes Visibility,
Chris Lattner58af2a12006-02-15 07:22:58 +0000751 bool isConstantGlobal, const Type *Ty,
Christopher Lamba8ed9bf2007-12-11 09:02:08 +0000752 Constant *Initializer, bool IsThreadLocal,
753 unsigned AddressSpace = 0) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000754 if (isa<FunctionType>(Ty)) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000755 GenerateError("Cannot declare global vars of function type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000756 return 0;
757 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +0000758 if (Ty == Type::LabelTy) {
759 GenerateError("Cannot declare global vars of label type");
760 return 0;
761 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000762
Christopher Lamba8ed9bf2007-12-11 09:02:08 +0000763 const PointerType *PTy = PointerType::get(Ty, AddressSpace);
Chris Lattner58af2a12006-02-15 07:22:58 +0000764
765 std::string Name;
766 if (NameStr) {
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000767 Name = *NameStr; // Copy string
768 delete NameStr; // Free old string
Chris Lattner58af2a12006-02-15 07:22:58 +0000769 }
770
771 // See if this global value was forward referenced. If so, recycle the
772 // object.
773 ValID ID;
774 if (!Name.empty()) {
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000775 ID = ValID::createGlobalName(Name);
Chris Lattner58af2a12006-02-15 07:22:58 +0000776 } else {
Reid Spencer93c40032007-03-19 18:40:50 +0000777 ID = ValID::createGlobalID(CurModule.Values.size());
Chris Lattner58af2a12006-02-15 07:22:58 +0000778 }
779
780 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
781 // Move the global to the end of the list, from whereever it was
782 // previously inserted.
783 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
784 CurModule.CurrentModule->getGlobalList().remove(GV);
785 CurModule.CurrentModule->getGlobalList().push_back(GV);
786 GV->setInitializer(Initializer);
787 GV->setLinkage(Linkage);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000788 GV->setVisibility(Visibility);
Chris Lattner58af2a12006-02-15 07:22:58 +0000789 GV->setConstant(isConstantGlobal);
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +0000790 GV->setThreadLocal(IsThreadLocal);
Chris Lattner58af2a12006-02-15 07:22:58 +0000791 InsertValue(GV, CurModule.Values);
Nuno Lopes191dfb92008-10-03 15:52:39 +0000792 ID.destroy();
Chris Lattner58af2a12006-02-15 07:22:58 +0000793 return GV;
794 }
795
Nuno Lopes191dfb92008-10-03 15:52:39 +0000796 ID.destroy();
797
Reid Spenceref9b9a72007-02-05 20:47:22 +0000798 // If this global has a name
Chris Lattner58af2a12006-02-15 07:22:58 +0000799 if (!Name.empty()) {
Reid Spenceref9b9a72007-02-05 20:47:22 +0000800 // if the global we're parsing has an initializer (is a definition) and
801 // has external linkage.
802 if (Initializer && Linkage != GlobalValue::InternalLinkage)
803 // If there is already a global with external linkage with this name
804 if (CurModule.CurrentModule->getGlobalVariable(Name, false)) {
805 // If we allow this GVar to get created, it will be renamed in the
806 // symbol table because it conflicts with an existing GVar. We can't
807 // allow redefinition of GVars whose linking indicates that their name
808 // must stay the same. Issue the error.
809 GenerateError("Redefinition of global variable named '" + Name +
810 "' of type '" + Ty->getDescription() + "'");
811 return 0;
812 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000813 }
814
815 // Otherwise there is no existing GV to use, create one now.
816 GlobalVariable *GV =
817 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
Christopher Lamba8ed9bf2007-12-11 09:02:08 +0000818 CurModule.CurrentModule, IsThreadLocal, AddressSpace);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000819 GV->setVisibility(Visibility);
Chris Lattner58af2a12006-02-15 07:22:58 +0000820 InsertValue(GV, CurModule.Values);
821 return GV;
822}
823
824// setTypeName - Set the specified type to the name given. The name may be
825// null potentially, in which case this is a noop. The string passed in is
826// assumed to be a malloc'd string buffer, and is freed by this function.
827//
828// This function returns true if the type has already been defined, but is
829// allowed to be redefined in the specified context. If the name is a new name
830// for the type plane, it is inserted and false is returned.
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000831static bool setTypeName(const Type *T, std::string *NameStr) {
Reid Spencera9720f52007-02-05 17:04:00 +0000832 assert(!inFunctionScope() && "Can't give types function-local names!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000833 if (NameStr == 0) return false;
Eric Christopher2a5196f2008-09-24 04:55:49 +0000834
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000835 std::string Name(*NameStr); // Copy string
836 delete NameStr; // Free old string
Chris Lattner58af2a12006-02-15 07:22:58 +0000837
838 // We don't allow assigning names to void type
Reid Spencer5b7e7532006-09-28 19:28:24 +0000839 if (T == Type::VoidTy) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000840 GenerateError("Can't assign name '" + Name + "' to the void type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000841 return false;
842 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000843
844 // Set the type name, checking for conflicts as we do so.
845 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
846
847 if (AlreadyExists) { // Inserting a name that is already defined???
848 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
Reid Spencera9720f52007-02-05 17:04:00 +0000849 assert(Existing && "Conflict but no matching type?!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000850
851 // There is only one case where this is allowed: when we are refining an
852 // opaque type. In this case, Existing will be an opaque type.
853 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
854 // We ARE replacing an opaque type!
855 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
856 return true;
857 }
858
859 // Otherwise, this is an attempt to redefine a type. That's okay if
860 // the redefinition is identical to the original. This will be so if
861 // Existing and T point to the same Type object. In this one case we
862 // allow the equivalent redefinition.
863 if (Existing == T) return true; // Yes, it's equal.
864
865 // Any other kind of (non-equivalent) redefinition is an error.
Reid Spencer63c34452007-01-05 21:51:07 +0000866 GenerateError("Redefinition of type named '" + Name + "' of type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +0000867 T->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +0000868 }
869
870 return false;
871}
872
873//===----------------------------------------------------------------------===//
874// Code for handling upreferences in type names...
875//
876
877// TypeContains - Returns true if Ty directly contains E in it.
878//
879static bool TypeContains(const Type *Ty, const Type *E) {
880 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
881 E) != Ty->subtype_end();
882}
883
884namespace {
885 struct UpRefRecord {
886 // NestingLevel - The number of nesting levels that need to be popped before
887 // this type is resolved.
888 unsigned NestingLevel;
889
890 // LastContainedTy - This is the type at the current binding level for the
891 // type. Every time we reduce the nesting level, this gets updated.
892 const Type *LastContainedTy;
893
894 // UpRefTy - This is the actual opaque type that the upreference is
895 // represented with.
896 OpaqueType *UpRefTy;
897
898 UpRefRecord(unsigned NL, OpaqueType *URTy)
899 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
900 };
901}
902
903// UpRefs - A list of the outstanding upreferences that need to be resolved.
904static std::vector<UpRefRecord> UpRefs;
905
906/// HandleUpRefs - Every time we finish a new layer of types, this function is
907/// called. It loops through the UpRefs vector, which is a list of the
908/// currently active types. For each type, if the up reference is contained in
909/// the newly completed type, we decrement the level count. When the level
910/// count reaches zero, the upreferenced type is the type that is passed in:
911/// thus we can complete the cycle.
912///
913static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner224f84f2006-08-18 17:34:45 +0000914 // If Ty isn't abstract, or if there are no up-references in it, then there is
915 // nothing to resolve here.
916 if (!ty->isAbstract() || UpRefs.empty()) return ty;
Eric Christopher2a5196f2008-09-24 04:55:49 +0000917
Chris Lattner58af2a12006-02-15 07:22:58 +0000918 PATypeHolder Ty(ty);
919 UR_OUT("Type '" << Ty->getDescription() <<
920 "' newly formed. Resolving upreferences.\n" <<
921 UpRefs.size() << " upreferences active!\n");
922
923 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
924 // to zero), we resolve them all together before we resolve them to Ty. At
925 // the end of the loop, if there is anything to resolve to Ty, it will be in
926 // this variable.
927 OpaqueType *TypeToResolve = 0;
928
929 for (unsigned i = 0; i != UpRefs.size(); ++i) {
930 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
931 << UpRefs[i].second->getDescription() << ") = "
932 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
933 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
934 // Decrement level of upreference
935 unsigned Level = --UpRefs[i].NestingLevel;
936 UpRefs[i].LastContainedTy = Ty;
937 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
938 if (Level == 0) { // Upreference should be resolved!
939 if (!TypeToResolve) {
940 TypeToResolve = UpRefs[i].UpRefTy;
941 } else {
942 UR_OUT(" * Resolving upreference for "
943 << UpRefs[i].second->getDescription() << "\n";
944 std::string OldName = UpRefs[i].UpRefTy->getDescription());
945 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
946 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
947 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
948 }
949 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
950 --i; // Do not skip the next element...
951 }
952 }
953 }
954
955 if (TypeToResolve) {
956 UR_OUT(" * Resolving upreference for "
957 << UpRefs[i].second->getDescription() << "\n";
958 std::string OldName = TypeToResolve->getDescription());
959 TypeToResolve->refineAbstractTypeTo(Ty);
960 }
961
962 return Ty;
963}
964
Chris Lattner58af2a12006-02-15 07:22:58 +0000965//===----------------------------------------------------------------------===//
966// RunVMAsmParser - Define an interface to this parser
967//===----------------------------------------------------------------------===//
968//
Reid Spencer14310612006-12-31 05:40:51 +0000969static Module* RunParser(Module * M);
970
Duncan Sandsdc024672007-11-27 13:23:08 +0000971Module *llvm::RunVMAsmParser(llvm::MemoryBuffer *MB) {
972 InitLLLexer(MB);
973 Module *M = RunParser(new Module(LLLgetFilename()));
974 FreeLexer();
975 return M;
Chris Lattner58af2a12006-02-15 07:22:58 +0000976}
977
978%}
979
980%union {
981 llvm::Module *ModuleVal;
982 llvm::Function *FunctionVal;
Chris Lattner58af2a12006-02-15 07:22:58 +0000983 llvm::BasicBlock *BasicBlockVal;
984 llvm::TerminatorInst *TermInstVal;
985 llvm::Instruction *InstVal;
Reid Spencera132e042006-12-03 05:46:11 +0000986 llvm::Constant *ConstVal;
Chris Lattner58af2a12006-02-15 07:22:58 +0000987
Reid Spencera132e042006-12-03 05:46:11 +0000988 const llvm::Type *PrimType;
Reid Spencer14310612006-12-31 05:40:51 +0000989 std::list<llvm::PATypeHolder> *TypeList;
Reid Spencera132e042006-12-03 05:46:11 +0000990 llvm::PATypeHolder *TypeVal;
991 llvm::Value *ValueVal;
Reid Spencera132e042006-12-03 05:46:11 +0000992 std::vector<llvm::Value*> *ValueList;
Dan Gohman81a0c0b2008-05-31 00:58:22 +0000993 std::vector<unsigned> *ConstantList;
Reid Spencer14310612006-12-31 05:40:51 +0000994 llvm::ArgListType *ArgList;
995 llvm::TypeWithAttrs TypeWithAttrs;
996 llvm::TypeWithAttrsList *TypeWithAttrsList;
Dale Johanneseneb57ea72007-11-05 21:20:28 +0000997 llvm::ParamList *ParamList;
Reid Spencer14310612006-12-31 05:40:51 +0000998
Chris Lattner58af2a12006-02-15 07:22:58 +0000999 // Represent the RHS of PHI node
Reid Spencera132e042006-12-03 05:46:11 +00001000 std::list<std::pair<llvm::Value*,
1001 llvm::BasicBlock*> > *PHIList;
Chris Lattner58af2a12006-02-15 07:22:58 +00001002 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
Reid Spencera132e042006-12-03 05:46:11 +00001003 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner58af2a12006-02-15 07:22:58 +00001004
1005 llvm::GlobalValue::LinkageTypes Linkage;
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001006 llvm::GlobalValue::VisibilityTypes Visibility;
Devang Patel05988662008-09-25 21:00:45 +00001007 llvm::Attributes Attributes;
Reid Spencer38c91a92007-02-28 02:24:54 +00001008 llvm::APInt *APIntVal;
Chris Lattner58af2a12006-02-15 07:22:58 +00001009 int64_t SInt64Val;
1010 uint64_t UInt64Val;
1011 int SIntVal;
1012 unsigned UIntVal;
Dale Johannesen43421b32007-09-06 18:13:44 +00001013 llvm::APFloat *FPVal;
Chris Lattner58af2a12006-02-15 07:22:58 +00001014 bool BoolVal;
1015
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001016 std::string *StrVal; // This memory must be deleted
1017 llvm::ValID ValIDVal;
Chris Lattner58af2a12006-02-15 07:22:58 +00001018
Reid Spencera132e042006-12-03 05:46:11 +00001019 llvm::Instruction::BinaryOps BinaryOpVal;
1020 llvm::Instruction::TermOps TermOpVal;
1021 llvm::Instruction::MemoryOps MemOpVal;
1022 llvm::Instruction::CastOps CastOpVal;
1023 llvm::Instruction::OtherOps OtherOpVal;
Reid Spencera132e042006-12-03 05:46:11 +00001024 llvm::ICmpInst::Predicate IPredicate;
1025 llvm::FCmpInst::Predicate FPredicate;
Chris Lattner58af2a12006-02-15 07:22:58 +00001026}
1027
Eric Christopher2a5196f2008-09-24 04:55:49 +00001028%type <ModuleVal> Module
Chris Lattner58af2a12006-02-15 07:22:58 +00001029%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
1030%type <BasicBlockVal> BasicBlock InstructionList
1031%type <TermInstVal> BBTerminatorInst
1032%type <InstVal> Inst InstVal MemoryInst
Anton Korobeynikov38e09802007-04-28 13:48:45 +00001033%type <ConstVal> ConstVal ConstExpr AliaseeRef
Chris Lattner58af2a12006-02-15 07:22:58 +00001034%type <ConstVector> ConstVector
1035%type <ArgList> ArgList ArgListH
Chris Lattner58af2a12006-02-15 07:22:58 +00001036%type <PHIList> PHIList
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001037%type <ParamList> ParamList // For call param lists & GEP indices
Reid Spencer14310612006-12-31 05:40:51 +00001038%type <ValueList> IndexList // For GEP indices
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001039%type <ConstantList> ConstantIndexList // For insertvalue/extractvalue indices
Eric Christopher2a5196f2008-09-24 04:55:49 +00001040%type <TypeList> TypeListI
Reid Spencer14310612006-12-31 05:40:51 +00001041%type <TypeWithAttrsList> ArgTypeList ArgTypeListI
Reid Spencer218ded22007-01-05 17:07:23 +00001042%type <TypeWithAttrs> ArgType
Chris Lattner58af2a12006-02-15 07:22:58 +00001043%type <JumpTable> JumpTable
1044%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00001045%type <BoolVal> ThreadLocal // 'thread_local' or not
Chris Lattner58af2a12006-02-15 07:22:58 +00001046%type <BoolVal> OptVolatile // 'volatile' or not
1047%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
1048%type <BoolVal> OptSideEffect // 'sideeffect' or not.
Reid Spencer14310612006-12-31 05:40:51 +00001049%type <Linkage> GVInternalLinkage GVExternalLinkage
1050%type <Linkage> FunctionDefineLinkage FunctionDeclareLinkage
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001051%type <Linkage> AliasLinkage
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001052%type <Visibility> GVVisibilityStyle
Chris Lattner58af2a12006-02-15 07:22:58 +00001053
1054// ValueRef - Unresolved reference to a definition or BB
1055%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
1056%type <ValueVal> ResolvedVal // <type> <valref> pair
Devang Patel7990dc72008-02-20 22:40:23 +00001057%type <ValueList> ReturnedVal
Chris Lattner58af2a12006-02-15 07:22:58 +00001058// Tokens and types for handling constant integer values
1059//
1060// ESINT64VAL - A negative number within long long range
1061%token <SInt64Val> ESINT64VAL
1062
1063// EUINT64VAL - A positive number within uns. long long range
1064%token <UInt64Val> EUINT64VAL
Chris Lattner58af2a12006-02-15 07:22:58 +00001065
Eric Christopher2a5196f2008-09-24 04:55:49 +00001066// ESAPINTVAL - A negative number with arbitrary precision
Reid Spencer38c91a92007-02-28 02:24:54 +00001067%token <APIntVal> ESAPINTVAL
1068
Eric Christopher2a5196f2008-09-24 04:55:49 +00001069// EUAPINTVAL - A positive number with arbitrary precision
Reid Spencer38c91a92007-02-28 02:24:54 +00001070%token <APIntVal> EUAPINTVAL
1071
Reid Spencer41dff5e2007-01-26 08:05:27 +00001072%token <UIntVal> LOCALVAL_ID GLOBALVAL_ID // %123 @123
Chris Lattner58af2a12006-02-15 07:22:58 +00001073%token <FPVal> FPVAL // Float or Double constant
1074
1075// Built in types...
Reid Spencer218ded22007-01-05 17:07:23 +00001076%type <TypeVal> Types ResultTypes
Chris Lattner740e7092008-10-15 06:16:57 +00001077%type <PrimType> PrimType // Classifications
Eric Christopher2a5196f2008-09-24 04:55:49 +00001078%token <PrimType> VOID INTTYPE
Dale Johannesen320fc8a2007-08-03 01:03:46 +00001079%token <PrimType> FLOAT DOUBLE X86_FP80 FP128 PPC_FP128 LABEL
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001080%token TYPE
Chris Lattner58af2a12006-02-15 07:22:58 +00001081
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001082
Eric Christopher2a5196f2008-09-24 04:55:49 +00001083%token<StrVal> LOCALVAR GLOBALVAR LABELSTR
Reid Spencered951ea2007-05-19 07:22:10 +00001084%token<StrVal> STRINGCONSTANT ATSTRINGCONSTANT PCTSTRINGCONSTANT
Reid Spencer41dff5e2007-01-26 08:05:27 +00001085%type <StrVal> LocalName OptLocalName OptLocalAssign
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001086%type <StrVal> GlobalName OptGlobalAssign GlobalAssign
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00001087%type <StrVal> OptSection SectionString OptGC
Chris Lattner58af2a12006-02-15 07:22:58 +00001088
Christopher Lambbf3348d2007-12-12 08:45:45 +00001089%type <UIntVal> OptAlign OptCAlign OptAddrSpace
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001090
Reid Spencer3d6b71e2007-04-09 01:56:05 +00001091%token ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001092%token DECLARE DEFINE GLOBAL CONSTANT SECTION ALIAS VOLATILE THREAD_LOCAL
Reid Spencer14310612006-12-31 05:40:51 +00001093%token TO DOTDOTDOT NULL_TOK UNDEF INTERNAL LINKONCE WEAK APPENDING
Dale Johannesenc7071cc2008-05-14 20:13:36 +00001094%token DLLIMPORT DLLEXPORT EXTERN_WEAK COMMON
Christopher Lamba8ed9bf2007-12-11 09:02:08 +00001095%token OPAQUE EXTERNAL TARGET TRIPLE ALIGN ADDRSPACE
Chris Lattner58af2a12006-02-15 07:22:58 +00001096%token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001097%token CC_TOK CCC_TOK FASTCC_TOK COLDCC_TOK X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
Nick Lewycky280a6e62008-04-25 16:53:59 +00001098%token DATALAYOUT
Chris Lattner15bd0952008-08-29 17:20:18 +00001099%type <UIntVal> OptCallingConv LocalNumber
Devang Patel05988662008-09-25 21:00:45 +00001100%type <Attributes> OptAttributes Attribute
1101%type <Attributes> OptFuncAttrs FuncAttr
Devang Patel652203f2008-09-29 20:49:50 +00001102%type <Attributes> OptRetAttrs RetAttr
Chris Lattner58af2a12006-02-15 07:22:58 +00001103
1104// Basic Block Terminating Operators
1105%token <TermOpVal> RET BR SWITCH INVOKE UNWIND UNREACHABLE
1106
1107// Binary Operators
Reid Spencere4d87aa2006-12-23 06:05:41 +00001108%type <BinaryOpVal> ArithmeticOps LogicalOps // Binops Subcatagories
Reid Spencer3ed469c2006-11-02 20:25:50 +00001109%token <BinaryOpVal> ADD SUB MUL UDIV SDIV FDIV UREM SREM FREM AND OR XOR
Reid Spencer832254e2007-02-02 02:16:23 +00001110%token <BinaryOpVal> SHL LSHR ASHR
1111
Eric Christopher2a5196f2008-09-24 04:55:49 +00001112%token <OtherOpVal> ICMP FCMP VICMP VFCMP
Reid Spencera132e042006-12-03 05:46:11 +00001113%type <IPredicate> IPredicates
Reid Spencera132e042006-12-03 05:46:11 +00001114%type <FPredicate> FPredicates
Eric Christopher2a5196f2008-09-24 04:55:49 +00001115%token EQ NE SLT SGT SLE SGE ULT UGT ULE UGE
Reid Spencer6e18b7d2006-12-03 06:59:29 +00001116%token OEQ ONE OLT OGT OLE OGE ORD UNO UEQ UNE
Chris Lattner58af2a12006-02-15 07:22:58 +00001117
1118// Memory Instructions
1119%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
1120
Reid Spencer3da59db2006-11-27 01:05:10 +00001121// Cast Operators
1122%type <CastOpVal> CastOps
1123%token <CastOpVal> TRUNC ZEXT SEXT FPTRUNC FPEXT BITCAST
1124%token <CastOpVal> UITOFP SITOFP FPTOUI FPTOSI INTTOPTR PTRTOINT
1125
Chris Lattner58af2a12006-02-15 07:22:58 +00001126// Other Operators
Reid Spencer832254e2007-02-02 02:16:23 +00001127%token <OtherOpVal> PHI_TOK SELECT VAARG
Chris Lattnerd5efe842006-04-08 01:18:56 +00001128%token <OtherOpVal> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
Devang Patel5a970972008-02-19 22:27:01 +00001129%token <OtherOpVal> GETRESULT
Dan Gohmane4977cf2008-05-23 01:55:30 +00001130%token <OtherOpVal> EXTRACTVALUE INSERTVALUE
Chris Lattner58af2a12006-02-15 07:22:58 +00001131
Reid Spencer218ded22007-01-05 17:07:23 +00001132// Function Attributes
Reid Spencerb8f85052007-07-31 03:50:36 +00001133%token SIGNEXT ZEROEXT NORETURN INREG SRET NOUNWIND NOALIAS BYVAL NEST
Devang Patel2c9c3e72008-09-26 23:51:19 +00001134%token READNONE READONLY GC OPTSIZE NOINLINE ALWAYSINLINE
Devang Pateld4980812008-09-02 20:52:40 +00001135
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001136// Visibility Styles
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +00001137%token DEFAULT HIDDEN PROTECTED
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001138
Chris Lattner58af2a12006-02-15 07:22:58 +00001139%start Module
1140%%
1141
Chris Lattner58af2a12006-02-15 07:22:58 +00001142
Chris Lattner58af2a12006-02-15 07:22:58 +00001143// Operations that are notably excluded from this list include:
1144// RET, BR, & SWITCH because they end basic blocks and are treated specially.
1145//
Reid Spencer3ed469c2006-11-02 20:25:50 +00001146ArithmeticOps: ADD | SUB | MUL | UDIV | SDIV | FDIV | UREM | SREM | FREM;
Reid Spencer832254e2007-02-02 02:16:23 +00001147LogicalOps : SHL | LSHR | ASHR | AND | OR | XOR;
Eric Christopher2a5196f2008-09-24 04:55:49 +00001148CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | BITCAST |
Reid Spencer3da59db2006-11-27 01:05:10 +00001149 UITOFP | SITOFP | FPTOUI | FPTOSI | INTTOPTR | PTRTOINT;
Reid Spencer832254e2007-02-02 02:16:23 +00001150
Eric Christopher2a5196f2008-09-24 04:55:49 +00001151IPredicates
Reid Spencer4012e832006-12-04 05:24:24 +00001152 : EQ { $$ = ICmpInst::ICMP_EQ; } | NE { $$ = ICmpInst::ICMP_NE; }
Reid Spencer6e18b7d2006-12-03 06:59:29 +00001153 | SLT { $$ = ICmpInst::ICMP_SLT; } | SGT { $$ = ICmpInst::ICMP_SGT; }
1154 | SLE { $$ = ICmpInst::ICMP_SLE; } | SGE { $$ = ICmpInst::ICMP_SGE; }
1155 | ULT { $$ = ICmpInst::ICMP_ULT; } | UGT { $$ = ICmpInst::ICMP_UGT; }
Eric Christopher2a5196f2008-09-24 04:55:49 +00001156 | ULE { $$ = ICmpInst::ICMP_ULE; } | UGE { $$ = ICmpInst::ICMP_UGE; }
Reid Spencer6e18b7d2006-12-03 06:59:29 +00001157 ;
1158
Eric Christopher2a5196f2008-09-24 04:55:49 +00001159FPredicates
Reid Spencer6e18b7d2006-12-03 06:59:29 +00001160 : OEQ { $$ = FCmpInst::FCMP_OEQ; } | ONE { $$ = FCmpInst::FCMP_ONE; }
1161 | OLT { $$ = FCmpInst::FCMP_OLT; } | OGT { $$ = FCmpInst::FCMP_OGT; }
1162 | OLE { $$ = FCmpInst::FCMP_OLE; } | OGE { $$ = FCmpInst::FCMP_OGE; }
1163 | ORD { $$ = FCmpInst::FCMP_ORD; } | UNO { $$ = FCmpInst::FCMP_UNO; }
1164 | UEQ { $$ = FCmpInst::FCMP_UEQ; } | UNE { $$ = FCmpInst::FCMP_UNE; }
1165 | ULT { $$ = FCmpInst::FCMP_ULT; } | UGT { $$ = FCmpInst::FCMP_UGT; }
1166 | ULE { $$ = FCmpInst::FCMP_ULE; } | UGE { $$ = FCmpInst::FCMP_UGE; }
1167 | TRUETOK { $$ = FCmpInst::FCMP_TRUE; }
1168 | FALSETOK { $$ = FCmpInst::FCMP_FALSE; }
1169 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00001170
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001171LocalName : LOCALVAR | STRINGCONSTANT | PCTSTRINGCONSTANT ;
Reid Spencer41dff5e2007-01-26 08:05:27 +00001172OptLocalName : LocalName | /*empty*/ { $$ = 0; };
1173
Christopher Lambbf3348d2007-12-12 08:45:45 +00001174OptAddrSpace : ADDRSPACE '(' EUINT64VAL ')' { $$=$3; }
1175 | /*empty*/ { $$=0; };
1176
Reid Spencer41dff5e2007-01-26 08:05:27 +00001177/// OptLocalAssign - Value producing statements have an optional assignment
1178/// component.
1179OptLocalAssign : LocalName '=' {
1180 $$ = $1;
1181 CHECK_FOR_ERROR
1182 }
1183 | /*empty*/ {
1184 $$ = 0;
1185 CHECK_FOR_ERROR
1186 };
1187
Chris Lattner15bd0952008-08-29 17:20:18 +00001188LocalNumber : LOCALVAL_ID '=' {
1189 $$ = $1;
1190 CHECK_FOR_ERROR
1191};
1192
1193
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001194GlobalName : GLOBALVAR | ATSTRINGCONSTANT ;
Reid Spencer41dff5e2007-01-26 08:05:27 +00001195
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001196OptGlobalAssign : GlobalAssign
Chris Lattner58af2a12006-02-15 07:22:58 +00001197 | /*empty*/ {
1198 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00001199 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001200 };
1201
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001202GlobalAssign : GlobalName '=' {
1203 $$ = $1;
1204 CHECK_FOR_ERROR
Chris Lattner6cdc6822007-04-26 05:31:05 +00001205 };
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001206
Eric Christopher2a5196f2008-09-24 04:55:49 +00001207GVInternalLinkage
1208 : INTERNAL { $$ = GlobalValue::InternalLinkage; }
1209 | WEAK { $$ = GlobalValue::WeakLinkage; }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001210 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1211 | APPENDING { $$ = GlobalValue::AppendingLinkage; }
Eric Christopher2a5196f2008-09-24 04:55:49 +00001212 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
Dale Johannesenc7071cc2008-05-14 20:13:36 +00001213 | COMMON { $$ = GlobalValue::CommonLinkage; }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001214 ;
1215
1216GVExternalLinkage
1217 : DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1218 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
1219 | EXTERNAL { $$ = GlobalValue::ExternalLinkage; }
1220 ;
1221
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001222GVVisibilityStyle
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +00001223 : /*empty*/ { $$ = GlobalValue::DefaultVisibility; }
1224 | DEFAULT { $$ = GlobalValue::DefaultVisibility; }
1225 | HIDDEN { $$ = GlobalValue::HiddenVisibility; }
1226 | PROTECTED { $$ = GlobalValue::ProtectedVisibility; }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001227 ;
1228
Reid Spencer14310612006-12-31 05:40:51 +00001229FunctionDeclareLinkage
1230 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
Eric Christopher2a5196f2008-09-24 04:55:49 +00001231 | DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
Reid Spencer14310612006-12-31 05:40:51 +00001232 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001233 ;
Eric Christopher2a5196f2008-09-24 04:55:49 +00001234
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001235FunctionDefineLinkage
Reid Spencer14310612006-12-31 05:40:51 +00001236 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1237 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001238 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1239 | WEAK { $$ = GlobalValue::WeakLinkage; }
Eric Christopher2a5196f2008-09-24 04:55:49 +00001240 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
1241 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00001242
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001243AliasLinkage
1244 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1245 | WEAK { $$ = GlobalValue::WeakLinkage; }
1246 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
1247 ;
1248
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00001249OptCallingConv : /*empty*/ { $$ = CallingConv::C; } |
1250 CCC_TOK { $$ = CallingConv::C; } |
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00001251 FASTCC_TOK { $$ = CallingConv::Fast; } |
1252 COLDCC_TOK { $$ = CallingConv::Cold; } |
1253 X86_STDCALLCC_TOK { $$ = CallingConv::X86_StdCall; } |
1254 X86_FASTCALLCC_TOK { $$ = CallingConv::X86_FastCall; } |
1255 CC_TOK EUINT64VAL {
Chris Lattner58af2a12006-02-15 07:22:58 +00001256 if ((unsigned)$2 != $2)
Reid Spencerb5334b02007-02-05 10:18:06 +00001257 GEN_ERROR("Calling conv too large");
Chris Lattner58af2a12006-02-15 07:22:58 +00001258 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001259 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001260 };
1261
Devang Patel05988662008-09-25 21:00:45 +00001262Attribute : ZEROEXT { $$ = Attribute::ZExt; }
1263 | ZEXT { $$ = Attribute::ZExt; }
1264 | SIGNEXT { $$ = Attribute::SExt; }
1265 | SEXT { $$ = Attribute::SExt; }
1266 | INREG { $$ = Attribute::InReg; }
1267 | SRET { $$ = Attribute::StructRet; }
1268 | NOALIAS { $$ = Attribute::NoAlias; }
1269 | BYVAL { $$ = Attribute::ByVal; }
1270 | NEST { $$ = Attribute::Nest; }
Eric Christopher2a5196f2008-09-24 04:55:49 +00001271 | ALIGN EUINT64VAL { $$ =
Devang Patel05988662008-09-25 21:00:45 +00001272 Attribute::constructAlignmentFromInt($2); }
Reid Spencer14310612006-12-31 05:40:51 +00001273 ;
1274
Devang Patel05988662008-09-25 21:00:45 +00001275OptAttributes : /* empty */ { $$ = Attribute::None; }
1276 | OptAttributes Attribute {
Reid Spencer7b5d4662007-04-09 06:16:21 +00001277 $$ = $1 | $2;
Reid Spencer14310612006-12-31 05:40:51 +00001278 }
1279 ;
1280
Devang Patel652203f2008-09-29 20:49:50 +00001281RetAttr : INREG { $$ = Attribute::InReg; }
1282 | ZEROEXT { $$ = Attribute::ZExt; }
1283 | SIGNEXT { $$ = Attribute::SExt; }
1284 ;
1285
1286OptRetAttrs : /* empty */ { $$ = Attribute::None; }
1287 | OptRetAttrs RetAttr {
1288 $$ = $1 | $2;
1289 }
1290 ;
1291
1292
Devang Patel05988662008-09-25 21:00:45 +00001293FuncAttr : NORETURN { $$ = Attribute::NoReturn; }
1294 | NOUNWIND { $$ = Attribute::NoUnwind; }
1295 | INREG { $$ = Attribute::InReg; }
1296 | ZEROEXT { $$ = Attribute::ZExt; }
1297 | SIGNEXT { $$ = Attribute::SExt; }
1298 | READNONE { $$ = Attribute::ReadNone; }
1299 | READONLY { $$ = Attribute::ReadOnly; }
Chris Lattner9fc4da42008-10-08 06:44:45 +00001300 | NOINLINE { $$ = Attribute::NoInline; }
1301 | ALWAYSINLINE { $$ = Attribute::AlwaysInline; }
1302 | OPTSIZE { $$ = Attribute::OptimizeForSize; }
Reid Spencer218ded22007-01-05 17:07:23 +00001303 ;
1304
Devang Patel05988662008-09-25 21:00:45 +00001305OptFuncAttrs : /* empty */ { $$ = Attribute::None; }
Reid Spencer218ded22007-01-05 17:07:23 +00001306 | OptFuncAttrs FuncAttr {
Reid Spencer7b5d4662007-04-09 06:16:21 +00001307 $$ = $1 | $2;
Reid Spencer218ded22007-01-05 17:07:23 +00001308 }
Reid Spencer14310612006-12-31 05:40:51 +00001309 ;
1310
Devang Patel652203f2008-09-29 20:49:50 +00001311
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00001312OptGC : /* empty */ { $$ = 0; }
1313 | GC STRINGCONSTANT {
1314 $$ = $2;
1315 }
1316 ;
1317
Chris Lattner58af2a12006-02-15 07:22:58 +00001318// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1319// a comma before it.
1320OptAlign : /*empty*/ { $$ = 0; } |
1321 ALIGN EUINT64VAL {
1322 $$ = $2;
1323 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencerb5334b02007-02-05 10:18:06 +00001324 GEN_ERROR("Alignment must be a power of two");
Reid Spencer61c83e02006-08-18 08:43:06 +00001325 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001326};
1327OptCAlign : /*empty*/ { $$ = 0; } |
1328 ',' ALIGN EUINT64VAL {
1329 $$ = $3;
1330 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencerb5334b02007-02-05 10:18:06 +00001331 GEN_ERROR("Alignment must be a power of two");
Reid Spencer61c83e02006-08-18 08:43:06 +00001332 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001333};
1334
1335
Christopher Lamba8ed9bf2007-12-11 09:02:08 +00001336
Chris Lattner58af2a12006-02-15 07:22:58 +00001337SectionString : SECTION STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001338 for (unsigned i = 0, e = $2->length(); i != e; ++i)
1339 if ((*$2)[i] == '"' || (*$2)[i] == '\\')
Reid Spencerb5334b02007-02-05 10:18:06 +00001340 GEN_ERROR("Invalid character in section name");
Chris Lattner58af2a12006-02-15 07:22:58 +00001341 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001342 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001343};
1344
1345OptSection : /*empty*/ { $$ = 0; } |
1346 SectionString { $$ = $1; };
1347
1348// GlobalVarAttributes - Used to pass the attributes string on a global. CurGV
1349// is set to be the global we are processing.
1350//
1351GlobalVarAttributes : /* empty */ {} |
1352 ',' GlobalVarAttribute GlobalVarAttributes {};
1353GlobalVarAttribute : SectionString {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001354 CurGV->setSection(*$1);
1355 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001356 CHECK_FOR_ERROR
Eric Christopher2a5196f2008-09-24 04:55:49 +00001357 }
Chris Lattner58af2a12006-02-15 07:22:58 +00001358 | ALIGN EUINT64VAL {
1359 if ($2 != 0 && !isPowerOf2_32($2))
Reid Spencerb5334b02007-02-05 10:18:06 +00001360 GEN_ERROR("Alignment must be a power of two");
Chris Lattner58af2a12006-02-15 07:22:58 +00001361 CurGV->setAlignment($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001362 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001363 };
1364
1365//===----------------------------------------------------------------------===//
1366// Types includes all predefined types... except void, because it can only be
Eric Christopher2a5196f2008-09-24 04:55:49 +00001367// used in specific contexts (function returning void for example).
Chris Lattner58af2a12006-02-15 07:22:58 +00001368
1369// Derived types are added later...
1370//
Dale Johannesen320fc8a2007-08-03 01:03:46 +00001371PrimType : INTTYPE | FLOAT | DOUBLE | PPC_FP128 | FP128 | X86_FP80 | LABEL ;
Reid Spencer14310612006-12-31 05:40:51 +00001372
Eric Christopher2a5196f2008-09-24 04:55:49 +00001373Types
Reid Spencer14310612006-12-31 05:40:51 +00001374 : OPAQUE {
Reid Spencera132e042006-12-03 05:46:11 +00001375 $$ = new PATypeHolder(OpaqueType::get());
Reid Spencer61c83e02006-08-18 08:43:06 +00001376 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001377 }
1378 | PrimType {
Reid Spencera132e042006-12-03 05:46:11 +00001379 $$ = new PATypeHolder($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001380 CHECK_FOR_ERROR
Reid Spencer14310612006-12-31 05:40:51 +00001381 }
Christopher Lambbf3348d2007-12-12 08:45:45 +00001382 | Types OptAddrSpace '*' { // Pointer type?
Reid Spencer14310612006-12-31 05:40:51 +00001383 if (*$1 == Type::LabelTy)
1384 GEN_ERROR("Cannot form a pointer to a basic block");
Christopher Lambbf3348d2007-12-12 08:45:45 +00001385 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1, $2)));
Christopher Lamba8ed9bf2007-12-11 09:02:08 +00001386 delete $1;
1387 CHECK_FOR_ERROR
1388 }
Reid Spencer14310612006-12-31 05:40:51 +00001389 | SymbolicValueRef { // Named types are also simple types...
1390 const Type* tmp = getTypeVal($1);
1391 CHECK_FOR_ERROR
1392 $$ = new PATypeHolder(tmp);
1393 }
1394 | '\\' EUINT64VAL { // Type UpReference
Reid Spencerb5334b02007-02-05 10:18:06 +00001395 if ($2 > (uint64_t)~0U) GEN_ERROR("Value out of range");
Chris Lattner58af2a12006-02-15 07:22:58 +00001396 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
1397 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Reid Spencera132e042006-12-03 05:46:11 +00001398 $$ = new PATypeHolder(OT);
Chris Lattner58af2a12006-02-15 07:22:58 +00001399 UR_OUT("New Upreference!\n");
Reid Spencer61c83e02006-08-18 08:43:06 +00001400 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001401 }
Reid Spencer218ded22007-01-05 17:07:23 +00001402 | Types '(' ArgTypeListI ')' OptFuncAttrs {
Duncan Sandsdc024672007-11-27 13:23:08 +00001403 // Allow but ignore attributes on function types; this permits auto-upgrade.
1404 // FIXME: remove in LLVM 3.0.
Chris Lattnera925a142008-04-23 05:37:08 +00001405 const Type *RetTy = *$1;
1406 if (!FunctionType::isValidReturnType(RetTy))
1407 GEN_ERROR("Invalid result type for LLVM function");
Eric Christopher2a5196f2008-09-24 04:55:49 +00001408
Chris Lattner58af2a12006-02-15 07:22:58 +00001409 std::vector<const Type*> Params;
Reid Spencer7b5d4662007-04-09 06:16:21 +00001410 TypeWithAttrsList::iterator I = $3->begin(), E = $3->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00001411 for (; I != E; ++I ) {
Reid Spencer66728ef2007-03-20 01:13:36 +00001412 const Type *Ty = I->Ty->get();
Reid Spencer66728ef2007-03-20 01:13:36 +00001413 Params.push_back(Ty);
Reid Spencer14310612006-12-31 05:40:51 +00001414 }
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001415
Chris Lattner58af2a12006-02-15 07:22:58 +00001416 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1417 if (isVarArg) Params.pop_back();
1418
Anton Korobeynikov05e5a742007-12-03 21:01:29 +00001419 for (unsigned i = 0; i != Params.size(); ++i)
1420 if (!(Params[i]->isFirstClassType() || isa<OpaqueType>(Params[i])))
1421 GEN_ERROR("Function arguments must be value types!");
1422
1423 CHECK_FOR_ERROR
1424
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001425 FunctionType *FT = FunctionType::get(RetTy, Params, isVarArg);
Reid Spencer14310612006-12-31 05:40:51 +00001426 delete $1; // Delete the return type handle
Eric Christopher2a5196f2008-09-24 04:55:49 +00001427 $$ = new PATypeHolder(HandleUpRefs(FT));
Nuno Lopes8a88a372008-10-05 16:49:34 +00001428
1429 // Delete the argument list
1430 for (I = $3->begin() ; I != E; ++I ) {
1431 delete I->Ty;
1432 }
1433 delete $3;
1434
Reid Spencer61c83e02006-08-18 08:43:06 +00001435 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001436 }
Reid Spencer218ded22007-01-05 17:07:23 +00001437 | VOID '(' ArgTypeListI ')' OptFuncAttrs {
Duncan Sandsdc024672007-11-27 13:23:08 +00001438 // Allow but ignore attributes on function types; this permits auto-upgrade.
1439 // FIXME: remove in LLVM 3.0.
Reid Spencer14310612006-12-31 05:40:51 +00001440 std::vector<const Type*> Params;
Reid Spencer7b5d4662007-04-09 06:16:21 +00001441 TypeWithAttrsList::iterator I = $3->begin(), E = $3->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00001442 for ( ; I != E; ++I ) {
Reid Spencer66728ef2007-03-20 01:13:36 +00001443 const Type* Ty = I->Ty->get();
Reid Spencer66728ef2007-03-20 01:13:36 +00001444 Params.push_back(Ty);
Reid Spencer14310612006-12-31 05:40:51 +00001445 }
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001446
Reid Spencer14310612006-12-31 05:40:51 +00001447 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1448 if (isVarArg) Params.pop_back();
1449
Anton Korobeynikov05e5a742007-12-03 21:01:29 +00001450 for (unsigned i = 0; i != Params.size(); ++i)
1451 if (!(Params[i]->isFirstClassType() || isa<OpaqueType>(Params[i])))
1452 GEN_ERROR("Function arguments must be value types!");
1453
1454 CHECK_FOR_ERROR
1455
Duncan Sandsdc024672007-11-27 13:23:08 +00001456 FunctionType *FT = FunctionType::get($1, Params, isVarArg);
Eric Christopher2a5196f2008-09-24 04:55:49 +00001457 $$ = new PATypeHolder(HandleUpRefs(FT));
Nuno Lopes8a88a372008-10-05 16:49:34 +00001458
1459 // Delete the argument list
1460 for (I = $3->begin() ; I != E; ++I ) {
1461 delete I->Ty;
1462 }
1463 delete $3;
1464
Reid Spencer14310612006-12-31 05:40:51 +00001465 CHECK_FOR_ERROR
1466 }
1467
1468 | '[' EUINT64VAL 'x' Types ']' { // Sized array type?
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001469 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, $2)));
Reid Spencera132e042006-12-03 05:46:11 +00001470 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00001471 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001472 }
Chris Lattner32980692007-02-19 07:44:24 +00001473 | '<' EUINT64VAL 'x' Types '>' { // Vector type?
Reid Spencera132e042006-12-03 05:46:11 +00001474 const llvm::Type* ElemTy = $4->get();
1475 if ((unsigned)$2 != $2)
1476 GEN_ERROR("Unsigned result not equal to signed result");
Chris Lattner42a75512007-01-15 02:27:26 +00001477 if (!ElemTy->isFloatingPoint() && !ElemTy->isInteger())
Reid Spencer9d6565a2007-02-15 02:26:10 +00001478 GEN_ERROR("Element type of a VectorType must be primitive");
Reid Spencer9d6565a2007-02-15 02:26:10 +00001479 $$ = new PATypeHolder(HandleUpRefs(VectorType::get(*$4, (unsigned)$2)));
Reid Spencera132e042006-12-03 05:46:11 +00001480 delete $4;
1481 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001482 }
1483 | '{' TypeListI '}' { // Structure type?
1484 std::vector<const Type*> Elements;
Reid Spencera132e042006-12-03 05:46:11 +00001485 for (std::list<llvm::PATypeHolder>::iterator I = $2->begin(),
Chris Lattner58af2a12006-02-15 07:22:58 +00001486 E = $2->end(); I != E; ++I)
Reid Spencera132e042006-12-03 05:46:11 +00001487 Elements.push_back(*I);
Chris Lattner58af2a12006-02-15 07:22:58 +00001488
Reid Spencera132e042006-12-03 05:46:11 +00001489 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattner58af2a12006-02-15 07:22:58 +00001490 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001491 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001492 }
1493 | '{' '}' { // Empty structure type?
Reid Spencera132e042006-12-03 05:46:11 +00001494 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Reid Spencer61c83e02006-08-18 08:43:06 +00001495 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001496 }
Andrew Lenharth6353e052006-12-08 18:07:09 +00001497 | '<' '{' TypeListI '}' '>' {
1498 std::vector<const Type*> Elements;
1499 for (std::list<llvm::PATypeHolder>::iterator I = $3->begin(),
1500 E = $3->end(); I != E; ++I)
1501 Elements.push_back(*I);
1502
1503 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements, true)));
1504 delete $3;
1505 CHECK_FOR_ERROR
1506 }
1507 | '<' '{' '}' '>' { // Empty structure type?
1508 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>(), true));
1509 CHECK_FOR_ERROR
1510 }
Reid Spencer14310612006-12-31 05:40:51 +00001511 ;
1512
Eric Christopher2a5196f2008-09-24 04:55:49 +00001513ArgType
Devang Patel05988662008-09-25 21:00:45 +00001514 : Types OptAttributes {
Duncan Sandsdc024672007-11-27 13:23:08 +00001515 // Allow but ignore attributes on function types; this permits auto-upgrade.
1516 // FIXME: remove in LLVM 3.0.
Eric Christopher2a5196f2008-09-24 04:55:49 +00001517 $$.Ty = $1;
Devang Patel05988662008-09-25 21:00:45 +00001518 $$.Attrs = Attribute::None;
Reid Spencer14310612006-12-31 05:40:51 +00001519 }
1520 ;
1521
Reid Spencer218ded22007-01-05 17:07:23 +00001522ResultTypes
1523 : Types {
Reid Spencer14310612006-12-31 05:40:51 +00001524 if (!UpRefs.empty())
1525 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Devang Patel20071732008-02-23 01:17:37 +00001526 if (!(*$1)->isFirstClassType() && !isa<StructType>($1->get()))
Reid Spencerb5334b02007-02-05 10:18:06 +00001527 GEN_ERROR("LLVM functions cannot return aggregate types");
Reid Spencer218ded22007-01-05 17:07:23 +00001528 $$ = $1;
Reid Spencer14310612006-12-31 05:40:51 +00001529 }
Reid Spencer218ded22007-01-05 17:07:23 +00001530 | VOID {
1531 $$ = new PATypeHolder(Type::VoidTy);
Reid Spencer14310612006-12-31 05:40:51 +00001532 }
1533 ;
1534
1535ArgTypeList : ArgType {
1536 $$ = new TypeWithAttrsList();
1537 $$->push_back($1);
1538 CHECK_FOR_ERROR
1539 }
1540 | ArgTypeList ',' ArgType {
1541 ($$=$1)->push_back($3);
1542 CHECK_FOR_ERROR
1543 }
1544 ;
1545
Eric Christopher2a5196f2008-09-24 04:55:49 +00001546ArgTypeListI
Reid Spencer14310612006-12-31 05:40:51 +00001547 : ArgTypeList
1548 | ArgTypeList ',' DOTDOTDOT {
1549 $$=$1;
Devang Patel05988662008-09-25 21:00:45 +00001550 TypeWithAttrs TWA; TWA.Attrs = Attribute::None;
Reid Spencer14310612006-12-31 05:40:51 +00001551 TWA.Ty = new PATypeHolder(Type::VoidTy);
1552 $$->push_back(TWA);
1553 CHECK_FOR_ERROR
1554 }
1555 | DOTDOTDOT {
1556 $$ = new TypeWithAttrsList;
Devang Patel05988662008-09-25 21:00:45 +00001557 TypeWithAttrs TWA; TWA.Attrs = Attribute::None;
Reid Spencer14310612006-12-31 05:40:51 +00001558 TWA.Ty = new PATypeHolder(Type::VoidTy);
1559 $$->push_back(TWA);
1560 CHECK_FOR_ERROR
1561 }
1562 | /*empty*/ {
1563 $$ = new TypeWithAttrsList();
Reid Spencer61c83e02006-08-18 08:43:06 +00001564 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001565 };
1566
Eric Christopher2a5196f2008-09-24 04:55:49 +00001567// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner58af2a12006-02-15 07:22:58 +00001568// declaration type lists
1569//
Reid Spencer14310612006-12-31 05:40:51 +00001570TypeListI : Types {
Reid Spencera132e042006-12-03 05:46:11 +00001571 $$ = new std::list<PATypeHolder>();
Eric Christopher2a5196f2008-09-24 04:55:49 +00001572 $$->push_back(*$1);
Reid Spencer66728ef2007-03-20 01:13:36 +00001573 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001574 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001575 }
Reid Spencer14310612006-12-31 05:40:51 +00001576 | TypeListI ',' Types {
Eric Christopher2a5196f2008-09-24 04:55:49 +00001577 ($$=$1)->push_back(*$3);
Reid Spencer66728ef2007-03-20 01:13:36 +00001578 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001579 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001580 };
1581
Chris Lattner58af2a12006-02-15 07:22:58 +00001582// ConstVal - The various declarations that go into the constant pool. This
1583// production is used ONLY to represent constants that show up AFTER a 'const',
1584// 'constant' or 'global' token at global scope. Constants that can be inlined
1585// into other expressions (such as integers and constexprs) are handled by the
1586// ResolvedVal, ValueRef and ConstValueRef productions.
1587//
1588ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencer14310612006-12-31 05:40:51 +00001589 if (!UpRefs.empty())
1590 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001591 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001592 if (ATy == 0)
Eric Christopher2a5196f2008-09-24 04:55:49 +00001593 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001594 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001595 const Type *ETy = ATy->getElementType();
Dan Gohman180c1692008-06-23 18:43:26 +00001596 uint64_t NumElements = ATy->getNumElements();
Chris Lattner58af2a12006-02-15 07:22:58 +00001597
1598 // Verify that we have the correct size...
Mon P Wang28873102008-06-25 08:15:39 +00001599 if (NumElements != uint64_t(-1) && NumElements != $3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001600 GEN_ERROR("Type mismatch: constant sized array initialized with " +
Eric Christopher2a5196f2008-09-24 04:55:49 +00001601 utostr($3->size()) + " arguments, but has size of " +
Mon P Wang28873102008-06-25 08:15:39 +00001602 utostr(NumElements) + "");
Chris Lattner58af2a12006-02-15 07:22:58 +00001603
1604 // Verify all elements are correct type!
1605 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencera132e042006-12-03 05:46:11 +00001606 if (ETy != (*$3)[i]->getType())
Eric Christopher2a5196f2008-09-24 04:55:49 +00001607 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00001608 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencera132e042006-12-03 05:46:11 +00001609 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner58af2a12006-02-15 07:22:58 +00001610 }
1611
Reid Spencera132e042006-12-03 05:46:11 +00001612 $$ = ConstantArray::get(ATy, *$3);
1613 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001614 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001615 }
1616 | Types '[' ']' {
Reid Spencer14310612006-12-31 05:40:51 +00001617 if (!UpRefs.empty())
1618 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001619 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001620 if (ATy == 0)
Eric Christopher2a5196f2008-09-24 04:55:49 +00001621 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001622 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001623
Dan Gohman180c1692008-06-23 18:43:26 +00001624 uint64_t NumElements = ATy->getNumElements();
Eric Christopher2a5196f2008-09-24 04:55:49 +00001625 if (NumElements != uint64_t(-1) && NumElements != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001626 GEN_ERROR("Type mismatch: constant sized array initialized with 0"
Mon P Wang28873102008-06-25 08:15:39 +00001627 " arguments, but has size of " + utostr(NumElements) +"");
Reid Spencera132e042006-12-03 05:46:11 +00001628 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
1629 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001630 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001631 }
1632 | Types 'c' STRINGCONSTANT {
Reid Spencer14310612006-12-31 05:40:51 +00001633 if (!UpRefs.empty())
1634 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001635 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001636 if (ATy == 0)
Eric Christopher2a5196f2008-09-24 04:55:49 +00001637 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001638 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001639
Dan Gohman180c1692008-06-23 18:43:26 +00001640 uint64_t NumElements = ATy->getNumElements();
Chris Lattner58af2a12006-02-15 07:22:58 +00001641 const Type *ETy = ATy->getElementType();
Mon P Wang28873102008-06-25 08:15:39 +00001642 if (NumElements != uint64_t(-1) && NumElements != $3->length())
Eric Christopher2a5196f2008-09-24 04:55:49 +00001643 GEN_ERROR("Can't build string constant of size " +
Mon P Wang28873102008-06-25 08:15:39 +00001644 utostr($3->length()) +
1645 " when array has size " + utostr(NumElements) + "");
Chris Lattner58af2a12006-02-15 07:22:58 +00001646 std::vector<Constant*> Vals;
Reid Spencer14310612006-12-31 05:40:51 +00001647 if (ETy == Type::Int8Ty) {
Mon P Wang28873102008-06-25 08:15:39 +00001648 for (uint64_t i = 0; i < $3->length(); ++i)
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001649 Vals.push_back(ConstantInt::get(ETy, (*$3)[i]));
Chris Lattner58af2a12006-02-15 07:22:58 +00001650 } else {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001651 delete $3;
Reid Spencerb5334b02007-02-05 10:18:06 +00001652 GEN_ERROR("Cannot build string arrays of non byte sized elements");
Chris Lattner58af2a12006-02-15 07:22:58 +00001653 }
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001654 delete $3;
Reid Spencera132e042006-12-03 05:46:11 +00001655 $$ = ConstantArray::get(ATy, Vals);
1656 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001657 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001658 }
1659 | Types '<' ConstVector '>' { // Nonempty unsized arr
Reid Spencer14310612006-12-31 05:40:51 +00001660 if (!UpRefs.empty())
1661 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer9d6565a2007-02-15 02:26:10 +00001662 const VectorType *PTy = dyn_cast<VectorType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001663 if (PTy == 0)
Eric Christopher2a5196f2008-09-24 04:55:49 +00001664 GEN_ERROR("Cannot make packed constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001665 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001666 const Type *ETy = PTy->getElementType();
Dan Gohman180c1692008-06-23 18:43:26 +00001667 unsigned NumElements = PTy->getNumElements();
Chris Lattner58af2a12006-02-15 07:22:58 +00001668
1669 // Verify that we have the correct size...
Mon P Wang28873102008-06-25 08:15:39 +00001670 if (NumElements != unsigned(-1) && NumElements != (unsigned)$3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001671 GEN_ERROR("Type mismatch: constant sized packed initialized with " +
Eric Christopher2a5196f2008-09-24 04:55:49 +00001672 utostr($3->size()) + " arguments, but has size of " +
Mon P Wang28873102008-06-25 08:15:39 +00001673 utostr(NumElements) + "");
Chris Lattner58af2a12006-02-15 07:22:58 +00001674
1675 // Verify all elements are correct type!
1676 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencera132e042006-12-03 05:46:11 +00001677 if (ETy != (*$3)[i]->getType())
Eric Christopher2a5196f2008-09-24 04:55:49 +00001678 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00001679 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencera132e042006-12-03 05:46:11 +00001680 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner58af2a12006-02-15 07:22:58 +00001681 }
1682
Reid Spencer9d6565a2007-02-15 02:26:10 +00001683 $$ = ConstantVector::get(PTy, *$3);
Reid Spencera132e042006-12-03 05:46:11 +00001684 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001685 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001686 }
1687 | Types '{' ConstVector '}' {
Reid Spencera132e042006-12-03 05:46:11 +00001688 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001689 if (STy == 0)
Eric Christopher2a5196f2008-09-24 04:55:49 +00001690 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001691 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001692
1693 if ($3->size() != STy->getNumContainedTypes())
Reid Spencerb5334b02007-02-05 10:18:06 +00001694 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattner58af2a12006-02-15 07:22:58 +00001695
1696 // Check to ensure that constants are compatible with the type initializer!
1697 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Reid Spencera132e042006-12-03 05:46:11 +00001698 if ((*$3)[i]->getType() != STy->getElementType(i))
Reid Spencer61c83e02006-08-18 08:43:06 +00001699 GEN_ERROR("Expected type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00001700 STy->getElementType(i)->getDescription() +
1701 "' for element #" + utostr(i) +
Reid Spencerb5334b02007-02-05 10:18:06 +00001702 " of structure initializer");
Chris Lattner58af2a12006-02-15 07:22:58 +00001703
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001704 // Check to ensure that Type is not packed
1705 if (STy->isPacked())
Chris Lattner6cdc6822007-04-26 05:31:05 +00001706 GEN_ERROR("Unpacked Initializer to vector type '" +
1707 STy->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001708
Reid Spencera132e042006-12-03 05:46:11 +00001709 $$ = ConstantStruct::get(STy, *$3);
1710 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001711 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001712 }
1713 | Types '{' '}' {
Reid Spencer14310612006-12-31 05:40:51 +00001714 if (!UpRefs.empty())
1715 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001716 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001717 if (STy == 0)
Eric Christopher2a5196f2008-09-24 04:55:49 +00001718 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001719 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001720
1721 if (STy->getNumContainedTypes() != 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00001722 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattner58af2a12006-02-15 07:22:58 +00001723
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001724 // Check to ensure that Type is not packed
1725 if (STy->isPacked())
Chris Lattner6cdc6822007-04-26 05:31:05 +00001726 GEN_ERROR("Unpacked Initializer to vector type '" +
1727 STy->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001728
1729 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1730 delete $1;
1731 CHECK_FOR_ERROR
1732 }
1733 | Types '<' '{' ConstVector '}' '>' {
1734 const StructType *STy = dyn_cast<StructType>($1->get());
1735 if (STy == 0)
Eric Christopher2a5196f2008-09-24 04:55:49 +00001736 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001737 (*$1)->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001738
1739 if ($4->size() != STy->getNumContainedTypes())
Reid Spencerb5334b02007-02-05 10:18:06 +00001740 GEN_ERROR("Illegal number of initializers for structure type");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001741
1742 // Check to ensure that constants are compatible with the type initializer!
1743 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1744 if ((*$4)[i]->getType() != STy->getElementType(i))
1745 GEN_ERROR("Expected type '" +
1746 STy->getElementType(i)->getDescription() +
1747 "' for element #" + utostr(i) +
Reid Spencerb5334b02007-02-05 10:18:06 +00001748 " of structure initializer");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001749
1750 // Check to ensure that Type is packed
1751 if (!STy->isPacked())
Eric Christopher2a5196f2008-09-24 04:55:49 +00001752 GEN_ERROR("Vector initializer to non-vector type '" +
Chris Lattner32980692007-02-19 07:44:24 +00001753 STy->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001754
1755 $$ = ConstantStruct::get(STy, *$4);
1756 delete $1; delete $4;
1757 CHECK_FOR_ERROR
1758 }
1759 | Types '<' '{' '}' '>' {
1760 if (!UpRefs.empty())
1761 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1762 const StructType *STy = dyn_cast<StructType>($1->get());
1763 if (STy == 0)
Eric Christopher2a5196f2008-09-24 04:55:49 +00001764 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001765 (*$1)->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001766
1767 if (STy->getNumContainedTypes() != 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00001768 GEN_ERROR("Illegal number of initializers for structure type");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001769
1770 // Check to ensure that Type is packed
1771 if (!STy->isPacked())
Eric Christopher2a5196f2008-09-24 04:55:49 +00001772 GEN_ERROR("Vector initializer to non-vector type '" +
Chris Lattner32980692007-02-19 07:44:24 +00001773 STy->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001774
Reid Spencera132e042006-12-03 05:46:11 +00001775 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1776 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001777 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001778 }
1779 | Types NULL_TOK {
Reid Spencer14310612006-12-31 05:40:51 +00001780 if (!UpRefs.empty())
1781 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001782 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001783 if (PTy == 0)
Eric Christopher2a5196f2008-09-24 04:55:49 +00001784 GEN_ERROR("Cannot make null pointer constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001785 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001786
Reid Spencera132e042006-12-03 05:46:11 +00001787 $$ = ConstantPointerNull::get(PTy);
1788 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001789 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001790 }
1791 | Types UNDEF {
Reid Spencer14310612006-12-31 05:40:51 +00001792 if (!UpRefs.empty())
1793 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001794 $$ = UndefValue::get($1->get());
1795 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001796 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001797 }
1798 | Types SymbolicValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00001799 if (!UpRefs.empty())
1800 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001801 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001802 if (Ty == 0)
Devang Patel5a970972008-02-19 22:27:01 +00001803 GEN_ERROR("Global const reference must be a pointer type " + (*$1)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00001804
1805 // ConstExprs can exist in the body of a function, thus creating
1806 // GlobalValues whenever they refer to a variable. Because we are in
Reid Spencer93c40032007-03-19 18:40:50 +00001807 // the context of a function, getExistingVal will search the functions
Chris Lattner58af2a12006-02-15 07:22:58 +00001808 // symbol table instead of the module symbol table for the global symbol,
1809 // which throws things all off. To get around this, we just tell
Reid Spencer93c40032007-03-19 18:40:50 +00001810 // getExistingVal that we are at global scope here.
Chris Lattner58af2a12006-02-15 07:22:58 +00001811 //
1812 Function *SavedCurFn = CurFun.CurrentFunction;
1813 CurFun.CurrentFunction = 0;
1814
Reid Spencer93c40032007-03-19 18:40:50 +00001815 Value *V = getExistingVal(Ty, $2);
Reid Spencer5b7e7532006-09-28 19:28:24 +00001816 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001817
1818 CurFun.CurrentFunction = SavedCurFn;
1819
1820 // If this is an initializer for a constant pointer, which is referencing a
1821 // (currently) undefined variable, create a stub now that shall be replaced
1822 // in the future with the right type of variable.
1823 //
1824 if (V == 0) {
Reid Spencera9720f52007-02-05 17:04:00 +00001825 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001826 const PointerType *PT = cast<PointerType>(Ty);
1827
1828 // First check to see if the forward references value is already created!
1829 PerModuleInfo::GlobalRefsType::iterator I =
1830 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Eric Christopher2a5196f2008-09-24 04:55:49 +00001831
Chris Lattner58af2a12006-02-15 07:22:58 +00001832 if (I != CurModule.GlobalRefs.end()) {
1833 V = I->second; // Placeholder already exists, use it...
1834 $2.destroy();
1835 } else {
1836 std::string Name;
Reid Spencer41dff5e2007-01-26 08:05:27 +00001837 if ($2.Type == ValID::GlobalName)
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001838 Name = $2.getName();
Reid Spencer41dff5e2007-01-26 08:05:27 +00001839 else if ($2.Type != ValID::GlobalID)
1840 GEN_ERROR("Invalid reference to global");
Chris Lattner58af2a12006-02-15 07:22:58 +00001841
1842 // Create the forward referenced global.
1843 GlobalValue *GV;
Eric Christopher2a5196f2008-09-24 04:55:49 +00001844 if (const FunctionType *FTy =
Chris Lattner58af2a12006-02-15 07:22:58 +00001845 dyn_cast<FunctionType>(PT->getElementType())) {
Gabor Greife64d2482008-04-06 23:07:54 +00001846 GV = Function::Create(FTy, GlobalValue::ExternalWeakLinkage, Name,
1847 CurModule.CurrentModule);
Chris Lattner58af2a12006-02-15 07:22:58 +00001848 } else {
1849 GV = new GlobalVariable(PT->getElementType(), false,
Chris Lattner6cdc6822007-04-26 05:31:05 +00001850 GlobalValue::ExternalWeakLinkage, 0,
Chris Lattner58af2a12006-02-15 07:22:58 +00001851 Name, CurModule.CurrentModule);
1852 }
1853
1854 // Keep track of the fact that we have a forward ref to recycle it
1855 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1856 V = GV;
1857 }
1858 }
1859
Reid Spencera132e042006-12-03 05:46:11 +00001860 $$ = cast<GlobalValue>(V);
1861 delete $1; // Free the type handle
Reid Spencer61c83e02006-08-18 08:43:06 +00001862 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001863 }
1864 | Types ConstExpr {
Reid Spencer14310612006-12-31 05:40:51 +00001865 if (!UpRefs.empty())
1866 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001867 if ($1->get() != $2->getType())
Eric Christopher2a5196f2008-09-24 04:55:49 +00001868 GEN_ERROR("Mismatched types for constant expression: " +
Reid Spencere68853b2007-01-04 00:06:14 +00001869 (*$1)->getDescription() + " and " + $2->getType()->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00001870 $$ = $2;
Reid Spencera132e042006-12-03 05:46:11 +00001871 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001872 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001873 }
1874 | Types ZEROINITIALIZER {
Reid Spencer14310612006-12-31 05:40:51 +00001875 if (!UpRefs.empty())
1876 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001877 const Type *Ty = $1->get();
Chris Lattner58af2a12006-02-15 07:22:58 +00001878 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
Reid Spencerb5334b02007-02-05 10:18:06 +00001879 GEN_ERROR("Cannot create a null initialized value of this type");
Reid Spencera132e042006-12-03 05:46:11 +00001880 $$ = Constant::getNullValue(Ty);
1881 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001882 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00001883 }
Chris Lattner740e7092008-10-15 06:16:57 +00001884 | Types ESINT64VAL { // integral constants
1885 if (IntegerType *IT = dyn_cast<IntegerType>($1->get())) {
1886 if (!ConstantInt::isValueValidForType(IT, $2))
1887 GEN_ERROR("Constant value doesn't fit in type");
1888 $$ = ConstantInt::get(IT, $2, true);
1889 } else {
1890 GEN_ERROR("integer constant must have integer type");
1891 }
1892 delete $1;
Reid Spencer38c91a92007-02-28 02:24:54 +00001893 CHECK_FOR_ERROR
1894 }
Chris Lattner740e7092008-10-15 06:16:57 +00001895 | Types ESAPINTVAL { // arbitrary precision integer constants
1896 if (IntegerType *IT = dyn_cast<IntegerType>($1->get())) {
1897 if ($2->getBitWidth() > IT->getBitWidth())
1898 GEN_ERROR("Constant value does not fit in type");
1899 $2->sextOrTrunc(IT->getBitWidth());
1900 $$ = ConstantInt::get(*$2);
1901 } else {
1902 GEN_ERROR("integer constant must have integer type");
Reid Spencer10794272007-03-01 19:41:47 +00001903 }
Chris Lattner740e7092008-10-15 06:16:57 +00001904 delete $1;
Reid Spencer38c91a92007-02-28 02:24:54 +00001905 delete $2;
Reid Spencere4d87aa2006-12-23 06:05:41 +00001906 CHECK_FOR_ERROR
1907 }
Chris Lattner740e7092008-10-15 06:16:57 +00001908 | Types EUINT64VAL { // integral constants
1909 if (IntegerType *IT = dyn_cast<IntegerType>($1->get())) {
1910 if (!ConstantInt::isValueValidForType(IT, $2))
1911 GEN_ERROR("Constant value doesn't fit in type");
1912 $$ = ConstantInt::get(IT, $2, false);
1913 } else {
1914 GEN_ERROR("integer constant must have integer type");
Eric Christopher2a5196f2008-09-24 04:55:49 +00001915 }
Chris Lattner740e7092008-10-15 06:16:57 +00001916 delete $1;
Reid Spencere4d87aa2006-12-23 06:05:41 +00001917 CHECK_FOR_ERROR
1918 }
Chris Lattner740e7092008-10-15 06:16:57 +00001919 | Types EUAPINTVAL { // arbitrary precision integer constants
1920 if (IntegerType *IT = dyn_cast<IntegerType>($1->get())) {
1921 if ($2->getBitWidth() > IT->getBitWidth())
1922 GEN_ERROR("Constant value does not fit in type");
1923 $2->zextOrTrunc(IT->getBitWidth());
1924 $$ = ConstantInt::get(*$2);
1925 } else {
1926 GEN_ERROR("integer constant must have integer type");
1927 }
1928
1929 delete $2;
1930 delete $1;
1931 CHECK_FOR_ERROR
1932 }
1933 | Types TRUETOK { // Boolean constants
1934 if ($1->get() != Type::Int1Ty)
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001935 GEN_ERROR("Constant true must have type i1");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001936 $$ = ConstantInt::getTrue();
Chris Lattner740e7092008-10-15 06:16:57 +00001937 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001938 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001939 }
Chris Lattner740e7092008-10-15 06:16:57 +00001940 | Types FALSETOK { // Boolean constants
1941 if ($1->get() != Type::Int1Ty)
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001942 GEN_ERROR("Constant false must have type i1");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001943 $$ = ConstantInt::getFalse();
Chris Lattner740e7092008-10-15 06:16:57 +00001944 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001945 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001946 }
Chris Lattner740e7092008-10-15 06:16:57 +00001947 | Types FPVAL { // Floating point constants
1948 if (!ConstantFP::isValueValidForType($1->get(), *$2))
Reid Spencerb5334b02007-02-05 10:18:06 +00001949 GEN_ERROR("Floating point constant invalid for type");
Chris Lattner740e7092008-10-15 06:16:57 +00001950
Eric Christopher2a5196f2008-09-24 04:55:49 +00001951 // Lexer has no type info, so builds all float and double FP constants
Dale Johannesenc72cd7e2007-09-11 18:33:39 +00001952 // as double. Fix this here. Long double is done right.
Chris Lattner740e7092008-10-15 06:16:57 +00001953 if (&$2->getSemantics()==&APFloat::IEEEdouble && $1->get()==Type::FloatTy) {
Dale Johannesen236bbd42008-10-09 23:01:34 +00001954 bool ignored;
1955 $2->convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven,
1956 &ignored);
1957 }
Chris Lattnerd8eb63f2008-04-20 00:41:19 +00001958 $$ = ConstantFP::get(*$2);
Chris Lattner740e7092008-10-15 06:16:57 +00001959 delete $1;
Dale Johannesencdd509a2007-09-07 21:07:57 +00001960 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001961 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001962 };
1963
1964
Reid Spencer3da59db2006-11-27 01:05:10 +00001965ConstExpr: CastOps '(' ConstVal TO Types ')' {
Reid Spencer14310612006-12-31 05:40:51 +00001966 if (!UpRefs.empty())
1967 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001968 Constant *Val = $3;
Reid Spencerb0fcf8f2007-01-17 02:48:45 +00001969 const Type *DestTy = $5->get();
1970 if (!CastInst::castIsValid($1, $3, DestTy))
1971 GEN_ERROR("invalid cast opcode for cast from '" +
1972 Val->getType()->getDescription() + "' to '" +
Eric Christopher2a5196f2008-09-24 04:55:49 +00001973 DestTy->getDescription() + "'");
Reid Spencerb0fcf8f2007-01-17 02:48:45 +00001974 $$ = ConstantExpr::getCast($1, $3, DestTy);
Reid Spencera132e042006-12-03 05:46:11 +00001975 delete $5;
Chris Lattner58af2a12006-02-15 07:22:58 +00001976 }
1977 | GETELEMENTPTR '(' ConstVal IndexList ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001978 if (!isa<PointerType>($3->getType()))
Reid Spencerb5334b02007-02-05 10:18:06 +00001979 GEN_ERROR("GetElementPtr requires a pointer operand");
Chris Lattner58af2a12006-02-15 07:22:58 +00001980
Reid Spencera132e042006-12-03 05:46:11 +00001981 const Type *IdxTy =
Dan Gohman041e2eb2008-05-15 19:50:34 +00001982 GetElementPtrInst::getIndexedType($3->getType(), $4->begin(), $4->end());
Reid Spencera132e042006-12-03 05:46:11 +00001983 if (!IdxTy)
Reid Spencerb5334b02007-02-05 10:18:06 +00001984 GEN_ERROR("Index list invalid for constant getelementptr");
Reid Spencera132e042006-12-03 05:46:11 +00001985
Chris Lattnerf7469af2007-01-31 04:44:08 +00001986 SmallVector<Constant*, 8> IdxVec;
Reid Spencera132e042006-12-03 05:46:11 +00001987 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1988 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattner58af2a12006-02-15 07:22:58 +00001989 IdxVec.push_back(C);
1990 else
Reid Spencerb5334b02007-02-05 10:18:06 +00001991 GEN_ERROR("Indices to constant getelementptr must be constants");
Chris Lattner58af2a12006-02-15 07:22:58 +00001992
1993 delete $4;
1994
Chris Lattnerf7469af2007-01-31 04:44:08 +00001995 $$ = ConstantExpr::getGetElementPtr($3, &IdxVec[0], IdxVec.size());
Reid Spencer61c83e02006-08-18 08:43:06 +00001996 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001997 }
1998 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer4fe16d62007-01-11 18:21:29 +00001999 if ($3->getType() != Type::Int1Ty)
Reid Spencerb5334b02007-02-05 10:18:06 +00002000 GEN_ERROR("Select condition must be of boolean type");
Reid Spencera132e042006-12-03 05:46:11 +00002001 if ($5->getType() != $7->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00002002 GEN_ERROR("Select operand types must match");
Reid Spencera132e042006-12-03 05:46:11 +00002003 $$ = ConstantExpr::getSelect($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00002004 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002005 }
2006 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00002007 if ($3->getType() != $5->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00002008 GEN_ERROR("Binary operator types must match");
Reid Spencer1628cec2006-10-26 06:15:43 +00002009 CHECK_FOR_ERROR;
Reid Spencer9eef56f2006-12-05 19:16:11 +00002010 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattner58af2a12006-02-15 07:22:58 +00002011 }
2012 | LogicalOps '(' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00002013 if ($3->getType() != $5->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00002014 GEN_ERROR("Logical operator types must match");
Chris Lattner42a75512007-01-15 02:27:26 +00002015 if (!$3->getType()->isInteger()) {
Eric Christopher2a5196f2008-09-24 04:55:49 +00002016 if (!isa<VectorType>($3->getType()) ||
Reid Spencer9d6565a2007-02-15 02:26:10 +00002017 !cast<VectorType>($3->getType())->getElementType()->isInteger())
Reid Spencerb5334b02007-02-05 10:18:06 +00002018 GEN_ERROR("Logical operator requires integral operands");
Chris Lattner58af2a12006-02-15 07:22:58 +00002019 }
Reid Spencera132e042006-12-03 05:46:11 +00002020 $$ = ConstantExpr::get($1, $3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002021 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002022 }
Reid Spencer4012e832006-12-04 05:24:24 +00002023 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
2024 if ($4->getType() != $6->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00002025 GEN_ERROR("icmp operand types must match");
Reid Spencer4012e832006-12-04 05:24:24 +00002026 $$ = ConstantExpr::getICmp($2, $4, $6);
Reid Spencera132e042006-12-03 05:46:11 +00002027 }
Reid Spencer4012e832006-12-04 05:24:24 +00002028 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
2029 if ($4->getType() != $6->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00002030 GEN_ERROR("fcmp operand types must match");
Reid Spencer4012e832006-12-04 05:24:24 +00002031 $$ = ConstantExpr::getFCmp($2, $4, $6);
Reid Spencera132e042006-12-03 05:46:11 +00002032 }
Nate Begemanac80ade2008-05-12 19:01:56 +00002033 | VICMP IPredicates '(' ConstVal ',' ConstVal ')' {
2034 if ($4->getType() != $6->getType())
2035 GEN_ERROR("vicmp operand types must match");
2036 $$ = ConstantExpr::getVICmp($2, $4, $6);
2037 }
2038 | VFCMP FPredicates '(' ConstVal ',' ConstVal ')' {
2039 if ($4->getType() != $6->getType())
2040 GEN_ERROR("vfcmp operand types must match");
2041 $$ = ConstantExpr::getVFCmp($2, $4, $6);
2042 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002043 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00002044 if (!ExtractElementInst::isValidOperands($3, $5))
Reid Spencerb5334b02007-02-05 10:18:06 +00002045 GEN_ERROR("Invalid extractelement operands");
Reid Spencera132e042006-12-03 05:46:11 +00002046 $$ = ConstantExpr::getExtractElement($3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002047 CHECK_FOR_ERROR
Chris Lattnerd25db202006-04-08 03:55:17 +00002048 }
2049 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00002050 if (!InsertElementInst::isValidOperands($3, $5, $7))
Reid Spencerb5334b02007-02-05 10:18:06 +00002051 GEN_ERROR("Invalid insertelement operands");
Reid Spencera132e042006-12-03 05:46:11 +00002052 $$ = ConstantExpr::getInsertElement($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00002053 CHECK_FOR_ERROR
Chris Lattnerd25db202006-04-08 03:55:17 +00002054 }
2055 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00002056 if (!ShuffleVectorInst::isValidOperands($3, $5, $7))
Reid Spencerb5334b02007-02-05 10:18:06 +00002057 GEN_ERROR("Invalid shufflevector operands");
Reid Spencera132e042006-12-03 05:46:11 +00002058 $$ = ConstantExpr::getShuffleVector($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00002059 CHECK_FOR_ERROR
Dan Gohmane4977cf2008-05-23 01:55:30 +00002060 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002061 | EXTRACTVALUE '(' ConstVal ConstantIndexList ')' {
Dan Gohmane4977cf2008-05-23 01:55:30 +00002062 if (!isa<StructType>($3->getType()) && !isa<ArrayType>($3->getType()))
2063 GEN_ERROR("ExtractValue requires an aggregate operand");
2064
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002065 $$ = ConstantExpr::getExtractValue($3, &(*$4)[0], $4->size());
Dan Gohmane4977cf2008-05-23 01:55:30 +00002066 delete $4;
Dan Gohmane4977cf2008-05-23 01:55:30 +00002067 CHECK_FOR_ERROR
2068 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002069 | INSERTVALUE '(' ConstVal ',' ConstVal ConstantIndexList ')' {
Dan Gohmane4977cf2008-05-23 01:55:30 +00002070 if (!isa<StructType>($3->getType()) && !isa<ArrayType>($3->getType()))
2071 GEN_ERROR("InsertValue requires an aggregate operand");
2072
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002073 $$ = ConstantExpr::getInsertValue($3, $5, &(*$6)[0], $6->size());
Dan Gohmane4977cf2008-05-23 01:55:30 +00002074 delete $6;
Dan Gohmane4977cf2008-05-23 01:55:30 +00002075 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002076 };
2077
Chris Lattnerd25db202006-04-08 03:55:17 +00002078
Chris Lattner58af2a12006-02-15 07:22:58 +00002079// ConstVector - A list of comma separated constants.
2080ConstVector : ConstVector ',' ConstVal {
2081 ($$ = $1)->push_back($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002082 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002083 }
2084 | ConstVal {
Reid Spencera132e042006-12-03 05:46:11 +00002085 $$ = new std::vector<Constant*>();
Chris Lattner58af2a12006-02-15 07:22:58 +00002086 $$->push_back($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002087 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002088 };
2089
2090
2091// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
2092GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
2093
Eric Christopher2a5196f2008-09-24 04:55:49 +00002094// ThreadLocal
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002095ThreadLocal : THREAD_LOCAL { $$ = true; } | { $$ = false; };
2096
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002097// AliaseeRef - Match either GlobalValue or bitcast to GlobalValue.
2098AliaseeRef : ResultTypes SymbolicValueRef {
2099 const Type* VTy = $1->get();
2100 Value *V = getVal(VTy, $2);
Chris Lattner0275cff2007-08-06 21:00:46 +00002101 CHECK_FOR_ERROR
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002102 GlobalValue* Aliasee = dyn_cast<GlobalValue>(V);
2103 if (!Aliasee)
2104 GEN_ERROR("Aliases can be created only to global values");
2105
2106 $$ = Aliasee;
2107 CHECK_FOR_ERROR
2108 delete $1;
2109 }
2110 | BITCAST '(' AliaseeRef TO Types ')' {
2111 Constant *Val = $3;
2112 const Type *DestTy = $5->get();
2113 if (!CastInst::castIsValid($1, $3, DestTy))
2114 GEN_ERROR("invalid cast opcode for cast from '" +
2115 Val->getType()->getDescription() + "' to '" +
2116 DestTy->getDescription() + "'");
Eric Christopher2a5196f2008-09-24 04:55:49 +00002117
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002118 $$ = ConstantExpr::getCast($1, $3, DestTy);
2119 CHECK_FOR_ERROR
2120 delete $5;
2121 };
Chris Lattner58af2a12006-02-15 07:22:58 +00002122
2123//===----------------------------------------------------------------------===//
2124// Rules to match Modules
2125//===----------------------------------------------------------------------===//
2126
2127// Module rule: Capture the result of parsing the whole file into a result
2128// variable...
2129//
Eric Christopher2a5196f2008-09-24 04:55:49 +00002130Module
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002131 : DefinitionList {
2132 $$ = ParserResult = CurModule.CurrentModule;
2133 CurModule.ModuleDone();
2134 CHECK_FOR_ERROR;
2135 }
2136 | /*empty*/ {
2137 $$ = ParserResult = CurModule.CurrentModule;
2138 CurModule.ModuleDone();
2139 CHECK_FOR_ERROR;
2140 }
2141 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00002142
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002143DefinitionList
2144 : Definition
2145 | DefinitionList Definition
2146 ;
2147
Eric Christopher2a5196f2008-09-24 04:55:49 +00002148Definition
Jeff Cohen361c3ef2007-01-21 19:19:31 +00002149 : DEFINE { CurFun.isDeclare = false; } Function {
Chris Lattner58af2a12006-02-15 07:22:58 +00002150 CurFun.FunctionDone();
Reid Spencer61c83e02006-08-18 08:43:06 +00002151 CHECK_FOR_ERROR
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002152 }
2153 | DECLARE { CurFun.isDeclare = true; } FunctionProto {
Reid Spencer61c83e02006-08-18 08:43:06 +00002154 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002155 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002156 | MODULE ASM_TOK AsmBlock {
Reid Spencer61c83e02006-08-18 08:43:06 +00002157 CHECK_FOR_ERROR
Eric Christopher2a5196f2008-09-24 04:55:49 +00002158 }
Reid Spencer41dff5e2007-01-26 08:05:27 +00002159 | OptLocalAssign TYPE Types {
Reid Spencer14310612006-12-31 05:40:51 +00002160 if (!UpRefs.empty())
2161 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00002162 // Eagerly resolve types. This is not an optimization, this is a
2163 // requirement that is due to the fact that we could have this:
2164 //
2165 // %list = type { %list * }
2166 // %list = type { %list * } ; repeated type decl
2167 //
2168 // If types are not resolved eagerly, then the two types will not be
2169 // determined to be the same type!
2170 //
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002171 ResolveTypeTo($1, *$3);
Chris Lattner58af2a12006-02-15 07:22:58 +00002172
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002173 if (!setTypeName(*$3, $1) && !$1) {
Reid Spencer5b7e7532006-09-28 19:28:24 +00002174 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002175 // If this is a named type that is not a redefinition, add it to the slot
2176 // table.
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002177 CurModule.Types.push_back(*$3);
Chris Lattner58af2a12006-02-15 07:22:58 +00002178 }
Reid Spencera132e042006-12-03 05:46:11 +00002179
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002180 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002181 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002182 }
Reid Spencer41dff5e2007-01-26 08:05:27 +00002183 | OptLocalAssign TYPE VOID {
Reid Spencer14310612006-12-31 05:40:51 +00002184 ResolveTypeTo($1, $3);
2185
2186 if (!setTypeName($3, $1) && !$1) {
2187 CHECK_FOR_ERROR
2188 // If this is a named type that is not a redefinition, add it to the slot
2189 // table.
2190 CurModule.Types.push_back($3);
2191 }
2192 CHECK_FOR_ERROR
2193 }
Eric Christopher2a5196f2008-09-24 04:55:49 +00002194 | OptGlobalAssign GVVisibilityStyle ThreadLocal GlobalType ConstVal
2195 OptAddrSpace {
Reid Spencer41dff5e2007-01-26 08:05:27 +00002196 /* "Externally Visible" Linkage */
Eric Christopher2a5196f2008-09-24 04:55:49 +00002197 if ($5 == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002198 GEN_ERROR("Global value initializer is not a constant");
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002199 CurGV = ParseGlobalVariable($1, GlobalValue::ExternalLinkage,
Christopher Lambbf3348d2007-12-12 08:45:45 +00002200 $2, $4, $5->getType(), $5, $3, $6);
Christopher Lamba8ed9bf2007-12-11 09:02:08 +00002201 CHECK_FOR_ERROR
2202 } GlobalVarAttributes {
2203 CurGV = 0;
2204 }
Chris Lattner6cdc6822007-04-26 05:31:05 +00002205 | OptGlobalAssign GVInternalLinkage GVVisibilityStyle ThreadLocal GlobalType
Christopher Lambbf3348d2007-12-12 08:45:45 +00002206 ConstVal OptAddrSpace {
Eric Christopher2a5196f2008-09-24 04:55:49 +00002207 if ($6 == 0)
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002208 GEN_ERROR("Global value initializer is not a constant");
Christopher Lambbf3348d2007-12-12 08:45:45 +00002209 CurGV = ParseGlobalVariable($1, $2, $3, $5, $6->getType(), $6, $4, $7);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002210 CHECK_FOR_ERROR
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002211 } GlobalVarAttributes {
2212 CurGV = 0;
2213 }
Chris Lattner6cdc6822007-04-26 05:31:05 +00002214 | OptGlobalAssign GVExternalLinkage GVVisibilityStyle ThreadLocal GlobalType
Christopher Lambbf3348d2007-12-12 08:45:45 +00002215 Types OptAddrSpace {
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002216 if (!UpRefs.empty())
2217 GEN_ERROR("Invalid upreference in type: " + (*$6)->getDescription());
Christopher Lambbf3348d2007-12-12 08:45:45 +00002218 CurGV = ParseGlobalVariable($1, $2, $3, $5, *$6, 0, $4, $7);
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002219 CHECK_FOR_ERROR
2220 delete $6;
Reid Spencer5b7e7532006-09-28 19:28:24 +00002221 } GlobalVarAttributes {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002222 CurGV = 0;
2223 CHECK_FOR_ERROR
2224 }
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002225 | OptGlobalAssign GVVisibilityStyle ALIAS AliasLinkage AliaseeRef {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002226 std::string Name;
2227 if ($1) {
2228 Name = *$1;
2229 delete $1;
2230 }
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00002231 if (Name.empty())
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002232 GEN_ERROR("Alias name cannot be empty");
Eric Christopher2a5196f2008-09-24 04:55:49 +00002233
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002234 Constant* Aliasee = $5;
2235 if (Aliasee == 0)
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002236 GEN_ERROR(std::string("Invalid aliasee for alias: ") + Name);
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002237
2238 GlobalAlias* GA = new GlobalAlias(Aliasee->getType(), $4, Name, Aliasee,
2239 CurModule.CurrentModule);
2240 GA->setVisibility($2);
2241 InsertValue(GA, CurModule.Values);
Eric Christopher2a5196f2008-09-24 04:55:49 +00002242
2243
Chris Lattner569f7372007-09-10 23:24:14 +00002244 // If there was a forward reference of this alias, resolve it now.
Eric Christopher2a5196f2008-09-24 04:55:49 +00002245
Chris Lattner569f7372007-09-10 23:24:14 +00002246 ValID ID;
2247 if (!Name.empty())
2248 ID = ValID::createGlobalName(Name);
2249 else
2250 ID = ValID::createGlobalID(CurModule.Values.size()-1);
Eric Christopher2a5196f2008-09-24 04:55:49 +00002251
Chris Lattner569f7372007-09-10 23:24:14 +00002252 if (GlobalValue *FWGV =
2253 CurModule.GetForwardRefForGlobal(GA->getType(), ID)) {
2254 // Replace uses of the fwdref with the actual alias.
2255 FWGV->replaceAllUsesWith(GA);
2256 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(FWGV))
2257 GV->eraseFromParent();
2258 else
2259 cast<Function>(FWGV)->eraseFromParent();
2260 }
2261 ID.destroy();
Eric Christopher2a5196f2008-09-24 04:55:49 +00002262
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00002263 CHECK_FOR_ERROR
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00002264 }
Eric Christopher2a5196f2008-09-24 04:55:49 +00002265 | TARGET TargetDefinition {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002266 CHECK_FOR_ERROR
2267 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002268 | DEPLIBS '=' LibrariesDefinition {
Reid Spencer61c83e02006-08-18 08:43:06 +00002269 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002270 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002271 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00002272
2273
2274AsmBlock : STRINGCONSTANT {
2275 const std::string &AsmSoFar = CurModule.CurrentModule->getModuleInlineAsm();
Chris Lattner58af2a12006-02-15 07:22:58 +00002276 if (AsmSoFar.empty())
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002277 CurModule.CurrentModule->setModuleInlineAsm(*$1);
Chris Lattner58af2a12006-02-15 07:22:58 +00002278 else
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002279 CurModule.CurrentModule->setModuleInlineAsm(AsmSoFar+"\n"+*$1);
2280 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002281 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002282};
2283
Reid Spencer41dff5e2007-01-26 08:05:27 +00002284TargetDefinition : TRIPLE '=' STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002285 CurModule.CurrentModule->setTargetTriple(*$3);
2286 delete $3;
John Criswell2f6a8b12006-10-24 19:09:48 +00002287 }
Chris Lattner1ae022f2006-10-22 06:08:13 +00002288 | DATALAYOUT '=' STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002289 CurModule.CurrentModule->setDataLayout(*$3);
2290 delete $3;
Owen Anderson1dc69692006-10-18 02:21:48 +00002291 };
Chris Lattner58af2a12006-02-15 07:22:58 +00002292
2293LibrariesDefinition : '[' LibList ']';
2294
2295LibList : LibList ',' STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002296 CurModule.CurrentModule->addLibrary(*$3);
2297 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002298 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002299 }
2300 | STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002301 CurModule.CurrentModule->addLibrary(*$1);
2302 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002303 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002304 }
2305 | /* empty: end of list */ {
Reid Spencer61c83e02006-08-18 08:43:06 +00002306 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002307 }
2308 ;
2309
2310//===----------------------------------------------------------------------===//
2311// Rules to match Function Headers
2312//===----------------------------------------------------------------------===//
2313
Devang Patel05988662008-09-25 21:00:45 +00002314ArgListH : ArgListH ',' Types OptAttributes OptLocalName {
Reid Spencer14310612006-12-31 05:40:51 +00002315 if (!UpRefs.empty())
2316 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002317 if (!(*$3)->isFirstClassType())
2318 GEN_ERROR("Argument types must be first-class");
Reid Spencer14310612006-12-31 05:40:51 +00002319 ArgListEntry E; E.Attrs = $4; E.Ty = $3; E.Name = $5;
Chris Lattner58af2a12006-02-15 07:22:58 +00002320 $$ = $1;
Reid Spencer14310612006-12-31 05:40:51 +00002321 $1->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002322 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002323 }
Devang Patel05988662008-09-25 21:00:45 +00002324 | Types OptAttributes OptLocalName {
Reid Spencer14310612006-12-31 05:40:51 +00002325 if (!UpRefs.empty())
2326 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002327 if (!(*$1)->isFirstClassType())
2328 GEN_ERROR("Argument types must be first-class");
Reid Spencer14310612006-12-31 05:40:51 +00002329 ArgListEntry E; E.Attrs = $2; E.Ty = $1; E.Name = $3;
2330 $$ = new ArgListType;
2331 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002332 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002333 };
2334
2335ArgList : ArgListH {
2336 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002337 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002338 }
2339 | ArgListH ',' DOTDOTDOT {
2340 $$ = $1;
Reid Spencer14310612006-12-31 05:40:51 +00002341 struct ArgListEntry E;
2342 E.Ty = new PATypeHolder(Type::VoidTy);
2343 E.Name = 0;
Devang Patel05988662008-09-25 21:00:45 +00002344 E.Attrs = Attribute::None;
Reid Spencer14310612006-12-31 05:40:51 +00002345 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002346 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002347 }
2348 | DOTDOTDOT {
Reid Spencer14310612006-12-31 05:40:51 +00002349 $$ = new ArgListType;
2350 struct ArgListEntry E;
2351 E.Ty = new PATypeHolder(Type::VoidTy);
2352 E.Name = 0;
Devang Patel05988662008-09-25 21:00:45 +00002353 E.Attrs = Attribute::None;
Reid Spencer14310612006-12-31 05:40:51 +00002354 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002355 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002356 }
2357 | /* empty */ {
2358 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00002359 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002360 };
2361
Devang Patel652203f2008-09-29 20:49:50 +00002362FunctionHeaderH : OptCallingConv OptRetAttrs ResultTypes GlobalName '(' ArgList ')'
Devang Patel2c9c3e72008-09-26 23:51:19 +00002363 OptFuncAttrs OptSection OptAlign OptGC {
Devang Patel652203f2008-09-29 20:49:50 +00002364 std::string FunctionName(*$4);
2365 delete $4; // Free strdup'd memory!
Eric Christopher2a5196f2008-09-24 04:55:49 +00002366
Reid Spencer8c8a2dc2007-01-02 21:54:12 +00002367 // Check the function result for abstractness if this is a define. We should
2368 // have no abstract types at this point
Devang Patel652203f2008-09-29 20:49:50 +00002369 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved($3))
2370 GEN_ERROR("Reference to abstract result: "+ $3->get()->getDescription());
Reid Spencer8c8a2dc2007-01-02 21:54:12 +00002371
Devang Patel652203f2008-09-29 20:49:50 +00002372 if (!FunctionType::isValidReturnType(*$3))
Chris Lattnera925a142008-04-23 05:37:08 +00002373 GEN_ERROR("Invalid result type for LLVM function");
Eric Christopher2a5196f2008-09-24 04:55:49 +00002374
Chris Lattner58af2a12006-02-15 07:22:58 +00002375 std::vector<const Type*> ParamTypeList;
Devang Patel05988662008-09-25 21:00:45 +00002376 SmallVector<AttributeWithIndex, 8> Attrs;
Devang Patel19c87462008-09-26 22:53:05 +00002377 //FIXME : In 3.0, stop accepting zext, sext and inreg as optional function
2378 //attributes.
Devang Patel652203f2008-09-29 20:49:50 +00002379 Attributes RetAttrs = $2;
2380 if ($8 != Attribute::None) {
2381 if ($8 & Attribute::ZExt) {
Devang Patel19c87462008-09-26 22:53:05 +00002382 RetAttrs = RetAttrs | Attribute::ZExt;
Devang Patel652203f2008-09-29 20:49:50 +00002383 $8 = $8 ^ Attribute::ZExt;
Devang Patel19c87462008-09-26 22:53:05 +00002384 }
Devang Patel652203f2008-09-29 20:49:50 +00002385 if ($8 & Attribute::SExt) {
Devang Patel19c87462008-09-26 22:53:05 +00002386 RetAttrs = RetAttrs | Attribute::SExt;
Devang Patel652203f2008-09-29 20:49:50 +00002387 $8 = $8 ^ Attribute::SExt;
Devang Patel19c87462008-09-26 22:53:05 +00002388 }
Devang Patel652203f2008-09-29 20:49:50 +00002389 if ($8 & Attribute::InReg) {
Devang Patel19c87462008-09-26 22:53:05 +00002390 RetAttrs = RetAttrs | Attribute::InReg;
Devang Patel652203f2008-09-29 20:49:50 +00002391 $8 = $8 ^ Attribute::InReg;
Devang Patel19c87462008-09-26 22:53:05 +00002392 }
Devang Patel19c87462008-09-26 22:53:05 +00002393 }
Devang Patel652203f2008-09-29 20:49:50 +00002394 if (RetAttrs != Attribute::None)
2395 Attrs.push_back(AttributeWithIndex::get(0, RetAttrs));
2396 if ($6) { // If there are arguments...
Reid Spencer7b5d4662007-04-09 06:16:21 +00002397 unsigned index = 1;
Devang Patel652203f2008-09-29 20:49:50 +00002398 for (ArgListType::iterator I = $6->begin(); I != $6->end(); ++I, ++index) {
Reid Spencer14310612006-12-31 05:40:51 +00002399 const Type* Ty = I->Ty->get();
Reid Spencer8c8a2dc2007-01-02 21:54:12 +00002400 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved(I->Ty))
2401 GEN_ERROR("Reference to abstract argument: " + Ty->getDescription());
Reid Spencer14310612006-12-31 05:40:51 +00002402 ParamTypeList.push_back(Ty);
Devang Patel05988662008-09-25 21:00:45 +00002403 if (Ty != Type::VoidTy && I->Attrs != Attribute::None)
2404 Attrs.push_back(AttributeWithIndex::get(index, I->Attrs));
Reid Spencer14310612006-12-31 05:40:51 +00002405 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002406 }
Devang Patel652203f2008-09-29 20:49:50 +00002407 if ($8 != Attribute::None)
2408 Attrs.push_back(AttributeWithIndex::get(~0, $8));
Chris Lattner58af2a12006-02-15 07:22:58 +00002409
2410 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
2411 if (isVarArg) ParamTypeList.pop_back();
2412
Devang Patel05988662008-09-25 21:00:45 +00002413 AttrListPtr PAL;
Christopher Lamb5c104242007-04-22 20:09:11 +00002414 if (!Attrs.empty())
Devang Patel05988662008-09-25 21:00:45 +00002415 PAL = AttrListPtr::get(Attrs.begin(), Attrs.end());
Reid Spencer7b5d4662007-04-09 06:16:21 +00002416
Devang Patel652203f2008-09-29 20:49:50 +00002417 FunctionType *FT = FunctionType::get(*$3, ParamTypeList, isVarArg);
Christopher Lamb4374f8e2007-12-17 01:17:35 +00002418 const PointerType *PFT = PointerType::getUnqual(FT);
Devang Patel652203f2008-09-29 20:49:50 +00002419 delete $3;
Chris Lattner58af2a12006-02-15 07:22:58 +00002420
2421 ValID ID;
2422 if (!FunctionName.empty()) {
Reid Spencer41dff5e2007-01-26 08:05:27 +00002423 ID = ValID::createGlobalName((char*)FunctionName.c_str());
Chris Lattner58af2a12006-02-15 07:22:58 +00002424 } else {
Reid Spencer93c40032007-03-19 18:40:50 +00002425 ID = ValID::createGlobalID(CurModule.Values.size());
Chris Lattner58af2a12006-02-15 07:22:58 +00002426 }
2427
2428 Function *Fn = 0;
2429 // See if this function was forward referenced. If so, recycle the object.
2430 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
Eric Christopher2a5196f2008-09-24 04:55:49 +00002431 // Move the function to the end of the list, from whereever it was
Chris Lattner58af2a12006-02-15 07:22:58 +00002432 // previously inserted.
2433 Fn = cast<Function>(FWRef);
Devang Patel05988662008-09-25 21:00:45 +00002434 assert(Fn->getAttributes().isEmpty() &&
Chris Lattner58d74912008-03-12 17:45:29 +00002435 "Forward reference has parameter attributes!");
Chris Lattner58af2a12006-02-15 07:22:58 +00002436 CurModule.CurrentModule->getFunctionList().remove(Fn);
2437 CurModule.CurrentModule->getFunctionList().push_back(Fn);
2438 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
Reid Spenceref9b9a72007-02-05 20:47:22 +00002439 (Fn = CurModule.CurrentModule->getFunction(FunctionName))) {
Duncan Sandsdc024672007-11-27 13:23:08 +00002440 if (Fn->getFunctionType() != FT ) {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002441 // The existing function doesn't have the same type. This is an overload
2442 // error.
2443 GEN_ERROR("Overload of function '" + FunctionName + "' not permitted.");
Devang Patel05988662008-09-25 21:00:45 +00002444 } else if (Fn->getAttributes() != PAL) {
Duncan Sandsdc024672007-11-27 13:23:08 +00002445 // The existing function doesn't have the same parameter attributes.
2446 // This is an overload error.
2447 GEN_ERROR("Overload of function '" + FunctionName + "' not permitted.");
Reid Spenceref9b9a72007-02-05 20:47:22 +00002448 } else if (!CurFun.isDeclare && !Fn->isDeclaration()) {
Chris Lattner6cdc6822007-04-26 05:31:05 +00002449 // Neither the existing or the current function is a declaration and they
2450 // have the same name and same type. Clearly this is a redefinition.
2451 GEN_ERROR("Redefinition of function '" + FunctionName + "'");
Duncan Sandsdc024672007-11-27 13:23:08 +00002452 } else if (Fn->isDeclaration()) {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002453 // Make sure to strip off any argument names so we can't get conflicts.
Chris Lattner58af2a12006-02-15 07:22:58 +00002454 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
2455 AI != AE; ++AI)
2456 AI->setName("");
Reid Spenceref9b9a72007-02-05 20:47:22 +00002457 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002458 } else { // Not already defined?
Gabor Greife64d2482008-04-06 23:07:54 +00002459 Fn = Function::Create(FT, GlobalValue::ExternalWeakLinkage, FunctionName,
2460 CurModule.CurrentModule);
Chris Lattner58af2a12006-02-15 07:22:58 +00002461 InsertValue(Fn, CurModule.Values);
2462 }
2463
Nuno Lopes9e9631d2008-10-03 15:45:58 +00002464 ID.destroy();
Chris Lattner58af2a12006-02-15 07:22:58 +00002465 CurFun.FunctionStart(Fn);
Anton Korobeynikov93c2b372006-09-17 13:06:18 +00002466
2467 if (CurFun.isDeclare) {
2468 // If we have declaration, always overwrite linkage. This will allow us to
2469 // correctly handle cases, when pointer to function is passed as argument to
2470 // another function.
2471 Fn->setLinkage(CurFun.Linkage);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002472 Fn->setVisibility(CurFun.Visibility);
Anton Korobeynikov93c2b372006-09-17 13:06:18 +00002473 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002474 Fn->setCallingConv($1);
Devang Patel05988662008-09-25 21:00:45 +00002475 Fn->setAttributes(PAL);
Devang Patel652203f2008-09-29 20:49:50 +00002476 Fn->setAlignment($10);
2477 if ($9) {
2478 Fn->setSection(*$9);
2479 delete $9;
Chris Lattner58af2a12006-02-15 07:22:58 +00002480 }
Devang Patel652203f2008-09-29 20:49:50 +00002481 if ($11) {
2482 Fn->setGC($11->c_str());
2483 delete $11;
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00002484 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002485
2486 // Add all of the arguments we parsed to the function...
Devang Patel652203f2008-09-29 20:49:50 +00002487 if ($6) { // Is null if empty...
Chris Lattner58af2a12006-02-15 07:22:58 +00002488 if (isVarArg) { // Nuke the last entry
Devang Patel652203f2008-09-29 20:49:50 +00002489 assert($6->back().Ty->get() == Type::VoidTy && $6->back().Name == 0 &&
Reid Spencera9720f52007-02-05 17:04:00 +00002490 "Not a varargs marker!");
Devang Patel652203f2008-09-29 20:49:50 +00002491 delete $6->back().Ty;
2492 $6->pop_back(); // Delete the last entry
Chris Lattner58af2a12006-02-15 07:22:58 +00002493 }
2494 Function::arg_iterator ArgIt = Fn->arg_begin();
Reid Spenceref9b9a72007-02-05 20:47:22 +00002495 Function::arg_iterator ArgEnd = Fn->arg_end();
Reid Spencer14310612006-12-31 05:40:51 +00002496 unsigned Idx = 1;
Devang Patel652203f2008-09-29 20:49:50 +00002497 for (ArgListType::iterator I = $6->begin();
2498 I != $6->end() && ArgIt != ArgEnd; ++I, ++ArgIt) {
Reid Spencer14310612006-12-31 05:40:51 +00002499 delete I->Ty; // Delete the typeholder...
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002500 setValueName(ArgIt, I->Name); // Insert arg into symtab...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002501 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002502 InsertValue(ArgIt);
Reid Spencer14310612006-12-31 05:40:51 +00002503 Idx++;
Chris Lattner58af2a12006-02-15 07:22:58 +00002504 }
Reid Spencera132e042006-12-03 05:46:11 +00002505
Devang Patel652203f2008-09-29 20:49:50 +00002506 delete $6; // We're now done with the argument list
Chris Lattner58af2a12006-02-15 07:22:58 +00002507 }
Reid Spencer61c83e02006-08-18 08:43:06 +00002508 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002509};
2510
2511BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
2512
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002513FunctionHeader : FunctionDefineLinkage GVVisibilityStyle FunctionHeaderH BEGIN {
Chris Lattner58af2a12006-02-15 07:22:58 +00002514 $$ = CurFun.CurrentFunction;
2515
2516 // Make sure that we keep track of the linkage type even if there was a
2517 // previous "declare".
2518 $$->setLinkage($1);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002519 $$->setVisibility($2);
Chris Lattner58af2a12006-02-15 07:22:58 +00002520};
2521
2522END : ENDTOK | '}'; // Allow end of '}' to end a function
2523
2524Function : BasicBlockList END {
2525 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002526 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002527};
2528
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002529FunctionProto : FunctionDeclareLinkage GVVisibilityStyle FunctionHeaderH {
Reid Spencer14310612006-12-31 05:40:51 +00002530 CurFun.CurrentFunction->setLinkage($1);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002531 CurFun.CurrentFunction->setVisibility($2);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002532 $$ = CurFun.CurrentFunction;
2533 CurFun.FunctionDone();
2534 CHECK_FOR_ERROR
2535 };
Chris Lattner58af2a12006-02-15 07:22:58 +00002536
2537//===----------------------------------------------------------------------===//
2538// Rules to match Basic Blocks
2539//===----------------------------------------------------------------------===//
2540
2541OptSideEffect : /* empty */ {
2542 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002543 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002544 }
2545 | SIDEEFFECT {
2546 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002547 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002548 };
2549
2550ConstValueRef : ESINT64VAL { // A reference to a direct constant
2551 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002552 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002553 }
2554 | EUINT64VAL {
2555 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002556 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002557 }
Chris Lattner1913b942008-07-11 00:30:39 +00002558 | ESAPINTVAL { // arbitrary precision integer constants
2559 $$ = ValID::create(*$1, true);
2560 delete $1;
2561 CHECK_FOR_ERROR
Eric Christopher2a5196f2008-09-24 04:55:49 +00002562 }
Chris Lattner1913b942008-07-11 00:30:39 +00002563 | EUAPINTVAL { // arbitrary precision integer constants
2564 $$ = ValID::create(*$1, false);
2565 delete $1;
2566 CHECK_FOR_ERROR
2567 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002568 | FPVAL { // Perhaps it's an FP constant?
2569 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002570 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002571 }
2572 | TRUETOK {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00002573 $$ = ValID::create(ConstantInt::getTrue());
Reid Spencer61c83e02006-08-18 08:43:06 +00002574 CHECK_FOR_ERROR
Eric Christopher2a5196f2008-09-24 04:55:49 +00002575 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002576 | FALSETOK {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00002577 $$ = ValID::create(ConstantInt::getFalse());
Reid Spencer61c83e02006-08-18 08:43:06 +00002578 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002579 }
2580 | NULL_TOK {
2581 $$ = ValID::createNull();
Reid Spencer61c83e02006-08-18 08:43:06 +00002582 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002583 }
2584 | UNDEF {
2585 $$ = ValID::createUndef();
Reid Spencer61c83e02006-08-18 08:43:06 +00002586 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002587 }
2588 | ZEROINITIALIZER { // A vector zero constant.
2589 $$ = ValID::createZeroInit();
Reid Spencer61c83e02006-08-18 08:43:06 +00002590 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002591 }
2592 | '<' ConstVector '>' { // Nonempty unsized packed vector
Reid Spencera132e042006-12-03 05:46:11 +00002593 const Type *ETy = (*$2)[0]->getType();
Eric Christopher2a5196f2008-09-24 04:55:49 +00002594 unsigned NumElements = $2->size();
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002595
2596 if (!ETy->isInteger() && !ETy->isFloatingPoint())
2597 GEN_ERROR("Invalid vector element type: " + ETy->getDescription());
Eric Christopher2a5196f2008-09-24 04:55:49 +00002598
Reid Spencer9d6565a2007-02-15 02:26:10 +00002599 VectorType* pt = VectorType::get(ETy, NumElements);
Dan Gohmanf910eaa2008-06-09 14:45:02 +00002600 PATypeHolder* PTy = new PATypeHolder(HandleUpRefs(pt));
Eric Christopher2a5196f2008-09-24 04:55:49 +00002601
Chris Lattner58af2a12006-02-15 07:22:58 +00002602 // Verify all elements are correct type!
2603 for (unsigned i = 0; i < $2->size(); i++) {
Reid Spencera132e042006-12-03 05:46:11 +00002604 if (ETy != (*$2)[i]->getType())
Eric Christopher2a5196f2008-09-24 04:55:49 +00002605 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00002606 ETy->getDescription() +"' as required!\nIt is of type '" +
Reid Spencera132e042006-12-03 05:46:11 +00002607 (*$2)[i]->getType()->getDescription() + "'.");
Chris Lattner58af2a12006-02-15 07:22:58 +00002608 }
2609
Reid Spencer9d6565a2007-02-15 02:26:10 +00002610 $$ = ValID::create(ConstantVector::get(pt, *$2));
Chris Lattner58af2a12006-02-15 07:22:58 +00002611 delete PTy; delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002612 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002613 }
Dan Gohmanf910eaa2008-06-09 14:45:02 +00002614 | '[' ConstVector ']' { // Nonempty unsized arr
2615 const Type *ETy = (*$2)[0]->getType();
Eric Christopher2a5196f2008-09-24 04:55:49 +00002616 uint64_t NumElements = $2->size();
Dan Gohmanf910eaa2008-06-09 14:45:02 +00002617
2618 if (!ETy->isFirstClassType())
2619 GEN_ERROR("Invalid array element type: " + ETy->getDescription());
2620
2621 ArrayType *ATy = ArrayType::get(ETy, NumElements);
2622 PATypeHolder* PTy = new PATypeHolder(HandleUpRefs(ATy));
2623
2624 // Verify all elements are correct type!
2625 for (unsigned i = 0; i < $2->size(); i++) {
2626 if (ETy != (*$2)[i]->getType())
Eric Christopher2a5196f2008-09-24 04:55:49 +00002627 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Dan Gohmanf910eaa2008-06-09 14:45:02 +00002628 ETy->getDescription() +"' as required!\nIt is of type '"+
2629 (*$2)[i]->getType()->getDescription() + "'.");
2630 }
2631
2632 $$ = ValID::create(ConstantArray::get(ATy, *$2));
2633 delete PTy; delete $2;
2634 CHECK_FOR_ERROR
2635 }
2636 | '[' ']' {
Dan Gohman180c1692008-06-23 18:43:26 +00002637 // Use undef instead of an array because it's inconvenient to determine
2638 // the element type at this point, there being no elements to examine.
Dan Gohmanf910eaa2008-06-09 14:45:02 +00002639 $$ = ValID::createUndef();
2640 CHECK_FOR_ERROR
2641 }
2642 | 'c' STRINGCONSTANT {
Dan Gohman180c1692008-06-23 18:43:26 +00002643 uint64_t NumElements = $2->length();
Dan Gohmanf910eaa2008-06-09 14:45:02 +00002644 const Type *ETy = Type::Int8Ty;
2645
2646 ArrayType *ATy = ArrayType::get(ETy, NumElements);
2647
2648 std::vector<Constant*> Vals;
2649 for (unsigned i = 0; i < $2->length(); ++i)
2650 Vals.push_back(ConstantInt::get(ETy, (*$2)[i]));
2651 delete $2;
2652 $$ = ValID::create(ConstantArray::get(ATy, Vals));
2653 CHECK_FOR_ERROR
2654 }
2655 | '{' ConstVector '}' {
2656 std::vector<const Type*> Elements($2->size());
2657 for (unsigned i = 0, e = $2->size(); i != e; ++i)
2658 Elements[i] = (*$2)[i]->getType();
2659
2660 const StructType *STy = StructType::get(Elements);
2661 PATypeHolder* PTy = new PATypeHolder(HandleUpRefs(STy));
2662
2663 $$ = ValID::create(ConstantStruct::get(STy, *$2));
2664 delete PTy; delete $2;
2665 CHECK_FOR_ERROR
2666 }
2667 | '{' '}' {
2668 const StructType *STy = StructType::get(std::vector<const Type*>());
2669 $$ = ValID::create(ConstantStruct::get(STy, std::vector<Constant*>()));
2670 CHECK_FOR_ERROR
2671 }
2672 | '<' '{' ConstVector '}' '>' {
2673 std::vector<const Type*> Elements($3->size());
2674 for (unsigned i = 0, e = $3->size(); i != e; ++i)
2675 Elements[i] = (*$3)[i]->getType();
2676
2677 const StructType *STy = StructType::get(Elements, /*isPacked=*/true);
2678 PATypeHolder* PTy = new PATypeHolder(HandleUpRefs(STy));
2679
2680 $$ = ValID::create(ConstantStruct::get(STy, *$3));
2681 delete PTy; delete $3;
2682 CHECK_FOR_ERROR
2683 }
2684 | '<' '{' '}' '>' {
2685 const StructType *STy = StructType::get(std::vector<const Type*>(),
2686 /*isPacked=*/true);
2687 $$ = ValID::create(ConstantStruct::get(STy, std::vector<Constant*>()));
2688 CHECK_FOR_ERROR
2689 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002690 | ConstExpr {
Reid Spencera132e042006-12-03 05:46:11 +00002691 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002692 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002693 }
2694 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002695 $$ = ValID::createInlineAsm(*$3, *$5, $2);
2696 delete $3;
2697 delete $5;
Reid Spencer61c83e02006-08-18 08:43:06 +00002698 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002699 };
2700
2701// SymbolicValueRef - Reference to one of two ways of symbolically refering to
2702// another value.
2703//
Reid Spencer41dff5e2007-01-26 08:05:27 +00002704SymbolicValueRef : LOCALVAL_ID { // Is it an integer reference...?
2705 $$ = ValID::createLocalID($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002706 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002707 }
Reid Spencer41dff5e2007-01-26 08:05:27 +00002708 | GLOBALVAL_ID {
2709 $$ = ValID::createGlobalID($1);
2710 CHECK_FOR_ERROR
2711 }
2712 | LocalName { // Is it a named reference...?
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002713 $$ = ValID::createLocalName(*$1);
2714 delete $1;
Reid Spencer41dff5e2007-01-26 08:05:27 +00002715 CHECK_FOR_ERROR
2716 }
2717 | GlobalName { // Is it a named reference...?
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002718 $$ = ValID::createGlobalName(*$1);
2719 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002720 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002721 };
2722
2723// ValueRef - A reference to a definition... either constant or symbolic
2724ValueRef : SymbolicValueRef | ConstValueRef;
2725
2726
2727// ResolvedVal - a <type> <value> pair. This is used only in cases where the
2728// type immediately preceeds the value reference, and allows complex constant
2729// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
2730ResolvedVal : Types ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00002731 if (!UpRefs.empty())
2732 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Eric Christopher2a5196f2008-09-24 04:55:49 +00002733 $$ = getVal(*$1, $2);
Reid Spencer14310612006-12-31 05:40:51 +00002734 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002735 CHECK_FOR_ERROR
Reid Spencer14310612006-12-31 05:40:51 +00002736 }
2737 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00002738
Devang Patel7990dc72008-02-20 22:40:23 +00002739ReturnedVal : ResolvedVal {
2740 $$ = new std::vector<Value *>();
Eric Christopher2a5196f2008-09-24 04:55:49 +00002741 $$->push_back($1);
Devang Patel7990dc72008-02-20 22:40:23 +00002742 CHECK_FOR_ERROR
2743 }
Devang Patel6bfc63b2008-02-23 00:38:56 +00002744 | ReturnedVal ',' ResolvedVal {
Eric Christopher2a5196f2008-09-24 04:55:49 +00002745 ($$=$1)->push_back($3);
Devang Patel7990dc72008-02-20 22:40:23 +00002746 CHECK_FOR_ERROR
2747 };
2748
Chris Lattner58af2a12006-02-15 07:22:58 +00002749BasicBlockList : BasicBlockList BasicBlock {
2750 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002751 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002752 }
Eric Christopher2a5196f2008-09-24 04:55:49 +00002753 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
Chris Lattner58af2a12006-02-15 07:22:58 +00002754 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002755 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002756 };
2757
2758
Eric Christopher2a5196f2008-09-24 04:55:49 +00002759// Basic blocks are terminated by branching instructions:
Chris Lattner58af2a12006-02-15 07:22:58 +00002760// br, br/cc, switch, ret
2761//
Chris Lattner15bd0952008-08-29 17:20:18 +00002762BasicBlock : InstructionList OptLocalAssign BBTerminatorInst {
Chris Lattner58af2a12006-02-15 07:22:58 +00002763 setValueName($3, $2);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002764 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002765 InsertValue($3);
Chris Lattner58af2a12006-02-15 07:22:58 +00002766 $1->getInstList().push_back($3);
Chris Lattner58af2a12006-02-15 07:22:58 +00002767 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002768 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002769 };
2770
Chris Lattner15bd0952008-08-29 17:20:18 +00002771BasicBlock : InstructionList LocalNumber BBTerminatorInst {
2772 CHECK_FOR_ERROR
2773 int ValNum = InsertValue($3);
2774 if (ValNum != (int)$2)
2775 GEN_ERROR("Result value number %" + utostr($2) +
2776 " is incorrect, expected %" + utostr((unsigned)ValNum));
Eric Christopher2a5196f2008-09-24 04:55:49 +00002777
Chris Lattner15bd0952008-08-29 17:20:18 +00002778 $1->getInstList().push_back($3);
2779 $$ = $1;
2780 CHECK_FOR_ERROR
2781};
2782
2783
Chris Lattner58af2a12006-02-15 07:22:58 +00002784InstructionList : InstructionList Inst {
Reid Spencer3da59db2006-11-27 01:05:10 +00002785 if (CastInst *CI1 = dyn_cast<CastInst>($2))
2786 if (CastInst *CI2 = dyn_cast<CastInst>(CI1->getOperand(0)))
2787 if (CI2->getParent() == 0)
2788 $1->getInstList().push_back(CI2);
Chris Lattner58af2a12006-02-15 07:22:58 +00002789 $1->getInstList().push_back($2);
2790 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002791 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002792 }
Reid Spencer93c40032007-03-19 18:40:50 +00002793 | /* empty */ { // Empty space between instruction lists
Nick Lewycky280a6e62008-04-25 16:53:59 +00002794 $$ = defineBBVal(ValID::createLocalID(CurFun.NextValNum));
Reid Spencer61c83e02006-08-18 08:43:06 +00002795 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002796 }
Reid Spencer93c40032007-03-19 18:40:50 +00002797 | LABELSTR { // Labelled (named) basic block
Nick Lewycky280a6e62008-04-25 16:53:59 +00002798 $$ = defineBBVal(ValID::createLocalName(*$1));
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002799 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002800 CHECK_FOR_ERROR
Nick Lewycky280a6e62008-04-25 16:53:59 +00002801
Chris Lattner58af2a12006-02-15 07:22:58 +00002802 };
2803
Eric Christopher2a5196f2008-09-24 04:55:49 +00002804BBTerminatorInst :
Devang Patel7990dc72008-02-20 22:40:23 +00002805 RET ReturnedVal { // Return with a result...
Devang Patelb82b7f22008-02-26 22:17:48 +00002806 ValueList &VL = *$2;
Devang Patel13b823c2008-02-26 23:19:08 +00002807 assert(!VL.empty() && "Invalid ret operands!");
Dan Gohman1a570242008-07-23 00:54:54 +00002808 const Type *ReturnType = CurFun.CurrentFunction->getReturnType();
2809 if (VL.size() > 1 ||
2810 (isa<StructType>(ReturnType) &&
2811 (VL.empty() || VL[0]->getType() != ReturnType))) {
2812 Value *RV = UndefValue::get(ReturnType);
2813 for (unsigned i = 0, e = VL.size(); i != e; ++i) {
2814 Instruction *I = InsertValueInst::Create(RV, VL[i], i, "mrv");
2815 ($<BasicBlockVal>-1)->getInstList().push_back(I);
2816 RV = I;
2817 }
2818 $$ = ReturnInst::Create(RV);
2819 } else {
2820 $$ = ReturnInst::Create(VL[0]);
2821 }
Devang Patel7990dc72008-02-20 22:40:23 +00002822 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002823 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002824 }
Reid Spencer93c40032007-03-19 18:40:50 +00002825 | RET VOID { // Return with no result...
Gabor Greife64d2482008-04-06 23:07:54 +00002826 $$ = ReturnInst::Create();
Reid Spencer61c83e02006-08-18 08:43:06 +00002827 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002828 }
Reid Spencer93c40032007-03-19 18:40:50 +00002829 | BR LABEL ValueRef { // Unconditional Branch...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002830 BasicBlock* tmpBB = getBBVal($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002831 CHECK_FOR_ERROR
Gabor Greife64d2482008-04-06 23:07:54 +00002832 $$ = BranchInst::Create(tmpBB);
Reid Spencer93c40032007-03-19 18:40:50 +00002833 } // Conditional Branch...
Eric Christopher2a5196f2008-09-24 04:55:49 +00002834 | BR INTTYPE ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002835 if (cast<IntegerType>($2)->getBitWidth() != 1)
2836 GEN_ERROR("Branch condition must have type i1");
Reid Spencer5b7e7532006-09-28 19:28:24 +00002837 BasicBlock* tmpBBA = getBBVal($6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002838 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002839 BasicBlock* tmpBBB = getBBVal($9);
2840 CHECK_FOR_ERROR
Reid Spencer4fe16d62007-01-11 18:21:29 +00002841 Value* tmpVal = getVal(Type::Int1Ty, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002842 CHECK_FOR_ERROR
Gabor Greife64d2482008-04-06 23:07:54 +00002843 $$ = BranchInst::Create(tmpBBA, tmpBBB, tmpVal);
Chris Lattner58af2a12006-02-15 07:22:58 +00002844 }
Chris Lattnerf9078f92008-10-15 06:03:48 +00002845 | SWITCH INTTYPE ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencera132e042006-12-03 05:46:11 +00002846 Value* tmpVal = getVal($2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002847 CHECK_FOR_ERROR
2848 BasicBlock* tmpBB = getBBVal($6);
2849 CHECK_FOR_ERROR
Gabor Greife64d2482008-04-06 23:07:54 +00002850 SwitchInst *S = SwitchInst::Create(tmpVal, tmpBB, $8->size());
Chris Lattner58af2a12006-02-15 07:22:58 +00002851 $$ = S;
2852
2853 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
2854 E = $8->end();
2855 for (; I != E; ++I) {
2856 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
2857 S->addCase(CI, I->second);
2858 else
Reid Spencerb5334b02007-02-05 10:18:06 +00002859 GEN_ERROR("Switch case is constant, but not a simple integer");
Chris Lattner58af2a12006-02-15 07:22:58 +00002860 }
2861 delete $8;
Reid Spencer61c83e02006-08-18 08:43:06 +00002862 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002863 }
Chris Lattnerf9078f92008-10-15 06:03:48 +00002864 | SWITCH INTTYPE ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencera132e042006-12-03 05:46:11 +00002865 Value* tmpVal = getVal($2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002866 CHECK_FOR_ERROR
2867 BasicBlock* tmpBB = getBBVal($6);
2868 CHECK_FOR_ERROR
Gabor Greife64d2482008-04-06 23:07:54 +00002869 SwitchInst *S = SwitchInst::Create(tmpVal, tmpBB, 0);
Chris Lattner58af2a12006-02-15 07:22:58 +00002870 $$ = S;
Reid Spencer61c83e02006-08-18 08:43:06 +00002871 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002872 }
Devang Patel652203f2008-09-29 20:49:50 +00002873 | INVOKE OptCallingConv OptRetAttrs ResultTypes ValueRef '(' ParamList ')'
2874 OptFuncAttrs TO LABEL ValueRef UNWIND LABEL ValueRef {
Chris Lattner58af2a12006-02-15 07:22:58 +00002875
Reid Spencer14310612006-12-31 05:40:51 +00002876 // Handle the short syntax
2877 const PointerType *PFTy = 0;
2878 const FunctionType *Ty = 0;
Devang Patel652203f2008-09-29 20:49:50 +00002879 if (!(PFTy = dyn_cast<PointerType>($4->get())) ||
Chris Lattner58af2a12006-02-15 07:22:58 +00002880 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
2881 // Pull out the types of all of the arguments...
2882 std::vector<const Type*> ParamTypes;
Devang Patel652203f2008-09-29 20:49:50 +00002883 ParamList::iterator I = $7->begin(), E = $7->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00002884 for (; I != E; ++I) {
Reid Spencer14310612006-12-31 05:40:51 +00002885 const Type *Ty = I->Val->getType();
2886 if (Ty == Type::VoidTy)
2887 GEN_ERROR("Short call syntax cannot be used with varargs");
2888 ParamTypes.push_back(Ty);
Chris Lattner58af2a12006-02-15 07:22:58 +00002889 }
Eric Christopher2a5196f2008-09-24 04:55:49 +00002890
Devang Patel652203f2008-09-29 20:49:50 +00002891 if (!FunctionType::isValidReturnType(*$4))
Chris Lattnera925a142008-04-23 05:37:08 +00002892 GEN_ERROR("Invalid result type for LLVM function");
2893
Devang Patel652203f2008-09-29 20:49:50 +00002894 Ty = FunctionType::get($4->get(), ParamTypes, false);
Christopher Lamb4374f8e2007-12-17 01:17:35 +00002895 PFTy = PointerType::getUnqual(Ty);
Chris Lattner58af2a12006-02-15 07:22:58 +00002896 }
2897
Devang Patel652203f2008-09-29 20:49:50 +00002898 delete $4;
Reid Spencer66728ef2007-03-20 01:13:36 +00002899
Devang Patel652203f2008-09-29 20:49:50 +00002900 Value *V = getVal(PFTy, $5); // Get the function we're calling...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002901 CHECK_FOR_ERROR
Devang Patel652203f2008-09-29 20:49:50 +00002902 BasicBlock *Normal = getBBVal($12);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002903 CHECK_FOR_ERROR
Devang Patel652203f2008-09-29 20:49:50 +00002904 BasicBlock *Except = getBBVal($15);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002905 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002906
Devang Patel05988662008-09-25 21:00:45 +00002907 SmallVector<AttributeWithIndex, 8> Attrs;
Devang Patel19c87462008-09-26 22:53:05 +00002908 //FIXME : In 3.0, stop accepting zext, sext and inreg as optional function
2909 //attributes.
Devang Patel652203f2008-09-29 20:49:50 +00002910 Attributes RetAttrs = $3;
2911 if ($9 != Attribute::None) {
2912 if ($9 & Attribute::ZExt) {
Devang Patel19c87462008-09-26 22:53:05 +00002913 RetAttrs = RetAttrs | Attribute::ZExt;
Devang Patel652203f2008-09-29 20:49:50 +00002914 $9 = $9 ^ Attribute::ZExt;
Devang Patel19c87462008-09-26 22:53:05 +00002915 }
Devang Patel652203f2008-09-29 20:49:50 +00002916 if ($9 & Attribute::SExt) {
Devang Patel19c87462008-09-26 22:53:05 +00002917 RetAttrs = RetAttrs | Attribute::SExt;
Devang Patel652203f2008-09-29 20:49:50 +00002918 $9 = $9 ^ Attribute::SExt;
Devang Patel19c87462008-09-26 22:53:05 +00002919 }
Devang Patel652203f2008-09-29 20:49:50 +00002920 if ($9 & Attribute::InReg) {
Devang Patel19c87462008-09-26 22:53:05 +00002921 RetAttrs = RetAttrs | Attribute::InReg;
Devang Patel652203f2008-09-29 20:49:50 +00002922 $9 = $9 ^ Attribute::InReg;
Devang Patel19c87462008-09-26 22:53:05 +00002923 }
Devang Patel19c87462008-09-26 22:53:05 +00002924 }
Devang Patel652203f2008-09-29 20:49:50 +00002925 if (RetAttrs != Attribute::None)
2926 Attrs.push_back(AttributeWithIndex::get(0, RetAttrs));
Devang Patel19c87462008-09-26 22:53:05 +00002927
Reid Spencer14310612006-12-31 05:40:51 +00002928 // Check the arguments
2929 ValueList Args;
Devang Patel652203f2008-09-29 20:49:50 +00002930 if ($7->empty()) { // Has no arguments?
Reid Spencer14310612006-12-31 05:40:51 +00002931 // Make sure no arguments is a good thing!
2932 if (Ty->getNumParams() != 0)
2933 GEN_ERROR("No arguments passed to a function that "
Reid Spencerb5334b02007-02-05 10:18:06 +00002934 "expects arguments");
Chris Lattner58af2a12006-02-15 07:22:58 +00002935 } else { // Has arguments?
2936 // Loop through FunctionType's arguments and ensure they are specified
2937 // correctly!
Chris Lattner58af2a12006-02-15 07:22:58 +00002938 FunctionType::param_iterator I = Ty->param_begin();
2939 FunctionType::param_iterator E = Ty->param_end();
Devang Patel652203f2008-09-29 20:49:50 +00002940 ParamList::iterator ArgI = $7->begin(), ArgE = $7->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00002941 unsigned index = 1;
Chris Lattner58af2a12006-02-15 07:22:58 +00002942
Duncan Sandsdc024672007-11-27 13:23:08 +00002943 for (; ArgI != ArgE && I != E; ++ArgI, ++I, ++index) {
Reid Spencer14310612006-12-31 05:40:51 +00002944 if (ArgI->Val->getType() != *I)
2945 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00002946 (*I)->getDescription() + "'");
Reid Spencer14310612006-12-31 05:40:51 +00002947 Args.push_back(ArgI->Val);
Devang Patel05988662008-09-25 21:00:45 +00002948 if (ArgI->Attrs != Attribute::None)
2949 Attrs.push_back(AttributeWithIndex::get(index, ArgI->Attrs));
Reid Spencer14310612006-12-31 05:40:51 +00002950 }
Reid Spencera132e042006-12-03 05:46:11 +00002951
Reid Spencer14310612006-12-31 05:40:51 +00002952 if (Ty->isVarArg()) {
2953 if (I == E)
Chris Lattner38905612008-02-19 04:36:25 +00002954 for (; ArgI != ArgE; ++ArgI, ++index) {
Reid Spencer14310612006-12-31 05:40:51 +00002955 Args.push_back(ArgI->Val); // push the remaining varargs
Devang Patel05988662008-09-25 21:00:45 +00002956 if (ArgI->Attrs != Attribute::None)
2957 Attrs.push_back(AttributeWithIndex::get(index, ArgI->Attrs));
Chris Lattner38905612008-02-19 04:36:25 +00002958 }
Reid Spencer14310612006-12-31 05:40:51 +00002959 } else if (I != E || ArgI != ArgE)
Reid Spencerb5334b02007-02-05 10:18:06 +00002960 GEN_ERROR("Invalid number of parameters detected");
Chris Lattner58af2a12006-02-15 07:22:58 +00002961 }
Devang Patel652203f2008-09-29 20:49:50 +00002962 if ($9 != Attribute::None)
2963 Attrs.push_back(AttributeWithIndex::get(~0, $9));
Devang Patel05988662008-09-25 21:00:45 +00002964 AttrListPtr PAL;
Duncan Sandsdc024672007-11-27 13:23:08 +00002965 if (!Attrs.empty())
Devang Patel05988662008-09-25 21:00:45 +00002966 PAL = AttrListPtr::get(Attrs.begin(), Attrs.end());
Duncan Sandsdc024672007-11-27 13:23:08 +00002967
Reid Spencer14310612006-12-31 05:40:51 +00002968 // Create the InvokeInst
Dan Gohman041e2eb2008-05-15 19:50:34 +00002969 InvokeInst *II = InvokeInst::Create(V, Normal, Except,
2970 Args.begin(), Args.end());
Reid Spencer14310612006-12-31 05:40:51 +00002971 II->setCallingConv($2);
Devang Patel05988662008-09-25 21:00:45 +00002972 II->setAttributes(PAL);
Reid Spencer14310612006-12-31 05:40:51 +00002973 $$ = II;
Devang Patel652203f2008-09-29 20:49:50 +00002974 delete $7;
Reid Spencer61c83e02006-08-18 08:43:06 +00002975 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002976 }
2977 | UNWIND {
2978 $$ = new UnwindInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002979 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002980 }
2981 | UNREACHABLE {
2982 $$ = new UnreachableInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002983 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002984 };
2985
2986
2987
Chris Lattnerf9078f92008-10-15 06:03:48 +00002988JumpTable : JumpTable INTTYPE ConstValueRef ',' LABEL ValueRef {
Chris Lattner58af2a12006-02-15 07:22:58 +00002989 $$ = $1;
Reid Spencer93c40032007-03-19 18:40:50 +00002990 Constant *V = cast<Constant>(getExistingVal($2, $3));
Reid Spencer5b7e7532006-09-28 19:28:24 +00002991 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002992 if (V == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002993 GEN_ERROR("May only switch on a constant pool value");
Chris Lattner58af2a12006-02-15 07:22:58 +00002994
Reid Spencer5b7e7532006-09-28 19:28:24 +00002995 BasicBlock* tmpBB = getBBVal($6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002996 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002997 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner58af2a12006-02-15 07:22:58 +00002998 }
Chris Lattnerf9078f92008-10-15 06:03:48 +00002999 | INTTYPE ConstValueRef ',' LABEL ValueRef {
Chris Lattner58af2a12006-02-15 07:22:58 +00003000 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Reid Spencer93c40032007-03-19 18:40:50 +00003001 Constant *V = cast<Constant>(getExistingVal($1, $2));
Reid Spencer5b7e7532006-09-28 19:28:24 +00003002 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003003
3004 if (V == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00003005 GEN_ERROR("May only switch on a constant pool value");
Chris Lattner58af2a12006-02-15 07:22:58 +00003006
Reid Spencer5b7e7532006-09-28 19:28:24 +00003007 BasicBlock* tmpBB = getBBVal($5);
Reid Spencer61c83e02006-08-18 08:43:06 +00003008 CHECK_FOR_ERROR
Eric Christopher2a5196f2008-09-24 04:55:49 +00003009 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner58af2a12006-02-15 07:22:58 +00003010 };
3011
Reid Spencer41dff5e2007-01-26 08:05:27 +00003012Inst : OptLocalAssign InstVal {
Reid Spenceref9b9a72007-02-05 20:47:22 +00003013 // Is this definition named?? if so, assign the name...
3014 setValueName($2, $1);
3015 CHECK_FOR_ERROR
3016 InsertValue($2);
3017 $$ = $2;
3018 CHECK_FOR_ERROR
3019 };
3020
Chris Lattner15bd0952008-08-29 17:20:18 +00003021Inst : LocalNumber InstVal {
3022 CHECK_FOR_ERROR
3023 int ValNum = InsertValue($2);
Eric Christopher2a5196f2008-09-24 04:55:49 +00003024
Chris Lattner15bd0952008-08-29 17:20:18 +00003025 if (ValNum != (int)$1)
3026 GEN_ERROR("Result value number %" + utostr($1) +
3027 " is incorrect, expected %" + utostr((unsigned)ValNum));
3028
3029 $$ = $2;
3030 CHECK_FOR_ERROR
3031 };
3032
Chris Lattner58af2a12006-02-15 07:22:58 +00003033
3034PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Reid Spencer14310612006-12-31 05:40:51 +00003035 if (!UpRefs.empty())
3036 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00003037 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Reid Spencera132e042006-12-03 05:46:11 +00003038 Value* tmpVal = getVal(*$1, $3);
Reid Spencer61c83e02006-08-18 08:43:06 +00003039 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00003040 BasicBlock* tmpBB = getBBVal($5);
3041 CHECK_FOR_ERROR
3042 $$->push_back(std::make_pair(tmpVal, tmpBB));
Reid Spencera132e042006-12-03 05:46:11 +00003043 delete $1;
Chris Lattner58af2a12006-02-15 07:22:58 +00003044 }
3045 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
3046 $$ = $1;
Reid Spencer5b7e7532006-09-28 19:28:24 +00003047 Value* tmpVal = getVal($1->front().first->getType(), $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00003048 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00003049 BasicBlock* tmpBB = getBBVal($6);
3050 CHECK_FOR_ERROR
3051 $1->push_back(std::make_pair(tmpVal, tmpBB));
Chris Lattner58af2a12006-02-15 07:22:58 +00003052 };
3053
3054
Devang Patel05988662008-09-25 21:00:45 +00003055ParamList : Types OptAttributes ValueRef OptAttributes {
3056 // FIXME: Remove trailing OptAttributes in LLVM 3.0, it was a mistake in 2.0
Reid Spencer14310612006-12-31 05:40:51 +00003057 if (!UpRefs.empty())
3058 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
3059 // Used for call and invoke instructions
Dale Johanneseneb57ea72007-11-05 21:20:28 +00003060 $$ = new ParamList();
Duncan Sandsdc024672007-11-27 13:23:08 +00003061 ParamListEntry E; E.Attrs = $2 | $4; E.Val = getVal($1->get(), $3);
Reid Spencer14310612006-12-31 05:40:51 +00003062 $$->push_back(E);
Reid Spencer66728ef2007-03-20 01:13:36 +00003063 delete $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00003064 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003065 }
Devang Patel05988662008-09-25 21:00:45 +00003066 | LABEL OptAttributes ValueRef OptAttributes {
3067 // FIXME: Remove trailing OptAttributes in LLVM 3.0, it was a mistake in 2.0
Dale Johanneseneb57ea72007-11-05 21:20:28 +00003068 // Labels are only valid in ASMs
3069 $$ = new ParamList();
Duncan Sandsdc024672007-11-27 13:23:08 +00003070 ParamListEntry E; E.Attrs = $2 | $4; E.Val = getBBVal($3);
Dale Johanneseneb57ea72007-11-05 21:20:28 +00003071 $$->push_back(E);
Duncan Sandsdc024672007-11-27 13:23:08 +00003072 CHECK_FOR_ERROR
Dale Johanneseneb57ea72007-11-05 21:20:28 +00003073 }
Devang Patel05988662008-09-25 21:00:45 +00003074 | ParamList ',' Types OptAttributes ValueRef OptAttributes {
3075 // FIXME: Remove trailing OptAttributes in LLVM 3.0, it was a mistake in 2.0
Reid Spencer14310612006-12-31 05:40:51 +00003076 if (!UpRefs.empty())
3077 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00003078 $$ = $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00003079 ParamListEntry E; E.Attrs = $4 | $6; E.Val = getVal($3->get(), $5);
Reid Spencer14310612006-12-31 05:40:51 +00003080 $$->push_back(E);
Reid Spencer66728ef2007-03-20 01:13:36 +00003081 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00003082 CHECK_FOR_ERROR
Reid Spencer14310612006-12-31 05:40:51 +00003083 }
Devang Patel05988662008-09-25 21:00:45 +00003084 | ParamList ',' LABEL OptAttributes ValueRef OptAttributes {
3085 // FIXME: Remove trailing OptAttributes in LLVM 3.0, it was a mistake in 2.0
Dale Johanneseneb57ea72007-11-05 21:20:28 +00003086 $$ = $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00003087 ParamListEntry E; E.Attrs = $4 | $6; E.Val = getBBVal($5);
Dale Johanneseneb57ea72007-11-05 21:20:28 +00003088 $$->push_back(E);
3089 CHECK_FOR_ERROR
3090 }
3091 | /*empty*/ { $$ = new ParamList(); };
Chris Lattner58af2a12006-02-15 07:22:58 +00003092
Reid Spencer14310612006-12-31 05:40:51 +00003093IndexList // Used for gep instructions and constant expressions
Reid Spencerc6c59fd2006-12-31 21:47:02 +00003094 : /*empty*/ { $$ = new std::vector<Value*>(); }
Reid Spencer14310612006-12-31 05:40:51 +00003095 | IndexList ',' ResolvedVal {
3096 $$ = $1;
3097 $$->push_back($3);
3098 CHECK_FOR_ERROR
3099 }
Reid Spencerc6c59fd2006-12-31 21:47:02 +00003100 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00003101
Dan Gohman81a0c0b2008-05-31 00:58:22 +00003102ConstantIndexList // Used for insertvalue and extractvalue instructions
3103 : ',' EUINT64VAL {
3104 $$ = new std::vector<unsigned>();
3105 if ((unsigned)$2 != $2)
3106 GEN_ERROR("Index " + utostr($2) + " is not valid for insertvalue or extractvalue.");
3107 $$->push_back($2);
3108 }
3109 | ConstantIndexList ',' EUINT64VAL {
3110 $$ = $1;
3111 if ((unsigned)$3 != $3)
3112 GEN_ERROR("Index " + utostr($3) + " is not valid for insertvalue or extractvalue.");
3113 $$->push_back($3);
3114 CHECK_FOR_ERROR
3115 }
3116 ;
3117
Chris Lattner58af2a12006-02-15 07:22:58 +00003118OptTailCall : TAIL CALL {
3119 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00003120 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003121 }
3122 | CALL {
3123 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00003124 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003125 };
3126
Chris Lattner58af2a12006-02-15 07:22:58 +00003127InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00003128 if (!UpRefs.empty())
3129 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Eric Christopher2a5196f2008-09-24 04:55:49 +00003130 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint() &&
Reid Spencer9d6565a2007-02-15 02:26:10 +00003131 !isa<VectorType>((*$2).get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00003132 GEN_ERROR(
Reid Spencerb5334b02007-02-05 10:18:06 +00003133 "Arithmetic operator requires integer, FP, or packed operands");
Eric Christopher2a5196f2008-09-24 04:55:49 +00003134 Value* val1 = getVal(*$2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00003135 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00003136 Value* val2 = getVal(*$2, $5);
Reid Spencer5b7e7532006-09-28 19:28:24 +00003137 CHECK_FOR_ERROR
Dan Gohmane4977cf2008-05-23 01:55:30 +00003138 $$ = BinaryOperator::Create($1, val1, val2);
Chris Lattner58af2a12006-02-15 07:22:58 +00003139 if ($$ == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00003140 GEN_ERROR("binary operator returned null");
Reid Spencera132e042006-12-03 05:46:11 +00003141 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00003142 }
3143 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00003144 if (!UpRefs.empty())
3145 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Chris Lattner42a75512007-01-15 02:27:26 +00003146 if (!(*$2)->isInteger()) {
Nate Begeman5bc1ea02008-07-29 15:49:41 +00003147 if (!isa<VectorType>($2->get()) ||
Reid Spencer9d6565a2007-02-15 02:26:10 +00003148 !cast<VectorType>($2->get())->getElementType()->isInteger())
Reid Spencerb5334b02007-02-05 10:18:06 +00003149 GEN_ERROR("Logical operator requires integral operands");
Chris Lattner58af2a12006-02-15 07:22:58 +00003150 }
Reid Spencera132e042006-12-03 05:46:11 +00003151 Value* tmpVal1 = getVal(*$2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00003152 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00003153 Value* tmpVal2 = getVal(*$2, $5);
Reid Spencer5b7e7532006-09-28 19:28:24 +00003154 CHECK_FOR_ERROR
Dan Gohmane4977cf2008-05-23 01:55:30 +00003155 $$ = BinaryOperator::Create($1, tmpVal1, tmpVal2);
Chris Lattner58af2a12006-02-15 07:22:58 +00003156 if ($$ == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00003157 GEN_ERROR("binary operator returned null");
Reid Spencera132e042006-12-03 05:46:11 +00003158 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00003159 }
Reid Spencera132e042006-12-03 05:46:11 +00003160 | ICMP IPredicates Types ValueRef ',' ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00003161 if (!UpRefs.empty())
3162 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003163 Value* tmpVal1 = getVal(*$3, $4);
3164 CHECK_FOR_ERROR
3165 Value* tmpVal2 = getVal(*$3, $6);
3166 CHECK_FOR_ERROR
Dan Gohmane4977cf2008-05-23 01:55:30 +00003167 $$ = CmpInst::Create($1, $2, tmpVal1, tmpVal2);
Reid Spencera132e042006-12-03 05:46:11 +00003168 if ($$ == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00003169 GEN_ERROR("icmp operator returned null");
Reid Spencer66728ef2007-03-20 01:13:36 +00003170 delete $3;
Reid Spencera132e042006-12-03 05:46:11 +00003171 }
3172 | FCMP FPredicates Types ValueRef ',' ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00003173 if (!UpRefs.empty())
3174 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003175 Value* tmpVal1 = getVal(*$3, $4);
3176 CHECK_FOR_ERROR
3177 Value* tmpVal2 = getVal(*$3, $6);
3178 CHECK_FOR_ERROR
Dan Gohmane4977cf2008-05-23 01:55:30 +00003179 $$ = CmpInst::Create($1, $2, tmpVal1, tmpVal2);
Reid Spencera132e042006-12-03 05:46:11 +00003180 if ($$ == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00003181 GEN_ERROR("fcmp operator returned null");
Reid Spencer66728ef2007-03-20 01:13:36 +00003182 delete $3;
Chris Lattner58af2a12006-02-15 07:22:58 +00003183 }
Nate Begemanac80ade2008-05-12 19:01:56 +00003184 | VICMP IPredicates Types ValueRef ',' ValueRef {
3185 if (!UpRefs.empty())
3186 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
3187 if (!isa<VectorType>((*$3).get()))
3188 GEN_ERROR("Scalar types not supported by vicmp instruction");
3189 Value* tmpVal1 = getVal(*$3, $4);
3190 CHECK_FOR_ERROR
3191 Value* tmpVal2 = getVal(*$3, $6);
3192 CHECK_FOR_ERROR
Dan Gohmane4977cf2008-05-23 01:55:30 +00003193 $$ = CmpInst::Create($1, $2, tmpVal1, tmpVal2);
Nate Begemanac80ade2008-05-12 19:01:56 +00003194 if ($$ == 0)
Dan Gohmand8ee59b2008-09-09 01:13:24 +00003195 GEN_ERROR("vicmp operator returned null");
Nate Begemanac80ade2008-05-12 19:01:56 +00003196 delete $3;
3197 }
3198 | VFCMP FPredicates Types ValueRef ',' ValueRef {
3199 if (!UpRefs.empty())
3200 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
3201 if (!isa<VectorType>((*$3).get()))
3202 GEN_ERROR("Scalar types not supported by vfcmp instruction");
3203 Value* tmpVal1 = getVal(*$3, $4);
3204 CHECK_FOR_ERROR
3205 Value* tmpVal2 = getVal(*$3, $6);
3206 CHECK_FOR_ERROR
Dan Gohmane4977cf2008-05-23 01:55:30 +00003207 $$ = CmpInst::Create($1, $2, tmpVal1, tmpVal2);
Nate Begemanac80ade2008-05-12 19:01:56 +00003208 if ($$ == 0)
Dan Gohmand8ee59b2008-09-09 01:13:24 +00003209 GEN_ERROR("vfcmp operator returned null");
Nate Begemanac80ade2008-05-12 19:01:56 +00003210 delete $3;
3211 }
Reid Spencer3da59db2006-11-27 01:05:10 +00003212 | CastOps ResolvedVal TO Types {
Reid Spencer14310612006-12-31 05:40:51 +00003213 if (!UpRefs.empty())
3214 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003215 Value* Val = $2;
Reid Spencerb0fcf8f2007-01-17 02:48:45 +00003216 const Type* DestTy = $4->get();
3217 if (!CastInst::castIsValid($1, Val, DestTy))
3218 GEN_ERROR("invalid cast opcode for cast from '" +
3219 Val->getType()->getDescription() + "' to '" +
Eric Christopher2a5196f2008-09-24 04:55:49 +00003220 DestTy->getDescription() + "'");
Dan Gohmane4977cf2008-05-23 01:55:30 +00003221 $$ = CastInst::Create($1, Val, DestTy);
Reid Spencera132e042006-12-03 05:46:11 +00003222 delete $4;
Chris Lattner58af2a12006-02-15 07:22:58 +00003223 }
3224 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Dan Gohmand8ee59b2008-09-09 01:13:24 +00003225 if (isa<VectorType>($2->getType())) {
3226 // vector select
3227 if (!isa<VectorType>($4->getType())
3228 || !isa<VectorType>($6->getType()) )
3229 GEN_ERROR("vector select value types must be vector types");
3230 const VectorType* cond_type = cast<VectorType>($2->getType());
3231 const VectorType* select_type = cast<VectorType>($4->getType());
3232 if (cond_type->getElementType() != Type::Int1Ty)
3233 GEN_ERROR("vector select condition element type must be boolean");
3234 if (cond_type->getNumElements() != select_type->getNumElements())
3235 GEN_ERROR("vector select number of elements must be the same");
3236 } else {
3237 if ($2->getType() != Type::Int1Ty)
3238 GEN_ERROR("select condition must be boolean");
3239 }
Reid Spencera132e042006-12-03 05:46:11 +00003240 if ($4->getType() != $6->getType())
Dan Gohmand8ee59b2008-09-09 01:13:24 +00003241 GEN_ERROR("select value types must match");
Gabor Greife64d2482008-04-06 23:07:54 +00003242 $$ = SelectInst::Create($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00003243 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003244 }
3245 | VAARG ResolvedVal ',' Types {
Reid Spencer14310612006-12-31 05:40:51 +00003246 if (!UpRefs.empty())
3247 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003248 $$ = new VAArgInst($2, *$4);
3249 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00003250 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003251 }
Chris Lattner58af2a12006-02-15 07:22:58 +00003252 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00003253 if (!ExtractElementInst::isValidOperands($2, $4))
Reid Spencerb5334b02007-02-05 10:18:06 +00003254 GEN_ERROR("Invalid extractelement operands");
Reid Spencera132e042006-12-03 05:46:11 +00003255 $$ = new ExtractElementInst($2, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00003256 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003257 }
3258 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00003259 if (!InsertElementInst::isValidOperands($2, $4, $6))
Reid Spencerb5334b02007-02-05 10:18:06 +00003260 GEN_ERROR("Invalid insertelement operands");
Gabor Greife64d2482008-04-06 23:07:54 +00003261 $$ = InsertElementInst::Create($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00003262 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003263 }
Chris Lattnerd5efe842006-04-08 01:18:56 +00003264 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00003265 if (!ShuffleVectorInst::isValidOperands($2, $4, $6))
Reid Spencerb5334b02007-02-05 10:18:06 +00003266 GEN_ERROR("Invalid shufflevector operands");
Reid Spencera132e042006-12-03 05:46:11 +00003267 $$ = new ShuffleVectorInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00003268 CHECK_FOR_ERROR
Chris Lattnerd5efe842006-04-08 01:18:56 +00003269 }
Chris Lattner58af2a12006-02-15 07:22:58 +00003270 | PHI_TOK PHIList {
3271 const Type *Ty = $2->front().first->getType();
3272 if (!Ty->isFirstClassType())
Reid Spencerb5334b02007-02-05 10:18:06 +00003273 GEN_ERROR("PHI node operands must be of first class type");
Gabor Greife64d2482008-04-06 23:07:54 +00003274 $$ = PHINode::Create(Ty);
Chris Lattner58af2a12006-02-15 07:22:58 +00003275 ((PHINode*)$$)->reserveOperandSpace($2->size());
3276 while ($2->begin() != $2->end()) {
Eric Christopher2a5196f2008-09-24 04:55:49 +00003277 if ($2->front().first->getType() != Ty)
Reid Spencerb5334b02007-02-05 10:18:06 +00003278 GEN_ERROR("All elements of a PHI node must be of the same type");
Chris Lattner58af2a12006-02-15 07:22:58 +00003279 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
3280 $2->pop_front();
3281 }
3282 delete $2; // Free the list...
Reid Spencer61c83e02006-08-18 08:43:06 +00003283 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003284 }
Devang Patel652203f2008-09-29 20:49:50 +00003285 | OptTailCall OptCallingConv OptRetAttrs ResultTypes ValueRef '(' ParamList ')'
Reid Spencer218ded22007-01-05 17:07:23 +00003286 OptFuncAttrs {
Reid Spencer14310612006-12-31 05:40:51 +00003287
3288 // Handle the short syntax
Reid Spencer3da59db2006-11-27 01:05:10 +00003289 const PointerType *PFTy = 0;
3290 const FunctionType *Ty = 0;
Devang Patel652203f2008-09-29 20:49:50 +00003291 if (!(PFTy = dyn_cast<PointerType>($4->get())) ||
Chris Lattner58af2a12006-02-15 07:22:58 +00003292 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
3293 // Pull out the types of all of the arguments...
3294 std::vector<const Type*> ParamTypes;
Devang Patel652203f2008-09-29 20:49:50 +00003295 ParamList::iterator I = $7->begin(), E = $7->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00003296 for (; I != E; ++I) {
Reid Spencer14310612006-12-31 05:40:51 +00003297 const Type *Ty = I->Val->getType();
3298 if (Ty == Type::VoidTy)
3299 GEN_ERROR("Short call syntax cannot be used with varargs");
3300 ParamTypes.push_back(Ty);
Chris Lattner58af2a12006-02-15 07:22:58 +00003301 }
Chris Lattnera925a142008-04-23 05:37:08 +00003302
Devang Patel652203f2008-09-29 20:49:50 +00003303 if (!FunctionType::isValidReturnType(*$4))
Chris Lattnera925a142008-04-23 05:37:08 +00003304 GEN_ERROR("Invalid result type for LLVM function");
3305
Devang Patel652203f2008-09-29 20:49:50 +00003306 Ty = FunctionType::get($4->get(), ParamTypes, false);
Christopher Lamb4374f8e2007-12-17 01:17:35 +00003307 PFTy = PointerType::getUnqual(Ty);
Chris Lattner58af2a12006-02-15 07:22:58 +00003308 }
Chris Lattner6cdc6822007-04-26 05:31:05 +00003309
Devang Patel652203f2008-09-29 20:49:50 +00003310 Value *V = getVal(PFTy, $5); // Get the function we're calling...
Reid Spencer5b7e7532006-09-28 19:28:24 +00003311 CHECK_FOR_ERROR
Chris Lattner6cdc6822007-04-26 05:31:05 +00003312
Reid Spencer7780acb2007-04-16 06:56:07 +00003313 // Check for call to invalid intrinsic to avoid crashing later.
3314 if (Function *theF = dyn_cast<Function>(V)) {
Reid Spencered48de22007-04-16 22:02:23 +00003315 if (theF->hasName() && (theF->getValueName()->getKeyLength() >= 5) &&
Reid Spencer36fdde12007-04-16 20:35:38 +00003316 (0 == strncmp(theF->getValueName()->getKeyData(), "llvm.", 5)) &&
3317 !theF->getIntrinsicID(true))
Reid Spencer7780acb2007-04-16 06:56:07 +00003318 GEN_ERROR("Call to invalid LLVM intrinsic function '" +
3319 theF->getName() + "'");
3320 }
3321
Devang Patel05988662008-09-25 21:00:45 +00003322 // Set up the Attributes for the function
3323 SmallVector<AttributeWithIndex, 8> Attrs;
Devang Patel19c87462008-09-26 22:53:05 +00003324 //FIXME : In 3.0, stop accepting zext, sext and inreg as optional function
3325 //attributes.
Devang Patel652203f2008-09-29 20:49:50 +00003326 Attributes RetAttrs = $3;
3327 if ($9 != Attribute::None) {
3328 if ($9 & Attribute::ZExt) {
Devang Patel19c87462008-09-26 22:53:05 +00003329 RetAttrs = RetAttrs | Attribute::ZExt;
Devang Patel652203f2008-09-29 20:49:50 +00003330 $9 = $9 ^ Attribute::ZExt;
Devang Patel19c87462008-09-26 22:53:05 +00003331 }
Devang Patel652203f2008-09-29 20:49:50 +00003332 if ($9 & Attribute::SExt) {
Devang Patel19c87462008-09-26 22:53:05 +00003333 RetAttrs = RetAttrs | Attribute::SExt;
Devang Patel652203f2008-09-29 20:49:50 +00003334 $9 = $9 ^ Attribute::SExt;
Devang Patel19c87462008-09-26 22:53:05 +00003335 }
Devang Patel652203f2008-09-29 20:49:50 +00003336 if ($9 & Attribute::InReg) {
Devang Patel19c87462008-09-26 22:53:05 +00003337 RetAttrs = RetAttrs | Attribute::InReg;
Devang Patel652203f2008-09-29 20:49:50 +00003338 $9 = $9 ^ Attribute::InReg;
Devang Patel19c87462008-09-26 22:53:05 +00003339 }
Devang Patel19c87462008-09-26 22:53:05 +00003340 }
Devang Patel652203f2008-09-29 20:49:50 +00003341 if (RetAttrs != Attribute::None)
3342 Attrs.push_back(AttributeWithIndex::get(0, RetAttrs));
Devang Patel19c87462008-09-26 22:53:05 +00003343
Eric Christopher2a5196f2008-09-24 04:55:49 +00003344 // Check the arguments
Reid Spencer14310612006-12-31 05:40:51 +00003345 ValueList Args;
Devang Patel652203f2008-09-29 20:49:50 +00003346 if ($7->empty()) { // Has no arguments?
Chris Lattner58af2a12006-02-15 07:22:58 +00003347 // Make sure no arguments is a good thing!
3348 if (Ty->getNumParams() != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00003349 GEN_ERROR("No arguments passed to a function that "
Reid Spencerb5334b02007-02-05 10:18:06 +00003350 "expects arguments");
Chris Lattner58af2a12006-02-15 07:22:58 +00003351 } else { // Has arguments?
3352 // Loop through FunctionType's arguments and ensure they are specified
Duncan Sandsdc024672007-11-27 13:23:08 +00003353 // correctly. Also, gather any parameter attributes.
Chris Lattner58af2a12006-02-15 07:22:58 +00003354 FunctionType::param_iterator I = Ty->param_begin();
3355 FunctionType::param_iterator E = Ty->param_end();
Devang Patel652203f2008-09-29 20:49:50 +00003356 ParamList::iterator ArgI = $7->begin(), ArgE = $7->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00003357 unsigned index = 1;
Chris Lattner58af2a12006-02-15 07:22:58 +00003358
Duncan Sandsdc024672007-11-27 13:23:08 +00003359 for (; ArgI != ArgE && I != E; ++ArgI, ++I, ++index) {
Reid Spencer14310612006-12-31 05:40:51 +00003360 if (ArgI->Val->getType() != *I)
3361 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00003362 (*I)->getDescription() + "'");
Reid Spencer14310612006-12-31 05:40:51 +00003363 Args.push_back(ArgI->Val);
Devang Patel05988662008-09-25 21:00:45 +00003364 if (ArgI->Attrs != Attribute::None)
3365 Attrs.push_back(AttributeWithIndex::get(index, ArgI->Attrs));
Reid Spencer14310612006-12-31 05:40:51 +00003366 }
3367 if (Ty->isVarArg()) {
3368 if (I == E)
Chris Lattner38905612008-02-19 04:36:25 +00003369 for (; ArgI != ArgE; ++ArgI, ++index) {
Reid Spencer14310612006-12-31 05:40:51 +00003370 Args.push_back(ArgI->Val); // push the remaining varargs
Devang Patel05988662008-09-25 21:00:45 +00003371 if (ArgI->Attrs != Attribute::None)
3372 Attrs.push_back(AttributeWithIndex::get(index, ArgI->Attrs));
Chris Lattner38905612008-02-19 04:36:25 +00003373 }
Reid Spencer14310612006-12-31 05:40:51 +00003374 } else if (I != E || ArgI != ArgE)
Reid Spencerb5334b02007-02-05 10:18:06 +00003375 GEN_ERROR("Invalid number of parameters detected");
Chris Lattner58af2a12006-02-15 07:22:58 +00003376 }
Devang Patel652203f2008-09-29 20:49:50 +00003377 if ($9 != Attribute::None)
3378 Attrs.push_back(AttributeWithIndex::get(~0, $9));
Duncan Sandsdc024672007-11-27 13:23:08 +00003379
Devang Patel05988662008-09-25 21:00:45 +00003380 // Finish off the Attributes and check them
3381 AttrListPtr PAL;
Duncan Sandsdc024672007-11-27 13:23:08 +00003382 if (!Attrs.empty())
Devang Patel05988662008-09-25 21:00:45 +00003383 PAL = AttrListPtr::get(Attrs.begin(), Attrs.end());
Duncan Sandsdc024672007-11-27 13:23:08 +00003384
Reid Spencer14310612006-12-31 05:40:51 +00003385 // Create the call node
Gabor Greife64d2482008-04-06 23:07:54 +00003386 CallInst *CI = CallInst::Create(V, Args.begin(), Args.end());
Reid Spencer14310612006-12-31 05:40:51 +00003387 CI->setTailCall($1);
3388 CI->setCallingConv($2);
Devang Patel05988662008-09-25 21:00:45 +00003389 CI->setAttributes(PAL);
Reid Spencer14310612006-12-31 05:40:51 +00003390 $$ = CI;
Devang Patel652203f2008-09-29 20:49:50 +00003391 delete $7;
3392 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00003393 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003394 }
3395 | MemoryInst {
3396 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00003397 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003398 };
3399
Chris Lattner58af2a12006-02-15 07:22:58 +00003400OptVolatile : VOLATILE {
3401 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00003402 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003403 }
3404 | /* empty */ {
3405 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00003406 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003407 };
3408
3409
3410
3411MemoryInst : MALLOC Types OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003412 if (!UpRefs.empty())
3413 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003414 $$ = new MallocInst(*$2, 0, $3);
3415 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00003416 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003417 }
Reid Spencera54b7cb2007-01-12 07:05:14 +00003418 | MALLOC Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003419 if (!UpRefs.empty())
3420 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Dan Gohman81a0c0b2008-05-31 00:58:22 +00003421 if ($4 != Type::Int32Ty)
3422 GEN_ERROR("Malloc array size is not a 32-bit integer!");
Reid Spencera132e042006-12-03 05:46:11 +00003423 Value* tmpVal = getVal($4, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00003424 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00003425 $$ = new MallocInst(*$2, tmpVal, $6);
3426 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00003427 }
3428 | ALLOCA Types OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003429 if (!UpRefs.empty())
3430 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003431 $$ = new AllocaInst(*$2, 0, $3);
3432 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00003433 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003434 }
Reid Spencera54b7cb2007-01-12 07:05:14 +00003435 | ALLOCA Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003436 if (!UpRefs.empty())
3437 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Dan Gohman81a0c0b2008-05-31 00:58:22 +00003438 if ($4 != Type::Int32Ty)
3439 GEN_ERROR("Alloca array size is not a 32-bit integer!");
Reid Spencera132e042006-12-03 05:46:11 +00003440 Value* tmpVal = getVal($4, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00003441 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00003442 $$ = new AllocaInst(*$2, tmpVal, $6);
3443 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00003444 }
3445 | FREE ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00003446 if (!isa<PointerType>($2->getType()))
Eric Christopher2a5196f2008-09-24 04:55:49 +00003447 GEN_ERROR("Trying to free nonpointer type " +
Reid Spencerb5334b02007-02-05 10:18:06 +00003448 $2->getType()->getDescription() + "");
Reid Spencera132e042006-12-03 05:46:11 +00003449 $$ = new FreeInst($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00003450 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003451 }
3452
Christopher Lamb5c104242007-04-22 20:09:11 +00003453 | OptVolatile LOAD Types ValueRef OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003454 if (!UpRefs.empty())
3455 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003456 if (!isa<PointerType>($3->get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00003457 GEN_ERROR("Can't load from nonpointer type: " +
Reid Spencera132e042006-12-03 05:46:11 +00003458 (*$3)->getDescription());
3459 if (!cast<PointerType>($3->get())->getElementType()->isFirstClassType())
Reid Spencer61c83e02006-08-18 08:43:06 +00003460 GEN_ERROR("Can't load from pointer of non-first-class type: " +
Reid Spencera132e042006-12-03 05:46:11 +00003461 (*$3)->getDescription());
3462 Value* tmpVal = getVal(*$3, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00003463 CHECK_FOR_ERROR
Christopher Lamb5c104242007-04-22 20:09:11 +00003464 $$ = new LoadInst(tmpVal, "", $1, $5);
Reid Spencera132e042006-12-03 05:46:11 +00003465 delete $3;
Chris Lattner58af2a12006-02-15 07:22:58 +00003466 }
Christopher Lamb5c104242007-04-22 20:09:11 +00003467 | OptVolatile STORE ResolvedVal ',' Types ValueRef OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003468 if (!UpRefs.empty())
3469 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003470 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00003471 if (!PT)
Reid Spencer61c83e02006-08-18 08:43:06 +00003472 GEN_ERROR("Can't store to a nonpointer type: " +
Reid Spencera132e042006-12-03 05:46:11 +00003473 (*$5)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00003474 const Type *ElTy = PT->getElementType();
Reid Spencera132e042006-12-03 05:46:11 +00003475 if (ElTy != $3->getType())
3476 GEN_ERROR("Can't store '" + $3->getType()->getDescription() +
Reid Spencerb5334b02007-02-05 10:18:06 +00003477 "' into space of type '" + ElTy->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00003478
Reid Spencera132e042006-12-03 05:46:11 +00003479 Value* tmpVal = getVal(*$5, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00003480 CHECK_FOR_ERROR
Christopher Lamb5c104242007-04-22 20:09:11 +00003481 $$ = new StoreInst($3, tmpVal, $1, $7);
Reid Spencera132e042006-12-03 05:46:11 +00003482 delete $5;
Chris Lattner58af2a12006-02-15 07:22:58 +00003483 }
Dan Gohmane4977cf2008-05-23 01:55:30 +00003484 | GETRESULT Types ValueRef ',' EUINT64VAL {
Dan Gohman1a570242008-07-23 00:54:54 +00003485 if (!UpRefs.empty())
3486 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
3487 if (!isa<StructType>($2->get()) && !isa<ArrayType>($2->get()))
3488 GEN_ERROR("getresult insn requires an aggregate operand");
3489 if (!ExtractValueInst::getIndexedType(*$2, $5))
3490 GEN_ERROR("Invalid getresult index for type '" +
3491 (*$2)->getDescription()+ "'");
3492
3493 Value *tmpVal = getVal(*$2, $3);
Devang Patel5a970972008-02-19 22:27:01 +00003494 CHECK_FOR_ERROR
Dan Gohman1a570242008-07-23 00:54:54 +00003495 $$ = ExtractValueInst::Create(tmpVal, $5);
3496 delete $2;
Devang Patel5a970972008-02-19 22:27:01 +00003497 }
Chris Lattner58af2a12006-02-15 07:22:58 +00003498 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencer14310612006-12-31 05:40:51 +00003499 if (!UpRefs.empty())
3500 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003501 if (!isa<PointerType>($2->get()))
Reid Spencerb5334b02007-02-05 10:18:06 +00003502 GEN_ERROR("getelementptr insn requires pointer operand");
Chris Lattner58af2a12006-02-15 07:22:58 +00003503
Dan Gohman041e2eb2008-05-15 19:50:34 +00003504 if (!GetElementPtrInst::getIndexedType(*$2, $4->begin(), $4->end()))
Reid Spencer61c83e02006-08-18 08:43:06 +00003505 GEN_ERROR("Invalid getelementptr indices for type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00003506 (*$2)->getDescription()+ "'");
Reid Spencera132e042006-12-03 05:46:11 +00003507 Value* tmpVal = getVal(*$2, $3);
Reid Spencer61c83e02006-08-18 08:43:06 +00003508 CHECK_FOR_ERROR
Gabor Greife64d2482008-04-06 23:07:54 +00003509 $$ = GetElementPtrInst::Create(tmpVal, $4->begin(), $4->end());
Eric Christopher2a5196f2008-09-24 04:55:49 +00003510 delete $2;
Reid Spencer5b7e7532006-09-28 19:28:24 +00003511 delete $4;
Dan Gohmane4977cf2008-05-23 01:55:30 +00003512 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +00003513 | EXTRACTVALUE Types ValueRef ConstantIndexList {
Dan Gohmane4977cf2008-05-23 01:55:30 +00003514 if (!UpRefs.empty())
3515 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
3516 if (!isa<StructType>($2->get()) && !isa<ArrayType>($2->get()))
3517 GEN_ERROR("extractvalue insn requires an aggregate operand");
3518
3519 if (!ExtractValueInst::getIndexedType(*$2, $4->begin(), $4->end()))
3520 GEN_ERROR("Invalid extractvalue indices for type '" +
3521 (*$2)->getDescription()+ "'");
3522 Value* tmpVal = getVal(*$2, $3);
3523 CHECK_FOR_ERROR
3524 $$ = ExtractValueInst::Create(tmpVal, $4->begin(), $4->end());
Eric Christopher2a5196f2008-09-24 04:55:49 +00003525 delete $2;
Dan Gohmane4977cf2008-05-23 01:55:30 +00003526 delete $4;
3527 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +00003528 | INSERTVALUE Types ValueRef ',' Types ValueRef ConstantIndexList {
Dan Gohmane4977cf2008-05-23 01:55:30 +00003529 if (!UpRefs.empty())
3530 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
3531 if (!isa<StructType>($2->get()) && !isa<ArrayType>($2->get()))
3532 GEN_ERROR("extractvalue insn requires an aggregate operand");
3533
3534 if (ExtractValueInst::getIndexedType(*$2, $7->begin(), $7->end()) != $5->get())
3535 GEN_ERROR("Invalid insertvalue indices for type '" +
3536 (*$2)->getDescription()+ "'");
3537 Value* aggVal = getVal(*$2, $3);
3538 Value* tmpVal = getVal(*$5, $6);
3539 CHECK_FOR_ERROR
3540 $$ = InsertValueInst::Create(aggVal, tmpVal, $7->begin(), $7->end());
Eric Christopher2a5196f2008-09-24 04:55:49 +00003541 delete $2;
Dan Gohmane4977cf2008-05-23 01:55:30 +00003542 delete $5;
3543 delete $7;
Chris Lattner58af2a12006-02-15 07:22:58 +00003544 };
3545
3546
3547%%
Reid Spencer61c83e02006-08-18 08:43:06 +00003548
Reid Spencer14310612006-12-31 05:40:51 +00003549// common code from the two 'RunVMAsmParser' functions
3550static Module* RunParser(Module * M) {
Reid Spencer14310612006-12-31 05:40:51 +00003551 CurModule.CurrentModule = M;
Reid Spencer14310612006-12-31 05:40:51 +00003552 // Check to make sure the parser succeeded
3553 if (yyparse()) {
3554 if (ParserResult)
3555 delete ParserResult;
3556 return 0;
3557 }
3558
Reid Spencer0d60b5a2007-03-30 01:37:39 +00003559 // Emit an error if there are any unresolved types left.
3560 if (!CurModule.LateResolveTypes.empty()) {
3561 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
3562 if (DID.Type == ValID::LocalName) {
3563 GenerateError("Undefined type remains at eof: '"+DID.getName() + "'");
3564 } else {
3565 GenerateError("Undefined type remains at eof: #" + itostr(DID.Num));
3566 }
3567 if (ParserResult)
3568 delete ParserResult;
3569 return 0;
3570 }
3571
3572 // Emit an error if there are any unresolved values left.
3573 if (!CurModule.LateResolveValues.empty()) {
3574 Value *V = CurModule.LateResolveValues.back();
3575 std::map<Value*, std::pair<ValID, int> >::iterator I =
3576 CurModule.PlaceHolderInfo.find(V);
3577
3578 if (I != CurModule.PlaceHolderInfo.end()) {
3579 ValID &DID = I->second.first;
3580 if (DID.Type == ValID::LocalName) {
3581 GenerateError("Undefined value remains at eof: "+DID.getName() + "'");
3582 } else {
3583 GenerateError("Undefined value remains at eof: #" + itostr(DID.Num));
3584 }
3585 if (ParserResult)
3586 delete ParserResult;
3587 return 0;
3588 }
3589 }
3590
Reid Spencer14310612006-12-31 05:40:51 +00003591 // Check to make sure that parsing produced a result
3592 if (!ParserResult)
3593 return 0;
3594
3595 // Reset ParserResult variable while saving its value for the result.
3596 Module *Result = ParserResult;
3597 ParserResult = 0;
3598
3599 return Result;
3600}
3601
Reid Spencer61c83e02006-08-18 08:43:06 +00003602void llvm::GenerateError(const std::string &message, int LineNo) {
Duncan Sandsdc024672007-11-27 13:23:08 +00003603 if (LineNo == -1) LineNo = LLLgetLineNo();
Reid Spencer61c83e02006-08-18 08:43:06 +00003604 // TODO: column number in exception
3605 if (TheParseError)
Duncan Sandsdc024672007-11-27 13:23:08 +00003606 TheParseError->setError(LLLgetFilename(), message, LineNo);
Reid Spencer61c83e02006-08-18 08:43:06 +00003607 TriggerError = 1;
3608}
3609
Chris Lattner58af2a12006-02-15 07:22:58 +00003610int yyerror(const char *ErrorMsg) {
Duncan Sandsdc024672007-11-27 13:23:08 +00003611 std::string where = LLLgetFilename() + ":" + utostr(LLLgetLineNo()) + ": ";
Reid Spenceref9b9a72007-02-05 20:47:22 +00003612 std::string errMsg = where + "error: " + std::string(ErrorMsg);
Duncan Sandsdc024672007-11-27 13:23:08 +00003613 if (yychar != YYEMPTY && yychar != 0) {
3614 errMsg += " while reading token: '";
Eric Christopher2a5196f2008-09-24 04:55:49 +00003615 errMsg += std::string(LLLgetTokenStart(),
Duncan Sandsdc024672007-11-27 13:23:08 +00003616 LLLgetTokenStart()+LLLgetTokenLength()) + "'";
3617 }
Reid Spencer61c83e02006-08-18 08:43:06 +00003618 GenerateError(errMsg);
Chris Lattner58af2a12006-02-15 07:22:58 +00003619 return 0;
3620}