blob: d76994a431a0e7617f390751537465c774a58701 [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;
Nuno Lopes8a5f3472008-10-15 12:05:02 +0000143 I->first.second.destroy();
Chris Lattner58af2a12006-02-15 07:22:58 +0000144 GlobalRefs.erase(I);
145 }
146 return Ret;
147 }
Reid Spencer8c8a2dc2007-01-02 21:54:12 +0000148
149 bool TypeIsUnresolved(PATypeHolder* PATy) {
150 // If it isn't abstract, its resolved
151 const Type* Ty = PATy->get();
152 if (!Ty->isAbstract())
153 return false;
154 // Traverse the type looking for abstract types. If it isn't abstract then
Eric Christopher2a5196f2008-09-24 04:55:49 +0000155 // we don't need to traverse that leg of the type.
Reid Spencer8c8a2dc2007-01-02 21:54:12 +0000156 std::vector<const Type*> WorkList, SeenList;
157 WorkList.push_back(Ty);
158 while (!WorkList.empty()) {
159 const Type* Ty = WorkList.back();
160 SeenList.push_back(Ty);
161 WorkList.pop_back();
162 if (const OpaqueType* OpTy = dyn_cast<OpaqueType>(Ty)) {
163 // Check to see if this is an unresolved type
164 std::map<ValID, PATypeHolder>::iterator I = LateResolveTypes.begin();
165 std::map<ValID, PATypeHolder>::iterator E = LateResolveTypes.end();
166 for ( ; I != E; ++I) {
167 if (I->second.get() == OpTy)
168 return true;
169 }
170 } else if (const SequentialType* SeqTy = dyn_cast<SequentialType>(Ty)) {
171 const Type* TheTy = SeqTy->getElementType();
172 if (TheTy->isAbstract() && TheTy != Ty) {
Eric Christopher2a5196f2008-09-24 04:55:49 +0000173 std::vector<const Type*>::iterator I = SeenList.begin(),
Reid Spencer8c8a2dc2007-01-02 21:54:12 +0000174 E = SeenList.end();
175 for ( ; I != E; ++I)
176 if (*I == TheTy)
177 break;
178 if (I == E)
179 WorkList.push_back(TheTy);
180 }
181 } else if (const StructType* StrTy = dyn_cast<StructType>(Ty)) {
182 for (unsigned i = 0; i < StrTy->getNumElements(); ++i) {
183 const Type* TheTy = StrTy->getElementType(i);
184 if (TheTy->isAbstract() && TheTy != Ty) {
Eric Christopher2a5196f2008-09-24 04:55:49 +0000185 std::vector<const Type*>::iterator I = SeenList.begin(),
Reid Spencer8c8a2dc2007-01-02 21:54:12 +0000186 E = SeenList.end();
187 for ( ; I != E; ++I)
188 if (*I == TheTy)
189 break;
190 if (I == E)
191 WorkList.push_back(TheTy);
192 }
193 }
194 }
195 }
196 return false;
197 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000198} CurModule;
199
200static struct PerFunctionInfo {
201 Function *CurrentFunction; // Pointer to current function being created
202
Reid Spencer93c40032007-03-19 18:40:50 +0000203 ValueList Values; // Keep track of #'d definitions
204 unsigned NextValNum;
205 ValueList LateResolveValues;
Reid Spenceref9b9a72007-02-05 20:47:22 +0000206 bool isDeclare; // Is this function a forward declararation?
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000207 GlobalValue::LinkageTypes Linkage; // Linkage for forward declaration.
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000208 GlobalValue::VisibilityTypes Visibility;
Chris Lattner58af2a12006-02-15 07:22:58 +0000209
210 /// BBForwardRefs - When we see forward references to basic blocks, keep
211 /// track of them here.
Reid Spencer93c40032007-03-19 18:40:50 +0000212 std::map<ValID, BasicBlock*> BBForwardRefs;
Chris Lattner58af2a12006-02-15 07:22:58 +0000213
214 inline PerFunctionInfo() {
215 CurrentFunction = 0;
216 isDeclare = false;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000217 Linkage = GlobalValue::ExternalLinkage;
218 Visibility = GlobalValue::DefaultVisibility;
Chris Lattner58af2a12006-02-15 07:22:58 +0000219 }
220
221 inline void FunctionStart(Function *M) {
222 CurrentFunction = M;
Reid Spencer93c40032007-03-19 18:40:50 +0000223 NextValNum = 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000224 }
225
226 void FunctionDone() {
Chris Lattner58af2a12006-02-15 07:22:58 +0000227 // Any forward referenced blocks left?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000228 if (!BBForwardRefs.empty()) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000229 GenerateError("Undefined reference to label " +
Reid Spencer93c40032007-03-19 18:40:50 +0000230 BBForwardRefs.begin()->second->getName());
Reid Spencer5b7e7532006-09-28 19:28:24 +0000231 return;
232 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000233
234 // Resolve all forward references now.
235 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
236
237 Values.clear(); // Clear out function local definitions
Reid Spencer93c40032007-03-19 18:40:50 +0000238 BBForwardRefs.clear();
Chris Lattner58af2a12006-02-15 07:22:58 +0000239 CurrentFunction = 0;
240 isDeclare = false;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000241 Linkage = GlobalValue::ExternalLinkage;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000242 Visibility = GlobalValue::DefaultVisibility;
Chris Lattner58af2a12006-02-15 07:22:58 +0000243 }
244} CurFun; // Info for the current function...
245
246static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
247
248
249//===----------------------------------------------------------------------===//
250// Code to handle definitions of all the types
251//===----------------------------------------------------------------------===//
252
Chris Lattner15bd0952008-08-29 17:20:18 +0000253/// InsertValue - Insert a value into the value table. If it is named, this
254/// returns -1, otherwise it returns the slot number for the value.
255static int InsertValue(Value *V, ValueList &ValueTab = CurFun.Values) {
Reid Spencer93c40032007-03-19 18:40:50 +0000256 // Things that have names or are void typed don't get slot numbers
257 if (V->hasName() || (V->getType() == Type::VoidTy))
Chris Lattner15bd0952008-08-29 17:20:18 +0000258 return -1;
Chris Lattner58af2a12006-02-15 07:22:58 +0000259
Reid Spencer93c40032007-03-19 18:40:50 +0000260 // In the case of function values, we have to allow for the forward reference
261 // of basic blocks, which are included in the numbering. Consequently, we keep
Eric Christopher2a5196f2008-09-24 04:55:49 +0000262 // track of the next insertion location with NextValNum. When a BB gets
Reid Spencer93c40032007-03-19 18:40:50 +0000263 // inserted, it could change the size of the CurFun.Values vector.
264 if (&ValueTab == &CurFun.Values) {
265 if (ValueTab.size() <= CurFun.NextValNum)
266 ValueTab.resize(CurFun.NextValNum+1);
267 ValueTab[CurFun.NextValNum++] = V;
Chris Lattner15bd0952008-08-29 17:20:18 +0000268 return CurFun.NextValNum-1;
Eric Christopher2a5196f2008-09-24 04:55:49 +0000269 }
Reid Spencer93c40032007-03-19 18:40:50 +0000270 // For all other lists, its okay to just tack it on the back of the vector.
271 ValueTab.push_back(V);
Chris Lattner15bd0952008-08-29 17:20:18 +0000272 return ValueTab.size()-1;
Chris Lattner58af2a12006-02-15 07:22:58 +0000273}
274
275static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
276 switch (D.Type) {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000277 case ValID::LocalID: // Is it a numbered definition?
Chris Lattner58af2a12006-02-15 07:22:58 +0000278 // Module constants occupy the lowest numbered slots...
Reid Spencer41dff5e2007-01-26 08:05:27 +0000279 if (D.Num < CurModule.Types.size())
280 return CurModule.Types[D.Num];
Chris Lattner58af2a12006-02-15 07:22:58 +0000281 break;
Reid Spencer41dff5e2007-01-26 08:05:27 +0000282 case ValID::LocalName: // Is it a named definition?
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000283 if (const Type *N = CurModule.CurrentModule->getTypeByName(D.getName())) {
Chris Lattner58af2a12006-02-15 07:22:58 +0000284 D.destroy(); // Free old strdup'd memory...
285 return N;
286 }
287 break;
288 default:
Reid Spencerb5334b02007-02-05 10:18:06 +0000289 GenerateError("Internal parser error: Invalid symbol type reference");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000290 return 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000291 }
292
293 // If we reached here, we referenced either a symbol that we don't know about
294 // or an id number that hasn't been read yet. We may be referencing something
295 // forward, so just create an entry to be resolved later and get to it...
296 //
297 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
298
299
300 if (inFunctionScope()) {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000301 if (D.Type == ValID::LocalName) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000302 GenerateError("Reference to an undefined type: '" + D.getName() + "'");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000303 return 0;
304 } else {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000305 GenerateError("Reference to an undefined type: #" + utostr(D.Num));
Reid Spencer5b7e7532006-09-28 19:28:24 +0000306 return 0;
307 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000308 }
309
Reid Spencer861d9d62006-11-28 07:29:44 +0000310 std::map<ValID, PATypeHolder>::iterator I =CurModule.LateResolveTypes.find(D);
Nuno Lopesf05ff662008-10-15 11:20:21 +0000311 if (I != CurModule.LateResolveTypes.end()) {
312 D.destroy();
Reid Spencer861d9d62006-11-28 07:29:44 +0000313 return I->second;
Nuno Lopesf05ff662008-10-15 11:20:21 +0000314 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000315
Reid Spencer861d9d62006-11-28 07:29:44 +0000316 Type *Typ = OpaqueType::get();
317 CurModule.LateResolveTypes.insert(std::make_pair(D, Typ));
318 return Typ;
Reid Spencera132e042006-12-03 05:46:11 +0000319 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000320
Reid Spencer93c40032007-03-19 18:40:50 +0000321// getExistingVal - Look up the value specified by the provided type and
Chris Lattner58af2a12006-02-15 07:22:58 +0000322// the provided ValID. If the value exists and has already been defined, return
323// it. Otherwise return null.
324//
Reid Spencer93c40032007-03-19 18:40:50 +0000325static Value *getExistingVal(const Type *Ty, const ValID &D) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000326 if (isa<FunctionType>(Ty)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000327 GenerateError("Functions are not values and "
Chris Lattner58af2a12006-02-15 07:22:58 +0000328 "must be referenced as pointers");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000329 return 0;
330 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000331
332 switch (D.Type) {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000333 case ValID::LocalID: { // Is it a numbered definition?
Reid Spencer41dff5e2007-01-26 08:05:27 +0000334 // Check that the number is within bounds.
Eric Christopher2a5196f2008-09-24 04:55:49 +0000335 if (D.Num >= CurFun.Values.size())
Reid Spencer93c40032007-03-19 18:40:50 +0000336 return 0;
337 Value *Result = CurFun.Values[D.Num];
338 if (Ty != Result->getType()) {
339 GenerateError("Numbered value (%" + utostr(D.Num) + ") of type '" +
Eric Christopher2a5196f2008-09-24 04:55:49 +0000340 Result->getType()->getDescription() + "' does not match "
Reid Spencer93c40032007-03-19 18:40:50 +0000341 "expected type, '" + Ty->getDescription() + "'");
342 return 0;
343 }
344 return Result;
Reid Spencer41dff5e2007-01-26 08:05:27 +0000345 }
346 case ValID::GlobalID: { // Is it a numbered definition?
Eric Christopher2a5196f2008-09-24 04:55:49 +0000347 if (D.Num >= CurModule.Values.size())
Reid Spenceref9b9a72007-02-05 20:47:22 +0000348 return 0;
Reid Spencer93c40032007-03-19 18:40:50 +0000349 Value *Result = CurModule.Values[D.Num];
350 if (Ty != Result->getType()) {
351 GenerateError("Numbered value (@" + utostr(D.Num) + ") of type '" +
Eric Christopher2a5196f2008-09-24 04:55:49 +0000352 Result->getType()->getDescription() + "' does not match "
Reid Spencer93c40032007-03-19 18:40:50 +0000353 "expected type, '" + Ty->getDescription() + "'");
Reid Spenceref9b9a72007-02-05 20:47:22 +0000354 return 0;
Reid Spencer93c40032007-03-19 18:40:50 +0000355 }
356 return Result;
Chris Lattner58af2a12006-02-15 07:22:58 +0000357 }
Eric Christopher2a5196f2008-09-24 04:55:49 +0000358
Reid Spencer41dff5e2007-01-26 08:05:27 +0000359 case ValID::LocalName: { // Is it a named definition?
Eric Christopher2a5196f2008-09-24 04:55:49 +0000360 if (!inFunctionScope())
Reid Spenceref9b9a72007-02-05 20:47:22 +0000361 return 0;
362 ValueSymbolTable &SymTab = CurFun.CurrentFunction->getValueSymbolTable();
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000363 Value *N = SymTab.lookup(D.getName());
Eric Christopher2a5196f2008-09-24 04:55:49 +0000364 if (N == 0)
Reid Spenceref9b9a72007-02-05 20:47:22 +0000365 return 0;
366 if (N->getType() != Ty)
367 return 0;
Eric Christopher2a5196f2008-09-24 04:55:49 +0000368
Reid Spencer41dff5e2007-01-26 08:05:27 +0000369 D.destroy(); // Free old strdup'd memory...
370 return N;
371 }
372 case ValID::GlobalName: { // Is it a named definition?
Reid Spenceref9b9a72007-02-05 20:47:22 +0000373 ValueSymbolTable &SymTab = CurModule.CurrentModule->getValueSymbolTable();
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000374 Value *N = SymTab.lookup(D.getName());
Eric Christopher2a5196f2008-09-24 04:55:49 +0000375 if (N == 0)
Reid Spenceref9b9a72007-02-05 20:47:22 +0000376 return 0;
377 if (N->getType() != Ty)
378 return 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000379
380 D.destroy(); // Free old strdup'd memory...
381 return N;
382 }
383
384 // Check to make sure that "Ty" is an integral type, and that our
385 // value will fit into the specified type...
386 case ValID::ConstSIntVal: // Is it a constant pool reference??
Chris Lattner38905612008-02-19 04:36:25 +0000387 if (!isa<IntegerType>(Ty) ||
388 !ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000389 GenerateError("Signed integral constant '" +
Chris Lattner58af2a12006-02-15 07:22:58 +0000390 itostr(D.ConstPool64) + "' is invalid for type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +0000391 Ty->getDescription() + "'");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000392 return 0;
393 }
Reid Spencer49d273e2007-03-19 20:40:51 +0000394 return ConstantInt::get(Ty, D.ConstPool64, true);
Chris Lattner58af2a12006-02-15 07:22:58 +0000395
396 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattner38905612008-02-19 04:36:25 +0000397 if (isa<IntegerType>(Ty) &&
398 ConstantInt::isValueValidForType(Ty, D.UConstPool64))
Reid Spencerb83eb642006-10-20 07:07:24 +0000399 return ConstantInt::get(Ty, D.UConstPool64);
Chris Lattner38905612008-02-19 04:36:25 +0000400
401 if (!isa<IntegerType>(Ty) ||
402 !ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
403 GenerateError("Integral constant '" + utostr(D.UConstPool64) +
404 "' is invalid or out of range for type '" +
405 Ty->getDescription() + "'");
406 return 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000407 }
Chris Lattner38905612008-02-19 04:36:25 +0000408 // This is really a signed reference. Transmogrify.
409 return ConstantInt::get(Ty, D.ConstPool64, true);
Chris Lattner58af2a12006-02-15 07:22:58 +0000410
Chris Lattner1913b942008-07-11 00:30:39 +0000411 case ValID::ConstAPInt: // Is it an unsigned const pool reference?
412 if (!isa<IntegerType>(Ty)) {
413 GenerateError("Integral constant '" + D.getName() +
414 "' is invalid or out of range for type '" +
415 Ty->getDescription() + "'");
416 return 0;
417 }
Eric Christopher2a5196f2008-09-24 04:55:49 +0000418
Chris Lattner1913b942008-07-11 00:30:39 +0000419 {
420 APSInt Tmp = *D.ConstPoolInt;
Nuno Lopes19830332008-11-04 14:28:33 +0000421 D.destroy();
Chris Lattner1913b942008-07-11 00:30:39 +0000422 Tmp.extOrTrunc(Ty->getPrimitiveSizeInBits());
423 return ConstantInt::get(Tmp);
424 }
Eric Christopher2a5196f2008-09-24 04:55:49 +0000425
Chris Lattner58af2a12006-02-15 07:22:58 +0000426 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattner38905612008-02-19 04:36:25 +0000427 if (!Ty->isFloatingPoint() ||
428 !ConstantFP::isValueValidForType(Ty, *D.ConstPoolFP)) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000429 GenerateError("FP constant invalid for type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000430 return 0;
431 }
Eric Christopher2a5196f2008-09-24 04:55:49 +0000432 // Lexer has no type info, so builds all float and double FP constants
Dale Johannesenc72cd7e2007-09-11 18:33:39 +0000433 // as double. Fix this here. Long double does not need this.
434 if (&D.ConstPoolFP->getSemantics() == &APFloat::IEEEdouble &&
Dale Johannesen236bbd42008-10-09 23:01:34 +0000435 Ty==Type::FloatTy) {
436 bool ignored;
437 D.ConstPoolFP->convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven,
438 &ignored);
439 }
Chris Lattnerd8eb63f2008-04-20 00:41:19 +0000440 return ConstantFP::get(*D.ConstPoolFP);
Chris Lattner58af2a12006-02-15 07:22:58 +0000441
442 case ValID::ConstNullVal: // Is it a null value?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000443 if (!isa<PointerType>(Ty)) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000444 GenerateError("Cannot create a a non pointer null");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000445 return 0;
446 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000447 return ConstantPointerNull::get(cast<PointerType>(Ty));
448
449 case ValID::ConstUndefVal: // Is it an undef value?
450 return UndefValue::get(Ty);
451
452 case ValID::ConstZeroVal: // Is it a zero value?
453 return Constant::getNullValue(Ty);
Eric Christopher2a5196f2008-09-24 04:55:49 +0000454
Chris Lattner58af2a12006-02-15 07:22:58 +0000455 case ValID::ConstantVal: // Fully resolved constant?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000456 if (D.ConstantValue->getType() != Ty) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000457 GenerateError("Constant expression type different from required type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000458 return 0;
459 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000460 return D.ConstantValue;
461
462 case ValID::InlineAsmVal: { // Inline asm expression
463 const PointerType *PTy = dyn_cast<PointerType>(Ty);
464 const FunctionType *FTy =
465 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
Reid Spencer5b7e7532006-09-28 19:28:24 +0000466 if (!FTy || !InlineAsm::Verify(FTy, D.IAD->Constraints)) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000467 GenerateError("Invalid type for asm constraint string");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000468 return 0;
469 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000470 InlineAsm *IA = InlineAsm::get(FTy, D.IAD->AsmString, D.IAD->Constraints,
471 D.IAD->HasSideEffects);
472 D.destroy(); // Free InlineAsmDescriptor.
473 return IA;
474 }
475 default:
Reid Spencera9720f52007-02-05 17:04:00 +0000476 assert(0 && "Unhandled case!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000477 return 0;
478 } // End of switch
479
Reid Spencera9720f52007-02-05 17:04:00 +0000480 assert(0 && "Unhandled case!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000481 return 0;
482}
483
Reid Spencer93c40032007-03-19 18:40:50 +0000484// getVal - This function is identical to getExistingVal, except that if a
Chris Lattner58af2a12006-02-15 07:22:58 +0000485// value is not already defined, it "improvises" by creating a placeholder var
486// that looks and acts just like the requested variable. When the value is
487// defined later, all uses of the placeholder variable are replaced with the
488// real thing.
489//
490static Value *getVal(const Type *Ty, const ValID &ID) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000491 if (Ty == Type::LabelTy) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000492 GenerateError("Cannot use a basic block here");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000493 return 0;
494 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000495
496 // See if the value has already been defined.
Reid Spencer93c40032007-03-19 18:40:50 +0000497 Value *V = getExistingVal(Ty, ID);
Chris Lattner58af2a12006-02-15 07:22:58 +0000498 if (V) return V;
Reid Spencer5b7e7532006-09-28 19:28:24 +0000499 if (TriggerError) return 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000500
Reid Spencer5b7e7532006-09-28 19:28:24 +0000501 if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty)) {
Dan Gohmane4977cf2008-05-23 01:55:30 +0000502 GenerateError("Invalid use of a non-first-class type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000503 return 0;
504 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000505
506 // If we reached here, we referenced either a symbol that we don't know about
507 // or an id number that hasn't been read yet. We may be referencing something
508 // forward, so just create an entry to be resolved later and get to it...
509 //
Anton Korobeynikov38e09802007-04-28 13:48:45 +0000510 switch (ID.Type) {
511 case ValID::GlobalName:
Reid Spencer9c9b63a2007-04-28 16:07:31 +0000512 case ValID::GlobalID: {
Anton Korobeynikov38e09802007-04-28 13:48:45 +0000513 const PointerType *PTy = dyn_cast<PointerType>(Ty);
514 if (!PTy) {
515 GenerateError("Invalid type for reference to global" );
516 return 0;
517 }
518 const Type* ElTy = PTy->getElementType();
519 if (const FunctionType *FTy = dyn_cast<FunctionType>(ElTy))
Gabor Greife64d2482008-04-06 23:07:54 +0000520 V = Function::Create(FTy, GlobalValue::ExternalLinkage);
Anton Korobeynikov38e09802007-04-28 13:48:45 +0000521 else
Christopher Lamba8ed9bf2007-12-11 09:02:08 +0000522 V = new GlobalVariable(ElTy, false, GlobalValue::ExternalLinkage, 0, "",
523 (Module*)0, false, PTy->getAddressSpace());
Anton Korobeynikov38e09802007-04-28 13:48:45 +0000524 break;
Reid Spencer9c9b63a2007-04-28 16:07:31 +0000525 }
Anton Korobeynikov38e09802007-04-28 13:48:45 +0000526 default:
527 V = new Argument(Ty);
528 }
Eric Christopher2a5196f2008-09-24 04:55:49 +0000529
Chris Lattner58af2a12006-02-15 07:22:58 +0000530 // Remember where this forward reference came from. FIXME, shouldn't we try
531 // to recycle these things??
532 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
Duncan Sandsdc024672007-11-27 13:23:08 +0000533 LLLgetLineNo())));
Chris Lattner58af2a12006-02-15 07:22:58 +0000534
535 if (inFunctionScope())
536 InsertValue(V, CurFun.LateResolveValues);
537 else
538 InsertValue(V, CurModule.LateResolveValues);
539 return V;
540}
541
Reid Spencer93c40032007-03-19 18:40:50 +0000542/// defineBBVal - This is a definition of a new basic block with the specified
543/// identifier which must be the same as CurFun.NextValNum, if its numeric.
Nick Lewycky280a6e62008-04-25 16:53:59 +0000544static BasicBlock *defineBBVal(const ValID &ID) {
Reid Spencera9720f52007-02-05 17:04:00 +0000545 assert(inFunctionScope() && "Can't get basic block at global scope!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000546
Chris Lattner58af2a12006-02-15 07:22:58 +0000547 BasicBlock *BB = 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000548
Reid Spencer93c40032007-03-19 18:40:50 +0000549 // First, see if this was forward referenced
Chris Lattner58af2a12006-02-15 07:22:58 +0000550
Reid Spencer93c40032007-03-19 18:40:50 +0000551 std::map<ValID, BasicBlock*>::iterator BBI = CurFun.BBForwardRefs.find(ID);
552 if (BBI != CurFun.BBForwardRefs.end()) {
553 BB = BBI->second;
Chris Lattner58af2a12006-02-15 07:22:58 +0000554 // The forward declaration could have been inserted anywhere in the
555 // function: insert it into the correct place now.
556 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
557 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
Reid Spencer93c40032007-03-19 18:40:50 +0000558
Reid Spencer66728ef2007-03-20 01:13:36 +0000559 // We're about to erase the entry, save the key so we can clean it up.
560 ValID Tmp = BBI->first;
561
Reid Spencer93c40032007-03-19 18:40:50 +0000562 // Erase the forward ref from the map as its no longer "forward"
563 CurFun.BBForwardRefs.erase(ID);
564
Eric Christopher2a5196f2008-09-24 04:55:49 +0000565 // The key has been removed from the map but so we don't want to leave
Reid Spencer66728ef2007-03-20 01:13:36 +0000566 // strdup'd memory around so destroy it too.
567 Tmp.destroy();
568
Reid Spencer93c40032007-03-19 18:40:50 +0000569 // If its a numbered definition, bump the number and set the BB value.
570 if (ID.Type == ValID::LocalID) {
571 assert(ID.Num == CurFun.NextValNum && "Invalid new block number");
572 InsertValue(BB);
573 }
Eric Christopher2a5196f2008-09-24 04:55:49 +0000574 } else {
575 // We haven't seen this BB before and its first mention is a definition.
Devang Patel67909432008-03-03 18:58:47 +0000576 // Just create it and return it.
577 std::string Name (ID.Type == ValID::LocalName ? ID.getName() : "");
Gabor Greife64d2482008-04-06 23:07:54 +0000578 BB = BasicBlock::Create(Name, CurFun.CurrentFunction);
Devang Patel67909432008-03-03 18:58:47 +0000579 if (ID.Type == ValID::LocalID) {
580 assert(ID.Num == CurFun.NextValNum && "Invalid new block number");
581 InsertValue(BB);
582 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000583 }
Reid Spencer93c40032007-03-19 18:40:50 +0000584
Devang Patel67909432008-03-03 18:58:47 +0000585 ID.destroy();
Reid Spencer93c40032007-03-19 18:40:50 +0000586 return BB;
587}
588
589/// getBBVal - get an existing BB value or create a forward reference for it.
Eric Christopher2a5196f2008-09-24 04:55:49 +0000590///
Reid Spencer93c40032007-03-19 18:40:50 +0000591static BasicBlock *getBBVal(const ValID &ID) {
592 assert(inFunctionScope() && "Can't get basic block at global scope!");
593
594 BasicBlock *BB = 0;
595
596 std::map<ValID, BasicBlock*>::iterator BBI = CurFun.BBForwardRefs.find(ID);
597 if (BBI != CurFun.BBForwardRefs.end()) {
598 BB = BBI->second;
599 } if (ID.Type == ValID::LocalName) {
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000600 std::string Name = ID.getName();
Reid Spencer93c40032007-03-19 18:40:50 +0000601 Value *N = CurFun.CurrentFunction->getValueSymbolTable().lookup(Name);
Anton Korobeynikov4aefd6b2008-02-20 12:07:57 +0000602 if (N) {
Reid Spencer93c40032007-03-19 18:40:50 +0000603 if (N->getType()->getTypeID() == Type::LabelTyID)
604 BB = cast<BasicBlock>(N);
605 else
606 GenerateError("Reference to label '" + Name + "' is actually of type '"+
607 N->getType()->getDescription() + "'");
Anton Korobeynikov4aefd6b2008-02-20 12:07:57 +0000608 }
Reid Spencer93c40032007-03-19 18:40:50 +0000609 } else if (ID.Type == ValID::LocalID) {
610 if (ID.Num < CurFun.NextValNum && ID.Num < CurFun.Values.size()) {
611 if (CurFun.Values[ID.Num]->getType()->getTypeID() == Type::LabelTyID)
612 BB = cast<BasicBlock>(CurFun.Values[ID.Num]);
613 else
Eric Christopher2a5196f2008-09-24 04:55:49 +0000614 GenerateError("Reference to label '%" + utostr(ID.Num) +
615 "' is actually of type '"+
Reid Spencer93c40032007-03-19 18:40:50 +0000616 CurFun.Values[ID.Num]->getType()->getDescription() + "'");
617 }
618 } else {
619 GenerateError("Illegal label reference " + ID.getName());
620 return 0;
621 }
622
623 // If its already been defined, return it now.
624 if (BB) {
625 ID.destroy(); // Free strdup'd memory.
626 return BB;
627 }
628
629 // Otherwise, this block has not been seen before, create it.
630 std::string Name;
631 if (ID.Type == ValID::LocalName)
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000632 Name = ID.getName();
Gabor Greife64d2482008-04-06 23:07:54 +0000633 BB = BasicBlock::Create(Name, CurFun.CurrentFunction);
Reid Spencer93c40032007-03-19 18:40:50 +0000634
635 // Insert it in the forward refs map.
636 CurFun.BBForwardRefs[ID] = BB;
637
Chris Lattner58af2a12006-02-15 07:22:58 +0000638 return BB;
639}
640
641
642//===----------------------------------------------------------------------===//
643// Code to handle forward references in instructions
644//===----------------------------------------------------------------------===//
645//
646// This code handles the late binding needed with statements that reference
647// values not defined yet... for example, a forward branch, or the PHI node for
648// a loop body.
649//
650// This keeps a table (CurFun.LateResolveValues) of all such forward references
651// and back patchs after we are done.
652//
653
654// ResolveDefinitions - If we could not resolve some defs at parsing
655// time (forward branches, phi functions for loops, etc...) resolve the
656// defs now...
657//
Eric Christopher2a5196f2008-09-24 04:55:49 +0000658static void
Reid Spencer93c40032007-03-19 18:40:50 +0000659ResolveDefinitions(ValueList &LateResolvers, ValueList *FutureLateResolvers) {
Chris Lattner58af2a12006-02-15 07:22:58 +0000660 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Reid Spencer93c40032007-03-19 18:40:50 +0000661 while (!LateResolvers.empty()) {
662 Value *V = LateResolvers.back();
663 LateResolvers.pop_back();
Chris Lattner58af2a12006-02-15 07:22:58 +0000664
Reid Spencer93c40032007-03-19 18:40:50 +0000665 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
666 CurModule.PlaceHolderInfo.find(V);
667 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000668
Reid Spencer93c40032007-03-19 18:40:50 +0000669 ValID &DID = PHI->second.first;
Chris Lattner58af2a12006-02-15 07:22:58 +0000670
Reid Spencer93c40032007-03-19 18:40:50 +0000671 Value *TheRealValue = getExistingVal(V->getType(), DID);
672 if (TriggerError)
673 return;
674 if (TheRealValue) {
675 V->replaceAllUsesWith(TheRealValue);
676 delete V;
677 CurModule.PlaceHolderInfo.erase(PHI);
678 } else if (FutureLateResolvers) {
679 // Functions have their unresolved items forwarded to the module late
680 // resolver table
681 InsertValue(V, *FutureLateResolvers);
682 } else {
683 if (DID.Type == ValID::LocalName || DID.Type == ValID::GlobalName) {
684 GenerateError("Reference to an invalid definition: '" +DID.getName()+
685 "' of type '" + V->getType()->getDescription() + "'",
686 PHI->second.second);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000687 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000688 } else {
Reid Spencer93c40032007-03-19 18:40:50 +0000689 GenerateError("Reference to an invalid definition: #" +
690 itostr(DID.Num) + " of type '" +
691 V->getType()->getDescription() + "'",
692 PHI->second.second);
693 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000694 }
695 }
696 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000697 LateResolvers.clear();
698}
699
700// ResolveTypeTo - A brand new type was just declared. This means that (if
701// name is not null) things referencing Name can be resolved. Otherwise, things
702// refering to the number can be resolved. Do this now.
703//
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000704static void ResolveTypeTo(std::string *Name, const Type *ToTy) {
Chris Lattner58af2a12006-02-15 07:22:58 +0000705 ValID D;
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000706 if (Name)
707 D = ValID::createLocalName(*Name);
Eric Christopher2a5196f2008-09-24 04:55:49 +0000708 else
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000709 D = ValID::createLocalID(CurModule.Types.size());
Chris Lattner58af2a12006-02-15 07:22:58 +0000710
Reid Spencer861d9d62006-11-28 07:29:44 +0000711 std::map<ValID, PATypeHolder>::iterator I =
Chris Lattner58af2a12006-02-15 07:22:58 +0000712 CurModule.LateResolveTypes.find(D);
713 if (I != CurModule.LateResolveTypes.end()) {
Reid Spencer861d9d62006-11-28 07:29:44 +0000714 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Nuno Lopes6ec8a252008-10-15 11:11:12 +0000715 I->first.destroy();
Chris Lattner58af2a12006-02-15 07:22:58 +0000716 CurModule.LateResolveTypes.erase(I);
717 }
Nuno Lopes191dfb92008-10-03 15:52:39 +0000718 D.destroy();
Chris Lattner58af2a12006-02-15 07:22:58 +0000719}
720
721// setValueName - Set the specified value to the name given. The name may be
722// null potentially, in which case this is a noop. The string passed in is
723// assumed to be a malloc'd string buffer, and is free'd by this function.
724//
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000725static void setValueName(Value *V, std::string *NameStr) {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000726 if (!NameStr) return;
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000727 std::string Name(*NameStr); // Copy string
728 delete NameStr; // Free old string
Chris Lattner58af2a12006-02-15 07:22:58 +0000729
Reid Spencer41dff5e2007-01-26 08:05:27 +0000730 if (V->getType() == Type::VoidTy) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000731 GenerateError("Can't assign name '" + Name+"' to value with void type");
Reid Spencer41dff5e2007-01-26 08:05:27 +0000732 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000733 }
Reid Spencer41dff5e2007-01-26 08:05:27 +0000734
Reid Spencera9720f52007-02-05 17:04:00 +0000735 assert(inFunctionScope() && "Must be in function scope!");
Reid Spenceref9b9a72007-02-05 20:47:22 +0000736 ValueSymbolTable &ST = CurFun.CurrentFunction->getValueSymbolTable();
737 if (ST.lookup(Name)) {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000738 GenerateError("Redefinition of value '" + Name + "' of type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +0000739 V->getType()->getDescription() + "'");
Reid Spencer41dff5e2007-01-26 08:05:27 +0000740 return;
741 }
742
743 // Set the name.
744 V->setName(Name);
Chris Lattner58af2a12006-02-15 07:22:58 +0000745}
746
747/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
748/// this is a declaration, otherwise it is a definition.
749static GlobalVariable *
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000750ParseGlobalVariable(std::string *NameStr,
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000751 GlobalValue::LinkageTypes Linkage,
752 GlobalValue::VisibilityTypes Visibility,
Chris Lattner58af2a12006-02-15 07:22:58 +0000753 bool isConstantGlobal, const Type *Ty,
Christopher Lamba8ed9bf2007-12-11 09:02:08 +0000754 Constant *Initializer, bool IsThreadLocal,
755 unsigned AddressSpace = 0) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000756 if (isa<FunctionType>(Ty)) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000757 GenerateError("Cannot declare global vars of function type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000758 return 0;
759 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +0000760 if (Ty == Type::LabelTy) {
761 GenerateError("Cannot declare global vars of label type");
762 return 0;
763 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000764
Christopher Lamba8ed9bf2007-12-11 09:02:08 +0000765 const PointerType *PTy = PointerType::get(Ty, AddressSpace);
Chris Lattner58af2a12006-02-15 07:22:58 +0000766
767 std::string Name;
768 if (NameStr) {
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000769 Name = *NameStr; // Copy string
770 delete NameStr; // Free old string
Chris Lattner58af2a12006-02-15 07:22:58 +0000771 }
772
773 // See if this global value was forward referenced. If so, recycle the
774 // object.
775 ValID ID;
776 if (!Name.empty()) {
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000777 ID = ValID::createGlobalName(Name);
Chris Lattner58af2a12006-02-15 07:22:58 +0000778 } else {
Reid Spencer93c40032007-03-19 18:40:50 +0000779 ID = ValID::createGlobalID(CurModule.Values.size());
Chris Lattner58af2a12006-02-15 07:22:58 +0000780 }
781
782 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
783 // Move the global to the end of the list, from whereever it was
784 // previously inserted.
785 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
786 CurModule.CurrentModule->getGlobalList().remove(GV);
787 CurModule.CurrentModule->getGlobalList().push_back(GV);
788 GV->setInitializer(Initializer);
789 GV->setLinkage(Linkage);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000790 GV->setVisibility(Visibility);
Chris Lattner58af2a12006-02-15 07:22:58 +0000791 GV->setConstant(isConstantGlobal);
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +0000792 GV->setThreadLocal(IsThreadLocal);
Chris Lattner58af2a12006-02-15 07:22:58 +0000793 InsertValue(GV, CurModule.Values);
Nuno Lopes191dfb92008-10-03 15:52:39 +0000794 ID.destroy();
Chris Lattner58af2a12006-02-15 07:22:58 +0000795 return GV;
796 }
797
Nuno Lopes191dfb92008-10-03 15:52:39 +0000798 ID.destroy();
799
Reid Spenceref9b9a72007-02-05 20:47:22 +0000800 // If this global has a name
Chris Lattner58af2a12006-02-15 07:22:58 +0000801 if (!Name.empty()) {
Reid Spenceref9b9a72007-02-05 20:47:22 +0000802 // if the global we're parsing has an initializer (is a definition) and
803 // has external linkage.
804 if (Initializer && Linkage != GlobalValue::InternalLinkage)
805 // If there is already a global with external linkage with this name
806 if (CurModule.CurrentModule->getGlobalVariable(Name, false)) {
807 // If we allow this GVar to get created, it will be renamed in the
808 // symbol table because it conflicts with an existing GVar. We can't
809 // allow redefinition of GVars whose linking indicates that their name
810 // must stay the same. Issue the error.
811 GenerateError("Redefinition of global variable named '" + Name +
812 "' of type '" + Ty->getDescription() + "'");
813 return 0;
814 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000815 }
816
817 // Otherwise there is no existing GV to use, create one now.
818 GlobalVariable *GV =
819 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
Christopher Lamba8ed9bf2007-12-11 09:02:08 +0000820 CurModule.CurrentModule, IsThreadLocal, AddressSpace);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000821 GV->setVisibility(Visibility);
Chris Lattner58af2a12006-02-15 07:22:58 +0000822 InsertValue(GV, CurModule.Values);
823 return GV;
824}
825
826// setTypeName - Set the specified type to the name given. The name may be
827// null potentially, in which case this is a noop. The string passed in is
828// assumed to be a malloc'd string buffer, and is freed by this function.
829//
830// This function returns true if the type has already been defined, but is
831// allowed to be redefined in the specified context. If the name is a new name
832// for the type plane, it is inserted and false is returned.
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000833static bool setTypeName(const Type *T, std::string *NameStr) {
Reid Spencera9720f52007-02-05 17:04:00 +0000834 assert(!inFunctionScope() && "Can't give types function-local names!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000835 if (NameStr == 0) return false;
Eric Christopher2a5196f2008-09-24 04:55:49 +0000836
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000837 std::string Name(*NameStr); // Copy string
838 delete NameStr; // Free old string
Chris Lattner58af2a12006-02-15 07:22:58 +0000839
840 // We don't allow assigning names to void type
Reid Spencer5b7e7532006-09-28 19:28:24 +0000841 if (T == Type::VoidTy) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000842 GenerateError("Can't assign name '" + Name + "' to the void type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000843 return false;
844 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000845
846 // Set the type name, checking for conflicts as we do so.
847 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
848
849 if (AlreadyExists) { // Inserting a name that is already defined???
850 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
Reid Spencera9720f52007-02-05 17:04:00 +0000851 assert(Existing && "Conflict but no matching type?!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000852
853 // There is only one case where this is allowed: when we are refining an
854 // opaque type. In this case, Existing will be an opaque type.
855 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
856 // We ARE replacing an opaque type!
857 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
858 return true;
859 }
860
861 // Otherwise, this is an attempt to redefine a type. That's okay if
862 // the redefinition is identical to the original. This will be so if
863 // Existing and T point to the same Type object. In this one case we
864 // allow the equivalent redefinition.
865 if (Existing == T) return true; // Yes, it's equal.
866
867 // Any other kind of (non-equivalent) redefinition is an error.
Reid Spencer63c34452007-01-05 21:51:07 +0000868 GenerateError("Redefinition of type named '" + Name + "' of type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +0000869 T->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +0000870 }
871
872 return false;
873}
874
875//===----------------------------------------------------------------------===//
876// Code for handling upreferences in type names...
877//
878
879// TypeContains - Returns true if Ty directly contains E in it.
880//
881static bool TypeContains(const Type *Ty, const Type *E) {
882 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
883 E) != Ty->subtype_end();
884}
885
886namespace {
887 struct UpRefRecord {
888 // NestingLevel - The number of nesting levels that need to be popped before
889 // this type is resolved.
890 unsigned NestingLevel;
891
892 // LastContainedTy - This is the type at the current binding level for the
893 // type. Every time we reduce the nesting level, this gets updated.
894 const Type *LastContainedTy;
895
896 // UpRefTy - This is the actual opaque type that the upreference is
897 // represented with.
898 OpaqueType *UpRefTy;
899
900 UpRefRecord(unsigned NL, OpaqueType *URTy)
901 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
902 };
903}
904
905// UpRefs - A list of the outstanding upreferences that need to be resolved.
906static std::vector<UpRefRecord> UpRefs;
907
908/// HandleUpRefs - Every time we finish a new layer of types, this function is
909/// called. It loops through the UpRefs vector, which is a list of the
910/// currently active types. For each type, if the up reference is contained in
911/// the newly completed type, we decrement the level count. When the level
912/// count reaches zero, the upreferenced type is the type that is passed in:
913/// thus we can complete the cycle.
914///
915static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner224f84f2006-08-18 17:34:45 +0000916 // If Ty isn't abstract, or if there are no up-references in it, then there is
917 // nothing to resolve here.
918 if (!ty->isAbstract() || UpRefs.empty()) return ty;
Eric Christopher2a5196f2008-09-24 04:55:49 +0000919
Chris Lattner58af2a12006-02-15 07:22:58 +0000920 PATypeHolder Ty(ty);
921 UR_OUT("Type '" << Ty->getDescription() <<
922 "' newly formed. Resolving upreferences.\n" <<
923 UpRefs.size() << " upreferences active!\n");
924
925 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
926 // to zero), we resolve them all together before we resolve them to Ty. At
927 // the end of the loop, if there is anything to resolve to Ty, it will be in
928 // this variable.
929 OpaqueType *TypeToResolve = 0;
930
931 for (unsigned i = 0; i != UpRefs.size(); ++i) {
932 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
933 << UpRefs[i].second->getDescription() << ") = "
934 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
935 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
936 // Decrement level of upreference
937 unsigned Level = --UpRefs[i].NestingLevel;
938 UpRefs[i].LastContainedTy = Ty;
939 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
940 if (Level == 0) { // Upreference should be resolved!
941 if (!TypeToResolve) {
942 TypeToResolve = UpRefs[i].UpRefTy;
943 } else {
944 UR_OUT(" * Resolving upreference for "
945 << UpRefs[i].second->getDescription() << "\n";
946 std::string OldName = UpRefs[i].UpRefTy->getDescription());
947 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
948 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
949 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
950 }
951 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
952 --i; // Do not skip the next element...
953 }
954 }
955 }
956
957 if (TypeToResolve) {
958 UR_OUT(" * Resolving upreference for "
959 << UpRefs[i].second->getDescription() << "\n";
960 std::string OldName = TypeToResolve->getDescription());
961 TypeToResolve->refineAbstractTypeTo(Ty);
962 }
963
964 return Ty;
965}
966
Chris Lattner58af2a12006-02-15 07:22:58 +0000967//===----------------------------------------------------------------------===//
968// RunVMAsmParser - Define an interface to this parser
969//===----------------------------------------------------------------------===//
970//
Reid Spencer14310612006-12-31 05:40:51 +0000971static Module* RunParser(Module * M);
972
Duncan Sandsdc024672007-11-27 13:23:08 +0000973Module *llvm::RunVMAsmParser(llvm::MemoryBuffer *MB) {
974 InitLLLexer(MB);
975 Module *M = RunParser(new Module(LLLgetFilename()));
976 FreeLexer();
977 return M;
Chris Lattner58af2a12006-02-15 07:22:58 +0000978}
979
980%}
981
982%union {
983 llvm::Module *ModuleVal;
984 llvm::Function *FunctionVal;
Chris Lattner58af2a12006-02-15 07:22:58 +0000985 llvm::BasicBlock *BasicBlockVal;
986 llvm::TerminatorInst *TermInstVal;
987 llvm::Instruction *InstVal;
Reid Spencera132e042006-12-03 05:46:11 +0000988 llvm::Constant *ConstVal;
Chris Lattner58af2a12006-02-15 07:22:58 +0000989
Reid Spencera132e042006-12-03 05:46:11 +0000990 const llvm::Type *PrimType;
Reid Spencer14310612006-12-31 05:40:51 +0000991 std::list<llvm::PATypeHolder> *TypeList;
Reid Spencera132e042006-12-03 05:46:11 +0000992 llvm::PATypeHolder *TypeVal;
993 llvm::Value *ValueVal;
Reid Spencera132e042006-12-03 05:46:11 +0000994 std::vector<llvm::Value*> *ValueList;
Dan Gohman81a0c0b2008-05-31 00:58:22 +0000995 std::vector<unsigned> *ConstantList;
Reid Spencer14310612006-12-31 05:40:51 +0000996 llvm::ArgListType *ArgList;
997 llvm::TypeWithAttrs TypeWithAttrs;
998 llvm::TypeWithAttrsList *TypeWithAttrsList;
Dale Johanneseneb57ea72007-11-05 21:20:28 +0000999 llvm::ParamList *ParamList;
Reid Spencer14310612006-12-31 05:40:51 +00001000
Chris Lattner58af2a12006-02-15 07:22:58 +00001001 // Represent the RHS of PHI node
Reid Spencera132e042006-12-03 05:46:11 +00001002 std::list<std::pair<llvm::Value*,
1003 llvm::BasicBlock*> > *PHIList;
Chris Lattner58af2a12006-02-15 07:22:58 +00001004 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
Reid Spencera132e042006-12-03 05:46:11 +00001005 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner58af2a12006-02-15 07:22:58 +00001006
1007 llvm::GlobalValue::LinkageTypes Linkage;
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001008 llvm::GlobalValue::VisibilityTypes Visibility;
Devang Patel05988662008-09-25 21:00:45 +00001009 llvm::Attributes Attributes;
Reid Spencer38c91a92007-02-28 02:24:54 +00001010 llvm::APInt *APIntVal;
Chris Lattner58af2a12006-02-15 07:22:58 +00001011 int64_t SInt64Val;
1012 uint64_t UInt64Val;
1013 int SIntVal;
1014 unsigned UIntVal;
Dale Johannesen43421b32007-09-06 18:13:44 +00001015 llvm::APFloat *FPVal;
Chris Lattner58af2a12006-02-15 07:22:58 +00001016 bool BoolVal;
1017
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001018 std::string *StrVal; // This memory must be deleted
1019 llvm::ValID ValIDVal;
Chris Lattner58af2a12006-02-15 07:22:58 +00001020
Reid Spencera132e042006-12-03 05:46:11 +00001021 llvm::Instruction::BinaryOps BinaryOpVal;
1022 llvm::Instruction::TermOps TermOpVal;
1023 llvm::Instruction::MemoryOps MemOpVal;
1024 llvm::Instruction::CastOps CastOpVal;
1025 llvm::Instruction::OtherOps OtherOpVal;
Reid Spencera132e042006-12-03 05:46:11 +00001026 llvm::ICmpInst::Predicate IPredicate;
1027 llvm::FCmpInst::Predicate FPredicate;
Chris Lattner58af2a12006-02-15 07:22:58 +00001028}
1029
Eric Christopher2a5196f2008-09-24 04:55:49 +00001030%type <ModuleVal> Module
Chris Lattner58af2a12006-02-15 07:22:58 +00001031%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
1032%type <BasicBlockVal> BasicBlock InstructionList
1033%type <TermInstVal> BBTerminatorInst
1034%type <InstVal> Inst InstVal MemoryInst
Anton Korobeynikov38e09802007-04-28 13:48:45 +00001035%type <ConstVal> ConstVal ConstExpr AliaseeRef
Chris Lattner58af2a12006-02-15 07:22:58 +00001036%type <ConstVector> ConstVector
1037%type <ArgList> ArgList ArgListH
Chris Lattner58af2a12006-02-15 07:22:58 +00001038%type <PHIList> PHIList
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001039%type <ParamList> ParamList // For call param lists & GEP indices
Reid Spencer14310612006-12-31 05:40:51 +00001040%type <ValueList> IndexList // For GEP indices
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001041%type <ConstantList> ConstantIndexList // For insertvalue/extractvalue indices
Eric Christopher2a5196f2008-09-24 04:55:49 +00001042%type <TypeList> TypeListI
Reid Spencer14310612006-12-31 05:40:51 +00001043%type <TypeWithAttrsList> ArgTypeList ArgTypeListI
Reid Spencer218ded22007-01-05 17:07:23 +00001044%type <TypeWithAttrs> ArgType
Chris Lattner58af2a12006-02-15 07:22:58 +00001045%type <JumpTable> JumpTable
1046%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00001047%type <BoolVal> ThreadLocal // 'thread_local' or not
Chris Lattner58af2a12006-02-15 07:22:58 +00001048%type <BoolVal> OptVolatile // 'volatile' or not
1049%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
1050%type <BoolVal> OptSideEffect // 'sideeffect' or not.
Reid Spencer14310612006-12-31 05:40:51 +00001051%type <Linkage> GVInternalLinkage GVExternalLinkage
1052%type <Linkage> FunctionDefineLinkage FunctionDeclareLinkage
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001053%type <Linkage> AliasLinkage
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001054%type <Visibility> GVVisibilityStyle
Chris Lattner58af2a12006-02-15 07:22:58 +00001055
1056// ValueRef - Unresolved reference to a definition or BB
1057%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
1058%type <ValueVal> ResolvedVal // <type> <valref> pair
Devang Patel7990dc72008-02-20 22:40:23 +00001059%type <ValueList> ReturnedVal
Chris Lattner58af2a12006-02-15 07:22:58 +00001060// Tokens and types for handling constant integer values
1061//
1062// ESINT64VAL - A negative number within long long range
1063%token <SInt64Val> ESINT64VAL
1064
1065// EUINT64VAL - A positive number within uns. long long range
1066%token <UInt64Val> EUINT64VAL
Chris Lattner58af2a12006-02-15 07:22:58 +00001067
Eric Christopher2a5196f2008-09-24 04:55:49 +00001068// ESAPINTVAL - A negative number with arbitrary precision
Reid Spencer38c91a92007-02-28 02:24:54 +00001069%token <APIntVal> ESAPINTVAL
1070
Eric Christopher2a5196f2008-09-24 04:55:49 +00001071// EUAPINTVAL - A positive number with arbitrary precision
Reid Spencer38c91a92007-02-28 02:24:54 +00001072%token <APIntVal> EUAPINTVAL
1073
Reid Spencer41dff5e2007-01-26 08:05:27 +00001074%token <UIntVal> LOCALVAL_ID GLOBALVAL_ID // %123 @123
Chris Lattner58af2a12006-02-15 07:22:58 +00001075%token <FPVal> FPVAL // Float or Double constant
1076
1077// Built in types...
Reid Spencer218ded22007-01-05 17:07:23 +00001078%type <TypeVal> Types ResultTypes
Chris Lattner740e7092008-10-15 06:16:57 +00001079%type <PrimType> PrimType // Classifications
Eric Christopher2a5196f2008-09-24 04:55:49 +00001080%token <PrimType> VOID INTTYPE
Dale Johannesen320fc8a2007-08-03 01:03:46 +00001081%token <PrimType> FLOAT DOUBLE X86_FP80 FP128 PPC_FP128 LABEL
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001082%token TYPE
Chris Lattner58af2a12006-02-15 07:22:58 +00001083
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001084
Eric Christopher2a5196f2008-09-24 04:55:49 +00001085%token<StrVal> LOCALVAR GLOBALVAR LABELSTR
Reid Spencered951ea2007-05-19 07:22:10 +00001086%token<StrVal> STRINGCONSTANT ATSTRINGCONSTANT PCTSTRINGCONSTANT
Reid Spencer41dff5e2007-01-26 08:05:27 +00001087%type <StrVal> LocalName OptLocalName OptLocalAssign
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001088%type <StrVal> GlobalName OptGlobalAssign GlobalAssign
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00001089%type <StrVal> OptSection SectionString OptGC
Chris Lattner58af2a12006-02-15 07:22:58 +00001090
Christopher Lambbf3348d2007-12-12 08:45:45 +00001091%type <UIntVal> OptAlign OptCAlign OptAddrSpace
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001092
Reid Spencer3d6b71e2007-04-09 01:56:05 +00001093%token ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001094%token DECLARE DEFINE GLOBAL CONSTANT SECTION ALIAS VOLATILE THREAD_LOCAL
Reid Spencer14310612006-12-31 05:40:51 +00001095%token TO DOTDOTDOT NULL_TOK UNDEF INTERNAL LINKONCE WEAK APPENDING
Dale Johannesenc7071cc2008-05-14 20:13:36 +00001096%token DLLIMPORT DLLEXPORT EXTERN_WEAK COMMON
Christopher Lamba8ed9bf2007-12-11 09:02:08 +00001097%token OPAQUE EXTERNAL TARGET TRIPLE ALIGN ADDRSPACE
Chris Lattner58af2a12006-02-15 07:22:58 +00001098%token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001099%token CC_TOK CCC_TOK FASTCC_TOK COLDCC_TOK X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
Nick Lewycky280a6e62008-04-25 16:53:59 +00001100%token DATALAYOUT
Chris Lattner15bd0952008-08-29 17:20:18 +00001101%type <UIntVal> OptCallingConv LocalNumber
Devang Patel05988662008-09-25 21:00:45 +00001102%type <Attributes> OptAttributes Attribute
1103%type <Attributes> OptFuncAttrs FuncAttr
Devang Patel652203f2008-09-29 20:49:50 +00001104%type <Attributes> OptRetAttrs RetAttr
Chris Lattner58af2a12006-02-15 07:22:58 +00001105
1106// Basic Block Terminating Operators
1107%token <TermOpVal> RET BR SWITCH INVOKE UNWIND UNREACHABLE
1108
1109// Binary Operators
Reid Spencere4d87aa2006-12-23 06:05:41 +00001110%type <BinaryOpVal> ArithmeticOps LogicalOps // Binops Subcatagories
Reid Spencer3ed469c2006-11-02 20:25:50 +00001111%token <BinaryOpVal> ADD SUB MUL UDIV SDIV FDIV UREM SREM FREM AND OR XOR
Reid Spencer832254e2007-02-02 02:16:23 +00001112%token <BinaryOpVal> SHL LSHR ASHR
1113
Eric Christopher2a5196f2008-09-24 04:55:49 +00001114%token <OtherOpVal> ICMP FCMP VICMP VFCMP
Reid Spencera132e042006-12-03 05:46:11 +00001115%type <IPredicate> IPredicates
Reid Spencera132e042006-12-03 05:46:11 +00001116%type <FPredicate> FPredicates
Eric Christopher2a5196f2008-09-24 04:55:49 +00001117%token EQ NE SLT SGT SLE SGE ULT UGT ULE UGE
Reid Spencer6e18b7d2006-12-03 06:59:29 +00001118%token OEQ ONE OLT OGT OLE OGE ORD UNO UEQ UNE
Chris Lattner58af2a12006-02-15 07:22:58 +00001119
1120// Memory Instructions
1121%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
1122
Reid Spencer3da59db2006-11-27 01:05:10 +00001123// Cast Operators
1124%type <CastOpVal> CastOps
1125%token <CastOpVal> TRUNC ZEXT SEXT FPTRUNC FPEXT BITCAST
1126%token <CastOpVal> UITOFP SITOFP FPTOUI FPTOSI INTTOPTR PTRTOINT
1127
Chris Lattner58af2a12006-02-15 07:22:58 +00001128// Other Operators
Reid Spencer832254e2007-02-02 02:16:23 +00001129%token <OtherOpVal> PHI_TOK SELECT VAARG
Chris Lattnerd5efe842006-04-08 01:18:56 +00001130%token <OtherOpVal> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
Devang Patel5a970972008-02-19 22:27:01 +00001131%token <OtherOpVal> GETRESULT
Dan Gohmane4977cf2008-05-23 01:55:30 +00001132%token <OtherOpVal> EXTRACTVALUE INSERTVALUE
Chris Lattner58af2a12006-02-15 07:22:58 +00001133
Reid Spencer218ded22007-01-05 17:07:23 +00001134// Function Attributes
Reid Spencerb8f85052007-07-31 03:50:36 +00001135%token SIGNEXT ZEROEXT NORETURN INREG SRET NOUNWIND NOALIAS BYVAL NEST
Devang Patel2c9c3e72008-09-26 23:51:19 +00001136%token READNONE READONLY GC OPTSIZE NOINLINE ALWAYSINLINE
Devang Pateld4980812008-09-02 20:52:40 +00001137
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001138// Visibility Styles
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +00001139%token DEFAULT HIDDEN PROTECTED
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001140
Chris Lattner58af2a12006-02-15 07:22:58 +00001141%start Module
1142%%
1143
Chris Lattner58af2a12006-02-15 07:22:58 +00001144
Chris Lattner58af2a12006-02-15 07:22:58 +00001145// Operations that are notably excluded from this list include:
1146// RET, BR, & SWITCH because they end basic blocks and are treated specially.
1147//
Reid Spencer3ed469c2006-11-02 20:25:50 +00001148ArithmeticOps: ADD | SUB | MUL | UDIV | SDIV | FDIV | UREM | SREM | FREM;
Reid Spencer832254e2007-02-02 02:16:23 +00001149LogicalOps : SHL | LSHR | ASHR | AND | OR | XOR;
Eric Christopher2a5196f2008-09-24 04:55:49 +00001150CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | BITCAST |
Reid Spencer3da59db2006-11-27 01:05:10 +00001151 UITOFP | SITOFP | FPTOUI | FPTOSI | INTTOPTR | PTRTOINT;
Reid Spencer832254e2007-02-02 02:16:23 +00001152
Eric Christopher2a5196f2008-09-24 04:55:49 +00001153IPredicates
Reid Spencer4012e832006-12-04 05:24:24 +00001154 : EQ { $$ = ICmpInst::ICMP_EQ; } | NE { $$ = ICmpInst::ICMP_NE; }
Reid Spencer6e18b7d2006-12-03 06:59:29 +00001155 | SLT { $$ = ICmpInst::ICMP_SLT; } | SGT { $$ = ICmpInst::ICMP_SGT; }
1156 | SLE { $$ = ICmpInst::ICMP_SLE; } | SGE { $$ = ICmpInst::ICMP_SGE; }
1157 | ULT { $$ = ICmpInst::ICMP_ULT; } | UGT { $$ = ICmpInst::ICMP_UGT; }
Eric Christopher2a5196f2008-09-24 04:55:49 +00001158 | ULE { $$ = ICmpInst::ICMP_ULE; } | UGE { $$ = ICmpInst::ICMP_UGE; }
Reid Spencer6e18b7d2006-12-03 06:59:29 +00001159 ;
1160
Eric Christopher2a5196f2008-09-24 04:55:49 +00001161FPredicates
Reid Spencer6e18b7d2006-12-03 06:59:29 +00001162 : OEQ { $$ = FCmpInst::FCMP_OEQ; } | ONE { $$ = FCmpInst::FCMP_ONE; }
1163 | OLT { $$ = FCmpInst::FCMP_OLT; } | OGT { $$ = FCmpInst::FCMP_OGT; }
1164 | OLE { $$ = FCmpInst::FCMP_OLE; } | OGE { $$ = FCmpInst::FCMP_OGE; }
1165 | ORD { $$ = FCmpInst::FCMP_ORD; } | UNO { $$ = FCmpInst::FCMP_UNO; }
1166 | UEQ { $$ = FCmpInst::FCMP_UEQ; } | UNE { $$ = FCmpInst::FCMP_UNE; }
1167 | ULT { $$ = FCmpInst::FCMP_ULT; } | UGT { $$ = FCmpInst::FCMP_UGT; }
1168 | ULE { $$ = FCmpInst::FCMP_ULE; } | UGE { $$ = FCmpInst::FCMP_UGE; }
1169 | TRUETOK { $$ = FCmpInst::FCMP_TRUE; }
1170 | FALSETOK { $$ = FCmpInst::FCMP_FALSE; }
1171 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00001172
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001173LocalName : LOCALVAR | STRINGCONSTANT | PCTSTRINGCONSTANT ;
Reid Spencer41dff5e2007-01-26 08:05:27 +00001174OptLocalName : LocalName | /*empty*/ { $$ = 0; };
1175
Christopher Lambbf3348d2007-12-12 08:45:45 +00001176OptAddrSpace : ADDRSPACE '(' EUINT64VAL ')' { $$=$3; }
1177 | /*empty*/ { $$=0; };
1178
Reid Spencer41dff5e2007-01-26 08:05:27 +00001179/// OptLocalAssign - Value producing statements have an optional assignment
1180/// component.
1181OptLocalAssign : LocalName '=' {
1182 $$ = $1;
1183 CHECK_FOR_ERROR
1184 }
1185 | /*empty*/ {
1186 $$ = 0;
1187 CHECK_FOR_ERROR
1188 };
1189
Chris Lattner15bd0952008-08-29 17:20:18 +00001190LocalNumber : LOCALVAL_ID '=' {
1191 $$ = $1;
1192 CHECK_FOR_ERROR
1193};
1194
1195
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001196GlobalName : GLOBALVAR | ATSTRINGCONSTANT ;
Reid Spencer41dff5e2007-01-26 08:05:27 +00001197
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001198OptGlobalAssign : GlobalAssign
Chris Lattner58af2a12006-02-15 07:22:58 +00001199 | /*empty*/ {
1200 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00001201 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001202 };
1203
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001204GlobalAssign : GlobalName '=' {
1205 $$ = $1;
1206 CHECK_FOR_ERROR
Chris Lattner6cdc6822007-04-26 05:31:05 +00001207 };
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001208
Eric Christopher2a5196f2008-09-24 04:55:49 +00001209GVInternalLinkage
1210 : INTERNAL { $$ = GlobalValue::InternalLinkage; }
1211 | WEAK { $$ = GlobalValue::WeakLinkage; }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001212 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1213 | APPENDING { $$ = GlobalValue::AppendingLinkage; }
Eric Christopher2a5196f2008-09-24 04:55:49 +00001214 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
Dale Johannesenc7071cc2008-05-14 20:13:36 +00001215 | COMMON { $$ = GlobalValue::CommonLinkage; }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001216 ;
1217
1218GVExternalLinkage
1219 : DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1220 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
1221 | EXTERNAL { $$ = GlobalValue::ExternalLinkage; }
1222 ;
1223
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001224GVVisibilityStyle
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +00001225 : /*empty*/ { $$ = GlobalValue::DefaultVisibility; }
1226 | DEFAULT { $$ = GlobalValue::DefaultVisibility; }
1227 | HIDDEN { $$ = GlobalValue::HiddenVisibility; }
1228 | PROTECTED { $$ = GlobalValue::ProtectedVisibility; }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001229 ;
1230
Reid Spencer14310612006-12-31 05:40:51 +00001231FunctionDeclareLinkage
1232 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
Eric Christopher2a5196f2008-09-24 04:55:49 +00001233 | DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
Reid Spencer14310612006-12-31 05:40:51 +00001234 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001235 ;
Eric Christopher2a5196f2008-09-24 04:55:49 +00001236
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001237FunctionDefineLinkage
Reid Spencer14310612006-12-31 05:40:51 +00001238 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1239 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001240 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1241 | WEAK { $$ = GlobalValue::WeakLinkage; }
Eric Christopher2a5196f2008-09-24 04:55:49 +00001242 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
1243 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00001244
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001245AliasLinkage
1246 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1247 | WEAK { $$ = GlobalValue::WeakLinkage; }
1248 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
1249 ;
1250
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00001251OptCallingConv : /*empty*/ { $$ = CallingConv::C; } |
1252 CCC_TOK { $$ = CallingConv::C; } |
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00001253 FASTCC_TOK { $$ = CallingConv::Fast; } |
1254 COLDCC_TOK { $$ = CallingConv::Cold; } |
1255 X86_STDCALLCC_TOK { $$ = CallingConv::X86_StdCall; } |
1256 X86_FASTCALLCC_TOK { $$ = CallingConv::X86_FastCall; } |
1257 CC_TOK EUINT64VAL {
Chris Lattner58af2a12006-02-15 07:22:58 +00001258 if ((unsigned)$2 != $2)
Reid Spencerb5334b02007-02-05 10:18:06 +00001259 GEN_ERROR("Calling conv too large");
Chris Lattner58af2a12006-02-15 07:22:58 +00001260 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001261 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001262 };
1263
Devang Patel05988662008-09-25 21:00:45 +00001264Attribute : ZEROEXT { $$ = Attribute::ZExt; }
1265 | ZEXT { $$ = Attribute::ZExt; }
1266 | SIGNEXT { $$ = Attribute::SExt; }
1267 | SEXT { $$ = Attribute::SExt; }
1268 | INREG { $$ = Attribute::InReg; }
1269 | SRET { $$ = Attribute::StructRet; }
1270 | NOALIAS { $$ = Attribute::NoAlias; }
1271 | BYVAL { $$ = Attribute::ByVal; }
1272 | NEST { $$ = Attribute::Nest; }
Eric Christopher2a5196f2008-09-24 04:55:49 +00001273 | ALIGN EUINT64VAL { $$ =
Devang Patel05988662008-09-25 21:00:45 +00001274 Attribute::constructAlignmentFromInt($2); }
Reid Spencer14310612006-12-31 05:40:51 +00001275 ;
1276
Devang Patel05988662008-09-25 21:00:45 +00001277OptAttributes : /* empty */ { $$ = Attribute::None; }
1278 | OptAttributes Attribute {
Reid Spencer7b5d4662007-04-09 06:16:21 +00001279 $$ = $1 | $2;
Reid Spencer14310612006-12-31 05:40:51 +00001280 }
1281 ;
1282
Devang Patel652203f2008-09-29 20:49:50 +00001283RetAttr : INREG { $$ = Attribute::InReg; }
1284 | ZEROEXT { $$ = Attribute::ZExt; }
1285 | SIGNEXT { $$ = Attribute::SExt; }
1286 ;
1287
1288OptRetAttrs : /* empty */ { $$ = Attribute::None; }
1289 | OptRetAttrs RetAttr {
1290 $$ = $1 | $2;
1291 }
1292 ;
1293
1294
Devang Patel05988662008-09-25 21:00:45 +00001295FuncAttr : NORETURN { $$ = Attribute::NoReturn; }
1296 | NOUNWIND { $$ = Attribute::NoUnwind; }
1297 | INREG { $$ = Attribute::InReg; }
1298 | ZEROEXT { $$ = Attribute::ZExt; }
1299 | SIGNEXT { $$ = Attribute::SExt; }
1300 | READNONE { $$ = Attribute::ReadNone; }
1301 | READONLY { $$ = Attribute::ReadOnly; }
Chris Lattner9fc4da42008-10-08 06:44:45 +00001302 | NOINLINE { $$ = Attribute::NoInline; }
1303 | ALWAYSINLINE { $$ = Attribute::AlwaysInline; }
1304 | OPTSIZE { $$ = Attribute::OptimizeForSize; }
Reid Spencer218ded22007-01-05 17:07:23 +00001305 ;
1306
Devang Patel05988662008-09-25 21:00:45 +00001307OptFuncAttrs : /* empty */ { $$ = Attribute::None; }
Reid Spencer218ded22007-01-05 17:07:23 +00001308 | OptFuncAttrs FuncAttr {
Reid Spencer7b5d4662007-04-09 06:16:21 +00001309 $$ = $1 | $2;
Reid Spencer218ded22007-01-05 17:07:23 +00001310 }
Reid Spencer14310612006-12-31 05:40:51 +00001311 ;
1312
Devang Patel652203f2008-09-29 20:49:50 +00001313
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00001314OptGC : /* empty */ { $$ = 0; }
1315 | GC STRINGCONSTANT {
1316 $$ = $2;
1317 }
1318 ;
1319
Chris Lattner58af2a12006-02-15 07:22:58 +00001320// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1321// a comma before it.
1322OptAlign : /*empty*/ { $$ = 0; } |
1323 ALIGN EUINT64VAL {
1324 $$ = $2;
1325 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencerb5334b02007-02-05 10:18:06 +00001326 GEN_ERROR("Alignment must be a power of two");
Reid Spencer61c83e02006-08-18 08:43:06 +00001327 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001328};
1329OptCAlign : /*empty*/ { $$ = 0; } |
1330 ',' ALIGN EUINT64VAL {
1331 $$ = $3;
1332 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencerb5334b02007-02-05 10:18:06 +00001333 GEN_ERROR("Alignment must be a power of two");
Reid Spencer61c83e02006-08-18 08:43:06 +00001334 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001335};
1336
1337
Christopher Lamba8ed9bf2007-12-11 09:02:08 +00001338
Chris Lattner58af2a12006-02-15 07:22:58 +00001339SectionString : SECTION STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001340 for (unsigned i = 0, e = $2->length(); i != e; ++i)
1341 if ((*$2)[i] == '"' || (*$2)[i] == '\\')
Reid Spencerb5334b02007-02-05 10:18:06 +00001342 GEN_ERROR("Invalid character in section name");
Chris Lattner58af2a12006-02-15 07:22:58 +00001343 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001344 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001345};
1346
1347OptSection : /*empty*/ { $$ = 0; } |
1348 SectionString { $$ = $1; };
1349
1350// GlobalVarAttributes - Used to pass the attributes string on a global. CurGV
1351// is set to be the global we are processing.
1352//
1353GlobalVarAttributes : /* empty */ {} |
1354 ',' GlobalVarAttribute GlobalVarAttributes {};
1355GlobalVarAttribute : SectionString {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001356 CurGV->setSection(*$1);
1357 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001358 CHECK_FOR_ERROR
Eric Christopher2a5196f2008-09-24 04:55:49 +00001359 }
Chris Lattner58af2a12006-02-15 07:22:58 +00001360 | ALIGN EUINT64VAL {
1361 if ($2 != 0 && !isPowerOf2_32($2))
Reid Spencerb5334b02007-02-05 10:18:06 +00001362 GEN_ERROR("Alignment must be a power of two");
Chris Lattner58af2a12006-02-15 07:22:58 +00001363 CurGV->setAlignment($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001364 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001365 };
1366
1367//===----------------------------------------------------------------------===//
1368// Types includes all predefined types... except void, because it can only be
Eric Christopher2a5196f2008-09-24 04:55:49 +00001369// used in specific contexts (function returning void for example).
Chris Lattner58af2a12006-02-15 07:22:58 +00001370
1371// Derived types are added later...
1372//
Dale Johannesen320fc8a2007-08-03 01:03:46 +00001373PrimType : INTTYPE | FLOAT | DOUBLE | PPC_FP128 | FP128 | X86_FP80 | LABEL ;
Reid Spencer14310612006-12-31 05:40:51 +00001374
Eric Christopher2a5196f2008-09-24 04:55:49 +00001375Types
Reid Spencer14310612006-12-31 05:40:51 +00001376 : OPAQUE {
Reid Spencera132e042006-12-03 05:46:11 +00001377 $$ = new PATypeHolder(OpaqueType::get());
Reid Spencer61c83e02006-08-18 08:43:06 +00001378 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001379 }
1380 | PrimType {
Reid Spencera132e042006-12-03 05:46:11 +00001381 $$ = new PATypeHolder($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001382 CHECK_FOR_ERROR
Reid Spencer14310612006-12-31 05:40:51 +00001383 }
Christopher Lambbf3348d2007-12-12 08:45:45 +00001384 | Types OptAddrSpace '*' { // Pointer type?
Reid Spencer14310612006-12-31 05:40:51 +00001385 if (*$1 == Type::LabelTy)
1386 GEN_ERROR("Cannot form a pointer to a basic block");
Christopher Lambbf3348d2007-12-12 08:45:45 +00001387 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1, $2)));
Christopher Lamba8ed9bf2007-12-11 09:02:08 +00001388 delete $1;
1389 CHECK_FOR_ERROR
1390 }
Reid Spencer14310612006-12-31 05:40:51 +00001391 | SymbolicValueRef { // Named types are also simple types...
1392 const Type* tmp = getTypeVal($1);
1393 CHECK_FOR_ERROR
1394 $$ = new PATypeHolder(tmp);
1395 }
1396 | '\\' EUINT64VAL { // Type UpReference
Reid Spencerb5334b02007-02-05 10:18:06 +00001397 if ($2 > (uint64_t)~0U) GEN_ERROR("Value out of range");
Chris Lattner58af2a12006-02-15 07:22:58 +00001398 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
1399 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Reid Spencera132e042006-12-03 05:46:11 +00001400 $$ = new PATypeHolder(OT);
Chris Lattner58af2a12006-02-15 07:22:58 +00001401 UR_OUT("New Upreference!\n");
Reid Spencer61c83e02006-08-18 08:43:06 +00001402 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001403 }
Reid Spencer218ded22007-01-05 17:07:23 +00001404 | Types '(' ArgTypeListI ')' OptFuncAttrs {
Duncan Sandsdc024672007-11-27 13:23:08 +00001405 // Allow but ignore attributes on function types; this permits auto-upgrade.
1406 // FIXME: remove in LLVM 3.0.
Chris Lattnera925a142008-04-23 05:37:08 +00001407 const Type *RetTy = *$1;
1408 if (!FunctionType::isValidReturnType(RetTy))
1409 GEN_ERROR("Invalid result type for LLVM function");
Eric Christopher2a5196f2008-09-24 04:55:49 +00001410
Chris Lattner58af2a12006-02-15 07:22:58 +00001411 std::vector<const Type*> Params;
Reid Spencer7b5d4662007-04-09 06:16:21 +00001412 TypeWithAttrsList::iterator I = $3->begin(), E = $3->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00001413 for (; I != E; ++I ) {
Reid Spencer66728ef2007-03-20 01:13:36 +00001414 const Type *Ty = I->Ty->get();
Reid Spencer66728ef2007-03-20 01:13:36 +00001415 Params.push_back(Ty);
Reid Spencer14310612006-12-31 05:40:51 +00001416 }
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001417
Chris Lattner58af2a12006-02-15 07:22:58 +00001418 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1419 if (isVarArg) Params.pop_back();
1420
Anton Korobeynikov05e5a742007-12-03 21:01:29 +00001421 for (unsigned i = 0; i != Params.size(); ++i)
1422 if (!(Params[i]->isFirstClassType() || isa<OpaqueType>(Params[i])))
1423 GEN_ERROR("Function arguments must be value types!");
1424
1425 CHECK_FOR_ERROR
1426
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001427 FunctionType *FT = FunctionType::get(RetTy, Params, isVarArg);
Reid Spencer14310612006-12-31 05:40:51 +00001428 delete $1; // Delete the return type handle
Eric Christopher2a5196f2008-09-24 04:55:49 +00001429 $$ = new PATypeHolder(HandleUpRefs(FT));
Nuno Lopes8a88a372008-10-05 16:49:34 +00001430
1431 // Delete the argument list
1432 for (I = $3->begin() ; I != E; ++I ) {
1433 delete I->Ty;
1434 }
1435 delete $3;
1436
Reid Spencer61c83e02006-08-18 08:43:06 +00001437 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001438 }
Reid Spencer218ded22007-01-05 17:07:23 +00001439 | VOID '(' ArgTypeListI ')' OptFuncAttrs {
Duncan Sandsdc024672007-11-27 13:23:08 +00001440 // Allow but ignore attributes on function types; this permits auto-upgrade.
1441 // FIXME: remove in LLVM 3.0.
Reid Spencer14310612006-12-31 05:40:51 +00001442 std::vector<const Type*> Params;
Reid Spencer7b5d4662007-04-09 06:16:21 +00001443 TypeWithAttrsList::iterator I = $3->begin(), E = $3->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00001444 for ( ; I != E; ++I ) {
Reid Spencer66728ef2007-03-20 01:13:36 +00001445 const Type* Ty = I->Ty->get();
Reid Spencer66728ef2007-03-20 01:13:36 +00001446 Params.push_back(Ty);
Reid Spencer14310612006-12-31 05:40:51 +00001447 }
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001448
Reid Spencer14310612006-12-31 05:40:51 +00001449 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1450 if (isVarArg) Params.pop_back();
1451
Anton Korobeynikov05e5a742007-12-03 21:01:29 +00001452 for (unsigned i = 0; i != Params.size(); ++i)
1453 if (!(Params[i]->isFirstClassType() || isa<OpaqueType>(Params[i])))
1454 GEN_ERROR("Function arguments must be value types!");
1455
1456 CHECK_FOR_ERROR
1457
Duncan Sandsdc024672007-11-27 13:23:08 +00001458 FunctionType *FT = FunctionType::get($1, Params, isVarArg);
Eric Christopher2a5196f2008-09-24 04:55:49 +00001459 $$ = new PATypeHolder(HandleUpRefs(FT));
Nuno Lopes8a88a372008-10-05 16:49:34 +00001460
1461 // Delete the argument list
1462 for (I = $3->begin() ; I != E; ++I ) {
1463 delete I->Ty;
1464 }
1465 delete $3;
1466
Reid Spencer14310612006-12-31 05:40:51 +00001467 CHECK_FOR_ERROR
1468 }
1469
1470 | '[' EUINT64VAL 'x' Types ']' { // Sized array type?
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001471 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, $2)));
Reid Spencera132e042006-12-03 05:46:11 +00001472 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00001473 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001474 }
Chris Lattner32980692007-02-19 07:44:24 +00001475 | '<' EUINT64VAL 'x' Types '>' { // Vector type?
Reid Spencera132e042006-12-03 05:46:11 +00001476 const llvm::Type* ElemTy = $4->get();
1477 if ((unsigned)$2 != $2)
1478 GEN_ERROR("Unsigned result not equal to signed result");
Chris Lattner42a75512007-01-15 02:27:26 +00001479 if (!ElemTy->isFloatingPoint() && !ElemTy->isInteger())
Reid Spencer9d6565a2007-02-15 02:26:10 +00001480 GEN_ERROR("Element type of a VectorType must be primitive");
Reid Spencer9d6565a2007-02-15 02:26:10 +00001481 $$ = new PATypeHolder(HandleUpRefs(VectorType::get(*$4, (unsigned)$2)));
Reid Spencera132e042006-12-03 05:46:11 +00001482 delete $4;
1483 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001484 }
1485 | '{' TypeListI '}' { // Structure type?
1486 std::vector<const Type*> Elements;
Reid Spencera132e042006-12-03 05:46:11 +00001487 for (std::list<llvm::PATypeHolder>::iterator I = $2->begin(),
Chris Lattner58af2a12006-02-15 07:22:58 +00001488 E = $2->end(); I != E; ++I)
Reid Spencera132e042006-12-03 05:46:11 +00001489 Elements.push_back(*I);
Chris Lattner58af2a12006-02-15 07:22:58 +00001490
Reid Spencera132e042006-12-03 05:46:11 +00001491 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattner58af2a12006-02-15 07:22:58 +00001492 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001493 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001494 }
1495 | '{' '}' { // Empty structure type?
Reid Spencera132e042006-12-03 05:46:11 +00001496 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Reid Spencer61c83e02006-08-18 08:43:06 +00001497 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001498 }
Andrew Lenharth6353e052006-12-08 18:07:09 +00001499 | '<' '{' TypeListI '}' '>' {
1500 std::vector<const Type*> Elements;
1501 for (std::list<llvm::PATypeHolder>::iterator I = $3->begin(),
1502 E = $3->end(); I != E; ++I)
1503 Elements.push_back(*I);
1504
1505 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements, true)));
1506 delete $3;
1507 CHECK_FOR_ERROR
1508 }
1509 | '<' '{' '}' '>' { // Empty structure type?
1510 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>(), true));
1511 CHECK_FOR_ERROR
1512 }
Reid Spencer14310612006-12-31 05:40:51 +00001513 ;
1514
Eric Christopher2a5196f2008-09-24 04:55:49 +00001515ArgType
Devang Patel05988662008-09-25 21:00:45 +00001516 : Types OptAttributes {
Duncan Sandsdc024672007-11-27 13:23:08 +00001517 // Allow but ignore attributes on function types; this permits auto-upgrade.
1518 // FIXME: remove in LLVM 3.0.
Eric Christopher2a5196f2008-09-24 04:55:49 +00001519 $$.Ty = $1;
Devang Patel05988662008-09-25 21:00:45 +00001520 $$.Attrs = Attribute::None;
Reid Spencer14310612006-12-31 05:40:51 +00001521 }
1522 ;
1523
Reid Spencer218ded22007-01-05 17:07:23 +00001524ResultTypes
1525 : Types {
Reid Spencer14310612006-12-31 05:40:51 +00001526 if (!UpRefs.empty())
1527 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Devang Patel20071732008-02-23 01:17:37 +00001528 if (!(*$1)->isFirstClassType() && !isa<StructType>($1->get()))
Reid Spencerb5334b02007-02-05 10:18:06 +00001529 GEN_ERROR("LLVM functions cannot return aggregate types");
Reid Spencer218ded22007-01-05 17:07:23 +00001530 $$ = $1;
Reid Spencer14310612006-12-31 05:40:51 +00001531 }
Reid Spencer218ded22007-01-05 17:07:23 +00001532 | VOID {
1533 $$ = new PATypeHolder(Type::VoidTy);
Reid Spencer14310612006-12-31 05:40:51 +00001534 }
1535 ;
1536
1537ArgTypeList : ArgType {
1538 $$ = new TypeWithAttrsList();
1539 $$->push_back($1);
1540 CHECK_FOR_ERROR
1541 }
1542 | ArgTypeList ',' ArgType {
1543 ($$=$1)->push_back($3);
1544 CHECK_FOR_ERROR
1545 }
1546 ;
1547
Eric Christopher2a5196f2008-09-24 04:55:49 +00001548ArgTypeListI
Reid Spencer14310612006-12-31 05:40:51 +00001549 : ArgTypeList
1550 | ArgTypeList ',' DOTDOTDOT {
1551 $$=$1;
Devang Patel05988662008-09-25 21:00:45 +00001552 TypeWithAttrs TWA; TWA.Attrs = Attribute::None;
Reid Spencer14310612006-12-31 05:40:51 +00001553 TWA.Ty = new PATypeHolder(Type::VoidTy);
1554 $$->push_back(TWA);
1555 CHECK_FOR_ERROR
1556 }
1557 | DOTDOTDOT {
1558 $$ = new TypeWithAttrsList;
Devang Patel05988662008-09-25 21:00:45 +00001559 TypeWithAttrs TWA; TWA.Attrs = Attribute::None;
Reid Spencer14310612006-12-31 05:40:51 +00001560 TWA.Ty = new PATypeHolder(Type::VoidTy);
1561 $$->push_back(TWA);
1562 CHECK_FOR_ERROR
1563 }
1564 | /*empty*/ {
1565 $$ = new TypeWithAttrsList();
Reid Spencer61c83e02006-08-18 08:43:06 +00001566 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001567 };
1568
Eric Christopher2a5196f2008-09-24 04:55:49 +00001569// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner58af2a12006-02-15 07:22:58 +00001570// declaration type lists
1571//
Reid Spencer14310612006-12-31 05:40:51 +00001572TypeListI : Types {
Reid Spencera132e042006-12-03 05:46:11 +00001573 $$ = new std::list<PATypeHolder>();
Eric Christopher2a5196f2008-09-24 04:55:49 +00001574 $$->push_back(*$1);
Reid Spencer66728ef2007-03-20 01:13:36 +00001575 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001576 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001577 }
Reid Spencer14310612006-12-31 05:40:51 +00001578 | TypeListI ',' Types {
Eric Christopher2a5196f2008-09-24 04:55:49 +00001579 ($$=$1)->push_back(*$3);
Reid Spencer66728ef2007-03-20 01:13:36 +00001580 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001581 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001582 };
1583
Chris Lattner58af2a12006-02-15 07:22:58 +00001584// ConstVal - The various declarations that go into the constant pool. This
1585// production is used ONLY to represent constants that show up AFTER a 'const',
1586// 'constant' or 'global' token at global scope. Constants that can be inlined
1587// into other expressions (such as integers and constexprs) are handled by the
1588// ResolvedVal, ValueRef and ConstValueRef productions.
1589//
1590ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencer14310612006-12-31 05:40:51 +00001591 if (!UpRefs.empty())
1592 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001593 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001594 if (ATy == 0)
Eric Christopher2a5196f2008-09-24 04:55:49 +00001595 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001596 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001597 const Type *ETy = ATy->getElementType();
Dan Gohman180c1692008-06-23 18:43:26 +00001598 uint64_t NumElements = ATy->getNumElements();
Chris Lattner58af2a12006-02-15 07:22:58 +00001599
1600 // Verify that we have the correct size...
Mon P Wang28873102008-06-25 08:15:39 +00001601 if (NumElements != uint64_t(-1) && NumElements != $3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001602 GEN_ERROR("Type mismatch: constant sized array initialized with " +
Eric Christopher2a5196f2008-09-24 04:55:49 +00001603 utostr($3->size()) + " arguments, but has size of " +
Mon P Wang28873102008-06-25 08:15:39 +00001604 utostr(NumElements) + "");
Chris Lattner58af2a12006-02-15 07:22:58 +00001605
1606 // Verify all elements are correct type!
1607 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencera132e042006-12-03 05:46:11 +00001608 if (ETy != (*$3)[i]->getType())
Eric Christopher2a5196f2008-09-24 04:55:49 +00001609 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00001610 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencera132e042006-12-03 05:46:11 +00001611 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner58af2a12006-02-15 07:22:58 +00001612 }
1613
Reid Spencera132e042006-12-03 05:46:11 +00001614 $$ = ConstantArray::get(ATy, *$3);
1615 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001616 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001617 }
1618 | Types '[' ']' {
Reid Spencer14310612006-12-31 05:40:51 +00001619 if (!UpRefs.empty())
1620 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001621 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001622 if (ATy == 0)
Eric Christopher2a5196f2008-09-24 04:55:49 +00001623 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001624 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001625
Dan Gohman180c1692008-06-23 18:43:26 +00001626 uint64_t NumElements = ATy->getNumElements();
Eric Christopher2a5196f2008-09-24 04:55:49 +00001627 if (NumElements != uint64_t(-1) && NumElements != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001628 GEN_ERROR("Type mismatch: constant sized array initialized with 0"
Mon P Wang28873102008-06-25 08:15:39 +00001629 " arguments, but has size of " + utostr(NumElements) +"");
Reid Spencera132e042006-12-03 05:46:11 +00001630 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
1631 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001632 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001633 }
1634 | Types 'c' STRINGCONSTANT {
Reid Spencer14310612006-12-31 05:40:51 +00001635 if (!UpRefs.empty())
1636 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001637 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001638 if (ATy == 0)
Eric Christopher2a5196f2008-09-24 04:55:49 +00001639 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001640 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001641
Dan Gohman180c1692008-06-23 18:43:26 +00001642 uint64_t NumElements = ATy->getNumElements();
Chris Lattner58af2a12006-02-15 07:22:58 +00001643 const Type *ETy = ATy->getElementType();
Mon P Wang28873102008-06-25 08:15:39 +00001644 if (NumElements != uint64_t(-1) && NumElements != $3->length())
Eric Christopher2a5196f2008-09-24 04:55:49 +00001645 GEN_ERROR("Can't build string constant of size " +
Mon P Wang28873102008-06-25 08:15:39 +00001646 utostr($3->length()) +
1647 " when array has size " + utostr(NumElements) + "");
Chris Lattner58af2a12006-02-15 07:22:58 +00001648 std::vector<Constant*> Vals;
Reid Spencer14310612006-12-31 05:40:51 +00001649 if (ETy == Type::Int8Ty) {
Mon P Wang28873102008-06-25 08:15:39 +00001650 for (uint64_t i = 0; i < $3->length(); ++i)
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001651 Vals.push_back(ConstantInt::get(ETy, (*$3)[i]));
Chris Lattner58af2a12006-02-15 07:22:58 +00001652 } else {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001653 delete $3;
Reid Spencerb5334b02007-02-05 10:18:06 +00001654 GEN_ERROR("Cannot build string arrays of non byte sized elements");
Chris Lattner58af2a12006-02-15 07:22:58 +00001655 }
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001656 delete $3;
Reid Spencera132e042006-12-03 05:46:11 +00001657 $$ = ConstantArray::get(ATy, Vals);
1658 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001659 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001660 }
1661 | Types '<' ConstVector '>' { // Nonempty unsized arr
Reid Spencer14310612006-12-31 05:40:51 +00001662 if (!UpRefs.empty())
1663 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer9d6565a2007-02-15 02:26:10 +00001664 const VectorType *PTy = dyn_cast<VectorType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001665 if (PTy == 0)
Eric Christopher2a5196f2008-09-24 04:55:49 +00001666 GEN_ERROR("Cannot make packed constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001667 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001668 const Type *ETy = PTy->getElementType();
Dan Gohman180c1692008-06-23 18:43:26 +00001669 unsigned NumElements = PTy->getNumElements();
Chris Lattner58af2a12006-02-15 07:22:58 +00001670
1671 // Verify that we have the correct size...
Mon P Wang28873102008-06-25 08:15:39 +00001672 if (NumElements != unsigned(-1) && NumElements != (unsigned)$3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001673 GEN_ERROR("Type mismatch: constant sized packed initialized with " +
Eric Christopher2a5196f2008-09-24 04:55:49 +00001674 utostr($3->size()) + " arguments, but has size of " +
Mon P Wang28873102008-06-25 08:15:39 +00001675 utostr(NumElements) + "");
Chris Lattner58af2a12006-02-15 07:22:58 +00001676
1677 // Verify all elements are correct type!
1678 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencera132e042006-12-03 05:46:11 +00001679 if (ETy != (*$3)[i]->getType())
Eric Christopher2a5196f2008-09-24 04:55:49 +00001680 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00001681 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencera132e042006-12-03 05:46:11 +00001682 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner58af2a12006-02-15 07:22:58 +00001683 }
1684
Reid Spencer9d6565a2007-02-15 02:26:10 +00001685 $$ = ConstantVector::get(PTy, *$3);
Reid Spencera132e042006-12-03 05:46:11 +00001686 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001687 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001688 }
1689 | Types '{' ConstVector '}' {
Reid Spencera132e042006-12-03 05:46:11 +00001690 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001691 if (STy == 0)
Eric Christopher2a5196f2008-09-24 04:55:49 +00001692 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001693 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001694
1695 if ($3->size() != STy->getNumContainedTypes())
Reid Spencerb5334b02007-02-05 10:18:06 +00001696 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattner58af2a12006-02-15 07:22:58 +00001697
1698 // Check to ensure that constants are compatible with the type initializer!
1699 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Reid Spencera132e042006-12-03 05:46:11 +00001700 if ((*$3)[i]->getType() != STy->getElementType(i))
Reid Spencer61c83e02006-08-18 08:43:06 +00001701 GEN_ERROR("Expected type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00001702 STy->getElementType(i)->getDescription() +
1703 "' for element #" + utostr(i) +
Reid Spencerb5334b02007-02-05 10:18:06 +00001704 " of structure initializer");
Chris Lattner58af2a12006-02-15 07:22:58 +00001705
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001706 // Check to ensure that Type is not packed
1707 if (STy->isPacked())
Chris Lattner6cdc6822007-04-26 05:31:05 +00001708 GEN_ERROR("Unpacked Initializer to vector type '" +
1709 STy->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001710
Reid Spencera132e042006-12-03 05:46:11 +00001711 $$ = ConstantStruct::get(STy, *$3);
1712 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001713 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001714 }
1715 | Types '{' '}' {
Reid Spencer14310612006-12-31 05:40:51 +00001716 if (!UpRefs.empty())
1717 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001718 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001719 if (STy == 0)
Eric Christopher2a5196f2008-09-24 04:55:49 +00001720 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001721 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001722
1723 if (STy->getNumContainedTypes() != 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00001724 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattner58af2a12006-02-15 07:22:58 +00001725
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001726 // Check to ensure that Type is not packed
1727 if (STy->isPacked())
Chris Lattner6cdc6822007-04-26 05:31:05 +00001728 GEN_ERROR("Unpacked Initializer to vector type '" +
1729 STy->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001730
1731 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1732 delete $1;
1733 CHECK_FOR_ERROR
1734 }
1735 | Types '<' '{' ConstVector '}' '>' {
1736 const StructType *STy = dyn_cast<StructType>($1->get());
1737 if (STy == 0)
Eric Christopher2a5196f2008-09-24 04:55:49 +00001738 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001739 (*$1)->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001740
1741 if ($4->size() != STy->getNumContainedTypes())
Reid Spencerb5334b02007-02-05 10:18:06 +00001742 GEN_ERROR("Illegal number of initializers for structure type");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001743
1744 // Check to ensure that constants are compatible with the type initializer!
1745 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1746 if ((*$4)[i]->getType() != STy->getElementType(i))
1747 GEN_ERROR("Expected type '" +
1748 STy->getElementType(i)->getDescription() +
1749 "' for element #" + utostr(i) +
Reid Spencerb5334b02007-02-05 10:18:06 +00001750 " of structure initializer");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001751
1752 // Check to ensure that Type is packed
1753 if (!STy->isPacked())
Eric Christopher2a5196f2008-09-24 04:55:49 +00001754 GEN_ERROR("Vector initializer to non-vector type '" +
Chris Lattner32980692007-02-19 07:44:24 +00001755 STy->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001756
1757 $$ = ConstantStruct::get(STy, *$4);
1758 delete $1; delete $4;
1759 CHECK_FOR_ERROR
1760 }
1761 | Types '<' '{' '}' '>' {
1762 if (!UpRefs.empty())
1763 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1764 const StructType *STy = dyn_cast<StructType>($1->get());
1765 if (STy == 0)
Eric Christopher2a5196f2008-09-24 04:55:49 +00001766 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001767 (*$1)->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001768
1769 if (STy->getNumContainedTypes() != 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00001770 GEN_ERROR("Illegal number of initializers for structure type");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001771
1772 // Check to ensure that Type is packed
1773 if (!STy->isPacked())
Eric Christopher2a5196f2008-09-24 04:55:49 +00001774 GEN_ERROR("Vector initializer to non-vector type '" +
Chris Lattner32980692007-02-19 07:44:24 +00001775 STy->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001776
Reid Spencera132e042006-12-03 05:46:11 +00001777 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1778 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001779 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001780 }
1781 | Types NULL_TOK {
Reid Spencer14310612006-12-31 05:40:51 +00001782 if (!UpRefs.empty())
1783 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001784 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001785 if (PTy == 0)
Eric Christopher2a5196f2008-09-24 04:55:49 +00001786 GEN_ERROR("Cannot make null pointer constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001787 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001788
Reid Spencera132e042006-12-03 05:46:11 +00001789 $$ = ConstantPointerNull::get(PTy);
1790 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001791 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001792 }
1793 | Types UNDEF {
Reid Spencer14310612006-12-31 05:40:51 +00001794 if (!UpRefs.empty())
1795 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001796 $$ = UndefValue::get($1->get());
1797 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001798 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001799 }
1800 | Types SymbolicValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00001801 if (!UpRefs.empty())
1802 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001803 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001804 if (Ty == 0)
Devang Patel5a970972008-02-19 22:27:01 +00001805 GEN_ERROR("Global const reference must be a pointer type " + (*$1)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00001806
1807 // ConstExprs can exist in the body of a function, thus creating
1808 // GlobalValues whenever they refer to a variable. Because we are in
Reid Spencer93c40032007-03-19 18:40:50 +00001809 // the context of a function, getExistingVal will search the functions
Chris Lattner58af2a12006-02-15 07:22:58 +00001810 // symbol table instead of the module symbol table for the global symbol,
1811 // which throws things all off. To get around this, we just tell
Reid Spencer93c40032007-03-19 18:40:50 +00001812 // getExistingVal that we are at global scope here.
Chris Lattner58af2a12006-02-15 07:22:58 +00001813 //
1814 Function *SavedCurFn = CurFun.CurrentFunction;
1815 CurFun.CurrentFunction = 0;
1816
Reid Spencer93c40032007-03-19 18:40:50 +00001817 Value *V = getExistingVal(Ty, $2);
Reid Spencer5b7e7532006-09-28 19:28:24 +00001818 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001819
1820 CurFun.CurrentFunction = SavedCurFn;
1821
1822 // If this is an initializer for a constant pointer, which is referencing a
1823 // (currently) undefined variable, create a stub now that shall be replaced
1824 // in the future with the right type of variable.
1825 //
1826 if (V == 0) {
Reid Spencera9720f52007-02-05 17:04:00 +00001827 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001828 const PointerType *PT = cast<PointerType>(Ty);
1829
1830 // First check to see if the forward references value is already created!
1831 PerModuleInfo::GlobalRefsType::iterator I =
1832 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Eric Christopher2a5196f2008-09-24 04:55:49 +00001833
Chris Lattner58af2a12006-02-15 07:22:58 +00001834 if (I != CurModule.GlobalRefs.end()) {
1835 V = I->second; // Placeholder already exists, use it...
1836 $2.destroy();
1837 } else {
1838 std::string Name;
Reid Spencer41dff5e2007-01-26 08:05:27 +00001839 if ($2.Type == ValID::GlobalName)
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001840 Name = $2.getName();
Reid Spencer41dff5e2007-01-26 08:05:27 +00001841 else if ($2.Type != ValID::GlobalID)
1842 GEN_ERROR("Invalid reference to global");
Chris Lattner58af2a12006-02-15 07:22:58 +00001843
1844 // Create the forward referenced global.
1845 GlobalValue *GV;
Eric Christopher2a5196f2008-09-24 04:55:49 +00001846 if (const FunctionType *FTy =
Chris Lattner58af2a12006-02-15 07:22:58 +00001847 dyn_cast<FunctionType>(PT->getElementType())) {
Gabor Greife64d2482008-04-06 23:07:54 +00001848 GV = Function::Create(FTy, GlobalValue::ExternalWeakLinkage, Name,
1849 CurModule.CurrentModule);
Chris Lattner58af2a12006-02-15 07:22:58 +00001850 } else {
1851 GV = new GlobalVariable(PT->getElementType(), false,
Chris Lattner6cdc6822007-04-26 05:31:05 +00001852 GlobalValue::ExternalWeakLinkage, 0,
Chris Lattner58af2a12006-02-15 07:22:58 +00001853 Name, CurModule.CurrentModule);
1854 }
1855
1856 // Keep track of the fact that we have a forward ref to recycle it
1857 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1858 V = GV;
1859 }
1860 }
1861
Reid Spencera132e042006-12-03 05:46:11 +00001862 $$ = cast<GlobalValue>(V);
1863 delete $1; // Free the type handle
Reid Spencer61c83e02006-08-18 08:43:06 +00001864 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001865 }
1866 | Types ConstExpr {
Reid Spencer14310612006-12-31 05:40:51 +00001867 if (!UpRefs.empty())
1868 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001869 if ($1->get() != $2->getType())
Eric Christopher2a5196f2008-09-24 04:55:49 +00001870 GEN_ERROR("Mismatched types for constant expression: " +
Reid Spencere68853b2007-01-04 00:06:14 +00001871 (*$1)->getDescription() + " and " + $2->getType()->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00001872 $$ = $2;
Reid Spencera132e042006-12-03 05:46:11 +00001873 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001874 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001875 }
1876 | Types ZEROINITIALIZER {
Reid Spencer14310612006-12-31 05:40:51 +00001877 if (!UpRefs.empty())
1878 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001879 const Type *Ty = $1->get();
Chris Lattner58af2a12006-02-15 07:22:58 +00001880 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
Reid Spencerb5334b02007-02-05 10:18:06 +00001881 GEN_ERROR("Cannot create a null initialized value of this type");
Reid Spencera132e042006-12-03 05:46:11 +00001882 $$ = Constant::getNullValue(Ty);
1883 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001884 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00001885 }
Chris Lattner740e7092008-10-15 06:16:57 +00001886 | Types ESINT64VAL { // integral constants
1887 if (IntegerType *IT = dyn_cast<IntegerType>($1->get())) {
1888 if (!ConstantInt::isValueValidForType(IT, $2))
1889 GEN_ERROR("Constant value doesn't fit in type");
1890 $$ = ConstantInt::get(IT, $2, true);
1891 } else {
1892 GEN_ERROR("integer constant must have integer type");
1893 }
1894 delete $1;
Reid Spencer38c91a92007-02-28 02:24:54 +00001895 CHECK_FOR_ERROR
1896 }
Chris Lattner740e7092008-10-15 06:16:57 +00001897 | Types ESAPINTVAL { // arbitrary precision integer constants
1898 if (IntegerType *IT = dyn_cast<IntegerType>($1->get())) {
1899 if ($2->getBitWidth() > IT->getBitWidth())
1900 GEN_ERROR("Constant value does not fit in type");
1901 $2->sextOrTrunc(IT->getBitWidth());
1902 $$ = ConstantInt::get(*$2);
1903 } else {
1904 GEN_ERROR("integer constant must have integer type");
Reid Spencer10794272007-03-01 19:41:47 +00001905 }
Chris Lattner740e7092008-10-15 06:16:57 +00001906 delete $1;
Reid Spencer38c91a92007-02-28 02:24:54 +00001907 delete $2;
Reid Spencere4d87aa2006-12-23 06:05:41 +00001908 CHECK_FOR_ERROR
1909 }
Chris Lattner740e7092008-10-15 06:16:57 +00001910 | Types EUINT64VAL { // integral constants
1911 if (IntegerType *IT = dyn_cast<IntegerType>($1->get())) {
1912 if (!ConstantInt::isValueValidForType(IT, $2))
1913 GEN_ERROR("Constant value doesn't fit in type");
1914 $$ = ConstantInt::get(IT, $2, false);
1915 } else {
1916 GEN_ERROR("integer constant must have integer type");
Eric Christopher2a5196f2008-09-24 04:55:49 +00001917 }
Chris Lattner740e7092008-10-15 06:16:57 +00001918 delete $1;
Reid Spencere4d87aa2006-12-23 06:05:41 +00001919 CHECK_FOR_ERROR
1920 }
Chris Lattner740e7092008-10-15 06:16:57 +00001921 | Types EUAPINTVAL { // arbitrary precision integer constants
1922 if (IntegerType *IT = dyn_cast<IntegerType>($1->get())) {
1923 if ($2->getBitWidth() > IT->getBitWidth())
1924 GEN_ERROR("Constant value does not fit in type");
1925 $2->zextOrTrunc(IT->getBitWidth());
1926 $$ = ConstantInt::get(*$2);
1927 } else {
1928 GEN_ERROR("integer constant must have integer type");
1929 }
1930
1931 delete $2;
1932 delete $1;
1933 CHECK_FOR_ERROR
1934 }
1935 | Types TRUETOK { // Boolean constants
1936 if ($1->get() != Type::Int1Ty)
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001937 GEN_ERROR("Constant true must have type i1");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001938 $$ = ConstantInt::getTrue();
Chris Lattner740e7092008-10-15 06:16:57 +00001939 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001940 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001941 }
Chris Lattner740e7092008-10-15 06:16:57 +00001942 | Types FALSETOK { // Boolean constants
1943 if ($1->get() != Type::Int1Ty)
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001944 GEN_ERROR("Constant false must have type i1");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001945 $$ = ConstantInt::getFalse();
Chris Lattner740e7092008-10-15 06:16:57 +00001946 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001947 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001948 }
Chris Lattner740e7092008-10-15 06:16:57 +00001949 | Types FPVAL { // Floating point constants
1950 if (!ConstantFP::isValueValidForType($1->get(), *$2))
Reid Spencerb5334b02007-02-05 10:18:06 +00001951 GEN_ERROR("Floating point constant invalid for type");
Chris Lattner740e7092008-10-15 06:16:57 +00001952
Eric Christopher2a5196f2008-09-24 04:55:49 +00001953 // Lexer has no type info, so builds all float and double FP constants
Dale Johannesenc72cd7e2007-09-11 18:33:39 +00001954 // as double. Fix this here. Long double is done right.
Chris Lattner740e7092008-10-15 06:16:57 +00001955 if (&$2->getSemantics()==&APFloat::IEEEdouble && $1->get()==Type::FloatTy) {
Dale Johannesen236bbd42008-10-09 23:01:34 +00001956 bool ignored;
1957 $2->convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven,
1958 &ignored);
1959 }
Chris Lattnerd8eb63f2008-04-20 00:41:19 +00001960 $$ = ConstantFP::get(*$2);
Chris Lattner740e7092008-10-15 06:16:57 +00001961 delete $1;
Dale Johannesencdd509a2007-09-07 21:07:57 +00001962 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001963 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001964 };
1965
1966
Reid Spencer3da59db2006-11-27 01:05:10 +00001967ConstExpr: CastOps '(' ConstVal TO Types ')' {
Reid Spencer14310612006-12-31 05:40:51 +00001968 if (!UpRefs.empty())
1969 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001970 Constant *Val = $3;
Reid Spencerb0fcf8f2007-01-17 02:48:45 +00001971 const Type *DestTy = $5->get();
1972 if (!CastInst::castIsValid($1, $3, DestTy))
1973 GEN_ERROR("invalid cast opcode for cast from '" +
1974 Val->getType()->getDescription() + "' to '" +
Eric Christopher2a5196f2008-09-24 04:55:49 +00001975 DestTy->getDescription() + "'");
Reid Spencerb0fcf8f2007-01-17 02:48:45 +00001976 $$ = ConstantExpr::getCast($1, $3, DestTy);
Reid Spencera132e042006-12-03 05:46:11 +00001977 delete $5;
Chris Lattner58af2a12006-02-15 07:22:58 +00001978 }
1979 | GETELEMENTPTR '(' ConstVal IndexList ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001980 if (!isa<PointerType>($3->getType()))
Reid Spencerb5334b02007-02-05 10:18:06 +00001981 GEN_ERROR("GetElementPtr requires a pointer operand");
Chris Lattner58af2a12006-02-15 07:22:58 +00001982
Reid Spencera132e042006-12-03 05:46:11 +00001983 const Type *IdxTy =
Dan Gohman041e2eb2008-05-15 19:50:34 +00001984 GetElementPtrInst::getIndexedType($3->getType(), $4->begin(), $4->end());
Reid Spencera132e042006-12-03 05:46:11 +00001985 if (!IdxTy)
Reid Spencerb5334b02007-02-05 10:18:06 +00001986 GEN_ERROR("Index list invalid for constant getelementptr");
Reid Spencera132e042006-12-03 05:46:11 +00001987
Chris Lattnerf7469af2007-01-31 04:44:08 +00001988 SmallVector<Constant*, 8> IdxVec;
Reid Spencera132e042006-12-03 05:46:11 +00001989 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1990 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattner58af2a12006-02-15 07:22:58 +00001991 IdxVec.push_back(C);
1992 else
Reid Spencerb5334b02007-02-05 10:18:06 +00001993 GEN_ERROR("Indices to constant getelementptr must be constants");
Chris Lattner58af2a12006-02-15 07:22:58 +00001994
1995 delete $4;
1996
Chris Lattnerf7469af2007-01-31 04:44:08 +00001997 $$ = ConstantExpr::getGetElementPtr($3, &IdxVec[0], IdxVec.size());
Reid Spencer61c83e02006-08-18 08:43:06 +00001998 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001999 }
2000 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer4fe16d62007-01-11 18:21:29 +00002001 if ($3->getType() != Type::Int1Ty)
Reid Spencerb5334b02007-02-05 10:18:06 +00002002 GEN_ERROR("Select condition must be of boolean type");
Reid Spencera132e042006-12-03 05:46:11 +00002003 if ($5->getType() != $7->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00002004 GEN_ERROR("Select operand types must match");
Reid Spencera132e042006-12-03 05:46:11 +00002005 $$ = ConstantExpr::getSelect($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00002006 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002007 }
2008 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00002009 if ($3->getType() != $5->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00002010 GEN_ERROR("Binary operator types must match");
Reid Spencer1628cec2006-10-26 06:15:43 +00002011 CHECK_FOR_ERROR;
Reid Spencer9eef56f2006-12-05 19:16:11 +00002012 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattner58af2a12006-02-15 07:22:58 +00002013 }
2014 | LogicalOps '(' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00002015 if ($3->getType() != $5->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00002016 GEN_ERROR("Logical operator types must match");
Chris Lattner42a75512007-01-15 02:27:26 +00002017 if (!$3->getType()->isInteger()) {
Eric Christopher2a5196f2008-09-24 04:55:49 +00002018 if (!isa<VectorType>($3->getType()) ||
Reid Spencer9d6565a2007-02-15 02:26:10 +00002019 !cast<VectorType>($3->getType())->getElementType()->isInteger())
Reid Spencerb5334b02007-02-05 10:18:06 +00002020 GEN_ERROR("Logical operator requires integral operands");
Chris Lattner58af2a12006-02-15 07:22:58 +00002021 }
Reid Spencera132e042006-12-03 05:46:11 +00002022 $$ = ConstantExpr::get($1, $3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002023 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002024 }
Reid Spencer4012e832006-12-04 05:24:24 +00002025 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
2026 if ($4->getType() != $6->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00002027 GEN_ERROR("icmp operand types must match");
Reid Spencer4012e832006-12-04 05:24:24 +00002028 $$ = ConstantExpr::getICmp($2, $4, $6);
Reid Spencera132e042006-12-03 05:46:11 +00002029 }
Reid Spencer4012e832006-12-04 05:24:24 +00002030 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
2031 if ($4->getType() != $6->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00002032 GEN_ERROR("fcmp operand types must match");
Reid Spencer4012e832006-12-04 05:24:24 +00002033 $$ = ConstantExpr::getFCmp($2, $4, $6);
Reid Spencera132e042006-12-03 05:46:11 +00002034 }
Nate Begemanac80ade2008-05-12 19:01:56 +00002035 | VICMP IPredicates '(' ConstVal ',' ConstVal ')' {
2036 if ($4->getType() != $6->getType())
2037 GEN_ERROR("vicmp operand types must match");
2038 $$ = ConstantExpr::getVICmp($2, $4, $6);
2039 }
2040 | VFCMP FPredicates '(' ConstVal ',' ConstVal ')' {
2041 if ($4->getType() != $6->getType())
2042 GEN_ERROR("vfcmp operand types must match");
2043 $$ = ConstantExpr::getVFCmp($2, $4, $6);
2044 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002045 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00002046 if (!ExtractElementInst::isValidOperands($3, $5))
Reid Spencerb5334b02007-02-05 10:18:06 +00002047 GEN_ERROR("Invalid extractelement operands");
Reid Spencera132e042006-12-03 05:46:11 +00002048 $$ = ConstantExpr::getExtractElement($3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002049 CHECK_FOR_ERROR
Chris Lattnerd25db202006-04-08 03:55:17 +00002050 }
2051 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00002052 if (!InsertElementInst::isValidOperands($3, $5, $7))
Reid Spencerb5334b02007-02-05 10:18:06 +00002053 GEN_ERROR("Invalid insertelement operands");
Reid Spencera132e042006-12-03 05:46:11 +00002054 $$ = ConstantExpr::getInsertElement($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00002055 CHECK_FOR_ERROR
Chris Lattnerd25db202006-04-08 03:55:17 +00002056 }
2057 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00002058 if (!ShuffleVectorInst::isValidOperands($3, $5, $7))
Reid Spencerb5334b02007-02-05 10:18:06 +00002059 GEN_ERROR("Invalid shufflevector operands");
Reid Spencera132e042006-12-03 05:46:11 +00002060 $$ = ConstantExpr::getShuffleVector($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00002061 CHECK_FOR_ERROR
Dan Gohmane4977cf2008-05-23 01:55:30 +00002062 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002063 | EXTRACTVALUE '(' ConstVal ConstantIndexList ')' {
Dan Gohmane4977cf2008-05-23 01:55:30 +00002064 if (!isa<StructType>($3->getType()) && !isa<ArrayType>($3->getType()))
2065 GEN_ERROR("ExtractValue requires an aggregate operand");
2066
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002067 $$ = ConstantExpr::getExtractValue($3, &(*$4)[0], $4->size());
Dan Gohmane4977cf2008-05-23 01:55:30 +00002068 delete $4;
Dan Gohmane4977cf2008-05-23 01:55:30 +00002069 CHECK_FOR_ERROR
2070 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002071 | INSERTVALUE '(' ConstVal ',' ConstVal ConstantIndexList ')' {
Dan Gohmane4977cf2008-05-23 01:55:30 +00002072 if (!isa<StructType>($3->getType()) && !isa<ArrayType>($3->getType()))
2073 GEN_ERROR("InsertValue requires an aggregate operand");
2074
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002075 $$ = ConstantExpr::getInsertValue($3, $5, &(*$6)[0], $6->size());
Dan Gohmane4977cf2008-05-23 01:55:30 +00002076 delete $6;
Dan Gohmane4977cf2008-05-23 01:55:30 +00002077 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002078 };
2079
Chris Lattnerd25db202006-04-08 03:55:17 +00002080
Chris Lattner58af2a12006-02-15 07:22:58 +00002081// ConstVector - A list of comma separated constants.
2082ConstVector : ConstVector ',' ConstVal {
2083 ($$ = $1)->push_back($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002084 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002085 }
2086 | ConstVal {
Reid Spencera132e042006-12-03 05:46:11 +00002087 $$ = new std::vector<Constant*>();
Chris Lattner58af2a12006-02-15 07:22:58 +00002088 $$->push_back($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002089 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002090 };
2091
2092
2093// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
2094GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
2095
Eric Christopher2a5196f2008-09-24 04:55:49 +00002096// ThreadLocal
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002097ThreadLocal : THREAD_LOCAL { $$ = true; } | { $$ = false; };
2098
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002099// AliaseeRef - Match either GlobalValue or bitcast to GlobalValue.
2100AliaseeRef : ResultTypes SymbolicValueRef {
2101 const Type* VTy = $1->get();
2102 Value *V = getVal(VTy, $2);
Chris Lattner0275cff2007-08-06 21:00:46 +00002103 CHECK_FOR_ERROR
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002104 GlobalValue* Aliasee = dyn_cast<GlobalValue>(V);
2105 if (!Aliasee)
2106 GEN_ERROR("Aliases can be created only to global values");
2107
2108 $$ = Aliasee;
2109 CHECK_FOR_ERROR
2110 delete $1;
2111 }
2112 | BITCAST '(' AliaseeRef TO Types ')' {
2113 Constant *Val = $3;
2114 const Type *DestTy = $5->get();
2115 if (!CastInst::castIsValid($1, $3, DestTy))
2116 GEN_ERROR("invalid cast opcode for cast from '" +
2117 Val->getType()->getDescription() + "' to '" +
2118 DestTy->getDescription() + "'");
Eric Christopher2a5196f2008-09-24 04:55:49 +00002119
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002120 $$ = ConstantExpr::getCast($1, $3, DestTy);
2121 CHECK_FOR_ERROR
2122 delete $5;
2123 };
Chris Lattner58af2a12006-02-15 07:22:58 +00002124
2125//===----------------------------------------------------------------------===//
2126// Rules to match Modules
2127//===----------------------------------------------------------------------===//
2128
2129// Module rule: Capture the result of parsing the whole file into a result
2130// variable...
2131//
Eric Christopher2a5196f2008-09-24 04:55:49 +00002132Module
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002133 : DefinitionList {
2134 $$ = ParserResult = CurModule.CurrentModule;
2135 CurModule.ModuleDone();
2136 CHECK_FOR_ERROR;
2137 }
2138 | /*empty*/ {
2139 $$ = ParserResult = CurModule.CurrentModule;
2140 CurModule.ModuleDone();
2141 CHECK_FOR_ERROR;
2142 }
2143 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00002144
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002145DefinitionList
2146 : Definition
2147 | DefinitionList Definition
2148 ;
2149
Eric Christopher2a5196f2008-09-24 04:55:49 +00002150Definition
Jeff Cohen361c3ef2007-01-21 19:19:31 +00002151 : DEFINE { CurFun.isDeclare = false; } Function {
Chris Lattner58af2a12006-02-15 07:22:58 +00002152 CurFun.FunctionDone();
Reid Spencer61c83e02006-08-18 08:43:06 +00002153 CHECK_FOR_ERROR
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002154 }
2155 | DECLARE { CurFun.isDeclare = true; } FunctionProto {
Reid Spencer61c83e02006-08-18 08:43:06 +00002156 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002157 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002158 | MODULE ASM_TOK AsmBlock {
Reid Spencer61c83e02006-08-18 08:43:06 +00002159 CHECK_FOR_ERROR
Eric Christopher2a5196f2008-09-24 04:55:49 +00002160 }
Reid Spencer41dff5e2007-01-26 08:05:27 +00002161 | OptLocalAssign TYPE Types {
Reid Spencer14310612006-12-31 05:40:51 +00002162 if (!UpRefs.empty())
2163 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00002164 // Eagerly resolve types. This is not an optimization, this is a
2165 // requirement that is due to the fact that we could have this:
2166 //
2167 // %list = type { %list * }
2168 // %list = type { %list * } ; repeated type decl
2169 //
2170 // If types are not resolved eagerly, then the two types will not be
2171 // determined to be the same type!
2172 //
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002173 ResolveTypeTo($1, *$3);
Chris Lattner58af2a12006-02-15 07:22:58 +00002174
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002175 if (!setTypeName(*$3, $1) && !$1) {
Reid Spencer5b7e7532006-09-28 19:28:24 +00002176 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002177 // If this is a named type that is not a redefinition, add it to the slot
2178 // table.
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002179 CurModule.Types.push_back(*$3);
Chris Lattner58af2a12006-02-15 07:22:58 +00002180 }
Reid Spencera132e042006-12-03 05:46:11 +00002181
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002182 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002183 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002184 }
Reid Spencer41dff5e2007-01-26 08:05:27 +00002185 | OptLocalAssign TYPE VOID {
Reid Spencer14310612006-12-31 05:40:51 +00002186 ResolveTypeTo($1, $3);
2187
2188 if (!setTypeName($3, $1) && !$1) {
2189 CHECK_FOR_ERROR
2190 // If this is a named type that is not a redefinition, add it to the slot
2191 // table.
2192 CurModule.Types.push_back($3);
2193 }
2194 CHECK_FOR_ERROR
2195 }
Eric Christopher2a5196f2008-09-24 04:55:49 +00002196 | OptGlobalAssign GVVisibilityStyle ThreadLocal GlobalType ConstVal
2197 OptAddrSpace {
Reid Spencer41dff5e2007-01-26 08:05:27 +00002198 /* "Externally Visible" Linkage */
Eric Christopher2a5196f2008-09-24 04:55:49 +00002199 if ($5 == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002200 GEN_ERROR("Global value initializer is not a constant");
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002201 CurGV = ParseGlobalVariable($1, GlobalValue::ExternalLinkage,
Christopher Lambbf3348d2007-12-12 08:45:45 +00002202 $2, $4, $5->getType(), $5, $3, $6);
Christopher Lamba8ed9bf2007-12-11 09:02:08 +00002203 CHECK_FOR_ERROR
2204 } GlobalVarAttributes {
2205 CurGV = 0;
2206 }
Chris Lattner6cdc6822007-04-26 05:31:05 +00002207 | OptGlobalAssign GVInternalLinkage GVVisibilityStyle ThreadLocal GlobalType
Christopher Lambbf3348d2007-12-12 08:45:45 +00002208 ConstVal OptAddrSpace {
Eric Christopher2a5196f2008-09-24 04:55:49 +00002209 if ($6 == 0)
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002210 GEN_ERROR("Global value initializer is not a constant");
Christopher Lambbf3348d2007-12-12 08:45:45 +00002211 CurGV = ParseGlobalVariable($1, $2, $3, $5, $6->getType(), $6, $4, $7);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002212 CHECK_FOR_ERROR
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002213 } GlobalVarAttributes {
2214 CurGV = 0;
2215 }
Chris Lattner6cdc6822007-04-26 05:31:05 +00002216 | OptGlobalAssign GVExternalLinkage GVVisibilityStyle ThreadLocal GlobalType
Christopher Lambbf3348d2007-12-12 08:45:45 +00002217 Types OptAddrSpace {
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002218 if (!UpRefs.empty())
2219 GEN_ERROR("Invalid upreference in type: " + (*$6)->getDescription());
Christopher Lambbf3348d2007-12-12 08:45:45 +00002220 CurGV = ParseGlobalVariable($1, $2, $3, $5, *$6, 0, $4, $7);
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002221 CHECK_FOR_ERROR
2222 delete $6;
Reid Spencer5b7e7532006-09-28 19:28:24 +00002223 } GlobalVarAttributes {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002224 CurGV = 0;
2225 CHECK_FOR_ERROR
2226 }
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002227 | OptGlobalAssign GVVisibilityStyle ALIAS AliasLinkage AliaseeRef {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002228 std::string Name;
2229 if ($1) {
2230 Name = *$1;
2231 delete $1;
2232 }
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00002233 if (Name.empty())
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002234 GEN_ERROR("Alias name cannot be empty");
Eric Christopher2a5196f2008-09-24 04:55:49 +00002235
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002236 Constant* Aliasee = $5;
2237 if (Aliasee == 0)
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002238 GEN_ERROR(std::string("Invalid aliasee for alias: ") + Name);
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002239
2240 GlobalAlias* GA = new GlobalAlias(Aliasee->getType(), $4, Name, Aliasee,
2241 CurModule.CurrentModule);
2242 GA->setVisibility($2);
2243 InsertValue(GA, CurModule.Values);
Eric Christopher2a5196f2008-09-24 04:55:49 +00002244
2245
Chris Lattner569f7372007-09-10 23:24:14 +00002246 // If there was a forward reference of this alias, resolve it now.
Eric Christopher2a5196f2008-09-24 04:55:49 +00002247
Chris Lattner569f7372007-09-10 23:24:14 +00002248 ValID ID;
2249 if (!Name.empty())
2250 ID = ValID::createGlobalName(Name);
2251 else
2252 ID = ValID::createGlobalID(CurModule.Values.size()-1);
Eric Christopher2a5196f2008-09-24 04:55:49 +00002253
Chris Lattner569f7372007-09-10 23:24:14 +00002254 if (GlobalValue *FWGV =
2255 CurModule.GetForwardRefForGlobal(GA->getType(), ID)) {
2256 // Replace uses of the fwdref with the actual alias.
2257 FWGV->replaceAllUsesWith(GA);
2258 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(FWGV))
2259 GV->eraseFromParent();
2260 else
2261 cast<Function>(FWGV)->eraseFromParent();
2262 }
2263 ID.destroy();
Eric Christopher2a5196f2008-09-24 04:55:49 +00002264
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00002265 CHECK_FOR_ERROR
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00002266 }
Eric Christopher2a5196f2008-09-24 04:55:49 +00002267 | TARGET TargetDefinition {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002268 CHECK_FOR_ERROR
2269 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002270 | DEPLIBS '=' LibrariesDefinition {
Reid Spencer61c83e02006-08-18 08:43:06 +00002271 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002272 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002273 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00002274
2275
2276AsmBlock : STRINGCONSTANT {
2277 const std::string &AsmSoFar = CurModule.CurrentModule->getModuleInlineAsm();
Chris Lattner58af2a12006-02-15 07:22:58 +00002278 if (AsmSoFar.empty())
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002279 CurModule.CurrentModule->setModuleInlineAsm(*$1);
Chris Lattner58af2a12006-02-15 07:22:58 +00002280 else
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002281 CurModule.CurrentModule->setModuleInlineAsm(AsmSoFar+"\n"+*$1);
2282 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002283 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002284};
2285
Reid Spencer41dff5e2007-01-26 08:05:27 +00002286TargetDefinition : TRIPLE '=' STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002287 CurModule.CurrentModule->setTargetTriple(*$3);
2288 delete $3;
John Criswell2f6a8b12006-10-24 19:09:48 +00002289 }
Chris Lattner1ae022f2006-10-22 06:08:13 +00002290 | DATALAYOUT '=' STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002291 CurModule.CurrentModule->setDataLayout(*$3);
2292 delete $3;
Owen Anderson1dc69692006-10-18 02:21:48 +00002293 };
Chris Lattner58af2a12006-02-15 07:22:58 +00002294
2295LibrariesDefinition : '[' LibList ']';
2296
2297LibList : LibList ',' STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002298 CurModule.CurrentModule->addLibrary(*$3);
2299 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002300 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002301 }
2302 | STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002303 CurModule.CurrentModule->addLibrary(*$1);
2304 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002305 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002306 }
2307 | /* empty: end of list */ {
Reid Spencer61c83e02006-08-18 08:43:06 +00002308 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002309 }
2310 ;
2311
2312//===----------------------------------------------------------------------===//
2313// Rules to match Function Headers
2314//===----------------------------------------------------------------------===//
2315
Devang Patel05988662008-09-25 21:00:45 +00002316ArgListH : ArgListH ',' Types OptAttributes OptLocalName {
Reid Spencer14310612006-12-31 05:40:51 +00002317 if (!UpRefs.empty())
2318 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002319 if (!(*$3)->isFirstClassType())
2320 GEN_ERROR("Argument types must be first-class");
Reid Spencer14310612006-12-31 05:40:51 +00002321 ArgListEntry E; E.Attrs = $4; E.Ty = $3; E.Name = $5;
Chris Lattner58af2a12006-02-15 07:22:58 +00002322 $$ = $1;
Reid Spencer14310612006-12-31 05:40:51 +00002323 $1->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002324 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002325 }
Devang Patel05988662008-09-25 21:00:45 +00002326 | Types OptAttributes OptLocalName {
Reid Spencer14310612006-12-31 05:40:51 +00002327 if (!UpRefs.empty())
2328 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002329 if (!(*$1)->isFirstClassType())
2330 GEN_ERROR("Argument types must be first-class");
Reid Spencer14310612006-12-31 05:40:51 +00002331 ArgListEntry E; E.Attrs = $2; E.Ty = $1; E.Name = $3;
2332 $$ = new ArgListType;
2333 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002334 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002335 };
2336
2337ArgList : ArgListH {
2338 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002339 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002340 }
2341 | ArgListH ',' DOTDOTDOT {
2342 $$ = $1;
Reid Spencer14310612006-12-31 05:40:51 +00002343 struct ArgListEntry E;
2344 E.Ty = new PATypeHolder(Type::VoidTy);
2345 E.Name = 0;
Devang Patel05988662008-09-25 21:00:45 +00002346 E.Attrs = Attribute::None;
Reid Spencer14310612006-12-31 05:40:51 +00002347 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002348 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002349 }
2350 | DOTDOTDOT {
Reid Spencer14310612006-12-31 05:40:51 +00002351 $$ = new ArgListType;
2352 struct ArgListEntry E;
2353 E.Ty = new PATypeHolder(Type::VoidTy);
2354 E.Name = 0;
Devang Patel05988662008-09-25 21:00:45 +00002355 E.Attrs = Attribute::None;
Reid Spencer14310612006-12-31 05:40:51 +00002356 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002357 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002358 }
2359 | /* empty */ {
2360 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00002361 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002362 };
2363
Devang Patel652203f2008-09-29 20:49:50 +00002364FunctionHeaderH : OptCallingConv OptRetAttrs ResultTypes GlobalName '(' ArgList ')'
Devang Patel2c9c3e72008-09-26 23:51:19 +00002365 OptFuncAttrs OptSection OptAlign OptGC {
Devang Patel652203f2008-09-29 20:49:50 +00002366 std::string FunctionName(*$4);
2367 delete $4; // Free strdup'd memory!
Eric Christopher2a5196f2008-09-24 04:55:49 +00002368
Reid Spencer8c8a2dc2007-01-02 21:54:12 +00002369 // Check the function result for abstractness if this is a define. We should
2370 // have no abstract types at this point
Devang Patel652203f2008-09-29 20:49:50 +00002371 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved($3))
2372 GEN_ERROR("Reference to abstract result: "+ $3->get()->getDescription());
Reid Spencer8c8a2dc2007-01-02 21:54:12 +00002373
Devang Patel652203f2008-09-29 20:49:50 +00002374 if (!FunctionType::isValidReturnType(*$3))
Chris Lattnera925a142008-04-23 05:37:08 +00002375 GEN_ERROR("Invalid result type for LLVM function");
Eric Christopher2a5196f2008-09-24 04:55:49 +00002376
Chris Lattner58af2a12006-02-15 07:22:58 +00002377 std::vector<const Type*> ParamTypeList;
Devang Patel05988662008-09-25 21:00:45 +00002378 SmallVector<AttributeWithIndex, 8> Attrs;
Devang Patel19c87462008-09-26 22:53:05 +00002379 //FIXME : In 3.0, stop accepting zext, sext and inreg as optional function
2380 //attributes.
Devang Patel652203f2008-09-29 20:49:50 +00002381 Attributes RetAttrs = $2;
2382 if ($8 != Attribute::None) {
2383 if ($8 & Attribute::ZExt) {
Devang Patel19c87462008-09-26 22:53:05 +00002384 RetAttrs = RetAttrs | Attribute::ZExt;
Devang Patel652203f2008-09-29 20:49:50 +00002385 $8 = $8 ^ Attribute::ZExt;
Devang Patel19c87462008-09-26 22:53:05 +00002386 }
Devang Patel652203f2008-09-29 20:49:50 +00002387 if ($8 & Attribute::SExt) {
Devang Patel19c87462008-09-26 22:53:05 +00002388 RetAttrs = RetAttrs | Attribute::SExt;
Devang Patel652203f2008-09-29 20:49:50 +00002389 $8 = $8 ^ Attribute::SExt;
Devang Patel19c87462008-09-26 22:53:05 +00002390 }
Devang Patel652203f2008-09-29 20:49:50 +00002391 if ($8 & Attribute::InReg) {
Devang Patel19c87462008-09-26 22:53:05 +00002392 RetAttrs = RetAttrs | Attribute::InReg;
Devang Patel652203f2008-09-29 20:49:50 +00002393 $8 = $8 ^ Attribute::InReg;
Devang Patel19c87462008-09-26 22:53:05 +00002394 }
Devang Patel19c87462008-09-26 22:53:05 +00002395 }
Devang Patel652203f2008-09-29 20:49:50 +00002396 if (RetAttrs != Attribute::None)
2397 Attrs.push_back(AttributeWithIndex::get(0, RetAttrs));
2398 if ($6) { // If there are arguments...
Reid Spencer7b5d4662007-04-09 06:16:21 +00002399 unsigned index = 1;
Devang Patel652203f2008-09-29 20:49:50 +00002400 for (ArgListType::iterator I = $6->begin(); I != $6->end(); ++I, ++index) {
Reid Spencer14310612006-12-31 05:40:51 +00002401 const Type* Ty = I->Ty->get();
Reid Spencer8c8a2dc2007-01-02 21:54:12 +00002402 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved(I->Ty))
2403 GEN_ERROR("Reference to abstract argument: " + Ty->getDescription());
Reid Spencer14310612006-12-31 05:40:51 +00002404 ParamTypeList.push_back(Ty);
Devang Patel05988662008-09-25 21:00:45 +00002405 if (Ty != Type::VoidTy && I->Attrs != Attribute::None)
2406 Attrs.push_back(AttributeWithIndex::get(index, I->Attrs));
Reid Spencer14310612006-12-31 05:40:51 +00002407 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002408 }
Devang Patel652203f2008-09-29 20:49:50 +00002409 if ($8 != Attribute::None)
2410 Attrs.push_back(AttributeWithIndex::get(~0, $8));
Chris Lattner58af2a12006-02-15 07:22:58 +00002411
2412 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
2413 if (isVarArg) ParamTypeList.pop_back();
2414
Devang Patel05988662008-09-25 21:00:45 +00002415 AttrListPtr PAL;
Christopher Lamb5c104242007-04-22 20:09:11 +00002416 if (!Attrs.empty())
Devang Patel05988662008-09-25 21:00:45 +00002417 PAL = AttrListPtr::get(Attrs.begin(), Attrs.end());
Reid Spencer7b5d4662007-04-09 06:16:21 +00002418
Devang Patel652203f2008-09-29 20:49:50 +00002419 FunctionType *FT = FunctionType::get(*$3, ParamTypeList, isVarArg);
Christopher Lamb4374f8e2007-12-17 01:17:35 +00002420 const PointerType *PFT = PointerType::getUnqual(FT);
Devang Patel652203f2008-09-29 20:49:50 +00002421 delete $3;
Chris Lattner58af2a12006-02-15 07:22:58 +00002422
2423 ValID ID;
2424 if (!FunctionName.empty()) {
Reid Spencer41dff5e2007-01-26 08:05:27 +00002425 ID = ValID::createGlobalName((char*)FunctionName.c_str());
Chris Lattner58af2a12006-02-15 07:22:58 +00002426 } else {
Reid Spencer93c40032007-03-19 18:40:50 +00002427 ID = ValID::createGlobalID(CurModule.Values.size());
Chris Lattner58af2a12006-02-15 07:22:58 +00002428 }
2429
2430 Function *Fn = 0;
2431 // See if this function was forward referenced. If so, recycle the object.
2432 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
Eric Christopher2a5196f2008-09-24 04:55:49 +00002433 // Move the function to the end of the list, from whereever it was
Chris Lattner58af2a12006-02-15 07:22:58 +00002434 // previously inserted.
2435 Fn = cast<Function>(FWRef);
Devang Patel05988662008-09-25 21:00:45 +00002436 assert(Fn->getAttributes().isEmpty() &&
Chris Lattner58d74912008-03-12 17:45:29 +00002437 "Forward reference has parameter attributes!");
Chris Lattner58af2a12006-02-15 07:22:58 +00002438 CurModule.CurrentModule->getFunctionList().remove(Fn);
2439 CurModule.CurrentModule->getFunctionList().push_back(Fn);
2440 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
Reid Spenceref9b9a72007-02-05 20:47:22 +00002441 (Fn = CurModule.CurrentModule->getFunction(FunctionName))) {
Duncan Sandsdc024672007-11-27 13:23:08 +00002442 if (Fn->getFunctionType() != FT ) {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002443 // The existing function doesn't have the same type. This is an overload
2444 // error.
2445 GEN_ERROR("Overload of function '" + FunctionName + "' not permitted.");
Devang Patel05988662008-09-25 21:00:45 +00002446 } else if (Fn->getAttributes() != PAL) {
Duncan Sandsdc024672007-11-27 13:23:08 +00002447 // The existing function doesn't have the same parameter attributes.
2448 // This is an overload error.
2449 GEN_ERROR("Overload of function '" + FunctionName + "' not permitted.");
Reid Spenceref9b9a72007-02-05 20:47:22 +00002450 } else if (!CurFun.isDeclare && !Fn->isDeclaration()) {
Chris Lattner6cdc6822007-04-26 05:31:05 +00002451 // Neither the existing or the current function is a declaration and they
2452 // have the same name and same type. Clearly this is a redefinition.
2453 GEN_ERROR("Redefinition of function '" + FunctionName + "'");
Duncan Sandsdc024672007-11-27 13:23:08 +00002454 } else if (Fn->isDeclaration()) {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002455 // Make sure to strip off any argument names so we can't get conflicts.
Chris Lattner58af2a12006-02-15 07:22:58 +00002456 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
2457 AI != AE; ++AI)
2458 AI->setName("");
Reid Spenceref9b9a72007-02-05 20:47:22 +00002459 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002460 } else { // Not already defined?
Gabor Greife64d2482008-04-06 23:07:54 +00002461 Fn = Function::Create(FT, GlobalValue::ExternalWeakLinkage, FunctionName,
2462 CurModule.CurrentModule);
Chris Lattner58af2a12006-02-15 07:22:58 +00002463 InsertValue(Fn, CurModule.Values);
2464 }
2465
Nuno Lopes9e9631d2008-10-03 15:45:58 +00002466 ID.destroy();
Chris Lattner58af2a12006-02-15 07:22:58 +00002467 CurFun.FunctionStart(Fn);
Anton Korobeynikov93c2b372006-09-17 13:06:18 +00002468
2469 if (CurFun.isDeclare) {
2470 // If we have declaration, always overwrite linkage. This will allow us to
2471 // correctly handle cases, when pointer to function is passed as argument to
2472 // another function.
2473 Fn->setLinkage(CurFun.Linkage);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002474 Fn->setVisibility(CurFun.Visibility);
Anton Korobeynikov93c2b372006-09-17 13:06:18 +00002475 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002476 Fn->setCallingConv($1);
Devang Patel05988662008-09-25 21:00:45 +00002477 Fn->setAttributes(PAL);
Devang Patel652203f2008-09-29 20:49:50 +00002478 Fn->setAlignment($10);
2479 if ($9) {
2480 Fn->setSection(*$9);
2481 delete $9;
Chris Lattner58af2a12006-02-15 07:22:58 +00002482 }
Devang Patel652203f2008-09-29 20:49:50 +00002483 if ($11) {
2484 Fn->setGC($11->c_str());
2485 delete $11;
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00002486 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002487
2488 // Add all of the arguments we parsed to the function...
Devang Patel652203f2008-09-29 20:49:50 +00002489 if ($6) { // Is null if empty...
Chris Lattner58af2a12006-02-15 07:22:58 +00002490 if (isVarArg) { // Nuke the last entry
Devang Patel652203f2008-09-29 20:49:50 +00002491 assert($6->back().Ty->get() == Type::VoidTy && $6->back().Name == 0 &&
Reid Spencera9720f52007-02-05 17:04:00 +00002492 "Not a varargs marker!");
Devang Patel652203f2008-09-29 20:49:50 +00002493 delete $6->back().Ty;
2494 $6->pop_back(); // Delete the last entry
Chris Lattner58af2a12006-02-15 07:22:58 +00002495 }
2496 Function::arg_iterator ArgIt = Fn->arg_begin();
Reid Spenceref9b9a72007-02-05 20:47:22 +00002497 Function::arg_iterator ArgEnd = Fn->arg_end();
Reid Spencer14310612006-12-31 05:40:51 +00002498 unsigned Idx = 1;
Devang Patel652203f2008-09-29 20:49:50 +00002499 for (ArgListType::iterator I = $6->begin();
2500 I != $6->end() && ArgIt != ArgEnd; ++I, ++ArgIt) {
Reid Spencer14310612006-12-31 05:40:51 +00002501 delete I->Ty; // Delete the typeholder...
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002502 setValueName(ArgIt, I->Name); // Insert arg into symtab...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002503 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002504 InsertValue(ArgIt);
Reid Spencer14310612006-12-31 05:40:51 +00002505 Idx++;
Chris Lattner58af2a12006-02-15 07:22:58 +00002506 }
Reid Spencera132e042006-12-03 05:46:11 +00002507
Devang Patel652203f2008-09-29 20:49:50 +00002508 delete $6; // We're now done with the argument list
Chris Lattner58af2a12006-02-15 07:22:58 +00002509 }
Reid Spencer61c83e02006-08-18 08:43:06 +00002510 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002511};
2512
2513BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
2514
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002515FunctionHeader : FunctionDefineLinkage GVVisibilityStyle FunctionHeaderH BEGIN {
Chris Lattner58af2a12006-02-15 07:22:58 +00002516 $$ = CurFun.CurrentFunction;
2517
2518 // Make sure that we keep track of the linkage type even if there was a
2519 // previous "declare".
2520 $$->setLinkage($1);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002521 $$->setVisibility($2);
Chris Lattner58af2a12006-02-15 07:22:58 +00002522};
2523
2524END : ENDTOK | '}'; // Allow end of '}' to end a function
2525
2526Function : BasicBlockList END {
2527 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002528 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002529};
2530
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002531FunctionProto : FunctionDeclareLinkage GVVisibilityStyle FunctionHeaderH {
Reid Spencer14310612006-12-31 05:40:51 +00002532 CurFun.CurrentFunction->setLinkage($1);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002533 CurFun.CurrentFunction->setVisibility($2);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002534 $$ = CurFun.CurrentFunction;
2535 CurFun.FunctionDone();
2536 CHECK_FOR_ERROR
2537 };
Chris Lattner58af2a12006-02-15 07:22:58 +00002538
2539//===----------------------------------------------------------------------===//
2540// Rules to match Basic Blocks
2541//===----------------------------------------------------------------------===//
2542
2543OptSideEffect : /* empty */ {
2544 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002545 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002546 }
2547 | SIDEEFFECT {
2548 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002549 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002550 };
2551
2552ConstValueRef : ESINT64VAL { // A reference to a direct constant
2553 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002554 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002555 }
2556 | EUINT64VAL {
2557 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002558 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002559 }
Chris Lattner1913b942008-07-11 00:30:39 +00002560 | ESAPINTVAL { // arbitrary precision integer constants
2561 $$ = ValID::create(*$1, true);
2562 delete $1;
2563 CHECK_FOR_ERROR
Eric Christopher2a5196f2008-09-24 04:55:49 +00002564 }
Chris Lattner1913b942008-07-11 00:30:39 +00002565 | EUAPINTVAL { // arbitrary precision integer constants
2566 $$ = ValID::create(*$1, false);
2567 delete $1;
2568 CHECK_FOR_ERROR
2569 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002570 | FPVAL { // Perhaps it's an FP constant?
2571 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002572 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002573 }
2574 | TRUETOK {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00002575 $$ = ValID::create(ConstantInt::getTrue());
Reid Spencer61c83e02006-08-18 08:43:06 +00002576 CHECK_FOR_ERROR
Eric Christopher2a5196f2008-09-24 04:55:49 +00002577 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002578 | FALSETOK {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00002579 $$ = ValID::create(ConstantInt::getFalse());
Reid Spencer61c83e02006-08-18 08:43:06 +00002580 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002581 }
2582 | NULL_TOK {
2583 $$ = ValID::createNull();
Reid Spencer61c83e02006-08-18 08:43:06 +00002584 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002585 }
2586 | UNDEF {
2587 $$ = ValID::createUndef();
Reid Spencer61c83e02006-08-18 08:43:06 +00002588 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002589 }
2590 | ZEROINITIALIZER { // A vector zero constant.
2591 $$ = ValID::createZeroInit();
Reid Spencer61c83e02006-08-18 08:43:06 +00002592 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002593 }
2594 | '<' ConstVector '>' { // Nonempty unsized packed vector
Reid Spencera132e042006-12-03 05:46:11 +00002595 const Type *ETy = (*$2)[0]->getType();
Eric Christopher2a5196f2008-09-24 04:55:49 +00002596 unsigned NumElements = $2->size();
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002597
2598 if (!ETy->isInteger() && !ETy->isFloatingPoint())
2599 GEN_ERROR("Invalid vector element type: " + ETy->getDescription());
Eric Christopher2a5196f2008-09-24 04:55:49 +00002600
Reid Spencer9d6565a2007-02-15 02:26:10 +00002601 VectorType* pt = VectorType::get(ETy, NumElements);
Dan Gohmanf910eaa2008-06-09 14:45:02 +00002602 PATypeHolder* PTy = new PATypeHolder(HandleUpRefs(pt));
Eric Christopher2a5196f2008-09-24 04:55:49 +00002603
Chris Lattner58af2a12006-02-15 07:22:58 +00002604 // Verify all elements are correct type!
2605 for (unsigned i = 0; i < $2->size(); i++) {
Reid Spencera132e042006-12-03 05:46:11 +00002606 if (ETy != (*$2)[i]->getType())
Eric Christopher2a5196f2008-09-24 04:55:49 +00002607 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00002608 ETy->getDescription() +"' as required!\nIt is of type '" +
Reid Spencera132e042006-12-03 05:46:11 +00002609 (*$2)[i]->getType()->getDescription() + "'.");
Chris Lattner58af2a12006-02-15 07:22:58 +00002610 }
2611
Reid Spencer9d6565a2007-02-15 02:26:10 +00002612 $$ = ValID::create(ConstantVector::get(pt, *$2));
Chris Lattner58af2a12006-02-15 07:22:58 +00002613 delete PTy; delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002614 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002615 }
Dan Gohmanf910eaa2008-06-09 14:45:02 +00002616 | '[' ConstVector ']' { // Nonempty unsized arr
2617 const Type *ETy = (*$2)[0]->getType();
Eric Christopher2a5196f2008-09-24 04:55:49 +00002618 uint64_t NumElements = $2->size();
Dan Gohmanf910eaa2008-06-09 14:45:02 +00002619
2620 if (!ETy->isFirstClassType())
2621 GEN_ERROR("Invalid array element type: " + ETy->getDescription());
2622
2623 ArrayType *ATy = ArrayType::get(ETy, NumElements);
2624 PATypeHolder* PTy = new PATypeHolder(HandleUpRefs(ATy));
2625
2626 // Verify all elements are correct type!
2627 for (unsigned i = 0; i < $2->size(); i++) {
2628 if (ETy != (*$2)[i]->getType())
Eric Christopher2a5196f2008-09-24 04:55:49 +00002629 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Dan Gohmanf910eaa2008-06-09 14:45:02 +00002630 ETy->getDescription() +"' as required!\nIt is of type '"+
2631 (*$2)[i]->getType()->getDescription() + "'.");
2632 }
2633
2634 $$ = ValID::create(ConstantArray::get(ATy, *$2));
2635 delete PTy; delete $2;
2636 CHECK_FOR_ERROR
2637 }
2638 | '[' ']' {
Dan Gohman180c1692008-06-23 18:43:26 +00002639 // Use undef instead of an array because it's inconvenient to determine
2640 // the element type at this point, there being no elements to examine.
Dan Gohmanf910eaa2008-06-09 14:45:02 +00002641 $$ = ValID::createUndef();
2642 CHECK_FOR_ERROR
2643 }
2644 | 'c' STRINGCONSTANT {
Dan Gohman180c1692008-06-23 18:43:26 +00002645 uint64_t NumElements = $2->length();
Dan Gohmanf910eaa2008-06-09 14:45:02 +00002646 const Type *ETy = Type::Int8Ty;
2647
2648 ArrayType *ATy = ArrayType::get(ETy, NumElements);
2649
2650 std::vector<Constant*> Vals;
2651 for (unsigned i = 0; i < $2->length(); ++i)
2652 Vals.push_back(ConstantInt::get(ETy, (*$2)[i]));
2653 delete $2;
2654 $$ = ValID::create(ConstantArray::get(ATy, Vals));
2655 CHECK_FOR_ERROR
2656 }
2657 | '{' ConstVector '}' {
2658 std::vector<const Type*> Elements($2->size());
2659 for (unsigned i = 0, e = $2->size(); i != e; ++i)
2660 Elements[i] = (*$2)[i]->getType();
2661
2662 const StructType *STy = StructType::get(Elements);
2663 PATypeHolder* PTy = new PATypeHolder(HandleUpRefs(STy));
2664
2665 $$ = ValID::create(ConstantStruct::get(STy, *$2));
2666 delete PTy; delete $2;
2667 CHECK_FOR_ERROR
2668 }
2669 | '{' '}' {
2670 const StructType *STy = StructType::get(std::vector<const Type*>());
2671 $$ = ValID::create(ConstantStruct::get(STy, std::vector<Constant*>()));
2672 CHECK_FOR_ERROR
2673 }
2674 | '<' '{' ConstVector '}' '>' {
2675 std::vector<const Type*> Elements($3->size());
2676 for (unsigned i = 0, e = $3->size(); i != e; ++i)
2677 Elements[i] = (*$3)[i]->getType();
2678
2679 const StructType *STy = StructType::get(Elements, /*isPacked=*/true);
2680 PATypeHolder* PTy = new PATypeHolder(HandleUpRefs(STy));
2681
2682 $$ = ValID::create(ConstantStruct::get(STy, *$3));
2683 delete PTy; delete $3;
2684 CHECK_FOR_ERROR
2685 }
2686 | '<' '{' '}' '>' {
2687 const StructType *STy = StructType::get(std::vector<const Type*>(),
2688 /*isPacked=*/true);
2689 $$ = ValID::create(ConstantStruct::get(STy, std::vector<Constant*>()));
2690 CHECK_FOR_ERROR
2691 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002692 | ConstExpr {
Reid Spencera132e042006-12-03 05:46:11 +00002693 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002694 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002695 }
2696 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002697 $$ = ValID::createInlineAsm(*$3, *$5, $2);
2698 delete $3;
2699 delete $5;
Reid Spencer61c83e02006-08-18 08:43:06 +00002700 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002701 };
2702
2703// SymbolicValueRef - Reference to one of two ways of symbolically refering to
2704// another value.
2705//
Reid Spencer41dff5e2007-01-26 08:05:27 +00002706SymbolicValueRef : LOCALVAL_ID { // Is it an integer reference...?
2707 $$ = ValID::createLocalID($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002708 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002709 }
Reid Spencer41dff5e2007-01-26 08:05:27 +00002710 | GLOBALVAL_ID {
2711 $$ = ValID::createGlobalID($1);
2712 CHECK_FOR_ERROR
2713 }
2714 | LocalName { // Is it a named reference...?
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002715 $$ = ValID::createLocalName(*$1);
2716 delete $1;
Reid Spencer41dff5e2007-01-26 08:05:27 +00002717 CHECK_FOR_ERROR
2718 }
2719 | GlobalName { // Is it a named reference...?
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002720 $$ = ValID::createGlobalName(*$1);
2721 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002722 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002723 };
2724
2725// ValueRef - A reference to a definition... either constant or symbolic
2726ValueRef : SymbolicValueRef | ConstValueRef;
2727
2728
2729// ResolvedVal - a <type> <value> pair. This is used only in cases where the
2730// type immediately preceeds the value reference, and allows complex constant
2731// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
2732ResolvedVal : Types ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00002733 if (!UpRefs.empty())
2734 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Eric Christopher2a5196f2008-09-24 04:55:49 +00002735 $$ = getVal(*$1, $2);
Reid Spencer14310612006-12-31 05:40:51 +00002736 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002737 CHECK_FOR_ERROR
Reid Spencer14310612006-12-31 05:40:51 +00002738 }
2739 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00002740
Devang Patel7990dc72008-02-20 22:40:23 +00002741ReturnedVal : ResolvedVal {
2742 $$ = new std::vector<Value *>();
Eric Christopher2a5196f2008-09-24 04:55:49 +00002743 $$->push_back($1);
Devang Patel7990dc72008-02-20 22:40:23 +00002744 CHECK_FOR_ERROR
2745 }
Devang Patel6bfc63b2008-02-23 00:38:56 +00002746 | ReturnedVal ',' ResolvedVal {
Eric Christopher2a5196f2008-09-24 04:55:49 +00002747 ($$=$1)->push_back($3);
Devang Patel7990dc72008-02-20 22:40:23 +00002748 CHECK_FOR_ERROR
2749 };
2750
Chris Lattner58af2a12006-02-15 07:22:58 +00002751BasicBlockList : BasicBlockList BasicBlock {
2752 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002753 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002754 }
Eric Christopher2a5196f2008-09-24 04:55:49 +00002755 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
Chris Lattner58af2a12006-02-15 07:22:58 +00002756 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002757 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002758 };
2759
2760
Eric Christopher2a5196f2008-09-24 04:55:49 +00002761// Basic blocks are terminated by branching instructions:
Chris Lattner58af2a12006-02-15 07:22:58 +00002762// br, br/cc, switch, ret
2763//
Chris Lattner15bd0952008-08-29 17:20:18 +00002764BasicBlock : InstructionList OptLocalAssign BBTerminatorInst {
Chris Lattner58af2a12006-02-15 07:22:58 +00002765 setValueName($3, $2);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002766 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002767 InsertValue($3);
Chris Lattner58af2a12006-02-15 07:22:58 +00002768 $1->getInstList().push_back($3);
Chris Lattner58af2a12006-02-15 07:22:58 +00002769 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002770 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002771 };
2772
Chris Lattner15bd0952008-08-29 17:20:18 +00002773BasicBlock : InstructionList LocalNumber BBTerminatorInst {
2774 CHECK_FOR_ERROR
2775 int ValNum = InsertValue($3);
2776 if (ValNum != (int)$2)
2777 GEN_ERROR("Result value number %" + utostr($2) +
2778 " is incorrect, expected %" + utostr((unsigned)ValNum));
Eric Christopher2a5196f2008-09-24 04:55:49 +00002779
Chris Lattner15bd0952008-08-29 17:20:18 +00002780 $1->getInstList().push_back($3);
2781 $$ = $1;
2782 CHECK_FOR_ERROR
2783};
2784
2785
Chris Lattner58af2a12006-02-15 07:22:58 +00002786InstructionList : InstructionList Inst {
Reid Spencer3da59db2006-11-27 01:05:10 +00002787 if (CastInst *CI1 = dyn_cast<CastInst>($2))
2788 if (CastInst *CI2 = dyn_cast<CastInst>(CI1->getOperand(0)))
2789 if (CI2->getParent() == 0)
2790 $1->getInstList().push_back(CI2);
Chris Lattner58af2a12006-02-15 07:22:58 +00002791 $1->getInstList().push_back($2);
2792 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002793 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002794 }
Reid Spencer93c40032007-03-19 18:40:50 +00002795 | /* empty */ { // Empty space between instruction lists
Nick Lewycky280a6e62008-04-25 16:53:59 +00002796 $$ = defineBBVal(ValID::createLocalID(CurFun.NextValNum));
Reid Spencer61c83e02006-08-18 08:43:06 +00002797 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002798 }
Reid Spencer93c40032007-03-19 18:40:50 +00002799 | LABELSTR { // Labelled (named) basic block
Nick Lewycky280a6e62008-04-25 16:53:59 +00002800 $$ = defineBBVal(ValID::createLocalName(*$1));
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002801 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002802 CHECK_FOR_ERROR
Nick Lewycky280a6e62008-04-25 16:53:59 +00002803
Chris Lattner58af2a12006-02-15 07:22:58 +00002804 };
2805
Eric Christopher2a5196f2008-09-24 04:55:49 +00002806BBTerminatorInst :
Devang Patel7990dc72008-02-20 22:40:23 +00002807 RET ReturnedVal { // Return with a result...
Devang Patelb82b7f22008-02-26 22:17:48 +00002808 ValueList &VL = *$2;
Devang Patel13b823c2008-02-26 23:19:08 +00002809 assert(!VL.empty() && "Invalid ret operands!");
Dan Gohman1a570242008-07-23 00:54:54 +00002810 const Type *ReturnType = CurFun.CurrentFunction->getReturnType();
2811 if (VL.size() > 1 ||
2812 (isa<StructType>(ReturnType) &&
2813 (VL.empty() || VL[0]->getType() != ReturnType))) {
2814 Value *RV = UndefValue::get(ReturnType);
2815 for (unsigned i = 0, e = VL.size(); i != e; ++i) {
2816 Instruction *I = InsertValueInst::Create(RV, VL[i], i, "mrv");
2817 ($<BasicBlockVal>-1)->getInstList().push_back(I);
2818 RV = I;
2819 }
2820 $$ = ReturnInst::Create(RV);
2821 } else {
2822 $$ = ReturnInst::Create(VL[0]);
2823 }
Devang Patel7990dc72008-02-20 22:40:23 +00002824 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002825 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002826 }
Reid Spencer93c40032007-03-19 18:40:50 +00002827 | RET VOID { // Return with no result...
Gabor Greife64d2482008-04-06 23:07:54 +00002828 $$ = ReturnInst::Create();
Reid Spencer61c83e02006-08-18 08:43:06 +00002829 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002830 }
Reid Spencer93c40032007-03-19 18:40:50 +00002831 | BR LABEL ValueRef { // Unconditional Branch...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002832 BasicBlock* tmpBB = getBBVal($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002833 CHECK_FOR_ERROR
Gabor Greife64d2482008-04-06 23:07:54 +00002834 $$ = BranchInst::Create(tmpBB);
Reid Spencer93c40032007-03-19 18:40:50 +00002835 } // Conditional Branch...
Eric Christopher2a5196f2008-09-24 04:55:49 +00002836 | BR INTTYPE ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002837 if (cast<IntegerType>($2)->getBitWidth() != 1)
2838 GEN_ERROR("Branch condition must have type i1");
Reid Spencer5b7e7532006-09-28 19:28:24 +00002839 BasicBlock* tmpBBA = getBBVal($6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002840 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002841 BasicBlock* tmpBBB = getBBVal($9);
2842 CHECK_FOR_ERROR
Reid Spencer4fe16d62007-01-11 18:21:29 +00002843 Value* tmpVal = getVal(Type::Int1Ty, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002844 CHECK_FOR_ERROR
Gabor Greife64d2482008-04-06 23:07:54 +00002845 $$ = BranchInst::Create(tmpBBA, tmpBBB, tmpVal);
Chris Lattner58af2a12006-02-15 07:22:58 +00002846 }
Chris Lattnerf9078f92008-10-15 06:03:48 +00002847 | SWITCH INTTYPE ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencera132e042006-12-03 05:46:11 +00002848 Value* tmpVal = getVal($2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002849 CHECK_FOR_ERROR
2850 BasicBlock* tmpBB = getBBVal($6);
2851 CHECK_FOR_ERROR
Gabor Greife64d2482008-04-06 23:07:54 +00002852 SwitchInst *S = SwitchInst::Create(tmpVal, tmpBB, $8->size());
Chris Lattner58af2a12006-02-15 07:22:58 +00002853 $$ = S;
2854
2855 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
2856 E = $8->end();
2857 for (; I != E; ++I) {
2858 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
2859 S->addCase(CI, I->second);
2860 else
Reid Spencerb5334b02007-02-05 10:18:06 +00002861 GEN_ERROR("Switch case is constant, but not a simple integer");
Chris Lattner58af2a12006-02-15 07:22:58 +00002862 }
2863 delete $8;
Reid Spencer61c83e02006-08-18 08:43:06 +00002864 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002865 }
Chris Lattnerf9078f92008-10-15 06:03:48 +00002866 | SWITCH INTTYPE ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencera132e042006-12-03 05:46:11 +00002867 Value* tmpVal = getVal($2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002868 CHECK_FOR_ERROR
2869 BasicBlock* tmpBB = getBBVal($6);
2870 CHECK_FOR_ERROR
Gabor Greife64d2482008-04-06 23:07:54 +00002871 SwitchInst *S = SwitchInst::Create(tmpVal, tmpBB, 0);
Chris Lattner58af2a12006-02-15 07:22:58 +00002872 $$ = S;
Reid Spencer61c83e02006-08-18 08:43:06 +00002873 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002874 }
Devang Patel652203f2008-09-29 20:49:50 +00002875 | INVOKE OptCallingConv OptRetAttrs ResultTypes ValueRef '(' ParamList ')'
2876 OptFuncAttrs TO LABEL ValueRef UNWIND LABEL ValueRef {
Chris Lattner58af2a12006-02-15 07:22:58 +00002877
Reid Spencer14310612006-12-31 05:40:51 +00002878 // Handle the short syntax
2879 const PointerType *PFTy = 0;
2880 const FunctionType *Ty = 0;
Devang Patel652203f2008-09-29 20:49:50 +00002881 if (!(PFTy = dyn_cast<PointerType>($4->get())) ||
Chris Lattner58af2a12006-02-15 07:22:58 +00002882 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
2883 // Pull out the types of all of the arguments...
2884 std::vector<const Type*> ParamTypes;
Devang Patel652203f2008-09-29 20:49:50 +00002885 ParamList::iterator I = $7->begin(), E = $7->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00002886 for (; I != E; ++I) {
Reid Spencer14310612006-12-31 05:40:51 +00002887 const Type *Ty = I->Val->getType();
2888 if (Ty == Type::VoidTy)
2889 GEN_ERROR("Short call syntax cannot be used with varargs");
2890 ParamTypes.push_back(Ty);
Chris Lattner58af2a12006-02-15 07:22:58 +00002891 }
Eric Christopher2a5196f2008-09-24 04:55:49 +00002892
Devang Patel652203f2008-09-29 20:49:50 +00002893 if (!FunctionType::isValidReturnType(*$4))
Chris Lattnera925a142008-04-23 05:37:08 +00002894 GEN_ERROR("Invalid result type for LLVM function");
2895
Devang Patel652203f2008-09-29 20:49:50 +00002896 Ty = FunctionType::get($4->get(), ParamTypes, false);
Christopher Lamb4374f8e2007-12-17 01:17:35 +00002897 PFTy = PointerType::getUnqual(Ty);
Chris Lattner58af2a12006-02-15 07:22:58 +00002898 }
2899
Devang Patel652203f2008-09-29 20:49:50 +00002900 delete $4;
Reid Spencer66728ef2007-03-20 01:13:36 +00002901
Devang Patel652203f2008-09-29 20:49:50 +00002902 Value *V = getVal(PFTy, $5); // Get the function we're calling...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002903 CHECK_FOR_ERROR
Devang Patel652203f2008-09-29 20:49:50 +00002904 BasicBlock *Normal = getBBVal($12);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002905 CHECK_FOR_ERROR
Devang Patel652203f2008-09-29 20:49:50 +00002906 BasicBlock *Except = getBBVal($15);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002907 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002908
Devang Patel05988662008-09-25 21:00:45 +00002909 SmallVector<AttributeWithIndex, 8> Attrs;
Devang Patel19c87462008-09-26 22:53:05 +00002910 //FIXME : In 3.0, stop accepting zext, sext and inreg as optional function
2911 //attributes.
Devang Patel652203f2008-09-29 20:49:50 +00002912 Attributes RetAttrs = $3;
2913 if ($9 != Attribute::None) {
2914 if ($9 & Attribute::ZExt) {
Devang Patel19c87462008-09-26 22:53:05 +00002915 RetAttrs = RetAttrs | Attribute::ZExt;
Devang Patel652203f2008-09-29 20:49:50 +00002916 $9 = $9 ^ Attribute::ZExt;
Devang Patel19c87462008-09-26 22:53:05 +00002917 }
Devang Patel652203f2008-09-29 20:49:50 +00002918 if ($9 & Attribute::SExt) {
Devang Patel19c87462008-09-26 22:53:05 +00002919 RetAttrs = RetAttrs | Attribute::SExt;
Devang Patel652203f2008-09-29 20:49:50 +00002920 $9 = $9 ^ Attribute::SExt;
Devang Patel19c87462008-09-26 22:53:05 +00002921 }
Devang Patel652203f2008-09-29 20:49:50 +00002922 if ($9 & Attribute::InReg) {
Devang Patel19c87462008-09-26 22:53:05 +00002923 RetAttrs = RetAttrs | Attribute::InReg;
Devang Patel652203f2008-09-29 20:49:50 +00002924 $9 = $9 ^ Attribute::InReg;
Devang Patel19c87462008-09-26 22:53:05 +00002925 }
Devang Patel19c87462008-09-26 22:53:05 +00002926 }
Devang Patel652203f2008-09-29 20:49:50 +00002927 if (RetAttrs != Attribute::None)
2928 Attrs.push_back(AttributeWithIndex::get(0, RetAttrs));
Devang Patel19c87462008-09-26 22:53:05 +00002929
Reid Spencer14310612006-12-31 05:40:51 +00002930 // Check the arguments
2931 ValueList Args;
Devang Patel652203f2008-09-29 20:49:50 +00002932 if ($7->empty()) { // Has no arguments?
Reid Spencer14310612006-12-31 05:40:51 +00002933 // Make sure no arguments is a good thing!
2934 if (Ty->getNumParams() != 0)
2935 GEN_ERROR("No arguments passed to a function that "
Reid Spencerb5334b02007-02-05 10:18:06 +00002936 "expects arguments");
Chris Lattner58af2a12006-02-15 07:22:58 +00002937 } else { // Has arguments?
2938 // Loop through FunctionType's arguments and ensure they are specified
2939 // correctly!
Chris Lattner58af2a12006-02-15 07:22:58 +00002940 FunctionType::param_iterator I = Ty->param_begin();
2941 FunctionType::param_iterator E = Ty->param_end();
Devang Patel652203f2008-09-29 20:49:50 +00002942 ParamList::iterator ArgI = $7->begin(), ArgE = $7->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00002943 unsigned index = 1;
Chris Lattner58af2a12006-02-15 07:22:58 +00002944
Duncan Sandsdc024672007-11-27 13:23:08 +00002945 for (; ArgI != ArgE && I != E; ++ArgI, ++I, ++index) {
Reid Spencer14310612006-12-31 05:40:51 +00002946 if (ArgI->Val->getType() != *I)
2947 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00002948 (*I)->getDescription() + "'");
Reid Spencer14310612006-12-31 05:40:51 +00002949 Args.push_back(ArgI->Val);
Devang Patel05988662008-09-25 21:00:45 +00002950 if (ArgI->Attrs != Attribute::None)
2951 Attrs.push_back(AttributeWithIndex::get(index, ArgI->Attrs));
Reid Spencer14310612006-12-31 05:40:51 +00002952 }
Reid Spencera132e042006-12-03 05:46:11 +00002953
Reid Spencer14310612006-12-31 05:40:51 +00002954 if (Ty->isVarArg()) {
2955 if (I == E)
Chris Lattner38905612008-02-19 04:36:25 +00002956 for (; ArgI != ArgE; ++ArgI, ++index) {
Reid Spencer14310612006-12-31 05:40:51 +00002957 Args.push_back(ArgI->Val); // push the remaining varargs
Devang Patel05988662008-09-25 21:00:45 +00002958 if (ArgI->Attrs != Attribute::None)
2959 Attrs.push_back(AttributeWithIndex::get(index, ArgI->Attrs));
Chris Lattner38905612008-02-19 04:36:25 +00002960 }
Reid Spencer14310612006-12-31 05:40:51 +00002961 } else if (I != E || ArgI != ArgE)
Reid Spencerb5334b02007-02-05 10:18:06 +00002962 GEN_ERROR("Invalid number of parameters detected");
Chris Lattner58af2a12006-02-15 07:22:58 +00002963 }
Devang Patel652203f2008-09-29 20:49:50 +00002964 if ($9 != Attribute::None)
2965 Attrs.push_back(AttributeWithIndex::get(~0, $9));
Devang Patel05988662008-09-25 21:00:45 +00002966 AttrListPtr PAL;
Duncan Sandsdc024672007-11-27 13:23:08 +00002967 if (!Attrs.empty())
Devang Patel05988662008-09-25 21:00:45 +00002968 PAL = AttrListPtr::get(Attrs.begin(), Attrs.end());
Duncan Sandsdc024672007-11-27 13:23:08 +00002969
Reid Spencer14310612006-12-31 05:40:51 +00002970 // Create the InvokeInst
Dan Gohman041e2eb2008-05-15 19:50:34 +00002971 InvokeInst *II = InvokeInst::Create(V, Normal, Except,
2972 Args.begin(), Args.end());
Reid Spencer14310612006-12-31 05:40:51 +00002973 II->setCallingConv($2);
Devang Patel05988662008-09-25 21:00:45 +00002974 II->setAttributes(PAL);
Reid Spencer14310612006-12-31 05:40:51 +00002975 $$ = II;
Devang Patel652203f2008-09-29 20:49:50 +00002976 delete $7;
Reid Spencer61c83e02006-08-18 08:43:06 +00002977 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002978 }
2979 | UNWIND {
2980 $$ = new UnwindInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002981 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002982 }
2983 | UNREACHABLE {
2984 $$ = new UnreachableInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002985 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002986 };
2987
2988
2989
Chris Lattnerf9078f92008-10-15 06:03:48 +00002990JumpTable : JumpTable INTTYPE ConstValueRef ',' LABEL ValueRef {
Chris Lattner58af2a12006-02-15 07:22:58 +00002991 $$ = $1;
Reid Spencer93c40032007-03-19 18:40:50 +00002992 Constant *V = cast<Constant>(getExistingVal($2, $3));
Reid Spencer5b7e7532006-09-28 19:28:24 +00002993 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002994 if (V == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002995 GEN_ERROR("May only switch on a constant pool value");
Chris Lattner58af2a12006-02-15 07:22:58 +00002996
Reid Spencer5b7e7532006-09-28 19:28:24 +00002997 BasicBlock* tmpBB = getBBVal($6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002998 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002999 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner58af2a12006-02-15 07:22:58 +00003000 }
Chris Lattnerf9078f92008-10-15 06:03:48 +00003001 | INTTYPE ConstValueRef ',' LABEL ValueRef {
Chris Lattner58af2a12006-02-15 07:22:58 +00003002 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Reid Spencer93c40032007-03-19 18:40:50 +00003003 Constant *V = cast<Constant>(getExistingVal($1, $2));
Reid Spencer5b7e7532006-09-28 19:28:24 +00003004 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003005
3006 if (V == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00003007 GEN_ERROR("May only switch on a constant pool value");
Chris Lattner58af2a12006-02-15 07:22:58 +00003008
Reid Spencer5b7e7532006-09-28 19:28:24 +00003009 BasicBlock* tmpBB = getBBVal($5);
Reid Spencer61c83e02006-08-18 08:43:06 +00003010 CHECK_FOR_ERROR
Eric Christopher2a5196f2008-09-24 04:55:49 +00003011 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner58af2a12006-02-15 07:22:58 +00003012 };
3013
Reid Spencer41dff5e2007-01-26 08:05:27 +00003014Inst : OptLocalAssign InstVal {
Reid Spenceref9b9a72007-02-05 20:47:22 +00003015 // Is this definition named?? if so, assign the name...
3016 setValueName($2, $1);
3017 CHECK_FOR_ERROR
3018 InsertValue($2);
3019 $$ = $2;
3020 CHECK_FOR_ERROR
3021 };
3022
Chris Lattner15bd0952008-08-29 17:20:18 +00003023Inst : LocalNumber InstVal {
3024 CHECK_FOR_ERROR
3025 int ValNum = InsertValue($2);
Eric Christopher2a5196f2008-09-24 04:55:49 +00003026
Chris Lattner15bd0952008-08-29 17:20:18 +00003027 if (ValNum != (int)$1)
3028 GEN_ERROR("Result value number %" + utostr($1) +
3029 " is incorrect, expected %" + utostr((unsigned)ValNum));
3030
3031 $$ = $2;
3032 CHECK_FOR_ERROR
3033 };
3034
Chris Lattner58af2a12006-02-15 07:22:58 +00003035
3036PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Reid Spencer14310612006-12-31 05:40:51 +00003037 if (!UpRefs.empty())
3038 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00003039 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Reid Spencera132e042006-12-03 05:46:11 +00003040 Value* tmpVal = getVal(*$1, $3);
Reid Spencer61c83e02006-08-18 08:43:06 +00003041 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00003042 BasicBlock* tmpBB = getBBVal($5);
3043 CHECK_FOR_ERROR
3044 $$->push_back(std::make_pair(tmpVal, tmpBB));
Reid Spencera132e042006-12-03 05:46:11 +00003045 delete $1;
Chris Lattner58af2a12006-02-15 07:22:58 +00003046 }
3047 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
3048 $$ = $1;
Reid Spencer5b7e7532006-09-28 19:28:24 +00003049 Value* tmpVal = getVal($1->front().first->getType(), $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00003050 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00003051 BasicBlock* tmpBB = getBBVal($6);
3052 CHECK_FOR_ERROR
3053 $1->push_back(std::make_pair(tmpVal, tmpBB));
Chris Lattner58af2a12006-02-15 07:22:58 +00003054 };
3055
3056
Devang Patel05988662008-09-25 21:00:45 +00003057ParamList : Types OptAttributes ValueRef OptAttributes {
3058 // FIXME: Remove trailing OptAttributes in LLVM 3.0, it was a mistake in 2.0
Reid Spencer14310612006-12-31 05:40:51 +00003059 if (!UpRefs.empty())
3060 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
3061 // Used for call and invoke instructions
Dale Johanneseneb57ea72007-11-05 21:20:28 +00003062 $$ = new ParamList();
Duncan Sandsdc024672007-11-27 13:23:08 +00003063 ParamListEntry E; E.Attrs = $2 | $4; E.Val = getVal($1->get(), $3);
Reid Spencer14310612006-12-31 05:40:51 +00003064 $$->push_back(E);
Reid Spencer66728ef2007-03-20 01:13:36 +00003065 delete $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00003066 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003067 }
Devang Patel05988662008-09-25 21:00:45 +00003068 | LABEL OptAttributes ValueRef OptAttributes {
3069 // FIXME: Remove trailing OptAttributes in LLVM 3.0, it was a mistake in 2.0
Dale Johanneseneb57ea72007-11-05 21:20:28 +00003070 // Labels are only valid in ASMs
3071 $$ = new ParamList();
Duncan Sandsdc024672007-11-27 13:23:08 +00003072 ParamListEntry E; E.Attrs = $2 | $4; E.Val = getBBVal($3);
Dale Johanneseneb57ea72007-11-05 21:20:28 +00003073 $$->push_back(E);
Duncan Sandsdc024672007-11-27 13:23:08 +00003074 CHECK_FOR_ERROR
Dale Johanneseneb57ea72007-11-05 21:20:28 +00003075 }
Devang Patel05988662008-09-25 21:00:45 +00003076 | ParamList ',' Types OptAttributes ValueRef OptAttributes {
3077 // FIXME: Remove trailing OptAttributes in LLVM 3.0, it was a mistake in 2.0
Reid Spencer14310612006-12-31 05:40:51 +00003078 if (!UpRefs.empty())
3079 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00003080 $$ = $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00003081 ParamListEntry E; E.Attrs = $4 | $6; E.Val = getVal($3->get(), $5);
Reid Spencer14310612006-12-31 05:40:51 +00003082 $$->push_back(E);
Reid Spencer66728ef2007-03-20 01:13:36 +00003083 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00003084 CHECK_FOR_ERROR
Reid Spencer14310612006-12-31 05:40:51 +00003085 }
Devang Patel05988662008-09-25 21:00:45 +00003086 | ParamList ',' LABEL OptAttributes ValueRef OptAttributes {
3087 // FIXME: Remove trailing OptAttributes in LLVM 3.0, it was a mistake in 2.0
Dale Johanneseneb57ea72007-11-05 21:20:28 +00003088 $$ = $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00003089 ParamListEntry E; E.Attrs = $4 | $6; E.Val = getBBVal($5);
Dale Johanneseneb57ea72007-11-05 21:20:28 +00003090 $$->push_back(E);
3091 CHECK_FOR_ERROR
3092 }
3093 | /*empty*/ { $$ = new ParamList(); };
Chris Lattner58af2a12006-02-15 07:22:58 +00003094
Reid Spencer14310612006-12-31 05:40:51 +00003095IndexList // Used for gep instructions and constant expressions
Reid Spencerc6c59fd2006-12-31 21:47:02 +00003096 : /*empty*/ { $$ = new std::vector<Value*>(); }
Reid Spencer14310612006-12-31 05:40:51 +00003097 | IndexList ',' ResolvedVal {
3098 $$ = $1;
3099 $$->push_back($3);
3100 CHECK_FOR_ERROR
3101 }
Reid Spencerc6c59fd2006-12-31 21:47:02 +00003102 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00003103
Dan Gohman81a0c0b2008-05-31 00:58:22 +00003104ConstantIndexList // Used for insertvalue and extractvalue instructions
3105 : ',' EUINT64VAL {
3106 $$ = new std::vector<unsigned>();
3107 if ((unsigned)$2 != $2)
3108 GEN_ERROR("Index " + utostr($2) + " is not valid for insertvalue or extractvalue.");
3109 $$->push_back($2);
3110 }
3111 | ConstantIndexList ',' EUINT64VAL {
3112 $$ = $1;
3113 if ((unsigned)$3 != $3)
3114 GEN_ERROR("Index " + utostr($3) + " is not valid for insertvalue or extractvalue.");
3115 $$->push_back($3);
3116 CHECK_FOR_ERROR
3117 }
3118 ;
3119
Chris Lattner58af2a12006-02-15 07:22:58 +00003120OptTailCall : TAIL CALL {
3121 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00003122 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003123 }
3124 | CALL {
3125 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00003126 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003127 };
3128
Chris Lattner58af2a12006-02-15 07:22:58 +00003129InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00003130 if (!UpRefs.empty())
3131 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Eric Christopher2a5196f2008-09-24 04:55:49 +00003132 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint() &&
Reid Spencer9d6565a2007-02-15 02:26:10 +00003133 !isa<VectorType>((*$2).get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00003134 GEN_ERROR(
Reid Spencerb5334b02007-02-05 10:18:06 +00003135 "Arithmetic operator requires integer, FP, or packed operands");
Eric Christopher2a5196f2008-09-24 04:55:49 +00003136 Value* val1 = getVal(*$2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00003137 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00003138 Value* val2 = getVal(*$2, $5);
Reid Spencer5b7e7532006-09-28 19:28:24 +00003139 CHECK_FOR_ERROR
Dan Gohmane4977cf2008-05-23 01:55:30 +00003140 $$ = BinaryOperator::Create($1, val1, val2);
Chris Lattner58af2a12006-02-15 07:22:58 +00003141 if ($$ == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00003142 GEN_ERROR("binary operator returned null");
Reid Spencera132e042006-12-03 05:46:11 +00003143 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00003144 }
3145 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00003146 if (!UpRefs.empty())
3147 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Chris Lattner42a75512007-01-15 02:27:26 +00003148 if (!(*$2)->isInteger()) {
Nate Begeman5bc1ea02008-07-29 15:49:41 +00003149 if (!isa<VectorType>($2->get()) ||
Reid Spencer9d6565a2007-02-15 02:26:10 +00003150 !cast<VectorType>($2->get())->getElementType()->isInteger())
Reid Spencerb5334b02007-02-05 10:18:06 +00003151 GEN_ERROR("Logical operator requires integral operands");
Chris Lattner58af2a12006-02-15 07:22:58 +00003152 }
Reid Spencera132e042006-12-03 05:46:11 +00003153 Value* tmpVal1 = getVal(*$2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00003154 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00003155 Value* tmpVal2 = getVal(*$2, $5);
Reid Spencer5b7e7532006-09-28 19:28:24 +00003156 CHECK_FOR_ERROR
Dan Gohmane4977cf2008-05-23 01:55:30 +00003157 $$ = BinaryOperator::Create($1, tmpVal1, tmpVal2);
Chris Lattner58af2a12006-02-15 07:22:58 +00003158 if ($$ == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00003159 GEN_ERROR("binary operator returned null");
Reid Spencera132e042006-12-03 05:46:11 +00003160 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00003161 }
Reid Spencera132e042006-12-03 05:46:11 +00003162 | ICMP IPredicates Types ValueRef ',' ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00003163 if (!UpRefs.empty())
3164 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003165 Value* tmpVal1 = getVal(*$3, $4);
3166 CHECK_FOR_ERROR
3167 Value* tmpVal2 = getVal(*$3, $6);
3168 CHECK_FOR_ERROR
Dan Gohmane4977cf2008-05-23 01:55:30 +00003169 $$ = CmpInst::Create($1, $2, tmpVal1, tmpVal2);
Reid Spencera132e042006-12-03 05:46:11 +00003170 if ($$ == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00003171 GEN_ERROR("icmp operator returned null");
Reid Spencer66728ef2007-03-20 01:13:36 +00003172 delete $3;
Reid Spencera132e042006-12-03 05:46:11 +00003173 }
3174 | FCMP FPredicates Types ValueRef ',' ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00003175 if (!UpRefs.empty())
3176 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003177 Value* tmpVal1 = getVal(*$3, $4);
3178 CHECK_FOR_ERROR
3179 Value* tmpVal2 = getVal(*$3, $6);
3180 CHECK_FOR_ERROR
Dan Gohmane4977cf2008-05-23 01:55:30 +00003181 $$ = CmpInst::Create($1, $2, tmpVal1, tmpVal2);
Reid Spencera132e042006-12-03 05:46:11 +00003182 if ($$ == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00003183 GEN_ERROR("fcmp operator returned null");
Reid Spencer66728ef2007-03-20 01:13:36 +00003184 delete $3;
Chris Lattner58af2a12006-02-15 07:22:58 +00003185 }
Nate Begemanac80ade2008-05-12 19:01:56 +00003186 | VICMP IPredicates Types ValueRef ',' ValueRef {
3187 if (!UpRefs.empty())
3188 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
3189 if (!isa<VectorType>((*$3).get()))
3190 GEN_ERROR("Scalar types not supported by vicmp instruction");
3191 Value* tmpVal1 = getVal(*$3, $4);
3192 CHECK_FOR_ERROR
3193 Value* tmpVal2 = getVal(*$3, $6);
3194 CHECK_FOR_ERROR
Dan Gohmane4977cf2008-05-23 01:55:30 +00003195 $$ = CmpInst::Create($1, $2, tmpVal1, tmpVal2);
Nate Begemanac80ade2008-05-12 19:01:56 +00003196 if ($$ == 0)
Dan Gohmand8ee59b2008-09-09 01:13:24 +00003197 GEN_ERROR("vicmp operator returned null");
Nate Begemanac80ade2008-05-12 19:01:56 +00003198 delete $3;
3199 }
3200 | VFCMP FPredicates Types ValueRef ',' ValueRef {
3201 if (!UpRefs.empty())
3202 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
3203 if (!isa<VectorType>((*$3).get()))
3204 GEN_ERROR("Scalar types not supported by vfcmp instruction");
3205 Value* tmpVal1 = getVal(*$3, $4);
3206 CHECK_FOR_ERROR
3207 Value* tmpVal2 = getVal(*$3, $6);
3208 CHECK_FOR_ERROR
Dan Gohmane4977cf2008-05-23 01:55:30 +00003209 $$ = CmpInst::Create($1, $2, tmpVal1, tmpVal2);
Nate Begemanac80ade2008-05-12 19:01:56 +00003210 if ($$ == 0)
Dan Gohmand8ee59b2008-09-09 01:13:24 +00003211 GEN_ERROR("vfcmp operator returned null");
Nate Begemanac80ade2008-05-12 19:01:56 +00003212 delete $3;
3213 }
Reid Spencer3da59db2006-11-27 01:05:10 +00003214 | CastOps ResolvedVal TO Types {
Reid Spencer14310612006-12-31 05:40:51 +00003215 if (!UpRefs.empty())
3216 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003217 Value* Val = $2;
Reid Spencerb0fcf8f2007-01-17 02:48:45 +00003218 const Type* DestTy = $4->get();
3219 if (!CastInst::castIsValid($1, Val, DestTy))
3220 GEN_ERROR("invalid cast opcode for cast from '" +
3221 Val->getType()->getDescription() + "' to '" +
Eric Christopher2a5196f2008-09-24 04:55:49 +00003222 DestTy->getDescription() + "'");
Dan Gohmane4977cf2008-05-23 01:55:30 +00003223 $$ = CastInst::Create($1, Val, DestTy);
Reid Spencera132e042006-12-03 05:46:11 +00003224 delete $4;
Chris Lattner58af2a12006-02-15 07:22:58 +00003225 }
3226 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Dan Gohmand8ee59b2008-09-09 01:13:24 +00003227 if (isa<VectorType>($2->getType())) {
3228 // vector select
3229 if (!isa<VectorType>($4->getType())
3230 || !isa<VectorType>($6->getType()) )
3231 GEN_ERROR("vector select value types must be vector types");
3232 const VectorType* cond_type = cast<VectorType>($2->getType());
3233 const VectorType* select_type = cast<VectorType>($4->getType());
3234 if (cond_type->getElementType() != Type::Int1Ty)
3235 GEN_ERROR("vector select condition element type must be boolean");
3236 if (cond_type->getNumElements() != select_type->getNumElements())
3237 GEN_ERROR("vector select number of elements must be the same");
3238 } else {
3239 if ($2->getType() != Type::Int1Ty)
3240 GEN_ERROR("select condition must be boolean");
3241 }
Reid Spencera132e042006-12-03 05:46:11 +00003242 if ($4->getType() != $6->getType())
Dan Gohmand8ee59b2008-09-09 01:13:24 +00003243 GEN_ERROR("select value types must match");
Gabor Greife64d2482008-04-06 23:07:54 +00003244 $$ = SelectInst::Create($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00003245 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003246 }
3247 | VAARG ResolvedVal ',' Types {
Reid Spencer14310612006-12-31 05:40:51 +00003248 if (!UpRefs.empty())
3249 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003250 $$ = new VAArgInst($2, *$4);
3251 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00003252 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003253 }
Chris Lattner58af2a12006-02-15 07:22:58 +00003254 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00003255 if (!ExtractElementInst::isValidOperands($2, $4))
Reid Spencerb5334b02007-02-05 10:18:06 +00003256 GEN_ERROR("Invalid extractelement operands");
Reid Spencera132e042006-12-03 05:46:11 +00003257 $$ = new ExtractElementInst($2, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00003258 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003259 }
3260 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00003261 if (!InsertElementInst::isValidOperands($2, $4, $6))
Reid Spencerb5334b02007-02-05 10:18:06 +00003262 GEN_ERROR("Invalid insertelement operands");
Gabor Greife64d2482008-04-06 23:07:54 +00003263 $$ = InsertElementInst::Create($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00003264 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003265 }
Chris Lattnerd5efe842006-04-08 01:18:56 +00003266 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00003267 if (!ShuffleVectorInst::isValidOperands($2, $4, $6))
Reid Spencerb5334b02007-02-05 10:18:06 +00003268 GEN_ERROR("Invalid shufflevector operands");
Reid Spencera132e042006-12-03 05:46:11 +00003269 $$ = new ShuffleVectorInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00003270 CHECK_FOR_ERROR
Chris Lattnerd5efe842006-04-08 01:18:56 +00003271 }
Chris Lattner58af2a12006-02-15 07:22:58 +00003272 | PHI_TOK PHIList {
3273 const Type *Ty = $2->front().first->getType();
3274 if (!Ty->isFirstClassType())
Reid Spencerb5334b02007-02-05 10:18:06 +00003275 GEN_ERROR("PHI node operands must be of first class type");
Gabor Greife64d2482008-04-06 23:07:54 +00003276 $$ = PHINode::Create(Ty);
Chris Lattner58af2a12006-02-15 07:22:58 +00003277 ((PHINode*)$$)->reserveOperandSpace($2->size());
3278 while ($2->begin() != $2->end()) {
Eric Christopher2a5196f2008-09-24 04:55:49 +00003279 if ($2->front().first->getType() != Ty)
Reid Spencerb5334b02007-02-05 10:18:06 +00003280 GEN_ERROR("All elements of a PHI node must be of the same type");
Chris Lattner58af2a12006-02-15 07:22:58 +00003281 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
3282 $2->pop_front();
3283 }
3284 delete $2; // Free the list...
Reid Spencer61c83e02006-08-18 08:43:06 +00003285 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003286 }
Devang Patel652203f2008-09-29 20:49:50 +00003287 | OptTailCall OptCallingConv OptRetAttrs ResultTypes ValueRef '(' ParamList ')'
Reid Spencer218ded22007-01-05 17:07:23 +00003288 OptFuncAttrs {
Reid Spencer14310612006-12-31 05:40:51 +00003289
3290 // Handle the short syntax
Reid Spencer3da59db2006-11-27 01:05:10 +00003291 const PointerType *PFTy = 0;
3292 const FunctionType *Ty = 0;
Devang Patel652203f2008-09-29 20:49:50 +00003293 if (!(PFTy = dyn_cast<PointerType>($4->get())) ||
Chris Lattner58af2a12006-02-15 07:22:58 +00003294 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
3295 // Pull out the types of all of the arguments...
3296 std::vector<const Type*> ParamTypes;
Devang Patel652203f2008-09-29 20:49:50 +00003297 ParamList::iterator I = $7->begin(), E = $7->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00003298 for (; I != E; ++I) {
Reid Spencer14310612006-12-31 05:40:51 +00003299 const Type *Ty = I->Val->getType();
3300 if (Ty == Type::VoidTy)
3301 GEN_ERROR("Short call syntax cannot be used with varargs");
3302 ParamTypes.push_back(Ty);
Chris Lattner58af2a12006-02-15 07:22:58 +00003303 }
Chris Lattnera925a142008-04-23 05:37:08 +00003304
Devang Patel652203f2008-09-29 20:49:50 +00003305 if (!FunctionType::isValidReturnType(*$4))
Chris Lattnera925a142008-04-23 05:37:08 +00003306 GEN_ERROR("Invalid result type for LLVM function");
3307
Devang Patel652203f2008-09-29 20:49:50 +00003308 Ty = FunctionType::get($4->get(), ParamTypes, false);
Christopher Lamb4374f8e2007-12-17 01:17:35 +00003309 PFTy = PointerType::getUnqual(Ty);
Chris Lattner58af2a12006-02-15 07:22:58 +00003310 }
Chris Lattner6cdc6822007-04-26 05:31:05 +00003311
Devang Patel652203f2008-09-29 20:49:50 +00003312 Value *V = getVal(PFTy, $5); // Get the function we're calling...
Reid Spencer5b7e7532006-09-28 19:28:24 +00003313 CHECK_FOR_ERROR
Chris Lattner6cdc6822007-04-26 05:31:05 +00003314
Reid Spencer7780acb2007-04-16 06:56:07 +00003315 // Check for call to invalid intrinsic to avoid crashing later.
3316 if (Function *theF = dyn_cast<Function>(V)) {
Reid Spencered48de22007-04-16 22:02:23 +00003317 if (theF->hasName() && (theF->getValueName()->getKeyLength() >= 5) &&
Reid Spencer36fdde12007-04-16 20:35:38 +00003318 (0 == strncmp(theF->getValueName()->getKeyData(), "llvm.", 5)) &&
3319 !theF->getIntrinsicID(true))
Reid Spencer7780acb2007-04-16 06:56:07 +00003320 GEN_ERROR("Call to invalid LLVM intrinsic function '" +
3321 theF->getName() + "'");
3322 }
3323
Devang Patel05988662008-09-25 21:00:45 +00003324 // Set up the Attributes for the function
3325 SmallVector<AttributeWithIndex, 8> Attrs;
Devang Patel19c87462008-09-26 22:53:05 +00003326 //FIXME : In 3.0, stop accepting zext, sext and inreg as optional function
3327 //attributes.
Devang Patel652203f2008-09-29 20:49:50 +00003328 Attributes RetAttrs = $3;
3329 if ($9 != Attribute::None) {
3330 if ($9 & Attribute::ZExt) {
Devang Patel19c87462008-09-26 22:53:05 +00003331 RetAttrs = RetAttrs | Attribute::ZExt;
Devang Patel652203f2008-09-29 20:49:50 +00003332 $9 = $9 ^ Attribute::ZExt;
Devang Patel19c87462008-09-26 22:53:05 +00003333 }
Devang Patel652203f2008-09-29 20:49:50 +00003334 if ($9 & Attribute::SExt) {
Devang Patel19c87462008-09-26 22:53:05 +00003335 RetAttrs = RetAttrs | Attribute::SExt;
Devang Patel652203f2008-09-29 20:49:50 +00003336 $9 = $9 ^ Attribute::SExt;
Devang Patel19c87462008-09-26 22:53:05 +00003337 }
Devang Patel652203f2008-09-29 20:49:50 +00003338 if ($9 & Attribute::InReg) {
Devang Patel19c87462008-09-26 22:53:05 +00003339 RetAttrs = RetAttrs | Attribute::InReg;
Devang Patel652203f2008-09-29 20:49:50 +00003340 $9 = $9 ^ Attribute::InReg;
Devang Patel19c87462008-09-26 22:53:05 +00003341 }
Devang Patel19c87462008-09-26 22:53:05 +00003342 }
Devang Patel652203f2008-09-29 20:49:50 +00003343 if (RetAttrs != Attribute::None)
3344 Attrs.push_back(AttributeWithIndex::get(0, RetAttrs));
Devang Patel19c87462008-09-26 22:53:05 +00003345
Eric Christopher2a5196f2008-09-24 04:55:49 +00003346 // Check the arguments
Reid Spencer14310612006-12-31 05:40:51 +00003347 ValueList Args;
Devang Patel652203f2008-09-29 20:49:50 +00003348 if ($7->empty()) { // Has no arguments?
Chris Lattner58af2a12006-02-15 07:22:58 +00003349 // Make sure no arguments is a good thing!
3350 if (Ty->getNumParams() != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00003351 GEN_ERROR("No arguments passed to a function that "
Reid Spencerb5334b02007-02-05 10:18:06 +00003352 "expects arguments");
Chris Lattner58af2a12006-02-15 07:22:58 +00003353 } else { // Has arguments?
3354 // Loop through FunctionType's arguments and ensure they are specified
Duncan Sandsdc024672007-11-27 13:23:08 +00003355 // correctly. Also, gather any parameter attributes.
Chris Lattner58af2a12006-02-15 07:22:58 +00003356 FunctionType::param_iterator I = Ty->param_begin();
3357 FunctionType::param_iterator E = Ty->param_end();
Devang Patel652203f2008-09-29 20:49:50 +00003358 ParamList::iterator ArgI = $7->begin(), ArgE = $7->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00003359 unsigned index = 1;
Chris Lattner58af2a12006-02-15 07:22:58 +00003360
Duncan Sandsdc024672007-11-27 13:23:08 +00003361 for (; ArgI != ArgE && I != E; ++ArgI, ++I, ++index) {
Reid Spencer14310612006-12-31 05:40:51 +00003362 if (ArgI->Val->getType() != *I)
3363 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00003364 (*I)->getDescription() + "'");
Reid Spencer14310612006-12-31 05:40:51 +00003365 Args.push_back(ArgI->Val);
Devang Patel05988662008-09-25 21:00:45 +00003366 if (ArgI->Attrs != Attribute::None)
3367 Attrs.push_back(AttributeWithIndex::get(index, ArgI->Attrs));
Reid Spencer14310612006-12-31 05:40:51 +00003368 }
3369 if (Ty->isVarArg()) {
3370 if (I == E)
Chris Lattner38905612008-02-19 04:36:25 +00003371 for (; ArgI != ArgE; ++ArgI, ++index) {
Reid Spencer14310612006-12-31 05:40:51 +00003372 Args.push_back(ArgI->Val); // push the remaining varargs
Devang Patel05988662008-09-25 21:00:45 +00003373 if (ArgI->Attrs != Attribute::None)
3374 Attrs.push_back(AttributeWithIndex::get(index, ArgI->Attrs));
Chris Lattner38905612008-02-19 04:36:25 +00003375 }
Reid Spencer14310612006-12-31 05:40:51 +00003376 } else if (I != E || ArgI != ArgE)
Reid Spencerb5334b02007-02-05 10:18:06 +00003377 GEN_ERROR("Invalid number of parameters detected");
Chris Lattner58af2a12006-02-15 07:22:58 +00003378 }
Devang Patel652203f2008-09-29 20:49:50 +00003379 if ($9 != Attribute::None)
3380 Attrs.push_back(AttributeWithIndex::get(~0, $9));
Duncan Sandsdc024672007-11-27 13:23:08 +00003381
Devang Patel05988662008-09-25 21:00:45 +00003382 // Finish off the Attributes and check them
3383 AttrListPtr PAL;
Duncan Sandsdc024672007-11-27 13:23:08 +00003384 if (!Attrs.empty())
Devang Patel05988662008-09-25 21:00:45 +00003385 PAL = AttrListPtr::get(Attrs.begin(), Attrs.end());
Duncan Sandsdc024672007-11-27 13:23:08 +00003386
Reid Spencer14310612006-12-31 05:40:51 +00003387 // Create the call node
Gabor Greife64d2482008-04-06 23:07:54 +00003388 CallInst *CI = CallInst::Create(V, Args.begin(), Args.end());
Reid Spencer14310612006-12-31 05:40:51 +00003389 CI->setTailCall($1);
3390 CI->setCallingConv($2);
Devang Patel05988662008-09-25 21:00:45 +00003391 CI->setAttributes(PAL);
Reid Spencer14310612006-12-31 05:40:51 +00003392 $$ = CI;
Devang Patel652203f2008-09-29 20:49:50 +00003393 delete $7;
3394 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00003395 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003396 }
3397 | MemoryInst {
3398 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00003399 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003400 };
3401
Chris Lattner58af2a12006-02-15 07:22:58 +00003402OptVolatile : VOLATILE {
3403 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00003404 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003405 }
3406 | /* empty */ {
3407 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00003408 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003409 };
3410
3411
3412
3413MemoryInst : MALLOC Types OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003414 if (!UpRefs.empty())
3415 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003416 $$ = new MallocInst(*$2, 0, $3);
3417 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00003418 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003419 }
Reid Spencera54b7cb2007-01-12 07:05:14 +00003420 | MALLOC Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003421 if (!UpRefs.empty())
3422 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Dan Gohman81a0c0b2008-05-31 00:58:22 +00003423 if ($4 != Type::Int32Ty)
3424 GEN_ERROR("Malloc array size is not a 32-bit integer!");
Reid Spencera132e042006-12-03 05:46:11 +00003425 Value* tmpVal = getVal($4, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00003426 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00003427 $$ = new MallocInst(*$2, tmpVal, $6);
3428 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00003429 }
3430 | ALLOCA Types OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003431 if (!UpRefs.empty())
3432 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003433 $$ = new AllocaInst(*$2, 0, $3);
3434 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00003435 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003436 }
Reid Spencera54b7cb2007-01-12 07:05:14 +00003437 | ALLOCA Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003438 if (!UpRefs.empty())
3439 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Dan Gohman81a0c0b2008-05-31 00:58:22 +00003440 if ($4 != Type::Int32Ty)
3441 GEN_ERROR("Alloca array size is not a 32-bit integer!");
Reid Spencera132e042006-12-03 05:46:11 +00003442 Value* tmpVal = getVal($4, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00003443 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00003444 $$ = new AllocaInst(*$2, tmpVal, $6);
3445 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00003446 }
3447 | FREE ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00003448 if (!isa<PointerType>($2->getType()))
Eric Christopher2a5196f2008-09-24 04:55:49 +00003449 GEN_ERROR("Trying to free nonpointer type " +
Reid Spencerb5334b02007-02-05 10:18:06 +00003450 $2->getType()->getDescription() + "");
Reid Spencera132e042006-12-03 05:46:11 +00003451 $$ = new FreeInst($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00003452 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003453 }
3454
Christopher Lamb5c104242007-04-22 20:09:11 +00003455 | OptVolatile LOAD Types ValueRef OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003456 if (!UpRefs.empty())
3457 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003458 if (!isa<PointerType>($3->get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00003459 GEN_ERROR("Can't load from nonpointer type: " +
Reid Spencera132e042006-12-03 05:46:11 +00003460 (*$3)->getDescription());
3461 if (!cast<PointerType>($3->get())->getElementType()->isFirstClassType())
Reid Spencer61c83e02006-08-18 08:43:06 +00003462 GEN_ERROR("Can't load from pointer of non-first-class type: " +
Reid Spencera132e042006-12-03 05:46:11 +00003463 (*$3)->getDescription());
3464 Value* tmpVal = getVal(*$3, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00003465 CHECK_FOR_ERROR
Christopher Lamb5c104242007-04-22 20:09:11 +00003466 $$ = new LoadInst(tmpVal, "", $1, $5);
Reid Spencera132e042006-12-03 05:46:11 +00003467 delete $3;
Chris Lattner58af2a12006-02-15 07:22:58 +00003468 }
Christopher Lamb5c104242007-04-22 20:09:11 +00003469 | OptVolatile STORE ResolvedVal ',' Types ValueRef OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003470 if (!UpRefs.empty())
3471 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003472 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00003473 if (!PT)
Reid Spencer61c83e02006-08-18 08:43:06 +00003474 GEN_ERROR("Can't store to a nonpointer type: " +
Reid Spencera132e042006-12-03 05:46:11 +00003475 (*$5)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00003476 const Type *ElTy = PT->getElementType();
Reid Spencera132e042006-12-03 05:46:11 +00003477 if (ElTy != $3->getType())
3478 GEN_ERROR("Can't store '" + $3->getType()->getDescription() +
Reid Spencerb5334b02007-02-05 10:18:06 +00003479 "' into space of type '" + ElTy->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00003480
Reid Spencera132e042006-12-03 05:46:11 +00003481 Value* tmpVal = getVal(*$5, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00003482 CHECK_FOR_ERROR
Christopher Lamb5c104242007-04-22 20:09:11 +00003483 $$ = new StoreInst($3, tmpVal, $1, $7);
Reid Spencera132e042006-12-03 05:46:11 +00003484 delete $5;
Chris Lattner58af2a12006-02-15 07:22:58 +00003485 }
Dan Gohmane4977cf2008-05-23 01:55:30 +00003486 | GETRESULT Types ValueRef ',' EUINT64VAL {
Dan Gohman1a570242008-07-23 00:54:54 +00003487 if (!UpRefs.empty())
3488 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
3489 if (!isa<StructType>($2->get()) && !isa<ArrayType>($2->get()))
3490 GEN_ERROR("getresult insn requires an aggregate operand");
3491 if (!ExtractValueInst::getIndexedType(*$2, $5))
3492 GEN_ERROR("Invalid getresult index for type '" +
3493 (*$2)->getDescription()+ "'");
3494
3495 Value *tmpVal = getVal(*$2, $3);
Devang Patel5a970972008-02-19 22:27:01 +00003496 CHECK_FOR_ERROR
Dan Gohman1a570242008-07-23 00:54:54 +00003497 $$ = ExtractValueInst::Create(tmpVal, $5);
3498 delete $2;
Devang Patel5a970972008-02-19 22:27:01 +00003499 }
Chris Lattner58af2a12006-02-15 07:22:58 +00003500 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencer14310612006-12-31 05:40:51 +00003501 if (!UpRefs.empty())
3502 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003503 if (!isa<PointerType>($2->get()))
Reid Spencerb5334b02007-02-05 10:18:06 +00003504 GEN_ERROR("getelementptr insn requires pointer operand");
Chris Lattner58af2a12006-02-15 07:22:58 +00003505
Dan Gohman041e2eb2008-05-15 19:50:34 +00003506 if (!GetElementPtrInst::getIndexedType(*$2, $4->begin(), $4->end()))
Reid Spencer61c83e02006-08-18 08:43:06 +00003507 GEN_ERROR("Invalid getelementptr indices for type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00003508 (*$2)->getDescription()+ "'");
Reid Spencera132e042006-12-03 05:46:11 +00003509 Value* tmpVal = getVal(*$2, $3);
Reid Spencer61c83e02006-08-18 08:43:06 +00003510 CHECK_FOR_ERROR
Gabor Greife64d2482008-04-06 23:07:54 +00003511 $$ = GetElementPtrInst::Create(tmpVal, $4->begin(), $4->end());
Eric Christopher2a5196f2008-09-24 04:55:49 +00003512 delete $2;
Reid Spencer5b7e7532006-09-28 19:28:24 +00003513 delete $4;
Dan Gohmane4977cf2008-05-23 01:55:30 +00003514 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +00003515 | EXTRACTVALUE Types ValueRef ConstantIndexList {
Dan Gohmane4977cf2008-05-23 01:55:30 +00003516 if (!UpRefs.empty())
3517 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
3518 if (!isa<StructType>($2->get()) && !isa<ArrayType>($2->get()))
3519 GEN_ERROR("extractvalue insn requires an aggregate operand");
3520
3521 if (!ExtractValueInst::getIndexedType(*$2, $4->begin(), $4->end()))
3522 GEN_ERROR("Invalid extractvalue indices for type '" +
3523 (*$2)->getDescription()+ "'");
3524 Value* tmpVal = getVal(*$2, $3);
3525 CHECK_FOR_ERROR
3526 $$ = ExtractValueInst::Create(tmpVal, $4->begin(), $4->end());
Eric Christopher2a5196f2008-09-24 04:55:49 +00003527 delete $2;
Dan Gohmane4977cf2008-05-23 01:55:30 +00003528 delete $4;
3529 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +00003530 | INSERTVALUE Types ValueRef ',' Types ValueRef ConstantIndexList {
Dan Gohmane4977cf2008-05-23 01:55:30 +00003531 if (!UpRefs.empty())
3532 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
3533 if (!isa<StructType>($2->get()) && !isa<ArrayType>($2->get()))
3534 GEN_ERROR("extractvalue insn requires an aggregate operand");
3535
3536 if (ExtractValueInst::getIndexedType(*$2, $7->begin(), $7->end()) != $5->get())
3537 GEN_ERROR("Invalid insertvalue indices for type '" +
3538 (*$2)->getDescription()+ "'");
3539 Value* aggVal = getVal(*$2, $3);
3540 Value* tmpVal = getVal(*$5, $6);
3541 CHECK_FOR_ERROR
3542 $$ = InsertValueInst::Create(aggVal, tmpVal, $7->begin(), $7->end());
Eric Christopher2a5196f2008-09-24 04:55:49 +00003543 delete $2;
Dan Gohmane4977cf2008-05-23 01:55:30 +00003544 delete $5;
3545 delete $7;
Chris Lattner58af2a12006-02-15 07:22:58 +00003546 };
3547
3548
3549%%
Reid Spencer61c83e02006-08-18 08:43:06 +00003550
Reid Spencer14310612006-12-31 05:40:51 +00003551// common code from the two 'RunVMAsmParser' functions
3552static Module* RunParser(Module * M) {
Reid Spencer14310612006-12-31 05:40:51 +00003553 CurModule.CurrentModule = M;
Reid Spencer14310612006-12-31 05:40:51 +00003554 // Check to make sure the parser succeeded
3555 if (yyparse()) {
3556 if (ParserResult)
3557 delete ParserResult;
3558 return 0;
3559 }
3560
Reid Spencer0d60b5a2007-03-30 01:37:39 +00003561 // Emit an error if there are any unresolved types left.
3562 if (!CurModule.LateResolveTypes.empty()) {
3563 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
3564 if (DID.Type == ValID::LocalName) {
3565 GenerateError("Undefined type remains at eof: '"+DID.getName() + "'");
3566 } else {
3567 GenerateError("Undefined type remains at eof: #" + itostr(DID.Num));
3568 }
3569 if (ParserResult)
3570 delete ParserResult;
3571 return 0;
3572 }
3573
3574 // Emit an error if there are any unresolved values left.
3575 if (!CurModule.LateResolveValues.empty()) {
3576 Value *V = CurModule.LateResolveValues.back();
3577 std::map<Value*, std::pair<ValID, int> >::iterator I =
3578 CurModule.PlaceHolderInfo.find(V);
3579
3580 if (I != CurModule.PlaceHolderInfo.end()) {
3581 ValID &DID = I->second.first;
3582 if (DID.Type == ValID::LocalName) {
3583 GenerateError("Undefined value remains at eof: "+DID.getName() + "'");
3584 } else {
3585 GenerateError("Undefined value remains at eof: #" + itostr(DID.Num));
3586 }
3587 if (ParserResult)
3588 delete ParserResult;
3589 return 0;
3590 }
3591 }
3592
Reid Spencer14310612006-12-31 05:40:51 +00003593 // Check to make sure that parsing produced a result
3594 if (!ParserResult)
3595 return 0;
3596
3597 // Reset ParserResult variable while saving its value for the result.
3598 Module *Result = ParserResult;
3599 ParserResult = 0;
3600
3601 return Result;
3602}
3603
Reid Spencer61c83e02006-08-18 08:43:06 +00003604void llvm::GenerateError(const std::string &message, int LineNo) {
Duncan Sandsdc024672007-11-27 13:23:08 +00003605 if (LineNo == -1) LineNo = LLLgetLineNo();
Reid Spencer61c83e02006-08-18 08:43:06 +00003606 // TODO: column number in exception
3607 if (TheParseError)
Duncan Sandsdc024672007-11-27 13:23:08 +00003608 TheParseError->setError(LLLgetFilename(), message, LineNo);
Reid Spencer61c83e02006-08-18 08:43:06 +00003609 TriggerError = 1;
3610}
3611
Chris Lattner58af2a12006-02-15 07:22:58 +00003612int yyerror(const char *ErrorMsg) {
Duncan Sandsdc024672007-11-27 13:23:08 +00003613 std::string where = LLLgetFilename() + ":" + utostr(LLLgetLineNo()) + ": ";
Reid Spenceref9b9a72007-02-05 20:47:22 +00003614 std::string errMsg = where + "error: " + std::string(ErrorMsg);
Duncan Sandsdc024672007-11-27 13:23:08 +00003615 if (yychar != YYEMPTY && yychar != 0) {
3616 errMsg += " while reading token: '";
Eric Christopher2a5196f2008-09-24 04:55:49 +00003617 errMsg += std::string(LLLgetTokenStart(),
Duncan Sandsdc024672007-11-27 13:23:08 +00003618 LLLgetTokenStart()+LLLgetTokenLength()) + "'";
3619 }
Reid Spencer61c83e02006-08-18 08:43:06 +00003620 GenerateError(errMsg);
Chris Lattner58af2a12006-02-15 07:22:58 +00003621 return 0;
3622}