blob: 163383934c0bba66ec09595d9efe3ec193716a44 [file] [log] [blame]
Chris Lattnerf7e79482002-04-07 22:31:46 +00001//===-- AsmWriter.cpp - Printing LLVM as an assembly file -----------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
10// This library implements the functionality defined in llvm/Assembly/Writer.h
11//
Chris Lattner189088e2002-04-12 18:21:53 +000012// Note that these routines must be extremely tolerant of various errors in the
Chris Lattnerf70da102003-05-08 02:44:12 +000013// LLVM code, because it can be used for debugging transformations.
Chris Lattner189088e2002-04-12 18:21:53 +000014//
Chris Lattner2f7c9632001-06-06 20:29:01 +000015//===----------------------------------------------------------------------===//
16
Chris Lattner7db79582001-11-07 04:21:57 +000017#include "llvm/Assembly/CachedWriter.h"
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +000018#include "llvm/Assembly/Writer.h"
Chris Lattner7f8845a2002-07-23 18:07:49 +000019#include "llvm/Assembly/PrintModulePass.h"
Chris Lattner8339f7d2003-10-30 23:41:03 +000020#include "llvm/Assembly/AsmAnnotationWriter.h"
Chris Lattnerf7b6d312005-05-06 20:26:43 +000021#include "llvm/CallingConv.h"
Chris Lattnerc70b3f62004-01-20 19:50:34 +000022#include "llvm/Constants.h"
Chris Lattner913d18f2002-04-29 18:46:50 +000023#include "llvm/DerivedTypes.h"
Chris Lattner8bbcda22006-01-25 18:57:27 +000024#include "llvm/InlineAsm.h"
Vikram S. Adveb952b542002-07-14 23:14:45 +000025#include "llvm/Instruction.h"
Misha Brukman2d3fa9e2004-07-29 16:53:53 +000026#include "llvm/Instructions.h"
Chris Lattnerc70b3f62004-01-20 19:50:34 +000027#include "llvm/Module.h"
Chris Lattnerfee714f2001-09-07 16:36:04 +000028#include "llvm/SymbolTable.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000029#include "llvm/ADT/StringExtras.h"
30#include "llvm/ADT/STLExtras.h"
Bill Wendlingdfc91892006-11-28 02:09:03 +000031#include "llvm/Support/CFG.h"
Jim Laskeyb74c6662005-08-17 19:34:49 +000032#include "llvm/Support/MathExtras.h"
Bill Wendlingdfc91892006-11-28 02:09:03 +000033#include "llvm/Support/Streams.h"
Chris Lattnerfee714f2001-09-07 16:36:04 +000034#include <algorithm>
Chris Lattner189d19f2003-11-21 20:23:48 +000035using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000036
Chris Lattner60a7dd12004-07-15 02:51:31 +000037namespace llvm {
Reid Spencer16f2f7f2004-05-26 07:18:52 +000038
Reid Spencer294715b2005-05-15 16:13:11 +000039// Make virtual table appear in this compilation unit.
40AssemblyAnnotationWriter::~AssemblyAnnotationWriter() {}
41
Reid Spencer16f2f7f2004-05-26 07:18:52 +000042/// This class provides computation of slot numbers for LLVM Assembly writing.
43/// @brief LLVM Assembly Writing Slot Computation.
44class SlotMachine {
45
46/// @name Types
47/// @{
48public:
49
50 /// @brief A mapping of Values to slot numbers
51 typedef std::map<const Value*, unsigned> ValueMap;
Reid Spencer58d30f22004-07-04 11:50:43 +000052 typedef std::map<const Type*, unsigned> TypeMap;
Reid Spencer16f2f7f2004-05-26 07:18:52 +000053
54 /// @brief A plane with next slot number and ValueMap
Misha Brukmanb1c93172005-04-21 23:48:37 +000055 struct ValuePlane {
Reid Spencer16f2f7f2004-05-26 07:18:52 +000056 unsigned next_slot; ///< The next slot number to use
57 ValueMap map; ///< The map of Value* -> unsigned
Reid Spencer58d30f22004-07-04 11:50:43 +000058 ValuePlane() { next_slot = 0; } ///< Make sure we start at 0
59 };
60
61 struct TypePlane {
62 unsigned next_slot;
63 TypeMap map;
64 TypePlane() { next_slot = 0; }
65 void clear() { map.clear(); next_slot = 0; }
Reid Spencer16f2f7f2004-05-26 07:18:52 +000066 };
67
68 /// @brief The map of planes by Type
Reid Spencer58d30f22004-07-04 11:50:43 +000069 typedef std::map<const Type*, ValuePlane> TypedPlanes;
Reid Spencer16f2f7f2004-05-26 07:18:52 +000070
71/// @}
72/// @name Constructors
73/// @{
74public:
75 /// @brief Construct from a module
76 SlotMachine(const Module *M );
77
78 /// @brief Construct from a function, starting out in incorp state.
79 SlotMachine(const Function *F );
80
81/// @}
82/// @name Accessors
83/// @{
84public:
85 /// Return the slot number of the specified value in it's type
86 /// plane. Its an error to ask for something not in the SlotMachine.
87 /// Its an error to ask for a Type*
Chris Lattner757ee0b2004-06-09 19:41:19 +000088 int getSlot(const Value *V);
Reid Spencer58d30f22004-07-04 11:50:43 +000089 int getSlot(const Type*Ty);
Reid Spencer8beac692004-06-09 15:26:53 +000090
91 /// Determine if a Value has a slot or not
92 bool hasSlot(const Value* V);
Reid Spencer58d30f22004-07-04 11:50:43 +000093 bool hasSlot(const Type* Ty);
Reid Spencer16f2f7f2004-05-26 07:18:52 +000094
95/// @}
96/// @name Mutators
97/// @{
98public:
Misha Brukmanb1c93172005-04-21 23:48:37 +000099 /// If you'd like to deal with a function instead of just a module, use
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000100 /// this method to get its data into the SlotMachine.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000101 void incorporateFunction(const Function *F) {
102 TheFunction = F;
Reid Spencerb0ac8c42004-08-16 07:46:33 +0000103 FunctionProcessed = false;
104 }
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000105
Misha Brukmanb1c93172005-04-21 23:48:37 +0000106 /// After calling incorporateFunction, use this method to remove the
107 /// most recently incorporated function from the SlotMachine. This
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000108 /// will reset the state of the machine back to just the module contents.
109 void purgeFunction();
110
111/// @}
112/// @name Implementation Details
113/// @{
114private:
Reid Spencer56010e42004-05-26 21:56:09 +0000115 /// This function does the actual initialization.
116 inline void initialize();
117
Misha Brukmanb1c93172005-04-21 23:48:37 +0000118 /// Values can be crammed into here at will. If they haven't
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000119 /// been inserted already, they get inserted, otherwise they are ignored.
120 /// Either way, the slot number for the Value* is returned.
121 unsigned createSlot(const Value *V);
Reid Spencer58d30f22004-07-04 11:50:43 +0000122 unsigned createSlot(const Type* Ty);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000123
124 /// Insert a value into the value table. Return the slot number
125 /// that it now occupies. BadThings(TM) will happen if you insert a
Misha Brukmanb1c93172005-04-21 23:48:37 +0000126 /// Value that's already been inserted.
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000127 unsigned insertValue( const Value *V );
Reid Spencer58d30f22004-07-04 11:50:43 +0000128 unsigned insertValue( const Type* Ty);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000129
130 /// Add all of the module level global variables (and their initializers)
131 /// and function declarations, but not the contents of those functions.
132 void processModule();
133
Reid Spencer56010e42004-05-26 21:56:09 +0000134 /// Add all of the functions arguments, basic blocks, and instructions
135 void processFunction();
136
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000137 SlotMachine(const SlotMachine &); // DO NOT IMPLEMENT
138 void operator=(const SlotMachine &); // DO NOT IMPLEMENT
139
140/// @}
141/// @name Data
142/// @{
143public:
144
145 /// @brief The module for which we are holding slot numbers
Reid Spencer56010e42004-05-26 21:56:09 +0000146 const Module* TheModule;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000147
Reid Spencer56010e42004-05-26 21:56:09 +0000148 /// @brief The function for which we are holding slot numbers
149 const Function* TheFunction;
Reid Spencerb0ac8c42004-08-16 07:46:33 +0000150 bool FunctionProcessed;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000151
152 /// @brief The TypePlanes map for the module level data
153 TypedPlanes mMap;
Reid Spencer58d30f22004-07-04 11:50:43 +0000154 TypePlane mTypes;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000155
156 /// @brief The TypePlanes map for the function level data
157 TypedPlanes fMap;
Reid Spencer58d30f22004-07-04 11:50:43 +0000158 TypePlane fTypes;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000159
160/// @}
161
162};
163
Chris Lattner60a7dd12004-07-15 02:51:31 +0000164} // end namespace llvm
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000165
Chris Lattnerc2d3d312006-08-27 22:42:52 +0000166static RegisterPass<PrintModulePass>
Chris Lattnere7134c52006-08-21 17:20:01 +0000167X("printm", "Print module to stderr");
Chris Lattnerc2d3d312006-08-27 22:42:52 +0000168static RegisterPass<PrintFunctionPass>
Chris Lattnere7134c52006-08-21 17:20:01 +0000169Y("print","Print function to stderr");
Chris Lattner7f8845a2002-07-23 18:07:49 +0000170
Misha Brukmanb1c93172005-04-21 23:48:37 +0000171static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000172 bool PrintName,
173 std::map<const Type *, std::string> &TypeTable,
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000174 SlotMachine *Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000175
Misha Brukmanb1c93172005-04-21 23:48:37 +0000176static void WriteAsOperandInternal(std::ostream &Out, const Type *T,
Reid Spencer58d30f22004-07-04 11:50:43 +0000177 bool PrintName,
178 std::map<const Type *, std::string> &TypeTable,
179 SlotMachine *Machine);
180
Chris Lattnerb86620e2001-10-29 16:37:48 +0000181static const Module *getModuleFromVal(const Value *V) {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000182 if (const Argument *MA = dyn_cast<Argument>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +0000183 return MA->getParent() ? MA->getParent()->getParent() : 0;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000184 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +0000185 return BB->getParent() ? BB->getParent()->getParent() : 0;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000186 else if (const Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner57698e22002-03-26 18:01:55 +0000187 const Function *M = I->getParent() ? I->getParent()->getParent() : 0;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000188 return M ? M->getParent() : 0;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000189 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +0000190 return GV->getParent();
Chris Lattnerb86620e2001-10-29 16:37:48 +0000191 return 0;
192}
193
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000194static SlotMachine *createSlotMachine(const Value *V) {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000195 if (const Argument *FA = dyn_cast<Argument>(V)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000196 return new SlotMachine(FA->getParent());
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000197 } else if (const Instruction *I = dyn_cast<Instruction>(V)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000198 return new SlotMachine(I->getParent()->getParent());
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000199 } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000200 return new SlotMachine(BB->getParent());
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000201 } else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)){
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000202 return new SlotMachine(GV->getParent());
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000203 } else if (const Function *Func = dyn_cast<Function>(V)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000204 return new SlotMachine(Func);
Chris Lattner7bfee412001-10-29 16:05:51 +0000205 }
206 return 0;
207}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000208
Chris Lattner1c343042003-08-22 05:40:38 +0000209// getLLVMName - Turn the specified string into an 'LLVM name', which is either
210// prefixed with % (if the string only contains simple characters) or is
211// surrounded with ""'s (if it has special chars in it).
Alkis Evlogimenos991d6ad2004-12-10 05:41:10 +0000212static std::string getLLVMName(const std::string &Name,
213 bool prefixName = true) {
Chris Lattner1c343042003-08-22 05:40:38 +0000214 assert(!Name.empty() && "Cannot get empty name!");
215
216 // First character cannot start with a number...
217 if (Name[0] >= '0' && Name[0] <= '9')
218 return "\"" + Name + "\"";
219
220 // Scan to see if we have any characters that are not on the "white list"
221 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
222 char C = Name[i];
223 assert(C != '"' && "Illegal character in LLVM value name!");
224 if ((C < 'a' || C > 'z') && (C < 'A' || C > 'Z') && (C < '0' || C > '9') &&
225 C != '-' && C != '.' && C != '_')
226 return "\"" + Name + "\"";
227 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000228
Chris Lattner1c343042003-08-22 05:40:38 +0000229 // If we get here, then the identifier is legal to use as a "VarID".
Alkis Evlogimenos991d6ad2004-12-10 05:41:10 +0000230 if (prefixName)
231 return "%"+Name;
232 else
233 return Name;
Chris Lattner1c343042003-08-22 05:40:38 +0000234}
235
Chris Lattnerb86620e2001-10-29 16:37:48 +0000236
Misha Brukmanc566ca362004-03-02 00:22:19 +0000237/// fillTypeNameTable - If the module has a symbol table, take all global types
238/// and stuff their names into the TypeNames map.
239///
Chris Lattnerb86620e2001-10-29 16:37:48 +0000240static void fillTypeNameTable(const Module *M,
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000241 std::map<const Type *, std::string> &TypeNames) {
Chris Lattner98cf1f52002-11-20 18:36:02 +0000242 if (!M) return;
243 const SymbolTable &ST = M->getSymbolTable();
Reid Spencere7e96712004-05-25 08:53:40 +0000244 SymbolTable::type_const_iterator TI = ST.type_begin();
245 for (; TI != ST.type_end(); ++TI ) {
246 // As a heuristic, don't insert pointer to primitive types, because
247 // they are used too often to have a single useful name.
248 //
249 const Type *Ty = cast<Type>(TI->second);
250 if (!isa<PointerType>(Ty) ||
Reid Spencer56010e42004-05-26 21:56:09 +0000251 !cast<PointerType>(Ty)->getElementType()->isPrimitiveType() ||
252 isa<OpaqueType>(cast<PointerType>(Ty)->getElementType()))
Reid Spencere7e96712004-05-25 08:53:40 +0000253 TypeNames.insert(std::make_pair(Ty, getLLVMName(TI->first)));
Chris Lattnerb86620e2001-10-29 16:37:48 +0000254 }
255}
256
257
258
Misha Brukmanb1c93172005-04-21 23:48:37 +0000259static void calcTypeName(const Type *Ty,
John Criswellcd116ba2004-06-01 14:54:08 +0000260 std::vector<const Type *> &TypeStack,
261 std::map<const Type *, std::string> &TypeNames,
262 std::string & Result){
263 if (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty)) {
264 Result += Ty->getDescription(); // Base case
265 return;
266 }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000267
268 // Check to see if the type is named.
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000269 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
John Criswellcd116ba2004-06-01 14:54:08 +0000270 if (I != TypeNames.end()) {
271 Result += I->second;
272 return;
273 }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000274
John Criswellcd116ba2004-06-01 14:54:08 +0000275 if (isa<OpaqueType>(Ty)) {
276 Result += "opaque";
277 return;
278 }
Chris Lattnerf14ead92003-10-30 00:22:33 +0000279
Chris Lattnerb86620e2001-10-29 16:37:48 +0000280 // Check to see if the Type is already on the stack...
281 unsigned Slot = 0, CurSize = TypeStack.size();
282 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
283
Misha Brukmanb1c93172005-04-21 23:48:37 +0000284 // This is another base case for the recursion. In this case, we know
Chris Lattnerb86620e2001-10-29 16:37:48 +0000285 // that we have looped back to a type that we have previously visited.
286 // Generate the appropriate upreference to handle this.
John Criswellcd116ba2004-06-01 14:54:08 +0000287 if (Slot < CurSize) {
288 Result += "\\" + utostr(CurSize-Slot); // Here's the upreference
289 return;
290 }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000291
292 TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
Misha Brukmanb1c93172005-04-21 23:48:37 +0000293
Chris Lattner6b727592004-06-17 18:19:28 +0000294 switch (Ty->getTypeID()) {
Chris Lattner91db5822002-03-29 03:44:36 +0000295 case Type::FunctionTyID: {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000296 const FunctionType *FTy = cast<FunctionType>(Ty);
John Criswellcd116ba2004-06-01 14:54:08 +0000297 calcTypeName(FTy->getReturnType(), TypeStack, TypeNames, Result);
298 Result += " (";
Chris Lattnerfa829be2004-02-09 04:14:01 +0000299 for (FunctionType::param_iterator I = FTy->param_begin(),
300 E = FTy->param_end(); I != E; ++I) {
301 if (I != FTy->param_begin())
Chris Lattnerb86620e2001-10-29 16:37:48 +0000302 Result += ", ";
John Criswellcd116ba2004-06-01 14:54:08 +0000303 calcTypeName(*I, TypeStack, TypeNames, Result);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000304 }
Chris Lattnerd816b532002-04-13 20:53:41 +0000305 if (FTy->isVarArg()) {
Chris Lattnerfa829be2004-02-09 04:14:01 +0000306 if (FTy->getNumParams()) Result += ", ";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000307 Result += "...";
308 }
309 Result += ")";
310 break;
311 }
312 case Type::StructTyID: {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000313 const StructType *STy = cast<StructType>(Ty);
John Criswellcd116ba2004-06-01 14:54:08 +0000314 Result += "{ ";
Chris Lattnerac6db752004-02-09 04:37:31 +0000315 for (StructType::element_iterator I = STy->element_begin(),
316 E = STy->element_end(); I != E; ++I) {
317 if (I != STy->element_begin())
Chris Lattnerb86620e2001-10-29 16:37:48 +0000318 Result += ", ";
John Criswellcd116ba2004-06-01 14:54:08 +0000319 calcTypeName(*I, TypeStack, TypeNames, Result);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000320 }
321 Result += " }";
322 break;
323 }
324 case Type::PointerTyID:
Misha Brukmanb1c93172005-04-21 23:48:37 +0000325 calcTypeName(cast<PointerType>(Ty)->getElementType(),
John Criswellcd116ba2004-06-01 14:54:08 +0000326 TypeStack, TypeNames, Result);
327 Result += "*";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000328 break;
329 case Type::ArrayTyID: {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000330 const ArrayType *ATy = cast<ArrayType>(Ty);
John Criswellcd116ba2004-06-01 14:54:08 +0000331 Result += "[" + utostr(ATy->getNumElements()) + " x ";
332 calcTypeName(ATy->getElementType(), TypeStack, TypeNames, Result);
333 Result += "]";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000334 break;
335 }
Brian Gaeke02209042004-08-20 06:00:58 +0000336 case Type::PackedTyID: {
337 const PackedType *PTy = cast<PackedType>(Ty);
338 Result += "<" + utostr(PTy->getNumElements()) + " x ";
339 calcTypeName(PTy->getElementType(), TypeStack, TypeNames, Result);
340 Result += ">";
341 break;
342 }
Chris Lattner15285ab2003-05-14 17:50:47 +0000343 case Type::OpaqueTyID:
John Criswellcd116ba2004-06-01 14:54:08 +0000344 Result += "opaque";
Chris Lattner15285ab2003-05-14 17:50:47 +0000345 break;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000346 default:
John Criswellcd116ba2004-06-01 14:54:08 +0000347 Result += "<unrecognized-type>";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000348 }
349
350 TypeStack.pop_back(); // Remove self from stack...
John Criswellcd116ba2004-06-01 14:54:08 +0000351 return;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000352}
353
354
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000355/// printTypeInt - The internal guts of printing out a type that has a
356/// potentially named portion.
357///
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000358static std::ostream &printTypeInt(std::ostream &Out, const Type *Ty,
359 std::map<const Type *, std::string> &TypeNames) {
Chris Lattnerb86620e2001-10-29 16:37:48 +0000360 // Primitive types always print out their description, regardless of whether
361 // they have been named or not.
362 //
Chris Lattner92d60532003-10-30 00:12:51 +0000363 if (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty))
364 return Out << Ty->getDescription();
Chris Lattnerb86620e2001-10-29 16:37:48 +0000365
366 // Check to see if the type is named.
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000367 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000368 if (I != TypeNames.end()) return Out << I->second;
369
370 // Otherwise we have a type that has not been named but is a derived type.
371 // Carefully recurse the type hierarchy to print out any contained symbolic
372 // names.
373 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000374 std::vector<const Type *> TypeStack;
John Criswellcd116ba2004-06-01 14:54:08 +0000375 std::string TypeName;
376 calcTypeName(Ty, TypeStack, TypeNames, TypeName);
Chris Lattner7f74a562002-01-20 22:54:45 +0000377 TypeNames.insert(std::make_pair(Ty, TypeName));//Cache type name for later use
John Criswellcd116ba2004-06-01 14:54:08 +0000378 return (Out << TypeName);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000379}
380
Chris Lattner34b95182001-10-31 04:33:19 +0000381
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000382/// WriteTypeSymbolic - This attempts to write the specified type as a symbolic
383/// type, iff there is an entry in the modules symbol table for the specified
384/// type or one of it's component types. This is slower than a simple x << Type
385///
Chris Lattner189d19f2003-11-21 20:23:48 +0000386std::ostream &llvm::WriteTypeSymbolic(std::ostream &Out, const Type *Ty,
387 const Module *M) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000388 Out << ' ';
Chris Lattnerb86620e2001-10-29 16:37:48 +0000389
390 // If they want us to print out a type, attempt to make it symbolic if there
391 // is a symbol table in the module...
Chris Lattner98cf1f52002-11-20 18:36:02 +0000392 if (M) {
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000393 std::map<const Type *, std::string> TypeNames;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000394 fillTypeNameTable(M, TypeNames);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000395
Chris Lattner72f866e2001-10-29 16:40:32 +0000396 return printTypeInt(Out, Ty, TypeNames);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000397 } else {
Chris Lattner72f866e2001-10-29 16:40:32 +0000398 return Out << Ty->getDescription();
Chris Lattnerb86620e2001-10-29 16:37:48 +0000399 }
400}
401
Chris Lattner6ed87bd2006-01-23 23:03:36 +0000402// PrintEscapedString - Print each character of the specified string, escaping
403// it if it is not printable or if it is an escape char.
404static void PrintEscapedString(const std::string &Str, std::ostream &Out) {
405 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
406 unsigned char C = Str[i];
407 if (isprint(C) && C != '"' && C != '\\') {
408 Out << C;
409 } else {
410 Out << '\\'
411 << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'))
412 << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
413 }
414 }
415}
416
Reid Spencer812a1be2006-12-04 05:19:18 +0000417static const char * getPredicateText(unsigned predicate) {
418 const char * pred = "unknown";
419 switch (predicate) {
420 case FCmpInst::FCMP_FALSE: pred = "false"; break;
421 case FCmpInst::FCMP_OEQ: pred = "oeq"; break;
422 case FCmpInst::FCMP_OGT: pred = "ogt"; break;
423 case FCmpInst::FCMP_OGE: pred = "oge"; break;
424 case FCmpInst::FCMP_OLT: pred = "olt"; break;
425 case FCmpInst::FCMP_OLE: pred = "ole"; break;
426 case FCmpInst::FCMP_ONE: pred = "one"; break;
427 case FCmpInst::FCMP_ORD: pred = "ord"; break;
428 case FCmpInst::FCMP_UNO: pred = "uno"; break;
429 case FCmpInst::FCMP_UEQ: pred = "ueq"; break;
430 case FCmpInst::FCMP_UGT: pred = "ugt"; break;
431 case FCmpInst::FCMP_UGE: pred = "uge"; break;
432 case FCmpInst::FCMP_ULT: pred = "ult"; break;
433 case FCmpInst::FCMP_ULE: pred = "ule"; break;
434 case FCmpInst::FCMP_UNE: pred = "une"; break;
435 case FCmpInst::FCMP_TRUE: pred = "true"; break;
436 case ICmpInst::ICMP_EQ: pred = "eq"; break;
437 case ICmpInst::ICMP_NE: pred = "ne"; break;
438 case ICmpInst::ICMP_SGT: pred = "sgt"; break;
439 case ICmpInst::ICMP_SGE: pred = "sge"; break;
440 case ICmpInst::ICMP_SLT: pred = "slt"; break;
441 case ICmpInst::ICMP_SLE: pred = "sle"; break;
442 case ICmpInst::ICMP_UGT: pred = "ugt"; break;
443 case ICmpInst::ICMP_UGE: pred = "uge"; break;
444 case ICmpInst::ICMP_ULT: pred = "ult"; break;
445 case ICmpInst::ICMP_ULE: pred = "ule"; break;
446 }
447 return pred;
448}
449
Misha Brukmanb1c93172005-04-21 23:48:37 +0000450/// @brief Internal constant writer.
451static void WriteConstantInt(std::ostream &Out, const Constant *CV,
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000452 bool PrintName,
453 std::map<const Type *, std::string> &TypeTable,
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000454 SlotMachine *Machine) {
Jim Laskey6be3d8e2006-02-27 10:33:53 +0000455 const int IndentSize = 4;
456 static std::string Indent = "\n";
Chris Lattner1e194682002-04-18 18:53:13 +0000457 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
Chris Lattner94e39cb2006-09-28 22:50:29 +0000458 Out << (CB->getValue() ? "true" : "false");
Reid Spencere0fc4df2006-10-20 07:07:24 +0000459 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
460 if (CI->getType()->isSigned())
461 Out << CI->getSExtValue();
462 else
463 Out << CI->getZExtValue();
Chris Lattner1e194682002-04-18 18:53:13 +0000464 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
465 // We would like to output the FP constant value in exponential notation,
466 // but we cannot do this if doing so will lose precision. Check here to
467 // make sure that we only output it in exponential format if we can parse
468 // the value back and get the same value.
469 //
470 std::string StrVal = ftostr(CFP->getValue());
471
472 // Check to make sure that the stringized number is not some string like
473 // "Inf" or NaN, that atof will accept, but the lexer will not. Check that
474 // the string matches the "[-+]?[0-9]" regex.
475 //
476 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
477 ((StrVal[0] == '-' || StrVal[0] == '+') &&
Brian Gaeke87b4f072003-06-17 23:55:35 +0000478 (StrVal[1] >= '0' && StrVal[1] <= '9')))
Chris Lattner1e194682002-04-18 18:53:13 +0000479 // Reparse stringized version!
480 if (atof(StrVal.c_str()) == CFP->getValue()) {
Chris Lattner472cc102005-01-04 01:56:57 +0000481 Out << StrVal;
482 return;
Chris Lattner1e194682002-04-18 18:53:13 +0000483 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000484
Chris Lattner1e194682002-04-18 18:53:13 +0000485 // Otherwise we could not reparse it to exactly the same value, so we must
486 // output the string in hexadecimal format!
Chris Lattner472cc102005-01-04 01:56:57 +0000487 assert(sizeof(double) == sizeof(uint64_t) &&
Chris Lattner1e194682002-04-18 18:53:13 +0000488 "assuming that double is 64 bits!");
Jim Laskeyb74c6662005-08-17 19:34:49 +0000489 Out << "0x" << utohexstr(DoubleToBits(CFP->getValue()));
Chris Lattner1e194682002-04-18 18:53:13 +0000490
Chris Lattner76b2ff42004-02-15 05:55:15 +0000491 } else if (isa<ConstantAggregateZero>(CV)) {
492 Out << "zeroinitializer";
Chris Lattner1e194682002-04-18 18:53:13 +0000493 } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
494 // As a special case, print the array as a string if it is an array of
495 // ubytes or an array of sbytes with positive values.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000496 //
Chris Lattner1e194682002-04-18 18:53:13 +0000497 const Type *ETy = CA->getType()->getElementType();
Chris Lattner6ed87bd2006-01-23 23:03:36 +0000498 if (CA->isString()) {
Chris Lattner1e194682002-04-18 18:53:13 +0000499 Out << "c\"";
Chris Lattner6ed87bd2006-01-23 23:03:36 +0000500 PrintEscapedString(CA->getAsString(), Out);
Chris Lattner1e194682002-04-18 18:53:13 +0000501 Out << "\"";
502
503 } else { // Cannot output in string format...
Misha Brukman21bbdb92004-06-04 21:11:51 +0000504 Out << '[';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000505 if (CA->getNumOperands()) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000506 Out << ' ';
Chris Lattner1e194682002-04-18 18:53:13 +0000507 printTypeInt(Out, ETy, TypeTable);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000508 WriteAsOperandInternal(Out, CA->getOperand(0),
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000509 PrintName, TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000510 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
511 Out << ", ";
Chris Lattner1e194682002-04-18 18:53:13 +0000512 printTypeInt(Out, ETy, TypeTable);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000513 WriteAsOperandInternal(Out, CA->getOperand(i), PrintName,
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000514 TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000515 }
516 }
517 Out << " ]";
518 }
519 } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000520 Out << '{';
Jim Laskey3bb78742006-02-25 12:27:03 +0000521 unsigned N = CS->getNumOperands();
522 if (N) {
Jim Laskey6be3d8e2006-02-27 10:33:53 +0000523 if (N > 2) {
524 Indent += std::string(IndentSize, ' ');
525 Out << Indent;
526 } else {
527 Out << ' ';
528 }
Chris Lattnerd84bb632002-04-16 21:36:08 +0000529 printTypeInt(Out, CS->getOperand(0)->getType(), TypeTable);
530
531 WriteAsOperandInternal(Out, CS->getOperand(0),
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000532 PrintName, TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000533
Jim Laskey3bb78742006-02-25 12:27:03 +0000534 for (unsigned i = 1; i < N; i++) {
Chris Lattnerd84bb632002-04-16 21:36:08 +0000535 Out << ", ";
Jim Laskey6be3d8e2006-02-27 10:33:53 +0000536 if (N > 2) Out << Indent;
Chris Lattnerd84bb632002-04-16 21:36:08 +0000537 printTypeInt(Out, CS->getOperand(i)->getType(), TypeTable);
538
539 WriteAsOperandInternal(Out, CS->getOperand(i),
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000540 PrintName, TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000541 }
Jim Laskey6be3d8e2006-02-27 10:33:53 +0000542 if (N > 2) Indent.resize(Indent.size() - IndentSize);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000543 }
Jim Laskey3bb78742006-02-25 12:27:03 +0000544
Chris Lattnerd84bb632002-04-16 21:36:08 +0000545 Out << " }";
Brian Gaeke02209042004-08-20 06:00:58 +0000546 } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(CV)) {
547 const Type *ETy = CP->getType()->getElementType();
Misha Brukmanb1c93172005-04-21 23:48:37 +0000548 assert(CP->getNumOperands() > 0 &&
Brian Gaeke02209042004-08-20 06:00:58 +0000549 "Number of operands for a PackedConst must be > 0");
550 Out << '<';
551 Out << ' ';
552 printTypeInt(Out, ETy, TypeTable);
553 WriteAsOperandInternal(Out, CP->getOperand(0),
554 PrintName, TypeTable, Machine);
555 for (unsigned i = 1, e = CP->getNumOperands(); i != e; ++i) {
556 Out << ", ";
557 printTypeInt(Out, ETy, TypeTable);
558 WriteAsOperandInternal(Out, CP->getOperand(i), PrintName,
559 TypeTable, Machine);
560 }
561 Out << " >";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000562 } else if (isa<ConstantPointerNull>(CV)) {
563 Out << "null";
564
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000565 } else if (isa<UndefValue>(CV)) {
566 Out << "undef";
567
Chris Lattner3cd8c562002-07-30 18:54:25 +0000568 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Reid Spencer812a1be2006-12-04 05:19:18 +0000569 Out << CE->getOpcodeName();
570 if (CE->isCompare())
571 Out << " " << getPredicateText(CE->getPredicate());
572 Out << " (";
Misha Brukmanb1c93172005-04-21 23:48:37 +0000573
Vikram S. Adveb952b542002-07-14 23:14:45 +0000574 for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
575 printTypeInt(Out, (*OI)->getType(), TypeTable);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000576 WriteAsOperandInternal(Out, *OI, PrintName, TypeTable, Machine);
Vikram S. Adveb952b542002-07-14 23:14:45 +0000577 if (OI+1 != CE->op_end())
Chris Lattner3cd8c562002-07-30 18:54:25 +0000578 Out << ", ";
Vikram S. Adveb952b542002-07-14 23:14:45 +0000579 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000580
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000581 if (CE->isCast()) {
Chris Lattner83b396b2002-08-15 19:37:43 +0000582 Out << " to ";
583 printTypeInt(Out, CE->getType(), TypeTable);
584 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000585
Misha Brukman21bbdb92004-06-04 21:11:51 +0000586 Out << ')';
Chris Lattner83b396b2002-08-15 19:37:43 +0000587
Chris Lattnerd84bb632002-04-16 21:36:08 +0000588 } else {
Vikram S. Adveb952b542002-07-14 23:14:45 +0000589 Out << "<placeholder or erroneous Constant>";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000590 }
591}
592
593
Misha Brukmanc566ca362004-03-02 00:22:19 +0000594/// WriteAsOperand - Write the name of the specified value out to the specified
595/// ostream. This can be useful when you just want to print int %reg126, not
596/// the whole instruction that generated it.
597///
Misha Brukmanb1c93172005-04-21 23:48:37 +0000598static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000599 bool PrintName,
600 std::map<const Type*, std::string> &TypeTable,
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000601 SlotMachine *Machine) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000602 Out << ' ';
Reid Spenceraccd7c72004-07-17 23:47:01 +0000603 if ((PrintName || isa<GlobalValue>(V)) && V->hasName())
Chris Lattner1c343042003-08-22 05:40:38 +0000604 Out << getLLVMName(V->getName());
Reid Spenceraccd7c72004-07-17 23:47:01 +0000605 else {
606 const Constant *CV = dyn_cast<Constant>(V);
Chris Lattnera2d810d2006-01-25 22:26:05 +0000607 if (CV && !isa<GlobalValue>(CV)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000608 WriteConstantInt(Out, CV, PrintName, TypeTable, Machine);
Chris Lattnera2d810d2006-01-25 22:26:05 +0000609 } else if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
610 Out << "asm ";
611 if (IA->hasSideEffects())
612 Out << "sideeffect ";
613 Out << '"';
614 PrintEscapedString(IA->getAsmString(), Out);
615 Out << "\", \"";
616 PrintEscapedString(IA->getConstraintString(), Out);
617 Out << '"';
618 } else {
Chris Lattnerd84bb632002-04-16 21:36:08 +0000619 int Slot;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000620 if (Machine) {
Reid Spencer56010e42004-05-26 21:56:09 +0000621 Slot = Machine->getSlot(V);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000622 } else {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000623 Machine = createSlotMachine(V);
Chris Lattner96749c42006-05-14 18:46:52 +0000624 if (Machine)
Chris Lattner757ee0b2004-06-09 19:41:19 +0000625 Slot = Machine->getSlot(V);
626 else
627 Slot = -1;
Reid Spencer56010e42004-05-26 21:56:09 +0000628 delete Machine;
Chris Lattnerd84bb632002-04-16 21:36:08 +0000629 }
Chris Lattner757ee0b2004-06-09 19:41:19 +0000630 if (Slot != -1)
631 Out << '%' << Slot;
632 else
633 Out << "<badref>";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000634 }
635 }
636}
637
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000638/// WriteAsOperand - Write the name of the specified value out to the specified
639/// ostream. This can be useful when you just want to print int %reg126, not
640/// the whole instruction that generated it.
641///
Chris Lattner189d19f2003-11-21 20:23:48 +0000642std::ostream &llvm::WriteAsOperand(std::ostream &Out, const Value *V,
Misha Brukmanb1c93172005-04-21 23:48:37 +0000643 bool PrintType, bool PrintName,
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000644 const Module *Context) {
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000645 std::map<const Type *, std::string> TypeNames;
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000646 if (Context == 0) Context = getModuleFromVal(V);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000647
Chris Lattner98cf1f52002-11-20 18:36:02 +0000648 if (Context)
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000649 fillTypeNameTable(Context, TypeNames);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000650
651 if (PrintType)
652 printTypeInt(Out, V->getType(), TypeNames);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000653
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000654 WriteAsOperandInternal(Out, V, PrintName, TypeNames, 0);
Chris Lattner5e5abe32001-07-20 19:15:21 +0000655 return Out;
656}
657
Misha Brukmanb1c93172005-04-21 23:48:37 +0000658/// WriteAsOperandInternal - Write the name of the specified value out to
659/// the specified ostream. This can be useful when you just want to print
Reid Spencer58d30f22004-07-04 11:50:43 +0000660/// int %reg126, not the whole instruction that generated it.
661///
Misha Brukmanb1c93172005-04-21 23:48:37 +0000662static void WriteAsOperandInternal(std::ostream &Out, const Type *T,
Reid Spencer58d30f22004-07-04 11:50:43 +0000663 bool PrintName,
664 std::map<const Type*, std::string> &TypeTable,
665 SlotMachine *Machine) {
666 Out << ' ';
667 int Slot;
668 if (Machine) {
669 Slot = Machine->getSlot(T);
670 if (Slot != -1)
671 Out << '%' << Slot;
672 else
673 Out << "<badref>";
674 } else {
675 Out << T->getDescription();
676 }
677}
678
679/// WriteAsOperand - Write the name of the specified value out to the specified
680/// ostream. This can be useful when you just want to print int %reg126, not
681/// the whole instruction that generated it.
682///
683std::ostream &llvm::WriteAsOperand(std::ostream &Out, const Type *Ty,
Misha Brukmanb1c93172005-04-21 23:48:37 +0000684 bool PrintType, bool PrintName,
Reid Spencer58d30f22004-07-04 11:50:43 +0000685 const Module *Context) {
686 std::map<const Type *, std::string> TypeNames;
687 assert(Context != 0 && "Can't write types as operand without module context");
688
689 fillTypeNameTable(Context, TypeNames);
690
691 // if (PrintType)
692 // printTypeInt(Out, V->getType(), TypeNames);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000693
Reid Spencer58d30f22004-07-04 11:50:43 +0000694 printTypeInt(Out, Ty, TypeNames);
695
696 WriteAsOperandInternal(Out, Ty, PrintName, TypeNames, 0);
697 return Out;
698}
699
Chris Lattner189d19f2003-11-21 20:23:48 +0000700namespace llvm {
Chris Lattner2e9fee42001-07-12 23:35:26 +0000701
Chris Lattnerfee714f2001-09-07 16:36:04 +0000702class AssemblyWriter {
Misha Brukmana6619a92004-06-21 21:53:56 +0000703 std::ostream &Out;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000704 SlotMachine &Machine;
Chris Lattner7bfee412001-10-29 16:05:51 +0000705 const Module *TheModule;
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000706 std::map<const Type *, std::string> TypeNames;
Chris Lattner8339f7d2003-10-30 23:41:03 +0000707 AssemblyAnnotationWriter *AnnotationWriter;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000708public:
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000709 inline AssemblyWriter(std::ostream &o, SlotMachine &Mac, const Module *M,
Chris Lattner8339f7d2003-10-30 23:41:03 +0000710 AssemblyAnnotationWriter *AAW)
Misha Brukmana6619a92004-06-21 21:53:56 +0000711 : Out(o), Machine(Mac), TheModule(M), AnnotationWriter(AAW) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000712
713 // If the module has a symbol table, take all global types and stuff their
714 // names into the TypeNames map.
715 //
Chris Lattnerb86620e2001-10-29 16:37:48 +0000716 fillTypeNameTable(M, TypeNames);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000717 }
718
Chris Lattner7bfee412001-10-29 16:05:51 +0000719 inline void write(const Module *M) { printModule(M); }
720 inline void write(const GlobalVariable *G) { printGlobal(G); }
Chris Lattner57698e22002-03-26 18:01:55 +0000721 inline void write(const Function *F) { printFunction(F); }
Chris Lattner7bfee412001-10-29 16:05:51 +0000722 inline void write(const BasicBlock *BB) { printBasicBlock(BB); }
Chris Lattner113f4f42002-06-25 16:13:24 +0000723 inline void write(const Instruction *I) { printInstruction(*I); }
Chris Lattner3462ae32001-12-03 22:26:30 +0000724 inline void write(const Constant *CPV) { printConstant(CPV); }
Chris Lattner7db79582001-11-07 04:21:57 +0000725 inline void write(const Type *Ty) { printType(Ty); }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000726
Chris Lattner1e194682002-04-18 18:53:13 +0000727 void writeOperand(const Value *Op, bool PrintType, bool PrintName = true);
728
Misha Brukman4685e262004-04-28 15:31:21 +0000729 const Module* getModule() { return TheModule; }
730
Misha Brukmand92f54a2004-11-15 19:30:05 +0000731private:
Chris Lattner7bfee412001-10-29 16:05:51 +0000732 void printModule(const Module *M);
733 void printSymbolTable(const SymbolTable &ST);
Chris Lattner3462ae32001-12-03 22:26:30 +0000734 void printConstant(const Constant *CPV);
Chris Lattner7bfee412001-10-29 16:05:51 +0000735 void printGlobal(const GlobalVariable *GV);
Chris Lattner57698e22002-03-26 18:01:55 +0000736 void printFunction(const Function *F);
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000737 void printArgument(const Argument *FA);
Chris Lattner7bfee412001-10-29 16:05:51 +0000738 void printBasicBlock(const BasicBlock *BB);
Chris Lattner113f4f42002-06-25 16:13:24 +0000739 void printInstruction(const Instruction &I);
Chris Lattnerd816b532002-04-13 20:53:41 +0000740
741 // printType - Go to extreme measures to attempt to print out a short,
742 // symbolic version of a type name.
743 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000744 std::ostream &printType(const Type *Ty) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000745 return printTypeInt(Out, Ty, TypeNames);
Chris Lattnerd816b532002-04-13 20:53:41 +0000746 }
747
748 // printTypeAtLeastOneLevel - Print out one level of the possibly complex type
749 // without considering any symbolic types that we may have equal to it.
750 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000751 std::ostream &printTypeAtLeastOneLevel(const Type *Ty);
Chris Lattner7bfee412001-10-29 16:05:51 +0000752
Chris Lattner862e3382001-10-13 06:42:36 +0000753 // printInfoComment - Print a little comment after the instruction indicating
754 // which slot it occupies.
Chris Lattner113f4f42002-06-25 16:13:24 +0000755 void printInfoComment(const Value &V);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000756};
Reid Spencerf43ac622004-05-27 22:04:46 +0000757} // end of llvm namespace
Chris Lattner2f7c9632001-06-06 20:29:01 +0000758
Misha Brukmanc566ca362004-03-02 00:22:19 +0000759/// printTypeAtLeastOneLevel - Print out one level of the possibly complex type
760/// without considering any symbolic types that we may have equal to it.
761///
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000762std::ostream &AssemblyWriter::printTypeAtLeastOneLevel(const Type *Ty) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000763 if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000764 printType(FTy->getReturnType()) << " (";
Chris Lattnerfa829be2004-02-09 04:14:01 +0000765 for (FunctionType::param_iterator I = FTy->param_begin(),
766 E = FTy->param_end(); I != E; ++I) {
767 if (I != FTy->param_begin())
Misha Brukmana6619a92004-06-21 21:53:56 +0000768 Out << ", ";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000769 printType(*I);
Chris Lattnerd816b532002-04-13 20:53:41 +0000770 }
771 if (FTy->isVarArg()) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000772 if (FTy->getNumParams()) Out << ", ";
773 Out << "...";
Chris Lattnerd816b532002-04-13 20:53:41 +0000774 }
Misha Brukmana6619a92004-06-21 21:53:56 +0000775 Out << ')';
Chris Lattner113f4f42002-06-25 16:13:24 +0000776 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000777 Out << "{ ";
Chris Lattnerac6db752004-02-09 04:37:31 +0000778 for (StructType::element_iterator I = STy->element_begin(),
779 E = STy->element_end(); I != E; ++I) {
780 if (I != STy->element_begin())
Misha Brukmana6619a92004-06-21 21:53:56 +0000781 Out << ", ";
Chris Lattnerd816b532002-04-13 20:53:41 +0000782 printType(*I);
783 }
Misha Brukmana6619a92004-06-21 21:53:56 +0000784 Out << " }";
Chris Lattner113f4f42002-06-25 16:13:24 +0000785 } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000786 printType(PTy->getElementType()) << '*';
Chris Lattner113f4f42002-06-25 16:13:24 +0000787 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000788 Out << '[' << ATy->getNumElements() << " x ";
Misha Brukman21bbdb92004-06-04 21:11:51 +0000789 printType(ATy->getElementType()) << ']';
Reid Spencere203d352004-08-20 15:37:30 +0000790 } else if (const PackedType *PTy = dyn_cast<PackedType>(Ty)) {
791 Out << '<' << PTy->getNumElements() << " x ";
792 printType(PTy->getElementType()) << '>';
793 }
Reid Spencerde46e482006-11-02 20:25:50 +0000794 else if (isa<OpaqueType>(Ty)) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000795 Out << "opaque";
Chris Lattnerd816b532002-04-13 20:53:41 +0000796 } else {
Vikram S. Adveb952b542002-07-14 23:14:45 +0000797 if (!Ty->isPrimitiveType())
Misha Brukmana6619a92004-06-21 21:53:56 +0000798 Out << "<unknown derived type>";
Chris Lattnerd816b532002-04-13 20:53:41 +0000799 printType(Ty);
800 }
Misha Brukmana6619a92004-06-21 21:53:56 +0000801 return Out;
Chris Lattnerd816b532002-04-13 20:53:41 +0000802}
803
804
Misha Brukmanb1c93172005-04-21 23:48:37 +0000805void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType,
Reid Spencer56010e42004-05-26 21:56:09 +0000806 bool PrintName) {
Chris Lattner08f7d0c2005-02-24 16:58:29 +0000807 if (Operand != 0) {
808 if (PrintType) { Out << ' '; printType(Operand->getType()); }
809 WriteAsOperandInternal(Out, Operand, PrintName, TypeNames, &Machine);
810 } else {
811 Out << "<null operand!>";
812 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000813}
814
Chris Lattner2f7c9632001-06-06 20:29:01 +0000815
Chris Lattner7bfee412001-10-29 16:05:51 +0000816void AssemblyWriter::printModule(const Module *M) {
Chris Lattner4d8689e2005-03-02 23:12:40 +0000817 if (!M->getModuleIdentifier().empty() &&
Misha Brukmanb1c93172005-04-21 23:48:37 +0000818 // Don't print the ID if it will start a new line (which would
Chris Lattner4d8689e2005-03-02 23:12:40 +0000819 // require a comment char before it).
820 M->getModuleIdentifier().find('\n') == std::string::npos)
821 Out << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
822
Owen Andersone2237542006-10-18 02:21:12 +0000823 if (!M->getDataLayout().empty())
Chris Lattner04897162006-10-22 06:06:56 +0000824 Out << "target datalayout = \"" << M->getDataLayout() << "\"\n";
Owen Andersone2237542006-10-18 02:21:12 +0000825
Chris Lattner8068e0c2003-08-24 13:48:48 +0000826 switch (M->getEndianness()) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000827 case Module::LittleEndian: Out << "target endian = little\n"; break;
828 case Module::BigEndian: Out << "target endian = big\n"; break;
Chris Lattner8068e0c2003-08-24 13:48:48 +0000829 case Module::AnyEndianness: break;
830 }
831 switch (M->getPointerSize()) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000832 case Module::Pointer32: Out << "target pointersize = 32\n"; break;
833 case Module::Pointer64: Out << "target pointersize = 64\n"; break;
Chris Lattner8068e0c2003-08-24 13:48:48 +0000834 case Module::AnyPointerSize: break;
835 }
Reid Spencer48f98c82004-07-25 21:44:54 +0000836 if (!M->getTargetTriple().empty())
Reid Spencerffec7df2004-07-25 21:29:43 +0000837 Out << "target triple = \"" << M->getTargetTriple() << "\"\n";
Misha Brukmanb1c93172005-04-21 23:48:37 +0000838
Chris Lattnereef2fe72006-01-24 04:13:11 +0000839 if (!M->getModuleInlineAsm().empty()) {
Chris Lattnerefaf35d2006-01-24 00:45:30 +0000840 // Split the string into lines, to make it easier to read the .ll file.
Chris Lattnereef2fe72006-01-24 04:13:11 +0000841 std::string Asm = M->getModuleInlineAsm();
Chris Lattnerefaf35d2006-01-24 00:45:30 +0000842 size_t CurPos = 0;
843 size_t NewLine = Asm.find_first_of('\n', CurPos);
844 while (NewLine != std::string::npos) {
845 // We found a newline, print the portion of the asm string from the
846 // last newline up to this newline.
847 Out << "module asm \"";
848 PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.begin()+NewLine),
849 Out);
850 Out << "\"\n";
851 CurPos = NewLine+1;
852 NewLine = Asm.find_first_of('\n', CurPos);
853 }
Chris Lattner3acaf5c2006-01-24 00:40:17 +0000854 Out << "module asm \"";
Chris Lattnerefaf35d2006-01-24 00:45:30 +0000855 PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.end()), Out);
Chris Lattner6ed87bd2006-01-23 23:03:36 +0000856 Out << "\"\n";
857 }
858
Chris Lattner2cdd49d2004-09-14 05:06:58 +0000859 // Loop over the dependent libraries and emit them.
Chris Lattner730cfe42004-09-14 04:51:44 +0000860 Module::lib_iterator LI = M->lib_begin();
861 Module::lib_iterator LE = M->lib_end();
Reid Spencer48f98c82004-07-25 21:44:54 +0000862 if (LI != LE) {
Chris Lattner730cfe42004-09-14 04:51:44 +0000863 Out << "deplibs = [ ";
864 while (LI != LE) {
Chris Lattner2cdd49d2004-09-14 05:06:58 +0000865 Out << '"' << *LI << '"';
Reid Spencerffec7df2004-07-25 21:29:43 +0000866 ++LI;
Chris Lattner730cfe42004-09-14 04:51:44 +0000867 if (LI != LE)
868 Out << ", ";
Reid Spencerffec7df2004-07-25 21:29:43 +0000869 }
870 Out << " ]\n";
Reid Spencercc5ff642004-07-25 18:08:18 +0000871 }
Reid Spencerb9e08772004-09-13 23:44:23 +0000872
Chris Lattner2cdd49d2004-09-14 05:06:58 +0000873 // Loop over the symbol table, emitting all named constants.
Chris Lattner98cf1f52002-11-20 18:36:02 +0000874 printSymbolTable(M->getSymbolTable());
Misha Brukmanb1c93172005-04-21 23:48:37 +0000875
Chris Lattner54932b02006-12-06 04:41:52 +0000876 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
877 I != E; ++I)
Chris Lattner113f4f42002-06-25 16:13:24 +0000878 printGlobal(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000879
Misha Brukmana6619a92004-06-21 21:53:56 +0000880 Out << "\nimplementation ; Functions:\n";
Misha Brukmanb1c93172005-04-21 23:48:37 +0000881
Chris Lattner2cdd49d2004-09-14 05:06:58 +0000882 // Output all of the functions.
Chris Lattner113f4f42002-06-25 16:13:24 +0000883 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
884 printFunction(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000885}
886
Chris Lattner7bfee412001-10-29 16:05:51 +0000887void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000888 if (GV->hasName()) Out << getLLVMName(GV->getName()) << " = ";
Chris Lattner37798642001-09-18 04:01:05 +0000889
Misha Brukmanb1c93172005-04-21 23:48:37 +0000890 if (!GV->hasInitializer())
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000891 switch (GV->getLinkage()) {
892 case GlobalValue::DLLImportLinkage: Out << "dllimport "; break;
893 case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break;
894 default: Out << "external "; break;
895 }
Chris Lattner379a8d22003-04-16 20:28:45 +0000896 else
897 switch (GV->getLinkage()) {
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000898 case GlobalValue::InternalLinkage: Out << "internal "; break;
899 case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
900 case GlobalValue::WeakLinkage: Out << "weak "; break;
901 case GlobalValue::AppendingLinkage: Out << "appending "; break;
902 case GlobalValue::DLLImportLinkage: Out << "dllimport "; break;
903 case GlobalValue::DLLExportLinkage: Out << "dllexport "; break;
904 case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break;
905 case GlobalValue::ExternalLinkage: break;
Misha Brukman70457632004-11-14 21:04:34 +0000906 case GlobalValue::GhostLinkage:
Bill Wendlingdfc91892006-11-28 02:09:03 +0000907 llvm_cerr << "GhostLinkage not allowed in AsmWriter!\n";
Misha Brukman70457632004-11-14 21:04:34 +0000908 abort();
Chris Lattner379a8d22003-04-16 20:28:45 +0000909 }
Chris Lattner37798642001-09-18 04:01:05 +0000910
Misha Brukmana6619a92004-06-21 21:53:56 +0000911 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattner2413b162001-12-04 00:03:30 +0000912 printType(GV->getType()->getElementType());
Chris Lattner37798642001-09-18 04:01:05 +0000913
Reid Spenceraccd7c72004-07-17 23:47:01 +0000914 if (GV->hasInitializer()) {
915 Constant* C = cast<Constant>(GV->getInitializer());
916 assert(C && "GlobalVar initializer isn't constant?");
Reid Spencerc9c90cf2004-07-18 01:04:19 +0000917 writeOperand(GV->getInitializer(), false, isa<GlobalValue>(C));
Reid Spenceraccd7c72004-07-17 23:47:01 +0000918 }
Chris Lattnerf8a974d2005-11-06 06:48:53 +0000919
Chris Lattner4b96c542005-11-12 00:10:19 +0000920 if (GV->hasSection())
921 Out << ", section \"" << GV->getSection() << '"';
922 if (GV->getAlignment())
Chris Lattnerf8a974d2005-11-06 06:48:53 +0000923 Out << ", align " << GV->getAlignment();
Chris Lattner4b96c542005-11-12 00:10:19 +0000924
Chris Lattner113f4f42002-06-25 16:13:24 +0000925 printInfoComment(*GV);
Misha Brukmana6619a92004-06-21 21:53:56 +0000926 Out << "\n";
Chris Lattnerda975502001-09-10 07:58:01 +0000927}
928
Chris Lattnerfee714f2001-09-07 16:36:04 +0000929
Reid Spencere7e96712004-05-25 08:53:40 +0000930// printSymbolTable - Run through symbol table looking for constants
931// and types. Emit their declarations.
Chris Lattner7bfee412001-10-29 16:05:51 +0000932void AssemblyWriter::printSymbolTable(const SymbolTable &ST) {
Reid Spencere7e96712004-05-25 08:53:40 +0000933
934 // Print the types.
935 for (SymbolTable::type_const_iterator TI = ST.type_begin();
936 TI != ST.type_end(); ++TI ) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000937 Out << "\t" << getLLVMName(TI->first) << " = type ";
Reid Spencere7e96712004-05-25 08:53:40 +0000938
939 // Make sure we print out at least one level of the type structure, so
940 // that we do not get %FILE = type %FILE
941 //
942 printTypeAtLeastOneLevel(TI->second) << "\n";
943 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000944
Reid Spencere7e96712004-05-25 08:53:40 +0000945 // Print the constants, in type plane order.
946 for (SymbolTable::plane_const_iterator PI = ST.plane_begin();
947 PI != ST.plane_end(); ++PI ) {
948 SymbolTable::value_const_iterator VI = ST.value_begin(PI->first);
949 SymbolTable::value_const_iterator VE = ST.value_end(PI->first);
950
951 for (; VI != VE; ++VI) {
Reid Spenceraccd7c72004-07-17 23:47:01 +0000952 const Value* V = VI->second;
953 const Constant *CPV = dyn_cast<Constant>(V) ;
954 if (CPV && !isa<GlobalValue>(V)) {
Reid Spencer56010e42004-05-26 21:56:09 +0000955 printConstant(CPV);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000956 }
957 }
Chris Lattnera7620d92001-07-15 06:35:59 +0000958 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000959}
960
961
Misha Brukmanc566ca362004-03-02 00:22:19 +0000962/// printConstant - Print out a constant pool entry...
963///
Chris Lattner3462ae32001-12-03 22:26:30 +0000964void AssemblyWriter::printConstant(const Constant *CPV) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000965 // Don't print out unnamed constants, they will be inlined
966 if (!CPV->hasName()) return;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000967
Chris Lattneree998be2001-07-26 16:29:38 +0000968 // Print out name...
Misha Brukmana6619a92004-06-21 21:53:56 +0000969 Out << "\t" << getLLVMName(CPV->getName()) << " =";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000970
971 // Write the value out now...
Chris Lattnerd84bb632002-04-16 21:36:08 +0000972 writeOperand(CPV, true, false);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000973
Chris Lattner113f4f42002-06-25 16:13:24 +0000974 printInfoComment(*CPV);
Misha Brukmana6619a92004-06-21 21:53:56 +0000975 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000976}
977
Misha Brukmanc566ca362004-03-02 00:22:19 +0000978/// printFunction - Print all aspects of a function.
979///
Chris Lattner113f4f42002-06-25 16:13:24 +0000980void AssemblyWriter::printFunction(const Function *F) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000981 // Print out the return type and name...
Misha Brukmana6619a92004-06-21 21:53:56 +0000982 Out << "\n";
Chris Lattner379a8d22003-04-16 20:28:45 +0000983
Chris Lattner97e36f22004-12-05 06:44:09 +0000984 // Ensure that no local symbols conflict with global symbols.
985 const_cast<Function*>(F)->renameLocalSymbols();
986
Misha Brukmana6619a92004-06-21 21:53:56 +0000987 if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +0000988
Chris Lattner379a8d22003-04-16 20:28:45 +0000989 if (F->isExternal())
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000990 switch (F->getLinkage()) {
991 case GlobalValue::DLLImportLinkage: Out << "declare dllimport "; break;
992 case GlobalValue::ExternalWeakLinkage: Out << "declare extern_weak "; break;
993 default: Out << "declare ";
994 }
Chris Lattner379a8d22003-04-16 20:28:45 +0000995 else
996 switch (F->getLinkage()) {
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000997 case GlobalValue::InternalLinkage: Out << "internal "; break;
998 case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
999 case GlobalValue::WeakLinkage: Out << "weak "; break;
1000 case GlobalValue::AppendingLinkage: Out << "appending "; break;
1001 case GlobalValue::DLLImportLinkage: Out << "dllimport "; break;
1002 case GlobalValue::DLLExportLinkage: Out << "dllexport "; break;
1003 case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break;
Chris Lattner379a8d22003-04-16 20:28:45 +00001004 case GlobalValue::ExternalLinkage: break;
Misha Brukman70457632004-11-14 21:04:34 +00001005 case GlobalValue::GhostLinkage:
Bill Wendlingdfc91892006-11-28 02:09:03 +00001006 llvm_cerr << "GhostLinkage not allowed in AsmWriter!\n";
Misha Brukman70457632004-11-14 21:04:34 +00001007 abort();
Chris Lattner379a8d22003-04-16 20:28:45 +00001008 }
1009
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001010 // Print the calling convention.
1011 switch (F->getCallingConv()) {
1012 case CallingConv::C: break; // default
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +00001013 case CallingConv::CSRet: Out << "csretcc "; break;
1014 case CallingConv::Fast: Out << "fastcc "; break;
1015 case CallingConv::Cold: Out << "coldcc "; break;
1016 case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break;
1017 case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break;
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001018 default: Out << "cc" << F->getCallingConv() << " "; break;
1019 }
1020
Misha Brukman21bbdb92004-06-04 21:11:51 +00001021 printType(F->getReturnType()) << ' ';
Chris Lattner5b337482003-10-18 05:57:43 +00001022 if (!F->getName().empty())
Misha Brukmana6619a92004-06-21 21:53:56 +00001023 Out << getLLVMName(F->getName());
Chris Lattner5b337482003-10-18 05:57:43 +00001024 else
Misha Brukmana6619a92004-06-21 21:53:56 +00001025 Out << "\"\"";
1026 Out << '(';
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001027 Machine.incorporateFunction(F);
Chris Lattnerfee714f2001-09-07 16:36:04 +00001028
Chris Lattner7bfee412001-10-29 16:05:51 +00001029 // Loop over the arguments, printing them...
Chris Lattner113f4f42002-06-25 16:13:24 +00001030 const FunctionType *FT = F->getFunctionType();
Chris Lattnerfee714f2001-09-07 16:36:04 +00001031
Chris Lattner54932b02006-12-06 04:41:52 +00001032 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
1033 I != E; ++I)
Chris Lattner149376d2002-10-13 20:57:00 +00001034 printArgument(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +00001035
1036 // Finish printing arguments...
Chris Lattner113f4f42002-06-25 16:13:24 +00001037 if (FT->isVarArg()) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001038 if (FT->getNumParams()) Out << ", ";
1039 Out << "..."; // Output varargs portion of signature!
Chris Lattnerfee714f2001-09-07 16:36:04 +00001040 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001041 Out << ')';
Chris Lattnerfee714f2001-09-07 16:36:04 +00001042
Chris Lattner4b96c542005-11-12 00:10:19 +00001043 if (F->hasSection())
1044 Out << " section \"" << F->getSection() << '"';
Chris Lattnerf8a974d2005-11-06 06:48:53 +00001045 if (F->getAlignment())
1046 Out << " align " << F->getAlignment();
Chris Lattner4b96c542005-11-12 00:10:19 +00001047
Chris Lattner113f4f42002-06-25 16:13:24 +00001048 if (F->isExternal()) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001049 Out << "\n";
Chris Lattnerb2f02e52002-05-06 03:00:40 +00001050 } else {
Misha Brukmana6619a92004-06-21 21:53:56 +00001051 Out << " {";
Misha Brukmanb1c93172005-04-21 23:48:37 +00001052
Chris Lattner6915f8f2002-04-07 22:49:37 +00001053 // Output all of its basic blocks... for the function
Chris Lattner113f4f42002-06-25 16:13:24 +00001054 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
1055 printBasicBlock(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +00001056
Misha Brukmana6619a92004-06-21 21:53:56 +00001057 Out << "}\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +00001058 }
1059
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001060 Machine.purgeFunction();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001061}
1062
Misha Brukmanc566ca362004-03-02 00:22:19 +00001063/// printArgument - This member is called for every argument that is passed into
1064/// the function. Simply print it out
1065///
Chris Lattner2e9fa6d2002-04-09 19:48:49 +00001066void AssemblyWriter::printArgument(const Argument *Arg) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001067 // Insert commas as we go... the first arg doesn't get a comma
Chris Lattnerfb972ad2005-03-15 05:03:36 +00001068 if (Arg != Arg->getParent()->arg_begin()) Out << ", ";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001069
1070 // Output type...
Chris Lattner7bfee412001-10-29 16:05:51 +00001071 printType(Arg->getType());
Misha Brukmanb1c93172005-04-21 23:48:37 +00001072
Chris Lattner2f7c9632001-06-06 20:29:01 +00001073 // Output name, if available...
1074 if (Arg->hasName())
Misha Brukmana6619a92004-06-21 21:53:56 +00001075 Out << ' ' << getLLVMName(Arg->getName());
Chris Lattner2f7c9632001-06-06 20:29:01 +00001076}
1077
Misha Brukmanc566ca362004-03-02 00:22:19 +00001078/// printBasicBlock - This member is called for each basic block in a method.
1079///
Chris Lattner7bfee412001-10-29 16:05:51 +00001080void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001081 if (BB->hasName()) { // Print out the label if it exists...
Alkis Evlogimenos991d6ad2004-12-10 05:41:10 +00001082 Out << "\n" << getLLVMName(BB->getName(), false) << ':';
Chris Lattner408dbdb2002-05-14 16:02:05 +00001083 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Misha Brukmana6619a92004-06-21 21:53:56 +00001084 Out << "\n; <label>:";
Chris Lattner757ee0b2004-06-09 19:41:19 +00001085 int Slot = Machine.getSlot(BB);
1086 if (Slot != -1)
Misha Brukmana6619a92004-06-21 21:53:56 +00001087 Out << Slot;
Chris Lattner757ee0b2004-06-09 19:41:19 +00001088 else
Misha Brukmana6619a92004-06-21 21:53:56 +00001089 Out << "<badref>";
Chris Lattner58185f22002-10-02 19:38:55 +00001090 }
Chris Lattner2447ef52003-11-20 00:09:43 +00001091
1092 if (BB->getParent() == 0)
Misha Brukmana6619a92004-06-21 21:53:56 +00001093 Out << "\t\t; Error: Block without parent!";
Chris Lattner2447ef52003-11-20 00:09:43 +00001094 else {
1095 if (BB != &BB->getParent()->front()) { // Not the entry block?
1096 // Output predecessors for the block...
Misha Brukmana6619a92004-06-21 21:53:56 +00001097 Out << "\t\t;";
Chris Lattner2447ef52003-11-20 00:09:43 +00001098 pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
Misha Brukmanb1c93172005-04-21 23:48:37 +00001099
Chris Lattner2447ef52003-11-20 00:09:43 +00001100 if (PI == PE) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001101 Out << " No predecessors!";
Chris Lattner2447ef52003-11-20 00:09:43 +00001102 } else {
Misha Brukmana6619a92004-06-21 21:53:56 +00001103 Out << " preds =";
Chris Lattner00211f12003-11-16 22:59:57 +00001104 writeOperand(*PI, false, true);
Chris Lattner2447ef52003-11-20 00:09:43 +00001105 for (++PI; PI != PE; ++PI) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001106 Out << ',';
Chris Lattner2447ef52003-11-20 00:09:43 +00001107 writeOperand(*PI, false, true);
1108 }
Chris Lattner00211f12003-11-16 22:59:57 +00001109 }
Chris Lattner58185f22002-10-02 19:38:55 +00001110 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001111 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001112
Misha Brukmana6619a92004-06-21 21:53:56 +00001113 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001114
Misha Brukmana6619a92004-06-21 21:53:56 +00001115 if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001116
Chris Lattnerfee714f2001-09-07 16:36:04 +00001117 // Output all of the instructions in the basic block...
Chris Lattner113f4f42002-06-25 16:13:24 +00001118 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
1119 printInstruction(*I);
Chris Lattner96cdd272004-03-08 18:51:45 +00001120
Misha Brukmana6619a92004-06-21 21:53:56 +00001121 if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001122}
1123
Chris Lattner862e3382001-10-13 06:42:36 +00001124
Misha Brukmanc566ca362004-03-02 00:22:19 +00001125/// printInfoComment - Print a little comment after the instruction indicating
1126/// which slot it occupies.
1127///
Chris Lattner113f4f42002-06-25 16:13:24 +00001128void AssemblyWriter::printInfoComment(const Value &V) {
1129 if (V.getType() != Type::VoidTy) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001130 Out << "\t\t; <";
Misha Brukman21bbdb92004-06-04 21:11:51 +00001131 printType(V.getType()) << '>';
Chris Lattner862e3382001-10-13 06:42:36 +00001132
Chris Lattner113f4f42002-06-25 16:13:24 +00001133 if (!V.hasName()) {
Chris Lattner757ee0b2004-06-09 19:41:19 +00001134 int SlotNum = Machine.getSlot(&V);
1135 if (SlotNum == -1)
Misha Brukmana6619a92004-06-21 21:53:56 +00001136 Out << ":<badref>";
Reid Spencer8beac692004-06-09 15:26:53 +00001137 else
Misha Brukmana6619a92004-06-21 21:53:56 +00001138 Out << ':' << SlotNum; // Print out the def slot taken.
Chris Lattner862e3382001-10-13 06:42:36 +00001139 }
Chris Lattnerb6c21db2005-02-01 01:24:01 +00001140 Out << " [#uses=" << V.getNumUses() << ']'; // Output # uses
Chris Lattner862e3382001-10-13 06:42:36 +00001141 }
1142}
1143
Reid Spencere7141c82006-08-28 01:02:49 +00001144// This member is called for each Instruction in a function..
Chris Lattner113f4f42002-06-25 16:13:24 +00001145void AssemblyWriter::printInstruction(const Instruction &I) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001146 if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001147
Misha Brukmana6619a92004-06-21 21:53:56 +00001148 Out << "\t";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001149
1150 // Print out name if it exists...
Chris Lattner113f4f42002-06-25 16:13:24 +00001151 if (I.hasName())
Misha Brukmana6619a92004-06-21 21:53:56 +00001152 Out << getLLVMName(I.getName()) << " = ";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001153
Chris Lattner06038452005-05-06 05:51:46 +00001154 // If this is a volatile load or store, print out the volatile marker.
Chris Lattner504f9242003-09-08 17:45:59 +00001155 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
Chris Lattner06038452005-05-06 05:51:46 +00001156 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile())) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001157 Out << "volatile ";
Chris Lattner06038452005-05-06 05:51:46 +00001158 } else if (isa<CallInst>(I) && cast<CallInst>(I).isTailCall()) {
1159 // If this is a call, check if it's a tail call.
1160 Out << "tail ";
1161 }
Chris Lattner504f9242003-09-08 17:45:59 +00001162
Chris Lattner2f7c9632001-06-06 20:29:01 +00001163 // Print out the opcode...
Misha Brukmana6619a92004-06-21 21:53:56 +00001164 Out << I.getOpcodeName();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001165
Reid Spencer45e52392006-12-03 06:27:29 +00001166 // Print out the compare instruction predicates
1167 if (const FCmpInst *FCI = dyn_cast<FCmpInst>(&I)) {
Reid Spencer812a1be2006-12-04 05:19:18 +00001168 Out << " " << getPredicateText(FCI->getPredicate());
Reid Spencer45e52392006-12-03 06:27:29 +00001169 } else if (const ICmpInst *ICI = dyn_cast<ICmpInst>(&I)) {
Reid Spencer812a1be2006-12-04 05:19:18 +00001170 Out << " " << getPredicateText(ICI->getPredicate());
Reid Spencer45e52392006-12-03 06:27:29 +00001171 }
1172
Chris Lattner2f7c9632001-06-06 20:29:01 +00001173 // Print out the type of the operands...
Chris Lattner113f4f42002-06-25 16:13:24 +00001174 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001175
1176 // Special case conditional branches to swizzle the condition out to the front
Chris Lattner113f4f42002-06-25 16:13:24 +00001177 if (isa<BranchInst>(I) && I.getNumOperands() > 1) {
1178 writeOperand(I.getOperand(2), true);
Misha Brukmana6619a92004-06-21 21:53:56 +00001179 Out << ',';
Chris Lattner2f7c9632001-06-06 20:29:01 +00001180 writeOperand(Operand, true);
Misha Brukmana6619a92004-06-21 21:53:56 +00001181 Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001182 writeOperand(I.getOperand(1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001183
Chris Lattner8d48df22002-04-13 18:34:38 +00001184 } else if (isa<SwitchInst>(I)) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001185 // Special case switch statement to get formatting nice and correct...
Misha Brukmana6619a92004-06-21 21:53:56 +00001186 writeOperand(Operand , true); Out << ',';
1187 writeOperand(I.getOperand(1), true); Out << " [";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001188
Chris Lattner113f4f42002-06-25 16:13:24 +00001189 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001190 Out << "\n\t\t";
1191 writeOperand(I.getOperand(op ), true); Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001192 writeOperand(I.getOperand(op+1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001193 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001194 Out << "\n\t]";
Chris Lattnerda558102001-10-02 03:41:24 +00001195 } else if (isa<PHINode>(I)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001196 Out << ' ';
Chris Lattner113f4f42002-06-25 16:13:24 +00001197 printType(I.getType());
Misha Brukmana6619a92004-06-21 21:53:56 +00001198 Out << ' ';
Chris Lattner2f7c9632001-06-06 20:29:01 +00001199
Chris Lattner113f4f42002-06-25 16:13:24 +00001200 for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001201 if (op) Out << ", ";
Misha Brukmanb1c93172005-04-21 23:48:37 +00001202 Out << '[';
Misha Brukmana6619a92004-06-21 21:53:56 +00001203 writeOperand(I.getOperand(op ), false); Out << ',';
1204 writeOperand(I.getOperand(op+1), false); Out << " ]";
Chris Lattner931ef3b2001-06-11 15:04:20 +00001205 }
Chris Lattner862e3382001-10-13 06:42:36 +00001206 } else if (isa<ReturnInst>(I) && !Operand) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001207 Out << " void";
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001208 } else if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
1209 // Print the calling convention being used.
1210 switch (CI->getCallingConv()) {
1211 case CallingConv::C: break; // default
Chris Lattner29d20852006-05-19 21:58:52 +00001212 case CallingConv::CSRet: Out << " csretcc"; break;
1213 case CallingConv::Fast: Out << " fastcc"; break;
1214 case CallingConv::Cold: Out << " coldcc"; break;
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +00001215 case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break;
1216 case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break;
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001217 default: Out << " cc" << CI->getCallingConv(); break;
1218 }
1219
Chris Lattner463d6a52003-08-05 15:34:45 +00001220 const PointerType *PTy = cast<PointerType>(Operand->getType());
1221 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1222 const Type *RetTy = FTy->getReturnType();
Chris Lattner2f2d9472001-11-06 21:28:12 +00001223
Chris Lattner463d6a52003-08-05 15:34:45 +00001224 // If possible, print out the short form of the call instruction. We can
Chris Lattner6915f8f2002-04-07 22:49:37 +00001225 // only do this if the first argument is a pointer to a nonvararg function,
Chris Lattner463d6a52003-08-05 15:34:45 +00001226 // and if the return type is not a pointer to a function.
Chris Lattner2f2d9472001-11-06 21:28:12 +00001227 //
Chris Lattner463d6a52003-08-05 15:34:45 +00001228 if (!FTy->isVarArg() &&
Misha Brukmanb1c93172005-04-21 23:48:37 +00001229 (!isa<PointerType>(RetTy) ||
Chris Lattnerd9a36a62002-07-25 20:58:51 +00001230 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001231 Out << ' '; printType(RetTy);
Chris Lattner2f2d9472001-11-06 21:28:12 +00001232 writeOperand(Operand, false);
1233 } else {
1234 writeOperand(Operand, true);
1235 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001236 Out << '(';
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001237 if (CI->getNumOperands() > 1) writeOperand(CI->getOperand(1), true);
Chris Lattner113f4f42002-06-25 16:13:24 +00001238 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; ++op) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001239 Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001240 writeOperand(I.getOperand(op), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001241 }
1242
Misha Brukmana6619a92004-06-21 21:53:56 +00001243 Out << " )";
Chris Lattner113f4f42002-06-25 16:13:24 +00001244 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Chris Lattner463d6a52003-08-05 15:34:45 +00001245 const PointerType *PTy = cast<PointerType>(Operand->getType());
1246 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1247 const Type *RetTy = FTy->getReturnType();
1248
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001249 // Print the calling convention being used.
1250 switch (II->getCallingConv()) {
1251 case CallingConv::C: break; // default
Chris Lattner29d20852006-05-19 21:58:52 +00001252 case CallingConv::CSRet: Out << " csretcc"; break;
1253 case CallingConv::Fast: Out << " fastcc"; break;
1254 case CallingConv::Cold: Out << " coldcc"; break;
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +00001255 case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break;
1256 case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break;
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001257 default: Out << " cc" << II->getCallingConv(); break;
1258 }
1259
Chris Lattner463d6a52003-08-05 15:34:45 +00001260 // If possible, print out the short form of the invoke instruction. We can
1261 // only do this if the first argument is a pointer to a nonvararg function,
1262 // and if the return type is not a pointer to a function.
1263 //
1264 if (!FTy->isVarArg() &&
Misha Brukmanb1c93172005-04-21 23:48:37 +00001265 (!isa<PointerType>(RetTy) ||
Chris Lattner463d6a52003-08-05 15:34:45 +00001266 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001267 Out << ' '; printType(RetTy);
Chris Lattner463d6a52003-08-05 15:34:45 +00001268 writeOperand(Operand, false);
1269 } else {
1270 writeOperand(Operand, true);
1271 }
1272
Misha Brukmana6619a92004-06-21 21:53:56 +00001273 Out << '(';
Chris Lattner113f4f42002-06-25 16:13:24 +00001274 if (I.getNumOperands() > 3) writeOperand(I.getOperand(3), true);
1275 for (unsigned op = 4, Eop = I.getNumOperands(); op < Eop; ++op) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001276 Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001277 writeOperand(I.getOperand(op), true);
Chris Lattner862e3382001-10-13 06:42:36 +00001278 }
1279
Misha Brukmana6619a92004-06-21 21:53:56 +00001280 Out << " )\n\t\t\tto";
Chris Lattner862e3382001-10-13 06:42:36 +00001281 writeOperand(II->getNormalDest(), true);
Misha Brukmana6619a92004-06-21 21:53:56 +00001282 Out << " unwind";
Chris Lattnerfae8ab32004-02-08 21:44:31 +00001283 writeOperand(II->getUnwindDest(), true);
Chris Lattner862e3382001-10-13 06:42:36 +00001284
Chris Lattner113f4f42002-06-25 16:13:24 +00001285 } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(&I)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001286 Out << ' ';
Chris Lattner8d48df22002-04-13 18:34:38 +00001287 printType(AI->getType()->getElementType());
1288 if (AI->isArrayAllocation()) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001289 Out << ',';
Chris Lattner8d48df22002-04-13 18:34:38 +00001290 writeOperand(AI->getArraySize(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001291 }
Nate Begeman848622f2005-11-05 09:21:28 +00001292 if (AI->getAlignment()) {
Chris Lattner7aeee3a2005-11-05 21:20:34 +00001293 Out << ", align " << AI->getAlignment();
Nate Begeman848622f2005-11-05 09:21:28 +00001294 }
Chris Lattner862e3382001-10-13 06:42:36 +00001295 } else if (isa<CastInst>(I)) {
Chris Lattnerc96e96b2003-11-17 01:17:04 +00001296 if (Operand) writeOperand(Operand, true); // Work with broken code
Misha Brukmana6619a92004-06-21 21:53:56 +00001297 Out << " to ";
Chris Lattner113f4f42002-06-25 16:13:24 +00001298 printType(I.getType());
Chris Lattner5b337482003-10-18 05:57:43 +00001299 } else if (isa<VAArgInst>(I)) {
Chris Lattnerc96e96b2003-11-17 01:17:04 +00001300 if (Operand) writeOperand(Operand, true); // Work with broken code
Misha Brukmana6619a92004-06-21 21:53:56 +00001301 Out << ", ";
Chris Lattnerf70da102003-05-08 02:44:12 +00001302 printType(I.getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +00001303 } else if (Operand) { // Print the normal way...
1304
Misha Brukmanb1c93172005-04-21 23:48:37 +00001305 // PrintAllTypes - Instructions who have operands of all the same type
Chris Lattner2f7c9632001-06-06 20:29:01 +00001306 // omit the type from all but the first operand. If the instruction has
1307 // different type operands (for example br), then they are all printed.
1308 bool PrintAllTypes = false;
1309 const Type *TheType = Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001310
Chris Lattner52bd5cb92004-03-12 05:53:14 +00001311 // Shift Left & Right print both types even for Ubyte LHS, and select prints
1312 // types even if all operands are bools.
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001313 if (isa<ShiftInst>(I) || isa<SelectInst>(I) || isa<StoreInst>(I) ||
1314 isa<ShuffleVectorInst>(I)) {
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00001315 PrintAllTypes = true;
1316 } else {
1317 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
1318 Operand = I.getOperand(i);
1319 if (Operand->getType() != TheType) {
1320 PrintAllTypes = true; // We have differing types! Print them all!
1321 break;
1322 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001323 }
1324 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001325
Chris Lattner7bfee412001-10-29 16:05:51 +00001326 if (!PrintAllTypes) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001327 Out << ' ';
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00001328 printType(TheType);
Chris Lattner7bfee412001-10-29 16:05:51 +00001329 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001330
Chris Lattner113f4f42002-06-25 16:13:24 +00001331 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001332 if (i) Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001333 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001334 }
1335 }
1336
Chris Lattner862e3382001-10-13 06:42:36 +00001337 printInfoComment(I);
Misha Brukmana6619a92004-06-21 21:53:56 +00001338 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001339}
1340
1341
1342//===----------------------------------------------------------------------===//
1343// External Interface declarations
1344//===----------------------------------------------------------------------===//
1345
Chris Lattner8339f7d2003-10-30 23:41:03 +00001346void Module::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001347 SlotMachine SlotTable(this);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001348 AssemblyWriter W(o, SlotTable, this, AAW);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001349 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001350}
1351
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001352void GlobalVariable::print(std::ostream &o) const {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001353 SlotMachine SlotTable(getParent());
Chris Lattner8339f7d2003-10-30 23:41:03 +00001354 AssemblyWriter W(o, SlotTable, getParent(), 0);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001355 W.write(this);
Chris Lattneradfe0d12001-09-10 20:08:19 +00001356}
1357
Chris Lattner8339f7d2003-10-30 23:41:03 +00001358void Function::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001359 SlotMachine SlotTable(getParent());
Chris Lattner8339f7d2003-10-30 23:41:03 +00001360 AssemblyWriter W(o, SlotTable, getParent(), AAW);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001361
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001362 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001363}
1364
Chris Lattnereef2fe72006-01-24 04:13:11 +00001365void InlineAsm::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Chris Lattnera2d810d2006-01-25 22:26:05 +00001366 WriteAsOperand(o, this, true, true, 0);
Chris Lattnereef2fe72006-01-24 04:13:11 +00001367}
1368
Chris Lattner8339f7d2003-10-30 23:41:03 +00001369void BasicBlock::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001370 SlotMachine SlotTable(getParent());
Misha Brukmanb1c93172005-04-21 23:48:37 +00001371 AssemblyWriter W(o, SlotTable,
Chris Lattner8339f7d2003-10-30 23:41:03 +00001372 getParent() ? getParent()->getParent() : 0, AAW);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001373 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001374}
1375
Chris Lattner8339f7d2003-10-30 23:41:03 +00001376void Instruction::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001377 const Function *F = getParent() ? getParent()->getParent() : 0;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001378 SlotMachine SlotTable(F);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001379 AssemblyWriter W(o, SlotTable, F ? F->getParent() : 0, AAW);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001380
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001381 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001382}
Chris Lattner7db79582001-11-07 04:21:57 +00001383
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001384void Constant::print(std::ostream &o) const {
1385 if (this == 0) { o << "<null> constant value\n"; return; }
Chris Lattner7d734802002-09-10 15:53:49 +00001386
Misha Brukman21bbdb92004-06-04 21:11:51 +00001387 o << ' ' << getType()->getDescription() << ' ';
Evan Cheng5b19a802006-03-01 22:17:00 +00001388
1389 std::map<const Type *, std::string> TypeTable;
1390 WriteConstantInt(o, this, false, TypeTable, 0);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001391}
1392
Misha Brukmanb1c93172005-04-21 23:48:37 +00001393void Type::print(std::ostream &o) const {
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001394 if (this == 0)
1395 o << "<null Type>";
1396 else
1397 o << getDescription();
1398}
1399
Chris Lattner2e9fa6d2002-04-09 19:48:49 +00001400void Argument::print(std::ostream &o) const {
Chris Lattnere52ed452004-07-13 23:14:34 +00001401 WriteAsOperand(o, this, true, true,
1402 getParent() ? getParent()->getParent() : 0);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001403}
1404
Reid Spencer52641832004-05-25 18:14:38 +00001405// Value::dump - allow easy printing of Values from the debugger.
1406// Located here because so much of the needed functionality is here.
Bill Wendlingdfc91892006-11-28 02:09:03 +00001407void Value::dump() const { print(std::cerr); llvm_cerr << '\n'; }
Reid Spencer52641832004-05-25 18:14:38 +00001408
1409// Type::dump - allow easy printing of Values from the debugger.
1410// Located here because so much of the needed functionality is here.
Bill Wendlingdfc91892006-11-28 02:09:03 +00001411void Type::dump() const { print(std::cerr); llvm_cerr << '\n'; }
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001412
1413//===----------------------------------------------------------------------===//
1414// CachedWriter Class Implementation
1415//===----------------------------------------------------------------------===//
1416
Chris Lattner7db79582001-11-07 04:21:57 +00001417void CachedWriter::setModule(const Module *M) {
1418 delete SC; delete AW;
1419 if (M) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001420 SC = new SlotMachine(M );
Misha Brukman21bbdb92004-06-04 21:11:51 +00001421 AW = new AssemblyWriter(Out, *SC, M, 0);
Chris Lattner7db79582001-11-07 04:21:57 +00001422 } else {
1423 SC = 0; AW = 0;
1424 }
1425}
1426
1427CachedWriter::~CachedWriter() {
1428 delete AW;
1429 delete SC;
1430}
1431
Chris Lattner60a7dd12004-07-15 02:51:31 +00001432CachedWriter &CachedWriter::operator<<(const Value &V) {
Chris Lattner7db79582001-11-07 04:21:57 +00001433 assert(AW && SC && "CachedWriter does not have a current module!");
Chris Lattner60a7dd12004-07-15 02:51:31 +00001434 if (const Instruction *I = dyn_cast<Instruction>(&V))
Chris Lattner80d2d532004-06-26 19:40:40 +00001435 AW->write(I);
Chris Lattner60a7dd12004-07-15 02:51:31 +00001436 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(&V))
Chris Lattner80d2d532004-06-26 19:40:40 +00001437 AW->write(BB);
Chris Lattner60a7dd12004-07-15 02:51:31 +00001438 else if (const Function *F = dyn_cast<Function>(&V))
Chris Lattner80d2d532004-06-26 19:40:40 +00001439 AW->write(F);
Chris Lattner60a7dd12004-07-15 02:51:31 +00001440 else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(&V))
Chris Lattner80d2d532004-06-26 19:40:40 +00001441 AW->write(GV);
Misha Brukmanb1c93172005-04-21 23:48:37 +00001442 else
Chris Lattner60a7dd12004-07-15 02:51:31 +00001443 AW->writeOperand(&V, true, true);
Chris Lattner7db79582001-11-07 04:21:57 +00001444 return *this;
1445}
Misha Brukman4685e262004-04-28 15:31:21 +00001446
Chris Lattner60a7dd12004-07-15 02:51:31 +00001447CachedWriter& CachedWriter::operator<<(const Type &Ty) {
Misha Brukman4685e262004-04-28 15:31:21 +00001448 if (SymbolicTypes) {
1449 const Module *M = AW->getModule();
Chris Lattner60a7dd12004-07-15 02:51:31 +00001450 if (M) WriteTypeSymbolic(Out, &Ty, M);
Reid Spencer58d30f22004-07-04 11:50:43 +00001451 } else {
Chris Lattner60a7dd12004-07-15 02:51:31 +00001452 AW->write(&Ty);
Reid Spencer58d30f22004-07-04 11:50:43 +00001453 }
1454 return *this;
Misha Brukman4685e262004-04-28 15:31:21 +00001455}
Misha Brukmanda546ea2004-04-28 19:24:28 +00001456
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001457//===----------------------------------------------------------------------===//
1458//===-- SlotMachine Implementation
1459//===----------------------------------------------------------------------===//
1460
1461#if 0
Bill Wendlingdfc91892006-11-28 02:09:03 +00001462#define SC_DEBUG(X) llvm_cerr << X
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001463#else
1464#define SC_DEBUG(X)
1465#endif
1466
1467// Module level constructor. Causes the contents of the Module (sans functions)
1468// to be added to the slot table.
Misha Brukmanb1c93172005-04-21 23:48:37 +00001469SlotMachine::SlotMachine(const Module *M)
Reid Spencer56010e42004-05-26 21:56:09 +00001470 : TheModule(M) ///< Saved for lazy initialization.
1471 , TheFunction(0)
Reid Spencerb0ac8c42004-08-16 07:46:33 +00001472 , FunctionProcessed(false)
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001473 , mMap()
Reid Spencer58d30f22004-07-04 11:50:43 +00001474 , mTypes()
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001475 , fMap()
Reid Spencer58d30f22004-07-04 11:50:43 +00001476 , fTypes()
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001477{
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001478}
1479
1480// Function level constructor. Causes the contents of the Module and the one
1481// function provided to be added to the slot table.
Misha Brukmanb1c93172005-04-21 23:48:37 +00001482SlotMachine::SlotMachine(const Function *F )
Reid Spencer56010e42004-05-26 21:56:09 +00001483 : TheModule( F ? F->getParent() : 0 ) ///< Saved for lazy initialization
1484 , TheFunction(F) ///< Saved for lazy initialization
Reid Spencerb0ac8c42004-08-16 07:46:33 +00001485 , FunctionProcessed(false)
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001486 , mMap()
Reid Spencer58d30f22004-07-04 11:50:43 +00001487 , mTypes()
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001488 , fMap()
Reid Spencer58d30f22004-07-04 11:50:43 +00001489 , fTypes()
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001490{
Reid Spencer56010e42004-05-26 21:56:09 +00001491}
1492
1493inline void SlotMachine::initialize(void) {
Misha Brukmanb1c93172005-04-21 23:48:37 +00001494 if ( TheModule) {
1495 processModule();
Reid Spencer56010e42004-05-26 21:56:09 +00001496 TheModule = 0; ///< Prevent re-processing next time we're called.
1497 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001498 if ( TheFunction && ! FunctionProcessed) {
1499 processFunction();
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001500 }
1501}
1502
1503// Iterate through all the global variables, functions, and global
Misha Brukmanb1c93172005-04-21 23:48:37 +00001504// variable initializers and create slots for them.
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001505void SlotMachine::processModule() {
1506 SC_DEBUG("begin processModule!\n");
1507
1508 // Add all of the global variables to the value table...
Chris Lattner54932b02006-12-06 04:41:52 +00001509 for (Module::const_global_iterator I = TheModule->global_begin(),
1510 E = TheModule->global_end(); I != E; ++I)
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001511 createSlot(I);
1512
1513 // Add all the functions to the table
1514 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
1515 I != E; ++I)
1516 createSlot(I);
1517
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001518 SC_DEBUG("end processModule!\n");
1519}
1520
1521
Reid Spencer56010e42004-05-26 21:56:09 +00001522// Process the arguments, basic blocks, and instructions of a function.
1523void SlotMachine::processFunction() {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001524 SC_DEBUG("begin processFunction!\n");
1525
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001526 // Add all the function arguments
Misha Brukmanb1c93172005-04-21 23:48:37 +00001527 for(Function::const_arg_iterator AI = TheFunction->arg_begin(),
Chris Lattner531f9e92005-03-15 04:54:21 +00001528 AE = TheFunction->arg_end(); AI != AE; ++AI)
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001529 createSlot(AI);
1530
1531 SC_DEBUG("Inserting Instructions:\n");
1532
1533 // Add all of the basic blocks and instructions
Misha Brukmanb1c93172005-04-21 23:48:37 +00001534 for (Function::const_iterator BB = TheFunction->begin(),
Reid Spencer56010e42004-05-26 21:56:09 +00001535 E = TheFunction->end(); BB != E; ++BB) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001536 createSlot(BB);
1537 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
1538 createSlot(I);
1539 }
1540 }
1541
Reid Spencerb0ac8c42004-08-16 07:46:33 +00001542 FunctionProcessed = true;
1543
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001544 SC_DEBUG("end processFunction!\n");
1545}
1546
1547// Clean up after incorporating a function. This is the only way
Reid Spencer56010e42004-05-26 21:56:09 +00001548// to get out of the function incorporation state that affects the
1549// getSlot/createSlot lock. Function incorporation state is indicated
1550// by TheFunction != 0.
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001551void SlotMachine::purgeFunction() {
1552 SC_DEBUG("begin purgeFunction!\n");
1553 fMap.clear(); // Simply discard the function level map
Reid Spencer58d30f22004-07-04 11:50:43 +00001554 fTypes.clear();
Reid Spencer56010e42004-05-26 21:56:09 +00001555 TheFunction = 0;
Reid Spencerb0ac8c42004-08-16 07:46:33 +00001556 FunctionProcessed = false;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001557 SC_DEBUG("end purgeFunction!\n");
1558}
1559
1560/// Get the slot number for a value. This function will assert if you
1561/// ask for a Value that hasn't previously been inserted with createSlot.
1562/// Types are forbidden because Type does not inherit from Value (any more).
Chris Lattner757ee0b2004-06-09 19:41:19 +00001563int SlotMachine::getSlot(const Value *V) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001564 assert( V && "Can't get slot for null Value" );
Misha Brukmanb1c93172005-04-21 23:48:37 +00001565 assert(!isa<Constant>(V) || isa<GlobalValue>(V) &&
1566 "Can't insert a non-GlobalValue Constant into SlotMachine");
Reid Spencer56010e42004-05-26 21:56:09 +00001567
1568 // Check for uninitialized state and do lazy initialization
1569 this->initialize();
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001570
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001571 // Get the type of the value
1572 const Type* VTy = V->getType();
1573
1574 // Find the type plane in the module map
1575 TypedPlanes::const_iterator MI = mMap.find(VTy);
1576
Reid Spencer56010e42004-05-26 21:56:09 +00001577 if ( TheFunction ) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001578 // Lookup the type in the function map too
1579 TypedPlanes::const_iterator FI = fMap.find(VTy);
1580 // If there is a corresponding type plane in the function map
1581 if ( FI != fMap.end() ) {
1582 // Lookup the Value in the function map
1583 ValueMap::const_iterator FVI = FI->second.map.find(V);
1584 // If the value doesn't exist in the function map
1585 if ( FVI == FI->second.map.end() ) {
Chris Lattner68a038e2004-06-09 22:22:10 +00001586 // Look up the value in the module map.
1587 if (MI == mMap.end()) return -1;
Reid Spencer56010e42004-05-26 21:56:09 +00001588 ValueMap::const_iterator MVI = MI->second.map.find(V);
1589 // If we didn't find it, it wasn't inserted
Chris Lattner757ee0b2004-06-09 19:41:19 +00001590 if (MVI == MI->second.map.end()) return -1;
Reid Spencer56010e42004-05-26 21:56:09 +00001591 assert( MVI != MI->second.map.end() && "Value not found");
1592 // We found it only at the module level
Misha Brukmanb1c93172005-04-21 23:48:37 +00001593 return MVI->second;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001594
1595 // else the value exists in the function map
1596 } else {
Reid Spencer56010e42004-05-26 21:56:09 +00001597 // Return the slot number as the module's contribution to
1598 // the type plane plus the index in the function's contribution
1599 // to the type plane.
Chris Lattnerb1f04782004-06-15 21:07:32 +00001600 if (MI != mMap.end())
1601 return MI->second.next_slot + FVI->second;
1602 else
1603 return FVI->second;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001604 }
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001605 }
1606 }
1607
Reid Spencer8beac692004-06-09 15:26:53 +00001608 // N.B. Can get here only if either !TheFunction or the function doesn't
1609 // have a corresponding type plane for the Value
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001610
1611 // Make sure the type plane exists
Chris Lattner757ee0b2004-06-09 19:41:19 +00001612 if (MI == mMap.end()) return -1;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001613 // Lookup the value in the module's map
1614 ValueMap::const_iterator MVI = MI->second.map.find(V);
1615 // Make sure we found it.
Chris Lattner757ee0b2004-06-09 19:41:19 +00001616 if (MVI == MI->second.map.end()) return -1;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001617 // Return it.
1618 return MVI->second;
1619}
1620
Reid Spencer58d30f22004-07-04 11:50:43 +00001621/// Get the slot number for a value. This function will assert if you
1622/// ask for a Value that hasn't previously been inserted with createSlot.
1623/// Types are forbidden because Type does not inherit from Value (any more).
1624int SlotMachine::getSlot(const Type *Ty) {
1625 assert( Ty && "Can't get slot for null Type" );
1626
1627 // Check for uninitialized state and do lazy initialization
1628 this->initialize();
1629
1630 if ( TheFunction ) {
1631 // Lookup the Type in the function map
1632 TypeMap::const_iterator FTI = fTypes.map.find(Ty);
1633 // If the Type doesn't exist in the function map
1634 if ( FTI == fTypes.map.end() ) {
1635 TypeMap::const_iterator MTI = mTypes.map.find(Ty);
1636 // If we didn't find it, it wasn't inserted
Misha Brukmanb1c93172005-04-21 23:48:37 +00001637 if (MTI == mTypes.map.end())
Reid Spencer58d30f22004-07-04 11:50:43 +00001638 return -1;
1639 // We found it only at the module level
Misha Brukmanb1c93172005-04-21 23:48:37 +00001640 return MTI->second;
Reid Spencer58d30f22004-07-04 11:50:43 +00001641
1642 // else the value exists in the function map
1643 } else {
1644 // Return the slot number as the module's contribution to
1645 // the type plane plus the index in the function's contribution
1646 // to the type plane.
1647 return mTypes.next_slot + FTI->second;
1648 }
1649 }
1650
1651 // N.B. Can get here only if either !TheFunction
1652
1653 // Lookup the value in the module's map
1654 TypeMap::const_iterator MTI = mTypes.map.find(Ty);
1655 // Make sure we found it.
1656 if (MTI == mTypes.map.end()) return -1;
1657 // Return it.
1658 return MTI->second;
1659}
1660
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001661// Create a new slot, or return the existing slot if it is already
1662// inserted. Note that the logic here parallels getSlot but instead
1663// of asserting when the Value* isn't found, it inserts the value.
1664unsigned SlotMachine::createSlot(const Value *V) {
1665 assert( V && "Can't insert a null Value to SlotMachine");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001666 assert(!isa<Constant>(V) || isa<GlobalValue>(V) &&
1667 "Can't insert a non-GlobalValue Constant into SlotMachine");
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001668
1669 const Type* VTy = V->getType();
1670
1671 // Just ignore void typed things
1672 if (VTy == Type::VoidTy) return 0; // FIXME: Wrong return value!
1673
1674 // Look up the type plane for the Value's type from the module map
1675 TypedPlanes::const_iterator MI = mMap.find(VTy);
1676
Reid Spencer56010e42004-05-26 21:56:09 +00001677 if ( TheFunction ) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001678 // Get the type plane for the Value's type from the function map
1679 TypedPlanes::const_iterator FI = fMap.find(VTy);
1680 // If there is a corresponding type plane in the function map
1681 if ( FI != fMap.end() ) {
1682 // Lookup the Value in the function map
1683 ValueMap::const_iterator FVI = FI->second.map.find(V);
1684 // If the value doesn't exist in the function map
1685 if ( FVI == FI->second.map.end() ) {
Reid Spencer56010e42004-05-26 21:56:09 +00001686 // If there is no corresponding type plane in the module map
1687 if ( MI == mMap.end() )
1688 return insertValue(V);
1689 // Look up the value in the module map
1690 ValueMap::const_iterator MVI = MI->second.map.find(V);
1691 // If we didn't find it, it wasn't inserted
1692 if ( MVI == MI->second.map.end() )
1693 return insertValue(V);
1694 else
1695 // We found it only at the module level
1696 return MVI->second;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001697
1698 // else the value exists in the function map
1699 } else {
Reid Spencer56010e42004-05-26 21:56:09 +00001700 if ( MI == mMap.end() )
1701 return FVI->second;
1702 else
1703 // Return the slot number as the module's contribution to
1704 // the type plane plus the index in the function's contribution
1705 // to the type plane.
1706 return MI->second.next_slot + FVI->second;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001707 }
1708
1709 // else there is not a corresponding type plane in the function map
1710 } else {
1711 // If the type plane doesn't exists at the module level
1712 if ( MI == mMap.end() ) {
Reid Spencer56010e42004-05-26 21:56:09 +00001713 return insertValue(V);
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001714 // else type plane exists at the module level, examine it
1715 } else {
Reid Spencer56010e42004-05-26 21:56:09 +00001716 // Look up the value in the module's map
1717 ValueMap::const_iterator MVI = MI->second.map.find(V);
1718 // If we didn't find it there either
1719 if ( MVI == MI->second.map.end() )
1720 // Return the slot number as the module's contribution to
1721 // the type plane plus the index of the function map insertion.
1722 return MI->second.next_slot + insertValue(V);
1723 else
1724 return MVI->second;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001725 }
1726 }
1727 }
1728
Reid Spencer56010e42004-05-26 21:56:09 +00001729 // N.B. Can only get here if !TheFunction
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001730
1731 // If the module map's type plane is not for the Value's type
1732 if ( MI != mMap.end() ) {
1733 // Lookup the value in the module's map
1734 ValueMap::const_iterator MVI = MI->second.map.find(V);
Misha Brukmanb1c93172005-04-21 23:48:37 +00001735 if ( MVI != MI->second.map.end() )
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001736 return MVI->second;
1737 }
1738
1739 return insertValue(V);
1740}
1741
Reid Spencer58d30f22004-07-04 11:50:43 +00001742// Create a new slot, or return the existing slot if it is already
1743// inserted. Note that the logic here parallels getSlot but instead
1744// of asserting when the Value* isn't found, it inserts the value.
1745unsigned SlotMachine::createSlot(const Type *Ty) {
1746 assert( Ty && "Can't insert a null Type to SlotMachine");
1747
1748 if ( TheFunction ) {
1749 // Lookup the Type in the function map
1750 TypeMap::const_iterator FTI = fTypes.map.find(Ty);
1751 // If the type doesn't exist in the function map
1752 if ( FTI == fTypes.map.end() ) {
1753 // Look up the type in the module map
1754 TypeMap::const_iterator MTI = mTypes.map.find(Ty);
1755 // If we didn't find it, it wasn't inserted
1756 if ( MTI == mTypes.map.end() )
1757 return insertValue(Ty);
1758 else
1759 // We found it only at the module level
1760 return MTI->second;
1761
1762 // else the value exists in the function map
1763 } else {
1764 // Return the slot number as the module's contribution to
1765 // the type plane plus the index in the function's contribution
1766 // to the type plane.
1767 return mTypes.next_slot + FTI->second;
1768 }
1769 }
1770
1771 // N.B. Can only get here if !TheFunction
1772
1773 // Lookup the type in the module's map
1774 TypeMap::const_iterator MTI = mTypes.map.find(Ty);
Misha Brukmanb1c93172005-04-21 23:48:37 +00001775 if ( MTI != mTypes.map.end() )
Reid Spencer58d30f22004-07-04 11:50:43 +00001776 return MTI->second;
1777
1778 return insertValue(Ty);
1779}
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001780
1781// Low level insert function. Minimal checking is done. This
1782// function is just for the convenience of createSlot (above).
1783unsigned SlotMachine::insertValue(const Value *V ) {
1784 assert(V && "Can't insert a null Value into SlotMachine!");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001785 assert(!isa<Constant>(V) || isa<GlobalValue>(V) &&
1786 "Can't insert a non-GlobalValue Constant into SlotMachine");
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001787
Reid Spencer56010e42004-05-26 21:56:09 +00001788 // If this value does not contribute to a plane (is void)
Misha Brukmanb1c93172005-04-21 23:48:37 +00001789 // or if the value already has a name then ignore it.
Reid Spencer56010e42004-05-26 21:56:09 +00001790 if (V->getType() == Type::VoidTy || V->hasName() ) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001791 SC_DEBUG("ignored value " << *V << "\n");
1792 return 0; // FIXME: Wrong return value
1793 }
1794
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001795 const Type *VTy = V->getType();
1796 unsigned DestSlot = 0;
1797
Reid Spencer56010e42004-05-26 21:56:09 +00001798 if ( TheFunction ) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001799 TypedPlanes::iterator I = fMap.find( VTy );
Misha Brukmanb1c93172005-04-21 23:48:37 +00001800 if ( I == fMap.end() )
Reid Spencer58d30f22004-07-04 11:50:43 +00001801 I = fMap.insert(std::make_pair(VTy,ValuePlane())).first;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001802 DestSlot = I->second.map[V] = I->second.next_slot++;
1803 } else {
1804 TypedPlanes::iterator I = mMap.find( VTy );
1805 if ( I == mMap.end() )
Reid Spencer58d30f22004-07-04 11:50:43 +00001806 I = mMap.insert(std::make_pair(VTy,ValuePlane())).first;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001807 DestSlot = I->second.map[V] = I->second.next_slot++;
1808 }
1809
Misha Brukmanb1c93172005-04-21 23:48:37 +00001810 SC_DEBUG(" Inserting value [" << VTy << "] = " << V << " slot=" <<
Reid Spencer56010e42004-05-26 21:56:09 +00001811 DestSlot << " [");
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001812 // G = Global, C = Constant, T = Type, F = Function, o = other
Misha Brukmanb1c93172005-04-21 23:48:37 +00001813 SC_DEBUG((isa<GlobalVariable>(V) ? 'G' : (isa<Function>(V) ? 'F' :
Reid Spenceraccd7c72004-07-17 23:47:01 +00001814 (isa<Constant>(V) ? 'C' : 'o'))));
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001815 SC_DEBUG("]\n");
1816 return DestSlot;
1817}
1818
Reid Spencer58d30f22004-07-04 11:50:43 +00001819// Low level insert function. Minimal checking is done. This
1820// function is just for the convenience of createSlot (above).
1821unsigned SlotMachine::insertValue(const Type *Ty ) {
1822 assert(Ty && "Can't insert a null Type into SlotMachine!");
1823
1824 unsigned DestSlot = 0;
1825
1826 if ( TheFunction ) {
1827 DestSlot = fTypes.map[Ty] = fTypes.next_slot++;
1828 } else {
1829 DestSlot = fTypes.map[Ty] = fTypes.next_slot++;
1830 }
1831 SC_DEBUG(" Inserting type [" << DestSlot << "] = " << Ty << "\n");
1832 return DestSlot;
1833}
1834
Reid Spencere7e96712004-05-25 08:53:40 +00001835// vim: sw=2