blob: f2e6404d307cc6d0633e55137e7565ad60a330cb [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;
421 Tmp.extOrTrunc(Ty->getPrimitiveSizeInBits());
422 return ConstantInt::get(Tmp);
423 }
Eric Christopher2a5196f2008-09-24 04:55:49 +0000424
Chris Lattner58af2a12006-02-15 07:22:58 +0000425 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattner38905612008-02-19 04:36:25 +0000426 if (!Ty->isFloatingPoint() ||
427 !ConstantFP::isValueValidForType(Ty, *D.ConstPoolFP)) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000428 GenerateError("FP constant invalid for type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000429 return 0;
430 }
Eric Christopher2a5196f2008-09-24 04:55:49 +0000431 // Lexer has no type info, so builds all float and double FP constants
Dale Johannesenc72cd7e2007-09-11 18:33:39 +0000432 // as double. Fix this here. Long double does not need this.
433 if (&D.ConstPoolFP->getSemantics() == &APFloat::IEEEdouble &&
Dale Johannesen236bbd42008-10-09 23:01:34 +0000434 Ty==Type::FloatTy) {
435 bool ignored;
436 D.ConstPoolFP->convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven,
437 &ignored);
438 }
Chris Lattnerd8eb63f2008-04-20 00:41:19 +0000439 return ConstantFP::get(*D.ConstPoolFP);
Chris Lattner58af2a12006-02-15 07:22:58 +0000440
441 case ValID::ConstNullVal: // Is it a null value?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000442 if (!isa<PointerType>(Ty)) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000443 GenerateError("Cannot create a a non pointer null");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000444 return 0;
445 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000446 return ConstantPointerNull::get(cast<PointerType>(Ty));
447
448 case ValID::ConstUndefVal: // Is it an undef value?
449 return UndefValue::get(Ty);
450
451 case ValID::ConstZeroVal: // Is it a zero value?
452 return Constant::getNullValue(Ty);
Eric Christopher2a5196f2008-09-24 04:55:49 +0000453
Chris Lattner58af2a12006-02-15 07:22:58 +0000454 case ValID::ConstantVal: // Fully resolved constant?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000455 if (D.ConstantValue->getType() != Ty) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000456 GenerateError("Constant expression type different from required type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000457 return 0;
458 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000459 return D.ConstantValue;
460
461 case ValID::InlineAsmVal: { // Inline asm expression
462 const PointerType *PTy = dyn_cast<PointerType>(Ty);
463 const FunctionType *FTy =
464 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
Reid Spencer5b7e7532006-09-28 19:28:24 +0000465 if (!FTy || !InlineAsm::Verify(FTy, D.IAD->Constraints)) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000466 GenerateError("Invalid type for asm constraint string");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000467 return 0;
468 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000469 InlineAsm *IA = InlineAsm::get(FTy, D.IAD->AsmString, D.IAD->Constraints,
470 D.IAD->HasSideEffects);
471 D.destroy(); // Free InlineAsmDescriptor.
472 return IA;
473 }
474 default:
Reid Spencera9720f52007-02-05 17:04:00 +0000475 assert(0 && "Unhandled case!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000476 return 0;
477 } // End of switch
478
Reid Spencera9720f52007-02-05 17:04:00 +0000479 assert(0 && "Unhandled case!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000480 return 0;
481}
482
Reid Spencer93c40032007-03-19 18:40:50 +0000483// getVal - This function is identical to getExistingVal, except that if a
Chris Lattner58af2a12006-02-15 07:22:58 +0000484// value is not already defined, it "improvises" by creating a placeholder var
485// that looks and acts just like the requested variable. When the value is
486// defined later, all uses of the placeholder variable are replaced with the
487// real thing.
488//
489static Value *getVal(const Type *Ty, const ValID &ID) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000490 if (Ty == Type::LabelTy) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000491 GenerateError("Cannot use a basic block here");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000492 return 0;
493 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000494
495 // See if the value has already been defined.
Reid Spencer93c40032007-03-19 18:40:50 +0000496 Value *V = getExistingVal(Ty, ID);
Chris Lattner58af2a12006-02-15 07:22:58 +0000497 if (V) return V;
Reid Spencer5b7e7532006-09-28 19:28:24 +0000498 if (TriggerError) return 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000499
Reid Spencer5b7e7532006-09-28 19:28:24 +0000500 if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty)) {
Dan Gohmane4977cf2008-05-23 01:55:30 +0000501 GenerateError("Invalid use of a non-first-class type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000502 return 0;
503 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000504
505 // If we reached here, we referenced either a symbol that we don't know about
506 // or an id number that hasn't been read yet. We may be referencing something
507 // forward, so just create an entry to be resolved later and get to it...
508 //
Anton Korobeynikov38e09802007-04-28 13:48:45 +0000509 switch (ID.Type) {
510 case ValID::GlobalName:
Reid Spencer9c9b63a2007-04-28 16:07:31 +0000511 case ValID::GlobalID: {
Anton Korobeynikov38e09802007-04-28 13:48:45 +0000512 const PointerType *PTy = dyn_cast<PointerType>(Ty);
513 if (!PTy) {
514 GenerateError("Invalid type for reference to global" );
515 return 0;
516 }
517 const Type* ElTy = PTy->getElementType();
518 if (const FunctionType *FTy = dyn_cast<FunctionType>(ElTy))
Gabor Greife64d2482008-04-06 23:07:54 +0000519 V = Function::Create(FTy, GlobalValue::ExternalLinkage);
Anton Korobeynikov38e09802007-04-28 13:48:45 +0000520 else
Christopher Lamba8ed9bf2007-12-11 09:02:08 +0000521 V = new GlobalVariable(ElTy, false, GlobalValue::ExternalLinkage, 0, "",
522 (Module*)0, false, PTy->getAddressSpace());
Anton Korobeynikov38e09802007-04-28 13:48:45 +0000523 break;
Reid Spencer9c9b63a2007-04-28 16:07:31 +0000524 }
Anton Korobeynikov38e09802007-04-28 13:48:45 +0000525 default:
526 V = new Argument(Ty);
527 }
Eric Christopher2a5196f2008-09-24 04:55:49 +0000528
Chris Lattner58af2a12006-02-15 07:22:58 +0000529 // Remember where this forward reference came from. FIXME, shouldn't we try
530 // to recycle these things??
531 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
Duncan Sandsdc024672007-11-27 13:23:08 +0000532 LLLgetLineNo())));
Chris Lattner58af2a12006-02-15 07:22:58 +0000533
534 if (inFunctionScope())
535 InsertValue(V, CurFun.LateResolveValues);
536 else
537 InsertValue(V, CurModule.LateResolveValues);
538 return V;
539}
540
Reid Spencer93c40032007-03-19 18:40:50 +0000541/// defineBBVal - This is a definition of a new basic block with the specified
542/// identifier which must be the same as CurFun.NextValNum, if its numeric.
Nick Lewycky280a6e62008-04-25 16:53:59 +0000543static BasicBlock *defineBBVal(const ValID &ID) {
Reid Spencera9720f52007-02-05 17:04:00 +0000544 assert(inFunctionScope() && "Can't get basic block at global scope!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000545
Chris Lattner58af2a12006-02-15 07:22:58 +0000546 BasicBlock *BB = 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000547
Reid Spencer93c40032007-03-19 18:40:50 +0000548 // First, see if this was forward referenced
Chris Lattner58af2a12006-02-15 07:22:58 +0000549
Reid Spencer93c40032007-03-19 18:40:50 +0000550 std::map<ValID, BasicBlock*>::iterator BBI = CurFun.BBForwardRefs.find(ID);
551 if (BBI != CurFun.BBForwardRefs.end()) {
552 BB = BBI->second;
Chris Lattner58af2a12006-02-15 07:22:58 +0000553 // The forward declaration could have been inserted anywhere in the
554 // function: insert it into the correct place now.
555 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
556 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
Reid Spencer93c40032007-03-19 18:40:50 +0000557
Reid Spencer66728ef2007-03-20 01:13:36 +0000558 // We're about to erase the entry, save the key so we can clean it up.
559 ValID Tmp = BBI->first;
560
Reid Spencer93c40032007-03-19 18:40:50 +0000561 // Erase the forward ref from the map as its no longer "forward"
562 CurFun.BBForwardRefs.erase(ID);
563
Eric Christopher2a5196f2008-09-24 04:55:49 +0000564 // The key has been removed from the map but so we don't want to leave
Reid Spencer66728ef2007-03-20 01:13:36 +0000565 // strdup'd memory around so destroy it too.
566 Tmp.destroy();
567
Reid Spencer93c40032007-03-19 18:40:50 +0000568 // If its a numbered definition, bump the number and set the BB value.
569 if (ID.Type == ValID::LocalID) {
570 assert(ID.Num == CurFun.NextValNum && "Invalid new block number");
571 InsertValue(BB);
572 }
Eric Christopher2a5196f2008-09-24 04:55:49 +0000573 } else {
574 // We haven't seen this BB before and its first mention is a definition.
Devang Patel67909432008-03-03 18:58:47 +0000575 // Just create it and return it.
576 std::string Name (ID.Type == ValID::LocalName ? ID.getName() : "");
Gabor Greife64d2482008-04-06 23:07:54 +0000577 BB = BasicBlock::Create(Name, CurFun.CurrentFunction);
Devang Patel67909432008-03-03 18:58:47 +0000578 if (ID.Type == ValID::LocalID) {
579 assert(ID.Num == CurFun.NextValNum && "Invalid new block number");
580 InsertValue(BB);
581 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000582 }
Reid Spencer93c40032007-03-19 18:40:50 +0000583
Devang Patel67909432008-03-03 18:58:47 +0000584 ID.destroy();
Reid Spencer93c40032007-03-19 18:40:50 +0000585 return BB;
586}
587
588/// getBBVal - get an existing BB value or create a forward reference for it.
Eric Christopher2a5196f2008-09-24 04:55:49 +0000589///
Reid Spencer93c40032007-03-19 18:40:50 +0000590static BasicBlock *getBBVal(const ValID &ID) {
591 assert(inFunctionScope() && "Can't get basic block at global scope!");
592
593 BasicBlock *BB = 0;
594
595 std::map<ValID, BasicBlock*>::iterator BBI = CurFun.BBForwardRefs.find(ID);
596 if (BBI != CurFun.BBForwardRefs.end()) {
597 BB = BBI->second;
598 } if (ID.Type == ValID::LocalName) {
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000599 std::string Name = ID.getName();
Reid Spencer93c40032007-03-19 18:40:50 +0000600 Value *N = CurFun.CurrentFunction->getValueSymbolTable().lookup(Name);
Anton Korobeynikov4aefd6b2008-02-20 12:07:57 +0000601 if (N) {
Reid Spencer93c40032007-03-19 18:40:50 +0000602 if (N->getType()->getTypeID() == Type::LabelTyID)
603 BB = cast<BasicBlock>(N);
604 else
605 GenerateError("Reference to label '" + Name + "' is actually of type '"+
606 N->getType()->getDescription() + "'");
Anton Korobeynikov4aefd6b2008-02-20 12:07:57 +0000607 }
Reid Spencer93c40032007-03-19 18:40:50 +0000608 } else if (ID.Type == ValID::LocalID) {
609 if (ID.Num < CurFun.NextValNum && ID.Num < CurFun.Values.size()) {
610 if (CurFun.Values[ID.Num]->getType()->getTypeID() == Type::LabelTyID)
611 BB = cast<BasicBlock>(CurFun.Values[ID.Num]);
612 else
Eric Christopher2a5196f2008-09-24 04:55:49 +0000613 GenerateError("Reference to label '%" + utostr(ID.Num) +
614 "' is actually of type '"+
Reid Spencer93c40032007-03-19 18:40:50 +0000615 CurFun.Values[ID.Num]->getType()->getDescription() + "'");
616 }
617 } else {
618 GenerateError("Illegal label reference " + ID.getName());
619 return 0;
620 }
621
622 // If its already been defined, return it now.
623 if (BB) {
624 ID.destroy(); // Free strdup'd memory.
625 return BB;
626 }
627
628 // Otherwise, this block has not been seen before, create it.
629 std::string Name;
630 if (ID.Type == ValID::LocalName)
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000631 Name = ID.getName();
Gabor Greife64d2482008-04-06 23:07:54 +0000632 BB = BasicBlock::Create(Name, CurFun.CurrentFunction);
Reid Spencer93c40032007-03-19 18:40:50 +0000633
634 // Insert it in the forward refs map.
635 CurFun.BBForwardRefs[ID] = BB;
636
Chris Lattner58af2a12006-02-15 07:22:58 +0000637 return BB;
638}
639
640
641//===----------------------------------------------------------------------===//
642// Code to handle forward references in instructions
643//===----------------------------------------------------------------------===//
644//
645// This code handles the late binding needed with statements that reference
646// values not defined yet... for example, a forward branch, or the PHI node for
647// a loop body.
648//
649// This keeps a table (CurFun.LateResolveValues) of all such forward references
650// and back patchs after we are done.
651//
652
653// ResolveDefinitions - If we could not resolve some defs at parsing
654// time (forward branches, phi functions for loops, etc...) resolve the
655// defs now...
656//
Eric Christopher2a5196f2008-09-24 04:55:49 +0000657static void
Reid Spencer93c40032007-03-19 18:40:50 +0000658ResolveDefinitions(ValueList &LateResolvers, ValueList *FutureLateResolvers) {
Chris Lattner58af2a12006-02-15 07:22:58 +0000659 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Reid Spencer93c40032007-03-19 18:40:50 +0000660 while (!LateResolvers.empty()) {
661 Value *V = LateResolvers.back();
662 LateResolvers.pop_back();
Chris Lattner58af2a12006-02-15 07:22:58 +0000663
Reid Spencer93c40032007-03-19 18:40:50 +0000664 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
665 CurModule.PlaceHolderInfo.find(V);
666 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000667
Reid Spencer93c40032007-03-19 18:40:50 +0000668 ValID &DID = PHI->second.first;
Chris Lattner58af2a12006-02-15 07:22:58 +0000669
Reid Spencer93c40032007-03-19 18:40:50 +0000670 Value *TheRealValue = getExistingVal(V->getType(), DID);
671 if (TriggerError)
672 return;
673 if (TheRealValue) {
674 V->replaceAllUsesWith(TheRealValue);
675 delete V;
676 CurModule.PlaceHolderInfo.erase(PHI);
677 } else if (FutureLateResolvers) {
678 // Functions have their unresolved items forwarded to the module late
679 // resolver table
680 InsertValue(V, *FutureLateResolvers);
681 } else {
682 if (DID.Type == ValID::LocalName || DID.Type == ValID::GlobalName) {
683 GenerateError("Reference to an invalid definition: '" +DID.getName()+
684 "' of type '" + V->getType()->getDescription() + "'",
685 PHI->second.second);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000686 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000687 } else {
Reid Spencer93c40032007-03-19 18:40:50 +0000688 GenerateError("Reference to an invalid definition: #" +
689 itostr(DID.Num) + " of type '" +
690 V->getType()->getDescription() + "'",
691 PHI->second.second);
692 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000693 }
694 }
695 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000696 LateResolvers.clear();
697}
698
699// ResolveTypeTo - A brand new type was just declared. This means that (if
700// name is not null) things referencing Name can be resolved. Otherwise, things
701// refering to the number can be resolved. Do this now.
702//
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000703static void ResolveTypeTo(std::string *Name, const Type *ToTy) {
Chris Lattner58af2a12006-02-15 07:22:58 +0000704 ValID D;
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000705 if (Name)
706 D = ValID::createLocalName(*Name);
Eric Christopher2a5196f2008-09-24 04:55:49 +0000707 else
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000708 D = ValID::createLocalID(CurModule.Types.size());
Chris Lattner58af2a12006-02-15 07:22:58 +0000709
Reid Spencer861d9d62006-11-28 07:29:44 +0000710 std::map<ValID, PATypeHolder>::iterator I =
Chris Lattner58af2a12006-02-15 07:22:58 +0000711 CurModule.LateResolveTypes.find(D);
712 if (I != CurModule.LateResolveTypes.end()) {
Reid Spencer861d9d62006-11-28 07:29:44 +0000713 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Nuno Lopes6ec8a252008-10-15 11:11:12 +0000714 I->first.destroy();
Chris Lattner58af2a12006-02-15 07:22:58 +0000715 CurModule.LateResolveTypes.erase(I);
716 }
Nuno Lopes191dfb92008-10-03 15:52:39 +0000717 D.destroy();
Chris Lattner58af2a12006-02-15 07:22:58 +0000718}
719
720// setValueName - Set the specified value to the name given. The name may be
721// null potentially, in which case this is a noop. The string passed in is
722// assumed to be a malloc'd string buffer, and is free'd by this function.
723//
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000724static void setValueName(Value *V, std::string *NameStr) {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000725 if (!NameStr) return;
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000726 std::string Name(*NameStr); // Copy string
727 delete NameStr; // Free old string
Chris Lattner58af2a12006-02-15 07:22:58 +0000728
Reid Spencer41dff5e2007-01-26 08:05:27 +0000729 if (V->getType() == Type::VoidTy) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000730 GenerateError("Can't assign name '" + Name+"' to value with void type");
Reid Spencer41dff5e2007-01-26 08:05:27 +0000731 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000732 }
Reid Spencer41dff5e2007-01-26 08:05:27 +0000733
Reid Spencera9720f52007-02-05 17:04:00 +0000734 assert(inFunctionScope() && "Must be in function scope!");
Reid Spenceref9b9a72007-02-05 20:47:22 +0000735 ValueSymbolTable &ST = CurFun.CurrentFunction->getValueSymbolTable();
736 if (ST.lookup(Name)) {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000737 GenerateError("Redefinition of value '" + Name + "' of type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +0000738 V->getType()->getDescription() + "'");
Reid Spencer41dff5e2007-01-26 08:05:27 +0000739 return;
740 }
741
742 // Set the name.
743 V->setName(Name);
Chris Lattner58af2a12006-02-15 07:22:58 +0000744}
745
746/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
747/// this is a declaration, otherwise it is a definition.
748static GlobalVariable *
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000749ParseGlobalVariable(std::string *NameStr,
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000750 GlobalValue::LinkageTypes Linkage,
751 GlobalValue::VisibilityTypes Visibility,
Chris Lattner58af2a12006-02-15 07:22:58 +0000752 bool isConstantGlobal, const Type *Ty,
Christopher Lamba8ed9bf2007-12-11 09:02:08 +0000753 Constant *Initializer, bool IsThreadLocal,
754 unsigned AddressSpace = 0) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000755 if (isa<FunctionType>(Ty)) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000756 GenerateError("Cannot declare global vars of function type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000757 return 0;
758 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +0000759 if (Ty == Type::LabelTy) {
760 GenerateError("Cannot declare global vars of label type");
761 return 0;
762 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000763
Christopher Lamba8ed9bf2007-12-11 09:02:08 +0000764 const PointerType *PTy = PointerType::get(Ty, AddressSpace);
Chris Lattner58af2a12006-02-15 07:22:58 +0000765
766 std::string Name;
767 if (NameStr) {
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000768 Name = *NameStr; // Copy string
769 delete NameStr; // Free old string
Chris Lattner58af2a12006-02-15 07:22:58 +0000770 }
771
772 // See if this global value was forward referenced. If so, recycle the
773 // object.
774 ValID ID;
775 if (!Name.empty()) {
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000776 ID = ValID::createGlobalName(Name);
Chris Lattner58af2a12006-02-15 07:22:58 +0000777 } else {
Reid Spencer93c40032007-03-19 18:40:50 +0000778 ID = ValID::createGlobalID(CurModule.Values.size());
Chris Lattner58af2a12006-02-15 07:22:58 +0000779 }
780
781 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
782 // Move the global to the end of the list, from whereever it was
783 // previously inserted.
784 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
785 CurModule.CurrentModule->getGlobalList().remove(GV);
786 CurModule.CurrentModule->getGlobalList().push_back(GV);
787 GV->setInitializer(Initializer);
788 GV->setLinkage(Linkage);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000789 GV->setVisibility(Visibility);
Chris Lattner58af2a12006-02-15 07:22:58 +0000790 GV->setConstant(isConstantGlobal);
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +0000791 GV->setThreadLocal(IsThreadLocal);
Chris Lattner58af2a12006-02-15 07:22:58 +0000792 InsertValue(GV, CurModule.Values);
Nuno Lopes191dfb92008-10-03 15:52:39 +0000793 ID.destroy();
Chris Lattner58af2a12006-02-15 07:22:58 +0000794 return GV;
795 }
796
Nuno Lopes191dfb92008-10-03 15:52:39 +0000797 ID.destroy();
798
Reid Spenceref9b9a72007-02-05 20:47:22 +0000799 // If this global has a name
Chris Lattner58af2a12006-02-15 07:22:58 +0000800 if (!Name.empty()) {
Reid Spenceref9b9a72007-02-05 20:47:22 +0000801 // if the global we're parsing has an initializer (is a definition) and
802 // has external linkage.
803 if (Initializer && Linkage != GlobalValue::InternalLinkage)
804 // If there is already a global with external linkage with this name
805 if (CurModule.CurrentModule->getGlobalVariable(Name, false)) {
806 // If we allow this GVar to get created, it will be renamed in the
807 // symbol table because it conflicts with an existing GVar. We can't
808 // allow redefinition of GVars whose linking indicates that their name
809 // must stay the same. Issue the error.
810 GenerateError("Redefinition of global variable named '" + Name +
811 "' of type '" + Ty->getDescription() + "'");
812 return 0;
813 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000814 }
815
816 // Otherwise there is no existing GV to use, create one now.
817 GlobalVariable *GV =
818 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
Christopher Lamba8ed9bf2007-12-11 09:02:08 +0000819 CurModule.CurrentModule, IsThreadLocal, AddressSpace);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000820 GV->setVisibility(Visibility);
Chris Lattner58af2a12006-02-15 07:22:58 +0000821 InsertValue(GV, CurModule.Values);
822 return GV;
823}
824
825// setTypeName - Set the specified type to the name given. The name may be
826// null potentially, in which case this is a noop. The string passed in is
827// assumed to be a malloc'd string buffer, and is freed by this function.
828//
829// This function returns true if the type has already been defined, but is
830// allowed to be redefined in the specified context. If the name is a new name
831// for the type plane, it is inserted and false is returned.
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000832static bool setTypeName(const Type *T, std::string *NameStr) {
Reid Spencera9720f52007-02-05 17:04:00 +0000833 assert(!inFunctionScope() && "Can't give types function-local names!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000834 if (NameStr == 0) return false;
Eric Christopher2a5196f2008-09-24 04:55:49 +0000835
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000836 std::string Name(*NameStr); // Copy string
837 delete NameStr; // Free old string
Chris Lattner58af2a12006-02-15 07:22:58 +0000838
839 // We don't allow assigning names to void type
Reid Spencer5b7e7532006-09-28 19:28:24 +0000840 if (T == Type::VoidTy) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000841 GenerateError("Can't assign name '" + Name + "' to the void type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000842 return false;
843 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000844
845 // Set the type name, checking for conflicts as we do so.
846 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
847
848 if (AlreadyExists) { // Inserting a name that is already defined???
849 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
Reid Spencera9720f52007-02-05 17:04:00 +0000850 assert(Existing && "Conflict but no matching type?!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000851
852 // There is only one case where this is allowed: when we are refining an
853 // opaque type. In this case, Existing will be an opaque type.
854 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
855 // We ARE replacing an opaque type!
856 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
857 return true;
858 }
859
860 // Otherwise, this is an attempt to redefine a type. That's okay if
861 // the redefinition is identical to the original. This will be so if
862 // Existing and T point to the same Type object. In this one case we
863 // allow the equivalent redefinition.
864 if (Existing == T) return true; // Yes, it's equal.
865
866 // Any other kind of (non-equivalent) redefinition is an error.
Reid Spencer63c34452007-01-05 21:51:07 +0000867 GenerateError("Redefinition of type named '" + Name + "' of type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +0000868 T->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +0000869 }
870
871 return false;
872}
873
874//===----------------------------------------------------------------------===//
875// Code for handling upreferences in type names...
876//
877
878// TypeContains - Returns true if Ty directly contains E in it.
879//
880static bool TypeContains(const Type *Ty, const Type *E) {
881 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
882 E) != Ty->subtype_end();
883}
884
885namespace {
886 struct UpRefRecord {
887 // NestingLevel - The number of nesting levels that need to be popped before
888 // this type is resolved.
889 unsigned NestingLevel;
890
891 // LastContainedTy - This is the type at the current binding level for the
892 // type. Every time we reduce the nesting level, this gets updated.
893 const Type *LastContainedTy;
894
895 // UpRefTy - This is the actual opaque type that the upreference is
896 // represented with.
897 OpaqueType *UpRefTy;
898
899 UpRefRecord(unsigned NL, OpaqueType *URTy)
900 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
901 };
902}
903
904// UpRefs - A list of the outstanding upreferences that need to be resolved.
905static std::vector<UpRefRecord> UpRefs;
906
907/// HandleUpRefs - Every time we finish a new layer of types, this function is
908/// called. It loops through the UpRefs vector, which is a list of the
909/// currently active types. For each type, if the up reference is contained in
910/// the newly completed type, we decrement the level count. When the level
911/// count reaches zero, the upreferenced type is the type that is passed in:
912/// thus we can complete the cycle.
913///
914static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner224f84f2006-08-18 17:34:45 +0000915 // If Ty isn't abstract, or if there are no up-references in it, then there is
916 // nothing to resolve here.
917 if (!ty->isAbstract() || UpRefs.empty()) return ty;
Eric Christopher2a5196f2008-09-24 04:55:49 +0000918
Chris Lattner58af2a12006-02-15 07:22:58 +0000919 PATypeHolder Ty(ty);
920 UR_OUT("Type '" << Ty->getDescription() <<
921 "' newly formed. Resolving upreferences.\n" <<
922 UpRefs.size() << " upreferences active!\n");
923
924 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
925 // to zero), we resolve them all together before we resolve them to Ty. At
926 // the end of the loop, if there is anything to resolve to Ty, it will be in
927 // this variable.
928 OpaqueType *TypeToResolve = 0;
929
930 for (unsigned i = 0; i != UpRefs.size(); ++i) {
931 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
932 << UpRefs[i].second->getDescription() << ") = "
933 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
934 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
935 // Decrement level of upreference
936 unsigned Level = --UpRefs[i].NestingLevel;
937 UpRefs[i].LastContainedTy = Ty;
938 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
939 if (Level == 0) { // Upreference should be resolved!
940 if (!TypeToResolve) {
941 TypeToResolve = UpRefs[i].UpRefTy;
942 } else {
943 UR_OUT(" * Resolving upreference for "
944 << UpRefs[i].second->getDescription() << "\n";
945 std::string OldName = UpRefs[i].UpRefTy->getDescription());
946 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
947 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
948 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
949 }
950 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
951 --i; // Do not skip the next element...
952 }
953 }
954 }
955
956 if (TypeToResolve) {
957 UR_OUT(" * Resolving upreference for "
958 << UpRefs[i].second->getDescription() << "\n";
959 std::string OldName = TypeToResolve->getDescription());
960 TypeToResolve->refineAbstractTypeTo(Ty);
961 }
962
963 return Ty;
964}
965
Chris Lattner58af2a12006-02-15 07:22:58 +0000966//===----------------------------------------------------------------------===//
967// RunVMAsmParser - Define an interface to this parser
968//===----------------------------------------------------------------------===//
969//
Reid Spencer14310612006-12-31 05:40:51 +0000970static Module* RunParser(Module * M);
971
Duncan Sandsdc024672007-11-27 13:23:08 +0000972Module *llvm::RunVMAsmParser(llvm::MemoryBuffer *MB) {
973 InitLLLexer(MB);
974 Module *M = RunParser(new Module(LLLgetFilename()));
975 FreeLexer();
976 return M;
Chris Lattner58af2a12006-02-15 07:22:58 +0000977}
978
979%}
980
981%union {
982 llvm::Module *ModuleVal;
983 llvm::Function *FunctionVal;
Chris Lattner58af2a12006-02-15 07:22:58 +0000984 llvm::BasicBlock *BasicBlockVal;
985 llvm::TerminatorInst *TermInstVal;
986 llvm::Instruction *InstVal;
Reid Spencera132e042006-12-03 05:46:11 +0000987 llvm::Constant *ConstVal;
Chris Lattner58af2a12006-02-15 07:22:58 +0000988
Reid Spencera132e042006-12-03 05:46:11 +0000989 const llvm::Type *PrimType;
Reid Spencer14310612006-12-31 05:40:51 +0000990 std::list<llvm::PATypeHolder> *TypeList;
Reid Spencera132e042006-12-03 05:46:11 +0000991 llvm::PATypeHolder *TypeVal;
992 llvm::Value *ValueVal;
Reid Spencera132e042006-12-03 05:46:11 +0000993 std::vector<llvm::Value*> *ValueList;
Dan Gohman81a0c0b2008-05-31 00:58:22 +0000994 std::vector<unsigned> *ConstantList;
Reid Spencer14310612006-12-31 05:40:51 +0000995 llvm::ArgListType *ArgList;
996 llvm::TypeWithAttrs TypeWithAttrs;
997 llvm::TypeWithAttrsList *TypeWithAttrsList;
Dale Johanneseneb57ea72007-11-05 21:20:28 +0000998 llvm::ParamList *ParamList;
Reid Spencer14310612006-12-31 05:40:51 +0000999
Chris Lattner58af2a12006-02-15 07:22:58 +00001000 // Represent the RHS of PHI node
Reid Spencera132e042006-12-03 05:46:11 +00001001 std::list<std::pair<llvm::Value*,
1002 llvm::BasicBlock*> > *PHIList;
Chris Lattner58af2a12006-02-15 07:22:58 +00001003 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
Reid Spencera132e042006-12-03 05:46:11 +00001004 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner58af2a12006-02-15 07:22:58 +00001005
1006 llvm::GlobalValue::LinkageTypes Linkage;
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001007 llvm::GlobalValue::VisibilityTypes Visibility;
Devang Patel05988662008-09-25 21:00:45 +00001008 llvm::Attributes Attributes;
Reid Spencer38c91a92007-02-28 02:24:54 +00001009 llvm::APInt *APIntVal;
Chris Lattner58af2a12006-02-15 07:22:58 +00001010 int64_t SInt64Val;
1011 uint64_t UInt64Val;
1012 int SIntVal;
1013 unsigned UIntVal;
Dale Johannesen43421b32007-09-06 18:13:44 +00001014 llvm::APFloat *FPVal;
Chris Lattner58af2a12006-02-15 07:22:58 +00001015 bool BoolVal;
1016
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001017 std::string *StrVal; // This memory must be deleted
1018 llvm::ValID ValIDVal;
Chris Lattner58af2a12006-02-15 07:22:58 +00001019
Reid Spencera132e042006-12-03 05:46:11 +00001020 llvm::Instruction::BinaryOps BinaryOpVal;
1021 llvm::Instruction::TermOps TermOpVal;
1022 llvm::Instruction::MemoryOps MemOpVal;
1023 llvm::Instruction::CastOps CastOpVal;
1024 llvm::Instruction::OtherOps OtherOpVal;
Reid Spencera132e042006-12-03 05:46:11 +00001025 llvm::ICmpInst::Predicate IPredicate;
1026 llvm::FCmpInst::Predicate FPredicate;
Chris Lattner58af2a12006-02-15 07:22:58 +00001027}
1028
Eric Christopher2a5196f2008-09-24 04:55:49 +00001029%type <ModuleVal> Module
Chris Lattner58af2a12006-02-15 07:22:58 +00001030%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
1031%type <BasicBlockVal> BasicBlock InstructionList
1032%type <TermInstVal> BBTerminatorInst
1033%type <InstVal> Inst InstVal MemoryInst
Anton Korobeynikov38e09802007-04-28 13:48:45 +00001034%type <ConstVal> ConstVal ConstExpr AliaseeRef
Chris Lattner58af2a12006-02-15 07:22:58 +00001035%type <ConstVector> ConstVector
1036%type <ArgList> ArgList ArgListH
Chris Lattner58af2a12006-02-15 07:22:58 +00001037%type <PHIList> PHIList
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001038%type <ParamList> ParamList // For call param lists & GEP indices
Reid Spencer14310612006-12-31 05:40:51 +00001039%type <ValueList> IndexList // For GEP indices
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001040%type <ConstantList> ConstantIndexList // For insertvalue/extractvalue indices
Eric Christopher2a5196f2008-09-24 04:55:49 +00001041%type <TypeList> TypeListI
Reid Spencer14310612006-12-31 05:40:51 +00001042%type <TypeWithAttrsList> ArgTypeList ArgTypeListI
Reid Spencer218ded22007-01-05 17:07:23 +00001043%type <TypeWithAttrs> ArgType
Chris Lattner58af2a12006-02-15 07:22:58 +00001044%type <JumpTable> JumpTable
1045%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00001046%type <BoolVal> ThreadLocal // 'thread_local' or not
Chris Lattner58af2a12006-02-15 07:22:58 +00001047%type <BoolVal> OptVolatile // 'volatile' or not
1048%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
1049%type <BoolVal> OptSideEffect // 'sideeffect' or not.
Reid Spencer14310612006-12-31 05:40:51 +00001050%type <Linkage> GVInternalLinkage GVExternalLinkage
1051%type <Linkage> FunctionDefineLinkage FunctionDeclareLinkage
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001052%type <Linkage> AliasLinkage
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001053%type <Visibility> GVVisibilityStyle
Chris Lattner58af2a12006-02-15 07:22:58 +00001054
1055// ValueRef - Unresolved reference to a definition or BB
1056%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
1057%type <ValueVal> ResolvedVal // <type> <valref> pair
Devang Patel7990dc72008-02-20 22:40:23 +00001058%type <ValueList> ReturnedVal
Chris Lattner58af2a12006-02-15 07:22:58 +00001059// Tokens and types for handling constant integer values
1060//
1061// ESINT64VAL - A negative number within long long range
1062%token <SInt64Val> ESINT64VAL
1063
1064// EUINT64VAL - A positive number within uns. long long range
1065%token <UInt64Val> EUINT64VAL
Chris Lattner58af2a12006-02-15 07:22:58 +00001066
Eric Christopher2a5196f2008-09-24 04:55:49 +00001067// ESAPINTVAL - A negative number with arbitrary precision
Reid Spencer38c91a92007-02-28 02:24:54 +00001068%token <APIntVal> ESAPINTVAL
1069
Eric Christopher2a5196f2008-09-24 04:55:49 +00001070// EUAPINTVAL - A positive number with arbitrary precision
Reid Spencer38c91a92007-02-28 02:24:54 +00001071%token <APIntVal> EUAPINTVAL
1072
Reid Spencer41dff5e2007-01-26 08:05:27 +00001073%token <UIntVal> LOCALVAL_ID GLOBALVAL_ID // %123 @123
Chris Lattner58af2a12006-02-15 07:22:58 +00001074%token <FPVal> FPVAL // Float or Double constant
1075
1076// Built in types...
Reid Spencer218ded22007-01-05 17:07:23 +00001077%type <TypeVal> Types ResultTypes
Chris Lattner740e7092008-10-15 06:16:57 +00001078%type <PrimType> PrimType // Classifications
Eric Christopher2a5196f2008-09-24 04:55:49 +00001079%token <PrimType> VOID INTTYPE
Dale Johannesen320fc8a2007-08-03 01:03:46 +00001080%token <PrimType> FLOAT DOUBLE X86_FP80 FP128 PPC_FP128 LABEL
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001081%token TYPE
Chris Lattner58af2a12006-02-15 07:22:58 +00001082
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001083
Eric Christopher2a5196f2008-09-24 04:55:49 +00001084%token<StrVal> LOCALVAR GLOBALVAR LABELSTR
Reid Spencered951ea2007-05-19 07:22:10 +00001085%token<StrVal> STRINGCONSTANT ATSTRINGCONSTANT PCTSTRINGCONSTANT
Reid Spencer41dff5e2007-01-26 08:05:27 +00001086%type <StrVal> LocalName OptLocalName OptLocalAssign
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001087%type <StrVal> GlobalName OptGlobalAssign GlobalAssign
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00001088%type <StrVal> OptSection SectionString OptGC
Chris Lattner58af2a12006-02-15 07:22:58 +00001089
Christopher Lambbf3348d2007-12-12 08:45:45 +00001090%type <UIntVal> OptAlign OptCAlign OptAddrSpace
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001091
Reid Spencer3d6b71e2007-04-09 01:56:05 +00001092%token ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001093%token DECLARE DEFINE GLOBAL CONSTANT SECTION ALIAS VOLATILE THREAD_LOCAL
Reid Spencer14310612006-12-31 05:40:51 +00001094%token TO DOTDOTDOT NULL_TOK UNDEF INTERNAL LINKONCE WEAK APPENDING
Dale Johannesenc7071cc2008-05-14 20:13:36 +00001095%token DLLIMPORT DLLEXPORT EXTERN_WEAK COMMON
Christopher Lamba8ed9bf2007-12-11 09:02:08 +00001096%token OPAQUE EXTERNAL TARGET TRIPLE ALIGN ADDRSPACE
Chris Lattner58af2a12006-02-15 07:22:58 +00001097%token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001098%token CC_TOK CCC_TOK FASTCC_TOK COLDCC_TOK X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
Nick Lewycky280a6e62008-04-25 16:53:59 +00001099%token DATALAYOUT
Chris Lattner15bd0952008-08-29 17:20:18 +00001100%type <UIntVal> OptCallingConv LocalNumber
Devang Patel05988662008-09-25 21:00:45 +00001101%type <Attributes> OptAttributes Attribute
1102%type <Attributes> OptFuncAttrs FuncAttr
Devang Patel652203f2008-09-29 20:49:50 +00001103%type <Attributes> OptRetAttrs RetAttr
Chris Lattner58af2a12006-02-15 07:22:58 +00001104
1105// Basic Block Terminating Operators
1106%token <TermOpVal> RET BR SWITCH INVOKE UNWIND UNREACHABLE
1107
1108// Binary Operators
Reid Spencere4d87aa2006-12-23 06:05:41 +00001109%type <BinaryOpVal> ArithmeticOps LogicalOps // Binops Subcatagories
Reid Spencer3ed469c2006-11-02 20:25:50 +00001110%token <BinaryOpVal> ADD SUB MUL UDIV SDIV FDIV UREM SREM FREM AND OR XOR
Reid Spencer832254e2007-02-02 02:16:23 +00001111%token <BinaryOpVal> SHL LSHR ASHR
1112
Eric Christopher2a5196f2008-09-24 04:55:49 +00001113%token <OtherOpVal> ICMP FCMP VICMP VFCMP
Reid Spencera132e042006-12-03 05:46:11 +00001114%type <IPredicate> IPredicates
Reid Spencera132e042006-12-03 05:46:11 +00001115%type <FPredicate> FPredicates
Eric Christopher2a5196f2008-09-24 04:55:49 +00001116%token EQ NE SLT SGT SLE SGE ULT UGT ULE UGE
Reid Spencer6e18b7d2006-12-03 06:59:29 +00001117%token OEQ ONE OLT OGT OLE OGE ORD UNO UEQ UNE
Chris Lattner58af2a12006-02-15 07:22:58 +00001118
1119// Memory Instructions
1120%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
1121
Reid Spencer3da59db2006-11-27 01:05:10 +00001122// Cast Operators
1123%type <CastOpVal> CastOps
1124%token <CastOpVal> TRUNC ZEXT SEXT FPTRUNC FPEXT BITCAST
1125%token <CastOpVal> UITOFP SITOFP FPTOUI FPTOSI INTTOPTR PTRTOINT
1126
Chris Lattner58af2a12006-02-15 07:22:58 +00001127// Other Operators
Reid Spencer832254e2007-02-02 02:16:23 +00001128%token <OtherOpVal> PHI_TOK SELECT VAARG
Chris Lattnerd5efe842006-04-08 01:18:56 +00001129%token <OtherOpVal> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
Devang Patel5a970972008-02-19 22:27:01 +00001130%token <OtherOpVal> GETRESULT
Dan Gohmane4977cf2008-05-23 01:55:30 +00001131%token <OtherOpVal> EXTRACTVALUE INSERTVALUE
Chris Lattner58af2a12006-02-15 07:22:58 +00001132
Reid Spencer218ded22007-01-05 17:07:23 +00001133// Function Attributes
Reid Spencerb8f85052007-07-31 03:50:36 +00001134%token SIGNEXT ZEROEXT NORETURN INREG SRET NOUNWIND NOALIAS BYVAL NEST
Devang Patel2c9c3e72008-09-26 23:51:19 +00001135%token READNONE READONLY GC OPTSIZE NOINLINE ALWAYSINLINE
Devang Pateld4980812008-09-02 20:52:40 +00001136
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001137// Visibility Styles
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +00001138%token DEFAULT HIDDEN PROTECTED
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001139
Chris Lattner58af2a12006-02-15 07:22:58 +00001140%start Module
1141%%
1142
Chris Lattner58af2a12006-02-15 07:22:58 +00001143
Chris Lattner58af2a12006-02-15 07:22:58 +00001144// Operations that are notably excluded from this list include:
1145// RET, BR, & SWITCH because they end basic blocks and are treated specially.
1146//
Reid Spencer3ed469c2006-11-02 20:25:50 +00001147ArithmeticOps: ADD | SUB | MUL | UDIV | SDIV | FDIV | UREM | SREM | FREM;
Reid Spencer832254e2007-02-02 02:16:23 +00001148LogicalOps : SHL | LSHR | ASHR | AND | OR | XOR;
Eric Christopher2a5196f2008-09-24 04:55:49 +00001149CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | BITCAST |
Reid Spencer3da59db2006-11-27 01:05:10 +00001150 UITOFP | SITOFP | FPTOUI | FPTOSI | INTTOPTR | PTRTOINT;
Reid Spencer832254e2007-02-02 02:16:23 +00001151
Eric Christopher2a5196f2008-09-24 04:55:49 +00001152IPredicates
Reid Spencer4012e832006-12-04 05:24:24 +00001153 : EQ { $$ = ICmpInst::ICMP_EQ; } | NE { $$ = ICmpInst::ICMP_NE; }
Reid Spencer6e18b7d2006-12-03 06:59:29 +00001154 | SLT { $$ = ICmpInst::ICMP_SLT; } | SGT { $$ = ICmpInst::ICMP_SGT; }
1155 | SLE { $$ = ICmpInst::ICMP_SLE; } | SGE { $$ = ICmpInst::ICMP_SGE; }
1156 | ULT { $$ = ICmpInst::ICMP_ULT; } | UGT { $$ = ICmpInst::ICMP_UGT; }
Eric Christopher2a5196f2008-09-24 04:55:49 +00001157 | ULE { $$ = ICmpInst::ICMP_ULE; } | UGE { $$ = ICmpInst::ICMP_UGE; }
Reid Spencer6e18b7d2006-12-03 06:59:29 +00001158 ;
1159
Eric Christopher2a5196f2008-09-24 04:55:49 +00001160FPredicates
Reid Spencer6e18b7d2006-12-03 06:59:29 +00001161 : OEQ { $$ = FCmpInst::FCMP_OEQ; } | ONE { $$ = FCmpInst::FCMP_ONE; }
1162 | OLT { $$ = FCmpInst::FCMP_OLT; } | OGT { $$ = FCmpInst::FCMP_OGT; }
1163 | OLE { $$ = FCmpInst::FCMP_OLE; } | OGE { $$ = FCmpInst::FCMP_OGE; }
1164 | ORD { $$ = FCmpInst::FCMP_ORD; } | UNO { $$ = FCmpInst::FCMP_UNO; }
1165 | UEQ { $$ = FCmpInst::FCMP_UEQ; } | UNE { $$ = FCmpInst::FCMP_UNE; }
1166 | ULT { $$ = FCmpInst::FCMP_ULT; } | UGT { $$ = FCmpInst::FCMP_UGT; }
1167 | ULE { $$ = FCmpInst::FCMP_ULE; } | UGE { $$ = FCmpInst::FCMP_UGE; }
1168 | TRUETOK { $$ = FCmpInst::FCMP_TRUE; }
1169 | FALSETOK { $$ = FCmpInst::FCMP_FALSE; }
1170 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00001171
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001172LocalName : LOCALVAR | STRINGCONSTANT | PCTSTRINGCONSTANT ;
Reid Spencer41dff5e2007-01-26 08:05:27 +00001173OptLocalName : LocalName | /*empty*/ { $$ = 0; };
1174
Christopher Lambbf3348d2007-12-12 08:45:45 +00001175OptAddrSpace : ADDRSPACE '(' EUINT64VAL ')' { $$=$3; }
1176 | /*empty*/ { $$=0; };
1177
Reid Spencer41dff5e2007-01-26 08:05:27 +00001178/// OptLocalAssign - Value producing statements have an optional assignment
1179/// component.
1180OptLocalAssign : LocalName '=' {
1181 $$ = $1;
1182 CHECK_FOR_ERROR
1183 }
1184 | /*empty*/ {
1185 $$ = 0;
1186 CHECK_FOR_ERROR
1187 };
1188
Chris Lattner15bd0952008-08-29 17:20:18 +00001189LocalNumber : LOCALVAL_ID '=' {
1190 $$ = $1;
1191 CHECK_FOR_ERROR
1192};
1193
1194
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001195GlobalName : GLOBALVAR | ATSTRINGCONSTANT ;
Reid Spencer41dff5e2007-01-26 08:05:27 +00001196
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001197OptGlobalAssign : GlobalAssign
Chris Lattner58af2a12006-02-15 07:22:58 +00001198 | /*empty*/ {
1199 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00001200 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001201 };
1202
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001203GlobalAssign : GlobalName '=' {
1204 $$ = $1;
1205 CHECK_FOR_ERROR
Chris Lattner6cdc6822007-04-26 05:31:05 +00001206 };
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001207
Eric Christopher2a5196f2008-09-24 04:55:49 +00001208GVInternalLinkage
1209 : INTERNAL { $$ = GlobalValue::InternalLinkage; }
1210 | WEAK { $$ = GlobalValue::WeakLinkage; }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001211 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1212 | APPENDING { $$ = GlobalValue::AppendingLinkage; }
Eric Christopher2a5196f2008-09-24 04:55:49 +00001213 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
Dale Johannesenc7071cc2008-05-14 20:13:36 +00001214 | COMMON { $$ = GlobalValue::CommonLinkage; }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001215 ;
1216
1217GVExternalLinkage
1218 : DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1219 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
1220 | EXTERNAL { $$ = GlobalValue::ExternalLinkage; }
1221 ;
1222
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001223GVVisibilityStyle
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +00001224 : /*empty*/ { $$ = GlobalValue::DefaultVisibility; }
1225 | DEFAULT { $$ = GlobalValue::DefaultVisibility; }
1226 | HIDDEN { $$ = GlobalValue::HiddenVisibility; }
1227 | PROTECTED { $$ = GlobalValue::ProtectedVisibility; }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001228 ;
1229
Reid Spencer14310612006-12-31 05:40:51 +00001230FunctionDeclareLinkage
1231 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
Eric Christopher2a5196f2008-09-24 04:55:49 +00001232 | DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
Reid Spencer14310612006-12-31 05:40:51 +00001233 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001234 ;
Eric Christopher2a5196f2008-09-24 04:55:49 +00001235
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001236FunctionDefineLinkage
Reid Spencer14310612006-12-31 05:40:51 +00001237 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1238 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001239 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1240 | WEAK { $$ = GlobalValue::WeakLinkage; }
Eric Christopher2a5196f2008-09-24 04:55:49 +00001241 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
1242 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00001243
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001244AliasLinkage
1245 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1246 | WEAK { $$ = GlobalValue::WeakLinkage; }
1247 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
1248 ;
1249
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00001250OptCallingConv : /*empty*/ { $$ = CallingConv::C; } |
1251 CCC_TOK { $$ = CallingConv::C; } |
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00001252 FASTCC_TOK { $$ = CallingConv::Fast; } |
1253 COLDCC_TOK { $$ = CallingConv::Cold; } |
1254 X86_STDCALLCC_TOK { $$ = CallingConv::X86_StdCall; } |
1255 X86_FASTCALLCC_TOK { $$ = CallingConv::X86_FastCall; } |
1256 CC_TOK EUINT64VAL {
Chris Lattner58af2a12006-02-15 07:22:58 +00001257 if ((unsigned)$2 != $2)
Reid Spencerb5334b02007-02-05 10:18:06 +00001258 GEN_ERROR("Calling conv too large");
Chris Lattner58af2a12006-02-15 07:22:58 +00001259 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001260 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001261 };
1262
Devang Patel05988662008-09-25 21:00:45 +00001263Attribute : ZEROEXT { $$ = Attribute::ZExt; }
1264 | ZEXT { $$ = Attribute::ZExt; }
1265 | SIGNEXT { $$ = Attribute::SExt; }
1266 | SEXT { $$ = Attribute::SExt; }
1267 | INREG { $$ = Attribute::InReg; }
1268 | SRET { $$ = Attribute::StructRet; }
1269 | NOALIAS { $$ = Attribute::NoAlias; }
1270 | BYVAL { $$ = Attribute::ByVal; }
1271 | NEST { $$ = Attribute::Nest; }
Eric Christopher2a5196f2008-09-24 04:55:49 +00001272 | ALIGN EUINT64VAL { $$ =
Devang Patel05988662008-09-25 21:00:45 +00001273 Attribute::constructAlignmentFromInt($2); }
Reid Spencer14310612006-12-31 05:40:51 +00001274 ;
1275
Devang Patel05988662008-09-25 21:00:45 +00001276OptAttributes : /* empty */ { $$ = Attribute::None; }
1277 | OptAttributes Attribute {
Reid Spencer7b5d4662007-04-09 06:16:21 +00001278 $$ = $1 | $2;
Reid Spencer14310612006-12-31 05:40:51 +00001279 }
1280 ;
1281
Devang Patel652203f2008-09-29 20:49:50 +00001282RetAttr : INREG { $$ = Attribute::InReg; }
1283 | ZEROEXT { $$ = Attribute::ZExt; }
1284 | SIGNEXT { $$ = Attribute::SExt; }
1285 ;
1286
1287OptRetAttrs : /* empty */ { $$ = Attribute::None; }
1288 | OptRetAttrs RetAttr {
1289 $$ = $1 | $2;
1290 }
1291 ;
1292
1293
Devang Patel05988662008-09-25 21:00:45 +00001294FuncAttr : NORETURN { $$ = Attribute::NoReturn; }
1295 | NOUNWIND { $$ = Attribute::NoUnwind; }
1296 | INREG { $$ = Attribute::InReg; }
1297 | ZEROEXT { $$ = Attribute::ZExt; }
1298 | SIGNEXT { $$ = Attribute::SExt; }
1299 | READNONE { $$ = Attribute::ReadNone; }
1300 | READONLY { $$ = Attribute::ReadOnly; }
Chris Lattner9fc4da42008-10-08 06:44:45 +00001301 | NOINLINE { $$ = Attribute::NoInline; }
1302 | ALWAYSINLINE { $$ = Attribute::AlwaysInline; }
1303 | OPTSIZE { $$ = Attribute::OptimizeForSize; }
Reid Spencer218ded22007-01-05 17:07:23 +00001304 ;
1305
Devang Patel05988662008-09-25 21:00:45 +00001306OptFuncAttrs : /* empty */ { $$ = Attribute::None; }
Reid Spencer218ded22007-01-05 17:07:23 +00001307 | OptFuncAttrs FuncAttr {
Reid Spencer7b5d4662007-04-09 06:16:21 +00001308 $$ = $1 | $2;
Reid Spencer218ded22007-01-05 17:07:23 +00001309 }
Reid Spencer14310612006-12-31 05:40:51 +00001310 ;
1311
Devang Patel652203f2008-09-29 20:49:50 +00001312
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00001313OptGC : /* empty */ { $$ = 0; }
1314 | GC STRINGCONSTANT {
1315 $$ = $2;
1316 }
1317 ;
1318
Chris Lattner58af2a12006-02-15 07:22:58 +00001319// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1320// a comma before it.
1321OptAlign : /*empty*/ { $$ = 0; } |
1322 ALIGN EUINT64VAL {
1323 $$ = $2;
1324 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencerb5334b02007-02-05 10:18:06 +00001325 GEN_ERROR("Alignment must be a power of two");
Reid Spencer61c83e02006-08-18 08:43:06 +00001326 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001327};
1328OptCAlign : /*empty*/ { $$ = 0; } |
1329 ',' ALIGN EUINT64VAL {
1330 $$ = $3;
1331 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencerb5334b02007-02-05 10:18:06 +00001332 GEN_ERROR("Alignment must be a power of two");
Reid Spencer61c83e02006-08-18 08:43:06 +00001333 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001334};
1335
1336
Christopher Lamba8ed9bf2007-12-11 09:02:08 +00001337
Chris Lattner58af2a12006-02-15 07:22:58 +00001338SectionString : SECTION STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001339 for (unsigned i = 0, e = $2->length(); i != e; ++i)
1340 if ((*$2)[i] == '"' || (*$2)[i] == '\\')
Reid Spencerb5334b02007-02-05 10:18:06 +00001341 GEN_ERROR("Invalid character in section name");
Chris Lattner58af2a12006-02-15 07:22:58 +00001342 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001343 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001344};
1345
1346OptSection : /*empty*/ { $$ = 0; } |
1347 SectionString { $$ = $1; };
1348
1349// GlobalVarAttributes - Used to pass the attributes string on a global. CurGV
1350// is set to be the global we are processing.
1351//
1352GlobalVarAttributes : /* empty */ {} |
1353 ',' GlobalVarAttribute GlobalVarAttributes {};
1354GlobalVarAttribute : SectionString {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001355 CurGV->setSection(*$1);
1356 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001357 CHECK_FOR_ERROR
Eric Christopher2a5196f2008-09-24 04:55:49 +00001358 }
Chris Lattner58af2a12006-02-15 07:22:58 +00001359 | ALIGN EUINT64VAL {
1360 if ($2 != 0 && !isPowerOf2_32($2))
Reid Spencerb5334b02007-02-05 10:18:06 +00001361 GEN_ERROR("Alignment must be a power of two");
Chris Lattner58af2a12006-02-15 07:22:58 +00001362 CurGV->setAlignment($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001363 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001364 };
1365
1366//===----------------------------------------------------------------------===//
1367// Types includes all predefined types... except void, because it can only be
Eric Christopher2a5196f2008-09-24 04:55:49 +00001368// used in specific contexts (function returning void for example).
Chris Lattner58af2a12006-02-15 07:22:58 +00001369
1370// Derived types are added later...
1371//
Dale Johannesen320fc8a2007-08-03 01:03:46 +00001372PrimType : INTTYPE | FLOAT | DOUBLE | PPC_FP128 | FP128 | X86_FP80 | LABEL ;
Reid Spencer14310612006-12-31 05:40:51 +00001373
Eric Christopher2a5196f2008-09-24 04:55:49 +00001374Types
Reid Spencer14310612006-12-31 05:40:51 +00001375 : OPAQUE {
Reid Spencera132e042006-12-03 05:46:11 +00001376 $$ = new PATypeHolder(OpaqueType::get());
Reid Spencer61c83e02006-08-18 08:43:06 +00001377 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001378 }
1379 | PrimType {
Reid Spencera132e042006-12-03 05:46:11 +00001380 $$ = new PATypeHolder($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001381 CHECK_FOR_ERROR
Reid Spencer14310612006-12-31 05:40:51 +00001382 }
Christopher Lambbf3348d2007-12-12 08:45:45 +00001383 | Types OptAddrSpace '*' { // Pointer type?
Reid Spencer14310612006-12-31 05:40:51 +00001384 if (*$1 == Type::LabelTy)
1385 GEN_ERROR("Cannot form a pointer to a basic block");
Christopher Lambbf3348d2007-12-12 08:45:45 +00001386 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1, $2)));
Christopher Lamba8ed9bf2007-12-11 09:02:08 +00001387 delete $1;
1388 CHECK_FOR_ERROR
1389 }
Reid Spencer14310612006-12-31 05:40:51 +00001390 | SymbolicValueRef { // Named types are also simple types...
1391 const Type* tmp = getTypeVal($1);
1392 CHECK_FOR_ERROR
1393 $$ = new PATypeHolder(tmp);
1394 }
1395 | '\\' EUINT64VAL { // Type UpReference
Reid Spencerb5334b02007-02-05 10:18:06 +00001396 if ($2 > (uint64_t)~0U) GEN_ERROR("Value out of range");
Chris Lattner58af2a12006-02-15 07:22:58 +00001397 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
1398 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Reid Spencera132e042006-12-03 05:46:11 +00001399 $$ = new PATypeHolder(OT);
Chris Lattner58af2a12006-02-15 07:22:58 +00001400 UR_OUT("New Upreference!\n");
Reid Spencer61c83e02006-08-18 08:43:06 +00001401 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001402 }
Reid Spencer218ded22007-01-05 17:07:23 +00001403 | Types '(' ArgTypeListI ')' OptFuncAttrs {
Duncan Sandsdc024672007-11-27 13:23:08 +00001404 // Allow but ignore attributes on function types; this permits auto-upgrade.
1405 // FIXME: remove in LLVM 3.0.
Chris Lattnera925a142008-04-23 05:37:08 +00001406 const Type *RetTy = *$1;
1407 if (!FunctionType::isValidReturnType(RetTy))
1408 GEN_ERROR("Invalid result type for LLVM function");
Eric Christopher2a5196f2008-09-24 04:55:49 +00001409
Chris Lattner58af2a12006-02-15 07:22:58 +00001410 std::vector<const Type*> Params;
Reid Spencer7b5d4662007-04-09 06:16:21 +00001411 TypeWithAttrsList::iterator I = $3->begin(), E = $3->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00001412 for (; I != E; ++I ) {
Reid Spencer66728ef2007-03-20 01:13:36 +00001413 const Type *Ty = I->Ty->get();
Reid Spencer66728ef2007-03-20 01:13:36 +00001414 Params.push_back(Ty);
Reid Spencer14310612006-12-31 05:40:51 +00001415 }
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001416
Chris Lattner58af2a12006-02-15 07:22:58 +00001417 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1418 if (isVarArg) Params.pop_back();
1419
Anton Korobeynikov05e5a742007-12-03 21:01:29 +00001420 for (unsigned i = 0; i != Params.size(); ++i)
1421 if (!(Params[i]->isFirstClassType() || isa<OpaqueType>(Params[i])))
1422 GEN_ERROR("Function arguments must be value types!");
1423
1424 CHECK_FOR_ERROR
1425
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001426 FunctionType *FT = FunctionType::get(RetTy, Params, isVarArg);
Reid Spencer14310612006-12-31 05:40:51 +00001427 delete $1; // Delete the return type handle
Eric Christopher2a5196f2008-09-24 04:55:49 +00001428 $$ = new PATypeHolder(HandleUpRefs(FT));
Nuno Lopes8a88a372008-10-05 16:49:34 +00001429
1430 // Delete the argument list
1431 for (I = $3->begin() ; I != E; ++I ) {
1432 delete I->Ty;
1433 }
1434 delete $3;
1435
Reid Spencer61c83e02006-08-18 08:43:06 +00001436 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001437 }
Reid Spencer218ded22007-01-05 17:07:23 +00001438 | VOID '(' ArgTypeListI ')' OptFuncAttrs {
Duncan Sandsdc024672007-11-27 13:23:08 +00001439 // Allow but ignore attributes on function types; this permits auto-upgrade.
1440 // FIXME: remove in LLVM 3.0.
Reid Spencer14310612006-12-31 05:40:51 +00001441 std::vector<const Type*> Params;
Reid Spencer7b5d4662007-04-09 06:16:21 +00001442 TypeWithAttrsList::iterator I = $3->begin(), E = $3->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00001443 for ( ; I != E; ++I ) {
Reid Spencer66728ef2007-03-20 01:13:36 +00001444 const Type* Ty = I->Ty->get();
Reid Spencer66728ef2007-03-20 01:13:36 +00001445 Params.push_back(Ty);
Reid Spencer14310612006-12-31 05:40:51 +00001446 }
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001447
Reid Spencer14310612006-12-31 05:40:51 +00001448 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1449 if (isVarArg) Params.pop_back();
1450
Anton Korobeynikov05e5a742007-12-03 21:01:29 +00001451 for (unsigned i = 0; i != Params.size(); ++i)
1452 if (!(Params[i]->isFirstClassType() || isa<OpaqueType>(Params[i])))
1453 GEN_ERROR("Function arguments must be value types!");
1454
1455 CHECK_FOR_ERROR
1456
Duncan Sandsdc024672007-11-27 13:23:08 +00001457 FunctionType *FT = FunctionType::get($1, Params, isVarArg);
Eric Christopher2a5196f2008-09-24 04:55:49 +00001458 $$ = new PATypeHolder(HandleUpRefs(FT));
Nuno Lopes8a88a372008-10-05 16:49:34 +00001459
1460 // Delete the argument list
1461 for (I = $3->begin() ; I != E; ++I ) {
1462 delete I->Ty;
1463 }
1464 delete $3;
1465
Reid Spencer14310612006-12-31 05:40:51 +00001466 CHECK_FOR_ERROR
1467 }
1468
1469 | '[' EUINT64VAL 'x' Types ']' { // Sized array type?
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001470 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, $2)));
Reid Spencera132e042006-12-03 05:46:11 +00001471 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00001472 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001473 }
Chris Lattner32980692007-02-19 07:44:24 +00001474 | '<' EUINT64VAL 'x' Types '>' { // Vector type?
Reid Spencera132e042006-12-03 05:46:11 +00001475 const llvm::Type* ElemTy = $4->get();
1476 if ((unsigned)$2 != $2)
1477 GEN_ERROR("Unsigned result not equal to signed result");
Chris Lattner42a75512007-01-15 02:27:26 +00001478 if (!ElemTy->isFloatingPoint() && !ElemTy->isInteger())
Reid Spencer9d6565a2007-02-15 02:26:10 +00001479 GEN_ERROR("Element type of a VectorType must be primitive");
Reid Spencer9d6565a2007-02-15 02:26:10 +00001480 $$ = new PATypeHolder(HandleUpRefs(VectorType::get(*$4, (unsigned)$2)));
Reid Spencera132e042006-12-03 05:46:11 +00001481 delete $4;
1482 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001483 }
1484 | '{' TypeListI '}' { // Structure type?
1485 std::vector<const Type*> Elements;
Reid Spencera132e042006-12-03 05:46:11 +00001486 for (std::list<llvm::PATypeHolder>::iterator I = $2->begin(),
Chris Lattner58af2a12006-02-15 07:22:58 +00001487 E = $2->end(); I != E; ++I)
Reid Spencera132e042006-12-03 05:46:11 +00001488 Elements.push_back(*I);
Chris Lattner58af2a12006-02-15 07:22:58 +00001489
Reid Spencera132e042006-12-03 05:46:11 +00001490 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattner58af2a12006-02-15 07:22:58 +00001491 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001492 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001493 }
1494 | '{' '}' { // Empty structure type?
Reid Spencera132e042006-12-03 05:46:11 +00001495 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Reid Spencer61c83e02006-08-18 08:43:06 +00001496 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001497 }
Andrew Lenharth6353e052006-12-08 18:07:09 +00001498 | '<' '{' TypeListI '}' '>' {
1499 std::vector<const Type*> Elements;
1500 for (std::list<llvm::PATypeHolder>::iterator I = $3->begin(),
1501 E = $3->end(); I != E; ++I)
1502 Elements.push_back(*I);
1503
1504 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements, true)));
1505 delete $3;
1506 CHECK_FOR_ERROR
1507 }
1508 | '<' '{' '}' '>' { // Empty structure type?
1509 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>(), true));
1510 CHECK_FOR_ERROR
1511 }
Reid Spencer14310612006-12-31 05:40:51 +00001512 ;
1513
Eric Christopher2a5196f2008-09-24 04:55:49 +00001514ArgType
Devang Patel05988662008-09-25 21:00:45 +00001515 : Types OptAttributes {
Duncan Sandsdc024672007-11-27 13:23:08 +00001516 // Allow but ignore attributes on function types; this permits auto-upgrade.
1517 // FIXME: remove in LLVM 3.0.
Eric Christopher2a5196f2008-09-24 04:55:49 +00001518 $$.Ty = $1;
Devang Patel05988662008-09-25 21:00:45 +00001519 $$.Attrs = Attribute::None;
Reid Spencer14310612006-12-31 05:40:51 +00001520 }
1521 ;
1522
Reid Spencer218ded22007-01-05 17:07:23 +00001523ResultTypes
1524 : Types {
Reid Spencer14310612006-12-31 05:40:51 +00001525 if (!UpRefs.empty())
1526 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Devang Patel20071732008-02-23 01:17:37 +00001527 if (!(*$1)->isFirstClassType() && !isa<StructType>($1->get()))
Reid Spencerb5334b02007-02-05 10:18:06 +00001528 GEN_ERROR("LLVM functions cannot return aggregate types");
Reid Spencer218ded22007-01-05 17:07:23 +00001529 $$ = $1;
Reid Spencer14310612006-12-31 05:40:51 +00001530 }
Reid Spencer218ded22007-01-05 17:07:23 +00001531 | VOID {
1532 $$ = new PATypeHolder(Type::VoidTy);
Reid Spencer14310612006-12-31 05:40:51 +00001533 }
1534 ;
1535
1536ArgTypeList : ArgType {
1537 $$ = new TypeWithAttrsList();
1538 $$->push_back($1);
1539 CHECK_FOR_ERROR
1540 }
1541 | ArgTypeList ',' ArgType {
1542 ($$=$1)->push_back($3);
1543 CHECK_FOR_ERROR
1544 }
1545 ;
1546
Eric Christopher2a5196f2008-09-24 04:55:49 +00001547ArgTypeListI
Reid Spencer14310612006-12-31 05:40:51 +00001548 : ArgTypeList
1549 | ArgTypeList ',' DOTDOTDOT {
1550 $$=$1;
Devang Patel05988662008-09-25 21:00:45 +00001551 TypeWithAttrs TWA; TWA.Attrs = Attribute::None;
Reid Spencer14310612006-12-31 05:40:51 +00001552 TWA.Ty = new PATypeHolder(Type::VoidTy);
1553 $$->push_back(TWA);
1554 CHECK_FOR_ERROR
1555 }
1556 | DOTDOTDOT {
1557 $$ = new TypeWithAttrsList;
Devang Patel05988662008-09-25 21:00:45 +00001558 TypeWithAttrs TWA; TWA.Attrs = Attribute::None;
Reid Spencer14310612006-12-31 05:40:51 +00001559 TWA.Ty = new PATypeHolder(Type::VoidTy);
1560 $$->push_back(TWA);
1561 CHECK_FOR_ERROR
1562 }
1563 | /*empty*/ {
1564 $$ = new TypeWithAttrsList();
Reid Spencer61c83e02006-08-18 08:43:06 +00001565 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001566 };
1567
Eric Christopher2a5196f2008-09-24 04:55:49 +00001568// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner58af2a12006-02-15 07:22:58 +00001569// declaration type lists
1570//
Reid Spencer14310612006-12-31 05:40:51 +00001571TypeListI : Types {
Reid Spencera132e042006-12-03 05:46:11 +00001572 $$ = new std::list<PATypeHolder>();
Eric Christopher2a5196f2008-09-24 04:55:49 +00001573 $$->push_back(*$1);
Reid Spencer66728ef2007-03-20 01:13:36 +00001574 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001575 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001576 }
Reid Spencer14310612006-12-31 05:40:51 +00001577 | TypeListI ',' Types {
Eric Christopher2a5196f2008-09-24 04:55:49 +00001578 ($$=$1)->push_back(*$3);
Reid Spencer66728ef2007-03-20 01:13:36 +00001579 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001580 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001581 };
1582
Chris Lattner58af2a12006-02-15 07:22:58 +00001583// ConstVal - The various declarations that go into the constant pool. This
1584// production is used ONLY to represent constants that show up AFTER a 'const',
1585// 'constant' or 'global' token at global scope. Constants that can be inlined
1586// into other expressions (such as integers and constexprs) are handled by the
1587// ResolvedVal, ValueRef and ConstValueRef productions.
1588//
1589ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencer14310612006-12-31 05:40:51 +00001590 if (!UpRefs.empty())
1591 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001592 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001593 if (ATy == 0)
Eric Christopher2a5196f2008-09-24 04:55:49 +00001594 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001595 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001596 const Type *ETy = ATy->getElementType();
Dan Gohman180c1692008-06-23 18:43:26 +00001597 uint64_t NumElements = ATy->getNumElements();
Chris Lattner58af2a12006-02-15 07:22:58 +00001598
1599 // Verify that we have the correct size...
Mon P Wang28873102008-06-25 08:15:39 +00001600 if (NumElements != uint64_t(-1) && NumElements != $3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001601 GEN_ERROR("Type mismatch: constant sized array initialized with " +
Eric Christopher2a5196f2008-09-24 04:55:49 +00001602 utostr($3->size()) + " arguments, but has size of " +
Mon P Wang28873102008-06-25 08:15:39 +00001603 utostr(NumElements) + "");
Chris Lattner58af2a12006-02-15 07:22:58 +00001604
1605 // Verify all elements are correct type!
1606 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencera132e042006-12-03 05:46:11 +00001607 if (ETy != (*$3)[i]->getType())
Eric Christopher2a5196f2008-09-24 04:55:49 +00001608 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00001609 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencera132e042006-12-03 05:46:11 +00001610 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner58af2a12006-02-15 07:22:58 +00001611 }
1612
Reid Spencera132e042006-12-03 05:46:11 +00001613 $$ = ConstantArray::get(ATy, *$3);
1614 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001615 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001616 }
1617 | Types '[' ']' {
Reid Spencer14310612006-12-31 05:40:51 +00001618 if (!UpRefs.empty())
1619 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001620 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001621 if (ATy == 0)
Eric Christopher2a5196f2008-09-24 04:55:49 +00001622 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001623 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001624
Dan Gohman180c1692008-06-23 18:43:26 +00001625 uint64_t NumElements = ATy->getNumElements();
Eric Christopher2a5196f2008-09-24 04:55:49 +00001626 if (NumElements != uint64_t(-1) && NumElements != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001627 GEN_ERROR("Type mismatch: constant sized array initialized with 0"
Mon P Wang28873102008-06-25 08:15:39 +00001628 " arguments, but has size of " + utostr(NumElements) +"");
Reid Spencera132e042006-12-03 05:46:11 +00001629 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
1630 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001631 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001632 }
1633 | Types 'c' STRINGCONSTANT {
Reid Spencer14310612006-12-31 05:40:51 +00001634 if (!UpRefs.empty())
1635 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001636 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001637 if (ATy == 0)
Eric Christopher2a5196f2008-09-24 04:55:49 +00001638 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001639 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001640
Dan Gohman180c1692008-06-23 18:43:26 +00001641 uint64_t NumElements = ATy->getNumElements();
Chris Lattner58af2a12006-02-15 07:22:58 +00001642 const Type *ETy = ATy->getElementType();
Mon P Wang28873102008-06-25 08:15:39 +00001643 if (NumElements != uint64_t(-1) && NumElements != $3->length())
Eric Christopher2a5196f2008-09-24 04:55:49 +00001644 GEN_ERROR("Can't build string constant of size " +
Mon P Wang28873102008-06-25 08:15:39 +00001645 utostr($3->length()) +
1646 " when array has size " + utostr(NumElements) + "");
Chris Lattner58af2a12006-02-15 07:22:58 +00001647 std::vector<Constant*> Vals;
Reid Spencer14310612006-12-31 05:40:51 +00001648 if (ETy == Type::Int8Ty) {
Mon P Wang28873102008-06-25 08:15:39 +00001649 for (uint64_t i = 0; i < $3->length(); ++i)
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001650 Vals.push_back(ConstantInt::get(ETy, (*$3)[i]));
Chris Lattner58af2a12006-02-15 07:22:58 +00001651 } else {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001652 delete $3;
Reid Spencerb5334b02007-02-05 10:18:06 +00001653 GEN_ERROR("Cannot build string arrays of non byte sized elements");
Chris Lattner58af2a12006-02-15 07:22:58 +00001654 }
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001655 delete $3;
Reid Spencera132e042006-12-03 05:46:11 +00001656 $$ = ConstantArray::get(ATy, Vals);
1657 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001658 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001659 }
1660 | Types '<' ConstVector '>' { // Nonempty unsized arr
Reid Spencer14310612006-12-31 05:40:51 +00001661 if (!UpRefs.empty())
1662 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer9d6565a2007-02-15 02:26:10 +00001663 const VectorType *PTy = dyn_cast<VectorType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001664 if (PTy == 0)
Eric Christopher2a5196f2008-09-24 04:55:49 +00001665 GEN_ERROR("Cannot make packed constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001666 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001667 const Type *ETy = PTy->getElementType();
Dan Gohman180c1692008-06-23 18:43:26 +00001668 unsigned NumElements = PTy->getNumElements();
Chris Lattner58af2a12006-02-15 07:22:58 +00001669
1670 // Verify that we have the correct size...
Mon P Wang28873102008-06-25 08:15:39 +00001671 if (NumElements != unsigned(-1) && NumElements != (unsigned)$3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001672 GEN_ERROR("Type mismatch: constant sized packed initialized with " +
Eric Christopher2a5196f2008-09-24 04:55:49 +00001673 utostr($3->size()) + " arguments, but has size of " +
Mon P Wang28873102008-06-25 08:15:39 +00001674 utostr(NumElements) + "");
Chris Lattner58af2a12006-02-15 07:22:58 +00001675
1676 // Verify all elements are correct type!
1677 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencera132e042006-12-03 05:46:11 +00001678 if (ETy != (*$3)[i]->getType())
Eric Christopher2a5196f2008-09-24 04:55:49 +00001679 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00001680 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencera132e042006-12-03 05:46:11 +00001681 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner58af2a12006-02-15 07:22:58 +00001682 }
1683
Reid Spencer9d6565a2007-02-15 02:26:10 +00001684 $$ = ConstantVector::get(PTy, *$3);
Reid Spencera132e042006-12-03 05:46:11 +00001685 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001686 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001687 }
1688 | Types '{' ConstVector '}' {
Reid Spencera132e042006-12-03 05:46:11 +00001689 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001690 if (STy == 0)
Eric Christopher2a5196f2008-09-24 04:55:49 +00001691 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001692 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001693
1694 if ($3->size() != STy->getNumContainedTypes())
Reid Spencerb5334b02007-02-05 10:18:06 +00001695 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattner58af2a12006-02-15 07:22:58 +00001696
1697 // Check to ensure that constants are compatible with the type initializer!
1698 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Reid Spencera132e042006-12-03 05:46:11 +00001699 if ((*$3)[i]->getType() != STy->getElementType(i))
Reid Spencer61c83e02006-08-18 08:43:06 +00001700 GEN_ERROR("Expected type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00001701 STy->getElementType(i)->getDescription() +
1702 "' for element #" + utostr(i) +
Reid Spencerb5334b02007-02-05 10:18:06 +00001703 " of structure initializer");
Chris Lattner58af2a12006-02-15 07:22:58 +00001704
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001705 // Check to ensure that Type is not packed
1706 if (STy->isPacked())
Chris Lattner6cdc6822007-04-26 05:31:05 +00001707 GEN_ERROR("Unpacked Initializer to vector type '" +
1708 STy->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001709
Reid Spencera132e042006-12-03 05:46:11 +00001710 $$ = ConstantStruct::get(STy, *$3);
1711 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001712 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001713 }
1714 | Types '{' '}' {
Reid Spencer14310612006-12-31 05:40:51 +00001715 if (!UpRefs.empty())
1716 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001717 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001718 if (STy == 0)
Eric Christopher2a5196f2008-09-24 04:55:49 +00001719 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001720 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001721
1722 if (STy->getNumContainedTypes() != 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00001723 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattner58af2a12006-02-15 07:22:58 +00001724
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001725 // Check to ensure that Type is not packed
1726 if (STy->isPacked())
Chris Lattner6cdc6822007-04-26 05:31:05 +00001727 GEN_ERROR("Unpacked Initializer to vector type '" +
1728 STy->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001729
1730 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1731 delete $1;
1732 CHECK_FOR_ERROR
1733 }
1734 | Types '<' '{' ConstVector '}' '>' {
1735 const StructType *STy = dyn_cast<StructType>($1->get());
1736 if (STy == 0)
Eric Christopher2a5196f2008-09-24 04:55:49 +00001737 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001738 (*$1)->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001739
1740 if ($4->size() != STy->getNumContainedTypes())
Reid Spencerb5334b02007-02-05 10:18:06 +00001741 GEN_ERROR("Illegal number of initializers for structure type");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001742
1743 // Check to ensure that constants are compatible with the type initializer!
1744 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1745 if ((*$4)[i]->getType() != STy->getElementType(i))
1746 GEN_ERROR("Expected type '" +
1747 STy->getElementType(i)->getDescription() +
1748 "' for element #" + utostr(i) +
Reid Spencerb5334b02007-02-05 10:18:06 +00001749 " of structure initializer");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001750
1751 // Check to ensure that Type is packed
1752 if (!STy->isPacked())
Eric Christopher2a5196f2008-09-24 04:55:49 +00001753 GEN_ERROR("Vector initializer to non-vector type '" +
Chris Lattner32980692007-02-19 07:44:24 +00001754 STy->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001755
1756 $$ = ConstantStruct::get(STy, *$4);
1757 delete $1; delete $4;
1758 CHECK_FOR_ERROR
1759 }
1760 | Types '<' '{' '}' '>' {
1761 if (!UpRefs.empty())
1762 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1763 const StructType *STy = dyn_cast<StructType>($1->get());
1764 if (STy == 0)
Eric Christopher2a5196f2008-09-24 04:55:49 +00001765 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001766 (*$1)->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001767
1768 if (STy->getNumContainedTypes() != 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00001769 GEN_ERROR("Illegal number of initializers for structure type");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001770
1771 // Check to ensure that Type is packed
1772 if (!STy->isPacked())
Eric Christopher2a5196f2008-09-24 04:55:49 +00001773 GEN_ERROR("Vector initializer to non-vector type '" +
Chris Lattner32980692007-02-19 07:44:24 +00001774 STy->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001775
Reid Spencera132e042006-12-03 05:46:11 +00001776 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1777 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001778 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001779 }
1780 | Types NULL_TOK {
Reid Spencer14310612006-12-31 05:40:51 +00001781 if (!UpRefs.empty())
1782 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001783 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001784 if (PTy == 0)
Eric Christopher2a5196f2008-09-24 04:55:49 +00001785 GEN_ERROR("Cannot make null pointer constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001786 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001787
Reid Spencera132e042006-12-03 05:46:11 +00001788 $$ = ConstantPointerNull::get(PTy);
1789 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001790 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001791 }
1792 | Types UNDEF {
Reid Spencer14310612006-12-31 05:40:51 +00001793 if (!UpRefs.empty())
1794 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001795 $$ = UndefValue::get($1->get());
1796 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001797 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001798 }
1799 | Types SymbolicValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00001800 if (!UpRefs.empty())
1801 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001802 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001803 if (Ty == 0)
Devang Patel5a970972008-02-19 22:27:01 +00001804 GEN_ERROR("Global const reference must be a pointer type " + (*$1)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00001805
1806 // ConstExprs can exist in the body of a function, thus creating
1807 // GlobalValues whenever they refer to a variable. Because we are in
Reid Spencer93c40032007-03-19 18:40:50 +00001808 // the context of a function, getExistingVal will search the functions
Chris Lattner58af2a12006-02-15 07:22:58 +00001809 // symbol table instead of the module symbol table for the global symbol,
1810 // which throws things all off. To get around this, we just tell
Reid Spencer93c40032007-03-19 18:40:50 +00001811 // getExistingVal that we are at global scope here.
Chris Lattner58af2a12006-02-15 07:22:58 +00001812 //
1813 Function *SavedCurFn = CurFun.CurrentFunction;
1814 CurFun.CurrentFunction = 0;
1815
Reid Spencer93c40032007-03-19 18:40:50 +00001816 Value *V = getExistingVal(Ty, $2);
Reid Spencer5b7e7532006-09-28 19:28:24 +00001817 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001818
1819 CurFun.CurrentFunction = SavedCurFn;
1820
1821 // If this is an initializer for a constant pointer, which is referencing a
1822 // (currently) undefined variable, create a stub now that shall be replaced
1823 // in the future with the right type of variable.
1824 //
1825 if (V == 0) {
Reid Spencera9720f52007-02-05 17:04:00 +00001826 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001827 const PointerType *PT = cast<PointerType>(Ty);
1828
1829 // First check to see if the forward references value is already created!
1830 PerModuleInfo::GlobalRefsType::iterator I =
1831 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Eric Christopher2a5196f2008-09-24 04:55:49 +00001832
Chris Lattner58af2a12006-02-15 07:22:58 +00001833 if (I != CurModule.GlobalRefs.end()) {
1834 V = I->second; // Placeholder already exists, use it...
1835 $2.destroy();
1836 } else {
1837 std::string Name;
Reid Spencer41dff5e2007-01-26 08:05:27 +00001838 if ($2.Type == ValID::GlobalName)
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001839 Name = $2.getName();
Reid Spencer41dff5e2007-01-26 08:05:27 +00001840 else if ($2.Type != ValID::GlobalID)
1841 GEN_ERROR("Invalid reference to global");
Chris Lattner58af2a12006-02-15 07:22:58 +00001842
1843 // Create the forward referenced global.
1844 GlobalValue *GV;
Eric Christopher2a5196f2008-09-24 04:55:49 +00001845 if (const FunctionType *FTy =
Chris Lattner58af2a12006-02-15 07:22:58 +00001846 dyn_cast<FunctionType>(PT->getElementType())) {
Gabor Greife64d2482008-04-06 23:07:54 +00001847 GV = Function::Create(FTy, GlobalValue::ExternalWeakLinkage, Name,
1848 CurModule.CurrentModule);
Chris Lattner58af2a12006-02-15 07:22:58 +00001849 } else {
1850 GV = new GlobalVariable(PT->getElementType(), false,
Chris Lattner6cdc6822007-04-26 05:31:05 +00001851 GlobalValue::ExternalWeakLinkage, 0,
Chris Lattner58af2a12006-02-15 07:22:58 +00001852 Name, CurModule.CurrentModule);
1853 }
1854
1855 // Keep track of the fact that we have a forward ref to recycle it
1856 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1857 V = GV;
1858 }
1859 }
1860
Reid Spencera132e042006-12-03 05:46:11 +00001861 $$ = cast<GlobalValue>(V);
1862 delete $1; // Free the type handle
Reid Spencer61c83e02006-08-18 08:43:06 +00001863 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001864 }
1865 | Types ConstExpr {
Reid Spencer14310612006-12-31 05:40:51 +00001866 if (!UpRefs.empty())
1867 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001868 if ($1->get() != $2->getType())
Eric Christopher2a5196f2008-09-24 04:55:49 +00001869 GEN_ERROR("Mismatched types for constant expression: " +
Reid Spencere68853b2007-01-04 00:06:14 +00001870 (*$1)->getDescription() + " and " + $2->getType()->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00001871 $$ = $2;
Reid Spencera132e042006-12-03 05:46:11 +00001872 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001873 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001874 }
1875 | Types ZEROINITIALIZER {
Reid Spencer14310612006-12-31 05:40:51 +00001876 if (!UpRefs.empty())
1877 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001878 const Type *Ty = $1->get();
Chris Lattner58af2a12006-02-15 07:22:58 +00001879 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
Reid Spencerb5334b02007-02-05 10:18:06 +00001880 GEN_ERROR("Cannot create a null initialized value of this type");
Reid Spencera132e042006-12-03 05:46:11 +00001881 $$ = Constant::getNullValue(Ty);
1882 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001883 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00001884 }
Chris Lattner740e7092008-10-15 06:16:57 +00001885 | Types ESINT64VAL { // integral constants
1886 if (IntegerType *IT = dyn_cast<IntegerType>($1->get())) {
1887 if (!ConstantInt::isValueValidForType(IT, $2))
1888 GEN_ERROR("Constant value doesn't fit in type");
1889 $$ = ConstantInt::get(IT, $2, true);
1890 } else {
1891 GEN_ERROR("integer constant must have integer type");
1892 }
1893 delete $1;
Reid Spencer38c91a92007-02-28 02:24:54 +00001894 CHECK_FOR_ERROR
1895 }
Chris Lattner740e7092008-10-15 06:16:57 +00001896 | Types ESAPINTVAL { // arbitrary precision integer constants
1897 if (IntegerType *IT = dyn_cast<IntegerType>($1->get())) {
1898 if ($2->getBitWidth() > IT->getBitWidth())
1899 GEN_ERROR("Constant value does not fit in type");
1900 $2->sextOrTrunc(IT->getBitWidth());
1901 $$ = ConstantInt::get(*$2);
1902 } else {
1903 GEN_ERROR("integer constant must have integer type");
Reid Spencer10794272007-03-01 19:41:47 +00001904 }
Chris Lattner740e7092008-10-15 06:16:57 +00001905 delete $1;
Reid Spencer38c91a92007-02-28 02:24:54 +00001906 delete $2;
Reid Spencere4d87aa2006-12-23 06:05:41 +00001907 CHECK_FOR_ERROR
1908 }
Chris Lattner740e7092008-10-15 06:16:57 +00001909 | Types EUINT64VAL { // integral constants
1910 if (IntegerType *IT = dyn_cast<IntegerType>($1->get())) {
1911 if (!ConstantInt::isValueValidForType(IT, $2))
1912 GEN_ERROR("Constant value doesn't fit in type");
1913 $$ = ConstantInt::get(IT, $2, false);
1914 } else {
1915 GEN_ERROR("integer constant must have integer type");
Eric Christopher2a5196f2008-09-24 04:55:49 +00001916 }
Chris Lattner740e7092008-10-15 06:16:57 +00001917 delete $1;
Reid Spencere4d87aa2006-12-23 06:05:41 +00001918 CHECK_FOR_ERROR
1919 }
Chris Lattner740e7092008-10-15 06:16:57 +00001920 | Types EUAPINTVAL { // arbitrary precision integer constants
1921 if (IntegerType *IT = dyn_cast<IntegerType>($1->get())) {
1922 if ($2->getBitWidth() > IT->getBitWidth())
1923 GEN_ERROR("Constant value does not fit in type");
1924 $2->zextOrTrunc(IT->getBitWidth());
1925 $$ = ConstantInt::get(*$2);
1926 } else {
1927 GEN_ERROR("integer constant must have integer type");
1928 }
1929
1930 delete $2;
1931 delete $1;
1932 CHECK_FOR_ERROR
1933 }
1934 | Types TRUETOK { // Boolean constants
1935 if ($1->get() != Type::Int1Ty)
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001936 GEN_ERROR("Constant true must have type i1");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001937 $$ = ConstantInt::getTrue();
Chris Lattner740e7092008-10-15 06:16:57 +00001938 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001939 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001940 }
Chris Lattner740e7092008-10-15 06:16:57 +00001941 | Types FALSETOK { // Boolean constants
1942 if ($1->get() != Type::Int1Ty)
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001943 GEN_ERROR("Constant false must have type i1");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001944 $$ = ConstantInt::getFalse();
Chris Lattner740e7092008-10-15 06:16:57 +00001945 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001946 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001947 }
Chris Lattner740e7092008-10-15 06:16:57 +00001948 | Types FPVAL { // Floating point constants
1949 if (!ConstantFP::isValueValidForType($1->get(), *$2))
Reid Spencerb5334b02007-02-05 10:18:06 +00001950 GEN_ERROR("Floating point constant invalid for type");
Chris Lattner740e7092008-10-15 06:16:57 +00001951
Eric Christopher2a5196f2008-09-24 04:55:49 +00001952 // Lexer has no type info, so builds all float and double FP constants
Dale Johannesenc72cd7e2007-09-11 18:33:39 +00001953 // as double. Fix this here. Long double is done right.
Chris Lattner740e7092008-10-15 06:16:57 +00001954 if (&$2->getSemantics()==&APFloat::IEEEdouble && $1->get()==Type::FloatTy) {
Dale Johannesen236bbd42008-10-09 23:01:34 +00001955 bool ignored;
1956 $2->convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven,
1957 &ignored);
1958 }
Chris Lattnerd8eb63f2008-04-20 00:41:19 +00001959 $$ = ConstantFP::get(*$2);
Chris Lattner740e7092008-10-15 06:16:57 +00001960 delete $1;
Dale Johannesencdd509a2007-09-07 21:07:57 +00001961 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001962 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001963 };
1964
1965
Reid Spencer3da59db2006-11-27 01:05:10 +00001966ConstExpr: CastOps '(' ConstVal TO Types ')' {
Reid Spencer14310612006-12-31 05:40:51 +00001967 if (!UpRefs.empty())
1968 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001969 Constant *Val = $3;
Reid Spencerb0fcf8f2007-01-17 02:48:45 +00001970 const Type *DestTy = $5->get();
1971 if (!CastInst::castIsValid($1, $3, DestTy))
1972 GEN_ERROR("invalid cast opcode for cast from '" +
1973 Val->getType()->getDescription() + "' to '" +
Eric Christopher2a5196f2008-09-24 04:55:49 +00001974 DestTy->getDescription() + "'");
Reid Spencerb0fcf8f2007-01-17 02:48:45 +00001975 $$ = ConstantExpr::getCast($1, $3, DestTy);
Reid Spencera132e042006-12-03 05:46:11 +00001976 delete $5;
Chris Lattner58af2a12006-02-15 07:22:58 +00001977 }
1978 | GETELEMENTPTR '(' ConstVal IndexList ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001979 if (!isa<PointerType>($3->getType()))
Reid Spencerb5334b02007-02-05 10:18:06 +00001980 GEN_ERROR("GetElementPtr requires a pointer operand");
Chris Lattner58af2a12006-02-15 07:22:58 +00001981
Reid Spencera132e042006-12-03 05:46:11 +00001982 const Type *IdxTy =
Dan Gohman041e2eb2008-05-15 19:50:34 +00001983 GetElementPtrInst::getIndexedType($3->getType(), $4->begin(), $4->end());
Reid Spencera132e042006-12-03 05:46:11 +00001984 if (!IdxTy)
Reid Spencerb5334b02007-02-05 10:18:06 +00001985 GEN_ERROR("Index list invalid for constant getelementptr");
Reid Spencera132e042006-12-03 05:46:11 +00001986
Chris Lattnerf7469af2007-01-31 04:44:08 +00001987 SmallVector<Constant*, 8> IdxVec;
Reid Spencera132e042006-12-03 05:46:11 +00001988 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1989 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattner58af2a12006-02-15 07:22:58 +00001990 IdxVec.push_back(C);
1991 else
Reid Spencerb5334b02007-02-05 10:18:06 +00001992 GEN_ERROR("Indices to constant getelementptr must be constants");
Chris Lattner58af2a12006-02-15 07:22:58 +00001993
1994 delete $4;
1995
Chris Lattnerf7469af2007-01-31 04:44:08 +00001996 $$ = ConstantExpr::getGetElementPtr($3, &IdxVec[0], IdxVec.size());
Reid Spencer61c83e02006-08-18 08:43:06 +00001997 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001998 }
1999 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer4fe16d62007-01-11 18:21:29 +00002000 if ($3->getType() != Type::Int1Ty)
Reid Spencerb5334b02007-02-05 10:18:06 +00002001 GEN_ERROR("Select condition must be of boolean type");
Reid Spencera132e042006-12-03 05:46:11 +00002002 if ($5->getType() != $7->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00002003 GEN_ERROR("Select operand types must match");
Reid Spencera132e042006-12-03 05:46:11 +00002004 $$ = ConstantExpr::getSelect($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00002005 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002006 }
2007 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00002008 if ($3->getType() != $5->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00002009 GEN_ERROR("Binary operator types must match");
Reid Spencer1628cec2006-10-26 06:15:43 +00002010 CHECK_FOR_ERROR;
Reid Spencer9eef56f2006-12-05 19:16:11 +00002011 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattner58af2a12006-02-15 07:22:58 +00002012 }
2013 | LogicalOps '(' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00002014 if ($3->getType() != $5->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00002015 GEN_ERROR("Logical operator types must match");
Chris Lattner42a75512007-01-15 02:27:26 +00002016 if (!$3->getType()->isInteger()) {
Eric Christopher2a5196f2008-09-24 04:55:49 +00002017 if (!isa<VectorType>($3->getType()) ||
Reid Spencer9d6565a2007-02-15 02:26:10 +00002018 !cast<VectorType>($3->getType())->getElementType()->isInteger())
Reid Spencerb5334b02007-02-05 10:18:06 +00002019 GEN_ERROR("Logical operator requires integral operands");
Chris Lattner58af2a12006-02-15 07:22:58 +00002020 }
Reid Spencera132e042006-12-03 05:46:11 +00002021 $$ = ConstantExpr::get($1, $3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002022 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002023 }
Reid Spencer4012e832006-12-04 05:24:24 +00002024 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
2025 if ($4->getType() != $6->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00002026 GEN_ERROR("icmp operand types must match");
Reid Spencer4012e832006-12-04 05:24:24 +00002027 $$ = ConstantExpr::getICmp($2, $4, $6);
Reid Spencera132e042006-12-03 05:46:11 +00002028 }
Reid Spencer4012e832006-12-04 05:24:24 +00002029 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
2030 if ($4->getType() != $6->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00002031 GEN_ERROR("fcmp operand types must match");
Reid Spencer4012e832006-12-04 05:24:24 +00002032 $$ = ConstantExpr::getFCmp($2, $4, $6);
Reid Spencera132e042006-12-03 05:46:11 +00002033 }
Nate Begemanac80ade2008-05-12 19:01:56 +00002034 | VICMP IPredicates '(' ConstVal ',' ConstVal ')' {
2035 if ($4->getType() != $6->getType())
2036 GEN_ERROR("vicmp operand types must match");
2037 $$ = ConstantExpr::getVICmp($2, $4, $6);
2038 }
2039 | VFCMP FPredicates '(' ConstVal ',' ConstVal ')' {
2040 if ($4->getType() != $6->getType())
2041 GEN_ERROR("vfcmp operand types must match");
2042 $$ = ConstantExpr::getVFCmp($2, $4, $6);
2043 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002044 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00002045 if (!ExtractElementInst::isValidOperands($3, $5))
Reid Spencerb5334b02007-02-05 10:18:06 +00002046 GEN_ERROR("Invalid extractelement operands");
Reid Spencera132e042006-12-03 05:46:11 +00002047 $$ = ConstantExpr::getExtractElement($3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002048 CHECK_FOR_ERROR
Chris Lattnerd25db202006-04-08 03:55:17 +00002049 }
2050 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00002051 if (!InsertElementInst::isValidOperands($3, $5, $7))
Reid Spencerb5334b02007-02-05 10:18:06 +00002052 GEN_ERROR("Invalid insertelement operands");
Reid Spencera132e042006-12-03 05:46:11 +00002053 $$ = ConstantExpr::getInsertElement($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00002054 CHECK_FOR_ERROR
Chris Lattnerd25db202006-04-08 03:55:17 +00002055 }
2056 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00002057 if (!ShuffleVectorInst::isValidOperands($3, $5, $7))
Reid Spencerb5334b02007-02-05 10:18:06 +00002058 GEN_ERROR("Invalid shufflevector operands");
Reid Spencera132e042006-12-03 05:46:11 +00002059 $$ = ConstantExpr::getShuffleVector($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00002060 CHECK_FOR_ERROR
Dan Gohmane4977cf2008-05-23 01:55:30 +00002061 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002062 | EXTRACTVALUE '(' ConstVal ConstantIndexList ')' {
Dan Gohmane4977cf2008-05-23 01:55:30 +00002063 if (!isa<StructType>($3->getType()) && !isa<ArrayType>($3->getType()))
2064 GEN_ERROR("ExtractValue requires an aggregate operand");
2065
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002066 $$ = ConstantExpr::getExtractValue($3, &(*$4)[0], $4->size());
Dan Gohmane4977cf2008-05-23 01:55:30 +00002067 delete $4;
Dan Gohmane4977cf2008-05-23 01:55:30 +00002068 CHECK_FOR_ERROR
2069 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002070 | INSERTVALUE '(' ConstVal ',' ConstVal ConstantIndexList ')' {
Dan Gohmane4977cf2008-05-23 01:55:30 +00002071 if (!isa<StructType>($3->getType()) && !isa<ArrayType>($3->getType()))
2072 GEN_ERROR("InsertValue requires an aggregate operand");
2073
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002074 $$ = ConstantExpr::getInsertValue($3, $5, &(*$6)[0], $6->size());
Dan Gohmane4977cf2008-05-23 01:55:30 +00002075 delete $6;
Dan Gohmane4977cf2008-05-23 01:55:30 +00002076 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002077 };
2078
Chris Lattnerd25db202006-04-08 03:55:17 +00002079
Chris Lattner58af2a12006-02-15 07:22:58 +00002080// ConstVector - A list of comma separated constants.
2081ConstVector : ConstVector ',' ConstVal {
2082 ($$ = $1)->push_back($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002083 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002084 }
2085 | ConstVal {
Reid Spencera132e042006-12-03 05:46:11 +00002086 $$ = new std::vector<Constant*>();
Chris Lattner58af2a12006-02-15 07:22:58 +00002087 $$->push_back($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002088 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002089 };
2090
2091
2092// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
2093GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
2094
Eric Christopher2a5196f2008-09-24 04:55:49 +00002095// ThreadLocal
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002096ThreadLocal : THREAD_LOCAL { $$ = true; } | { $$ = false; };
2097
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002098// AliaseeRef - Match either GlobalValue or bitcast to GlobalValue.
2099AliaseeRef : ResultTypes SymbolicValueRef {
2100 const Type* VTy = $1->get();
2101 Value *V = getVal(VTy, $2);
Chris Lattner0275cff2007-08-06 21:00:46 +00002102 CHECK_FOR_ERROR
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002103 GlobalValue* Aliasee = dyn_cast<GlobalValue>(V);
2104 if (!Aliasee)
2105 GEN_ERROR("Aliases can be created only to global values");
2106
2107 $$ = Aliasee;
2108 CHECK_FOR_ERROR
2109 delete $1;
2110 }
2111 | BITCAST '(' AliaseeRef TO Types ')' {
2112 Constant *Val = $3;
2113 const Type *DestTy = $5->get();
2114 if (!CastInst::castIsValid($1, $3, DestTy))
2115 GEN_ERROR("invalid cast opcode for cast from '" +
2116 Val->getType()->getDescription() + "' to '" +
2117 DestTy->getDescription() + "'");
Eric Christopher2a5196f2008-09-24 04:55:49 +00002118
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002119 $$ = ConstantExpr::getCast($1, $3, DestTy);
2120 CHECK_FOR_ERROR
2121 delete $5;
2122 };
Chris Lattner58af2a12006-02-15 07:22:58 +00002123
2124//===----------------------------------------------------------------------===//
2125// Rules to match Modules
2126//===----------------------------------------------------------------------===//
2127
2128// Module rule: Capture the result of parsing the whole file into a result
2129// variable...
2130//
Eric Christopher2a5196f2008-09-24 04:55:49 +00002131Module
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002132 : DefinitionList {
2133 $$ = ParserResult = CurModule.CurrentModule;
2134 CurModule.ModuleDone();
2135 CHECK_FOR_ERROR;
2136 }
2137 | /*empty*/ {
2138 $$ = ParserResult = CurModule.CurrentModule;
2139 CurModule.ModuleDone();
2140 CHECK_FOR_ERROR;
2141 }
2142 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00002143
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002144DefinitionList
2145 : Definition
2146 | DefinitionList Definition
2147 ;
2148
Eric Christopher2a5196f2008-09-24 04:55:49 +00002149Definition
Jeff Cohen361c3ef2007-01-21 19:19:31 +00002150 : DEFINE { CurFun.isDeclare = false; } Function {
Chris Lattner58af2a12006-02-15 07:22:58 +00002151 CurFun.FunctionDone();
Reid Spencer61c83e02006-08-18 08:43:06 +00002152 CHECK_FOR_ERROR
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002153 }
2154 | DECLARE { CurFun.isDeclare = true; } FunctionProto {
Reid Spencer61c83e02006-08-18 08:43:06 +00002155 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002156 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002157 | MODULE ASM_TOK AsmBlock {
Reid Spencer61c83e02006-08-18 08:43:06 +00002158 CHECK_FOR_ERROR
Eric Christopher2a5196f2008-09-24 04:55:49 +00002159 }
Reid Spencer41dff5e2007-01-26 08:05:27 +00002160 | OptLocalAssign TYPE Types {
Reid Spencer14310612006-12-31 05:40:51 +00002161 if (!UpRefs.empty())
2162 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00002163 // Eagerly resolve types. This is not an optimization, this is a
2164 // requirement that is due to the fact that we could have this:
2165 //
2166 // %list = type { %list * }
2167 // %list = type { %list * } ; repeated type decl
2168 //
2169 // If types are not resolved eagerly, then the two types will not be
2170 // determined to be the same type!
2171 //
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002172 ResolveTypeTo($1, *$3);
Chris Lattner58af2a12006-02-15 07:22:58 +00002173
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002174 if (!setTypeName(*$3, $1) && !$1) {
Reid Spencer5b7e7532006-09-28 19:28:24 +00002175 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002176 // If this is a named type that is not a redefinition, add it to the slot
2177 // table.
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002178 CurModule.Types.push_back(*$3);
Chris Lattner58af2a12006-02-15 07:22:58 +00002179 }
Reid Spencera132e042006-12-03 05:46:11 +00002180
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002181 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002182 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002183 }
Reid Spencer41dff5e2007-01-26 08:05:27 +00002184 | OptLocalAssign TYPE VOID {
Reid Spencer14310612006-12-31 05:40:51 +00002185 ResolveTypeTo($1, $3);
2186
2187 if (!setTypeName($3, $1) && !$1) {
2188 CHECK_FOR_ERROR
2189 // If this is a named type that is not a redefinition, add it to the slot
2190 // table.
2191 CurModule.Types.push_back($3);
2192 }
2193 CHECK_FOR_ERROR
2194 }
Eric Christopher2a5196f2008-09-24 04:55:49 +00002195 | OptGlobalAssign GVVisibilityStyle ThreadLocal GlobalType ConstVal
2196 OptAddrSpace {
Reid Spencer41dff5e2007-01-26 08:05:27 +00002197 /* "Externally Visible" Linkage */
Eric Christopher2a5196f2008-09-24 04:55:49 +00002198 if ($5 == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002199 GEN_ERROR("Global value initializer is not a constant");
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002200 CurGV = ParseGlobalVariable($1, GlobalValue::ExternalLinkage,
Christopher Lambbf3348d2007-12-12 08:45:45 +00002201 $2, $4, $5->getType(), $5, $3, $6);
Christopher Lamba8ed9bf2007-12-11 09:02:08 +00002202 CHECK_FOR_ERROR
2203 } GlobalVarAttributes {
2204 CurGV = 0;
2205 }
Chris Lattner6cdc6822007-04-26 05:31:05 +00002206 | OptGlobalAssign GVInternalLinkage GVVisibilityStyle ThreadLocal GlobalType
Christopher Lambbf3348d2007-12-12 08:45:45 +00002207 ConstVal OptAddrSpace {
Eric Christopher2a5196f2008-09-24 04:55:49 +00002208 if ($6 == 0)
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002209 GEN_ERROR("Global value initializer is not a constant");
Christopher Lambbf3348d2007-12-12 08:45:45 +00002210 CurGV = ParseGlobalVariable($1, $2, $3, $5, $6->getType(), $6, $4, $7);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002211 CHECK_FOR_ERROR
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002212 } GlobalVarAttributes {
2213 CurGV = 0;
2214 }
Chris Lattner6cdc6822007-04-26 05:31:05 +00002215 | OptGlobalAssign GVExternalLinkage GVVisibilityStyle ThreadLocal GlobalType
Christopher Lambbf3348d2007-12-12 08:45:45 +00002216 Types OptAddrSpace {
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002217 if (!UpRefs.empty())
2218 GEN_ERROR("Invalid upreference in type: " + (*$6)->getDescription());
Christopher Lambbf3348d2007-12-12 08:45:45 +00002219 CurGV = ParseGlobalVariable($1, $2, $3, $5, *$6, 0, $4, $7);
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002220 CHECK_FOR_ERROR
2221 delete $6;
Reid Spencer5b7e7532006-09-28 19:28:24 +00002222 } GlobalVarAttributes {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002223 CurGV = 0;
2224 CHECK_FOR_ERROR
2225 }
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002226 | OptGlobalAssign GVVisibilityStyle ALIAS AliasLinkage AliaseeRef {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002227 std::string Name;
2228 if ($1) {
2229 Name = *$1;
2230 delete $1;
2231 }
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00002232 if (Name.empty())
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002233 GEN_ERROR("Alias name cannot be empty");
Eric Christopher2a5196f2008-09-24 04:55:49 +00002234
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002235 Constant* Aliasee = $5;
2236 if (Aliasee == 0)
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002237 GEN_ERROR(std::string("Invalid aliasee for alias: ") + Name);
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002238
2239 GlobalAlias* GA = new GlobalAlias(Aliasee->getType(), $4, Name, Aliasee,
2240 CurModule.CurrentModule);
2241 GA->setVisibility($2);
2242 InsertValue(GA, CurModule.Values);
Eric Christopher2a5196f2008-09-24 04:55:49 +00002243
2244
Chris Lattner569f7372007-09-10 23:24:14 +00002245 // If there was a forward reference of this alias, resolve it now.
Eric Christopher2a5196f2008-09-24 04:55:49 +00002246
Chris Lattner569f7372007-09-10 23:24:14 +00002247 ValID ID;
2248 if (!Name.empty())
2249 ID = ValID::createGlobalName(Name);
2250 else
2251 ID = ValID::createGlobalID(CurModule.Values.size()-1);
Eric Christopher2a5196f2008-09-24 04:55:49 +00002252
Chris Lattner569f7372007-09-10 23:24:14 +00002253 if (GlobalValue *FWGV =
2254 CurModule.GetForwardRefForGlobal(GA->getType(), ID)) {
2255 // Replace uses of the fwdref with the actual alias.
2256 FWGV->replaceAllUsesWith(GA);
2257 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(FWGV))
2258 GV->eraseFromParent();
2259 else
2260 cast<Function>(FWGV)->eraseFromParent();
2261 }
2262 ID.destroy();
Eric Christopher2a5196f2008-09-24 04:55:49 +00002263
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00002264 CHECK_FOR_ERROR
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00002265 }
Eric Christopher2a5196f2008-09-24 04:55:49 +00002266 | TARGET TargetDefinition {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002267 CHECK_FOR_ERROR
2268 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002269 | DEPLIBS '=' LibrariesDefinition {
Reid Spencer61c83e02006-08-18 08:43:06 +00002270 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002271 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002272 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00002273
2274
2275AsmBlock : STRINGCONSTANT {
2276 const std::string &AsmSoFar = CurModule.CurrentModule->getModuleInlineAsm();
Chris Lattner58af2a12006-02-15 07:22:58 +00002277 if (AsmSoFar.empty())
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002278 CurModule.CurrentModule->setModuleInlineAsm(*$1);
Chris Lattner58af2a12006-02-15 07:22:58 +00002279 else
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002280 CurModule.CurrentModule->setModuleInlineAsm(AsmSoFar+"\n"+*$1);
2281 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002282 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002283};
2284
Reid Spencer41dff5e2007-01-26 08:05:27 +00002285TargetDefinition : TRIPLE '=' STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002286 CurModule.CurrentModule->setTargetTriple(*$3);
2287 delete $3;
John Criswell2f6a8b12006-10-24 19:09:48 +00002288 }
Chris Lattner1ae022f2006-10-22 06:08:13 +00002289 | DATALAYOUT '=' STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002290 CurModule.CurrentModule->setDataLayout(*$3);
2291 delete $3;
Owen Anderson1dc69692006-10-18 02:21:48 +00002292 };
Chris Lattner58af2a12006-02-15 07:22:58 +00002293
2294LibrariesDefinition : '[' LibList ']';
2295
2296LibList : LibList ',' STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002297 CurModule.CurrentModule->addLibrary(*$3);
2298 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002299 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002300 }
2301 | STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002302 CurModule.CurrentModule->addLibrary(*$1);
2303 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002304 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002305 }
2306 | /* empty: end of list */ {
Reid Spencer61c83e02006-08-18 08:43:06 +00002307 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002308 }
2309 ;
2310
2311//===----------------------------------------------------------------------===//
2312// Rules to match Function Headers
2313//===----------------------------------------------------------------------===//
2314
Devang Patel05988662008-09-25 21:00:45 +00002315ArgListH : ArgListH ',' Types OptAttributes OptLocalName {
Reid Spencer14310612006-12-31 05:40:51 +00002316 if (!UpRefs.empty())
2317 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002318 if (!(*$3)->isFirstClassType())
2319 GEN_ERROR("Argument types must be first-class");
Reid Spencer14310612006-12-31 05:40:51 +00002320 ArgListEntry E; E.Attrs = $4; E.Ty = $3; E.Name = $5;
Chris Lattner58af2a12006-02-15 07:22:58 +00002321 $$ = $1;
Reid Spencer14310612006-12-31 05:40:51 +00002322 $1->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002323 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002324 }
Devang Patel05988662008-09-25 21:00:45 +00002325 | Types OptAttributes OptLocalName {
Reid Spencer14310612006-12-31 05:40:51 +00002326 if (!UpRefs.empty())
2327 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002328 if (!(*$1)->isFirstClassType())
2329 GEN_ERROR("Argument types must be first-class");
Reid Spencer14310612006-12-31 05:40:51 +00002330 ArgListEntry E; E.Attrs = $2; E.Ty = $1; E.Name = $3;
2331 $$ = new ArgListType;
2332 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002333 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002334 };
2335
2336ArgList : ArgListH {
2337 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002338 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002339 }
2340 | ArgListH ',' DOTDOTDOT {
2341 $$ = $1;
Reid Spencer14310612006-12-31 05:40:51 +00002342 struct ArgListEntry E;
2343 E.Ty = new PATypeHolder(Type::VoidTy);
2344 E.Name = 0;
Devang Patel05988662008-09-25 21:00:45 +00002345 E.Attrs = Attribute::None;
Reid Spencer14310612006-12-31 05:40:51 +00002346 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002347 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002348 }
2349 | DOTDOTDOT {
Reid Spencer14310612006-12-31 05:40:51 +00002350 $$ = new ArgListType;
2351 struct ArgListEntry E;
2352 E.Ty = new PATypeHolder(Type::VoidTy);
2353 E.Name = 0;
Devang Patel05988662008-09-25 21:00:45 +00002354 E.Attrs = Attribute::None;
Reid Spencer14310612006-12-31 05:40:51 +00002355 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002356 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002357 }
2358 | /* empty */ {
2359 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00002360 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002361 };
2362
Devang Patel652203f2008-09-29 20:49:50 +00002363FunctionHeaderH : OptCallingConv OptRetAttrs ResultTypes GlobalName '(' ArgList ')'
Devang Patel2c9c3e72008-09-26 23:51:19 +00002364 OptFuncAttrs OptSection OptAlign OptGC {
Devang Patel652203f2008-09-29 20:49:50 +00002365 std::string FunctionName(*$4);
2366 delete $4; // Free strdup'd memory!
Eric Christopher2a5196f2008-09-24 04:55:49 +00002367
Reid Spencer8c8a2dc2007-01-02 21:54:12 +00002368 // Check the function result for abstractness if this is a define. We should
2369 // have no abstract types at this point
Devang Patel652203f2008-09-29 20:49:50 +00002370 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved($3))
2371 GEN_ERROR("Reference to abstract result: "+ $3->get()->getDescription());
Reid Spencer8c8a2dc2007-01-02 21:54:12 +00002372
Devang Patel652203f2008-09-29 20:49:50 +00002373 if (!FunctionType::isValidReturnType(*$3))
Chris Lattnera925a142008-04-23 05:37:08 +00002374 GEN_ERROR("Invalid result type for LLVM function");
Eric Christopher2a5196f2008-09-24 04:55:49 +00002375
Chris Lattner58af2a12006-02-15 07:22:58 +00002376 std::vector<const Type*> ParamTypeList;
Devang Patel05988662008-09-25 21:00:45 +00002377 SmallVector<AttributeWithIndex, 8> Attrs;
Devang Patel19c87462008-09-26 22:53:05 +00002378 //FIXME : In 3.0, stop accepting zext, sext and inreg as optional function
2379 //attributes.
Devang Patel652203f2008-09-29 20:49:50 +00002380 Attributes RetAttrs = $2;
2381 if ($8 != Attribute::None) {
2382 if ($8 & Attribute::ZExt) {
Devang Patel19c87462008-09-26 22:53:05 +00002383 RetAttrs = RetAttrs | Attribute::ZExt;
Devang Patel652203f2008-09-29 20:49:50 +00002384 $8 = $8 ^ Attribute::ZExt;
Devang Patel19c87462008-09-26 22:53:05 +00002385 }
Devang Patel652203f2008-09-29 20:49:50 +00002386 if ($8 & Attribute::SExt) {
Devang Patel19c87462008-09-26 22:53:05 +00002387 RetAttrs = RetAttrs | Attribute::SExt;
Devang Patel652203f2008-09-29 20:49:50 +00002388 $8 = $8 ^ Attribute::SExt;
Devang Patel19c87462008-09-26 22:53:05 +00002389 }
Devang Patel652203f2008-09-29 20:49:50 +00002390 if ($8 & Attribute::InReg) {
Devang Patel19c87462008-09-26 22:53:05 +00002391 RetAttrs = RetAttrs | Attribute::InReg;
Devang Patel652203f2008-09-29 20:49:50 +00002392 $8 = $8 ^ Attribute::InReg;
Devang Patel19c87462008-09-26 22:53:05 +00002393 }
Devang Patel19c87462008-09-26 22:53:05 +00002394 }
Devang Patel652203f2008-09-29 20:49:50 +00002395 if (RetAttrs != Attribute::None)
2396 Attrs.push_back(AttributeWithIndex::get(0, RetAttrs));
2397 if ($6) { // If there are arguments...
Reid Spencer7b5d4662007-04-09 06:16:21 +00002398 unsigned index = 1;
Devang Patel652203f2008-09-29 20:49:50 +00002399 for (ArgListType::iterator I = $6->begin(); I != $6->end(); ++I, ++index) {
Reid Spencer14310612006-12-31 05:40:51 +00002400 const Type* Ty = I->Ty->get();
Reid Spencer8c8a2dc2007-01-02 21:54:12 +00002401 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved(I->Ty))
2402 GEN_ERROR("Reference to abstract argument: " + Ty->getDescription());
Reid Spencer14310612006-12-31 05:40:51 +00002403 ParamTypeList.push_back(Ty);
Devang Patel05988662008-09-25 21:00:45 +00002404 if (Ty != Type::VoidTy && I->Attrs != Attribute::None)
2405 Attrs.push_back(AttributeWithIndex::get(index, I->Attrs));
Reid Spencer14310612006-12-31 05:40:51 +00002406 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002407 }
Devang Patel652203f2008-09-29 20:49:50 +00002408 if ($8 != Attribute::None)
2409 Attrs.push_back(AttributeWithIndex::get(~0, $8));
Chris Lattner58af2a12006-02-15 07:22:58 +00002410
2411 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
2412 if (isVarArg) ParamTypeList.pop_back();
2413
Devang Patel05988662008-09-25 21:00:45 +00002414 AttrListPtr PAL;
Christopher Lamb5c104242007-04-22 20:09:11 +00002415 if (!Attrs.empty())
Devang Patel05988662008-09-25 21:00:45 +00002416 PAL = AttrListPtr::get(Attrs.begin(), Attrs.end());
Reid Spencer7b5d4662007-04-09 06:16:21 +00002417
Devang Patel652203f2008-09-29 20:49:50 +00002418 FunctionType *FT = FunctionType::get(*$3, ParamTypeList, isVarArg);
Christopher Lamb4374f8e2007-12-17 01:17:35 +00002419 const PointerType *PFT = PointerType::getUnqual(FT);
Devang Patel652203f2008-09-29 20:49:50 +00002420 delete $3;
Chris Lattner58af2a12006-02-15 07:22:58 +00002421
2422 ValID ID;
2423 if (!FunctionName.empty()) {
Reid Spencer41dff5e2007-01-26 08:05:27 +00002424 ID = ValID::createGlobalName((char*)FunctionName.c_str());
Chris Lattner58af2a12006-02-15 07:22:58 +00002425 } else {
Reid Spencer93c40032007-03-19 18:40:50 +00002426 ID = ValID::createGlobalID(CurModule.Values.size());
Chris Lattner58af2a12006-02-15 07:22:58 +00002427 }
2428
2429 Function *Fn = 0;
2430 // See if this function was forward referenced. If so, recycle the object.
2431 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
Eric Christopher2a5196f2008-09-24 04:55:49 +00002432 // Move the function to the end of the list, from whereever it was
Chris Lattner58af2a12006-02-15 07:22:58 +00002433 // previously inserted.
2434 Fn = cast<Function>(FWRef);
Devang Patel05988662008-09-25 21:00:45 +00002435 assert(Fn->getAttributes().isEmpty() &&
Chris Lattner58d74912008-03-12 17:45:29 +00002436 "Forward reference has parameter attributes!");
Chris Lattner58af2a12006-02-15 07:22:58 +00002437 CurModule.CurrentModule->getFunctionList().remove(Fn);
2438 CurModule.CurrentModule->getFunctionList().push_back(Fn);
2439 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
Reid Spenceref9b9a72007-02-05 20:47:22 +00002440 (Fn = CurModule.CurrentModule->getFunction(FunctionName))) {
Duncan Sandsdc024672007-11-27 13:23:08 +00002441 if (Fn->getFunctionType() != FT ) {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002442 // The existing function doesn't have the same type. This is an overload
2443 // error.
2444 GEN_ERROR("Overload of function '" + FunctionName + "' not permitted.");
Devang Patel05988662008-09-25 21:00:45 +00002445 } else if (Fn->getAttributes() != PAL) {
Duncan Sandsdc024672007-11-27 13:23:08 +00002446 // The existing function doesn't have the same parameter attributes.
2447 // This is an overload error.
2448 GEN_ERROR("Overload of function '" + FunctionName + "' not permitted.");
Reid Spenceref9b9a72007-02-05 20:47:22 +00002449 } else if (!CurFun.isDeclare && !Fn->isDeclaration()) {
Chris Lattner6cdc6822007-04-26 05:31:05 +00002450 // Neither the existing or the current function is a declaration and they
2451 // have the same name and same type. Clearly this is a redefinition.
2452 GEN_ERROR("Redefinition of function '" + FunctionName + "'");
Duncan Sandsdc024672007-11-27 13:23:08 +00002453 } else if (Fn->isDeclaration()) {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002454 // Make sure to strip off any argument names so we can't get conflicts.
Chris Lattner58af2a12006-02-15 07:22:58 +00002455 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
2456 AI != AE; ++AI)
2457 AI->setName("");
Reid Spenceref9b9a72007-02-05 20:47:22 +00002458 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002459 } else { // Not already defined?
Gabor Greife64d2482008-04-06 23:07:54 +00002460 Fn = Function::Create(FT, GlobalValue::ExternalWeakLinkage, FunctionName,
2461 CurModule.CurrentModule);
Chris Lattner58af2a12006-02-15 07:22:58 +00002462 InsertValue(Fn, CurModule.Values);
2463 }
2464
Nuno Lopes9e9631d2008-10-03 15:45:58 +00002465 ID.destroy();
Chris Lattner58af2a12006-02-15 07:22:58 +00002466 CurFun.FunctionStart(Fn);
Anton Korobeynikov93c2b372006-09-17 13:06:18 +00002467
2468 if (CurFun.isDeclare) {
2469 // If we have declaration, always overwrite linkage. This will allow us to
2470 // correctly handle cases, when pointer to function is passed as argument to
2471 // another function.
2472 Fn->setLinkage(CurFun.Linkage);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002473 Fn->setVisibility(CurFun.Visibility);
Anton Korobeynikov93c2b372006-09-17 13:06:18 +00002474 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002475 Fn->setCallingConv($1);
Devang Patel05988662008-09-25 21:00:45 +00002476 Fn->setAttributes(PAL);
Devang Patel652203f2008-09-29 20:49:50 +00002477 Fn->setAlignment($10);
2478 if ($9) {
2479 Fn->setSection(*$9);
2480 delete $9;
Chris Lattner58af2a12006-02-15 07:22:58 +00002481 }
Devang Patel652203f2008-09-29 20:49:50 +00002482 if ($11) {
2483 Fn->setGC($11->c_str());
2484 delete $11;
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00002485 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002486
2487 // Add all of the arguments we parsed to the function...
Devang Patel652203f2008-09-29 20:49:50 +00002488 if ($6) { // Is null if empty...
Chris Lattner58af2a12006-02-15 07:22:58 +00002489 if (isVarArg) { // Nuke the last entry
Devang Patel652203f2008-09-29 20:49:50 +00002490 assert($6->back().Ty->get() == Type::VoidTy && $6->back().Name == 0 &&
Reid Spencera9720f52007-02-05 17:04:00 +00002491 "Not a varargs marker!");
Devang Patel652203f2008-09-29 20:49:50 +00002492 delete $6->back().Ty;
2493 $6->pop_back(); // Delete the last entry
Chris Lattner58af2a12006-02-15 07:22:58 +00002494 }
2495 Function::arg_iterator ArgIt = Fn->arg_begin();
Reid Spenceref9b9a72007-02-05 20:47:22 +00002496 Function::arg_iterator ArgEnd = Fn->arg_end();
Reid Spencer14310612006-12-31 05:40:51 +00002497 unsigned Idx = 1;
Devang Patel652203f2008-09-29 20:49:50 +00002498 for (ArgListType::iterator I = $6->begin();
2499 I != $6->end() && ArgIt != ArgEnd; ++I, ++ArgIt) {
Reid Spencer14310612006-12-31 05:40:51 +00002500 delete I->Ty; // Delete the typeholder...
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002501 setValueName(ArgIt, I->Name); // Insert arg into symtab...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002502 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002503 InsertValue(ArgIt);
Reid Spencer14310612006-12-31 05:40:51 +00002504 Idx++;
Chris Lattner58af2a12006-02-15 07:22:58 +00002505 }
Reid Spencera132e042006-12-03 05:46:11 +00002506
Devang Patel652203f2008-09-29 20:49:50 +00002507 delete $6; // We're now done with the argument list
Chris Lattner58af2a12006-02-15 07:22:58 +00002508 }
Reid Spencer61c83e02006-08-18 08:43:06 +00002509 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002510};
2511
2512BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
2513
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002514FunctionHeader : FunctionDefineLinkage GVVisibilityStyle FunctionHeaderH BEGIN {
Chris Lattner58af2a12006-02-15 07:22:58 +00002515 $$ = CurFun.CurrentFunction;
2516
2517 // Make sure that we keep track of the linkage type even if there was a
2518 // previous "declare".
2519 $$->setLinkage($1);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002520 $$->setVisibility($2);
Chris Lattner58af2a12006-02-15 07:22:58 +00002521};
2522
2523END : ENDTOK | '}'; // Allow end of '}' to end a function
2524
2525Function : BasicBlockList END {
2526 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002527 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002528};
2529
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002530FunctionProto : FunctionDeclareLinkage GVVisibilityStyle FunctionHeaderH {
Reid Spencer14310612006-12-31 05:40:51 +00002531 CurFun.CurrentFunction->setLinkage($1);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002532 CurFun.CurrentFunction->setVisibility($2);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002533 $$ = CurFun.CurrentFunction;
2534 CurFun.FunctionDone();
2535 CHECK_FOR_ERROR
2536 };
Chris Lattner58af2a12006-02-15 07:22:58 +00002537
2538//===----------------------------------------------------------------------===//
2539// Rules to match Basic Blocks
2540//===----------------------------------------------------------------------===//
2541
2542OptSideEffect : /* empty */ {
2543 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002544 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002545 }
2546 | SIDEEFFECT {
2547 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002548 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002549 };
2550
2551ConstValueRef : ESINT64VAL { // A reference to a direct constant
2552 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002553 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002554 }
2555 | EUINT64VAL {
2556 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002557 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002558 }
Chris Lattner1913b942008-07-11 00:30:39 +00002559 | ESAPINTVAL { // arbitrary precision integer constants
2560 $$ = ValID::create(*$1, true);
2561 delete $1;
2562 CHECK_FOR_ERROR
Eric Christopher2a5196f2008-09-24 04:55:49 +00002563 }
Chris Lattner1913b942008-07-11 00:30:39 +00002564 | EUAPINTVAL { // arbitrary precision integer constants
2565 $$ = ValID::create(*$1, false);
2566 delete $1;
2567 CHECK_FOR_ERROR
2568 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002569 | FPVAL { // Perhaps it's an FP constant?
2570 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002571 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002572 }
2573 | TRUETOK {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00002574 $$ = ValID::create(ConstantInt::getTrue());
Reid Spencer61c83e02006-08-18 08:43:06 +00002575 CHECK_FOR_ERROR
Eric Christopher2a5196f2008-09-24 04:55:49 +00002576 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002577 | FALSETOK {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00002578 $$ = ValID::create(ConstantInt::getFalse());
Reid Spencer61c83e02006-08-18 08:43:06 +00002579 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002580 }
2581 | NULL_TOK {
2582 $$ = ValID::createNull();
Reid Spencer61c83e02006-08-18 08:43:06 +00002583 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002584 }
2585 | UNDEF {
2586 $$ = ValID::createUndef();
Reid Spencer61c83e02006-08-18 08:43:06 +00002587 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002588 }
2589 | ZEROINITIALIZER { // A vector zero constant.
2590 $$ = ValID::createZeroInit();
Reid Spencer61c83e02006-08-18 08:43:06 +00002591 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002592 }
2593 | '<' ConstVector '>' { // Nonempty unsized packed vector
Reid Spencera132e042006-12-03 05:46:11 +00002594 const Type *ETy = (*$2)[0]->getType();
Eric Christopher2a5196f2008-09-24 04:55:49 +00002595 unsigned NumElements = $2->size();
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002596
2597 if (!ETy->isInteger() && !ETy->isFloatingPoint())
2598 GEN_ERROR("Invalid vector element type: " + ETy->getDescription());
Eric Christopher2a5196f2008-09-24 04:55:49 +00002599
Reid Spencer9d6565a2007-02-15 02:26:10 +00002600 VectorType* pt = VectorType::get(ETy, NumElements);
Dan Gohmanf910eaa2008-06-09 14:45:02 +00002601 PATypeHolder* PTy = new PATypeHolder(HandleUpRefs(pt));
Eric Christopher2a5196f2008-09-24 04:55:49 +00002602
Chris Lattner58af2a12006-02-15 07:22:58 +00002603 // Verify all elements are correct type!
2604 for (unsigned i = 0; i < $2->size(); i++) {
Reid Spencera132e042006-12-03 05:46:11 +00002605 if (ETy != (*$2)[i]->getType())
Eric Christopher2a5196f2008-09-24 04:55:49 +00002606 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00002607 ETy->getDescription() +"' as required!\nIt is of type '" +
Reid Spencera132e042006-12-03 05:46:11 +00002608 (*$2)[i]->getType()->getDescription() + "'.");
Chris Lattner58af2a12006-02-15 07:22:58 +00002609 }
2610
Reid Spencer9d6565a2007-02-15 02:26:10 +00002611 $$ = ValID::create(ConstantVector::get(pt, *$2));
Chris Lattner58af2a12006-02-15 07:22:58 +00002612 delete PTy; delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002613 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002614 }
Dan Gohmanf910eaa2008-06-09 14:45:02 +00002615 | '[' ConstVector ']' { // Nonempty unsized arr
2616 const Type *ETy = (*$2)[0]->getType();
Eric Christopher2a5196f2008-09-24 04:55:49 +00002617 uint64_t NumElements = $2->size();
Dan Gohmanf910eaa2008-06-09 14:45:02 +00002618
2619 if (!ETy->isFirstClassType())
2620 GEN_ERROR("Invalid array element type: " + ETy->getDescription());
2621
2622 ArrayType *ATy = ArrayType::get(ETy, NumElements);
2623 PATypeHolder* PTy = new PATypeHolder(HandleUpRefs(ATy));
2624
2625 // Verify all elements are correct type!
2626 for (unsigned i = 0; i < $2->size(); i++) {
2627 if (ETy != (*$2)[i]->getType())
Eric Christopher2a5196f2008-09-24 04:55:49 +00002628 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Dan Gohmanf910eaa2008-06-09 14:45:02 +00002629 ETy->getDescription() +"' as required!\nIt is of type '"+
2630 (*$2)[i]->getType()->getDescription() + "'.");
2631 }
2632
2633 $$ = ValID::create(ConstantArray::get(ATy, *$2));
2634 delete PTy; delete $2;
2635 CHECK_FOR_ERROR
2636 }
2637 | '[' ']' {
Dan Gohman180c1692008-06-23 18:43:26 +00002638 // Use undef instead of an array because it's inconvenient to determine
2639 // the element type at this point, there being no elements to examine.
Dan Gohmanf910eaa2008-06-09 14:45:02 +00002640 $$ = ValID::createUndef();
2641 CHECK_FOR_ERROR
2642 }
2643 | 'c' STRINGCONSTANT {
Dan Gohman180c1692008-06-23 18:43:26 +00002644 uint64_t NumElements = $2->length();
Dan Gohmanf910eaa2008-06-09 14:45:02 +00002645 const Type *ETy = Type::Int8Ty;
2646
2647 ArrayType *ATy = ArrayType::get(ETy, NumElements);
2648
2649 std::vector<Constant*> Vals;
2650 for (unsigned i = 0; i < $2->length(); ++i)
2651 Vals.push_back(ConstantInt::get(ETy, (*$2)[i]));
2652 delete $2;
2653 $$ = ValID::create(ConstantArray::get(ATy, Vals));
2654 CHECK_FOR_ERROR
2655 }
2656 | '{' ConstVector '}' {
2657 std::vector<const Type*> Elements($2->size());
2658 for (unsigned i = 0, e = $2->size(); i != e; ++i)
2659 Elements[i] = (*$2)[i]->getType();
2660
2661 const StructType *STy = StructType::get(Elements);
2662 PATypeHolder* PTy = new PATypeHolder(HandleUpRefs(STy));
2663
2664 $$ = ValID::create(ConstantStruct::get(STy, *$2));
2665 delete PTy; delete $2;
2666 CHECK_FOR_ERROR
2667 }
2668 | '{' '}' {
2669 const StructType *STy = StructType::get(std::vector<const Type*>());
2670 $$ = ValID::create(ConstantStruct::get(STy, std::vector<Constant*>()));
2671 CHECK_FOR_ERROR
2672 }
2673 | '<' '{' ConstVector '}' '>' {
2674 std::vector<const Type*> Elements($3->size());
2675 for (unsigned i = 0, e = $3->size(); i != e; ++i)
2676 Elements[i] = (*$3)[i]->getType();
2677
2678 const StructType *STy = StructType::get(Elements, /*isPacked=*/true);
2679 PATypeHolder* PTy = new PATypeHolder(HandleUpRefs(STy));
2680
2681 $$ = ValID::create(ConstantStruct::get(STy, *$3));
2682 delete PTy; delete $3;
2683 CHECK_FOR_ERROR
2684 }
2685 | '<' '{' '}' '>' {
2686 const StructType *STy = StructType::get(std::vector<const Type*>(),
2687 /*isPacked=*/true);
2688 $$ = ValID::create(ConstantStruct::get(STy, std::vector<Constant*>()));
2689 CHECK_FOR_ERROR
2690 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002691 | ConstExpr {
Reid Spencera132e042006-12-03 05:46:11 +00002692 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002693 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002694 }
2695 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002696 $$ = ValID::createInlineAsm(*$3, *$5, $2);
2697 delete $3;
2698 delete $5;
Reid Spencer61c83e02006-08-18 08:43:06 +00002699 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002700 };
2701
2702// SymbolicValueRef - Reference to one of two ways of symbolically refering to
2703// another value.
2704//
Reid Spencer41dff5e2007-01-26 08:05:27 +00002705SymbolicValueRef : LOCALVAL_ID { // Is it an integer reference...?
2706 $$ = ValID::createLocalID($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002707 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002708 }
Reid Spencer41dff5e2007-01-26 08:05:27 +00002709 | GLOBALVAL_ID {
2710 $$ = ValID::createGlobalID($1);
2711 CHECK_FOR_ERROR
2712 }
2713 | LocalName { // Is it a named reference...?
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002714 $$ = ValID::createLocalName(*$1);
2715 delete $1;
Reid Spencer41dff5e2007-01-26 08:05:27 +00002716 CHECK_FOR_ERROR
2717 }
2718 | GlobalName { // Is it a named reference...?
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002719 $$ = ValID::createGlobalName(*$1);
2720 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002721 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002722 };
2723
2724// ValueRef - A reference to a definition... either constant or symbolic
2725ValueRef : SymbolicValueRef | ConstValueRef;
2726
2727
2728// ResolvedVal - a <type> <value> pair. This is used only in cases where the
2729// type immediately preceeds the value reference, and allows complex constant
2730// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
2731ResolvedVal : Types ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00002732 if (!UpRefs.empty())
2733 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Eric Christopher2a5196f2008-09-24 04:55:49 +00002734 $$ = getVal(*$1, $2);
Reid Spencer14310612006-12-31 05:40:51 +00002735 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002736 CHECK_FOR_ERROR
Reid Spencer14310612006-12-31 05:40:51 +00002737 }
2738 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00002739
Devang Patel7990dc72008-02-20 22:40:23 +00002740ReturnedVal : ResolvedVal {
2741 $$ = new std::vector<Value *>();
Eric Christopher2a5196f2008-09-24 04:55:49 +00002742 $$->push_back($1);
Devang Patel7990dc72008-02-20 22:40:23 +00002743 CHECK_FOR_ERROR
2744 }
Devang Patel6bfc63b2008-02-23 00:38:56 +00002745 | ReturnedVal ',' ResolvedVal {
Eric Christopher2a5196f2008-09-24 04:55:49 +00002746 ($$=$1)->push_back($3);
Devang Patel7990dc72008-02-20 22:40:23 +00002747 CHECK_FOR_ERROR
2748 };
2749
Chris Lattner58af2a12006-02-15 07:22:58 +00002750BasicBlockList : BasicBlockList BasicBlock {
2751 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002752 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002753 }
Eric Christopher2a5196f2008-09-24 04:55:49 +00002754 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
Chris Lattner58af2a12006-02-15 07:22:58 +00002755 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002756 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002757 };
2758
2759
Eric Christopher2a5196f2008-09-24 04:55:49 +00002760// Basic blocks are terminated by branching instructions:
Chris Lattner58af2a12006-02-15 07:22:58 +00002761// br, br/cc, switch, ret
2762//
Chris Lattner15bd0952008-08-29 17:20:18 +00002763BasicBlock : InstructionList OptLocalAssign BBTerminatorInst {
Chris Lattner58af2a12006-02-15 07:22:58 +00002764 setValueName($3, $2);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002765 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002766 InsertValue($3);
Chris Lattner58af2a12006-02-15 07:22:58 +00002767 $1->getInstList().push_back($3);
Chris Lattner58af2a12006-02-15 07:22:58 +00002768 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002769 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002770 };
2771
Chris Lattner15bd0952008-08-29 17:20:18 +00002772BasicBlock : InstructionList LocalNumber BBTerminatorInst {
2773 CHECK_FOR_ERROR
2774 int ValNum = InsertValue($3);
2775 if (ValNum != (int)$2)
2776 GEN_ERROR("Result value number %" + utostr($2) +
2777 " is incorrect, expected %" + utostr((unsigned)ValNum));
Eric Christopher2a5196f2008-09-24 04:55:49 +00002778
Chris Lattner15bd0952008-08-29 17:20:18 +00002779 $1->getInstList().push_back($3);
2780 $$ = $1;
2781 CHECK_FOR_ERROR
2782};
2783
2784
Chris Lattner58af2a12006-02-15 07:22:58 +00002785InstructionList : InstructionList Inst {
Reid Spencer3da59db2006-11-27 01:05:10 +00002786 if (CastInst *CI1 = dyn_cast<CastInst>($2))
2787 if (CastInst *CI2 = dyn_cast<CastInst>(CI1->getOperand(0)))
2788 if (CI2->getParent() == 0)
2789 $1->getInstList().push_back(CI2);
Chris Lattner58af2a12006-02-15 07:22:58 +00002790 $1->getInstList().push_back($2);
2791 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002792 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002793 }
Reid Spencer93c40032007-03-19 18:40:50 +00002794 | /* empty */ { // Empty space between instruction lists
Nick Lewycky280a6e62008-04-25 16:53:59 +00002795 $$ = defineBBVal(ValID::createLocalID(CurFun.NextValNum));
Reid Spencer61c83e02006-08-18 08:43:06 +00002796 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002797 }
Reid Spencer93c40032007-03-19 18:40:50 +00002798 | LABELSTR { // Labelled (named) basic block
Nick Lewycky280a6e62008-04-25 16:53:59 +00002799 $$ = defineBBVal(ValID::createLocalName(*$1));
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002800 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002801 CHECK_FOR_ERROR
Nick Lewycky280a6e62008-04-25 16:53:59 +00002802
Chris Lattner58af2a12006-02-15 07:22:58 +00002803 };
2804
Eric Christopher2a5196f2008-09-24 04:55:49 +00002805BBTerminatorInst :
Devang Patel7990dc72008-02-20 22:40:23 +00002806 RET ReturnedVal { // Return with a result...
Devang Patelb82b7f22008-02-26 22:17:48 +00002807 ValueList &VL = *$2;
Devang Patel13b823c2008-02-26 23:19:08 +00002808 assert(!VL.empty() && "Invalid ret operands!");
Dan Gohman1a570242008-07-23 00:54:54 +00002809 const Type *ReturnType = CurFun.CurrentFunction->getReturnType();
2810 if (VL.size() > 1 ||
2811 (isa<StructType>(ReturnType) &&
2812 (VL.empty() || VL[0]->getType() != ReturnType))) {
2813 Value *RV = UndefValue::get(ReturnType);
2814 for (unsigned i = 0, e = VL.size(); i != e; ++i) {
2815 Instruction *I = InsertValueInst::Create(RV, VL[i], i, "mrv");
2816 ($<BasicBlockVal>-1)->getInstList().push_back(I);
2817 RV = I;
2818 }
2819 $$ = ReturnInst::Create(RV);
2820 } else {
2821 $$ = ReturnInst::Create(VL[0]);
2822 }
Devang Patel7990dc72008-02-20 22:40:23 +00002823 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002824 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002825 }
Reid Spencer93c40032007-03-19 18:40:50 +00002826 | RET VOID { // Return with no result...
Gabor Greife64d2482008-04-06 23:07:54 +00002827 $$ = ReturnInst::Create();
Reid Spencer61c83e02006-08-18 08:43:06 +00002828 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002829 }
Reid Spencer93c40032007-03-19 18:40:50 +00002830 | BR LABEL ValueRef { // Unconditional Branch...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002831 BasicBlock* tmpBB = getBBVal($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002832 CHECK_FOR_ERROR
Gabor Greife64d2482008-04-06 23:07:54 +00002833 $$ = BranchInst::Create(tmpBB);
Reid Spencer93c40032007-03-19 18:40:50 +00002834 } // Conditional Branch...
Eric Christopher2a5196f2008-09-24 04:55:49 +00002835 | BR INTTYPE ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002836 if (cast<IntegerType>($2)->getBitWidth() != 1)
2837 GEN_ERROR("Branch condition must have type i1");
Reid Spencer5b7e7532006-09-28 19:28:24 +00002838 BasicBlock* tmpBBA = getBBVal($6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002839 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002840 BasicBlock* tmpBBB = getBBVal($9);
2841 CHECK_FOR_ERROR
Reid Spencer4fe16d62007-01-11 18:21:29 +00002842 Value* tmpVal = getVal(Type::Int1Ty, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002843 CHECK_FOR_ERROR
Gabor Greife64d2482008-04-06 23:07:54 +00002844 $$ = BranchInst::Create(tmpBBA, tmpBBB, tmpVal);
Chris Lattner58af2a12006-02-15 07:22:58 +00002845 }
Chris Lattnerf9078f92008-10-15 06:03:48 +00002846 | SWITCH INTTYPE ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencera132e042006-12-03 05:46:11 +00002847 Value* tmpVal = getVal($2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002848 CHECK_FOR_ERROR
2849 BasicBlock* tmpBB = getBBVal($6);
2850 CHECK_FOR_ERROR
Gabor Greife64d2482008-04-06 23:07:54 +00002851 SwitchInst *S = SwitchInst::Create(tmpVal, tmpBB, $8->size());
Chris Lattner58af2a12006-02-15 07:22:58 +00002852 $$ = S;
2853
2854 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
2855 E = $8->end();
2856 for (; I != E; ++I) {
2857 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
2858 S->addCase(CI, I->second);
2859 else
Reid Spencerb5334b02007-02-05 10:18:06 +00002860 GEN_ERROR("Switch case is constant, but not a simple integer");
Chris Lattner58af2a12006-02-15 07:22:58 +00002861 }
2862 delete $8;
Reid Spencer61c83e02006-08-18 08:43:06 +00002863 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002864 }
Chris Lattnerf9078f92008-10-15 06:03:48 +00002865 | SWITCH INTTYPE ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencera132e042006-12-03 05:46:11 +00002866 Value* tmpVal = getVal($2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002867 CHECK_FOR_ERROR
2868 BasicBlock* tmpBB = getBBVal($6);
2869 CHECK_FOR_ERROR
Gabor Greife64d2482008-04-06 23:07:54 +00002870 SwitchInst *S = SwitchInst::Create(tmpVal, tmpBB, 0);
Chris Lattner58af2a12006-02-15 07:22:58 +00002871 $$ = S;
Reid Spencer61c83e02006-08-18 08:43:06 +00002872 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002873 }
Devang Patel652203f2008-09-29 20:49:50 +00002874 | INVOKE OptCallingConv OptRetAttrs ResultTypes ValueRef '(' ParamList ')'
2875 OptFuncAttrs TO LABEL ValueRef UNWIND LABEL ValueRef {
Chris Lattner58af2a12006-02-15 07:22:58 +00002876
Reid Spencer14310612006-12-31 05:40:51 +00002877 // Handle the short syntax
2878 const PointerType *PFTy = 0;
2879 const FunctionType *Ty = 0;
Devang Patel652203f2008-09-29 20:49:50 +00002880 if (!(PFTy = dyn_cast<PointerType>($4->get())) ||
Chris Lattner58af2a12006-02-15 07:22:58 +00002881 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
2882 // Pull out the types of all of the arguments...
2883 std::vector<const Type*> ParamTypes;
Devang Patel652203f2008-09-29 20:49:50 +00002884 ParamList::iterator I = $7->begin(), E = $7->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00002885 for (; I != E; ++I) {
Reid Spencer14310612006-12-31 05:40:51 +00002886 const Type *Ty = I->Val->getType();
2887 if (Ty == Type::VoidTy)
2888 GEN_ERROR("Short call syntax cannot be used with varargs");
2889 ParamTypes.push_back(Ty);
Chris Lattner58af2a12006-02-15 07:22:58 +00002890 }
Eric Christopher2a5196f2008-09-24 04:55:49 +00002891
Devang Patel652203f2008-09-29 20:49:50 +00002892 if (!FunctionType::isValidReturnType(*$4))
Chris Lattnera925a142008-04-23 05:37:08 +00002893 GEN_ERROR("Invalid result type for LLVM function");
2894
Devang Patel652203f2008-09-29 20:49:50 +00002895 Ty = FunctionType::get($4->get(), ParamTypes, false);
Christopher Lamb4374f8e2007-12-17 01:17:35 +00002896 PFTy = PointerType::getUnqual(Ty);
Chris Lattner58af2a12006-02-15 07:22:58 +00002897 }
2898
Devang Patel652203f2008-09-29 20:49:50 +00002899 delete $4;
Reid Spencer66728ef2007-03-20 01:13:36 +00002900
Devang Patel652203f2008-09-29 20:49:50 +00002901 Value *V = getVal(PFTy, $5); // Get the function we're calling...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002902 CHECK_FOR_ERROR
Devang Patel652203f2008-09-29 20:49:50 +00002903 BasicBlock *Normal = getBBVal($12);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002904 CHECK_FOR_ERROR
Devang Patel652203f2008-09-29 20:49:50 +00002905 BasicBlock *Except = getBBVal($15);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002906 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002907
Devang Patel05988662008-09-25 21:00:45 +00002908 SmallVector<AttributeWithIndex, 8> Attrs;
Devang Patel19c87462008-09-26 22:53:05 +00002909 //FIXME : In 3.0, stop accepting zext, sext and inreg as optional function
2910 //attributes.
Devang Patel652203f2008-09-29 20:49:50 +00002911 Attributes RetAttrs = $3;
2912 if ($9 != Attribute::None) {
2913 if ($9 & Attribute::ZExt) {
Devang Patel19c87462008-09-26 22:53:05 +00002914 RetAttrs = RetAttrs | Attribute::ZExt;
Devang Patel652203f2008-09-29 20:49:50 +00002915 $9 = $9 ^ Attribute::ZExt;
Devang Patel19c87462008-09-26 22:53:05 +00002916 }
Devang Patel652203f2008-09-29 20:49:50 +00002917 if ($9 & Attribute::SExt) {
Devang Patel19c87462008-09-26 22:53:05 +00002918 RetAttrs = RetAttrs | Attribute::SExt;
Devang Patel652203f2008-09-29 20:49:50 +00002919 $9 = $9 ^ Attribute::SExt;
Devang Patel19c87462008-09-26 22:53:05 +00002920 }
Devang Patel652203f2008-09-29 20:49:50 +00002921 if ($9 & Attribute::InReg) {
Devang Patel19c87462008-09-26 22:53:05 +00002922 RetAttrs = RetAttrs | Attribute::InReg;
Devang Patel652203f2008-09-29 20:49:50 +00002923 $9 = $9 ^ Attribute::InReg;
Devang Patel19c87462008-09-26 22:53:05 +00002924 }
Devang Patel19c87462008-09-26 22:53:05 +00002925 }
Devang Patel652203f2008-09-29 20:49:50 +00002926 if (RetAttrs != Attribute::None)
2927 Attrs.push_back(AttributeWithIndex::get(0, RetAttrs));
Devang Patel19c87462008-09-26 22:53:05 +00002928
Reid Spencer14310612006-12-31 05:40:51 +00002929 // Check the arguments
2930 ValueList Args;
Devang Patel652203f2008-09-29 20:49:50 +00002931 if ($7->empty()) { // Has no arguments?
Reid Spencer14310612006-12-31 05:40:51 +00002932 // Make sure no arguments is a good thing!
2933 if (Ty->getNumParams() != 0)
2934 GEN_ERROR("No arguments passed to a function that "
Reid Spencerb5334b02007-02-05 10:18:06 +00002935 "expects arguments");
Chris Lattner58af2a12006-02-15 07:22:58 +00002936 } else { // Has arguments?
2937 // Loop through FunctionType's arguments and ensure they are specified
2938 // correctly!
Chris Lattner58af2a12006-02-15 07:22:58 +00002939 FunctionType::param_iterator I = Ty->param_begin();
2940 FunctionType::param_iterator E = Ty->param_end();
Devang Patel652203f2008-09-29 20:49:50 +00002941 ParamList::iterator ArgI = $7->begin(), ArgE = $7->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00002942 unsigned index = 1;
Chris Lattner58af2a12006-02-15 07:22:58 +00002943
Duncan Sandsdc024672007-11-27 13:23:08 +00002944 for (; ArgI != ArgE && I != E; ++ArgI, ++I, ++index) {
Reid Spencer14310612006-12-31 05:40:51 +00002945 if (ArgI->Val->getType() != *I)
2946 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00002947 (*I)->getDescription() + "'");
Reid Spencer14310612006-12-31 05:40:51 +00002948 Args.push_back(ArgI->Val);
Devang Patel05988662008-09-25 21:00:45 +00002949 if (ArgI->Attrs != Attribute::None)
2950 Attrs.push_back(AttributeWithIndex::get(index, ArgI->Attrs));
Reid Spencer14310612006-12-31 05:40:51 +00002951 }
Reid Spencera132e042006-12-03 05:46:11 +00002952
Reid Spencer14310612006-12-31 05:40:51 +00002953 if (Ty->isVarArg()) {
2954 if (I == E)
Chris Lattner38905612008-02-19 04:36:25 +00002955 for (; ArgI != ArgE; ++ArgI, ++index) {
Reid Spencer14310612006-12-31 05:40:51 +00002956 Args.push_back(ArgI->Val); // push the remaining varargs
Devang Patel05988662008-09-25 21:00:45 +00002957 if (ArgI->Attrs != Attribute::None)
2958 Attrs.push_back(AttributeWithIndex::get(index, ArgI->Attrs));
Chris Lattner38905612008-02-19 04:36:25 +00002959 }
Reid Spencer14310612006-12-31 05:40:51 +00002960 } else if (I != E || ArgI != ArgE)
Reid Spencerb5334b02007-02-05 10:18:06 +00002961 GEN_ERROR("Invalid number of parameters detected");
Chris Lattner58af2a12006-02-15 07:22:58 +00002962 }
Devang Patel652203f2008-09-29 20:49:50 +00002963 if ($9 != Attribute::None)
2964 Attrs.push_back(AttributeWithIndex::get(~0, $9));
Devang Patel05988662008-09-25 21:00:45 +00002965 AttrListPtr PAL;
Duncan Sandsdc024672007-11-27 13:23:08 +00002966 if (!Attrs.empty())
Devang Patel05988662008-09-25 21:00:45 +00002967 PAL = AttrListPtr::get(Attrs.begin(), Attrs.end());
Duncan Sandsdc024672007-11-27 13:23:08 +00002968
Reid Spencer14310612006-12-31 05:40:51 +00002969 // Create the InvokeInst
Dan Gohman041e2eb2008-05-15 19:50:34 +00002970 InvokeInst *II = InvokeInst::Create(V, Normal, Except,
2971 Args.begin(), Args.end());
Reid Spencer14310612006-12-31 05:40:51 +00002972 II->setCallingConv($2);
Devang Patel05988662008-09-25 21:00:45 +00002973 II->setAttributes(PAL);
Reid Spencer14310612006-12-31 05:40:51 +00002974 $$ = II;
Devang Patel652203f2008-09-29 20:49:50 +00002975 delete $7;
Reid Spencer61c83e02006-08-18 08:43:06 +00002976 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002977 }
2978 | UNWIND {
2979 $$ = new UnwindInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002980 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002981 }
2982 | UNREACHABLE {
2983 $$ = new UnreachableInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002984 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002985 };
2986
2987
2988
Chris Lattnerf9078f92008-10-15 06:03:48 +00002989JumpTable : JumpTable INTTYPE ConstValueRef ',' LABEL ValueRef {
Chris Lattner58af2a12006-02-15 07:22:58 +00002990 $$ = $1;
Reid Spencer93c40032007-03-19 18:40:50 +00002991 Constant *V = cast<Constant>(getExistingVal($2, $3));
Reid Spencer5b7e7532006-09-28 19:28:24 +00002992 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002993 if (V == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002994 GEN_ERROR("May only switch on a constant pool value");
Chris Lattner58af2a12006-02-15 07:22:58 +00002995
Reid Spencer5b7e7532006-09-28 19:28:24 +00002996 BasicBlock* tmpBB = getBBVal($6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002997 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002998 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner58af2a12006-02-15 07:22:58 +00002999 }
Chris Lattnerf9078f92008-10-15 06:03:48 +00003000 | INTTYPE ConstValueRef ',' LABEL ValueRef {
Chris Lattner58af2a12006-02-15 07:22:58 +00003001 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Reid Spencer93c40032007-03-19 18:40:50 +00003002 Constant *V = cast<Constant>(getExistingVal($1, $2));
Reid Spencer5b7e7532006-09-28 19:28:24 +00003003 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003004
3005 if (V == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00003006 GEN_ERROR("May only switch on a constant pool value");
Chris Lattner58af2a12006-02-15 07:22:58 +00003007
Reid Spencer5b7e7532006-09-28 19:28:24 +00003008 BasicBlock* tmpBB = getBBVal($5);
Reid Spencer61c83e02006-08-18 08:43:06 +00003009 CHECK_FOR_ERROR
Eric Christopher2a5196f2008-09-24 04:55:49 +00003010 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner58af2a12006-02-15 07:22:58 +00003011 };
3012
Reid Spencer41dff5e2007-01-26 08:05:27 +00003013Inst : OptLocalAssign InstVal {
Reid Spenceref9b9a72007-02-05 20:47:22 +00003014 // Is this definition named?? if so, assign the name...
3015 setValueName($2, $1);
3016 CHECK_FOR_ERROR
3017 InsertValue($2);
3018 $$ = $2;
3019 CHECK_FOR_ERROR
3020 };
3021
Chris Lattner15bd0952008-08-29 17:20:18 +00003022Inst : LocalNumber InstVal {
3023 CHECK_FOR_ERROR
3024 int ValNum = InsertValue($2);
Eric Christopher2a5196f2008-09-24 04:55:49 +00003025
Chris Lattner15bd0952008-08-29 17:20:18 +00003026 if (ValNum != (int)$1)
3027 GEN_ERROR("Result value number %" + utostr($1) +
3028 " is incorrect, expected %" + utostr((unsigned)ValNum));
3029
3030 $$ = $2;
3031 CHECK_FOR_ERROR
3032 };
3033
Chris Lattner58af2a12006-02-15 07:22:58 +00003034
3035PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Reid Spencer14310612006-12-31 05:40:51 +00003036 if (!UpRefs.empty())
3037 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00003038 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Reid Spencera132e042006-12-03 05:46:11 +00003039 Value* tmpVal = getVal(*$1, $3);
Reid Spencer61c83e02006-08-18 08:43:06 +00003040 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00003041 BasicBlock* tmpBB = getBBVal($5);
3042 CHECK_FOR_ERROR
3043 $$->push_back(std::make_pair(tmpVal, tmpBB));
Reid Spencera132e042006-12-03 05:46:11 +00003044 delete $1;
Chris Lattner58af2a12006-02-15 07:22:58 +00003045 }
3046 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
3047 $$ = $1;
Reid Spencer5b7e7532006-09-28 19:28:24 +00003048 Value* tmpVal = getVal($1->front().first->getType(), $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00003049 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00003050 BasicBlock* tmpBB = getBBVal($6);
3051 CHECK_FOR_ERROR
3052 $1->push_back(std::make_pair(tmpVal, tmpBB));
Chris Lattner58af2a12006-02-15 07:22:58 +00003053 };
3054
3055
Devang Patel05988662008-09-25 21:00:45 +00003056ParamList : Types OptAttributes ValueRef OptAttributes {
3057 // FIXME: Remove trailing OptAttributes in LLVM 3.0, it was a mistake in 2.0
Reid Spencer14310612006-12-31 05:40:51 +00003058 if (!UpRefs.empty())
3059 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
3060 // Used for call and invoke instructions
Dale Johanneseneb57ea72007-11-05 21:20:28 +00003061 $$ = new ParamList();
Duncan Sandsdc024672007-11-27 13:23:08 +00003062 ParamListEntry E; E.Attrs = $2 | $4; E.Val = getVal($1->get(), $3);
Reid Spencer14310612006-12-31 05:40:51 +00003063 $$->push_back(E);
Reid Spencer66728ef2007-03-20 01:13:36 +00003064 delete $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00003065 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003066 }
Devang Patel05988662008-09-25 21:00:45 +00003067 | LABEL OptAttributes ValueRef OptAttributes {
3068 // FIXME: Remove trailing OptAttributes in LLVM 3.0, it was a mistake in 2.0
Dale Johanneseneb57ea72007-11-05 21:20:28 +00003069 // Labels are only valid in ASMs
3070 $$ = new ParamList();
Duncan Sandsdc024672007-11-27 13:23:08 +00003071 ParamListEntry E; E.Attrs = $2 | $4; E.Val = getBBVal($3);
Dale Johanneseneb57ea72007-11-05 21:20:28 +00003072 $$->push_back(E);
Duncan Sandsdc024672007-11-27 13:23:08 +00003073 CHECK_FOR_ERROR
Dale Johanneseneb57ea72007-11-05 21:20:28 +00003074 }
Devang Patel05988662008-09-25 21:00:45 +00003075 | ParamList ',' Types OptAttributes ValueRef OptAttributes {
3076 // FIXME: Remove trailing OptAttributes in LLVM 3.0, it was a mistake in 2.0
Reid Spencer14310612006-12-31 05:40:51 +00003077 if (!UpRefs.empty())
3078 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00003079 $$ = $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00003080 ParamListEntry E; E.Attrs = $4 | $6; E.Val = getVal($3->get(), $5);
Reid Spencer14310612006-12-31 05:40:51 +00003081 $$->push_back(E);
Reid Spencer66728ef2007-03-20 01:13:36 +00003082 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00003083 CHECK_FOR_ERROR
Reid Spencer14310612006-12-31 05:40:51 +00003084 }
Devang Patel05988662008-09-25 21:00:45 +00003085 | ParamList ',' LABEL OptAttributes ValueRef OptAttributes {
3086 // FIXME: Remove trailing OptAttributes in LLVM 3.0, it was a mistake in 2.0
Dale Johanneseneb57ea72007-11-05 21:20:28 +00003087 $$ = $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00003088 ParamListEntry E; E.Attrs = $4 | $6; E.Val = getBBVal($5);
Dale Johanneseneb57ea72007-11-05 21:20:28 +00003089 $$->push_back(E);
3090 CHECK_FOR_ERROR
3091 }
3092 | /*empty*/ { $$ = new ParamList(); };
Chris Lattner58af2a12006-02-15 07:22:58 +00003093
Reid Spencer14310612006-12-31 05:40:51 +00003094IndexList // Used for gep instructions and constant expressions
Reid Spencerc6c59fd2006-12-31 21:47:02 +00003095 : /*empty*/ { $$ = new std::vector<Value*>(); }
Reid Spencer14310612006-12-31 05:40:51 +00003096 | IndexList ',' ResolvedVal {
3097 $$ = $1;
3098 $$->push_back($3);
3099 CHECK_FOR_ERROR
3100 }
Reid Spencerc6c59fd2006-12-31 21:47:02 +00003101 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00003102
Dan Gohman81a0c0b2008-05-31 00:58:22 +00003103ConstantIndexList // Used for insertvalue and extractvalue instructions
3104 : ',' EUINT64VAL {
3105 $$ = new std::vector<unsigned>();
3106 if ((unsigned)$2 != $2)
3107 GEN_ERROR("Index " + utostr($2) + " is not valid for insertvalue or extractvalue.");
3108 $$->push_back($2);
3109 }
3110 | ConstantIndexList ',' EUINT64VAL {
3111 $$ = $1;
3112 if ((unsigned)$3 != $3)
3113 GEN_ERROR("Index " + utostr($3) + " is not valid for insertvalue or extractvalue.");
3114 $$->push_back($3);
3115 CHECK_FOR_ERROR
3116 }
3117 ;
3118
Chris Lattner58af2a12006-02-15 07:22:58 +00003119OptTailCall : TAIL CALL {
3120 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00003121 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003122 }
3123 | CALL {
3124 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00003125 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003126 };
3127
Chris Lattner58af2a12006-02-15 07:22:58 +00003128InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00003129 if (!UpRefs.empty())
3130 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Eric Christopher2a5196f2008-09-24 04:55:49 +00003131 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint() &&
Reid Spencer9d6565a2007-02-15 02:26:10 +00003132 !isa<VectorType>((*$2).get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00003133 GEN_ERROR(
Reid Spencerb5334b02007-02-05 10:18:06 +00003134 "Arithmetic operator requires integer, FP, or packed operands");
Eric Christopher2a5196f2008-09-24 04:55:49 +00003135 Value* val1 = getVal(*$2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00003136 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00003137 Value* val2 = getVal(*$2, $5);
Reid Spencer5b7e7532006-09-28 19:28:24 +00003138 CHECK_FOR_ERROR
Dan Gohmane4977cf2008-05-23 01:55:30 +00003139 $$ = BinaryOperator::Create($1, val1, val2);
Chris Lattner58af2a12006-02-15 07:22:58 +00003140 if ($$ == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00003141 GEN_ERROR("binary operator returned null");
Reid Spencera132e042006-12-03 05:46:11 +00003142 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00003143 }
3144 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00003145 if (!UpRefs.empty())
3146 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Chris Lattner42a75512007-01-15 02:27:26 +00003147 if (!(*$2)->isInteger()) {
Nate Begeman5bc1ea02008-07-29 15:49:41 +00003148 if (!isa<VectorType>($2->get()) ||
Reid Spencer9d6565a2007-02-15 02:26:10 +00003149 !cast<VectorType>($2->get())->getElementType()->isInteger())
Reid Spencerb5334b02007-02-05 10:18:06 +00003150 GEN_ERROR("Logical operator requires integral operands");
Chris Lattner58af2a12006-02-15 07:22:58 +00003151 }
Reid Spencera132e042006-12-03 05:46:11 +00003152 Value* tmpVal1 = getVal(*$2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00003153 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00003154 Value* tmpVal2 = getVal(*$2, $5);
Reid Spencer5b7e7532006-09-28 19:28:24 +00003155 CHECK_FOR_ERROR
Dan Gohmane4977cf2008-05-23 01:55:30 +00003156 $$ = BinaryOperator::Create($1, tmpVal1, tmpVal2);
Chris Lattner58af2a12006-02-15 07:22:58 +00003157 if ($$ == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00003158 GEN_ERROR("binary operator returned null");
Reid Spencera132e042006-12-03 05:46:11 +00003159 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00003160 }
Reid Spencera132e042006-12-03 05:46:11 +00003161 | ICMP IPredicates Types ValueRef ',' ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00003162 if (!UpRefs.empty())
3163 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003164 Value* tmpVal1 = getVal(*$3, $4);
3165 CHECK_FOR_ERROR
3166 Value* tmpVal2 = getVal(*$3, $6);
3167 CHECK_FOR_ERROR
Dan Gohmane4977cf2008-05-23 01:55:30 +00003168 $$ = CmpInst::Create($1, $2, tmpVal1, tmpVal2);
Reid Spencera132e042006-12-03 05:46:11 +00003169 if ($$ == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00003170 GEN_ERROR("icmp operator returned null");
Reid Spencer66728ef2007-03-20 01:13:36 +00003171 delete $3;
Reid Spencera132e042006-12-03 05:46:11 +00003172 }
3173 | FCMP FPredicates Types ValueRef ',' ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00003174 if (!UpRefs.empty())
3175 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003176 Value* tmpVal1 = getVal(*$3, $4);
3177 CHECK_FOR_ERROR
3178 Value* tmpVal2 = getVal(*$3, $6);
3179 CHECK_FOR_ERROR
Dan Gohmane4977cf2008-05-23 01:55:30 +00003180 $$ = CmpInst::Create($1, $2, tmpVal1, tmpVal2);
Reid Spencera132e042006-12-03 05:46:11 +00003181 if ($$ == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00003182 GEN_ERROR("fcmp operator returned null");
Reid Spencer66728ef2007-03-20 01:13:36 +00003183 delete $3;
Chris Lattner58af2a12006-02-15 07:22:58 +00003184 }
Nate Begemanac80ade2008-05-12 19:01:56 +00003185 | VICMP IPredicates Types ValueRef ',' ValueRef {
3186 if (!UpRefs.empty())
3187 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
3188 if (!isa<VectorType>((*$3).get()))
3189 GEN_ERROR("Scalar types not supported by vicmp instruction");
3190 Value* tmpVal1 = getVal(*$3, $4);
3191 CHECK_FOR_ERROR
3192 Value* tmpVal2 = getVal(*$3, $6);
3193 CHECK_FOR_ERROR
Dan Gohmane4977cf2008-05-23 01:55:30 +00003194 $$ = CmpInst::Create($1, $2, tmpVal1, tmpVal2);
Nate Begemanac80ade2008-05-12 19:01:56 +00003195 if ($$ == 0)
Dan Gohmand8ee59b2008-09-09 01:13:24 +00003196 GEN_ERROR("vicmp operator returned null");
Nate Begemanac80ade2008-05-12 19:01:56 +00003197 delete $3;
3198 }
3199 | VFCMP FPredicates Types ValueRef ',' ValueRef {
3200 if (!UpRefs.empty())
3201 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
3202 if (!isa<VectorType>((*$3).get()))
3203 GEN_ERROR("Scalar types not supported by vfcmp instruction");
3204 Value* tmpVal1 = getVal(*$3, $4);
3205 CHECK_FOR_ERROR
3206 Value* tmpVal2 = getVal(*$3, $6);
3207 CHECK_FOR_ERROR
Dan Gohmane4977cf2008-05-23 01:55:30 +00003208 $$ = CmpInst::Create($1, $2, tmpVal1, tmpVal2);
Nate Begemanac80ade2008-05-12 19:01:56 +00003209 if ($$ == 0)
Dan Gohmand8ee59b2008-09-09 01:13:24 +00003210 GEN_ERROR("vfcmp operator returned null");
Nate Begemanac80ade2008-05-12 19:01:56 +00003211 delete $3;
3212 }
Reid Spencer3da59db2006-11-27 01:05:10 +00003213 | CastOps ResolvedVal TO Types {
Reid Spencer14310612006-12-31 05:40:51 +00003214 if (!UpRefs.empty())
3215 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003216 Value* Val = $2;
Reid Spencerb0fcf8f2007-01-17 02:48:45 +00003217 const Type* DestTy = $4->get();
3218 if (!CastInst::castIsValid($1, Val, DestTy))
3219 GEN_ERROR("invalid cast opcode for cast from '" +
3220 Val->getType()->getDescription() + "' to '" +
Eric Christopher2a5196f2008-09-24 04:55:49 +00003221 DestTy->getDescription() + "'");
Dan Gohmane4977cf2008-05-23 01:55:30 +00003222 $$ = CastInst::Create($1, Val, DestTy);
Reid Spencera132e042006-12-03 05:46:11 +00003223 delete $4;
Chris Lattner58af2a12006-02-15 07:22:58 +00003224 }
3225 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Dan Gohmand8ee59b2008-09-09 01:13:24 +00003226 if (isa<VectorType>($2->getType())) {
3227 // vector select
3228 if (!isa<VectorType>($4->getType())
3229 || !isa<VectorType>($6->getType()) )
3230 GEN_ERROR("vector select value types must be vector types");
3231 const VectorType* cond_type = cast<VectorType>($2->getType());
3232 const VectorType* select_type = cast<VectorType>($4->getType());
3233 if (cond_type->getElementType() != Type::Int1Ty)
3234 GEN_ERROR("vector select condition element type must be boolean");
3235 if (cond_type->getNumElements() != select_type->getNumElements())
3236 GEN_ERROR("vector select number of elements must be the same");
3237 } else {
3238 if ($2->getType() != Type::Int1Ty)
3239 GEN_ERROR("select condition must be boolean");
3240 }
Reid Spencera132e042006-12-03 05:46:11 +00003241 if ($4->getType() != $6->getType())
Dan Gohmand8ee59b2008-09-09 01:13:24 +00003242 GEN_ERROR("select value types must match");
Gabor Greife64d2482008-04-06 23:07:54 +00003243 $$ = SelectInst::Create($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00003244 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003245 }
3246 | VAARG ResolvedVal ',' Types {
Reid Spencer14310612006-12-31 05:40:51 +00003247 if (!UpRefs.empty())
3248 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003249 $$ = new VAArgInst($2, *$4);
3250 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00003251 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003252 }
Chris Lattner58af2a12006-02-15 07:22:58 +00003253 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00003254 if (!ExtractElementInst::isValidOperands($2, $4))
Reid Spencerb5334b02007-02-05 10:18:06 +00003255 GEN_ERROR("Invalid extractelement operands");
Reid Spencera132e042006-12-03 05:46:11 +00003256 $$ = new ExtractElementInst($2, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00003257 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003258 }
3259 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00003260 if (!InsertElementInst::isValidOperands($2, $4, $6))
Reid Spencerb5334b02007-02-05 10:18:06 +00003261 GEN_ERROR("Invalid insertelement operands");
Gabor Greife64d2482008-04-06 23:07:54 +00003262 $$ = InsertElementInst::Create($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00003263 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003264 }
Chris Lattnerd5efe842006-04-08 01:18:56 +00003265 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00003266 if (!ShuffleVectorInst::isValidOperands($2, $4, $6))
Reid Spencerb5334b02007-02-05 10:18:06 +00003267 GEN_ERROR("Invalid shufflevector operands");
Reid Spencera132e042006-12-03 05:46:11 +00003268 $$ = new ShuffleVectorInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00003269 CHECK_FOR_ERROR
Chris Lattnerd5efe842006-04-08 01:18:56 +00003270 }
Chris Lattner58af2a12006-02-15 07:22:58 +00003271 | PHI_TOK PHIList {
3272 const Type *Ty = $2->front().first->getType();
3273 if (!Ty->isFirstClassType())
Reid Spencerb5334b02007-02-05 10:18:06 +00003274 GEN_ERROR("PHI node operands must be of first class type");
Gabor Greife64d2482008-04-06 23:07:54 +00003275 $$ = PHINode::Create(Ty);
Chris Lattner58af2a12006-02-15 07:22:58 +00003276 ((PHINode*)$$)->reserveOperandSpace($2->size());
3277 while ($2->begin() != $2->end()) {
Eric Christopher2a5196f2008-09-24 04:55:49 +00003278 if ($2->front().first->getType() != Ty)
Reid Spencerb5334b02007-02-05 10:18:06 +00003279 GEN_ERROR("All elements of a PHI node must be of the same type");
Chris Lattner58af2a12006-02-15 07:22:58 +00003280 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
3281 $2->pop_front();
3282 }
3283 delete $2; // Free the list...
Reid Spencer61c83e02006-08-18 08:43:06 +00003284 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003285 }
Devang Patel652203f2008-09-29 20:49:50 +00003286 | OptTailCall OptCallingConv OptRetAttrs ResultTypes ValueRef '(' ParamList ')'
Reid Spencer218ded22007-01-05 17:07:23 +00003287 OptFuncAttrs {
Reid Spencer14310612006-12-31 05:40:51 +00003288
3289 // Handle the short syntax
Reid Spencer3da59db2006-11-27 01:05:10 +00003290 const PointerType *PFTy = 0;
3291 const FunctionType *Ty = 0;
Devang Patel652203f2008-09-29 20:49:50 +00003292 if (!(PFTy = dyn_cast<PointerType>($4->get())) ||
Chris Lattner58af2a12006-02-15 07:22:58 +00003293 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
3294 // Pull out the types of all of the arguments...
3295 std::vector<const Type*> ParamTypes;
Devang Patel652203f2008-09-29 20:49:50 +00003296 ParamList::iterator I = $7->begin(), E = $7->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00003297 for (; I != E; ++I) {
Reid Spencer14310612006-12-31 05:40:51 +00003298 const Type *Ty = I->Val->getType();
3299 if (Ty == Type::VoidTy)
3300 GEN_ERROR("Short call syntax cannot be used with varargs");
3301 ParamTypes.push_back(Ty);
Chris Lattner58af2a12006-02-15 07:22:58 +00003302 }
Chris Lattnera925a142008-04-23 05:37:08 +00003303
Devang Patel652203f2008-09-29 20:49:50 +00003304 if (!FunctionType::isValidReturnType(*$4))
Chris Lattnera925a142008-04-23 05:37:08 +00003305 GEN_ERROR("Invalid result type for LLVM function");
3306
Devang Patel652203f2008-09-29 20:49:50 +00003307 Ty = FunctionType::get($4->get(), ParamTypes, false);
Christopher Lamb4374f8e2007-12-17 01:17:35 +00003308 PFTy = PointerType::getUnqual(Ty);
Chris Lattner58af2a12006-02-15 07:22:58 +00003309 }
Chris Lattner6cdc6822007-04-26 05:31:05 +00003310
Devang Patel652203f2008-09-29 20:49:50 +00003311 Value *V = getVal(PFTy, $5); // Get the function we're calling...
Reid Spencer5b7e7532006-09-28 19:28:24 +00003312 CHECK_FOR_ERROR
Chris Lattner6cdc6822007-04-26 05:31:05 +00003313
Reid Spencer7780acb2007-04-16 06:56:07 +00003314 // Check for call to invalid intrinsic to avoid crashing later.
3315 if (Function *theF = dyn_cast<Function>(V)) {
Reid Spencered48de22007-04-16 22:02:23 +00003316 if (theF->hasName() && (theF->getValueName()->getKeyLength() >= 5) &&
Reid Spencer36fdde12007-04-16 20:35:38 +00003317 (0 == strncmp(theF->getValueName()->getKeyData(), "llvm.", 5)) &&
3318 !theF->getIntrinsicID(true))
Reid Spencer7780acb2007-04-16 06:56:07 +00003319 GEN_ERROR("Call to invalid LLVM intrinsic function '" +
3320 theF->getName() + "'");
3321 }
3322
Devang Patel05988662008-09-25 21:00:45 +00003323 // Set up the Attributes for the function
3324 SmallVector<AttributeWithIndex, 8> Attrs;
Devang Patel19c87462008-09-26 22:53:05 +00003325 //FIXME : In 3.0, stop accepting zext, sext and inreg as optional function
3326 //attributes.
Devang Patel652203f2008-09-29 20:49:50 +00003327 Attributes RetAttrs = $3;
3328 if ($9 != Attribute::None) {
3329 if ($9 & Attribute::ZExt) {
Devang Patel19c87462008-09-26 22:53:05 +00003330 RetAttrs = RetAttrs | Attribute::ZExt;
Devang Patel652203f2008-09-29 20:49:50 +00003331 $9 = $9 ^ Attribute::ZExt;
Devang Patel19c87462008-09-26 22:53:05 +00003332 }
Devang Patel652203f2008-09-29 20:49:50 +00003333 if ($9 & Attribute::SExt) {
Devang Patel19c87462008-09-26 22:53:05 +00003334 RetAttrs = RetAttrs | Attribute::SExt;
Devang Patel652203f2008-09-29 20:49:50 +00003335 $9 = $9 ^ Attribute::SExt;
Devang Patel19c87462008-09-26 22:53:05 +00003336 }
Devang Patel652203f2008-09-29 20:49:50 +00003337 if ($9 & Attribute::InReg) {
Devang Patel19c87462008-09-26 22:53:05 +00003338 RetAttrs = RetAttrs | Attribute::InReg;
Devang Patel652203f2008-09-29 20:49:50 +00003339 $9 = $9 ^ Attribute::InReg;
Devang Patel19c87462008-09-26 22:53:05 +00003340 }
Devang Patel19c87462008-09-26 22:53:05 +00003341 }
Devang Patel652203f2008-09-29 20:49:50 +00003342 if (RetAttrs != Attribute::None)
3343 Attrs.push_back(AttributeWithIndex::get(0, RetAttrs));
Devang Patel19c87462008-09-26 22:53:05 +00003344
Eric Christopher2a5196f2008-09-24 04:55:49 +00003345 // Check the arguments
Reid Spencer14310612006-12-31 05:40:51 +00003346 ValueList Args;
Devang Patel652203f2008-09-29 20:49:50 +00003347 if ($7->empty()) { // Has no arguments?
Chris Lattner58af2a12006-02-15 07:22:58 +00003348 // Make sure no arguments is a good thing!
3349 if (Ty->getNumParams() != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00003350 GEN_ERROR("No arguments passed to a function that "
Reid Spencerb5334b02007-02-05 10:18:06 +00003351 "expects arguments");
Chris Lattner58af2a12006-02-15 07:22:58 +00003352 } else { // Has arguments?
3353 // Loop through FunctionType's arguments and ensure they are specified
Duncan Sandsdc024672007-11-27 13:23:08 +00003354 // correctly. Also, gather any parameter attributes.
Chris Lattner58af2a12006-02-15 07:22:58 +00003355 FunctionType::param_iterator I = Ty->param_begin();
3356 FunctionType::param_iterator E = Ty->param_end();
Devang Patel652203f2008-09-29 20:49:50 +00003357 ParamList::iterator ArgI = $7->begin(), ArgE = $7->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00003358 unsigned index = 1;
Chris Lattner58af2a12006-02-15 07:22:58 +00003359
Duncan Sandsdc024672007-11-27 13:23:08 +00003360 for (; ArgI != ArgE && I != E; ++ArgI, ++I, ++index) {
Reid Spencer14310612006-12-31 05:40:51 +00003361 if (ArgI->Val->getType() != *I)
3362 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00003363 (*I)->getDescription() + "'");
Reid Spencer14310612006-12-31 05:40:51 +00003364 Args.push_back(ArgI->Val);
Devang Patel05988662008-09-25 21:00:45 +00003365 if (ArgI->Attrs != Attribute::None)
3366 Attrs.push_back(AttributeWithIndex::get(index, ArgI->Attrs));
Reid Spencer14310612006-12-31 05:40:51 +00003367 }
3368 if (Ty->isVarArg()) {
3369 if (I == E)
Chris Lattner38905612008-02-19 04:36:25 +00003370 for (; ArgI != ArgE; ++ArgI, ++index) {
Reid Spencer14310612006-12-31 05:40:51 +00003371 Args.push_back(ArgI->Val); // push the remaining varargs
Devang Patel05988662008-09-25 21:00:45 +00003372 if (ArgI->Attrs != Attribute::None)
3373 Attrs.push_back(AttributeWithIndex::get(index, ArgI->Attrs));
Chris Lattner38905612008-02-19 04:36:25 +00003374 }
Reid Spencer14310612006-12-31 05:40:51 +00003375 } else if (I != E || ArgI != ArgE)
Reid Spencerb5334b02007-02-05 10:18:06 +00003376 GEN_ERROR("Invalid number of parameters detected");
Chris Lattner58af2a12006-02-15 07:22:58 +00003377 }
Devang Patel652203f2008-09-29 20:49:50 +00003378 if ($9 != Attribute::None)
3379 Attrs.push_back(AttributeWithIndex::get(~0, $9));
Duncan Sandsdc024672007-11-27 13:23:08 +00003380
Devang Patel05988662008-09-25 21:00:45 +00003381 // Finish off the Attributes and check them
3382 AttrListPtr PAL;
Duncan Sandsdc024672007-11-27 13:23:08 +00003383 if (!Attrs.empty())
Devang Patel05988662008-09-25 21:00:45 +00003384 PAL = AttrListPtr::get(Attrs.begin(), Attrs.end());
Duncan Sandsdc024672007-11-27 13:23:08 +00003385
Reid Spencer14310612006-12-31 05:40:51 +00003386 // Create the call node
Gabor Greife64d2482008-04-06 23:07:54 +00003387 CallInst *CI = CallInst::Create(V, Args.begin(), Args.end());
Reid Spencer14310612006-12-31 05:40:51 +00003388 CI->setTailCall($1);
3389 CI->setCallingConv($2);
Devang Patel05988662008-09-25 21:00:45 +00003390 CI->setAttributes(PAL);
Reid Spencer14310612006-12-31 05:40:51 +00003391 $$ = CI;
Devang Patel652203f2008-09-29 20:49:50 +00003392 delete $7;
3393 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00003394 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003395 }
3396 | MemoryInst {
3397 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00003398 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003399 };
3400
Chris Lattner58af2a12006-02-15 07:22:58 +00003401OptVolatile : VOLATILE {
3402 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00003403 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003404 }
3405 | /* empty */ {
3406 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00003407 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003408 };
3409
3410
3411
3412MemoryInst : MALLOC Types OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003413 if (!UpRefs.empty())
3414 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003415 $$ = new MallocInst(*$2, 0, $3);
3416 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00003417 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003418 }
Reid Spencera54b7cb2007-01-12 07:05:14 +00003419 | MALLOC Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003420 if (!UpRefs.empty())
3421 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Dan Gohman81a0c0b2008-05-31 00:58:22 +00003422 if ($4 != Type::Int32Ty)
3423 GEN_ERROR("Malloc array size is not a 32-bit integer!");
Reid Spencera132e042006-12-03 05:46:11 +00003424 Value* tmpVal = getVal($4, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00003425 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00003426 $$ = new MallocInst(*$2, tmpVal, $6);
3427 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00003428 }
3429 | ALLOCA Types OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003430 if (!UpRefs.empty())
3431 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003432 $$ = new AllocaInst(*$2, 0, $3);
3433 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00003434 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003435 }
Reid Spencera54b7cb2007-01-12 07:05:14 +00003436 | ALLOCA Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003437 if (!UpRefs.empty())
3438 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Dan Gohman81a0c0b2008-05-31 00:58:22 +00003439 if ($4 != Type::Int32Ty)
3440 GEN_ERROR("Alloca array size is not a 32-bit integer!");
Reid Spencera132e042006-12-03 05:46:11 +00003441 Value* tmpVal = getVal($4, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00003442 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00003443 $$ = new AllocaInst(*$2, tmpVal, $6);
3444 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00003445 }
3446 | FREE ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00003447 if (!isa<PointerType>($2->getType()))
Eric Christopher2a5196f2008-09-24 04:55:49 +00003448 GEN_ERROR("Trying to free nonpointer type " +
Reid Spencerb5334b02007-02-05 10:18:06 +00003449 $2->getType()->getDescription() + "");
Reid Spencera132e042006-12-03 05:46:11 +00003450 $$ = new FreeInst($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00003451 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003452 }
3453
Christopher Lamb5c104242007-04-22 20:09:11 +00003454 | OptVolatile LOAD Types ValueRef OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003455 if (!UpRefs.empty())
3456 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003457 if (!isa<PointerType>($3->get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00003458 GEN_ERROR("Can't load from nonpointer type: " +
Reid Spencera132e042006-12-03 05:46:11 +00003459 (*$3)->getDescription());
3460 if (!cast<PointerType>($3->get())->getElementType()->isFirstClassType())
Reid Spencer61c83e02006-08-18 08:43:06 +00003461 GEN_ERROR("Can't load from pointer of non-first-class type: " +
Reid Spencera132e042006-12-03 05:46:11 +00003462 (*$3)->getDescription());
3463 Value* tmpVal = getVal(*$3, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00003464 CHECK_FOR_ERROR
Christopher Lamb5c104242007-04-22 20:09:11 +00003465 $$ = new LoadInst(tmpVal, "", $1, $5);
Reid Spencera132e042006-12-03 05:46:11 +00003466 delete $3;
Chris Lattner58af2a12006-02-15 07:22:58 +00003467 }
Christopher Lamb5c104242007-04-22 20:09:11 +00003468 | OptVolatile STORE ResolvedVal ',' Types ValueRef OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003469 if (!UpRefs.empty())
3470 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003471 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00003472 if (!PT)
Reid Spencer61c83e02006-08-18 08:43:06 +00003473 GEN_ERROR("Can't store to a nonpointer type: " +
Reid Spencera132e042006-12-03 05:46:11 +00003474 (*$5)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00003475 const Type *ElTy = PT->getElementType();
Reid Spencera132e042006-12-03 05:46:11 +00003476 if (ElTy != $3->getType())
3477 GEN_ERROR("Can't store '" + $3->getType()->getDescription() +
Reid Spencerb5334b02007-02-05 10:18:06 +00003478 "' into space of type '" + ElTy->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00003479
Reid Spencera132e042006-12-03 05:46:11 +00003480 Value* tmpVal = getVal(*$5, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00003481 CHECK_FOR_ERROR
Christopher Lamb5c104242007-04-22 20:09:11 +00003482 $$ = new StoreInst($3, tmpVal, $1, $7);
Reid Spencera132e042006-12-03 05:46:11 +00003483 delete $5;
Chris Lattner58af2a12006-02-15 07:22:58 +00003484 }
Dan Gohmane4977cf2008-05-23 01:55:30 +00003485 | GETRESULT Types ValueRef ',' EUINT64VAL {
Dan Gohman1a570242008-07-23 00:54:54 +00003486 if (!UpRefs.empty())
3487 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
3488 if (!isa<StructType>($2->get()) && !isa<ArrayType>($2->get()))
3489 GEN_ERROR("getresult insn requires an aggregate operand");
3490 if (!ExtractValueInst::getIndexedType(*$2, $5))
3491 GEN_ERROR("Invalid getresult index for type '" +
3492 (*$2)->getDescription()+ "'");
3493
3494 Value *tmpVal = getVal(*$2, $3);
Devang Patel5a970972008-02-19 22:27:01 +00003495 CHECK_FOR_ERROR
Dan Gohman1a570242008-07-23 00:54:54 +00003496 $$ = ExtractValueInst::Create(tmpVal, $5);
3497 delete $2;
Devang Patel5a970972008-02-19 22:27:01 +00003498 }
Chris Lattner58af2a12006-02-15 07:22:58 +00003499 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencer14310612006-12-31 05:40:51 +00003500 if (!UpRefs.empty())
3501 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003502 if (!isa<PointerType>($2->get()))
Reid Spencerb5334b02007-02-05 10:18:06 +00003503 GEN_ERROR("getelementptr insn requires pointer operand");
Chris Lattner58af2a12006-02-15 07:22:58 +00003504
Dan Gohman041e2eb2008-05-15 19:50:34 +00003505 if (!GetElementPtrInst::getIndexedType(*$2, $4->begin(), $4->end()))
Reid Spencer61c83e02006-08-18 08:43:06 +00003506 GEN_ERROR("Invalid getelementptr indices for type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00003507 (*$2)->getDescription()+ "'");
Reid Spencera132e042006-12-03 05:46:11 +00003508 Value* tmpVal = getVal(*$2, $3);
Reid Spencer61c83e02006-08-18 08:43:06 +00003509 CHECK_FOR_ERROR
Gabor Greife64d2482008-04-06 23:07:54 +00003510 $$ = GetElementPtrInst::Create(tmpVal, $4->begin(), $4->end());
Eric Christopher2a5196f2008-09-24 04:55:49 +00003511 delete $2;
Reid Spencer5b7e7532006-09-28 19:28:24 +00003512 delete $4;
Dan Gohmane4977cf2008-05-23 01:55:30 +00003513 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +00003514 | EXTRACTVALUE Types ValueRef ConstantIndexList {
Dan Gohmane4977cf2008-05-23 01:55:30 +00003515 if (!UpRefs.empty())
3516 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
3517 if (!isa<StructType>($2->get()) && !isa<ArrayType>($2->get()))
3518 GEN_ERROR("extractvalue insn requires an aggregate operand");
3519
3520 if (!ExtractValueInst::getIndexedType(*$2, $4->begin(), $4->end()))
3521 GEN_ERROR("Invalid extractvalue indices for type '" +
3522 (*$2)->getDescription()+ "'");
3523 Value* tmpVal = getVal(*$2, $3);
3524 CHECK_FOR_ERROR
3525 $$ = ExtractValueInst::Create(tmpVal, $4->begin(), $4->end());
Eric Christopher2a5196f2008-09-24 04:55:49 +00003526 delete $2;
Dan Gohmane4977cf2008-05-23 01:55:30 +00003527 delete $4;
3528 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +00003529 | INSERTVALUE Types ValueRef ',' Types ValueRef ConstantIndexList {
Dan Gohmane4977cf2008-05-23 01:55:30 +00003530 if (!UpRefs.empty())
3531 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
3532 if (!isa<StructType>($2->get()) && !isa<ArrayType>($2->get()))
3533 GEN_ERROR("extractvalue insn requires an aggregate operand");
3534
3535 if (ExtractValueInst::getIndexedType(*$2, $7->begin(), $7->end()) != $5->get())
3536 GEN_ERROR("Invalid insertvalue indices for type '" +
3537 (*$2)->getDescription()+ "'");
3538 Value* aggVal = getVal(*$2, $3);
3539 Value* tmpVal = getVal(*$5, $6);
3540 CHECK_FOR_ERROR
3541 $$ = InsertValueInst::Create(aggVal, tmpVal, $7->begin(), $7->end());
Eric Christopher2a5196f2008-09-24 04:55:49 +00003542 delete $2;
Dan Gohmane4977cf2008-05-23 01:55:30 +00003543 delete $5;
3544 delete $7;
Chris Lattner58af2a12006-02-15 07:22:58 +00003545 };
3546
3547
3548%%
Reid Spencer61c83e02006-08-18 08:43:06 +00003549
Reid Spencer14310612006-12-31 05:40:51 +00003550// common code from the two 'RunVMAsmParser' functions
3551static Module* RunParser(Module * M) {
Reid Spencer14310612006-12-31 05:40:51 +00003552 CurModule.CurrentModule = M;
Reid Spencer14310612006-12-31 05:40:51 +00003553 // Check to make sure the parser succeeded
3554 if (yyparse()) {
3555 if (ParserResult)
3556 delete ParserResult;
3557 return 0;
3558 }
3559
Reid Spencer0d60b5a2007-03-30 01:37:39 +00003560 // Emit an error if there are any unresolved types left.
3561 if (!CurModule.LateResolveTypes.empty()) {
3562 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
3563 if (DID.Type == ValID::LocalName) {
3564 GenerateError("Undefined type remains at eof: '"+DID.getName() + "'");
3565 } else {
3566 GenerateError("Undefined type remains at eof: #" + itostr(DID.Num));
3567 }
3568 if (ParserResult)
3569 delete ParserResult;
3570 return 0;
3571 }
3572
3573 // Emit an error if there are any unresolved values left.
3574 if (!CurModule.LateResolveValues.empty()) {
3575 Value *V = CurModule.LateResolveValues.back();
3576 std::map<Value*, std::pair<ValID, int> >::iterator I =
3577 CurModule.PlaceHolderInfo.find(V);
3578
3579 if (I != CurModule.PlaceHolderInfo.end()) {
3580 ValID &DID = I->second.first;
3581 if (DID.Type == ValID::LocalName) {
3582 GenerateError("Undefined value remains at eof: "+DID.getName() + "'");
3583 } else {
3584 GenerateError("Undefined value remains at eof: #" + itostr(DID.Num));
3585 }
3586 if (ParserResult)
3587 delete ParserResult;
3588 return 0;
3589 }
3590 }
3591
Reid Spencer14310612006-12-31 05:40:51 +00003592 // Check to make sure that parsing produced a result
3593 if (!ParserResult)
3594 return 0;
3595
3596 // Reset ParserResult variable while saving its value for the result.
3597 Module *Result = ParserResult;
3598 ParserResult = 0;
3599
3600 return Result;
3601}
3602
Reid Spencer61c83e02006-08-18 08:43:06 +00003603void llvm::GenerateError(const std::string &message, int LineNo) {
Duncan Sandsdc024672007-11-27 13:23:08 +00003604 if (LineNo == -1) LineNo = LLLgetLineNo();
Reid Spencer61c83e02006-08-18 08:43:06 +00003605 // TODO: column number in exception
3606 if (TheParseError)
Duncan Sandsdc024672007-11-27 13:23:08 +00003607 TheParseError->setError(LLLgetFilename(), message, LineNo);
Reid Spencer61c83e02006-08-18 08:43:06 +00003608 TriggerError = 1;
3609}
3610
Chris Lattner58af2a12006-02-15 07:22:58 +00003611int yyerror(const char *ErrorMsg) {
Duncan Sandsdc024672007-11-27 13:23:08 +00003612 std::string where = LLLgetFilename() + ":" + utostr(LLLgetLineNo()) + ": ";
Reid Spenceref9b9a72007-02-05 20:47:22 +00003613 std::string errMsg = where + "error: " + std::string(ErrorMsg);
Duncan Sandsdc024672007-11-27 13:23:08 +00003614 if (yychar != YYEMPTY && yychar != 0) {
3615 errMsg += " while reading token: '";
Eric Christopher2a5196f2008-09-24 04:55:49 +00003616 errMsg += std::string(LLLgetTokenStart(),
Duncan Sandsdc024672007-11-27 13:23:08 +00003617 LLLgetTokenStart()+LLLgetTokenLength()) + "'";
3618 }
Reid Spencer61c83e02006-08-18 08:43:06 +00003619 GenerateError(errMsg);
Chris Lattner58af2a12006-02-15 07:22:58 +00003620 return 0;
3621}