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 // Loop over all of the uses of the GlobalValue. The only thing they are
123 // allowed to be is ConstantPointerRef's.
124 assert(OldGV->hasOneUse() && "Only one reference should exist!");
125 User *U = OldGV->use_back(); // Must be a ConstantPointerRef...
126 ConstantPointerRef *CPR = cast<ConstantPointerRef>(U);
128 // Change the const pool reference to point to the real global variable
129 // now. This should drop a use from the OldGV.
130 CPR->replaceUsesOfWithOnConstant(OldGV, GV);
131 assert(OldGV->use_empty() && "All uses should be gone now!");
133 // Remove OldGV from the module...
134 if (GlobalVariable *GVar = dyn_cast<GlobalVariable>(OldGV))
135 CurrentModule->getGlobalList().erase(GVar);
137 CurrentModule->getFunctionList().erase(cast<Function>(OldGV));
139 // Remove the map entry for the global now that it has been created...
146 static struct PerFunctionInfo {
147 Function *CurrentFunction; // Pointer to current function being created
149 std::map<const Type*, ValueList> Values; // Keep track of #'d definitions
150 std::map<const Type*, ValueList> LateResolveValues;
151 std::vector<PATypeHolder> Types;
152 std::map<ValID, PATypeHolder> LateResolveTypes;
153 SymbolTable LocalSymtab;
154 bool isDeclare; // Is this function a forward declararation?
156 inline PerFunctionInfo() {
161 inline void FunctionStart(Function *M) {
165 void FunctionDone() {
166 // If we could not resolve some blocks at parsing time (forward branches)
167 // resolve the branches now...
168 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
170 // Make sure to resolve any constant expr references that might exist within
171 // the function we just declared itself.
173 if (CurrentFunction->hasName()) {
174 FID = ValID::create((char*)CurrentFunction->getName().c_str());
176 // Figure out which slot number if is...
177 ValueList &List = CurModule.Values[CurrentFunction->getType()];
178 for (unsigned i = 0; ; ++i) {
179 assert(i < List.size() && "Function not found!");
180 if (List[i] == CurrentFunction) {
181 FID = ValID::create((int)i);
186 CurModule.DeclareNewGlobalValue(CurrentFunction, FID);
188 Values.clear(); // Clear out function local definitions
189 Types.clear(); // Clear out function local types
190 LocalSymtab.clear(); // Clear out function local symbol table
194 } CurFun; // Info for the current function...
196 static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
199 //===----------------------------------------------------------------------===//
200 // Code to handle definitions of all the types
201 //===----------------------------------------------------------------------===//
203 static int InsertValue(Value *V,
204 std::map<const Type*,ValueList> &ValueTab = CurFun.Values) {
205 if (V->hasName()) return -1; // Is this a numbered definition?
207 // Yes, insert the value into the value table...
208 ValueList &List = ValueTab[V->getType()];
210 return List.size()-1;
213 // TODO: FIXME when Type are not const
214 static void InsertType(const Type *Ty, std::vector<PATypeHolder> &Types) {
218 static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
220 case ValID::NumberVal: { // Is it a numbered definition?
221 unsigned Num = (unsigned)D.Num;
223 // Module constants occupy the lowest numbered slots...
224 if (Num < CurModule.Types.size())
225 return CurModule.Types[Num];
227 Num -= CurModule.Types.size();
229 // Check that the number is within bounds...
230 if (Num <= CurFun.Types.size())
231 return CurFun.Types[Num];
234 case ValID::NameVal: { // Is it a named definition?
235 std::string Name(D.Name);
236 SymbolTable *SymTab = 0;
238 if (inFunctionScope()) {
239 SymTab = &CurFun.CurrentFunction->getSymbolTable();
240 N = SymTab->lookupType(Name);
244 // Symbol table doesn't automatically chain yet... because the function
245 // hasn't been added to the module...
247 SymTab = &CurModule.CurrentModule->getSymbolTable();
248 N = SymTab->lookupType(Name);
252 D.destroy(); // Free old strdup'd memory...
253 return cast<Type>(N);
256 ThrowException("Internal parser error: Invalid symbol type reference!");
259 // If we reached here, we referenced either a symbol that we don't know about
260 // or an id number that hasn't been read yet. We may be referencing something
261 // forward, so just create an entry to be resolved later and get to it...
263 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
265 std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
266 CurFun.LateResolveTypes : CurModule.LateResolveTypes;
268 std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
269 if (I != LateResolver.end()) {
273 Type *Typ = OpaqueType::get();
274 LateResolver.insert(std::make_pair(D, Typ));
278 static Value *lookupInSymbolTable(const Type *Ty, const std::string &Name) {
279 SymbolTable &SymTab =
280 inFunctionScope() ? CurFun.CurrentFunction->getSymbolTable() :
281 CurModule.CurrentModule->getSymbolTable();
282 return SymTab.lookup(Ty, Name);
285 // getValNonImprovising - Look up the value specified by the provided type and
286 // the provided ValID. If the value exists and has already been defined, return
287 // it. Otherwise return null.
289 static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
290 if (isa<FunctionType>(Ty))
291 ThrowException("Functions are not values and "
292 "must be referenced as pointers");
295 case ValID::NumberVal: { // Is it a numbered definition?
296 unsigned Num = (unsigned)D.Num;
298 // Module constants occupy the lowest numbered slots...
299 std::map<const Type*,ValueList>::iterator VI = CurModule.Values.find(Ty);
300 if (VI != CurModule.Values.end()) {
301 if (Num < VI->second.size())
302 return VI->second[Num];
303 Num -= VI->second.size();
306 // Make sure that our type is within bounds
307 VI = CurFun.Values.find(Ty);
308 if (VI == CurFun.Values.end()) return 0;
310 // Check that the number is within bounds...
311 if (VI->second.size() <= Num) return 0;
313 return VI->second[Num];
316 case ValID::NameVal: { // Is it a named definition?
317 Value *N = lookupInSymbolTable(Ty, std::string(D.Name));
318 if (N == 0) return 0;
320 D.destroy(); // Free old strdup'd memory...
324 // Check to make sure that "Ty" is an integral type, and that our
325 // value will fit into the specified type...
326 case ValID::ConstSIntVal: // Is it a constant pool reference??
327 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64))
328 ThrowException("Signed integral constant '" +
329 itostr(D.ConstPool64) + "' is invalid for type '" +
330 Ty->getDescription() + "'!");
331 return ConstantSInt::get(Ty, D.ConstPool64);
333 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
334 if (!ConstantUInt::isValueValidForType(Ty, D.UConstPool64)) {
335 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64)) {
336 ThrowException("Integral constant '" + utostr(D.UConstPool64) +
337 "' is invalid or out of range!");
338 } else { // This is really a signed reference. Transmogrify.
339 return ConstantSInt::get(Ty, D.ConstPool64);
342 return ConstantUInt::get(Ty, D.UConstPool64);
345 case ValID::ConstFPVal: // Is it a floating point const pool reference?
346 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
347 ThrowException("FP constant invalid for type!!");
348 return ConstantFP::get(Ty, D.ConstPoolFP);
350 case ValID::ConstNullVal: // Is it a null value?
351 if (!isa<PointerType>(Ty))
352 ThrowException("Cannot create a a non pointer null!");
353 return ConstantPointerNull::get(cast<PointerType>(Ty));
355 case ValID::ConstantVal: // Fully resolved constant?
356 if (D.ConstantValue->getType() != Ty)
357 ThrowException("Constant expression type different from required type!");
358 return D.ConstantValue;
361 assert(0 && "Unhandled case!");
365 assert(0 && "Unhandled case!");
370 // getVal - This function is identical to getValNonImprovising, except that if a
371 // value is not already defined, it "improvises" by creating a placeholder var
372 // that looks and acts just like the requested variable. When the value is
373 // defined later, all uses of the placeholder variable are replaced with the
376 static Value *getVal(const Type *Ty, const ValID &D) {
378 // See if the value has already been defined...
379 Value *V = getValNonImprovising(Ty, D);
382 // If we reached here, we referenced either a symbol that we don't know about
383 // or an id number that hasn't been read yet. We may be referencing something
384 // forward, so just create an entry to be resolved later and get to it...
387 switch (Ty->getTypeID()) {
388 case Type::LabelTyID: d = new BBPlaceHolder(Ty, D); break;
389 default: d = new ValuePlaceHolder(Ty, D); break;
392 assert(d != 0 && "How did we not make something?");
393 if (inFunctionScope())
394 InsertValue(d, CurFun.LateResolveValues);
396 InsertValue(d, CurModule.LateResolveValues);
401 //===----------------------------------------------------------------------===//
402 // Code to handle forward references in instructions
403 //===----------------------------------------------------------------------===//
405 // This code handles the late binding needed with statements that reference
406 // values not defined yet... for example, a forward branch, or the PHI node for
409 // This keeps a table (CurFun.LateResolveValues) of all such forward references
410 // and back patchs after we are done.
413 // ResolveDefinitions - If we could not resolve some defs at parsing
414 // time (forward branches, phi functions for loops, etc...) resolve the
417 static void ResolveDefinitions(std::map<const Type*,ValueList> &LateResolvers,
418 std::map<const Type*,ValueList> *FutureLateResolvers) {
419 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
420 for (std::map<const Type*,ValueList>::iterator LRI = LateResolvers.begin(),
421 E = LateResolvers.end(); LRI != E; ++LRI) {
422 ValueList &List = LRI->second;
423 while (!List.empty()) {
424 Value *V = List.back();
426 ValID &DID = getValIDFromPlaceHolder(V);
428 Value *TheRealValue = getValNonImprovising(LRI->first, DID);
430 V->replaceAllUsesWith(TheRealValue);
432 } else if (FutureLateResolvers) {
433 // Functions have their unresolved items forwarded to the module late
435 InsertValue(V, *FutureLateResolvers);
437 if (DID.Type == ValID::NameVal)
438 ThrowException("Reference to an invalid definition: '" +DID.getName()+
439 "' of type '" + V->getType()->getDescription() + "'",
440 getLineNumFromPlaceHolder(V));
442 ThrowException("Reference to an invalid definition: #" +
443 itostr(DID.Num) + " of type '" +
444 V->getType()->getDescription() + "'",
445 getLineNumFromPlaceHolder(V));
450 LateResolvers.clear();
453 // ResolveTypeTo - A brand new type was just declared. This means that (if
454 // name is not null) things referencing Name can be resolved. Otherwise, things
455 // refering to the number can be resolved. Do this now.
457 static void ResolveTypeTo(char *Name, const Type *ToTy) {
458 std::vector<PATypeHolder> &Types = inFunctionScope() ?
459 CurFun.Types : CurModule.Types;
462 if (Name) D = ValID::create(Name);
463 else D = ValID::create((int)Types.size());
465 std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
466 CurFun.LateResolveTypes : CurModule.LateResolveTypes;
468 std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
469 if (I != LateResolver.end()) {
470 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
471 LateResolver.erase(I);
475 // ResolveTypes - At this point, all types should be resolved. Any that aren't
478 static void ResolveTypes(std::map<ValID, PATypeHolder> &LateResolveTypes) {
479 if (!LateResolveTypes.empty()) {
480 const ValID &DID = LateResolveTypes.begin()->first;
482 if (DID.Type == ValID::NameVal)
483 ThrowException("Reference to an invalid type: '" +DID.getName() + "'");
485 ThrowException("Reference to an invalid type: #" + itostr(DID.Num));
490 // setValueName - Set the specified value to the name given. The name may be
491 // null potentially, in which case this is a noop. The string passed in is
492 // assumed to be a malloc'd string buffer, and is freed by this function.
494 // This function returns true if the value has already been defined, but is
495 // allowed to be redefined in the specified context. If the name is a new name
496 // for the typeplane, false is returned.
498 static bool setValueName(Value *V, char *NameStr) {
499 if (NameStr == 0) return false;
501 std::string Name(NameStr); // Copy string
502 free(NameStr); // Free old string
504 if (V->getType() == Type::VoidTy)
505 ThrowException("Can't assign name '" + Name +
506 "' to a null valued instruction!");
508 SymbolTable &ST = inFunctionScope() ?
509 CurFun.CurrentFunction->getSymbolTable() :
510 CurModule.CurrentModule->getSymbolTable();
512 Value *Existing = ST.lookup(V->getType(), Name);
514 if (Existing) { // Inserting a name that is already defined???
515 // We are a simple redefinition of a value, check to see if it
516 // is defined the same as the old one...
517 if (const Constant *C = dyn_cast<Constant>(Existing)) {
518 if (C == V) return true; // Constants are equal to themselves
519 } else if (GlobalVariable *EGV = dyn_cast<GlobalVariable>(Existing)) {
520 // We are allowed to redefine a global variable in two circumstances:
521 // 1. If at least one of the globals is uninitialized or
522 // 2. If both initializers have the same value.
524 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
525 if (!EGV->hasInitializer() || !GV->hasInitializer() ||
526 EGV->getInitializer() == GV->getInitializer()) {
528 // Make sure the existing global version gets the initializer! Make
529 // sure that it also gets marked const if the new version is.
530 if (GV->hasInitializer() && !EGV->hasInitializer())
531 EGV->setInitializer(GV->getInitializer());
532 if (GV->isConstant())
533 EGV->setConstant(true);
534 EGV->setLinkage(GV->getLinkage());
536 delete GV; // Destroy the duplicate!
537 return true; // They are equivalent!
542 ThrowException("Redefinition of value named '" + Name + "' in the '" +
543 V->getType()->getDescription() + "' type plane!");
547 V->setName(Name, &ST);
549 // If we're in function scope
550 if (inFunctionScope()) {
551 // Look up the symbol in the function's local symboltable
552 Existing = CurFun.LocalSymtab.lookup(V->getType(),Name);
554 // If it already exists
557 ThrowException("Redefinition of value named '" + Name + "' in the '" +
558 V->getType()->getDescription() + "' type plane!");
560 // otherwise, since it doesn't exist
563 CurFun.LocalSymtab.insert(V);
569 // setTypeName - Set the specified type to the name given. The name may be
570 // null potentially, in which case this is a noop. The string passed in is
571 // assumed to be a malloc'd string buffer, and is freed by this function.
573 // This function returns true if the type has already been defined, but is
574 // allowed to be redefined in the specified context. If the name is a new name
575 // for the type plane, it is inserted and false is returned.
576 static bool setTypeName(Type *T, char *NameStr) {
577 if (NameStr == 0) return false;
579 std::string Name(NameStr); // Copy string
580 free(NameStr); // Free old string
582 // We don't allow assigning names to void type
583 if (T == Type::VoidTy)
584 ThrowException("Can't assign name '" + Name + "' to the null type!");
586 SymbolTable &ST = inFunctionScope() ?
587 CurFun.CurrentFunction->getSymbolTable() :
588 CurModule.CurrentModule->getSymbolTable();
590 Type *Existing = ST.lookupType(Name);
592 if (Existing) { // Inserting a name that is already defined???
593 // There is only one case where this is allowed: when we are refining an
594 // opaque type. In this case, Existing will be an opaque type.
595 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
596 // We ARE replacing an opaque type!
597 ((OpaqueType*)OpTy)->refineAbstractTypeTo(T);
601 // Otherwise, this is an attempt to redefine a type. That's okay if
602 // the redefinition is identical to the original. This will be so if
603 // Existing and T point to the same Type object. In this one case we
604 // allow the equivalent redefinition.
605 if (Existing == T) return true; // Yes, it's equal.
607 // Any other kind of (non-equivalent) redefinition is an error.
608 ThrowException("Redefinition of type named '" + Name + "' in the '" +
609 T->getDescription() + "' type plane!");
612 // Okay, its a newly named type. Set its name.
613 if (!Name.empty()) ST.insert(Name, T);
615 // If we're in function scope
616 if (inFunctionScope()) {
617 // Look up the symbol in the function's local symboltable
618 Existing = CurFun.LocalSymtab.lookupType(Name);
620 // If it already exists
623 ThrowException("Redefinition of type named '" + Name + "' in the '" +
624 T->getDescription() + "' type plane in function scope!");
626 // otherwise, since it doesn't exist
629 CurFun.LocalSymtab.insert(Name,T);
635 //===----------------------------------------------------------------------===//
636 // Code for handling upreferences in type names...
639 // TypeContains - Returns true if Ty directly contains E in it.
641 static bool TypeContains(const Type *Ty, const Type *E) {
642 return find(Ty->subtype_begin(), Ty->subtype_end(), E) != Ty->subtype_end();
647 // NestingLevel - The number of nesting levels that need to be popped before
648 // this type is resolved.
649 unsigned NestingLevel;
651 // LastContainedTy - This is the type at the current binding level for the
652 // type. Every time we reduce the nesting level, this gets updated.
653 const Type *LastContainedTy;
655 // UpRefTy - This is the actual opaque type that the upreference is
659 UpRefRecord(unsigned NL, OpaqueType *URTy)
660 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
664 // UpRefs - A list of the outstanding upreferences that need to be resolved.
665 static std::vector<UpRefRecord> UpRefs;
667 /// HandleUpRefs - Every time we finish a new layer of types, this function is
668 /// called. It loops through the UpRefs vector, which is a list of the
669 /// currently active types. For each type, if the up reference is contained in
670 /// the newly completed type, we decrement the level count. When the level
671 /// count reaches zero, the upreferenced type is the type that is passed in:
672 /// thus we can complete the cycle.
674 static PATypeHolder HandleUpRefs(const Type *ty) {
675 if (!ty->isAbstract()) return ty;
677 UR_OUT("Type '" << Ty->getDescription() <<
678 "' newly formed. Resolving upreferences.\n" <<
679 UpRefs.size() << " upreferences active!\n");
681 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
682 // to zero), we resolve them all together before we resolve them to Ty. At
683 // the end of the loop, if there is anything to resolve to Ty, it will be in
685 OpaqueType *TypeToResolve = 0;
687 for (unsigned i = 0; i != UpRefs.size(); ++i) {
688 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
689 << UpRefs[i].second->getDescription() << ") = "
690 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
691 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
692 // Decrement level of upreference
693 unsigned Level = --UpRefs[i].NestingLevel;
694 UpRefs[i].LastContainedTy = Ty;
695 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
696 if (Level == 0) { // Upreference should be resolved!
697 if (!TypeToResolve) {
698 TypeToResolve = UpRefs[i].UpRefTy;
700 UR_OUT(" * Resolving upreference for "
701 << UpRefs[i].second->getDescription() << "\n";
702 std::string OldName = UpRefs[i].UpRefTy->getDescription());
703 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
704 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
705 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
707 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
708 --i; // Do not skip the next element...
714 UR_OUT(" * Resolving upreference for "
715 << UpRefs[i].second->getDescription() << "\n";
716 std::string OldName = TypeToResolve->getDescription());
717 TypeToResolve->refineAbstractTypeTo(Ty);
724 //===----------------------------------------------------------------------===//
725 // RunVMAsmParser - Define an interface to this parser
726 //===----------------------------------------------------------------------===//
728 Module *RunVMAsmParser(const std::string &Filename, FILE *F) {
730 CurFilename = Filename;
731 llvmAsmlineno = 1; // Reset the current line number...
732 ObsoleteVarArgs = false;
734 // Allocate a new module to read
735 CurModule.CurrentModule = new Module(Filename);
738 yyparse(); // Parse the file.
740 // Clear the symbol table so it doesn't complain when it
742 CurFun.LocalSymtab.clear();
746 Module *Result = ParserResult;
748 // Check to see if they called va_start but not va_arg..
749 if (!ObsoleteVarArgs)
750 if (Function *F = Result->getNamedFunction("llvm.va_start"))
751 if (F->asize() == 1) {
752 std::cerr << "WARNING: this file uses obsolete features. "
753 << "Assemble and disassemble to update it.\n";
754 ObsoleteVarArgs = true;
758 if (ObsoleteVarArgs) {
759 // If the user is making use of obsolete varargs intrinsics, adjust them for
761 if (Function *F = Result->getNamedFunction("llvm.va_start")) {
762 assert(F->asize() == 1 && "Obsolete va_start takes 1 argument!");
764 const Type *RetTy = F->getFunctionType()->getParamType(0);
765 RetTy = cast<PointerType>(RetTy)->getElementType();
766 Function *NF = Result->getOrInsertFunction("llvm.va_start", RetTy, 0);
768 while (!F->use_empty()) {
769 CallInst *CI = cast<CallInst>(F->use_back());
770 Value *V = new CallInst(NF, "", CI);
771 new StoreInst(V, CI->getOperand(1), CI);
772 CI->getParent()->getInstList().erase(CI);
774 Result->getFunctionList().erase(F);
777 if (Function *F = Result->getNamedFunction("llvm.va_end")) {
778 assert(F->asize() == 1 && "Obsolete va_end takes 1 argument!");
779 const Type *ArgTy = F->getFunctionType()->getParamType(0);
780 ArgTy = cast<PointerType>(ArgTy)->getElementType();
781 Function *NF = Result->getOrInsertFunction("llvm.va_end", Type::VoidTy,
784 while (!F->use_empty()) {
785 CallInst *CI = cast<CallInst>(F->use_back());
786 Value *V = new LoadInst(CI->getOperand(1), "", CI);
787 new CallInst(NF, V, "", CI);
788 CI->getParent()->getInstList().erase(CI);
790 Result->getFunctionList().erase(F);
793 if (Function *F = Result->getNamedFunction("llvm.va_copy")) {
794 assert(F->asize() == 2 && "Obsolete va_copy takes 2 argument!");
795 const Type *ArgTy = F->getFunctionType()->getParamType(0);
796 ArgTy = cast<PointerType>(ArgTy)->getElementType();
797 Function *NF = Result->getOrInsertFunction("llvm.va_copy", ArgTy,
800 while (!F->use_empty()) {
801 CallInst *CI = cast<CallInst>(F->use_back());
802 Value *V = new CallInst(NF, CI->getOperand(2), "", CI);
803 new StoreInst(V, CI->getOperand(1), CI);
804 CI->getParent()->getInstList().erase(CI);
806 Result->getFunctionList().erase(F);
810 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
816 } // End llvm namespace
818 using namespace llvm;
823 llvm::Module *ModuleVal;
824 llvm::Function *FunctionVal;
825 std::pair<llvm::PATypeHolder*, char*> *ArgVal;
826 llvm::BasicBlock *BasicBlockVal;
827 llvm::TerminatorInst *TermInstVal;
828 llvm::Instruction *InstVal;
829 llvm::Constant *ConstVal;
831 const llvm::Type *PrimType;
832 llvm::PATypeHolder *TypeVal;
833 llvm::Value *ValueVal;
835 std::vector<std::pair<llvm::PATypeHolder*,char*> > *ArgList;
836 std::vector<llvm::Value*> *ValueList;
837 std::list<llvm::PATypeHolder> *TypeList;
838 std::list<std::pair<llvm::Value*,
839 llvm::BasicBlock*> > *PHIList; // Represent the RHS of PHI node
840 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
841 std::vector<llvm::Constant*> *ConstVector;
843 llvm::GlobalValue::LinkageTypes Linkage;
851 char *StrVal; // This memory is strdup'd!
852 llvm::ValID ValIDVal; // strdup'd memory maybe!
854 llvm::Instruction::BinaryOps BinaryOpVal;
855 llvm::Instruction::TermOps TermOpVal;
856 llvm::Instruction::MemoryOps MemOpVal;
857 llvm::Instruction::OtherOps OtherOpVal;
858 llvm::Module::Endianness Endianness;
861 %type <ModuleVal> Module FunctionList
862 %type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
863 %type <BasicBlockVal> BasicBlock InstructionList
864 %type <TermInstVal> BBTerminatorInst
865 %type <InstVal> Inst InstVal MemoryInst
866 %type <ConstVal> ConstVal ConstExpr
867 %type <ConstVector> ConstVector
868 %type <ArgList> ArgList ArgListH
869 %type <ArgVal> ArgVal
870 %type <PHIList> PHIList
871 %type <ValueList> ValueRefList ValueRefListE // For call param lists
872 %type <ValueList> IndexList // For GEP derived indices
873 %type <TypeList> TypeListI ArgTypeListI
874 %type <JumpTable> JumpTable
875 %type <BoolVal> GlobalType // GLOBAL or CONSTANT?
876 %type <BoolVal> OptVolatile // 'volatile' or not
877 %type <Linkage> OptLinkage
878 %type <Endianness> BigOrLittle
880 // ValueRef - Unresolved reference to a definition or BB
881 %type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
882 %type <ValueVal> ResolvedVal // <type> <valref> pair
883 // Tokens and types for handling constant integer values
885 // ESINT64VAL - A negative number within long long range
886 %token <SInt64Val> ESINT64VAL
888 // EUINT64VAL - A positive number within uns. long long range
889 %token <UInt64Val> EUINT64VAL
890 %type <SInt64Val> EINT64VAL
892 %token <SIntVal> SINTVAL // Signed 32 bit ints...
893 %token <UIntVal> UINTVAL // Unsigned 32 bit ints...
894 %type <SIntVal> INTVAL
895 %token <FPVal> FPVAL // Float or Double constant
898 %type <TypeVal> Types TypesV UpRTypes UpRTypesV
899 %type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
900 %token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
901 %token <PrimType> FLOAT DOUBLE TYPE LABEL
903 %token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
904 %type <StrVal> Name OptName OptAssign
907 %token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
908 %token DECLARE GLOBAL CONSTANT VOLATILE
909 %token TO DOTDOTDOT NULL_TOK CONST INTERNAL LINKONCE WEAK APPENDING
910 %token OPAQUE NOT EXTERNAL TARGET ENDIAN POINTERSIZE LITTLE BIG
912 // Basic Block Terminating Operators
913 %token <TermOpVal> RET BR SWITCH INVOKE UNWIND
916 %type <BinaryOpVal> BinaryOps // all the binary operators
917 %type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
918 %token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
919 %token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
921 // Memory Instructions
922 %token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
925 %type <OtherOpVal> ShiftOps
926 %token <OtherOpVal> PHI_TOK CALL CAST SELECT SHL SHR VAARG VANEXT
927 %token VA_ARG // FIXME: OBSOLETE
932 // Handle constant integer size restriction and conversion...
936 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
937 ThrowException("Value too large for type!");
942 EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
943 EINT64VAL : EUINT64VAL {
944 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
945 ThrowException("Value too large for type!");
949 // Operations that are notably excluded from this list include:
950 // RET, BR, & SWITCH because they end basic blocks and are treated specially.
952 ArithmeticOps: ADD | SUB | MUL | DIV | REM;
953 LogicalOps : AND | OR | XOR;
954 SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
955 BinaryOps : ArithmeticOps | LogicalOps | SetCondOps;
957 ShiftOps : SHL | SHR;
959 // These are some types that allow classification if we only want a particular
960 // thing... for example, only a signed, unsigned, or integral type.
961 SIntType : LONG | INT | SHORT | SBYTE;
962 UIntType : ULONG | UINT | USHORT | UBYTE;
963 IntType : SIntType | UIntType;
964 FPType : FLOAT | DOUBLE;
966 // OptAssign - Value producing statements have an optional assignment component
967 OptAssign : Name '=' {
974 OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
975 LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
976 WEAK { $$ = GlobalValue::WeakLinkage; } |
977 APPENDING { $$ = GlobalValue::AppendingLinkage; } |
978 /*empty*/ { $$ = GlobalValue::ExternalLinkage; };
980 //===----------------------------------------------------------------------===//
981 // Types includes all predefined types... except void, because it can only be
982 // used in specific contexts (function returning void for example). To have
983 // access to it, a user must explicitly use TypesV.
986 // TypesV includes all of 'Types', but it also includes the void type.
987 TypesV : Types | VOID { $$ = new PATypeHolder($1); };
988 UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
992 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
997 // Derived types are added later...
999 PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
1000 PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
1002 $$ = new PATypeHolder(OpaqueType::get());
1005 $$ = new PATypeHolder($1);
1007 UpRTypes : SymbolicValueRef { // Named types are also simple types...
1008 $$ = new PATypeHolder(getTypeVal($1));
1011 // Include derived types in the Types production.
1013 UpRTypes : '\\' EUINT64VAL { // Type UpReference
1014 if ($2 > (uint64_t)~0U) ThrowException("Value out of range!");
1015 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
1016 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
1017 $$ = new PATypeHolder(OT);
1018 UR_OUT("New Upreference!\n");
1020 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
1021 std::vector<const Type*> Params;
1022 mapto($3->begin(), $3->end(), std::back_inserter(Params),
1023 std::mem_fun_ref(&PATypeHolder::get));
1024 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1025 if (isVarArg) Params.pop_back();
1027 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
1028 delete $3; // Delete the argument list
1029 delete $1; // Delete the return type handle
1031 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
1032 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
1035 | '{' TypeListI '}' { // Structure type?
1036 std::vector<const Type*> Elements;
1037 mapto($2->begin(), $2->end(), std::back_inserter(Elements),
1038 std::mem_fun_ref(&PATypeHolder::get));
1040 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
1043 | '{' '}' { // Empty structure type?
1044 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
1046 | UpRTypes '*' { // Pointer type?
1047 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
1051 // TypeList - Used for struct declarations and as a basis for function type
1052 // declaration type lists
1054 TypeListI : UpRTypes {
1055 $$ = new std::list<PATypeHolder>();
1056 $$->push_back(*$1); delete $1;
1058 | TypeListI ',' UpRTypes {
1059 ($$=$1)->push_back(*$3); delete $3;
1062 // ArgTypeList - List of types for a function type declaration...
1063 ArgTypeListI : TypeListI
1064 | TypeListI ',' DOTDOTDOT {
1065 ($$=$1)->push_back(Type::VoidTy);
1068 ($$ = new std::list<PATypeHolder>())->push_back(Type::VoidTy);
1071 $$ = new std::list<PATypeHolder>();
1074 // ConstVal - The various declarations that go into the constant pool. This
1075 // production is used ONLY to represent constants that show up AFTER a 'const',
1076 // 'constant' or 'global' token at global scope. Constants that can be inlined
1077 // into other expressions (such as integers and constexprs) are handled by the
1078 // ResolvedVal, ValueRef and ConstValueRef productions.
1080 ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
1081 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
1083 ThrowException("Cannot make array constant with type: '" +
1084 (*$1)->getDescription() + "'!");
1085 const Type *ETy = ATy->getElementType();
1086 int NumElements = ATy->getNumElements();
1088 // Verify that we have the correct size...
1089 if (NumElements != -1 && NumElements != (int)$3->size())
1090 ThrowException("Type mismatch: constant sized array initialized with " +
1091 utostr($3->size()) + " arguments, but has size of " +
1092 itostr(NumElements) + "!");
1094 // Verify all elements are correct type!
1095 for (unsigned i = 0; i < $3->size(); i++) {
1096 if (ETy != (*$3)[i]->getType())
1097 ThrowException("Element #" + utostr(i) + " is not of type '" +
1098 ETy->getDescription() +"' as required!\nIt is of type '"+
1099 (*$3)[i]->getType()->getDescription() + "'.");
1102 $$ = ConstantArray::get(ATy, *$3);
1103 delete $1; delete $3;
1106 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
1108 ThrowException("Cannot make array constant with type: '" +
1109 (*$1)->getDescription() + "'!");
1111 int NumElements = ATy->getNumElements();
1112 if (NumElements != -1 && NumElements != 0)
1113 ThrowException("Type mismatch: constant sized array initialized with 0"
1114 " arguments, but has size of " + itostr(NumElements) +"!");
1115 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
1118 | Types 'c' STRINGCONSTANT {
1119 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
1121 ThrowException("Cannot make array constant with type: '" +
1122 (*$1)->getDescription() + "'!");
1124 int NumElements = ATy->getNumElements();
1125 const Type *ETy = ATy->getElementType();
1126 char *EndStr = UnEscapeLexed($3, true);
1127 if (NumElements != -1 && NumElements != (EndStr-$3))
1128 ThrowException("Can't build string constant of size " +
1129 itostr((int)(EndStr-$3)) +
1130 " when array has size " + itostr(NumElements) + "!");
1131 std::vector<Constant*> Vals;
1132 if (ETy == Type::SByteTy) {
1133 for (char *C = $3; C != EndStr; ++C)
1134 Vals.push_back(ConstantSInt::get(ETy, *C));
1135 } else if (ETy == Type::UByteTy) {
1136 for (char *C = $3; C != EndStr; ++C)
1137 Vals.push_back(ConstantUInt::get(ETy, (unsigned char)*C));
1140 ThrowException("Cannot build string arrays of non byte sized elements!");
1143 $$ = ConstantArray::get(ATy, Vals);
1146 | Types '{' ConstVector '}' {
1147 const StructType *STy = dyn_cast<StructType>($1->get());
1149 ThrowException("Cannot make struct constant with type: '" +
1150 (*$1)->getDescription() + "'!");
1152 if ($3->size() != STy->getNumContainedTypes())
1153 ThrowException("Illegal number of initializers for structure type!");
1155 // Check to ensure that constants are compatible with the type initializer!
1156 for (unsigned i = 0, e = $3->size(); i != e; ++i)
1157 if ((*$3)[i]->getType() != STy->getElementType(i))
1158 ThrowException("Expected type '" +
1159 STy->getElementType(i)->getDescription() +
1160 "' for element #" + utostr(i) +
1161 " of structure initializer!");
1163 $$ = ConstantStruct::get(STy, *$3);
1164 delete $1; delete $3;
1167 const StructType *STy = dyn_cast<StructType>($1->get());
1169 ThrowException("Cannot make struct constant with type: '" +
1170 (*$1)->getDescription() + "'!");
1172 if (STy->getNumContainedTypes() != 0)
1173 ThrowException("Illegal number of initializers for structure type!");
1175 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1179 const PointerType *PTy = dyn_cast<PointerType>($1->get());
1181 ThrowException("Cannot make null pointer constant with type: '" +
1182 (*$1)->getDescription() + "'!");
1184 $$ = ConstantPointerNull::get(PTy);
1187 | Types SymbolicValueRef {
1188 const PointerType *Ty = dyn_cast<PointerType>($1->get());
1190 ThrowException("Global const reference must be a pointer type!");
1192 // ConstExprs can exist in the body of a function, thus creating
1193 // ConstantPointerRefs whenever they refer to a variable. Because we are in
1194 // the context of a function, getValNonImprovising will search the functions
1195 // symbol table instead of the module symbol table for the global symbol,
1196 // which throws things all off. To get around this, we just tell
1197 // getValNonImprovising that we are at global scope here.
1199 Function *SavedCurFn = CurFun.CurrentFunction;
1200 CurFun.CurrentFunction = 0;
1202 Value *V = getValNonImprovising(Ty, $2);
1204 CurFun.CurrentFunction = SavedCurFn;
1206 // If this is an initializer for a constant pointer, which is referencing a
1207 // (currently) undefined variable, create a stub now that shall be replaced
1208 // in the future with the right type of variable.
1211 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1212 const PointerType *PT = cast<PointerType>(Ty);
1214 // First check to see if the forward references value is already created!
1215 PerModuleInfo::GlobalRefsType::iterator I =
1216 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
1218 if (I != CurModule.GlobalRefs.end()) {
1219 V = I->second; // Placeholder already exists, use it...
1222 // Create a placeholder for the global variable reference...
1223 GlobalVariable *GV = new GlobalVariable(PT->getElementType(),
1225 GlobalValue::ExternalLinkage);
1226 // Keep track of the fact that we have a forward ref to recycle it
1227 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1229 // Must temporarily push this value into the module table...
1230 CurModule.CurrentModule->getGlobalList().push_back(GV);
1235 GlobalValue *GV = cast<GlobalValue>(V);
1236 $$ = ConstantPointerRef::get(GV);
1237 delete $1; // Free the type handle
1240 if ($1->get() != $2->getType())
1241 ThrowException("Mismatched types for constant expression!");
1245 | Types ZEROINITIALIZER {
1246 $$ = Constant::getNullValue($1->get());
1250 ConstVal : SIntType EINT64VAL { // integral constants
1251 if (!ConstantSInt::isValueValidForType($1, $2))
1252 ThrowException("Constant value doesn't fit in type!");
1253 $$ = ConstantSInt::get($1, $2);
1255 | UIntType EUINT64VAL { // integral constants
1256 if (!ConstantUInt::isValueValidForType($1, $2))
1257 ThrowException("Constant value doesn't fit in type!");
1258 $$ = ConstantUInt::get($1, $2);
1260 | BOOL TRUETOK { // Boolean constants
1261 $$ = ConstantBool::True;
1263 | BOOL FALSETOK { // Boolean constants
1264 $$ = ConstantBool::False;
1266 | FPType FPVAL { // Float & Double constants
1267 $$ = ConstantFP::get($1, $2);
1271 ConstExpr: CAST '(' ConstVal TO Types ')' {
1272 if (!$3->getType()->isFirstClassType())
1273 ThrowException("cast constant expression from a non-primitive type: '" +
1274 $3->getType()->getDescription() + "'!");
1275 if (!$5->get()->isFirstClassType())
1276 ThrowException("cast constant expression to a non-primitive type: '" +
1277 $5->get()->getDescription() + "'!");
1278 $$ = ConstantExpr::getCast($3, $5->get());
1281 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1282 if (!isa<PointerType>($3->getType()))
1283 ThrowException("GetElementPtr requires a pointer operand!");
1285 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
1286 // indices to uint struct indices for compatibility.
1287 generic_gep_type_iterator<std::vector<Value*>::iterator>
1288 GTI = gep_type_begin($3->getType(), $4->begin(), $4->end()),
1289 GTE = gep_type_end($3->getType(), $4->begin(), $4->end());
1290 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
1291 if (isa<StructType>(*GTI)) // Only change struct indices
1292 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
1293 if (CUI->getType() == Type::UByteTy)
1294 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
1297 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
1299 ThrowException("Index list invalid for constant getelementptr!");
1301 std::vector<Constant*> IdxVec;
1302 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1303 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
1304 IdxVec.push_back(C);
1306 ThrowException("Indices to constant getelementptr must be constants!");
1310 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
1312 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1313 if ($3->getType() != Type::BoolTy)
1314 ThrowException("Select condition must be of boolean type!");
1315 if ($5->getType() != $7->getType())
1316 ThrowException("Select operand types must match!");
1317 $$ = ConstantExpr::getSelect($3, $5, $7);
1319 | BinaryOps '(' ConstVal ',' ConstVal ')' {
1320 if ($3->getType() != $5->getType())
1321 ThrowException("Binary operator types must match!");
1322 $$ = ConstantExpr::get($1, $3, $5);
1324 | ShiftOps '(' ConstVal ',' ConstVal ')' {
1325 if ($5->getType() != Type::UByteTy)
1326 ThrowException("Shift count for shift constant must be unsigned byte!");
1327 if (!$3->getType()->isInteger())
1328 ThrowException("Shift constant expression requires integer operand!");
1329 $$ = ConstantExpr::get($1, $3, $5);
1333 // ConstVector - A list of comma separated constants.
1334 ConstVector : ConstVector ',' ConstVal {
1335 ($$ = $1)->push_back($3);
1338 $$ = new std::vector<Constant*>();
1343 // GlobalType - Match either GLOBAL or CONSTANT for global declarations...
1344 GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
1347 //===----------------------------------------------------------------------===//
1348 // Rules to match Modules
1349 //===----------------------------------------------------------------------===//
1351 // Module rule: Capture the result of parsing the whole file into a result
1354 Module : FunctionList {
1355 $$ = ParserResult = $1;
1356 CurModule.ModuleDone();
1359 // FunctionList - A list of functions, preceeded by a constant pool.
1361 FunctionList : FunctionList Function {
1363 CurFun.FunctionDone();
1365 | FunctionList FunctionProto {
1368 | FunctionList IMPLEMENTATION {
1372 $$ = CurModule.CurrentModule;
1373 // Resolve circular types before we parse the body of the module
1374 ResolveTypes(CurModule.LateResolveTypes);
1377 // ConstPool - Constants with optional names assigned to them.
1378 ConstPool : ConstPool OptAssign CONST ConstVal {
1379 if (!setValueName($4, $2))
1382 | ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
1383 // Eagerly resolve types. This is not an optimization, this is a
1384 // requirement that is due to the fact that we could have this:
1386 // %list = type { %list * }
1387 // %list = type { %list * } ; repeated type decl
1389 // If types are not resolved eagerly, then the two types will not be
1390 // determined to be the same type!
1392 ResolveTypeTo($2, $4->get());
1394 // TODO: FIXME when Type are not const
1395 if (!setTypeName(const_cast<Type*>($4->get()), $2)) {
1396 // If this is not a redefinition of a type...
1398 InsertType($4->get(),
1399 inFunctionScope() ? CurFun.Types : CurModule.Types);
1405 | ConstPool FunctionProto { // Function prototypes can be in const pool
1407 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
1408 const Type *Ty = $5->getType();
1409 // Global declarations appear in Constant Pool
1410 Constant *Initializer = $5;
1411 if (Initializer == 0)
1412 ThrowException("Global value initializer is not a constant!");
1414 GlobalVariable *GV = new GlobalVariable(Ty, $4, $3, Initializer);
1415 if (!setValueName(GV, $2)) { // If not redefining...
1416 CurModule.CurrentModule->getGlobalList().push_back(GV);
1417 int Slot = InsertValue(GV, CurModule.Values);
1420 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1422 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1423 (char*)GV->getName().c_str()));
1427 | ConstPool OptAssign EXTERNAL GlobalType Types {
1428 const Type *Ty = *$5;
1429 // Global declarations appear in Constant Pool
1430 GlobalVariable *GV = new GlobalVariable(Ty,$4,GlobalValue::ExternalLinkage);
1431 if (!setValueName(GV, $2)) { // If not redefining...
1432 CurModule.CurrentModule->getGlobalList().push_back(GV);
1433 int Slot = InsertValue(GV, CurModule.Values);
1436 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1438 assert(GV->hasName() && "Not named and not numbered!?");
1439 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1440 (char*)GV->getName().c_str()));
1445 | ConstPool TARGET TargetDefinition {
1447 | /* empty: end of list */ {
1452 BigOrLittle : BIG { $$ = Module::BigEndian; };
1453 BigOrLittle : LITTLE { $$ = Module::LittleEndian; };
1455 TargetDefinition : ENDIAN '=' BigOrLittle {
1456 CurModule.CurrentModule->setEndianness($3);
1458 | POINTERSIZE '=' EUINT64VAL {
1460 CurModule.CurrentModule->setPointerSize(Module::Pointer32);
1462 CurModule.CurrentModule->setPointerSize(Module::Pointer64);
1464 ThrowException("Invalid pointer size: '" + utostr($3) + "'!");
1468 //===----------------------------------------------------------------------===//
1469 // Rules to match Function Headers
1470 //===----------------------------------------------------------------------===//
1472 Name : VAR_ID | STRINGCONSTANT;
1473 OptName : Name | /*empty*/ { $$ = 0; };
1475 ArgVal : Types OptName {
1476 if (*$1 == Type::VoidTy)
1477 ThrowException("void typed arguments are invalid!");
1478 $$ = new std::pair<PATypeHolder*, char*>($1, $2);
1481 ArgListH : ArgListH ',' ArgVal {
1487 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1492 ArgList : ArgListH {
1495 | ArgListH ',' DOTDOTDOT {
1497 $$->push_back(std::pair<PATypeHolder*,
1498 char*>(new PATypeHolder(Type::VoidTy), 0));
1501 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1502 $$->push_back(std::make_pair(new PATypeHolder(Type::VoidTy), (char*)0));
1508 FunctionHeaderH : TypesV Name '(' ArgList ')' {
1510 std::string FunctionName($2);
1512 if (!(*$1)->isFirstClassType() && *$1 != Type::VoidTy)
1513 ThrowException("LLVM functions cannot return aggregate types!");
1515 std::vector<const Type*> ParamTypeList;
1516 if ($4) { // If there are arguments...
1517 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $4->begin();
1518 I != $4->end(); ++I)
1519 ParamTypeList.push_back(I->first->get());
1522 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1523 if (isVarArg) ParamTypeList.pop_back();
1525 const FunctionType *FT = FunctionType::get(*$1, ParamTypeList, isVarArg);
1526 const PointerType *PFT = PointerType::get(FT);
1530 // Is the function already in symtab?
1531 if ((Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
1532 // Yes it is. If this is the case, either we need to be a forward decl,
1533 // or it needs to be.
1534 if (!CurFun.isDeclare && !Fn->isExternal())
1535 ThrowException("Redefinition of function '" + FunctionName + "'!");
1537 // Make sure to strip off any argument names so we can't get conflicts...
1538 for (Function::aiterator AI = Fn->abegin(), AE = Fn->aend(); AI != AE; ++AI)
1541 } else { // Not already defined?
1542 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName,
1543 CurModule.CurrentModule);
1544 InsertValue(Fn, CurModule.Values);
1545 CurModule.DeclareNewGlobalValue(Fn, ValID::create($2));
1547 free($2); // Free strdup'd memory!
1549 CurFun.FunctionStart(Fn);
1551 // Add all of the arguments we parsed to the function...
1552 if ($4) { // Is null if empty...
1553 if (isVarArg) { // Nuke the last entry
1554 assert($4->back().first->get() == Type::VoidTy && $4->back().second == 0&&
1555 "Not a varargs marker!");
1556 delete $4->back().first;
1557 $4->pop_back(); // Delete the last entry
1559 Function::aiterator ArgIt = Fn->abegin();
1560 for (std::vector<std::pair<PATypeHolder*, char*> >::iterator I =$4->begin();
1561 I != $4->end(); ++I, ++ArgIt) {
1562 delete I->first; // Delete the typeholder...
1564 if (setValueName(ArgIt, I->second)) // Insert arg into symtab...
1565 assert(0 && "No arg redef allowed!");
1570 delete $4; // We're now done with the argument list
1574 BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1576 FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
1577 $$ = CurFun.CurrentFunction;
1579 // Make sure that we keep track of the linkage type even if there was a
1580 // previous "declare".
1583 // Resolve circular types before we parse the body of the function.
1584 ResolveTypes(CurFun.LateResolveTypes);
1587 END : ENDTOK | '}'; // Allow end of '}' to end a function
1589 Function : BasicBlockList END {
1593 FunctionProto : DECLARE { CurFun.isDeclare = true; } FunctionHeaderH {
1594 $$ = CurFun.CurrentFunction;
1595 CurFun.FunctionDone();
1598 //===----------------------------------------------------------------------===//
1599 // Rules to match Basic Blocks
1600 //===----------------------------------------------------------------------===//
1602 ConstValueRef : ESINT64VAL { // A reference to a direct constant
1603 $$ = ValID::create($1);
1606 $$ = ValID::create($1);
1608 | FPVAL { // Perhaps it's an FP constant?
1609 $$ = ValID::create($1);
1612 $$ = ValID::create(ConstantBool::True);
1615 $$ = ValID::create(ConstantBool::False);
1618 $$ = ValID::createNull();
1621 $$ = ValID::create($1);
1624 // SymbolicValueRef - Reference to one of two ways of symbolically refering to
1627 SymbolicValueRef : INTVAL { // Is it an integer reference...?
1628 $$ = ValID::create($1);
1630 | Name { // Is it a named reference...?
1631 $$ = ValID::create($1);
1634 // ValueRef - A reference to a definition... either constant or symbolic
1635 ValueRef : SymbolicValueRef | ConstValueRef;
1638 // ResolvedVal - a <type> <value> pair. This is used only in cases where the
1639 // type immediately preceeds the value reference, and allows complex constant
1640 // pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
1641 ResolvedVal : Types ValueRef {
1642 $$ = getVal(*$1, $2); delete $1;
1645 BasicBlockList : BasicBlockList BasicBlock {
1646 ($$ = $1)->getBasicBlockList().push_back($2);
1648 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
1649 ($$ = $1)->getBasicBlockList().push_back($2);
1653 // Basic blocks are terminated by branching instructions:
1654 // br, br/cc, switch, ret
1656 BasicBlock : InstructionList OptAssign BBTerminatorInst {
1657 if (setValueName($3, $2)) { assert(0 && "No redefn allowed!"); }
1660 $1->getInstList().push_back($3);
1664 | LABELSTR InstructionList OptAssign BBTerminatorInst {
1665 if (setValueName($4, $3)) { assert(0 && "No redefn allowed!"); }
1668 $2->getInstList().push_back($4);
1669 if (setValueName($2, $1)) { assert(0 && "No label redef allowed!"); }
1675 InstructionList : InstructionList Inst {
1676 $1->getInstList().push_back($2);
1680 $$ = CurBB = new BasicBlock();
1683 BBTerminatorInst : RET ResolvedVal { // Return with a result...
1684 $$ = new ReturnInst($2);
1686 | RET VOID { // Return with no result...
1687 $$ = new ReturnInst();
1689 | BR LABEL ValueRef { // Unconditional Branch...
1690 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $3)));
1691 } // Conditional Branch...
1692 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
1693 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $6)),
1694 cast<BasicBlock>(getVal(Type::LabelTy, $9)),
1695 getVal(Type::BoolTy, $3));
1697 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1698 SwitchInst *S = new SwitchInst(getVal($2, $3),
1699 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
1702 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
1705 S->addCase(I->first, I->second);
1708 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
1709 SwitchInst *S = new SwitchInst(getVal($2, $3),
1710 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
1713 | INVOKE TypesV ValueRef '(' ValueRefListE ')' TO ResolvedVal
1714 UNWIND ResolvedVal {
1715 const PointerType *PFTy;
1716 const FunctionType *Ty;
1718 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1719 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
1720 // Pull out the types of all of the arguments...
1721 std::vector<const Type*> ParamTypes;
1723 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1725 ParamTypes.push_back((*I)->getType());
1728 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1729 if (isVarArg) ParamTypes.pop_back();
1731 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
1732 PFTy = PointerType::get(Ty);
1735 Value *V = getVal(PFTy, $3); // Get the function we're calling...
1737 BasicBlock *Normal = dyn_cast<BasicBlock>($8);
1738 BasicBlock *Except = dyn_cast<BasicBlock>($10);
1740 if (Normal == 0 || Except == 0)
1741 ThrowException("Invoke instruction without label destinations!");
1743 // Create the call node...
1744 if (!$5) { // Has no arguments?
1745 $$ = new InvokeInst(V, Normal, Except, std::vector<Value*>());
1746 } else { // Has arguments?
1747 // Loop through FunctionType's arguments and ensure they are specified
1750 FunctionType::param_iterator I = Ty->param_begin();
1751 FunctionType::param_iterator E = Ty->param_end();
1752 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
1754 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1755 if ((*ArgI)->getType() != *I)
1756 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
1757 (*I)->getDescription() + "'!");
1759 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1760 ThrowException("Invalid number of parameters detected!");
1762 $$ = new InvokeInst(V, Normal, Except, *$5);
1768 $$ = new UnwindInst();
1773 JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1775 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
1777 ThrowException("May only switch on a constant pool value!");
1779 $$->push_back(std::make_pair(V, cast<BasicBlock>(getVal($5, $6))));
1781 | IntType ConstValueRef ',' LABEL ValueRef {
1782 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
1783 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
1786 ThrowException("May only switch on a constant pool value!");
1788 $$->push_back(std::make_pair(V, cast<BasicBlock>(getVal($4, $5))));
1791 Inst : OptAssign InstVal {
1792 // Is this definition named?? if so, assign the name...
1793 if (setValueName($2, $1)) { assert(0 && "No redefin allowed!"); }
1798 PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
1799 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
1800 $$->push_back(std::make_pair(getVal(*$1, $3),
1801 cast<BasicBlock>(getVal(Type::LabelTy, $5))));
1804 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1806 $1->push_back(std::make_pair(getVal($1->front().first->getType(), $4),
1807 cast<BasicBlock>(getVal(Type::LabelTy, $6))));
1811 ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
1812 $$ = new std::vector<Value*>();
1815 | ValueRefList ',' ResolvedVal {
1820 // ValueRefListE - Just like ValueRefList, except that it may also be empty!
1821 ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
1823 InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
1824 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint())
1825 ThrowException("Arithmetic operator requires integer or FP operands!");
1826 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1828 ThrowException("binary operator returned null!");
1831 | LogicalOps Types ValueRef ',' ValueRef {
1832 if (!(*$2)->isIntegral())
1833 ThrowException("Logical operator requires integral operands!");
1834 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1836 ThrowException("binary operator returned null!");
1839 | SetCondOps Types ValueRef ',' ValueRef {
1840 $$ = new SetCondInst($1, getVal(*$2, $3), getVal(*$2, $5));
1842 ThrowException("binary operator returned null!");
1846 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
1847 << " Replacing with 'xor'.\n";
1849 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
1851 ThrowException("Expected integral type for not instruction!");
1853 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
1855 ThrowException("Could not create a xor instruction!");
1857 | ShiftOps ResolvedVal ',' ResolvedVal {
1858 if ($4->getType() != Type::UByteTy)
1859 ThrowException("Shift amount must be ubyte!");
1860 if (!$2->getType()->isInteger())
1861 ThrowException("Shift constant expression requires integer operand!");
1862 $$ = new ShiftInst($1, $2, $4);
1864 | CAST ResolvedVal TO Types {
1865 if (!$4->get()->isFirstClassType())
1866 ThrowException("cast instruction to a non-primitive type: '" +
1867 $4->get()->getDescription() + "'!");
1868 $$ = new CastInst($2, *$4);
1871 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1872 if ($2->getType() != Type::BoolTy)
1873 ThrowException("select condition must be boolean!");
1874 if ($4->getType() != $6->getType())
1875 ThrowException("select value types should match!");
1876 $$ = new SelectInst($2, $4, $6);
1878 | VA_ARG ResolvedVal ',' Types {
1879 // FIXME: This is emulation code for an obsolete syntax. This should be
1880 // removed at some point.
1881 if (!ObsoleteVarArgs) {
1882 std::cerr << "WARNING: this file uses obsolete features. "
1883 << "Assemble and disassemble to update it.\n";
1884 ObsoleteVarArgs = true;
1887 // First, load the valist...
1888 Instruction *CurVAList = new LoadInst($2, "");
1889 CurBB->getInstList().push_back(CurVAList);
1891 // Emit the vaarg instruction.
1892 $$ = new VAArgInst(CurVAList, *$4);
1894 // Now we must advance the pointer and update it in memory.
1895 Instruction *TheVANext = new VANextInst(CurVAList, *$4);
1896 CurBB->getInstList().push_back(TheVANext);
1898 CurBB->getInstList().push_back(new StoreInst(TheVANext, $2));
1901 | VAARG ResolvedVal ',' Types {
1902 $$ = new VAArgInst($2, *$4);
1905 | VANEXT ResolvedVal ',' Types {
1906 $$ = new VANextInst($2, *$4);
1910 const Type *Ty = $2->front().first->getType();
1911 if (!Ty->isFirstClassType())
1912 ThrowException("PHI node operands must be of first class type!");
1913 $$ = new PHINode(Ty);
1914 $$->op_reserve($2->size()*2);
1915 while ($2->begin() != $2->end()) {
1916 if ($2->front().first->getType() != Ty)
1917 ThrowException("All elements of a PHI node must be of the same type!");
1918 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
1921 delete $2; // Free the list...
1923 | CALL TypesV ValueRef '(' ValueRefListE ')' {
1924 const PointerType *PFTy;
1925 const FunctionType *Ty;
1927 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1928 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
1929 // Pull out the types of all of the arguments...
1930 std::vector<const Type*> ParamTypes;
1932 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1934 ParamTypes.push_back((*I)->getType());
1937 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1938 if (isVarArg) ParamTypes.pop_back();
1940 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
1941 PFTy = PointerType::get(Ty);
1944 Value *V = getVal(PFTy, $3); // Get the function we're calling...
1946 // Create the call node...
1947 if (!$5) { // Has no arguments?
1948 // Make sure no arguments is a good thing!
1949 if (Ty->getNumParams() != 0)
1950 ThrowException("No arguments passed to a function that "
1951 "expects arguments!");
1953 $$ = new CallInst(V, std::vector<Value*>());
1954 } else { // Has arguments?
1955 // Loop through FunctionType's arguments and ensure they are specified
1958 FunctionType::param_iterator I = Ty->param_begin();
1959 FunctionType::param_iterator E = Ty->param_end();
1960 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
1962 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1963 if ((*ArgI)->getType() != *I)
1964 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
1965 (*I)->getDescription() + "'!");
1967 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1968 ThrowException("Invalid number of parameters detected!");
1970 $$ = new CallInst(V, *$5);
1980 // IndexList - List of indices for GEP based instructions...
1981 IndexList : ',' ValueRefList {
1984 $$ = new std::vector<Value*>();
1987 OptVolatile : VOLATILE {
1995 MemoryInst : MALLOC Types {
1996 $$ = new MallocInst(*$2);
1999 | MALLOC Types ',' UINT ValueRef {
2000 $$ = new MallocInst(*$2, getVal($4, $5));
2004 $$ = new AllocaInst(*$2);
2007 | ALLOCA Types ',' UINT ValueRef {
2008 $$ = new AllocaInst(*$2, getVal($4, $5));
2011 | FREE ResolvedVal {
2012 if (!isa<PointerType>($2->getType()))
2013 ThrowException("Trying to free nonpointer type " +
2014 $2->getType()->getDescription() + "!");
2015 $$ = new FreeInst($2);
2018 | OptVolatile LOAD Types ValueRef {
2019 if (!isa<PointerType>($3->get()))
2020 ThrowException("Can't load from nonpointer type: " +
2021 (*$3)->getDescription());
2022 $$ = new LoadInst(getVal(*$3, $4), "", $1);
2025 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
2026 const PointerType *PT = dyn_cast<PointerType>($5->get());
2028 ThrowException("Can't store to a nonpointer type: " +
2029 (*$5)->getDescription());
2030 const Type *ElTy = PT->getElementType();
2031 if (ElTy != $3->getType())
2032 ThrowException("Can't store '" + $3->getType()->getDescription() +
2033 "' into space of type '" + ElTy->getDescription() + "'!");
2035 $$ = new StoreInst($3, getVal(*$5, $6), $1);
2038 | GETELEMENTPTR Types ValueRef IndexList {
2039 if (!isa<PointerType>($2->get()))
2040 ThrowException("getelementptr insn requires pointer operand!");
2042 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
2043 // indices to uint struct indices for compatibility.
2044 generic_gep_type_iterator<std::vector<Value*>::iterator>
2045 GTI = gep_type_begin($2->get(), $4->begin(), $4->end()),
2046 GTE = gep_type_end($2->get(), $4->begin(), $4->end());
2047 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
2048 if (isa<StructType>(*GTI)) // Only change struct indices
2049 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
2050 if (CUI->getType() == Type::UByteTy)
2051 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
2053 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
2054 ThrowException("Invalid getelementptr indices for type '" +
2055 (*$2)->getDescription()+ "'!");
2056 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
2057 delete $2; delete $4;
2062 int yyerror(const char *ErrorMsg) {
2064 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
2065 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
2066 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
2067 if (yychar == YYEMPTY || yychar == 0)
2068 errMsg += "end-of-file.";
2070 errMsg += "token: '" + std::string(llvmAsmtext, llvmAsmleng) + "'";
2071 ThrowException(errMsg);