Rename ConstPoolVal -> Constant
[oota-llvm.git] / lib / VMCore / AsmWriter.cpp
1 //===-- Writer.cpp - Library for Printing VM assembly files ------*- C++ -*--=//
2 //
3 // This library implements the functionality defined in llvm/Assembly/Writer.h
4 //
5 // This library uses the Analysis library to figure out offsets for
6 // variables in the method tables...
7 //
8 // TODO: print out the type name instead of the full type if a particular type
9 //       is in the symbol table...
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "llvm/Assembly/CachedWriter.h"
14 #include "llvm/Analysis/SlotCalculator.h"
15 #include "llvm/Module.h"
16 #include "llvm/Method.h"
17 #include "llvm/GlobalVariable.h"
18 #include "llvm/BasicBlock.h"
19 #include "llvm/ConstantVals.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 "Support/StringExtras.h"
26 #include "Support/STLExtras.h"
27 #include <algorithm>
28 #include <map>
29
30 static const Module *getModuleFromVal(const Value *V) {
31   if (const MethodArgument *MA =dyn_cast<const MethodArgument>(V))
32     return MA->getParent() ? MA->getParent()->getParent() : 0;
33   else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(V))
34     return BB->getParent() ? BB->getParent()->getParent() : 0;
35   else if (const Instruction *I = dyn_cast<const Instruction>(V)) {
36     const Method *M = I->getParent() ? I->getParent()->getParent() : 0;
37     return M ? M->getParent() : 0;
38   } else if (const GlobalValue *GV =dyn_cast<const GlobalValue>(V))
39     return GV->getParent();
40   else if (const Module *Mod  = dyn_cast<const Module>(V))
41     return Mod;
42   return 0;
43 }
44
45 static SlotCalculator *createSlotCalculator(const Value *V) {
46   assert(!isa<Type>(V) && "Can't create an SC for a type!");
47   if (const MethodArgument *MA =dyn_cast<const MethodArgument>(V)){
48     return new SlotCalculator(MA->getParent(), true);
49   } else if (const Instruction *I = dyn_cast<const Instruction>(V)) {
50     return new SlotCalculator(I->getParent()->getParent(), true);
51   } else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(V)) {
52     return new SlotCalculator(BB->getParent(), true);
53   } else if (const GlobalVariable *GV =dyn_cast<const GlobalVariable>(V)){
54     return new SlotCalculator(GV->getParent(), true);
55   } else if (const Method *Meth = dyn_cast<const Method>(V)) {
56     return new SlotCalculator(Meth, true);
57   } else if (const Module *Mod  = dyn_cast<const Module>(V)) {
58     return new SlotCalculator(Mod, true);
59   }
60   return 0;
61 }
62
63 // WriteAsOperand - Write the name of the specified value out to the specified
64 // ostream.  This can be useful when you just want to print int %reg126, not the
65 // whole instruction that generated it.
66 //
67 static void WriteAsOperandInternal(ostream &Out, const Value *V, bool PrintName,
68                                    SlotCalculator *Table) {
69   if (PrintName && V->hasName()) {
70     Out << " %" << V->getName();
71   } else {
72     if (const Constant *CPV = dyn_cast<const Constant>(V)) {
73       Out << " " << CPV->getStrValue();
74     } else {
75       int Slot;
76       if (Table) {
77         Slot = Table->getValSlot(V);
78       } else {
79         if (const Type *Ty = dyn_cast<const Type>(V)) {
80           Out << " " << Ty->getDescription();
81           return;
82         }
83
84         Table = createSlotCalculator(V);
85         if (Table == 0) { Out << "BAD VALUE TYPE!"; return; }
86
87         Slot = Table->getValSlot(V);
88         delete Table;
89       }
90       if (Slot >= 0)  Out << " %" << Slot;
91       else if (PrintName)
92         Out << "<badref>";     // Not embeded into a location?
93     }
94   }
95 }
96
97
98 // If the module has a symbol table, take all global types and stuff their
99 // names into the TypeNames map.
100 //
101 static void fillTypeNameTable(const Module *M,
102                               map<const Type *, string> &TypeNames) {
103   if (M && M->hasSymbolTable()) {
104     const SymbolTable *ST = M->getSymbolTable();
105     SymbolTable::const_iterator PI = ST->find(Type::TypeTy);
106     if (PI != ST->end()) {
107       SymbolTable::type_const_iterator I = PI->second.begin();
108       for (; I != PI->second.end(); ++I) {
109         // As a heuristic, don't insert pointer to primitive types, because
110         // they are used too often to have a single useful name.
111         //
112         const Type *Ty = cast<const Type>(I->second);
113         if (!isa<PointerType>(Ty) ||
114             !cast<PointerType>(Ty)->getValueType()->isPrimitiveType())
115           TypeNames.insert(make_pair(Ty, "%"+I->first));
116       }
117     }
118   }
119 }
120
121
122
123 static string calcTypeName(const Type *Ty, vector<const Type *> &TypeStack,
124                            map<const Type *, string> &TypeNames) {
125   if (Ty->isPrimitiveType()) return Ty->getDescription();  // Base case
126
127   // Check to see if the type is named.
128   map<const Type *, string>::iterator I = TypeNames.find(Ty);
129   if (I != TypeNames.end()) return I->second;
130
131   // Check to see if the Type is already on the stack...
132   unsigned Slot = 0, CurSize = TypeStack.size();
133   while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
134
135   // This is another base case for the recursion.  In this case, we know 
136   // that we have looped back to a type that we have previously visited.
137   // Generate the appropriate upreference to handle this.
138   // 
139   if (Slot < CurSize)
140     return "\\" + utostr(CurSize-Slot);       // Here's the upreference
141
142   TypeStack.push_back(Ty);    // Recursive case: Add us to the stack..
143   
144   string Result;
145   switch (Ty->getPrimitiveID()) {
146   case Type::MethodTyID: {
147     const MethodType *MTy = cast<const MethodType>(Ty);
148     Result = calcTypeName(MTy->getReturnType(), TypeStack, TypeNames) + " (";
149     for (MethodType::ParamTypes::const_iterator
150            I = MTy->getParamTypes().begin(),
151            E = MTy->getParamTypes().end(); I != E; ++I) {
152       if (I != MTy->getParamTypes().begin())
153         Result += ", ";
154       Result += calcTypeName(*I, TypeStack, TypeNames);
155     }
156     if (MTy->isVarArg()) {
157       if (!MTy->getParamTypes().empty()) Result += ", ";
158       Result += "...";
159     }
160     Result += ")";
161     break;
162   }
163   case Type::StructTyID: {
164     const StructType *STy = cast<const StructType>(Ty);
165     Result = "{ ";
166     for (StructType::ElementTypes::const_iterator
167            I = STy->getElementTypes().begin(),
168            E = STy->getElementTypes().end(); I != E; ++I) {
169       if (I != STy->getElementTypes().begin())
170         Result += ", ";
171       Result += calcTypeName(*I, TypeStack, TypeNames);
172     }
173     Result += " }";
174     break;
175   }
176   case Type::PointerTyID:
177     Result = calcTypeName(cast<const PointerType>(Ty)->getValueType(), 
178                           TypeStack, TypeNames) + " *";
179     break;
180   case Type::ArrayTyID: {
181     const ArrayType *ATy = cast<const ArrayType>(Ty);
182     int NumElements = ATy->getNumElements();
183     Result = "[";
184     if (NumElements != -1) Result += itostr(NumElements) + " x ";
185     Result += calcTypeName(ATy->getElementType(), TypeStack, TypeNames) + "]";
186     break;
187   }
188   default:
189     assert(0 && "Unhandled case in getTypeProps!");
190     Result = "<error>";
191   }
192
193   TypeStack.pop_back();       // Remove self from stack...
194   return Result;
195 }
196
197
198 // printTypeInt - The internal guts of printing out a type that has a
199 // potentially named portion.
200 //
201 static ostream &printTypeInt(ostream &Out, const Type *Ty,
202                              map<const Type *, string> &TypeNames) {
203   // Primitive types always print out their description, regardless of whether
204   // they have been named or not.
205   //
206   if (Ty->isPrimitiveType()) return Out << Ty->getDescription();
207
208   // Check to see if the type is named.
209   map<const Type *, string>::iterator I = TypeNames.find(Ty);
210   if (I != TypeNames.end()) return Out << I->second;
211
212   // Otherwise we have a type that has not been named but is a derived type.
213   // Carefully recurse the type hierarchy to print out any contained symbolic
214   // names.
215   //
216   vector<const Type *> TypeStack;
217   string TypeName = calcTypeName(Ty, TypeStack, TypeNames);
218   TypeNames.insert(make_pair(Ty, TypeName));   // Cache type name for later use
219   return Out << TypeName;
220 }
221
222
223 // WriteTypeSymbolic - This attempts to write the specified type as a symbolic
224 // type, iff there is an entry in the modules symbol table for the specified
225 // type or one of it's component types.  This is slower than a simple x << Type;
226 //
227 ostream &WriteTypeSymbolic(ostream &Out, const Type *Ty, const Module *M) {
228   Out << " "; 
229
230   // If they want us to print out a type, attempt to make it symbolic if there
231   // is a symbol table in the module...
232   if (M && M->hasSymbolTable()) {
233     map<const Type *, string> TypeNames;
234     fillTypeNameTable(M, TypeNames);
235     
236     return printTypeInt(Out, Ty, TypeNames);
237   } else {
238     return Out << Ty->getDescription();
239   }
240 }
241
242
243 // WriteAsOperand - Write the name of the specified value out to the specified
244 // ostream.  This can be useful when you just want to print int %reg126, not the
245 // whole instruction that generated it.
246 //
247 ostream &WriteAsOperand(ostream &Out, const Value *V, bool PrintType, 
248                         bool PrintName, SlotCalculator *Table) {
249   if (PrintType)
250     WriteTypeSymbolic(Out, V->getType(), getModuleFromVal(V));
251
252   WriteAsOperandInternal(Out, V, PrintName, Table);
253   return Out;
254 }
255
256
257
258 class AssemblyWriter {
259   ostream &Out;
260   SlotCalculator &Table;
261   const Module *TheModule;
262   map<const Type *, string> TypeNames;
263 public:
264   inline AssemblyWriter(ostream &o, SlotCalculator &Tab, const Module *M)
265     : Out(o), Table(Tab), TheModule(M) {
266
267     // If the module has a symbol table, take all global types and stuff their
268     // names into the TypeNames map.
269     //
270     fillTypeNameTable(M, TypeNames);
271   }
272
273   inline void write(const Module *M)         { printModule(M);      }
274   inline void write(const GlobalVariable *G) { printGlobal(G);      }
275   inline void write(const Method *M)         { printMethod(M);      }
276   inline void write(const BasicBlock *BB)    { printBasicBlock(BB); }
277   inline void write(const Instruction *I)    { printInstruction(I); }
278   inline void write(const Constant *CPV)     { printConstant(CPV);  }
279   inline void write(const Type *Ty)          { printType(Ty);       }
280
281 private :
282   void printModule(const Module *M);
283   void printSymbolTable(const SymbolTable &ST);
284   void printConstant(const Constant *CPV);
285   void printGlobal(const GlobalVariable *GV);
286   void printMethod(const Method *M);
287   void printMethodArgument(const MethodArgument *MA);
288   void printBasicBlock(const BasicBlock *BB);
289   void printInstruction(const Instruction *I);
290   ostream &printType(const Type *Ty);
291
292   void writeOperand(const Value *Op, bool PrintType, bool PrintName = true);
293
294   // printInfoComment - Print a little comment after the instruction indicating
295   // which slot it occupies.
296   void printInfoComment(const Value *V);
297 };
298
299
300 void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType, 
301                                   bool PrintName) {
302   if (PrintType) { Out << " "; printType(Operand->getType()); }
303   WriteAsOperandInternal(Out, Operand, PrintName, &Table);
304 }
305
306
307 void AssemblyWriter::printModule(const Module *M) {
308   // Loop over the symbol table, emitting all named constants...
309   if (M->hasSymbolTable())
310     printSymbolTable(*M->getSymbolTable());
311   
312   for_each(M->gbegin(), M->gend(), 
313            bind_obj(this, &AssemblyWriter::printGlobal));
314
315   Out << "implementation\n";
316   
317   // Output all of the methods...
318   for_each(M->begin(), M->end(), bind_obj(this,&AssemblyWriter::printMethod));
319 }
320
321 void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
322   if (GV->hasName()) Out << "%" << GV->getName() << " = ";
323
324   if (GV->hasInternalLinkage()) Out << "internal ";
325   if (!GV->hasInitializer()) Out << "uninitialized ";
326
327   Out << (GV->isConstant() ? "constant " : "global ");
328   printType(GV->getType()->getValueType());
329
330   if (GV->hasInitializer())
331     writeOperand(GV->getInitializer(), false, false);
332
333   printInfoComment(GV);
334   Out << endl;
335 }
336
337
338 // printSymbolTable - Run through symbol table looking for named constants
339 // if a named constant is found, emit it's declaration...
340 //
341 void AssemblyWriter::printSymbolTable(const SymbolTable &ST) {
342   for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
343     SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
344     SymbolTable::type_const_iterator End = ST.type_end(TI->first);
345     
346     for (; I != End; ++I) {
347       const Value *V = I->second;
348       if (const Constant *CPV = dyn_cast<const Constant>(V)) {
349         printConstant(CPV);
350       } else if (const Type *Ty = dyn_cast<const Type>(V)) {
351         Out << "\t%" << I->first << " = type " << Ty->getDescription() << endl;
352       }
353     }
354   }
355 }
356
357
358 // printConstant - Print out a constant pool entry...
359 //
360 void AssemblyWriter::printConstant(const Constant *CPV) {
361   // Don't print out unnamed constants, they will be inlined
362   if (!CPV->hasName()) return;
363
364   // Print out name...
365   Out << "\t%" << CPV->getName() << " = ";
366
367   // Print out the constant type...
368   printType(CPV->getType());
369
370   // Write the value out now...
371   writeOperand(CPV, false, false);
372
373   if (!CPV->hasName() && CPV->getType() != Type::VoidTy) {
374     int Slot = Table.getValSlot(CPV); // Print out the def slot taken...
375     Out << "\t\t; <";
376     printType(CPV->getType()) << ">:";
377     if (Slot >= 0) Out << Slot;
378     else Out << "<badref>";
379   } 
380
381   Out << endl;
382 }
383
384 // printMethod - Print all aspects of a method.
385 //
386 void AssemblyWriter::printMethod(const Method *M) {
387   // Print out the return type and name...
388   Out << "\n" << (M->isExternal() ? "declare " : "")
389       << (M->hasInternalLinkage() ? "internal " : "");
390   printType(M->getReturnType()) << " \"" << M->getName() << "\"(";
391   Table.incorporateMethod(M);
392
393   // Loop over the arguments, printing them...
394   const MethodType *MT = cast<const MethodType>(M->getMethodType());
395
396   if (!M->isExternal()) {
397     for_each(M->getArgumentList().begin(), M->getArgumentList().end(),
398              bind_obj(this, &AssemblyWriter::printMethodArgument));
399   } else {
400     // Loop over the arguments, printing them...
401     const MethodType *MT = cast<const MethodType>(M->getMethodType());
402     for (MethodType::ParamTypes::const_iterator I = MT->getParamTypes().begin(),
403            E = MT->getParamTypes().end(); I != E; ++I) {
404       if (I != MT->getParamTypes().begin()) Out << ", ";
405       printType(*I);
406     }
407   }
408
409   // Finish printing arguments...
410   if (MT->isVarArg()) {
411     if (MT->getParamTypes().size()) Out << ", ";
412     Out << "...";  // Output varargs portion of signature!
413   }
414   Out << ")\n";
415
416   if (!M->isExternal()) {
417     // Loop over the symbol table, emitting all named constants...
418     if (M->hasSymbolTable())
419       printSymbolTable(*M->getSymbolTable());
420
421     Out << "begin";
422   
423     // Output all of its basic blocks... for the method
424     for_each(M->begin(), M->end(),
425              bind_obj(this, &AssemblyWriter::printBasicBlock));
426
427     Out << "end\n";
428   }
429
430   Table.purgeMethod();
431 }
432
433 // printMethodArgument - This member is called for every argument that 
434 // is passed into the method.  Simply print it out
435 //
436 void AssemblyWriter::printMethodArgument(const MethodArgument *Arg) {
437   // Insert commas as we go... the first arg doesn't get a comma
438   if (Arg != Arg->getParent()->getArgumentList().front()) Out << ", ";
439
440   // Output type...
441   printType(Arg->getType());
442   
443   // Output name, if available...
444   if (Arg->hasName())
445     Out << " %" << Arg->getName();
446   else if (Table.getValSlot(Arg) < 0)
447     Out << "<badref>";
448 }
449
450 // printBasicBlock - This member is called for each basic block in a methd.
451 //
452 void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
453   if (BB->hasName()) {              // Print out the label if it exists...
454     Out << "\n" << BB->getName() << ":";
455   } else {
456     int Slot = Table.getValSlot(BB);
457     Out << "\n; <label>:";
458     if (Slot >= 0) 
459       Out << Slot;         // Extra newline seperates out label's
460     else 
461       Out << "<badref>"; 
462   }
463   Out << "\t\t\t\t\t;[#uses=" << BB->use_size() << "]\n";  // Output # uses
464
465   // Output all of the instructions in the basic block...
466   for_each(BB->begin(), BB->end(),
467            bind_obj(this, &AssemblyWriter::printInstruction));
468 }
469
470
471 // printInfoComment - Print a little comment after the instruction indicating
472 // which slot it occupies.
473 //
474 void AssemblyWriter::printInfoComment(const Value *V) {
475   if (V->getType() != Type::VoidTy) {
476     Out << "\t\t; <";
477     printType(V->getType()) << ">";
478
479     if (!V->hasName()) {
480       int Slot = Table.getValSlot(V); // Print out the def slot taken...
481       if (Slot >= 0) Out << ":" << Slot;
482       else Out << ":<badref>";
483     }
484     Out << "\t[#uses=" << V->use_size() << "]";  // Output # uses
485   }
486 }
487
488 // printInstruction - This member is called for each Instruction in a methd.
489 //
490 void AssemblyWriter::printInstruction(const Instruction *I) {
491   Out << "\t";
492
493   // Print out name if it exists...
494   if (I && I->hasName())
495     Out << "%" << I->getName() << " = ";
496
497   // Print out the opcode...
498   Out << I->getOpcodeName();
499
500   // Print out the type of the operands...
501   const Value *Operand = I->getNumOperands() ? I->getOperand(0) : 0;
502
503   // Special case conditional branches to swizzle the condition out to the front
504   if (I->getOpcode() == Instruction::Br && I->getNumOperands() > 1) {
505     writeOperand(I->getOperand(2), true);
506     Out << ",";
507     writeOperand(Operand, true);
508     Out << ",";
509     writeOperand(I->getOperand(1), true);
510
511   } else if (I->getOpcode() == Instruction::Switch) {
512     // Special case switch statement to get formatting nice and correct...
513     writeOperand(Operand         , true); Out << ",";
514     writeOperand(I->getOperand(1), true); Out << " [";
515
516     for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; op += 2) {
517       Out << "\n\t\t";
518       writeOperand(I->getOperand(op  ), true); Out << ",";
519       writeOperand(I->getOperand(op+1), true);
520     }
521     Out << "\n\t]";
522   } else if (isa<PHINode>(I)) {
523     Out << " ";
524     printType(I->getType());
525     Out << " ";
526
527     for (unsigned op = 0, Eop = I->getNumOperands(); op < Eop; op += 2) {
528       if (op) Out << ", ";
529       Out << "[";  
530       writeOperand(I->getOperand(op  ), false); Out << ",";
531       writeOperand(I->getOperand(op+1), false); Out << " ]";
532     }
533   } else if (isa<ReturnInst>(I) && !Operand) {
534     Out << " void";
535   } else if (isa<CallInst>(I)) {
536     const PointerType *PTy = dyn_cast<PointerType>(Operand->getType());
537     const MethodType  *MTy = PTy ? dyn_cast<MethodType>(PTy->getValueType()) :0;
538     const Type      *RetTy = MTy ? MTy->getReturnType() : 0;
539
540     // If possible, print out the short form of the call instruction, but we can
541     // only do this if the first argument is a pointer to a nonvararg method,
542     // and if the value returned is not a pointer to a method.
543     //
544     if (RetTy && !MTy->isVarArg() &&
545         (!isa<PointerType>(RetTy)||!isa<MethodType>(cast<PointerType>(RetTy)))){
546       Out << " "; printType(RetTy);
547       writeOperand(Operand, false);
548     } else {
549       writeOperand(Operand, true);
550     }
551     Out << "(";
552     if (I->getNumOperands() > 1) writeOperand(I->getOperand(1), true);
553     for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; ++op) {
554       Out << ",";
555       writeOperand(I->getOperand(op), true);
556     }
557
558     Out << " )";
559   } else if (const InvokeInst *II = dyn_cast<InvokeInst>(I)) {
560     // TODO: Should try to print out short form of the Invoke instruction
561     writeOperand(Operand, true);
562     Out << "(";
563     if (I->getNumOperands() > 3) writeOperand(I->getOperand(3), true);
564     for (unsigned op = 4, Eop = I->getNumOperands(); op < Eop; ++op) {
565       Out << ",";
566       writeOperand(I->getOperand(op), true);
567     }
568
569     Out << " )\n\t\t\tto";
570     writeOperand(II->getNormalDest(), true);
571     Out << " except";
572     writeOperand(II->getExceptionalDest(), true);
573
574   } else if (I->getOpcode() == Instruction::Malloc || 
575              I->getOpcode() == Instruction::Alloca) {
576     Out << " ";
577     printType(cast<const PointerType>(I->getType())->getValueType());
578     if (I->getNumOperands()) {
579       Out << ",";
580       writeOperand(I->getOperand(0), true);
581     }
582   } else if (isa<CastInst>(I)) {
583     writeOperand(Operand, true);
584     Out << " to ";
585     printType(I->getType());
586   } else if (Operand) {   // Print the normal way...
587
588     // PrintAllTypes - Instructions who have operands of all the same type 
589     // omit the type from all but the first operand.  If the instruction has
590     // different type operands (for example br), then they are all printed.
591     bool PrintAllTypes = false;
592     const Type *TheType = Operand->getType();
593
594     for (unsigned i = 1, E = I->getNumOperands(); i != E; ++i) {
595       Operand = I->getOperand(i);
596       if (Operand->getType() != TheType) {
597         PrintAllTypes = true;       // We have differing types!  Print them all!
598         break;
599       }
600     }
601
602     // Shift Left & Right print both types even for Ubyte LHS
603     if (isa<ShiftInst>(I)) PrintAllTypes = true;
604
605     if (!PrintAllTypes) {
606       Out << " ";
607       printType(I->getOperand(0)->getType());
608     }
609
610     for (unsigned i = 0, E = I->getNumOperands(); i != E; ++i) {
611       if (i) Out << ",";
612       writeOperand(I->getOperand(i), PrintAllTypes);
613     }
614   }
615
616   printInfoComment(I);
617   Out << endl;
618 }
619
620
621 // printType - Go to extreme measures to attempt to print out a short, symbolic
622 // version of a type name.
623 //
624 ostream &AssemblyWriter::printType(const Type *Ty) {
625   return printTypeInt(Out, Ty, TypeNames);
626 }
627
628
629 //===----------------------------------------------------------------------===//
630 //                       External Interface declarations
631 //===----------------------------------------------------------------------===//
632
633
634
635 void WriteToAssembly(const Module *M, ostream &o) {
636   if (M == 0) { o << "<null> module\n"; return; }
637   SlotCalculator SlotTable(M, true);
638   AssemblyWriter W(o, SlotTable, M);
639
640   W.write(M);
641 }
642
643 void WriteToAssembly(const GlobalVariable *G, ostream &o) {
644   if (G == 0) { o << "<null> global variable\n"; return; }
645   SlotCalculator SlotTable(G->getParent(), true);
646   AssemblyWriter W(o, SlotTable, G->getParent());
647   W.write(G);
648 }
649
650 void WriteToAssembly(const Method *M, ostream &o) {
651   if (M == 0) { o << "<null> method\n"; return; }
652   SlotCalculator SlotTable(M->getParent(), true);
653   AssemblyWriter W(o, SlotTable, M->getParent());
654
655   W.write(M);
656 }
657
658
659 void WriteToAssembly(const BasicBlock *BB, ostream &o) {
660   if (BB == 0) { o << "<null> basic block\n"; return; }
661
662   SlotCalculator SlotTable(BB->getParent(), true);
663   AssemblyWriter W(o, SlotTable, 
664                    BB->getParent() ? BB->getParent()->getParent() : 0);
665
666   W.write(BB);
667 }
668
669 void WriteToAssembly(const Constant *CPV, ostream &o) {
670   if (CPV == 0) { o << "<null> constant pool value\n"; return; }
671   o << " " << CPV->getType()->getDescription() << " " << CPV->getStrValue();
672 }
673
674 void WriteToAssembly(const Instruction *I, ostream &o) {
675   if (I == 0) { o << "<null> instruction\n"; return; }
676
677   const Method *M = I->getParent() ? I->getParent()->getParent() : 0;
678   SlotCalculator SlotTable(M, true);
679   AssemblyWriter W(o, SlotTable, M ? M->getParent() : 0);
680
681   W.write(I);
682 }
683
684 void CachedWriter::setModule(const Module *M) {
685   delete SC; delete AW;
686   if (M) {
687     SC = new SlotCalculator(M, true);
688     AW = new AssemblyWriter(Out, *SC, M);
689   } else {
690     SC = 0; AW = 0;
691   }
692 }
693
694 CachedWriter::~CachedWriter() {
695   delete AW;
696   delete SC;
697 }
698
699 CachedWriter &CachedWriter::operator<<(const Value *V) {
700   assert(AW && SC && "CachedWriter does not have a current module!");
701   switch (V->getValueType()) {
702   case Value::ConstantVal:
703     Out << " "; AW->write(V->getType());
704     Out << " " << cast<Constant>(V)->getStrValue(); break;
705   case Value::MethodArgumentVal: 
706     AW->write(V->getType()); Out << " " << V->getName(); break;
707   case Value::TypeVal:           AW->write(cast<const Type>(V)); break;
708   case Value::InstructionVal:    AW->write(cast<Instruction>(V)); break;
709   case Value::BasicBlockVal:     AW->write(cast<BasicBlock>(V)); break;
710   case Value::MethodVal:         AW->write(cast<Method>(V)); break;
711   case Value::GlobalVariableVal: AW->write(cast<GlobalVariable>(V)); break;
712   case Value::ModuleVal:         AW->write(cast<Module>(V)); break;
713   default: Out << "<unknown value type: " << V->getValueType() << ">"; break;
714   }
715   return *this;
716 }