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