blob: 26119d14d132349fc30553fb2b088d86ab9b92e9 [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//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha 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"
Reid Spencerb138a062007-04-09 06:10:42 +000023#include "llvm/ParameterAttributes.h"
Chris Lattner863517a2006-01-25 18:57:27 +000024#include "llvm/InlineAsm.h"
Vikram S. Adveb4dbb442002-07-14 23:14:45 +000025#include "llvm/Instruction.h"
Misha Brukman44336292004-07-29 16:53:53 +000026#include "llvm/Instructions.h"
Chris Lattnerf2d577b2004-01-20 19:50:34 +000027#include "llvm/Module.h"
Reid Spenceref9b9a72007-02-05 20:47:22 +000028#include "llvm/ValueSymbolTable.h"
Reid Spencer78d033e2007-01-06 07:24:44 +000029#include "llvm/TypeSymbolTable.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000030#include "llvm/ADT/StringExtras.h"
31#include "llvm/ADT/STLExtras.h"
Bill Wendling8f487662006-11-28 02:09:03 +000032#include "llvm/Support/CFG.h"
Jim Laskeycb6682f2005-08-17 19:34:49 +000033#include "llvm/Support/MathExtras.h"
Bill Wendling8f487662006-11-28 02:09:03 +000034#include "llvm/Support/Streams.h"
Chris Lattner007377f2001-09-07 16:36:04 +000035#include <algorithm>
Reid Spencer4ad513c2007-05-22 19:27:35 +000036#include <cctype>
Chris Lattner31f84992003-11-21 20:23:48 +000037using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000038
Chris Lattner0a8e8e12004-07-15 02:51:31 +000039namespace llvm {
Reid Spencer0d1b77e2004-05-26 07:18:52 +000040
Reid Spenceredd5d9e2005-05-15 16:13:11 +000041// Make virtual table appear in this compilation unit.
42AssemblyAnnotationWriter::~AssemblyAnnotationWriter() {}
43
Reid Spencer0d1b77e2004-05-26 07:18:52 +000044/// This class provides computation of slot numbers for LLVM Assembly writing.
45/// @brief LLVM Assembly Writing Slot Computation.
46class SlotMachine {
47
48/// @name Types
49/// @{
50public:
51
52 /// @brief A mapping of Values to slot numbers
Reid Spencer590b3c52007-03-19 18:32:53 +000053 typedef std::map<const Value*,unsigned> ValueMap;
Reid Spencer0d1b77e2004-05-26 07:18:52 +000054
55/// @}
56/// @name Constructors
57/// @{
58public:
59 /// @brief Construct from a module
Chris Lattnerc96ce892006-12-06 05:12:21 +000060 SlotMachine(const Module *M);
Reid Spencer0d1b77e2004-05-26 07:18:52 +000061
62 /// @brief Construct from a function, starting out in incorp state.
Chris Lattnerc96ce892006-12-06 05:12:21 +000063 SlotMachine(const Function *F);
Reid Spencer0d1b77e2004-05-26 07:18:52 +000064
65/// @}
66/// @name Accessors
67/// @{
68public:
69 /// Return the slot number of the specified value in it's type
Chris Lattner22379bc2007-01-11 03:54:27 +000070 /// plane. If something is not in the SlotMachine, return -1.
71 int getLocalSlot(const Value *V);
72 int getGlobalSlot(const GlobalValue *V);
Reid Spencerfc621e22004-06-09 15:26:53 +000073
Reid Spencer0d1b77e2004-05-26 07:18:52 +000074/// @}
75/// @name Mutators
76/// @{
77public:
Misha Brukmanfd939082005-04-21 23:48:37 +000078 /// If you'd like to deal with a function instead of just a module, use
Reid Spencer0d1b77e2004-05-26 07:18:52 +000079 /// this method to get its data into the SlotMachine.
Misha Brukmanfd939082005-04-21 23:48:37 +000080 void incorporateFunction(const Function *F) {
81 TheFunction = F;
Reid Spencer28531c72004-08-16 07:46:33 +000082 FunctionProcessed = false;
83 }
Reid Spencer0d1b77e2004-05-26 07:18:52 +000084
Misha Brukmanfd939082005-04-21 23:48:37 +000085 /// After calling incorporateFunction, use this method to remove the
86 /// most recently incorporated function from the SlotMachine. This
Reid Spencer0d1b77e2004-05-26 07:18:52 +000087 /// will reset the state of the machine back to just the module contents.
88 void purgeFunction();
89
90/// @}
91/// @name Implementation Details
92/// @{
93private:
Reid Spencerb03de0c2004-05-26 21:56:09 +000094 /// This function does the actual initialization.
95 inline void initialize();
96
Chris Lattner9446bbe2007-01-09 07:55:49 +000097 /// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
98 void CreateModuleSlot(const GlobalValue *V);
99
100 /// CreateFunctionSlot - Insert the specified Value* into the slot table.
101 void CreateFunctionSlot(const Value *V);
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000102
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000103 /// Add all of the module level global variables (and their initializers)
104 /// and function declarations, but not the contents of those functions.
105 void processModule();
106
Reid Spencerb03de0c2004-05-26 21:56:09 +0000107 /// Add all of the functions arguments, basic blocks, and instructions
108 void processFunction();
109
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000110 SlotMachine(const SlotMachine &); // DO NOT IMPLEMENT
111 void operator=(const SlotMachine &); // DO NOT IMPLEMENT
112
113/// @}
114/// @name Data
115/// @{
116public:
117
118 /// @brief The module for which we are holding slot numbers
Reid Spencerb03de0c2004-05-26 21:56:09 +0000119 const Module* TheModule;
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000120
Reid Spencerb03de0c2004-05-26 21:56:09 +0000121 /// @brief The function for which we are holding slot numbers
122 const Function* TheFunction;
Reid Spencer28531c72004-08-16 07:46:33 +0000123 bool FunctionProcessed;
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000124
125 /// @brief The TypePlanes map for the module level data
Reid Spencer590b3c52007-03-19 18:32:53 +0000126 ValueMap mMap;
127 unsigned mNext;
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000128
129 /// @brief The TypePlanes map for the function level data
Reid Spencer590b3c52007-03-19 18:32:53 +0000130 ValueMap fMap;
131 unsigned fNext;
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000132
133/// @}
134
135};
136
Chris Lattner0a8e8e12004-07-15 02:51:31 +0000137} // end namespace llvm
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000138
Devang Patel19974732007-05-03 01:11:54 +0000139char PrintModulePass::ID = 0;
Chris Lattner7f8897f2006-08-27 22:42:52 +0000140static RegisterPass<PrintModulePass>
Chris Lattner3dd965c2006-08-21 17:20:01 +0000141X("printm", "Print module to stderr");
Devang Patel19974732007-05-03 01:11:54 +0000142char PrintFunctionPass::ID = 0;
Chris Lattner7f8897f2006-08-27 22:42:52 +0000143static RegisterPass<PrintFunctionPass>
Chris Lattner3dd965c2006-08-21 17:20:01 +0000144Y("print","Print function to stderr");
Chris Lattnerf082b802002-07-23 18:07:49 +0000145
Misha Brukmanfd939082005-04-21 23:48:37 +0000146static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
Chris Lattner919e70c2006-12-06 05:50:41 +0000147 std::map<const Type *, std::string> &TypeTable,
Reid Spencer0e25e1c2004-07-04 11:50:43 +0000148 SlotMachine *Machine);
149
Chris Lattner207b5bc2001-10-29 16:37:48 +0000150static const Module *getModuleFromVal(const Value *V) {
Chris Lattner949a3622003-07-23 15:30:06 +0000151 if (const Argument *MA = dyn_cast<Argument>(V))
Chris Lattner207b5bc2001-10-29 16:37:48 +0000152 return MA->getParent() ? MA->getParent()->getParent() : 0;
Chris Lattner949a3622003-07-23 15:30:06 +0000153 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
Chris Lattner207b5bc2001-10-29 16:37:48 +0000154 return BB->getParent() ? BB->getParent()->getParent() : 0;
Chris Lattner949a3622003-07-23 15:30:06 +0000155 else if (const Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000156 const Function *M = I->getParent() ? I->getParent()->getParent() : 0;
Chris Lattner207b5bc2001-10-29 16:37:48 +0000157 return M ? M->getParent() : 0;
Chris Lattner949a3622003-07-23 15:30:06 +0000158 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
Chris Lattner207b5bc2001-10-29 16:37:48 +0000159 return GV->getParent();
Chris Lattner207b5bc2001-10-29 16:37:48 +0000160 return 0;
161}
162
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000163static SlotMachine *createSlotMachine(const Value *V) {
Chris Lattner949a3622003-07-23 15:30:06 +0000164 if (const Argument *FA = dyn_cast<Argument>(V)) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000165 return new SlotMachine(FA->getParent());
Chris Lattner949a3622003-07-23 15:30:06 +0000166 } else if (const Instruction *I = dyn_cast<Instruction>(V)) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000167 return new SlotMachine(I->getParent()->getParent());
Chris Lattner949a3622003-07-23 15:30:06 +0000168 } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000169 return new SlotMachine(BB->getParent());
Chris Lattner949a3622003-07-23 15:30:06 +0000170 } else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)){
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000171 return new SlotMachine(GV->getParent());
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000172 } else if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V)){
173 return new SlotMachine(GA->getParent());
Chris Lattner949a3622003-07-23 15:30:06 +0000174 } else if (const Function *Func = dyn_cast<Function>(V)) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000175 return new SlotMachine(Func);
Chris Lattnerc1824992001-10-29 16:05:51 +0000176 }
177 return 0;
178}
Chris Lattner00950542001-06-06 20:29:01 +0000179
Reid Spencer3702d262007-01-26 08:02:52 +0000180/// NameNeedsQuotes - Return true if the specified llvm name should be wrapped
181/// with ""'s.
Reid Spencer4ad513c2007-05-22 19:27:35 +0000182static std::string QuoteNameIfNeeded(const std::string &Name) {
183 std::string result;
184 bool needsQuotes = Name[0] >= '0' && Name[0] <= '9';
185 // Scan the name to see if it needs quotes and to replace funky chars with
186 // their octal equivalent.
Chris Lattner24b8a5d2003-08-22 05:40:38 +0000187 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
188 char C = Name[i];
189 assert(C != '"' && "Illegal character in LLVM value name!");
Reid Spencer4ad513c2007-05-22 19:27:35 +0000190 if (isalnum(C) || C == '-' || C == '.' || C == '_')
191 result += C;
192 else if (C == '\\') {
193 needsQuotes = true;
194 result += "\\\\";
195 } else if (isprint(C)) {
196 needsQuotes = true;
197 result += C;
198 } else {
199 needsQuotes = true;
200 result += "\\";
201 char hex1 = (C >> 4) & 0x0F;
202 if (hex1 < 10)
203 result += hex1 + '0';
204 else
205 result += hex1 - 10 + 'A';
206 char hex2 = C & 0x0F;
207 if (hex2 < 10)
208 result += hex2 + '0';
209 else
210 result += hex2 - 10 + 'A';
211 }
Reid Spencer3702d262007-01-26 08:02:52 +0000212 }
Reid Spencer4ad513c2007-05-22 19:27:35 +0000213 if (needsQuotes) {
214 result.insert(0,"\"");
215 result += '"';
216 }
217 return result;
Reid Spencer3702d262007-01-26 08:02:52 +0000218}
219
220enum PrefixType {
221 GlobalPrefix,
222 LabelPrefix,
223 LocalPrefix
224};
225
226/// getLLVMName - Turn the specified string into an 'LLVM name', which is either
227/// prefixed with % (if the string only contains simple characters) or is
228/// surrounded with ""'s (if it has special chars in it).
229static std::string getLLVMName(const std::string &Name, PrefixType Prefix) {
230 assert(!Name.empty() && "Cannot get empty name!");
Reid Spencer3702d262007-01-26 08:02:52 +0000231 switch (Prefix) {
232 default: assert(0 && "Bad prefix!");
Reid Spencer4ad513c2007-05-22 19:27:35 +0000233 case GlobalPrefix: return '@' + QuoteNameIfNeeded(Name);
234 case LabelPrefix: return QuoteNameIfNeeded(Name);
235 case LocalPrefix: return '%' + QuoteNameIfNeeded(Name);
Reid Spencer3702d262007-01-26 08:02:52 +0000236 }
Chris Lattner24b8a5d2003-08-22 05:40:38 +0000237}
238
Chris Lattner207b5bc2001-10-29 16:37:48 +0000239
Misha Brukmanab5c6002004-03-02 00:22:19 +0000240/// fillTypeNameTable - If the module has a symbol table, take all global types
241/// and stuff their names into the TypeNames map.
242///
Chris Lattner207b5bc2001-10-29 16:37:48 +0000243static void fillTypeNameTable(const Module *M,
Chris Lattner7b13f562003-05-08 02:08:14 +0000244 std::map<const Type *, std::string> &TypeNames) {
Chris Lattner6e6026b2002-11-20 18:36:02 +0000245 if (!M) return;
Reid Spencer78d033e2007-01-06 07:24:44 +0000246 const TypeSymbolTable &ST = M->getTypeSymbolTable();
247 TypeSymbolTable::const_iterator TI = ST.begin();
248 for (; TI != ST.end(); ++TI) {
Reid Spencer9231ac82004-05-25 08:53:40 +0000249 // As a heuristic, don't insert pointer to primitive types, because
250 // they are used too often to have a single useful name.
251 //
252 const Type *Ty = cast<Type>(TI->second);
253 if (!isa<PointerType>(Ty) ||
Reid Spencerb03de0c2004-05-26 21:56:09 +0000254 !cast<PointerType>(Ty)->getElementType()->isPrimitiveType() ||
Chris Lattner42a75512007-01-15 02:27:26 +0000255 !cast<PointerType>(Ty)->getElementType()->isInteger() ||
Reid Spencerb03de0c2004-05-26 21:56:09 +0000256 isa<OpaqueType>(cast<PointerType>(Ty)->getElementType()))
Reid Spencer3702d262007-01-26 08:02:52 +0000257 TypeNames.insert(std::make_pair(Ty, getLLVMName(TI->first, LocalPrefix)));
Chris Lattner207b5bc2001-10-29 16:37:48 +0000258 }
259}
260
261
262
Misha Brukmanfd939082005-04-21 23:48:37 +0000263static void calcTypeName(const Type *Ty,
John Criswell4ff620a2004-06-01 14:54:08 +0000264 std::vector<const Type *> &TypeStack,
265 std::map<const Type *, std::string> &TypeNames,
266 std::string & Result){
Chris Lattner42a75512007-01-15 02:27:26 +0000267 if (Ty->isInteger() || (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty))) {
John Criswell4ff620a2004-06-01 14:54:08 +0000268 Result += Ty->getDescription(); // Base case
269 return;
270 }
Chris Lattner207b5bc2001-10-29 16:37:48 +0000271
272 // Check to see if the type is named.
Chris Lattner7b13f562003-05-08 02:08:14 +0000273 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
John Criswell4ff620a2004-06-01 14:54:08 +0000274 if (I != TypeNames.end()) {
275 Result += I->second;
276 return;
277 }
Chris Lattner207b5bc2001-10-29 16:37:48 +0000278
John Criswell4ff620a2004-06-01 14:54:08 +0000279 if (isa<OpaqueType>(Ty)) {
280 Result += "opaque";
281 return;
282 }
Chris Lattner88c17382003-10-30 00:22:33 +0000283
Chris Lattner207b5bc2001-10-29 16:37:48 +0000284 // Check to see if the Type is already on the stack...
285 unsigned Slot = 0, CurSize = TypeStack.size();
286 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
287
Misha Brukmanfd939082005-04-21 23:48:37 +0000288 // This is another base case for the recursion. In this case, we know
Chris Lattner207b5bc2001-10-29 16:37:48 +0000289 // that we have looped back to a type that we have previously visited.
290 // Generate the appropriate upreference to handle this.
John Criswell4ff620a2004-06-01 14:54:08 +0000291 if (Slot < CurSize) {
292 Result += "\\" + utostr(CurSize-Slot); // Here's the upreference
293 return;
294 }
Chris Lattner207b5bc2001-10-29 16:37:48 +0000295
296 TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
Misha Brukmanfd939082005-04-21 23:48:37 +0000297
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000298 switch (Ty->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000299 case Type::IntegerTyID: {
300 unsigned BitWidth = cast<IntegerType>(Ty)->getBitWidth();
Reid Spencer15ee9352007-01-12 07:25:20 +0000301 Result += "i" + utostr(BitWidth);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000302 break;
303 }
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000304 case Type::FunctionTyID: {
Chris Lattner949a3622003-07-23 15:30:06 +0000305 const FunctionType *FTy = cast<FunctionType>(Ty);
John Criswell4ff620a2004-06-01 14:54:08 +0000306 calcTypeName(FTy->getReturnType(), TypeStack, TypeNames, Result);
307 Result += " (";
Chris Lattnerd5d89962004-02-09 04:14:01 +0000308 for (FunctionType::param_iterator I = FTy->param_begin(),
Duncan Sandsdc024672007-11-27 13:23:08 +0000309 E = FTy->param_end(); I != E; ++I) {
Chris Lattnerd5d89962004-02-09 04:14:01 +0000310 if (I != FTy->param_begin())
Chris Lattner207b5bc2001-10-29 16:37:48 +0000311 Result += ", ";
John Criswell4ff620a2004-06-01 14:54:08 +0000312 calcTypeName(*I, TypeStack, TypeNames, Result);
Chris Lattner207b5bc2001-10-29 16:37:48 +0000313 }
Chris Lattner2761e9f2002-04-13 20:53:41 +0000314 if (FTy->isVarArg()) {
Chris Lattnerd5d89962004-02-09 04:14:01 +0000315 if (FTy->getNumParams()) Result += ", ";
Chris Lattner207b5bc2001-10-29 16:37:48 +0000316 Result += "...";
317 }
318 Result += ")";
319 break;
320 }
321 case Type::StructTyID: {
Chris Lattner949a3622003-07-23 15:30:06 +0000322 const StructType *STy = cast<StructType>(Ty);
Andrew Lenharth38ecbf12006-12-08 18:06:16 +0000323 if (STy->isPacked())
324 Result += '<';
John Criswell4ff620a2004-06-01 14:54:08 +0000325 Result += "{ ";
Chris Lattnerd21cd802004-02-09 04:37:31 +0000326 for (StructType::element_iterator I = STy->element_begin(),
327 E = STy->element_end(); I != E; ++I) {
328 if (I != STy->element_begin())
Chris Lattner207b5bc2001-10-29 16:37:48 +0000329 Result += ", ";
John Criswell4ff620a2004-06-01 14:54:08 +0000330 calcTypeName(*I, TypeStack, TypeNames, Result);
Chris Lattner207b5bc2001-10-29 16:37:48 +0000331 }
332 Result += " }";
Andrew Lenharth38ecbf12006-12-08 18:06:16 +0000333 if (STy->isPacked())
334 Result += '>';
Chris Lattner207b5bc2001-10-29 16:37:48 +0000335 break;
336 }
Christopher Lambfe63fb92007-12-11 08:59:05 +0000337 case Type::PointerTyID: {
338 const PointerType *PTy = cast<PointerType>(Ty);
339 calcTypeName(PTy->getElementType(),
John Criswell4ff620a2004-06-01 14:54:08 +0000340 TypeStack, TypeNames, Result);
Christopher Lambfe63fb92007-12-11 08:59:05 +0000341 if (unsigned AddressSpace = PTy->getAddressSpace())
342 Result += " addrspace(" + utostr(AddressSpace) + ")";
John Criswell4ff620a2004-06-01 14:54:08 +0000343 Result += "*";
Chris Lattner207b5bc2001-10-29 16:37:48 +0000344 break;
Christopher Lambfe63fb92007-12-11 08:59:05 +0000345 }
Chris Lattner207b5bc2001-10-29 16:37:48 +0000346 case Type::ArrayTyID: {
Chris Lattner949a3622003-07-23 15:30:06 +0000347 const ArrayType *ATy = cast<ArrayType>(Ty);
John Criswell4ff620a2004-06-01 14:54:08 +0000348 Result += "[" + utostr(ATy->getNumElements()) + " x ";
349 calcTypeName(ATy->getElementType(), TypeStack, TypeNames, Result);
350 Result += "]";
Chris Lattner207b5bc2001-10-29 16:37:48 +0000351 break;
352 }
Reid Spencer9d6565a2007-02-15 02:26:10 +0000353 case Type::VectorTyID: {
354 const VectorType *PTy = cast<VectorType>(Ty);
Brian Gaeke715c90b2004-08-20 06:00:58 +0000355 Result += "<" + utostr(PTy->getNumElements()) + " x ";
356 calcTypeName(PTy->getElementType(), TypeStack, TypeNames, Result);
357 Result += ">";
358 break;
359 }
Chris Lattner9e094c42003-05-14 17:50:47 +0000360 case Type::OpaqueTyID:
John Criswell4ff620a2004-06-01 14:54:08 +0000361 Result += "opaque";
Chris Lattner9e094c42003-05-14 17:50:47 +0000362 break;
Chris Lattner207b5bc2001-10-29 16:37:48 +0000363 default:
John Criswell4ff620a2004-06-01 14:54:08 +0000364 Result += "<unrecognized-type>";
Chris Lattner82c4bc72006-12-06 06:40:49 +0000365 break;
Chris Lattner207b5bc2001-10-29 16:37:48 +0000366 }
367
368 TypeStack.pop_back(); // Remove self from stack...
Chris Lattner207b5bc2001-10-29 16:37:48 +0000369}
370
371
Misha Brukman9d0802e2004-03-01 19:48:13 +0000372/// printTypeInt - The internal guts of printing out a type that has a
373/// potentially named portion.
374///
Chris Lattner7b13f562003-05-08 02:08:14 +0000375static std::ostream &printTypeInt(std::ostream &Out, const Type *Ty,
376 std::map<const Type *, std::string> &TypeNames) {
Chris Lattner207b5bc2001-10-29 16:37:48 +0000377 // Primitive types always print out their description, regardless of whether
378 // they have been named or not.
379 //
Chris Lattner42a75512007-01-15 02:27:26 +0000380 if (Ty->isInteger() || (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty)))
Chris Lattnerdaf2a492003-10-30 00:12:51 +0000381 return Out << Ty->getDescription();
Chris Lattner207b5bc2001-10-29 16:37:48 +0000382
383 // Check to see if the type is named.
Chris Lattner7b13f562003-05-08 02:08:14 +0000384 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
Chris Lattner207b5bc2001-10-29 16:37:48 +0000385 if (I != TypeNames.end()) return Out << I->second;
386
387 // Otherwise we have a type that has not been named but is a derived type.
388 // Carefully recurse the type hierarchy to print out any contained symbolic
389 // names.
390 //
Chris Lattner7b13f562003-05-08 02:08:14 +0000391 std::vector<const Type *> TypeStack;
John Criswell4ff620a2004-06-01 14:54:08 +0000392 std::string TypeName;
393 calcTypeName(Ty, TypeStack, TypeNames, TypeName);
Chris Lattner697954c2002-01-20 22:54:45 +0000394 TypeNames.insert(std::make_pair(Ty, TypeName));//Cache type name for later use
John Criswell4ff620a2004-06-01 14:54:08 +0000395 return (Out << TypeName);
Chris Lattner207b5bc2001-10-29 16:37:48 +0000396}
397
Chris Lattnere51e03b2001-10-31 04:33:19 +0000398
Misha Brukman9d0802e2004-03-01 19:48:13 +0000399/// WriteTypeSymbolic - This attempts to write the specified type as a symbolic
400/// type, iff there is an entry in the modules symbol table for the specified
401/// type or one of it's component types. This is slower than a simple x << Type
402///
Chris Lattner31f84992003-11-21 20:23:48 +0000403std::ostream &llvm::WriteTypeSymbolic(std::ostream &Out, const Type *Ty,
404 const Module *M) {
Misha Brukmanfd939082005-04-21 23:48:37 +0000405 Out << ' ';
Chris Lattner207b5bc2001-10-29 16:37:48 +0000406
Chris Lattner82c4bc72006-12-06 06:40:49 +0000407 // If they want us to print out a type, but there is no context, we can't
408 // print it symbolically.
409 if (!M)
Chris Lattner7b8660d2001-10-29 16:40:32 +0000410 return Out << Ty->getDescription();
Chris Lattner82c4bc72006-12-06 06:40:49 +0000411
412 std::map<const Type *, std::string> TypeNames;
413 fillTypeNameTable(M, TypeNames);
414 return printTypeInt(Out, Ty, TypeNames);
Chris Lattner207b5bc2001-10-29 16:37:48 +0000415}
416
Chris Lattner18365502006-01-23 23:03:36 +0000417// PrintEscapedString - Print each character of the specified string, escaping
418// it if it is not printable or if it is an escape char.
419static void PrintEscapedString(const std::string &Str, std::ostream &Out) {
420 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
421 unsigned char C = Str[i];
422 if (isprint(C) && C != '"' && C != '\\') {
423 Out << C;
424 } else {
425 Out << '\\'
426 << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'))
427 << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
428 }
429 }
430}
431
Chris Lattner82c4bc72006-12-06 06:40:49 +0000432static const char *getPredicateText(unsigned predicate) {
Reid Spencer81dfeb32006-12-04 05:19:18 +0000433 const char * pred = "unknown";
434 switch (predicate) {
435 case FCmpInst::FCMP_FALSE: pred = "false"; break;
436 case FCmpInst::FCMP_OEQ: pred = "oeq"; break;
437 case FCmpInst::FCMP_OGT: pred = "ogt"; break;
438 case FCmpInst::FCMP_OGE: pred = "oge"; break;
439 case FCmpInst::FCMP_OLT: pred = "olt"; break;
440 case FCmpInst::FCMP_OLE: pred = "ole"; break;
441 case FCmpInst::FCMP_ONE: pred = "one"; break;
442 case FCmpInst::FCMP_ORD: pred = "ord"; break;
443 case FCmpInst::FCMP_UNO: pred = "uno"; break;
444 case FCmpInst::FCMP_UEQ: pred = "ueq"; break;
445 case FCmpInst::FCMP_UGT: pred = "ugt"; break;
446 case FCmpInst::FCMP_UGE: pred = "uge"; break;
447 case FCmpInst::FCMP_ULT: pred = "ult"; break;
448 case FCmpInst::FCMP_ULE: pred = "ule"; break;
449 case FCmpInst::FCMP_UNE: pred = "une"; break;
450 case FCmpInst::FCMP_TRUE: pred = "true"; break;
451 case ICmpInst::ICMP_EQ: pred = "eq"; break;
452 case ICmpInst::ICMP_NE: pred = "ne"; break;
453 case ICmpInst::ICMP_SGT: pred = "sgt"; break;
454 case ICmpInst::ICMP_SGE: pred = "sge"; break;
455 case ICmpInst::ICMP_SLT: pred = "slt"; break;
456 case ICmpInst::ICMP_SLE: pred = "sle"; break;
457 case ICmpInst::ICMP_UGT: pred = "ugt"; break;
458 case ICmpInst::ICMP_UGE: pred = "uge"; break;
459 case ICmpInst::ICMP_ULT: pred = "ult"; break;
460 case ICmpInst::ICMP_ULE: pred = "ule"; break;
461 }
462 return pred;
463}
464
Misha Brukmanfd939082005-04-21 23:48:37 +0000465/// @brief Internal constant writer.
466static void WriteConstantInt(std::ostream &Out, const Constant *CV,
Chris Lattner7b13f562003-05-08 02:08:14 +0000467 std::map<const Type *, std::string> &TypeTable,
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000468 SlotMachine *Machine) {
Jim Laskey38a409c2006-02-27 10:33:53 +0000469 const int IndentSize = 4;
470 static std::string Indent = "\n";
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000471 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Reid Spencer4fe16d62007-01-11 18:21:29 +0000472 if (CI->getType() == Type::Int1Ty)
Reid Spencer579dca12007-01-12 04:24:46 +0000473 Out << (CI->getZExtValue() ? "true" : "false");
474 else
Reid Spencer914c3bc2007-02-27 20:25:25 +0000475 Out << CI->getValue().toStringSigned(10);
Chris Lattner66e810b2002-04-18 18:53:13 +0000476 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000477 if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEdouble ||
478 &CFP->getValueAPF().getSemantics() == &APFloat::IEEEsingle) {
479 // We would like to output the FP constant value in exponential notation,
480 // but we cannot do this if doing so will lose precision. Check here to
481 // make sure that we only output it in exponential format if we can parse
482 // the value back and get the same value.
483 //
484 bool isDouble = &CFP->getValueAPF().getSemantics()==&APFloat::IEEEdouble;
485 double Val = (isDouble) ? CFP->getValueAPF().convertToDouble() :
486 CFP->getValueAPF().convertToFloat();
487 std::string StrVal = ftostr(CFP->getValueAPF());
Chris Lattner66e810b2002-04-18 18:53:13 +0000488
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000489 // Check to make sure that the stringized number is not some string like
490 // "Inf" or NaN, that atof will accept, but the lexer will not. Check
491 // that the string matches the "[-+]?[0-9]" regex.
492 //
493 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
494 ((StrVal[0] == '-' || StrVal[0] == '+') &&
495 (StrVal[1] >= '0' && StrVal[1] <= '9'))) {
496 // Reparse stringized version!
497 if (atof(StrVal.c_str()) == Val) {
498 Out << StrVal;
499 return;
500 }
Chris Lattner66e810b2002-04-18 18:53:13 +0000501 }
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000502 // Otherwise we could not reparse it to exactly the same value, so we must
503 // output the string in hexadecimal format!
504 assert(sizeof(double) == sizeof(uint64_t) &&
505 "assuming that double is 64 bits!");
506 Out << "0x" << utohexstr(DoubleToBits(Val));
507 } else {
508 // Some form of long double. These appear as a magic letter identifying
509 // the type, then a fixed number of hex digits.
510 Out << "0x";
511 if (&CFP->getValueAPF().getSemantics() == &APFloat::x87DoubleExtended)
512 Out << 'K';
513 else if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEquad)
514 Out << 'L';
Dale Johannesena471c2e2007-10-11 18:07:22 +0000515 else if (&CFP->getValueAPF().getSemantics() == &APFloat::PPCDoubleDouble)
516 Out << 'M';
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000517 else
518 assert(0 && "Unsupported floating point type");
Dale Johannesen693717f2007-09-26 23:20:33 +0000519 // api needed to prevent premature destruction
520 APInt api = CFP->getValueAPF().convertToAPInt();
521 const uint64_t* p = api.getRawData();
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000522 uint64_t word = *p;
523 int shiftcount=60;
Dale Johannesena471c2e2007-10-11 18:07:22 +0000524 int width = api.getBitWidth();
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000525 for (int j=0; j<width; j+=4, shiftcount-=4) {
526 unsigned int nibble = (word>>shiftcount) & 15;
527 if (nibble < 10)
528 Out << (unsigned char)(nibble + '0');
529 else
530 Out << (unsigned char)(nibble - 10 + 'A');
531 if (shiftcount == 0) {
532 word = *(++p);
Dale Johannesena471c2e2007-10-11 18:07:22 +0000533 shiftcount = 64;
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000534 if (width-j-4 < 64)
535 shiftcount = width-j-4;
536 }
537 }
538 }
Chris Lattnerde512b52004-02-15 05:55:15 +0000539 } else if (isa<ConstantAggregateZero>(CV)) {
540 Out << "zeroinitializer";
Chris Lattner66e810b2002-04-18 18:53:13 +0000541 } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
542 // As a special case, print the array as a string if it is an array of
543 // ubytes or an array of sbytes with positive values.
Misha Brukmanfd939082005-04-21 23:48:37 +0000544 //
Chris Lattner66e810b2002-04-18 18:53:13 +0000545 const Type *ETy = CA->getType()->getElementType();
Chris Lattner18365502006-01-23 23:03:36 +0000546 if (CA->isString()) {
Chris Lattner66e810b2002-04-18 18:53:13 +0000547 Out << "c\"";
Chris Lattner18365502006-01-23 23:03:36 +0000548 PrintEscapedString(CA->getAsString(), Out);
Chris Lattner66e810b2002-04-18 18:53:13 +0000549 Out << "\"";
550
551 } else { // Cannot output in string format...
Misha Brukman40c732c2004-06-04 21:11:51 +0000552 Out << '[';
Chris Lattner7a716ad2002-04-16 21:36:08 +0000553 if (CA->getNumOperands()) {
Misha Brukman40c732c2004-06-04 21:11:51 +0000554 Out << ' ';
Chris Lattner66e810b2002-04-18 18:53:13 +0000555 printTypeInt(Out, ETy, TypeTable);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000556 WriteAsOperandInternal(Out, CA->getOperand(0),
Chris Lattner2fcfdb72006-12-06 06:24:27 +0000557 TypeTable, Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000558 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
559 Out << ", ";
Chris Lattner66e810b2002-04-18 18:53:13 +0000560 printTypeInt(Out, ETy, TypeTable);
Chris Lattner2fcfdb72006-12-06 06:24:27 +0000561 WriteAsOperandInternal(Out, CA->getOperand(i), TypeTable, Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000562 }
563 }
564 Out << " ]";
565 }
566 } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Andrew Lenharth43f344a2007-01-08 18:21:30 +0000567 if (CS->getType()->isPacked())
568 Out << '<';
Misha Brukman40c732c2004-06-04 21:11:51 +0000569 Out << '{';
Jim Laskeya3f332b2006-02-25 12:27:03 +0000570 unsigned N = CS->getNumOperands();
571 if (N) {
Jim Laskey38a409c2006-02-27 10:33:53 +0000572 if (N > 2) {
573 Indent += std::string(IndentSize, ' ');
574 Out << Indent;
575 } else {
576 Out << ' ';
577 }
Chris Lattner7a716ad2002-04-16 21:36:08 +0000578 printTypeInt(Out, CS->getOperand(0)->getType(), TypeTable);
579
Chris Lattner2fcfdb72006-12-06 06:24:27 +0000580 WriteAsOperandInternal(Out, CS->getOperand(0), TypeTable, Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000581
Jim Laskeya3f332b2006-02-25 12:27:03 +0000582 for (unsigned i = 1; i < N; i++) {
Chris Lattner7a716ad2002-04-16 21:36:08 +0000583 Out << ", ";
Jim Laskey38a409c2006-02-27 10:33:53 +0000584 if (N > 2) Out << Indent;
Chris Lattner7a716ad2002-04-16 21:36:08 +0000585 printTypeInt(Out, CS->getOperand(i)->getType(), TypeTable);
586
Chris Lattner2fcfdb72006-12-06 06:24:27 +0000587 WriteAsOperandInternal(Out, CS->getOperand(i), TypeTable, Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000588 }
Jim Laskey38a409c2006-02-27 10:33:53 +0000589 if (N > 2) Indent.resize(Indent.size() - IndentSize);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000590 }
Jim Laskeya3f332b2006-02-25 12:27:03 +0000591
Chris Lattner7a716ad2002-04-16 21:36:08 +0000592 Out << " }";
Andrew Lenharth43f344a2007-01-08 18:21:30 +0000593 if (CS->getType()->isPacked())
594 Out << '>';
Reid Spencer9d6565a2007-02-15 02:26:10 +0000595 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
Brian Gaeke715c90b2004-08-20 06:00:58 +0000596 const Type *ETy = CP->getType()->getElementType();
Misha Brukmanfd939082005-04-21 23:48:37 +0000597 assert(CP->getNumOperands() > 0 &&
Brian Gaeke715c90b2004-08-20 06:00:58 +0000598 "Number of operands for a PackedConst must be > 0");
599 Out << '<';
600 Out << ' ';
601 printTypeInt(Out, ETy, TypeTable);
Chris Lattner2fcfdb72006-12-06 06:24:27 +0000602 WriteAsOperandInternal(Out, CP->getOperand(0), TypeTable, Machine);
Brian Gaeke715c90b2004-08-20 06:00:58 +0000603 for (unsigned i = 1, e = CP->getNumOperands(); i != e; ++i) {
604 Out << ", ";
605 printTypeInt(Out, ETy, TypeTable);
Chris Lattner2fcfdb72006-12-06 06:24:27 +0000606 WriteAsOperandInternal(Out, CP->getOperand(i), TypeTable, Machine);
Brian Gaeke715c90b2004-08-20 06:00:58 +0000607 }
608 Out << " >";
Chris Lattner7a716ad2002-04-16 21:36:08 +0000609 } else if (isa<ConstantPointerNull>(CV)) {
610 Out << "null";
611
Chris Lattnerb976e662004-10-16 18:08:06 +0000612 } else if (isa<UndefValue>(CV)) {
613 Out << "undef";
614
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000615 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Reid Spencer81dfeb32006-12-04 05:19:18 +0000616 Out << CE->getOpcodeName();
617 if (CE->isCompare())
618 Out << " " << getPredicateText(CE->getPredicate());
619 Out << " (";
Misha Brukmanfd939082005-04-21 23:48:37 +0000620
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000621 for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
622 printTypeInt(Out, (*OI)->getType(), TypeTable);
Chris Lattner2fcfdb72006-12-06 06:24:27 +0000623 WriteAsOperandInternal(Out, *OI, TypeTable, Machine);
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000624 if (OI+1 != CE->op_end())
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000625 Out << ", ";
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000626 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000627
Reid Spencer3da59db2006-11-27 01:05:10 +0000628 if (CE->isCast()) {
Chris Lattner95586b82002-08-15 19:37:43 +0000629 Out << " to ";
630 printTypeInt(Out, CE->getType(), TypeTable);
631 }
Reid Spencer3da59db2006-11-27 01:05:10 +0000632
Misha Brukman40c732c2004-06-04 21:11:51 +0000633 Out << ')';
Chris Lattner95586b82002-08-15 19:37:43 +0000634
Chris Lattner7a716ad2002-04-16 21:36:08 +0000635 } else {
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000636 Out << "<placeholder or erroneous Constant>";
Chris Lattner7a716ad2002-04-16 21:36:08 +0000637 }
638}
639
640
Misha Brukmanab5c6002004-03-02 00:22:19 +0000641/// WriteAsOperand - Write the name of the specified value out to the specified
642/// ostream. This can be useful when you just want to print int %reg126, not
643/// the whole instruction that generated it.
644///
Misha Brukmanfd939082005-04-21 23:48:37 +0000645static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
Chris Lattner7b13f562003-05-08 02:08:14 +0000646 std::map<const Type*, std::string> &TypeTable,
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000647 SlotMachine *Machine) {
Misha Brukman40c732c2004-06-04 21:11:51 +0000648 Out << ' ';
Chris Lattner2fcfdb72006-12-06 06:24:27 +0000649 if (V->hasName())
Reid Spencer3702d262007-01-26 08:02:52 +0000650 Out << getLLVMName(V->getName(),
651 isa<GlobalValue>(V) ? GlobalPrefix : LocalPrefix);
Reid Spencer79703962004-07-17 23:47:01 +0000652 else {
653 const Constant *CV = dyn_cast<Constant>(V);
Chris Lattner80cd1152006-01-25 22:26:05 +0000654 if (CV && !isa<GlobalValue>(CV)) {
Chris Lattner2fcfdb72006-12-06 06:24:27 +0000655 WriteConstantInt(Out, CV, TypeTable, Machine);
Chris Lattner80cd1152006-01-25 22:26:05 +0000656 } else if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
657 Out << "asm ";
658 if (IA->hasSideEffects())
659 Out << "sideeffect ";
660 Out << '"';
661 PrintEscapedString(IA->getAsmString(), Out);
662 Out << "\", \"";
663 PrintEscapedString(IA->getConstraintString(), Out);
664 Out << '"';
665 } else {
Reid Spencer3702d262007-01-26 08:02:52 +0000666 char Prefix = '%';
Chris Lattner7a716ad2002-04-16 21:36:08 +0000667 int Slot;
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000668 if (Machine) {
Reid Spencer3702d262007-01-26 08:02:52 +0000669 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattner22379bc2007-01-11 03:54:27 +0000670 Slot = Machine->getGlobalSlot(GV);
Reid Spencer3702d262007-01-26 08:02:52 +0000671 Prefix = '@';
672 } else {
Chris Lattner22379bc2007-01-11 03:54:27 +0000673 Slot = Machine->getLocalSlot(V);
Reid Spencer3702d262007-01-26 08:02:52 +0000674 }
Chris Lattner7a716ad2002-04-16 21:36:08 +0000675 } else {
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000676 Machine = createSlotMachine(V);
Chris Lattner22379bc2007-01-11 03:54:27 +0000677 if (Machine) {
Reid Spencer3702d262007-01-26 08:02:52 +0000678 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattner22379bc2007-01-11 03:54:27 +0000679 Slot = Machine->getGlobalSlot(GV);
Reid Spencer3702d262007-01-26 08:02:52 +0000680 Prefix = '@';
681 } else {
Chris Lattner22379bc2007-01-11 03:54:27 +0000682 Slot = Machine->getLocalSlot(V);
Reid Spencer3702d262007-01-26 08:02:52 +0000683 }
Chris Lattner22379bc2007-01-11 03:54:27 +0000684 } else {
Chris Lattner69566452004-06-09 19:41:19 +0000685 Slot = -1;
Chris Lattner22379bc2007-01-11 03:54:27 +0000686 }
Reid Spencerb03de0c2004-05-26 21:56:09 +0000687 delete Machine;
Chris Lattner7a716ad2002-04-16 21:36:08 +0000688 }
Chris Lattner69566452004-06-09 19:41:19 +0000689 if (Slot != -1)
Reid Spencer3702d262007-01-26 08:02:52 +0000690 Out << Prefix << Slot;
Chris Lattner69566452004-06-09 19:41:19 +0000691 else
692 Out << "<badref>";
Chris Lattner7a716ad2002-04-16 21:36:08 +0000693 }
694 }
695}
696
Misha Brukman9d0802e2004-03-01 19:48:13 +0000697/// WriteAsOperand - Write the name of the specified value out to the specified
698/// ostream. This can be useful when you just want to print int %reg126, not
699/// the whole instruction that generated it.
700///
Chris Lattner31f84992003-11-21 20:23:48 +0000701std::ostream &llvm::WriteAsOperand(std::ostream &Out, const Value *V,
Chris Lattnera6b1ffc2006-12-06 06:15:43 +0000702 bool PrintType, const Module *Context) {
Chris Lattner7b13f562003-05-08 02:08:14 +0000703 std::map<const Type *, std::string> TypeNames;
Chris Lattner607dc682002-07-10 16:48:17 +0000704 if (Context == 0) Context = getModuleFromVal(V);
Chris Lattner207b5bc2001-10-29 16:37:48 +0000705
Chris Lattner6e6026b2002-11-20 18:36:02 +0000706 if (Context)
Chris Lattner607dc682002-07-10 16:48:17 +0000707 fillTypeNameTable(Context, TypeNames);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000708
709 if (PrintType)
710 printTypeInt(Out, V->getType(), TypeNames);
Misha Brukmanfd939082005-04-21 23:48:37 +0000711
Chris Lattner2fcfdb72006-12-06 06:24:27 +0000712 WriteAsOperandInternal(Out, V, TypeNames, 0);
Chris Lattner622f7402001-07-20 19:15:21 +0000713 return Out;
714}
715
Reid Spencer0e25e1c2004-07-04 11:50:43 +0000716
Chris Lattner31f84992003-11-21 20:23:48 +0000717namespace llvm {
Chris Lattnerd8c2e422001-07-12 23:35:26 +0000718
Chris Lattner007377f2001-09-07 16:36:04 +0000719class AssemblyWriter {
Misha Brukman0313e0b2004-06-21 21:53:56 +0000720 std::ostream &Out;
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000721 SlotMachine &Machine;
Chris Lattnerc1824992001-10-29 16:05:51 +0000722 const Module *TheModule;
Chris Lattner7b13f562003-05-08 02:08:14 +0000723 std::map<const Type *, std::string> TypeNames;
Chris Lattner95e5a2c2003-10-30 23:41:03 +0000724 AssemblyAnnotationWriter *AnnotationWriter;
Chris Lattner00950542001-06-06 20:29:01 +0000725public:
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000726 inline AssemblyWriter(std::ostream &o, SlotMachine &Mac, const Module *M,
Chris Lattner95e5a2c2003-10-30 23:41:03 +0000727 AssemblyAnnotationWriter *AAW)
Misha Brukman0313e0b2004-06-21 21:53:56 +0000728 : Out(o), Machine(Mac), TheModule(M), AnnotationWriter(AAW) {
Chris Lattnerc1824992001-10-29 16:05:51 +0000729
730 // If the module has a symbol table, take all global types and stuff their
731 // names into the TypeNames map.
732 //
Chris Lattner207b5bc2001-10-29 16:37:48 +0000733 fillTypeNameTable(M, TypeNames);
Chris Lattner00950542001-06-06 20:29:01 +0000734 }
735
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000736 inline void write(const Module *M) { printModule(M); }
737 inline void write(const GlobalVariable *G) { printGlobal(G); }
738 inline void write(const GlobalAlias *G) { printAlias(G); }
739 inline void write(const Function *F) { printFunction(F); }
740 inline void write(const BasicBlock *BB) { printBasicBlock(BB); }
Chris Lattner7e708292002-06-25 16:13:24 +0000741 inline void write(const Instruction *I) { printInstruction(*I); }
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000742 inline void write(const Type *Ty) { printType(Ty); }
Chris Lattner00950542001-06-06 20:29:01 +0000743
Chris Lattner2fcfdb72006-12-06 06:24:27 +0000744 void writeOperand(const Value *Op, bool PrintType);
Duncan Sandsdc024672007-11-27 13:23:08 +0000745 void writeParamOperand(const Value *Operand, uint16_t Attrs);
Chris Lattner66e810b2002-04-18 18:53:13 +0000746
Misha Brukman5cf1acf2004-04-28 15:31:21 +0000747 const Module* getModule() { return TheModule; }
748
Misha Brukmanf771bea2004-11-15 19:30:05 +0000749private:
Chris Lattnerc1824992001-10-29 16:05:51 +0000750 void printModule(const Module *M);
Reid Spencer78d033e2007-01-06 07:24:44 +0000751 void printTypeSymbolTable(const TypeSymbolTable &ST);
Chris Lattnerc1824992001-10-29 16:05:51 +0000752 void printGlobal(const GlobalVariable *GV);
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000753 void printAlias(const GlobalAlias *GV);
Chris Lattner79df7c02002-03-26 18:01:55 +0000754 void printFunction(const Function *F);
Reid Spencerb138a062007-04-09 06:10:42 +0000755 void printArgument(const Argument *FA, uint16_t ParamAttrs);
Chris Lattnerc1824992001-10-29 16:05:51 +0000756 void printBasicBlock(const BasicBlock *BB);
Chris Lattner7e708292002-06-25 16:13:24 +0000757 void printInstruction(const Instruction &I);
Chris Lattner2761e9f2002-04-13 20:53:41 +0000758
759 // printType - Go to extreme measures to attempt to print out a short,
760 // symbolic version of a type name.
761 //
Chris Lattner7b13f562003-05-08 02:08:14 +0000762 std::ostream &printType(const Type *Ty) {
Misha Brukman0313e0b2004-06-21 21:53:56 +0000763 return printTypeInt(Out, Ty, TypeNames);
Chris Lattner2761e9f2002-04-13 20:53:41 +0000764 }
765
766 // printTypeAtLeastOneLevel - Print out one level of the possibly complex type
767 // without considering any symbolic types that we may have equal to it.
768 //
Chris Lattner7b13f562003-05-08 02:08:14 +0000769 std::ostream &printTypeAtLeastOneLevel(const Type *Ty);
Chris Lattnerc1824992001-10-29 16:05:51 +0000770
Chris Lattnere02fa852001-10-13 06:42:36 +0000771 // printInfoComment - Print a little comment after the instruction indicating
772 // which slot it occupies.
Chris Lattner7e708292002-06-25 16:13:24 +0000773 void printInfoComment(const Value &V);
Chris Lattner00950542001-06-06 20:29:01 +0000774};
Reid Spencer73b74952004-05-27 22:04:46 +0000775} // end of llvm namespace
Chris Lattner00950542001-06-06 20:29:01 +0000776
Misha Brukmanab5c6002004-03-02 00:22:19 +0000777/// printTypeAtLeastOneLevel - Print out one level of the possibly complex type
778/// without considering any symbolic types that we may have equal to it.
779///
Chris Lattner7b13f562003-05-08 02:08:14 +0000780std::ostream &AssemblyWriter::printTypeAtLeastOneLevel(const Type *Ty) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000781 if (const IntegerType *ITy = dyn_cast<IntegerType>(Ty))
782 Out << "i" << utostr(ITy->getBitWidth());
783 else if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
Reid Spencerbd5db8e2006-12-31 05:24:50 +0000784 printType(FTy->getReturnType());
Reid Spencerbd5db8e2006-12-31 05:24:50 +0000785 Out << " (";
Chris Lattnerd5d89962004-02-09 04:14:01 +0000786 for (FunctionType::param_iterator I = FTy->param_begin(),
787 E = FTy->param_end(); I != E; ++I) {
788 if (I != FTy->param_begin())
Misha Brukman0313e0b2004-06-21 21:53:56 +0000789 Out << ", ";
Chris Lattner7a716ad2002-04-16 21:36:08 +0000790 printType(*I);
Chris Lattner2761e9f2002-04-13 20:53:41 +0000791 }
792 if (FTy->isVarArg()) {
Misha Brukman0313e0b2004-06-21 21:53:56 +0000793 if (FTy->getNumParams()) Out << ", ";
794 Out << "...";
Chris Lattner2761e9f2002-04-13 20:53:41 +0000795 }
Misha Brukman0313e0b2004-06-21 21:53:56 +0000796 Out << ')';
Chris Lattner7e708292002-06-25 16:13:24 +0000797 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
Andrew Lenharth38ecbf12006-12-08 18:06:16 +0000798 if (STy->isPacked())
799 Out << '<';
Misha Brukman0313e0b2004-06-21 21:53:56 +0000800 Out << "{ ";
Chris Lattnerd21cd802004-02-09 04:37:31 +0000801 for (StructType::element_iterator I = STy->element_begin(),
802 E = STy->element_end(); I != E; ++I) {
803 if (I != STy->element_begin())
Misha Brukman0313e0b2004-06-21 21:53:56 +0000804 Out << ", ";
Chris Lattner2761e9f2002-04-13 20:53:41 +0000805 printType(*I);
806 }
Misha Brukman0313e0b2004-06-21 21:53:56 +0000807 Out << " }";
Andrew Lenharth38ecbf12006-12-08 18:06:16 +0000808 if (STy->isPacked())
809 Out << '>';
Chris Lattner7e708292002-06-25 16:13:24 +0000810 } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
Christopher Lambef989a22007-12-18 03:49:35 +0000811 printType(PTy->getElementType());
812 if (unsigned AddressSpace = PTy->getAddressSpace())
813 Out << " addrspace(" << AddressSpace << ")";
814 Out << '*';
Chris Lattner7e708292002-06-25 16:13:24 +0000815 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Misha Brukman0313e0b2004-06-21 21:53:56 +0000816 Out << '[' << ATy->getNumElements() << " x ";
Misha Brukman40c732c2004-06-04 21:11:51 +0000817 printType(ATy->getElementType()) << ']';
Reid Spencer9d6565a2007-02-15 02:26:10 +0000818 } else if (const VectorType *PTy = dyn_cast<VectorType>(Ty)) {
Reid Spencer5527c0b2004-08-20 15:37:30 +0000819 Out << '<' << PTy->getNumElements() << " x ";
820 printType(PTy->getElementType()) << '>';
821 }
Reid Spencer3ed469c2006-11-02 20:25:50 +0000822 else if (isa<OpaqueType>(Ty)) {
Misha Brukman0313e0b2004-06-21 21:53:56 +0000823 Out << "opaque";
Chris Lattner2761e9f2002-04-13 20:53:41 +0000824 } else {
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000825 if (!Ty->isPrimitiveType())
Misha Brukman0313e0b2004-06-21 21:53:56 +0000826 Out << "<unknown derived type>";
Chris Lattner2761e9f2002-04-13 20:53:41 +0000827 printType(Ty);
828 }
Misha Brukman0313e0b2004-06-21 21:53:56 +0000829 return Out;
Chris Lattner2761e9f2002-04-13 20:53:41 +0000830}
831
832
Chris Lattner2fcfdb72006-12-06 06:24:27 +0000833void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType) {
834 if (Operand == 0) {
Chris Lattneraab18202005-02-24 16:58:29 +0000835 Out << "<null operand!>";
Chris Lattner2fcfdb72006-12-06 06:24:27 +0000836 } else {
837 if (PrintType) { Out << ' '; printType(Operand->getType()); }
838 WriteAsOperandInternal(Out, Operand, TypeNames, &Machine);
Chris Lattneraab18202005-02-24 16:58:29 +0000839 }
Chris Lattner00950542001-06-06 20:29:01 +0000840}
841
Duncan Sandsdc024672007-11-27 13:23:08 +0000842void AssemblyWriter::writeParamOperand(const Value *Operand, uint16_t Attrs) {
843 if (Operand == 0) {
844 Out << "<null operand!>";
845 } else {
846 Out << ' ';
847 // Print the type
848 printType(Operand->getType());
849 // Print parameter attributes list
850 if (Attrs != ParamAttr::None)
851 Out << ' ' << ParamAttrsList::getParamAttrsText(Attrs);
852 // Print the operand
853 WriteAsOperandInternal(Out, Operand, TypeNames, &Machine);
854 }
855}
Chris Lattner00950542001-06-06 20:29:01 +0000856
Chris Lattnerc1824992001-10-29 16:05:51 +0000857void AssemblyWriter::printModule(const Module *M) {
Chris Lattner31ab1b32005-03-02 23:12:40 +0000858 if (!M->getModuleIdentifier().empty() &&
Misha Brukmanfd939082005-04-21 23:48:37 +0000859 // Don't print the ID if it will start a new line (which would
Chris Lattner31ab1b32005-03-02 23:12:40 +0000860 // require a comment char before it).
861 M->getModuleIdentifier().find('\n') == std::string::npos)
862 Out << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
863
Owen Andersoncf7ff2b2006-10-18 02:21:12 +0000864 if (!M->getDataLayout().empty())
Chris Lattnerd2f9e602006-10-22 06:06:56 +0000865 Out << "target datalayout = \"" << M->getDataLayout() << "\"\n";
Reid Spencercddc86f2004-07-25 21:44:54 +0000866 if (!M->getTargetTriple().empty())
Reid Spencerc9a1f0d2004-07-25 21:29:43 +0000867 Out << "target triple = \"" << M->getTargetTriple() << "\"\n";
Misha Brukmanfd939082005-04-21 23:48:37 +0000868
Chris Lattnercc041ba2006-01-24 04:13:11 +0000869 if (!M->getModuleInlineAsm().empty()) {
Chris Lattner42a162e2006-01-24 00:45:30 +0000870 // Split the string into lines, to make it easier to read the .ll file.
Chris Lattnercc041ba2006-01-24 04:13:11 +0000871 std::string Asm = M->getModuleInlineAsm();
Chris Lattner42a162e2006-01-24 00:45:30 +0000872 size_t CurPos = 0;
873 size_t NewLine = Asm.find_first_of('\n', CurPos);
874 while (NewLine != std::string::npos) {
875 // We found a newline, print the portion of the asm string from the
876 // last newline up to this newline.
877 Out << "module asm \"";
878 PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.begin()+NewLine),
879 Out);
880 Out << "\"\n";
881 CurPos = NewLine+1;
882 NewLine = Asm.find_first_of('\n', CurPos);
883 }
Chris Lattner71cdba32006-01-24 00:40:17 +0000884 Out << "module asm \"";
Chris Lattner42a162e2006-01-24 00:45:30 +0000885 PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.end()), Out);
Chris Lattner18365502006-01-23 23:03:36 +0000886 Out << "\"\n";
887 }
888
Chris Lattner44da7d72004-09-14 05:06:58 +0000889 // Loop over the dependent libraries and emit them.
Chris Lattnercfe97b72004-09-14 04:51:44 +0000890 Module::lib_iterator LI = M->lib_begin();
891 Module::lib_iterator LE = M->lib_end();
Reid Spencercddc86f2004-07-25 21:44:54 +0000892 if (LI != LE) {
Chris Lattnercfe97b72004-09-14 04:51:44 +0000893 Out << "deplibs = [ ";
894 while (LI != LE) {
Chris Lattner44da7d72004-09-14 05:06:58 +0000895 Out << '"' << *LI << '"';
Reid Spencerc9a1f0d2004-07-25 21:29:43 +0000896 ++LI;
Chris Lattnercfe97b72004-09-14 04:51:44 +0000897 if (LI != LE)
898 Out << ", ";
Reid Spencerc9a1f0d2004-07-25 21:29:43 +0000899 }
900 Out << " ]\n";
Reid Spencer83f6a772004-07-25 18:08:18 +0000901 }
Reid Spencere59eaf42004-09-13 23:44:23 +0000902
Chris Lattner44da7d72004-09-14 05:06:58 +0000903 // Loop over the symbol table, emitting all named constants.
Reid Spencer78d033e2007-01-06 07:24:44 +0000904 printTypeSymbolTable(M->getTypeSymbolTable());
Misha Brukmanfd939082005-04-21 23:48:37 +0000905
Chris Lattnerd6d826c2006-12-06 04:41:52 +0000906 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
907 I != E; ++I)
Chris Lattner7e708292002-06-25 16:13:24 +0000908 printGlobal(I);
Chris Lattner69dacfc2007-04-26 02:24:10 +0000909
910 // Output all aliases.
911 if (!M->alias_empty()) Out << "\n";
912 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
913 I != E; ++I)
914 printAlias(I);
Chris Lattner007377f2001-09-07 16:36:04 +0000915
Chris Lattner44da7d72004-09-14 05:06:58 +0000916 // Output all of the functions.
Chris Lattner7e708292002-06-25 16:13:24 +0000917 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
918 printFunction(I);
Chris Lattner007377f2001-09-07 16:36:04 +0000919}
920
Chris Lattnerc1824992001-10-29 16:05:51 +0000921void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Reid Spencer3702d262007-01-26 08:02:52 +0000922 if (GV->hasName()) Out << getLLVMName(GV->getName(), GlobalPrefix) << " = ";
Chris Lattnerd70684f2001-09-18 04:01:05 +0000923
Misha Brukmanfd939082005-04-21 23:48:37 +0000924 if (!GV->hasInitializer())
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000925 switch (GV->getLinkage()) {
926 case GlobalValue::DLLImportLinkage: Out << "dllimport "; break;
927 case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break;
928 default: Out << "external "; break;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000929 } else {
Chris Lattner4ad02e72003-04-16 20:28:45 +0000930 switch (GV->getLinkage()) {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000931 case GlobalValue::InternalLinkage: Out << "internal "; break;
932 case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
933 case GlobalValue::WeakLinkage: Out << "weak "; break;
934 case GlobalValue::AppendingLinkage: Out << "appending "; break;
935 case GlobalValue::DLLImportLinkage: Out << "dllimport "; break;
936 case GlobalValue::DLLExportLinkage: Out << "dllexport "; break;
937 case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break;
938 case GlobalValue::ExternalLinkage: break;
Misha Brukman3c8f2c62004-11-14 21:04:34 +0000939 case GlobalValue::GhostLinkage:
Bill Wendlinge8156192006-12-07 01:30:32 +0000940 cerr << "GhostLinkage not allowed in AsmWriter!\n";
Misha Brukman3c8f2c62004-11-14 21:04:34 +0000941 abort();
Chris Lattner4ad02e72003-04-16 20:28:45 +0000942 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000943 switch (GV->getVisibility()) {
Chris Lattner9a40c022007-01-15 18:28:18 +0000944 default: assert(0 && "Invalid visibility style!");
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000945 case GlobalValue::DefaultVisibility: break;
946 case GlobalValue::HiddenVisibility: Out << "hidden "; break;
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +0000947 case GlobalValue::ProtectedVisibility: Out << "protected "; break;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000948 }
949 }
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +0000950
951 if (GV->isThreadLocal()) Out << "thread_local ";
Misha Brukman0313e0b2004-06-21 21:53:56 +0000952 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattner7a176752001-12-04 00:03:30 +0000953 printType(GV->getType()->getElementType());
Chris Lattnerd70684f2001-09-18 04:01:05 +0000954
Reid Spencer79703962004-07-17 23:47:01 +0000955 if (GV->hasInitializer()) {
956 Constant* C = cast<Constant>(GV->getInitializer());
957 assert(C && "GlobalVar initializer isn't constant?");
Chris Lattner2fcfdb72006-12-06 06:24:27 +0000958 writeOperand(GV->getInitializer(), false);
Reid Spencer79703962004-07-17 23:47:01 +0000959 }
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000960
Christopher Lambfe63fb92007-12-11 08:59:05 +0000961 if (unsigned AddressSpace = GV->getType()->getAddressSpace())
962 Out << " addrspace(" << AddressSpace << ") ";
963
Chris Lattner60962db2005-11-12 00:10:19 +0000964 if (GV->hasSection())
965 Out << ", section \"" << GV->getSection() << '"';
966 if (GV->getAlignment())
Chris Lattner30caa282005-11-06 06:48:53 +0000967 Out << ", align " << GV->getAlignment();
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000968
Chris Lattner7e708292002-06-25 16:13:24 +0000969 printInfoComment(*GV);
Misha Brukman0313e0b2004-06-21 21:53:56 +0000970 Out << "\n";
Chris Lattner70cc3392001-09-10 07:58:01 +0000971}
972
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000973void AssemblyWriter::printAlias(const GlobalAlias *GA) {
974 Out << getLLVMName(GA->getName(), GlobalPrefix) << " = ";
975 switch (GA->getVisibility()) {
976 default: assert(0 && "Invalid visibility style!");
977 case GlobalValue::DefaultVisibility: break;
978 case GlobalValue::HiddenVisibility: Out << "hidden "; break;
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +0000979 case GlobalValue::ProtectedVisibility: Out << "protected "; break;
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000980 }
981
982 Out << "alias ";
983
984 switch (GA->getLinkage()) {
985 case GlobalValue::WeakLinkage: Out << "weak "; break;
986 case GlobalValue::InternalLinkage: Out << "internal "; break;
987 case GlobalValue::ExternalLinkage: break;
988 default:
989 assert(0 && "Invalid alias linkage");
990 }
991
Anton Korobeynikovc6c98af2007-04-29 18:02:48 +0000992 const Constant *Aliasee = GA->getAliasee();
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000993
994 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Aliasee)) {
995 printType(GV->getType());
996 Out << " " << getLLVMName(GV->getName(), GlobalPrefix);
997 } else if (const Function *F = dyn_cast<Function>(Aliasee)) {
998 printType(F->getFunctionType());
999 Out << "* ";
1000
1001 if (!F->getName().empty())
1002 Out << getLLVMName(F->getName(), GlobalPrefix);
1003 else
1004 Out << "@\"\"";
Anton Korobeynikova80e1182007-04-28 13:45:00 +00001005 } else {
1006 const ConstantExpr *CE = 0;
1007 if ((CE = dyn_cast<ConstantExpr>(Aliasee)) &&
1008 (CE->getOpcode() == Instruction::BitCast)) {
1009 writeOperand(CE, false);
1010 } else
1011 assert(0 && "Unsupported aliasee");
1012 }
1013
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001014 printInfoComment(*GA);
1015 Out << "\n";
1016}
1017
Reid Spencer78d033e2007-01-06 07:24:44 +00001018void AssemblyWriter::printTypeSymbolTable(const TypeSymbolTable &ST) {
Reid Spencer9231ac82004-05-25 08:53:40 +00001019 // Print the types.
Reid Spencer78d033e2007-01-06 07:24:44 +00001020 for (TypeSymbolTable::const_iterator TI = ST.begin(), TE = ST.end();
1021 TI != TE; ++TI) {
Reid Spencer3702d262007-01-26 08:02:52 +00001022 Out << "\t" << getLLVMName(TI->first, LocalPrefix) << " = type ";
Reid Spencer9231ac82004-05-25 08:53:40 +00001023
1024 // Make sure we print out at least one level of the type structure, so
1025 // that we do not get %FILE = type %FILE
1026 //
1027 printTypeAtLeastOneLevel(TI->second) << "\n";
1028 }
Reid Spencer78d033e2007-01-06 07:24:44 +00001029}
1030
Misha Brukmanab5c6002004-03-02 00:22:19 +00001031/// printFunction - Print all aspects of a function.
1032///
Chris Lattner7e708292002-06-25 16:13:24 +00001033void AssemblyWriter::printFunction(const Function *F) {
Chris Lattner00950542001-06-06 20:29:01 +00001034 // Print out the return type and name...
Misha Brukman0313e0b2004-06-21 21:53:56 +00001035 Out << "\n";
Chris Lattner4ad02e72003-04-16 20:28:45 +00001036
Misha Brukman0313e0b2004-06-21 21:53:56 +00001037 if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out);
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001038
Reid Spencer5cbf9852007-01-30 20:08:39 +00001039 if (F->isDeclaration())
Chris Lattner3aa60662007-08-19 22:15:26 +00001040 Out << "declare ";
1041 else
Reid Spencerb951bc02006-12-29 20:29:48 +00001042 Out << "define ";
Chris Lattner3aa60662007-08-19 22:15:26 +00001043
1044 switch (F->getLinkage()) {
1045 case GlobalValue::InternalLinkage: Out << "internal "; break;
1046 case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
1047 case GlobalValue::WeakLinkage: Out << "weak "; break;
1048 case GlobalValue::AppendingLinkage: Out << "appending "; break;
1049 case GlobalValue::DLLImportLinkage: Out << "dllimport "; break;
1050 case GlobalValue::DLLExportLinkage: Out << "dllexport "; break;
1051 case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break;
1052 case GlobalValue::ExternalLinkage: break;
1053 case GlobalValue::GhostLinkage:
1054 cerr << "GhostLinkage not allowed in AsmWriter!\n";
1055 abort();
1056 }
1057 switch (F->getVisibility()) {
1058 default: assert(0 && "Invalid visibility style!");
1059 case GlobalValue::DefaultVisibility: break;
1060 case GlobalValue::HiddenVisibility: Out << "hidden "; break;
1061 case GlobalValue::ProtectedVisibility: Out << "protected "; break;
Reid Spencerb951bc02006-12-29 20:29:48 +00001062 }
Chris Lattner4ad02e72003-04-16 20:28:45 +00001063
Chris Lattnerd5118982005-05-06 20:26:43 +00001064 // Print the calling convention.
1065 switch (F->getCallingConv()) {
1066 case CallingConv::C: break; // default
Anton Korobeynikovf8248682006-09-20 22:03:51 +00001067 case CallingConv::Fast: Out << "fastcc "; break;
1068 case CallingConv::Cold: Out << "coldcc "; break;
1069 case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break;
1070 case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break;
Chris Lattnerd5118982005-05-06 20:26:43 +00001071 default: Out << "cc" << F->getCallingConv() << " "; break;
1072 }
1073
Reid Spencerbd5db8e2006-12-31 05:24:50 +00001074 const FunctionType *FT = F->getFunctionType();
Duncan Sandsdc024672007-11-27 13:23:08 +00001075 const ParamAttrsList *Attrs = F->getParamAttrs();
Misha Brukman40c732c2004-06-04 21:11:51 +00001076 printType(F->getReturnType()) << ' ';
Chris Lattner4d45bd02003-10-18 05:57:43 +00001077 if (!F->getName().empty())
Reid Spencer3702d262007-01-26 08:02:52 +00001078 Out << getLLVMName(F->getName(), GlobalPrefix);
Chris Lattner4d45bd02003-10-18 05:57:43 +00001079 else
Reid Spencer3702d262007-01-26 08:02:52 +00001080 Out << "@\"\"";
Misha Brukman0313e0b2004-06-21 21:53:56 +00001081 Out << '(';
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001082 Machine.incorporateFunction(F);
Chris Lattner007377f2001-09-07 16:36:04 +00001083
Chris Lattnerc1824992001-10-29 16:05:51 +00001084 // Loop over the arguments, printing them...
Chris Lattner007377f2001-09-07 16:36:04 +00001085
Reid Spencerbd5db8e2006-12-31 05:24:50 +00001086 unsigned Idx = 1;
Chris Lattner8dcd2f12007-04-18 00:57:22 +00001087 if (!F->isDeclaration()) {
1088 // If this isn't a declaration, print the argument names as well.
1089 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
1090 I != E; ++I) {
1091 // Insert commas as we go... the first arg doesn't get a comma
1092 if (I != F->arg_begin()) Out << ", ";
1093 printArgument(I, (Attrs ? Attrs->getParamAttrs(Idx)
1094 : uint16_t(ParamAttr::None)));
1095 Idx++;
1096 }
1097 } else {
1098 // Otherwise, print the types from the function type.
1099 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
1100 // Insert commas as we go... the first arg doesn't get a comma
1101 if (i) Out << ", ";
1102
1103 // Output type...
1104 printType(FT->getParamType(i));
1105
1106 unsigned ArgAttrs = ParamAttr::None;
1107 if (Attrs) ArgAttrs = Attrs->getParamAttrs(i+1);
1108 if (ArgAttrs != ParamAttr::None)
1109 Out << ' ' << ParamAttrsList::getParamAttrsText(ArgAttrs);
1110 }
Reid Spencerbd5db8e2006-12-31 05:24:50 +00001111 }
Chris Lattner007377f2001-09-07 16:36:04 +00001112
1113 // Finish printing arguments...
Chris Lattner7e708292002-06-25 16:13:24 +00001114 if (FT->isVarArg()) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001115 if (FT->getNumParams()) Out << ", ";
1116 Out << "..."; // Output varargs portion of signature!
Chris Lattner007377f2001-09-07 16:36:04 +00001117 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00001118 Out << ')';
Reid Spencer18da0722007-04-11 02:44:20 +00001119 if (Attrs && Attrs->getParamAttrs(0) != ParamAttr::None)
Reid Spencerb138a062007-04-09 06:10:42 +00001120 Out << ' ' << Attrs->getParamAttrsTextByIndex(0);
Chris Lattner60962db2005-11-12 00:10:19 +00001121 if (F->hasSection())
1122 Out << " section \"" << F->getSection() << '"';
Chris Lattner30caa282005-11-06 06:48:53 +00001123 if (F->getAlignment())
1124 Out << " align " << F->getAlignment();
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00001125 if (F->hasCollector())
1126 Out << " gc \"" << F->getCollector() << '"';
Chris Lattner60962db2005-11-12 00:10:19 +00001127
Reid Spencer5cbf9852007-01-30 20:08:39 +00001128 if (F->isDeclaration()) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001129 Out << "\n";
Chris Lattner03e2acb2002-05-06 03:00:40 +00001130 } else {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001131 Out << " {";
Misha Brukmanfd939082005-04-21 23:48:37 +00001132
Chris Lattnerb5794002002-04-07 22:49:37 +00001133 // Output all of its basic blocks... for the function
Chris Lattner7e708292002-06-25 16:13:24 +00001134 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
1135 printBasicBlock(I);
Chris Lattner007377f2001-09-07 16:36:04 +00001136
Misha Brukman0313e0b2004-06-21 21:53:56 +00001137 Out << "}\n";
Chris Lattner007377f2001-09-07 16:36:04 +00001138 }
1139
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001140 Machine.purgeFunction();
Chris Lattner00950542001-06-06 20:29:01 +00001141}
1142
Misha Brukmanab5c6002004-03-02 00:22:19 +00001143/// printArgument - This member is called for every argument that is passed into
1144/// the function. Simply print it out
1145///
Reid Spencerb138a062007-04-09 06:10:42 +00001146void AssemblyWriter::printArgument(const Argument *Arg, uint16_t Attrs) {
Chris Lattner00950542001-06-06 20:29:01 +00001147 // Output type...
Chris Lattnerc1824992001-10-29 16:05:51 +00001148 printType(Arg->getType());
Misha Brukmanfd939082005-04-21 23:48:37 +00001149
Duncan Sandsdc024672007-11-27 13:23:08 +00001150 // Output parameter attributes list
Reid Spencer18da0722007-04-11 02:44:20 +00001151 if (Attrs != ParamAttr::None)
Reid Spencerb138a062007-04-09 06:10:42 +00001152 Out << ' ' << ParamAttrsList::getParamAttrsText(Attrs);
Reid Spencerbd5db8e2006-12-31 05:24:50 +00001153
Chris Lattner00950542001-06-06 20:29:01 +00001154 // Output name, if available...
1155 if (Arg->hasName())
Reid Spencer3702d262007-01-26 08:02:52 +00001156 Out << ' ' << getLLVMName(Arg->getName(), LocalPrefix);
Chris Lattner00950542001-06-06 20:29:01 +00001157}
1158
Misha Brukmanab5c6002004-03-02 00:22:19 +00001159/// printBasicBlock - This member is called for each basic block in a method.
1160///
Chris Lattnerc1824992001-10-29 16:05:51 +00001161void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Chris Lattner00950542001-06-06 20:29:01 +00001162 if (BB->hasName()) { // Print out the label if it exists...
Reid Spencer3702d262007-01-26 08:02:52 +00001163 Out << "\n" << getLLVMName(BB->getName(), LabelPrefix) << ':';
Chris Lattnerafc38682002-05-14 16:02:05 +00001164 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Misha Brukman0313e0b2004-06-21 21:53:56 +00001165 Out << "\n; <label>:";
Chris Lattner22379bc2007-01-11 03:54:27 +00001166 int Slot = Machine.getLocalSlot(BB);
Chris Lattner69566452004-06-09 19:41:19 +00001167 if (Slot != -1)
Misha Brukman0313e0b2004-06-21 21:53:56 +00001168 Out << Slot;
Chris Lattner69566452004-06-09 19:41:19 +00001169 else
Misha Brukman0313e0b2004-06-21 21:53:56 +00001170 Out << "<badref>";
Chris Lattner061269b2002-10-02 19:38:55 +00001171 }
Chris Lattner4e4d8622003-11-20 00:09:43 +00001172
1173 if (BB->getParent() == 0)
Misha Brukman0313e0b2004-06-21 21:53:56 +00001174 Out << "\t\t; Error: Block without parent!";
Chris Lattner4e4d8622003-11-20 00:09:43 +00001175 else {
Dan Gohmanecb7a772007-03-22 16:38:57 +00001176 if (BB != &BB->getParent()->getEntryBlock()) { // Not the entry block?
Chris Lattner4e4d8622003-11-20 00:09:43 +00001177 // Output predecessors for the block...
Misha Brukman0313e0b2004-06-21 21:53:56 +00001178 Out << "\t\t;";
Chris Lattner4e4d8622003-11-20 00:09:43 +00001179 pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
Misha Brukmanfd939082005-04-21 23:48:37 +00001180
Chris Lattner4e4d8622003-11-20 00:09:43 +00001181 if (PI == PE) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001182 Out << " No predecessors!";
Chris Lattner4e4d8622003-11-20 00:09:43 +00001183 } else {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001184 Out << " preds =";
Chris Lattner2fcfdb72006-12-06 06:24:27 +00001185 writeOperand(*PI, false);
Chris Lattner4e4d8622003-11-20 00:09:43 +00001186 for (++PI; PI != PE; ++PI) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001187 Out << ',';
Chris Lattner2fcfdb72006-12-06 06:24:27 +00001188 writeOperand(*PI, false);
Chris Lattner4e4d8622003-11-20 00:09:43 +00001189 }
Chris Lattner40efcec2003-11-16 22:59:57 +00001190 }
Chris Lattner061269b2002-10-02 19:38:55 +00001191 }
Chris Lattner00950542001-06-06 20:29:01 +00001192 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001193
Misha Brukman0313e0b2004-06-21 21:53:56 +00001194 Out << "\n";
Chris Lattner00950542001-06-06 20:29:01 +00001195
Misha Brukman0313e0b2004-06-21 21:53:56 +00001196 if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out);
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001197
Chris Lattner007377f2001-09-07 16:36:04 +00001198 // Output all of the instructions in the basic block...
Chris Lattner7e708292002-06-25 16:13:24 +00001199 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
1200 printInstruction(*I);
Chris Lattner9f717ef2004-03-08 18:51:45 +00001201
Misha Brukman0313e0b2004-06-21 21:53:56 +00001202 if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out);
Chris Lattner00950542001-06-06 20:29:01 +00001203}
1204
Chris Lattnere02fa852001-10-13 06:42:36 +00001205
Misha Brukmanab5c6002004-03-02 00:22:19 +00001206/// printInfoComment - Print a little comment after the instruction indicating
1207/// which slot it occupies.
1208///
Chris Lattner7e708292002-06-25 16:13:24 +00001209void AssemblyWriter::printInfoComment(const Value &V) {
1210 if (V.getType() != Type::VoidTy) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001211 Out << "\t\t; <";
Misha Brukman40c732c2004-06-04 21:11:51 +00001212 printType(V.getType()) << '>';
Chris Lattnere02fa852001-10-13 06:42:36 +00001213
Chris Lattner7e708292002-06-25 16:13:24 +00001214 if (!V.hasName()) {
Chris Lattner22379bc2007-01-11 03:54:27 +00001215 int SlotNum;
1216 if (const GlobalValue *GV = dyn_cast<GlobalValue>(&V))
1217 SlotNum = Machine.getGlobalSlot(GV);
1218 else
1219 SlotNum = Machine.getLocalSlot(&V);
Chris Lattner69566452004-06-09 19:41:19 +00001220 if (SlotNum == -1)
Misha Brukman0313e0b2004-06-21 21:53:56 +00001221 Out << ":<badref>";
Reid Spencerfc621e22004-06-09 15:26:53 +00001222 else
Misha Brukman0313e0b2004-06-21 21:53:56 +00001223 Out << ':' << SlotNum; // Print out the def slot taken.
Chris Lattnere02fa852001-10-13 06:42:36 +00001224 }
Chris Lattner5c461402005-02-01 01:24:01 +00001225 Out << " [#uses=" << V.getNumUses() << ']'; // Output # uses
Chris Lattnere02fa852001-10-13 06:42:36 +00001226 }
1227}
1228
Reid Spencer3a9ec242006-08-28 01:02:49 +00001229// This member is called for each Instruction in a function..
Chris Lattner7e708292002-06-25 16:13:24 +00001230void AssemblyWriter::printInstruction(const Instruction &I) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001231 if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out);
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001232
Misha Brukman0313e0b2004-06-21 21:53:56 +00001233 Out << "\t";
Chris Lattner00950542001-06-06 20:29:01 +00001234
1235 // Print out name if it exists...
Chris Lattner7e708292002-06-25 16:13:24 +00001236 if (I.hasName())
Reid Spencer3702d262007-01-26 08:02:52 +00001237 Out << getLLVMName(I.getName(), LocalPrefix) << " = ";
Chris Lattner00950542001-06-06 20:29:01 +00001238
Chris Lattnerddb6db42005-05-06 05:51:46 +00001239 // If this is a volatile load or store, print out the volatile marker.
Chris Lattnere5e475e2003-09-08 17:45:59 +00001240 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
Chris Lattnerddb6db42005-05-06 05:51:46 +00001241 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile())) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001242 Out << "volatile ";
Chris Lattnerddb6db42005-05-06 05:51:46 +00001243 } else if (isa<CallInst>(I) && cast<CallInst>(I).isTailCall()) {
1244 // If this is a call, check if it's a tail call.
1245 Out << "tail ";
1246 }
Chris Lattnere5e475e2003-09-08 17:45:59 +00001247
Chris Lattner00950542001-06-06 20:29:01 +00001248 // Print out the opcode...
Misha Brukman0313e0b2004-06-21 21:53:56 +00001249 Out << I.getOpcodeName();
Chris Lattner00950542001-06-06 20:29:01 +00001250
Reid Spencer74f16422006-12-03 06:27:29 +00001251 // Print out the compare instruction predicates
1252 if (const FCmpInst *FCI = dyn_cast<FCmpInst>(&I)) {
Reid Spencer81dfeb32006-12-04 05:19:18 +00001253 Out << " " << getPredicateText(FCI->getPredicate());
Reid Spencer74f16422006-12-03 06:27:29 +00001254 } else if (const ICmpInst *ICI = dyn_cast<ICmpInst>(&I)) {
Reid Spencer81dfeb32006-12-04 05:19:18 +00001255 Out << " " << getPredicateText(ICI->getPredicate());
Reid Spencer74f16422006-12-03 06:27:29 +00001256 }
1257
Chris Lattner00950542001-06-06 20:29:01 +00001258 // Print out the type of the operands...
Chris Lattner7e708292002-06-25 16:13:24 +00001259 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
Chris Lattner00950542001-06-06 20:29:01 +00001260
1261 // Special case conditional branches to swizzle the condition out to the front
Chris Lattner7e708292002-06-25 16:13:24 +00001262 if (isa<BranchInst>(I) && I.getNumOperands() > 1) {
1263 writeOperand(I.getOperand(2), true);
Misha Brukman0313e0b2004-06-21 21:53:56 +00001264 Out << ',';
Chris Lattner00950542001-06-06 20:29:01 +00001265 writeOperand(Operand, true);
Misha Brukman0313e0b2004-06-21 21:53:56 +00001266 Out << ',';
Chris Lattner7e708292002-06-25 16:13:24 +00001267 writeOperand(I.getOperand(1), true);
Chris Lattner00950542001-06-06 20:29:01 +00001268
Chris Lattner94dc1f22002-04-13 18:34:38 +00001269 } else if (isa<SwitchInst>(I)) {
Chris Lattner00950542001-06-06 20:29:01 +00001270 // Special case switch statement to get formatting nice and correct...
Misha Brukman0313e0b2004-06-21 21:53:56 +00001271 writeOperand(Operand , true); Out << ',';
1272 writeOperand(I.getOperand(1), true); Out << " [";
Chris Lattner00950542001-06-06 20:29:01 +00001273
Chris Lattner7e708292002-06-25 16:13:24 +00001274 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001275 Out << "\n\t\t";
1276 writeOperand(I.getOperand(op ), true); Out << ',';
Chris Lattner7e708292002-06-25 16:13:24 +00001277 writeOperand(I.getOperand(op+1), true);
Chris Lattner00950542001-06-06 20:29:01 +00001278 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00001279 Out << "\n\t]";
Chris Lattnerb00c5822001-10-02 03:41:24 +00001280 } else if (isa<PHINode>(I)) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001281 Out << ' ';
Chris Lattner7e708292002-06-25 16:13:24 +00001282 printType(I.getType());
Misha Brukman0313e0b2004-06-21 21:53:56 +00001283 Out << ' ';
Chris Lattner00950542001-06-06 20:29:01 +00001284
Chris Lattner7e708292002-06-25 16:13:24 +00001285 for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001286 if (op) Out << ", ";
Misha Brukmanfd939082005-04-21 23:48:37 +00001287 Out << '[';
Misha Brukman0313e0b2004-06-21 21:53:56 +00001288 writeOperand(I.getOperand(op ), false); Out << ',';
1289 writeOperand(I.getOperand(op+1), false); Out << " ]";
Chris Lattnerc24d2082001-06-11 15:04:20 +00001290 }
Chris Lattnere02fa852001-10-13 06:42:36 +00001291 } else if (isa<ReturnInst>(I) && !Operand) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001292 Out << " void";
Chris Lattnerd5118982005-05-06 20:26:43 +00001293 } else if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
1294 // Print the calling convention being used.
1295 switch (CI->getCallingConv()) {
1296 case CallingConv::C: break; // default
Chris Lattner0deaab82006-05-19 21:58:52 +00001297 case CallingConv::Fast: Out << " fastcc"; break;
1298 case CallingConv::Cold: Out << " coldcc"; break;
Chris Lattnerb28a6dc2007-11-18 18:32:16 +00001299 case CallingConv::X86_StdCall: Out << " x86_stdcallcc"; break;
1300 case CallingConv::X86_FastCall: Out << " x86_fastcallcc"; break;
Chris Lattnerd5118982005-05-06 20:26:43 +00001301 default: Out << " cc" << CI->getCallingConv(); break;
1302 }
1303
Reid Spencerb138a062007-04-09 06:10:42 +00001304 const PointerType *PTy = cast<PointerType>(Operand->getType());
1305 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1306 const Type *RetTy = FTy->getReturnType();
Duncan Sandsdc024672007-11-27 13:23:08 +00001307 const ParamAttrsList *PAL = CI->getParamAttrs();
Chris Lattner268de042001-11-06 21:28:12 +00001308
Chris Lattner7a012292003-08-05 15:34:45 +00001309 // If possible, print out the short form of the call instruction. We can
Chris Lattnerb5794002002-04-07 22:49:37 +00001310 // only do this if the first argument is a pointer to a nonvararg function,
Chris Lattner7a012292003-08-05 15:34:45 +00001311 // and if the return type is not a pointer to a function.
Chris Lattner268de042001-11-06 21:28:12 +00001312 //
Chris Lattner7a012292003-08-05 15:34:45 +00001313 if (!FTy->isVarArg() &&
Misha Brukmanfd939082005-04-21 23:48:37 +00001314 (!isa<PointerType>(RetTy) ||
Chris Lattnerc1b27182002-07-25 20:58:51 +00001315 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001316 Out << ' '; printType(RetTy);
Chris Lattner268de042001-11-06 21:28:12 +00001317 writeOperand(Operand, false);
1318 } else {
1319 writeOperand(Operand, true);
1320 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00001321 Out << '(';
Reid Spencerbd5db8e2006-12-31 05:24:50 +00001322 for (unsigned op = 1, Eop = I.getNumOperands(); op < Eop; ++op) {
1323 if (op > 1)
1324 Out << ',';
Duncan Sandsdc024672007-11-27 13:23:08 +00001325 writeParamOperand(I.getOperand(op), PAL ? PAL->getParamAttrs(op) : 0);
Chris Lattner00950542001-06-06 20:29:01 +00001326 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00001327 Out << " )";
Reid Spencer18da0722007-04-11 02:44:20 +00001328 if (PAL && PAL->getParamAttrs(0) != ParamAttr::None)
Reid Spencerb138a062007-04-09 06:10:42 +00001329 Out << ' ' << PAL->getParamAttrsTextByIndex(0);
Chris Lattner7e708292002-06-25 16:13:24 +00001330 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Reid Spencerb138a062007-04-09 06:10:42 +00001331 const PointerType *PTy = cast<PointerType>(Operand->getType());
1332 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1333 const Type *RetTy = FTy->getReturnType();
Duncan Sandsdc024672007-11-27 13:23:08 +00001334 const ParamAttrsList *PAL = II->getParamAttrs();
Chris Lattner7a012292003-08-05 15:34:45 +00001335
Chris Lattnerd5118982005-05-06 20:26:43 +00001336 // Print the calling convention being used.
1337 switch (II->getCallingConv()) {
1338 case CallingConv::C: break; // default
Chris Lattner0deaab82006-05-19 21:58:52 +00001339 case CallingConv::Fast: Out << " fastcc"; break;
1340 case CallingConv::Cold: Out << " coldcc"; break;
Anton Korobeynikovf8248682006-09-20 22:03:51 +00001341 case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break;
1342 case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break;
Chris Lattnerd5118982005-05-06 20:26:43 +00001343 default: Out << " cc" << II->getCallingConv(); break;
1344 }
1345
Chris Lattner7a012292003-08-05 15:34:45 +00001346 // If possible, print out the short form of the invoke instruction. We can
1347 // only do this if the first argument is a pointer to a nonvararg function,
1348 // and if the return type is not a pointer to a function.
1349 //
1350 if (!FTy->isVarArg() &&
Misha Brukmanfd939082005-04-21 23:48:37 +00001351 (!isa<PointerType>(RetTy) ||
Chris Lattner7a012292003-08-05 15:34:45 +00001352 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001353 Out << ' '; printType(RetTy);
Chris Lattner7a012292003-08-05 15:34:45 +00001354 writeOperand(Operand, false);
1355 } else {
1356 writeOperand(Operand, true);
1357 }
1358
Misha Brukman0313e0b2004-06-21 21:53:56 +00001359 Out << '(';
Reid Spencerbd5db8e2006-12-31 05:24:50 +00001360 for (unsigned op = 3, Eop = I.getNumOperands(); op < Eop; ++op) {
1361 if (op > 3)
1362 Out << ',';
Duncan Sandsdc024672007-11-27 13:23:08 +00001363 writeParamOperand(I.getOperand(op), PAL ? PAL->getParamAttrs(op-2) : 0);
Chris Lattnere02fa852001-10-13 06:42:36 +00001364 }
1365
Reid Spencer2c261782007-01-05 17:06:19 +00001366 Out << " )";
Reid Spencer18da0722007-04-11 02:44:20 +00001367 if (PAL && PAL->getParamAttrs(0) != ParamAttr::None)
Reid Spencerb138a062007-04-09 06:10:42 +00001368 Out << " " << PAL->getParamAttrsTextByIndex(0);
Reid Spencer2c261782007-01-05 17:06:19 +00001369 Out << "\n\t\t\tto";
Chris Lattnere02fa852001-10-13 06:42:36 +00001370 writeOperand(II->getNormalDest(), true);
Misha Brukman0313e0b2004-06-21 21:53:56 +00001371 Out << " unwind";
Chris Lattneraeb2a1d2004-02-08 21:44:31 +00001372 writeOperand(II->getUnwindDest(), true);
Chris Lattnere02fa852001-10-13 06:42:36 +00001373
Chris Lattner7e708292002-06-25 16:13:24 +00001374 } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(&I)) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001375 Out << ' ';
Chris Lattner94dc1f22002-04-13 18:34:38 +00001376 printType(AI->getType()->getElementType());
1377 if (AI->isArrayAllocation()) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001378 Out << ',';
Chris Lattner94dc1f22002-04-13 18:34:38 +00001379 writeOperand(AI->getArraySize(), true);
Chris Lattner00950542001-06-06 20:29:01 +00001380 }
Nate Begeman14b05292005-11-05 09:21:28 +00001381 if (AI->getAlignment()) {
Chris Lattner9fad0b92005-11-05 21:20:34 +00001382 Out << ", align " << AI->getAlignment();
Nate Begeman14b05292005-11-05 09:21:28 +00001383 }
Chris Lattnere02fa852001-10-13 06:42:36 +00001384 } else if (isa<CastInst>(I)) {
Chris Lattner41495a22003-11-17 01:17:04 +00001385 if (Operand) writeOperand(Operand, true); // Work with broken code
Misha Brukman0313e0b2004-06-21 21:53:56 +00001386 Out << " to ";
Chris Lattner7e708292002-06-25 16:13:24 +00001387 printType(I.getType());
Chris Lattner4d45bd02003-10-18 05:57:43 +00001388 } else if (isa<VAArgInst>(I)) {
Chris Lattner41495a22003-11-17 01:17:04 +00001389 if (Operand) writeOperand(Operand, true); // Work with broken code
Misha Brukman0313e0b2004-06-21 21:53:56 +00001390 Out << ", ";
Chris Lattner8f77dae2003-05-08 02:44:12 +00001391 printType(I.getType());
Chris Lattner00950542001-06-06 20:29:01 +00001392 } else if (Operand) { // Print the normal way...
1393
Misha Brukmanfd939082005-04-21 23:48:37 +00001394 // PrintAllTypes - Instructions who have operands of all the same type
Chris Lattner00950542001-06-06 20:29:01 +00001395 // omit the type from all but the first operand. If the instruction has
1396 // different type operands (for example br), then they are all printed.
1397 bool PrintAllTypes = false;
1398 const Type *TheType = Operand->getType();
Chris Lattner00950542001-06-06 20:29:01 +00001399
Reid Spencerebe57e32007-02-02 13:54:55 +00001400 // Select, Store and ShuffleVector always print all types.
Reid Spencer832254e2007-02-02 02:16:23 +00001401 if (isa<SelectInst>(I) || isa<StoreInst>(I) || isa<ShuffleVectorInst>(I)) {
Chris Lattnerffd9bf42003-04-16 20:20:02 +00001402 PrintAllTypes = true;
1403 } else {
1404 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
1405 Operand = I.getOperand(i);
1406 if (Operand->getType() != TheType) {
1407 PrintAllTypes = true; // We have differing types! Print them all!
1408 break;
1409 }
Chris Lattner00950542001-06-06 20:29:01 +00001410 }
1411 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001412
Chris Lattnerc1824992001-10-29 16:05:51 +00001413 if (!PrintAllTypes) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001414 Out << ' ';
Chris Lattnerffd9bf42003-04-16 20:20:02 +00001415 printType(TheType);
Chris Lattnerc1824992001-10-29 16:05:51 +00001416 }
Chris Lattner00950542001-06-06 20:29:01 +00001417
Chris Lattner7e708292002-06-25 16:13:24 +00001418 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001419 if (i) Out << ',';
Chris Lattner7e708292002-06-25 16:13:24 +00001420 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner00950542001-06-06 20:29:01 +00001421 }
1422 }
Christopher Lamb43c7f372007-04-22 19:24:39 +00001423
1424 // Print post operand alignment for load/store
1425 if (isa<LoadInst>(I) && cast<LoadInst>(I).getAlignment()) {
1426 Out << ", align " << cast<LoadInst>(I).getAlignment();
1427 } else if (isa<StoreInst>(I) && cast<StoreInst>(I).getAlignment()) {
1428 Out << ", align " << cast<StoreInst>(I).getAlignment();
1429 }
Chris Lattner00950542001-06-06 20:29:01 +00001430
Chris Lattnere02fa852001-10-13 06:42:36 +00001431 printInfoComment(I);
Misha Brukman0313e0b2004-06-21 21:53:56 +00001432 Out << "\n";
Chris Lattner00950542001-06-06 20:29:01 +00001433}
1434
1435
1436//===----------------------------------------------------------------------===//
1437// External Interface declarations
1438//===----------------------------------------------------------------------===//
1439
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001440void Module::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001441 SlotMachine SlotTable(this);
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001442 AssemblyWriter W(o, SlotTable, this, AAW);
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001443 W.write(this);
Chris Lattner00950542001-06-06 20:29:01 +00001444}
1445
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001446void GlobalVariable::print(std::ostream &o) const {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001447 SlotMachine SlotTable(getParent());
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001448 AssemblyWriter W(o, SlotTable, getParent(), 0);
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001449 W.write(this);
Chris Lattnerb0e45232001-09-10 20:08:19 +00001450}
1451
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001452void GlobalAlias::print(std::ostream &o) const {
1453 SlotMachine SlotTable(getParent());
1454 AssemblyWriter W(o, SlotTable, getParent(), 0);
1455 W.write(this);
1456}
1457
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001458void Function::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001459 SlotMachine SlotTable(getParent());
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001460 AssemblyWriter W(o, SlotTable, getParent(), AAW);
Chris Lattner00950542001-06-06 20:29:01 +00001461
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001462 W.write(this);
Chris Lattner00950542001-06-06 20:29:01 +00001463}
1464
Chris Lattnercc041ba2006-01-24 04:13:11 +00001465void InlineAsm::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Chris Lattner2fcfdb72006-12-06 06:24:27 +00001466 WriteAsOperand(o, this, true, 0);
Chris Lattnercc041ba2006-01-24 04:13:11 +00001467}
1468
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001469void BasicBlock::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001470 SlotMachine SlotTable(getParent());
Misha Brukmanfd939082005-04-21 23:48:37 +00001471 AssemblyWriter W(o, SlotTable,
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001472 getParent() ? getParent()->getParent() : 0, AAW);
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001473 W.write(this);
Chris Lattner00950542001-06-06 20:29:01 +00001474}
1475
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001476void Instruction::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001477 const Function *F = getParent() ? getParent()->getParent() : 0;
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001478 SlotMachine SlotTable(F);
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001479 AssemblyWriter W(o, SlotTable, F ? F->getParent() : 0, AAW);
Chris Lattner00950542001-06-06 20:29:01 +00001480
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001481 W.write(this);
Chris Lattner00950542001-06-06 20:29:01 +00001482}
Chris Lattnerda1fbcc2001-11-07 04:21:57 +00001483
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001484void Constant::print(std::ostream &o) const {
1485 if (this == 0) { o << "<null> constant value\n"; return; }
Chris Lattner3bc06b32002-09-10 15:53:49 +00001486
Misha Brukman40c732c2004-06-04 21:11:51 +00001487 o << ' ' << getType()->getDescription() << ' ';
Evan Chenga4ffcc22006-03-01 22:17:00 +00001488
1489 std::map<const Type *, std::string> TypeTable;
Chris Lattner2fcfdb72006-12-06 06:24:27 +00001490 WriteConstantInt(o, this, TypeTable, 0);
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001491}
1492
Misha Brukmanfd939082005-04-21 23:48:37 +00001493void Type::print(std::ostream &o) const {
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001494 if (this == 0)
1495 o << "<null Type>";
1496 else
1497 o << getDescription();
1498}
1499
Chris Lattner73e21422002-04-09 19:48:49 +00001500void Argument::print(std::ostream &o) const {
Chris Lattner2fcfdb72006-12-06 06:24:27 +00001501 WriteAsOperand(o, this, true, getParent() ? getParent()->getParent() : 0);
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001502}
1503
Reid Spencerfa452c02004-05-25 18:14:38 +00001504// Value::dump - allow easy printing of Values from the debugger.
1505// Located here because so much of the needed functionality is here.
Bill Wendling832171c2006-12-07 20:04:42 +00001506void Value::dump() const { print(*cerr.stream()); cerr << '\n'; }
Reid Spencerfa452c02004-05-25 18:14:38 +00001507
1508// Type::dump - allow easy printing of Values from the debugger.
1509// Located here because so much of the needed functionality is here.
Bill Wendling832171c2006-12-07 20:04:42 +00001510void Type::dump() const { print(*cerr.stream()); cerr << '\n'; }
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001511
Reid Spencer4f859aa2007-04-22 05:46:44 +00001512void
1513ParamAttrsList::dump() const {
1514 cerr << "PAL[ ";
1515 for (unsigned i = 0; i < attrs.size(); ++i) {
1516 uint16_t index = getParamIndex(i);
1517 uint16_t attrs = getParamAttrs(index);
1518 cerr << "{" << index << "," << attrs << "} ";
1519 }
1520 cerr << "]\n";
1521}
1522
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001523//===----------------------------------------------------------------------===//
Chris Lattner82c4bc72006-12-06 06:40:49 +00001524// SlotMachine Implementation
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001525//===----------------------------------------------------------------------===//
1526
1527#if 0
Bill Wendlinge8156192006-12-07 01:30:32 +00001528#define SC_DEBUG(X) cerr << X
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001529#else
1530#define SC_DEBUG(X)
1531#endif
1532
1533// Module level constructor. Causes the contents of the Module (sans functions)
1534// to be added to the slot table.
Misha Brukmanfd939082005-04-21 23:48:37 +00001535SlotMachine::SlotMachine(const Module *M)
Reid Spencerb03de0c2004-05-26 21:56:09 +00001536 : TheModule(M) ///< Saved for lazy initialization.
1537 , TheFunction(0)
Reid Spencer28531c72004-08-16 07:46:33 +00001538 , FunctionProcessed(false)
Reid Spencer590b3c52007-03-19 18:32:53 +00001539 , mMap(), mNext(0), fMap(), fNext(0)
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001540{
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001541}
1542
1543// Function level constructor. Causes the contents of the Module and the one
1544// function provided to be added to the slot table.
Chris Lattnerc96ce892006-12-06 05:12:21 +00001545SlotMachine::SlotMachine(const Function *F)
1546 : TheModule(F ? F->getParent() : 0) ///< Saved for lazy initialization
Reid Spencerb03de0c2004-05-26 21:56:09 +00001547 , TheFunction(F) ///< Saved for lazy initialization
Reid Spencer28531c72004-08-16 07:46:33 +00001548 , FunctionProcessed(false)
Reid Spencer590b3c52007-03-19 18:32:53 +00001549 , mMap(), mNext(0), fMap(), fNext(0)
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001550{
Reid Spencerb03de0c2004-05-26 21:56:09 +00001551}
1552
Chris Lattner22379bc2007-01-11 03:54:27 +00001553inline void SlotMachine::initialize() {
Chris Lattnerc96ce892006-12-06 05:12:21 +00001554 if (TheModule) {
Misha Brukmanfd939082005-04-21 23:48:37 +00001555 processModule();
Reid Spencerb03de0c2004-05-26 21:56:09 +00001556 TheModule = 0; ///< Prevent re-processing next time we're called.
1557 }
Chris Lattnerde891a62006-12-06 05:27:40 +00001558 if (TheFunction && !FunctionProcessed)
Misha Brukmanfd939082005-04-21 23:48:37 +00001559 processFunction();
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001560}
1561
1562// Iterate through all the global variables, functions, and global
Misha Brukmanfd939082005-04-21 23:48:37 +00001563// variable initializers and create slots for them.
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001564void SlotMachine::processModule() {
1565 SC_DEBUG("begin processModule!\n");
1566
Chris Lattnera6b1ffc2006-12-06 06:15:43 +00001567 // Add all of the unnamed global variables to the value table.
Chris Lattnerd6d826c2006-12-06 04:41:52 +00001568 for (Module::const_global_iterator I = TheModule->global_begin(),
1569 E = TheModule->global_end(); I != E; ++I)
Chris Lattnera6b1ffc2006-12-06 06:15:43 +00001570 if (!I->hasName())
Chris Lattner9446bbe2007-01-09 07:55:49 +00001571 CreateModuleSlot(I);
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001572
Chris Lattnera6b1ffc2006-12-06 06:15:43 +00001573 // Add all the unnamed functions to the table.
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001574 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
1575 I != E; ++I)
Chris Lattnera6b1ffc2006-12-06 06:15:43 +00001576 if (!I->hasName())
Chris Lattner9446bbe2007-01-09 07:55:49 +00001577 CreateModuleSlot(I);
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001578
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001579 SC_DEBUG("end processModule!\n");
1580}
1581
1582
Reid Spencerb03de0c2004-05-26 21:56:09 +00001583// Process the arguments, basic blocks, and instructions of a function.
1584void SlotMachine::processFunction() {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001585 SC_DEBUG("begin processFunction!\n");
Reid Spencer590b3c52007-03-19 18:32:53 +00001586 fNext = 0;
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001587
Chris Lattnera6b1ffc2006-12-06 06:15:43 +00001588 // Add all the function arguments with no names.
Misha Brukmanfd939082005-04-21 23:48:37 +00001589 for(Function::const_arg_iterator AI = TheFunction->arg_begin(),
Chris Lattnere4d5c442005-03-15 04:54:21 +00001590 AE = TheFunction->arg_end(); AI != AE; ++AI)
Chris Lattnera6b1ffc2006-12-06 06:15:43 +00001591 if (!AI->hasName())
Chris Lattner9446bbe2007-01-09 07:55:49 +00001592 CreateFunctionSlot(AI);
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001593
1594 SC_DEBUG("Inserting Instructions:\n");
1595
Chris Lattnera6b1ffc2006-12-06 06:15:43 +00001596 // Add all of the basic blocks and instructions with no names.
Misha Brukmanfd939082005-04-21 23:48:37 +00001597 for (Function::const_iterator BB = TheFunction->begin(),
Reid Spencerb03de0c2004-05-26 21:56:09 +00001598 E = TheFunction->end(); BB != E; ++BB) {
Chris Lattnera6b1ffc2006-12-06 06:15:43 +00001599 if (!BB->hasName())
Chris Lattner9446bbe2007-01-09 07:55:49 +00001600 CreateFunctionSlot(BB);
Chris Lattnera6b1ffc2006-12-06 06:15:43 +00001601 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
1602 if (I->getType() != Type::VoidTy && !I->hasName())
Chris Lattner9446bbe2007-01-09 07:55:49 +00001603 CreateFunctionSlot(I);
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001604 }
1605
Reid Spencer28531c72004-08-16 07:46:33 +00001606 FunctionProcessed = true;
1607
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001608 SC_DEBUG("end processFunction!\n");
1609}
1610
Chris Lattnerde891a62006-12-06 05:27:40 +00001611/// Clean up after incorporating a function. This is the only way to get out of
Chris Lattner22379bc2007-01-11 03:54:27 +00001612/// the function incorporation state that affects get*Slot/Create*Slot. Function
Chris Lattner3a4621c2007-01-09 07:46:21 +00001613/// incorporation state is indicated by TheFunction != 0.
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001614void SlotMachine::purgeFunction() {
1615 SC_DEBUG("begin purgeFunction!\n");
1616 fMap.clear(); // Simply discard the function level map
Reid Spencerb03de0c2004-05-26 21:56:09 +00001617 TheFunction = 0;
Reid Spencer28531c72004-08-16 07:46:33 +00001618 FunctionProcessed = false;
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001619 SC_DEBUG("end purgeFunction!\n");
1620}
1621
Chris Lattner22379bc2007-01-11 03:54:27 +00001622/// getGlobalSlot - Get the slot number of a global value.
1623int SlotMachine::getGlobalSlot(const GlobalValue *V) {
1624 // Check for uninitialized state and do lazy initialization.
1625 initialize();
1626
1627 // Find the type plane in the module map
Reid Spencer590b3c52007-03-19 18:32:53 +00001628 ValueMap::const_iterator MI = mMap.find(V);
Chris Lattner22379bc2007-01-11 03:54:27 +00001629 if (MI == mMap.end()) return -1;
Reid Spencer590b3c52007-03-19 18:32:53 +00001630
1631 return MI->second;
Chris Lattner22379bc2007-01-11 03:54:27 +00001632}
Reid Spencerb03de0c2004-05-26 21:56:09 +00001633
Chris Lattner22379bc2007-01-11 03:54:27 +00001634
1635/// getLocalSlot - Get the slot number for a value that is local to a function.
1636int SlotMachine::getLocalSlot(const Value *V) {
1637 assert(!isa<Constant>(V) && "Can't get a constant or global slot with this!");
1638
1639 // Check for uninitialized state and do lazy initialization.
1640 initialize();
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001641
Reid Spencer590b3c52007-03-19 18:32:53 +00001642 ValueMap::const_iterator FI = fMap.find(V);
Chris Lattner22379bc2007-01-11 03:54:27 +00001643 if (FI == fMap.end()) return -1;
1644
Reid Spencer590b3c52007-03-19 18:32:53 +00001645 return FI->second;
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001646}
1647
Reid Spencer0e25e1c2004-07-04 11:50:43 +00001648
Chris Lattner9446bbe2007-01-09 07:55:49 +00001649/// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
1650void SlotMachine::CreateModuleSlot(const GlobalValue *V) {
Chris Lattnercf8790a2007-01-09 08:04:59 +00001651 assert(V && "Can't insert a null Value into SlotMachine!");
Reid Spencer590b3c52007-03-19 18:32:53 +00001652 assert(V->getType() != Type::VoidTy && "Doesn't need a slot!");
1653 assert(!V->hasName() && "Doesn't need a slot!");
Chris Lattnercf8790a2007-01-09 08:04:59 +00001654
Reid Spencer590b3c52007-03-19 18:32:53 +00001655 unsigned DestSlot = mNext++;
1656 mMap[V] = DestSlot;
Chris Lattnercf8790a2007-01-09 08:04:59 +00001657
Reid Spencer590b3c52007-03-19 18:32:53 +00001658 SC_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
Chris Lattnercf8790a2007-01-09 08:04:59 +00001659 DestSlot << " [");
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001660 // G = Global, F = Function, A = Alias, o = other
1661 SC_DEBUG((isa<GlobalVariable>(V) ? 'G' :
1662 (isa<Function> ? 'F' :
1663 (isa<GlobalAlias> ? 'A' : 'o'))) << "]\n");
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001664}
1665
1666
Chris Lattner9446bbe2007-01-09 07:55:49 +00001667/// CreateSlot - Create a new slot for the specified value if it has no name.
1668void SlotMachine::CreateFunctionSlot(const Value *V) {
1669 const Type *VTy = V->getType();
1670 assert(VTy != Type::VoidTy && !V->hasName() && "Doesn't need a slot!");
Chris Lattnercf8790a2007-01-09 08:04:59 +00001671
Reid Spencer590b3c52007-03-19 18:32:53 +00001672 unsigned DestSlot = fNext++;
1673 fMap[V] = DestSlot;
Chris Lattnercf8790a2007-01-09 08:04:59 +00001674
Chris Lattner4932a5a2006-12-06 05:55:41 +00001675 // G = Global, F = Function, o = other
Chris Lattnercf8790a2007-01-09 08:04:59 +00001676 SC_DEBUG(" Inserting value [" << VTy << "] = " << V << " slot=" <<
1677 DestSlot << " [o]\n");
1678}