1 //===-- AsmWriter.cpp - Printing LLVM as an assembly file -----------------===//
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 library implements the functionality defined in llvm/Assembly/Writer.h
12 // Note that these routines must be extremely tolerant of various errors in the
13 // LLVM code, because it can be used for debugging transformations.
15 //===----------------------------------------------------------------------===//
17 #include "llvm/Assembly/Writer.h"
18 #include "llvm/Assembly/PrintModulePass.h"
19 #include "llvm/Assembly/AsmAnnotationWriter.h"
20 #include "llvm/CallingConv.h"
21 #include "llvm/Constants.h"
22 #include "llvm/DerivedTypes.h"
23 #include "llvm/InlineAsm.h"
24 #include "llvm/Instruction.h"
25 #include "llvm/Instructions.h"
26 #include "llvm/Module.h"
27 #include "llvm/ValueSymbolTable.h"
28 #include "llvm/TypeSymbolTable.h"
29 #include "llvm/ADT/StringExtras.h"
30 #include "llvm/ADT/STLExtras.h"
31 #include "llvm/Support/CFG.h"
32 #include "llvm/Support/MathExtras.h"
33 #include "llvm/Support/Streams.h"
39 // Make virtual table appear in this compilation unit.
40 AssemblyAnnotationWriter::~AssemblyAnnotationWriter() {}
42 /// This class provides computation of slot numbers for LLVM Assembly writing.
43 /// @brief LLVM Assembly Writing Slot Computation.
50 /// @brief A mapping of Values to slot numbers
51 typedef std::map<const Value*,unsigned> ValueMap;
54 /// @name Constructors
57 /// @brief Construct from a module
58 SlotMachine(const Module *M);
60 /// @brief Construct from a function, starting out in incorp state.
61 SlotMachine(const Function *F);
67 /// Return the slot number of the specified value in it's type
68 /// plane. If something is not in the SlotMachine, return -1.
69 int getLocalSlot(const Value *V);
70 int getGlobalSlot(const GlobalValue *V);
76 /// If you'd like to deal with a function instead of just a module, use
77 /// this method to get its data into the SlotMachine.
78 void incorporateFunction(const Function *F) {
80 FunctionProcessed = false;
83 /// After calling incorporateFunction, use this method to remove the
84 /// most recently incorporated function from the SlotMachine. This
85 /// will reset the state of the machine back to just the module contents.
89 /// @name Implementation Details
92 /// This function does the actual initialization.
93 inline void initialize();
95 /// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
96 void CreateModuleSlot(const GlobalValue *V);
98 /// CreateFunctionSlot - Insert the specified Value* into the slot table.
99 void CreateFunctionSlot(const Value *V);
101 /// Add all of the module level global variables (and their initializers)
102 /// and function declarations, but not the contents of those functions.
103 void processModule();
105 /// Add all of the functions arguments, basic blocks, and instructions
106 void processFunction();
108 SlotMachine(const SlotMachine &); // DO NOT IMPLEMENT
109 void operator=(const SlotMachine &); // DO NOT IMPLEMENT
116 /// @brief The module for which we are holding slot numbers
117 const Module* TheModule;
119 /// @brief The function for which we are holding slot numbers
120 const Function* TheFunction;
121 bool FunctionProcessed;
123 /// @brief The TypePlanes map for the module level data
127 /// @brief The TypePlanes map for the function level data
135 } // end namespace llvm
137 static RegisterPass<PrintModulePass>
138 X("printm", "Print module to stderr");
139 static RegisterPass<PrintFunctionPass>
140 Y("print","Print function to stderr");
142 static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
143 std::map<const Type *, std::string> &TypeTable,
144 SlotMachine *Machine);
146 static const Module *getModuleFromVal(const Value *V) {
147 if (const Argument *MA = dyn_cast<Argument>(V))
148 return MA->getParent() ? MA->getParent()->getParent() : 0;
149 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
150 return BB->getParent() ? BB->getParent()->getParent() : 0;
151 else if (const Instruction *I = dyn_cast<Instruction>(V)) {
152 const Function *M = I->getParent() ? I->getParent()->getParent() : 0;
153 return M ? M->getParent() : 0;
154 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
155 return GV->getParent();
159 static SlotMachine *createSlotMachine(const Value *V) {
160 if (const Argument *FA = dyn_cast<Argument>(V)) {
161 return new SlotMachine(FA->getParent());
162 } else if (const Instruction *I = dyn_cast<Instruction>(V)) {
163 return new SlotMachine(I->getParent()->getParent());
164 } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
165 return new SlotMachine(BB->getParent());
166 } else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)){
167 return new SlotMachine(GV->getParent());
168 } else if (const Function *Func = dyn_cast<Function>(V)) {
169 return new SlotMachine(Func);
174 /// NameNeedsQuotes - Return true if the specified llvm name should be wrapped
176 static bool NameNeedsQuotes(const std::string &Name) {
177 if (Name[0] >= '0' && Name[0] <= '9') return true;
178 // Scan to see if we have any characters that are not on the "white list"
179 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
181 assert(C != '"' && "Illegal character in LLVM value name!");
182 if ((C < 'a' || C > 'z') && (C < 'A' || C > 'Z') && (C < '0' || C > '9') &&
183 C != '-' && C != '.' && C != '_')
195 /// getLLVMName - Turn the specified string into an 'LLVM name', which is either
196 /// prefixed with % (if the string only contains simple characters) or is
197 /// surrounded with ""'s (if it has special chars in it).
198 static std::string getLLVMName(const std::string &Name, PrefixType Prefix) {
199 assert(!Name.empty() && "Cannot get empty name!");
201 // First character cannot start with a number...
202 if (NameNeedsQuotes(Name)) {
203 if (Prefix == GlobalPrefix)
204 return "@\"" + Name + "\"";
205 return "\"" + Name + "\"";
208 // If we get here, then the identifier is legal to use as a "VarID".
210 default: assert(0 && "Bad prefix!");
211 case GlobalPrefix: return '@' + Name;
212 case LabelPrefix: return Name;
213 case LocalPrefix: return '%' + Name;
218 /// fillTypeNameTable - If the module has a symbol table, take all global types
219 /// and stuff their names into the TypeNames map.
221 static void fillTypeNameTable(const Module *M,
222 std::map<const Type *, std::string> &TypeNames) {
224 const TypeSymbolTable &ST = M->getTypeSymbolTable();
225 TypeSymbolTable::const_iterator TI = ST.begin();
226 for (; TI != ST.end(); ++TI) {
227 // As a heuristic, don't insert pointer to primitive types, because
228 // they are used too often to have a single useful name.
230 const Type *Ty = cast<Type>(TI->second);
231 if (!isa<PointerType>(Ty) ||
232 !cast<PointerType>(Ty)->getElementType()->isPrimitiveType() ||
233 !cast<PointerType>(Ty)->getElementType()->isInteger() ||
234 isa<OpaqueType>(cast<PointerType>(Ty)->getElementType()))
235 TypeNames.insert(std::make_pair(Ty, getLLVMName(TI->first, LocalPrefix)));
241 static void calcTypeName(const Type *Ty,
242 std::vector<const Type *> &TypeStack,
243 std::map<const Type *, std::string> &TypeNames,
244 std::string & Result){
245 if (Ty->isInteger() || (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty))) {
246 Result += Ty->getDescription(); // Base case
250 // Check to see if the type is named.
251 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
252 if (I != TypeNames.end()) {
257 if (isa<OpaqueType>(Ty)) {
262 // Check to see if the Type is already on the stack...
263 unsigned Slot = 0, CurSize = TypeStack.size();
264 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
266 // This is another base case for the recursion. In this case, we know
267 // that we have looped back to a type that we have previously visited.
268 // Generate the appropriate upreference to handle this.
269 if (Slot < CurSize) {
270 Result += "\\" + utostr(CurSize-Slot); // Here's the upreference
274 TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
276 switch (Ty->getTypeID()) {
277 case Type::IntegerTyID: {
278 unsigned BitWidth = cast<IntegerType>(Ty)->getBitWidth();
279 Result += "i" + utostr(BitWidth);
282 case Type::FunctionTyID: {
283 const FunctionType *FTy = cast<FunctionType>(Ty);
284 calcTypeName(FTy->getReturnType(), TypeStack, TypeNames, Result);
287 for (FunctionType::param_iterator I = FTy->param_begin(),
288 E = FTy->param_end(); I != E; ++I) {
289 if (I != FTy->param_begin())
291 calcTypeName(*I, TypeStack, TypeNames, Result);
292 if (FTy->getParamAttrs(Idx)) {
294 Result += FunctionType::getParamAttrsText(FTy->getParamAttrs(Idx));
298 if (FTy->isVarArg()) {
299 if (FTy->getNumParams()) Result += ", ";
303 if (FTy->getParamAttrs(0)) {
305 Result += FunctionType::getParamAttrsText(FTy->getParamAttrs(0));
309 case Type::StructTyID: {
310 const StructType *STy = cast<StructType>(Ty);
314 for (StructType::element_iterator I = STy->element_begin(),
315 E = STy->element_end(); I != E; ++I) {
316 if (I != STy->element_begin())
318 calcTypeName(*I, TypeStack, TypeNames, Result);
325 case Type::PointerTyID:
326 calcTypeName(cast<PointerType>(Ty)->getElementType(),
327 TypeStack, TypeNames, Result);
330 case Type::ArrayTyID: {
331 const ArrayType *ATy = cast<ArrayType>(Ty);
332 Result += "[" + utostr(ATy->getNumElements()) + " x ";
333 calcTypeName(ATy->getElementType(), TypeStack, TypeNames, Result);
337 case Type::VectorTyID: {
338 const VectorType *PTy = cast<VectorType>(Ty);
339 Result += "<" + utostr(PTy->getNumElements()) + " x ";
340 calcTypeName(PTy->getElementType(), TypeStack, TypeNames, Result);
344 case Type::OpaqueTyID:
348 Result += "<unrecognized-type>";
352 TypeStack.pop_back(); // Remove self from stack...
356 /// printTypeInt - The internal guts of printing out a type that has a
357 /// potentially named portion.
359 static std::ostream &printTypeInt(std::ostream &Out, const Type *Ty,
360 std::map<const Type *, std::string> &TypeNames) {
361 // Primitive types always print out their description, regardless of whether
362 // they have been named or not.
364 if (Ty->isInteger() || (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty)))
365 return Out << Ty->getDescription();
367 // Check to see if the type is named.
368 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
369 if (I != TypeNames.end()) return Out << I->second;
371 // Otherwise we have a type that has not been named but is a derived type.
372 // Carefully recurse the type hierarchy to print out any contained symbolic
375 std::vector<const Type *> TypeStack;
376 std::string TypeName;
377 calcTypeName(Ty, TypeStack, TypeNames, TypeName);
378 TypeNames.insert(std::make_pair(Ty, TypeName));//Cache type name for later use
379 return (Out << TypeName);
383 /// WriteTypeSymbolic - This attempts to write the specified type as a symbolic
384 /// type, iff there is an entry in the modules symbol table for the specified
385 /// type or one of it's component types. This is slower than a simple x << Type
387 std::ostream &llvm::WriteTypeSymbolic(std::ostream &Out, const Type *Ty,
391 // If they want us to print out a type, but there is no context, we can't
392 // print it symbolically.
394 return Out << Ty->getDescription();
396 std::map<const Type *, std::string> TypeNames;
397 fillTypeNameTable(M, TypeNames);
398 return printTypeInt(Out, Ty, TypeNames);
401 // PrintEscapedString - Print each character of the specified string, escaping
402 // it if it is not printable or if it is an escape char.
403 static void PrintEscapedString(const std::string &Str, std::ostream &Out) {
404 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
405 unsigned char C = Str[i];
406 if (isprint(C) && C != '"' && C != '\\') {
410 << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'))
411 << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
416 static const char *getPredicateText(unsigned predicate) {
417 const char * pred = "unknown";
419 case FCmpInst::FCMP_FALSE: pred = "false"; break;
420 case FCmpInst::FCMP_OEQ: pred = "oeq"; break;
421 case FCmpInst::FCMP_OGT: pred = "ogt"; break;
422 case FCmpInst::FCMP_OGE: pred = "oge"; break;
423 case FCmpInst::FCMP_OLT: pred = "olt"; break;
424 case FCmpInst::FCMP_OLE: pred = "ole"; break;
425 case FCmpInst::FCMP_ONE: pred = "one"; break;
426 case FCmpInst::FCMP_ORD: pred = "ord"; break;
427 case FCmpInst::FCMP_UNO: pred = "uno"; break;
428 case FCmpInst::FCMP_UEQ: pred = "ueq"; break;
429 case FCmpInst::FCMP_UGT: pred = "ugt"; break;
430 case FCmpInst::FCMP_UGE: pred = "uge"; break;
431 case FCmpInst::FCMP_ULT: pred = "ult"; break;
432 case FCmpInst::FCMP_ULE: pred = "ule"; break;
433 case FCmpInst::FCMP_UNE: pred = "une"; break;
434 case FCmpInst::FCMP_TRUE: pred = "true"; break;
435 case ICmpInst::ICMP_EQ: pred = "eq"; break;
436 case ICmpInst::ICMP_NE: pred = "ne"; break;
437 case ICmpInst::ICMP_SGT: pred = "sgt"; break;
438 case ICmpInst::ICMP_SGE: pred = "sge"; break;
439 case ICmpInst::ICMP_SLT: pred = "slt"; break;
440 case ICmpInst::ICMP_SLE: pred = "sle"; break;
441 case ICmpInst::ICMP_UGT: pred = "ugt"; break;
442 case ICmpInst::ICMP_UGE: pred = "uge"; break;
443 case ICmpInst::ICMP_ULT: pred = "ult"; break;
444 case ICmpInst::ICMP_ULE: pred = "ule"; break;
449 /// @brief Internal constant writer.
450 static void WriteConstantInt(std::ostream &Out, const Constant *CV,
451 std::map<const Type *, std::string> &TypeTable,
452 SlotMachine *Machine) {
453 const int IndentSize = 4;
454 static std::string Indent = "\n";
455 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
456 if (CI->getType() == Type::Int1Ty)
457 Out << (CI->getZExtValue() ? "true" : "false");
459 Out << CI->getValue().toStringSigned(10);
460 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
461 // We would like to output the FP constant value in exponential notation,
462 // but we cannot do this if doing so will lose precision. Check here to
463 // make sure that we only output it in exponential format if we can parse
464 // the value back and get the same value.
466 std::string StrVal = ftostr(CFP->getValue());
468 // Check to make sure that the stringized number is not some string like
469 // "Inf" or NaN, that atof will accept, but the lexer will not. Check that
470 // the string matches the "[-+]?[0-9]" regex.
472 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
473 ((StrVal[0] == '-' || StrVal[0] == '+') &&
474 (StrVal[1] >= '0' && StrVal[1] <= '9')))
475 // Reparse stringized version!
476 if (atof(StrVal.c_str()) == CFP->getValue()) {
481 // Otherwise we could not reparse it to exactly the same value, so we must
482 // output the string in hexadecimal format!
483 assert(sizeof(double) == sizeof(uint64_t) &&
484 "assuming that double is 64 bits!");
485 Out << "0x" << utohexstr(DoubleToBits(CFP->getValue()));
487 } else if (isa<ConstantAggregateZero>(CV)) {
488 Out << "zeroinitializer";
489 } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
490 // As a special case, print the array as a string if it is an array of
491 // ubytes or an array of sbytes with positive values.
493 const Type *ETy = CA->getType()->getElementType();
494 if (CA->isString()) {
496 PrintEscapedString(CA->getAsString(), Out);
499 } else { // Cannot output in string format...
501 if (CA->getNumOperands()) {
503 printTypeInt(Out, ETy, TypeTable);
504 WriteAsOperandInternal(Out, CA->getOperand(0),
506 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
508 printTypeInt(Out, ETy, TypeTable);
509 WriteAsOperandInternal(Out, CA->getOperand(i), TypeTable, Machine);
514 } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
515 if (CS->getType()->isPacked())
518 unsigned N = CS->getNumOperands();
521 Indent += std::string(IndentSize, ' ');
526 printTypeInt(Out, CS->getOperand(0)->getType(), TypeTable);
528 WriteAsOperandInternal(Out, CS->getOperand(0), TypeTable, Machine);
530 for (unsigned i = 1; i < N; i++) {
532 if (N > 2) Out << Indent;
533 printTypeInt(Out, CS->getOperand(i)->getType(), TypeTable);
535 WriteAsOperandInternal(Out, CS->getOperand(i), TypeTable, Machine);
537 if (N > 2) Indent.resize(Indent.size() - IndentSize);
541 if (CS->getType()->isPacked())
543 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
544 const Type *ETy = CP->getType()->getElementType();
545 assert(CP->getNumOperands() > 0 &&
546 "Number of operands for a PackedConst must be > 0");
549 printTypeInt(Out, ETy, TypeTable);
550 WriteAsOperandInternal(Out, CP->getOperand(0), TypeTable, Machine);
551 for (unsigned i = 1, e = CP->getNumOperands(); i != e; ++i) {
553 printTypeInt(Out, ETy, TypeTable);
554 WriteAsOperandInternal(Out, CP->getOperand(i), TypeTable, Machine);
557 } else if (isa<ConstantPointerNull>(CV)) {
560 } else if (isa<UndefValue>(CV)) {
563 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
564 Out << CE->getOpcodeName();
566 Out << " " << getPredicateText(CE->getPredicate());
569 for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
570 printTypeInt(Out, (*OI)->getType(), TypeTable);
571 WriteAsOperandInternal(Out, *OI, TypeTable, Machine);
572 if (OI+1 != CE->op_end())
578 printTypeInt(Out, CE->getType(), TypeTable);
584 Out << "<placeholder or erroneous Constant>";
589 /// WriteAsOperand - Write the name of the specified value out to the specified
590 /// ostream. This can be useful when you just want to print int %reg126, not
591 /// the whole instruction that generated it.
593 static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
594 std::map<const Type*, std::string> &TypeTable,
595 SlotMachine *Machine) {
598 Out << getLLVMName(V->getName(),
599 isa<GlobalValue>(V) ? GlobalPrefix : LocalPrefix);
601 const Constant *CV = dyn_cast<Constant>(V);
602 if (CV && !isa<GlobalValue>(CV)) {
603 WriteConstantInt(Out, CV, TypeTable, Machine);
604 } else if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
606 if (IA->hasSideEffects())
607 Out << "sideeffect ";
609 PrintEscapedString(IA->getAsmString(), Out);
611 PrintEscapedString(IA->getConstraintString(), Out);
617 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
618 Slot = Machine->getGlobalSlot(GV);
621 Slot = Machine->getLocalSlot(V);
624 Machine = createSlotMachine(V);
626 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
627 Slot = Machine->getGlobalSlot(GV);
630 Slot = Machine->getLocalSlot(V);
638 Out << Prefix << Slot;
645 /// WriteAsOperand - Write the name of the specified value out to the specified
646 /// ostream. This can be useful when you just want to print int %reg126, not
647 /// the whole instruction that generated it.
649 std::ostream &llvm::WriteAsOperand(std::ostream &Out, const Value *V,
650 bool PrintType, const Module *Context) {
651 std::map<const Type *, std::string> TypeNames;
652 if (Context == 0) Context = getModuleFromVal(V);
655 fillTypeNameTable(Context, TypeNames);
658 printTypeInt(Out, V->getType(), TypeNames);
660 WriteAsOperandInternal(Out, V, TypeNames, 0);
667 class AssemblyWriter {
669 SlotMachine &Machine;
670 const Module *TheModule;
671 std::map<const Type *, std::string> TypeNames;
672 AssemblyAnnotationWriter *AnnotationWriter;
674 inline AssemblyWriter(std::ostream &o, SlotMachine &Mac, const Module *M,
675 AssemblyAnnotationWriter *AAW)
676 : Out(o), Machine(Mac), TheModule(M), AnnotationWriter(AAW) {
678 // If the module has a symbol table, take all global types and stuff their
679 // names into the TypeNames map.
681 fillTypeNameTable(M, TypeNames);
684 inline void write(const Module *M) { printModule(M); }
685 inline void write(const GlobalVariable *G) { printGlobal(G); }
686 inline void write(const Function *F) { printFunction(F); }
687 inline void write(const BasicBlock *BB) { printBasicBlock(BB); }
688 inline void write(const Instruction *I) { printInstruction(*I); }
689 inline void write(const Type *Ty) { printType(Ty); }
691 void writeOperand(const Value *Op, bool PrintType);
693 const Module* getModule() { return TheModule; }
696 void printModule(const Module *M);
697 void printTypeSymbolTable(const TypeSymbolTable &ST);
698 void printGlobal(const GlobalVariable *GV);
699 void printFunction(const Function *F);
700 void printArgument(const Argument *FA, FunctionType::ParameterAttributes A);
701 void printBasicBlock(const BasicBlock *BB);
702 void printInstruction(const Instruction &I);
704 // printType - Go to extreme measures to attempt to print out a short,
705 // symbolic version of a type name.
707 std::ostream &printType(const Type *Ty) {
708 return printTypeInt(Out, Ty, TypeNames);
711 // printTypeAtLeastOneLevel - Print out one level of the possibly complex type
712 // without considering any symbolic types that we may have equal to it.
714 std::ostream &printTypeAtLeastOneLevel(const Type *Ty);
716 // printInfoComment - Print a little comment after the instruction indicating
717 // which slot it occupies.
718 void printInfoComment(const Value &V);
720 } // end of llvm namespace
722 /// printTypeAtLeastOneLevel - Print out one level of the possibly complex type
723 /// without considering any symbolic types that we may have equal to it.
725 std::ostream &AssemblyWriter::printTypeAtLeastOneLevel(const Type *Ty) {
726 if (const IntegerType *ITy = dyn_cast<IntegerType>(Ty))
727 Out << "i" << utostr(ITy->getBitWidth());
728 else if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
729 printType(FTy->getReturnType());
732 for (FunctionType::param_iterator I = FTy->param_begin(),
733 E = FTy->param_end(); I != E; ++I) {
734 if (I != FTy->param_begin())
737 if (FTy->getParamAttrs(Idx)) {
738 Out << " " << FunctionType::getParamAttrsText(FTy->getParamAttrs(Idx));
742 if (FTy->isVarArg()) {
743 if (FTy->getNumParams()) Out << ", ";
747 if (FTy->getParamAttrs(0))
748 Out << ' ' << FunctionType::getParamAttrsText(FTy->getParamAttrs(0));
749 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
753 for (StructType::element_iterator I = STy->element_begin(),
754 E = STy->element_end(); I != E; ++I) {
755 if (I != STy->element_begin())
762 } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
763 printType(PTy->getElementType()) << '*';
764 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
765 Out << '[' << ATy->getNumElements() << " x ";
766 printType(ATy->getElementType()) << ']';
767 } else if (const VectorType *PTy = dyn_cast<VectorType>(Ty)) {
768 Out << '<' << PTy->getNumElements() << " x ";
769 printType(PTy->getElementType()) << '>';
771 else if (isa<OpaqueType>(Ty)) {
774 if (!Ty->isPrimitiveType())
775 Out << "<unknown derived type>";
782 void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType) {
784 Out << "<null operand!>";
786 if (PrintType) { Out << ' '; printType(Operand->getType()); }
787 WriteAsOperandInternal(Out, Operand, TypeNames, &Machine);
792 void AssemblyWriter::printModule(const Module *M) {
793 if (!M->getModuleIdentifier().empty() &&
794 // Don't print the ID if it will start a new line (which would
795 // require a comment char before it).
796 M->getModuleIdentifier().find('\n') == std::string::npos)
797 Out << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
799 if (!M->getDataLayout().empty())
800 Out << "target datalayout = \"" << M->getDataLayout() << "\"\n";
801 if (!M->getTargetTriple().empty())
802 Out << "target triple = \"" << M->getTargetTriple() << "\"\n";
804 if (!M->getModuleInlineAsm().empty()) {
805 // Split the string into lines, to make it easier to read the .ll file.
806 std::string Asm = M->getModuleInlineAsm();
808 size_t NewLine = Asm.find_first_of('\n', CurPos);
809 while (NewLine != std::string::npos) {
810 // We found a newline, print the portion of the asm string from the
811 // last newline up to this newline.
812 Out << "module asm \"";
813 PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.begin()+NewLine),
817 NewLine = Asm.find_first_of('\n', CurPos);
819 Out << "module asm \"";
820 PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.end()), Out);
824 // Loop over the dependent libraries and emit them.
825 Module::lib_iterator LI = M->lib_begin();
826 Module::lib_iterator LE = M->lib_end();
828 Out << "deplibs = [ ";
830 Out << '"' << *LI << '"';
838 // Loop over the symbol table, emitting all named constants.
839 printTypeSymbolTable(M->getTypeSymbolTable());
841 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
845 Out << "\nimplementation ; Functions:\n";
847 // Output all of the functions.
848 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
852 void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
853 if (GV->hasName()) Out << getLLVMName(GV->getName(), GlobalPrefix) << " = ";
855 if (!GV->hasInitializer())
856 switch (GV->getLinkage()) {
857 case GlobalValue::DLLImportLinkage: Out << "dllimport "; break;
858 case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break;
859 default: Out << "external "; break;
861 switch (GV->getLinkage()) {
862 case GlobalValue::InternalLinkage: Out << "internal "; break;
863 case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
864 case GlobalValue::WeakLinkage: Out << "weak "; break;
865 case GlobalValue::AppendingLinkage: Out << "appending "; break;
866 case GlobalValue::DLLImportLinkage: Out << "dllimport "; break;
867 case GlobalValue::DLLExportLinkage: Out << "dllexport "; break;
868 case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break;
869 case GlobalValue::ExternalLinkage: break;
870 case GlobalValue::GhostLinkage:
871 cerr << "GhostLinkage not allowed in AsmWriter!\n";
874 switch (GV->getVisibility()) {
875 default: assert(0 && "Invalid visibility style!");
876 case GlobalValue::DefaultVisibility: break;
877 case GlobalValue::HiddenVisibility: Out << "hidden "; break;
881 Out << (GV->isConstant() ? "constant " : "global ");
882 printType(GV->getType()->getElementType());
884 if (GV->hasInitializer()) {
885 Constant* C = cast<Constant>(GV->getInitializer());
886 assert(C && "GlobalVar initializer isn't constant?");
887 writeOperand(GV->getInitializer(), false);
890 if (GV->hasSection())
891 Out << ", section \"" << GV->getSection() << '"';
892 if (GV->getAlignment())
893 Out << ", align " << GV->getAlignment();
895 printInfoComment(*GV);
899 void AssemblyWriter::printTypeSymbolTable(const TypeSymbolTable &ST) {
901 for (TypeSymbolTable::const_iterator TI = ST.begin(), TE = ST.end();
903 Out << "\t" << getLLVMName(TI->first, LocalPrefix) << " = type ";
905 // Make sure we print out at least one level of the type structure, so
906 // that we do not get %FILE = type %FILE
908 printTypeAtLeastOneLevel(TI->second) << "\n";
912 /// printFunction - Print all aspects of a function.
914 void AssemblyWriter::printFunction(const Function *F) {
915 // Print out the return type and name...
918 if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out);
920 if (F->isDeclaration())
921 switch (F->getLinkage()) {
922 case GlobalValue::DLLImportLinkage: Out << "declare dllimport "; break;
923 case GlobalValue::ExternalWeakLinkage: Out << "declare extern_weak "; break;
924 default: Out << "declare ";
928 switch (F->getLinkage()) {
929 case GlobalValue::InternalLinkage: Out << "internal "; break;
930 case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
931 case GlobalValue::WeakLinkage: Out << "weak "; break;
932 case GlobalValue::AppendingLinkage: Out << "appending "; break;
933 case GlobalValue::DLLImportLinkage: Out << "dllimport "; break;
934 case GlobalValue::DLLExportLinkage: Out << "dllexport "; break;
935 case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break;
936 case GlobalValue::ExternalLinkage: break;
937 case GlobalValue::GhostLinkage:
938 cerr << "GhostLinkage not allowed in AsmWriter!\n";
941 switch (F->getVisibility()) {
942 default: assert(0 && "Invalid visibility style!");
943 case GlobalValue::DefaultVisibility: break;
944 case GlobalValue::HiddenVisibility: Out << "hidden "; break;
948 // Print the calling convention.
949 switch (F->getCallingConv()) {
950 case CallingConv::C: break; // default
951 case CallingConv::Fast: Out << "fastcc "; break;
952 case CallingConv::Cold: Out << "coldcc "; break;
953 case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break;
954 case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break;
955 default: Out << "cc" << F->getCallingConv() << " "; break;
958 const FunctionType *FT = F->getFunctionType();
959 printType(F->getReturnType()) << ' ';
960 if (!F->getName().empty())
961 Out << getLLVMName(F->getName(), GlobalPrefix);
965 Machine.incorporateFunction(F);
967 // Loop over the arguments, printing them...
970 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
972 // Insert commas as we go... the first arg doesn't get a comma
973 if (I != F->arg_begin()) Out << ", ";
974 printArgument(I, FT->getParamAttrs(Idx));
978 // Finish printing arguments...
979 if (FT->isVarArg()) {
980 if (FT->getNumParams()) Out << ", ";
981 Out << "..."; // Output varargs portion of signature!
984 if (FT->getParamAttrs(0))
985 Out << ' ' << FunctionType::getParamAttrsText(FT->getParamAttrs(0));
987 Out << " section \"" << F->getSection() << '"';
988 if (F->getAlignment())
989 Out << " align " << F->getAlignment();
991 if (F->isDeclaration()) {
996 // Output all of its basic blocks... for the function
997 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
1003 Machine.purgeFunction();
1006 /// printArgument - This member is called for every argument that is passed into
1007 /// the function. Simply print it out
1009 void AssemblyWriter::printArgument(const Argument *Arg,
1010 FunctionType::ParameterAttributes attrs) {
1012 printType(Arg->getType());
1014 if (attrs != FunctionType::NoAttributeSet)
1015 Out << ' ' << FunctionType::getParamAttrsText(attrs);
1017 // Output name, if available...
1019 Out << ' ' << getLLVMName(Arg->getName(), LocalPrefix);
1022 /// printBasicBlock - This member is called for each basic block in a method.
1024 void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
1025 if (BB->hasName()) { // Print out the label if it exists...
1026 Out << "\n" << getLLVMName(BB->getName(), LabelPrefix) << ':';
1027 } else if (!BB->use_empty()) { // Don't print block # of no uses...
1028 Out << "\n; <label>:";
1029 int Slot = Machine.getLocalSlot(BB);
1036 if (BB->getParent() == 0)
1037 Out << "\t\t; Error: Block without parent!";
1039 if (BB != &BB->getParent()->getEntryBlock()) { // Not the entry block?
1040 // Output predecessors for the block...
1042 pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
1045 Out << " No predecessors!";
1048 writeOperand(*PI, false);
1049 for (++PI; PI != PE; ++PI) {
1051 writeOperand(*PI, false);
1059 if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out);
1061 // Output all of the instructions in the basic block...
1062 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
1063 printInstruction(*I);
1065 if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out);
1069 /// printInfoComment - Print a little comment after the instruction indicating
1070 /// which slot it occupies.
1072 void AssemblyWriter::printInfoComment(const Value &V) {
1073 if (V.getType() != Type::VoidTy) {
1075 printType(V.getType()) << '>';
1079 if (const GlobalValue *GV = dyn_cast<GlobalValue>(&V))
1080 SlotNum = Machine.getGlobalSlot(GV);
1082 SlotNum = Machine.getLocalSlot(&V);
1086 Out << ':' << SlotNum; // Print out the def slot taken.
1088 Out << " [#uses=" << V.getNumUses() << ']'; // Output # uses
1092 // This member is called for each Instruction in a function..
1093 void AssemblyWriter::printInstruction(const Instruction &I) {
1094 if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out);
1098 // Print out name if it exists...
1100 Out << getLLVMName(I.getName(), LocalPrefix) << " = ";
1102 // If this is a volatile load or store, print out the volatile marker.
1103 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
1104 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile())) {
1106 } else if (isa<CallInst>(I) && cast<CallInst>(I).isTailCall()) {
1107 // If this is a call, check if it's a tail call.
1111 // Print out the opcode...
1112 Out << I.getOpcodeName();
1114 // Print out the compare instruction predicates
1115 if (const FCmpInst *FCI = dyn_cast<FCmpInst>(&I)) {
1116 Out << " " << getPredicateText(FCI->getPredicate());
1117 } else if (const ICmpInst *ICI = dyn_cast<ICmpInst>(&I)) {
1118 Out << " " << getPredicateText(ICI->getPredicate());
1121 // Print out the type of the operands...
1122 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
1124 // Special case conditional branches to swizzle the condition out to the front
1125 if (isa<BranchInst>(I) && I.getNumOperands() > 1) {
1126 writeOperand(I.getOperand(2), true);
1128 writeOperand(Operand, true);
1130 writeOperand(I.getOperand(1), true);
1132 } else if (isa<SwitchInst>(I)) {
1133 // Special case switch statement to get formatting nice and correct...
1134 writeOperand(Operand , true); Out << ',';
1135 writeOperand(I.getOperand(1), true); Out << " [";
1137 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; op += 2) {
1139 writeOperand(I.getOperand(op ), true); Out << ',';
1140 writeOperand(I.getOperand(op+1), true);
1143 } else if (isa<PHINode>(I)) {
1145 printType(I.getType());
1148 for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) {
1149 if (op) Out << ", ";
1151 writeOperand(I.getOperand(op ), false); Out << ',';
1152 writeOperand(I.getOperand(op+1), false); Out << " ]";
1154 } else if (isa<ReturnInst>(I) && !Operand) {
1156 } else if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
1157 // Print the calling convention being used.
1158 switch (CI->getCallingConv()) {
1159 case CallingConv::C: break; // default
1160 case CallingConv::Fast: Out << " fastcc"; break;
1161 case CallingConv::Cold: Out << " coldcc"; break;
1162 case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break;
1163 case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break;
1164 default: Out << " cc" << CI->getCallingConv(); break;
1167 const PointerType *PTy = cast<PointerType>(Operand->getType());
1168 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1169 const Type *RetTy = FTy->getReturnType();
1171 // If possible, print out the short form of the call instruction. We can
1172 // only do this if the first argument is a pointer to a nonvararg function,
1173 // and if the return type is not a pointer to a function.
1175 if (!FTy->isVarArg() &&
1176 (!isa<PointerType>(RetTy) ||
1177 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
1178 Out << ' '; printType(RetTy);
1179 writeOperand(Operand, false);
1181 writeOperand(Operand, true);
1184 for (unsigned op = 1, Eop = I.getNumOperands(); op < Eop; ++op) {
1187 writeOperand(I.getOperand(op), true);
1188 if (FTy->getParamAttrs(op) != FunctionType::NoAttributeSet)
1189 Out << " " << FTy->getParamAttrsText(FTy->getParamAttrs(op));
1192 if (FTy->getParamAttrs(0) != FunctionType::NoAttributeSet)
1193 Out << ' ' << FTy->getParamAttrsText(FTy->getParamAttrs(0));
1194 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
1195 const PointerType *PTy = cast<PointerType>(Operand->getType());
1196 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1197 const Type *RetTy = FTy->getReturnType();
1199 // Print the calling convention being used.
1200 switch (II->getCallingConv()) {
1201 case CallingConv::C: break; // default
1202 case CallingConv::Fast: Out << " fastcc"; break;
1203 case CallingConv::Cold: Out << " coldcc"; break;
1204 case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break;
1205 case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break;
1206 default: Out << " cc" << II->getCallingConv(); break;
1209 // If possible, print out the short form of the invoke instruction. We can
1210 // only do this if the first argument is a pointer to a nonvararg function,
1211 // and if the return type is not a pointer to a function.
1213 if (!FTy->isVarArg() &&
1214 (!isa<PointerType>(RetTy) ||
1215 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
1216 Out << ' '; printType(RetTy);
1217 writeOperand(Operand, false);
1219 writeOperand(Operand, true);
1223 for (unsigned op = 3, Eop = I.getNumOperands(); op < Eop; ++op) {
1226 writeOperand(I.getOperand(op), true);
1227 if (FTy->getParamAttrs(op-2) != FunctionType::NoAttributeSet)
1228 Out << " " << FTy->getParamAttrsText(FTy->getParamAttrs(op-2));
1232 if (FTy->getParamAttrs(0) != FunctionType::NoAttributeSet)
1233 Out << " " << FTy->getParamAttrsText(FTy->getParamAttrs(0));
1234 Out << "\n\t\t\tto";
1235 writeOperand(II->getNormalDest(), true);
1237 writeOperand(II->getUnwindDest(), true);
1239 } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(&I)) {
1241 printType(AI->getType()->getElementType());
1242 if (AI->isArrayAllocation()) {
1244 writeOperand(AI->getArraySize(), true);
1246 if (AI->getAlignment()) {
1247 Out << ", align " << AI->getAlignment();
1249 } else if (isa<CastInst>(I)) {
1250 if (Operand) writeOperand(Operand, true); // Work with broken code
1252 printType(I.getType());
1253 } else if (isa<VAArgInst>(I)) {
1254 if (Operand) writeOperand(Operand, true); // Work with broken code
1256 printType(I.getType());
1257 } else if (Operand) { // Print the normal way...
1259 // PrintAllTypes - Instructions who have operands of all the same type
1260 // omit the type from all but the first operand. If the instruction has
1261 // different type operands (for example br), then they are all printed.
1262 bool PrintAllTypes = false;
1263 const Type *TheType = Operand->getType();
1265 // Select, Store and ShuffleVector always print all types.
1266 if (isa<SelectInst>(I) || isa<StoreInst>(I) || isa<ShuffleVectorInst>(I)) {
1267 PrintAllTypes = true;
1269 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
1270 Operand = I.getOperand(i);
1271 if (Operand->getType() != TheType) {
1272 PrintAllTypes = true; // We have differing types! Print them all!
1278 if (!PrintAllTypes) {
1283 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
1285 writeOperand(I.getOperand(i), PrintAllTypes);
1289 printInfoComment(I);
1294 //===----------------------------------------------------------------------===//
1295 // External Interface declarations
1296 //===----------------------------------------------------------------------===//
1298 void Module::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
1299 SlotMachine SlotTable(this);
1300 AssemblyWriter W(o, SlotTable, this, AAW);
1304 void GlobalVariable::print(std::ostream &o) const {
1305 SlotMachine SlotTable(getParent());
1306 AssemblyWriter W(o, SlotTable, getParent(), 0);
1310 void Function::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
1311 SlotMachine SlotTable(getParent());
1312 AssemblyWriter W(o, SlotTable, getParent(), AAW);
1317 void InlineAsm::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
1318 WriteAsOperand(o, this, true, 0);
1321 void BasicBlock::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
1322 SlotMachine SlotTable(getParent());
1323 AssemblyWriter W(o, SlotTable,
1324 getParent() ? getParent()->getParent() : 0, AAW);
1328 void Instruction::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
1329 const Function *F = getParent() ? getParent()->getParent() : 0;
1330 SlotMachine SlotTable(F);
1331 AssemblyWriter W(o, SlotTable, F ? F->getParent() : 0, AAW);
1336 void Constant::print(std::ostream &o) const {
1337 if (this == 0) { o << "<null> constant value\n"; return; }
1339 o << ' ' << getType()->getDescription() << ' ';
1341 std::map<const Type *, std::string> TypeTable;
1342 WriteConstantInt(o, this, TypeTable, 0);
1345 void Type::print(std::ostream &o) const {
1349 o << getDescription();
1352 void Argument::print(std::ostream &o) const {
1353 WriteAsOperand(o, this, true, getParent() ? getParent()->getParent() : 0);
1356 // Value::dump - allow easy printing of Values from the debugger.
1357 // Located here because so much of the needed functionality is here.
1358 void Value::dump() const { print(*cerr.stream()); cerr << '\n'; }
1360 // Type::dump - allow easy printing of Values from the debugger.
1361 // Located here because so much of the needed functionality is here.
1362 void Type::dump() const { print(*cerr.stream()); cerr << '\n'; }
1364 //===----------------------------------------------------------------------===//
1365 // SlotMachine Implementation
1366 //===----------------------------------------------------------------------===//
1369 #define SC_DEBUG(X) cerr << X
1374 // Module level constructor. Causes the contents of the Module (sans functions)
1375 // to be added to the slot table.
1376 SlotMachine::SlotMachine(const Module *M)
1377 : TheModule(M) ///< Saved for lazy initialization.
1379 , FunctionProcessed(false)
1380 , mMap(), mNext(0), fMap(), fNext(0)
1384 // Function level constructor. Causes the contents of the Module and the one
1385 // function provided to be added to the slot table.
1386 SlotMachine::SlotMachine(const Function *F)
1387 : TheModule(F ? F->getParent() : 0) ///< Saved for lazy initialization
1388 , TheFunction(F) ///< Saved for lazy initialization
1389 , FunctionProcessed(false)
1390 , mMap(), mNext(0), fMap(), fNext(0)
1394 inline void SlotMachine::initialize() {
1397 TheModule = 0; ///< Prevent re-processing next time we're called.
1399 if (TheFunction && !FunctionProcessed)
1403 // Iterate through all the global variables, functions, and global
1404 // variable initializers and create slots for them.
1405 void SlotMachine::processModule() {
1406 SC_DEBUG("begin processModule!\n");
1408 // Add all of the unnamed global variables to the value table.
1409 for (Module::const_global_iterator I = TheModule->global_begin(),
1410 E = TheModule->global_end(); I != E; ++I)
1412 CreateModuleSlot(I);
1414 // Add all the unnamed functions to the table.
1415 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
1418 CreateModuleSlot(I);
1420 SC_DEBUG("end processModule!\n");
1424 // Process the arguments, basic blocks, and instructions of a function.
1425 void SlotMachine::processFunction() {
1426 SC_DEBUG("begin processFunction!\n");
1429 // Add all the function arguments with no names.
1430 for(Function::const_arg_iterator AI = TheFunction->arg_begin(),
1431 AE = TheFunction->arg_end(); AI != AE; ++AI)
1433 CreateFunctionSlot(AI);
1435 SC_DEBUG("Inserting Instructions:\n");
1437 // Add all of the basic blocks and instructions with no names.
1438 for (Function::const_iterator BB = TheFunction->begin(),
1439 E = TheFunction->end(); BB != E; ++BB) {
1441 CreateFunctionSlot(BB);
1442 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
1443 if (I->getType() != Type::VoidTy && !I->hasName())
1444 CreateFunctionSlot(I);
1447 FunctionProcessed = true;
1449 SC_DEBUG("end processFunction!\n");
1452 /// Clean up after incorporating a function. This is the only way to get out of
1453 /// the function incorporation state that affects get*Slot/Create*Slot. Function
1454 /// incorporation state is indicated by TheFunction != 0.
1455 void SlotMachine::purgeFunction() {
1456 SC_DEBUG("begin purgeFunction!\n");
1457 fMap.clear(); // Simply discard the function level map
1459 FunctionProcessed = false;
1460 SC_DEBUG("end purgeFunction!\n");
1463 /// getGlobalSlot - Get the slot number of a global value.
1464 int SlotMachine::getGlobalSlot(const GlobalValue *V) {
1465 // Check for uninitialized state and do lazy initialization.
1468 // Find the type plane in the module map
1469 ValueMap::const_iterator MI = mMap.find(V);
1470 if (MI == mMap.end()) return -1;
1476 /// getLocalSlot - Get the slot number for a value that is local to a function.
1477 int SlotMachine::getLocalSlot(const Value *V) {
1478 assert(!isa<Constant>(V) && "Can't get a constant or global slot with this!");
1480 // Check for uninitialized state and do lazy initialization.
1483 ValueMap::const_iterator FI = fMap.find(V);
1484 if (FI == fMap.end()) return -1;
1490 /// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
1491 void SlotMachine::CreateModuleSlot(const GlobalValue *V) {
1492 assert(V && "Can't insert a null Value into SlotMachine!");
1493 assert(V->getType() != Type::VoidTy && "Doesn't need a slot!");
1494 assert(!V->hasName() && "Doesn't need a slot!");
1496 unsigned DestSlot = mNext++;
1499 SC_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
1501 // G = Global, F = Function, o = other
1502 SC_DEBUG((isa<GlobalVariable>(V) ? 'G' : 'F') << "]\n");
1506 /// CreateSlot - Create a new slot for the specified value if it has no name.
1507 void SlotMachine::CreateFunctionSlot(const Value *V) {
1508 const Type *VTy = V->getType();
1509 assert(VTy != Type::VoidTy && !V->hasName() && "Doesn't need a slot!");
1511 unsigned DestSlot = fNext++;
1514 // G = Global, F = Function, o = other
1515 SC_DEBUG(" Inserting value [" << VTy << "] = " << V << " slot=" <<
1516 DestSlot << " [o]\n");