blob: 529dd1ac8b2d501a4824caab84ac0d056b6ace2e [file] [log] [blame]
Chris Lattnerf7e79482002-04-07 22:31:46 +00001//===-- AsmWriter.cpp - Printing LLVM as an assembly file -----------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
10// This library implements the functionality defined in llvm/Assembly/Writer.h
11//
Chris Lattner189088e2002-04-12 18:21:53 +000012// Note that these routines must be extremely tolerant of various errors in the
Chris Lattnerf70da102003-05-08 02:44:12 +000013// LLVM code, because it can be used for debugging transformations.
Chris Lattner189088e2002-04-12 18:21:53 +000014//
Chris Lattner2f7c9632001-06-06 20:29:01 +000015//===----------------------------------------------------------------------===//
16
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +000017#include "llvm/Assembly/Writer.h"
Chris Lattner7f8845a2002-07-23 18:07:49 +000018#include "llvm/Assembly/PrintModulePass.h"
Chris Lattner8339f7d2003-10-30 23:41:03 +000019#include "llvm/Assembly/AsmAnnotationWriter.h"
Chris Lattnerf7b6d312005-05-06 20:26:43 +000020#include "llvm/CallingConv.h"
Chris Lattnerc70b3f62004-01-20 19:50:34 +000021#include "llvm/Constants.h"
Chris Lattner913d18f2002-04-29 18:46:50 +000022#include "llvm/DerivedTypes.h"
Chris Lattner8bbcda22006-01-25 18:57:27 +000023#include "llvm/InlineAsm.h"
Vikram S. Adveb952b542002-07-14 23:14:45 +000024#include "llvm/Instruction.h"
Misha Brukman2d3fa9e2004-07-29 16:53:53 +000025#include "llvm/Instructions.h"
Chris Lattnerc70b3f62004-01-20 19:50:34 +000026#include "llvm/Module.h"
Chris Lattnerfee714f2001-09-07 16:36:04 +000027#include "llvm/SymbolTable.h"
Reid Spencer32af9e82007-01-06 07:24:44 +000028#include "llvm/TypeSymbolTable.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000029#include "llvm/ADT/StringExtras.h"
30#include "llvm/ADT/STLExtras.h"
Bill Wendlingdfc91892006-11-28 02:09:03 +000031#include "llvm/Support/CFG.h"
Jim Laskeyb74c6662005-08-17 19:34:49 +000032#include "llvm/Support/MathExtras.h"
Bill Wendlingdfc91892006-11-28 02:09:03 +000033#include "llvm/Support/Streams.h"
Chris Lattnerfee714f2001-09-07 16:36:04 +000034#include <algorithm>
Chris Lattner189d19f2003-11-21 20:23:48 +000035using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000036
Chris Lattner60a7dd12004-07-15 02:51:31 +000037namespace llvm {
Reid Spencer16f2f7f2004-05-26 07:18:52 +000038
Reid Spencer294715b2005-05-15 16:13:11 +000039// Make virtual table appear in this compilation unit.
40AssemblyAnnotationWriter::~AssemblyAnnotationWriter() {}
41
Reid Spencer16f2f7f2004-05-26 07:18:52 +000042/// This class provides computation of slot numbers for LLVM Assembly writing.
43/// @brief LLVM Assembly Writing Slot Computation.
44class SlotMachine {
45
46/// @name Types
47/// @{
48public:
49
50 /// @brief A mapping of Values to slot numbers
51 typedef std::map<const Value*, unsigned> ValueMap;
52
53 /// @brief A plane with next slot number and ValueMap
Misha Brukmanb1c93172005-04-21 23:48:37 +000054 struct ValuePlane {
Reid Spencer16f2f7f2004-05-26 07:18:52 +000055 unsigned next_slot; ///< The next slot number to use
56 ValueMap map; ///< The map of Value* -> unsigned
Reid Spencer58d30f22004-07-04 11:50:43 +000057 ValuePlane() { next_slot = 0; } ///< Make sure we start at 0
58 };
59
Reid Spencer16f2f7f2004-05-26 07:18:52 +000060 /// @brief The map of planes by Type
Reid Spencer58d30f22004-07-04 11:50:43 +000061 typedef std::map<const Type*, ValuePlane> TypedPlanes;
Reid Spencer16f2f7f2004-05-26 07:18:52 +000062
63/// @}
64/// @name Constructors
65/// @{
66public:
67 /// @brief Construct from a module
Chris Lattner23e829a2006-12-06 05:12:21 +000068 SlotMachine(const Module *M);
Reid Spencer16f2f7f2004-05-26 07:18:52 +000069
70 /// @brief Construct from a function, starting out in incorp state.
Chris Lattner23e829a2006-12-06 05:12:21 +000071 SlotMachine(const Function *F);
Reid Spencer16f2f7f2004-05-26 07:18:52 +000072
73/// @}
74/// @name Accessors
75/// @{
76public:
77 /// Return the slot number of the specified value in it's type
78 /// plane. Its an error to ask for something not in the SlotMachine.
79 /// Its an error to ask for a Type*
Chris Lattner757ee0b2004-06-09 19:41:19 +000080 int getSlot(const Value *V);
Reid Spencer8beac692004-06-09 15:26:53 +000081
Reid Spencer16f2f7f2004-05-26 07:18:52 +000082/// @}
83/// @name Mutators
84/// @{
85public:
Misha Brukmanb1c93172005-04-21 23:48:37 +000086 /// If you'd like to deal with a function instead of just a module, use
Reid Spencer16f2f7f2004-05-26 07:18:52 +000087 /// this method to get its data into the SlotMachine.
Misha Brukmanb1c93172005-04-21 23:48:37 +000088 void incorporateFunction(const Function *F) {
89 TheFunction = F;
Reid Spencerb0ac8c42004-08-16 07:46:33 +000090 FunctionProcessed = false;
91 }
Reid Spencer16f2f7f2004-05-26 07:18:52 +000092
Misha Brukmanb1c93172005-04-21 23:48:37 +000093 /// After calling incorporateFunction, use this method to remove the
94 /// most recently incorporated function from the SlotMachine. This
Reid Spencer16f2f7f2004-05-26 07:18:52 +000095 /// will reset the state of the machine back to just the module contents.
96 void purgeFunction();
97
98/// @}
99/// @name Implementation Details
100/// @{
101private:
Reid Spencer56010e42004-05-26 21:56:09 +0000102 /// This function does the actual initialization.
103 inline void initialize();
104
Chris Lattner89e774e2007-01-09 07:46:21 +0000105 /// CreateSlot - If the specified Value* doesn't have a name, assign it a slot
106 /// number.
107 void CreateSlot(const Value *V);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000108
Chris Lattner89e774e2007-01-09 07:46:21 +0000109 /// Insert a value into the value table.
110 void insertValue(const Value *V);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000111
112 /// Add all of the module level global variables (and their initializers)
113 /// and function declarations, but not the contents of those functions.
114 void processModule();
115
Reid Spencer56010e42004-05-26 21:56:09 +0000116 /// Add all of the functions arguments, basic blocks, and instructions
117 void processFunction();
118
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000119 SlotMachine(const SlotMachine &); // DO NOT IMPLEMENT
120 void operator=(const SlotMachine &); // DO NOT IMPLEMENT
121
122/// @}
123/// @name Data
124/// @{
125public:
126
127 /// @brief The module for which we are holding slot numbers
Reid Spencer56010e42004-05-26 21:56:09 +0000128 const Module* TheModule;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000129
Reid Spencer56010e42004-05-26 21:56:09 +0000130 /// @brief The function for which we are holding slot numbers
131 const Function* TheFunction;
Reid Spencerb0ac8c42004-08-16 07:46:33 +0000132 bool FunctionProcessed;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000133
134 /// @brief The TypePlanes map for the module level data
135 TypedPlanes mMap;
136
137 /// @brief The TypePlanes map for the function level data
138 TypedPlanes fMap;
139
140/// @}
141
142};
143
Chris Lattner60a7dd12004-07-15 02:51:31 +0000144} // end namespace llvm
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000145
Chris Lattnerc2d3d312006-08-27 22:42:52 +0000146static RegisterPass<PrintModulePass>
Chris Lattnere7134c52006-08-21 17:20:01 +0000147X("printm", "Print module to stderr");
Chris Lattnerc2d3d312006-08-27 22:42:52 +0000148static RegisterPass<PrintFunctionPass>
Chris Lattnere7134c52006-08-21 17:20:01 +0000149Y("print","Print function to stderr");
Chris Lattner7f8845a2002-07-23 18:07:49 +0000150
Misha Brukmanb1c93172005-04-21 23:48:37 +0000151static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
Chris Lattnera9f0a112006-12-06 05:50:41 +0000152 std::map<const Type *, std::string> &TypeTable,
Reid Spencer58d30f22004-07-04 11:50:43 +0000153 SlotMachine *Machine);
154
Chris Lattnerb86620e2001-10-29 16:37:48 +0000155static const Module *getModuleFromVal(const Value *V) {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000156 if (const Argument *MA = dyn_cast<Argument>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +0000157 return MA->getParent() ? MA->getParent()->getParent() : 0;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000158 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +0000159 return BB->getParent() ? BB->getParent()->getParent() : 0;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000160 else if (const Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner57698e22002-03-26 18:01:55 +0000161 const Function *M = I->getParent() ? I->getParent()->getParent() : 0;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000162 return M ? M->getParent() : 0;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000163 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +0000164 return GV->getParent();
Chris Lattnerb86620e2001-10-29 16:37:48 +0000165 return 0;
166}
167
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000168static SlotMachine *createSlotMachine(const Value *V) {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000169 if (const Argument *FA = dyn_cast<Argument>(V)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000170 return new SlotMachine(FA->getParent());
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000171 } else if (const Instruction *I = dyn_cast<Instruction>(V)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000172 return new SlotMachine(I->getParent()->getParent());
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000173 } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000174 return new SlotMachine(BB->getParent());
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000175 } else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)){
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000176 return new SlotMachine(GV->getParent());
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000177 } else if (const Function *Func = dyn_cast<Function>(V)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000178 return new SlotMachine(Func);
Chris Lattner7bfee412001-10-29 16:05:51 +0000179 }
180 return 0;
181}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000182
Chris Lattner1c343042003-08-22 05:40:38 +0000183// getLLVMName - Turn the specified string into an 'LLVM name', which is either
184// prefixed with % (if the string only contains simple characters) or is
185// surrounded with ""'s (if it has special chars in it).
Alkis Evlogimenos991d6ad2004-12-10 05:41:10 +0000186static std::string getLLVMName(const std::string &Name,
187 bool prefixName = true) {
Chris Lattner1c343042003-08-22 05:40:38 +0000188 assert(!Name.empty() && "Cannot get empty name!");
189
190 // First character cannot start with a number...
191 if (Name[0] >= '0' && Name[0] <= '9')
192 return "\"" + Name + "\"";
193
194 // Scan to see if we have any characters that are not on the "white list"
195 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
196 char C = Name[i];
197 assert(C != '"' && "Illegal character in LLVM value name!");
198 if ((C < 'a' || C > 'z') && (C < 'A' || C > 'Z') && (C < '0' || C > '9') &&
199 C != '-' && C != '.' && C != '_')
200 return "\"" + Name + "\"";
201 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000202
Chris Lattner1c343042003-08-22 05:40:38 +0000203 // If we get here, then the identifier is legal to use as a "VarID".
Alkis Evlogimenos991d6ad2004-12-10 05:41:10 +0000204 if (prefixName)
205 return "%"+Name;
206 else
207 return Name;
Chris Lattner1c343042003-08-22 05:40:38 +0000208}
209
Chris Lattnerb86620e2001-10-29 16:37:48 +0000210
Misha Brukmanc566ca362004-03-02 00:22:19 +0000211/// fillTypeNameTable - If the module has a symbol table, take all global types
212/// and stuff their names into the TypeNames map.
213///
Chris Lattnerb86620e2001-10-29 16:37:48 +0000214static void fillTypeNameTable(const Module *M,
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000215 std::map<const Type *, std::string> &TypeNames) {
Chris Lattner98cf1f52002-11-20 18:36:02 +0000216 if (!M) return;
Reid Spencer32af9e82007-01-06 07:24:44 +0000217 const TypeSymbolTable &ST = M->getTypeSymbolTable();
218 TypeSymbolTable::const_iterator TI = ST.begin();
219 for (; TI != ST.end(); ++TI) {
Reid Spencere7e96712004-05-25 08:53:40 +0000220 // As a heuristic, don't insert pointer to primitive types, because
221 // they are used too often to have a single useful name.
222 //
223 const Type *Ty = cast<Type>(TI->second);
224 if (!isa<PointerType>(Ty) ||
Reid Spencer56010e42004-05-26 21:56:09 +0000225 !cast<PointerType>(Ty)->getElementType()->isPrimitiveType() ||
226 isa<OpaqueType>(cast<PointerType>(Ty)->getElementType()))
Reid Spencere7e96712004-05-25 08:53:40 +0000227 TypeNames.insert(std::make_pair(Ty, getLLVMName(TI->first)));
Chris Lattnerb86620e2001-10-29 16:37:48 +0000228 }
229}
230
231
232
Misha Brukmanb1c93172005-04-21 23:48:37 +0000233static void calcTypeName(const Type *Ty,
John Criswellcd116ba2004-06-01 14:54:08 +0000234 std::vector<const Type *> &TypeStack,
235 std::map<const Type *, std::string> &TypeNames,
236 std::string & Result){
237 if (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty)) {
238 Result += Ty->getDescription(); // Base case
239 return;
240 }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000241
242 // Check to see if the type is named.
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000243 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
John Criswellcd116ba2004-06-01 14:54:08 +0000244 if (I != TypeNames.end()) {
245 Result += I->second;
246 return;
247 }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000248
John Criswellcd116ba2004-06-01 14:54:08 +0000249 if (isa<OpaqueType>(Ty)) {
250 Result += "opaque";
251 return;
252 }
Chris Lattnerf14ead92003-10-30 00:22:33 +0000253
Chris Lattnerb86620e2001-10-29 16:37:48 +0000254 // Check to see if the Type is already on the stack...
255 unsigned Slot = 0, CurSize = TypeStack.size();
256 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
257
Misha Brukmanb1c93172005-04-21 23:48:37 +0000258 // This is another base case for the recursion. In this case, we know
Chris Lattnerb86620e2001-10-29 16:37:48 +0000259 // that we have looped back to a type that we have previously visited.
260 // Generate the appropriate upreference to handle this.
John Criswellcd116ba2004-06-01 14:54:08 +0000261 if (Slot < CurSize) {
262 Result += "\\" + utostr(CurSize-Slot); // Here's the upreference
263 return;
264 }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000265
266 TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
Misha Brukmanb1c93172005-04-21 23:48:37 +0000267
Chris Lattner6b727592004-06-17 18:19:28 +0000268 switch (Ty->getTypeID()) {
Chris Lattner91db5822002-03-29 03:44:36 +0000269 case Type::FunctionTyID: {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000270 const FunctionType *FTy = cast<FunctionType>(Ty);
John Criswellcd116ba2004-06-01 14:54:08 +0000271 calcTypeName(FTy->getReturnType(), TypeStack, TypeNames, Result);
272 Result += " (";
Reid Spencer8c4914c2006-12-31 05:24:50 +0000273 unsigned Idx = 1;
Chris Lattnerfa829be2004-02-09 04:14:01 +0000274 for (FunctionType::param_iterator I = FTy->param_begin(),
275 E = FTy->param_end(); I != E; ++I) {
276 if (I != FTy->param_begin())
Chris Lattnerb86620e2001-10-29 16:37:48 +0000277 Result += ", ";
John Criswellcd116ba2004-06-01 14:54:08 +0000278 calcTypeName(*I, TypeStack, TypeNames, Result);
Reid Spencer8c4914c2006-12-31 05:24:50 +0000279 if (FTy->getParamAttrs(Idx)) {
280 Result += + " ";
281 Result += FunctionType::getParamAttrsText(FTy->getParamAttrs(Idx));
282 }
283 Idx++;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000284 }
Chris Lattnerd816b532002-04-13 20:53:41 +0000285 if (FTy->isVarArg()) {
Chris Lattnerfa829be2004-02-09 04:14:01 +0000286 if (FTy->getNumParams()) Result += ", ";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000287 Result += "...";
288 }
289 Result += ")";
Reid Spencer136a91c2007-01-05 17:06:19 +0000290 if (FTy->getParamAttrs(0)) {
291 Result += " ";
292 Result += FunctionType::getParamAttrsText(FTy->getParamAttrs(0));
293 }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000294 break;
295 }
296 case Type::StructTyID: {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000297 const StructType *STy = cast<StructType>(Ty);
Andrew Lenharthdcb3c972006-12-08 18:06:16 +0000298 if (STy->isPacked())
299 Result += '<';
John Criswellcd116ba2004-06-01 14:54:08 +0000300 Result += "{ ";
Chris Lattnerac6db752004-02-09 04:37:31 +0000301 for (StructType::element_iterator I = STy->element_begin(),
302 E = STy->element_end(); I != E; ++I) {
303 if (I != STy->element_begin())
Chris Lattnerb86620e2001-10-29 16:37:48 +0000304 Result += ", ";
John Criswellcd116ba2004-06-01 14:54:08 +0000305 calcTypeName(*I, TypeStack, TypeNames, Result);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000306 }
307 Result += " }";
Andrew Lenharthdcb3c972006-12-08 18:06:16 +0000308 if (STy->isPacked())
309 Result += '>';
Chris Lattnerb86620e2001-10-29 16:37:48 +0000310 break;
311 }
312 case Type::PointerTyID:
Misha Brukmanb1c93172005-04-21 23:48:37 +0000313 calcTypeName(cast<PointerType>(Ty)->getElementType(),
John Criswellcd116ba2004-06-01 14:54:08 +0000314 TypeStack, TypeNames, Result);
315 Result += "*";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000316 break;
317 case Type::ArrayTyID: {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000318 const ArrayType *ATy = cast<ArrayType>(Ty);
John Criswellcd116ba2004-06-01 14:54:08 +0000319 Result += "[" + utostr(ATy->getNumElements()) + " x ";
320 calcTypeName(ATy->getElementType(), TypeStack, TypeNames, Result);
321 Result += "]";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000322 break;
323 }
Brian Gaeke02209042004-08-20 06:00:58 +0000324 case Type::PackedTyID: {
325 const PackedType *PTy = cast<PackedType>(Ty);
326 Result += "<" + utostr(PTy->getNumElements()) + " x ";
327 calcTypeName(PTy->getElementType(), TypeStack, TypeNames, Result);
328 Result += ">";
329 break;
330 }
Chris Lattner15285ab2003-05-14 17:50:47 +0000331 case Type::OpaqueTyID:
John Criswellcd116ba2004-06-01 14:54:08 +0000332 Result += "opaque";
Chris Lattner15285ab2003-05-14 17:50:47 +0000333 break;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000334 default:
John Criswellcd116ba2004-06-01 14:54:08 +0000335 Result += "<unrecognized-type>";
Chris Lattnerfc9f1c92006-12-06 06:40:49 +0000336 break;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000337 }
338
339 TypeStack.pop_back(); // Remove self from stack...
Chris Lattnerb86620e2001-10-29 16:37:48 +0000340}
341
342
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000343/// printTypeInt - The internal guts of printing out a type that has a
344/// potentially named portion.
345///
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000346static std::ostream &printTypeInt(std::ostream &Out, const Type *Ty,
347 std::map<const Type *, std::string> &TypeNames) {
Chris Lattnerb86620e2001-10-29 16:37:48 +0000348 // Primitive types always print out their description, regardless of whether
349 // they have been named or not.
350 //
Chris Lattner92d60532003-10-30 00:12:51 +0000351 if (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty))
352 return Out << Ty->getDescription();
Chris Lattnerb86620e2001-10-29 16:37:48 +0000353
354 // Check to see if the type is named.
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000355 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000356 if (I != TypeNames.end()) return Out << I->second;
357
358 // Otherwise we have a type that has not been named but is a derived type.
359 // Carefully recurse the type hierarchy to print out any contained symbolic
360 // names.
361 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000362 std::vector<const Type *> TypeStack;
John Criswellcd116ba2004-06-01 14:54:08 +0000363 std::string TypeName;
364 calcTypeName(Ty, TypeStack, TypeNames, TypeName);
Chris Lattner7f74a562002-01-20 22:54:45 +0000365 TypeNames.insert(std::make_pair(Ty, TypeName));//Cache type name for later use
John Criswellcd116ba2004-06-01 14:54:08 +0000366 return (Out << TypeName);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000367}
368
Chris Lattner34b95182001-10-31 04:33:19 +0000369
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000370/// WriteTypeSymbolic - This attempts to write the specified type as a symbolic
371/// type, iff there is an entry in the modules symbol table for the specified
372/// type or one of it's component types. This is slower than a simple x << Type
373///
Chris Lattner189d19f2003-11-21 20:23:48 +0000374std::ostream &llvm::WriteTypeSymbolic(std::ostream &Out, const Type *Ty,
375 const Module *M) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000376 Out << ' ';
Chris Lattnerb86620e2001-10-29 16:37:48 +0000377
Chris Lattnerfc9f1c92006-12-06 06:40:49 +0000378 // If they want us to print out a type, but there is no context, we can't
379 // print it symbolically.
380 if (!M)
Chris Lattner72f866e2001-10-29 16:40:32 +0000381 return Out << Ty->getDescription();
Chris Lattnerfc9f1c92006-12-06 06:40:49 +0000382
383 std::map<const Type *, std::string> TypeNames;
384 fillTypeNameTable(M, TypeNames);
385 return printTypeInt(Out, Ty, TypeNames);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000386}
387
Chris Lattner6ed87bd2006-01-23 23:03:36 +0000388// PrintEscapedString - Print each character of the specified string, escaping
389// it if it is not printable or if it is an escape char.
390static void PrintEscapedString(const std::string &Str, std::ostream &Out) {
391 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
392 unsigned char C = Str[i];
393 if (isprint(C) && C != '"' && C != '\\') {
394 Out << C;
395 } else {
396 Out << '\\'
397 << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'))
398 << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
399 }
400 }
401}
402
Chris Lattnerfc9f1c92006-12-06 06:40:49 +0000403static const char *getPredicateText(unsigned predicate) {
Reid Spencer812a1be2006-12-04 05:19:18 +0000404 const char * pred = "unknown";
405 switch (predicate) {
406 case FCmpInst::FCMP_FALSE: pred = "false"; break;
407 case FCmpInst::FCMP_OEQ: pred = "oeq"; break;
408 case FCmpInst::FCMP_OGT: pred = "ogt"; break;
409 case FCmpInst::FCMP_OGE: pred = "oge"; break;
410 case FCmpInst::FCMP_OLT: pred = "olt"; break;
411 case FCmpInst::FCMP_OLE: pred = "ole"; break;
412 case FCmpInst::FCMP_ONE: pred = "one"; break;
413 case FCmpInst::FCMP_ORD: pred = "ord"; break;
414 case FCmpInst::FCMP_UNO: pred = "uno"; break;
415 case FCmpInst::FCMP_UEQ: pred = "ueq"; break;
416 case FCmpInst::FCMP_UGT: pred = "ugt"; break;
417 case FCmpInst::FCMP_UGE: pred = "uge"; break;
418 case FCmpInst::FCMP_ULT: pred = "ult"; break;
419 case FCmpInst::FCMP_ULE: pred = "ule"; break;
420 case FCmpInst::FCMP_UNE: pred = "une"; break;
421 case FCmpInst::FCMP_TRUE: pred = "true"; break;
422 case ICmpInst::ICMP_EQ: pred = "eq"; break;
423 case ICmpInst::ICMP_NE: pred = "ne"; break;
424 case ICmpInst::ICMP_SGT: pred = "sgt"; break;
425 case ICmpInst::ICMP_SGE: pred = "sge"; break;
426 case ICmpInst::ICMP_SLT: pred = "slt"; break;
427 case ICmpInst::ICMP_SLE: pred = "sle"; break;
428 case ICmpInst::ICMP_UGT: pred = "ugt"; break;
429 case ICmpInst::ICMP_UGE: pred = "uge"; break;
430 case ICmpInst::ICMP_ULT: pred = "ult"; break;
431 case ICmpInst::ICMP_ULE: pred = "ule"; break;
432 }
433 return pred;
434}
435
Misha Brukmanb1c93172005-04-21 23:48:37 +0000436/// @brief Internal constant writer.
437static void WriteConstantInt(std::ostream &Out, const Constant *CV,
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000438 std::map<const Type *, std::string> &TypeTable,
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000439 SlotMachine *Machine) {
Jim Laskey6be3d8e2006-02-27 10:33:53 +0000440 const int IndentSize = 4;
441 static std::string Indent = "\n";
Chris Lattner1e194682002-04-18 18:53:13 +0000442 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
Chris Lattner94e39cb2006-09-28 22:50:29 +0000443 Out << (CB->getValue() ? "true" : "false");
Reid Spencere0fc4df2006-10-20 07:07:24 +0000444 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Reid Spencer2acb8e12006-12-19 21:16:35 +0000445 Out << CI->getSExtValue();
Chris Lattner1e194682002-04-18 18:53:13 +0000446 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
447 // We would like to output the FP constant value in exponential notation,
448 // but we cannot do this if doing so will lose precision. Check here to
449 // make sure that we only output it in exponential format if we can parse
450 // the value back and get the same value.
451 //
452 std::string StrVal = ftostr(CFP->getValue());
453
454 // Check to make sure that the stringized number is not some string like
455 // "Inf" or NaN, that atof will accept, but the lexer will not. Check that
456 // the string matches the "[-+]?[0-9]" regex.
457 //
458 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
459 ((StrVal[0] == '-' || StrVal[0] == '+') &&
Brian Gaeke87b4f072003-06-17 23:55:35 +0000460 (StrVal[1] >= '0' && StrVal[1] <= '9')))
Chris Lattner1e194682002-04-18 18:53:13 +0000461 // Reparse stringized version!
462 if (atof(StrVal.c_str()) == CFP->getValue()) {
Chris Lattner472cc102005-01-04 01:56:57 +0000463 Out << StrVal;
464 return;
Chris Lattner1e194682002-04-18 18:53:13 +0000465 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000466
Chris Lattner1e194682002-04-18 18:53:13 +0000467 // Otherwise we could not reparse it to exactly the same value, so we must
468 // output the string in hexadecimal format!
Chris Lattner472cc102005-01-04 01:56:57 +0000469 assert(sizeof(double) == sizeof(uint64_t) &&
Chris Lattner1e194682002-04-18 18:53:13 +0000470 "assuming that double is 64 bits!");
Jim Laskeyb74c6662005-08-17 19:34:49 +0000471 Out << "0x" << utohexstr(DoubleToBits(CFP->getValue()));
Chris Lattner1e194682002-04-18 18:53:13 +0000472
Chris Lattner76b2ff42004-02-15 05:55:15 +0000473 } else if (isa<ConstantAggregateZero>(CV)) {
474 Out << "zeroinitializer";
Chris Lattner1e194682002-04-18 18:53:13 +0000475 } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
476 // As a special case, print the array as a string if it is an array of
477 // ubytes or an array of sbytes with positive values.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000478 //
Chris Lattner1e194682002-04-18 18:53:13 +0000479 const Type *ETy = CA->getType()->getElementType();
Chris Lattner6ed87bd2006-01-23 23:03:36 +0000480 if (CA->isString()) {
Chris Lattner1e194682002-04-18 18:53:13 +0000481 Out << "c\"";
Chris Lattner6ed87bd2006-01-23 23:03:36 +0000482 PrintEscapedString(CA->getAsString(), Out);
Chris Lattner1e194682002-04-18 18:53:13 +0000483 Out << "\"";
484
485 } else { // Cannot output in string format...
Misha Brukman21bbdb92004-06-04 21:11:51 +0000486 Out << '[';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000487 if (CA->getNumOperands()) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000488 Out << ' ';
Chris Lattner1e194682002-04-18 18:53:13 +0000489 printTypeInt(Out, ETy, TypeTable);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000490 WriteAsOperandInternal(Out, CA->getOperand(0),
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000491 TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000492 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
493 Out << ", ";
Chris Lattner1e194682002-04-18 18:53:13 +0000494 printTypeInt(Out, ETy, TypeTable);
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000495 WriteAsOperandInternal(Out, CA->getOperand(i), TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000496 }
497 }
498 Out << " ]";
499 }
500 } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Andrew Lenharth0d124b82007-01-08 18:21:30 +0000501 if (CS->getType()->isPacked())
502 Out << '<';
Misha Brukman21bbdb92004-06-04 21:11:51 +0000503 Out << '{';
Jim Laskey3bb78742006-02-25 12:27:03 +0000504 unsigned N = CS->getNumOperands();
505 if (N) {
Jim Laskey6be3d8e2006-02-27 10:33:53 +0000506 if (N > 2) {
507 Indent += std::string(IndentSize, ' ');
508 Out << Indent;
509 } else {
510 Out << ' ';
511 }
Chris Lattnerd84bb632002-04-16 21:36:08 +0000512 printTypeInt(Out, CS->getOperand(0)->getType(), TypeTable);
513
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000514 WriteAsOperandInternal(Out, CS->getOperand(0), TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000515
Jim Laskey3bb78742006-02-25 12:27:03 +0000516 for (unsigned i = 1; i < N; i++) {
Chris Lattnerd84bb632002-04-16 21:36:08 +0000517 Out << ", ";
Jim Laskey6be3d8e2006-02-27 10:33:53 +0000518 if (N > 2) Out << Indent;
Chris Lattnerd84bb632002-04-16 21:36:08 +0000519 printTypeInt(Out, CS->getOperand(i)->getType(), TypeTable);
520
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000521 WriteAsOperandInternal(Out, CS->getOperand(i), TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000522 }
Jim Laskey6be3d8e2006-02-27 10:33:53 +0000523 if (N > 2) Indent.resize(Indent.size() - IndentSize);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000524 }
Jim Laskey3bb78742006-02-25 12:27:03 +0000525
Chris Lattnerd84bb632002-04-16 21:36:08 +0000526 Out << " }";
Andrew Lenharth0d124b82007-01-08 18:21:30 +0000527 if (CS->getType()->isPacked())
528 Out << '>';
Brian Gaeke02209042004-08-20 06:00:58 +0000529 } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(CV)) {
530 const Type *ETy = CP->getType()->getElementType();
Misha Brukmanb1c93172005-04-21 23:48:37 +0000531 assert(CP->getNumOperands() > 0 &&
Brian Gaeke02209042004-08-20 06:00:58 +0000532 "Number of operands for a PackedConst must be > 0");
533 Out << '<';
534 Out << ' ';
535 printTypeInt(Out, ETy, TypeTable);
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000536 WriteAsOperandInternal(Out, CP->getOperand(0), TypeTable, Machine);
Brian Gaeke02209042004-08-20 06:00:58 +0000537 for (unsigned i = 1, e = CP->getNumOperands(); i != e; ++i) {
538 Out << ", ";
539 printTypeInt(Out, ETy, TypeTable);
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000540 WriteAsOperandInternal(Out, CP->getOperand(i), TypeTable, Machine);
Brian Gaeke02209042004-08-20 06:00:58 +0000541 }
542 Out << " >";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000543 } else if (isa<ConstantPointerNull>(CV)) {
544 Out << "null";
545
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000546 } else if (isa<UndefValue>(CV)) {
547 Out << "undef";
548
Chris Lattner3cd8c562002-07-30 18:54:25 +0000549 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Reid Spencer812a1be2006-12-04 05:19:18 +0000550 Out << CE->getOpcodeName();
551 if (CE->isCompare())
552 Out << " " << getPredicateText(CE->getPredicate());
553 Out << " (";
Misha Brukmanb1c93172005-04-21 23:48:37 +0000554
Vikram S. Adveb952b542002-07-14 23:14:45 +0000555 for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
556 printTypeInt(Out, (*OI)->getType(), TypeTable);
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000557 WriteAsOperandInternal(Out, *OI, TypeTable, Machine);
Vikram S. Adveb952b542002-07-14 23:14:45 +0000558 if (OI+1 != CE->op_end())
Chris Lattner3cd8c562002-07-30 18:54:25 +0000559 Out << ", ";
Vikram S. Adveb952b542002-07-14 23:14:45 +0000560 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000561
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000562 if (CE->isCast()) {
Chris Lattner83b396b2002-08-15 19:37:43 +0000563 Out << " to ";
564 printTypeInt(Out, CE->getType(), TypeTable);
565 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000566
Misha Brukman21bbdb92004-06-04 21:11:51 +0000567 Out << ')';
Chris Lattner83b396b2002-08-15 19:37:43 +0000568
Chris Lattnerd84bb632002-04-16 21:36:08 +0000569 } else {
Vikram S. Adveb952b542002-07-14 23:14:45 +0000570 Out << "<placeholder or erroneous Constant>";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000571 }
572}
573
574
Misha Brukmanc566ca362004-03-02 00:22:19 +0000575/// WriteAsOperand - Write the name of the specified value out to the specified
576/// ostream. This can be useful when you just want to print int %reg126, not
577/// the whole instruction that generated it.
578///
Misha Brukmanb1c93172005-04-21 23:48:37 +0000579static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000580 std::map<const Type*, std::string> &TypeTable,
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000581 SlotMachine *Machine) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000582 Out << ' ';
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000583 if (V->hasName())
Chris Lattner1c343042003-08-22 05:40:38 +0000584 Out << getLLVMName(V->getName());
Reid Spenceraccd7c72004-07-17 23:47:01 +0000585 else {
586 const Constant *CV = dyn_cast<Constant>(V);
Chris Lattnera2d810d2006-01-25 22:26:05 +0000587 if (CV && !isa<GlobalValue>(CV)) {
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000588 WriteConstantInt(Out, CV, TypeTable, Machine);
Chris Lattnera2d810d2006-01-25 22:26:05 +0000589 } else if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
590 Out << "asm ";
591 if (IA->hasSideEffects())
592 Out << "sideeffect ";
593 Out << '"';
594 PrintEscapedString(IA->getAsmString(), Out);
595 Out << "\", \"";
596 PrintEscapedString(IA->getConstraintString(), Out);
597 Out << '"';
598 } else {
Chris Lattnerd84bb632002-04-16 21:36:08 +0000599 int Slot;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000600 if (Machine) {
Reid Spencer56010e42004-05-26 21:56:09 +0000601 Slot = Machine->getSlot(V);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000602 } else {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000603 Machine = createSlotMachine(V);
Chris Lattner96749c42006-05-14 18:46:52 +0000604 if (Machine)
Chris Lattner757ee0b2004-06-09 19:41:19 +0000605 Slot = Machine->getSlot(V);
606 else
607 Slot = -1;
Reid Spencer56010e42004-05-26 21:56:09 +0000608 delete Machine;
Chris Lattnerd84bb632002-04-16 21:36:08 +0000609 }
Chris Lattner757ee0b2004-06-09 19:41:19 +0000610 if (Slot != -1)
611 Out << '%' << Slot;
612 else
613 Out << "<badref>";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000614 }
615 }
616}
617
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000618/// WriteAsOperand - Write the name of the specified value out to the specified
619/// ostream. This can be useful when you just want to print int %reg126, not
620/// the whole instruction that generated it.
621///
Chris Lattner189d19f2003-11-21 20:23:48 +0000622std::ostream &llvm::WriteAsOperand(std::ostream &Out, const Value *V,
Chris Lattner0dda8612006-12-06 06:15:43 +0000623 bool PrintType, const Module *Context) {
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000624 std::map<const Type *, std::string> TypeNames;
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000625 if (Context == 0) Context = getModuleFromVal(V);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000626
Chris Lattner98cf1f52002-11-20 18:36:02 +0000627 if (Context)
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000628 fillTypeNameTable(Context, TypeNames);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000629
630 if (PrintType)
631 printTypeInt(Out, V->getType(), TypeNames);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000632
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000633 WriteAsOperandInternal(Out, V, TypeNames, 0);
Chris Lattner5e5abe32001-07-20 19:15:21 +0000634 return Out;
635}
636
Reid Spencer58d30f22004-07-04 11:50:43 +0000637
Chris Lattner189d19f2003-11-21 20:23:48 +0000638namespace llvm {
Chris Lattner2e9fee42001-07-12 23:35:26 +0000639
Chris Lattnerfee714f2001-09-07 16:36:04 +0000640class AssemblyWriter {
Misha Brukmana6619a92004-06-21 21:53:56 +0000641 std::ostream &Out;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000642 SlotMachine &Machine;
Chris Lattner7bfee412001-10-29 16:05:51 +0000643 const Module *TheModule;
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000644 std::map<const Type *, std::string> TypeNames;
Chris Lattner8339f7d2003-10-30 23:41:03 +0000645 AssemblyAnnotationWriter *AnnotationWriter;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000646public:
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000647 inline AssemblyWriter(std::ostream &o, SlotMachine &Mac, const Module *M,
Chris Lattner8339f7d2003-10-30 23:41:03 +0000648 AssemblyAnnotationWriter *AAW)
Misha Brukmana6619a92004-06-21 21:53:56 +0000649 : Out(o), Machine(Mac), TheModule(M), AnnotationWriter(AAW) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000650
651 // If the module has a symbol table, take all global types and stuff their
652 // names into the TypeNames map.
653 //
Chris Lattnerb86620e2001-10-29 16:37:48 +0000654 fillTypeNameTable(M, TypeNames);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000655 }
656
Chris Lattner7bfee412001-10-29 16:05:51 +0000657 inline void write(const Module *M) { printModule(M); }
658 inline void write(const GlobalVariable *G) { printGlobal(G); }
Chris Lattner57698e22002-03-26 18:01:55 +0000659 inline void write(const Function *F) { printFunction(F); }
Chris Lattner7bfee412001-10-29 16:05:51 +0000660 inline void write(const BasicBlock *BB) { printBasicBlock(BB); }
Chris Lattner113f4f42002-06-25 16:13:24 +0000661 inline void write(const Instruction *I) { printInstruction(*I); }
Chris Lattner3462ae32001-12-03 22:26:30 +0000662 inline void write(const Constant *CPV) { printConstant(CPV); }
Chris Lattner7db79582001-11-07 04:21:57 +0000663 inline void write(const Type *Ty) { printType(Ty); }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000664
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000665 void writeOperand(const Value *Op, bool PrintType);
Chris Lattner1e194682002-04-18 18:53:13 +0000666
Misha Brukman4685e262004-04-28 15:31:21 +0000667 const Module* getModule() { return TheModule; }
668
Misha Brukmand92f54a2004-11-15 19:30:05 +0000669private:
Chris Lattner7bfee412001-10-29 16:05:51 +0000670 void printModule(const Module *M);
Reid Spencer32af9e82007-01-06 07:24:44 +0000671 void printTypeSymbolTable(const TypeSymbolTable &ST);
672 void printValueSymbolTable(const SymbolTable &ST);
Chris Lattner3462ae32001-12-03 22:26:30 +0000673 void printConstant(const Constant *CPV);
Chris Lattner7bfee412001-10-29 16:05:51 +0000674 void printGlobal(const GlobalVariable *GV);
Chris Lattner57698e22002-03-26 18:01:55 +0000675 void printFunction(const Function *F);
Reid Spencer8c4914c2006-12-31 05:24:50 +0000676 void printArgument(const Argument *FA, FunctionType::ParameterAttributes A);
Chris Lattner7bfee412001-10-29 16:05:51 +0000677 void printBasicBlock(const BasicBlock *BB);
Chris Lattner113f4f42002-06-25 16:13:24 +0000678 void printInstruction(const Instruction &I);
Chris Lattnerd816b532002-04-13 20:53:41 +0000679
680 // printType - Go to extreme measures to attempt to print out a short,
681 // symbolic version of a type name.
682 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000683 std::ostream &printType(const Type *Ty) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000684 return printTypeInt(Out, Ty, TypeNames);
Chris Lattnerd816b532002-04-13 20:53:41 +0000685 }
686
687 // printTypeAtLeastOneLevel - Print out one level of the possibly complex type
688 // without considering any symbolic types that we may have equal to it.
689 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000690 std::ostream &printTypeAtLeastOneLevel(const Type *Ty);
Chris Lattner7bfee412001-10-29 16:05:51 +0000691
Chris Lattner862e3382001-10-13 06:42:36 +0000692 // printInfoComment - Print a little comment after the instruction indicating
693 // which slot it occupies.
Chris Lattner113f4f42002-06-25 16:13:24 +0000694 void printInfoComment(const Value &V);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000695};
Reid Spencerf43ac622004-05-27 22:04:46 +0000696} // end of llvm namespace
Chris Lattner2f7c9632001-06-06 20:29:01 +0000697
Misha Brukmanc566ca362004-03-02 00:22:19 +0000698/// printTypeAtLeastOneLevel - Print out one level of the possibly complex type
699/// without considering any symbolic types that we may have equal to it.
700///
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000701std::ostream &AssemblyWriter::printTypeAtLeastOneLevel(const Type *Ty) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000702 if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
Reid Spencer8c4914c2006-12-31 05:24:50 +0000703 printType(FTy->getReturnType());
Reid Spencer8c4914c2006-12-31 05:24:50 +0000704 Out << " (";
705 unsigned Idx = 1;
Chris Lattnerfa829be2004-02-09 04:14:01 +0000706 for (FunctionType::param_iterator I = FTy->param_begin(),
707 E = FTy->param_end(); I != E; ++I) {
708 if (I != FTy->param_begin())
Misha Brukmana6619a92004-06-21 21:53:56 +0000709 Out << ", ";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000710 printType(*I);
Reid Spencer8c4914c2006-12-31 05:24:50 +0000711 if (FTy->getParamAttrs(Idx)) {
712 Out << " " << FunctionType::getParamAttrsText(FTy->getParamAttrs(Idx));
713 }
714 Idx++;
Chris Lattnerd816b532002-04-13 20:53:41 +0000715 }
716 if (FTy->isVarArg()) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000717 if (FTy->getNumParams()) Out << ", ";
718 Out << "...";
Chris Lattnerd816b532002-04-13 20:53:41 +0000719 }
Misha Brukmana6619a92004-06-21 21:53:56 +0000720 Out << ')';
Reid Spencer136a91c2007-01-05 17:06:19 +0000721 if (FTy->getParamAttrs(0))
722 Out << ' ' << FunctionType::getParamAttrsText(FTy->getParamAttrs(0));
Chris Lattner113f4f42002-06-25 16:13:24 +0000723 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
Andrew Lenharthdcb3c972006-12-08 18:06:16 +0000724 if (STy->isPacked())
725 Out << '<';
Misha Brukmana6619a92004-06-21 21:53:56 +0000726 Out << "{ ";
Chris Lattnerac6db752004-02-09 04:37:31 +0000727 for (StructType::element_iterator I = STy->element_begin(),
728 E = STy->element_end(); I != E; ++I) {
729 if (I != STy->element_begin())
Misha Brukmana6619a92004-06-21 21:53:56 +0000730 Out << ", ";
Chris Lattnerd816b532002-04-13 20:53:41 +0000731 printType(*I);
732 }
Misha Brukmana6619a92004-06-21 21:53:56 +0000733 Out << " }";
Andrew Lenharthdcb3c972006-12-08 18:06:16 +0000734 if (STy->isPacked())
735 Out << '>';
Chris Lattner113f4f42002-06-25 16:13:24 +0000736 } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000737 printType(PTy->getElementType()) << '*';
Chris Lattner113f4f42002-06-25 16:13:24 +0000738 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000739 Out << '[' << ATy->getNumElements() << " x ";
Misha Brukman21bbdb92004-06-04 21:11:51 +0000740 printType(ATy->getElementType()) << ']';
Reid Spencere203d352004-08-20 15:37:30 +0000741 } else if (const PackedType *PTy = dyn_cast<PackedType>(Ty)) {
742 Out << '<' << PTy->getNumElements() << " x ";
743 printType(PTy->getElementType()) << '>';
744 }
Reid Spencerde46e482006-11-02 20:25:50 +0000745 else if (isa<OpaqueType>(Ty)) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000746 Out << "opaque";
Chris Lattnerd816b532002-04-13 20:53:41 +0000747 } else {
Vikram S. Adveb952b542002-07-14 23:14:45 +0000748 if (!Ty->isPrimitiveType())
Misha Brukmana6619a92004-06-21 21:53:56 +0000749 Out << "<unknown derived type>";
Chris Lattnerd816b532002-04-13 20:53:41 +0000750 printType(Ty);
751 }
Misha Brukmana6619a92004-06-21 21:53:56 +0000752 return Out;
Chris Lattnerd816b532002-04-13 20:53:41 +0000753}
754
755
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000756void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType) {
757 if (Operand == 0) {
Chris Lattner08f7d0c2005-02-24 16:58:29 +0000758 Out << "<null operand!>";
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000759 } else {
760 if (PrintType) { Out << ' '; printType(Operand->getType()); }
761 WriteAsOperandInternal(Out, Operand, TypeNames, &Machine);
Chris Lattner08f7d0c2005-02-24 16:58:29 +0000762 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000763}
764
Chris Lattner2f7c9632001-06-06 20:29:01 +0000765
Chris Lattner7bfee412001-10-29 16:05:51 +0000766void AssemblyWriter::printModule(const Module *M) {
Chris Lattner4d8689e2005-03-02 23:12:40 +0000767 if (!M->getModuleIdentifier().empty() &&
Misha Brukmanb1c93172005-04-21 23:48:37 +0000768 // Don't print the ID if it will start a new line (which would
Chris Lattner4d8689e2005-03-02 23:12:40 +0000769 // require a comment char before it).
770 M->getModuleIdentifier().find('\n') == std::string::npos)
771 Out << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
772
Owen Andersone2237542006-10-18 02:21:12 +0000773 if (!M->getDataLayout().empty())
Chris Lattner04897162006-10-22 06:06:56 +0000774 Out << "target datalayout = \"" << M->getDataLayout() << "\"\n";
Owen Andersone2237542006-10-18 02:21:12 +0000775
Chris Lattner8068e0c2003-08-24 13:48:48 +0000776 switch (M->getEndianness()) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000777 case Module::LittleEndian: Out << "target endian = little\n"; break;
778 case Module::BigEndian: Out << "target endian = big\n"; break;
Chris Lattner8068e0c2003-08-24 13:48:48 +0000779 case Module::AnyEndianness: break;
780 }
781 switch (M->getPointerSize()) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000782 case Module::Pointer32: Out << "target pointersize = 32\n"; break;
783 case Module::Pointer64: Out << "target pointersize = 64\n"; break;
Chris Lattner8068e0c2003-08-24 13:48:48 +0000784 case Module::AnyPointerSize: break;
785 }
Reid Spencer48f98c82004-07-25 21:44:54 +0000786 if (!M->getTargetTriple().empty())
Reid Spencerffec7df2004-07-25 21:29:43 +0000787 Out << "target triple = \"" << M->getTargetTriple() << "\"\n";
Misha Brukmanb1c93172005-04-21 23:48:37 +0000788
Chris Lattnereef2fe72006-01-24 04:13:11 +0000789 if (!M->getModuleInlineAsm().empty()) {
Chris Lattnerefaf35d2006-01-24 00:45:30 +0000790 // Split the string into lines, to make it easier to read the .ll file.
Chris Lattnereef2fe72006-01-24 04:13:11 +0000791 std::string Asm = M->getModuleInlineAsm();
Chris Lattnerefaf35d2006-01-24 00:45:30 +0000792 size_t CurPos = 0;
793 size_t NewLine = Asm.find_first_of('\n', CurPos);
794 while (NewLine != std::string::npos) {
795 // We found a newline, print the portion of the asm string from the
796 // last newline up to this newline.
797 Out << "module asm \"";
798 PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.begin()+NewLine),
799 Out);
800 Out << "\"\n";
801 CurPos = NewLine+1;
802 NewLine = Asm.find_first_of('\n', CurPos);
803 }
Chris Lattner3acaf5c2006-01-24 00:40:17 +0000804 Out << "module asm \"";
Chris Lattnerefaf35d2006-01-24 00:45:30 +0000805 PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.end()), Out);
Chris Lattner6ed87bd2006-01-23 23:03:36 +0000806 Out << "\"\n";
807 }
808
Chris Lattner2cdd49d2004-09-14 05:06:58 +0000809 // Loop over the dependent libraries and emit them.
Chris Lattner730cfe42004-09-14 04:51:44 +0000810 Module::lib_iterator LI = M->lib_begin();
811 Module::lib_iterator LE = M->lib_end();
Reid Spencer48f98c82004-07-25 21:44:54 +0000812 if (LI != LE) {
Chris Lattner730cfe42004-09-14 04:51:44 +0000813 Out << "deplibs = [ ";
814 while (LI != LE) {
Chris Lattner2cdd49d2004-09-14 05:06:58 +0000815 Out << '"' << *LI << '"';
Reid Spencerffec7df2004-07-25 21:29:43 +0000816 ++LI;
Chris Lattner730cfe42004-09-14 04:51:44 +0000817 if (LI != LE)
818 Out << ", ";
Reid Spencerffec7df2004-07-25 21:29:43 +0000819 }
820 Out << " ]\n";
Reid Spencercc5ff642004-07-25 18:08:18 +0000821 }
Reid Spencerb9e08772004-09-13 23:44:23 +0000822
Chris Lattner2cdd49d2004-09-14 05:06:58 +0000823 // Loop over the symbol table, emitting all named constants.
Reid Spencer32af9e82007-01-06 07:24:44 +0000824 printTypeSymbolTable(M->getTypeSymbolTable());
825 printValueSymbolTable(M->getValueSymbolTable());
Misha Brukmanb1c93172005-04-21 23:48:37 +0000826
Chris Lattner54932b02006-12-06 04:41:52 +0000827 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
828 I != E; ++I)
Chris Lattner113f4f42002-06-25 16:13:24 +0000829 printGlobal(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000830
Misha Brukmana6619a92004-06-21 21:53:56 +0000831 Out << "\nimplementation ; Functions:\n";
Misha Brukmanb1c93172005-04-21 23:48:37 +0000832
Chris Lattner2cdd49d2004-09-14 05:06:58 +0000833 // Output all of the functions.
Chris Lattner113f4f42002-06-25 16:13:24 +0000834 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
835 printFunction(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000836}
837
Chris Lattner7bfee412001-10-29 16:05:51 +0000838void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000839 if (GV->hasName()) Out << getLLVMName(GV->getName()) << " = ";
Chris Lattner37798642001-09-18 04:01:05 +0000840
Misha Brukmanb1c93172005-04-21 23:48:37 +0000841 if (!GV->hasInitializer())
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000842 switch (GV->getLinkage()) {
843 case GlobalValue::DLLImportLinkage: Out << "dllimport "; break;
844 case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break;
845 default: Out << "external "; break;
846 }
Chris Lattner379a8d22003-04-16 20:28:45 +0000847 else
848 switch (GV->getLinkage()) {
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000849 case GlobalValue::InternalLinkage: Out << "internal "; break;
850 case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
851 case GlobalValue::WeakLinkage: Out << "weak "; break;
852 case GlobalValue::AppendingLinkage: Out << "appending "; break;
853 case GlobalValue::DLLImportLinkage: Out << "dllimport "; break;
854 case GlobalValue::DLLExportLinkage: Out << "dllexport "; break;
855 case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break;
856 case GlobalValue::ExternalLinkage: break;
Misha Brukman70457632004-11-14 21:04:34 +0000857 case GlobalValue::GhostLinkage:
Bill Wendlingf3baad32006-12-07 01:30:32 +0000858 cerr << "GhostLinkage not allowed in AsmWriter!\n";
Misha Brukman70457632004-11-14 21:04:34 +0000859 abort();
Chris Lattner379a8d22003-04-16 20:28:45 +0000860 }
Chris Lattner37798642001-09-18 04:01:05 +0000861
Misha Brukmana6619a92004-06-21 21:53:56 +0000862 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattner2413b162001-12-04 00:03:30 +0000863 printType(GV->getType()->getElementType());
Chris Lattner37798642001-09-18 04:01:05 +0000864
Reid Spenceraccd7c72004-07-17 23:47:01 +0000865 if (GV->hasInitializer()) {
866 Constant* C = cast<Constant>(GV->getInitializer());
867 assert(C && "GlobalVar initializer isn't constant?");
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000868 writeOperand(GV->getInitializer(), false);
Reid Spenceraccd7c72004-07-17 23:47:01 +0000869 }
Chris Lattnerf8a974d2005-11-06 06:48:53 +0000870
Chris Lattner4b96c542005-11-12 00:10:19 +0000871 if (GV->hasSection())
872 Out << ", section \"" << GV->getSection() << '"';
873 if (GV->getAlignment())
Chris Lattnerf8a974d2005-11-06 06:48:53 +0000874 Out << ", align " << GV->getAlignment();
Chris Lattner4b96c542005-11-12 00:10:19 +0000875
Chris Lattner113f4f42002-06-25 16:13:24 +0000876 printInfoComment(*GV);
Misha Brukmana6619a92004-06-21 21:53:56 +0000877 Out << "\n";
Chris Lattnerda975502001-09-10 07:58:01 +0000878}
879
Reid Spencer32af9e82007-01-06 07:24:44 +0000880void AssemblyWriter::printTypeSymbolTable(const TypeSymbolTable &ST) {
Reid Spencere7e96712004-05-25 08:53:40 +0000881 // Print the types.
Reid Spencer32af9e82007-01-06 07:24:44 +0000882 for (TypeSymbolTable::const_iterator TI = ST.begin(), TE = ST.end();
883 TI != TE; ++TI) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000884 Out << "\t" << getLLVMName(TI->first) << " = type ";
Reid Spencere7e96712004-05-25 08:53:40 +0000885
886 // Make sure we print out at least one level of the type structure, so
887 // that we do not get %FILE = type %FILE
888 //
889 printTypeAtLeastOneLevel(TI->second) << "\n";
890 }
Reid Spencer32af9e82007-01-06 07:24:44 +0000891}
892
893// printSymbolTable - Run through symbol table looking for constants
894// and types. Emit their declarations.
895void AssemblyWriter::printValueSymbolTable(const SymbolTable &ST) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000896
Reid Spencere7e96712004-05-25 08:53:40 +0000897 // Print the constants, in type plane order.
898 for (SymbolTable::plane_const_iterator PI = ST.plane_begin();
Chris Lattner23e829a2006-12-06 05:12:21 +0000899 PI != ST.plane_end(); ++PI) {
Reid Spencere7e96712004-05-25 08:53:40 +0000900 SymbolTable::value_const_iterator VI = ST.value_begin(PI->first);
901 SymbolTable::value_const_iterator VE = ST.value_end(PI->first);
902
903 for (; VI != VE; ++VI) {
Reid Spenceraccd7c72004-07-17 23:47:01 +0000904 const Value* V = VI->second;
905 const Constant *CPV = dyn_cast<Constant>(V) ;
906 if (CPV && !isa<GlobalValue>(V)) {
Reid Spencer56010e42004-05-26 21:56:09 +0000907 printConstant(CPV);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000908 }
909 }
Chris Lattnera7620d92001-07-15 06:35:59 +0000910 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000911}
912
913
Misha Brukmanc566ca362004-03-02 00:22:19 +0000914/// printConstant - Print out a constant pool entry...
915///
Chris Lattner3462ae32001-12-03 22:26:30 +0000916void AssemblyWriter::printConstant(const Constant *CPV) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000917 // Don't print out unnamed constants, they will be inlined
918 if (!CPV->hasName()) return;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000919
Chris Lattneree998be2001-07-26 16:29:38 +0000920 // Print out name...
Misha Brukmana6619a92004-06-21 21:53:56 +0000921 Out << "\t" << getLLVMName(CPV->getName()) << " =";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000922
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000923 // Write the value out now.
924 writeOperand(CPV, true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000925
Chris Lattner113f4f42002-06-25 16:13:24 +0000926 printInfoComment(*CPV);
Misha Brukmana6619a92004-06-21 21:53:56 +0000927 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000928}
929
Misha Brukmanc566ca362004-03-02 00:22:19 +0000930/// printFunction - Print all aspects of a function.
931///
Chris Lattner113f4f42002-06-25 16:13:24 +0000932void AssemblyWriter::printFunction(const Function *F) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000933 // Print out the return type and name...
Misha Brukmana6619a92004-06-21 21:53:56 +0000934 Out << "\n";
Chris Lattner379a8d22003-04-16 20:28:45 +0000935
Chris Lattner97e36f22004-12-05 06:44:09 +0000936 // Ensure that no local symbols conflict with global symbols.
937 const_cast<Function*>(F)->renameLocalSymbols();
938
Misha Brukmana6619a92004-06-21 21:53:56 +0000939 if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +0000940
Chris Lattner379a8d22003-04-16 20:28:45 +0000941 if (F->isExternal())
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000942 switch (F->getLinkage()) {
943 case GlobalValue::DLLImportLinkage: Out << "declare dllimport "; break;
944 case GlobalValue::ExternalWeakLinkage: Out << "declare extern_weak "; break;
945 default: Out << "declare ";
946 }
Reid Spencer7ce2d2a2006-12-29 20:29:48 +0000947 else {
948 Out << "define ";
Chris Lattner379a8d22003-04-16 20:28:45 +0000949 switch (F->getLinkage()) {
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000950 case GlobalValue::InternalLinkage: Out << "internal "; break;
951 case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
952 case GlobalValue::WeakLinkage: Out << "weak "; break;
953 case GlobalValue::AppendingLinkage: Out << "appending "; break;
954 case GlobalValue::DLLImportLinkage: Out << "dllimport "; break;
955 case GlobalValue::DLLExportLinkage: Out << "dllexport "; break;
956 case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break;
Chris Lattner379a8d22003-04-16 20:28:45 +0000957 case GlobalValue::ExternalLinkage: break;
Misha Brukman70457632004-11-14 21:04:34 +0000958 case GlobalValue::GhostLinkage:
Bill Wendlingf3baad32006-12-07 01:30:32 +0000959 cerr << "GhostLinkage not allowed in AsmWriter!\n";
Misha Brukman70457632004-11-14 21:04:34 +0000960 abort();
Chris Lattner379a8d22003-04-16 20:28:45 +0000961 }
Reid Spencer7ce2d2a2006-12-29 20:29:48 +0000962 }
Chris Lattner379a8d22003-04-16 20:28:45 +0000963
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000964 // Print the calling convention.
965 switch (F->getCallingConv()) {
966 case CallingConv::C: break; // default
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +0000967 case CallingConv::CSRet: Out << "csretcc "; break;
968 case CallingConv::Fast: Out << "fastcc "; break;
969 case CallingConv::Cold: Out << "coldcc "; break;
970 case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break;
971 case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break;
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000972 default: Out << "cc" << F->getCallingConv() << " "; break;
973 }
974
Reid Spencer8c4914c2006-12-31 05:24:50 +0000975 const FunctionType *FT = F->getFunctionType();
Misha Brukman21bbdb92004-06-04 21:11:51 +0000976 printType(F->getReturnType()) << ' ';
Chris Lattner5b337482003-10-18 05:57:43 +0000977 if (!F->getName().empty())
Misha Brukmana6619a92004-06-21 21:53:56 +0000978 Out << getLLVMName(F->getName());
Chris Lattner5b337482003-10-18 05:57:43 +0000979 else
Misha Brukmana6619a92004-06-21 21:53:56 +0000980 Out << "\"\"";
981 Out << '(';
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000982 Machine.incorporateFunction(F);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000983
Chris Lattner7bfee412001-10-29 16:05:51 +0000984 // Loop over the arguments, printing them...
Chris Lattnerfee714f2001-09-07 16:36:04 +0000985
Reid Spencer8c4914c2006-12-31 05:24:50 +0000986 unsigned Idx = 1;
Chris Lattner54932b02006-12-06 04:41:52 +0000987 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
Reid Spencer8c4914c2006-12-31 05:24:50 +0000988 I != E; ++I) {
989 // Insert commas as we go... the first arg doesn't get a comma
990 if (I != F->arg_begin()) Out << ", ";
991 printArgument(I, FT->getParamAttrs(Idx));
992 Idx++;
993 }
Chris Lattnerfee714f2001-09-07 16:36:04 +0000994
995 // Finish printing arguments...
Chris Lattner113f4f42002-06-25 16:13:24 +0000996 if (FT->isVarArg()) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000997 if (FT->getNumParams()) Out << ", ";
998 Out << "..."; // Output varargs portion of signature!
Chris Lattnerfee714f2001-09-07 16:36:04 +0000999 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001000 Out << ')';
Reid Spencer136a91c2007-01-05 17:06:19 +00001001 if (FT->getParamAttrs(0))
1002 Out << ' ' << FunctionType::getParamAttrsText(FT->getParamAttrs(0));
Chris Lattner4b96c542005-11-12 00:10:19 +00001003 if (F->hasSection())
1004 Out << " section \"" << F->getSection() << '"';
Chris Lattnerf8a974d2005-11-06 06:48:53 +00001005 if (F->getAlignment())
1006 Out << " align " << F->getAlignment();
Chris Lattner4b96c542005-11-12 00:10:19 +00001007
Chris Lattner113f4f42002-06-25 16:13:24 +00001008 if (F->isExternal()) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001009 Out << "\n";
Chris Lattnerb2f02e52002-05-06 03:00:40 +00001010 } else {
Misha Brukmana6619a92004-06-21 21:53:56 +00001011 Out << " {";
Misha Brukmanb1c93172005-04-21 23:48:37 +00001012
Chris Lattner6915f8f2002-04-07 22:49:37 +00001013 // Output all of its basic blocks... for the function
Chris Lattner113f4f42002-06-25 16:13:24 +00001014 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
1015 printBasicBlock(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +00001016
Misha Brukmana6619a92004-06-21 21:53:56 +00001017 Out << "}\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +00001018 }
1019
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001020 Machine.purgeFunction();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001021}
1022
Misha Brukmanc566ca362004-03-02 00:22:19 +00001023/// printArgument - This member is called for every argument that is passed into
1024/// the function. Simply print it out
1025///
Reid Spencer8c4914c2006-12-31 05:24:50 +00001026void AssemblyWriter::printArgument(const Argument *Arg,
1027 FunctionType::ParameterAttributes attrs) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001028 // Output type...
Chris Lattner7bfee412001-10-29 16:05:51 +00001029 printType(Arg->getType());
Misha Brukmanb1c93172005-04-21 23:48:37 +00001030
Reid Spencer8c4914c2006-12-31 05:24:50 +00001031 if (attrs != FunctionType::NoAttributeSet)
1032 Out << ' ' << FunctionType::getParamAttrsText(attrs);
1033
Chris Lattner2f7c9632001-06-06 20:29:01 +00001034 // Output name, if available...
1035 if (Arg->hasName())
Misha Brukmana6619a92004-06-21 21:53:56 +00001036 Out << ' ' << getLLVMName(Arg->getName());
Chris Lattner2f7c9632001-06-06 20:29:01 +00001037}
1038
Misha Brukmanc566ca362004-03-02 00:22:19 +00001039/// printBasicBlock - This member is called for each basic block in a method.
1040///
Chris Lattner7bfee412001-10-29 16:05:51 +00001041void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001042 if (BB->hasName()) { // Print out the label if it exists...
Alkis Evlogimenos991d6ad2004-12-10 05:41:10 +00001043 Out << "\n" << getLLVMName(BB->getName(), false) << ':';
Chris Lattner408dbdb2002-05-14 16:02:05 +00001044 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Misha Brukmana6619a92004-06-21 21:53:56 +00001045 Out << "\n; <label>:";
Chris Lattner757ee0b2004-06-09 19:41:19 +00001046 int Slot = Machine.getSlot(BB);
1047 if (Slot != -1)
Misha Brukmana6619a92004-06-21 21:53:56 +00001048 Out << Slot;
Chris Lattner757ee0b2004-06-09 19:41:19 +00001049 else
Misha Brukmana6619a92004-06-21 21:53:56 +00001050 Out << "<badref>";
Chris Lattner58185f22002-10-02 19:38:55 +00001051 }
Chris Lattner2447ef52003-11-20 00:09:43 +00001052
1053 if (BB->getParent() == 0)
Misha Brukmana6619a92004-06-21 21:53:56 +00001054 Out << "\t\t; Error: Block without parent!";
Chris Lattner2447ef52003-11-20 00:09:43 +00001055 else {
1056 if (BB != &BB->getParent()->front()) { // Not the entry block?
1057 // Output predecessors for the block...
Misha Brukmana6619a92004-06-21 21:53:56 +00001058 Out << "\t\t;";
Chris Lattner2447ef52003-11-20 00:09:43 +00001059 pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
Misha Brukmanb1c93172005-04-21 23:48:37 +00001060
Chris Lattner2447ef52003-11-20 00:09:43 +00001061 if (PI == PE) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001062 Out << " No predecessors!";
Chris Lattner2447ef52003-11-20 00:09:43 +00001063 } else {
Misha Brukmana6619a92004-06-21 21:53:56 +00001064 Out << " preds =";
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001065 writeOperand(*PI, false);
Chris Lattner2447ef52003-11-20 00:09:43 +00001066 for (++PI; PI != PE; ++PI) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001067 Out << ',';
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001068 writeOperand(*PI, false);
Chris Lattner2447ef52003-11-20 00:09:43 +00001069 }
Chris Lattner00211f12003-11-16 22:59:57 +00001070 }
Chris Lattner58185f22002-10-02 19:38:55 +00001071 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001072 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001073
Misha Brukmana6619a92004-06-21 21:53:56 +00001074 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001075
Misha Brukmana6619a92004-06-21 21:53:56 +00001076 if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001077
Chris Lattnerfee714f2001-09-07 16:36:04 +00001078 // Output all of the instructions in the basic block...
Chris Lattner113f4f42002-06-25 16:13:24 +00001079 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
1080 printInstruction(*I);
Chris Lattner96cdd272004-03-08 18:51:45 +00001081
Misha Brukmana6619a92004-06-21 21:53:56 +00001082 if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001083}
1084
Chris Lattner862e3382001-10-13 06:42:36 +00001085
Misha Brukmanc566ca362004-03-02 00:22:19 +00001086/// printInfoComment - Print a little comment after the instruction indicating
1087/// which slot it occupies.
1088///
Chris Lattner113f4f42002-06-25 16:13:24 +00001089void AssemblyWriter::printInfoComment(const Value &V) {
1090 if (V.getType() != Type::VoidTy) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001091 Out << "\t\t; <";
Misha Brukman21bbdb92004-06-04 21:11:51 +00001092 printType(V.getType()) << '>';
Chris Lattner862e3382001-10-13 06:42:36 +00001093
Chris Lattner113f4f42002-06-25 16:13:24 +00001094 if (!V.hasName()) {
Chris Lattner757ee0b2004-06-09 19:41:19 +00001095 int SlotNum = Machine.getSlot(&V);
1096 if (SlotNum == -1)
Misha Brukmana6619a92004-06-21 21:53:56 +00001097 Out << ":<badref>";
Reid Spencer8beac692004-06-09 15:26:53 +00001098 else
Misha Brukmana6619a92004-06-21 21:53:56 +00001099 Out << ':' << SlotNum; // Print out the def slot taken.
Chris Lattner862e3382001-10-13 06:42:36 +00001100 }
Chris Lattnerb6c21db2005-02-01 01:24:01 +00001101 Out << " [#uses=" << V.getNumUses() << ']'; // Output # uses
Chris Lattner862e3382001-10-13 06:42:36 +00001102 }
1103}
1104
Reid Spencere7141c82006-08-28 01:02:49 +00001105// This member is called for each Instruction in a function..
Chris Lattner113f4f42002-06-25 16:13:24 +00001106void AssemblyWriter::printInstruction(const Instruction &I) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001107 if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001108
Misha Brukmana6619a92004-06-21 21:53:56 +00001109 Out << "\t";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001110
1111 // Print out name if it exists...
Chris Lattner113f4f42002-06-25 16:13:24 +00001112 if (I.hasName())
Misha Brukmana6619a92004-06-21 21:53:56 +00001113 Out << getLLVMName(I.getName()) << " = ";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001114
Chris Lattner06038452005-05-06 05:51:46 +00001115 // If this is a volatile load or store, print out the volatile marker.
Chris Lattner504f9242003-09-08 17:45:59 +00001116 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
Chris Lattner06038452005-05-06 05:51:46 +00001117 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile())) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001118 Out << "volatile ";
Chris Lattner06038452005-05-06 05:51:46 +00001119 } else if (isa<CallInst>(I) && cast<CallInst>(I).isTailCall()) {
1120 // If this is a call, check if it's a tail call.
1121 Out << "tail ";
1122 }
Chris Lattner504f9242003-09-08 17:45:59 +00001123
Chris Lattner2f7c9632001-06-06 20:29:01 +00001124 // Print out the opcode...
Misha Brukmana6619a92004-06-21 21:53:56 +00001125 Out << I.getOpcodeName();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001126
Reid Spencer45e52392006-12-03 06:27:29 +00001127 // Print out the compare instruction predicates
1128 if (const FCmpInst *FCI = dyn_cast<FCmpInst>(&I)) {
Reid Spencer812a1be2006-12-04 05:19:18 +00001129 Out << " " << getPredicateText(FCI->getPredicate());
Reid Spencer45e52392006-12-03 06:27:29 +00001130 } else if (const ICmpInst *ICI = dyn_cast<ICmpInst>(&I)) {
Reid Spencer812a1be2006-12-04 05:19:18 +00001131 Out << " " << getPredicateText(ICI->getPredicate());
Reid Spencer45e52392006-12-03 06:27:29 +00001132 }
1133
Chris Lattner2f7c9632001-06-06 20:29:01 +00001134 // Print out the type of the operands...
Chris Lattner113f4f42002-06-25 16:13:24 +00001135 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001136
1137 // Special case conditional branches to swizzle the condition out to the front
Chris Lattner113f4f42002-06-25 16:13:24 +00001138 if (isa<BranchInst>(I) && I.getNumOperands() > 1) {
1139 writeOperand(I.getOperand(2), true);
Misha Brukmana6619a92004-06-21 21:53:56 +00001140 Out << ',';
Chris Lattner2f7c9632001-06-06 20:29:01 +00001141 writeOperand(Operand, true);
Misha Brukmana6619a92004-06-21 21:53:56 +00001142 Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001143 writeOperand(I.getOperand(1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001144
Chris Lattner8d48df22002-04-13 18:34:38 +00001145 } else if (isa<SwitchInst>(I)) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001146 // Special case switch statement to get formatting nice and correct...
Misha Brukmana6619a92004-06-21 21:53:56 +00001147 writeOperand(Operand , true); Out << ',';
1148 writeOperand(I.getOperand(1), true); Out << " [";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001149
Chris Lattner113f4f42002-06-25 16:13:24 +00001150 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001151 Out << "\n\t\t";
1152 writeOperand(I.getOperand(op ), true); Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001153 writeOperand(I.getOperand(op+1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001154 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001155 Out << "\n\t]";
Chris Lattnerda558102001-10-02 03:41:24 +00001156 } else if (isa<PHINode>(I)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001157 Out << ' ';
Chris Lattner113f4f42002-06-25 16:13:24 +00001158 printType(I.getType());
Misha Brukmana6619a92004-06-21 21:53:56 +00001159 Out << ' ';
Chris Lattner2f7c9632001-06-06 20:29:01 +00001160
Chris Lattner113f4f42002-06-25 16:13:24 +00001161 for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001162 if (op) Out << ", ";
Misha Brukmanb1c93172005-04-21 23:48:37 +00001163 Out << '[';
Misha Brukmana6619a92004-06-21 21:53:56 +00001164 writeOperand(I.getOperand(op ), false); Out << ',';
1165 writeOperand(I.getOperand(op+1), false); Out << " ]";
Chris Lattner931ef3b2001-06-11 15:04:20 +00001166 }
Chris Lattner862e3382001-10-13 06:42:36 +00001167 } else if (isa<ReturnInst>(I) && !Operand) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001168 Out << " void";
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001169 } else if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
1170 // Print the calling convention being used.
1171 switch (CI->getCallingConv()) {
1172 case CallingConv::C: break; // default
Chris Lattner29d20852006-05-19 21:58:52 +00001173 case CallingConv::CSRet: Out << " csretcc"; break;
1174 case CallingConv::Fast: Out << " fastcc"; break;
1175 case CallingConv::Cold: Out << " coldcc"; break;
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +00001176 case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break;
1177 case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break;
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001178 default: Out << " cc" << CI->getCallingConv(); break;
1179 }
1180
Chris Lattner463d6a52003-08-05 15:34:45 +00001181 const PointerType *PTy = cast<PointerType>(Operand->getType());
1182 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1183 const Type *RetTy = FTy->getReturnType();
Chris Lattner2f2d9472001-11-06 21:28:12 +00001184
Chris Lattner463d6a52003-08-05 15:34:45 +00001185 // If possible, print out the short form of the call instruction. We can
Chris Lattner6915f8f2002-04-07 22:49:37 +00001186 // only do this if the first argument is a pointer to a nonvararg function,
Chris Lattner463d6a52003-08-05 15:34:45 +00001187 // and if the return type is not a pointer to a function.
Chris Lattner2f2d9472001-11-06 21:28:12 +00001188 //
Chris Lattner463d6a52003-08-05 15:34:45 +00001189 if (!FTy->isVarArg() &&
Misha Brukmanb1c93172005-04-21 23:48:37 +00001190 (!isa<PointerType>(RetTy) ||
Chris Lattnerd9a36a62002-07-25 20:58:51 +00001191 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001192 Out << ' '; printType(RetTy);
Chris Lattner2f2d9472001-11-06 21:28:12 +00001193 writeOperand(Operand, false);
1194 } else {
1195 writeOperand(Operand, true);
1196 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001197 Out << '(';
Reid Spencer8c4914c2006-12-31 05:24:50 +00001198 for (unsigned op = 1, Eop = I.getNumOperands(); op < Eop; ++op) {
1199 if (op > 1)
1200 Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001201 writeOperand(I.getOperand(op), true);
Reid Spencer8c4914c2006-12-31 05:24:50 +00001202 if (FTy->getParamAttrs(op) != FunctionType::NoAttributeSet)
1203 Out << " " << FTy->getParamAttrsText(FTy->getParamAttrs(op));
Chris Lattner2f7c9632001-06-06 20:29:01 +00001204 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001205 Out << " )";
Reid Spencer136a91c2007-01-05 17:06:19 +00001206 if (FTy->getParamAttrs(0) != FunctionType::NoAttributeSet)
1207 Out << ' ' << FTy->getParamAttrsText(FTy->getParamAttrs(0));
Chris Lattner113f4f42002-06-25 16:13:24 +00001208 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Chris Lattner463d6a52003-08-05 15:34:45 +00001209 const PointerType *PTy = cast<PointerType>(Operand->getType());
1210 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1211 const Type *RetTy = FTy->getReturnType();
1212
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001213 // Print the calling convention being used.
1214 switch (II->getCallingConv()) {
1215 case CallingConv::C: break; // default
Chris Lattner29d20852006-05-19 21:58:52 +00001216 case CallingConv::CSRet: Out << " csretcc"; break;
1217 case CallingConv::Fast: Out << " fastcc"; break;
1218 case CallingConv::Cold: Out << " coldcc"; break;
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +00001219 case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break;
1220 case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break;
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001221 default: Out << " cc" << II->getCallingConv(); break;
1222 }
1223
Chris Lattner463d6a52003-08-05 15:34:45 +00001224 // If possible, print out the short form of the invoke instruction. We can
1225 // only do this if the first argument is a pointer to a nonvararg function,
1226 // and if the return type is not a pointer to a function.
1227 //
1228 if (!FTy->isVarArg() &&
Misha Brukmanb1c93172005-04-21 23:48:37 +00001229 (!isa<PointerType>(RetTy) ||
Chris Lattner463d6a52003-08-05 15:34:45 +00001230 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001231 Out << ' '; printType(RetTy);
Chris Lattner463d6a52003-08-05 15:34:45 +00001232 writeOperand(Operand, false);
1233 } else {
1234 writeOperand(Operand, true);
1235 }
1236
Misha Brukmana6619a92004-06-21 21:53:56 +00001237 Out << '(';
Reid Spencer8c4914c2006-12-31 05:24:50 +00001238 for (unsigned op = 3, Eop = I.getNumOperands(); op < Eop; ++op) {
1239 if (op > 3)
1240 Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001241 writeOperand(I.getOperand(op), true);
Reid Spencer9cfa4e82006-12-31 22:17:01 +00001242 if (FTy->getParamAttrs(op-2) != FunctionType::NoAttributeSet)
1243 Out << " " << FTy->getParamAttrsText(FTy->getParamAttrs(op-2));
Chris Lattner862e3382001-10-13 06:42:36 +00001244 }
1245
Reid Spencer136a91c2007-01-05 17:06:19 +00001246 Out << " )";
1247 if (FTy->getParamAttrs(0) != FunctionType::NoAttributeSet)
1248 Out << " " << FTy->getParamAttrsText(FTy->getParamAttrs(0));
1249 Out << "\n\t\t\tto";
Chris Lattner862e3382001-10-13 06:42:36 +00001250 writeOperand(II->getNormalDest(), true);
Misha Brukmana6619a92004-06-21 21:53:56 +00001251 Out << " unwind";
Chris Lattnerfae8ab32004-02-08 21:44:31 +00001252 writeOperand(II->getUnwindDest(), true);
Chris Lattner862e3382001-10-13 06:42:36 +00001253
Chris Lattner113f4f42002-06-25 16:13:24 +00001254 } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(&I)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001255 Out << ' ';
Chris Lattner8d48df22002-04-13 18:34:38 +00001256 printType(AI->getType()->getElementType());
1257 if (AI->isArrayAllocation()) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001258 Out << ',';
Chris Lattner8d48df22002-04-13 18:34:38 +00001259 writeOperand(AI->getArraySize(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001260 }
Nate Begeman848622f2005-11-05 09:21:28 +00001261 if (AI->getAlignment()) {
Chris Lattner7aeee3a2005-11-05 21:20:34 +00001262 Out << ", align " << AI->getAlignment();
Nate Begeman848622f2005-11-05 09:21:28 +00001263 }
Chris Lattner862e3382001-10-13 06:42:36 +00001264 } else if (isa<CastInst>(I)) {
Chris Lattnerc96e96b2003-11-17 01:17:04 +00001265 if (Operand) writeOperand(Operand, true); // Work with broken code
Misha Brukmana6619a92004-06-21 21:53:56 +00001266 Out << " to ";
Chris Lattner113f4f42002-06-25 16:13:24 +00001267 printType(I.getType());
Chris Lattner5b337482003-10-18 05:57:43 +00001268 } else if (isa<VAArgInst>(I)) {
Chris Lattnerc96e96b2003-11-17 01:17:04 +00001269 if (Operand) writeOperand(Operand, true); // Work with broken code
Misha Brukmana6619a92004-06-21 21:53:56 +00001270 Out << ", ";
Chris Lattnerf70da102003-05-08 02:44:12 +00001271 printType(I.getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +00001272 } else if (Operand) { // Print the normal way...
1273
Misha Brukmanb1c93172005-04-21 23:48:37 +00001274 // PrintAllTypes - Instructions who have operands of all the same type
Chris Lattner2f7c9632001-06-06 20:29:01 +00001275 // omit the type from all but the first operand. If the instruction has
1276 // different type operands (for example br), then they are all printed.
1277 bool PrintAllTypes = false;
1278 const Type *TheType = Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001279
Chris Lattner52bd5cb92004-03-12 05:53:14 +00001280 // Shift Left & Right print both types even for Ubyte LHS, and select prints
1281 // types even if all operands are bools.
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001282 if (isa<ShiftInst>(I) || isa<SelectInst>(I) || isa<StoreInst>(I) ||
1283 isa<ShuffleVectorInst>(I)) {
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00001284 PrintAllTypes = true;
1285 } else {
1286 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
1287 Operand = I.getOperand(i);
1288 if (Operand->getType() != TheType) {
1289 PrintAllTypes = true; // We have differing types! Print them all!
1290 break;
1291 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001292 }
1293 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001294
Chris Lattner7bfee412001-10-29 16:05:51 +00001295 if (!PrintAllTypes) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001296 Out << ' ';
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00001297 printType(TheType);
Chris Lattner7bfee412001-10-29 16:05:51 +00001298 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001299
Chris Lattner113f4f42002-06-25 16:13:24 +00001300 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001301 if (i) Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001302 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001303 }
1304 }
1305
Chris Lattner862e3382001-10-13 06:42:36 +00001306 printInfoComment(I);
Misha Brukmana6619a92004-06-21 21:53:56 +00001307 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001308}
1309
1310
1311//===----------------------------------------------------------------------===//
1312// External Interface declarations
1313//===----------------------------------------------------------------------===//
1314
Chris Lattner8339f7d2003-10-30 23:41:03 +00001315void Module::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001316 SlotMachine SlotTable(this);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001317 AssemblyWriter W(o, SlotTable, this, AAW);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001318 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001319}
1320
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001321void GlobalVariable::print(std::ostream &o) const {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001322 SlotMachine SlotTable(getParent());
Chris Lattner8339f7d2003-10-30 23:41:03 +00001323 AssemblyWriter W(o, SlotTable, getParent(), 0);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001324 W.write(this);
Chris Lattneradfe0d12001-09-10 20:08:19 +00001325}
1326
Chris Lattner8339f7d2003-10-30 23:41:03 +00001327void Function::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001328 SlotMachine SlotTable(getParent());
Chris Lattner8339f7d2003-10-30 23:41:03 +00001329 AssemblyWriter W(o, SlotTable, getParent(), AAW);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001330
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001331 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001332}
1333
Chris Lattnereef2fe72006-01-24 04:13:11 +00001334void InlineAsm::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001335 WriteAsOperand(o, this, true, 0);
Chris Lattnereef2fe72006-01-24 04:13:11 +00001336}
1337
Chris Lattner8339f7d2003-10-30 23:41:03 +00001338void BasicBlock::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001339 SlotMachine SlotTable(getParent());
Misha Brukmanb1c93172005-04-21 23:48:37 +00001340 AssemblyWriter W(o, SlotTable,
Chris Lattner8339f7d2003-10-30 23:41:03 +00001341 getParent() ? getParent()->getParent() : 0, AAW);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001342 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001343}
1344
Chris Lattner8339f7d2003-10-30 23:41:03 +00001345void Instruction::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001346 const Function *F = getParent() ? getParent()->getParent() : 0;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001347 SlotMachine SlotTable(F);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001348 AssemblyWriter W(o, SlotTable, F ? F->getParent() : 0, AAW);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001349
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001350 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001351}
Chris Lattner7db79582001-11-07 04:21:57 +00001352
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001353void Constant::print(std::ostream &o) const {
1354 if (this == 0) { o << "<null> constant value\n"; return; }
Chris Lattner7d734802002-09-10 15:53:49 +00001355
Misha Brukman21bbdb92004-06-04 21:11:51 +00001356 o << ' ' << getType()->getDescription() << ' ';
Evan Cheng5b19a802006-03-01 22:17:00 +00001357
1358 std::map<const Type *, std::string> TypeTable;
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001359 WriteConstantInt(o, this, TypeTable, 0);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001360}
1361
Misha Brukmanb1c93172005-04-21 23:48:37 +00001362void Type::print(std::ostream &o) const {
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001363 if (this == 0)
1364 o << "<null Type>";
1365 else
1366 o << getDescription();
1367}
1368
Chris Lattner2e9fa6d2002-04-09 19:48:49 +00001369void Argument::print(std::ostream &o) const {
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001370 WriteAsOperand(o, this, true, getParent() ? getParent()->getParent() : 0);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001371}
1372
Reid Spencer52641832004-05-25 18:14:38 +00001373// Value::dump - allow easy printing of Values from the debugger.
1374// Located here because so much of the needed functionality is here.
Bill Wendling22e978a2006-12-07 20:04:42 +00001375void Value::dump() const { print(*cerr.stream()); cerr << '\n'; }
Reid Spencer52641832004-05-25 18:14:38 +00001376
1377// Type::dump - allow easy printing of Values from the debugger.
1378// Located here because so much of the needed functionality is here.
Bill Wendling22e978a2006-12-07 20:04:42 +00001379void Type::dump() const { print(*cerr.stream()); cerr << '\n'; }
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001380
1381//===----------------------------------------------------------------------===//
Chris Lattnerfc9f1c92006-12-06 06:40:49 +00001382// SlotMachine Implementation
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001383//===----------------------------------------------------------------------===//
1384
1385#if 0
Bill Wendlingf3baad32006-12-07 01:30:32 +00001386#define SC_DEBUG(X) cerr << X
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001387#else
1388#define SC_DEBUG(X)
1389#endif
1390
1391// Module level constructor. Causes the contents of the Module (sans functions)
1392// to be added to the slot table.
Misha Brukmanb1c93172005-04-21 23:48:37 +00001393SlotMachine::SlotMachine(const Module *M)
Reid Spencer56010e42004-05-26 21:56:09 +00001394 : TheModule(M) ///< Saved for lazy initialization.
1395 , TheFunction(0)
Reid Spencerb0ac8c42004-08-16 07:46:33 +00001396 , FunctionProcessed(false)
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001397{
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001398}
1399
1400// Function level constructor. Causes the contents of the Module and the one
1401// function provided to be added to the slot table.
Chris Lattner23e829a2006-12-06 05:12:21 +00001402SlotMachine::SlotMachine(const Function *F)
1403 : TheModule(F ? F->getParent() : 0) ///< Saved for lazy initialization
Reid Spencer56010e42004-05-26 21:56:09 +00001404 , TheFunction(F) ///< Saved for lazy initialization
Reid Spencerb0ac8c42004-08-16 07:46:33 +00001405 , FunctionProcessed(false)
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001406{
Reid Spencer56010e42004-05-26 21:56:09 +00001407}
1408
1409inline void SlotMachine::initialize(void) {
Chris Lattner23e829a2006-12-06 05:12:21 +00001410 if (TheModule) {
Misha Brukmanb1c93172005-04-21 23:48:37 +00001411 processModule();
Reid Spencer56010e42004-05-26 21:56:09 +00001412 TheModule = 0; ///< Prevent re-processing next time we're called.
1413 }
Chris Lattner193de902006-12-06 05:27:40 +00001414 if (TheFunction && !FunctionProcessed)
Misha Brukmanb1c93172005-04-21 23:48:37 +00001415 processFunction();
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001416}
1417
1418// Iterate through all the global variables, functions, and global
Misha Brukmanb1c93172005-04-21 23:48:37 +00001419// variable initializers and create slots for them.
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001420void SlotMachine::processModule() {
1421 SC_DEBUG("begin processModule!\n");
1422
Chris Lattner0dda8612006-12-06 06:15:43 +00001423 // Add all of the unnamed global variables to the value table.
Chris Lattner54932b02006-12-06 04:41:52 +00001424 for (Module::const_global_iterator I = TheModule->global_begin(),
1425 E = TheModule->global_end(); I != E; ++I)
Chris Lattner0dda8612006-12-06 06:15:43 +00001426 if (!I->hasName())
Chris Lattner89e774e2007-01-09 07:46:21 +00001427 CreateSlot(I);
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001428
Chris Lattner0dda8612006-12-06 06:15:43 +00001429 // Add all the unnamed functions to the table.
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001430 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
1431 I != E; ++I)
Chris Lattner0dda8612006-12-06 06:15:43 +00001432 if (!I->hasName())
Chris Lattner89e774e2007-01-09 07:46:21 +00001433 CreateSlot(I);
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001434
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001435 SC_DEBUG("end processModule!\n");
1436}
1437
1438
Reid Spencer56010e42004-05-26 21:56:09 +00001439// Process the arguments, basic blocks, and instructions of a function.
1440void SlotMachine::processFunction() {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001441 SC_DEBUG("begin processFunction!\n");
1442
Chris Lattner0dda8612006-12-06 06:15:43 +00001443 // Add all the function arguments with no names.
Misha Brukmanb1c93172005-04-21 23:48:37 +00001444 for(Function::const_arg_iterator AI = TheFunction->arg_begin(),
Chris Lattner531f9e92005-03-15 04:54:21 +00001445 AE = TheFunction->arg_end(); AI != AE; ++AI)
Chris Lattner0dda8612006-12-06 06:15:43 +00001446 if (!AI->hasName())
Chris Lattner89e774e2007-01-09 07:46:21 +00001447 CreateSlot(AI);
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001448
1449 SC_DEBUG("Inserting Instructions:\n");
1450
Chris Lattner0dda8612006-12-06 06:15:43 +00001451 // Add all of the basic blocks and instructions with no names.
Misha Brukmanb1c93172005-04-21 23:48:37 +00001452 for (Function::const_iterator BB = TheFunction->begin(),
Reid Spencer56010e42004-05-26 21:56:09 +00001453 E = TheFunction->end(); BB != E; ++BB) {
Chris Lattner0dda8612006-12-06 06:15:43 +00001454 if (!BB->hasName())
Chris Lattner89e774e2007-01-09 07:46:21 +00001455 CreateSlot(BB);
Chris Lattner0dda8612006-12-06 06:15:43 +00001456 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
1457 if (I->getType() != Type::VoidTy && !I->hasName())
Chris Lattner89e774e2007-01-09 07:46:21 +00001458 CreateSlot(I);
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001459 }
1460
Reid Spencerb0ac8c42004-08-16 07:46:33 +00001461 FunctionProcessed = true;
1462
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001463 SC_DEBUG("end processFunction!\n");
1464}
1465
Chris Lattner193de902006-12-06 05:27:40 +00001466/// Clean up after incorporating a function. This is the only way to get out of
Chris Lattner89e774e2007-01-09 07:46:21 +00001467/// the function incorporation state that affects getSlot/CreateSlot. Function
1468/// incorporation state is indicated by TheFunction != 0.
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001469void SlotMachine::purgeFunction() {
1470 SC_DEBUG("begin purgeFunction!\n");
1471 fMap.clear(); // Simply discard the function level map
Reid Spencer56010e42004-05-26 21:56:09 +00001472 TheFunction = 0;
Reid Spencerb0ac8c42004-08-16 07:46:33 +00001473 FunctionProcessed = false;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001474 SC_DEBUG("end purgeFunction!\n");
1475}
1476
1477/// Get the slot number for a value. This function will assert if you
Chris Lattner89e774e2007-01-09 07:46:21 +00001478/// ask for a Value that hasn't previously been inserted with CreateSlot.
Chris Lattner757ee0b2004-06-09 19:41:19 +00001479int SlotMachine::getSlot(const Value *V) {
Chris Lattner23e829a2006-12-06 05:12:21 +00001480 assert(V && "Can't get slot for null Value");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001481 assert(!isa<Constant>(V) || isa<GlobalValue>(V) &&
1482 "Can't insert a non-GlobalValue Constant into SlotMachine");
Reid Spencer56010e42004-05-26 21:56:09 +00001483
1484 // Check for uninitialized state and do lazy initialization
1485 this->initialize();
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001486
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001487 // Get the type of the value
1488 const Type* VTy = V->getType();
1489
1490 // Find the type plane in the module map
1491 TypedPlanes::const_iterator MI = mMap.find(VTy);
1492
Chris Lattner23e829a2006-12-06 05:12:21 +00001493 if (TheFunction) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001494 // Lookup the type in the function map too
1495 TypedPlanes::const_iterator FI = fMap.find(VTy);
1496 // If there is a corresponding type plane in the function map
Chris Lattner23e829a2006-12-06 05:12:21 +00001497 if (FI != fMap.end()) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001498 // Lookup the Value in the function map
1499 ValueMap::const_iterator FVI = FI->second.map.find(V);
1500 // If the value doesn't exist in the function map
Chris Lattner23e829a2006-12-06 05:12:21 +00001501 if (FVI == FI->second.map.end()) {
Chris Lattner68a038e2004-06-09 22:22:10 +00001502 // Look up the value in the module map.
1503 if (MI == mMap.end()) return -1;
Reid Spencer56010e42004-05-26 21:56:09 +00001504 ValueMap::const_iterator MVI = MI->second.map.find(V);
1505 // If we didn't find it, it wasn't inserted
Chris Lattner757ee0b2004-06-09 19:41:19 +00001506 if (MVI == MI->second.map.end()) return -1;
Chris Lattner23e829a2006-12-06 05:12:21 +00001507 assert(MVI != MI->second.map.end() && "Value not found");
Reid Spencer56010e42004-05-26 21:56:09 +00001508 // We found it only at the module level
Misha Brukmanb1c93172005-04-21 23:48:37 +00001509 return MVI->second;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001510
1511 // else the value exists in the function map
1512 } else {
Reid Spencer56010e42004-05-26 21:56:09 +00001513 // Return the slot number as the module's contribution to
1514 // the type plane plus the index in the function's contribution
1515 // to the type plane.
Chris Lattnerb1f04782004-06-15 21:07:32 +00001516 if (MI != mMap.end())
1517 return MI->second.next_slot + FVI->second;
1518 else
1519 return FVI->second;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001520 }
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001521 }
1522 }
1523
Reid Spencer8beac692004-06-09 15:26:53 +00001524 // N.B. Can get here only if either !TheFunction or the function doesn't
1525 // have a corresponding type plane for the Value
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001526
1527 // Make sure the type plane exists
Chris Lattner757ee0b2004-06-09 19:41:19 +00001528 if (MI == mMap.end()) return -1;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001529 // Lookup the value in the module's map
1530 ValueMap::const_iterator MVI = MI->second.map.find(V);
1531 // Make sure we found it.
Chris Lattner757ee0b2004-06-09 19:41:19 +00001532 if (MVI == MI->second.map.end()) return -1;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001533 // Return it.
1534 return MVI->second;
1535}
1536
Reid Spencer58d30f22004-07-04 11:50:43 +00001537
Chris Lattner89e774e2007-01-09 07:46:21 +00001538/// CreateSlot - Create a new slot for the specified value if it has no name.
1539void SlotMachine::CreateSlot(const Value *V) {
Chris Lattner0dda8612006-12-06 06:15:43 +00001540 const Type* VTy = V->getType();
1541 assert(VTy != Type::VoidTy && !V->hasName() && "Doesn't need a slot!");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001542 assert(!isa<Constant>(V) || isa<GlobalValue>(V) &&
1543 "Can't insert a non-GlobalValue Constant into SlotMachine");
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001544
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001545 // Look up the type plane for the Value's type from the module map
1546 TypedPlanes::const_iterator MI = mMap.find(VTy);
1547
Chris Lattner23e829a2006-12-06 05:12:21 +00001548 if (TheFunction) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001549 // Get the type plane for the Value's type from the function map
1550 TypedPlanes::const_iterator FI = fMap.find(VTy);
1551 // If there is a corresponding type plane in the function map
Chris Lattner23e829a2006-12-06 05:12:21 +00001552 if (FI != fMap.end()) {
Chris Lattner89e774e2007-01-09 07:46:21 +00001553 // Lookup the Value in the function map.
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001554 ValueMap::const_iterator FVI = FI->second.map.find(V);
Chris Lattner89e774e2007-01-09 07:46:21 +00001555 // If the value exists in the function map, we're done.
1556 if (FVI != FI->second.map.end())
1557 return;
1558
1559 // If there is no corresponding type plane in the module map
1560 if (MI == mMap.end())
1561 return insertValue(V);
1562 // Look up the value in the module map
1563 ValueMap::const_iterator MVI = MI->second.map.find(V);
1564 // If we found it, it was already inserted
1565 if (MVI != MI->second.map.end())
1566 return;
1567 return insertValue(V);
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001568
1569 // else there is not a corresponding type plane in the function map
1570 } else {
1571 // If the type plane doesn't exists at the module level
Chris Lattner23e829a2006-12-06 05:12:21 +00001572 if (MI == mMap.end()) {
Reid Spencer56010e42004-05-26 21:56:09 +00001573 return insertValue(V);
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001574 // else type plane exists at the module level, examine it
1575 } else {
Reid Spencer56010e42004-05-26 21:56:09 +00001576 // Look up the value in the module's map
1577 ValueMap::const_iterator MVI = MI->second.map.find(V);
1578 // If we didn't find it there either
Chris Lattner23e829a2006-12-06 05:12:21 +00001579 if (MVI == MI->second.map.end())
Reid Spencer56010e42004-05-26 21:56:09 +00001580 // Return the slot number as the module's contribution to
1581 // the type plane plus the index of the function map insertion.
Chris Lattner89e774e2007-01-09 07:46:21 +00001582 return insertValue(V);
Reid Spencer56010e42004-05-26 21:56:09 +00001583 else
Chris Lattner89e774e2007-01-09 07:46:21 +00001584 return;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001585 }
1586 }
1587 }
1588
Chris Lattner0dda8612006-12-06 06:15:43 +00001589 // N.B. Can only get here if TheFunction == 0
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001590
1591 // If the module map's type plane is not for the Value's type
Chris Lattner23e829a2006-12-06 05:12:21 +00001592 if (MI != mMap.end()) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001593 // Lookup the value in the module's map
1594 ValueMap::const_iterator MVI = MI->second.map.find(V);
Chris Lattner23e829a2006-12-06 05:12:21 +00001595 if (MVI != MI->second.map.end())
Chris Lattner89e774e2007-01-09 07:46:21 +00001596 return;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001597 }
1598
1599 return insertValue(V);
1600}
1601
1602
1603// Low level insert function. Minimal checking is done. This
Chris Lattner89e774e2007-01-09 07:46:21 +00001604// function is just for the convenience of CreateSlot (above).
1605void SlotMachine::insertValue(const Value *V) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001606 assert(V && "Can't insert a null Value into SlotMachine!");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001607 assert(!isa<Constant>(V) || isa<GlobalValue>(V) &&
Chris Lattner7e4e7662006-12-06 05:42:32 +00001608 "Can't insert a non-GlobalValue Constant into SlotMachine");
1609 assert(V->getType() != Type::VoidTy && !V->hasName());
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001610
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001611 const Type *VTy = V->getType();
1612 unsigned DestSlot = 0;
1613
Chris Lattner23e829a2006-12-06 05:12:21 +00001614 if (TheFunction) {
1615 TypedPlanes::iterator I = fMap.find(VTy);
1616 if (I == fMap.end())
Reid Spencer58d30f22004-07-04 11:50:43 +00001617 I = fMap.insert(std::make_pair(VTy,ValuePlane())).first;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001618 DestSlot = I->second.map[V] = I->second.next_slot++;
1619 } else {
Chris Lattner23e829a2006-12-06 05:12:21 +00001620 TypedPlanes::iterator I = mMap.find(VTy);
1621 if (I == mMap.end())
Reid Spencer58d30f22004-07-04 11:50:43 +00001622 I = mMap.insert(std::make_pair(VTy,ValuePlane())).first;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001623 DestSlot = I->second.map[V] = I->second.next_slot++;
1624 }
1625
Misha Brukmanb1c93172005-04-21 23:48:37 +00001626 SC_DEBUG(" Inserting value [" << VTy << "] = " << V << " slot=" <<
Reid Spencer56010e42004-05-26 21:56:09 +00001627 DestSlot << " [");
Chris Lattnerf8090cb2006-12-06 05:55:41 +00001628 // G = Global, F = Function, o = other
1629 SC_DEBUG((isa<GlobalVariable>(V) ? 'G' : (isa<Function>(V) ? 'F' : 'o')));
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001630 SC_DEBUG("]\n");
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001631}