* Minor cleanups
[oota-llvm.git] / include / llvm / Assembly / Writer.h
1 //===-- llvm/Assembly/Writer.h - Printer for VM assembly files ---*- C++ -*--=//
2 //
3 // This functionality is implemented by the lib/Assembly/Writer library.
4 // This library is used to print VM assembly language files to an iostream. It
5 // can print VM code at a variety of granularities, ranging from a whole class
6 // down to an individual instruction.  This makes it useful for debugging.
7 //
8 // This file also defines functions that allow it to output files that a program
9 // called VCG can read.
10 //
11 // This library uses the Analysis library to figure out offsets for
12 // variables in the method tables...
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_ASSEMBLY_WRITER_H
17 #define LLVM_ASSEMBLY_WRITER_H
18
19 #include <iostream>
20 #include "llvm/Type.h"
21
22 class Module;
23 class GlobalVariable;
24 class Method;
25 class BasicBlock;
26 class Instruction;
27 class SlotCalculator;
28
29 // The only interface defined by this file... convert the internal 
30 // representation of an object into an ascii bytestream that the parser can 
31 // understand later... (the parser only understands whole classes though)
32 //
33 void WriteToAssembly(const Module  *Module, std::ostream &o);
34 void WriteToAssembly(const GlobalVariable *G, std::ostream &o);
35 void WriteToAssembly(const Method  *Method, std::ostream &o);
36 void WriteToAssembly(const BasicBlock  *BB, std::ostream &o);
37 void WriteToAssembly(const Instruction *In, std::ostream &o);
38 void WriteToAssembly(const Constant     *V, std::ostream &o);
39
40 // WriteTypeSymbolic - This attempts to write the specified type as a symbolic
41 // type, iff there is an entry in the modules symbol table for the specified
42 // type or one of it's component types.  This is slower than a simple x << Type;
43 //
44 std::ostream &WriteTypeSymbolic(std::ostream &, const Type *, const Module *M);
45
46
47 // WriteAsOperand - Write the name of the specified value out to the specified
48 // ostream.  This can be useful when you just want to print int %reg126, not the
49 // whole instruction that generated it.
50 //
51 std::ostream &WriteAsOperand(std::ostream &, const Value *, bool PrintTy = true,
52                              bool PrintName = true, SlotCalculator *Table = 0);
53
54
55 // WriteToVCG - Dump the specified structure to a VCG file.  If method is
56 // dumped, then the file named is created.  If a module is to be written, a
57 // family of files with a common base name is created, with a method name
58 // suffix.
59 //
60 void WriteToVCG(const Module *Module, const std::string &Filename);
61 void WriteToVCG(const Method *Method, const std::string &Filename);
62
63
64
65
66 // Define operator<< to work on the various classes that we can send to an 
67 // ostream...
68 //
69 inline std::ostream &operator<<(std::ostream &o, const Module *C) {
70   WriteToAssembly(C, o); return o;
71 }
72
73 inline std::ostream &operator<<(std::ostream &o, const GlobalVariable *G) {
74   WriteToAssembly(G, o); return o;
75 }
76
77 inline std::ostream &operator<<(std::ostream &o, const Method *M) {
78   WriteToAssembly(M, o); return o;
79 }
80
81 inline std::ostream &operator<<(std::ostream &o, const BasicBlock *B) {
82   WriteToAssembly(B, o); return o;
83 }
84
85 inline std::ostream &operator<<(std::ostream &o, const Instruction *I) {
86   WriteToAssembly(I, o); return o;
87 }
88
89 inline std::ostream &operator<<(std::ostream &o, const Constant *I) {
90   WriteToAssembly(I, o); return o;
91 }
92
93
94 inline std::ostream &operator<<(std::ostream &o, const Type *T) {
95   if (!T) return o << "<null Type>";
96   return o << T->getDescription();
97 }
98
99 inline std::ostream &operator<<(std::ostream &o, const Value *I) {
100   switch (I->getValueType()) {
101   case Value::TypeVal:       return o << cast<const Type>(I);
102   case Value::ConstantVal:   WriteToAssembly(cast<Constant>(I)      , o); break;
103   case Value::MethodArgumentVal: return o << I->getType() << " "<< I->getName();
104   case Value::InstructionVal:WriteToAssembly(cast<Instruction>(I)   , o); break;
105   case Value::BasicBlockVal: WriteToAssembly(cast<BasicBlock>(I)    , o); break;
106   case Value::MethodVal:     WriteToAssembly(cast<Method>(I)        , o); break;
107   case Value::GlobalVariableVal:
108                              WriteToAssembly(cast<GlobalVariable>(I), o); break;
109   case Value::ModuleVal:     WriteToAssembly(cast<Module>(I)        , o); break;
110   default: return o << "<unknown value type: " << I->getValueType() << ">";
111   }
112   return o;
113 }
114
115 #endif