blob: a5ac8f03311d3131fd804772aec28dbbbf242be3 [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"
Misha Brukman4685e262004-04-28 15:31:21 +000029#include "llvm/Assembly/Writer.h"
Chris Lattner58185f22002-10-02 19:38:55 +000030#include "llvm/Support/CFG.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000031#include "llvm/ADT/StringExtras.h"
32#include "llvm/ADT/STLExtras.h"
Jim Laskeyb74c6662005-08-17 19:34:49 +000033#include "llvm/Support/MathExtras.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 Lattnerc8b70922002-07-26 21:12:46 +0000166static RegisterPass<PrintModulePass>
167X("printm", "Print module to stderr",PassInfo::Analysis|PassInfo::Optimization);
168static RegisterPass<PrintFunctionPass>
169Y("print","Print function to stderr",PassInfo::Analysis|PassInfo::Optimization);
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) {
Chris Lattner1e194682002-04-18 18:53:13 +0000422 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
423 Out << (CB == ConstantBool::True ? "true" : "false");
424 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV)) {
425 Out << CI->getValue();
426 } else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV)) {
427 Out << CI->getValue();
428 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
429 // We would like to output the FP constant value in exponential notation,
430 // but we cannot do this if doing so will lose precision. Check here to
431 // make sure that we only output it in exponential format if we can parse
432 // the value back and get the same value.
433 //
434 std::string StrVal = ftostr(CFP->getValue());
435
436 // Check to make sure that the stringized number is not some string like
437 // "Inf" or NaN, that atof will accept, but the lexer will not. Check that
438 // the string matches the "[-+]?[0-9]" regex.
439 //
440 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
441 ((StrVal[0] == '-' || StrVal[0] == '+') &&
Brian Gaeke87b4f072003-06-17 23:55:35 +0000442 (StrVal[1] >= '0' && StrVal[1] <= '9')))
Chris Lattner1e194682002-04-18 18:53:13 +0000443 // Reparse stringized version!
444 if (atof(StrVal.c_str()) == CFP->getValue()) {
Chris Lattner472cc102005-01-04 01:56:57 +0000445 Out << StrVal;
446 return;
Chris Lattner1e194682002-04-18 18:53:13 +0000447 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000448
Chris Lattner1e194682002-04-18 18:53:13 +0000449 // Otherwise we could not reparse it to exactly the same value, so we must
450 // output the string in hexadecimal format!
Chris Lattner472cc102005-01-04 01:56:57 +0000451 assert(sizeof(double) == sizeof(uint64_t) &&
Chris Lattner1e194682002-04-18 18:53:13 +0000452 "assuming that double is 64 bits!");
Jim Laskeyb74c6662005-08-17 19:34:49 +0000453 Out << "0x" << utohexstr(DoubleToBits(CFP->getValue()));
Chris Lattner1e194682002-04-18 18:53:13 +0000454
Chris Lattner76b2ff42004-02-15 05:55:15 +0000455 } else if (isa<ConstantAggregateZero>(CV)) {
456 Out << "zeroinitializer";
Chris Lattner1e194682002-04-18 18:53:13 +0000457 } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
458 // As a special case, print the array as a string if it is an array of
459 // ubytes or an array of sbytes with positive values.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000460 //
Chris Lattner1e194682002-04-18 18:53:13 +0000461 const Type *ETy = CA->getType()->getElementType();
Chris Lattner6ed87bd2006-01-23 23:03:36 +0000462 if (CA->isString()) {
Chris Lattner1e194682002-04-18 18:53:13 +0000463 Out << "c\"";
Chris Lattner6ed87bd2006-01-23 23:03:36 +0000464 PrintEscapedString(CA->getAsString(), Out);
Chris Lattner1e194682002-04-18 18:53:13 +0000465 Out << "\"";
466
467 } else { // Cannot output in string format...
Misha Brukman21bbdb92004-06-04 21:11:51 +0000468 Out << '[';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000469 if (CA->getNumOperands()) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000470 Out << ' ';
Chris Lattner1e194682002-04-18 18:53:13 +0000471 printTypeInt(Out, ETy, TypeTable);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000472 WriteAsOperandInternal(Out, CA->getOperand(0),
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000473 PrintName, TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000474 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
475 Out << ", ";
Chris Lattner1e194682002-04-18 18:53:13 +0000476 printTypeInt(Out, ETy, TypeTable);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000477 WriteAsOperandInternal(Out, CA->getOperand(i), PrintName,
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000478 TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000479 }
480 }
481 Out << " ]";
482 }
483 } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000484 Out << '{';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000485 if (CS->getNumOperands()) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000486 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000487 printTypeInt(Out, CS->getOperand(0)->getType(), TypeTable);
488
489 WriteAsOperandInternal(Out, CS->getOperand(0),
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000490 PrintName, TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000491
492 for (unsigned i = 1; i < CS->getNumOperands(); i++) {
493 Out << ", ";
494 printTypeInt(Out, CS->getOperand(i)->getType(), TypeTable);
495
496 WriteAsOperandInternal(Out, CS->getOperand(i),
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000497 PrintName, TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000498 }
499 }
500
501 Out << " }";
Brian Gaeke02209042004-08-20 06:00:58 +0000502 } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(CV)) {
503 const Type *ETy = CP->getType()->getElementType();
Misha Brukmanb1c93172005-04-21 23:48:37 +0000504 assert(CP->getNumOperands() > 0 &&
Brian Gaeke02209042004-08-20 06:00:58 +0000505 "Number of operands for a PackedConst must be > 0");
506 Out << '<';
507 Out << ' ';
508 printTypeInt(Out, ETy, TypeTable);
509 WriteAsOperandInternal(Out, CP->getOperand(0),
510 PrintName, TypeTable, Machine);
511 for (unsigned i = 1, e = CP->getNumOperands(); i != e; ++i) {
512 Out << ", ";
513 printTypeInt(Out, ETy, TypeTable);
514 WriteAsOperandInternal(Out, CP->getOperand(i), PrintName,
515 TypeTable, Machine);
516 }
517 Out << " >";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000518 } else if (isa<ConstantPointerNull>(CV)) {
519 Out << "null";
520
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000521 } else if (isa<UndefValue>(CV)) {
522 Out << "undef";
523
Chris Lattner3cd8c562002-07-30 18:54:25 +0000524 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Chris Lattnerb8257222003-03-06 23:23:32 +0000525 Out << CE->getOpcodeName() << " (";
Misha Brukmanb1c93172005-04-21 23:48:37 +0000526
Vikram S. Adveb952b542002-07-14 23:14:45 +0000527 for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
528 printTypeInt(Out, (*OI)->getType(), TypeTable);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000529 WriteAsOperandInternal(Out, *OI, PrintName, TypeTable, Machine);
Vikram S. Adveb952b542002-07-14 23:14:45 +0000530 if (OI+1 != CE->op_end())
Chris Lattner3cd8c562002-07-30 18:54:25 +0000531 Out << ", ";
Vikram S. Adveb952b542002-07-14 23:14:45 +0000532 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000533
Chris Lattnercfe8f532002-08-16 21:17:11 +0000534 if (CE->getOpcode() == Instruction::Cast) {
Chris Lattner83b396b2002-08-15 19:37:43 +0000535 Out << " to ";
536 printTypeInt(Out, CE->getType(), TypeTable);
537 }
Misha Brukman21bbdb92004-06-04 21:11:51 +0000538 Out << ')';
Chris Lattner83b396b2002-08-15 19:37:43 +0000539
Chris Lattnerd84bb632002-04-16 21:36:08 +0000540 } else {
Vikram S. Adveb952b542002-07-14 23:14:45 +0000541 Out << "<placeholder or erroneous Constant>";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000542 }
543}
544
545
Misha Brukmanc566ca362004-03-02 00:22:19 +0000546/// WriteAsOperand - Write the name of the specified value out to the specified
547/// ostream. This can be useful when you just want to print int %reg126, not
548/// the whole instruction that generated it.
549///
Misha Brukmanb1c93172005-04-21 23:48:37 +0000550static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000551 bool PrintName,
552 std::map<const Type*, std::string> &TypeTable,
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000553 SlotMachine *Machine) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000554 Out << ' ';
Reid Spenceraccd7c72004-07-17 23:47:01 +0000555 if ((PrintName || isa<GlobalValue>(V)) && V->hasName())
Chris Lattner1c343042003-08-22 05:40:38 +0000556 Out << getLLVMName(V->getName());
Reid Spenceraccd7c72004-07-17 23:47:01 +0000557 else {
558 const Constant *CV = dyn_cast<Constant>(V);
559 if (CV && !isa<GlobalValue>(CV))
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000560 WriteConstantInt(Out, CV, PrintName, TypeTable, Machine);
Reid Spenceraccd7c72004-07-17 23:47:01 +0000561 else {
Chris Lattnerd84bb632002-04-16 21:36:08 +0000562 int Slot;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000563 if (Machine) {
Reid Spencer56010e42004-05-26 21:56:09 +0000564 Slot = Machine->getSlot(V);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000565 } else {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000566 Machine = createSlotMachine(V);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000567 if (Machine == 0)
Chris Lattner757ee0b2004-06-09 19:41:19 +0000568 Slot = Machine->getSlot(V);
569 else
570 Slot = -1;
Reid Spencer56010e42004-05-26 21:56:09 +0000571 delete Machine;
Chris Lattnerd84bb632002-04-16 21:36:08 +0000572 }
Chris Lattner757ee0b2004-06-09 19:41:19 +0000573 if (Slot != -1)
574 Out << '%' << Slot;
575 else
576 Out << "<badref>";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000577 }
578 }
579}
580
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000581/// WriteAsOperand - Write the name of the specified value out to the specified
582/// ostream. This can be useful when you just want to print int %reg126, not
583/// the whole instruction that generated it.
584///
Chris Lattner189d19f2003-11-21 20:23:48 +0000585std::ostream &llvm::WriteAsOperand(std::ostream &Out, const Value *V,
Misha Brukmanb1c93172005-04-21 23:48:37 +0000586 bool PrintType, bool PrintName,
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000587 const Module *Context) {
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000588 std::map<const Type *, std::string> TypeNames;
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000589 if (Context == 0) Context = getModuleFromVal(V);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000590
Chris Lattner98cf1f52002-11-20 18:36:02 +0000591 if (Context)
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000592 fillTypeNameTable(Context, TypeNames);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000593
594 if (PrintType)
595 printTypeInt(Out, V->getType(), TypeNames);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000596
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000597 WriteAsOperandInternal(Out, V, PrintName, TypeNames, 0);
Chris Lattner5e5abe32001-07-20 19:15:21 +0000598 return Out;
599}
600
Misha Brukmanb1c93172005-04-21 23:48:37 +0000601/// WriteAsOperandInternal - Write the name of the specified value out to
602/// the specified ostream. This can be useful when you just want to print
Reid Spencer58d30f22004-07-04 11:50:43 +0000603/// int %reg126, not the whole instruction that generated it.
604///
Misha Brukmanb1c93172005-04-21 23:48:37 +0000605static void WriteAsOperandInternal(std::ostream &Out, const Type *T,
Reid Spencer58d30f22004-07-04 11:50:43 +0000606 bool PrintName,
607 std::map<const Type*, std::string> &TypeTable,
608 SlotMachine *Machine) {
609 Out << ' ';
610 int Slot;
611 if (Machine) {
612 Slot = Machine->getSlot(T);
613 if (Slot != -1)
614 Out << '%' << Slot;
615 else
616 Out << "<badref>";
617 } else {
618 Out << T->getDescription();
619 }
620}
621
622/// WriteAsOperand - Write the name of the specified value out to the specified
623/// ostream. This can be useful when you just want to print int %reg126, not
624/// the whole instruction that generated it.
625///
626std::ostream &llvm::WriteAsOperand(std::ostream &Out, const Type *Ty,
Misha Brukmanb1c93172005-04-21 23:48:37 +0000627 bool PrintType, bool PrintName,
Reid Spencer58d30f22004-07-04 11:50:43 +0000628 const Module *Context) {
629 std::map<const Type *, std::string> TypeNames;
630 assert(Context != 0 && "Can't write types as operand without module context");
631
632 fillTypeNameTable(Context, TypeNames);
633
634 // if (PrintType)
635 // printTypeInt(Out, V->getType(), TypeNames);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000636
Reid Spencer58d30f22004-07-04 11:50:43 +0000637 printTypeInt(Out, Ty, TypeNames);
638
639 WriteAsOperandInternal(Out, Ty, PrintName, TypeNames, 0);
640 return Out;
641}
642
Chris Lattner189d19f2003-11-21 20:23:48 +0000643namespace llvm {
Chris Lattner2e9fee42001-07-12 23:35:26 +0000644
Chris Lattnerfee714f2001-09-07 16:36:04 +0000645class AssemblyWriter {
Misha Brukmana6619a92004-06-21 21:53:56 +0000646 std::ostream &Out;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000647 SlotMachine &Machine;
Chris Lattner7bfee412001-10-29 16:05:51 +0000648 const Module *TheModule;
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000649 std::map<const Type *, std::string> TypeNames;
Chris Lattner8339f7d2003-10-30 23:41:03 +0000650 AssemblyAnnotationWriter *AnnotationWriter;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000651public:
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000652 inline AssemblyWriter(std::ostream &o, SlotMachine &Mac, const Module *M,
Chris Lattner8339f7d2003-10-30 23:41:03 +0000653 AssemblyAnnotationWriter *AAW)
Misha Brukmana6619a92004-06-21 21:53:56 +0000654 : Out(o), Machine(Mac), TheModule(M), AnnotationWriter(AAW) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000655
656 // If the module has a symbol table, take all global types and stuff their
657 // names into the TypeNames map.
658 //
Chris Lattnerb86620e2001-10-29 16:37:48 +0000659 fillTypeNameTable(M, TypeNames);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000660 }
661
Chris Lattner7bfee412001-10-29 16:05:51 +0000662 inline void write(const Module *M) { printModule(M); }
663 inline void write(const GlobalVariable *G) { printGlobal(G); }
Chris Lattner57698e22002-03-26 18:01:55 +0000664 inline void write(const Function *F) { printFunction(F); }
Chris Lattner7bfee412001-10-29 16:05:51 +0000665 inline void write(const BasicBlock *BB) { printBasicBlock(BB); }
Chris Lattner113f4f42002-06-25 16:13:24 +0000666 inline void write(const Instruction *I) { printInstruction(*I); }
Chris Lattner3462ae32001-12-03 22:26:30 +0000667 inline void write(const Constant *CPV) { printConstant(CPV); }
Chris Lattner7db79582001-11-07 04:21:57 +0000668 inline void write(const Type *Ty) { printType(Ty); }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000669
Chris Lattner1e194682002-04-18 18:53:13 +0000670 void writeOperand(const Value *Op, bool PrintType, bool PrintName = true);
671
Misha Brukman4685e262004-04-28 15:31:21 +0000672 const Module* getModule() { return TheModule; }
673
Misha Brukmand92f54a2004-11-15 19:30:05 +0000674private:
Chris Lattner7bfee412001-10-29 16:05:51 +0000675 void printModule(const Module *M);
676 void printSymbolTable(const SymbolTable &ST);
Chris Lattner3462ae32001-12-03 22:26:30 +0000677 void printConstant(const Constant *CPV);
Chris Lattner7bfee412001-10-29 16:05:51 +0000678 void printGlobal(const GlobalVariable *GV);
Chris Lattner57698e22002-03-26 18:01:55 +0000679 void printFunction(const Function *F);
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000680 void printArgument(const Argument *FA);
Chris Lattner7bfee412001-10-29 16:05:51 +0000681 void printBasicBlock(const BasicBlock *BB);
Chris Lattner113f4f42002-06-25 16:13:24 +0000682 void printInstruction(const Instruction &I);
Chris Lattnerd816b532002-04-13 20:53:41 +0000683
684 // printType - Go to extreme measures to attempt to print out a short,
685 // symbolic version of a type name.
686 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000687 std::ostream &printType(const Type *Ty) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000688 return printTypeInt(Out, Ty, TypeNames);
Chris Lattnerd816b532002-04-13 20:53:41 +0000689 }
690
691 // printTypeAtLeastOneLevel - Print out one level of the possibly complex type
692 // without considering any symbolic types that we may have equal to it.
693 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000694 std::ostream &printTypeAtLeastOneLevel(const Type *Ty);
Chris Lattner7bfee412001-10-29 16:05:51 +0000695
Chris Lattner862e3382001-10-13 06:42:36 +0000696 // printInfoComment - Print a little comment after the instruction indicating
697 // which slot it occupies.
Chris Lattner113f4f42002-06-25 16:13:24 +0000698 void printInfoComment(const Value &V);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000699};
Reid Spencerf43ac622004-05-27 22:04:46 +0000700} // end of llvm namespace
Chris Lattner2f7c9632001-06-06 20:29:01 +0000701
Misha Brukmanc566ca362004-03-02 00:22:19 +0000702/// printTypeAtLeastOneLevel - Print out one level of the possibly complex type
703/// without considering any symbolic types that we may have equal to it.
704///
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000705std::ostream &AssemblyWriter::printTypeAtLeastOneLevel(const Type *Ty) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000706 if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000707 printType(FTy->getReturnType()) << " (";
Chris Lattnerfa829be2004-02-09 04:14:01 +0000708 for (FunctionType::param_iterator I = FTy->param_begin(),
709 E = FTy->param_end(); I != E; ++I) {
710 if (I != FTy->param_begin())
Misha Brukmana6619a92004-06-21 21:53:56 +0000711 Out << ", ";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000712 printType(*I);
Chris Lattnerd816b532002-04-13 20:53:41 +0000713 }
714 if (FTy->isVarArg()) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000715 if (FTy->getNumParams()) Out << ", ";
716 Out << "...";
Chris Lattnerd816b532002-04-13 20:53:41 +0000717 }
Misha Brukmana6619a92004-06-21 21:53:56 +0000718 Out << ')';
Chris Lattner113f4f42002-06-25 16:13:24 +0000719 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000720 Out << "{ ";
Chris Lattnerac6db752004-02-09 04:37:31 +0000721 for (StructType::element_iterator I = STy->element_begin(),
722 E = STy->element_end(); I != E; ++I) {
723 if (I != STy->element_begin())
Misha Brukmana6619a92004-06-21 21:53:56 +0000724 Out << ", ";
Chris Lattnerd816b532002-04-13 20:53:41 +0000725 printType(*I);
726 }
Misha Brukmana6619a92004-06-21 21:53:56 +0000727 Out << " }";
Chris Lattner113f4f42002-06-25 16:13:24 +0000728 } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000729 printType(PTy->getElementType()) << '*';
Chris Lattner113f4f42002-06-25 16:13:24 +0000730 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000731 Out << '[' << ATy->getNumElements() << " x ";
Misha Brukman21bbdb92004-06-04 21:11:51 +0000732 printType(ATy->getElementType()) << ']';
Reid Spencere203d352004-08-20 15:37:30 +0000733 } else if (const PackedType *PTy = dyn_cast<PackedType>(Ty)) {
734 Out << '<' << PTy->getNumElements() << " x ";
735 printType(PTy->getElementType()) << '>';
736 }
737 else if (const OpaqueType *OTy = dyn_cast<OpaqueType>(Ty)) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000738 Out << "opaque";
Chris Lattnerd816b532002-04-13 20:53:41 +0000739 } else {
Vikram S. Adveb952b542002-07-14 23:14:45 +0000740 if (!Ty->isPrimitiveType())
Misha Brukmana6619a92004-06-21 21:53:56 +0000741 Out << "<unknown derived type>";
Chris Lattnerd816b532002-04-13 20:53:41 +0000742 printType(Ty);
743 }
Misha Brukmana6619a92004-06-21 21:53:56 +0000744 return Out;
Chris Lattnerd816b532002-04-13 20:53:41 +0000745}
746
747
Misha Brukmanb1c93172005-04-21 23:48:37 +0000748void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType,
Reid Spencer56010e42004-05-26 21:56:09 +0000749 bool PrintName) {
Chris Lattner08f7d0c2005-02-24 16:58:29 +0000750 if (Operand != 0) {
751 if (PrintType) { Out << ' '; printType(Operand->getType()); }
752 WriteAsOperandInternal(Out, Operand, PrintName, TypeNames, &Machine);
753 } else {
754 Out << "<null operand!>";
755 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000756}
757
Chris Lattner2f7c9632001-06-06 20:29:01 +0000758
Chris Lattner7bfee412001-10-29 16:05:51 +0000759void AssemblyWriter::printModule(const Module *M) {
Chris Lattner4d8689e2005-03-02 23:12:40 +0000760 if (!M->getModuleIdentifier().empty() &&
Misha Brukmanb1c93172005-04-21 23:48:37 +0000761 // Don't print the ID if it will start a new line (which would
Chris Lattner4d8689e2005-03-02 23:12:40 +0000762 // require a comment char before it).
763 M->getModuleIdentifier().find('\n') == std::string::npos)
764 Out << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
765
Chris Lattner8068e0c2003-08-24 13:48:48 +0000766 switch (M->getEndianness()) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000767 case Module::LittleEndian: Out << "target endian = little\n"; break;
768 case Module::BigEndian: Out << "target endian = big\n"; break;
Chris Lattner8068e0c2003-08-24 13:48:48 +0000769 case Module::AnyEndianness: break;
770 }
771 switch (M->getPointerSize()) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000772 case Module::Pointer32: Out << "target pointersize = 32\n"; break;
773 case Module::Pointer64: Out << "target pointersize = 64\n"; break;
Chris Lattner8068e0c2003-08-24 13:48:48 +0000774 case Module::AnyPointerSize: break;
775 }
Reid Spencer48f98c82004-07-25 21:44:54 +0000776 if (!M->getTargetTriple().empty())
Reid Spencerffec7df2004-07-25 21:29:43 +0000777 Out << "target triple = \"" << M->getTargetTriple() << "\"\n";
Misha Brukmanb1c93172005-04-21 23:48:37 +0000778
Chris Lattnereef2fe72006-01-24 04:13:11 +0000779 if (!M->getModuleInlineAsm().empty()) {
Chris Lattnerefaf35d2006-01-24 00:45:30 +0000780 // Split the string into lines, to make it easier to read the .ll file.
Chris Lattnereef2fe72006-01-24 04:13:11 +0000781 std::string Asm = M->getModuleInlineAsm();
Chris Lattnerefaf35d2006-01-24 00:45:30 +0000782 size_t CurPos = 0;
783 size_t NewLine = Asm.find_first_of('\n', CurPos);
784 while (NewLine != std::string::npos) {
785 // We found a newline, print the portion of the asm string from the
786 // last newline up to this newline.
787 Out << "module asm \"";
788 PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.begin()+NewLine),
789 Out);
790 Out << "\"\n";
791 CurPos = NewLine+1;
792 NewLine = Asm.find_first_of('\n', CurPos);
793 }
Chris Lattner3acaf5c2006-01-24 00:40:17 +0000794 Out << "module asm \"";
Chris Lattnerefaf35d2006-01-24 00:45:30 +0000795 PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.end()), Out);
Chris Lattner6ed87bd2006-01-23 23:03:36 +0000796 Out << "\"\n";
797 }
798
Chris Lattner2cdd49d2004-09-14 05:06:58 +0000799 // Loop over the dependent libraries and emit them.
Chris Lattner730cfe42004-09-14 04:51:44 +0000800 Module::lib_iterator LI = M->lib_begin();
801 Module::lib_iterator LE = M->lib_end();
Reid Spencer48f98c82004-07-25 21:44:54 +0000802 if (LI != LE) {
Chris Lattner730cfe42004-09-14 04:51:44 +0000803 Out << "deplibs = [ ";
804 while (LI != LE) {
Chris Lattner2cdd49d2004-09-14 05:06:58 +0000805 Out << '"' << *LI << '"';
Reid Spencerffec7df2004-07-25 21:29:43 +0000806 ++LI;
Chris Lattner730cfe42004-09-14 04:51:44 +0000807 if (LI != LE)
808 Out << ", ";
Reid Spencerffec7df2004-07-25 21:29:43 +0000809 }
810 Out << " ]\n";
Reid Spencercc5ff642004-07-25 18:08:18 +0000811 }
Reid Spencerb9e08772004-09-13 23:44:23 +0000812
Chris Lattner2cdd49d2004-09-14 05:06:58 +0000813 // Loop over the symbol table, emitting all named constants.
Chris Lattner98cf1f52002-11-20 18:36:02 +0000814 printSymbolTable(M->getSymbolTable());
Misha Brukmanb1c93172005-04-21 23:48:37 +0000815
Chris Lattner531f9e92005-03-15 04:54:21 +0000816 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end(); I != E; ++I)
Chris Lattner113f4f42002-06-25 16:13:24 +0000817 printGlobal(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000818
Misha Brukmana6619a92004-06-21 21:53:56 +0000819 Out << "\nimplementation ; Functions:\n";
Misha Brukmanb1c93172005-04-21 23:48:37 +0000820
Chris Lattner2cdd49d2004-09-14 05:06:58 +0000821 // Output all of the functions.
Chris Lattner113f4f42002-06-25 16:13:24 +0000822 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
823 printFunction(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000824}
825
Chris Lattner7bfee412001-10-29 16:05:51 +0000826void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000827 if (GV->hasName()) Out << getLLVMName(GV->getName()) << " = ";
Chris Lattner37798642001-09-18 04:01:05 +0000828
Misha Brukmanb1c93172005-04-21 23:48:37 +0000829 if (!GV->hasInitializer())
Misha Brukmana6619a92004-06-21 21:53:56 +0000830 Out << "external ";
Chris Lattner379a8d22003-04-16 20:28:45 +0000831 else
832 switch (GV->getLinkage()) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000833 case GlobalValue::InternalLinkage: Out << "internal "; break;
834 case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
835 case GlobalValue::WeakLinkage: Out << "weak "; break;
836 case GlobalValue::AppendingLinkage: Out << "appending "; break;
Chris Lattner379a8d22003-04-16 20:28:45 +0000837 case GlobalValue::ExternalLinkage: break;
Misha Brukman70457632004-11-14 21:04:34 +0000838 case GlobalValue::GhostLinkage:
839 std::cerr << "GhostLinkage not allowed in AsmWriter!\n";
840 abort();
Chris Lattner379a8d22003-04-16 20:28:45 +0000841 }
Chris Lattner37798642001-09-18 04:01:05 +0000842
Misha Brukmana6619a92004-06-21 21:53:56 +0000843 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattner2413b162001-12-04 00:03:30 +0000844 printType(GV->getType()->getElementType());
Chris Lattner37798642001-09-18 04:01:05 +0000845
Reid Spenceraccd7c72004-07-17 23:47:01 +0000846 if (GV->hasInitializer()) {
847 Constant* C = cast<Constant>(GV->getInitializer());
848 assert(C && "GlobalVar initializer isn't constant?");
Reid Spencerc9c90cf2004-07-18 01:04:19 +0000849 writeOperand(GV->getInitializer(), false, isa<GlobalValue>(C));
Reid Spenceraccd7c72004-07-17 23:47:01 +0000850 }
Chris Lattnerf8a974d2005-11-06 06:48:53 +0000851
Chris Lattner4b96c542005-11-12 00:10:19 +0000852 if (GV->hasSection())
853 Out << ", section \"" << GV->getSection() << '"';
854 if (GV->getAlignment())
Chris Lattnerf8a974d2005-11-06 06:48:53 +0000855 Out << ", align " << GV->getAlignment();
Chris Lattner4b96c542005-11-12 00:10:19 +0000856
Chris Lattner113f4f42002-06-25 16:13:24 +0000857 printInfoComment(*GV);
Misha Brukmana6619a92004-06-21 21:53:56 +0000858 Out << "\n";
Chris Lattnerda975502001-09-10 07:58:01 +0000859}
860
Chris Lattnerfee714f2001-09-07 16:36:04 +0000861
Reid Spencere7e96712004-05-25 08:53:40 +0000862// printSymbolTable - Run through symbol table looking for constants
863// and types. Emit their declarations.
Chris Lattner7bfee412001-10-29 16:05:51 +0000864void AssemblyWriter::printSymbolTable(const SymbolTable &ST) {
Reid Spencere7e96712004-05-25 08:53:40 +0000865
866 // Print the types.
867 for (SymbolTable::type_const_iterator TI = ST.type_begin();
868 TI != ST.type_end(); ++TI ) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000869 Out << "\t" << getLLVMName(TI->first) << " = type ";
Reid Spencere7e96712004-05-25 08:53:40 +0000870
871 // Make sure we print out at least one level of the type structure, so
872 // that we do not get %FILE = type %FILE
873 //
874 printTypeAtLeastOneLevel(TI->second) << "\n";
875 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000876
Reid Spencere7e96712004-05-25 08:53:40 +0000877 // Print the constants, in type plane order.
878 for (SymbolTable::plane_const_iterator PI = ST.plane_begin();
879 PI != ST.plane_end(); ++PI ) {
880 SymbolTable::value_const_iterator VI = ST.value_begin(PI->first);
881 SymbolTable::value_const_iterator VE = ST.value_end(PI->first);
882
883 for (; VI != VE; ++VI) {
Reid Spenceraccd7c72004-07-17 23:47:01 +0000884 const Value* V = VI->second;
885 const Constant *CPV = dyn_cast<Constant>(V) ;
886 if (CPV && !isa<GlobalValue>(V)) {
Reid Spencer56010e42004-05-26 21:56:09 +0000887 printConstant(CPV);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000888 }
889 }
Chris Lattnera7620d92001-07-15 06:35:59 +0000890 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000891}
892
893
Misha Brukmanc566ca362004-03-02 00:22:19 +0000894/// printConstant - Print out a constant pool entry...
895///
Chris Lattner3462ae32001-12-03 22:26:30 +0000896void AssemblyWriter::printConstant(const Constant *CPV) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000897 // Don't print out unnamed constants, they will be inlined
898 if (!CPV->hasName()) return;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000899
Chris Lattneree998be2001-07-26 16:29:38 +0000900 // Print out name...
Misha Brukmana6619a92004-06-21 21:53:56 +0000901 Out << "\t" << getLLVMName(CPV->getName()) << " =";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000902
903 // Write the value out now...
Chris Lattnerd84bb632002-04-16 21:36:08 +0000904 writeOperand(CPV, true, false);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000905
Chris Lattner113f4f42002-06-25 16:13:24 +0000906 printInfoComment(*CPV);
Misha Brukmana6619a92004-06-21 21:53:56 +0000907 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000908}
909
Misha Brukmanc566ca362004-03-02 00:22:19 +0000910/// printFunction - Print all aspects of a function.
911///
Chris Lattner113f4f42002-06-25 16:13:24 +0000912void AssemblyWriter::printFunction(const Function *F) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000913 // Print out the return type and name...
Misha Brukmana6619a92004-06-21 21:53:56 +0000914 Out << "\n";
Chris Lattner379a8d22003-04-16 20:28:45 +0000915
Chris Lattner97e36f22004-12-05 06:44:09 +0000916 // Ensure that no local symbols conflict with global symbols.
917 const_cast<Function*>(F)->renameLocalSymbols();
918
Misha Brukmana6619a92004-06-21 21:53:56 +0000919 if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +0000920
Chris Lattner379a8d22003-04-16 20:28:45 +0000921 if (F->isExternal())
Misha Brukmana6619a92004-06-21 21:53:56 +0000922 Out << "declare ";
Chris Lattner379a8d22003-04-16 20:28:45 +0000923 else
924 switch (F->getLinkage()) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000925 case GlobalValue::InternalLinkage: Out << "internal "; break;
926 case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
927 case GlobalValue::WeakLinkage: Out << "weak "; break;
928 case GlobalValue::AppendingLinkage: Out << "appending "; break;
Chris Lattner379a8d22003-04-16 20:28:45 +0000929 case GlobalValue::ExternalLinkage: break;
Misha Brukman70457632004-11-14 21:04:34 +0000930 case GlobalValue::GhostLinkage:
931 std::cerr << "GhostLinkage not allowed in AsmWriter!\n";
932 abort();
Chris Lattner379a8d22003-04-16 20:28:45 +0000933 }
934
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000935 // Print the calling convention.
936 switch (F->getCallingConv()) {
937 case CallingConv::C: break; // default
938 case CallingConv::Fast: Out << "fastcc "; break;
939 case CallingConv::Cold: Out << "coldcc "; break;
940 default: Out << "cc" << F->getCallingConv() << " "; break;
941 }
942
Misha Brukman21bbdb92004-06-04 21:11:51 +0000943 printType(F->getReturnType()) << ' ';
Chris Lattner5b337482003-10-18 05:57:43 +0000944 if (!F->getName().empty())
Misha Brukmana6619a92004-06-21 21:53:56 +0000945 Out << getLLVMName(F->getName());
Chris Lattner5b337482003-10-18 05:57:43 +0000946 else
Misha Brukmana6619a92004-06-21 21:53:56 +0000947 Out << "\"\"";
948 Out << '(';
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000949 Machine.incorporateFunction(F);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000950
Chris Lattner7bfee412001-10-29 16:05:51 +0000951 // Loop over the arguments, printing them...
Chris Lattner113f4f42002-06-25 16:13:24 +0000952 const FunctionType *FT = F->getFunctionType();
Chris Lattnerfee714f2001-09-07 16:36:04 +0000953
Chris Lattner531f9e92005-03-15 04:54:21 +0000954 for(Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I)
Chris Lattner149376d2002-10-13 20:57:00 +0000955 printArgument(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000956
957 // Finish printing arguments...
Chris Lattner113f4f42002-06-25 16:13:24 +0000958 if (FT->isVarArg()) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000959 if (FT->getNumParams()) Out << ", ";
960 Out << "..."; // Output varargs portion of signature!
Chris Lattnerfee714f2001-09-07 16:36:04 +0000961 }
Misha Brukmana6619a92004-06-21 21:53:56 +0000962 Out << ')';
Chris Lattnerfee714f2001-09-07 16:36:04 +0000963
Chris Lattner4b96c542005-11-12 00:10:19 +0000964 if (F->hasSection())
965 Out << " section \"" << F->getSection() << '"';
Chris Lattnerf8a974d2005-11-06 06:48:53 +0000966 if (F->getAlignment())
967 Out << " align " << F->getAlignment();
Chris Lattner4b96c542005-11-12 00:10:19 +0000968
Chris Lattner113f4f42002-06-25 16:13:24 +0000969 if (F->isExternal()) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000970 Out << "\n";
Chris Lattnerb2f02e52002-05-06 03:00:40 +0000971 } else {
Misha Brukmana6619a92004-06-21 21:53:56 +0000972 Out << " {";
Misha Brukmanb1c93172005-04-21 23:48:37 +0000973
Chris Lattner6915f8f2002-04-07 22:49:37 +0000974 // Output all of its basic blocks... for the function
Chris Lattner113f4f42002-06-25 16:13:24 +0000975 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
976 printBasicBlock(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000977
Misha Brukmana6619a92004-06-21 21:53:56 +0000978 Out << "}\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000979 }
980
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000981 Machine.purgeFunction();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000982}
983
Misha Brukmanc566ca362004-03-02 00:22:19 +0000984/// printArgument - This member is called for every argument that is passed into
985/// the function. Simply print it out
986///
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000987void AssemblyWriter::printArgument(const Argument *Arg) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000988 // Insert commas as we go... the first arg doesn't get a comma
Chris Lattnerfb972ad2005-03-15 05:03:36 +0000989 if (Arg != Arg->getParent()->arg_begin()) Out << ", ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000990
991 // Output type...
Chris Lattner7bfee412001-10-29 16:05:51 +0000992 printType(Arg->getType());
Misha Brukmanb1c93172005-04-21 23:48:37 +0000993
Chris Lattner2f7c9632001-06-06 20:29:01 +0000994 // Output name, if available...
995 if (Arg->hasName())
Misha Brukmana6619a92004-06-21 21:53:56 +0000996 Out << ' ' << getLLVMName(Arg->getName());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000997}
998
Misha Brukmanc566ca362004-03-02 00:22:19 +0000999/// printBasicBlock - This member is called for each basic block in a method.
1000///
Chris Lattner7bfee412001-10-29 16:05:51 +00001001void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001002 if (BB->hasName()) { // Print out the label if it exists...
Alkis Evlogimenos991d6ad2004-12-10 05:41:10 +00001003 Out << "\n" << getLLVMName(BB->getName(), false) << ':';
Chris Lattner408dbdb2002-05-14 16:02:05 +00001004 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Misha Brukmana6619a92004-06-21 21:53:56 +00001005 Out << "\n; <label>:";
Chris Lattner757ee0b2004-06-09 19:41:19 +00001006 int Slot = Machine.getSlot(BB);
1007 if (Slot != -1)
Misha Brukmana6619a92004-06-21 21:53:56 +00001008 Out << Slot;
Chris Lattner757ee0b2004-06-09 19:41:19 +00001009 else
Misha Brukmana6619a92004-06-21 21:53:56 +00001010 Out << "<badref>";
Chris Lattner58185f22002-10-02 19:38:55 +00001011 }
Chris Lattner2447ef52003-11-20 00:09:43 +00001012
1013 if (BB->getParent() == 0)
Misha Brukmana6619a92004-06-21 21:53:56 +00001014 Out << "\t\t; Error: Block without parent!";
Chris Lattner2447ef52003-11-20 00:09:43 +00001015 else {
1016 if (BB != &BB->getParent()->front()) { // Not the entry block?
1017 // Output predecessors for the block...
Misha Brukmana6619a92004-06-21 21:53:56 +00001018 Out << "\t\t;";
Chris Lattner2447ef52003-11-20 00:09:43 +00001019 pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
Misha Brukmanb1c93172005-04-21 23:48:37 +00001020
Chris Lattner2447ef52003-11-20 00:09:43 +00001021 if (PI == PE) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001022 Out << " No predecessors!";
Chris Lattner2447ef52003-11-20 00:09:43 +00001023 } else {
Misha Brukmana6619a92004-06-21 21:53:56 +00001024 Out << " preds =";
Chris Lattner00211f12003-11-16 22:59:57 +00001025 writeOperand(*PI, false, true);
Chris Lattner2447ef52003-11-20 00:09:43 +00001026 for (++PI; PI != PE; ++PI) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001027 Out << ',';
Chris Lattner2447ef52003-11-20 00:09:43 +00001028 writeOperand(*PI, false, true);
1029 }
Chris Lattner00211f12003-11-16 22:59:57 +00001030 }
Chris Lattner58185f22002-10-02 19:38:55 +00001031 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001032 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001033
Misha Brukmana6619a92004-06-21 21:53:56 +00001034 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001035
Misha Brukmana6619a92004-06-21 21:53:56 +00001036 if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001037
Chris Lattnerfee714f2001-09-07 16:36:04 +00001038 // Output all of the instructions in the basic block...
Chris Lattner113f4f42002-06-25 16:13:24 +00001039 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
1040 printInstruction(*I);
Chris Lattner96cdd272004-03-08 18:51:45 +00001041
Misha Brukmana6619a92004-06-21 21:53:56 +00001042 if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001043}
1044
Chris Lattner862e3382001-10-13 06:42:36 +00001045
Misha Brukmanc566ca362004-03-02 00:22:19 +00001046/// printInfoComment - Print a little comment after the instruction indicating
1047/// which slot it occupies.
1048///
Chris Lattner113f4f42002-06-25 16:13:24 +00001049void AssemblyWriter::printInfoComment(const Value &V) {
1050 if (V.getType() != Type::VoidTy) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001051 Out << "\t\t; <";
Misha Brukman21bbdb92004-06-04 21:11:51 +00001052 printType(V.getType()) << '>';
Chris Lattner862e3382001-10-13 06:42:36 +00001053
Chris Lattner113f4f42002-06-25 16:13:24 +00001054 if (!V.hasName()) {
Chris Lattner757ee0b2004-06-09 19:41:19 +00001055 int SlotNum = Machine.getSlot(&V);
1056 if (SlotNum == -1)
Misha Brukmana6619a92004-06-21 21:53:56 +00001057 Out << ":<badref>";
Reid Spencer8beac692004-06-09 15:26:53 +00001058 else
Misha Brukmana6619a92004-06-21 21:53:56 +00001059 Out << ':' << SlotNum; // Print out the def slot taken.
Chris Lattner862e3382001-10-13 06:42:36 +00001060 }
Chris Lattnerb6c21db2005-02-01 01:24:01 +00001061 Out << " [#uses=" << V.getNumUses() << ']'; // Output # uses
Chris Lattner862e3382001-10-13 06:42:36 +00001062 }
1063}
1064
Reid Spencer8beac692004-06-09 15:26:53 +00001065/// printInstruction - This member is called for each Instruction in a function..
Misha Brukmanc566ca362004-03-02 00:22:19 +00001066///
Chris Lattner113f4f42002-06-25 16:13:24 +00001067void AssemblyWriter::printInstruction(const Instruction &I) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001068 if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001069
Misha Brukmana6619a92004-06-21 21:53:56 +00001070 Out << "\t";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001071
1072 // Print out name if it exists...
Chris Lattner113f4f42002-06-25 16:13:24 +00001073 if (I.hasName())
Misha Brukmana6619a92004-06-21 21:53:56 +00001074 Out << getLLVMName(I.getName()) << " = ";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001075
Chris Lattner06038452005-05-06 05:51:46 +00001076 // If this is a volatile load or store, print out the volatile marker.
Chris Lattner504f9242003-09-08 17:45:59 +00001077 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
Chris Lattner06038452005-05-06 05:51:46 +00001078 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile())) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001079 Out << "volatile ";
Chris Lattner06038452005-05-06 05:51:46 +00001080 } else if (isa<CallInst>(I) && cast<CallInst>(I).isTailCall()) {
1081 // If this is a call, check if it's a tail call.
1082 Out << "tail ";
1083 }
Chris Lattner504f9242003-09-08 17:45:59 +00001084
Chris Lattner2f7c9632001-06-06 20:29:01 +00001085 // Print out the opcode...
Misha Brukmana6619a92004-06-21 21:53:56 +00001086 Out << I.getOpcodeName();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001087
1088 // Print out the type of the operands...
Chris Lattner113f4f42002-06-25 16:13:24 +00001089 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001090
1091 // Special case conditional branches to swizzle the condition out to the front
Chris Lattner113f4f42002-06-25 16:13:24 +00001092 if (isa<BranchInst>(I) && I.getNumOperands() > 1) {
1093 writeOperand(I.getOperand(2), true);
Misha Brukmana6619a92004-06-21 21:53:56 +00001094 Out << ',';
Chris Lattner2f7c9632001-06-06 20:29:01 +00001095 writeOperand(Operand, true);
Misha Brukmana6619a92004-06-21 21:53:56 +00001096 Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001097 writeOperand(I.getOperand(1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001098
Chris Lattner8d48df22002-04-13 18:34:38 +00001099 } else if (isa<SwitchInst>(I)) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001100 // Special case switch statement to get formatting nice and correct...
Misha Brukmana6619a92004-06-21 21:53:56 +00001101 writeOperand(Operand , true); Out << ',';
1102 writeOperand(I.getOperand(1), true); Out << " [";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001103
Chris Lattner113f4f42002-06-25 16:13:24 +00001104 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001105 Out << "\n\t\t";
1106 writeOperand(I.getOperand(op ), true); Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001107 writeOperand(I.getOperand(op+1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001108 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001109 Out << "\n\t]";
Chris Lattnerda558102001-10-02 03:41:24 +00001110 } else if (isa<PHINode>(I)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001111 Out << ' ';
Chris Lattner113f4f42002-06-25 16:13:24 +00001112 printType(I.getType());
Misha Brukmana6619a92004-06-21 21:53:56 +00001113 Out << ' ';
Chris Lattner2f7c9632001-06-06 20:29:01 +00001114
Chris Lattner113f4f42002-06-25 16:13:24 +00001115 for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001116 if (op) Out << ", ";
Misha Brukmanb1c93172005-04-21 23:48:37 +00001117 Out << '[';
Misha Brukmana6619a92004-06-21 21:53:56 +00001118 writeOperand(I.getOperand(op ), false); Out << ',';
1119 writeOperand(I.getOperand(op+1), false); Out << " ]";
Chris Lattner931ef3b2001-06-11 15:04:20 +00001120 }
Chris Lattner862e3382001-10-13 06:42:36 +00001121 } else if (isa<ReturnInst>(I) && !Operand) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001122 Out << " void";
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001123 } else if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
1124 // Print the calling convention being used.
1125 switch (CI->getCallingConv()) {
1126 case CallingConv::C: break; // default
1127 case CallingConv::Fast: Out << " fastcc"; break;
1128 case CallingConv::Cold: Out << " coldcc"; break;
1129 default: Out << " cc" << CI->getCallingConv(); break;
1130 }
1131
Chris Lattner463d6a52003-08-05 15:34:45 +00001132 const PointerType *PTy = cast<PointerType>(Operand->getType());
1133 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1134 const Type *RetTy = FTy->getReturnType();
Chris Lattner2f2d9472001-11-06 21:28:12 +00001135
Chris Lattner463d6a52003-08-05 15:34:45 +00001136 // If possible, print out the short form of the call instruction. We can
Chris Lattner6915f8f2002-04-07 22:49:37 +00001137 // only do this if the first argument is a pointer to a nonvararg function,
Chris Lattner463d6a52003-08-05 15:34:45 +00001138 // and if the return type is not a pointer to a function.
Chris Lattner2f2d9472001-11-06 21:28:12 +00001139 //
Chris Lattner463d6a52003-08-05 15:34:45 +00001140 if (!FTy->isVarArg() &&
Misha Brukmanb1c93172005-04-21 23:48:37 +00001141 (!isa<PointerType>(RetTy) ||
Chris Lattnerd9a36a62002-07-25 20:58:51 +00001142 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001143 Out << ' '; printType(RetTy);
Chris Lattner2f2d9472001-11-06 21:28:12 +00001144 writeOperand(Operand, false);
1145 } else {
1146 writeOperand(Operand, true);
1147 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001148 Out << '(';
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001149 if (CI->getNumOperands() > 1) writeOperand(CI->getOperand(1), true);
Chris Lattner113f4f42002-06-25 16:13:24 +00001150 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; ++op) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001151 Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001152 writeOperand(I.getOperand(op), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001153 }
1154
Misha Brukmana6619a92004-06-21 21:53:56 +00001155 Out << " )";
Chris Lattner113f4f42002-06-25 16:13:24 +00001156 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Chris Lattner463d6a52003-08-05 15:34:45 +00001157 const PointerType *PTy = cast<PointerType>(Operand->getType());
1158 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1159 const Type *RetTy = FTy->getReturnType();
1160
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001161 // Print the calling convention being used.
1162 switch (II->getCallingConv()) {
1163 case CallingConv::C: break; // default
1164 case CallingConv::Fast: Out << " fastcc"; break;
1165 case CallingConv::Cold: Out << " coldcc"; break;
1166 default: Out << " cc" << II->getCallingConv(); break;
1167 }
1168
Chris Lattner463d6a52003-08-05 15:34:45 +00001169 // If possible, print out the short form of the invoke instruction. We can
1170 // only do this if the first argument is a pointer to a nonvararg function,
1171 // and if the return type is not a pointer to a function.
1172 //
1173 if (!FTy->isVarArg() &&
Misha Brukmanb1c93172005-04-21 23:48:37 +00001174 (!isa<PointerType>(RetTy) ||
Chris Lattner463d6a52003-08-05 15:34:45 +00001175 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001176 Out << ' '; printType(RetTy);
Chris Lattner463d6a52003-08-05 15:34:45 +00001177 writeOperand(Operand, false);
1178 } else {
1179 writeOperand(Operand, true);
1180 }
1181
Misha Brukmana6619a92004-06-21 21:53:56 +00001182 Out << '(';
Chris Lattner113f4f42002-06-25 16:13:24 +00001183 if (I.getNumOperands() > 3) writeOperand(I.getOperand(3), true);
1184 for (unsigned op = 4, Eop = I.getNumOperands(); op < Eop; ++op) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001185 Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001186 writeOperand(I.getOperand(op), true);
Chris Lattner862e3382001-10-13 06:42:36 +00001187 }
1188
Misha Brukmana6619a92004-06-21 21:53:56 +00001189 Out << " )\n\t\t\tto";
Chris Lattner862e3382001-10-13 06:42:36 +00001190 writeOperand(II->getNormalDest(), true);
Misha Brukmana6619a92004-06-21 21:53:56 +00001191 Out << " unwind";
Chris Lattnerfae8ab32004-02-08 21:44:31 +00001192 writeOperand(II->getUnwindDest(), true);
Chris Lattner862e3382001-10-13 06:42:36 +00001193
Chris Lattner113f4f42002-06-25 16:13:24 +00001194 } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(&I)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001195 Out << ' ';
Chris Lattner8d48df22002-04-13 18:34:38 +00001196 printType(AI->getType()->getElementType());
1197 if (AI->isArrayAllocation()) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001198 Out << ',';
Chris Lattner8d48df22002-04-13 18:34:38 +00001199 writeOperand(AI->getArraySize(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001200 }
Nate Begeman848622f2005-11-05 09:21:28 +00001201 if (AI->getAlignment()) {
Chris Lattner7aeee3a2005-11-05 21:20:34 +00001202 Out << ", align " << AI->getAlignment();
Nate Begeman848622f2005-11-05 09:21:28 +00001203 }
Chris Lattner862e3382001-10-13 06:42:36 +00001204 } else if (isa<CastInst>(I)) {
Chris Lattnerc96e96b2003-11-17 01:17:04 +00001205 if (Operand) writeOperand(Operand, true); // Work with broken code
Misha Brukmana6619a92004-06-21 21:53:56 +00001206 Out << " to ";
Chris Lattner113f4f42002-06-25 16:13:24 +00001207 printType(I.getType());
Chris Lattner5b337482003-10-18 05:57:43 +00001208 } else if (isa<VAArgInst>(I)) {
Chris Lattnerc96e96b2003-11-17 01:17:04 +00001209 if (Operand) writeOperand(Operand, true); // Work with broken code
Misha Brukmana6619a92004-06-21 21:53:56 +00001210 Out << ", ";
Chris Lattnerf70da102003-05-08 02:44:12 +00001211 printType(I.getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +00001212 } else if (Operand) { // Print the normal way...
1213
Misha Brukmanb1c93172005-04-21 23:48:37 +00001214 // PrintAllTypes - Instructions who have operands of all the same type
Chris Lattner2f7c9632001-06-06 20:29:01 +00001215 // omit the type from all but the first operand. If the instruction has
1216 // different type operands (for example br), then they are all printed.
1217 bool PrintAllTypes = false;
1218 const Type *TheType = Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001219
Chris Lattner52bd5cb92004-03-12 05:53:14 +00001220 // Shift Left & Right print both types even for Ubyte LHS, and select prints
1221 // types even if all operands are bools.
Chris Lattner159485f2005-02-09 17:45:03 +00001222 if (isa<ShiftInst>(I) || isa<SelectInst>(I) || isa<StoreInst>(I)) {
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00001223 PrintAllTypes = true;
1224 } else {
1225 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
1226 Operand = I.getOperand(i);
1227 if (Operand->getType() != TheType) {
1228 PrintAllTypes = true; // We have differing types! Print them all!
1229 break;
1230 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001231 }
1232 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001233
Chris Lattner7bfee412001-10-29 16:05:51 +00001234 if (!PrintAllTypes) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001235 Out << ' ';
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00001236 printType(TheType);
Chris Lattner7bfee412001-10-29 16:05:51 +00001237 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001238
Chris Lattner113f4f42002-06-25 16:13:24 +00001239 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001240 if (i) Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001241 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001242 }
1243 }
1244
Chris Lattner862e3382001-10-13 06:42:36 +00001245 printInfoComment(I);
Misha Brukmana6619a92004-06-21 21:53:56 +00001246 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001247}
1248
1249
1250//===----------------------------------------------------------------------===//
1251// External Interface declarations
1252//===----------------------------------------------------------------------===//
1253
Chris Lattner8339f7d2003-10-30 23:41:03 +00001254void Module::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001255 SlotMachine SlotTable(this);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001256 AssemblyWriter W(o, SlotTable, this, AAW);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001257 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001258}
1259
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001260void GlobalVariable::print(std::ostream &o) const {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001261 SlotMachine SlotTable(getParent());
Chris Lattner8339f7d2003-10-30 23:41:03 +00001262 AssemblyWriter W(o, SlotTable, getParent(), 0);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001263 W.write(this);
Chris Lattneradfe0d12001-09-10 20:08:19 +00001264}
1265
Chris Lattner8339f7d2003-10-30 23:41:03 +00001266void Function::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001267 SlotMachine SlotTable(getParent());
Chris Lattner8339f7d2003-10-30 23:41:03 +00001268 AssemblyWriter W(o, SlotTable, getParent(), AAW);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001269
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001270 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001271}
1272
Chris Lattnereef2fe72006-01-24 04:13:11 +00001273void InlineAsm::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Chris Lattnereef2fe72006-01-24 04:13:11 +00001274 assert(0 && "Inline asm printing unimplemented!");
1275 //W.write(this);
1276}
1277
Chris Lattner8339f7d2003-10-30 23:41:03 +00001278void BasicBlock::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001279 SlotMachine SlotTable(getParent());
Misha Brukmanb1c93172005-04-21 23:48:37 +00001280 AssemblyWriter W(o, SlotTable,
Chris Lattner8339f7d2003-10-30 23:41:03 +00001281 getParent() ? getParent()->getParent() : 0, AAW);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001282 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001283}
1284
Chris Lattner8339f7d2003-10-30 23:41:03 +00001285void Instruction::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001286 const Function *F = getParent() ? getParent()->getParent() : 0;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001287 SlotMachine SlotTable(F);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001288 AssemblyWriter W(o, SlotTable, F ? F->getParent() : 0, AAW);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001289
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001290 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001291}
Chris Lattner7db79582001-11-07 04:21:57 +00001292
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001293void Constant::print(std::ostream &o) const {
1294 if (this == 0) { o << "<null> constant value\n"; return; }
Chris Lattner7d734802002-09-10 15:53:49 +00001295
Misha Brukman21bbdb92004-06-04 21:11:51 +00001296 o << ' ' << getType()->getDescription() << ' ';
Chris Lattner1e194682002-04-18 18:53:13 +00001297
Chris Lattnerab7d1ab2003-05-08 02:08:14 +00001298 std::map<const Type *, std::string> TypeTable;
Chris Lattner1e194682002-04-18 18:53:13 +00001299 WriteConstantInt(o, this, false, TypeTable, 0);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001300}
1301
Misha Brukmanb1c93172005-04-21 23:48:37 +00001302void Type::print(std::ostream &o) const {
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001303 if (this == 0)
1304 o << "<null Type>";
1305 else
1306 o << getDescription();
1307}
1308
Chris Lattner2e9fa6d2002-04-09 19:48:49 +00001309void Argument::print(std::ostream &o) const {
Chris Lattnere52ed452004-07-13 23:14:34 +00001310 WriteAsOperand(o, this, true, true,
1311 getParent() ? getParent()->getParent() : 0);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001312}
1313
Reid Spencer52641832004-05-25 18:14:38 +00001314// Value::dump - allow easy printing of Values from the debugger.
1315// Located here because so much of the needed functionality is here.
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001316void Value::dump() const { print(std::cerr); }
Reid Spencer52641832004-05-25 18:14:38 +00001317
1318// Type::dump - allow easy printing of Values from the debugger.
1319// Located here because so much of the needed functionality is here.
Reid Spencere7e96712004-05-25 08:53:40 +00001320void Type::dump() const { print(std::cerr); }
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001321
1322//===----------------------------------------------------------------------===//
1323// CachedWriter Class Implementation
1324//===----------------------------------------------------------------------===//
1325
Chris Lattner7db79582001-11-07 04:21:57 +00001326void CachedWriter::setModule(const Module *M) {
1327 delete SC; delete AW;
1328 if (M) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001329 SC = new SlotMachine(M );
Misha Brukman21bbdb92004-06-04 21:11:51 +00001330 AW = new AssemblyWriter(Out, *SC, M, 0);
Chris Lattner7db79582001-11-07 04:21:57 +00001331 } else {
1332 SC = 0; AW = 0;
1333 }
1334}
1335
1336CachedWriter::~CachedWriter() {
1337 delete AW;
1338 delete SC;
1339}
1340
Chris Lattner60a7dd12004-07-15 02:51:31 +00001341CachedWriter &CachedWriter::operator<<(const Value &V) {
Chris Lattner7db79582001-11-07 04:21:57 +00001342 assert(AW && SC && "CachedWriter does not have a current module!");
Chris Lattner60a7dd12004-07-15 02:51:31 +00001343 if (const Instruction *I = dyn_cast<Instruction>(&V))
Chris Lattner80d2d532004-06-26 19:40:40 +00001344 AW->write(I);
Chris Lattner60a7dd12004-07-15 02:51:31 +00001345 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(&V))
Chris Lattner80d2d532004-06-26 19:40:40 +00001346 AW->write(BB);
Chris Lattner60a7dd12004-07-15 02:51:31 +00001347 else if (const Function *F = dyn_cast<Function>(&V))
Chris Lattner80d2d532004-06-26 19:40:40 +00001348 AW->write(F);
Chris Lattner60a7dd12004-07-15 02:51:31 +00001349 else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(&V))
Chris Lattner80d2d532004-06-26 19:40:40 +00001350 AW->write(GV);
Misha Brukmanb1c93172005-04-21 23:48:37 +00001351 else
Chris Lattner60a7dd12004-07-15 02:51:31 +00001352 AW->writeOperand(&V, true, true);
Chris Lattner7db79582001-11-07 04:21:57 +00001353 return *this;
1354}
Misha Brukman4685e262004-04-28 15:31:21 +00001355
Chris Lattner60a7dd12004-07-15 02:51:31 +00001356CachedWriter& CachedWriter::operator<<(const Type &Ty) {
Misha Brukman4685e262004-04-28 15:31:21 +00001357 if (SymbolicTypes) {
1358 const Module *M = AW->getModule();
Chris Lattner60a7dd12004-07-15 02:51:31 +00001359 if (M) WriteTypeSymbolic(Out, &Ty, M);
Reid Spencer58d30f22004-07-04 11:50:43 +00001360 } else {
Chris Lattner60a7dd12004-07-15 02:51:31 +00001361 AW->write(&Ty);
Reid Spencer58d30f22004-07-04 11:50:43 +00001362 }
1363 return *this;
Misha Brukman4685e262004-04-28 15:31:21 +00001364}
Misha Brukmanda546ea2004-04-28 19:24:28 +00001365
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001366//===----------------------------------------------------------------------===//
1367//===-- SlotMachine Implementation
1368//===----------------------------------------------------------------------===//
1369
1370#if 0
1371#define SC_DEBUG(X) std::cerr << X
1372#else
1373#define SC_DEBUG(X)
1374#endif
1375
1376// Module level constructor. Causes the contents of the Module (sans functions)
1377// to be added to the slot table.
Misha Brukmanb1c93172005-04-21 23:48:37 +00001378SlotMachine::SlotMachine(const Module *M)
Reid Spencer56010e42004-05-26 21:56:09 +00001379 : TheModule(M) ///< Saved for lazy initialization.
1380 , TheFunction(0)
Reid Spencerb0ac8c42004-08-16 07:46:33 +00001381 , FunctionProcessed(false)
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001382 , mMap()
Reid Spencer58d30f22004-07-04 11:50:43 +00001383 , mTypes()
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001384 , fMap()
Reid Spencer58d30f22004-07-04 11:50:43 +00001385 , fTypes()
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001386{
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001387}
1388
1389// Function level constructor. Causes the contents of the Module and the one
1390// function provided to be added to the slot table.
Misha Brukmanb1c93172005-04-21 23:48:37 +00001391SlotMachine::SlotMachine(const Function *F )
Reid Spencer56010e42004-05-26 21:56:09 +00001392 : TheModule( F ? F->getParent() : 0 ) ///< Saved for lazy initialization
1393 , TheFunction(F) ///< Saved for lazy initialization
Reid Spencerb0ac8c42004-08-16 07:46:33 +00001394 , FunctionProcessed(false)
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001395 , mMap()
Reid Spencer58d30f22004-07-04 11:50:43 +00001396 , mTypes()
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001397 , fMap()
Reid Spencer58d30f22004-07-04 11:50:43 +00001398 , fTypes()
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001399{
Reid Spencer56010e42004-05-26 21:56:09 +00001400}
1401
1402inline void SlotMachine::initialize(void) {
Misha Brukmanb1c93172005-04-21 23:48:37 +00001403 if ( TheModule) {
1404 processModule();
Reid Spencer56010e42004-05-26 21:56:09 +00001405 TheModule = 0; ///< Prevent re-processing next time we're called.
1406 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001407 if ( TheFunction && ! FunctionProcessed) {
1408 processFunction();
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001409 }
1410}
1411
1412// Iterate through all the global variables, functions, and global
Misha Brukmanb1c93172005-04-21 23:48:37 +00001413// variable initializers and create slots for them.
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001414void SlotMachine::processModule() {
1415 SC_DEBUG("begin processModule!\n");
1416
1417 // Add all of the global variables to the value table...
Chris Lattner531f9e92005-03-15 04:54:21 +00001418 for (Module::const_global_iterator I = TheModule->global_begin(), E = TheModule->global_end();
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001419 I != E; ++I)
1420 createSlot(I);
1421
1422 // Add all the functions to the table
1423 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
1424 I != E; ++I)
1425 createSlot(I);
1426
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001427 SC_DEBUG("end processModule!\n");
1428}
1429
1430
Reid Spencer56010e42004-05-26 21:56:09 +00001431// Process the arguments, basic blocks, and instructions of a function.
1432void SlotMachine::processFunction() {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001433 SC_DEBUG("begin processFunction!\n");
1434
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001435 // Add all the function arguments
Misha Brukmanb1c93172005-04-21 23:48:37 +00001436 for(Function::const_arg_iterator AI = TheFunction->arg_begin(),
Chris Lattner531f9e92005-03-15 04:54:21 +00001437 AE = TheFunction->arg_end(); AI != AE; ++AI)
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001438 createSlot(AI);
1439
1440 SC_DEBUG("Inserting Instructions:\n");
1441
1442 // Add all of the basic blocks and instructions
Misha Brukmanb1c93172005-04-21 23:48:37 +00001443 for (Function::const_iterator BB = TheFunction->begin(),
Reid Spencer56010e42004-05-26 21:56:09 +00001444 E = TheFunction->end(); BB != E; ++BB) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001445 createSlot(BB);
1446 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
1447 createSlot(I);
1448 }
1449 }
1450
Reid Spencerb0ac8c42004-08-16 07:46:33 +00001451 FunctionProcessed = true;
1452
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001453 SC_DEBUG("end processFunction!\n");
1454}
1455
1456// Clean up after incorporating a function. This is the only way
Reid Spencer56010e42004-05-26 21:56:09 +00001457// to get out of the function incorporation state that affects the
1458// getSlot/createSlot lock. Function incorporation state is indicated
1459// by TheFunction != 0.
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001460void SlotMachine::purgeFunction() {
1461 SC_DEBUG("begin purgeFunction!\n");
1462 fMap.clear(); // Simply discard the function level map
Reid Spencer58d30f22004-07-04 11:50:43 +00001463 fTypes.clear();
Reid Spencer56010e42004-05-26 21:56:09 +00001464 TheFunction = 0;
Reid Spencerb0ac8c42004-08-16 07:46:33 +00001465 FunctionProcessed = false;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001466 SC_DEBUG("end purgeFunction!\n");
1467}
1468
1469/// Get the slot number for a value. This function will assert if you
1470/// ask for a Value that hasn't previously been inserted with createSlot.
1471/// Types are forbidden because Type does not inherit from Value (any more).
Chris Lattner757ee0b2004-06-09 19:41:19 +00001472int SlotMachine::getSlot(const Value *V) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001473 assert( V && "Can't get slot for null Value" );
Misha Brukmanb1c93172005-04-21 23:48:37 +00001474 assert(!isa<Constant>(V) || isa<GlobalValue>(V) &&
1475 "Can't insert a non-GlobalValue Constant into SlotMachine");
Reid Spencer56010e42004-05-26 21:56:09 +00001476
1477 // Check for uninitialized state and do lazy initialization
1478 this->initialize();
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001479
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001480 // Get the type of the value
1481 const Type* VTy = V->getType();
1482
1483 // Find the type plane in the module map
1484 TypedPlanes::const_iterator MI = mMap.find(VTy);
1485
Reid Spencer56010e42004-05-26 21:56:09 +00001486 if ( TheFunction ) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001487 // Lookup the type in the function map too
1488 TypedPlanes::const_iterator FI = fMap.find(VTy);
1489 // If there is a corresponding type plane in the function map
1490 if ( FI != fMap.end() ) {
1491 // Lookup the Value in the function map
1492 ValueMap::const_iterator FVI = FI->second.map.find(V);
1493 // If the value doesn't exist in the function map
1494 if ( FVI == FI->second.map.end() ) {
Chris Lattner68a038e2004-06-09 22:22:10 +00001495 // Look up the value in the module map.
1496 if (MI == mMap.end()) return -1;
Reid Spencer56010e42004-05-26 21:56:09 +00001497 ValueMap::const_iterator MVI = MI->second.map.find(V);
1498 // If we didn't find it, it wasn't inserted
Chris Lattner757ee0b2004-06-09 19:41:19 +00001499 if (MVI == MI->second.map.end()) return -1;
Reid Spencer56010e42004-05-26 21:56:09 +00001500 assert( MVI != MI->second.map.end() && "Value not found");
1501 // We found it only at the module level
Misha Brukmanb1c93172005-04-21 23:48:37 +00001502 return MVI->second;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001503
1504 // else the value exists in the function map
1505 } else {
Reid Spencer56010e42004-05-26 21:56:09 +00001506 // Return the slot number as the module's contribution to
1507 // the type plane plus the index in the function's contribution
1508 // to the type plane.
Chris Lattnerb1f04782004-06-15 21:07:32 +00001509 if (MI != mMap.end())
1510 return MI->second.next_slot + FVI->second;
1511 else
1512 return FVI->second;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001513 }
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001514 }
1515 }
1516
Reid Spencer8beac692004-06-09 15:26:53 +00001517 // N.B. Can get here only if either !TheFunction or the function doesn't
1518 // have a corresponding type plane for the Value
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001519
1520 // Make sure the type plane exists
Chris Lattner757ee0b2004-06-09 19:41:19 +00001521 if (MI == mMap.end()) return -1;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001522 // Lookup the value in the module's map
1523 ValueMap::const_iterator MVI = MI->second.map.find(V);
1524 // Make sure we found it.
Chris Lattner757ee0b2004-06-09 19:41:19 +00001525 if (MVI == MI->second.map.end()) return -1;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001526 // Return it.
1527 return MVI->second;
1528}
1529
Reid Spencer58d30f22004-07-04 11:50:43 +00001530/// Get the slot number for a value. This function will assert if you
1531/// ask for a Value that hasn't previously been inserted with createSlot.
1532/// Types are forbidden because Type does not inherit from Value (any more).
1533int SlotMachine::getSlot(const Type *Ty) {
1534 assert( Ty && "Can't get slot for null Type" );
1535
1536 // Check for uninitialized state and do lazy initialization
1537 this->initialize();
1538
1539 if ( TheFunction ) {
1540 // Lookup the Type in the function map
1541 TypeMap::const_iterator FTI = fTypes.map.find(Ty);
1542 // If the Type doesn't exist in the function map
1543 if ( FTI == fTypes.map.end() ) {
1544 TypeMap::const_iterator MTI = mTypes.map.find(Ty);
1545 // If we didn't find it, it wasn't inserted
Misha Brukmanb1c93172005-04-21 23:48:37 +00001546 if (MTI == mTypes.map.end())
Reid Spencer58d30f22004-07-04 11:50:43 +00001547 return -1;
1548 // We found it only at the module level
Misha Brukmanb1c93172005-04-21 23:48:37 +00001549 return MTI->second;
Reid Spencer58d30f22004-07-04 11:50:43 +00001550
1551 // else the value exists in the function map
1552 } else {
1553 // Return the slot number as the module's contribution to
1554 // the type plane plus the index in the function's contribution
1555 // to the type plane.
1556 return mTypes.next_slot + FTI->second;
1557 }
1558 }
1559
1560 // N.B. Can get here only if either !TheFunction
1561
1562 // Lookup the value in the module's map
1563 TypeMap::const_iterator MTI = mTypes.map.find(Ty);
1564 // Make sure we found it.
1565 if (MTI == mTypes.map.end()) return -1;
1566 // Return it.
1567 return MTI->second;
1568}
1569
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001570// Create a new slot, or return the existing slot if it is already
1571// inserted. Note that the logic here parallels getSlot but instead
1572// of asserting when the Value* isn't found, it inserts the value.
1573unsigned SlotMachine::createSlot(const Value *V) {
1574 assert( V && "Can't insert a null Value to SlotMachine");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001575 assert(!isa<Constant>(V) || isa<GlobalValue>(V) &&
1576 "Can't insert a non-GlobalValue Constant into SlotMachine");
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001577
1578 const Type* VTy = V->getType();
1579
1580 // Just ignore void typed things
1581 if (VTy == Type::VoidTy) return 0; // FIXME: Wrong return value!
1582
1583 // Look up the type plane for the Value's type from the module map
1584 TypedPlanes::const_iterator MI = mMap.find(VTy);
1585
Reid Spencer56010e42004-05-26 21:56:09 +00001586 if ( TheFunction ) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001587 // Get the type plane for the Value's type from the function map
1588 TypedPlanes::const_iterator FI = fMap.find(VTy);
1589 // If there is a corresponding type plane in the function map
1590 if ( FI != fMap.end() ) {
1591 // Lookup the Value in the function map
1592 ValueMap::const_iterator FVI = FI->second.map.find(V);
1593 // If the value doesn't exist in the function map
1594 if ( FVI == FI->second.map.end() ) {
Reid Spencer56010e42004-05-26 21:56:09 +00001595 // If there is no corresponding type plane in the module map
1596 if ( MI == mMap.end() )
1597 return insertValue(V);
1598 // Look up the value in the module map
1599 ValueMap::const_iterator MVI = MI->second.map.find(V);
1600 // If we didn't find it, it wasn't inserted
1601 if ( MVI == MI->second.map.end() )
1602 return insertValue(V);
1603 else
1604 // We found it only at the module level
1605 return MVI->second;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001606
1607 // else the value exists in the function map
1608 } else {
Reid Spencer56010e42004-05-26 21:56:09 +00001609 if ( MI == mMap.end() )
1610 return FVI->second;
1611 else
1612 // Return the slot number as the module's contribution to
1613 // the type plane plus the index in the function's contribution
1614 // to the type plane.
1615 return MI->second.next_slot + FVI->second;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001616 }
1617
1618 // else there is not a corresponding type plane in the function map
1619 } else {
1620 // If the type plane doesn't exists at the module level
1621 if ( MI == mMap.end() ) {
Reid Spencer56010e42004-05-26 21:56:09 +00001622 return insertValue(V);
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001623 // else type plane exists at the module level, examine it
1624 } else {
Reid Spencer56010e42004-05-26 21:56:09 +00001625 // Look up the value in the module's map
1626 ValueMap::const_iterator MVI = MI->second.map.find(V);
1627 // If we didn't find it there either
1628 if ( MVI == MI->second.map.end() )
1629 // Return the slot number as the module's contribution to
1630 // the type plane plus the index of the function map insertion.
1631 return MI->second.next_slot + insertValue(V);
1632 else
1633 return MVI->second;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001634 }
1635 }
1636 }
1637
Reid Spencer56010e42004-05-26 21:56:09 +00001638 // N.B. Can only get here if !TheFunction
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001639
1640 // If the module map's type plane is not for the Value's type
1641 if ( MI != mMap.end() ) {
1642 // Lookup the value in the module's map
1643 ValueMap::const_iterator MVI = MI->second.map.find(V);
Misha Brukmanb1c93172005-04-21 23:48:37 +00001644 if ( MVI != MI->second.map.end() )
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001645 return MVI->second;
1646 }
1647
1648 return insertValue(V);
1649}
1650
Reid Spencer58d30f22004-07-04 11:50:43 +00001651// Create a new slot, or return the existing slot if it is already
1652// inserted. Note that the logic here parallels getSlot but instead
1653// of asserting when the Value* isn't found, it inserts the value.
1654unsigned SlotMachine::createSlot(const Type *Ty) {
1655 assert( Ty && "Can't insert a null Type to SlotMachine");
1656
1657 if ( TheFunction ) {
1658 // Lookup the Type in the function map
1659 TypeMap::const_iterator FTI = fTypes.map.find(Ty);
1660 // If the type doesn't exist in the function map
1661 if ( FTI == fTypes.map.end() ) {
1662 // Look up the type in the module map
1663 TypeMap::const_iterator MTI = mTypes.map.find(Ty);
1664 // If we didn't find it, it wasn't inserted
1665 if ( MTI == mTypes.map.end() )
1666 return insertValue(Ty);
1667 else
1668 // We found it only at the module level
1669 return MTI->second;
1670
1671 // else the value exists in the function map
1672 } else {
1673 // Return the slot number as the module's contribution to
1674 // the type plane plus the index in the function's contribution
1675 // to the type plane.
1676 return mTypes.next_slot + FTI->second;
1677 }
1678 }
1679
1680 // N.B. Can only get here if !TheFunction
1681
1682 // Lookup the type in the module's map
1683 TypeMap::const_iterator MTI = mTypes.map.find(Ty);
Misha Brukmanb1c93172005-04-21 23:48:37 +00001684 if ( MTI != mTypes.map.end() )
Reid Spencer58d30f22004-07-04 11:50:43 +00001685 return MTI->second;
1686
1687 return insertValue(Ty);
1688}
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001689
1690// Low level insert function. Minimal checking is done. This
1691// function is just for the convenience of createSlot (above).
1692unsigned SlotMachine::insertValue(const Value *V ) {
1693 assert(V && "Can't insert a null Value into SlotMachine!");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001694 assert(!isa<Constant>(V) || isa<GlobalValue>(V) &&
1695 "Can't insert a non-GlobalValue Constant into SlotMachine");
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001696
Reid Spencer56010e42004-05-26 21:56:09 +00001697 // If this value does not contribute to a plane (is void)
Misha Brukmanb1c93172005-04-21 23:48:37 +00001698 // or if the value already has a name then ignore it.
Reid Spencer56010e42004-05-26 21:56:09 +00001699 if (V->getType() == Type::VoidTy || V->hasName() ) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001700 SC_DEBUG("ignored value " << *V << "\n");
1701 return 0; // FIXME: Wrong return value
1702 }
1703
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001704 const Type *VTy = V->getType();
1705 unsigned DestSlot = 0;
1706
Reid Spencer56010e42004-05-26 21:56:09 +00001707 if ( TheFunction ) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001708 TypedPlanes::iterator I = fMap.find( VTy );
Misha Brukmanb1c93172005-04-21 23:48:37 +00001709 if ( I == fMap.end() )
Reid Spencer58d30f22004-07-04 11:50:43 +00001710 I = fMap.insert(std::make_pair(VTy,ValuePlane())).first;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001711 DestSlot = I->second.map[V] = I->second.next_slot++;
1712 } else {
1713 TypedPlanes::iterator I = mMap.find( VTy );
1714 if ( I == mMap.end() )
Reid Spencer58d30f22004-07-04 11:50:43 +00001715 I = mMap.insert(std::make_pair(VTy,ValuePlane())).first;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001716 DestSlot = I->second.map[V] = I->second.next_slot++;
1717 }
1718
Misha Brukmanb1c93172005-04-21 23:48:37 +00001719 SC_DEBUG(" Inserting value [" << VTy << "] = " << V << " slot=" <<
Reid Spencer56010e42004-05-26 21:56:09 +00001720 DestSlot << " [");
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001721 // G = Global, C = Constant, T = Type, F = Function, o = other
Misha Brukmanb1c93172005-04-21 23:48:37 +00001722 SC_DEBUG((isa<GlobalVariable>(V) ? 'G' : (isa<Function>(V) ? 'F' :
Reid Spenceraccd7c72004-07-17 23:47:01 +00001723 (isa<Constant>(V) ? 'C' : 'o'))));
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001724 SC_DEBUG("]\n");
1725 return DestSlot;
1726}
1727
Reid Spencer58d30f22004-07-04 11:50:43 +00001728// Low level insert function. Minimal checking is done. This
1729// function is just for the convenience of createSlot (above).
1730unsigned SlotMachine::insertValue(const Type *Ty ) {
1731 assert(Ty && "Can't insert a null Type into SlotMachine!");
1732
1733 unsigned DestSlot = 0;
1734
1735 if ( TheFunction ) {
1736 DestSlot = fTypes.map[Ty] = fTypes.next_slot++;
1737 } else {
1738 DestSlot = fTypes.map[Ty] = fTypes.next_slot++;
1739 }
1740 SC_DEBUG(" Inserting type [" << DestSlot << "] = " << Ty << "\n");
1741 return DestSlot;
1742}
1743
Reid Spencere7e96712004-05-25 08:53:40 +00001744// vim: sw=2