blob: f007c7645cc017dd1a2e775d5dc929be2ec4259e [file] [log] [blame]
Chris Lattner8da78af2002-04-07 22:31:46 +00001//===-- AsmWriter.cpp - Printing LLVM as an assembly file -----------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
10// This library implements the functionality defined in llvm/Assembly/Writer.h
11//
Chris Lattner02b93992002-04-12 18:21:53 +000012// Note that these routines must be extremely tolerant of various errors in the
Chris Lattner8f77dae2003-05-08 02:44:12 +000013// LLVM code, because it can be used for debugging transformations.
Chris Lattner02b93992002-04-12 18:21:53 +000014//
Chris Lattner00950542001-06-06 20:29:01 +000015//===----------------------------------------------------------------------===//
16
Chris Lattner75cf7cf2002-04-08 22:03:40 +000017#include "llvm/Assembly/Writer.h"
Chris Lattnerf082b802002-07-23 18:07:49 +000018#include "llvm/Assembly/PrintModulePass.h"
Chris Lattner95e5a2c2003-10-30 23:41:03 +000019#include "llvm/Assembly/AsmAnnotationWriter.h"
Chris Lattnerd5118982005-05-06 20:26:43 +000020#include "llvm/CallingConv.h"
Chris Lattnerf2d577b2004-01-20 19:50:34 +000021#include "llvm/Constants.h"
Chris Lattner3eb59c02002-04-29 18:46:50 +000022#include "llvm/DerivedTypes.h"
Chris Lattner863517a2006-01-25 18:57:27 +000023#include "llvm/InlineAsm.h"
Vikram S. Adveb4dbb442002-07-14 23:14:45 +000024#include "llvm/Instruction.h"
Misha Brukman44336292004-07-29 16:53:53 +000025#include "llvm/Instructions.h"
Chris Lattnerf2d577b2004-01-20 19:50:34 +000026#include "llvm/Module.h"
Reid Spenceref9b9a72007-02-05 20:47:22 +000027#include "llvm/ValueSymbolTable.h"
Reid Spencer78d033e2007-01-06 07:24:44 +000028#include "llvm/TypeSymbolTable.h"
Chris Lattner413fd232009-03-01 00:03:38 +000029#include "llvm/ADT/DenseSet.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000030#include "llvm/ADT/StringExtras.h"
31#include "llvm/ADT/STLExtras.h"
Bill Wendling8f487662006-11-28 02:09:03 +000032#include "llvm/Support/CFG.h"
Jim Laskeycb6682f2005-08-17 19:34:49 +000033#include "llvm/Support/MathExtras.h"
Chris Lattner45d4c732008-08-17 04:17:45 +000034#include "llvm/Support/raw_ostream.h"
Chris Lattner007377f2001-09-07 16:36:04 +000035#include <algorithm>
Reid Spencer4ad513c2007-05-22 19:27:35 +000036#include <cctype>
Chris Lattner31f84992003-11-21 20:23:48 +000037using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000038
Reid Spenceredd5d9e2005-05-15 16:13:11 +000039// Make virtual table appear in this compilation unit.
40AssemblyAnnotationWriter::~AssemblyAnnotationWriter() {}
41
Chris Lattner6ab910b2008-08-19 04:36:02 +000042//===----------------------------------------------------------------------===//
43// Helper Functions
44//===----------------------------------------------------------------------===//
45
46static const Module *getModuleFromVal(const Value *V) {
47 if (const Argument *MA = dyn_cast<Argument>(V))
48 return MA->getParent() ? MA->getParent()->getParent() : 0;
49
50 if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
51 return BB->getParent() ? BB->getParent()->getParent() : 0;
52
53 if (const Instruction *I = dyn_cast<Instruction>(V)) {
54 const Function *M = I->getParent() ? I->getParent()->getParent() : 0;
55 return M ? M->getParent() : 0;
56 }
57
58 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
59 return GV->getParent();
60 return 0;
61}
62
Daniel Dunbare9da1332008-10-28 19:33:02 +000063// PrintEscapedString - Print each character of the specified string, escaping
64// it if it is not printable or if it is an escape char.
65static void PrintEscapedString(const char *Str, unsigned Length,
66 raw_ostream &Out) {
67 for (unsigned i = 0; i != Length; ++i) {
68 unsigned char C = Str[i];
Nick Lewycky34a40862009-03-15 06:39:52 +000069 if (isprint(C) && C != '\\' && C != '"')
Daniel Dunbare9da1332008-10-28 19:33:02 +000070 Out << C;
71 else
72 Out << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F);
73 }
74}
75
76// PrintEscapedString - Print each character of the specified string, escaping
77// it if it is not printable or if it is an escape char.
78static void PrintEscapedString(const std::string &Str, raw_ostream &Out) {
79 PrintEscapedString(Str.c_str(), Str.size(), Out);
80}
81
Chris Lattner6ab910b2008-08-19 04:36:02 +000082enum PrefixType {
83 GlobalPrefix,
84 LabelPrefix,
Daniel Dunbarcad35802008-10-14 23:28:09 +000085 LocalPrefix,
86 NoPrefix
Chris Lattner6ab910b2008-08-19 04:36:02 +000087};
88
89/// PrintLLVMName - Turn the specified name into an 'LLVM name', which is either
90/// prefixed with % (if the string only contains simple characters) or is
91/// surrounded with ""'s (if it has special chars in it). Print it out.
Chris Lattner944fac72008-08-23 22:23:09 +000092static void PrintLLVMName(raw_ostream &OS, const char *NameStr,
Chris Lattner52b26de2008-08-19 05:16:28 +000093 unsigned NameLen, PrefixType Prefix) {
94 assert(NameStr && "Cannot get empty name!");
Chris Lattner6ab910b2008-08-19 04:36:02 +000095 switch (Prefix) {
Chris Lattner52b26de2008-08-19 05:16:28 +000096 default: assert(0 && "Bad prefix!");
Daniel Dunbarcad35802008-10-14 23:28:09 +000097 case NoPrefix: break;
Chris Lattner52b26de2008-08-19 05:16:28 +000098 case GlobalPrefix: OS << '@'; break;
99 case LabelPrefix: break;
100 case LocalPrefix: OS << '%'; break;
Nick Lewycky04234832009-03-19 06:31:22 +0000101 }
Chris Lattner6ab910b2008-08-19 04:36:02 +0000102
103 // Scan the name to see if it needs quotes first.
Daniel Dunbare9da1332008-10-28 19:33:02 +0000104 bool NeedsQuotes = isdigit(NameStr[0]);
Chris Lattner6ab910b2008-08-19 04:36:02 +0000105 if (!NeedsQuotes) {
106 for (unsigned i = 0; i != NameLen; ++i) {
107 char C = NameStr[i];
108 if (!isalnum(C) && C != '-' && C != '.' && C != '_') {
109 NeedsQuotes = true;
110 break;
111 }
112 }
113 }
114
115 // If we didn't need any quotes, just write out the name in one blast.
116 if (!NeedsQuotes) {
117 OS.write(NameStr, NameLen);
118 return;
119 }
120
121 // Okay, we need quotes. Output the quotes and escape any scary characters as
122 // needed.
123 OS << '"';
Daniel Dunbare9da1332008-10-28 19:33:02 +0000124 PrintEscapedString(NameStr, NameLen, OS);
Chris Lattner6ab910b2008-08-19 04:36:02 +0000125 OS << '"';
126}
127
128/// PrintLLVMName - Turn the specified name into an 'LLVM name', which is either
129/// prefixed with % (if the string only contains simple characters) or is
130/// surrounded with ""'s (if it has special chars in it). Print it out.
Chris Lattner944fac72008-08-23 22:23:09 +0000131static void PrintLLVMName(raw_ostream &OS, const Value *V) {
Chris Lattner52b26de2008-08-19 05:16:28 +0000132 PrintLLVMName(OS, V->getNameStart(), V->getNameLen(),
Chris Lattner6ab910b2008-08-19 04:36:02 +0000133 isa<GlobalValue>(V) ? GlobalPrefix : LocalPrefix);
134}
135
Chris Lattner9cc34462009-02-28 20:25:14 +0000136//===----------------------------------------------------------------------===//
137// TypePrinting Class: Type printing machinery
138//===----------------------------------------------------------------------===//
139
Chris Lattner87185e82009-02-28 23:03:55 +0000140static DenseMap<const Type *, std::string> &getTypeNamesMap(void *M) {
141 return *static_cast<DenseMap<const Type *, std::string>*>(M);
Chris Lattnerd8030a72009-02-28 22:34:45 +0000142}
143
144void TypePrinting::clear() {
145 getTypeNamesMap(TypeNames).clear();
146}
Chris Lattner9cc34462009-02-28 20:25:14 +0000147
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000148bool TypePrinting::hasTypeName(const Type *Ty) const {
149 return getTypeNamesMap(TypeNames).count(Ty);
150}
151
152void TypePrinting::addTypeName(const Type *Ty, const std::string &N) {
153 getTypeNamesMap(TypeNames).insert(std::make_pair(Ty, N));
154}
155
156
157TypePrinting::TypePrinting() {
Chris Lattner87185e82009-02-28 23:03:55 +0000158 TypeNames = new DenseMap<const Type *, std::string>();
Chris Lattner9cc34462009-02-28 20:25:14 +0000159}
160
Chris Lattnerd8030a72009-02-28 22:34:45 +0000161TypePrinting::~TypePrinting() {
162 delete &getTypeNamesMap(TypeNames);
163}
164
Chris Lattner534361e2009-02-28 20:49:40 +0000165/// CalcTypeName - Write the specified type to the specified raw_ostream, making
166/// use of type names or up references to shorten the type name where possible.
167void TypePrinting::CalcTypeName(const Type *Ty,
Chris Lattnerb840cac2009-02-28 20:34:19 +0000168 SmallVectorImpl<const Type *> &TypeStack,
Chris Lattnerd2b6cb02009-03-01 01:16:21 +0000169 raw_ostream &OS, bool IgnoreTopLevelName) {
Chris Lattner9cc34462009-02-28 20:25:14 +0000170 // Check to see if the type is named.
Chris Lattnerd2b6cb02009-03-01 01:16:21 +0000171 if (!IgnoreTopLevelName) {
Nick Lewycky04234832009-03-19 06:31:22 +0000172 DenseMap<const Type *, std::string> &TM = getTypeNamesMap(TypeNames);
Chris Lattnerd2b6cb02009-03-01 01:16:21 +0000173 DenseMap<const Type *, std::string>::iterator I = TM.find(Ty);
174 if (I != TM.end()) {
175 OS << I->second;
176 return;
177 }
Chris Lattner9cc34462009-02-28 20:25:14 +0000178 }
179
180 // Check to see if the Type is already on the stack...
181 unsigned Slot = 0, CurSize = TypeStack.size();
182 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
183
184 // This is another base case for the recursion. In this case, we know
185 // that we have looped back to a type that we have previously visited.
186 // Generate the appropriate upreference to handle this.
187 if (Slot < CurSize) {
Chris Lattner30794262009-02-28 21:27:31 +0000188 OS << '\\' << unsigned(CurSize-Slot); // Here's the upreference
Chris Lattner9cc34462009-02-28 20:25:14 +0000189 return;
190 }
191
192 TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
193
194 switch (Ty->getTypeID()) {
Chris Lattner30794262009-02-28 21:27:31 +0000195 case Type::VoidTyID: OS << "void"; break;
196 case Type::FloatTyID: OS << "float"; break;
197 case Type::DoubleTyID: OS << "double"; break;
198 case Type::X86_FP80TyID: OS << "x86_fp80"; break;
199 case Type::FP128TyID: OS << "fp128"; break;
200 case Type::PPC_FP128TyID: OS << "ppc_fp128"; break;
201 case Type::LabelTyID: OS << "label"; break;
Chris Lattner583ffd82009-02-28 21:18:43 +0000202 case Type::IntegerTyID:
Chris Lattner30794262009-02-28 21:27:31 +0000203 OS << 'i' << cast<IntegerType>(Ty)->getBitWidth();
Chris Lattner583ffd82009-02-28 21:18:43 +0000204 break;
205
Chris Lattner36942d72009-02-28 20:35:42 +0000206 case Type::FunctionTyID: {
207 const FunctionType *FTy = cast<FunctionType>(Ty);
Chris Lattner30794262009-02-28 21:27:31 +0000208 CalcTypeName(FTy->getReturnType(), TypeStack, OS);
209 OS << " (";
Chris Lattner36942d72009-02-28 20:35:42 +0000210 for (FunctionType::param_iterator I = FTy->param_begin(),
211 E = FTy->param_end(); I != E; ++I) {
212 if (I != FTy->param_begin())
Chris Lattner30794262009-02-28 21:27:31 +0000213 OS << ", ";
214 CalcTypeName(*I, TypeStack, OS);
Chris Lattner9cc34462009-02-28 20:25:14 +0000215 }
Chris Lattner36942d72009-02-28 20:35:42 +0000216 if (FTy->isVarArg()) {
Chris Lattner30794262009-02-28 21:27:31 +0000217 if (FTy->getNumParams()) OS << ", ";
218 OS << "...";
Chris Lattner9cc34462009-02-28 20:25:14 +0000219 }
Chris Lattner30794262009-02-28 21:27:31 +0000220 OS << ')';
Chris Lattner36942d72009-02-28 20:35:42 +0000221 break;
222 }
223 case Type::StructTyID: {
224 const StructType *STy = cast<StructType>(Ty);
225 if (STy->isPacked())
Chris Lattner30794262009-02-28 21:27:31 +0000226 OS << '<';
227 OS << "{ ";
Chris Lattner36942d72009-02-28 20:35:42 +0000228 for (StructType::element_iterator I = STy->element_begin(),
229 E = STy->element_end(); I != E; ++I) {
Chris Lattner30794262009-02-28 21:27:31 +0000230 CalcTypeName(*I, TypeStack, OS);
Chris Lattner36942d72009-02-28 20:35:42 +0000231 if (next(I) != STy->element_end())
Chris Lattner30794262009-02-28 21:27:31 +0000232 OS << ',';
233 OS << ' ';
Chris Lattner9cc34462009-02-28 20:25:14 +0000234 }
Chris Lattner30794262009-02-28 21:27:31 +0000235 OS << '}';
Chris Lattner36942d72009-02-28 20:35:42 +0000236 if (STy->isPacked())
Chris Lattner30794262009-02-28 21:27:31 +0000237 OS << '>';
Chris Lattner36942d72009-02-28 20:35:42 +0000238 break;
239 }
240 case Type::PointerTyID: {
241 const PointerType *PTy = cast<PointerType>(Ty);
Chris Lattner30794262009-02-28 21:27:31 +0000242 CalcTypeName(PTy->getElementType(), TypeStack, OS);
Chris Lattner36942d72009-02-28 20:35:42 +0000243 if (unsigned AddressSpace = PTy->getAddressSpace())
Chris Lattner30794262009-02-28 21:27:31 +0000244 OS << " addrspace(" << AddressSpace << ')';
245 OS << '*';
Chris Lattner36942d72009-02-28 20:35:42 +0000246 break;
247 }
248 case Type::ArrayTyID: {
249 const ArrayType *ATy = cast<ArrayType>(Ty);
Chris Lattner30794262009-02-28 21:27:31 +0000250 OS << '[' << ATy->getNumElements() << " x ";
251 CalcTypeName(ATy->getElementType(), TypeStack, OS);
252 OS << ']';
Chris Lattner36942d72009-02-28 20:35:42 +0000253 break;
254 }
255 case Type::VectorTyID: {
256 const VectorType *PTy = cast<VectorType>(Ty);
Chris Lattner30794262009-02-28 21:27:31 +0000257 OS << "<" << PTy->getNumElements() << " x ";
258 CalcTypeName(PTy->getElementType(), TypeStack, OS);
259 OS << '>';
Chris Lattner36942d72009-02-28 20:35:42 +0000260 break;
261 }
262 case Type::OpaqueTyID:
Chris Lattner30794262009-02-28 21:27:31 +0000263 OS << "opaque";
Chris Lattner36942d72009-02-28 20:35:42 +0000264 break;
265 default:
Chris Lattner30794262009-02-28 21:27:31 +0000266 OS << "<unrecognized-type>";
Chris Lattner36942d72009-02-28 20:35:42 +0000267 break;
Chris Lattner9cc34462009-02-28 20:25:14 +0000268 }
269
Chris Lattner534361e2009-02-28 20:49:40 +0000270 TypeStack.pop_back(); // Remove self from stack.
Chris Lattner9cc34462009-02-28 20:25:14 +0000271}
272
273/// printTypeInt - The internal guts of printing out a type that has a
274/// potentially named portion.
275///
Chris Lattnerd2b6cb02009-03-01 01:16:21 +0000276void TypePrinting::print(const Type *Ty, raw_ostream &OS,
277 bool IgnoreTopLevelName) {
Chris Lattner9cc34462009-02-28 20:25:14 +0000278 // Check to see if the type is named.
Chris Lattner87185e82009-02-28 23:03:55 +0000279 DenseMap<const Type*, std::string> &TM = getTypeNamesMap(TypeNames);
Chris Lattnerd2b6cb02009-03-01 01:16:21 +0000280 if (!IgnoreTopLevelName) {
281 DenseMap<const Type*, std::string>::iterator I = TM.find(Ty);
282 if (I != TM.end()) {
283 OS << I->second;
284 return;
285 }
Chris Lattner9cc34462009-02-28 20:25:14 +0000286 }
287
288 // Otherwise we have a type that has not been named but is a derived type.
289 // Carefully recurse the type hierarchy to print out any contained symbolic
290 // names.
Chris Lattnerb840cac2009-02-28 20:34:19 +0000291 SmallVector<const Type *, 16> TypeStack;
Chris Lattner9cc34462009-02-28 20:25:14 +0000292 std::string TypeName;
Chris Lattner534361e2009-02-28 20:49:40 +0000293
294 raw_string_ostream TypeOS(TypeName);
Chris Lattnerd2b6cb02009-03-01 01:16:21 +0000295 CalcTypeName(Ty, TypeStack, TypeOS, IgnoreTopLevelName);
Chris Lattner534361e2009-02-28 20:49:40 +0000296 OS << TypeOS.str();
297
298 // Cache type name for later use.
Chris Lattnerd2b6cb02009-03-01 01:16:21 +0000299 if (!IgnoreTopLevelName)
300 TM.insert(std::make_pair(Ty, TypeOS.str()));
Chris Lattner9cc34462009-02-28 20:25:14 +0000301}
302
Chris Lattner413fd232009-03-01 00:03:38 +0000303namespace {
304 class TypeFinder {
305 // To avoid walking constant expressions multiple times and other IR
306 // objects, we keep several helper maps.
307 DenseSet<const Value*> VisitedConstants;
308 DenseSet<const Type*> VisitedTypes;
309
310 TypePrinting &TP;
311 std::vector<const Type*> &NumberedTypes;
312 public:
313 TypeFinder(TypePrinting &tp, std::vector<const Type*> &numberedTypes)
314 : TP(tp), NumberedTypes(numberedTypes) {}
315
316 void Run(const Module &M) {
Chris Lattner88485862009-03-01 00:32:33 +0000317 // Get types from the type symbol table. This gets opaque types referened
318 // only through derived named types.
319 const TypeSymbolTable &ST = M.getTypeSymbolTable();
320 for (TypeSymbolTable::const_iterator TI = ST.begin(), E = ST.end();
321 TI != E; ++TI)
322 IncorporateType(TI->second);
323
Chris Lattner413fd232009-03-01 00:03:38 +0000324 // Get types from global variables.
325 for (Module::const_global_iterator I = M.global_begin(),
326 E = M.global_end(); I != E; ++I) {
327 IncorporateType(I->getType());
328 if (I->hasInitializer())
329 IncorporateValue(I->getInitializer());
330 }
331
332 // Get types from aliases.
333 for (Module::const_alias_iterator I = M.alias_begin(),
334 E = M.alias_end(); I != E; ++I) {
335 IncorporateType(I->getType());
336 IncorporateValue(I->getAliasee());
337 }
338
339 // Get types from functions.
340 for (Module::const_iterator FI = M.begin(), E = M.end(); FI != E; ++FI) {
341 IncorporateType(FI->getType());
342
343 for (Function::const_iterator BB = FI->begin(), E = FI->end();
344 BB != E;++BB)
345 for (BasicBlock::const_iterator II = BB->begin(),
346 E = BB->end(); II != E; ++II) {
347 const Instruction &I = *II;
348 // Incorporate the type of the instruction and all its operands.
349 IncorporateType(I.getType());
350 for (User::const_op_iterator OI = I.op_begin(), OE = I.op_end();
351 OI != OE; ++OI)
352 IncorporateValue(*OI);
353 }
354 }
355 }
356
357 private:
358 void IncorporateType(const Type *Ty) {
359 // Check to see if we're already visited this type.
Chris Lattner88485862009-03-01 00:32:33 +0000360 if (!VisitedTypes.insert(Ty).second)
Chris Lattner413fd232009-03-01 00:03:38 +0000361 return;
362
363 // If this is a structure or opaque type, add a name for the type.
Nick Lewycky21cc4462009-04-04 07:22:01 +0000364 if (((isa<StructType>(Ty) && cast<StructType>(Ty)->getNumElements())
365 || isa<OpaqueType>(Ty)) && !TP.hasTypeName(Ty)) {
Chris Lattner413fd232009-03-01 00:03:38 +0000366 TP.addTypeName(Ty, "%"+utostr(unsigned(NumberedTypes.size())));
367 NumberedTypes.push_back(Ty);
368 }
369
370 // Recursively walk all contained types.
371 for (Type::subtype_iterator I = Ty->subtype_begin(),
372 E = Ty->subtype_end(); I != E; ++I)
373 IncorporateType(*I);
374 }
375
376 /// IncorporateValue - This method is used to walk operand lists finding
377 /// types hiding in constant expressions and other operands that won't be
378 /// walked in other ways. GlobalValues, basic blocks, instructions, and
379 /// inst operands are all explicitly enumerated.
380 void IncorporateValue(const Value *V) {
381 if (V == 0 || !isa<Constant>(V) || isa<GlobalValue>(V)) return;
382
383 // Already visited?
384 if (!VisitedConstants.insert(V).second)
385 return;
386
387 // Check this type.
388 IncorporateType(V->getType());
389
390 // Look in operands for types.
391 const Constant *C = cast<Constant>(V);
392 for (Constant::const_op_iterator I = C->op_begin(),
393 E = C->op_end(); I != E;++I)
394 IncorporateValue(*I);
395 }
396 };
397} // end anonymous namespace
398
399
400/// AddModuleTypesToPrinter - Add all of the symbolic type names for types in
401/// the specified module to the TypePrinter and all numbered types to it and the
402/// NumberedTypes table.
403static void AddModuleTypesToPrinter(TypePrinting &TP,
404 std::vector<const Type*> &NumberedTypes,
405 const Module *M) {
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000406 if (M == 0) return;
407
408 // If the module has a symbol table, take all global types and stuff their
409 // names into the TypeNames map.
410 const TypeSymbolTable &ST = M->getTypeSymbolTable();
411 for (TypeSymbolTable::const_iterator TI = ST.begin(), E = ST.end();
412 TI != E; ++TI) {
413 const Type *Ty = cast<Type>(TI->second);
414
415 // As a heuristic, don't insert pointer to primitive types, because
416 // they are used too often to have a single useful name.
417 if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
418 const Type *PETy = PTy->getElementType();
419 if ((PETy->isPrimitiveType() || PETy->isInteger()) &&
420 !isa<OpaqueType>(PETy))
421 continue;
422 }
423
424 // Likewise don't insert primitives either.
425 if (Ty->isInteger() || Ty->isPrimitiveType())
426 continue;
427
428 // Get the name as a string and insert it into TypeNames.
429 std::string NameStr;
430 raw_string_ostream NameOS(NameStr);
431 PrintLLVMName(NameOS, TI->first.c_str(), TI->first.length(), LocalPrefix);
432 TP.addTypeName(Ty, NameOS.str());
433 }
Chris Lattner413fd232009-03-01 00:03:38 +0000434
435 // Walk the entire module to find references to unnamed structure and opaque
436 // types. This is required for correctness by opaque types (because multiple
437 // uses of an unnamed opaque type needs to be referred to by the same ID) and
438 // it shrinks complex recursive structure types substantially in some cases.
439 TypeFinder(TP, NumberedTypes).Run(*M);
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000440}
441
Chris Lattner9cc34462009-02-28 20:25:14 +0000442
Chris Lattner9cc34462009-02-28 20:25:14 +0000443/// WriteTypeSymbolic - This attempts to write the specified type as a symbolic
444/// type, iff there is an entry in the modules symbol table for the specified
Chris Lattnerc2871372009-02-28 21:05:51 +0000445/// type or one of it's component types.
Chris Lattner9cc34462009-02-28 20:25:14 +0000446///
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000447void llvm::WriteTypeSymbolic(raw_ostream &OS, const Type *Ty, const Module *M) {
448 TypePrinting Printer;
Chris Lattner413fd232009-03-01 00:03:38 +0000449 std::vector<const Type*> NumberedTypes;
450 AddModuleTypesToPrinter(Printer, NumberedTypes, M);
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000451 Printer.print(Ty, OS);
Chris Lattner9cc34462009-02-28 20:25:14 +0000452}
453
Chris Lattner6ab910b2008-08-19 04:36:02 +0000454//===----------------------------------------------------------------------===//
455// SlotTracker Class: Enumerate slot numbers for unnamed values
456//===----------------------------------------------------------------------===//
457
Chris Lattnerb64871a2008-08-19 04:28:07 +0000458namespace {
459
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000460/// This class provides computation of slot numbers for LLVM Assembly writing.
Chris Lattner45d4c732008-08-17 04:17:45 +0000461///
Chris Lattner0d9574a2008-08-19 04:26:57 +0000462class SlotTracker {
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000463public:
Chris Lattner45d4c732008-08-17 04:17:45 +0000464 /// ValueMap - A mapping of Values to slot numbers
Chris Lattner92255072008-08-17 17:25:25 +0000465 typedef DenseMap<const Value*, unsigned> ValueMap;
Chris Lattner45d4c732008-08-17 04:17:45 +0000466
467private:
468 /// TheModule - The module for which we are holding slot numbers
469 const Module* TheModule;
470
471 /// TheFunction - The function for which we are holding slot numbers
472 const Function* TheFunction;
473 bool FunctionProcessed;
474
475 /// mMap - The TypePlanes map for the module level data
476 ValueMap mMap;
477 unsigned mNext;
478
479 /// fMap - The TypePlanes map for the function level data
480 ValueMap fMap;
481 unsigned fNext;
482
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000483public:
Chris Lattner45d4c732008-08-17 04:17:45 +0000484 /// Construct from a module
Chris Lattner0d9574a2008-08-19 04:26:57 +0000485 explicit SlotTracker(const Module *M);
Chris Lattner45d4c732008-08-17 04:17:45 +0000486 /// Construct from a function, starting out in incorp state.
Chris Lattner0d9574a2008-08-19 04:26:57 +0000487 explicit SlotTracker(const Function *F);
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000488
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000489 /// Return the slot number of the specified value in it's type
Chris Lattner0d9574a2008-08-19 04:26:57 +0000490 /// plane. If something is not in the SlotTracker, return -1.
Chris Lattner22379bc2007-01-11 03:54:27 +0000491 int getLocalSlot(const Value *V);
492 int getGlobalSlot(const GlobalValue *V);
Reid Spencerfc621e22004-06-09 15:26:53 +0000493
Misha Brukmanfd939082005-04-21 23:48:37 +0000494 /// If you'd like to deal with a function instead of just a module, use
Chris Lattner0d9574a2008-08-19 04:26:57 +0000495 /// this method to get its data into the SlotTracker.
Misha Brukmanfd939082005-04-21 23:48:37 +0000496 void incorporateFunction(const Function *F) {
497 TheFunction = F;
Reid Spencer28531c72004-08-16 07:46:33 +0000498 FunctionProcessed = false;
499 }
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000500
Misha Brukmanfd939082005-04-21 23:48:37 +0000501 /// After calling incorporateFunction, use this method to remove the
Chris Lattner0d9574a2008-08-19 04:26:57 +0000502 /// most recently incorporated function from the SlotTracker. This
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000503 /// will reset the state of the machine back to just the module contents.
504 void purgeFunction();
505
Chris Lattner45d4c732008-08-17 04:17:45 +0000506 // Implementation Details
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000507private:
Reid Spencerb03de0c2004-05-26 21:56:09 +0000508 /// This function does the actual initialization.
509 inline void initialize();
510
Chris Lattner9446bbe2007-01-09 07:55:49 +0000511 /// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
512 void CreateModuleSlot(const GlobalValue *V);
513
514 /// CreateFunctionSlot - Insert the specified Value* into the slot table.
515 void CreateFunctionSlot(const Value *V);
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000516
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000517 /// Add all of the module level global variables (and their initializers)
518 /// and function declarations, but not the contents of those functions.
519 void processModule();
520
Reid Spencerb03de0c2004-05-26 21:56:09 +0000521 /// Add all of the functions arguments, basic blocks, and instructions
522 void processFunction();
523
Chris Lattner0d9574a2008-08-19 04:26:57 +0000524 SlotTracker(const SlotTracker &); // DO NOT IMPLEMENT
525 void operator=(const SlotTracker &); // DO NOT IMPLEMENT
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000526};
527
Chris Lattnerb64871a2008-08-19 04:28:07 +0000528} // end anonymous namespace
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000529
Chris Lattner6ab910b2008-08-19 04:36:02 +0000530
531static SlotTracker *createSlotTracker(const Value *V) {
532 if (const Argument *FA = dyn_cast<Argument>(V))
533 return new SlotTracker(FA->getParent());
534
535 if (const Instruction *I = dyn_cast<Instruction>(V))
536 return new SlotTracker(I->getParent()->getParent());
537
538 if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
539 return new SlotTracker(BB->getParent());
540
541 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
542 return new SlotTracker(GV->getParent());
543
544 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V))
545 return new SlotTracker(GA->getParent());
546
547 if (const Function *Func = dyn_cast<Function>(V))
548 return new SlotTracker(Func);
549
550 return 0;
551}
552
553#if 0
Chris Lattner24233032008-08-19 04:47:09 +0000554#define ST_DEBUG(X) cerr << X
Chris Lattner6ab910b2008-08-19 04:36:02 +0000555#else
Chris Lattner24233032008-08-19 04:47:09 +0000556#define ST_DEBUG(X)
Chris Lattner6ab910b2008-08-19 04:36:02 +0000557#endif
558
559// Module level constructor. Causes the contents of the Module (sans functions)
560// to be added to the slot table.
561SlotTracker::SlotTracker(const Module *M)
Chris Lattnercfb5a202008-08-19 05:06:27 +0000562 : TheModule(M), TheFunction(0), FunctionProcessed(false), mNext(0), fNext(0) {
Chris Lattner6ab910b2008-08-19 04:36:02 +0000563}
564
565// Function level constructor. Causes the contents of the Module and the one
566// function provided to be added to the slot table.
567SlotTracker::SlotTracker(const Function *F)
Chris Lattnercfb5a202008-08-19 05:06:27 +0000568 : TheModule(F ? F->getParent() : 0), TheFunction(F), FunctionProcessed(false),
569 mNext(0), fNext(0) {
Chris Lattner6ab910b2008-08-19 04:36:02 +0000570}
571
572inline void SlotTracker::initialize() {
573 if (TheModule) {
574 processModule();
575 TheModule = 0; ///< Prevent re-processing next time we're called.
576 }
Chris Lattnercfb5a202008-08-19 05:06:27 +0000577
Chris Lattner6ab910b2008-08-19 04:36:02 +0000578 if (TheFunction && !FunctionProcessed)
579 processFunction();
580}
581
582// Iterate through all the global variables, functions, and global
583// variable initializers and create slots for them.
584void SlotTracker::processModule() {
Chris Lattner24233032008-08-19 04:47:09 +0000585 ST_DEBUG("begin processModule!\n");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000586
587 // Add all of the unnamed global variables to the value table.
588 for (Module::const_global_iterator I = TheModule->global_begin(),
589 E = TheModule->global_end(); I != E; ++I)
590 if (!I->hasName())
591 CreateModuleSlot(I);
592
593 // Add all the unnamed functions to the table.
594 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
595 I != E; ++I)
596 if (!I->hasName())
597 CreateModuleSlot(I);
598
Chris Lattner24233032008-08-19 04:47:09 +0000599 ST_DEBUG("end processModule!\n");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000600}
601
602
603// Process the arguments, basic blocks, and instructions of a function.
604void SlotTracker::processFunction() {
Chris Lattner24233032008-08-19 04:47:09 +0000605 ST_DEBUG("begin processFunction!\n");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000606 fNext = 0;
607
608 // Add all the function arguments with no names.
609 for(Function::const_arg_iterator AI = TheFunction->arg_begin(),
610 AE = TheFunction->arg_end(); AI != AE; ++AI)
611 if (!AI->hasName())
612 CreateFunctionSlot(AI);
613
Chris Lattner24233032008-08-19 04:47:09 +0000614 ST_DEBUG("Inserting Instructions:\n");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000615
616 // Add all of the basic blocks and instructions with no names.
617 for (Function::const_iterator BB = TheFunction->begin(),
618 E = TheFunction->end(); BB != E; ++BB) {
619 if (!BB->hasName())
620 CreateFunctionSlot(BB);
621 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
622 if (I->getType() != Type::VoidTy && !I->hasName())
623 CreateFunctionSlot(I);
624 }
625
626 FunctionProcessed = true;
627
Chris Lattner24233032008-08-19 04:47:09 +0000628 ST_DEBUG("end processFunction!\n");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000629}
630
631/// Clean up after incorporating a function. This is the only way to get out of
632/// the function incorporation state that affects get*Slot/Create*Slot. Function
633/// incorporation state is indicated by TheFunction != 0.
634void SlotTracker::purgeFunction() {
Chris Lattner24233032008-08-19 04:47:09 +0000635 ST_DEBUG("begin purgeFunction!\n");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000636 fMap.clear(); // Simply discard the function level map
637 TheFunction = 0;
638 FunctionProcessed = false;
Chris Lattner24233032008-08-19 04:47:09 +0000639 ST_DEBUG("end purgeFunction!\n");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000640}
641
642/// getGlobalSlot - Get the slot number of a global value.
643int SlotTracker::getGlobalSlot(const GlobalValue *V) {
644 // Check for uninitialized state and do lazy initialization.
645 initialize();
646
647 // Find the type plane in the module map
648 ValueMap::iterator MI = mMap.find(V);
Dan Gohmanaeaf2452008-10-01 19:58:59 +0000649 return MI == mMap.end() ? -1 : (int)MI->second;
Chris Lattner6ab910b2008-08-19 04:36:02 +0000650}
651
652
653/// getLocalSlot - Get the slot number for a value that is local to a function.
654int SlotTracker::getLocalSlot(const Value *V) {
655 assert(!isa<Constant>(V) && "Can't get a constant or global slot with this!");
656
657 // Check for uninitialized state and do lazy initialization.
658 initialize();
659
660 ValueMap::iterator FI = fMap.find(V);
Dan Gohmanaeaf2452008-10-01 19:58:59 +0000661 return FI == fMap.end() ? -1 : (int)FI->second;
Chris Lattner6ab910b2008-08-19 04:36:02 +0000662}
663
664
665/// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
666void SlotTracker::CreateModuleSlot(const GlobalValue *V) {
667 assert(V && "Can't insert a null Value into SlotTracker!");
668 assert(V->getType() != Type::VoidTy && "Doesn't need a slot!");
669 assert(!V->hasName() && "Doesn't need a slot!");
670
671 unsigned DestSlot = mNext++;
672 mMap[V] = DestSlot;
673
Chris Lattner24233032008-08-19 04:47:09 +0000674 ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
Chris Lattner6ab910b2008-08-19 04:36:02 +0000675 DestSlot << " [");
676 // G = Global, F = Function, A = Alias, o = other
Chris Lattner24233032008-08-19 04:47:09 +0000677 ST_DEBUG((isa<GlobalVariable>(V) ? 'G' :
Chris Lattner6ab910b2008-08-19 04:36:02 +0000678 (isa<Function>(V) ? 'F' :
679 (isa<GlobalAlias>(V) ? 'A' : 'o'))) << "]\n");
680}
681
682
683/// CreateSlot - Create a new slot for the specified value if it has no name.
684void SlotTracker::CreateFunctionSlot(const Value *V) {
685 assert(V->getType() != Type::VoidTy && !V->hasName() &&
686 "Doesn't need a slot!");
687
688 unsigned DestSlot = fNext++;
689 fMap[V] = DestSlot;
690
691 // G = Global, F = Function, o = other
Chris Lattner24233032008-08-19 04:47:09 +0000692 ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
Chris Lattner6ab910b2008-08-19 04:36:02 +0000693 DestSlot << " [o]\n");
694}
695
696
697
698//===----------------------------------------------------------------------===//
699// AsmWriter Implementation
700//===----------------------------------------------------------------------===//
Chris Lattnerf082b802002-07-23 18:07:49 +0000701
Chris Lattner944fac72008-08-23 22:23:09 +0000702static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
Chris Lattner9cc34462009-02-28 20:25:14 +0000703 TypePrinting &TypePrinter,
Chris Lattner0d9574a2008-08-19 04:26:57 +0000704 SlotTracker *Machine);
Reid Spencer0e25e1c2004-07-04 11:50:43 +0000705
Chris Lattnerc97536e2008-08-17 04:40:13 +0000706
Chris Lattner207b5bc2001-10-29 16:37:48 +0000707
Chris Lattner82c4bc72006-12-06 06:40:49 +0000708static const char *getPredicateText(unsigned predicate) {
Reid Spencer81dfeb32006-12-04 05:19:18 +0000709 const char * pred = "unknown";
710 switch (predicate) {
711 case FCmpInst::FCMP_FALSE: pred = "false"; break;
712 case FCmpInst::FCMP_OEQ: pred = "oeq"; break;
713 case FCmpInst::FCMP_OGT: pred = "ogt"; break;
714 case FCmpInst::FCMP_OGE: pred = "oge"; break;
715 case FCmpInst::FCMP_OLT: pred = "olt"; break;
716 case FCmpInst::FCMP_OLE: pred = "ole"; break;
717 case FCmpInst::FCMP_ONE: pred = "one"; break;
718 case FCmpInst::FCMP_ORD: pred = "ord"; break;
719 case FCmpInst::FCMP_UNO: pred = "uno"; break;
720 case FCmpInst::FCMP_UEQ: pred = "ueq"; break;
721 case FCmpInst::FCMP_UGT: pred = "ugt"; break;
722 case FCmpInst::FCMP_UGE: pred = "uge"; break;
723 case FCmpInst::FCMP_ULT: pred = "ult"; break;
724 case FCmpInst::FCMP_ULE: pred = "ule"; break;
725 case FCmpInst::FCMP_UNE: pred = "une"; break;
726 case FCmpInst::FCMP_TRUE: pred = "true"; break;
727 case ICmpInst::ICMP_EQ: pred = "eq"; break;
728 case ICmpInst::ICMP_NE: pred = "ne"; break;
729 case ICmpInst::ICMP_SGT: pred = "sgt"; break;
730 case ICmpInst::ICMP_SGE: pred = "sge"; break;
731 case ICmpInst::ICMP_SLT: pred = "slt"; break;
732 case ICmpInst::ICMP_SLE: pred = "sle"; break;
733 case ICmpInst::ICMP_UGT: pred = "ugt"; break;
734 case ICmpInst::ICMP_UGE: pred = "uge"; break;
735 case ICmpInst::ICMP_ULT: pred = "ult"; break;
736 case ICmpInst::ICMP_ULE: pred = "ule"; break;
737 }
738 return pred;
739}
740
Chris Lattner944fac72008-08-23 22:23:09 +0000741static void WriteConstantInt(raw_ostream &Out, const Constant *CV,
Chris Lattner9cc34462009-02-28 20:25:14 +0000742 TypePrinting &TypePrinter, SlotTracker *Machine) {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000743 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Chris Lattnerfad86b02008-08-17 07:19:36 +0000744 if (CI->getType() == Type::Int1Ty) {
Reid Spencer579dca12007-01-12 04:24:46 +0000745 Out << (CI->getZExtValue() ? "true" : "false");
Chris Lattnerfad86b02008-08-17 07:19:36 +0000746 return;
747 }
748 Out << CI->getValue();
749 return;
750 }
751
752 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000753 if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEdouble ||
754 &CFP->getValueAPF().getSemantics() == &APFloat::IEEEsingle) {
755 // We would like to output the FP constant value in exponential notation,
756 // but we cannot do this if doing so will lose precision. Check here to
757 // make sure that we only output it in exponential format if we can parse
758 // the value back and get the same value.
759 //
Dale Johannesen541ed9f2009-01-21 20:32:55 +0000760 bool ignored;
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000761 bool isDouble = &CFP->getValueAPF().getSemantics()==&APFloat::IEEEdouble;
Chris Lattnerfad86b02008-08-17 07:19:36 +0000762 double Val = isDouble ? CFP->getValueAPF().convertToDouble() :
763 CFP->getValueAPF().convertToFloat();
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000764 std::string StrVal = ftostr(CFP->getValueAPF());
Chris Lattner66e810b2002-04-18 18:53:13 +0000765
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000766 // Check to make sure that the stringized number is not some string like
767 // "Inf" or NaN, that atof will accept, but the lexer will not. Check
768 // that the string matches the "[-+]?[0-9]" regex.
769 //
770 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
771 ((StrVal[0] == '-' || StrVal[0] == '+') &&
772 (StrVal[1] >= '0' && StrVal[1] <= '9'))) {
773 // Reparse stringized version!
774 if (atof(StrVal.c_str()) == Val) {
775 Out << StrVal;
776 return;
777 }
Chris Lattner66e810b2002-04-18 18:53:13 +0000778 }
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000779 // Otherwise we could not reparse it to exactly the same value, so we must
Dale Johannesen541ed9f2009-01-21 20:32:55 +0000780 // output the string in hexadecimal format! Note that loading and storing
781 // floating point types changes the bits of NaNs on some hosts, notably
782 // x86, so we must not use these types.
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000783 assert(sizeof(double) == sizeof(uint64_t) &&
784 "assuming that double is 64 bits!");
Chris Lattnerc6a13462008-11-10 04:30:26 +0000785 char Buffer[40];
Dale Johannesen541ed9f2009-01-21 20:32:55 +0000786 APFloat apf = CFP->getValueAPF();
787 // Floats are represented in ASCII IR as double, convert.
788 if (!isDouble)
789 apf.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven,
790 &ignored);
791 Out << "0x" <<
792 utohex_buffer(uint64_t(apf.bitcastToAPInt().getZExtValue()),
793 Buffer+40);
Chris Lattnercfb5a202008-08-19 05:06:27 +0000794 return;
795 }
796
797 // Some form of long double. These appear as a magic letter identifying
798 // the type, then a fixed number of hex digits.
799 Out << "0x";
Dale Johannesen1b25cb22009-03-23 21:16:53 +0000800 if (&CFP->getValueAPF().getSemantics() == &APFloat::x87DoubleExtended) {
Chris Lattnercfb5a202008-08-19 05:06:27 +0000801 Out << 'K';
Dale Johannesen1b25cb22009-03-23 21:16:53 +0000802 // api needed to prevent premature destruction
803 APInt api = CFP->getValueAPF().bitcastToAPInt();
804 const uint64_t* p = api.getRawData();
805 uint64_t word = p[1];
806 int shiftcount=12;
807 int width = api.getBitWidth();
808 for (int j=0; j<width; j+=4, shiftcount-=4) {
809 unsigned int nibble = (word>>shiftcount) & 15;
810 if (nibble < 10)
811 Out << (unsigned char)(nibble + '0');
812 else
813 Out << (unsigned char)(nibble - 10 + 'A');
814 if (shiftcount == 0 && j+4 < width) {
815 word = *p;
816 shiftcount = 64;
817 if (width-j-4 < 64)
818 shiftcount = width-j-4;
819 }
820 }
821 return;
822 } else if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEquad)
Chris Lattnercfb5a202008-08-19 05:06:27 +0000823 Out << 'L';
824 else if (&CFP->getValueAPF().getSemantics() == &APFloat::PPCDoubleDouble)
825 Out << 'M';
826 else
827 assert(0 && "Unsupported floating point type");
828 // api needed to prevent premature destruction
Dale Johannesen7111b022008-10-09 18:53:47 +0000829 APInt api = CFP->getValueAPF().bitcastToAPInt();
Chris Lattnercfb5a202008-08-19 05:06:27 +0000830 const uint64_t* p = api.getRawData();
831 uint64_t word = *p;
832 int shiftcount=60;
833 int width = api.getBitWidth();
834 for (int j=0; j<width; j+=4, shiftcount-=4) {
835 unsigned int nibble = (word>>shiftcount) & 15;
836 if (nibble < 10)
837 Out << (unsigned char)(nibble + '0');
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000838 else
Chris Lattnercfb5a202008-08-19 05:06:27 +0000839 Out << (unsigned char)(nibble - 10 + 'A');
840 if (shiftcount == 0 && j+4 < width) {
841 word = *(++p);
842 shiftcount = 64;
843 if (width-j-4 < 64)
844 shiftcount = width-j-4;
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000845 }
846 }
Chris Lattnercfb5a202008-08-19 05:06:27 +0000847 return;
848 }
849
850 if (isa<ConstantAggregateZero>(CV)) {
Chris Lattnerde512b52004-02-15 05:55:15 +0000851 Out << "zeroinitializer";
Chris Lattnercfb5a202008-08-19 05:06:27 +0000852 return;
853 }
854
855 if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
Chris Lattner66e810b2002-04-18 18:53:13 +0000856 // As a special case, print the array as a string if it is an array of
Dan Gohman9b38d7d2008-05-12 16:34:30 +0000857 // i8 with ConstantInt values.
Misha Brukmanfd939082005-04-21 23:48:37 +0000858 //
Chris Lattner66e810b2002-04-18 18:53:13 +0000859 const Type *ETy = CA->getType()->getElementType();
Chris Lattner18365502006-01-23 23:03:36 +0000860 if (CA->isString()) {
Chris Lattner66e810b2002-04-18 18:53:13 +0000861 Out << "c\"";
Chris Lattner18365502006-01-23 23:03:36 +0000862 PrintEscapedString(CA->getAsString(), Out);
Chris Lattnercfb5a202008-08-19 05:06:27 +0000863 Out << '"';
Chris Lattner66e810b2002-04-18 18:53:13 +0000864 } else { // Cannot output in string format...
Misha Brukman40c732c2004-06-04 21:11:51 +0000865 Out << '[';
Chris Lattner7a716ad2002-04-16 21:36:08 +0000866 if (CA->getNumOperands()) {
Chris Lattner0f7364b2009-02-28 21:26:53 +0000867 TypePrinter.print(ETy, Out);
Dan Gohman8dae1382008-09-14 17:21:12 +0000868 Out << ' ';
Chris Lattner7a716ad2002-04-16 21:36:08 +0000869 WriteAsOperandInternal(Out, CA->getOperand(0),
Chris Lattner9cc34462009-02-28 20:25:14 +0000870 TypePrinter, Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000871 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
872 Out << ", ";
Chris Lattner0f7364b2009-02-28 21:26:53 +0000873 TypePrinter.print(ETy, Out);
Dan Gohman8dae1382008-09-14 17:21:12 +0000874 Out << ' ';
Chris Lattner9cc34462009-02-28 20:25:14 +0000875 WriteAsOperandInternal(Out, CA->getOperand(i), TypePrinter, Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000876 }
877 }
Dan Gohman8dae1382008-09-14 17:21:12 +0000878 Out << ']';
Chris Lattner7a716ad2002-04-16 21:36:08 +0000879 }
Chris Lattnercfb5a202008-08-19 05:06:27 +0000880 return;
881 }
882
883 if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Andrew Lenharth43f344a2007-01-08 18:21:30 +0000884 if (CS->getType()->isPacked())
885 Out << '<';
Misha Brukman40c732c2004-06-04 21:11:51 +0000886 Out << '{';
Jim Laskeya3f332b2006-02-25 12:27:03 +0000887 unsigned N = CS->getNumOperands();
888 if (N) {
Chris Lattner24233032008-08-19 04:47:09 +0000889 Out << ' ';
Chris Lattner0f7364b2009-02-28 21:26:53 +0000890 TypePrinter.print(CS->getOperand(0)->getType(), Out);
Dan Gohman8dae1382008-09-14 17:21:12 +0000891 Out << ' ';
Chris Lattner7a716ad2002-04-16 21:36:08 +0000892
Chris Lattner9cc34462009-02-28 20:25:14 +0000893 WriteAsOperandInternal(Out, CS->getOperand(0), TypePrinter, Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000894
Jim Laskeya3f332b2006-02-25 12:27:03 +0000895 for (unsigned i = 1; i < N; i++) {
Chris Lattner7a716ad2002-04-16 21:36:08 +0000896 Out << ", ";
Chris Lattner0f7364b2009-02-28 21:26:53 +0000897 TypePrinter.print(CS->getOperand(i)->getType(), Out);
Dan Gohman8dae1382008-09-14 17:21:12 +0000898 Out << ' ';
Chris Lattner7a716ad2002-04-16 21:36:08 +0000899
Chris Lattner9cc34462009-02-28 20:25:14 +0000900 WriteAsOperandInternal(Out, CS->getOperand(i), TypePrinter, Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000901 }
Dan Gohman8dae1382008-09-14 17:21:12 +0000902 Out << ' ';
Chris Lattner7a716ad2002-04-16 21:36:08 +0000903 }
Jim Laskeya3f332b2006-02-25 12:27:03 +0000904
Dan Gohman8dae1382008-09-14 17:21:12 +0000905 Out << '}';
Andrew Lenharth43f344a2007-01-08 18:21:30 +0000906 if (CS->getType()->isPacked())
907 Out << '>';
Chris Lattnercfb5a202008-08-19 05:06:27 +0000908 return;
909 }
910
911 if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
912 const Type *ETy = CP->getType()->getElementType();
913 assert(CP->getNumOperands() > 0 &&
914 "Number of operands for a PackedConst must be > 0");
Dan Gohman7dfa07f2009-02-11 00:25:25 +0000915 Out << '<';
Chris Lattner0f7364b2009-02-28 21:26:53 +0000916 TypePrinter.print(ETy, Out);
Dan Gohman8dae1382008-09-14 17:21:12 +0000917 Out << ' ';
Chris Lattner9cc34462009-02-28 20:25:14 +0000918 WriteAsOperandInternal(Out, CP->getOperand(0), TypePrinter, Machine);
Chris Lattnercfb5a202008-08-19 05:06:27 +0000919 for (unsigned i = 1, e = CP->getNumOperands(); i != e; ++i) {
Chris Lattner4667b712008-08-19 05:26:17 +0000920 Out << ", ";
Chris Lattner0f7364b2009-02-28 21:26:53 +0000921 TypePrinter.print(ETy, Out);
Dan Gohman8dae1382008-09-14 17:21:12 +0000922 Out << ' ';
Chris Lattner9cc34462009-02-28 20:25:14 +0000923 WriteAsOperandInternal(Out, CP->getOperand(i), TypePrinter, Machine);
Chris Lattnercfb5a202008-08-19 05:06:27 +0000924 }
Dan Gohman7dfa07f2009-02-11 00:25:25 +0000925 Out << '>';
Chris Lattnercfb5a202008-08-19 05:06:27 +0000926 return;
927 }
928
929 if (isa<ConstantPointerNull>(CV)) {
Chris Lattner7a716ad2002-04-16 21:36:08 +0000930 Out << "null";
Chris Lattnercfb5a202008-08-19 05:06:27 +0000931 return;
932 }
933
934 if (isa<UndefValue>(CV)) {
Chris Lattnerb976e662004-10-16 18:08:06 +0000935 Out << "undef";
Chris Lattnercfb5a202008-08-19 05:06:27 +0000936 return;
937 }
Nick Lewycky21cc4462009-04-04 07:22:01 +0000938
939 if (const MDString *S = dyn_cast<MDString>(CV)) {
940 Out << "!\"";
941 PrintEscapedString(S->begin(), S->size(), Out);
942 Out << '"';
943 return;
944 }
Chris Lattnerb976e662004-10-16 18:08:06 +0000945
Nick Lewycky21cc4462009-04-04 07:22:01 +0000946 if (const MDNode *N = dyn_cast<MDNode>(CV)) {
947 Out << "!{";
948 for (MDNode::const_op_iterator I = N->op_begin(), E = N->op_end(); I != E;){
949 TypePrinter.print((*I)->getType(), Out);
950 Out << ' ';
951 WriteAsOperandInternal(Out, *I, TypePrinter, Machine);
952 if (++I != E)
953 Out << ", ";
954 }
955 Out << "}";
956 return;
957 }
958
Chris Lattnercfb5a202008-08-19 05:06:27 +0000959 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Reid Spencer81dfeb32006-12-04 05:19:18 +0000960 Out << CE->getOpcodeName();
961 if (CE->isCompare())
Chris Lattnercfb5a202008-08-19 05:06:27 +0000962 Out << ' ' << getPredicateText(CE->getPredicate());
Reid Spencer81dfeb32006-12-04 05:19:18 +0000963 Out << " (";
Misha Brukmanfd939082005-04-21 23:48:37 +0000964
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000965 for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
Chris Lattner0f7364b2009-02-28 21:26:53 +0000966 TypePrinter.print((*OI)->getType(), Out);
Dan Gohman8dae1382008-09-14 17:21:12 +0000967 Out << ' ';
Chris Lattner9cc34462009-02-28 20:25:14 +0000968 WriteAsOperandInternal(Out, *OI, TypePrinter, Machine);
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000969 if (OI+1 != CE->op_end())
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000970 Out << ", ";
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000971 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000972
Dan Gohman995be7d2008-05-31 19:12:39 +0000973 if (CE->hasIndices()) {
974 const SmallVector<unsigned, 4> &Indices = CE->getIndices();
975 for (unsigned i = 0, e = Indices.size(); i != e; ++i)
976 Out << ", " << Indices[i];
977 }
978
Reid Spencer3da59db2006-11-27 01:05:10 +0000979 if (CE->isCast()) {
Chris Lattner95586b82002-08-15 19:37:43 +0000980 Out << " to ";
Chris Lattner0f7364b2009-02-28 21:26:53 +0000981 TypePrinter.print(CE->getType(), Out);
Chris Lattner95586b82002-08-15 19:37:43 +0000982 }
Reid Spencer3da59db2006-11-27 01:05:10 +0000983
Misha Brukman40c732c2004-06-04 21:11:51 +0000984 Out << ')';
Chris Lattnercfb5a202008-08-19 05:06:27 +0000985 return;
Chris Lattner7a716ad2002-04-16 21:36:08 +0000986 }
Chris Lattnercfb5a202008-08-19 05:06:27 +0000987
988 Out << "<placeholder or erroneous Constant>";
Chris Lattner7a716ad2002-04-16 21:36:08 +0000989}
990
991
Misha Brukmanab5c6002004-03-02 00:22:19 +0000992/// WriteAsOperand - Write the name of the specified value out to the specified
993/// ostream. This can be useful when you just want to print int %reg126, not
994/// the whole instruction that generated it.
995///
Chris Lattner944fac72008-08-23 22:23:09 +0000996static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
Chris Lattner9cc34462009-02-28 20:25:14 +0000997 TypePrinting &TypePrinter,
Chris Lattner0d9574a2008-08-19 04:26:57 +0000998 SlotTracker *Machine) {
Chris Lattnerc97536e2008-08-17 04:40:13 +0000999 if (V->hasName()) {
1000 PrintLLVMName(Out, V);
1001 return;
1002 }
1003
1004 const Constant *CV = dyn_cast<Constant>(V);
1005 if (CV && !isa<GlobalValue>(CV)) {
Chris Lattner9cc34462009-02-28 20:25:14 +00001006 WriteConstantInt(Out, CV, TypePrinter, Machine);
Chris Lattnercfb5a202008-08-19 05:06:27 +00001007 return;
1008 }
1009
1010 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
Chris Lattnerc97536e2008-08-17 04:40:13 +00001011 Out << "asm ";
1012 if (IA->hasSideEffects())
1013 Out << "sideeffect ";
1014 Out << '"';
1015 PrintEscapedString(IA->getAsmString(), Out);
1016 Out << "\", \"";
1017 PrintEscapedString(IA->getConstraintString(), Out);
1018 Out << '"';
Chris Lattnercfb5a202008-08-19 05:06:27 +00001019 return;
1020 }
1021
1022 char Prefix = '%';
1023 int Slot;
1024 if (Machine) {
1025 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1026 Slot = Machine->getGlobalSlot(GV);
1027 Prefix = '@';
1028 } else {
1029 Slot = Machine->getLocalSlot(V);
1030 }
Chris Lattnerc97536e2008-08-17 04:40:13 +00001031 } else {
Chris Lattnercfb5a202008-08-19 05:06:27 +00001032 Machine = createSlotTracker(V);
Chris Lattnerc97536e2008-08-17 04:40:13 +00001033 if (Machine) {
1034 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1035 Slot = Machine->getGlobalSlot(GV);
1036 Prefix = '@';
1037 } else {
1038 Slot = Machine->getLocalSlot(V);
1039 }
Chris Lattner80cd1152006-01-25 22:26:05 +00001040 } else {
Chris Lattnercfb5a202008-08-19 05:06:27 +00001041 Slot = -1;
Chris Lattner7a716ad2002-04-16 21:36:08 +00001042 }
Chris Lattnercfb5a202008-08-19 05:06:27 +00001043 delete Machine;
Chris Lattner7a716ad2002-04-16 21:36:08 +00001044 }
Chris Lattnercfb5a202008-08-19 05:06:27 +00001045
1046 if (Slot != -1)
1047 Out << Prefix << Slot;
1048 else
1049 Out << "<badref>";
Chris Lattner7a716ad2002-04-16 21:36:08 +00001050}
1051
Misha Brukman9d0802e2004-03-01 19:48:13 +00001052/// WriteAsOperand - Write the name of the specified value out to the specified
1053/// ostream. This can be useful when you just want to print int %reg126, not
1054/// the whole instruction that generated it.
1055///
Chris Lattner24233032008-08-19 04:47:09 +00001056void llvm::WriteAsOperand(std::ostream &Out, const Value *V, bool PrintType,
1057 const Module *Context) {
Chris Lattner944fac72008-08-23 22:23:09 +00001058 raw_os_ostream OS(Out);
1059 WriteAsOperand(OS, V, PrintType, Context);
1060}
1061
1062void llvm::WriteAsOperand(raw_ostream &Out, const Value *V, bool PrintType,
1063 const Module *Context) {
Chris Lattner607dc682002-07-10 16:48:17 +00001064 if (Context == 0) Context = getModuleFromVal(V);
Chris Lattner207b5bc2001-10-29 16:37:48 +00001065
Chris Lattnere9fa33e2009-02-28 23:20:19 +00001066 TypePrinting TypePrinter;
Chris Lattner413fd232009-03-01 00:03:38 +00001067 std::vector<const Type*> NumberedTypes;
1068 AddModuleTypesToPrinter(TypePrinter, NumberedTypes, Context);
Dan Gohman8dae1382008-09-14 17:21:12 +00001069 if (PrintType) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001070 TypePrinter.print(V->getType(), Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001071 Out << ' ';
1072 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001073
Chris Lattner9cc34462009-02-28 20:25:14 +00001074 WriteAsOperandInternal(Out, V, TypePrinter, 0);
Chris Lattner622f7402001-07-20 19:15:21 +00001075}
1076
Reid Spencer0e25e1c2004-07-04 11:50:43 +00001077
Chris Lattnercfb5a202008-08-19 05:06:27 +00001078namespace {
Chris Lattnerd8c2e422001-07-12 23:35:26 +00001079
Chris Lattner007377f2001-09-07 16:36:04 +00001080class AssemblyWriter {
Chris Lattner944fac72008-08-23 22:23:09 +00001081 raw_ostream &Out;
Chris Lattner0d9574a2008-08-19 04:26:57 +00001082 SlotTracker &Machine;
Chris Lattnerc1824992001-10-29 16:05:51 +00001083 const Module *TheModule;
Chris Lattner9cc34462009-02-28 20:25:14 +00001084 TypePrinting TypePrinter;
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001085 AssemblyAnnotationWriter *AnnotationWriter;
Chris Lattner413fd232009-03-01 00:03:38 +00001086 std::vector<const Type*> NumberedTypes;
Chris Lattner00950542001-06-06 20:29:01 +00001087public:
Chris Lattner944fac72008-08-23 22:23:09 +00001088 inline AssemblyWriter(raw_ostream &o, SlotTracker &Mac, const Module *M,
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001089 AssemblyAnnotationWriter *AAW)
Chris Lattnere9fa33e2009-02-28 23:20:19 +00001090 : Out(o), Machine(Mac), TheModule(M), AnnotationWriter(AAW) {
Chris Lattner413fd232009-03-01 00:03:38 +00001091 AddModuleTypesToPrinter(TypePrinter, NumberedTypes, M);
Chris Lattner00950542001-06-06 20:29:01 +00001092 }
1093
Chris Lattner413fd232009-03-01 00:03:38 +00001094 void write(const Module *M) { printModule(M); }
Chris Lattner944fac72008-08-23 22:23:09 +00001095
1096 void write(const GlobalValue *G) {
1097 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(G))
1098 printGlobal(GV);
1099 else if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(G))
1100 printAlias(GA);
1101 else if (const Function *F = dyn_cast<Function>(G))
1102 printFunction(F);
1103 else
1104 assert(0 && "Unknown global");
1105 }
1106
Chris Lattnercfb5a202008-08-19 05:06:27 +00001107 void write(const BasicBlock *BB) { printBasicBlock(BB); }
1108 void write(const Instruction *I) { printInstruction(*I); }
Chris Lattner00950542001-06-06 20:29:01 +00001109
Chris Lattner2fcfdb72006-12-06 06:24:27 +00001110 void writeOperand(const Value *Op, bool PrintType);
Devang Pateleaf42ab2008-09-23 23:03:40 +00001111 void writeParamOperand(const Value *Operand, Attributes Attrs);
Chris Lattner66e810b2002-04-18 18:53:13 +00001112
Misha Brukman5cf1acf2004-04-28 15:31:21 +00001113 const Module* getModule() { return TheModule; }
1114
Misha Brukmanf771bea2004-11-15 19:30:05 +00001115private:
Chris Lattnerc1824992001-10-29 16:05:51 +00001116 void printModule(const Module *M);
Reid Spencer78d033e2007-01-06 07:24:44 +00001117 void printTypeSymbolTable(const TypeSymbolTable &ST);
Chris Lattnerc1824992001-10-29 16:05:51 +00001118 void printGlobal(const GlobalVariable *GV);
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001119 void printAlias(const GlobalAlias *GV);
Chris Lattner79df7c02002-03-26 18:01:55 +00001120 void printFunction(const Function *F);
Devang Pateleaf42ab2008-09-23 23:03:40 +00001121 void printArgument(const Argument *FA, Attributes Attrs);
Chris Lattnerc1824992001-10-29 16:05:51 +00001122 void printBasicBlock(const BasicBlock *BB);
Chris Lattner7e708292002-06-25 16:13:24 +00001123 void printInstruction(const Instruction &I);
Chris Lattner2761e9f2002-04-13 20:53:41 +00001124
Chris Lattnere02fa852001-10-13 06:42:36 +00001125 // printInfoComment - Print a little comment after the instruction indicating
1126 // which slot it occupies.
Chris Lattner7e708292002-06-25 16:13:24 +00001127 void printInfoComment(const Value &V);
Chris Lattner00950542001-06-06 20:29:01 +00001128};
Chris Lattner413fd232009-03-01 00:03:38 +00001129} // end of anonymous namespace
Chris Lattner00950542001-06-06 20:29:01 +00001130
Chris Lattner2761e9f2002-04-13 20:53:41 +00001131
Chris Lattner2fcfdb72006-12-06 06:24:27 +00001132void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType) {
1133 if (Operand == 0) {
Chris Lattneraab18202005-02-24 16:58:29 +00001134 Out << "<null operand!>";
Chris Lattner2fcfdb72006-12-06 06:24:27 +00001135 } else {
Chris Lattnercfb5a202008-08-19 05:06:27 +00001136 if (PrintType) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001137 TypePrinter.print(Operand->getType(), Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001138 Out << ' ';
Chris Lattnercfb5a202008-08-19 05:06:27 +00001139 }
Chris Lattner9cc34462009-02-28 20:25:14 +00001140 WriteAsOperandInternal(Out, Operand, TypePrinter, &Machine);
Chris Lattneraab18202005-02-24 16:58:29 +00001141 }
Chris Lattner00950542001-06-06 20:29:01 +00001142}
1143
Dale Johannesen0d51e7e2008-02-19 21:38:47 +00001144void AssemblyWriter::writeParamOperand(const Value *Operand,
Devang Pateleaf42ab2008-09-23 23:03:40 +00001145 Attributes Attrs) {
Duncan Sandsdc024672007-11-27 13:23:08 +00001146 if (Operand == 0) {
1147 Out << "<null operand!>";
1148 } else {
Duncan Sandsdc024672007-11-27 13:23:08 +00001149 // Print the type
Chris Lattner0f7364b2009-02-28 21:26:53 +00001150 TypePrinter.print(Operand->getType(), Out);
Duncan Sandsdc024672007-11-27 13:23:08 +00001151 // Print parameter attributes list
Devang Patel05988662008-09-25 21:00:45 +00001152 if (Attrs != Attribute::None)
1153 Out << ' ' << Attribute::getAsString(Attrs);
Dan Gohman8dae1382008-09-14 17:21:12 +00001154 Out << ' ';
Duncan Sandsdc024672007-11-27 13:23:08 +00001155 // Print the operand
Chris Lattner9cc34462009-02-28 20:25:14 +00001156 WriteAsOperandInternal(Out, Operand, TypePrinter, &Machine);
Duncan Sandsdc024672007-11-27 13:23:08 +00001157 }
1158}
Chris Lattner00950542001-06-06 20:29:01 +00001159
Chris Lattnerc1824992001-10-29 16:05:51 +00001160void AssemblyWriter::printModule(const Module *M) {
Chris Lattner31ab1b32005-03-02 23:12:40 +00001161 if (!M->getModuleIdentifier().empty() &&
Misha Brukmanfd939082005-04-21 23:48:37 +00001162 // Don't print the ID if it will start a new line (which would
Chris Lattner31ab1b32005-03-02 23:12:40 +00001163 // require a comment char before it).
1164 M->getModuleIdentifier().find('\n') == std::string::npos)
1165 Out << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
1166
Owen Andersoncf7ff2b2006-10-18 02:21:12 +00001167 if (!M->getDataLayout().empty())
Chris Lattnerd2f9e602006-10-22 06:06:56 +00001168 Out << "target datalayout = \"" << M->getDataLayout() << "\"\n";
Reid Spencercddc86f2004-07-25 21:44:54 +00001169 if (!M->getTargetTriple().empty())
Reid Spencerc9a1f0d2004-07-25 21:29:43 +00001170 Out << "target triple = \"" << M->getTargetTriple() << "\"\n";
Misha Brukmanfd939082005-04-21 23:48:37 +00001171
Chris Lattnercc041ba2006-01-24 04:13:11 +00001172 if (!M->getModuleInlineAsm().empty()) {
Chris Lattner42a162e2006-01-24 00:45:30 +00001173 // Split the string into lines, to make it easier to read the .ll file.
Chris Lattnercc041ba2006-01-24 04:13:11 +00001174 std::string Asm = M->getModuleInlineAsm();
Chris Lattner42a162e2006-01-24 00:45:30 +00001175 size_t CurPos = 0;
1176 size_t NewLine = Asm.find_first_of('\n', CurPos);
1177 while (NewLine != std::string::npos) {
1178 // We found a newline, print the portion of the asm string from the
1179 // last newline up to this newline.
1180 Out << "module asm \"";
1181 PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.begin()+NewLine),
1182 Out);
1183 Out << "\"\n";
1184 CurPos = NewLine+1;
1185 NewLine = Asm.find_first_of('\n', CurPos);
1186 }
Chris Lattner71cdba32006-01-24 00:40:17 +00001187 Out << "module asm \"";
Chris Lattner42a162e2006-01-24 00:45:30 +00001188 PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.end()), Out);
Chris Lattner18365502006-01-23 23:03:36 +00001189 Out << "\"\n";
1190 }
1191
Chris Lattner44da7d72004-09-14 05:06:58 +00001192 // Loop over the dependent libraries and emit them.
Chris Lattnercfe97b72004-09-14 04:51:44 +00001193 Module::lib_iterator LI = M->lib_begin();
1194 Module::lib_iterator LE = M->lib_end();
Reid Spencercddc86f2004-07-25 21:44:54 +00001195 if (LI != LE) {
Chris Lattnercfe97b72004-09-14 04:51:44 +00001196 Out << "deplibs = [ ";
1197 while (LI != LE) {
Chris Lattner44da7d72004-09-14 05:06:58 +00001198 Out << '"' << *LI << '"';
Reid Spencerc9a1f0d2004-07-25 21:29:43 +00001199 ++LI;
Chris Lattnercfe97b72004-09-14 04:51:44 +00001200 if (LI != LE)
1201 Out << ", ";
Reid Spencerc9a1f0d2004-07-25 21:29:43 +00001202 }
1203 Out << " ]\n";
Reid Spencer83f6a772004-07-25 18:08:18 +00001204 }
Reid Spencere59eaf42004-09-13 23:44:23 +00001205
Chris Lattner413fd232009-03-01 00:03:38 +00001206 // Loop over the symbol table, emitting all id'd types.
Reid Spencer78d033e2007-01-06 07:24:44 +00001207 printTypeSymbolTable(M->getTypeSymbolTable());
Misha Brukmanfd939082005-04-21 23:48:37 +00001208
Chris Lattnerd6d826c2006-12-06 04:41:52 +00001209 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
1210 I != E; ++I)
Chris Lattner7e708292002-06-25 16:13:24 +00001211 printGlobal(I);
Chris Lattner69dacfc2007-04-26 02:24:10 +00001212
1213 // Output all aliases.
1214 if (!M->alias_empty()) Out << "\n";
1215 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
1216 I != E; ++I)
1217 printAlias(I);
Chris Lattner007377f2001-09-07 16:36:04 +00001218
Chris Lattner44da7d72004-09-14 05:06:58 +00001219 // Output all of the functions.
Chris Lattner7e708292002-06-25 16:13:24 +00001220 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
1221 printFunction(I);
Chris Lattner007377f2001-09-07 16:36:04 +00001222}
1223
Chris Lattner944fac72008-08-23 22:23:09 +00001224static void PrintLinkage(GlobalValue::LinkageTypes LT, raw_ostream &Out) {
Chris Lattnercfb5a202008-08-19 05:06:27 +00001225 switch (LT) {
Duncan Sands667d4b82009-03-07 15:45:40 +00001226 case GlobalValue::PrivateLinkage: Out << "private "; break;
1227 case GlobalValue::InternalLinkage: Out << "internal "; break;
Chris Lattner266c7bb2009-04-13 05:44:34 +00001228 case GlobalValue::AvailableExternallyLinkage:
1229 Out << "available_externally ";
1230 break;
Duncan Sands667d4b82009-03-07 15:45:40 +00001231 case GlobalValue::LinkOnceAnyLinkage: Out << "linkonce "; break;
1232 case GlobalValue::LinkOnceODRLinkage: Out << "linkonce_odr "; break;
1233 case GlobalValue::WeakAnyLinkage: Out << "weak "; break;
1234 case GlobalValue::WeakODRLinkage: Out << "weak_odr "; break;
Duncan Sands4dc2b392009-03-11 20:14:15 +00001235 case GlobalValue::CommonLinkage: Out << "common "; break;
Duncan Sands667d4b82009-03-07 15:45:40 +00001236 case GlobalValue::AppendingLinkage: Out << "appending "; break;
1237 case GlobalValue::DLLImportLinkage: Out << "dllimport "; break;
1238 case GlobalValue::DLLExportLinkage: Out << "dllexport "; break;
Duncan Sands5f4ee1f2009-03-11 08:08:06 +00001239 case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break;
Chris Lattnercfb5a202008-08-19 05:06:27 +00001240 case GlobalValue::ExternalLinkage: break;
1241 case GlobalValue::GhostLinkage:
1242 Out << "GhostLinkage not allowed in AsmWriter!\n";
1243 abort();
1244 }
1245}
Duncan Sands667d4b82009-03-07 15:45:40 +00001246
Chris Lattnercfb5a202008-08-19 05:06:27 +00001247
1248static void PrintVisibility(GlobalValue::VisibilityTypes Vis,
Chris Lattner944fac72008-08-23 22:23:09 +00001249 raw_ostream &Out) {
Chris Lattnercfb5a202008-08-19 05:06:27 +00001250 switch (Vis) {
1251 default: assert(0 && "Invalid visibility style!");
1252 case GlobalValue::DefaultVisibility: break;
1253 case GlobalValue::HiddenVisibility: Out << "hidden "; break;
1254 case GlobalValue::ProtectedVisibility: Out << "protected "; break;
1255 }
1256}
1257
Chris Lattnerc1824992001-10-29 16:05:51 +00001258void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Chris Lattnerc97536e2008-08-17 04:40:13 +00001259 if (GV->hasName()) {
1260 PrintLLVMName(Out, GV);
1261 Out << " = ";
1262 }
Chris Lattnerd70684f2001-09-18 04:01:05 +00001263
Chris Lattner52b26de2008-08-19 05:16:28 +00001264 if (!GV->hasInitializer() && GV->hasExternalLinkage())
1265 Out << "external ";
1266
1267 PrintLinkage(GV->getLinkage(), Out);
1268 PrintVisibility(GV->getVisibility(), Out);
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00001269
1270 if (GV->isThreadLocal()) Out << "thread_local ";
Chris Lattnerdf986172009-01-02 07:01:27 +00001271 if (unsigned AddressSpace = GV->getType()->getAddressSpace())
1272 Out << "addrspace(" << AddressSpace << ") ";
Misha Brukman0313e0b2004-06-21 21:53:56 +00001273 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattner0f7364b2009-02-28 21:26:53 +00001274 TypePrinter.print(GV->getType()->getElementType(), Out);
Chris Lattnerd70684f2001-09-18 04:01:05 +00001275
Dan Gohman8dae1382008-09-14 17:21:12 +00001276 if (GV->hasInitializer()) {
1277 Out << ' ';
Chris Lattner2fcfdb72006-12-06 06:24:27 +00001278 writeOperand(GV->getInitializer(), false);
Dan Gohman8dae1382008-09-14 17:21:12 +00001279 }
Christopher Lambfe63fb92007-12-11 08:59:05 +00001280
Chris Lattner60962db2005-11-12 00:10:19 +00001281 if (GV->hasSection())
1282 Out << ", section \"" << GV->getSection() << '"';
1283 if (GV->getAlignment())
Chris Lattner30caa282005-11-06 06:48:53 +00001284 Out << ", align " << GV->getAlignment();
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001285
Chris Lattner7e708292002-06-25 16:13:24 +00001286 printInfoComment(*GV);
Chris Lattnercfb5a202008-08-19 05:06:27 +00001287 Out << '\n';
Chris Lattner70cc3392001-09-10 07:58:01 +00001288}
1289
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001290void AssemblyWriter::printAlias(const GlobalAlias *GA) {
Dale Johannesen24f07dc2008-06-03 18:14:29 +00001291 // Don't crash when dumping partially built GA
1292 if (!GA->hasName())
1293 Out << "<<nameless>> = ";
Chris Lattnerc97536e2008-08-17 04:40:13 +00001294 else {
1295 PrintLLVMName(Out, GA);
1296 Out << " = ";
1297 }
Chris Lattnercfb5a202008-08-19 05:06:27 +00001298 PrintVisibility(GA->getVisibility(), Out);
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001299
1300 Out << "alias ";
1301
Chris Lattnercfb5a202008-08-19 05:06:27 +00001302 PrintLinkage(GA->getLinkage(), Out);
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001303
Anton Korobeynikovc6c98af2007-04-29 18:02:48 +00001304 const Constant *Aliasee = GA->getAliasee();
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001305
1306 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Aliasee)) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001307 TypePrinter.print(GV->getType(), Out);
Chris Lattnerc97536e2008-08-17 04:40:13 +00001308 Out << ' ';
1309 PrintLLVMName(Out, GV);
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001310 } else if (const Function *F = dyn_cast<Function>(Aliasee)) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001311 TypePrinter.print(F->getFunctionType(), Out);
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001312 Out << "* ";
1313
Chris Lattner9cc34462009-02-28 20:25:14 +00001314 WriteAsOperandInternal(Out, F, TypePrinter, &Machine);
Anton Korobeynikov591858a2008-03-22 08:17:17 +00001315 } else if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(Aliasee)) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001316 TypePrinter.print(GA->getType(), Out);
1317 Out << ' ';
Chris Lattnerc97536e2008-08-17 04:40:13 +00001318 PrintLLVMName(Out, GA);
Anton Korobeynikova80e1182007-04-28 13:45:00 +00001319 } else {
1320 const ConstantExpr *CE = 0;
1321 if ((CE = dyn_cast<ConstantExpr>(Aliasee)) &&
1322 (CE->getOpcode() == Instruction::BitCast)) {
1323 writeOperand(CE, false);
1324 } else
1325 assert(0 && "Unsupported aliasee");
1326 }
1327
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001328 printInfoComment(*GA);
Chris Lattner52b26de2008-08-19 05:16:28 +00001329 Out << '\n';
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001330}
1331
Reid Spencer78d033e2007-01-06 07:24:44 +00001332void AssemblyWriter::printTypeSymbolTable(const TypeSymbolTable &ST) {
Chris Lattner413fd232009-03-01 00:03:38 +00001333 // Emit all numbered types.
1334 for (unsigned i = 0, e = NumberedTypes.size(); i != e; ++i) {
1335 Out << "\ttype ";
1336
1337 // Make sure we print out at least one level of the type structure, so
1338 // that we do not get %2 = type %2
1339 TypePrinter.printAtLeastOneLevel(NumberedTypes[i], Out);
1340 Out << "\t\t; type %" << i << '\n';
1341 }
1342
1343 // Print the named types.
Reid Spencer78d033e2007-01-06 07:24:44 +00001344 for (TypeSymbolTable::const_iterator TI = ST.begin(), TE = ST.end();
1345 TI != TE; ++TI) {
Chris Lattner52b26de2008-08-19 05:16:28 +00001346 Out << '\t';
1347 PrintLLVMName(Out, &TI->first[0], TI->first.size(), LocalPrefix);
1348 Out << " = type ";
Reid Spencer9231ac82004-05-25 08:53:40 +00001349
1350 // Make sure we print out at least one level of the type structure, so
1351 // that we do not get %FILE = type %FILE
Chris Lattner0f7364b2009-02-28 21:26:53 +00001352 TypePrinter.printAtLeastOneLevel(TI->second, Out);
Chris Lattnercfb5a202008-08-19 05:06:27 +00001353 Out << '\n';
Reid Spencer9231ac82004-05-25 08:53:40 +00001354 }
Reid Spencer78d033e2007-01-06 07:24:44 +00001355}
1356
Misha Brukmanab5c6002004-03-02 00:22:19 +00001357/// printFunction - Print all aspects of a function.
1358///
Chris Lattner7e708292002-06-25 16:13:24 +00001359void AssemblyWriter::printFunction(const Function *F) {
Chris Lattnercfb5a202008-08-19 05:06:27 +00001360 // Print out the return type and name.
1361 Out << '\n';
Chris Lattner4ad02e72003-04-16 20:28:45 +00001362
Misha Brukman0313e0b2004-06-21 21:53:56 +00001363 if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out);
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001364
Reid Spencer5cbf9852007-01-30 20:08:39 +00001365 if (F->isDeclaration())
Chris Lattner3aa60662007-08-19 22:15:26 +00001366 Out << "declare ";
1367 else
Reid Spencerb951bc02006-12-29 20:29:48 +00001368 Out << "define ";
Chris Lattnercfb5a202008-08-19 05:06:27 +00001369
1370 PrintLinkage(F->getLinkage(), Out);
1371 PrintVisibility(F->getVisibility(), Out);
Chris Lattner4ad02e72003-04-16 20:28:45 +00001372
Chris Lattnerd5118982005-05-06 20:26:43 +00001373 // Print the calling convention.
1374 switch (F->getCallingConv()) {
1375 case CallingConv::C: break; // default
Anton Korobeynikovf8248682006-09-20 22:03:51 +00001376 case CallingConv::Fast: Out << "fastcc "; break;
1377 case CallingConv::Cold: Out << "coldcc "; break;
1378 case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break;
1379 case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break;
Chris Lattnerd5118982005-05-06 20:26:43 +00001380 default: Out << "cc" << F->getCallingConv() << " "; break;
1381 }
1382
Reid Spencerbd5db8e2006-12-31 05:24:50 +00001383 const FunctionType *FT = F->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00001384 const AttrListPtr &Attrs = F->getAttributes();
Devang Patel652203f2008-09-29 20:49:50 +00001385 Attributes RetAttrs = Attrs.getRetAttributes();
1386 if (RetAttrs != Attribute::None)
1387 Out << Attribute::getAsString(Attrs.getRetAttributes()) << ' ';
Chris Lattner0f7364b2009-02-28 21:26:53 +00001388 TypePrinter.print(F->getReturnType(), Out);
Chris Lattner4667b712008-08-19 05:26:17 +00001389 Out << ' ';
Chris Lattner9cc34462009-02-28 20:25:14 +00001390 WriteAsOperandInternal(Out, F, TypePrinter, &Machine);
Misha Brukman0313e0b2004-06-21 21:53:56 +00001391 Out << '(';
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001392 Machine.incorporateFunction(F);
Chris Lattner007377f2001-09-07 16:36:04 +00001393
Chris Lattnerc1824992001-10-29 16:05:51 +00001394 // Loop over the arguments, printing them...
Chris Lattner007377f2001-09-07 16:36:04 +00001395
Reid Spencerbd5db8e2006-12-31 05:24:50 +00001396 unsigned Idx = 1;
Chris Lattner8dcd2f12007-04-18 00:57:22 +00001397 if (!F->isDeclaration()) {
1398 // If this isn't a declaration, print the argument names as well.
1399 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
1400 I != E; ++I) {
1401 // Insert commas as we go... the first arg doesn't get a comma
1402 if (I != F->arg_begin()) Out << ", ";
Devang Patel19c87462008-09-26 22:53:05 +00001403 printArgument(I, Attrs.getParamAttributes(Idx));
Chris Lattner8dcd2f12007-04-18 00:57:22 +00001404 Idx++;
1405 }
1406 } else {
1407 // Otherwise, print the types from the function type.
1408 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
1409 // Insert commas as we go... the first arg doesn't get a comma
1410 if (i) Out << ", ";
1411
1412 // Output type...
Chris Lattner0f7364b2009-02-28 21:26:53 +00001413 TypePrinter.print(FT->getParamType(i), Out);
Chris Lattner8dcd2f12007-04-18 00:57:22 +00001414
Devang Patel19c87462008-09-26 22:53:05 +00001415 Attributes ArgAttrs = Attrs.getParamAttributes(i+1);
Devang Patel05988662008-09-25 21:00:45 +00001416 if (ArgAttrs != Attribute::None)
1417 Out << ' ' << Attribute::getAsString(ArgAttrs);
Chris Lattner8dcd2f12007-04-18 00:57:22 +00001418 }
Reid Spencerbd5db8e2006-12-31 05:24:50 +00001419 }
Chris Lattner007377f2001-09-07 16:36:04 +00001420
1421 // Finish printing arguments...
Chris Lattner7e708292002-06-25 16:13:24 +00001422 if (FT->isVarArg()) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001423 if (FT->getNumParams()) Out << ", ";
1424 Out << "..."; // Output varargs portion of signature!
Chris Lattner007377f2001-09-07 16:36:04 +00001425 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00001426 Out << ')';
Devang Patel19c87462008-09-26 22:53:05 +00001427 Attributes FnAttrs = Attrs.getFnAttributes();
1428 if (FnAttrs != Attribute::None)
1429 Out << ' ' << Attribute::getAsString(Attrs.getFnAttributes());
Chris Lattner60962db2005-11-12 00:10:19 +00001430 if (F->hasSection())
1431 Out << " section \"" << F->getSection() << '"';
Chris Lattner30caa282005-11-06 06:48:53 +00001432 if (F->getAlignment())
1433 Out << " align " << F->getAlignment();
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001434 if (F->hasGC())
1435 Out << " gc \"" << F->getGC() << '"';
Reid Spencer5cbf9852007-01-30 20:08:39 +00001436 if (F->isDeclaration()) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001437 Out << "\n";
Chris Lattner03e2acb2002-05-06 03:00:40 +00001438 } else {
Chris Lattnera1b58582008-04-21 06:12:55 +00001439 Out << " {";
Misha Brukmanfd939082005-04-21 23:48:37 +00001440
Chris Lattnerb5794002002-04-07 22:49:37 +00001441 // Output all of its basic blocks... for the function
Chris Lattner7e708292002-06-25 16:13:24 +00001442 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
1443 printBasicBlock(I);
Chris Lattner007377f2001-09-07 16:36:04 +00001444
Misha Brukman0313e0b2004-06-21 21:53:56 +00001445 Out << "}\n";
Chris Lattner007377f2001-09-07 16:36:04 +00001446 }
1447
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001448 Machine.purgeFunction();
Chris Lattner00950542001-06-06 20:29:01 +00001449}
1450
Misha Brukmanab5c6002004-03-02 00:22:19 +00001451/// printArgument - This member is called for every argument that is passed into
1452/// the function. Simply print it out
1453///
Dale Johannesen0d51e7e2008-02-19 21:38:47 +00001454void AssemblyWriter::printArgument(const Argument *Arg,
Devang Pateleaf42ab2008-09-23 23:03:40 +00001455 Attributes Attrs) {
Chris Lattner00950542001-06-06 20:29:01 +00001456 // Output type...
Chris Lattner0f7364b2009-02-28 21:26:53 +00001457 TypePrinter.print(Arg->getType(), Out);
Misha Brukmanfd939082005-04-21 23:48:37 +00001458
Duncan Sandsdc024672007-11-27 13:23:08 +00001459 // Output parameter attributes list
Devang Patel05988662008-09-25 21:00:45 +00001460 if (Attrs != Attribute::None)
1461 Out << ' ' << Attribute::getAsString(Attrs);
Reid Spencerbd5db8e2006-12-31 05:24:50 +00001462
Chris Lattner00950542001-06-06 20:29:01 +00001463 // Output name, if available...
Chris Lattnerc97536e2008-08-17 04:40:13 +00001464 if (Arg->hasName()) {
1465 Out << ' ';
1466 PrintLLVMName(Out, Arg);
1467 }
Chris Lattner00950542001-06-06 20:29:01 +00001468}
1469
Misha Brukmanab5c6002004-03-02 00:22:19 +00001470/// printBasicBlock - This member is called for each basic block in a method.
1471///
Chris Lattnerc1824992001-10-29 16:05:51 +00001472void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Nick Lewycky280a6e62008-04-25 16:53:59 +00001473 if (BB->hasName()) { // Print out the label if it exists...
Chris Lattnerc97536e2008-08-17 04:40:13 +00001474 Out << "\n";
Chris Lattner52b26de2008-08-19 05:16:28 +00001475 PrintLLVMName(Out, BB->getNameStart(), BB->getNameLen(), LabelPrefix);
Chris Lattnerc97536e2008-08-17 04:40:13 +00001476 Out << ':';
Nick Lewycky280a6e62008-04-25 16:53:59 +00001477 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Chris Lattner96c5b2f2008-04-21 04:20:33 +00001478 Out << "\n; <label>:";
Chris Lattner22379bc2007-01-11 03:54:27 +00001479 int Slot = Machine.getLocalSlot(BB);
Chris Lattner69566452004-06-09 19:41:19 +00001480 if (Slot != -1)
Misha Brukman0313e0b2004-06-21 21:53:56 +00001481 Out << Slot;
Chris Lattner69566452004-06-09 19:41:19 +00001482 else
Misha Brukman0313e0b2004-06-21 21:53:56 +00001483 Out << "<badref>";
Chris Lattner061269b2002-10-02 19:38:55 +00001484 }
Chris Lattner4e4d8622003-11-20 00:09:43 +00001485
1486 if (BB->getParent() == 0)
Misha Brukman0313e0b2004-06-21 21:53:56 +00001487 Out << "\t\t; Error: Block without parent!";
Chris Lattnereb411292008-04-22 02:45:44 +00001488 else if (BB != &BB->getParent()->getEntryBlock()) { // Not the entry block?
1489 // Output predecessors for the block...
1490 Out << "\t\t;";
1491 pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
1492
1493 if (PI == PE) {
1494 Out << " No predecessors!";
1495 } else {
Dan Gohman8dae1382008-09-14 17:21:12 +00001496 Out << " preds = ";
Chris Lattnereb411292008-04-22 02:45:44 +00001497 writeOperand(*PI, false);
1498 for (++PI; PI != PE; ++PI) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001499 Out << ", ";
Chris Lattner2fcfdb72006-12-06 06:24:27 +00001500 writeOperand(*PI, false);
Chris Lattner40efcec2003-11-16 22:59:57 +00001501 }
Chris Lattner061269b2002-10-02 19:38:55 +00001502 }
Chris Lattner00950542001-06-06 20:29:01 +00001503 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001504
Chris Lattnereb411292008-04-22 02:45:44 +00001505 Out << "\n";
Chris Lattner00950542001-06-06 20:29:01 +00001506
Misha Brukman0313e0b2004-06-21 21:53:56 +00001507 if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out);
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001508
Chris Lattner007377f2001-09-07 16:36:04 +00001509 // Output all of the instructions in the basic block...
Chris Lattner7e708292002-06-25 16:13:24 +00001510 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
1511 printInstruction(*I);
Chris Lattner9f717ef2004-03-08 18:51:45 +00001512
Misha Brukman0313e0b2004-06-21 21:53:56 +00001513 if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out);
Chris Lattner00950542001-06-06 20:29:01 +00001514}
1515
Chris Lattnere02fa852001-10-13 06:42:36 +00001516
Misha Brukmanab5c6002004-03-02 00:22:19 +00001517/// printInfoComment - Print a little comment after the instruction indicating
1518/// which slot it occupies.
1519///
Chris Lattner7e708292002-06-25 16:13:24 +00001520void AssemblyWriter::printInfoComment(const Value &V) {
1521 if (V.getType() != Type::VoidTy) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001522 Out << "\t\t; <";
Chris Lattner0f7364b2009-02-28 21:26:53 +00001523 TypePrinter.print(V.getType(), Out);
Chris Lattner4667b712008-08-19 05:26:17 +00001524 Out << '>';
Chris Lattnere02fa852001-10-13 06:42:36 +00001525
Chris Lattner828db8a2008-08-29 17:19:30 +00001526 if (!V.hasName() && !isa<Instruction>(V)) {
Chris Lattner22379bc2007-01-11 03:54:27 +00001527 int SlotNum;
1528 if (const GlobalValue *GV = dyn_cast<GlobalValue>(&V))
1529 SlotNum = Machine.getGlobalSlot(GV);
1530 else
1531 SlotNum = Machine.getLocalSlot(&V);
Chris Lattner69566452004-06-09 19:41:19 +00001532 if (SlotNum == -1)
Misha Brukman0313e0b2004-06-21 21:53:56 +00001533 Out << ":<badref>";
Reid Spencerfc621e22004-06-09 15:26:53 +00001534 else
Misha Brukman0313e0b2004-06-21 21:53:56 +00001535 Out << ':' << SlotNum; // Print out the def slot taken.
Chris Lattnere02fa852001-10-13 06:42:36 +00001536 }
Chris Lattner5c461402005-02-01 01:24:01 +00001537 Out << " [#uses=" << V.getNumUses() << ']'; // Output # uses
Chris Lattnere02fa852001-10-13 06:42:36 +00001538 }
1539}
1540
Reid Spencer3a9ec242006-08-28 01:02:49 +00001541// This member is called for each Instruction in a function..
Chris Lattner7e708292002-06-25 16:13:24 +00001542void AssemblyWriter::printInstruction(const Instruction &I) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001543 if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out);
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001544
Chris Lattnerab49ee72008-08-23 22:52:27 +00001545 Out << '\t';
Chris Lattner00950542001-06-06 20:29:01 +00001546
1547 // Print out name if it exists...
Chris Lattnerc97536e2008-08-17 04:40:13 +00001548 if (I.hasName()) {
1549 PrintLLVMName(Out, &I);
1550 Out << " = ";
Chris Lattner828db8a2008-08-29 17:19:30 +00001551 } else if (I.getType() != Type::VoidTy) {
1552 // Print out the def slot taken.
1553 int SlotNum = Machine.getLocalSlot(&I);
1554 if (SlotNum == -1)
1555 Out << "<badref> = ";
1556 else
1557 Out << '%' << SlotNum << " = ";
Chris Lattnerc97536e2008-08-17 04:40:13 +00001558 }
Chris Lattner00950542001-06-06 20:29:01 +00001559
Chris Lattnerddb6db42005-05-06 05:51:46 +00001560 // If this is a volatile load or store, print out the volatile marker.
Chris Lattnere5e475e2003-09-08 17:45:59 +00001561 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
Chris Lattnerddb6db42005-05-06 05:51:46 +00001562 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile())) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001563 Out << "volatile ";
Chris Lattnerddb6db42005-05-06 05:51:46 +00001564 } else if (isa<CallInst>(I) && cast<CallInst>(I).isTailCall()) {
1565 // If this is a call, check if it's a tail call.
1566 Out << "tail ";
1567 }
Chris Lattnere5e475e2003-09-08 17:45:59 +00001568
Chris Lattner00950542001-06-06 20:29:01 +00001569 // Print out the opcode...
Misha Brukman0313e0b2004-06-21 21:53:56 +00001570 Out << I.getOpcodeName();
Chris Lattner00950542001-06-06 20:29:01 +00001571
Reid Spencer74f16422006-12-03 06:27:29 +00001572 // Print out the compare instruction predicates
Nate Begemanac80ade2008-05-12 19:01:56 +00001573 if (const CmpInst *CI = dyn_cast<CmpInst>(&I))
Chris Lattnerab49ee72008-08-23 22:52:27 +00001574 Out << ' ' << getPredicateText(CI->getPredicate());
Reid Spencer74f16422006-12-03 06:27:29 +00001575
Chris Lattner00950542001-06-06 20:29:01 +00001576 // Print out the type of the operands...
Chris Lattner7e708292002-06-25 16:13:24 +00001577 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
Chris Lattner00950542001-06-06 20:29:01 +00001578
1579 // Special case conditional branches to swizzle the condition out to the front
Gabor Greifccd27fb2009-02-09 15:45:06 +00001580 if (isa<BranchInst>(I) && cast<BranchInst>(I).isConditional()) {
1581 BranchInst &BI(cast<BranchInst>(I));
Dan Gohman8dae1382008-09-14 17:21:12 +00001582 Out << ' ';
Gabor Greifccd27fb2009-02-09 15:45:06 +00001583 writeOperand(BI.getCondition(), true);
Dan Gohman8dae1382008-09-14 17:21:12 +00001584 Out << ", ";
Gabor Greifccd27fb2009-02-09 15:45:06 +00001585 writeOperand(BI.getSuccessor(0), true);
Dan Gohman8dae1382008-09-14 17:21:12 +00001586 Out << ", ";
Gabor Greifccd27fb2009-02-09 15:45:06 +00001587 writeOperand(BI.getSuccessor(1), true);
Chris Lattner00950542001-06-06 20:29:01 +00001588
Chris Lattner94dc1f22002-04-13 18:34:38 +00001589 } else if (isa<SwitchInst>(I)) {
Chris Lattner00950542001-06-06 20:29:01 +00001590 // Special case switch statement to get formatting nice and correct...
Dan Gohman8dae1382008-09-14 17:21:12 +00001591 Out << ' ';
Chris Lattnerab49ee72008-08-23 22:52:27 +00001592 writeOperand(Operand , true);
Dan Gohman8dae1382008-09-14 17:21:12 +00001593 Out << ", ";
Chris Lattnerab49ee72008-08-23 22:52:27 +00001594 writeOperand(I.getOperand(1), true);
1595 Out << " [";
Chris Lattner00950542001-06-06 20:29:01 +00001596
Chris Lattner7e708292002-06-25 16:13:24 +00001597 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001598 Out << "\n\t\t";
Chris Lattnerab49ee72008-08-23 22:52:27 +00001599 writeOperand(I.getOperand(op ), true);
Dan Gohman8dae1382008-09-14 17:21:12 +00001600 Out << ", ";
Chris Lattner7e708292002-06-25 16:13:24 +00001601 writeOperand(I.getOperand(op+1), true);
Chris Lattner00950542001-06-06 20:29:01 +00001602 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00001603 Out << "\n\t]";
Chris Lattnerb00c5822001-10-02 03:41:24 +00001604 } else if (isa<PHINode>(I)) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001605 Out << ' ';
Chris Lattner0f7364b2009-02-28 21:26:53 +00001606 TypePrinter.print(I.getType(), Out);
Misha Brukman0313e0b2004-06-21 21:53:56 +00001607 Out << ' ';
Chris Lattner00950542001-06-06 20:29:01 +00001608
Chris Lattner7e708292002-06-25 16:13:24 +00001609 for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001610 if (op) Out << ", ";
Dan Gohman8dae1382008-09-14 17:21:12 +00001611 Out << "[ ";
1612 writeOperand(I.getOperand(op ), false); Out << ", ";
Misha Brukman0313e0b2004-06-21 21:53:56 +00001613 writeOperand(I.getOperand(op+1), false); Out << " ]";
Chris Lattnerc24d2082001-06-11 15:04:20 +00001614 }
Dan Gohman995be7d2008-05-31 19:12:39 +00001615 } else if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(&I)) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001616 Out << ' ';
Dan Gohman995be7d2008-05-31 19:12:39 +00001617 writeOperand(I.getOperand(0), true);
1618 for (const unsigned *i = EVI->idx_begin(), *e = EVI->idx_end(); i != e; ++i)
1619 Out << ", " << *i;
1620 } else if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(&I)) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001621 Out << ' ';
1622 writeOperand(I.getOperand(0), true); Out << ", ";
Dan Gohman995be7d2008-05-31 19:12:39 +00001623 writeOperand(I.getOperand(1), true);
1624 for (const unsigned *i = IVI->idx_begin(), *e = IVI->idx_end(); i != e; ++i)
1625 Out << ", " << *i;
Devang Patel57ef4f42008-02-23 00:35:18 +00001626 } else if (isa<ReturnInst>(I) && !Operand) {
1627 Out << " void";
Chris Lattnerd5118982005-05-06 20:26:43 +00001628 } else if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
1629 // Print the calling convention being used.
1630 switch (CI->getCallingConv()) {
1631 case CallingConv::C: break; // default
Chris Lattner0deaab82006-05-19 21:58:52 +00001632 case CallingConv::Fast: Out << " fastcc"; break;
1633 case CallingConv::Cold: Out << " coldcc"; break;
Chris Lattnerb28a6dc2007-11-18 18:32:16 +00001634 case CallingConv::X86_StdCall: Out << " x86_stdcallcc"; break;
1635 case CallingConv::X86_FastCall: Out << " x86_fastcallcc"; break;
Chris Lattnerd5118982005-05-06 20:26:43 +00001636 default: Out << " cc" << CI->getCallingConv(); break;
1637 }
1638
Reid Spencerb138a062007-04-09 06:10:42 +00001639 const PointerType *PTy = cast<PointerType>(Operand->getType());
1640 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1641 const Type *RetTy = FTy->getReturnType();
Devang Patel05988662008-09-25 21:00:45 +00001642 const AttrListPtr &PAL = CI->getAttributes();
Chris Lattner268de042001-11-06 21:28:12 +00001643
Devang Patel652203f2008-09-29 20:49:50 +00001644 if (PAL.getRetAttributes() != Attribute::None)
1645 Out << ' ' << Attribute::getAsString(PAL.getRetAttributes());
1646
Chris Lattner7a012292003-08-05 15:34:45 +00001647 // If possible, print out the short form of the call instruction. We can
Chris Lattnerb5794002002-04-07 22:49:37 +00001648 // only do this if the first argument is a pointer to a nonvararg function,
Chris Lattner7a012292003-08-05 15:34:45 +00001649 // and if the return type is not a pointer to a function.
Chris Lattner268de042001-11-06 21:28:12 +00001650 //
Dan Gohman8dae1382008-09-14 17:21:12 +00001651 Out << ' ';
Chris Lattner7a012292003-08-05 15:34:45 +00001652 if (!FTy->isVarArg() &&
Misha Brukmanfd939082005-04-21 23:48:37 +00001653 (!isa<PointerType>(RetTy) ||
Chris Lattnerc1b27182002-07-25 20:58:51 +00001654 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001655 TypePrinter.print(RetTy, Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001656 Out << ' ';
Chris Lattner268de042001-11-06 21:28:12 +00001657 writeOperand(Operand, false);
1658 } else {
1659 writeOperand(Operand, true);
1660 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00001661 Out << '(';
Reid Spencerbd5db8e2006-12-31 05:24:50 +00001662 for (unsigned op = 1, Eop = I.getNumOperands(); op < Eop; ++op) {
1663 if (op > 1)
Dan Gohman8dae1382008-09-14 17:21:12 +00001664 Out << ", ";
Devang Patel19c87462008-09-26 22:53:05 +00001665 writeParamOperand(I.getOperand(op), PAL.getParamAttributes(op));
Chris Lattner00950542001-06-06 20:29:01 +00001666 }
Dan Gohman8dae1382008-09-14 17:21:12 +00001667 Out << ')';
Devang Patel19c87462008-09-26 22:53:05 +00001668 if (PAL.getFnAttributes() != Attribute::None)
1669 Out << ' ' << Attribute::getAsString(PAL.getFnAttributes());
Chris Lattner7e708292002-06-25 16:13:24 +00001670 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Reid Spencerb138a062007-04-09 06:10:42 +00001671 const PointerType *PTy = cast<PointerType>(Operand->getType());
1672 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1673 const Type *RetTy = FTy->getReturnType();
Devang Patel05988662008-09-25 21:00:45 +00001674 const AttrListPtr &PAL = II->getAttributes();
Chris Lattner7a012292003-08-05 15:34:45 +00001675
Chris Lattnerd5118982005-05-06 20:26:43 +00001676 // Print the calling convention being used.
1677 switch (II->getCallingConv()) {
1678 case CallingConv::C: break; // default
Chris Lattner0deaab82006-05-19 21:58:52 +00001679 case CallingConv::Fast: Out << " fastcc"; break;
1680 case CallingConv::Cold: Out << " coldcc"; break;
Dan Gohman8dae1382008-09-14 17:21:12 +00001681 case CallingConv::X86_StdCall: Out << " x86_stdcallcc"; break;
1682 case CallingConv::X86_FastCall: Out << " x86_fastcallcc"; break;
Chris Lattnerd5118982005-05-06 20:26:43 +00001683 default: Out << " cc" << II->getCallingConv(); break;
1684 }
1685
Devang Patel652203f2008-09-29 20:49:50 +00001686 if (PAL.getRetAttributes() != Attribute::None)
1687 Out << ' ' << Attribute::getAsString(PAL.getRetAttributes());
1688
Chris Lattner7a012292003-08-05 15:34:45 +00001689 // If possible, print out the short form of the invoke instruction. We can
1690 // only do this if the first argument is a pointer to a nonvararg function,
1691 // and if the return type is not a pointer to a function.
1692 //
Dan Gohman2b6c3d92008-10-15 18:02:08 +00001693 Out << ' ';
Chris Lattner7a012292003-08-05 15:34:45 +00001694 if (!FTy->isVarArg() &&
Misha Brukmanfd939082005-04-21 23:48:37 +00001695 (!isa<PointerType>(RetTy) ||
Chris Lattner7a012292003-08-05 15:34:45 +00001696 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001697 TypePrinter.print(RetTy, Out);
Dan Gohman2b6c3d92008-10-15 18:02:08 +00001698 Out << ' ';
Chris Lattner7a012292003-08-05 15:34:45 +00001699 writeOperand(Operand, false);
1700 } else {
1701 writeOperand(Operand, true);
1702 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00001703 Out << '(';
Bill Wendling9a507cd2009-03-13 21:15:59 +00001704 for (unsigned op = 3, Eop = I.getNumOperands(); op < Eop; ++op) {
1705 if (op > 3)
Dan Gohman8dae1382008-09-14 17:21:12 +00001706 Out << ", ";
Bill Wendling9a507cd2009-03-13 21:15:59 +00001707 writeParamOperand(I.getOperand(op), PAL.getParamAttributes(op-2));
Chris Lattnere02fa852001-10-13 06:42:36 +00001708 }
1709
Dan Gohman8dae1382008-09-14 17:21:12 +00001710 Out << ')';
Devang Patel19c87462008-09-26 22:53:05 +00001711 if (PAL.getFnAttributes() != Attribute::None)
1712 Out << ' ' << Attribute::getAsString(PAL.getFnAttributes());
1713
Dan Gohman8dae1382008-09-14 17:21:12 +00001714 Out << "\n\t\t\tto ";
Chris Lattnere02fa852001-10-13 06:42:36 +00001715 writeOperand(II->getNormalDest(), true);
Dan Gohman8dae1382008-09-14 17:21:12 +00001716 Out << " unwind ";
Chris Lattneraeb2a1d2004-02-08 21:44:31 +00001717 writeOperand(II->getUnwindDest(), true);
Chris Lattnere02fa852001-10-13 06:42:36 +00001718
Chris Lattner7e708292002-06-25 16:13:24 +00001719 } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(&I)) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001720 Out << ' ';
Chris Lattner0f7364b2009-02-28 21:26:53 +00001721 TypePrinter.print(AI->getType()->getElementType(), Out);
Chris Lattner94dc1f22002-04-13 18:34:38 +00001722 if (AI->isArrayAllocation()) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001723 Out << ", ";
Chris Lattner94dc1f22002-04-13 18:34:38 +00001724 writeOperand(AI->getArraySize(), true);
Chris Lattner00950542001-06-06 20:29:01 +00001725 }
Nate Begeman14b05292005-11-05 09:21:28 +00001726 if (AI->getAlignment()) {
Chris Lattner9fad0b92005-11-05 21:20:34 +00001727 Out << ", align " << AI->getAlignment();
Nate Begeman14b05292005-11-05 09:21:28 +00001728 }
Chris Lattnere02fa852001-10-13 06:42:36 +00001729 } else if (isa<CastInst>(I)) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001730 if (Operand) {
1731 Out << ' ';
1732 writeOperand(Operand, true); // Work with broken code
1733 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00001734 Out << " to ";
Chris Lattner0f7364b2009-02-28 21:26:53 +00001735 TypePrinter.print(I.getType(), Out);
Chris Lattner4d45bd02003-10-18 05:57:43 +00001736 } else if (isa<VAArgInst>(I)) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001737 if (Operand) {
1738 Out << ' ';
1739 writeOperand(Operand, true); // Work with broken code
1740 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00001741 Out << ", ";
Chris Lattner0f7364b2009-02-28 21:26:53 +00001742 TypePrinter.print(I.getType(), Out);
1743 } else if (Operand) { // Print the normal way.
Chris Lattner00950542001-06-06 20:29:01 +00001744
Misha Brukmanfd939082005-04-21 23:48:37 +00001745 // PrintAllTypes - Instructions who have operands of all the same type
Chris Lattner00950542001-06-06 20:29:01 +00001746 // omit the type from all but the first operand. If the instruction has
1747 // different type operands (for example br), then they are all printed.
1748 bool PrintAllTypes = false;
1749 const Type *TheType = Operand->getType();
Chris Lattner00950542001-06-06 20:29:01 +00001750
Reid Spencerebe57e32007-02-02 13:54:55 +00001751 // Select, Store and ShuffleVector always print all types.
Devang Patel64947682008-03-04 22:05:14 +00001752 if (isa<SelectInst>(I) || isa<StoreInst>(I) || isa<ShuffleVectorInst>(I)
1753 || isa<ReturnInst>(I)) {
Chris Lattnerffd9bf42003-04-16 20:20:02 +00001754 PrintAllTypes = true;
1755 } else {
1756 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
1757 Operand = I.getOperand(i);
Nuno Lopes6ad2b2a2009-01-15 18:40:57 +00001758 // note that Operand shouldn't be null, but the test helps make dump()
1759 // more tolerant of malformed IR
Nuno Lopesa8c78a92009-01-14 17:51:41 +00001760 if (Operand && Operand->getType() != TheType) {
Chris Lattnerffd9bf42003-04-16 20:20:02 +00001761 PrintAllTypes = true; // We have differing types! Print them all!
1762 break;
1763 }
Chris Lattner00950542001-06-06 20:29:01 +00001764 }
1765 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001766
Chris Lattnerc1824992001-10-29 16:05:51 +00001767 if (!PrintAllTypes) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001768 Out << ' ';
Chris Lattner0f7364b2009-02-28 21:26:53 +00001769 TypePrinter.print(TheType, Out);
Chris Lattnerc1824992001-10-29 16:05:51 +00001770 }
Chris Lattner00950542001-06-06 20:29:01 +00001771
Dan Gohman8dae1382008-09-14 17:21:12 +00001772 Out << ' ';
Chris Lattner7e708292002-06-25 16:13:24 +00001773 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001774 if (i) Out << ", ";
Chris Lattner7e708292002-06-25 16:13:24 +00001775 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner00950542001-06-06 20:29:01 +00001776 }
1777 }
Christopher Lamb43c7f372007-04-22 19:24:39 +00001778
1779 // Print post operand alignment for load/store
1780 if (isa<LoadInst>(I) && cast<LoadInst>(I).getAlignment()) {
1781 Out << ", align " << cast<LoadInst>(I).getAlignment();
1782 } else if (isa<StoreInst>(I) && cast<StoreInst>(I).getAlignment()) {
1783 Out << ", align " << cast<StoreInst>(I).getAlignment();
1784 }
Chris Lattner00950542001-06-06 20:29:01 +00001785
Chris Lattnere02fa852001-10-13 06:42:36 +00001786 printInfoComment(I);
Chris Lattnerab49ee72008-08-23 22:52:27 +00001787 Out << '\n';
Chris Lattner00950542001-06-06 20:29:01 +00001788}
1789
1790
1791//===----------------------------------------------------------------------===//
1792// External Interface declarations
1793//===----------------------------------------------------------------------===//
1794
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001795void Module::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Chris Lattner944fac72008-08-23 22:23:09 +00001796 raw_os_ostream OS(o);
1797 print(OS, AAW);
1798}
1799void Module::print(raw_ostream &OS, AssemblyAnnotationWriter *AAW) const {
Chris Lattner0d9574a2008-08-19 04:26:57 +00001800 SlotTracker SlotTable(this);
Chris Lattner944fac72008-08-23 22:23:09 +00001801 AssemblyWriter W(OS, SlotTable, this, AAW);
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001802 W.write(this);
Chris Lattner00950542001-06-06 20:29:01 +00001803}
1804
Misha Brukmanfd939082005-04-21 23:48:37 +00001805void Type::print(std::ostream &o) const {
Chris Lattner944fac72008-08-23 22:23:09 +00001806 raw_os_ostream OS(o);
1807 print(OS);
1808}
1809
Chris Lattner6d4306e2009-02-28 21:11:05 +00001810void Type::print(raw_ostream &OS) const {
1811 if (this == 0) {
1812 OS << "<null Type>";
1813 return;
1814 }
Chris Lattnere9fa33e2009-02-28 23:20:19 +00001815 TypePrinting().print(this, OS);
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001816}
1817
Chris Lattner944fac72008-08-23 22:23:09 +00001818void Value::print(raw_ostream &OS, AssemblyAnnotationWriter *AAW) const {
1819 if (this == 0) {
1820 OS << "printing a <null> value\n";
1821 return;
1822 }
1823
1824 if (const Instruction *I = dyn_cast<Instruction>(this)) {
1825 const Function *F = I->getParent() ? I->getParent()->getParent() : 0;
1826 SlotTracker SlotTable(F);
1827 AssemblyWriter W(OS, SlotTable, F ? F->getParent() : 0, AAW);
1828 W.write(I);
1829 } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(this)) {
1830 SlotTracker SlotTable(BB->getParent());
1831 AssemblyWriter W(OS, SlotTable,
1832 BB->getParent() ? BB->getParent()->getParent() : 0, AAW);
1833 W.write(BB);
1834 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(this)) {
1835 SlotTracker SlotTable(GV->getParent());
Dan Gohmanba0941f2009-04-20 16:10:33 +00001836 AssemblyWriter W(OS, SlotTable, GV->getParent(), AAW);
Chris Lattner944fac72008-08-23 22:23:09 +00001837 W.write(GV);
1838 } else if (const Constant *C = dyn_cast<Constant>(this)) {
Chris Lattnere9fa33e2009-02-28 23:20:19 +00001839 TypePrinting TypePrinter;
Chris Lattner0f7364b2009-02-28 21:26:53 +00001840 TypePrinter.print(C->getType(), OS);
Chris Lattner6d4306e2009-02-28 21:11:05 +00001841 OS << ' ';
Chris Lattner9cc34462009-02-28 20:25:14 +00001842 WriteConstantInt(OS, C, TypePrinter, 0);
Chris Lattner944fac72008-08-23 22:23:09 +00001843 } else if (const Argument *A = dyn_cast<Argument>(this)) {
1844 WriteAsOperand(OS, this, true,
1845 A->getParent() ? A->getParent()->getParent() : 0);
1846 } else if (isa<InlineAsm>(this)) {
1847 WriteAsOperand(OS, this, true, 0);
1848 } else {
Dan Gohmand71703d2008-12-03 21:37:21 +00001849 assert(0 && "Unknown value to print out!");
Chris Lattner944fac72008-08-23 22:23:09 +00001850 }
1851}
1852
1853void Value::print(std::ostream &O, AssemblyAnnotationWriter *AAW) const {
1854 raw_os_ostream OS(O);
1855 print(OS, AAW);
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001856}
1857
Chris Lattner7059e532008-08-25 17:03:15 +00001858// Value::dump - allow easy printing of Values from the debugger.
Dan Gohmanf871ccb2009-03-23 15:57:19 +00001859void Value::dump() const { print(errs()); errs() << '\n'; }
Reid Spencerfa452c02004-05-25 18:14:38 +00001860
Chris Lattner7059e532008-08-25 17:03:15 +00001861// Type::dump - allow easy printing of Types from the debugger.
Chris Lattner795daec2008-10-01 20:16:19 +00001862// This one uses type names from the given context module
1863void Type::dump(const Module *Context) const {
1864 WriteTypeSymbolic(errs(), this, Context);
1865 errs() << '\n';
Chris Lattner795daec2008-10-01 20:16:19 +00001866}
1867
Chris Lattnerc2871372009-02-28 21:05:51 +00001868// Type::dump - allow easy printing of Types from the debugger.
1869void Type::dump() const { dump(0); }
1870
Chris Lattner7059e532008-08-25 17:03:15 +00001871// Module::dump() - Allow printing of Modules from the debugger.
Dan Gohmanf871ccb2009-03-23 15:57:19 +00001872void Module::dump() const { print(errs(), 0); }