blob: 28f26c833ad91744b2ca0f50ea9a9097491e6847 [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
Chris Lattnerf70da102003-05-08 02:44:12 +00006// LLVM code, because it can be used for debugging transformations.
Chris Lattner189088e2002-04-12 18:21:53 +00007//
Chris Lattner2f7c9632001-06-06 20:29:01 +00008//===----------------------------------------------------------------------===//
9
Chris Lattner7db79582001-11-07 04:21:57 +000010#include "llvm/Assembly/CachedWriter.h"
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +000011#include "llvm/Assembly/Writer.h"
Chris Lattner7f8845a2002-07-23 18:07:49 +000012#include "llvm/Assembly/PrintModulePass.h"
Chris Lattner6915f8f2002-04-07 22:49:37 +000013#include "llvm/SlotCalculator.h"
Chris Lattner913d18f2002-04-29 18:46:50 +000014#include "llvm/DerivedTypes.h"
Vikram S. Adveb952b542002-07-14 23:14:45 +000015#include "llvm/Instruction.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000016#include "llvm/Module.h"
Chris Lattnerca142372002-04-28 19:55:58 +000017#include "llvm/Constants.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000018#include "llvm/iMemory.h"
Chris Lattner862e3382001-10-13 06:42:36 +000019#include "llvm/iTerminators.h"
Chris Lattnerfb5ae022001-12-03 18:02:31 +000020#include "llvm/iPHINode.h"
21#include "llvm/iOther.h"
Chris Lattnerfee714f2001-09-07 16:36:04 +000022#include "llvm/SymbolTable.h"
Chris Lattner58185f22002-10-02 19:38:55 +000023#include "llvm/Support/CFG.h"
Chris Lattner5de22042001-11-27 00:03:19 +000024#include "Support/StringExtras.h"
25#include "Support/STLExtras.h"
Chris Lattnerfee714f2001-09-07 16:36:04 +000026#include <algorithm>
Chris Lattner7bfee412001-10-29 16:05:51 +000027
Chris Lattnerc8b70922002-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 Lattner7f8845a2002-07-23 18:07:49 +000032
Chris Lattnerab7d1ab2003-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 Lattnerd84bb632002-04-16 21:36:08 +000036 SlotCalculator *Table);
37
Chris Lattnerb86620e2001-10-29 16:37:48 +000038static const Module *getModuleFromVal(const Value *V) {
Chris Lattner2e9fa6d2002-04-09 19:48:49 +000039 if (const Argument *MA = dyn_cast<const Argument>(V))
Chris Lattnerb86620e2001-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 Lattner57698e22002-03-26 18:01:55 +000044 const Function *M = I->getParent() ? I->getParent()->getParent() : 0;
Chris Lattnerb86620e2001-10-29 16:37:48 +000045 return M ? M->getParent() : 0;
Chris Lattner57698e22002-03-26 18:01:55 +000046 } else if (const GlobalValue *GV = dyn_cast<const GlobalValue>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +000047 return GV->getParent();
Chris Lattnerb86620e2001-10-29 16:37:48 +000048 return 0;
49}
50
Chris Lattner7bfee412001-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 Lattner2e9fa6d2002-04-09 19:48:49 +000053 if (const Argument *FA = dyn_cast<const Argument>(V)) {
Chris Lattner57698e22002-03-26 18:01:55 +000054 return new SlotCalculator(FA->getParent(), true);
Chris Lattner7bfee412001-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 Lattner57698e22002-03-26 18:01:55 +000059 } else if (const GlobalVariable *GV = dyn_cast<const GlobalVariable>(V)){
Chris Lattner7bfee412001-10-29 16:05:51 +000060 return new SlotCalculator(GV->getParent(), true);
Chris Lattner57698e22002-03-26 18:01:55 +000061 } else if (const Function *Func = dyn_cast<const Function>(V)) {
62 return new SlotCalculator(Func, true);
Chris Lattner7bfee412001-10-29 16:05:51 +000063 }
64 return 0;
65}
Chris Lattner2f7c9632001-06-06 20:29:01 +000066
Chris Lattnerb86620e2001-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 Lattnerab7d1ab2003-05-08 02:08:14 +000072 std::map<const Type *, std::string> &TypeNames) {
Chris Lattner98cf1f52002-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 Lattnerb86620e2001-10-29 16:37:48 +000086 }
87 }
88}
89
90
91
Chris Lattnerab7d1ab2003-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 Lattnerb86620e2001-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 Lattnerab7d1ab2003-05-08 02:08:14 +000098 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
Chris Lattnerb86620e2001-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 Lattnerab7d1ab2003-05-08 02:08:14 +0000114 std::string Result;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000115 switch (Ty->getPrimitiveID()) {
Chris Lattner91db5822002-03-29 03:44:36 +0000116 case Type::FunctionTyID: {
Chris Lattnerd816b532002-04-13 20:53:41 +0000117 const FunctionType *FTy = cast<const FunctionType>(Ty);
118 Result = calcTypeName(FTy->getReturnType(), TypeStack, TypeNames) + " (";
Chris Lattner91db5822002-03-29 03:44:36 +0000119 for (FunctionType::ParamTypes::const_iterator
Chris Lattnerd816b532002-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 Lattnerb86620e2001-10-29 16:37:48 +0000123 Result += ", ";
124 Result += calcTypeName(*I, TypeStack, TypeNames);
125 }
Chris Lattnerd816b532002-04-13 20:53:41 +0000126 if (FTy->isVarArg()) {
127 if (!FTy->getParamTypes().empty()) Result += ", ";
Chris Lattnerb86620e2001-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 Lattner2413b162001-12-04 00:03:30 +0000147 Result = calcTypeName(cast<const PointerType>(Ty)->getElementType(),
Chris Lattner189088e2002-04-12 18:21:53 +0000148 TypeStack, TypeNames) + "*";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000149 break;
150 case Type::ArrayTyID: {
151 const ArrayType *ATy = cast<const ArrayType>(Ty);
Chris Lattner8a939c92002-04-13 21:11:04 +0000152 Result = "[" + utostr(ATy->getNumElements()) + " x ";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000153 Result += calcTypeName(ATy->getElementType(), TypeStack, TypeNames) + "]";
154 break;
155 }
156 default:
Vikram S. Adveb952b542002-07-14 23:14:45 +0000157 Result = "<unrecognized-type>";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000158 }
159
160 TypeStack.pop_back(); // Remove self from stack...
161 return Result;
162}
163
164
165// printTypeInt - The internal guts of printing out a type that has a
166// potentially named portion.
167//
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000168static std::ostream &printTypeInt(std::ostream &Out, const Type *Ty,
169 std::map<const Type *, std::string> &TypeNames) {
Chris Lattnerb86620e2001-10-29 16:37:48 +0000170 // Primitive types always print out their description, regardless of whether
171 // they have been named or not.
172 //
173 if (Ty->isPrimitiveType()) return Out << Ty->getDescription();
174
175 // Check to see if the type is named.
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000176 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000177 if (I != TypeNames.end()) return Out << I->second;
178
179 // Otherwise we have a type that has not been named but is a derived type.
180 // Carefully recurse the type hierarchy to print out any contained symbolic
181 // names.
182 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000183 std::vector<const Type *> TypeStack;
184 std::string TypeName = calcTypeName(Ty, TypeStack, TypeNames);
Chris Lattner7f74a562002-01-20 22:54:45 +0000185 TypeNames.insert(std::make_pair(Ty, TypeName));//Cache type name for later use
Chris Lattnerb86620e2001-10-29 16:37:48 +0000186 return Out << TypeName;
187}
188
Chris Lattner34b95182001-10-31 04:33:19 +0000189
Chris Lattnerb86620e2001-10-29 16:37:48 +0000190// WriteTypeSymbolic - This attempts to write the specified type as a symbolic
191// type, iff there is an entry in the modules symbol table for the specified
192// type or one of it's component types. This is slower than a simple x << Type;
193//
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000194std::ostream &WriteTypeSymbolic(std::ostream &Out, const Type *Ty,
195 const Module *M) {
Chris Lattnerb86620e2001-10-29 16:37:48 +0000196 Out << " ";
197
198 // If they want us to print out a type, attempt to make it symbolic if there
199 // is a symbol table in the module...
Chris Lattner98cf1f52002-11-20 18:36:02 +0000200 if (M) {
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000201 std::map<const Type *, std::string> TypeNames;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000202 fillTypeNameTable(M, TypeNames);
203
Chris Lattner72f866e2001-10-29 16:40:32 +0000204 return printTypeInt(Out, Ty, TypeNames);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000205 } else {
Chris Lattner72f866e2001-10-29 16:40:32 +0000206 return Out << Ty->getDescription();
Chris Lattnerb86620e2001-10-29 16:37:48 +0000207 }
208}
209
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000210static void WriteConstantInt(std::ostream &Out, const Constant *CV,
211 bool PrintName,
212 std::map<const Type *, std::string> &TypeTable,
Chris Lattnerd84bb632002-04-16 21:36:08 +0000213 SlotCalculator *Table) {
Chris Lattner1e194682002-04-18 18:53:13 +0000214 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
215 Out << (CB == ConstantBool::True ? "true" : "false");
216 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV)) {
217 Out << CI->getValue();
218 } else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV)) {
219 Out << CI->getValue();
220 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
221 // We would like to output the FP constant value in exponential notation,
222 // but we cannot do this if doing so will lose precision. Check here to
223 // make sure that we only output it in exponential format if we can parse
224 // the value back and get the same value.
225 //
226 std::string StrVal = ftostr(CFP->getValue());
227
228 // Check to make sure that the stringized number is not some string like
229 // "Inf" or NaN, that atof will accept, but the lexer will not. Check that
230 // the string matches the "[-+]?[0-9]" regex.
231 //
232 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
233 ((StrVal[0] == '-' || StrVal[0] == '+') &&
234 (StrVal[0] >= '0' && StrVal[0] <= '9')))
235 // Reparse stringized version!
236 if (atof(StrVal.c_str()) == CFP->getValue()) {
237 Out << StrVal; return;
238 }
239
240 // Otherwise we could not reparse it to exactly the same value, so we must
241 // output the string in hexadecimal format!
242 //
243 // Behave nicely in the face of C TBAA rules... see:
244 // http://www.nullstone.com/htmls/category/aliastyp.htm
245 //
246 double Val = CFP->getValue();
247 char *Ptr = (char*)&Val;
248 assert(sizeof(double) == sizeof(uint64_t) && sizeof(double) == 8 &&
249 "assuming that double is 64 bits!");
250 Out << "0x" << utohexstr(*(uint64_t*)Ptr);
251
252 } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
253 // As a special case, print the array as a string if it is an array of
254 // ubytes or an array of sbytes with positive values.
255 //
256 const Type *ETy = CA->getType()->getElementType();
257 bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
258
259 if (ETy == Type::SByteTy)
260 for (unsigned i = 0; i < CA->getNumOperands(); ++i)
261 if (cast<ConstantSInt>(CA->getOperand(i))->getValue() < 0) {
262 isString = false;
263 break;
264 }
265
266 if (isString) {
267 Out << "c\"";
268 for (unsigned i = 0; i < CA->getNumOperands(); ++i) {
269 unsigned char C = (ETy == Type::SByteTy) ?
270 (unsigned char)cast<ConstantSInt>(CA->getOperand(i))->getValue() :
271 (unsigned char)cast<ConstantUInt>(CA->getOperand(i))->getValue();
272
Chris Lattnere5fd3862002-07-31 23:56:44 +0000273 if (isprint(C) && C != '"' && C != '\\') {
Chris Lattner1e194682002-04-18 18:53:13 +0000274 Out << C;
275 } else {
276 Out << '\\'
277 << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'))
278 << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
279 }
280 }
281 Out << "\"";
282
283 } else { // Cannot output in string format...
Chris Lattnerd84bb632002-04-16 21:36:08 +0000284 Out << "[";
285 if (CA->getNumOperands()) {
286 Out << " ";
Chris Lattner1e194682002-04-18 18:53:13 +0000287 printTypeInt(Out, ETy, TypeTable);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000288 WriteAsOperandInternal(Out, CA->getOperand(0),
289 PrintName, TypeTable, Table);
290 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
291 Out << ", ";
Chris Lattner1e194682002-04-18 18:53:13 +0000292 printTypeInt(Out, ETy, TypeTable);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000293 WriteAsOperandInternal(Out, CA->getOperand(i), PrintName,
294 TypeTable, Table);
295 }
296 }
297 Out << " ]";
298 }
299 } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
300 Out << "{";
301 if (CS->getNumOperands()) {
302 Out << " ";
303 printTypeInt(Out, CS->getOperand(0)->getType(), TypeTable);
304
305 WriteAsOperandInternal(Out, CS->getOperand(0),
306 PrintName, TypeTable, Table);
307
308 for (unsigned i = 1; i < CS->getNumOperands(); i++) {
309 Out << ", ";
310 printTypeInt(Out, CS->getOperand(i)->getType(), TypeTable);
311
312 WriteAsOperandInternal(Out, CS->getOperand(i),
313 PrintName, TypeTable, Table);
314 }
315 }
316
317 Out << " }";
318 } else if (isa<ConstantPointerNull>(CV)) {
319 Out << "null";
320
Chris Lattner113f4f42002-06-25 16:13:24 +0000321 } else if (const ConstantPointerRef *PR = dyn_cast<ConstantPointerRef>(CV)) {
Chris Lattner1e194682002-04-18 18:53:13 +0000322 const GlobalValue *V = PR->getValue();
323 if (V->hasName()) {
324 Out << "%" << V->getName();
325 } else if (Table) {
326 int Slot = Table->getValSlot(V);
327 if (Slot >= 0)
328 Out << "%" << Slot;
329 else
330 Out << "<pointer reference badref>";
331 } else {
332 Out << "<pointer reference without context info>";
333 }
Vikram S. Adveb952b542002-07-14 23:14:45 +0000334
Chris Lattner3cd8c562002-07-30 18:54:25 +0000335 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Chris Lattnerb8257222003-03-06 23:23:32 +0000336 Out << CE->getOpcodeName() << " (";
Vikram S. Adveb952b542002-07-14 23:14:45 +0000337
338 for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
339 printTypeInt(Out, (*OI)->getType(), TypeTable);
340 WriteAsOperandInternal(Out, *OI, PrintName, TypeTable, Table);
341 if (OI+1 != CE->op_end())
Chris Lattner3cd8c562002-07-30 18:54:25 +0000342 Out << ", ";
Vikram S. Adveb952b542002-07-14 23:14:45 +0000343 }
344
Chris Lattnercfe8f532002-08-16 21:17:11 +0000345 if (CE->getOpcode() == Instruction::Cast) {
Chris Lattner83b396b2002-08-15 19:37:43 +0000346 Out << " to ";
347 printTypeInt(Out, CE->getType(), TypeTable);
348 }
Chris Lattnercfe8f532002-08-16 21:17:11 +0000349 Out << ")";
Chris Lattner83b396b2002-08-15 19:37:43 +0000350
Chris Lattnerd84bb632002-04-16 21:36:08 +0000351 } else {
Vikram S. Adveb952b542002-07-14 23:14:45 +0000352 Out << "<placeholder or erroneous Constant>";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000353 }
354}
355
356
357// WriteAsOperand - Write the name of the specified value out to the specified
358// ostream. This can be useful when you just want to print int %reg126, not the
359// whole instruction that generated it.
360//
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000361static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
362 bool PrintName,
363 std::map<const Type*, std::string> &TypeTable,
Chris Lattnerd84bb632002-04-16 21:36:08 +0000364 SlotCalculator *Table) {
365 Out << " ";
366 if (PrintName && V->hasName()) {
367 Out << "%" << V->getName();
368 } else {
369 if (const Constant *CV = dyn_cast<const Constant>(V)) {
370 WriteConstantInt(Out, CV, PrintName, TypeTable, Table);
371 } else {
372 int Slot;
373 if (Table) {
374 Slot = Table->getValSlot(V);
375 } else {
376 if (const Type *Ty = dyn_cast<const Type>(V)) {
377 Out << Ty->getDescription();
378 return;
379 }
380
381 Table = createSlotCalculator(V);
382 if (Table == 0) { Out << "BAD VALUE TYPE!"; return; }
383
384 Slot = Table->getValSlot(V);
385 delete Table;
386 }
387 if (Slot >= 0) Out << "%" << Slot;
388 else if (PrintName)
389 Out << "<badref>"; // Not embeded into a location?
390 }
391 }
392}
393
394
Chris Lattnerb86620e2001-10-29 16:37:48 +0000395
396// WriteAsOperand - Write the name of the specified value out to the specified
397// ostream. This can be useful when you just want to print int %reg126, not the
398// whole instruction that generated it.
399//
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000400std::ostream &WriteAsOperand(std::ostream &Out, const Value *V, bool PrintType,
401 bool PrintName, const Module *Context) {
402 std::map<const Type *, std::string> TypeNames;
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000403 if (Context == 0) Context = getModuleFromVal(V);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000404
Chris Lattner98cf1f52002-11-20 18:36:02 +0000405 if (Context)
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000406 fillTypeNameTable(Context, TypeNames);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000407
408 if (PrintType)
409 printTypeInt(Out, V->getType(), TypeNames);
410
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000411 WriteAsOperandInternal(Out, V, PrintName, TypeNames, 0);
Chris Lattner5e5abe32001-07-20 19:15:21 +0000412 return Out;
413}
414
415
Chris Lattner2e9fee42001-07-12 23:35:26 +0000416
Chris Lattnerfee714f2001-09-07 16:36:04 +0000417class AssemblyWriter {
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000418 std::ostream &Out;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000419 SlotCalculator &Table;
Chris Lattner7bfee412001-10-29 16:05:51 +0000420 const Module *TheModule;
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000421 std::map<const Type *, std::string> TypeNames;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000422public:
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000423 inline AssemblyWriter(std::ostream &o, SlotCalculator &Tab, const Module *M)
Chris Lattner7bfee412001-10-29 16:05:51 +0000424 : Out(o), Table(Tab), TheModule(M) {
425
426 // If the module has a symbol table, take all global types and stuff their
427 // names into the TypeNames map.
428 //
Chris Lattnerb86620e2001-10-29 16:37:48 +0000429 fillTypeNameTable(M, TypeNames);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000430 }
431
Chris Lattner7bfee412001-10-29 16:05:51 +0000432 inline void write(const Module *M) { printModule(M); }
433 inline void write(const GlobalVariable *G) { printGlobal(G); }
Chris Lattner57698e22002-03-26 18:01:55 +0000434 inline void write(const Function *F) { printFunction(F); }
Chris Lattner7bfee412001-10-29 16:05:51 +0000435 inline void write(const BasicBlock *BB) { printBasicBlock(BB); }
Chris Lattner113f4f42002-06-25 16:13:24 +0000436 inline void write(const Instruction *I) { printInstruction(*I); }
Chris Lattner3462ae32001-12-03 22:26:30 +0000437 inline void write(const Constant *CPV) { printConstant(CPV); }
Chris Lattner7db79582001-11-07 04:21:57 +0000438 inline void write(const Type *Ty) { printType(Ty); }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000439
Chris Lattner1e194682002-04-18 18:53:13 +0000440 void writeOperand(const Value *Op, bool PrintType, bool PrintName = true);
441
Chris Lattner2f7c9632001-06-06 20:29:01 +0000442private :
Chris Lattner7bfee412001-10-29 16:05:51 +0000443 void printModule(const Module *M);
444 void printSymbolTable(const SymbolTable &ST);
Chris Lattner3462ae32001-12-03 22:26:30 +0000445 void printConstant(const Constant *CPV);
Chris Lattner7bfee412001-10-29 16:05:51 +0000446 void printGlobal(const GlobalVariable *GV);
Chris Lattner57698e22002-03-26 18:01:55 +0000447 void printFunction(const Function *F);
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000448 void printArgument(const Argument *FA);
Chris Lattner7bfee412001-10-29 16:05:51 +0000449 void printBasicBlock(const BasicBlock *BB);
Chris Lattner113f4f42002-06-25 16:13:24 +0000450 void printInstruction(const Instruction &I);
Chris Lattnerd816b532002-04-13 20:53:41 +0000451
452 // printType - Go to extreme measures to attempt to print out a short,
453 // symbolic version of a type name.
454 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000455 std::ostream &printType(const Type *Ty) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000456 return printTypeInt(Out, Ty, TypeNames);
457 }
458
459 // printTypeAtLeastOneLevel - Print out one level of the possibly complex type
460 // without considering any symbolic types that we may have equal to it.
461 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000462 std::ostream &printTypeAtLeastOneLevel(const Type *Ty);
Chris Lattner7bfee412001-10-29 16:05:51 +0000463
Chris Lattner862e3382001-10-13 06:42:36 +0000464 // printInfoComment - Print a little comment after the instruction indicating
465 // which slot it occupies.
Chris Lattner113f4f42002-06-25 16:13:24 +0000466 void printInfoComment(const Value &V);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000467};
468
469
Chris Lattnerd816b532002-04-13 20:53:41 +0000470// printTypeAtLeastOneLevel - Print out one level of the possibly complex type
471// without considering any symbolic types that we may have equal to it.
472//
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000473std::ostream &AssemblyWriter::printTypeAtLeastOneLevel(const Type *Ty) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000474 if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000475 printType(FTy->getReturnType()) << " (";
476 for (FunctionType::ParamTypes::const_iterator
477 I = FTy->getParamTypes().begin(),
478 E = FTy->getParamTypes().end(); I != E; ++I) {
479 if (I != FTy->getParamTypes().begin())
480 Out << ", ";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000481 printType(*I);
Chris Lattnerd816b532002-04-13 20:53:41 +0000482 }
483 if (FTy->isVarArg()) {
484 if (!FTy->getParamTypes().empty()) Out << ", ";
485 Out << "...";
486 }
487 Out << ")";
Chris Lattner113f4f42002-06-25 16:13:24 +0000488 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000489 Out << "{ ";
490 for (StructType::ElementTypes::const_iterator
491 I = STy->getElementTypes().begin(),
492 E = STy->getElementTypes().end(); I != E; ++I) {
493 if (I != STy->getElementTypes().begin())
494 Out << ", ";
495 printType(*I);
496 }
497 Out << " }";
Chris Lattner113f4f42002-06-25 16:13:24 +0000498 } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000499 printType(PTy->getElementType()) << "*";
Chris Lattner113f4f42002-06-25 16:13:24 +0000500 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000501 Out << "[" << ATy->getNumElements() << " x ";
502 printType(ATy->getElementType()) << "]";
Chris Lattner113f4f42002-06-25 16:13:24 +0000503 } else if (const OpaqueType *OTy = dyn_cast<OpaqueType>(Ty)) {
Chris Lattner2904f442002-05-26 20:17:54 +0000504 Out << OTy->getDescription();
Chris Lattnerd816b532002-04-13 20:53:41 +0000505 } else {
Vikram S. Adveb952b542002-07-14 23:14:45 +0000506 if (!Ty->isPrimitiveType())
507 Out << "<unknown derived type>";
Chris Lattnerd816b532002-04-13 20:53:41 +0000508 printType(Ty);
509 }
510 return Out;
511}
512
513
Chris Lattnerfee714f2001-09-07 16:36:04 +0000514void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType,
515 bool PrintName) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000516 if (PrintType) { Out << " "; printType(Operand->getType()); }
Chris Lattnerd84bb632002-04-16 21:36:08 +0000517 WriteAsOperandInternal(Out, Operand, PrintName, TypeNames, &Table);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000518}
519
Chris Lattner2f7c9632001-06-06 20:29:01 +0000520
Chris Lattner7bfee412001-10-29 16:05:51 +0000521void AssemblyWriter::printModule(const Module *M) {
Chris Lattnerc840aa62003-04-22 19:07:19 +0000522 Out << "target endian = " << (M->isLittleEndian() ? "little" : "big") << "\n";
523 Out << "target pointersize = " << (M->has32BitPointers() ? 32 : 64) << "\n";
524
Chris Lattnerfee714f2001-09-07 16:36:04 +0000525 // Loop over the symbol table, emitting all named constants...
Chris Lattner98cf1f52002-11-20 18:36:02 +0000526 printSymbolTable(M->getSymbolTable());
Chris Lattnerda975502001-09-10 07:58:01 +0000527
Chris Lattner113f4f42002-06-25 16:13:24 +0000528 for (Module::const_giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
529 printGlobal(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000530
Chris Lattnerb2f02e52002-05-06 03:00:40 +0000531 Out << "\nimplementation ; Functions:\n";
Vikram S. Adve13ba19a2001-09-18 12:48:16 +0000532
Chris Lattner6915f8f2002-04-07 22:49:37 +0000533 // Output all of the functions...
Chris Lattner113f4f42002-06-25 16:13:24 +0000534 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
535 printFunction(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000536}
537
Chris Lattner7bfee412001-10-29 16:05:51 +0000538void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Chris Lattnerda975502001-09-10 07:58:01 +0000539 if (GV->hasName()) Out << "%" << GV->getName() << " = ";
Chris Lattner37798642001-09-18 04:01:05 +0000540
Chris Lattner379a8d22003-04-16 20:28:45 +0000541 if (!GV->hasInitializer())
542 Out << "external ";
543 else
544 switch (GV->getLinkage()) {
545 case GlobalValue::InternalLinkage: Out << "internal "; break;
546 case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
547 case GlobalValue::AppendingLinkage: Out << "appending "; break;
548 case GlobalValue::ExternalLinkage: break;
549 }
Chris Lattner37798642001-09-18 04:01:05 +0000550
Chris Lattner7bfee412001-10-29 16:05:51 +0000551 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattner2413b162001-12-04 00:03:30 +0000552 printType(GV->getType()->getElementType());
Chris Lattner37798642001-09-18 04:01:05 +0000553
554 if (GV->hasInitializer())
555 writeOperand(GV->getInitializer(), false, false);
556
Chris Lattner113f4f42002-06-25 16:13:24 +0000557 printInfoComment(*GV);
Chris Lattner7f74a562002-01-20 22:54:45 +0000558 Out << "\n";
Chris Lattnerda975502001-09-10 07:58:01 +0000559}
560
Chris Lattnerfee714f2001-09-07 16:36:04 +0000561
Chris Lattner7bfee412001-10-29 16:05:51 +0000562// printSymbolTable - Run through symbol table looking for named constants
Chris Lattnerfee714f2001-09-07 16:36:04 +0000563// if a named constant is found, emit it's declaration...
564//
Chris Lattner7bfee412001-10-29 16:05:51 +0000565void AssemblyWriter::printSymbolTable(const SymbolTable &ST) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000566 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
567 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
568 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
569
570 for (; I != End; ++I) {
571 const Value *V = I->second;
Chris Lattner3462ae32001-12-03 22:26:30 +0000572 if (const Constant *CPV = dyn_cast<const Constant>(V)) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000573 printConstant(CPV);
Chris Lattner38569342001-10-01 20:11:19 +0000574 } else if (const Type *Ty = dyn_cast<const Type>(V)) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000575 Out << "\t%" << I->first << " = type ";
576
577 // Make sure we print out at least one level of the type structure, so
578 // that we do not get %FILE = type %FILE
579 //
580 printTypeAtLeastOneLevel(Ty) << "\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000581 }
582 }
Chris Lattnera7620d92001-07-15 06:35:59 +0000583 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000584}
585
586
Chris Lattner7bfee412001-10-29 16:05:51 +0000587// printConstant - Print out a constant pool entry...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000588//
Chris Lattner3462ae32001-12-03 22:26:30 +0000589void AssemblyWriter::printConstant(const Constant *CPV) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000590 // Don't print out unnamed constants, they will be inlined
591 if (!CPV->hasName()) return;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000592
Chris Lattneree998be2001-07-26 16:29:38 +0000593 // Print out name...
Chris Lattnerd84bb632002-04-16 21:36:08 +0000594 Out << "\t%" << CPV->getName() << " =";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000595
596 // Write the value out now...
Chris Lattnerd84bb632002-04-16 21:36:08 +0000597 writeOperand(CPV, true, false);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000598
Chris Lattner113f4f42002-06-25 16:13:24 +0000599 printInfoComment(*CPV);
Chris Lattner7f74a562002-01-20 22:54:45 +0000600 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000601}
602
Chris Lattner6915f8f2002-04-07 22:49:37 +0000603// printFunction - Print all aspects of a function.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000604//
Chris Lattner113f4f42002-06-25 16:13:24 +0000605void AssemblyWriter::printFunction(const Function *F) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000606 // Print out the return type and name...
Chris Lattner379a8d22003-04-16 20:28:45 +0000607 Out << "\n";
608
609 if (F->isExternal())
610 Out << "declare ";
611 else
612 switch (F->getLinkage()) {
613 case GlobalValue::InternalLinkage: Out << "internal "; break;
614 case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
615 case GlobalValue::AppendingLinkage: Out << "appending "; break;
616 case GlobalValue::ExternalLinkage: break;
617 }
618
Chris Lattner113f4f42002-06-25 16:13:24 +0000619 printType(F->getReturnType()) << " %" << F->getName() << "(";
620 Table.incorporateFunction(F);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000621
Chris Lattner7bfee412001-10-29 16:05:51 +0000622 // Loop over the arguments, printing them...
Chris Lattner113f4f42002-06-25 16:13:24 +0000623 const FunctionType *FT = F->getFunctionType();
Chris Lattnerfee714f2001-09-07 16:36:04 +0000624
Chris Lattner149376d2002-10-13 20:57:00 +0000625 for(Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I)
626 printArgument(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000627
628 // Finish printing arguments...
Chris Lattner113f4f42002-06-25 16:13:24 +0000629 if (FT->isVarArg()) {
630 if (FT->getParamTypes().size()) Out << ", ";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000631 Out << "..."; // Output varargs portion of signature!
632 }
Chris Lattnerb2f02e52002-05-06 03:00:40 +0000633 Out << ")";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000634
Chris Lattner113f4f42002-06-25 16:13:24 +0000635 if (F->isExternal()) {
Chris Lattnerb2f02e52002-05-06 03:00:40 +0000636 Out << "\n";
637 } else {
638 Out << " {";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000639
Chris Lattner6915f8f2002-04-07 22:49:37 +0000640 // Output all of its basic blocks... for the function
Chris Lattner113f4f42002-06-25 16:13:24 +0000641 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
642 printBasicBlock(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000643
Chris Lattnerb2f02e52002-05-06 03:00:40 +0000644 Out << "}\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000645 }
646
Chris Lattner6915f8f2002-04-07 22:49:37 +0000647 Table.purgeFunction();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000648}
649
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000650// printArgument - This member is called for every argument that
Chris Lattner6915f8f2002-04-07 22:49:37 +0000651// is passed into the function. Simply print it out
Chris Lattner2f7c9632001-06-06 20:29:01 +0000652//
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000653void AssemblyWriter::printArgument(const Argument *Arg) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000654 // Insert commas as we go... the first arg doesn't get a comma
Chris Lattner113f4f42002-06-25 16:13:24 +0000655 if (Arg != &Arg->getParent()->afront()) Out << ", ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000656
657 // Output type...
Chris Lattner7bfee412001-10-29 16:05:51 +0000658 printType(Arg->getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000659
660 // Output name, if available...
661 if (Arg->hasName())
662 Out << " %" << Arg->getName();
663 else if (Table.getValSlot(Arg) < 0)
664 Out << "<badref>";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000665}
666
Chris Lattner7bfee412001-10-29 16:05:51 +0000667// printBasicBlock - This member is called for each basic block in a methd.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000668//
Chris Lattner7bfee412001-10-29 16:05:51 +0000669void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000670 if (BB->hasName()) { // Print out the label if it exists...
Chris Lattner58185f22002-10-02 19:38:55 +0000671 Out << "\n" << BB->getName() << ":";
Chris Lattner408dbdb2002-05-14 16:02:05 +0000672 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000673 int Slot = Table.getValSlot(BB);
Chris Lattnera2f01872001-06-07 16:58:55 +0000674 Out << "\n; <label>:";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000675 if (Slot >= 0)
Chris Lattnera2f01872001-06-07 16:58:55 +0000676 Out << Slot; // Extra newline seperates out label's
Chris Lattner2f7c9632001-06-06 20:29:01 +0000677 else
Chris Lattnera2f01872001-06-07 16:58:55 +0000678 Out << "<badref>";
Chris Lattner58185f22002-10-02 19:38:55 +0000679 }
680
681 // Output predecessors for the block...
682 Out << "\t\t;";
683 pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
684
685 if (PI == PE) {
686 Out << " No predecessors!";
687 } else {
688 Out << " preds =";
689 writeOperand(*PI, false, true);
690 for (++PI; PI != PE; ++PI) {
691 Out << ",";
692 writeOperand(*PI, false, true);
693 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000694 }
Chris Lattner408dbdb2002-05-14 16:02:05 +0000695
696 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000697
Chris Lattnerfee714f2001-09-07 16:36:04 +0000698 // Output all of the instructions in the basic block...
Chris Lattner113f4f42002-06-25 16:13:24 +0000699 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
700 printInstruction(*I);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000701}
702
Chris Lattner862e3382001-10-13 06:42:36 +0000703
704// printInfoComment - Print a little comment after the instruction indicating
705// which slot it occupies.
706//
Chris Lattner113f4f42002-06-25 16:13:24 +0000707void AssemblyWriter::printInfoComment(const Value &V) {
708 if (V.getType() != Type::VoidTy) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000709 Out << "\t\t; <";
Chris Lattner113f4f42002-06-25 16:13:24 +0000710 printType(V.getType()) << ">";
Chris Lattner862e3382001-10-13 06:42:36 +0000711
Chris Lattner113f4f42002-06-25 16:13:24 +0000712 if (!V.hasName()) {
713 int Slot = Table.getValSlot(&V); // Print out the def slot taken...
Chris Lattner862e3382001-10-13 06:42:36 +0000714 if (Slot >= 0) Out << ":" << Slot;
715 else Out << ":<badref>";
716 }
Chris Lattner113f4f42002-06-25 16:13:24 +0000717 Out << " [#uses=" << V.use_size() << "]"; // Output # uses
Chris Lattner862e3382001-10-13 06:42:36 +0000718 }
719}
720
Chris Lattner7bfee412001-10-29 16:05:51 +0000721// printInstruction - This member is called for each Instruction in a methd.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000722//
Chris Lattner113f4f42002-06-25 16:13:24 +0000723void AssemblyWriter::printInstruction(const Instruction &I) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000724 Out << "\t";
725
726 // Print out name if it exists...
Chris Lattner113f4f42002-06-25 16:13:24 +0000727 if (I.hasName())
728 Out << "%" << I.getName() << " = ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000729
730 // Print out the opcode...
Chris Lattner113f4f42002-06-25 16:13:24 +0000731 Out << I.getOpcodeName();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000732
733 // Print out the type of the operands...
Chris Lattner113f4f42002-06-25 16:13:24 +0000734 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000735
736 // Special case conditional branches to swizzle the condition out to the front
Chris Lattner113f4f42002-06-25 16:13:24 +0000737 if (isa<BranchInst>(I) && I.getNumOperands() > 1) {
738 writeOperand(I.getOperand(2), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000739 Out << ",";
740 writeOperand(Operand, true);
741 Out << ",";
Chris Lattner113f4f42002-06-25 16:13:24 +0000742 writeOperand(I.getOperand(1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000743
Chris Lattner8d48df22002-04-13 18:34:38 +0000744 } else if (isa<SwitchInst>(I)) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000745 // Special case switch statement to get formatting nice and correct...
Chris Lattner113f4f42002-06-25 16:13:24 +0000746 writeOperand(Operand , true); Out << ",";
747 writeOperand(I.getOperand(1), true); Out << " [";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000748
Chris Lattner113f4f42002-06-25 16:13:24 +0000749 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; op += 2) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000750 Out << "\n\t\t";
Chris Lattner113f4f42002-06-25 16:13:24 +0000751 writeOperand(I.getOperand(op ), true); Out << ",";
752 writeOperand(I.getOperand(op+1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000753 }
754 Out << "\n\t]";
Chris Lattnerda558102001-10-02 03:41:24 +0000755 } else if (isa<PHINode>(I)) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000756 Out << " ";
Chris Lattner113f4f42002-06-25 16:13:24 +0000757 printType(I.getType());
Chris Lattner9d3292d2001-11-06 08:33:46 +0000758 Out << " ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000759
Chris Lattner113f4f42002-06-25 16:13:24 +0000760 for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) {
Chris Lattner29644b32001-11-06 01:48:45 +0000761 if (op) Out << ", ";
762 Out << "[";
Chris Lattner113f4f42002-06-25 16:13:24 +0000763 writeOperand(I.getOperand(op ), false); Out << ",";
764 writeOperand(I.getOperand(op+1), false); Out << " ]";
Chris Lattner931ef3b2001-06-11 15:04:20 +0000765 }
Chris Lattner862e3382001-10-13 06:42:36 +0000766 } else if (isa<ReturnInst>(I) && !Operand) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000767 Out << " void";
Chris Lattner862e3382001-10-13 06:42:36 +0000768 } else if (isa<CallInst>(I)) {
Chris Lattner2f2d9472001-11-06 21:28:12 +0000769 const PointerType *PTy = dyn_cast<PointerType>(Operand->getType());
Chris Lattner91db5822002-03-29 03:44:36 +0000770 const FunctionType*MTy = PTy ? dyn_cast<FunctionType>(PTy->getElementType()):0;
Chris Lattner2f2d9472001-11-06 21:28:12 +0000771 const Type *RetTy = MTy ? MTy->getReturnType() : 0;
772
773 // If possible, print out the short form of the call instruction, but we can
Chris Lattner6915f8f2002-04-07 22:49:37 +0000774 // only do this if the first argument is a pointer to a nonvararg function,
775 // and if the value returned is not a pointer to a function.
Chris Lattner2f2d9472001-11-06 21:28:12 +0000776 //
Chris Lattner8d48df22002-04-13 18:34:38 +0000777 if (RetTy && MTy && !MTy->isVarArg() &&
778 (!isa<PointerType>(RetTy) ||
Chris Lattnerd9a36a62002-07-25 20:58:51 +0000779 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Chris Lattner2f2d9472001-11-06 21:28:12 +0000780 Out << " "; printType(RetTy);
781 writeOperand(Operand, false);
782 } else {
783 writeOperand(Operand, true);
784 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000785 Out << "(";
Chris Lattner113f4f42002-06-25 16:13:24 +0000786 if (I.getNumOperands() > 1) writeOperand(I.getOperand(1), true);
787 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; ++op) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000788 Out << ",";
Chris Lattner113f4f42002-06-25 16:13:24 +0000789 writeOperand(I.getOperand(op), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000790 }
791
792 Out << " )";
Chris Lattner113f4f42002-06-25 16:13:24 +0000793 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Chris Lattner862e3382001-10-13 06:42:36 +0000794 // TODO: Should try to print out short form of the Invoke instruction
795 writeOperand(Operand, true);
796 Out << "(";
Chris Lattner113f4f42002-06-25 16:13:24 +0000797 if (I.getNumOperands() > 3) writeOperand(I.getOperand(3), true);
798 for (unsigned op = 4, Eop = I.getNumOperands(); op < Eop; ++op) {
Chris Lattner862e3382001-10-13 06:42:36 +0000799 Out << ",";
Chris Lattner113f4f42002-06-25 16:13:24 +0000800 writeOperand(I.getOperand(op), true);
Chris Lattner862e3382001-10-13 06:42:36 +0000801 }
802
803 Out << " )\n\t\t\tto";
804 writeOperand(II->getNormalDest(), true);
805 Out << " except";
806 writeOperand(II->getExceptionalDest(), true);
807
Chris Lattner113f4f42002-06-25 16:13:24 +0000808 } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(&I)) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000809 Out << " ";
Chris Lattner8d48df22002-04-13 18:34:38 +0000810 printType(AI->getType()->getElementType());
811 if (AI->isArrayAllocation()) {
Chris Lattnera073acb2001-07-07 08:36:50 +0000812 Out << ",";
Chris Lattner8d48df22002-04-13 18:34:38 +0000813 writeOperand(AI->getArraySize(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000814 }
Chris Lattner862e3382001-10-13 06:42:36 +0000815 } else if (isa<CastInst>(I)) {
Chris Lattnerf70da102003-05-08 02:44:12 +0000816 writeOperand(Operand, true);
Chris Lattner7bfee412001-10-29 16:05:51 +0000817 Out << " to ";
Chris Lattner113f4f42002-06-25 16:13:24 +0000818 printType(I.getType());
Chris Lattnerf70da102003-05-08 02:44:12 +0000819 } else if (isa<VarArgInst>(I)) {
820 writeOperand(Operand, true);
821 Out << ", ";
822 printType(I.getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000823 } else if (Operand) { // Print the normal way...
824
825 // PrintAllTypes - Instructions who have operands of all the same type
826 // omit the type from all but the first operand. If the instruction has
827 // different type operands (for example br), then they are all printed.
828 bool PrintAllTypes = false;
829 const Type *TheType = Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000830
Chris Lattnerdeccfaf2003-04-16 20:20:02 +0000831 // Shift Left & Right print both types even for Ubyte LHS
832 if (isa<ShiftInst>(I)) {
833 PrintAllTypes = true;
834 } else {
835 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
836 Operand = I.getOperand(i);
837 if (Operand->getType() != TheType) {
838 PrintAllTypes = true; // We have differing types! Print them all!
839 break;
840 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000841 }
842 }
Chris Lattnerdeccfaf2003-04-16 20:20:02 +0000843
Chris Lattner7bfee412001-10-29 16:05:51 +0000844 if (!PrintAllTypes) {
845 Out << " ";
Chris Lattnerdeccfaf2003-04-16 20:20:02 +0000846 printType(TheType);
Chris Lattner7bfee412001-10-29 16:05:51 +0000847 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000848
Chris Lattner113f4f42002-06-25 16:13:24 +0000849 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000850 if (i) Out << ",";
Chris Lattner113f4f42002-06-25 16:13:24 +0000851 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000852 }
853 }
854
Chris Lattner862e3382001-10-13 06:42:36 +0000855 printInfoComment(I);
Chris Lattner7f74a562002-01-20 22:54:45 +0000856 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000857}
858
859
860//===----------------------------------------------------------------------===//
861// External Interface declarations
862//===----------------------------------------------------------------------===//
863
864
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000865void Module::print(std::ostream &o) const {
866 SlotCalculator SlotTable(this, true);
867 AssemblyWriter W(o, SlotTable, this);
868 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000869}
870
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000871void GlobalVariable::print(std::ostream &o) const {
872 SlotCalculator SlotTable(getParent(), true);
873 AssemblyWriter W(o, SlotTable, getParent());
874 W.write(this);
Chris Lattneradfe0d12001-09-10 20:08:19 +0000875}
876
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000877void Function::print(std::ostream &o) const {
878 SlotCalculator SlotTable(getParent(), true);
879 AssemblyWriter W(o, SlotTable, getParent());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000880
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000881 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000882}
883
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000884void BasicBlock::print(std::ostream &o) const {
885 SlotCalculator SlotTable(getParent(), true);
Chris Lattner7bfee412001-10-29 16:05:51 +0000886 AssemblyWriter W(o, SlotTable,
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000887 getParent() ? getParent()->getParent() : 0);
888 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000889}
890
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000891void Instruction::print(std::ostream &o) const {
892 const Function *F = getParent() ? getParent()->getParent() : 0;
Chris Lattner57698e22002-03-26 18:01:55 +0000893 SlotCalculator SlotTable(F, true);
894 AssemblyWriter W(o, SlotTable, F ? F->getParent() : 0);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000895
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000896 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000897}
Chris Lattner7db79582001-11-07 04:21:57 +0000898
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000899void Constant::print(std::ostream &o) const {
900 if (this == 0) { o << "<null> constant value\n"; return; }
Chris Lattner7d734802002-09-10 15:53:49 +0000901
902 // Handle CPR's special, because they have context information...
903 if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(this)) {
904 CPR->getValue()->print(o); // Print as a global value, with context info.
905 return;
906 }
907
Chris Lattner1e194682002-04-18 18:53:13 +0000908 o << " " << getType()->getDescription() << " ";
909
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000910 std::map<const Type *, std::string> TypeTable;
Chris Lattner1e194682002-04-18 18:53:13 +0000911 WriteConstantInt(o, this, false, TypeTable, 0);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000912}
913
914void Type::print(std::ostream &o) const {
915 if (this == 0)
916 o << "<null Type>";
917 else
918 o << getDescription();
919}
920
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000921void Argument::print(std::ostream &o) const {
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000922 o << getType() << " " << getName();
923}
924
925void Value::dump() const { print(std::cerr); }
926
927//===----------------------------------------------------------------------===//
928// CachedWriter Class Implementation
929//===----------------------------------------------------------------------===//
930
Chris Lattner7db79582001-11-07 04:21:57 +0000931void CachedWriter::setModule(const Module *M) {
932 delete SC; delete AW;
933 if (M) {
934 SC = new SlotCalculator(M, true);
935 AW = new AssemblyWriter(Out, *SC, M);
936 } else {
937 SC = 0; AW = 0;
938 }
939}
940
941CachedWriter::~CachedWriter() {
942 delete AW;
943 delete SC;
944}
945
946CachedWriter &CachedWriter::operator<<(const Value *V) {
947 assert(AW && SC && "CachedWriter does not have a current module!");
948 switch (V->getValueType()) {
949 case Value::ConstantVal:
Chris Lattner1e194682002-04-18 18:53:13 +0000950 case Value::ArgumentVal: AW->writeOperand(V, true, true); break;
Chris Lattner7db79582001-11-07 04:21:57 +0000951 case Value::TypeVal: AW->write(cast<const Type>(V)); break;
952 case Value::InstructionVal: AW->write(cast<Instruction>(V)); break;
953 case Value::BasicBlockVal: AW->write(cast<BasicBlock>(V)); break;
Chris Lattner57698e22002-03-26 18:01:55 +0000954 case Value::FunctionVal: AW->write(cast<Function>(V)); break;
Chris Lattner7db79582001-11-07 04:21:57 +0000955 case Value::GlobalVariableVal: AW->write(cast<GlobalVariable>(V)); break;
Chris Lattner7db79582001-11-07 04:21:57 +0000956 default: Out << "<unknown value type: " << V->getValueType() << ">"; break;
957 }
958 return *this;
959}