1 //===-- llvm/Assembly/Writer.h - Printer for VM assembly files ---*- C++ -*--=//
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.
8 // This file also defines functions that allow it to output files that a program
9 // called VCG can read.
11 // This library uses the Analysis library to figure out offsets for
12 // variables in the method tables...
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_ASSEMBLY_WRITER_H
17 #define LLVM_ASSEMBLY_WRITER_H
20 #include "llvm/Type.h"
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)
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);
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;
44 std::ostream &WriteTypeSymbolic(std::ostream &, const Type *, const Module *M);
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.
51 std::ostream &WriteAsOperand(std::ostream &, const Value *, bool PrintTy = true,
52 bool PrintName = true, SlotCalculator *Table = 0);
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
60 void WriteToVCG(const Module *Module, const std::string &Filename);
61 void WriteToVCG(const Method *Method, const std::string &Filename);
66 // Define operator<< to work on the various classes that we can send to an
69 inline std::ostream &operator<<(std::ostream &o, const Module *C) {
70 WriteToAssembly(C, o); return o;
73 inline std::ostream &operator<<(std::ostream &o, const GlobalVariable *G) {
74 WriteToAssembly(G, o); return o;
77 inline std::ostream &operator<<(std::ostream &o, const Method *M) {
78 WriteToAssembly(M, o); return o;
81 inline std::ostream &operator<<(std::ostream &o, const BasicBlock *B) {
82 WriteToAssembly(B, o); return o;
85 inline std::ostream &operator<<(std::ostream &o, const Instruction *I) {
86 WriteToAssembly(I, o); return o;
89 inline std::ostream &operator<<(std::ostream &o, const Constant *I) {
90 WriteToAssembly(I, o); return o;
94 inline std::ostream &operator<<(std::ostream &o, const Type *T) {
95 if (!T) return o << "<null Type>";
96 return o << T->getDescription();
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() << ">";