blob: 50526c395941feb17bec0c2a38f122db7dc3bd21 [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;
Reid Spencer58d30f22004-07-04 11:50:43 +000052 typedef std::map<const Type*, unsigned> TypeMap;
Reid Spencer16f2f7f2004-05-26 07:18:52 +000053
54 /// @brief A plane with next slot number and ValueMap
Misha Brukmanb1c93172005-04-21 23:48:37 +000055 struct ValuePlane {
Reid Spencer16f2f7f2004-05-26 07:18:52 +000056 unsigned next_slot; ///< The next slot number to use
57 ValueMap map; ///< The map of Value* -> unsigned
Reid Spencer58d30f22004-07-04 11:50:43 +000058 ValuePlane() { next_slot = 0; } ///< Make sure we start at 0
59 };
60
61 struct TypePlane {
62 unsigned next_slot;
63 TypeMap map;
64 TypePlane() { next_slot = 0; }
65 void clear() { map.clear(); next_slot = 0; }
Reid Spencer16f2f7f2004-05-26 07:18:52 +000066 };
67
68 /// @brief The map of planes by Type
Reid Spencer58d30f22004-07-04 11:50:43 +000069 typedef std::map<const Type*, ValuePlane> TypedPlanes;
Reid Spencer16f2f7f2004-05-26 07:18:52 +000070
71/// @}
72/// @name Constructors
73/// @{
74public:
75 /// @brief Construct from a module
76 SlotMachine(const Module *M );
77
78 /// @brief Construct from a function, starting out in incorp state.
79 SlotMachine(const Function *F );
80
81/// @}
82/// @name Accessors
83/// @{
84public:
85 /// Return the slot number of the specified value in it's type
86 /// plane. Its an error to ask for something not in the SlotMachine.
87 /// Its an error to ask for a Type*
Chris Lattner757ee0b2004-06-09 19:41:19 +000088 int getSlot(const Value *V);
Reid Spencer58d30f22004-07-04 11:50:43 +000089 int getSlot(const Type*Ty);
Reid Spencer8beac692004-06-09 15:26:53 +000090
91 /// Determine if a Value has a slot or not
92 bool hasSlot(const Value* V);
Reid Spencer58d30f22004-07-04 11:50:43 +000093 bool hasSlot(const Type* Ty);
Reid Spencer16f2f7f2004-05-26 07:18:52 +000094
95/// @}
96/// @name Mutators
97/// @{
98public:
Misha Brukmanb1c93172005-04-21 23:48:37 +000099 /// If you'd like to deal with a function instead of just a module, use
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000100 /// this method to get its data into the SlotMachine.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000101 void incorporateFunction(const Function *F) {
102 TheFunction = F;
Reid Spencerb0ac8c42004-08-16 07:46:33 +0000103 FunctionProcessed = false;
104 }
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000105
Misha Brukmanb1c93172005-04-21 23:48:37 +0000106 /// After calling incorporateFunction, use this method to remove the
107 /// most recently incorporated function from the SlotMachine. This
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000108 /// will reset the state of the machine back to just the module contents.
109 void purgeFunction();
110
111/// @}
112/// @name Implementation Details
113/// @{
114private:
Reid Spencer56010e42004-05-26 21:56:09 +0000115 /// This function does the actual initialization.
116 inline void initialize();
117
Misha Brukmanb1c93172005-04-21 23:48:37 +0000118 /// Values can be crammed into here at will. If they haven't
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000119 /// been inserted already, they get inserted, otherwise they are ignored.
120 /// Either way, the slot number for the Value* is returned.
121 unsigned createSlot(const Value *V);
Reid Spencer58d30f22004-07-04 11:50:43 +0000122 unsigned createSlot(const Type* Ty);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000123
124 /// Insert a value into the value table. Return the slot number
125 /// that it now occupies. BadThings(TM) will happen if you insert a
Misha Brukmanb1c93172005-04-21 23:48:37 +0000126 /// Value that's already been inserted.
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000127 unsigned insertValue( const Value *V );
Reid Spencer58d30f22004-07-04 11:50:43 +0000128 unsigned insertValue( const Type* Ty);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000129
130 /// Add all of the module level global variables (and their initializers)
131 /// and function declarations, but not the contents of those functions.
132 void processModule();
133
Reid Spencer56010e42004-05-26 21:56:09 +0000134 /// Add all of the functions arguments, basic blocks, and instructions
135 void processFunction();
136
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000137 SlotMachine(const SlotMachine &); // DO NOT IMPLEMENT
138 void operator=(const SlotMachine &); // DO NOT IMPLEMENT
139
140/// @}
141/// @name Data
142/// @{
143public:
144
145 /// @brief The module for which we are holding slot numbers
Reid Spencer56010e42004-05-26 21:56:09 +0000146 const Module* TheModule;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000147
Reid Spencer56010e42004-05-26 21:56:09 +0000148 /// @brief The function for which we are holding slot numbers
149 const Function* TheFunction;
Reid Spencerb0ac8c42004-08-16 07:46:33 +0000150 bool FunctionProcessed;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000151
152 /// @brief The TypePlanes map for the module level data
153 TypedPlanes mMap;
Reid Spencer58d30f22004-07-04 11:50:43 +0000154 TypePlane mTypes;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000155
156 /// @brief The TypePlanes map for the function level data
157 TypedPlanes fMap;
Reid Spencer58d30f22004-07-04 11:50:43 +0000158 TypePlane fTypes;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000159
160/// @}
161
162};
163
Chris Lattner60a7dd12004-07-15 02:51:31 +0000164} // end namespace llvm
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000165
Chris Lattnerc2d3d312006-08-27 22:42:52 +0000166static RegisterPass<PrintModulePass>
Chris Lattnere7134c52006-08-21 17:20:01 +0000167X("printm", "Print module to stderr");
Chris Lattnerc2d3d312006-08-27 22:42:52 +0000168static RegisterPass<PrintFunctionPass>
Chris Lattnere7134c52006-08-21 17:20:01 +0000169Y("print","Print function to stderr");
Chris Lattner7f8845a2002-07-23 18:07:49 +0000170
Misha Brukmanb1c93172005-04-21 23:48:37 +0000171static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000172 bool PrintName,
173 std::map<const Type *, std::string> &TypeTable,
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000174 SlotMachine *Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000175
Misha Brukmanb1c93172005-04-21 23:48:37 +0000176static void WriteAsOperandInternal(std::ostream &Out, const Type *T,
Reid Spencer58d30f22004-07-04 11:50:43 +0000177 bool PrintName,
178 std::map<const Type *, std::string> &TypeTable,
179 SlotMachine *Machine);
180
Chris Lattnerb86620e2001-10-29 16:37:48 +0000181static const Module *getModuleFromVal(const Value *V) {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000182 if (const Argument *MA = dyn_cast<Argument>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +0000183 return MA->getParent() ? MA->getParent()->getParent() : 0;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000184 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +0000185 return BB->getParent() ? BB->getParent()->getParent() : 0;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000186 else if (const Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner57698e22002-03-26 18:01:55 +0000187 const Function *M = I->getParent() ? I->getParent()->getParent() : 0;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000188 return M ? M->getParent() : 0;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000189 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +0000190 return GV->getParent();
Chris Lattnerb86620e2001-10-29 16:37:48 +0000191 return 0;
192}
193
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000194static SlotMachine *createSlotMachine(const Value *V) {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000195 if (const Argument *FA = dyn_cast<Argument>(V)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000196 return new SlotMachine(FA->getParent());
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000197 } else if (const Instruction *I = dyn_cast<Instruction>(V)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000198 return new SlotMachine(I->getParent()->getParent());
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000199 } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000200 return new SlotMachine(BB->getParent());
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000201 } else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)){
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000202 return new SlotMachine(GV->getParent());
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000203 } else if (const Function *Func = dyn_cast<Function>(V)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000204 return new SlotMachine(Func);
Chris Lattner7bfee412001-10-29 16:05:51 +0000205 }
206 return 0;
207}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000208
Chris Lattner1c343042003-08-22 05:40:38 +0000209// getLLVMName - Turn the specified string into an 'LLVM name', which is either
210// prefixed with % (if the string only contains simple characters) or is
211// surrounded with ""'s (if it has special chars in it).
Alkis Evlogimenos991d6ad2004-12-10 05:41:10 +0000212static std::string getLLVMName(const std::string &Name,
213 bool prefixName = true) {
Chris Lattner1c343042003-08-22 05:40:38 +0000214 assert(!Name.empty() && "Cannot get empty name!");
215
216 // First character cannot start with a number...
217 if (Name[0] >= '0' && Name[0] <= '9')
218 return "\"" + Name + "\"";
219
220 // Scan to see if we have any characters that are not on the "white list"
221 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
222 char C = Name[i];
223 assert(C != '"' && "Illegal character in LLVM value name!");
224 if ((C < 'a' || C > 'z') && (C < 'A' || C > 'Z') && (C < '0' || C > '9') &&
225 C != '-' && C != '.' && C != '_')
226 return "\"" + Name + "\"";
227 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000228
Chris Lattner1c343042003-08-22 05:40:38 +0000229 // If we get here, then the identifier is legal to use as a "VarID".
Alkis Evlogimenos991d6ad2004-12-10 05:41:10 +0000230 if (prefixName)
231 return "%"+Name;
232 else
233 return Name;
Chris Lattner1c343042003-08-22 05:40:38 +0000234}
235
Chris Lattnerb86620e2001-10-29 16:37:48 +0000236
Misha Brukmanc566ca362004-03-02 00:22:19 +0000237/// fillTypeNameTable - If the module has a symbol table, take all global types
238/// and stuff their names into the TypeNames map.
239///
Chris Lattnerb86620e2001-10-29 16:37:48 +0000240static void fillTypeNameTable(const Module *M,
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000241 std::map<const Type *, std::string> &TypeNames) {
Chris Lattner98cf1f52002-11-20 18:36:02 +0000242 if (!M) return;
243 const SymbolTable &ST = M->getSymbolTable();
Reid Spencere7e96712004-05-25 08:53:40 +0000244 SymbolTable::type_const_iterator TI = ST.type_begin();
245 for (; TI != ST.type_end(); ++TI ) {
246 // As a heuristic, don't insert pointer to primitive types, because
247 // they are used too often to have a single useful name.
248 //
249 const Type *Ty = cast<Type>(TI->second);
250 if (!isa<PointerType>(Ty) ||
Reid Spencer56010e42004-05-26 21:56:09 +0000251 !cast<PointerType>(Ty)->getElementType()->isPrimitiveType() ||
252 isa<OpaqueType>(cast<PointerType>(Ty)->getElementType()))
Reid Spencere7e96712004-05-25 08:53:40 +0000253 TypeNames.insert(std::make_pair(Ty, getLLVMName(TI->first)));
Chris Lattnerb86620e2001-10-29 16:37:48 +0000254 }
255}
256
257
258
Misha Brukmanb1c93172005-04-21 23:48:37 +0000259static void calcTypeName(const Type *Ty,
John Criswellcd116ba2004-06-01 14:54:08 +0000260 std::vector<const Type *> &TypeStack,
261 std::map<const Type *, std::string> &TypeNames,
262 std::string & Result){
263 if (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty)) {
264 Result += Ty->getDescription(); // Base case
265 return;
266 }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000267
268 // Check to see if the type is named.
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000269 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
John Criswellcd116ba2004-06-01 14:54:08 +0000270 if (I != TypeNames.end()) {
271 Result += I->second;
272 return;
273 }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000274
John Criswellcd116ba2004-06-01 14:54:08 +0000275 if (isa<OpaqueType>(Ty)) {
276 Result += "opaque";
277 return;
278 }
Chris Lattnerf14ead92003-10-30 00:22:33 +0000279
Chris Lattnerb86620e2001-10-29 16:37:48 +0000280 // Check to see if the Type is already on the stack...
281 unsigned Slot = 0, CurSize = TypeStack.size();
282 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
283
Misha Brukmanb1c93172005-04-21 23:48:37 +0000284 // This is another base case for the recursion. In this case, we know
Chris Lattnerb86620e2001-10-29 16:37:48 +0000285 // that we have looped back to a type that we have previously visited.
286 // Generate the appropriate upreference to handle this.
John Criswellcd116ba2004-06-01 14:54:08 +0000287 if (Slot < CurSize) {
288 Result += "\\" + utostr(CurSize-Slot); // Here's the upreference
289 return;
290 }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000291
292 TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
Misha Brukmanb1c93172005-04-21 23:48:37 +0000293
Chris Lattner6b727592004-06-17 18:19:28 +0000294 switch (Ty->getTypeID()) {
Chris Lattner91db5822002-03-29 03:44:36 +0000295 case Type::FunctionTyID: {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000296 const FunctionType *FTy = cast<FunctionType>(Ty);
John Criswellcd116ba2004-06-01 14:54:08 +0000297 calcTypeName(FTy->getReturnType(), TypeStack, TypeNames, Result);
298 Result += " (";
Chris Lattnerfa829be2004-02-09 04:14:01 +0000299 for (FunctionType::param_iterator I = FTy->param_begin(),
300 E = FTy->param_end(); I != E; ++I) {
301 if (I != FTy->param_begin())
Chris Lattnerb86620e2001-10-29 16:37:48 +0000302 Result += ", ";
John Criswellcd116ba2004-06-01 14:54:08 +0000303 calcTypeName(*I, TypeStack, TypeNames, Result);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000304 }
Chris Lattnerd816b532002-04-13 20:53:41 +0000305 if (FTy->isVarArg()) {
Chris Lattnerfa829be2004-02-09 04:14:01 +0000306 if (FTy->getNumParams()) Result += ", ";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000307 Result += "...";
308 }
309 Result += ")";
310 break;
311 }
312 case Type::StructTyID: {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000313 const StructType *STy = cast<StructType>(Ty);
John Criswellcd116ba2004-06-01 14:54:08 +0000314 Result += "{ ";
Chris Lattnerac6db752004-02-09 04:37:31 +0000315 for (StructType::element_iterator I = STy->element_begin(),
316 E = STy->element_end(); I != E; ++I) {
317 if (I != STy->element_begin())
Chris Lattnerb86620e2001-10-29 16:37:48 +0000318 Result += ", ";
John Criswellcd116ba2004-06-01 14:54:08 +0000319 calcTypeName(*I, TypeStack, TypeNames, Result);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000320 }
321 Result += " }";
322 break;
323 }
324 case Type::PointerTyID:
Misha Brukmanb1c93172005-04-21 23:48:37 +0000325 calcTypeName(cast<PointerType>(Ty)->getElementType(),
John Criswellcd116ba2004-06-01 14:54:08 +0000326 TypeStack, TypeNames, Result);
327 Result += "*";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000328 break;
329 case Type::ArrayTyID: {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000330 const ArrayType *ATy = cast<ArrayType>(Ty);
John Criswellcd116ba2004-06-01 14:54:08 +0000331 Result += "[" + utostr(ATy->getNumElements()) + " x ";
332 calcTypeName(ATy->getElementType(), TypeStack, TypeNames, Result);
333 Result += "]";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000334 break;
335 }
Brian Gaeke02209042004-08-20 06:00:58 +0000336 case Type::PackedTyID: {
337 const PackedType *PTy = cast<PackedType>(Ty);
338 Result += "<" + utostr(PTy->getNumElements()) + " x ";
339 calcTypeName(PTy->getElementType(), TypeStack, TypeNames, Result);
340 Result += ">";
341 break;
342 }
Chris Lattner15285ab2003-05-14 17:50:47 +0000343 case Type::OpaqueTyID:
John Criswellcd116ba2004-06-01 14:54:08 +0000344 Result += "opaque";
Chris Lattner15285ab2003-05-14 17:50:47 +0000345 break;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000346 default:
John Criswellcd116ba2004-06-01 14:54:08 +0000347 Result += "<unrecognized-type>";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000348 }
349
350 TypeStack.pop_back(); // Remove self from stack...
John Criswellcd116ba2004-06-01 14:54:08 +0000351 return;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000352}
353
354
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000355/// printTypeInt - The internal guts of printing out a type that has a
356/// potentially named portion.
357///
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000358static std::ostream &printTypeInt(std::ostream &Out, const Type *Ty,
359 std::map<const Type *, std::string> &TypeNames) {
Chris Lattnerb86620e2001-10-29 16:37:48 +0000360 // Primitive types always print out their description, regardless of whether
361 // they have been named or not.
362 //
Chris Lattner92d60532003-10-30 00:12:51 +0000363 if (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty))
364 return Out << Ty->getDescription();
Chris Lattnerb86620e2001-10-29 16:37:48 +0000365
366 // Check to see if the type is named.
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000367 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000368 if (I != TypeNames.end()) return Out << I->second;
369
370 // Otherwise we have a type that has not been named but is a derived type.
371 // Carefully recurse the type hierarchy to print out any contained symbolic
372 // names.
373 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000374 std::vector<const Type *> TypeStack;
John Criswellcd116ba2004-06-01 14:54:08 +0000375 std::string TypeName;
376 calcTypeName(Ty, TypeStack, TypeNames, TypeName);
Chris Lattner7f74a562002-01-20 22:54:45 +0000377 TypeNames.insert(std::make_pair(Ty, TypeName));//Cache type name for later use
John Criswellcd116ba2004-06-01 14:54:08 +0000378 return (Out << TypeName);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000379}
380
Chris Lattner34b95182001-10-31 04:33:19 +0000381
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000382/// WriteTypeSymbolic - This attempts to write the specified type as a symbolic
383/// type, iff there is an entry in the modules symbol table for the specified
384/// type or one of it's component types. This is slower than a simple x << Type
385///
Chris Lattner189d19f2003-11-21 20:23:48 +0000386std::ostream &llvm::WriteTypeSymbolic(std::ostream &Out, const Type *Ty,
387 const Module *M) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000388 Out << ' ';
Chris Lattnerb86620e2001-10-29 16:37:48 +0000389
390 // If they want us to print out a type, attempt to make it symbolic if there
391 // is a symbol table in the module...
Chris Lattner98cf1f52002-11-20 18:36:02 +0000392 if (M) {
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000393 std::map<const Type *, std::string> TypeNames;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000394 fillTypeNameTable(M, TypeNames);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000395
Chris Lattner72f866e2001-10-29 16:40:32 +0000396 return printTypeInt(Out, Ty, TypeNames);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000397 } else {
Chris Lattner72f866e2001-10-29 16:40:32 +0000398 return Out << Ty->getDescription();
Chris Lattnerb86620e2001-10-29 16:37:48 +0000399 }
400}
401
Chris Lattner6ed87bd2006-01-23 23:03:36 +0000402// PrintEscapedString - Print each character of the specified string, escaping
403// it if it is not printable or if it is an escape char.
404static void PrintEscapedString(const std::string &Str, std::ostream &Out) {
405 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
406 unsigned char C = Str[i];
407 if (isprint(C) && C != '"' && C != '\\') {
408 Out << C;
409 } else {
410 Out << '\\'
411 << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'))
412 << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
413 }
414 }
415}
416
Misha Brukmanb1c93172005-04-21 23:48:37 +0000417/// @brief Internal constant writer.
418static void WriteConstantInt(std::ostream &Out, const Constant *CV,
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000419 bool PrintName,
420 std::map<const Type *, std::string> &TypeTable,
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000421 SlotMachine *Machine) {
Jim Laskey6be3d8e2006-02-27 10:33:53 +0000422 const int IndentSize = 4;
423 static std::string Indent = "\n";
Chris Lattner1e194682002-04-18 18:53:13 +0000424 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
Chris Lattner94e39cb2006-09-28 22:50:29 +0000425 Out << (CB->getValue() ? "true" : "false");
Reid Spencere0fc4df2006-10-20 07:07:24 +0000426 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
427 if (CI->getType()->isSigned())
428 Out << CI->getSExtValue();
429 else
430 Out << CI->getZExtValue();
Chris Lattner1e194682002-04-18 18:53:13 +0000431 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
432 // We would like to output the FP constant value in exponential notation,
433 // but we cannot do this if doing so will lose precision. Check here to
434 // make sure that we only output it in exponential format if we can parse
435 // the value back and get the same value.
436 //
437 std::string StrVal = ftostr(CFP->getValue());
438
439 // Check to make sure that the stringized number is not some string like
440 // "Inf" or NaN, that atof will accept, but the lexer will not. Check that
441 // the string matches the "[-+]?[0-9]" regex.
442 //
443 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
444 ((StrVal[0] == '-' || StrVal[0] == '+') &&
Brian Gaeke87b4f072003-06-17 23:55:35 +0000445 (StrVal[1] >= '0' && StrVal[1] <= '9')))
Chris Lattner1e194682002-04-18 18:53:13 +0000446 // Reparse stringized version!
447 if (atof(StrVal.c_str()) == CFP->getValue()) {
Chris Lattner472cc102005-01-04 01:56:57 +0000448 Out << StrVal;
449 return;
Chris Lattner1e194682002-04-18 18:53:13 +0000450 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000451
Chris Lattner1e194682002-04-18 18:53:13 +0000452 // Otherwise we could not reparse it to exactly the same value, so we must
453 // output the string in hexadecimal format!
Chris Lattner472cc102005-01-04 01:56:57 +0000454 assert(sizeof(double) == sizeof(uint64_t) &&
Chris Lattner1e194682002-04-18 18:53:13 +0000455 "assuming that double is 64 bits!");
Jim Laskeyb74c6662005-08-17 19:34:49 +0000456 Out << "0x" << utohexstr(DoubleToBits(CFP->getValue()));
Chris Lattner1e194682002-04-18 18:53:13 +0000457
Chris Lattner76b2ff42004-02-15 05:55:15 +0000458 } else if (isa<ConstantAggregateZero>(CV)) {
459 Out << "zeroinitializer";
Chris Lattner1e194682002-04-18 18:53:13 +0000460 } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
461 // As a special case, print the array as a string if it is an array of
462 // ubytes or an array of sbytes with positive values.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000463 //
Chris Lattner1e194682002-04-18 18:53:13 +0000464 const Type *ETy = CA->getType()->getElementType();
Chris Lattner6ed87bd2006-01-23 23:03:36 +0000465 if (CA->isString()) {
Chris Lattner1e194682002-04-18 18:53:13 +0000466 Out << "c\"";
Chris Lattner6ed87bd2006-01-23 23:03:36 +0000467 PrintEscapedString(CA->getAsString(), Out);
Chris Lattner1e194682002-04-18 18:53:13 +0000468 Out << "\"";
469
470 } else { // Cannot output in string format...
Misha Brukman21bbdb92004-06-04 21:11:51 +0000471 Out << '[';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000472 if (CA->getNumOperands()) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000473 Out << ' ';
Chris Lattner1e194682002-04-18 18:53:13 +0000474 printTypeInt(Out, ETy, TypeTable);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000475 WriteAsOperandInternal(Out, CA->getOperand(0),
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000476 PrintName, TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000477 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
478 Out << ", ";
Chris Lattner1e194682002-04-18 18:53:13 +0000479 printTypeInt(Out, ETy, TypeTable);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000480 WriteAsOperandInternal(Out, CA->getOperand(i), PrintName,
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000481 TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000482 }
483 }
484 Out << " ]";
485 }
486 } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000487 Out << '{';
Jim Laskey3bb78742006-02-25 12:27:03 +0000488 unsigned N = CS->getNumOperands();
489 if (N) {
Jim Laskey6be3d8e2006-02-27 10:33:53 +0000490 if (N > 2) {
491 Indent += std::string(IndentSize, ' ');
492 Out << Indent;
493 } else {
494 Out << ' ';
495 }
Chris Lattnerd84bb632002-04-16 21:36:08 +0000496 printTypeInt(Out, CS->getOperand(0)->getType(), TypeTable);
497
498 WriteAsOperandInternal(Out, CS->getOperand(0),
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000499 PrintName, TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000500
Jim Laskey3bb78742006-02-25 12:27:03 +0000501 for (unsigned i = 1; i < N; i++) {
Chris Lattnerd84bb632002-04-16 21:36:08 +0000502 Out << ", ";
Jim Laskey6be3d8e2006-02-27 10:33:53 +0000503 if (N > 2) Out << Indent;
Chris Lattnerd84bb632002-04-16 21:36:08 +0000504 printTypeInt(Out, CS->getOperand(i)->getType(), TypeTable);
505
506 WriteAsOperandInternal(Out, CS->getOperand(i),
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000507 PrintName, TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000508 }
Jim Laskey6be3d8e2006-02-27 10:33:53 +0000509 if (N > 2) Indent.resize(Indent.size() - IndentSize);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000510 }
Jim Laskey3bb78742006-02-25 12:27:03 +0000511
Chris Lattnerd84bb632002-04-16 21:36:08 +0000512 Out << " }";
Brian Gaeke02209042004-08-20 06:00:58 +0000513 } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(CV)) {
514 const Type *ETy = CP->getType()->getElementType();
Misha Brukmanb1c93172005-04-21 23:48:37 +0000515 assert(CP->getNumOperands() > 0 &&
Brian Gaeke02209042004-08-20 06:00:58 +0000516 "Number of operands for a PackedConst must be > 0");
517 Out << '<';
518 Out << ' ';
519 printTypeInt(Out, ETy, TypeTable);
520 WriteAsOperandInternal(Out, CP->getOperand(0),
521 PrintName, TypeTable, Machine);
522 for (unsigned i = 1, e = CP->getNumOperands(); i != e; ++i) {
523 Out << ", ";
524 printTypeInt(Out, ETy, TypeTable);
525 WriteAsOperandInternal(Out, CP->getOperand(i), PrintName,
526 TypeTable, Machine);
527 }
528 Out << " >";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000529 } else if (isa<ConstantPointerNull>(CV)) {
530 Out << "null";
531
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000532 } else if (isa<UndefValue>(CV)) {
533 Out << "undef";
534
Chris Lattner3cd8c562002-07-30 18:54:25 +0000535 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Chris Lattnerb8257222003-03-06 23:23:32 +0000536 Out << CE->getOpcodeName() << " (";
Misha Brukmanb1c93172005-04-21 23:48:37 +0000537
Vikram S. Adveb952b542002-07-14 23:14:45 +0000538 for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
539 printTypeInt(Out, (*OI)->getType(), TypeTable);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000540 WriteAsOperandInternal(Out, *OI, PrintName, TypeTable, Machine);
Vikram S. Adveb952b542002-07-14 23:14:45 +0000541 if (OI+1 != CE->op_end())
Chris Lattner3cd8c562002-07-30 18:54:25 +0000542 Out << ", ";
Vikram S. Adveb952b542002-07-14 23:14:45 +0000543 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000544
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000545 if (CE->isCast()) {
Chris Lattner83b396b2002-08-15 19:37:43 +0000546 Out << " to ";
547 printTypeInt(Out, CE->getType(), TypeTable);
548 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000549
Misha Brukman21bbdb92004-06-04 21:11:51 +0000550 Out << ')';
Chris Lattner83b396b2002-08-15 19:37:43 +0000551
Chris Lattnerd84bb632002-04-16 21:36:08 +0000552 } else {
Vikram S. Adveb952b542002-07-14 23:14:45 +0000553 Out << "<placeholder or erroneous Constant>";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000554 }
555}
556
557
Misha Brukmanc566ca362004-03-02 00:22:19 +0000558/// WriteAsOperand - Write the name of the specified value out to the specified
559/// ostream. This can be useful when you just want to print int %reg126, not
560/// the whole instruction that generated it.
561///
Misha Brukmanb1c93172005-04-21 23:48:37 +0000562static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000563 bool PrintName,
564 std::map<const Type*, std::string> &TypeTable,
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000565 SlotMachine *Machine) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000566 Out << ' ';
Reid Spenceraccd7c72004-07-17 23:47:01 +0000567 if ((PrintName || isa<GlobalValue>(V)) && V->hasName())
Chris Lattner1c343042003-08-22 05:40:38 +0000568 Out << getLLVMName(V->getName());
Reid Spenceraccd7c72004-07-17 23:47:01 +0000569 else {
570 const Constant *CV = dyn_cast<Constant>(V);
Chris Lattnera2d810d2006-01-25 22:26:05 +0000571 if (CV && !isa<GlobalValue>(CV)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000572 WriteConstantInt(Out, CV, PrintName, TypeTable, Machine);
Chris Lattnera2d810d2006-01-25 22:26:05 +0000573 } else if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
574 Out << "asm ";
575 if (IA->hasSideEffects())
576 Out << "sideeffect ";
577 Out << '"';
578 PrintEscapedString(IA->getAsmString(), Out);
579 Out << "\", \"";
580 PrintEscapedString(IA->getConstraintString(), Out);
581 Out << '"';
582 } else {
Chris Lattnerd84bb632002-04-16 21:36:08 +0000583 int Slot;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000584 if (Machine) {
Reid Spencer56010e42004-05-26 21:56:09 +0000585 Slot = Machine->getSlot(V);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000586 } else {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000587 Machine = createSlotMachine(V);
Chris Lattner96749c42006-05-14 18:46:52 +0000588 if (Machine)
Chris Lattner757ee0b2004-06-09 19:41:19 +0000589 Slot = Machine->getSlot(V);
590 else
591 Slot = -1;
Reid Spencer56010e42004-05-26 21:56:09 +0000592 delete Machine;
Chris Lattnerd84bb632002-04-16 21:36:08 +0000593 }
Chris Lattner757ee0b2004-06-09 19:41:19 +0000594 if (Slot != -1)
595 Out << '%' << Slot;
596 else
597 Out << "<badref>";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000598 }
599 }
600}
601
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000602/// WriteAsOperand - Write the name of the specified value out to the specified
603/// ostream. This can be useful when you just want to print int %reg126, not
604/// the whole instruction that generated it.
605///
Chris Lattner189d19f2003-11-21 20:23:48 +0000606std::ostream &llvm::WriteAsOperand(std::ostream &Out, const Value *V,
Misha Brukmanb1c93172005-04-21 23:48:37 +0000607 bool PrintType, bool PrintName,
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000608 const Module *Context) {
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000609 std::map<const Type *, std::string> TypeNames;
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000610 if (Context == 0) Context = getModuleFromVal(V);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000611
Chris Lattner98cf1f52002-11-20 18:36:02 +0000612 if (Context)
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000613 fillTypeNameTable(Context, TypeNames);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000614
615 if (PrintType)
616 printTypeInt(Out, V->getType(), TypeNames);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000617
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000618 WriteAsOperandInternal(Out, V, PrintName, TypeNames, 0);
Chris Lattner5e5abe32001-07-20 19:15:21 +0000619 return Out;
620}
621
Misha Brukmanb1c93172005-04-21 23:48:37 +0000622/// WriteAsOperandInternal - Write the name of the specified value out to
623/// the specified ostream. This can be useful when you just want to print
Reid Spencer58d30f22004-07-04 11:50:43 +0000624/// int %reg126, not the whole instruction that generated it.
625///
Misha Brukmanb1c93172005-04-21 23:48:37 +0000626static void WriteAsOperandInternal(std::ostream &Out, const Type *T,
Reid Spencer58d30f22004-07-04 11:50:43 +0000627 bool PrintName,
628 std::map<const Type*, std::string> &TypeTable,
629 SlotMachine *Machine) {
630 Out << ' ';
631 int Slot;
632 if (Machine) {
633 Slot = Machine->getSlot(T);
634 if (Slot != -1)
635 Out << '%' << Slot;
636 else
637 Out << "<badref>";
638 } else {
639 Out << T->getDescription();
640 }
641}
642
643/// WriteAsOperand - Write the name of the specified value out to the specified
644/// ostream. This can be useful when you just want to print int %reg126, not
645/// the whole instruction that generated it.
646///
647std::ostream &llvm::WriteAsOperand(std::ostream &Out, const Type *Ty,
Misha Brukmanb1c93172005-04-21 23:48:37 +0000648 bool PrintType, bool PrintName,
Reid Spencer58d30f22004-07-04 11:50:43 +0000649 const Module *Context) {
650 std::map<const Type *, std::string> TypeNames;
651 assert(Context != 0 && "Can't write types as operand without module context");
652
653 fillTypeNameTable(Context, TypeNames);
654
655 // if (PrintType)
656 // printTypeInt(Out, V->getType(), TypeNames);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000657
Reid Spencer58d30f22004-07-04 11:50:43 +0000658 printTypeInt(Out, Ty, TypeNames);
659
660 WriteAsOperandInternal(Out, Ty, PrintName, TypeNames, 0);
661 return Out;
662}
663
Chris Lattner189d19f2003-11-21 20:23:48 +0000664namespace llvm {
Chris Lattner2e9fee42001-07-12 23:35:26 +0000665
Chris Lattnerfee714f2001-09-07 16:36:04 +0000666class AssemblyWriter {
Misha Brukmana6619a92004-06-21 21:53:56 +0000667 std::ostream &Out;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000668 SlotMachine &Machine;
Chris Lattner7bfee412001-10-29 16:05:51 +0000669 const Module *TheModule;
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000670 std::map<const Type *, std::string> TypeNames;
Chris Lattner8339f7d2003-10-30 23:41:03 +0000671 AssemblyAnnotationWriter *AnnotationWriter;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000672public:
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000673 inline AssemblyWriter(std::ostream &o, SlotMachine &Mac, const Module *M,
Chris Lattner8339f7d2003-10-30 23:41:03 +0000674 AssemblyAnnotationWriter *AAW)
Misha Brukmana6619a92004-06-21 21:53:56 +0000675 : Out(o), Machine(Mac), TheModule(M), AnnotationWriter(AAW) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000676
677 // If the module has a symbol table, take all global types and stuff their
678 // names into the TypeNames map.
679 //
Chris Lattnerb86620e2001-10-29 16:37:48 +0000680 fillTypeNameTable(M, TypeNames);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000681 }
682
Chris Lattner7bfee412001-10-29 16:05:51 +0000683 inline void write(const Module *M) { printModule(M); }
684 inline void write(const GlobalVariable *G) { printGlobal(G); }
Chris Lattner57698e22002-03-26 18:01:55 +0000685 inline void write(const Function *F) { printFunction(F); }
Chris Lattner7bfee412001-10-29 16:05:51 +0000686 inline void write(const BasicBlock *BB) { printBasicBlock(BB); }
Chris Lattner113f4f42002-06-25 16:13:24 +0000687 inline void write(const Instruction *I) { printInstruction(*I); }
Chris Lattner3462ae32001-12-03 22:26:30 +0000688 inline void write(const Constant *CPV) { printConstant(CPV); }
Chris Lattner7db79582001-11-07 04:21:57 +0000689 inline void write(const Type *Ty) { printType(Ty); }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000690
Chris Lattner1e194682002-04-18 18:53:13 +0000691 void writeOperand(const Value *Op, bool PrintType, bool PrintName = true);
692
Misha Brukman4685e262004-04-28 15:31:21 +0000693 const Module* getModule() { return TheModule; }
694
Misha Brukmand92f54a2004-11-15 19:30:05 +0000695private:
Chris Lattner7bfee412001-10-29 16:05:51 +0000696 void printModule(const Module *M);
697 void printSymbolTable(const SymbolTable &ST);
Chris Lattner3462ae32001-12-03 22:26:30 +0000698 void printConstant(const Constant *CPV);
Chris Lattner7bfee412001-10-29 16:05:51 +0000699 void printGlobal(const GlobalVariable *GV);
Chris Lattner57698e22002-03-26 18:01:55 +0000700 void printFunction(const Function *F);
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000701 void printArgument(const Argument *FA);
Chris Lattner7bfee412001-10-29 16:05:51 +0000702 void printBasicBlock(const BasicBlock *BB);
Chris Lattner113f4f42002-06-25 16:13:24 +0000703 void printInstruction(const Instruction &I);
Chris Lattnerd816b532002-04-13 20:53:41 +0000704
705 // printType - Go to extreme measures to attempt to print out a short,
706 // symbolic version of a type name.
707 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000708 std::ostream &printType(const Type *Ty) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000709 return printTypeInt(Out, Ty, TypeNames);
Chris Lattnerd816b532002-04-13 20:53:41 +0000710 }
711
712 // printTypeAtLeastOneLevel - Print out one level of the possibly complex type
713 // without considering any symbolic types that we may have equal to it.
714 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000715 std::ostream &printTypeAtLeastOneLevel(const Type *Ty);
Chris Lattner7bfee412001-10-29 16:05:51 +0000716
Chris Lattner862e3382001-10-13 06:42:36 +0000717 // printInfoComment - Print a little comment after the instruction indicating
718 // which slot it occupies.
Chris Lattner113f4f42002-06-25 16:13:24 +0000719 void printInfoComment(const Value &V);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000720};
Reid Spencerf43ac622004-05-27 22:04:46 +0000721} // end of llvm namespace
Chris Lattner2f7c9632001-06-06 20:29:01 +0000722
Misha Brukmanc566ca362004-03-02 00:22:19 +0000723/// printTypeAtLeastOneLevel - Print out one level of the possibly complex type
724/// without considering any symbolic types that we may have equal to it.
725///
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000726std::ostream &AssemblyWriter::printTypeAtLeastOneLevel(const Type *Ty) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000727 if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000728 printType(FTy->getReturnType()) << " (";
Chris Lattnerfa829be2004-02-09 04:14:01 +0000729 for (FunctionType::param_iterator I = FTy->param_begin(),
730 E = FTy->param_end(); I != E; ++I) {
731 if (I != FTy->param_begin())
Misha Brukmana6619a92004-06-21 21:53:56 +0000732 Out << ", ";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000733 printType(*I);
Chris Lattnerd816b532002-04-13 20:53:41 +0000734 }
735 if (FTy->isVarArg()) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000736 if (FTy->getNumParams()) Out << ", ";
737 Out << "...";
Chris Lattnerd816b532002-04-13 20:53:41 +0000738 }
Misha Brukmana6619a92004-06-21 21:53:56 +0000739 Out << ')';
Chris Lattner113f4f42002-06-25 16:13:24 +0000740 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000741 Out << "{ ";
Chris Lattnerac6db752004-02-09 04:37:31 +0000742 for (StructType::element_iterator I = STy->element_begin(),
743 E = STy->element_end(); I != E; ++I) {
744 if (I != STy->element_begin())
Misha Brukmana6619a92004-06-21 21:53:56 +0000745 Out << ", ";
Chris Lattnerd816b532002-04-13 20:53:41 +0000746 printType(*I);
747 }
Misha Brukmana6619a92004-06-21 21:53:56 +0000748 Out << " }";
Chris Lattner113f4f42002-06-25 16:13:24 +0000749 } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000750 printType(PTy->getElementType()) << '*';
Chris Lattner113f4f42002-06-25 16:13:24 +0000751 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000752 Out << '[' << ATy->getNumElements() << " x ";
Misha Brukman21bbdb92004-06-04 21:11:51 +0000753 printType(ATy->getElementType()) << ']';
Reid Spencere203d352004-08-20 15:37:30 +0000754 } else if (const PackedType *PTy = dyn_cast<PackedType>(Ty)) {
755 Out << '<' << PTy->getNumElements() << " x ";
756 printType(PTy->getElementType()) << '>';
757 }
Reid Spencerde46e482006-11-02 20:25:50 +0000758 else if (isa<OpaqueType>(Ty)) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000759 Out << "opaque";
Chris Lattnerd816b532002-04-13 20:53:41 +0000760 } else {
Vikram S. Adveb952b542002-07-14 23:14:45 +0000761 if (!Ty->isPrimitiveType())
Misha Brukmana6619a92004-06-21 21:53:56 +0000762 Out << "<unknown derived type>";
Chris Lattnerd816b532002-04-13 20:53:41 +0000763 printType(Ty);
764 }
Misha Brukmana6619a92004-06-21 21:53:56 +0000765 return Out;
Chris Lattnerd816b532002-04-13 20:53:41 +0000766}
767
768
Misha Brukmanb1c93172005-04-21 23:48:37 +0000769void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType,
Reid Spencer56010e42004-05-26 21:56:09 +0000770 bool PrintName) {
Chris Lattner08f7d0c2005-02-24 16:58:29 +0000771 if (Operand != 0) {
772 if (PrintType) { Out << ' '; printType(Operand->getType()); }
773 WriteAsOperandInternal(Out, Operand, PrintName, TypeNames, &Machine);
774 } else {
775 Out << "<null operand!>";
776 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000777}
778
Chris Lattner2f7c9632001-06-06 20:29:01 +0000779
Chris Lattner7bfee412001-10-29 16:05:51 +0000780void AssemblyWriter::printModule(const Module *M) {
Chris Lattner4d8689e2005-03-02 23:12:40 +0000781 if (!M->getModuleIdentifier().empty() &&
Misha Brukmanb1c93172005-04-21 23:48:37 +0000782 // Don't print the ID if it will start a new line (which would
Chris Lattner4d8689e2005-03-02 23:12:40 +0000783 // require a comment char before it).
784 M->getModuleIdentifier().find('\n') == std::string::npos)
785 Out << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
786
Owen Andersone2237542006-10-18 02:21:12 +0000787 if (!M->getDataLayout().empty())
Chris Lattner04897162006-10-22 06:06:56 +0000788 Out << "target datalayout = \"" << M->getDataLayout() << "\"\n";
Owen Andersone2237542006-10-18 02:21:12 +0000789
Chris Lattner8068e0c2003-08-24 13:48:48 +0000790 switch (M->getEndianness()) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000791 case Module::LittleEndian: Out << "target endian = little\n"; break;
792 case Module::BigEndian: Out << "target endian = big\n"; break;
Chris Lattner8068e0c2003-08-24 13:48:48 +0000793 case Module::AnyEndianness: break;
794 }
795 switch (M->getPointerSize()) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000796 case Module::Pointer32: Out << "target pointersize = 32\n"; break;
797 case Module::Pointer64: Out << "target pointersize = 64\n"; break;
Chris Lattner8068e0c2003-08-24 13:48:48 +0000798 case Module::AnyPointerSize: break;
799 }
Reid Spencer48f98c82004-07-25 21:44:54 +0000800 if (!M->getTargetTriple().empty())
Reid Spencerffec7df2004-07-25 21:29:43 +0000801 Out << "target triple = \"" << M->getTargetTriple() << "\"\n";
Misha Brukmanb1c93172005-04-21 23:48:37 +0000802
Chris Lattnereef2fe72006-01-24 04:13:11 +0000803 if (!M->getModuleInlineAsm().empty()) {
Chris Lattnerefaf35d2006-01-24 00:45:30 +0000804 // Split the string into lines, to make it easier to read the .ll file.
Chris Lattnereef2fe72006-01-24 04:13:11 +0000805 std::string Asm = M->getModuleInlineAsm();
Chris Lattnerefaf35d2006-01-24 00:45:30 +0000806 size_t CurPos = 0;
807 size_t NewLine = Asm.find_first_of('\n', CurPos);
808 while (NewLine != std::string::npos) {
809 // We found a newline, print the portion of the asm string from the
810 // last newline up to this newline.
811 Out << "module asm \"";
812 PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.begin()+NewLine),
813 Out);
814 Out << "\"\n";
815 CurPos = NewLine+1;
816 NewLine = Asm.find_first_of('\n', CurPos);
817 }
Chris Lattner3acaf5c2006-01-24 00:40:17 +0000818 Out << "module asm \"";
Chris Lattnerefaf35d2006-01-24 00:45:30 +0000819 PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.end()), Out);
Chris Lattner6ed87bd2006-01-23 23:03:36 +0000820 Out << "\"\n";
821 }
822
Chris Lattner2cdd49d2004-09-14 05:06:58 +0000823 // Loop over the dependent libraries and emit them.
Chris Lattner730cfe42004-09-14 04:51:44 +0000824 Module::lib_iterator LI = M->lib_begin();
825 Module::lib_iterator LE = M->lib_end();
Reid Spencer48f98c82004-07-25 21:44:54 +0000826 if (LI != LE) {
Chris Lattner730cfe42004-09-14 04:51:44 +0000827 Out << "deplibs = [ ";
828 while (LI != LE) {
Chris Lattner2cdd49d2004-09-14 05:06:58 +0000829 Out << '"' << *LI << '"';
Reid Spencerffec7df2004-07-25 21:29:43 +0000830 ++LI;
Chris Lattner730cfe42004-09-14 04:51:44 +0000831 if (LI != LE)
832 Out << ", ";
Reid Spencerffec7df2004-07-25 21:29:43 +0000833 }
834 Out << " ]\n";
Reid Spencercc5ff642004-07-25 18:08:18 +0000835 }
Reid Spencerb9e08772004-09-13 23:44:23 +0000836
Chris Lattner2cdd49d2004-09-14 05:06:58 +0000837 // Loop over the symbol table, emitting all named constants.
Chris Lattner98cf1f52002-11-20 18:36:02 +0000838 printSymbolTable(M->getSymbolTable());
Misha Brukmanb1c93172005-04-21 23:48:37 +0000839
Chris Lattner531f9e92005-03-15 04:54:21 +0000840 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end(); I != E; ++I)
Chris Lattner113f4f42002-06-25 16:13:24 +0000841 printGlobal(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000842
Misha Brukmana6619a92004-06-21 21:53:56 +0000843 Out << "\nimplementation ; Functions:\n";
Misha Brukmanb1c93172005-04-21 23:48:37 +0000844
Chris Lattner2cdd49d2004-09-14 05:06:58 +0000845 // Output all of the functions.
Chris Lattner113f4f42002-06-25 16:13:24 +0000846 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
847 printFunction(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000848}
849
Chris Lattner7bfee412001-10-29 16:05:51 +0000850void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000851 if (GV->hasName()) Out << getLLVMName(GV->getName()) << " = ";
Chris Lattner37798642001-09-18 04:01:05 +0000852
Misha Brukmanb1c93172005-04-21 23:48:37 +0000853 if (!GV->hasInitializer())
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000854 switch (GV->getLinkage()) {
855 case GlobalValue::DLLImportLinkage: Out << "dllimport "; break;
856 case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break;
857 default: Out << "external "; break;
858 }
Chris Lattner379a8d22003-04-16 20:28:45 +0000859 else
860 switch (GV->getLinkage()) {
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000861 case GlobalValue::InternalLinkage: Out << "internal "; break;
862 case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
863 case GlobalValue::WeakLinkage: Out << "weak "; break;
864 case GlobalValue::AppendingLinkage: Out << "appending "; break;
865 case GlobalValue::DLLImportLinkage: Out << "dllimport "; break;
866 case GlobalValue::DLLExportLinkage: Out << "dllexport "; break;
867 case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break;
868 case GlobalValue::ExternalLinkage: break;
Misha Brukman70457632004-11-14 21:04:34 +0000869 case GlobalValue::GhostLinkage:
Bill Wendlingdfc91892006-11-28 02:09:03 +0000870 llvm_cerr << "GhostLinkage not allowed in AsmWriter!\n";
Misha Brukman70457632004-11-14 21:04:34 +0000871 abort();
Chris Lattner379a8d22003-04-16 20:28:45 +0000872 }
Chris Lattner37798642001-09-18 04:01:05 +0000873
Misha Brukmana6619a92004-06-21 21:53:56 +0000874 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattner2413b162001-12-04 00:03:30 +0000875 printType(GV->getType()->getElementType());
Chris Lattner37798642001-09-18 04:01:05 +0000876
Reid Spenceraccd7c72004-07-17 23:47:01 +0000877 if (GV->hasInitializer()) {
878 Constant* C = cast<Constant>(GV->getInitializer());
879 assert(C && "GlobalVar initializer isn't constant?");
Reid Spencerc9c90cf2004-07-18 01:04:19 +0000880 writeOperand(GV->getInitializer(), false, isa<GlobalValue>(C));
Reid Spenceraccd7c72004-07-17 23:47:01 +0000881 }
Chris Lattnerf8a974d2005-11-06 06:48:53 +0000882
Chris Lattner4b96c542005-11-12 00:10:19 +0000883 if (GV->hasSection())
884 Out << ", section \"" << GV->getSection() << '"';
885 if (GV->getAlignment())
Chris Lattnerf8a974d2005-11-06 06:48:53 +0000886 Out << ", align " << GV->getAlignment();
Chris Lattner4b96c542005-11-12 00:10:19 +0000887
Chris Lattner113f4f42002-06-25 16:13:24 +0000888 printInfoComment(*GV);
Misha Brukmana6619a92004-06-21 21:53:56 +0000889 Out << "\n";
Chris Lattnerda975502001-09-10 07:58:01 +0000890}
891
Chris Lattnerfee714f2001-09-07 16:36:04 +0000892
Reid Spencere7e96712004-05-25 08:53:40 +0000893// printSymbolTable - Run through symbol table looking for constants
894// and types. Emit their declarations.
Chris Lattner7bfee412001-10-29 16:05:51 +0000895void AssemblyWriter::printSymbolTable(const SymbolTable &ST) {
Reid Spencere7e96712004-05-25 08:53:40 +0000896
897 // Print the types.
898 for (SymbolTable::type_const_iterator TI = ST.type_begin();
899 TI != ST.type_end(); ++TI ) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000900 Out << "\t" << getLLVMName(TI->first) << " = type ";
Reid Spencere7e96712004-05-25 08:53:40 +0000901
902 // Make sure we print out at least one level of the type structure, so
903 // that we do not get %FILE = type %FILE
904 //
905 printTypeAtLeastOneLevel(TI->second) << "\n";
906 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000907
Reid Spencere7e96712004-05-25 08:53:40 +0000908 // Print the constants, in type plane order.
909 for (SymbolTable::plane_const_iterator PI = ST.plane_begin();
910 PI != ST.plane_end(); ++PI ) {
911 SymbolTable::value_const_iterator VI = ST.value_begin(PI->first);
912 SymbolTable::value_const_iterator VE = ST.value_end(PI->first);
913
914 for (; VI != VE; ++VI) {
Reid Spenceraccd7c72004-07-17 23:47:01 +0000915 const Value* V = VI->second;
916 const Constant *CPV = dyn_cast<Constant>(V) ;
917 if (CPV && !isa<GlobalValue>(V)) {
Reid Spencer56010e42004-05-26 21:56:09 +0000918 printConstant(CPV);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000919 }
920 }
Chris Lattnera7620d92001-07-15 06:35:59 +0000921 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000922}
923
924
Misha Brukmanc566ca362004-03-02 00:22:19 +0000925/// printConstant - Print out a constant pool entry...
926///
Chris Lattner3462ae32001-12-03 22:26:30 +0000927void AssemblyWriter::printConstant(const Constant *CPV) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000928 // Don't print out unnamed constants, they will be inlined
929 if (!CPV->hasName()) return;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000930
Chris Lattneree998be2001-07-26 16:29:38 +0000931 // Print out name...
Misha Brukmana6619a92004-06-21 21:53:56 +0000932 Out << "\t" << getLLVMName(CPV->getName()) << " =";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000933
934 // Write the value out now...
Chris Lattnerd84bb632002-04-16 21:36:08 +0000935 writeOperand(CPV, true, false);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000936
Chris Lattner113f4f42002-06-25 16:13:24 +0000937 printInfoComment(*CPV);
Misha Brukmana6619a92004-06-21 21:53:56 +0000938 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000939}
940
Misha Brukmanc566ca362004-03-02 00:22:19 +0000941/// printFunction - Print all aspects of a function.
942///
Chris Lattner113f4f42002-06-25 16:13:24 +0000943void AssemblyWriter::printFunction(const Function *F) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000944 // Print out the return type and name...
Misha Brukmana6619a92004-06-21 21:53:56 +0000945 Out << "\n";
Chris Lattner379a8d22003-04-16 20:28:45 +0000946
Chris Lattner97e36f22004-12-05 06:44:09 +0000947 // Ensure that no local symbols conflict with global symbols.
948 const_cast<Function*>(F)->renameLocalSymbols();
949
Misha Brukmana6619a92004-06-21 21:53:56 +0000950 if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +0000951
Chris Lattner379a8d22003-04-16 20:28:45 +0000952 if (F->isExternal())
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000953 switch (F->getLinkage()) {
954 case GlobalValue::DLLImportLinkage: Out << "declare dllimport "; break;
955 case GlobalValue::ExternalWeakLinkage: Out << "declare extern_weak "; break;
956 default: Out << "declare ";
957 }
Chris Lattner379a8d22003-04-16 20:28:45 +0000958 else
959 switch (F->getLinkage()) {
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000960 case GlobalValue::InternalLinkage: Out << "internal "; break;
961 case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
962 case GlobalValue::WeakLinkage: Out << "weak "; break;
963 case GlobalValue::AppendingLinkage: Out << "appending "; break;
964 case GlobalValue::DLLImportLinkage: Out << "dllimport "; break;
965 case GlobalValue::DLLExportLinkage: Out << "dllexport "; break;
966 case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break;
Chris Lattner379a8d22003-04-16 20:28:45 +0000967 case GlobalValue::ExternalLinkage: break;
Misha Brukman70457632004-11-14 21:04:34 +0000968 case GlobalValue::GhostLinkage:
Bill Wendlingdfc91892006-11-28 02:09:03 +0000969 llvm_cerr << "GhostLinkage not allowed in AsmWriter!\n";
Misha Brukman70457632004-11-14 21:04:34 +0000970 abort();
Chris Lattner379a8d22003-04-16 20:28:45 +0000971 }
972
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000973 // Print the calling convention.
974 switch (F->getCallingConv()) {
975 case CallingConv::C: break; // default
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +0000976 case CallingConv::CSRet: Out << "csretcc "; break;
977 case CallingConv::Fast: Out << "fastcc "; break;
978 case CallingConv::Cold: Out << "coldcc "; break;
979 case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break;
980 case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break;
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000981 default: Out << "cc" << F->getCallingConv() << " "; break;
982 }
983
Misha Brukman21bbdb92004-06-04 21:11:51 +0000984 printType(F->getReturnType()) << ' ';
Chris Lattner5b337482003-10-18 05:57:43 +0000985 if (!F->getName().empty())
Misha Brukmana6619a92004-06-21 21:53:56 +0000986 Out << getLLVMName(F->getName());
Chris Lattner5b337482003-10-18 05:57:43 +0000987 else
Misha Brukmana6619a92004-06-21 21:53:56 +0000988 Out << "\"\"";
989 Out << '(';
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000990 Machine.incorporateFunction(F);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000991
Chris Lattner7bfee412001-10-29 16:05:51 +0000992 // Loop over the arguments, printing them...
Chris Lattner113f4f42002-06-25 16:13:24 +0000993 const FunctionType *FT = F->getFunctionType();
Chris Lattnerfee714f2001-09-07 16:36:04 +0000994
Chris Lattner531f9e92005-03-15 04:54:21 +0000995 for(Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I)
Chris Lattner149376d2002-10-13 20:57:00 +0000996 printArgument(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000997
998 // Finish printing arguments...
Chris Lattner113f4f42002-06-25 16:13:24 +0000999 if (FT->isVarArg()) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001000 if (FT->getNumParams()) Out << ", ";
1001 Out << "..."; // Output varargs portion of signature!
Chris Lattnerfee714f2001-09-07 16:36:04 +00001002 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001003 Out << ')';
Chris Lattnerfee714f2001-09-07 16:36:04 +00001004
Chris Lattner4b96c542005-11-12 00:10:19 +00001005 if (F->hasSection())
1006 Out << " section \"" << F->getSection() << '"';
Chris Lattnerf8a974d2005-11-06 06:48:53 +00001007 if (F->getAlignment())
1008 Out << " align " << F->getAlignment();
Chris Lattner4b96c542005-11-12 00:10:19 +00001009
Chris Lattner113f4f42002-06-25 16:13:24 +00001010 if (F->isExternal()) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001011 Out << "\n";
Chris Lattnerb2f02e52002-05-06 03:00:40 +00001012 } else {
Misha Brukmana6619a92004-06-21 21:53:56 +00001013 Out << " {";
Misha Brukmanb1c93172005-04-21 23:48:37 +00001014
Chris Lattner6915f8f2002-04-07 22:49:37 +00001015 // Output all of its basic blocks... for the function
Chris Lattner113f4f42002-06-25 16:13:24 +00001016 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
1017 printBasicBlock(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +00001018
Misha Brukmana6619a92004-06-21 21:53:56 +00001019 Out << "}\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +00001020 }
1021
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001022 Machine.purgeFunction();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001023}
1024
Misha Brukmanc566ca362004-03-02 00:22:19 +00001025/// printArgument - This member is called for every argument that is passed into
1026/// the function. Simply print it out
1027///
Chris Lattner2e9fa6d2002-04-09 19:48:49 +00001028void AssemblyWriter::printArgument(const Argument *Arg) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001029 // Insert commas as we go... the first arg doesn't get a comma
Chris Lattnerfb972ad2005-03-15 05:03:36 +00001030 if (Arg != Arg->getParent()->arg_begin()) Out << ", ";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001031
1032 // Output type...
Chris Lattner7bfee412001-10-29 16:05:51 +00001033 printType(Arg->getType());
Misha Brukmanb1c93172005-04-21 23:48:37 +00001034
Chris Lattner2f7c9632001-06-06 20:29:01 +00001035 // Output name, if available...
1036 if (Arg->hasName())
Misha Brukmana6619a92004-06-21 21:53:56 +00001037 Out << ' ' << getLLVMName(Arg->getName());
Chris Lattner2f7c9632001-06-06 20:29:01 +00001038}
1039
Misha Brukmanc566ca362004-03-02 00:22:19 +00001040/// printBasicBlock - This member is called for each basic block in a method.
1041///
Chris Lattner7bfee412001-10-29 16:05:51 +00001042void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001043 if (BB->hasName()) { // Print out the label if it exists...
Alkis Evlogimenos991d6ad2004-12-10 05:41:10 +00001044 Out << "\n" << getLLVMName(BB->getName(), false) << ':';
Chris Lattner408dbdb2002-05-14 16:02:05 +00001045 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Misha Brukmana6619a92004-06-21 21:53:56 +00001046 Out << "\n; <label>:";
Chris Lattner757ee0b2004-06-09 19:41:19 +00001047 int Slot = Machine.getSlot(BB);
1048 if (Slot != -1)
Misha Brukmana6619a92004-06-21 21:53:56 +00001049 Out << Slot;
Chris Lattner757ee0b2004-06-09 19:41:19 +00001050 else
Misha Brukmana6619a92004-06-21 21:53:56 +00001051 Out << "<badref>";
Chris Lattner58185f22002-10-02 19:38:55 +00001052 }
Chris Lattner2447ef52003-11-20 00:09:43 +00001053
1054 if (BB->getParent() == 0)
Misha Brukmana6619a92004-06-21 21:53:56 +00001055 Out << "\t\t; Error: Block without parent!";
Chris Lattner2447ef52003-11-20 00:09:43 +00001056 else {
1057 if (BB != &BB->getParent()->front()) { // Not the entry block?
1058 // Output predecessors for the block...
Misha Brukmana6619a92004-06-21 21:53:56 +00001059 Out << "\t\t;";
Chris Lattner2447ef52003-11-20 00:09:43 +00001060 pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
Misha Brukmanb1c93172005-04-21 23:48:37 +00001061
Chris Lattner2447ef52003-11-20 00:09:43 +00001062 if (PI == PE) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001063 Out << " No predecessors!";
Chris Lattner2447ef52003-11-20 00:09:43 +00001064 } else {
Misha Brukmana6619a92004-06-21 21:53:56 +00001065 Out << " preds =";
Chris Lattner00211f12003-11-16 22:59:57 +00001066 writeOperand(*PI, false, true);
Chris Lattner2447ef52003-11-20 00:09:43 +00001067 for (++PI; PI != PE; ++PI) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001068 Out << ',';
Chris Lattner2447ef52003-11-20 00:09:43 +00001069 writeOperand(*PI, false, true);
1070 }
Chris Lattner00211f12003-11-16 22:59:57 +00001071 }
Chris Lattner58185f22002-10-02 19:38:55 +00001072 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001073 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001074
Misha Brukmana6619a92004-06-21 21:53:56 +00001075 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001076
Misha Brukmana6619a92004-06-21 21:53:56 +00001077 if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001078
Chris Lattnerfee714f2001-09-07 16:36:04 +00001079 // Output all of the instructions in the basic block...
Chris Lattner113f4f42002-06-25 16:13:24 +00001080 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
1081 printInstruction(*I);
Chris Lattner96cdd272004-03-08 18:51:45 +00001082
Misha Brukmana6619a92004-06-21 21:53:56 +00001083 if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001084}
1085
Chris Lattner862e3382001-10-13 06:42:36 +00001086
Misha Brukmanc566ca362004-03-02 00:22:19 +00001087/// printInfoComment - Print a little comment after the instruction indicating
1088/// which slot it occupies.
1089///
Chris Lattner113f4f42002-06-25 16:13:24 +00001090void AssemblyWriter::printInfoComment(const Value &V) {
1091 if (V.getType() != Type::VoidTy) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001092 Out << "\t\t; <";
Misha Brukman21bbdb92004-06-04 21:11:51 +00001093 printType(V.getType()) << '>';
Chris Lattner862e3382001-10-13 06:42:36 +00001094
Chris Lattner113f4f42002-06-25 16:13:24 +00001095 if (!V.hasName()) {
Chris Lattner757ee0b2004-06-09 19:41:19 +00001096 int SlotNum = Machine.getSlot(&V);
1097 if (SlotNum == -1)
Misha Brukmana6619a92004-06-21 21:53:56 +00001098 Out << ":<badref>";
Reid Spencer8beac692004-06-09 15:26:53 +00001099 else
Misha Brukmana6619a92004-06-21 21:53:56 +00001100 Out << ':' << SlotNum; // Print out the def slot taken.
Chris Lattner862e3382001-10-13 06:42:36 +00001101 }
Chris Lattnerb6c21db2005-02-01 01:24:01 +00001102 Out << " [#uses=" << V.getNumUses() << ']'; // Output # uses
Chris Lattner862e3382001-10-13 06:42:36 +00001103 }
1104}
1105
Reid Spencere7141c82006-08-28 01:02:49 +00001106// This member is called for each Instruction in a function..
Chris Lattner113f4f42002-06-25 16:13:24 +00001107void AssemblyWriter::printInstruction(const Instruction &I) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001108 if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001109
Misha Brukmana6619a92004-06-21 21:53:56 +00001110 Out << "\t";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001111
1112 // Print out name if it exists...
Chris Lattner113f4f42002-06-25 16:13:24 +00001113 if (I.hasName())
Misha Brukmana6619a92004-06-21 21:53:56 +00001114 Out << getLLVMName(I.getName()) << " = ";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001115
Chris Lattner06038452005-05-06 05:51:46 +00001116 // If this is a volatile load or store, print out the volatile marker.
Chris Lattner504f9242003-09-08 17:45:59 +00001117 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
Chris Lattner06038452005-05-06 05:51:46 +00001118 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile())) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001119 Out << "volatile ";
Chris Lattner06038452005-05-06 05:51:46 +00001120 } else if (isa<CallInst>(I) && cast<CallInst>(I).isTailCall()) {
1121 // If this is a call, check if it's a tail call.
1122 Out << "tail ";
1123 }
Chris Lattner504f9242003-09-08 17:45:59 +00001124
Chris Lattner2f7c9632001-06-06 20:29:01 +00001125 // Print out the opcode...
Misha Brukmana6619a92004-06-21 21:53:56 +00001126 Out << I.getOpcodeName();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001127
1128 // Print out the type of the operands...
Chris Lattner113f4f42002-06-25 16:13:24 +00001129 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001130
1131 // Special case conditional branches to swizzle the condition out to the front
Chris Lattner113f4f42002-06-25 16:13:24 +00001132 if (isa<BranchInst>(I) && I.getNumOperands() > 1) {
1133 writeOperand(I.getOperand(2), true);
Misha Brukmana6619a92004-06-21 21:53:56 +00001134 Out << ',';
Chris Lattner2f7c9632001-06-06 20:29:01 +00001135 writeOperand(Operand, true);
Misha Brukmana6619a92004-06-21 21:53:56 +00001136 Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001137 writeOperand(I.getOperand(1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001138
Chris Lattner8d48df22002-04-13 18:34:38 +00001139 } else if (isa<SwitchInst>(I)) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001140 // Special case switch statement to get formatting nice and correct...
Misha Brukmana6619a92004-06-21 21:53:56 +00001141 writeOperand(Operand , true); Out << ',';
1142 writeOperand(I.getOperand(1), true); Out << " [";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001143
Chris Lattner113f4f42002-06-25 16:13:24 +00001144 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001145 Out << "\n\t\t";
1146 writeOperand(I.getOperand(op ), true); Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001147 writeOperand(I.getOperand(op+1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001148 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001149 Out << "\n\t]";
Chris Lattnerda558102001-10-02 03:41:24 +00001150 } else if (isa<PHINode>(I)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001151 Out << ' ';
Chris Lattner113f4f42002-06-25 16:13:24 +00001152 printType(I.getType());
Misha Brukmana6619a92004-06-21 21:53:56 +00001153 Out << ' ';
Chris Lattner2f7c9632001-06-06 20:29:01 +00001154
Chris Lattner113f4f42002-06-25 16:13:24 +00001155 for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001156 if (op) Out << ", ";
Misha Brukmanb1c93172005-04-21 23:48:37 +00001157 Out << '[';
Misha Brukmana6619a92004-06-21 21:53:56 +00001158 writeOperand(I.getOperand(op ), false); Out << ',';
1159 writeOperand(I.getOperand(op+1), false); Out << " ]";
Chris Lattner931ef3b2001-06-11 15:04:20 +00001160 }
Chris Lattner862e3382001-10-13 06:42:36 +00001161 } else if (isa<ReturnInst>(I) && !Operand) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001162 Out << " void";
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001163 } else if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
1164 // Print the calling convention being used.
1165 switch (CI->getCallingConv()) {
1166 case CallingConv::C: break; // default
Chris Lattner29d20852006-05-19 21:58:52 +00001167 case CallingConv::CSRet: Out << " csretcc"; break;
1168 case CallingConv::Fast: Out << " fastcc"; break;
1169 case CallingConv::Cold: Out << " coldcc"; break;
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +00001170 case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break;
1171 case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break;
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001172 default: Out << " cc" << CI->getCallingConv(); break;
1173 }
1174
Chris Lattner463d6a52003-08-05 15:34:45 +00001175 const PointerType *PTy = cast<PointerType>(Operand->getType());
1176 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1177 const Type *RetTy = FTy->getReturnType();
Chris Lattner2f2d9472001-11-06 21:28:12 +00001178
Chris Lattner463d6a52003-08-05 15:34:45 +00001179 // If possible, print out the short form of the call instruction. We can
Chris Lattner6915f8f2002-04-07 22:49:37 +00001180 // only do this if the first argument is a pointer to a nonvararg function,
Chris Lattner463d6a52003-08-05 15:34:45 +00001181 // and if the return type is not a pointer to a function.
Chris Lattner2f2d9472001-11-06 21:28:12 +00001182 //
Chris Lattner463d6a52003-08-05 15:34:45 +00001183 if (!FTy->isVarArg() &&
Misha Brukmanb1c93172005-04-21 23:48:37 +00001184 (!isa<PointerType>(RetTy) ||
Chris Lattnerd9a36a62002-07-25 20:58:51 +00001185 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001186 Out << ' '; printType(RetTy);
Chris Lattner2f2d9472001-11-06 21:28:12 +00001187 writeOperand(Operand, false);
1188 } else {
1189 writeOperand(Operand, true);
1190 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001191 Out << '(';
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001192 if (CI->getNumOperands() > 1) writeOperand(CI->getOperand(1), true);
Chris Lattner113f4f42002-06-25 16:13:24 +00001193 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; ++op) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001194 Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001195 writeOperand(I.getOperand(op), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001196 }
1197
Misha Brukmana6619a92004-06-21 21:53:56 +00001198 Out << " )";
Chris Lattner113f4f42002-06-25 16:13:24 +00001199 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Chris Lattner463d6a52003-08-05 15:34:45 +00001200 const PointerType *PTy = cast<PointerType>(Operand->getType());
1201 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1202 const Type *RetTy = FTy->getReturnType();
1203
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001204 // Print the calling convention being used.
1205 switch (II->getCallingConv()) {
1206 case CallingConv::C: break; // default
Chris Lattner29d20852006-05-19 21:58:52 +00001207 case CallingConv::CSRet: Out << " csretcc"; break;
1208 case CallingConv::Fast: Out << " fastcc"; break;
1209 case CallingConv::Cold: Out << " coldcc"; break;
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +00001210 case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break;
1211 case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break;
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001212 default: Out << " cc" << II->getCallingConv(); break;
1213 }
1214
Chris Lattner463d6a52003-08-05 15:34:45 +00001215 // If possible, print out the short form of the invoke instruction. We can
1216 // only do this if the first argument is a pointer to a nonvararg function,
1217 // and if the return type is not a pointer to a function.
1218 //
1219 if (!FTy->isVarArg() &&
Misha Brukmanb1c93172005-04-21 23:48:37 +00001220 (!isa<PointerType>(RetTy) ||
Chris Lattner463d6a52003-08-05 15:34:45 +00001221 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001222 Out << ' '; printType(RetTy);
Chris Lattner463d6a52003-08-05 15:34:45 +00001223 writeOperand(Operand, false);
1224 } else {
1225 writeOperand(Operand, true);
1226 }
1227
Misha Brukmana6619a92004-06-21 21:53:56 +00001228 Out << '(';
Chris Lattner113f4f42002-06-25 16:13:24 +00001229 if (I.getNumOperands() > 3) writeOperand(I.getOperand(3), true);
1230 for (unsigned op = 4, Eop = I.getNumOperands(); op < Eop; ++op) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001231 Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001232 writeOperand(I.getOperand(op), true);
Chris Lattner862e3382001-10-13 06:42:36 +00001233 }
1234
Misha Brukmana6619a92004-06-21 21:53:56 +00001235 Out << " )\n\t\t\tto";
Chris Lattner862e3382001-10-13 06:42:36 +00001236 writeOperand(II->getNormalDest(), true);
Misha Brukmana6619a92004-06-21 21:53:56 +00001237 Out << " unwind";
Chris Lattnerfae8ab32004-02-08 21:44:31 +00001238 writeOperand(II->getUnwindDest(), true);
Chris Lattner862e3382001-10-13 06:42:36 +00001239
Chris Lattner113f4f42002-06-25 16:13:24 +00001240 } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(&I)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001241 Out << ' ';
Chris Lattner8d48df22002-04-13 18:34:38 +00001242 printType(AI->getType()->getElementType());
1243 if (AI->isArrayAllocation()) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001244 Out << ',';
Chris Lattner8d48df22002-04-13 18:34:38 +00001245 writeOperand(AI->getArraySize(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001246 }
Nate Begeman848622f2005-11-05 09:21:28 +00001247 if (AI->getAlignment()) {
Chris Lattner7aeee3a2005-11-05 21:20:34 +00001248 Out << ", align " << AI->getAlignment();
Nate Begeman848622f2005-11-05 09:21:28 +00001249 }
Chris Lattner862e3382001-10-13 06:42:36 +00001250 } else if (isa<CastInst>(I)) {
Chris Lattnerc96e96b2003-11-17 01:17:04 +00001251 if (Operand) writeOperand(Operand, true); // Work with broken code
Misha Brukmana6619a92004-06-21 21:53:56 +00001252 Out << " to ";
Chris Lattner113f4f42002-06-25 16:13:24 +00001253 printType(I.getType());
Chris Lattner5b337482003-10-18 05:57:43 +00001254 } else if (isa<VAArgInst>(I)) {
Chris Lattnerc96e96b2003-11-17 01:17:04 +00001255 if (Operand) writeOperand(Operand, true); // Work with broken code
Misha Brukmana6619a92004-06-21 21:53:56 +00001256 Out << ", ";
Chris Lattnerf70da102003-05-08 02:44:12 +00001257 printType(I.getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +00001258 } else if (Operand) { // Print the normal way...
1259
Misha Brukmanb1c93172005-04-21 23:48:37 +00001260 // PrintAllTypes - Instructions who have operands of all the same type
Chris Lattner2f7c9632001-06-06 20:29:01 +00001261 // omit the type from all but the first operand. If the instruction has
1262 // different type operands (for example br), then they are all printed.
1263 bool PrintAllTypes = false;
1264 const Type *TheType = Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001265
Chris Lattner52bd5cb92004-03-12 05:53:14 +00001266 // Shift Left & Right print both types even for Ubyte LHS, and select prints
1267 // types even if all operands are bools.
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001268 if (isa<ShiftInst>(I) || isa<SelectInst>(I) || isa<StoreInst>(I) ||
1269 isa<ShuffleVectorInst>(I)) {
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00001270 PrintAllTypes = true;
1271 } else {
1272 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
1273 Operand = I.getOperand(i);
1274 if (Operand->getType() != TheType) {
1275 PrintAllTypes = true; // We have differing types! Print them all!
1276 break;
1277 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001278 }
1279 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001280
Chris Lattner7bfee412001-10-29 16:05:51 +00001281 if (!PrintAllTypes) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001282 Out << ' ';
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00001283 printType(TheType);
Chris Lattner7bfee412001-10-29 16:05:51 +00001284 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001285
Chris Lattner113f4f42002-06-25 16:13:24 +00001286 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001287 if (i) Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001288 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001289 }
1290 }
1291
Chris Lattner862e3382001-10-13 06:42:36 +00001292 printInfoComment(I);
Misha Brukmana6619a92004-06-21 21:53:56 +00001293 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001294}
1295
1296
1297//===----------------------------------------------------------------------===//
1298// External Interface declarations
1299//===----------------------------------------------------------------------===//
1300
Chris Lattner8339f7d2003-10-30 23:41:03 +00001301void Module::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001302 SlotMachine SlotTable(this);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001303 AssemblyWriter W(o, SlotTable, this, AAW);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001304 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001305}
1306
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001307void GlobalVariable::print(std::ostream &o) const {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001308 SlotMachine SlotTable(getParent());
Chris Lattner8339f7d2003-10-30 23:41:03 +00001309 AssemblyWriter W(o, SlotTable, getParent(), 0);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001310 W.write(this);
Chris Lattneradfe0d12001-09-10 20:08:19 +00001311}
1312
Chris Lattner8339f7d2003-10-30 23:41:03 +00001313void Function::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001314 SlotMachine SlotTable(getParent());
Chris Lattner8339f7d2003-10-30 23:41:03 +00001315 AssemblyWriter W(o, SlotTable, getParent(), AAW);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001316
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001317 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001318}
1319
Chris Lattnereef2fe72006-01-24 04:13:11 +00001320void InlineAsm::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Chris Lattnera2d810d2006-01-25 22:26:05 +00001321 WriteAsOperand(o, this, true, true, 0);
Chris Lattnereef2fe72006-01-24 04:13:11 +00001322}
1323
Chris Lattner8339f7d2003-10-30 23:41:03 +00001324void BasicBlock::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001325 SlotMachine SlotTable(getParent());
Misha Brukmanb1c93172005-04-21 23:48:37 +00001326 AssemblyWriter W(o, SlotTable,
Chris Lattner8339f7d2003-10-30 23:41:03 +00001327 getParent() ? getParent()->getParent() : 0, AAW);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001328 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001329}
1330
Chris Lattner8339f7d2003-10-30 23:41:03 +00001331void Instruction::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001332 const Function *F = getParent() ? getParent()->getParent() : 0;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001333 SlotMachine SlotTable(F);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001334 AssemblyWriter W(o, SlotTable, F ? F->getParent() : 0, AAW);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001335
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001336 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001337}
Chris Lattner7db79582001-11-07 04:21:57 +00001338
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001339void Constant::print(std::ostream &o) const {
1340 if (this == 0) { o << "<null> constant value\n"; return; }
Chris Lattner7d734802002-09-10 15:53:49 +00001341
Misha Brukman21bbdb92004-06-04 21:11:51 +00001342 o << ' ' << getType()->getDescription() << ' ';
Evan Cheng5b19a802006-03-01 22:17:00 +00001343
1344 std::map<const Type *, std::string> TypeTable;
1345 WriteConstantInt(o, this, false, TypeTable, 0);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001346}
1347
Misha Brukmanb1c93172005-04-21 23:48:37 +00001348void Type::print(std::ostream &o) const {
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001349 if (this == 0)
1350 o << "<null Type>";
1351 else
1352 o << getDescription();
1353}
1354
Chris Lattner2e9fa6d2002-04-09 19:48:49 +00001355void Argument::print(std::ostream &o) const {
Chris Lattnere52ed452004-07-13 23:14:34 +00001356 WriteAsOperand(o, this, true, true,
1357 getParent() ? getParent()->getParent() : 0);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001358}
1359
Reid Spencer52641832004-05-25 18:14:38 +00001360// Value::dump - allow easy printing of Values from the debugger.
1361// Located here because so much of the needed functionality is here.
Bill Wendlingdfc91892006-11-28 02:09:03 +00001362void Value::dump() const { print(std::cerr); llvm_cerr << '\n'; }
Reid Spencer52641832004-05-25 18:14:38 +00001363
1364// Type::dump - allow easy printing of Values from the debugger.
1365// Located here because so much of the needed functionality is here.
Bill Wendlingdfc91892006-11-28 02:09:03 +00001366void Type::dump() const { print(std::cerr); llvm_cerr << '\n'; }
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001367
1368//===----------------------------------------------------------------------===//
1369// CachedWriter Class Implementation
1370//===----------------------------------------------------------------------===//
1371
Chris Lattner7db79582001-11-07 04:21:57 +00001372void CachedWriter::setModule(const Module *M) {
1373 delete SC; delete AW;
1374 if (M) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001375 SC = new SlotMachine(M );
Misha Brukman21bbdb92004-06-04 21:11:51 +00001376 AW = new AssemblyWriter(Out, *SC, M, 0);
Chris Lattner7db79582001-11-07 04:21:57 +00001377 } else {
1378 SC = 0; AW = 0;
1379 }
1380}
1381
1382CachedWriter::~CachedWriter() {
1383 delete AW;
1384 delete SC;
1385}
1386
Chris Lattner60a7dd12004-07-15 02:51:31 +00001387CachedWriter &CachedWriter::operator<<(const Value &V) {
Chris Lattner7db79582001-11-07 04:21:57 +00001388 assert(AW && SC && "CachedWriter does not have a current module!");
Chris Lattner60a7dd12004-07-15 02:51:31 +00001389 if (const Instruction *I = dyn_cast<Instruction>(&V))
Chris Lattner80d2d532004-06-26 19:40:40 +00001390 AW->write(I);
Chris Lattner60a7dd12004-07-15 02:51:31 +00001391 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(&V))
Chris Lattner80d2d532004-06-26 19:40:40 +00001392 AW->write(BB);
Chris Lattner60a7dd12004-07-15 02:51:31 +00001393 else if (const Function *F = dyn_cast<Function>(&V))
Chris Lattner80d2d532004-06-26 19:40:40 +00001394 AW->write(F);
Chris Lattner60a7dd12004-07-15 02:51:31 +00001395 else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(&V))
Chris Lattner80d2d532004-06-26 19:40:40 +00001396 AW->write(GV);
Misha Brukmanb1c93172005-04-21 23:48:37 +00001397 else
Chris Lattner60a7dd12004-07-15 02:51:31 +00001398 AW->writeOperand(&V, true, true);
Chris Lattner7db79582001-11-07 04:21:57 +00001399 return *this;
1400}
Misha Brukman4685e262004-04-28 15:31:21 +00001401
Chris Lattner60a7dd12004-07-15 02:51:31 +00001402CachedWriter& CachedWriter::operator<<(const Type &Ty) {
Misha Brukman4685e262004-04-28 15:31:21 +00001403 if (SymbolicTypes) {
1404 const Module *M = AW->getModule();
Chris Lattner60a7dd12004-07-15 02:51:31 +00001405 if (M) WriteTypeSymbolic(Out, &Ty, M);
Reid Spencer58d30f22004-07-04 11:50:43 +00001406 } else {
Chris Lattner60a7dd12004-07-15 02:51:31 +00001407 AW->write(&Ty);
Reid Spencer58d30f22004-07-04 11:50:43 +00001408 }
1409 return *this;
Misha Brukman4685e262004-04-28 15:31:21 +00001410}
Misha Brukmanda546ea2004-04-28 19:24:28 +00001411
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001412//===----------------------------------------------------------------------===//
1413//===-- SlotMachine Implementation
1414//===----------------------------------------------------------------------===//
1415
1416#if 0
Bill Wendlingdfc91892006-11-28 02:09:03 +00001417#define SC_DEBUG(X) llvm_cerr << X
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001418#else
1419#define SC_DEBUG(X)
1420#endif
1421
1422// Module level constructor. Causes the contents of the Module (sans functions)
1423// to be added to the slot table.
Misha Brukmanb1c93172005-04-21 23:48:37 +00001424SlotMachine::SlotMachine(const Module *M)
Reid Spencer56010e42004-05-26 21:56:09 +00001425 : TheModule(M) ///< Saved for lazy initialization.
1426 , TheFunction(0)
Reid Spencerb0ac8c42004-08-16 07:46:33 +00001427 , FunctionProcessed(false)
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001428 , mMap()
Reid Spencer58d30f22004-07-04 11:50:43 +00001429 , mTypes()
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001430 , fMap()
Reid Spencer58d30f22004-07-04 11:50:43 +00001431 , fTypes()
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001432{
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001433}
1434
1435// Function level constructor. Causes the contents of the Module and the one
1436// function provided to be added to the slot table.
Misha Brukmanb1c93172005-04-21 23:48:37 +00001437SlotMachine::SlotMachine(const Function *F )
Reid Spencer56010e42004-05-26 21:56:09 +00001438 : TheModule( F ? F->getParent() : 0 ) ///< Saved for lazy initialization
1439 , TheFunction(F) ///< Saved for lazy initialization
Reid Spencerb0ac8c42004-08-16 07:46:33 +00001440 , FunctionProcessed(false)
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001441 , mMap()
Reid Spencer58d30f22004-07-04 11:50:43 +00001442 , mTypes()
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001443 , fMap()
Reid Spencer58d30f22004-07-04 11:50:43 +00001444 , fTypes()
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001445{
Reid Spencer56010e42004-05-26 21:56:09 +00001446}
1447
1448inline void SlotMachine::initialize(void) {
Misha Brukmanb1c93172005-04-21 23:48:37 +00001449 if ( TheModule) {
1450 processModule();
Reid Spencer56010e42004-05-26 21:56:09 +00001451 TheModule = 0; ///< Prevent re-processing next time we're called.
1452 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001453 if ( TheFunction && ! FunctionProcessed) {
1454 processFunction();
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001455 }
1456}
1457
1458// Iterate through all the global variables, functions, and global
Misha Brukmanb1c93172005-04-21 23:48:37 +00001459// variable initializers and create slots for them.
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001460void SlotMachine::processModule() {
1461 SC_DEBUG("begin processModule!\n");
1462
1463 // Add all of the global variables to the value table...
Chris Lattner531f9e92005-03-15 04:54:21 +00001464 for (Module::const_global_iterator I = TheModule->global_begin(), E = TheModule->global_end();
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001465 I != E; ++I)
1466 createSlot(I);
1467
1468 // Add all the functions to the table
1469 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
1470 I != E; ++I)
1471 createSlot(I);
1472
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001473 SC_DEBUG("end processModule!\n");
1474}
1475
1476
Reid Spencer56010e42004-05-26 21:56:09 +00001477// Process the arguments, basic blocks, and instructions of a function.
1478void SlotMachine::processFunction() {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001479 SC_DEBUG("begin processFunction!\n");
1480
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001481 // Add all the function arguments
Misha Brukmanb1c93172005-04-21 23:48:37 +00001482 for(Function::const_arg_iterator AI = TheFunction->arg_begin(),
Chris Lattner531f9e92005-03-15 04:54:21 +00001483 AE = TheFunction->arg_end(); AI != AE; ++AI)
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001484 createSlot(AI);
1485
1486 SC_DEBUG("Inserting Instructions:\n");
1487
1488 // Add all of the basic blocks and instructions
Misha Brukmanb1c93172005-04-21 23:48:37 +00001489 for (Function::const_iterator BB = TheFunction->begin(),
Reid Spencer56010e42004-05-26 21:56:09 +00001490 E = TheFunction->end(); BB != E; ++BB) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001491 createSlot(BB);
1492 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
1493 createSlot(I);
1494 }
1495 }
1496
Reid Spencerb0ac8c42004-08-16 07:46:33 +00001497 FunctionProcessed = true;
1498
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001499 SC_DEBUG("end processFunction!\n");
1500}
1501
1502// Clean up after incorporating a function. This is the only way
Reid Spencer56010e42004-05-26 21:56:09 +00001503// to get out of the function incorporation state that affects the
1504// getSlot/createSlot lock. Function incorporation state is indicated
1505// by TheFunction != 0.
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001506void SlotMachine::purgeFunction() {
1507 SC_DEBUG("begin purgeFunction!\n");
1508 fMap.clear(); // Simply discard the function level map
Reid Spencer58d30f22004-07-04 11:50:43 +00001509 fTypes.clear();
Reid Spencer56010e42004-05-26 21:56:09 +00001510 TheFunction = 0;
Reid Spencerb0ac8c42004-08-16 07:46:33 +00001511 FunctionProcessed = false;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001512 SC_DEBUG("end purgeFunction!\n");
1513}
1514
1515/// Get the slot number for a value. This function will assert if you
1516/// ask for a Value that hasn't previously been inserted with createSlot.
1517/// Types are forbidden because Type does not inherit from Value (any more).
Chris Lattner757ee0b2004-06-09 19:41:19 +00001518int SlotMachine::getSlot(const Value *V) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001519 assert( V && "Can't get slot for null Value" );
Misha Brukmanb1c93172005-04-21 23:48:37 +00001520 assert(!isa<Constant>(V) || isa<GlobalValue>(V) &&
1521 "Can't insert a non-GlobalValue Constant into SlotMachine");
Reid Spencer56010e42004-05-26 21:56:09 +00001522
1523 // Check for uninitialized state and do lazy initialization
1524 this->initialize();
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001525
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001526 // Get the type of the value
1527 const Type* VTy = V->getType();
1528
1529 // Find the type plane in the module map
1530 TypedPlanes::const_iterator MI = mMap.find(VTy);
1531
Reid Spencer56010e42004-05-26 21:56:09 +00001532 if ( TheFunction ) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001533 // Lookup the type in the function map too
1534 TypedPlanes::const_iterator FI = fMap.find(VTy);
1535 // If there is a corresponding type plane in the function map
1536 if ( FI != fMap.end() ) {
1537 // Lookup the Value in the function map
1538 ValueMap::const_iterator FVI = FI->second.map.find(V);
1539 // If the value doesn't exist in the function map
1540 if ( FVI == FI->second.map.end() ) {
Chris Lattner68a038e2004-06-09 22:22:10 +00001541 // Look up the value in the module map.
1542 if (MI == mMap.end()) return -1;
Reid Spencer56010e42004-05-26 21:56:09 +00001543 ValueMap::const_iterator MVI = MI->second.map.find(V);
1544 // If we didn't find it, it wasn't inserted
Chris Lattner757ee0b2004-06-09 19:41:19 +00001545 if (MVI == MI->second.map.end()) return -1;
Reid Spencer56010e42004-05-26 21:56:09 +00001546 assert( MVI != MI->second.map.end() && "Value not found");
1547 // We found it only at the module level
Misha Brukmanb1c93172005-04-21 23:48:37 +00001548 return MVI->second;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001549
1550 // else the value exists in the function map
1551 } else {
Reid Spencer56010e42004-05-26 21:56:09 +00001552 // Return the slot number as the module's contribution to
1553 // the type plane plus the index in the function's contribution
1554 // to the type plane.
Chris Lattnerb1f04782004-06-15 21:07:32 +00001555 if (MI != mMap.end())
1556 return MI->second.next_slot + FVI->second;
1557 else
1558 return FVI->second;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001559 }
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001560 }
1561 }
1562
Reid Spencer8beac692004-06-09 15:26:53 +00001563 // N.B. Can get here only if either !TheFunction or the function doesn't
1564 // have a corresponding type plane for the Value
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001565
1566 // Make sure the type plane exists
Chris Lattner757ee0b2004-06-09 19:41:19 +00001567 if (MI == mMap.end()) return -1;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001568 // Lookup the value in the module's map
1569 ValueMap::const_iterator MVI = MI->second.map.find(V);
1570 // Make sure we found it.
Chris Lattner757ee0b2004-06-09 19:41:19 +00001571 if (MVI == MI->second.map.end()) return -1;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001572 // Return it.
1573 return MVI->second;
1574}
1575
Reid Spencer58d30f22004-07-04 11:50:43 +00001576/// Get the slot number for a value. This function will assert if you
1577/// ask for a Value that hasn't previously been inserted with createSlot.
1578/// Types are forbidden because Type does not inherit from Value (any more).
1579int SlotMachine::getSlot(const Type *Ty) {
1580 assert( Ty && "Can't get slot for null Type" );
1581
1582 // Check for uninitialized state and do lazy initialization
1583 this->initialize();
1584
1585 if ( TheFunction ) {
1586 // Lookup the Type in the function map
1587 TypeMap::const_iterator FTI = fTypes.map.find(Ty);
1588 // If the Type doesn't exist in the function map
1589 if ( FTI == fTypes.map.end() ) {
1590 TypeMap::const_iterator MTI = mTypes.map.find(Ty);
1591 // If we didn't find it, it wasn't inserted
Misha Brukmanb1c93172005-04-21 23:48:37 +00001592 if (MTI == mTypes.map.end())
Reid Spencer58d30f22004-07-04 11:50:43 +00001593 return -1;
1594 // We found it only at the module level
Misha Brukmanb1c93172005-04-21 23:48:37 +00001595 return MTI->second;
Reid Spencer58d30f22004-07-04 11:50:43 +00001596
1597 // else the value exists in the function map
1598 } else {
1599 // Return the slot number as the module's contribution to
1600 // the type plane plus the index in the function's contribution
1601 // to the type plane.
1602 return mTypes.next_slot + FTI->second;
1603 }
1604 }
1605
1606 // N.B. Can get here only if either !TheFunction
1607
1608 // Lookup the value in the module's map
1609 TypeMap::const_iterator MTI = mTypes.map.find(Ty);
1610 // Make sure we found it.
1611 if (MTI == mTypes.map.end()) return -1;
1612 // Return it.
1613 return MTI->second;
1614}
1615
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001616// Create a new slot, or return the existing slot if it is already
1617// inserted. Note that the logic here parallels getSlot but instead
1618// of asserting when the Value* isn't found, it inserts the value.
1619unsigned SlotMachine::createSlot(const Value *V) {
1620 assert( V && "Can't insert a null Value to SlotMachine");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001621 assert(!isa<Constant>(V) || isa<GlobalValue>(V) &&
1622 "Can't insert a non-GlobalValue Constant into SlotMachine");
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001623
1624 const Type* VTy = V->getType();
1625
1626 // Just ignore void typed things
1627 if (VTy == Type::VoidTy) return 0; // FIXME: Wrong return value!
1628
1629 // Look up the type plane for the Value's type from the module map
1630 TypedPlanes::const_iterator MI = mMap.find(VTy);
1631
Reid Spencer56010e42004-05-26 21:56:09 +00001632 if ( TheFunction ) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001633 // Get the type plane for the Value's type from the function map
1634 TypedPlanes::const_iterator FI = fMap.find(VTy);
1635 // If there is a corresponding type plane in the function map
1636 if ( FI != fMap.end() ) {
1637 // Lookup the Value in the function map
1638 ValueMap::const_iterator FVI = FI->second.map.find(V);
1639 // If the value doesn't exist in the function map
1640 if ( FVI == FI->second.map.end() ) {
Reid Spencer56010e42004-05-26 21:56:09 +00001641 // If there is no corresponding type plane in the module map
1642 if ( MI == mMap.end() )
1643 return insertValue(V);
1644 // Look up the value in the module map
1645 ValueMap::const_iterator MVI = MI->second.map.find(V);
1646 // If we didn't find it, it wasn't inserted
1647 if ( MVI == MI->second.map.end() )
1648 return insertValue(V);
1649 else
1650 // We found it only at the module level
1651 return MVI->second;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001652
1653 // else the value exists in the function map
1654 } else {
Reid Spencer56010e42004-05-26 21:56:09 +00001655 if ( MI == mMap.end() )
1656 return FVI->second;
1657 else
1658 // Return the slot number as the module's contribution to
1659 // the type plane plus the index in the function's contribution
1660 // to the type plane.
1661 return MI->second.next_slot + FVI->second;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001662 }
1663
1664 // else there is not a corresponding type plane in the function map
1665 } else {
1666 // If the type plane doesn't exists at the module level
1667 if ( MI == mMap.end() ) {
Reid Spencer56010e42004-05-26 21:56:09 +00001668 return insertValue(V);
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001669 // else type plane exists at the module level, examine it
1670 } else {
Reid Spencer56010e42004-05-26 21:56:09 +00001671 // Look up the value in the module's map
1672 ValueMap::const_iterator MVI = MI->second.map.find(V);
1673 // If we didn't find it there either
1674 if ( MVI == MI->second.map.end() )
1675 // Return the slot number as the module's contribution to
1676 // the type plane plus the index of the function map insertion.
1677 return MI->second.next_slot + insertValue(V);
1678 else
1679 return MVI->second;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001680 }
1681 }
1682 }
1683
Reid Spencer56010e42004-05-26 21:56:09 +00001684 // N.B. Can only get here if !TheFunction
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001685
1686 // If the module map's type plane is not for the Value's type
1687 if ( MI != mMap.end() ) {
1688 // Lookup the value in the module's map
1689 ValueMap::const_iterator MVI = MI->second.map.find(V);
Misha Brukmanb1c93172005-04-21 23:48:37 +00001690 if ( MVI != MI->second.map.end() )
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001691 return MVI->second;
1692 }
1693
1694 return insertValue(V);
1695}
1696
Reid Spencer58d30f22004-07-04 11:50:43 +00001697// Create a new slot, or return the existing slot if it is already
1698// inserted. Note that the logic here parallels getSlot but instead
1699// of asserting when the Value* isn't found, it inserts the value.
1700unsigned SlotMachine::createSlot(const Type *Ty) {
1701 assert( Ty && "Can't insert a null Type to SlotMachine");
1702
1703 if ( TheFunction ) {
1704 // Lookup the Type in the function map
1705 TypeMap::const_iterator FTI = fTypes.map.find(Ty);
1706 // If the type doesn't exist in the function map
1707 if ( FTI == fTypes.map.end() ) {
1708 // Look up the type in the module map
1709 TypeMap::const_iterator MTI = mTypes.map.find(Ty);
1710 // If we didn't find it, it wasn't inserted
1711 if ( MTI == mTypes.map.end() )
1712 return insertValue(Ty);
1713 else
1714 // We found it only at the module level
1715 return MTI->second;
1716
1717 // else the value exists in the function map
1718 } else {
1719 // Return the slot number as the module's contribution to
1720 // the type plane plus the index in the function's contribution
1721 // to the type plane.
1722 return mTypes.next_slot + FTI->second;
1723 }
1724 }
1725
1726 // N.B. Can only get here if !TheFunction
1727
1728 // Lookup the type in the module's map
1729 TypeMap::const_iterator MTI = mTypes.map.find(Ty);
Misha Brukmanb1c93172005-04-21 23:48:37 +00001730 if ( MTI != mTypes.map.end() )
Reid Spencer58d30f22004-07-04 11:50:43 +00001731 return MTI->second;
1732
1733 return insertValue(Ty);
1734}
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001735
1736// Low level insert function. Minimal checking is done. This
1737// function is just for the convenience of createSlot (above).
1738unsigned SlotMachine::insertValue(const Value *V ) {
1739 assert(V && "Can't insert a null Value into SlotMachine!");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001740 assert(!isa<Constant>(V) || isa<GlobalValue>(V) &&
1741 "Can't insert a non-GlobalValue Constant into SlotMachine");
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001742
Reid Spencer56010e42004-05-26 21:56:09 +00001743 // If this value does not contribute to a plane (is void)
Misha Brukmanb1c93172005-04-21 23:48:37 +00001744 // or if the value already has a name then ignore it.
Reid Spencer56010e42004-05-26 21:56:09 +00001745 if (V->getType() == Type::VoidTy || V->hasName() ) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001746 SC_DEBUG("ignored value " << *V << "\n");
1747 return 0; // FIXME: Wrong return value
1748 }
1749
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001750 const Type *VTy = V->getType();
1751 unsigned DestSlot = 0;
1752
Reid Spencer56010e42004-05-26 21:56:09 +00001753 if ( TheFunction ) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001754 TypedPlanes::iterator I = fMap.find( VTy );
Misha Brukmanb1c93172005-04-21 23:48:37 +00001755 if ( I == fMap.end() )
Reid Spencer58d30f22004-07-04 11:50:43 +00001756 I = fMap.insert(std::make_pair(VTy,ValuePlane())).first;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001757 DestSlot = I->second.map[V] = I->second.next_slot++;
1758 } else {
1759 TypedPlanes::iterator I = mMap.find( VTy );
1760 if ( I == mMap.end() )
Reid Spencer58d30f22004-07-04 11:50:43 +00001761 I = mMap.insert(std::make_pair(VTy,ValuePlane())).first;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001762 DestSlot = I->second.map[V] = I->second.next_slot++;
1763 }
1764
Misha Brukmanb1c93172005-04-21 23:48:37 +00001765 SC_DEBUG(" Inserting value [" << VTy << "] = " << V << " slot=" <<
Reid Spencer56010e42004-05-26 21:56:09 +00001766 DestSlot << " [");
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001767 // G = Global, C = Constant, T = Type, F = Function, o = other
Misha Brukmanb1c93172005-04-21 23:48:37 +00001768 SC_DEBUG((isa<GlobalVariable>(V) ? 'G' : (isa<Function>(V) ? 'F' :
Reid Spenceraccd7c72004-07-17 23:47:01 +00001769 (isa<Constant>(V) ? 'C' : 'o'))));
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001770 SC_DEBUG("]\n");
1771 return DestSlot;
1772}
1773
Reid Spencer58d30f22004-07-04 11:50:43 +00001774// Low level insert function. Minimal checking is done. This
1775// function is just for the convenience of createSlot (above).
1776unsigned SlotMachine::insertValue(const Type *Ty ) {
1777 assert(Ty && "Can't insert a null Type into SlotMachine!");
1778
1779 unsigned DestSlot = 0;
1780
1781 if ( TheFunction ) {
1782 DestSlot = fTypes.map[Ty] = fTypes.next_slot++;
1783 } else {
1784 DestSlot = fTypes.map[Ty] = fTypes.next_slot++;
1785 }
1786 SC_DEBUG(" Inserting type [" << DestSlot << "] = " << Ty << "\n");
1787 return DestSlot;
1788}
1789
Reid Spencere7e96712004-05-25 08:53:40 +00001790// vim: sw=2