blob: fbe5f07d8a39b95f4690306b8e22ca7125bf4081 [file] [log] [blame]
Chris Lattner8da78af2002-04-07 22:31:46 +00001//===-- AsmWriter.cpp - Printing LLVM as an assembly file -----------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
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 Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
10// This library implements the functionality defined in llvm/Assembly/Writer.h
11//
Chris Lattner02b93992002-04-12 18:21:53 +000012// Note that these routines must be extremely tolerant of various errors in the
Chris Lattner8f77dae2003-05-08 02:44:12 +000013// LLVM code, because it can be used for debugging transformations.
Chris Lattner02b93992002-04-12 18:21:53 +000014//
Chris Lattner00950542001-06-06 20:29:01 +000015//===----------------------------------------------------------------------===//
16
Chris Lattner75cf7cf2002-04-08 22:03:40 +000017#include "llvm/Assembly/Writer.h"
Chris Lattnerf082b802002-07-23 18:07:49 +000018#include "llvm/Assembly/PrintModulePass.h"
Chris Lattner95e5a2c2003-10-30 23:41:03 +000019#include "llvm/Assembly/AsmAnnotationWriter.h"
Chris Lattnerd5118982005-05-06 20:26:43 +000020#include "llvm/CallingConv.h"
Chris Lattnerf2d577b2004-01-20 19:50:34 +000021#include "llvm/Constants.h"
Chris Lattner3eb59c02002-04-29 18:46:50 +000022#include "llvm/DerivedTypes.h"
Chris Lattner863517a2006-01-25 18:57:27 +000023#include "llvm/InlineAsm.h"
Vikram S. Adveb4dbb442002-07-14 23:14:45 +000024#include "llvm/Instruction.h"
Misha Brukman44336292004-07-29 16:53:53 +000025#include "llvm/Instructions.h"
Chris Lattnerf2d577b2004-01-20 19:50:34 +000026#include "llvm/Module.h"
Reid Spenceref9b9a72007-02-05 20:47:22 +000027#include "llvm/ValueSymbolTable.h"
Reid Spencer78d033e2007-01-06 07:24:44 +000028#include "llvm/TypeSymbolTable.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000029#include "llvm/ADT/StringExtras.h"
30#include "llvm/ADT/STLExtras.h"
Bill Wendling8f487662006-11-28 02:09:03 +000031#include "llvm/Support/CFG.h"
Jim Laskeycb6682f2005-08-17 19:34:49 +000032#include "llvm/Support/MathExtras.h"
Bill Wendling8f487662006-11-28 02:09:03 +000033#include "llvm/Support/Streams.h"
Chris Lattner007377f2001-09-07 16:36:04 +000034#include <algorithm>
Chris Lattner31f84992003-11-21 20:23:48 +000035using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000036
Chris Lattner0a8e8e12004-07-15 02:51:31 +000037namespace llvm {
Reid Spencer0d1b77e2004-05-26 07:18:52 +000038
Reid Spenceredd5d9e2005-05-15 16:13:11 +000039// Make virtual table appear in this compilation unit.
40AssemblyAnnotationWriter::~AssemblyAnnotationWriter() {}
41
Reid Spencer0d1b77e2004-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 Brukmanfd939082005-04-21 23:48:37 +000054 struct ValuePlane {
Reid Spencer0d1b77e2004-05-26 07:18:52 +000055 unsigned next_slot; ///< The next slot number to use
56 ValueMap map; ///< The map of Value* -> unsigned
Reid Spencer0e25e1c2004-07-04 11:50:43 +000057 ValuePlane() { next_slot = 0; } ///< Make sure we start at 0
58 };
59
Reid Spencer0d1b77e2004-05-26 07:18:52 +000060 /// @brief The map of planes by Type
Reid Spencer0e25e1c2004-07-04 11:50:43 +000061 typedef std::map<const Type*, ValuePlane> TypedPlanes;
Reid Spencer0d1b77e2004-05-26 07:18:52 +000062
63/// @}
64/// @name Constructors
65/// @{
66public:
67 /// @brief Construct from a module
Chris Lattnerc96ce892006-12-06 05:12:21 +000068 SlotMachine(const Module *M);
Reid Spencer0d1b77e2004-05-26 07:18:52 +000069
70 /// @brief Construct from a function, starting out in incorp state.
Chris Lattnerc96ce892006-12-06 05:12:21 +000071 SlotMachine(const Function *F);
Reid Spencer0d1b77e2004-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 Lattner22379bc2007-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 Spencerfc621e22004-06-09 15:26:53 +000081
Reid Spencer0d1b77e2004-05-26 07:18:52 +000082/// @}
83/// @name Mutators
84/// @{
85public:
Misha Brukmanfd939082005-04-21 23:48:37 +000086 /// If you'd like to deal with a function instead of just a module, use
Reid Spencer0d1b77e2004-05-26 07:18:52 +000087 /// this method to get its data into the SlotMachine.
Misha Brukmanfd939082005-04-21 23:48:37 +000088 void incorporateFunction(const Function *F) {
89 TheFunction = F;
Reid Spencer28531c72004-08-16 07:46:33 +000090 FunctionProcessed = false;
91 }
Reid Spencer0d1b77e2004-05-26 07:18:52 +000092
Misha Brukmanfd939082005-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 Spencer0d1b77e2004-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 Spencerb03de0c2004-05-26 21:56:09 +0000102 /// This function does the actual initialization.
103 inline void initialize();
104
Chris Lattner9446bbe2007-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 Spencer0d1b77e2004-05-26 07:18:52 +0000110
Reid Spencer0d1b77e2004-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 Spencerb03de0c2004-05-26 21:56:09 +0000115 /// Add all of the functions arguments, basic blocks, and instructions
116 void processFunction();
117
Reid Spencer0d1b77e2004-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 Spencerb03de0c2004-05-26 21:56:09 +0000127 const Module* TheModule;
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000128
Reid Spencerb03de0c2004-05-26 21:56:09 +0000129 /// @brief The function for which we are holding slot numbers
130 const Function* TheFunction;
Reid Spencer28531c72004-08-16 07:46:33 +0000131 bool FunctionProcessed;
Reid Spencer0d1b77e2004-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 Lattner0a8e8e12004-07-15 02:51:31 +0000143} // end namespace llvm
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000144
Chris Lattner7f8897f2006-08-27 22:42:52 +0000145static RegisterPass<PrintModulePass>
Chris Lattner3dd965c2006-08-21 17:20:01 +0000146X("printm", "Print module to stderr");
Chris Lattner7f8897f2006-08-27 22:42:52 +0000147static RegisterPass<PrintFunctionPass>
Chris Lattner3dd965c2006-08-21 17:20:01 +0000148Y("print","Print function to stderr");
Chris Lattnerf082b802002-07-23 18:07:49 +0000149
Misha Brukmanfd939082005-04-21 23:48:37 +0000150static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
Chris Lattner919e70c2006-12-06 05:50:41 +0000151 std::map<const Type *, std::string> &TypeTable,
Reid Spencer0e25e1c2004-07-04 11:50:43 +0000152 SlotMachine *Machine);
153
Chris Lattner207b5bc2001-10-29 16:37:48 +0000154static const Module *getModuleFromVal(const Value *V) {
Chris Lattner949a3622003-07-23 15:30:06 +0000155 if (const Argument *MA = dyn_cast<Argument>(V))
Chris Lattner207b5bc2001-10-29 16:37:48 +0000156 return MA->getParent() ? MA->getParent()->getParent() : 0;
Chris Lattner949a3622003-07-23 15:30:06 +0000157 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
Chris Lattner207b5bc2001-10-29 16:37:48 +0000158 return BB->getParent() ? BB->getParent()->getParent() : 0;
Chris Lattner949a3622003-07-23 15:30:06 +0000159 else if (const Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000160 const Function *M = I->getParent() ? I->getParent()->getParent() : 0;
Chris Lattner207b5bc2001-10-29 16:37:48 +0000161 return M ? M->getParent() : 0;
Chris Lattner949a3622003-07-23 15:30:06 +0000162 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
Chris Lattner207b5bc2001-10-29 16:37:48 +0000163 return GV->getParent();
Chris Lattner207b5bc2001-10-29 16:37:48 +0000164 return 0;
165}
166
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000167static SlotMachine *createSlotMachine(const Value *V) {
Chris Lattner949a3622003-07-23 15:30:06 +0000168 if (const Argument *FA = dyn_cast<Argument>(V)) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000169 return new SlotMachine(FA->getParent());
Chris Lattner949a3622003-07-23 15:30:06 +0000170 } else if (const Instruction *I = dyn_cast<Instruction>(V)) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000171 return new SlotMachine(I->getParent()->getParent());
Chris Lattner949a3622003-07-23 15:30:06 +0000172 } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000173 return new SlotMachine(BB->getParent());
Chris Lattner949a3622003-07-23 15:30:06 +0000174 } else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)){
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000175 return new SlotMachine(GV->getParent());
Chris Lattner949a3622003-07-23 15:30:06 +0000176 } else if (const Function *Func = dyn_cast<Function>(V)) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000177 return new SlotMachine(Func);
Chris Lattnerc1824992001-10-29 16:05:51 +0000178 }
179 return 0;
180}
Chris Lattner00950542001-06-06 20:29:01 +0000181
Reid Spencer3702d262007-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 Lattner24b8a5d2003-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 Spencer3702d262007-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 Lattner24b8a5d2003-08-22 05:40:38 +0000214 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000215
Chris Lattner24b8a5d2003-08-22 05:40:38 +0000216 // If we get here, then the identifier is legal to use as a "VarID".
Reid Spencer3702d262007-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 Lattner24b8a5d2003-08-22 05:40:38 +0000223}
224
Chris Lattner207b5bc2001-10-29 16:37:48 +0000225
Misha Brukmanab5c6002004-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 Lattner207b5bc2001-10-29 16:37:48 +0000229static void fillTypeNameTable(const Module *M,
Chris Lattner7b13f562003-05-08 02:08:14 +0000230 std::map<const Type *, std::string> &TypeNames) {
Chris Lattner6e6026b2002-11-20 18:36:02 +0000231 if (!M) return;
Reid Spencer78d033e2007-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 Spencer9231ac82004-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 Spencerb03de0c2004-05-26 21:56:09 +0000240 !cast<PointerType>(Ty)->getElementType()->isPrimitiveType() ||
Chris Lattner42a75512007-01-15 02:27:26 +0000241 !cast<PointerType>(Ty)->getElementType()->isInteger() ||
Reid Spencerb03de0c2004-05-26 21:56:09 +0000242 isa<OpaqueType>(cast<PointerType>(Ty)->getElementType()))
Reid Spencer3702d262007-01-26 08:02:52 +0000243 TypeNames.insert(std::make_pair(Ty, getLLVMName(TI->first, LocalPrefix)));
Chris Lattner207b5bc2001-10-29 16:37:48 +0000244 }
245}
246
247
248
Misha Brukmanfd939082005-04-21 23:48:37 +0000249static void calcTypeName(const Type *Ty,
John Criswell4ff620a2004-06-01 14:54:08 +0000250 std::vector<const Type *> &TypeStack,
251 std::map<const Type *, std::string> &TypeNames,
252 std::string & Result){
Chris Lattner42a75512007-01-15 02:27:26 +0000253 if (Ty->isInteger() || (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty))) {
John Criswell4ff620a2004-06-01 14:54:08 +0000254 Result += Ty->getDescription(); // Base case
255 return;
256 }
Chris Lattner207b5bc2001-10-29 16:37:48 +0000257
258 // Check to see if the type is named.
Chris Lattner7b13f562003-05-08 02:08:14 +0000259 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
John Criswell4ff620a2004-06-01 14:54:08 +0000260 if (I != TypeNames.end()) {
261 Result += I->second;
262 return;
263 }
Chris Lattner207b5bc2001-10-29 16:37:48 +0000264
John Criswell4ff620a2004-06-01 14:54:08 +0000265 if (isa<OpaqueType>(Ty)) {
266 Result += "opaque";
267 return;
268 }
Chris Lattner88c17382003-10-30 00:22:33 +0000269
Chris Lattner207b5bc2001-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 Brukmanfd939082005-04-21 23:48:37 +0000274 // This is another base case for the recursion. In this case, we know
Chris Lattner207b5bc2001-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 Criswell4ff620a2004-06-01 14:54:08 +0000277 if (Slot < CurSize) {
278 Result += "\\" + utostr(CurSize-Slot); // Here's the upreference
279 return;
280 }
Chris Lattner207b5bc2001-10-29 16:37:48 +0000281
282 TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
Misha Brukmanfd939082005-04-21 23:48:37 +0000283
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000284 switch (Ty->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000285 case Type::IntegerTyID: {
286 unsigned BitWidth = cast<IntegerType>(Ty)->getBitWidth();
Reid Spencer15ee9352007-01-12 07:25:20 +0000287 Result += "i" + utostr(BitWidth);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000288 break;
289 }
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000290 case Type::FunctionTyID: {
Chris Lattner949a3622003-07-23 15:30:06 +0000291 const FunctionType *FTy = cast<FunctionType>(Ty);
John Criswell4ff620a2004-06-01 14:54:08 +0000292 calcTypeName(FTy->getReturnType(), TypeStack, TypeNames, Result);
293 Result += " (";
Reid Spencerbd5db8e2006-12-31 05:24:50 +0000294 unsigned Idx = 1;
Chris Lattnerd5d89962004-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 Lattner207b5bc2001-10-29 16:37:48 +0000298 Result += ", ";
John Criswell4ff620a2004-06-01 14:54:08 +0000299 calcTypeName(*I, TypeStack, TypeNames, Result);
Reid Spencerbd5db8e2006-12-31 05:24:50 +0000300 if (FTy->getParamAttrs(Idx)) {
301 Result += + " ";
302 Result += FunctionType::getParamAttrsText(FTy->getParamAttrs(Idx));
303 }
304 Idx++;
Chris Lattner207b5bc2001-10-29 16:37:48 +0000305 }
Chris Lattner2761e9f2002-04-13 20:53:41 +0000306 if (FTy->isVarArg()) {
Chris Lattnerd5d89962004-02-09 04:14:01 +0000307 if (FTy->getNumParams()) Result += ", ";
Chris Lattner207b5bc2001-10-29 16:37:48 +0000308 Result += "...";
309 }
310 Result += ")";
Reid Spencer2c261782007-01-05 17:06:19 +0000311 if (FTy->getParamAttrs(0)) {
312 Result += " ";
313 Result += FunctionType::getParamAttrsText(FTy->getParamAttrs(0));
314 }
Chris Lattner207b5bc2001-10-29 16:37:48 +0000315 break;
316 }
317 case Type::StructTyID: {
Chris Lattner949a3622003-07-23 15:30:06 +0000318 const StructType *STy = cast<StructType>(Ty);
Andrew Lenharth38ecbf12006-12-08 18:06:16 +0000319 if (STy->isPacked())
320 Result += '<';
John Criswell4ff620a2004-06-01 14:54:08 +0000321 Result += "{ ";
Chris Lattnerd21cd802004-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 Lattner207b5bc2001-10-29 16:37:48 +0000325 Result += ", ";
John Criswell4ff620a2004-06-01 14:54:08 +0000326 calcTypeName(*I, TypeStack, TypeNames, Result);
Chris Lattner207b5bc2001-10-29 16:37:48 +0000327 }
328 Result += " }";
Andrew Lenharth38ecbf12006-12-08 18:06:16 +0000329 if (STy->isPacked())
330 Result += '>';
Chris Lattner207b5bc2001-10-29 16:37:48 +0000331 break;
332 }
333 case Type::PointerTyID:
Misha Brukmanfd939082005-04-21 23:48:37 +0000334 calcTypeName(cast<PointerType>(Ty)->getElementType(),
John Criswell4ff620a2004-06-01 14:54:08 +0000335 TypeStack, TypeNames, Result);
336 Result += "*";
Chris Lattner207b5bc2001-10-29 16:37:48 +0000337 break;
338 case Type::ArrayTyID: {
Chris Lattner949a3622003-07-23 15:30:06 +0000339 const ArrayType *ATy = cast<ArrayType>(Ty);
John Criswell4ff620a2004-06-01 14:54:08 +0000340 Result += "[" + utostr(ATy->getNumElements()) + " x ";
341 calcTypeName(ATy->getElementType(), TypeStack, TypeNames, Result);
342 Result += "]";
Chris Lattner207b5bc2001-10-29 16:37:48 +0000343 break;
344 }
Reid Spencer9d6565a2007-02-15 02:26:10 +0000345 case Type::VectorTyID: {
346 const VectorType *PTy = cast<VectorType>(Ty);
Brian Gaeke715c90b2004-08-20 06:00:58 +0000347 Result += "<" + utostr(PTy->getNumElements()) + " x ";
348 calcTypeName(PTy->getElementType(), TypeStack, TypeNames, Result);
349 Result += ">";
350 break;
351 }
Chris Lattner9e094c42003-05-14 17:50:47 +0000352 case Type::OpaqueTyID:
John Criswell4ff620a2004-06-01 14:54:08 +0000353 Result += "opaque";
Chris Lattner9e094c42003-05-14 17:50:47 +0000354 break;
Chris Lattner207b5bc2001-10-29 16:37:48 +0000355 default:
John Criswell4ff620a2004-06-01 14:54:08 +0000356 Result += "<unrecognized-type>";
Chris Lattner82c4bc72006-12-06 06:40:49 +0000357 break;
Chris Lattner207b5bc2001-10-29 16:37:48 +0000358 }
359
360 TypeStack.pop_back(); // Remove self from stack...
Chris Lattner207b5bc2001-10-29 16:37:48 +0000361}
362
363
Misha Brukman9d0802e2004-03-01 19:48:13 +0000364/// printTypeInt - The internal guts of printing out a type that has a
365/// potentially named portion.
366///
Chris Lattner7b13f562003-05-08 02:08:14 +0000367static std::ostream &printTypeInt(std::ostream &Out, const Type *Ty,
368 std::map<const Type *, std::string> &TypeNames) {
Chris Lattner207b5bc2001-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 Lattner42a75512007-01-15 02:27:26 +0000372 if (Ty->isInteger() || (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty)))
Chris Lattnerdaf2a492003-10-30 00:12:51 +0000373 return Out << Ty->getDescription();
Chris Lattner207b5bc2001-10-29 16:37:48 +0000374
375 // Check to see if the type is named.
Chris Lattner7b13f562003-05-08 02:08:14 +0000376 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
Chris Lattner207b5bc2001-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 Lattner7b13f562003-05-08 02:08:14 +0000383 std::vector<const Type *> TypeStack;
John Criswell4ff620a2004-06-01 14:54:08 +0000384 std::string TypeName;
385 calcTypeName(Ty, TypeStack, TypeNames, TypeName);
Chris Lattner697954c2002-01-20 22:54:45 +0000386 TypeNames.insert(std::make_pair(Ty, TypeName));//Cache type name for later use
John Criswell4ff620a2004-06-01 14:54:08 +0000387 return (Out << TypeName);
Chris Lattner207b5bc2001-10-29 16:37:48 +0000388}
389
Chris Lattnere51e03b2001-10-31 04:33:19 +0000390
Misha Brukman9d0802e2004-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 Lattner31f84992003-11-21 20:23:48 +0000395std::ostream &llvm::WriteTypeSymbolic(std::ostream &Out, const Type *Ty,
396 const Module *M) {
Misha Brukmanfd939082005-04-21 23:48:37 +0000397 Out << ' ';
Chris Lattner207b5bc2001-10-29 16:37:48 +0000398
Chris Lattner82c4bc72006-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 Lattner7b8660d2001-10-29 16:40:32 +0000402 return Out << Ty->getDescription();
Chris Lattner82c4bc72006-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 Lattner207b5bc2001-10-29 16:37:48 +0000407}
408
Chris Lattner18365502006-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 Lattner82c4bc72006-12-06 06:40:49 +0000424static const char *getPredicateText(unsigned predicate) {
Reid Spencer81dfeb32006-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 Brukmanfd939082005-04-21 23:48:37 +0000457/// @brief Internal constant writer.
458static void WriteConstantInt(std::ostream &Out, const Constant *CV,
Chris Lattner7b13f562003-05-08 02:08:14 +0000459 std::map<const Type *, std::string> &TypeTable,
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000460 SlotMachine *Machine) {
Jim Laskey38a409c2006-02-27 10:33:53 +0000461 const int IndentSize = 4;
462 static std::string Indent = "\n";
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000463 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Reid Spencer4fe16d62007-01-11 18:21:29 +0000464 if (CI->getType() == Type::Int1Ty)
Reid Spencer579dca12007-01-12 04:24:46 +0000465 Out << (CI->getZExtValue() ? "true" : "false");
466 else
467 Out << CI->getSExtValue();
Chris Lattner66e810b2002-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 Gaekeb471a232003-06-17 23:55:35 +0000482 (StrVal[1] >= '0' && StrVal[1] <= '9')))
Chris Lattner66e810b2002-04-18 18:53:13 +0000483 // Reparse stringized version!
484 if (atof(StrVal.c_str()) == CFP->getValue()) {
Chris Lattner71d94d12005-01-04 01:56:57 +0000485 Out << StrVal;
486 return;
Chris Lattner66e810b2002-04-18 18:53:13 +0000487 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000488
Chris Lattner66e810b2002-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 Lattner71d94d12005-01-04 01:56:57 +0000491 assert(sizeof(double) == sizeof(uint64_t) &&
Chris Lattner66e810b2002-04-18 18:53:13 +0000492 "assuming that double is 64 bits!");
Jim Laskeycb6682f2005-08-17 19:34:49 +0000493 Out << "0x" << utohexstr(DoubleToBits(CFP->getValue()));
Chris Lattner66e810b2002-04-18 18:53:13 +0000494
Chris Lattnerde512b52004-02-15 05:55:15 +0000495 } else if (isa<ConstantAggregateZero>(CV)) {
496 Out << "zeroinitializer";
Chris Lattner66e810b2002-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 Brukmanfd939082005-04-21 23:48:37 +0000500 //
Chris Lattner66e810b2002-04-18 18:53:13 +0000501 const Type *ETy = CA->getType()->getElementType();
Chris Lattner18365502006-01-23 23:03:36 +0000502 if (CA->isString()) {
Chris Lattner66e810b2002-04-18 18:53:13 +0000503 Out << "c\"";
Chris Lattner18365502006-01-23 23:03:36 +0000504 PrintEscapedString(CA->getAsString(), Out);
Chris Lattner66e810b2002-04-18 18:53:13 +0000505 Out << "\"";
506
507 } else { // Cannot output in string format...
Misha Brukman40c732c2004-06-04 21:11:51 +0000508 Out << '[';
Chris Lattner7a716ad2002-04-16 21:36:08 +0000509 if (CA->getNumOperands()) {
Misha Brukman40c732c2004-06-04 21:11:51 +0000510 Out << ' ';
Chris Lattner66e810b2002-04-18 18:53:13 +0000511 printTypeInt(Out, ETy, TypeTable);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000512 WriteAsOperandInternal(Out, CA->getOperand(0),
Chris Lattner2fcfdb72006-12-06 06:24:27 +0000513 TypeTable, Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000514 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
515 Out << ", ";
Chris Lattner66e810b2002-04-18 18:53:13 +0000516 printTypeInt(Out, ETy, TypeTable);
Chris Lattner2fcfdb72006-12-06 06:24:27 +0000517 WriteAsOperandInternal(Out, CA->getOperand(i), TypeTable, Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000518 }
519 }
520 Out << " ]";
521 }
522 } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Andrew Lenharth43f344a2007-01-08 18:21:30 +0000523 if (CS->getType()->isPacked())
524 Out << '<';
Misha Brukman40c732c2004-06-04 21:11:51 +0000525 Out << '{';
Jim Laskeya3f332b2006-02-25 12:27:03 +0000526 unsigned N = CS->getNumOperands();
527 if (N) {
Jim Laskey38a409c2006-02-27 10:33:53 +0000528 if (N > 2) {
529 Indent += std::string(IndentSize, ' ');
530 Out << Indent;
531 } else {
532 Out << ' ';
533 }
Chris Lattner7a716ad2002-04-16 21:36:08 +0000534 printTypeInt(Out, CS->getOperand(0)->getType(), TypeTable);
535
Chris Lattner2fcfdb72006-12-06 06:24:27 +0000536 WriteAsOperandInternal(Out, CS->getOperand(0), TypeTable, Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000537
Jim Laskeya3f332b2006-02-25 12:27:03 +0000538 for (unsigned i = 1; i < N; i++) {
Chris Lattner7a716ad2002-04-16 21:36:08 +0000539 Out << ", ";
Jim Laskey38a409c2006-02-27 10:33:53 +0000540 if (N > 2) Out << Indent;
Chris Lattner7a716ad2002-04-16 21:36:08 +0000541 printTypeInt(Out, CS->getOperand(i)->getType(), TypeTable);
542
Chris Lattner2fcfdb72006-12-06 06:24:27 +0000543 WriteAsOperandInternal(Out, CS->getOperand(i), TypeTable, Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000544 }
Jim Laskey38a409c2006-02-27 10:33:53 +0000545 if (N > 2) Indent.resize(Indent.size() - IndentSize);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000546 }
Jim Laskeya3f332b2006-02-25 12:27:03 +0000547
Chris Lattner7a716ad2002-04-16 21:36:08 +0000548 Out << " }";
Andrew Lenharth43f344a2007-01-08 18:21:30 +0000549 if (CS->getType()->isPacked())
550 Out << '>';
Reid Spencer9d6565a2007-02-15 02:26:10 +0000551 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
Brian Gaeke715c90b2004-08-20 06:00:58 +0000552 const Type *ETy = CP->getType()->getElementType();
Misha Brukmanfd939082005-04-21 23:48:37 +0000553 assert(CP->getNumOperands() > 0 &&
Brian Gaeke715c90b2004-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 Lattner2fcfdb72006-12-06 06:24:27 +0000558 WriteAsOperandInternal(Out, CP->getOperand(0), TypeTable, Machine);
Brian Gaeke715c90b2004-08-20 06:00:58 +0000559 for (unsigned i = 1, e = CP->getNumOperands(); i != e; ++i) {
560 Out << ", ";
561 printTypeInt(Out, ETy, TypeTable);
Chris Lattner2fcfdb72006-12-06 06:24:27 +0000562 WriteAsOperandInternal(Out, CP->getOperand(i), TypeTable, Machine);
Brian Gaeke715c90b2004-08-20 06:00:58 +0000563 }
564 Out << " >";
Chris Lattner7a716ad2002-04-16 21:36:08 +0000565 } else if (isa<ConstantPointerNull>(CV)) {
566 Out << "null";
567
Chris Lattnerb976e662004-10-16 18:08:06 +0000568 } else if (isa<UndefValue>(CV)) {
569 Out << "undef";
570
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000571 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Reid Spencer81dfeb32006-12-04 05:19:18 +0000572 Out << CE->getOpcodeName();
573 if (CE->isCompare())
574 Out << " " << getPredicateText(CE->getPredicate());
575 Out << " (";
Misha Brukmanfd939082005-04-21 23:48:37 +0000576
Vikram S. Adveb4dbb442002-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 Lattner2fcfdb72006-12-06 06:24:27 +0000579 WriteAsOperandInternal(Out, *OI, TypeTable, Machine);
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000580 if (OI+1 != CE->op_end())
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000581 Out << ", ";
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000582 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000583
Reid Spencer3da59db2006-11-27 01:05:10 +0000584 if (CE->isCast()) {
Chris Lattner95586b82002-08-15 19:37:43 +0000585 Out << " to ";
586 printTypeInt(Out, CE->getType(), TypeTable);
587 }
Reid Spencer3da59db2006-11-27 01:05:10 +0000588
Misha Brukman40c732c2004-06-04 21:11:51 +0000589 Out << ')';
Chris Lattner95586b82002-08-15 19:37:43 +0000590
Chris Lattner7a716ad2002-04-16 21:36:08 +0000591 } else {
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000592 Out << "<placeholder or erroneous Constant>";
Chris Lattner7a716ad2002-04-16 21:36:08 +0000593 }
594}
595
596
Misha Brukmanab5c6002004-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 Brukmanfd939082005-04-21 23:48:37 +0000601static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
Chris Lattner7b13f562003-05-08 02:08:14 +0000602 std::map<const Type*, std::string> &TypeTable,
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000603 SlotMachine *Machine) {
Misha Brukman40c732c2004-06-04 21:11:51 +0000604 Out << ' ';
Chris Lattner2fcfdb72006-12-06 06:24:27 +0000605 if (V->hasName())
Reid Spencer3702d262007-01-26 08:02:52 +0000606 Out << getLLVMName(V->getName(),
607 isa<GlobalValue>(V) ? GlobalPrefix : LocalPrefix);
Reid Spencer79703962004-07-17 23:47:01 +0000608 else {
609 const Constant *CV = dyn_cast<Constant>(V);
Chris Lattner80cd1152006-01-25 22:26:05 +0000610 if (CV && !isa<GlobalValue>(CV)) {
Chris Lattner2fcfdb72006-12-06 06:24:27 +0000611 WriteConstantInt(Out, CV, TypeTable, Machine);
Chris Lattner80cd1152006-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 Spencer3702d262007-01-26 08:02:52 +0000622 char Prefix = '%';
Chris Lattner7a716ad2002-04-16 21:36:08 +0000623 int Slot;
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000624 if (Machine) {
Reid Spencer3702d262007-01-26 08:02:52 +0000625 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattner22379bc2007-01-11 03:54:27 +0000626 Slot = Machine->getGlobalSlot(GV);
Reid Spencer3702d262007-01-26 08:02:52 +0000627 Prefix = '@';
628 } else {
Chris Lattner22379bc2007-01-11 03:54:27 +0000629 Slot = Machine->getLocalSlot(V);
Reid Spencer3702d262007-01-26 08:02:52 +0000630 }
Chris Lattner7a716ad2002-04-16 21:36:08 +0000631 } else {
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000632 Machine = createSlotMachine(V);
Chris Lattner22379bc2007-01-11 03:54:27 +0000633 if (Machine) {
Reid Spencer3702d262007-01-26 08:02:52 +0000634 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattner22379bc2007-01-11 03:54:27 +0000635 Slot = Machine->getGlobalSlot(GV);
Reid Spencer3702d262007-01-26 08:02:52 +0000636 Prefix = '@';
637 } else {
Chris Lattner22379bc2007-01-11 03:54:27 +0000638 Slot = Machine->getLocalSlot(V);
Reid Spencer3702d262007-01-26 08:02:52 +0000639 }
Chris Lattner22379bc2007-01-11 03:54:27 +0000640 } else {
Chris Lattner69566452004-06-09 19:41:19 +0000641 Slot = -1;
Chris Lattner22379bc2007-01-11 03:54:27 +0000642 }
Reid Spencerb03de0c2004-05-26 21:56:09 +0000643 delete Machine;
Chris Lattner7a716ad2002-04-16 21:36:08 +0000644 }
Chris Lattner69566452004-06-09 19:41:19 +0000645 if (Slot != -1)
Reid Spencer3702d262007-01-26 08:02:52 +0000646 Out << Prefix << Slot;
Chris Lattner69566452004-06-09 19:41:19 +0000647 else
648 Out << "<badref>";
Chris Lattner7a716ad2002-04-16 21:36:08 +0000649 }
650 }
651}
652
Misha Brukman9d0802e2004-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 Lattner31f84992003-11-21 20:23:48 +0000657std::ostream &llvm::WriteAsOperand(std::ostream &Out, const Value *V,
Chris Lattnera6b1ffc2006-12-06 06:15:43 +0000658 bool PrintType, const Module *Context) {
Chris Lattner7b13f562003-05-08 02:08:14 +0000659 std::map<const Type *, std::string> TypeNames;
Chris Lattner607dc682002-07-10 16:48:17 +0000660 if (Context == 0) Context = getModuleFromVal(V);
Chris Lattner207b5bc2001-10-29 16:37:48 +0000661
Chris Lattner6e6026b2002-11-20 18:36:02 +0000662 if (Context)
Chris Lattner607dc682002-07-10 16:48:17 +0000663 fillTypeNameTable(Context, TypeNames);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000664
665 if (PrintType)
666 printTypeInt(Out, V->getType(), TypeNames);
Misha Brukmanfd939082005-04-21 23:48:37 +0000667
Chris Lattner2fcfdb72006-12-06 06:24:27 +0000668 WriteAsOperandInternal(Out, V, TypeNames, 0);
Chris Lattner622f7402001-07-20 19:15:21 +0000669 return Out;
670}
671
Reid Spencer0e25e1c2004-07-04 11:50:43 +0000672
Chris Lattner31f84992003-11-21 20:23:48 +0000673namespace llvm {
Chris Lattnerd8c2e422001-07-12 23:35:26 +0000674
Chris Lattner007377f2001-09-07 16:36:04 +0000675class AssemblyWriter {
Misha Brukman0313e0b2004-06-21 21:53:56 +0000676 std::ostream &Out;
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000677 SlotMachine &Machine;
Chris Lattnerc1824992001-10-29 16:05:51 +0000678 const Module *TheModule;
Chris Lattner7b13f562003-05-08 02:08:14 +0000679 std::map<const Type *, std::string> TypeNames;
Chris Lattner95e5a2c2003-10-30 23:41:03 +0000680 AssemblyAnnotationWriter *AnnotationWriter;
Chris Lattner00950542001-06-06 20:29:01 +0000681public:
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000682 inline AssemblyWriter(std::ostream &o, SlotMachine &Mac, const Module *M,
Chris Lattner95e5a2c2003-10-30 23:41:03 +0000683 AssemblyAnnotationWriter *AAW)
Misha Brukman0313e0b2004-06-21 21:53:56 +0000684 : Out(o), Machine(Mac), TheModule(M), AnnotationWriter(AAW) {
Chris Lattnerc1824992001-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 Lattner207b5bc2001-10-29 16:37:48 +0000689 fillTypeNameTable(M, TypeNames);
Chris Lattner00950542001-06-06 20:29:01 +0000690 }
691
Chris Lattnerc1824992001-10-29 16:05:51 +0000692 inline void write(const Module *M) { printModule(M); }
693 inline void write(const GlobalVariable *G) { printGlobal(G); }
Chris Lattner79df7c02002-03-26 18:01:55 +0000694 inline void write(const Function *F) { printFunction(F); }
Chris Lattnerc1824992001-10-29 16:05:51 +0000695 inline void write(const BasicBlock *BB) { printBasicBlock(BB); }
Chris Lattner7e708292002-06-25 16:13:24 +0000696 inline void write(const Instruction *I) { printInstruction(*I); }
Chris Lattnerda1fbcc2001-11-07 04:21:57 +0000697 inline void write(const Type *Ty) { printType(Ty); }
Chris Lattner00950542001-06-06 20:29:01 +0000698
Chris Lattner2fcfdb72006-12-06 06:24:27 +0000699 void writeOperand(const Value *Op, bool PrintType);
Chris Lattner66e810b2002-04-18 18:53:13 +0000700
Misha Brukman5cf1acf2004-04-28 15:31:21 +0000701 const Module* getModule() { return TheModule; }
702
Misha Brukmanf771bea2004-11-15 19:30:05 +0000703private:
Chris Lattnerc1824992001-10-29 16:05:51 +0000704 void printModule(const Module *M);
Reid Spencer78d033e2007-01-06 07:24:44 +0000705 void printTypeSymbolTable(const TypeSymbolTable &ST);
Chris Lattnerc1824992001-10-29 16:05:51 +0000706 void printGlobal(const GlobalVariable *GV);
Chris Lattner79df7c02002-03-26 18:01:55 +0000707 void printFunction(const Function *F);
Reid Spencerbd5db8e2006-12-31 05:24:50 +0000708 void printArgument(const Argument *FA, FunctionType::ParameterAttributes A);
Chris Lattnerc1824992001-10-29 16:05:51 +0000709 void printBasicBlock(const BasicBlock *BB);
Chris Lattner7e708292002-06-25 16:13:24 +0000710 void printInstruction(const Instruction &I);
Chris Lattner2761e9f2002-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 Lattner7b13f562003-05-08 02:08:14 +0000715 std::ostream &printType(const Type *Ty) {
Misha Brukman0313e0b2004-06-21 21:53:56 +0000716 return printTypeInt(Out, Ty, TypeNames);
Chris Lattner2761e9f2002-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 Lattner7b13f562003-05-08 02:08:14 +0000722 std::ostream &printTypeAtLeastOneLevel(const Type *Ty);
Chris Lattnerc1824992001-10-29 16:05:51 +0000723
Chris Lattnere02fa852001-10-13 06:42:36 +0000724 // printInfoComment - Print a little comment after the instruction indicating
725 // which slot it occupies.
Chris Lattner7e708292002-06-25 16:13:24 +0000726 void printInfoComment(const Value &V);
Chris Lattner00950542001-06-06 20:29:01 +0000727};
Reid Spencer73b74952004-05-27 22:04:46 +0000728} // end of llvm namespace
Chris Lattner00950542001-06-06 20:29:01 +0000729
Misha Brukmanab5c6002004-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 Lattner7b13f562003-05-08 02:08:14 +0000733std::ostream &AssemblyWriter::printTypeAtLeastOneLevel(const Type *Ty) {
Reid Spencera54b7cb2007-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 Spencerbd5db8e2006-12-31 05:24:50 +0000737 printType(FTy->getReturnType());
Reid Spencerbd5db8e2006-12-31 05:24:50 +0000738 Out << " (";
739 unsigned Idx = 1;
Chris Lattnerd5d89962004-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 Brukman0313e0b2004-06-21 21:53:56 +0000743 Out << ", ";
Chris Lattner7a716ad2002-04-16 21:36:08 +0000744 printType(*I);
Reid Spencerbd5db8e2006-12-31 05:24:50 +0000745 if (FTy->getParamAttrs(Idx)) {
746 Out << " " << FunctionType::getParamAttrsText(FTy->getParamAttrs(Idx));
747 }
748 Idx++;
Chris Lattner2761e9f2002-04-13 20:53:41 +0000749 }
750 if (FTy->isVarArg()) {
Misha Brukman0313e0b2004-06-21 21:53:56 +0000751 if (FTy->getNumParams()) Out << ", ";
752 Out << "...";
Chris Lattner2761e9f2002-04-13 20:53:41 +0000753 }
Misha Brukman0313e0b2004-06-21 21:53:56 +0000754 Out << ')';
Reid Spencer2c261782007-01-05 17:06:19 +0000755 if (FTy->getParamAttrs(0))
756 Out << ' ' << FunctionType::getParamAttrsText(FTy->getParamAttrs(0));
Chris Lattner7e708292002-06-25 16:13:24 +0000757 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
Andrew Lenharth38ecbf12006-12-08 18:06:16 +0000758 if (STy->isPacked())
759 Out << '<';
Misha Brukman0313e0b2004-06-21 21:53:56 +0000760 Out << "{ ";
Chris Lattnerd21cd802004-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 Brukman0313e0b2004-06-21 21:53:56 +0000764 Out << ", ";
Chris Lattner2761e9f2002-04-13 20:53:41 +0000765 printType(*I);
766 }
Misha Brukman0313e0b2004-06-21 21:53:56 +0000767 Out << " }";
Andrew Lenharth38ecbf12006-12-08 18:06:16 +0000768 if (STy->isPacked())
769 Out << '>';
Chris Lattner7e708292002-06-25 16:13:24 +0000770 } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
Misha Brukman40c732c2004-06-04 21:11:51 +0000771 printType(PTy->getElementType()) << '*';
Chris Lattner7e708292002-06-25 16:13:24 +0000772 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Misha Brukman0313e0b2004-06-21 21:53:56 +0000773 Out << '[' << ATy->getNumElements() << " x ";
Misha Brukman40c732c2004-06-04 21:11:51 +0000774 printType(ATy->getElementType()) << ']';
Reid Spencer9d6565a2007-02-15 02:26:10 +0000775 } else if (const VectorType *PTy = dyn_cast<VectorType>(Ty)) {
Reid Spencer5527c0b2004-08-20 15:37:30 +0000776 Out << '<' << PTy->getNumElements() << " x ";
777 printType(PTy->getElementType()) << '>';
778 }
Reid Spencer3ed469c2006-11-02 20:25:50 +0000779 else if (isa<OpaqueType>(Ty)) {
Misha Brukman0313e0b2004-06-21 21:53:56 +0000780 Out << "opaque";
Chris Lattner2761e9f2002-04-13 20:53:41 +0000781 } else {
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000782 if (!Ty->isPrimitiveType())
Misha Brukman0313e0b2004-06-21 21:53:56 +0000783 Out << "<unknown derived type>";
Chris Lattner2761e9f2002-04-13 20:53:41 +0000784 printType(Ty);
785 }
Misha Brukman0313e0b2004-06-21 21:53:56 +0000786 return Out;
Chris Lattner2761e9f2002-04-13 20:53:41 +0000787}
788
789
Chris Lattner2fcfdb72006-12-06 06:24:27 +0000790void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType) {
791 if (Operand == 0) {
Chris Lattneraab18202005-02-24 16:58:29 +0000792 Out << "<null operand!>";
Chris Lattner2fcfdb72006-12-06 06:24:27 +0000793 } else {
794 if (PrintType) { Out << ' '; printType(Operand->getType()); }
795 WriteAsOperandInternal(Out, Operand, TypeNames, &Machine);
Chris Lattneraab18202005-02-24 16:58:29 +0000796 }
Chris Lattner00950542001-06-06 20:29:01 +0000797}
798
Chris Lattner00950542001-06-06 20:29:01 +0000799
Chris Lattnerc1824992001-10-29 16:05:51 +0000800void AssemblyWriter::printModule(const Module *M) {
Chris Lattner31ab1b32005-03-02 23:12:40 +0000801 if (!M->getModuleIdentifier().empty() &&
Misha Brukmanfd939082005-04-21 23:48:37 +0000802 // Don't print the ID if it will start a new line (which would
Chris Lattner31ab1b32005-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 Andersoncf7ff2b2006-10-18 02:21:12 +0000807 if (!M->getDataLayout().empty())
Chris Lattnerd2f9e602006-10-22 06:06:56 +0000808 Out << "target datalayout = \"" << M->getDataLayout() << "\"\n";
Reid Spencercddc86f2004-07-25 21:44:54 +0000809 if (!M->getTargetTriple().empty())
Reid Spencerc9a1f0d2004-07-25 21:29:43 +0000810 Out << "target triple = \"" << M->getTargetTriple() << "\"\n";
Misha Brukmanfd939082005-04-21 23:48:37 +0000811
Chris Lattnercc041ba2006-01-24 04:13:11 +0000812 if (!M->getModuleInlineAsm().empty()) {
Chris Lattner42a162e2006-01-24 00:45:30 +0000813 // Split the string into lines, to make it easier to read the .ll file.
Chris Lattnercc041ba2006-01-24 04:13:11 +0000814 std::string Asm = M->getModuleInlineAsm();
Chris Lattner42a162e2006-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 Lattner71cdba32006-01-24 00:40:17 +0000827 Out << "module asm \"";
Chris Lattner42a162e2006-01-24 00:45:30 +0000828 PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.end()), Out);
Chris Lattner18365502006-01-23 23:03:36 +0000829 Out << "\"\n";
830 }
831
Chris Lattner44da7d72004-09-14 05:06:58 +0000832 // Loop over the dependent libraries and emit them.
Chris Lattnercfe97b72004-09-14 04:51:44 +0000833 Module::lib_iterator LI = M->lib_begin();
834 Module::lib_iterator LE = M->lib_end();
Reid Spencercddc86f2004-07-25 21:44:54 +0000835 if (LI != LE) {
Chris Lattnercfe97b72004-09-14 04:51:44 +0000836 Out << "deplibs = [ ";
837 while (LI != LE) {
Chris Lattner44da7d72004-09-14 05:06:58 +0000838 Out << '"' << *LI << '"';
Reid Spencerc9a1f0d2004-07-25 21:29:43 +0000839 ++LI;
Chris Lattnercfe97b72004-09-14 04:51:44 +0000840 if (LI != LE)
841 Out << ", ";
Reid Spencerc9a1f0d2004-07-25 21:29:43 +0000842 }
843 Out << " ]\n";
Reid Spencer83f6a772004-07-25 18:08:18 +0000844 }
Reid Spencere59eaf42004-09-13 23:44:23 +0000845
Chris Lattner44da7d72004-09-14 05:06:58 +0000846 // Loop over the symbol table, emitting all named constants.
Reid Spencer78d033e2007-01-06 07:24:44 +0000847 printTypeSymbolTable(M->getTypeSymbolTable());
Misha Brukmanfd939082005-04-21 23:48:37 +0000848
Chris Lattnerd6d826c2006-12-06 04:41:52 +0000849 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
850 I != E; ++I)
Chris Lattner7e708292002-06-25 16:13:24 +0000851 printGlobal(I);
Chris Lattner007377f2001-09-07 16:36:04 +0000852
Misha Brukman0313e0b2004-06-21 21:53:56 +0000853 Out << "\nimplementation ; Functions:\n";
Misha Brukmanfd939082005-04-21 23:48:37 +0000854
Chris Lattner44da7d72004-09-14 05:06:58 +0000855 // Output all of the functions.
Chris Lattner7e708292002-06-25 16:13:24 +0000856 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
857 printFunction(I);
Chris Lattner007377f2001-09-07 16:36:04 +0000858}
859
Chris Lattnerc1824992001-10-29 16:05:51 +0000860void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Reid Spencer3702d262007-01-26 08:02:52 +0000861 if (GV->hasName()) Out << getLLVMName(GV->getName(), GlobalPrefix) << " = ";
Chris Lattnerd70684f2001-09-18 04:01:05 +0000862
Misha Brukmanfd939082005-04-21 23:48:37 +0000863 if (!GV->hasInitializer())
Anton Korobeynikovb74ed072006-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 Korobeynikov7f705592007-01-12 19:20:47 +0000868 } else {
Chris Lattner4ad02e72003-04-16 20:28:45 +0000869 switch (GV->getLinkage()) {
Anton Korobeynikovb74ed072006-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 Brukman3c8f2c62004-11-14 21:04:34 +0000878 case GlobalValue::GhostLinkage:
Bill Wendlinge8156192006-12-07 01:30:32 +0000879 cerr << "GhostLinkage not allowed in AsmWriter!\n";
Misha Brukman3c8f2c62004-11-14 21:04:34 +0000880 abort();
Chris Lattner4ad02e72003-04-16 20:28:45 +0000881 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000882 switch (GV->getVisibility()) {
Chris Lattner9a40c022007-01-15 18:28:18 +0000883 default: assert(0 && "Invalid visibility style!");
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000884 case GlobalValue::DefaultVisibility: break;
885 case GlobalValue::HiddenVisibility: Out << "hidden "; break;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000886 }
887 }
888
Misha Brukman0313e0b2004-06-21 21:53:56 +0000889 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattner7a176752001-12-04 00:03:30 +0000890 printType(GV->getType()->getElementType());
Chris Lattnerd70684f2001-09-18 04:01:05 +0000891
Reid Spencer79703962004-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 Lattner2fcfdb72006-12-06 06:24:27 +0000895 writeOperand(GV->getInitializer(), false);
Reid Spencer79703962004-07-17 23:47:01 +0000896 }
Chris Lattner30caa282005-11-06 06:48:53 +0000897
Chris Lattner60962db2005-11-12 00:10:19 +0000898 if (GV->hasSection())
899 Out << ", section \"" << GV->getSection() << '"';
900 if (GV->getAlignment())
Chris Lattner30caa282005-11-06 06:48:53 +0000901 Out << ", align " << GV->getAlignment();
Chris Lattner60962db2005-11-12 00:10:19 +0000902
Chris Lattner7e708292002-06-25 16:13:24 +0000903 printInfoComment(*GV);
Misha Brukman0313e0b2004-06-21 21:53:56 +0000904 Out << "\n";
Chris Lattner70cc3392001-09-10 07:58:01 +0000905}
906
Reid Spencer78d033e2007-01-06 07:24:44 +0000907void AssemblyWriter::printTypeSymbolTable(const TypeSymbolTable &ST) {
Reid Spencer9231ac82004-05-25 08:53:40 +0000908 // Print the types.
Reid Spencer78d033e2007-01-06 07:24:44 +0000909 for (TypeSymbolTable::const_iterator TI = ST.begin(), TE = ST.end();
910 TI != TE; ++TI) {
Reid Spencer3702d262007-01-26 08:02:52 +0000911 Out << "\t" << getLLVMName(TI->first, LocalPrefix) << " = type ";
Reid Spencer9231ac82004-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 Spencer78d033e2007-01-06 07:24:44 +0000918}
919
Misha Brukmanab5c6002004-03-02 00:22:19 +0000920/// printFunction - Print all aspects of a function.
921///
Chris Lattner7e708292002-06-25 16:13:24 +0000922void AssemblyWriter::printFunction(const Function *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000923 // Print out the return type and name...
Misha Brukman0313e0b2004-06-21 21:53:56 +0000924 Out << "\n";
Chris Lattner4ad02e72003-04-16 20:28:45 +0000925
Misha Brukman0313e0b2004-06-21 21:53:56 +0000926 if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out);
Chris Lattner95e5a2c2003-10-30 23:41:03 +0000927
Reid Spencer5cbf9852007-01-30 20:08:39 +0000928 if (F->isDeclaration())
Anton Korobeynikovb74ed072006-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 Spencerb951bc02006-12-29 20:29:48 +0000934 else {
935 Out << "define ";
Chris Lattner4ad02e72003-04-16 20:28:45 +0000936 switch (F->getLinkage()) {
Anton Korobeynikovb74ed072006-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 Lattner4ad02e72003-04-16 20:28:45 +0000944 case GlobalValue::ExternalLinkage: break;
Misha Brukman3c8f2c62004-11-14 21:04:34 +0000945 case GlobalValue::GhostLinkage:
Bill Wendlinge8156192006-12-07 01:30:32 +0000946 cerr << "GhostLinkage not allowed in AsmWriter!\n";
Misha Brukman3c8f2c62004-11-14 21:04:34 +0000947 abort();
Chris Lattner4ad02e72003-04-16 20:28:45 +0000948 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000949 switch (F->getVisibility()) {
Chris Lattner9a40c022007-01-15 18:28:18 +0000950 default: assert(0 && "Invalid visibility style!");
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000951 case GlobalValue::DefaultVisibility: break;
952 case GlobalValue::HiddenVisibility: Out << "hidden "; break;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000953 }
Reid Spencerb951bc02006-12-29 20:29:48 +0000954 }
Chris Lattner4ad02e72003-04-16 20:28:45 +0000955
Chris Lattnerd5118982005-05-06 20:26:43 +0000956 // Print the calling convention.
957 switch (F->getCallingConv()) {
958 case CallingConv::C: break; // default
Anton Korobeynikovf8248682006-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 Lattnerd5118982005-05-06 20:26:43 +0000963 default: Out << "cc" << F->getCallingConv() << " "; break;
964 }
965
Reid Spencerbd5db8e2006-12-31 05:24:50 +0000966 const FunctionType *FT = F->getFunctionType();
Misha Brukman40c732c2004-06-04 21:11:51 +0000967 printType(F->getReturnType()) << ' ';
Chris Lattner4d45bd02003-10-18 05:57:43 +0000968 if (!F->getName().empty())
Reid Spencer3702d262007-01-26 08:02:52 +0000969 Out << getLLVMName(F->getName(), GlobalPrefix);
Chris Lattner4d45bd02003-10-18 05:57:43 +0000970 else
Reid Spencer3702d262007-01-26 08:02:52 +0000971 Out << "@\"\"";
Misha Brukman0313e0b2004-06-21 21:53:56 +0000972 Out << '(';
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000973 Machine.incorporateFunction(F);
Chris Lattner007377f2001-09-07 16:36:04 +0000974
Chris Lattnerc1824992001-10-29 16:05:51 +0000975 // Loop over the arguments, printing them...
Chris Lattner007377f2001-09-07 16:36:04 +0000976
Reid Spencerbd5db8e2006-12-31 05:24:50 +0000977 unsigned Idx = 1;
Chris Lattnerd6d826c2006-12-06 04:41:52 +0000978 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
Reid Spencerbd5db8e2006-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 Lattner007377f2001-09-07 16:36:04 +0000985
986 // Finish printing arguments...
Chris Lattner7e708292002-06-25 16:13:24 +0000987 if (FT->isVarArg()) {
Misha Brukman0313e0b2004-06-21 21:53:56 +0000988 if (FT->getNumParams()) Out << ", ";
989 Out << "..."; // Output varargs portion of signature!
Chris Lattner007377f2001-09-07 16:36:04 +0000990 }
Misha Brukman0313e0b2004-06-21 21:53:56 +0000991 Out << ')';
Reid Spencer2c261782007-01-05 17:06:19 +0000992 if (FT->getParamAttrs(0))
993 Out << ' ' << FunctionType::getParamAttrsText(FT->getParamAttrs(0));
Chris Lattner60962db2005-11-12 00:10:19 +0000994 if (F->hasSection())
995 Out << " section \"" << F->getSection() << '"';
Chris Lattner30caa282005-11-06 06:48:53 +0000996 if (F->getAlignment())
997 Out << " align " << F->getAlignment();
Chris Lattner60962db2005-11-12 00:10:19 +0000998
Reid Spencer5cbf9852007-01-30 20:08:39 +0000999 if (F->isDeclaration()) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001000 Out << "\n";
Chris Lattner03e2acb2002-05-06 03:00:40 +00001001 } else {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001002 Out << " {";
Misha Brukmanfd939082005-04-21 23:48:37 +00001003
Chris Lattnerb5794002002-04-07 22:49:37 +00001004 // Output all of its basic blocks... for the function
Chris Lattner7e708292002-06-25 16:13:24 +00001005 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
1006 printBasicBlock(I);
Chris Lattner007377f2001-09-07 16:36:04 +00001007
Misha Brukman0313e0b2004-06-21 21:53:56 +00001008 Out << "}\n";
Chris Lattner007377f2001-09-07 16:36:04 +00001009 }
1010
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001011 Machine.purgeFunction();
Chris Lattner00950542001-06-06 20:29:01 +00001012}
1013
Misha Brukmanab5c6002004-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 Spencerbd5db8e2006-12-31 05:24:50 +00001017void AssemblyWriter::printArgument(const Argument *Arg,
1018 FunctionType::ParameterAttributes attrs) {
Chris Lattner00950542001-06-06 20:29:01 +00001019 // Output type...
Chris Lattnerc1824992001-10-29 16:05:51 +00001020 printType(Arg->getType());
Misha Brukmanfd939082005-04-21 23:48:37 +00001021
Reid Spencerbd5db8e2006-12-31 05:24:50 +00001022 if (attrs != FunctionType::NoAttributeSet)
1023 Out << ' ' << FunctionType::getParamAttrsText(attrs);
1024
Chris Lattner00950542001-06-06 20:29:01 +00001025 // Output name, if available...
1026 if (Arg->hasName())
Reid Spencer3702d262007-01-26 08:02:52 +00001027 Out << ' ' << getLLVMName(Arg->getName(), LocalPrefix);
Chris Lattner00950542001-06-06 20:29:01 +00001028}
1029
Misha Brukmanab5c6002004-03-02 00:22:19 +00001030/// printBasicBlock - This member is called for each basic block in a method.
1031///
Chris Lattnerc1824992001-10-29 16:05:51 +00001032void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Chris Lattner00950542001-06-06 20:29:01 +00001033 if (BB->hasName()) { // Print out the label if it exists...
Reid Spencer3702d262007-01-26 08:02:52 +00001034 Out << "\n" << getLLVMName(BB->getName(), LabelPrefix) << ':';
Chris Lattnerafc38682002-05-14 16:02:05 +00001035 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Misha Brukman0313e0b2004-06-21 21:53:56 +00001036 Out << "\n; <label>:";
Chris Lattner22379bc2007-01-11 03:54:27 +00001037 int Slot = Machine.getLocalSlot(BB);
Chris Lattner69566452004-06-09 19:41:19 +00001038 if (Slot != -1)
Misha Brukman0313e0b2004-06-21 21:53:56 +00001039 Out << Slot;
Chris Lattner69566452004-06-09 19:41:19 +00001040 else
Misha Brukman0313e0b2004-06-21 21:53:56 +00001041 Out << "<badref>";
Chris Lattner061269b2002-10-02 19:38:55 +00001042 }
Chris Lattner4e4d8622003-11-20 00:09:43 +00001043
1044 if (BB->getParent() == 0)
Misha Brukman0313e0b2004-06-21 21:53:56 +00001045 Out << "\t\t; Error: Block without parent!";
Chris Lattner4e4d8622003-11-20 00:09:43 +00001046 else {
1047 if (BB != &BB->getParent()->front()) { // Not the entry block?
1048 // Output predecessors for the block...
Misha Brukman0313e0b2004-06-21 21:53:56 +00001049 Out << "\t\t;";
Chris Lattner4e4d8622003-11-20 00:09:43 +00001050 pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
Misha Brukmanfd939082005-04-21 23:48:37 +00001051
Chris Lattner4e4d8622003-11-20 00:09:43 +00001052 if (PI == PE) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001053 Out << " No predecessors!";
Chris Lattner4e4d8622003-11-20 00:09:43 +00001054 } else {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001055 Out << " preds =";
Chris Lattner2fcfdb72006-12-06 06:24:27 +00001056 writeOperand(*PI, false);
Chris Lattner4e4d8622003-11-20 00:09:43 +00001057 for (++PI; PI != PE; ++PI) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001058 Out << ',';
Chris Lattner2fcfdb72006-12-06 06:24:27 +00001059 writeOperand(*PI, false);
Chris Lattner4e4d8622003-11-20 00:09:43 +00001060 }
Chris Lattner40efcec2003-11-16 22:59:57 +00001061 }
Chris Lattner061269b2002-10-02 19:38:55 +00001062 }
Chris Lattner00950542001-06-06 20:29:01 +00001063 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001064
Misha Brukman0313e0b2004-06-21 21:53:56 +00001065 Out << "\n";
Chris Lattner00950542001-06-06 20:29:01 +00001066
Misha Brukman0313e0b2004-06-21 21:53:56 +00001067 if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out);
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001068
Chris Lattner007377f2001-09-07 16:36:04 +00001069 // Output all of the instructions in the basic block...
Chris Lattner7e708292002-06-25 16:13:24 +00001070 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
1071 printInstruction(*I);
Chris Lattner9f717ef2004-03-08 18:51:45 +00001072
Misha Brukman0313e0b2004-06-21 21:53:56 +00001073 if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out);
Chris Lattner00950542001-06-06 20:29:01 +00001074}
1075
Chris Lattnere02fa852001-10-13 06:42:36 +00001076
Misha Brukmanab5c6002004-03-02 00:22:19 +00001077/// printInfoComment - Print a little comment after the instruction indicating
1078/// which slot it occupies.
1079///
Chris Lattner7e708292002-06-25 16:13:24 +00001080void AssemblyWriter::printInfoComment(const Value &V) {
1081 if (V.getType() != Type::VoidTy) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001082 Out << "\t\t; <";
Misha Brukman40c732c2004-06-04 21:11:51 +00001083 printType(V.getType()) << '>';
Chris Lattnere02fa852001-10-13 06:42:36 +00001084
Chris Lattner7e708292002-06-25 16:13:24 +00001085 if (!V.hasName()) {
Chris Lattner22379bc2007-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 Lattner69566452004-06-09 19:41:19 +00001091 if (SlotNum == -1)
Misha Brukman0313e0b2004-06-21 21:53:56 +00001092 Out << ":<badref>";
Reid Spencerfc621e22004-06-09 15:26:53 +00001093 else
Misha Brukman0313e0b2004-06-21 21:53:56 +00001094 Out << ':' << SlotNum; // Print out the def slot taken.
Chris Lattnere02fa852001-10-13 06:42:36 +00001095 }
Chris Lattner5c461402005-02-01 01:24:01 +00001096 Out << " [#uses=" << V.getNumUses() << ']'; // Output # uses
Chris Lattnere02fa852001-10-13 06:42:36 +00001097 }
1098}
1099
Reid Spencer3a9ec242006-08-28 01:02:49 +00001100// This member is called for each Instruction in a function..
Chris Lattner7e708292002-06-25 16:13:24 +00001101void AssemblyWriter::printInstruction(const Instruction &I) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001102 if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out);
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001103
Misha Brukman0313e0b2004-06-21 21:53:56 +00001104 Out << "\t";
Chris Lattner00950542001-06-06 20:29:01 +00001105
1106 // Print out name if it exists...
Chris Lattner7e708292002-06-25 16:13:24 +00001107 if (I.hasName())
Reid Spencer3702d262007-01-26 08:02:52 +00001108 Out << getLLVMName(I.getName(), LocalPrefix) << " = ";
Chris Lattner00950542001-06-06 20:29:01 +00001109
Chris Lattnerddb6db42005-05-06 05:51:46 +00001110 // If this is a volatile load or store, print out the volatile marker.
Chris Lattnere5e475e2003-09-08 17:45:59 +00001111 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
Chris Lattnerddb6db42005-05-06 05:51:46 +00001112 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile())) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001113 Out << "volatile ";
Chris Lattnerddb6db42005-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 Lattnere5e475e2003-09-08 17:45:59 +00001118
Chris Lattner00950542001-06-06 20:29:01 +00001119 // Print out the opcode...
Misha Brukman0313e0b2004-06-21 21:53:56 +00001120 Out << I.getOpcodeName();
Chris Lattner00950542001-06-06 20:29:01 +00001121
Reid Spencer74f16422006-12-03 06:27:29 +00001122 // Print out the compare instruction predicates
1123 if (const FCmpInst *FCI = dyn_cast<FCmpInst>(&I)) {
Reid Spencer81dfeb32006-12-04 05:19:18 +00001124 Out << " " << getPredicateText(FCI->getPredicate());
Reid Spencer74f16422006-12-03 06:27:29 +00001125 } else if (const ICmpInst *ICI = dyn_cast<ICmpInst>(&I)) {
Reid Spencer81dfeb32006-12-04 05:19:18 +00001126 Out << " " << getPredicateText(ICI->getPredicate());
Reid Spencer74f16422006-12-03 06:27:29 +00001127 }
1128
Chris Lattner00950542001-06-06 20:29:01 +00001129 // Print out the type of the operands...
Chris Lattner7e708292002-06-25 16:13:24 +00001130 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
Chris Lattner00950542001-06-06 20:29:01 +00001131
1132 // Special case conditional branches to swizzle the condition out to the front
Chris Lattner7e708292002-06-25 16:13:24 +00001133 if (isa<BranchInst>(I) && I.getNumOperands() > 1) {
1134 writeOperand(I.getOperand(2), true);
Misha Brukman0313e0b2004-06-21 21:53:56 +00001135 Out << ',';
Chris Lattner00950542001-06-06 20:29:01 +00001136 writeOperand(Operand, true);
Misha Brukman0313e0b2004-06-21 21:53:56 +00001137 Out << ',';
Chris Lattner7e708292002-06-25 16:13:24 +00001138 writeOperand(I.getOperand(1), true);
Chris Lattner00950542001-06-06 20:29:01 +00001139
Chris Lattner94dc1f22002-04-13 18:34:38 +00001140 } else if (isa<SwitchInst>(I)) {
Chris Lattner00950542001-06-06 20:29:01 +00001141 // Special case switch statement to get formatting nice and correct...
Misha Brukman0313e0b2004-06-21 21:53:56 +00001142 writeOperand(Operand , true); Out << ',';
1143 writeOperand(I.getOperand(1), true); Out << " [";
Chris Lattner00950542001-06-06 20:29:01 +00001144
Chris Lattner7e708292002-06-25 16:13:24 +00001145 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001146 Out << "\n\t\t";
1147 writeOperand(I.getOperand(op ), true); Out << ',';
Chris Lattner7e708292002-06-25 16:13:24 +00001148 writeOperand(I.getOperand(op+1), true);
Chris Lattner00950542001-06-06 20:29:01 +00001149 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00001150 Out << "\n\t]";
Chris Lattnerb00c5822001-10-02 03:41:24 +00001151 } else if (isa<PHINode>(I)) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001152 Out << ' ';
Chris Lattner7e708292002-06-25 16:13:24 +00001153 printType(I.getType());
Misha Brukman0313e0b2004-06-21 21:53:56 +00001154 Out << ' ';
Chris Lattner00950542001-06-06 20:29:01 +00001155
Chris Lattner7e708292002-06-25 16:13:24 +00001156 for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001157 if (op) Out << ", ";
Misha Brukmanfd939082005-04-21 23:48:37 +00001158 Out << '[';
Misha Brukman0313e0b2004-06-21 21:53:56 +00001159 writeOperand(I.getOperand(op ), false); Out << ',';
1160 writeOperand(I.getOperand(op+1), false); Out << " ]";
Chris Lattnerc24d2082001-06-11 15:04:20 +00001161 }
Chris Lattnere02fa852001-10-13 06:42:36 +00001162 } else if (isa<ReturnInst>(I) && !Operand) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001163 Out << " void";
Chris Lattnerd5118982005-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 Lattner0deaab82006-05-19 21:58:52 +00001168 case CallingConv::Fast: Out << " fastcc"; break;
1169 case CallingConv::Cold: Out << " coldcc"; break;
Anton Korobeynikovf8248682006-09-20 22:03:51 +00001170 case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break;
1171 case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break;
Chris Lattnerd5118982005-05-06 20:26:43 +00001172 default: Out << " cc" << CI->getCallingConv(); break;
1173 }
1174
Chris Lattner7a012292003-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 Lattner268de042001-11-06 21:28:12 +00001178
Chris Lattner7a012292003-08-05 15:34:45 +00001179 // If possible, print out the short form of the call instruction. We can
Chris Lattnerb5794002002-04-07 22:49:37 +00001180 // only do this if the first argument is a pointer to a nonvararg function,
Chris Lattner7a012292003-08-05 15:34:45 +00001181 // and if the return type is not a pointer to a function.
Chris Lattner268de042001-11-06 21:28:12 +00001182 //
Chris Lattner7a012292003-08-05 15:34:45 +00001183 if (!FTy->isVarArg() &&
Misha Brukmanfd939082005-04-21 23:48:37 +00001184 (!isa<PointerType>(RetTy) ||
Chris Lattnerc1b27182002-07-25 20:58:51 +00001185 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001186 Out << ' '; printType(RetTy);
Chris Lattner268de042001-11-06 21:28:12 +00001187 writeOperand(Operand, false);
1188 } else {
1189 writeOperand(Operand, true);
1190 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00001191 Out << '(';
Reid Spencerbd5db8e2006-12-31 05:24:50 +00001192 for (unsigned op = 1, Eop = I.getNumOperands(); op < Eop; ++op) {
1193 if (op > 1)
1194 Out << ',';
Chris Lattner7e708292002-06-25 16:13:24 +00001195 writeOperand(I.getOperand(op), true);
Reid Spencerbd5db8e2006-12-31 05:24:50 +00001196 if (FTy->getParamAttrs(op) != FunctionType::NoAttributeSet)
1197 Out << " " << FTy->getParamAttrsText(FTy->getParamAttrs(op));
Chris Lattner00950542001-06-06 20:29:01 +00001198 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00001199 Out << " )";
Reid Spencer2c261782007-01-05 17:06:19 +00001200 if (FTy->getParamAttrs(0) != FunctionType::NoAttributeSet)
1201 Out << ' ' << FTy->getParamAttrsText(FTy->getParamAttrs(0));
Chris Lattner7e708292002-06-25 16:13:24 +00001202 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Chris Lattner7a012292003-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 Lattnerd5118982005-05-06 20:26:43 +00001207 // Print the calling convention being used.
1208 switch (II->getCallingConv()) {
1209 case CallingConv::C: break; // default
Chris Lattner0deaab82006-05-19 21:58:52 +00001210 case CallingConv::Fast: Out << " fastcc"; break;
1211 case CallingConv::Cold: Out << " coldcc"; break;
Anton Korobeynikovf8248682006-09-20 22:03:51 +00001212 case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break;
1213 case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break;
Chris Lattnerd5118982005-05-06 20:26:43 +00001214 default: Out << " cc" << II->getCallingConv(); break;
1215 }
1216
Chris Lattner7a012292003-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 Brukmanfd939082005-04-21 23:48:37 +00001222 (!isa<PointerType>(RetTy) ||
Chris Lattner7a012292003-08-05 15:34:45 +00001223 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001224 Out << ' '; printType(RetTy);
Chris Lattner7a012292003-08-05 15:34:45 +00001225 writeOperand(Operand, false);
1226 } else {
1227 writeOperand(Operand, true);
1228 }
1229
Misha Brukman0313e0b2004-06-21 21:53:56 +00001230 Out << '(';
Reid Spencerbd5db8e2006-12-31 05:24:50 +00001231 for (unsigned op = 3, Eop = I.getNumOperands(); op < Eop; ++op) {
1232 if (op > 3)
1233 Out << ',';
Chris Lattner7e708292002-06-25 16:13:24 +00001234 writeOperand(I.getOperand(op), true);
Reid Spencer63596182006-12-31 22:17:01 +00001235 if (FTy->getParamAttrs(op-2) != FunctionType::NoAttributeSet)
1236 Out << " " << FTy->getParamAttrsText(FTy->getParamAttrs(op-2));
Chris Lattnere02fa852001-10-13 06:42:36 +00001237 }
1238
Reid Spencer2c261782007-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 Lattnere02fa852001-10-13 06:42:36 +00001243 writeOperand(II->getNormalDest(), true);
Misha Brukman0313e0b2004-06-21 21:53:56 +00001244 Out << " unwind";
Chris Lattneraeb2a1d2004-02-08 21:44:31 +00001245 writeOperand(II->getUnwindDest(), true);
Chris Lattnere02fa852001-10-13 06:42:36 +00001246
Chris Lattner7e708292002-06-25 16:13:24 +00001247 } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(&I)) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001248 Out << ' ';
Chris Lattner94dc1f22002-04-13 18:34:38 +00001249 printType(AI->getType()->getElementType());
1250 if (AI->isArrayAllocation()) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001251 Out << ',';
Chris Lattner94dc1f22002-04-13 18:34:38 +00001252 writeOperand(AI->getArraySize(), true);
Chris Lattner00950542001-06-06 20:29:01 +00001253 }
Nate Begeman14b05292005-11-05 09:21:28 +00001254 if (AI->getAlignment()) {
Chris Lattner9fad0b92005-11-05 21:20:34 +00001255 Out << ", align " << AI->getAlignment();
Nate Begeman14b05292005-11-05 09:21:28 +00001256 }
Chris Lattnere02fa852001-10-13 06:42:36 +00001257 } else if (isa<CastInst>(I)) {
Chris Lattner41495a22003-11-17 01:17:04 +00001258 if (Operand) writeOperand(Operand, true); // Work with broken code
Misha Brukman0313e0b2004-06-21 21:53:56 +00001259 Out << " to ";
Chris Lattner7e708292002-06-25 16:13:24 +00001260 printType(I.getType());
Chris Lattner4d45bd02003-10-18 05:57:43 +00001261 } else if (isa<VAArgInst>(I)) {
Chris Lattner41495a22003-11-17 01:17:04 +00001262 if (Operand) writeOperand(Operand, true); // Work with broken code
Misha Brukman0313e0b2004-06-21 21:53:56 +00001263 Out << ", ";
Chris Lattner8f77dae2003-05-08 02:44:12 +00001264 printType(I.getType());
Chris Lattner00950542001-06-06 20:29:01 +00001265 } else if (Operand) { // Print the normal way...
1266
Misha Brukmanfd939082005-04-21 23:48:37 +00001267 // PrintAllTypes - Instructions who have operands of all the same type
Chris Lattner00950542001-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 Lattner00950542001-06-06 20:29:01 +00001272
Reid Spencerebe57e32007-02-02 13:54:55 +00001273 // Select, Store and ShuffleVector always print all types.
Reid Spencer832254e2007-02-02 02:16:23 +00001274 if (isa<SelectInst>(I) || isa<StoreInst>(I) || isa<ShuffleVectorInst>(I)) {
Chris Lattnerffd9bf42003-04-16 20:20:02 +00001275 PrintAllTypes = true;
1276 } else {
1277 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
1278 Operand = I.getOperand(i);
1279 if (Operand->getType() != TheType) {
1280 PrintAllTypes = true; // We have differing types! Print them all!
1281 break;
1282 }
Chris Lattner00950542001-06-06 20:29:01 +00001283 }
1284 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001285
Chris Lattnerc1824992001-10-29 16:05:51 +00001286 if (!PrintAllTypes) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001287 Out << ' ';
Chris Lattnerffd9bf42003-04-16 20:20:02 +00001288 printType(TheType);
Chris Lattnerc1824992001-10-29 16:05:51 +00001289 }
Chris Lattner00950542001-06-06 20:29:01 +00001290
Chris Lattner7e708292002-06-25 16:13:24 +00001291 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001292 if (i) Out << ',';
Chris Lattner7e708292002-06-25 16:13:24 +00001293 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner00950542001-06-06 20:29:01 +00001294 }
1295 }
1296
Chris Lattnere02fa852001-10-13 06:42:36 +00001297 printInfoComment(I);
Misha Brukman0313e0b2004-06-21 21:53:56 +00001298 Out << "\n";
Chris Lattner00950542001-06-06 20:29:01 +00001299}
1300
1301
1302//===----------------------------------------------------------------------===//
1303// External Interface declarations
1304//===----------------------------------------------------------------------===//
1305
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001306void Module::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001307 SlotMachine SlotTable(this);
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001308 AssemblyWriter W(o, SlotTable, this, AAW);
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001309 W.write(this);
Chris Lattner00950542001-06-06 20:29:01 +00001310}
1311
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001312void GlobalVariable::print(std::ostream &o) const {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001313 SlotMachine SlotTable(getParent());
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001314 AssemblyWriter W(o, SlotTable, getParent(), 0);
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001315 W.write(this);
Chris Lattnerb0e45232001-09-10 20:08:19 +00001316}
1317
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001318void Function::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001319 SlotMachine SlotTable(getParent());
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001320 AssemblyWriter W(o, SlotTable, getParent(), AAW);
Chris Lattner00950542001-06-06 20:29:01 +00001321
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001322 W.write(this);
Chris Lattner00950542001-06-06 20:29:01 +00001323}
1324
Chris Lattnercc041ba2006-01-24 04:13:11 +00001325void InlineAsm::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Chris Lattner2fcfdb72006-12-06 06:24:27 +00001326 WriteAsOperand(o, this, true, 0);
Chris Lattnercc041ba2006-01-24 04:13:11 +00001327}
1328
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001329void BasicBlock::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001330 SlotMachine SlotTable(getParent());
Misha Brukmanfd939082005-04-21 23:48:37 +00001331 AssemblyWriter W(o, SlotTable,
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001332 getParent() ? getParent()->getParent() : 0, AAW);
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001333 W.write(this);
Chris Lattner00950542001-06-06 20:29:01 +00001334}
1335
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001336void Instruction::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001337 const Function *F = getParent() ? getParent()->getParent() : 0;
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001338 SlotMachine SlotTable(F);
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001339 AssemblyWriter W(o, SlotTable, F ? F->getParent() : 0, AAW);
Chris Lattner00950542001-06-06 20:29:01 +00001340
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001341 W.write(this);
Chris Lattner00950542001-06-06 20:29:01 +00001342}
Chris Lattnerda1fbcc2001-11-07 04:21:57 +00001343
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001344void Constant::print(std::ostream &o) const {
1345 if (this == 0) { o << "<null> constant value\n"; return; }
Chris Lattner3bc06b32002-09-10 15:53:49 +00001346
Misha Brukman40c732c2004-06-04 21:11:51 +00001347 o << ' ' << getType()->getDescription() << ' ';
Evan Chenga4ffcc22006-03-01 22:17:00 +00001348
1349 std::map<const Type *, std::string> TypeTable;
Chris Lattner2fcfdb72006-12-06 06:24:27 +00001350 WriteConstantInt(o, this, TypeTable, 0);
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001351}
1352
Misha Brukmanfd939082005-04-21 23:48:37 +00001353void Type::print(std::ostream &o) const {
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001354 if (this == 0)
1355 o << "<null Type>";
1356 else
1357 o << getDescription();
1358}
1359
Chris Lattner73e21422002-04-09 19:48:49 +00001360void Argument::print(std::ostream &o) const {
Chris Lattner2fcfdb72006-12-06 06:24:27 +00001361 WriteAsOperand(o, this, true, getParent() ? getParent()->getParent() : 0);
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001362}
1363
Reid Spencerfa452c02004-05-25 18:14:38 +00001364// Value::dump - allow easy printing of Values from the debugger.
1365// Located here because so much of the needed functionality is here.
Bill Wendling832171c2006-12-07 20:04:42 +00001366void Value::dump() const { print(*cerr.stream()); cerr << '\n'; }
Reid Spencerfa452c02004-05-25 18:14:38 +00001367
1368// Type::dump - allow easy printing of Values from the debugger.
1369// Located here because so much of the needed functionality is here.
Bill Wendling832171c2006-12-07 20:04:42 +00001370void Type::dump() const { print(*cerr.stream()); cerr << '\n'; }
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001371
1372//===----------------------------------------------------------------------===//
Chris Lattner82c4bc72006-12-06 06:40:49 +00001373// SlotMachine Implementation
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001374//===----------------------------------------------------------------------===//
1375
1376#if 0
Bill Wendlinge8156192006-12-07 01:30:32 +00001377#define SC_DEBUG(X) cerr << X
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001378#else
1379#define SC_DEBUG(X)
1380#endif
1381
1382// Module level constructor. Causes the contents of the Module (sans functions)
1383// to be added to the slot table.
Misha Brukmanfd939082005-04-21 23:48:37 +00001384SlotMachine::SlotMachine(const Module *M)
Reid Spencerb03de0c2004-05-26 21:56:09 +00001385 : TheModule(M) ///< Saved for lazy initialization.
1386 , TheFunction(0)
Reid Spencer28531c72004-08-16 07:46:33 +00001387 , FunctionProcessed(false)
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001388{
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001389}
1390
1391// Function level constructor. Causes the contents of the Module and the one
1392// function provided to be added to the slot table.
Chris Lattnerc96ce892006-12-06 05:12:21 +00001393SlotMachine::SlotMachine(const Function *F)
1394 : TheModule(F ? F->getParent() : 0) ///< Saved for lazy initialization
Reid Spencerb03de0c2004-05-26 21:56:09 +00001395 , TheFunction(F) ///< Saved for lazy initialization
Reid Spencer28531c72004-08-16 07:46:33 +00001396 , FunctionProcessed(false)
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001397{
Reid Spencerb03de0c2004-05-26 21:56:09 +00001398}
1399
Chris Lattner22379bc2007-01-11 03:54:27 +00001400inline void SlotMachine::initialize() {
Chris Lattnerc96ce892006-12-06 05:12:21 +00001401 if (TheModule) {
Misha Brukmanfd939082005-04-21 23:48:37 +00001402 processModule();
Reid Spencerb03de0c2004-05-26 21:56:09 +00001403 TheModule = 0; ///< Prevent re-processing next time we're called.
1404 }
Chris Lattnerde891a62006-12-06 05:27:40 +00001405 if (TheFunction && !FunctionProcessed)
Misha Brukmanfd939082005-04-21 23:48:37 +00001406 processFunction();
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001407}
1408
1409// Iterate through all the global variables, functions, and global
Misha Brukmanfd939082005-04-21 23:48:37 +00001410// variable initializers and create slots for them.
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001411void SlotMachine::processModule() {
1412 SC_DEBUG("begin processModule!\n");
1413
Chris Lattnera6b1ffc2006-12-06 06:15:43 +00001414 // Add all of the unnamed global variables to the value table.
Chris Lattnerd6d826c2006-12-06 04:41:52 +00001415 for (Module::const_global_iterator I = TheModule->global_begin(),
1416 E = TheModule->global_end(); I != E; ++I)
Chris Lattnera6b1ffc2006-12-06 06:15:43 +00001417 if (!I->hasName())
Chris Lattner9446bbe2007-01-09 07:55:49 +00001418 CreateModuleSlot(I);
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001419
Chris Lattnera6b1ffc2006-12-06 06:15:43 +00001420 // Add all the unnamed functions to the table.
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001421 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
1422 I != E; ++I)
Chris Lattnera6b1ffc2006-12-06 06:15:43 +00001423 if (!I->hasName())
Chris Lattner9446bbe2007-01-09 07:55:49 +00001424 CreateModuleSlot(I);
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001425
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001426 SC_DEBUG("end processModule!\n");
1427}
1428
1429
Reid Spencerb03de0c2004-05-26 21:56:09 +00001430// Process the arguments, basic blocks, and instructions of a function.
1431void SlotMachine::processFunction() {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001432 SC_DEBUG("begin processFunction!\n");
1433
Chris Lattnera6b1ffc2006-12-06 06:15:43 +00001434 // Add all the function arguments with no names.
Misha Brukmanfd939082005-04-21 23:48:37 +00001435 for(Function::const_arg_iterator AI = TheFunction->arg_begin(),
Chris Lattnere4d5c442005-03-15 04:54:21 +00001436 AE = TheFunction->arg_end(); AI != AE; ++AI)
Chris Lattnera6b1ffc2006-12-06 06:15:43 +00001437 if (!AI->hasName())
Chris Lattner9446bbe2007-01-09 07:55:49 +00001438 CreateFunctionSlot(AI);
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001439
1440 SC_DEBUG("Inserting Instructions:\n");
1441
Chris Lattnera6b1ffc2006-12-06 06:15:43 +00001442 // Add all of the basic blocks and instructions with no names.
Misha Brukmanfd939082005-04-21 23:48:37 +00001443 for (Function::const_iterator BB = TheFunction->begin(),
Reid Spencerb03de0c2004-05-26 21:56:09 +00001444 E = TheFunction->end(); BB != E; ++BB) {
Chris Lattnera6b1ffc2006-12-06 06:15:43 +00001445 if (!BB->hasName())
Chris Lattner9446bbe2007-01-09 07:55:49 +00001446 CreateFunctionSlot(BB);
Chris Lattnera6b1ffc2006-12-06 06:15:43 +00001447 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
1448 if (I->getType() != Type::VoidTy && !I->hasName())
Chris Lattner9446bbe2007-01-09 07:55:49 +00001449 CreateFunctionSlot(I);
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001450 }
1451
Reid Spencer28531c72004-08-16 07:46:33 +00001452 FunctionProcessed = true;
1453
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001454 SC_DEBUG("end processFunction!\n");
1455}
1456
Chris Lattnerde891a62006-12-06 05:27:40 +00001457/// Clean up after incorporating a function. This is the only way to get out of
Chris Lattner22379bc2007-01-11 03:54:27 +00001458/// the function incorporation state that affects get*Slot/Create*Slot. Function
Chris Lattner3a4621c2007-01-09 07:46:21 +00001459/// incorporation state is indicated by TheFunction != 0.
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001460void SlotMachine::purgeFunction() {
1461 SC_DEBUG("begin purgeFunction!\n");
1462 fMap.clear(); // Simply discard the function level map
Reid Spencerb03de0c2004-05-26 21:56:09 +00001463 TheFunction = 0;
Reid Spencer28531c72004-08-16 07:46:33 +00001464 FunctionProcessed = false;
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001465 SC_DEBUG("end purgeFunction!\n");
1466}
1467
Chris Lattner22379bc2007-01-11 03:54:27 +00001468/// getGlobalSlot - Get the slot number of a global value.
1469int SlotMachine::getGlobalSlot(const GlobalValue *V) {
1470 // Check for uninitialized state and do lazy initialization.
1471 initialize();
1472
1473 // Find the type plane in the module map
1474 TypedPlanes::const_iterator MI = mMap.find(V->getType());
1475 if (MI == mMap.end()) return -1;
1476
1477 // Lookup the value in the module plane's map.
1478 ValueMap::const_iterator MVI = MI->second.map.find(V);
Reid Spencer11b910c2007-01-11 07:58:19 +00001479 return MVI != MI->second.map.end() ? int(MVI->second) : -1;
Chris Lattner22379bc2007-01-11 03:54:27 +00001480}
Reid Spencerb03de0c2004-05-26 21:56:09 +00001481
Chris Lattner22379bc2007-01-11 03:54:27 +00001482
1483/// getLocalSlot - Get the slot number for a value that is local to a function.
1484int SlotMachine::getLocalSlot(const Value *V) {
1485 assert(!isa<Constant>(V) && "Can't get a constant or global slot with this!");
1486
1487 // Check for uninitialized state and do lazy initialization.
1488 initialize();
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001489
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001490 // Get the type of the value
Chris Lattner22379bc2007-01-11 03:54:27 +00001491 const Type *VTy = V->getType();
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001492
Chris Lattner22379bc2007-01-11 03:54:27 +00001493 TypedPlanes::const_iterator FI = fMap.find(VTy);
1494 if (FI == fMap.end()) return -1;
1495
1496 // Lookup the Value in the function and module maps.
1497 ValueMap::const_iterator FVI = FI->second.map.find(V);
Chris Lattner22379bc2007-01-11 03:54:27 +00001498
Chris Lattnerac618e62007-01-11 04:30:21 +00001499 // If the value doesn't exist in the function map, it is a <badref>
1500 if (FVI == FI->second.map.end()) return -1;
Chris Lattner22379bc2007-01-11 03:54:27 +00001501
Reid Spencer3702d262007-01-26 08:02:52 +00001502 return FVI->second;
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001503}
1504
Reid Spencer0e25e1c2004-07-04 11:50:43 +00001505
Chris Lattner9446bbe2007-01-09 07:55:49 +00001506/// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
1507void SlotMachine::CreateModuleSlot(const GlobalValue *V) {
Chris Lattnercf8790a2007-01-09 08:04:59 +00001508 assert(V && "Can't insert a null Value into SlotMachine!");
1509
1510 unsigned DestSlot = 0;
1511 const Type *VTy = V->getType();
1512
Chris Lattner55e73a52007-01-10 06:43:26 +00001513 ValuePlane &PlaneMap = mMap[VTy];
1514 DestSlot = PlaneMap.map[V] = PlaneMap.next_slot++;
Chris Lattnercf8790a2007-01-09 08:04:59 +00001515
1516 SC_DEBUG(" Inserting value [" << VTy << "] = " << V << " slot=" <<
1517 DestSlot << " [");
1518 // G = Global, F = Function, o = other
1519 SC_DEBUG((isa<GlobalVariable>(V) ? 'G' : 'F') << "]\n");
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001520}
1521
1522
Chris Lattner9446bbe2007-01-09 07:55:49 +00001523/// CreateSlot - Create a new slot for the specified value if it has no name.
1524void SlotMachine::CreateFunctionSlot(const Value *V) {
1525 const Type *VTy = V->getType();
1526 assert(VTy != Type::VoidTy && !V->hasName() && "Doesn't need a slot!");
Chris Lattnercf8790a2007-01-09 08:04:59 +00001527
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001528 unsigned DestSlot = 0;
Chris Lattnercf8790a2007-01-09 08:04:59 +00001529
Chris Lattner55e73a52007-01-10 06:43:26 +00001530 ValuePlane &PlaneMap = fMap[VTy];
1531 DestSlot = PlaneMap.map[V] = PlaneMap.next_slot++;
Chris Lattnercf8790a2007-01-09 08:04:59 +00001532
Chris Lattner4932a5a2006-12-06 05:55:41 +00001533 // G = Global, F = Function, o = other
Chris Lattnercf8790a2007-01-09 08:04:59 +00001534 SC_DEBUG(" Inserting value [" << VTy << "] = " << V << " slot=" <<
1535 DestSlot << " [o]\n");
1536}