1 //===-- AsmWriter.cpp - Printing LLVM as an assembly file -----------------===//
3 // This library implements the functionality defined in llvm/Assembly/Writer.h
5 // Note that these routines must be extremely tolerant of various errors in the
6 // LLVM code, because of of the primary uses of it is for debugging
9 //===----------------------------------------------------------------------===//
11 #include "llvm/Assembly/CachedWriter.h"
12 #include "llvm/Assembly/Writer.h"
13 #include "llvm/SlotCalculator.h"
14 #include "llvm/DerivedTypes.h"
15 #include "llvm/Module.h"
16 #include "llvm/Constants.h"
17 #include "llvm/iMemory.h"
18 #include "llvm/iTerminators.h"
19 #include "llvm/iPHINode.h"
20 #include "llvm/iOther.h"
21 #include "llvm/SymbolTable.h"
22 #include "Support/StringExtras.h"
23 #include "Support/STLExtras.h"
30 static void WriteAsOperandInternal(ostream &Out, const Value *V, bool PrintName,
31 map<const Type *, string> &TypeTable,
32 SlotCalculator *Table);
34 static const Module *getModuleFromVal(const Value *V) {
35 if (const Argument *MA = dyn_cast<const Argument>(V))
36 return MA->getParent() ? MA->getParent()->getParent() : 0;
37 else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(V))
38 return BB->getParent() ? BB->getParent()->getParent() : 0;
39 else if (const Instruction *I = dyn_cast<const Instruction>(V)) {
40 const Function *M = I->getParent() ? I->getParent()->getParent() : 0;
41 return M ? M->getParent() : 0;
42 } else if (const GlobalValue *GV = dyn_cast<const GlobalValue>(V))
43 return GV->getParent();
47 static SlotCalculator *createSlotCalculator(const Value *V) {
48 assert(!isa<Type>(V) && "Can't create an SC for a type!");
49 if (const Argument *FA = dyn_cast<const Argument>(V)) {
50 return new SlotCalculator(FA->getParent(), true);
51 } else if (const Instruction *I = dyn_cast<const Instruction>(V)) {
52 return new SlotCalculator(I->getParent()->getParent(), true);
53 } else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(V)) {
54 return new SlotCalculator(BB->getParent(), true);
55 } else if (const GlobalVariable *GV = dyn_cast<const GlobalVariable>(V)){
56 return new SlotCalculator(GV->getParent(), true);
57 } else if (const Function *Func = dyn_cast<const Function>(V)) {
58 return new SlotCalculator(Func, true);
64 // If the module has a symbol table, take all global types and stuff their
65 // names into the TypeNames map.
67 static void fillTypeNameTable(const Module *M,
68 map<const Type *, string> &TypeNames) {
69 if (M && M->hasSymbolTable()) {
70 const SymbolTable *ST = M->getSymbolTable();
71 SymbolTable::const_iterator PI = ST->find(Type::TypeTy);
72 if (PI != ST->end()) {
73 SymbolTable::type_const_iterator I = PI->second.begin();
74 for (; I != PI->second.end(); ++I) {
75 // As a heuristic, don't insert pointer to primitive types, because
76 // they are used too often to have a single useful name.
78 const Type *Ty = cast<const Type>(I->second);
79 if (!isa<PointerType>(Ty) ||
80 !cast<PointerType>(Ty)->getElementType()->isPrimitiveType())
81 TypeNames.insert(std::make_pair(Ty, "%"+I->first));
89 static string calcTypeName(const Type *Ty, vector<const Type *> &TypeStack,
90 map<const Type *, string> &TypeNames) {
91 if (Ty->isPrimitiveType()) return Ty->getDescription(); // Base case
93 // Check to see if the type is named.
94 map<const Type *, string>::iterator I = TypeNames.find(Ty);
95 if (I != TypeNames.end()) return I->second;
97 // Check to see if the Type is already on the stack...
98 unsigned Slot = 0, CurSize = TypeStack.size();
99 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
101 // This is another base case for the recursion. In this case, we know
102 // that we have looped back to a type that we have previously visited.
103 // Generate the appropriate upreference to handle this.
106 return "\\" + utostr(CurSize-Slot); // Here's the upreference
108 TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
111 switch (Ty->getPrimitiveID()) {
112 case Type::FunctionTyID: {
113 const FunctionType *FTy = cast<const FunctionType>(Ty);
114 Result = calcTypeName(FTy->getReturnType(), TypeStack, TypeNames) + " (";
115 for (FunctionType::ParamTypes::const_iterator
116 I = FTy->getParamTypes().begin(),
117 E = FTy->getParamTypes().end(); I != E; ++I) {
118 if (I != FTy->getParamTypes().begin())
120 Result += calcTypeName(*I, TypeStack, TypeNames);
122 if (FTy->isVarArg()) {
123 if (!FTy->getParamTypes().empty()) Result += ", ";
129 case Type::StructTyID: {
130 const StructType *STy = cast<const StructType>(Ty);
132 for (StructType::ElementTypes::const_iterator
133 I = STy->getElementTypes().begin(),
134 E = STy->getElementTypes().end(); I != E; ++I) {
135 if (I != STy->getElementTypes().begin())
137 Result += calcTypeName(*I, TypeStack, TypeNames);
142 case Type::PointerTyID:
143 Result = calcTypeName(cast<const PointerType>(Ty)->getElementType(),
144 TypeStack, TypeNames) + "*";
146 case Type::ArrayTyID: {
147 const ArrayType *ATy = cast<const ArrayType>(Ty);
148 Result = "[" + utostr(ATy->getNumElements()) + " x ";
149 Result += calcTypeName(ATy->getElementType(), TypeStack, TypeNames) + "]";
153 assert(0 && "Unhandled case in getTypeProps!");
157 TypeStack.pop_back(); // Remove self from stack...
162 // printTypeInt - The internal guts of printing out a type that has a
163 // potentially named portion.
165 static ostream &printTypeInt(ostream &Out, const Type *Ty,
166 map<const Type *, string> &TypeNames) {
167 // Primitive types always print out their description, regardless of whether
168 // they have been named or not.
170 if (Ty->isPrimitiveType()) return Out << Ty->getDescription();
172 // Check to see if the type is named.
173 map<const Type *, string>::iterator I = TypeNames.find(Ty);
174 if (I != TypeNames.end()) return Out << I->second;
176 // Otherwise we have a type that has not been named but is a derived type.
177 // Carefully recurse the type hierarchy to print out any contained symbolic
180 vector<const Type *> TypeStack;
181 string TypeName = calcTypeName(Ty, TypeStack, TypeNames);
182 TypeNames.insert(std::make_pair(Ty, TypeName));//Cache type name for later use
183 return Out << TypeName;
187 // WriteTypeSymbolic - This attempts to write the specified type as a symbolic
188 // type, iff there is an entry in the modules symbol table for the specified
189 // type or one of it's component types. This is slower than a simple x << Type;
191 ostream &WriteTypeSymbolic(ostream &Out, const Type *Ty, const Module *M) {
194 // If they want us to print out a type, attempt to make it symbolic if there
195 // is a symbol table in the module...
196 if (M && M->hasSymbolTable()) {
197 map<const Type *, string> TypeNames;
198 fillTypeNameTable(M, TypeNames);
200 return printTypeInt(Out, Ty, TypeNames);
202 return Out << Ty->getDescription();
206 static void WriteConstantInt(ostream &Out, const Constant *CV, bool PrintName,
207 map<const Type *, string> &TypeTable,
208 SlotCalculator *Table) {
209 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
210 Out << (CB == ConstantBool::True ? "true" : "false");
211 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV)) {
212 Out << CI->getValue();
213 } else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV)) {
214 Out << CI->getValue();
215 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
216 // We would like to output the FP constant value in exponential notation,
217 // but we cannot do this if doing so will lose precision. Check here to
218 // make sure that we only output it in exponential format if we can parse
219 // the value back and get the same value.
221 std::string StrVal = ftostr(CFP->getValue());
223 // Check to make sure that the stringized number is not some string like
224 // "Inf" or NaN, that atof will accept, but the lexer will not. Check that
225 // the string matches the "[-+]?[0-9]" regex.
227 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
228 ((StrVal[0] == '-' || StrVal[0] == '+') &&
229 (StrVal[0] >= '0' && StrVal[0] <= '9')))
230 // Reparse stringized version!
231 if (atof(StrVal.c_str()) == CFP->getValue()) {
232 Out << StrVal; return;
235 // Otherwise we could not reparse it to exactly the same value, so we must
236 // output the string in hexadecimal format!
238 // Behave nicely in the face of C TBAA rules... see:
239 // http://www.nullstone.com/htmls/category/aliastyp.htm
241 double Val = CFP->getValue();
242 char *Ptr = (char*)&Val;
243 assert(sizeof(double) == sizeof(uint64_t) && sizeof(double) == 8 &&
244 "assuming that double is 64 bits!");
245 Out << "0x" << utohexstr(*(uint64_t*)Ptr);
247 } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
248 // As a special case, print the array as a string if it is an array of
249 // ubytes or an array of sbytes with positive values.
251 const Type *ETy = CA->getType()->getElementType();
252 bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
254 if (ETy == Type::SByteTy)
255 for (unsigned i = 0; i < CA->getNumOperands(); ++i)
256 if (cast<ConstantSInt>(CA->getOperand(i))->getValue() < 0) {
263 for (unsigned i = 0; i < CA->getNumOperands(); ++i) {
264 unsigned char C = (ETy == Type::SByteTy) ?
265 (unsigned char)cast<ConstantSInt>(CA->getOperand(i))->getValue() :
266 (unsigned char)cast<ConstantUInt>(CA->getOperand(i))->getValue();
272 << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'))
273 << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
278 } else { // Cannot output in string format...
280 if (CA->getNumOperands()) {
282 printTypeInt(Out, ETy, TypeTable);
283 WriteAsOperandInternal(Out, CA->getOperand(0),
284 PrintName, TypeTable, Table);
285 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
287 printTypeInt(Out, ETy, TypeTable);
288 WriteAsOperandInternal(Out, CA->getOperand(i), PrintName,
294 } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
296 if (CS->getNumOperands()) {
298 printTypeInt(Out, CS->getOperand(0)->getType(), TypeTable);
300 WriteAsOperandInternal(Out, CS->getOperand(0),
301 PrintName, TypeTable, Table);
303 for (unsigned i = 1; i < CS->getNumOperands(); i++) {
305 printTypeInt(Out, CS->getOperand(i)->getType(), TypeTable);
307 WriteAsOperandInternal(Out, CS->getOperand(i),
308 PrintName, TypeTable, Table);
313 } else if (isa<ConstantPointerNull>(CV)) {
316 } else if (const ConstantPointerRef *PR = dyn_cast<ConstantPointerRef>(CV)) {
317 const GlobalValue *V = PR->getValue();
319 Out << "%" << V->getName();
321 int Slot = Table->getValSlot(V);
325 Out << "<pointer reference badref>";
327 Out << "<pointer reference without context info>";
330 assert(0 && "Unrecognized constant value!!!");
335 // WriteAsOperand - Write the name of the specified value out to the specified
336 // ostream. This can be useful when you just want to print int %reg126, not the
337 // whole instruction that generated it.
339 static void WriteAsOperandInternal(ostream &Out, const Value *V, bool PrintName,
340 map<const Type *, string> &TypeTable,
341 SlotCalculator *Table) {
343 if (PrintName && V->hasName()) {
344 Out << "%" << V->getName();
346 if (const Constant *CV = dyn_cast<const Constant>(V)) {
347 WriteConstantInt(Out, CV, PrintName, TypeTable, Table);
351 Slot = Table->getValSlot(V);
353 if (const Type *Ty = dyn_cast<const Type>(V)) {
354 Out << Ty->getDescription();
358 Table = createSlotCalculator(V);
359 if (Table == 0) { Out << "BAD VALUE TYPE!"; return; }
361 Slot = Table->getValSlot(V);
364 if (Slot >= 0) Out << "%" << Slot;
366 Out << "<badref>"; // Not embeded into a location?
373 // WriteAsOperand - Write the name of the specified value out to the specified
374 // ostream. This can be useful when you just want to print int %reg126, not the
375 // whole instruction that generated it.
377 ostream &WriteAsOperand(ostream &Out, const Value *V, bool PrintType,
378 bool PrintName, SlotCalculator *Table) {
379 map<const Type *, string> TypeNames;
380 const Module *M = getModuleFromVal(V);
382 if (M && M->hasSymbolTable())
383 fillTypeNameTable(M, TypeNames);
386 printTypeInt(Out, V->getType(), TypeNames);
388 WriteAsOperandInternal(Out, V, PrintName, TypeNames, Table);
394 class AssemblyWriter {
396 SlotCalculator &Table;
397 const Module *TheModule;
398 map<const Type *, string> TypeNames;
400 inline AssemblyWriter(ostream &o, SlotCalculator &Tab, const Module *M)
401 : Out(o), Table(Tab), TheModule(M) {
403 // If the module has a symbol table, take all global types and stuff their
404 // names into the TypeNames map.
406 fillTypeNameTable(M, TypeNames);
409 inline void write(const Module *M) { printModule(M); }
410 inline void write(const GlobalVariable *G) { printGlobal(G); }
411 inline void write(const Function *F) { printFunction(F); }
412 inline void write(const BasicBlock *BB) { printBasicBlock(BB); }
413 inline void write(const Instruction *I) { printInstruction(*I); }
414 inline void write(const Constant *CPV) { printConstant(CPV); }
415 inline void write(const Type *Ty) { printType(Ty); }
417 void writeOperand(const Value *Op, bool PrintType, bool PrintName = true);
420 void printModule(const Module *M);
421 void printSymbolTable(const SymbolTable &ST);
422 void printConstant(const Constant *CPV);
423 void printGlobal(const GlobalVariable *GV);
424 void printFunction(const Function *F);
425 void printArgument(const Argument *FA);
426 void printBasicBlock(const BasicBlock *BB);
427 void printInstruction(const Instruction &I);
429 // printType - Go to extreme measures to attempt to print out a short,
430 // symbolic version of a type name.
432 ostream &printType(const Type *Ty) {
433 return printTypeInt(Out, Ty, TypeNames);
436 // printTypeAtLeastOneLevel - Print out one level of the possibly complex type
437 // without considering any symbolic types that we may have equal to it.
439 ostream &printTypeAtLeastOneLevel(const Type *Ty);
441 // printInfoComment - Print a little comment after the instruction indicating
442 // which slot it occupies.
443 void printInfoComment(const Value &V);
447 // printTypeAtLeastOneLevel - Print out one level of the possibly complex type
448 // without considering any symbolic types that we may have equal to it.
450 ostream &AssemblyWriter::printTypeAtLeastOneLevel(const Type *Ty) {
451 if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
452 printType(FTy->getReturnType()) << " (";
453 for (FunctionType::ParamTypes::const_iterator
454 I = FTy->getParamTypes().begin(),
455 E = FTy->getParamTypes().end(); I != E; ++I) {
456 if (I != FTy->getParamTypes().begin())
460 if (FTy->isVarArg()) {
461 if (!FTy->getParamTypes().empty()) Out << ", ";
465 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
467 for (StructType::ElementTypes::const_iterator
468 I = STy->getElementTypes().begin(),
469 E = STy->getElementTypes().end(); I != E; ++I) {
470 if (I != STy->getElementTypes().begin())
475 } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
476 printType(PTy->getElementType()) << "*";
477 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
478 Out << "[" << ATy->getNumElements() << " x ";
479 printType(ATy->getElementType()) << "]";
480 } else if (const OpaqueType *OTy = dyn_cast<OpaqueType>(Ty)) {
481 Out << OTy->getDescription();
483 assert(Ty->isPrimitiveType() && "Unknown derived type!");
490 void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType,
492 if (PrintType) { Out << " "; printType(Operand->getType()); }
493 WriteAsOperandInternal(Out, Operand, PrintName, TypeNames, &Table);
497 void AssemblyWriter::printModule(const Module *M) {
498 // Loop over the symbol table, emitting all named constants...
499 if (M->hasSymbolTable())
500 printSymbolTable(*M->getSymbolTable());
502 for (Module::const_giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
505 Out << "\nimplementation ; Functions:\n";
507 // Output all of the functions...
508 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
512 void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
513 if (GV->hasName()) Out << "%" << GV->getName() << " = ";
515 if (GV->hasInternalLinkage()) Out << "internal ";
516 if (!GV->hasInitializer()) Out << "uninitialized ";
518 Out << (GV->isConstant() ? "constant " : "global ");
519 printType(GV->getType()->getElementType());
521 if (GV->hasInitializer())
522 writeOperand(GV->getInitializer(), false, false);
524 printInfoComment(*GV);
529 // printSymbolTable - Run through symbol table looking for named constants
530 // if a named constant is found, emit it's declaration...
532 void AssemblyWriter::printSymbolTable(const SymbolTable &ST) {
533 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
534 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
535 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
537 for (; I != End; ++I) {
538 const Value *V = I->second;
539 if (const Constant *CPV = dyn_cast<const Constant>(V)) {
541 } else if (const Type *Ty = dyn_cast<const Type>(V)) {
542 Out << "\t%" << I->first << " = type ";
544 // Make sure we print out at least one level of the type structure, so
545 // that we do not get %FILE = type %FILE
547 printTypeAtLeastOneLevel(Ty) << "\n";
554 // printConstant - Print out a constant pool entry...
556 void AssemblyWriter::printConstant(const Constant *CPV) {
557 // Don't print out unnamed constants, they will be inlined
558 if (!CPV->hasName()) return;
561 Out << "\t%" << CPV->getName() << " =";
563 // Write the value out now...
564 writeOperand(CPV, true, false);
566 printInfoComment(*CPV);
570 // printFunction - Print all aspects of a function.
572 void AssemblyWriter::printFunction(const Function *F) {
573 // Print out the return type and name...
574 Out << "\n" << (F->isExternal() ? "declare " : "")
575 << (F->hasInternalLinkage() ? "internal " : "");
576 printType(F->getReturnType()) << " %" << F->getName() << "(";
577 Table.incorporateFunction(F);
579 // Loop over the arguments, printing them...
580 const FunctionType *FT = F->getFunctionType();
582 if (!F->isExternal()) {
583 for(Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I)
586 // Loop over the arguments, printing them...
587 for (FunctionType::ParamTypes::const_iterator I = FT->getParamTypes().begin(),
588 E = FT->getParamTypes().end(); I != E; ++I) {
589 if (I != FT->getParamTypes().begin()) Out << ", ";
594 // Finish printing arguments...
595 if (FT->isVarArg()) {
596 if (FT->getParamTypes().size()) Out << ", ";
597 Out << "..."; // Output varargs portion of signature!
601 if (F->isExternal()) {
606 // Output all of its basic blocks... for the function
607 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
613 Table.purgeFunction();
616 // printArgument - This member is called for every argument that
617 // is passed into the function. Simply print it out
619 void AssemblyWriter::printArgument(const Argument *Arg) {
620 // Insert commas as we go... the first arg doesn't get a comma
621 if (Arg != &Arg->getParent()->afront()) Out << ", ";
624 printType(Arg->getType());
626 // Output name, if available...
628 Out << " %" << Arg->getName();
629 else if (Table.getValSlot(Arg) < 0)
633 // printBasicBlock - This member is called for each basic block in a methd.
635 void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
636 if (BB->hasName()) { // Print out the label if it exists...
637 Out << "\n" << BB->getName() << ":\t\t\t\t\t;[#uses="
638 << BB->use_size() << "]"; // Output # uses
639 } else if (!BB->use_empty()) { // Don't print block # of no uses...
640 int Slot = Table.getValSlot(BB);
641 Out << "\n; <label>:";
643 Out << Slot; // Extra newline seperates out label's
646 Out << "\t\t\t\t\t;[#uses=" << BB->use_size() << "]"; // Output # uses
651 // Output all of the instructions in the basic block...
652 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
653 printInstruction(*I);
657 // printInfoComment - Print a little comment after the instruction indicating
658 // which slot it occupies.
660 void AssemblyWriter::printInfoComment(const Value &V) {
661 if (V.getType() != Type::VoidTy) {
663 printType(V.getType()) << ">";
666 int Slot = Table.getValSlot(&V); // Print out the def slot taken...
667 if (Slot >= 0) Out << ":" << Slot;
668 else Out << ":<badref>";
670 Out << " [#uses=" << V.use_size() << "]"; // Output # uses
674 // printInstruction - This member is called for each Instruction in a methd.
676 void AssemblyWriter::printInstruction(const Instruction &I) {
679 // Print out name if it exists...
681 Out << "%" << I.getName() << " = ";
683 // Print out the opcode...
684 Out << I.getOpcodeName();
686 // Print out the type of the operands...
687 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
689 // Special case conditional branches to swizzle the condition out to the front
690 if (isa<BranchInst>(I) && I.getNumOperands() > 1) {
691 writeOperand(I.getOperand(2), true);
693 writeOperand(Operand, true);
695 writeOperand(I.getOperand(1), true);
697 } else if (isa<SwitchInst>(I)) {
698 // Special case switch statement to get formatting nice and correct...
699 writeOperand(Operand , true); Out << ",";
700 writeOperand(I.getOperand(1), true); Out << " [";
702 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; op += 2) {
704 writeOperand(I.getOperand(op ), true); Out << ",";
705 writeOperand(I.getOperand(op+1), true);
708 } else if (isa<PHINode>(I)) {
710 printType(I.getType());
713 for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) {
716 writeOperand(I.getOperand(op ), false); Out << ",";
717 writeOperand(I.getOperand(op+1), false); Out << " ]";
719 } else if (isa<ReturnInst>(I) && !Operand) {
721 } else if (isa<CallInst>(I)) {
722 const PointerType *PTy = dyn_cast<PointerType>(Operand->getType());
723 const FunctionType*MTy = PTy ? dyn_cast<FunctionType>(PTy->getElementType()):0;
724 const Type *RetTy = MTy ? MTy->getReturnType() : 0;
726 // If possible, print out the short form of the call instruction, but we can
727 // only do this if the first argument is a pointer to a nonvararg function,
728 // and if the value returned is not a pointer to a function.
730 if (RetTy && MTy && !MTy->isVarArg() &&
731 (!isa<PointerType>(RetTy) ||
732 !isa<FunctionType>(cast<PointerType>(RetTy)))) {
733 Out << " "; printType(RetTy);
734 writeOperand(Operand, false);
736 writeOperand(Operand, true);
739 if (I.getNumOperands() > 1) writeOperand(I.getOperand(1), true);
740 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; ++op) {
742 writeOperand(I.getOperand(op), true);
746 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
747 // TODO: Should try to print out short form of the Invoke instruction
748 writeOperand(Operand, true);
750 if (I.getNumOperands() > 3) writeOperand(I.getOperand(3), true);
751 for (unsigned op = 4, Eop = I.getNumOperands(); op < Eop; ++op) {
753 writeOperand(I.getOperand(op), true);
756 Out << " )\n\t\t\tto";
757 writeOperand(II->getNormalDest(), true);
759 writeOperand(II->getExceptionalDest(), true);
761 } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(&I)) {
763 printType(AI->getType()->getElementType());
764 if (AI->isArrayAllocation()) {
766 writeOperand(AI->getArraySize(), true);
768 } else if (isa<CastInst>(I)) {
769 if (Operand) writeOperand(Operand, true);
771 printType(I.getType());
772 } else if (Operand) { // Print the normal way...
774 // PrintAllTypes - Instructions who have operands of all the same type
775 // omit the type from all but the first operand. If the instruction has
776 // different type operands (for example br), then they are all printed.
777 bool PrintAllTypes = false;
778 const Type *TheType = Operand->getType();
780 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
781 Operand = I.getOperand(i);
782 if (Operand->getType() != TheType) {
783 PrintAllTypes = true; // We have differing types! Print them all!
788 // Shift Left & Right print both types even for Ubyte LHS
789 if (isa<ShiftInst>(I)) PrintAllTypes = true;
791 if (!PrintAllTypes) {
793 printType(I.getOperand(0)->getType());
796 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
798 writeOperand(I.getOperand(i), PrintAllTypes);
807 //===----------------------------------------------------------------------===//
808 // External Interface declarations
809 //===----------------------------------------------------------------------===//
812 void Module::print(std::ostream &o) const {
813 SlotCalculator SlotTable(this, true);
814 AssemblyWriter W(o, SlotTable, this);
818 void GlobalVariable::print(std::ostream &o) const {
819 SlotCalculator SlotTable(getParent(), true);
820 AssemblyWriter W(o, SlotTable, getParent());
824 void Function::print(std::ostream &o) const {
825 SlotCalculator SlotTable(getParent(), true);
826 AssemblyWriter W(o, SlotTable, getParent());
831 void BasicBlock::print(std::ostream &o) const {
832 SlotCalculator SlotTable(getParent(), true);
833 AssemblyWriter W(o, SlotTable,
834 getParent() ? getParent()->getParent() : 0);
838 void Instruction::print(std::ostream &o) const {
839 const Function *F = getParent() ? getParent()->getParent() : 0;
840 SlotCalculator SlotTable(F, true);
841 AssemblyWriter W(o, SlotTable, F ? F->getParent() : 0);
846 void Constant::print(std::ostream &o) const {
847 if (this == 0) { o << "<null> constant value\n"; return; }
848 o << " " << getType()->getDescription() << " ";
850 map<const Type *, string> TypeTable;
851 WriteConstantInt(o, this, false, TypeTable, 0);
854 void Type::print(std::ostream &o) const {
858 o << getDescription();
861 void Argument::print(std::ostream &o) const {
862 o << getType() << " " << getName();
865 void Value::dump() const { print(std::cerr); }
867 //===----------------------------------------------------------------------===//
868 // CachedWriter Class Implementation
869 //===----------------------------------------------------------------------===//
871 void CachedWriter::setModule(const Module *M) {
872 delete SC; delete AW;
874 SC = new SlotCalculator(M, true);
875 AW = new AssemblyWriter(Out, *SC, M);
881 CachedWriter::~CachedWriter() {
886 CachedWriter &CachedWriter::operator<<(const Value *V) {
887 assert(AW && SC && "CachedWriter does not have a current module!");
888 switch (V->getValueType()) {
889 case Value::ConstantVal:
890 case Value::ArgumentVal: AW->writeOperand(V, true, true); break;
891 case Value::TypeVal: AW->write(cast<const Type>(V)); break;
892 case Value::InstructionVal: AW->write(cast<Instruction>(V)); break;
893 case Value::BasicBlockVal: AW->write(cast<BasicBlock>(V)); break;
894 case Value::FunctionVal: AW->write(cast<Function>(V)); break;
895 case Value::GlobalVariableVal: AW->write(cast<GlobalVariable>(V)); break;
896 default: Out << "<unknown value type: " << V->getValueType() << ">"; break;