blob: 0c62d05d2b3781db6d819f9b191e17d2cc4b4baf [file] [log] [blame]
Chris Lattner8da78af2002-04-07 22:31:46 +00001//===-- AsmWriter.cpp - Printing LLVM as an assembly file -----------------===//
Chris Lattner00950542001-06-06 20:29:01 +00002//
3// This library implements the functionality defined in llvm/Assembly/Writer.h
4//
Chris Lattner02b93992002-04-12 18:21:53 +00005// Note that these routines must be extremely tolerant of various errors in the
Chris Lattner8f77dae2003-05-08 02:44:12 +00006// LLVM code, because it can be used for debugging transformations.
Chris Lattner02b93992002-04-12 18:21:53 +00007//
Chris Lattner00950542001-06-06 20:29:01 +00008//===----------------------------------------------------------------------===//
9
Chris Lattnerda1fbcc2001-11-07 04:21:57 +000010#include "llvm/Assembly/CachedWriter.h"
Chris Lattner75cf7cf2002-04-08 22:03:40 +000011#include "llvm/Assembly/Writer.h"
Chris Lattnerf082b802002-07-23 18:07:49 +000012#include "llvm/Assembly/PrintModulePass.h"
Chris Lattnerb5794002002-04-07 22:49:37 +000013#include "llvm/SlotCalculator.h"
Chris Lattner3eb59c02002-04-29 18:46:50 +000014#include "llvm/DerivedTypes.h"
Vikram S. Adveb4dbb442002-07-14 23:14:45 +000015#include "llvm/Instruction.h"
Chris Lattner00950542001-06-06 20:29:01 +000016#include "llvm/Module.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000017#include "llvm/Constants.h"
Chris Lattner00950542001-06-06 20:29:01 +000018#include "llvm/iMemory.h"
Chris Lattnere02fa852001-10-13 06:42:36 +000019#include "llvm/iTerminators.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000020#include "llvm/iPHINode.h"
21#include "llvm/iOther.h"
Chris Lattner007377f2001-09-07 16:36:04 +000022#include "llvm/SymbolTable.h"
Chris Lattner061269b2002-10-02 19:38:55 +000023#include "llvm/Support/CFG.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000024#include "Support/StringExtras.h"
25#include "Support/STLExtras.h"
Chris Lattner007377f2001-09-07 16:36:04 +000026#include <algorithm>
Chris Lattnerc1824992001-10-29 16:05:51 +000027
Chris Lattnera6275cc2002-07-26 21:12:46 +000028static RegisterPass<PrintModulePass>
29X("printm", "Print module to stderr",PassInfo::Analysis|PassInfo::Optimization);
30static RegisterPass<PrintFunctionPass>
31Y("print","Print function to stderr",PassInfo::Analysis|PassInfo::Optimization);
Chris Lattnerf082b802002-07-23 18:07:49 +000032
Chris Lattner7b13f562003-05-08 02:08:14 +000033static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
34 bool PrintName,
35 std::map<const Type *, std::string> &TypeTable,
Chris Lattner7a716ad2002-04-16 21:36:08 +000036 SlotCalculator *Table);
37
Chris Lattner207b5bc2001-10-29 16:37:48 +000038static const Module *getModuleFromVal(const Value *V) {
Chris Lattner73e21422002-04-09 19:48:49 +000039 if (const Argument *MA = dyn_cast<const Argument>(V))
Chris Lattner207b5bc2001-10-29 16:37:48 +000040 return MA->getParent() ? MA->getParent()->getParent() : 0;
41 else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(V))
42 return BB->getParent() ? BB->getParent()->getParent() : 0;
43 else if (const Instruction *I = dyn_cast<const Instruction>(V)) {
Chris Lattner79df7c02002-03-26 18:01:55 +000044 const Function *M = I->getParent() ? I->getParent()->getParent() : 0;
Chris Lattner207b5bc2001-10-29 16:37:48 +000045 return M ? M->getParent() : 0;
Chris Lattner79df7c02002-03-26 18:01:55 +000046 } else if (const GlobalValue *GV = dyn_cast<const GlobalValue>(V))
Chris Lattner207b5bc2001-10-29 16:37:48 +000047 return GV->getParent();
Chris Lattner207b5bc2001-10-29 16:37:48 +000048 return 0;
49}
50
Chris Lattnerc1824992001-10-29 16:05:51 +000051static SlotCalculator *createSlotCalculator(const Value *V) {
52 assert(!isa<Type>(V) && "Can't create an SC for a type!");
Chris Lattner73e21422002-04-09 19:48:49 +000053 if (const Argument *FA = dyn_cast<const Argument>(V)) {
Chris Lattner79df7c02002-03-26 18:01:55 +000054 return new SlotCalculator(FA->getParent(), true);
Chris Lattnerc1824992001-10-29 16:05:51 +000055 } else if (const Instruction *I = dyn_cast<const Instruction>(V)) {
56 return new SlotCalculator(I->getParent()->getParent(), true);
57 } else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(V)) {
58 return new SlotCalculator(BB->getParent(), true);
Chris Lattner79df7c02002-03-26 18:01:55 +000059 } else if (const GlobalVariable *GV = dyn_cast<const GlobalVariable>(V)){
Chris Lattnerc1824992001-10-29 16:05:51 +000060 return new SlotCalculator(GV->getParent(), true);
Chris Lattner79df7c02002-03-26 18:01:55 +000061 } else if (const Function *Func = dyn_cast<const Function>(V)) {
62 return new SlotCalculator(Func, true);
Chris Lattnerc1824992001-10-29 16:05:51 +000063 }
64 return 0;
65}
Chris Lattner00950542001-06-06 20:29:01 +000066
Chris Lattner207b5bc2001-10-29 16:37:48 +000067
68// If the module has a symbol table, take all global types and stuff their
69// names into the TypeNames map.
70//
71static void fillTypeNameTable(const Module *M,
Chris Lattner7b13f562003-05-08 02:08:14 +000072 std::map<const Type *, std::string> &TypeNames) {
Chris Lattner6e6026b2002-11-20 18:36:02 +000073 if (!M) return;
74 const SymbolTable &ST = M->getSymbolTable();
75 SymbolTable::const_iterator PI = ST.find(Type::TypeTy);
76 if (PI != ST.end()) {
77 SymbolTable::type_const_iterator I = PI->second.begin();
78 for (; I != PI->second.end(); ++I) {
79 // As a heuristic, don't insert pointer to primitive types, because
80 // they are used too often to have a single useful name.
81 //
82 const Type *Ty = cast<const Type>(I->second);
83 if (!isa<PointerType>(Ty) ||
84 !cast<PointerType>(Ty)->getElementType()->isPrimitiveType())
85 TypeNames.insert(std::make_pair(Ty, "%"+I->first));
Chris Lattner207b5bc2001-10-29 16:37:48 +000086 }
87 }
88}
89
90
91
Chris Lattner7b13f562003-05-08 02:08:14 +000092static std::string calcTypeName(const Type *Ty,
93 std::vector<const Type *> &TypeStack,
94 std::map<const Type *, std::string> &TypeNames){
Chris Lattner207b5bc2001-10-29 16:37:48 +000095 if (Ty->isPrimitiveType()) return Ty->getDescription(); // Base case
96
97 // Check to see if the type is named.
Chris Lattner7b13f562003-05-08 02:08:14 +000098 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
Chris Lattner207b5bc2001-10-29 16:37:48 +000099 if (I != TypeNames.end()) return I->second;
100
101 // Check to see if the Type is already on the stack...
102 unsigned Slot = 0, CurSize = TypeStack.size();
103 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
104
105 // This is another base case for the recursion. In this case, we know
106 // that we have looped back to a type that we have previously visited.
107 // Generate the appropriate upreference to handle this.
108 //
109 if (Slot < CurSize)
110 return "\\" + utostr(CurSize-Slot); // Here's the upreference
111
112 TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
113
Chris Lattner7b13f562003-05-08 02:08:14 +0000114 std::string Result;
Chris Lattner207b5bc2001-10-29 16:37:48 +0000115 switch (Ty->getPrimitiveID()) {
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000116 case Type::FunctionTyID: {
Chris Lattner2761e9f2002-04-13 20:53:41 +0000117 const FunctionType *FTy = cast<const FunctionType>(Ty);
118 Result = calcTypeName(FTy->getReturnType(), TypeStack, TypeNames) + " (";
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000119 for (FunctionType::ParamTypes::const_iterator
Chris Lattner2761e9f2002-04-13 20:53:41 +0000120 I = FTy->getParamTypes().begin(),
121 E = FTy->getParamTypes().end(); I != E; ++I) {
122 if (I != FTy->getParamTypes().begin())
Chris Lattner207b5bc2001-10-29 16:37:48 +0000123 Result += ", ";
124 Result += calcTypeName(*I, TypeStack, TypeNames);
125 }
Chris Lattner2761e9f2002-04-13 20:53:41 +0000126 if (FTy->isVarArg()) {
127 if (!FTy->getParamTypes().empty()) Result += ", ";
Chris Lattner207b5bc2001-10-29 16:37:48 +0000128 Result += "...";
129 }
130 Result += ")";
131 break;
132 }
133 case Type::StructTyID: {
134 const StructType *STy = cast<const StructType>(Ty);
135 Result = "{ ";
136 for (StructType::ElementTypes::const_iterator
137 I = STy->getElementTypes().begin(),
138 E = STy->getElementTypes().end(); I != E; ++I) {
139 if (I != STy->getElementTypes().begin())
140 Result += ", ";
141 Result += calcTypeName(*I, TypeStack, TypeNames);
142 }
143 Result += " }";
144 break;
145 }
146 case Type::PointerTyID:
Chris Lattner7a176752001-12-04 00:03:30 +0000147 Result = calcTypeName(cast<const PointerType>(Ty)->getElementType(),
Chris Lattner02b93992002-04-12 18:21:53 +0000148 TypeStack, TypeNames) + "*";
Chris Lattner207b5bc2001-10-29 16:37:48 +0000149 break;
150 case Type::ArrayTyID: {
151 const ArrayType *ATy = cast<const ArrayType>(Ty);
Chris Lattnerff5c2962002-04-13 21:11:04 +0000152 Result = "[" + utostr(ATy->getNumElements()) + " x ";
Chris Lattner207b5bc2001-10-29 16:37:48 +0000153 Result += calcTypeName(ATy->getElementType(), TypeStack, TypeNames) + "]";
154 break;
155 }
Chris Lattner9e094c42003-05-14 17:50:47 +0000156 case Type::OpaqueTyID:
157 Result = "opaque";
158 break;
Chris Lattner207b5bc2001-10-29 16:37:48 +0000159 default:
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000160 Result = "<unrecognized-type>";
Chris Lattner207b5bc2001-10-29 16:37:48 +0000161 }
162
163 TypeStack.pop_back(); // Remove self from stack...
164 return Result;
165}
166
167
168// printTypeInt - The internal guts of printing out a type that has a
169// potentially named portion.
170//
Chris Lattner7b13f562003-05-08 02:08:14 +0000171static std::ostream &printTypeInt(std::ostream &Out, const Type *Ty,
172 std::map<const Type *, std::string> &TypeNames) {
Chris Lattner207b5bc2001-10-29 16:37:48 +0000173 // Primitive types always print out their description, regardless of whether
174 // they have been named or not.
175 //
176 if (Ty->isPrimitiveType()) return Out << Ty->getDescription();
177
178 // Check to see if the type is named.
Chris Lattner7b13f562003-05-08 02:08:14 +0000179 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
Chris Lattner207b5bc2001-10-29 16:37:48 +0000180 if (I != TypeNames.end()) return Out << I->second;
181
182 // Otherwise we have a type that has not been named but is a derived type.
183 // Carefully recurse the type hierarchy to print out any contained symbolic
184 // names.
185 //
Chris Lattner7b13f562003-05-08 02:08:14 +0000186 std::vector<const Type *> TypeStack;
187 std::string TypeName = calcTypeName(Ty, TypeStack, TypeNames);
Chris Lattner697954c2002-01-20 22:54:45 +0000188 TypeNames.insert(std::make_pair(Ty, TypeName));//Cache type name for later use
Chris Lattner207b5bc2001-10-29 16:37:48 +0000189 return Out << TypeName;
190}
191
Chris Lattnere51e03b2001-10-31 04:33:19 +0000192
Chris Lattner207b5bc2001-10-29 16:37:48 +0000193// WriteTypeSymbolic - This attempts to write the specified type as a symbolic
194// type, iff there is an entry in the modules symbol table for the specified
195// type or one of it's component types. This is slower than a simple x << Type;
196//
Chris Lattner7b13f562003-05-08 02:08:14 +0000197std::ostream &WriteTypeSymbolic(std::ostream &Out, const Type *Ty,
198 const Module *M) {
Chris Lattner207b5bc2001-10-29 16:37:48 +0000199 Out << " ";
200
201 // If they want us to print out a type, attempt to make it symbolic if there
202 // is a symbol table in the module...
Chris Lattner6e6026b2002-11-20 18:36:02 +0000203 if (M) {
Chris Lattner7b13f562003-05-08 02:08:14 +0000204 std::map<const Type *, std::string> TypeNames;
Chris Lattner207b5bc2001-10-29 16:37:48 +0000205 fillTypeNameTable(M, TypeNames);
206
Chris Lattner7b8660d2001-10-29 16:40:32 +0000207 return printTypeInt(Out, Ty, TypeNames);
Chris Lattner207b5bc2001-10-29 16:37:48 +0000208 } else {
Chris Lattner7b8660d2001-10-29 16:40:32 +0000209 return Out << Ty->getDescription();
Chris Lattner207b5bc2001-10-29 16:37:48 +0000210 }
211}
212
Chris Lattner7b13f562003-05-08 02:08:14 +0000213static void WriteConstantInt(std::ostream &Out, const Constant *CV,
214 bool PrintName,
215 std::map<const Type *, std::string> &TypeTable,
Chris Lattner7a716ad2002-04-16 21:36:08 +0000216 SlotCalculator *Table) {
Chris Lattner66e810b2002-04-18 18:53:13 +0000217 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
218 Out << (CB == ConstantBool::True ? "true" : "false");
219 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV)) {
220 Out << CI->getValue();
221 } else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV)) {
222 Out << CI->getValue();
223 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
224 // We would like to output the FP constant value in exponential notation,
225 // but we cannot do this if doing so will lose precision. Check here to
226 // make sure that we only output it in exponential format if we can parse
227 // the value back and get the same value.
228 //
229 std::string StrVal = ftostr(CFP->getValue());
230
231 // Check to make sure that the stringized number is not some string like
232 // "Inf" or NaN, that atof will accept, but the lexer will not. Check that
233 // the string matches the "[-+]?[0-9]" regex.
234 //
235 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
236 ((StrVal[0] == '-' || StrVal[0] == '+') &&
Brian Gaekeb471a232003-06-17 23:55:35 +0000237 (StrVal[1] >= '0' && StrVal[1] <= '9')))
Chris Lattner66e810b2002-04-18 18:53:13 +0000238 // Reparse stringized version!
239 if (atof(StrVal.c_str()) == CFP->getValue()) {
240 Out << StrVal; return;
241 }
242
243 // Otherwise we could not reparse it to exactly the same value, so we must
244 // output the string in hexadecimal format!
245 //
246 // Behave nicely in the face of C TBAA rules... see:
247 // http://www.nullstone.com/htmls/category/aliastyp.htm
248 //
249 double Val = CFP->getValue();
250 char *Ptr = (char*)&Val;
251 assert(sizeof(double) == sizeof(uint64_t) && sizeof(double) == 8 &&
252 "assuming that double is 64 bits!");
253 Out << "0x" << utohexstr(*(uint64_t*)Ptr);
254
255 } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
Chris Lattnere9a64ea2003-06-28 20:08:24 +0000256 if (CA->getNumOperands() > 5 && CA->isNullValue()) {
257 Out << "zeroinitializer";
258 return;
259 }
260
Chris Lattner66e810b2002-04-18 18:53:13 +0000261 // As a special case, print the array as a string if it is an array of
262 // ubytes or an array of sbytes with positive values.
263 //
264 const Type *ETy = CA->getType()->getElementType();
265 bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
266
267 if (ETy == Type::SByteTy)
268 for (unsigned i = 0; i < CA->getNumOperands(); ++i)
269 if (cast<ConstantSInt>(CA->getOperand(i))->getValue() < 0) {
270 isString = false;
271 break;
272 }
273
274 if (isString) {
275 Out << "c\"";
276 for (unsigned i = 0; i < CA->getNumOperands(); ++i) {
277 unsigned char C = (ETy == Type::SByteTy) ?
278 (unsigned char)cast<ConstantSInt>(CA->getOperand(i))->getValue() :
279 (unsigned char)cast<ConstantUInt>(CA->getOperand(i))->getValue();
280
Chris Lattnerfc944462002-07-31 23:56:44 +0000281 if (isprint(C) && C != '"' && C != '\\') {
Chris Lattner66e810b2002-04-18 18:53:13 +0000282 Out << C;
283 } else {
284 Out << '\\'
285 << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'))
286 << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
287 }
288 }
289 Out << "\"";
290
291 } else { // Cannot output in string format...
Chris Lattner7a716ad2002-04-16 21:36:08 +0000292 Out << "[";
293 if (CA->getNumOperands()) {
294 Out << " ";
Chris Lattner66e810b2002-04-18 18:53:13 +0000295 printTypeInt(Out, ETy, TypeTable);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000296 WriteAsOperandInternal(Out, CA->getOperand(0),
297 PrintName, TypeTable, Table);
298 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
299 Out << ", ";
Chris Lattner66e810b2002-04-18 18:53:13 +0000300 printTypeInt(Out, ETy, TypeTable);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000301 WriteAsOperandInternal(Out, CA->getOperand(i), PrintName,
302 TypeTable, Table);
303 }
304 }
305 Out << " ]";
306 }
307 } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Chris Lattnere9a64ea2003-06-28 20:08:24 +0000308 if (CS->getNumOperands() > 5 && CS->isNullValue()) {
309 Out << "zeroinitializer";
310 return;
311 }
312
Chris Lattner7a716ad2002-04-16 21:36:08 +0000313 Out << "{";
314 if (CS->getNumOperands()) {
315 Out << " ";
316 printTypeInt(Out, CS->getOperand(0)->getType(), TypeTable);
317
318 WriteAsOperandInternal(Out, CS->getOperand(0),
319 PrintName, TypeTable, Table);
320
321 for (unsigned i = 1; i < CS->getNumOperands(); i++) {
322 Out << ", ";
323 printTypeInt(Out, CS->getOperand(i)->getType(), TypeTable);
324
325 WriteAsOperandInternal(Out, CS->getOperand(i),
326 PrintName, TypeTable, Table);
327 }
328 }
329
330 Out << " }";
331 } else if (isa<ConstantPointerNull>(CV)) {
332 Out << "null";
333
Chris Lattner7e708292002-06-25 16:13:24 +0000334 } else if (const ConstantPointerRef *PR = dyn_cast<ConstantPointerRef>(CV)) {
Chris Lattner66e810b2002-04-18 18:53:13 +0000335 const GlobalValue *V = PR->getValue();
336 if (V->hasName()) {
337 Out << "%" << V->getName();
338 } else if (Table) {
339 int Slot = Table->getValSlot(V);
340 if (Slot >= 0)
341 Out << "%" << Slot;
342 else
343 Out << "<pointer reference badref>";
344 } else {
345 Out << "<pointer reference without context info>";
346 }
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000347
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000348 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Chris Lattner38d87732003-03-06 23:23:32 +0000349 Out << CE->getOpcodeName() << " (";
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000350
351 for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
352 printTypeInt(Out, (*OI)->getType(), TypeTable);
353 WriteAsOperandInternal(Out, *OI, PrintName, TypeTable, Table);
354 if (OI+1 != CE->op_end())
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000355 Out << ", ";
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000356 }
357
Chris Lattner1c93e5b2002-08-16 21:17:11 +0000358 if (CE->getOpcode() == Instruction::Cast) {
Chris Lattner95586b82002-08-15 19:37:43 +0000359 Out << " to ";
360 printTypeInt(Out, CE->getType(), TypeTable);
361 }
Chris Lattner1c93e5b2002-08-16 21:17:11 +0000362 Out << ")";
Chris Lattner95586b82002-08-15 19:37:43 +0000363
Chris Lattner7a716ad2002-04-16 21:36:08 +0000364 } else {
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000365 Out << "<placeholder or erroneous Constant>";
Chris Lattner7a716ad2002-04-16 21:36:08 +0000366 }
367}
368
369
370// WriteAsOperand - Write the name of the specified value out to the specified
371// ostream. This can be useful when you just want to print int %reg126, not the
372// whole instruction that generated it.
373//
Chris Lattner7b13f562003-05-08 02:08:14 +0000374static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
375 bool PrintName,
376 std::map<const Type*, std::string> &TypeTable,
Chris Lattner7a716ad2002-04-16 21:36:08 +0000377 SlotCalculator *Table) {
378 Out << " ";
379 if (PrintName && V->hasName()) {
380 Out << "%" << V->getName();
381 } else {
382 if (const Constant *CV = dyn_cast<const Constant>(V)) {
383 WriteConstantInt(Out, CV, PrintName, TypeTable, Table);
384 } else {
385 int Slot;
386 if (Table) {
387 Slot = Table->getValSlot(V);
388 } else {
389 if (const Type *Ty = dyn_cast<const Type>(V)) {
390 Out << Ty->getDescription();
391 return;
392 }
393
394 Table = createSlotCalculator(V);
395 if (Table == 0) { Out << "BAD VALUE TYPE!"; return; }
396
397 Slot = Table->getValSlot(V);
398 delete Table;
399 }
400 if (Slot >= 0) Out << "%" << Slot;
401 else if (PrintName)
402 Out << "<badref>"; // Not embeded into a location?
403 }
404 }
405}
406
407
Chris Lattner207b5bc2001-10-29 16:37:48 +0000408
409// WriteAsOperand - Write the name of the specified value out to the specified
410// ostream. This can be useful when you just want to print int %reg126, not the
411// whole instruction that generated it.
412//
Chris Lattner7b13f562003-05-08 02:08:14 +0000413std::ostream &WriteAsOperand(std::ostream &Out, const Value *V, bool PrintType,
414 bool PrintName, const Module *Context) {
415 std::map<const Type *, std::string> TypeNames;
Chris Lattner607dc682002-07-10 16:48:17 +0000416 if (Context == 0) Context = getModuleFromVal(V);
Chris Lattner207b5bc2001-10-29 16:37:48 +0000417
Chris Lattner6e6026b2002-11-20 18:36:02 +0000418 if (Context)
Chris Lattner607dc682002-07-10 16:48:17 +0000419 fillTypeNameTable(Context, TypeNames);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000420
421 if (PrintType)
422 printTypeInt(Out, V->getType(), TypeNames);
423
Chris Lattner607dc682002-07-10 16:48:17 +0000424 WriteAsOperandInternal(Out, V, PrintName, TypeNames, 0);
Chris Lattner622f7402001-07-20 19:15:21 +0000425 return Out;
426}
427
428
Chris Lattnerd8c2e422001-07-12 23:35:26 +0000429
Chris Lattner007377f2001-09-07 16:36:04 +0000430class AssemblyWriter {
Chris Lattner7b13f562003-05-08 02:08:14 +0000431 std::ostream &Out;
Chris Lattner00950542001-06-06 20:29:01 +0000432 SlotCalculator &Table;
Chris Lattnerc1824992001-10-29 16:05:51 +0000433 const Module *TheModule;
Chris Lattner7b13f562003-05-08 02:08:14 +0000434 std::map<const Type *, std::string> TypeNames;
Chris Lattner00950542001-06-06 20:29:01 +0000435public:
Chris Lattner7b13f562003-05-08 02:08:14 +0000436 inline AssemblyWriter(std::ostream &o, SlotCalculator &Tab, const Module *M)
Chris Lattnerc1824992001-10-29 16:05:51 +0000437 : Out(o), Table(Tab), TheModule(M) {
438
439 // If the module has a symbol table, take all global types and stuff their
440 // names into the TypeNames map.
441 //
Chris Lattner207b5bc2001-10-29 16:37:48 +0000442 fillTypeNameTable(M, TypeNames);
Chris Lattner00950542001-06-06 20:29:01 +0000443 }
444
Chris Lattnerc1824992001-10-29 16:05:51 +0000445 inline void write(const Module *M) { printModule(M); }
446 inline void write(const GlobalVariable *G) { printGlobal(G); }
Chris Lattner79df7c02002-03-26 18:01:55 +0000447 inline void write(const Function *F) { printFunction(F); }
Chris Lattnerc1824992001-10-29 16:05:51 +0000448 inline void write(const BasicBlock *BB) { printBasicBlock(BB); }
Chris Lattner7e708292002-06-25 16:13:24 +0000449 inline void write(const Instruction *I) { printInstruction(*I); }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000450 inline void write(const Constant *CPV) { printConstant(CPV); }
Chris Lattnerda1fbcc2001-11-07 04:21:57 +0000451 inline void write(const Type *Ty) { printType(Ty); }
Chris Lattner00950542001-06-06 20:29:01 +0000452
Chris Lattner66e810b2002-04-18 18:53:13 +0000453 void writeOperand(const Value *Op, bool PrintType, bool PrintName = true);
454
Chris Lattner00950542001-06-06 20:29:01 +0000455private :
Chris Lattnerc1824992001-10-29 16:05:51 +0000456 void printModule(const Module *M);
457 void printSymbolTable(const SymbolTable &ST);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000458 void printConstant(const Constant *CPV);
Chris Lattnerc1824992001-10-29 16:05:51 +0000459 void printGlobal(const GlobalVariable *GV);
Chris Lattner79df7c02002-03-26 18:01:55 +0000460 void printFunction(const Function *F);
Chris Lattner73e21422002-04-09 19:48:49 +0000461 void printArgument(const Argument *FA);
Chris Lattnerc1824992001-10-29 16:05:51 +0000462 void printBasicBlock(const BasicBlock *BB);
Chris Lattner7e708292002-06-25 16:13:24 +0000463 void printInstruction(const Instruction &I);
Chris Lattner2761e9f2002-04-13 20:53:41 +0000464
465 // printType - Go to extreme measures to attempt to print out a short,
466 // symbolic version of a type name.
467 //
Chris Lattner7b13f562003-05-08 02:08:14 +0000468 std::ostream &printType(const Type *Ty) {
Chris Lattner2761e9f2002-04-13 20:53:41 +0000469 return printTypeInt(Out, Ty, TypeNames);
470 }
471
472 // printTypeAtLeastOneLevel - Print out one level of the possibly complex type
473 // without considering any symbolic types that we may have equal to it.
474 //
Chris Lattner7b13f562003-05-08 02:08:14 +0000475 std::ostream &printTypeAtLeastOneLevel(const Type *Ty);
Chris Lattnerc1824992001-10-29 16:05:51 +0000476
Chris Lattnere02fa852001-10-13 06:42:36 +0000477 // printInfoComment - Print a little comment after the instruction indicating
478 // which slot it occupies.
Chris Lattner7e708292002-06-25 16:13:24 +0000479 void printInfoComment(const Value &V);
Chris Lattner00950542001-06-06 20:29:01 +0000480};
481
482
Chris Lattner2761e9f2002-04-13 20:53:41 +0000483// printTypeAtLeastOneLevel - Print out one level of the possibly complex type
484// without considering any symbolic types that we may have equal to it.
485//
Chris Lattner7b13f562003-05-08 02:08:14 +0000486std::ostream &AssemblyWriter::printTypeAtLeastOneLevel(const Type *Ty) {
Chris Lattner7e708292002-06-25 16:13:24 +0000487 if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
Chris Lattner2761e9f2002-04-13 20:53:41 +0000488 printType(FTy->getReturnType()) << " (";
489 for (FunctionType::ParamTypes::const_iterator
490 I = FTy->getParamTypes().begin(),
491 E = FTy->getParamTypes().end(); I != E; ++I) {
492 if (I != FTy->getParamTypes().begin())
493 Out << ", ";
Chris Lattner7a716ad2002-04-16 21:36:08 +0000494 printType(*I);
Chris Lattner2761e9f2002-04-13 20:53:41 +0000495 }
496 if (FTy->isVarArg()) {
497 if (!FTy->getParamTypes().empty()) Out << ", ";
498 Out << "...";
499 }
500 Out << ")";
Chris Lattner7e708292002-06-25 16:13:24 +0000501 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
Chris Lattner2761e9f2002-04-13 20:53:41 +0000502 Out << "{ ";
503 for (StructType::ElementTypes::const_iterator
504 I = STy->getElementTypes().begin(),
505 E = STy->getElementTypes().end(); I != E; ++I) {
506 if (I != STy->getElementTypes().begin())
507 Out << ", ";
508 printType(*I);
509 }
510 Out << " }";
Chris Lattner7e708292002-06-25 16:13:24 +0000511 } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
Chris Lattner2761e9f2002-04-13 20:53:41 +0000512 printType(PTy->getElementType()) << "*";
Chris Lattner7e708292002-06-25 16:13:24 +0000513 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Chris Lattner2761e9f2002-04-13 20:53:41 +0000514 Out << "[" << ATy->getNumElements() << " x ";
515 printType(ATy->getElementType()) << "]";
Chris Lattner7e708292002-06-25 16:13:24 +0000516 } else if (const OpaqueType *OTy = dyn_cast<OpaqueType>(Ty)) {
Chris Lattner26c69152003-06-01 03:45:51 +0000517 Out << "opaque";
Chris Lattner2761e9f2002-04-13 20:53:41 +0000518 } else {
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000519 if (!Ty->isPrimitiveType())
520 Out << "<unknown derived type>";
Chris Lattner2761e9f2002-04-13 20:53:41 +0000521 printType(Ty);
522 }
523 return Out;
524}
525
526
Chris Lattner007377f2001-09-07 16:36:04 +0000527void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType,
528 bool PrintName) {
Chris Lattnerc1824992001-10-29 16:05:51 +0000529 if (PrintType) { Out << " "; printType(Operand->getType()); }
Chris Lattner7a716ad2002-04-16 21:36:08 +0000530 WriteAsOperandInternal(Out, Operand, PrintName, TypeNames, &Table);
Chris Lattner00950542001-06-06 20:29:01 +0000531}
532
Chris Lattner00950542001-06-06 20:29:01 +0000533
Chris Lattnerc1824992001-10-29 16:05:51 +0000534void AssemblyWriter::printModule(const Module *M) {
Chris Lattner61a909a2003-04-22 19:07:19 +0000535 Out << "target endian = " << (M->isLittleEndian() ? "little" : "big") << "\n";
536 Out << "target pointersize = " << (M->has32BitPointers() ? 32 : 64) << "\n";
537
Chris Lattner007377f2001-09-07 16:36:04 +0000538 // Loop over the symbol table, emitting all named constants...
Chris Lattner6e6026b2002-11-20 18:36:02 +0000539 printSymbolTable(M->getSymbolTable());
Chris Lattner70cc3392001-09-10 07:58:01 +0000540
Chris Lattner7e708292002-06-25 16:13:24 +0000541 for (Module::const_giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
542 printGlobal(I);
Chris Lattner007377f2001-09-07 16:36:04 +0000543
Chris Lattner03e2acb2002-05-06 03:00:40 +0000544 Out << "\nimplementation ; Functions:\n";
Vikram S. Adve5efa3cc2001-09-18 12:48:16 +0000545
Chris Lattnerb5794002002-04-07 22:49:37 +0000546 // Output all of the functions...
Chris Lattner7e708292002-06-25 16:13:24 +0000547 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
548 printFunction(I);
Chris Lattner007377f2001-09-07 16:36:04 +0000549}
550
Chris Lattnerc1824992001-10-29 16:05:51 +0000551void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Chris Lattner70cc3392001-09-10 07:58:01 +0000552 if (GV->hasName()) Out << "%" << GV->getName() << " = ";
Chris Lattnerd70684f2001-09-18 04:01:05 +0000553
Chris Lattner4ad02e72003-04-16 20:28:45 +0000554 if (!GV->hasInitializer())
555 Out << "external ";
556 else
557 switch (GV->getLinkage()) {
558 case GlobalValue::InternalLinkage: Out << "internal "; break;
559 case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
560 case GlobalValue::AppendingLinkage: Out << "appending "; break;
561 case GlobalValue::ExternalLinkage: break;
562 }
Chris Lattnerd70684f2001-09-18 04:01:05 +0000563
Chris Lattnerc1824992001-10-29 16:05:51 +0000564 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattner7a176752001-12-04 00:03:30 +0000565 printType(GV->getType()->getElementType());
Chris Lattnerd70684f2001-09-18 04:01:05 +0000566
567 if (GV->hasInitializer())
568 writeOperand(GV->getInitializer(), false, false);
569
Chris Lattner7e708292002-06-25 16:13:24 +0000570 printInfoComment(*GV);
Chris Lattner697954c2002-01-20 22:54:45 +0000571 Out << "\n";
Chris Lattner70cc3392001-09-10 07:58:01 +0000572}
573
Chris Lattner007377f2001-09-07 16:36:04 +0000574
Chris Lattnerc1824992001-10-29 16:05:51 +0000575// printSymbolTable - Run through symbol table looking for named constants
Chris Lattner007377f2001-09-07 16:36:04 +0000576// if a named constant is found, emit it's declaration...
577//
Chris Lattnerc1824992001-10-29 16:05:51 +0000578void AssemblyWriter::printSymbolTable(const SymbolTable &ST) {
Chris Lattner007377f2001-09-07 16:36:04 +0000579 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
580 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
581 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
582
583 for (; I != End; ++I) {
584 const Value *V = I->second;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000585 if (const Constant *CPV = dyn_cast<const Constant>(V)) {
Chris Lattnerc1824992001-10-29 16:05:51 +0000586 printConstant(CPV);
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000587 } else if (const Type *Ty = dyn_cast<const Type>(V)) {
Chris Lattner2761e9f2002-04-13 20:53:41 +0000588 Out << "\t%" << I->first << " = type ";
589
590 // Make sure we print out at least one level of the type structure, so
591 // that we do not get %FILE = type %FILE
592 //
593 printTypeAtLeastOneLevel(Ty) << "\n";
Chris Lattner007377f2001-09-07 16:36:04 +0000594 }
595 }
Chris Lattner739a56d2001-07-15 06:35:59 +0000596 }
Chris Lattner00950542001-06-06 20:29:01 +0000597}
598
599
Chris Lattnerc1824992001-10-29 16:05:51 +0000600// printConstant - Print out a constant pool entry...
Chris Lattner00950542001-06-06 20:29:01 +0000601//
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000602void AssemblyWriter::printConstant(const Constant *CPV) {
Chris Lattner007377f2001-09-07 16:36:04 +0000603 // Don't print out unnamed constants, they will be inlined
604 if (!CPV->hasName()) return;
Chris Lattner00950542001-06-06 20:29:01 +0000605
Chris Lattner1333ed52001-07-26 16:29:38 +0000606 // Print out name...
Chris Lattner7a716ad2002-04-16 21:36:08 +0000607 Out << "\t%" << CPV->getName() << " =";
Chris Lattner00950542001-06-06 20:29:01 +0000608
609 // Write the value out now...
Chris Lattner7a716ad2002-04-16 21:36:08 +0000610 writeOperand(CPV, true, false);
Chris Lattner00950542001-06-06 20:29:01 +0000611
Chris Lattner7e708292002-06-25 16:13:24 +0000612 printInfoComment(*CPV);
Chris Lattner697954c2002-01-20 22:54:45 +0000613 Out << "\n";
Chris Lattner00950542001-06-06 20:29:01 +0000614}
615
Chris Lattnerb5794002002-04-07 22:49:37 +0000616// printFunction - Print all aspects of a function.
Chris Lattner00950542001-06-06 20:29:01 +0000617//
Chris Lattner7e708292002-06-25 16:13:24 +0000618void AssemblyWriter::printFunction(const Function *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000619 // Print out the return type and name...
Chris Lattner4ad02e72003-04-16 20:28:45 +0000620 Out << "\n";
621
622 if (F->isExternal())
623 Out << "declare ";
624 else
625 switch (F->getLinkage()) {
626 case GlobalValue::InternalLinkage: Out << "internal "; break;
627 case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
628 case GlobalValue::AppendingLinkage: Out << "appending "; break;
629 case GlobalValue::ExternalLinkage: break;
630 }
631
Chris Lattner7e708292002-06-25 16:13:24 +0000632 printType(F->getReturnType()) << " %" << F->getName() << "(";
633 Table.incorporateFunction(F);
Chris Lattner007377f2001-09-07 16:36:04 +0000634
Chris Lattnerc1824992001-10-29 16:05:51 +0000635 // Loop over the arguments, printing them...
Chris Lattner7e708292002-06-25 16:13:24 +0000636 const FunctionType *FT = F->getFunctionType();
Chris Lattner007377f2001-09-07 16:36:04 +0000637
Chris Lattner69da5cf2002-10-13 20:57:00 +0000638 for(Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I)
639 printArgument(I);
Chris Lattner007377f2001-09-07 16:36:04 +0000640
641 // Finish printing arguments...
Chris Lattner7e708292002-06-25 16:13:24 +0000642 if (FT->isVarArg()) {
643 if (FT->getParamTypes().size()) Out << ", ";
Chris Lattner007377f2001-09-07 16:36:04 +0000644 Out << "..."; // Output varargs portion of signature!
645 }
Chris Lattner03e2acb2002-05-06 03:00:40 +0000646 Out << ")";
Chris Lattner007377f2001-09-07 16:36:04 +0000647
Chris Lattner7e708292002-06-25 16:13:24 +0000648 if (F->isExternal()) {
Chris Lattner03e2acb2002-05-06 03:00:40 +0000649 Out << "\n";
650 } else {
651 Out << " {";
Chris Lattner007377f2001-09-07 16:36:04 +0000652
Chris Lattnerb5794002002-04-07 22:49:37 +0000653 // Output all of its basic blocks... for the function
Chris Lattner7e708292002-06-25 16:13:24 +0000654 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
655 printBasicBlock(I);
Chris Lattner007377f2001-09-07 16:36:04 +0000656
Chris Lattner03e2acb2002-05-06 03:00:40 +0000657 Out << "}\n";
Chris Lattner007377f2001-09-07 16:36:04 +0000658 }
659
Chris Lattnerb5794002002-04-07 22:49:37 +0000660 Table.purgeFunction();
Chris Lattner00950542001-06-06 20:29:01 +0000661}
662
Chris Lattner73e21422002-04-09 19:48:49 +0000663// printArgument - This member is called for every argument that
Chris Lattnerb5794002002-04-07 22:49:37 +0000664// is passed into the function. Simply print it out
Chris Lattner00950542001-06-06 20:29:01 +0000665//
Chris Lattner73e21422002-04-09 19:48:49 +0000666void AssemblyWriter::printArgument(const Argument *Arg) {
Chris Lattner00950542001-06-06 20:29:01 +0000667 // Insert commas as we go... the first arg doesn't get a comma
Chris Lattner7e708292002-06-25 16:13:24 +0000668 if (Arg != &Arg->getParent()->afront()) Out << ", ";
Chris Lattner00950542001-06-06 20:29:01 +0000669
670 // Output type...
Chris Lattnerc1824992001-10-29 16:05:51 +0000671 printType(Arg->getType());
Chris Lattner00950542001-06-06 20:29:01 +0000672
673 // Output name, if available...
674 if (Arg->hasName())
675 Out << " %" << Arg->getName();
676 else if (Table.getValSlot(Arg) < 0)
677 Out << "<badref>";
Chris Lattner00950542001-06-06 20:29:01 +0000678}
679
Chris Lattnerc1824992001-10-29 16:05:51 +0000680// printBasicBlock - This member is called for each basic block in a methd.
Chris Lattner00950542001-06-06 20:29:01 +0000681//
Chris Lattnerc1824992001-10-29 16:05:51 +0000682void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Chris Lattner00950542001-06-06 20:29:01 +0000683 if (BB->hasName()) { // Print out the label if it exists...
Chris Lattner061269b2002-10-02 19:38:55 +0000684 Out << "\n" << BB->getName() << ":";
Chris Lattnerafc38682002-05-14 16:02:05 +0000685 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Chris Lattner00950542001-06-06 20:29:01 +0000686 int Slot = Table.getValSlot(BB);
Chris Lattnerb9a45782001-06-07 16:58:55 +0000687 Out << "\n; <label>:";
Chris Lattner00950542001-06-06 20:29:01 +0000688 if (Slot >= 0)
Misha Brukmanbc0e9982003-07-14 17:20:40 +0000689 Out << Slot; // Extra newline separates out label's
Chris Lattner00950542001-06-06 20:29:01 +0000690 else
Chris Lattnerb9a45782001-06-07 16:58:55 +0000691 Out << "<badref>";
Chris Lattner061269b2002-10-02 19:38:55 +0000692 }
693
694 // Output predecessors for the block...
695 Out << "\t\t;";
696 pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
697
698 if (PI == PE) {
699 Out << " No predecessors!";
700 } else {
701 Out << " preds =";
702 writeOperand(*PI, false, true);
703 for (++PI; PI != PE; ++PI) {
704 Out << ",";
705 writeOperand(*PI, false, true);
706 }
Chris Lattner00950542001-06-06 20:29:01 +0000707 }
Chris Lattnerafc38682002-05-14 16:02:05 +0000708
709 Out << "\n";
Chris Lattner00950542001-06-06 20:29:01 +0000710
Chris Lattner007377f2001-09-07 16:36:04 +0000711 // Output all of the instructions in the basic block...
Chris Lattner7e708292002-06-25 16:13:24 +0000712 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
713 printInstruction(*I);
Chris Lattner00950542001-06-06 20:29:01 +0000714}
715
Chris Lattnere02fa852001-10-13 06:42:36 +0000716
717// printInfoComment - Print a little comment after the instruction indicating
718// which slot it occupies.
719//
Chris Lattner7e708292002-06-25 16:13:24 +0000720void AssemblyWriter::printInfoComment(const Value &V) {
721 if (V.getType() != Type::VoidTy) {
Chris Lattnerc1824992001-10-29 16:05:51 +0000722 Out << "\t\t; <";
Chris Lattner7e708292002-06-25 16:13:24 +0000723 printType(V.getType()) << ">";
Chris Lattnere02fa852001-10-13 06:42:36 +0000724
Chris Lattner7e708292002-06-25 16:13:24 +0000725 if (!V.hasName()) {
726 int Slot = Table.getValSlot(&V); // Print out the def slot taken...
Chris Lattnere02fa852001-10-13 06:42:36 +0000727 if (Slot >= 0) Out << ":" << Slot;
728 else Out << ":<badref>";
729 }
Chris Lattner7e708292002-06-25 16:13:24 +0000730 Out << " [#uses=" << V.use_size() << "]"; // Output # uses
Chris Lattnere02fa852001-10-13 06:42:36 +0000731 }
732}
733
Chris Lattnerc1824992001-10-29 16:05:51 +0000734// printInstruction - This member is called for each Instruction in a methd.
Chris Lattner00950542001-06-06 20:29:01 +0000735//
Chris Lattner7e708292002-06-25 16:13:24 +0000736void AssemblyWriter::printInstruction(const Instruction &I) {
Chris Lattner00950542001-06-06 20:29:01 +0000737 Out << "\t";
738
739 // Print out name if it exists...
Chris Lattner7e708292002-06-25 16:13:24 +0000740 if (I.hasName())
741 Out << "%" << I.getName() << " = ";
Chris Lattner00950542001-06-06 20:29:01 +0000742
743 // Print out the opcode...
Chris Lattner7e708292002-06-25 16:13:24 +0000744 Out << I.getOpcodeName();
Chris Lattner00950542001-06-06 20:29:01 +0000745
746 // Print out the type of the operands...
Chris Lattner7e708292002-06-25 16:13:24 +0000747 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
Chris Lattner00950542001-06-06 20:29:01 +0000748
749 // Special case conditional branches to swizzle the condition out to the front
Chris Lattner7e708292002-06-25 16:13:24 +0000750 if (isa<BranchInst>(I) && I.getNumOperands() > 1) {
751 writeOperand(I.getOperand(2), true);
Chris Lattner00950542001-06-06 20:29:01 +0000752 Out << ",";
753 writeOperand(Operand, true);
754 Out << ",";
Chris Lattner7e708292002-06-25 16:13:24 +0000755 writeOperand(I.getOperand(1), true);
Chris Lattner00950542001-06-06 20:29:01 +0000756
Chris Lattner94dc1f22002-04-13 18:34:38 +0000757 } else if (isa<SwitchInst>(I)) {
Chris Lattner00950542001-06-06 20:29:01 +0000758 // Special case switch statement to get formatting nice and correct...
Chris Lattner7e708292002-06-25 16:13:24 +0000759 writeOperand(Operand , true); Out << ",";
760 writeOperand(I.getOperand(1), true); Out << " [";
Chris Lattner00950542001-06-06 20:29:01 +0000761
Chris Lattner7e708292002-06-25 16:13:24 +0000762 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; op += 2) {
Chris Lattner00950542001-06-06 20:29:01 +0000763 Out << "\n\t\t";
Chris Lattner7e708292002-06-25 16:13:24 +0000764 writeOperand(I.getOperand(op ), true); Out << ",";
765 writeOperand(I.getOperand(op+1), true);
Chris Lattner00950542001-06-06 20:29:01 +0000766 }
767 Out << "\n\t]";
Chris Lattnerb00c5822001-10-02 03:41:24 +0000768 } else if (isa<PHINode>(I)) {
Chris Lattnerc1824992001-10-29 16:05:51 +0000769 Out << " ";
Chris Lattner7e708292002-06-25 16:13:24 +0000770 printType(I.getType());
Chris Lattnereed1fc72001-11-06 08:33:46 +0000771 Out << " ";
Chris Lattner00950542001-06-06 20:29:01 +0000772
Chris Lattner7e708292002-06-25 16:13:24 +0000773 for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) {
Chris Lattner8f410ca2001-11-06 01:48:45 +0000774 if (op) Out << ", ";
775 Out << "[";
Chris Lattner7e708292002-06-25 16:13:24 +0000776 writeOperand(I.getOperand(op ), false); Out << ",";
777 writeOperand(I.getOperand(op+1), false); Out << " ]";
Chris Lattnerc24d2082001-06-11 15:04:20 +0000778 }
Chris Lattnere02fa852001-10-13 06:42:36 +0000779 } else if (isa<ReturnInst>(I) && !Operand) {
Chris Lattner00950542001-06-06 20:29:01 +0000780 Out << " void";
Chris Lattnere02fa852001-10-13 06:42:36 +0000781 } else if (isa<CallInst>(I)) {
Chris Lattner268de042001-11-06 21:28:12 +0000782 const PointerType *PTy = dyn_cast<PointerType>(Operand->getType());
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000783 const FunctionType*MTy = PTy ? dyn_cast<FunctionType>(PTy->getElementType()):0;
Chris Lattner268de042001-11-06 21:28:12 +0000784 const Type *RetTy = MTy ? MTy->getReturnType() : 0;
785
786 // If possible, print out the short form of the call instruction, but we can
Chris Lattnerb5794002002-04-07 22:49:37 +0000787 // only do this if the first argument is a pointer to a nonvararg function,
788 // and if the value returned is not a pointer to a function.
Chris Lattner268de042001-11-06 21:28:12 +0000789 //
Chris Lattner94dc1f22002-04-13 18:34:38 +0000790 if (RetTy && MTy && !MTy->isVarArg() &&
791 (!isa<PointerType>(RetTy) ||
Chris Lattnerc1b27182002-07-25 20:58:51 +0000792 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Chris Lattner268de042001-11-06 21:28:12 +0000793 Out << " "; printType(RetTy);
794 writeOperand(Operand, false);
795 } else {
796 writeOperand(Operand, true);
797 }
Chris Lattner00950542001-06-06 20:29:01 +0000798 Out << "(";
Chris Lattner7e708292002-06-25 16:13:24 +0000799 if (I.getNumOperands() > 1) writeOperand(I.getOperand(1), true);
800 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; ++op) {
Chris Lattner00950542001-06-06 20:29:01 +0000801 Out << ",";
Chris Lattner7e708292002-06-25 16:13:24 +0000802 writeOperand(I.getOperand(op), true);
Chris Lattner00950542001-06-06 20:29:01 +0000803 }
804
805 Out << " )";
Chris Lattner7e708292002-06-25 16:13:24 +0000806 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Chris Lattnere02fa852001-10-13 06:42:36 +0000807 // TODO: Should try to print out short form of the Invoke instruction
808 writeOperand(Operand, true);
809 Out << "(";
Chris Lattner7e708292002-06-25 16:13:24 +0000810 if (I.getNumOperands() > 3) writeOperand(I.getOperand(3), true);
811 for (unsigned op = 4, Eop = I.getNumOperands(); op < Eop; ++op) {
Chris Lattnere02fa852001-10-13 06:42:36 +0000812 Out << ",";
Chris Lattner7e708292002-06-25 16:13:24 +0000813 writeOperand(I.getOperand(op), true);
Chris Lattnere02fa852001-10-13 06:42:36 +0000814 }
815
816 Out << " )\n\t\t\tto";
817 writeOperand(II->getNormalDest(), true);
818 Out << " except";
819 writeOperand(II->getExceptionalDest(), true);
820
Chris Lattner7e708292002-06-25 16:13:24 +0000821 } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(&I)) {
Chris Lattnerc1824992001-10-29 16:05:51 +0000822 Out << " ";
Chris Lattner94dc1f22002-04-13 18:34:38 +0000823 printType(AI->getType()->getElementType());
824 if (AI->isArrayAllocation()) {
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000825 Out << ",";
Chris Lattner94dc1f22002-04-13 18:34:38 +0000826 writeOperand(AI->getArraySize(), true);
Chris Lattner00950542001-06-06 20:29:01 +0000827 }
Chris Lattnere02fa852001-10-13 06:42:36 +0000828 } else if (isa<CastInst>(I)) {
Chris Lattner8f77dae2003-05-08 02:44:12 +0000829 writeOperand(Operand, true);
Chris Lattnerc1824992001-10-29 16:05:51 +0000830 Out << " to ";
Chris Lattner7e708292002-06-25 16:13:24 +0000831 printType(I.getType());
Chris Lattner8f77dae2003-05-08 02:44:12 +0000832 } else if (isa<VarArgInst>(I)) {
833 writeOperand(Operand, true);
834 Out << ", ";
835 printType(I.getType());
Chris Lattner00950542001-06-06 20:29:01 +0000836 } else if (Operand) { // Print the normal way...
837
838 // PrintAllTypes - Instructions who have operands of all the same type
839 // omit the type from all but the first operand. If the instruction has
840 // different type operands (for example br), then they are all printed.
841 bool PrintAllTypes = false;
842 const Type *TheType = Operand->getType();
Chris Lattner00950542001-06-06 20:29:01 +0000843
Chris Lattnerffd9bf42003-04-16 20:20:02 +0000844 // Shift Left & Right print both types even for Ubyte LHS
845 if (isa<ShiftInst>(I)) {
846 PrintAllTypes = true;
847 } else {
848 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
849 Operand = I.getOperand(i);
850 if (Operand->getType() != TheType) {
851 PrintAllTypes = true; // We have differing types! Print them all!
852 break;
853 }
Chris Lattner00950542001-06-06 20:29:01 +0000854 }
855 }
Chris Lattnerffd9bf42003-04-16 20:20:02 +0000856
Chris Lattnerc1824992001-10-29 16:05:51 +0000857 if (!PrintAllTypes) {
858 Out << " ";
Chris Lattnerffd9bf42003-04-16 20:20:02 +0000859 printType(TheType);
Chris Lattnerc1824992001-10-29 16:05:51 +0000860 }
Chris Lattner00950542001-06-06 20:29:01 +0000861
Chris Lattner7e708292002-06-25 16:13:24 +0000862 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Chris Lattner00950542001-06-06 20:29:01 +0000863 if (i) Out << ",";
Chris Lattner7e708292002-06-25 16:13:24 +0000864 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner00950542001-06-06 20:29:01 +0000865 }
866 }
867
Chris Lattnere02fa852001-10-13 06:42:36 +0000868 printInfoComment(I);
Chris Lattner697954c2002-01-20 22:54:45 +0000869 Out << "\n";
Chris Lattner00950542001-06-06 20:29:01 +0000870}
871
872
873//===----------------------------------------------------------------------===//
874// External Interface declarations
875//===----------------------------------------------------------------------===//
876
877
Chris Lattner75cf7cf2002-04-08 22:03:40 +0000878void Module::print(std::ostream &o) const {
879 SlotCalculator SlotTable(this, true);
880 AssemblyWriter W(o, SlotTable, this);
881 W.write(this);
Chris Lattner00950542001-06-06 20:29:01 +0000882}
883
Chris Lattner75cf7cf2002-04-08 22:03:40 +0000884void GlobalVariable::print(std::ostream &o) const {
885 SlotCalculator SlotTable(getParent(), true);
886 AssemblyWriter W(o, SlotTable, getParent());
887 W.write(this);
Chris Lattnerb0e45232001-09-10 20:08:19 +0000888}
889
Chris Lattner75cf7cf2002-04-08 22:03:40 +0000890void Function::print(std::ostream &o) const {
891 SlotCalculator SlotTable(getParent(), true);
892 AssemblyWriter W(o, SlotTable, getParent());
Chris Lattner00950542001-06-06 20:29:01 +0000893
Chris Lattner75cf7cf2002-04-08 22:03:40 +0000894 W.write(this);
Chris Lattner00950542001-06-06 20:29:01 +0000895}
896
Chris Lattner75cf7cf2002-04-08 22:03:40 +0000897void BasicBlock::print(std::ostream &o) const {
898 SlotCalculator SlotTable(getParent(), true);
Chris Lattnerc1824992001-10-29 16:05:51 +0000899 AssemblyWriter W(o, SlotTable,
Chris Lattner75cf7cf2002-04-08 22:03:40 +0000900 getParent() ? getParent()->getParent() : 0);
901 W.write(this);
Chris Lattner00950542001-06-06 20:29:01 +0000902}
903
Chris Lattner75cf7cf2002-04-08 22:03:40 +0000904void Instruction::print(std::ostream &o) const {
905 const Function *F = getParent() ? getParent()->getParent() : 0;
Chris Lattner79df7c02002-03-26 18:01:55 +0000906 SlotCalculator SlotTable(F, true);
907 AssemblyWriter W(o, SlotTable, F ? F->getParent() : 0);
Chris Lattner00950542001-06-06 20:29:01 +0000908
Chris Lattner75cf7cf2002-04-08 22:03:40 +0000909 W.write(this);
Chris Lattner00950542001-06-06 20:29:01 +0000910}
Chris Lattnerda1fbcc2001-11-07 04:21:57 +0000911
Chris Lattner75cf7cf2002-04-08 22:03:40 +0000912void Constant::print(std::ostream &o) const {
913 if (this == 0) { o << "<null> constant value\n"; return; }
Chris Lattner3bc06b32002-09-10 15:53:49 +0000914
915 // Handle CPR's special, because they have context information...
916 if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(this)) {
917 CPR->getValue()->print(o); // Print as a global value, with context info.
918 return;
919 }
920
Chris Lattner66e810b2002-04-18 18:53:13 +0000921 o << " " << getType()->getDescription() << " ";
922
Chris Lattner7b13f562003-05-08 02:08:14 +0000923 std::map<const Type *, std::string> TypeTable;
Chris Lattner66e810b2002-04-18 18:53:13 +0000924 WriteConstantInt(o, this, false, TypeTable, 0);
Chris Lattner75cf7cf2002-04-08 22:03:40 +0000925}
926
927void Type::print(std::ostream &o) const {
928 if (this == 0)
929 o << "<null Type>";
930 else
931 o << getDescription();
932}
933
Chris Lattner73e21422002-04-09 19:48:49 +0000934void Argument::print(std::ostream &o) const {
Chris Lattner75cf7cf2002-04-08 22:03:40 +0000935 o << getType() << " " << getName();
936}
937
938void Value::dump() const { print(std::cerr); }
939
940//===----------------------------------------------------------------------===//
941// CachedWriter Class Implementation
942//===----------------------------------------------------------------------===//
943
Chris Lattnerda1fbcc2001-11-07 04:21:57 +0000944void CachedWriter::setModule(const Module *M) {
945 delete SC; delete AW;
946 if (M) {
947 SC = new SlotCalculator(M, true);
948 AW = new AssemblyWriter(Out, *SC, M);
949 } else {
950 SC = 0; AW = 0;
951 }
952}
953
954CachedWriter::~CachedWriter() {
955 delete AW;
956 delete SC;
957}
958
959CachedWriter &CachedWriter::operator<<(const Value *V) {
960 assert(AW && SC && "CachedWriter does not have a current module!");
961 switch (V->getValueType()) {
962 case Value::ConstantVal:
Chris Lattner66e810b2002-04-18 18:53:13 +0000963 case Value::ArgumentVal: AW->writeOperand(V, true, true); break;
Chris Lattnerda1fbcc2001-11-07 04:21:57 +0000964 case Value::TypeVal: AW->write(cast<const Type>(V)); break;
965 case Value::InstructionVal: AW->write(cast<Instruction>(V)); break;
966 case Value::BasicBlockVal: AW->write(cast<BasicBlock>(V)); break;
Chris Lattner79df7c02002-03-26 18:01:55 +0000967 case Value::FunctionVal: AW->write(cast<Function>(V)); break;
Chris Lattnerda1fbcc2001-11-07 04:21:57 +0000968 case Value::GlobalVariableVal: AW->write(cast<GlobalVariable>(V)); break;
Chris Lattnerda1fbcc2001-11-07 04:21:57 +0000969 default: Out << "<unknown value type: " << V->getValueType() << ">"; break;
970 }
971 return *this;
972}