blob: 608ed721f717b61a2cb9f0a8484fa9aa3054eb42 [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;
143 GlobalRefs.erase(I);
144 }
145 return Ret;
146 }
147
148 bool TypeIsUnresolved(PATypeHolder* PATy) {
149 // If it isn't abstract, its resolved
150 const Type* Ty = PATy->get();
151 if (!Ty->isAbstract())
152 return false;
153 // Traverse the type looking for abstract types. If it isn't abstract then
Eric Christopher329d2672008-09-24 04:55:49 +0000154 // we don't need to traverse that leg of the type.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000155 std::vector<const Type*> WorkList, SeenList;
156 WorkList.push_back(Ty);
157 while (!WorkList.empty()) {
158 const Type* Ty = WorkList.back();
159 SeenList.push_back(Ty);
160 WorkList.pop_back();
161 if (const OpaqueType* OpTy = dyn_cast<OpaqueType>(Ty)) {
162 // Check to see if this is an unresolved type
163 std::map<ValID, PATypeHolder>::iterator I = LateResolveTypes.begin();
164 std::map<ValID, PATypeHolder>::iterator E = LateResolveTypes.end();
165 for ( ; I != E; ++I) {
166 if (I->second.get() == OpTy)
167 return true;
168 }
169 } else if (const SequentialType* SeqTy = dyn_cast<SequentialType>(Ty)) {
170 const Type* TheTy = SeqTy->getElementType();
171 if (TheTy->isAbstract() && TheTy != Ty) {
Eric Christopher329d2672008-09-24 04:55:49 +0000172 std::vector<const Type*>::iterator I = SeenList.begin(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000173 E = SeenList.end();
174 for ( ; I != E; ++I)
175 if (*I == TheTy)
176 break;
177 if (I == E)
178 WorkList.push_back(TheTy);
179 }
180 } else if (const StructType* StrTy = dyn_cast<StructType>(Ty)) {
181 for (unsigned i = 0; i < StrTy->getNumElements(); ++i) {
182 const Type* TheTy = StrTy->getElementType(i);
183 if (TheTy->isAbstract() && TheTy != Ty) {
Eric Christopher329d2672008-09-24 04:55:49 +0000184 std::vector<const Type*>::iterator I = SeenList.begin(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000185 E = SeenList.end();
186 for ( ; I != E; ++I)
187 if (*I == TheTy)
188 break;
189 if (I == E)
190 WorkList.push_back(TheTy);
191 }
192 }
193 }
194 }
195 return false;
196 }
197} CurModule;
198
199static struct PerFunctionInfo {
200 Function *CurrentFunction; // Pointer to current function being created
201
202 ValueList Values; // Keep track of #'d definitions
203 unsigned NextValNum;
204 ValueList LateResolveValues;
205 bool isDeclare; // Is this function a forward declararation?
206 GlobalValue::LinkageTypes Linkage; // Linkage for forward declaration.
207 GlobalValue::VisibilityTypes Visibility;
208
209 /// BBForwardRefs - When we see forward references to basic blocks, keep
210 /// track of them here.
211 std::map<ValID, BasicBlock*> BBForwardRefs;
212
213 inline PerFunctionInfo() {
214 CurrentFunction = 0;
215 isDeclare = false;
216 Linkage = GlobalValue::ExternalLinkage;
217 Visibility = GlobalValue::DefaultVisibility;
218 }
219
220 inline void FunctionStart(Function *M) {
221 CurrentFunction = M;
222 NextValNum = 0;
223 }
224
225 void FunctionDone() {
226 // Any forward referenced blocks left?
227 if (!BBForwardRefs.empty()) {
228 GenerateError("Undefined reference to label " +
229 BBForwardRefs.begin()->second->getName());
230 return;
231 }
232
233 // Resolve all forward references now.
234 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
235
236 Values.clear(); // Clear out function local definitions
237 BBForwardRefs.clear();
238 CurrentFunction = 0;
239 isDeclare = false;
240 Linkage = GlobalValue::ExternalLinkage;
241 Visibility = GlobalValue::DefaultVisibility;
242 }
243} CurFun; // Info for the current function...
244
245static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
246
247
248//===----------------------------------------------------------------------===//
249// Code to handle definitions of all the types
250//===----------------------------------------------------------------------===//
251
Chris Lattner906773a2008-08-29 17:20:18 +0000252/// InsertValue - Insert a value into the value table. If it is named, this
253/// returns -1, otherwise it returns the slot number for the value.
254static int InsertValue(Value *V, ValueList &ValueTab = CurFun.Values) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000255 // Things that have names or are void typed don't get slot numbers
256 if (V->hasName() || (V->getType() == Type::VoidTy))
Chris Lattner906773a2008-08-29 17:20:18 +0000257 return -1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000258
259 // In the case of function values, we have to allow for the forward reference
260 // of basic blocks, which are included in the numbering. Consequently, we keep
Eric Christopher329d2672008-09-24 04:55:49 +0000261 // track of the next insertion location with NextValNum. When a BB gets
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000262 // inserted, it could change the size of the CurFun.Values vector.
263 if (&ValueTab == &CurFun.Values) {
264 if (ValueTab.size() <= CurFun.NextValNum)
265 ValueTab.resize(CurFun.NextValNum+1);
266 ValueTab[CurFun.NextValNum++] = V;
Chris Lattner906773a2008-08-29 17:20:18 +0000267 return CurFun.NextValNum-1;
Eric Christopher329d2672008-09-24 04:55:49 +0000268 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000269 // For all other lists, its okay to just tack it on the back of the vector.
270 ValueTab.push_back(V);
Chris Lattner906773a2008-08-29 17:20:18 +0000271 return ValueTab.size()-1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000272}
273
274static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
275 switch (D.Type) {
276 case ValID::LocalID: // Is it a numbered definition?
277 // Module constants occupy the lowest numbered slots...
278 if (D.Num < CurModule.Types.size())
279 return CurModule.Types[D.Num];
280 break;
281 case ValID::LocalName: // Is it a named definition?
282 if (const Type *N = CurModule.CurrentModule->getTypeByName(D.getName())) {
283 D.destroy(); // Free old strdup'd memory...
284 return N;
285 }
286 break;
287 default:
288 GenerateError("Internal parser error: Invalid symbol type reference");
289 return 0;
290 }
291
292 // If we reached here, we referenced either a symbol that we don't know about
293 // or an id number that hasn't been read yet. We may be referencing something
294 // forward, so just create an entry to be resolved later and get to it...
295 //
296 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
297
298
299 if (inFunctionScope()) {
300 if (D.Type == ValID::LocalName) {
301 GenerateError("Reference to an undefined type: '" + D.getName() + "'");
302 return 0;
303 } else {
304 GenerateError("Reference to an undefined type: #" + utostr(D.Num));
305 return 0;
306 }
307 }
308
309 std::map<ValID, PATypeHolder>::iterator I =CurModule.LateResolveTypes.find(D);
310 if (I != CurModule.LateResolveTypes.end())
311 return I->second;
312
313 Type *Typ = OpaqueType::get();
314 CurModule.LateResolveTypes.insert(std::make_pair(D, Typ));
315 return Typ;
316 }
317
318// getExistingVal - Look up the value specified by the provided type and
319// the provided ValID. If the value exists and has already been defined, return
320// it. Otherwise return null.
321//
322static Value *getExistingVal(const Type *Ty, const ValID &D) {
323 if (isa<FunctionType>(Ty)) {
324 GenerateError("Functions are not values and "
325 "must be referenced as pointers");
326 return 0;
327 }
328
329 switch (D.Type) {
330 case ValID::LocalID: { // Is it a numbered definition?
331 // Check that the number is within bounds.
Eric Christopher329d2672008-09-24 04:55:49 +0000332 if (D.Num >= CurFun.Values.size())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000333 return 0;
334 Value *Result = CurFun.Values[D.Num];
335 if (Ty != Result->getType()) {
336 GenerateError("Numbered value (%" + utostr(D.Num) + ") of type '" +
Eric Christopher329d2672008-09-24 04:55:49 +0000337 Result->getType()->getDescription() + "' does not match "
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000338 "expected type, '" + Ty->getDescription() + "'");
339 return 0;
340 }
341 return Result;
342 }
343 case ValID::GlobalID: { // Is it a numbered definition?
Eric Christopher329d2672008-09-24 04:55:49 +0000344 if (D.Num >= CurModule.Values.size())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000345 return 0;
346 Value *Result = CurModule.Values[D.Num];
347 if (Ty != Result->getType()) {
348 GenerateError("Numbered value (@" + utostr(D.Num) + ") of type '" +
Eric Christopher329d2672008-09-24 04:55:49 +0000349 Result->getType()->getDescription() + "' does not match "
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000350 "expected type, '" + Ty->getDescription() + "'");
351 return 0;
352 }
353 return Result;
354 }
Eric Christopher329d2672008-09-24 04:55:49 +0000355
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000356 case ValID::LocalName: { // Is it a named definition?
Eric Christopher329d2672008-09-24 04:55:49 +0000357 if (!inFunctionScope())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000358 return 0;
359 ValueSymbolTable &SymTab = CurFun.CurrentFunction->getValueSymbolTable();
360 Value *N = SymTab.lookup(D.getName());
Eric Christopher329d2672008-09-24 04:55:49 +0000361 if (N == 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000362 return 0;
363 if (N->getType() != Ty)
364 return 0;
Eric Christopher329d2672008-09-24 04:55:49 +0000365
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000366 D.destroy(); // Free old strdup'd memory...
367 return N;
368 }
369 case ValID::GlobalName: { // Is it a named definition?
370 ValueSymbolTable &SymTab = CurModule.CurrentModule->getValueSymbolTable();
371 Value *N = SymTab.lookup(D.getName());
Eric Christopher329d2672008-09-24 04:55:49 +0000372 if (N == 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000373 return 0;
374 if (N->getType() != Ty)
375 return 0;
376
377 D.destroy(); // Free old strdup'd memory...
378 return N;
379 }
380
381 // Check to make sure that "Ty" is an integral type, and that our
382 // value will fit into the specified type...
383 case ValID::ConstSIntVal: // Is it a constant pool reference??
Chris Lattner59363a32008-02-19 04:36:25 +0000384 if (!isa<IntegerType>(Ty) ||
385 !ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000386 GenerateError("Signed integral constant '" +
387 itostr(D.ConstPool64) + "' is invalid for type '" +
388 Ty->getDescription() + "'");
389 return 0;
390 }
391 return ConstantInt::get(Ty, D.ConstPool64, true);
392
393 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattner59363a32008-02-19 04:36:25 +0000394 if (isa<IntegerType>(Ty) &&
395 ConstantInt::isValueValidForType(Ty, D.UConstPool64))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000396 return ConstantInt::get(Ty, D.UConstPool64);
Chris Lattner59363a32008-02-19 04:36:25 +0000397
398 if (!isa<IntegerType>(Ty) ||
399 !ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
400 GenerateError("Integral constant '" + utostr(D.UConstPool64) +
401 "' is invalid or out of range for type '" +
402 Ty->getDescription() + "'");
403 return 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000404 }
Chris Lattner59363a32008-02-19 04:36:25 +0000405 // This is really a signed reference. Transmogrify.
406 return ConstantInt::get(Ty, D.ConstPool64, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000407
Chris Lattnerf3d40022008-07-11 00:30:39 +0000408 case ValID::ConstAPInt: // Is it an unsigned const pool reference?
409 if (!isa<IntegerType>(Ty)) {
410 GenerateError("Integral constant '" + D.getName() +
411 "' is invalid or out of range for type '" +
412 Ty->getDescription() + "'");
413 return 0;
414 }
Eric Christopher329d2672008-09-24 04:55:49 +0000415
Chris Lattnerf3d40022008-07-11 00:30:39 +0000416 {
417 APSInt Tmp = *D.ConstPoolInt;
418 Tmp.extOrTrunc(Ty->getPrimitiveSizeInBits());
419 return ConstantInt::get(Tmp);
420 }
Eric Christopher329d2672008-09-24 04:55:49 +0000421
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000422 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattner59363a32008-02-19 04:36:25 +0000423 if (!Ty->isFloatingPoint() ||
424 !ConstantFP::isValueValidForType(Ty, *D.ConstPoolFP)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000425 GenerateError("FP constant invalid for type");
426 return 0;
427 }
Eric Christopher329d2672008-09-24 04:55:49 +0000428 // Lexer has no type info, so builds all float and double FP constants
Dale Johannesen255b8fe2007-09-11 18:33:39 +0000429 // as double. Fix this here. Long double does not need this.
430 if (&D.ConstPoolFP->getSemantics() == &APFloat::IEEEdouble &&
431 Ty==Type::FloatTy)
Dale Johannesenb9de9f02007-09-06 18:13:44 +0000432 D.ConstPoolFP->convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven);
Chris Lattner05ba86e2008-04-20 00:41:19 +0000433 return ConstantFP::get(*D.ConstPoolFP);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000434
435 case ValID::ConstNullVal: // Is it a null value?
436 if (!isa<PointerType>(Ty)) {
437 GenerateError("Cannot create a a non pointer null");
438 return 0;
439 }
440 return ConstantPointerNull::get(cast<PointerType>(Ty));
441
442 case ValID::ConstUndefVal: // Is it an undef value?
443 return UndefValue::get(Ty);
444
445 case ValID::ConstZeroVal: // Is it a zero value?
446 return Constant::getNullValue(Ty);
Eric Christopher329d2672008-09-24 04:55:49 +0000447
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000448 case ValID::ConstantVal: // Fully resolved constant?
449 if (D.ConstantValue->getType() != Ty) {
450 GenerateError("Constant expression type different from required type");
451 return 0;
452 }
453 return D.ConstantValue;
454
455 case ValID::InlineAsmVal: { // Inline asm expression
456 const PointerType *PTy = dyn_cast<PointerType>(Ty);
457 const FunctionType *FTy =
458 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
459 if (!FTy || !InlineAsm::Verify(FTy, D.IAD->Constraints)) {
460 GenerateError("Invalid type for asm constraint string");
461 return 0;
462 }
463 InlineAsm *IA = InlineAsm::get(FTy, D.IAD->AsmString, D.IAD->Constraints,
464 D.IAD->HasSideEffects);
465 D.destroy(); // Free InlineAsmDescriptor.
466 return IA;
467 }
468 default:
469 assert(0 && "Unhandled case!");
470 return 0;
471 } // End of switch
472
473 assert(0 && "Unhandled case!");
474 return 0;
475}
476
477// getVal - This function is identical to getExistingVal, except that if a
478// value is not already defined, it "improvises" by creating a placeholder var
479// that looks and acts just like the requested variable. When the value is
480// defined later, all uses of the placeholder variable are replaced with the
481// real thing.
482//
483static Value *getVal(const Type *Ty, const ValID &ID) {
484 if (Ty == Type::LabelTy) {
485 GenerateError("Cannot use a basic block here");
486 return 0;
487 }
488
489 // See if the value has already been defined.
490 Value *V = getExistingVal(Ty, ID);
491 if (V) return V;
492 if (TriggerError) return 0;
493
494 if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty)) {
Dan Gohmane6b1ee62008-05-23 01:55:30 +0000495 GenerateError("Invalid use of a non-first-class type");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000496 return 0;
497 }
498
499 // If we reached here, we referenced either a symbol that we don't know about
500 // or an id number that hasn't been read yet. We may be referencing something
501 // forward, so just create an entry to be resolved later and get to it...
502 //
503 switch (ID.Type) {
504 case ValID::GlobalName:
505 case ValID::GlobalID: {
506 const PointerType *PTy = dyn_cast<PointerType>(Ty);
507 if (!PTy) {
508 GenerateError("Invalid type for reference to global" );
509 return 0;
510 }
511 const Type* ElTy = PTy->getElementType();
512 if (const FunctionType *FTy = dyn_cast<FunctionType>(ElTy))
Gabor Greif89f01162008-04-06 23:07:54 +0000513 V = Function::Create(FTy, GlobalValue::ExternalLinkage);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000514 else
Christopher Lamb0a243582007-12-11 09:02:08 +0000515 V = new GlobalVariable(ElTy, false, GlobalValue::ExternalLinkage, 0, "",
516 (Module*)0, false, PTy->getAddressSpace());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000517 break;
518 }
519 default:
520 V = new Argument(Ty);
521 }
Eric Christopher329d2672008-09-24 04:55:49 +0000522
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000523 // Remember where this forward reference came from. FIXME, shouldn't we try
524 // to recycle these things??
525 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
Duncan Sandsf5588dc2007-11-27 13:23:08 +0000526 LLLgetLineNo())));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000527
528 if (inFunctionScope())
529 InsertValue(V, CurFun.LateResolveValues);
530 else
531 InsertValue(V, CurModule.LateResolveValues);
532 return V;
533}
534
535/// defineBBVal - This is a definition of a new basic block with the specified
536/// identifier which must be the same as CurFun.NextValNum, if its numeric.
Nick Lewyckyd8aa33a2008-04-25 16:53:59 +0000537static BasicBlock *defineBBVal(const ValID &ID) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000538 assert(inFunctionScope() && "Can't get basic block at global scope!");
539
540 BasicBlock *BB = 0;
541
542 // First, see if this was forward referenced
543
544 std::map<ValID, BasicBlock*>::iterator BBI = CurFun.BBForwardRefs.find(ID);
545 if (BBI != CurFun.BBForwardRefs.end()) {
546 BB = BBI->second;
547 // The forward declaration could have been inserted anywhere in the
548 // function: insert it into the correct place now.
549 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
550 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
551
552 // We're about to erase the entry, save the key so we can clean it up.
553 ValID Tmp = BBI->first;
554
555 // Erase the forward ref from the map as its no longer "forward"
556 CurFun.BBForwardRefs.erase(ID);
557
Eric Christopher329d2672008-09-24 04:55:49 +0000558 // The key has been removed from the map but so we don't want to leave
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000559 // strdup'd memory around so destroy it too.
560 Tmp.destroy();
561
562 // If its a numbered definition, bump the number and set the BB value.
563 if (ID.Type == ValID::LocalID) {
564 assert(ID.Num == CurFun.NextValNum && "Invalid new block number");
565 InsertValue(BB);
566 }
Eric Christopher329d2672008-09-24 04:55:49 +0000567 } else {
568 // We haven't seen this BB before and its first mention is a definition.
Devang Patel890cc572008-03-03 18:58:47 +0000569 // Just create it and return it.
570 std::string Name (ID.Type == ValID::LocalName ? ID.getName() : "");
Gabor Greif89f01162008-04-06 23:07:54 +0000571 BB = BasicBlock::Create(Name, CurFun.CurrentFunction);
Devang Patel890cc572008-03-03 18:58:47 +0000572 if (ID.Type == ValID::LocalID) {
573 assert(ID.Num == CurFun.NextValNum && "Invalid new block number");
574 InsertValue(BB);
575 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000576 }
577
Devang Patel890cc572008-03-03 18:58:47 +0000578 ID.destroy();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000579 return BB;
580}
581
582/// getBBVal - get an existing BB value or create a forward reference for it.
Eric Christopher329d2672008-09-24 04:55:49 +0000583///
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000584static BasicBlock *getBBVal(const ValID &ID) {
585 assert(inFunctionScope() && "Can't get basic block at global scope!");
586
587 BasicBlock *BB = 0;
588
589 std::map<ValID, BasicBlock*>::iterator BBI = CurFun.BBForwardRefs.find(ID);
590 if (BBI != CurFun.BBForwardRefs.end()) {
591 BB = BBI->second;
592 } if (ID.Type == ValID::LocalName) {
593 std::string Name = ID.getName();
594 Value *N = CurFun.CurrentFunction->getValueSymbolTable().lookup(Name);
Anton Korobeynikov6a4a9332008-02-20 12:07:57 +0000595 if (N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000596 if (N->getType()->getTypeID() == Type::LabelTyID)
597 BB = cast<BasicBlock>(N);
598 else
599 GenerateError("Reference to label '" + Name + "' is actually of type '"+
600 N->getType()->getDescription() + "'");
Anton Korobeynikov6a4a9332008-02-20 12:07:57 +0000601 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000602 } else if (ID.Type == ValID::LocalID) {
603 if (ID.Num < CurFun.NextValNum && ID.Num < CurFun.Values.size()) {
604 if (CurFun.Values[ID.Num]->getType()->getTypeID() == Type::LabelTyID)
605 BB = cast<BasicBlock>(CurFun.Values[ID.Num]);
606 else
Eric Christopher329d2672008-09-24 04:55:49 +0000607 GenerateError("Reference to label '%" + utostr(ID.Num) +
608 "' is actually of type '"+
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000609 CurFun.Values[ID.Num]->getType()->getDescription() + "'");
610 }
611 } else {
612 GenerateError("Illegal label reference " + ID.getName());
613 return 0;
614 }
615
616 // If its already been defined, return it now.
617 if (BB) {
618 ID.destroy(); // Free strdup'd memory.
619 return BB;
620 }
621
622 // Otherwise, this block has not been seen before, create it.
623 std::string Name;
624 if (ID.Type == ValID::LocalName)
625 Name = ID.getName();
Gabor Greif89f01162008-04-06 23:07:54 +0000626 BB = BasicBlock::Create(Name, CurFun.CurrentFunction);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000627
628 // Insert it in the forward refs map.
629 CurFun.BBForwardRefs[ID] = BB;
630
631 return BB;
632}
633
634
635//===----------------------------------------------------------------------===//
636// Code to handle forward references in instructions
637//===----------------------------------------------------------------------===//
638//
639// This code handles the late binding needed with statements that reference
640// values not defined yet... for example, a forward branch, or the PHI node for
641// a loop body.
642//
643// This keeps a table (CurFun.LateResolveValues) of all such forward references
644// and back patchs after we are done.
645//
646
647// ResolveDefinitions - If we could not resolve some defs at parsing
648// time (forward branches, phi functions for loops, etc...) resolve the
649// defs now...
650//
Eric Christopher329d2672008-09-24 04:55:49 +0000651static void
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000652ResolveDefinitions(ValueList &LateResolvers, ValueList *FutureLateResolvers) {
653 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
654 while (!LateResolvers.empty()) {
655 Value *V = LateResolvers.back();
656 LateResolvers.pop_back();
657
658 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
659 CurModule.PlaceHolderInfo.find(V);
660 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
661
662 ValID &DID = PHI->second.first;
663
664 Value *TheRealValue = getExistingVal(V->getType(), DID);
665 if (TriggerError)
666 return;
667 if (TheRealValue) {
668 V->replaceAllUsesWith(TheRealValue);
669 delete V;
670 CurModule.PlaceHolderInfo.erase(PHI);
671 } else if (FutureLateResolvers) {
672 // Functions have their unresolved items forwarded to the module late
673 // resolver table
674 InsertValue(V, *FutureLateResolvers);
675 } else {
676 if (DID.Type == ValID::LocalName || DID.Type == ValID::GlobalName) {
677 GenerateError("Reference to an invalid definition: '" +DID.getName()+
678 "' of type '" + V->getType()->getDescription() + "'",
679 PHI->second.second);
680 return;
681 } else {
682 GenerateError("Reference to an invalid definition: #" +
683 itostr(DID.Num) + " of type '" +
684 V->getType()->getDescription() + "'",
685 PHI->second.second);
686 return;
687 }
688 }
689 }
690 LateResolvers.clear();
691}
692
693// ResolveTypeTo - A brand new type was just declared. This means that (if
694// name is not null) things referencing Name can be resolved. Otherwise, things
695// refering to the number can be resolved. Do this now.
696//
697static void ResolveTypeTo(std::string *Name, const Type *ToTy) {
698 ValID D;
699 if (Name)
700 D = ValID::createLocalName(*Name);
Eric Christopher329d2672008-09-24 04:55:49 +0000701 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000702 D = ValID::createLocalID(CurModule.Types.size());
703
704 std::map<ValID, PATypeHolder>::iterator I =
705 CurModule.LateResolveTypes.find(D);
706 if (I != CurModule.LateResolveTypes.end()) {
707 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
708 CurModule.LateResolveTypes.erase(I);
709 }
Nuno Lopes1697e8b2008-10-03 15:52:39 +0000710 D.destroy();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000711}
712
713// setValueName - Set the specified value to the name given. The name may be
714// null potentially, in which case this is a noop. The string passed in is
715// assumed to be a malloc'd string buffer, and is free'd by this function.
716//
717static void setValueName(Value *V, std::string *NameStr) {
718 if (!NameStr) return;
719 std::string Name(*NameStr); // Copy string
720 delete NameStr; // Free old string
721
722 if (V->getType() == Type::VoidTy) {
723 GenerateError("Can't assign name '" + Name+"' to value with void type");
724 return;
725 }
726
727 assert(inFunctionScope() && "Must be in function scope!");
728 ValueSymbolTable &ST = CurFun.CurrentFunction->getValueSymbolTable();
729 if (ST.lookup(Name)) {
730 GenerateError("Redefinition of value '" + Name + "' of type '" +
731 V->getType()->getDescription() + "'");
732 return;
733 }
734
735 // Set the name.
736 V->setName(Name);
737}
738
739/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
740/// this is a declaration, otherwise it is a definition.
741static GlobalVariable *
742ParseGlobalVariable(std::string *NameStr,
743 GlobalValue::LinkageTypes Linkage,
744 GlobalValue::VisibilityTypes Visibility,
745 bool isConstantGlobal, const Type *Ty,
Christopher Lamb0a243582007-12-11 09:02:08 +0000746 Constant *Initializer, bool IsThreadLocal,
747 unsigned AddressSpace = 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000748 if (isa<FunctionType>(Ty)) {
749 GenerateError("Cannot declare global vars of function type");
750 return 0;
751 }
Dan Gohmane5febe42008-05-31 00:58:22 +0000752 if (Ty == Type::LabelTy) {
753 GenerateError("Cannot declare global vars of label type");
754 return 0;
755 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000756
Christopher Lamb0a243582007-12-11 09:02:08 +0000757 const PointerType *PTy = PointerType::get(Ty, AddressSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000758
759 std::string Name;
760 if (NameStr) {
761 Name = *NameStr; // Copy string
762 delete NameStr; // Free old string
763 }
764
765 // See if this global value was forward referenced. If so, recycle the
766 // object.
767 ValID ID;
768 if (!Name.empty()) {
769 ID = ValID::createGlobalName(Name);
770 } else {
771 ID = ValID::createGlobalID(CurModule.Values.size());
772 }
773
774 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
775 // Move the global to the end of the list, from whereever it was
776 // previously inserted.
777 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
778 CurModule.CurrentModule->getGlobalList().remove(GV);
779 CurModule.CurrentModule->getGlobalList().push_back(GV);
780 GV->setInitializer(Initializer);
781 GV->setLinkage(Linkage);
782 GV->setVisibility(Visibility);
783 GV->setConstant(isConstantGlobal);
784 GV->setThreadLocal(IsThreadLocal);
785 InsertValue(GV, CurModule.Values);
Nuno Lopes1697e8b2008-10-03 15:52:39 +0000786 ID.destroy();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000787 return GV;
788 }
789
Nuno Lopes1697e8b2008-10-03 15:52:39 +0000790 ID.destroy();
791
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000792 // If this global has a name
793 if (!Name.empty()) {
794 // if the global we're parsing has an initializer (is a definition) and
795 // has external linkage.
796 if (Initializer && Linkage != GlobalValue::InternalLinkage)
797 // If there is already a global with external linkage with this name
798 if (CurModule.CurrentModule->getGlobalVariable(Name, false)) {
799 // If we allow this GVar to get created, it will be renamed in the
800 // symbol table because it conflicts with an existing GVar. We can't
801 // allow redefinition of GVars whose linking indicates that their name
802 // must stay the same. Issue the error.
803 GenerateError("Redefinition of global variable named '" + Name +
804 "' of type '" + Ty->getDescription() + "'");
805 return 0;
806 }
807 }
808
809 // Otherwise there is no existing GV to use, create one now.
810 GlobalVariable *GV =
811 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
Christopher Lamb0a243582007-12-11 09:02:08 +0000812 CurModule.CurrentModule, IsThreadLocal, AddressSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000813 GV->setVisibility(Visibility);
814 InsertValue(GV, CurModule.Values);
815 return GV;
816}
817
818// setTypeName - Set the specified type to the name given. The name may be
819// null potentially, in which case this is a noop. The string passed in is
820// assumed to be a malloc'd string buffer, and is freed by this function.
821//
822// This function returns true if the type has already been defined, but is
823// allowed to be redefined in the specified context. If the name is a new name
824// for the type plane, it is inserted and false is returned.
825static bool setTypeName(const Type *T, std::string *NameStr) {
826 assert(!inFunctionScope() && "Can't give types function-local names!");
827 if (NameStr == 0) return false;
Eric Christopher329d2672008-09-24 04:55:49 +0000828
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000829 std::string Name(*NameStr); // Copy string
830 delete NameStr; // Free old string
831
832 // We don't allow assigning names to void type
833 if (T == Type::VoidTy) {
834 GenerateError("Can't assign name '" + Name + "' to the void type");
835 return false;
836 }
837
838 // Set the type name, checking for conflicts as we do so.
839 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
840
841 if (AlreadyExists) { // Inserting a name that is already defined???
842 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
843 assert(Existing && "Conflict but no matching type?!");
844
845 // There is only one case where this is allowed: when we are refining an
846 // opaque type. In this case, Existing will be an opaque type.
847 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
848 // We ARE replacing an opaque type!
849 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
850 return true;
851 }
852
853 // Otherwise, this is an attempt to redefine a type. That's okay if
854 // the redefinition is identical to the original. This will be so if
855 // Existing and T point to the same Type object. In this one case we
856 // allow the equivalent redefinition.
857 if (Existing == T) return true; // Yes, it's equal.
858
859 // Any other kind of (non-equivalent) redefinition is an error.
860 GenerateError("Redefinition of type named '" + Name + "' of type '" +
861 T->getDescription() + "'");
862 }
863
864 return false;
865}
866
867//===----------------------------------------------------------------------===//
868// Code for handling upreferences in type names...
869//
870
871// TypeContains - Returns true if Ty directly contains E in it.
872//
873static bool TypeContains(const Type *Ty, const Type *E) {
874 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
875 E) != Ty->subtype_end();
876}
877
878namespace {
879 struct UpRefRecord {
880 // NestingLevel - The number of nesting levels that need to be popped before
881 // this type is resolved.
882 unsigned NestingLevel;
883
884 // LastContainedTy - This is the type at the current binding level for the
885 // type. Every time we reduce the nesting level, this gets updated.
886 const Type *LastContainedTy;
887
888 // UpRefTy - This is the actual opaque type that the upreference is
889 // represented with.
890 OpaqueType *UpRefTy;
891
892 UpRefRecord(unsigned NL, OpaqueType *URTy)
893 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
894 };
895}
896
897// UpRefs - A list of the outstanding upreferences that need to be resolved.
898static std::vector<UpRefRecord> UpRefs;
899
900/// HandleUpRefs - Every time we finish a new layer of types, this function is
901/// called. It loops through the UpRefs vector, which is a list of the
902/// currently active types. For each type, if the up reference is contained in
903/// the newly completed type, we decrement the level count. When the level
904/// count reaches zero, the upreferenced type is the type that is passed in:
905/// thus we can complete the cycle.
906///
907static PATypeHolder HandleUpRefs(const Type *ty) {
908 // If Ty isn't abstract, or if there are no up-references in it, then there is
909 // nothing to resolve here.
910 if (!ty->isAbstract() || UpRefs.empty()) return ty;
Eric Christopher329d2672008-09-24 04:55:49 +0000911
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000912 PATypeHolder Ty(ty);
913 UR_OUT("Type '" << Ty->getDescription() <<
914 "' newly formed. Resolving upreferences.\n" <<
915 UpRefs.size() << " upreferences active!\n");
916
917 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
918 // to zero), we resolve them all together before we resolve them to Ty. At
919 // the end of the loop, if there is anything to resolve to Ty, it will be in
920 // this variable.
921 OpaqueType *TypeToResolve = 0;
922
923 for (unsigned i = 0; i != UpRefs.size(); ++i) {
924 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
925 << UpRefs[i].second->getDescription() << ") = "
926 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
927 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
928 // Decrement level of upreference
929 unsigned Level = --UpRefs[i].NestingLevel;
930 UpRefs[i].LastContainedTy = Ty;
931 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
932 if (Level == 0) { // Upreference should be resolved!
933 if (!TypeToResolve) {
934 TypeToResolve = UpRefs[i].UpRefTy;
935 } else {
936 UR_OUT(" * Resolving upreference for "
937 << UpRefs[i].second->getDescription() << "\n";
938 std::string OldName = UpRefs[i].UpRefTy->getDescription());
939 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
940 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
941 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
942 }
943 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
944 --i; // Do not skip the next element...
945 }
946 }
947 }
948
949 if (TypeToResolve) {
950 UR_OUT(" * Resolving upreference for "
951 << UpRefs[i].second->getDescription() << "\n";
952 std::string OldName = TypeToResolve->getDescription());
953 TypeToResolve->refineAbstractTypeTo(Ty);
954 }
955
956 return Ty;
957}
958
959//===----------------------------------------------------------------------===//
960// RunVMAsmParser - Define an interface to this parser
961//===----------------------------------------------------------------------===//
962//
963static Module* RunParser(Module * M);
964
Duncan Sandsf5588dc2007-11-27 13:23:08 +0000965Module *llvm::RunVMAsmParser(llvm::MemoryBuffer *MB) {
966 InitLLLexer(MB);
967 Module *M = RunParser(new Module(LLLgetFilename()));
968 FreeLexer();
969 return M;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000970}
971
972%}
973
974%union {
975 llvm::Module *ModuleVal;
976 llvm::Function *FunctionVal;
977 llvm::BasicBlock *BasicBlockVal;
978 llvm::TerminatorInst *TermInstVal;
979 llvm::Instruction *InstVal;
980 llvm::Constant *ConstVal;
981
982 const llvm::Type *PrimType;
983 std::list<llvm::PATypeHolder> *TypeList;
984 llvm::PATypeHolder *TypeVal;
985 llvm::Value *ValueVal;
986 std::vector<llvm::Value*> *ValueList;
Dan Gohmane5febe42008-05-31 00:58:22 +0000987 std::vector<unsigned> *ConstantList;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000988 llvm::ArgListType *ArgList;
989 llvm::TypeWithAttrs TypeWithAttrs;
990 llvm::TypeWithAttrsList *TypeWithAttrsList;
Dale Johannesencfb19e62007-11-05 21:20:28 +0000991 llvm::ParamList *ParamList;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000992
993 // Represent the RHS of PHI node
994 std::list<std::pair<llvm::Value*,
995 llvm::BasicBlock*> > *PHIList;
996 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
997 std::vector<llvm::Constant*> *ConstVector;
998
999 llvm::GlobalValue::LinkageTypes Linkage;
1000 llvm::GlobalValue::VisibilityTypes Visibility;
Devang Pateld222f862008-09-25 21:00:45 +00001001 llvm::Attributes Attributes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001002 llvm::APInt *APIntVal;
1003 int64_t SInt64Val;
1004 uint64_t UInt64Val;
1005 int SIntVal;
1006 unsigned UIntVal;
Dale Johannesenb9de9f02007-09-06 18:13:44 +00001007 llvm::APFloat *FPVal;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001008 bool BoolVal;
1009
1010 std::string *StrVal; // This memory must be deleted
1011 llvm::ValID ValIDVal;
1012
1013 llvm::Instruction::BinaryOps BinaryOpVal;
1014 llvm::Instruction::TermOps TermOpVal;
1015 llvm::Instruction::MemoryOps MemOpVal;
1016 llvm::Instruction::CastOps CastOpVal;
1017 llvm::Instruction::OtherOps OtherOpVal;
1018 llvm::ICmpInst::Predicate IPredicate;
1019 llvm::FCmpInst::Predicate FPredicate;
1020}
1021
Eric Christopher329d2672008-09-24 04:55:49 +00001022%type <ModuleVal> Module
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001023%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
1024%type <BasicBlockVal> BasicBlock InstructionList
1025%type <TermInstVal> BBTerminatorInst
1026%type <InstVal> Inst InstVal MemoryInst
1027%type <ConstVal> ConstVal ConstExpr AliaseeRef
1028%type <ConstVector> ConstVector
1029%type <ArgList> ArgList ArgListH
1030%type <PHIList> PHIList
Dale Johannesencfb19e62007-11-05 21:20:28 +00001031%type <ParamList> ParamList // For call param lists & GEP indices
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001032%type <ValueList> IndexList // For GEP indices
Dan Gohmane5febe42008-05-31 00:58:22 +00001033%type <ConstantList> ConstantIndexList // For insertvalue/extractvalue indices
Eric Christopher329d2672008-09-24 04:55:49 +00001034%type <TypeList> TypeListI
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001035%type <TypeWithAttrsList> ArgTypeList ArgTypeListI
1036%type <TypeWithAttrs> ArgType
1037%type <JumpTable> JumpTable
1038%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
1039%type <BoolVal> ThreadLocal // 'thread_local' or not
1040%type <BoolVal> OptVolatile // 'volatile' or not
1041%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
1042%type <BoolVal> OptSideEffect // 'sideeffect' or not.
1043%type <Linkage> GVInternalLinkage GVExternalLinkage
1044%type <Linkage> FunctionDefineLinkage FunctionDeclareLinkage
1045%type <Linkage> AliasLinkage
1046%type <Visibility> GVVisibilityStyle
1047
1048// ValueRef - Unresolved reference to a definition or BB
1049%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
1050%type <ValueVal> ResolvedVal // <type> <valref> pair
Devang Patelbf507402008-02-20 22:40:23 +00001051%type <ValueList> ReturnedVal
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001052// Tokens and types for handling constant integer values
1053//
1054// ESINT64VAL - A negative number within long long range
1055%token <SInt64Val> ESINT64VAL
1056
1057// EUINT64VAL - A positive number within uns. long long range
1058%token <UInt64Val> EUINT64VAL
1059
Eric Christopher329d2672008-09-24 04:55:49 +00001060// ESAPINTVAL - A negative number with arbitrary precision
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001061%token <APIntVal> ESAPINTVAL
1062
Eric Christopher329d2672008-09-24 04:55:49 +00001063// EUAPINTVAL - A positive number with arbitrary precision
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001064%token <APIntVal> EUAPINTVAL
1065
1066%token <UIntVal> LOCALVAL_ID GLOBALVAL_ID // %123 @123
1067%token <FPVal> FPVAL // Float or Double constant
1068
1069// Built in types...
1070%type <TypeVal> Types ResultTypes
1071%type <PrimType> IntType FPType PrimType // Classifications
Eric Christopher329d2672008-09-24 04:55:49 +00001072%token <PrimType> VOID INTTYPE
Dale Johannesenf325d9f2007-08-03 01:03:46 +00001073%token <PrimType> FLOAT DOUBLE X86_FP80 FP128 PPC_FP128 LABEL
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001074%token TYPE
1075
1076
Eric Christopher329d2672008-09-24 04:55:49 +00001077%token<StrVal> LOCALVAR GLOBALVAR LABELSTR
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001078%token<StrVal> STRINGCONSTANT ATSTRINGCONSTANT PCTSTRINGCONSTANT
1079%type <StrVal> LocalName OptLocalName OptLocalAssign
1080%type <StrVal> GlobalName OptGlobalAssign GlobalAssign
Gordon Henriksen13fe5e32007-12-10 03:18:06 +00001081%type <StrVal> OptSection SectionString OptGC
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001082
Christopher Lamb668d9a02007-12-12 08:45:45 +00001083%type <UIntVal> OptAlign OptCAlign OptAddrSpace
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001084
1085%token ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
1086%token DECLARE DEFINE GLOBAL CONSTANT SECTION ALIAS VOLATILE THREAD_LOCAL
1087%token TO DOTDOTDOT NULL_TOK UNDEF INTERNAL LINKONCE WEAK APPENDING
Dale Johannesen280e7bc2008-05-14 20:13:36 +00001088%token DLLIMPORT DLLEXPORT EXTERN_WEAK COMMON
Christopher Lamb0a243582007-12-11 09:02:08 +00001089%token OPAQUE EXTERNAL TARGET TRIPLE ALIGN ADDRSPACE
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001090%token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
1091%token CC_TOK CCC_TOK FASTCC_TOK COLDCC_TOK X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
Nick Lewyckyd8aa33a2008-04-25 16:53:59 +00001092%token DATALAYOUT
Chris Lattner906773a2008-08-29 17:20:18 +00001093%type <UIntVal> OptCallingConv LocalNumber
Devang Pateld222f862008-09-25 21:00:45 +00001094%type <Attributes> OptAttributes Attribute
1095%type <Attributes> OptFuncAttrs FuncAttr
Devang Patelcd842482008-09-29 20:49:50 +00001096%type <Attributes> OptRetAttrs RetAttr
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001097
1098// Basic Block Terminating Operators
1099%token <TermOpVal> RET BR SWITCH INVOKE UNWIND UNREACHABLE
1100
1101// Binary Operators
1102%type <BinaryOpVal> ArithmeticOps LogicalOps // Binops Subcatagories
1103%token <BinaryOpVal> ADD SUB MUL UDIV SDIV FDIV UREM SREM FREM AND OR XOR
1104%token <BinaryOpVal> SHL LSHR ASHR
1105
Eric Christopher329d2672008-09-24 04:55:49 +00001106%token <OtherOpVal> ICMP FCMP VICMP VFCMP
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001107%type <IPredicate> IPredicates
1108%type <FPredicate> FPredicates
Eric Christopher329d2672008-09-24 04:55:49 +00001109%token EQ NE SLT SGT SLE SGE ULT UGT ULE UGE
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001110%token OEQ ONE OLT OGT OLE OGE ORD UNO UEQ UNE
1111
1112// Memory Instructions
1113%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
1114
1115// Cast Operators
1116%type <CastOpVal> CastOps
1117%token <CastOpVal> TRUNC ZEXT SEXT FPTRUNC FPEXT BITCAST
1118%token <CastOpVal> UITOFP SITOFP FPTOUI FPTOSI INTTOPTR PTRTOINT
1119
1120// Other Operators
1121%token <OtherOpVal> PHI_TOK SELECT VAARG
1122%token <OtherOpVal> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
Devang Patel3b8849c2008-02-19 22:27:01 +00001123%token <OtherOpVal> GETRESULT
Dan Gohmane6b1ee62008-05-23 01:55:30 +00001124%token <OtherOpVal> EXTRACTVALUE INSERTVALUE
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001125
1126// Function Attributes
Reid Spenceraa8ae282007-07-31 03:50:36 +00001127%token SIGNEXT ZEROEXT NORETURN INREG SRET NOUNWIND NOALIAS BYVAL NEST
Devang Patel008cd3e2008-09-26 23:51:19 +00001128%token READNONE READONLY GC OPTSIZE NOINLINE ALWAYSINLINE
Devang Patel5df692d2008-09-02 20:52:40 +00001129
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001130// Visibility Styles
1131%token DEFAULT HIDDEN PROTECTED
1132
1133%start Module
1134%%
1135
1136
1137// Operations that are notably excluded from this list include:
1138// RET, BR, & SWITCH because they end basic blocks and are treated specially.
1139//
1140ArithmeticOps: ADD | SUB | MUL | UDIV | SDIV | FDIV | UREM | SREM | FREM;
1141LogicalOps : SHL | LSHR | ASHR | AND | OR | XOR;
Eric Christopher329d2672008-09-24 04:55:49 +00001142CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | BITCAST |
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001143 UITOFP | SITOFP | FPTOUI | FPTOSI | INTTOPTR | PTRTOINT;
1144
Eric Christopher329d2672008-09-24 04:55:49 +00001145IPredicates
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001146 : EQ { $$ = ICmpInst::ICMP_EQ; } | NE { $$ = ICmpInst::ICMP_NE; }
1147 | SLT { $$ = ICmpInst::ICMP_SLT; } | SGT { $$ = ICmpInst::ICMP_SGT; }
1148 | SLE { $$ = ICmpInst::ICMP_SLE; } | SGE { $$ = ICmpInst::ICMP_SGE; }
1149 | ULT { $$ = ICmpInst::ICMP_ULT; } | UGT { $$ = ICmpInst::ICMP_UGT; }
Eric Christopher329d2672008-09-24 04:55:49 +00001150 | ULE { $$ = ICmpInst::ICMP_ULE; } | UGE { $$ = ICmpInst::ICMP_UGE; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001151 ;
1152
Eric Christopher329d2672008-09-24 04:55:49 +00001153FPredicates
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001154 : OEQ { $$ = FCmpInst::FCMP_OEQ; } | ONE { $$ = FCmpInst::FCMP_ONE; }
1155 | OLT { $$ = FCmpInst::FCMP_OLT; } | OGT { $$ = FCmpInst::FCMP_OGT; }
1156 | OLE { $$ = FCmpInst::FCMP_OLE; } | OGE { $$ = FCmpInst::FCMP_OGE; }
1157 | ORD { $$ = FCmpInst::FCMP_ORD; } | UNO { $$ = FCmpInst::FCMP_UNO; }
1158 | UEQ { $$ = FCmpInst::FCMP_UEQ; } | UNE { $$ = FCmpInst::FCMP_UNE; }
1159 | ULT { $$ = FCmpInst::FCMP_ULT; } | UGT { $$ = FCmpInst::FCMP_UGT; }
1160 | ULE { $$ = FCmpInst::FCMP_ULE; } | UGE { $$ = FCmpInst::FCMP_UGE; }
1161 | TRUETOK { $$ = FCmpInst::FCMP_TRUE; }
1162 | FALSETOK { $$ = FCmpInst::FCMP_FALSE; }
1163 ;
1164
Eric Christopher329d2672008-09-24 04:55:49 +00001165// These are some types that allow classification if we only want a particular
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001166// thing... for example, only a signed, unsigned, or integral type.
1167IntType : INTTYPE;
Dale Johannesenf325d9f2007-08-03 01:03:46 +00001168FPType : FLOAT | DOUBLE | PPC_FP128 | FP128 | X86_FP80;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001169
1170LocalName : LOCALVAR | STRINGCONSTANT | PCTSTRINGCONSTANT ;
1171OptLocalName : LocalName | /*empty*/ { $$ = 0; };
1172
Christopher Lamb668d9a02007-12-12 08:45:45 +00001173OptAddrSpace : ADDRSPACE '(' EUINT64VAL ')' { $$=$3; }
1174 | /*empty*/ { $$=0; };
1175
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001176/// OptLocalAssign - Value producing statements have an optional assignment
1177/// component.
1178OptLocalAssign : LocalName '=' {
1179 $$ = $1;
1180 CHECK_FOR_ERROR
1181 }
1182 | /*empty*/ {
1183 $$ = 0;
1184 CHECK_FOR_ERROR
1185 };
1186
Chris Lattner906773a2008-08-29 17:20:18 +00001187LocalNumber : LOCALVAL_ID '=' {
1188 $$ = $1;
1189 CHECK_FOR_ERROR
1190};
1191
1192
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001193GlobalName : GLOBALVAR | ATSTRINGCONSTANT ;
1194
1195OptGlobalAssign : GlobalAssign
1196 | /*empty*/ {
1197 $$ = 0;
1198 CHECK_FOR_ERROR
1199 };
1200
1201GlobalAssign : GlobalName '=' {
1202 $$ = $1;
1203 CHECK_FOR_ERROR
1204 };
1205
Eric Christopher329d2672008-09-24 04:55:49 +00001206GVInternalLinkage
1207 : INTERNAL { $$ = GlobalValue::InternalLinkage; }
1208 | WEAK { $$ = GlobalValue::WeakLinkage; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001209 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1210 | APPENDING { $$ = GlobalValue::AppendingLinkage; }
Eric Christopher329d2672008-09-24 04:55:49 +00001211 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
Dale Johannesen280e7bc2008-05-14 20:13:36 +00001212 | COMMON { $$ = GlobalValue::CommonLinkage; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001213 ;
1214
1215GVExternalLinkage
1216 : DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1217 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
1218 | EXTERNAL { $$ = GlobalValue::ExternalLinkage; }
1219 ;
1220
1221GVVisibilityStyle
1222 : /*empty*/ { $$ = GlobalValue::DefaultVisibility; }
1223 | DEFAULT { $$ = GlobalValue::DefaultVisibility; }
1224 | HIDDEN { $$ = GlobalValue::HiddenVisibility; }
1225 | PROTECTED { $$ = GlobalValue::ProtectedVisibility; }
1226 ;
1227
1228FunctionDeclareLinkage
1229 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
Eric Christopher329d2672008-09-24 04:55:49 +00001230 | DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001231 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
1232 ;
Eric Christopher329d2672008-09-24 04:55:49 +00001233
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001234FunctionDefineLinkage
1235 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1236 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
1237 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1238 | WEAK { $$ = GlobalValue::WeakLinkage; }
Eric Christopher329d2672008-09-24 04:55:49 +00001239 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
1240 ;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001241
1242AliasLinkage
1243 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1244 | WEAK { $$ = GlobalValue::WeakLinkage; }
1245 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
1246 ;
1247
1248OptCallingConv : /*empty*/ { $$ = CallingConv::C; } |
1249 CCC_TOK { $$ = CallingConv::C; } |
1250 FASTCC_TOK { $$ = CallingConv::Fast; } |
1251 COLDCC_TOK { $$ = CallingConv::Cold; } |
1252 X86_STDCALLCC_TOK { $$ = CallingConv::X86_StdCall; } |
1253 X86_FASTCALLCC_TOK { $$ = CallingConv::X86_FastCall; } |
1254 CC_TOK EUINT64VAL {
1255 if ((unsigned)$2 != $2)
1256 GEN_ERROR("Calling conv too large");
1257 $$ = $2;
1258 CHECK_FOR_ERROR
1259 };
1260
Devang Pateld222f862008-09-25 21:00:45 +00001261Attribute : ZEROEXT { $$ = Attribute::ZExt; }
1262 | ZEXT { $$ = Attribute::ZExt; }
1263 | SIGNEXT { $$ = Attribute::SExt; }
1264 | SEXT { $$ = Attribute::SExt; }
1265 | INREG { $$ = Attribute::InReg; }
1266 | SRET { $$ = Attribute::StructRet; }
1267 | NOALIAS { $$ = Attribute::NoAlias; }
1268 | BYVAL { $$ = Attribute::ByVal; }
1269 | NEST { $$ = Attribute::Nest; }
Eric Christopher329d2672008-09-24 04:55:49 +00001270 | ALIGN EUINT64VAL { $$ =
Devang Pateld222f862008-09-25 21:00:45 +00001271 Attribute::constructAlignmentFromInt($2); }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001272 ;
1273
Devang Pateld222f862008-09-25 21:00:45 +00001274OptAttributes : /* empty */ { $$ = Attribute::None; }
1275 | OptAttributes Attribute {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001276 $$ = $1 | $2;
1277 }
1278 ;
1279
Devang Patelcd842482008-09-29 20:49:50 +00001280RetAttr : INREG { $$ = Attribute::InReg; }
1281 | ZEROEXT { $$ = Attribute::ZExt; }
1282 | SIGNEXT { $$ = Attribute::SExt; }
1283 ;
1284
1285OptRetAttrs : /* empty */ { $$ = Attribute::None; }
1286 | OptRetAttrs RetAttr {
1287 $$ = $1 | $2;
1288 }
1289 ;
1290
1291
Devang Pateld222f862008-09-25 21:00:45 +00001292FuncAttr : NORETURN { $$ = Attribute::NoReturn; }
1293 | NOUNWIND { $$ = Attribute::NoUnwind; }
1294 | INREG { $$ = Attribute::InReg; }
1295 | ZEROEXT { $$ = Attribute::ZExt; }
1296 | SIGNEXT { $$ = Attribute::SExt; }
1297 | READNONE { $$ = Attribute::ReadNone; }
1298 | READONLY { $$ = Attribute::ReadOnly; }
Devang Patel008cd3e2008-09-26 23:51:19 +00001299 | NOINLINE { $$ = Attribute::NoInline }
1300 | ALWAYSINLINE { $$ = Attribute::AlwaysInline }
1301 | OPTSIZE { $$ = Attribute::OptimizeForSize }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001302 ;
1303
Devang Pateld222f862008-09-25 21:00:45 +00001304OptFuncAttrs : /* empty */ { $$ = Attribute::None; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001305 | OptFuncAttrs FuncAttr {
1306 $$ = $1 | $2;
1307 }
1308 ;
1309
Devang Patelcd842482008-09-29 20:49:50 +00001310
Gordon Henriksen13fe5e32007-12-10 03:18:06 +00001311OptGC : /* empty */ { $$ = 0; }
1312 | GC STRINGCONSTANT {
1313 $$ = $2;
1314 }
1315 ;
1316
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001317// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1318// a comma before it.
1319OptAlign : /*empty*/ { $$ = 0; } |
1320 ALIGN EUINT64VAL {
1321 $$ = $2;
1322 if ($$ != 0 && !isPowerOf2_32($$))
1323 GEN_ERROR("Alignment must be a power of two");
1324 CHECK_FOR_ERROR
1325};
1326OptCAlign : /*empty*/ { $$ = 0; } |
1327 ',' ALIGN EUINT64VAL {
1328 $$ = $3;
1329 if ($$ != 0 && !isPowerOf2_32($$))
1330 GEN_ERROR("Alignment must be a power of two");
1331 CHECK_FOR_ERROR
1332};
1333
1334
Christopher Lamb0a243582007-12-11 09:02:08 +00001335
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001336SectionString : SECTION STRINGCONSTANT {
1337 for (unsigned i = 0, e = $2->length(); i != e; ++i)
1338 if ((*$2)[i] == '"' || (*$2)[i] == '\\')
1339 GEN_ERROR("Invalid character in section name");
1340 $$ = $2;
1341 CHECK_FOR_ERROR
1342};
1343
1344OptSection : /*empty*/ { $$ = 0; } |
1345 SectionString { $$ = $1; };
1346
1347// GlobalVarAttributes - Used to pass the attributes string on a global. CurGV
1348// is set to be the global we are processing.
1349//
1350GlobalVarAttributes : /* empty */ {} |
1351 ',' GlobalVarAttribute GlobalVarAttributes {};
1352GlobalVarAttribute : SectionString {
1353 CurGV->setSection(*$1);
1354 delete $1;
1355 CHECK_FOR_ERROR
Eric Christopher329d2672008-09-24 04:55:49 +00001356 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001357 | ALIGN EUINT64VAL {
1358 if ($2 != 0 && !isPowerOf2_32($2))
1359 GEN_ERROR("Alignment must be a power of two");
1360 CurGV->setAlignment($2);
1361 CHECK_FOR_ERROR
1362 };
1363
1364//===----------------------------------------------------------------------===//
1365// Types includes all predefined types... except void, because it can only be
Eric Christopher329d2672008-09-24 04:55:49 +00001366// used in specific contexts (function returning void for example).
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001367
1368// Derived types are added later...
1369//
Dale Johannesenf325d9f2007-08-03 01:03:46 +00001370PrimType : INTTYPE | FLOAT | DOUBLE | PPC_FP128 | FP128 | X86_FP80 | LABEL ;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001371
Eric Christopher329d2672008-09-24 04:55:49 +00001372Types
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001373 : OPAQUE {
1374 $$ = new PATypeHolder(OpaqueType::get());
1375 CHECK_FOR_ERROR
1376 }
1377 | PrimType {
1378 $$ = new PATypeHolder($1);
1379 CHECK_FOR_ERROR
1380 }
Christopher Lamb668d9a02007-12-12 08:45:45 +00001381 | Types OptAddrSpace '*' { // Pointer type?
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001382 if (*$1 == Type::LabelTy)
1383 GEN_ERROR("Cannot form a pointer to a basic block");
Christopher Lamb668d9a02007-12-12 08:45:45 +00001384 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1, $2)));
Christopher Lamb0a243582007-12-11 09:02:08 +00001385 delete $1;
1386 CHECK_FOR_ERROR
1387 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001388 | SymbolicValueRef { // Named types are also simple types...
1389 const Type* tmp = getTypeVal($1);
1390 CHECK_FOR_ERROR
1391 $$ = new PATypeHolder(tmp);
1392 }
1393 | '\\' EUINT64VAL { // Type UpReference
1394 if ($2 > (uint64_t)~0U) GEN_ERROR("Value out of range");
1395 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
1396 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
1397 $$ = new PATypeHolder(OT);
1398 UR_OUT("New Upreference!\n");
1399 CHECK_FOR_ERROR
1400 }
1401 | Types '(' ArgTypeListI ')' OptFuncAttrs {
Duncan Sandsf5588dc2007-11-27 13:23:08 +00001402 // Allow but ignore attributes on function types; this permits auto-upgrade.
1403 // FIXME: remove in LLVM 3.0.
Chris Lattner73de3c02008-04-23 05:37:08 +00001404 const Type *RetTy = *$1;
1405 if (!FunctionType::isValidReturnType(RetTy))
1406 GEN_ERROR("Invalid result type for LLVM function");
Eric Christopher329d2672008-09-24 04:55:49 +00001407
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001408 std::vector<const Type*> Params;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001409 TypeWithAttrsList::iterator I = $3->begin(), E = $3->end();
Duncan Sandsf5588dc2007-11-27 13:23:08 +00001410 for (; I != E; ++I ) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001411 const Type *Ty = I->Ty->get();
1412 Params.push_back(Ty);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001413 }
Anton Korobeynikova2c02272007-12-03 19:16:54 +00001414
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001415 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1416 if (isVarArg) Params.pop_back();
1417
Anton Korobeynikove286f6d2007-12-03 21:01:29 +00001418 for (unsigned i = 0; i != Params.size(); ++i)
1419 if (!(Params[i]->isFirstClassType() || isa<OpaqueType>(Params[i])))
1420 GEN_ERROR("Function arguments must be value types!");
1421
1422 CHECK_FOR_ERROR
1423
Anton Korobeynikova2c02272007-12-03 19:16:54 +00001424 FunctionType *FT = FunctionType::get(RetTy, Params, isVarArg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001425 delete $3; // Delete the argument list
1426 delete $1; // Delete the return type handle
Eric Christopher329d2672008-09-24 04:55:49 +00001427 $$ = new PATypeHolder(HandleUpRefs(FT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001428 CHECK_FOR_ERROR
1429 }
1430 | VOID '(' ArgTypeListI ')' OptFuncAttrs {
Duncan Sandsf5588dc2007-11-27 13:23:08 +00001431 // Allow but ignore attributes on function types; this permits auto-upgrade.
1432 // FIXME: remove in LLVM 3.0.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001433 std::vector<const Type*> Params;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001434 TypeWithAttrsList::iterator I = $3->begin(), E = $3->end();
Duncan Sandsf5588dc2007-11-27 13:23:08 +00001435 for ( ; I != E; ++I ) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001436 const Type* Ty = I->Ty->get();
1437 Params.push_back(Ty);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001438 }
Anton Korobeynikova2c02272007-12-03 19:16:54 +00001439
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001440 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1441 if (isVarArg) Params.pop_back();
1442
Anton Korobeynikove286f6d2007-12-03 21:01:29 +00001443 for (unsigned i = 0; i != Params.size(); ++i)
1444 if (!(Params[i]->isFirstClassType() || isa<OpaqueType>(Params[i])))
1445 GEN_ERROR("Function arguments must be value types!");
1446
1447 CHECK_FOR_ERROR
1448
Duncan Sandsf5588dc2007-11-27 13:23:08 +00001449 FunctionType *FT = FunctionType::get($1, Params, isVarArg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001450 delete $3; // Delete the argument list
Eric Christopher329d2672008-09-24 04:55:49 +00001451 $$ = new PATypeHolder(HandleUpRefs(FT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001452 CHECK_FOR_ERROR
1453 }
1454
1455 | '[' EUINT64VAL 'x' Types ']' { // Sized array type?
Dan Gohmane5febe42008-05-31 00:58:22 +00001456 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, $2)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001457 delete $4;
1458 CHECK_FOR_ERROR
1459 }
1460 | '<' EUINT64VAL 'x' Types '>' { // Vector type?
1461 const llvm::Type* ElemTy = $4->get();
1462 if ((unsigned)$2 != $2)
1463 GEN_ERROR("Unsigned result not equal to signed result");
1464 if (!ElemTy->isFloatingPoint() && !ElemTy->isInteger())
1465 GEN_ERROR("Element type of a VectorType must be primitive");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001466 $$ = new PATypeHolder(HandleUpRefs(VectorType::get(*$4, (unsigned)$2)));
1467 delete $4;
1468 CHECK_FOR_ERROR
1469 }
1470 | '{' TypeListI '}' { // Structure type?
1471 std::vector<const Type*> Elements;
1472 for (std::list<llvm::PATypeHolder>::iterator I = $2->begin(),
1473 E = $2->end(); I != E; ++I)
1474 Elements.push_back(*I);
1475
1476 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
1477 delete $2;
1478 CHECK_FOR_ERROR
1479 }
1480 | '{' '}' { // Empty structure type?
1481 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
1482 CHECK_FOR_ERROR
1483 }
1484 | '<' '{' TypeListI '}' '>' {
1485 std::vector<const Type*> Elements;
1486 for (std::list<llvm::PATypeHolder>::iterator I = $3->begin(),
1487 E = $3->end(); I != E; ++I)
1488 Elements.push_back(*I);
1489
1490 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements, true)));
1491 delete $3;
1492 CHECK_FOR_ERROR
1493 }
1494 | '<' '{' '}' '>' { // Empty structure type?
1495 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>(), true));
1496 CHECK_FOR_ERROR
1497 }
1498 ;
1499
Eric Christopher329d2672008-09-24 04:55:49 +00001500ArgType
Devang Pateld222f862008-09-25 21:00:45 +00001501 : Types OptAttributes {
Duncan Sandsf5588dc2007-11-27 13:23:08 +00001502 // Allow but ignore attributes on function types; this permits auto-upgrade.
1503 // FIXME: remove in LLVM 3.0.
Eric Christopher329d2672008-09-24 04:55:49 +00001504 $$.Ty = $1;
Devang Pateld222f862008-09-25 21:00:45 +00001505 $$.Attrs = Attribute::None;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001506 }
1507 ;
1508
1509ResultTypes
1510 : Types {
1511 if (!UpRefs.empty())
1512 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Devang Patel3d5a1e862008-02-23 01:17:37 +00001513 if (!(*$1)->isFirstClassType() && !isa<StructType>($1->get()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001514 GEN_ERROR("LLVM functions cannot return aggregate types");
1515 $$ = $1;
1516 }
1517 | VOID {
1518 $$ = new PATypeHolder(Type::VoidTy);
1519 }
1520 ;
1521
1522ArgTypeList : ArgType {
1523 $$ = new TypeWithAttrsList();
1524 $$->push_back($1);
1525 CHECK_FOR_ERROR
1526 }
1527 | ArgTypeList ',' ArgType {
1528 ($$=$1)->push_back($3);
1529 CHECK_FOR_ERROR
1530 }
1531 ;
1532
Eric Christopher329d2672008-09-24 04:55:49 +00001533ArgTypeListI
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001534 : ArgTypeList
1535 | ArgTypeList ',' DOTDOTDOT {
1536 $$=$1;
Devang Pateld222f862008-09-25 21:00:45 +00001537 TypeWithAttrs TWA; TWA.Attrs = Attribute::None;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001538 TWA.Ty = new PATypeHolder(Type::VoidTy);
1539 $$->push_back(TWA);
1540 CHECK_FOR_ERROR
1541 }
1542 | DOTDOTDOT {
1543 $$ = new TypeWithAttrsList;
Devang Pateld222f862008-09-25 21:00:45 +00001544 TypeWithAttrs TWA; TWA.Attrs = Attribute::None;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001545 TWA.Ty = new PATypeHolder(Type::VoidTy);
1546 $$->push_back(TWA);
1547 CHECK_FOR_ERROR
1548 }
1549 | /*empty*/ {
1550 $$ = new TypeWithAttrsList();
1551 CHECK_FOR_ERROR
1552 };
1553
Eric Christopher329d2672008-09-24 04:55:49 +00001554// TypeList - Used for struct declarations and as a basis for function type
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001555// declaration type lists
1556//
1557TypeListI : Types {
1558 $$ = new std::list<PATypeHolder>();
Eric Christopher329d2672008-09-24 04:55:49 +00001559 $$->push_back(*$1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001560 delete $1;
1561 CHECK_FOR_ERROR
1562 }
1563 | TypeListI ',' Types {
Eric Christopher329d2672008-09-24 04:55:49 +00001564 ($$=$1)->push_back(*$3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001565 delete $3;
1566 CHECK_FOR_ERROR
1567 };
1568
1569// ConstVal - The various declarations that go into the constant pool. This
1570// production is used ONLY to represent constants that show up AFTER a 'const',
1571// 'constant' or 'global' token at global scope. Constants that can be inlined
1572// into other expressions (such as integers and constexprs) are handled by the
1573// ResolvedVal, ValueRef and ConstValueRef productions.
1574//
1575ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
1576 if (!UpRefs.empty())
1577 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1578 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
1579 if (ATy == 0)
Eric Christopher329d2672008-09-24 04:55:49 +00001580 GEN_ERROR("Cannot make array constant with type: '" +
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001581 (*$1)->getDescription() + "'");
1582 const Type *ETy = ATy->getElementType();
Dan Gohman7185e4b2008-06-23 18:43:26 +00001583 uint64_t NumElements = ATy->getNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001584
1585 // Verify that we have the correct size...
Mon P Wang6bde9ec2008-06-25 08:15:39 +00001586 if (NumElements != uint64_t(-1) && NumElements != $3->size())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001587 GEN_ERROR("Type mismatch: constant sized array initialized with " +
Eric Christopher329d2672008-09-24 04:55:49 +00001588 utostr($3->size()) + " arguments, but has size of " +
Mon P Wang6bde9ec2008-06-25 08:15:39 +00001589 utostr(NumElements) + "");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001590
1591 // Verify all elements are correct type!
1592 for (unsigned i = 0; i < $3->size(); i++) {
1593 if (ETy != (*$3)[i]->getType())
Eric Christopher329d2672008-09-24 04:55:49 +00001594 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001595 ETy->getDescription() +"' as required!\nIt is of type '"+
1596 (*$3)[i]->getType()->getDescription() + "'.");
1597 }
1598
1599 $$ = ConstantArray::get(ATy, *$3);
1600 delete $1; delete $3;
1601 CHECK_FOR_ERROR
1602 }
1603 | Types '[' ']' {
1604 if (!UpRefs.empty())
1605 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1606 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
1607 if (ATy == 0)
Eric Christopher329d2672008-09-24 04:55:49 +00001608 GEN_ERROR("Cannot make array constant with type: '" +
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001609 (*$1)->getDescription() + "'");
1610
Dan Gohman7185e4b2008-06-23 18:43:26 +00001611 uint64_t NumElements = ATy->getNumElements();
Eric Christopher329d2672008-09-24 04:55:49 +00001612 if (NumElements != uint64_t(-1) && NumElements != 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001613 GEN_ERROR("Type mismatch: constant sized array initialized with 0"
Mon P Wang6bde9ec2008-06-25 08:15:39 +00001614 " arguments, but has size of " + utostr(NumElements) +"");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001615 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
1616 delete $1;
1617 CHECK_FOR_ERROR
1618 }
1619 | Types 'c' STRINGCONSTANT {
1620 if (!UpRefs.empty())
1621 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1622 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
1623 if (ATy == 0)
Eric Christopher329d2672008-09-24 04:55:49 +00001624 GEN_ERROR("Cannot make array constant with type: '" +
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001625 (*$1)->getDescription() + "'");
1626
Dan Gohman7185e4b2008-06-23 18:43:26 +00001627 uint64_t NumElements = ATy->getNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001628 const Type *ETy = ATy->getElementType();
Mon P Wang6bde9ec2008-06-25 08:15:39 +00001629 if (NumElements != uint64_t(-1) && NumElements != $3->length())
Eric Christopher329d2672008-09-24 04:55:49 +00001630 GEN_ERROR("Can't build string constant of size " +
Mon P Wang6bde9ec2008-06-25 08:15:39 +00001631 utostr($3->length()) +
1632 " when array has size " + utostr(NumElements) + "");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001633 std::vector<Constant*> Vals;
1634 if (ETy == Type::Int8Ty) {
Mon P Wang6bde9ec2008-06-25 08:15:39 +00001635 for (uint64_t i = 0; i < $3->length(); ++i)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001636 Vals.push_back(ConstantInt::get(ETy, (*$3)[i]));
1637 } else {
1638 delete $3;
1639 GEN_ERROR("Cannot build string arrays of non byte sized elements");
1640 }
1641 delete $3;
1642 $$ = ConstantArray::get(ATy, Vals);
1643 delete $1;
1644 CHECK_FOR_ERROR
1645 }
1646 | Types '<' ConstVector '>' { // Nonempty unsized arr
1647 if (!UpRefs.empty())
1648 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1649 const VectorType *PTy = dyn_cast<VectorType>($1->get());
1650 if (PTy == 0)
Eric Christopher329d2672008-09-24 04:55:49 +00001651 GEN_ERROR("Cannot make packed constant with type: '" +
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001652 (*$1)->getDescription() + "'");
1653 const Type *ETy = PTy->getElementType();
Dan Gohman7185e4b2008-06-23 18:43:26 +00001654 unsigned NumElements = PTy->getNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001655
1656 // Verify that we have the correct size...
Mon P Wang6bde9ec2008-06-25 08:15:39 +00001657 if (NumElements != unsigned(-1) && NumElements != (unsigned)$3->size())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001658 GEN_ERROR("Type mismatch: constant sized packed initialized with " +
Eric Christopher329d2672008-09-24 04:55:49 +00001659 utostr($3->size()) + " arguments, but has size of " +
Mon P Wang6bde9ec2008-06-25 08:15:39 +00001660 utostr(NumElements) + "");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001661
1662 // Verify all elements are correct type!
1663 for (unsigned i = 0; i < $3->size(); i++) {
1664 if (ETy != (*$3)[i]->getType())
Eric Christopher329d2672008-09-24 04:55:49 +00001665 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001666 ETy->getDescription() +"' as required!\nIt is of type '"+
1667 (*$3)[i]->getType()->getDescription() + "'.");
1668 }
1669
1670 $$ = ConstantVector::get(PTy, *$3);
1671 delete $1; delete $3;
1672 CHECK_FOR_ERROR
1673 }
1674 | Types '{' ConstVector '}' {
1675 const StructType *STy = dyn_cast<StructType>($1->get());
1676 if (STy == 0)
Eric Christopher329d2672008-09-24 04:55:49 +00001677 GEN_ERROR("Cannot make struct constant with type: '" +
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001678 (*$1)->getDescription() + "'");
1679
1680 if ($3->size() != STy->getNumContainedTypes())
1681 GEN_ERROR("Illegal number of initializers for structure type");
1682
1683 // Check to ensure that constants are compatible with the type initializer!
1684 for (unsigned i = 0, e = $3->size(); i != e; ++i)
1685 if ((*$3)[i]->getType() != STy->getElementType(i))
1686 GEN_ERROR("Expected type '" +
1687 STy->getElementType(i)->getDescription() +
1688 "' for element #" + utostr(i) +
1689 " of structure initializer");
1690
1691 // Check to ensure that Type is not packed
1692 if (STy->isPacked())
1693 GEN_ERROR("Unpacked Initializer to vector type '" +
1694 STy->getDescription() + "'");
1695
1696 $$ = ConstantStruct::get(STy, *$3);
1697 delete $1; delete $3;
1698 CHECK_FOR_ERROR
1699 }
1700 | Types '{' '}' {
1701 if (!UpRefs.empty())
1702 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1703 const StructType *STy = dyn_cast<StructType>($1->get());
1704 if (STy == 0)
Eric Christopher329d2672008-09-24 04:55:49 +00001705 GEN_ERROR("Cannot make struct constant with type: '" +
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001706 (*$1)->getDescription() + "'");
1707
1708 if (STy->getNumContainedTypes() != 0)
1709 GEN_ERROR("Illegal number of initializers for structure type");
1710
1711 // Check to ensure that Type is not packed
1712 if (STy->isPacked())
1713 GEN_ERROR("Unpacked Initializer to vector type '" +
1714 STy->getDescription() + "'");
1715
1716 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1717 delete $1;
1718 CHECK_FOR_ERROR
1719 }
1720 | Types '<' '{' ConstVector '}' '>' {
1721 const StructType *STy = dyn_cast<StructType>($1->get());
1722 if (STy == 0)
Eric Christopher329d2672008-09-24 04:55:49 +00001723 GEN_ERROR("Cannot make struct constant with type: '" +
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001724 (*$1)->getDescription() + "'");
1725
1726 if ($4->size() != STy->getNumContainedTypes())
1727 GEN_ERROR("Illegal number of initializers for structure type");
1728
1729 // Check to ensure that constants are compatible with the type initializer!
1730 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1731 if ((*$4)[i]->getType() != STy->getElementType(i))
1732 GEN_ERROR("Expected type '" +
1733 STy->getElementType(i)->getDescription() +
1734 "' for element #" + utostr(i) +
1735 " of structure initializer");
1736
1737 // Check to ensure that Type is packed
1738 if (!STy->isPacked())
Eric Christopher329d2672008-09-24 04:55:49 +00001739 GEN_ERROR("Vector initializer to non-vector type '" +
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001740 STy->getDescription() + "'");
1741
1742 $$ = ConstantStruct::get(STy, *$4);
1743 delete $1; delete $4;
1744 CHECK_FOR_ERROR
1745 }
1746 | Types '<' '{' '}' '>' {
1747 if (!UpRefs.empty())
1748 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1749 const StructType *STy = dyn_cast<StructType>($1->get());
1750 if (STy == 0)
Eric Christopher329d2672008-09-24 04:55:49 +00001751 GEN_ERROR("Cannot make struct constant with type: '" +
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001752 (*$1)->getDescription() + "'");
1753
1754 if (STy->getNumContainedTypes() != 0)
1755 GEN_ERROR("Illegal number of initializers for structure type");
1756
1757 // Check to ensure that Type is packed
1758 if (!STy->isPacked())
Eric Christopher329d2672008-09-24 04:55:49 +00001759 GEN_ERROR("Vector initializer to non-vector type '" +
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001760 STy->getDescription() + "'");
1761
1762 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1763 delete $1;
1764 CHECK_FOR_ERROR
1765 }
1766 | Types NULL_TOK {
1767 if (!UpRefs.empty())
1768 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1769 const PointerType *PTy = dyn_cast<PointerType>($1->get());
1770 if (PTy == 0)
Eric Christopher329d2672008-09-24 04:55:49 +00001771 GEN_ERROR("Cannot make null pointer constant with type: '" +
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001772 (*$1)->getDescription() + "'");
1773
1774 $$ = ConstantPointerNull::get(PTy);
1775 delete $1;
1776 CHECK_FOR_ERROR
1777 }
1778 | Types UNDEF {
1779 if (!UpRefs.empty())
1780 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1781 $$ = UndefValue::get($1->get());
1782 delete $1;
1783 CHECK_FOR_ERROR
1784 }
1785 | Types SymbolicValueRef {
1786 if (!UpRefs.empty())
1787 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1788 const PointerType *Ty = dyn_cast<PointerType>($1->get());
1789 if (Ty == 0)
Devang Patel3b8849c2008-02-19 22:27:01 +00001790 GEN_ERROR("Global const reference must be a pointer type " + (*$1)->getDescription());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001791
1792 // ConstExprs can exist in the body of a function, thus creating
1793 // GlobalValues whenever they refer to a variable. Because we are in
1794 // the context of a function, getExistingVal will search the functions
1795 // symbol table instead of the module symbol table for the global symbol,
1796 // which throws things all off. To get around this, we just tell
1797 // getExistingVal that we are at global scope here.
1798 //
1799 Function *SavedCurFn = CurFun.CurrentFunction;
1800 CurFun.CurrentFunction = 0;
1801
1802 Value *V = getExistingVal(Ty, $2);
1803 CHECK_FOR_ERROR
1804
1805 CurFun.CurrentFunction = SavedCurFn;
1806
1807 // If this is an initializer for a constant pointer, which is referencing a
1808 // (currently) undefined variable, create a stub now that shall be replaced
1809 // in the future with the right type of variable.
1810 //
1811 if (V == 0) {
1812 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1813 const PointerType *PT = cast<PointerType>(Ty);
1814
1815 // First check to see if the forward references value is already created!
1816 PerModuleInfo::GlobalRefsType::iterator I =
1817 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Eric Christopher329d2672008-09-24 04:55:49 +00001818
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001819 if (I != CurModule.GlobalRefs.end()) {
1820 V = I->second; // Placeholder already exists, use it...
1821 $2.destroy();
1822 } else {
1823 std::string Name;
1824 if ($2.Type == ValID::GlobalName)
1825 Name = $2.getName();
1826 else if ($2.Type != ValID::GlobalID)
1827 GEN_ERROR("Invalid reference to global");
1828
1829 // Create the forward referenced global.
1830 GlobalValue *GV;
Eric Christopher329d2672008-09-24 04:55:49 +00001831 if (const FunctionType *FTy =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001832 dyn_cast<FunctionType>(PT->getElementType())) {
Gabor Greif89f01162008-04-06 23:07:54 +00001833 GV = Function::Create(FTy, GlobalValue::ExternalWeakLinkage, Name,
1834 CurModule.CurrentModule);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001835 } else {
1836 GV = new GlobalVariable(PT->getElementType(), false,
1837 GlobalValue::ExternalWeakLinkage, 0,
1838 Name, CurModule.CurrentModule);
1839 }
1840
1841 // Keep track of the fact that we have a forward ref to recycle it
1842 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1843 V = GV;
1844 }
1845 }
1846
1847 $$ = cast<GlobalValue>(V);
1848 delete $1; // Free the type handle
1849 CHECK_FOR_ERROR
1850 }
1851 | Types ConstExpr {
1852 if (!UpRefs.empty())
1853 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1854 if ($1->get() != $2->getType())
Eric Christopher329d2672008-09-24 04:55:49 +00001855 GEN_ERROR("Mismatched types for constant expression: " +
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001856 (*$1)->getDescription() + " and " + $2->getType()->getDescription());
1857 $$ = $2;
1858 delete $1;
1859 CHECK_FOR_ERROR
1860 }
1861 | Types ZEROINITIALIZER {
1862 if (!UpRefs.empty())
1863 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1864 const Type *Ty = $1->get();
1865 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
1866 GEN_ERROR("Cannot create a null initialized value of this type");
1867 $$ = Constant::getNullValue(Ty);
1868 delete $1;
1869 CHECK_FOR_ERROR
1870 }
1871 | IntType ESINT64VAL { // integral constants
1872 if (!ConstantInt::isValueValidForType($1, $2))
1873 GEN_ERROR("Constant value doesn't fit in type");
1874 $$ = ConstantInt::get($1, $2, true);
1875 CHECK_FOR_ERROR
1876 }
1877 | IntType ESAPINTVAL { // arbitrary precision integer constants
1878 uint32_t BitWidth = cast<IntegerType>($1)->getBitWidth();
1879 if ($2->getBitWidth() > BitWidth) {
1880 GEN_ERROR("Constant value does not fit in type");
1881 }
1882 $2->sextOrTrunc(BitWidth);
1883 $$ = ConstantInt::get(*$2);
1884 delete $2;
1885 CHECK_FOR_ERROR
1886 }
1887 | IntType EUINT64VAL { // integral constants
1888 if (!ConstantInt::isValueValidForType($1, $2))
1889 GEN_ERROR("Constant value doesn't fit in type");
1890 $$ = ConstantInt::get($1, $2, false);
1891 CHECK_FOR_ERROR
1892 }
1893 | IntType EUAPINTVAL { // arbitrary precision integer constants
1894 uint32_t BitWidth = cast<IntegerType>($1)->getBitWidth();
1895 if ($2->getBitWidth() > BitWidth) {
1896 GEN_ERROR("Constant value does not fit in type");
Eric Christopher329d2672008-09-24 04:55:49 +00001897 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001898 $2->zextOrTrunc(BitWidth);
1899 $$ = ConstantInt::get(*$2);
1900 delete $2;
1901 CHECK_FOR_ERROR
1902 }
1903 | INTTYPE TRUETOK { // Boolean constants
Dan Gohmane5febe42008-05-31 00:58:22 +00001904 if (cast<IntegerType>($1)->getBitWidth() != 1)
1905 GEN_ERROR("Constant true must have type i1");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001906 $$ = ConstantInt::getTrue();
1907 CHECK_FOR_ERROR
1908 }
1909 | INTTYPE FALSETOK { // Boolean constants
Dan Gohmane5febe42008-05-31 00:58:22 +00001910 if (cast<IntegerType>($1)->getBitWidth() != 1)
1911 GEN_ERROR("Constant false must have type i1");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001912 $$ = ConstantInt::getFalse();
1913 CHECK_FOR_ERROR
1914 }
Dale Johannesen043064d2007-09-12 03:31:28 +00001915 | FPType FPVAL { // Floating point constants
Dale Johannesenb9de9f02007-09-06 18:13:44 +00001916 if (!ConstantFP::isValueValidForType($1, *$2))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001917 GEN_ERROR("Floating point constant invalid for type");
Eric Christopher329d2672008-09-24 04:55:49 +00001918 // Lexer has no type info, so builds all float and double FP constants
Dale Johannesen255b8fe2007-09-11 18:33:39 +00001919 // as double. Fix this here. Long double is done right.
1920 if (&$2->getSemantics()==&APFloat::IEEEdouble && $1==Type::FloatTy)
Dale Johannesenb9de9f02007-09-06 18:13:44 +00001921 $2->convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven);
Chris Lattner05ba86e2008-04-20 00:41:19 +00001922 $$ = ConstantFP::get(*$2);
Dale Johannesen3afee192007-09-07 21:07:57 +00001923 delete $2;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001924 CHECK_FOR_ERROR
1925 };
1926
1927
1928ConstExpr: CastOps '(' ConstVal TO Types ')' {
1929 if (!UpRefs.empty())
1930 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
1931 Constant *Val = $3;
1932 const Type *DestTy = $5->get();
1933 if (!CastInst::castIsValid($1, $3, DestTy))
1934 GEN_ERROR("invalid cast opcode for cast from '" +
1935 Val->getType()->getDescription() + "' to '" +
Eric Christopher329d2672008-09-24 04:55:49 +00001936 DestTy->getDescription() + "'");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001937 $$ = ConstantExpr::getCast($1, $3, DestTy);
1938 delete $5;
1939 }
1940 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1941 if (!isa<PointerType>($3->getType()))
1942 GEN_ERROR("GetElementPtr requires a pointer operand");
1943
1944 const Type *IdxTy =
Dan Gohman8055f772008-05-15 19:50:34 +00001945 GetElementPtrInst::getIndexedType($3->getType(), $4->begin(), $4->end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001946 if (!IdxTy)
1947 GEN_ERROR("Index list invalid for constant getelementptr");
1948
1949 SmallVector<Constant*, 8> IdxVec;
1950 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1951 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
1952 IdxVec.push_back(C);
1953 else
1954 GEN_ERROR("Indices to constant getelementptr must be constants");
1955
1956 delete $4;
1957
1958 $$ = ConstantExpr::getGetElementPtr($3, &IdxVec[0], IdxVec.size());
1959 CHECK_FOR_ERROR
1960 }
1961 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1962 if ($3->getType() != Type::Int1Ty)
1963 GEN_ERROR("Select condition must be of boolean type");
1964 if ($5->getType() != $7->getType())
1965 GEN_ERROR("Select operand types must match");
1966 $$ = ConstantExpr::getSelect($3, $5, $7);
1967 CHECK_FOR_ERROR
1968 }
1969 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
1970 if ($3->getType() != $5->getType())
1971 GEN_ERROR("Binary operator types must match");
1972 CHECK_FOR_ERROR;
1973 $$ = ConstantExpr::get($1, $3, $5);
1974 }
1975 | LogicalOps '(' ConstVal ',' ConstVal ')' {
1976 if ($3->getType() != $5->getType())
1977 GEN_ERROR("Logical operator types must match");
1978 if (!$3->getType()->isInteger()) {
Eric Christopher329d2672008-09-24 04:55:49 +00001979 if (!isa<VectorType>($3->getType()) ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001980 !cast<VectorType>($3->getType())->getElementType()->isInteger())
1981 GEN_ERROR("Logical operator requires integral operands");
1982 }
1983 $$ = ConstantExpr::get($1, $3, $5);
1984 CHECK_FOR_ERROR
1985 }
1986 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
1987 if ($4->getType() != $6->getType())
1988 GEN_ERROR("icmp operand types must match");
1989 $$ = ConstantExpr::getICmp($2, $4, $6);
1990 }
1991 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
1992 if ($4->getType() != $6->getType())
1993 GEN_ERROR("fcmp operand types must match");
1994 $$ = ConstantExpr::getFCmp($2, $4, $6);
1995 }
Nate Begeman646fa482008-05-12 19:01:56 +00001996 | VICMP IPredicates '(' ConstVal ',' ConstVal ')' {
1997 if ($4->getType() != $6->getType())
1998 GEN_ERROR("vicmp operand types must match");
1999 $$ = ConstantExpr::getVICmp($2, $4, $6);
2000 }
2001 | VFCMP FPredicates '(' ConstVal ',' ConstVal ')' {
2002 if ($4->getType() != $6->getType())
2003 GEN_ERROR("vfcmp operand types must match");
2004 $$ = ConstantExpr::getVFCmp($2, $4, $6);
2005 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002006 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
2007 if (!ExtractElementInst::isValidOperands($3, $5))
2008 GEN_ERROR("Invalid extractelement operands");
2009 $$ = ConstantExpr::getExtractElement($3, $5);
2010 CHECK_FOR_ERROR
2011 }
2012 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
2013 if (!InsertElementInst::isValidOperands($3, $5, $7))
2014 GEN_ERROR("Invalid insertelement operands");
2015 $$ = ConstantExpr::getInsertElement($3, $5, $7);
2016 CHECK_FOR_ERROR
2017 }
2018 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
2019 if (!ShuffleVectorInst::isValidOperands($3, $5, $7))
2020 GEN_ERROR("Invalid shufflevector operands");
2021 $$ = ConstantExpr::getShuffleVector($3, $5, $7);
2022 CHECK_FOR_ERROR
Dan Gohmane6b1ee62008-05-23 01:55:30 +00002023 }
Dan Gohmane5febe42008-05-31 00:58:22 +00002024 | EXTRACTVALUE '(' ConstVal ConstantIndexList ')' {
Dan Gohmane6b1ee62008-05-23 01:55:30 +00002025 if (!isa<StructType>($3->getType()) && !isa<ArrayType>($3->getType()))
2026 GEN_ERROR("ExtractValue requires an aggregate operand");
2027
Dan Gohmane5febe42008-05-31 00:58:22 +00002028 $$ = ConstantExpr::getExtractValue($3, &(*$4)[0], $4->size());
Dan Gohmane6b1ee62008-05-23 01:55:30 +00002029 delete $4;
Dan Gohmane6b1ee62008-05-23 01:55:30 +00002030 CHECK_FOR_ERROR
2031 }
Dan Gohmane5febe42008-05-31 00:58:22 +00002032 | INSERTVALUE '(' ConstVal ',' ConstVal ConstantIndexList ')' {
Dan Gohmane6b1ee62008-05-23 01:55:30 +00002033 if (!isa<StructType>($3->getType()) && !isa<ArrayType>($3->getType()))
2034 GEN_ERROR("InsertValue requires an aggregate operand");
2035
Dan Gohmane5febe42008-05-31 00:58:22 +00002036 $$ = ConstantExpr::getInsertValue($3, $5, &(*$6)[0], $6->size());
Dan Gohmane6b1ee62008-05-23 01:55:30 +00002037 delete $6;
Dan Gohmane6b1ee62008-05-23 01:55:30 +00002038 CHECK_FOR_ERROR
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002039 };
2040
2041
2042// ConstVector - A list of comma separated constants.
2043ConstVector : ConstVector ',' ConstVal {
2044 ($$ = $1)->push_back($3);
2045 CHECK_FOR_ERROR
2046 }
2047 | ConstVal {
2048 $$ = new std::vector<Constant*>();
2049 $$->push_back($1);
2050 CHECK_FOR_ERROR
2051 };
2052
2053
2054// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
2055GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
2056
Eric Christopher329d2672008-09-24 04:55:49 +00002057// ThreadLocal
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002058ThreadLocal : THREAD_LOCAL { $$ = true; } | { $$ = false; };
2059
2060// AliaseeRef - Match either GlobalValue or bitcast to GlobalValue.
2061AliaseeRef : ResultTypes SymbolicValueRef {
2062 const Type* VTy = $1->get();
2063 Value *V = getVal(VTy, $2);
Chris Lattnerbb856a32007-08-06 21:00:46 +00002064 CHECK_FOR_ERROR
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002065 GlobalValue* Aliasee = dyn_cast<GlobalValue>(V);
2066 if (!Aliasee)
2067 GEN_ERROR("Aliases can be created only to global values");
2068
2069 $$ = Aliasee;
2070 CHECK_FOR_ERROR
2071 delete $1;
2072 }
2073 | BITCAST '(' AliaseeRef TO Types ')' {
2074 Constant *Val = $3;
2075 const Type *DestTy = $5->get();
2076 if (!CastInst::castIsValid($1, $3, DestTy))
2077 GEN_ERROR("invalid cast opcode for cast from '" +
2078 Val->getType()->getDescription() + "' to '" +
2079 DestTy->getDescription() + "'");
Eric Christopher329d2672008-09-24 04:55:49 +00002080
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002081 $$ = ConstantExpr::getCast($1, $3, DestTy);
2082 CHECK_FOR_ERROR
2083 delete $5;
2084 };
2085
2086//===----------------------------------------------------------------------===//
2087// Rules to match Modules
2088//===----------------------------------------------------------------------===//
2089
2090// Module rule: Capture the result of parsing the whole file into a result
2091// variable...
2092//
Eric Christopher329d2672008-09-24 04:55:49 +00002093Module
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002094 : DefinitionList {
2095 $$ = ParserResult = CurModule.CurrentModule;
2096 CurModule.ModuleDone();
2097 CHECK_FOR_ERROR;
2098 }
2099 | /*empty*/ {
2100 $$ = ParserResult = CurModule.CurrentModule;
2101 CurModule.ModuleDone();
2102 CHECK_FOR_ERROR;
2103 }
2104 ;
2105
2106DefinitionList
2107 : Definition
2108 | DefinitionList Definition
2109 ;
2110
Eric Christopher329d2672008-09-24 04:55:49 +00002111Definition
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002112 : DEFINE { CurFun.isDeclare = false; } Function {
2113 CurFun.FunctionDone();
2114 CHECK_FOR_ERROR
2115 }
2116 | DECLARE { CurFun.isDeclare = true; } FunctionProto {
2117 CHECK_FOR_ERROR
2118 }
2119 | MODULE ASM_TOK AsmBlock {
2120 CHECK_FOR_ERROR
Eric Christopher329d2672008-09-24 04:55:49 +00002121 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002122 | OptLocalAssign TYPE Types {
2123 if (!UpRefs.empty())
2124 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
2125 // Eagerly resolve types. This is not an optimization, this is a
2126 // requirement that is due to the fact that we could have this:
2127 //
2128 // %list = type { %list * }
2129 // %list = type { %list * } ; repeated type decl
2130 //
2131 // If types are not resolved eagerly, then the two types will not be
2132 // determined to be the same type!
2133 //
2134 ResolveTypeTo($1, *$3);
2135
2136 if (!setTypeName(*$3, $1) && !$1) {
2137 CHECK_FOR_ERROR
2138 // If this is a named type that is not a redefinition, add it to the slot
2139 // table.
2140 CurModule.Types.push_back(*$3);
2141 }
2142
2143 delete $3;
2144 CHECK_FOR_ERROR
2145 }
2146 | OptLocalAssign TYPE VOID {
2147 ResolveTypeTo($1, $3);
2148
2149 if (!setTypeName($3, $1) && !$1) {
2150 CHECK_FOR_ERROR
2151 // If this is a named type that is not a redefinition, add it to the slot
2152 // table.
2153 CurModule.Types.push_back($3);
2154 }
2155 CHECK_FOR_ERROR
2156 }
Eric Christopher329d2672008-09-24 04:55:49 +00002157 | OptGlobalAssign GVVisibilityStyle ThreadLocal GlobalType ConstVal
2158 OptAddrSpace {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002159 /* "Externally Visible" Linkage */
Eric Christopher329d2672008-09-24 04:55:49 +00002160 if ($5 == 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002161 GEN_ERROR("Global value initializer is not a constant");
2162 CurGV = ParseGlobalVariable($1, GlobalValue::ExternalLinkage,
Christopher Lamb668d9a02007-12-12 08:45:45 +00002163 $2, $4, $5->getType(), $5, $3, $6);
Christopher Lamb0a243582007-12-11 09:02:08 +00002164 CHECK_FOR_ERROR
2165 } GlobalVarAttributes {
2166 CurGV = 0;
2167 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002168 | OptGlobalAssign GVInternalLinkage GVVisibilityStyle ThreadLocal GlobalType
Christopher Lamb668d9a02007-12-12 08:45:45 +00002169 ConstVal OptAddrSpace {
Eric Christopher329d2672008-09-24 04:55:49 +00002170 if ($6 == 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002171 GEN_ERROR("Global value initializer is not a constant");
Christopher Lamb668d9a02007-12-12 08:45:45 +00002172 CurGV = ParseGlobalVariable($1, $2, $3, $5, $6->getType(), $6, $4, $7);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002173 CHECK_FOR_ERROR
2174 } GlobalVarAttributes {
2175 CurGV = 0;
2176 }
2177 | OptGlobalAssign GVExternalLinkage GVVisibilityStyle ThreadLocal GlobalType
Christopher Lamb668d9a02007-12-12 08:45:45 +00002178 Types OptAddrSpace {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002179 if (!UpRefs.empty())
2180 GEN_ERROR("Invalid upreference in type: " + (*$6)->getDescription());
Christopher Lamb668d9a02007-12-12 08:45:45 +00002181 CurGV = ParseGlobalVariable($1, $2, $3, $5, *$6, 0, $4, $7);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002182 CHECK_FOR_ERROR
2183 delete $6;
2184 } GlobalVarAttributes {
2185 CurGV = 0;
2186 CHECK_FOR_ERROR
2187 }
2188 | OptGlobalAssign GVVisibilityStyle ALIAS AliasLinkage AliaseeRef {
2189 std::string Name;
2190 if ($1) {
2191 Name = *$1;
2192 delete $1;
2193 }
2194 if (Name.empty())
2195 GEN_ERROR("Alias name cannot be empty");
Eric Christopher329d2672008-09-24 04:55:49 +00002196
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002197 Constant* Aliasee = $5;
2198 if (Aliasee == 0)
2199 GEN_ERROR(std::string("Invalid aliasee for alias: ") + Name);
2200
2201 GlobalAlias* GA = new GlobalAlias(Aliasee->getType(), $4, Name, Aliasee,
2202 CurModule.CurrentModule);
2203 GA->setVisibility($2);
2204 InsertValue(GA, CurModule.Values);
Eric Christopher329d2672008-09-24 04:55:49 +00002205
2206
Chris Lattner5eefce32007-09-10 23:24:14 +00002207 // If there was a forward reference of this alias, resolve it now.
Eric Christopher329d2672008-09-24 04:55:49 +00002208
Chris Lattner5eefce32007-09-10 23:24:14 +00002209 ValID ID;
2210 if (!Name.empty())
2211 ID = ValID::createGlobalName(Name);
2212 else
2213 ID = ValID::createGlobalID(CurModule.Values.size()-1);
Eric Christopher329d2672008-09-24 04:55:49 +00002214
Chris Lattner5eefce32007-09-10 23:24:14 +00002215 if (GlobalValue *FWGV =
2216 CurModule.GetForwardRefForGlobal(GA->getType(), ID)) {
2217 // Replace uses of the fwdref with the actual alias.
2218 FWGV->replaceAllUsesWith(GA);
2219 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(FWGV))
2220 GV->eraseFromParent();
2221 else
2222 cast<Function>(FWGV)->eraseFromParent();
2223 }
2224 ID.destroy();
Eric Christopher329d2672008-09-24 04:55:49 +00002225
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002226 CHECK_FOR_ERROR
2227 }
Eric Christopher329d2672008-09-24 04:55:49 +00002228 | TARGET TargetDefinition {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002229 CHECK_FOR_ERROR
2230 }
2231 | DEPLIBS '=' LibrariesDefinition {
2232 CHECK_FOR_ERROR
2233 }
2234 ;
2235
2236
2237AsmBlock : STRINGCONSTANT {
2238 const std::string &AsmSoFar = CurModule.CurrentModule->getModuleInlineAsm();
2239 if (AsmSoFar.empty())
2240 CurModule.CurrentModule->setModuleInlineAsm(*$1);
2241 else
2242 CurModule.CurrentModule->setModuleInlineAsm(AsmSoFar+"\n"+*$1);
2243 delete $1;
2244 CHECK_FOR_ERROR
2245};
2246
2247TargetDefinition : TRIPLE '=' STRINGCONSTANT {
2248 CurModule.CurrentModule->setTargetTriple(*$3);
2249 delete $3;
2250 }
2251 | DATALAYOUT '=' STRINGCONSTANT {
2252 CurModule.CurrentModule->setDataLayout(*$3);
2253 delete $3;
2254 };
2255
2256LibrariesDefinition : '[' LibList ']';
2257
2258LibList : LibList ',' STRINGCONSTANT {
2259 CurModule.CurrentModule->addLibrary(*$3);
2260 delete $3;
2261 CHECK_FOR_ERROR
2262 }
2263 | STRINGCONSTANT {
2264 CurModule.CurrentModule->addLibrary(*$1);
2265 delete $1;
2266 CHECK_FOR_ERROR
2267 }
2268 | /* empty: end of list */ {
2269 CHECK_FOR_ERROR
2270 }
2271 ;
2272
2273//===----------------------------------------------------------------------===//
2274// Rules to match Function Headers
2275//===----------------------------------------------------------------------===//
2276
Devang Pateld222f862008-09-25 21:00:45 +00002277ArgListH : ArgListH ',' Types OptAttributes OptLocalName {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002278 if (!UpRefs.empty())
2279 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Dan Gohmane5febe42008-05-31 00:58:22 +00002280 if (!(*$3)->isFirstClassType())
2281 GEN_ERROR("Argument types must be first-class");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002282 ArgListEntry E; E.Attrs = $4; E.Ty = $3; E.Name = $5;
2283 $$ = $1;
2284 $1->push_back(E);
2285 CHECK_FOR_ERROR
2286 }
Devang Pateld222f862008-09-25 21:00:45 +00002287 | Types OptAttributes OptLocalName {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002288 if (!UpRefs.empty())
2289 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Dan Gohmane5febe42008-05-31 00:58:22 +00002290 if (!(*$1)->isFirstClassType())
2291 GEN_ERROR("Argument types must be first-class");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002292 ArgListEntry E; E.Attrs = $2; E.Ty = $1; E.Name = $3;
2293 $$ = new ArgListType;
2294 $$->push_back(E);
2295 CHECK_FOR_ERROR
2296 };
2297
2298ArgList : ArgListH {
2299 $$ = $1;
2300 CHECK_FOR_ERROR
2301 }
2302 | ArgListH ',' DOTDOTDOT {
2303 $$ = $1;
2304 struct ArgListEntry E;
2305 E.Ty = new PATypeHolder(Type::VoidTy);
2306 E.Name = 0;
Devang Pateld222f862008-09-25 21:00:45 +00002307 E.Attrs = Attribute::None;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002308 $$->push_back(E);
2309 CHECK_FOR_ERROR
2310 }
2311 | DOTDOTDOT {
2312 $$ = new ArgListType;
2313 struct ArgListEntry E;
2314 E.Ty = new PATypeHolder(Type::VoidTy);
2315 E.Name = 0;
Devang Pateld222f862008-09-25 21:00:45 +00002316 E.Attrs = Attribute::None;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002317 $$->push_back(E);
2318 CHECK_FOR_ERROR
2319 }
2320 | /* empty */ {
2321 $$ = 0;
2322 CHECK_FOR_ERROR
2323 };
2324
Devang Patelcd842482008-09-29 20:49:50 +00002325FunctionHeaderH : OptCallingConv OptRetAttrs ResultTypes GlobalName '(' ArgList ')'
Devang Patel008cd3e2008-09-26 23:51:19 +00002326 OptFuncAttrs OptSection OptAlign OptGC {
Devang Patelcd842482008-09-29 20:49:50 +00002327 std::string FunctionName(*$4);
2328 delete $4; // Free strdup'd memory!
Eric Christopher329d2672008-09-24 04:55:49 +00002329
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002330 // Check the function result for abstractness if this is a define. We should
2331 // have no abstract types at this point
Devang Patelcd842482008-09-29 20:49:50 +00002332 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved($3))
2333 GEN_ERROR("Reference to abstract result: "+ $3->get()->getDescription());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002334
Devang Patelcd842482008-09-29 20:49:50 +00002335 if (!FunctionType::isValidReturnType(*$3))
Chris Lattner73de3c02008-04-23 05:37:08 +00002336 GEN_ERROR("Invalid result type for LLVM function");
Eric Christopher329d2672008-09-24 04:55:49 +00002337
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002338 std::vector<const Type*> ParamTypeList;
Devang Pateld222f862008-09-25 21:00:45 +00002339 SmallVector<AttributeWithIndex, 8> Attrs;
Devang Patelf2a4a922008-09-26 22:53:05 +00002340 //FIXME : In 3.0, stop accepting zext, sext and inreg as optional function
2341 //attributes.
Devang Patelcd842482008-09-29 20:49:50 +00002342 Attributes RetAttrs = $2;
2343 if ($8 != Attribute::None) {
2344 if ($8 & Attribute::ZExt) {
Devang Patelf2a4a922008-09-26 22:53:05 +00002345 RetAttrs = RetAttrs | Attribute::ZExt;
Devang Patelcd842482008-09-29 20:49:50 +00002346 $8 = $8 ^ Attribute::ZExt;
Devang Patelf2a4a922008-09-26 22:53:05 +00002347 }
Devang Patelcd842482008-09-29 20:49:50 +00002348 if ($8 & Attribute::SExt) {
Devang Patelf2a4a922008-09-26 22:53:05 +00002349 RetAttrs = RetAttrs | Attribute::SExt;
Devang Patelcd842482008-09-29 20:49:50 +00002350 $8 = $8 ^ Attribute::SExt;
Devang Patelf2a4a922008-09-26 22:53:05 +00002351 }
Devang Patelcd842482008-09-29 20:49:50 +00002352 if ($8 & Attribute::InReg) {
Devang Patelf2a4a922008-09-26 22:53:05 +00002353 RetAttrs = RetAttrs | Attribute::InReg;
Devang Patelcd842482008-09-29 20:49:50 +00002354 $8 = $8 ^ Attribute::InReg;
Devang Patelf2a4a922008-09-26 22:53:05 +00002355 }
Devang Patelf2a4a922008-09-26 22:53:05 +00002356 }
Devang Patelcd842482008-09-29 20:49:50 +00002357 if (RetAttrs != Attribute::None)
2358 Attrs.push_back(AttributeWithIndex::get(0, RetAttrs));
2359 if ($6) { // If there are arguments...
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002360 unsigned index = 1;
Devang Patelcd842482008-09-29 20:49:50 +00002361 for (ArgListType::iterator I = $6->begin(); I != $6->end(); ++I, ++index) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002362 const Type* Ty = I->Ty->get();
2363 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved(I->Ty))
2364 GEN_ERROR("Reference to abstract argument: " + Ty->getDescription());
2365 ParamTypeList.push_back(Ty);
Devang Pateld222f862008-09-25 21:00:45 +00002366 if (Ty != Type::VoidTy && I->Attrs != Attribute::None)
2367 Attrs.push_back(AttributeWithIndex::get(index, I->Attrs));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002368 }
2369 }
Devang Patelcd842482008-09-29 20:49:50 +00002370 if ($8 != Attribute::None)
2371 Attrs.push_back(AttributeWithIndex::get(~0, $8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002372
2373 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
2374 if (isVarArg) ParamTypeList.pop_back();
2375
Devang Pateld222f862008-09-25 21:00:45 +00002376 AttrListPtr PAL;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002377 if (!Attrs.empty())
Devang Pateld222f862008-09-25 21:00:45 +00002378 PAL = AttrListPtr::get(Attrs.begin(), Attrs.end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002379
Devang Patelcd842482008-09-29 20:49:50 +00002380 FunctionType *FT = FunctionType::get(*$3, ParamTypeList, isVarArg);
Christopher Lambfb623c62007-12-17 01:17:35 +00002381 const PointerType *PFT = PointerType::getUnqual(FT);
Devang Patelcd842482008-09-29 20:49:50 +00002382 delete $3;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002383
2384 ValID ID;
2385 if (!FunctionName.empty()) {
2386 ID = ValID::createGlobalName((char*)FunctionName.c_str());
2387 } else {
2388 ID = ValID::createGlobalID(CurModule.Values.size());
2389 }
2390
2391 Function *Fn = 0;
2392 // See if this function was forward referenced. If so, recycle the object.
2393 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
Eric Christopher329d2672008-09-24 04:55:49 +00002394 // Move the function to the end of the list, from whereever it was
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002395 // previously inserted.
2396 Fn = cast<Function>(FWRef);
Devang Pateld222f862008-09-25 21:00:45 +00002397 assert(Fn->getAttributes().isEmpty() &&
Chris Lattner1c8733e2008-03-12 17:45:29 +00002398 "Forward reference has parameter attributes!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002399 CurModule.CurrentModule->getFunctionList().remove(Fn);
2400 CurModule.CurrentModule->getFunctionList().push_back(Fn);
2401 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
2402 (Fn = CurModule.CurrentModule->getFunction(FunctionName))) {
Duncan Sandsf5588dc2007-11-27 13:23:08 +00002403 if (Fn->getFunctionType() != FT ) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002404 // The existing function doesn't have the same type. This is an overload
2405 // error.
2406 GEN_ERROR("Overload of function '" + FunctionName + "' not permitted.");
Devang Pateld222f862008-09-25 21:00:45 +00002407 } else if (Fn->getAttributes() != PAL) {
Duncan Sandsf5588dc2007-11-27 13:23:08 +00002408 // The existing function doesn't have the same parameter attributes.
2409 // This is an overload error.
2410 GEN_ERROR("Overload of function '" + FunctionName + "' not permitted.");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002411 } else if (!CurFun.isDeclare && !Fn->isDeclaration()) {
2412 // Neither the existing or the current function is a declaration and they
2413 // have the same name and same type. Clearly this is a redefinition.
2414 GEN_ERROR("Redefinition of function '" + FunctionName + "'");
Duncan Sandsf5588dc2007-11-27 13:23:08 +00002415 } else if (Fn->isDeclaration()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002416 // Make sure to strip off any argument names so we can't get conflicts.
2417 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
2418 AI != AE; ++AI)
2419 AI->setName("");
2420 }
2421 } else { // Not already defined?
Gabor Greif89f01162008-04-06 23:07:54 +00002422 Fn = Function::Create(FT, GlobalValue::ExternalWeakLinkage, FunctionName,
2423 CurModule.CurrentModule);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002424 InsertValue(Fn, CurModule.Values);
2425 }
2426
Nuno Lopese20dbca2008-10-03 15:45:58 +00002427 ID.destroy();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002428 CurFun.FunctionStart(Fn);
2429
2430 if (CurFun.isDeclare) {
2431 // If we have declaration, always overwrite linkage. This will allow us to
2432 // correctly handle cases, when pointer to function is passed as argument to
2433 // another function.
2434 Fn->setLinkage(CurFun.Linkage);
2435 Fn->setVisibility(CurFun.Visibility);
2436 }
2437 Fn->setCallingConv($1);
Devang Pateld222f862008-09-25 21:00:45 +00002438 Fn->setAttributes(PAL);
Devang Patelcd842482008-09-29 20:49:50 +00002439 Fn->setAlignment($10);
2440 if ($9) {
2441 Fn->setSection(*$9);
2442 delete $9;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002443 }
Devang Patelcd842482008-09-29 20:49:50 +00002444 if ($11) {
2445 Fn->setGC($11->c_str());
2446 delete $11;
Gordon Henriksen13fe5e32007-12-10 03:18:06 +00002447 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002448
2449 // Add all of the arguments we parsed to the function...
Devang Patelcd842482008-09-29 20:49:50 +00002450 if ($6) { // Is null if empty...
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002451 if (isVarArg) { // Nuke the last entry
Devang Patelcd842482008-09-29 20:49:50 +00002452 assert($6->back().Ty->get() == Type::VoidTy && $6->back().Name == 0 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002453 "Not a varargs marker!");
Devang Patelcd842482008-09-29 20:49:50 +00002454 delete $6->back().Ty;
2455 $6->pop_back(); // Delete the last entry
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002456 }
2457 Function::arg_iterator ArgIt = Fn->arg_begin();
2458 Function::arg_iterator ArgEnd = Fn->arg_end();
2459 unsigned Idx = 1;
Devang Patelcd842482008-09-29 20:49:50 +00002460 for (ArgListType::iterator I = $6->begin();
2461 I != $6->end() && ArgIt != ArgEnd; ++I, ++ArgIt) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002462 delete I->Ty; // Delete the typeholder...
2463 setValueName(ArgIt, I->Name); // Insert arg into symtab...
2464 CHECK_FOR_ERROR
2465 InsertValue(ArgIt);
2466 Idx++;
2467 }
2468
Devang Patelcd842482008-09-29 20:49:50 +00002469 delete $6; // We're now done with the argument list
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002470 }
2471 CHECK_FOR_ERROR
2472};
2473
2474BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
2475
2476FunctionHeader : FunctionDefineLinkage GVVisibilityStyle FunctionHeaderH BEGIN {
2477 $$ = CurFun.CurrentFunction;
2478
2479 // Make sure that we keep track of the linkage type even if there was a
2480 // previous "declare".
2481 $$->setLinkage($1);
2482 $$->setVisibility($2);
2483};
2484
2485END : ENDTOK | '}'; // Allow end of '}' to end a function
2486
2487Function : BasicBlockList END {
2488 $$ = $1;
2489 CHECK_FOR_ERROR
2490};
2491
2492FunctionProto : FunctionDeclareLinkage GVVisibilityStyle FunctionHeaderH {
2493 CurFun.CurrentFunction->setLinkage($1);
2494 CurFun.CurrentFunction->setVisibility($2);
2495 $$ = CurFun.CurrentFunction;
2496 CurFun.FunctionDone();
2497 CHECK_FOR_ERROR
2498 };
2499
2500//===----------------------------------------------------------------------===//
2501// Rules to match Basic Blocks
2502//===----------------------------------------------------------------------===//
2503
2504OptSideEffect : /* empty */ {
2505 $$ = false;
2506 CHECK_FOR_ERROR
2507 }
2508 | SIDEEFFECT {
2509 $$ = true;
2510 CHECK_FOR_ERROR
2511 };
2512
2513ConstValueRef : ESINT64VAL { // A reference to a direct constant
2514 $$ = ValID::create($1);
2515 CHECK_FOR_ERROR
2516 }
2517 | EUINT64VAL {
2518 $$ = ValID::create($1);
2519 CHECK_FOR_ERROR
2520 }
Chris Lattnerf3d40022008-07-11 00:30:39 +00002521 | ESAPINTVAL { // arbitrary precision integer constants
2522 $$ = ValID::create(*$1, true);
2523 delete $1;
2524 CHECK_FOR_ERROR
Eric Christopher329d2672008-09-24 04:55:49 +00002525 }
Chris Lattnerf3d40022008-07-11 00:30:39 +00002526 | EUAPINTVAL { // arbitrary precision integer constants
2527 $$ = ValID::create(*$1, false);
2528 delete $1;
2529 CHECK_FOR_ERROR
2530 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002531 | FPVAL { // Perhaps it's an FP constant?
2532 $$ = ValID::create($1);
2533 CHECK_FOR_ERROR
2534 }
2535 | TRUETOK {
2536 $$ = ValID::create(ConstantInt::getTrue());
2537 CHECK_FOR_ERROR
Eric Christopher329d2672008-09-24 04:55:49 +00002538 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002539 | FALSETOK {
2540 $$ = ValID::create(ConstantInt::getFalse());
2541 CHECK_FOR_ERROR
2542 }
2543 | NULL_TOK {
2544 $$ = ValID::createNull();
2545 CHECK_FOR_ERROR
2546 }
2547 | UNDEF {
2548 $$ = ValID::createUndef();
2549 CHECK_FOR_ERROR
2550 }
2551 | ZEROINITIALIZER { // A vector zero constant.
2552 $$ = ValID::createZeroInit();
2553 CHECK_FOR_ERROR
2554 }
2555 | '<' ConstVector '>' { // Nonempty unsized packed vector
2556 const Type *ETy = (*$2)[0]->getType();
Eric Christopher329d2672008-09-24 04:55:49 +00002557 unsigned NumElements = $2->size();
Dan Gohmane5febe42008-05-31 00:58:22 +00002558
2559 if (!ETy->isInteger() && !ETy->isFloatingPoint())
2560 GEN_ERROR("Invalid vector element type: " + ETy->getDescription());
Eric Christopher329d2672008-09-24 04:55:49 +00002561
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002562 VectorType* pt = VectorType::get(ETy, NumElements);
Dan Gohman9fc6cb02008-06-09 14:45:02 +00002563 PATypeHolder* PTy = new PATypeHolder(HandleUpRefs(pt));
Eric Christopher329d2672008-09-24 04:55:49 +00002564
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002565 // Verify all elements are correct type!
2566 for (unsigned i = 0; i < $2->size(); i++) {
2567 if (ETy != (*$2)[i]->getType())
Eric Christopher329d2672008-09-24 04:55:49 +00002568 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002569 ETy->getDescription() +"' as required!\nIt is of type '" +
2570 (*$2)[i]->getType()->getDescription() + "'.");
2571 }
2572
2573 $$ = ValID::create(ConstantVector::get(pt, *$2));
2574 delete PTy; delete $2;
2575 CHECK_FOR_ERROR
2576 }
Dan Gohman9fc6cb02008-06-09 14:45:02 +00002577 | '[' ConstVector ']' { // Nonempty unsized arr
2578 const Type *ETy = (*$2)[0]->getType();
Eric Christopher329d2672008-09-24 04:55:49 +00002579 uint64_t NumElements = $2->size();
Dan Gohman9fc6cb02008-06-09 14:45:02 +00002580
2581 if (!ETy->isFirstClassType())
2582 GEN_ERROR("Invalid array element type: " + ETy->getDescription());
2583
2584 ArrayType *ATy = ArrayType::get(ETy, NumElements);
2585 PATypeHolder* PTy = new PATypeHolder(HandleUpRefs(ATy));
2586
2587 // Verify all elements are correct type!
2588 for (unsigned i = 0; i < $2->size(); i++) {
2589 if (ETy != (*$2)[i]->getType())
Eric Christopher329d2672008-09-24 04:55:49 +00002590 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Dan Gohman9fc6cb02008-06-09 14:45:02 +00002591 ETy->getDescription() +"' as required!\nIt is of type '"+
2592 (*$2)[i]->getType()->getDescription() + "'.");
2593 }
2594
2595 $$ = ValID::create(ConstantArray::get(ATy, *$2));
2596 delete PTy; delete $2;
2597 CHECK_FOR_ERROR
2598 }
2599 | '[' ']' {
Dan Gohman7185e4b2008-06-23 18:43:26 +00002600 // Use undef instead of an array because it's inconvenient to determine
2601 // the element type at this point, there being no elements to examine.
Dan Gohman9fc6cb02008-06-09 14:45:02 +00002602 $$ = ValID::createUndef();
2603 CHECK_FOR_ERROR
2604 }
2605 | 'c' STRINGCONSTANT {
Dan Gohman7185e4b2008-06-23 18:43:26 +00002606 uint64_t NumElements = $2->length();
Dan Gohman9fc6cb02008-06-09 14:45:02 +00002607 const Type *ETy = Type::Int8Ty;
2608
2609 ArrayType *ATy = ArrayType::get(ETy, NumElements);
2610
2611 std::vector<Constant*> Vals;
2612 for (unsigned i = 0; i < $2->length(); ++i)
2613 Vals.push_back(ConstantInt::get(ETy, (*$2)[i]));
2614 delete $2;
2615 $$ = ValID::create(ConstantArray::get(ATy, Vals));
2616 CHECK_FOR_ERROR
2617 }
2618 | '{' ConstVector '}' {
2619 std::vector<const Type*> Elements($2->size());
2620 for (unsigned i = 0, e = $2->size(); i != e; ++i)
2621 Elements[i] = (*$2)[i]->getType();
2622
2623 const StructType *STy = StructType::get(Elements);
2624 PATypeHolder* PTy = new PATypeHolder(HandleUpRefs(STy));
2625
2626 $$ = ValID::create(ConstantStruct::get(STy, *$2));
2627 delete PTy; delete $2;
2628 CHECK_FOR_ERROR
2629 }
2630 | '{' '}' {
2631 const StructType *STy = StructType::get(std::vector<const Type*>());
2632 $$ = ValID::create(ConstantStruct::get(STy, std::vector<Constant*>()));
2633 CHECK_FOR_ERROR
2634 }
2635 | '<' '{' ConstVector '}' '>' {
2636 std::vector<const Type*> Elements($3->size());
2637 for (unsigned i = 0, e = $3->size(); i != e; ++i)
2638 Elements[i] = (*$3)[i]->getType();
2639
2640 const StructType *STy = StructType::get(Elements, /*isPacked=*/true);
2641 PATypeHolder* PTy = new PATypeHolder(HandleUpRefs(STy));
2642
2643 $$ = ValID::create(ConstantStruct::get(STy, *$3));
2644 delete PTy; delete $3;
2645 CHECK_FOR_ERROR
2646 }
2647 | '<' '{' '}' '>' {
2648 const StructType *STy = StructType::get(std::vector<const Type*>(),
2649 /*isPacked=*/true);
2650 $$ = ValID::create(ConstantStruct::get(STy, std::vector<Constant*>()));
2651 CHECK_FOR_ERROR
2652 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002653 | ConstExpr {
2654 $$ = ValID::create($1);
2655 CHECK_FOR_ERROR
2656 }
2657 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
2658 $$ = ValID::createInlineAsm(*$3, *$5, $2);
2659 delete $3;
2660 delete $5;
2661 CHECK_FOR_ERROR
2662 };
2663
2664// SymbolicValueRef - Reference to one of two ways of symbolically refering to
2665// another value.
2666//
2667SymbolicValueRef : LOCALVAL_ID { // Is it an integer reference...?
2668 $$ = ValID::createLocalID($1);
2669 CHECK_FOR_ERROR
2670 }
2671 | GLOBALVAL_ID {
2672 $$ = ValID::createGlobalID($1);
2673 CHECK_FOR_ERROR
2674 }
2675 | LocalName { // Is it a named reference...?
2676 $$ = ValID::createLocalName(*$1);
2677 delete $1;
2678 CHECK_FOR_ERROR
2679 }
2680 | GlobalName { // Is it a named reference...?
2681 $$ = ValID::createGlobalName(*$1);
2682 delete $1;
2683 CHECK_FOR_ERROR
2684 };
2685
2686// ValueRef - A reference to a definition... either constant or symbolic
2687ValueRef : SymbolicValueRef | ConstValueRef;
2688
2689
2690// ResolvedVal - a <type> <value> pair. This is used only in cases where the
2691// type immediately preceeds the value reference, and allows complex constant
2692// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
2693ResolvedVal : Types ValueRef {
2694 if (!UpRefs.empty())
2695 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Eric Christopher329d2672008-09-24 04:55:49 +00002696 $$ = getVal(*$1, $2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002697 delete $1;
2698 CHECK_FOR_ERROR
2699 }
2700 ;
2701
Devang Patelbf507402008-02-20 22:40:23 +00002702ReturnedVal : ResolvedVal {
2703 $$ = new std::vector<Value *>();
Eric Christopher329d2672008-09-24 04:55:49 +00002704 $$->push_back($1);
Devang Patelbf507402008-02-20 22:40:23 +00002705 CHECK_FOR_ERROR
2706 }
Devang Patel087fe2b2008-02-23 00:38:56 +00002707 | ReturnedVal ',' ResolvedVal {
Eric Christopher329d2672008-09-24 04:55:49 +00002708 ($$=$1)->push_back($3);
Devang Patelbf507402008-02-20 22:40:23 +00002709 CHECK_FOR_ERROR
2710 };
2711
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002712BasicBlockList : BasicBlockList BasicBlock {
2713 $$ = $1;
2714 CHECK_FOR_ERROR
2715 }
Eric Christopher329d2672008-09-24 04:55:49 +00002716 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002717 $$ = $1;
2718 CHECK_FOR_ERROR
2719 };
2720
2721
Eric Christopher329d2672008-09-24 04:55:49 +00002722// Basic blocks are terminated by branching instructions:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002723// br, br/cc, switch, ret
2724//
Chris Lattner906773a2008-08-29 17:20:18 +00002725BasicBlock : InstructionList OptLocalAssign BBTerminatorInst {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002726 setValueName($3, $2);
2727 CHECK_FOR_ERROR
2728 InsertValue($3);
2729 $1->getInstList().push_back($3);
2730 $$ = $1;
2731 CHECK_FOR_ERROR
2732 };
2733
Chris Lattner906773a2008-08-29 17:20:18 +00002734BasicBlock : InstructionList LocalNumber BBTerminatorInst {
2735 CHECK_FOR_ERROR
2736 int ValNum = InsertValue($3);
2737 if (ValNum != (int)$2)
2738 GEN_ERROR("Result value number %" + utostr($2) +
2739 " is incorrect, expected %" + utostr((unsigned)ValNum));
Eric Christopher329d2672008-09-24 04:55:49 +00002740
Chris Lattner906773a2008-08-29 17:20:18 +00002741 $1->getInstList().push_back($3);
2742 $$ = $1;
2743 CHECK_FOR_ERROR
2744};
2745
2746
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002747InstructionList : InstructionList Inst {
2748 if (CastInst *CI1 = dyn_cast<CastInst>($2))
2749 if (CastInst *CI2 = dyn_cast<CastInst>(CI1->getOperand(0)))
2750 if (CI2->getParent() == 0)
2751 $1->getInstList().push_back(CI2);
2752 $1->getInstList().push_back($2);
2753 $$ = $1;
2754 CHECK_FOR_ERROR
2755 }
2756 | /* empty */ { // Empty space between instruction lists
Nick Lewyckyd8aa33a2008-04-25 16:53:59 +00002757 $$ = defineBBVal(ValID::createLocalID(CurFun.NextValNum));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002758 CHECK_FOR_ERROR
2759 }
2760 | LABELSTR { // Labelled (named) basic block
Nick Lewyckyd8aa33a2008-04-25 16:53:59 +00002761 $$ = defineBBVal(ValID::createLocalName(*$1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002762 delete $1;
2763 CHECK_FOR_ERROR
Nick Lewyckyd8aa33a2008-04-25 16:53:59 +00002764
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002765 };
2766
Eric Christopher329d2672008-09-24 04:55:49 +00002767BBTerminatorInst :
Devang Patelbf507402008-02-20 22:40:23 +00002768 RET ReturnedVal { // Return with a result...
Devang Patelda2c8d52008-02-26 22:17:48 +00002769 ValueList &VL = *$2;
Devang Patelb4851dc2008-02-26 23:19:08 +00002770 assert(!VL.empty() && "Invalid ret operands!");
Dan Gohmanb94a0ba2008-07-23 00:54:54 +00002771 const Type *ReturnType = CurFun.CurrentFunction->getReturnType();
2772 if (VL.size() > 1 ||
2773 (isa<StructType>(ReturnType) &&
2774 (VL.empty() || VL[0]->getType() != ReturnType))) {
2775 Value *RV = UndefValue::get(ReturnType);
2776 for (unsigned i = 0, e = VL.size(); i != e; ++i) {
2777 Instruction *I = InsertValueInst::Create(RV, VL[i], i, "mrv");
2778 ($<BasicBlockVal>-1)->getInstList().push_back(I);
2779 RV = I;
2780 }
2781 $$ = ReturnInst::Create(RV);
2782 } else {
2783 $$ = ReturnInst::Create(VL[0]);
2784 }
Devang Patelbf507402008-02-20 22:40:23 +00002785 delete $2;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002786 CHECK_FOR_ERROR
2787 }
2788 | RET VOID { // Return with no result...
Gabor Greif89f01162008-04-06 23:07:54 +00002789 $$ = ReturnInst::Create();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002790 CHECK_FOR_ERROR
2791 }
2792 | BR LABEL ValueRef { // Unconditional Branch...
2793 BasicBlock* tmpBB = getBBVal($3);
2794 CHECK_FOR_ERROR
Gabor Greif89f01162008-04-06 23:07:54 +00002795 $$ = BranchInst::Create(tmpBB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002796 } // Conditional Branch...
Eric Christopher329d2672008-09-24 04:55:49 +00002797 | BR INTTYPE ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Dan Gohmane5febe42008-05-31 00:58:22 +00002798 if (cast<IntegerType>($2)->getBitWidth() != 1)
2799 GEN_ERROR("Branch condition must have type i1");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002800 BasicBlock* tmpBBA = getBBVal($6);
2801 CHECK_FOR_ERROR
2802 BasicBlock* tmpBBB = getBBVal($9);
2803 CHECK_FOR_ERROR
2804 Value* tmpVal = getVal(Type::Int1Ty, $3);
2805 CHECK_FOR_ERROR
Gabor Greif89f01162008-04-06 23:07:54 +00002806 $$ = BranchInst::Create(tmpBBA, tmpBBB, tmpVal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002807 }
2808 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
2809 Value* tmpVal = getVal($2, $3);
2810 CHECK_FOR_ERROR
2811 BasicBlock* tmpBB = getBBVal($6);
2812 CHECK_FOR_ERROR
Gabor Greif89f01162008-04-06 23:07:54 +00002813 SwitchInst *S = SwitchInst::Create(tmpVal, tmpBB, $8->size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002814 $$ = S;
2815
2816 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
2817 E = $8->end();
2818 for (; I != E; ++I) {
2819 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
2820 S->addCase(CI, I->second);
2821 else
2822 GEN_ERROR("Switch case is constant, but not a simple integer");
2823 }
2824 delete $8;
2825 CHECK_FOR_ERROR
2826 }
2827 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
2828 Value* tmpVal = getVal($2, $3);
2829 CHECK_FOR_ERROR
2830 BasicBlock* tmpBB = getBBVal($6);
2831 CHECK_FOR_ERROR
Gabor Greif89f01162008-04-06 23:07:54 +00002832 SwitchInst *S = SwitchInst::Create(tmpVal, tmpBB, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002833 $$ = S;
2834 CHECK_FOR_ERROR
2835 }
Devang Patelcd842482008-09-29 20:49:50 +00002836 | INVOKE OptCallingConv OptRetAttrs ResultTypes ValueRef '(' ParamList ')'
2837 OptFuncAttrs TO LABEL ValueRef UNWIND LABEL ValueRef {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002838
2839 // Handle the short syntax
2840 const PointerType *PFTy = 0;
2841 const FunctionType *Ty = 0;
Devang Patelcd842482008-09-29 20:49:50 +00002842 if (!(PFTy = dyn_cast<PointerType>($4->get())) ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002843 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
2844 // Pull out the types of all of the arguments...
2845 std::vector<const Type*> ParamTypes;
Devang Patelcd842482008-09-29 20:49:50 +00002846 ParamList::iterator I = $7->begin(), E = $7->end();
Duncan Sandsf5588dc2007-11-27 13:23:08 +00002847 for (; I != E; ++I) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002848 const Type *Ty = I->Val->getType();
2849 if (Ty == Type::VoidTy)
2850 GEN_ERROR("Short call syntax cannot be used with varargs");
2851 ParamTypes.push_back(Ty);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002852 }
Eric Christopher329d2672008-09-24 04:55:49 +00002853
Devang Patelcd842482008-09-29 20:49:50 +00002854 if (!FunctionType::isValidReturnType(*$4))
Chris Lattner73de3c02008-04-23 05:37:08 +00002855 GEN_ERROR("Invalid result type for LLVM function");
2856
Devang Patelcd842482008-09-29 20:49:50 +00002857 Ty = FunctionType::get($4->get(), ParamTypes, false);
Christopher Lambfb623c62007-12-17 01:17:35 +00002858 PFTy = PointerType::getUnqual(Ty);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002859 }
2860
Devang Patelcd842482008-09-29 20:49:50 +00002861 delete $4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002862
Devang Patelcd842482008-09-29 20:49:50 +00002863 Value *V = getVal(PFTy, $5); // Get the function we're calling...
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002864 CHECK_FOR_ERROR
Devang Patelcd842482008-09-29 20:49:50 +00002865 BasicBlock *Normal = getBBVal($12);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002866 CHECK_FOR_ERROR
Devang Patelcd842482008-09-29 20:49:50 +00002867 BasicBlock *Except = getBBVal($15);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002868 CHECK_FOR_ERROR
2869
Devang Pateld222f862008-09-25 21:00:45 +00002870 SmallVector<AttributeWithIndex, 8> Attrs;
Devang Patelf2a4a922008-09-26 22:53:05 +00002871 //FIXME : In 3.0, stop accepting zext, sext and inreg as optional function
2872 //attributes.
Devang Patelcd842482008-09-29 20:49:50 +00002873 Attributes RetAttrs = $3;
2874 if ($9 != Attribute::None) {
2875 if ($9 & Attribute::ZExt) {
Devang Patelf2a4a922008-09-26 22:53:05 +00002876 RetAttrs = RetAttrs | Attribute::ZExt;
Devang Patelcd842482008-09-29 20:49:50 +00002877 $9 = $9 ^ Attribute::ZExt;
Devang Patelf2a4a922008-09-26 22:53:05 +00002878 }
Devang Patelcd842482008-09-29 20:49:50 +00002879 if ($9 & Attribute::SExt) {
Devang Patelf2a4a922008-09-26 22:53:05 +00002880 RetAttrs = RetAttrs | Attribute::SExt;
Devang Patelcd842482008-09-29 20:49:50 +00002881 $9 = $9 ^ Attribute::SExt;
Devang Patelf2a4a922008-09-26 22:53:05 +00002882 }
Devang Patelcd842482008-09-29 20:49:50 +00002883 if ($9 & Attribute::InReg) {
Devang Patelf2a4a922008-09-26 22:53:05 +00002884 RetAttrs = RetAttrs | Attribute::InReg;
Devang Patelcd842482008-09-29 20:49:50 +00002885 $9 = $9 ^ Attribute::InReg;
Devang Patelf2a4a922008-09-26 22:53:05 +00002886 }
Devang Patelf2a4a922008-09-26 22:53:05 +00002887 }
Devang Patelcd842482008-09-29 20:49:50 +00002888 if (RetAttrs != Attribute::None)
2889 Attrs.push_back(AttributeWithIndex::get(0, RetAttrs));
Devang Patelf2a4a922008-09-26 22:53:05 +00002890
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002891 // Check the arguments
2892 ValueList Args;
Devang Patelcd842482008-09-29 20:49:50 +00002893 if ($7->empty()) { // Has no arguments?
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002894 // Make sure no arguments is a good thing!
2895 if (Ty->getNumParams() != 0)
2896 GEN_ERROR("No arguments passed to a function that "
2897 "expects arguments");
2898 } else { // Has arguments?
2899 // Loop through FunctionType's arguments and ensure they are specified
2900 // correctly!
2901 FunctionType::param_iterator I = Ty->param_begin();
2902 FunctionType::param_iterator E = Ty->param_end();
Devang Patelcd842482008-09-29 20:49:50 +00002903 ParamList::iterator ArgI = $7->begin(), ArgE = $7->end();
Duncan Sandsf5588dc2007-11-27 13:23:08 +00002904 unsigned index = 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002905
Duncan Sandsf5588dc2007-11-27 13:23:08 +00002906 for (; ArgI != ArgE && I != E; ++ArgI, ++I, ++index) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002907 if (ArgI->Val->getType() != *I)
2908 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
2909 (*I)->getDescription() + "'");
2910 Args.push_back(ArgI->Val);
Devang Pateld222f862008-09-25 21:00:45 +00002911 if (ArgI->Attrs != Attribute::None)
2912 Attrs.push_back(AttributeWithIndex::get(index, ArgI->Attrs));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002913 }
2914
2915 if (Ty->isVarArg()) {
2916 if (I == E)
Chris Lattner59363a32008-02-19 04:36:25 +00002917 for (; ArgI != ArgE; ++ArgI, ++index) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002918 Args.push_back(ArgI->Val); // push the remaining varargs
Devang Pateld222f862008-09-25 21:00:45 +00002919 if (ArgI->Attrs != Attribute::None)
2920 Attrs.push_back(AttributeWithIndex::get(index, ArgI->Attrs));
Chris Lattner59363a32008-02-19 04:36:25 +00002921 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002922 } else if (I != E || ArgI != ArgE)
2923 GEN_ERROR("Invalid number of parameters detected");
2924 }
Devang Patelcd842482008-09-29 20:49:50 +00002925 if ($9 != Attribute::None)
2926 Attrs.push_back(AttributeWithIndex::get(~0, $9));
Devang Pateld222f862008-09-25 21:00:45 +00002927 AttrListPtr PAL;
Duncan Sandsf5588dc2007-11-27 13:23:08 +00002928 if (!Attrs.empty())
Devang Pateld222f862008-09-25 21:00:45 +00002929 PAL = AttrListPtr::get(Attrs.begin(), Attrs.end());
Duncan Sandsf5588dc2007-11-27 13:23:08 +00002930
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002931 // Create the InvokeInst
Dan Gohman8055f772008-05-15 19:50:34 +00002932 InvokeInst *II = InvokeInst::Create(V, Normal, Except,
2933 Args.begin(), Args.end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002934 II->setCallingConv($2);
Devang Pateld222f862008-09-25 21:00:45 +00002935 II->setAttributes(PAL);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002936 $$ = II;
Devang Patelcd842482008-09-29 20:49:50 +00002937 delete $7;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002938 CHECK_FOR_ERROR
2939 }
2940 | UNWIND {
2941 $$ = new UnwindInst();
2942 CHECK_FOR_ERROR
2943 }
2944 | UNREACHABLE {
2945 $$ = new UnreachableInst();
2946 CHECK_FOR_ERROR
2947 };
2948
2949
2950
2951JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
2952 $$ = $1;
2953 Constant *V = cast<Constant>(getExistingVal($2, $3));
2954 CHECK_FOR_ERROR
2955 if (V == 0)
2956 GEN_ERROR("May only switch on a constant pool value");
2957
2958 BasicBlock* tmpBB = getBBVal($6);
2959 CHECK_FOR_ERROR
2960 $$->push_back(std::make_pair(V, tmpBB));
2961 }
2962 | IntType ConstValueRef ',' LABEL ValueRef {
2963 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
2964 Constant *V = cast<Constant>(getExistingVal($1, $2));
2965 CHECK_FOR_ERROR
2966
2967 if (V == 0)
2968 GEN_ERROR("May only switch on a constant pool value");
2969
2970 BasicBlock* tmpBB = getBBVal($5);
2971 CHECK_FOR_ERROR
Eric Christopher329d2672008-09-24 04:55:49 +00002972 $$->push_back(std::make_pair(V, tmpBB));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002973 };
2974
2975Inst : OptLocalAssign InstVal {
2976 // Is this definition named?? if so, assign the name...
2977 setValueName($2, $1);
2978 CHECK_FOR_ERROR
2979 InsertValue($2);
2980 $$ = $2;
2981 CHECK_FOR_ERROR
2982 };
2983
Chris Lattner906773a2008-08-29 17:20:18 +00002984Inst : LocalNumber InstVal {
2985 CHECK_FOR_ERROR
2986 int ValNum = InsertValue($2);
Eric Christopher329d2672008-09-24 04:55:49 +00002987
Chris Lattner906773a2008-08-29 17:20:18 +00002988 if (ValNum != (int)$1)
2989 GEN_ERROR("Result value number %" + utostr($1) +
2990 " is incorrect, expected %" + utostr((unsigned)ValNum));
2991
2992 $$ = $2;
2993 CHECK_FOR_ERROR
2994 };
2995
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002996
2997PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
2998 if (!UpRefs.empty())
2999 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
3000 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
3001 Value* tmpVal = getVal(*$1, $3);
3002 CHECK_FOR_ERROR
3003 BasicBlock* tmpBB = getBBVal($5);
3004 CHECK_FOR_ERROR
3005 $$->push_back(std::make_pair(tmpVal, tmpBB));
3006 delete $1;
3007 }
3008 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
3009 $$ = $1;
3010 Value* tmpVal = getVal($1->front().first->getType(), $4);
3011 CHECK_FOR_ERROR
3012 BasicBlock* tmpBB = getBBVal($6);
3013 CHECK_FOR_ERROR
3014 $1->push_back(std::make_pair(tmpVal, tmpBB));
3015 };
3016
3017
Devang Pateld222f862008-09-25 21:00:45 +00003018ParamList : Types OptAttributes ValueRef OptAttributes {
3019 // FIXME: Remove trailing OptAttributes in LLVM 3.0, it was a mistake in 2.0
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003020 if (!UpRefs.empty())
3021 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
3022 // Used for call and invoke instructions
Dale Johannesencfb19e62007-11-05 21:20:28 +00003023 $$ = new ParamList();
Duncan Sandsf5588dc2007-11-27 13:23:08 +00003024 ParamListEntry E; E.Attrs = $2 | $4; E.Val = getVal($1->get(), $3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003025 $$->push_back(E);
3026 delete $1;
Duncan Sandsf5588dc2007-11-27 13:23:08 +00003027 CHECK_FOR_ERROR
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003028 }
Devang Pateld222f862008-09-25 21:00:45 +00003029 | LABEL OptAttributes ValueRef OptAttributes {
3030 // FIXME: Remove trailing OptAttributes in LLVM 3.0, it was a mistake in 2.0
Dale Johannesencfb19e62007-11-05 21:20:28 +00003031 // Labels are only valid in ASMs
3032 $$ = new ParamList();
Duncan Sandsf5588dc2007-11-27 13:23:08 +00003033 ParamListEntry E; E.Attrs = $2 | $4; E.Val = getBBVal($3);
Dale Johannesencfb19e62007-11-05 21:20:28 +00003034 $$->push_back(E);
Duncan Sandsf5588dc2007-11-27 13:23:08 +00003035 CHECK_FOR_ERROR
Dale Johannesencfb19e62007-11-05 21:20:28 +00003036 }
Devang Pateld222f862008-09-25 21:00:45 +00003037 | ParamList ',' Types OptAttributes ValueRef OptAttributes {
3038 // FIXME: Remove trailing OptAttributes in LLVM 3.0, it was a mistake in 2.0
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003039 if (!UpRefs.empty())
3040 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
3041 $$ = $1;
Duncan Sandsf5588dc2007-11-27 13:23:08 +00003042 ParamListEntry E; E.Attrs = $4 | $6; E.Val = getVal($3->get(), $5);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003043 $$->push_back(E);
3044 delete $3;
3045 CHECK_FOR_ERROR
3046 }
Devang Pateld222f862008-09-25 21:00:45 +00003047 | ParamList ',' LABEL OptAttributes ValueRef OptAttributes {
3048 // FIXME: Remove trailing OptAttributes in LLVM 3.0, it was a mistake in 2.0
Dale Johannesencfb19e62007-11-05 21:20:28 +00003049 $$ = $1;
Duncan Sandsf5588dc2007-11-27 13:23:08 +00003050 ParamListEntry E; E.Attrs = $4 | $6; E.Val = getBBVal($5);
Dale Johannesencfb19e62007-11-05 21:20:28 +00003051 $$->push_back(E);
3052 CHECK_FOR_ERROR
3053 }
3054 | /*empty*/ { $$ = new ParamList(); };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003055
3056IndexList // Used for gep instructions and constant expressions
3057 : /*empty*/ { $$ = new std::vector<Value*>(); }
3058 | IndexList ',' ResolvedVal {
3059 $$ = $1;
3060 $$->push_back($3);
3061 CHECK_FOR_ERROR
3062 }
3063 ;
3064
Dan Gohmane5febe42008-05-31 00:58:22 +00003065ConstantIndexList // Used for insertvalue and extractvalue instructions
3066 : ',' EUINT64VAL {
3067 $$ = new std::vector<unsigned>();
3068 if ((unsigned)$2 != $2)
3069 GEN_ERROR("Index " + utostr($2) + " is not valid for insertvalue or extractvalue.");
3070 $$->push_back($2);
3071 }
3072 | ConstantIndexList ',' EUINT64VAL {
3073 $$ = $1;
3074 if ((unsigned)$3 != $3)
3075 GEN_ERROR("Index " + utostr($3) + " is not valid for insertvalue or extractvalue.");
3076 $$->push_back($3);
3077 CHECK_FOR_ERROR
3078 }
3079 ;
3080
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003081OptTailCall : TAIL CALL {
3082 $$ = true;
3083 CHECK_FOR_ERROR
3084 }
3085 | CALL {
3086 $$ = false;
3087 CHECK_FOR_ERROR
3088 };
3089
3090InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
3091 if (!UpRefs.empty())
3092 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Eric Christopher329d2672008-09-24 04:55:49 +00003093 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003094 !isa<VectorType>((*$2).get()))
3095 GEN_ERROR(
3096 "Arithmetic operator requires integer, FP, or packed operands");
Eric Christopher329d2672008-09-24 04:55:49 +00003097 Value* val1 = getVal(*$2, $3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003098 CHECK_FOR_ERROR
3099 Value* val2 = getVal(*$2, $5);
3100 CHECK_FOR_ERROR
Dan Gohmane6b1ee62008-05-23 01:55:30 +00003101 $$ = BinaryOperator::Create($1, val1, val2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003102 if ($$ == 0)
3103 GEN_ERROR("binary operator returned null");
3104 delete $2;
3105 }
3106 | LogicalOps Types ValueRef ',' ValueRef {
3107 if (!UpRefs.empty())
3108 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
3109 if (!(*$2)->isInteger()) {
Nate Begemanbb1ce942008-07-29 15:49:41 +00003110 if (!isa<VectorType>($2->get()) ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003111 !cast<VectorType>($2->get())->getElementType()->isInteger())
3112 GEN_ERROR("Logical operator requires integral operands");
3113 }
3114 Value* tmpVal1 = getVal(*$2, $3);
3115 CHECK_FOR_ERROR
3116 Value* tmpVal2 = getVal(*$2, $5);
3117 CHECK_FOR_ERROR
Dan Gohmane6b1ee62008-05-23 01:55:30 +00003118 $$ = BinaryOperator::Create($1, tmpVal1, tmpVal2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003119 if ($$ == 0)
3120 GEN_ERROR("binary operator returned null");
3121 delete $2;
3122 }
3123 | ICMP IPredicates Types ValueRef ',' ValueRef {
3124 if (!UpRefs.empty())
3125 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003126 Value* tmpVal1 = getVal(*$3, $4);
3127 CHECK_FOR_ERROR
3128 Value* tmpVal2 = getVal(*$3, $6);
3129 CHECK_FOR_ERROR
Dan Gohmane6b1ee62008-05-23 01:55:30 +00003130 $$ = CmpInst::Create($1, $2, tmpVal1, tmpVal2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003131 if ($$ == 0)
3132 GEN_ERROR("icmp operator returned null");
3133 delete $3;
3134 }
3135 | FCMP FPredicates Types ValueRef ',' ValueRef {
3136 if (!UpRefs.empty())
3137 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003138 Value* tmpVal1 = getVal(*$3, $4);
3139 CHECK_FOR_ERROR
3140 Value* tmpVal2 = getVal(*$3, $6);
3141 CHECK_FOR_ERROR
Dan Gohmane6b1ee62008-05-23 01:55:30 +00003142 $$ = CmpInst::Create($1, $2, tmpVal1, tmpVal2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003143 if ($$ == 0)
3144 GEN_ERROR("fcmp operator returned null");
3145 delete $3;
3146 }
Nate Begeman646fa482008-05-12 19:01:56 +00003147 | VICMP IPredicates Types ValueRef ',' ValueRef {
3148 if (!UpRefs.empty())
3149 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
3150 if (!isa<VectorType>((*$3).get()))
3151 GEN_ERROR("Scalar types not supported by vicmp instruction");
3152 Value* tmpVal1 = getVal(*$3, $4);
3153 CHECK_FOR_ERROR
3154 Value* tmpVal2 = getVal(*$3, $6);
3155 CHECK_FOR_ERROR
Dan Gohmane6b1ee62008-05-23 01:55:30 +00003156 $$ = CmpInst::Create($1, $2, tmpVal1, tmpVal2);
Nate Begeman646fa482008-05-12 19:01:56 +00003157 if ($$ == 0)
Dan Gohman181f4e42008-09-09 01:13:24 +00003158 GEN_ERROR("vicmp operator returned null");
Nate Begeman646fa482008-05-12 19:01:56 +00003159 delete $3;
3160 }
3161 | VFCMP FPredicates Types ValueRef ',' ValueRef {
3162 if (!UpRefs.empty())
3163 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
3164 if (!isa<VectorType>((*$3).get()))
3165 GEN_ERROR("Scalar types not supported by vfcmp instruction");
3166 Value* tmpVal1 = getVal(*$3, $4);
3167 CHECK_FOR_ERROR
3168 Value* tmpVal2 = getVal(*$3, $6);
3169 CHECK_FOR_ERROR
Dan Gohmane6b1ee62008-05-23 01:55:30 +00003170 $$ = CmpInst::Create($1, $2, tmpVal1, tmpVal2);
Nate Begeman646fa482008-05-12 19:01:56 +00003171 if ($$ == 0)
Dan Gohman181f4e42008-09-09 01:13:24 +00003172 GEN_ERROR("vfcmp operator returned null");
Nate Begeman646fa482008-05-12 19:01:56 +00003173 delete $3;
3174 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003175 | CastOps ResolvedVal TO Types {
3176 if (!UpRefs.empty())
3177 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
3178 Value* Val = $2;
3179 const Type* DestTy = $4->get();
3180 if (!CastInst::castIsValid($1, Val, DestTy))
3181 GEN_ERROR("invalid cast opcode for cast from '" +
3182 Val->getType()->getDescription() + "' to '" +
Eric Christopher329d2672008-09-24 04:55:49 +00003183 DestTy->getDescription() + "'");
Dan Gohmane6b1ee62008-05-23 01:55:30 +00003184 $$ = CastInst::Create($1, Val, DestTy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003185 delete $4;
3186 }
3187 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Dan Gohman181f4e42008-09-09 01:13:24 +00003188 if (isa<VectorType>($2->getType())) {
3189 // vector select
3190 if (!isa<VectorType>($4->getType())
3191 || !isa<VectorType>($6->getType()) )
3192 GEN_ERROR("vector select value types must be vector types");
3193 const VectorType* cond_type = cast<VectorType>($2->getType());
3194 const VectorType* select_type = cast<VectorType>($4->getType());
3195 if (cond_type->getElementType() != Type::Int1Ty)
3196 GEN_ERROR("vector select condition element type must be boolean");
3197 if (cond_type->getNumElements() != select_type->getNumElements())
3198 GEN_ERROR("vector select number of elements must be the same");
3199 } else {
3200 if ($2->getType() != Type::Int1Ty)
3201 GEN_ERROR("select condition must be boolean");
3202 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003203 if ($4->getType() != $6->getType())
Dan Gohman181f4e42008-09-09 01:13:24 +00003204 GEN_ERROR("select value types must match");
Gabor Greif89f01162008-04-06 23:07:54 +00003205 $$ = SelectInst::Create($2, $4, $6);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003206 CHECK_FOR_ERROR
3207 }
3208 | VAARG ResolvedVal ',' Types {
3209 if (!UpRefs.empty())
3210 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
3211 $$ = new VAArgInst($2, *$4);
3212 delete $4;
3213 CHECK_FOR_ERROR
3214 }
3215 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
3216 if (!ExtractElementInst::isValidOperands($2, $4))
3217 GEN_ERROR("Invalid extractelement operands");
3218 $$ = new ExtractElementInst($2, $4);
3219 CHECK_FOR_ERROR
3220 }
3221 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
3222 if (!InsertElementInst::isValidOperands($2, $4, $6))
3223 GEN_ERROR("Invalid insertelement operands");
Gabor Greif89f01162008-04-06 23:07:54 +00003224 $$ = InsertElementInst::Create($2, $4, $6);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003225 CHECK_FOR_ERROR
3226 }
3227 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
3228 if (!ShuffleVectorInst::isValidOperands($2, $4, $6))
3229 GEN_ERROR("Invalid shufflevector operands");
3230 $$ = new ShuffleVectorInst($2, $4, $6);
3231 CHECK_FOR_ERROR
3232 }
3233 | PHI_TOK PHIList {
3234 const Type *Ty = $2->front().first->getType();
3235 if (!Ty->isFirstClassType())
3236 GEN_ERROR("PHI node operands must be of first class type");
Gabor Greif89f01162008-04-06 23:07:54 +00003237 $$ = PHINode::Create(Ty);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003238 ((PHINode*)$$)->reserveOperandSpace($2->size());
3239 while ($2->begin() != $2->end()) {
Eric Christopher329d2672008-09-24 04:55:49 +00003240 if ($2->front().first->getType() != Ty)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003241 GEN_ERROR("All elements of a PHI node must be of the same type");
3242 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
3243 $2->pop_front();
3244 }
3245 delete $2; // Free the list...
3246 CHECK_FOR_ERROR
3247 }
Devang Patelcd842482008-09-29 20:49:50 +00003248 | OptTailCall OptCallingConv OptRetAttrs ResultTypes ValueRef '(' ParamList ')'
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003249 OptFuncAttrs {
3250
3251 // Handle the short syntax
3252 const PointerType *PFTy = 0;
3253 const FunctionType *Ty = 0;
Devang Patelcd842482008-09-29 20:49:50 +00003254 if (!(PFTy = dyn_cast<PointerType>($4->get())) ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003255 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
3256 // Pull out the types of all of the arguments...
3257 std::vector<const Type*> ParamTypes;
Devang Patelcd842482008-09-29 20:49:50 +00003258 ParamList::iterator I = $7->begin(), E = $7->end();
Duncan Sandsf5588dc2007-11-27 13:23:08 +00003259 for (; I != E; ++I) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003260 const Type *Ty = I->Val->getType();
3261 if (Ty == Type::VoidTy)
3262 GEN_ERROR("Short call syntax cannot be used with varargs");
3263 ParamTypes.push_back(Ty);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003264 }
Chris Lattner73de3c02008-04-23 05:37:08 +00003265
Devang Patelcd842482008-09-29 20:49:50 +00003266 if (!FunctionType::isValidReturnType(*$4))
Chris Lattner73de3c02008-04-23 05:37:08 +00003267 GEN_ERROR("Invalid result type for LLVM function");
3268
Devang Patelcd842482008-09-29 20:49:50 +00003269 Ty = FunctionType::get($4->get(), ParamTypes, false);
Christopher Lambfb623c62007-12-17 01:17:35 +00003270 PFTy = PointerType::getUnqual(Ty);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003271 }
3272
Devang Patelcd842482008-09-29 20:49:50 +00003273 Value *V = getVal(PFTy, $5); // Get the function we're calling...
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003274 CHECK_FOR_ERROR
3275
3276 // Check for call to invalid intrinsic to avoid crashing later.
3277 if (Function *theF = dyn_cast<Function>(V)) {
3278 if (theF->hasName() && (theF->getValueName()->getKeyLength() >= 5) &&
3279 (0 == strncmp(theF->getValueName()->getKeyData(), "llvm.", 5)) &&
3280 !theF->getIntrinsicID(true))
3281 GEN_ERROR("Call to invalid LLVM intrinsic function '" +
3282 theF->getName() + "'");
3283 }
3284
Devang Pateld222f862008-09-25 21:00:45 +00003285 // Set up the Attributes for the function
3286 SmallVector<AttributeWithIndex, 8> Attrs;
Devang Patelf2a4a922008-09-26 22:53:05 +00003287 //FIXME : In 3.0, stop accepting zext, sext and inreg as optional function
3288 //attributes.
Devang Patelcd842482008-09-29 20:49:50 +00003289 Attributes RetAttrs = $3;
3290 if ($9 != Attribute::None) {
3291 if ($9 & Attribute::ZExt) {
Devang Patelf2a4a922008-09-26 22:53:05 +00003292 RetAttrs = RetAttrs | Attribute::ZExt;
Devang Patelcd842482008-09-29 20:49:50 +00003293 $9 = $9 ^ Attribute::ZExt;
Devang Patelf2a4a922008-09-26 22:53:05 +00003294 }
Devang Patelcd842482008-09-29 20:49:50 +00003295 if ($9 & Attribute::SExt) {
Devang Patelf2a4a922008-09-26 22:53:05 +00003296 RetAttrs = RetAttrs | Attribute::SExt;
Devang Patelcd842482008-09-29 20:49:50 +00003297 $9 = $9 ^ Attribute::SExt;
Devang Patelf2a4a922008-09-26 22:53:05 +00003298 }
Devang Patelcd842482008-09-29 20:49:50 +00003299 if ($9 & Attribute::InReg) {
Devang Patelf2a4a922008-09-26 22:53:05 +00003300 RetAttrs = RetAttrs | Attribute::InReg;
Devang Patelcd842482008-09-29 20:49:50 +00003301 $9 = $9 ^ Attribute::InReg;
Devang Patelf2a4a922008-09-26 22:53:05 +00003302 }
Devang Patelf2a4a922008-09-26 22:53:05 +00003303 }
Devang Patelcd842482008-09-29 20:49:50 +00003304 if (RetAttrs != Attribute::None)
3305 Attrs.push_back(AttributeWithIndex::get(0, RetAttrs));
Devang Patelf2a4a922008-09-26 22:53:05 +00003306
Eric Christopher329d2672008-09-24 04:55:49 +00003307 // Check the arguments
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003308 ValueList Args;
Devang Patelcd842482008-09-29 20:49:50 +00003309 if ($7->empty()) { // Has no arguments?
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003310 // Make sure no arguments is a good thing!
3311 if (Ty->getNumParams() != 0)
3312 GEN_ERROR("No arguments passed to a function that "
3313 "expects arguments");
3314 } else { // Has arguments?
3315 // Loop through FunctionType's arguments and ensure they are specified
Duncan Sandsf5588dc2007-11-27 13:23:08 +00003316 // correctly. Also, gather any parameter attributes.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003317 FunctionType::param_iterator I = Ty->param_begin();
3318 FunctionType::param_iterator E = Ty->param_end();
Devang Patelcd842482008-09-29 20:49:50 +00003319 ParamList::iterator ArgI = $7->begin(), ArgE = $7->end();
Duncan Sandsf5588dc2007-11-27 13:23:08 +00003320 unsigned index = 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003321
Duncan Sandsf5588dc2007-11-27 13:23:08 +00003322 for (; ArgI != ArgE && I != E; ++ArgI, ++I, ++index) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003323 if (ArgI->Val->getType() != *I)
3324 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
3325 (*I)->getDescription() + "'");
3326 Args.push_back(ArgI->Val);
Devang Pateld222f862008-09-25 21:00:45 +00003327 if (ArgI->Attrs != Attribute::None)
3328 Attrs.push_back(AttributeWithIndex::get(index, ArgI->Attrs));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003329 }
3330 if (Ty->isVarArg()) {
3331 if (I == E)
Chris Lattner59363a32008-02-19 04:36:25 +00003332 for (; ArgI != ArgE; ++ArgI, ++index) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003333 Args.push_back(ArgI->Val); // push the remaining varargs
Devang Pateld222f862008-09-25 21:00:45 +00003334 if (ArgI->Attrs != Attribute::None)
3335 Attrs.push_back(AttributeWithIndex::get(index, ArgI->Attrs));
Chris Lattner59363a32008-02-19 04:36:25 +00003336 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003337 } else if (I != E || ArgI != ArgE)
3338 GEN_ERROR("Invalid number of parameters detected");
3339 }
Devang Patelcd842482008-09-29 20:49:50 +00003340 if ($9 != Attribute::None)
3341 Attrs.push_back(AttributeWithIndex::get(~0, $9));
Duncan Sandsf5588dc2007-11-27 13:23:08 +00003342
Devang Pateld222f862008-09-25 21:00:45 +00003343 // Finish off the Attributes and check them
3344 AttrListPtr PAL;
Duncan Sandsf5588dc2007-11-27 13:23:08 +00003345 if (!Attrs.empty())
Devang Pateld222f862008-09-25 21:00:45 +00003346 PAL = AttrListPtr::get(Attrs.begin(), Attrs.end());
Duncan Sandsf5588dc2007-11-27 13:23:08 +00003347
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003348 // Create the call node
Gabor Greif89f01162008-04-06 23:07:54 +00003349 CallInst *CI = CallInst::Create(V, Args.begin(), Args.end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003350 CI->setTailCall($1);
3351 CI->setCallingConv($2);
Devang Pateld222f862008-09-25 21:00:45 +00003352 CI->setAttributes(PAL);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003353 $$ = CI;
Devang Patelcd842482008-09-29 20:49:50 +00003354 delete $7;
3355 delete $4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003356 CHECK_FOR_ERROR
3357 }
3358 | MemoryInst {
3359 $$ = $1;
3360 CHECK_FOR_ERROR
3361 };
3362
3363OptVolatile : VOLATILE {
3364 $$ = true;
3365 CHECK_FOR_ERROR
3366 }
3367 | /* empty */ {
3368 $$ = false;
3369 CHECK_FOR_ERROR
3370 };
3371
3372
3373
3374MemoryInst : MALLOC Types OptCAlign {
3375 if (!UpRefs.empty())
3376 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
3377 $$ = new MallocInst(*$2, 0, $3);
3378 delete $2;
3379 CHECK_FOR_ERROR
3380 }
3381 | MALLOC Types ',' INTTYPE ValueRef OptCAlign {
3382 if (!UpRefs.empty())
3383 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Dan Gohmane5febe42008-05-31 00:58:22 +00003384 if ($4 != Type::Int32Ty)
3385 GEN_ERROR("Malloc array size is not a 32-bit integer!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003386 Value* tmpVal = getVal($4, $5);
3387 CHECK_FOR_ERROR
3388 $$ = new MallocInst(*$2, tmpVal, $6);
3389 delete $2;
3390 }
3391 | ALLOCA Types OptCAlign {
3392 if (!UpRefs.empty())
3393 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
3394 $$ = new AllocaInst(*$2, 0, $3);
3395 delete $2;
3396 CHECK_FOR_ERROR
3397 }
3398 | ALLOCA Types ',' INTTYPE ValueRef OptCAlign {
3399 if (!UpRefs.empty())
3400 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Dan Gohmane5febe42008-05-31 00:58:22 +00003401 if ($4 != Type::Int32Ty)
3402 GEN_ERROR("Alloca array size is not a 32-bit integer!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003403 Value* tmpVal = getVal($4, $5);
3404 CHECK_FOR_ERROR
3405 $$ = new AllocaInst(*$2, tmpVal, $6);
3406 delete $2;
3407 }
3408 | FREE ResolvedVal {
3409 if (!isa<PointerType>($2->getType()))
Eric Christopher329d2672008-09-24 04:55:49 +00003410 GEN_ERROR("Trying to free nonpointer type " +
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003411 $2->getType()->getDescription() + "");
3412 $$ = new FreeInst($2);
3413 CHECK_FOR_ERROR
3414 }
3415
3416 | OptVolatile LOAD Types ValueRef OptCAlign {
3417 if (!UpRefs.empty())
3418 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
3419 if (!isa<PointerType>($3->get()))
3420 GEN_ERROR("Can't load from nonpointer type: " +
3421 (*$3)->getDescription());
3422 if (!cast<PointerType>($3->get())->getElementType()->isFirstClassType())
3423 GEN_ERROR("Can't load from pointer of non-first-class type: " +
3424 (*$3)->getDescription());
3425 Value* tmpVal = getVal(*$3, $4);
3426 CHECK_FOR_ERROR
3427 $$ = new LoadInst(tmpVal, "", $1, $5);
3428 delete $3;
3429 }
3430 | OptVolatile STORE ResolvedVal ',' Types ValueRef OptCAlign {
3431 if (!UpRefs.empty())
3432 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
3433 const PointerType *PT = dyn_cast<PointerType>($5->get());
3434 if (!PT)
3435 GEN_ERROR("Can't store to a nonpointer type: " +
3436 (*$5)->getDescription());
3437 const Type *ElTy = PT->getElementType();
3438 if (ElTy != $3->getType())
3439 GEN_ERROR("Can't store '" + $3->getType()->getDescription() +
3440 "' into space of type '" + ElTy->getDescription() + "'");
3441
3442 Value* tmpVal = getVal(*$5, $6);
3443 CHECK_FOR_ERROR
3444 $$ = new StoreInst($3, tmpVal, $1, $7);
3445 delete $5;
3446 }
Dan Gohmane6b1ee62008-05-23 01:55:30 +00003447 | GETRESULT Types ValueRef ',' EUINT64VAL {
Dan Gohmanb94a0ba2008-07-23 00:54:54 +00003448 if (!UpRefs.empty())
3449 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
3450 if (!isa<StructType>($2->get()) && !isa<ArrayType>($2->get()))
3451 GEN_ERROR("getresult insn requires an aggregate operand");
3452 if (!ExtractValueInst::getIndexedType(*$2, $5))
3453 GEN_ERROR("Invalid getresult index for type '" +
3454 (*$2)->getDescription()+ "'");
3455
3456 Value *tmpVal = getVal(*$2, $3);
Devang Patel3b8849c2008-02-19 22:27:01 +00003457 CHECK_FOR_ERROR
Dan Gohmanb94a0ba2008-07-23 00:54:54 +00003458 $$ = ExtractValueInst::Create(tmpVal, $5);
3459 delete $2;
Devang Patel3b8849c2008-02-19 22:27:01 +00003460 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003461 | GETELEMENTPTR Types ValueRef IndexList {
3462 if (!UpRefs.empty())
3463 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
3464 if (!isa<PointerType>($2->get()))
3465 GEN_ERROR("getelementptr insn requires pointer operand");
3466
Dan Gohman8055f772008-05-15 19:50:34 +00003467 if (!GetElementPtrInst::getIndexedType(*$2, $4->begin(), $4->end()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003468 GEN_ERROR("Invalid getelementptr indices for type '" +
3469 (*$2)->getDescription()+ "'");
3470 Value* tmpVal = getVal(*$2, $3);
3471 CHECK_FOR_ERROR
Gabor Greif89f01162008-04-06 23:07:54 +00003472 $$ = GetElementPtrInst::Create(tmpVal, $4->begin(), $4->end());
Eric Christopher329d2672008-09-24 04:55:49 +00003473 delete $2;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003474 delete $4;
Dan Gohmane6b1ee62008-05-23 01:55:30 +00003475 }
Dan Gohmane5febe42008-05-31 00:58:22 +00003476 | EXTRACTVALUE Types ValueRef ConstantIndexList {
Dan Gohmane6b1ee62008-05-23 01:55:30 +00003477 if (!UpRefs.empty())
3478 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
3479 if (!isa<StructType>($2->get()) && !isa<ArrayType>($2->get()))
3480 GEN_ERROR("extractvalue insn requires an aggregate operand");
3481
3482 if (!ExtractValueInst::getIndexedType(*$2, $4->begin(), $4->end()))
3483 GEN_ERROR("Invalid extractvalue indices for type '" +
3484 (*$2)->getDescription()+ "'");
3485 Value* tmpVal = getVal(*$2, $3);
3486 CHECK_FOR_ERROR
3487 $$ = ExtractValueInst::Create(tmpVal, $4->begin(), $4->end());
Eric Christopher329d2672008-09-24 04:55:49 +00003488 delete $2;
Dan Gohmane6b1ee62008-05-23 01:55:30 +00003489 delete $4;
3490 }
Dan Gohmane5febe42008-05-31 00:58:22 +00003491 | INSERTVALUE Types ValueRef ',' Types ValueRef ConstantIndexList {
Dan Gohmane6b1ee62008-05-23 01:55:30 +00003492 if (!UpRefs.empty())
3493 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
3494 if (!isa<StructType>($2->get()) && !isa<ArrayType>($2->get()))
3495 GEN_ERROR("extractvalue insn requires an aggregate operand");
3496
3497 if (ExtractValueInst::getIndexedType(*$2, $7->begin(), $7->end()) != $5->get())
3498 GEN_ERROR("Invalid insertvalue indices for type '" +
3499 (*$2)->getDescription()+ "'");
3500 Value* aggVal = getVal(*$2, $3);
3501 Value* tmpVal = getVal(*$5, $6);
3502 CHECK_FOR_ERROR
3503 $$ = InsertValueInst::Create(aggVal, tmpVal, $7->begin(), $7->end());
Eric Christopher329d2672008-09-24 04:55:49 +00003504 delete $2;
Dan Gohmane6b1ee62008-05-23 01:55:30 +00003505 delete $5;
3506 delete $7;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003507 };
3508
3509
3510%%
3511
3512// common code from the two 'RunVMAsmParser' functions
3513static Module* RunParser(Module * M) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003514 CurModule.CurrentModule = M;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003515 // Check to make sure the parser succeeded
3516 if (yyparse()) {
3517 if (ParserResult)
3518 delete ParserResult;
3519 return 0;
3520 }
3521
3522 // Emit an error if there are any unresolved types left.
3523 if (!CurModule.LateResolveTypes.empty()) {
3524 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
3525 if (DID.Type == ValID::LocalName) {
3526 GenerateError("Undefined type remains at eof: '"+DID.getName() + "'");
3527 } else {
3528 GenerateError("Undefined type remains at eof: #" + itostr(DID.Num));
3529 }
3530 if (ParserResult)
3531 delete ParserResult;
3532 return 0;
3533 }
3534
3535 // Emit an error if there are any unresolved values left.
3536 if (!CurModule.LateResolveValues.empty()) {
3537 Value *V = CurModule.LateResolveValues.back();
3538 std::map<Value*, std::pair<ValID, int> >::iterator I =
3539 CurModule.PlaceHolderInfo.find(V);
3540
3541 if (I != CurModule.PlaceHolderInfo.end()) {
3542 ValID &DID = I->second.first;
3543 if (DID.Type == ValID::LocalName) {
3544 GenerateError("Undefined value remains at eof: "+DID.getName() + "'");
3545 } else {
3546 GenerateError("Undefined value remains at eof: #" + itostr(DID.Num));
3547 }
3548 if (ParserResult)
3549 delete ParserResult;
3550 return 0;
3551 }
3552 }
3553
3554 // Check to make sure that parsing produced a result
3555 if (!ParserResult)
3556 return 0;
3557
3558 // Reset ParserResult variable while saving its value for the result.
3559 Module *Result = ParserResult;
3560 ParserResult = 0;
3561
3562 return Result;
3563}
3564
3565void llvm::GenerateError(const std::string &message, int LineNo) {
Duncan Sandsf5588dc2007-11-27 13:23:08 +00003566 if (LineNo == -1) LineNo = LLLgetLineNo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003567 // TODO: column number in exception
3568 if (TheParseError)
Duncan Sandsf5588dc2007-11-27 13:23:08 +00003569 TheParseError->setError(LLLgetFilename(), message, LineNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003570 TriggerError = 1;
3571}
3572
3573int yyerror(const char *ErrorMsg) {
Duncan Sandsf5588dc2007-11-27 13:23:08 +00003574 std::string where = LLLgetFilename() + ":" + utostr(LLLgetLineNo()) + ": ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003575 std::string errMsg = where + "error: " + std::string(ErrorMsg);
Duncan Sandsf5588dc2007-11-27 13:23:08 +00003576 if (yychar != YYEMPTY && yychar != 0) {
3577 errMsg += " while reading token: '";
Eric Christopher329d2672008-09-24 04:55:49 +00003578 errMsg += std::string(LLLgetTokenStart(),
Duncan Sandsf5588dc2007-11-27 13:23:08 +00003579 LLLgetTokenStart()+LLLgetTokenLength()) + "'";
3580 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003581 GenerateError(errMsg);
3582 return 0;
3583}