blob: 0e36286ab42391e73d05d406601dee2c0aebf641 [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);
Chris Lattner58af2a12006-02-15 07:22:58 +0000310 if (I != CurModule.LateResolveTypes.end())
Reid Spencer861d9d62006-11-28 07:29:44 +0000311 return I->second;
Chris Lattner58af2a12006-02-15 07:22:58 +0000312
Reid Spencer861d9d62006-11-28 07:29:44 +0000313 Type *Typ = OpaqueType::get();
314 CurModule.LateResolveTypes.insert(std::make_pair(D, Typ));
315 return Typ;
Reid Spencera132e042006-12-03 05:46:11 +0000316 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000317
Reid Spencer93c40032007-03-19 18:40:50 +0000318// getExistingVal - Look up the value specified by the provided type and
Chris Lattner58af2a12006-02-15 07:22:58 +0000319// the provided ValID. If the value exists and has already been defined, return
320// it. Otherwise return null.
321//
Reid Spencer93c40032007-03-19 18:40:50 +0000322static Value *getExistingVal(const Type *Ty, const ValID &D) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000323 if (isa<FunctionType>(Ty)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000324 GenerateError("Functions are not values and "
Chris Lattner58af2a12006-02-15 07:22:58 +0000325 "must be referenced as pointers");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000326 return 0;
327 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000328
329 switch (D.Type) {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000330 case ValID::LocalID: { // Is it a numbered definition?
Reid Spencer41dff5e2007-01-26 08:05:27 +0000331 // Check that the number is within bounds.
Eric Christopher2a5196f2008-09-24 04:55:49 +0000332 if (D.Num >= CurFun.Values.size())
Reid Spencer93c40032007-03-19 18:40:50 +0000333 return 0;
334 Value *Result = CurFun.Values[D.Num];
335 if (Ty != Result->getType()) {
336 GenerateError("Numbered value (%" + utostr(D.Num) + ") of type '" +
Eric Christopher2a5196f2008-09-24 04:55:49 +0000337 Result->getType()->getDescription() + "' does not match "
Reid Spencer93c40032007-03-19 18:40:50 +0000338 "expected type, '" + Ty->getDescription() + "'");
339 return 0;
340 }
341 return Result;
Reid Spencer41dff5e2007-01-26 08:05:27 +0000342 }
343 case ValID::GlobalID: { // Is it a numbered definition?
Eric Christopher2a5196f2008-09-24 04:55:49 +0000344 if (D.Num >= CurModule.Values.size())
Reid Spenceref9b9a72007-02-05 20:47:22 +0000345 return 0;
Reid Spencer93c40032007-03-19 18:40:50 +0000346 Value *Result = CurModule.Values[D.Num];
347 if (Ty != Result->getType()) {
348 GenerateError("Numbered value (@" + utostr(D.Num) + ") of type '" +
Eric Christopher2a5196f2008-09-24 04:55:49 +0000349 Result->getType()->getDescription() + "' does not match "
Reid Spencer93c40032007-03-19 18:40:50 +0000350 "expected type, '" + Ty->getDescription() + "'");
Reid Spenceref9b9a72007-02-05 20:47:22 +0000351 return 0;
Reid Spencer93c40032007-03-19 18:40:50 +0000352 }
353 return Result;
Chris Lattner58af2a12006-02-15 07:22:58 +0000354 }
Eric Christopher2a5196f2008-09-24 04:55:49 +0000355
Reid Spencer41dff5e2007-01-26 08:05:27 +0000356 case ValID::LocalName: { // Is it a named definition?
Eric Christopher2a5196f2008-09-24 04:55:49 +0000357 if (!inFunctionScope())
Reid Spenceref9b9a72007-02-05 20:47:22 +0000358 return 0;
359 ValueSymbolTable &SymTab = CurFun.CurrentFunction->getValueSymbolTable();
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000360 Value *N = SymTab.lookup(D.getName());
Eric Christopher2a5196f2008-09-24 04:55:49 +0000361 if (N == 0)
Reid Spenceref9b9a72007-02-05 20:47:22 +0000362 return 0;
363 if (N->getType() != Ty)
364 return 0;
Eric Christopher2a5196f2008-09-24 04:55:49 +0000365
Reid Spencer41dff5e2007-01-26 08:05:27 +0000366 D.destroy(); // Free old strdup'd memory...
367 return N;
368 }
369 case ValID::GlobalName: { // Is it a named definition?
Reid Spenceref9b9a72007-02-05 20:47:22 +0000370 ValueSymbolTable &SymTab = CurModule.CurrentModule->getValueSymbolTable();
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000371 Value *N = SymTab.lookup(D.getName());
Eric Christopher2a5196f2008-09-24 04:55:49 +0000372 if (N == 0)
Reid Spenceref9b9a72007-02-05 20:47:22 +0000373 return 0;
374 if (N->getType() != Ty)
375 return 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000376
377 D.destroy(); // Free old strdup'd memory...
378 return N;
379 }
380
381 // Check to make sure that "Ty" is an integral type, and that our
382 // value will fit into the specified type...
383 case ValID::ConstSIntVal: // Is it a constant pool reference??
Chris Lattner38905612008-02-19 04:36:25 +0000384 if (!isa<IntegerType>(Ty) ||
385 !ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000386 GenerateError("Signed integral constant '" +
Chris Lattner58af2a12006-02-15 07:22:58 +0000387 itostr(D.ConstPool64) + "' is invalid for type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +0000388 Ty->getDescription() + "'");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000389 return 0;
390 }
Reid Spencer49d273e2007-03-19 20:40:51 +0000391 return ConstantInt::get(Ty, D.ConstPool64, true);
Chris Lattner58af2a12006-02-15 07:22:58 +0000392
393 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattner38905612008-02-19 04:36:25 +0000394 if (isa<IntegerType>(Ty) &&
395 ConstantInt::isValueValidForType(Ty, D.UConstPool64))
Reid Spencerb83eb642006-10-20 07:07:24 +0000396 return ConstantInt::get(Ty, D.UConstPool64);
Chris Lattner38905612008-02-19 04:36:25 +0000397
398 if (!isa<IntegerType>(Ty) ||
399 !ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
400 GenerateError("Integral constant '" + utostr(D.UConstPool64) +
401 "' is invalid or out of range for type '" +
402 Ty->getDescription() + "'");
403 return 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000404 }
Chris Lattner38905612008-02-19 04:36:25 +0000405 // This is really a signed reference. Transmogrify.
406 return ConstantInt::get(Ty, D.ConstPool64, true);
Chris Lattner58af2a12006-02-15 07:22:58 +0000407
Chris Lattner1913b942008-07-11 00:30:39 +0000408 case ValID::ConstAPInt: // Is it an unsigned const pool reference?
409 if (!isa<IntegerType>(Ty)) {
410 GenerateError("Integral constant '" + D.getName() +
411 "' is invalid or out of range for type '" +
412 Ty->getDescription() + "'");
413 return 0;
414 }
Eric Christopher2a5196f2008-09-24 04:55:49 +0000415
Chris Lattner1913b942008-07-11 00:30:39 +0000416 {
417 APSInt Tmp = *D.ConstPoolInt;
418 Tmp.extOrTrunc(Ty->getPrimitiveSizeInBits());
419 return ConstantInt::get(Tmp);
420 }
Eric Christopher2a5196f2008-09-24 04:55:49 +0000421
Chris Lattner58af2a12006-02-15 07:22:58 +0000422 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattner38905612008-02-19 04:36:25 +0000423 if (!Ty->isFloatingPoint() ||
424 !ConstantFP::isValueValidForType(Ty, *D.ConstPoolFP)) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000425 GenerateError("FP constant invalid for type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000426 return 0;
427 }
Eric Christopher2a5196f2008-09-24 04:55:49 +0000428 // Lexer has no type info, so builds all float and double FP constants
Dale Johannesenc72cd7e2007-09-11 18:33:39 +0000429 // as double. Fix this here. Long double does not need this.
430 if (&D.ConstPoolFP->getSemantics() == &APFloat::IEEEdouble &&
Dale Johannesen236bbd42008-10-09 23:01:34 +0000431 Ty==Type::FloatTy) {
432 bool ignored;
433 D.ConstPoolFP->convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven,
434 &ignored);
435 }
Chris Lattnerd8eb63f2008-04-20 00:41:19 +0000436 return ConstantFP::get(*D.ConstPoolFP);
Chris Lattner58af2a12006-02-15 07:22:58 +0000437
438 case ValID::ConstNullVal: // Is it a null value?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000439 if (!isa<PointerType>(Ty)) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000440 GenerateError("Cannot create a a non pointer null");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000441 return 0;
442 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000443 return ConstantPointerNull::get(cast<PointerType>(Ty));
444
445 case ValID::ConstUndefVal: // Is it an undef value?
446 return UndefValue::get(Ty);
447
448 case ValID::ConstZeroVal: // Is it a zero value?
449 return Constant::getNullValue(Ty);
Eric Christopher2a5196f2008-09-24 04:55:49 +0000450
Chris Lattner58af2a12006-02-15 07:22:58 +0000451 case ValID::ConstantVal: // Fully resolved constant?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000452 if (D.ConstantValue->getType() != Ty) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000453 GenerateError("Constant expression type different from required type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000454 return 0;
455 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000456 return D.ConstantValue;
457
458 case ValID::InlineAsmVal: { // Inline asm expression
459 const PointerType *PTy = dyn_cast<PointerType>(Ty);
460 const FunctionType *FTy =
461 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
Reid Spencer5b7e7532006-09-28 19:28:24 +0000462 if (!FTy || !InlineAsm::Verify(FTy, D.IAD->Constraints)) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000463 GenerateError("Invalid type for asm constraint string");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000464 return 0;
465 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000466 InlineAsm *IA = InlineAsm::get(FTy, D.IAD->AsmString, D.IAD->Constraints,
467 D.IAD->HasSideEffects);
468 D.destroy(); // Free InlineAsmDescriptor.
469 return IA;
470 }
471 default:
Reid Spencera9720f52007-02-05 17:04:00 +0000472 assert(0 && "Unhandled case!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000473 return 0;
474 } // End of switch
475
Reid Spencera9720f52007-02-05 17:04:00 +0000476 assert(0 && "Unhandled case!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000477 return 0;
478}
479
Reid Spencer93c40032007-03-19 18:40:50 +0000480// getVal - This function is identical to getExistingVal, except that if a
Chris Lattner58af2a12006-02-15 07:22:58 +0000481// value is not already defined, it "improvises" by creating a placeholder var
482// that looks and acts just like the requested variable. When the value is
483// defined later, all uses of the placeholder variable are replaced with the
484// real thing.
485//
486static Value *getVal(const Type *Ty, const ValID &ID) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000487 if (Ty == Type::LabelTy) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000488 GenerateError("Cannot use a basic block here");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000489 return 0;
490 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000491
492 // See if the value has already been defined.
Reid Spencer93c40032007-03-19 18:40:50 +0000493 Value *V = getExistingVal(Ty, ID);
Chris Lattner58af2a12006-02-15 07:22:58 +0000494 if (V) return V;
Reid Spencer5b7e7532006-09-28 19:28:24 +0000495 if (TriggerError) return 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000496
Reid Spencer5b7e7532006-09-28 19:28:24 +0000497 if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty)) {
Dan Gohmane4977cf2008-05-23 01:55:30 +0000498 GenerateError("Invalid use of a non-first-class type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000499 return 0;
500 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000501
502 // If we reached here, we referenced either a symbol that we don't know about
503 // or an id number that hasn't been read yet. We may be referencing something
504 // forward, so just create an entry to be resolved later and get to it...
505 //
Anton Korobeynikov38e09802007-04-28 13:48:45 +0000506 switch (ID.Type) {
507 case ValID::GlobalName:
Reid Spencer9c9b63a2007-04-28 16:07:31 +0000508 case ValID::GlobalID: {
Anton Korobeynikov38e09802007-04-28 13:48:45 +0000509 const PointerType *PTy = dyn_cast<PointerType>(Ty);
510 if (!PTy) {
511 GenerateError("Invalid type for reference to global" );
512 return 0;
513 }
514 const Type* ElTy = PTy->getElementType();
515 if (const FunctionType *FTy = dyn_cast<FunctionType>(ElTy))
Gabor Greife64d2482008-04-06 23:07:54 +0000516 V = Function::Create(FTy, GlobalValue::ExternalLinkage);
Anton Korobeynikov38e09802007-04-28 13:48:45 +0000517 else
Christopher Lamba8ed9bf2007-12-11 09:02:08 +0000518 V = new GlobalVariable(ElTy, false, GlobalValue::ExternalLinkage, 0, "",
519 (Module*)0, false, PTy->getAddressSpace());
Anton Korobeynikov38e09802007-04-28 13:48:45 +0000520 break;
Reid Spencer9c9b63a2007-04-28 16:07:31 +0000521 }
Anton Korobeynikov38e09802007-04-28 13:48:45 +0000522 default:
523 V = new Argument(Ty);
524 }
Eric Christopher2a5196f2008-09-24 04:55:49 +0000525
Chris Lattner58af2a12006-02-15 07:22:58 +0000526 // Remember where this forward reference came from. FIXME, shouldn't we try
527 // to recycle these things??
528 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
Duncan Sandsdc024672007-11-27 13:23:08 +0000529 LLLgetLineNo())));
Chris Lattner58af2a12006-02-15 07:22:58 +0000530
531 if (inFunctionScope())
532 InsertValue(V, CurFun.LateResolveValues);
533 else
534 InsertValue(V, CurModule.LateResolveValues);
535 return V;
536}
537
Reid Spencer93c40032007-03-19 18:40:50 +0000538/// defineBBVal - This is a definition of a new basic block with the specified
539/// identifier which must be the same as CurFun.NextValNum, if its numeric.
Nick Lewycky280a6e62008-04-25 16:53:59 +0000540static BasicBlock *defineBBVal(const ValID &ID) {
Reid Spencera9720f52007-02-05 17:04:00 +0000541 assert(inFunctionScope() && "Can't get basic block at global scope!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000542
Chris Lattner58af2a12006-02-15 07:22:58 +0000543 BasicBlock *BB = 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000544
Reid Spencer93c40032007-03-19 18:40:50 +0000545 // First, see if this was forward referenced
Chris Lattner58af2a12006-02-15 07:22:58 +0000546
Reid Spencer93c40032007-03-19 18:40:50 +0000547 std::map<ValID, BasicBlock*>::iterator BBI = CurFun.BBForwardRefs.find(ID);
548 if (BBI != CurFun.BBForwardRefs.end()) {
549 BB = BBI->second;
Chris Lattner58af2a12006-02-15 07:22:58 +0000550 // The forward declaration could have been inserted anywhere in the
551 // function: insert it into the correct place now.
552 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
553 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
Reid Spencer93c40032007-03-19 18:40:50 +0000554
Reid Spencer66728ef2007-03-20 01:13:36 +0000555 // We're about to erase the entry, save the key so we can clean it up.
556 ValID Tmp = BBI->first;
557
Reid Spencer93c40032007-03-19 18:40:50 +0000558 // Erase the forward ref from the map as its no longer "forward"
559 CurFun.BBForwardRefs.erase(ID);
560
Eric Christopher2a5196f2008-09-24 04:55:49 +0000561 // The key has been removed from the map but so we don't want to leave
Reid Spencer66728ef2007-03-20 01:13:36 +0000562 // strdup'd memory around so destroy it too.
563 Tmp.destroy();
564
Reid Spencer93c40032007-03-19 18:40:50 +0000565 // If its a numbered definition, bump the number and set the BB value.
566 if (ID.Type == ValID::LocalID) {
567 assert(ID.Num == CurFun.NextValNum && "Invalid new block number");
568 InsertValue(BB);
569 }
Eric Christopher2a5196f2008-09-24 04:55:49 +0000570 } else {
571 // We haven't seen this BB before and its first mention is a definition.
Devang Patel67909432008-03-03 18:58:47 +0000572 // Just create it and return it.
573 std::string Name (ID.Type == ValID::LocalName ? ID.getName() : "");
Gabor Greife64d2482008-04-06 23:07:54 +0000574 BB = BasicBlock::Create(Name, CurFun.CurrentFunction);
Devang Patel67909432008-03-03 18:58:47 +0000575 if (ID.Type == ValID::LocalID) {
576 assert(ID.Num == CurFun.NextValNum && "Invalid new block number");
577 InsertValue(BB);
578 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000579 }
Reid Spencer93c40032007-03-19 18:40:50 +0000580
Devang Patel67909432008-03-03 18:58:47 +0000581 ID.destroy();
Reid Spencer93c40032007-03-19 18:40:50 +0000582 return BB;
583}
584
585/// getBBVal - get an existing BB value or create a forward reference for it.
Eric Christopher2a5196f2008-09-24 04:55:49 +0000586///
Reid Spencer93c40032007-03-19 18:40:50 +0000587static BasicBlock *getBBVal(const ValID &ID) {
588 assert(inFunctionScope() && "Can't get basic block at global scope!");
589
590 BasicBlock *BB = 0;
591
592 std::map<ValID, BasicBlock*>::iterator BBI = CurFun.BBForwardRefs.find(ID);
593 if (BBI != CurFun.BBForwardRefs.end()) {
594 BB = BBI->second;
595 } if (ID.Type == ValID::LocalName) {
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000596 std::string Name = ID.getName();
Reid Spencer93c40032007-03-19 18:40:50 +0000597 Value *N = CurFun.CurrentFunction->getValueSymbolTable().lookup(Name);
Anton Korobeynikov4aefd6b2008-02-20 12:07:57 +0000598 if (N) {
Reid Spencer93c40032007-03-19 18:40:50 +0000599 if (N->getType()->getTypeID() == Type::LabelTyID)
600 BB = cast<BasicBlock>(N);
601 else
602 GenerateError("Reference to label '" + Name + "' is actually of type '"+
603 N->getType()->getDescription() + "'");
Anton Korobeynikov4aefd6b2008-02-20 12:07:57 +0000604 }
Reid Spencer93c40032007-03-19 18:40:50 +0000605 } else if (ID.Type == ValID::LocalID) {
606 if (ID.Num < CurFun.NextValNum && ID.Num < CurFun.Values.size()) {
607 if (CurFun.Values[ID.Num]->getType()->getTypeID() == Type::LabelTyID)
608 BB = cast<BasicBlock>(CurFun.Values[ID.Num]);
609 else
Eric Christopher2a5196f2008-09-24 04:55:49 +0000610 GenerateError("Reference to label '%" + utostr(ID.Num) +
611 "' is actually of type '"+
Reid Spencer93c40032007-03-19 18:40:50 +0000612 CurFun.Values[ID.Num]->getType()->getDescription() + "'");
613 }
614 } else {
615 GenerateError("Illegal label reference " + ID.getName());
616 return 0;
617 }
618
619 // If its already been defined, return it now.
620 if (BB) {
621 ID.destroy(); // Free strdup'd memory.
622 return BB;
623 }
624
625 // Otherwise, this block has not been seen before, create it.
626 std::string Name;
627 if (ID.Type == ValID::LocalName)
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000628 Name = ID.getName();
Gabor Greife64d2482008-04-06 23:07:54 +0000629 BB = BasicBlock::Create(Name, CurFun.CurrentFunction);
Reid Spencer93c40032007-03-19 18:40:50 +0000630
631 // Insert it in the forward refs map.
632 CurFun.BBForwardRefs[ID] = BB;
633
Chris Lattner58af2a12006-02-15 07:22:58 +0000634 return BB;
635}
636
637
638//===----------------------------------------------------------------------===//
639// Code to handle forward references in instructions
640//===----------------------------------------------------------------------===//
641//
642// This code handles the late binding needed with statements that reference
643// values not defined yet... for example, a forward branch, or the PHI node for
644// a loop body.
645//
646// This keeps a table (CurFun.LateResolveValues) of all such forward references
647// and back patchs after we are done.
648//
649
650// ResolveDefinitions - If we could not resolve some defs at parsing
651// time (forward branches, phi functions for loops, etc...) resolve the
652// defs now...
653//
Eric Christopher2a5196f2008-09-24 04:55:49 +0000654static void
Reid Spencer93c40032007-03-19 18:40:50 +0000655ResolveDefinitions(ValueList &LateResolvers, ValueList *FutureLateResolvers) {
Chris Lattner58af2a12006-02-15 07:22:58 +0000656 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Reid Spencer93c40032007-03-19 18:40:50 +0000657 while (!LateResolvers.empty()) {
658 Value *V = LateResolvers.back();
659 LateResolvers.pop_back();
Chris Lattner58af2a12006-02-15 07:22:58 +0000660
Reid Spencer93c40032007-03-19 18:40:50 +0000661 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
662 CurModule.PlaceHolderInfo.find(V);
663 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000664
Reid Spencer93c40032007-03-19 18:40:50 +0000665 ValID &DID = PHI->second.first;
Chris Lattner58af2a12006-02-15 07:22:58 +0000666
Reid Spencer93c40032007-03-19 18:40:50 +0000667 Value *TheRealValue = getExistingVal(V->getType(), DID);
668 if (TriggerError)
669 return;
670 if (TheRealValue) {
671 V->replaceAllUsesWith(TheRealValue);
672 delete V;
673 CurModule.PlaceHolderInfo.erase(PHI);
674 } else if (FutureLateResolvers) {
675 // Functions have their unresolved items forwarded to the module late
676 // resolver table
677 InsertValue(V, *FutureLateResolvers);
678 } else {
679 if (DID.Type == ValID::LocalName || DID.Type == ValID::GlobalName) {
680 GenerateError("Reference to an invalid definition: '" +DID.getName()+
681 "' of type '" + V->getType()->getDescription() + "'",
682 PHI->second.second);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000683 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000684 } else {
Reid Spencer93c40032007-03-19 18:40:50 +0000685 GenerateError("Reference to an invalid definition: #" +
686 itostr(DID.Num) + " of type '" +
687 V->getType()->getDescription() + "'",
688 PHI->second.second);
689 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000690 }
691 }
692 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000693 LateResolvers.clear();
694}
695
696// ResolveTypeTo - A brand new type was just declared. This means that (if
697// name is not null) things referencing Name can be resolved. Otherwise, things
698// refering to the number can be resolved. Do this now.
699//
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000700static void ResolveTypeTo(std::string *Name, const Type *ToTy) {
Chris Lattner58af2a12006-02-15 07:22:58 +0000701 ValID D;
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000702 if (Name)
703 D = ValID::createLocalName(*Name);
Eric Christopher2a5196f2008-09-24 04:55:49 +0000704 else
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000705 D = ValID::createLocalID(CurModule.Types.size());
Chris Lattner58af2a12006-02-15 07:22:58 +0000706
Reid Spencer861d9d62006-11-28 07:29:44 +0000707 std::map<ValID, PATypeHolder>::iterator I =
Chris Lattner58af2a12006-02-15 07:22:58 +0000708 CurModule.LateResolveTypes.find(D);
709 if (I != CurModule.LateResolveTypes.end()) {
Reid Spencer861d9d62006-11-28 07:29:44 +0000710 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattner58af2a12006-02-15 07:22:58 +0000711 CurModule.LateResolveTypes.erase(I);
712 }
Nuno Lopes191dfb92008-10-03 15:52:39 +0000713 D.destroy();
Chris Lattner58af2a12006-02-15 07:22:58 +0000714}
715
716// setValueName - Set the specified value to the name given. The name may be
717// null potentially, in which case this is a noop. The string passed in is
718// assumed to be a malloc'd string buffer, and is free'd by this function.
719//
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000720static void setValueName(Value *V, std::string *NameStr) {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000721 if (!NameStr) return;
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000722 std::string Name(*NameStr); // Copy string
723 delete NameStr; // Free old string
Chris Lattner58af2a12006-02-15 07:22:58 +0000724
Reid Spencer41dff5e2007-01-26 08:05:27 +0000725 if (V->getType() == Type::VoidTy) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000726 GenerateError("Can't assign name '" + Name+"' to value with void type");
Reid Spencer41dff5e2007-01-26 08:05:27 +0000727 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000728 }
Reid Spencer41dff5e2007-01-26 08:05:27 +0000729
Reid Spencera9720f52007-02-05 17:04:00 +0000730 assert(inFunctionScope() && "Must be in function scope!");
Reid Spenceref9b9a72007-02-05 20:47:22 +0000731 ValueSymbolTable &ST = CurFun.CurrentFunction->getValueSymbolTable();
732 if (ST.lookup(Name)) {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000733 GenerateError("Redefinition of value '" + Name + "' of type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +0000734 V->getType()->getDescription() + "'");
Reid Spencer41dff5e2007-01-26 08:05:27 +0000735 return;
736 }
737
738 // Set the name.
739 V->setName(Name);
Chris Lattner58af2a12006-02-15 07:22:58 +0000740}
741
742/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
743/// this is a declaration, otherwise it is a definition.
744static GlobalVariable *
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000745ParseGlobalVariable(std::string *NameStr,
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000746 GlobalValue::LinkageTypes Linkage,
747 GlobalValue::VisibilityTypes Visibility,
Chris Lattner58af2a12006-02-15 07:22:58 +0000748 bool isConstantGlobal, const Type *Ty,
Christopher Lamba8ed9bf2007-12-11 09:02:08 +0000749 Constant *Initializer, bool IsThreadLocal,
750 unsigned AddressSpace = 0) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000751 if (isa<FunctionType>(Ty)) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000752 GenerateError("Cannot declare global vars of function type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000753 return 0;
754 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +0000755 if (Ty == Type::LabelTy) {
756 GenerateError("Cannot declare global vars of label type");
757 return 0;
758 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000759
Christopher Lamba8ed9bf2007-12-11 09:02:08 +0000760 const PointerType *PTy = PointerType::get(Ty, AddressSpace);
Chris Lattner58af2a12006-02-15 07:22:58 +0000761
762 std::string Name;
763 if (NameStr) {
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000764 Name = *NameStr; // Copy string
765 delete NameStr; // Free old string
Chris Lattner58af2a12006-02-15 07:22:58 +0000766 }
767
768 // See if this global value was forward referenced. If so, recycle the
769 // object.
770 ValID ID;
771 if (!Name.empty()) {
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000772 ID = ValID::createGlobalName(Name);
Chris Lattner58af2a12006-02-15 07:22:58 +0000773 } else {
Reid Spencer93c40032007-03-19 18:40:50 +0000774 ID = ValID::createGlobalID(CurModule.Values.size());
Chris Lattner58af2a12006-02-15 07:22:58 +0000775 }
776
777 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
778 // Move the global to the end of the list, from whereever it was
779 // previously inserted.
780 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
781 CurModule.CurrentModule->getGlobalList().remove(GV);
782 CurModule.CurrentModule->getGlobalList().push_back(GV);
783 GV->setInitializer(Initializer);
784 GV->setLinkage(Linkage);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000785 GV->setVisibility(Visibility);
Chris Lattner58af2a12006-02-15 07:22:58 +0000786 GV->setConstant(isConstantGlobal);
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +0000787 GV->setThreadLocal(IsThreadLocal);
Chris Lattner58af2a12006-02-15 07:22:58 +0000788 InsertValue(GV, CurModule.Values);
Nuno Lopes191dfb92008-10-03 15:52:39 +0000789 ID.destroy();
Chris Lattner58af2a12006-02-15 07:22:58 +0000790 return GV;
791 }
792
Nuno Lopes191dfb92008-10-03 15:52:39 +0000793 ID.destroy();
794
Reid Spenceref9b9a72007-02-05 20:47:22 +0000795 // If this global has a name
Chris Lattner58af2a12006-02-15 07:22:58 +0000796 if (!Name.empty()) {
Reid Spenceref9b9a72007-02-05 20:47:22 +0000797 // if the global we're parsing has an initializer (is a definition) and
798 // has external linkage.
799 if (Initializer && Linkage != GlobalValue::InternalLinkage)
800 // If there is already a global with external linkage with this name
801 if (CurModule.CurrentModule->getGlobalVariable(Name, false)) {
802 // If we allow this GVar to get created, it will be renamed in the
803 // symbol table because it conflicts with an existing GVar. We can't
804 // allow redefinition of GVars whose linking indicates that their name
805 // must stay the same. Issue the error.
806 GenerateError("Redefinition of global variable named '" + Name +
807 "' of type '" + Ty->getDescription() + "'");
808 return 0;
809 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000810 }
811
812 // Otherwise there is no existing GV to use, create one now.
813 GlobalVariable *GV =
814 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
Christopher Lamba8ed9bf2007-12-11 09:02:08 +0000815 CurModule.CurrentModule, IsThreadLocal, AddressSpace);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000816 GV->setVisibility(Visibility);
Chris Lattner58af2a12006-02-15 07:22:58 +0000817 InsertValue(GV, CurModule.Values);
818 return GV;
819}
820
821// setTypeName - Set the specified type to the name given. The name may be
822// null potentially, in which case this is a noop. The string passed in is
823// assumed to be a malloc'd string buffer, and is freed by this function.
824//
825// This function returns true if the type has already been defined, but is
826// allowed to be redefined in the specified context. If the name is a new name
827// for the type plane, it is inserted and false is returned.
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000828static bool setTypeName(const Type *T, std::string *NameStr) {
Reid Spencera9720f52007-02-05 17:04:00 +0000829 assert(!inFunctionScope() && "Can't give types function-local names!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000830 if (NameStr == 0) return false;
Eric Christopher2a5196f2008-09-24 04:55:49 +0000831
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000832 std::string Name(*NameStr); // Copy string
833 delete NameStr; // Free old string
Chris Lattner58af2a12006-02-15 07:22:58 +0000834
835 // We don't allow assigning names to void type
Reid Spencer5b7e7532006-09-28 19:28:24 +0000836 if (T == Type::VoidTy) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000837 GenerateError("Can't assign name '" + Name + "' to the void type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000838 return false;
839 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000840
841 // Set the type name, checking for conflicts as we do so.
842 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
843
844 if (AlreadyExists) { // Inserting a name that is already defined???
845 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
Reid Spencera9720f52007-02-05 17:04:00 +0000846 assert(Existing && "Conflict but no matching type?!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000847
848 // There is only one case where this is allowed: when we are refining an
849 // opaque type. In this case, Existing will be an opaque type.
850 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
851 // We ARE replacing an opaque type!
852 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
853 return true;
854 }
855
856 // Otherwise, this is an attempt to redefine a type. That's okay if
857 // the redefinition is identical to the original. This will be so if
858 // Existing and T point to the same Type object. In this one case we
859 // allow the equivalent redefinition.
860 if (Existing == T) return true; // Yes, it's equal.
861
862 // Any other kind of (non-equivalent) redefinition is an error.
Reid Spencer63c34452007-01-05 21:51:07 +0000863 GenerateError("Redefinition of type named '" + Name + "' of type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +0000864 T->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +0000865 }
866
867 return false;
868}
869
870//===----------------------------------------------------------------------===//
871// Code for handling upreferences in type names...
872//
873
874// TypeContains - Returns true if Ty directly contains E in it.
875//
876static bool TypeContains(const Type *Ty, const Type *E) {
877 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
878 E) != Ty->subtype_end();
879}
880
881namespace {
882 struct UpRefRecord {
883 // NestingLevel - The number of nesting levels that need to be popped before
884 // this type is resolved.
885 unsigned NestingLevel;
886
887 // LastContainedTy - This is the type at the current binding level for the
888 // type. Every time we reduce the nesting level, this gets updated.
889 const Type *LastContainedTy;
890
891 // UpRefTy - This is the actual opaque type that the upreference is
892 // represented with.
893 OpaqueType *UpRefTy;
894
895 UpRefRecord(unsigned NL, OpaqueType *URTy)
896 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
897 };
898}
899
900// UpRefs - A list of the outstanding upreferences that need to be resolved.
901static std::vector<UpRefRecord> UpRefs;
902
903/// HandleUpRefs - Every time we finish a new layer of types, this function is
904/// called. It loops through the UpRefs vector, which is a list of the
905/// currently active types. For each type, if the up reference is contained in
906/// the newly completed type, we decrement the level count. When the level
907/// count reaches zero, the upreferenced type is the type that is passed in:
908/// thus we can complete the cycle.
909///
910static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner224f84f2006-08-18 17:34:45 +0000911 // If Ty isn't abstract, or if there are no up-references in it, then there is
912 // nothing to resolve here.
913 if (!ty->isAbstract() || UpRefs.empty()) return ty;
Eric Christopher2a5196f2008-09-24 04:55:49 +0000914
Chris Lattner58af2a12006-02-15 07:22:58 +0000915 PATypeHolder Ty(ty);
916 UR_OUT("Type '" << Ty->getDescription() <<
917 "' newly formed. Resolving upreferences.\n" <<
918 UpRefs.size() << " upreferences active!\n");
919
920 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
921 // to zero), we resolve them all together before we resolve them to Ty. At
922 // the end of the loop, if there is anything to resolve to Ty, it will be in
923 // this variable.
924 OpaqueType *TypeToResolve = 0;
925
926 for (unsigned i = 0; i != UpRefs.size(); ++i) {
927 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
928 << UpRefs[i].second->getDescription() << ") = "
929 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
930 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
931 // Decrement level of upreference
932 unsigned Level = --UpRefs[i].NestingLevel;
933 UpRefs[i].LastContainedTy = Ty;
934 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
935 if (Level == 0) { // Upreference should be resolved!
936 if (!TypeToResolve) {
937 TypeToResolve = UpRefs[i].UpRefTy;
938 } else {
939 UR_OUT(" * Resolving upreference for "
940 << UpRefs[i].second->getDescription() << "\n";
941 std::string OldName = UpRefs[i].UpRefTy->getDescription());
942 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
943 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
944 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
945 }
946 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
947 --i; // Do not skip the next element...
948 }
949 }
950 }
951
952 if (TypeToResolve) {
953 UR_OUT(" * Resolving upreference for "
954 << UpRefs[i].second->getDescription() << "\n";
955 std::string OldName = TypeToResolve->getDescription());
956 TypeToResolve->refineAbstractTypeTo(Ty);
957 }
958
959 return Ty;
960}
961
Chris Lattner58af2a12006-02-15 07:22:58 +0000962//===----------------------------------------------------------------------===//
963// RunVMAsmParser - Define an interface to this parser
964//===----------------------------------------------------------------------===//
965//
Reid Spencer14310612006-12-31 05:40:51 +0000966static Module* RunParser(Module * M);
967
Duncan Sandsdc024672007-11-27 13:23:08 +0000968Module *llvm::RunVMAsmParser(llvm::MemoryBuffer *MB) {
969 InitLLLexer(MB);
970 Module *M = RunParser(new Module(LLLgetFilename()));
971 FreeLexer();
972 return M;
Chris Lattner58af2a12006-02-15 07:22:58 +0000973}
974
975%}
976
977%union {
978 llvm::Module *ModuleVal;
979 llvm::Function *FunctionVal;
Chris Lattner58af2a12006-02-15 07:22:58 +0000980 llvm::BasicBlock *BasicBlockVal;
981 llvm::TerminatorInst *TermInstVal;
982 llvm::Instruction *InstVal;
Reid Spencera132e042006-12-03 05:46:11 +0000983 llvm::Constant *ConstVal;
Chris Lattner58af2a12006-02-15 07:22:58 +0000984
Reid Spencera132e042006-12-03 05:46:11 +0000985 const llvm::Type *PrimType;
Reid Spencer14310612006-12-31 05:40:51 +0000986 std::list<llvm::PATypeHolder> *TypeList;
Reid Spencera132e042006-12-03 05:46:11 +0000987 llvm::PATypeHolder *TypeVal;
988 llvm::Value *ValueVal;
Reid Spencera132e042006-12-03 05:46:11 +0000989 std::vector<llvm::Value*> *ValueList;
Dan Gohman81a0c0b2008-05-31 00:58:22 +0000990 std::vector<unsigned> *ConstantList;
Reid Spencer14310612006-12-31 05:40:51 +0000991 llvm::ArgListType *ArgList;
992 llvm::TypeWithAttrs TypeWithAttrs;
993 llvm::TypeWithAttrsList *TypeWithAttrsList;
Dale Johanneseneb57ea72007-11-05 21:20:28 +0000994 llvm::ParamList *ParamList;
Reid Spencer14310612006-12-31 05:40:51 +0000995
Chris Lattner58af2a12006-02-15 07:22:58 +0000996 // Represent the RHS of PHI node
Reid Spencera132e042006-12-03 05:46:11 +0000997 std::list<std::pair<llvm::Value*,
998 llvm::BasicBlock*> > *PHIList;
Chris Lattner58af2a12006-02-15 07:22:58 +0000999 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
Reid Spencera132e042006-12-03 05:46:11 +00001000 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner58af2a12006-02-15 07:22:58 +00001001
1002 llvm::GlobalValue::LinkageTypes Linkage;
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001003 llvm::GlobalValue::VisibilityTypes Visibility;
Devang Patel05988662008-09-25 21:00:45 +00001004 llvm::Attributes Attributes;
Reid Spencer38c91a92007-02-28 02:24:54 +00001005 llvm::APInt *APIntVal;
Chris Lattner58af2a12006-02-15 07:22:58 +00001006 int64_t SInt64Val;
1007 uint64_t UInt64Val;
1008 int SIntVal;
1009 unsigned UIntVal;
Dale Johannesen43421b32007-09-06 18:13:44 +00001010 llvm::APFloat *FPVal;
Chris Lattner58af2a12006-02-15 07:22:58 +00001011 bool BoolVal;
1012
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001013 std::string *StrVal; // This memory must be deleted
1014 llvm::ValID ValIDVal;
Chris Lattner58af2a12006-02-15 07:22:58 +00001015
Reid Spencera132e042006-12-03 05:46:11 +00001016 llvm::Instruction::BinaryOps BinaryOpVal;
1017 llvm::Instruction::TermOps TermOpVal;
1018 llvm::Instruction::MemoryOps MemOpVal;
1019 llvm::Instruction::CastOps CastOpVal;
1020 llvm::Instruction::OtherOps OtherOpVal;
Reid Spencera132e042006-12-03 05:46:11 +00001021 llvm::ICmpInst::Predicate IPredicate;
1022 llvm::FCmpInst::Predicate FPredicate;
Chris Lattner58af2a12006-02-15 07:22:58 +00001023}
1024
Eric Christopher2a5196f2008-09-24 04:55:49 +00001025%type <ModuleVal> Module
Chris Lattner58af2a12006-02-15 07:22:58 +00001026%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
1027%type <BasicBlockVal> BasicBlock InstructionList
1028%type <TermInstVal> BBTerminatorInst
1029%type <InstVal> Inst InstVal MemoryInst
Anton Korobeynikov38e09802007-04-28 13:48:45 +00001030%type <ConstVal> ConstVal ConstExpr AliaseeRef
Chris Lattner58af2a12006-02-15 07:22:58 +00001031%type <ConstVector> ConstVector
1032%type <ArgList> ArgList ArgListH
Chris Lattner58af2a12006-02-15 07:22:58 +00001033%type <PHIList> PHIList
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001034%type <ParamList> ParamList // For call param lists & GEP indices
Reid Spencer14310612006-12-31 05:40:51 +00001035%type <ValueList> IndexList // For GEP indices
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001036%type <ConstantList> ConstantIndexList // For insertvalue/extractvalue indices
Eric Christopher2a5196f2008-09-24 04:55:49 +00001037%type <TypeList> TypeListI
Reid Spencer14310612006-12-31 05:40:51 +00001038%type <TypeWithAttrsList> ArgTypeList ArgTypeListI
Reid Spencer218ded22007-01-05 17:07:23 +00001039%type <TypeWithAttrs> ArgType
Chris Lattner58af2a12006-02-15 07:22:58 +00001040%type <JumpTable> JumpTable
1041%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00001042%type <BoolVal> ThreadLocal // 'thread_local' or not
Chris Lattner58af2a12006-02-15 07:22:58 +00001043%type <BoolVal> OptVolatile // 'volatile' or not
1044%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
1045%type <BoolVal> OptSideEffect // 'sideeffect' or not.
Reid Spencer14310612006-12-31 05:40:51 +00001046%type <Linkage> GVInternalLinkage GVExternalLinkage
1047%type <Linkage> FunctionDefineLinkage FunctionDeclareLinkage
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001048%type <Linkage> AliasLinkage
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001049%type <Visibility> GVVisibilityStyle
Chris Lattner58af2a12006-02-15 07:22:58 +00001050
1051// ValueRef - Unresolved reference to a definition or BB
1052%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
1053%type <ValueVal> ResolvedVal // <type> <valref> pair
Devang Patel7990dc72008-02-20 22:40:23 +00001054%type <ValueList> ReturnedVal
Chris Lattner58af2a12006-02-15 07:22:58 +00001055// Tokens and types for handling constant integer values
1056//
1057// ESINT64VAL - A negative number within long long range
1058%token <SInt64Val> ESINT64VAL
1059
1060// EUINT64VAL - A positive number within uns. long long range
1061%token <UInt64Val> EUINT64VAL
Chris Lattner58af2a12006-02-15 07:22:58 +00001062
Eric Christopher2a5196f2008-09-24 04:55:49 +00001063// ESAPINTVAL - A negative number with arbitrary precision
Reid Spencer38c91a92007-02-28 02:24:54 +00001064%token <APIntVal> ESAPINTVAL
1065
Eric Christopher2a5196f2008-09-24 04:55:49 +00001066// EUAPINTVAL - A positive number with arbitrary precision
Reid Spencer38c91a92007-02-28 02:24:54 +00001067%token <APIntVal> EUAPINTVAL
1068
Reid Spencer41dff5e2007-01-26 08:05:27 +00001069%token <UIntVal> LOCALVAL_ID GLOBALVAL_ID // %123 @123
Chris Lattner58af2a12006-02-15 07:22:58 +00001070%token <FPVal> FPVAL // Float or Double constant
1071
1072// Built in types...
Reid Spencer218ded22007-01-05 17:07:23 +00001073%type <TypeVal> Types ResultTypes
Chris Lattner740e7092008-10-15 06:16:57 +00001074%type <PrimType> PrimType // Classifications
Eric Christopher2a5196f2008-09-24 04:55:49 +00001075%token <PrimType> VOID INTTYPE
Dale Johannesen320fc8a2007-08-03 01:03:46 +00001076%token <PrimType> FLOAT DOUBLE X86_FP80 FP128 PPC_FP128 LABEL
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001077%token TYPE
Chris Lattner58af2a12006-02-15 07:22:58 +00001078
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001079
Eric Christopher2a5196f2008-09-24 04:55:49 +00001080%token<StrVal> LOCALVAR GLOBALVAR LABELSTR
Reid Spencered951ea2007-05-19 07:22:10 +00001081%token<StrVal> STRINGCONSTANT ATSTRINGCONSTANT PCTSTRINGCONSTANT
Reid Spencer41dff5e2007-01-26 08:05:27 +00001082%type <StrVal> LocalName OptLocalName OptLocalAssign
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001083%type <StrVal> GlobalName OptGlobalAssign GlobalAssign
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00001084%type <StrVal> OptSection SectionString OptGC
Chris Lattner58af2a12006-02-15 07:22:58 +00001085
Christopher Lambbf3348d2007-12-12 08:45:45 +00001086%type <UIntVal> OptAlign OptCAlign OptAddrSpace
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001087
Reid Spencer3d6b71e2007-04-09 01:56:05 +00001088%token ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001089%token DECLARE DEFINE GLOBAL CONSTANT SECTION ALIAS VOLATILE THREAD_LOCAL
Reid Spencer14310612006-12-31 05:40:51 +00001090%token TO DOTDOTDOT NULL_TOK UNDEF INTERNAL LINKONCE WEAK APPENDING
Dale Johannesenc7071cc2008-05-14 20:13:36 +00001091%token DLLIMPORT DLLEXPORT EXTERN_WEAK COMMON
Christopher Lamba8ed9bf2007-12-11 09:02:08 +00001092%token OPAQUE EXTERNAL TARGET TRIPLE ALIGN ADDRSPACE
Chris Lattner58af2a12006-02-15 07:22:58 +00001093%token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001094%token CC_TOK CCC_TOK FASTCC_TOK COLDCC_TOK X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
Nick Lewycky280a6e62008-04-25 16:53:59 +00001095%token DATALAYOUT
Chris Lattner15bd0952008-08-29 17:20:18 +00001096%type <UIntVal> OptCallingConv LocalNumber
Devang Patel05988662008-09-25 21:00:45 +00001097%type <Attributes> OptAttributes Attribute
1098%type <Attributes> OptFuncAttrs FuncAttr
Devang Patel652203f2008-09-29 20:49:50 +00001099%type <Attributes> OptRetAttrs RetAttr
Chris Lattner58af2a12006-02-15 07:22:58 +00001100
1101// Basic Block Terminating Operators
1102%token <TermOpVal> RET BR SWITCH INVOKE UNWIND UNREACHABLE
1103
1104// Binary Operators
Reid Spencere4d87aa2006-12-23 06:05:41 +00001105%type <BinaryOpVal> ArithmeticOps LogicalOps // Binops Subcatagories
Reid Spencer3ed469c2006-11-02 20:25:50 +00001106%token <BinaryOpVal> ADD SUB MUL UDIV SDIV FDIV UREM SREM FREM AND OR XOR
Reid Spencer832254e2007-02-02 02:16:23 +00001107%token <BinaryOpVal> SHL LSHR ASHR
1108
Eric Christopher2a5196f2008-09-24 04:55:49 +00001109%token <OtherOpVal> ICMP FCMP VICMP VFCMP
Reid Spencera132e042006-12-03 05:46:11 +00001110%type <IPredicate> IPredicates
Reid Spencera132e042006-12-03 05:46:11 +00001111%type <FPredicate> FPredicates
Eric Christopher2a5196f2008-09-24 04:55:49 +00001112%token EQ NE SLT SGT SLE SGE ULT UGT ULE UGE
Reid Spencer6e18b7d2006-12-03 06:59:29 +00001113%token OEQ ONE OLT OGT OLE OGE ORD UNO UEQ UNE
Chris Lattner58af2a12006-02-15 07:22:58 +00001114
1115// Memory Instructions
1116%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
1117
Reid Spencer3da59db2006-11-27 01:05:10 +00001118// Cast Operators
1119%type <CastOpVal> CastOps
1120%token <CastOpVal> TRUNC ZEXT SEXT FPTRUNC FPEXT BITCAST
1121%token <CastOpVal> UITOFP SITOFP FPTOUI FPTOSI INTTOPTR PTRTOINT
1122
Chris Lattner58af2a12006-02-15 07:22:58 +00001123// Other Operators
Reid Spencer832254e2007-02-02 02:16:23 +00001124%token <OtherOpVal> PHI_TOK SELECT VAARG
Chris Lattnerd5efe842006-04-08 01:18:56 +00001125%token <OtherOpVal> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
Devang Patel5a970972008-02-19 22:27:01 +00001126%token <OtherOpVal> GETRESULT
Dan Gohmane4977cf2008-05-23 01:55:30 +00001127%token <OtherOpVal> EXTRACTVALUE INSERTVALUE
Chris Lattner58af2a12006-02-15 07:22:58 +00001128
Reid Spencer218ded22007-01-05 17:07:23 +00001129// Function Attributes
Reid Spencerb8f85052007-07-31 03:50:36 +00001130%token SIGNEXT ZEROEXT NORETURN INREG SRET NOUNWIND NOALIAS BYVAL NEST
Devang Patel2c9c3e72008-09-26 23:51:19 +00001131%token READNONE READONLY GC OPTSIZE NOINLINE ALWAYSINLINE
Devang Pateld4980812008-09-02 20:52:40 +00001132
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001133// Visibility Styles
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +00001134%token DEFAULT HIDDEN PROTECTED
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001135
Chris Lattner58af2a12006-02-15 07:22:58 +00001136%start Module
1137%%
1138
Chris Lattner58af2a12006-02-15 07:22:58 +00001139
Chris Lattner58af2a12006-02-15 07:22:58 +00001140// Operations that are notably excluded from this list include:
1141// RET, BR, & SWITCH because they end basic blocks and are treated specially.
1142//
Reid Spencer3ed469c2006-11-02 20:25:50 +00001143ArithmeticOps: ADD | SUB | MUL | UDIV | SDIV | FDIV | UREM | SREM | FREM;
Reid Spencer832254e2007-02-02 02:16:23 +00001144LogicalOps : SHL | LSHR | ASHR | AND | OR | XOR;
Eric Christopher2a5196f2008-09-24 04:55:49 +00001145CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | BITCAST |
Reid Spencer3da59db2006-11-27 01:05:10 +00001146 UITOFP | SITOFP | FPTOUI | FPTOSI | INTTOPTR | PTRTOINT;
Reid Spencer832254e2007-02-02 02:16:23 +00001147
Eric Christopher2a5196f2008-09-24 04:55:49 +00001148IPredicates
Reid Spencer4012e832006-12-04 05:24:24 +00001149 : EQ { $$ = ICmpInst::ICMP_EQ; } | NE { $$ = ICmpInst::ICMP_NE; }
Reid Spencer6e18b7d2006-12-03 06:59:29 +00001150 | SLT { $$ = ICmpInst::ICMP_SLT; } | SGT { $$ = ICmpInst::ICMP_SGT; }
1151 | SLE { $$ = ICmpInst::ICMP_SLE; } | SGE { $$ = ICmpInst::ICMP_SGE; }
1152 | ULT { $$ = ICmpInst::ICMP_ULT; } | UGT { $$ = ICmpInst::ICMP_UGT; }
Eric Christopher2a5196f2008-09-24 04:55:49 +00001153 | ULE { $$ = ICmpInst::ICMP_ULE; } | UGE { $$ = ICmpInst::ICMP_UGE; }
Reid Spencer6e18b7d2006-12-03 06:59:29 +00001154 ;
1155
Eric Christopher2a5196f2008-09-24 04:55:49 +00001156FPredicates
Reid Spencer6e18b7d2006-12-03 06:59:29 +00001157 : OEQ { $$ = FCmpInst::FCMP_OEQ; } | ONE { $$ = FCmpInst::FCMP_ONE; }
1158 | OLT { $$ = FCmpInst::FCMP_OLT; } | OGT { $$ = FCmpInst::FCMP_OGT; }
1159 | OLE { $$ = FCmpInst::FCMP_OLE; } | OGE { $$ = FCmpInst::FCMP_OGE; }
1160 | ORD { $$ = FCmpInst::FCMP_ORD; } | UNO { $$ = FCmpInst::FCMP_UNO; }
1161 | UEQ { $$ = FCmpInst::FCMP_UEQ; } | UNE { $$ = FCmpInst::FCMP_UNE; }
1162 | ULT { $$ = FCmpInst::FCMP_ULT; } | UGT { $$ = FCmpInst::FCMP_UGT; }
1163 | ULE { $$ = FCmpInst::FCMP_ULE; } | UGE { $$ = FCmpInst::FCMP_UGE; }
1164 | TRUETOK { $$ = FCmpInst::FCMP_TRUE; }
1165 | FALSETOK { $$ = FCmpInst::FCMP_FALSE; }
1166 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00001167
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001168LocalName : LOCALVAR | STRINGCONSTANT | PCTSTRINGCONSTANT ;
Reid Spencer41dff5e2007-01-26 08:05:27 +00001169OptLocalName : LocalName | /*empty*/ { $$ = 0; };
1170
Christopher Lambbf3348d2007-12-12 08:45:45 +00001171OptAddrSpace : ADDRSPACE '(' EUINT64VAL ')' { $$=$3; }
1172 | /*empty*/ { $$=0; };
1173
Reid Spencer41dff5e2007-01-26 08:05:27 +00001174/// OptLocalAssign - Value producing statements have an optional assignment
1175/// component.
1176OptLocalAssign : LocalName '=' {
1177 $$ = $1;
1178 CHECK_FOR_ERROR
1179 }
1180 | /*empty*/ {
1181 $$ = 0;
1182 CHECK_FOR_ERROR
1183 };
1184
Chris Lattner15bd0952008-08-29 17:20:18 +00001185LocalNumber : LOCALVAL_ID '=' {
1186 $$ = $1;
1187 CHECK_FOR_ERROR
1188};
1189
1190
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001191GlobalName : GLOBALVAR | ATSTRINGCONSTANT ;
Reid Spencer41dff5e2007-01-26 08:05:27 +00001192
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001193OptGlobalAssign : GlobalAssign
Chris Lattner58af2a12006-02-15 07:22:58 +00001194 | /*empty*/ {
1195 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00001196 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001197 };
1198
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001199GlobalAssign : GlobalName '=' {
1200 $$ = $1;
1201 CHECK_FOR_ERROR
Chris Lattner6cdc6822007-04-26 05:31:05 +00001202 };
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001203
Eric Christopher2a5196f2008-09-24 04:55:49 +00001204GVInternalLinkage
1205 : INTERNAL { $$ = GlobalValue::InternalLinkage; }
1206 | WEAK { $$ = GlobalValue::WeakLinkage; }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001207 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1208 | APPENDING { $$ = GlobalValue::AppendingLinkage; }
Eric Christopher2a5196f2008-09-24 04:55:49 +00001209 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
Dale Johannesenc7071cc2008-05-14 20:13:36 +00001210 | COMMON { $$ = GlobalValue::CommonLinkage; }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001211 ;
1212
1213GVExternalLinkage
1214 : DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1215 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
1216 | EXTERNAL { $$ = GlobalValue::ExternalLinkage; }
1217 ;
1218
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001219GVVisibilityStyle
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +00001220 : /*empty*/ { $$ = GlobalValue::DefaultVisibility; }
1221 | DEFAULT { $$ = GlobalValue::DefaultVisibility; }
1222 | HIDDEN { $$ = GlobalValue::HiddenVisibility; }
1223 | PROTECTED { $$ = GlobalValue::ProtectedVisibility; }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001224 ;
1225
Reid Spencer14310612006-12-31 05:40:51 +00001226FunctionDeclareLinkage
1227 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
Eric Christopher2a5196f2008-09-24 04:55:49 +00001228 | DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
Reid Spencer14310612006-12-31 05:40:51 +00001229 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001230 ;
Eric Christopher2a5196f2008-09-24 04:55:49 +00001231
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001232FunctionDefineLinkage
Reid Spencer14310612006-12-31 05:40:51 +00001233 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1234 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001235 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1236 | WEAK { $$ = GlobalValue::WeakLinkage; }
Eric Christopher2a5196f2008-09-24 04:55:49 +00001237 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
1238 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00001239
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001240AliasLinkage
1241 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1242 | WEAK { $$ = GlobalValue::WeakLinkage; }
1243 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
1244 ;
1245
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00001246OptCallingConv : /*empty*/ { $$ = CallingConv::C; } |
1247 CCC_TOK { $$ = CallingConv::C; } |
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00001248 FASTCC_TOK { $$ = CallingConv::Fast; } |
1249 COLDCC_TOK { $$ = CallingConv::Cold; } |
1250 X86_STDCALLCC_TOK { $$ = CallingConv::X86_StdCall; } |
1251 X86_FASTCALLCC_TOK { $$ = CallingConv::X86_FastCall; } |
1252 CC_TOK EUINT64VAL {
Chris Lattner58af2a12006-02-15 07:22:58 +00001253 if ((unsigned)$2 != $2)
Reid Spencerb5334b02007-02-05 10:18:06 +00001254 GEN_ERROR("Calling conv too large");
Chris Lattner58af2a12006-02-15 07:22:58 +00001255 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001256 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001257 };
1258
Devang Patel05988662008-09-25 21:00:45 +00001259Attribute : ZEROEXT { $$ = Attribute::ZExt; }
1260 | ZEXT { $$ = Attribute::ZExt; }
1261 | SIGNEXT { $$ = Attribute::SExt; }
1262 | SEXT { $$ = Attribute::SExt; }
1263 | INREG { $$ = Attribute::InReg; }
1264 | SRET { $$ = Attribute::StructRet; }
1265 | NOALIAS { $$ = Attribute::NoAlias; }
1266 | BYVAL { $$ = Attribute::ByVal; }
1267 | NEST { $$ = Attribute::Nest; }
Eric Christopher2a5196f2008-09-24 04:55:49 +00001268 | ALIGN EUINT64VAL { $$ =
Devang Patel05988662008-09-25 21:00:45 +00001269 Attribute::constructAlignmentFromInt($2); }
Reid Spencer14310612006-12-31 05:40:51 +00001270 ;
1271
Devang Patel05988662008-09-25 21:00:45 +00001272OptAttributes : /* empty */ { $$ = Attribute::None; }
1273 | OptAttributes Attribute {
Reid Spencer7b5d4662007-04-09 06:16:21 +00001274 $$ = $1 | $2;
Reid Spencer14310612006-12-31 05:40:51 +00001275 }
1276 ;
1277
Devang Patel652203f2008-09-29 20:49:50 +00001278RetAttr : INREG { $$ = Attribute::InReg; }
1279 | ZEROEXT { $$ = Attribute::ZExt; }
1280 | SIGNEXT { $$ = Attribute::SExt; }
1281 ;
1282
1283OptRetAttrs : /* empty */ { $$ = Attribute::None; }
1284 | OptRetAttrs RetAttr {
1285 $$ = $1 | $2;
1286 }
1287 ;
1288
1289
Devang Patel05988662008-09-25 21:00:45 +00001290FuncAttr : NORETURN { $$ = Attribute::NoReturn; }
1291 | NOUNWIND { $$ = Attribute::NoUnwind; }
1292 | INREG { $$ = Attribute::InReg; }
1293 | ZEROEXT { $$ = Attribute::ZExt; }
1294 | SIGNEXT { $$ = Attribute::SExt; }
1295 | READNONE { $$ = Attribute::ReadNone; }
1296 | READONLY { $$ = Attribute::ReadOnly; }
Chris Lattner9fc4da42008-10-08 06:44:45 +00001297 | NOINLINE { $$ = Attribute::NoInline; }
1298 | ALWAYSINLINE { $$ = Attribute::AlwaysInline; }
1299 | OPTSIZE { $$ = Attribute::OptimizeForSize; }
Reid Spencer218ded22007-01-05 17:07:23 +00001300 ;
1301
Devang Patel05988662008-09-25 21:00:45 +00001302OptFuncAttrs : /* empty */ { $$ = Attribute::None; }
Reid Spencer218ded22007-01-05 17:07:23 +00001303 | OptFuncAttrs FuncAttr {
Reid Spencer7b5d4662007-04-09 06:16:21 +00001304 $$ = $1 | $2;
Reid Spencer218ded22007-01-05 17:07:23 +00001305 }
Reid Spencer14310612006-12-31 05:40:51 +00001306 ;
1307
Devang Patel652203f2008-09-29 20:49:50 +00001308
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00001309OptGC : /* empty */ { $$ = 0; }
1310 | GC STRINGCONSTANT {
1311 $$ = $2;
1312 }
1313 ;
1314
Chris Lattner58af2a12006-02-15 07:22:58 +00001315// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1316// a comma before it.
1317OptAlign : /*empty*/ { $$ = 0; } |
1318 ALIGN EUINT64VAL {
1319 $$ = $2;
1320 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencerb5334b02007-02-05 10:18:06 +00001321 GEN_ERROR("Alignment must be a power of two");
Reid Spencer61c83e02006-08-18 08:43:06 +00001322 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001323};
1324OptCAlign : /*empty*/ { $$ = 0; } |
1325 ',' ALIGN EUINT64VAL {
1326 $$ = $3;
1327 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencerb5334b02007-02-05 10:18:06 +00001328 GEN_ERROR("Alignment must be a power of two");
Reid Spencer61c83e02006-08-18 08:43:06 +00001329 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001330};
1331
1332
Christopher Lamba8ed9bf2007-12-11 09:02:08 +00001333
Chris Lattner58af2a12006-02-15 07:22:58 +00001334SectionString : SECTION STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001335 for (unsigned i = 0, e = $2->length(); i != e; ++i)
1336 if ((*$2)[i] == '"' || (*$2)[i] == '\\')
Reid Spencerb5334b02007-02-05 10:18:06 +00001337 GEN_ERROR("Invalid character in section name");
Chris Lattner58af2a12006-02-15 07:22:58 +00001338 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001339 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001340};
1341
1342OptSection : /*empty*/ { $$ = 0; } |
1343 SectionString { $$ = $1; };
1344
1345// GlobalVarAttributes - Used to pass the attributes string on a global. CurGV
1346// is set to be the global we are processing.
1347//
1348GlobalVarAttributes : /* empty */ {} |
1349 ',' GlobalVarAttribute GlobalVarAttributes {};
1350GlobalVarAttribute : SectionString {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001351 CurGV->setSection(*$1);
1352 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001353 CHECK_FOR_ERROR
Eric Christopher2a5196f2008-09-24 04:55:49 +00001354 }
Chris Lattner58af2a12006-02-15 07:22:58 +00001355 | ALIGN EUINT64VAL {
1356 if ($2 != 0 && !isPowerOf2_32($2))
Reid Spencerb5334b02007-02-05 10:18:06 +00001357 GEN_ERROR("Alignment must be a power of two");
Chris Lattner58af2a12006-02-15 07:22:58 +00001358 CurGV->setAlignment($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001359 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001360 };
1361
1362//===----------------------------------------------------------------------===//
1363// Types includes all predefined types... except void, because it can only be
Eric Christopher2a5196f2008-09-24 04:55:49 +00001364// used in specific contexts (function returning void for example).
Chris Lattner58af2a12006-02-15 07:22:58 +00001365
1366// Derived types are added later...
1367//
Dale Johannesen320fc8a2007-08-03 01:03:46 +00001368PrimType : INTTYPE | FLOAT | DOUBLE | PPC_FP128 | FP128 | X86_FP80 | LABEL ;
Reid Spencer14310612006-12-31 05:40:51 +00001369
Eric Christopher2a5196f2008-09-24 04:55:49 +00001370Types
Reid Spencer14310612006-12-31 05:40:51 +00001371 : OPAQUE {
Reid Spencera132e042006-12-03 05:46:11 +00001372 $$ = new PATypeHolder(OpaqueType::get());
Reid Spencer61c83e02006-08-18 08:43:06 +00001373 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001374 }
1375 | PrimType {
Reid Spencera132e042006-12-03 05:46:11 +00001376 $$ = new PATypeHolder($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001377 CHECK_FOR_ERROR
Reid Spencer14310612006-12-31 05:40:51 +00001378 }
Christopher Lambbf3348d2007-12-12 08:45:45 +00001379 | Types OptAddrSpace '*' { // Pointer type?
Reid Spencer14310612006-12-31 05:40:51 +00001380 if (*$1 == Type::LabelTy)
1381 GEN_ERROR("Cannot form a pointer to a basic block");
Christopher Lambbf3348d2007-12-12 08:45:45 +00001382 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1, $2)));
Christopher Lamba8ed9bf2007-12-11 09:02:08 +00001383 delete $1;
1384 CHECK_FOR_ERROR
1385 }
Reid Spencer14310612006-12-31 05:40:51 +00001386 | SymbolicValueRef { // Named types are also simple types...
1387 const Type* tmp = getTypeVal($1);
1388 CHECK_FOR_ERROR
1389 $$ = new PATypeHolder(tmp);
1390 }
1391 | '\\' EUINT64VAL { // Type UpReference
Reid Spencerb5334b02007-02-05 10:18:06 +00001392 if ($2 > (uint64_t)~0U) GEN_ERROR("Value out of range");
Chris Lattner58af2a12006-02-15 07:22:58 +00001393 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
1394 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Reid Spencera132e042006-12-03 05:46:11 +00001395 $$ = new PATypeHolder(OT);
Chris Lattner58af2a12006-02-15 07:22:58 +00001396 UR_OUT("New Upreference!\n");
Reid Spencer61c83e02006-08-18 08:43:06 +00001397 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001398 }
Reid Spencer218ded22007-01-05 17:07:23 +00001399 | Types '(' ArgTypeListI ')' OptFuncAttrs {
Duncan Sandsdc024672007-11-27 13:23:08 +00001400 // Allow but ignore attributes on function types; this permits auto-upgrade.
1401 // FIXME: remove in LLVM 3.0.
Chris Lattnera925a142008-04-23 05:37:08 +00001402 const Type *RetTy = *$1;
1403 if (!FunctionType::isValidReturnType(RetTy))
1404 GEN_ERROR("Invalid result type for LLVM function");
Eric Christopher2a5196f2008-09-24 04:55:49 +00001405
Chris Lattner58af2a12006-02-15 07:22:58 +00001406 std::vector<const Type*> Params;
Reid Spencer7b5d4662007-04-09 06:16:21 +00001407 TypeWithAttrsList::iterator I = $3->begin(), E = $3->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00001408 for (; I != E; ++I ) {
Reid Spencer66728ef2007-03-20 01:13:36 +00001409 const Type *Ty = I->Ty->get();
Reid Spencer66728ef2007-03-20 01:13:36 +00001410 Params.push_back(Ty);
Reid Spencer14310612006-12-31 05:40:51 +00001411 }
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001412
Chris Lattner58af2a12006-02-15 07:22:58 +00001413 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1414 if (isVarArg) Params.pop_back();
1415
Anton Korobeynikov05e5a742007-12-03 21:01:29 +00001416 for (unsigned i = 0; i != Params.size(); ++i)
1417 if (!(Params[i]->isFirstClassType() || isa<OpaqueType>(Params[i])))
1418 GEN_ERROR("Function arguments must be value types!");
1419
1420 CHECK_FOR_ERROR
1421
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001422 FunctionType *FT = FunctionType::get(RetTy, Params, isVarArg);
Reid Spencer14310612006-12-31 05:40:51 +00001423 delete $1; // Delete the return type handle
Eric Christopher2a5196f2008-09-24 04:55:49 +00001424 $$ = new PATypeHolder(HandleUpRefs(FT));
Nuno Lopes8a88a372008-10-05 16:49:34 +00001425
1426 // Delete the argument list
1427 for (I = $3->begin() ; I != E; ++I ) {
1428 delete I->Ty;
1429 }
1430 delete $3;
1431
Reid Spencer61c83e02006-08-18 08:43:06 +00001432 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001433 }
Reid Spencer218ded22007-01-05 17:07:23 +00001434 | VOID '(' ArgTypeListI ')' OptFuncAttrs {
Duncan Sandsdc024672007-11-27 13:23:08 +00001435 // Allow but ignore attributes on function types; this permits auto-upgrade.
1436 // FIXME: remove in LLVM 3.0.
Reid Spencer14310612006-12-31 05:40:51 +00001437 std::vector<const Type*> Params;
Reid Spencer7b5d4662007-04-09 06:16:21 +00001438 TypeWithAttrsList::iterator I = $3->begin(), E = $3->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00001439 for ( ; I != E; ++I ) {
Reid Spencer66728ef2007-03-20 01:13:36 +00001440 const Type* Ty = I->Ty->get();
Reid Spencer66728ef2007-03-20 01:13:36 +00001441 Params.push_back(Ty);
Reid Spencer14310612006-12-31 05:40:51 +00001442 }
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001443
Reid Spencer14310612006-12-31 05:40:51 +00001444 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1445 if (isVarArg) Params.pop_back();
1446
Anton Korobeynikov05e5a742007-12-03 21:01:29 +00001447 for (unsigned i = 0; i != Params.size(); ++i)
1448 if (!(Params[i]->isFirstClassType() || isa<OpaqueType>(Params[i])))
1449 GEN_ERROR("Function arguments must be value types!");
1450
1451 CHECK_FOR_ERROR
1452
Duncan Sandsdc024672007-11-27 13:23:08 +00001453 FunctionType *FT = FunctionType::get($1, Params, isVarArg);
Eric Christopher2a5196f2008-09-24 04:55:49 +00001454 $$ = new PATypeHolder(HandleUpRefs(FT));
Nuno Lopes8a88a372008-10-05 16:49:34 +00001455
1456 // Delete the argument list
1457 for (I = $3->begin() ; I != E; ++I ) {
1458 delete I->Ty;
1459 }
1460 delete $3;
1461
Reid Spencer14310612006-12-31 05:40:51 +00001462 CHECK_FOR_ERROR
1463 }
1464
1465 | '[' EUINT64VAL 'x' Types ']' { // Sized array type?
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001466 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, $2)));
Reid Spencera132e042006-12-03 05:46:11 +00001467 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00001468 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001469 }
Chris Lattner32980692007-02-19 07:44:24 +00001470 | '<' EUINT64VAL 'x' Types '>' { // Vector type?
Reid Spencera132e042006-12-03 05:46:11 +00001471 const llvm::Type* ElemTy = $4->get();
1472 if ((unsigned)$2 != $2)
1473 GEN_ERROR("Unsigned result not equal to signed result");
Chris Lattner42a75512007-01-15 02:27:26 +00001474 if (!ElemTy->isFloatingPoint() && !ElemTy->isInteger())
Reid Spencer9d6565a2007-02-15 02:26:10 +00001475 GEN_ERROR("Element type of a VectorType must be primitive");
Reid Spencer9d6565a2007-02-15 02:26:10 +00001476 $$ = new PATypeHolder(HandleUpRefs(VectorType::get(*$4, (unsigned)$2)));
Reid Spencera132e042006-12-03 05:46:11 +00001477 delete $4;
1478 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001479 }
1480 | '{' TypeListI '}' { // Structure type?
1481 std::vector<const Type*> Elements;
Reid Spencera132e042006-12-03 05:46:11 +00001482 for (std::list<llvm::PATypeHolder>::iterator I = $2->begin(),
Chris Lattner58af2a12006-02-15 07:22:58 +00001483 E = $2->end(); I != E; ++I)
Reid Spencera132e042006-12-03 05:46:11 +00001484 Elements.push_back(*I);
Chris Lattner58af2a12006-02-15 07:22:58 +00001485
Reid Spencera132e042006-12-03 05:46:11 +00001486 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattner58af2a12006-02-15 07:22:58 +00001487 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001488 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001489 }
1490 | '{' '}' { // Empty structure type?
Reid Spencera132e042006-12-03 05:46:11 +00001491 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Reid Spencer61c83e02006-08-18 08:43:06 +00001492 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001493 }
Andrew Lenharth6353e052006-12-08 18:07:09 +00001494 | '<' '{' TypeListI '}' '>' {
1495 std::vector<const Type*> Elements;
1496 for (std::list<llvm::PATypeHolder>::iterator I = $3->begin(),
1497 E = $3->end(); I != E; ++I)
1498 Elements.push_back(*I);
1499
1500 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements, true)));
1501 delete $3;
1502 CHECK_FOR_ERROR
1503 }
1504 | '<' '{' '}' '>' { // Empty structure type?
1505 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>(), true));
1506 CHECK_FOR_ERROR
1507 }
Reid Spencer14310612006-12-31 05:40:51 +00001508 ;
1509
Eric Christopher2a5196f2008-09-24 04:55:49 +00001510ArgType
Devang Patel05988662008-09-25 21:00:45 +00001511 : Types OptAttributes {
Duncan Sandsdc024672007-11-27 13:23:08 +00001512 // Allow but ignore attributes on function types; this permits auto-upgrade.
1513 // FIXME: remove in LLVM 3.0.
Eric Christopher2a5196f2008-09-24 04:55:49 +00001514 $$.Ty = $1;
Devang Patel05988662008-09-25 21:00:45 +00001515 $$.Attrs = Attribute::None;
Reid Spencer14310612006-12-31 05:40:51 +00001516 }
1517 ;
1518
Reid Spencer218ded22007-01-05 17:07:23 +00001519ResultTypes
1520 : Types {
Reid Spencer14310612006-12-31 05:40:51 +00001521 if (!UpRefs.empty())
1522 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Devang Patel20071732008-02-23 01:17:37 +00001523 if (!(*$1)->isFirstClassType() && !isa<StructType>($1->get()))
Reid Spencerb5334b02007-02-05 10:18:06 +00001524 GEN_ERROR("LLVM functions cannot return aggregate types");
Reid Spencer218ded22007-01-05 17:07:23 +00001525 $$ = $1;
Reid Spencer14310612006-12-31 05:40:51 +00001526 }
Reid Spencer218ded22007-01-05 17:07:23 +00001527 | VOID {
1528 $$ = new PATypeHolder(Type::VoidTy);
Reid Spencer14310612006-12-31 05:40:51 +00001529 }
1530 ;
1531
1532ArgTypeList : ArgType {
1533 $$ = new TypeWithAttrsList();
1534 $$->push_back($1);
1535 CHECK_FOR_ERROR
1536 }
1537 | ArgTypeList ',' ArgType {
1538 ($$=$1)->push_back($3);
1539 CHECK_FOR_ERROR
1540 }
1541 ;
1542
Eric Christopher2a5196f2008-09-24 04:55:49 +00001543ArgTypeListI
Reid Spencer14310612006-12-31 05:40:51 +00001544 : ArgTypeList
1545 | ArgTypeList ',' DOTDOTDOT {
1546 $$=$1;
Devang Patel05988662008-09-25 21:00:45 +00001547 TypeWithAttrs TWA; TWA.Attrs = Attribute::None;
Reid Spencer14310612006-12-31 05:40:51 +00001548 TWA.Ty = new PATypeHolder(Type::VoidTy);
1549 $$->push_back(TWA);
1550 CHECK_FOR_ERROR
1551 }
1552 | DOTDOTDOT {
1553 $$ = new TypeWithAttrsList;
Devang Patel05988662008-09-25 21:00:45 +00001554 TypeWithAttrs TWA; TWA.Attrs = Attribute::None;
Reid Spencer14310612006-12-31 05:40:51 +00001555 TWA.Ty = new PATypeHolder(Type::VoidTy);
1556 $$->push_back(TWA);
1557 CHECK_FOR_ERROR
1558 }
1559 | /*empty*/ {
1560 $$ = new TypeWithAttrsList();
Reid Spencer61c83e02006-08-18 08:43:06 +00001561 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001562 };
1563
Eric Christopher2a5196f2008-09-24 04:55:49 +00001564// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner58af2a12006-02-15 07:22:58 +00001565// declaration type lists
1566//
Reid Spencer14310612006-12-31 05:40:51 +00001567TypeListI : Types {
Reid Spencera132e042006-12-03 05:46:11 +00001568 $$ = new std::list<PATypeHolder>();
Eric Christopher2a5196f2008-09-24 04:55:49 +00001569 $$->push_back(*$1);
Reid Spencer66728ef2007-03-20 01:13:36 +00001570 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001571 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001572 }
Reid Spencer14310612006-12-31 05:40:51 +00001573 | TypeListI ',' Types {
Eric Christopher2a5196f2008-09-24 04:55:49 +00001574 ($$=$1)->push_back(*$3);
Reid Spencer66728ef2007-03-20 01:13:36 +00001575 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001576 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001577 };
1578
Chris Lattner58af2a12006-02-15 07:22:58 +00001579// ConstVal - The various declarations that go into the constant pool. This
1580// production is used ONLY to represent constants that show up AFTER a 'const',
1581// 'constant' or 'global' token at global scope. Constants that can be inlined
1582// into other expressions (such as integers and constexprs) are handled by the
1583// ResolvedVal, ValueRef and ConstValueRef productions.
1584//
1585ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencer14310612006-12-31 05:40:51 +00001586 if (!UpRefs.empty())
1587 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001588 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001589 if (ATy == 0)
Eric Christopher2a5196f2008-09-24 04:55:49 +00001590 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001591 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001592 const Type *ETy = ATy->getElementType();
Dan Gohman180c1692008-06-23 18:43:26 +00001593 uint64_t NumElements = ATy->getNumElements();
Chris Lattner58af2a12006-02-15 07:22:58 +00001594
1595 // Verify that we have the correct size...
Mon P Wang28873102008-06-25 08:15:39 +00001596 if (NumElements != uint64_t(-1) && NumElements != $3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001597 GEN_ERROR("Type mismatch: constant sized array initialized with " +
Eric Christopher2a5196f2008-09-24 04:55:49 +00001598 utostr($3->size()) + " arguments, but has size of " +
Mon P Wang28873102008-06-25 08:15:39 +00001599 utostr(NumElements) + "");
Chris Lattner58af2a12006-02-15 07:22:58 +00001600
1601 // Verify all elements are correct type!
1602 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencera132e042006-12-03 05:46:11 +00001603 if (ETy != (*$3)[i]->getType())
Eric Christopher2a5196f2008-09-24 04:55:49 +00001604 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00001605 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencera132e042006-12-03 05:46:11 +00001606 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner58af2a12006-02-15 07:22:58 +00001607 }
1608
Reid Spencera132e042006-12-03 05:46:11 +00001609 $$ = ConstantArray::get(ATy, *$3);
1610 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001611 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001612 }
1613 | Types '[' ']' {
Reid Spencer14310612006-12-31 05:40:51 +00001614 if (!UpRefs.empty())
1615 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001616 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001617 if (ATy == 0)
Eric Christopher2a5196f2008-09-24 04:55:49 +00001618 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001619 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001620
Dan Gohman180c1692008-06-23 18:43:26 +00001621 uint64_t NumElements = ATy->getNumElements();
Eric Christopher2a5196f2008-09-24 04:55:49 +00001622 if (NumElements != uint64_t(-1) && NumElements != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001623 GEN_ERROR("Type mismatch: constant sized array initialized with 0"
Mon P Wang28873102008-06-25 08:15:39 +00001624 " arguments, but has size of " + utostr(NumElements) +"");
Reid Spencera132e042006-12-03 05:46:11 +00001625 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
1626 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001627 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001628 }
1629 | Types 'c' STRINGCONSTANT {
Reid Spencer14310612006-12-31 05:40:51 +00001630 if (!UpRefs.empty())
1631 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001632 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001633 if (ATy == 0)
Eric Christopher2a5196f2008-09-24 04:55:49 +00001634 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001635 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001636
Dan Gohman180c1692008-06-23 18:43:26 +00001637 uint64_t NumElements = ATy->getNumElements();
Chris Lattner58af2a12006-02-15 07:22:58 +00001638 const Type *ETy = ATy->getElementType();
Mon P Wang28873102008-06-25 08:15:39 +00001639 if (NumElements != uint64_t(-1) && NumElements != $3->length())
Eric Christopher2a5196f2008-09-24 04:55:49 +00001640 GEN_ERROR("Can't build string constant of size " +
Mon P Wang28873102008-06-25 08:15:39 +00001641 utostr($3->length()) +
1642 " when array has size " + utostr(NumElements) + "");
Chris Lattner58af2a12006-02-15 07:22:58 +00001643 std::vector<Constant*> Vals;
Reid Spencer14310612006-12-31 05:40:51 +00001644 if (ETy == Type::Int8Ty) {
Mon P Wang28873102008-06-25 08:15:39 +00001645 for (uint64_t i = 0; i < $3->length(); ++i)
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001646 Vals.push_back(ConstantInt::get(ETy, (*$3)[i]));
Chris Lattner58af2a12006-02-15 07:22:58 +00001647 } else {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001648 delete $3;
Reid Spencerb5334b02007-02-05 10:18:06 +00001649 GEN_ERROR("Cannot build string arrays of non byte sized elements");
Chris Lattner58af2a12006-02-15 07:22:58 +00001650 }
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001651 delete $3;
Reid Spencera132e042006-12-03 05:46:11 +00001652 $$ = ConstantArray::get(ATy, Vals);
1653 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001654 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001655 }
1656 | Types '<' ConstVector '>' { // Nonempty unsized arr
Reid Spencer14310612006-12-31 05:40:51 +00001657 if (!UpRefs.empty())
1658 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer9d6565a2007-02-15 02:26:10 +00001659 const VectorType *PTy = dyn_cast<VectorType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001660 if (PTy == 0)
Eric Christopher2a5196f2008-09-24 04:55:49 +00001661 GEN_ERROR("Cannot make packed constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001662 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001663 const Type *ETy = PTy->getElementType();
Dan Gohman180c1692008-06-23 18:43:26 +00001664 unsigned NumElements = PTy->getNumElements();
Chris Lattner58af2a12006-02-15 07:22:58 +00001665
1666 // Verify that we have the correct size...
Mon P Wang28873102008-06-25 08:15:39 +00001667 if (NumElements != unsigned(-1) && NumElements != (unsigned)$3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001668 GEN_ERROR("Type mismatch: constant sized packed initialized with " +
Eric Christopher2a5196f2008-09-24 04:55:49 +00001669 utostr($3->size()) + " arguments, but has size of " +
Mon P Wang28873102008-06-25 08:15:39 +00001670 utostr(NumElements) + "");
Chris Lattner58af2a12006-02-15 07:22:58 +00001671
1672 // Verify all elements are correct type!
1673 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencera132e042006-12-03 05:46:11 +00001674 if (ETy != (*$3)[i]->getType())
Eric Christopher2a5196f2008-09-24 04:55:49 +00001675 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00001676 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencera132e042006-12-03 05:46:11 +00001677 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner58af2a12006-02-15 07:22:58 +00001678 }
1679
Reid Spencer9d6565a2007-02-15 02:26:10 +00001680 $$ = ConstantVector::get(PTy, *$3);
Reid Spencera132e042006-12-03 05:46:11 +00001681 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001682 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001683 }
1684 | Types '{' ConstVector '}' {
Reid Spencera132e042006-12-03 05:46:11 +00001685 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001686 if (STy == 0)
Eric Christopher2a5196f2008-09-24 04:55:49 +00001687 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001688 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001689
1690 if ($3->size() != STy->getNumContainedTypes())
Reid Spencerb5334b02007-02-05 10:18:06 +00001691 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattner58af2a12006-02-15 07:22:58 +00001692
1693 // Check to ensure that constants are compatible with the type initializer!
1694 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Reid Spencera132e042006-12-03 05:46:11 +00001695 if ((*$3)[i]->getType() != STy->getElementType(i))
Reid Spencer61c83e02006-08-18 08:43:06 +00001696 GEN_ERROR("Expected type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00001697 STy->getElementType(i)->getDescription() +
1698 "' for element #" + utostr(i) +
Reid Spencerb5334b02007-02-05 10:18:06 +00001699 " of structure initializer");
Chris Lattner58af2a12006-02-15 07:22:58 +00001700
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001701 // Check to ensure that Type is not packed
1702 if (STy->isPacked())
Chris Lattner6cdc6822007-04-26 05:31:05 +00001703 GEN_ERROR("Unpacked Initializer to vector type '" +
1704 STy->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001705
Reid Spencera132e042006-12-03 05:46:11 +00001706 $$ = ConstantStruct::get(STy, *$3);
1707 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001708 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001709 }
1710 | Types '{' '}' {
Reid Spencer14310612006-12-31 05:40:51 +00001711 if (!UpRefs.empty())
1712 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001713 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001714 if (STy == 0)
Eric Christopher2a5196f2008-09-24 04:55:49 +00001715 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001716 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001717
1718 if (STy->getNumContainedTypes() != 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00001719 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattner58af2a12006-02-15 07:22:58 +00001720
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001721 // Check to ensure that Type is not packed
1722 if (STy->isPacked())
Chris Lattner6cdc6822007-04-26 05:31:05 +00001723 GEN_ERROR("Unpacked Initializer to vector type '" +
1724 STy->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001725
1726 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1727 delete $1;
1728 CHECK_FOR_ERROR
1729 }
1730 | Types '<' '{' ConstVector '}' '>' {
1731 const StructType *STy = dyn_cast<StructType>($1->get());
1732 if (STy == 0)
Eric Christopher2a5196f2008-09-24 04:55:49 +00001733 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001734 (*$1)->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001735
1736 if ($4->size() != STy->getNumContainedTypes())
Reid Spencerb5334b02007-02-05 10:18:06 +00001737 GEN_ERROR("Illegal number of initializers for structure type");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001738
1739 // Check to ensure that constants are compatible with the type initializer!
1740 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1741 if ((*$4)[i]->getType() != STy->getElementType(i))
1742 GEN_ERROR("Expected type '" +
1743 STy->getElementType(i)->getDescription() +
1744 "' for element #" + utostr(i) +
Reid Spencerb5334b02007-02-05 10:18:06 +00001745 " of structure initializer");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001746
1747 // Check to ensure that Type is packed
1748 if (!STy->isPacked())
Eric Christopher2a5196f2008-09-24 04:55:49 +00001749 GEN_ERROR("Vector initializer to non-vector type '" +
Chris Lattner32980692007-02-19 07:44:24 +00001750 STy->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001751
1752 $$ = ConstantStruct::get(STy, *$4);
1753 delete $1; delete $4;
1754 CHECK_FOR_ERROR
1755 }
1756 | Types '<' '{' '}' '>' {
1757 if (!UpRefs.empty())
1758 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1759 const StructType *STy = dyn_cast<StructType>($1->get());
1760 if (STy == 0)
Eric Christopher2a5196f2008-09-24 04:55:49 +00001761 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001762 (*$1)->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001763
1764 if (STy->getNumContainedTypes() != 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00001765 GEN_ERROR("Illegal number of initializers for structure type");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001766
1767 // Check to ensure that Type is packed
1768 if (!STy->isPacked())
Eric Christopher2a5196f2008-09-24 04:55:49 +00001769 GEN_ERROR("Vector initializer to non-vector type '" +
Chris Lattner32980692007-02-19 07:44:24 +00001770 STy->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001771
Reid Spencera132e042006-12-03 05:46:11 +00001772 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1773 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001774 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001775 }
1776 | Types NULL_TOK {
Reid Spencer14310612006-12-31 05:40:51 +00001777 if (!UpRefs.empty())
1778 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001779 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001780 if (PTy == 0)
Eric Christopher2a5196f2008-09-24 04:55:49 +00001781 GEN_ERROR("Cannot make null pointer constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001782 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001783
Reid Spencera132e042006-12-03 05:46:11 +00001784 $$ = ConstantPointerNull::get(PTy);
1785 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001786 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001787 }
1788 | Types UNDEF {
Reid Spencer14310612006-12-31 05:40:51 +00001789 if (!UpRefs.empty())
1790 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001791 $$ = UndefValue::get($1->get());
1792 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001793 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001794 }
1795 | Types SymbolicValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00001796 if (!UpRefs.empty())
1797 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001798 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001799 if (Ty == 0)
Devang Patel5a970972008-02-19 22:27:01 +00001800 GEN_ERROR("Global const reference must be a pointer type " + (*$1)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00001801
1802 // ConstExprs can exist in the body of a function, thus creating
1803 // GlobalValues whenever they refer to a variable. Because we are in
Reid Spencer93c40032007-03-19 18:40:50 +00001804 // the context of a function, getExistingVal will search the functions
Chris Lattner58af2a12006-02-15 07:22:58 +00001805 // symbol table instead of the module symbol table for the global symbol,
1806 // which throws things all off. To get around this, we just tell
Reid Spencer93c40032007-03-19 18:40:50 +00001807 // getExistingVal that we are at global scope here.
Chris Lattner58af2a12006-02-15 07:22:58 +00001808 //
1809 Function *SavedCurFn = CurFun.CurrentFunction;
1810 CurFun.CurrentFunction = 0;
1811
Reid Spencer93c40032007-03-19 18:40:50 +00001812 Value *V = getExistingVal(Ty, $2);
Reid Spencer5b7e7532006-09-28 19:28:24 +00001813 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001814
1815 CurFun.CurrentFunction = SavedCurFn;
1816
1817 // If this is an initializer for a constant pointer, which is referencing a
1818 // (currently) undefined variable, create a stub now that shall be replaced
1819 // in the future with the right type of variable.
1820 //
1821 if (V == 0) {
Reid Spencera9720f52007-02-05 17:04:00 +00001822 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001823 const PointerType *PT = cast<PointerType>(Ty);
1824
1825 // First check to see if the forward references value is already created!
1826 PerModuleInfo::GlobalRefsType::iterator I =
1827 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Eric Christopher2a5196f2008-09-24 04:55:49 +00001828
Chris Lattner58af2a12006-02-15 07:22:58 +00001829 if (I != CurModule.GlobalRefs.end()) {
1830 V = I->second; // Placeholder already exists, use it...
1831 $2.destroy();
1832 } else {
1833 std::string Name;
Reid Spencer41dff5e2007-01-26 08:05:27 +00001834 if ($2.Type == ValID::GlobalName)
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001835 Name = $2.getName();
Reid Spencer41dff5e2007-01-26 08:05:27 +00001836 else if ($2.Type != ValID::GlobalID)
1837 GEN_ERROR("Invalid reference to global");
Chris Lattner58af2a12006-02-15 07:22:58 +00001838
1839 // Create the forward referenced global.
1840 GlobalValue *GV;
Eric Christopher2a5196f2008-09-24 04:55:49 +00001841 if (const FunctionType *FTy =
Chris Lattner58af2a12006-02-15 07:22:58 +00001842 dyn_cast<FunctionType>(PT->getElementType())) {
Gabor Greife64d2482008-04-06 23:07:54 +00001843 GV = Function::Create(FTy, GlobalValue::ExternalWeakLinkage, Name,
1844 CurModule.CurrentModule);
Chris Lattner58af2a12006-02-15 07:22:58 +00001845 } else {
1846 GV = new GlobalVariable(PT->getElementType(), false,
Chris Lattner6cdc6822007-04-26 05:31:05 +00001847 GlobalValue::ExternalWeakLinkage, 0,
Chris Lattner58af2a12006-02-15 07:22:58 +00001848 Name, CurModule.CurrentModule);
1849 }
1850
1851 // Keep track of the fact that we have a forward ref to recycle it
1852 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1853 V = GV;
1854 }
1855 }
1856
Reid Spencera132e042006-12-03 05:46:11 +00001857 $$ = cast<GlobalValue>(V);
1858 delete $1; // Free the type handle
Reid Spencer61c83e02006-08-18 08:43:06 +00001859 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001860 }
1861 | Types ConstExpr {
Reid Spencer14310612006-12-31 05:40:51 +00001862 if (!UpRefs.empty())
1863 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001864 if ($1->get() != $2->getType())
Eric Christopher2a5196f2008-09-24 04:55:49 +00001865 GEN_ERROR("Mismatched types for constant expression: " +
Reid Spencere68853b2007-01-04 00:06:14 +00001866 (*$1)->getDescription() + " and " + $2->getType()->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00001867 $$ = $2;
Reid Spencera132e042006-12-03 05:46:11 +00001868 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001869 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001870 }
1871 | Types ZEROINITIALIZER {
Reid Spencer14310612006-12-31 05:40:51 +00001872 if (!UpRefs.empty())
1873 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001874 const Type *Ty = $1->get();
Chris Lattner58af2a12006-02-15 07:22:58 +00001875 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
Reid Spencerb5334b02007-02-05 10:18:06 +00001876 GEN_ERROR("Cannot create a null initialized value of this type");
Reid Spencera132e042006-12-03 05:46:11 +00001877 $$ = Constant::getNullValue(Ty);
1878 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001879 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00001880 }
Chris Lattner740e7092008-10-15 06:16:57 +00001881 | Types ESINT64VAL { // integral constants
1882 if (IntegerType *IT = dyn_cast<IntegerType>($1->get())) {
1883 if (!ConstantInt::isValueValidForType(IT, $2))
1884 GEN_ERROR("Constant value doesn't fit in type");
1885 $$ = ConstantInt::get(IT, $2, true);
1886 } else {
1887 GEN_ERROR("integer constant must have integer type");
1888 }
1889 delete $1;
Reid Spencer38c91a92007-02-28 02:24:54 +00001890 CHECK_FOR_ERROR
1891 }
Chris Lattner740e7092008-10-15 06:16:57 +00001892 | Types ESAPINTVAL { // arbitrary precision integer constants
1893 if (IntegerType *IT = dyn_cast<IntegerType>($1->get())) {
1894 if ($2->getBitWidth() > IT->getBitWidth())
1895 GEN_ERROR("Constant value does not fit in type");
1896 $2->sextOrTrunc(IT->getBitWidth());
1897 $$ = ConstantInt::get(*$2);
1898 } else {
1899 GEN_ERROR("integer constant must have integer type");
Reid Spencer10794272007-03-01 19:41:47 +00001900 }
Chris Lattner740e7092008-10-15 06:16:57 +00001901 delete $1;
Reid Spencer38c91a92007-02-28 02:24:54 +00001902 delete $2;
Reid Spencere4d87aa2006-12-23 06:05:41 +00001903 CHECK_FOR_ERROR
1904 }
Chris Lattner740e7092008-10-15 06:16:57 +00001905 | Types EUINT64VAL { // integral constants
1906 if (IntegerType *IT = dyn_cast<IntegerType>($1->get())) {
1907 if (!ConstantInt::isValueValidForType(IT, $2))
1908 GEN_ERROR("Constant value doesn't fit in type");
1909 $$ = ConstantInt::get(IT, $2, false);
1910 } else {
1911 GEN_ERROR("integer constant must have integer type");
Eric Christopher2a5196f2008-09-24 04:55:49 +00001912 }
Chris Lattner740e7092008-10-15 06:16:57 +00001913 delete $1;
Reid Spencere4d87aa2006-12-23 06:05:41 +00001914 CHECK_FOR_ERROR
1915 }
Chris Lattner740e7092008-10-15 06:16:57 +00001916 | Types EUAPINTVAL { // arbitrary precision integer constants
1917 if (IntegerType *IT = dyn_cast<IntegerType>($1->get())) {
1918 if ($2->getBitWidth() > IT->getBitWidth())
1919 GEN_ERROR("Constant value does not fit in type");
1920 $2->zextOrTrunc(IT->getBitWidth());
1921 $$ = ConstantInt::get(*$2);
1922 } else {
1923 GEN_ERROR("integer constant must have integer type");
1924 }
1925
1926 delete $2;
1927 delete $1;
1928 CHECK_FOR_ERROR
1929 }
1930 | Types TRUETOK { // Boolean constants
1931 if ($1->get() != Type::Int1Ty)
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001932 GEN_ERROR("Constant true must have type i1");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001933 $$ = ConstantInt::getTrue();
Chris Lattner740e7092008-10-15 06:16:57 +00001934 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001935 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001936 }
Chris Lattner740e7092008-10-15 06:16:57 +00001937 | Types FALSETOK { // Boolean constants
1938 if ($1->get() != Type::Int1Ty)
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001939 GEN_ERROR("Constant false must have type i1");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001940 $$ = ConstantInt::getFalse();
Chris Lattner740e7092008-10-15 06:16:57 +00001941 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001942 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001943 }
Chris Lattner740e7092008-10-15 06:16:57 +00001944 | Types FPVAL { // Floating point constants
1945 if (!ConstantFP::isValueValidForType($1->get(), *$2))
Reid Spencerb5334b02007-02-05 10:18:06 +00001946 GEN_ERROR("Floating point constant invalid for type");
Chris Lattner740e7092008-10-15 06:16:57 +00001947
Eric Christopher2a5196f2008-09-24 04:55:49 +00001948 // Lexer has no type info, so builds all float and double FP constants
Dale Johannesenc72cd7e2007-09-11 18:33:39 +00001949 // as double. Fix this here. Long double is done right.
Chris Lattner740e7092008-10-15 06:16:57 +00001950 if (&$2->getSemantics()==&APFloat::IEEEdouble && $1->get()==Type::FloatTy) {
Dale Johannesen236bbd42008-10-09 23:01:34 +00001951 bool ignored;
1952 $2->convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven,
1953 &ignored);
1954 }
Chris Lattnerd8eb63f2008-04-20 00:41:19 +00001955 $$ = ConstantFP::get(*$2);
Chris Lattner740e7092008-10-15 06:16:57 +00001956 delete $1;
Dale Johannesencdd509a2007-09-07 21:07:57 +00001957 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001958 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001959 };
1960
1961
Reid Spencer3da59db2006-11-27 01:05:10 +00001962ConstExpr: CastOps '(' ConstVal TO Types ')' {
Reid Spencer14310612006-12-31 05:40:51 +00001963 if (!UpRefs.empty())
1964 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001965 Constant *Val = $3;
Reid Spencerb0fcf8f2007-01-17 02:48:45 +00001966 const Type *DestTy = $5->get();
1967 if (!CastInst::castIsValid($1, $3, DestTy))
1968 GEN_ERROR("invalid cast opcode for cast from '" +
1969 Val->getType()->getDescription() + "' to '" +
Eric Christopher2a5196f2008-09-24 04:55:49 +00001970 DestTy->getDescription() + "'");
Reid Spencerb0fcf8f2007-01-17 02:48:45 +00001971 $$ = ConstantExpr::getCast($1, $3, DestTy);
Reid Spencera132e042006-12-03 05:46:11 +00001972 delete $5;
Chris Lattner58af2a12006-02-15 07:22:58 +00001973 }
1974 | GETELEMENTPTR '(' ConstVal IndexList ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001975 if (!isa<PointerType>($3->getType()))
Reid Spencerb5334b02007-02-05 10:18:06 +00001976 GEN_ERROR("GetElementPtr requires a pointer operand");
Chris Lattner58af2a12006-02-15 07:22:58 +00001977
Reid Spencera132e042006-12-03 05:46:11 +00001978 const Type *IdxTy =
Dan Gohman041e2eb2008-05-15 19:50:34 +00001979 GetElementPtrInst::getIndexedType($3->getType(), $4->begin(), $4->end());
Reid Spencera132e042006-12-03 05:46:11 +00001980 if (!IdxTy)
Reid Spencerb5334b02007-02-05 10:18:06 +00001981 GEN_ERROR("Index list invalid for constant getelementptr");
Reid Spencera132e042006-12-03 05:46:11 +00001982
Chris Lattnerf7469af2007-01-31 04:44:08 +00001983 SmallVector<Constant*, 8> IdxVec;
Reid Spencera132e042006-12-03 05:46:11 +00001984 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1985 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattner58af2a12006-02-15 07:22:58 +00001986 IdxVec.push_back(C);
1987 else
Reid Spencerb5334b02007-02-05 10:18:06 +00001988 GEN_ERROR("Indices to constant getelementptr must be constants");
Chris Lattner58af2a12006-02-15 07:22:58 +00001989
1990 delete $4;
1991
Chris Lattnerf7469af2007-01-31 04:44:08 +00001992 $$ = ConstantExpr::getGetElementPtr($3, &IdxVec[0], IdxVec.size());
Reid Spencer61c83e02006-08-18 08:43:06 +00001993 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001994 }
1995 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer4fe16d62007-01-11 18:21:29 +00001996 if ($3->getType() != Type::Int1Ty)
Reid Spencerb5334b02007-02-05 10:18:06 +00001997 GEN_ERROR("Select condition must be of boolean type");
Reid Spencera132e042006-12-03 05:46:11 +00001998 if ($5->getType() != $7->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00001999 GEN_ERROR("Select operand types must match");
Reid Spencera132e042006-12-03 05:46:11 +00002000 $$ = ConstantExpr::getSelect($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00002001 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002002 }
2003 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00002004 if ($3->getType() != $5->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00002005 GEN_ERROR("Binary operator types must match");
Reid Spencer1628cec2006-10-26 06:15:43 +00002006 CHECK_FOR_ERROR;
Reid Spencer9eef56f2006-12-05 19:16:11 +00002007 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattner58af2a12006-02-15 07:22:58 +00002008 }
2009 | LogicalOps '(' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00002010 if ($3->getType() != $5->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00002011 GEN_ERROR("Logical operator types must match");
Chris Lattner42a75512007-01-15 02:27:26 +00002012 if (!$3->getType()->isInteger()) {
Eric Christopher2a5196f2008-09-24 04:55:49 +00002013 if (!isa<VectorType>($3->getType()) ||
Reid Spencer9d6565a2007-02-15 02:26:10 +00002014 !cast<VectorType>($3->getType())->getElementType()->isInteger())
Reid Spencerb5334b02007-02-05 10:18:06 +00002015 GEN_ERROR("Logical operator requires integral operands");
Chris Lattner58af2a12006-02-15 07:22:58 +00002016 }
Reid Spencera132e042006-12-03 05:46:11 +00002017 $$ = ConstantExpr::get($1, $3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002018 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002019 }
Reid Spencer4012e832006-12-04 05:24:24 +00002020 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
2021 if ($4->getType() != $6->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00002022 GEN_ERROR("icmp operand types must match");
Reid Spencer4012e832006-12-04 05:24:24 +00002023 $$ = ConstantExpr::getICmp($2, $4, $6);
Reid Spencera132e042006-12-03 05:46:11 +00002024 }
Reid Spencer4012e832006-12-04 05:24:24 +00002025 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
2026 if ($4->getType() != $6->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00002027 GEN_ERROR("fcmp operand types must match");
Reid Spencer4012e832006-12-04 05:24:24 +00002028 $$ = ConstantExpr::getFCmp($2, $4, $6);
Reid Spencera132e042006-12-03 05:46:11 +00002029 }
Nate Begemanac80ade2008-05-12 19:01:56 +00002030 | VICMP IPredicates '(' ConstVal ',' ConstVal ')' {
2031 if ($4->getType() != $6->getType())
2032 GEN_ERROR("vicmp operand types must match");
2033 $$ = ConstantExpr::getVICmp($2, $4, $6);
2034 }
2035 | VFCMP FPredicates '(' ConstVal ',' ConstVal ')' {
2036 if ($4->getType() != $6->getType())
2037 GEN_ERROR("vfcmp operand types must match");
2038 $$ = ConstantExpr::getVFCmp($2, $4, $6);
2039 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002040 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00002041 if (!ExtractElementInst::isValidOperands($3, $5))
Reid Spencerb5334b02007-02-05 10:18:06 +00002042 GEN_ERROR("Invalid extractelement operands");
Reid Spencera132e042006-12-03 05:46:11 +00002043 $$ = ConstantExpr::getExtractElement($3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002044 CHECK_FOR_ERROR
Chris Lattnerd25db202006-04-08 03:55:17 +00002045 }
2046 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00002047 if (!InsertElementInst::isValidOperands($3, $5, $7))
Reid Spencerb5334b02007-02-05 10:18:06 +00002048 GEN_ERROR("Invalid insertelement operands");
Reid Spencera132e042006-12-03 05:46:11 +00002049 $$ = ConstantExpr::getInsertElement($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00002050 CHECK_FOR_ERROR
Chris Lattnerd25db202006-04-08 03:55:17 +00002051 }
2052 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00002053 if (!ShuffleVectorInst::isValidOperands($3, $5, $7))
Reid Spencerb5334b02007-02-05 10:18:06 +00002054 GEN_ERROR("Invalid shufflevector operands");
Reid Spencera132e042006-12-03 05:46:11 +00002055 $$ = ConstantExpr::getShuffleVector($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00002056 CHECK_FOR_ERROR
Dan Gohmane4977cf2008-05-23 01:55:30 +00002057 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002058 | EXTRACTVALUE '(' ConstVal ConstantIndexList ')' {
Dan Gohmane4977cf2008-05-23 01:55:30 +00002059 if (!isa<StructType>($3->getType()) && !isa<ArrayType>($3->getType()))
2060 GEN_ERROR("ExtractValue requires an aggregate operand");
2061
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002062 $$ = ConstantExpr::getExtractValue($3, &(*$4)[0], $4->size());
Dan Gohmane4977cf2008-05-23 01:55:30 +00002063 delete $4;
Dan Gohmane4977cf2008-05-23 01:55:30 +00002064 CHECK_FOR_ERROR
2065 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002066 | INSERTVALUE '(' ConstVal ',' ConstVal ConstantIndexList ')' {
Dan Gohmane4977cf2008-05-23 01:55:30 +00002067 if (!isa<StructType>($3->getType()) && !isa<ArrayType>($3->getType()))
2068 GEN_ERROR("InsertValue requires an aggregate operand");
2069
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002070 $$ = ConstantExpr::getInsertValue($3, $5, &(*$6)[0], $6->size());
Dan Gohmane4977cf2008-05-23 01:55:30 +00002071 delete $6;
Dan Gohmane4977cf2008-05-23 01:55:30 +00002072 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002073 };
2074
Chris Lattnerd25db202006-04-08 03:55:17 +00002075
Chris Lattner58af2a12006-02-15 07:22:58 +00002076// ConstVector - A list of comma separated constants.
2077ConstVector : ConstVector ',' ConstVal {
2078 ($$ = $1)->push_back($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002079 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002080 }
2081 | ConstVal {
Reid Spencera132e042006-12-03 05:46:11 +00002082 $$ = new std::vector<Constant*>();
Chris Lattner58af2a12006-02-15 07:22:58 +00002083 $$->push_back($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002084 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002085 };
2086
2087
2088// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
2089GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
2090
Eric Christopher2a5196f2008-09-24 04:55:49 +00002091// ThreadLocal
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002092ThreadLocal : THREAD_LOCAL { $$ = true; } | { $$ = false; };
2093
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002094// AliaseeRef - Match either GlobalValue or bitcast to GlobalValue.
2095AliaseeRef : ResultTypes SymbolicValueRef {
2096 const Type* VTy = $1->get();
2097 Value *V = getVal(VTy, $2);
Chris Lattner0275cff2007-08-06 21:00:46 +00002098 CHECK_FOR_ERROR
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002099 GlobalValue* Aliasee = dyn_cast<GlobalValue>(V);
2100 if (!Aliasee)
2101 GEN_ERROR("Aliases can be created only to global values");
2102
2103 $$ = Aliasee;
2104 CHECK_FOR_ERROR
2105 delete $1;
2106 }
2107 | BITCAST '(' AliaseeRef TO Types ')' {
2108 Constant *Val = $3;
2109 const Type *DestTy = $5->get();
2110 if (!CastInst::castIsValid($1, $3, DestTy))
2111 GEN_ERROR("invalid cast opcode for cast from '" +
2112 Val->getType()->getDescription() + "' to '" +
2113 DestTy->getDescription() + "'");
Eric Christopher2a5196f2008-09-24 04:55:49 +00002114
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002115 $$ = ConstantExpr::getCast($1, $3, DestTy);
2116 CHECK_FOR_ERROR
2117 delete $5;
2118 };
Chris Lattner58af2a12006-02-15 07:22:58 +00002119
2120//===----------------------------------------------------------------------===//
2121// Rules to match Modules
2122//===----------------------------------------------------------------------===//
2123
2124// Module rule: Capture the result of parsing the whole file into a result
2125// variable...
2126//
Eric Christopher2a5196f2008-09-24 04:55:49 +00002127Module
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002128 : DefinitionList {
2129 $$ = ParserResult = CurModule.CurrentModule;
2130 CurModule.ModuleDone();
2131 CHECK_FOR_ERROR;
2132 }
2133 | /*empty*/ {
2134 $$ = ParserResult = CurModule.CurrentModule;
2135 CurModule.ModuleDone();
2136 CHECK_FOR_ERROR;
2137 }
2138 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00002139
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002140DefinitionList
2141 : Definition
2142 | DefinitionList Definition
2143 ;
2144
Eric Christopher2a5196f2008-09-24 04:55:49 +00002145Definition
Jeff Cohen361c3ef2007-01-21 19:19:31 +00002146 : DEFINE { CurFun.isDeclare = false; } Function {
Chris Lattner58af2a12006-02-15 07:22:58 +00002147 CurFun.FunctionDone();
Reid Spencer61c83e02006-08-18 08:43:06 +00002148 CHECK_FOR_ERROR
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002149 }
2150 | DECLARE { CurFun.isDeclare = true; } FunctionProto {
Reid Spencer61c83e02006-08-18 08:43:06 +00002151 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002152 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002153 | MODULE ASM_TOK AsmBlock {
Reid Spencer61c83e02006-08-18 08:43:06 +00002154 CHECK_FOR_ERROR
Eric Christopher2a5196f2008-09-24 04:55:49 +00002155 }
Reid Spencer41dff5e2007-01-26 08:05:27 +00002156 | OptLocalAssign TYPE Types {
Reid Spencer14310612006-12-31 05:40:51 +00002157 if (!UpRefs.empty())
2158 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00002159 // Eagerly resolve types. This is not an optimization, this is a
2160 // requirement that is due to the fact that we could have this:
2161 //
2162 // %list = type { %list * }
2163 // %list = type { %list * } ; repeated type decl
2164 //
2165 // If types are not resolved eagerly, then the two types will not be
2166 // determined to be the same type!
2167 //
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002168 ResolveTypeTo($1, *$3);
Chris Lattner58af2a12006-02-15 07:22:58 +00002169
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002170 if (!setTypeName(*$3, $1) && !$1) {
Reid Spencer5b7e7532006-09-28 19:28:24 +00002171 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002172 // If this is a named type that is not a redefinition, add it to the slot
2173 // table.
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002174 CurModule.Types.push_back(*$3);
Chris Lattner58af2a12006-02-15 07:22:58 +00002175 }
Reid Spencera132e042006-12-03 05:46:11 +00002176
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002177 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002178 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002179 }
Reid Spencer41dff5e2007-01-26 08:05:27 +00002180 | OptLocalAssign TYPE VOID {
Reid Spencer14310612006-12-31 05:40:51 +00002181 ResolveTypeTo($1, $3);
2182
2183 if (!setTypeName($3, $1) && !$1) {
2184 CHECK_FOR_ERROR
2185 // If this is a named type that is not a redefinition, add it to the slot
2186 // table.
2187 CurModule.Types.push_back($3);
2188 }
2189 CHECK_FOR_ERROR
2190 }
Eric Christopher2a5196f2008-09-24 04:55:49 +00002191 | OptGlobalAssign GVVisibilityStyle ThreadLocal GlobalType ConstVal
2192 OptAddrSpace {
Reid Spencer41dff5e2007-01-26 08:05:27 +00002193 /* "Externally Visible" Linkage */
Eric Christopher2a5196f2008-09-24 04:55:49 +00002194 if ($5 == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002195 GEN_ERROR("Global value initializer is not a constant");
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002196 CurGV = ParseGlobalVariable($1, GlobalValue::ExternalLinkage,
Christopher Lambbf3348d2007-12-12 08:45:45 +00002197 $2, $4, $5->getType(), $5, $3, $6);
Christopher Lamba8ed9bf2007-12-11 09:02:08 +00002198 CHECK_FOR_ERROR
2199 } GlobalVarAttributes {
2200 CurGV = 0;
2201 }
Chris Lattner6cdc6822007-04-26 05:31:05 +00002202 | OptGlobalAssign GVInternalLinkage GVVisibilityStyle ThreadLocal GlobalType
Christopher Lambbf3348d2007-12-12 08:45:45 +00002203 ConstVal OptAddrSpace {
Eric Christopher2a5196f2008-09-24 04:55:49 +00002204 if ($6 == 0)
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002205 GEN_ERROR("Global value initializer is not a constant");
Christopher Lambbf3348d2007-12-12 08:45:45 +00002206 CurGV = ParseGlobalVariable($1, $2, $3, $5, $6->getType(), $6, $4, $7);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002207 CHECK_FOR_ERROR
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002208 } GlobalVarAttributes {
2209 CurGV = 0;
2210 }
Chris Lattner6cdc6822007-04-26 05:31:05 +00002211 | OptGlobalAssign GVExternalLinkage GVVisibilityStyle ThreadLocal GlobalType
Christopher Lambbf3348d2007-12-12 08:45:45 +00002212 Types OptAddrSpace {
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002213 if (!UpRefs.empty())
2214 GEN_ERROR("Invalid upreference in type: " + (*$6)->getDescription());
Christopher Lambbf3348d2007-12-12 08:45:45 +00002215 CurGV = ParseGlobalVariable($1, $2, $3, $5, *$6, 0, $4, $7);
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002216 CHECK_FOR_ERROR
2217 delete $6;
Reid Spencer5b7e7532006-09-28 19:28:24 +00002218 } GlobalVarAttributes {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002219 CurGV = 0;
2220 CHECK_FOR_ERROR
2221 }
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002222 | OptGlobalAssign GVVisibilityStyle ALIAS AliasLinkage AliaseeRef {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002223 std::string Name;
2224 if ($1) {
2225 Name = *$1;
2226 delete $1;
2227 }
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00002228 if (Name.empty())
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002229 GEN_ERROR("Alias name cannot be empty");
Eric Christopher2a5196f2008-09-24 04:55:49 +00002230
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002231 Constant* Aliasee = $5;
2232 if (Aliasee == 0)
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002233 GEN_ERROR(std::string("Invalid aliasee for alias: ") + Name);
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002234
2235 GlobalAlias* GA = new GlobalAlias(Aliasee->getType(), $4, Name, Aliasee,
2236 CurModule.CurrentModule);
2237 GA->setVisibility($2);
2238 InsertValue(GA, CurModule.Values);
Eric Christopher2a5196f2008-09-24 04:55:49 +00002239
2240
Chris Lattner569f7372007-09-10 23:24:14 +00002241 // If there was a forward reference of this alias, resolve it now.
Eric Christopher2a5196f2008-09-24 04:55:49 +00002242
Chris Lattner569f7372007-09-10 23:24:14 +00002243 ValID ID;
2244 if (!Name.empty())
2245 ID = ValID::createGlobalName(Name);
2246 else
2247 ID = ValID::createGlobalID(CurModule.Values.size()-1);
Eric Christopher2a5196f2008-09-24 04:55:49 +00002248
Chris Lattner569f7372007-09-10 23:24:14 +00002249 if (GlobalValue *FWGV =
2250 CurModule.GetForwardRefForGlobal(GA->getType(), ID)) {
2251 // Replace uses of the fwdref with the actual alias.
2252 FWGV->replaceAllUsesWith(GA);
2253 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(FWGV))
2254 GV->eraseFromParent();
2255 else
2256 cast<Function>(FWGV)->eraseFromParent();
2257 }
2258 ID.destroy();
Eric Christopher2a5196f2008-09-24 04:55:49 +00002259
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00002260 CHECK_FOR_ERROR
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00002261 }
Eric Christopher2a5196f2008-09-24 04:55:49 +00002262 | TARGET TargetDefinition {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002263 CHECK_FOR_ERROR
2264 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002265 | DEPLIBS '=' LibrariesDefinition {
Reid Spencer61c83e02006-08-18 08:43:06 +00002266 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002267 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002268 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00002269
2270
2271AsmBlock : STRINGCONSTANT {
2272 const std::string &AsmSoFar = CurModule.CurrentModule->getModuleInlineAsm();
Chris Lattner58af2a12006-02-15 07:22:58 +00002273 if (AsmSoFar.empty())
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002274 CurModule.CurrentModule->setModuleInlineAsm(*$1);
Chris Lattner58af2a12006-02-15 07:22:58 +00002275 else
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002276 CurModule.CurrentModule->setModuleInlineAsm(AsmSoFar+"\n"+*$1);
2277 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002278 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002279};
2280
Reid Spencer41dff5e2007-01-26 08:05:27 +00002281TargetDefinition : TRIPLE '=' STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002282 CurModule.CurrentModule->setTargetTriple(*$3);
2283 delete $3;
John Criswell2f6a8b12006-10-24 19:09:48 +00002284 }
Chris Lattner1ae022f2006-10-22 06:08:13 +00002285 | DATALAYOUT '=' STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002286 CurModule.CurrentModule->setDataLayout(*$3);
2287 delete $3;
Owen Anderson1dc69692006-10-18 02:21:48 +00002288 };
Chris Lattner58af2a12006-02-15 07:22:58 +00002289
2290LibrariesDefinition : '[' LibList ']';
2291
2292LibList : LibList ',' STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002293 CurModule.CurrentModule->addLibrary(*$3);
2294 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002295 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002296 }
2297 | STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002298 CurModule.CurrentModule->addLibrary(*$1);
2299 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002300 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002301 }
2302 | /* empty: end of list */ {
Reid Spencer61c83e02006-08-18 08:43:06 +00002303 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002304 }
2305 ;
2306
2307//===----------------------------------------------------------------------===//
2308// Rules to match Function Headers
2309//===----------------------------------------------------------------------===//
2310
Devang Patel05988662008-09-25 21:00:45 +00002311ArgListH : ArgListH ',' Types OptAttributes OptLocalName {
Reid Spencer14310612006-12-31 05:40:51 +00002312 if (!UpRefs.empty())
2313 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002314 if (!(*$3)->isFirstClassType())
2315 GEN_ERROR("Argument types must be first-class");
Reid Spencer14310612006-12-31 05:40:51 +00002316 ArgListEntry E; E.Attrs = $4; E.Ty = $3; E.Name = $5;
Chris Lattner58af2a12006-02-15 07:22:58 +00002317 $$ = $1;
Reid Spencer14310612006-12-31 05:40:51 +00002318 $1->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002319 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002320 }
Devang Patel05988662008-09-25 21:00:45 +00002321 | Types OptAttributes OptLocalName {
Reid Spencer14310612006-12-31 05:40:51 +00002322 if (!UpRefs.empty())
2323 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002324 if (!(*$1)->isFirstClassType())
2325 GEN_ERROR("Argument types must be first-class");
Reid Spencer14310612006-12-31 05:40:51 +00002326 ArgListEntry E; E.Attrs = $2; E.Ty = $1; E.Name = $3;
2327 $$ = new ArgListType;
2328 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002329 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002330 };
2331
2332ArgList : ArgListH {
2333 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002334 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002335 }
2336 | ArgListH ',' DOTDOTDOT {
2337 $$ = $1;
Reid Spencer14310612006-12-31 05:40:51 +00002338 struct ArgListEntry E;
2339 E.Ty = new PATypeHolder(Type::VoidTy);
2340 E.Name = 0;
Devang Patel05988662008-09-25 21:00:45 +00002341 E.Attrs = Attribute::None;
Reid Spencer14310612006-12-31 05:40:51 +00002342 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002343 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002344 }
2345 | DOTDOTDOT {
Reid Spencer14310612006-12-31 05:40:51 +00002346 $$ = new ArgListType;
2347 struct ArgListEntry E;
2348 E.Ty = new PATypeHolder(Type::VoidTy);
2349 E.Name = 0;
Devang Patel05988662008-09-25 21:00:45 +00002350 E.Attrs = Attribute::None;
Reid Spencer14310612006-12-31 05:40:51 +00002351 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002352 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002353 }
2354 | /* empty */ {
2355 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00002356 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002357 };
2358
Devang Patel652203f2008-09-29 20:49:50 +00002359FunctionHeaderH : OptCallingConv OptRetAttrs ResultTypes GlobalName '(' ArgList ')'
Devang Patel2c9c3e72008-09-26 23:51:19 +00002360 OptFuncAttrs OptSection OptAlign OptGC {
Devang Patel652203f2008-09-29 20:49:50 +00002361 std::string FunctionName(*$4);
2362 delete $4; // Free strdup'd memory!
Eric Christopher2a5196f2008-09-24 04:55:49 +00002363
Reid Spencer8c8a2dc2007-01-02 21:54:12 +00002364 // Check the function result for abstractness if this is a define. We should
2365 // have no abstract types at this point
Devang Patel652203f2008-09-29 20:49:50 +00002366 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved($3))
2367 GEN_ERROR("Reference to abstract result: "+ $3->get()->getDescription());
Reid Spencer8c8a2dc2007-01-02 21:54:12 +00002368
Devang Patel652203f2008-09-29 20:49:50 +00002369 if (!FunctionType::isValidReturnType(*$3))
Chris Lattnera925a142008-04-23 05:37:08 +00002370 GEN_ERROR("Invalid result type for LLVM function");
Eric Christopher2a5196f2008-09-24 04:55:49 +00002371
Chris Lattner58af2a12006-02-15 07:22:58 +00002372 std::vector<const Type*> ParamTypeList;
Devang Patel05988662008-09-25 21:00:45 +00002373 SmallVector<AttributeWithIndex, 8> Attrs;
Devang Patel19c87462008-09-26 22:53:05 +00002374 //FIXME : In 3.0, stop accepting zext, sext and inreg as optional function
2375 //attributes.
Devang Patel652203f2008-09-29 20:49:50 +00002376 Attributes RetAttrs = $2;
2377 if ($8 != Attribute::None) {
2378 if ($8 & Attribute::ZExt) {
Devang Patel19c87462008-09-26 22:53:05 +00002379 RetAttrs = RetAttrs | Attribute::ZExt;
Devang Patel652203f2008-09-29 20:49:50 +00002380 $8 = $8 ^ Attribute::ZExt;
Devang Patel19c87462008-09-26 22:53:05 +00002381 }
Devang Patel652203f2008-09-29 20:49:50 +00002382 if ($8 & Attribute::SExt) {
Devang Patel19c87462008-09-26 22:53:05 +00002383 RetAttrs = RetAttrs | Attribute::SExt;
Devang Patel652203f2008-09-29 20:49:50 +00002384 $8 = $8 ^ Attribute::SExt;
Devang Patel19c87462008-09-26 22:53:05 +00002385 }
Devang Patel652203f2008-09-29 20:49:50 +00002386 if ($8 & Attribute::InReg) {
Devang Patel19c87462008-09-26 22:53:05 +00002387 RetAttrs = RetAttrs | Attribute::InReg;
Devang Patel652203f2008-09-29 20:49:50 +00002388 $8 = $8 ^ Attribute::InReg;
Devang Patel19c87462008-09-26 22:53:05 +00002389 }
Devang Patel19c87462008-09-26 22:53:05 +00002390 }
Devang Patel652203f2008-09-29 20:49:50 +00002391 if (RetAttrs != Attribute::None)
2392 Attrs.push_back(AttributeWithIndex::get(0, RetAttrs));
2393 if ($6) { // If there are arguments...
Reid Spencer7b5d4662007-04-09 06:16:21 +00002394 unsigned index = 1;
Devang Patel652203f2008-09-29 20:49:50 +00002395 for (ArgListType::iterator I = $6->begin(); I != $6->end(); ++I, ++index) {
Reid Spencer14310612006-12-31 05:40:51 +00002396 const Type* Ty = I->Ty->get();
Reid Spencer8c8a2dc2007-01-02 21:54:12 +00002397 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved(I->Ty))
2398 GEN_ERROR("Reference to abstract argument: " + Ty->getDescription());
Reid Spencer14310612006-12-31 05:40:51 +00002399 ParamTypeList.push_back(Ty);
Devang Patel05988662008-09-25 21:00:45 +00002400 if (Ty != Type::VoidTy && I->Attrs != Attribute::None)
2401 Attrs.push_back(AttributeWithIndex::get(index, I->Attrs));
Reid Spencer14310612006-12-31 05:40:51 +00002402 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002403 }
Devang Patel652203f2008-09-29 20:49:50 +00002404 if ($8 != Attribute::None)
2405 Attrs.push_back(AttributeWithIndex::get(~0, $8));
Chris Lattner58af2a12006-02-15 07:22:58 +00002406
2407 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
2408 if (isVarArg) ParamTypeList.pop_back();
2409
Devang Patel05988662008-09-25 21:00:45 +00002410 AttrListPtr PAL;
Christopher Lamb5c104242007-04-22 20:09:11 +00002411 if (!Attrs.empty())
Devang Patel05988662008-09-25 21:00:45 +00002412 PAL = AttrListPtr::get(Attrs.begin(), Attrs.end());
Reid Spencer7b5d4662007-04-09 06:16:21 +00002413
Devang Patel652203f2008-09-29 20:49:50 +00002414 FunctionType *FT = FunctionType::get(*$3, ParamTypeList, isVarArg);
Christopher Lamb4374f8e2007-12-17 01:17:35 +00002415 const PointerType *PFT = PointerType::getUnqual(FT);
Devang Patel652203f2008-09-29 20:49:50 +00002416 delete $3;
Chris Lattner58af2a12006-02-15 07:22:58 +00002417
2418 ValID ID;
2419 if (!FunctionName.empty()) {
Reid Spencer41dff5e2007-01-26 08:05:27 +00002420 ID = ValID::createGlobalName((char*)FunctionName.c_str());
Chris Lattner58af2a12006-02-15 07:22:58 +00002421 } else {
Reid Spencer93c40032007-03-19 18:40:50 +00002422 ID = ValID::createGlobalID(CurModule.Values.size());
Chris Lattner58af2a12006-02-15 07:22:58 +00002423 }
2424
2425 Function *Fn = 0;
2426 // See if this function was forward referenced. If so, recycle the object.
2427 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
Eric Christopher2a5196f2008-09-24 04:55:49 +00002428 // Move the function to the end of the list, from whereever it was
Chris Lattner58af2a12006-02-15 07:22:58 +00002429 // previously inserted.
2430 Fn = cast<Function>(FWRef);
Devang Patel05988662008-09-25 21:00:45 +00002431 assert(Fn->getAttributes().isEmpty() &&
Chris Lattner58d74912008-03-12 17:45:29 +00002432 "Forward reference has parameter attributes!");
Chris Lattner58af2a12006-02-15 07:22:58 +00002433 CurModule.CurrentModule->getFunctionList().remove(Fn);
2434 CurModule.CurrentModule->getFunctionList().push_back(Fn);
2435 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
Reid Spenceref9b9a72007-02-05 20:47:22 +00002436 (Fn = CurModule.CurrentModule->getFunction(FunctionName))) {
Duncan Sandsdc024672007-11-27 13:23:08 +00002437 if (Fn->getFunctionType() != FT ) {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002438 // The existing function doesn't have the same type. This is an overload
2439 // error.
2440 GEN_ERROR("Overload of function '" + FunctionName + "' not permitted.");
Devang Patel05988662008-09-25 21:00:45 +00002441 } else if (Fn->getAttributes() != PAL) {
Duncan Sandsdc024672007-11-27 13:23:08 +00002442 // The existing function doesn't have the same parameter attributes.
2443 // This is an overload error.
2444 GEN_ERROR("Overload of function '" + FunctionName + "' not permitted.");
Reid Spenceref9b9a72007-02-05 20:47:22 +00002445 } else if (!CurFun.isDeclare && !Fn->isDeclaration()) {
Chris Lattner6cdc6822007-04-26 05:31:05 +00002446 // Neither the existing or the current function is a declaration and they
2447 // have the same name and same type. Clearly this is a redefinition.
2448 GEN_ERROR("Redefinition of function '" + FunctionName + "'");
Duncan Sandsdc024672007-11-27 13:23:08 +00002449 } else if (Fn->isDeclaration()) {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002450 // Make sure to strip off any argument names so we can't get conflicts.
Chris Lattner58af2a12006-02-15 07:22:58 +00002451 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
2452 AI != AE; ++AI)
2453 AI->setName("");
Reid Spenceref9b9a72007-02-05 20:47:22 +00002454 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002455 } else { // Not already defined?
Gabor Greife64d2482008-04-06 23:07:54 +00002456 Fn = Function::Create(FT, GlobalValue::ExternalWeakLinkage, FunctionName,
2457 CurModule.CurrentModule);
Chris Lattner58af2a12006-02-15 07:22:58 +00002458 InsertValue(Fn, CurModule.Values);
2459 }
2460
Nuno Lopes9e9631d2008-10-03 15:45:58 +00002461 ID.destroy();
Chris Lattner58af2a12006-02-15 07:22:58 +00002462 CurFun.FunctionStart(Fn);
Anton Korobeynikov93c2b372006-09-17 13:06:18 +00002463
2464 if (CurFun.isDeclare) {
2465 // If we have declaration, always overwrite linkage. This will allow us to
2466 // correctly handle cases, when pointer to function is passed as argument to
2467 // another function.
2468 Fn->setLinkage(CurFun.Linkage);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002469 Fn->setVisibility(CurFun.Visibility);
Anton Korobeynikov93c2b372006-09-17 13:06:18 +00002470 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002471 Fn->setCallingConv($1);
Devang Patel05988662008-09-25 21:00:45 +00002472 Fn->setAttributes(PAL);
Devang Patel652203f2008-09-29 20:49:50 +00002473 Fn->setAlignment($10);
2474 if ($9) {
2475 Fn->setSection(*$9);
2476 delete $9;
Chris Lattner58af2a12006-02-15 07:22:58 +00002477 }
Devang Patel652203f2008-09-29 20:49:50 +00002478 if ($11) {
2479 Fn->setGC($11->c_str());
2480 delete $11;
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00002481 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002482
2483 // Add all of the arguments we parsed to the function...
Devang Patel652203f2008-09-29 20:49:50 +00002484 if ($6) { // Is null if empty...
Chris Lattner58af2a12006-02-15 07:22:58 +00002485 if (isVarArg) { // Nuke the last entry
Devang Patel652203f2008-09-29 20:49:50 +00002486 assert($6->back().Ty->get() == Type::VoidTy && $6->back().Name == 0 &&
Reid Spencera9720f52007-02-05 17:04:00 +00002487 "Not a varargs marker!");
Devang Patel652203f2008-09-29 20:49:50 +00002488 delete $6->back().Ty;
2489 $6->pop_back(); // Delete the last entry
Chris Lattner58af2a12006-02-15 07:22:58 +00002490 }
2491 Function::arg_iterator ArgIt = Fn->arg_begin();
Reid Spenceref9b9a72007-02-05 20:47:22 +00002492 Function::arg_iterator ArgEnd = Fn->arg_end();
Reid Spencer14310612006-12-31 05:40:51 +00002493 unsigned Idx = 1;
Devang Patel652203f2008-09-29 20:49:50 +00002494 for (ArgListType::iterator I = $6->begin();
2495 I != $6->end() && ArgIt != ArgEnd; ++I, ++ArgIt) {
Reid Spencer14310612006-12-31 05:40:51 +00002496 delete I->Ty; // Delete the typeholder...
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002497 setValueName(ArgIt, I->Name); // Insert arg into symtab...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002498 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002499 InsertValue(ArgIt);
Reid Spencer14310612006-12-31 05:40:51 +00002500 Idx++;
Chris Lattner58af2a12006-02-15 07:22:58 +00002501 }
Reid Spencera132e042006-12-03 05:46:11 +00002502
Devang Patel652203f2008-09-29 20:49:50 +00002503 delete $6; // We're now done with the argument list
Chris Lattner58af2a12006-02-15 07:22:58 +00002504 }
Reid Spencer61c83e02006-08-18 08:43:06 +00002505 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002506};
2507
2508BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
2509
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002510FunctionHeader : FunctionDefineLinkage GVVisibilityStyle FunctionHeaderH BEGIN {
Chris Lattner58af2a12006-02-15 07:22:58 +00002511 $$ = CurFun.CurrentFunction;
2512
2513 // Make sure that we keep track of the linkage type even if there was a
2514 // previous "declare".
2515 $$->setLinkage($1);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002516 $$->setVisibility($2);
Chris Lattner58af2a12006-02-15 07:22:58 +00002517};
2518
2519END : ENDTOK | '}'; // Allow end of '}' to end a function
2520
2521Function : BasicBlockList END {
2522 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002523 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002524};
2525
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002526FunctionProto : FunctionDeclareLinkage GVVisibilityStyle FunctionHeaderH {
Reid Spencer14310612006-12-31 05:40:51 +00002527 CurFun.CurrentFunction->setLinkage($1);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002528 CurFun.CurrentFunction->setVisibility($2);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002529 $$ = CurFun.CurrentFunction;
2530 CurFun.FunctionDone();
2531 CHECK_FOR_ERROR
2532 };
Chris Lattner58af2a12006-02-15 07:22:58 +00002533
2534//===----------------------------------------------------------------------===//
2535// Rules to match Basic Blocks
2536//===----------------------------------------------------------------------===//
2537
2538OptSideEffect : /* empty */ {
2539 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002540 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002541 }
2542 | SIDEEFFECT {
2543 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002544 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002545 };
2546
2547ConstValueRef : ESINT64VAL { // A reference to a direct constant
2548 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002549 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002550 }
2551 | EUINT64VAL {
2552 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002553 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002554 }
Chris Lattner1913b942008-07-11 00:30:39 +00002555 | ESAPINTVAL { // arbitrary precision integer constants
2556 $$ = ValID::create(*$1, true);
2557 delete $1;
2558 CHECK_FOR_ERROR
Eric Christopher2a5196f2008-09-24 04:55:49 +00002559 }
Chris Lattner1913b942008-07-11 00:30:39 +00002560 | EUAPINTVAL { // arbitrary precision integer constants
2561 $$ = ValID::create(*$1, false);
2562 delete $1;
2563 CHECK_FOR_ERROR
2564 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002565 | FPVAL { // Perhaps it's an FP constant?
2566 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002567 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002568 }
2569 | TRUETOK {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00002570 $$ = ValID::create(ConstantInt::getTrue());
Reid Spencer61c83e02006-08-18 08:43:06 +00002571 CHECK_FOR_ERROR
Eric Christopher2a5196f2008-09-24 04:55:49 +00002572 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002573 | FALSETOK {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00002574 $$ = ValID::create(ConstantInt::getFalse());
Reid Spencer61c83e02006-08-18 08:43:06 +00002575 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002576 }
2577 | NULL_TOK {
2578 $$ = ValID::createNull();
Reid Spencer61c83e02006-08-18 08:43:06 +00002579 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002580 }
2581 | UNDEF {
2582 $$ = ValID::createUndef();
Reid Spencer61c83e02006-08-18 08:43:06 +00002583 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002584 }
2585 | ZEROINITIALIZER { // A vector zero constant.
2586 $$ = ValID::createZeroInit();
Reid Spencer61c83e02006-08-18 08:43:06 +00002587 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002588 }
2589 | '<' ConstVector '>' { // Nonempty unsized packed vector
Reid Spencera132e042006-12-03 05:46:11 +00002590 const Type *ETy = (*$2)[0]->getType();
Eric Christopher2a5196f2008-09-24 04:55:49 +00002591 unsigned NumElements = $2->size();
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002592
2593 if (!ETy->isInteger() && !ETy->isFloatingPoint())
2594 GEN_ERROR("Invalid vector element type: " + ETy->getDescription());
Eric Christopher2a5196f2008-09-24 04:55:49 +00002595
Reid Spencer9d6565a2007-02-15 02:26:10 +00002596 VectorType* pt = VectorType::get(ETy, NumElements);
Dan Gohmanf910eaa2008-06-09 14:45:02 +00002597 PATypeHolder* PTy = new PATypeHolder(HandleUpRefs(pt));
Eric Christopher2a5196f2008-09-24 04:55:49 +00002598
Chris Lattner58af2a12006-02-15 07:22:58 +00002599 // Verify all elements are correct type!
2600 for (unsigned i = 0; i < $2->size(); i++) {
Reid Spencera132e042006-12-03 05:46:11 +00002601 if (ETy != (*$2)[i]->getType())
Eric Christopher2a5196f2008-09-24 04:55:49 +00002602 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00002603 ETy->getDescription() +"' as required!\nIt is of type '" +
Reid Spencera132e042006-12-03 05:46:11 +00002604 (*$2)[i]->getType()->getDescription() + "'.");
Chris Lattner58af2a12006-02-15 07:22:58 +00002605 }
2606
Reid Spencer9d6565a2007-02-15 02:26:10 +00002607 $$ = ValID::create(ConstantVector::get(pt, *$2));
Chris Lattner58af2a12006-02-15 07:22:58 +00002608 delete PTy; delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002609 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002610 }
Dan Gohmanf910eaa2008-06-09 14:45:02 +00002611 | '[' ConstVector ']' { // Nonempty unsized arr
2612 const Type *ETy = (*$2)[0]->getType();
Eric Christopher2a5196f2008-09-24 04:55:49 +00002613 uint64_t NumElements = $2->size();
Dan Gohmanf910eaa2008-06-09 14:45:02 +00002614
2615 if (!ETy->isFirstClassType())
2616 GEN_ERROR("Invalid array element type: " + ETy->getDescription());
2617
2618 ArrayType *ATy = ArrayType::get(ETy, NumElements);
2619 PATypeHolder* PTy = new PATypeHolder(HandleUpRefs(ATy));
2620
2621 // Verify all elements are correct type!
2622 for (unsigned i = 0; i < $2->size(); i++) {
2623 if (ETy != (*$2)[i]->getType())
Eric Christopher2a5196f2008-09-24 04:55:49 +00002624 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Dan Gohmanf910eaa2008-06-09 14:45:02 +00002625 ETy->getDescription() +"' as required!\nIt is of type '"+
2626 (*$2)[i]->getType()->getDescription() + "'.");
2627 }
2628
2629 $$ = ValID::create(ConstantArray::get(ATy, *$2));
2630 delete PTy; delete $2;
2631 CHECK_FOR_ERROR
2632 }
2633 | '[' ']' {
Dan Gohman180c1692008-06-23 18:43:26 +00002634 // Use undef instead of an array because it's inconvenient to determine
2635 // the element type at this point, there being no elements to examine.
Dan Gohmanf910eaa2008-06-09 14:45:02 +00002636 $$ = ValID::createUndef();
2637 CHECK_FOR_ERROR
2638 }
2639 | 'c' STRINGCONSTANT {
Dan Gohman180c1692008-06-23 18:43:26 +00002640 uint64_t NumElements = $2->length();
Dan Gohmanf910eaa2008-06-09 14:45:02 +00002641 const Type *ETy = Type::Int8Ty;
2642
2643 ArrayType *ATy = ArrayType::get(ETy, NumElements);
2644
2645 std::vector<Constant*> Vals;
2646 for (unsigned i = 0; i < $2->length(); ++i)
2647 Vals.push_back(ConstantInt::get(ETy, (*$2)[i]));
2648 delete $2;
2649 $$ = ValID::create(ConstantArray::get(ATy, Vals));
2650 CHECK_FOR_ERROR
2651 }
2652 | '{' ConstVector '}' {
2653 std::vector<const Type*> Elements($2->size());
2654 for (unsigned i = 0, e = $2->size(); i != e; ++i)
2655 Elements[i] = (*$2)[i]->getType();
2656
2657 const StructType *STy = StructType::get(Elements);
2658 PATypeHolder* PTy = new PATypeHolder(HandleUpRefs(STy));
2659
2660 $$ = ValID::create(ConstantStruct::get(STy, *$2));
2661 delete PTy; delete $2;
2662 CHECK_FOR_ERROR
2663 }
2664 | '{' '}' {
2665 const StructType *STy = StructType::get(std::vector<const Type*>());
2666 $$ = ValID::create(ConstantStruct::get(STy, std::vector<Constant*>()));
2667 CHECK_FOR_ERROR
2668 }
2669 | '<' '{' ConstVector '}' '>' {
2670 std::vector<const Type*> Elements($3->size());
2671 for (unsigned i = 0, e = $3->size(); i != e; ++i)
2672 Elements[i] = (*$3)[i]->getType();
2673
2674 const StructType *STy = StructType::get(Elements, /*isPacked=*/true);
2675 PATypeHolder* PTy = new PATypeHolder(HandleUpRefs(STy));
2676
2677 $$ = ValID::create(ConstantStruct::get(STy, *$3));
2678 delete PTy; delete $3;
2679 CHECK_FOR_ERROR
2680 }
2681 | '<' '{' '}' '>' {
2682 const StructType *STy = StructType::get(std::vector<const Type*>(),
2683 /*isPacked=*/true);
2684 $$ = ValID::create(ConstantStruct::get(STy, std::vector<Constant*>()));
2685 CHECK_FOR_ERROR
2686 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002687 | ConstExpr {
Reid Spencera132e042006-12-03 05:46:11 +00002688 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002689 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002690 }
2691 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002692 $$ = ValID::createInlineAsm(*$3, *$5, $2);
2693 delete $3;
2694 delete $5;
Reid Spencer61c83e02006-08-18 08:43:06 +00002695 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002696 };
2697
2698// SymbolicValueRef - Reference to one of two ways of symbolically refering to
2699// another value.
2700//
Reid Spencer41dff5e2007-01-26 08:05:27 +00002701SymbolicValueRef : LOCALVAL_ID { // Is it an integer reference...?
2702 $$ = ValID::createLocalID($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002703 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002704 }
Reid Spencer41dff5e2007-01-26 08:05:27 +00002705 | GLOBALVAL_ID {
2706 $$ = ValID::createGlobalID($1);
2707 CHECK_FOR_ERROR
2708 }
2709 | LocalName { // Is it a named reference...?
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002710 $$ = ValID::createLocalName(*$1);
2711 delete $1;
Reid Spencer41dff5e2007-01-26 08:05:27 +00002712 CHECK_FOR_ERROR
2713 }
2714 | GlobalName { // Is it a named reference...?
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002715 $$ = ValID::createGlobalName(*$1);
2716 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002717 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002718 };
2719
2720// ValueRef - A reference to a definition... either constant or symbolic
2721ValueRef : SymbolicValueRef | ConstValueRef;
2722
2723
2724// ResolvedVal - a <type> <value> pair. This is used only in cases where the
2725// type immediately preceeds the value reference, and allows complex constant
2726// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
2727ResolvedVal : Types ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00002728 if (!UpRefs.empty())
2729 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Eric Christopher2a5196f2008-09-24 04:55:49 +00002730 $$ = getVal(*$1, $2);
Reid Spencer14310612006-12-31 05:40:51 +00002731 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002732 CHECK_FOR_ERROR
Reid Spencer14310612006-12-31 05:40:51 +00002733 }
2734 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00002735
Devang Patel7990dc72008-02-20 22:40:23 +00002736ReturnedVal : ResolvedVal {
2737 $$ = new std::vector<Value *>();
Eric Christopher2a5196f2008-09-24 04:55:49 +00002738 $$->push_back($1);
Devang Patel7990dc72008-02-20 22:40:23 +00002739 CHECK_FOR_ERROR
2740 }
Devang Patel6bfc63b2008-02-23 00:38:56 +00002741 | ReturnedVal ',' ResolvedVal {
Eric Christopher2a5196f2008-09-24 04:55:49 +00002742 ($$=$1)->push_back($3);
Devang Patel7990dc72008-02-20 22:40:23 +00002743 CHECK_FOR_ERROR
2744 };
2745
Chris Lattner58af2a12006-02-15 07:22:58 +00002746BasicBlockList : BasicBlockList BasicBlock {
2747 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002748 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002749 }
Eric Christopher2a5196f2008-09-24 04:55:49 +00002750 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
Chris Lattner58af2a12006-02-15 07:22:58 +00002751 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002752 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002753 };
2754
2755
Eric Christopher2a5196f2008-09-24 04:55:49 +00002756// Basic blocks are terminated by branching instructions:
Chris Lattner58af2a12006-02-15 07:22:58 +00002757// br, br/cc, switch, ret
2758//
Chris Lattner15bd0952008-08-29 17:20:18 +00002759BasicBlock : InstructionList OptLocalAssign BBTerminatorInst {
Chris Lattner58af2a12006-02-15 07:22:58 +00002760 setValueName($3, $2);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002761 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002762 InsertValue($3);
Chris Lattner58af2a12006-02-15 07:22:58 +00002763 $1->getInstList().push_back($3);
Chris Lattner58af2a12006-02-15 07:22:58 +00002764 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002765 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002766 };
2767
Chris Lattner15bd0952008-08-29 17:20:18 +00002768BasicBlock : InstructionList LocalNumber BBTerminatorInst {
2769 CHECK_FOR_ERROR
2770 int ValNum = InsertValue($3);
2771 if (ValNum != (int)$2)
2772 GEN_ERROR("Result value number %" + utostr($2) +
2773 " is incorrect, expected %" + utostr((unsigned)ValNum));
Eric Christopher2a5196f2008-09-24 04:55:49 +00002774
Chris Lattner15bd0952008-08-29 17:20:18 +00002775 $1->getInstList().push_back($3);
2776 $$ = $1;
2777 CHECK_FOR_ERROR
2778};
2779
2780
Chris Lattner58af2a12006-02-15 07:22:58 +00002781InstructionList : InstructionList Inst {
Reid Spencer3da59db2006-11-27 01:05:10 +00002782 if (CastInst *CI1 = dyn_cast<CastInst>($2))
2783 if (CastInst *CI2 = dyn_cast<CastInst>(CI1->getOperand(0)))
2784 if (CI2->getParent() == 0)
2785 $1->getInstList().push_back(CI2);
Chris Lattner58af2a12006-02-15 07:22:58 +00002786 $1->getInstList().push_back($2);
2787 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002788 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002789 }
Reid Spencer93c40032007-03-19 18:40:50 +00002790 | /* empty */ { // Empty space between instruction lists
Nick Lewycky280a6e62008-04-25 16:53:59 +00002791 $$ = defineBBVal(ValID::createLocalID(CurFun.NextValNum));
Reid Spencer61c83e02006-08-18 08:43:06 +00002792 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002793 }
Reid Spencer93c40032007-03-19 18:40:50 +00002794 | LABELSTR { // Labelled (named) basic block
Nick Lewycky280a6e62008-04-25 16:53:59 +00002795 $$ = defineBBVal(ValID::createLocalName(*$1));
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002796 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002797 CHECK_FOR_ERROR
Nick Lewycky280a6e62008-04-25 16:53:59 +00002798
Chris Lattner58af2a12006-02-15 07:22:58 +00002799 };
2800
Eric Christopher2a5196f2008-09-24 04:55:49 +00002801BBTerminatorInst :
Devang Patel7990dc72008-02-20 22:40:23 +00002802 RET ReturnedVal { // Return with a result...
Devang Patelb82b7f22008-02-26 22:17:48 +00002803 ValueList &VL = *$2;
Devang Patel13b823c2008-02-26 23:19:08 +00002804 assert(!VL.empty() && "Invalid ret operands!");
Dan Gohman1a570242008-07-23 00:54:54 +00002805 const Type *ReturnType = CurFun.CurrentFunction->getReturnType();
2806 if (VL.size() > 1 ||
2807 (isa<StructType>(ReturnType) &&
2808 (VL.empty() || VL[0]->getType() != ReturnType))) {
2809 Value *RV = UndefValue::get(ReturnType);
2810 for (unsigned i = 0, e = VL.size(); i != e; ++i) {
2811 Instruction *I = InsertValueInst::Create(RV, VL[i], i, "mrv");
2812 ($<BasicBlockVal>-1)->getInstList().push_back(I);
2813 RV = I;
2814 }
2815 $$ = ReturnInst::Create(RV);
2816 } else {
2817 $$ = ReturnInst::Create(VL[0]);
2818 }
Devang Patel7990dc72008-02-20 22:40:23 +00002819 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002820 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002821 }
Reid Spencer93c40032007-03-19 18:40:50 +00002822 | RET VOID { // Return with no result...
Gabor Greife64d2482008-04-06 23:07:54 +00002823 $$ = ReturnInst::Create();
Reid Spencer61c83e02006-08-18 08:43:06 +00002824 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002825 }
Reid Spencer93c40032007-03-19 18:40:50 +00002826 | BR LABEL ValueRef { // Unconditional Branch...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002827 BasicBlock* tmpBB = getBBVal($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002828 CHECK_FOR_ERROR
Gabor Greife64d2482008-04-06 23:07:54 +00002829 $$ = BranchInst::Create(tmpBB);
Reid Spencer93c40032007-03-19 18:40:50 +00002830 } // Conditional Branch...
Eric Christopher2a5196f2008-09-24 04:55:49 +00002831 | BR INTTYPE ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002832 if (cast<IntegerType>($2)->getBitWidth() != 1)
2833 GEN_ERROR("Branch condition must have type i1");
Reid Spencer5b7e7532006-09-28 19:28:24 +00002834 BasicBlock* tmpBBA = getBBVal($6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002835 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002836 BasicBlock* tmpBBB = getBBVal($9);
2837 CHECK_FOR_ERROR
Reid Spencer4fe16d62007-01-11 18:21:29 +00002838 Value* tmpVal = getVal(Type::Int1Ty, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002839 CHECK_FOR_ERROR
Gabor Greife64d2482008-04-06 23:07:54 +00002840 $$ = BranchInst::Create(tmpBBA, tmpBBB, tmpVal);
Chris Lattner58af2a12006-02-15 07:22:58 +00002841 }
Chris Lattnerf9078f92008-10-15 06:03:48 +00002842 | SWITCH INTTYPE ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencera132e042006-12-03 05:46:11 +00002843 Value* tmpVal = getVal($2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002844 CHECK_FOR_ERROR
2845 BasicBlock* tmpBB = getBBVal($6);
2846 CHECK_FOR_ERROR
Gabor Greife64d2482008-04-06 23:07:54 +00002847 SwitchInst *S = SwitchInst::Create(tmpVal, tmpBB, $8->size());
Chris Lattner58af2a12006-02-15 07:22:58 +00002848 $$ = S;
2849
2850 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
2851 E = $8->end();
2852 for (; I != E; ++I) {
2853 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
2854 S->addCase(CI, I->second);
2855 else
Reid Spencerb5334b02007-02-05 10:18:06 +00002856 GEN_ERROR("Switch case is constant, but not a simple integer");
Chris Lattner58af2a12006-02-15 07:22:58 +00002857 }
2858 delete $8;
Reid Spencer61c83e02006-08-18 08:43:06 +00002859 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002860 }
Chris Lattnerf9078f92008-10-15 06:03:48 +00002861 | SWITCH INTTYPE ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencera132e042006-12-03 05:46:11 +00002862 Value* tmpVal = getVal($2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002863 CHECK_FOR_ERROR
2864 BasicBlock* tmpBB = getBBVal($6);
2865 CHECK_FOR_ERROR
Gabor Greife64d2482008-04-06 23:07:54 +00002866 SwitchInst *S = SwitchInst::Create(tmpVal, tmpBB, 0);
Chris Lattner58af2a12006-02-15 07:22:58 +00002867 $$ = S;
Reid Spencer61c83e02006-08-18 08:43:06 +00002868 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002869 }
Devang Patel652203f2008-09-29 20:49:50 +00002870 | INVOKE OptCallingConv OptRetAttrs ResultTypes ValueRef '(' ParamList ')'
2871 OptFuncAttrs TO LABEL ValueRef UNWIND LABEL ValueRef {
Chris Lattner58af2a12006-02-15 07:22:58 +00002872
Reid Spencer14310612006-12-31 05:40:51 +00002873 // Handle the short syntax
2874 const PointerType *PFTy = 0;
2875 const FunctionType *Ty = 0;
Devang Patel652203f2008-09-29 20:49:50 +00002876 if (!(PFTy = dyn_cast<PointerType>($4->get())) ||
Chris Lattner58af2a12006-02-15 07:22:58 +00002877 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
2878 // Pull out the types of all of the arguments...
2879 std::vector<const Type*> ParamTypes;
Devang Patel652203f2008-09-29 20:49:50 +00002880 ParamList::iterator I = $7->begin(), E = $7->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00002881 for (; I != E; ++I) {
Reid Spencer14310612006-12-31 05:40:51 +00002882 const Type *Ty = I->Val->getType();
2883 if (Ty == Type::VoidTy)
2884 GEN_ERROR("Short call syntax cannot be used with varargs");
2885 ParamTypes.push_back(Ty);
Chris Lattner58af2a12006-02-15 07:22:58 +00002886 }
Eric Christopher2a5196f2008-09-24 04:55:49 +00002887
Devang Patel652203f2008-09-29 20:49:50 +00002888 if (!FunctionType::isValidReturnType(*$4))
Chris Lattnera925a142008-04-23 05:37:08 +00002889 GEN_ERROR("Invalid result type for LLVM function");
2890
Devang Patel652203f2008-09-29 20:49:50 +00002891 Ty = FunctionType::get($4->get(), ParamTypes, false);
Christopher Lamb4374f8e2007-12-17 01:17:35 +00002892 PFTy = PointerType::getUnqual(Ty);
Chris Lattner58af2a12006-02-15 07:22:58 +00002893 }
2894
Devang Patel652203f2008-09-29 20:49:50 +00002895 delete $4;
Reid Spencer66728ef2007-03-20 01:13:36 +00002896
Devang Patel652203f2008-09-29 20:49:50 +00002897 Value *V = getVal(PFTy, $5); // Get the function we're calling...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002898 CHECK_FOR_ERROR
Devang Patel652203f2008-09-29 20:49:50 +00002899 BasicBlock *Normal = getBBVal($12);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002900 CHECK_FOR_ERROR
Devang Patel652203f2008-09-29 20:49:50 +00002901 BasicBlock *Except = getBBVal($15);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002902 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002903
Devang Patel05988662008-09-25 21:00:45 +00002904 SmallVector<AttributeWithIndex, 8> Attrs;
Devang Patel19c87462008-09-26 22:53:05 +00002905 //FIXME : In 3.0, stop accepting zext, sext and inreg as optional function
2906 //attributes.
Devang Patel652203f2008-09-29 20:49:50 +00002907 Attributes RetAttrs = $3;
2908 if ($9 != Attribute::None) {
2909 if ($9 & Attribute::ZExt) {
Devang Patel19c87462008-09-26 22:53:05 +00002910 RetAttrs = RetAttrs | Attribute::ZExt;
Devang Patel652203f2008-09-29 20:49:50 +00002911 $9 = $9 ^ Attribute::ZExt;
Devang Patel19c87462008-09-26 22:53:05 +00002912 }
Devang Patel652203f2008-09-29 20:49:50 +00002913 if ($9 & Attribute::SExt) {
Devang Patel19c87462008-09-26 22:53:05 +00002914 RetAttrs = RetAttrs | Attribute::SExt;
Devang Patel652203f2008-09-29 20:49:50 +00002915 $9 = $9 ^ Attribute::SExt;
Devang Patel19c87462008-09-26 22:53:05 +00002916 }
Devang Patel652203f2008-09-29 20:49:50 +00002917 if ($9 & Attribute::InReg) {
Devang Patel19c87462008-09-26 22:53:05 +00002918 RetAttrs = RetAttrs | Attribute::InReg;
Devang Patel652203f2008-09-29 20:49:50 +00002919 $9 = $9 ^ Attribute::InReg;
Devang Patel19c87462008-09-26 22:53:05 +00002920 }
Devang Patel19c87462008-09-26 22:53:05 +00002921 }
Devang Patel652203f2008-09-29 20:49:50 +00002922 if (RetAttrs != Attribute::None)
2923 Attrs.push_back(AttributeWithIndex::get(0, RetAttrs));
Devang Patel19c87462008-09-26 22:53:05 +00002924
Reid Spencer14310612006-12-31 05:40:51 +00002925 // Check the arguments
2926 ValueList Args;
Devang Patel652203f2008-09-29 20:49:50 +00002927 if ($7->empty()) { // Has no arguments?
Reid Spencer14310612006-12-31 05:40:51 +00002928 // Make sure no arguments is a good thing!
2929 if (Ty->getNumParams() != 0)
2930 GEN_ERROR("No arguments passed to a function that "
Reid Spencerb5334b02007-02-05 10:18:06 +00002931 "expects arguments");
Chris Lattner58af2a12006-02-15 07:22:58 +00002932 } else { // Has arguments?
2933 // Loop through FunctionType's arguments and ensure they are specified
2934 // correctly!
Chris Lattner58af2a12006-02-15 07:22:58 +00002935 FunctionType::param_iterator I = Ty->param_begin();
2936 FunctionType::param_iterator E = Ty->param_end();
Devang Patel652203f2008-09-29 20:49:50 +00002937 ParamList::iterator ArgI = $7->begin(), ArgE = $7->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00002938 unsigned index = 1;
Chris Lattner58af2a12006-02-15 07:22:58 +00002939
Duncan Sandsdc024672007-11-27 13:23:08 +00002940 for (; ArgI != ArgE && I != E; ++ArgI, ++I, ++index) {
Reid Spencer14310612006-12-31 05:40:51 +00002941 if (ArgI->Val->getType() != *I)
2942 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00002943 (*I)->getDescription() + "'");
Reid Spencer14310612006-12-31 05:40:51 +00002944 Args.push_back(ArgI->Val);
Devang Patel05988662008-09-25 21:00:45 +00002945 if (ArgI->Attrs != Attribute::None)
2946 Attrs.push_back(AttributeWithIndex::get(index, ArgI->Attrs));
Reid Spencer14310612006-12-31 05:40:51 +00002947 }
Reid Spencera132e042006-12-03 05:46:11 +00002948
Reid Spencer14310612006-12-31 05:40:51 +00002949 if (Ty->isVarArg()) {
2950 if (I == E)
Chris Lattner38905612008-02-19 04:36:25 +00002951 for (; ArgI != ArgE; ++ArgI, ++index) {
Reid Spencer14310612006-12-31 05:40:51 +00002952 Args.push_back(ArgI->Val); // push the remaining varargs
Devang Patel05988662008-09-25 21:00:45 +00002953 if (ArgI->Attrs != Attribute::None)
2954 Attrs.push_back(AttributeWithIndex::get(index, ArgI->Attrs));
Chris Lattner38905612008-02-19 04:36:25 +00002955 }
Reid Spencer14310612006-12-31 05:40:51 +00002956 } else if (I != E || ArgI != ArgE)
Reid Spencerb5334b02007-02-05 10:18:06 +00002957 GEN_ERROR("Invalid number of parameters detected");
Chris Lattner58af2a12006-02-15 07:22:58 +00002958 }
Devang Patel652203f2008-09-29 20:49:50 +00002959 if ($9 != Attribute::None)
2960 Attrs.push_back(AttributeWithIndex::get(~0, $9));
Devang Patel05988662008-09-25 21:00:45 +00002961 AttrListPtr PAL;
Duncan Sandsdc024672007-11-27 13:23:08 +00002962 if (!Attrs.empty())
Devang Patel05988662008-09-25 21:00:45 +00002963 PAL = AttrListPtr::get(Attrs.begin(), Attrs.end());
Duncan Sandsdc024672007-11-27 13:23:08 +00002964
Reid Spencer14310612006-12-31 05:40:51 +00002965 // Create the InvokeInst
Dan Gohman041e2eb2008-05-15 19:50:34 +00002966 InvokeInst *II = InvokeInst::Create(V, Normal, Except,
2967 Args.begin(), Args.end());
Reid Spencer14310612006-12-31 05:40:51 +00002968 II->setCallingConv($2);
Devang Patel05988662008-09-25 21:00:45 +00002969 II->setAttributes(PAL);
Reid Spencer14310612006-12-31 05:40:51 +00002970 $$ = II;
Devang Patel652203f2008-09-29 20:49:50 +00002971 delete $7;
Reid Spencer61c83e02006-08-18 08:43:06 +00002972 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002973 }
2974 | UNWIND {
2975 $$ = new UnwindInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002976 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002977 }
2978 | UNREACHABLE {
2979 $$ = new UnreachableInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002980 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002981 };
2982
2983
2984
Chris Lattnerf9078f92008-10-15 06:03:48 +00002985JumpTable : JumpTable INTTYPE ConstValueRef ',' LABEL ValueRef {
Chris Lattner58af2a12006-02-15 07:22:58 +00002986 $$ = $1;
Reid Spencer93c40032007-03-19 18:40:50 +00002987 Constant *V = cast<Constant>(getExistingVal($2, $3));
Reid Spencer5b7e7532006-09-28 19:28:24 +00002988 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002989 if (V == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002990 GEN_ERROR("May only switch on a constant pool value");
Chris Lattner58af2a12006-02-15 07:22:58 +00002991
Reid Spencer5b7e7532006-09-28 19:28:24 +00002992 BasicBlock* tmpBB = getBBVal($6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002993 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002994 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner58af2a12006-02-15 07:22:58 +00002995 }
Chris Lattnerf9078f92008-10-15 06:03:48 +00002996 | INTTYPE ConstValueRef ',' LABEL ValueRef {
Chris Lattner58af2a12006-02-15 07:22:58 +00002997 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Reid Spencer93c40032007-03-19 18:40:50 +00002998 Constant *V = cast<Constant>(getExistingVal($1, $2));
Reid Spencer5b7e7532006-09-28 19:28:24 +00002999 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003000
3001 if (V == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00003002 GEN_ERROR("May only switch on a constant pool value");
Chris Lattner58af2a12006-02-15 07:22:58 +00003003
Reid Spencer5b7e7532006-09-28 19:28:24 +00003004 BasicBlock* tmpBB = getBBVal($5);
Reid Spencer61c83e02006-08-18 08:43:06 +00003005 CHECK_FOR_ERROR
Eric Christopher2a5196f2008-09-24 04:55:49 +00003006 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner58af2a12006-02-15 07:22:58 +00003007 };
3008
Reid Spencer41dff5e2007-01-26 08:05:27 +00003009Inst : OptLocalAssign InstVal {
Reid Spenceref9b9a72007-02-05 20:47:22 +00003010 // Is this definition named?? if so, assign the name...
3011 setValueName($2, $1);
3012 CHECK_FOR_ERROR
3013 InsertValue($2);
3014 $$ = $2;
3015 CHECK_FOR_ERROR
3016 };
3017
Chris Lattner15bd0952008-08-29 17:20:18 +00003018Inst : LocalNumber InstVal {
3019 CHECK_FOR_ERROR
3020 int ValNum = InsertValue($2);
Eric Christopher2a5196f2008-09-24 04:55:49 +00003021
Chris Lattner15bd0952008-08-29 17:20:18 +00003022 if (ValNum != (int)$1)
3023 GEN_ERROR("Result value number %" + utostr($1) +
3024 " is incorrect, expected %" + utostr((unsigned)ValNum));
3025
3026 $$ = $2;
3027 CHECK_FOR_ERROR
3028 };
3029
Chris Lattner58af2a12006-02-15 07:22:58 +00003030
3031PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Reid Spencer14310612006-12-31 05:40:51 +00003032 if (!UpRefs.empty())
3033 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00003034 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Reid Spencera132e042006-12-03 05:46:11 +00003035 Value* tmpVal = getVal(*$1, $3);
Reid Spencer61c83e02006-08-18 08:43:06 +00003036 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00003037 BasicBlock* tmpBB = getBBVal($5);
3038 CHECK_FOR_ERROR
3039 $$->push_back(std::make_pair(tmpVal, tmpBB));
Reid Spencera132e042006-12-03 05:46:11 +00003040 delete $1;
Chris Lattner58af2a12006-02-15 07:22:58 +00003041 }
3042 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
3043 $$ = $1;
Reid Spencer5b7e7532006-09-28 19:28:24 +00003044 Value* tmpVal = getVal($1->front().first->getType(), $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00003045 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00003046 BasicBlock* tmpBB = getBBVal($6);
3047 CHECK_FOR_ERROR
3048 $1->push_back(std::make_pair(tmpVal, tmpBB));
Chris Lattner58af2a12006-02-15 07:22:58 +00003049 };
3050
3051
Devang Patel05988662008-09-25 21:00:45 +00003052ParamList : Types OptAttributes ValueRef OptAttributes {
3053 // FIXME: Remove trailing OptAttributes in LLVM 3.0, it was a mistake in 2.0
Reid Spencer14310612006-12-31 05:40:51 +00003054 if (!UpRefs.empty())
3055 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
3056 // Used for call and invoke instructions
Dale Johanneseneb57ea72007-11-05 21:20:28 +00003057 $$ = new ParamList();
Duncan Sandsdc024672007-11-27 13:23:08 +00003058 ParamListEntry E; E.Attrs = $2 | $4; E.Val = getVal($1->get(), $3);
Reid Spencer14310612006-12-31 05:40:51 +00003059 $$->push_back(E);
Reid Spencer66728ef2007-03-20 01:13:36 +00003060 delete $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00003061 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003062 }
Devang Patel05988662008-09-25 21:00:45 +00003063 | LABEL OptAttributes ValueRef OptAttributes {
3064 // FIXME: Remove trailing OptAttributes in LLVM 3.0, it was a mistake in 2.0
Dale Johanneseneb57ea72007-11-05 21:20:28 +00003065 // Labels are only valid in ASMs
3066 $$ = new ParamList();
Duncan Sandsdc024672007-11-27 13:23:08 +00003067 ParamListEntry E; E.Attrs = $2 | $4; E.Val = getBBVal($3);
Dale Johanneseneb57ea72007-11-05 21:20:28 +00003068 $$->push_back(E);
Duncan Sandsdc024672007-11-27 13:23:08 +00003069 CHECK_FOR_ERROR
Dale Johanneseneb57ea72007-11-05 21:20:28 +00003070 }
Devang Patel05988662008-09-25 21:00:45 +00003071 | ParamList ',' Types OptAttributes ValueRef OptAttributes {
3072 // FIXME: Remove trailing OptAttributes in LLVM 3.0, it was a mistake in 2.0
Reid Spencer14310612006-12-31 05:40:51 +00003073 if (!UpRefs.empty())
3074 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00003075 $$ = $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00003076 ParamListEntry E; E.Attrs = $4 | $6; E.Val = getVal($3->get(), $5);
Reid Spencer14310612006-12-31 05:40:51 +00003077 $$->push_back(E);
Reid Spencer66728ef2007-03-20 01:13:36 +00003078 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00003079 CHECK_FOR_ERROR
Reid Spencer14310612006-12-31 05:40:51 +00003080 }
Devang Patel05988662008-09-25 21:00:45 +00003081 | ParamList ',' LABEL OptAttributes ValueRef OptAttributes {
3082 // FIXME: Remove trailing OptAttributes in LLVM 3.0, it was a mistake in 2.0
Dale Johanneseneb57ea72007-11-05 21:20:28 +00003083 $$ = $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00003084 ParamListEntry E; E.Attrs = $4 | $6; E.Val = getBBVal($5);
Dale Johanneseneb57ea72007-11-05 21:20:28 +00003085 $$->push_back(E);
3086 CHECK_FOR_ERROR
3087 }
3088 | /*empty*/ { $$ = new ParamList(); };
Chris Lattner58af2a12006-02-15 07:22:58 +00003089
Reid Spencer14310612006-12-31 05:40:51 +00003090IndexList // Used for gep instructions and constant expressions
Reid Spencerc6c59fd2006-12-31 21:47:02 +00003091 : /*empty*/ { $$ = new std::vector<Value*>(); }
Reid Spencer14310612006-12-31 05:40:51 +00003092 | IndexList ',' ResolvedVal {
3093 $$ = $1;
3094 $$->push_back($3);
3095 CHECK_FOR_ERROR
3096 }
Reid Spencerc6c59fd2006-12-31 21:47:02 +00003097 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00003098
Dan Gohman81a0c0b2008-05-31 00:58:22 +00003099ConstantIndexList // Used for insertvalue and extractvalue instructions
3100 : ',' EUINT64VAL {
3101 $$ = new std::vector<unsigned>();
3102 if ((unsigned)$2 != $2)
3103 GEN_ERROR("Index " + utostr($2) + " is not valid for insertvalue or extractvalue.");
3104 $$->push_back($2);
3105 }
3106 | ConstantIndexList ',' EUINT64VAL {
3107 $$ = $1;
3108 if ((unsigned)$3 != $3)
3109 GEN_ERROR("Index " + utostr($3) + " is not valid for insertvalue or extractvalue.");
3110 $$->push_back($3);
3111 CHECK_FOR_ERROR
3112 }
3113 ;
3114
Chris Lattner58af2a12006-02-15 07:22:58 +00003115OptTailCall : TAIL CALL {
3116 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00003117 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003118 }
3119 | CALL {
3120 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00003121 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003122 };
3123
Chris Lattner58af2a12006-02-15 07:22:58 +00003124InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00003125 if (!UpRefs.empty())
3126 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Eric Christopher2a5196f2008-09-24 04:55:49 +00003127 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint() &&
Reid Spencer9d6565a2007-02-15 02:26:10 +00003128 !isa<VectorType>((*$2).get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00003129 GEN_ERROR(
Reid Spencerb5334b02007-02-05 10:18:06 +00003130 "Arithmetic operator requires integer, FP, or packed operands");
Eric Christopher2a5196f2008-09-24 04:55:49 +00003131 Value* val1 = getVal(*$2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00003132 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00003133 Value* val2 = getVal(*$2, $5);
Reid Spencer5b7e7532006-09-28 19:28:24 +00003134 CHECK_FOR_ERROR
Dan Gohmane4977cf2008-05-23 01:55:30 +00003135 $$ = BinaryOperator::Create($1, val1, val2);
Chris Lattner58af2a12006-02-15 07:22:58 +00003136 if ($$ == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00003137 GEN_ERROR("binary operator returned null");
Reid Spencera132e042006-12-03 05:46:11 +00003138 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00003139 }
3140 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00003141 if (!UpRefs.empty())
3142 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Chris Lattner42a75512007-01-15 02:27:26 +00003143 if (!(*$2)->isInteger()) {
Nate Begeman5bc1ea02008-07-29 15:49:41 +00003144 if (!isa<VectorType>($2->get()) ||
Reid Spencer9d6565a2007-02-15 02:26:10 +00003145 !cast<VectorType>($2->get())->getElementType()->isInteger())
Reid Spencerb5334b02007-02-05 10:18:06 +00003146 GEN_ERROR("Logical operator requires integral operands");
Chris Lattner58af2a12006-02-15 07:22:58 +00003147 }
Reid Spencera132e042006-12-03 05:46:11 +00003148 Value* tmpVal1 = getVal(*$2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00003149 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00003150 Value* tmpVal2 = getVal(*$2, $5);
Reid Spencer5b7e7532006-09-28 19:28:24 +00003151 CHECK_FOR_ERROR
Dan Gohmane4977cf2008-05-23 01:55:30 +00003152 $$ = BinaryOperator::Create($1, tmpVal1, tmpVal2);
Chris Lattner58af2a12006-02-15 07:22:58 +00003153 if ($$ == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00003154 GEN_ERROR("binary operator returned null");
Reid Spencera132e042006-12-03 05:46:11 +00003155 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00003156 }
Reid Spencera132e042006-12-03 05:46:11 +00003157 | ICMP IPredicates Types ValueRef ',' ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00003158 if (!UpRefs.empty())
3159 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003160 Value* tmpVal1 = getVal(*$3, $4);
3161 CHECK_FOR_ERROR
3162 Value* tmpVal2 = getVal(*$3, $6);
3163 CHECK_FOR_ERROR
Dan Gohmane4977cf2008-05-23 01:55:30 +00003164 $$ = CmpInst::Create($1, $2, tmpVal1, tmpVal2);
Reid Spencera132e042006-12-03 05:46:11 +00003165 if ($$ == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00003166 GEN_ERROR("icmp operator returned null");
Reid Spencer66728ef2007-03-20 01:13:36 +00003167 delete $3;
Reid Spencera132e042006-12-03 05:46:11 +00003168 }
3169 | FCMP FPredicates Types ValueRef ',' ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00003170 if (!UpRefs.empty())
3171 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003172 Value* tmpVal1 = getVal(*$3, $4);
3173 CHECK_FOR_ERROR
3174 Value* tmpVal2 = getVal(*$3, $6);
3175 CHECK_FOR_ERROR
Dan Gohmane4977cf2008-05-23 01:55:30 +00003176 $$ = CmpInst::Create($1, $2, tmpVal1, tmpVal2);
Reid Spencera132e042006-12-03 05:46:11 +00003177 if ($$ == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00003178 GEN_ERROR("fcmp operator returned null");
Reid Spencer66728ef2007-03-20 01:13:36 +00003179 delete $3;
Chris Lattner58af2a12006-02-15 07:22:58 +00003180 }
Nate Begemanac80ade2008-05-12 19:01:56 +00003181 | VICMP IPredicates Types ValueRef ',' ValueRef {
3182 if (!UpRefs.empty())
3183 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
3184 if (!isa<VectorType>((*$3).get()))
3185 GEN_ERROR("Scalar types not supported by vicmp instruction");
3186 Value* tmpVal1 = getVal(*$3, $4);
3187 CHECK_FOR_ERROR
3188 Value* tmpVal2 = getVal(*$3, $6);
3189 CHECK_FOR_ERROR
Dan Gohmane4977cf2008-05-23 01:55:30 +00003190 $$ = CmpInst::Create($1, $2, tmpVal1, tmpVal2);
Nate Begemanac80ade2008-05-12 19:01:56 +00003191 if ($$ == 0)
Dan Gohmand8ee59b2008-09-09 01:13:24 +00003192 GEN_ERROR("vicmp operator returned null");
Nate Begemanac80ade2008-05-12 19:01:56 +00003193 delete $3;
3194 }
3195 | VFCMP FPredicates Types ValueRef ',' ValueRef {
3196 if (!UpRefs.empty())
3197 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
3198 if (!isa<VectorType>((*$3).get()))
3199 GEN_ERROR("Scalar types not supported by vfcmp instruction");
3200 Value* tmpVal1 = getVal(*$3, $4);
3201 CHECK_FOR_ERROR
3202 Value* tmpVal2 = getVal(*$3, $6);
3203 CHECK_FOR_ERROR
Dan Gohmane4977cf2008-05-23 01:55:30 +00003204 $$ = CmpInst::Create($1, $2, tmpVal1, tmpVal2);
Nate Begemanac80ade2008-05-12 19:01:56 +00003205 if ($$ == 0)
Dan Gohmand8ee59b2008-09-09 01:13:24 +00003206 GEN_ERROR("vfcmp operator returned null");
Nate Begemanac80ade2008-05-12 19:01:56 +00003207 delete $3;
3208 }
Reid Spencer3da59db2006-11-27 01:05:10 +00003209 | CastOps ResolvedVal TO Types {
Reid Spencer14310612006-12-31 05:40:51 +00003210 if (!UpRefs.empty())
3211 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003212 Value* Val = $2;
Reid Spencerb0fcf8f2007-01-17 02:48:45 +00003213 const Type* DestTy = $4->get();
3214 if (!CastInst::castIsValid($1, Val, DestTy))
3215 GEN_ERROR("invalid cast opcode for cast from '" +
3216 Val->getType()->getDescription() + "' to '" +
Eric Christopher2a5196f2008-09-24 04:55:49 +00003217 DestTy->getDescription() + "'");
Dan Gohmane4977cf2008-05-23 01:55:30 +00003218 $$ = CastInst::Create($1, Val, DestTy);
Reid Spencera132e042006-12-03 05:46:11 +00003219 delete $4;
Chris Lattner58af2a12006-02-15 07:22:58 +00003220 }
3221 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Dan Gohmand8ee59b2008-09-09 01:13:24 +00003222 if (isa<VectorType>($2->getType())) {
3223 // vector select
3224 if (!isa<VectorType>($4->getType())
3225 || !isa<VectorType>($6->getType()) )
3226 GEN_ERROR("vector select value types must be vector types");
3227 const VectorType* cond_type = cast<VectorType>($2->getType());
3228 const VectorType* select_type = cast<VectorType>($4->getType());
3229 if (cond_type->getElementType() != Type::Int1Ty)
3230 GEN_ERROR("vector select condition element type must be boolean");
3231 if (cond_type->getNumElements() != select_type->getNumElements())
3232 GEN_ERROR("vector select number of elements must be the same");
3233 } else {
3234 if ($2->getType() != Type::Int1Ty)
3235 GEN_ERROR("select condition must be boolean");
3236 }
Reid Spencera132e042006-12-03 05:46:11 +00003237 if ($4->getType() != $6->getType())
Dan Gohmand8ee59b2008-09-09 01:13:24 +00003238 GEN_ERROR("select value types must match");
Gabor Greife64d2482008-04-06 23:07:54 +00003239 $$ = SelectInst::Create($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00003240 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003241 }
3242 | VAARG ResolvedVal ',' Types {
Reid Spencer14310612006-12-31 05:40:51 +00003243 if (!UpRefs.empty())
3244 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003245 $$ = new VAArgInst($2, *$4);
3246 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00003247 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003248 }
Chris Lattner58af2a12006-02-15 07:22:58 +00003249 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00003250 if (!ExtractElementInst::isValidOperands($2, $4))
Reid Spencerb5334b02007-02-05 10:18:06 +00003251 GEN_ERROR("Invalid extractelement operands");
Reid Spencera132e042006-12-03 05:46:11 +00003252 $$ = new ExtractElementInst($2, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00003253 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003254 }
3255 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00003256 if (!InsertElementInst::isValidOperands($2, $4, $6))
Reid Spencerb5334b02007-02-05 10:18:06 +00003257 GEN_ERROR("Invalid insertelement operands");
Gabor Greife64d2482008-04-06 23:07:54 +00003258 $$ = InsertElementInst::Create($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00003259 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003260 }
Chris Lattnerd5efe842006-04-08 01:18:56 +00003261 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00003262 if (!ShuffleVectorInst::isValidOperands($2, $4, $6))
Reid Spencerb5334b02007-02-05 10:18:06 +00003263 GEN_ERROR("Invalid shufflevector operands");
Reid Spencera132e042006-12-03 05:46:11 +00003264 $$ = new ShuffleVectorInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00003265 CHECK_FOR_ERROR
Chris Lattnerd5efe842006-04-08 01:18:56 +00003266 }
Chris Lattner58af2a12006-02-15 07:22:58 +00003267 | PHI_TOK PHIList {
3268 const Type *Ty = $2->front().first->getType();
3269 if (!Ty->isFirstClassType())
Reid Spencerb5334b02007-02-05 10:18:06 +00003270 GEN_ERROR("PHI node operands must be of first class type");
Gabor Greife64d2482008-04-06 23:07:54 +00003271 $$ = PHINode::Create(Ty);
Chris Lattner58af2a12006-02-15 07:22:58 +00003272 ((PHINode*)$$)->reserveOperandSpace($2->size());
3273 while ($2->begin() != $2->end()) {
Eric Christopher2a5196f2008-09-24 04:55:49 +00003274 if ($2->front().first->getType() != Ty)
Reid Spencerb5334b02007-02-05 10:18:06 +00003275 GEN_ERROR("All elements of a PHI node must be of the same type");
Chris Lattner58af2a12006-02-15 07:22:58 +00003276 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
3277 $2->pop_front();
3278 }
3279 delete $2; // Free the list...
Reid Spencer61c83e02006-08-18 08:43:06 +00003280 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003281 }
Devang Patel652203f2008-09-29 20:49:50 +00003282 | OptTailCall OptCallingConv OptRetAttrs ResultTypes ValueRef '(' ParamList ')'
Reid Spencer218ded22007-01-05 17:07:23 +00003283 OptFuncAttrs {
Reid Spencer14310612006-12-31 05:40:51 +00003284
3285 // Handle the short syntax
Reid Spencer3da59db2006-11-27 01:05:10 +00003286 const PointerType *PFTy = 0;
3287 const FunctionType *Ty = 0;
Devang Patel652203f2008-09-29 20:49:50 +00003288 if (!(PFTy = dyn_cast<PointerType>($4->get())) ||
Chris Lattner58af2a12006-02-15 07:22:58 +00003289 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
3290 // Pull out the types of all of the arguments...
3291 std::vector<const Type*> ParamTypes;
Devang Patel652203f2008-09-29 20:49:50 +00003292 ParamList::iterator I = $7->begin(), E = $7->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00003293 for (; I != E; ++I) {
Reid Spencer14310612006-12-31 05:40:51 +00003294 const Type *Ty = I->Val->getType();
3295 if (Ty == Type::VoidTy)
3296 GEN_ERROR("Short call syntax cannot be used with varargs");
3297 ParamTypes.push_back(Ty);
Chris Lattner58af2a12006-02-15 07:22:58 +00003298 }
Chris Lattnera925a142008-04-23 05:37:08 +00003299
Devang Patel652203f2008-09-29 20:49:50 +00003300 if (!FunctionType::isValidReturnType(*$4))
Chris Lattnera925a142008-04-23 05:37:08 +00003301 GEN_ERROR("Invalid result type for LLVM function");
3302
Devang Patel652203f2008-09-29 20:49:50 +00003303 Ty = FunctionType::get($4->get(), ParamTypes, false);
Christopher Lamb4374f8e2007-12-17 01:17:35 +00003304 PFTy = PointerType::getUnqual(Ty);
Chris Lattner58af2a12006-02-15 07:22:58 +00003305 }
Chris Lattner6cdc6822007-04-26 05:31:05 +00003306
Devang Patel652203f2008-09-29 20:49:50 +00003307 Value *V = getVal(PFTy, $5); // Get the function we're calling...
Reid Spencer5b7e7532006-09-28 19:28:24 +00003308 CHECK_FOR_ERROR
Chris Lattner6cdc6822007-04-26 05:31:05 +00003309
Reid Spencer7780acb2007-04-16 06:56:07 +00003310 // Check for call to invalid intrinsic to avoid crashing later.
3311 if (Function *theF = dyn_cast<Function>(V)) {
Reid Spencered48de22007-04-16 22:02:23 +00003312 if (theF->hasName() && (theF->getValueName()->getKeyLength() >= 5) &&
Reid Spencer36fdde12007-04-16 20:35:38 +00003313 (0 == strncmp(theF->getValueName()->getKeyData(), "llvm.", 5)) &&
3314 !theF->getIntrinsicID(true))
Reid Spencer7780acb2007-04-16 06:56:07 +00003315 GEN_ERROR("Call to invalid LLVM intrinsic function '" +
3316 theF->getName() + "'");
3317 }
3318
Devang Patel05988662008-09-25 21:00:45 +00003319 // Set up the Attributes for the function
3320 SmallVector<AttributeWithIndex, 8> Attrs;
Devang Patel19c87462008-09-26 22:53:05 +00003321 //FIXME : In 3.0, stop accepting zext, sext and inreg as optional function
3322 //attributes.
Devang Patel652203f2008-09-29 20:49:50 +00003323 Attributes RetAttrs = $3;
3324 if ($9 != Attribute::None) {
3325 if ($9 & Attribute::ZExt) {
Devang Patel19c87462008-09-26 22:53:05 +00003326 RetAttrs = RetAttrs | Attribute::ZExt;
Devang Patel652203f2008-09-29 20:49:50 +00003327 $9 = $9 ^ Attribute::ZExt;
Devang Patel19c87462008-09-26 22:53:05 +00003328 }
Devang Patel652203f2008-09-29 20:49:50 +00003329 if ($9 & Attribute::SExt) {
Devang Patel19c87462008-09-26 22:53:05 +00003330 RetAttrs = RetAttrs | Attribute::SExt;
Devang Patel652203f2008-09-29 20:49:50 +00003331 $9 = $9 ^ Attribute::SExt;
Devang Patel19c87462008-09-26 22:53:05 +00003332 }
Devang Patel652203f2008-09-29 20:49:50 +00003333 if ($9 & Attribute::InReg) {
Devang Patel19c87462008-09-26 22:53:05 +00003334 RetAttrs = RetAttrs | Attribute::InReg;
Devang Patel652203f2008-09-29 20:49:50 +00003335 $9 = $9 ^ Attribute::InReg;
Devang Patel19c87462008-09-26 22:53:05 +00003336 }
Devang Patel19c87462008-09-26 22:53:05 +00003337 }
Devang Patel652203f2008-09-29 20:49:50 +00003338 if (RetAttrs != Attribute::None)
3339 Attrs.push_back(AttributeWithIndex::get(0, RetAttrs));
Devang Patel19c87462008-09-26 22:53:05 +00003340
Eric Christopher2a5196f2008-09-24 04:55:49 +00003341 // Check the arguments
Reid Spencer14310612006-12-31 05:40:51 +00003342 ValueList Args;
Devang Patel652203f2008-09-29 20:49:50 +00003343 if ($7->empty()) { // Has no arguments?
Chris Lattner58af2a12006-02-15 07:22:58 +00003344 // Make sure no arguments is a good thing!
3345 if (Ty->getNumParams() != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00003346 GEN_ERROR("No arguments passed to a function that "
Reid Spencerb5334b02007-02-05 10:18:06 +00003347 "expects arguments");
Chris Lattner58af2a12006-02-15 07:22:58 +00003348 } else { // Has arguments?
3349 // Loop through FunctionType's arguments and ensure they are specified
Duncan Sandsdc024672007-11-27 13:23:08 +00003350 // correctly. Also, gather any parameter attributes.
Chris Lattner58af2a12006-02-15 07:22:58 +00003351 FunctionType::param_iterator I = Ty->param_begin();
3352 FunctionType::param_iterator E = Ty->param_end();
Devang Patel652203f2008-09-29 20:49:50 +00003353 ParamList::iterator ArgI = $7->begin(), ArgE = $7->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00003354 unsigned index = 1;
Chris Lattner58af2a12006-02-15 07:22:58 +00003355
Duncan Sandsdc024672007-11-27 13:23:08 +00003356 for (; ArgI != ArgE && I != E; ++ArgI, ++I, ++index) {
Reid Spencer14310612006-12-31 05:40:51 +00003357 if (ArgI->Val->getType() != *I)
3358 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00003359 (*I)->getDescription() + "'");
Reid Spencer14310612006-12-31 05:40:51 +00003360 Args.push_back(ArgI->Val);
Devang Patel05988662008-09-25 21:00:45 +00003361 if (ArgI->Attrs != Attribute::None)
3362 Attrs.push_back(AttributeWithIndex::get(index, ArgI->Attrs));
Reid Spencer14310612006-12-31 05:40:51 +00003363 }
3364 if (Ty->isVarArg()) {
3365 if (I == E)
Chris Lattner38905612008-02-19 04:36:25 +00003366 for (; ArgI != ArgE; ++ArgI, ++index) {
Reid Spencer14310612006-12-31 05:40:51 +00003367 Args.push_back(ArgI->Val); // push the remaining varargs
Devang Patel05988662008-09-25 21:00:45 +00003368 if (ArgI->Attrs != Attribute::None)
3369 Attrs.push_back(AttributeWithIndex::get(index, ArgI->Attrs));
Chris Lattner38905612008-02-19 04:36:25 +00003370 }
Reid Spencer14310612006-12-31 05:40:51 +00003371 } else if (I != E || ArgI != ArgE)
Reid Spencerb5334b02007-02-05 10:18:06 +00003372 GEN_ERROR("Invalid number of parameters detected");
Chris Lattner58af2a12006-02-15 07:22:58 +00003373 }
Devang Patel652203f2008-09-29 20:49:50 +00003374 if ($9 != Attribute::None)
3375 Attrs.push_back(AttributeWithIndex::get(~0, $9));
Duncan Sandsdc024672007-11-27 13:23:08 +00003376
Devang Patel05988662008-09-25 21:00:45 +00003377 // Finish off the Attributes and check them
3378 AttrListPtr PAL;
Duncan Sandsdc024672007-11-27 13:23:08 +00003379 if (!Attrs.empty())
Devang Patel05988662008-09-25 21:00:45 +00003380 PAL = AttrListPtr::get(Attrs.begin(), Attrs.end());
Duncan Sandsdc024672007-11-27 13:23:08 +00003381
Reid Spencer14310612006-12-31 05:40:51 +00003382 // Create the call node
Gabor Greife64d2482008-04-06 23:07:54 +00003383 CallInst *CI = CallInst::Create(V, Args.begin(), Args.end());
Reid Spencer14310612006-12-31 05:40:51 +00003384 CI->setTailCall($1);
3385 CI->setCallingConv($2);
Devang Patel05988662008-09-25 21:00:45 +00003386 CI->setAttributes(PAL);
Reid Spencer14310612006-12-31 05:40:51 +00003387 $$ = CI;
Devang Patel652203f2008-09-29 20:49:50 +00003388 delete $7;
3389 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00003390 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003391 }
3392 | MemoryInst {
3393 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00003394 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003395 };
3396
Chris Lattner58af2a12006-02-15 07:22:58 +00003397OptVolatile : VOLATILE {
3398 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00003399 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003400 }
3401 | /* empty */ {
3402 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00003403 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003404 };
3405
3406
3407
3408MemoryInst : MALLOC Types OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003409 if (!UpRefs.empty())
3410 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003411 $$ = new MallocInst(*$2, 0, $3);
3412 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00003413 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003414 }
Reid Spencera54b7cb2007-01-12 07:05:14 +00003415 | MALLOC Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003416 if (!UpRefs.empty())
3417 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Dan Gohman81a0c0b2008-05-31 00:58:22 +00003418 if ($4 != Type::Int32Ty)
3419 GEN_ERROR("Malloc array size is not a 32-bit integer!");
Reid Spencera132e042006-12-03 05:46:11 +00003420 Value* tmpVal = getVal($4, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00003421 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00003422 $$ = new MallocInst(*$2, tmpVal, $6);
3423 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00003424 }
3425 | ALLOCA Types OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003426 if (!UpRefs.empty())
3427 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003428 $$ = new AllocaInst(*$2, 0, $3);
3429 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00003430 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003431 }
Reid Spencera54b7cb2007-01-12 07:05:14 +00003432 | ALLOCA Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003433 if (!UpRefs.empty())
3434 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Dan Gohman81a0c0b2008-05-31 00:58:22 +00003435 if ($4 != Type::Int32Ty)
3436 GEN_ERROR("Alloca array size is not a 32-bit integer!");
Reid Spencera132e042006-12-03 05:46:11 +00003437 Value* tmpVal = getVal($4, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00003438 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00003439 $$ = new AllocaInst(*$2, tmpVal, $6);
3440 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00003441 }
3442 | FREE ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00003443 if (!isa<PointerType>($2->getType()))
Eric Christopher2a5196f2008-09-24 04:55:49 +00003444 GEN_ERROR("Trying to free nonpointer type " +
Reid Spencerb5334b02007-02-05 10:18:06 +00003445 $2->getType()->getDescription() + "");
Reid Spencera132e042006-12-03 05:46:11 +00003446 $$ = new FreeInst($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00003447 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003448 }
3449
Christopher Lamb5c104242007-04-22 20:09:11 +00003450 | OptVolatile LOAD Types ValueRef OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003451 if (!UpRefs.empty())
3452 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003453 if (!isa<PointerType>($3->get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00003454 GEN_ERROR("Can't load from nonpointer type: " +
Reid Spencera132e042006-12-03 05:46:11 +00003455 (*$3)->getDescription());
3456 if (!cast<PointerType>($3->get())->getElementType()->isFirstClassType())
Reid Spencer61c83e02006-08-18 08:43:06 +00003457 GEN_ERROR("Can't load from pointer of non-first-class type: " +
Reid Spencera132e042006-12-03 05:46:11 +00003458 (*$3)->getDescription());
3459 Value* tmpVal = getVal(*$3, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00003460 CHECK_FOR_ERROR
Christopher Lamb5c104242007-04-22 20:09:11 +00003461 $$ = new LoadInst(tmpVal, "", $1, $5);
Reid Spencera132e042006-12-03 05:46:11 +00003462 delete $3;
Chris Lattner58af2a12006-02-15 07:22:58 +00003463 }
Christopher Lamb5c104242007-04-22 20:09:11 +00003464 | OptVolatile STORE ResolvedVal ',' Types ValueRef OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003465 if (!UpRefs.empty())
3466 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003467 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00003468 if (!PT)
Reid Spencer61c83e02006-08-18 08:43:06 +00003469 GEN_ERROR("Can't store to a nonpointer type: " +
Reid Spencera132e042006-12-03 05:46:11 +00003470 (*$5)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00003471 const Type *ElTy = PT->getElementType();
Reid Spencera132e042006-12-03 05:46:11 +00003472 if (ElTy != $3->getType())
3473 GEN_ERROR("Can't store '" + $3->getType()->getDescription() +
Reid Spencerb5334b02007-02-05 10:18:06 +00003474 "' into space of type '" + ElTy->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00003475
Reid Spencera132e042006-12-03 05:46:11 +00003476 Value* tmpVal = getVal(*$5, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00003477 CHECK_FOR_ERROR
Christopher Lamb5c104242007-04-22 20:09:11 +00003478 $$ = new StoreInst($3, tmpVal, $1, $7);
Reid Spencera132e042006-12-03 05:46:11 +00003479 delete $5;
Chris Lattner58af2a12006-02-15 07:22:58 +00003480 }
Dan Gohmane4977cf2008-05-23 01:55:30 +00003481 | GETRESULT Types ValueRef ',' EUINT64VAL {
Dan Gohman1a570242008-07-23 00:54:54 +00003482 if (!UpRefs.empty())
3483 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
3484 if (!isa<StructType>($2->get()) && !isa<ArrayType>($2->get()))
3485 GEN_ERROR("getresult insn requires an aggregate operand");
3486 if (!ExtractValueInst::getIndexedType(*$2, $5))
3487 GEN_ERROR("Invalid getresult index for type '" +
3488 (*$2)->getDescription()+ "'");
3489
3490 Value *tmpVal = getVal(*$2, $3);
Devang Patel5a970972008-02-19 22:27:01 +00003491 CHECK_FOR_ERROR
Dan Gohman1a570242008-07-23 00:54:54 +00003492 $$ = ExtractValueInst::Create(tmpVal, $5);
3493 delete $2;
Devang Patel5a970972008-02-19 22:27:01 +00003494 }
Chris Lattner58af2a12006-02-15 07:22:58 +00003495 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencer14310612006-12-31 05:40:51 +00003496 if (!UpRefs.empty())
3497 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003498 if (!isa<PointerType>($2->get()))
Reid Spencerb5334b02007-02-05 10:18:06 +00003499 GEN_ERROR("getelementptr insn requires pointer operand");
Chris Lattner58af2a12006-02-15 07:22:58 +00003500
Dan Gohman041e2eb2008-05-15 19:50:34 +00003501 if (!GetElementPtrInst::getIndexedType(*$2, $4->begin(), $4->end()))
Reid Spencer61c83e02006-08-18 08:43:06 +00003502 GEN_ERROR("Invalid getelementptr indices for type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00003503 (*$2)->getDescription()+ "'");
Reid Spencera132e042006-12-03 05:46:11 +00003504 Value* tmpVal = getVal(*$2, $3);
Reid Spencer61c83e02006-08-18 08:43:06 +00003505 CHECK_FOR_ERROR
Gabor Greife64d2482008-04-06 23:07:54 +00003506 $$ = GetElementPtrInst::Create(tmpVal, $4->begin(), $4->end());
Eric Christopher2a5196f2008-09-24 04:55:49 +00003507 delete $2;
Reid Spencer5b7e7532006-09-28 19:28:24 +00003508 delete $4;
Dan Gohmane4977cf2008-05-23 01:55:30 +00003509 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +00003510 | EXTRACTVALUE Types ValueRef ConstantIndexList {
Dan Gohmane4977cf2008-05-23 01:55:30 +00003511 if (!UpRefs.empty())
3512 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
3513 if (!isa<StructType>($2->get()) && !isa<ArrayType>($2->get()))
3514 GEN_ERROR("extractvalue insn requires an aggregate operand");
3515
3516 if (!ExtractValueInst::getIndexedType(*$2, $4->begin(), $4->end()))
3517 GEN_ERROR("Invalid extractvalue indices for type '" +
3518 (*$2)->getDescription()+ "'");
3519 Value* tmpVal = getVal(*$2, $3);
3520 CHECK_FOR_ERROR
3521 $$ = ExtractValueInst::Create(tmpVal, $4->begin(), $4->end());
Eric Christopher2a5196f2008-09-24 04:55:49 +00003522 delete $2;
Dan Gohmane4977cf2008-05-23 01:55:30 +00003523 delete $4;
3524 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +00003525 | INSERTVALUE Types ValueRef ',' Types ValueRef ConstantIndexList {
Dan Gohmane4977cf2008-05-23 01:55:30 +00003526 if (!UpRefs.empty())
3527 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
3528 if (!isa<StructType>($2->get()) && !isa<ArrayType>($2->get()))
3529 GEN_ERROR("extractvalue insn requires an aggregate operand");
3530
3531 if (ExtractValueInst::getIndexedType(*$2, $7->begin(), $7->end()) != $5->get())
3532 GEN_ERROR("Invalid insertvalue indices for type '" +
3533 (*$2)->getDescription()+ "'");
3534 Value* aggVal = getVal(*$2, $3);
3535 Value* tmpVal = getVal(*$5, $6);
3536 CHECK_FOR_ERROR
3537 $$ = InsertValueInst::Create(aggVal, tmpVal, $7->begin(), $7->end());
Eric Christopher2a5196f2008-09-24 04:55:49 +00003538 delete $2;
Dan Gohmane4977cf2008-05-23 01:55:30 +00003539 delete $5;
3540 delete $7;
Chris Lattner58af2a12006-02-15 07:22:58 +00003541 };
3542
3543
3544%%
Reid Spencer61c83e02006-08-18 08:43:06 +00003545
Reid Spencer14310612006-12-31 05:40:51 +00003546// common code from the two 'RunVMAsmParser' functions
3547static Module* RunParser(Module * M) {
Reid Spencer14310612006-12-31 05:40:51 +00003548 CurModule.CurrentModule = M;
Reid Spencer14310612006-12-31 05:40:51 +00003549 // Check to make sure the parser succeeded
3550 if (yyparse()) {
3551 if (ParserResult)
3552 delete ParserResult;
3553 return 0;
3554 }
3555
Reid Spencer0d60b5a2007-03-30 01:37:39 +00003556 // Emit an error if there are any unresolved types left.
3557 if (!CurModule.LateResolveTypes.empty()) {
3558 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
3559 if (DID.Type == ValID::LocalName) {
3560 GenerateError("Undefined type remains at eof: '"+DID.getName() + "'");
3561 } else {
3562 GenerateError("Undefined type remains at eof: #" + itostr(DID.Num));
3563 }
3564 if (ParserResult)
3565 delete ParserResult;
3566 return 0;
3567 }
3568
3569 // Emit an error if there are any unresolved values left.
3570 if (!CurModule.LateResolveValues.empty()) {
3571 Value *V = CurModule.LateResolveValues.back();
3572 std::map<Value*, std::pair<ValID, int> >::iterator I =
3573 CurModule.PlaceHolderInfo.find(V);
3574
3575 if (I != CurModule.PlaceHolderInfo.end()) {
3576 ValID &DID = I->second.first;
3577 if (DID.Type == ValID::LocalName) {
3578 GenerateError("Undefined value remains at eof: "+DID.getName() + "'");
3579 } else {
3580 GenerateError("Undefined value remains at eof: #" + itostr(DID.Num));
3581 }
3582 if (ParserResult)
3583 delete ParserResult;
3584 return 0;
3585 }
3586 }
3587
Reid Spencer14310612006-12-31 05:40:51 +00003588 // Check to make sure that parsing produced a result
3589 if (!ParserResult)
3590 return 0;
3591
3592 // Reset ParserResult variable while saving its value for the result.
3593 Module *Result = ParserResult;
3594 ParserResult = 0;
3595
3596 return Result;
3597}
3598
Reid Spencer61c83e02006-08-18 08:43:06 +00003599void llvm::GenerateError(const std::string &message, int LineNo) {
Duncan Sandsdc024672007-11-27 13:23:08 +00003600 if (LineNo == -1) LineNo = LLLgetLineNo();
Reid Spencer61c83e02006-08-18 08:43:06 +00003601 // TODO: column number in exception
3602 if (TheParseError)
Duncan Sandsdc024672007-11-27 13:23:08 +00003603 TheParseError->setError(LLLgetFilename(), message, LineNo);
Reid Spencer61c83e02006-08-18 08:43:06 +00003604 TriggerError = 1;
3605}
3606
Chris Lattner58af2a12006-02-15 07:22:58 +00003607int yyerror(const char *ErrorMsg) {
Duncan Sandsdc024672007-11-27 13:23:08 +00003608 std::string where = LLLgetFilename() + ":" + utostr(LLLgetLineNo()) + ": ";
Reid Spenceref9b9a72007-02-05 20:47:22 +00003609 std::string errMsg = where + "error: " + std::string(ErrorMsg);
Duncan Sandsdc024672007-11-27 13:23:08 +00003610 if (yychar != YYEMPTY && yychar != 0) {
3611 errMsg += " while reading token: '";
Eric Christopher2a5196f2008-09-24 04:55:49 +00003612 errMsg += std::string(LLLgetTokenStart(),
Duncan Sandsdc024672007-11-27 13:23:08 +00003613 LLLgetTokenStart()+LLLgetTokenLength()) + "'";
3614 }
Reid Spencer61c83e02006-08-18 08:43:06 +00003615 GenerateError(errMsg);
Chris Lattner58af2a12006-02-15 07:22:58 +00003616 return 0;
3617}