blob: b1ce257105212880dae8f5b4e0636cde6f1d2e08 [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
71
72/// NameNeedsQuotes - Return true if the specified llvm name should be wrapped
73/// with ""'s.
74static std::string QuoteNameIfNeeded(const std::string &Name) {
75 std::string result;
76 bool needsQuotes = Name[0] >= '0' && Name[0] <= '9';
77 // Scan the name to see if it needs quotes and to replace funky chars with
78 // their octal equivalent.
79 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
80 char C = Name[i];
81 assert(C != '"' && "Illegal character in LLVM value name!");
82 if (isalnum(C) || C == '-' || C == '.' || C == '_')
83 result += C;
84 else if (C == '\\') {
85 needsQuotes = true;
86 result += "\\\\";
87 } else if (isprint(C)) {
88 needsQuotes = true;
89 result += C;
90 } else {
91 needsQuotes = true;
92 result += "\\";
93 char hex1 = (C >> 4) & 0x0F;
94 if (hex1 < 10)
95 result += hex1 + '0';
96 else
97 result += hex1 - 10 + 'A';
98 char hex2 = C & 0x0F;
99 if (hex2 < 10)
100 result += hex2 + '0';
101 else
102 result += hex2 - 10 + 'A';
103 }
104 }
105 if (needsQuotes) {
106 result.insert(0,"\"");
107 result += '"';
108 }
109 return result;
110}
111
Chris Lattner585297e82008-08-19 05:26:17 +0000112/// getLLVMName - Turn the specified string into an 'LLVM name', which is
113/// surrounded with ""'s and escaped if it has special chars in it.
Chris Lattner3eee99c2008-08-19 04:36:02 +0000114static std::string getLLVMName(const std::string &Name) {
115 assert(!Name.empty() && "Cannot get empty name!");
Chris Lattner585297e82008-08-19 05:26:17 +0000116 return QuoteNameIfNeeded(Name);
Chris Lattner3eee99c2008-08-19 04:36:02 +0000117}
118
119enum PrefixType {
120 GlobalPrefix,
121 LabelPrefix,
122 LocalPrefix
123};
124
125/// PrintLLVMName - Turn the specified name into an 'LLVM name', which is either
126/// prefixed with % (if the string only contains simple characters) or is
127/// surrounded with ""'s (if it has special chars in it). Print it out.
Chris Lattner0c19df42008-08-23 22:23:09 +0000128static void PrintLLVMName(raw_ostream &OS, const char *NameStr,
Chris Lattner1508d3f2008-08-19 05:16:28 +0000129 unsigned NameLen, PrefixType Prefix) {
130 assert(NameStr && "Cannot get empty name!");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000131 switch (Prefix) {
Chris Lattner1508d3f2008-08-19 05:16:28 +0000132 default: assert(0 && "Bad prefix!");
133 case GlobalPrefix: OS << '@'; break;
134 case LabelPrefix: break;
135 case LocalPrefix: OS << '%'; break;
Chris Lattner3eee99c2008-08-19 04:36:02 +0000136 }
137
138 // Scan the name to see if it needs quotes first.
Chris Lattner3eee99c2008-08-19 04:36:02 +0000139 bool NeedsQuotes = NameStr[0] >= '0' && NameStr[0] <= '9';
140 if (!NeedsQuotes) {
141 for (unsigned i = 0; i != NameLen; ++i) {
142 char C = NameStr[i];
143 if (!isalnum(C) && C != '-' && C != '.' && C != '_') {
144 NeedsQuotes = true;
145 break;
146 }
147 }
148 }
149
150 // If we didn't need any quotes, just write out the name in one blast.
151 if (!NeedsQuotes) {
152 OS.write(NameStr, NameLen);
153 return;
154 }
155
156 // Okay, we need quotes. Output the quotes and escape any scary characters as
157 // needed.
158 OS << '"';
159 for (unsigned i = 0; i != NameLen; ++i) {
160 char C = NameStr[i];
161 assert(C != '"' && "Illegal character in LLVM value name!");
162 if (C == '\\') {
163 OS << "\\\\";
164 } else if (isprint(C)) {
165 OS << C;
166 } else {
167 OS << '\\';
168 char hex1 = (C >> 4) & 0x0F;
169 if (hex1 < 10)
170 OS << (char)(hex1 + '0');
171 else
172 OS << (char)(hex1 - 10 + 'A');
173 char hex2 = C & 0x0F;
174 if (hex2 < 10)
175 OS << (char)(hex2 + '0');
176 else
177 OS << (char)(hex2 - 10 + 'A');
178 }
179 }
180 OS << '"';
181}
182
183/// PrintLLVMName - Turn the specified name into an 'LLVM name', which is either
184/// prefixed with % (if the string only contains simple characters) or is
185/// surrounded with ""'s (if it has special chars in it). Print it out.
Chris Lattner0c19df42008-08-23 22:23:09 +0000186static void PrintLLVMName(raw_ostream &OS, const Value *V) {
Chris Lattner1508d3f2008-08-19 05:16:28 +0000187 PrintLLVMName(OS, V->getNameStart(), V->getNameLen(),
Chris Lattner3eee99c2008-08-19 04:36:02 +0000188 isa<GlobalValue>(V) ? GlobalPrefix : LocalPrefix);
189}
190
191
192
193//===----------------------------------------------------------------------===//
194// SlotTracker Class: Enumerate slot numbers for unnamed values
195//===----------------------------------------------------------------------===//
196
Chris Lattner3ee58762008-08-19 04:28:07 +0000197namespace {
198
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000199/// This class provides computation of slot numbers for LLVM Assembly writing.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000200///
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000201class SlotTracker {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000202public:
Chris Lattner393b7cd2008-08-17 04:17:45 +0000203 /// ValueMap - A mapping of Values to slot numbers
Chris Lattnera204d412008-08-17 17:25:25 +0000204 typedef DenseMap<const Value*, unsigned> ValueMap;
Chris Lattner393b7cd2008-08-17 04:17:45 +0000205
206private:
207 /// TheModule - The module for which we are holding slot numbers
208 const Module* TheModule;
209
210 /// TheFunction - The function for which we are holding slot numbers
211 const Function* TheFunction;
212 bool FunctionProcessed;
213
214 /// mMap - The TypePlanes map for the module level data
215 ValueMap mMap;
216 unsigned mNext;
217
218 /// fMap - The TypePlanes map for the function level data
219 ValueMap fMap;
220 unsigned fNext;
221
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000222public:
Chris Lattner393b7cd2008-08-17 04:17:45 +0000223 /// Construct from a module
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000224 explicit SlotTracker(const Module *M);
Chris Lattner393b7cd2008-08-17 04:17:45 +0000225 /// Construct from a function, starting out in incorp state.
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000226 explicit SlotTracker(const Function *F);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000227
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000228 /// Return the slot number of the specified value in it's type
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000229 /// plane. If something is not in the SlotTracker, return -1.
Chris Lattner5e043322007-01-11 03:54:27 +0000230 int getLocalSlot(const Value *V);
231 int getGlobalSlot(const GlobalValue *V);
Reid Spencer8beac692004-06-09 15:26:53 +0000232
Misha Brukmanb1c93172005-04-21 23:48:37 +0000233 /// If you'd like to deal with a function instead of just a module, use
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000234 /// this method to get its data into the SlotTracker.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000235 void incorporateFunction(const Function *F) {
236 TheFunction = F;
Reid Spencerb0ac8c42004-08-16 07:46:33 +0000237 FunctionProcessed = false;
238 }
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000239
Misha Brukmanb1c93172005-04-21 23:48:37 +0000240 /// After calling incorporateFunction, use this method to remove the
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000241 /// most recently incorporated function from the SlotTracker. This
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000242 /// will reset the state of the machine back to just the module contents.
243 void purgeFunction();
244
Chris Lattner393b7cd2008-08-17 04:17:45 +0000245 // Implementation Details
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000246private:
Reid Spencer56010e42004-05-26 21:56:09 +0000247 /// This function does the actual initialization.
248 inline void initialize();
249
Chris Lattnerea862a32007-01-09 07:55:49 +0000250 /// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
251 void CreateModuleSlot(const GlobalValue *V);
252
253 /// CreateFunctionSlot - Insert the specified Value* into the slot table.
254 void CreateFunctionSlot(const Value *V);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000255
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000256 /// Add all of the module level global variables (and their initializers)
257 /// and function declarations, but not the contents of those functions.
258 void processModule();
259
Reid Spencer56010e42004-05-26 21:56:09 +0000260 /// Add all of the functions arguments, basic blocks, and instructions
261 void processFunction();
262
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000263 SlotTracker(const SlotTracker &); // DO NOT IMPLEMENT
264 void operator=(const SlotTracker &); // DO NOT IMPLEMENT
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000265};
266
Chris Lattner3ee58762008-08-19 04:28:07 +0000267} // end anonymous namespace
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000268
Chris Lattner3eee99c2008-08-19 04:36:02 +0000269
270static SlotTracker *createSlotTracker(const Value *V) {
271 if (const Argument *FA = dyn_cast<Argument>(V))
272 return new SlotTracker(FA->getParent());
273
274 if (const Instruction *I = dyn_cast<Instruction>(V))
275 return new SlotTracker(I->getParent()->getParent());
276
277 if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
278 return new SlotTracker(BB->getParent());
279
280 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
281 return new SlotTracker(GV->getParent());
282
283 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V))
284 return new SlotTracker(GA->getParent());
285
286 if (const Function *Func = dyn_cast<Function>(V))
287 return new SlotTracker(Func);
288
289 return 0;
290}
291
292#if 0
Chris Lattner604e3512008-08-19 04:47:09 +0000293#define ST_DEBUG(X) cerr << X
Chris Lattner3eee99c2008-08-19 04:36:02 +0000294#else
Chris Lattner604e3512008-08-19 04:47:09 +0000295#define ST_DEBUG(X)
Chris Lattner3eee99c2008-08-19 04:36:02 +0000296#endif
297
298// Module level constructor. Causes the contents of the Module (sans functions)
299// to be added to the slot table.
300SlotTracker::SlotTracker(const Module *M)
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000301 : TheModule(M), TheFunction(0), FunctionProcessed(false), mNext(0), fNext(0) {
Chris Lattner3eee99c2008-08-19 04:36:02 +0000302}
303
304// Function level constructor. Causes the contents of the Module and the one
305// function provided to be added to the slot table.
306SlotTracker::SlotTracker(const Function *F)
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000307 : TheModule(F ? F->getParent() : 0), TheFunction(F), FunctionProcessed(false),
308 mNext(0), fNext(0) {
Chris Lattner3eee99c2008-08-19 04:36:02 +0000309}
310
311inline void SlotTracker::initialize() {
312 if (TheModule) {
313 processModule();
314 TheModule = 0; ///< Prevent re-processing next time we're called.
315 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000316
Chris Lattner3eee99c2008-08-19 04:36:02 +0000317 if (TheFunction && !FunctionProcessed)
318 processFunction();
319}
320
321// Iterate through all the global variables, functions, and global
322// variable initializers and create slots for them.
323void SlotTracker::processModule() {
Chris Lattner604e3512008-08-19 04:47:09 +0000324 ST_DEBUG("begin processModule!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000325
326 // Add all of the unnamed global variables to the value table.
327 for (Module::const_global_iterator I = TheModule->global_begin(),
328 E = TheModule->global_end(); I != E; ++I)
329 if (!I->hasName())
330 CreateModuleSlot(I);
331
332 // Add all the unnamed functions to the table.
333 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
334 I != E; ++I)
335 if (!I->hasName())
336 CreateModuleSlot(I);
337
Chris Lattner604e3512008-08-19 04:47:09 +0000338 ST_DEBUG("end processModule!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000339}
340
341
342// Process the arguments, basic blocks, and instructions of a function.
343void SlotTracker::processFunction() {
Chris Lattner604e3512008-08-19 04:47:09 +0000344 ST_DEBUG("begin processFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000345 fNext = 0;
346
347 // Add all the function arguments with no names.
348 for(Function::const_arg_iterator AI = TheFunction->arg_begin(),
349 AE = TheFunction->arg_end(); AI != AE; ++AI)
350 if (!AI->hasName())
351 CreateFunctionSlot(AI);
352
Chris Lattner604e3512008-08-19 04:47:09 +0000353 ST_DEBUG("Inserting Instructions:\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000354
355 // Add all of the basic blocks and instructions with no names.
356 for (Function::const_iterator BB = TheFunction->begin(),
357 E = TheFunction->end(); BB != E; ++BB) {
358 if (!BB->hasName())
359 CreateFunctionSlot(BB);
360 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
361 if (I->getType() != Type::VoidTy && !I->hasName())
362 CreateFunctionSlot(I);
363 }
364
365 FunctionProcessed = true;
366
Chris Lattner604e3512008-08-19 04:47:09 +0000367 ST_DEBUG("end processFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000368}
369
370/// Clean up after incorporating a function. This is the only way to get out of
371/// the function incorporation state that affects get*Slot/Create*Slot. Function
372/// incorporation state is indicated by TheFunction != 0.
373void SlotTracker::purgeFunction() {
Chris Lattner604e3512008-08-19 04:47:09 +0000374 ST_DEBUG("begin purgeFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000375 fMap.clear(); // Simply discard the function level map
376 TheFunction = 0;
377 FunctionProcessed = false;
Chris Lattner604e3512008-08-19 04:47:09 +0000378 ST_DEBUG("end purgeFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000379}
380
381/// getGlobalSlot - Get the slot number of a global value.
382int SlotTracker::getGlobalSlot(const GlobalValue *V) {
383 // Check for uninitialized state and do lazy initialization.
384 initialize();
385
386 // Find the type plane in the module map
387 ValueMap::iterator MI = mMap.find(V);
388 return MI == mMap.end() ? -1 : MI->second;
389}
390
391
392/// getLocalSlot - Get the slot number for a value that is local to a function.
393int SlotTracker::getLocalSlot(const Value *V) {
394 assert(!isa<Constant>(V) && "Can't get a constant or global slot with this!");
395
396 // Check for uninitialized state and do lazy initialization.
397 initialize();
398
399 ValueMap::iterator FI = fMap.find(V);
400 return FI == fMap.end() ? -1 : FI->second;
401}
402
403
404/// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
405void SlotTracker::CreateModuleSlot(const GlobalValue *V) {
406 assert(V && "Can't insert a null Value into SlotTracker!");
407 assert(V->getType() != Type::VoidTy && "Doesn't need a slot!");
408 assert(!V->hasName() && "Doesn't need a slot!");
409
410 unsigned DestSlot = mNext++;
411 mMap[V] = DestSlot;
412
Chris Lattner604e3512008-08-19 04:47:09 +0000413 ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
Chris Lattner3eee99c2008-08-19 04:36:02 +0000414 DestSlot << " [");
415 // G = Global, F = Function, A = Alias, o = other
Chris Lattner604e3512008-08-19 04:47:09 +0000416 ST_DEBUG((isa<GlobalVariable>(V) ? 'G' :
Chris Lattner3eee99c2008-08-19 04:36:02 +0000417 (isa<Function>(V) ? 'F' :
418 (isa<GlobalAlias>(V) ? 'A' : 'o'))) << "]\n");
419}
420
421
422/// CreateSlot - Create a new slot for the specified value if it has no name.
423void SlotTracker::CreateFunctionSlot(const Value *V) {
424 assert(V->getType() != Type::VoidTy && !V->hasName() &&
425 "Doesn't need a slot!");
426
427 unsigned DestSlot = fNext++;
428 fMap[V] = DestSlot;
429
430 // G = Global, F = Function, o = other
Chris Lattner604e3512008-08-19 04:47:09 +0000431 ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
Chris Lattner3eee99c2008-08-19 04:36:02 +0000432 DestSlot << " [o]\n");
433}
434
435
436
437//===----------------------------------------------------------------------===//
438// AsmWriter Implementation
439//===----------------------------------------------------------------------===//
Chris Lattner7f8845a2002-07-23 18:07:49 +0000440
Chris Lattner0c19df42008-08-23 22:23:09 +0000441static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
Chris Lattnera9f0a112006-12-06 05:50:41 +0000442 std::map<const Type *, std::string> &TypeTable,
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000443 SlotTracker *Machine);
Reid Spencer58d30f22004-07-04 11:50:43 +0000444
Chris Lattner033935d2008-08-17 04:40:13 +0000445
Chris Lattnerb86620e2001-10-29 16:37:48 +0000446
Misha Brukmanc566ca362004-03-02 00:22:19 +0000447/// fillTypeNameTable - If the module has a symbol table, take all global types
448/// and stuff their names into the TypeNames map.
449///
Chris Lattnerb86620e2001-10-29 16:37:48 +0000450static void fillTypeNameTable(const Module *M,
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000451 std::map<const Type *, std::string> &TypeNames) {
Chris Lattner98cf1f52002-11-20 18:36:02 +0000452 if (!M) return;
Reid Spencer32af9e82007-01-06 07:24:44 +0000453 const TypeSymbolTable &ST = M->getTypeSymbolTable();
454 TypeSymbolTable::const_iterator TI = ST.begin();
455 for (; TI != ST.end(); ++TI) {
Reid Spencere7e96712004-05-25 08:53:40 +0000456 // As a heuristic, don't insert pointer to primitive types, because
457 // they are used too often to have a single useful name.
458 //
459 const Type *Ty = cast<Type>(TI->second);
460 if (!isa<PointerType>(Ty) ||
Reid Spencer56010e42004-05-26 21:56:09 +0000461 !cast<PointerType>(Ty)->getElementType()->isPrimitiveType() ||
Chris Lattner03c49532007-01-15 02:27:26 +0000462 !cast<PointerType>(Ty)->getElementType()->isInteger() ||
Reid Spencer56010e42004-05-26 21:56:09 +0000463 isa<OpaqueType>(cast<PointerType>(Ty)->getElementType()))
Chris Lattner585297e82008-08-19 05:26:17 +0000464 TypeNames.insert(std::make_pair(Ty, '%' + getLLVMName(TI->first)));
Chris Lattnerb86620e2001-10-29 16:37:48 +0000465 }
466}
467
468
469
Misha Brukmanb1c93172005-04-21 23:48:37 +0000470static void calcTypeName(const Type *Ty,
John Criswellcd116ba2004-06-01 14:54:08 +0000471 std::vector<const Type *> &TypeStack,
472 std::map<const Type *, std::string> &TypeNames,
Chris Lattner585297e82008-08-19 05:26:17 +0000473 std::string &Result) {
Chris Lattner03c49532007-01-15 02:27:26 +0000474 if (Ty->isInteger() || (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty))) {
John Criswellcd116ba2004-06-01 14:54:08 +0000475 Result += Ty->getDescription(); // Base case
476 return;
477 }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000478
479 // Check to see if the type is named.
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000480 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
John Criswellcd116ba2004-06-01 14:54:08 +0000481 if (I != TypeNames.end()) {
482 Result += I->second;
483 return;
484 }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000485
John Criswellcd116ba2004-06-01 14:54:08 +0000486 if (isa<OpaqueType>(Ty)) {
487 Result += "opaque";
488 return;
489 }
Chris Lattnerf14ead92003-10-30 00:22:33 +0000490
Chris Lattnerb86620e2001-10-29 16:37:48 +0000491 // Check to see if the Type is already on the stack...
492 unsigned Slot = 0, CurSize = TypeStack.size();
493 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
494
Misha Brukmanb1c93172005-04-21 23:48:37 +0000495 // This is another base case for the recursion. In this case, we know
Chris Lattnerb86620e2001-10-29 16:37:48 +0000496 // that we have looped back to a type that we have previously visited.
497 // Generate the appropriate upreference to handle this.
John Criswellcd116ba2004-06-01 14:54:08 +0000498 if (Slot < CurSize) {
499 Result += "\\" + utostr(CurSize-Slot); // Here's the upreference
500 return;
501 }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000502
503 TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
Misha Brukmanb1c93172005-04-21 23:48:37 +0000504
Chris Lattner6b727592004-06-17 18:19:28 +0000505 switch (Ty->getTypeID()) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000506 case Type::IntegerTyID: {
507 unsigned BitWidth = cast<IntegerType>(Ty)->getBitWidth();
Reid Spencerc8721592007-01-12 07:25:20 +0000508 Result += "i" + utostr(BitWidth);
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000509 break;
510 }
Chris Lattner91db5822002-03-29 03:44:36 +0000511 case Type::FunctionTyID: {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000512 const FunctionType *FTy = cast<FunctionType>(Ty);
John Criswellcd116ba2004-06-01 14:54:08 +0000513 calcTypeName(FTy->getReturnType(), TypeStack, TypeNames, Result);
514 Result += " (";
Chris Lattnerfa829be2004-02-09 04:14:01 +0000515 for (FunctionType::param_iterator I = FTy->param_begin(),
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000516 E = FTy->param_end(); I != E; ++I) {
Chris Lattnerfa829be2004-02-09 04:14:01 +0000517 if (I != FTy->param_begin())
Chris Lattnerb86620e2001-10-29 16:37:48 +0000518 Result += ", ";
John Criswellcd116ba2004-06-01 14:54:08 +0000519 calcTypeName(*I, TypeStack, TypeNames, Result);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000520 }
Chris Lattnerd816b532002-04-13 20:53:41 +0000521 if (FTy->isVarArg()) {
Chris Lattnerfa829be2004-02-09 04:14:01 +0000522 if (FTy->getNumParams()) Result += ", ";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000523 Result += "...";
524 }
525 Result += ")";
526 break;
527 }
528 case Type::StructTyID: {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000529 const StructType *STy = cast<StructType>(Ty);
Andrew Lenharthdcb3c972006-12-08 18:06:16 +0000530 if (STy->isPacked())
531 Result += '<';
John Criswellcd116ba2004-06-01 14:54:08 +0000532 Result += "{ ";
Chris Lattnerac6db752004-02-09 04:37:31 +0000533 for (StructType::element_iterator I = STy->element_begin(),
534 E = STy->element_end(); I != E; ++I) {
John Criswellcd116ba2004-06-01 14:54:08 +0000535 calcTypeName(*I, TypeStack, TypeNames, Result);
Dan Gohmanbb9a2112008-09-25 17:37:20 +0000536 if (next(I) != STy->element_end())
537 Result += ',';
538 Result += ' ';
Chris Lattnerb86620e2001-10-29 16:37:48 +0000539 }
Dan Gohmanbb9a2112008-09-25 17:37:20 +0000540 Result += '}';
Andrew Lenharthdcb3c972006-12-08 18:06:16 +0000541 if (STy->isPacked())
542 Result += '>';
Chris Lattnerb86620e2001-10-29 16:37:48 +0000543 break;
544 }
Christopher Lamb54dd24c2007-12-11 08:59:05 +0000545 case Type::PointerTyID: {
546 const PointerType *PTy = cast<PointerType>(Ty);
Chris Lattner585297e82008-08-19 05:26:17 +0000547 calcTypeName(PTy->getElementType(), TypeStack, TypeNames, Result);
Christopher Lamb54dd24c2007-12-11 08:59:05 +0000548 if (unsigned AddressSpace = PTy->getAddressSpace())
549 Result += " addrspace(" + utostr(AddressSpace) + ")";
John Criswellcd116ba2004-06-01 14:54:08 +0000550 Result += "*";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000551 break;
Christopher Lamb54dd24c2007-12-11 08:59:05 +0000552 }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000553 case Type::ArrayTyID: {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000554 const ArrayType *ATy = cast<ArrayType>(Ty);
John Criswellcd116ba2004-06-01 14:54:08 +0000555 Result += "[" + utostr(ATy->getNumElements()) + " x ";
556 calcTypeName(ATy->getElementType(), TypeStack, TypeNames, Result);
557 Result += "]";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000558 break;
559 }
Reid Spencerd84d35b2007-02-15 02:26:10 +0000560 case Type::VectorTyID: {
561 const VectorType *PTy = cast<VectorType>(Ty);
Brian Gaeke02209042004-08-20 06:00:58 +0000562 Result += "<" + utostr(PTy->getNumElements()) + " x ";
563 calcTypeName(PTy->getElementType(), TypeStack, TypeNames, Result);
564 Result += ">";
565 break;
566 }
Chris Lattner15285ab2003-05-14 17:50:47 +0000567 case Type::OpaqueTyID:
John Criswellcd116ba2004-06-01 14:54:08 +0000568 Result += "opaque";
Chris Lattner15285ab2003-05-14 17:50:47 +0000569 break;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000570 default:
John Criswellcd116ba2004-06-01 14:54:08 +0000571 Result += "<unrecognized-type>";
Chris Lattnerfc9f1c92006-12-06 06:40:49 +0000572 break;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000573 }
574
575 TypeStack.pop_back(); // Remove self from stack...
Chris Lattnerb86620e2001-10-29 16:37:48 +0000576}
577
578
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000579/// printTypeInt - The internal guts of printing out a type that has a
580/// potentially named portion.
581///
Chris Lattner0c19df42008-08-23 22:23:09 +0000582static void printTypeInt(raw_ostream &Out, const Type *Ty,
Chris Lattner585297e82008-08-19 05:26:17 +0000583 std::map<const Type *, std::string> &TypeNames) {
Chris Lattnerb86620e2001-10-29 16:37:48 +0000584 // Primitive types always print out their description, regardless of whether
585 // they have been named or not.
586 //
Chris Lattner585297e82008-08-19 05:26:17 +0000587 if (Ty->isInteger() || (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty))) {
588 Out << Ty->getDescription();
589 return;
590 }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000591
592 // Check to see if the type is named.
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000593 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
Chris Lattner585297e82008-08-19 05:26:17 +0000594 if (I != TypeNames.end()) {
595 Out << I->second;
596 return;
597 }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000598
599 // Otherwise we have a type that has not been named but is a derived type.
600 // Carefully recurse the type hierarchy to print out any contained symbolic
601 // names.
602 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000603 std::vector<const Type *> TypeStack;
John Criswellcd116ba2004-06-01 14:54:08 +0000604 std::string TypeName;
605 calcTypeName(Ty, TypeStack, TypeNames, TypeName);
Chris Lattner7f74a562002-01-20 22:54:45 +0000606 TypeNames.insert(std::make_pair(Ty, TypeName));//Cache type name for later use
Chris Lattner585297e82008-08-19 05:26:17 +0000607 Out << TypeName;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000608}
609
Chris Lattner34b95182001-10-31 04:33:19 +0000610
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000611/// WriteTypeSymbolic - This attempts to write the specified type as a symbolic
612/// type, iff there is an entry in the modules symbol table for the specified
613/// type or one of it's component types. This is slower than a simple x << Type
614///
Chris Lattner604e3512008-08-19 04:47:09 +0000615void llvm::WriteTypeSymbolic(std::ostream &Out, const Type *Ty,
616 const Module *M) {
Chris Lattner0c19df42008-08-23 22:23:09 +0000617 raw_os_ostream RO(Out);
618 WriteTypeSymbolic(RO, Ty, M);
619}
620
621void llvm::WriteTypeSymbolic(raw_ostream &Out, const Type *Ty, const Module *M){
Misha Brukmanb1c93172005-04-21 23:48:37 +0000622 Out << ' ';
Chris Lattnerb86620e2001-10-29 16:37:48 +0000623
Chris Lattnerfc9f1c92006-12-06 06:40:49 +0000624 // If they want us to print out a type, but there is no context, we can't
625 // print it symbolically.
Chris Lattner604e3512008-08-19 04:47:09 +0000626 if (!M) {
627 Out << Ty->getDescription();
628 } else {
629 std::map<const Type *, std::string> TypeNames;
630 fillTypeNameTable(M, TypeNames);
631 printTypeInt(Out, Ty, TypeNames);
632 }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000633}
634
Chris Lattner6ed87bd2006-01-23 23:03:36 +0000635// PrintEscapedString - Print each character of the specified string, escaping
636// it if it is not printable or if it is an escape char.
Chris Lattner0c19df42008-08-23 22:23:09 +0000637static void PrintEscapedString(const std::string &Str, raw_ostream &Out) {
Chris Lattner6ed87bd2006-01-23 23:03:36 +0000638 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
639 unsigned char C = Str[i];
640 if (isprint(C) && C != '"' && C != '\\') {
641 Out << C;
642 } else {
643 Out << '\\'
644 << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'))
645 << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
646 }
647 }
648}
649
Chris Lattnerfc9f1c92006-12-06 06:40:49 +0000650static const char *getPredicateText(unsigned predicate) {
Reid Spencer812a1be2006-12-04 05:19:18 +0000651 const char * pred = "unknown";
652 switch (predicate) {
653 case FCmpInst::FCMP_FALSE: pred = "false"; break;
654 case FCmpInst::FCMP_OEQ: pred = "oeq"; break;
655 case FCmpInst::FCMP_OGT: pred = "ogt"; break;
656 case FCmpInst::FCMP_OGE: pred = "oge"; break;
657 case FCmpInst::FCMP_OLT: pred = "olt"; break;
658 case FCmpInst::FCMP_OLE: pred = "ole"; break;
659 case FCmpInst::FCMP_ONE: pred = "one"; break;
660 case FCmpInst::FCMP_ORD: pred = "ord"; break;
661 case FCmpInst::FCMP_UNO: pred = "uno"; break;
662 case FCmpInst::FCMP_UEQ: pred = "ueq"; break;
663 case FCmpInst::FCMP_UGT: pred = "ugt"; break;
664 case FCmpInst::FCMP_UGE: pred = "uge"; break;
665 case FCmpInst::FCMP_ULT: pred = "ult"; break;
666 case FCmpInst::FCMP_ULE: pred = "ule"; break;
667 case FCmpInst::FCMP_UNE: pred = "une"; break;
668 case FCmpInst::FCMP_TRUE: pred = "true"; break;
669 case ICmpInst::ICMP_EQ: pred = "eq"; break;
670 case ICmpInst::ICMP_NE: pred = "ne"; break;
671 case ICmpInst::ICMP_SGT: pred = "sgt"; break;
672 case ICmpInst::ICMP_SGE: pred = "sge"; break;
673 case ICmpInst::ICMP_SLT: pred = "slt"; break;
674 case ICmpInst::ICMP_SLE: pred = "sle"; break;
675 case ICmpInst::ICMP_UGT: pred = "ugt"; break;
676 case ICmpInst::ICMP_UGE: pred = "uge"; break;
677 case ICmpInst::ICMP_ULT: pred = "ult"; break;
678 case ICmpInst::ICMP_ULE: pred = "ule"; break;
679 }
680 return pred;
681}
682
Chris Lattner0c19df42008-08-23 22:23:09 +0000683static void WriteConstantInt(raw_ostream &Out, const Constant *CV,
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000684 std::map<const Type *, std::string> &TypeTable,
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000685 SlotTracker *Machine) {
Zhou Sheng75b871f2007-01-11 12:24:14 +0000686 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Chris Lattner17f71652008-08-17 07:19:36 +0000687 if (CI->getType() == Type::Int1Ty) {
Reid Spencercddc9df2007-01-12 04:24:46 +0000688 Out << (CI->getZExtValue() ? "true" : "false");
Chris Lattner17f71652008-08-17 07:19:36 +0000689 return;
690 }
691 Out << CI->getValue();
692 return;
693 }
694
695 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Dale Johannesen028084e2007-09-12 03:30:33 +0000696 if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEdouble ||
697 &CFP->getValueAPF().getSemantics() == &APFloat::IEEEsingle) {
698 // We would like to output the FP constant value in exponential notation,
699 // but we cannot do this if doing so will lose precision. Check here to
700 // make sure that we only output it in exponential format if we can parse
701 // the value back and get the same value.
702 //
703 bool isDouble = &CFP->getValueAPF().getSemantics()==&APFloat::IEEEdouble;
Chris Lattner17f71652008-08-17 07:19:36 +0000704 double Val = isDouble ? CFP->getValueAPF().convertToDouble() :
705 CFP->getValueAPF().convertToFloat();
Dale Johannesen028084e2007-09-12 03:30:33 +0000706 std::string StrVal = ftostr(CFP->getValueAPF());
Chris Lattner1e194682002-04-18 18:53:13 +0000707
Dale Johannesen028084e2007-09-12 03:30:33 +0000708 // Check to make sure that the stringized number is not some string like
709 // "Inf" or NaN, that atof will accept, but the lexer will not. Check
710 // that the string matches the "[-+]?[0-9]" regex.
711 //
712 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
713 ((StrVal[0] == '-' || StrVal[0] == '+') &&
714 (StrVal[1] >= '0' && StrVal[1] <= '9'))) {
715 // Reparse stringized version!
716 if (atof(StrVal.c_str()) == Val) {
717 Out << StrVal;
718 return;
719 }
Chris Lattner1e194682002-04-18 18:53:13 +0000720 }
Dale Johannesen028084e2007-09-12 03:30:33 +0000721 // Otherwise we could not reparse it to exactly the same value, so we must
722 // output the string in hexadecimal format!
723 assert(sizeof(double) == sizeof(uint64_t) &&
724 "assuming that double is 64 bits!");
725 Out << "0x" << utohexstr(DoubleToBits(Val));
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000726 return;
727 }
728
729 // Some form of long double. These appear as a magic letter identifying
730 // the type, then a fixed number of hex digits.
731 Out << "0x";
732 if (&CFP->getValueAPF().getSemantics() == &APFloat::x87DoubleExtended)
733 Out << 'K';
734 else if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEquad)
735 Out << 'L';
736 else if (&CFP->getValueAPF().getSemantics() == &APFloat::PPCDoubleDouble)
737 Out << 'M';
738 else
739 assert(0 && "Unsupported floating point type");
740 // api needed to prevent premature destruction
741 APInt api = CFP->getValueAPF().convertToAPInt();
742 const uint64_t* p = api.getRawData();
743 uint64_t word = *p;
744 int shiftcount=60;
745 int width = api.getBitWidth();
746 for (int j=0; j<width; j+=4, shiftcount-=4) {
747 unsigned int nibble = (word>>shiftcount) & 15;
748 if (nibble < 10)
749 Out << (unsigned char)(nibble + '0');
Dale Johannesen028084e2007-09-12 03:30:33 +0000750 else
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000751 Out << (unsigned char)(nibble - 10 + 'A');
752 if (shiftcount == 0 && j+4 < width) {
753 word = *(++p);
754 shiftcount = 64;
755 if (width-j-4 < 64)
756 shiftcount = width-j-4;
Dale Johannesen028084e2007-09-12 03:30:33 +0000757 }
758 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000759 return;
760 }
761
762 if (isa<ConstantAggregateZero>(CV)) {
Chris Lattner76b2ff42004-02-15 05:55:15 +0000763 Out << "zeroinitializer";
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000764 return;
765 }
766
767 if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
Chris Lattner1e194682002-04-18 18:53:13 +0000768 // As a special case, print the array as a string if it is an array of
Dan Gohmane9bc2ba2008-05-12 16:34:30 +0000769 // i8 with ConstantInt values.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000770 //
Chris Lattner1e194682002-04-18 18:53:13 +0000771 const Type *ETy = CA->getType()->getElementType();
Chris Lattner6ed87bd2006-01-23 23:03:36 +0000772 if (CA->isString()) {
Chris Lattner1e194682002-04-18 18:53:13 +0000773 Out << "c\"";
Chris Lattner6ed87bd2006-01-23 23:03:36 +0000774 PrintEscapedString(CA->getAsString(), Out);
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000775 Out << '"';
Chris Lattner1e194682002-04-18 18:53:13 +0000776 } else { // Cannot output in string format...
Misha Brukman21bbdb92004-06-04 21:11:51 +0000777 Out << '[';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000778 if (CA->getNumOperands()) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000779 Out << ' ';
Chris Lattner1e194682002-04-18 18:53:13 +0000780 printTypeInt(Out, ETy, TypeTable);
Dan Gohman81313fd2008-09-14 17:21:12 +0000781 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000782 WriteAsOperandInternal(Out, CA->getOperand(0),
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000783 TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000784 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
785 Out << ", ";
Chris Lattner1e194682002-04-18 18:53:13 +0000786 printTypeInt(Out, ETy, TypeTable);
Dan Gohman81313fd2008-09-14 17:21:12 +0000787 Out << ' ';
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000788 WriteAsOperandInternal(Out, CA->getOperand(i), TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000789 }
Dan Gohman81313fd2008-09-14 17:21:12 +0000790 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000791 }
Dan Gohman81313fd2008-09-14 17:21:12 +0000792 Out << ']';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000793 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000794 return;
795 }
796
797 if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Andrew Lenharth0d124b82007-01-08 18:21:30 +0000798 if (CS->getType()->isPacked())
799 Out << '<';
Misha Brukman21bbdb92004-06-04 21:11:51 +0000800 Out << '{';
Jim Laskey3bb78742006-02-25 12:27:03 +0000801 unsigned N = CS->getNumOperands();
802 if (N) {
Chris Lattner604e3512008-08-19 04:47:09 +0000803 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000804 printTypeInt(Out, CS->getOperand(0)->getType(), TypeTable);
Dan Gohman81313fd2008-09-14 17:21:12 +0000805 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000806
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000807 WriteAsOperandInternal(Out, CS->getOperand(0), TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000808
Jim Laskey3bb78742006-02-25 12:27:03 +0000809 for (unsigned i = 1; i < N; i++) {
Chris Lattnerd84bb632002-04-16 21:36:08 +0000810 Out << ", ";
811 printTypeInt(Out, CS->getOperand(i)->getType(), TypeTable);
Dan Gohman81313fd2008-09-14 17:21:12 +0000812 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000813
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000814 WriteAsOperandInternal(Out, CS->getOperand(i), TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000815 }
Dan Gohman81313fd2008-09-14 17:21:12 +0000816 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000817 }
Jim Laskey3bb78742006-02-25 12:27:03 +0000818
Dan Gohman81313fd2008-09-14 17:21:12 +0000819 Out << '}';
Andrew Lenharth0d124b82007-01-08 18:21:30 +0000820 if (CS->getType()->isPacked())
821 Out << '>';
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000822 return;
823 }
824
825 if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
826 const Type *ETy = CP->getType()->getElementType();
827 assert(CP->getNumOperands() > 0 &&
828 "Number of operands for a PackedConst must be > 0");
829 Out << "< ";
830 printTypeInt(Out, ETy, TypeTable);
Dan Gohman81313fd2008-09-14 17:21:12 +0000831 Out << ' ';
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000832 WriteAsOperandInternal(Out, CP->getOperand(0), TypeTable, Machine);
833 for (unsigned i = 1, e = CP->getNumOperands(); i != e; ++i) {
Chris Lattner585297e82008-08-19 05:26:17 +0000834 Out << ", ";
835 printTypeInt(Out, ETy, TypeTable);
Dan Gohman81313fd2008-09-14 17:21:12 +0000836 Out << ' ';
Chris Lattner585297e82008-08-19 05:26:17 +0000837 WriteAsOperandInternal(Out, CP->getOperand(i), TypeTable, Machine);
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000838 }
839 Out << " >";
840 return;
841 }
842
843 if (isa<ConstantPointerNull>(CV)) {
Chris Lattnerd84bb632002-04-16 21:36:08 +0000844 Out << "null";
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000845 return;
846 }
847
848 if (isa<UndefValue>(CV)) {
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000849 Out << "undef";
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000850 return;
851 }
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000852
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000853 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Reid Spencer812a1be2006-12-04 05:19:18 +0000854 Out << CE->getOpcodeName();
855 if (CE->isCompare())
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000856 Out << ' ' << getPredicateText(CE->getPredicate());
Reid Spencer812a1be2006-12-04 05:19:18 +0000857 Out << " (";
Misha Brukmanb1c93172005-04-21 23:48:37 +0000858
Vikram S. Adveb952b542002-07-14 23:14:45 +0000859 for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
860 printTypeInt(Out, (*OI)->getType(), TypeTable);
Dan Gohman81313fd2008-09-14 17:21:12 +0000861 Out << ' ';
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000862 WriteAsOperandInternal(Out, *OI, TypeTable, Machine);
Vikram S. Adveb952b542002-07-14 23:14:45 +0000863 if (OI+1 != CE->op_end())
Chris Lattner3cd8c562002-07-30 18:54:25 +0000864 Out << ", ";
Vikram S. Adveb952b542002-07-14 23:14:45 +0000865 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000866
Dan Gohmana76f0f72008-05-31 19:12:39 +0000867 if (CE->hasIndices()) {
868 const SmallVector<unsigned, 4> &Indices = CE->getIndices();
869 for (unsigned i = 0, e = Indices.size(); i != e; ++i)
870 Out << ", " << Indices[i];
871 }
872
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000873 if (CE->isCast()) {
Chris Lattner83b396b2002-08-15 19:37:43 +0000874 Out << " to ";
875 printTypeInt(Out, CE->getType(), TypeTable);
876 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000877
Misha Brukman21bbdb92004-06-04 21:11:51 +0000878 Out << ')';
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000879 return;
Chris Lattnerd84bb632002-04-16 21:36:08 +0000880 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000881
882 Out << "<placeholder or erroneous Constant>";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000883}
884
885
Misha Brukmanc566ca362004-03-02 00:22:19 +0000886/// WriteAsOperand - Write the name of the specified value out to the specified
887/// ostream. This can be useful when you just want to print int %reg126, not
888/// the whole instruction that generated it.
889///
Chris Lattner0c19df42008-08-23 22:23:09 +0000890static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000891 std::map<const Type*, std::string> &TypeTable,
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000892 SlotTracker *Machine) {
Chris Lattner033935d2008-08-17 04:40:13 +0000893 if (V->hasName()) {
894 PrintLLVMName(Out, V);
895 return;
896 }
897
898 const Constant *CV = dyn_cast<Constant>(V);
899 if (CV && !isa<GlobalValue>(CV)) {
900 WriteConstantInt(Out, CV, TypeTable, Machine);
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000901 return;
902 }
903
904 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
Chris Lattner033935d2008-08-17 04:40:13 +0000905 Out << "asm ";
906 if (IA->hasSideEffects())
907 Out << "sideeffect ";
908 Out << '"';
909 PrintEscapedString(IA->getAsmString(), Out);
910 Out << "\", \"";
911 PrintEscapedString(IA->getConstraintString(), Out);
912 Out << '"';
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000913 return;
914 }
915
916 char Prefix = '%';
917 int Slot;
918 if (Machine) {
919 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
920 Slot = Machine->getGlobalSlot(GV);
921 Prefix = '@';
922 } else {
923 Slot = Machine->getLocalSlot(V);
924 }
Chris Lattner033935d2008-08-17 04:40:13 +0000925 } else {
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000926 Machine = createSlotTracker(V);
Chris Lattner033935d2008-08-17 04:40:13 +0000927 if (Machine) {
928 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
929 Slot = Machine->getGlobalSlot(GV);
930 Prefix = '@';
931 } else {
932 Slot = Machine->getLocalSlot(V);
933 }
Chris Lattnera2d810d2006-01-25 22:26:05 +0000934 } else {
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000935 Slot = -1;
Chris Lattnerd84bb632002-04-16 21:36:08 +0000936 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000937 delete Machine;
Chris Lattnerd84bb632002-04-16 21:36:08 +0000938 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000939
940 if (Slot != -1)
941 Out << Prefix << Slot;
942 else
943 Out << "<badref>";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000944}
945
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000946/// WriteAsOperand - Write the name of the specified value out to the specified
947/// ostream. This can be useful when you just want to print int %reg126, not
948/// the whole instruction that generated it.
949///
Chris Lattner604e3512008-08-19 04:47:09 +0000950void llvm::WriteAsOperand(std::ostream &Out, const Value *V, bool PrintType,
951 const Module *Context) {
Chris Lattner0c19df42008-08-23 22:23:09 +0000952 raw_os_ostream OS(Out);
953 WriteAsOperand(OS, V, PrintType, Context);
954}
955
956void llvm::WriteAsOperand(raw_ostream &Out, const Value *V, bool PrintType,
957 const Module *Context) {
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000958 std::map<const Type *, std::string> TypeNames;
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000959 if (Context == 0) Context = getModuleFromVal(V);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000960
Chris Lattner98cf1f52002-11-20 18:36:02 +0000961 if (Context)
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000962 fillTypeNameTable(Context, TypeNames);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000963
Dan Gohman81313fd2008-09-14 17:21:12 +0000964 if (PrintType) {
Chris Lattnerd84bb632002-04-16 21:36:08 +0000965 printTypeInt(Out, V->getType(), TypeNames);
Dan Gohman81313fd2008-09-14 17:21:12 +0000966 Out << ' ';
967 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000968
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000969 WriteAsOperandInternal(Out, V, TypeNames, 0);
Chris Lattner5e5abe32001-07-20 19:15:21 +0000970}
971
Reid Spencer58d30f22004-07-04 11:50:43 +0000972
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000973namespace {
Chris Lattner2e9fee42001-07-12 23:35:26 +0000974
Chris Lattnerfee714f2001-09-07 16:36:04 +0000975class AssemblyWriter {
Chris Lattner0c19df42008-08-23 22:23:09 +0000976 raw_ostream &Out;
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000977 SlotTracker &Machine;
Chris Lattner7bfee412001-10-29 16:05:51 +0000978 const Module *TheModule;
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000979 std::map<const Type *, std::string> TypeNames;
Chris Lattner8339f7d2003-10-30 23:41:03 +0000980 AssemblyAnnotationWriter *AnnotationWriter;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000981public:
Chris Lattner0c19df42008-08-23 22:23:09 +0000982 inline AssemblyWriter(raw_ostream &o, SlotTracker &Mac, const Module *M,
Chris Lattner8339f7d2003-10-30 23:41:03 +0000983 AssemblyAnnotationWriter *AAW)
Misha Brukmana6619a92004-06-21 21:53:56 +0000984 : Out(o), Machine(Mac), TheModule(M), AnnotationWriter(AAW) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000985
986 // If the module has a symbol table, take all global types and stuff their
987 // names into the TypeNames map.
988 //
Chris Lattnerb86620e2001-10-29 16:37:48 +0000989 fillTypeNameTable(M, TypeNames);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000990 }
991
Chris Lattner0c19df42008-08-23 22:23:09 +0000992 void write(const Module *M) { printModule(M); }
993
994 void write(const GlobalValue *G) {
995 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(G))
996 printGlobal(GV);
997 else if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(G))
998 printAlias(GA);
999 else if (const Function *F = dyn_cast<Function>(G))
1000 printFunction(F);
1001 else
1002 assert(0 && "Unknown global");
1003 }
1004
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001005 void write(const BasicBlock *BB) { printBasicBlock(BB); }
1006 void write(const Instruction *I) { printInstruction(*I); }
1007 void write(const Type *Ty) { printType(Ty); }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001008
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001009 void writeOperand(const Value *Op, bool PrintType);
Devang Patelba3fa6c2008-09-23 23:03:40 +00001010 void writeParamOperand(const Value *Operand, Attributes Attrs);
Chris Lattner1e194682002-04-18 18:53:13 +00001011
Misha Brukman4685e262004-04-28 15:31:21 +00001012 const Module* getModule() { return TheModule; }
1013
Misha Brukmand92f54a2004-11-15 19:30:05 +00001014private:
Chris Lattner7bfee412001-10-29 16:05:51 +00001015 void printModule(const Module *M);
Reid Spencer32af9e82007-01-06 07:24:44 +00001016 void printTypeSymbolTable(const TypeSymbolTable &ST);
Chris Lattner7bfee412001-10-29 16:05:51 +00001017 void printGlobal(const GlobalVariable *GV);
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001018 void printAlias(const GlobalAlias *GV);
Chris Lattner57698e22002-03-26 18:01:55 +00001019 void printFunction(const Function *F);
Devang Patelba3fa6c2008-09-23 23:03:40 +00001020 void printArgument(const Argument *FA, Attributes Attrs);
Chris Lattner7bfee412001-10-29 16:05:51 +00001021 void printBasicBlock(const BasicBlock *BB);
Chris Lattner113f4f42002-06-25 16:13:24 +00001022 void printInstruction(const Instruction &I);
Chris Lattnerd816b532002-04-13 20:53:41 +00001023
1024 // printType - Go to extreme measures to attempt to print out a short,
1025 // symbolic version of a type name.
1026 //
Chris Lattner585297e82008-08-19 05:26:17 +00001027 void printType(const Type *Ty) {
1028 printTypeInt(Out, Ty, TypeNames);
Chris Lattnerd816b532002-04-13 20:53:41 +00001029 }
1030
1031 // printTypeAtLeastOneLevel - Print out one level of the possibly complex type
1032 // without considering any symbolic types that we may have equal to it.
1033 //
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001034 void printTypeAtLeastOneLevel(const Type *Ty);
Chris Lattner7bfee412001-10-29 16:05:51 +00001035
Chris Lattner862e3382001-10-13 06:42:36 +00001036 // printInfoComment - Print a little comment after the instruction indicating
1037 // which slot it occupies.
Chris Lattner113f4f42002-06-25 16:13:24 +00001038 void printInfoComment(const Value &V);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001039};
Reid Spencerf43ac622004-05-27 22:04:46 +00001040} // end of llvm namespace
Chris Lattner2f7c9632001-06-06 20:29:01 +00001041
Misha Brukmanc566ca362004-03-02 00:22:19 +00001042/// printTypeAtLeastOneLevel - Print out one level of the possibly complex type
1043/// without considering any symbolic types that we may have equal to it.
1044///
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001045void AssemblyWriter::printTypeAtLeastOneLevel(const Type *Ty) {
1046 if (const IntegerType *ITy = dyn_cast<IntegerType>(Ty)) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001047 Out << "i" << utostr(ITy->getBitWidth());
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001048 return;
1049 }
1050
1051 if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
Reid Spencer8c4914c2006-12-31 05:24:50 +00001052 printType(FTy->getReturnType());
Reid Spencer8c4914c2006-12-31 05:24:50 +00001053 Out << " (";
Chris Lattnerfa829be2004-02-09 04:14:01 +00001054 for (FunctionType::param_iterator I = FTy->param_begin(),
1055 E = FTy->param_end(); I != E; ++I) {
1056 if (I != FTy->param_begin())
Misha Brukmana6619a92004-06-21 21:53:56 +00001057 Out << ", ";
Chris Lattnerd84bb632002-04-16 21:36:08 +00001058 printType(*I);
Chris Lattnerd816b532002-04-13 20:53:41 +00001059 }
1060 if (FTy->isVarArg()) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001061 if (FTy->getNumParams()) Out << ", ";
1062 Out << "...";
Chris Lattnerd816b532002-04-13 20:53:41 +00001063 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001064 Out << ')';
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001065 return;
1066 }
1067
1068 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
Andrew Lenharthdcb3c972006-12-08 18:06:16 +00001069 if (STy->isPacked())
1070 Out << '<';
Misha Brukmana6619a92004-06-21 21:53:56 +00001071 Out << "{ ";
Chris Lattnerac6db752004-02-09 04:37:31 +00001072 for (StructType::element_iterator I = STy->element_begin(),
1073 E = STy->element_end(); I != E; ++I) {
1074 if (I != STy->element_begin())
Misha Brukmana6619a92004-06-21 21:53:56 +00001075 Out << ", ";
Chris Lattnerd816b532002-04-13 20:53:41 +00001076 printType(*I);
1077 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001078 Out << " }";
Andrew Lenharthdcb3c972006-12-08 18:06:16 +00001079 if (STy->isPacked())
1080 Out << '>';
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001081 return;
1082 }
1083
1084 if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
Christopher Lambac7d6312007-12-18 03:49:35 +00001085 printType(PTy->getElementType());
1086 if (unsigned AddressSpace = PTy->getAddressSpace())
1087 Out << " addrspace(" << AddressSpace << ")";
1088 Out << '*';
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001089 return;
1090 }
1091
1092 if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001093 Out << '[' << ATy->getNumElements() << " x ";
Chris Lattner585297e82008-08-19 05:26:17 +00001094 printType(ATy->getElementType());
1095 Out << ']';
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001096 return;
1097 }
1098
1099 if (const VectorType *PTy = dyn_cast<VectorType>(Ty)) {
Reid Spencere203d352004-08-20 15:37:30 +00001100 Out << '<' << PTy->getNumElements() << " x ";
Chris Lattner585297e82008-08-19 05:26:17 +00001101 printType(PTy->getElementType());
1102 Out << '>';
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001103 return;
Reid Spencere203d352004-08-20 15:37:30 +00001104 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001105
1106 if (isa<OpaqueType>(Ty)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001107 Out << "opaque";
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001108 return;
Chris Lattnerd816b532002-04-13 20:53:41 +00001109 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001110
1111 if (!Ty->isPrimitiveType())
1112 Out << "<unknown derived type>";
1113 printType(Ty);
Chris Lattnerd816b532002-04-13 20:53:41 +00001114}
1115
1116
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001117void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType) {
1118 if (Operand == 0) {
Chris Lattner08f7d0c2005-02-24 16:58:29 +00001119 Out << "<null operand!>";
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001120 } else {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001121 if (PrintType) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001122 printType(Operand->getType());
Dan Gohman81313fd2008-09-14 17:21:12 +00001123 Out << ' ';
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001124 }
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001125 WriteAsOperandInternal(Out, Operand, TypeNames, &Machine);
Chris Lattner08f7d0c2005-02-24 16:58:29 +00001126 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001127}
1128
Dale Johannesen89268bc2008-02-19 21:38:47 +00001129void AssemblyWriter::writeParamOperand(const Value *Operand,
Devang Patelba3fa6c2008-09-23 23:03:40 +00001130 Attributes Attrs) {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00001131 if (Operand == 0) {
1132 Out << "<null operand!>";
1133 } else {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00001134 // Print the type
1135 printType(Operand->getType());
1136 // Print parameter attributes list
Devang Patel4c758ea2008-09-25 21:00:45 +00001137 if (Attrs != Attribute::None)
1138 Out << ' ' << Attribute::getAsString(Attrs);
Dan Gohman81313fd2008-09-14 17:21:12 +00001139 Out << ' ';
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00001140 // Print the operand
1141 WriteAsOperandInternal(Out, Operand, TypeNames, &Machine);
1142 }
1143}
Chris Lattner2f7c9632001-06-06 20:29:01 +00001144
Chris Lattner7bfee412001-10-29 16:05:51 +00001145void AssemblyWriter::printModule(const Module *M) {
Chris Lattner4d8689e2005-03-02 23:12:40 +00001146 if (!M->getModuleIdentifier().empty() &&
Misha Brukmanb1c93172005-04-21 23:48:37 +00001147 // Don't print the ID if it will start a new line (which would
Chris Lattner4d8689e2005-03-02 23:12:40 +00001148 // require a comment char before it).
1149 M->getModuleIdentifier().find('\n') == std::string::npos)
1150 Out << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
1151
Owen Andersone2237542006-10-18 02:21:12 +00001152 if (!M->getDataLayout().empty())
Chris Lattner04897162006-10-22 06:06:56 +00001153 Out << "target datalayout = \"" << M->getDataLayout() << "\"\n";
Reid Spencer48f98c82004-07-25 21:44:54 +00001154 if (!M->getTargetTriple().empty())
Reid Spencerffec7df2004-07-25 21:29:43 +00001155 Out << "target triple = \"" << M->getTargetTriple() << "\"\n";
Misha Brukmanb1c93172005-04-21 23:48:37 +00001156
Chris Lattnereef2fe72006-01-24 04:13:11 +00001157 if (!M->getModuleInlineAsm().empty()) {
Chris Lattnerefaf35d2006-01-24 00:45:30 +00001158 // Split the string into lines, to make it easier to read the .ll file.
Chris Lattnereef2fe72006-01-24 04:13:11 +00001159 std::string Asm = M->getModuleInlineAsm();
Chris Lattnerefaf35d2006-01-24 00:45:30 +00001160 size_t CurPos = 0;
1161 size_t NewLine = Asm.find_first_of('\n', CurPos);
1162 while (NewLine != std::string::npos) {
1163 // We found a newline, print the portion of the asm string from the
1164 // last newline up to this newline.
1165 Out << "module asm \"";
1166 PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.begin()+NewLine),
1167 Out);
1168 Out << "\"\n";
1169 CurPos = NewLine+1;
1170 NewLine = Asm.find_first_of('\n', CurPos);
1171 }
Chris Lattner3acaf5c2006-01-24 00:40:17 +00001172 Out << "module asm \"";
Chris Lattnerefaf35d2006-01-24 00:45:30 +00001173 PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.end()), Out);
Chris Lattner6ed87bd2006-01-23 23:03:36 +00001174 Out << "\"\n";
1175 }
1176
Chris Lattner2cdd49d2004-09-14 05:06:58 +00001177 // Loop over the dependent libraries and emit them.
Chris Lattner730cfe42004-09-14 04:51:44 +00001178 Module::lib_iterator LI = M->lib_begin();
1179 Module::lib_iterator LE = M->lib_end();
Reid Spencer48f98c82004-07-25 21:44:54 +00001180 if (LI != LE) {
Chris Lattner730cfe42004-09-14 04:51:44 +00001181 Out << "deplibs = [ ";
1182 while (LI != LE) {
Chris Lattner2cdd49d2004-09-14 05:06:58 +00001183 Out << '"' << *LI << '"';
Reid Spencerffec7df2004-07-25 21:29:43 +00001184 ++LI;
Chris Lattner730cfe42004-09-14 04:51:44 +00001185 if (LI != LE)
1186 Out << ", ";
Reid Spencerffec7df2004-07-25 21:29:43 +00001187 }
1188 Out << " ]\n";
Reid Spencercc5ff642004-07-25 18:08:18 +00001189 }
Reid Spencerb9e08772004-09-13 23:44:23 +00001190
Chris Lattner2cdd49d2004-09-14 05:06:58 +00001191 // Loop over the symbol table, emitting all named constants.
Reid Spencer32af9e82007-01-06 07:24:44 +00001192 printTypeSymbolTable(M->getTypeSymbolTable());
Misha Brukmanb1c93172005-04-21 23:48:37 +00001193
Chris Lattner54932b02006-12-06 04:41:52 +00001194 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
1195 I != E; ++I)
Chris Lattner113f4f42002-06-25 16:13:24 +00001196 printGlobal(I);
Chris Lattnerd2747052007-04-26 02:24:10 +00001197
1198 // Output all aliases.
1199 if (!M->alias_empty()) Out << "\n";
1200 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
1201 I != E; ++I)
1202 printAlias(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +00001203
Chris Lattner2cdd49d2004-09-14 05:06:58 +00001204 // Output all of the functions.
Chris Lattner113f4f42002-06-25 16:13:24 +00001205 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
1206 printFunction(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +00001207}
1208
Chris Lattner0c19df42008-08-23 22:23:09 +00001209static void PrintLinkage(GlobalValue::LinkageTypes LT, raw_ostream &Out) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001210 switch (LT) {
1211 case GlobalValue::InternalLinkage: Out << "internal "; break;
1212 case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
1213 case GlobalValue::WeakLinkage: Out << "weak "; break;
1214 case GlobalValue::CommonLinkage: Out << "common "; break;
1215 case GlobalValue::AppendingLinkage: Out << "appending "; break;
1216 case GlobalValue::DLLImportLinkage: Out << "dllimport "; break;
1217 case GlobalValue::DLLExportLinkage: Out << "dllexport "; break;
1218 case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break;
1219 case GlobalValue::ExternalLinkage: break;
1220 case GlobalValue::GhostLinkage:
1221 Out << "GhostLinkage not allowed in AsmWriter!\n";
1222 abort();
1223 }
1224}
1225
1226
1227static void PrintVisibility(GlobalValue::VisibilityTypes Vis,
Chris Lattner0c19df42008-08-23 22:23:09 +00001228 raw_ostream &Out) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001229 switch (Vis) {
1230 default: assert(0 && "Invalid visibility style!");
1231 case GlobalValue::DefaultVisibility: break;
1232 case GlobalValue::HiddenVisibility: Out << "hidden "; break;
1233 case GlobalValue::ProtectedVisibility: Out << "protected "; break;
1234 }
1235}
1236
Chris Lattner7bfee412001-10-29 16:05:51 +00001237void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Chris Lattner033935d2008-08-17 04:40:13 +00001238 if (GV->hasName()) {
1239 PrintLLVMName(Out, GV);
1240 Out << " = ";
1241 }
Chris Lattner37798642001-09-18 04:01:05 +00001242
Chris Lattner1508d3f2008-08-19 05:16:28 +00001243 if (!GV->hasInitializer() && GV->hasExternalLinkage())
1244 Out << "external ";
1245
1246 PrintLinkage(GV->getLinkage(), Out);
1247 PrintVisibility(GV->getVisibility(), Out);
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +00001248
1249 if (GV->isThreadLocal()) Out << "thread_local ";
Misha Brukmana6619a92004-06-21 21:53:56 +00001250 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattner2413b162001-12-04 00:03:30 +00001251 printType(GV->getType()->getElementType());
Chris Lattner37798642001-09-18 04:01:05 +00001252
Dan Gohman81313fd2008-09-14 17:21:12 +00001253 if (GV->hasInitializer()) {
1254 Out << ' ';
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001255 writeOperand(GV->getInitializer(), false);
Dan Gohman81313fd2008-09-14 17:21:12 +00001256 }
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001257
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001258 if (unsigned AddressSpace = GV->getType()->getAddressSpace())
1259 Out << " addrspace(" << AddressSpace << ") ";
1260
Chris Lattner4b96c542005-11-12 00:10:19 +00001261 if (GV->hasSection())
1262 Out << ", section \"" << GV->getSection() << '"';
1263 if (GV->getAlignment())
Chris Lattnerf8a974d2005-11-06 06:48:53 +00001264 Out << ", align " << GV->getAlignment();
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001265
Chris Lattner113f4f42002-06-25 16:13:24 +00001266 printInfoComment(*GV);
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001267 Out << '\n';
Chris Lattnerda975502001-09-10 07:58:01 +00001268}
1269
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001270void AssemblyWriter::printAlias(const GlobalAlias *GA) {
Dale Johannesen83e468a2008-06-03 18:14:29 +00001271 // Don't crash when dumping partially built GA
1272 if (!GA->hasName())
1273 Out << "<<nameless>> = ";
Chris Lattner033935d2008-08-17 04:40:13 +00001274 else {
1275 PrintLLVMName(Out, GA);
1276 Out << " = ";
1277 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001278 PrintVisibility(GA->getVisibility(), Out);
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001279
1280 Out << "alias ";
1281
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001282 PrintLinkage(GA->getLinkage(), Out);
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001283
Anton Korobeynikov546ea7e2007-04-29 18:02:48 +00001284 const Constant *Aliasee = GA->getAliasee();
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001285
1286 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Aliasee)) {
1287 printType(GV->getType());
Chris Lattner033935d2008-08-17 04:40:13 +00001288 Out << ' ';
1289 PrintLLVMName(Out, GV);
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001290 } else if (const Function *F = dyn_cast<Function>(Aliasee)) {
1291 printType(F->getFunctionType());
1292 Out << "* ";
1293
Chris Lattner17f71652008-08-17 07:19:36 +00001294 if (F->hasName())
Chris Lattner033935d2008-08-17 04:40:13 +00001295 PrintLLVMName(Out, F);
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001296 else
1297 Out << "@\"\"";
Anton Korobeynikov72d5d422008-03-22 08:17:17 +00001298 } else if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(Aliasee)) {
1299 printType(GA->getType());
Chris Lattner033935d2008-08-17 04:40:13 +00001300 Out << " ";
1301 PrintLLVMName(Out, GA);
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +00001302 } else {
1303 const ConstantExpr *CE = 0;
1304 if ((CE = dyn_cast<ConstantExpr>(Aliasee)) &&
1305 (CE->getOpcode() == Instruction::BitCast)) {
1306 writeOperand(CE, false);
1307 } else
1308 assert(0 && "Unsupported aliasee");
1309 }
1310
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001311 printInfoComment(*GA);
Chris Lattner1508d3f2008-08-19 05:16:28 +00001312 Out << '\n';
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001313}
1314
Reid Spencer32af9e82007-01-06 07:24:44 +00001315void AssemblyWriter::printTypeSymbolTable(const TypeSymbolTable &ST) {
Reid Spencere7e96712004-05-25 08:53:40 +00001316 // Print the types.
Reid Spencer32af9e82007-01-06 07:24:44 +00001317 for (TypeSymbolTable::const_iterator TI = ST.begin(), TE = ST.end();
1318 TI != TE; ++TI) {
Chris Lattner1508d3f2008-08-19 05:16:28 +00001319 Out << '\t';
1320 PrintLLVMName(Out, &TI->first[0], TI->first.size(), LocalPrefix);
1321 Out << " = type ";
Reid Spencere7e96712004-05-25 08:53:40 +00001322
1323 // Make sure we print out at least one level of the type structure, so
1324 // that we do not get %FILE = type %FILE
1325 //
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001326 printTypeAtLeastOneLevel(TI->second);
1327 Out << '\n';
Reid Spencere7e96712004-05-25 08:53:40 +00001328 }
Reid Spencer32af9e82007-01-06 07:24:44 +00001329}
1330
Misha Brukmanc566ca362004-03-02 00:22:19 +00001331/// printFunction - Print all aspects of a function.
1332///
Chris Lattner113f4f42002-06-25 16:13:24 +00001333void AssemblyWriter::printFunction(const Function *F) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001334 // Print out the return type and name.
1335 Out << '\n';
Chris Lattner379a8d22003-04-16 20:28:45 +00001336
Misha Brukmana6619a92004-06-21 21:53:56 +00001337 if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001338
Reid Spencer5301e7c2007-01-30 20:08:39 +00001339 if (F->isDeclaration())
Chris Lattner10f03a62007-08-19 22:15:26 +00001340 Out << "declare ";
1341 else
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00001342 Out << "define ";
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001343
1344 PrintLinkage(F->getLinkage(), Out);
1345 PrintVisibility(F->getVisibility(), Out);
Chris Lattner379a8d22003-04-16 20:28:45 +00001346
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001347 // Print the calling convention.
1348 switch (F->getCallingConv()) {
1349 case CallingConv::C: break; // default
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +00001350 case CallingConv::Fast: Out << "fastcc "; break;
1351 case CallingConv::Cold: Out << "coldcc "; break;
1352 case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break;
1353 case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break;
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001354 default: Out << "cc" << F->getCallingConv() << " "; break;
1355 }
1356
Reid Spencer8c4914c2006-12-31 05:24:50 +00001357 const FunctionType *FT = F->getFunctionType();
Devang Patel4c758ea2008-09-25 21:00:45 +00001358 const AttrListPtr &Attrs = F->getAttributes();
Chris Lattner585297e82008-08-19 05:26:17 +00001359 printType(F->getReturnType());
1360 Out << ' ';
Chris Lattneredceac32008-08-17 07:24:08 +00001361 if (F->hasName())
Chris Lattner033935d2008-08-17 04:40:13 +00001362 PrintLLVMName(Out, F);
Chris Lattner5b337482003-10-18 05:57:43 +00001363 else
Reid Spencer788e3172007-01-26 08:02:52 +00001364 Out << "@\"\"";
Misha Brukmana6619a92004-06-21 21:53:56 +00001365 Out << '(';
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001366 Machine.incorporateFunction(F);
Chris Lattnerfee714f2001-09-07 16:36:04 +00001367
Chris Lattner7bfee412001-10-29 16:05:51 +00001368 // Loop over the arguments, printing them...
Chris Lattnerfee714f2001-09-07 16:36:04 +00001369
Reid Spencer8c4914c2006-12-31 05:24:50 +00001370 unsigned Idx = 1;
Chris Lattner82738fe2007-04-18 00:57:22 +00001371 if (!F->isDeclaration()) {
1372 // If this isn't a declaration, print the argument names as well.
1373 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
1374 I != E; ++I) {
1375 // Insert commas as we go... the first arg doesn't get a comma
1376 if (I != F->arg_begin()) Out << ", ";
Devang Patel4c758ea2008-09-25 21:00:45 +00001377 printArgument(I, Attrs.getAttributes(Idx));
Chris Lattner82738fe2007-04-18 00:57:22 +00001378 Idx++;
1379 }
1380 } else {
1381 // Otherwise, print the types from the function type.
1382 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
1383 // Insert commas as we go... the first arg doesn't get a comma
1384 if (i) Out << ", ";
1385
1386 // Output type...
1387 printType(FT->getParamType(i));
1388
Devang Patel4c758ea2008-09-25 21:00:45 +00001389 Attributes ArgAttrs = Attrs.getAttributes(i+1);
1390 if (ArgAttrs != Attribute::None)
1391 Out << ' ' << Attribute::getAsString(ArgAttrs);
Chris Lattner82738fe2007-04-18 00:57:22 +00001392 }
Reid Spencer8c4914c2006-12-31 05:24:50 +00001393 }
Chris Lattnerfee714f2001-09-07 16:36:04 +00001394
1395 // Finish printing arguments...
Chris Lattner113f4f42002-06-25 16:13:24 +00001396 if (FT->isVarArg()) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001397 if (FT->getNumParams()) Out << ", ";
1398 Out << "..."; // Output varargs portion of signature!
Chris Lattnerfee714f2001-09-07 16:36:04 +00001399 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001400 Out << ')';
Devang Patel4c758ea2008-09-25 21:00:45 +00001401 Attributes RetAttrs = Attrs.getAttributes(0);
1402 if (RetAttrs != Attribute::None)
1403 Out << ' ' << Attribute::getAsString(Attrs.getAttributes(0));
Chris Lattner4b96c542005-11-12 00:10:19 +00001404 if (F->hasSection())
1405 Out << " section \"" << F->getSection() << '"';
Chris Lattnerf8a974d2005-11-06 06:48:53 +00001406 if (F->getAlignment())
1407 Out << " align " << F->getAlignment();
Gordon Henriksend930f912008-08-17 18:44:35 +00001408 if (F->hasGC())
1409 Out << " gc \"" << F->getGC() << '"';
Reid Spencer5301e7c2007-01-30 20:08:39 +00001410 if (F->isDeclaration()) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001411 Out << "\n";
Chris Lattnerb2f02e52002-05-06 03:00:40 +00001412 } else {
Devang Patel329fe722008-09-22 22:32:29 +00001413
1414 bool insideNotes = false;
Devang Patel4c758ea2008-09-25 21:00:45 +00001415 if (F->hasNote(Attribute::AlwaysInline)) {
Dan Gohman5628f642008-09-26 22:02:59 +00001416 Out << " notes(";
Devang Patel329fe722008-09-22 22:32:29 +00001417 insideNotes = true;
1418 Out << "inline=always";
1419 }
Devang Patel4c758ea2008-09-25 21:00:45 +00001420 if (F->hasNote(Attribute::NoInline)) {
Devang Patel329fe722008-09-22 22:32:29 +00001421 if (insideNotes)
1422 Out << ",";
1423 else {
Dan Gohman5628f642008-09-26 22:02:59 +00001424 Out << " notes(";
Devang Patel329fe722008-09-22 22:32:29 +00001425 insideNotes = true;
1426 }
1427 Out << "inline=never";
1428 }
Devang Patel4c758ea2008-09-25 21:00:45 +00001429 if (F->hasNote(Attribute::OptimizeForSize)) {
Devang Patel329fe722008-09-22 22:32:29 +00001430 if (insideNotes)
1431 Out << ",";
1432 else {
Dan Gohman5628f642008-09-26 22:02:59 +00001433 Out << " notes(";
Devang Patel329fe722008-09-22 22:32:29 +00001434 insideNotes = true;
1435 }
1436 Out << "opt_size";
1437 }
1438 if (insideNotes)
1439 Out << ")";
1440
Chris Lattnerd5fbc8f2008-04-21 06:12:55 +00001441 Out << " {";
Misha Brukmanb1c93172005-04-21 23:48:37 +00001442
Chris Lattner6915f8f2002-04-07 22:49:37 +00001443 // Output all of its basic blocks... for the function
Chris Lattner113f4f42002-06-25 16:13:24 +00001444 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
1445 printBasicBlock(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +00001446
Misha Brukmana6619a92004-06-21 21:53:56 +00001447 Out << "}\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +00001448 }
1449
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001450 Machine.purgeFunction();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001451}
1452
Misha Brukmanc566ca362004-03-02 00:22:19 +00001453/// printArgument - This member is called for every argument that is passed into
1454/// the function. Simply print it out
1455///
Dale Johannesen89268bc2008-02-19 21:38:47 +00001456void AssemblyWriter::printArgument(const Argument *Arg,
Devang Patelba3fa6c2008-09-23 23:03:40 +00001457 Attributes Attrs) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001458 // Output type...
Chris Lattner7bfee412001-10-29 16:05:51 +00001459 printType(Arg->getType());
Misha Brukmanb1c93172005-04-21 23:48:37 +00001460
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00001461 // Output parameter attributes list
Devang Patel4c758ea2008-09-25 21:00:45 +00001462 if (Attrs != Attribute::None)
1463 Out << ' ' << Attribute::getAsString(Attrs);
Reid Spencer8c4914c2006-12-31 05:24:50 +00001464
Chris Lattner2f7c9632001-06-06 20:29:01 +00001465 // Output name, if available...
Chris Lattner033935d2008-08-17 04:40:13 +00001466 if (Arg->hasName()) {
1467 Out << ' ';
1468 PrintLLVMName(Out, Arg);
1469 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001470}
1471
Misha Brukmanc566ca362004-03-02 00:22:19 +00001472/// printBasicBlock - This member is called for each basic block in a method.
1473///
Chris Lattner7bfee412001-10-29 16:05:51 +00001474void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Nick Lewycky4d43d3c2008-04-25 16:53:59 +00001475 if (BB->hasName()) { // Print out the label if it exists...
Chris Lattner033935d2008-08-17 04:40:13 +00001476 Out << "\n";
Chris Lattner1508d3f2008-08-19 05:16:28 +00001477 PrintLLVMName(Out, BB->getNameStart(), BB->getNameLen(), LabelPrefix);
Chris Lattner033935d2008-08-17 04:40:13 +00001478 Out << ':';
Nick Lewycky4d43d3c2008-04-25 16:53:59 +00001479 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Chris Lattner67bec132008-04-21 04:20:33 +00001480 Out << "\n; <label>:";
Chris Lattner5e043322007-01-11 03:54:27 +00001481 int Slot = Machine.getLocalSlot(BB);
Chris Lattner757ee0b2004-06-09 19:41:19 +00001482 if (Slot != -1)
Misha Brukmana6619a92004-06-21 21:53:56 +00001483 Out << Slot;
Chris Lattner757ee0b2004-06-09 19:41:19 +00001484 else
Misha Brukmana6619a92004-06-21 21:53:56 +00001485 Out << "<badref>";
Chris Lattner58185f22002-10-02 19:38:55 +00001486 }
Chris Lattner2447ef52003-11-20 00:09:43 +00001487
1488 if (BB->getParent() == 0)
Misha Brukmana6619a92004-06-21 21:53:56 +00001489 Out << "\t\t; Error: Block without parent!";
Chris Lattnerff834c02008-04-22 02:45:44 +00001490 else if (BB != &BB->getParent()->getEntryBlock()) { // Not the entry block?
1491 // Output predecessors for the block...
1492 Out << "\t\t;";
1493 pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
1494
1495 if (PI == PE) {
1496 Out << " No predecessors!";
1497 } else {
Dan Gohman81313fd2008-09-14 17:21:12 +00001498 Out << " preds = ";
Chris Lattnerff834c02008-04-22 02:45:44 +00001499 writeOperand(*PI, false);
1500 for (++PI; PI != PE; ++PI) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001501 Out << ", ";
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001502 writeOperand(*PI, false);
Chris Lattner00211f12003-11-16 22:59:57 +00001503 }
Chris Lattner58185f22002-10-02 19:38:55 +00001504 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001505 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001506
Chris Lattnerff834c02008-04-22 02:45:44 +00001507 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001508
Misha Brukmana6619a92004-06-21 21:53:56 +00001509 if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001510
Chris Lattnerfee714f2001-09-07 16:36:04 +00001511 // Output all of the instructions in the basic block...
Chris Lattner113f4f42002-06-25 16:13:24 +00001512 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
1513 printInstruction(*I);
Chris Lattner96cdd272004-03-08 18:51:45 +00001514
Misha Brukmana6619a92004-06-21 21:53:56 +00001515 if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001516}
1517
Chris Lattner862e3382001-10-13 06:42:36 +00001518
Misha Brukmanc566ca362004-03-02 00:22:19 +00001519/// printInfoComment - Print a little comment after the instruction indicating
1520/// which slot it occupies.
1521///
Chris Lattner113f4f42002-06-25 16:13:24 +00001522void AssemblyWriter::printInfoComment(const Value &V) {
1523 if (V.getType() != Type::VoidTy) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001524 Out << "\t\t; <";
Chris Lattner585297e82008-08-19 05:26:17 +00001525 printType(V.getType());
1526 Out << '>';
Chris Lattner862e3382001-10-13 06:42:36 +00001527
Chris Lattnerb25e5ea2008-08-29 17:19:30 +00001528 if (!V.hasName() && !isa<Instruction>(V)) {
Chris Lattner5e043322007-01-11 03:54:27 +00001529 int SlotNum;
1530 if (const GlobalValue *GV = dyn_cast<GlobalValue>(&V))
1531 SlotNum = Machine.getGlobalSlot(GV);
1532 else
1533 SlotNum = Machine.getLocalSlot(&V);
Chris Lattner757ee0b2004-06-09 19:41:19 +00001534 if (SlotNum == -1)
Misha Brukmana6619a92004-06-21 21:53:56 +00001535 Out << ":<badref>";
Reid Spencer8beac692004-06-09 15:26:53 +00001536 else
Misha Brukmana6619a92004-06-21 21:53:56 +00001537 Out << ':' << SlotNum; // Print out the def slot taken.
Chris Lattner862e3382001-10-13 06:42:36 +00001538 }
Chris Lattnerb6c21db2005-02-01 01:24:01 +00001539 Out << " [#uses=" << V.getNumUses() << ']'; // Output # uses
Chris Lattner862e3382001-10-13 06:42:36 +00001540 }
1541}
1542
Reid Spencere7141c82006-08-28 01:02:49 +00001543// This member is called for each Instruction in a function..
Chris Lattner113f4f42002-06-25 16:13:24 +00001544void AssemblyWriter::printInstruction(const Instruction &I) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001545 if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001546
Chris Lattner82ff9232008-08-23 22:52:27 +00001547 Out << '\t';
Chris Lattner2f7c9632001-06-06 20:29:01 +00001548
1549 // Print out name if it exists...
Chris Lattner033935d2008-08-17 04:40:13 +00001550 if (I.hasName()) {
1551 PrintLLVMName(Out, &I);
1552 Out << " = ";
Chris Lattnerb25e5ea2008-08-29 17:19:30 +00001553 } else if (I.getType() != Type::VoidTy) {
1554 // Print out the def slot taken.
1555 int SlotNum = Machine.getLocalSlot(&I);
1556 if (SlotNum == -1)
1557 Out << "<badref> = ";
1558 else
1559 Out << '%' << SlotNum << " = ";
Chris Lattner033935d2008-08-17 04:40:13 +00001560 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001561
Chris Lattner06038452005-05-06 05:51:46 +00001562 // If this is a volatile load or store, print out the volatile marker.
Chris Lattner504f9242003-09-08 17:45:59 +00001563 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
Chris Lattner06038452005-05-06 05:51:46 +00001564 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile())) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001565 Out << "volatile ";
Chris Lattner06038452005-05-06 05:51:46 +00001566 } else if (isa<CallInst>(I) && cast<CallInst>(I).isTailCall()) {
1567 // If this is a call, check if it's a tail call.
1568 Out << "tail ";
1569 }
Chris Lattner504f9242003-09-08 17:45:59 +00001570
Chris Lattner2f7c9632001-06-06 20:29:01 +00001571 // Print out the opcode...
Misha Brukmana6619a92004-06-21 21:53:56 +00001572 Out << I.getOpcodeName();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001573
Reid Spencer45e52392006-12-03 06:27:29 +00001574 // Print out the compare instruction predicates
Nate Begemand2195702008-05-12 19:01:56 +00001575 if (const CmpInst *CI = dyn_cast<CmpInst>(&I))
Chris Lattner82ff9232008-08-23 22:52:27 +00001576 Out << ' ' << getPredicateText(CI->getPredicate());
Reid Spencer45e52392006-12-03 06:27:29 +00001577
Chris Lattner2f7c9632001-06-06 20:29:01 +00001578 // Print out the type of the operands...
Chris Lattner113f4f42002-06-25 16:13:24 +00001579 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001580
1581 // Special case conditional branches to swizzle the condition out to the front
Chris Lattner113f4f42002-06-25 16:13:24 +00001582 if (isa<BranchInst>(I) && I.getNumOperands() > 1) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001583 Out << ' ';
Chris Lattner113f4f42002-06-25 16:13:24 +00001584 writeOperand(I.getOperand(2), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001585 Out << ", ";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001586 writeOperand(Operand, true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001587 Out << ", ";
Chris Lattner113f4f42002-06-25 16:13:24 +00001588 writeOperand(I.getOperand(1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001589
Chris Lattner8d48df22002-04-13 18:34:38 +00001590 } else if (isa<SwitchInst>(I)) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001591 // Special case switch statement to get formatting nice and correct...
Dan Gohman81313fd2008-09-14 17:21:12 +00001592 Out << ' ';
Chris Lattner82ff9232008-08-23 22:52:27 +00001593 writeOperand(Operand , true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001594 Out << ", ";
Chris Lattner82ff9232008-08-23 22:52:27 +00001595 writeOperand(I.getOperand(1), true);
1596 Out << " [";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001597
Chris Lattner113f4f42002-06-25 16:13:24 +00001598 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001599 Out << "\n\t\t";
Chris Lattner82ff9232008-08-23 22:52:27 +00001600 writeOperand(I.getOperand(op ), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001601 Out << ", ";
Chris Lattner113f4f42002-06-25 16:13:24 +00001602 writeOperand(I.getOperand(op+1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001603 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001604 Out << "\n\t]";
Chris Lattnerda558102001-10-02 03:41:24 +00001605 } else if (isa<PHINode>(I)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001606 Out << ' ';
Chris Lattner113f4f42002-06-25 16:13:24 +00001607 printType(I.getType());
Misha Brukmana6619a92004-06-21 21:53:56 +00001608 Out << ' ';
Chris Lattner2f7c9632001-06-06 20:29:01 +00001609
Chris Lattner113f4f42002-06-25 16:13:24 +00001610 for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001611 if (op) Out << ", ";
Dan Gohman81313fd2008-09-14 17:21:12 +00001612 Out << "[ ";
1613 writeOperand(I.getOperand(op ), false); Out << ", ";
Misha Brukmana6619a92004-06-21 21:53:56 +00001614 writeOperand(I.getOperand(op+1), false); Out << " ]";
Chris Lattner931ef3b2001-06-11 15:04:20 +00001615 }
Dan Gohmana76f0f72008-05-31 19:12:39 +00001616 } else if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(&I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001617 Out << ' ';
Dan Gohmana76f0f72008-05-31 19:12:39 +00001618 writeOperand(I.getOperand(0), true);
1619 for (const unsigned *i = EVI->idx_begin(), *e = EVI->idx_end(); i != e; ++i)
1620 Out << ", " << *i;
1621 } else if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(&I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001622 Out << ' ';
1623 writeOperand(I.getOperand(0), true); Out << ", ";
Dan Gohmana76f0f72008-05-31 19:12:39 +00001624 writeOperand(I.getOperand(1), true);
1625 for (const unsigned *i = IVI->idx_begin(), *e = IVI->idx_end(); i != e; ++i)
1626 Out << ", " << *i;
Devang Patel59643e52008-02-23 00:35:18 +00001627 } else if (isa<ReturnInst>(I) && !Operand) {
1628 Out << " void";
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001629 } else if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
1630 // Print the calling convention being used.
1631 switch (CI->getCallingConv()) {
1632 case CallingConv::C: break; // default
Chris Lattner29d20852006-05-19 21:58:52 +00001633 case CallingConv::Fast: Out << " fastcc"; break;
1634 case CallingConv::Cold: Out << " coldcc"; break;
Chris Lattnerf5270372007-11-18 18:32:16 +00001635 case CallingConv::X86_StdCall: Out << " x86_stdcallcc"; break;
1636 case CallingConv::X86_FastCall: Out << " x86_fastcallcc"; break;
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001637 default: Out << " cc" << CI->getCallingConv(); break;
1638 }
1639
Reid Spencer1517de32007-04-09 06:10:42 +00001640 const PointerType *PTy = cast<PointerType>(Operand->getType());
1641 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1642 const Type *RetTy = FTy->getReturnType();
Devang Patel4c758ea2008-09-25 21:00:45 +00001643 const AttrListPtr &PAL = CI->getAttributes();
Chris Lattner2f2d9472001-11-06 21:28:12 +00001644
Chris Lattner463d6a52003-08-05 15:34:45 +00001645 // If possible, print out the short form of the call instruction. We can
Chris Lattner6915f8f2002-04-07 22:49:37 +00001646 // only do this if the first argument is a pointer to a nonvararg function,
Chris Lattner463d6a52003-08-05 15:34:45 +00001647 // and if the return type is not a pointer to a function.
Chris Lattner2f2d9472001-11-06 21:28:12 +00001648 //
Dan Gohman81313fd2008-09-14 17:21:12 +00001649 Out << ' ';
Chris Lattner463d6a52003-08-05 15:34:45 +00001650 if (!FTy->isVarArg() &&
Misha Brukmanb1c93172005-04-21 23:48:37 +00001651 (!isa<PointerType>(RetTy) ||
Chris Lattnerd9a36a62002-07-25 20:58:51 +00001652 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001653 printType(RetTy);
1654 Out << ' ';
Chris Lattner2f2d9472001-11-06 21:28:12 +00001655 writeOperand(Operand, false);
1656 } else {
1657 writeOperand(Operand, true);
1658 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001659 Out << '(';
Reid Spencer8c4914c2006-12-31 05:24:50 +00001660 for (unsigned op = 1, Eop = I.getNumOperands(); op < Eop; ++op) {
1661 if (op > 1)
Dan Gohman81313fd2008-09-14 17:21:12 +00001662 Out << ", ";
Devang Patel4c758ea2008-09-25 21:00:45 +00001663 writeParamOperand(I.getOperand(op), PAL.getAttributes(op));
Chris Lattner2f7c9632001-06-06 20:29:01 +00001664 }
Dan Gohman81313fd2008-09-14 17:21:12 +00001665 Out << ')';
Devang Patel4c758ea2008-09-25 21:00:45 +00001666 if (PAL.getAttributes(0) != Attribute::None)
1667 Out << ' ' << Attribute::getAsString(PAL.getAttributes(0));
Chris Lattner113f4f42002-06-25 16:13:24 +00001668 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Reid Spencer1517de32007-04-09 06:10:42 +00001669 const PointerType *PTy = cast<PointerType>(Operand->getType());
1670 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1671 const Type *RetTy = FTy->getReturnType();
Devang Patel4c758ea2008-09-25 21:00:45 +00001672 const AttrListPtr &PAL = II->getAttributes();
Chris Lattner463d6a52003-08-05 15:34:45 +00001673
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001674 // Print the calling convention being used.
1675 switch (II->getCallingConv()) {
1676 case CallingConv::C: break; // default
Chris Lattner29d20852006-05-19 21:58:52 +00001677 case CallingConv::Fast: Out << " fastcc"; break;
1678 case CallingConv::Cold: Out << " coldcc"; break;
Dan Gohman81313fd2008-09-14 17:21:12 +00001679 case CallingConv::X86_StdCall: Out << " x86_stdcallcc"; break;
1680 case CallingConv::X86_FastCall: Out << " x86_fastcallcc"; break;
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001681 default: Out << " cc" << II->getCallingConv(); break;
1682 }
1683
Chris Lattner463d6a52003-08-05 15:34:45 +00001684 // If possible, print out the short form of the invoke instruction. We can
1685 // only do this if the first argument is a pointer to a nonvararg function,
1686 // and if the return type is not a pointer to a function.
1687 //
1688 if (!FTy->isVarArg() &&
Misha Brukmanb1c93172005-04-21 23:48:37 +00001689 (!isa<PointerType>(RetTy) ||
Chris Lattner463d6a52003-08-05 15:34:45 +00001690 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001691 Out << ' '; printType(RetTy);
Chris Lattner463d6a52003-08-05 15:34:45 +00001692 writeOperand(Operand, false);
1693 } else {
Dan Gohman81313fd2008-09-14 17:21:12 +00001694 Out << ' ';
Chris Lattner463d6a52003-08-05 15:34:45 +00001695 writeOperand(Operand, true);
1696 }
1697
Misha Brukmana6619a92004-06-21 21:53:56 +00001698 Out << '(';
Reid Spencer8c4914c2006-12-31 05:24:50 +00001699 for (unsigned op = 3, Eop = I.getNumOperands(); op < Eop; ++op) {
1700 if (op > 3)
Dan Gohman81313fd2008-09-14 17:21:12 +00001701 Out << ", ";
Devang Patel4c758ea2008-09-25 21:00:45 +00001702 writeParamOperand(I.getOperand(op), PAL.getAttributes(op-2));
Chris Lattner862e3382001-10-13 06:42:36 +00001703 }
1704
Dan Gohman81313fd2008-09-14 17:21:12 +00001705 Out << ')';
Devang Patel4c758ea2008-09-25 21:00:45 +00001706 if (PAL.getAttributes(0) != Attribute::None)
1707 Out << ' ' << Attribute::getAsString(PAL.getAttributes(0));
Dan Gohman81313fd2008-09-14 17:21:12 +00001708 Out << "\n\t\t\tto ";
Chris Lattner862e3382001-10-13 06:42:36 +00001709 writeOperand(II->getNormalDest(), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001710 Out << " unwind ";
Chris Lattnerfae8ab32004-02-08 21:44:31 +00001711 writeOperand(II->getUnwindDest(), true);
Chris Lattner862e3382001-10-13 06:42:36 +00001712
Chris Lattner113f4f42002-06-25 16:13:24 +00001713 } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(&I)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001714 Out << ' ';
Chris Lattner8d48df22002-04-13 18:34:38 +00001715 printType(AI->getType()->getElementType());
1716 if (AI->isArrayAllocation()) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001717 Out << ", ";
Chris Lattner8d48df22002-04-13 18:34:38 +00001718 writeOperand(AI->getArraySize(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001719 }
Nate Begeman848622f2005-11-05 09:21:28 +00001720 if (AI->getAlignment()) {
Chris Lattner7aeee3a2005-11-05 21:20:34 +00001721 Out << ", align " << AI->getAlignment();
Nate Begeman848622f2005-11-05 09:21:28 +00001722 }
Chris Lattner862e3382001-10-13 06:42:36 +00001723 } else if (isa<CastInst>(I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001724 if (Operand) {
1725 Out << ' ';
1726 writeOperand(Operand, true); // Work with broken code
1727 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001728 Out << " to ";
Chris Lattner113f4f42002-06-25 16:13:24 +00001729 printType(I.getType());
Chris Lattner5b337482003-10-18 05:57:43 +00001730 } else if (isa<VAArgInst>(I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001731 if (Operand) {
1732 Out << ' ';
1733 writeOperand(Operand, true); // Work with broken code
1734 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001735 Out << ", ";
Chris Lattnerf70da102003-05-08 02:44:12 +00001736 printType(I.getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +00001737 } else if (Operand) { // Print the normal way...
1738
Misha Brukmanb1c93172005-04-21 23:48:37 +00001739 // PrintAllTypes - Instructions who have operands of all the same type
Chris Lattner2f7c9632001-06-06 20:29:01 +00001740 // omit the type from all but the first operand. If the instruction has
1741 // different type operands (for example br), then they are all printed.
1742 bool PrintAllTypes = false;
1743 const Type *TheType = Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001744
Reid Spencer0cdd04f2007-02-02 13:54:55 +00001745 // Select, Store and ShuffleVector always print all types.
Devang Patelce556d92008-03-04 22:05:14 +00001746 if (isa<SelectInst>(I) || isa<StoreInst>(I) || isa<ShuffleVectorInst>(I)
1747 || isa<ReturnInst>(I)) {
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00001748 PrintAllTypes = true;
1749 } else {
1750 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
1751 Operand = I.getOperand(i);
1752 if (Operand->getType() != TheType) {
1753 PrintAllTypes = true; // We have differing types! Print them all!
1754 break;
1755 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001756 }
1757 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001758
Chris Lattner7bfee412001-10-29 16:05:51 +00001759 if (!PrintAllTypes) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001760 Out << ' ';
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00001761 printType(TheType);
Chris Lattner7bfee412001-10-29 16:05:51 +00001762 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001763
Dan Gohman81313fd2008-09-14 17:21:12 +00001764 Out << ' ';
Chris Lattner113f4f42002-06-25 16:13:24 +00001765 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001766 if (i) Out << ", ";
Chris Lattner113f4f42002-06-25 16:13:24 +00001767 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001768 }
1769 }
Christopher Lamb84485702007-04-22 19:24:39 +00001770
1771 // Print post operand alignment for load/store
1772 if (isa<LoadInst>(I) && cast<LoadInst>(I).getAlignment()) {
1773 Out << ", align " << cast<LoadInst>(I).getAlignment();
1774 } else if (isa<StoreInst>(I) && cast<StoreInst>(I).getAlignment()) {
1775 Out << ", align " << cast<StoreInst>(I).getAlignment();
1776 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001777
Chris Lattner862e3382001-10-13 06:42:36 +00001778 printInfoComment(I);
Chris Lattner82ff9232008-08-23 22:52:27 +00001779 Out << '\n';
Chris Lattner2f7c9632001-06-06 20:29:01 +00001780}
1781
1782
1783//===----------------------------------------------------------------------===//
1784// External Interface declarations
1785//===----------------------------------------------------------------------===//
1786
Chris Lattner8339f7d2003-10-30 23:41:03 +00001787void Module::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Chris Lattner0c19df42008-08-23 22:23:09 +00001788 raw_os_ostream OS(o);
1789 print(OS, AAW);
1790}
1791void Module::print(raw_ostream &OS, AssemblyAnnotationWriter *AAW) const {
Chris Lattnere36fd8a2008-08-19 04:26:57 +00001792 SlotTracker SlotTable(this);
Chris Lattner0c19df42008-08-23 22:23:09 +00001793 AssemblyWriter W(OS, SlotTable, this, AAW);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001794 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001795}
1796
Misha Brukmanb1c93172005-04-21 23:48:37 +00001797void Type::print(std::ostream &o) const {
Chris Lattner0c19df42008-08-23 22:23:09 +00001798 raw_os_ostream OS(o);
1799 print(OS);
1800}
1801
1802void Type::print(raw_ostream &o) const {
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001803 if (this == 0)
1804 o << "<null Type>";
1805 else
1806 o << getDescription();
1807}
1808
Chris Lattner0c19df42008-08-23 22:23:09 +00001809void Value::print(raw_ostream &OS, AssemblyAnnotationWriter *AAW) const {
1810 if (this == 0) {
1811 OS << "printing a <null> value\n";
1812 return;
1813 }
1814
1815 if (const Instruction *I = dyn_cast<Instruction>(this)) {
1816 const Function *F = I->getParent() ? I->getParent()->getParent() : 0;
1817 SlotTracker SlotTable(F);
1818 AssemblyWriter W(OS, SlotTable, F ? F->getParent() : 0, AAW);
1819 W.write(I);
1820 } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(this)) {
1821 SlotTracker SlotTable(BB->getParent());
1822 AssemblyWriter W(OS, SlotTable,
1823 BB->getParent() ? BB->getParent()->getParent() : 0, AAW);
1824 W.write(BB);
1825 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(this)) {
1826 SlotTracker SlotTable(GV->getParent());
1827 AssemblyWriter W(OS, SlotTable, GV->getParent(), 0);
1828 W.write(GV);
1829 } else if (const Constant *C = dyn_cast<Constant>(this)) {
1830 OS << ' ' << C->getType()->getDescription() << ' ';
1831 std::map<const Type *, std::string> TypeTable;
1832 WriteConstantInt(OS, C, TypeTable, 0);
1833 } else if (const Argument *A = dyn_cast<Argument>(this)) {
1834 WriteAsOperand(OS, this, true,
1835 A->getParent() ? A->getParent()->getParent() : 0);
1836 } else if (isa<InlineAsm>(this)) {
1837 WriteAsOperand(OS, this, true, 0);
1838 } else {
Chris Lattnerd7586252008-08-24 18:33:17 +00001839 // FIXME: PseudoSourceValue breaks this!
1840 //assert(0 && "Unknown value to print out!");
Chris Lattner0c19df42008-08-23 22:23:09 +00001841 }
1842}
1843
1844void Value::print(std::ostream &O, AssemblyAnnotationWriter *AAW) const {
1845 raw_os_ostream OS(O);
1846 print(OS, AAW);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001847}
1848
Chris Lattnerdab942552008-08-25 17:03:15 +00001849// Value::dump - allow easy printing of Values from the debugger.
Chris Lattner820eebc2008-08-25 04:55:46 +00001850void Value::dump() const { print(errs()); errs() << '\n'; errs().flush(); }
Reid Spencer52641832004-05-25 18:14:38 +00001851
Chris Lattnerdab942552008-08-25 17:03:15 +00001852// Type::dump - allow easy printing of Types from the debugger.
Chris Lattner820eebc2008-08-25 04:55:46 +00001853void Type::dump() const { print(errs()); errs() << '\n'; errs().flush(); }
Chris Lattner0c19df42008-08-23 22:23:09 +00001854
Chris Lattnerdab942552008-08-25 17:03:15 +00001855// Module::dump() - Allow printing of Modules from the debugger.
Chris Lattner820eebc2008-08-25 04:55:46 +00001856void Module::dump() const { print(errs(), 0); errs().flush(); }
Chris Lattner0c19df42008-08-23 22:23:09 +00001857
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001858