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.
34 std::string CurFilename;
38 static Module *ParserResult;
40 // DEBUG_UPREFS - Define this symbol if you want to enable debugging output
41 // relating to upreferences in the input stream.
43 //#define DEBUG_UPREFS 1
45 #define UR_OUT(X) std::cerr << X
50 #define YYERROR_VERBOSE 1
52 // HACK ALERT: This variable is used to implement the automatic conversion of
53 // variable argument instructions from their old to new forms. When this
54 // compatiblity "Feature" is removed, this should be too.
56 static BasicBlock *CurBB;
57 static bool ObsoleteVarArgs;
60 // This contains info used when building the body of a function. It is
61 // destroyed when the function is completed.
63 typedef std::vector<Value *> ValueList; // Numbered defs
64 static void ResolveDefinitions(std::map<const Type *,ValueList> &LateResolvers,
65 std::map<const Type *,ValueList> *FutureLateResolvers = 0);
67 static struct PerModuleInfo {
68 Module *CurrentModule;
69 std::map<const Type *, ValueList> Values; // Module level numbered definitions
70 std::map<const Type *,ValueList> LateResolveValues;
71 std::vector<PATypeHolder> Types;
72 std::map<ValID, PATypeHolder> LateResolveTypes;
74 /// PlaceHolderInfo - When temporary placeholder objects are created, remember
75 /// how they were referenced and one which line of the input they came from so
76 /// that we can resolve them later and print error messages as appropriate.
77 std::map<Value*, std::pair<ValID, int> > PlaceHolderInfo;
79 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
80 // references to global values. Global values may be referenced before they
81 // are defined, and if so, the temporary object that they represent is held
82 // here. This is used for forward references of ConstantPointerRefs.
84 typedef std::map<std::pair<const PointerType *,
85 ValID>, GlobalValue*> GlobalRefsType;
86 GlobalRefsType GlobalRefs;
89 // If we could not resolve some functions at function compilation time
90 // (calls to functions before they are defined), resolve them now... Types
91 // are resolved when the constant pool has been completely parsed.
93 ResolveDefinitions(LateResolveValues);
95 // Check to make sure that all global value forward references have been
98 if (!GlobalRefs.empty()) {
99 std::string UndefinedReferences = "Unresolved global references exist:\n";
101 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
103 UndefinedReferences += " " + I->first.first->getDescription() + " " +
104 I->first.second.getName() + "\n";
106 ThrowException(UndefinedReferences);
109 Values.clear(); // Clear out function local definitions
115 // DeclareNewGlobalValue - Called every time a new GV has been defined. This
116 // is used to remove things from the forward declaration map, resolving them
117 // to the correct thing as needed.
119 void DeclareNewGlobalValue(GlobalValue *GV, ValID D) {
120 // Check to see if there is a forward reference to this global variable...
121 // if there is, eliminate it and patch the reference to use the new def'n.
122 GlobalRefsType::iterator I =
123 GlobalRefs.find(std::make_pair(GV->getType(), D));
125 if (I != GlobalRefs.end()) {
126 GlobalValue *OldGV = I->second; // Get the placeholder...
127 I->first.second.destroy(); // Free string memory if necessary
129 // Replace all uses of the placeholder with the new GV
130 OldGV->replaceAllUsesWith(GV);
132 // Remove OldGV from the module...
133 if (GlobalVariable *GVar = dyn_cast<GlobalVariable>(OldGV))
134 CurrentModule->getGlobalList().erase(GVar);
136 CurrentModule->getFunctionList().erase(cast<Function>(OldGV));
138 // Remove the map entry for the global now that it has been created...
145 static struct PerFunctionInfo {
146 Function *CurrentFunction; // Pointer to current function being created
148 std::map<const Type*, ValueList> Values; // Keep track of #'d definitions
149 std::map<const Type*, ValueList> LateResolveValues;
150 std::vector<PATypeHolder> Types;
151 std::map<ValID, PATypeHolder> LateResolveTypes;
152 bool isDeclare; // Is this function a forward declararation?
154 /// BBForwardRefs - When we see forward references to basic blocks, keep
155 /// track of them here.
156 std::map<BasicBlock*, std::pair<ValID, int> > BBForwardRefs;
157 std::vector<BasicBlock*> NumberedBlocks;
160 inline PerFunctionInfo() {
165 inline void FunctionStart(Function *M) {
170 void FunctionDone() {
171 NumberedBlocks.clear();
173 // Any forward referenced blocks left?
174 if (!BBForwardRefs.empty())
175 ThrowException("Undefined reference to label " +
176 BBForwardRefs.begin()->second.first.getName());
178 // Resolve all forward references now.
179 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
181 // Make sure to resolve any constant expr references that might exist within
182 // the function we just declared itself.
184 if (CurrentFunction->hasName()) {
185 FID = ValID::create((char*)CurrentFunction->getName().c_str());
187 // Figure out which slot number if is...
188 ValueList &List = CurModule.Values[CurrentFunction->getType()];
189 for (unsigned i = 0; ; ++i) {
190 assert(i < List.size() && "Function not found!");
191 if (List[i] == CurrentFunction) {
192 FID = ValID::create((int)i);
197 CurModule.DeclareNewGlobalValue(CurrentFunction, FID);
199 Values.clear(); // Clear out function local definitions
200 Types.clear(); // Clear out function local types
204 } CurFun; // Info for the current function...
206 static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
209 //===----------------------------------------------------------------------===//
210 // Code to handle definitions of all the types
211 //===----------------------------------------------------------------------===//
213 static int InsertValue(Value *V,
214 std::map<const Type*,ValueList> &ValueTab = CurFun.Values) {
215 if (V->hasName()) return -1; // Is this a numbered definition?
217 // Yes, insert the value into the value table...
218 ValueList &List = ValueTab[V->getType()];
220 return List.size()-1;
223 static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
225 case ValID::NumberVal: { // Is it a numbered definition?
226 unsigned Num = (unsigned)D.Num;
228 // Module constants occupy the lowest numbered slots...
229 if (Num < CurModule.Types.size())
230 return CurModule.Types[Num];
232 Num -= CurModule.Types.size();
234 // Check that the number is within bounds...
235 if (Num <= CurFun.Types.size())
236 return CurFun.Types[Num];
239 case ValID::NameVal: { // Is it a named definition?
240 std::string Name(D.Name);
241 SymbolTable *SymTab = 0;
243 if (inFunctionScope()) {
244 SymTab = &CurFun.CurrentFunction->getSymbolTable();
245 N = SymTab->lookupType(Name);
249 // Symbol table doesn't automatically chain yet... because the function
250 // hasn't been added to the module...
252 SymTab = &CurModule.CurrentModule->getSymbolTable();
253 N = SymTab->lookupType(Name);
257 D.destroy(); // Free old strdup'd memory...
258 return cast<Type>(N);
261 ThrowException("Internal parser error: Invalid symbol type reference!");
264 // If we reached here, we referenced either a symbol that we don't know about
265 // or an id number that hasn't been read yet. We may be referencing something
266 // forward, so just create an entry to be resolved later and get to it...
268 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
270 std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
271 CurFun.LateResolveTypes : CurModule.LateResolveTypes;
273 std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
274 if (I != LateResolver.end()) {
278 Type *Typ = OpaqueType::get();
279 LateResolver.insert(std::make_pair(D, Typ));
283 static Value *lookupInSymbolTable(const Type *Ty, const std::string &Name) {
284 SymbolTable &SymTab =
285 inFunctionScope() ? CurFun.CurrentFunction->getSymbolTable() :
286 CurModule.CurrentModule->getSymbolTable();
287 return SymTab.lookup(Ty, Name);
290 // getValNonImprovising - Look up the value specified by the provided type and
291 // the provided ValID. If the value exists and has already been defined, return
292 // it. Otherwise return null.
294 static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
295 if (isa<FunctionType>(Ty))
296 ThrowException("Functions are not values and "
297 "must be referenced as pointers");
300 case ValID::NumberVal: { // Is it a numbered definition?
301 unsigned Num = (unsigned)D.Num;
303 // Module constants occupy the lowest numbered slots...
304 std::map<const Type*,ValueList>::iterator VI = CurModule.Values.find(Ty);
305 if (VI != CurModule.Values.end()) {
306 if (Num < VI->second.size())
307 return VI->second[Num];
308 Num -= VI->second.size();
311 // Make sure that our type is within bounds
312 VI = CurFun.Values.find(Ty);
313 if (VI == CurFun.Values.end()) return 0;
315 // Check that the number is within bounds...
316 if (VI->second.size() <= Num) return 0;
318 return VI->second[Num];
321 case ValID::NameVal: { // Is it a named definition?
322 Value *N = lookupInSymbolTable(Ty, std::string(D.Name));
323 if (N == 0) return 0;
325 D.destroy(); // Free old strdup'd memory...
329 // Check to make sure that "Ty" is an integral type, and that our
330 // value will fit into the specified type...
331 case ValID::ConstSIntVal: // Is it a constant pool reference??
332 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64))
333 ThrowException("Signed integral constant '" +
334 itostr(D.ConstPool64) + "' is invalid for type '" +
335 Ty->getDescription() + "'!");
336 return ConstantSInt::get(Ty, D.ConstPool64);
338 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
339 if (!ConstantUInt::isValueValidForType(Ty, D.UConstPool64)) {
340 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64)) {
341 ThrowException("Integral constant '" + utostr(D.UConstPool64) +
342 "' is invalid or out of range!");
343 } else { // This is really a signed reference. Transmogrify.
344 return ConstantSInt::get(Ty, D.ConstPool64);
347 return ConstantUInt::get(Ty, D.UConstPool64);
350 case ValID::ConstFPVal: // Is it a floating point const pool reference?
351 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
352 ThrowException("FP constant invalid for type!!");
353 return ConstantFP::get(Ty, D.ConstPoolFP);
355 case ValID::ConstNullVal: // Is it a null value?
356 if (!isa<PointerType>(Ty))
357 ThrowException("Cannot create a a non pointer null!");
358 return ConstantPointerNull::get(cast<PointerType>(Ty));
360 case ValID::ConstantVal: // Fully resolved constant?
361 if (D.ConstantValue->getType() != Ty)
362 ThrowException("Constant expression type different from required type!");
363 return D.ConstantValue;
366 assert(0 && "Unhandled case!");
370 assert(0 && "Unhandled case!");
374 // getVal - This function is identical to getValNonImprovising, except that if a
375 // value is not already defined, it "improvises" by creating a placeholder var
376 // that looks and acts just like the requested variable. When the value is
377 // defined later, all uses of the placeholder variable are replaced with the
380 static Value *getVal(const Type *Ty, const ValID &ID) {
381 if (Ty == Type::LabelTy)
382 ThrowException("Cannot use a basic block here");
384 // See if the value has already been defined.
385 Value *V = getValNonImprovising(Ty, ID);
388 // If we reached here, we referenced either a symbol that we don't know about
389 // or an id number that hasn't been read yet. We may be referencing something
390 // forward, so just create an entry to be resolved later and get to it...
392 V = new Argument(Ty);
394 // Remember where this forward reference came from. FIXME, shouldn't we try
395 // to recycle these things??
396 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
399 if (inFunctionScope())
400 InsertValue(V, CurFun.LateResolveValues);
402 InsertValue(V, CurModule.LateResolveValues);
406 /// getBBVal - This is used for two purposes:
407 /// * If isDefinition is true, a new basic block with the specified ID is being
409 /// * If isDefinition is true, this is a reference to a basic block, which may
410 /// or may not be a forward reference.
412 static BasicBlock *getBBVal(const ValID &ID, bool isDefinition = false) {
413 assert(inFunctionScope() && "Can't get basic block at global scope!");
418 default: ThrowException("Illegal label reference " + ID.getName());
419 case ValID::NumberVal: // Is it a numbered definition?
420 if (unsigned(ID.Num) >= CurFun.NumberedBlocks.size())
421 CurFun.NumberedBlocks.resize(ID.Num+1);
422 BB = CurFun.NumberedBlocks[ID.Num];
424 case ValID::NameVal: // Is it a named definition?
426 if (Value *N = lookupInSymbolTable(Type::LabelTy, Name))
427 BB = cast<BasicBlock>(N);
431 // See if the block has already been defined.
433 // If this is the definition of the block, make sure the existing value was
434 // just a forward reference. If it was a forward reference, there will be
435 // an entry for it in the PlaceHolderInfo map.
436 if (isDefinition && !CurFun.BBForwardRefs.erase(BB))
437 // The existing value was a definition, not a forward reference.
438 ThrowException("Redefinition of label " + ID.getName());
440 ID.destroy(); // Free strdup'd memory.
444 // Otherwise this block has not been seen before.
445 BB = new BasicBlock("", CurFun.CurrentFunction);
446 if (ID.Type == ValID::NameVal) {
447 BB->setName(ID.Name);
449 CurFun.NumberedBlocks[ID.Num] = BB;
452 // If this is not a definition, keep track of it so we can use it as a forward
455 // Remember where this forward reference came from.
456 CurFun.BBForwardRefs[BB] = std::make_pair(ID, llvmAsmlineno);
458 // The forward declaration could have been inserted anywhere in the
459 // function: insert it into the correct place now.
460 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
461 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
468 //===----------------------------------------------------------------------===//
469 // Code to handle forward references in instructions
470 //===----------------------------------------------------------------------===//
472 // This code handles the late binding needed with statements that reference
473 // values not defined yet... for example, a forward branch, or the PHI node for
476 // This keeps a table (CurFun.LateResolveValues) of all such forward references
477 // and back patchs after we are done.
480 // ResolveDefinitions - If we could not resolve some defs at parsing
481 // time (forward branches, phi functions for loops, etc...) resolve the
484 static void ResolveDefinitions(std::map<const Type*,ValueList> &LateResolvers,
485 std::map<const Type*,ValueList> *FutureLateResolvers) {
486 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
487 for (std::map<const Type*,ValueList>::iterator LRI = LateResolvers.begin(),
488 E = LateResolvers.end(); LRI != E; ++LRI) {
489 ValueList &List = LRI->second;
490 while (!List.empty()) {
491 Value *V = List.back();
494 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
495 CurModule.PlaceHolderInfo.find(V);
496 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
498 ValID &DID = PHI->second.first;
500 Value *TheRealValue = getValNonImprovising(LRI->first, DID);
502 V->replaceAllUsesWith(TheRealValue);
504 CurModule.PlaceHolderInfo.erase(PHI);
505 } else if (FutureLateResolvers) {
506 // Functions have their unresolved items forwarded to the module late
508 InsertValue(V, *FutureLateResolvers);
510 if (DID.Type == ValID::NameVal)
511 ThrowException("Reference to an invalid definition: '" +DID.getName()+
512 "' of type '" + V->getType()->getDescription() + "'",
515 ThrowException("Reference to an invalid definition: #" +
516 itostr(DID.Num) + " of type '" +
517 V->getType()->getDescription() + "'",
523 LateResolvers.clear();
526 // ResolveTypeTo - A brand new type was just declared. This means that (if
527 // name is not null) things referencing Name can be resolved. Otherwise, things
528 // refering to the number can be resolved. Do this now.
530 static void ResolveTypeTo(char *Name, const Type *ToTy) {
531 std::vector<PATypeHolder> &Types = inFunctionScope() ?
532 CurFun.Types : CurModule.Types;
535 if (Name) D = ValID::create(Name);
536 else D = ValID::create((int)Types.size());
538 std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
539 CurFun.LateResolveTypes : CurModule.LateResolveTypes;
541 std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
542 if (I != LateResolver.end()) {
543 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
544 LateResolver.erase(I);
548 // ResolveTypes - At this point, all types should be resolved. Any that aren't
551 static void ResolveTypes(std::map<ValID, PATypeHolder> &LateResolveTypes) {
552 if (!LateResolveTypes.empty()) {
553 const ValID &DID = LateResolveTypes.begin()->first;
555 if (DID.Type == ValID::NameVal)
556 ThrowException("Reference to an invalid type: '" +DID.getName() + "'");
558 ThrowException("Reference to an invalid type: #" + itostr(DID.Num));
562 // setValueName - Set the specified value to the name given. The name may be
563 // null potentially, in which case this is a noop. The string passed in is
564 // assumed to be a malloc'd string buffer, and is free'd by this function.
566 static void setValueName(Value *V, char *NameStr) {
568 std::string Name(NameStr); // Copy string
569 free(NameStr); // Free old string
571 if (V->getType() == Type::VoidTy)
572 ThrowException("Can't assign name '" + Name+"' to value with void type!");
574 assert(inFunctionScope() && "Must be in function scope!");
575 SymbolTable &ST = CurFun.CurrentFunction->getSymbolTable();
576 if (ST.lookup(V->getType(), Name))
577 ThrowException("Redefinition of value named '" + Name + "' in the '" +
578 V->getType()->getDescription() + "' type plane!");
581 V->setName(Name, &ST);
585 // setValueNameMergingDuplicates - Set the specified value to the name given.
586 // The name may be null potentially, in which case this is a noop. The string
587 // passed in is assumed to be a malloc'd string buffer, and is free'd by this
590 // This function returns true if the value has already been defined, but is
591 // allowed to be redefined in the specified context. If the name is a new name
592 // for the typeplane, false is returned.
594 static bool setValueNameMergingDuplicates(GlobalVariable *V, char *NameStr) {
595 if (NameStr == 0) return false;
597 std::string Name(NameStr); // Copy string
598 free(NameStr); // Free old string
600 SymbolTable &ST = CurModule.CurrentModule->getSymbolTable();
602 Value *Existing = ST.lookup(V->getType(), Name);
603 if (Existing) { // Inserting a name that is already defined???
605 // We are a simple redefinition of a value, check to see if it is defined
606 // the same as the old one...
607 if (GlobalVariable *EGV = dyn_cast<GlobalVariable>(Existing)) {
608 // We are allowed to redefine a global variable in two circumstances:
609 // 1. If at least one of the globals is uninitialized or
610 // 2. If both initializers have the same value.
612 if (!EGV->hasInitializer() || !V->hasInitializer() ||
613 EGV->getInitializer() == V->getInitializer()) {
615 // Make sure the existing global version gets the initializer! Make
616 // sure that it also gets marked const if the new version is.
617 if (V->hasInitializer() && !EGV->hasInitializer())
618 EGV->setInitializer(V->getInitializer());
620 EGV->setConstant(true);
621 EGV->setLinkage(V->getLinkage());
623 delete V; // Destroy the duplicate!
624 return true; // They are equivalent!
628 ThrowException("Redefinition of value named '" + Name + "' in the '" +
629 V->getType()->getDescription() + "' type plane!");
633 V->setName(Name, &ST);
637 /// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
638 /// this is a declaration, otherwise it is a definition.
639 static void ParseGlobalVariable(char *NameStr,GlobalValue::LinkageTypes Linkage,
640 bool isConstantGlobal, const Type *Ty,
641 Constant *Initializer) {
642 // Global declarations appear in Constant Pool
643 GlobalVariable *GV = new GlobalVariable(Ty, isConstantGlobal, Linkage,
645 if (!setValueNameMergingDuplicates(GV, NameStr)) { // If not redefining...
646 CurModule.CurrentModule->getGlobalList().push_back(GV);
647 int Slot = InsertValue(GV, CurModule.Values);
650 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
652 CurModule.DeclareNewGlobalValue(GV,
653 ValID::create((char*)GV->getName().c_str()));
658 // setTypeName - Set the specified type to the name given. The name may be
659 // null potentially, in which case this is a noop. The string passed in is
660 // assumed to be a malloc'd string buffer, and is freed by this function.
662 // This function returns true if the type has already been defined, but is
663 // allowed to be redefined in the specified context. If the name is a new name
664 // for the type plane, it is inserted and false is returned.
665 static bool setTypeName(const Type *T, char *NameStr) {
666 if (NameStr == 0) return false;
668 std::string Name(NameStr); // Copy string
669 free(NameStr); // Free old string
671 // We don't allow assigning names to void type
672 if (T == Type::VoidTy)
673 ThrowException("Can't assign name '" + Name + "' to the void type!");
675 SymbolTable &ST = inFunctionScope() ?
676 CurFun.CurrentFunction->getSymbolTable() :
677 CurModule.CurrentModule->getSymbolTable();
679 // Inserting a name that is already defined???
680 if (Type *Existing = ST.lookupType(Name)) {
681 // There is only one case where this is allowed: when we are refining an
682 // opaque type. In this case, Existing will be an opaque type.
683 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
684 // We ARE replacing an opaque type!
685 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
689 // Otherwise, this is an attempt to redefine a type. That's okay if
690 // the redefinition is identical to the original. This will be so if
691 // Existing and T point to the same Type object. In this one case we
692 // allow the equivalent redefinition.
693 if (Existing == T) return true; // Yes, it's equal.
695 // Any other kind of (non-equivalent) redefinition is an error.
696 ThrowException("Redefinition of type named '" + Name + "' in the '" +
697 T->getDescription() + "' type plane!");
700 // Okay, its a newly named type. Set its name.
701 if (!Name.empty()) ST.insert(Name, T);
706 //===----------------------------------------------------------------------===//
707 // Code for handling upreferences in type names...
710 // TypeContains - Returns true if Ty directly contains E in it.
712 static bool TypeContains(const Type *Ty, const Type *E) {
713 return find(Ty->subtype_begin(), Ty->subtype_end(), E) != Ty->subtype_end();
718 // NestingLevel - The number of nesting levels that need to be popped before
719 // this type is resolved.
720 unsigned NestingLevel;
722 // LastContainedTy - This is the type at the current binding level for the
723 // type. Every time we reduce the nesting level, this gets updated.
724 const Type *LastContainedTy;
726 // UpRefTy - This is the actual opaque type that the upreference is
730 UpRefRecord(unsigned NL, OpaqueType *URTy)
731 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
735 // UpRefs - A list of the outstanding upreferences that need to be resolved.
736 static std::vector<UpRefRecord> UpRefs;
738 /// HandleUpRefs - Every time we finish a new layer of types, this function is
739 /// called. It loops through the UpRefs vector, which is a list of the
740 /// currently active types. For each type, if the up reference is contained in
741 /// the newly completed type, we decrement the level count. When the level
742 /// count reaches zero, the upreferenced type is the type that is passed in:
743 /// thus we can complete the cycle.
745 static PATypeHolder HandleUpRefs(const Type *ty) {
746 if (!ty->isAbstract()) return ty;
748 UR_OUT("Type '" << Ty->getDescription() <<
749 "' newly formed. Resolving upreferences.\n" <<
750 UpRefs.size() << " upreferences active!\n");
752 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
753 // to zero), we resolve them all together before we resolve them to Ty. At
754 // the end of the loop, if there is anything to resolve to Ty, it will be in
756 OpaqueType *TypeToResolve = 0;
758 for (unsigned i = 0; i != UpRefs.size(); ++i) {
759 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
760 << UpRefs[i].second->getDescription() << ") = "
761 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
762 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
763 // Decrement level of upreference
764 unsigned Level = --UpRefs[i].NestingLevel;
765 UpRefs[i].LastContainedTy = Ty;
766 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
767 if (Level == 0) { // Upreference should be resolved!
768 if (!TypeToResolve) {
769 TypeToResolve = UpRefs[i].UpRefTy;
771 UR_OUT(" * Resolving upreference for "
772 << UpRefs[i].second->getDescription() << "\n";
773 std::string OldName = UpRefs[i].UpRefTy->getDescription());
774 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
775 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
776 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
778 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
779 --i; // Do not skip the next element...
785 UR_OUT(" * Resolving upreference for "
786 << UpRefs[i].second->getDescription() << "\n";
787 std::string OldName = TypeToResolve->getDescription());
788 TypeToResolve->refineAbstractTypeTo(Ty);
795 //===----------------------------------------------------------------------===//
796 // RunVMAsmParser - Define an interface to this parser
797 //===----------------------------------------------------------------------===//
799 Module *llvm::RunVMAsmParser(const std::string &Filename, FILE *F) {
801 CurFilename = Filename;
802 llvmAsmlineno = 1; // Reset the current line number...
803 ObsoleteVarArgs = false;
805 // Allocate a new module to read
806 CurModule.CurrentModule = new Module(Filename);
808 yyparse(); // Parse the file, potentially throwing exception
810 Module *Result = ParserResult;
812 // Check to see if they called va_start but not va_arg..
813 if (!ObsoleteVarArgs)
814 if (Function *F = Result->getNamedFunction("llvm.va_start"))
815 if (F->asize() == 1) {
816 std::cerr << "WARNING: this file uses obsolete features. "
817 << "Assemble and disassemble to update it.\n";
818 ObsoleteVarArgs = true;
821 if (ObsoleteVarArgs) {
822 // If the user is making use of obsolete varargs intrinsics, adjust them for
824 if (Function *F = Result->getNamedFunction("llvm.va_start")) {
825 assert(F->asize() == 1 && "Obsolete va_start takes 1 argument!");
827 const Type *RetTy = F->getFunctionType()->getParamType(0);
828 RetTy = cast<PointerType>(RetTy)->getElementType();
829 Function *NF = Result->getOrInsertFunction("llvm.va_start", RetTy, 0);
831 while (!F->use_empty()) {
832 CallInst *CI = cast<CallInst>(F->use_back());
833 Value *V = new CallInst(NF, "", CI);
834 new StoreInst(V, CI->getOperand(1), CI);
835 CI->getParent()->getInstList().erase(CI);
837 Result->getFunctionList().erase(F);
840 if (Function *F = Result->getNamedFunction("llvm.va_end")) {
841 assert(F->asize() == 1 && "Obsolete va_end takes 1 argument!");
842 const Type *ArgTy = F->getFunctionType()->getParamType(0);
843 ArgTy = cast<PointerType>(ArgTy)->getElementType();
844 Function *NF = Result->getOrInsertFunction("llvm.va_end", Type::VoidTy,
847 while (!F->use_empty()) {
848 CallInst *CI = cast<CallInst>(F->use_back());
849 Value *V = new LoadInst(CI->getOperand(1), "", CI);
850 new CallInst(NF, V, "", CI);
851 CI->getParent()->getInstList().erase(CI);
853 Result->getFunctionList().erase(F);
856 if (Function *F = Result->getNamedFunction("llvm.va_copy")) {
857 assert(F->asize() == 2 && "Obsolete va_copy takes 2 argument!");
858 const Type *ArgTy = F->getFunctionType()->getParamType(0);
859 ArgTy = cast<PointerType>(ArgTy)->getElementType();
860 Function *NF = Result->getOrInsertFunction("llvm.va_copy", ArgTy,
863 while (!F->use_empty()) {
864 CallInst *CI = cast<CallInst>(F->use_back());
865 Value *V = new CallInst(NF, CI->getOperand(2), "", CI);
866 new StoreInst(V, CI->getOperand(1), CI);
867 CI->getParent()->getInstList().erase(CI);
869 Result->getFunctionList().erase(F);
873 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
882 llvm::Module *ModuleVal;
883 llvm::Function *FunctionVal;
884 std::pair<llvm::PATypeHolder*, char*> *ArgVal;
885 llvm::BasicBlock *BasicBlockVal;
886 llvm::TerminatorInst *TermInstVal;
887 llvm::Instruction *InstVal;
888 llvm::Constant *ConstVal;
890 const llvm::Type *PrimType;
891 llvm::PATypeHolder *TypeVal;
892 llvm::Value *ValueVal;
894 std::vector<std::pair<llvm::PATypeHolder*,char*> > *ArgList;
895 std::vector<llvm::Value*> *ValueList;
896 std::list<llvm::PATypeHolder> *TypeList;
897 std::list<std::pair<llvm::Value*,
898 llvm::BasicBlock*> > *PHIList; // Represent the RHS of PHI node
899 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
900 std::vector<llvm::Constant*> *ConstVector;
902 llvm::GlobalValue::LinkageTypes Linkage;
910 char *StrVal; // This memory is strdup'd!
911 llvm::ValID ValIDVal; // strdup'd memory maybe!
913 llvm::Instruction::BinaryOps BinaryOpVal;
914 llvm::Instruction::TermOps TermOpVal;
915 llvm::Instruction::MemoryOps MemOpVal;
916 llvm::Instruction::OtherOps OtherOpVal;
917 llvm::Module::Endianness Endianness;
920 %type <ModuleVal> Module FunctionList
921 %type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
922 %type <BasicBlockVal> BasicBlock InstructionList
923 %type <TermInstVal> BBTerminatorInst
924 %type <InstVal> Inst InstVal MemoryInst
925 %type <ConstVal> ConstVal ConstExpr
926 %type <ConstVector> ConstVector
927 %type <ArgList> ArgList ArgListH
928 %type <ArgVal> ArgVal
929 %type <PHIList> PHIList
930 %type <ValueList> ValueRefList ValueRefListE // For call param lists
931 %type <ValueList> IndexList // For GEP derived indices
932 %type <TypeList> TypeListI ArgTypeListI
933 %type <JumpTable> JumpTable
934 %type <BoolVal> GlobalType // GLOBAL or CONSTANT?
935 %type <BoolVal> OptVolatile // 'volatile' or not
936 %type <Linkage> OptLinkage
937 %type <Endianness> BigOrLittle
939 // ValueRef - Unresolved reference to a definition or BB
940 %type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
941 %type <ValueVal> ResolvedVal // <type> <valref> pair
942 // Tokens and types for handling constant integer values
944 // ESINT64VAL - A negative number within long long range
945 %token <SInt64Val> ESINT64VAL
947 // EUINT64VAL - A positive number within uns. long long range
948 %token <UInt64Val> EUINT64VAL
949 %type <SInt64Val> EINT64VAL
951 %token <SIntVal> SINTVAL // Signed 32 bit ints...
952 %token <UIntVal> UINTVAL // Unsigned 32 bit ints...
953 %type <SIntVal> INTVAL
954 %token <FPVal> FPVAL // Float or Double constant
957 %type <TypeVal> Types TypesV UpRTypes UpRTypesV
958 %type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
959 %token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
960 %token <PrimType> FLOAT DOUBLE TYPE LABEL
962 %token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
963 %type <StrVal> Name OptName OptAssign
966 %token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
967 %token DECLARE GLOBAL CONSTANT VOLATILE
968 %token TO DOTDOTDOT NULL_TOK CONST INTERNAL LINKONCE WEAK APPENDING
969 %token OPAQUE NOT EXTERNAL TARGET ENDIAN POINTERSIZE LITTLE BIG
971 // Basic Block Terminating Operators
972 %token <TermOpVal> RET BR SWITCH INVOKE UNWIND
975 %type <BinaryOpVal> BinaryOps // all the binary operators
976 %type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
977 %token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
978 %token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
980 // Memory Instructions
981 %token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
984 %type <OtherOpVal> ShiftOps
985 %token <OtherOpVal> PHI_TOK CALL CAST SELECT SHL SHR VAARG VANEXT
986 %token VA_ARG // FIXME: OBSOLETE
991 // Handle constant integer size restriction and conversion...
995 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
996 ThrowException("Value too large for type!");
1001 EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
1002 EINT64VAL : EUINT64VAL {
1003 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
1004 ThrowException("Value too large for type!");
1008 // Operations that are notably excluded from this list include:
1009 // RET, BR, & SWITCH because they end basic blocks and are treated specially.
1011 ArithmeticOps: ADD | SUB | MUL | DIV | REM;
1012 LogicalOps : AND | OR | XOR;
1013 SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
1014 BinaryOps : ArithmeticOps | LogicalOps | SetCondOps;
1016 ShiftOps : SHL | SHR;
1018 // These are some types that allow classification if we only want a particular
1019 // thing... for example, only a signed, unsigned, or integral type.
1020 SIntType : LONG | INT | SHORT | SBYTE;
1021 UIntType : ULONG | UINT | USHORT | UBYTE;
1022 IntType : SIntType | UIntType;
1023 FPType : FLOAT | DOUBLE;
1025 // OptAssign - Value producing statements have an optional assignment component
1026 OptAssign : Name '=' {
1033 OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
1034 LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
1035 WEAK { $$ = GlobalValue::WeakLinkage; } |
1036 APPENDING { $$ = GlobalValue::AppendingLinkage; } |
1037 /*empty*/ { $$ = GlobalValue::ExternalLinkage; };
1039 //===----------------------------------------------------------------------===//
1040 // Types includes all predefined types... except void, because it can only be
1041 // used in specific contexts (function returning void for example). To have
1042 // access to it, a user must explicitly use TypesV.
1045 // TypesV includes all of 'Types', but it also includes the void type.
1046 TypesV : Types | VOID { $$ = new PATypeHolder($1); };
1047 UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
1050 if (!UpRefs.empty())
1051 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
1056 // Derived types are added later...
1058 PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
1059 PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
1061 $$ = new PATypeHolder(OpaqueType::get());
1064 $$ = new PATypeHolder($1);
1066 UpRTypes : SymbolicValueRef { // Named types are also simple types...
1067 $$ = new PATypeHolder(getTypeVal($1));
1070 // Include derived types in the Types production.
1072 UpRTypes : '\\' EUINT64VAL { // Type UpReference
1073 if ($2 > (uint64_t)~0U) ThrowException("Value out of range!");
1074 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
1075 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
1076 $$ = new PATypeHolder(OT);
1077 UR_OUT("New Upreference!\n");
1079 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
1080 std::vector<const Type*> Params;
1081 mapto($3->begin(), $3->end(), std::back_inserter(Params),
1082 std::mem_fun_ref(&PATypeHolder::get));
1083 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1084 if (isVarArg) Params.pop_back();
1086 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
1087 delete $3; // Delete the argument list
1088 delete $1; // Delete the return type handle
1090 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
1091 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
1094 | '{' TypeListI '}' { // Structure type?
1095 std::vector<const Type*> Elements;
1096 mapto($2->begin(), $2->end(), std::back_inserter(Elements),
1097 std::mem_fun_ref(&PATypeHolder::get));
1099 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
1102 | '{' '}' { // Empty structure type?
1103 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
1105 | UpRTypes '*' { // Pointer type?
1106 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
1110 // TypeList - Used for struct declarations and as a basis for function type
1111 // declaration type lists
1113 TypeListI : UpRTypes {
1114 $$ = new std::list<PATypeHolder>();
1115 $$->push_back(*$1); delete $1;
1117 | TypeListI ',' UpRTypes {
1118 ($$=$1)->push_back(*$3); delete $3;
1121 // ArgTypeList - List of types for a function type declaration...
1122 ArgTypeListI : TypeListI
1123 | TypeListI ',' DOTDOTDOT {
1124 ($$=$1)->push_back(Type::VoidTy);
1127 ($$ = new std::list<PATypeHolder>())->push_back(Type::VoidTy);
1130 $$ = new std::list<PATypeHolder>();
1133 // ConstVal - The various declarations that go into the constant pool. This
1134 // production is used ONLY to represent constants that show up AFTER a 'const',
1135 // 'constant' or 'global' token at global scope. Constants that can be inlined
1136 // into other expressions (such as integers and constexprs) are handled by the
1137 // ResolvedVal, ValueRef and ConstValueRef productions.
1139 ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
1140 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
1142 ThrowException("Cannot make array constant with type: '" +
1143 (*$1)->getDescription() + "'!");
1144 const Type *ETy = ATy->getElementType();
1145 int NumElements = ATy->getNumElements();
1147 // Verify that we have the correct size...
1148 if (NumElements != -1 && NumElements != (int)$3->size())
1149 ThrowException("Type mismatch: constant sized array initialized with " +
1150 utostr($3->size()) + " arguments, but has size of " +
1151 itostr(NumElements) + "!");
1153 // Verify all elements are correct type!
1154 for (unsigned i = 0; i < $3->size(); i++) {
1155 if (ETy != (*$3)[i]->getType())
1156 ThrowException("Element #" + utostr(i) + " is not of type '" +
1157 ETy->getDescription() +"' as required!\nIt is of type '"+
1158 (*$3)[i]->getType()->getDescription() + "'.");
1161 $$ = ConstantArray::get(ATy, *$3);
1162 delete $1; delete $3;
1165 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
1167 ThrowException("Cannot make array constant with type: '" +
1168 (*$1)->getDescription() + "'!");
1170 int NumElements = ATy->getNumElements();
1171 if (NumElements != -1 && NumElements != 0)
1172 ThrowException("Type mismatch: constant sized array initialized with 0"
1173 " arguments, but has size of " + itostr(NumElements) +"!");
1174 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
1177 | Types 'c' STRINGCONSTANT {
1178 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
1180 ThrowException("Cannot make array constant with type: '" +
1181 (*$1)->getDescription() + "'!");
1183 int NumElements = ATy->getNumElements();
1184 const Type *ETy = ATy->getElementType();
1185 char *EndStr = UnEscapeLexed($3, true);
1186 if (NumElements != -1 && NumElements != (EndStr-$3))
1187 ThrowException("Can't build string constant of size " +
1188 itostr((int)(EndStr-$3)) +
1189 " when array has size " + itostr(NumElements) + "!");
1190 std::vector<Constant*> Vals;
1191 if (ETy == Type::SByteTy) {
1192 for (char *C = $3; C != EndStr; ++C)
1193 Vals.push_back(ConstantSInt::get(ETy, *C));
1194 } else if (ETy == Type::UByteTy) {
1195 for (char *C = $3; C != EndStr; ++C)
1196 Vals.push_back(ConstantUInt::get(ETy, (unsigned char)*C));
1199 ThrowException("Cannot build string arrays of non byte sized elements!");
1202 $$ = ConstantArray::get(ATy, Vals);
1205 | Types '{' ConstVector '}' {
1206 const StructType *STy = dyn_cast<StructType>($1->get());
1208 ThrowException("Cannot make struct constant with type: '" +
1209 (*$1)->getDescription() + "'!");
1211 if ($3->size() != STy->getNumContainedTypes())
1212 ThrowException("Illegal number of initializers for structure type!");
1214 // Check to ensure that constants are compatible with the type initializer!
1215 for (unsigned i = 0, e = $3->size(); i != e; ++i)
1216 if ((*$3)[i]->getType() != STy->getElementType(i))
1217 ThrowException("Expected type '" +
1218 STy->getElementType(i)->getDescription() +
1219 "' for element #" + utostr(i) +
1220 " of structure initializer!");
1222 $$ = ConstantStruct::get(STy, *$3);
1223 delete $1; delete $3;
1226 const StructType *STy = dyn_cast<StructType>($1->get());
1228 ThrowException("Cannot make struct constant with type: '" +
1229 (*$1)->getDescription() + "'!");
1231 if (STy->getNumContainedTypes() != 0)
1232 ThrowException("Illegal number of initializers for structure type!");
1234 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1238 const PointerType *PTy = dyn_cast<PointerType>($1->get());
1240 ThrowException("Cannot make null pointer constant with type: '" +
1241 (*$1)->getDescription() + "'!");
1243 $$ = ConstantPointerNull::get(PTy);
1246 | Types SymbolicValueRef {
1247 const PointerType *Ty = dyn_cast<PointerType>($1->get());
1249 ThrowException("Global const reference must be a pointer type!");
1251 // ConstExprs can exist in the body of a function, thus creating
1252 // ConstantPointerRefs whenever they refer to a variable. Because we are in
1253 // the context of a function, getValNonImprovising will search the functions
1254 // symbol table instead of the module symbol table for the global symbol,
1255 // which throws things all off. To get around this, we just tell
1256 // getValNonImprovising that we are at global scope here.
1258 Function *SavedCurFn = CurFun.CurrentFunction;
1259 CurFun.CurrentFunction = 0;
1261 Value *V = getValNonImprovising(Ty, $2);
1263 CurFun.CurrentFunction = SavedCurFn;
1265 // If this is an initializer for a constant pointer, which is referencing a
1266 // (currently) undefined variable, create a stub now that shall be replaced
1267 // in the future with the right type of variable.
1270 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1271 const PointerType *PT = cast<PointerType>(Ty);
1273 // First check to see if the forward references value is already created!
1274 PerModuleInfo::GlobalRefsType::iterator I =
1275 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
1277 if (I != CurModule.GlobalRefs.end()) {
1278 V = I->second; // Placeholder already exists, use it...
1281 // Create a placeholder for the global variable reference...
1282 GlobalVariable *GV = new GlobalVariable(PT->getElementType(),
1284 GlobalValue::ExternalLinkage);
1285 // Keep track of the fact that we have a forward ref to recycle it
1286 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1288 // Must temporarily push this value into the module table...
1289 CurModule.CurrentModule->getGlobalList().push_back(GV);
1294 GlobalValue *GV = cast<GlobalValue>(V);
1295 $$ = ConstantPointerRef::get(GV);
1296 delete $1; // Free the type handle
1299 if ($1->get() != $2->getType())
1300 ThrowException("Mismatched types for constant expression!");
1304 | Types ZEROINITIALIZER {
1305 $$ = Constant::getNullValue($1->get());
1309 ConstVal : SIntType EINT64VAL { // integral constants
1310 if (!ConstantSInt::isValueValidForType($1, $2))
1311 ThrowException("Constant value doesn't fit in type!");
1312 $$ = ConstantSInt::get($1, $2);
1314 | UIntType EUINT64VAL { // integral constants
1315 if (!ConstantUInt::isValueValidForType($1, $2))
1316 ThrowException("Constant value doesn't fit in type!");
1317 $$ = ConstantUInt::get($1, $2);
1319 | BOOL TRUETOK { // Boolean constants
1320 $$ = ConstantBool::True;
1322 | BOOL FALSETOK { // Boolean constants
1323 $$ = ConstantBool::False;
1325 | FPType FPVAL { // Float & Double constants
1326 $$ = ConstantFP::get($1, $2);
1330 ConstExpr: CAST '(' ConstVal TO Types ')' {
1331 if (!$3->getType()->isFirstClassType())
1332 ThrowException("cast constant expression from a non-primitive type: '" +
1333 $3->getType()->getDescription() + "'!");
1334 if (!$5->get()->isFirstClassType())
1335 ThrowException("cast constant expression to a non-primitive type: '" +
1336 $5->get()->getDescription() + "'!");
1337 $$ = ConstantExpr::getCast($3, $5->get());
1340 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1341 if (!isa<PointerType>($3->getType()))
1342 ThrowException("GetElementPtr requires a pointer operand!");
1344 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
1345 // indices to uint struct indices for compatibility.
1346 generic_gep_type_iterator<std::vector<Value*>::iterator>
1347 GTI = gep_type_begin($3->getType(), $4->begin(), $4->end()),
1348 GTE = gep_type_end($3->getType(), $4->begin(), $4->end());
1349 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
1350 if (isa<StructType>(*GTI)) // Only change struct indices
1351 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
1352 if (CUI->getType() == Type::UByteTy)
1353 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
1356 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
1358 ThrowException("Index list invalid for constant getelementptr!");
1360 std::vector<Constant*> IdxVec;
1361 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1362 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
1363 IdxVec.push_back(C);
1365 ThrowException("Indices to constant getelementptr must be constants!");
1369 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
1371 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1372 if ($3->getType() != Type::BoolTy)
1373 ThrowException("Select condition must be of boolean type!");
1374 if ($5->getType() != $7->getType())
1375 ThrowException("Select operand types must match!");
1376 $$ = ConstantExpr::getSelect($3, $5, $7);
1378 | BinaryOps '(' ConstVal ',' ConstVal ')' {
1379 if ($3->getType() != $5->getType())
1380 ThrowException("Binary operator types must match!");
1381 $$ = ConstantExpr::get($1, $3, $5);
1383 | ShiftOps '(' ConstVal ',' ConstVal ')' {
1384 if ($5->getType() != Type::UByteTy)
1385 ThrowException("Shift count for shift constant must be unsigned byte!");
1386 if (!$3->getType()->isInteger())
1387 ThrowException("Shift constant expression requires integer operand!");
1388 $$ = ConstantExpr::get($1, $3, $5);
1392 // ConstVector - A list of comma separated constants.
1393 ConstVector : ConstVector ',' ConstVal {
1394 ($$ = $1)->push_back($3);
1397 $$ = new std::vector<Constant*>();
1402 // GlobalType - Match either GLOBAL or CONSTANT for global declarations...
1403 GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
1406 //===----------------------------------------------------------------------===//
1407 // Rules to match Modules
1408 //===----------------------------------------------------------------------===//
1410 // Module rule: Capture the result of parsing the whole file into a result
1413 Module : FunctionList {
1414 $$ = ParserResult = $1;
1415 CurModule.ModuleDone();
1418 // FunctionList - A list of functions, preceeded by a constant pool.
1420 FunctionList : FunctionList Function {
1422 CurFun.FunctionDone();
1424 | FunctionList FunctionProto {
1427 | FunctionList IMPLEMENTATION {
1431 $$ = CurModule.CurrentModule;
1432 // Resolve circular types before we parse the body of the module
1433 ResolveTypes(CurModule.LateResolveTypes);
1436 // ConstPool - Constants with optional names assigned to them.
1437 ConstPool : ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
1438 // Eagerly resolve types. This is not an optimization, this is a
1439 // requirement that is due to the fact that we could have this:
1441 // %list = type { %list * }
1442 // %list = type { %list * } ; repeated type decl
1444 // If types are not resolved eagerly, then the two types will not be
1445 // determined to be the same type!
1447 ResolveTypeTo($2, *$4);
1449 if (!setTypeName(*$4, $2) && !$2) {
1450 // If this is a named type that is not a redefinition, add it to the slot
1452 if (inFunctionScope())
1453 CurFun.Types.push_back(*$4);
1455 CurModule.Types.push_back(*$4);
1460 | ConstPool FunctionProto { // Function prototypes can be in const pool
1462 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
1463 if ($5 == 0) ThrowException("Global value initializer is not a constant!");
1464 ParseGlobalVariable($2, $3, $4, $5->getType(), $5);
1466 | ConstPool OptAssign EXTERNAL GlobalType Types {
1467 ParseGlobalVariable($2, GlobalValue::ExternalLinkage, $4, *$5, 0);
1470 | ConstPool TARGET TargetDefinition {
1472 | /* empty: end of list */ {
1477 BigOrLittle : BIG { $$ = Module::BigEndian; };
1478 BigOrLittle : LITTLE { $$ = Module::LittleEndian; };
1480 TargetDefinition : ENDIAN '=' BigOrLittle {
1481 CurModule.CurrentModule->setEndianness($3);
1483 | POINTERSIZE '=' EUINT64VAL {
1485 CurModule.CurrentModule->setPointerSize(Module::Pointer32);
1487 CurModule.CurrentModule->setPointerSize(Module::Pointer64);
1489 ThrowException("Invalid pointer size: '" + utostr($3) + "'!");
1493 //===----------------------------------------------------------------------===//
1494 // Rules to match Function Headers
1495 //===----------------------------------------------------------------------===//
1497 Name : VAR_ID | STRINGCONSTANT;
1498 OptName : Name | /*empty*/ { $$ = 0; };
1500 ArgVal : Types OptName {
1501 if (*$1 == Type::VoidTy)
1502 ThrowException("void typed arguments are invalid!");
1503 $$ = new std::pair<PATypeHolder*, char*>($1, $2);
1506 ArgListH : ArgListH ',' ArgVal {
1512 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1517 ArgList : ArgListH {
1520 | ArgListH ',' DOTDOTDOT {
1522 $$->push_back(std::pair<PATypeHolder*,
1523 char*>(new PATypeHolder(Type::VoidTy), 0));
1526 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1527 $$->push_back(std::make_pair(new PATypeHolder(Type::VoidTy), (char*)0));
1533 FunctionHeaderH : TypesV Name '(' ArgList ')' {
1535 std::string FunctionName($2);
1537 if (!(*$1)->isFirstClassType() && *$1 != Type::VoidTy)
1538 ThrowException("LLVM functions cannot return aggregate types!");
1540 std::vector<const Type*> ParamTypeList;
1541 if ($4) { // If there are arguments...
1542 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $4->begin();
1543 I != $4->end(); ++I)
1544 ParamTypeList.push_back(I->first->get());
1547 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1548 if (isVarArg) ParamTypeList.pop_back();
1550 const FunctionType *FT = FunctionType::get(*$1, ParamTypeList, isVarArg);
1551 const PointerType *PFT = PointerType::get(FT);
1554 // Is the function already in symtab?
1555 if (CurModule.CurrentModule->getFunction(FunctionName, FT))
1556 ThrowException("Redefinition of function '" + FunctionName + "'!");
1558 Function *Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName,
1559 CurModule.CurrentModule);
1560 InsertValue(Fn, CurModule.Values);
1561 CurModule.DeclareNewGlobalValue(Fn, ValID::create($2));
1562 free($2); // Free strdup'd memory!
1564 CurFun.FunctionStart(Fn);
1566 // Add all of the arguments we parsed to the function...
1567 if ($4) { // Is null if empty...
1568 if (isVarArg) { // Nuke the last entry
1569 assert($4->back().first->get() == Type::VoidTy && $4->back().second == 0&&
1570 "Not a varargs marker!");
1571 delete $4->back().first;
1572 $4->pop_back(); // Delete the last entry
1574 Function::aiterator ArgIt = Fn->abegin();
1575 for (std::vector<std::pair<PATypeHolder*, char*> >::iterator I =$4->begin();
1576 I != $4->end(); ++I, ++ArgIt) {
1577 delete I->first; // Delete the typeholder...
1579 setValueName(ArgIt, I->second); // Insert arg into symtab...
1583 delete $4; // We're now done with the argument list
1587 BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1589 FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
1590 $$ = CurFun.CurrentFunction;
1592 // Make sure that we keep track of the linkage type even if there was a
1593 // previous "declare".
1596 // Resolve circular types before we parse the body of the function.
1597 ResolveTypes(CurFun.LateResolveTypes);
1600 END : ENDTOK | '}'; // Allow end of '}' to end a function
1602 Function : BasicBlockList END {
1606 FunctionProto : DECLARE { CurFun.isDeclare = true; } FunctionHeaderH {
1607 $$ = CurFun.CurrentFunction;
1608 CurFun.FunctionDone();
1611 //===----------------------------------------------------------------------===//
1612 // Rules to match Basic Blocks
1613 //===----------------------------------------------------------------------===//
1615 ConstValueRef : ESINT64VAL { // A reference to a direct constant
1616 $$ = ValID::create($1);
1619 $$ = ValID::create($1);
1621 | FPVAL { // Perhaps it's an FP constant?
1622 $$ = ValID::create($1);
1625 $$ = ValID::create(ConstantBool::True);
1628 $$ = ValID::create(ConstantBool::False);
1631 $$ = ValID::createNull();
1634 $$ = ValID::create($1);
1637 // SymbolicValueRef - Reference to one of two ways of symbolically refering to
1640 SymbolicValueRef : INTVAL { // Is it an integer reference...?
1641 $$ = ValID::create($1);
1643 | Name { // Is it a named reference...?
1644 $$ = ValID::create($1);
1647 // ValueRef - A reference to a definition... either constant or symbolic
1648 ValueRef : SymbolicValueRef | ConstValueRef;
1651 // ResolvedVal - a <type> <value> pair. This is used only in cases where the
1652 // type immediately preceeds the value reference, and allows complex constant
1653 // pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
1654 ResolvedVal : Types ValueRef {
1655 $$ = getVal(*$1, $2); delete $1;
1658 BasicBlockList : BasicBlockList BasicBlock {
1661 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
1666 // Basic blocks are terminated by branching instructions:
1667 // br, br/cc, switch, ret
1669 BasicBlock : InstructionList OptAssign BBTerminatorInst {
1670 setValueName($3, $2);
1673 $1->getInstList().push_back($3);
1678 InstructionList : InstructionList Inst {
1679 $1->getInstList().push_back($2);
1683 $$ = CurBB = getBBVal(ValID::create((int)CurFun.NextBBNum++), true);
1686 $$ = CurBB = getBBVal(ValID::create($1), true);
1689 BBTerminatorInst : RET ResolvedVal { // Return with a result...
1690 $$ = new ReturnInst($2);
1692 | RET VOID { // Return with no result...
1693 $$ = new ReturnInst();
1695 | BR LABEL ValueRef { // Unconditional Branch...
1696 $$ = new BranchInst(getBBVal($3));
1697 } // Conditional Branch...
1698 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
1699 $$ = new BranchInst(getBBVal($6), getBBVal($9), getVal(Type::BoolTy, $3));
1701 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1702 SwitchInst *S = new SwitchInst(getVal($2, $3), getBBVal($6));
1705 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
1708 S->addCase(I->first, I->second);
1711 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
1712 SwitchInst *S = new SwitchInst(getVal($2, $3), getBBVal($6));
1715 | INVOKE TypesV ValueRef '(' ValueRefListE ')' TO LABEL ValueRef
1716 UNWIND LABEL ValueRef {
1717 const PointerType *PFTy;
1718 const FunctionType *Ty;
1720 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1721 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
1722 // Pull out the types of all of the arguments...
1723 std::vector<const Type*> ParamTypes;
1725 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1727 ParamTypes.push_back((*I)->getType());
1730 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1731 if (isVarArg) ParamTypes.pop_back();
1733 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
1734 PFTy = PointerType::get(Ty);
1737 Value *V = getVal(PFTy, $3); // Get the function we're calling...
1739 BasicBlock *Normal = getBBVal($9);
1740 BasicBlock *Except = getBBVal($12);
1742 // Create the call node...
1743 if (!$5) { // Has no arguments?
1744 $$ = new InvokeInst(V, Normal, Except, std::vector<Value*>());
1745 } else { // Has arguments?
1746 // Loop through FunctionType's arguments and ensure they are specified
1749 FunctionType::param_iterator I = Ty->param_begin();
1750 FunctionType::param_iterator E = Ty->param_end();
1751 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
1753 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1754 if ((*ArgI)->getType() != *I)
1755 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
1756 (*I)->getDescription() + "'!");
1758 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1759 ThrowException("Invalid number of parameters detected!");
1761 $$ = new InvokeInst(V, Normal, Except, *$5);
1767 $$ = new UnwindInst();
1772 JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1774 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
1776 ThrowException("May only switch on a constant pool value!");
1778 $$->push_back(std::make_pair(V, getBBVal($6)));
1780 | IntType ConstValueRef ',' LABEL ValueRef {
1781 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
1782 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
1785 ThrowException("May only switch on a constant pool value!");
1787 $$->push_back(std::make_pair(V, getBBVal($5)));
1790 Inst : OptAssign InstVal {
1791 // Is this definition named?? if so, assign the name...
1792 setValueName($2, $1);
1797 PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
1798 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
1799 $$->push_back(std::make_pair(getVal(*$1, $3), getBBVal($5)));
1802 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1804 $1->push_back(std::make_pair(getVal($1->front().first->getType(), $4),
1809 ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
1810 $$ = new std::vector<Value*>();
1813 | ValueRefList ',' ResolvedVal {
1818 // ValueRefListE - Just like ValueRefList, except that it may also be empty!
1819 ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
1821 InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
1822 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint())
1823 ThrowException("Arithmetic operator requires integer or FP operands!");
1824 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1826 ThrowException("binary operator returned null!");
1829 | LogicalOps Types ValueRef ',' ValueRef {
1830 if (!(*$2)->isIntegral())
1831 ThrowException("Logical operator requires integral operands!");
1832 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1834 ThrowException("binary operator returned null!");
1837 | SetCondOps Types ValueRef ',' ValueRef {
1838 $$ = new SetCondInst($1, getVal(*$2, $3), getVal(*$2, $5));
1840 ThrowException("binary operator returned null!");
1844 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
1845 << " Replacing with 'xor'.\n";
1847 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
1849 ThrowException("Expected integral type for not instruction!");
1851 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
1853 ThrowException("Could not create a xor instruction!");
1855 | ShiftOps ResolvedVal ',' ResolvedVal {
1856 if ($4->getType() != Type::UByteTy)
1857 ThrowException("Shift amount must be ubyte!");
1858 if (!$2->getType()->isInteger())
1859 ThrowException("Shift constant expression requires integer operand!");
1860 $$ = new ShiftInst($1, $2, $4);
1862 | CAST ResolvedVal TO Types {
1863 if (!$4->get()->isFirstClassType())
1864 ThrowException("cast instruction to a non-primitive type: '" +
1865 $4->get()->getDescription() + "'!");
1866 $$ = new CastInst($2, *$4);
1869 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1870 if ($2->getType() != Type::BoolTy)
1871 ThrowException("select condition must be boolean!");
1872 if ($4->getType() != $6->getType())
1873 ThrowException("select value types should match!");
1874 $$ = new SelectInst($2, $4, $6);
1876 | VA_ARG ResolvedVal ',' Types {
1877 // FIXME: This is emulation code for an obsolete syntax. This should be
1878 // removed at some point.
1879 if (!ObsoleteVarArgs) {
1880 std::cerr << "WARNING: this file uses obsolete features. "
1881 << "Assemble and disassemble to update it.\n";
1882 ObsoleteVarArgs = true;
1885 // First, load the valist...
1886 Instruction *CurVAList = new LoadInst($2, "");
1887 CurBB->getInstList().push_back(CurVAList);
1889 // Emit the vaarg instruction.
1890 $$ = new VAArgInst(CurVAList, *$4);
1892 // Now we must advance the pointer and update it in memory.
1893 Instruction *TheVANext = new VANextInst(CurVAList, *$4);
1894 CurBB->getInstList().push_back(TheVANext);
1896 CurBB->getInstList().push_back(new StoreInst(TheVANext, $2));
1899 | VAARG ResolvedVal ',' Types {
1900 $$ = new VAArgInst($2, *$4);
1903 | VANEXT ResolvedVal ',' Types {
1904 $$ = new VANextInst($2, *$4);
1908 const Type *Ty = $2->front().first->getType();
1909 if (!Ty->isFirstClassType())
1910 ThrowException("PHI node operands must be of first class type!");
1911 $$ = new PHINode(Ty);
1912 $$->op_reserve($2->size()*2);
1913 while ($2->begin() != $2->end()) {
1914 if ($2->front().first->getType() != Ty)
1915 ThrowException("All elements of a PHI node must be of the same type!");
1916 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
1919 delete $2; // Free the list...
1921 | CALL TypesV ValueRef '(' ValueRefListE ')' {
1922 const PointerType *PFTy;
1923 const FunctionType *Ty;
1925 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1926 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
1927 // Pull out the types of all of the arguments...
1928 std::vector<const Type*> ParamTypes;
1930 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1932 ParamTypes.push_back((*I)->getType());
1935 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1936 if (isVarArg) ParamTypes.pop_back();
1938 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
1939 PFTy = PointerType::get(Ty);
1942 Value *V = getVal(PFTy, $3); // Get the function we're calling...
1944 // Create the call node...
1945 if (!$5) { // Has no arguments?
1946 // Make sure no arguments is a good thing!
1947 if (Ty->getNumParams() != 0)
1948 ThrowException("No arguments passed to a function that "
1949 "expects arguments!");
1951 $$ = new CallInst(V, std::vector<Value*>());
1952 } else { // Has arguments?
1953 // Loop through FunctionType's arguments and ensure they are specified
1956 FunctionType::param_iterator I = Ty->param_begin();
1957 FunctionType::param_iterator E = Ty->param_end();
1958 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
1960 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1961 if ((*ArgI)->getType() != *I)
1962 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
1963 (*I)->getDescription() + "'!");
1965 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1966 ThrowException("Invalid number of parameters detected!");
1968 $$ = new CallInst(V, *$5);
1978 // IndexList - List of indices for GEP based instructions...
1979 IndexList : ',' ValueRefList {
1982 $$ = new std::vector<Value*>();
1985 OptVolatile : VOLATILE {
1993 MemoryInst : MALLOC Types {
1994 $$ = new MallocInst(*$2);
1997 | MALLOC Types ',' UINT ValueRef {
1998 $$ = new MallocInst(*$2, getVal($4, $5));
2002 $$ = new AllocaInst(*$2);
2005 | ALLOCA Types ',' UINT ValueRef {
2006 $$ = new AllocaInst(*$2, getVal($4, $5));
2009 | FREE ResolvedVal {
2010 if (!isa<PointerType>($2->getType()))
2011 ThrowException("Trying to free nonpointer type " +
2012 $2->getType()->getDescription() + "!");
2013 $$ = new FreeInst($2);
2016 | OptVolatile LOAD Types ValueRef {
2017 if (!isa<PointerType>($3->get()))
2018 ThrowException("Can't load from nonpointer type: " +
2019 (*$3)->getDescription());
2020 $$ = new LoadInst(getVal(*$3, $4), "", $1);
2023 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
2024 const PointerType *PT = dyn_cast<PointerType>($5->get());
2026 ThrowException("Can't store to a nonpointer type: " +
2027 (*$5)->getDescription());
2028 const Type *ElTy = PT->getElementType();
2029 if (ElTy != $3->getType())
2030 ThrowException("Can't store '" + $3->getType()->getDescription() +
2031 "' into space of type '" + ElTy->getDescription() + "'!");
2033 $$ = new StoreInst($3, getVal(*$5, $6), $1);
2036 | GETELEMENTPTR Types ValueRef IndexList {
2037 if (!isa<PointerType>($2->get()))
2038 ThrowException("getelementptr insn requires pointer operand!");
2040 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
2041 // indices to uint struct indices for compatibility.
2042 generic_gep_type_iterator<std::vector<Value*>::iterator>
2043 GTI = gep_type_begin($2->get(), $4->begin(), $4->end()),
2044 GTE = gep_type_end($2->get(), $4->begin(), $4->end());
2045 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
2046 if (isa<StructType>(*GTI)) // Only change struct indices
2047 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
2048 if (CUI->getType() == Type::UByteTy)
2049 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
2051 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
2052 ThrowException("Invalid getelementptr indices for type '" +
2053 (*$2)->getDescription()+ "'!");
2054 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
2055 delete $2; delete $4;
2060 int yyerror(const char *ErrorMsg) {
2062 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
2063 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
2064 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
2065 if (yychar == YYEMPTY || yychar == 0)
2066 errMsg += "end-of-file.";
2068 errMsg += "token: '" + std::string(llvmAsmtext, llvmAsmleng) + "'";
2069 ThrowException(errMsg);