blob: ddcb8d6c0abafe6680eb263aec84d1f3ac802a13 [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 +000042char PrintModulePass::ID = 0;
43static RegisterPass<PrintModulePass>
Duncan Sands9c40c282008-09-23 12:47:39 +000044X("print-module", "Print module to stderr");
Chris Lattner3eee99c2008-08-19 04:36:02 +000045char PrintFunctionPass::ID = 0;
46static RegisterPass<PrintFunctionPass>
Duncan Sands9c40c282008-09-23 12:47:39 +000047Y("print-function","Print function to stderr");
Chris Lattner3eee99c2008-08-19 04:36:02 +000048
49
50//===----------------------------------------------------------------------===//
51// Helper Functions
52//===----------------------------------------------------------------------===//
53
54static const Module *getModuleFromVal(const Value *V) {
55 if (const Argument *MA = dyn_cast<Argument>(V))
56 return MA->getParent() ? MA->getParent()->getParent() : 0;
57
58 if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
59 return BB->getParent() ? BB->getParent()->getParent() : 0;
60
61 if (const Instruction *I = dyn_cast<Instruction>(V)) {
62 const Function *M = I->getParent() ? I->getParent()->getParent() : 0;
63 return M ? M->getParent() : 0;
64 }
65
66 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
67 return GV->getParent();
68 return 0;
69}
70
Chris Lattner3eee99c2008-08-19 04:36:02 +000071enum PrefixType {
72 GlobalPrefix,
73 LabelPrefix,
Daniel Dunbar389529a2008-10-14 23:28:09 +000074 LocalPrefix,
75 NoPrefix
Chris Lattner3eee99c2008-08-19 04:36:02 +000076};
77
78/// PrintLLVMName - Turn the specified name into an 'LLVM name', which is either
79/// prefixed with % (if the string only contains simple characters) or is
80/// surrounded with ""'s (if it has special chars in it). Print it out.
Chris Lattner0c19df42008-08-23 22:23:09 +000081static void PrintLLVMName(raw_ostream &OS, const char *NameStr,
Chris Lattner1508d3f2008-08-19 05:16:28 +000082 unsigned NameLen, PrefixType Prefix) {
83 assert(NameStr && "Cannot get empty name!");
Chris Lattner3eee99c2008-08-19 04:36:02 +000084 switch (Prefix) {
Chris Lattner1508d3f2008-08-19 05:16:28 +000085 default: assert(0 && "Bad prefix!");
Daniel Dunbar389529a2008-10-14 23:28:09 +000086 case NoPrefix: break;
Chris Lattner1508d3f2008-08-19 05:16:28 +000087 case GlobalPrefix: OS << '@'; break;
88 case LabelPrefix: break;
89 case LocalPrefix: OS << '%'; break;
Chris Lattner3eee99c2008-08-19 04:36:02 +000090 }
91
92 // Scan the name to see if it needs quotes first.
Chris Lattner3eee99c2008-08-19 04:36:02 +000093 bool NeedsQuotes = NameStr[0] >= '0' && NameStr[0] <= '9';
94 if (!NeedsQuotes) {
95 for (unsigned i = 0; i != NameLen; ++i) {
96 char C = NameStr[i];
97 if (!isalnum(C) && C != '-' && C != '.' && C != '_') {
98 NeedsQuotes = true;
99 break;
100 }
101 }
102 }
103
104 // If we didn't need any quotes, just write out the name in one blast.
105 if (!NeedsQuotes) {
106 OS.write(NameStr, NameLen);
107 return;
108 }
109
110 // Okay, we need quotes. Output the quotes and escape any scary characters as
111 // needed.
112 OS << '"';
113 for (unsigned i = 0; i != NameLen; ++i) {
114 char C = NameStr[i];
115 assert(C != '"' && "Illegal character in LLVM value name!");
116 if (C == '\\') {
117 OS << "\\\\";
118 } else if (isprint(C)) {
119 OS << C;
120 } else {
121 OS << '\\';
Daniel Dunbar389529a2008-10-14 23:28:09 +0000122 OS << hexdigit((C >> 4) & 0x0F);
123 OS << hexdigit((C >> 0) & 0x0F);
Chris Lattner3eee99c2008-08-19 04:36:02 +0000124 }
125 }
126 OS << '"';
127}
128
Daniel Dunbar389529a2008-10-14 23:28:09 +0000129/// getLLVMName - Turn the specified string into an 'LLVM name', which is
130/// surrounded with ""'s and escaped if it has special chars in it.
131static std::string getLLVMName(const std::string &Name) {
132 assert(!Name.empty() && "Cannot get empty name!");
133 std::string result;
134 raw_string_ostream OS(result);
135 PrintLLVMName(OS, Name.c_str(), Name.length(), NoPrefix);
136 return OS.str();
137}
138
Chris Lattner3eee99c2008-08-19 04:36:02 +0000139/// PrintLLVMName - Turn the specified name into an 'LLVM name', which is either
140/// prefixed with % (if the string only contains simple characters) or is
141/// surrounded with ""'s (if it has special chars in it). Print it out.
Chris Lattner0c19df42008-08-23 22:23:09 +0000142static void PrintLLVMName(raw_ostream &OS, const Value *V) {
Chris Lattner1508d3f2008-08-19 05:16:28 +0000143 PrintLLVMName(OS, V->getNameStart(), V->getNameLen(),
Chris Lattner3eee99c2008-08-19 04:36:02 +0000144 isa<GlobalValue>(V) ? GlobalPrefix : LocalPrefix);
145}
146
147
148
149//===----------------------------------------------------------------------===//
150// SlotTracker Class: Enumerate slot numbers for unnamed values
151//===----------------------------------------------------------------------===//
152
Chris Lattner3ee58762008-08-19 04:28:07 +0000153namespace {
154
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000155/// This class provides computation of slot numbers for LLVM Assembly writing.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000156///
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000157class SlotTracker {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000158public:
Chris Lattner393b7cd2008-08-17 04:17:45 +0000159 /// ValueMap - A mapping of Values to slot numbers
Chris Lattnera204d412008-08-17 17:25:25 +0000160 typedef DenseMap<const Value*, unsigned> ValueMap;
Chris Lattner393b7cd2008-08-17 04:17:45 +0000161
162private:
163 /// TheModule - The module for which we are holding slot numbers
164 const Module* TheModule;
165
166 /// TheFunction - The function for which we are holding slot numbers
167 const Function* TheFunction;
168 bool FunctionProcessed;
169
170 /// mMap - The TypePlanes map for the module level data
171 ValueMap mMap;
172 unsigned mNext;
173
174 /// fMap - The TypePlanes map for the function level data
175 ValueMap fMap;
176 unsigned fNext;
177
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000178public:
Chris Lattner393b7cd2008-08-17 04:17:45 +0000179 /// Construct from a module
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000180 explicit SlotTracker(const Module *M);
Chris Lattner393b7cd2008-08-17 04:17:45 +0000181 /// Construct from a function, starting out in incorp state.
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000182 explicit SlotTracker(const Function *F);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000183
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000184 /// Return the slot number of the specified value in it's type
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000185 /// plane. If something is not in the SlotTracker, return -1.
Chris Lattner5e043322007-01-11 03:54:27 +0000186 int getLocalSlot(const Value *V);
187 int getGlobalSlot(const GlobalValue *V);
Reid Spencer8beac692004-06-09 15:26:53 +0000188
Misha Brukmanb1c93172005-04-21 23:48:37 +0000189 /// If you'd like to deal with a function instead of just a module, use
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000190 /// this method to get its data into the SlotTracker.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000191 void incorporateFunction(const Function *F) {
192 TheFunction = F;
Reid Spencerb0ac8c42004-08-16 07:46:33 +0000193 FunctionProcessed = false;
194 }
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000195
Misha Brukmanb1c93172005-04-21 23:48:37 +0000196 /// After calling incorporateFunction, use this method to remove the
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000197 /// most recently incorporated function from the SlotTracker. This
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000198 /// will reset the state of the machine back to just the module contents.
199 void purgeFunction();
200
Chris Lattner393b7cd2008-08-17 04:17:45 +0000201 // Implementation Details
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000202private:
Reid Spencer56010e42004-05-26 21:56:09 +0000203 /// This function does the actual initialization.
204 inline void initialize();
205
Chris Lattnerea862a32007-01-09 07:55:49 +0000206 /// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
207 void CreateModuleSlot(const GlobalValue *V);
208
209 /// CreateFunctionSlot - Insert the specified Value* into the slot table.
210 void CreateFunctionSlot(const Value *V);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000211
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000212 /// Add all of the module level global variables (and their initializers)
213 /// and function declarations, but not the contents of those functions.
214 void processModule();
215
Reid Spencer56010e42004-05-26 21:56:09 +0000216 /// Add all of the functions arguments, basic blocks, and instructions
217 void processFunction();
218
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000219 SlotTracker(const SlotTracker &); // DO NOT IMPLEMENT
220 void operator=(const SlotTracker &); // DO NOT IMPLEMENT
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000221};
222
Chris Lattner3ee58762008-08-19 04:28:07 +0000223} // end anonymous namespace
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000224
Chris Lattner3eee99c2008-08-19 04:36:02 +0000225
226static SlotTracker *createSlotTracker(const Value *V) {
227 if (const Argument *FA = dyn_cast<Argument>(V))
228 return new SlotTracker(FA->getParent());
229
230 if (const Instruction *I = dyn_cast<Instruction>(V))
231 return new SlotTracker(I->getParent()->getParent());
232
233 if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
234 return new SlotTracker(BB->getParent());
235
236 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
237 return new SlotTracker(GV->getParent());
238
239 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V))
240 return new SlotTracker(GA->getParent());
241
242 if (const Function *Func = dyn_cast<Function>(V))
243 return new SlotTracker(Func);
244
245 return 0;
246}
247
248#if 0
Chris Lattner604e3512008-08-19 04:47:09 +0000249#define ST_DEBUG(X) cerr << X
Chris Lattner3eee99c2008-08-19 04:36:02 +0000250#else
Chris Lattner604e3512008-08-19 04:47:09 +0000251#define ST_DEBUG(X)
Chris Lattner3eee99c2008-08-19 04:36:02 +0000252#endif
253
254// Module level constructor. Causes the contents of the Module (sans functions)
255// to be added to the slot table.
256SlotTracker::SlotTracker(const Module *M)
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000257 : TheModule(M), TheFunction(0), FunctionProcessed(false), mNext(0), fNext(0) {
Chris Lattner3eee99c2008-08-19 04:36:02 +0000258}
259
260// Function level constructor. Causes the contents of the Module and the one
261// function provided to be added to the slot table.
262SlotTracker::SlotTracker(const Function *F)
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000263 : TheModule(F ? F->getParent() : 0), TheFunction(F), FunctionProcessed(false),
264 mNext(0), fNext(0) {
Chris Lattner3eee99c2008-08-19 04:36:02 +0000265}
266
267inline void SlotTracker::initialize() {
268 if (TheModule) {
269 processModule();
270 TheModule = 0; ///< Prevent re-processing next time we're called.
271 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000272
Chris Lattner3eee99c2008-08-19 04:36:02 +0000273 if (TheFunction && !FunctionProcessed)
274 processFunction();
275}
276
277// Iterate through all the global variables, functions, and global
278// variable initializers and create slots for them.
279void SlotTracker::processModule() {
Chris Lattner604e3512008-08-19 04:47:09 +0000280 ST_DEBUG("begin processModule!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000281
282 // Add all of the unnamed global variables to the value table.
283 for (Module::const_global_iterator I = TheModule->global_begin(),
284 E = TheModule->global_end(); I != E; ++I)
285 if (!I->hasName())
286 CreateModuleSlot(I);
287
288 // Add all the unnamed functions to the table.
289 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
290 I != E; ++I)
291 if (!I->hasName())
292 CreateModuleSlot(I);
293
Chris Lattner604e3512008-08-19 04:47:09 +0000294 ST_DEBUG("end processModule!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000295}
296
297
298// Process the arguments, basic blocks, and instructions of a function.
299void SlotTracker::processFunction() {
Chris Lattner604e3512008-08-19 04:47:09 +0000300 ST_DEBUG("begin processFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000301 fNext = 0;
302
303 // Add all the function arguments with no names.
304 for(Function::const_arg_iterator AI = TheFunction->arg_begin(),
305 AE = TheFunction->arg_end(); AI != AE; ++AI)
306 if (!AI->hasName())
307 CreateFunctionSlot(AI);
308
Chris Lattner604e3512008-08-19 04:47:09 +0000309 ST_DEBUG("Inserting Instructions:\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000310
311 // Add all of the basic blocks and instructions with no names.
312 for (Function::const_iterator BB = TheFunction->begin(),
313 E = TheFunction->end(); BB != E; ++BB) {
314 if (!BB->hasName())
315 CreateFunctionSlot(BB);
316 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
317 if (I->getType() != Type::VoidTy && !I->hasName())
318 CreateFunctionSlot(I);
319 }
320
321 FunctionProcessed = true;
322
Chris Lattner604e3512008-08-19 04:47:09 +0000323 ST_DEBUG("end processFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000324}
325
326/// Clean up after incorporating a function. This is the only way to get out of
327/// the function incorporation state that affects get*Slot/Create*Slot. Function
328/// incorporation state is indicated by TheFunction != 0.
329void SlotTracker::purgeFunction() {
Chris Lattner604e3512008-08-19 04:47:09 +0000330 ST_DEBUG("begin purgeFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000331 fMap.clear(); // Simply discard the function level map
332 TheFunction = 0;
333 FunctionProcessed = false;
Chris Lattner604e3512008-08-19 04:47:09 +0000334 ST_DEBUG("end purgeFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000335}
336
337/// getGlobalSlot - Get the slot number of a global value.
338int SlotTracker::getGlobalSlot(const GlobalValue *V) {
339 // Check for uninitialized state and do lazy initialization.
340 initialize();
341
342 // Find the type plane in the module map
343 ValueMap::iterator MI = mMap.find(V);
Dan Gohman1dd27572008-10-01 19:58:59 +0000344 return MI == mMap.end() ? -1 : (int)MI->second;
Chris Lattner3eee99c2008-08-19 04:36:02 +0000345}
346
347
348/// getLocalSlot - Get the slot number for a value that is local to a function.
349int SlotTracker::getLocalSlot(const Value *V) {
350 assert(!isa<Constant>(V) && "Can't get a constant or global slot with this!");
351
352 // Check for uninitialized state and do lazy initialization.
353 initialize();
354
355 ValueMap::iterator FI = fMap.find(V);
Dan Gohman1dd27572008-10-01 19:58:59 +0000356 return FI == fMap.end() ? -1 : (int)FI->second;
Chris Lattner3eee99c2008-08-19 04:36:02 +0000357}
358
359
360/// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
361void SlotTracker::CreateModuleSlot(const GlobalValue *V) {
362 assert(V && "Can't insert a null Value into SlotTracker!");
363 assert(V->getType() != Type::VoidTy && "Doesn't need a slot!");
364 assert(!V->hasName() && "Doesn't need a slot!");
365
366 unsigned DestSlot = mNext++;
367 mMap[V] = DestSlot;
368
Chris Lattner604e3512008-08-19 04:47:09 +0000369 ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
Chris Lattner3eee99c2008-08-19 04:36:02 +0000370 DestSlot << " [");
371 // G = Global, F = Function, A = Alias, o = other
Chris Lattner604e3512008-08-19 04:47:09 +0000372 ST_DEBUG((isa<GlobalVariable>(V) ? 'G' :
Chris Lattner3eee99c2008-08-19 04:36:02 +0000373 (isa<Function>(V) ? 'F' :
374 (isa<GlobalAlias>(V) ? 'A' : 'o'))) << "]\n");
375}
376
377
378/// CreateSlot - Create a new slot for the specified value if it has no name.
379void SlotTracker::CreateFunctionSlot(const Value *V) {
380 assert(V->getType() != Type::VoidTy && !V->hasName() &&
381 "Doesn't need a slot!");
382
383 unsigned DestSlot = fNext++;
384 fMap[V] = DestSlot;
385
386 // G = Global, F = Function, o = other
Chris Lattner604e3512008-08-19 04:47:09 +0000387 ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
Chris Lattner3eee99c2008-08-19 04:36:02 +0000388 DestSlot << " [o]\n");
389}
390
391
392
393//===----------------------------------------------------------------------===//
394// AsmWriter Implementation
395//===----------------------------------------------------------------------===//
Chris Lattner7f8845a2002-07-23 18:07:49 +0000396
Chris Lattner0c19df42008-08-23 22:23:09 +0000397static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
Chris Lattnera9f0a112006-12-06 05:50:41 +0000398 std::map<const Type *, std::string> &TypeTable,
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000399 SlotTracker *Machine);
Reid Spencer58d30f22004-07-04 11:50:43 +0000400
Chris Lattner033935d2008-08-17 04:40:13 +0000401
Chris Lattnerb86620e2001-10-29 16:37:48 +0000402
Misha Brukmanc566ca362004-03-02 00:22:19 +0000403/// fillTypeNameTable - If the module has a symbol table, take all global types
404/// and stuff their names into the TypeNames map.
405///
Chris Lattnerb86620e2001-10-29 16:37:48 +0000406static void fillTypeNameTable(const Module *M,
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000407 std::map<const Type *, std::string> &TypeNames) {
Chris Lattner98cf1f52002-11-20 18:36:02 +0000408 if (!M) return;
Reid Spencer32af9e82007-01-06 07:24:44 +0000409 const TypeSymbolTable &ST = M->getTypeSymbolTable();
410 TypeSymbolTable::const_iterator TI = ST.begin();
411 for (; TI != ST.end(); ++TI) {
Reid Spencere7e96712004-05-25 08:53:40 +0000412 // As a heuristic, don't insert pointer to primitive types, because
413 // they are used too often to have a single useful name.
414 //
415 const Type *Ty = cast<Type>(TI->second);
416 if (!isa<PointerType>(Ty) ||
Reid Spencer56010e42004-05-26 21:56:09 +0000417 !cast<PointerType>(Ty)->getElementType()->isPrimitiveType() ||
Chris Lattner03c49532007-01-15 02:27:26 +0000418 !cast<PointerType>(Ty)->getElementType()->isInteger() ||
Reid Spencer56010e42004-05-26 21:56:09 +0000419 isa<OpaqueType>(cast<PointerType>(Ty)->getElementType()))
Chris Lattner585297e82008-08-19 05:26:17 +0000420 TypeNames.insert(std::make_pair(Ty, '%' + getLLVMName(TI->first)));
Chris Lattnerb86620e2001-10-29 16:37:48 +0000421 }
422}
423
424
425
Misha Brukmanb1c93172005-04-21 23:48:37 +0000426static void calcTypeName(const Type *Ty,
John Criswellcd116ba2004-06-01 14:54:08 +0000427 std::vector<const Type *> &TypeStack,
428 std::map<const Type *, std::string> &TypeNames,
Chris Lattner585297e82008-08-19 05:26:17 +0000429 std::string &Result) {
Chris Lattner03c49532007-01-15 02:27:26 +0000430 if (Ty->isInteger() || (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty))) {
John Criswellcd116ba2004-06-01 14:54:08 +0000431 Result += Ty->getDescription(); // Base case
432 return;
433 }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000434
435 // Check to see if the type is named.
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000436 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
John Criswellcd116ba2004-06-01 14:54:08 +0000437 if (I != TypeNames.end()) {
438 Result += I->second;
439 return;
440 }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000441
John Criswellcd116ba2004-06-01 14:54:08 +0000442 if (isa<OpaqueType>(Ty)) {
443 Result += "opaque";
444 return;
445 }
Chris Lattnerf14ead92003-10-30 00:22:33 +0000446
Chris Lattnerb86620e2001-10-29 16:37:48 +0000447 // Check to see if the Type is already on the stack...
448 unsigned Slot = 0, CurSize = TypeStack.size();
449 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
450
Misha Brukmanb1c93172005-04-21 23:48:37 +0000451 // This is another base case for the recursion. In this case, we know
Chris Lattnerb86620e2001-10-29 16:37:48 +0000452 // that we have looped back to a type that we have previously visited.
453 // Generate the appropriate upreference to handle this.
John Criswellcd116ba2004-06-01 14:54:08 +0000454 if (Slot < CurSize) {
455 Result += "\\" + utostr(CurSize-Slot); // Here's the upreference
456 return;
457 }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000458
459 TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
Misha Brukmanb1c93172005-04-21 23:48:37 +0000460
Chris Lattner6b727592004-06-17 18:19:28 +0000461 switch (Ty->getTypeID()) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000462 case Type::IntegerTyID: {
463 unsigned BitWidth = cast<IntegerType>(Ty)->getBitWidth();
Reid Spencerc8721592007-01-12 07:25:20 +0000464 Result += "i" + utostr(BitWidth);
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000465 break;
466 }
Chris Lattner91db5822002-03-29 03:44:36 +0000467 case Type::FunctionTyID: {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000468 const FunctionType *FTy = cast<FunctionType>(Ty);
John Criswellcd116ba2004-06-01 14:54:08 +0000469 calcTypeName(FTy->getReturnType(), TypeStack, TypeNames, Result);
470 Result += " (";
Chris Lattnerfa829be2004-02-09 04:14:01 +0000471 for (FunctionType::param_iterator I = FTy->param_begin(),
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000472 E = FTy->param_end(); I != E; ++I) {
Chris Lattnerfa829be2004-02-09 04:14:01 +0000473 if (I != FTy->param_begin())
Chris Lattnerb86620e2001-10-29 16:37:48 +0000474 Result += ", ";
John Criswellcd116ba2004-06-01 14:54:08 +0000475 calcTypeName(*I, TypeStack, TypeNames, Result);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000476 }
Chris Lattnerd816b532002-04-13 20:53:41 +0000477 if (FTy->isVarArg()) {
Chris Lattnerfa829be2004-02-09 04:14:01 +0000478 if (FTy->getNumParams()) Result += ", ";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000479 Result += "...";
480 }
481 Result += ")";
482 break;
483 }
484 case Type::StructTyID: {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000485 const StructType *STy = cast<StructType>(Ty);
Andrew Lenharthdcb3c972006-12-08 18:06:16 +0000486 if (STy->isPacked())
487 Result += '<';
John Criswellcd116ba2004-06-01 14:54:08 +0000488 Result += "{ ";
Chris Lattnerac6db752004-02-09 04:37:31 +0000489 for (StructType::element_iterator I = STy->element_begin(),
490 E = STy->element_end(); I != E; ++I) {
John Criswellcd116ba2004-06-01 14:54:08 +0000491 calcTypeName(*I, TypeStack, TypeNames, Result);
Dan Gohmanbb9a2112008-09-25 17:37:20 +0000492 if (next(I) != STy->element_end())
493 Result += ',';
494 Result += ' ';
Chris Lattnerb86620e2001-10-29 16:37:48 +0000495 }
Dan Gohmanbb9a2112008-09-25 17:37:20 +0000496 Result += '}';
Andrew Lenharthdcb3c972006-12-08 18:06:16 +0000497 if (STy->isPacked())
498 Result += '>';
Chris Lattnerb86620e2001-10-29 16:37:48 +0000499 break;
500 }
Christopher Lamb54dd24c2007-12-11 08:59:05 +0000501 case Type::PointerTyID: {
502 const PointerType *PTy = cast<PointerType>(Ty);
Chris Lattner585297e82008-08-19 05:26:17 +0000503 calcTypeName(PTy->getElementType(), TypeStack, TypeNames, Result);
Christopher Lamb54dd24c2007-12-11 08:59:05 +0000504 if (unsigned AddressSpace = PTy->getAddressSpace())
505 Result += " addrspace(" + utostr(AddressSpace) + ")";
John Criswellcd116ba2004-06-01 14:54:08 +0000506 Result += "*";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000507 break;
Christopher Lamb54dd24c2007-12-11 08:59:05 +0000508 }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000509 case Type::ArrayTyID: {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000510 const ArrayType *ATy = cast<ArrayType>(Ty);
John Criswellcd116ba2004-06-01 14:54:08 +0000511 Result += "[" + utostr(ATy->getNumElements()) + " x ";
512 calcTypeName(ATy->getElementType(), TypeStack, TypeNames, Result);
513 Result += "]";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000514 break;
515 }
Reid Spencerd84d35b2007-02-15 02:26:10 +0000516 case Type::VectorTyID: {
517 const VectorType *PTy = cast<VectorType>(Ty);
Brian Gaeke02209042004-08-20 06:00:58 +0000518 Result += "<" + utostr(PTy->getNumElements()) + " x ";
519 calcTypeName(PTy->getElementType(), TypeStack, TypeNames, Result);
520 Result += ">";
521 break;
522 }
Chris Lattner15285ab2003-05-14 17:50:47 +0000523 case Type::OpaqueTyID:
John Criswellcd116ba2004-06-01 14:54:08 +0000524 Result += "opaque";
Chris Lattner15285ab2003-05-14 17:50:47 +0000525 break;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000526 default:
John Criswellcd116ba2004-06-01 14:54:08 +0000527 Result += "<unrecognized-type>";
Chris Lattnerfc9f1c92006-12-06 06:40:49 +0000528 break;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000529 }
530
531 TypeStack.pop_back(); // Remove self from stack...
Chris Lattnerb86620e2001-10-29 16:37:48 +0000532}
533
534
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000535/// printTypeInt - The internal guts of printing out a type that has a
536/// potentially named portion.
537///
Chris Lattner0c19df42008-08-23 22:23:09 +0000538static void printTypeInt(raw_ostream &Out, const Type *Ty,
Chris Lattner585297e82008-08-19 05:26:17 +0000539 std::map<const Type *, std::string> &TypeNames) {
Chris Lattnerb86620e2001-10-29 16:37:48 +0000540 // Primitive types always print out their description, regardless of whether
541 // they have been named or not.
542 //
Chris Lattner585297e82008-08-19 05:26:17 +0000543 if (Ty->isInteger() || (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty))) {
544 Out << Ty->getDescription();
545 return;
546 }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000547
548 // Check to see if the type is named.
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000549 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
Chris Lattner585297e82008-08-19 05:26:17 +0000550 if (I != TypeNames.end()) {
551 Out << I->second;
552 return;
553 }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000554
555 // Otherwise we have a type that has not been named but is a derived type.
556 // Carefully recurse the type hierarchy to print out any contained symbolic
557 // names.
558 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000559 std::vector<const Type *> TypeStack;
John Criswellcd116ba2004-06-01 14:54:08 +0000560 std::string TypeName;
561 calcTypeName(Ty, TypeStack, TypeNames, TypeName);
Chris Lattner7f74a562002-01-20 22:54:45 +0000562 TypeNames.insert(std::make_pair(Ty, TypeName));//Cache type name for later use
Chris Lattner585297e82008-08-19 05:26:17 +0000563 Out << TypeName;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000564}
565
Chris Lattner34b95182001-10-31 04:33:19 +0000566
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000567/// WriteTypeSymbolic - This attempts to write the specified type as a symbolic
568/// type, iff there is an entry in the modules symbol table for the specified
569/// type or one of it's component types. This is slower than a simple x << Type
570///
Chris Lattner604e3512008-08-19 04:47:09 +0000571void llvm::WriteTypeSymbolic(std::ostream &Out, const Type *Ty,
572 const Module *M) {
Chris Lattner0c19df42008-08-23 22:23:09 +0000573 raw_os_ostream RO(Out);
574 WriteTypeSymbolic(RO, Ty, M);
575}
576
577void llvm::WriteTypeSymbolic(raw_ostream &Out, const Type *Ty, const Module *M){
Misha Brukmanb1c93172005-04-21 23:48:37 +0000578 Out << ' ';
Chris Lattnerb86620e2001-10-29 16:37:48 +0000579
Chris Lattnerfc9f1c92006-12-06 06:40:49 +0000580 // If they want us to print out a type, but there is no context, we can't
581 // print it symbolically.
Chris Lattner604e3512008-08-19 04:47:09 +0000582 if (!M) {
583 Out << Ty->getDescription();
584 } else {
585 std::map<const Type *, std::string> TypeNames;
586 fillTypeNameTable(M, TypeNames);
587 printTypeInt(Out, Ty, TypeNames);
588 }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000589}
590
Chris Lattner6ed87bd2006-01-23 23:03:36 +0000591// PrintEscapedString - Print each character of the specified string, escaping
592// it if it is not printable or if it is an escape char.
Chris Lattner0c19df42008-08-23 22:23:09 +0000593static void PrintEscapedString(const std::string &Str, raw_ostream &Out) {
Chris Lattner6ed87bd2006-01-23 23:03:36 +0000594 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
595 unsigned char C = Str[i];
596 if (isprint(C) && C != '"' && C != '\\') {
597 Out << C;
598 } else {
599 Out << '\\'
600 << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'))
601 << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
602 }
603 }
604}
605
Chris Lattnerfc9f1c92006-12-06 06:40:49 +0000606static const char *getPredicateText(unsigned predicate) {
Reid Spencer812a1be2006-12-04 05:19:18 +0000607 const char * pred = "unknown";
608 switch (predicate) {
609 case FCmpInst::FCMP_FALSE: pred = "false"; break;
610 case FCmpInst::FCMP_OEQ: pred = "oeq"; break;
611 case FCmpInst::FCMP_OGT: pred = "ogt"; break;
612 case FCmpInst::FCMP_OGE: pred = "oge"; break;
613 case FCmpInst::FCMP_OLT: pred = "olt"; break;
614 case FCmpInst::FCMP_OLE: pred = "ole"; break;
615 case FCmpInst::FCMP_ONE: pred = "one"; break;
616 case FCmpInst::FCMP_ORD: pred = "ord"; break;
617 case FCmpInst::FCMP_UNO: pred = "uno"; break;
618 case FCmpInst::FCMP_UEQ: pred = "ueq"; break;
619 case FCmpInst::FCMP_UGT: pred = "ugt"; break;
620 case FCmpInst::FCMP_UGE: pred = "uge"; break;
621 case FCmpInst::FCMP_ULT: pred = "ult"; break;
622 case FCmpInst::FCMP_ULE: pred = "ule"; break;
623 case FCmpInst::FCMP_UNE: pred = "une"; break;
624 case FCmpInst::FCMP_TRUE: pred = "true"; break;
625 case ICmpInst::ICMP_EQ: pred = "eq"; break;
626 case ICmpInst::ICMP_NE: pred = "ne"; break;
627 case ICmpInst::ICMP_SGT: pred = "sgt"; break;
628 case ICmpInst::ICMP_SGE: pred = "sge"; break;
629 case ICmpInst::ICMP_SLT: pred = "slt"; break;
630 case ICmpInst::ICMP_SLE: pred = "sle"; break;
631 case ICmpInst::ICMP_UGT: pred = "ugt"; break;
632 case ICmpInst::ICMP_UGE: pred = "uge"; break;
633 case ICmpInst::ICMP_ULT: pred = "ult"; break;
634 case ICmpInst::ICMP_ULE: pred = "ule"; break;
635 }
636 return pred;
637}
638
Chris Lattner0c19df42008-08-23 22:23:09 +0000639static void WriteConstantInt(raw_ostream &Out, const Constant *CV,
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000640 std::map<const Type *, std::string> &TypeTable,
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000641 SlotTracker *Machine) {
Zhou Sheng75b871f2007-01-11 12:24:14 +0000642 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Chris Lattner17f71652008-08-17 07:19:36 +0000643 if (CI->getType() == Type::Int1Ty) {
Reid Spencercddc9df2007-01-12 04:24:46 +0000644 Out << (CI->getZExtValue() ? "true" : "false");
Chris Lattner17f71652008-08-17 07:19:36 +0000645 return;
646 }
647 Out << CI->getValue();
648 return;
649 }
650
651 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Dale Johannesen028084e2007-09-12 03:30:33 +0000652 if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEdouble ||
653 &CFP->getValueAPF().getSemantics() == &APFloat::IEEEsingle) {
654 // We would like to output the FP constant value in exponential notation,
655 // but we cannot do this if doing so will lose precision. Check here to
656 // make sure that we only output it in exponential format if we can parse
657 // the value back and get the same value.
658 //
659 bool isDouble = &CFP->getValueAPF().getSemantics()==&APFloat::IEEEdouble;
Chris Lattner17f71652008-08-17 07:19:36 +0000660 double Val = isDouble ? CFP->getValueAPF().convertToDouble() :
661 CFP->getValueAPF().convertToFloat();
Dale Johannesen028084e2007-09-12 03:30:33 +0000662 std::string StrVal = ftostr(CFP->getValueAPF());
Chris Lattner1e194682002-04-18 18:53:13 +0000663
Dale Johannesen028084e2007-09-12 03:30:33 +0000664 // Check to make sure that the stringized number is not some string like
665 // "Inf" or NaN, that atof will accept, but the lexer will not. Check
666 // that the string matches the "[-+]?[0-9]" regex.
667 //
668 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
669 ((StrVal[0] == '-' || StrVal[0] == '+') &&
670 (StrVal[1] >= '0' && StrVal[1] <= '9'))) {
671 // Reparse stringized version!
672 if (atof(StrVal.c_str()) == Val) {
673 Out << StrVal;
674 return;
675 }
Chris Lattner1e194682002-04-18 18:53:13 +0000676 }
Dale Johannesen028084e2007-09-12 03:30:33 +0000677 // Otherwise we could not reparse it to exactly the same value, so we must
678 // output the string in hexadecimal format!
679 assert(sizeof(double) == sizeof(uint64_t) &&
680 "assuming that double is 64 bits!");
681 Out << "0x" << utohexstr(DoubleToBits(Val));
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000682 return;
683 }
684
685 // Some form of long double. These appear as a magic letter identifying
686 // the type, then a fixed number of hex digits.
687 Out << "0x";
688 if (&CFP->getValueAPF().getSemantics() == &APFloat::x87DoubleExtended)
689 Out << 'K';
690 else if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEquad)
691 Out << 'L';
692 else if (&CFP->getValueAPF().getSemantics() == &APFloat::PPCDoubleDouble)
693 Out << 'M';
694 else
695 assert(0 && "Unsupported floating point type");
696 // api needed to prevent premature destruction
Dale Johannesen54306fe2008-10-09 18:53:47 +0000697 APInt api = CFP->getValueAPF().bitcastToAPInt();
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000698 const uint64_t* p = api.getRawData();
699 uint64_t word = *p;
700 int shiftcount=60;
701 int width = api.getBitWidth();
702 for (int j=0; j<width; j+=4, shiftcount-=4) {
703 unsigned int nibble = (word>>shiftcount) & 15;
704 if (nibble < 10)
705 Out << (unsigned char)(nibble + '0');
Dale Johannesen028084e2007-09-12 03:30:33 +0000706 else
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000707 Out << (unsigned char)(nibble - 10 + 'A');
708 if (shiftcount == 0 && j+4 < width) {
709 word = *(++p);
710 shiftcount = 64;
711 if (width-j-4 < 64)
712 shiftcount = width-j-4;
Dale Johannesen028084e2007-09-12 03:30:33 +0000713 }
714 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000715 return;
716 }
717
718 if (isa<ConstantAggregateZero>(CV)) {
Chris Lattner76b2ff42004-02-15 05:55:15 +0000719 Out << "zeroinitializer";
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000720 return;
721 }
722
723 if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
Chris Lattner1e194682002-04-18 18:53:13 +0000724 // As a special case, print the array as a string if it is an array of
Dan Gohmane9bc2ba2008-05-12 16:34:30 +0000725 // i8 with ConstantInt values.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000726 //
Chris Lattner1e194682002-04-18 18:53:13 +0000727 const Type *ETy = CA->getType()->getElementType();
Chris Lattner6ed87bd2006-01-23 23:03:36 +0000728 if (CA->isString()) {
Chris Lattner1e194682002-04-18 18:53:13 +0000729 Out << "c\"";
Chris Lattner6ed87bd2006-01-23 23:03:36 +0000730 PrintEscapedString(CA->getAsString(), Out);
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000731 Out << '"';
Chris Lattner1e194682002-04-18 18:53:13 +0000732 } else { // Cannot output in string format...
Misha Brukman21bbdb92004-06-04 21:11:51 +0000733 Out << '[';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000734 if (CA->getNumOperands()) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000735 Out << ' ';
Chris Lattner1e194682002-04-18 18:53:13 +0000736 printTypeInt(Out, ETy, TypeTable);
Dan Gohman81313fd2008-09-14 17:21:12 +0000737 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000738 WriteAsOperandInternal(Out, CA->getOperand(0),
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000739 TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000740 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
741 Out << ", ";
Chris Lattner1e194682002-04-18 18:53:13 +0000742 printTypeInt(Out, ETy, TypeTable);
Dan Gohman81313fd2008-09-14 17:21:12 +0000743 Out << ' ';
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000744 WriteAsOperandInternal(Out, CA->getOperand(i), TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000745 }
Dan Gohman81313fd2008-09-14 17:21:12 +0000746 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000747 }
Dan Gohman81313fd2008-09-14 17:21:12 +0000748 Out << ']';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000749 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000750 return;
751 }
752
753 if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Andrew Lenharth0d124b82007-01-08 18:21:30 +0000754 if (CS->getType()->isPacked())
755 Out << '<';
Misha Brukman21bbdb92004-06-04 21:11:51 +0000756 Out << '{';
Jim Laskey3bb78742006-02-25 12:27:03 +0000757 unsigned N = CS->getNumOperands();
758 if (N) {
Chris Lattner604e3512008-08-19 04:47:09 +0000759 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000760 printTypeInt(Out, CS->getOperand(0)->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(0), TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000764
Jim Laskey3bb78742006-02-25 12:27:03 +0000765 for (unsigned i = 1; i < N; i++) {
Chris Lattnerd84bb632002-04-16 21:36:08 +0000766 Out << ", ";
767 printTypeInt(Out, CS->getOperand(i)->getType(), TypeTable);
Dan Gohman81313fd2008-09-14 17:21:12 +0000768 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000769
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000770 WriteAsOperandInternal(Out, CS->getOperand(i), TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000771 }
Dan Gohman81313fd2008-09-14 17:21:12 +0000772 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000773 }
Jim Laskey3bb78742006-02-25 12:27:03 +0000774
Dan Gohman81313fd2008-09-14 17:21:12 +0000775 Out << '}';
Andrew Lenharth0d124b82007-01-08 18:21:30 +0000776 if (CS->getType()->isPacked())
777 Out << '>';
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000778 return;
779 }
780
781 if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
782 const Type *ETy = CP->getType()->getElementType();
783 assert(CP->getNumOperands() > 0 &&
784 "Number of operands for a PackedConst must be > 0");
785 Out << "< ";
786 printTypeInt(Out, ETy, TypeTable);
Dan Gohman81313fd2008-09-14 17:21:12 +0000787 Out << ' ';
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000788 WriteAsOperandInternal(Out, CP->getOperand(0), TypeTable, Machine);
789 for (unsigned i = 1, e = CP->getNumOperands(); i != e; ++i) {
Chris Lattner585297e82008-08-19 05:26:17 +0000790 Out << ", ";
791 printTypeInt(Out, ETy, TypeTable);
Dan Gohman81313fd2008-09-14 17:21:12 +0000792 Out << ' ';
Chris Lattner585297e82008-08-19 05:26:17 +0000793 WriteAsOperandInternal(Out, CP->getOperand(i), TypeTable, Machine);
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000794 }
795 Out << " >";
796 return;
797 }
798
799 if (isa<ConstantPointerNull>(CV)) {
Chris Lattnerd84bb632002-04-16 21:36:08 +0000800 Out << "null";
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000801 return;
802 }
803
804 if (isa<UndefValue>(CV)) {
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000805 Out << "undef";
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000806 return;
807 }
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000808
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000809 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Reid Spencer812a1be2006-12-04 05:19:18 +0000810 Out << CE->getOpcodeName();
811 if (CE->isCompare())
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000812 Out << ' ' << getPredicateText(CE->getPredicate());
Reid Spencer812a1be2006-12-04 05:19:18 +0000813 Out << " (";
Misha Brukmanb1c93172005-04-21 23:48:37 +0000814
Vikram S. Adveb952b542002-07-14 23:14:45 +0000815 for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
816 printTypeInt(Out, (*OI)->getType(), TypeTable);
Dan Gohman81313fd2008-09-14 17:21:12 +0000817 Out << ' ';
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000818 WriteAsOperandInternal(Out, *OI, TypeTable, Machine);
Vikram S. Adveb952b542002-07-14 23:14:45 +0000819 if (OI+1 != CE->op_end())
Chris Lattner3cd8c562002-07-30 18:54:25 +0000820 Out << ", ";
Vikram S. Adveb952b542002-07-14 23:14:45 +0000821 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000822
Dan Gohmana76f0f72008-05-31 19:12:39 +0000823 if (CE->hasIndices()) {
824 const SmallVector<unsigned, 4> &Indices = CE->getIndices();
825 for (unsigned i = 0, e = Indices.size(); i != e; ++i)
826 Out << ", " << Indices[i];
827 }
828
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000829 if (CE->isCast()) {
Chris Lattner83b396b2002-08-15 19:37:43 +0000830 Out << " to ";
831 printTypeInt(Out, CE->getType(), TypeTable);
832 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000833
Misha Brukman21bbdb92004-06-04 21:11:51 +0000834 Out << ')';
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000835 return;
Chris Lattnerd84bb632002-04-16 21:36:08 +0000836 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000837
838 Out << "<placeholder or erroneous Constant>";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000839}
840
841
Misha Brukmanc566ca362004-03-02 00:22:19 +0000842/// WriteAsOperand - Write the name of the specified value out to the specified
843/// ostream. This can be useful when you just want to print int %reg126, not
844/// the whole instruction that generated it.
845///
Chris Lattner0c19df42008-08-23 22:23:09 +0000846static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000847 std::map<const Type*, std::string> &TypeTable,
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000848 SlotTracker *Machine) {
Chris Lattner033935d2008-08-17 04:40:13 +0000849 if (V->hasName()) {
850 PrintLLVMName(Out, V);
851 return;
852 }
853
854 const Constant *CV = dyn_cast<Constant>(V);
855 if (CV && !isa<GlobalValue>(CV)) {
856 WriteConstantInt(Out, CV, TypeTable, Machine);
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000857 return;
858 }
859
860 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
Chris Lattner033935d2008-08-17 04:40:13 +0000861 Out << "asm ";
862 if (IA->hasSideEffects())
863 Out << "sideeffect ";
864 Out << '"';
865 PrintEscapedString(IA->getAsmString(), Out);
866 Out << "\", \"";
867 PrintEscapedString(IA->getConstraintString(), Out);
868 Out << '"';
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000869 return;
870 }
871
872 char Prefix = '%';
873 int Slot;
874 if (Machine) {
875 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
876 Slot = Machine->getGlobalSlot(GV);
877 Prefix = '@';
878 } else {
879 Slot = Machine->getLocalSlot(V);
880 }
Chris Lattner033935d2008-08-17 04:40:13 +0000881 } else {
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000882 Machine = createSlotTracker(V);
Chris Lattner033935d2008-08-17 04:40:13 +0000883 if (Machine) {
884 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
885 Slot = Machine->getGlobalSlot(GV);
886 Prefix = '@';
887 } else {
888 Slot = Machine->getLocalSlot(V);
889 }
Chris Lattnera2d810d2006-01-25 22:26:05 +0000890 } else {
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000891 Slot = -1;
Chris Lattnerd84bb632002-04-16 21:36:08 +0000892 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000893 delete Machine;
Chris Lattnerd84bb632002-04-16 21:36:08 +0000894 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000895
896 if (Slot != -1)
897 Out << Prefix << Slot;
898 else
899 Out << "<badref>";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000900}
901
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000902/// WriteAsOperand - Write the name of the specified value out to the specified
903/// ostream. This can be useful when you just want to print int %reg126, not
904/// the whole instruction that generated it.
905///
Chris Lattner604e3512008-08-19 04:47:09 +0000906void llvm::WriteAsOperand(std::ostream &Out, const Value *V, bool PrintType,
907 const Module *Context) {
Chris Lattner0c19df42008-08-23 22:23:09 +0000908 raw_os_ostream OS(Out);
909 WriteAsOperand(OS, V, PrintType, Context);
910}
911
912void llvm::WriteAsOperand(raw_ostream &Out, const Value *V, bool PrintType,
913 const Module *Context) {
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000914 std::map<const Type *, std::string> TypeNames;
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000915 if (Context == 0) Context = getModuleFromVal(V);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000916
Chris Lattner98cf1f52002-11-20 18:36:02 +0000917 if (Context)
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000918 fillTypeNameTable(Context, TypeNames);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000919
Dan Gohman81313fd2008-09-14 17:21:12 +0000920 if (PrintType) {
Chris Lattnerd84bb632002-04-16 21:36:08 +0000921 printTypeInt(Out, V->getType(), TypeNames);
Dan Gohman81313fd2008-09-14 17:21:12 +0000922 Out << ' ';
923 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000924
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000925 WriteAsOperandInternal(Out, V, TypeNames, 0);
Chris Lattner5e5abe32001-07-20 19:15:21 +0000926}
927
Reid Spencer58d30f22004-07-04 11:50:43 +0000928
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000929namespace {
Chris Lattner2e9fee42001-07-12 23:35:26 +0000930
Chris Lattnerfee714f2001-09-07 16:36:04 +0000931class AssemblyWriter {
Chris Lattner0c19df42008-08-23 22:23:09 +0000932 raw_ostream &Out;
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000933 SlotTracker &Machine;
Chris Lattner7bfee412001-10-29 16:05:51 +0000934 const Module *TheModule;
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000935 std::map<const Type *, std::string> TypeNames;
Chris Lattner8339f7d2003-10-30 23:41:03 +0000936 AssemblyAnnotationWriter *AnnotationWriter;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000937public:
Chris Lattner0c19df42008-08-23 22:23:09 +0000938 inline AssemblyWriter(raw_ostream &o, SlotTracker &Mac, const Module *M,
Chris Lattner8339f7d2003-10-30 23:41:03 +0000939 AssemblyAnnotationWriter *AAW)
Misha Brukmana6619a92004-06-21 21:53:56 +0000940 : Out(o), Machine(Mac), TheModule(M), AnnotationWriter(AAW) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000941
942 // If the module has a symbol table, take all global types and stuff their
943 // names into the TypeNames map.
944 //
Chris Lattnerb86620e2001-10-29 16:37:48 +0000945 fillTypeNameTable(M, TypeNames);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000946 }
947
Chris Lattner0c19df42008-08-23 22:23:09 +0000948 void write(const Module *M) { printModule(M); }
949
950 void write(const GlobalValue *G) {
951 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(G))
952 printGlobal(GV);
953 else if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(G))
954 printAlias(GA);
955 else if (const Function *F = dyn_cast<Function>(G))
956 printFunction(F);
957 else
958 assert(0 && "Unknown global");
959 }
960
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000961 void write(const BasicBlock *BB) { printBasicBlock(BB); }
962 void write(const Instruction *I) { printInstruction(*I); }
963 void write(const Type *Ty) { printType(Ty); }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000964
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000965 void writeOperand(const Value *Op, bool PrintType);
Devang Patelba3fa6c2008-09-23 23:03:40 +0000966 void writeParamOperand(const Value *Operand, Attributes Attrs);
Chris Lattner1e194682002-04-18 18:53:13 +0000967
Misha Brukman4685e262004-04-28 15:31:21 +0000968 const Module* getModule() { return TheModule; }
969
Misha Brukmand92f54a2004-11-15 19:30:05 +0000970private:
Chris Lattner7bfee412001-10-29 16:05:51 +0000971 void printModule(const Module *M);
Reid Spencer32af9e82007-01-06 07:24:44 +0000972 void printTypeSymbolTable(const TypeSymbolTable &ST);
Chris Lattner7bfee412001-10-29 16:05:51 +0000973 void printGlobal(const GlobalVariable *GV);
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000974 void printAlias(const GlobalAlias *GV);
Chris Lattner57698e22002-03-26 18:01:55 +0000975 void printFunction(const Function *F);
Devang Patelba3fa6c2008-09-23 23:03:40 +0000976 void printArgument(const Argument *FA, Attributes Attrs);
Chris Lattner7bfee412001-10-29 16:05:51 +0000977 void printBasicBlock(const BasicBlock *BB);
Chris Lattner113f4f42002-06-25 16:13:24 +0000978 void printInstruction(const Instruction &I);
Chris Lattnerd816b532002-04-13 20:53:41 +0000979
980 // printType - Go to extreme measures to attempt to print out a short,
981 // symbolic version of a type name.
982 //
Chris Lattner585297e82008-08-19 05:26:17 +0000983 void printType(const Type *Ty) {
984 printTypeInt(Out, Ty, TypeNames);
Chris Lattnerd816b532002-04-13 20:53:41 +0000985 }
986
987 // printTypeAtLeastOneLevel - Print out one level of the possibly complex type
988 // without considering any symbolic types that we may have equal to it.
989 //
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000990 void printTypeAtLeastOneLevel(const Type *Ty);
Chris Lattner7bfee412001-10-29 16:05:51 +0000991
Chris Lattner862e3382001-10-13 06:42:36 +0000992 // printInfoComment - Print a little comment after the instruction indicating
993 // which slot it occupies.
Chris Lattner113f4f42002-06-25 16:13:24 +0000994 void printInfoComment(const Value &V);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000995};
Reid Spencerf43ac622004-05-27 22:04:46 +0000996} // end of llvm namespace
Chris Lattner2f7c9632001-06-06 20:29:01 +0000997
Misha Brukmanc566ca362004-03-02 00:22:19 +0000998/// printTypeAtLeastOneLevel - Print out one level of the possibly complex type
999/// without considering any symbolic types that we may have equal to it.
1000///
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001001void AssemblyWriter::printTypeAtLeastOneLevel(const Type *Ty) {
1002 if (const IntegerType *ITy = dyn_cast<IntegerType>(Ty)) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001003 Out << "i" << utostr(ITy->getBitWidth());
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001004 return;
1005 }
1006
1007 if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
Reid Spencer8c4914c2006-12-31 05:24:50 +00001008 printType(FTy->getReturnType());
Reid Spencer8c4914c2006-12-31 05:24:50 +00001009 Out << " (";
Chris Lattnerfa829be2004-02-09 04:14:01 +00001010 for (FunctionType::param_iterator I = FTy->param_begin(),
1011 E = FTy->param_end(); I != E; ++I) {
1012 if (I != FTy->param_begin())
Misha Brukmana6619a92004-06-21 21:53:56 +00001013 Out << ", ";
Chris Lattnerd84bb632002-04-16 21:36:08 +00001014 printType(*I);
Chris Lattnerd816b532002-04-13 20:53:41 +00001015 }
1016 if (FTy->isVarArg()) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001017 if (FTy->getNumParams()) Out << ", ";
1018 Out << "...";
Chris Lattnerd816b532002-04-13 20:53:41 +00001019 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001020 Out << ')';
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001021 return;
1022 }
1023
1024 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
Andrew Lenharthdcb3c972006-12-08 18:06:16 +00001025 if (STy->isPacked())
1026 Out << '<';
Misha Brukmana6619a92004-06-21 21:53:56 +00001027 Out << "{ ";
Chris Lattnerac6db752004-02-09 04:37:31 +00001028 for (StructType::element_iterator I = STy->element_begin(),
1029 E = STy->element_end(); I != E; ++I) {
1030 if (I != STy->element_begin())
Misha Brukmana6619a92004-06-21 21:53:56 +00001031 Out << ", ";
Chris Lattnerd816b532002-04-13 20:53:41 +00001032 printType(*I);
1033 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001034 Out << " }";
Andrew Lenharthdcb3c972006-12-08 18:06:16 +00001035 if (STy->isPacked())
1036 Out << '>';
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001037 return;
1038 }
1039
1040 if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
Christopher Lambac7d6312007-12-18 03:49:35 +00001041 printType(PTy->getElementType());
1042 if (unsigned AddressSpace = PTy->getAddressSpace())
1043 Out << " addrspace(" << AddressSpace << ")";
1044 Out << '*';
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001045 return;
1046 }
1047
1048 if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001049 Out << '[' << ATy->getNumElements() << " x ";
Chris Lattner585297e82008-08-19 05:26:17 +00001050 printType(ATy->getElementType());
1051 Out << ']';
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001052 return;
1053 }
1054
1055 if (const VectorType *PTy = dyn_cast<VectorType>(Ty)) {
Reid Spencere203d352004-08-20 15:37:30 +00001056 Out << '<' << PTy->getNumElements() << " x ";
Chris Lattner585297e82008-08-19 05:26:17 +00001057 printType(PTy->getElementType());
1058 Out << '>';
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001059 return;
Reid Spencere203d352004-08-20 15:37:30 +00001060 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001061
1062 if (isa<OpaqueType>(Ty)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001063 Out << "opaque";
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001064 return;
Chris Lattnerd816b532002-04-13 20:53:41 +00001065 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001066
1067 if (!Ty->isPrimitiveType())
1068 Out << "<unknown derived type>";
1069 printType(Ty);
Chris Lattnerd816b532002-04-13 20:53:41 +00001070}
1071
1072
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001073void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType) {
1074 if (Operand == 0) {
Chris Lattner08f7d0c2005-02-24 16:58:29 +00001075 Out << "<null operand!>";
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001076 } else {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001077 if (PrintType) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001078 printType(Operand->getType());
Dan Gohman81313fd2008-09-14 17:21:12 +00001079 Out << ' ';
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001080 }
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001081 WriteAsOperandInternal(Out, Operand, TypeNames, &Machine);
Chris Lattner08f7d0c2005-02-24 16:58:29 +00001082 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001083}
1084
Dale Johannesen89268bc2008-02-19 21:38:47 +00001085void AssemblyWriter::writeParamOperand(const Value *Operand,
Devang Patelba3fa6c2008-09-23 23:03:40 +00001086 Attributes Attrs) {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00001087 if (Operand == 0) {
1088 Out << "<null operand!>";
1089 } else {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00001090 // Print the type
1091 printType(Operand->getType());
1092 // Print parameter attributes list
Devang Patel4c758ea2008-09-25 21:00:45 +00001093 if (Attrs != Attribute::None)
1094 Out << ' ' << Attribute::getAsString(Attrs);
Dan Gohman81313fd2008-09-14 17:21:12 +00001095 Out << ' ';
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00001096 // Print the operand
1097 WriteAsOperandInternal(Out, Operand, TypeNames, &Machine);
1098 }
1099}
Chris Lattner2f7c9632001-06-06 20:29:01 +00001100
Chris Lattner7bfee412001-10-29 16:05:51 +00001101void AssemblyWriter::printModule(const Module *M) {
Chris Lattner4d8689e2005-03-02 23:12:40 +00001102 if (!M->getModuleIdentifier().empty() &&
Misha Brukmanb1c93172005-04-21 23:48:37 +00001103 // Don't print the ID if it will start a new line (which would
Chris Lattner4d8689e2005-03-02 23:12:40 +00001104 // require a comment char before it).
1105 M->getModuleIdentifier().find('\n') == std::string::npos)
1106 Out << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
1107
Owen Andersone2237542006-10-18 02:21:12 +00001108 if (!M->getDataLayout().empty())
Chris Lattner04897162006-10-22 06:06:56 +00001109 Out << "target datalayout = \"" << M->getDataLayout() << "\"\n";
Reid Spencer48f98c82004-07-25 21:44:54 +00001110 if (!M->getTargetTriple().empty())
Reid Spencerffec7df2004-07-25 21:29:43 +00001111 Out << "target triple = \"" << M->getTargetTriple() << "\"\n";
Misha Brukmanb1c93172005-04-21 23:48:37 +00001112
Chris Lattnereef2fe72006-01-24 04:13:11 +00001113 if (!M->getModuleInlineAsm().empty()) {
Chris Lattnerefaf35d2006-01-24 00:45:30 +00001114 // Split the string into lines, to make it easier to read the .ll file.
Chris Lattnereef2fe72006-01-24 04:13:11 +00001115 std::string Asm = M->getModuleInlineAsm();
Chris Lattnerefaf35d2006-01-24 00:45:30 +00001116 size_t CurPos = 0;
1117 size_t NewLine = Asm.find_first_of('\n', CurPos);
1118 while (NewLine != std::string::npos) {
1119 // We found a newline, print the portion of the asm string from the
1120 // last newline up to this newline.
1121 Out << "module asm \"";
1122 PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.begin()+NewLine),
1123 Out);
1124 Out << "\"\n";
1125 CurPos = NewLine+1;
1126 NewLine = Asm.find_first_of('\n', CurPos);
1127 }
Chris Lattner3acaf5c2006-01-24 00:40:17 +00001128 Out << "module asm \"";
Chris Lattnerefaf35d2006-01-24 00:45:30 +00001129 PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.end()), Out);
Chris Lattner6ed87bd2006-01-23 23:03:36 +00001130 Out << "\"\n";
1131 }
1132
Chris Lattner2cdd49d2004-09-14 05:06:58 +00001133 // Loop over the dependent libraries and emit them.
Chris Lattner730cfe42004-09-14 04:51:44 +00001134 Module::lib_iterator LI = M->lib_begin();
1135 Module::lib_iterator LE = M->lib_end();
Reid Spencer48f98c82004-07-25 21:44:54 +00001136 if (LI != LE) {
Chris Lattner730cfe42004-09-14 04:51:44 +00001137 Out << "deplibs = [ ";
1138 while (LI != LE) {
Chris Lattner2cdd49d2004-09-14 05:06:58 +00001139 Out << '"' << *LI << '"';
Reid Spencerffec7df2004-07-25 21:29:43 +00001140 ++LI;
Chris Lattner730cfe42004-09-14 04:51:44 +00001141 if (LI != LE)
1142 Out << ", ";
Reid Spencerffec7df2004-07-25 21:29:43 +00001143 }
1144 Out << " ]\n";
Reid Spencercc5ff642004-07-25 18:08:18 +00001145 }
Reid Spencerb9e08772004-09-13 23:44:23 +00001146
Chris Lattner2cdd49d2004-09-14 05:06:58 +00001147 // Loop over the symbol table, emitting all named constants.
Reid Spencer32af9e82007-01-06 07:24:44 +00001148 printTypeSymbolTable(M->getTypeSymbolTable());
Misha Brukmanb1c93172005-04-21 23:48:37 +00001149
Chris Lattner54932b02006-12-06 04:41:52 +00001150 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
1151 I != E; ++I)
Chris Lattner113f4f42002-06-25 16:13:24 +00001152 printGlobal(I);
Chris Lattnerd2747052007-04-26 02:24:10 +00001153
1154 // Output all aliases.
1155 if (!M->alias_empty()) Out << "\n";
1156 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
1157 I != E; ++I)
1158 printAlias(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +00001159
Chris Lattner2cdd49d2004-09-14 05:06:58 +00001160 // Output all of the functions.
Chris Lattner113f4f42002-06-25 16:13:24 +00001161 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
1162 printFunction(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +00001163}
1164
Chris Lattner0c19df42008-08-23 22:23:09 +00001165static void PrintLinkage(GlobalValue::LinkageTypes LT, raw_ostream &Out) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001166 switch (LT) {
1167 case GlobalValue::InternalLinkage: Out << "internal "; break;
1168 case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
1169 case GlobalValue::WeakLinkage: Out << "weak "; break;
1170 case GlobalValue::CommonLinkage: Out << "common "; break;
1171 case GlobalValue::AppendingLinkage: Out << "appending "; break;
1172 case GlobalValue::DLLImportLinkage: Out << "dllimport "; break;
1173 case GlobalValue::DLLExportLinkage: Out << "dllexport "; break;
1174 case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break;
1175 case GlobalValue::ExternalLinkage: break;
1176 case GlobalValue::GhostLinkage:
1177 Out << "GhostLinkage not allowed in AsmWriter!\n";
1178 abort();
1179 }
1180}
1181
1182
1183static void PrintVisibility(GlobalValue::VisibilityTypes Vis,
Chris Lattner0c19df42008-08-23 22:23:09 +00001184 raw_ostream &Out) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001185 switch (Vis) {
1186 default: assert(0 && "Invalid visibility style!");
1187 case GlobalValue::DefaultVisibility: break;
1188 case GlobalValue::HiddenVisibility: Out << "hidden "; break;
1189 case GlobalValue::ProtectedVisibility: Out << "protected "; break;
1190 }
1191}
1192
Chris Lattner7bfee412001-10-29 16:05:51 +00001193void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Chris Lattner033935d2008-08-17 04:40:13 +00001194 if (GV->hasName()) {
1195 PrintLLVMName(Out, GV);
1196 Out << " = ";
1197 }
Chris Lattner37798642001-09-18 04:01:05 +00001198
Chris Lattner1508d3f2008-08-19 05:16:28 +00001199 if (!GV->hasInitializer() && GV->hasExternalLinkage())
1200 Out << "external ";
1201
1202 PrintLinkage(GV->getLinkage(), Out);
1203 PrintVisibility(GV->getVisibility(), Out);
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +00001204
1205 if (GV->isThreadLocal()) Out << "thread_local ";
Misha Brukmana6619a92004-06-21 21:53:56 +00001206 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattner2413b162001-12-04 00:03:30 +00001207 printType(GV->getType()->getElementType());
Chris Lattner37798642001-09-18 04:01:05 +00001208
Dan Gohman81313fd2008-09-14 17:21:12 +00001209 if (GV->hasInitializer()) {
1210 Out << ' ';
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001211 writeOperand(GV->getInitializer(), false);
Dan Gohman81313fd2008-09-14 17:21:12 +00001212 }
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001213
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001214 if (unsigned AddressSpace = GV->getType()->getAddressSpace())
1215 Out << " addrspace(" << AddressSpace << ") ";
1216
Chris Lattner4b96c542005-11-12 00:10:19 +00001217 if (GV->hasSection())
1218 Out << ", section \"" << GV->getSection() << '"';
1219 if (GV->getAlignment())
Chris Lattnerf8a974d2005-11-06 06:48:53 +00001220 Out << ", align " << GV->getAlignment();
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001221
Chris Lattner113f4f42002-06-25 16:13:24 +00001222 printInfoComment(*GV);
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001223 Out << '\n';
Chris Lattnerda975502001-09-10 07:58:01 +00001224}
1225
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001226void AssemblyWriter::printAlias(const GlobalAlias *GA) {
Dale Johannesen83e468a2008-06-03 18:14:29 +00001227 // Don't crash when dumping partially built GA
1228 if (!GA->hasName())
1229 Out << "<<nameless>> = ";
Chris Lattner033935d2008-08-17 04:40:13 +00001230 else {
1231 PrintLLVMName(Out, GA);
1232 Out << " = ";
1233 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001234 PrintVisibility(GA->getVisibility(), Out);
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001235
1236 Out << "alias ";
1237
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001238 PrintLinkage(GA->getLinkage(), Out);
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001239
Anton Korobeynikov546ea7e2007-04-29 18:02:48 +00001240 const Constant *Aliasee = GA->getAliasee();
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001241
1242 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Aliasee)) {
1243 printType(GV->getType());
Chris Lattner033935d2008-08-17 04:40:13 +00001244 Out << ' ';
1245 PrintLLVMName(Out, GV);
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001246 } else if (const Function *F = dyn_cast<Function>(Aliasee)) {
1247 printType(F->getFunctionType());
1248 Out << "* ";
1249
Chris Lattner17f71652008-08-17 07:19:36 +00001250 if (F->hasName())
Chris Lattner033935d2008-08-17 04:40:13 +00001251 PrintLLVMName(Out, F);
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001252 else
1253 Out << "@\"\"";
Anton Korobeynikov72d5d422008-03-22 08:17:17 +00001254 } else if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(Aliasee)) {
1255 printType(GA->getType());
Chris Lattner033935d2008-08-17 04:40:13 +00001256 Out << " ";
1257 PrintLLVMName(Out, GA);
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +00001258 } else {
1259 const ConstantExpr *CE = 0;
1260 if ((CE = dyn_cast<ConstantExpr>(Aliasee)) &&
1261 (CE->getOpcode() == Instruction::BitCast)) {
1262 writeOperand(CE, false);
1263 } else
1264 assert(0 && "Unsupported aliasee");
1265 }
1266
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001267 printInfoComment(*GA);
Chris Lattner1508d3f2008-08-19 05:16:28 +00001268 Out << '\n';
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001269}
1270
Reid Spencer32af9e82007-01-06 07:24:44 +00001271void AssemblyWriter::printTypeSymbolTable(const TypeSymbolTable &ST) {
Reid Spencere7e96712004-05-25 08:53:40 +00001272 // Print the types.
Reid Spencer32af9e82007-01-06 07:24:44 +00001273 for (TypeSymbolTable::const_iterator TI = ST.begin(), TE = ST.end();
1274 TI != TE; ++TI) {
Chris Lattner1508d3f2008-08-19 05:16:28 +00001275 Out << '\t';
1276 PrintLLVMName(Out, &TI->first[0], TI->first.size(), LocalPrefix);
1277 Out << " = type ";
Reid Spencere7e96712004-05-25 08:53:40 +00001278
1279 // Make sure we print out at least one level of the type structure, so
1280 // that we do not get %FILE = type %FILE
1281 //
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001282 printTypeAtLeastOneLevel(TI->second);
1283 Out << '\n';
Reid Spencere7e96712004-05-25 08:53:40 +00001284 }
Reid Spencer32af9e82007-01-06 07:24:44 +00001285}
1286
Misha Brukmanc566ca362004-03-02 00:22:19 +00001287/// printFunction - Print all aspects of a function.
1288///
Chris Lattner113f4f42002-06-25 16:13:24 +00001289void AssemblyWriter::printFunction(const Function *F) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001290 // Print out the return type and name.
1291 Out << '\n';
Chris Lattner379a8d22003-04-16 20:28:45 +00001292
Misha Brukmana6619a92004-06-21 21:53:56 +00001293 if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001294
Reid Spencer5301e7c2007-01-30 20:08:39 +00001295 if (F->isDeclaration())
Chris Lattner10f03a62007-08-19 22:15:26 +00001296 Out << "declare ";
1297 else
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00001298 Out << "define ";
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001299
1300 PrintLinkage(F->getLinkage(), Out);
1301 PrintVisibility(F->getVisibility(), Out);
Chris Lattner379a8d22003-04-16 20:28:45 +00001302
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001303 // Print the calling convention.
1304 switch (F->getCallingConv()) {
1305 case CallingConv::C: break; // default
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +00001306 case CallingConv::Fast: Out << "fastcc "; break;
1307 case CallingConv::Cold: Out << "coldcc "; break;
1308 case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break;
1309 case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break;
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001310 default: Out << "cc" << F->getCallingConv() << " "; break;
1311 }
1312
Reid Spencer8c4914c2006-12-31 05:24:50 +00001313 const FunctionType *FT = F->getFunctionType();
Devang Patel4c758ea2008-09-25 21:00:45 +00001314 const AttrListPtr &Attrs = F->getAttributes();
Devang Patel221fe422008-09-29 20:49:50 +00001315 Attributes RetAttrs = Attrs.getRetAttributes();
1316 if (RetAttrs != Attribute::None)
1317 Out << Attribute::getAsString(Attrs.getRetAttributes()) << ' ';
Chris Lattner585297e82008-08-19 05:26:17 +00001318 printType(F->getReturnType());
1319 Out << ' ';
Chris Lattneredceac32008-08-17 07:24:08 +00001320 if (F->hasName())
Chris Lattner033935d2008-08-17 04:40:13 +00001321 PrintLLVMName(Out, F);
Chris Lattner5b337482003-10-18 05:57:43 +00001322 else
Reid Spencer788e3172007-01-26 08:02:52 +00001323 Out << "@\"\"";
Misha Brukmana6619a92004-06-21 21:53:56 +00001324 Out << '(';
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001325 Machine.incorporateFunction(F);
Chris Lattnerfee714f2001-09-07 16:36:04 +00001326
Chris Lattner7bfee412001-10-29 16:05:51 +00001327 // Loop over the arguments, printing them...
Chris Lattnerfee714f2001-09-07 16:36:04 +00001328
Reid Spencer8c4914c2006-12-31 05:24:50 +00001329 unsigned Idx = 1;
Chris Lattner82738fe2007-04-18 00:57:22 +00001330 if (!F->isDeclaration()) {
1331 // If this isn't a declaration, print the argument names as well.
1332 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
1333 I != E; ++I) {
1334 // Insert commas as we go... the first arg doesn't get a comma
1335 if (I != F->arg_begin()) Out << ", ";
Devang Patela05633e2008-09-26 22:53:05 +00001336 printArgument(I, Attrs.getParamAttributes(Idx));
Chris Lattner82738fe2007-04-18 00:57:22 +00001337 Idx++;
1338 }
1339 } else {
1340 // Otherwise, print the types from the function type.
1341 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
1342 // Insert commas as we go... the first arg doesn't get a comma
1343 if (i) Out << ", ";
1344
1345 // Output type...
1346 printType(FT->getParamType(i));
1347
Devang Patela05633e2008-09-26 22:53:05 +00001348 Attributes ArgAttrs = Attrs.getParamAttributes(i+1);
Devang Patel4c758ea2008-09-25 21:00:45 +00001349 if (ArgAttrs != Attribute::None)
1350 Out << ' ' << Attribute::getAsString(ArgAttrs);
Chris Lattner82738fe2007-04-18 00:57:22 +00001351 }
Reid Spencer8c4914c2006-12-31 05:24:50 +00001352 }
Chris Lattnerfee714f2001-09-07 16:36:04 +00001353
1354 // Finish printing arguments...
Chris Lattner113f4f42002-06-25 16:13:24 +00001355 if (FT->isVarArg()) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001356 if (FT->getNumParams()) Out << ", ";
1357 Out << "..."; // Output varargs portion of signature!
Chris Lattnerfee714f2001-09-07 16:36:04 +00001358 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001359 Out << ')';
Devang Patela05633e2008-09-26 22:53:05 +00001360 Attributes FnAttrs = Attrs.getFnAttributes();
1361 if (FnAttrs != Attribute::None)
1362 Out << ' ' << Attribute::getAsString(Attrs.getFnAttributes());
Chris Lattner4b96c542005-11-12 00:10:19 +00001363 if (F->hasSection())
1364 Out << " section \"" << F->getSection() << '"';
Chris Lattnerf8a974d2005-11-06 06:48:53 +00001365 if (F->getAlignment())
1366 Out << " align " << F->getAlignment();
Gordon Henriksend930f912008-08-17 18:44:35 +00001367 if (F->hasGC())
1368 Out << " gc \"" << F->getGC() << '"';
Reid Spencer5301e7c2007-01-30 20:08:39 +00001369 if (F->isDeclaration()) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001370 Out << "\n";
Chris Lattnerb2f02e52002-05-06 03:00:40 +00001371 } else {
Chris Lattnerd5fbc8f2008-04-21 06:12:55 +00001372 Out << " {";
Misha Brukmanb1c93172005-04-21 23:48:37 +00001373
Chris Lattner6915f8f2002-04-07 22:49:37 +00001374 // Output all of its basic blocks... for the function
Chris Lattner113f4f42002-06-25 16:13:24 +00001375 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
1376 printBasicBlock(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +00001377
Misha Brukmana6619a92004-06-21 21:53:56 +00001378 Out << "}\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +00001379 }
1380
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001381 Machine.purgeFunction();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001382}
1383
Misha Brukmanc566ca362004-03-02 00:22:19 +00001384/// printArgument - This member is called for every argument that is passed into
1385/// the function. Simply print it out
1386///
Dale Johannesen89268bc2008-02-19 21:38:47 +00001387void AssemblyWriter::printArgument(const Argument *Arg,
Devang Patelba3fa6c2008-09-23 23:03:40 +00001388 Attributes Attrs) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001389 // Output type...
Chris Lattner7bfee412001-10-29 16:05:51 +00001390 printType(Arg->getType());
Misha Brukmanb1c93172005-04-21 23:48:37 +00001391
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00001392 // Output parameter attributes list
Devang Patel4c758ea2008-09-25 21:00:45 +00001393 if (Attrs != Attribute::None)
1394 Out << ' ' << Attribute::getAsString(Attrs);
Reid Spencer8c4914c2006-12-31 05:24:50 +00001395
Chris Lattner2f7c9632001-06-06 20:29:01 +00001396 // Output name, if available...
Chris Lattner033935d2008-08-17 04:40:13 +00001397 if (Arg->hasName()) {
1398 Out << ' ';
1399 PrintLLVMName(Out, Arg);
1400 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001401}
1402
Misha Brukmanc566ca362004-03-02 00:22:19 +00001403/// printBasicBlock - This member is called for each basic block in a method.
1404///
Chris Lattner7bfee412001-10-29 16:05:51 +00001405void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Nick Lewycky4d43d3c2008-04-25 16:53:59 +00001406 if (BB->hasName()) { // Print out the label if it exists...
Chris Lattner033935d2008-08-17 04:40:13 +00001407 Out << "\n";
Chris Lattner1508d3f2008-08-19 05:16:28 +00001408 PrintLLVMName(Out, BB->getNameStart(), BB->getNameLen(), LabelPrefix);
Chris Lattner033935d2008-08-17 04:40:13 +00001409 Out << ':';
Nick Lewycky4d43d3c2008-04-25 16:53:59 +00001410 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Chris Lattner67bec132008-04-21 04:20:33 +00001411 Out << "\n; <label>:";
Chris Lattner5e043322007-01-11 03:54:27 +00001412 int Slot = Machine.getLocalSlot(BB);
Chris Lattner757ee0b2004-06-09 19:41:19 +00001413 if (Slot != -1)
Misha Brukmana6619a92004-06-21 21:53:56 +00001414 Out << Slot;
Chris Lattner757ee0b2004-06-09 19:41:19 +00001415 else
Misha Brukmana6619a92004-06-21 21:53:56 +00001416 Out << "<badref>";
Chris Lattner58185f22002-10-02 19:38:55 +00001417 }
Chris Lattner2447ef52003-11-20 00:09:43 +00001418
1419 if (BB->getParent() == 0)
Misha Brukmana6619a92004-06-21 21:53:56 +00001420 Out << "\t\t; Error: Block without parent!";
Chris Lattnerff834c02008-04-22 02:45:44 +00001421 else if (BB != &BB->getParent()->getEntryBlock()) { // Not the entry block?
1422 // Output predecessors for the block...
1423 Out << "\t\t;";
1424 pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
1425
1426 if (PI == PE) {
1427 Out << " No predecessors!";
1428 } else {
Dan Gohman81313fd2008-09-14 17:21:12 +00001429 Out << " preds = ";
Chris Lattnerff834c02008-04-22 02:45:44 +00001430 writeOperand(*PI, false);
1431 for (++PI; PI != PE; ++PI) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001432 Out << ", ";
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001433 writeOperand(*PI, false);
Chris Lattner00211f12003-11-16 22:59:57 +00001434 }
Chris Lattner58185f22002-10-02 19:38:55 +00001435 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001436 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001437
Chris Lattnerff834c02008-04-22 02:45:44 +00001438 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001439
Misha Brukmana6619a92004-06-21 21:53:56 +00001440 if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001441
Chris Lattnerfee714f2001-09-07 16:36:04 +00001442 // Output all of the instructions in the basic block...
Chris Lattner113f4f42002-06-25 16:13:24 +00001443 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
1444 printInstruction(*I);
Chris Lattner96cdd272004-03-08 18:51:45 +00001445
Misha Brukmana6619a92004-06-21 21:53:56 +00001446 if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001447}
1448
Chris Lattner862e3382001-10-13 06:42:36 +00001449
Misha Brukmanc566ca362004-03-02 00:22:19 +00001450/// printInfoComment - Print a little comment after the instruction indicating
1451/// which slot it occupies.
1452///
Chris Lattner113f4f42002-06-25 16:13:24 +00001453void AssemblyWriter::printInfoComment(const Value &V) {
1454 if (V.getType() != Type::VoidTy) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001455 Out << "\t\t; <";
Chris Lattner585297e82008-08-19 05:26:17 +00001456 printType(V.getType());
1457 Out << '>';
Chris Lattner862e3382001-10-13 06:42:36 +00001458
Chris Lattnerb25e5ea2008-08-29 17:19:30 +00001459 if (!V.hasName() && !isa<Instruction>(V)) {
Chris Lattner5e043322007-01-11 03:54:27 +00001460 int SlotNum;
1461 if (const GlobalValue *GV = dyn_cast<GlobalValue>(&V))
1462 SlotNum = Machine.getGlobalSlot(GV);
1463 else
1464 SlotNum = Machine.getLocalSlot(&V);
Chris Lattner757ee0b2004-06-09 19:41:19 +00001465 if (SlotNum == -1)
Misha Brukmana6619a92004-06-21 21:53:56 +00001466 Out << ":<badref>";
Reid Spencer8beac692004-06-09 15:26:53 +00001467 else
Misha Brukmana6619a92004-06-21 21:53:56 +00001468 Out << ':' << SlotNum; // Print out the def slot taken.
Chris Lattner862e3382001-10-13 06:42:36 +00001469 }
Chris Lattnerb6c21db2005-02-01 01:24:01 +00001470 Out << " [#uses=" << V.getNumUses() << ']'; // Output # uses
Chris Lattner862e3382001-10-13 06:42:36 +00001471 }
1472}
1473
Reid Spencere7141c82006-08-28 01:02:49 +00001474// This member is called for each Instruction in a function..
Chris Lattner113f4f42002-06-25 16:13:24 +00001475void AssemblyWriter::printInstruction(const Instruction &I) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001476 if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001477
Chris Lattner82ff9232008-08-23 22:52:27 +00001478 Out << '\t';
Chris Lattner2f7c9632001-06-06 20:29:01 +00001479
1480 // Print out name if it exists...
Chris Lattner033935d2008-08-17 04:40:13 +00001481 if (I.hasName()) {
1482 PrintLLVMName(Out, &I);
1483 Out << " = ";
Chris Lattnerb25e5ea2008-08-29 17:19:30 +00001484 } else if (I.getType() != Type::VoidTy) {
1485 // Print out the def slot taken.
1486 int SlotNum = Machine.getLocalSlot(&I);
1487 if (SlotNum == -1)
1488 Out << "<badref> = ";
1489 else
1490 Out << '%' << SlotNum << " = ";
Chris Lattner033935d2008-08-17 04:40:13 +00001491 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001492
Chris Lattner06038452005-05-06 05:51:46 +00001493 // If this is a volatile load or store, print out the volatile marker.
Chris Lattner504f9242003-09-08 17:45:59 +00001494 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
Chris Lattner06038452005-05-06 05:51:46 +00001495 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile())) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001496 Out << "volatile ";
Chris Lattner06038452005-05-06 05:51:46 +00001497 } else if (isa<CallInst>(I) && cast<CallInst>(I).isTailCall()) {
1498 // If this is a call, check if it's a tail call.
1499 Out << "tail ";
1500 }
Chris Lattner504f9242003-09-08 17:45:59 +00001501
Chris Lattner2f7c9632001-06-06 20:29:01 +00001502 // Print out the opcode...
Misha Brukmana6619a92004-06-21 21:53:56 +00001503 Out << I.getOpcodeName();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001504
Reid Spencer45e52392006-12-03 06:27:29 +00001505 // Print out the compare instruction predicates
Nate Begemand2195702008-05-12 19:01:56 +00001506 if (const CmpInst *CI = dyn_cast<CmpInst>(&I))
Chris Lattner82ff9232008-08-23 22:52:27 +00001507 Out << ' ' << getPredicateText(CI->getPredicate());
Reid Spencer45e52392006-12-03 06:27:29 +00001508
Chris Lattner2f7c9632001-06-06 20:29:01 +00001509 // Print out the type of the operands...
Chris Lattner113f4f42002-06-25 16:13:24 +00001510 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001511
1512 // Special case conditional branches to swizzle the condition out to the front
Chris Lattner113f4f42002-06-25 16:13:24 +00001513 if (isa<BranchInst>(I) && I.getNumOperands() > 1) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001514 Out << ' ';
Chris Lattner113f4f42002-06-25 16:13:24 +00001515 writeOperand(I.getOperand(2), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001516 Out << ", ";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001517 writeOperand(Operand, true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001518 Out << ", ";
Chris Lattner113f4f42002-06-25 16:13:24 +00001519 writeOperand(I.getOperand(1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001520
Chris Lattner8d48df22002-04-13 18:34:38 +00001521 } else if (isa<SwitchInst>(I)) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001522 // Special case switch statement to get formatting nice and correct...
Dan Gohman81313fd2008-09-14 17:21:12 +00001523 Out << ' ';
Chris Lattner82ff9232008-08-23 22:52:27 +00001524 writeOperand(Operand , true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001525 Out << ", ";
Chris Lattner82ff9232008-08-23 22:52:27 +00001526 writeOperand(I.getOperand(1), true);
1527 Out << " [";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001528
Chris Lattner113f4f42002-06-25 16:13:24 +00001529 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001530 Out << "\n\t\t";
Chris Lattner82ff9232008-08-23 22:52:27 +00001531 writeOperand(I.getOperand(op ), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001532 Out << ", ";
Chris Lattner113f4f42002-06-25 16:13:24 +00001533 writeOperand(I.getOperand(op+1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001534 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001535 Out << "\n\t]";
Chris Lattnerda558102001-10-02 03:41:24 +00001536 } else if (isa<PHINode>(I)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001537 Out << ' ';
Chris Lattner113f4f42002-06-25 16:13:24 +00001538 printType(I.getType());
Misha Brukmana6619a92004-06-21 21:53:56 +00001539 Out << ' ';
Chris Lattner2f7c9632001-06-06 20:29:01 +00001540
Chris Lattner113f4f42002-06-25 16:13:24 +00001541 for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001542 if (op) Out << ", ";
Dan Gohman81313fd2008-09-14 17:21:12 +00001543 Out << "[ ";
1544 writeOperand(I.getOperand(op ), false); Out << ", ";
Misha Brukmana6619a92004-06-21 21:53:56 +00001545 writeOperand(I.getOperand(op+1), false); Out << " ]";
Chris Lattner931ef3b2001-06-11 15:04:20 +00001546 }
Dan Gohmana76f0f72008-05-31 19:12:39 +00001547 } else if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(&I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001548 Out << ' ';
Dan Gohmana76f0f72008-05-31 19:12:39 +00001549 writeOperand(I.getOperand(0), true);
1550 for (const unsigned *i = EVI->idx_begin(), *e = EVI->idx_end(); i != e; ++i)
1551 Out << ", " << *i;
1552 } else if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(&I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001553 Out << ' ';
1554 writeOperand(I.getOperand(0), true); Out << ", ";
Dan Gohmana76f0f72008-05-31 19:12:39 +00001555 writeOperand(I.getOperand(1), true);
1556 for (const unsigned *i = IVI->idx_begin(), *e = IVI->idx_end(); i != e; ++i)
1557 Out << ", " << *i;
Devang Patel59643e52008-02-23 00:35:18 +00001558 } else if (isa<ReturnInst>(I) && !Operand) {
1559 Out << " void";
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001560 } else if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
1561 // Print the calling convention being used.
1562 switch (CI->getCallingConv()) {
1563 case CallingConv::C: break; // default
Chris Lattner29d20852006-05-19 21:58:52 +00001564 case CallingConv::Fast: Out << " fastcc"; break;
1565 case CallingConv::Cold: Out << " coldcc"; break;
Chris Lattnerf5270372007-11-18 18:32:16 +00001566 case CallingConv::X86_StdCall: Out << " x86_stdcallcc"; break;
1567 case CallingConv::X86_FastCall: Out << " x86_fastcallcc"; break;
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001568 default: Out << " cc" << CI->getCallingConv(); break;
1569 }
1570
Reid Spencer1517de32007-04-09 06:10:42 +00001571 const PointerType *PTy = cast<PointerType>(Operand->getType());
1572 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1573 const Type *RetTy = FTy->getReturnType();
Devang Patel4c758ea2008-09-25 21:00:45 +00001574 const AttrListPtr &PAL = CI->getAttributes();
Chris Lattner2f2d9472001-11-06 21:28:12 +00001575
Devang Patel221fe422008-09-29 20:49:50 +00001576 if (PAL.getRetAttributes() != Attribute::None)
1577 Out << ' ' << Attribute::getAsString(PAL.getRetAttributes());
1578
Chris Lattner463d6a52003-08-05 15:34:45 +00001579 // If possible, print out the short form of the call instruction. We can
Chris Lattner6915f8f2002-04-07 22:49:37 +00001580 // only do this if the first argument is a pointer to a nonvararg function,
Chris Lattner463d6a52003-08-05 15:34:45 +00001581 // and if the return type is not a pointer to a function.
Chris Lattner2f2d9472001-11-06 21:28:12 +00001582 //
Dan Gohman81313fd2008-09-14 17:21:12 +00001583 Out << ' ';
Chris Lattner463d6a52003-08-05 15:34:45 +00001584 if (!FTy->isVarArg() &&
Misha Brukmanb1c93172005-04-21 23:48:37 +00001585 (!isa<PointerType>(RetTy) ||
Chris Lattnerd9a36a62002-07-25 20:58:51 +00001586 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001587 printType(RetTy);
1588 Out << ' ';
Chris Lattner2f2d9472001-11-06 21:28:12 +00001589 writeOperand(Operand, false);
1590 } else {
1591 writeOperand(Operand, true);
1592 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001593 Out << '(';
Reid Spencer8c4914c2006-12-31 05:24:50 +00001594 for (unsigned op = 1, Eop = I.getNumOperands(); op < Eop; ++op) {
1595 if (op > 1)
Dan Gohman81313fd2008-09-14 17:21:12 +00001596 Out << ", ";
Devang Patela05633e2008-09-26 22:53:05 +00001597 writeParamOperand(I.getOperand(op), PAL.getParamAttributes(op));
Chris Lattner2f7c9632001-06-06 20:29:01 +00001598 }
Dan Gohman81313fd2008-09-14 17:21:12 +00001599 Out << ')';
Devang Patela05633e2008-09-26 22:53:05 +00001600 if (PAL.getFnAttributes() != Attribute::None)
1601 Out << ' ' << Attribute::getAsString(PAL.getFnAttributes());
Chris Lattner113f4f42002-06-25 16:13:24 +00001602 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Reid Spencer1517de32007-04-09 06:10:42 +00001603 const PointerType *PTy = cast<PointerType>(Operand->getType());
1604 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1605 const Type *RetTy = FTy->getReturnType();
Devang Patel4c758ea2008-09-25 21:00:45 +00001606 const AttrListPtr &PAL = II->getAttributes();
Chris Lattner463d6a52003-08-05 15:34:45 +00001607
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001608 // Print the calling convention being used.
1609 switch (II->getCallingConv()) {
1610 case CallingConv::C: break; // default
Chris Lattner29d20852006-05-19 21:58:52 +00001611 case CallingConv::Fast: Out << " fastcc"; break;
1612 case CallingConv::Cold: Out << " coldcc"; break;
Dan Gohman81313fd2008-09-14 17:21:12 +00001613 case CallingConv::X86_StdCall: Out << " x86_stdcallcc"; break;
1614 case CallingConv::X86_FastCall: Out << " x86_fastcallcc"; break;
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001615 default: Out << " cc" << II->getCallingConv(); break;
1616 }
1617
Devang Patel221fe422008-09-29 20:49:50 +00001618 if (PAL.getRetAttributes() != Attribute::None)
1619 Out << ' ' << Attribute::getAsString(PAL.getRetAttributes());
1620
Chris Lattner463d6a52003-08-05 15:34:45 +00001621 // If possible, print out the short form of the invoke instruction. We can
1622 // only do this if the first argument is a pointer to a nonvararg function,
1623 // and if the return type is not a pointer to a function.
1624 //
1625 if (!FTy->isVarArg() &&
Misha Brukmanb1c93172005-04-21 23:48:37 +00001626 (!isa<PointerType>(RetTy) ||
Chris Lattner463d6a52003-08-05 15:34:45 +00001627 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001628 Out << ' '; printType(RetTy);
Chris Lattner463d6a52003-08-05 15:34:45 +00001629 writeOperand(Operand, false);
1630 } else {
Dan Gohman81313fd2008-09-14 17:21:12 +00001631 Out << ' ';
Chris Lattner463d6a52003-08-05 15:34:45 +00001632 writeOperand(Operand, true);
1633 }
1634
Misha Brukmana6619a92004-06-21 21:53:56 +00001635 Out << '(';
Reid Spencer8c4914c2006-12-31 05:24:50 +00001636 for (unsigned op = 3, Eop = I.getNumOperands(); op < Eop; ++op) {
1637 if (op > 3)
Dan Gohman81313fd2008-09-14 17:21:12 +00001638 Out << ", ";
Devang Patela05633e2008-09-26 22:53:05 +00001639 writeParamOperand(I.getOperand(op), PAL.getParamAttributes(op-2));
Chris Lattner862e3382001-10-13 06:42:36 +00001640 }
1641
Dan Gohman81313fd2008-09-14 17:21:12 +00001642 Out << ')';
Devang Patela05633e2008-09-26 22:53:05 +00001643 if (PAL.getFnAttributes() != Attribute::None)
1644 Out << ' ' << Attribute::getAsString(PAL.getFnAttributes());
1645
Dan Gohman81313fd2008-09-14 17:21:12 +00001646 Out << "\n\t\t\tto ";
Chris Lattner862e3382001-10-13 06:42:36 +00001647 writeOperand(II->getNormalDest(), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001648 Out << " unwind ";
Chris Lattnerfae8ab32004-02-08 21:44:31 +00001649 writeOperand(II->getUnwindDest(), true);
Chris Lattner862e3382001-10-13 06:42:36 +00001650
Chris Lattner113f4f42002-06-25 16:13:24 +00001651 } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(&I)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001652 Out << ' ';
Chris Lattner8d48df22002-04-13 18:34:38 +00001653 printType(AI->getType()->getElementType());
1654 if (AI->isArrayAllocation()) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001655 Out << ", ";
Chris Lattner8d48df22002-04-13 18:34:38 +00001656 writeOperand(AI->getArraySize(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001657 }
Nate Begeman848622f2005-11-05 09:21:28 +00001658 if (AI->getAlignment()) {
Chris Lattner7aeee3a2005-11-05 21:20:34 +00001659 Out << ", align " << AI->getAlignment();
Nate Begeman848622f2005-11-05 09:21:28 +00001660 }
Chris Lattner862e3382001-10-13 06:42:36 +00001661 } else if (isa<CastInst>(I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001662 if (Operand) {
1663 Out << ' ';
1664 writeOperand(Operand, true); // Work with broken code
1665 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001666 Out << " to ";
Chris Lattner113f4f42002-06-25 16:13:24 +00001667 printType(I.getType());
Chris Lattner5b337482003-10-18 05:57:43 +00001668 } else if (isa<VAArgInst>(I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001669 if (Operand) {
1670 Out << ' ';
1671 writeOperand(Operand, true); // Work with broken code
1672 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001673 Out << ", ";
Chris Lattnerf70da102003-05-08 02:44:12 +00001674 printType(I.getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +00001675 } else if (Operand) { // Print the normal way...
1676
Misha Brukmanb1c93172005-04-21 23:48:37 +00001677 // PrintAllTypes - Instructions who have operands of all the same type
Chris Lattner2f7c9632001-06-06 20:29:01 +00001678 // omit the type from all but the first operand. If the instruction has
1679 // different type operands (for example br), then they are all printed.
1680 bool PrintAllTypes = false;
1681 const Type *TheType = Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001682
Reid Spencer0cdd04f2007-02-02 13:54:55 +00001683 // Select, Store and ShuffleVector always print all types.
Devang Patelce556d92008-03-04 22:05:14 +00001684 if (isa<SelectInst>(I) || isa<StoreInst>(I) || isa<ShuffleVectorInst>(I)
1685 || isa<ReturnInst>(I)) {
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00001686 PrintAllTypes = true;
1687 } else {
1688 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
1689 Operand = I.getOperand(i);
1690 if (Operand->getType() != TheType) {
1691 PrintAllTypes = true; // We have differing types! Print them all!
1692 break;
1693 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001694 }
1695 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001696
Chris Lattner7bfee412001-10-29 16:05:51 +00001697 if (!PrintAllTypes) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001698 Out << ' ';
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00001699 printType(TheType);
Chris Lattner7bfee412001-10-29 16:05:51 +00001700 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001701
Dan Gohman81313fd2008-09-14 17:21:12 +00001702 Out << ' ';
Chris Lattner113f4f42002-06-25 16:13:24 +00001703 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001704 if (i) Out << ", ";
Chris Lattner113f4f42002-06-25 16:13:24 +00001705 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001706 }
1707 }
Christopher Lamb84485702007-04-22 19:24:39 +00001708
1709 // Print post operand alignment for load/store
1710 if (isa<LoadInst>(I) && cast<LoadInst>(I).getAlignment()) {
1711 Out << ", align " << cast<LoadInst>(I).getAlignment();
1712 } else if (isa<StoreInst>(I) && cast<StoreInst>(I).getAlignment()) {
1713 Out << ", align " << cast<StoreInst>(I).getAlignment();
1714 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001715
Chris Lattner862e3382001-10-13 06:42:36 +00001716 printInfoComment(I);
Chris Lattner82ff9232008-08-23 22:52:27 +00001717 Out << '\n';
Chris Lattner2f7c9632001-06-06 20:29:01 +00001718}
1719
1720
1721//===----------------------------------------------------------------------===//
1722// External Interface declarations
1723//===----------------------------------------------------------------------===//
1724
Chris Lattner8339f7d2003-10-30 23:41:03 +00001725void Module::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Chris Lattner0c19df42008-08-23 22:23:09 +00001726 raw_os_ostream OS(o);
1727 print(OS, AAW);
1728}
1729void Module::print(raw_ostream &OS, AssemblyAnnotationWriter *AAW) const {
Chris Lattnere36fd8a2008-08-19 04:26:57 +00001730 SlotTracker SlotTable(this);
Chris Lattner0c19df42008-08-23 22:23:09 +00001731 AssemblyWriter W(OS, SlotTable, this, AAW);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001732 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001733}
1734
Misha Brukmanb1c93172005-04-21 23:48:37 +00001735void Type::print(std::ostream &o) const {
Chris Lattner0c19df42008-08-23 22:23:09 +00001736 raw_os_ostream OS(o);
1737 print(OS);
1738}
1739
1740void Type::print(raw_ostream &o) const {
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001741 if (this == 0)
1742 o << "<null Type>";
1743 else
1744 o << getDescription();
1745}
1746
Chris Lattner0c19df42008-08-23 22:23:09 +00001747void Value::print(raw_ostream &OS, AssemblyAnnotationWriter *AAW) const {
1748 if (this == 0) {
1749 OS << "printing a <null> value\n";
1750 return;
1751 }
1752
1753 if (const Instruction *I = dyn_cast<Instruction>(this)) {
1754 const Function *F = I->getParent() ? I->getParent()->getParent() : 0;
1755 SlotTracker SlotTable(F);
1756 AssemblyWriter W(OS, SlotTable, F ? F->getParent() : 0, AAW);
1757 W.write(I);
1758 } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(this)) {
1759 SlotTracker SlotTable(BB->getParent());
1760 AssemblyWriter W(OS, SlotTable,
1761 BB->getParent() ? BB->getParent()->getParent() : 0, AAW);
1762 W.write(BB);
1763 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(this)) {
1764 SlotTracker SlotTable(GV->getParent());
1765 AssemblyWriter W(OS, SlotTable, GV->getParent(), 0);
1766 W.write(GV);
1767 } else if (const Constant *C = dyn_cast<Constant>(this)) {
Dan Gohmanfbd67be2008-10-01 15:09:37 +00001768 OS << C->getType()->getDescription() << ' ';
Chris Lattner0c19df42008-08-23 22:23:09 +00001769 std::map<const Type *, std::string> TypeTable;
1770 WriteConstantInt(OS, C, TypeTable, 0);
1771 } else if (const Argument *A = dyn_cast<Argument>(this)) {
1772 WriteAsOperand(OS, this, true,
1773 A->getParent() ? A->getParent()->getParent() : 0);
1774 } else if (isa<InlineAsm>(this)) {
1775 WriteAsOperand(OS, this, true, 0);
1776 } else {
Chris Lattnerd7586252008-08-24 18:33:17 +00001777 // FIXME: PseudoSourceValue breaks this!
1778 //assert(0 && "Unknown value to print out!");
Chris Lattner0c19df42008-08-23 22:23:09 +00001779 }
1780}
1781
1782void Value::print(std::ostream &O, AssemblyAnnotationWriter *AAW) const {
1783 raw_os_ostream OS(O);
1784 print(OS, AAW);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001785}
1786
Chris Lattnerdab942552008-08-25 17:03:15 +00001787// Value::dump - allow easy printing of Values from the debugger.
Chris Lattner820eebc2008-08-25 04:55:46 +00001788void Value::dump() const { print(errs()); errs() << '\n'; errs().flush(); }
Reid Spencer52641832004-05-25 18:14:38 +00001789
Chris Lattnerdab942552008-08-25 17:03:15 +00001790// Type::dump - allow easy printing of Types from the debugger.
Chris Lattner820eebc2008-08-25 04:55:46 +00001791void Type::dump() const { print(errs()); errs() << '\n'; errs().flush(); }
Chris Lattner0c19df42008-08-23 22:23:09 +00001792
Chris Lattner41a83d92008-10-01 20:16:19 +00001793// Type::dump - allow easy printing of Types from the debugger.
1794// This one uses type names from the given context module
1795void Type::dump(const Module *Context) const {
1796 WriteTypeSymbolic(errs(), this, Context);
1797 errs() << '\n';
1798 errs().flush();
1799}
1800
Chris Lattnerdab942552008-08-25 17:03:15 +00001801// Module::dump() - Allow printing of Modules from the debugger.
Chris Lattner820eebc2008-08-25 04:55:46 +00001802void Module::dump() const { print(errs(), 0); errs().flush(); }
Chris Lattner0c19df42008-08-23 22:23:09 +00001803
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001804