1 //===-- llvmAsmParser.y - Parser for llvm assembly files --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the bison parser for LLVM assembly languages files.
12 //===----------------------------------------------------------------------===//
15 #include "ParserInternals.h"
16 #include "llvm/SymbolTable.h"
17 #include "llvm/Module.h"
18 #include "llvm/iTerminators.h"
19 #include "llvm/iMemory.h"
20 #include "llvm/iOperators.h"
21 #include "llvm/iPHINode.h"
22 #include "llvm/Support/GetElementPtrTypeIterator.h"
23 #include "Support/STLExtras.h"
29 int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
30 int yylex(); // declaration" of xxx warnings.
35 static Module *ParserResult;
36 std::string CurFilename;
38 // DEBUG_UPREFS - Define this symbol if you want to enable debugging output
39 // relating to upreferences in the input stream.
41 //#define DEBUG_UPREFS 1
43 #define UR_OUT(X) std::cerr << X
48 #define YYERROR_VERBOSE 1
50 // HACK ALERT: This variable is used to implement the automatic conversion of
51 // variable argument instructions from their old to new forms. When this
52 // compatiblity "Feature" is removed, this should be too.
54 static BasicBlock *CurBB;
55 static bool ObsoleteVarArgs;
58 // This contains info used when building the body of a function. It is
59 // destroyed when the function is completed.
61 typedef std::vector<Value *> ValueList; // Numbered defs
62 static void ResolveDefinitions(std::map<const Type *,ValueList> &LateResolvers,
63 std::map<const Type *,ValueList> *FutureLateResolvers = 0);
65 static struct PerModuleInfo {
66 Module *CurrentModule;
67 std::map<const Type *, ValueList> Values; // Module level numbered definitions
68 std::map<const Type *,ValueList> LateResolveValues;
69 std::vector<PATypeHolder> Types;
70 std::map<ValID, PATypeHolder> LateResolveTypes;
72 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
73 // references to global values. Global values may be referenced before they
74 // are defined, and if so, the temporary object that they represent is held
75 // here. This is used for forward references of ConstantPointerRefs.
77 typedef std::map<std::pair<const PointerType *,
78 ValID>, GlobalValue*> GlobalRefsType;
79 GlobalRefsType GlobalRefs;
82 // If we could not resolve some functions at function compilation time
83 // (calls to functions before they are defined), resolve them now... Types
84 // are resolved when the constant pool has been completely parsed.
86 ResolveDefinitions(LateResolveValues);
88 // Check to make sure that all global value forward references have been
91 if (!GlobalRefs.empty()) {
92 std::string UndefinedReferences = "Unresolved global references exist:\n";
94 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
96 UndefinedReferences += " " + I->first.first->getDescription() + " " +
97 I->first.second.getName() + "\n";
99 ThrowException(UndefinedReferences);
102 Values.clear(); // Clear out function local definitions
108 // DeclareNewGlobalValue - Called every time a new GV has been defined. This
109 // is used to remove things from the forward declaration map, resolving them
110 // to the correct thing as needed.
112 void DeclareNewGlobalValue(GlobalValue *GV, ValID D) {
113 // Check to see if there is a forward reference to this global variable...
114 // if there is, eliminate it and patch the reference to use the new def'n.
115 GlobalRefsType::iterator I =
116 GlobalRefs.find(std::make_pair(GV->getType(), D));
118 if (I != GlobalRefs.end()) {
119 GlobalValue *OldGV = I->second; // Get the placeholder...
120 I->first.second.destroy(); // Free string memory if necessary
122 // Replace all uses of the placeholder with the new GV
123 OldGV->replaceAllUsesWith(GV);
125 // Remove OldGV from the module...
126 if (GlobalVariable *GVar = dyn_cast<GlobalVariable>(OldGV))
127 CurrentModule->getGlobalList().erase(GVar);
129 CurrentModule->getFunctionList().erase(cast<Function>(OldGV));
131 // Remove the map entry for the global now that it has been created...
138 static struct PerFunctionInfo {
139 Function *CurrentFunction; // Pointer to current function being created
141 std::map<const Type*, ValueList> Values; // Keep track of #'d definitions
142 std::map<const Type*, ValueList> LateResolveValues;
143 std::vector<PATypeHolder> Types;
144 std::map<ValID, PATypeHolder> LateResolveTypes;
145 bool isDeclare; // Is this function a forward declararation?
147 inline PerFunctionInfo() {
152 inline void FunctionStart(Function *M) {
156 void FunctionDone() {
157 // If we could not resolve some blocks at parsing time (forward branches)
158 // resolve the branches now...
159 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
161 // Make sure to resolve any constant expr references that might exist within
162 // the function we just declared itself.
164 if (CurrentFunction->hasName()) {
165 FID = ValID::create((char*)CurrentFunction->getName().c_str());
167 // Figure out which slot number if is...
168 ValueList &List = CurModule.Values[CurrentFunction->getType()];
169 for (unsigned i = 0; ; ++i) {
170 assert(i < List.size() && "Function not found!");
171 if (List[i] == CurrentFunction) {
172 FID = ValID::create((int)i);
177 CurModule.DeclareNewGlobalValue(CurrentFunction, FID);
179 Values.clear(); // Clear out function local definitions
180 Types.clear(); // Clear out function local types
184 } CurFun; // Info for the current function...
186 static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
189 //===----------------------------------------------------------------------===//
190 // Code to handle definitions of all the types
191 //===----------------------------------------------------------------------===//
193 static int InsertValue(Value *V,
194 std::map<const Type*,ValueList> &ValueTab = CurFun.Values) {
195 if (V->hasName()) return -1; // Is this a numbered definition?
197 // Yes, insert the value into the value table...
198 ValueList &List = ValueTab[V->getType()];
200 return List.size()-1;
203 // TODO: FIXME when Type are not const
204 static void InsertType(const Type *Ty, std::vector<PATypeHolder> &Types) {
208 static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
210 case ValID::NumberVal: { // Is it a numbered definition?
211 unsigned Num = (unsigned)D.Num;
213 // Module constants occupy the lowest numbered slots...
214 if (Num < CurModule.Types.size())
215 return CurModule.Types[Num];
217 Num -= CurModule.Types.size();
219 // Check that the number is within bounds...
220 if (Num <= CurFun.Types.size())
221 return CurFun.Types[Num];
224 case ValID::NameVal: { // Is it a named definition?
225 std::string Name(D.Name);
226 SymbolTable *SymTab = 0;
228 if (inFunctionScope()) {
229 SymTab = &CurFun.CurrentFunction->getSymbolTable();
230 N = SymTab->lookupType(Name);
234 // Symbol table doesn't automatically chain yet... because the function
235 // hasn't been added to the module...
237 SymTab = &CurModule.CurrentModule->getSymbolTable();
238 N = SymTab->lookupType(Name);
242 D.destroy(); // Free old strdup'd memory...
243 return cast<Type>(N);
246 ThrowException("Internal parser error: Invalid symbol type reference!");
249 // If we reached here, we referenced either a symbol that we don't know about
250 // or an id number that hasn't been read yet. We may be referencing something
251 // forward, so just create an entry to be resolved later and get to it...
253 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
255 std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
256 CurFun.LateResolveTypes : CurModule.LateResolveTypes;
258 std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
259 if (I != LateResolver.end()) {
263 Type *Typ = OpaqueType::get();
264 LateResolver.insert(std::make_pair(D, Typ));
268 static Value *lookupInSymbolTable(const Type *Ty, const std::string &Name) {
269 SymbolTable &SymTab =
270 inFunctionScope() ? CurFun.CurrentFunction->getSymbolTable() :
271 CurModule.CurrentModule->getSymbolTable();
272 return SymTab.lookup(Ty, Name);
275 // getValNonImprovising - Look up the value specified by the provided type and
276 // the provided ValID. If the value exists and has already been defined, return
277 // it. Otherwise return null.
279 static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
280 if (isa<FunctionType>(Ty))
281 ThrowException("Functions are not values and "
282 "must be referenced as pointers");
285 case ValID::NumberVal: { // Is it a numbered definition?
286 unsigned Num = (unsigned)D.Num;
288 // Module constants occupy the lowest numbered slots...
289 std::map<const Type*,ValueList>::iterator VI = CurModule.Values.find(Ty);
290 if (VI != CurModule.Values.end()) {
291 if (Num < VI->second.size())
292 return VI->second[Num];
293 Num -= VI->second.size();
296 // Make sure that our type is within bounds
297 VI = CurFun.Values.find(Ty);
298 if (VI == CurFun.Values.end()) return 0;
300 // Check that the number is within bounds...
301 if (VI->second.size() <= Num) return 0;
303 return VI->second[Num];
306 case ValID::NameVal: { // Is it a named definition?
307 Value *N = lookupInSymbolTable(Ty, std::string(D.Name));
308 if (N == 0) return 0;
310 D.destroy(); // Free old strdup'd memory...
314 // Check to make sure that "Ty" is an integral type, and that our
315 // value will fit into the specified type...
316 case ValID::ConstSIntVal: // Is it a constant pool reference??
317 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64))
318 ThrowException("Signed integral constant '" +
319 itostr(D.ConstPool64) + "' is invalid for type '" +
320 Ty->getDescription() + "'!");
321 return ConstantSInt::get(Ty, D.ConstPool64);
323 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
324 if (!ConstantUInt::isValueValidForType(Ty, D.UConstPool64)) {
325 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64)) {
326 ThrowException("Integral constant '" + utostr(D.UConstPool64) +
327 "' is invalid or out of range!");
328 } else { // This is really a signed reference. Transmogrify.
329 return ConstantSInt::get(Ty, D.ConstPool64);
332 return ConstantUInt::get(Ty, D.UConstPool64);
335 case ValID::ConstFPVal: // Is it a floating point const pool reference?
336 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
337 ThrowException("FP constant invalid for type!!");
338 return ConstantFP::get(Ty, D.ConstPoolFP);
340 case ValID::ConstNullVal: // Is it a null value?
341 if (!isa<PointerType>(Ty))
342 ThrowException("Cannot create a a non pointer null!");
343 return ConstantPointerNull::get(cast<PointerType>(Ty));
345 case ValID::ConstantVal: // Fully resolved constant?
346 if (D.ConstantValue->getType() != Ty)
347 ThrowException("Constant expression type different from required type!");
348 return D.ConstantValue;
351 assert(0 && "Unhandled case!");
355 assert(0 && "Unhandled case!");
360 // getVal - This function is identical to getValNonImprovising, except that if a
361 // value is not already defined, it "improvises" by creating a placeholder var
362 // that looks and acts just like the requested variable. When the value is
363 // defined later, all uses of the placeholder variable are replaced with the
366 static Value *getVal(const Type *Ty, const ValID &D) {
368 // See if the value has already been defined...
369 Value *V = getValNonImprovising(Ty, D);
372 // If we reached here, we referenced either a symbol that we don't know about
373 // or an id number that hasn't been read yet. We may be referencing something
374 // forward, so just create an entry to be resolved later and get to it...
377 switch (Ty->getTypeID()) {
378 case Type::LabelTyID: d = new BBPlaceHolder(Ty, D); break;
379 default: d = new ValuePlaceHolder(Ty, D); break;
382 assert(d != 0 && "How did we not make something?");
383 if (inFunctionScope())
384 InsertValue(d, CurFun.LateResolveValues);
386 InsertValue(d, CurModule.LateResolveValues);
391 //===----------------------------------------------------------------------===//
392 // Code to handle forward references in instructions
393 //===----------------------------------------------------------------------===//
395 // This code handles the late binding needed with statements that reference
396 // values not defined yet... for example, a forward branch, or the PHI node for
399 // This keeps a table (CurFun.LateResolveValues) of all such forward references
400 // and back patchs after we are done.
403 // ResolveDefinitions - If we could not resolve some defs at parsing
404 // time (forward branches, phi functions for loops, etc...) resolve the
407 static void ResolveDefinitions(std::map<const Type*,ValueList> &LateResolvers,
408 std::map<const Type*,ValueList> *FutureLateResolvers) {
409 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
410 for (std::map<const Type*,ValueList>::iterator LRI = LateResolvers.begin(),
411 E = LateResolvers.end(); LRI != E; ++LRI) {
412 ValueList &List = LRI->second;
413 while (!List.empty()) {
414 Value *V = List.back();
416 ValID &DID = getValIDFromPlaceHolder(V);
418 Value *TheRealValue = getValNonImprovising(LRI->first, DID);
420 V->replaceAllUsesWith(TheRealValue);
422 } else if (FutureLateResolvers) {
423 // Functions have their unresolved items forwarded to the module late
425 InsertValue(V, *FutureLateResolvers);
427 if (DID.Type == ValID::NameVal)
428 ThrowException("Reference to an invalid definition: '" +DID.getName()+
429 "' of type '" + V->getType()->getDescription() + "'",
430 getLineNumFromPlaceHolder(V));
432 ThrowException("Reference to an invalid definition: #" +
433 itostr(DID.Num) + " of type '" +
434 V->getType()->getDescription() + "'",
435 getLineNumFromPlaceHolder(V));
440 LateResolvers.clear();
443 // ResolveTypeTo - A brand new type was just declared. This means that (if
444 // name is not null) things referencing Name can be resolved. Otherwise, things
445 // refering to the number can be resolved. Do this now.
447 static void ResolveTypeTo(char *Name, const Type *ToTy) {
448 std::vector<PATypeHolder> &Types = inFunctionScope() ?
449 CurFun.Types : CurModule.Types;
452 if (Name) D = ValID::create(Name);
453 else D = ValID::create((int)Types.size());
455 std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
456 CurFun.LateResolveTypes : CurModule.LateResolveTypes;
458 std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
459 if (I != LateResolver.end()) {
460 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
461 LateResolver.erase(I);
465 // ResolveTypes - At this point, all types should be resolved. Any that aren't
468 static void ResolveTypes(std::map<ValID, PATypeHolder> &LateResolveTypes) {
469 if (!LateResolveTypes.empty()) {
470 const ValID &DID = LateResolveTypes.begin()->first;
472 if (DID.Type == ValID::NameVal)
473 ThrowException("Reference to an invalid type: '" +DID.getName() + "'");
475 ThrowException("Reference to an invalid type: #" + itostr(DID.Num));
480 static void setValueNameInternal(Value *V, const std::string &Name,
482 if (V->getType() == Type::VoidTy)
483 ThrowException("Can't assign name '" + Name + "' to value with void type!");
486 V->setName(Name, &ST);
489 // setValueName - Set the specified value to the name given. The name may be
490 // null potentially, in which case this is a noop. The string passed in is
491 // assumed to be a malloc'd string buffer, and is free'd by this function.
493 static void setValueName(Value *V, char *NameStr) {
495 std::string Name(NameStr); // Copy string
496 free(NameStr); // Free old string
498 assert(inFunctionScope() && "Must be in function scope!");
499 SymbolTable &ST = CurFun.CurrentFunction->getSymbolTable();
500 if (ST.lookup(V->getType(), Name))
501 ThrowException("Redefinition of value named '" + Name + "' in the '" +
502 V->getType()->getDescription() + "' type plane!");
504 setValueNameInternal(V, Name, ST);
508 // setValueNameMergingDuplicates - Set the specified value to the name given.
509 // The name may be null potentially, in which case this is a noop. The string
510 // passed in is assumed to be a malloc'd string buffer, and is free'd by this
513 // This function returns true if the value has already been defined, but is
514 // allowed to be redefined in the specified context. If the name is a new name
515 // for the typeplane, false is returned.
517 static bool setValueNameMergingDuplicates(Value *V, char *NameStr) {
518 if (NameStr == 0) return false;
520 std::string Name(NameStr); // Copy string
521 free(NameStr); // Free old string
523 // FIXME: If we eliminated the function constant pool (which we should), this
524 // would just unconditionally look at the module symtab.
525 SymbolTable &ST = inFunctionScope() ?
526 CurFun.CurrentFunction->getSymbolTable() :
527 CurModule.CurrentModule->getSymbolTable();
529 Value *Existing = ST.lookup(V->getType(), Name);
530 if (Existing) { // Inserting a name that is already defined???
531 // We are a simple redefinition of a value, check to see if it is defined
532 // the same as the old one...
533 if (GlobalVariable *EGV = dyn_cast<GlobalVariable>(Existing)) {
534 // We are allowed to redefine a global variable in two circumstances:
535 // 1. If at least one of the globals is uninitialized or
536 // 2. If both initializers have the same value.
538 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
539 if (!EGV->hasInitializer() || !GV->hasInitializer() ||
540 EGV->getInitializer() == GV->getInitializer()) {
542 // Make sure the existing global version gets the initializer! Make
543 // sure that it also gets marked const if the new version is.
544 if (GV->hasInitializer() && !EGV->hasInitializer())
545 EGV->setInitializer(GV->getInitializer());
546 if (GV->isConstant())
547 EGV->setConstant(true);
548 EGV->setLinkage(GV->getLinkage());
550 delete GV; // Destroy the duplicate!
551 return true; // They are equivalent!
554 } else if (const Constant *C = dyn_cast<Constant>(Existing)) {
555 if (C == V) return true; // Constants are equal to themselves
558 ThrowException("Redefinition of value named '" + Name + "' in the '" +
559 V->getType()->getDescription() + "' type plane!");
562 setValueNameInternal(V, Name, ST);
568 // setTypeName - Set the specified type to the name given. The name may be
569 // null potentially, in which case this is a noop. The string passed in is
570 // assumed to be a malloc'd string buffer, and is freed by this function.
572 // This function returns true if the type has already been defined, but is
573 // allowed to be redefined in the specified context. If the name is a new name
574 // for the type plane, it is inserted and false is returned.
575 static bool setTypeName(const Type *T, char *NameStr) {
576 if (NameStr == 0) return false;
578 std::string Name(NameStr); // Copy string
579 free(NameStr); // Free old string
581 // We don't allow assigning names to void type
582 if (T == Type::VoidTy)
583 ThrowException("Can't assign name '" + Name + "' to the void type!");
585 SymbolTable &ST = inFunctionScope() ?
586 CurFun.CurrentFunction->getSymbolTable() :
587 CurModule.CurrentModule->getSymbolTable();
589 // Inserting a name that is already defined???
590 if (Type *Existing = ST.lookupType(Name)) {
591 // There is only one case where this is allowed: when we are refining an
592 // opaque type. In this case, Existing will be an opaque type.
593 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
594 // We ARE replacing an opaque type!
595 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
599 // Otherwise, this is an attempt to redefine a type. That's okay if
600 // the redefinition is identical to the original. This will be so if
601 // Existing and T point to the same Type object. In this one case we
602 // allow the equivalent redefinition.
603 if (Existing == T) return true; // Yes, it's equal.
605 // Any other kind of (non-equivalent) redefinition is an error.
606 ThrowException("Redefinition of type named '" + Name + "' in the '" +
607 T->getDescription() + "' type plane!");
610 // Okay, its a newly named type. Set its name.
611 if (!Name.empty()) ST.insert(Name, T);
616 //===----------------------------------------------------------------------===//
617 // Code for handling upreferences in type names...
620 // TypeContains - Returns true if Ty directly contains E in it.
622 static bool TypeContains(const Type *Ty, const Type *E) {
623 return find(Ty->subtype_begin(), Ty->subtype_end(), E) != Ty->subtype_end();
628 // NestingLevel - The number of nesting levels that need to be popped before
629 // this type is resolved.
630 unsigned NestingLevel;
632 // LastContainedTy - This is the type at the current binding level for the
633 // type. Every time we reduce the nesting level, this gets updated.
634 const Type *LastContainedTy;
636 // UpRefTy - This is the actual opaque type that the upreference is
640 UpRefRecord(unsigned NL, OpaqueType *URTy)
641 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
645 // UpRefs - A list of the outstanding upreferences that need to be resolved.
646 static std::vector<UpRefRecord> UpRefs;
648 /// HandleUpRefs - Every time we finish a new layer of types, this function is
649 /// called. It loops through the UpRefs vector, which is a list of the
650 /// currently active types. For each type, if the up reference is contained in
651 /// the newly completed type, we decrement the level count. When the level
652 /// count reaches zero, the upreferenced type is the type that is passed in:
653 /// thus we can complete the cycle.
655 static PATypeHolder HandleUpRefs(const Type *ty) {
656 if (!ty->isAbstract()) return ty;
658 UR_OUT("Type '" << Ty->getDescription() <<
659 "' newly formed. Resolving upreferences.\n" <<
660 UpRefs.size() << " upreferences active!\n");
662 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
663 // to zero), we resolve them all together before we resolve them to Ty. At
664 // the end of the loop, if there is anything to resolve to Ty, it will be in
666 OpaqueType *TypeToResolve = 0;
668 for (unsigned i = 0; i != UpRefs.size(); ++i) {
669 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
670 << UpRefs[i].second->getDescription() << ") = "
671 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
672 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
673 // Decrement level of upreference
674 unsigned Level = --UpRefs[i].NestingLevel;
675 UpRefs[i].LastContainedTy = Ty;
676 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
677 if (Level == 0) { // Upreference should be resolved!
678 if (!TypeToResolve) {
679 TypeToResolve = UpRefs[i].UpRefTy;
681 UR_OUT(" * Resolving upreference for "
682 << UpRefs[i].second->getDescription() << "\n";
683 std::string OldName = UpRefs[i].UpRefTy->getDescription());
684 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
685 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
686 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
688 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
689 --i; // Do not skip the next element...
695 UR_OUT(" * Resolving upreference for "
696 << UpRefs[i].second->getDescription() << "\n";
697 std::string OldName = TypeToResolve->getDescription());
698 TypeToResolve->refineAbstractTypeTo(Ty);
705 //===----------------------------------------------------------------------===//
706 // RunVMAsmParser - Define an interface to this parser
707 //===----------------------------------------------------------------------===//
709 Module *RunVMAsmParser(const std::string &Filename, FILE *F) {
711 CurFilename = Filename;
712 llvmAsmlineno = 1; // Reset the current line number...
713 ObsoleteVarArgs = false;
715 // Allocate a new module to read
716 CurModule.CurrentModule = new Module(Filename);
718 yyparse(); // Parse the file, potentially throwing exception
720 Module *Result = ParserResult;
722 // Check to see if they called va_start but not va_arg..
723 if (!ObsoleteVarArgs)
724 if (Function *F = Result->getNamedFunction("llvm.va_start"))
725 if (F->asize() == 1) {
726 std::cerr << "WARNING: this file uses obsolete features. "
727 << "Assemble and disassemble to update it.\n";
728 ObsoleteVarArgs = true;
731 if (ObsoleteVarArgs) {
732 // If the user is making use of obsolete varargs intrinsics, adjust them for
734 if (Function *F = Result->getNamedFunction("llvm.va_start")) {
735 assert(F->asize() == 1 && "Obsolete va_start takes 1 argument!");
737 const Type *RetTy = F->getFunctionType()->getParamType(0);
738 RetTy = cast<PointerType>(RetTy)->getElementType();
739 Function *NF = Result->getOrInsertFunction("llvm.va_start", RetTy, 0);
741 while (!F->use_empty()) {
742 CallInst *CI = cast<CallInst>(F->use_back());
743 Value *V = new CallInst(NF, "", CI);
744 new StoreInst(V, CI->getOperand(1), CI);
745 CI->getParent()->getInstList().erase(CI);
747 Result->getFunctionList().erase(F);
750 if (Function *F = Result->getNamedFunction("llvm.va_end")) {
751 assert(F->asize() == 1 && "Obsolete va_end takes 1 argument!");
752 const Type *ArgTy = F->getFunctionType()->getParamType(0);
753 ArgTy = cast<PointerType>(ArgTy)->getElementType();
754 Function *NF = Result->getOrInsertFunction("llvm.va_end", Type::VoidTy,
757 while (!F->use_empty()) {
758 CallInst *CI = cast<CallInst>(F->use_back());
759 Value *V = new LoadInst(CI->getOperand(1), "", CI);
760 new CallInst(NF, V, "", CI);
761 CI->getParent()->getInstList().erase(CI);
763 Result->getFunctionList().erase(F);
766 if (Function *F = Result->getNamedFunction("llvm.va_copy")) {
767 assert(F->asize() == 2 && "Obsolete va_copy takes 2 argument!");
768 const Type *ArgTy = F->getFunctionType()->getParamType(0);
769 ArgTy = cast<PointerType>(ArgTy)->getElementType();
770 Function *NF = Result->getOrInsertFunction("llvm.va_copy", ArgTy,
773 while (!F->use_empty()) {
774 CallInst *CI = cast<CallInst>(F->use_back());
775 Value *V = new CallInst(NF, CI->getOperand(2), "", CI);
776 new StoreInst(V, CI->getOperand(1), CI);
777 CI->getParent()->getInstList().erase(CI);
779 Result->getFunctionList().erase(F);
783 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
789 } // End llvm namespace
791 using namespace llvm;
796 llvm::Module *ModuleVal;
797 llvm::Function *FunctionVal;
798 std::pair<llvm::PATypeHolder*, char*> *ArgVal;
799 llvm::BasicBlock *BasicBlockVal;
800 llvm::TerminatorInst *TermInstVal;
801 llvm::Instruction *InstVal;
802 llvm::Constant *ConstVal;
804 const llvm::Type *PrimType;
805 llvm::PATypeHolder *TypeVal;
806 llvm::Value *ValueVal;
808 std::vector<std::pair<llvm::PATypeHolder*,char*> > *ArgList;
809 std::vector<llvm::Value*> *ValueList;
810 std::list<llvm::PATypeHolder> *TypeList;
811 std::list<std::pair<llvm::Value*,
812 llvm::BasicBlock*> > *PHIList; // Represent the RHS of PHI node
813 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
814 std::vector<llvm::Constant*> *ConstVector;
816 llvm::GlobalValue::LinkageTypes Linkage;
824 char *StrVal; // This memory is strdup'd!
825 llvm::ValID ValIDVal; // strdup'd memory maybe!
827 llvm::Instruction::BinaryOps BinaryOpVal;
828 llvm::Instruction::TermOps TermOpVal;
829 llvm::Instruction::MemoryOps MemOpVal;
830 llvm::Instruction::OtherOps OtherOpVal;
831 llvm::Module::Endianness Endianness;
834 %type <ModuleVal> Module FunctionList
835 %type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
836 %type <BasicBlockVal> BasicBlock InstructionList
837 %type <TermInstVal> BBTerminatorInst
838 %type <InstVal> Inst InstVal MemoryInst
839 %type <ConstVal> ConstVal ConstExpr
840 %type <ConstVector> ConstVector
841 %type <ArgList> ArgList ArgListH
842 %type <ArgVal> ArgVal
843 %type <PHIList> PHIList
844 %type <ValueList> ValueRefList ValueRefListE // For call param lists
845 %type <ValueList> IndexList // For GEP derived indices
846 %type <TypeList> TypeListI ArgTypeListI
847 %type <JumpTable> JumpTable
848 %type <BoolVal> GlobalType // GLOBAL or CONSTANT?
849 %type <BoolVal> OptVolatile // 'volatile' or not
850 %type <Linkage> OptLinkage
851 %type <Endianness> BigOrLittle
853 // ValueRef - Unresolved reference to a definition or BB
854 %type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
855 %type <ValueVal> ResolvedVal // <type> <valref> pair
856 // Tokens and types for handling constant integer values
858 // ESINT64VAL - A negative number within long long range
859 %token <SInt64Val> ESINT64VAL
861 // EUINT64VAL - A positive number within uns. long long range
862 %token <UInt64Val> EUINT64VAL
863 %type <SInt64Val> EINT64VAL
865 %token <SIntVal> SINTVAL // Signed 32 bit ints...
866 %token <UIntVal> UINTVAL // Unsigned 32 bit ints...
867 %type <SIntVal> INTVAL
868 %token <FPVal> FPVAL // Float or Double constant
871 %type <TypeVal> Types TypesV UpRTypes UpRTypesV
872 %type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
873 %token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
874 %token <PrimType> FLOAT DOUBLE TYPE LABEL
876 %token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
877 %type <StrVal> Name OptName OptAssign
880 %token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
881 %token DECLARE GLOBAL CONSTANT VOLATILE
882 %token TO DOTDOTDOT NULL_TOK CONST INTERNAL LINKONCE WEAK APPENDING
883 %token OPAQUE NOT EXTERNAL TARGET ENDIAN POINTERSIZE LITTLE BIG
885 // Basic Block Terminating Operators
886 %token <TermOpVal> RET BR SWITCH INVOKE UNWIND
889 %type <BinaryOpVal> BinaryOps // all the binary operators
890 %type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
891 %token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
892 %token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
894 // Memory Instructions
895 %token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
898 %type <OtherOpVal> ShiftOps
899 %token <OtherOpVal> PHI_TOK CALL CAST SELECT SHL SHR VAARG VANEXT
900 %token VA_ARG // FIXME: OBSOLETE
905 // Handle constant integer size restriction and conversion...
909 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
910 ThrowException("Value too large for type!");
915 EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
916 EINT64VAL : EUINT64VAL {
917 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
918 ThrowException("Value too large for type!");
922 // Operations that are notably excluded from this list include:
923 // RET, BR, & SWITCH because they end basic blocks and are treated specially.
925 ArithmeticOps: ADD | SUB | MUL | DIV | REM;
926 LogicalOps : AND | OR | XOR;
927 SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
928 BinaryOps : ArithmeticOps | LogicalOps | SetCondOps;
930 ShiftOps : SHL | SHR;
932 // These are some types that allow classification if we only want a particular
933 // thing... for example, only a signed, unsigned, or integral type.
934 SIntType : LONG | INT | SHORT | SBYTE;
935 UIntType : ULONG | UINT | USHORT | UBYTE;
936 IntType : SIntType | UIntType;
937 FPType : FLOAT | DOUBLE;
939 // OptAssign - Value producing statements have an optional assignment component
940 OptAssign : Name '=' {
947 OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
948 LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
949 WEAK { $$ = GlobalValue::WeakLinkage; } |
950 APPENDING { $$ = GlobalValue::AppendingLinkage; } |
951 /*empty*/ { $$ = GlobalValue::ExternalLinkage; };
953 //===----------------------------------------------------------------------===//
954 // Types includes all predefined types... except void, because it can only be
955 // used in specific contexts (function returning void for example). To have
956 // access to it, a user must explicitly use TypesV.
959 // TypesV includes all of 'Types', but it also includes the void type.
960 TypesV : Types | VOID { $$ = new PATypeHolder($1); };
961 UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
965 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
970 // Derived types are added later...
972 PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
973 PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
975 $$ = new PATypeHolder(OpaqueType::get());
978 $$ = new PATypeHolder($1);
980 UpRTypes : SymbolicValueRef { // Named types are also simple types...
981 $$ = new PATypeHolder(getTypeVal($1));
984 // Include derived types in the Types production.
986 UpRTypes : '\\' EUINT64VAL { // Type UpReference
987 if ($2 > (uint64_t)~0U) ThrowException("Value out of range!");
988 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
989 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
990 $$ = new PATypeHolder(OT);
991 UR_OUT("New Upreference!\n");
993 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
994 std::vector<const Type*> Params;
995 mapto($3->begin(), $3->end(), std::back_inserter(Params),
996 std::mem_fun_ref(&PATypeHolder::get));
997 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
998 if (isVarArg) Params.pop_back();
1000 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
1001 delete $3; // Delete the argument list
1002 delete $1; // Delete the return type handle
1004 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
1005 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
1008 | '{' TypeListI '}' { // Structure type?
1009 std::vector<const Type*> Elements;
1010 mapto($2->begin(), $2->end(), std::back_inserter(Elements),
1011 std::mem_fun_ref(&PATypeHolder::get));
1013 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
1016 | '{' '}' { // Empty structure type?
1017 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
1019 | UpRTypes '*' { // Pointer type?
1020 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
1024 // TypeList - Used for struct declarations and as a basis for function type
1025 // declaration type lists
1027 TypeListI : UpRTypes {
1028 $$ = new std::list<PATypeHolder>();
1029 $$->push_back(*$1); delete $1;
1031 | TypeListI ',' UpRTypes {
1032 ($$=$1)->push_back(*$3); delete $3;
1035 // ArgTypeList - List of types for a function type declaration...
1036 ArgTypeListI : TypeListI
1037 | TypeListI ',' DOTDOTDOT {
1038 ($$=$1)->push_back(Type::VoidTy);
1041 ($$ = new std::list<PATypeHolder>())->push_back(Type::VoidTy);
1044 $$ = new std::list<PATypeHolder>();
1047 // ConstVal - The various declarations that go into the constant pool. This
1048 // production is used ONLY to represent constants that show up AFTER a 'const',
1049 // 'constant' or 'global' token at global scope. Constants that can be inlined
1050 // into other expressions (such as integers and constexprs) are handled by the
1051 // ResolvedVal, ValueRef and ConstValueRef productions.
1053 ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
1054 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
1056 ThrowException("Cannot make array constant with type: '" +
1057 (*$1)->getDescription() + "'!");
1058 const Type *ETy = ATy->getElementType();
1059 int NumElements = ATy->getNumElements();
1061 // Verify that we have the correct size...
1062 if (NumElements != -1 && NumElements != (int)$3->size())
1063 ThrowException("Type mismatch: constant sized array initialized with " +
1064 utostr($3->size()) + " arguments, but has size of " +
1065 itostr(NumElements) + "!");
1067 // Verify all elements are correct type!
1068 for (unsigned i = 0; i < $3->size(); i++) {
1069 if (ETy != (*$3)[i]->getType())
1070 ThrowException("Element #" + utostr(i) + " is not of type '" +
1071 ETy->getDescription() +"' as required!\nIt is of type '"+
1072 (*$3)[i]->getType()->getDescription() + "'.");
1075 $$ = ConstantArray::get(ATy, *$3);
1076 delete $1; delete $3;
1079 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
1081 ThrowException("Cannot make array constant with type: '" +
1082 (*$1)->getDescription() + "'!");
1084 int NumElements = ATy->getNumElements();
1085 if (NumElements != -1 && NumElements != 0)
1086 ThrowException("Type mismatch: constant sized array initialized with 0"
1087 " arguments, but has size of " + itostr(NumElements) +"!");
1088 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
1091 | Types 'c' STRINGCONSTANT {
1092 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
1094 ThrowException("Cannot make array constant with type: '" +
1095 (*$1)->getDescription() + "'!");
1097 int NumElements = ATy->getNumElements();
1098 const Type *ETy = ATy->getElementType();
1099 char *EndStr = UnEscapeLexed($3, true);
1100 if (NumElements != -1 && NumElements != (EndStr-$3))
1101 ThrowException("Can't build string constant of size " +
1102 itostr((int)(EndStr-$3)) +
1103 " when array has size " + itostr(NumElements) + "!");
1104 std::vector<Constant*> Vals;
1105 if (ETy == Type::SByteTy) {
1106 for (char *C = $3; C != EndStr; ++C)
1107 Vals.push_back(ConstantSInt::get(ETy, *C));
1108 } else if (ETy == Type::UByteTy) {
1109 for (char *C = $3; C != EndStr; ++C)
1110 Vals.push_back(ConstantUInt::get(ETy, (unsigned char)*C));
1113 ThrowException("Cannot build string arrays of non byte sized elements!");
1116 $$ = ConstantArray::get(ATy, Vals);
1119 | Types '{' ConstVector '}' {
1120 const StructType *STy = dyn_cast<StructType>($1->get());
1122 ThrowException("Cannot make struct constant with type: '" +
1123 (*$1)->getDescription() + "'!");
1125 if ($3->size() != STy->getNumContainedTypes())
1126 ThrowException("Illegal number of initializers for structure type!");
1128 // Check to ensure that constants are compatible with the type initializer!
1129 for (unsigned i = 0, e = $3->size(); i != e; ++i)
1130 if ((*$3)[i]->getType() != STy->getElementType(i))
1131 ThrowException("Expected type '" +
1132 STy->getElementType(i)->getDescription() +
1133 "' for element #" + utostr(i) +
1134 " of structure initializer!");
1136 $$ = ConstantStruct::get(STy, *$3);
1137 delete $1; delete $3;
1140 const StructType *STy = dyn_cast<StructType>($1->get());
1142 ThrowException("Cannot make struct constant with type: '" +
1143 (*$1)->getDescription() + "'!");
1145 if (STy->getNumContainedTypes() != 0)
1146 ThrowException("Illegal number of initializers for structure type!");
1148 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1152 const PointerType *PTy = dyn_cast<PointerType>($1->get());
1154 ThrowException("Cannot make null pointer constant with type: '" +
1155 (*$1)->getDescription() + "'!");
1157 $$ = ConstantPointerNull::get(PTy);
1160 | Types SymbolicValueRef {
1161 const PointerType *Ty = dyn_cast<PointerType>($1->get());
1163 ThrowException("Global const reference must be a pointer type!");
1165 // ConstExprs can exist in the body of a function, thus creating
1166 // ConstantPointerRefs whenever they refer to a variable. Because we are in
1167 // the context of a function, getValNonImprovising will search the functions
1168 // symbol table instead of the module symbol table for the global symbol,
1169 // which throws things all off. To get around this, we just tell
1170 // getValNonImprovising that we are at global scope here.
1172 Function *SavedCurFn = CurFun.CurrentFunction;
1173 CurFun.CurrentFunction = 0;
1175 Value *V = getValNonImprovising(Ty, $2);
1177 CurFun.CurrentFunction = SavedCurFn;
1179 // If this is an initializer for a constant pointer, which is referencing a
1180 // (currently) undefined variable, create a stub now that shall be replaced
1181 // in the future with the right type of variable.
1184 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1185 const PointerType *PT = cast<PointerType>(Ty);
1187 // First check to see if the forward references value is already created!
1188 PerModuleInfo::GlobalRefsType::iterator I =
1189 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
1191 if (I != CurModule.GlobalRefs.end()) {
1192 V = I->second; // Placeholder already exists, use it...
1195 // Create a placeholder for the global variable reference...
1196 GlobalVariable *GV = new GlobalVariable(PT->getElementType(),
1198 GlobalValue::ExternalLinkage);
1199 // Keep track of the fact that we have a forward ref to recycle it
1200 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1202 // Must temporarily push this value into the module table...
1203 CurModule.CurrentModule->getGlobalList().push_back(GV);
1208 GlobalValue *GV = cast<GlobalValue>(V);
1209 $$ = ConstantPointerRef::get(GV);
1210 delete $1; // Free the type handle
1213 if ($1->get() != $2->getType())
1214 ThrowException("Mismatched types for constant expression!");
1218 | Types ZEROINITIALIZER {
1219 $$ = Constant::getNullValue($1->get());
1223 ConstVal : SIntType EINT64VAL { // integral constants
1224 if (!ConstantSInt::isValueValidForType($1, $2))
1225 ThrowException("Constant value doesn't fit in type!");
1226 $$ = ConstantSInt::get($1, $2);
1228 | UIntType EUINT64VAL { // integral constants
1229 if (!ConstantUInt::isValueValidForType($1, $2))
1230 ThrowException("Constant value doesn't fit in type!");
1231 $$ = ConstantUInt::get($1, $2);
1233 | BOOL TRUETOK { // Boolean constants
1234 $$ = ConstantBool::True;
1236 | BOOL FALSETOK { // Boolean constants
1237 $$ = ConstantBool::False;
1239 | FPType FPVAL { // Float & Double constants
1240 $$ = ConstantFP::get($1, $2);
1244 ConstExpr: CAST '(' ConstVal TO Types ')' {
1245 if (!$3->getType()->isFirstClassType())
1246 ThrowException("cast constant expression from a non-primitive type: '" +
1247 $3->getType()->getDescription() + "'!");
1248 if (!$5->get()->isFirstClassType())
1249 ThrowException("cast constant expression to a non-primitive type: '" +
1250 $5->get()->getDescription() + "'!");
1251 $$ = ConstantExpr::getCast($3, $5->get());
1254 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1255 if (!isa<PointerType>($3->getType()))
1256 ThrowException("GetElementPtr requires a pointer operand!");
1258 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
1259 // indices to uint struct indices for compatibility.
1260 generic_gep_type_iterator<std::vector<Value*>::iterator>
1261 GTI = gep_type_begin($3->getType(), $4->begin(), $4->end()),
1262 GTE = gep_type_end($3->getType(), $4->begin(), $4->end());
1263 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
1264 if (isa<StructType>(*GTI)) // Only change struct indices
1265 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
1266 if (CUI->getType() == Type::UByteTy)
1267 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
1270 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
1272 ThrowException("Index list invalid for constant getelementptr!");
1274 std::vector<Constant*> IdxVec;
1275 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1276 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
1277 IdxVec.push_back(C);
1279 ThrowException("Indices to constant getelementptr must be constants!");
1283 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
1285 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1286 if ($3->getType() != Type::BoolTy)
1287 ThrowException("Select condition must be of boolean type!");
1288 if ($5->getType() != $7->getType())
1289 ThrowException("Select operand types must match!");
1290 $$ = ConstantExpr::getSelect($3, $5, $7);
1292 | BinaryOps '(' ConstVal ',' ConstVal ')' {
1293 if ($3->getType() != $5->getType())
1294 ThrowException("Binary operator types must match!");
1295 $$ = ConstantExpr::get($1, $3, $5);
1297 | ShiftOps '(' ConstVal ',' ConstVal ')' {
1298 if ($5->getType() != Type::UByteTy)
1299 ThrowException("Shift count for shift constant must be unsigned byte!");
1300 if (!$3->getType()->isInteger())
1301 ThrowException("Shift constant expression requires integer operand!");
1302 $$ = ConstantExpr::get($1, $3, $5);
1306 // ConstVector - A list of comma separated constants.
1307 ConstVector : ConstVector ',' ConstVal {
1308 ($$ = $1)->push_back($3);
1311 $$ = new std::vector<Constant*>();
1316 // GlobalType - Match either GLOBAL or CONSTANT for global declarations...
1317 GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
1320 //===----------------------------------------------------------------------===//
1321 // Rules to match Modules
1322 //===----------------------------------------------------------------------===//
1324 // Module rule: Capture the result of parsing the whole file into a result
1327 Module : FunctionList {
1328 $$ = ParserResult = $1;
1329 CurModule.ModuleDone();
1332 // FunctionList - A list of functions, preceeded by a constant pool.
1334 FunctionList : FunctionList Function {
1336 CurFun.FunctionDone();
1338 | FunctionList FunctionProto {
1341 | FunctionList IMPLEMENTATION {
1345 $$ = CurModule.CurrentModule;
1346 // Resolve circular types before we parse the body of the module
1347 ResolveTypes(CurModule.LateResolveTypes);
1350 // ConstPool - Constants with optional names assigned to them.
1351 ConstPool : ConstPool OptAssign CONST ConstVal {
1352 // FIXME: THIS SHOULD REALLY BE ELIMINATED. It is totally unneeded.
1353 if (!setValueNameMergingDuplicates($4, $2))
1356 | ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
1357 // Eagerly resolve types. This is not an optimization, this is a
1358 // requirement that is due to the fact that we could have this:
1360 // %list = type { %list * }
1361 // %list = type { %list * } ; repeated type decl
1363 // If types are not resolved eagerly, then the two types will not be
1364 // determined to be the same type!
1366 ResolveTypeTo($2, *$4);
1368 if (!setTypeName(*$4, $2) && !$2) {
1369 // If this is a named type that is not a redefinition, add it to the slot
1371 InsertType(*$4, inFunctionScope() ? CurFun.Types : CurModule.Types);
1376 | ConstPool FunctionProto { // Function prototypes can be in const pool
1378 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
1379 const Type *Ty = $5->getType();
1380 // Global declarations appear in Constant Pool
1381 Constant *Initializer = $5;
1382 if (Initializer == 0)
1383 ThrowException("Global value initializer is not a constant!");
1385 GlobalVariable *GV = new GlobalVariable(Ty, $4, $3, Initializer);
1386 if (!setValueNameMergingDuplicates(GV, $2)) { // If not redefining...
1387 CurModule.CurrentModule->getGlobalList().push_back(GV);
1388 int Slot = InsertValue(GV, CurModule.Values);
1391 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1393 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1394 (char*)GV->getName().c_str()));
1398 | ConstPool OptAssign EXTERNAL GlobalType Types {
1399 const Type *Ty = *$5;
1400 // Global declarations appear in Constant Pool
1401 GlobalVariable *GV = new GlobalVariable(Ty,$4,GlobalValue::ExternalLinkage);
1402 if (!setValueNameMergingDuplicates(GV, $2)) { // If not redefining...
1403 CurModule.CurrentModule->getGlobalList().push_back(GV);
1404 int Slot = InsertValue(GV, CurModule.Values);
1407 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1409 assert(GV->hasName() && "Not named and not numbered!?");
1410 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1411 (char*)GV->getName().c_str()));
1416 | ConstPool TARGET TargetDefinition {
1418 | /* empty: end of list */ {
1423 BigOrLittle : BIG { $$ = Module::BigEndian; };
1424 BigOrLittle : LITTLE { $$ = Module::LittleEndian; };
1426 TargetDefinition : ENDIAN '=' BigOrLittle {
1427 CurModule.CurrentModule->setEndianness($3);
1429 | POINTERSIZE '=' EUINT64VAL {
1431 CurModule.CurrentModule->setPointerSize(Module::Pointer32);
1433 CurModule.CurrentModule->setPointerSize(Module::Pointer64);
1435 ThrowException("Invalid pointer size: '" + utostr($3) + "'!");
1439 //===----------------------------------------------------------------------===//
1440 // Rules to match Function Headers
1441 //===----------------------------------------------------------------------===//
1443 Name : VAR_ID | STRINGCONSTANT;
1444 OptName : Name | /*empty*/ { $$ = 0; };
1446 ArgVal : Types OptName {
1447 if (*$1 == Type::VoidTy)
1448 ThrowException("void typed arguments are invalid!");
1449 $$ = new std::pair<PATypeHolder*, char*>($1, $2);
1452 ArgListH : ArgListH ',' ArgVal {
1458 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1463 ArgList : ArgListH {
1466 | ArgListH ',' DOTDOTDOT {
1468 $$->push_back(std::pair<PATypeHolder*,
1469 char*>(new PATypeHolder(Type::VoidTy), 0));
1472 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1473 $$->push_back(std::make_pair(new PATypeHolder(Type::VoidTy), (char*)0));
1479 FunctionHeaderH : TypesV Name '(' ArgList ')' {
1481 std::string FunctionName($2);
1483 if (!(*$1)->isFirstClassType() && *$1 != Type::VoidTy)
1484 ThrowException("LLVM functions cannot return aggregate types!");
1486 std::vector<const Type*> ParamTypeList;
1487 if ($4) { // If there are arguments...
1488 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $4->begin();
1489 I != $4->end(); ++I)
1490 ParamTypeList.push_back(I->first->get());
1493 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1494 if (isVarArg) ParamTypeList.pop_back();
1496 const FunctionType *FT = FunctionType::get(*$1, ParamTypeList, isVarArg);
1497 const PointerType *PFT = PointerType::get(FT);
1501 // Is the function already in symtab?
1502 if ((Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
1503 // Yes it is. If this is the case, either we need to be a forward decl,
1504 // or it needs to be.
1505 if (!CurFun.isDeclare && !Fn->isExternal())
1506 ThrowException("Redefinition of function '" + FunctionName + "'!");
1508 // Make sure to strip off any argument names so we can't get conflicts...
1509 for (Function::aiterator AI = Fn->abegin(), AE = Fn->aend(); AI != AE; ++AI)
1512 } else { // Not already defined?
1513 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName,
1514 CurModule.CurrentModule);
1515 InsertValue(Fn, CurModule.Values);
1516 CurModule.DeclareNewGlobalValue(Fn, ValID::create($2));
1518 free($2); // Free strdup'd memory!
1520 CurFun.FunctionStart(Fn);
1522 // Add all of the arguments we parsed to the function...
1523 if ($4) { // Is null if empty...
1524 if (isVarArg) { // Nuke the last entry
1525 assert($4->back().first->get() == Type::VoidTy && $4->back().second == 0&&
1526 "Not a varargs marker!");
1527 delete $4->back().first;
1528 $4->pop_back(); // Delete the last entry
1530 Function::aiterator ArgIt = Fn->abegin();
1531 for (std::vector<std::pair<PATypeHolder*, char*> >::iterator I =$4->begin();
1532 I != $4->end(); ++I, ++ArgIt) {
1533 delete I->first; // Delete the typeholder...
1535 setValueName(ArgIt, I->second); // Insert arg into symtab...
1539 delete $4; // We're now done with the argument list
1543 BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1545 FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
1546 $$ = CurFun.CurrentFunction;
1548 // Make sure that we keep track of the linkage type even if there was a
1549 // previous "declare".
1552 // Resolve circular types before we parse the body of the function.
1553 ResolveTypes(CurFun.LateResolveTypes);
1556 END : ENDTOK | '}'; // Allow end of '}' to end a function
1558 Function : BasicBlockList END {
1562 FunctionProto : DECLARE { CurFun.isDeclare = true; } FunctionHeaderH {
1563 $$ = CurFun.CurrentFunction;
1564 CurFun.FunctionDone();
1567 //===----------------------------------------------------------------------===//
1568 // Rules to match Basic Blocks
1569 //===----------------------------------------------------------------------===//
1571 ConstValueRef : ESINT64VAL { // A reference to a direct constant
1572 $$ = ValID::create($1);
1575 $$ = ValID::create($1);
1577 | FPVAL { // Perhaps it's an FP constant?
1578 $$ = ValID::create($1);
1581 $$ = ValID::create(ConstantBool::True);
1584 $$ = ValID::create(ConstantBool::False);
1587 $$ = ValID::createNull();
1590 $$ = ValID::create($1);
1593 // SymbolicValueRef - Reference to one of two ways of symbolically refering to
1596 SymbolicValueRef : INTVAL { // Is it an integer reference...?
1597 $$ = ValID::create($1);
1599 | Name { // Is it a named reference...?
1600 $$ = ValID::create($1);
1603 // ValueRef - A reference to a definition... either constant or symbolic
1604 ValueRef : SymbolicValueRef | ConstValueRef;
1607 // ResolvedVal - a <type> <value> pair. This is used only in cases where the
1608 // type immediately preceeds the value reference, and allows complex constant
1609 // pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
1610 ResolvedVal : Types ValueRef {
1611 $$ = getVal(*$1, $2); delete $1;
1614 BasicBlockList : BasicBlockList BasicBlock {
1617 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
1622 // Basic blocks are terminated by branching instructions:
1623 // br, br/cc, switch, ret
1625 BasicBlock : InstructionList OptAssign BBTerminatorInst {
1626 setValueName($3, $2);
1629 $1->getInstList().push_back($3);
1633 | LABELSTR InstructionList OptAssign BBTerminatorInst {
1634 setValueName($4, $3);
1637 $2->getInstList().push_back($4);
1638 setValueName($2, $1);
1644 InstructionList : InstructionList Inst {
1645 $1->getInstList().push_back($2);
1649 $$ = CurBB = new BasicBlock("", CurFun.CurrentFunction);
1652 BBTerminatorInst : RET ResolvedVal { // Return with a result...
1653 $$ = new ReturnInst($2);
1655 | RET VOID { // Return with no result...
1656 $$ = new ReturnInst();
1658 | BR LABEL ValueRef { // Unconditional Branch...
1659 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $3)));
1660 } // Conditional Branch...
1661 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
1662 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $6)),
1663 cast<BasicBlock>(getVal(Type::LabelTy, $9)),
1664 getVal(Type::BoolTy, $3));
1666 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1667 SwitchInst *S = new SwitchInst(getVal($2, $3),
1668 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
1671 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
1674 S->addCase(I->first, I->second);
1677 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
1678 SwitchInst *S = new SwitchInst(getVal($2, $3),
1679 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
1682 | INVOKE TypesV ValueRef '(' ValueRefListE ')' TO ResolvedVal
1683 UNWIND ResolvedVal {
1684 const PointerType *PFTy;
1685 const FunctionType *Ty;
1687 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1688 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
1689 // Pull out the types of all of the arguments...
1690 std::vector<const Type*> ParamTypes;
1692 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1694 ParamTypes.push_back((*I)->getType());
1697 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1698 if (isVarArg) ParamTypes.pop_back();
1700 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
1701 PFTy = PointerType::get(Ty);
1704 Value *V = getVal(PFTy, $3); // Get the function we're calling...
1706 BasicBlock *Normal = dyn_cast<BasicBlock>($8);
1707 BasicBlock *Except = dyn_cast<BasicBlock>($10);
1709 if (Normal == 0 || Except == 0)
1710 ThrowException("Invoke instruction without label destinations!");
1712 // Create the call node...
1713 if (!$5) { // Has no arguments?
1714 $$ = new InvokeInst(V, Normal, Except, std::vector<Value*>());
1715 } else { // Has arguments?
1716 // Loop through FunctionType's arguments and ensure they are specified
1719 FunctionType::param_iterator I = Ty->param_begin();
1720 FunctionType::param_iterator E = Ty->param_end();
1721 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
1723 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1724 if ((*ArgI)->getType() != *I)
1725 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
1726 (*I)->getDescription() + "'!");
1728 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1729 ThrowException("Invalid number of parameters detected!");
1731 $$ = new InvokeInst(V, Normal, Except, *$5);
1737 $$ = new UnwindInst();
1742 JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1744 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
1746 ThrowException("May only switch on a constant pool value!");
1748 $$->push_back(std::make_pair(V, cast<BasicBlock>(getVal($5, $6))));
1750 | IntType ConstValueRef ',' LABEL ValueRef {
1751 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
1752 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
1755 ThrowException("May only switch on a constant pool value!");
1757 $$->push_back(std::make_pair(V, cast<BasicBlock>(getVal($4, $5))));
1760 Inst : OptAssign InstVal {
1761 // Is this definition named?? if so, assign the name...
1762 setValueName($2, $1);
1767 PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
1768 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
1769 $$->push_back(std::make_pair(getVal(*$1, $3),
1770 cast<BasicBlock>(getVal(Type::LabelTy, $5))));
1773 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1775 $1->push_back(std::make_pair(getVal($1->front().first->getType(), $4),
1776 cast<BasicBlock>(getVal(Type::LabelTy, $6))));
1780 ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
1781 $$ = new std::vector<Value*>();
1784 | ValueRefList ',' ResolvedVal {
1789 // ValueRefListE - Just like ValueRefList, except that it may also be empty!
1790 ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
1792 InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
1793 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint())
1794 ThrowException("Arithmetic operator requires integer or FP operands!");
1795 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1797 ThrowException("binary operator returned null!");
1800 | LogicalOps Types ValueRef ',' ValueRef {
1801 if (!(*$2)->isIntegral())
1802 ThrowException("Logical operator requires integral operands!");
1803 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1805 ThrowException("binary operator returned null!");
1808 | SetCondOps Types ValueRef ',' ValueRef {
1809 $$ = new SetCondInst($1, getVal(*$2, $3), getVal(*$2, $5));
1811 ThrowException("binary operator returned null!");
1815 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
1816 << " Replacing with 'xor'.\n";
1818 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
1820 ThrowException("Expected integral type for not instruction!");
1822 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
1824 ThrowException("Could not create a xor instruction!");
1826 | ShiftOps ResolvedVal ',' ResolvedVal {
1827 if ($4->getType() != Type::UByteTy)
1828 ThrowException("Shift amount must be ubyte!");
1829 if (!$2->getType()->isInteger())
1830 ThrowException("Shift constant expression requires integer operand!");
1831 $$ = new ShiftInst($1, $2, $4);
1833 | CAST ResolvedVal TO Types {
1834 if (!$4->get()->isFirstClassType())
1835 ThrowException("cast instruction to a non-primitive type: '" +
1836 $4->get()->getDescription() + "'!");
1837 $$ = new CastInst($2, *$4);
1840 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1841 if ($2->getType() != Type::BoolTy)
1842 ThrowException("select condition must be boolean!");
1843 if ($4->getType() != $6->getType())
1844 ThrowException("select value types should match!");
1845 $$ = new SelectInst($2, $4, $6);
1847 | VA_ARG ResolvedVal ',' Types {
1848 // FIXME: This is emulation code for an obsolete syntax. This should be
1849 // removed at some point.
1850 if (!ObsoleteVarArgs) {
1851 std::cerr << "WARNING: this file uses obsolete features. "
1852 << "Assemble and disassemble to update it.\n";
1853 ObsoleteVarArgs = true;
1856 // First, load the valist...
1857 Instruction *CurVAList = new LoadInst($2, "");
1858 CurBB->getInstList().push_back(CurVAList);
1860 // Emit the vaarg instruction.
1861 $$ = new VAArgInst(CurVAList, *$4);
1863 // Now we must advance the pointer and update it in memory.
1864 Instruction *TheVANext = new VANextInst(CurVAList, *$4);
1865 CurBB->getInstList().push_back(TheVANext);
1867 CurBB->getInstList().push_back(new StoreInst(TheVANext, $2));
1870 | VAARG ResolvedVal ',' Types {
1871 $$ = new VAArgInst($2, *$4);
1874 | VANEXT ResolvedVal ',' Types {
1875 $$ = new VANextInst($2, *$4);
1879 const Type *Ty = $2->front().first->getType();
1880 if (!Ty->isFirstClassType())
1881 ThrowException("PHI node operands must be of first class type!");
1882 $$ = new PHINode(Ty);
1883 $$->op_reserve($2->size()*2);
1884 while ($2->begin() != $2->end()) {
1885 if ($2->front().first->getType() != Ty)
1886 ThrowException("All elements of a PHI node must be of the same type!");
1887 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
1890 delete $2; // Free the list...
1892 | CALL TypesV ValueRef '(' ValueRefListE ')' {
1893 const PointerType *PFTy;
1894 const FunctionType *Ty;
1896 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1897 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
1898 // Pull out the types of all of the arguments...
1899 std::vector<const Type*> ParamTypes;
1901 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1903 ParamTypes.push_back((*I)->getType());
1906 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1907 if (isVarArg) ParamTypes.pop_back();
1909 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
1910 PFTy = PointerType::get(Ty);
1913 Value *V = getVal(PFTy, $3); // Get the function we're calling...
1915 // Create the call node...
1916 if (!$5) { // Has no arguments?
1917 // Make sure no arguments is a good thing!
1918 if (Ty->getNumParams() != 0)
1919 ThrowException("No arguments passed to a function that "
1920 "expects arguments!");
1922 $$ = new CallInst(V, std::vector<Value*>());
1923 } else { // Has arguments?
1924 // Loop through FunctionType's arguments and ensure they are specified
1927 FunctionType::param_iterator I = Ty->param_begin();
1928 FunctionType::param_iterator E = Ty->param_end();
1929 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
1931 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1932 if ((*ArgI)->getType() != *I)
1933 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
1934 (*I)->getDescription() + "'!");
1936 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1937 ThrowException("Invalid number of parameters detected!");
1939 $$ = new CallInst(V, *$5);
1949 // IndexList - List of indices for GEP based instructions...
1950 IndexList : ',' ValueRefList {
1953 $$ = new std::vector<Value*>();
1956 OptVolatile : VOLATILE {
1964 MemoryInst : MALLOC Types {
1965 $$ = new MallocInst(*$2);
1968 | MALLOC Types ',' UINT ValueRef {
1969 $$ = new MallocInst(*$2, getVal($4, $5));
1973 $$ = new AllocaInst(*$2);
1976 | ALLOCA Types ',' UINT ValueRef {
1977 $$ = new AllocaInst(*$2, getVal($4, $5));
1980 | FREE ResolvedVal {
1981 if (!isa<PointerType>($2->getType()))
1982 ThrowException("Trying to free nonpointer type " +
1983 $2->getType()->getDescription() + "!");
1984 $$ = new FreeInst($2);
1987 | OptVolatile LOAD Types ValueRef {
1988 if (!isa<PointerType>($3->get()))
1989 ThrowException("Can't load from nonpointer type: " +
1990 (*$3)->getDescription());
1991 $$ = new LoadInst(getVal(*$3, $4), "", $1);
1994 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
1995 const PointerType *PT = dyn_cast<PointerType>($5->get());
1997 ThrowException("Can't store to a nonpointer type: " +
1998 (*$5)->getDescription());
1999 const Type *ElTy = PT->getElementType();
2000 if (ElTy != $3->getType())
2001 ThrowException("Can't store '" + $3->getType()->getDescription() +
2002 "' into space of type '" + ElTy->getDescription() + "'!");
2004 $$ = new StoreInst($3, getVal(*$5, $6), $1);
2007 | GETELEMENTPTR Types ValueRef IndexList {
2008 if (!isa<PointerType>($2->get()))
2009 ThrowException("getelementptr insn requires pointer operand!");
2011 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
2012 // indices to uint struct indices for compatibility.
2013 generic_gep_type_iterator<std::vector<Value*>::iterator>
2014 GTI = gep_type_begin($2->get(), $4->begin(), $4->end()),
2015 GTE = gep_type_end($2->get(), $4->begin(), $4->end());
2016 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
2017 if (isa<StructType>(*GTI)) // Only change struct indices
2018 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
2019 if (CUI->getType() == Type::UByteTy)
2020 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
2022 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
2023 ThrowException("Invalid getelementptr indices for type '" +
2024 (*$2)->getDescription()+ "'!");
2025 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
2026 delete $2; delete $4;
2031 int yyerror(const char *ErrorMsg) {
2033 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
2034 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
2035 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
2036 if (yychar == YYEMPTY || yychar == 0)
2037 errMsg += "end-of-file.";
2039 errMsg += "token: '" + std::string(llvmAsmtext, llvmAsmleng) + "'";
2040 ThrowException(errMsg);