blob: 97dcf45c3ccd901039726fbe88d05a4b5861a754 [file] [log] [blame]
Chris Lattnerf7e79482002-04-07 22:31:46 +00001//===-- AsmWriter.cpp - Printing LLVM as an assembly file -----------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00002//
3// This library implements the functionality defined in llvm/Assembly/Writer.h
4//
Chris Lattner189088e2002-04-12 18:21:53 +00005// 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
7// transformations.
8//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009// TODO: print out the type name instead of the full type if a particular type
Chris Lattner189088e2002-04-12 18:21:53 +000010// is in the symbol table...
Chris Lattner2f7c9632001-06-06 20:29:01 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner7db79582001-11-07 04:21:57 +000014#include "llvm/Assembly/CachedWriter.h"
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +000015#include "llvm/Assembly/Writer.h"
Chris Lattner6915f8f2002-04-07 22:49:37 +000016#include "llvm/SlotCalculator.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000017#include "llvm/Module.h"
Chris Lattner57698e22002-03-26 18:01:55 +000018#include "llvm/Function.h"
Chris Lattnerda975502001-09-10 07:58:01 +000019#include "llvm/GlobalVariable.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000020#include "llvm/BasicBlock.h"
Chris Lattner3462ae32001-12-03 22:26:30 +000021#include "llvm/ConstantVals.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000022#include "llvm/iMemory.h"
Chris Lattner862e3382001-10-13 06:42:36 +000023#include "llvm/iTerminators.h"
Chris Lattnerfb5ae022001-12-03 18:02:31 +000024#include "llvm/iPHINode.h"
25#include "llvm/iOther.h"
Chris Lattnerfee714f2001-09-07 16:36:04 +000026#include "llvm/SymbolTable.h"
Chris Lattner2e9fa6d2002-04-09 19:48:49 +000027#include "llvm/Argument.h"
Chris Lattner5de22042001-11-27 00:03:19 +000028#include "Support/StringExtras.h"
29#include "Support/STLExtras.h"
Chris Lattnerfee714f2001-09-07 16:36:04 +000030#include <algorithm>
Chris Lattner7bfee412001-10-29 16:05:51 +000031#include <map>
Chris Lattner7f74a562002-01-20 22:54:45 +000032using std::string;
33using std::map;
34using std::vector;
35using std::ostream;
Chris Lattner7bfee412001-10-29 16:05:51 +000036
Chris Lattnerd84bb632002-04-16 21:36:08 +000037static void WriteAsOperandInternal(ostream &Out, const Value *V, bool PrintName,
38 map<const Type *, string> &TypeTable,
39 SlotCalculator *Table);
40
Chris Lattnerb86620e2001-10-29 16:37:48 +000041static const Module *getModuleFromVal(const Value *V) {
Chris Lattner2e9fa6d2002-04-09 19:48:49 +000042 if (const Argument *MA = dyn_cast<const Argument>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +000043 return MA->getParent() ? MA->getParent()->getParent() : 0;
44 else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(V))
45 return BB->getParent() ? BB->getParent()->getParent() : 0;
46 else if (const Instruction *I = dyn_cast<const Instruction>(V)) {
Chris Lattner57698e22002-03-26 18:01:55 +000047 const Function *M = I->getParent() ? I->getParent()->getParent() : 0;
Chris Lattnerb86620e2001-10-29 16:37:48 +000048 return M ? M->getParent() : 0;
Chris Lattner57698e22002-03-26 18:01:55 +000049 } else if (const GlobalValue *GV = dyn_cast<const GlobalValue>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +000050 return GV->getParent();
51 else if (const Module *Mod = dyn_cast<const Module>(V))
52 return Mod;
53 return 0;
54}
55
Chris Lattner7bfee412001-10-29 16:05:51 +000056static SlotCalculator *createSlotCalculator(const Value *V) {
57 assert(!isa<Type>(V) && "Can't create an SC for a type!");
Chris Lattner2e9fa6d2002-04-09 19:48:49 +000058 if (const Argument *FA = dyn_cast<const Argument>(V)) {
Chris Lattner57698e22002-03-26 18:01:55 +000059 return new SlotCalculator(FA->getParent(), true);
Chris Lattner7bfee412001-10-29 16:05:51 +000060 } else if (const Instruction *I = dyn_cast<const Instruction>(V)) {
61 return new SlotCalculator(I->getParent()->getParent(), true);
62 } else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(V)) {
63 return new SlotCalculator(BB->getParent(), true);
Chris Lattner57698e22002-03-26 18:01:55 +000064 } else if (const GlobalVariable *GV = dyn_cast<const GlobalVariable>(V)){
Chris Lattner7bfee412001-10-29 16:05:51 +000065 return new SlotCalculator(GV->getParent(), true);
Chris Lattner57698e22002-03-26 18:01:55 +000066 } else if (const Function *Func = dyn_cast<const Function>(V)) {
67 return new SlotCalculator(Func, true);
Chris Lattner7bfee412001-10-29 16:05:51 +000068 } else if (const Module *Mod = dyn_cast<const Module>(V)) {
69 return new SlotCalculator(Mod, true);
70 }
71 return 0;
72}
Chris Lattner2f7c9632001-06-06 20:29:01 +000073
Chris Lattnerb86620e2001-10-29 16:37:48 +000074
75// If the module has a symbol table, take all global types and stuff their
76// names into the TypeNames map.
77//
78static void fillTypeNameTable(const Module *M,
79 map<const Type *, string> &TypeNames) {
80 if (M && M->hasSymbolTable()) {
81 const SymbolTable *ST = M->getSymbolTable();
82 SymbolTable::const_iterator PI = ST->find(Type::TypeTy);
83 if (PI != ST->end()) {
84 SymbolTable::type_const_iterator I = PI->second.begin();
85 for (; I != PI->second.end(); ++I) {
86 // As a heuristic, don't insert pointer to primitive types, because
87 // they are used too often to have a single useful name.
88 //
89 const Type *Ty = cast<const Type>(I->second);
90 if (!isa<PointerType>(Ty) ||
Chris Lattner2413b162001-12-04 00:03:30 +000091 !cast<PointerType>(Ty)->getElementType()->isPrimitiveType())
Chris Lattner7f74a562002-01-20 22:54:45 +000092 TypeNames.insert(std::make_pair(Ty, "%"+I->first));
Chris Lattnerb86620e2001-10-29 16:37:48 +000093 }
94 }
95 }
96}
97
98
99
100static string calcTypeName(const Type *Ty, vector<const Type *> &TypeStack,
101 map<const Type *, string> &TypeNames) {
102 if (Ty->isPrimitiveType()) return Ty->getDescription(); // Base case
103
104 // Check to see if the type is named.
105 map<const Type *, string>::iterator I = TypeNames.find(Ty);
106 if (I != TypeNames.end()) return I->second;
107
108 // Check to see if the Type is already on the stack...
109 unsigned Slot = 0, CurSize = TypeStack.size();
110 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
111
112 // This is another base case for the recursion. In this case, we know
113 // that we have looped back to a type that we have previously visited.
114 // Generate the appropriate upreference to handle this.
115 //
116 if (Slot < CurSize)
117 return "\\" + utostr(CurSize-Slot); // Here's the upreference
118
119 TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
120
121 string Result;
122 switch (Ty->getPrimitiveID()) {
Chris Lattner91db5822002-03-29 03:44:36 +0000123 case Type::FunctionTyID: {
Chris Lattnerd816b532002-04-13 20:53:41 +0000124 const FunctionType *FTy = cast<const FunctionType>(Ty);
125 Result = calcTypeName(FTy->getReturnType(), TypeStack, TypeNames) + " (";
Chris Lattner91db5822002-03-29 03:44:36 +0000126 for (FunctionType::ParamTypes::const_iterator
Chris Lattnerd816b532002-04-13 20:53:41 +0000127 I = FTy->getParamTypes().begin(),
128 E = FTy->getParamTypes().end(); I != E; ++I) {
129 if (I != FTy->getParamTypes().begin())
Chris Lattnerb86620e2001-10-29 16:37:48 +0000130 Result += ", ";
131 Result += calcTypeName(*I, TypeStack, TypeNames);
132 }
Chris Lattnerd816b532002-04-13 20:53:41 +0000133 if (FTy->isVarArg()) {
134 if (!FTy->getParamTypes().empty()) Result += ", ";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000135 Result += "...";
136 }
137 Result += ")";
138 break;
139 }
140 case Type::StructTyID: {
141 const StructType *STy = cast<const StructType>(Ty);
142 Result = "{ ";
143 for (StructType::ElementTypes::const_iterator
144 I = STy->getElementTypes().begin(),
145 E = STy->getElementTypes().end(); I != E; ++I) {
146 if (I != STy->getElementTypes().begin())
147 Result += ", ";
148 Result += calcTypeName(*I, TypeStack, TypeNames);
149 }
150 Result += " }";
151 break;
152 }
153 case Type::PointerTyID:
Chris Lattner2413b162001-12-04 00:03:30 +0000154 Result = calcTypeName(cast<const PointerType>(Ty)->getElementType(),
Chris Lattner189088e2002-04-12 18:21:53 +0000155 TypeStack, TypeNames) + "*";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000156 break;
157 case Type::ArrayTyID: {
158 const ArrayType *ATy = cast<const ArrayType>(Ty);
Chris Lattner8a939c92002-04-13 21:11:04 +0000159 Result = "[" + utostr(ATy->getNumElements()) + " x ";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000160 Result += calcTypeName(ATy->getElementType(), TypeStack, TypeNames) + "]";
161 break;
162 }
163 default:
164 assert(0 && "Unhandled case in getTypeProps!");
165 Result = "<error>";
166 }
167
168 TypeStack.pop_back(); // Remove self from stack...
169 return Result;
170}
171
172
173// printTypeInt - The internal guts of printing out a type that has a
174// potentially named portion.
175//
176static ostream &printTypeInt(ostream &Out, const Type *Ty,
177 map<const Type *, string> &TypeNames) {
178 // Primitive types always print out their description, regardless of whether
179 // they have been named or not.
180 //
181 if (Ty->isPrimitiveType()) return Out << Ty->getDescription();
182
183 // Check to see if the type is named.
184 map<const Type *, string>::iterator I = TypeNames.find(Ty);
185 if (I != TypeNames.end()) return Out << I->second;
186
187 // Otherwise we have a type that has not been named but is a derived type.
188 // Carefully recurse the type hierarchy to print out any contained symbolic
189 // names.
190 //
191 vector<const Type *> TypeStack;
192 string TypeName = calcTypeName(Ty, TypeStack, TypeNames);
Chris Lattner7f74a562002-01-20 22:54:45 +0000193 TypeNames.insert(std::make_pair(Ty, TypeName));//Cache type name for later use
Chris Lattnerb86620e2001-10-29 16:37:48 +0000194 return Out << TypeName;
195}
196
Chris Lattner34b95182001-10-31 04:33:19 +0000197
Chris Lattnerb86620e2001-10-29 16:37:48 +0000198// WriteTypeSymbolic - This attempts to write the specified type as a symbolic
199// type, iff there is an entry in the modules symbol table for the specified
200// type or one of it's component types. This is slower than a simple x << Type;
201//
202ostream &WriteTypeSymbolic(ostream &Out, const Type *Ty, const Module *M) {
203 Out << " ";
204
205 // If they want us to print out a type, attempt to make it symbolic if there
206 // is a symbol table in the module...
207 if (M && M->hasSymbolTable()) {
208 map<const Type *, string> TypeNames;
209 fillTypeNameTable(M, TypeNames);
210
Chris Lattner72f866e2001-10-29 16:40:32 +0000211 return printTypeInt(Out, Ty, TypeNames);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000212 } else {
Chris Lattner72f866e2001-10-29 16:40:32 +0000213 return Out << Ty->getDescription();
Chris Lattnerb86620e2001-10-29 16:37:48 +0000214 }
215}
216
Chris Lattnerd84bb632002-04-16 21:36:08 +0000217static void WriteConstantInt(ostream &Out, const Constant *CV, bool PrintName,
218 map<const Type *, string> &TypeTable,
219 SlotCalculator *Table) {
220 if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
221 const Type *SubType = CA->getType()->getElementType();
222 if (SubType == Type::SByteTy) {
223 Out << CV->getStrValue(); // Output string format if possible...
224 } else {
225 Out << "[";
226 if (CA->getNumOperands()) {
227 Out << " ";
228 printTypeInt(Out, SubType, TypeTable);
229 WriteAsOperandInternal(Out, CA->getOperand(0),
230 PrintName, TypeTable, Table);
231 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
232 Out << ", ";
233 printTypeInt(Out, SubType, TypeTable);
234 WriteAsOperandInternal(Out, CA->getOperand(i), PrintName,
235 TypeTable, Table);
236 }
237 }
238 Out << " ]";
239 }
240 } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
241 Out << "{";
242 if (CS->getNumOperands()) {
243 Out << " ";
244 printTypeInt(Out, CS->getOperand(0)->getType(), TypeTable);
245
246 WriteAsOperandInternal(Out, CS->getOperand(0),
247 PrintName, TypeTable, Table);
248
249 for (unsigned i = 1; i < CS->getNumOperands(); i++) {
250 Out << ", ";
251 printTypeInt(Out, CS->getOperand(i)->getType(), TypeTable);
252
253 WriteAsOperandInternal(Out, CS->getOperand(i),
254 PrintName, TypeTable, Table);
255 }
256 }
257
258 Out << " }";
259 } else if (isa<ConstantPointerNull>(CV)) {
260 Out << "null";
261
262 // FIXME: Handle ConstantPointerRef + lots of others...
263 } else {
264 Out << CV->getStrValue();
265 }
266}
267
268
269// WriteAsOperand - Write the name of the specified value out to the specified
270// ostream. This can be useful when you just want to print int %reg126, not the
271// whole instruction that generated it.
272//
273static void WriteAsOperandInternal(ostream &Out, const Value *V, bool PrintName,
274 map<const Type *, string> &TypeTable,
275 SlotCalculator *Table) {
276 Out << " ";
277 if (PrintName && V->hasName()) {
278 Out << "%" << V->getName();
279 } else {
280 if (const Constant *CV = dyn_cast<const Constant>(V)) {
281 WriteConstantInt(Out, CV, PrintName, TypeTable, Table);
282 } else {
283 int Slot;
284 if (Table) {
285 Slot = Table->getValSlot(V);
286 } else {
287 if (const Type *Ty = dyn_cast<const Type>(V)) {
288 Out << Ty->getDescription();
289 return;
290 }
291
292 Table = createSlotCalculator(V);
293 if (Table == 0) { Out << "BAD VALUE TYPE!"; return; }
294
295 Slot = Table->getValSlot(V);
296 delete Table;
297 }
298 if (Slot >= 0) Out << "%" << Slot;
299 else if (PrintName)
300 Out << "<badref>"; // Not embeded into a location?
301 }
302 }
303}
304
305
Chris Lattnerb86620e2001-10-29 16:37:48 +0000306
307// WriteAsOperand - Write the name of the specified value out to the specified
308// ostream. This can be useful when you just want to print int %reg126, not the
309// whole instruction that generated it.
310//
311ostream &WriteAsOperand(ostream &Out, const Value *V, bool PrintType,
312 bool PrintName, SlotCalculator *Table) {
Chris Lattnerd84bb632002-04-16 21:36:08 +0000313 map<const Type *, string> TypeNames;
314 const Module *M = getModuleFromVal(V);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000315
Chris Lattnerd84bb632002-04-16 21:36:08 +0000316 if (M && M->hasSymbolTable())
317 fillTypeNameTable(M, TypeNames);
318
319 if (PrintType)
320 printTypeInt(Out, V->getType(), TypeNames);
321
322 WriteAsOperandInternal(Out, V, PrintName, TypeNames, Table);
Chris Lattner5e5abe32001-07-20 19:15:21 +0000323 return Out;
324}
325
326
Chris Lattner2e9fee42001-07-12 23:35:26 +0000327
Chris Lattnerfee714f2001-09-07 16:36:04 +0000328class AssemblyWriter {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000329 ostream &Out;
330 SlotCalculator &Table;
Chris Lattner7bfee412001-10-29 16:05:51 +0000331 const Module *TheModule;
332 map<const Type *, string> TypeNames;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000333public:
Chris Lattner7bfee412001-10-29 16:05:51 +0000334 inline AssemblyWriter(ostream &o, SlotCalculator &Tab, const Module *M)
335 : Out(o), Table(Tab), TheModule(M) {
336
337 // If the module has a symbol table, take all global types and stuff their
338 // names into the TypeNames map.
339 //
Chris Lattnerb86620e2001-10-29 16:37:48 +0000340 fillTypeNameTable(M, TypeNames);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000341 }
342
Chris Lattner7bfee412001-10-29 16:05:51 +0000343 inline void write(const Module *M) { printModule(M); }
344 inline void write(const GlobalVariable *G) { printGlobal(G); }
Chris Lattner57698e22002-03-26 18:01:55 +0000345 inline void write(const Function *F) { printFunction(F); }
Chris Lattner7bfee412001-10-29 16:05:51 +0000346 inline void write(const BasicBlock *BB) { printBasicBlock(BB); }
347 inline void write(const Instruction *I) { printInstruction(I); }
Chris Lattner3462ae32001-12-03 22:26:30 +0000348 inline void write(const Constant *CPV) { printConstant(CPV); }
Chris Lattner7db79582001-11-07 04:21:57 +0000349 inline void write(const Type *Ty) { printType(Ty); }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000350
Chris Lattner2f7c9632001-06-06 20:29:01 +0000351private :
Chris Lattner7bfee412001-10-29 16:05:51 +0000352 void printModule(const Module *M);
353 void printSymbolTable(const SymbolTable &ST);
Chris Lattner3462ae32001-12-03 22:26:30 +0000354 void printConstant(const Constant *CPV);
Chris Lattner7bfee412001-10-29 16:05:51 +0000355 void printGlobal(const GlobalVariable *GV);
Chris Lattner57698e22002-03-26 18:01:55 +0000356 void printFunction(const Function *F);
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000357 void printArgument(const Argument *FA);
Chris Lattner7bfee412001-10-29 16:05:51 +0000358 void printBasicBlock(const BasicBlock *BB);
359 void printInstruction(const Instruction *I);
Chris Lattnerd816b532002-04-13 20:53:41 +0000360
361 // printType - Go to extreme measures to attempt to print out a short,
362 // symbolic version of a type name.
363 //
364 ostream &printType(const Type *Ty) {
365 return printTypeInt(Out, Ty, TypeNames);
366 }
367
368 // printTypeAtLeastOneLevel - Print out one level of the possibly complex type
369 // without considering any symbolic types that we may have equal to it.
370 //
371 ostream &printTypeAtLeastOneLevel(const Type *Ty);
Chris Lattner7bfee412001-10-29 16:05:51 +0000372
Chris Lattner2f7c9632001-06-06 20:29:01 +0000373 void writeOperand(const Value *Op, bool PrintType, bool PrintName = true);
Chris Lattner862e3382001-10-13 06:42:36 +0000374
375 // printInfoComment - Print a little comment after the instruction indicating
376 // which slot it occupies.
377 void printInfoComment(const Value *V);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000378};
379
380
Chris Lattnerd816b532002-04-13 20:53:41 +0000381// printTypeAtLeastOneLevel - Print out one level of the possibly complex type
382// without considering any symbolic types that we may have equal to it.
383//
384ostream &AssemblyWriter::printTypeAtLeastOneLevel(const Type *Ty) {
385 if (FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
386 printType(FTy->getReturnType()) << " (";
387 for (FunctionType::ParamTypes::const_iterator
388 I = FTy->getParamTypes().begin(),
389 E = FTy->getParamTypes().end(); I != E; ++I) {
390 if (I != FTy->getParamTypes().begin())
391 Out << ", ";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000392 printType(*I);
Chris Lattnerd816b532002-04-13 20:53:41 +0000393 }
394 if (FTy->isVarArg()) {
395 if (!FTy->getParamTypes().empty()) Out << ", ";
396 Out << "...";
397 }
398 Out << ")";
399 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
400 Out << "{ ";
401 for (StructType::ElementTypes::const_iterator
402 I = STy->getElementTypes().begin(),
403 E = STy->getElementTypes().end(); I != E; ++I) {
404 if (I != STy->getElementTypes().begin())
405 Out << ", ";
406 printType(*I);
407 }
408 Out << " }";
409 } else if (PointerType *PTy = dyn_cast<PointerType>(Ty)) {
410 printType(PTy->getElementType()) << "*";
411 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
412 Out << "[" << ATy->getNumElements() << " x ";
413 printType(ATy->getElementType()) << "]";
414 } else {
415 assert(Ty->isPrimitiveType() && "Unknown derived type!");
416 printType(Ty);
417 }
418 return Out;
419}
420
421
Chris Lattnerfee714f2001-09-07 16:36:04 +0000422void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType,
423 bool PrintName) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000424 if (PrintType) { Out << " "; printType(Operand->getType()); }
Chris Lattnerd84bb632002-04-16 21:36:08 +0000425 WriteAsOperandInternal(Out, Operand, PrintName, TypeNames, &Table);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000426}
427
Chris Lattner2f7c9632001-06-06 20:29:01 +0000428
Chris Lattner7bfee412001-10-29 16:05:51 +0000429void AssemblyWriter::printModule(const Module *M) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000430 // Loop over the symbol table, emitting all named constants...
431 if (M->hasSymbolTable())
Chris Lattner7bfee412001-10-29 16:05:51 +0000432 printSymbolTable(*M->getSymbolTable());
Chris Lattnerda975502001-09-10 07:58:01 +0000433
434 for_each(M->gbegin(), M->gend(),
Chris Lattner7bfee412001-10-29 16:05:51 +0000435 bind_obj(this, &AssemblyWriter::printGlobal));
Chris Lattnerfee714f2001-09-07 16:36:04 +0000436
Vikram S. Adve13ba19a2001-09-18 12:48:16 +0000437 Out << "implementation\n";
438
Chris Lattner6915f8f2002-04-07 22:49:37 +0000439 // Output all of the functions...
Chris Lattner57698e22002-03-26 18:01:55 +0000440 for_each(M->begin(), M->end(), bind_obj(this,&AssemblyWriter::printFunction));
Chris Lattnerfee714f2001-09-07 16:36:04 +0000441}
442
Chris Lattner7bfee412001-10-29 16:05:51 +0000443void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Chris Lattnerda975502001-09-10 07:58:01 +0000444 if (GV->hasName()) Out << "%" << GV->getName() << " = ";
Chris Lattner37798642001-09-18 04:01:05 +0000445
Chris Lattner841d8b92001-11-26 18:54:16 +0000446 if (GV->hasInternalLinkage()) Out << "internal ";
Chris Lattner37798642001-09-18 04:01:05 +0000447 if (!GV->hasInitializer()) Out << "uninitialized ";
448
Chris Lattner7bfee412001-10-29 16:05:51 +0000449 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattner2413b162001-12-04 00:03:30 +0000450 printType(GV->getType()->getElementType());
Chris Lattner37798642001-09-18 04:01:05 +0000451
452 if (GV->hasInitializer())
453 writeOperand(GV->getInitializer(), false, false);
454
Chris Lattner862e3382001-10-13 06:42:36 +0000455 printInfoComment(GV);
Chris Lattner7f74a562002-01-20 22:54:45 +0000456 Out << "\n";
Chris Lattnerda975502001-09-10 07:58:01 +0000457}
458
Chris Lattnerfee714f2001-09-07 16:36:04 +0000459
Chris Lattner7bfee412001-10-29 16:05:51 +0000460// printSymbolTable - Run through symbol table looking for named constants
Chris Lattnerfee714f2001-09-07 16:36:04 +0000461// if a named constant is found, emit it's declaration...
462//
Chris Lattner7bfee412001-10-29 16:05:51 +0000463void AssemblyWriter::printSymbolTable(const SymbolTable &ST) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000464 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
465 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
466 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
467
468 for (; I != End; ++I) {
469 const Value *V = I->second;
Chris Lattner3462ae32001-12-03 22:26:30 +0000470 if (const Constant *CPV = dyn_cast<const Constant>(V)) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000471 printConstant(CPV);
Chris Lattner38569342001-10-01 20:11:19 +0000472 } else if (const Type *Ty = dyn_cast<const Type>(V)) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000473 Out << "\t%" << I->first << " = type ";
474
475 // Make sure we print out at least one level of the type structure, so
476 // that we do not get %FILE = type %FILE
477 //
478 printTypeAtLeastOneLevel(Ty) << "\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000479 }
480 }
Chris Lattnera7620d92001-07-15 06:35:59 +0000481 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000482}
483
484
Chris Lattner7bfee412001-10-29 16:05:51 +0000485// printConstant - Print out a constant pool entry...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000486//
Chris Lattner3462ae32001-12-03 22:26:30 +0000487void AssemblyWriter::printConstant(const Constant *CPV) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000488 // Don't print out unnamed constants, they will be inlined
489 if (!CPV->hasName()) return;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000490
Chris Lattneree998be2001-07-26 16:29:38 +0000491 // Print out name...
Chris Lattnerd84bb632002-04-16 21:36:08 +0000492 Out << "\t%" << CPV->getName() << " =";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000493
494 // Write the value out now...
Chris Lattnerd84bb632002-04-16 21:36:08 +0000495 writeOperand(CPV, true, false);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000496
497 if (!CPV->hasName() && CPV->getType() != Type::VoidTy) {
498 int Slot = Table.getValSlot(CPV); // Print out the def slot taken...
Chris Lattner7bfee412001-10-29 16:05:51 +0000499 Out << "\t\t; <";
500 printType(CPV->getType()) << ">:";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000501 if (Slot >= 0) Out << Slot;
502 else Out << "<badref>";
503 }
504
Chris Lattner7f74a562002-01-20 22:54:45 +0000505 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000506}
507
Chris Lattner6915f8f2002-04-07 22:49:37 +0000508// printFunction - Print all aspects of a function.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000509//
Chris Lattner57698e22002-03-26 18:01:55 +0000510void AssemblyWriter::printFunction(const Function *M) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000511 // Print out the return type and name...
Chris Lattner841d8b92001-11-26 18:54:16 +0000512 Out << "\n" << (M->isExternal() ? "declare " : "")
513 << (M->hasInternalLinkage() ? "internal " : "");
Chris Lattner7bfee412001-10-29 16:05:51 +0000514 printType(M->getReturnType()) << " \"" << M->getName() << "\"(";
Chris Lattner6915f8f2002-04-07 22:49:37 +0000515 Table.incorporateFunction(M);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000516
Chris Lattner7bfee412001-10-29 16:05:51 +0000517 // Loop over the arguments, printing them...
Chris Lattner91db5822002-03-29 03:44:36 +0000518 const FunctionType *MT = M->getFunctionType();
Chris Lattnerfee714f2001-09-07 16:36:04 +0000519
Chris Lattner862e3382001-10-13 06:42:36 +0000520 if (!M->isExternal()) {
521 for_each(M->getArgumentList().begin(), M->getArgumentList().end(),
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000522 bind_obj(this, &AssemblyWriter::printArgument));
Chris Lattner862e3382001-10-13 06:42:36 +0000523 } else {
Chris Lattner7bfee412001-10-29 16:05:51 +0000524 // Loop over the arguments, printing them...
Chris Lattner91db5822002-03-29 03:44:36 +0000525 const FunctionType *MT = M->getFunctionType();
526 for (FunctionType::ParamTypes::const_iterator I = MT->getParamTypes().begin(),
Chris Lattner862e3382001-10-13 06:42:36 +0000527 E = MT->getParamTypes().end(); I != E; ++I) {
528 if (I != MT->getParamTypes().begin()) Out << ", ";
Chris Lattner7bfee412001-10-29 16:05:51 +0000529 printType(*I);
Chris Lattner862e3382001-10-13 06:42:36 +0000530 }
531 }
Chris Lattnerfee714f2001-09-07 16:36:04 +0000532
533 // Finish printing arguments...
Chris Lattnerfee714f2001-09-07 16:36:04 +0000534 if (MT->isVarArg()) {
535 if (MT->getParamTypes().size()) Out << ", ";
536 Out << "..."; // Output varargs portion of signature!
537 }
538 Out << ")\n";
539
540 if (!M->isExternal()) {
541 // Loop over the symbol table, emitting all named constants...
542 if (M->hasSymbolTable())
Chris Lattner7bfee412001-10-29 16:05:51 +0000543 printSymbolTable(*M->getSymbolTable());
Chris Lattnerfee714f2001-09-07 16:36:04 +0000544
545 Out << "begin";
546
Chris Lattner6915f8f2002-04-07 22:49:37 +0000547 // Output all of its basic blocks... for the function
Chris Lattnerfee714f2001-09-07 16:36:04 +0000548 for_each(M->begin(), M->end(),
Chris Lattner7bfee412001-10-29 16:05:51 +0000549 bind_obj(this, &AssemblyWriter::printBasicBlock));
Chris Lattnerfee714f2001-09-07 16:36:04 +0000550
Chris Lattnera7620d92001-07-15 06:35:59 +0000551 Out << "end\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000552 }
553
Chris Lattner6915f8f2002-04-07 22:49:37 +0000554 Table.purgeFunction();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000555}
556
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000557// printArgument - This member is called for every argument that
Chris Lattner6915f8f2002-04-07 22:49:37 +0000558// is passed into the function. Simply print it out
Chris Lattner2f7c9632001-06-06 20:29:01 +0000559//
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000560void AssemblyWriter::printArgument(const Argument *Arg) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000561 // Insert commas as we go... the first arg doesn't get a comma
562 if (Arg != Arg->getParent()->getArgumentList().front()) Out << ", ";
563
564 // Output type...
Chris Lattner7bfee412001-10-29 16:05:51 +0000565 printType(Arg->getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000566
567 // Output name, if available...
568 if (Arg->hasName())
569 Out << " %" << Arg->getName();
570 else if (Table.getValSlot(Arg) < 0)
571 Out << "<badref>";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000572}
573
Chris Lattner7bfee412001-10-29 16:05:51 +0000574// printBasicBlock - This member is called for each basic block in a methd.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000575//
Chris Lattner7bfee412001-10-29 16:05:51 +0000576void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000577 if (BB->hasName()) { // Print out the label if it exists...
Chris Lattnera2f01872001-06-07 16:58:55 +0000578 Out << "\n" << BB->getName() << ":";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000579 } else {
580 int Slot = Table.getValSlot(BB);
Chris Lattnera2f01872001-06-07 16:58:55 +0000581 Out << "\n; <label>:";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000582 if (Slot >= 0)
Chris Lattnera2f01872001-06-07 16:58:55 +0000583 Out << Slot; // Extra newline seperates out label's
Chris Lattner2f7c9632001-06-06 20:29:01 +0000584 else
Chris Lattnera2f01872001-06-07 16:58:55 +0000585 Out << "<badref>";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000586 }
Chris Lattnera2f01872001-06-07 16:58:55 +0000587 Out << "\t\t\t\t\t;[#uses=" << BB->use_size() << "]\n"; // Output # uses
Chris Lattner2f7c9632001-06-06 20:29:01 +0000588
Chris Lattnerfee714f2001-09-07 16:36:04 +0000589 // Output all of the instructions in the basic block...
590 for_each(BB->begin(), BB->end(),
Chris Lattner7bfee412001-10-29 16:05:51 +0000591 bind_obj(this, &AssemblyWriter::printInstruction));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000592}
593
Chris Lattner862e3382001-10-13 06:42:36 +0000594
595// printInfoComment - Print a little comment after the instruction indicating
596// which slot it occupies.
597//
598void AssemblyWriter::printInfoComment(const Value *V) {
599 if (V->getType() != Type::VoidTy) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000600 Out << "\t\t; <";
601 printType(V->getType()) << ">";
Chris Lattner862e3382001-10-13 06:42:36 +0000602
603 if (!V->hasName()) {
604 int Slot = Table.getValSlot(V); // Print out the def slot taken...
605 if (Slot >= 0) Out << ":" << Slot;
606 else Out << ":<badref>";
607 }
Chris Lattner3cb3a1f2001-12-14 16:29:12 +0000608 Out << " [#uses=" << V->use_size() << "]"; // Output # uses
Chris Lattner862e3382001-10-13 06:42:36 +0000609 }
610}
611
Chris Lattner7bfee412001-10-29 16:05:51 +0000612// printInstruction - This member is called for each Instruction in a methd.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000613//
Chris Lattner7bfee412001-10-29 16:05:51 +0000614void AssemblyWriter::printInstruction(const Instruction *I) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000615 Out << "\t";
616
617 // Print out name if it exists...
618 if (I && I->hasName())
619 Out << "%" << I->getName() << " = ";
620
621 // Print out the opcode...
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000622 Out << I->getOpcodeName();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000623
624 // Print out the type of the operands...
Chris Lattnera073acb2001-07-07 08:36:50 +0000625 const Value *Operand = I->getNumOperands() ? I->getOperand(0) : 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000626
627 // Special case conditional branches to swizzle the condition out to the front
Chris Lattner8d48df22002-04-13 18:34:38 +0000628 if (isa<BranchInst>(I) && I->getNumOperands() > 1) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000629 writeOperand(I->getOperand(2), true);
630 Out << ",";
631 writeOperand(Operand, true);
632 Out << ",";
633 writeOperand(I->getOperand(1), true);
634
Chris Lattner8d48df22002-04-13 18:34:38 +0000635 } else if (isa<SwitchInst>(I)) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000636 // Special case switch statement to get formatting nice and correct...
637 writeOperand(Operand , true); Out << ",";
638 writeOperand(I->getOperand(1), true); Out << " [";
639
Chris Lattnera073acb2001-07-07 08:36:50 +0000640 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; op += 2) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000641 Out << "\n\t\t";
Chris Lattnera073acb2001-07-07 08:36:50 +0000642 writeOperand(I->getOperand(op ), true); Out << ",";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000643 writeOperand(I->getOperand(op+1), true);
644 }
645 Out << "\n\t]";
Chris Lattnerda558102001-10-02 03:41:24 +0000646 } else if (isa<PHINode>(I)) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000647 Out << " ";
Chris Lattner29644b32001-11-06 01:48:45 +0000648 printType(I->getType());
Chris Lattner9d3292d2001-11-06 08:33:46 +0000649 Out << " ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000650
Chris Lattner29644b32001-11-06 01:48:45 +0000651 for (unsigned op = 0, Eop = I->getNumOperands(); op < Eop; op += 2) {
652 if (op) Out << ", ";
653 Out << "[";
Chris Lattnera073acb2001-07-07 08:36:50 +0000654 writeOperand(I->getOperand(op ), false); Out << ",";
Chris Lattner4b94e232001-06-21 05:29:56 +0000655 writeOperand(I->getOperand(op+1), false); Out << " ]";
Chris Lattner931ef3b2001-06-11 15:04:20 +0000656 }
Chris Lattner862e3382001-10-13 06:42:36 +0000657 } else if (isa<ReturnInst>(I) && !Operand) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000658 Out << " void";
Chris Lattner862e3382001-10-13 06:42:36 +0000659 } else if (isa<CallInst>(I)) {
Chris Lattner2f2d9472001-11-06 21:28:12 +0000660 const PointerType *PTy = dyn_cast<PointerType>(Operand->getType());
Chris Lattner91db5822002-03-29 03:44:36 +0000661 const FunctionType*MTy = PTy ? dyn_cast<FunctionType>(PTy->getElementType()):0;
Chris Lattner2f2d9472001-11-06 21:28:12 +0000662 const Type *RetTy = MTy ? MTy->getReturnType() : 0;
663
664 // If possible, print out the short form of the call instruction, but we can
Chris Lattner6915f8f2002-04-07 22:49:37 +0000665 // only do this if the first argument is a pointer to a nonvararg function,
666 // and if the value returned is not a pointer to a function.
Chris Lattner2f2d9472001-11-06 21:28:12 +0000667 //
Chris Lattner8d48df22002-04-13 18:34:38 +0000668 if (RetTy && MTy && !MTy->isVarArg() &&
669 (!isa<PointerType>(RetTy) ||
670 !isa<FunctionType>(cast<PointerType>(RetTy)))) {
Chris Lattner2f2d9472001-11-06 21:28:12 +0000671 Out << " "; printType(RetTy);
672 writeOperand(Operand, false);
673 } else {
674 writeOperand(Operand, true);
675 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000676 Out << "(";
Chris Lattnera073acb2001-07-07 08:36:50 +0000677 if (I->getNumOperands() > 1) writeOperand(I->getOperand(1), true);
678 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; ++op) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000679 Out << ",";
Chris Lattnera073acb2001-07-07 08:36:50 +0000680 writeOperand(I->getOperand(op), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000681 }
682
683 Out << " )";
Chris Lattner862e3382001-10-13 06:42:36 +0000684 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(I)) {
685 // TODO: Should try to print out short form of the Invoke instruction
686 writeOperand(Operand, true);
687 Out << "(";
688 if (I->getNumOperands() > 3) writeOperand(I->getOperand(3), true);
689 for (unsigned op = 4, Eop = I->getNumOperands(); op < Eop; ++op) {
690 Out << ",";
691 writeOperand(I->getOperand(op), true);
692 }
693
694 Out << " )\n\t\t\tto";
695 writeOperand(II->getNormalDest(), true);
696 Out << " except";
697 writeOperand(II->getExceptionalDest(), true);
698
Chris Lattner8d48df22002-04-13 18:34:38 +0000699 } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(I)) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000700 Out << " ";
Chris Lattner8d48df22002-04-13 18:34:38 +0000701 printType(AI->getType()->getElementType());
702 if (AI->isArrayAllocation()) {
Chris Lattnera073acb2001-07-07 08:36:50 +0000703 Out << ",";
Chris Lattner8d48df22002-04-13 18:34:38 +0000704 writeOperand(AI->getArraySize(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000705 }
Chris Lattner862e3382001-10-13 06:42:36 +0000706 } else if (isa<CastInst>(I)) {
Chris Lattnera6821822001-07-08 04:57:15 +0000707 writeOperand(Operand, true);
Chris Lattner7bfee412001-10-29 16:05:51 +0000708 Out << " to ";
709 printType(I->getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000710 } else if (Operand) { // Print the normal way...
711
712 // PrintAllTypes - Instructions who have operands of all the same type
713 // omit the type from all but the first operand. If the instruction has
714 // different type operands (for example br), then they are all printed.
715 bool PrintAllTypes = false;
716 const Type *TheType = Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000717
Chris Lattnera073acb2001-07-07 08:36:50 +0000718 for (unsigned i = 1, E = I->getNumOperands(); i != E; ++i) {
719 Operand = I->getOperand(i);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000720 if (Operand->getType() != TheType) {
721 PrintAllTypes = true; // We have differing types! Print them all!
722 break;
723 }
724 }
725
Chris Lattnerb6fe2342001-10-20 09:33:10 +0000726 // Shift Left & Right print both types even for Ubyte LHS
727 if (isa<ShiftInst>(I)) PrintAllTypes = true;
728
Chris Lattner7bfee412001-10-29 16:05:51 +0000729 if (!PrintAllTypes) {
730 Out << " ";
731 printType(I->getOperand(0)->getType());
732 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000733
Chris Lattnera073acb2001-07-07 08:36:50 +0000734 for (unsigned i = 0, E = I->getNumOperands(); i != E; ++i) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000735 if (i) Out << ",";
Chris Lattnera073acb2001-07-07 08:36:50 +0000736 writeOperand(I->getOperand(i), PrintAllTypes);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000737 }
738 }
739
Chris Lattner862e3382001-10-13 06:42:36 +0000740 printInfoComment(I);
Chris Lattner7f74a562002-01-20 22:54:45 +0000741 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000742}
743
744
745//===----------------------------------------------------------------------===//
746// External Interface declarations
747//===----------------------------------------------------------------------===//
748
749
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000750void Module::print(std::ostream &o) const {
751 SlotCalculator SlotTable(this, true);
752 AssemblyWriter W(o, SlotTable, this);
753 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000754}
755
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000756void GlobalVariable::print(std::ostream &o) const {
757 SlotCalculator SlotTable(getParent(), true);
758 AssemblyWriter W(o, SlotTable, getParent());
759 W.write(this);
Chris Lattneradfe0d12001-09-10 20:08:19 +0000760}
761
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000762void Function::print(std::ostream &o) const {
763 SlotCalculator SlotTable(getParent(), true);
764 AssemblyWriter W(o, SlotTable, getParent());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000765
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000766 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000767}
768
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000769void BasicBlock::print(std::ostream &o) const {
770 SlotCalculator SlotTable(getParent(), true);
Chris Lattner7bfee412001-10-29 16:05:51 +0000771 AssemblyWriter W(o, SlotTable,
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000772 getParent() ? getParent()->getParent() : 0);
773 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000774}
775
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000776void Instruction::print(std::ostream &o) const {
777 const Function *F = getParent() ? getParent()->getParent() : 0;
Chris Lattner57698e22002-03-26 18:01:55 +0000778 SlotCalculator SlotTable(F, true);
779 AssemblyWriter W(o, SlotTable, F ? F->getParent() : 0);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000780
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000781 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000782}
Chris Lattner7db79582001-11-07 04:21:57 +0000783
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000784void Constant::print(std::ostream &o) const {
785 if (this == 0) { o << "<null> constant value\n"; return; }
786 o << " " << getType()->getDescription() << " " << getStrValue();
787}
788
789void Type::print(std::ostream &o) const {
790 if (this == 0)
791 o << "<null Type>";
792 else
793 o << getDescription();
794}
795
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000796void Argument::print(std::ostream &o) const {
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000797 o << getType() << " " << getName();
798}
799
800void Value::dump() const { print(std::cerr); }
801
802//===----------------------------------------------------------------------===//
803// CachedWriter Class Implementation
804//===----------------------------------------------------------------------===//
805
Chris Lattner7db79582001-11-07 04:21:57 +0000806void CachedWriter::setModule(const Module *M) {
807 delete SC; delete AW;
808 if (M) {
809 SC = new SlotCalculator(M, true);
810 AW = new AssemblyWriter(Out, *SC, M);
811 } else {
812 SC = 0; AW = 0;
813 }
814}
815
816CachedWriter::~CachedWriter() {
817 delete AW;
818 delete SC;
819}
820
821CachedWriter &CachedWriter::operator<<(const Value *V) {
822 assert(AW && SC && "CachedWriter does not have a current module!");
823 switch (V->getValueType()) {
824 case Value::ConstantVal:
825 Out << " "; AW->write(V->getType());
Chris Lattner3462ae32001-12-03 22:26:30 +0000826 Out << " " << cast<Constant>(V)->getStrValue(); break;
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000827 case Value::ArgumentVal:
Chris Lattner7db79582001-11-07 04:21:57 +0000828 AW->write(V->getType()); Out << " " << V->getName(); break;
829 case Value::TypeVal: AW->write(cast<const Type>(V)); break;
830 case Value::InstructionVal: AW->write(cast<Instruction>(V)); break;
831 case Value::BasicBlockVal: AW->write(cast<BasicBlock>(V)); break;
Chris Lattner57698e22002-03-26 18:01:55 +0000832 case Value::FunctionVal: AW->write(cast<Function>(V)); break;
Chris Lattner7db79582001-11-07 04:21:57 +0000833 case Value::GlobalVariableVal: AW->write(cast<GlobalVariable>(V)); break;
834 case Value::ModuleVal: AW->write(cast<Module>(V)); break;
835 default: Out << "<unknown value type: " << V->getValueType() << ">"; break;
836 }
837 return *this;
838}