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/Function.h"
17 #include "llvm/GlobalVariable.h"
18 #include "llvm/BasicBlock.h"
19 #include "llvm/Constants.h"
20 #include "llvm/iMemory.h"
21 #include "llvm/iTerminators.h"
22 #include "llvm/iPHINode.h"
23 #include "llvm/iOther.h"
24 #include "llvm/SymbolTable.h"
25 #include "llvm/Argument.h"
26 #include "Support/StringExtras.h"
27 #include "Support/STLExtras.h"
34 static void WriteAsOperandInternal(ostream &Out, const Value *V, bool PrintName,
35 map<const Type *, string> &TypeTable,
36 SlotCalculator *Table);
38 static const Module *getModuleFromVal(const Value *V) {
39 if (const Argument *MA = dyn_cast<const Argument>(V))
40 return MA->getParent() ? MA->getParent()->getParent() : 0;
41 else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(V))
42 return BB->getParent() ? BB->getParent()->getParent() : 0;
43 else if (const Instruction *I = dyn_cast<const Instruction>(V)) {
44 const Function *M = I->getParent() ? I->getParent()->getParent() : 0;
45 return M ? M->getParent() : 0;
46 } else if (const GlobalValue *GV = dyn_cast<const GlobalValue>(V))
47 return GV->getParent();
51 static SlotCalculator *createSlotCalculator(const Value *V) {
52 assert(!isa<Type>(V) && "Can't create an SC for a type!");
53 if (const Argument *FA = dyn_cast<const Argument>(V)) {
54 return new SlotCalculator(FA->getParent(), true);
55 } else if (const Instruction *I = dyn_cast<const Instruction>(V)) {
56 return new SlotCalculator(I->getParent()->getParent(), true);
57 } else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(V)) {
58 return new SlotCalculator(BB->getParent(), true);
59 } else if (const GlobalVariable *GV = dyn_cast<const GlobalVariable>(V)){
60 return new SlotCalculator(GV->getParent(), true);
61 } else if (const Function *Func = dyn_cast<const Function>(V)) {
62 return new SlotCalculator(Func, true);
68 // If the module has a symbol table, take all global types and stuff their
69 // names into the TypeNames map.
71 static void fillTypeNameTable(const Module *M,
72 map<const Type *, string> &TypeNames) {
73 if (M && M->hasSymbolTable()) {
74 const SymbolTable *ST = M->getSymbolTable();
75 SymbolTable::const_iterator PI = ST->find(Type::TypeTy);
76 if (PI != ST->end()) {
77 SymbolTable::type_const_iterator I = PI->second.begin();
78 for (; I != PI->second.end(); ++I) {
79 // As a heuristic, don't insert pointer to primitive types, because
80 // they are used too often to have a single useful name.
82 const Type *Ty = cast<const Type>(I->second);
83 if (!isa<PointerType>(Ty) ||
84 !cast<PointerType>(Ty)->getElementType()->isPrimitiveType())
85 TypeNames.insert(std::make_pair(Ty, "%"+I->first));
93 static string calcTypeName(const Type *Ty, vector<const Type *> &TypeStack,
94 map<const Type *, string> &TypeNames) {
95 if (Ty->isPrimitiveType()) return Ty->getDescription(); // Base case
97 // Check to see if the type is named.
98 map<const Type *, string>::iterator I = TypeNames.find(Ty);
99 if (I != TypeNames.end()) return I->second;
101 // Check to see if the Type is already on the stack...
102 unsigned Slot = 0, CurSize = TypeStack.size();
103 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
105 // This is another base case for the recursion. In this case, we know
106 // that we have looped back to a type that we have previously visited.
107 // Generate the appropriate upreference to handle this.
110 return "\\" + utostr(CurSize-Slot); // Here's the upreference
112 TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
115 switch (Ty->getPrimitiveID()) {
116 case Type::FunctionTyID: {
117 const FunctionType *FTy = cast<const FunctionType>(Ty);
118 Result = calcTypeName(FTy->getReturnType(), TypeStack, TypeNames) + " (";
119 for (FunctionType::ParamTypes::const_iterator
120 I = FTy->getParamTypes().begin(),
121 E = FTy->getParamTypes().end(); I != E; ++I) {
122 if (I != FTy->getParamTypes().begin())
124 Result += calcTypeName(*I, TypeStack, TypeNames);
126 if (FTy->isVarArg()) {
127 if (!FTy->getParamTypes().empty()) Result += ", ";
133 case Type::StructTyID: {
134 const StructType *STy = cast<const StructType>(Ty);
136 for (StructType::ElementTypes::const_iterator
137 I = STy->getElementTypes().begin(),
138 E = STy->getElementTypes().end(); I != E; ++I) {
139 if (I != STy->getElementTypes().begin())
141 Result += calcTypeName(*I, TypeStack, TypeNames);
146 case Type::PointerTyID:
147 Result = calcTypeName(cast<const PointerType>(Ty)->getElementType(),
148 TypeStack, TypeNames) + "*";
150 case Type::ArrayTyID: {
151 const ArrayType *ATy = cast<const ArrayType>(Ty);
152 Result = "[" + utostr(ATy->getNumElements()) + " x ";
153 Result += calcTypeName(ATy->getElementType(), TypeStack, TypeNames) + "]";
157 assert(0 && "Unhandled case in getTypeProps!");
161 TypeStack.pop_back(); // Remove self from stack...
166 // printTypeInt - The internal guts of printing out a type that has a
167 // potentially named portion.
169 static ostream &printTypeInt(ostream &Out, const Type *Ty,
170 map<const Type *, string> &TypeNames) {
171 // Primitive types always print out their description, regardless of whether
172 // they have been named or not.
174 if (Ty->isPrimitiveType()) return Out << Ty->getDescription();
176 // Check to see if the type is named.
177 map<const Type *, string>::iterator I = TypeNames.find(Ty);
178 if (I != TypeNames.end()) return Out << I->second;
180 // Otherwise we have a type that has not been named but is a derived type.
181 // Carefully recurse the type hierarchy to print out any contained symbolic
184 vector<const Type *> TypeStack;
185 string TypeName = calcTypeName(Ty, TypeStack, TypeNames);
186 TypeNames.insert(std::make_pair(Ty, TypeName));//Cache type name for later use
187 return Out << TypeName;
191 // WriteTypeSymbolic - This attempts to write the specified type as a symbolic
192 // type, iff there is an entry in the modules symbol table for the specified
193 // type or one of it's component types. This is slower than a simple x << Type;
195 ostream &WriteTypeSymbolic(ostream &Out, const Type *Ty, const Module *M) {
198 // If they want us to print out a type, attempt to make it symbolic if there
199 // is a symbol table in the module...
200 if (M && M->hasSymbolTable()) {
201 map<const Type *, string> TypeNames;
202 fillTypeNameTable(M, TypeNames);
204 return printTypeInt(Out, Ty, TypeNames);
206 return Out << Ty->getDescription();
210 static void WriteConstantInt(ostream &Out, const Constant *CV, bool PrintName,
211 map<const Type *, string> &TypeTable,
212 SlotCalculator *Table) {
213 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
214 Out << (CB == ConstantBool::True ? "true" : "false");
215 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV)) {
216 Out << CI->getValue();
217 } else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV)) {
218 Out << CI->getValue();
219 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
220 // We would like to output the FP constant value in exponential notation,
221 // but we cannot do this if doing so will lose precision. Check here to
222 // make sure that we only output it in exponential format if we can parse
223 // the value back and get the same value.
225 std::string StrVal = ftostr(CFP->getValue());
227 // Check to make sure that the stringized number is not some string like
228 // "Inf" or NaN, that atof will accept, but the lexer will not. Check that
229 // the string matches the "[-+]?[0-9]" regex.
231 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
232 ((StrVal[0] == '-' || StrVal[0] == '+') &&
233 (StrVal[0] >= '0' && StrVal[0] <= '9')))
234 // Reparse stringized version!
235 if (atof(StrVal.c_str()) == CFP->getValue()) {
236 Out << StrVal; return;
239 // Otherwise we could not reparse it to exactly the same value, so we must
240 // output the string in hexadecimal format!
242 // Behave nicely in the face of C TBAA rules... see:
243 // http://www.nullstone.com/htmls/category/aliastyp.htm
245 double Val = CFP->getValue();
246 char *Ptr = (char*)&Val;
247 assert(sizeof(double) == sizeof(uint64_t) && sizeof(double) == 8 &&
248 "assuming that double is 64 bits!");
249 Out << "0x" << utohexstr(*(uint64_t*)Ptr);
251 } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
252 // As a special case, print the array as a string if it is an array of
253 // ubytes or an array of sbytes with positive values.
255 const Type *ETy = CA->getType()->getElementType();
256 bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
258 if (ETy == Type::SByteTy)
259 for (unsigned i = 0; i < CA->getNumOperands(); ++i)
260 if (cast<ConstantSInt>(CA->getOperand(i))->getValue() < 0) {
267 for (unsigned i = 0; i < CA->getNumOperands(); ++i) {
268 unsigned char C = (ETy == Type::SByteTy) ?
269 (unsigned char)cast<ConstantSInt>(CA->getOperand(i))->getValue() :
270 (unsigned char)cast<ConstantUInt>(CA->getOperand(i))->getValue();
276 << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'))
277 << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
282 } else { // Cannot output in string format...
284 if (CA->getNumOperands()) {
286 printTypeInt(Out, ETy, TypeTable);
287 WriteAsOperandInternal(Out, CA->getOperand(0),
288 PrintName, TypeTable, Table);
289 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
291 printTypeInt(Out, ETy, TypeTable);
292 WriteAsOperandInternal(Out, CA->getOperand(i), PrintName,
298 } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
300 if (CS->getNumOperands()) {
302 printTypeInt(Out, CS->getOperand(0)->getType(), TypeTable);
304 WriteAsOperandInternal(Out, CS->getOperand(0),
305 PrintName, TypeTable, Table);
307 for (unsigned i = 1; i < CS->getNumOperands(); i++) {
309 printTypeInt(Out, CS->getOperand(i)->getType(), TypeTable);
311 WriteAsOperandInternal(Out, CS->getOperand(i),
312 PrintName, TypeTable, Table);
317 } else if (isa<ConstantPointerNull>(CV)) {
320 } else if (ConstantPointerRef *PR = dyn_cast<ConstantPointerRef>(CV)) {
321 const GlobalValue *V = PR->getValue();
323 Out << "%" << V->getName();
325 int Slot = Table->getValSlot(V);
329 Out << "<pointer reference badref>";
331 Out << "<pointer reference without context info>";
334 assert(0 && "Unrecognized constant value!!!");
339 // WriteAsOperand - Write the name of the specified value out to the specified
340 // ostream. This can be useful when you just want to print int %reg126, not the
341 // whole instruction that generated it.
343 static void WriteAsOperandInternal(ostream &Out, const Value *V, bool PrintName,
344 map<const Type *, string> &TypeTable,
345 SlotCalculator *Table) {
347 if (PrintName && V->hasName()) {
348 Out << "%" << V->getName();
350 if (const Constant *CV = dyn_cast<const Constant>(V)) {
351 WriteConstantInt(Out, CV, PrintName, TypeTable, Table);
355 Slot = Table->getValSlot(V);
357 if (const Type *Ty = dyn_cast<const Type>(V)) {
358 Out << Ty->getDescription();
362 Table = createSlotCalculator(V);
363 if (Table == 0) { Out << "BAD VALUE TYPE!"; return; }
365 Slot = Table->getValSlot(V);
368 if (Slot >= 0) Out << "%" << Slot;
370 Out << "<badref>"; // Not embeded into a location?
377 // WriteAsOperand - Write the name of the specified value out to the specified
378 // ostream. This can be useful when you just want to print int %reg126, not the
379 // whole instruction that generated it.
381 ostream &WriteAsOperand(ostream &Out, const Value *V, bool PrintType,
382 bool PrintName, SlotCalculator *Table) {
383 map<const Type *, string> TypeNames;
384 const Module *M = getModuleFromVal(V);
386 if (M && M->hasSymbolTable())
387 fillTypeNameTable(M, TypeNames);
390 printTypeInt(Out, V->getType(), TypeNames);
392 WriteAsOperandInternal(Out, V, PrintName, TypeNames, Table);
398 class AssemblyWriter {
400 SlotCalculator &Table;
401 const Module *TheModule;
402 map<const Type *, string> TypeNames;
404 inline AssemblyWriter(ostream &o, SlotCalculator &Tab, const Module *M)
405 : Out(o), Table(Tab), TheModule(M) {
407 // If the module has a symbol table, take all global types and stuff their
408 // names into the TypeNames map.
410 fillTypeNameTable(M, TypeNames);
413 inline void write(const Module *M) { printModule(M); }
414 inline void write(const GlobalVariable *G) { printGlobal(G); }
415 inline void write(const Function *F) { printFunction(F); }
416 inline void write(const BasicBlock *BB) { printBasicBlock(BB); }
417 inline void write(const Instruction *I) { printInstruction(I); }
418 inline void write(const Constant *CPV) { printConstant(CPV); }
419 inline void write(const Type *Ty) { printType(Ty); }
421 void writeOperand(const Value *Op, bool PrintType, bool PrintName = true);
424 void printModule(const Module *M);
425 void printSymbolTable(const SymbolTable &ST);
426 void printConstant(const Constant *CPV);
427 void printGlobal(const GlobalVariable *GV);
428 void printFunction(const Function *F);
429 void printArgument(const Argument *FA);
430 void printBasicBlock(const BasicBlock *BB);
431 void printInstruction(const Instruction *I);
433 // printType - Go to extreme measures to attempt to print out a short,
434 // symbolic version of a type name.
436 ostream &printType(const Type *Ty) {
437 return printTypeInt(Out, Ty, TypeNames);
440 // printTypeAtLeastOneLevel - Print out one level of the possibly complex type
441 // without considering any symbolic types that we may have equal to it.
443 ostream &printTypeAtLeastOneLevel(const Type *Ty);
445 // printInfoComment - Print a little comment after the instruction indicating
446 // which slot it occupies.
447 void printInfoComment(const Value *V);
451 // printTypeAtLeastOneLevel - Print out one level of the possibly complex type
452 // without considering any symbolic types that we may have equal to it.
454 ostream &AssemblyWriter::printTypeAtLeastOneLevel(const Type *Ty) {
455 if (FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
456 printType(FTy->getReturnType()) << " (";
457 for (FunctionType::ParamTypes::const_iterator
458 I = FTy->getParamTypes().begin(),
459 E = FTy->getParamTypes().end(); I != E; ++I) {
460 if (I != FTy->getParamTypes().begin())
464 if (FTy->isVarArg()) {
465 if (!FTy->getParamTypes().empty()) Out << ", ";
469 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
471 for (StructType::ElementTypes::const_iterator
472 I = STy->getElementTypes().begin(),
473 E = STy->getElementTypes().end(); I != E; ++I) {
474 if (I != STy->getElementTypes().begin())
479 } else if (PointerType *PTy = dyn_cast<PointerType>(Ty)) {
480 printType(PTy->getElementType()) << "*";
481 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
482 Out << "[" << ATy->getNumElements() << " x ";
483 printType(ATy->getElementType()) << "]";
485 assert(Ty->isPrimitiveType() && "Unknown derived type!");
492 void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType,
494 if (PrintType) { Out << " "; printType(Operand->getType()); }
495 WriteAsOperandInternal(Out, Operand, PrintName, TypeNames, &Table);
499 void AssemblyWriter::printModule(const Module *M) {
500 // Loop over the symbol table, emitting all named constants...
501 if (M->hasSymbolTable())
502 printSymbolTable(*M->getSymbolTable());
504 for_each(M->gbegin(), M->gend(),
505 bind_obj(this, &AssemblyWriter::printGlobal));
507 Out << "\n; Functions:\n";
509 // Output all of the functions...
510 for_each(M->begin(), M->end(), bind_obj(this,&AssemblyWriter::printFunction));
513 void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
514 if (GV->hasName()) Out << "%" << GV->getName() << " = ";
516 if (GV->hasInternalLinkage()) Out << "internal ";
517 if (!GV->hasInitializer()) Out << "uninitialized ";
519 Out << (GV->isConstant() ? "constant " : "global ");
520 printType(GV->getType()->getElementType());
522 if (GV->hasInitializer())
523 writeOperand(GV->getInitializer(), false, false);
525 printInfoComment(GV);
530 // printSymbolTable - Run through symbol table looking for named constants
531 // if a named constant is found, emit it's declaration...
533 void AssemblyWriter::printSymbolTable(const SymbolTable &ST) {
534 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
535 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
536 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
538 for (; I != End; ++I) {
539 const Value *V = I->second;
540 if (const Constant *CPV = dyn_cast<const Constant>(V)) {
542 } else if (const Type *Ty = dyn_cast<const Type>(V)) {
543 Out << "\t%" << I->first << " = type ";
545 // Make sure we print out at least one level of the type structure, so
546 // that we do not get %FILE = type %FILE
548 printTypeAtLeastOneLevel(Ty) << "\n";
555 // printConstant - Print out a constant pool entry...
557 void AssemblyWriter::printConstant(const Constant *CPV) {
558 // Don't print out unnamed constants, they will be inlined
559 if (!CPV->hasName()) return;
562 Out << "\t%" << CPV->getName() << " =";
564 // Write the value out now...
565 writeOperand(CPV, true, false);
567 printInfoComment(CPV);
571 // printFunction - Print all aspects of a function.
573 void AssemblyWriter::printFunction(const Function *M) {
574 // Print out the return type and name...
575 Out << "\n" << (M->isExternal() ? "declare " : "")
576 << (M->hasInternalLinkage() ? "internal " : "");
577 printType(M->getReturnType()) << " \"" << M->getName() << "\"(";
578 Table.incorporateFunction(M);
580 // Loop over the arguments, printing them...
581 const FunctionType *MT = M->getFunctionType();
583 if (!M->isExternal()) {
584 for_each(M->getArgumentList().begin(), M->getArgumentList().end(),
585 bind_obj(this, &AssemblyWriter::printArgument));
587 // Loop over the arguments, printing them...
588 const FunctionType *MT = M->getFunctionType();
589 for (FunctionType::ParamTypes::const_iterator I = MT->getParamTypes().begin(),
590 E = MT->getParamTypes().end(); I != E; ++I) {
591 if (I != MT->getParamTypes().begin()) Out << ", ";
596 // Finish printing arguments...
597 if (MT->isVarArg()) {
598 if (MT->getParamTypes().size()) Out << ", ";
599 Out << "..."; // Output varargs portion of signature!
603 if (!M->isExternal()) {
604 // Loop over the symbol table, emitting all named constants...
605 if (M->hasSymbolTable())
606 printSymbolTable(*M->getSymbolTable());
610 // Output all of its basic blocks... for the function
611 for_each(M->begin(), M->end(),
612 bind_obj(this, &AssemblyWriter::printBasicBlock));
617 Table.purgeFunction();
620 // printArgument - This member is called for every argument that
621 // is passed into the function. Simply print it out
623 void AssemblyWriter::printArgument(const Argument *Arg) {
624 // Insert commas as we go... the first arg doesn't get a comma
625 if (Arg != Arg->getParent()->getArgumentList().front()) Out << ", ";
628 printType(Arg->getType());
630 // Output name, if available...
632 Out << " %" << Arg->getName();
633 else if (Table.getValSlot(Arg) < 0)
637 // printBasicBlock - This member is called for each basic block in a methd.
639 void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
640 if (BB->hasName()) { // Print out the label if it exists...
641 Out << "\n" << BB->getName() << ":";
643 int Slot = Table.getValSlot(BB);
644 Out << "\n; <label>:";
646 Out << Slot; // Extra newline seperates out label's
650 Out << "\t\t\t\t\t;[#uses=" << BB->use_size() << "]\n"; // Output # uses
652 // Output all of the instructions in the basic block...
653 for_each(BB->begin(), BB->end(),
654 bind_obj(this, &AssemblyWriter::printInstruction));
658 // printInfoComment - Print a little comment after the instruction indicating
659 // which slot it occupies.
661 void AssemblyWriter::printInfoComment(const Value *V) {
662 if (V->getType() != Type::VoidTy) {
664 printType(V->getType()) << ">";
667 int Slot = Table.getValSlot(V); // Print out the def slot taken...
668 if (Slot >= 0) Out << ":" << Slot;
669 else Out << ":<badref>";
671 Out << " [#uses=" << V->use_size() << "]"; // Output # uses
675 // printInstruction - This member is called for each Instruction in a methd.
677 void AssemblyWriter::printInstruction(const Instruction *I) {
680 // Print out name if it exists...
681 if (I && I->hasName())
682 Out << "%" << I->getName() << " = ";
684 // Print out the opcode...
685 Out << I->getOpcodeName();
687 // Print out the type of the operands...
688 const Value *Operand = I->getNumOperands() ? I->getOperand(0) : 0;
690 // Special case conditional branches to swizzle the condition out to the front
691 if (isa<BranchInst>(I) && I->getNumOperands() > 1) {
692 writeOperand(I->getOperand(2), true);
694 writeOperand(Operand, true);
696 writeOperand(I->getOperand(1), true);
698 } else if (isa<SwitchInst>(I)) {
699 // Special case switch statement to get formatting nice and correct...
700 writeOperand(Operand , true); Out << ",";
701 writeOperand(I->getOperand(1), true); Out << " [";
703 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; op += 2) {
705 writeOperand(I->getOperand(op ), true); Out << ",";
706 writeOperand(I->getOperand(op+1), true);
709 } else if (isa<PHINode>(I)) {
711 printType(I->getType());
714 for (unsigned op = 0, Eop = I->getNumOperands(); op < Eop; op += 2) {
717 writeOperand(I->getOperand(op ), false); Out << ",";
718 writeOperand(I->getOperand(op+1), false); Out << " ]";
720 } else if (isa<ReturnInst>(I) && !Operand) {
722 } else if (isa<CallInst>(I)) {
723 const PointerType *PTy = dyn_cast<PointerType>(Operand->getType());
724 const FunctionType*MTy = PTy ? dyn_cast<FunctionType>(PTy->getElementType()):0;
725 const Type *RetTy = MTy ? MTy->getReturnType() : 0;
727 // If possible, print out the short form of the call instruction, but we can
728 // only do this if the first argument is a pointer to a nonvararg function,
729 // and if the value returned is not a pointer to a function.
731 if (RetTy && MTy && !MTy->isVarArg() &&
732 (!isa<PointerType>(RetTy) ||
733 !isa<FunctionType>(cast<PointerType>(RetTy)))) {
734 Out << " "; printType(RetTy);
735 writeOperand(Operand, false);
737 writeOperand(Operand, true);
740 if (I->getNumOperands() > 1) writeOperand(I->getOperand(1), true);
741 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; ++op) {
743 writeOperand(I->getOperand(op), true);
747 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(I)) {
748 // TODO: Should try to print out short form of the Invoke instruction
749 writeOperand(Operand, true);
751 if (I->getNumOperands() > 3) writeOperand(I->getOperand(3), true);
752 for (unsigned op = 4, Eop = I->getNumOperands(); op < Eop; ++op) {
754 writeOperand(I->getOperand(op), true);
757 Out << " )\n\t\t\tto";
758 writeOperand(II->getNormalDest(), true);
760 writeOperand(II->getExceptionalDest(), true);
762 } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(I)) {
764 printType(AI->getType()->getElementType());
765 if (AI->isArrayAllocation()) {
767 writeOperand(AI->getArraySize(), true);
769 } else if (isa<CastInst>(I)) {
770 writeOperand(Operand, true);
772 printType(I->getType());
773 } else if (Operand) { // Print the normal way...
775 // PrintAllTypes - Instructions who have operands of all the same type
776 // omit the type from all but the first operand. If the instruction has
777 // different type operands (for example br), then they are all printed.
778 bool PrintAllTypes = false;
779 const Type *TheType = Operand->getType();
781 for (unsigned i = 1, E = I->getNumOperands(); i != E; ++i) {
782 Operand = I->getOperand(i);
783 if (Operand->getType() != TheType) {
784 PrintAllTypes = true; // We have differing types! Print them all!
789 // Shift Left & Right print both types even for Ubyte LHS
790 if (isa<ShiftInst>(I)) PrintAllTypes = true;
792 if (!PrintAllTypes) {
794 printType(I->getOperand(0)->getType());
797 for (unsigned i = 0, E = I->getNumOperands(); i != E; ++i) {
799 writeOperand(I->getOperand(i), PrintAllTypes);
808 //===----------------------------------------------------------------------===//
809 // External Interface declarations
810 //===----------------------------------------------------------------------===//
813 void Module::print(std::ostream &o) const {
814 SlotCalculator SlotTable(this, true);
815 AssemblyWriter W(o, SlotTable, this);
819 void GlobalVariable::print(std::ostream &o) const {
820 SlotCalculator SlotTable(getParent(), true);
821 AssemblyWriter W(o, SlotTable, getParent());
825 void Function::print(std::ostream &o) const {
826 SlotCalculator SlotTable(getParent(), true);
827 AssemblyWriter W(o, SlotTable, getParent());
832 void BasicBlock::print(std::ostream &o) const {
833 SlotCalculator SlotTable(getParent(), true);
834 AssemblyWriter W(o, SlotTable,
835 getParent() ? getParent()->getParent() : 0);
839 void Instruction::print(std::ostream &o) const {
840 const Function *F = getParent() ? getParent()->getParent() : 0;
841 SlotCalculator SlotTable(F, true);
842 AssemblyWriter W(o, SlotTable, F ? F->getParent() : 0);
847 void Constant::print(std::ostream &o) const {
848 if (this == 0) { o << "<null> constant value\n"; return; }
849 o << " " << getType()->getDescription() << " ";
851 map<const Type *, string> TypeTable;
852 WriteConstantInt(o, this, false, TypeTable, 0);
855 void Type::print(std::ostream &o) const {
859 o << getDescription();
862 void Argument::print(std::ostream &o) const {
863 o << getType() << " " << getName();
866 void Value::dump() const { print(std::cerr); }
868 //===----------------------------------------------------------------------===//
869 // CachedWriter Class Implementation
870 //===----------------------------------------------------------------------===//
872 void CachedWriter::setModule(const Module *M) {
873 delete SC; delete AW;
875 SC = new SlotCalculator(M, true);
876 AW = new AssemblyWriter(Out, *SC, M);
882 CachedWriter::~CachedWriter() {
887 CachedWriter &CachedWriter::operator<<(const Value *V) {
888 assert(AW && SC && "CachedWriter does not have a current module!");
889 switch (V->getValueType()) {
890 case Value::ConstantVal:
891 case Value::ArgumentVal: AW->writeOperand(V, true, true); break;
892 case Value::TypeVal: AW->write(cast<const Type>(V)); break;
893 case Value::InstructionVal: AW->write(cast<Instruction>(V)); break;
894 case Value::BasicBlockVal: AW->write(cast<BasicBlock>(V)); break;
895 case Value::FunctionVal: AW->write(cast<Function>(V)); break;
896 case Value::GlobalVariableVal: AW->write(cast<GlobalVariable>(V)); break;
897 default: Out << "<unknown value type: " << V->getValueType() << ">"; break;