blob: 68cf438e29a55ab9c90ac5789e8ea860845023d0 [file] [log] [blame]
Chris Lattnerf7e79482002-04-07 22:31:46 +00001//===-- AsmWriter.cpp - Printing LLVM as an assembly file -----------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
10// This library implements the functionality defined in llvm/Assembly/Writer.h
11//
Chris Lattner189088e2002-04-12 18:21:53 +000012// Note that these routines must be extremely tolerant of various errors in the
Chris Lattnerf70da102003-05-08 02:44:12 +000013// LLVM code, because it can be used for debugging transformations.
Chris Lattner189088e2002-04-12 18:21:53 +000014//
Chris Lattner2f7c9632001-06-06 20:29:01 +000015//===----------------------------------------------------------------------===//
16
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +000017#include "llvm/Assembly/Writer.h"
Chris Lattner7f8845a2002-07-23 18:07:49 +000018#include "llvm/Assembly/PrintModulePass.h"
Chris Lattner8339f7d2003-10-30 23:41:03 +000019#include "llvm/Assembly/AsmAnnotationWriter.h"
Chris Lattnerf7b6d312005-05-06 20:26:43 +000020#include "llvm/CallingConv.h"
Chris Lattnerc70b3f62004-01-20 19:50:34 +000021#include "llvm/Constants.h"
Chris Lattner913d18f2002-04-29 18:46:50 +000022#include "llvm/DerivedTypes.h"
Chris Lattner8bbcda22006-01-25 18:57:27 +000023#include "llvm/InlineAsm.h"
Vikram S. Adveb952b542002-07-14 23:14:45 +000024#include "llvm/Instruction.h"
Misha Brukman2d3fa9e2004-07-29 16:53:53 +000025#include "llvm/Instructions.h"
Chris Lattnerc70b3f62004-01-20 19:50:34 +000026#include "llvm/Module.h"
Reid Spencer3aaaa0b2007-02-05 20:47:22 +000027#include "llvm/ValueSymbolTable.h"
Reid Spencer32af9e82007-01-06 07:24:44 +000028#include "llvm/TypeSymbolTable.h"
Chris Lattnera204d412008-08-17 17:25:25 +000029#include "llvm/ADT/DenseMap.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000030#include "llvm/ADT/StringExtras.h"
31#include "llvm/ADT/STLExtras.h"
Bill Wendlingdfc91892006-11-28 02:09:03 +000032#include "llvm/Support/CFG.h"
Jim Laskeyb74c6662005-08-17 19:34:49 +000033#include "llvm/Support/MathExtras.h"
Chris Lattner393b7cd2008-08-17 04:17:45 +000034#include "llvm/Support/raw_ostream.h"
Chris Lattnerfee714f2001-09-07 16:36:04 +000035#include <algorithm>
Reid Spencerbdf03b42007-05-22 19:27:35 +000036#include <cctype>
Chris Lattner189d19f2003-11-21 20:23:48 +000037using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000038
Reid Spencer294715b2005-05-15 16:13:11 +000039// Make virtual table appear in this compilation unit.
40AssemblyAnnotationWriter::~AssemblyAnnotationWriter() {}
41
Chris Lattner3eee99c2008-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 Dunbardb0b70a2008-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];
69 if (isprint(C) && C != '\\' && C != '"' && isprint(C))
70 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 Lattner3eee99c2008-08-19 04:36:02 +000082enum PrefixType {
83 GlobalPrefix,
84 LabelPrefix,
Daniel Dunbar389529a2008-10-14 23:28:09 +000085 LocalPrefix,
86 NoPrefix
Chris Lattner3eee99c2008-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 Lattner0c19df42008-08-23 22:23:09 +000092static void PrintLLVMName(raw_ostream &OS, const char *NameStr,
Chris Lattner1508d3f2008-08-19 05:16:28 +000093 unsigned NameLen, PrefixType Prefix) {
94 assert(NameStr && "Cannot get empty name!");
Chris Lattner3eee99c2008-08-19 04:36:02 +000095 switch (Prefix) {
Chris Lattner1508d3f2008-08-19 05:16:28 +000096 default: assert(0 && "Bad prefix!");
Daniel Dunbar389529a2008-10-14 23:28:09 +000097 case NoPrefix: break;
Chris Lattner1508d3f2008-08-19 05:16:28 +000098 case GlobalPrefix: OS << '@'; break;
99 case LabelPrefix: break;
100 case LocalPrefix: OS << '%'; break;
Chris Lattner3eee99c2008-08-19 04:36:02 +0000101 }
102
103 // Scan the name to see if it needs quotes first.
Daniel Dunbardb0b70a2008-10-28 19:33:02 +0000104 bool NeedsQuotes = isdigit(NameStr[0]);
Chris Lattner3eee99c2008-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 Dunbardb0b70a2008-10-28 19:33:02 +0000124 PrintEscapedString(NameStr, NameLen, OS);
Chris Lattner3eee99c2008-08-19 04:36:02 +0000125 OS << '"';
126}
127
Daniel Dunbar389529a2008-10-14 23:28:09 +0000128/// getLLVMName - Turn the specified string into an 'LLVM name', which is
129/// surrounded with ""'s and escaped if it has special chars in it.
130static std::string getLLVMName(const std::string &Name) {
131 assert(!Name.empty() && "Cannot get empty name!");
132 std::string result;
133 raw_string_ostream OS(result);
134 PrintLLVMName(OS, Name.c_str(), Name.length(), NoPrefix);
135 return OS.str();
136}
137
Chris Lattner3eee99c2008-08-19 04:36:02 +0000138/// PrintLLVMName - Turn the specified name into an 'LLVM name', which is either
139/// prefixed with % (if the string only contains simple characters) or is
140/// surrounded with ""'s (if it has special chars in it). Print it out.
Chris Lattner0c19df42008-08-23 22:23:09 +0000141static void PrintLLVMName(raw_ostream &OS, const Value *V) {
Chris Lattner1508d3f2008-08-19 05:16:28 +0000142 PrintLLVMName(OS, V->getNameStart(), V->getNameLen(),
Chris Lattner3eee99c2008-08-19 04:36:02 +0000143 isa<GlobalValue>(V) ? GlobalPrefix : LocalPrefix);
144}
145
146
147
148//===----------------------------------------------------------------------===//
149// SlotTracker Class: Enumerate slot numbers for unnamed values
150//===----------------------------------------------------------------------===//
151
Chris Lattner3ee58762008-08-19 04:28:07 +0000152namespace {
153
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000154/// This class provides computation of slot numbers for LLVM Assembly writing.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000155///
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000156class SlotTracker {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000157public:
Chris Lattner393b7cd2008-08-17 04:17:45 +0000158 /// ValueMap - A mapping of Values to slot numbers
Chris Lattnera204d412008-08-17 17:25:25 +0000159 typedef DenseMap<const Value*, unsigned> ValueMap;
Chris Lattner393b7cd2008-08-17 04:17:45 +0000160
161private:
162 /// TheModule - The module for which we are holding slot numbers
163 const Module* TheModule;
164
165 /// TheFunction - The function for which we are holding slot numbers
166 const Function* TheFunction;
167 bool FunctionProcessed;
168
169 /// mMap - The TypePlanes map for the module level data
170 ValueMap mMap;
171 unsigned mNext;
172
173 /// fMap - The TypePlanes map for the function level data
174 ValueMap fMap;
175 unsigned fNext;
176
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000177public:
Chris Lattner393b7cd2008-08-17 04:17:45 +0000178 /// Construct from a module
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000179 explicit SlotTracker(const Module *M);
Chris Lattner393b7cd2008-08-17 04:17:45 +0000180 /// Construct from a function, starting out in incorp state.
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000181 explicit SlotTracker(const Function *F);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000182
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000183 /// Return the slot number of the specified value in it's type
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000184 /// plane. If something is not in the SlotTracker, return -1.
Chris Lattner5e043322007-01-11 03:54:27 +0000185 int getLocalSlot(const Value *V);
186 int getGlobalSlot(const GlobalValue *V);
Reid Spencer8beac692004-06-09 15:26:53 +0000187
Misha Brukmanb1c93172005-04-21 23:48:37 +0000188 /// If you'd like to deal with a function instead of just a module, use
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000189 /// this method to get its data into the SlotTracker.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000190 void incorporateFunction(const Function *F) {
191 TheFunction = F;
Reid Spencerb0ac8c42004-08-16 07:46:33 +0000192 FunctionProcessed = false;
193 }
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000194
Misha Brukmanb1c93172005-04-21 23:48:37 +0000195 /// After calling incorporateFunction, use this method to remove the
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000196 /// most recently incorporated function from the SlotTracker. This
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000197 /// will reset the state of the machine back to just the module contents.
198 void purgeFunction();
199
Chris Lattner393b7cd2008-08-17 04:17:45 +0000200 // Implementation Details
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000201private:
Reid Spencer56010e42004-05-26 21:56:09 +0000202 /// This function does the actual initialization.
203 inline void initialize();
204
Chris Lattnerea862a32007-01-09 07:55:49 +0000205 /// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
206 void CreateModuleSlot(const GlobalValue *V);
207
208 /// CreateFunctionSlot - Insert the specified Value* into the slot table.
209 void CreateFunctionSlot(const Value *V);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000210
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000211 /// Add all of the module level global variables (and their initializers)
212 /// and function declarations, but not the contents of those functions.
213 void processModule();
214
Reid Spencer56010e42004-05-26 21:56:09 +0000215 /// Add all of the functions arguments, basic blocks, and instructions
216 void processFunction();
217
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000218 SlotTracker(const SlotTracker &); // DO NOT IMPLEMENT
219 void operator=(const SlotTracker &); // DO NOT IMPLEMENT
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000220};
221
Chris Lattner3ee58762008-08-19 04:28:07 +0000222} // end anonymous namespace
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000223
Chris Lattner3eee99c2008-08-19 04:36:02 +0000224
225static SlotTracker *createSlotTracker(const Value *V) {
226 if (const Argument *FA = dyn_cast<Argument>(V))
227 return new SlotTracker(FA->getParent());
228
229 if (const Instruction *I = dyn_cast<Instruction>(V))
230 return new SlotTracker(I->getParent()->getParent());
231
232 if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
233 return new SlotTracker(BB->getParent());
234
235 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
236 return new SlotTracker(GV->getParent());
237
238 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V))
239 return new SlotTracker(GA->getParent());
240
241 if (const Function *Func = dyn_cast<Function>(V))
242 return new SlotTracker(Func);
243
244 return 0;
245}
246
247#if 0
Chris Lattner604e3512008-08-19 04:47:09 +0000248#define ST_DEBUG(X) cerr << X
Chris Lattner3eee99c2008-08-19 04:36:02 +0000249#else
Chris Lattner604e3512008-08-19 04:47:09 +0000250#define ST_DEBUG(X)
Chris Lattner3eee99c2008-08-19 04:36:02 +0000251#endif
252
253// Module level constructor. Causes the contents of the Module (sans functions)
254// to be added to the slot table.
255SlotTracker::SlotTracker(const Module *M)
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000256 : TheModule(M), TheFunction(0), FunctionProcessed(false), mNext(0), fNext(0) {
Chris Lattner3eee99c2008-08-19 04:36:02 +0000257}
258
259// Function level constructor. Causes the contents of the Module and the one
260// function provided to be added to the slot table.
261SlotTracker::SlotTracker(const Function *F)
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000262 : TheModule(F ? F->getParent() : 0), TheFunction(F), FunctionProcessed(false),
263 mNext(0), fNext(0) {
Chris Lattner3eee99c2008-08-19 04:36:02 +0000264}
265
266inline void SlotTracker::initialize() {
267 if (TheModule) {
268 processModule();
269 TheModule = 0; ///< Prevent re-processing next time we're called.
270 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000271
Chris Lattner3eee99c2008-08-19 04:36:02 +0000272 if (TheFunction && !FunctionProcessed)
273 processFunction();
274}
275
276// Iterate through all the global variables, functions, and global
277// variable initializers and create slots for them.
278void SlotTracker::processModule() {
Chris Lattner604e3512008-08-19 04:47:09 +0000279 ST_DEBUG("begin processModule!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000280
281 // Add all of the unnamed global variables to the value table.
282 for (Module::const_global_iterator I = TheModule->global_begin(),
283 E = TheModule->global_end(); I != E; ++I)
284 if (!I->hasName())
285 CreateModuleSlot(I);
286
287 // Add all the unnamed functions to the table.
288 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
289 I != E; ++I)
290 if (!I->hasName())
291 CreateModuleSlot(I);
292
Chris Lattner604e3512008-08-19 04:47:09 +0000293 ST_DEBUG("end processModule!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000294}
295
296
297// Process the arguments, basic blocks, and instructions of a function.
298void SlotTracker::processFunction() {
Chris Lattner604e3512008-08-19 04:47:09 +0000299 ST_DEBUG("begin processFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000300 fNext = 0;
301
302 // Add all the function arguments with no names.
303 for(Function::const_arg_iterator AI = TheFunction->arg_begin(),
304 AE = TheFunction->arg_end(); AI != AE; ++AI)
305 if (!AI->hasName())
306 CreateFunctionSlot(AI);
307
Chris Lattner604e3512008-08-19 04:47:09 +0000308 ST_DEBUG("Inserting Instructions:\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000309
310 // Add all of the basic blocks and instructions with no names.
311 for (Function::const_iterator BB = TheFunction->begin(),
312 E = TheFunction->end(); BB != E; ++BB) {
313 if (!BB->hasName())
314 CreateFunctionSlot(BB);
315 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
316 if (I->getType() != Type::VoidTy && !I->hasName())
317 CreateFunctionSlot(I);
318 }
319
320 FunctionProcessed = true;
321
Chris Lattner604e3512008-08-19 04:47:09 +0000322 ST_DEBUG("end processFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000323}
324
325/// Clean up after incorporating a function. This is the only way to get out of
326/// the function incorporation state that affects get*Slot/Create*Slot. Function
327/// incorporation state is indicated by TheFunction != 0.
328void SlotTracker::purgeFunction() {
Chris Lattner604e3512008-08-19 04:47:09 +0000329 ST_DEBUG("begin purgeFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000330 fMap.clear(); // Simply discard the function level map
331 TheFunction = 0;
332 FunctionProcessed = false;
Chris Lattner604e3512008-08-19 04:47:09 +0000333 ST_DEBUG("end purgeFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000334}
335
336/// getGlobalSlot - Get the slot number of a global value.
337int SlotTracker::getGlobalSlot(const GlobalValue *V) {
338 // Check for uninitialized state and do lazy initialization.
339 initialize();
340
341 // Find the type plane in the module map
342 ValueMap::iterator MI = mMap.find(V);
Dan Gohman1dd27572008-10-01 19:58:59 +0000343 return MI == mMap.end() ? -1 : (int)MI->second;
Chris Lattner3eee99c2008-08-19 04:36:02 +0000344}
345
346
347/// getLocalSlot - Get the slot number for a value that is local to a function.
348int SlotTracker::getLocalSlot(const Value *V) {
349 assert(!isa<Constant>(V) && "Can't get a constant or global slot with this!");
350
351 // Check for uninitialized state and do lazy initialization.
352 initialize();
353
354 ValueMap::iterator FI = fMap.find(V);
Dan Gohman1dd27572008-10-01 19:58:59 +0000355 return FI == fMap.end() ? -1 : (int)FI->second;
Chris Lattner3eee99c2008-08-19 04:36:02 +0000356}
357
358
359/// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
360void SlotTracker::CreateModuleSlot(const GlobalValue *V) {
361 assert(V && "Can't insert a null Value into SlotTracker!");
362 assert(V->getType() != Type::VoidTy && "Doesn't need a slot!");
363 assert(!V->hasName() && "Doesn't need a slot!");
364
365 unsigned DestSlot = mNext++;
366 mMap[V] = DestSlot;
367
Chris Lattner604e3512008-08-19 04:47:09 +0000368 ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
Chris Lattner3eee99c2008-08-19 04:36:02 +0000369 DestSlot << " [");
370 // G = Global, F = Function, A = Alias, o = other
Chris Lattner604e3512008-08-19 04:47:09 +0000371 ST_DEBUG((isa<GlobalVariable>(V) ? 'G' :
Chris Lattner3eee99c2008-08-19 04:36:02 +0000372 (isa<Function>(V) ? 'F' :
373 (isa<GlobalAlias>(V) ? 'A' : 'o'))) << "]\n");
374}
375
376
377/// CreateSlot - Create a new slot for the specified value if it has no name.
378void SlotTracker::CreateFunctionSlot(const Value *V) {
379 assert(V->getType() != Type::VoidTy && !V->hasName() &&
380 "Doesn't need a slot!");
381
382 unsigned DestSlot = fNext++;
383 fMap[V] = DestSlot;
384
385 // G = Global, F = Function, o = other
Chris Lattner604e3512008-08-19 04:47:09 +0000386 ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
Chris Lattner3eee99c2008-08-19 04:36:02 +0000387 DestSlot << " [o]\n");
388}
389
390
391
392//===----------------------------------------------------------------------===//
393// AsmWriter Implementation
394//===----------------------------------------------------------------------===//
Chris Lattner7f8845a2002-07-23 18:07:49 +0000395
Chris Lattner0c19df42008-08-23 22:23:09 +0000396static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
Chris Lattnera9f0a112006-12-06 05:50:41 +0000397 std::map<const Type *, std::string> &TypeTable,
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000398 SlotTracker *Machine);
Reid Spencer58d30f22004-07-04 11:50:43 +0000399
Chris Lattner033935d2008-08-17 04:40:13 +0000400
Chris Lattnerb86620e2001-10-29 16:37:48 +0000401
Misha Brukmanc566ca362004-03-02 00:22:19 +0000402/// fillTypeNameTable - If the module has a symbol table, take all global types
403/// and stuff their names into the TypeNames map.
404///
Chris Lattnerb86620e2001-10-29 16:37:48 +0000405static void fillTypeNameTable(const Module *M,
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000406 std::map<const Type *, std::string> &TypeNames) {
Chris Lattner98cf1f52002-11-20 18:36:02 +0000407 if (!M) return;
Reid Spencer32af9e82007-01-06 07:24:44 +0000408 const TypeSymbolTable &ST = M->getTypeSymbolTable();
409 TypeSymbolTable::const_iterator TI = ST.begin();
410 for (; TI != ST.end(); ++TI) {
Reid Spencere7e96712004-05-25 08:53:40 +0000411 // As a heuristic, don't insert pointer to primitive types, because
412 // they are used too often to have a single useful name.
413 //
414 const Type *Ty = cast<Type>(TI->second);
415 if (!isa<PointerType>(Ty) ||
Reid Spencer56010e42004-05-26 21:56:09 +0000416 !cast<PointerType>(Ty)->getElementType()->isPrimitiveType() ||
Chris Lattner03c49532007-01-15 02:27:26 +0000417 !cast<PointerType>(Ty)->getElementType()->isInteger() ||
Reid Spencer56010e42004-05-26 21:56:09 +0000418 isa<OpaqueType>(cast<PointerType>(Ty)->getElementType()))
Chris Lattner585297e82008-08-19 05:26:17 +0000419 TypeNames.insert(std::make_pair(Ty, '%' + getLLVMName(TI->first)));
Chris Lattnerb86620e2001-10-29 16:37:48 +0000420 }
421}
422
423
424
Misha Brukmanb1c93172005-04-21 23:48:37 +0000425static void calcTypeName(const Type *Ty,
John Criswellcd116ba2004-06-01 14:54:08 +0000426 std::vector<const Type *> &TypeStack,
427 std::map<const Type *, std::string> &TypeNames,
Chris Lattner585297e82008-08-19 05:26:17 +0000428 std::string &Result) {
Chris Lattner03c49532007-01-15 02:27:26 +0000429 if (Ty->isInteger() || (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty))) {
John Criswellcd116ba2004-06-01 14:54:08 +0000430 Result += Ty->getDescription(); // Base case
431 return;
432 }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000433
434 // Check to see if the type is named.
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000435 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
John Criswellcd116ba2004-06-01 14:54:08 +0000436 if (I != TypeNames.end()) {
437 Result += I->second;
438 return;
439 }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000440
John Criswellcd116ba2004-06-01 14:54:08 +0000441 if (isa<OpaqueType>(Ty)) {
442 Result += "opaque";
443 return;
444 }
Chris Lattnerf14ead92003-10-30 00:22:33 +0000445
Chris Lattnerb86620e2001-10-29 16:37:48 +0000446 // Check to see if the Type is already on the stack...
447 unsigned Slot = 0, CurSize = TypeStack.size();
448 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
449
Misha Brukmanb1c93172005-04-21 23:48:37 +0000450 // This is another base case for the recursion. In this case, we know
Chris Lattnerb86620e2001-10-29 16:37:48 +0000451 // that we have looped back to a type that we have previously visited.
452 // Generate the appropriate upreference to handle this.
John Criswellcd116ba2004-06-01 14:54:08 +0000453 if (Slot < CurSize) {
454 Result += "\\" + utostr(CurSize-Slot); // Here's the upreference
455 return;
456 }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000457
458 TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
Misha Brukmanb1c93172005-04-21 23:48:37 +0000459
Chris Lattner6b727592004-06-17 18:19:28 +0000460 switch (Ty->getTypeID()) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000461 case Type::IntegerTyID: {
462 unsigned BitWidth = cast<IntegerType>(Ty)->getBitWidth();
Reid Spencerc8721592007-01-12 07:25:20 +0000463 Result += "i" + utostr(BitWidth);
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000464 break;
465 }
Chris Lattner91db5822002-03-29 03:44:36 +0000466 case Type::FunctionTyID: {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000467 const FunctionType *FTy = cast<FunctionType>(Ty);
John Criswellcd116ba2004-06-01 14:54:08 +0000468 calcTypeName(FTy->getReturnType(), TypeStack, TypeNames, Result);
469 Result += " (";
Chris Lattnerfa829be2004-02-09 04:14:01 +0000470 for (FunctionType::param_iterator I = FTy->param_begin(),
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000471 E = FTy->param_end(); I != E; ++I) {
Chris Lattnerfa829be2004-02-09 04:14:01 +0000472 if (I != FTy->param_begin())
Chris Lattnerb86620e2001-10-29 16:37:48 +0000473 Result += ", ";
John Criswellcd116ba2004-06-01 14:54:08 +0000474 calcTypeName(*I, TypeStack, TypeNames, Result);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000475 }
Chris Lattnerd816b532002-04-13 20:53:41 +0000476 if (FTy->isVarArg()) {
Chris Lattnerfa829be2004-02-09 04:14:01 +0000477 if (FTy->getNumParams()) Result += ", ";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000478 Result += "...";
479 }
480 Result += ")";
481 break;
482 }
483 case Type::StructTyID: {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000484 const StructType *STy = cast<StructType>(Ty);
Andrew Lenharthdcb3c972006-12-08 18:06:16 +0000485 if (STy->isPacked())
486 Result += '<';
John Criswellcd116ba2004-06-01 14:54:08 +0000487 Result += "{ ";
Chris Lattnerac6db752004-02-09 04:37:31 +0000488 for (StructType::element_iterator I = STy->element_begin(),
489 E = STy->element_end(); I != E; ++I) {
John Criswellcd116ba2004-06-01 14:54:08 +0000490 calcTypeName(*I, TypeStack, TypeNames, Result);
Dan Gohmanbb9a2112008-09-25 17:37:20 +0000491 if (next(I) != STy->element_end())
492 Result += ',';
493 Result += ' ';
Chris Lattnerb86620e2001-10-29 16:37:48 +0000494 }
Dan Gohmanbb9a2112008-09-25 17:37:20 +0000495 Result += '}';
Andrew Lenharthdcb3c972006-12-08 18:06:16 +0000496 if (STy->isPacked())
497 Result += '>';
Chris Lattnerb86620e2001-10-29 16:37:48 +0000498 break;
499 }
Christopher Lamb54dd24c2007-12-11 08:59:05 +0000500 case Type::PointerTyID: {
501 const PointerType *PTy = cast<PointerType>(Ty);
Chris Lattner585297e82008-08-19 05:26:17 +0000502 calcTypeName(PTy->getElementType(), TypeStack, TypeNames, Result);
Christopher Lamb54dd24c2007-12-11 08:59:05 +0000503 if (unsigned AddressSpace = PTy->getAddressSpace())
504 Result += " addrspace(" + utostr(AddressSpace) + ")";
John Criswellcd116ba2004-06-01 14:54:08 +0000505 Result += "*";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000506 break;
Christopher Lamb54dd24c2007-12-11 08:59:05 +0000507 }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000508 case Type::ArrayTyID: {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000509 const ArrayType *ATy = cast<ArrayType>(Ty);
John Criswellcd116ba2004-06-01 14:54:08 +0000510 Result += "[" + utostr(ATy->getNumElements()) + " x ";
511 calcTypeName(ATy->getElementType(), TypeStack, TypeNames, Result);
512 Result += "]";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000513 break;
514 }
Reid Spencerd84d35b2007-02-15 02:26:10 +0000515 case Type::VectorTyID: {
516 const VectorType *PTy = cast<VectorType>(Ty);
Brian Gaeke02209042004-08-20 06:00:58 +0000517 Result += "<" + utostr(PTy->getNumElements()) + " x ";
518 calcTypeName(PTy->getElementType(), TypeStack, TypeNames, Result);
519 Result += ">";
520 break;
521 }
Chris Lattner15285ab2003-05-14 17:50:47 +0000522 case Type::OpaqueTyID:
John Criswellcd116ba2004-06-01 14:54:08 +0000523 Result += "opaque";
Chris Lattner15285ab2003-05-14 17:50:47 +0000524 break;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000525 default:
John Criswellcd116ba2004-06-01 14:54:08 +0000526 Result += "<unrecognized-type>";
Chris Lattnerfc9f1c92006-12-06 06:40:49 +0000527 break;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000528 }
529
530 TypeStack.pop_back(); // Remove self from stack...
Chris Lattnerb86620e2001-10-29 16:37:48 +0000531}
532
533
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000534/// printTypeInt - The internal guts of printing out a type that has a
535/// potentially named portion.
536///
Chris Lattner0c19df42008-08-23 22:23:09 +0000537static void printTypeInt(raw_ostream &Out, const Type *Ty,
Chris Lattner585297e82008-08-19 05:26:17 +0000538 std::map<const Type *, std::string> &TypeNames) {
Chris Lattnerb86620e2001-10-29 16:37:48 +0000539 // Primitive types always print out their description, regardless of whether
540 // they have been named or not.
541 //
Chris Lattner585297e82008-08-19 05:26:17 +0000542 if (Ty->isInteger() || (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty))) {
543 Out << Ty->getDescription();
544 return;
545 }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000546
547 // Check to see if the type is named.
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000548 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
Chris Lattner585297e82008-08-19 05:26:17 +0000549 if (I != TypeNames.end()) {
550 Out << I->second;
551 return;
552 }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000553
554 // Otherwise we have a type that has not been named but is a derived type.
555 // Carefully recurse the type hierarchy to print out any contained symbolic
556 // names.
557 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000558 std::vector<const Type *> TypeStack;
John Criswellcd116ba2004-06-01 14:54:08 +0000559 std::string TypeName;
560 calcTypeName(Ty, TypeStack, TypeNames, TypeName);
Chris Lattner7f74a562002-01-20 22:54:45 +0000561 TypeNames.insert(std::make_pair(Ty, TypeName));//Cache type name for later use
Chris Lattner585297e82008-08-19 05:26:17 +0000562 Out << TypeName;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000563}
564
Chris Lattner34b95182001-10-31 04:33:19 +0000565
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000566/// WriteTypeSymbolic - This attempts to write the specified type as a symbolic
567/// type, iff there is an entry in the modules symbol table for the specified
568/// type or one of it's component types. This is slower than a simple x << Type
569///
Chris Lattner604e3512008-08-19 04:47:09 +0000570void llvm::WriteTypeSymbolic(std::ostream &Out, const Type *Ty,
571 const Module *M) {
Chris Lattner0c19df42008-08-23 22:23:09 +0000572 raw_os_ostream RO(Out);
573 WriteTypeSymbolic(RO, Ty, M);
574}
575
576void llvm::WriteTypeSymbolic(raw_ostream &Out, const Type *Ty, const Module *M){
Misha Brukmanb1c93172005-04-21 23:48:37 +0000577 Out << ' ';
Chris Lattnerb86620e2001-10-29 16:37:48 +0000578
Chris Lattnerfc9f1c92006-12-06 06:40:49 +0000579 // If they want us to print out a type, but there is no context, we can't
580 // print it symbolically.
Chris Lattner604e3512008-08-19 04:47:09 +0000581 if (!M) {
582 Out << Ty->getDescription();
583 } else {
584 std::map<const Type *, std::string> TypeNames;
585 fillTypeNameTable(M, TypeNames);
586 printTypeInt(Out, Ty, TypeNames);
587 }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000588}
589
Chris Lattnerfc9f1c92006-12-06 06:40:49 +0000590static const char *getPredicateText(unsigned predicate) {
Reid Spencer812a1be2006-12-04 05:19:18 +0000591 const char * pred = "unknown";
592 switch (predicate) {
593 case FCmpInst::FCMP_FALSE: pred = "false"; break;
594 case FCmpInst::FCMP_OEQ: pred = "oeq"; break;
595 case FCmpInst::FCMP_OGT: pred = "ogt"; break;
596 case FCmpInst::FCMP_OGE: pred = "oge"; break;
597 case FCmpInst::FCMP_OLT: pred = "olt"; break;
598 case FCmpInst::FCMP_OLE: pred = "ole"; break;
599 case FCmpInst::FCMP_ONE: pred = "one"; break;
600 case FCmpInst::FCMP_ORD: pred = "ord"; break;
601 case FCmpInst::FCMP_UNO: pred = "uno"; break;
602 case FCmpInst::FCMP_UEQ: pred = "ueq"; break;
603 case FCmpInst::FCMP_UGT: pred = "ugt"; break;
604 case FCmpInst::FCMP_UGE: pred = "uge"; break;
605 case FCmpInst::FCMP_ULT: pred = "ult"; break;
606 case FCmpInst::FCMP_ULE: pred = "ule"; break;
607 case FCmpInst::FCMP_UNE: pred = "une"; break;
608 case FCmpInst::FCMP_TRUE: pred = "true"; break;
609 case ICmpInst::ICMP_EQ: pred = "eq"; break;
610 case ICmpInst::ICMP_NE: pred = "ne"; break;
611 case ICmpInst::ICMP_SGT: pred = "sgt"; break;
612 case ICmpInst::ICMP_SGE: pred = "sge"; break;
613 case ICmpInst::ICMP_SLT: pred = "slt"; break;
614 case ICmpInst::ICMP_SLE: pred = "sle"; break;
615 case ICmpInst::ICMP_UGT: pred = "ugt"; break;
616 case ICmpInst::ICMP_UGE: pred = "uge"; break;
617 case ICmpInst::ICMP_ULT: pred = "ult"; break;
618 case ICmpInst::ICMP_ULE: pred = "ule"; break;
619 }
620 return pred;
621}
622
Chris Lattner0c19df42008-08-23 22:23:09 +0000623static void WriteConstantInt(raw_ostream &Out, const Constant *CV,
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000624 std::map<const Type *, std::string> &TypeTable,
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000625 SlotTracker *Machine) {
Zhou Sheng75b871f2007-01-11 12:24:14 +0000626 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Chris Lattner17f71652008-08-17 07:19:36 +0000627 if (CI->getType() == Type::Int1Ty) {
Reid Spencercddc9df2007-01-12 04:24:46 +0000628 Out << (CI->getZExtValue() ? "true" : "false");
Chris Lattner17f71652008-08-17 07:19:36 +0000629 return;
630 }
631 Out << CI->getValue();
632 return;
633 }
634
635 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Dale Johannesen028084e2007-09-12 03:30:33 +0000636 if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEdouble ||
637 &CFP->getValueAPF().getSemantics() == &APFloat::IEEEsingle) {
638 // We would like to output the FP constant value in exponential notation,
639 // but we cannot do this if doing so will lose precision. Check here to
640 // make sure that we only output it in exponential format if we can parse
641 // the value back and get the same value.
642 //
Dale Johannesen1f864982009-01-21 20:32:55 +0000643 bool ignored;
Dale Johannesen028084e2007-09-12 03:30:33 +0000644 bool isDouble = &CFP->getValueAPF().getSemantics()==&APFloat::IEEEdouble;
Chris Lattner17f71652008-08-17 07:19:36 +0000645 double Val = isDouble ? CFP->getValueAPF().convertToDouble() :
646 CFP->getValueAPF().convertToFloat();
Dale Johannesen028084e2007-09-12 03:30:33 +0000647 std::string StrVal = ftostr(CFP->getValueAPF());
Chris Lattner1e194682002-04-18 18:53:13 +0000648
Dale Johannesen028084e2007-09-12 03:30:33 +0000649 // Check to make sure that the stringized number is not some string like
650 // "Inf" or NaN, that atof will accept, but the lexer will not. Check
651 // that the string matches the "[-+]?[0-9]" regex.
652 //
653 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
654 ((StrVal[0] == '-' || StrVal[0] == '+') &&
655 (StrVal[1] >= '0' && StrVal[1] <= '9'))) {
656 // Reparse stringized version!
657 if (atof(StrVal.c_str()) == Val) {
658 Out << StrVal;
659 return;
660 }
Chris Lattner1e194682002-04-18 18:53:13 +0000661 }
Dale Johannesen028084e2007-09-12 03:30:33 +0000662 // Otherwise we could not reparse it to exactly the same value, so we must
Dale Johannesen1f864982009-01-21 20:32:55 +0000663 // output the string in hexadecimal format! Note that loading and storing
664 // floating point types changes the bits of NaNs on some hosts, notably
665 // x86, so we must not use these types.
Dale Johannesen028084e2007-09-12 03:30:33 +0000666 assert(sizeof(double) == sizeof(uint64_t) &&
667 "assuming that double is 64 bits!");
Chris Lattner5505eed2008-11-10 04:30:26 +0000668 char Buffer[40];
Dale Johannesen1f864982009-01-21 20:32:55 +0000669 APFloat apf = CFP->getValueAPF();
670 // Floats are represented in ASCII IR as double, convert.
671 if (!isDouble)
672 apf.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven,
673 &ignored);
674 Out << "0x" <<
675 utohex_buffer(uint64_t(apf.bitcastToAPInt().getZExtValue()),
676 Buffer+40);
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000677 return;
678 }
679
680 // Some form of long double. These appear as a magic letter identifying
681 // the type, then a fixed number of hex digits.
682 Out << "0x";
683 if (&CFP->getValueAPF().getSemantics() == &APFloat::x87DoubleExtended)
684 Out << 'K';
685 else if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEquad)
686 Out << 'L';
687 else if (&CFP->getValueAPF().getSemantics() == &APFloat::PPCDoubleDouble)
688 Out << 'M';
689 else
690 assert(0 && "Unsupported floating point type");
691 // api needed to prevent premature destruction
Dale Johannesen54306fe2008-10-09 18:53:47 +0000692 APInt api = CFP->getValueAPF().bitcastToAPInt();
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000693 const uint64_t* p = api.getRawData();
694 uint64_t word = *p;
695 int shiftcount=60;
696 int width = api.getBitWidth();
697 for (int j=0; j<width; j+=4, shiftcount-=4) {
698 unsigned int nibble = (word>>shiftcount) & 15;
699 if (nibble < 10)
700 Out << (unsigned char)(nibble + '0');
Dale Johannesen028084e2007-09-12 03:30:33 +0000701 else
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000702 Out << (unsigned char)(nibble - 10 + 'A');
703 if (shiftcount == 0 && j+4 < width) {
704 word = *(++p);
705 shiftcount = 64;
706 if (width-j-4 < 64)
707 shiftcount = width-j-4;
Dale Johannesen028084e2007-09-12 03:30:33 +0000708 }
709 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000710 return;
711 }
712
713 if (isa<ConstantAggregateZero>(CV)) {
Chris Lattner76b2ff42004-02-15 05:55:15 +0000714 Out << "zeroinitializer";
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000715 return;
716 }
717
718 if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
Chris Lattner1e194682002-04-18 18:53:13 +0000719 // As a special case, print the array as a string if it is an array of
Dan Gohmane9bc2ba2008-05-12 16:34:30 +0000720 // i8 with ConstantInt values.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000721 //
Chris Lattner1e194682002-04-18 18:53:13 +0000722 const Type *ETy = CA->getType()->getElementType();
Chris Lattner6ed87bd2006-01-23 23:03:36 +0000723 if (CA->isString()) {
Chris Lattner1e194682002-04-18 18:53:13 +0000724 Out << "c\"";
Chris Lattner6ed87bd2006-01-23 23:03:36 +0000725 PrintEscapedString(CA->getAsString(), Out);
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000726 Out << '"';
Chris Lattner1e194682002-04-18 18:53:13 +0000727 } else { // Cannot output in string format...
Misha Brukman21bbdb92004-06-04 21:11:51 +0000728 Out << '[';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000729 if (CA->getNumOperands()) {
Chris Lattner1e194682002-04-18 18:53:13 +0000730 printTypeInt(Out, ETy, TypeTable);
Dan Gohman81313fd2008-09-14 17:21:12 +0000731 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000732 WriteAsOperandInternal(Out, CA->getOperand(0),
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000733 TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000734 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
735 Out << ", ";
Chris Lattner1e194682002-04-18 18:53:13 +0000736 printTypeInt(Out, ETy, TypeTable);
Dan Gohman81313fd2008-09-14 17:21:12 +0000737 Out << ' ';
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000738 WriteAsOperandInternal(Out, CA->getOperand(i), TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000739 }
740 }
Dan Gohman81313fd2008-09-14 17:21:12 +0000741 Out << ']';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000742 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000743 return;
744 }
745
746 if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Andrew Lenharth0d124b82007-01-08 18:21:30 +0000747 if (CS->getType()->isPacked())
748 Out << '<';
Misha Brukman21bbdb92004-06-04 21:11:51 +0000749 Out << '{';
Jim Laskey3bb78742006-02-25 12:27:03 +0000750 unsigned N = CS->getNumOperands();
751 if (N) {
Chris Lattner604e3512008-08-19 04:47:09 +0000752 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000753 printTypeInt(Out, CS->getOperand(0)->getType(), TypeTable);
Dan Gohman81313fd2008-09-14 17:21:12 +0000754 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000755
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000756 WriteAsOperandInternal(Out, CS->getOperand(0), TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000757
Jim Laskey3bb78742006-02-25 12:27:03 +0000758 for (unsigned i = 1; i < N; i++) {
Chris Lattnerd84bb632002-04-16 21:36:08 +0000759 Out << ", ";
760 printTypeInt(Out, CS->getOperand(i)->getType(), TypeTable);
Dan Gohman81313fd2008-09-14 17:21:12 +0000761 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000762
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000763 WriteAsOperandInternal(Out, CS->getOperand(i), TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000764 }
Dan Gohman81313fd2008-09-14 17:21:12 +0000765 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000766 }
Jim Laskey3bb78742006-02-25 12:27:03 +0000767
Dan Gohman81313fd2008-09-14 17:21:12 +0000768 Out << '}';
Andrew Lenharth0d124b82007-01-08 18:21:30 +0000769 if (CS->getType()->isPacked())
770 Out << '>';
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000771 return;
772 }
773
774 if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
775 const Type *ETy = CP->getType()->getElementType();
776 assert(CP->getNumOperands() > 0 &&
777 "Number of operands for a PackedConst must be > 0");
Dan Gohman1262a252009-02-11 00:25:25 +0000778 Out << '<';
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000779 printTypeInt(Out, ETy, TypeTable);
Dan Gohman81313fd2008-09-14 17:21:12 +0000780 Out << ' ';
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000781 WriteAsOperandInternal(Out, CP->getOperand(0), TypeTable, Machine);
782 for (unsigned i = 1, e = CP->getNumOperands(); i != e; ++i) {
Chris Lattner585297e82008-08-19 05:26:17 +0000783 Out << ", ";
784 printTypeInt(Out, ETy, TypeTable);
Dan Gohman81313fd2008-09-14 17:21:12 +0000785 Out << ' ';
Chris Lattner585297e82008-08-19 05:26:17 +0000786 WriteAsOperandInternal(Out, CP->getOperand(i), TypeTable, Machine);
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000787 }
Dan Gohman1262a252009-02-11 00:25:25 +0000788 Out << '>';
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000789 return;
790 }
791
792 if (isa<ConstantPointerNull>(CV)) {
Chris Lattnerd84bb632002-04-16 21:36:08 +0000793 Out << "null";
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000794 return;
795 }
796
797 if (isa<UndefValue>(CV)) {
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000798 Out << "undef";
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000799 return;
800 }
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000801
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000802 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Reid Spencer812a1be2006-12-04 05:19:18 +0000803 Out << CE->getOpcodeName();
804 if (CE->isCompare())
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000805 Out << ' ' << getPredicateText(CE->getPredicate());
Reid Spencer812a1be2006-12-04 05:19:18 +0000806 Out << " (";
Misha Brukmanb1c93172005-04-21 23:48:37 +0000807
Vikram S. Adveb952b542002-07-14 23:14:45 +0000808 for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
809 printTypeInt(Out, (*OI)->getType(), TypeTable);
Dan Gohman81313fd2008-09-14 17:21:12 +0000810 Out << ' ';
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000811 WriteAsOperandInternal(Out, *OI, TypeTable, Machine);
Vikram S. Adveb952b542002-07-14 23:14:45 +0000812 if (OI+1 != CE->op_end())
Chris Lattner3cd8c562002-07-30 18:54:25 +0000813 Out << ", ";
Vikram S. Adveb952b542002-07-14 23:14:45 +0000814 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000815
Dan Gohmana76f0f72008-05-31 19:12:39 +0000816 if (CE->hasIndices()) {
817 const SmallVector<unsigned, 4> &Indices = CE->getIndices();
818 for (unsigned i = 0, e = Indices.size(); i != e; ++i)
819 Out << ", " << Indices[i];
820 }
821
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000822 if (CE->isCast()) {
Chris Lattner83b396b2002-08-15 19:37:43 +0000823 Out << " to ";
824 printTypeInt(Out, CE->getType(), TypeTable);
825 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000826
Misha Brukman21bbdb92004-06-04 21:11:51 +0000827 Out << ')';
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000828 return;
Chris Lattnerd84bb632002-04-16 21:36:08 +0000829 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000830
831 Out << "<placeholder or erroneous Constant>";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000832}
833
834
Misha Brukmanc566ca362004-03-02 00:22:19 +0000835/// WriteAsOperand - Write the name of the specified value out to the specified
836/// ostream. This can be useful when you just want to print int %reg126, not
837/// the whole instruction that generated it.
838///
Chris Lattner0c19df42008-08-23 22:23:09 +0000839static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000840 std::map<const Type*, std::string> &TypeTable,
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000841 SlotTracker *Machine) {
Chris Lattner033935d2008-08-17 04:40:13 +0000842 if (V->hasName()) {
843 PrintLLVMName(Out, V);
844 return;
845 }
846
847 const Constant *CV = dyn_cast<Constant>(V);
848 if (CV && !isa<GlobalValue>(CV)) {
849 WriteConstantInt(Out, CV, TypeTable, Machine);
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000850 return;
851 }
852
853 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
Chris Lattner033935d2008-08-17 04:40:13 +0000854 Out << "asm ";
855 if (IA->hasSideEffects())
856 Out << "sideeffect ";
857 Out << '"';
858 PrintEscapedString(IA->getAsmString(), Out);
859 Out << "\", \"";
860 PrintEscapedString(IA->getConstraintString(), Out);
861 Out << '"';
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000862 return;
863 }
864
865 char Prefix = '%';
866 int Slot;
867 if (Machine) {
868 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
869 Slot = Machine->getGlobalSlot(GV);
870 Prefix = '@';
871 } else {
872 Slot = Machine->getLocalSlot(V);
873 }
Chris Lattner033935d2008-08-17 04:40:13 +0000874 } else {
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000875 Machine = createSlotTracker(V);
Chris Lattner033935d2008-08-17 04:40:13 +0000876 if (Machine) {
877 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
878 Slot = Machine->getGlobalSlot(GV);
879 Prefix = '@';
880 } else {
881 Slot = Machine->getLocalSlot(V);
882 }
Chris Lattnera2d810d2006-01-25 22:26:05 +0000883 } else {
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000884 Slot = -1;
Chris Lattnerd84bb632002-04-16 21:36:08 +0000885 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000886 delete Machine;
Chris Lattnerd84bb632002-04-16 21:36:08 +0000887 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000888
889 if (Slot != -1)
890 Out << Prefix << Slot;
891 else
892 Out << "<badref>";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000893}
894
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000895/// WriteAsOperand - Write the name of the specified value out to the specified
896/// ostream. This can be useful when you just want to print int %reg126, not
897/// the whole instruction that generated it.
898///
Chris Lattner604e3512008-08-19 04:47:09 +0000899void llvm::WriteAsOperand(std::ostream &Out, const Value *V, bool PrintType,
900 const Module *Context) {
Chris Lattner0c19df42008-08-23 22:23:09 +0000901 raw_os_ostream OS(Out);
902 WriteAsOperand(OS, V, PrintType, Context);
903}
904
905void llvm::WriteAsOperand(raw_ostream &Out, const Value *V, bool PrintType,
906 const Module *Context) {
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000907 std::map<const Type *, std::string> TypeNames;
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000908 if (Context == 0) Context = getModuleFromVal(V);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000909
Chris Lattner98cf1f52002-11-20 18:36:02 +0000910 if (Context)
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000911 fillTypeNameTable(Context, TypeNames);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000912
Dan Gohman81313fd2008-09-14 17:21:12 +0000913 if (PrintType) {
Chris Lattnerd84bb632002-04-16 21:36:08 +0000914 printTypeInt(Out, V->getType(), TypeNames);
Dan Gohman81313fd2008-09-14 17:21:12 +0000915 Out << ' ';
916 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000917
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000918 WriteAsOperandInternal(Out, V, TypeNames, 0);
Chris Lattner5e5abe32001-07-20 19:15:21 +0000919}
920
Reid Spencer58d30f22004-07-04 11:50:43 +0000921
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000922namespace {
Chris Lattner2e9fee42001-07-12 23:35:26 +0000923
Chris Lattnerfee714f2001-09-07 16:36:04 +0000924class AssemblyWriter {
Chris Lattner0c19df42008-08-23 22:23:09 +0000925 raw_ostream &Out;
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000926 SlotTracker &Machine;
Chris Lattner7bfee412001-10-29 16:05:51 +0000927 const Module *TheModule;
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000928 std::map<const Type *, std::string> TypeNames;
Chris Lattner8339f7d2003-10-30 23:41:03 +0000929 AssemblyAnnotationWriter *AnnotationWriter;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000930public:
Chris Lattner0c19df42008-08-23 22:23:09 +0000931 inline AssemblyWriter(raw_ostream &o, SlotTracker &Mac, const Module *M,
Chris Lattner8339f7d2003-10-30 23:41:03 +0000932 AssemblyAnnotationWriter *AAW)
Misha Brukmana6619a92004-06-21 21:53:56 +0000933 : Out(o), Machine(Mac), TheModule(M), AnnotationWriter(AAW) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000934
935 // If the module has a symbol table, take all global types and stuff their
936 // names into the TypeNames map.
937 //
Chris Lattnerb86620e2001-10-29 16:37:48 +0000938 fillTypeNameTable(M, TypeNames);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000939 }
940
Chris Lattner0c19df42008-08-23 22:23:09 +0000941 void write(const Module *M) { printModule(M); }
942
943 void write(const GlobalValue *G) {
944 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(G))
945 printGlobal(GV);
946 else if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(G))
947 printAlias(GA);
948 else if (const Function *F = dyn_cast<Function>(G))
949 printFunction(F);
950 else
951 assert(0 && "Unknown global");
952 }
953
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000954 void write(const BasicBlock *BB) { printBasicBlock(BB); }
955 void write(const Instruction *I) { printInstruction(*I); }
956 void write(const Type *Ty) { printType(Ty); }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000957
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000958 void writeOperand(const Value *Op, bool PrintType);
Devang Patelba3fa6c2008-09-23 23:03:40 +0000959 void writeParamOperand(const Value *Operand, Attributes Attrs);
Chris Lattner1e194682002-04-18 18:53:13 +0000960
Misha Brukman4685e262004-04-28 15:31:21 +0000961 const Module* getModule() { return TheModule; }
962
Misha Brukmand92f54a2004-11-15 19:30:05 +0000963private:
Chris Lattner7bfee412001-10-29 16:05:51 +0000964 void printModule(const Module *M);
Reid Spencer32af9e82007-01-06 07:24:44 +0000965 void printTypeSymbolTable(const TypeSymbolTable &ST);
Chris Lattner7bfee412001-10-29 16:05:51 +0000966 void printGlobal(const GlobalVariable *GV);
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000967 void printAlias(const GlobalAlias *GV);
Chris Lattner57698e22002-03-26 18:01:55 +0000968 void printFunction(const Function *F);
Devang Patelba3fa6c2008-09-23 23:03:40 +0000969 void printArgument(const Argument *FA, Attributes Attrs);
Chris Lattner7bfee412001-10-29 16:05:51 +0000970 void printBasicBlock(const BasicBlock *BB);
Chris Lattner113f4f42002-06-25 16:13:24 +0000971 void printInstruction(const Instruction &I);
Chris Lattnerd816b532002-04-13 20:53:41 +0000972
973 // printType - Go to extreme measures to attempt to print out a short,
974 // symbolic version of a type name.
975 //
Chris Lattner585297e82008-08-19 05:26:17 +0000976 void printType(const Type *Ty) {
977 printTypeInt(Out, Ty, TypeNames);
Chris Lattnerd816b532002-04-13 20:53:41 +0000978 }
979
980 // printTypeAtLeastOneLevel - Print out one level of the possibly complex type
981 // without considering any symbolic types that we may have equal to it.
982 //
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000983 void printTypeAtLeastOneLevel(const Type *Ty);
Chris Lattner7bfee412001-10-29 16:05:51 +0000984
Chris Lattner862e3382001-10-13 06:42:36 +0000985 // printInfoComment - Print a little comment after the instruction indicating
986 // which slot it occupies.
Chris Lattner113f4f42002-06-25 16:13:24 +0000987 void printInfoComment(const Value &V);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000988};
Reid Spencerf43ac622004-05-27 22:04:46 +0000989} // end of llvm namespace
Chris Lattner2f7c9632001-06-06 20:29:01 +0000990
Misha Brukmanc566ca362004-03-02 00:22:19 +0000991/// printTypeAtLeastOneLevel - Print out one level of the possibly complex type
992/// without considering any symbolic types that we may have equal to it.
993///
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000994void AssemblyWriter::printTypeAtLeastOneLevel(const Type *Ty) {
995 if (const IntegerType *ITy = dyn_cast<IntegerType>(Ty)) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000996 Out << "i" << utostr(ITy->getBitWidth());
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000997 return;
998 }
999
1000 if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
Reid Spencer8c4914c2006-12-31 05:24:50 +00001001 printType(FTy->getReturnType());
Reid Spencer8c4914c2006-12-31 05:24:50 +00001002 Out << " (";
Chris Lattnerfa829be2004-02-09 04:14:01 +00001003 for (FunctionType::param_iterator I = FTy->param_begin(),
1004 E = FTy->param_end(); I != E; ++I) {
1005 if (I != FTy->param_begin())
Misha Brukmana6619a92004-06-21 21:53:56 +00001006 Out << ", ";
Chris Lattnerd84bb632002-04-16 21:36:08 +00001007 printType(*I);
Chris Lattnerd816b532002-04-13 20:53:41 +00001008 }
1009 if (FTy->isVarArg()) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001010 if (FTy->getNumParams()) Out << ", ";
1011 Out << "...";
Chris Lattnerd816b532002-04-13 20:53:41 +00001012 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001013 Out << ')';
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001014 return;
1015 }
1016
1017 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
Andrew Lenharthdcb3c972006-12-08 18:06:16 +00001018 if (STy->isPacked())
1019 Out << '<';
Misha Brukmana6619a92004-06-21 21:53:56 +00001020 Out << "{ ";
Chris Lattnerac6db752004-02-09 04:37:31 +00001021 for (StructType::element_iterator I = STy->element_begin(),
1022 E = STy->element_end(); I != E; ++I) {
1023 if (I != STy->element_begin())
Misha Brukmana6619a92004-06-21 21:53:56 +00001024 Out << ", ";
Chris Lattnerd816b532002-04-13 20:53:41 +00001025 printType(*I);
1026 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001027 Out << " }";
Andrew Lenharthdcb3c972006-12-08 18:06:16 +00001028 if (STy->isPacked())
1029 Out << '>';
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001030 return;
1031 }
1032
1033 if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
Christopher Lambac7d6312007-12-18 03:49:35 +00001034 printType(PTy->getElementType());
1035 if (unsigned AddressSpace = PTy->getAddressSpace())
1036 Out << " addrspace(" << AddressSpace << ")";
1037 Out << '*';
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001038 return;
1039 }
1040
1041 if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001042 Out << '[' << ATy->getNumElements() << " x ";
Chris Lattner585297e82008-08-19 05:26:17 +00001043 printType(ATy->getElementType());
1044 Out << ']';
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001045 return;
1046 }
1047
1048 if (const VectorType *PTy = dyn_cast<VectorType>(Ty)) {
Reid Spencere203d352004-08-20 15:37:30 +00001049 Out << '<' << PTy->getNumElements() << " x ";
Chris Lattner585297e82008-08-19 05:26:17 +00001050 printType(PTy->getElementType());
1051 Out << '>';
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001052 return;
Reid Spencere203d352004-08-20 15:37:30 +00001053 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001054
1055 if (isa<OpaqueType>(Ty)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001056 Out << "opaque";
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001057 return;
Chris Lattnerd816b532002-04-13 20:53:41 +00001058 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001059
1060 if (!Ty->isPrimitiveType())
1061 Out << "<unknown derived type>";
1062 printType(Ty);
Chris Lattnerd816b532002-04-13 20:53:41 +00001063}
1064
1065
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001066void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType) {
1067 if (Operand == 0) {
Chris Lattner08f7d0c2005-02-24 16:58:29 +00001068 Out << "<null operand!>";
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001069 } else {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001070 if (PrintType) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001071 printType(Operand->getType());
Dan Gohman81313fd2008-09-14 17:21:12 +00001072 Out << ' ';
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001073 }
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001074 WriteAsOperandInternal(Out, Operand, TypeNames, &Machine);
Chris Lattner08f7d0c2005-02-24 16:58:29 +00001075 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001076}
1077
Dale Johannesen89268bc2008-02-19 21:38:47 +00001078void AssemblyWriter::writeParamOperand(const Value *Operand,
Devang Patelba3fa6c2008-09-23 23:03:40 +00001079 Attributes Attrs) {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00001080 if (Operand == 0) {
1081 Out << "<null operand!>";
1082 } else {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00001083 // Print the type
1084 printType(Operand->getType());
1085 // Print parameter attributes list
Devang Patel4c758ea2008-09-25 21:00:45 +00001086 if (Attrs != Attribute::None)
1087 Out << ' ' << Attribute::getAsString(Attrs);
Dan Gohman81313fd2008-09-14 17:21:12 +00001088 Out << ' ';
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00001089 // Print the operand
1090 WriteAsOperandInternal(Out, Operand, TypeNames, &Machine);
1091 }
1092}
Chris Lattner2f7c9632001-06-06 20:29:01 +00001093
Chris Lattner7bfee412001-10-29 16:05:51 +00001094void AssemblyWriter::printModule(const Module *M) {
Chris Lattner4d8689e2005-03-02 23:12:40 +00001095 if (!M->getModuleIdentifier().empty() &&
Misha Brukmanb1c93172005-04-21 23:48:37 +00001096 // Don't print the ID if it will start a new line (which would
Chris Lattner4d8689e2005-03-02 23:12:40 +00001097 // require a comment char before it).
1098 M->getModuleIdentifier().find('\n') == std::string::npos)
1099 Out << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
1100
Owen Andersone2237542006-10-18 02:21:12 +00001101 if (!M->getDataLayout().empty())
Chris Lattner04897162006-10-22 06:06:56 +00001102 Out << "target datalayout = \"" << M->getDataLayout() << "\"\n";
Reid Spencer48f98c82004-07-25 21:44:54 +00001103 if (!M->getTargetTriple().empty())
Reid Spencerffec7df2004-07-25 21:29:43 +00001104 Out << "target triple = \"" << M->getTargetTriple() << "\"\n";
Misha Brukmanb1c93172005-04-21 23:48:37 +00001105
Chris Lattnereef2fe72006-01-24 04:13:11 +00001106 if (!M->getModuleInlineAsm().empty()) {
Chris Lattnerefaf35d2006-01-24 00:45:30 +00001107 // Split the string into lines, to make it easier to read the .ll file.
Chris Lattnereef2fe72006-01-24 04:13:11 +00001108 std::string Asm = M->getModuleInlineAsm();
Chris Lattnerefaf35d2006-01-24 00:45:30 +00001109 size_t CurPos = 0;
1110 size_t NewLine = Asm.find_first_of('\n', CurPos);
1111 while (NewLine != std::string::npos) {
1112 // We found a newline, print the portion of the asm string from the
1113 // last newline up to this newline.
1114 Out << "module asm \"";
1115 PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.begin()+NewLine),
1116 Out);
1117 Out << "\"\n";
1118 CurPos = NewLine+1;
1119 NewLine = Asm.find_first_of('\n', CurPos);
1120 }
Chris Lattner3acaf5c2006-01-24 00:40:17 +00001121 Out << "module asm \"";
Chris Lattnerefaf35d2006-01-24 00:45:30 +00001122 PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.end()), Out);
Chris Lattner6ed87bd2006-01-23 23:03:36 +00001123 Out << "\"\n";
1124 }
1125
Chris Lattner2cdd49d2004-09-14 05:06:58 +00001126 // Loop over the dependent libraries and emit them.
Chris Lattner730cfe42004-09-14 04:51:44 +00001127 Module::lib_iterator LI = M->lib_begin();
1128 Module::lib_iterator LE = M->lib_end();
Reid Spencer48f98c82004-07-25 21:44:54 +00001129 if (LI != LE) {
Chris Lattner730cfe42004-09-14 04:51:44 +00001130 Out << "deplibs = [ ";
1131 while (LI != LE) {
Chris Lattner2cdd49d2004-09-14 05:06:58 +00001132 Out << '"' << *LI << '"';
Reid Spencerffec7df2004-07-25 21:29:43 +00001133 ++LI;
Chris Lattner730cfe42004-09-14 04:51:44 +00001134 if (LI != LE)
1135 Out << ", ";
Reid Spencerffec7df2004-07-25 21:29:43 +00001136 }
1137 Out << " ]\n";
Reid Spencercc5ff642004-07-25 18:08:18 +00001138 }
Reid Spencerb9e08772004-09-13 23:44:23 +00001139
Chris Lattner2cdd49d2004-09-14 05:06:58 +00001140 // Loop over the symbol table, emitting all named constants.
Reid Spencer32af9e82007-01-06 07:24:44 +00001141 printTypeSymbolTable(M->getTypeSymbolTable());
Misha Brukmanb1c93172005-04-21 23:48:37 +00001142
Chris Lattner54932b02006-12-06 04:41:52 +00001143 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
1144 I != E; ++I)
Chris Lattner113f4f42002-06-25 16:13:24 +00001145 printGlobal(I);
Chris Lattnerd2747052007-04-26 02:24:10 +00001146
1147 // Output all aliases.
1148 if (!M->alias_empty()) Out << "\n";
1149 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
1150 I != E; ++I)
1151 printAlias(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +00001152
Chris Lattner2cdd49d2004-09-14 05:06:58 +00001153 // Output all of the functions.
Chris Lattner113f4f42002-06-25 16:13:24 +00001154 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
1155 printFunction(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +00001156}
1157
Chris Lattner0c19df42008-08-23 22:23:09 +00001158static void PrintLinkage(GlobalValue::LinkageTypes LT, raw_ostream &Out) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001159 switch (LT) {
Rafael Espindola6de96a12009-01-15 20:18:42 +00001160 case GlobalValue::PrivateLinkage: Out << "private "; break;
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001161 case GlobalValue::InternalLinkage: Out << "internal "; break;
1162 case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
1163 case GlobalValue::WeakLinkage: Out << "weak "; break;
1164 case GlobalValue::CommonLinkage: Out << "common "; break;
1165 case GlobalValue::AppendingLinkage: Out << "appending "; break;
1166 case GlobalValue::DLLImportLinkage: Out << "dllimport "; break;
1167 case GlobalValue::DLLExportLinkage: Out << "dllexport "; break;
1168 case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break;
1169 case GlobalValue::ExternalLinkage: break;
1170 case GlobalValue::GhostLinkage:
1171 Out << "GhostLinkage not allowed in AsmWriter!\n";
1172 abort();
1173 }
1174}
1175
1176
1177static void PrintVisibility(GlobalValue::VisibilityTypes Vis,
Chris Lattner0c19df42008-08-23 22:23:09 +00001178 raw_ostream &Out) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001179 switch (Vis) {
1180 default: assert(0 && "Invalid visibility style!");
1181 case GlobalValue::DefaultVisibility: break;
1182 case GlobalValue::HiddenVisibility: Out << "hidden "; break;
1183 case GlobalValue::ProtectedVisibility: Out << "protected "; break;
1184 }
1185}
1186
Chris Lattner7bfee412001-10-29 16:05:51 +00001187void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Chris Lattner033935d2008-08-17 04:40:13 +00001188 if (GV->hasName()) {
1189 PrintLLVMName(Out, GV);
1190 Out << " = ";
1191 }
Chris Lattner37798642001-09-18 04:01:05 +00001192
Chris Lattner1508d3f2008-08-19 05:16:28 +00001193 if (!GV->hasInitializer() && GV->hasExternalLinkage())
1194 Out << "external ";
1195
1196 PrintLinkage(GV->getLinkage(), Out);
1197 PrintVisibility(GV->getVisibility(), Out);
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +00001198
1199 if (GV->isThreadLocal()) Out << "thread_local ";
Chris Lattnerac161bf2009-01-02 07:01:27 +00001200 if (unsigned AddressSpace = GV->getType()->getAddressSpace())
1201 Out << "addrspace(" << AddressSpace << ") ";
Misha Brukmana6619a92004-06-21 21:53:56 +00001202 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattner2413b162001-12-04 00:03:30 +00001203 printType(GV->getType()->getElementType());
Chris Lattner37798642001-09-18 04:01:05 +00001204
Dan Gohman81313fd2008-09-14 17:21:12 +00001205 if (GV->hasInitializer()) {
1206 Out << ' ';
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001207 writeOperand(GV->getInitializer(), false);
Dan Gohman81313fd2008-09-14 17:21:12 +00001208 }
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001209
Chris Lattner4b96c542005-11-12 00:10:19 +00001210 if (GV->hasSection())
1211 Out << ", section \"" << GV->getSection() << '"';
1212 if (GV->getAlignment())
Chris Lattnerf8a974d2005-11-06 06:48:53 +00001213 Out << ", align " << GV->getAlignment();
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001214
Chris Lattner113f4f42002-06-25 16:13:24 +00001215 printInfoComment(*GV);
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001216 Out << '\n';
Chris Lattnerda975502001-09-10 07:58:01 +00001217}
1218
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001219void AssemblyWriter::printAlias(const GlobalAlias *GA) {
Dale Johannesen83e468a2008-06-03 18:14:29 +00001220 // Don't crash when dumping partially built GA
1221 if (!GA->hasName())
1222 Out << "<<nameless>> = ";
Chris Lattner033935d2008-08-17 04:40:13 +00001223 else {
1224 PrintLLVMName(Out, GA);
1225 Out << " = ";
1226 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001227 PrintVisibility(GA->getVisibility(), Out);
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001228
1229 Out << "alias ";
1230
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001231 PrintLinkage(GA->getLinkage(), Out);
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001232
Anton Korobeynikov546ea7e2007-04-29 18:02:48 +00001233 const Constant *Aliasee = GA->getAliasee();
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001234
1235 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Aliasee)) {
1236 printType(GV->getType());
Chris Lattner033935d2008-08-17 04:40:13 +00001237 Out << ' ';
1238 PrintLLVMName(Out, GV);
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001239 } else if (const Function *F = dyn_cast<Function>(Aliasee)) {
1240 printType(F->getFunctionType());
1241 Out << "* ";
1242
Chris Lattner778c62c2009-02-18 21:48:13 +00001243 WriteAsOperandInternal(Out, F, TypeNames, &Machine);
Anton Korobeynikov72d5d422008-03-22 08:17:17 +00001244 } else if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(Aliasee)) {
1245 printType(GA->getType());
Chris Lattner033935d2008-08-17 04:40:13 +00001246 Out << " ";
1247 PrintLLVMName(Out, GA);
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +00001248 } else {
1249 const ConstantExpr *CE = 0;
1250 if ((CE = dyn_cast<ConstantExpr>(Aliasee)) &&
1251 (CE->getOpcode() == Instruction::BitCast)) {
1252 writeOperand(CE, false);
1253 } else
1254 assert(0 && "Unsupported aliasee");
1255 }
1256
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001257 printInfoComment(*GA);
Chris Lattner1508d3f2008-08-19 05:16:28 +00001258 Out << '\n';
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001259}
1260
Reid Spencer32af9e82007-01-06 07:24:44 +00001261void AssemblyWriter::printTypeSymbolTable(const TypeSymbolTable &ST) {
Reid Spencere7e96712004-05-25 08:53:40 +00001262 // Print the types.
Reid Spencer32af9e82007-01-06 07:24:44 +00001263 for (TypeSymbolTable::const_iterator TI = ST.begin(), TE = ST.end();
1264 TI != TE; ++TI) {
Chris Lattner1508d3f2008-08-19 05:16:28 +00001265 Out << '\t';
1266 PrintLLVMName(Out, &TI->first[0], TI->first.size(), LocalPrefix);
1267 Out << " = type ";
Reid Spencere7e96712004-05-25 08:53:40 +00001268
1269 // Make sure we print out at least one level of the type structure, so
1270 // that we do not get %FILE = type %FILE
1271 //
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001272 printTypeAtLeastOneLevel(TI->second);
1273 Out << '\n';
Reid Spencere7e96712004-05-25 08:53:40 +00001274 }
Reid Spencer32af9e82007-01-06 07:24:44 +00001275}
1276
Misha Brukmanc566ca362004-03-02 00:22:19 +00001277/// printFunction - Print all aspects of a function.
1278///
Chris Lattner113f4f42002-06-25 16:13:24 +00001279void AssemblyWriter::printFunction(const Function *F) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001280 // Print out the return type and name.
1281 Out << '\n';
Chris Lattner379a8d22003-04-16 20:28:45 +00001282
Misha Brukmana6619a92004-06-21 21:53:56 +00001283 if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001284
Reid Spencer5301e7c2007-01-30 20:08:39 +00001285 if (F->isDeclaration())
Chris Lattner10f03a62007-08-19 22:15:26 +00001286 Out << "declare ";
1287 else
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00001288 Out << "define ";
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001289
1290 PrintLinkage(F->getLinkage(), Out);
1291 PrintVisibility(F->getVisibility(), Out);
Chris Lattner379a8d22003-04-16 20:28:45 +00001292
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001293 // Print the calling convention.
1294 switch (F->getCallingConv()) {
1295 case CallingConv::C: break; // default
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +00001296 case CallingConv::Fast: Out << "fastcc "; break;
1297 case CallingConv::Cold: Out << "coldcc "; break;
1298 case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break;
1299 case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break;
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001300 default: Out << "cc" << F->getCallingConv() << " "; break;
1301 }
1302
Reid Spencer8c4914c2006-12-31 05:24:50 +00001303 const FunctionType *FT = F->getFunctionType();
Devang Patel4c758ea2008-09-25 21:00:45 +00001304 const AttrListPtr &Attrs = F->getAttributes();
Devang Patel221fe422008-09-29 20:49:50 +00001305 Attributes RetAttrs = Attrs.getRetAttributes();
1306 if (RetAttrs != Attribute::None)
1307 Out << Attribute::getAsString(Attrs.getRetAttributes()) << ' ';
Chris Lattner585297e82008-08-19 05:26:17 +00001308 printType(F->getReturnType());
1309 Out << ' ';
Chris Lattner778c62c2009-02-18 21:48:13 +00001310 WriteAsOperandInternal(Out, F, TypeNames, &Machine);
Misha Brukmana6619a92004-06-21 21:53:56 +00001311 Out << '(';
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001312 Machine.incorporateFunction(F);
Chris Lattnerfee714f2001-09-07 16:36:04 +00001313
Chris Lattner7bfee412001-10-29 16:05:51 +00001314 // Loop over the arguments, printing them...
Chris Lattnerfee714f2001-09-07 16:36:04 +00001315
Reid Spencer8c4914c2006-12-31 05:24:50 +00001316 unsigned Idx = 1;
Chris Lattner82738fe2007-04-18 00:57:22 +00001317 if (!F->isDeclaration()) {
1318 // If this isn't a declaration, print the argument names as well.
1319 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
1320 I != E; ++I) {
1321 // Insert commas as we go... the first arg doesn't get a comma
1322 if (I != F->arg_begin()) Out << ", ";
Devang Patela05633e2008-09-26 22:53:05 +00001323 printArgument(I, Attrs.getParamAttributes(Idx));
Chris Lattner82738fe2007-04-18 00:57:22 +00001324 Idx++;
1325 }
1326 } else {
1327 // Otherwise, print the types from the function type.
1328 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
1329 // Insert commas as we go... the first arg doesn't get a comma
1330 if (i) Out << ", ";
1331
1332 // Output type...
1333 printType(FT->getParamType(i));
1334
Devang Patela05633e2008-09-26 22:53:05 +00001335 Attributes ArgAttrs = Attrs.getParamAttributes(i+1);
Devang Patel4c758ea2008-09-25 21:00:45 +00001336 if (ArgAttrs != Attribute::None)
1337 Out << ' ' << Attribute::getAsString(ArgAttrs);
Chris Lattner82738fe2007-04-18 00:57:22 +00001338 }
Reid Spencer8c4914c2006-12-31 05:24:50 +00001339 }
Chris Lattnerfee714f2001-09-07 16:36:04 +00001340
1341 // Finish printing arguments...
Chris Lattner113f4f42002-06-25 16:13:24 +00001342 if (FT->isVarArg()) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001343 if (FT->getNumParams()) Out << ", ";
1344 Out << "..."; // Output varargs portion of signature!
Chris Lattnerfee714f2001-09-07 16:36:04 +00001345 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001346 Out << ')';
Devang Patela05633e2008-09-26 22:53:05 +00001347 Attributes FnAttrs = Attrs.getFnAttributes();
1348 if (FnAttrs != Attribute::None)
1349 Out << ' ' << Attribute::getAsString(Attrs.getFnAttributes());
Chris Lattner4b96c542005-11-12 00:10:19 +00001350 if (F->hasSection())
1351 Out << " section \"" << F->getSection() << '"';
Chris Lattnerf8a974d2005-11-06 06:48:53 +00001352 if (F->getAlignment())
1353 Out << " align " << F->getAlignment();
Gordon Henriksend930f912008-08-17 18:44:35 +00001354 if (F->hasGC())
1355 Out << " gc \"" << F->getGC() << '"';
Reid Spencer5301e7c2007-01-30 20:08:39 +00001356 if (F->isDeclaration()) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001357 Out << "\n";
Chris Lattnerb2f02e52002-05-06 03:00:40 +00001358 } else {
Chris Lattnerd5fbc8f2008-04-21 06:12:55 +00001359 Out << " {";
Misha Brukmanb1c93172005-04-21 23:48:37 +00001360
Chris Lattner6915f8f2002-04-07 22:49:37 +00001361 // Output all of its basic blocks... for the function
Chris Lattner113f4f42002-06-25 16:13:24 +00001362 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
1363 printBasicBlock(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +00001364
Misha Brukmana6619a92004-06-21 21:53:56 +00001365 Out << "}\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +00001366 }
1367
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001368 Machine.purgeFunction();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001369}
1370
Misha Brukmanc566ca362004-03-02 00:22:19 +00001371/// printArgument - This member is called for every argument that is passed into
1372/// the function. Simply print it out
1373///
Dale Johannesen89268bc2008-02-19 21:38:47 +00001374void AssemblyWriter::printArgument(const Argument *Arg,
Devang Patelba3fa6c2008-09-23 23:03:40 +00001375 Attributes Attrs) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001376 // Output type...
Chris Lattner7bfee412001-10-29 16:05:51 +00001377 printType(Arg->getType());
Misha Brukmanb1c93172005-04-21 23:48:37 +00001378
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00001379 // Output parameter attributes list
Devang Patel4c758ea2008-09-25 21:00:45 +00001380 if (Attrs != Attribute::None)
1381 Out << ' ' << Attribute::getAsString(Attrs);
Reid Spencer8c4914c2006-12-31 05:24:50 +00001382
Chris Lattner2f7c9632001-06-06 20:29:01 +00001383 // Output name, if available...
Chris Lattner033935d2008-08-17 04:40:13 +00001384 if (Arg->hasName()) {
1385 Out << ' ';
1386 PrintLLVMName(Out, Arg);
1387 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001388}
1389
Misha Brukmanc566ca362004-03-02 00:22:19 +00001390/// printBasicBlock - This member is called for each basic block in a method.
1391///
Chris Lattner7bfee412001-10-29 16:05:51 +00001392void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Nick Lewycky4d43d3c2008-04-25 16:53:59 +00001393 if (BB->hasName()) { // Print out the label if it exists...
Chris Lattner033935d2008-08-17 04:40:13 +00001394 Out << "\n";
Chris Lattner1508d3f2008-08-19 05:16:28 +00001395 PrintLLVMName(Out, BB->getNameStart(), BB->getNameLen(), LabelPrefix);
Chris Lattner033935d2008-08-17 04:40:13 +00001396 Out << ':';
Nick Lewycky4d43d3c2008-04-25 16:53:59 +00001397 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Chris Lattner67bec132008-04-21 04:20:33 +00001398 Out << "\n; <label>:";
Chris Lattner5e043322007-01-11 03:54:27 +00001399 int Slot = Machine.getLocalSlot(BB);
Chris Lattner757ee0b2004-06-09 19:41:19 +00001400 if (Slot != -1)
Misha Brukmana6619a92004-06-21 21:53:56 +00001401 Out << Slot;
Chris Lattner757ee0b2004-06-09 19:41:19 +00001402 else
Misha Brukmana6619a92004-06-21 21:53:56 +00001403 Out << "<badref>";
Chris Lattner58185f22002-10-02 19:38:55 +00001404 }
Chris Lattner2447ef52003-11-20 00:09:43 +00001405
1406 if (BB->getParent() == 0)
Misha Brukmana6619a92004-06-21 21:53:56 +00001407 Out << "\t\t; Error: Block without parent!";
Chris Lattnerff834c02008-04-22 02:45:44 +00001408 else if (BB != &BB->getParent()->getEntryBlock()) { // Not the entry block?
1409 // Output predecessors for the block...
1410 Out << "\t\t;";
1411 pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
1412
1413 if (PI == PE) {
1414 Out << " No predecessors!";
1415 } else {
Dan Gohman81313fd2008-09-14 17:21:12 +00001416 Out << " preds = ";
Chris Lattnerff834c02008-04-22 02:45:44 +00001417 writeOperand(*PI, false);
1418 for (++PI; PI != PE; ++PI) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001419 Out << ", ";
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001420 writeOperand(*PI, false);
Chris Lattner00211f12003-11-16 22:59:57 +00001421 }
Chris Lattner58185f22002-10-02 19:38:55 +00001422 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001423 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001424
Chris Lattnerff834c02008-04-22 02:45:44 +00001425 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001426
Misha Brukmana6619a92004-06-21 21:53:56 +00001427 if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001428
Chris Lattnerfee714f2001-09-07 16:36:04 +00001429 // Output all of the instructions in the basic block...
Chris Lattner113f4f42002-06-25 16:13:24 +00001430 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
1431 printInstruction(*I);
Chris Lattner96cdd272004-03-08 18:51:45 +00001432
Misha Brukmana6619a92004-06-21 21:53:56 +00001433 if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001434}
1435
Chris Lattner862e3382001-10-13 06:42:36 +00001436
Misha Brukmanc566ca362004-03-02 00:22:19 +00001437/// printInfoComment - Print a little comment after the instruction indicating
1438/// which slot it occupies.
1439///
Chris Lattner113f4f42002-06-25 16:13:24 +00001440void AssemblyWriter::printInfoComment(const Value &V) {
1441 if (V.getType() != Type::VoidTy) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001442 Out << "\t\t; <";
Chris Lattner585297e82008-08-19 05:26:17 +00001443 printType(V.getType());
1444 Out << '>';
Chris Lattner862e3382001-10-13 06:42:36 +00001445
Chris Lattnerb25e5ea2008-08-29 17:19:30 +00001446 if (!V.hasName() && !isa<Instruction>(V)) {
Chris Lattner5e043322007-01-11 03:54:27 +00001447 int SlotNum;
1448 if (const GlobalValue *GV = dyn_cast<GlobalValue>(&V))
1449 SlotNum = Machine.getGlobalSlot(GV);
1450 else
1451 SlotNum = Machine.getLocalSlot(&V);
Chris Lattner757ee0b2004-06-09 19:41:19 +00001452 if (SlotNum == -1)
Misha Brukmana6619a92004-06-21 21:53:56 +00001453 Out << ":<badref>";
Reid Spencer8beac692004-06-09 15:26:53 +00001454 else
Misha Brukmana6619a92004-06-21 21:53:56 +00001455 Out << ':' << SlotNum; // Print out the def slot taken.
Chris Lattner862e3382001-10-13 06:42:36 +00001456 }
Chris Lattnerb6c21db2005-02-01 01:24:01 +00001457 Out << " [#uses=" << V.getNumUses() << ']'; // Output # uses
Chris Lattner862e3382001-10-13 06:42:36 +00001458 }
1459}
1460
Reid Spencere7141c82006-08-28 01:02:49 +00001461// This member is called for each Instruction in a function..
Chris Lattner113f4f42002-06-25 16:13:24 +00001462void AssemblyWriter::printInstruction(const Instruction &I) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001463 if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001464
Chris Lattner82ff9232008-08-23 22:52:27 +00001465 Out << '\t';
Chris Lattner2f7c9632001-06-06 20:29:01 +00001466
1467 // Print out name if it exists...
Chris Lattner033935d2008-08-17 04:40:13 +00001468 if (I.hasName()) {
1469 PrintLLVMName(Out, &I);
1470 Out << " = ";
Chris Lattnerb25e5ea2008-08-29 17:19:30 +00001471 } else if (I.getType() != Type::VoidTy) {
1472 // Print out the def slot taken.
1473 int SlotNum = Machine.getLocalSlot(&I);
1474 if (SlotNum == -1)
1475 Out << "<badref> = ";
1476 else
1477 Out << '%' << SlotNum << " = ";
Chris Lattner033935d2008-08-17 04:40:13 +00001478 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001479
Chris Lattner06038452005-05-06 05:51:46 +00001480 // If this is a volatile load or store, print out the volatile marker.
Chris Lattner504f9242003-09-08 17:45:59 +00001481 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
Chris Lattner06038452005-05-06 05:51:46 +00001482 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile())) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001483 Out << "volatile ";
Chris Lattner06038452005-05-06 05:51:46 +00001484 } else if (isa<CallInst>(I) && cast<CallInst>(I).isTailCall()) {
1485 // If this is a call, check if it's a tail call.
1486 Out << "tail ";
1487 }
Chris Lattner504f9242003-09-08 17:45:59 +00001488
Chris Lattner2f7c9632001-06-06 20:29:01 +00001489 // Print out the opcode...
Misha Brukmana6619a92004-06-21 21:53:56 +00001490 Out << I.getOpcodeName();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001491
Reid Spencer45e52392006-12-03 06:27:29 +00001492 // Print out the compare instruction predicates
Nate Begemand2195702008-05-12 19:01:56 +00001493 if (const CmpInst *CI = dyn_cast<CmpInst>(&I))
Chris Lattner82ff9232008-08-23 22:52:27 +00001494 Out << ' ' << getPredicateText(CI->getPredicate());
Reid Spencer45e52392006-12-03 06:27:29 +00001495
Chris Lattner2f7c9632001-06-06 20:29:01 +00001496 // Print out the type of the operands...
Chris Lattner113f4f42002-06-25 16:13:24 +00001497 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001498
1499 // Special case conditional branches to swizzle the condition out to the front
Gabor Greifcab008f2009-02-09 15:45:06 +00001500 if (isa<BranchInst>(I) && cast<BranchInst>(I).isConditional()) {
1501 BranchInst &BI(cast<BranchInst>(I));
Dan Gohman81313fd2008-09-14 17:21:12 +00001502 Out << ' ';
Gabor Greifcab008f2009-02-09 15:45:06 +00001503 writeOperand(BI.getCondition(), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001504 Out << ", ";
Gabor Greifcab008f2009-02-09 15:45:06 +00001505 writeOperand(BI.getSuccessor(0), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001506 Out << ", ";
Gabor Greifcab008f2009-02-09 15:45:06 +00001507 writeOperand(BI.getSuccessor(1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001508
Chris Lattner8d48df22002-04-13 18:34:38 +00001509 } else if (isa<SwitchInst>(I)) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001510 // Special case switch statement to get formatting nice and correct...
Dan Gohman81313fd2008-09-14 17:21:12 +00001511 Out << ' ';
Chris Lattner82ff9232008-08-23 22:52:27 +00001512 writeOperand(Operand , true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001513 Out << ", ";
Chris Lattner82ff9232008-08-23 22:52:27 +00001514 writeOperand(I.getOperand(1), true);
1515 Out << " [";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001516
Chris Lattner113f4f42002-06-25 16:13:24 +00001517 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001518 Out << "\n\t\t";
Chris Lattner82ff9232008-08-23 22:52:27 +00001519 writeOperand(I.getOperand(op ), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001520 Out << ", ";
Chris Lattner113f4f42002-06-25 16:13:24 +00001521 writeOperand(I.getOperand(op+1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001522 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001523 Out << "\n\t]";
Chris Lattnerda558102001-10-02 03:41:24 +00001524 } else if (isa<PHINode>(I)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001525 Out << ' ';
Chris Lattner113f4f42002-06-25 16:13:24 +00001526 printType(I.getType());
Misha Brukmana6619a92004-06-21 21:53:56 +00001527 Out << ' ';
Chris Lattner2f7c9632001-06-06 20:29:01 +00001528
Chris Lattner113f4f42002-06-25 16:13:24 +00001529 for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001530 if (op) Out << ", ";
Dan Gohman81313fd2008-09-14 17:21:12 +00001531 Out << "[ ";
1532 writeOperand(I.getOperand(op ), false); Out << ", ";
Misha Brukmana6619a92004-06-21 21:53:56 +00001533 writeOperand(I.getOperand(op+1), false); Out << " ]";
Chris Lattner931ef3b2001-06-11 15:04:20 +00001534 }
Dan Gohmana76f0f72008-05-31 19:12:39 +00001535 } else if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(&I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001536 Out << ' ';
Dan Gohmana76f0f72008-05-31 19:12:39 +00001537 writeOperand(I.getOperand(0), true);
1538 for (const unsigned *i = EVI->idx_begin(), *e = EVI->idx_end(); i != e; ++i)
1539 Out << ", " << *i;
1540 } else if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(&I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001541 Out << ' ';
1542 writeOperand(I.getOperand(0), true); Out << ", ";
Dan Gohmana76f0f72008-05-31 19:12:39 +00001543 writeOperand(I.getOperand(1), true);
1544 for (const unsigned *i = IVI->idx_begin(), *e = IVI->idx_end(); i != e; ++i)
1545 Out << ", " << *i;
Devang Patel59643e52008-02-23 00:35:18 +00001546 } else if (isa<ReturnInst>(I) && !Operand) {
1547 Out << " void";
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001548 } else if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
1549 // Print the calling convention being used.
1550 switch (CI->getCallingConv()) {
1551 case CallingConv::C: break; // default
Chris Lattner29d20852006-05-19 21:58:52 +00001552 case CallingConv::Fast: Out << " fastcc"; break;
1553 case CallingConv::Cold: Out << " coldcc"; break;
Chris Lattnerf5270372007-11-18 18:32:16 +00001554 case CallingConv::X86_StdCall: Out << " x86_stdcallcc"; break;
1555 case CallingConv::X86_FastCall: Out << " x86_fastcallcc"; break;
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001556 default: Out << " cc" << CI->getCallingConv(); break;
1557 }
1558
Reid Spencer1517de32007-04-09 06:10:42 +00001559 const PointerType *PTy = cast<PointerType>(Operand->getType());
1560 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1561 const Type *RetTy = FTy->getReturnType();
Devang Patel4c758ea2008-09-25 21:00:45 +00001562 const AttrListPtr &PAL = CI->getAttributes();
Chris Lattner2f2d9472001-11-06 21:28:12 +00001563
Devang Patel221fe422008-09-29 20:49:50 +00001564 if (PAL.getRetAttributes() != Attribute::None)
1565 Out << ' ' << Attribute::getAsString(PAL.getRetAttributes());
1566
Chris Lattner463d6a52003-08-05 15:34:45 +00001567 // If possible, print out the short form of the call instruction. We can
Chris Lattner6915f8f2002-04-07 22:49:37 +00001568 // only do this if the first argument is a pointer to a nonvararg function,
Chris Lattner463d6a52003-08-05 15:34:45 +00001569 // and if the return type is not a pointer to a function.
Chris Lattner2f2d9472001-11-06 21:28:12 +00001570 //
Dan Gohman81313fd2008-09-14 17:21:12 +00001571 Out << ' ';
Chris Lattner463d6a52003-08-05 15:34:45 +00001572 if (!FTy->isVarArg() &&
Misha Brukmanb1c93172005-04-21 23:48:37 +00001573 (!isa<PointerType>(RetTy) ||
Chris Lattnerd9a36a62002-07-25 20:58:51 +00001574 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001575 printType(RetTy);
1576 Out << ' ';
Chris Lattner2f2d9472001-11-06 21:28:12 +00001577 writeOperand(Operand, false);
1578 } else {
1579 writeOperand(Operand, true);
1580 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001581 Out << '(';
Reid Spencer8c4914c2006-12-31 05:24:50 +00001582 for (unsigned op = 1, Eop = I.getNumOperands(); op < Eop; ++op) {
1583 if (op > 1)
Dan Gohman81313fd2008-09-14 17:21:12 +00001584 Out << ", ";
Devang Patela05633e2008-09-26 22:53:05 +00001585 writeParamOperand(I.getOperand(op), PAL.getParamAttributes(op));
Chris Lattner2f7c9632001-06-06 20:29:01 +00001586 }
Dan Gohman81313fd2008-09-14 17:21:12 +00001587 Out << ')';
Devang Patela05633e2008-09-26 22:53:05 +00001588 if (PAL.getFnAttributes() != Attribute::None)
1589 Out << ' ' << Attribute::getAsString(PAL.getFnAttributes());
Chris Lattner113f4f42002-06-25 16:13:24 +00001590 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Reid Spencer1517de32007-04-09 06:10:42 +00001591 const PointerType *PTy = cast<PointerType>(Operand->getType());
1592 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1593 const Type *RetTy = FTy->getReturnType();
Devang Patel4c758ea2008-09-25 21:00:45 +00001594 const AttrListPtr &PAL = II->getAttributes();
Chris Lattner463d6a52003-08-05 15:34:45 +00001595
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001596 // Print the calling convention being used.
1597 switch (II->getCallingConv()) {
1598 case CallingConv::C: break; // default
Chris Lattner29d20852006-05-19 21:58:52 +00001599 case CallingConv::Fast: Out << " fastcc"; break;
1600 case CallingConv::Cold: Out << " coldcc"; break;
Dan Gohman81313fd2008-09-14 17:21:12 +00001601 case CallingConv::X86_StdCall: Out << " x86_stdcallcc"; break;
1602 case CallingConv::X86_FastCall: Out << " x86_fastcallcc"; break;
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001603 default: Out << " cc" << II->getCallingConv(); break;
1604 }
1605
Devang Patel221fe422008-09-29 20:49:50 +00001606 if (PAL.getRetAttributes() != Attribute::None)
1607 Out << ' ' << Attribute::getAsString(PAL.getRetAttributes());
1608
Chris Lattner463d6a52003-08-05 15:34:45 +00001609 // If possible, print out the short form of the invoke instruction. We can
1610 // only do this if the first argument is a pointer to a nonvararg function,
1611 // and if the return type is not a pointer to a function.
1612 //
Dan Gohmanc7e00ba2008-10-15 18:02:08 +00001613 Out << ' ';
Chris Lattner463d6a52003-08-05 15:34:45 +00001614 if (!FTy->isVarArg() &&
Misha Brukmanb1c93172005-04-21 23:48:37 +00001615 (!isa<PointerType>(RetTy) ||
Chris Lattner463d6a52003-08-05 15:34:45 +00001616 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Dan Gohmanc7e00ba2008-10-15 18:02:08 +00001617 printType(RetTy);
1618 Out << ' ';
Chris Lattner463d6a52003-08-05 15:34:45 +00001619 writeOperand(Operand, false);
1620 } else {
1621 writeOperand(Operand, true);
1622 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001623 Out << '(';
Reid Spencer8c4914c2006-12-31 05:24:50 +00001624 for (unsigned op = 3, Eop = I.getNumOperands(); op < Eop; ++op) {
1625 if (op > 3)
Dan Gohman81313fd2008-09-14 17:21:12 +00001626 Out << ", ";
Devang Patela05633e2008-09-26 22:53:05 +00001627 writeParamOperand(I.getOperand(op), PAL.getParamAttributes(op-2));
Chris Lattner862e3382001-10-13 06:42:36 +00001628 }
1629
Dan Gohman81313fd2008-09-14 17:21:12 +00001630 Out << ')';
Devang Patela05633e2008-09-26 22:53:05 +00001631 if (PAL.getFnAttributes() != Attribute::None)
1632 Out << ' ' << Attribute::getAsString(PAL.getFnAttributes());
1633
Dan Gohman81313fd2008-09-14 17:21:12 +00001634 Out << "\n\t\t\tto ";
Chris Lattner862e3382001-10-13 06:42:36 +00001635 writeOperand(II->getNormalDest(), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001636 Out << " unwind ";
Chris Lattnerfae8ab32004-02-08 21:44:31 +00001637 writeOperand(II->getUnwindDest(), true);
Chris Lattner862e3382001-10-13 06:42:36 +00001638
Chris Lattner113f4f42002-06-25 16:13:24 +00001639 } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(&I)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001640 Out << ' ';
Chris Lattner8d48df22002-04-13 18:34:38 +00001641 printType(AI->getType()->getElementType());
1642 if (AI->isArrayAllocation()) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001643 Out << ", ";
Chris Lattner8d48df22002-04-13 18:34:38 +00001644 writeOperand(AI->getArraySize(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001645 }
Nate Begeman848622f2005-11-05 09:21:28 +00001646 if (AI->getAlignment()) {
Chris Lattner7aeee3a2005-11-05 21:20:34 +00001647 Out << ", align " << AI->getAlignment();
Nate Begeman848622f2005-11-05 09:21:28 +00001648 }
Chris Lattner862e3382001-10-13 06:42:36 +00001649 } else if (isa<CastInst>(I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001650 if (Operand) {
1651 Out << ' ';
1652 writeOperand(Operand, true); // Work with broken code
1653 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001654 Out << " to ";
Chris Lattner113f4f42002-06-25 16:13:24 +00001655 printType(I.getType());
Chris Lattner5b337482003-10-18 05:57:43 +00001656 } else if (isa<VAArgInst>(I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001657 if (Operand) {
1658 Out << ' ';
1659 writeOperand(Operand, true); // Work with broken code
1660 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001661 Out << ", ";
Chris Lattnerf70da102003-05-08 02:44:12 +00001662 printType(I.getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +00001663 } else if (Operand) { // Print the normal way...
1664
Misha Brukmanb1c93172005-04-21 23:48:37 +00001665 // PrintAllTypes - Instructions who have operands of all the same type
Chris Lattner2f7c9632001-06-06 20:29:01 +00001666 // omit the type from all but the first operand. If the instruction has
1667 // different type operands (for example br), then they are all printed.
1668 bool PrintAllTypes = false;
1669 const Type *TheType = Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001670
Reid Spencer0cdd04f2007-02-02 13:54:55 +00001671 // Select, Store and ShuffleVector always print all types.
Devang Patelce556d92008-03-04 22:05:14 +00001672 if (isa<SelectInst>(I) || isa<StoreInst>(I) || isa<ShuffleVectorInst>(I)
1673 || isa<ReturnInst>(I)) {
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00001674 PrintAllTypes = true;
1675 } else {
1676 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
1677 Operand = I.getOperand(i);
Nuno Lopes04fe2f02009-01-15 18:40:57 +00001678 // note that Operand shouldn't be null, but the test helps make dump()
1679 // more tolerant of malformed IR
Nuno Lopes0971e772009-01-14 17:51:41 +00001680 if (Operand && Operand->getType() != TheType) {
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00001681 PrintAllTypes = true; // We have differing types! Print them all!
1682 break;
1683 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001684 }
1685 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001686
Chris Lattner7bfee412001-10-29 16:05:51 +00001687 if (!PrintAllTypes) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001688 Out << ' ';
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00001689 printType(TheType);
Chris Lattner7bfee412001-10-29 16:05:51 +00001690 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001691
Dan Gohman81313fd2008-09-14 17:21:12 +00001692 Out << ' ';
Chris Lattner113f4f42002-06-25 16:13:24 +00001693 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001694 if (i) Out << ", ";
Chris Lattner113f4f42002-06-25 16:13:24 +00001695 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001696 }
1697 }
Christopher Lamb84485702007-04-22 19:24:39 +00001698
1699 // Print post operand alignment for load/store
1700 if (isa<LoadInst>(I) && cast<LoadInst>(I).getAlignment()) {
1701 Out << ", align " << cast<LoadInst>(I).getAlignment();
1702 } else if (isa<StoreInst>(I) && cast<StoreInst>(I).getAlignment()) {
1703 Out << ", align " << cast<StoreInst>(I).getAlignment();
1704 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001705
Chris Lattner862e3382001-10-13 06:42:36 +00001706 printInfoComment(I);
Chris Lattner82ff9232008-08-23 22:52:27 +00001707 Out << '\n';
Chris Lattner2f7c9632001-06-06 20:29:01 +00001708}
1709
1710
1711//===----------------------------------------------------------------------===//
1712// External Interface declarations
1713//===----------------------------------------------------------------------===//
1714
Chris Lattner8339f7d2003-10-30 23:41:03 +00001715void Module::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Chris Lattner0c19df42008-08-23 22:23:09 +00001716 raw_os_ostream OS(o);
1717 print(OS, AAW);
1718}
1719void Module::print(raw_ostream &OS, AssemblyAnnotationWriter *AAW) const {
Chris Lattnere36fd8a2008-08-19 04:26:57 +00001720 SlotTracker SlotTable(this);
Chris Lattner0c19df42008-08-23 22:23:09 +00001721 AssemblyWriter W(OS, SlotTable, this, AAW);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001722 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001723}
1724
Misha Brukmanb1c93172005-04-21 23:48:37 +00001725void Type::print(std::ostream &o) const {
Chris Lattner0c19df42008-08-23 22:23:09 +00001726 raw_os_ostream OS(o);
1727 print(OS);
1728}
1729
1730void Type::print(raw_ostream &o) const {
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001731 if (this == 0)
1732 o << "<null Type>";
1733 else
1734 o << getDescription();
1735}
1736
Chris Lattner0c19df42008-08-23 22:23:09 +00001737void Value::print(raw_ostream &OS, AssemblyAnnotationWriter *AAW) const {
1738 if (this == 0) {
1739 OS << "printing a <null> value\n";
1740 return;
1741 }
1742
1743 if (const Instruction *I = dyn_cast<Instruction>(this)) {
1744 const Function *F = I->getParent() ? I->getParent()->getParent() : 0;
1745 SlotTracker SlotTable(F);
1746 AssemblyWriter W(OS, SlotTable, F ? F->getParent() : 0, AAW);
1747 W.write(I);
1748 } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(this)) {
1749 SlotTracker SlotTable(BB->getParent());
1750 AssemblyWriter W(OS, SlotTable,
1751 BB->getParent() ? BB->getParent()->getParent() : 0, AAW);
1752 W.write(BB);
1753 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(this)) {
1754 SlotTracker SlotTable(GV->getParent());
1755 AssemblyWriter W(OS, SlotTable, GV->getParent(), 0);
1756 W.write(GV);
1757 } else if (const Constant *C = dyn_cast<Constant>(this)) {
Dan Gohmanfbd67be2008-10-01 15:09:37 +00001758 OS << C->getType()->getDescription() << ' ';
Chris Lattner0c19df42008-08-23 22:23:09 +00001759 std::map<const Type *, std::string> TypeTable;
1760 WriteConstantInt(OS, C, TypeTable, 0);
1761 } else if (const Argument *A = dyn_cast<Argument>(this)) {
1762 WriteAsOperand(OS, this, true,
1763 A->getParent() ? A->getParent()->getParent() : 0);
1764 } else if (isa<InlineAsm>(this)) {
1765 WriteAsOperand(OS, this, true, 0);
1766 } else {
Dan Gohmanee1cd172008-12-03 21:37:21 +00001767 assert(0 && "Unknown value to print out!");
Chris Lattner0c19df42008-08-23 22:23:09 +00001768 }
1769}
1770
1771void Value::print(std::ostream &O, AssemblyAnnotationWriter *AAW) const {
1772 raw_os_ostream OS(O);
1773 print(OS, AAW);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001774}
1775
Chris Lattnerdab942552008-08-25 17:03:15 +00001776// Value::dump - allow easy printing of Values from the debugger.
Chris Lattner820eebc2008-08-25 04:55:46 +00001777void Value::dump() const { print(errs()); errs() << '\n'; errs().flush(); }
Reid Spencer52641832004-05-25 18:14:38 +00001778
Chris Lattnerdab942552008-08-25 17:03:15 +00001779// Type::dump - allow easy printing of Types from the debugger.
Chris Lattner820eebc2008-08-25 04:55:46 +00001780void Type::dump() const { print(errs()); errs() << '\n'; errs().flush(); }
Chris Lattner0c19df42008-08-23 22:23:09 +00001781
Chris Lattner41a83d92008-10-01 20:16:19 +00001782// Type::dump - allow easy printing of Types from the debugger.
1783// This one uses type names from the given context module
1784void Type::dump(const Module *Context) const {
1785 WriteTypeSymbolic(errs(), this, Context);
1786 errs() << '\n';
1787 errs().flush();
1788}
1789
Chris Lattnerdab942552008-08-25 17:03:15 +00001790// Module::dump() - Allow printing of Modules from the debugger.
Chris Lattner820eebc2008-08-25 04:55:46 +00001791void Module::dump() const { print(errs(), 0); errs().flush(); }
Chris Lattner0c19df42008-08-23 22:23:09 +00001792
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001793