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 // GetForwardRefForGlobal - Check to see if there is a forward reference
116 // for this global. If so, remove it from the GlobalRefs map and return it.
117 // If not, just return null.
118 GlobalValue *GetForwardRefForGlobal(const PointerType *PTy, ValID ID) {
119 // Check to see if there is a forward reference to this global variable...
120 // if there is, eliminate it and patch the reference to use the new def'n.
121 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(PTy, ID));
122 GlobalValue *Ret = 0;
123 if (I != GlobalRefs.end()) {
131 static struct PerFunctionInfo {
132 Function *CurrentFunction; // Pointer to current function being created
134 std::map<const Type*, ValueList> Values; // Keep track of #'d definitions
135 std::map<const Type*, ValueList> LateResolveValues;
136 std::vector<PATypeHolder> Types;
137 std::map<ValID, PATypeHolder> LateResolveTypes;
138 bool isDeclare; // Is this function a forward declararation?
140 /// BBForwardRefs - When we see forward references to basic blocks, keep
141 /// track of them here.
142 std::map<BasicBlock*, std::pair<ValID, int> > BBForwardRefs;
143 std::vector<BasicBlock*> NumberedBlocks;
146 inline PerFunctionInfo() {
151 inline void FunctionStart(Function *M) {
156 void FunctionDone() {
157 NumberedBlocks.clear();
159 // Any forward referenced blocks left?
160 if (!BBForwardRefs.empty())
161 ThrowException("Undefined reference to label " +
162 BBForwardRefs.begin()->second.first.getName());
164 // Resolve all forward references now.
165 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
167 // Make sure to resolve any constant expr references that might exist within
168 // the function we just declared itself.
170 if (CurrentFunction->hasName()) {
171 FID = ValID::create((char*)CurrentFunction->getName().c_str());
173 // Figure out which slot number if is...
174 ValueList &List = CurModule.Values[CurrentFunction->getType()];
175 for (unsigned i = 0; ; ++i) {
176 assert(i < List.size() && "Function not found!");
177 if (List[i] == CurrentFunction) {
178 FID = ValID::create((int)i);
184 Values.clear(); // Clear out function local definitions
185 Types.clear(); // Clear out function local types
189 } CurFun; // Info for the current function...
191 static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
194 //===----------------------------------------------------------------------===//
195 // Code to handle definitions of all the types
196 //===----------------------------------------------------------------------===//
198 static int InsertValue(Value *V,
199 std::map<const Type*,ValueList> &ValueTab = CurFun.Values) {
200 if (V->hasName()) return -1; // Is this a numbered definition?
202 // Yes, insert the value into the value table...
203 ValueList &List = ValueTab[V->getType()];
205 return List.size()-1;
208 static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
210 case ValID::NumberVal: // Is it a numbered definition?
211 // Module constants occupy the lowest numbered slots...
212 if ((unsigned)D.Num < CurModule.Types.size())
213 return CurModule.Types[(unsigned)D.Num];
215 case ValID::NameVal: // Is it a named definition?
216 if (const Type *N = CurModule.CurrentModule->getTypeByName(D.Name)) {
217 D.destroy(); // Free old strdup'd memory...
222 ThrowException("Internal parser error: Invalid symbol type reference!");
225 // If we reached here, we referenced either a symbol that we don't know about
226 // or an id number that hasn't been read yet. We may be referencing something
227 // forward, so just create an entry to be resolved later and get to it...
229 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
231 std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
232 CurFun.LateResolveTypes : CurModule.LateResolveTypes;
234 std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
235 if (I != LateResolver.end()) {
239 Type *Typ = OpaqueType::get();
240 LateResolver.insert(std::make_pair(D, Typ));
244 static Value *lookupInSymbolTable(const Type *Ty, const std::string &Name) {
245 SymbolTable &SymTab =
246 inFunctionScope() ? CurFun.CurrentFunction->getSymbolTable() :
247 CurModule.CurrentModule->getSymbolTable();
248 return SymTab.lookup(Ty, Name);
251 // getValNonImprovising - Look up the value specified by the provided type and
252 // the provided ValID. If the value exists and has already been defined, return
253 // it. Otherwise return null.
255 static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
256 if (isa<FunctionType>(Ty))
257 ThrowException("Functions are not values and "
258 "must be referenced as pointers");
261 case ValID::NumberVal: { // Is it a numbered definition?
262 unsigned Num = (unsigned)D.Num;
264 // Module constants occupy the lowest numbered slots...
265 std::map<const Type*,ValueList>::iterator VI = CurModule.Values.find(Ty);
266 if (VI != CurModule.Values.end()) {
267 if (Num < VI->second.size())
268 return VI->second[Num];
269 Num -= VI->second.size();
272 // Make sure that our type is within bounds
273 VI = CurFun.Values.find(Ty);
274 if (VI == CurFun.Values.end()) return 0;
276 // Check that the number is within bounds...
277 if (VI->second.size() <= Num) return 0;
279 return VI->second[Num];
282 case ValID::NameVal: { // Is it a named definition?
283 Value *N = lookupInSymbolTable(Ty, std::string(D.Name));
284 if (N == 0) return 0;
286 D.destroy(); // Free old strdup'd memory...
290 // Check to make sure that "Ty" is an integral type, and that our
291 // value will fit into the specified type...
292 case ValID::ConstSIntVal: // Is it a constant pool reference??
293 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64))
294 ThrowException("Signed integral constant '" +
295 itostr(D.ConstPool64) + "' is invalid for type '" +
296 Ty->getDescription() + "'!");
297 return ConstantSInt::get(Ty, D.ConstPool64);
299 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
300 if (!ConstantUInt::isValueValidForType(Ty, D.UConstPool64)) {
301 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64)) {
302 ThrowException("Integral constant '" + utostr(D.UConstPool64) +
303 "' is invalid or out of range!");
304 } else { // This is really a signed reference. Transmogrify.
305 return ConstantSInt::get(Ty, D.ConstPool64);
308 return ConstantUInt::get(Ty, D.UConstPool64);
311 case ValID::ConstFPVal: // Is it a floating point const pool reference?
312 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
313 ThrowException("FP constant invalid for type!!");
314 return ConstantFP::get(Ty, D.ConstPoolFP);
316 case ValID::ConstNullVal: // Is it a null value?
317 if (!isa<PointerType>(Ty))
318 ThrowException("Cannot create a a non pointer null!");
319 return ConstantPointerNull::get(cast<PointerType>(Ty));
321 case ValID::ConstantVal: // Fully resolved constant?
322 if (D.ConstantValue->getType() != Ty)
323 ThrowException("Constant expression type different from required type!");
324 return D.ConstantValue;
327 assert(0 && "Unhandled case!");
331 assert(0 && "Unhandled case!");
335 // getVal - This function is identical to getValNonImprovising, except that if a
336 // value is not already defined, it "improvises" by creating a placeholder var
337 // that looks and acts just like the requested variable. When the value is
338 // defined later, all uses of the placeholder variable are replaced with the
341 static Value *getVal(const Type *Ty, const ValID &ID) {
342 if (Ty == Type::LabelTy)
343 ThrowException("Cannot use a basic block here");
345 // See if the value has already been defined.
346 Value *V = getValNonImprovising(Ty, ID);
349 // If we reached here, we referenced either a symbol that we don't know about
350 // or an id number that hasn't been read yet. We may be referencing something
351 // forward, so just create an entry to be resolved later and get to it...
353 V = new Argument(Ty);
355 // Remember where this forward reference came from. FIXME, shouldn't we try
356 // to recycle these things??
357 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
360 if (inFunctionScope())
361 InsertValue(V, CurFun.LateResolveValues);
363 InsertValue(V, CurModule.LateResolveValues);
367 /// getBBVal - This is used for two purposes:
368 /// * If isDefinition is true, a new basic block with the specified ID is being
370 /// * If isDefinition is true, this is a reference to a basic block, which may
371 /// or may not be a forward reference.
373 static BasicBlock *getBBVal(const ValID &ID, bool isDefinition = false) {
374 assert(inFunctionScope() && "Can't get basic block at global scope!");
379 default: ThrowException("Illegal label reference " + ID.getName());
380 case ValID::NumberVal: // Is it a numbered definition?
381 if (unsigned(ID.Num) >= CurFun.NumberedBlocks.size())
382 CurFun.NumberedBlocks.resize(ID.Num+1);
383 BB = CurFun.NumberedBlocks[ID.Num];
385 case ValID::NameVal: // Is it a named definition?
387 if (Value *N = CurFun.CurrentFunction->
388 getSymbolTable().lookup(Type::LabelTy, Name))
389 BB = cast<BasicBlock>(N);
393 // See if the block has already been defined.
395 // If this is the definition of the block, make sure the existing value was
396 // just a forward reference. If it was a forward reference, there will be
397 // an entry for it in the PlaceHolderInfo map.
398 if (isDefinition && !CurFun.BBForwardRefs.erase(BB))
399 // The existing value was a definition, not a forward reference.
400 ThrowException("Redefinition of label " + ID.getName());
402 ID.destroy(); // Free strdup'd memory.
406 // Otherwise this block has not been seen before.
407 BB = new BasicBlock("", CurFun.CurrentFunction);
408 if (ID.Type == ValID::NameVal) {
409 BB->setName(ID.Name);
411 CurFun.NumberedBlocks[ID.Num] = BB;
414 // If this is not a definition, keep track of it so we can use it as a forward
417 // Remember where this forward reference came from.
418 CurFun.BBForwardRefs[BB] = std::make_pair(ID, llvmAsmlineno);
420 // The forward declaration could have been inserted anywhere in the
421 // function: insert it into the correct place now.
422 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
423 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
430 //===----------------------------------------------------------------------===//
431 // Code to handle forward references in instructions
432 //===----------------------------------------------------------------------===//
434 // This code handles the late binding needed with statements that reference
435 // values not defined yet... for example, a forward branch, or the PHI node for
438 // This keeps a table (CurFun.LateResolveValues) of all such forward references
439 // and back patchs after we are done.
442 // ResolveDefinitions - If we could not resolve some defs at parsing
443 // time (forward branches, phi functions for loops, etc...) resolve the
446 static void ResolveDefinitions(std::map<const Type*,ValueList> &LateResolvers,
447 std::map<const Type*,ValueList> *FutureLateResolvers) {
448 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
449 for (std::map<const Type*,ValueList>::iterator LRI = LateResolvers.begin(),
450 E = LateResolvers.end(); LRI != E; ++LRI) {
451 ValueList &List = LRI->second;
452 while (!List.empty()) {
453 Value *V = List.back();
456 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
457 CurModule.PlaceHolderInfo.find(V);
458 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
460 ValID &DID = PHI->second.first;
462 Value *TheRealValue = getValNonImprovising(LRI->first, DID);
464 V->replaceAllUsesWith(TheRealValue);
466 CurModule.PlaceHolderInfo.erase(PHI);
467 } else if (FutureLateResolvers) {
468 // Functions have their unresolved items forwarded to the module late
470 InsertValue(V, *FutureLateResolvers);
472 if (DID.Type == ValID::NameVal)
473 ThrowException("Reference to an invalid definition: '" +DID.getName()+
474 "' of type '" + V->getType()->getDescription() + "'",
477 ThrowException("Reference to an invalid definition: #" +
478 itostr(DID.Num) + " of type '" +
479 V->getType()->getDescription() + "'",
485 LateResolvers.clear();
488 // ResolveTypeTo - A brand new type was just declared. This means that (if
489 // name is not null) things referencing Name can be resolved. Otherwise, things
490 // refering to the number can be resolved. Do this now.
492 static void ResolveTypeTo(char *Name, const Type *ToTy) {
493 std::vector<PATypeHolder> &Types = inFunctionScope() ?
494 CurFun.Types : CurModule.Types;
497 if (Name) D = ValID::create(Name);
498 else D = ValID::create((int)Types.size());
500 std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
501 CurFun.LateResolveTypes : CurModule.LateResolveTypes;
503 std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
504 if (I != LateResolver.end()) {
505 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
506 LateResolver.erase(I);
510 // ResolveTypes - At this point, all types should be resolved. Any that aren't
513 static void ResolveTypes(std::map<ValID, PATypeHolder> &LateResolveTypes) {
514 if (!LateResolveTypes.empty()) {
515 const ValID &DID = LateResolveTypes.begin()->first;
517 if (DID.Type == ValID::NameVal)
518 ThrowException("Reference to an invalid type: '" +DID.getName() + "'");
520 ThrowException("Reference to an invalid type: #" + itostr(DID.Num));
524 // setValueName - Set the specified value to the name given. The name may be
525 // null potentially, in which case this is a noop. The string passed in is
526 // assumed to be a malloc'd string buffer, and is free'd by this function.
528 static void setValueName(Value *V, char *NameStr) {
530 std::string Name(NameStr); // Copy string
531 free(NameStr); // Free old string
533 if (V->getType() == Type::VoidTy)
534 ThrowException("Can't assign name '" + Name+"' to value with void type!");
536 assert(inFunctionScope() && "Must be in function scope!");
537 SymbolTable &ST = CurFun.CurrentFunction->getSymbolTable();
538 if (ST.lookup(V->getType(), Name))
539 ThrowException("Redefinition of value named '" + Name + "' in the '" +
540 V->getType()->getDescription() + "' type plane!");
543 V->setName(Name, &ST);
547 /// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
548 /// this is a declaration, otherwise it is a definition.
549 static void ParseGlobalVariable(char *NameStr,GlobalValue::LinkageTypes Linkage,
550 bool isConstantGlobal, const Type *Ty,
551 Constant *Initializer) {
552 if (isa<FunctionType>(Ty))
553 ThrowException("Cannot declare global vars of function type!");
555 const PointerType *PTy = PointerType::get(Ty);
559 Name = NameStr; // Copy string
560 free(NameStr); // Free old string
563 // See if this global value was forward referenced. If so, recycle the
567 ID = ValID::create((char*)Name.c_str());
569 ID = ValID::create((int)CurModule.Values[PTy].size());
572 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
573 // Move the global to the end of the list, from whereever it was
574 // previously inserted.
575 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
576 CurModule.CurrentModule->getGlobalList().remove(GV);
577 CurModule.CurrentModule->getGlobalList().push_back(GV);
578 GV->setInitializer(Initializer);
579 GV->setLinkage(Linkage);
580 GV->setConstant(isConstantGlobal);
584 // If this global has a name, check to see if there is already a definition
585 // of this global in the module. If so, merge as appropriate. Note that
586 // this is really just a hack around problems in the CFE. :(
588 // We are a simple redefinition of a value, check to see if it is defined
589 // the same as the old one.
590 if (GlobalVariable *EGV =
591 CurModule.CurrentModule->getGlobalVariable(Name, Ty)) {
592 // We are allowed to redefine a global variable in two circumstances:
593 // 1. If at least one of the globals is uninitialized or
594 // 2. If both initializers have the same value.
596 if (!EGV->hasInitializer() || !Initializer ||
597 EGV->getInitializer() == Initializer) {
599 // Make sure the existing global version gets the initializer! Make
600 // sure that it also gets marked const if the new version is.
601 if (Initializer && !EGV->hasInitializer())
602 EGV->setInitializer(Initializer);
603 if (isConstantGlobal)
604 EGV->setConstant(true);
605 EGV->setLinkage(Linkage);
609 ThrowException("Redefinition of global variable named '" + Name +
610 "' in the '" + Ty->getDescription() + "' type plane!");
614 // Otherwise there is no existing GV to use, create one now.
615 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
616 CurModule.CurrentModule);
619 // setTypeName - Set the specified type to the name given. The name may be
620 // null potentially, in which case this is a noop. The string passed in is
621 // assumed to be a malloc'd string buffer, and is freed by this function.
623 // This function returns true if the type has already been defined, but is
624 // allowed to be redefined in the specified context. If the name is a new name
625 // for the type plane, it is inserted and false is returned.
626 static bool setTypeName(const Type *T, char *NameStr) {
627 assert(!inFunctionScope() && "Can't give types function-local names!");
628 if (NameStr == 0) return false;
630 std::string Name(NameStr); // Copy string
631 free(NameStr); // Free old string
633 // We don't allow assigning names to void type
634 if (T == Type::VoidTy)
635 ThrowException("Can't assign name '" + Name + "' to the void type!");
637 // Set the type name, checking for conflicts as we do so.
638 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
640 if (AlreadyExists) { // Inserting a name that is already defined???
641 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
642 assert(Existing && "Conflict but no matching type?");
644 // There is only one case where this is allowed: when we are refining an
645 // opaque type. In this case, Existing will be an opaque type.
646 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
647 // We ARE replacing an opaque type!
648 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
652 // Otherwise, this is an attempt to redefine a type. That's okay if
653 // the redefinition is identical to the original. This will be so if
654 // Existing and T point to the same Type object. In this one case we
655 // allow the equivalent redefinition.
656 if (Existing == T) return true; // Yes, it's equal.
658 // Any other kind of (non-equivalent) redefinition is an error.
659 ThrowException("Redefinition of type named '" + Name + "' in the '" +
660 T->getDescription() + "' type plane!");
666 //===----------------------------------------------------------------------===//
667 // Code for handling upreferences in type names...
670 // TypeContains - Returns true if Ty directly contains E in it.
672 static bool TypeContains(const Type *Ty, const Type *E) {
673 return find(Ty->subtype_begin(), Ty->subtype_end(), E) != Ty->subtype_end();
678 // NestingLevel - The number of nesting levels that need to be popped before
679 // this type is resolved.
680 unsigned NestingLevel;
682 // LastContainedTy - This is the type at the current binding level for the
683 // type. Every time we reduce the nesting level, this gets updated.
684 const Type *LastContainedTy;
686 // UpRefTy - This is the actual opaque type that the upreference is
690 UpRefRecord(unsigned NL, OpaqueType *URTy)
691 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
695 // UpRefs - A list of the outstanding upreferences that need to be resolved.
696 static std::vector<UpRefRecord> UpRefs;
698 /// HandleUpRefs - Every time we finish a new layer of types, this function is
699 /// called. It loops through the UpRefs vector, which is a list of the
700 /// currently active types. For each type, if the up reference is contained in
701 /// the newly completed type, we decrement the level count. When the level
702 /// count reaches zero, the upreferenced type is the type that is passed in:
703 /// thus we can complete the cycle.
705 static PATypeHolder HandleUpRefs(const Type *ty) {
706 if (!ty->isAbstract()) return ty;
708 UR_OUT("Type '" << Ty->getDescription() <<
709 "' newly formed. Resolving upreferences.\n" <<
710 UpRefs.size() << " upreferences active!\n");
712 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
713 // to zero), we resolve them all together before we resolve them to Ty. At
714 // the end of the loop, if there is anything to resolve to Ty, it will be in
716 OpaqueType *TypeToResolve = 0;
718 for (unsigned i = 0; i != UpRefs.size(); ++i) {
719 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
720 << UpRefs[i].second->getDescription() << ") = "
721 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
722 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
723 // Decrement level of upreference
724 unsigned Level = --UpRefs[i].NestingLevel;
725 UpRefs[i].LastContainedTy = Ty;
726 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
727 if (Level == 0) { // Upreference should be resolved!
728 if (!TypeToResolve) {
729 TypeToResolve = UpRefs[i].UpRefTy;
731 UR_OUT(" * Resolving upreference for "
732 << UpRefs[i].second->getDescription() << "\n";
733 std::string OldName = UpRefs[i].UpRefTy->getDescription());
734 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
735 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
736 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
738 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
739 --i; // Do not skip the next element...
745 UR_OUT(" * Resolving upreference for "
746 << UpRefs[i].second->getDescription() << "\n";
747 std::string OldName = TypeToResolve->getDescription());
748 TypeToResolve->refineAbstractTypeTo(Ty);
755 //===----------------------------------------------------------------------===//
756 // RunVMAsmParser - Define an interface to this parser
757 //===----------------------------------------------------------------------===//
759 Module *llvm::RunVMAsmParser(const std::string &Filename, FILE *F) {
761 CurFilename = Filename;
762 llvmAsmlineno = 1; // Reset the current line number...
763 ObsoleteVarArgs = false;
765 // Allocate a new module to read
766 CurModule.CurrentModule = new Module(Filename);
768 yyparse(); // Parse the file, potentially throwing exception
770 Module *Result = ParserResult;
772 // Check to see if they called va_start but not va_arg..
773 if (!ObsoleteVarArgs)
774 if (Function *F = Result->getNamedFunction("llvm.va_start"))
775 if (F->asize() == 1) {
776 std::cerr << "WARNING: this file uses obsolete features. "
777 << "Assemble and disassemble to update it.\n";
778 ObsoleteVarArgs = true;
781 if (ObsoleteVarArgs) {
782 // If the user is making use of obsolete varargs intrinsics, adjust them for
784 if (Function *F = Result->getNamedFunction("llvm.va_start")) {
785 assert(F->asize() == 1 && "Obsolete va_start takes 1 argument!");
787 const Type *RetTy = F->getFunctionType()->getParamType(0);
788 RetTy = cast<PointerType>(RetTy)->getElementType();
789 Function *NF = Result->getOrInsertFunction("llvm.va_start", RetTy, 0);
791 while (!F->use_empty()) {
792 CallInst *CI = cast<CallInst>(F->use_back());
793 Value *V = new CallInst(NF, "", CI);
794 new StoreInst(V, CI->getOperand(1), CI);
795 CI->getParent()->getInstList().erase(CI);
797 Result->getFunctionList().erase(F);
800 if (Function *F = Result->getNamedFunction("llvm.va_end")) {
801 assert(F->asize() == 1 && "Obsolete va_end takes 1 argument!");
802 const Type *ArgTy = F->getFunctionType()->getParamType(0);
803 ArgTy = cast<PointerType>(ArgTy)->getElementType();
804 Function *NF = Result->getOrInsertFunction("llvm.va_end", Type::VoidTy,
807 while (!F->use_empty()) {
808 CallInst *CI = cast<CallInst>(F->use_back());
809 Value *V = new LoadInst(CI->getOperand(1), "", CI);
810 new CallInst(NF, V, "", CI);
811 CI->getParent()->getInstList().erase(CI);
813 Result->getFunctionList().erase(F);
816 if (Function *F = Result->getNamedFunction("llvm.va_copy")) {
817 assert(F->asize() == 2 && "Obsolete va_copy takes 2 argument!");
818 const Type *ArgTy = F->getFunctionType()->getParamType(0);
819 ArgTy = cast<PointerType>(ArgTy)->getElementType();
820 Function *NF = Result->getOrInsertFunction("llvm.va_copy", ArgTy,
823 while (!F->use_empty()) {
824 CallInst *CI = cast<CallInst>(F->use_back());
825 Value *V = new CallInst(NF, CI->getOperand(2), "", CI);
826 new StoreInst(V, CI->getOperand(1), CI);
827 CI->getParent()->getInstList().erase(CI);
829 Result->getFunctionList().erase(F);
833 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
842 llvm::Module *ModuleVal;
843 llvm::Function *FunctionVal;
844 std::pair<llvm::PATypeHolder*, char*> *ArgVal;
845 llvm::BasicBlock *BasicBlockVal;
846 llvm::TerminatorInst *TermInstVal;
847 llvm::Instruction *InstVal;
848 llvm::Constant *ConstVal;
850 const llvm::Type *PrimType;
851 llvm::PATypeHolder *TypeVal;
852 llvm::Value *ValueVal;
854 std::vector<std::pair<llvm::PATypeHolder*,char*> > *ArgList;
855 std::vector<llvm::Value*> *ValueList;
856 std::list<llvm::PATypeHolder> *TypeList;
857 std::list<std::pair<llvm::Value*,
858 llvm::BasicBlock*> > *PHIList; // Represent the RHS of PHI node
859 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
860 std::vector<llvm::Constant*> *ConstVector;
862 llvm::GlobalValue::LinkageTypes Linkage;
870 char *StrVal; // This memory is strdup'd!
871 llvm::ValID ValIDVal; // strdup'd memory maybe!
873 llvm::Instruction::BinaryOps BinaryOpVal;
874 llvm::Instruction::TermOps TermOpVal;
875 llvm::Instruction::MemoryOps MemOpVal;
876 llvm::Instruction::OtherOps OtherOpVal;
877 llvm::Module::Endianness Endianness;
880 %type <ModuleVal> Module FunctionList
881 %type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
882 %type <BasicBlockVal> BasicBlock InstructionList
883 %type <TermInstVal> BBTerminatorInst
884 %type <InstVal> Inst InstVal MemoryInst
885 %type <ConstVal> ConstVal ConstExpr
886 %type <ConstVector> ConstVector
887 %type <ArgList> ArgList ArgListH
888 %type <ArgVal> ArgVal
889 %type <PHIList> PHIList
890 %type <ValueList> ValueRefList ValueRefListE // For call param lists
891 %type <ValueList> IndexList // For GEP derived indices
892 %type <TypeList> TypeListI ArgTypeListI
893 %type <JumpTable> JumpTable
894 %type <BoolVal> GlobalType // GLOBAL or CONSTANT?
895 %type <BoolVal> OptVolatile // 'volatile' or not
896 %type <Linkage> OptLinkage
897 %type <Endianness> BigOrLittle
899 // ValueRef - Unresolved reference to a definition or BB
900 %type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
901 %type <ValueVal> ResolvedVal // <type> <valref> pair
902 // Tokens and types for handling constant integer values
904 // ESINT64VAL - A negative number within long long range
905 %token <SInt64Val> ESINT64VAL
907 // EUINT64VAL - A positive number within uns. long long range
908 %token <UInt64Val> EUINT64VAL
909 %type <SInt64Val> EINT64VAL
911 %token <SIntVal> SINTVAL // Signed 32 bit ints...
912 %token <UIntVal> UINTVAL // Unsigned 32 bit ints...
913 %type <SIntVal> INTVAL
914 %token <FPVal> FPVAL // Float or Double constant
917 %type <TypeVal> Types TypesV UpRTypes UpRTypesV
918 %type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
919 %token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
920 %token <PrimType> FLOAT DOUBLE TYPE LABEL
922 %token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
923 %type <StrVal> Name OptName OptAssign
926 %token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
927 %token DECLARE GLOBAL CONSTANT VOLATILE
928 %token TO DOTDOTDOT NULL_TOK CONST INTERNAL LINKONCE WEAK APPENDING
929 %token OPAQUE NOT EXTERNAL TARGET ENDIAN POINTERSIZE LITTLE BIG
931 // Basic Block Terminating Operators
932 %token <TermOpVal> RET BR SWITCH INVOKE UNWIND
935 %type <BinaryOpVal> BinaryOps // all the binary operators
936 %type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
937 %token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
938 %token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
940 // Memory Instructions
941 %token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
944 %type <OtherOpVal> ShiftOps
945 %token <OtherOpVal> PHI_TOK CALL CAST SELECT SHL SHR VAARG VANEXT
946 %token VA_ARG // FIXME: OBSOLETE
951 // Handle constant integer size restriction and conversion...
955 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
956 ThrowException("Value too large for type!");
961 EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
962 EINT64VAL : EUINT64VAL {
963 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
964 ThrowException("Value too large for type!");
968 // Operations that are notably excluded from this list include:
969 // RET, BR, & SWITCH because they end basic blocks and are treated specially.
971 ArithmeticOps: ADD | SUB | MUL | DIV | REM;
972 LogicalOps : AND | OR | XOR;
973 SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
974 BinaryOps : ArithmeticOps | LogicalOps | SetCondOps;
976 ShiftOps : SHL | SHR;
978 // These are some types that allow classification if we only want a particular
979 // thing... for example, only a signed, unsigned, or integral type.
980 SIntType : LONG | INT | SHORT | SBYTE;
981 UIntType : ULONG | UINT | USHORT | UBYTE;
982 IntType : SIntType | UIntType;
983 FPType : FLOAT | DOUBLE;
985 // OptAssign - Value producing statements have an optional assignment component
986 OptAssign : Name '=' {
993 OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
994 LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
995 WEAK { $$ = GlobalValue::WeakLinkage; } |
996 APPENDING { $$ = GlobalValue::AppendingLinkage; } |
997 /*empty*/ { $$ = GlobalValue::ExternalLinkage; };
999 //===----------------------------------------------------------------------===//
1000 // Types includes all predefined types... except void, because it can only be
1001 // used in specific contexts (function returning void for example). To have
1002 // access to it, a user must explicitly use TypesV.
1005 // TypesV includes all of 'Types', but it also includes the void type.
1006 TypesV : Types | VOID { $$ = new PATypeHolder($1); };
1007 UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
1010 if (!UpRefs.empty())
1011 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
1016 // Derived types are added later...
1018 PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
1019 PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
1021 $$ = new PATypeHolder(OpaqueType::get());
1024 $$ = new PATypeHolder($1);
1026 UpRTypes : SymbolicValueRef { // Named types are also simple types...
1027 $$ = new PATypeHolder(getTypeVal($1));
1030 // Include derived types in the Types production.
1032 UpRTypes : '\\' EUINT64VAL { // Type UpReference
1033 if ($2 > (uint64_t)~0U) ThrowException("Value out of range!");
1034 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
1035 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
1036 $$ = new PATypeHolder(OT);
1037 UR_OUT("New Upreference!\n");
1039 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
1040 std::vector<const Type*> Params;
1041 mapto($3->begin(), $3->end(), std::back_inserter(Params),
1042 std::mem_fun_ref(&PATypeHolder::get));
1043 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1044 if (isVarArg) Params.pop_back();
1046 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
1047 delete $3; // Delete the argument list
1048 delete $1; // Delete the return type handle
1050 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
1051 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
1054 | '{' TypeListI '}' { // Structure type?
1055 std::vector<const Type*> Elements;
1056 mapto($2->begin(), $2->end(), std::back_inserter(Elements),
1057 std::mem_fun_ref(&PATypeHolder::get));
1059 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
1062 | '{' '}' { // Empty structure type?
1063 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
1065 | UpRTypes '*' { // Pointer type?
1066 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
1070 // TypeList - Used for struct declarations and as a basis for function type
1071 // declaration type lists
1073 TypeListI : UpRTypes {
1074 $$ = new std::list<PATypeHolder>();
1075 $$->push_back(*$1); delete $1;
1077 | TypeListI ',' UpRTypes {
1078 ($$=$1)->push_back(*$3); delete $3;
1081 // ArgTypeList - List of types for a function type declaration...
1082 ArgTypeListI : TypeListI
1083 | TypeListI ',' DOTDOTDOT {
1084 ($$=$1)->push_back(Type::VoidTy);
1087 ($$ = new std::list<PATypeHolder>())->push_back(Type::VoidTy);
1090 $$ = new std::list<PATypeHolder>();
1093 // ConstVal - The various declarations that go into the constant pool. This
1094 // production is used ONLY to represent constants that show up AFTER a 'const',
1095 // 'constant' or 'global' token at global scope. Constants that can be inlined
1096 // into other expressions (such as integers and constexprs) are handled by the
1097 // ResolvedVal, ValueRef and ConstValueRef productions.
1099 ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
1100 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
1102 ThrowException("Cannot make array constant with type: '" +
1103 (*$1)->getDescription() + "'!");
1104 const Type *ETy = ATy->getElementType();
1105 int NumElements = ATy->getNumElements();
1107 // Verify that we have the correct size...
1108 if (NumElements != -1 && NumElements != (int)$3->size())
1109 ThrowException("Type mismatch: constant sized array initialized with " +
1110 utostr($3->size()) + " arguments, but has size of " +
1111 itostr(NumElements) + "!");
1113 // Verify all elements are correct type!
1114 for (unsigned i = 0; i < $3->size(); i++) {
1115 if (ETy != (*$3)[i]->getType())
1116 ThrowException("Element #" + utostr(i) + " is not of type '" +
1117 ETy->getDescription() +"' as required!\nIt is of type '"+
1118 (*$3)[i]->getType()->getDescription() + "'.");
1121 $$ = ConstantArray::get(ATy, *$3);
1122 delete $1; delete $3;
1125 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
1127 ThrowException("Cannot make array constant with type: '" +
1128 (*$1)->getDescription() + "'!");
1130 int NumElements = ATy->getNumElements();
1131 if (NumElements != -1 && NumElements != 0)
1132 ThrowException("Type mismatch: constant sized array initialized with 0"
1133 " arguments, but has size of " + itostr(NumElements) +"!");
1134 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
1137 | Types 'c' STRINGCONSTANT {
1138 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
1140 ThrowException("Cannot make array constant with type: '" +
1141 (*$1)->getDescription() + "'!");
1143 int NumElements = ATy->getNumElements();
1144 const Type *ETy = ATy->getElementType();
1145 char *EndStr = UnEscapeLexed($3, true);
1146 if (NumElements != -1 && NumElements != (EndStr-$3))
1147 ThrowException("Can't build string constant of size " +
1148 itostr((int)(EndStr-$3)) +
1149 " when array has size " + itostr(NumElements) + "!");
1150 std::vector<Constant*> Vals;
1151 if (ETy == Type::SByteTy) {
1152 for (char *C = $3; C != EndStr; ++C)
1153 Vals.push_back(ConstantSInt::get(ETy, *C));
1154 } else if (ETy == Type::UByteTy) {
1155 for (char *C = $3; C != EndStr; ++C)
1156 Vals.push_back(ConstantUInt::get(ETy, (unsigned char)*C));
1159 ThrowException("Cannot build string arrays of non byte sized elements!");
1162 $$ = ConstantArray::get(ATy, Vals);
1165 | Types '{' ConstVector '}' {
1166 const StructType *STy = dyn_cast<StructType>($1->get());
1168 ThrowException("Cannot make struct constant with type: '" +
1169 (*$1)->getDescription() + "'!");
1171 if ($3->size() != STy->getNumContainedTypes())
1172 ThrowException("Illegal number of initializers for structure type!");
1174 // Check to ensure that constants are compatible with the type initializer!
1175 for (unsigned i = 0, e = $3->size(); i != e; ++i)
1176 if ((*$3)[i]->getType() != STy->getElementType(i))
1177 ThrowException("Expected type '" +
1178 STy->getElementType(i)->getDescription() +
1179 "' for element #" + utostr(i) +
1180 " of structure initializer!");
1182 $$ = ConstantStruct::get(STy, *$3);
1183 delete $1; delete $3;
1186 const StructType *STy = dyn_cast<StructType>($1->get());
1188 ThrowException("Cannot make struct constant with type: '" +
1189 (*$1)->getDescription() + "'!");
1191 if (STy->getNumContainedTypes() != 0)
1192 ThrowException("Illegal number of initializers for structure type!");
1194 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1198 const PointerType *PTy = dyn_cast<PointerType>($1->get());
1200 ThrowException("Cannot make null pointer constant with type: '" +
1201 (*$1)->getDescription() + "'!");
1203 $$ = ConstantPointerNull::get(PTy);
1206 | Types SymbolicValueRef {
1207 const PointerType *Ty = dyn_cast<PointerType>($1->get());
1209 ThrowException("Global const reference must be a pointer type!");
1211 // ConstExprs can exist in the body of a function, thus creating
1212 // ConstantPointerRefs whenever they refer to a variable. Because we are in
1213 // the context of a function, getValNonImprovising will search the functions
1214 // symbol table instead of the module symbol table for the global symbol,
1215 // which throws things all off. To get around this, we just tell
1216 // getValNonImprovising that we are at global scope here.
1218 Function *SavedCurFn = CurFun.CurrentFunction;
1219 CurFun.CurrentFunction = 0;
1221 Value *V = getValNonImprovising(Ty, $2);
1223 CurFun.CurrentFunction = SavedCurFn;
1225 // If this is an initializer for a constant pointer, which is referencing a
1226 // (currently) undefined variable, create a stub now that shall be replaced
1227 // in the future with the right type of variable.
1230 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1231 const PointerType *PT = cast<PointerType>(Ty);
1233 // First check to see if the forward references value is already created!
1234 PerModuleInfo::GlobalRefsType::iterator I =
1235 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
1237 if (I != CurModule.GlobalRefs.end()) {
1238 V = I->second; // Placeholder already exists, use it...
1242 if ($2.Type == ValID::NameVal) Name = $2.Name;
1244 // Create the forward referenced global.
1246 if (const FunctionType *FTy =
1247 dyn_cast<FunctionType>(PT->getElementType())) {
1248 GV = new Function(FTy, GlobalValue::ExternalLinkage, Name,
1249 CurModule.CurrentModule);
1251 GV = new GlobalVariable(PT->getElementType(), false,
1252 GlobalValue::ExternalLinkage, 0,
1253 Name, CurModule.CurrentModule);
1256 // Keep track of the fact that we have a forward ref to recycle it
1257 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1262 GlobalValue *GV = cast<GlobalValue>(V);
1263 $$ = ConstantPointerRef::get(GV);
1264 delete $1; // Free the type handle
1267 if ($1->get() != $2->getType())
1268 ThrowException("Mismatched types for constant expression!");
1272 | Types ZEROINITIALIZER {
1273 $$ = Constant::getNullValue($1->get());
1277 ConstVal : SIntType EINT64VAL { // integral constants
1278 if (!ConstantSInt::isValueValidForType($1, $2))
1279 ThrowException("Constant value doesn't fit in type!");
1280 $$ = ConstantSInt::get($1, $2);
1282 | UIntType EUINT64VAL { // integral constants
1283 if (!ConstantUInt::isValueValidForType($1, $2))
1284 ThrowException("Constant value doesn't fit in type!");
1285 $$ = ConstantUInt::get($1, $2);
1287 | BOOL TRUETOK { // Boolean constants
1288 $$ = ConstantBool::True;
1290 | BOOL FALSETOK { // Boolean constants
1291 $$ = ConstantBool::False;
1293 | FPType FPVAL { // Float & Double constants
1294 $$ = ConstantFP::get($1, $2);
1298 ConstExpr: CAST '(' ConstVal TO Types ')' {
1299 if (!$3->getType()->isFirstClassType())
1300 ThrowException("cast constant expression from a non-primitive type: '" +
1301 $3->getType()->getDescription() + "'!");
1302 if (!$5->get()->isFirstClassType())
1303 ThrowException("cast constant expression to a non-primitive type: '" +
1304 $5->get()->getDescription() + "'!");
1305 $$ = ConstantExpr::getCast($3, $5->get());
1308 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1309 if (!isa<PointerType>($3->getType()))
1310 ThrowException("GetElementPtr requires a pointer operand!");
1312 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
1313 // indices to uint struct indices for compatibility.
1314 generic_gep_type_iterator<std::vector<Value*>::iterator>
1315 GTI = gep_type_begin($3->getType(), $4->begin(), $4->end()),
1316 GTE = gep_type_end($3->getType(), $4->begin(), $4->end());
1317 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
1318 if (isa<StructType>(*GTI)) // Only change struct indices
1319 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
1320 if (CUI->getType() == Type::UByteTy)
1321 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
1324 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
1326 ThrowException("Index list invalid for constant getelementptr!");
1328 std::vector<Constant*> IdxVec;
1329 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1330 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
1331 IdxVec.push_back(C);
1333 ThrowException("Indices to constant getelementptr must be constants!");
1337 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
1339 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1340 if ($3->getType() != Type::BoolTy)
1341 ThrowException("Select condition must be of boolean type!");
1342 if ($5->getType() != $7->getType())
1343 ThrowException("Select operand types must match!");
1344 $$ = ConstantExpr::getSelect($3, $5, $7);
1346 | BinaryOps '(' ConstVal ',' ConstVal ')' {
1347 if ($3->getType() != $5->getType())
1348 ThrowException("Binary operator types must match!");
1349 $$ = ConstantExpr::get($1, $3, $5);
1351 | ShiftOps '(' ConstVal ',' ConstVal ')' {
1352 if ($5->getType() != Type::UByteTy)
1353 ThrowException("Shift count for shift constant must be unsigned byte!");
1354 if (!$3->getType()->isInteger())
1355 ThrowException("Shift constant expression requires integer operand!");
1356 $$ = ConstantExpr::get($1, $3, $5);
1360 // ConstVector - A list of comma separated constants.
1361 ConstVector : ConstVector ',' ConstVal {
1362 ($$ = $1)->push_back($3);
1365 $$ = new std::vector<Constant*>();
1370 // GlobalType - Match either GLOBAL or CONSTANT for global declarations...
1371 GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
1374 //===----------------------------------------------------------------------===//
1375 // Rules to match Modules
1376 //===----------------------------------------------------------------------===//
1378 // Module rule: Capture the result of parsing the whole file into a result
1381 Module : FunctionList {
1382 $$ = ParserResult = $1;
1383 CurModule.ModuleDone();
1386 // FunctionList - A list of functions, preceeded by a constant pool.
1388 FunctionList : FunctionList Function {
1390 CurFun.FunctionDone();
1392 | FunctionList FunctionProto {
1395 | FunctionList IMPLEMENTATION {
1399 $$ = CurModule.CurrentModule;
1400 // Resolve circular types before we parse the body of the module
1401 ResolveTypes(CurModule.LateResolveTypes);
1404 // ConstPool - Constants with optional names assigned to them.
1405 ConstPool : ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
1406 // Eagerly resolve types. This is not an optimization, this is a
1407 // requirement that is due to the fact that we could have this:
1409 // %list = type { %list * }
1410 // %list = type { %list * } ; repeated type decl
1412 // If types are not resolved eagerly, then the two types will not be
1413 // determined to be the same type!
1415 ResolveTypeTo($2, *$4);
1417 if (!setTypeName(*$4, $2) && !$2) {
1418 // If this is a named type that is not a redefinition, add it to the slot
1420 if (inFunctionScope())
1421 CurFun.Types.push_back(*$4);
1423 CurModule.Types.push_back(*$4);
1428 | ConstPool FunctionProto { // Function prototypes can be in const pool
1430 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
1431 if ($5 == 0) ThrowException("Global value initializer is not a constant!");
1432 ParseGlobalVariable($2, $3, $4, $5->getType(), $5);
1434 | ConstPool OptAssign EXTERNAL GlobalType Types {
1435 ParseGlobalVariable($2, GlobalValue::ExternalLinkage, $4, *$5, 0);
1438 | ConstPool TARGET TargetDefinition {
1440 | /* empty: end of list */ {
1445 BigOrLittle : BIG { $$ = Module::BigEndian; };
1446 BigOrLittle : LITTLE { $$ = Module::LittleEndian; };
1448 TargetDefinition : ENDIAN '=' BigOrLittle {
1449 CurModule.CurrentModule->setEndianness($3);
1451 | POINTERSIZE '=' EUINT64VAL {
1453 CurModule.CurrentModule->setPointerSize(Module::Pointer32);
1455 CurModule.CurrentModule->setPointerSize(Module::Pointer64);
1457 ThrowException("Invalid pointer size: '" + utostr($3) + "'!");
1461 //===----------------------------------------------------------------------===//
1462 // Rules to match Function Headers
1463 //===----------------------------------------------------------------------===//
1465 Name : VAR_ID | STRINGCONSTANT;
1466 OptName : Name | /*empty*/ { $$ = 0; };
1468 ArgVal : Types OptName {
1469 if (*$1 == Type::VoidTy)
1470 ThrowException("void typed arguments are invalid!");
1471 $$ = new std::pair<PATypeHolder*, char*>($1, $2);
1474 ArgListH : ArgListH ',' ArgVal {
1480 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1485 ArgList : ArgListH {
1488 | ArgListH ',' DOTDOTDOT {
1490 $$->push_back(std::pair<PATypeHolder*,
1491 char*>(new PATypeHolder(Type::VoidTy), 0));
1494 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1495 $$->push_back(std::make_pair(new PATypeHolder(Type::VoidTy), (char*)0));
1501 FunctionHeaderH : TypesV Name '(' ArgList ')' {
1503 std::string FunctionName($2);
1504 free($2); // Free strdup'd memory!
1506 if (!(*$1)->isFirstClassType() && *$1 != Type::VoidTy)
1507 ThrowException("LLVM functions cannot return aggregate types!");
1509 std::vector<const Type*> ParamTypeList;
1510 if ($4) { // If there are arguments...
1511 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $4->begin();
1512 I != $4->end(); ++I)
1513 ParamTypeList.push_back(I->first->get());
1516 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1517 if (isVarArg) ParamTypeList.pop_back();
1519 const FunctionType *FT = FunctionType::get(*$1, ParamTypeList, isVarArg);
1520 const PointerType *PFT = PointerType::get(FT);
1524 if (!FunctionName.empty()) {
1525 ID = ValID::create((char*)FunctionName.c_str());
1527 ID = ValID::create((int)CurModule.Values[PFT].size());
1531 // See if this function was forward referenced. If so, recycle the object.
1532 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
1533 // Move the function to the end of the list, from whereever it was
1534 // previously inserted.
1535 Fn = cast<Function>(FWRef);
1536 CurModule.CurrentModule->getFunctionList().remove(Fn);
1537 CurModule.CurrentModule->getFunctionList().push_back(Fn);
1538 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
1539 (Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
1540 // If this is the case, either we need to be a forward decl, or it needs
1542 if (!CurFun.isDeclare && !Fn->isExternal())
1543 ThrowException("Redefinition of function '" + FunctionName + "'!");
1545 // Make sure to strip off any argument names so we can't get conflicts.
1546 if (Fn->isExternal())
1547 for (Function::aiterator AI = Fn->abegin(), AE = Fn->aend();
1551 } else { // Not already defined?
1552 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName,
1553 CurModule.CurrentModule);
1554 InsertValue(Fn, CurModule.Values);
1557 CurFun.FunctionStart(Fn);
1559 // Add all of the arguments we parsed to the function...
1560 if ($4) { // Is null if empty...
1561 if (isVarArg) { // Nuke the last entry
1562 assert($4->back().first->get() == Type::VoidTy && $4->back().second == 0&&
1563 "Not a varargs marker!");
1564 delete $4->back().first;
1565 $4->pop_back(); // Delete the last entry
1567 Function::aiterator ArgIt = Fn->abegin();
1568 for (std::vector<std::pair<PATypeHolder*, char*> >::iterator I =$4->begin();
1569 I != $4->end(); ++I, ++ArgIt) {
1570 delete I->first; // Delete the typeholder...
1572 setValueName(ArgIt, I->second); // Insert arg into symtab...
1576 delete $4; // We're now done with the argument list
1580 BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1582 FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
1583 $$ = CurFun.CurrentFunction;
1585 // Make sure that we keep track of the linkage type even if there was a
1586 // previous "declare".
1589 // Resolve circular types before we parse the body of the function.
1590 ResolveTypes(CurFun.LateResolveTypes);
1593 END : ENDTOK | '}'; // Allow end of '}' to end a function
1595 Function : BasicBlockList END {
1599 FunctionProto : DECLARE { CurFun.isDeclare = true; } FunctionHeaderH {
1600 $$ = CurFun.CurrentFunction;
1601 CurFun.FunctionDone();
1604 //===----------------------------------------------------------------------===//
1605 // Rules to match Basic Blocks
1606 //===----------------------------------------------------------------------===//
1608 ConstValueRef : ESINT64VAL { // A reference to a direct constant
1609 $$ = ValID::create($1);
1612 $$ = ValID::create($1);
1614 | FPVAL { // Perhaps it's an FP constant?
1615 $$ = ValID::create($1);
1618 $$ = ValID::create(ConstantBool::True);
1621 $$ = ValID::create(ConstantBool::False);
1624 $$ = ValID::createNull();
1627 $$ = ValID::create($1);
1630 // SymbolicValueRef - Reference to one of two ways of symbolically refering to
1633 SymbolicValueRef : INTVAL { // Is it an integer reference...?
1634 $$ = ValID::create($1);
1636 | Name { // Is it a named reference...?
1637 $$ = ValID::create($1);
1640 // ValueRef - A reference to a definition... either constant or symbolic
1641 ValueRef : SymbolicValueRef | ConstValueRef;
1644 // ResolvedVal - a <type> <value> pair. This is used only in cases where the
1645 // type immediately preceeds the value reference, and allows complex constant
1646 // pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
1647 ResolvedVal : Types ValueRef {
1648 $$ = getVal(*$1, $2); delete $1;
1651 BasicBlockList : BasicBlockList BasicBlock {
1654 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
1659 // Basic blocks are terminated by branching instructions:
1660 // br, br/cc, switch, ret
1662 BasicBlock : InstructionList OptAssign BBTerminatorInst {
1663 setValueName($3, $2);
1666 $1->getInstList().push_back($3);
1671 InstructionList : InstructionList Inst {
1672 $1->getInstList().push_back($2);
1676 $$ = CurBB = getBBVal(ValID::create((int)CurFun.NextBBNum++), true);
1679 $$ = CurBB = getBBVal(ValID::create($1), true);
1682 BBTerminatorInst : RET ResolvedVal { // Return with a result...
1683 $$ = new ReturnInst($2);
1685 | RET VOID { // Return with no result...
1686 $$ = new ReturnInst();
1688 | BR LABEL ValueRef { // Unconditional Branch...
1689 $$ = new BranchInst(getBBVal($3));
1690 } // Conditional Branch...
1691 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
1692 $$ = new BranchInst(getBBVal($6), getBBVal($9), getVal(Type::BoolTy, $3));
1694 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1695 SwitchInst *S = new SwitchInst(getVal($2, $3), getBBVal($6));
1698 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
1701 S->addCase(I->first, I->second);
1704 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
1705 SwitchInst *S = new SwitchInst(getVal($2, $3), getBBVal($6));
1708 | INVOKE TypesV ValueRef '(' ValueRefListE ')' TO LABEL ValueRef
1709 UNWIND LABEL ValueRef {
1710 const PointerType *PFTy;
1711 const FunctionType *Ty;
1713 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1714 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
1715 // Pull out the types of all of the arguments...
1716 std::vector<const Type*> ParamTypes;
1718 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1720 ParamTypes.push_back((*I)->getType());
1723 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1724 if (isVarArg) ParamTypes.pop_back();
1726 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
1727 PFTy = PointerType::get(Ty);
1730 Value *V = getVal(PFTy, $3); // Get the function we're calling...
1732 BasicBlock *Normal = getBBVal($9);
1733 BasicBlock *Except = getBBVal($12);
1735 // Create the call node...
1736 if (!$5) { // Has no arguments?
1737 $$ = new InvokeInst(V, Normal, Except, std::vector<Value*>());
1738 } else { // Has arguments?
1739 // Loop through FunctionType's arguments and ensure they are specified
1742 FunctionType::param_iterator I = Ty->param_begin();
1743 FunctionType::param_iterator E = Ty->param_end();
1744 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
1746 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1747 if ((*ArgI)->getType() != *I)
1748 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
1749 (*I)->getDescription() + "'!");
1751 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1752 ThrowException("Invalid number of parameters detected!");
1754 $$ = new InvokeInst(V, Normal, Except, *$5);
1760 $$ = new UnwindInst();
1765 JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1767 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
1769 ThrowException("May only switch on a constant pool value!");
1771 $$->push_back(std::make_pair(V, getBBVal($6)));
1773 | IntType ConstValueRef ',' LABEL ValueRef {
1774 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
1775 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
1778 ThrowException("May only switch on a constant pool value!");
1780 $$->push_back(std::make_pair(V, getBBVal($5)));
1783 Inst : OptAssign InstVal {
1784 // Is this definition named?? if so, assign the name...
1785 setValueName($2, $1);
1790 PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
1791 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
1792 $$->push_back(std::make_pair(getVal(*$1, $3), getBBVal($5)));
1795 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1797 $1->push_back(std::make_pair(getVal($1->front().first->getType(), $4),
1802 ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
1803 $$ = new std::vector<Value*>();
1806 | ValueRefList ',' ResolvedVal {
1811 // ValueRefListE - Just like ValueRefList, except that it may also be empty!
1812 ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
1814 InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
1815 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint())
1816 ThrowException("Arithmetic operator requires integer or FP operands!");
1817 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1819 ThrowException("binary operator returned null!");
1822 | LogicalOps Types ValueRef ',' ValueRef {
1823 if (!(*$2)->isIntegral())
1824 ThrowException("Logical operator requires integral operands!");
1825 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1827 ThrowException("binary operator returned null!");
1830 | SetCondOps Types ValueRef ',' ValueRef {
1831 $$ = new SetCondInst($1, getVal(*$2, $3), getVal(*$2, $5));
1833 ThrowException("binary operator returned null!");
1837 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
1838 << " Replacing with 'xor'.\n";
1840 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
1842 ThrowException("Expected integral type for not instruction!");
1844 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
1846 ThrowException("Could not create a xor instruction!");
1848 | ShiftOps ResolvedVal ',' ResolvedVal {
1849 if ($4->getType() != Type::UByteTy)
1850 ThrowException("Shift amount must be ubyte!");
1851 if (!$2->getType()->isInteger())
1852 ThrowException("Shift constant expression requires integer operand!");
1853 $$ = new ShiftInst($1, $2, $4);
1855 | CAST ResolvedVal TO Types {
1856 if (!$4->get()->isFirstClassType())
1857 ThrowException("cast instruction to a non-primitive type: '" +
1858 $4->get()->getDescription() + "'!");
1859 $$ = new CastInst($2, *$4);
1862 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1863 if ($2->getType() != Type::BoolTy)
1864 ThrowException("select condition must be boolean!");
1865 if ($4->getType() != $6->getType())
1866 ThrowException("select value types should match!");
1867 $$ = new SelectInst($2, $4, $6);
1869 | VA_ARG ResolvedVal ',' Types {
1870 // FIXME: This is emulation code for an obsolete syntax. This should be
1871 // removed at some point.
1872 if (!ObsoleteVarArgs) {
1873 std::cerr << "WARNING: this file uses obsolete features. "
1874 << "Assemble and disassemble to update it.\n";
1875 ObsoleteVarArgs = true;
1878 // First, load the valist...
1879 Instruction *CurVAList = new LoadInst($2, "");
1880 CurBB->getInstList().push_back(CurVAList);
1882 // Emit the vaarg instruction.
1883 $$ = new VAArgInst(CurVAList, *$4);
1885 // Now we must advance the pointer and update it in memory.
1886 Instruction *TheVANext = new VANextInst(CurVAList, *$4);
1887 CurBB->getInstList().push_back(TheVANext);
1889 CurBB->getInstList().push_back(new StoreInst(TheVANext, $2));
1892 | VAARG ResolvedVal ',' Types {
1893 $$ = new VAArgInst($2, *$4);
1896 | VANEXT ResolvedVal ',' Types {
1897 $$ = new VANextInst($2, *$4);
1901 const Type *Ty = $2->front().first->getType();
1902 if (!Ty->isFirstClassType())
1903 ThrowException("PHI node operands must be of first class type!");
1904 $$ = new PHINode(Ty);
1905 $$->op_reserve($2->size()*2);
1906 while ($2->begin() != $2->end()) {
1907 if ($2->front().first->getType() != Ty)
1908 ThrowException("All elements of a PHI node must be of the same type!");
1909 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
1912 delete $2; // Free the list...
1914 | CALL TypesV ValueRef '(' ValueRefListE ')' {
1915 const PointerType *PFTy;
1916 const FunctionType *Ty;
1918 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1919 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
1920 // Pull out the types of all of the arguments...
1921 std::vector<const Type*> ParamTypes;
1923 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1925 ParamTypes.push_back((*I)->getType());
1928 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1929 if (isVarArg) ParamTypes.pop_back();
1931 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
1932 PFTy = PointerType::get(Ty);
1935 Value *V = getVal(PFTy, $3); // Get the function we're calling...
1937 // Create the call node...
1938 if (!$5) { // Has no arguments?
1939 // Make sure no arguments is a good thing!
1940 if (Ty->getNumParams() != 0)
1941 ThrowException("No arguments passed to a function that "
1942 "expects arguments!");
1944 $$ = new CallInst(V, std::vector<Value*>());
1945 } else { // Has arguments?
1946 // Loop through FunctionType's arguments and ensure they are specified
1949 FunctionType::param_iterator I = Ty->param_begin();
1950 FunctionType::param_iterator E = Ty->param_end();
1951 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
1953 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1954 if ((*ArgI)->getType() != *I)
1955 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
1956 (*I)->getDescription() + "'!");
1958 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1959 ThrowException("Invalid number of parameters detected!");
1961 $$ = new CallInst(V, *$5);
1971 // IndexList - List of indices for GEP based instructions...
1972 IndexList : ',' ValueRefList {
1975 $$ = new std::vector<Value*>();
1978 OptVolatile : VOLATILE {
1986 MemoryInst : MALLOC Types {
1987 $$ = new MallocInst(*$2);
1990 | MALLOC Types ',' UINT ValueRef {
1991 $$ = new MallocInst(*$2, getVal($4, $5));
1995 $$ = new AllocaInst(*$2);
1998 | ALLOCA Types ',' UINT ValueRef {
1999 $$ = new AllocaInst(*$2, getVal($4, $5));
2002 | FREE ResolvedVal {
2003 if (!isa<PointerType>($2->getType()))
2004 ThrowException("Trying to free nonpointer type " +
2005 $2->getType()->getDescription() + "!");
2006 $$ = new FreeInst($2);
2009 | OptVolatile LOAD Types ValueRef {
2010 if (!isa<PointerType>($3->get()))
2011 ThrowException("Can't load from nonpointer type: " +
2012 (*$3)->getDescription());
2013 $$ = new LoadInst(getVal(*$3, $4), "", $1);
2016 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
2017 const PointerType *PT = dyn_cast<PointerType>($5->get());
2019 ThrowException("Can't store to a nonpointer type: " +
2020 (*$5)->getDescription());
2021 const Type *ElTy = PT->getElementType();
2022 if (ElTy != $3->getType())
2023 ThrowException("Can't store '" + $3->getType()->getDescription() +
2024 "' into space of type '" + ElTy->getDescription() + "'!");
2026 $$ = new StoreInst($3, getVal(*$5, $6), $1);
2029 | GETELEMENTPTR Types ValueRef IndexList {
2030 if (!isa<PointerType>($2->get()))
2031 ThrowException("getelementptr insn requires pointer operand!");
2033 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
2034 // indices to uint struct indices for compatibility.
2035 generic_gep_type_iterator<std::vector<Value*>::iterator>
2036 GTI = gep_type_begin($2->get(), $4->begin(), $4->end()),
2037 GTE = gep_type_end($2->get(), $4->begin(), $4->end());
2038 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
2039 if (isa<StructType>(*GTI)) // Only change struct indices
2040 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
2041 if (CUI->getType() == Type::UByteTy)
2042 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
2044 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
2045 ThrowException("Invalid getelementptr indices for type '" +
2046 (*$2)->getDescription()+ "'!");
2047 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
2048 delete $2; delete $4;
2053 int yyerror(const char *ErrorMsg) {
2055 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
2056 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
2057 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
2058 if (yychar == YYEMPTY || yychar == 0)
2059 errMsg += "end-of-file.";
2061 errMsg += "token: '" + std::string(llvmAsmtext, llvmAsmleng) + "'";
2062 ThrowException(errMsg);