blob: af8e3afee3f6213abce006c314634c24dbac03af [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- llvmAsmParser.y - Parser for llvm assembly files --------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +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"
20#include "llvm/ValueSymbolTable.h"
Chandler Carruth563d4a42007-08-04 01:56:21 +000021#include "llvm/AutoUpgrade.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000022#include "llvm/Support/GetElementPtrTypeIterator.h"
23#include "llvm/Support/CommandLine.h"
24#include "llvm/ADT/SmallVector.h"
25#include "llvm/ADT/STLExtras.h"
26#include "llvm/Support/MathExtras.h"
27#include "llvm/Support/Streams.h"
28#include <algorithm>
29#include <list>
30#include <map>
31#include <utility>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000032
33// 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 Christopher329d2672008-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
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 Christopher329d2672008-09-24 04:55:49 +000042// immediately invokes YYERROR. This would be so much cleaner if it was a
Dan Gohmanf17a25c2007-07-18 16:29:46 +000043// recursive descent parser.
44static bool TriggerError = false;
45#define CHECK_FOR_ERROR { if (TriggerError) { TriggerError = false; YYABORT; } }
46#define GEN_ERROR(msg) { GenerateError(msg); YYERROR; }
47
48int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
49int yylex(); // declaration" of xxx warnings.
50int yyparse();
Dan Gohmanf17a25c2007-07-18 16:29:46 +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
60#define UR_OUT(X) cerr << X
61#else
62#define UR_OUT(X)
63#endif
64
65#define YYERROR_VERBOSE 1
66
67static 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
74
Eric Christopher329d2672008-09-24 04:55:49 +000075static void
Dan Gohmanf17a25c2007-07-18 16:29:46 +000076ResolveDefinitions(ValueList &LateResolvers, ValueList *FutureLateResolvers=0);
77
78static struct PerModuleInfo {
79 Module *CurrentModule;
80 ValueList Values; // Module level numbered definitions
81 ValueList LateResolveValues;
82 std::vector<PATypeHolder> Types;
83 std::map<ValID, PATypeHolder> LateResolveTypes;
84
85 /// PlaceHolderInfo - When temporary placeholder objects are created, remember
86 /// how they were referenced and on which line of the input they came from so
87 /// 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);
105 if (TriggerError)
106 return;
107
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 }
119 GenerateError(UndefinedReferences);
120 return;
121 }
122
Chandler Carruth563d4a42007-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
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 Lopes363e49d2008-10-15 12:05:02 +0000143 I->first.second.destroy();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000144 GlobalRefs.erase(I);
145 }
146 return Ret;
147 }
148
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 Christopher329d2672008-09-24 04:55:49 +0000155 // we don't need to traverse that leg of the type.
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 Christopher329d2672008-09-24 04:55:49 +0000173 std::vector<const Type*>::iterator I = SeenList.begin(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 Christopher329d2672008-09-24 04:55:49 +0000185 std::vector<const Type*>::iterator I = SeenList.begin(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 }
198} CurModule;
199
200static struct PerFunctionInfo {
201 Function *CurrentFunction; // Pointer to current function being created
202
203 ValueList Values; // Keep track of #'d definitions
204 unsigned NextValNum;
205 ValueList LateResolveValues;
206 bool isDeclare; // Is this function a forward declararation?
207 GlobalValue::LinkageTypes Linkage; // Linkage for forward declaration.
208 GlobalValue::VisibilityTypes Visibility;
209
210 /// BBForwardRefs - When we see forward references to basic blocks, keep
211 /// track of them here.
212 std::map<ValID, BasicBlock*> BBForwardRefs;
213
214 inline PerFunctionInfo() {
215 CurrentFunction = 0;
216 isDeclare = false;
217 Linkage = GlobalValue::ExternalLinkage;
218 Visibility = GlobalValue::DefaultVisibility;
219 }
220
221 inline void FunctionStart(Function *M) {
222 CurrentFunction = M;
223 NextValNum = 0;
224 }
225
226 void FunctionDone() {
227 // Any forward referenced blocks left?
228 if (!BBForwardRefs.empty()) {
229 GenerateError("Undefined reference to label " +
230 BBForwardRefs.begin()->second->getName());
231 return;
232 }
233
234 // Resolve all forward references now.
235 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
236
237 Values.clear(); // Clear out function local definitions
238 BBForwardRefs.clear();
239 CurrentFunction = 0;
240 isDeclare = false;
241 Linkage = GlobalValue::ExternalLinkage;
242 Visibility = GlobalValue::DefaultVisibility;
243 }
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 Lattner906773a2008-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) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000256 // Things that have names or are void typed don't get slot numbers
257 if (V->hasName() || (V->getType() == Type::VoidTy))
Chris Lattner906773a2008-08-29 17:20:18 +0000258 return -1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000259
260 // 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 Christopher329d2672008-09-24 04:55:49 +0000262 // track of the next insertion location with NextValNum. When a BB gets
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 Lattner906773a2008-08-29 17:20:18 +0000268 return CurFun.NextValNum-1;
Eric Christopher329d2672008-09-24 04:55:49 +0000269 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000270 // For all other lists, its okay to just tack it on the back of the vector.
271 ValueTab.push_back(V);
Chris Lattner906773a2008-08-29 17:20:18 +0000272 return ValueTab.size()-1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000273}
274
275static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
276 switch (D.Type) {
277 case ValID::LocalID: // Is it a numbered definition?
278 // Module constants occupy the lowest numbered slots...
279 if (D.Num < CurModule.Types.size())
280 return CurModule.Types[D.Num];
281 break;
282 case ValID::LocalName: // Is it a named definition?
283 if (const Type *N = CurModule.CurrentModule->getTypeByName(D.getName())) {
284 D.destroy(); // Free old strdup'd memory...
285 return N;
286 }
287 break;
288 default:
289 GenerateError("Internal parser error: Invalid symbol type reference");
290 return 0;
291 }
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()) {
301 if (D.Type == ValID::LocalName) {
302 GenerateError("Reference to an undefined type: '" + D.getName() + "'");
303 return 0;
304 } else {
305 GenerateError("Reference to an undefined type: #" + utostr(D.Num));
306 return 0;
307 }
308 }
309
310 std::map<ValID, PATypeHolder>::iterator I =CurModule.LateResolveTypes.find(D);
Nuno Lopesb6f72c82008-10-15 11:20:21 +0000311 if (I != CurModule.LateResolveTypes.end()) {
312 D.destroy();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000313 return I->second;
Nuno Lopesb6f72c82008-10-15 11:20:21 +0000314 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000315
316 Type *Typ = OpaqueType::get();
317 CurModule.LateResolveTypes.insert(std::make_pair(D, Typ));
318 return Typ;
319 }
320
321// getExistingVal - Look up the value specified by the provided type and
322// the provided ValID. If the value exists and has already been defined, return
323// it. Otherwise return null.
324//
325static Value *getExistingVal(const Type *Ty, const ValID &D) {
326 if (isa<FunctionType>(Ty)) {
327 GenerateError("Functions are not values and "
328 "must be referenced as pointers");
329 return 0;
330 }
331
332 switch (D.Type) {
333 case ValID::LocalID: { // Is it a numbered definition?
334 // Check that the number is within bounds.
Eric Christopher329d2672008-09-24 04:55:49 +0000335 if (D.Num >= CurFun.Values.size())
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 Christopher329d2672008-09-24 04:55:49 +0000340 Result->getType()->getDescription() + "' does not match "
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000341 "expected type, '" + Ty->getDescription() + "'");
342 return 0;
343 }
344 return Result;
345 }
346 case ValID::GlobalID: { // Is it a numbered definition?
Eric Christopher329d2672008-09-24 04:55:49 +0000347 if (D.Num >= CurModule.Values.size())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000348 return 0;
349 Value *Result = CurModule.Values[D.Num];
350 if (Ty != Result->getType()) {
351 GenerateError("Numbered value (@" + utostr(D.Num) + ") of type '" +
Eric Christopher329d2672008-09-24 04:55:49 +0000352 Result->getType()->getDescription() + "' does not match "
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000353 "expected type, '" + Ty->getDescription() + "'");
354 return 0;
355 }
356 return Result;
357 }
Eric Christopher329d2672008-09-24 04:55:49 +0000358
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000359 case ValID::LocalName: { // Is it a named definition?
Eric Christopher329d2672008-09-24 04:55:49 +0000360 if (!inFunctionScope())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000361 return 0;
362 ValueSymbolTable &SymTab = CurFun.CurrentFunction->getValueSymbolTable();
363 Value *N = SymTab.lookup(D.getName());
Eric Christopher329d2672008-09-24 04:55:49 +0000364 if (N == 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000365 return 0;
366 if (N->getType() != Ty)
367 return 0;
Eric Christopher329d2672008-09-24 04:55:49 +0000368
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000369 D.destroy(); // Free old strdup'd memory...
370 return N;
371 }
372 case ValID::GlobalName: { // Is it a named definition?
373 ValueSymbolTable &SymTab = CurModule.CurrentModule->getValueSymbolTable();
374 Value *N = SymTab.lookup(D.getName());
Eric Christopher329d2672008-09-24 04:55:49 +0000375 if (N == 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000376 return 0;
377 if (N->getType() != Ty)
378 return 0;
379
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 Lattner59363a32008-02-19 04:36:25 +0000387 if (!isa<IntegerType>(Ty) ||
388 !ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000389 GenerateError("Signed integral constant '" +
390 itostr(D.ConstPool64) + "' is invalid for type '" +
391 Ty->getDescription() + "'");
392 return 0;
393 }
394 return ConstantInt::get(Ty, D.ConstPool64, true);
395
396 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattner59363a32008-02-19 04:36:25 +0000397 if (isa<IntegerType>(Ty) &&
398 ConstantInt::isValueValidForType(Ty, D.UConstPool64))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000399 return ConstantInt::get(Ty, D.UConstPool64);
Chris Lattner59363a32008-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;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000407 }
Chris Lattner59363a32008-02-19 04:36:25 +0000408 // This is really a signed reference. Transmogrify.
409 return ConstantInt::get(Ty, D.ConstPool64, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000410
Chris Lattnerf3d40022008-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 Christopher329d2672008-09-24 04:55:49 +0000418
Chris Lattnerf3d40022008-07-11 00:30:39 +0000419 {
420 APSInt Tmp = *D.ConstPoolInt;
Nuno Lopesafed5ce2008-11-04 14:28:33 +0000421 D.destroy();
Chris Lattnerf3d40022008-07-11 00:30:39 +0000422 Tmp.extOrTrunc(Ty->getPrimitiveSizeInBits());
423 return ConstantInt::get(Tmp);
424 }
Eric Christopher329d2672008-09-24 04:55:49 +0000425
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000426 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattner59363a32008-02-19 04:36:25 +0000427 if (!Ty->isFloatingPoint() ||
428 !ConstantFP::isValueValidForType(Ty, *D.ConstPoolFP)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000429 GenerateError("FP constant invalid for type");
430 return 0;
431 }
Eric Christopher329d2672008-09-24 04:55:49 +0000432 // Lexer has no type info, so builds all float and double FP constants
Dale Johannesen255b8fe2007-09-11 18:33:39 +0000433 // as double. Fix this here. Long double does not need this.
434 if (&D.ConstPoolFP->getSemantics() == &APFloat::IEEEdouble &&
Dale Johannesen5ba85fd2008-10-09 23:01:34 +0000435 Ty==Type::FloatTy) {
436 bool ignored;
437 D.ConstPoolFP->convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven,
438 &ignored);
439 }
Nuno Lopes97cf0032008-11-04 14:43:20 +0000440 {
441 ConstantFP *tmp = ConstantFP::get(*D.ConstPoolFP);
442 D.destroy();
443 return tmp;
444 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000445
446 case ValID::ConstNullVal: // Is it a null value?
447 if (!isa<PointerType>(Ty)) {
448 GenerateError("Cannot create a a non pointer null");
449 return 0;
450 }
451 return ConstantPointerNull::get(cast<PointerType>(Ty));
452
453 case ValID::ConstUndefVal: // Is it an undef value?
454 return UndefValue::get(Ty);
455
456 case ValID::ConstZeroVal: // Is it a zero value?
457 return Constant::getNullValue(Ty);
Eric Christopher329d2672008-09-24 04:55:49 +0000458
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000459 case ValID::ConstantVal: // Fully resolved constant?
460 if (D.ConstantValue->getType() != Ty) {
461 GenerateError("Constant expression type different from required type");
462 return 0;
463 }
464 return D.ConstantValue;
465
466 case ValID::InlineAsmVal: { // Inline asm expression
467 const PointerType *PTy = dyn_cast<PointerType>(Ty);
468 const FunctionType *FTy =
469 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
470 if (!FTy || !InlineAsm::Verify(FTy, D.IAD->Constraints)) {
471 GenerateError("Invalid type for asm constraint string");
472 return 0;
473 }
474 InlineAsm *IA = InlineAsm::get(FTy, D.IAD->AsmString, D.IAD->Constraints,
475 D.IAD->HasSideEffects);
476 D.destroy(); // Free InlineAsmDescriptor.
477 return IA;
478 }
479 default:
480 assert(0 && "Unhandled case!");
481 return 0;
482 } // End of switch
483
484 assert(0 && "Unhandled case!");
485 return 0;
486}
487
488// getVal - This function is identical to getExistingVal, except that if a
489// value is not already defined, it "improvises" by creating a placeholder var
490// that looks and acts just like the requested variable. When the value is
491// defined later, all uses of the placeholder variable are replaced with the
492// real thing.
493//
494static Value *getVal(const Type *Ty, const ValID &ID) {
495 if (Ty == Type::LabelTy) {
496 GenerateError("Cannot use a basic block here");
497 return 0;
498 }
499
500 // See if the value has already been defined.
501 Value *V = getExistingVal(Ty, ID);
502 if (V) return V;
503 if (TriggerError) return 0;
504
505 if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty)) {
Dan Gohmane6b1ee62008-05-23 01:55:30 +0000506 GenerateError("Invalid use of a non-first-class type");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000507 return 0;
508 }
509
510 // If we reached here, we referenced either a symbol that we don't know about
511 // or an id number that hasn't been read yet. We may be referencing something
512 // forward, so just create an entry to be resolved later and get to it...
513 //
514 switch (ID.Type) {
515 case ValID::GlobalName:
516 case ValID::GlobalID: {
517 const PointerType *PTy = dyn_cast<PointerType>(Ty);
518 if (!PTy) {
519 GenerateError("Invalid type for reference to global" );
520 return 0;
521 }
522 const Type* ElTy = PTy->getElementType();
523 if (const FunctionType *FTy = dyn_cast<FunctionType>(ElTy))
Gabor Greif89f01162008-04-06 23:07:54 +0000524 V = Function::Create(FTy, GlobalValue::ExternalLinkage);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000525 else
Christopher Lamb0a243582007-12-11 09:02:08 +0000526 V = new GlobalVariable(ElTy, false, GlobalValue::ExternalLinkage, 0, "",
527 (Module*)0, false, PTy->getAddressSpace());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000528 break;
529 }
530 default:
531 V = new Argument(Ty);
532 }
Eric Christopher329d2672008-09-24 04:55:49 +0000533
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000534 // Remember where this forward reference came from. FIXME, shouldn't we try
535 // to recycle these things??
536 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
Duncan Sandsf5588dc2007-11-27 13:23:08 +0000537 LLLgetLineNo())));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000538
539 if (inFunctionScope())
540 InsertValue(V, CurFun.LateResolveValues);
541 else
542 InsertValue(V, CurModule.LateResolveValues);
543 return V;
544}
545
546/// defineBBVal - This is a definition of a new basic block with the specified
547/// identifier which must be the same as CurFun.NextValNum, if its numeric.
Nick Lewyckyd8aa33a2008-04-25 16:53:59 +0000548static BasicBlock *defineBBVal(const ValID &ID) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000549 assert(inFunctionScope() && "Can't get basic block at global scope!");
550
551 BasicBlock *BB = 0;
552
553 // First, see if this was forward referenced
554
555 std::map<ValID, BasicBlock*>::iterator BBI = CurFun.BBForwardRefs.find(ID);
556 if (BBI != CurFun.BBForwardRefs.end()) {
557 BB = BBI->second;
558 // The forward declaration could have been inserted anywhere in the
559 // function: insert it into the correct place now.
560 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
561 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
562
563 // We're about to erase the entry, save the key so we can clean it up.
564 ValID Tmp = BBI->first;
565
566 // Erase the forward ref from the map as its no longer "forward"
567 CurFun.BBForwardRefs.erase(ID);
568
Eric Christopher329d2672008-09-24 04:55:49 +0000569 // The key has been removed from the map but so we don't want to leave
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000570 // strdup'd memory around so destroy it too.
571 Tmp.destroy();
572
573 // If its a numbered definition, bump the number and set the BB value.
574 if (ID.Type == ValID::LocalID) {
575 assert(ID.Num == CurFun.NextValNum && "Invalid new block number");
576 InsertValue(BB);
577 }
Eric Christopher329d2672008-09-24 04:55:49 +0000578 } else {
579 // We haven't seen this BB before and its first mention is a definition.
Devang Patel890cc572008-03-03 18:58:47 +0000580 // Just create it and return it.
581 std::string Name (ID.Type == ValID::LocalName ? ID.getName() : "");
Gabor Greif89f01162008-04-06 23:07:54 +0000582 BB = BasicBlock::Create(Name, CurFun.CurrentFunction);
Devang Patel890cc572008-03-03 18:58:47 +0000583 if (ID.Type == ValID::LocalID) {
584 assert(ID.Num == CurFun.NextValNum && "Invalid new block number");
585 InsertValue(BB);
586 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000587 }
588
Devang Patel890cc572008-03-03 18:58:47 +0000589 ID.destroy();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000590 return BB;
591}
592
593/// getBBVal - get an existing BB value or create a forward reference for it.
Eric Christopher329d2672008-09-24 04:55:49 +0000594///
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000595static BasicBlock *getBBVal(const ValID &ID) {
596 assert(inFunctionScope() && "Can't get basic block at global scope!");
597
598 BasicBlock *BB = 0;
599
600 std::map<ValID, BasicBlock*>::iterator BBI = CurFun.BBForwardRefs.find(ID);
601 if (BBI != CurFun.BBForwardRefs.end()) {
602 BB = BBI->second;
603 } if (ID.Type == ValID::LocalName) {
604 std::string Name = ID.getName();
605 Value *N = CurFun.CurrentFunction->getValueSymbolTable().lookup(Name);
Anton Korobeynikov6a4a9332008-02-20 12:07:57 +0000606 if (N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000607 if (N->getType()->getTypeID() == Type::LabelTyID)
608 BB = cast<BasicBlock>(N);
609 else
610 GenerateError("Reference to label '" + Name + "' is actually of type '"+
611 N->getType()->getDescription() + "'");
Anton Korobeynikov6a4a9332008-02-20 12:07:57 +0000612 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000613 } else if (ID.Type == ValID::LocalID) {
614 if (ID.Num < CurFun.NextValNum && ID.Num < CurFun.Values.size()) {
615 if (CurFun.Values[ID.Num]->getType()->getTypeID() == Type::LabelTyID)
616 BB = cast<BasicBlock>(CurFun.Values[ID.Num]);
617 else
Eric Christopher329d2672008-09-24 04:55:49 +0000618 GenerateError("Reference to label '%" + utostr(ID.Num) +
619 "' is actually of type '"+
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000620 CurFun.Values[ID.Num]->getType()->getDescription() + "'");
621 }
622 } else {
623 GenerateError("Illegal label reference " + ID.getName());
624 return 0;
625 }
626
627 // If its already been defined, return it now.
628 if (BB) {
629 ID.destroy(); // Free strdup'd memory.
630 return BB;
631 }
632
633 // Otherwise, this block has not been seen before, create it.
634 std::string Name;
635 if (ID.Type == ValID::LocalName)
636 Name = ID.getName();
Gabor Greif89f01162008-04-06 23:07:54 +0000637 BB = BasicBlock::Create(Name, CurFun.CurrentFunction);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000638
639 // Insert it in the forward refs map.
640 CurFun.BBForwardRefs[ID] = BB;
641
642 return BB;
643}
644
645
646//===----------------------------------------------------------------------===//
647// Code to handle forward references in instructions
648//===----------------------------------------------------------------------===//
649//
650// This code handles the late binding needed with statements that reference
651// values not defined yet... for example, a forward branch, or the PHI node for
652// a loop body.
653//
654// This keeps a table (CurFun.LateResolveValues) of all such forward references
655// and back patchs after we are done.
656//
657
658// ResolveDefinitions - If we could not resolve some defs at parsing
659// time (forward branches, phi functions for loops, etc...) resolve the
660// defs now...
661//
Eric Christopher329d2672008-09-24 04:55:49 +0000662static void
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000663ResolveDefinitions(ValueList &LateResolvers, ValueList *FutureLateResolvers) {
664 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
665 while (!LateResolvers.empty()) {
666 Value *V = LateResolvers.back();
667 LateResolvers.pop_back();
668
669 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
670 CurModule.PlaceHolderInfo.find(V);
671 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
672
673 ValID &DID = PHI->second.first;
674
675 Value *TheRealValue = getExistingVal(V->getType(), DID);
676 if (TriggerError)
677 return;
678 if (TheRealValue) {
679 V->replaceAllUsesWith(TheRealValue);
680 delete V;
681 CurModule.PlaceHolderInfo.erase(PHI);
682 } else if (FutureLateResolvers) {
683 // Functions have their unresolved items forwarded to the module late
684 // resolver table
685 InsertValue(V, *FutureLateResolvers);
686 } else {
687 if (DID.Type == ValID::LocalName || DID.Type == ValID::GlobalName) {
688 GenerateError("Reference to an invalid definition: '" +DID.getName()+
689 "' of type '" + V->getType()->getDescription() + "'",
690 PHI->second.second);
691 return;
692 } else {
693 GenerateError("Reference to an invalid definition: #" +
694 itostr(DID.Num) + " of type '" +
695 V->getType()->getDescription() + "'",
696 PHI->second.second);
697 return;
698 }
699 }
700 }
701 LateResolvers.clear();
702}
703
704// ResolveTypeTo - A brand new type was just declared. This means that (if
705// name is not null) things referencing Name can be resolved. Otherwise, things
706// refering to the number can be resolved. Do this now.
707//
708static void ResolveTypeTo(std::string *Name, const Type *ToTy) {
709 ValID D;
710 if (Name)
711 D = ValID::createLocalName(*Name);
Eric Christopher329d2672008-09-24 04:55:49 +0000712 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000713 D = ValID::createLocalID(CurModule.Types.size());
714
715 std::map<ValID, PATypeHolder>::iterator I =
716 CurModule.LateResolveTypes.find(D);
717 if (I != CurModule.LateResolveTypes.end()) {
718 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Nuno Lopes37e4b7a2008-10-15 11:11:12 +0000719 I->first.destroy();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000720 CurModule.LateResolveTypes.erase(I);
721 }
Nuno Lopes1697e8b2008-10-03 15:52:39 +0000722 D.destroy();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000723}
724
725// setValueName - Set the specified value to the name given. The name may be
726// null potentially, in which case this is a noop. The string passed in is
727// assumed to be a malloc'd string buffer, and is free'd by this function.
728//
729static void setValueName(Value *V, std::string *NameStr) {
730 if (!NameStr) return;
731 std::string Name(*NameStr); // Copy string
732 delete NameStr; // Free old string
733
734 if (V->getType() == Type::VoidTy) {
735 GenerateError("Can't assign name '" + Name+"' to value with void type");
736 return;
737 }
738
739 assert(inFunctionScope() && "Must be in function scope!");
740 ValueSymbolTable &ST = CurFun.CurrentFunction->getValueSymbolTable();
741 if (ST.lookup(Name)) {
742 GenerateError("Redefinition of value '" + Name + "' of type '" +
743 V->getType()->getDescription() + "'");
744 return;
745 }
746
747 // Set the name.
748 V->setName(Name);
749}
750
751/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
752/// this is a declaration, otherwise it is a definition.
753static GlobalVariable *
754ParseGlobalVariable(std::string *NameStr,
755 GlobalValue::LinkageTypes Linkage,
756 GlobalValue::VisibilityTypes Visibility,
757 bool isConstantGlobal, const Type *Ty,
Christopher Lamb0a243582007-12-11 09:02:08 +0000758 Constant *Initializer, bool IsThreadLocal,
759 unsigned AddressSpace = 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000760 if (isa<FunctionType>(Ty)) {
761 GenerateError("Cannot declare global vars of function type");
762 return 0;
763 }
Dan Gohmane5febe42008-05-31 00:58:22 +0000764 if (Ty == Type::LabelTy) {
765 GenerateError("Cannot declare global vars of label type");
766 return 0;
767 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000768
Christopher Lamb0a243582007-12-11 09:02:08 +0000769 const PointerType *PTy = PointerType::get(Ty, AddressSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000770
771 std::string Name;
772 if (NameStr) {
773 Name = *NameStr; // Copy string
774 delete NameStr; // Free old string
775 }
776
777 // See if this global value was forward referenced. If so, recycle the
778 // object.
779 ValID ID;
780 if (!Name.empty()) {
781 ID = ValID::createGlobalName(Name);
782 } else {
783 ID = ValID::createGlobalID(CurModule.Values.size());
784 }
785
786 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
787 // Move the global to the end of the list, from whereever it was
788 // previously inserted.
789 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
790 CurModule.CurrentModule->getGlobalList().remove(GV);
791 CurModule.CurrentModule->getGlobalList().push_back(GV);
792 GV->setInitializer(Initializer);
793 GV->setLinkage(Linkage);
794 GV->setVisibility(Visibility);
795 GV->setConstant(isConstantGlobal);
796 GV->setThreadLocal(IsThreadLocal);
797 InsertValue(GV, CurModule.Values);
Nuno Lopes1697e8b2008-10-03 15:52:39 +0000798 ID.destroy();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000799 return GV;
800 }
801
Nuno Lopes1697e8b2008-10-03 15:52:39 +0000802 ID.destroy();
803
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000804 // If this global has a name
805 if (!Name.empty()) {
806 // if the global we're parsing has an initializer (is a definition) and
807 // has external linkage.
808 if (Initializer && Linkage != GlobalValue::InternalLinkage)
809 // If there is already a global with external linkage with this name
810 if (CurModule.CurrentModule->getGlobalVariable(Name, false)) {
811 // If we allow this GVar to get created, it will be renamed in the
812 // symbol table because it conflicts with an existing GVar. We can't
813 // allow redefinition of GVars whose linking indicates that their name
814 // must stay the same. Issue the error.
815 GenerateError("Redefinition of global variable named '" + Name +
816 "' of type '" + Ty->getDescription() + "'");
817 return 0;
818 }
819 }
820
821 // Otherwise there is no existing GV to use, create one now.
822 GlobalVariable *GV =
823 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
Christopher Lamb0a243582007-12-11 09:02:08 +0000824 CurModule.CurrentModule, IsThreadLocal, AddressSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000825 GV->setVisibility(Visibility);
826 InsertValue(GV, CurModule.Values);
827 return GV;
828}
829
830// setTypeName - Set the specified type to the name given. The name may be
831// null potentially, in which case this is a noop. The string passed in is
832// assumed to be a malloc'd string buffer, and is freed by this function.
833//
834// This function returns true if the type has already been defined, but is
835// allowed to be redefined in the specified context. If the name is a new name
836// for the type plane, it is inserted and false is returned.
837static bool setTypeName(const Type *T, std::string *NameStr) {
838 assert(!inFunctionScope() && "Can't give types function-local names!");
839 if (NameStr == 0) return false;
Eric Christopher329d2672008-09-24 04:55:49 +0000840
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000841 std::string Name(*NameStr); // Copy string
842 delete NameStr; // Free old string
843
844 // We don't allow assigning names to void type
845 if (T == Type::VoidTy) {
846 GenerateError("Can't assign name '" + Name + "' to the void type");
847 return false;
848 }
849
850 // Set the type name, checking for conflicts as we do so.
851 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
852
853 if (AlreadyExists) { // Inserting a name that is already defined???
854 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
855 assert(Existing && "Conflict but no matching type?!");
856
857 // There is only one case where this is allowed: when we are refining an
858 // opaque type. In this case, Existing will be an opaque type.
859 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
860 // We ARE replacing an opaque type!
861 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
862 return true;
863 }
864
865 // Otherwise, this is an attempt to redefine a type. That's okay if
866 // the redefinition is identical to the original. This will be so if
867 // Existing and T point to the same Type object. In this one case we
868 // allow the equivalent redefinition.
869 if (Existing == T) return true; // Yes, it's equal.
870
871 // Any other kind of (non-equivalent) redefinition is an error.
872 GenerateError("Redefinition of type named '" + Name + "' of type '" +
873 T->getDescription() + "'");
874 }
875
876 return false;
877}
878
879//===----------------------------------------------------------------------===//
880// Code for handling upreferences in type names...
881//
882
883// TypeContains - Returns true if Ty directly contains E in it.
884//
885static bool TypeContains(const Type *Ty, const Type *E) {
886 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
887 E) != Ty->subtype_end();
888}
889
890namespace {
891 struct UpRefRecord {
892 // NestingLevel - The number of nesting levels that need to be popped before
893 // this type is resolved.
894 unsigned NestingLevel;
895
896 // LastContainedTy - This is the type at the current binding level for the
897 // type. Every time we reduce the nesting level, this gets updated.
898 const Type *LastContainedTy;
899
900 // UpRefTy - This is the actual opaque type that the upreference is
901 // represented with.
902 OpaqueType *UpRefTy;
903
904 UpRefRecord(unsigned NL, OpaqueType *URTy)
905 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
906 };
907}
908
909// UpRefs - A list of the outstanding upreferences that need to be resolved.
910static std::vector<UpRefRecord> UpRefs;
911
912/// HandleUpRefs - Every time we finish a new layer of types, this function is
913/// called. It loops through the UpRefs vector, which is a list of the
914/// currently active types. For each type, if the up reference is contained in
915/// the newly completed type, we decrement the level count. When the level
916/// count reaches zero, the upreferenced type is the type that is passed in:
917/// thus we can complete the cycle.
918///
919static PATypeHolder HandleUpRefs(const Type *ty) {
920 // If Ty isn't abstract, or if there are no up-references in it, then there is
921 // nothing to resolve here.
922 if (!ty->isAbstract() || UpRefs.empty()) return ty;
Eric Christopher329d2672008-09-24 04:55:49 +0000923
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000924 PATypeHolder Ty(ty);
925 UR_OUT("Type '" << Ty->getDescription() <<
926 "' newly formed. Resolving upreferences.\n" <<
927 UpRefs.size() << " upreferences active!\n");
928
929 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
930 // to zero), we resolve them all together before we resolve them to Ty. At
931 // the end of the loop, if there is anything to resolve to Ty, it will be in
932 // this variable.
933 OpaqueType *TypeToResolve = 0;
934
935 for (unsigned i = 0; i != UpRefs.size(); ++i) {
936 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
937 << UpRefs[i].second->getDescription() << ") = "
938 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
939 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
940 // Decrement level of upreference
941 unsigned Level = --UpRefs[i].NestingLevel;
942 UpRefs[i].LastContainedTy = Ty;
943 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
944 if (Level == 0) { // Upreference should be resolved!
945 if (!TypeToResolve) {
946 TypeToResolve = UpRefs[i].UpRefTy;
947 } else {
948 UR_OUT(" * Resolving upreference for "
949 << UpRefs[i].second->getDescription() << "\n";
950 std::string OldName = UpRefs[i].UpRefTy->getDescription());
951 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
952 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
953 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
954 }
955 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
956 --i; // Do not skip the next element...
957 }
958 }
959 }
960
961 if (TypeToResolve) {
962 UR_OUT(" * Resolving upreference for "
963 << UpRefs[i].second->getDescription() << "\n";
964 std::string OldName = TypeToResolve->getDescription());
965 TypeToResolve->refineAbstractTypeTo(Ty);
966 }
967
968 return Ty;
969}
970
971//===----------------------------------------------------------------------===//
972// RunVMAsmParser - Define an interface to this parser
973//===----------------------------------------------------------------------===//
974//
975static Module* RunParser(Module * M);
976
Duncan Sandsf5588dc2007-11-27 13:23:08 +0000977Module *llvm::RunVMAsmParser(llvm::MemoryBuffer *MB) {
978 InitLLLexer(MB);
979 Module *M = RunParser(new Module(LLLgetFilename()));
980 FreeLexer();
981 return M;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000982}
983
984%}
985
986%union {
987 llvm::Module *ModuleVal;
988 llvm::Function *FunctionVal;
989 llvm::BasicBlock *BasicBlockVal;
990 llvm::TerminatorInst *TermInstVal;
991 llvm::Instruction *InstVal;
992 llvm::Constant *ConstVal;
993
994 const llvm::Type *PrimType;
995 std::list<llvm::PATypeHolder> *TypeList;
996 llvm::PATypeHolder *TypeVal;
997 llvm::Value *ValueVal;
998 std::vector<llvm::Value*> *ValueList;
Dan Gohmane5febe42008-05-31 00:58:22 +0000999 std::vector<unsigned> *ConstantList;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001000 llvm::ArgListType *ArgList;
1001 llvm::TypeWithAttrs TypeWithAttrs;
1002 llvm::TypeWithAttrsList *TypeWithAttrsList;
Dale Johannesencfb19e62007-11-05 21:20:28 +00001003 llvm::ParamList *ParamList;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001004
1005 // Represent the RHS of PHI node
1006 std::list<std::pair<llvm::Value*,
1007 llvm::BasicBlock*> > *PHIList;
1008 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
1009 std::vector<llvm::Constant*> *ConstVector;
1010
1011 llvm::GlobalValue::LinkageTypes Linkage;
1012 llvm::GlobalValue::VisibilityTypes Visibility;
Devang Pateld222f862008-09-25 21:00:45 +00001013 llvm::Attributes Attributes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001014 llvm::APInt *APIntVal;
1015 int64_t SInt64Val;
1016 uint64_t UInt64Val;
1017 int SIntVal;
1018 unsigned UIntVal;
Dale Johannesenb9de9f02007-09-06 18:13:44 +00001019 llvm::APFloat *FPVal;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001020 bool BoolVal;
1021
1022 std::string *StrVal; // This memory must be deleted
1023 llvm::ValID ValIDVal;
1024
1025 llvm::Instruction::BinaryOps BinaryOpVal;
1026 llvm::Instruction::TermOps TermOpVal;
1027 llvm::Instruction::MemoryOps MemOpVal;
1028 llvm::Instruction::CastOps CastOpVal;
1029 llvm::Instruction::OtherOps OtherOpVal;
1030 llvm::ICmpInst::Predicate IPredicate;
1031 llvm::FCmpInst::Predicate FPredicate;
1032}
1033
Eric Christopher329d2672008-09-24 04:55:49 +00001034%type <ModuleVal> Module
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001035%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
1036%type <BasicBlockVal> BasicBlock InstructionList
1037%type <TermInstVal> BBTerminatorInst
1038%type <InstVal> Inst InstVal MemoryInst
1039%type <ConstVal> ConstVal ConstExpr AliaseeRef
1040%type <ConstVector> ConstVector
1041%type <ArgList> ArgList ArgListH
1042%type <PHIList> PHIList
Dale Johannesencfb19e62007-11-05 21:20:28 +00001043%type <ParamList> ParamList // For call param lists & GEP indices
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001044%type <ValueList> IndexList // For GEP indices
Dan Gohmane5febe42008-05-31 00:58:22 +00001045%type <ConstantList> ConstantIndexList // For insertvalue/extractvalue indices
Eric Christopher329d2672008-09-24 04:55:49 +00001046%type <TypeList> TypeListI
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001047%type <TypeWithAttrsList> ArgTypeList ArgTypeListI
1048%type <TypeWithAttrs> ArgType
1049%type <JumpTable> JumpTable
1050%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
1051%type <BoolVal> ThreadLocal // 'thread_local' or not
1052%type <BoolVal> OptVolatile // 'volatile' or not
1053%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
1054%type <BoolVal> OptSideEffect // 'sideeffect' or not.
1055%type <Linkage> GVInternalLinkage GVExternalLinkage
1056%type <Linkage> FunctionDefineLinkage FunctionDeclareLinkage
1057%type <Linkage> AliasLinkage
1058%type <Visibility> GVVisibilityStyle
1059
1060// ValueRef - Unresolved reference to a definition or BB
1061%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
1062%type <ValueVal> ResolvedVal // <type> <valref> pair
Devang Patelbf507402008-02-20 22:40:23 +00001063%type <ValueList> ReturnedVal
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001064// Tokens and types for handling constant integer values
1065//
1066// ESINT64VAL - A negative number within long long range
1067%token <SInt64Val> ESINT64VAL
1068
1069// EUINT64VAL - A positive number within uns. long long range
1070%token <UInt64Val> EUINT64VAL
1071
Eric Christopher329d2672008-09-24 04:55:49 +00001072// ESAPINTVAL - A negative number with arbitrary precision
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001073%token <APIntVal> ESAPINTVAL
1074
Eric Christopher329d2672008-09-24 04:55:49 +00001075// EUAPINTVAL - A positive number with arbitrary precision
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001076%token <APIntVal> EUAPINTVAL
1077
1078%token <UIntVal> LOCALVAL_ID GLOBALVAL_ID // %123 @123
1079%token <FPVal> FPVAL // Float or Double constant
1080
1081// Built in types...
1082%type <TypeVal> Types ResultTypes
Chris Lattnerc5320232008-10-15 06:16:57 +00001083%type <PrimType> PrimType // Classifications
Eric Christopher329d2672008-09-24 04:55:49 +00001084%token <PrimType> VOID INTTYPE
Dale Johannesenf325d9f2007-08-03 01:03:46 +00001085%token <PrimType> FLOAT DOUBLE X86_FP80 FP128 PPC_FP128 LABEL
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001086%token TYPE
1087
1088
Eric Christopher329d2672008-09-24 04:55:49 +00001089%token<StrVal> LOCALVAR GLOBALVAR LABELSTR
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001090%token<StrVal> STRINGCONSTANT ATSTRINGCONSTANT PCTSTRINGCONSTANT
1091%type <StrVal> LocalName OptLocalName OptLocalAssign
1092%type <StrVal> GlobalName OptGlobalAssign GlobalAssign
Gordon Henriksen13fe5e32007-12-10 03:18:06 +00001093%type <StrVal> OptSection SectionString OptGC
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001094
Christopher Lamb668d9a02007-12-12 08:45:45 +00001095%type <UIntVal> OptAlign OptCAlign OptAddrSpace
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001096
1097%token ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
1098%token DECLARE DEFINE GLOBAL CONSTANT SECTION ALIAS VOLATILE THREAD_LOCAL
1099%token TO DOTDOTDOT NULL_TOK UNDEF INTERNAL LINKONCE WEAK APPENDING
Dale Johannesen280e7bc2008-05-14 20:13:36 +00001100%token DLLIMPORT DLLEXPORT EXTERN_WEAK COMMON
Christopher Lamb0a243582007-12-11 09:02:08 +00001101%token OPAQUE EXTERNAL TARGET TRIPLE ALIGN ADDRSPACE
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001102%token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
1103%token CC_TOK CCC_TOK FASTCC_TOK COLDCC_TOK X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
Nick Lewyckyd8aa33a2008-04-25 16:53:59 +00001104%token DATALAYOUT
Chris Lattner906773a2008-08-29 17:20:18 +00001105%type <UIntVal> OptCallingConv LocalNumber
Devang Pateld222f862008-09-25 21:00:45 +00001106%type <Attributes> OptAttributes Attribute
1107%type <Attributes> OptFuncAttrs FuncAttr
Devang Patelcd842482008-09-29 20:49:50 +00001108%type <Attributes> OptRetAttrs RetAttr
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001109
1110// Basic Block Terminating Operators
1111%token <TermOpVal> RET BR SWITCH INVOKE UNWIND UNREACHABLE
1112
1113// Binary Operators
1114%type <BinaryOpVal> ArithmeticOps LogicalOps // Binops Subcatagories
1115%token <BinaryOpVal> ADD SUB MUL UDIV SDIV FDIV UREM SREM FREM AND OR XOR
1116%token <BinaryOpVal> SHL LSHR ASHR
1117
Eric Christopher329d2672008-09-24 04:55:49 +00001118%token <OtherOpVal> ICMP FCMP VICMP VFCMP
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001119%type <IPredicate> IPredicates
1120%type <FPredicate> FPredicates
Eric Christopher329d2672008-09-24 04:55:49 +00001121%token EQ NE SLT SGT SLE SGE ULT UGT ULE UGE
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001122%token OEQ ONE OLT OGT OLE OGE ORD UNO UEQ UNE
1123
1124// Memory Instructions
1125%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
1126
1127// Cast Operators
1128%type <CastOpVal> CastOps
1129%token <CastOpVal> TRUNC ZEXT SEXT FPTRUNC FPEXT BITCAST
1130%token <CastOpVal> UITOFP SITOFP FPTOUI FPTOSI INTTOPTR PTRTOINT
1131
1132// Other Operators
1133%token <OtherOpVal> PHI_TOK SELECT VAARG
1134%token <OtherOpVal> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
Devang Patel3b8849c2008-02-19 22:27:01 +00001135%token <OtherOpVal> GETRESULT
Dan Gohmane6b1ee62008-05-23 01:55:30 +00001136%token <OtherOpVal> EXTRACTVALUE INSERTVALUE
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001137
1138// Function Attributes
Reid Spenceraa8ae282007-07-31 03:50:36 +00001139%token SIGNEXT ZEROEXT NORETURN INREG SRET NOUNWIND NOALIAS BYVAL NEST
Bill Wendling60f02fc2008-11-13 01:03:00 +00001140%token READNONE READONLY GC OPTSIZE NOINLINE ALWAYSINLINE SSP SSPREQ
Devang Patel5df692d2008-09-02 20:52:40 +00001141
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001142// Visibility Styles
1143%token DEFAULT HIDDEN PROTECTED
1144
1145%start Module
1146%%
1147
1148
1149// Operations that are notably excluded from this list include:
1150// RET, BR, & SWITCH because they end basic blocks and are treated specially.
1151//
1152ArithmeticOps: ADD | SUB | MUL | UDIV | SDIV | FDIV | UREM | SREM | FREM;
1153LogicalOps : SHL | LSHR | ASHR | AND | OR | XOR;
Eric Christopher329d2672008-09-24 04:55:49 +00001154CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | BITCAST |
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001155 UITOFP | SITOFP | FPTOUI | FPTOSI | INTTOPTR | PTRTOINT;
1156
Eric Christopher329d2672008-09-24 04:55:49 +00001157IPredicates
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001158 : EQ { $$ = ICmpInst::ICMP_EQ; } | NE { $$ = ICmpInst::ICMP_NE; }
1159 | SLT { $$ = ICmpInst::ICMP_SLT; } | SGT { $$ = ICmpInst::ICMP_SGT; }
1160 | SLE { $$ = ICmpInst::ICMP_SLE; } | SGE { $$ = ICmpInst::ICMP_SGE; }
1161 | ULT { $$ = ICmpInst::ICMP_ULT; } | UGT { $$ = ICmpInst::ICMP_UGT; }
Eric Christopher329d2672008-09-24 04:55:49 +00001162 | ULE { $$ = ICmpInst::ICMP_ULE; } | UGE { $$ = ICmpInst::ICMP_UGE; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001163 ;
1164
Eric Christopher329d2672008-09-24 04:55:49 +00001165FPredicates
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001166 : OEQ { $$ = FCmpInst::FCMP_OEQ; } | ONE { $$ = FCmpInst::FCMP_ONE; }
1167 | OLT { $$ = FCmpInst::FCMP_OLT; } | OGT { $$ = FCmpInst::FCMP_OGT; }
1168 | OLE { $$ = FCmpInst::FCMP_OLE; } | OGE { $$ = FCmpInst::FCMP_OGE; }
1169 | ORD { $$ = FCmpInst::FCMP_ORD; } | UNO { $$ = FCmpInst::FCMP_UNO; }
1170 | UEQ { $$ = FCmpInst::FCMP_UEQ; } | UNE { $$ = FCmpInst::FCMP_UNE; }
1171 | ULT { $$ = FCmpInst::FCMP_ULT; } | UGT { $$ = FCmpInst::FCMP_UGT; }
1172 | ULE { $$ = FCmpInst::FCMP_ULE; } | UGE { $$ = FCmpInst::FCMP_UGE; }
1173 | TRUETOK { $$ = FCmpInst::FCMP_TRUE; }
1174 | FALSETOK { $$ = FCmpInst::FCMP_FALSE; }
1175 ;
1176
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001177LocalName : LOCALVAR | STRINGCONSTANT | PCTSTRINGCONSTANT ;
1178OptLocalName : LocalName | /*empty*/ { $$ = 0; };
1179
Christopher Lamb668d9a02007-12-12 08:45:45 +00001180OptAddrSpace : ADDRSPACE '(' EUINT64VAL ')' { $$=$3; }
1181 | /*empty*/ { $$=0; };
1182
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001183/// OptLocalAssign - Value producing statements have an optional assignment
1184/// component.
1185OptLocalAssign : LocalName '=' {
1186 $$ = $1;
1187 CHECK_FOR_ERROR
1188 }
1189 | /*empty*/ {
1190 $$ = 0;
1191 CHECK_FOR_ERROR
1192 };
1193
Chris Lattner906773a2008-08-29 17:20:18 +00001194LocalNumber : LOCALVAL_ID '=' {
1195 $$ = $1;
1196 CHECK_FOR_ERROR
1197};
1198
1199
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001200GlobalName : GLOBALVAR | ATSTRINGCONSTANT ;
1201
1202OptGlobalAssign : GlobalAssign
1203 | /*empty*/ {
1204 $$ = 0;
1205 CHECK_FOR_ERROR
1206 };
1207
1208GlobalAssign : GlobalName '=' {
1209 $$ = $1;
1210 CHECK_FOR_ERROR
1211 };
1212
Eric Christopher329d2672008-09-24 04:55:49 +00001213GVInternalLinkage
1214 : INTERNAL { $$ = GlobalValue::InternalLinkage; }
1215 | WEAK { $$ = GlobalValue::WeakLinkage; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001216 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1217 | APPENDING { $$ = GlobalValue::AppendingLinkage; }
Eric Christopher329d2672008-09-24 04:55:49 +00001218 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
Dale Johannesen280e7bc2008-05-14 20:13:36 +00001219 | COMMON { $$ = GlobalValue::CommonLinkage; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001220 ;
1221
1222GVExternalLinkage
1223 : DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1224 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
1225 | EXTERNAL { $$ = GlobalValue::ExternalLinkage; }
1226 ;
1227
1228GVVisibilityStyle
1229 : /*empty*/ { $$ = GlobalValue::DefaultVisibility; }
1230 | DEFAULT { $$ = GlobalValue::DefaultVisibility; }
1231 | HIDDEN { $$ = GlobalValue::HiddenVisibility; }
1232 | PROTECTED { $$ = GlobalValue::ProtectedVisibility; }
1233 ;
1234
1235FunctionDeclareLinkage
1236 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
Eric Christopher329d2672008-09-24 04:55:49 +00001237 | DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001238 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
1239 ;
Eric Christopher329d2672008-09-24 04:55:49 +00001240
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001241FunctionDefineLinkage
1242 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1243 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
1244 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1245 | WEAK { $$ = GlobalValue::WeakLinkage; }
Eric Christopher329d2672008-09-24 04:55:49 +00001246 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
1247 ;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001248
1249AliasLinkage
1250 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1251 | WEAK { $$ = GlobalValue::WeakLinkage; }
1252 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
1253 ;
1254
1255OptCallingConv : /*empty*/ { $$ = CallingConv::C; } |
1256 CCC_TOK { $$ = CallingConv::C; } |
1257 FASTCC_TOK { $$ = CallingConv::Fast; } |
1258 COLDCC_TOK { $$ = CallingConv::Cold; } |
1259 X86_STDCALLCC_TOK { $$ = CallingConv::X86_StdCall; } |
1260 X86_FASTCALLCC_TOK { $$ = CallingConv::X86_FastCall; } |
1261 CC_TOK EUINT64VAL {
1262 if ((unsigned)$2 != $2)
1263 GEN_ERROR("Calling conv too large");
1264 $$ = $2;
1265 CHECK_FOR_ERROR
1266 };
1267
Devang Pateld222f862008-09-25 21:00:45 +00001268Attribute : ZEROEXT { $$ = Attribute::ZExt; }
1269 | ZEXT { $$ = Attribute::ZExt; }
1270 | SIGNEXT { $$ = Attribute::SExt; }
1271 | SEXT { $$ = Attribute::SExt; }
1272 | INREG { $$ = Attribute::InReg; }
1273 | SRET { $$ = Attribute::StructRet; }
1274 | NOALIAS { $$ = Attribute::NoAlias; }
1275 | BYVAL { $$ = Attribute::ByVal; }
1276 | NEST { $$ = Attribute::Nest; }
Eric Christopher329d2672008-09-24 04:55:49 +00001277 | ALIGN EUINT64VAL { $$ =
Devang Pateld222f862008-09-25 21:00:45 +00001278 Attribute::constructAlignmentFromInt($2); }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001279 ;
1280
Devang Pateld222f862008-09-25 21:00:45 +00001281OptAttributes : /* empty */ { $$ = Attribute::None; }
1282 | OptAttributes Attribute {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001283 $$ = $1 | $2;
1284 }
1285 ;
1286
Devang Patelcd842482008-09-29 20:49:50 +00001287RetAttr : INREG { $$ = Attribute::InReg; }
1288 | ZEROEXT { $$ = Attribute::ZExt; }
1289 | SIGNEXT { $$ = Attribute::SExt; }
1290 ;
1291
1292OptRetAttrs : /* empty */ { $$ = Attribute::None; }
1293 | OptRetAttrs RetAttr {
1294 $$ = $1 | $2;
1295 }
1296 ;
1297
1298
Devang Pateld222f862008-09-25 21:00:45 +00001299FuncAttr : NORETURN { $$ = Attribute::NoReturn; }
1300 | NOUNWIND { $$ = Attribute::NoUnwind; }
1301 | INREG { $$ = Attribute::InReg; }
1302 | ZEROEXT { $$ = Attribute::ZExt; }
1303 | SIGNEXT { $$ = Attribute::SExt; }
1304 | READNONE { $$ = Attribute::ReadNone; }
1305 | READONLY { $$ = Attribute::ReadOnly; }
Chris Lattner5a0f2fe2008-10-08 06:44:45 +00001306 | NOINLINE { $$ = Attribute::NoInline; }
1307 | ALWAYSINLINE { $$ = Attribute::AlwaysInline; }
Bill Wendling60f02fc2008-11-13 01:03:00 +00001308 | OPTSIZE { $$ = Attribute::OptimizeForSize; }
1309 | SSP { $$ = Attribute::StackProtect; }
1310 | SSPREQ { $$ = Attribute::StackProtectReq; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001311 ;
1312
Devang Pateld222f862008-09-25 21:00:45 +00001313OptFuncAttrs : /* empty */ { $$ = Attribute::None; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001314 | OptFuncAttrs FuncAttr {
1315 $$ = $1 | $2;
1316 }
1317 ;
1318
Devang Patelcd842482008-09-29 20:49:50 +00001319
Gordon Henriksen13fe5e32007-12-10 03:18:06 +00001320OptGC : /* empty */ { $$ = 0; }
1321 | GC STRINGCONSTANT {
1322 $$ = $2;
1323 }
1324 ;
1325
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001326// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1327// a comma before it.
1328OptAlign : /*empty*/ { $$ = 0; } |
1329 ALIGN EUINT64VAL {
1330 $$ = $2;
1331 if ($$ != 0 && !isPowerOf2_32($$))
1332 GEN_ERROR("Alignment must be a power of two");
1333 CHECK_FOR_ERROR
1334};
1335OptCAlign : /*empty*/ { $$ = 0; } |
1336 ',' ALIGN EUINT64VAL {
1337 $$ = $3;
1338 if ($$ != 0 && !isPowerOf2_32($$))
1339 GEN_ERROR("Alignment must be a power of two");
1340 CHECK_FOR_ERROR
1341};
1342
1343
Christopher Lamb0a243582007-12-11 09:02:08 +00001344
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001345SectionString : SECTION STRINGCONSTANT {
1346 for (unsigned i = 0, e = $2->length(); i != e; ++i)
1347 if ((*$2)[i] == '"' || (*$2)[i] == '\\')
1348 GEN_ERROR("Invalid character in section name");
1349 $$ = $2;
1350 CHECK_FOR_ERROR
1351};
1352
1353OptSection : /*empty*/ { $$ = 0; } |
1354 SectionString { $$ = $1; };
1355
1356// GlobalVarAttributes - Used to pass the attributes string on a global. CurGV
1357// is set to be the global we are processing.
1358//
1359GlobalVarAttributes : /* empty */ {} |
1360 ',' GlobalVarAttribute GlobalVarAttributes {};
1361GlobalVarAttribute : SectionString {
1362 CurGV->setSection(*$1);
1363 delete $1;
1364 CHECK_FOR_ERROR
Eric Christopher329d2672008-09-24 04:55:49 +00001365 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001366 | ALIGN EUINT64VAL {
1367 if ($2 != 0 && !isPowerOf2_32($2))
1368 GEN_ERROR("Alignment must be a power of two");
1369 CurGV->setAlignment($2);
1370 CHECK_FOR_ERROR
1371 };
1372
1373//===----------------------------------------------------------------------===//
1374// Types includes all predefined types... except void, because it can only be
Eric Christopher329d2672008-09-24 04:55:49 +00001375// used in specific contexts (function returning void for example).
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001376
1377// Derived types are added later...
1378//
Dale Johannesenf325d9f2007-08-03 01:03:46 +00001379PrimType : INTTYPE | FLOAT | DOUBLE | PPC_FP128 | FP128 | X86_FP80 | LABEL ;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001380
Eric Christopher329d2672008-09-24 04:55:49 +00001381Types
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001382 : OPAQUE {
1383 $$ = new PATypeHolder(OpaqueType::get());
1384 CHECK_FOR_ERROR
1385 }
1386 | PrimType {
1387 $$ = new PATypeHolder($1);
1388 CHECK_FOR_ERROR
1389 }
Christopher Lamb668d9a02007-12-12 08:45:45 +00001390 | Types OptAddrSpace '*' { // Pointer type?
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001391 if (*$1 == Type::LabelTy)
1392 GEN_ERROR("Cannot form a pointer to a basic block");
Christopher Lamb668d9a02007-12-12 08:45:45 +00001393 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1, $2)));
Christopher Lamb0a243582007-12-11 09:02:08 +00001394 delete $1;
1395 CHECK_FOR_ERROR
1396 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001397 | SymbolicValueRef { // Named types are also simple types...
1398 const Type* tmp = getTypeVal($1);
1399 CHECK_FOR_ERROR
1400 $$ = new PATypeHolder(tmp);
1401 }
1402 | '\\' EUINT64VAL { // Type UpReference
1403 if ($2 > (uint64_t)~0U) GEN_ERROR("Value out of range");
1404 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
1405 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
1406 $$ = new PATypeHolder(OT);
1407 UR_OUT("New Upreference!\n");
1408 CHECK_FOR_ERROR
1409 }
1410 | Types '(' ArgTypeListI ')' OptFuncAttrs {
Duncan Sandsf5588dc2007-11-27 13:23:08 +00001411 // Allow but ignore attributes on function types; this permits auto-upgrade.
1412 // FIXME: remove in LLVM 3.0.
Chris Lattner73de3c02008-04-23 05:37:08 +00001413 const Type *RetTy = *$1;
1414 if (!FunctionType::isValidReturnType(RetTy))
1415 GEN_ERROR("Invalid result type for LLVM function");
Eric Christopher329d2672008-09-24 04:55:49 +00001416
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001417 std::vector<const Type*> Params;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001418 TypeWithAttrsList::iterator I = $3->begin(), E = $3->end();
Duncan Sandsf5588dc2007-11-27 13:23:08 +00001419 for (; I != E; ++I ) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001420 const Type *Ty = I->Ty->get();
1421 Params.push_back(Ty);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001422 }
Anton Korobeynikova2c02272007-12-03 19:16:54 +00001423
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001424 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1425 if (isVarArg) Params.pop_back();
1426
Anton Korobeynikove286f6d2007-12-03 21:01:29 +00001427 for (unsigned i = 0; i != Params.size(); ++i)
1428 if (!(Params[i]->isFirstClassType() || isa<OpaqueType>(Params[i])))
1429 GEN_ERROR("Function arguments must be value types!");
1430
1431 CHECK_FOR_ERROR
1432
Anton Korobeynikova2c02272007-12-03 19:16:54 +00001433 FunctionType *FT = FunctionType::get(RetTy, Params, isVarArg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001434 delete $1; // Delete the return type handle
Eric Christopher329d2672008-09-24 04:55:49 +00001435 $$ = new PATypeHolder(HandleUpRefs(FT));
Nuno Lopes896f4572008-10-05 16:49:34 +00001436
1437 // Delete the argument list
1438 for (I = $3->begin() ; I != E; ++I ) {
1439 delete I->Ty;
1440 }
1441 delete $3;
1442
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001443 CHECK_FOR_ERROR
1444 }
1445 | VOID '(' ArgTypeListI ')' OptFuncAttrs {
Duncan Sandsf5588dc2007-11-27 13:23:08 +00001446 // Allow but ignore attributes on function types; this permits auto-upgrade.
1447 // FIXME: remove in LLVM 3.0.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001448 std::vector<const Type*> Params;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001449 TypeWithAttrsList::iterator I = $3->begin(), E = $3->end();
Duncan Sandsf5588dc2007-11-27 13:23:08 +00001450 for ( ; I != E; ++I ) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001451 const Type* Ty = I->Ty->get();
1452 Params.push_back(Ty);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001453 }
Anton Korobeynikova2c02272007-12-03 19:16:54 +00001454
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001455 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1456 if (isVarArg) Params.pop_back();
1457
Anton Korobeynikove286f6d2007-12-03 21:01:29 +00001458 for (unsigned i = 0; i != Params.size(); ++i)
1459 if (!(Params[i]->isFirstClassType() || isa<OpaqueType>(Params[i])))
1460 GEN_ERROR("Function arguments must be value types!");
1461
1462 CHECK_FOR_ERROR
1463
Duncan Sandsf5588dc2007-11-27 13:23:08 +00001464 FunctionType *FT = FunctionType::get($1, Params, isVarArg);
Eric Christopher329d2672008-09-24 04:55:49 +00001465 $$ = new PATypeHolder(HandleUpRefs(FT));
Nuno Lopes896f4572008-10-05 16:49:34 +00001466
1467 // Delete the argument list
1468 for (I = $3->begin() ; I != E; ++I ) {
1469 delete I->Ty;
1470 }
1471 delete $3;
1472
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001473 CHECK_FOR_ERROR
1474 }
1475
1476 | '[' EUINT64VAL 'x' Types ']' { // Sized array type?
Dan Gohmane5febe42008-05-31 00:58:22 +00001477 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, $2)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001478 delete $4;
1479 CHECK_FOR_ERROR
1480 }
1481 | '<' EUINT64VAL 'x' Types '>' { // Vector type?
1482 const llvm::Type* ElemTy = $4->get();
1483 if ((unsigned)$2 != $2)
1484 GEN_ERROR("Unsigned result not equal to signed result");
1485 if (!ElemTy->isFloatingPoint() && !ElemTy->isInteger())
1486 GEN_ERROR("Element type of a VectorType must be primitive");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001487 $$ = new PATypeHolder(HandleUpRefs(VectorType::get(*$4, (unsigned)$2)));
1488 delete $4;
1489 CHECK_FOR_ERROR
1490 }
1491 | '{' TypeListI '}' { // Structure type?
1492 std::vector<const Type*> Elements;
1493 for (std::list<llvm::PATypeHolder>::iterator I = $2->begin(),
1494 E = $2->end(); I != E; ++I)
1495 Elements.push_back(*I);
1496
1497 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
1498 delete $2;
1499 CHECK_FOR_ERROR
1500 }
1501 | '{' '}' { // Empty structure type?
1502 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
1503 CHECK_FOR_ERROR
1504 }
1505 | '<' '{' TypeListI '}' '>' {
1506 std::vector<const Type*> Elements;
1507 for (std::list<llvm::PATypeHolder>::iterator I = $3->begin(),
1508 E = $3->end(); I != E; ++I)
1509 Elements.push_back(*I);
1510
1511 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements, true)));
1512 delete $3;
1513 CHECK_FOR_ERROR
1514 }
1515 | '<' '{' '}' '>' { // Empty structure type?
1516 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>(), true));
1517 CHECK_FOR_ERROR
1518 }
1519 ;
1520
Eric Christopher329d2672008-09-24 04:55:49 +00001521ArgType
Devang Pateld222f862008-09-25 21:00:45 +00001522 : Types OptAttributes {
Duncan Sandsf5588dc2007-11-27 13:23:08 +00001523 // Allow but ignore attributes on function types; this permits auto-upgrade.
1524 // FIXME: remove in LLVM 3.0.
Eric Christopher329d2672008-09-24 04:55:49 +00001525 $$.Ty = $1;
Devang Pateld222f862008-09-25 21:00:45 +00001526 $$.Attrs = Attribute::None;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001527 }
1528 ;
1529
1530ResultTypes
1531 : Types {
1532 if (!UpRefs.empty())
1533 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Devang Patel3d5a1e862008-02-23 01:17:37 +00001534 if (!(*$1)->isFirstClassType() && !isa<StructType>($1->get()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001535 GEN_ERROR("LLVM functions cannot return aggregate types");
1536 $$ = $1;
1537 }
1538 | VOID {
1539 $$ = new PATypeHolder(Type::VoidTy);
1540 }
1541 ;
1542
1543ArgTypeList : ArgType {
1544 $$ = new TypeWithAttrsList();
1545 $$->push_back($1);
1546 CHECK_FOR_ERROR
1547 }
1548 | ArgTypeList ',' ArgType {
1549 ($$=$1)->push_back($3);
1550 CHECK_FOR_ERROR
1551 }
1552 ;
1553
Eric Christopher329d2672008-09-24 04:55:49 +00001554ArgTypeListI
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001555 : ArgTypeList
1556 | ArgTypeList ',' DOTDOTDOT {
1557 $$=$1;
Devang Pateld222f862008-09-25 21:00:45 +00001558 TypeWithAttrs TWA; TWA.Attrs = Attribute::None;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001559 TWA.Ty = new PATypeHolder(Type::VoidTy);
1560 $$->push_back(TWA);
1561 CHECK_FOR_ERROR
1562 }
1563 | DOTDOTDOT {
1564 $$ = new TypeWithAttrsList;
Devang Pateld222f862008-09-25 21:00:45 +00001565 TypeWithAttrs TWA; TWA.Attrs = Attribute::None;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001566 TWA.Ty = new PATypeHolder(Type::VoidTy);
1567 $$->push_back(TWA);
1568 CHECK_FOR_ERROR
1569 }
1570 | /*empty*/ {
1571 $$ = new TypeWithAttrsList();
1572 CHECK_FOR_ERROR
1573 };
1574
Eric Christopher329d2672008-09-24 04:55:49 +00001575// TypeList - Used for struct declarations and as a basis for function type
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001576// declaration type lists
1577//
1578TypeListI : Types {
1579 $$ = new std::list<PATypeHolder>();
Eric Christopher329d2672008-09-24 04:55:49 +00001580 $$->push_back(*$1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001581 delete $1;
1582 CHECK_FOR_ERROR
1583 }
1584 | TypeListI ',' Types {
Eric Christopher329d2672008-09-24 04:55:49 +00001585 ($$=$1)->push_back(*$3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001586 delete $3;
1587 CHECK_FOR_ERROR
1588 };
1589
1590// ConstVal - The various declarations that go into the constant pool. This
1591// production is used ONLY to represent constants that show up AFTER a 'const',
1592// 'constant' or 'global' token at global scope. Constants that can be inlined
1593// into other expressions (such as integers and constexprs) are handled by the
1594// ResolvedVal, ValueRef and ConstValueRef productions.
1595//
1596ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
1597 if (!UpRefs.empty())
1598 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1599 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
1600 if (ATy == 0)
Eric Christopher329d2672008-09-24 04:55:49 +00001601 GEN_ERROR("Cannot make array constant with type: '" +
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001602 (*$1)->getDescription() + "'");
1603 const Type *ETy = ATy->getElementType();
Dan Gohman7185e4b2008-06-23 18:43:26 +00001604 uint64_t NumElements = ATy->getNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001605
1606 // Verify that we have the correct size...
Mon P Wang6bde9ec2008-06-25 08:15:39 +00001607 if (NumElements != uint64_t(-1) && NumElements != $3->size())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001608 GEN_ERROR("Type mismatch: constant sized array initialized with " +
Eric Christopher329d2672008-09-24 04:55:49 +00001609 utostr($3->size()) + " arguments, but has size of " +
Mon P Wang6bde9ec2008-06-25 08:15:39 +00001610 utostr(NumElements) + "");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001611
1612 // Verify all elements are correct type!
1613 for (unsigned i = 0; i < $3->size(); i++) {
1614 if (ETy != (*$3)[i]->getType())
Eric Christopher329d2672008-09-24 04:55:49 +00001615 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001616 ETy->getDescription() +"' as required!\nIt is of type '"+
1617 (*$3)[i]->getType()->getDescription() + "'.");
1618 }
1619
1620 $$ = ConstantArray::get(ATy, *$3);
1621 delete $1; delete $3;
1622 CHECK_FOR_ERROR
1623 }
1624 | Types '[' ']' {
1625 if (!UpRefs.empty())
1626 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1627 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
1628 if (ATy == 0)
Eric Christopher329d2672008-09-24 04:55:49 +00001629 GEN_ERROR("Cannot make array constant with type: '" +
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001630 (*$1)->getDescription() + "'");
1631
Dan Gohman7185e4b2008-06-23 18:43:26 +00001632 uint64_t NumElements = ATy->getNumElements();
Eric Christopher329d2672008-09-24 04:55:49 +00001633 if (NumElements != uint64_t(-1) && NumElements != 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001634 GEN_ERROR("Type mismatch: constant sized array initialized with 0"
Mon P Wang6bde9ec2008-06-25 08:15:39 +00001635 " arguments, but has size of " + utostr(NumElements) +"");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001636 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
1637 delete $1;
1638 CHECK_FOR_ERROR
1639 }
1640 | Types 'c' STRINGCONSTANT {
1641 if (!UpRefs.empty())
1642 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1643 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
1644 if (ATy == 0)
Eric Christopher329d2672008-09-24 04:55:49 +00001645 GEN_ERROR("Cannot make array constant with type: '" +
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001646 (*$1)->getDescription() + "'");
1647
Dan Gohman7185e4b2008-06-23 18:43:26 +00001648 uint64_t NumElements = ATy->getNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001649 const Type *ETy = ATy->getElementType();
Mon P Wang6bde9ec2008-06-25 08:15:39 +00001650 if (NumElements != uint64_t(-1) && NumElements != $3->length())
Eric Christopher329d2672008-09-24 04:55:49 +00001651 GEN_ERROR("Can't build string constant of size " +
Mon P Wang6bde9ec2008-06-25 08:15:39 +00001652 utostr($3->length()) +
1653 " when array has size " + utostr(NumElements) + "");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001654 std::vector<Constant*> Vals;
1655 if (ETy == Type::Int8Ty) {
Mon P Wang6bde9ec2008-06-25 08:15:39 +00001656 for (uint64_t i = 0; i < $3->length(); ++i)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001657 Vals.push_back(ConstantInt::get(ETy, (*$3)[i]));
1658 } else {
1659 delete $3;
1660 GEN_ERROR("Cannot build string arrays of non byte sized elements");
1661 }
1662 delete $3;
1663 $$ = ConstantArray::get(ATy, Vals);
1664 delete $1;
1665 CHECK_FOR_ERROR
1666 }
1667 | Types '<' ConstVector '>' { // Nonempty unsized arr
1668 if (!UpRefs.empty())
1669 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1670 const VectorType *PTy = dyn_cast<VectorType>($1->get());
1671 if (PTy == 0)
Eric Christopher329d2672008-09-24 04:55:49 +00001672 GEN_ERROR("Cannot make packed constant with type: '" +
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001673 (*$1)->getDescription() + "'");
1674 const Type *ETy = PTy->getElementType();
Dan Gohman7185e4b2008-06-23 18:43:26 +00001675 unsigned NumElements = PTy->getNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001676
1677 // Verify that we have the correct size...
Mon P Wang6bde9ec2008-06-25 08:15:39 +00001678 if (NumElements != unsigned(-1) && NumElements != (unsigned)$3->size())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001679 GEN_ERROR("Type mismatch: constant sized packed initialized with " +
Eric Christopher329d2672008-09-24 04:55:49 +00001680 utostr($3->size()) + " arguments, but has size of " +
Mon P Wang6bde9ec2008-06-25 08:15:39 +00001681 utostr(NumElements) + "");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001682
1683 // Verify all elements are correct type!
1684 for (unsigned i = 0; i < $3->size(); i++) {
1685 if (ETy != (*$3)[i]->getType())
Eric Christopher329d2672008-09-24 04:55:49 +00001686 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001687 ETy->getDescription() +"' as required!\nIt is of type '"+
1688 (*$3)[i]->getType()->getDescription() + "'.");
1689 }
1690
1691 $$ = ConstantVector::get(PTy, *$3);
1692 delete $1; delete $3;
1693 CHECK_FOR_ERROR
1694 }
1695 | Types '{' ConstVector '}' {
1696 const StructType *STy = dyn_cast<StructType>($1->get());
1697 if (STy == 0)
Eric Christopher329d2672008-09-24 04:55:49 +00001698 GEN_ERROR("Cannot make struct constant with type: '" +
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001699 (*$1)->getDescription() + "'");
1700
1701 if ($3->size() != STy->getNumContainedTypes())
1702 GEN_ERROR("Illegal number of initializers for structure type");
1703
1704 // Check to ensure that constants are compatible with the type initializer!
1705 for (unsigned i = 0, e = $3->size(); i != e; ++i)
1706 if ((*$3)[i]->getType() != STy->getElementType(i))
1707 GEN_ERROR("Expected type '" +
1708 STy->getElementType(i)->getDescription() +
1709 "' for element #" + utostr(i) +
1710 " of structure initializer");
1711
1712 // Check to ensure that Type is not packed
1713 if (STy->isPacked())
1714 GEN_ERROR("Unpacked Initializer to vector type '" +
1715 STy->getDescription() + "'");
1716
1717 $$ = ConstantStruct::get(STy, *$3);
1718 delete $1; delete $3;
1719 CHECK_FOR_ERROR
1720 }
1721 | Types '{' '}' {
1722 if (!UpRefs.empty())
1723 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1724 const StructType *STy = dyn_cast<StructType>($1->get());
1725 if (STy == 0)
Eric Christopher329d2672008-09-24 04:55:49 +00001726 GEN_ERROR("Cannot make struct constant with type: '" +
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001727 (*$1)->getDescription() + "'");
1728
1729 if (STy->getNumContainedTypes() != 0)
1730 GEN_ERROR("Illegal number of initializers for structure type");
1731
1732 // Check to ensure that Type is not packed
1733 if (STy->isPacked())
1734 GEN_ERROR("Unpacked Initializer to vector type '" +
1735 STy->getDescription() + "'");
1736
1737 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1738 delete $1;
1739 CHECK_FOR_ERROR
1740 }
1741 | Types '<' '{' ConstVector '}' '>' {
1742 const StructType *STy = dyn_cast<StructType>($1->get());
1743 if (STy == 0)
Eric Christopher329d2672008-09-24 04:55:49 +00001744 GEN_ERROR("Cannot make struct constant with type: '" +
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001745 (*$1)->getDescription() + "'");
1746
1747 if ($4->size() != STy->getNumContainedTypes())
1748 GEN_ERROR("Illegal number of initializers for structure type");
1749
1750 // Check to ensure that constants are compatible with the type initializer!
1751 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1752 if ((*$4)[i]->getType() != STy->getElementType(i))
1753 GEN_ERROR("Expected type '" +
1754 STy->getElementType(i)->getDescription() +
1755 "' for element #" + utostr(i) +
1756 " of structure initializer");
1757
1758 // Check to ensure that Type is packed
1759 if (!STy->isPacked())
Eric Christopher329d2672008-09-24 04:55:49 +00001760 GEN_ERROR("Vector initializer to non-vector type '" +
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001761 STy->getDescription() + "'");
1762
1763 $$ = ConstantStruct::get(STy, *$4);
1764 delete $1; delete $4;
1765 CHECK_FOR_ERROR
1766 }
1767 | Types '<' '{' '}' '>' {
1768 if (!UpRefs.empty())
1769 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1770 const StructType *STy = dyn_cast<StructType>($1->get());
1771 if (STy == 0)
Eric Christopher329d2672008-09-24 04:55:49 +00001772 GEN_ERROR("Cannot make struct constant with type: '" +
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001773 (*$1)->getDescription() + "'");
1774
1775 if (STy->getNumContainedTypes() != 0)
1776 GEN_ERROR("Illegal number of initializers for structure type");
1777
1778 // Check to ensure that Type is packed
1779 if (!STy->isPacked())
Eric Christopher329d2672008-09-24 04:55:49 +00001780 GEN_ERROR("Vector initializer to non-vector type '" +
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001781 STy->getDescription() + "'");
1782
1783 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1784 delete $1;
1785 CHECK_FOR_ERROR
1786 }
1787 | Types NULL_TOK {
1788 if (!UpRefs.empty())
1789 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1790 const PointerType *PTy = dyn_cast<PointerType>($1->get());
1791 if (PTy == 0)
Eric Christopher329d2672008-09-24 04:55:49 +00001792 GEN_ERROR("Cannot make null pointer constant with type: '" +
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001793 (*$1)->getDescription() + "'");
1794
1795 $$ = ConstantPointerNull::get(PTy);
1796 delete $1;
1797 CHECK_FOR_ERROR
1798 }
1799 | Types UNDEF {
1800 if (!UpRefs.empty())
1801 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1802 $$ = UndefValue::get($1->get());
1803 delete $1;
1804 CHECK_FOR_ERROR
1805 }
1806 | Types SymbolicValueRef {
1807 if (!UpRefs.empty())
1808 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1809 const PointerType *Ty = dyn_cast<PointerType>($1->get());
1810 if (Ty == 0)
Devang Patel3b8849c2008-02-19 22:27:01 +00001811 GEN_ERROR("Global const reference must be a pointer type " + (*$1)->getDescription());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001812
1813 // ConstExprs can exist in the body of a function, thus creating
1814 // GlobalValues whenever they refer to a variable. Because we are in
1815 // the context of a function, getExistingVal will search the functions
1816 // symbol table instead of the module symbol table for the global symbol,
1817 // which throws things all off. To get around this, we just tell
1818 // getExistingVal that we are at global scope here.
1819 //
1820 Function *SavedCurFn = CurFun.CurrentFunction;
1821 CurFun.CurrentFunction = 0;
1822
1823 Value *V = getExistingVal(Ty, $2);
1824 CHECK_FOR_ERROR
1825
1826 CurFun.CurrentFunction = SavedCurFn;
1827
1828 // If this is an initializer for a constant pointer, which is referencing a
1829 // (currently) undefined variable, create a stub now that shall be replaced
1830 // in the future with the right type of variable.
1831 //
1832 if (V == 0) {
1833 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1834 const PointerType *PT = cast<PointerType>(Ty);
1835
1836 // First check to see if the forward references value is already created!
1837 PerModuleInfo::GlobalRefsType::iterator I =
1838 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Eric Christopher329d2672008-09-24 04:55:49 +00001839
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001840 if (I != CurModule.GlobalRefs.end()) {
1841 V = I->second; // Placeholder already exists, use it...
1842 $2.destroy();
1843 } else {
1844 std::string Name;
1845 if ($2.Type == ValID::GlobalName)
1846 Name = $2.getName();
1847 else if ($2.Type != ValID::GlobalID)
1848 GEN_ERROR("Invalid reference to global");
1849
1850 // Create the forward referenced global.
1851 GlobalValue *GV;
Eric Christopher329d2672008-09-24 04:55:49 +00001852 if (const FunctionType *FTy =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001853 dyn_cast<FunctionType>(PT->getElementType())) {
Gabor Greif89f01162008-04-06 23:07:54 +00001854 GV = Function::Create(FTy, GlobalValue::ExternalWeakLinkage, Name,
1855 CurModule.CurrentModule);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001856 } else {
1857 GV = new GlobalVariable(PT->getElementType(), false,
1858 GlobalValue::ExternalWeakLinkage, 0,
1859 Name, CurModule.CurrentModule);
1860 }
1861
1862 // Keep track of the fact that we have a forward ref to recycle it
1863 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1864 V = GV;
1865 }
1866 }
1867
1868 $$ = cast<GlobalValue>(V);
1869 delete $1; // Free the type handle
1870 CHECK_FOR_ERROR
1871 }
1872 | Types ConstExpr {
1873 if (!UpRefs.empty())
1874 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1875 if ($1->get() != $2->getType())
Eric Christopher329d2672008-09-24 04:55:49 +00001876 GEN_ERROR("Mismatched types for constant expression: " +
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001877 (*$1)->getDescription() + " and " + $2->getType()->getDescription());
1878 $$ = $2;
1879 delete $1;
1880 CHECK_FOR_ERROR
1881 }
1882 | Types ZEROINITIALIZER {
1883 if (!UpRefs.empty())
1884 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1885 const Type *Ty = $1->get();
1886 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
1887 GEN_ERROR("Cannot create a null initialized value of this type");
1888 $$ = Constant::getNullValue(Ty);
1889 delete $1;
1890 CHECK_FOR_ERROR
1891 }
Chris Lattnerc5320232008-10-15 06:16:57 +00001892 | Types ESINT64VAL { // integral constants
1893 if (IntegerType *IT = dyn_cast<IntegerType>($1->get())) {
1894 if (!ConstantInt::isValueValidForType(IT, $2))
1895 GEN_ERROR("Constant value doesn't fit in type");
1896 $$ = ConstantInt::get(IT, $2, true);
1897 } else {
1898 GEN_ERROR("integer constant must have integer type");
1899 }
1900 delete $1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001901 CHECK_FOR_ERROR
1902 }
Chris Lattnerc5320232008-10-15 06:16:57 +00001903 | Types ESAPINTVAL { // arbitrary precision integer constants
1904 if (IntegerType *IT = dyn_cast<IntegerType>($1->get())) {
1905 if ($2->getBitWidth() > IT->getBitWidth())
1906 GEN_ERROR("Constant value does not fit in type");
1907 $2->sextOrTrunc(IT->getBitWidth());
1908 $$ = ConstantInt::get(*$2);
1909 } else {
1910 GEN_ERROR("integer constant must have integer type");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001911 }
Chris Lattnerc5320232008-10-15 06:16:57 +00001912 delete $1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001913 delete $2;
1914 CHECK_FOR_ERROR
1915 }
Chris Lattnerc5320232008-10-15 06:16:57 +00001916 | Types EUINT64VAL { // integral constants
1917 if (IntegerType *IT = dyn_cast<IntegerType>($1->get())) {
1918 if (!ConstantInt::isValueValidForType(IT, $2))
1919 GEN_ERROR("Constant value doesn't fit in type");
1920 $$ = ConstantInt::get(IT, $2, false);
1921 } else {
1922 GEN_ERROR("integer constant must have integer type");
Eric Christopher329d2672008-09-24 04:55:49 +00001923 }
Chris Lattnerc5320232008-10-15 06:16:57 +00001924 delete $1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001925 CHECK_FOR_ERROR
1926 }
Chris Lattnerc5320232008-10-15 06:16:57 +00001927 | Types EUAPINTVAL { // arbitrary precision integer constants
1928 if (IntegerType *IT = dyn_cast<IntegerType>($1->get())) {
1929 if ($2->getBitWidth() > IT->getBitWidth())
1930 GEN_ERROR("Constant value does not fit in type");
1931 $2->zextOrTrunc(IT->getBitWidth());
1932 $$ = ConstantInt::get(*$2);
1933 } else {
1934 GEN_ERROR("integer constant must have integer type");
1935 }
1936
1937 delete $2;
1938 delete $1;
1939 CHECK_FOR_ERROR
1940 }
1941 | Types TRUETOK { // Boolean constants
1942 if ($1->get() != Type::Int1Ty)
Dan Gohmane5febe42008-05-31 00:58:22 +00001943 GEN_ERROR("Constant true must have type i1");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001944 $$ = ConstantInt::getTrue();
Chris Lattnerc5320232008-10-15 06:16:57 +00001945 delete $1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001946 CHECK_FOR_ERROR
1947 }
Chris Lattnerc5320232008-10-15 06:16:57 +00001948 | Types FALSETOK { // Boolean constants
1949 if ($1->get() != Type::Int1Ty)
Dan Gohmane5febe42008-05-31 00:58:22 +00001950 GEN_ERROR("Constant false must have type i1");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001951 $$ = ConstantInt::getFalse();
Chris Lattnerc5320232008-10-15 06:16:57 +00001952 delete $1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001953 CHECK_FOR_ERROR
1954 }
Chris Lattnerc5320232008-10-15 06:16:57 +00001955 | Types FPVAL { // Floating point constants
1956 if (!ConstantFP::isValueValidForType($1->get(), *$2))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001957 GEN_ERROR("Floating point constant invalid for type");
Chris Lattnerc5320232008-10-15 06:16:57 +00001958
Eric Christopher329d2672008-09-24 04:55:49 +00001959 // Lexer has no type info, so builds all float and double FP constants
Dale Johannesen255b8fe2007-09-11 18:33:39 +00001960 // as double. Fix this here. Long double is done right.
Chris Lattnerc5320232008-10-15 06:16:57 +00001961 if (&$2->getSemantics()==&APFloat::IEEEdouble && $1->get()==Type::FloatTy) {
Dale Johannesen5ba85fd2008-10-09 23:01:34 +00001962 bool ignored;
1963 $2->convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven,
1964 &ignored);
1965 }
Chris Lattner05ba86e2008-04-20 00:41:19 +00001966 $$ = ConstantFP::get(*$2);
Chris Lattnerc5320232008-10-15 06:16:57 +00001967 delete $1;
Dale Johannesen3afee192007-09-07 21:07:57 +00001968 delete $2;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001969 CHECK_FOR_ERROR
1970 };
1971
1972
1973ConstExpr: CastOps '(' ConstVal TO Types ')' {
1974 if (!UpRefs.empty())
1975 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
1976 Constant *Val = $3;
1977 const Type *DestTy = $5->get();
1978 if (!CastInst::castIsValid($1, $3, DestTy))
1979 GEN_ERROR("invalid cast opcode for cast from '" +
1980 Val->getType()->getDescription() + "' to '" +
Eric Christopher329d2672008-09-24 04:55:49 +00001981 DestTy->getDescription() + "'");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001982 $$ = ConstantExpr::getCast($1, $3, DestTy);
1983 delete $5;
1984 }
1985 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1986 if (!isa<PointerType>($3->getType()))
1987 GEN_ERROR("GetElementPtr requires a pointer operand");
1988
1989 const Type *IdxTy =
Dan Gohman8055f772008-05-15 19:50:34 +00001990 GetElementPtrInst::getIndexedType($3->getType(), $4->begin(), $4->end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001991 if (!IdxTy)
1992 GEN_ERROR("Index list invalid for constant getelementptr");
1993
1994 SmallVector<Constant*, 8> IdxVec;
1995 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1996 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
1997 IdxVec.push_back(C);
1998 else
1999 GEN_ERROR("Indices to constant getelementptr must be constants");
2000
2001 delete $4;
2002
2003 $$ = ConstantExpr::getGetElementPtr($3, &IdxVec[0], IdxVec.size());
2004 CHECK_FOR_ERROR
2005 }
2006 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
2007 if ($3->getType() != Type::Int1Ty)
2008 GEN_ERROR("Select condition must be of boolean type");
2009 if ($5->getType() != $7->getType())
2010 GEN_ERROR("Select operand types must match");
2011 $$ = ConstantExpr::getSelect($3, $5, $7);
2012 CHECK_FOR_ERROR
2013 }
2014 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
2015 if ($3->getType() != $5->getType())
2016 GEN_ERROR("Binary operator types must match");
2017 CHECK_FOR_ERROR;
2018 $$ = ConstantExpr::get($1, $3, $5);
2019 }
2020 | LogicalOps '(' ConstVal ',' ConstVal ')' {
2021 if ($3->getType() != $5->getType())
2022 GEN_ERROR("Logical operator types must match");
2023 if (!$3->getType()->isInteger()) {
Eric Christopher329d2672008-09-24 04:55:49 +00002024 if (!isa<VectorType>($3->getType()) ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002025 !cast<VectorType>($3->getType())->getElementType()->isInteger())
2026 GEN_ERROR("Logical operator requires integral operands");
2027 }
2028 $$ = ConstantExpr::get($1, $3, $5);
2029 CHECK_FOR_ERROR
2030 }
2031 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
2032 if ($4->getType() != $6->getType())
2033 GEN_ERROR("icmp operand types must match");
2034 $$ = ConstantExpr::getICmp($2, $4, $6);
2035 }
2036 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
2037 if ($4->getType() != $6->getType())
2038 GEN_ERROR("fcmp operand types must match");
2039 $$ = ConstantExpr::getFCmp($2, $4, $6);
2040 }
Nate Begeman646fa482008-05-12 19:01:56 +00002041 | VICMP IPredicates '(' ConstVal ',' ConstVal ')' {
2042 if ($4->getType() != $6->getType())
2043 GEN_ERROR("vicmp operand types must match");
2044 $$ = ConstantExpr::getVICmp($2, $4, $6);
2045 }
2046 | VFCMP FPredicates '(' ConstVal ',' ConstVal ')' {
2047 if ($4->getType() != $6->getType())
2048 GEN_ERROR("vfcmp operand types must match");
2049 $$ = ConstantExpr::getVFCmp($2, $4, $6);
2050 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002051 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
2052 if (!ExtractElementInst::isValidOperands($3, $5))
2053 GEN_ERROR("Invalid extractelement operands");
2054 $$ = ConstantExpr::getExtractElement($3, $5);
2055 CHECK_FOR_ERROR
2056 }
2057 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
2058 if (!InsertElementInst::isValidOperands($3, $5, $7))
2059 GEN_ERROR("Invalid insertelement operands");
2060 $$ = ConstantExpr::getInsertElement($3, $5, $7);
2061 CHECK_FOR_ERROR
2062 }
2063 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
2064 if (!ShuffleVectorInst::isValidOperands($3, $5, $7))
2065 GEN_ERROR("Invalid shufflevector operands");
2066 $$ = ConstantExpr::getShuffleVector($3, $5, $7);
2067 CHECK_FOR_ERROR
Dan Gohmane6b1ee62008-05-23 01:55:30 +00002068 }
Dan Gohmane5febe42008-05-31 00:58:22 +00002069 | EXTRACTVALUE '(' ConstVal ConstantIndexList ')' {
Dan Gohmane6b1ee62008-05-23 01:55:30 +00002070 if (!isa<StructType>($3->getType()) && !isa<ArrayType>($3->getType()))
2071 GEN_ERROR("ExtractValue requires an aggregate operand");
2072
Dan Gohmane5febe42008-05-31 00:58:22 +00002073 $$ = ConstantExpr::getExtractValue($3, &(*$4)[0], $4->size());
Dan Gohmane6b1ee62008-05-23 01:55:30 +00002074 delete $4;
Dan Gohmane6b1ee62008-05-23 01:55:30 +00002075 CHECK_FOR_ERROR
2076 }
Dan Gohmane5febe42008-05-31 00:58:22 +00002077 | INSERTVALUE '(' ConstVal ',' ConstVal ConstantIndexList ')' {
Dan Gohmane6b1ee62008-05-23 01:55:30 +00002078 if (!isa<StructType>($3->getType()) && !isa<ArrayType>($3->getType()))
2079 GEN_ERROR("InsertValue requires an aggregate operand");
2080
Dan Gohmane5febe42008-05-31 00:58:22 +00002081 $$ = ConstantExpr::getInsertValue($3, $5, &(*$6)[0], $6->size());
Dan Gohmane6b1ee62008-05-23 01:55:30 +00002082 delete $6;
Dan Gohmane6b1ee62008-05-23 01:55:30 +00002083 CHECK_FOR_ERROR
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002084 };
2085
2086
2087// ConstVector - A list of comma separated constants.
2088ConstVector : ConstVector ',' ConstVal {
2089 ($$ = $1)->push_back($3);
2090 CHECK_FOR_ERROR
2091 }
2092 | ConstVal {
2093 $$ = new std::vector<Constant*>();
2094 $$->push_back($1);
2095 CHECK_FOR_ERROR
2096 };
2097
2098
2099// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
2100GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
2101
Eric Christopher329d2672008-09-24 04:55:49 +00002102// ThreadLocal
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002103ThreadLocal : THREAD_LOCAL { $$ = true; } | { $$ = false; };
2104
2105// AliaseeRef - Match either GlobalValue or bitcast to GlobalValue.
2106AliaseeRef : ResultTypes SymbolicValueRef {
2107 const Type* VTy = $1->get();
2108 Value *V = getVal(VTy, $2);
Chris Lattnerbb856a32007-08-06 21:00:46 +00002109 CHECK_FOR_ERROR
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002110 GlobalValue* Aliasee = dyn_cast<GlobalValue>(V);
2111 if (!Aliasee)
2112 GEN_ERROR("Aliases can be created only to global values");
2113
2114 $$ = Aliasee;
2115 CHECK_FOR_ERROR
2116 delete $1;
2117 }
2118 | BITCAST '(' AliaseeRef TO Types ')' {
2119 Constant *Val = $3;
2120 const Type *DestTy = $5->get();
2121 if (!CastInst::castIsValid($1, $3, DestTy))
2122 GEN_ERROR("invalid cast opcode for cast from '" +
2123 Val->getType()->getDescription() + "' to '" +
2124 DestTy->getDescription() + "'");
Eric Christopher329d2672008-09-24 04:55:49 +00002125
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002126 $$ = ConstantExpr::getCast($1, $3, DestTy);
2127 CHECK_FOR_ERROR
2128 delete $5;
2129 };
2130
2131//===----------------------------------------------------------------------===//
2132// Rules to match Modules
2133//===----------------------------------------------------------------------===//
2134
2135// Module rule: Capture the result of parsing the whole file into a result
2136// variable...
2137//
Eric Christopher329d2672008-09-24 04:55:49 +00002138Module
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002139 : DefinitionList {
2140 $$ = ParserResult = CurModule.CurrentModule;
2141 CurModule.ModuleDone();
2142 CHECK_FOR_ERROR;
2143 }
2144 | /*empty*/ {
2145 $$ = ParserResult = CurModule.CurrentModule;
2146 CurModule.ModuleDone();
2147 CHECK_FOR_ERROR;
2148 }
2149 ;
2150
2151DefinitionList
2152 : Definition
2153 | DefinitionList Definition
2154 ;
2155
Eric Christopher329d2672008-09-24 04:55:49 +00002156Definition
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002157 : DEFINE { CurFun.isDeclare = false; } Function {
2158 CurFun.FunctionDone();
2159 CHECK_FOR_ERROR
2160 }
2161 | DECLARE { CurFun.isDeclare = true; } FunctionProto {
2162 CHECK_FOR_ERROR
2163 }
2164 | MODULE ASM_TOK AsmBlock {
2165 CHECK_FOR_ERROR
Eric Christopher329d2672008-09-24 04:55:49 +00002166 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002167 | OptLocalAssign TYPE Types {
2168 if (!UpRefs.empty())
2169 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
2170 // Eagerly resolve types. This is not an optimization, this is a
2171 // requirement that is due to the fact that we could have this:
2172 //
2173 // %list = type { %list * }
2174 // %list = type { %list * } ; repeated type decl
2175 //
2176 // If types are not resolved eagerly, then the two types will not be
2177 // determined to be the same type!
2178 //
2179 ResolveTypeTo($1, *$3);
2180
2181 if (!setTypeName(*$3, $1) && !$1) {
2182 CHECK_FOR_ERROR
2183 // If this is a named type that is not a redefinition, add it to the slot
2184 // table.
2185 CurModule.Types.push_back(*$3);
2186 }
2187
2188 delete $3;
2189 CHECK_FOR_ERROR
2190 }
2191 | OptLocalAssign TYPE VOID {
2192 ResolveTypeTo($1, $3);
2193
2194 if (!setTypeName($3, $1) && !$1) {
2195 CHECK_FOR_ERROR
2196 // If this is a named type that is not a redefinition, add it to the slot
2197 // table.
2198 CurModule.Types.push_back($3);
2199 }
2200 CHECK_FOR_ERROR
2201 }
Eric Christopher329d2672008-09-24 04:55:49 +00002202 | OptGlobalAssign GVVisibilityStyle ThreadLocal GlobalType ConstVal
2203 OptAddrSpace {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002204 /* "Externally Visible" Linkage */
Eric Christopher329d2672008-09-24 04:55:49 +00002205 if ($5 == 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002206 GEN_ERROR("Global value initializer is not a constant");
2207 CurGV = ParseGlobalVariable($1, GlobalValue::ExternalLinkage,
Christopher Lamb668d9a02007-12-12 08:45:45 +00002208 $2, $4, $5->getType(), $5, $3, $6);
Christopher Lamb0a243582007-12-11 09:02:08 +00002209 CHECK_FOR_ERROR
2210 } GlobalVarAttributes {
2211 CurGV = 0;
2212 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002213 | OptGlobalAssign GVInternalLinkage GVVisibilityStyle ThreadLocal GlobalType
Christopher Lamb668d9a02007-12-12 08:45:45 +00002214 ConstVal OptAddrSpace {
Eric Christopher329d2672008-09-24 04:55:49 +00002215 if ($6 == 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002216 GEN_ERROR("Global value initializer is not a constant");
Christopher Lamb668d9a02007-12-12 08:45:45 +00002217 CurGV = ParseGlobalVariable($1, $2, $3, $5, $6->getType(), $6, $4, $7);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002218 CHECK_FOR_ERROR
2219 } GlobalVarAttributes {
2220 CurGV = 0;
2221 }
2222 | OptGlobalAssign GVExternalLinkage GVVisibilityStyle ThreadLocal GlobalType
Christopher Lamb668d9a02007-12-12 08:45:45 +00002223 Types OptAddrSpace {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002224 if (!UpRefs.empty())
2225 GEN_ERROR("Invalid upreference in type: " + (*$6)->getDescription());
Christopher Lamb668d9a02007-12-12 08:45:45 +00002226 CurGV = ParseGlobalVariable($1, $2, $3, $5, *$6, 0, $4, $7);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002227 CHECK_FOR_ERROR
2228 delete $6;
2229 } GlobalVarAttributes {
2230 CurGV = 0;
2231 CHECK_FOR_ERROR
2232 }
2233 | OptGlobalAssign GVVisibilityStyle ALIAS AliasLinkage AliaseeRef {
2234 std::string Name;
2235 if ($1) {
2236 Name = *$1;
2237 delete $1;
2238 }
2239 if (Name.empty())
2240 GEN_ERROR("Alias name cannot be empty");
Eric Christopher329d2672008-09-24 04:55:49 +00002241
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002242 Constant* Aliasee = $5;
2243 if (Aliasee == 0)
2244 GEN_ERROR(std::string("Invalid aliasee for alias: ") + Name);
2245
2246 GlobalAlias* GA = new GlobalAlias(Aliasee->getType(), $4, Name, Aliasee,
2247 CurModule.CurrentModule);
2248 GA->setVisibility($2);
2249 InsertValue(GA, CurModule.Values);
Eric Christopher329d2672008-09-24 04:55:49 +00002250
2251
Chris Lattner5eefce32007-09-10 23:24:14 +00002252 // If there was a forward reference of this alias, resolve it now.
Eric Christopher329d2672008-09-24 04:55:49 +00002253
Chris Lattner5eefce32007-09-10 23:24:14 +00002254 ValID ID;
2255 if (!Name.empty())
2256 ID = ValID::createGlobalName(Name);
2257 else
2258 ID = ValID::createGlobalID(CurModule.Values.size()-1);
Eric Christopher329d2672008-09-24 04:55:49 +00002259
Chris Lattner5eefce32007-09-10 23:24:14 +00002260 if (GlobalValue *FWGV =
2261 CurModule.GetForwardRefForGlobal(GA->getType(), ID)) {
2262 // Replace uses of the fwdref with the actual alias.
2263 FWGV->replaceAllUsesWith(GA);
2264 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(FWGV))
2265 GV->eraseFromParent();
2266 else
2267 cast<Function>(FWGV)->eraseFromParent();
2268 }
2269 ID.destroy();
Eric Christopher329d2672008-09-24 04:55:49 +00002270
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002271 CHECK_FOR_ERROR
2272 }
Eric Christopher329d2672008-09-24 04:55:49 +00002273 | TARGET TargetDefinition {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002274 CHECK_FOR_ERROR
2275 }
2276 | DEPLIBS '=' LibrariesDefinition {
2277 CHECK_FOR_ERROR
2278 }
2279 ;
2280
2281
2282AsmBlock : STRINGCONSTANT {
2283 const std::string &AsmSoFar = CurModule.CurrentModule->getModuleInlineAsm();
2284 if (AsmSoFar.empty())
2285 CurModule.CurrentModule->setModuleInlineAsm(*$1);
2286 else
2287 CurModule.CurrentModule->setModuleInlineAsm(AsmSoFar+"\n"+*$1);
2288 delete $1;
2289 CHECK_FOR_ERROR
2290};
2291
2292TargetDefinition : TRIPLE '=' STRINGCONSTANT {
2293 CurModule.CurrentModule->setTargetTriple(*$3);
2294 delete $3;
2295 }
2296 | DATALAYOUT '=' STRINGCONSTANT {
2297 CurModule.CurrentModule->setDataLayout(*$3);
2298 delete $3;
2299 };
2300
2301LibrariesDefinition : '[' LibList ']';
2302
2303LibList : LibList ',' STRINGCONSTANT {
2304 CurModule.CurrentModule->addLibrary(*$3);
2305 delete $3;
2306 CHECK_FOR_ERROR
2307 }
2308 | STRINGCONSTANT {
2309 CurModule.CurrentModule->addLibrary(*$1);
2310 delete $1;
2311 CHECK_FOR_ERROR
2312 }
2313 | /* empty: end of list */ {
2314 CHECK_FOR_ERROR
2315 }
2316 ;
2317
2318//===----------------------------------------------------------------------===//
2319// Rules to match Function Headers
2320//===----------------------------------------------------------------------===//
2321
Devang Pateld222f862008-09-25 21:00:45 +00002322ArgListH : ArgListH ',' Types OptAttributes OptLocalName {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002323 if (!UpRefs.empty())
2324 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Dan Gohmane5febe42008-05-31 00:58:22 +00002325 if (!(*$3)->isFirstClassType())
2326 GEN_ERROR("Argument types must be first-class");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002327 ArgListEntry E; E.Attrs = $4; E.Ty = $3; E.Name = $5;
2328 $$ = $1;
2329 $1->push_back(E);
2330 CHECK_FOR_ERROR
2331 }
Devang Pateld222f862008-09-25 21:00:45 +00002332 | Types OptAttributes OptLocalName {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002333 if (!UpRefs.empty())
2334 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Dan Gohmane5febe42008-05-31 00:58:22 +00002335 if (!(*$1)->isFirstClassType())
2336 GEN_ERROR("Argument types must be first-class");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002337 ArgListEntry E; E.Attrs = $2; E.Ty = $1; E.Name = $3;
2338 $$ = new ArgListType;
2339 $$->push_back(E);
2340 CHECK_FOR_ERROR
2341 };
2342
2343ArgList : ArgListH {
2344 $$ = $1;
2345 CHECK_FOR_ERROR
2346 }
2347 | ArgListH ',' DOTDOTDOT {
2348 $$ = $1;
2349 struct ArgListEntry E;
2350 E.Ty = new PATypeHolder(Type::VoidTy);
2351 E.Name = 0;
Devang Pateld222f862008-09-25 21:00:45 +00002352 E.Attrs = Attribute::None;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002353 $$->push_back(E);
2354 CHECK_FOR_ERROR
2355 }
2356 | DOTDOTDOT {
2357 $$ = new ArgListType;
2358 struct ArgListEntry E;
2359 E.Ty = new PATypeHolder(Type::VoidTy);
2360 E.Name = 0;
Devang Pateld222f862008-09-25 21:00:45 +00002361 E.Attrs = Attribute::None;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002362 $$->push_back(E);
2363 CHECK_FOR_ERROR
2364 }
2365 | /* empty */ {
2366 $$ = 0;
2367 CHECK_FOR_ERROR
2368 };
2369
Devang Patelcd842482008-09-29 20:49:50 +00002370FunctionHeaderH : OptCallingConv OptRetAttrs ResultTypes GlobalName '(' ArgList ')'
Devang Patel008cd3e2008-09-26 23:51:19 +00002371 OptFuncAttrs OptSection OptAlign OptGC {
Devang Patelcd842482008-09-29 20:49:50 +00002372 std::string FunctionName(*$4);
2373 delete $4; // Free strdup'd memory!
Eric Christopher329d2672008-09-24 04:55:49 +00002374
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002375 // Check the function result for abstractness if this is a define. We should
2376 // have no abstract types at this point
Devang Patelcd842482008-09-29 20:49:50 +00002377 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved($3))
2378 GEN_ERROR("Reference to abstract result: "+ $3->get()->getDescription());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002379
Devang Patelcd842482008-09-29 20:49:50 +00002380 if (!FunctionType::isValidReturnType(*$3))
Chris Lattner73de3c02008-04-23 05:37:08 +00002381 GEN_ERROR("Invalid result type for LLVM function");
Eric Christopher329d2672008-09-24 04:55:49 +00002382
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002383 std::vector<const Type*> ParamTypeList;
Devang Pateld222f862008-09-25 21:00:45 +00002384 SmallVector<AttributeWithIndex, 8> Attrs;
Devang Patelf2a4a922008-09-26 22:53:05 +00002385 //FIXME : In 3.0, stop accepting zext, sext and inreg as optional function
2386 //attributes.
Devang Patelcd842482008-09-29 20:49:50 +00002387 Attributes RetAttrs = $2;
2388 if ($8 != Attribute::None) {
2389 if ($8 & Attribute::ZExt) {
Devang Patelf2a4a922008-09-26 22:53:05 +00002390 RetAttrs = RetAttrs | Attribute::ZExt;
Devang Patelcd842482008-09-29 20:49:50 +00002391 $8 = $8 ^ Attribute::ZExt;
Devang Patelf2a4a922008-09-26 22:53:05 +00002392 }
Devang Patelcd842482008-09-29 20:49:50 +00002393 if ($8 & Attribute::SExt) {
Devang Patelf2a4a922008-09-26 22:53:05 +00002394 RetAttrs = RetAttrs | Attribute::SExt;
Devang Patelcd842482008-09-29 20:49:50 +00002395 $8 = $8 ^ Attribute::SExt;
Devang Patelf2a4a922008-09-26 22:53:05 +00002396 }
Devang Patelcd842482008-09-29 20:49:50 +00002397 if ($8 & Attribute::InReg) {
Devang Patelf2a4a922008-09-26 22:53:05 +00002398 RetAttrs = RetAttrs | Attribute::InReg;
Devang Patelcd842482008-09-29 20:49:50 +00002399 $8 = $8 ^ Attribute::InReg;
Devang Patelf2a4a922008-09-26 22:53:05 +00002400 }
Devang Patelf2a4a922008-09-26 22:53:05 +00002401 }
Devang Patelcd842482008-09-29 20:49:50 +00002402 if (RetAttrs != Attribute::None)
2403 Attrs.push_back(AttributeWithIndex::get(0, RetAttrs));
2404 if ($6) { // If there are arguments...
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002405 unsigned index = 1;
Devang Patelcd842482008-09-29 20:49:50 +00002406 for (ArgListType::iterator I = $6->begin(); I != $6->end(); ++I, ++index) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002407 const Type* Ty = I->Ty->get();
2408 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved(I->Ty))
2409 GEN_ERROR("Reference to abstract argument: " + Ty->getDescription());
2410 ParamTypeList.push_back(Ty);
Devang Pateld222f862008-09-25 21:00:45 +00002411 if (Ty != Type::VoidTy && I->Attrs != Attribute::None)
2412 Attrs.push_back(AttributeWithIndex::get(index, I->Attrs));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002413 }
2414 }
Devang Patelcd842482008-09-29 20:49:50 +00002415 if ($8 != Attribute::None)
2416 Attrs.push_back(AttributeWithIndex::get(~0, $8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002417
2418 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
2419 if (isVarArg) ParamTypeList.pop_back();
2420
Devang Pateld222f862008-09-25 21:00:45 +00002421 AttrListPtr PAL;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002422 if (!Attrs.empty())
Devang Pateld222f862008-09-25 21:00:45 +00002423 PAL = AttrListPtr::get(Attrs.begin(), Attrs.end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002424
Devang Patelcd842482008-09-29 20:49:50 +00002425 FunctionType *FT = FunctionType::get(*$3, ParamTypeList, isVarArg);
Christopher Lambfb623c62007-12-17 01:17:35 +00002426 const PointerType *PFT = PointerType::getUnqual(FT);
Devang Patelcd842482008-09-29 20:49:50 +00002427 delete $3;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002428
2429 ValID ID;
2430 if (!FunctionName.empty()) {
2431 ID = ValID::createGlobalName((char*)FunctionName.c_str());
2432 } else {
2433 ID = ValID::createGlobalID(CurModule.Values.size());
2434 }
2435
2436 Function *Fn = 0;
2437 // See if this function was forward referenced. If so, recycle the object.
2438 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
Eric Christopher329d2672008-09-24 04:55:49 +00002439 // Move the function to the end of the list, from whereever it was
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002440 // previously inserted.
2441 Fn = cast<Function>(FWRef);
Devang Pateld222f862008-09-25 21:00:45 +00002442 assert(Fn->getAttributes().isEmpty() &&
Chris Lattner1c8733e2008-03-12 17:45:29 +00002443 "Forward reference has parameter attributes!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002444 CurModule.CurrentModule->getFunctionList().remove(Fn);
2445 CurModule.CurrentModule->getFunctionList().push_back(Fn);
2446 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
2447 (Fn = CurModule.CurrentModule->getFunction(FunctionName))) {
Duncan Sandsf5588dc2007-11-27 13:23:08 +00002448 if (Fn->getFunctionType() != FT ) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002449 // The existing function doesn't have the same type. This is an overload
2450 // error.
2451 GEN_ERROR("Overload of function '" + FunctionName + "' not permitted.");
Devang Pateld222f862008-09-25 21:00:45 +00002452 } else if (Fn->getAttributes() != PAL) {
Duncan Sandsf5588dc2007-11-27 13:23:08 +00002453 // The existing function doesn't have the same parameter attributes.
2454 // This is an overload error.
2455 GEN_ERROR("Overload of function '" + FunctionName + "' not permitted.");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002456 } else if (!CurFun.isDeclare && !Fn->isDeclaration()) {
2457 // Neither the existing or the current function is a declaration and they
2458 // have the same name and same type. Clearly this is a redefinition.
2459 GEN_ERROR("Redefinition of function '" + FunctionName + "'");
Duncan Sandsf5588dc2007-11-27 13:23:08 +00002460 } else if (Fn->isDeclaration()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002461 // Make sure to strip off any argument names so we can't get conflicts.
2462 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
2463 AI != AE; ++AI)
2464 AI->setName("");
2465 }
2466 } else { // Not already defined?
Gabor Greif89f01162008-04-06 23:07:54 +00002467 Fn = Function::Create(FT, GlobalValue::ExternalWeakLinkage, FunctionName,
2468 CurModule.CurrentModule);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002469 InsertValue(Fn, CurModule.Values);
2470 }
2471
Nuno Lopese20dbca2008-10-03 15:45:58 +00002472 ID.destroy();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002473 CurFun.FunctionStart(Fn);
2474
2475 if (CurFun.isDeclare) {
2476 // If we have declaration, always overwrite linkage. This will allow us to
2477 // correctly handle cases, when pointer to function is passed as argument to
2478 // another function.
2479 Fn->setLinkage(CurFun.Linkage);
2480 Fn->setVisibility(CurFun.Visibility);
2481 }
2482 Fn->setCallingConv($1);
Devang Pateld222f862008-09-25 21:00:45 +00002483 Fn->setAttributes(PAL);
Devang Patelcd842482008-09-29 20:49:50 +00002484 Fn->setAlignment($10);
2485 if ($9) {
2486 Fn->setSection(*$9);
2487 delete $9;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002488 }
Devang Patelcd842482008-09-29 20:49:50 +00002489 if ($11) {
2490 Fn->setGC($11->c_str());
2491 delete $11;
Gordon Henriksen13fe5e32007-12-10 03:18:06 +00002492 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002493
2494 // Add all of the arguments we parsed to the function...
Devang Patelcd842482008-09-29 20:49:50 +00002495 if ($6) { // Is null if empty...
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002496 if (isVarArg) { // Nuke the last entry
Devang Patelcd842482008-09-29 20:49:50 +00002497 assert($6->back().Ty->get() == Type::VoidTy && $6->back().Name == 0 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002498 "Not a varargs marker!");
Devang Patelcd842482008-09-29 20:49:50 +00002499 delete $6->back().Ty;
2500 $6->pop_back(); // Delete the last entry
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002501 }
2502 Function::arg_iterator ArgIt = Fn->arg_begin();
2503 Function::arg_iterator ArgEnd = Fn->arg_end();
2504 unsigned Idx = 1;
Devang Patelcd842482008-09-29 20:49:50 +00002505 for (ArgListType::iterator I = $6->begin();
2506 I != $6->end() && ArgIt != ArgEnd; ++I, ++ArgIt) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002507 delete I->Ty; // Delete the typeholder...
2508 setValueName(ArgIt, I->Name); // Insert arg into symtab...
2509 CHECK_FOR_ERROR
2510 InsertValue(ArgIt);
2511 Idx++;
2512 }
2513
Devang Patelcd842482008-09-29 20:49:50 +00002514 delete $6; // We're now done with the argument list
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002515 }
2516 CHECK_FOR_ERROR
2517};
2518
2519BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
2520
2521FunctionHeader : FunctionDefineLinkage GVVisibilityStyle FunctionHeaderH BEGIN {
2522 $$ = CurFun.CurrentFunction;
2523
2524 // Make sure that we keep track of the linkage type even if there was a
2525 // previous "declare".
2526 $$->setLinkage($1);
2527 $$->setVisibility($2);
2528};
2529
2530END : ENDTOK | '}'; // Allow end of '}' to end a function
2531
2532Function : BasicBlockList END {
2533 $$ = $1;
2534 CHECK_FOR_ERROR
2535};
2536
2537FunctionProto : FunctionDeclareLinkage GVVisibilityStyle FunctionHeaderH {
2538 CurFun.CurrentFunction->setLinkage($1);
2539 CurFun.CurrentFunction->setVisibility($2);
2540 $$ = CurFun.CurrentFunction;
2541 CurFun.FunctionDone();
2542 CHECK_FOR_ERROR
2543 };
2544
2545//===----------------------------------------------------------------------===//
2546// Rules to match Basic Blocks
2547//===----------------------------------------------------------------------===//
2548
2549OptSideEffect : /* empty */ {
2550 $$ = false;
2551 CHECK_FOR_ERROR
2552 }
2553 | SIDEEFFECT {
2554 $$ = true;
2555 CHECK_FOR_ERROR
2556 };
2557
2558ConstValueRef : ESINT64VAL { // A reference to a direct constant
2559 $$ = ValID::create($1);
2560 CHECK_FOR_ERROR
2561 }
2562 | EUINT64VAL {
2563 $$ = ValID::create($1);
2564 CHECK_FOR_ERROR
2565 }
Chris Lattnerf3d40022008-07-11 00:30:39 +00002566 | ESAPINTVAL { // arbitrary precision integer constants
2567 $$ = ValID::create(*$1, true);
2568 delete $1;
2569 CHECK_FOR_ERROR
Eric Christopher329d2672008-09-24 04:55:49 +00002570 }
Chris Lattnerf3d40022008-07-11 00:30:39 +00002571 | EUAPINTVAL { // arbitrary precision integer constants
2572 $$ = ValID::create(*$1, false);
2573 delete $1;
2574 CHECK_FOR_ERROR
2575 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002576 | FPVAL { // Perhaps it's an FP constant?
2577 $$ = ValID::create($1);
2578 CHECK_FOR_ERROR
2579 }
2580 | TRUETOK {
2581 $$ = ValID::create(ConstantInt::getTrue());
2582 CHECK_FOR_ERROR
Eric Christopher329d2672008-09-24 04:55:49 +00002583 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002584 | FALSETOK {
2585 $$ = ValID::create(ConstantInt::getFalse());
2586 CHECK_FOR_ERROR
2587 }
2588 | NULL_TOK {
2589 $$ = ValID::createNull();
2590 CHECK_FOR_ERROR
2591 }
2592 | UNDEF {
2593 $$ = ValID::createUndef();
2594 CHECK_FOR_ERROR
2595 }
2596 | ZEROINITIALIZER { // A vector zero constant.
2597 $$ = ValID::createZeroInit();
2598 CHECK_FOR_ERROR
2599 }
2600 | '<' ConstVector '>' { // Nonempty unsized packed vector
2601 const Type *ETy = (*$2)[0]->getType();
Eric Christopher329d2672008-09-24 04:55:49 +00002602 unsigned NumElements = $2->size();
Dan Gohmane5febe42008-05-31 00:58:22 +00002603
2604 if (!ETy->isInteger() && !ETy->isFloatingPoint())
2605 GEN_ERROR("Invalid vector element type: " + ETy->getDescription());
Eric Christopher329d2672008-09-24 04:55:49 +00002606
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002607 VectorType* pt = VectorType::get(ETy, NumElements);
Dan Gohman9fc6cb02008-06-09 14:45:02 +00002608 PATypeHolder* PTy = new PATypeHolder(HandleUpRefs(pt));
Eric Christopher329d2672008-09-24 04:55:49 +00002609
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002610 // Verify all elements are correct type!
2611 for (unsigned i = 0; i < $2->size(); i++) {
2612 if (ETy != (*$2)[i]->getType())
Eric Christopher329d2672008-09-24 04:55:49 +00002613 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002614 ETy->getDescription() +"' as required!\nIt is of type '" +
2615 (*$2)[i]->getType()->getDescription() + "'.");
2616 }
2617
2618 $$ = ValID::create(ConstantVector::get(pt, *$2));
2619 delete PTy; delete $2;
2620 CHECK_FOR_ERROR
2621 }
Dan Gohman9fc6cb02008-06-09 14:45:02 +00002622 | '[' ConstVector ']' { // Nonempty unsized arr
2623 const Type *ETy = (*$2)[0]->getType();
Eric Christopher329d2672008-09-24 04:55:49 +00002624 uint64_t NumElements = $2->size();
Dan Gohman9fc6cb02008-06-09 14:45:02 +00002625
2626 if (!ETy->isFirstClassType())
2627 GEN_ERROR("Invalid array element type: " + ETy->getDescription());
2628
2629 ArrayType *ATy = ArrayType::get(ETy, NumElements);
2630 PATypeHolder* PTy = new PATypeHolder(HandleUpRefs(ATy));
2631
2632 // Verify all elements are correct type!
2633 for (unsigned i = 0; i < $2->size(); i++) {
2634 if (ETy != (*$2)[i]->getType())
Eric Christopher329d2672008-09-24 04:55:49 +00002635 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Dan Gohman9fc6cb02008-06-09 14:45:02 +00002636 ETy->getDescription() +"' as required!\nIt is of type '"+
2637 (*$2)[i]->getType()->getDescription() + "'.");
2638 }
2639
2640 $$ = ValID::create(ConstantArray::get(ATy, *$2));
2641 delete PTy; delete $2;
2642 CHECK_FOR_ERROR
2643 }
2644 | '[' ']' {
Dan Gohman7185e4b2008-06-23 18:43:26 +00002645 // Use undef instead of an array because it's inconvenient to determine
2646 // the element type at this point, there being no elements to examine.
Dan Gohman9fc6cb02008-06-09 14:45:02 +00002647 $$ = ValID::createUndef();
2648 CHECK_FOR_ERROR
2649 }
2650 | 'c' STRINGCONSTANT {
Dan Gohman7185e4b2008-06-23 18:43:26 +00002651 uint64_t NumElements = $2->length();
Dan Gohman9fc6cb02008-06-09 14:45:02 +00002652 const Type *ETy = Type::Int8Ty;
2653
2654 ArrayType *ATy = ArrayType::get(ETy, NumElements);
2655
2656 std::vector<Constant*> Vals;
2657 for (unsigned i = 0; i < $2->length(); ++i)
2658 Vals.push_back(ConstantInt::get(ETy, (*$2)[i]));
2659 delete $2;
2660 $$ = ValID::create(ConstantArray::get(ATy, Vals));
2661 CHECK_FOR_ERROR
2662 }
2663 | '{' ConstVector '}' {
2664 std::vector<const Type*> Elements($2->size());
2665 for (unsigned i = 0, e = $2->size(); i != e; ++i)
2666 Elements[i] = (*$2)[i]->getType();
2667
2668 const StructType *STy = StructType::get(Elements);
2669 PATypeHolder* PTy = new PATypeHolder(HandleUpRefs(STy));
2670
2671 $$ = ValID::create(ConstantStruct::get(STy, *$2));
2672 delete PTy; delete $2;
2673 CHECK_FOR_ERROR
2674 }
2675 | '{' '}' {
2676 const StructType *STy = StructType::get(std::vector<const Type*>());
2677 $$ = ValID::create(ConstantStruct::get(STy, std::vector<Constant*>()));
2678 CHECK_FOR_ERROR
2679 }
2680 | '<' '{' ConstVector '}' '>' {
2681 std::vector<const Type*> Elements($3->size());
2682 for (unsigned i = 0, e = $3->size(); i != e; ++i)
2683 Elements[i] = (*$3)[i]->getType();
2684
2685 const StructType *STy = StructType::get(Elements, /*isPacked=*/true);
2686 PATypeHolder* PTy = new PATypeHolder(HandleUpRefs(STy));
2687
2688 $$ = ValID::create(ConstantStruct::get(STy, *$3));
2689 delete PTy; delete $3;
2690 CHECK_FOR_ERROR
2691 }
2692 | '<' '{' '}' '>' {
2693 const StructType *STy = StructType::get(std::vector<const Type*>(),
2694 /*isPacked=*/true);
2695 $$ = ValID::create(ConstantStruct::get(STy, std::vector<Constant*>()));
2696 CHECK_FOR_ERROR
2697 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002698 | ConstExpr {
2699 $$ = ValID::create($1);
2700 CHECK_FOR_ERROR
2701 }
2702 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
2703 $$ = ValID::createInlineAsm(*$3, *$5, $2);
2704 delete $3;
2705 delete $5;
2706 CHECK_FOR_ERROR
2707 };
2708
2709// SymbolicValueRef - Reference to one of two ways of symbolically refering to
2710// another value.
2711//
2712SymbolicValueRef : LOCALVAL_ID { // Is it an integer reference...?
2713 $$ = ValID::createLocalID($1);
2714 CHECK_FOR_ERROR
2715 }
2716 | GLOBALVAL_ID {
2717 $$ = ValID::createGlobalID($1);
2718 CHECK_FOR_ERROR
2719 }
2720 | LocalName { // Is it a named reference...?
2721 $$ = ValID::createLocalName(*$1);
2722 delete $1;
2723 CHECK_FOR_ERROR
2724 }
2725 | GlobalName { // Is it a named reference...?
2726 $$ = ValID::createGlobalName(*$1);
2727 delete $1;
2728 CHECK_FOR_ERROR
2729 };
2730
2731// ValueRef - A reference to a definition... either constant or symbolic
2732ValueRef : SymbolicValueRef | ConstValueRef;
2733
2734
2735// ResolvedVal - a <type> <value> pair. This is used only in cases where the
2736// type immediately preceeds the value reference, and allows complex constant
2737// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
2738ResolvedVal : Types ValueRef {
2739 if (!UpRefs.empty())
2740 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Eric Christopher329d2672008-09-24 04:55:49 +00002741 $$ = getVal(*$1, $2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002742 delete $1;
2743 CHECK_FOR_ERROR
2744 }
2745 ;
2746
Devang Patelbf507402008-02-20 22:40:23 +00002747ReturnedVal : ResolvedVal {
2748 $$ = new std::vector<Value *>();
Eric Christopher329d2672008-09-24 04:55:49 +00002749 $$->push_back($1);
Devang Patelbf507402008-02-20 22:40:23 +00002750 CHECK_FOR_ERROR
2751 }
Devang Patel087fe2b2008-02-23 00:38:56 +00002752 | ReturnedVal ',' ResolvedVal {
Eric Christopher329d2672008-09-24 04:55:49 +00002753 ($$=$1)->push_back($3);
Devang Patelbf507402008-02-20 22:40:23 +00002754 CHECK_FOR_ERROR
2755 };
2756
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002757BasicBlockList : BasicBlockList BasicBlock {
2758 $$ = $1;
2759 CHECK_FOR_ERROR
2760 }
Eric Christopher329d2672008-09-24 04:55:49 +00002761 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002762 $$ = $1;
2763 CHECK_FOR_ERROR
2764 };
2765
2766
Eric Christopher329d2672008-09-24 04:55:49 +00002767// Basic blocks are terminated by branching instructions:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002768// br, br/cc, switch, ret
2769//
Chris Lattner906773a2008-08-29 17:20:18 +00002770BasicBlock : InstructionList OptLocalAssign BBTerminatorInst {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002771 setValueName($3, $2);
2772 CHECK_FOR_ERROR
2773 InsertValue($3);
2774 $1->getInstList().push_back($3);
2775 $$ = $1;
2776 CHECK_FOR_ERROR
2777 };
2778
Chris Lattner906773a2008-08-29 17:20:18 +00002779BasicBlock : InstructionList LocalNumber BBTerminatorInst {
2780 CHECK_FOR_ERROR
2781 int ValNum = InsertValue($3);
2782 if (ValNum != (int)$2)
2783 GEN_ERROR("Result value number %" + utostr($2) +
2784 " is incorrect, expected %" + utostr((unsigned)ValNum));
Eric Christopher329d2672008-09-24 04:55:49 +00002785
Chris Lattner906773a2008-08-29 17:20:18 +00002786 $1->getInstList().push_back($3);
2787 $$ = $1;
2788 CHECK_FOR_ERROR
2789};
2790
2791
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002792InstructionList : InstructionList Inst {
2793 if (CastInst *CI1 = dyn_cast<CastInst>($2))
2794 if (CastInst *CI2 = dyn_cast<CastInst>(CI1->getOperand(0)))
2795 if (CI2->getParent() == 0)
2796 $1->getInstList().push_back(CI2);
2797 $1->getInstList().push_back($2);
2798 $$ = $1;
2799 CHECK_FOR_ERROR
2800 }
2801 | /* empty */ { // Empty space between instruction lists
Nick Lewyckyd8aa33a2008-04-25 16:53:59 +00002802 $$ = defineBBVal(ValID::createLocalID(CurFun.NextValNum));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002803 CHECK_FOR_ERROR
2804 }
2805 | LABELSTR { // Labelled (named) basic block
Nick Lewyckyd8aa33a2008-04-25 16:53:59 +00002806 $$ = defineBBVal(ValID::createLocalName(*$1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002807 delete $1;
2808 CHECK_FOR_ERROR
Nick Lewyckyd8aa33a2008-04-25 16:53:59 +00002809
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002810 };
2811
Eric Christopher329d2672008-09-24 04:55:49 +00002812BBTerminatorInst :
Devang Patelbf507402008-02-20 22:40:23 +00002813 RET ReturnedVal { // Return with a result...
Devang Patelda2c8d52008-02-26 22:17:48 +00002814 ValueList &VL = *$2;
Devang Patelb4851dc2008-02-26 23:19:08 +00002815 assert(!VL.empty() && "Invalid ret operands!");
Dan Gohmanb94a0ba2008-07-23 00:54:54 +00002816 const Type *ReturnType = CurFun.CurrentFunction->getReturnType();
2817 if (VL.size() > 1 ||
2818 (isa<StructType>(ReturnType) &&
2819 (VL.empty() || VL[0]->getType() != ReturnType))) {
2820 Value *RV = UndefValue::get(ReturnType);
2821 for (unsigned i = 0, e = VL.size(); i != e; ++i) {
2822 Instruction *I = InsertValueInst::Create(RV, VL[i], i, "mrv");
2823 ($<BasicBlockVal>-1)->getInstList().push_back(I);
2824 RV = I;
2825 }
2826 $$ = ReturnInst::Create(RV);
2827 } else {
2828 $$ = ReturnInst::Create(VL[0]);
2829 }
Devang Patelbf507402008-02-20 22:40:23 +00002830 delete $2;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002831 CHECK_FOR_ERROR
2832 }
2833 | RET VOID { // Return with no result...
Gabor Greif89f01162008-04-06 23:07:54 +00002834 $$ = ReturnInst::Create();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002835 CHECK_FOR_ERROR
2836 }
2837 | BR LABEL ValueRef { // Unconditional Branch...
2838 BasicBlock* tmpBB = getBBVal($3);
2839 CHECK_FOR_ERROR
Gabor Greif89f01162008-04-06 23:07:54 +00002840 $$ = BranchInst::Create(tmpBB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002841 } // Conditional Branch...
Eric Christopher329d2672008-09-24 04:55:49 +00002842 | BR INTTYPE ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Dan Gohmane5febe42008-05-31 00:58:22 +00002843 if (cast<IntegerType>($2)->getBitWidth() != 1)
2844 GEN_ERROR("Branch condition must have type i1");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002845 BasicBlock* tmpBBA = getBBVal($6);
2846 CHECK_FOR_ERROR
2847 BasicBlock* tmpBBB = getBBVal($9);
2848 CHECK_FOR_ERROR
2849 Value* tmpVal = getVal(Type::Int1Ty, $3);
2850 CHECK_FOR_ERROR
Gabor Greif89f01162008-04-06 23:07:54 +00002851 $$ = BranchInst::Create(tmpBBA, tmpBBB, tmpVal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002852 }
Chris Lattner8f5544c2008-10-15 06:03:48 +00002853 | SWITCH INTTYPE ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002854 Value* tmpVal = getVal($2, $3);
2855 CHECK_FOR_ERROR
2856 BasicBlock* tmpBB = getBBVal($6);
2857 CHECK_FOR_ERROR
Gabor Greif89f01162008-04-06 23:07:54 +00002858 SwitchInst *S = SwitchInst::Create(tmpVal, tmpBB, $8->size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002859 $$ = S;
2860
2861 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
2862 E = $8->end();
2863 for (; I != E; ++I) {
2864 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
2865 S->addCase(CI, I->second);
2866 else
2867 GEN_ERROR("Switch case is constant, but not a simple integer");
2868 }
2869 delete $8;
2870 CHECK_FOR_ERROR
2871 }
Chris Lattner8f5544c2008-10-15 06:03:48 +00002872 | SWITCH INTTYPE ValueRef ',' LABEL ValueRef '[' ']' {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002873 Value* tmpVal = getVal($2, $3);
2874 CHECK_FOR_ERROR
2875 BasicBlock* tmpBB = getBBVal($6);
2876 CHECK_FOR_ERROR
Gabor Greif89f01162008-04-06 23:07:54 +00002877 SwitchInst *S = SwitchInst::Create(tmpVal, tmpBB, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002878 $$ = S;
2879 CHECK_FOR_ERROR
2880 }
Devang Patelcd842482008-09-29 20:49:50 +00002881 | INVOKE OptCallingConv OptRetAttrs ResultTypes ValueRef '(' ParamList ')'
2882 OptFuncAttrs TO LABEL ValueRef UNWIND LABEL ValueRef {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002883
2884 // Handle the short syntax
2885 const PointerType *PFTy = 0;
2886 const FunctionType *Ty = 0;
Devang Patelcd842482008-09-29 20:49:50 +00002887 if (!(PFTy = dyn_cast<PointerType>($4->get())) ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002888 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
2889 // Pull out the types of all of the arguments...
2890 std::vector<const Type*> ParamTypes;
Devang Patelcd842482008-09-29 20:49:50 +00002891 ParamList::iterator I = $7->begin(), E = $7->end();
Duncan Sandsf5588dc2007-11-27 13:23:08 +00002892 for (; I != E; ++I) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002893 const Type *Ty = I->Val->getType();
2894 if (Ty == Type::VoidTy)
2895 GEN_ERROR("Short call syntax cannot be used with varargs");
2896 ParamTypes.push_back(Ty);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002897 }
Eric Christopher329d2672008-09-24 04:55:49 +00002898
Devang Patelcd842482008-09-29 20:49:50 +00002899 if (!FunctionType::isValidReturnType(*$4))
Chris Lattner73de3c02008-04-23 05:37:08 +00002900 GEN_ERROR("Invalid result type for LLVM function");
2901
Devang Patelcd842482008-09-29 20:49:50 +00002902 Ty = FunctionType::get($4->get(), ParamTypes, false);
Christopher Lambfb623c62007-12-17 01:17:35 +00002903 PFTy = PointerType::getUnqual(Ty);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002904 }
2905
Devang Patelcd842482008-09-29 20:49:50 +00002906 delete $4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002907
Devang Patelcd842482008-09-29 20:49:50 +00002908 Value *V = getVal(PFTy, $5); // Get the function we're calling...
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002909 CHECK_FOR_ERROR
Devang Patelcd842482008-09-29 20:49:50 +00002910 BasicBlock *Normal = getBBVal($12);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002911 CHECK_FOR_ERROR
Devang Patelcd842482008-09-29 20:49:50 +00002912 BasicBlock *Except = getBBVal($15);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002913 CHECK_FOR_ERROR
2914
Devang Pateld222f862008-09-25 21:00:45 +00002915 SmallVector<AttributeWithIndex, 8> Attrs;
Devang Patelf2a4a922008-09-26 22:53:05 +00002916 //FIXME : In 3.0, stop accepting zext, sext and inreg as optional function
2917 //attributes.
Devang Patelcd842482008-09-29 20:49:50 +00002918 Attributes RetAttrs = $3;
2919 if ($9 != Attribute::None) {
2920 if ($9 & Attribute::ZExt) {
Devang Patelf2a4a922008-09-26 22:53:05 +00002921 RetAttrs = RetAttrs | Attribute::ZExt;
Devang Patelcd842482008-09-29 20:49:50 +00002922 $9 = $9 ^ Attribute::ZExt;
Devang Patelf2a4a922008-09-26 22:53:05 +00002923 }
Devang Patelcd842482008-09-29 20:49:50 +00002924 if ($9 & Attribute::SExt) {
Devang Patelf2a4a922008-09-26 22:53:05 +00002925 RetAttrs = RetAttrs | Attribute::SExt;
Devang Patelcd842482008-09-29 20:49:50 +00002926 $9 = $9 ^ Attribute::SExt;
Devang Patelf2a4a922008-09-26 22:53:05 +00002927 }
Devang Patelcd842482008-09-29 20:49:50 +00002928 if ($9 & Attribute::InReg) {
Devang Patelf2a4a922008-09-26 22:53:05 +00002929 RetAttrs = RetAttrs | Attribute::InReg;
Devang Patelcd842482008-09-29 20:49:50 +00002930 $9 = $9 ^ Attribute::InReg;
Devang Patelf2a4a922008-09-26 22:53:05 +00002931 }
Devang Patelf2a4a922008-09-26 22:53:05 +00002932 }
Devang Patelcd842482008-09-29 20:49:50 +00002933 if (RetAttrs != Attribute::None)
2934 Attrs.push_back(AttributeWithIndex::get(0, RetAttrs));
Devang Patelf2a4a922008-09-26 22:53:05 +00002935
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002936 // Check the arguments
2937 ValueList Args;
Devang Patelcd842482008-09-29 20:49:50 +00002938 if ($7->empty()) { // Has no arguments?
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002939 // Make sure no arguments is a good thing!
2940 if (Ty->getNumParams() != 0)
2941 GEN_ERROR("No arguments passed to a function that "
2942 "expects arguments");
2943 } else { // Has arguments?
2944 // Loop through FunctionType's arguments and ensure they are specified
2945 // correctly!
2946 FunctionType::param_iterator I = Ty->param_begin();
2947 FunctionType::param_iterator E = Ty->param_end();
Devang Patelcd842482008-09-29 20:49:50 +00002948 ParamList::iterator ArgI = $7->begin(), ArgE = $7->end();
Duncan Sandsf5588dc2007-11-27 13:23:08 +00002949 unsigned index = 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002950
Duncan Sandsf5588dc2007-11-27 13:23:08 +00002951 for (; ArgI != ArgE && I != E; ++ArgI, ++I, ++index) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002952 if (ArgI->Val->getType() != *I)
2953 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
2954 (*I)->getDescription() + "'");
2955 Args.push_back(ArgI->Val);
Devang Pateld222f862008-09-25 21:00:45 +00002956 if (ArgI->Attrs != Attribute::None)
2957 Attrs.push_back(AttributeWithIndex::get(index, ArgI->Attrs));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002958 }
2959
2960 if (Ty->isVarArg()) {
2961 if (I == E)
Chris Lattner59363a32008-02-19 04:36:25 +00002962 for (; ArgI != ArgE; ++ArgI, ++index) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002963 Args.push_back(ArgI->Val); // push the remaining varargs
Devang Pateld222f862008-09-25 21:00:45 +00002964 if (ArgI->Attrs != Attribute::None)
2965 Attrs.push_back(AttributeWithIndex::get(index, ArgI->Attrs));
Chris Lattner59363a32008-02-19 04:36:25 +00002966 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002967 } else if (I != E || ArgI != ArgE)
2968 GEN_ERROR("Invalid number of parameters detected");
2969 }
Devang Patelcd842482008-09-29 20:49:50 +00002970 if ($9 != Attribute::None)
2971 Attrs.push_back(AttributeWithIndex::get(~0, $9));
Devang Pateld222f862008-09-25 21:00:45 +00002972 AttrListPtr PAL;
Duncan Sandsf5588dc2007-11-27 13:23:08 +00002973 if (!Attrs.empty())
Devang Pateld222f862008-09-25 21:00:45 +00002974 PAL = AttrListPtr::get(Attrs.begin(), Attrs.end());
Duncan Sandsf5588dc2007-11-27 13:23:08 +00002975
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002976 // Create the InvokeInst
Dan Gohman8055f772008-05-15 19:50:34 +00002977 InvokeInst *II = InvokeInst::Create(V, Normal, Except,
2978 Args.begin(), Args.end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002979 II->setCallingConv($2);
Devang Pateld222f862008-09-25 21:00:45 +00002980 II->setAttributes(PAL);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002981 $$ = II;
Devang Patelcd842482008-09-29 20:49:50 +00002982 delete $7;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002983 CHECK_FOR_ERROR
2984 }
2985 | UNWIND {
2986 $$ = new UnwindInst();
2987 CHECK_FOR_ERROR
2988 }
2989 | UNREACHABLE {
2990 $$ = new UnreachableInst();
2991 CHECK_FOR_ERROR
2992 };
2993
2994
2995
Chris Lattner8f5544c2008-10-15 06:03:48 +00002996JumpTable : JumpTable INTTYPE ConstValueRef ',' LABEL ValueRef {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002997 $$ = $1;
2998 Constant *V = cast<Constant>(getExistingVal($2, $3));
2999 CHECK_FOR_ERROR
3000 if (V == 0)
3001 GEN_ERROR("May only switch on a constant pool value");
3002
3003 BasicBlock* tmpBB = getBBVal($6);
3004 CHECK_FOR_ERROR
3005 $$->push_back(std::make_pair(V, tmpBB));
3006 }
Chris Lattner8f5544c2008-10-15 06:03:48 +00003007 | INTTYPE ConstValueRef ',' LABEL ValueRef {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003008 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
3009 Constant *V = cast<Constant>(getExistingVal($1, $2));
3010 CHECK_FOR_ERROR
3011
3012 if (V == 0)
3013 GEN_ERROR("May only switch on a constant pool value");
3014
3015 BasicBlock* tmpBB = getBBVal($5);
3016 CHECK_FOR_ERROR
Eric Christopher329d2672008-09-24 04:55:49 +00003017 $$->push_back(std::make_pair(V, tmpBB));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003018 };
3019
3020Inst : OptLocalAssign InstVal {
3021 // Is this definition named?? if so, assign the name...
3022 setValueName($2, $1);
3023 CHECK_FOR_ERROR
3024 InsertValue($2);
3025 $$ = $2;
3026 CHECK_FOR_ERROR
3027 };
3028
Chris Lattner906773a2008-08-29 17:20:18 +00003029Inst : LocalNumber InstVal {
3030 CHECK_FOR_ERROR
3031 int ValNum = InsertValue($2);
Eric Christopher329d2672008-09-24 04:55:49 +00003032
Chris Lattner906773a2008-08-29 17:20:18 +00003033 if (ValNum != (int)$1)
3034 GEN_ERROR("Result value number %" + utostr($1) +
3035 " is incorrect, expected %" + utostr((unsigned)ValNum));
3036
3037 $$ = $2;
3038 CHECK_FOR_ERROR
3039 };
3040
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003041
3042PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
3043 if (!UpRefs.empty())
3044 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
3045 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
3046 Value* tmpVal = getVal(*$1, $3);
3047 CHECK_FOR_ERROR
3048 BasicBlock* tmpBB = getBBVal($5);
3049 CHECK_FOR_ERROR
3050 $$->push_back(std::make_pair(tmpVal, tmpBB));
3051 delete $1;
3052 }
3053 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
3054 $$ = $1;
3055 Value* tmpVal = getVal($1->front().first->getType(), $4);
3056 CHECK_FOR_ERROR
3057 BasicBlock* tmpBB = getBBVal($6);
3058 CHECK_FOR_ERROR
3059 $1->push_back(std::make_pair(tmpVal, tmpBB));
3060 };
3061
3062
Devang Pateld222f862008-09-25 21:00:45 +00003063ParamList : Types OptAttributes ValueRef OptAttributes {
3064 // FIXME: Remove trailing OptAttributes in LLVM 3.0, it was a mistake in 2.0
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003065 if (!UpRefs.empty())
3066 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
3067 // Used for call and invoke instructions
Dale Johannesencfb19e62007-11-05 21:20:28 +00003068 $$ = new ParamList();
Duncan Sandsf5588dc2007-11-27 13:23:08 +00003069 ParamListEntry E; E.Attrs = $2 | $4; E.Val = getVal($1->get(), $3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003070 $$->push_back(E);
3071 delete $1;
Duncan Sandsf5588dc2007-11-27 13:23:08 +00003072 CHECK_FOR_ERROR
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003073 }
Devang Pateld222f862008-09-25 21:00:45 +00003074 | LABEL OptAttributes ValueRef OptAttributes {
3075 // FIXME: Remove trailing OptAttributes in LLVM 3.0, it was a mistake in 2.0
Dale Johannesencfb19e62007-11-05 21:20:28 +00003076 // Labels are only valid in ASMs
3077 $$ = new ParamList();
Duncan Sandsf5588dc2007-11-27 13:23:08 +00003078 ParamListEntry E; E.Attrs = $2 | $4; E.Val = getBBVal($3);
Dale Johannesencfb19e62007-11-05 21:20:28 +00003079 $$->push_back(E);
Duncan Sandsf5588dc2007-11-27 13:23:08 +00003080 CHECK_FOR_ERROR
Dale Johannesencfb19e62007-11-05 21:20:28 +00003081 }
Devang Pateld222f862008-09-25 21:00:45 +00003082 | ParamList ',' Types OptAttributes ValueRef OptAttributes {
3083 // FIXME: Remove trailing OptAttributes in LLVM 3.0, it was a mistake in 2.0
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003084 if (!UpRefs.empty())
3085 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
3086 $$ = $1;
Duncan Sandsf5588dc2007-11-27 13:23:08 +00003087 ParamListEntry E; E.Attrs = $4 | $6; E.Val = getVal($3->get(), $5);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003088 $$->push_back(E);
3089 delete $3;
3090 CHECK_FOR_ERROR
3091 }
Devang Pateld222f862008-09-25 21:00:45 +00003092 | ParamList ',' LABEL OptAttributes ValueRef OptAttributes {
3093 // FIXME: Remove trailing OptAttributes in LLVM 3.0, it was a mistake in 2.0
Dale Johannesencfb19e62007-11-05 21:20:28 +00003094 $$ = $1;
Duncan Sandsf5588dc2007-11-27 13:23:08 +00003095 ParamListEntry E; E.Attrs = $4 | $6; E.Val = getBBVal($5);
Dale Johannesencfb19e62007-11-05 21:20:28 +00003096 $$->push_back(E);
3097 CHECK_FOR_ERROR
3098 }
3099 | /*empty*/ { $$ = new ParamList(); };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003100
3101IndexList // Used for gep instructions and constant expressions
3102 : /*empty*/ { $$ = new std::vector<Value*>(); }
3103 | IndexList ',' ResolvedVal {
3104 $$ = $1;
3105 $$->push_back($3);
3106 CHECK_FOR_ERROR
3107 }
3108 ;
3109
Dan Gohmane5febe42008-05-31 00:58:22 +00003110ConstantIndexList // Used for insertvalue and extractvalue instructions
3111 : ',' EUINT64VAL {
3112 $$ = new std::vector<unsigned>();
3113 if ((unsigned)$2 != $2)
3114 GEN_ERROR("Index " + utostr($2) + " is not valid for insertvalue or extractvalue.");
3115 $$->push_back($2);
3116 }
3117 | ConstantIndexList ',' EUINT64VAL {
3118 $$ = $1;
3119 if ((unsigned)$3 != $3)
3120 GEN_ERROR("Index " + utostr($3) + " is not valid for insertvalue or extractvalue.");
3121 $$->push_back($3);
3122 CHECK_FOR_ERROR
3123 }
3124 ;
3125
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003126OptTailCall : TAIL CALL {
3127 $$ = true;
3128 CHECK_FOR_ERROR
3129 }
3130 | CALL {
3131 $$ = false;
3132 CHECK_FOR_ERROR
3133 };
3134
3135InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
3136 if (!UpRefs.empty())
3137 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Eric Christopher329d2672008-09-24 04:55:49 +00003138 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003139 !isa<VectorType>((*$2).get()))
3140 GEN_ERROR(
3141 "Arithmetic operator requires integer, FP, or packed operands");
Eric Christopher329d2672008-09-24 04:55:49 +00003142 Value* val1 = getVal(*$2, $3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003143 CHECK_FOR_ERROR
3144 Value* val2 = getVal(*$2, $5);
3145 CHECK_FOR_ERROR
Dan Gohmane6b1ee62008-05-23 01:55:30 +00003146 $$ = BinaryOperator::Create($1, val1, val2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003147 if ($$ == 0)
3148 GEN_ERROR("binary operator returned null");
3149 delete $2;
3150 }
3151 | LogicalOps Types ValueRef ',' ValueRef {
3152 if (!UpRefs.empty())
3153 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
3154 if (!(*$2)->isInteger()) {
Nate Begemanbb1ce942008-07-29 15:49:41 +00003155 if (!isa<VectorType>($2->get()) ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003156 !cast<VectorType>($2->get())->getElementType()->isInteger())
3157 GEN_ERROR("Logical operator requires integral operands");
3158 }
3159 Value* tmpVal1 = getVal(*$2, $3);
3160 CHECK_FOR_ERROR
3161 Value* tmpVal2 = getVal(*$2, $5);
3162 CHECK_FOR_ERROR
Dan Gohmane6b1ee62008-05-23 01:55:30 +00003163 $$ = BinaryOperator::Create($1, tmpVal1, tmpVal2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003164 if ($$ == 0)
3165 GEN_ERROR("binary operator returned null");
3166 delete $2;
3167 }
3168 | ICMP IPredicates Types ValueRef ',' ValueRef {
3169 if (!UpRefs.empty())
3170 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003171 Value* tmpVal1 = getVal(*$3, $4);
3172 CHECK_FOR_ERROR
3173 Value* tmpVal2 = getVal(*$3, $6);
3174 CHECK_FOR_ERROR
Dan Gohmane6b1ee62008-05-23 01:55:30 +00003175 $$ = CmpInst::Create($1, $2, tmpVal1, tmpVal2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003176 if ($$ == 0)
3177 GEN_ERROR("icmp operator returned null");
3178 delete $3;
3179 }
3180 | FCMP FPredicates Types ValueRef ',' ValueRef {
3181 if (!UpRefs.empty())
3182 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003183 Value* tmpVal1 = getVal(*$3, $4);
3184 CHECK_FOR_ERROR
3185 Value* tmpVal2 = getVal(*$3, $6);
3186 CHECK_FOR_ERROR
Dan Gohmane6b1ee62008-05-23 01:55:30 +00003187 $$ = CmpInst::Create($1, $2, tmpVal1, tmpVal2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003188 if ($$ == 0)
3189 GEN_ERROR("fcmp operator returned null");
3190 delete $3;
3191 }
Nate Begeman646fa482008-05-12 19:01:56 +00003192 | VICMP IPredicates Types ValueRef ',' ValueRef {
3193 if (!UpRefs.empty())
3194 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
3195 if (!isa<VectorType>((*$3).get()))
3196 GEN_ERROR("Scalar types not supported by vicmp instruction");
3197 Value* tmpVal1 = getVal(*$3, $4);
3198 CHECK_FOR_ERROR
3199 Value* tmpVal2 = getVal(*$3, $6);
3200 CHECK_FOR_ERROR
Dan Gohmane6b1ee62008-05-23 01:55:30 +00003201 $$ = CmpInst::Create($1, $2, tmpVal1, tmpVal2);
Nate Begeman646fa482008-05-12 19:01:56 +00003202 if ($$ == 0)
Dan Gohman181f4e42008-09-09 01:13:24 +00003203 GEN_ERROR("vicmp operator returned null");
Nate Begeman646fa482008-05-12 19:01:56 +00003204 delete $3;
3205 }
3206 | VFCMP FPredicates Types ValueRef ',' ValueRef {
3207 if (!UpRefs.empty())
3208 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
3209 if (!isa<VectorType>((*$3).get()))
3210 GEN_ERROR("Scalar types not supported by vfcmp instruction");
3211 Value* tmpVal1 = getVal(*$3, $4);
3212 CHECK_FOR_ERROR
3213 Value* tmpVal2 = getVal(*$3, $6);
3214 CHECK_FOR_ERROR
Dan Gohmane6b1ee62008-05-23 01:55:30 +00003215 $$ = CmpInst::Create($1, $2, tmpVal1, tmpVal2);
Nate Begeman646fa482008-05-12 19:01:56 +00003216 if ($$ == 0)
Dan Gohman181f4e42008-09-09 01:13:24 +00003217 GEN_ERROR("vfcmp operator returned null");
Nate Begeman646fa482008-05-12 19:01:56 +00003218 delete $3;
3219 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003220 | CastOps ResolvedVal TO Types {
3221 if (!UpRefs.empty())
3222 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
3223 Value* Val = $2;
3224 const Type* DestTy = $4->get();
3225 if (!CastInst::castIsValid($1, Val, DestTy))
3226 GEN_ERROR("invalid cast opcode for cast from '" +
3227 Val->getType()->getDescription() + "' to '" +
Eric Christopher329d2672008-09-24 04:55:49 +00003228 DestTy->getDescription() + "'");
Dan Gohmane6b1ee62008-05-23 01:55:30 +00003229 $$ = CastInst::Create($1, Val, DestTy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003230 delete $4;
3231 }
3232 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Dan Gohman181f4e42008-09-09 01:13:24 +00003233 if (isa<VectorType>($2->getType())) {
3234 // vector select
3235 if (!isa<VectorType>($4->getType())
3236 || !isa<VectorType>($6->getType()) )
3237 GEN_ERROR("vector select value types must be vector types");
3238 const VectorType* cond_type = cast<VectorType>($2->getType());
3239 const VectorType* select_type = cast<VectorType>($4->getType());
3240 if (cond_type->getElementType() != Type::Int1Ty)
3241 GEN_ERROR("vector select condition element type must be boolean");
3242 if (cond_type->getNumElements() != select_type->getNumElements())
3243 GEN_ERROR("vector select number of elements must be the same");
3244 } else {
3245 if ($2->getType() != Type::Int1Ty)
3246 GEN_ERROR("select condition must be boolean");
3247 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003248 if ($4->getType() != $6->getType())
Dan Gohman181f4e42008-09-09 01:13:24 +00003249 GEN_ERROR("select value types must match");
Gabor Greif89f01162008-04-06 23:07:54 +00003250 $$ = SelectInst::Create($2, $4, $6);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003251 CHECK_FOR_ERROR
3252 }
3253 | VAARG ResolvedVal ',' Types {
3254 if (!UpRefs.empty())
3255 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
3256 $$ = new VAArgInst($2, *$4);
3257 delete $4;
3258 CHECK_FOR_ERROR
3259 }
3260 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
3261 if (!ExtractElementInst::isValidOperands($2, $4))
3262 GEN_ERROR("Invalid extractelement operands");
3263 $$ = new ExtractElementInst($2, $4);
3264 CHECK_FOR_ERROR
3265 }
3266 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
3267 if (!InsertElementInst::isValidOperands($2, $4, $6))
3268 GEN_ERROR("Invalid insertelement operands");
Gabor Greif89f01162008-04-06 23:07:54 +00003269 $$ = InsertElementInst::Create($2, $4, $6);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003270 CHECK_FOR_ERROR
3271 }
3272 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
3273 if (!ShuffleVectorInst::isValidOperands($2, $4, $6))
3274 GEN_ERROR("Invalid shufflevector operands");
3275 $$ = new ShuffleVectorInst($2, $4, $6);
3276 CHECK_FOR_ERROR
3277 }
3278 | PHI_TOK PHIList {
3279 const Type *Ty = $2->front().first->getType();
3280 if (!Ty->isFirstClassType())
3281 GEN_ERROR("PHI node operands must be of first class type");
Gabor Greif89f01162008-04-06 23:07:54 +00003282 $$ = PHINode::Create(Ty);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003283 ((PHINode*)$$)->reserveOperandSpace($2->size());
3284 while ($2->begin() != $2->end()) {
Eric Christopher329d2672008-09-24 04:55:49 +00003285 if ($2->front().first->getType() != Ty)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003286 GEN_ERROR("All elements of a PHI node must be of the same type");
3287 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
3288 $2->pop_front();
3289 }
3290 delete $2; // Free the list...
3291 CHECK_FOR_ERROR
3292 }
Devang Patelcd842482008-09-29 20:49:50 +00003293 | OptTailCall OptCallingConv OptRetAttrs ResultTypes ValueRef '(' ParamList ')'
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003294 OptFuncAttrs {
3295
3296 // Handle the short syntax
3297 const PointerType *PFTy = 0;
3298 const FunctionType *Ty = 0;
Devang Patelcd842482008-09-29 20:49:50 +00003299 if (!(PFTy = dyn_cast<PointerType>($4->get())) ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003300 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
3301 // Pull out the types of all of the arguments...
3302 std::vector<const Type*> ParamTypes;
Devang Patelcd842482008-09-29 20:49:50 +00003303 ParamList::iterator I = $7->begin(), E = $7->end();
Duncan Sandsf5588dc2007-11-27 13:23:08 +00003304 for (; I != E; ++I) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003305 const Type *Ty = I->Val->getType();
3306 if (Ty == Type::VoidTy)
3307 GEN_ERROR("Short call syntax cannot be used with varargs");
3308 ParamTypes.push_back(Ty);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003309 }
Chris Lattner73de3c02008-04-23 05:37:08 +00003310
Devang Patelcd842482008-09-29 20:49:50 +00003311 if (!FunctionType::isValidReturnType(*$4))
Chris Lattner73de3c02008-04-23 05:37:08 +00003312 GEN_ERROR("Invalid result type for LLVM function");
3313
Devang Patelcd842482008-09-29 20:49:50 +00003314 Ty = FunctionType::get($4->get(), ParamTypes, false);
Christopher Lambfb623c62007-12-17 01:17:35 +00003315 PFTy = PointerType::getUnqual(Ty);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003316 }
3317
Devang Patelcd842482008-09-29 20:49:50 +00003318 Value *V = getVal(PFTy, $5); // Get the function we're calling...
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003319 CHECK_FOR_ERROR
3320
3321 // Check for call to invalid intrinsic to avoid crashing later.
3322 if (Function *theF = dyn_cast<Function>(V)) {
3323 if (theF->hasName() && (theF->getValueName()->getKeyLength() >= 5) &&
3324 (0 == strncmp(theF->getValueName()->getKeyData(), "llvm.", 5)) &&
3325 !theF->getIntrinsicID(true))
3326 GEN_ERROR("Call to invalid LLVM intrinsic function '" +
3327 theF->getName() + "'");
3328 }
3329
Devang Pateld222f862008-09-25 21:00:45 +00003330 // Set up the Attributes for the function
3331 SmallVector<AttributeWithIndex, 8> Attrs;
Devang Patelf2a4a922008-09-26 22:53:05 +00003332 //FIXME : In 3.0, stop accepting zext, sext and inreg as optional function
3333 //attributes.
Devang Patelcd842482008-09-29 20:49:50 +00003334 Attributes RetAttrs = $3;
3335 if ($9 != Attribute::None) {
3336 if ($9 & Attribute::ZExt) {
Devang Patelf2a4a922008-09-26 22:53:05 +00003337 RetAttrs = RetAttrs | Attribute::ZExt;
Devang Patelcd842482008-09-29 20:49:50 +00003338 $9 = $9 ^ Attribute::ZExt;
Devang Patelf2a4a922008-09-26 22:53:05 +00003339 }
Devang Patelcd842482008-09-29 20:49:50 +00003340 if ($9 & Attribute::SExt) {
Devang Patelf2a4a922008-09-26 22:53:05 +00003341 RetAttrs = RetAttrs | Attribute::SExt;
Devang Patelcd842482008-09-29 20:49:50 +00003342 $9 = $9 ^ Attribute::SExt;
Devang Patelf2a4a922008-09-26 22:53:05 +00003343 }
Devang Patelcd842482008-09-29 20:49:50 +00003344 if ($9 & Attribute::InReg) {
Devang Patelf2a4a922008-09-26 22:53:05 +00003345 RetAttrs = RetAttrs | Attribute::InReg;
Devang Patelcd842482008-09-29 20:49:50 +00003346 $9 = $9 ^ Attribute::InReg;
Devang Patelf2a4a922008-09-26 22:53:05 +00003347 }
Devang Patelf2a4a922008-09-26 22:53:05 +00003348 }
Devang Patelcd842482008-09-29 20:49:50 +00003349 if (RetAttrs != Attribute::None)
3350 Attrs.push_back(AttributeWithIndex::get(0, RetAttrs));
Devang Patelf2a4a922008-09-26 22:53:05 +00003351
Eric Christopher329d2672008-09-24 04:55:49 +00003352 // Check the arguments
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003353 ValueList Args;
Devang Patelcd842482008-09-29 20:49:50 +00003354 if ($7->empty()) { // Has no arguments?
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003355 // Make sure no arguments is a good thing!
3356 if (Ty->getNumParams() != 0)
3357 GEN_ERROR("No arguments passed to a function that "
3358 "expects arguments");
3359 } else { // Has arguments?
3360 // Loop through FunctionType's arguments and ensure they are specified
Duncan Sandsf5588dc2007-11-27 13:23:08 +00003361 // correctly. Also, gather any parameter attributes.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003362 FunctionType::param_iterator I = Ty->param_begin();
3363 FunctionType::param_iterator E = Ty->param_end();
Devang Patelcd842482008-09-29 20:49:50 +00003364 ParamList::iterator ArgI = $7->begin(), ArgE = $7->end();
Duncan Sandsf5588dc2007-11-27 13:23:08 +00003365 unsigned index = 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003366
Duncan Sandsf5588dc2007-11-27 13:23:08 +00003367 for (; ArgI != ArgE && I != E; ++ArgI, ++I, ++index) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003368 if (ArgI->Val->getType() != *I)
3369 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
3370 (*I)->getDescription() + "'");
3371 Args.push_back(ArgI->Val);
Devang Pateld222f862008-09-25 21:00:45 +00003372 if (ArgI->Attrs != Attribute::None)
3373 Attrs.push_back(AttributeWithIndex::get(index, ArgI->Attrs));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003374 }
3375 if (Ty->isVarArg()) {
3376 if (I == E)
Chris Lattner59363a32008-02-19 04:36:25 +00003377 for (; ArgI != ArgE; ++ArgI, ++index) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003378 Args.push_back(ArgI->Val); // push the remaining varargs
Devang Pateld222f862008-09-25 21:00:45 +00003379 if (ArgI->Attrs != Attribute::None)
3380 Attrs.push_back(AttributeWithIndex::get(index, ArgI->Attrs));
Chris Lattner59363a32008-02-19 04:36:25 +00003381 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003382 } else if (I != E || ArgI != ArgE)
3383 GEN_ERROR("Invalid number of parameters detected");
3384 }
Devang Patelcd842482008-09-29 20:49:50 +00003385 if ($9 != Attribute::None)
3386 Attrs.push_back(AttributeWithIndex::get(~0, $9));
Duncan Sandsf5588dc2007-11-27 13:23:08 +00003387
Devang Pateld222f862008-09-25 21:00:45 +00003388 // Finish off the Attributes and check them
3389 AttrListPtr PAL;
Duncan Sandsf5588dc2007-11-27 13:23:08 +00003390 if (!Attrs.empty())
Devang Pateld222f862008-09-25 21:00:45 +00003391 PAL = AttrListPtr::get(Attrs.begin(), Attrs.end());
Duncan Sandsf5588dc2007-11-27 13:23:08 +00003392
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003393 // Create the call node
Gabor Greif89f01162008-04-06 23:07:54 +00003394 CallInst *CI = CallInst::Create(V, Args.begin(), Args.end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003395 CI->setTailCall($1);
3396 CI->setCallingConv($2);
Devang Pateld222f862008-09-25 21:00:45 +00003397 CI->setAttributes(PAL);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003398 $$ = CI;
Devang Patelcd842482008-09-29 20:49:50 +00003399 delete $7;
3400 delete $4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003401 CHECK_FOR_ERROR
3402 }
3403 | MemoryInst {
3404 $$ = $1;
3405 CHECK_FOR_ERROR
3406 };
3407
3408OptVolatile : VOLATILE {
3409 $$ = true;
3410 CHECK_FOR_ERROR
3411 }
3412 | /* empty */ {
3413 $$ = false;
3414 CHECK_FOR_ERROR
3415 };
3416
3417
3418
3419MemoryInst : MALLOC Types OptCAlign {
3420 if (!UpRefs.empty())
3421 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
3422 $$ = new MallocInst(*$2, 0, $3);
3423 delete $2;
3424 CHECK_FOR_ERROR
3425 }
3426 | MALLOC Types ',' INTTYPE ValueRef OptCAlign {
3427 if (!UpRefs.empty())
3428 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Dan Gohmane5febe42008-05-31 00:58:22 +00003429 if ($4 != Type::Int32Ty)
3430 GEN_ERROR("Malloc array size is not a 32-bit integer!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003431 Value* tmpVal = getVal($4, $5);
3432 CHECK_FOR_ERROR
3433 $$ = new MallocInst(*$2, tmpVal, $6);
3434 delete $2;
3435 }
3436 | ALLOCA Types OptCAlign {
3437 if (!UpRefs.empty())
3438 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
3439 $$ = new AllocaInst(*$2, 0, $3);
3440 delete $2;
3441 CHECK_FOR_ERROR
3442 }
3443 | ALLOCA Types ',' INTTYPE ValueRef OptCAlign {
3444 if (!UpRefs.empty())
3445 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Dan Gohmane5febe42008-05-31 00:58:22 +00003446 if ($4 != Type::Int32Ty)
3447 GEN_ERROR("Alloca array size is not a 32-bit integer!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003448 Value* tmpVal = getVal($4, $5);
3449 CHECK_FOR_ERROR
3450 $$ = new AllocaInst(*$2, tmpVal, $6);
3451 delete $2;
3452 }
3453 | FREE ResolvedVal {
3454 if (!isa<PointerType>($2->getType()))
Eric Christopher329d2672008-09-24 04:55:49 +00003455 GEN_ERROR("Trying to free nonpointer type " +
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003456 $2->getType()->getDescription() + "");
3457 $$ = new FreeInst($2);
3458 CHECK_FOR_ERROR
3459 }
3460
3461 | OptVolatile LOAD Types ValueRef OptCAlign {
3462 if (!UpRefs.empty())
3463 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
3464 if (!isa<PointerType>($3->get()))
3465 GEN_ERROR("Can't load from nonpointer type: " +
3466 (*$3)->getDescription());
3467 if (!cast<PointerType>($3->get())->getElementType()->isFirstClassType())
3468 GEN_ERROR("Can't load from pointer of non-first-class type: " +
3469 (*$3)->getDescription());
3470 Value* tmpVal = getVal(*$3, $4);
3471 CHECK_FOR_ERROR
3472 $$ = new LoadInst(tmpVal, "", $1, $5);
3473 delete $3;
3474 }
3475 | OptVolatile STORE ResolvedVal ',' Types ValueRef OptCAlign {
3476 if (!UpRefs.empty())
3477 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
3478 const PointerType *PT = dyn_cast<PointerType>($5->get());
3479 if (!PT)
3480 GEN_ERROR("Can't store to a nonpointer type: " +
3481 (*$5)->getDescription());
3482 const Type *ElTy = PT->getElementType();
3483 if (ElTy != $3->getType())
3484 GEN_ERROR("Can't store '" + $3->getType()->getDescription() +
3485 "' into space of type '" + ElTy->getDescription() + "'");
3486
3487 Value* tmpVal = getVal(*$5, $6);
3488 CHECK_FOR_ERROR
3489 $$ = new StoreInst($3, tmpVal, $1, $7);
3490 delete $5;
3491 }
Dan Gohmane6b1ee62008-05-23 01:55:30 +00003492 | GETRESULT Types ValueRef ',' EUINT64VAL {
Dan Gohmanb94a0ba2008-07-23 00:54:54 +00003493 if (!UpRefs.empty())
3494 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
3495 if (!isa<StructType>($2->get()) && !isa<ArrayType>($2->get()))
3496 GEN_ERROR("getresult insn requires an aggregate operand");
3497 if (!ExtractValueInst::getIndexedType(*$2, $5))
3498 GEN_ERROR("Invalid getresult index for type '" +
3499 (*$2)->getDescription()+ "'");
3500
3501 Value *tmpVal = getVal(*$2, $3);
Devang Patel3b8849c2008-02-19 22:27:01 +00003502 CHECK_FOR_ERROR
Dan Gohmanb94a0ba2008-07-23 00:54:54 +00003503 $$ = ExtractValueInst::Create(tmpVal, $5);
3504 delete $2;
Devang Patel3b8849c2008-02-19 22:27:01 +00003505 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003506 | GETELEMENTPTR Types ValueRef IndexList {
3507 if (!UpRefs.empty())
3508 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
3509 if (!isa<PointerType>($2->get()))
3510 GEN_ERROR("getelementptr insn requires pointer operand");
3511
Dan Gohman8055f772008-05-15 19:50:34 +00003512 if (!GetElementPtrInst::getIndexedType(*$2, $4->begin(), $4->end()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003513 GEN_ERROR("Invalid getelementptr indices for type '" +
3514 (*$2)->getDescription()+ "'");
3515 Value* tmpVal = getVal(*$2, $3);
3516 CHECK_FOR_ERROR
Gabor Greif89f01162008-04-06 23:07:54 +00003517 $$ = GetElementPtrInst::Create(tmpVal, $4->begin(), $4->end());
Eric Christopher329d2672008-09-24 04:55:49 +00003518 delete $2;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003519 delete $4;
Dan Gohmane6b1ee62008-05-23 01:55:30 +00003520 }
Dan Gohmane5febe42008-05-31 00:58:22 +00003521 | EXTRACTVALUE Types ValueRef ConstantIndexList {
Dan Gohmane6b1ee62008-05-23 01:55:30 +00003522 if (!UpRefs.empty())
3523 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
3524 if (!isa<StructType>($2->get()) && !isa<ArrayType>($2->get()))
3525 GEN_ERROR("extractvalue insn requires an aggregate operand");
3526
3527 if (!ExtractValueInst::getIndexedType(*$2, $4->begin(), $4->end()))
3528 GEN_ERROR("Invalid extractvalue indices for type '" +
3529 (*$2)->getDescription()+ "'");
3530 Value* tmpVal = getVal(*$2, $3);
3531 CHECK_FOR_ERROR
3532 $$ = ExtractValueInst::Create(tmpVal, $4->begin(), $4->end());
Eric Christopher329d2672008-09-24 04:55:49 +00003533 delete $2;
Dan Gohmane6b1ee62008-05-23 01:55:30 +00003534 delete $4;
3535 }
Dan Gohmane5febe42008-05-31 00:58:22 +00003536 | INSERTVALUE Types ValueRef ',' Types ValueRef ConstantIndexList {
Dan Gohmane6b1ee62008-05-23 01:55:30 +00003537 if (!UpRefs.empty())
3538 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
3539 if (!isa<StructType>($2->get()) && !isa<ArrayType>($2->get()))
3540 GEN_ERROR("extractvalue insn requires an aggregate operand");
3541
3542 if (ExtractValueInst::getIndexedType(*$2, $7->begin(), $7->end()) != $5->get())
3543 GEN_ERROR("Invalid insertvalue indices for type '" +
3544 (*$2)->getDescription()+ "'");
3545 Value* aggVal = getVal(*$2, $3);
3546 Value* tmpVal = getVal(*$5, $6);
3547 CHECK_FOR_ERROR
3548 $$ = InsertValueInst::Create(aggVal, tmpVal, $7->begin(), $7->end());
Eric Christopher329d2672008-09-24 04:55:49 +00003549 delete $2;
Dan Gohmane6b1ee62008-05-23 01:55:30 +00003550 delete $5;
3551 delete $7;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003552 };
3553
3554
3555%%
3556
3557// common code from the two 'RunVMAsmParser' functions
3558static Module* RunParser(Module * M) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003559 CurModule.CurrentModule = M;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003560 // Check to make sure the parser succeeded
3561 if (yyparse()) {
3562 if (ParserResult)
3563 delete ParserResult;
3564 return 0;
3565 }
3566
3567 // Emit an error if there are any unresolved types left.
3568 if (!CurModule.LateResolveTypes.empty()) {
3569 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
3570 if (DID.Type == ValID::LocalName) {
3571 GenerateError("Undefined type remains at eof: '"+DID.getName() + "'");
3572 } else {
3573 GenerateError("Undefined type remains at eof: #" + itostr(DID.Num));
3574 }
3575 if (ParserResult)
3576 delete ParserResult;
3577 return 0;
3578 }
3579
3580 // Emit an error if there are any unresolved values left.
3581 if (!CurModule.LateResolveValues.empty()) {
3582 Value *V = CurModule.LateResolveValues.back();
3583 std::map<Value*, std::pair<ValID, int> >::iterator I =
3584 CurModule.PlaceHolderInfo.find(V);
3585
3586 if (I != CurModule.PlaceHolderInfo.end()) {
3587 ValID &DID = I->second.first;
3588 if (DID.Type == ValID::LocalName) {
3589 GenerateError("Undefined value remains at eof: "+DID.getName() + "'");
3590 } else {
3591 GenerateError("Undefined value remains at eof: #" + itostr(DID.Num));
3592 }
3593 if (ParserResult)
3594 delete ParserResult;
3595 return 0;
3596 }
3597 }
3598
3599 // Check to make sure that parsing produced a result
3600 if (!ParserResult)
3601 return 0;
3602
3603 // Reset ParserResult variable while saving its value for the result.
3604 Module *Result = ParserResult;
3605 ParserResult = 0;
3606
3607 return Result;
3608}
3609
3610void llvm::GenerateError(const std::string &message, int LineNo) {
Duncan Sandsf5588dc2007-11-27 13:23:08 +00003611 if (LineNo == -1) LineNo = LLLgetLineNo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003612 // TODO: column number in exception
3613 if (TheParseError)
Duncan Sandsf5588dc2007-11-27 13:23:08 +00003614 TheParseError->setError(LLLgetFilename(), message, LineNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003615 TriggerError = 1;
3616}
3617
3618int yyerror(const char *ErrorMsg) {
Duncan Sandsf5588dc2007-11-27 13:23:08 +00003619 std::string where = LLLgetFilename() + ":" + utostr(LLLgetLineNo()) + ": ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003620 std::string errMsg = where + "error: " + std::string(ErrorMsg);
Duncan Sandsf5588dc2007-11-27 13:23:08 +00003621 if (yychar != YYEMPTY && yychar != 0) {
3622 errMsg += " while reading token: '";
Eric Christopher329d2672008-09-24 04:55:49 +00003623 errMsg += std::string(LLLgetTokenStart(),
Duncan Sandsf5588dc2007-11-27 13:23:08 +00003624 LLLgetTokenStart()+LLLgetTokenLength()) + "'";
3625 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003626 GenerateError(errMsg);
3627 return 0;
3628}