blob: 4fe3c02324fc3a5d26dce33f2fa54101d17e8c51 [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//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source 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"
Chris Lattnerfee714f2001-09-07 16:36:04 +000027#include "llvm/SymbolTable.h"
Reid Spencer32af9e82007-01-06 07:24:44 +000028#include "llvm/TypeSymbolTable.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000029#include "llvm/ADT/StringExtras.h"
30#include "llvm/ADT/STLExtras.h"
Bill Wendlingdfc91892006-11-28 02:09:03 +000031#include "llvm/Support/CFG.h"
Jim Laskeyb74c6662005-08-17 19:34:49 +000032#include "llvm/Support/MathExtras.h"
Bill Wendlingdfc91892006-11-28 02:09:03 +000033#include "llvm/Support/Streams.h"
Chris Lattnerfee714f2001-09-07 16:36:04 +000034#include <algorithm>
Chris Lattner189d19f2003-11-21 20:23:48 +000035using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000036
Chris Lattner60a7dd12004-07-15 02:51:31 +000037namespace llvm {
Reid Spencer16f2f7f2004-05-26 07:18:52 +000038
Reid Spencer294715b2005-05-15 16:13:11 +000039// Make virtual table appear in this compilation unit.
40AssemblyAnnotationWriter::~AssemblyAnnotationWriter() {}
41
Reid Spencer16f2f7f2004-05-26 07:18:52 +000042/// This class provides computation of slot numbers for LLVM Assembly writing.
43/// @brief LLVM Assembly Writing Slot Computation.
44class SlotMachine {
45
46/// @name Types
47/// @{
48public:
49
50 /// @brief A mapping of Values to slot numbers
51 typedef std::map<const Value*, unsigned> ValueMap;
52
53 /// @brief A plane with next slot number and ValueMap
Misha Brukmanb1c93172005-04-21 23:48:37 +000054 struct ValuePlane {
Reid Spencer16f2f7f2004-05-26 07:18:52 +000055 unsigned next_slot; ///< The next slot number to use
56 ValueMap map; ///< The map of Value* -> unsigned
Reid Spencer58d30f22004-07-04 11:50:43 +000057 ValuePlane() { next_slot = 0; } ///< Make sure we start at 0
58 };
59
Reid Spencer16f2f7f2004-05-26 07:18:52 +000060 /// @brief The map of planes by Type
Reid Spencer58d30f22004-07-04 11:50:43 +000061 typedef std::map<const Type*, ValuePlane> TypedPlanes;
Reid Spencer16f2f7f2004-05-26 07:18:52 +000062
63/// @}
64/// @name Constructors
65/// @{
66public:
67 /// @brief Construct from a module
Chris Lattner23e829a2006-12-06 05:12:21 +000068 SlotMachine(const Module *M);
Reid Spencer16f2f7f2004-05-26 07:18:52 +000069
70 /// @brief Construct from a function, starting out in incorp state.
Chris Lattner23e829a2006-12-06 05:12:21 +000071 SlotMachine(const Function *F);
Reid Spencer16f2f7f2004-05-26 07:18:52 +000072
73/// @}
74/// @name Accessors
75/// @{
76public:
77 /// Return the slot number of the specified value in it's type
Chris Lattner5e043322007-01-11 03:54:27 +000078 /// plane. If something is not in the SlotMachine, return -1.
79 int getLocalSlot(const Value *V);
80 int getGlobalSlot(const GlobalValue *V);
Reid Spencer8beac692004-06-09 15:26:53 +000081
Reid Spencer16f2f7f2004-05-26 07:18:52 +000082/// @}
83/// @name Mutators
84/// @{
85public:
Misha Brukmanb1c93172005-04-21 23:48:37 +000086 /// If you'd like to deal with a function instead of just a module, use
Reid Spencer16f2f7f2004-05-26 07:18:52 +000087 /// this method to get its data into the SlotMachine.
Misha Brukmanb1c93172005-04-21 23:48:37 +000088 void incorporateFunction(const Function *F) {
89 TheFunction = F;
Reid Spencerb0ac8c42004-08-16 07:46:33 +000090 FunctionProcessed = false;
91 }
Reid Spencer16f2f7f2004-05-26 07:18:52 +000092
Misha Brukmanb1c93172005-04-21 23:48:37 +000093 /// After calling incorporateFunction, use this method to remove the
94 /// most recently incorporated function from the SlotMachine. This
Reid Spencer16f2f7f2004-05-26 07:18:52 +000095 /// will reset the state of the machine back to just the module contents.
96 void purgeFunction();
97
98/// @}
99/// @name Implementation Details
100/// @{
101private:
Reid Spencer56010e42004-05-26 21:56:09 +0000102 /// This function does the actual initialization.
103 inline void initialize();
104
Chris Lattnerea862a32007-01-09 07:55:49 +0000105 /// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
106 void CreateModuleSlot(const GlobalValue *V);
107
108 /// CreateFunctionSlot - Insert the specified Value* into the slot table.
109 void CreateFunctionSlot(const Value *V);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000110
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000111 /// Add all of the module level global variables (and their initializers)
112 /// and function declarations, but not the contents of those functions.
113 void processModule();
114
Reid Spencer56010e42004-05-26 21:56:09 +0000115 /// Add all of the functions arguments, basic blocks, and instructions
116 void processFunction();
117
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000118 SlotMachine(const SlotMachine &); // DO NOT IMPLEMENT
119 void operator=(const SlotMachine &); // DO NOT IMPLEMENT
120
121/// @}
122/// @name Data
123/// @{
124public:
125
126 /// @brief The module for which we are holding slot numbers
Reid Spencer56010e42004-05-26 21:56:09 +0000127 const Module* TheModule;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000128
Reid Spencer56010e42004-05-26 21:56:09 +0000129 /// @brief The function for which we are holding slot numbers
130 const Function* TheFunction;
Reid Spencerb0ac8c42004-08-16 07:46:33 +0000131 bool FunctionProcessed;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000132
133 /// @brief The TypePlanes map for the module level data
134 TypedPlanes mMap;
135
136 /// @brief The TypePlanes map for the function level data
137 TypedPlanes fMap;
138
139/// @}
140
141};
142
Chris Lattner60a7dd12004-07-15 02:51:31 +0000143} // end namespace llvm
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000144
Chris Lattnerc2d3d312006-08-27 22:42:52 +0000145static RegisterPass<PrintModulePass>
Chris Lattnere7134c52006-08-21 17:20:01 +0000146X("printm", "Print module to stderr");
Chris Lattnerc2d3d312006-08-27 22:42:52 +0000147static RegisterPass<PrintFunctionPass>
Chris Lattnere7134c52006-08-21 17:20:01 +0000148Y("print","Print function to stderr");
Chris Lattner7f8845a2002-07-23 18:07:49 +0000149
Misha Brukmanb1c93172005-04-21 23:48:37 +0000150static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
Chris Lattnera9f0a112006-12-06 05:50:41 +0000151 std::map<const Type *, std::string> &TypeTable,
Reid Spencer58d30f22004-07-04 11:50:43 +0000152 SlotMachine *Machine);
153
Chris Lattnerb86620e2001-10-29 16:37:48 +0000154static const Module *getModuleFromVal(const Value *V) {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000155 if (const Argument *MA = dyn_cast<Argument>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +0000156 return MA->getParent() ? MA->getParent()->getParent() : 0;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000157 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +0000158 return BB->getParent() ? BB->getParent()->getParent() : 0;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000159 else if (const Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner57698e22002-03-26 18:01:55 +0000160 const Function *M = I->getParent() ? I->getParent()->getParent() : 0;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000161 return M ? M->getParent() : 0;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000162 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +0000163 return GV->getParent();
Chris Lattnerb86620e2001-10-29 16:37:48 +0000164 return 0;
165}
166
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000167static SlotMachine *createSlotMachine(const Value *V) {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000168 if (const Argument *FA = dyn_cast<Argument>(V)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000169 return new SlotMachine(FA->getParent());
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000170 } else if (const Instruction *I = dyn_cast<Instruction>(V)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000171 return new SlotMachine(I->getParent()->getParent());
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000172 } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000173 return new SlotMachine(BB->getParent());
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000174 } else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)){
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000175 return new SlotMachine(GV->getParent());
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000176 } else if (const Function *Func = dyn_cast<Function>(V)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000177 return new SlotMachine(Func);
Chris Lattner7bfee412001-10-29 16:05:51 +0000178 }
179 return 0;
180}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000181
Reid Spencer788e3172007-01-26 08:02:52 +0000182/// NameNeedsQuotes - Return true if the specified llvm name should be wrapped
183/// with ""'s.
184static bool NameNeedsQuotes(const std::string &Name) {
185 if (Name[0] >= '0' && Name[0] <= '9') return true;
Chris Lattner1c343042003-08-22 05:40:38 +0000186 // Scan to see if we have any characters that are not on the "white list"
187 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
188 char C = Name[i];
189 assert(C != '"' && "Illegal character in LLVM value name!");
190 if ((C < 'a' || C > 'z') && (C < 'A' || C > 'Z') && (C < '0' || C > '9') &&
191 C != '-' && C != '.' && C != '_')
Reid Spencer788e3172007-01-26 08:02:52 +0000192 return true;
193 }
194 return false;
195}
196
197enum PrefixType {
198 GlobalPrefix,
199 LabelPrefix,
200 LocalPrefix
201};
202
203/// getLLVMName - Turn the specified string into an 'LLVM name', which is either
204/// prefixed with % (if the string only contains simple characters) or is
205/// surrounded with ""'s (if it has special chars in it).
206static std::string getLLVMName(const std::string &Name, PrefixType Prefix) {
207 assert(!Name.empty() && "Cannot get empty name!");
208
209 // First character cannot start with a number...
210 if (NameNeedsQuotes(Name)) {
211 if (Prefix == GlobalPrefix)
212 return "@\"" + Name + "\"";
213 return "\"" + Name + "\"";
Chris Lattner1c343042003-08-22 05:40:38 +0000214 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000215
Chris Lattner1c343042003-08-22 05:40:38 +0000216 // If we get here, then the identifier is legal to use as a "VarID".
Reid Spencer788e3172007-01-26 08:02:52 +0000217 switch (Prefix) {
218 default: assert(0 && "Bad prefix!");
219 case GlobalPrefix: return '@' + Name;
220 case LabelPrefix: return Name;
221 case LocalPrefix: return '%' + Name;
222 }
Chris Lattner1c343042003-08-22 05:40:38 +0000223}
224
Chris Lattnerb86620e2001-10-29 16:37:48 +0000225
Misha Brukmanc566ca362004-03-02 00:22:19 +0000226/// fillTypeNameTable - If the module has a symbol table, take all global types
227/// and stuff their names into the TypeNames map.
228///
Chris Lattnerb86620e2001-10-29 16:37:48 +0000229static void fillTypeNameTable(const Module *M,
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000230 std::map<const Type *, std::string> &TypeNames) {
Chris Lattner98cf1f52002-11-20 18:36:02 +0000231 if (!M) return;
Reid Spencer32af9e82007-01-06 07:24:44 +0000232 const TypeSymbolTable &ST = M->getTypeSymbolTable();
233 TypeSymbolTable::const_iterator TI = ST.begin();
234 for (; TI != ST.end(); ++TI) {
Reid Spencere7e96712004-05-25 08:53:40 +0000235 // As a heuristic, don't insert pointer to primitive types, because
236 // they are used too often to have a single useful name.
237 //
238 const Type *Ty = cast<Type>(TI->second);
239 if (!isa<PointerType>(Ty) ||
Reid Spencer56010e42004-05-26 21:56:09 +0000240 !cast<PointerType>(Ty)->getElementType()->isPrimitiveType() ||
Chris Lattner03c49532007-01-15 02:27:26 +0000241 !cast<PointerType>(Ty)->getElementType()->isInteger() ||
Reid Spencer56010e42004-05-26 21:56:09 +0000242 isa<OpaqueType>(cast<PointerType>(Ty)->getElementType()))
Reid Spencer788e3172007-01-26 08:02:52 +0000243 TypeNames.insert(std::make_pair(Ty, getLLVMName(TI->first, LocalPrefix)));
Chris Lattnerb86620e2001-10-29 16:37:48 +0000244 }
245}
246
247
248
Misha Brukmanb1c93172005-04-21 23:48:37 +0000249static void calcTypeName(const Type *Ty,
John Criswellcd116ba2004-06-01 14:54:08 +0000250 std::vector<const Type *> &TypeStack,
251 std::map<const Type *, std::string> &TypeNames,
252 std::string & Result){
Chris Lattner03c49532007-01-15 02:27:26 +0000253 if (Ty->isInteger() || (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty))) {
John Criswellcd116ba2004-06-01 14:54:08 +0000254 Result += Ty->getDescription(); // Base case
255 return;
256 }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000257
258 // Check to see if the type is named.
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000259 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
John Criswellcd116ba2004-06-01 14:54:08 +0000260 if (I != TypeNames.end()) {
261 Result += I->second;
262 return;
263 }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000264
John Criswellcd116ba2004-06-01 14:54:08 +0000265 if (isa<OpaqueType>(Ty)) {
266 Result += "opaque";
267 return;
268 }
Chris Lattnerf14ead92003-10-30 00:22:33 +0000269
Chris Lattnerb86620e2001-10-29 16:37:48 +0000270 // Check to see if the Type is already on the stack...
271 unsigned Slot = 0, CurSize = TypeStack.size();
272 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
273
Misha Brukmanb1c93172005-04-21 23:48:37 +0000274 // This is another base case for the recursion. In this case, we know
Chris Lattnerb86620e2001-10-29 16:37:48 +0000275 // that we have looped back to a type that we have previously visited.
276 // Generate the appropriate upreference to handle this.
John Criswellcd116ba2004-06-01 14:54:08 +0000277 if (Slot < CurSize) {
278 Result += "\\" + utostr(CurSize-Slot); // Here's the upreference
279 return;
280 }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000281
282 TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
Misha Brukmanb1c93172005-04-21 23:48:37 +0000283
Chris Lattner6b727592004-06-17 18:19:28 +0000284 switch (Ty->getTypeID()) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000285 case Type::IntegerTyID: {
286 unsigned BitWidth = cast<IntegerType>(Ty)->getBitWidth();
Reid Spencerc8721592007-01-12 07:25:20 +0000287 Result += "i" + utostr(BitWidth);
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000288 break;
289 }
Chris Lattner91db5822002-03-29 03:44:36 +0000290 case Type::FunctionTyID: {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000291 const FunctionType *FTy = cast<FunctionType>(Ty);
John Criswellcd116ba2004-06-01 14:54:08 +0000292 calcTypeName(FTy->getReturnType(), TypeStack, TypeNames, Result);
293 Result += " (";
Reid Spencer8c4914c2006-12-31 05:24:50 +0000294 unsigned Idx = 1;
Chris Lattnerfa829be2004-02-09 04:14:01 +0000295 for (FunctionType::param_iterator I = FTy->param_begin(),
296 E = FTy->param_end(); I != E; ++I) {
297 if (I != FTy->param_begin())
Chris Lattnerb86620e2001-10-29 16:37:48 +0000298 Result += ", ";
John Criswellcd116ba2004-06-01 14:54:08 +0000299 calcTypeName(*I, TypeStack, TypeNames, Result);
Reid Spencer8c4914c2006-12-31 05:24:50 +0000300 if (FTy->getParamAttrs(Idx)) {
301 Result += + " ";
302 Result += FunctionType::getParamAttrsText(FTy->getParamAttrs(Idx));
303 }
304 Idx++;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000305 }
Chris Lattnerd816b532002-04-13 20:53:41 +0000306 if (FTy->isVarArg()) {
Chris Lattnerfa829be2004-02-09 04:14:01 +0000307 if (FTy->getNumParams()) Result += ", ";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000308 Result += "...";
309 }
310 Result += ")";
Reid Spencer136a91c2007-01-05 17:06:19 +0000311 if (FTy->getParamAttrs(0)) {
312 Result += " ";
313 Result += FunctionType::getParamAttrsText(FTy->getParamAttrs(0));
314 }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000315 break;
316 }
317 case Type::StructTyID: {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000318 const StructType *STy = cast<StructType>(Ty);
Andrew Lenharthdcb3c972006-12-08 18:06:16 +0000319 if (STy->isPacked())
320 Result += '<';
John Criswellcd116ba2004-06-01 14:54:08 +0000321 Result += "{ ";
Chris Lattnerac6db752004-02-09 04:37:31 +0000322 for (StructType::element_iterator I = STy->element_begin(),
323 E = STy->element_end(); I != E; ++I) {
324 if (I != STy->element_begin())
Chris Lattnerb86620e2001-10-29 16:37:48 +0000325 Result += ", ";
John Criswellcd116ba2004-06-01 14:54:08 +0000326 calcTypeName(*I, TypeStack, TypeNames, Result);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000327 }
328 Result += " }";
Andrew Lenharthdcb3c972006-12-08 18:06:16 +0000329 if (STy->isPacked())
330 Result += '>';
Chris Lattnerb86620e2001-10-29 16:37:48 +0000331 break;
332 }
333 case Type::PointerTyID:
Misha Brukmanb1c93172005-04-21 23:48:37 +0000334 calcTypeName(cast<PointerType>(Ty)->getElementType(),
John Criswellcd116ba2004-06-01 14:54:08 +0000335 TypeStack, TypeNames, Result);
336 Result += "*";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000337 break;
338 case Type::ArrayTyID: {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000339 const ArrayType *ATy = cast<ArrayType>(Ty);
John Criswellcd116ba2004-06-01 14:54:08 +0000340 Result += "[" + utostr(ATy->getNumElements()) + " x ";
341 calcTypeName(ATy->getElementType(), TypeStack, TypeNames, Result);
342 Result += "]";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000343 break;
344 }
Brian Gaeke02209042004-08-20 06:00:58 +0000345 case Type::PackedTyID: {
346 const PackedType *PTy = cast<PackedType>(Ty);
347 Result += "<" + utostr(PTy->getNumElements()) + " x ";
348 calcTypeName(PTy->getElementType(), TypeStack, TypeNames, Result);
349 Result += ">";
350 break;
351 }
Chris Lattner15285ab2003-05-14 17:50:47 +0000352 case Type::OpaqueTyID:
John Criswellcd116ba2004-06-01 14:54:08 +0000353 Result += "opaque";
Chris Lattner15285ab2003-05-14 17:50:47 +0000354 break;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000355 default:
John Criswellcd116ba2004-06-01 14:54:08 +0000356 Result += "<unrecognized-type>";
Chris Lattnerfc9f1c92006-12-06 06:40:49 +0000357 break;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000358 }
359
360 TypeStack.pop_back(); // Remove self from stack...
Chris Lattnerb86620e2001-10-29 16:37:48 +0000361}
362
363
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000364/// printTypeInt - The internal guts of printing out a type that has a
365/// potentially named portion.
366///
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000367static std::ostream &printTypeInt(std::ostream &Out, const Type *Ty,
368 std::map<const Type *, std::string> &TypeNames) {
Chris Lattnerb86620e2001-10-29 16:37:48 +0000369 // Primitive types always print out their description, regardless of whether
370 // they have been named or not.
371 //
Chris Lattner03c49532007-01-15 02:27:26 +0000372 if (Ty->isInteger() || (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty)))
Chris Lattner92d60532003-10-30 00:12:51 +0000373 return Out << Ty->getDescription();
Chris Lattnerb86620e2001-10-29 16:37:48 +0000374
375 // Check to see if the type is named.
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000376 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000377 if (I != TypeNames.end()) return Out << I->second;
378
379 // Otherwise we have a type that has not been named but is a derived type.
380 // Carefully recurse the type hierarchy to print out any contained symbolic
381 // names.
382 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000383 std::vector<const Type *> TypeStack;
John Criswellcd116ba2004-06-01 14:54:08 +0000384 std::string TypeName;
385 calcTypeName(Ty, TypeStack, TypeNames, TypeName);
Chris Lattner7f74a562002-01-20 22:54:45 +0000386 TypeNames.insert(std::make_pair(Ty, TypeName));//Cache type name for later use
John Criswellcd116ba2004-06-01 14:54:08 +0000387 return (Out << TypeName);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000388}
389
Chris Lattner34b95182001-10-31 04:33:19 +0000390
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000391/// WriteTypeSymbolic - This attempts to write the specified type as a symbolic
392/// type, iff there is an entry in the modules symbol table for the specified
393/// type or one of it's component types. This is slower than a simple x << Type
394///
Chris Lattner189d19f2003-11-21 20:23:48 +0000395std::ostream &llvm::WriteTypeSymbolic(std::ostream &Out, const Type *Ty,
396 const Module *M) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000397 Out << ' ';
Chris Lattnerb86620e2001-10-29 16:37:48 +0000398
Chris Lattnerfc9f1c92006-12-06 06:40:49 +0000399 // If they want us to print out a type, but there is no context, we can't
400 // print it symbolically.
401 if (!M)
Chris Lattner72f866e2001-10-29 16:40:32 +0000402 return Out << Ty->getDescription();
Chris Lattnerfc9f1c92006-12-06 06:40:49 +0000403
404 std::map<const Type *, std::string> TypeNames;
405 fillTypeNameTable(M, TypeNames);
406 return printTypeInt(Out, Ty, TypeNames);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000407}
408
Chris Lattner6ed87bd2006-01-23 23:03:36 +0000409// PrintEscapedString - Print each character of the specified string, escaping
410// it if it is not printable or if it is an escape char.
411static void PrintEscapedString(const std::string &Str, std::ostream &Out) {
412 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
413 unsigned char C = Str[i];
414 if (isprint(C) && C != '"' && C != '\\') {
415 Out << C;
416 } else {
417 Out << '\\'
418 << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'))
419 << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
420 }
421 }
422}
423
Chris Lattnerfc9f1c92006-12-06 06:40:49 +0000424static const char *getPredicateText(unsigned predicate) {
Reid Spencer812a1be2006-12-04 05:19:18 +0000425 const char * pred = "unknown";
426 switch (predicate) {
427 case FCmpInst::FCMP_FALSE: pred = "false"; break;
428 case FCmpInst::FCMP_OEQ: pred = "oeq"; break;
429 case FCmpInst::FCMP_OGT: pred = "ogt"; break;
430 case FCmpInst::FCMP_OGE: pred = "oge"; break;
431 case FCmpInst::FCMP_OLT: pred = "olt"; break;
432 case FCmpInst::FCMP_OLE: pred = "ole"; break;
433 case FCmpInst::FCMP_ONE: pred = "one"; break;
434 case FCmpInst::FCMP_ORD: pred = "ord"; break;
435 case FCmpInst::FCMP_UNO: pred = "uno"; break;
436 case FCmpInst::FCMP_UEQ: pred = "ueq"; break;
437 case FCmpInst::FCMP_UGT: pred = "ugt"; break;
438 case FCmpInst::FCMP_UGE: pred = "uge"; break;
439 case FCmpInst::FCMP_ULT: pred = "ult"; break;
440 case FCmpInst::FCMP_ULE: pred = "ule"; break;
441 case FCmpInst::FCMP_UNE: pred = "une"; break;
442 case FCmpInst::FCMP_TRUE: pred = "true"; break;
443 case ICmpInst::ICMP_EQ: pred = "eq"; break;
444 case ICmpInst::ICMP_NE: pred = "ne"; break;
445 case ICmpInst::ICMP_SGT: pred = "sgt"; break;
446 case ICmpInst::ICMP_SGE: pred = "sge"; break;
447 case ICmpInst::ICMP_SLT: pred = "slt"; break;
448 case ICmpInst::ICMP_SLE: pred = "sle"; break;
449 case ICmpInst::ICMP_UGT: pred = "ugt"; break;
450 case ICmpInst::ICMP_UGE: pred = "uge"; break;
451 case ICmpInst::ICMP_ULT: pred = "ult"; break;
452 case ICmpInst::ICMP_ULE: pred = "ule"; break;
453 }
454 return pred;
455}
456
Misha Brukmanb1c93172005-04-21 23:48:37 +0000457/// @brief Internal constant writer.
458static void WriteConstantInt(std::ostream &Out, const Constant *CV,
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000459 std::map<const Type *, std::string> &TypeTable,
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000460 SlotMachine *Machine) {
Jim Laskey6be3d8e2006-02-27 10:33:53 +0000461 const int IndentSize = 4;
462 static std::string Indent = "\n";
Zhou Sheng75b871f2007-01-11 12:24:14 +0000463 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Reid Spencer542964f2007-01-11 18:21:29 +0000464 if (CI->getType() == Type::Int1Ty)
Reid Spencercddc9df2007-01-12 04:24:46 +0000465 Out << (CI->getZExtValue() ? "true" : "false");
466 else
467 Out << CI->getSExtValue();
Chris Lattner1e194682002-04-18 18:53:13 +0000468 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
469 // We would like to output the FP constant value in exponential notation,
470 // but we cannot do this if doing so will lose precision. Check here to
471 // make sure that we only output it in exponential format if we can parse
472 // the value back and get the same value.
473 //
474 std::string StrVal = ftostr(CFP->getValue());
475
476 // Check to make sure that the stringized number is not some string like
477 // "Inf" or NaN, that atof will accept, but the lexer will not. Check that
478 // the string matches the "[-+]?[0-9]" regex.
479 //
480 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
481 ((StrVal[0] == '-' || StrVal[0] == '+') &&
Brian Gaeke87b4f072003-06-17 23:55:35 +0000482 (StrVal[1] >= '0' && StrVal[1] <= '9')))
Chris Lattner1e194682002-04-18 18:53:13 +0000483 // Reparse stringized version!
484 if (atof(StrVal.c_str()) == CFP->getValue()) {
Chris Lattner472cc102005-01-04 01:56:57 +0000485 Out << StrVal;
486 return;
Chris Lattner1e194682002-04-18 18:53:13 +0000487 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000488
Chris Lattner1e194682002-04-18 18:53:13 +0000489 // Otherwise we could not reparse it to exactly the same value, so we must
490 // output the string in hexadecimal format!
Chris Lattner472cc102005-01-04 01:56:57 +0000491 assert(sizeof(double) == sizeof(uint64_t) &&
Chris Lattner1e194682002-04-18 18:53:13 +0000492 "assuming that double is 64 bits!");
Jim Laskeyb74c6662005-08-17 19:34:49 +0000493 Out << "0x" << utohexstr(DoubleToBits(CFP->getValue()));
Chris Lattner1e194682002-04-18 18:53:13 +0000494
Chris Lattner76b2ff42004-02-15 05:55:15 +0000495 } else if (isa<ConstantAggregateZero>(CV)) {
496 Out << "zeroinitializer";
Chris Lattner1e194682002-04-18 18:53:13 +0000497 } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
498 // As a special case, print the array as a string if it is an array of
499 // ubytes or an array of sbytes with positive values.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000500 //
Chris Lattner1e194682002-04-18 18:53:13 +0000501 const Type *ETy = CA->getType()->getElementType();
Chris Lattner6ed87bd2006-01-23 23:03:36 +0000502 if (CA->isString()) {
Chris Lattner1e194682002-04-18 18:53:13 +0000503 Out << "c\"";
Chris Lattner6ed87bd2006-01-23 23:03:36 +0000504 PrintEscapedString(CA->getAsString(), Out);
Chris Lattner1e194682002-04-18 18:53:13 +0000505 Out << "\"";
506
507 } else { // Cannot output in string format...
Misha Brukman21bbdb92004-06-04 21:11:51 +0000508 Out << '[';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000509 if (CA->getNumOperands()) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000510 Out << ' ';
Chris Lattner1e194682002-04-18 18:53:13 +0000511 printTypeInt(Out, ETy, TypeTable);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000512 WriteAsOperandInternal(Out, CA->getOperand(0),
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000513 TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000514 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
515 Out << ", ";
Chris Lattner1e194682002-04-18 18:53:13 +0000516 printTypeInt(Out, ETy, TypeTable);
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000517 WriteAsOperandInternal(Out, CA->getOperand(i), TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000518 }
519 }
520 Out << " ]";
521 }
522 } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Andrew Lenharth0d124b82007-01-08 18:21:30 +0000523 if (CS->getType()->isPacked())
524 Out << '<';
Misha Brukman21bbdb92004-06-04 21:11:51 +0000525 Out << '{';
Jim Laskey3bb78742006-02-25 12:27:03 +0000526 unsigned N = CS->getNumOperands();
527 if (N) {
Jim Laskey6be3d8e2006-02-27 10:33:53 +0000528 if (N > 2) {
529 Indent += std::string(IndentSize, ' ');
530 Out << Indent;
531 } else {
532 Out << ' ';
533 }
Chris Lattnerd84bb632002-04-16 21:36:08 +0000534 printTypeInt(Out, CS->getOperand(0)->getType(), TypeTable);
535
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000536 WriteAsOperandInternal(Out, CS->getOperand(0), TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000537
Jim Laskey3bb78742006-02-25 12:27:03 +0000538 for (unsigned i = 1; i < N; i++) {
Chris Lattnerd84bb632002-04-16 21:36:08 +0000539 Out << ", ";
Jim Laskey6be3d8e2006-02-27 10:33:53 +0000540 if (N > 2) Out << Indent;
Chris Lattnerd84bb632002-04-16 21:36:08 +0000541 printTypeInt(Out, CS->getOperand(i)->getType(), TypeTable);
542
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000543 WriteAsOperandInternal(Out, CS->getOperand(i), TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000544 }
Jim Laskey6be3d8e2006-02-27 10:33:53 +0000545 if (N > 2) Indent.resize(Indent.size() - IndentSize);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000546 }
Jim Laskey3bb78742006-02-25 12:27:03 +0000547
Chris Lattnerd84bb632002-04-16 21:36:08 +0000548 Out << " }";
Andrew Lenharth0d124b82007-01-08 18:21:30 +0000549 if (CS->getType()->isPacked())
550 Out << '>';
Brian Gaeke02209042004-08-20 06:00:58 +0000551 } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(CV)) {
552 const Type *ETy = CP->getType()->getElementType();
Misha Brukmanb1c93172005-04-21 23:48:37 +0000553 assert(CP->getNumOperands() > 0 &&
Brian Gaeke02209042004-08-20 06:00:58 +0000554 "Number of operands for a PackedConst must be > 0");
555 Out << '<';
556 Out << ' ';
557 printTypeInt(Out, ETy, TypeTable);
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000558 WriteAsOperandInternal(Out, CP->getOperand(0), TypeTable, Machine);
Brian Gaeke02209042004-08-20 06:00:58 +0000559 for (unsigned i = 1, e = CP->getNumOperands(); i != e; ++i) {
560 Out << ", ";
561 printTypeInt(Out, ETy, TypeTable);
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000562 WriteAsOperandInternal(Out, CP->getOperand(i), TypeTable, Machine);
Brian Gaeke02209042004-08-20 06:00:58 +0000563 }
564 Out << " >";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000565 } else if (isa<ConstantPointerNull>(CV)) {
566 Out << "null";
567
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000568 } else if (isa<UndefValue>(CV)) {
569 Out << "undef";
570
Chris Lattner3cd8c562002-07-30 18:54:25 +0000571 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Reid Spencer812a1be2006-12-04 05:19:18 +0000572 Out << CE->getOpcodeName();
573 if (CE->isCompare())
574 Out << " " << getPredicateText(CE->getPredicate());
575 Out << " (";
Misha Brukmanb1c93172005-04-21 23:48:37 +0000576
Vikram S. Adveb952b542002-07-14 23:14:45 +0000577 for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
578 printTypeInt(Out, (*OI)->getType(), TypeTable);
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000579 WriteAsOperandInternal(Out, *OI, TypeTable, Machine);
Vikram S. Adveb952b542002-07-14 23:14:45 +0000580 if (OI+1 != CE->op_end())
Chris Lattner3cd8c562002-07-30 18:54:25 +0000581 Out << ", ";
Vikram S. Adveb952b542002-07-14 23:14:45 +0000582 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000583
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000584 if (CE->isCast()) {
Chris Lattner83b396b2002-08-15 19:37:43 +0000585 Out << " to ";
586 printTypeInt(Out, CE->getType(), TypeTable);
587 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000588
Misha Brukman21bbdb92004-06-04 21:11:51 +0000589 Out << ')';
Chris Lattner83b396b2002-08-15 19:37:43 +0000590
Chris Lattnerd84bb632002-04-16 21:36:08 +0000591 } else {
Vikram S. Adveb952b542002-07-14 23:14:45 +0000592 Out << "<placeholder or erroneous Constant>";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000593 }
594}
595
596
Misha Brukmanc566ca362004-03-02 00:22:19 +0000597/// WriteAsOperand - Write the name of the specified value out to the specified
598/// ostream. This can be useful when you just want to print int %reg126, not
599/// the whole instruction that generated it.
600///
Misha Brukmanb1c93172005-04-21 23:48:37 +0000601static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000602 std::map<const Type*, std::string> &TypeTable,
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000603 SlotMachine *Machine) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000604 Out << ' ';
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000605 if (V->hasName())
Reid Spencer788e3172007-01-26 08:02:52 +0000606 Out << getLLVMName(V->getName(),
607 isa<GlobalValue>(V) ? GlobalPrefix : LocalPrefix);
Reid Spenceraccd7c72004-07-17 23:47:01 +0000608 else {
609 const Constant *CV = dyn_cast<Constant>(V);
Chris Lattnera2d810d2006-01-25 22:26:05 +0000610 if (CV && !isa<GlobalValue>(CV)) {
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000611 WriteConstantInt(Out, CV, TypeTable, Machine);
Chris Lattnera2d810d2006-01-25 22:26:05 +0000612 } else if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
613 Out << "asm ";
614 if (IA->hasSideEffects())
615 Out << "sideeffect ";
616 Out << '"';
617 PrintEscapedString(IA->getAsmString(), Out);
618 Out << "\", \"";
619 PrintEscapedString(IA->getConstraintString(), Out);
620 Out << '"';
621 } else {
Reid Spencer788e3172007-01-26 08:02:52 +0000622 char Prefix = '%';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000623 int Slot;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000624 if (Machine) {
Reid Spencer788e3172007-01-26 08:02:52 +0000625 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattner5e043322007-01-11 03:54:27 +0000626 Slot = Machine->getGlobalSlot(GV);
Reid Spencer788e3172007-01-26 08:02:52 +0000627 Prefix = '@';
628 } else {
Chris Lattner5e043322007-01-11 03:54:27 +0000629 Slot = Machine->getLocalSlot(V);
Reid Spencer788e3172007-01-26 08:02:52 +0000630 }
Chris Lattnerd84bb632002-04-16 21:36:08 +0000631 } else {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000632 Machine = createSlotMachine(V);
Chris Lattner5e043322007-01-11 03:54:27 +0000633 if (Machine) {
Reid Spencer788e3172007-01-26 08:02:52 +0000634 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattner5e043322007-01-11 03:54:27 +0000635 Slot = Machine->getGlobalSlot(GV);
Reid Spencer788e3172007-01-26 08:02:52 +0000636 Prefix = '@';
637 } else {
Chris Lattner5e043322007-01-11 03:54:27 +0000638 Slot = Machine->getLocalSlot(V);
Reid Spencer788e3172007-01-26 08:02:52 +0000639 }
Chris Lattner5e043322007-01-11 03:54:27 +0000640 } else {
Chris Lattner757ee0b2004-06-09 19:41:19 +0000641 Slot = -1;
Chris Lattner5e043322007-01-11 03:54:27 +0000642 }
Reid Spencer56010e42004-05-26 21:56:09 +0000643 delete Machine;
Chris Lattnerd84bb632002-04-16 21:36:08 +0000644 }
Chris Lattner757ee0b2004-06-09 19:41:19 +0000645 if (Slot != -1)
Reid Spencer788e3172007-01-26 08:02:52 +0000646 Out << Prefix << Slot;
Chris Lattner757ee0b2004-06-09 19:41:19 +0000647 else
648 Out << "<badref>";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000649 }
650 }
651}
652
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000653/// WriteAsOperand - Write the name of the specified value out to the specified
654/// ostream. This can be useful when you just want to print int %reg126, not
655/// the whole instruction that generated it.
656///
Chris Lattner189d19f2003-11-21 20:23:48 +0000657std::ostream &llvm::WriteAsOperand(std::ostream &Out, const Value *V,
Chris Lattner0dda8612006-12-06 06:15:43 +0000658 bool PrintType, const Module *Context) {
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000659 std::map<const Type *, std::string> TypeNames;
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000660 if (Context == 0) Context = getModuleFromVal(V);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000661
Chris Lattner98cf1f52002-11-20 18:36:02 +0000662 if (Context)
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000663 fillTypeNameTable(Context, TypeNames);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000664
665 if (PrintType)
666 printTypeInt(Out, V->getType(), TypeNames);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000667
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000668 WriteAsOperandInternal(Out, V, TypeNames, 0);
Chris Lattner5e5abe32001-07-20 19:15:21 +0000669 return Out;
670}
671
Reid Spencer58d30f22004-07-04 11:50:43 +0000672
Chris Lattner189d19f2003-11-21 20:23:48 +0000673namespace llvm {
Chris Lattner2e9fee42001-07-12 23:35:26 +0000674
Chris Lattnerfee714f2001-09-07 16:36:04 +0000675class AssemblyWriter {
Misha Brukmana6619a92004-06-21 21:53:56 +0000676 std::ostream &Out;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000677 SlotMachine &Machine;
Chris Lattner7bfee412001-10-29 16:05:51 +0000678 const Module *TheModule;
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000679 std::map<const Type *, std::string> TypeNames;
Chris Lattner8339f7d2003-10-30 23:41:03 +0000680 AssemblyAnnotationWriter *AnnotationWriter;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000681public:
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000682 inline AssemblyWriter(std::ostream &o, SlotMachine &Mac, const Module *M,
Chris Lattner8339f7d2003-10-30 23:41:03 +0000683 AssemblyAnnotationWriter *AAW)
Misha Brukmana6619a92004-06-21 21:53:56 +0000684 : Out(o), Machine(Mac), TheModule(M), AnnotationWriter(AAW) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000685
686 // If the module has a symbol table, take all global types and stuff their
687 // names into the TypeNames map.
688 //
Chris Lattnerb86620e2001-10-29 16:37:48 +0000689 fillTypeNameTable(M, TypeNames);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000690 }
691
Chris Lattner7bfee412001-10-29 16:05:51 +0000692 inline void write(const Module *M) { printModule(M); }
693 inline void write(const GlobalVariable *G) { printGlobal(G); }
Chris Lattner57698e22002-03-26 18:01:55 +0000694 inline void write(const Function *F) { printFunction(F); }
Chris Lattner7bfee412001-10-29 16:05:51 +0000695 inline void write(const BasicBlock *BB) { printBasicBlock(BB); }
Chris Lattner113f4f42002-06-25 16:13:24 +0000696 inline void write(const Instruction *I) { printInstruction(*I); }
Chris Lattner7db79582001-11-07 04:21:57 +0000697 inline void write(const Type *Ty) { printType(Ty); }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000698
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000699 void writeOperand(const Value *Op, bool PrintType);
Chris Lattner1e194682002-04-18 18:53:13 +0000700
Misha Brukman4685e262004-04-28 15:31:21 +0000701 const Module* getModule() { return TheModule; }
702
Misha Brukmand92f54a2004-11-15 19:30:05 +0000703private:
Chris Lattner7bfee412001-10-29 16:05:51 +0000704 void printModule(const Module *M);
Reid Spencer32af9e82007-01-06 07:24:44 +0000705 void printTypeSymbolTable(const TypeSymbolTable &ST);
Chris Lattner7bfee412001-10-29 16:05:51 +0000706 void printGlobal(const GlobalVariable *GV);
Chris Lattner57698e22002-03-26 18:01:55 +0000707 void printFunction(const Function *F);
Reid Spencer8c4914c2006-12-31 05:24:50 +0000708 void printArgument(const Argument *FA, FunctionType::ParameterAttributes A);
Chris Lattner7bfee412001-10-29 16:05:51 +0000709 void printBasicBlock(const BasicBlock *BB);
Chris Lattner113f4f42002-06-25 16:13:24 +0000710 void printInstruction(const Instruction &I);
Chris Lattnerd816b532002-04-13 20:53:41 +0000711
712 // printType - Go to extreme measures to attempt to print out a short,
713 // symbolic version of a type name.
714 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000715 std::ostream &printType(const Type *Ty) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000716 return printTypeInt(Out, Ty, TypeNames);
Chris Lattnerd816b532002-04-13 20:53:41 +0000717 }
718
719 // printTypeAtLeastOneLevel - Print out one level of the possibly complex type
720 // without considering any symbolic types that we may have equal to it.
721 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000722 std::ostream &printTypeAtLeastOneLevel(const Type *Ty);
Chris Lattner7bfee412001-10-29 16:05:51 +0000723
Chris Lattner862e3382001-10-13 06:42:36 +0000724 // printInfoComment - Print a little comment after the instruction indicating
725 // which slot it occupies.
Chris Lattner113f4f42002-06-25 16:13:24 +0000726 void printInfoComment(const Value &V);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000727};
Reid Spencerf43ac622004-05-27 22:04:46 +0000728} // end of llvm namespace
Chris Lattner2f7c9632001-06-06 20:29:01 +0000729
Misha Brukmanc566ca362004-03-02 00:22:19 +0000730/// printTypeAtLeastOneLevel - Print out one level of the possibly complex type
731/// without considering any symbolic types that we may have equal to it.
732///
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000733std::ostream &AssemblyWriter::printTypeAtLeastOneLevel(const Type *Ty) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000734 if (const IntegerType *ITy = dyn_cast<IntegerType>(Ty))
735 Out << "i" << utostr(ITy->getBitWidth());
736 else if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
Reid Spencer8c4914c2006-12-31 05:24:50 +0000737 printType(FTy->getReturnType());
Reid Spencer8c4914c2006-12-31 05:24:50 +0000738 Out << " (";
739 unsigned Idx = 1;
Chris Lattnerfa829be2004-02-09 04:14:01 +0000740 for (FunctionType::param_iterator I = FTy->param_begin(),
741 E = FTy->param_end(); I != E; ++I) {
742 if (I != FTy->param_begin())
Misha Brukmana6619a92004-06-21 21:53:56 +0000743 Out << ", ";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000744 printType(*I);
Reid Spencer8c4914c2006-12-31 05:24:50 +0000745 if (FTy->getParamAttrs(Idx)) {
746 Out << " " << FunctionType::getParamAttrsText(FTy->getParamAttrs(Idx));
747 }
748 Idx++;
Chris Lattnerd816b532002-04-13 20:53:41 +0000749 }
750 if (FTy->isVarArg()) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000751 if (FTy->getNumParams()) Out << ", ";
752 Out << "...";
Chris Lattnerd816b532002-04-13 20:53:41 +0000753 }
Misha Brukmana6619a92004-06-21 21:53:56 +0000754 Out << ')';
Reid Spencer136a91c2007-01-05 17:06:19 +0000755 if (FTy->getParamAttrs(0))
756 Out << ' ' << FunctionType::getParamAttrsText(FTy->getParamAttrs(0));
Chris Lattner113f4f42002-06-25 16:13:24 +0000757 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
Andrew Lenharthdcb3c972006-12-08 18:06:16 +0000758 if (STy->isPacked())
759 Out << '<';
Misha Brukmana6619a92004-06-21 21:53:56 +0000760 Out << "{ ";
Chris Lattnerac6db752004-02-09 04:37:31 +0000761 for (StructType::element_iterator I = STy->element_begin(),
762 E = STy->element_end(); I != E; ++I) {
763 if (I != STy->element_begin())
Misha Brukmana6619a92004-06-21 21:53:56 +0000764 Out << ", ";
Chris Lattnerd816b532002-04-13 20:53:41 +0000765 printType(*I);
766 }
Misha Brukmana6619a92004-06-21 21:53:56 +0000767 Out << " }";
Andrew Lenharthdcb3c972006-12-08 18:06:16 +0000768 if (STy->isPacked())
769 Out << '>';
Chris Lattner113f4f42002-06-25 16:13:24 +0000770 } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000771 printType(PTy->getElementType()) << '*';
Chris Lattner113f4f42002-06-25 16:13:24 +0000772 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000773 Out << '[' << ATy->getNumElements() << " x ";
Misha Brukman21bbdb92004-06-04 21:11:51 +0000774 printType(ATy->getElementType()) << ']';
Reid Spencere203d352004-08-20 15:37:30 +0000775 } else if (const PackedType *PTy = dyn_cast<PackedType>(Ty)) {
776 Out << '<' << PTy->getNumElements() << " x ";
777 printType(PTy->getElementType()) << '>';
778 }
Reid Spencerde46e482006-11-02 20:25:50 +0000779 else if (isa<OpaqueType>(Ty)) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000780 Out << "opaque";
Chris Lattnerd816b532002-04-13 20:53:41 +0000781 } else {
Vikram S. Adveb952b542002-07-14 23:14:45 +0000782 if (!Ty->isPrimitiveType())
Misha Brukmana6619a92004-06-21 21:53:56 +0000783 Out << "<unknown derived type>";
Chris Lattnerd816b532002-04-13 20:53:41 +0000784 printType(Ty);
785 }
Misha Brukmana6619a92004-06-21 21:53:56 +0000786 return Out;
Chris Lattnerd816b532002-04-13 20:53:41 +0000787}
788
789
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000790void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType) {
791 if (Operand == 0) {
Chris Lattner08f7d0c2005-02-24 16:58:29 +0000792 Out << "<null operand!>";
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000793 } else {
794 if (PrintType) { Out << ' '; printType(Operand->getType()); }
795 WriteAsOperandInternal(Out, Operand, TypeNames, &Machine);
Chris Lattner08f7d0c2005-02-24 16:58:29 +0000796 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000797}
798
Chris Lattner2f7c9632001-06-06 20:29:01 +0000799
Chris Lattner7bfee412001-10-29 16:05:51 +0000800void AssemblyWriter::printModule(const Module *M) {
Chris Lattner4d8689e2005-03-02 23:12:40 +0000801 if (!M->getModuleIdentifier().empty() &&
Misha Brukmanb1c93172005-04-21 23:48:37 +0000802 // Don't print the ID if it will start a new line (which would
Chris Lattner4d8689e2005-03-02 23:12:40 +0000803 // require a comment char before it).
804 M->getModuleIdentifier().find('\n') == std::string::npos)
805 Out << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
806
Owen Andersone2237542006-10-18 02:21:12 +0000807 if (!M->getDataLayout().empty())
Chris Lattner04897162006-10-22 06:06:56 +0000808 Out << "target datalayout = \"" << M->getDataLayout() << "\"\n";
Reid Spencer48f98c82004-07-25 21:44:54 +0000809 if (!M->getTargetTriple().empty())
Reid Spencerffec7df2004-07-25 21:29:43 +0000810 Out << "target triple = \"" << M->getTargetTriple() << "\"\n";
Misha Brukmanb1c93172005-04-21 23:48:37 +0000811
Chris Lattnereef2fe72006-01-24 04:13:11 +0000812 if (!M->getModuleInlineAsm().empty()) {
Chris Lattnerefaf35d2006-01-24 00:45:30 +0000813 // Split the string into lines, to make it easier to read the .ll file.
Chris Lattnereef2fe72006-01-24 04:13:11 +0000814 std::string Asm = M->getModuleInlineAsm();
Chris Lattnerefaf35d2006-01-24 00:45:30 +0000815 size_t CurPos = 0;
816 size_t NewLine = Asm.find_first_of('\n', CurPos);
817 while (NewLine != std::string::npos) {
818 // We found a newline, print the portion of the asm string from the
819 // last newline up to this newline.
820 Out << "module asm \"";
821 PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.begin()+NewLine),
822 Out);
823 Out << "\"\n";
824 CurPos = NewLine+1;
825 NewLine = Asm.find_first_of('\n', CurPos);
826 }
Chris Lattner3acaf5c2006-01-24 00:40:17 +0000827 Out << "module asm \"";
Chris Lattnerefaf35d2006-01-24 00:45:30 +0000828 PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.end()), Out);
Chris Lattner6ed87bd2006-01-23 23:03:36 +0000829 Out << "\"\n";
830 }
831
Chris Lattner2cdd49d2004-09-14 05:06:58 +0000832 // Loop over the dependent libraries and emit them.
Chris Lattner730cfe42004-09-14 04:51:44 +0000833 Module::lib_iterator LI = M->lib_begin();
834 Module::lib_iterator LE = M->lib_end();
Reid Spencer48f98c82004-07-25 21:44:54 +0000835 if (LI != LE) {
Chris Lattner730cfe42004-09-14 04:51:44 +0000836 Out << "deplibs = [ ";
837 while (LI != LE) {
Chris Lattner2cdd49d2004-09-14 05:06:58 +0000838 Out << '"' << *LI << '"';
Reid Spencerffec7df2004-07-25 21:29:43 +0000839 ++LI;
Chris Lattner730cfe42004-09-14 04:51:44 +0000840 if (LI != LE)
841 Out << ", ";
Reid Spencerffec7df2004-07-25 21:29:43 +0000842 }
843 Out << " ]\n";
Reid Spencercc5ff642004-07-25 18:08:18 +0000844 }
Reid Spencerb9e08772004-09-13 23:44:23 +0000845
Chris Lattner2cdd49d2004-09-14 05:06:58 +0000846 // Loop over the symbol table, emitting all named constants.
Reid Spencer32af9e82007-01-06 07:24:44 +0000847 printTypeSymbolTable(M->getTypeSymbolTable());
Misha Brukmanb1c93172005-04-21 23:48:37 +0000848
Chris Lattner54932b02006-12-06 04:41:52 +0000849 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
850 I != E; ++I)
Chris Lattner113f4f42002-06-25 16:13:24 +0000851 printGlobal(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000852
Misha Brukmana6619a92004-06-21 21:53:56 +0000853 Out << "\nimplementation ; Functions:\n";
Misha Brukmanb1c93172005-04-21 23:48:37 +0000854
Chris Lattner2cdd49d2004-09-14 05:06:58 +0000855 // Output all of the functions.
Chris Lattner113f4f42002-06-25 16:13:24 +0000856 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
857 printFunction(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000858}
859
Chris Lattner7bfee412001-10-29 16:05:51 +0000860void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Reid Spencer788e3172007-01-26 08:02:52 +0000861 if (GV->hasName()) Out << getLLVMName(GV->getName(), GlobalPrefix) << " = ";
Chris Lattner37798642001-09-18 04:01:05 +0000862
Misha Brukmanb1c93172005-04-21 23:48:37 +0000863 if (!GV->hasInitializer())
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000864 switch (GV->getLinkage()) {
865 case GlobalValue::DLLImportLinkage: Out << "dllimport "; break;
866 case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break;
867 default: Out << "external "; break;
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000868 } else {
Chris Lattner379a8d22003-04-16 20:28:45 +0000869 switch (GV->getLinkage()) {
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000870 case GlobalValue::InternalLinkage: Out << "internal "; break;
871 case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
872 case GlobalValue::WeakLinkage: Out << "weak "; break;
873 case GlobalValue::AppendingLinkage: Out << "appending "; break;
874 case GlobalValue::DLLImportLinkage: Out << "dllimport "; break;
875 case GlobalValue::DLLExportLinkage: Out << "dllexport "; break;
876 case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break;
877 case GlobalValue::ExternalLinkage: break;
Misha Brukman70457632004-11-14 21:04:34 +0000878 case GlobalValue::GhostLinkage:
Bill Wendlingf3baad32006-12-07 01:30:32 +0000879 cerr << "GhostLinkage not allowed in AsmWriter!\n";
Misha Brukman70457632004-11-14 21:04:34 +0000880 abort();
Chris Lattner379a8d22003-04-16 20:28:45 +0000881 }
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000882 switch (GV->getVisibility()) {
Chris Lattner90d2e422007-01-15 18:28:18 +0000883 default: assert(0 && "Invalid visibility style!");
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000884 case GlobalValue::DefaultVisibility: break;
885 case GlobalValue::HiddenVisibility: Out << "hidden "; break;
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000886 }
887 }
888
Misha Brukmana6619a92004-06-21 21:53:56 +0000889 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattner2413b162001-12-04 00:03:30 +0000890 printType(GV->getType()->getElementType());
Chris Lattner37798642001-09-18 04:01:05 +0000891
Reid Spenceraccd7c72004-07-17 23:47:01 +0000892 if (GV->hasInitializer()) {
893 Constant* C = cast<Constant>(GV->getInitializer());
894 assert(C && "GlobalVar initializer isn't constant?");
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000895 writeOperand(GV->getInitializer(), false);
Reid Spenceraccd7c72004-07-17 23:47:01 +0000896 }
Chris Lattnerf8a974d2005-11-06 06:48:53 +0000897
Chris Lattner4b96c542005-11-12 00:10:19 +0000898 if (GV->hasSection())
899 Out << ", section \"" << GV->getSection() << '"';
900 if (GV->getAlignment())
Chris Lattnerf8a974d2005-11-06 06:48:53 +0000901 Out << ", align " << GV->getAlignment();
Chris Lattner4b96c542005-11-12 00:10:19 +0000902
Chris Lattner113f4f42002-06-25 16:13:24 +0000903 printInfoComment(*GV);
Misha Brukmana6619a92004-06-21 21:53:56 +0000904 Out << "\n";
Chris Lattnerda975502001-09-10 07:58:01 +0000905}
906
Reid Spencer32af9e82007-01-06 07:24:44 +0000907void AssemblyWriter::printTypeSymbolTable(const TypeSymbolTable &ST) {
Reid Spencere7e96712004-05-25 08:53:40 +0000908 // Print the types.
Reid Spencer32af9e82007-01-06 07:24:44 +0000909 for (TypeSymbolTable::const_iterator TI = ST.begin(), TE = ST.end();
910 TI != TE; ++TI) {
Reid Spencer788e3172007-01-26 08:02:52 +0000911 Out << "\t" << getLLVMName(TI->first, LocalPrefix) << " = type ";
Reid Spencere7e96712004-05-25 08:53:40 +0000912
913 // Make sure we print out at least one level of the type structure, so
914 // that we do not get %FILE = type %FILE
915 //
916 printTypeAtLeastOneLevel(TI->second) << "\n";
917 }
Reid Spencer32af9e82007-01-06 07:24:44 +0000918}
919
Misha Brukmanc566ca362004-03-02 00:22:19 +0000920/// printFunction - Print all aspects of a function.
921///
Chris Lattner113f4f42002-06-25 16:13:24 +0000922void AssemblyWriter::printFunction(const Function *F) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000923 // Print out the return type and name...
Misha Brukmana6619a92004-06-21 21:53:56 +0000924 Out << "\n";
Chris Lattner379a8d22003-04-16 20:28:45 +0000925
Misha Brukmana6619a92004-06-21 21:53:56 +0000926 if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +0000927
Reid Spencer5301e7c2007-01-30 20:08:39 +0000928 if (F->isDeclaration())
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000929 switch (F->getLinkage()) {
930 case GlobalValue::DLLImportLinkage: Out << "declare dllimport "; break;
931 case GlobalValue::ExternalWeakLinkage: Out << "declare extern_weak "; break;
932 default: Out << "declare ";
933 }
Reid Spencer7ce2d2a2006-12-29 20:29:48 +0000934 else {
935 Out << "define ";
Chris Lattner379a8d22003-04-16 20:28:45 +0000936 switch (F->getLinkage()) {
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000937 case GlobalValue::InternalLinkage: Out << "internal "; break;
938 case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
939 case GlobalValue::WeakLinkage: Out << "weak "; break;
940 case GlobalValue::AppendingLinkage: Out << "appending "; break;
941 case GlobalValue::DLLImportLinkage: Out << "dllimport "; break;
942 case GlobalValue::DLLExportLinkage: Out << "dllexport "; break;
943 case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break;
Chris Lattner379a8d22003-04-16 20:28:45 +0000944 case GlobalValue::ExternalLinkage: break;
Misha Brukman70457632004-11-14 21:04:34 +0000945 case GlobalValue::GhostLinkage:
Bill Wendlingf3baad32006-12-07 01:30:32 +0000946 cerr << "GhostLinkage not allowed in AsmWriter!\n";
Misha Brukman70457632004-11-14 21:04:34 +0000947 abort();
Chris Lattner379a8d22003-04-16 20:28:45 +0000948 }
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000949 switch (F->getVisibility()) {
Chris Lattner90d2e422007-01-15 18:28:18 +0000950 default: assert(0 && "Invalid visibility style!");
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000951 case GlobalValue::DefaultVisibility: break;
952 case GlobalValue::HiddenVisibility: Out << "hidden "; break;
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000953 }
Reid Spencer7ce2d2a2006-12-29 20:29:48 +0000954 }
Chris Lattner379a8d22003-04-16 20:28:45 +0000955
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000956 // Print the calling convention.
957 switch (F->getCallingConv()) {
958 case CallingConv::C: break; // default
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +0000959 case CallingConv::Fast: Out << "fastcc "; break;
960 case CallingConv::Cold: Out << "coldcc "; break;
961 case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break;
962 case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break;
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000963 default: Out << "cc" << F->getCallingConv() << " "; break;
964 }
965
Reid Spencer8c4914c2006-12-31 05:24:50 +0000966 const FunctionType *FT = F->getFunctionType();
Misha Brukman21bbdb92004-06-04 21:11:51 +0000967 printType(F->getReturnType()) << ' ';
Chris Lattner5b337482003-10-18 05:57:43 +0000968 if (!F->getName().empty())
Reid Spencer788e3172007-01-26 08:02:52 +0000969 Out << getLLVMName(F->getName(), GlobalPrefix);
Chris Lattner5b337482003-10-18 05:57:43 +0000970 else
Reid Spencer788e3172007-01-26 08:02:52 +0000971 Out << "@\"\"";
Misha Brukmana6619a92004-06-21 21:53:56 +0000972 Out << '(';
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000973 Machine.incorporateFunction(F);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000974
Chris Lattner7bfee412001-10-29 16:05:51 +0000975 // Loop over the arguments, printing them...
Chris Lattnerfee714f2001-09-07 16:36:04 +0000976
Reid Spencer8c4914c2006-12-31 05:24:50 +0000977 unsigned Idx = 1;
Chris Lattner54932b02006-12-06 04:41:52 +0000978 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
Reid Spencer8c4914c2006-12-31 05:24:50 +0000979 I != E; ++I) {
980 // Insert commas as we go... the first arg doesn't get a comma
981 if (I != F->arg_begin()) Out << ", ";
982 printArgument(I, FT->getParamAttrs(Idx));
983 Idx++;
984 }
Chris Lattnerfee714f2001-09-07 16:36:04 +0000985
986 // Finish printing arguments...
Chris Lattner113f4f42002-06-25 16:13:24 +0000987 if (FT->isVarArg()) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000988 if (FT->getNumParams()) Out << ", ";
989 Out << "..."; // Output varargs portion of signature!
Chris Lattnerfee714f2001-09-07 16:36:04 +0000990 }
Misha Brukmana6619a92004-06-21 21:53:56 +0000991 Out << ')';
Reid Spencer136a91c2007-01-05 17:06:19 +0000992 if (FT->getParamAttrs(0))
993 Out << ' ' << FunctionType::getParamAttrsText(FT->getParamAttrs(0));
Chris Lattner4b96c542005-11-12 00:10:19 +0000994 if (F->hasSection())
995 Out << " section \"" << F->getSection() << '"';
Chris Lattnerf8a974d2005-11-06 06:48:53 +0000996 if (F->getAlignment())
997 Out << " align " << F->getAlignment();
Chris Lattner4b96c542005-11-12 00:10:19 +0000998
Reid Spencer5301e7c2007-01-30 20:08:39 +0000999 if (F->isDeclaration()) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001000 Out << "\n";
Chris Lattnerb2f02e52002-05-06 03:00:40 +00001001 } else {
Misha Brukmana6619a92004-06-21 21:53:56 +00001002 Out << " {";
Misha Brukmanb1c93172005-04-21 23:48:37 +00001003
Chris Lattner6915f8f2002-04-07 22:49:37 +00001004 // Output all of its basic blocks... for the function
Chris Lattner113f4f42002-06-25 16:13:24 +00001005 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
1006 printBasicBlock(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +00001007
Misha Brukmana6619a92004-06-21 21:53:56 +00001008 Out << "}\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +00001009 }
1010
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001011 Machine.purgeFunction();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001012}
1013
Misha Brukmanc566ca362004-03-02 00:22:19 +00001014/// printArgument - This member is called for every argument that is passed into
1015/// the function. Simply print it out
1016///
Reid Spencer8c4914c2006-12-31 05:24:50 +00001017void AssemblyWriter::printArgument(const Argument *Arg,
1018 FunctionType::ParameterAttributes attrs) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001019 // Output type...
Chris Lattner7bfee412001-10-29 16:05:51 +00001020 printType(Arg->getType());
Misha Brukmanb1c93172005-04-21 23:48:37 +00001021
Reid Spencer8c4914c2006-12-31 05:24:50 +00001022 if (attrs != FunctionType::NoAttributeSet)
1023 Out << ' ' << FunctionType::getParamAttrsText(attrs);
1024
Chris Lattner2f7c9632001-06-06 20:29:01 +00001025 // Output name, if available...
1026 if (Arg->hasName())
Reid Spencer788e3172007-01-26 08:02:52 +00001027 Out << ' ' << getLLVMName(Arg->getName(), LocalPrefix);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001028}
1029
Misha Brukmanc566ca362004-03-02 00:22:19 +00001030/// printBasicBlock - This member is called for each basic block in a method.
1031///
Chris Lattner7bfee412001-10-29 16:05:51 +00001032void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001033 if (BB->hasName()) { // Print out the label if it exists...
Reid Spencer788e3172007-01-26 08:02:52 +00001034 Out << "\n" << getLLVMName(BB->getName(), LabelPrefix) << ':';
Chris Lattner408dbdb2002-05-14 16:02:05 +00001035 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Misha Brukmana6619a92004-06-21 21:53:56 +00001036 Out << "\n; <label>:";
Chris Lattner5e043322007-01-11 03:54:27 +00001037 int Slot = Machine.getLocalSlot(BB);
Chris Lattner757ee0b2004-06-09 19:41:19 +00001038 if (Slot != -1)
Misha Brukmana6619a92004-06-21 21:53:56 +00001039 Out << Slot;
Chris Lattner757ee0b2004-06-09 19:41:19 +00001040 else
Misha Brukmana6619a92004-06-21 21:53:56 +00001041 Out << "<badref>";
Chris Lattner58185f22002-10-02 19:38:55 +00001042 }
Chris Lattner2447ef52003-11-20 00:09:43 +00001043
1044 if (BB->getParent() == 0)
Misha Brukmana6619a92004-06-21 21:53:56 +00001045 Out << "\t\t; Error: Block without parent!";
Chris Lattner2447ef52003-11-20 00:09:43 +00001046 else {
1047 if (BB != &BB->getParent()->front()) { // Not the entry block?
1048 // Output predecessors for the block...
Misha Brukmana6619a92004-06-21 21:53:56 +00001049 Out << "\t\t;";
Chris Lattner2447ef52003-11-20 00:09:43 +00001050 pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
Misha Brukmanb1c93172005-04-21 23:48:37 +00001051
Chris Lattner2447ef52003-11-20 00:09:43 +00001052 if (PI == PE) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001053 Out << " No predecessors!";
Chris Lattner2447ef52003-11-20 00:09:43 +00001054 } else {
Misha Brukmana6619a92004-06-21 21:53:56 +00001055 Out << " preds =";
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001056 writeOperand(*PI, false);
Chris Lattner2447ef52003-11-20 00:09:43 +00001057 for (++PI; PI != PE; ++PI) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001058 Out << ',';
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001059 writeOperand(*PI, false);
Chris Lattner2447ef52003-11-20 00:09:43 +00001060 }
Chris Lattner00211f12003-11-16 22:59:57 +00001061 }
Chris Lattner58185f22002-10-02 19:38:55 +00001062 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001063 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001064
Misha Brukmana6619a92004-06-21 21:53:56 +00001065 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001066
Misha Brukmana6619a92004-06-21 21:53:56 +00001067 if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001068
Chris Lattnerfee714f2001-09-07 16:36:04 +00001069 // Output all of the instructions in the basic block...
Chris Lattner113f4f42002-06-25 16:13:24 +00001070 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
1071 printInstruction(*I);
Chris Lattner96cdd272004-03-08 18:51:45 +00001072
Misha Brukmana6619a92004-06-21 21:53:56 +00001073 if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001074}
1075
Chris Lattner862e3382001-10-13 06:42:36 +00001076
Misha Brukmanc566ca362004-03-02 00:22:19 +00001077/// printInfoComment - Print a little comment after the instruction indicating
1078/// which slot it occupies.
1079///
Chris Lattner113f4f42002-06-25 16:13:24 +00001080void AssemblyWriter::printInfoComment(const Value &V) {
1081 if (V.getType() != Type::VoidTy) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001082 Out << "\t\t; <";
Misha Brukman21bbdb92004-06-04 21:11:51 +00001083 printType(V.getType()) << '>';
Chris Lattner862e3382001-10-13 06:42:36 +00001084
Chris Lattner113f4f42002-06-25 16:13:24 +00001085 if (!V.hasName()) {
Chris Lattner5e043322007-01-11 03:54:27 +00001086 int SlotNum;
1087 if (const GlobalValue *GV = dyn_cast<GlobalValue>(&V))
1088 SlotNum = Machine.getGlobalSlot(GV);
1089 else
1090 SlotNum = Machine.getLocalSlot(&V);
Chris Lattner757ee0b2004-06-09 19:41:19 +00001091 if (SlotNum == -1)
Misha Brukmana6619a92004-06-21 21:53:56 +00001092 Out << ":<badref>";
Reid Spencer8beac692004-06-09 15:26:53 +00001093 else
Misha Brukmana6619a92004-06-21 21:53:56 +00001094 Out << ':' << SlotNum; // Print out the def slot taken.
Chris Lattner862e3382001-10-13 06:42:36 +00001095 }
Chris Lattnerb6c21db2005-02-01 01:24:01 +00001096 Out << " [#uses=" << V.getNumUses() << ']'; // Output # uses
Chris Lattner862e3382001-10-13 06:42:36 +00001097 }
1098}
1099
Reid Spencere7141c82006-08-28 01:02:49 +00001100// This member is called for each Instruction in a function..
Chris Lattner113f4f42002-06-25 16:13:24 +00001101void AssemblyWriter::printInstruction(const Instruction &I) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001102 if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001103
Misha Brukmana6619a92004-06-21 21:53:56 +00001104 Out << "\t";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001105
1106 // Print out name if it exists...
Chris Lattner113f4f42002-06-25 16:13:24 +00001107 if (I.hasName())
Reid Spencer788e3172007-01-26 08:02:52 +00001108 Out << getLLVMName(I.getName(), LocalPrefix) << " = ";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001109
Chris Lattner06038452005-05-06 05:51:46 +00001110 // If this is a volatile load or store, print out the volatile marker.
Chris Lattner504f9242003-09-08 17:45:59 +00001111 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
Chris Lattner06038452005-05-06 05:51:46 +00001112 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile())) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001113 Out << "volatile ";
Chris Lattner06038452005-05-06 05:51:46 +00001114 } else if (isa<CallInst>(I) && cast<CallInst>(I).isTailCall()) {
1115 // If this is a call, check if it's a tail call.
1116 Out << "tail ";
1117 }
Chris Lattner504f9242003-09-08 17:45:59 +00001118
Chris Lattner2f7c9632001-06-06 20:29:01 +00001119 // Print out the opcode...
Misha Brukmana6619a92004-06-21 21:53:56 +00001120 Out << I.getOpcodeName();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001121
Reid Spencer45e52392006-12-03 06:27:29 +00001122 // Print out the compare instruction predicates
1123 if (const FCmpInst *FCI = dyn_cast<FCmpInst>(&I)) {
Reid Spencer812a1be2006-12-04 05:19:18 +00001124 Out << " " << getPredicateText(FCI->getPredicate());
Reid Spencer45e52392006-12-03 06:27:29 +00001125 } else if (const ICmpInst *ICI = dyn_cast<ICmpInst>(&I)) {
Reid Spencer812a1be2006-12-04 05:19:18 +00001126 Out << " " << getPredicateText(ICI->getPredicate());
Reid Spencer45e52392006-12-03 06:27:29 +00001127 }
1128
Chris Lattner2f7c9632001-06-06 20:29:01 +00001129 // Print out the type of the operands...
Chris Lattner113f4f42002-06-25 16:13:24 +00001130 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001131
1132 // Special case conditional branches to swizzle the condition out to the front
Chris Lattner113f4f42002-06-25 16:13:24 +00001133 if (isa<BranchInst>(I) && I.getNumOperands() > 1) {
1134 writeOperand(I.getOperand(2), true);
Misha Brukmana6619a92004-06-21 21:53:56 +00001135 Out << ',';
Chris Lattner2f7c9632001-06-06 20:29:01 +00001136 writeOperand(Operand, true);
Misha Brukmana6619a92004-06-21 21:53:56 +00001137 Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001138 writeOperand(I.getOperand(1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001139
Chris Lattner8d48df22002-04-13 18:34:38 +00001140 } else if (isa<SwitchInst>(I)) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001141 // Special case switch statement to get formatting nice and correct...
Misha Brukmana6619a92004-06-21 21:53:56 +00001142 writeOperand(Operand , true); Out << ',';
1143 writeOperand(I.getOperand(1), true); Out << " [";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001144
Chris Lattner113f4f42002-06-25 16:13:24 +00001145 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001146 Out << "\n\t\t";
1147 writeOperand(I.getOperand(op ), true); Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001148 writeOperand(I.getOperand(op+1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001149 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001150 Out << "\n\t]";
Chris Lattnerda558102001-10-02 03:41:24 +00001151 } else if (isa<PHINode>(I)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001152 Out << ' ';
Chris Lattner113f4f42002-06-25 16:13:24 +00001153 printType(I.getType());
Misha Brukmana6619a92004-06-21 21:53:56 +00001154 Out << ' ';
Chris Lattner2f7c9632001-06-06 20:29:01 +00001155
Chris Lattner113f4f42002-06-25 16:13:24 +00001156 for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001157 if (op) Out << ", ";
Misha Brukmanb1c93172005-04-21 23:48:37 +00001158 Out << '[';
Misha Brukmana6619a92004-06-21 21:53:56 +00001159 writeOperand(I.getOperand(op ), false); Out << ',';
1160 writeOperand(I.getOperand(op+1), false); Out << " ]";
Chris Lattner931ef3b2001-06-11 15:04:20 +00001161 }
Chris Lattner862e3382001-10-13 06:42:36 +00001162 } else if (isa<ReturnInst>(I) && !Operand) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001163 Out << " void";
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001164 } else if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
1165 // Print the calling convention being used.
1166 switch (CI->getCallingConv()) {
1167 case CallingConv::C: break; // default
Chris Lattner29d20852006-05-19 21:58:52 +00001168 case CallingConv::Fast: Out << " fastcc"; break;
1169 case CallingConv::Cold: Out << " coldcc"; break;
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +00001170 case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break;
1171 case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break;
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001172 default: Out << " cc" << CI->getCallingConv(); break;
1173 }
1174
Chris Lattner463d6a52003-08-05 15:34:45 +00001175 const PointerType *PTy = cast<PointerType>(Operand->getType());
1176 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1177 const Type *RetTy = FTy->getReturnType();
Chris Lattner2f2d9472001-11-06 21:28:12 +00001178
Chris Lattner463d6a52003-08-05 15:34:45 +00001179 // If possible, print out the short form of the call instruction. We can
Chris Lattner6915f8f2002-04-07 22:49:37 +00001180 // only do this if the first argument is a pointer to a nonvararg function,
Chris Lattner463d6a52003-08-05 15:34:45 +00001181 // and if the return type is not a pointer to a function.
Chris Lattner2f2d9472001-11-06 21:28:12 +00001182 //
Chris Lattner463d6a52003-08-05 15:34:45 +00001183 if (!FTy->isVarArg() &&
Misha Brukmanb1c93172005-04-21 23:48:37 +00001184 (!isa<PointerType>(RetTy) ||
Chris Lattnerd9a36a62002-07-25 20:58:51 +00001185 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001186 Out << ' '; printType(RetTy);
Chris Lattner2f2d9472001-11-06 21:28:12 +00001187 writeOperand(Operand, false);
1188 } else {
1189 writeOperand(Operand, true);
1190 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001191 Out << '(';
Reid Spencer8c4914c2006-12-31 05:24:50 +00001192 for (unsigned op = 1, Eop = I.getNumOperands(); op < Eop; ++op) {
1193 if (op > 1)
1194 Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001195 writeOperand(I.getOperand(op), true);
Reid Spencer8c4914c2006-12-31 05:24:50 +00001196 if (FTy->getParamAttrs(op) != FunctionType::NoAttributeSet)
1197 Out << " " << FTy->getParamAttrsText(FTy->getParamAttrs(op));
Chris Lattner2f7c9632001-06-06 20:29:01 +00001198 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001199 Out << " )";
Reid Spencer136a91c2007-01-05 17:06:19 +00001200 if (FTy->getParamAttrs(0) != FunctionType::NoAttributeSet)
1201 Out << ' ' << FTy->getParamAttrsText(FTy->getParamAttrs(0));
Chris Lattner113f4f42002-06-25 16:13:24 +00001202 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Chris Lattner463d6a52003-08-05 15:34:45 +00001203 const PointerType *PTy = cast<PointerType>(Operand->getType());
1204 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1205 const Type *RetTy = FTy->getReturnType();
1206
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001207 // Print the calling convention being used.
1208 switch (II->getCallingConv()) {
1209 case CallingConv::C: break; // default
Chris Lattner29d20852006-05-19 21:58:52 +00001210 case CallingConv::Fast: Out << " fastcc"; break;
1211 case CallingConv::Cold: Out << " coldcc"; break;
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +00001212 case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break;
1213 case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break;
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001214 default: Out << " cc" << II->getCallingConv(); break;
1215 }
1216
Chris Lattner463d6a52003-08-05 15:34:45 +00001217 // If possible, print out the short form of the invoke instruction. We can
1218 // only do this if the first argument is a pointer to a nonvararg function,
1219 // and if the return type is not a pointer to a function.
1220 //
1221 if (!FTy->isVarArg() &&
Misha Brukmanb1c93172005-04-21 23:48:37 +00001222 (!isa<PointerType>(RetTy) ||
Chris Lattner463d6a52003-08-05 15:34:45 +00001223 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001224 Out << ' '; printType(RetTy);
Chris Lattner463d6a52003-08-05 15:34:45 +00001225 writeOperand(Operand, false);
1226 } else {
1227 writeOperand(Operand, true);
1228 }
1229
Misha Brukmana6619a92004-06-21 21:53:56 +00001230 Out << '(';
Reid Spencer8c4914c2006-12-31 05:24:50 +00001231 for (unsigned op = 3, Eop = I.getNumOperands(); op < Eop; ++op) {
1232 if (op > 3)
1233 Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001234 writeOperand(I.getOperand(op), true);
Reid Spencer9cfa4e82006-12-31 22:17:01 +00001235 if (FTy->getParamAttrs(op-2) != FunctionType::NoAttributeSet)
1236 Out << " " << FTy->getParamAttrsText(FTy->getParamAttrs(op-2));
Chris Lattner862e3382001-10-13 06:42:36 +00001237 }
1238
Reid Spencer136a91c2007-01-05 17:06:19 +00001239 Out << " )";
1240 if (FTy->getParamAttrs(0) != FunctionType::NoAttributeSet)
1241 Out << " " << FTy->getParamAttrsText(FTy->getParamAttrs(0));
1242 Out << "\n\t\t\tto";
Chris Lattner862e3382001-10-13 06:42:36 +00001243 writeOperand(II->getNormalDest(), true);
Misha Brukmana6619a92004-06-21 21:53:56 +00001244 Out << " unwind";
Chris Lattnerfae8ab32004-02-08 21:44:31 +00001245 writeOperand(II->getUnwindDest(), true);
Chris Lattner862e3382001-10-13 06:42:36 +00001246
Chris Lattner113f4f42002-06-25 16:13:24 +00001247 } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(&I)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001248 Out << ' ';
Chris Lattner8d48df22002-04-13 18:34:38 +00001249 printType(AI->getType()->getElementType());
1250 if (AI->isArrayAllocation()) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001251 Out << ',';
Chris Lattner8d48df22002-04-13 18:34:38 +00001252 writeOperand(AI->getArraySize(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001253 }
Nate Begeman848622f2005-11-05 09:21:28 +00001254 if (AI->getAlignment()) {
Chris Lattner7aeee3a2005-11-05 21:20:34 +00001255 Out << ", align " << AI->getAlignment();
Nate Begeman848622f2005-11-05 09:21:28 +00001256 }
Chris Lattner862e3382001-10-13 06:42:36 +00001257 } else if (isa<CastInst>(I)) {
Chris Lattnerc96e96b2003-11-17 01:17:04 +00001258 if (Operand) writeOperand(Operand, true); // Work with broken code
Misha Brukmana6619a92004-06-21 21:53:56 +00001259 Out << " to ";
Chris Lattner113f4f42002-06-25 16:13:24 +00001260 printType(I.getType());
Chris Lattner5b337482003-10-18 05:57:43 +00001261 } else if (isa<VAArgInst>(I)) {
Chris Lattnerc96e96b2003-11-17 01:17:04 +00001262 if (Operand) writeOperand(Operand, true); // Work with broken code
Misha Brukmana6619a92004-06-21 21:53:56 +00001263 Out << ", ";
Chris Lattnerf70da102003-05-08 02:44:12 +00001264 printType(I.getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +00001265 } else if (Operand) { // Print the normal way...
1266
Misha Brukmanb1c93172005-04-21 23:48:37 +00001267 // PrintAllTypes - Instructions who have operands of all the same type
Chris Lattner2f7c9632001-06-06 20:29:01 +00001268 // omit the type from all but the first operand. If the instruction has
1269 // different type operands (for example br), then they are all printed.
1270 bool PrintAllTypes = false;
1271 const Type *TheType = Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001272
Chris Lattner52bd5cb92004-03-12 05:53:14 +00001273 // Shift Left & Right print both types even for Ubyte LHS, and select prints
1274 // types even if all operands are bools.
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001275 if (isa<ShiftInst>(I) || isa<SelectInst>(I) || isa<StoreInst>(I) ||
1276 isa<ShuffleVectorInst>(I)) {
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00001277 PrintAllTypes = true;
1278 } else {
1279 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
1280 Operand = I.getOperand(i);
1281 if (Operand->getType() != TheType) {
1282 PrintAllTypes = true; // We have differing types! Print them all!
1283 break;
1284 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001285 }
1286 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001287
Chris Lattner7bfee412001-10-29 16:05:51 +00001288 if (!PrintAllTypes) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001289 Out << ' ';
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00001290 printType(TheType);
Chris Lattner7bfee412001-10-29 16:05:51 +00001291 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001292
Chris Lattner113f4f42002-06-25 16:13:24 +00001293 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001294 if (i) Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001295 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001296 }
1297 }
1298
Chris Lattner862e3382001-10-13 06:42:36 +00001299 printInfoComment(I);
Misha Brukmana6619a92004-06-21 21:53:56 +00001300 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001301}
1302
1303
1304//===----------------------------------------------------------------------===//
1305// External Interface declarations
1306//===----------------------------------------------------------------------===//
1307
Chris Lattner8339f7d2003-10-30 23:41:03 +00001308void Module::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001309 SlotMachine SlotTable(this);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001310 AssemblyWriter W(o, SlotTable, this, AAW);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001311 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001312}
1313
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001314void GlobalVariable::print(std::ostream &o) const {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001315 SlotMachine SlotTable(getParent());
Chris Lattner8339f7d2003-10-30 23:41:03 +00001316 AssemblyWriter W(o, SlotTable, getParent(), 0);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001317 W.write(this);
Chris Lattneradfe0d12001-09-10 20:08:19 +00001318}
1319
Chris Lattner8339f7d2003-10-30 23:41:03 +00001320void Function::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001321 SlotMachine SlotTable(getParent());
Chris Lattner8339f7d2003-10-30 23:41:03 +00001322 AssemblyWriter W(o, SlotTable, getParent(), AAW);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001323
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001324 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001325}
1326
Chris Lattnereef2fe72006-01-24 04:13:11 +00001327void InlineAsm::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001328 WriteAsOperand(o, this, true, 0);
Chris Lattnereef2fe72006-01-24 04:13:11 +00001329}
1330
Chris Lattner8339f7d2003-10-30 23:41:03 +00001331void BasicBlock::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001332 SlotMachine SlotTable(getParent());
Misha Brukmanb1c93172005-04-21 23:48:37 +00001333 AssemblyWriter W(o, SlotTable,
Chris Lattner8339f7d2003-10-30 23:41:03 +00001334 getParent() ? getParent()->getParent() : 0, AAW);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001335 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001336}
1337
Chris Lattner8339f7d2003-10-30 23:41:03 +00001338void Instruction::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001339 const Function *F = getParent() ? getParent()->getParent() : 0;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001340 SlotMachine SlotTable(F);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001341 AssemblyWriter W(o, SlotTable, F ? F->getParent() : 0, AAW);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001342
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001343 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001344}
Chris Lattner7db79582001-11-07 04:21:57 +00001345
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001346void Constant::print(std::ostream &o) const {
1347 if (this == 0) { o << "<null> constant value\n"; return; }
Chris Lattner7d734802002-09-10 15:53:49 +00001348
Misha Brukman21bbdb92004-06-04 21:11:51 +00001349 o << ' ' << getType()->getDescription() << ' ';
Evan Cheng5b19a802006-03-01 22:17:00 +00001350
1351 std::map<const Type *, std::string> TypeTable;
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001352 WriteConstantInt(o, this, TypeTable, 0);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001353}
1354
Misha Brukmanb1c93172005-04-21 23:48:37 +00001355void Type::print(std::ostream &o) const {
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001356 if (this == 0)
1357 o << "<null Type>";
1358 else
1359 o << getDescription();
1360}
1361
Chris Lattner2e9fa6d2002-04-09 19:48:49 +00001362void Argument::print(std::ostream &o) const {
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001363 WriteAsOperand(o, this, true, getParent() ? getParent()->getParent() : 0);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001364}
1365
Reid Spencer52641832004-05-25 18:14:38 +00001366// Value::dump - allow easy printing of Values from the debugger.
1367// Located here because so much of the needed functionality is here.
Bill Wendling22e978a2006-12-07 20:04:42 +00001368void Value::dump() const { print(*cerr.stream()); cerr << '\n'; }
Reid Spencer52641832004-05-25 18:14:38 +00001369
1370// Type::dump - allow easy printing of Values from the debugger.
1371// Located here because so much of the needed functionality is here.
Bill Wendling22e978a2006-12-07 20:04:42 +00001372void Type::dump() const { print(*cerr.stream()); cerr << '\n'; }
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001373
1374//===----------------------------------------------------------------------===//
Chris Lattnerfc9f1c92006-12-06 06:40:49 +00001375// SlotMachine Implementation
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001376//===----------------------------------------------------------------------===//
1377
1378#if 0
Bill Wendlingf3baad32006-12-07 01:30:32 +00001379#define SC_DEBUG(X) cerr << X
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001380#else
1381#define SC_DEBUG(X)
1382#endif
1383
1384// Module level constructor. Causes the contents of the Module (sans functions)
1385// to be added to the slot table.
Misha Brukmanb1c93172005-04-21 23:48:37 +00001386SlotMachine::SlotMachine(const Module *M)
Reid Spencer56010e42004-05-26 21:56:09 +00001387 : TheModule(M) ///< Saved for lazy initialization.
1388 , TheFunction(0)
Reid Spencerb0ac8c42004-08-16 07:46:33 +00001389 , FunctionProcessed(false)
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001390{
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001391}
1392
1393// Function level constructor. Causes the contents of the Module and the one
1394// function provided to be added to the slot table.
Chris Lattner23e829a2006-12-06 05:12:21 +00001395SlotMachine::SlotMachine(const Function *F)
1396 : TheModule(F ? F->getParent() : 0) ///< Saved for lazy initialization
Reid Spencer56010e42004-05-26 21:56:09 +00001397 , TheFunction(F) ///< Saved for lazy initialization
Reid Spencerb0ac8c42004-08-16 07:46:33 +00001398 , FunctionProcessed(false)
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001399{
Reid Spencer56010e42004-05-26 21:56:09 +00001400}
1401
Chris Lattner5e043322007-01-11 03:54:27 +00001402inline void SlotMachine::initialize() {
Chris Lattner23e829a2006-12-06 05:12:21 +00001403 if (TheModule) {
Misha Brukmanb1c93172005-04-21 23:48:37 +00001404 processModule();
Reid Spencer56010e42004-05-26 21:56:09 +00001405 TheModule = 0; ///< Prevent re-processing next time we're called.
1406 }
Chris Lattner193de902006-12-06 05:27:40 +00001407 if (TheFunction && !FunctionProcessed)
Misha Brukmanb1c93172005-04-21 23:48:37 +00001408 processFunction();
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001409}
1410
1411// Iterate through all the global variables, functions, and global
Misha Brukmanb1c93172005-04-21 23:48:37 +00001412// variable initializers and create slots for them.
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001413void SlotMachine::processModule() {
1414 SC_DEBUG("begin processModule!\n");
1415
Chris Lattner0dda8612006-12-06 06:15:43 +00001416 // Add all of the unnamed global variables to the value table.
Chris Lattner54932b02006-12-06 04:41:52 +00001417 for (Module::const_global_iterator I = TheModule->global_begin(),
1418 E = TheModule->global_end(); I != E; ++I)
Chris Lattner0dda8612006-12-06 06:15:43 +00001419 if (!I->hasName())
Chris Lattnerea862a32007-01-09 07:55:49 +00001420 CreateModuleSlot(I);
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001421
Chris Lattner0dda8612006-12-06 06:15:43 +00001422 // Add all the unnamed functions to the table.
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001423 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
1424 I != E; ++I)
Chris Lattner0dda8612006-12-06 06:15:43 +00001425 if (!I->hasName())
Chris Lattnerea862a32007-01-09 07:55:49 +00001426 CreateModuleSlot(I);
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001427
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001428 SC_DEBUG("end processModule!\n");
1429}
1430
1431
Reid Spencer56010e42004-05-26 21:56:09 +00001432// Process the arguments, basic blocks, and instructions of a function.
1433void SlotMachine::processFunction() {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001434 SC_DEBUG("begin processFunction!\n");
1435
Chris Lattner0dda8612006-12-06 06:15:43 +00001436 // Add all the function arguments with no names.
Misha Brukmanb1c93172005-04-21 23:48:37 +00001437 for(Function::const_arg_iterator AI = TheFunction->arg_begin(),
Chris Lattner531f9e92005-03-15 04:54:21 +00001438 AE = TheFunction->arg_end(); AI != AE; ++AI)
Chris Lattner0dda8612006-12-06 06:15:43 +00001439 if (!AI->hasName())
Chris Lattnerea862a32007-01-09 07:55:49 +00001440 CreateFunctionSlot(AI);
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001441
1442 SC_DEBUG("Inserting Instructions:\n");
1443
Chris Lattner0dda8612006-12-06 06:15:43 +00001444 // Add all of the basic blocks and instructions with no names.
Misha Brukmanb1c93172005-04-21 23:48:37 +00001445 for (Function::const_iterator BB = TheFunction->begin(),
Reid Spencer56010e42004-05-26 21:56:09 +00001446 E = TheFunction->end(); BB != E; ++BB) {
Chris Lattner0dda8612006-12-06 06:15:43 +00001447 if (!BB->hasName())
Chris Lattnerea862a32007-01-09 07:55:49 +00001448 CreateFunctionSlot(BB);
Chris Lattner0dda8612006-12-06 06:15:43 +00001449 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
1450 if (I->getType() != Type::VoidTy && !I->hasName())
Chris Lattnerea862a32007-01-09 07:55:49 +00001451 CreateFunctionSlot(I);
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001452 }
1453
Reid Spencerb0ac8c42004-08-16 07:46:33 +00001454 FunctionProcessed = true;
1455
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001456 SC_DEBUG("end processFunction!\n");
1457}
1458
Chris Lattner193de902006-12-06 05:27:40 +00001459/// Clean up after incorporating a function. This is the only way to get out of
Chris Lattner5e043322007-01-11 03:54:27 +00001460/// the function incorporation state that affects get*Slot/Create*Slot. Function
Chris Lattner89e774e2007-01-09 07:46:21 +00001461/// incorporation state is indicated by TheFunction != 0.
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001462void SlotMachine::purgeFunction() {
1463 SC_DEBUG("begin purgeFunction!\n");
1464 fMap.clear(); // Simply discard the function level map
Reid Spencer56010e42004-05-26 21:56:09 +00001465 TheFunction = 0;
Reid Spencerb0ac8c42004-08-16 07:46:33 +00001466 FunctionProcessed = false;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001467 SC_DEBUG("end purgeFunction!\n");
1468}
1469
Chris Lattner5e043322007-01-11 03:54:27 +00001470/// getGlobalSlot - Get the slot number of a global value.
1471int SlotMachine::getGlobalSlot(const GlobalValue *V) {
1472 // Check for uninitialized state and do lazy initialization.
1473 initialize();
1474
1475 // Find the type plane in the module map
1476 TypedPlanes::const_iterator MI = mMap.find(V->getType());
1477 if (MI == mMap.end()) return -1;
1478
1479 // Lookup the value in the module plane's map.
1480 ValueMap::const_iterator MVI = MI->second.map.find(V);
Reid Spencer5656cad2007-01-11 07:58:19 +00001481 return MVI != MI->second.map.end() ? int(MVI->second) : -1;
Chris Lattner5e043322007-01-11 03:54:27 +00001482}
Reid Spencer56010e42004-05-26 21:56:09 +00001483
Chris Lattner5e043322007-01-11 03:54:27 +00001484
1485/// getLocalSlot - Get the slot number for a value that is local to a function.
1486int SlotMachine::getLocalSlot(const Value *V) {
1487 assert(!isa<Constant>(V) && "Can't get a constant or global slot with this!");
1488
1489 // Check for uninitialized state and do lazy initialization.
1490 initialize();
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001491
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001492 // Get the type of the value
Chris Lattner5e043322007-01-11 03:54:27 +00001493 const Type *VTy = V->getType();
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001494
Chris Lattner5e043322007-01-11 03:54:27 +00001495 TypedPlanes::const_iterator FI = fMap.find(VTy);
1496 if (FI == fMap.end()) return -1;
1497
1498 // Lookup the Value in the function and module maps.
1499 ValueMap::const_iterator FVI = FI->second.map.find(V);
Chris Lattner5e043322007-01-11 03:54:27 +00001500
Chris Lattner550a8f32007-01-11 04:30:21 +00001501 // If the value doesn't exist in the function map, it is a <badref>
1502 if (FVI == FI->second.map.end()) return -1;
Chris Lattner5e043322007-01-11 03:54:27 +00001503
Reid Spencer788e3172007-01-26 08:02:52 +00001504 return FVI->second;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001505}
1506
Reid Spencer58d30f22004-07-04 11:50:43 +00001507
Chris Lattnerea862a32007-01-09 07:55:49 +00001508/// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
1509void SlotMachine::CreateModuleSlot(const GlobalValue *V) {
Chris Lattner04398e82007-01-09 08:04:59 +00001510 assert(V && "Can't insert a null Value into SlotMachine!");
1511
1512 unsigned DestSlot = 0;
1513 const Type *VTy = V->getType();
1514
Chris Lattner2cf85c72007-01-10 06:43:26 +00001515 ValuePlane &PlaneMap = mMap[VTy];
1516 DestSlot = PlaneMap.map[V] = PlaneMap.next_slot++;
Chris Lattner04398e82007-01-09 08:04:59 +00001517
1518 SC_DEBUG(" Inserting value [" << VTy << "] = " << V << " slot=" <<
1519 DestSlot << " [");
1520 // G = Global, F = Function, o = other
1521 SC_DEBUG((isa<GlobalVariable>(V) ? 'G' : 'F') << "]\n");
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001522}
1523
1524
Chris Lattnerea862a32007-01-09 07:55:49 +00001525/// CreateSlot - Create a new slot for the specified value if it has no name.
1526void SlotMachine::CreateFunctionSlot(const Value *V) {
1527 const Type *VTy = V->getType();
1528 assert(VTy != Type::VoidTy && !V->hasName() && "Doesn't need a slot!");
Chris Lattner04398e82007-01-09 08:04:59 +00001529
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001530 unsigned DestSlot = 0;
Chris Lattner04398e82007-01-09 08:04:59 +00001531
Chris Lattner2cf85c72007-01-10 06:43:26 +00001532 ValuePlane &PlaneMap = fMap[VTy];
1533 DestSlot = PlaneMap.map[V] = PlaneMap.next_slot++;
Chris Lattner04398e82007-01-09 08:04:59 +00001534
Chris Lattnerf8090cb2006-12-06 05:55:41 +00001535 // G = Global, F = Function, o = other
Chris Lattner04398e82007-01-09 08:04:59 +00001536 SC_DEBUG(" Inserting value [" << VTy << "] = " << V << " slot=" <<
1537 DestSlot << " [o]\n");
1538}