blob: e89e32f9be0a82c203acd5010ae2a182b9cfff0a [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 Lattner7db79582001-11-07 04:21:57 +000017#include "llvm/Assembly/CachedWriter.h"
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +000018#include "llvm/Assembly/Writer.h"
Chris Lattner7f8845a2002-07-23 18:07:49 +000019#include "llvm/Assembly/PrintModulePass.h"
Chris Lattner8339f7d2003-10-30 23:41:03 +000020#include "llvm/Assembly/AsmAnnotationWriter.h"
Chris Lattnerf7b6d312005-05-06 20:26:43 +000021#include "llvm/CallingConv.h"
Chris Lattnerc70b3f62004-01-20 19:50:34 +000022#include "llvm/Constants.h"
Chris Lattner913d18f2002-04-29 18:46:50 +000023#include "llvm/DerivedTypes.h"
Chris Lattner8bbcda22006-01-25 18:57:27 +000024#include "llvm/InlineAsm.h"
Vikram S. Adveb952b542002-07-14 23:14:45 +000025#include "llvm/Instruction.h"
Misha Brukman2d3fa9e2004-07-29 16:53:53 +000026#include "llvm/Instructions.h"
Chris Lattnerc70b3f62004-01-20 19:50:34 +000027#include "llvm/Module.h"
Chris Lattnerfee714f2001-09-07 16:36:04 +000028#include "llvm/SymbolTable.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
82 /// Determine if a Value has a slot or not
83 bool hasSlot(const Value* V);
Reid Spencer58d30f22004-07-04 11:50:43 +000084 bool hasSlot(const Type* Ty);
Reid Spencer16f2f7f2004-05-26 07:18:52 +000085
86/// @}
87/// @name Mutators
88/// @{
89public:
Misha Brukmanb1c93172005-04-21 23:48:37 +000090 /// If you'd like to deal with a function instead of just a module, use
Reid Spencer16f2f7f2004-05-26 07:18:52 +000091 /// this method to get its data into the SlotMachine.
Misha Brukmanb1c93172005-04-21 23:48:37 +000092 void incorporateFunction(const Function *F) {
93 TheFunction = F;
Reid Spencerb0ac8c42004-08-16 07:46:33 +000094 FunctionProcessed = false;
95 }
Reid Spencer16f2f7f2004-05-26 07:18:52 +000096
Misha Brukmanb1c93172005-04-21 23:48:37 +000097 /// After calling incorporateFunction, use this method to remove the
98 /// most recently incorporated function from the SlotMachine. This
Reid Spencer16f2f7f2004-05-26 07:18:52 +000099 /// will reset the state of the machine back to just the module contents.
100 void purgeFunction();
101
102/// @}
103/// @name Implementation Details
104/// @{
105private:
Reid Spencer56010e42004-05-26 21:56:09 +0000106 /// This function does the actual initialization.
107 inline void initialize();
108
Misha Brukmanb1c93172005-04-21 23:48:37 +0000109 /// Values can be crammed into here at will. If they haven't
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000110 /// been inserted already, they get inserted, otherwise they are ignored.
111 /// Either way, the slot number for the Value* is returned.
Chris Lattner193de902006-12-06 05:27:40 +0000112 unsigned getOrCreateSlot(const Value *V);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000113
114 /// Insert a value into the value table. Return the slot number
115 /// that it now occupies. BadThings(TM) will happen if you insert a
Misha Brukmanb1c93172005-04-21 23:48:37 +0000116 /// Value that's already been inserted.
Chris Lattner23e829a2006-12-06 05:12:21 +0000117 unsigned insertValue(const Value *V);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000118
119 /// Add all of the module level global variables (and their initializers)
120 /// and function declarations, but not the contents of those functions.
121 void processModule();
122
Reid Spencer56010e42004-05-26 21:56:09 +0000123 /// Add all of the functions arguments, basic blocks, and instructions
124 void processFunction();
125
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000126 SlotMachine(const SlotMachine &); // DO NOT IMPLEMENT
127 void operator=(const SlotMachine &); // DO NOT IMPLEMENT
128
129/// @}
130/// @name Data
131/// @{
132public:
133
134 /// @brief The module for which we are holding slot numbers
Reid Spencer56010e42004-05-26 21:56:09 +0000135 const Module* TheModule;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000136
Reid Spencer56010e42004-05-26 21:56:09 +0000137 /// @brief The function for which we are holding slot numbers
138 const Function* TheFunction;
Reid Spencerb0ac8c42004-08-16 07:46:33 +0000139 bool FunctionProcessed;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000140
141 /// @brief The TypePlanes map for the module level data
142 TypedPlanes mMap;
143
144 /// @brief The TypePlanes map for the function level data
145 TypedPlanes fMap;
146
147/// @}
148
149};
150
Chris Lattner60a7dd12004-07-15 02:51:31 +0000151} // end namespace llvm
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000152
Chris Lattnerc2d3d312006-08-27 22:42:52 +0000153static RegisterPass<PrintModulePass>
Chris Lattnere7134c52006-08-21 17:20:01 +0000154X("printm", "Print module to stderr");
Chris Lattnerc2d3d312006-08-27 22:42:52 +0000155static RegisterPass<PrintFunctionPass>
Chris Lattnere7134c52006-08-21 17:20:01 +0000156Y("print","Print function to stderr");
Chris Lattner7f8845a2002-07-23 18:07:49 +0000157
Misha Brukmanb1c93172005-04-21 23:48:37 +0000158static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000159 bool PrintName,
Chris Lattnera9f0a112006-12-06 05:50:41 +0000160 std::map<const Type *, std::string> &TypeTable,
Reid Spencer58d30f22004-07-04 11:50:43 +0000161 SlotMachine *Machine);
162
Chris Lattnerb86620e2001-10-29 16:37:48 +0000163static const Module *getModuleFromVal(const Value *V) {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000164 if (const Argument *MA = dyn_cast<Argument>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +0000165 return MA->getParent() ? MA->getParent()->getParent() : 0;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000166 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +0000167 return BB->getParent() ? BB->getParent()->getParent() : 0;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000168 else if (const Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner57698e22002-03-26 18:01:55 +0000169 const Function *M = I->getParent() ? I->getParent()->getParent() : 0;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000170 return M ? M->getParent() : 0;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000171 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +0000172 return GV->getParent();
Chris Lattnerb86620e2001-10-29 16:37:48 +0000173 return 0;
174}
175
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000176static SlotMachine *createSlotMachine(const Value *V) {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000177 if (const Argument *FA = dyn_cast<Argument>(V)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000178 return new SlotMachine(FA->getParent());
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000179 } else if (const Instruction *I = dyn_cast<Instruction>(V)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000180 return new SlotMachine(I->getParent()->getParent());
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000181 } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000182 return new SlotMachine(BB->getParent());
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000183 } else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)){
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000184 return new SlotMachine(GV->getParent());
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000185 } else if (const Function *Func = dyn_cast<Function>(V)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000186 return new SlotMachine(Func);
Chris Lattner7bfee412001-10-29 16:05:51 +0000187 }
188 return 0;
189}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000190
Chris Lattner1c343042003-08-22 05:40:38 +0000191// getLLVMName - Turn the specified string into an 'LLVM name', which is either
192// prefixed with % (if the string only contains simple characters) or is
193// surrounded with ""'s (if it has special chars in it).
Alkis Evlogimenos991d6ad2004-12-10 05:41:10 +0000194static std::string getLLVMName(const std::string &Name,
195 bool prefixName = true) {
Chris Lattner1c343042003-08-22 05:40:38 +0000196 assert(!Name.empty() && "Cannot get empty name!");
197
198 // First character cannot start with a number...
199 if (Name[0] >= '0' && Name[0] <= '9')
200 return "\"" + Name + "\"";
201
202 // Scan to see if we have any characters that are not on the "white list"
203 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
204 char C = Name[i];
205 assert(C != '"' && "Illegal character in LLVM value name!");
206 if ((C < 'a' || C > 'z') && (C < 'A' || C > 'Z') && (C < '0' || C > '9') &&
207 C != '-' && C != '.' && C != '_')
208 return "\"" + Name + "\"";
209 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000210
Chris Lattner1c343042003-08-22 05:40:38 +0000211 // If we get here, then the identifier is legal to use as a "VarID".
Alkis Evlogimenos991d6ad2004-12-10 05:41:10 +0000212 if (prefixName)
213 return "%"+Name;
214 else
215 return Name;
Chris Lattner1c343042003-08-22 05:40:38 +0000216}
217
Chris Lattnerb86620e2001-10-29 16:37:48 +0000218
Misha Brukmanc566ca362004-03-02 00:22:19 +0000219/// fillTypeNameTable - If the module has a symbol table, take all global types
220/// and stuff their names into the TypeNames map.
221///
Chris Lattnerb86620e2001-10-29 16:37:48 +0000222static void fillTypeNameTable(const Module *M,
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000223 std::map<const Type *, std::string> &TypeNames) {
Chris Lattner98cf1f52002-11-20 18:36:02 +0000224 if (!M) return;
225 const SymbolTable &ST = M->getSymbolTable();
Reid Spencere7e96712004-05-25 08:53:40 +0000226 SymbolTable::type_const_iterator TI = ST.type_begin();
Chris Lattner23e829a2006-12-06 05:12:21 +0000227 for (; TI != ST.type_end(); ++TI) {
Reid Spencere7e96712004-05-25 08:53:40 +0000228 // As a heuristic, don't insert pointer to primitive types, because
229 // they are used too often to have a single useful name.
230 //
231 const Type *Ty = cast<Type>(TI->second);
232 if (!isa<PointerType>(Ty) ||
Reid Spencer56010e42004-05-26 21:56:09 +0000233 !cast<PointerType>(Ty)->getElementType()->isPrimitiveType() ||
234 isa<OpaqueType>(cast<PointerType>(Ty)->getElementType()))
Reid Spencere7e96712004-05-25 08:53:40 +0000235 TypeNames.insert(std::make_pair(Ty, getLLVMName(TI->first)));
Chris Lattnerb86620e2001-10-29 16:37:48 +0000236 }
237}
238
239
240
Misha Brukmanb1c93172005-04-21 23:48:37 +0000241static void calcTypeName(const Type *Ty,
John Criswellcd116ba2004-06-01 14:54:08 +0000242 std::vector<const Type *> &TypeStack,
243 std::map<const Type *, std::string> &TypeNames,
244 std::string & Result){
245 if (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty)) {
246 Result += Ty->getDescription(); // Base case
247 return;
248 }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000249
250 // Check to see if the type is named.
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000251 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
John Criswellcd116ba2004-06-01 14:54:08 +0000252 if (I != TypeNames.end()) {
253 Result += I->second;
254 return;
255 }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000256
John Criswellcd116ba2004-06-01 14:54:08 +0000257 if (isa<OpaqueType>(Ty)) {
258 Result += "opaque";
259 return;
260 }
Chris Lattnerf14ead92003-10-30 00:22:33 +0000261
Chris Lattnerb86620e2001-10-29 16:37:48 +0000262 // Check to see if the Type is already on the stack...
263 unsigned Slot = 0, CurSize = TypeStack.size();
264 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
265
Misha Brukmanb1c93172005-04-21 23:48:37 +0000266 // This is another base case for the recursion. In this case, we know
Chris Lattnerb86620e2001-10-29 16:37:48 +0000267 // that we have looped back to a type that we have previously visited.
268 // Generate the appropriate upreference to handle this.
John Criswellcd116ba2004-06-01 14:54:08 +0000269 if (Slot < CurSize) {
270 Result += "\\" + utostr(CurSize-Slot); // Here's the upreference
271 return;
272 }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000273
274 TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
Misha Brukmanb1c93172005-04-21 23:48:37 +0000275
Chris Lattner6b727592004-06-17 18:19:28 +0000276 switch (Ty->getTypeID()) {
Chris Lattner91db5822002-03-29 03:44:36 +0000277 case Type::FunctionTyID: {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000278 const FunctionType *FTy = cast<FunctionType>(Ty);
John Criswellcd116ba2004-06-01 14:54:08 +0000279 calcTypeName(FTy->getReturnType(), TypeStack, TypeNames, Result);
280 Result += " (";
Chris Lattnerfa829be2004-02-09 04:14:01 +0000281 for (FunctionType::param_iterator I = FTy->param_begin(),
282 E = FTy->param_end(); I != E; ++I) {
283 if (I != FTy->param_begin())
Chris Lattnerb86620e2001-10-29 16:37:48 +0000284 Result += ", ";
John Criswellcd116ba2004-06-01 14:54:08 +0000285 calcTypeName(*I, TypeStack, TypeNames, Result);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000286 }
Chris Lattnerd816b532002-04-13 20:53:41 +0000287 if (FTy->isVarArg()) {
Chris Lattnerfa829be2004-02-09 04:14:01 +0000288 if (FTy->getNumParams()) Result += ", ";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000289 Result += "...";
290 }
291 Result += ")";
292 break;
293 }
294 case Type::StructTyID: {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000295 const StructType *STy = cast<StructType>(Ty);
John Criswellcd116ba2004-06-01 14:54:08 +0000296 Result += "{ ";
Chris Lattnerac6db752004-02-09 04:37:31 +0000297 for (StructType::element_iterator I = STy->element_begin(),
298 E = STy->element_end(); I != E; ++I) {
299 if (I != STy->element_begin())
Chris Lattnerb86620e2001-10-29 16:37:48 +0000300 Result += ", ";
John Criswellcd116ba2004-06-01 14:54:08 +0000301 calcTypeName(*I, TypeStack, TypeNames, Result);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000302 }
303 Result += " }";
304 break;
305 }
306 case Type::PointerTyID:
Misha Brukmanb1c93172005-04-21 23:48:37 +0000307 calcTypeName(cast<PointerType>(Ty)->getElementType(),
John Criswellcd116ba2004-06-01 14:54:08 +0000308 TypeStack, TypeNames, Result);
309 Result += "*";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000310 break;
311 case Type::ArrayTyID: {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000312 const ArrayType *ATy = cast<ArrayType>(Ty);
John Criswellcd116ba2004-06-01 14:54:08 +0000313 Result += "[" + utostr(ATy->getNumElements()) + " x ";
314 calcTypeName(ATy->getElementType(), TypeStack, TypeNames, Result);
315 Result += "]";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000316 break;
317 }
Brian Gaeke02209042004-08-20 06:00:58 +0000318 case Type::PackedTyID: {
319 const PackedType *PTy = cast<PackedType>(Ty);
320 Result += "<" + utostr(PTy->getNumElements()) + " x ";
321 calcTypeName(PTy->getElementType(), TypeStack, TypeNames, Result);
322 Result += ">";
323 break;
324 }
Chris Lattner15285ab2003-05-14 17:50:47 +0000325 case Type::OpaqueTyID:
John Criswellcd116ba2004-06-01 14:54:08 +0000326 Result += "opaque";
Chris Lattner15285ab2003-05-14 17:50:47 +0000327 break;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000328 default:
John Criswellcd116ba2004-06-01 14:54:08 +0000329 Result += "<unrecognized-type>";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000330 }
331
332 TypeStack.pop_back(); // Remove self from stack...
John Criswellcd116ba2004-06-01 14:54:08 +0000333 return;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000334}
335
336
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000337/// printTypeInt - The internal guts of printing out a type that has a
338/// potentially named portion.
339///
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000340static std::ostream &printTypeInt(std::ostream &Out, const Type *Ty,
341 std::map<const Type *, std::string> &TypeNames) {
Chris Lattnerb86620e2001-10-29 16:37:48 +0000342 // Primitive types always print out their description, regardless of whether
343 // they have been named or not.
344 //
Chris Lattner92d60532003-10-30 00:12:51 +0000345 if (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty))
346 return Out << Ty->getDescription();
Chris Lattnerb86620e2001-10-29 16:37:48 +0000347
348 // Check to see if the type is named.
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000349 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000350 if (I != TypeNames.end()) return Out << I->second;
351
352 // Otherwise we have a type that has not been named but is a derived type.
353 // Carefully recurse the type hierarchy to print out any contained symbolic
354 // names.
355 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000356 std::vector<const Type *> TypeStack;
John Criswellcd116ba2004-06-01 14:54:08 +0000357 std::string TypeName;
358 calcTypeName(Ty, TypeStack, TypeNames, TypeName);
Chris Lattner7f74a562002-01-20 22:54:45 +0000359 TypeNames.insert(std::make_pair(Ty, TypeName));//Cache type name for later use
John Criswellcd116ba2004-06-01 14:54:08 +0000360 return (Out << TypeName);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000361}
362
Chris Lattner34b95182001-10-31 04:33:19 +0000363
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000364/// WriteTypeSymbolic - This attempts to write the specified type as a symbolic
365/// type, iff there is an entry in the modules symbol table for the specified
366/// type or one of it's component types. This is slower than a simple x << Type
367///
Chris Lattner189d19f2003-11-21 20:23:48 +0000368std::ostream &llvm::WriteTypeSymbolic(std::ostream &Out, const Type *Ty,
369 const Module *M) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000370 Out << ' ';
Chris Lattnerb86620e2001-10-29 16:37:48 +0000371
372 // If they want us to print out a type, attempt to make it symbolic if there
373 // is a symbol table in the module...
Chris Lattner98cf1f52002-11-20 18:36:02 +0000374 if (M) {
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000375 std::map<const Type *, std::string> TypeNames;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000376 fillTypeNameTable(M, TypeNames);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000377
Chris Lattner72f866e2001-10-29 16:40:32 +0000378 return printTypeInt(Out, Ty, TypeNames);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000379 } else {
Chris Lattner72f866e2001-10-29 16:40:32 +0000380 return Out << Ty->getDescription();
Chris Lattnerb86620e2001-10-29 16:37:48 +0000381 }
382}
383
Chris Lattner6ed87bd2006-01-23 23:03:36 +0000384// PrintEscapedString - Print each character of the specified string, escaping
385// it if it is not printable or if it is an escape char.
386static void PrintEscapedString(const std::string &Str, std::ostream &Out) {
387 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
388 unsigned char C = Str[i];
389 if (isprint(C) && C != '"' && C != '\\') {
390 Out << C;
391 } else {
392 Out << '\\'
393 << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'))
394 << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
395 }
396 }
397}
398
Reid Spencer812a1be2006-12-04 05:19:18 +0000399static const char * getPredicateText(unsigned predicate) {
400 const char * pred = "unknown";
401 switch (predicate) {
402 case FCmpInst::FCMP_FALSE: pred = "false"; break;
403 case FCmpInst::FCMP_OEQ: pred = "oeq"; break;
404 case FCmpInst::FCMP_OGT: pred = "ogt"; break;
405 case FCmpInst::FCMP_OGE: pred = "oge"; break;
406 case FCmpInst::FCMP_OLT: pred = "olt"; break;
407 case FCmpInst::FCMP_OLE: pred = "ole"; break;
408 case FCmpInst::FCMP_ONE: pred = "one"; break;
409 case FCmpInst::FCMP_ORD: pred = "ord"; break;
410 case FCmpInst::FCMP_UNO: pred = "uno"; break;
411 case FCmpInst::FCMP_UEQ: pred = "ueq"; break;
412 case FCmpInst::FCMP_UGT: pred = "ugt"; break;
413 case FCmpInst::FCMP_UGE: pred = "uge"; break;
414 case FCmpInst::FCMP_ULT: pred = "ult"; break;
415 case FCmpInst::FCMP_ULE: pred = "ule"; break;
416 case FCmpInst::FCMP_UNE: pred = "une"; break;
417 case FCmpInst::FCMP_TRUE: pred = "true"; break;
418 case ICmpInst::ICMP_EQ: pred = "eq"; break;
419 case ICmpInst::ICMP_NE: pred = "ne"; break;
420 case ICmpInst::ICMP_SGT: pred = "sgt"; break;
421 case ICmpInst::ICMP_SGE: pred = "sge"; break;
422 case ICmpInst::ICMP_SLT: pred = "slt"; break;
423 case ICmpInst::ICMP_SLE: pred = "sle"; break;
424 case ICmpInst::ICMP_UGT: pred = "ugt"; break;
425 case ICmpInst::ICMP_UGE: pred = "uge"; break;
426 case ICmpInst::ICMP_ULT: pred = "ult"; break;
427 case ICmpInst::ICMP_ULE: pred = "ule"; break;
428 }
429 return pred;
430}
431
Misha Brukmanb1c93172005-04-21 23:48:37 +0000432/// @brief Internal constant writer.
433static void WriteConstantInt(std::ostream &Out, const Constant *CV,
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000434 bool PrintName,
435 std::map<const Type *, std::string> &TypeTable,
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000436 SlotMachine *Machine) {
Jim Laskey6be3d8e2006-02-27 10:33:53 +0000437 const int IndentSize = 4;
438 static std::string Indent = "\n";
Chris Lattner1e194682002-04-18 18:53:13 +0000439 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
Chris Lattner94e39cb2006-09-28 22:50:29 +0000440 Out << (CB->getValue() ? "true" : "false");
Reid Spencere0fc4df2006-10-20 07:07:24 +0000441 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
442 if (CI->getType()->isSigned())
443 Out << CI->getSExtValue();
444 else
445 Out << CI->getZExtValue();
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),
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000491 PrintName, 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 Lattnerd84bb632002-04-16 21:36:08 +0000495 WriteAsOperandInternal(Out, CA->getOperand(i), PrintName,
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000496 TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000497 }
498 }
499 Out << " ]";
500 }
501 } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000502 Out << '{';
Jim Laskey3bb78742006-02-25 12:27:03 +0000503 unsigned N = CS->getNumOperands();
504 if (N) {
Jim Laskey6be3d8e2006-02-27 10:33:53 +0000505 if (N > 2) {
506 Indent += std::string(IndentSize, ' ');
507 Out << Indent;
508 } else {
509 Out << ' ';
510 }
Chris Lattnerd84bb632002-04-16 21:36:08 +0000511 printTypeInt(Out, CS->getOperand(0)->getType(), TypeTable);
512
513 WriteAsOperandInternal(Out, CS->getOperand(0),
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000514 PrintName, 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
521 WriteAsOperandInternal(Out, CS->getOperand(i),
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000522 PrintName, TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000523 }
Jim Laskey6be3d8e2006-02-27 10:33:53 +0000524 if (N > 2) Indent.resize(Indent.size() - IndentSize);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000525 }
Jim Laskey3bb78742006-02-25 12:27:03 +0000526
Chris Lattnerd84bb632002-04-16 21:36:08 +0000527 Out << " }";
Brian Gaeke02209042004-08-20 06:00:58 +0000528 } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(CV)) {
529 const Type *ETy = CP->getType()->getElementType();
Misha Brukmanb1c93172005-04-21 23:48:37 +0000530 assert(CP->getNumOperands() > 0 &&
Brian Gaeke02209042004-08-20 06:00:58 +0000531 "Number of operands for a PackedConst must be > 0");
532 Out << '<';
533 Out << ' ';
534 printTypeInt(Out, ETy, TypeTable);
535 WriteAsOperandInternal(Out, CP->getOperand(0),
536 PrintName, TypeTable, Machine);
537 for (unsigned i = 1, e = CP->getNumOperands(); i != e; ++i) {
538 Out << ", ";
539 printTypeInt(Out, ETy, TypeTable);
540 WriteAsOperandInternal(Out, CP->getOperand(i), PrintName,
541 TypeTable, Machine);
542 }
543 Out << " >";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000544 } else if (isa<ConstantPointerNull>(CV)) {
545 Out << "null";
546
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000547 } else if (isa<UndefValue>(CV)) {
548 Out << "undef";
549
Chris Lattner3cd8c562002-07-30 18:54:25 +0000550 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Reid Spencer812a1be2006-12-04 05:19:18 +0000551 Out << CE->getOpcodeName();
552 if (CE->isCompare())
553 Out << " " << getPredicateText(CE->getPredicate());
554 Out << " (";
Misha Brukmanb1c93172005-04-21 23:48:37 +0000555
Vikram S. Adveb952b542002-07-14 23:14:45 +0000556 for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
557 printTypeInt(Out, (*OI)->getType(), TypeTable);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000558 WriteAsOperandInternal(Out, *OI, PrintName, TypeTable, Machine);
Vikram S. Adveb952b542002-07-14 23:14:45 +0000559 if (OI+1 != CE->op_end())
Chris Lattner3cd8c562002-07-30 18:54:25 +0000560 Out << ", ";
Vikram S. Adveb952b542002-07-14 23:14:45 +0000561 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000562
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000563 if (CE->isCast()) {
Chris Lattner83b396b2002-08-15 19:37:43 +0000564 Out << " to ";
565 printTypeInt(Out, CE->getType(), TypeTable);
566 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000567
Misha Brukman21bbdb92004-06-04 21:11:51 +0000568 Out << ')';
Chris Lattner83b396b2002-08-15 19:37:43 +0000569
Chris Lattnerd84bb632002-04-16 21:36:08 +0000570 } else {
Vikram S. Adveb952b542002-07-14 23:14:45 +0000571 Out << "<placeholder or erroneous Constant>";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000572 }
573}
574
575
Misha Brukmanc566ca362004-03-02 00:22:19 +0000576/// WriteAsOperand - Write the name of the specified value out to the specified
577/// ostream. This can be useful when you just want to print int %reg126, not
578/// the whole instruction that generated it.
579///
Misha Brukmanb1c93172005-04-21 23:48:37 +0000580static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000581 bool PrintName,
582 std::map<const Type*, std::string> &TypeTable,
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000583 SlotMachine *Machine) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000584 Out << ' ';
Reid Spenceraccd7c72004-07-17 23:47:01 +0000585 if ((PrintName || isa<GlobalValue>(V)) && V->hasName())
Chris Lattner1c343042003-08-22 05:40:38 +0000586 Out << getLLVMName(V->getName());
Reid Spenceraccd7c72004-07-17 23:47:01 +0000587 else {
588 const Constant *CV = dyn_cast<Constant>(V);
Chris Lattnera2d810d2006-01-25 22:26:05 +0000589 if (CV && !isa<GlobalValue>(CV)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000590 WriteConstantInt(Out, CV, PrintName, TypeTable, Machine);
Chris Lattnera2d810d2006-01-25 22:26:05 +0000591 } else if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
592 Out << "asm ";
593 if (IA->hasSideEffects())
594 Out << "sideeffect ";
595 Out << '"';
596 PrintEscapedString(IA->getAsmString(), Out);
597 Out << "\", \"";
598 PrintEscapedString(IA->getConstraintString(), Out);
599 Out << '"';
600 } else {
Chris Lattnerd84bb632002-04-16 21:36:08 +0000601 int Slot;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000602 if (Machine) {
Reid Spencer56010e42004-05-26 21:56:09 +0000603 Slot = Machine->getSlot(V);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000604 } else {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000605 Machine = createSlotMachine(V);
Chris Lattner96749c42006-05-14 18:46:52 +0000606 if (Machine)
Chris Lattner757ee0b2004-06-09 19:41:19 +0000607 Slot = Machine->getSlot(V);
608 else
609 Slot = -1;
Reid Spencer56010e42004-05-26 21:56:09 +0000610 delete Machine;
Chris Lattnerd84bb632002-04-16 21:36:08 +0000611 }
Chris Lattner757ee0b2004-06-09 19:41:19 +0000612 if (Slot != -1)
613 Out << '%' << Slot;
614 else
615 Out << "<badref>";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000616 }
617 }
618}
619
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000620/// WriteAsOperand - Write the name of the specified value out to the specified
621/// ostream. This can be useful when you just want to print int %reg126, not
622/// the whole instruction that generated it.
623///
Chris Lattner189d19f2003-11-21 20:23:48 +0000624std::ostream &llvm::WriteAsOperand(std::ostream &Out, const Value *V,
Misha Brukmanb1c93172005-04-21 23:48:37 +0000625 bool PrintType, bool PrintName,
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000626 const Module *Context) {
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000627 std::map<const Type *, std::string> TypeNames;
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000628 if (Context == 0) Context = getModuleFromVal(V);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000629
Chris Lattner98cf1f52002-11-20 18:36:02 +0000630 if (Context)
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000631 fillTypeNameTable(Context, TypeNames);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000632
633 if (PrintType)
634 printTypeInt(Out, V->getType(), TypeNames);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000635
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000636 WriteAsOperandInternal(Out, V, PrintName, TypeNames, 0);
Chris Lattner5e5abe32001-07-20 19:15:21 +0000637 return Out;
638}
639
Reid Spencer58d30f22004-07-04 11:50:43 +0000640
641/// WriteAsOperand - Write the name of the specified value out to the specified
642/// ostream. This can be useful when you just want to print int %reg126, not
643/// the whole instruction that generated it.
644///
645std::ostream &llvm::WriteAsOperand(std::ostream &Out, const Type *Ty,
Misha Brukmanb1c93172005-04-21 23:48:37 +0000646 bool PrintType, bool PrintName,
Reid Spencer58d30f22004-07-04 11:50:43 +0000647 const Module *Context) {
648 std::map<const Type *, std::string> TypeNames;
649 assert(Context != 0 && "Can't write types as operand without module context");
650
651 fillTypeNameTable(Context, TypeNames);
652
Reid Spencer58d30f22004-07-04 11:50:43 +0000653 printTypeInt(Out, Ty, TypeNames);
654
Chris Lattnera9f0a112006-12-06 05:50:41 +0000655 return Out << ' ' << Ty->getDescription();
Reid Spencer58d30f22004-07-04 11:50:43 +0000656}
657
Chris Lattner189d19f2003-11-21 20:23:48 +0000658namespace llvm {
Chris Lattner2e9fee42001-07-12 23:35:26 +0000659
Chris Lattnerfee714f2001-09-07 16:36:04 +0000660class AssemblyWriter {
Misha Brukmana6619a92004-06-21 21:53:56 +0000661 std::ostream &Out;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000662 SlotMachine &Machine;
Chris Lattner7bfee412001-10-29 16:05:51 +0000663 const Module *TheModule;
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000664 std::map<const Type *, std::string> TypeNames;
Chris Lattner8339f7d2003-10-30 23:41:03 +0000665 AssemblyAnnotationWriter *AnnotationWriter;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000666public:
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000667 inline AssemblyWriter(std::ostream &o, SlotMachine &Mac, const Module *M,
Chris Lattner8339f7d2003-10-30 23:41:03 +0000668 AssemblyAnnotationWriter *AAW)
Misha Brukmana6619a92004-06-21 21:53:56 +0000669 : Out(o), Machine(Mac), TheModule(M), AnnotationWriter(AAW) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000670
671 // If the module has a symbol table, take all global types and stuff their
672 // names into the TypeNames map.
673 //
Chris Lattnerb86620e2001-10-29 16:37:48 +0000674 fillTypeNameTable(M, TypeNames);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000675 }
676
Chris Lattner7bfee412001-10-29 16:05:51 +0000677 inline void write(const Module *M) { printModule(M); }
678 inline void write(const GlobalVariable *G) { printGlobal(G); }
Chris Lattner57698e22002-03-26 18:01:55 +0000679 inline void write(const Function *F) { printFunction(F); }
Chris Lattner7bfee412001-10-29 16:05:51 +0000680 inline void write(const BasicBlock *BB) { printBasicBlock(BB); }
Chris Lattner113f4f42002-06-25 16:13:24 +0000681 inline void write(const Instruction *I) { printInstruction(*I); }
Chris Lattner3462ae32001-12-03 22:26:30 +0000682 inline void write(const Constant *CPV) { printConstant(CPV); }
Chris Lattner7db79582001-11-07 04:21:57 +0000683 inline void write(const Type *Ty) { printType(Ty); }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000684
Chris Lattner1e194682002-04-18 18:53:13 +0000685 void writeOperand(const Value *Op, bool PrintType, bool PrintName = true);
686
Misha Brukman4685e262004-04-28 15:31:21 +0000687 const Module* getModule() { return TheModule; }
688
Misha Brukmand92f54a2004-11-15 19:30:05 +0000689private:
Chris Lattner7bfee412001-10-29 16:05:51 +0000690 void printModule(const Module *M);
691 void printSymbolTable(const SymbolTable &ST);
Chris Lattner3462ae32001-12-03 22:26:30 +0000692 void printConstant(const Constant *CPV);
Chris Lattner7bfee412001-10-29 16:05:51 +0000693 void printGlobal(const GlobalVariable *GV);
Chris Lattner57698e22002-03-26 18:01:55 +0000694 void printFunction(const Function *F);
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000695 void printArgument(const Argument *FA);
Chris Lattner7bfee412001-10-29 16:05:51 +0000696 void printBasicBlock(const BasicBlock *BB);
Chris Lattner113f4f42002-06-25 16:13:24 +0000697 void printInstruction(const Instruction &I);
Chris Lattnerd816b532002-04-13 20:53:41 +0000698
699 // printType - Go to extreme measures to attempt to print out a short,
700 // symbolic version of a type name.
701 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000702 std::ostream &printType(const Type *Ty) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000703 return printTypeInt(Out, Ty, TypeNames);
Chris Lattnerd816b532002-04-13 20:53:41 +0000704 }
705
706 // printTypeAtLeastOneLevel - Print out one level of the possibly complex type
707 // without considering any symbolic types that we may have equal to it.
708 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000709 std::ostream &printTypeAtLeastOneLevel(const Type *Ty);
Chris Lattner7bfee412001-10-29 16:05:51 +0000710
Chris Lattner862e3382001-10-13 06:42:36 +0000711 // printInfoComment - Print a little comment after the instruction indicating
712 // which slot it occupies.
Chris Lattner113f4f42002-06-25 16:13:24 +0000713 void printInfoComment(const Value &V);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000714};
Reid Spencerf43ac622004-05-27 22:04:46 +0000715} // end of llvm namespace
Chris Lattner2f7c9632001-06-06 20:29:01 +0000716
Misha Brukmanc566ca362004-03-02 00:22:19 +0000717/// printTypeAtLeastOneLevel - Print out one level of the possibly complex type
718/// without considering any symbolic types that we may have equal to it.
719///
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000720std::ostream &AssemblyWriter::printTypeAtLeastOneLevel(const Type *Ty) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000721 if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000722 printType(FTy->getReturnType()) << " (";
Chris Lattnerfa829be2004-02-09 04:14:01 +0000723 for (FunctionType::param_iterator I = FTy->param_begin(),
724 E = FTy->param_end(); I != E; ++I) {
725 if (I != FTy->param_begin())
Misha Brukmana6619a92004-06-21 21:53:56 +0000726 Out << ", ";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000727 printType(*I);
Chris Lattnerd816b532002-04-13 20:53:41 +0000728 }
729 if (FTy->isVarArg()) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000730 if (FTy->getNumParams()) Out << ", ";
731 Out << "...";
Chris Lattnerd816b532002-04-13 20:53:41 +0000732 }
Misha Brukmana6619a92004-06-21 21:53:56 +0000733 Out << ')';
Chris Lattner113f4f42002-06-25 16:13:24 +0000734 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000735 Out << "{ ";
Chris Lattnerac6db752004-02-09 04:37:31 +0000736 for (StructType::element_iterator I = STy->element_begin(),
737 E = STy->element_end(); I != E; ++I) {
738 if (I != STy->element_begin())
Misha Brukmana6619a92004-06-21 21:53:56 +0000739 Out << ", ";
Chris Lattnerd816b532002-04-13 20:53:41 +0000740 printType(*I);
741 }
Misha Brukmana6619a92004-06-21 21:53:56 +0000742 Out << " }";
Chris Lattner113f4f42002-06-25 16:13:24 +0000743 } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000744 printType(PTy->getElementType()) << '*';
Chris Lattner113f4f42002-06-25 16:13:24 +0000745 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000746 Out << '[' << ATy->getNumElements() << " x ";
Misha Brukman21bbdb92004-06-04 21:11:51 +0000747 printType(ATy->getElementType()) << ']';
Reid Spencere203d352004-08-20 15:37:30 +0000748 } else if (const PackedType *PTy = dyn_cast<PackedType>(Ty)) {
749 Out << '<' << PTy->getNumElements() << " x ";
750 printType(PTy->getElementType()) << '>';
751 }
Reid Spencerde46e482006-11-02 20:25:50 +0000752 else if (isa<OpaqueType>(Ty)) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000753 Out << "opaque";
Chris Lattnerd816b532002-04-13 20:53:41 +0000754 } else {
Vikram S. Adveb952b542002-07-14 23:14:45 +0000755 if (!Ty->isPrimitiveType())
Misha Brukmana6619a92004-06-21 21:53:56 +0000756 Out << "<unknown derived type>";
Chris Lattnerd816b532002-04-13 20:53:41 +0000757 printType(Ty);
758 }
Misha Brukmana6619a92004-06-21 21:53:56 +0000759 return Out;
Chris Lattnerd816b532002-04-13 20:53:41 +0000760}
761
762
Misha Brukmanb1c93172005-04-21 23:48:37 +0000763void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType,
Reid Spencer56010e42004-05-26 21:56:09 +0000764 bool PrintName) {
Chris Lattner08f7d0c2005-02-24 16:58:29 +0000765 if (Operand != 0) {
766 if (PrintType) { Out << ' '; printType(Operand->getType()); }
767 WriteAsOperandInternal(Out, Operand, PrintName, TypeNames, &Machine);
768 } else {
769 Out << "<null operand!>";
770 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000771}
772
Chris Lattner2f7c9632001-06-06 20:29:01 +0000773
Chris Lattner7bfee412001-10-29 16:05:51 +0000774void AssemblyWriter::printModule(const Module *M) {
Chris Lattner4d8689e2005-03-02 23:12:40 +0000775 if (!M->getModuleIdentifier().empty() &&
Misha Brukmanb1c93172005-04-21 23:48:37 +0000776 // Don't print the ID if it will start a new line (which would
Chris Lattner4d8689e2005-03-02 23:12:40 +0000777 // require a comment char before it).
778 M->getModuleIdentifier().find('\n') == std::string::npos)
779 Out << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
780
Owen Andersone2237542006-10-18 02:21:12 +0000781 if (!M->getDataLayout().empty())
Chris Lattner04897162006-10-22 06:06:56 +0000782 Out << "target datalayout = \"" << M->getDataLayout() << "\"\n";
Owen Andersone2237542006-10-18 02:21:12 +0000783
Chris Lattner8068e0c2003-08-24 13:48:48 +0000784 switch (M->getEndianness()) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000785 case Module::LittleEndian: Out << "target endian = little\n"; break;
786 case Module::BigEndian: Out << "target endian = big\n"; break;
Chris Lattner8068e0c2003-08-24 13:48:48 +0000787 case Module::AnyEndianness: break;
788 }
789 switch (M->getPointerSize()) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000790 case Module::Pointer32: Out << "target pointersize = 32\n"; break;
791 case Module::Pointer64: Out << "target pointersize = 64\n"; break;
Chris Lattner8068e0c2003-08-24 13:48:48 +0000792 case Module::AnyPointerSize: break;
793 }
Reid Spencer48f98c82004-07-25 21:44:54 +0000794 if (!M->getTargetTriple().empty())
Reid Spencerffec7df2004-07-25 21:29:43 +0000795 Out << "target triple = \"" << M->getTargetTriple() << "\"\n";
Misha Brukmanb1c93172005-04-21 23:48:37 +0000796
Chris Lattnereef2fe72006-01-24 04:13:11 +0000797 if (!M->getModuleInlineAsm().empty()) {
Chris Lattnerefaf35d2006-01-24 00:45:30 +0000798 // Split the string into lines, to make it easier to read the .ll file.
Chris Lattnereef2fe72006-01-24 04:13:11 +0000799 std::string Asm = M->getModuleInlineAsm();
Chris Lattnerefaf35d2006-01-24 00:45:30 +0000800 size_t CurPos = 0;
801 size_t NewLine = Asm.find_first_of('\n', CurPos);
802 while (NewLine != std::string::npos) {
803 // We found a newline, print the portion of the asm string from the
804 // last newline up to this newline.
805 Out << "module asm \"";
806 PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.begin()+NewLine),
807 Out);
808 Out << "\"\n";
809 CurPos = NewLine+1;
810 NewLine = Asm.find_first_of('\n', CurPos);
811 }
Chris Lattner3acaf5c2006-01-24 00:40:17 +0000812 Out << "module asm \"";
Chris Lattnerefaf35d2006-01-24 00:45:30 +0000813 PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.end()), Out);
Chris Lattner6ed87bd2006-01-23 23:03:36 +0000814 Out << "\"\n";
815 }
816
Chris Lattner2cdd49d2004-09-14 05:06:58 +0000817 // Loop over the dependent libraries and emit them.
Chris Lattner730cfe42004-09-14 04:51:44 +0000818 Module::lib_iterator LI = M->lib_begin();
819 Module::lib_iterator LE = M->lib_end();
Reid Spencer48f98c82004-07-25 21:44:54 +0000820 if (LI != LE) {
Chris Lattner730cfe42004-09-14 04:51:44 +0000821 Out << "deplibs = [ ";
822 while (LI != LE) {
Chris Lattner2cdd49d2004-09-14 05:06:58 +0000823 Out << '"' << *LI << '"';
Reid Spencerffec7df2004-07-25 21:29:43 +0000824 ++LI;
Chris Lattner730cfe42004-09-14 04:51:44 +0000825 if (LI != LE)
826 Out << ", ";
Reid Spencerffec7df2004-07-25 21:29:43 +0000827 }
828 Out << " ]\n";
Reid Spencercc5ff642004-07-25 18:08:18 +0000829 }
Reid Spencerb9e08772004-09-13 23:44:23 +0000830
Chris Lattner2cdd49d2004-09-14 05:06:58 +0000831 // Loop over the symbol table, emitting all named constants.
Chris Lattner98cf1f52002-11-20 18:36:02 +0000832 printSymbolTable(M->getSymbolTable());
Misha Brukmanb1c93172005-04-21 23:48:37 +0000833
Chris Lattner54932b02006-12-06 04:41:52 +0000834 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
835 I != E; ++I)
Chris Lattner113f4f42002-06-25 16:13:24 +0000836 printGlobal(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000837
Misha Brukmana6619a92004-06-21 21:53:56 +0000838 Out << "\nimplementation ; Functions:\n";
Misha Brukmanb1c93172005-04-21 23:48:37 +0000839
Chris Lattner2cdd49d2004-09-14 05:06:58 +0000840 // Output all of the functions.
Chris Lattner113f4f42002-06-25 16:13:24 +0000841 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
842 printFunction(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000843}
844
Chris Lattner7bfee412001-10-29 16:05:51 +0000845void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000846 if (GV->hasName()) Out << getLLVMName(GV->getName()) << " = ";
Chris Lattner37798642001-09-18 04:01:05 +0000847
Misha Brukmanb1c93172005-04-21 23:48:37 +0000848 if (!GV->hasInitializer())
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000849 switch (GV->getLinkage()) {
850 case GlobalValue::DLLImportLinkage: Out << "dllimport "; break;
851 case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break;
852 default: Out << "external "; break;
853 }
Chris Lattner379a8d22003-04-16 20:28:45 +0000854 else
855 switch (GV->getLinkage()) {
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000856 case GlobalValue::InternalLinkage: Out << "internal "; break;
857 case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
858 case GlobalValue::WeakLinkage: Out << "weak "; break;
859 case GlobalValue::AppendingLinkage: Out << "appending "; break;
860 case GlobalValue::DLLImportLinkage: Out << "dllimport "; break;
861 case GlobalValue::DLLExportLinkage: Out << "dllexport "; break;
862 case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break;
863 case GlobalValue::ExternalLinkage: break;
Misha Brukman70457632004-11-14 21:04:34 +0000864 case GlobalValue::GhostLinkage:
Bill Wendlingdfc91892006-11-28 02:09:03 +0000865 llvm_cerr << "GhostLinkage not allowed in AsmWriter!\n";
Misha Brukman70457632004-11-14 21:04:34 +0000866 abort();
Chris Lattner379a8d22003-04-16 20:28:45 +0000867 }
Chris Lattner37798642001-09-18 04:01:05 +0000868
Misha Brukmana6619a92004-06-21 21:53:56 +0000869 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattner2413b162001-12-04 00:03:30 +0000870 printType(GV->getType()->getElementType());
Chris Lattner37798642001-09-18 04:01:05 +0000871
Reid Spenceraccd7c72004-07-17 23:47:01 +0000872 if (GV->hasInitializer()) {
873 Constant* C = cast<Constant>(GV->getInitializer());
874 assert(C && "GlobalVar initializer isn't constant?");
Reid Spencerc9c90cf2004-07-18 01:04:19 +0000875 writeOperand(GV->getInitializer(), false, isa<GlobalValue>(C));
Reid Spenceraccd7c72004-07-17 23:47:01 +0000876 }
Chris Lattnerf8a974d2005-11-06 06:48:53 +0000877
Chris Lattner4b96c542005-11-12 00:10:19 +0000878 if (GV->hasSection())
879 Out << ", section \"" << GV->getSection() << '"';
880 if (GV->getAlignment())
Chris Lattnerf8a974d2005-11-06 06:48:53 +0000881 Out << ", align " << GV->getAlignment();
Chris Lattner4b96c542005-11-12 00:10:19 +0000882
Chris Lattner113f4f42002-06-25 16:13:24 +0000883 printInfoComment(*GV);
Misha Brukmana6619a92004-06-21 21:53:56 +0000884 Out << "\n";
Chris Lattnerda975502001-09-10 07:58:01 +0000885}
886
Chris Lattnerfee714f2001-09-07 16:36:04 +0000887
Reid Spencere7e96712004-05-25 08:53:40 +0000888// printSymbolTable - Run through symbol table looking for constants
889// and types. Emit their declarations.
Chris Lattner7bfee412001-10-29 16:05:51 +0000890void AssemblyWriter::printSymbolTable(const SymbolTable &ST) {
Reid Spencere7e96712004-05-25 08:53:40 +0000891
892 // Print the types.
893 for (SymbolTable::type_const_iterator TI = ST.type_begin();
Chris Lattner23e829a2006-12-06 05:12:21 +0000894 TI != ST.type_end(); ++TI) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000895 Out << "\t" << getLLVMName(TI->first) << " = type ";
Reid Spencere7e96712004-05-25 08:53:40 +0000896
897 // Make sure we print out at least one level of the type structure, so
898 // that we do not get %FILE = type %FILE
899 //
900 printTypeAtLeastOneLevel(TI->second) << "\n";
901 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000902
Reid Spencere7e96712004-05-25 08:53:40 +0000903 // Print the constants, in type plane order.
904 for (SymbolTable::plane_const_iterator PI = ST.plane_begin();
Chris Lattner23e829a2006-12-06 05:12:21 +0000905 PI != ST.plane_end(); ++PI) {
Reid Spencere7e96712004-05-25 08:53:40 +0000906 SymbolTable::value_const_iterator VI = ST.value_begin(PI->first);
907 SymbolTable::value_const_iterator VE = ST.value_end(PI->first);
908
909 for (; VI != VE; ++VI) {
Reid Spenceraccd7c72004-07-17 23:47:01 +0000910 const Value* V = VI->second;
911 const Constant *CPV = dyn_cast<Constant>(V) ;
912 if (CPV && !isa<GlobalValue>(V)) {
Reid Spencer56010e42004-05-26 21:56:09 +0000913 printConstant(CPV);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000914 }
915 }
Chris Lattnera7620d92001-07-15 06:35:59 +0000916 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000917}
918
919
Misha Brukmanc566ca362004-03-02 00:22:19 +0000920/// printConstant - Print out a constant pool entry...
921///
Chris Lattner3462ae32001-12-03 22:26:30 +0000922void AssemblyWriter::printConstant(const Constant *CPV) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000923 // Don't print out unnamed constants, they will be inlined
924 if (!CPV->hasName()) return;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000925
Chris Lattneree998be2001-07-26 16:29:38 +0000926 // Print out name...
Misha Brukmana6619a92004-06-21 21:53:56 +0000927 Out << "\t" << getLLVMName(CPV->getName()) << " =";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000928
929 // Write the value out now...
Chris Lattnerd84bb632002-04-16 21:36:08 +0000930 writeOperand(CPV, true, false);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000931
Chris Lattner113f4f42002-06-25 16:13:24 +0000932 printInfoComment(*CPV);
Misha Brukmana6619a92004-06-21 21:53:56 +0000933 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000934}
935
Misha Brukmanc566ca362004-03-02 00:22:19 +0000936/// printFunction - Print all aspects of a function.
937///
Chris Lattner113f4f42002-06-25 16:13:24 +0000938void AssemblyWriter::printFunction(const Function *F) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000939 // Print out the return type and name...
Misha Brukmana6619a92004-06-21 21:53:56 +0000940 Out << "\n";
Chris Lattner379a8d22003-04-16 20:28:45 +0000941
Chris Lattner97e36f22004-12-05 06:44:09 +0000942 // Ensure that no local symbols conflict with global symbols.
943 const_cast<Function*>(F)->renameLocalSymbols();
944
Misha Brukmana6619a92004-06-21 21:53:56 +0000945 if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +0000946
Chris Lattner379a8d22003-04-16 20:28:45 +0000947 if (F->isExternal())
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000948 switch (F->getLinkage()) {
949 case GlobalValue::DLLImportLinkage: Out << "declare dllimport "; break;
950 case GlobalValue::ExternalWeakLinkage: Out << "declare extern_weak "; break;
951 default: Out << "declare ";
952 }
Chris Lattner379a8d22003-04-16 20:28:45 +0000953 else
954 switch (F->getLinkage()) {
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000955 case GlobalValue::InternalLinkage: Out << "internal "; break;
956 case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
957 case GlobalValue::WeakLinkage: Out << "weak "; break;
958 case GlobalValue::AppendingLinkage: Out << "appending "; break;
959 case GlobalValue::DLLImportLinkage: Out << "dllimport "; break;
960 case GlobalValue::DLLExportLinkage: Out << "dllexport "; break;
961 case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break;
Chris Lattner379a8d22003-04-16 20:28:45 +0000962 case GlobalValue::ExternalLinkage: break;
Misha Brukman70457632004-11-14 21:04:34 +0000963 case GlobalValue::GhostLinkage:
Bill Wendlingdfc91892006-11-28 02:09:03 +0000964 llvm_cerr << "GhostLinkage not allowed in AsmWriter!\n";
Misha Brukman70457632004-11-14 21:04:34 +0000965 abort();
Chris Lattner379a8d22003-04-16 20:28:45 +0000966 }
967
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000968 // Print the calling convention.
969 switch (F->getCallingConv()) {
970 case CallingConv::C: break; // default
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +0000971 case CallingConv::CSRet: Out << "csretcc "; break;
972 case CallingConv::Fast: Out << "fastcc "; break;
973 case CallingConv::Cold: Out << "coldcc "; break;
974 case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break;
975 case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break;
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000976 default: Out << "cc" << F->getCallingConv() << " "; break;
977 }
978
Misha Brukman21bbdb92004-06-04 21:11:51 +0000979 printType(F->getReturnType()) << ' ';
Chris Lattner5b337482003-10-18 05:57:43 +0000980 if (!F->getName().empty())
Misha Brukmana6619a92004-06-21 21:53:56 +0000981 Out << getLLVMName(F->getName());
Chris Lattner5b337482003-10-18 05:57:43 +0000982 else
Misha Brukmana6619a92004-06-21 21:53:56 +0000983 Out << "\"\"";
984 Out << '(';
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000985 Machine.incorporateFunction(F);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000986
Chris Lattner7bfee412001-10-29 16:05:51 +0000987 // Loop over the arguments, printing them...
Chris Lattner113f4f42002-06-25 16:13:24 +0000988 const FunctionType *FT = F->getFunctionType();
Chris Lattnerfee714f2001-09-07 16:36:04 +0000989
Chris Lattner54932b02006-12-06 04:41:52 +0000990 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
991 I != E; ++I)
Chris Lattner149376d2002-10-13 20:57:00 +0000992 printArgument(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000993
994 // Finish printing arguments...
Chris Lattner113f4f42002-06-25 16:13:24 +0000995 if (FT->isVarArg()) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000996 if (FT->getNumParams()) Out << ", ";
997 Out << "..."; // Output varargs portion of signature!
Chris Lattnerfee714f2001-09-07 16:36:04 +0000998 }
Misha Brukmana6619a92004-06-21 21:53:56 +0000999 Out << ')';
Chris Lattnerfee714f2001-09-07 16:36:04 +00001000
Chris Lattner4b96c542005-11-12 00:10:19 +00001001 if (F->hasSection())
1002 Out << " section \"" << F->getSection() << '"';
Chris Lattnerf8a974d2005-11-06 06:48:53 +00001003 if (F->getAlignment())
1004 Out << " align " << F->getAlignment();
Chris Lattner4b96c542005-11-12 00:10:19 +00001005
Chris Lattner113f4f42002-06-25 16:13:24 +00001006 if (F->isExternal()) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001007 Out << "\n";
Chris Lattnerb2f02e52002-05-06 03:00:40 +00001008 } else {
Misha Brukmana6619a92004-06-21 21:53:56 +00001009 Out << " {";
Misha Brukmanb1c93172005-04-21 23:48:37 +00001010
Chris Lattner6915f8f2002-04-07 22:49:37 +00001011 // Output all of its basic blocks... for the function
Chris Lattner113f4f42002-06-25 16:13:24 +00001012 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
1013 printBasicBlock(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +00001014
Misha Brukmana6619a92004-06-21 21:53:56 +00001015 Out << "}\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +00001016 }
1017
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001018 Machine.purgeFunction();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001019}
1020
Misha Brukmanc566ca362004-03-02 00:22:19 +00001021/// printArgument - This member is called for every argument that is passed into
1022/// the function. Simply print it out
1023///
Chris Lattner2e9fa6d2002-04-09 19:48:49 +00001024void AssemblyWriter::printArgument(const Argument *Arg) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001025 // Insert commas as we go... the first arg doesn't get a comma
Chris Lattnerfb972ad2005-03-15 05:03:36 +00001026 if (Arg != Arg->getParent()->arg_begin()) Out << ", ";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001027
1028 // Output type...
Chris Lattner7bfee412001-10-29 16:05:51 +00001029 printType(Arg->getType());
Misha Brukmanb1c93172005-04-21 23:48:37 +00001030
Chris Lattner2f7c9632001-06-06 20:29:01 +00001031 // Output name, if available...
1032 if (Arg->hasName())
Misha Brukmana6619a92004-06-21 21:53:56 +00001033 Out << ' ' << getLLVMName(Arg->getName());
Chris Lattner2f7c9632001-06-06 20:29:01 +00001034}
1035
Misha Brukmanc566ca362004-03-02 00:22:19 +00001036/// printBasicBlock - This member is called for each basic block in a method.
1037///
Chris Lattner7bfee412001-10-29 16:05:51 +00001038void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001039 if (BB->hasName()) { // Print out the label if it exists...
Alkis Evlogimenos991d6ad2004-12-10 05:41:10 +00001040 Out << "\n" << getLLVMName(BB->getName(), false) << ':';
Chris Lattner408dbdb2002-05-14 16:02:05 +00001041 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Misha Brukmana6619a92004-06-21 21:53:56 +00001042 Out << "\n; <label>:";
Chris Lattner757ee0b2004-06-09 19:41:19 +00001043 int Slot = Machine.getSlot(BB);
1044 if (Slot != -1)
Misha Brukmana6619a92004-06-21 21:53:56 +00001045 Out << Slot;
Chris Lattner757ee0b2004-06-09 19:41:19 +00001046 else
Misha Brukmana6619a92004-06-21 21:53:56 +00001047 Out << "<badref>";
Chris Lattner58185f22002-10-02 19:38:55 +00001048 }
Chris Lattner2447ef52003-11-20 00:09:43 +00001049
1050 if (BB->getParent() == 0)
Misha Brukmana6619a92004-06-21 21:53:56 +00001051 Out << "\t\t; Error: Block without parent!";
Chris Lattner2447ef52003-11-20 00:09:43 +00001052 else {
1053 if (BB != &BB->getParent()->front()) { // Not the entry block?
1054 // Output predecessors for the block...
Misha Brukmana6619a92004-06-21 21:53:56 +00001055 Out << "\t\t;";
Chris Lattner2447ef52003-11-20 00:09:43 +00001056 pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
Misha Brukmanb1c93172005-04-21 23:48:37 +00001057
Chris Lattner2447ef52003-11-20 00:09:43 +00001058 if (PI == PE) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001059 Out << " No predecessors!";
Chris Lattner2447ef52003-11-20 00:09:43 +00001060 } else {
Misha Brukmana6619a92004-06-21 21:53:56 +00001061 Out << " preds =";
Chris Lattner00211f12003-11-16 22:59:57 +00001062 writeOperand(*PI, false, true);
Chris Lattner2447ef52003-11-20 00:09:43 +00001063 for (++PI; PI != PE; ++PI) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001064 Out << ',';
Chris Lattner2447ef52003-11-20 00:09:43 +00001065 writeOperand(*PI, false, true);
1066 }
Chris Lattner00211f12003-11-16 22:59:57 +00001067 }
Chris Lattner58185f22002-10-02 19:38:55 +00001068 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001069 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001070
Misha Brukmana6619a92004-06-21 21:53:56 +00001071 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001072
Misha Brukmana6619a92004-06-21 21:53:56 +00001073 if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001074
Chris Lattnerfee714f2001-09-07 16:36:04 +00001075 // Output all of the instructions in the basic block...
Chris Lattner113f4f42002-06-25 16:13:24 +00001076 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
1077 printInstruction(*I);
Chris Lattner96cdd272004-03-08 18:51:45 +00001078
Misha Brukmana6619a92004-06-21 21:53:56 +00001079 if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001080}
1081
Chris Lattner862e3382001-10-13 06:42:36 +00001082
Misha Brukmanc566ca362004-03-02 00:22:19 +00001083/// printInfoComment - Print a little comment after the instruction indicating
1084/// which slot it occupies.
1085///
Chris Lattner113f4f42002-06-25 16:13:24 +00001086void AssemblyWriter::printInfoComment(const Value &V) {
1087 if (V.getType() != Type::VoidTy) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001088 Out << "\t\t; <";
Misha Brukman21bbdb92004-06-04 21:11:51 +00001089 printType(V.getType()) << '>';
Chris Lattner862e3382001-10-13 06:42:36 +00001090
Chris Lattner113f4f42002-06-25 16:13:24 +00001091 if (!V.hasName()) {
Chris Lattner757ee0b2004-06-09 19:41:19 +00001092 int SlotNum = Machine.getSlot(&V);
1093 if (SlotNum == -1)
Misha Brukmana6619a92004-06-21 21:53:56 +00001094 Out << ":<badref>";
Reid Spencer8beac692004-06-09 15:26:53 +00001095 else
Misha Brukmana6619a92004-06-21 21:53:56 +00001096 Out << ':' << SlotNum; // Print out the def slot taken.
Chris Lattner862e3382001-10-13 06:42:36 +00001097 }
Chris Lattnerb6c21db2005-02-01 01:24:01 +00001098 Out << " [#uses=" << V.getNumUses() << ']'; // Output # uses
Chris Lattner862e3382001-10-13 06:42:36 +00001099 }
1100}
1101
Reid Spencere7141c82006-08-28 01:02:49 +00001102// This member is called for each Instruction in a function..
Chris Lattner113f4f42002-06-25 16:13:24 +00001103void AssemblyWriter::printInstruction(const Instruction &I) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001104 if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001105
Misha Brukmana6619a92004-06-21 21:53:56 +00001106 Out << "\t";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001107
1108 // Print out name if it exists...
Chris Lattner113f4f42002-06-25 16:13:24 +00001109 if (I.hasName())
Misha Brukmana6619a92004-06-21 21:53:56 +00001110 Out << getLLVMName(I.getName()) << " = ";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001111
Chris Lattner06038452005-05-06 05:51:46 +00001112 // If this is a volatile load or store, print out the volatile marker.
Chris Lattner504f9242003-09-08 17:45:59 +00001113 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
Chris Lattner06038452005-05-06 05:51:46 +00001114 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile())) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001115 Out << "volatile ";
Chris Lattner06038452005-05-06 05:51:46 +00001116 } else if (isa<CallInst>(I) && cast<CallInst>(I).isTailCall()) {
1117 // If this is a call, check if it's a tail call.
1118 Out << "tail ";
1119 }
Chris Lattner504f9242003-09-08 17:45:59 +00001120
Chris Lattner2f7c9632001-06-06 20:29:01 +00001121 // Print out the opcode...
Misha Brukmana6619a92004-06-21 21:53:56 +00001122 Out << I.getOpcodeName();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001123
Reid Spencer45e52392006-12-03 06:27:29 +00001124 // Print out the compare instruction predicates
1125 if (const FCmpInst *FCI = dyn_cast<FCmpInst>(&I)) {
Reid Spencer812a1be2006-12-04 05:19:18 +00001126 Out << " " << getPredicateText(FCI->getPredicate());
Reid Spencer45e52392006-12-03 06:27:29 +00001127 } else if (const ICmpInst *ICI = dyn_cast<ICmpInst>(&I)) {
Reid Spencer812a1be2006-12-04 05:19:18 +00001128 Out << " " << getPredicateText(ICI->getPredicate());
Reid Spencer45e52392006-12-03 06:27:29 +00001129 }
1130
Chris Lattner2f7c9632001-06-06 20:29:01 +00001131 // Print out the type of the operands...
Chris Lattner113f4f42002-06-25 16:13:24 +00001132 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001133
1134 // Special case conditional branches to swizzle the condition out to the front
Chris Lattner113f4f42002-06-25 16:13:24 +00001135 if (isa<BranchInst>(I) && I.getNumOperands() > 1) {
1136 writeOperand(I.getOperand(2), true);
Misha Brukmana6619a92004-06-21 21:53:56 +00001137 Out << ',';
Chris Lattner2f7c9632001-06-06 20:29:01 +00001138 writeOperand(Operand, true);
Misha Brukmana6619a92004-06-21 21:53:56 +00001139 Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001140 writeOperand(I.getOperand(1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001141
Chris Lattner8d48df22002-04-13 18:34:38 +00001142 } else if (isa<SwitchInst>(I)) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001143 // Special case switch statement to get formatting nice and correct...
Misha Brukmana6619a92004-06-21 21:53:56 +00001144 writeOperand(Operand , true); Out << ',';
1145 writeOperand(I.getOperand(1), true); Out << " [";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001146
Chris Lattner113f4f42002-06-25 16:13:24 +00001147 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001148 Out << "\n\t\t";
1149 writeOperand(I.getOperand(op ), true); Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001150 writeOperand(I.getOperand(op+1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001151 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001152 Out << "\n\t]";
Chris Lattnerda558102001-10-02 03:41:24 +00001153 } else if (isa<PHINode>(I)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001154 Out << ' ';
Chris Lattner113f4f42002-06-25 16:13:24 +00001155 printType(I.getType());
Misha Brukmana6619a92004-06-21 21:53:56 +00001156 Out << ' ';
Chris Lattner2f7c9632001-06-06 20:29:01 +00001157
Chris Lattner113f4f42002-06-25 16:13:24 +00001158 for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001159 if (op) Out << ", ";
Misha Brukmanb1c93172005-04-21 23:48:37 +00001160 Out << '[';
Misha Brukmana6619a92004-06-21 21:53:56 +00001161 writeOperand(I.getOperand(op ), false); Out << ',';
1162 writeOperand(I.getOperand(op+1), false); Out << " ]";
Chris Lattner931ef3b2001-06-11 15:04:20 +00001163 }
Chris Lattner862e3382001-10-13 06:42:36 +00001164 } else if (isa<ReturnInst>(I) && !Operand) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001165 Out << " void";
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001166 } else if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
1167 // Print the calling convention being used.
1168 switch (CI->getCallingConv()) {
1169 case CallingConv::C: break; // default
Chris Lattner29d20852006-05-19 21:58:52 +00001170 case CallingConv::CSRet: Out << " csretcc"; break;
1171 case CallingConv::Fast: Out << " fastcc"; break;
1172 case CallingConv::Cold: Out << " coldcc"; break;
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +00001173 case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break;
1174 case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break;
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001175 default: Out << " cc" << CI->getCallingConv(); break;
1176 }
1177
Chris Lattner463d6a52003-08-05 15:34:45 +00001178 const PointerType *PTy = cast<PointerType>(Operand->getType());
1179 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1180 const Type *RetTy = FTy->getReturnType();
Chris Lattner2f2d9472001-11-06 21:28:12 +00001181
Chris Lattner463d6a52003-08-05 15:34:45 +00001182 // If possible, print out the short form of the call instruction. We can
Chris Lattner6915f8f2002-04-07 22:49:37 +00001183 // only do this if the first argument is a pointer to a nonvararg function,
Chris Lattner463d6a52003-08-05 15:34:45 +00001184 // and if the return type is not a pointer to a function.
Chris Lattner2f2d9472001-11-06 21:28:12 +00001185 //
Chris Lattner463d6a52003-08-05 15:34:45 +00001186 if (!FTy->isVarArg() &&
Misha Brukmanb1c93172005-04-21 23:48:37 +00001187 (!isa<PointerType>(RetTy) ||
Chris Lattnerd9a36a62002-07-25 20:58:51 +00001188 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001189 Out << ' '; printType(RetTy);
Chris Lattner2f2d9472001-11-06 21:28:12 +00001190 writeOperand(Operand, false);
1191 } else {
1192 writeOperand(Operand, true);
1193 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001194 Out << '(';
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001195 if (CI->getNumOperands() > 1) writeOperand(CI->getOperand(1), true);
Chris Lattner113f4f42002-06-25 16:13:24 +00001196 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; ++op) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001197 Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001198 writeOperand(I.getOperand(op), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001199 }
1200
Misha Brukmana6619a92004-06-21 21:53:56 +00001201 Out << " )";
Chris Lattner113f4f42002-06-25 16:13:24 +00001202 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Chris Lattner463d6a52003-08-05 15:34:45 +00001203 const PointerType *PTy = cast<PointerType>(Operand->getType());
1204 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1205 const Type *RetTy = FTy->getReturnType();
1206
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001207 // Print the calling convention being used.
1208 switch (II->getCallingConv()) {
1209 case CallingConv::C: break; // default
Chris Lattner29d20852006-05-19 21:58:52 +00001210 case CallingConv::CSRet: Out << " csretcc"; break;
1211 case CallingConv::Fast: Out << " fastcc"; break;
1212 case CallingConv::Cold: Out << " coldcc"; break;
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +00001213 case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break;
1214 case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break;
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001215 default: Out << " cc" << II->getCallingConv(); break;
1216 }
1217
Chris Lattner463d6a52003-08-05 15:34:45 +00001218 // If possible, print out the short form of the invoke instruction. We can
1219 // only do this if the first argument is a pointer to a nonvararg function,
1220 // and if the return type is not a pointer to a function.
1221 //
1222 if (!FTy->isVarArg() &&
Misha Brukmanb1c93172005-04-21 23:48:37 +00001223 (!isa<PointerType>(RetTy) ||
Chris Lattner463d6a52003-08-05 15:34:45 +00001224 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001225 Out << ' '; printType(RetTy);
Chris Lattner463d6a52003-08-05 15:34:45 +00001226 writeOperand(Operand, false);
1227 } else {
1228 writeOperand(Operand, true);
1229 }
1230
Misha Brukmana6619a92004-06-21 21:53:56 +00001231 Out << '(';
Chris Lattner113f4f42002-06-25 16:13:24 +00001232 if (I.getNumOperands() > 3) writeOperand(I.getOperand(3), true);
1233 for (unsigned op = 4, Eop = I.getNumOperands(); op < Eop; ++op) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001234 Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001235 writeOperand(I.getOperand(op), true);
Chris Lattner862e3382001-10-13 06:42:36 +00001236 }
1237
Misha Brukmana6619a92004-06-21 21:53:56 +00001238 Out << " )\n\t\t\tto";
Chris Lattner862e3382001-10-13 06:42:36 +00001239 writeOperand(II->getNormalDest(), true);
Misha Brukmana6619a92004-06-21 21:53:56 +00001240 Out << " unwind";
Chris Lattnerfae8ab32004-02-08 21:44:31 +00001241 writeOperand(II->getUnwindDest(), true);
Chris Lattner862e3382001-10-13 06:42:36 +00001242
Chris Lattner113f4f42002-06-25 16:13:24 +00001243 } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(&I)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001244 Out << ' ';
Chris Lattner8d48df22002-04-13 18:34:38 +00001245 printType(AI->getType()->getElementType());
1246 if (AI->isArrayAllocation()) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001247 Out << ',';
Chris Lattner8d48df22002-04-13 18:34:38 +00001248 writeOperand(AI->getArraySize(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001249 }
Nate Begeman848622f2005-11-05 09:21:28 +00001250 if (AI->getAlignment()) {
Chris Lattner7aeee3a2005-11-05 21:20:34 +00001251 Out << ", align " << AI->getAlignment();
Nate Begeman848622f2005-11-05 09:21:28 +00001252 }
Chris Lattner862e3382001-10-13 06:42:36 +00001253 } else if (isa<CastInst>(I)) {
Chris Lattnerc96e96b2003-11-17 01:17:04 +00001254 if (Operand) writeOperand(Operand, true); // Work with broken code
Misha Brukmana6619a92004-06-21 21:53:56 +00001255 Out << " to ";
Chris Lattner113f4f42002-06-25 16:13:24 +00001256 printType(I.getType());
Chris Lattner5b337482003-10-18 05:57:43 +00001257 } else if (isa<VAArgInst>(I)) {
Chris Lattnerc96e96b2003-11-17 01:17:04 +00001258 if (Operand) writeOperand(Operand, true); // Work with broken code
Misha Brukmana6619a92004-06-21 21:53:56 +00001259 Out << ", ";
Chris Lattnerf70da102003-05-08 02:44:12 +00001260 printType(I.getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +00001261 } else if (Operand) { // Print the normal way...
1262
Misha Brukmanb1c93172005-04-21 23:48:37 +00001263 // PrintAllTypes - Instructions who have operands of all the same type
Chris Lattner2f7c9632001-06-06 20:29:01 +00001264 // omit the type from all but the first operand. If the instruction has
1265 // different type operands (for example br), then they are all printed.
1266 bool PrintAllTypes = false;
1267 const Type *TheType = Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001268
Chris Lattner52bd5cb92004-03-12 05:53:14 +00001269 // Shift Left & Right print both types even for Ubyte LHS, and select prints
1270 // types even if all operands are bools.
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001271 if (isa<ShiftInst>(I) || isa<SelectInst>(I) || isa<StoreInst>(I) ||
1272 isa<ShuffleVectorInst>(I)) {
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00001273 PrintAllTypes = true;
1274 } else {
1275 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
1276 Operand = I.getOperand(i);
1277 if (Operand->getType() != TheType) {
1278 PrintAllTypes = true; // We have differing types! Print them all!
1279 break;
1280 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001281 }
1282 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001283
Chris Lattner7bfee412001-10-29 16:05:51 +00001284 if (!PrintAllTypes) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001285 Out << ' ';
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00001286 printType(TheType);
Chris Lattner7bfee412001-10-29 16:05:51 +00001287 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001288
Chris Lattner113f4f42002-06-25 16:13:24 +00001289 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001290 if (i) Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001291 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001292 }
1293 }
1294
Chris Lattner862e3382001-10-13 06:42:36 +00001295 printInfoComment(I);
Misha Brukmana6619a92004-06-21 21:53:56 +00001296 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001297}
1298
1299
1300//===----------------------------------------------------------------------===//
1301// External Interface declarations
1302//===----------------------------------------------------------------------===//
1303
Chris Lattner8339f7d2003-10-30 23:41:03 +00001304void Module::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001305 SlotMachine SlotTable(this);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001306 AssemblyWriter W(o, SlotTable, this, AAW);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001307 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001308}
1309
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001310void GlobalVariable::print(std::ostream &o) const {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001311 SlotMachine SlotTable(getParent());
Chris Lattner8339f7d2003-10-30 23:41:03 +00001312 AssemblyWriter W(o, SlotTable, getParent(), 0);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001313 W.write(this);
Chris Lattneradfe0d12001-09-10 20:08:19 +00001314}
1315
Chris Lattner8339f7d2003-10-30 23:41:03 +00001316void Function::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001317 SlotMachine SlotTable(getParent());
Chris Lattner8339f7d2003-10-30 23:41:03 +00001318 AssemblyWriter W(o, SlotTable, getParent(), AAW);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001319
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001320 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001321}
1322
Chris Lattnereef2fe72006-01-24 04:13:11 +00001323void InlineAsm::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Chris Lattnera2d810d2006-01-25 22:26:05 +00001324 WriteAsOperand(o, this, true, true, 0);
Chris Lattnereef2fe72006-01-24 04:13:11 +00001325}
1326
Chris Lattner8339f7d2003-10-30 23:41:03 +00001327void BasicBlock::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001328 SlotMachine SlotTable(getParent());
Misha Brukmanb1c93172005-04-21 23:48:37 +00001329 AssemblyWriter W(o, SlotTable,
Chris Lattner8339f7d2003-10-30 23:41:03 +00001330 getParent() ? getParent()->getParent() : 0, AAW);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001331 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001332}
1333
Chris Lattner8339f7d2003-10-30 23:41:03 +00001334void Instruction::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001335 const Function *F = getParent() ? getParent()->getParent() : 0;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001336 SlotMachine SlotTable(F);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001337 AssemblyWriter W(o, SlotTable, F ? F->getParent() : 0, AAW);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001338
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001339 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001340}
Chris Lattner7db79582001-11-07 04:21:57 +00001341
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001342void Constant::print(std::ostream &o) const {
1343 if (this == 0) { o << "<null> constant value\n"; return; }
Chris Lattner7d734802002-09-10 15:53:49 +00001344
Misha Brukman21bbdb92004-06-04 21:11:51 +00001345 o << ' ' << getType()->getDescription() << ' ';
Evan Cheng5b19a802006-03-01 22:17:00 +00001346
1347 std::map<const Type *, std::string> TypeTable;
1348 WriteConstantInt(o, this, false, TypeTable, 0);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001349}
1350
Misha Brukmanb1c93172005-04-21 23:48:37 +00001351void Type::print(std::ostream &o) const {
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001352 if (this == 0)
1353 o << "<null Type>";
1354 else
1355 o << getDescription();
1356}
1357
Chris Lattner2e9fa6d2002-04-09 19:48:49 +00001358void Argument::print(std::ostream &o) const {
Chris Lattnere52ed452004-07-13 23:14:34 +00001359 WriteAsOperand(o, this, true, true,
1360 getParent() ? getParent()->getParent() : 0);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001361}
1362
Reid Spencer52641832004-05-25 18:14:38 +00001363// Value::dump - allow easy printing of Values from the debugger.
1364// Located here because so much of the needed functionality is here.
Bill Wendlingdfc91892006-11-28 02:09:03 +00001365void Value::dump() const { print(std::cerr); llvm_cerr << '\n'; }
Reid Spencer52641832004-05-25 18:14:38 +00001366
1367// Type::dump - allow easy printing of Values from the debugger.
1368// Located here because so much of the needed functionality is here.
Bill Wendlingdfc91892006-11-28 02:09:03 +00001369void Type::dump() const { print(std::cerr); llvm_cerr << '\n'; }
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001370
1371//===----------------------------------------------------------------------===//
1372// CachedWriter Class Implementation
1373//===----------------------------------------------------------------------===//
1374
Chris Lattner7db79582001-11-07 04:21:57 +00001375void CachedWriter::setModule(const Module *M) {
1376 delete SC; delete AW;
1377 if (M) {
Chris Lattner23e829a2006-12-06 05:12:21 +00001378 SC = new SlotMachine(M);
Misha Brukman21bbdb92004-06-04 21:11:51 +00001379 AW = new AssemblyWriter(Out, *SC, M, 0);
Chris Lattner7db79582001-11-07 04:21:57 +00001380 } else {
1381 SC = 0; AW = 0;
1382 }
1383}
1384
1385CachedWriter::~CachedWriter() {
1386 delete AW;
1387 delete SC;
1388}
1389
Chris Lattner60a7dd12004-07-15 02:51:31 +00001390CachedWriter &CachedWriter::operator<<(const Value &V) {
Chris Lattner7db79582001-11-07 04:21:57 +00001391 assert(AW && SC && "CachedWriter does not have a current module!");
Chris Lattner60a7dd12004-07-15 02:51:31 +00001392 if (const Instruction *I = dyn_cast<Instruction>(&V))
Chris Lattner80d2d532004-06-26 19:40:40 +00001393 AW->write(I);
Chris Lattner60a7dd12004-07-15 02:51:31 +00001394 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(&V))
Chris Lattner80d2d532004-06-26 19:40:40 +00001395 AW->write(BB);
Chris Lattner60a7dd12004-07-15 02:51:31 +00001396 else if (const Function *F = dyn_cast<Function>(&V))
Chris Lattner80d2d532004-06-26 19:40:40 +00001397 AW->write(F);
Chris Lattner60a7dd12004-07-15 02:51:31 +00001398 else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(&V))
Chris Lattner80d2d532004-06-26 19:40:40 +00001399 AW->write(GV);
Misha Brukmanb1c93172005-04-21 23:48:37 +00001400 else
Chris Lattner60a7dd12004-07-15 02:51:31 +00001401 AW->writeOperand(&V, true, true);
Chris Lattner7db79582001-11-07 04:21:57 +00001402 return *this;
1403}
Misha Brukman4685e262004-04-28 15:31:21 +00001404
Chris Lattner60a7dd12004-07-15 02:51:31 +00001405CachedWriter& CachedWriter::operator<<(const Type &Ty) {
Misha Brukman4685e262004-04-28 15:31:21 +00001406 if (SymbolicTypes) {
1407 const Module *M = AW->getModule();
Chris Lattner60a7dd12004-07-15 02:51:31 +00001408 if (M) WriteTypeSymbolic(Out, &Ty, M);
Reid Spencer58d30f22004-07-04 11:50:43 +00001409 } else {
Chris Lattner60a7dd12004-07-15 02:51:31 +00001410 AW->write(&Ty);
Reid Spencer58d30f22004-07-04 11:50:43 +00001411 }
1412 return *this;
Misha Brukman4685e262004-04-28 15:31:21 +00001413}
Misha Brukmanda546ea2004-04-28 19:24:28 +00001414
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001415//===----------------------------------------------------------------------===//
1416//===-- SlotMachine Implementation
1417//===----------------------------------------------------------------------===//
1418
1419#if 0
Bill Wendlingdfc91892006-11-28 02:09:03 +00001420#define SC_DEBUG(X) llvm_cerr << X
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001421#else
1422#define SC_DEBUG(X)
1423#endif
1424
1425// Module level constructor. Causes the contents of the Module (sans functions)
1426// to be added to the slot table.
Misha Brukmanb1c93172005-04-21 23:48:37 +00001427SlotMachine::SlotMachine(const Module *M)
Reid Spencer56010e42004-05-26 21:56:09 +00001428 : TheModule(M) ///< Saved for lazy initialization.
1429 , TheFunction(0)
Reid Spencerb0ac8c42004-08-16 07:46:33 +00001430 , FunctionProcessed(false)
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001431{
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001432}
1433
1434// Function level constructor. Causes the contents of the Module and the one
1435// function provided to be added to the slot table.
Chris Lattner23e829a2006-12-06 05:12:21 +00001436SlotMachine::SlotMachine(const Function *F)
1437 : TheModule(F ? F->getParent() : 0) ///< Saved for lazy initialization
Reid Spencer56010e42004-05-26 21:56:09 +00001438 , TheFunction(F) ///< Saved for lazy initialization
Reid Spencerb0ac8c42004-08-16 07:46:33 +00001439 , FunctionProcessed(false)
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001440{
Reid Spencer56010e42004-05-26 21:56:09 +00001441}
1442
1443inline void SlotMachine::initialize(void) {
Chris Lattner23e829a2006-12-06 05:12:21 +00001444 if (TheModule) {
Misha Brukmanb1c93172005-04-21 23:48:37 +00001445 processModule();
Reid Spencer56010e42004-05-26 21:56:09 +00001446 TheModule = 0; ///< Prevent re-processing next time we're called.
1447 }
Chris Lattner193de902006-12-06 05:27:40 +00001448 if (TheFunction && !FunctionProcessed)
Misha Brukmanb1c93172005-04-21 23:48:37 +00001449 processFunction();
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001450}
1451
1452// Iterate through all the global variables, functions, and global
Misha Brukmanb1c93172005-04-21 23:48:37 +00001453// variable initializers and create slots for them.
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001454void SlotMachine::processModule() {
1455 SC_DEBUG("begin processModule!\n");
1456
1457 // Add all of the global variables to the value table...
Chris Lattner54932b02006-12-06 04:41:52 +00001458 for (Module::const_global_iterator I = TheModule->global_begin(),
1459 E = TheModule->global_end(); I != E; ++I)
Chris Lattner193de902006-12-06 05:27:40 +00001460 getOrCreateSlot(I);
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001461
1462 // Add all the functions to the table
1463 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
1464 I != E; ++I)
Chris Lattner193de902006-12-06 05:27:40 +00001465 getOrCreateSlot(I);
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001466
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001467 SC_DEBUG("end processModule!\n");
1468}
1469
1470
Reid Spencer56010e42004-05-26 21:56:09 +00001471// Process the arguments, basic blocks, and instructions of a function.
1472void SlotMachine::processFunction() {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001473 SC_DEBUG("begin processFunction!\n");
1474
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001475 // Add all the function arguments
Misha Brukmanb1c93172005-04-21 23:48:37 +00001476 for(Function::const_arg_iterator AI = TheFunction->arg_begin(),
Chris Lattner531f9e92005-03-15 04:54:21 +00001477 AE = TheFunction->arg_end(); AI != AE; ++AI)
Chris Lattner193de902006-12-06 05:27:40 +00001478 getOrCreateSlot(AI);
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001479
1480 SC_DEBUG("Inserting Instructions:\n");
1481
1482 // Add all of the basic blocks and instructions
Misha Brukmanb1c93172005-04-21 23:48:37 +00001483 for (Function::const_iterator BB = TheFunction->begin(),
Reid Spencer56010e42004-05-26 21:56:09 +00001484 E = TheFunction->end(); BB != E; ++BB) {
Chris Lattner193de902006-12-06 05:27:40 +00001485 getOrCreateSlot(BB);
1486 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I)
1487 getOrCreateSlot(I);
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001488 }
1489
Reid Spencerb0ac8c42004-08-16 07:46:33 +00001490 FunctionProcessed = true;
1491
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001492 SC_DEBUG("end processFunction!\n");
1493}
1494
Chris Lattner193de902006-12-06 05:27:40 +00001495/// Clean up after incorporating a function. This is the only way to get out of
1496/// the function incorporation state that affects the
1497/// getSlot/getOrCreateSlot lock. Function incorporation state is indicated
1498/// by TheFunction != 0.
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001499void SlotMachine::purgeFunction() {
1500 SC_DEBUG("begin purgeFunction!\n");
1501 fMap.clear(); // Simply discard the function level map
Reid Spencer56010e42004-05-26 21:56:09 +00001502 TheFunction = 0;
Reid Spencerb0ac8c42004-08-16 07:46:33 +00001503 FunctionProcessed = false;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001504 SC_DEBUG("end purgeFunction!\n");
1505}
1506
1507/// Get the slot number for a value. This function will assert if you
Chris Lattner193de902006-12-06 05:27:40 +00001508/// ask for a Value that hasn't previously been inserted with getOrCreateSlot.
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001509/// Types are forbidden because Type does not inherit from Value (any more).
Chris Lattner757ee0b2004-06-09 19:41:19 +00001510int SlotMachine::getSlot(const Value *V) {
Chris Lattner23e829a2006-12-06 05:12:21 +00001511 assert(V && "Can't get slot for null Value");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001512 assert(!isa<Constant>(V) || isa<GlobalValue>(V) &&
1513 "Can't insert a non-GlobalValue Constant into SlotMachine");
Reid Spencer56010e42004-05-26 21:56:09 +00001514
1515 // Check for uninitialized state and do lazy initialization
1516 this->initialize();
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001517
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001518 // Get the type of the value
1519 const Type* VTy = V->getType();
1520
1521 // Find the type plane in the module map
1522 TypedPlanes::const_iterator MI = mMap.find(VTy);
1523
Chris Lattner23e829a2006-12-06 05:12:21 +00001524 if (TheFunction) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001525 // Lookup the type in the function map too
1526 TypedPlanes::const_iterator FI = fMap.find(VTy);
1527 // If there is a corresponding type plane in the function map
Chris Lattner23e829a2006-12-06 05:12:21 +00001528 if (FI != fMap.end()) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001529 // Lookup the Value in the function map
1530 ValueMap::const_iterator FVI = FI->second.map.find(V);
1531 // If the value doesn't exist in the function map
Chris Lattner23e829a2006-12-06 05:12:21 +00001532 if (FVI == FI->second.map.end()) {
Chris Lattner68a038e2004-06-09 22:22:10 +00001533 // Look up the value in the module map.
1534 if (MI == mMap.end()) return -1;
Reid Spencer56010e42004-05-26 21:56:09 +00001535 ValueMap::const_iterator MVI = MI->second.map.find(V);
1536 // If we didn't find it, it wasn't inserted
Chris Lattner757ee0b2004-06-09 19:41:19 +00001537 if (MVI == MI->second.map.end()) return -1;
Chris Lattner23e829a2006-12-06 05:12:21 +00001538 assert(MVI != MI->second.map.end() && "Value not found");
Reid Spencer56010e42004-05-26 21:56:09 +00001539 // We found it only at the module level
Misha Brukmanb1c93172005-04-21 23:48:37 +00001540 return MVI->second;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001541
1542 // else the value exists in the function map
1543 } else {
Reid Spencer56010e42004-05-26 21:56:09 +00001544 // Return the slot number as the module's contribution to
1545 // the type plane plus the index in the function's contribution
1546 // to the type plane.
Chris Lattnerb1f04782004-06-15 21:07:32 +00001547 if (MI != mMap.end())
1548 return MI->second.next_slot + FVI->second;
1549 else
1550 return FVI->second;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001551 }
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001552 }
1553 }
1554
Reid Spencer8beac692004-06-09 15:26:53 +00001555 // N.B. Can get here only if either !TheFunction or the function doesn't
1556 // have a corresponding type plane for the Value
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001557
1558 // Make sure the type plane exists
Chris Lattner757ee0b2004-06-09 19:41:19 +00001559 if (MI == mMap.end()) return -1;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001560 // Lookup the value in the module's map
1561 ValueMap::const_iterator MVI = MI->second.map.find(V);
1562 // Make sure we found it.
Chris Lattner757ee0b2004-06-09 19:41:19 +00001563 if (MVI == MI->second.map.end()) return -1;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001564 // Return it.
1565 return MVI->second;
1566}
1567
Reid Spencer58d30f22004-07-04 11:50:43 +00001568
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001569// Create a new slot, or return the existing slot if it is already
1570// inserted. Note that the logic here parallels getSlot but instead
1571// of asserting when the Value* isn't found, it inserts the value.
Chris Lattner193de902006-12-06 05:27:40 +00001572unsigned SlotMachine::getOrCreateSlot(const Value *V) {
Chris Lattner23e829a2006-12-06 05:12:21 +00001573 assert(V && "Can't insert a null Value to SlotMachine");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001574 assert(!isa<Constant>(V) || isa<GlobalValue>(V) &&
1575 "Can't insert a non-GlobalValue Constant into SlotMachine");
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001576
1577 const Type* VTy = V->getType();
1578
Chris Lattner7e4e7662006-12-06 05:42:32 +00001579 // Just ignore void typed things or things with names.
1580 if (VTy == Type::VoidTy || V->hasName())
1581 return 0; // FIXME: Wrong return value!
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001582
1583 // Look up the type plane for the Value's type from the module map
1584 TypedPlanes::const_iterator MI = mMap.find(VTy);
1585
Chris Lattner23e829a2006-12-06 05:12:21 +00001586 if (TheFunction) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001587 // Get the type plane for the Value's type from the function map
1588 TypedPlanes::const_iterator FI = fMap.find(VTy);
1589 // If there is a corresponding type plane in the function map
Chris Lattner23e829a2006-12-06 05:12:21 +00001590 if (FI != fMap.end()) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001591 // Lookup the Value in the function map
1592 ValueMap::const_iterator FVI = FI->second.map.find(V);
1593 // If the value doesn't exist in the function map
Chris Lattner23e829a2006-12-06 05:12:21 +00001594 if (FVI == FI->second.map.end()) {
Reid Spencer56010e42004-05-26 21:56:09 +00001595 // If there is no corresponding type plane in the module map
Chris Lattner23e829a2006-12-06 05:12:21 +00001596 if (MI == mMap.end())
Reid Spencer56010e42004-05-26 21:56:09 +00001597 return insertValue(V);
1598 // Look up the value in the module map
1599 ValueMap::const_iterator MVI = MI->second.map.find(V);
1600 // If we didn't find it, it wasn't inserted
Chris Lattner23e829a2006-12-06 05:12:21 +00001601 if (MVI == MI->second.map.end())
Reid Spencer56010e42004-05-26 21:56:09 +00001602 return insertValue(V);
1603 else
1604 // We found it only at the module level
1605 return MVI->second;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001606
1607 // else the value exists in the function map
1608 } else {
Chris Lattner23e829a2006-12-06 05:12:21 +00001609 if (MI == mMap.end())
Reid Spencer56010e42004-05-26 21:56:09 +00001610 return FVI->second;
1611 else
1612 // Return the slot number as the module's contribution to
1613 // the type plane plus the index in the function's contribution
1614 // to the type plane.
1615 return MI->second.next_slot + FVI->second;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001616 }
1617
1618 // else there is not a corresponding type plane in the function map
1619 } else {
1620 // If the type plane doesn't exists at the module level
Chris Lattner23e829a2006-12-06 05:12:21 +00001621 if (MI == mMap.end()) {
Reid Spencer56010e42004-05-26 21:56:09 +00001622 return insertValue(V);
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001623 // else type plane exists at the module level, examine it
1624 } else {
Reid Spencer56010e42004-05-26 21:56:09 +00001625 // Look up the value in the module's map
1626 ValueMap::const_iterator MVI = MI->second.map.find(V);
1627 // If we didn't find it there either
Chris Lattner23e829a2006-12-06 05:12:21 +00001628 if (MVI == MI->second.map.end())
Reid Spencer56010e42004-05-26 21:56:09 +00001629 // Return the slot number as the module's contribution to
1630 // the type plane plus the index of the function map insertion.
1631 return MI->second.next_slot + insertValue(V);
1632 else
1633 return MVI->second;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001634 }
1635 }
1636 }
1637
Reid Spencer56010e42004-05-26 21:56:09 +00001638 // N.B. Can only get here if !TheFunction
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001639
1640 // If the module map's type plane is not for the Value's type
Chris Lattner23e829a2006-12-06 05:12:21 +00001641 if (MI != mMap.end()) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001642 // Lookup the value in the module's map
1643 ValueMap::const_iterator MVI = MI->second.map.find(V);
Chris Lattner23e829a2006-12-06 05:12:21 +00001644 if (MVI != MI->second.map.end())
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001645 return MVI->second;
1646 }
1647
1648 return insertValue(V);
1649}
1650
1651
1652// Low level insert function. Minimal checking is done. This
Chris Lattner193de902006-12-06 05:27:40 +00001653// function is just for the convenience of getOrCreateSlot (above).
Chris Lattner23e829a2006-12-06 05:12:21 +00001654unsigned SlotMachine::insertValue(const Value *V) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001655 assert(V && "Can't insert a null Value into SlotMachine!");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001656 assert(!isa<Constant>(V) || isa<GlobalValue>(V) &&
Chris Lattner7e4e7662006-12-06 05:42:32 +00001657 "Can't insert a non-GlobalValue Constant into SlotMachine");
1658 assert(V->getType() != Type::VoidTy && !V->hasName());
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001659
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001660 const Type *VTy = V->getType();
1661 unsigned DestSlot = 0;
1662
Chris Lattner23e829a2006-12-06 05:12:21 +00001663 if (TheFunction) {
1664 TypedPlanes::iterator I = fMap.find(VTy);
1665 if (I == fMap.end())
Reid Spencer58d30f22004-07-04 11:50:43 +00001666 I = fMap.insert(std::make_pair(VTy,ValuePlane())).first;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001667 DestSlot = I->second.map[V] = I->second.next_slot++;
1668 } else {
Chris Lattner23e829a2006-12-06 05:12:21 +00001669 TypedPlanes::iterator I = mMap.find(VTy);
1670 if (I == mMap.end())
Reid Spencer58d30f22004-07-04 11:50:43 +00001671 I = mMap.insert(std::make_pair(VTy,ValuePlane())).first;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001672 DestSlot = I->second.map[V] = I->second.next_slot++;
1673 }
1674
Misha Brukmanb1c93172005-04-21 23:48:37 +00001675 SC_DEBUG(" Inserting value [" << VTy << "] = " << V << " slot=" <<
Reid Spencer56010e42004-05-26 21:56:09 +00001676 DestSlot << " [");
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001677 // G = Global, C = Constant, T = Type, F = Function, o = other
Misha Brukmanb1c93172005-04-21 23:48:37 +00001678 SC_DEBUG((isa<GlobalVariable>(V) ? 'G' : (isa<Function>(V) ? 'F' :
Reid Spenceraccd7c72004-07-17 23:47:01 +00001679 (isa<Constant>(V) ? 'C' : 'o'))));
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001680 SC_DEBUG("]\n");
1681 return DestSlot;
1682}