blob: 889ae4067cbc74f591b34170747de652252c05f0 [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"
Vikram S. Adveb952b542002-07-14 23:14:45 +000024#include "llvm/Instruction.h"
Misha Brukman2d3fa9e2004-07-29 16:53:53 +000025#include "llvm/Instructions.h"
Chris Lattnerc70b3f62004-01-20 19:50:34 +000026#include "llvm/Module.h"
Chris Lattnerfee714f2001-09-07 16:36:04 +000027#include "llvm/SymbolTable.h"
Misha Brukman4685e262004-04-28 15:31:21 +000028#include "llvm/Assembly/Writer.h"
Chris Lattner58185f22002-10-02 19:38:55 +000029#include "llvm/Support/CFG.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000030#include "llvm/ADT/StringExtras.h"
31#include "llvm/ADT/STLExtras.h"
Chris Lattnerfee714f2001-09-07 16:36:04 +000032#include <algorithm>
Chris Lattner189d19f2003-11-21 20:23:48 +000033using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000034
Chris Lattner60a7dd12004-07-15 02:51:31 +000035namespace llvm {
Reid Spencer16f2f7f2004-05-26 07:18:52 +000036
37/// This class provides computation of slot numbers for LLVM Assembly writing.
38/// @brief LLVM Assembly Writing Slot Computation.
39class SlotMachine {
40
41/// @name Types
42/// @{
43public:
44
45 /// @brief A mapping of Values to slot numbers
46 typedef std::map<const Value*, unsigned> ValueMap;
Reid Spencer58d30f22004-07-04 11:50:43 +000047 typedef std::map<const Type*, unsigned> TypeMap;
Reid Spencer16f2f7f2004-05-26 07:18:52 +000048
49 /// @brief A plane with next slot number and ValueMap
Misha Brukmanb1c93172005-04-21 23:48:37 +000050 struct ValuePlane {
Reid Spencer16f2f7f2004-05-26 07:18:52 +000051 unsigned next_slot; ///< The next slot number to use
52 ValueMap map; ///< The map of Value* -> unsigned
Reid Spencer58d30f22004-07-04 11:50:43 +000053 ValuePlane() { next_slot = 0; } ///< Make sure we start at 0
54 };
55
56 struct TypePlane {
57 unsigned next_slot;
58 TypeMap map;
59 TypePlane() { next_slot = 0; }
60 void clear() { map.clear(); next_slot = 0; }
Reid Spencer16f2f7f2004-05-26 07:18:52 +000061 };
62
63 /// @brief The map of planes by Type
Reid Spencer58d30f22004-07-04 11:50:43 +000064 typedef std::map<const Type*, ValuePlane> TypedPlanes;
Reid Spencer16f2f7f2004-05-26 07:18:52 +000065
66/// @}
67/// @name Constructors
68/// @{
69public:
70 /// @brief Construct from a module
71 SlotMachine(const Module *M );
72
73 /// @brief Construct from a function, starting out in incorp state.
74 SlotMachine(const Function *F );
75
76/// @}
77/// @name Accessors
78/// @{
79public:
80 /// Return the slot number of the specified value in it's type
81 /// plane. Its an error to ask for something not in the SlotMachine.
82 /// Its an error to ask for a Type*
Chris Lattner757ee0b2004-06-09 19:41:19 +000083 int getSlot(const Value *V);
Reid Spencer58d30f22004-07-04 11:50:43 +000084 int getSlot(const Type*Ty);
Reid Spencer8beac692004-06-09 15:26:53 +000085
86 /// Determine if a Value has a slot or not
87 bool hasSlot(const Value* V);
Reid Spencer58d30f22004-07-04 11:50:43 +000088 bool hasSlot(const Type* Ty);
Reid Spencer16f2f7f2004-05-26 07:18:52 +000089
90/// @}
91/// @name Mutators
92/// @{
93public:
Misha Brukmanb1c93172005-04-21 23:48:37 +000094 /// If you'd like to deal with a function instead of just a module, use
Reid Spencer16f2f7f2004-05-26 07:18:52 +000095 /// this method to get its data into the SlotMachine.
Misha Brukmanb1c93172005-04-21 23:48:37 +000096 void incorporateFunction(const Function *F) {
97 TheFunction = F;
Reid Spencerb0ac8c42004-08-16 07:46:33 +000098 FunctionProcessed = false;
99 }
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000100
Misha Brukmanb1c93172005-04-21 23:48:37 +0000101 /// After calling incorporateFunction, use this method to remove the
102 /// most recently incorporated function from the SlotMachine. This
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000103 /// will reset the state of the machine back to just the module contents.
104 void purgeFunction();
105
106/// @}
107/// @name Implementation Details
108/// @{
109private:
Reid Spencer56010e42004-05-26 21:56:09 +0000110 /// This function does the actual initialization.
111 inline void initialize();
112
Misha Brukmanb1c93172005-04-21 23:48:37 +0000113 /// Values can be crammed into here at will. If they haven't
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000114 /// been inserted already, they get inserted, otherwise they are ignored.
115 /// Either way, the slot number for the Value* is returned.
116 unsigned createSlot(const Value *V);
Reid Spencer58d30f22004-07-04 11:50:43 +0000117 unsigned createSlot(const Type* Ty);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000118
119 /// Insert a value into the value table. Return the slot number
120 /// that it now occupies. BadThings(TM) will happen if you insert a
Misha Brukmanb1c93172005-04-21 23:48:37 +0000121 /// Value that's already been inserted.
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000122 unsigned insertValue( const Value *V );
Reid Spencer58d30f22004-07-04 11:50:43 +0000123 unsigned insertValue( const Type* Ty);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000124
125 /// Add all of the module level global variables (and their initializers)
126 /// and function declarations, but not the contents of those functions.
127 void processModule();
128
Reid Spencer56010e42004-05-26 21:56:09 +0000129 /// Add all of the functions arguments, basic blocks, and instructions
130 void processFunction();
131
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000132 SlotMachine(const SlotMachine &); // DO NOT IMPLEMENT
133 void operator=(const SlotMachine &); // DO NOT IMPLEMENT
134
135/// @}
136/// @name Data
137/// @{
138public:
139
140 /// @brief The module for which we are holding slot numbers
Reid Spencer56010e42004-05-26 21:56:09 +0000141 const Module* TheModule;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000142
Reid Spencer56010e42004-05-26 21:56:09 +0000143 /// @brief The function for which we are holding slot numbers
144 const Function* TheFunction;
Reid Spencerb0ac8c42004-08-16 07:46:33 +0000145 bool FunctionProcessed;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000146
147 /// @brief The TypePlanes map for the module level data
148 TypedPlanes mMap;
Reid Spencer58d30f22004-07-04 11:50:43 +0000149 TypePlane mTypes;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000150
151 /// @brief The TypePlanes map for the function level data
152 TypedPlanes fMap;
Reid Spencer58d30f22004-07-04 11:50:43 +0000153 TypePlane fTypes;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000154
155/// @}
156
157};
158
Chris Lattner60a7dd12004-07-15 02:51:31 +0000159} // end namespace llvm
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000160
Chris Lattnerc8b70922002-07-26 21:12:46 +0000161static RegisterPass<PrintModulePass>
162X("printm", "Print module to stderr",PassInfo::Analysis|PassInfo::Optimization);
163static RegisterPass<PrintFunctionPass>
164Y("print","Print function to stderr",PassInfo::Analysis|PassInfo::Optimization);
Chris Lattner7f8845a2002-07-23 18:07:49 +0000165
Misha Brukmanb1c93172005-04-21 23:48:37 +0000166static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000167 bool PrintName,
168 std::map<const Type *, std::string> &TypeTable,
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000169 SlotMachine *Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000170
Misha Brukmanb1c93172005-04-21 23:48:37 +0000171static void WriteAsOperandInternal(std::ostream &Out, const Type *T,
Reid Spencer58d30f22004-07-04 11:50:43 +0000172 bool PrintName,
173 std::map<const Type *, std::string> &TypeTable,
174 SlotMachine *Machine);
175
Chris Lattnerb86620e2001-10-29 16:37:48 +0000176static const Module *getModuleFromVal(const Value *V) {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000177 if (const Argument *MA = dyn_cast<Argument>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +0000178 return MA->getParent() ? MA->getParent()->getParent() : 0;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000179 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +0000180 return BB->getParent() ? BB->getParent()->getParent() : 0;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000181 else if (const Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner57698e22002-03-26 18:01:55 +0000182 const Function *M = I->getParent() ? I->getParent()->getParent() : 0;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000183 return M ? M->getParent() : 0;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000184 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +0000185 return GV->getParent();
Chris Lattnerb86620e2001-10-29 16:37:48 +0000186 return 0;
187}
188
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000189static SlotMachine *createSlotMachine(const Value *V) {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000190 if (const Argument *FA = dyn_cast<Argument>(V)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000191 return new SlotMachine(FA->getParent());
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000192 } else if (const Instruction *I = dyn_cast<Instruction>(V)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000193 return new SlotMachine(I->getParent()->getParent());
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000194 } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000195 return new SlotMachine(BB->getParent());
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000196 } else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)){
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000197 return new SlotMachine(GV->getParent());
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000198 } else if (const Function *Func = dyn_cast<Function>(V)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000199 return new SlotMachine(Func);
Chris Lattner7bfee412001-10-29 16:05:51 +0000200 }
201 return 0;
202}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000203
Chris Lattner1c343042003-08-22 05:40:38 +0000204// getLLVMName - Turn the specified string into an 'LLVM name', which is either
205// prefixed with % (if the string only contains simple characters) or is
206// surrounded with ""'s (if it has special chars in it).
Alkis Evlogimenos991d6ad2004-12-10 05:41:10 +0000207static std::string getLLVMName(const std::string &Name,
208 bool prefixName = true) {
Chris Lattner1c343042003-08-22 05:40:38 +0000209 assert(!Name.empty() && "Cannot get empty name!");
210
211 // First character cannot start with a number...
212 if (Name[0] >= '0' && Name[0] <= '9')
213 return "\"" + Name + "\"";
214
215 // Scan to see if we have any characters that are not on the "white list"
216 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
217 char C = Name[i];
218 assert(C != '"' && "Illegal character in LLVM value name!");
219 if ((C < 'a' || C > 'z') && (C < 'A' || C > 'Z') && (C < '0' || C > '9') &&
220 C != '-' && C != '.' && C != '_')
221 return "\"" + Name + "\"";
222 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000223
Chris Lattner1c343042003-08-22 05:40:38 +0000224 // If we get here, then the identifier is legal to use as a "VarID".
Alkis Evlogimenos991d6ad2004-12-10 05:41:10 +0000225 if (prefixName)
226 return "%"+Name;
227 else
228 return Name;
Chris Lattner1c343042003-08-22 05:40:38 +0000229}
230
Chris Lattnerb86620e2001-10-29 16:37:48 +0000231
Misha Brukmanc566ca362004-03-02 00:22:19 +0000232/// fillTypeNameTable - If the module has a symbol table, take all global types
233/// and stuff their names into the TypeNames map.
234///
Chris Lattnerb86620e2001-10-29 16:37:48 +0000235static void fillTypeNameTable(const Module *M,
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000236 std::map<const Type *, std::string> &TypeNames) {
Chris Lattner98cf1f52002-11-20 18:36:02 +0000237 if (!M) return;
238 const SymbolTable &ST = M->getSymbolTable();
Reid Spencere7e96712004-05-25 08:53:40 +0000239 SymbolTable::type_const_iterator TI = ST.type_begin();
240 for (; TI != ST.type_end(); ++TI ) {
241 // As a heuristic, don't insert pointer to primitive types, because
242 // they are used too often to have a single useful name.
243 //
244 const Type *Ty = cast<Type>(TI->second);
245 if (!isa<PointerType>(Ty) ||
Reid Spencer56010e42004-05-26 21:56:09 +0000246 !cast<PointerType>(Ty)->getElementType()->isPrimitiveType() ||
247 isa<OpaqueType>(cast<PointerType>(Ty)->getElementType()))
Reid Spencere7e96712004-05-25 08:53:40 +0000248 TypeNames.insert(std::make_pair(Ty, getLLVMName(TI->first)));
Chris Lattnerb86620e2001-10-29 16:37:48 +0000249 }
250}
251
252
253
Misha Brukmanb1c93172005-04-21 23:48:37 +0000254static void calcTypeName(const Type *Ty,
John Criswellcd116ba2004-06-01 14:54:08 +0000255 std::vector<const Type *> &TypeStack,
256 std::map<const Type *, std::string> &TypeNames,
257 std::string & Result){
258 if (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty)) {
259 Result += Ty->getDescription(); // Base case
260 return;
261 }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000262
263 // Check to see if the type is named.
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000264 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
John Criswellcd116ba2004-06-01 14:54:08 +0000265 if (I != TypeNames.end()) {
266 Result += I->second;
267 return;
268 }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000269
John Criswellcd116ba2004-06-01 14:54:08 +0000270 if (isa<OpaqueType>(Ty)) {
271 Result += "opaque";
272 return;
273 }
Chris Lattnerf14ead92003-10-30 00:22:33 +0000274
Chris Lattnerb86620e2001-10-29 16:37:48 +0000275 // Check to see if the Type is already on the stack...
276 unsigned Slot = 0, CurSize = TypeStack.size();
277 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
278
Misha Brukmanb1c93172005-04-21 23:48:37 +0000279 // This is another base case for the recursion. In this case, we know
Chris Lattnerb86620e2001-10-29 16:37:48 +0000280 // that we have looped back to a type that we have previously visited.
281 // Generate the appropriate upreference to handle this.
John Criswellcd116ba2004-06-01 14:54:08 +0000282 if (Slot < CurSize) {
283 Result += "\\" + utostr(CurSize-Slot); // Here's the upreference
284 return;
285 }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000286
287 TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
Misha Brukmanb1c93172005-04-21 23:48:37 +0000288
Chris Lattner6b727592004-06-17 18:19:28 +0000289 switch (Ty->getTypeID()) {
Chris Lattner91db5822002-03-29 03:44:36 +0000290 case Type::FunctionTyID: {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000291 const FunctionType *FTy = cast<FunctionType>(Ty);
John Criswellcd116ba2004-06-01 14:54:08 +0000292 calcTypeName(FTy->getReturnType(), TypeStack, TypeNames, Result);
293 Result += " (";
Chris Lattnerfa829be2004-02-09 04:14:01 +0000294 for (FunctionType::param_iterator I = FTy->param_begin(),
295 E = FTy->param_end(); I != E; ++I) {
296 if (I != FTy->param_begin())
Chris Lattnerb86620e2001-10-29 16:37:48 +0000297 Result += ", ";
John Criswellcd116ba2004-06-01 14:54:08 +0000298 calcTypeName(*I, TypeStack, TypeNames, Result);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000299 }
Chris Lattnerd816b532002-04-13 20:53:41 +0000300 if (FTy->isVarArg()) {
Chris Lattnerfa829be2004-02-09 04:14:01 +0000301 if (FTy->getNumParams()) Result += ", ";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000302 Result += "...";
303 }
304 Result += ")";
305 break;
306 }
307 case Type::StructTyID: {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000308 const StructType *STy = cast<StructType>(Ty);
John Criswellcd116ba2004-06-01 14:54:08 +0000309 Result += "{ ";
Chris Lattnerac6db752004-02-09 04:37:31 +0000310 for (StructType::element_iterator I = STy->element_begin(),
311 E = STy->element_end(); I != E; ++I) {
312 if (I != STy->element_begin())
Chris Lattnerb86620e2001-10-29 16:37:48 +0000313 Result += ", ";
John Criswellcd116ba2004-06-01 14:54:08 +0000314 calcTypeName(*I, TypeStack, TypeNames, Result);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000315 }
316 Result += " }";
317 break;
318 }
319 case Type::PointerTyID:
Misha Brukmanb1c93172005-04-21 23:48:37 +0000320 calcTypeName(cast<PointerType>(Ty)->getElementType(),
John Criswellcd116ba2004-06-01 14:54:08 +0000321 TypeStack, TypeNames, Result);
322 Result += "*";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000323 break;
324 case Type::ArrayTyID: {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000325 const ArrayType *ATy = cast<ArrayType>(Ty);
John Criswellcd116ba2004-06-01 14:54:08 +0000326 Result += "[" + utostr(ATy->getNumElements()) + " x ";
327 calcTypeName(ATy->getElementType(), TypeStack, TypeNames, Result);
328 Result += "]";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000329 break;
330 }
Brian Gaeke02209042004-08-20 06:00:58 +0000331 case Type::PackedTyID: {
332 const PackedType *PTy = cast<PackedType>(Ty);
333 Result += "<" + utostr(PTy->getNumElements()) + " x ";
334 calcTypeName(PTy->getElementType(), TypeStack, TypeNames, Result);
335 Result += ">";
336 break;
337 }
Chris Lattner15285ab2003-05-14 17:50:47 +0000338 case Type::OpaqueTyID:
John Criswellcd116ba2004-06-01 14:54:08 +0000339 Result += "opaque";
Chris Lattner15285ab2003-05-14 17:50:47 +0000340 break;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000341 default:
John Criswellcd116ba2004-06-01 14:54:08 +0000342 Result += "<unrecognized-type>";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000343 }
344
345 TypeStack.pop_back(); // Remove self from stack...
John Criswellcd116ba2004-06-01 14:54:08 +0000346 return;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000347}
348
349
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000350/// printTypeInt - The internal guts of printing out a type that has a
351/// potentially named portion.
352///
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000353static std::ostream &printTypeInt(std::ostream &Out, const Type *Ty,
354 std::map<const Type *, std::string> &TypeNames) {
Chris Lattnerb86620e2001-10-29 16:37:48 +0000355 // Primitive types always print out their description, regardless of whether
356 // they have been named or not.
357 //
Chris Lattner92d60532003-10-30 00:12:51 +0000358 if (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty))
359 return Out << Ty->getDescription();
Chris Lattnerb86620e2001-10-29 16:37:48 +0000360
361 // Check to see if the type is named.
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000362 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000363 if (I != TypeNames.end()) return Out << I->second;
364
365 // Otherwise we have a type that has not been named but is a derived type.
366 // Carefully recurse the type hierarchy to print out any contained symbolic
367 // names.
368 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000369 std::vector<const Type *> TypeStack;
John Criswellcd116ba2004-06-01 14:54:08 +0000370 std::string TypeName;
371 calcTypeName(Ty, TypeStack, TypeNames, TypeName);
Chris Lattner7f74a562002-01-20 22:54:45 +0000372 TypeNames.insert(std::make_pair(Ty, TypeName));//Cache type name for later use
John Criswellcd116ba2004-06-01 14:54:08 +0000373 return (Out << TypeName);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000374}
375
Chris Lattner34b95182001-10-31 04:33:19 +0000376
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000377/// WriteTypeSymbolic - This attempts to write the specified type as a symbolic
378/// type, iff there is an entry in the modules symbol table for the specified
379/// type or one of it's component types. This is slower than a simple x << Type
380///
Chris Lattner189d19f2003-11-21 20:23:48 +0000381std::ostream &llvm::WriteTypeSymbolic(std::ostream &Out, const Type *Ty,
382 const Module *M) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000383 Out << ' ';
Chris Lattnerb86620e2001-10-29 16:37:48 +0000384
385 // If they want us to print out a type, attempt to make it symbolic if there
386 // is a symbol table in the module...
Chris Lattner98cf1f52002-11-20 18:36:02 +0000387 if (M) {
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000388 std::map<const Type *, std::string> TypeNames;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000389 fillTypeNameTable(M, TypeNames);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000390
Chris Lattner72f866e2001-10-29 16:40:32 +0000391 return printTypeInt(Out, Ty, TypeNames);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000392 } else {
Chris Lattner72f866e2001-10-29 16:40:32 +0000393 return Out << Ty->getDescription();
Chris Lattnerb86620e2001-10-29 16:37:48 +0000394 }
395}
396
Misha Brukmanb1c93172005-04-21 23:48:37 +0000397/// @brief Internal constant writer.
398static void WriteConstantInt(std::ostream &Out, const Constant *CV,
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000399 bool PrintName,
400 std::map<const Type *, std::string> &TypeTable,
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000401 SlotMachine *Machine) {
Chris Lattner1e194682002-04-18 18:53:13 +0000402 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
403 Out << (CB == ConstantBool::True ? "true" : "false");
404 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV)) {
405 Out << CI->getValue();
406 } else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV)) {
407 Out << CI->getValue();
408 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
409 // We would like to output the FP constant value in exponential notation,
410 // but we cannot do this if doing so will lose precision. Check here to
411 // make sure that we only output it in exponential format if we can parse
412 // the value back and get the same value.
413 //
414 std::string StrVal = ftostr(CFP->getValue());
415
416 // Check to make sure that the stringized number is not some string like
417 // "Inf" or NaN, that atof will accept, but the lexer will not. Check that
418 // the string matches the "[-+]?[0-9]" regex.
419 //
420 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
421 ((StrVal[0] == '-' || StrVal[0] == '+') &&
Brian Gaeke87b4f072003-06-17 23:55:35 +0000422 (StrVal[1] >= '0' && StrVal[1] <= '9')))
Chris Lattner1e194682002-04-18 18:53:13 +0000423 // Reparse stringized version!
424 if (atof(StrVal.c_str()) == CFP->getValue()) {
Chris Lattner472cc102005-01-04 01:56:57 +0000425 Out << StrVal;
426 return;
Chris Lattner1e194682002-04-18 18:53:13 +0000427 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000428
Chris Lattner1e194682002-04-18 18:53:13 +0000429 // Otherwise we could not reparse it to exactly the same value, so we must
430 // output the string in hexadecimal format!
431 //
432 // Behave nicely in the face of C TBAA rules... see:
433 // http://www.nullstone.com/htmls/category/aliastyp.htm
434 //
Chris Lattner472cc102005-01-04 01:56:57 +0000435 union {
436 double D;
437 uint64_t U;
438 } V;
439 V.D = CFP->getValue();
440 assert(sizeof(double) == sizeof(uint64_t) &&
Chris Lattner1e194682002-04-18 18:53:13 +0000441 "assuming that double is 64 bits!");
Chris Lattner472cc102005-01-04 01:56:57 +0000442 Out << "0x" << utohexstr(V.U);
Chris Lattner1e194682002-04-18 18:53:13 +0000443
Chris Lattner76b2ff42004-02-15 05:55:15 +0000444 } else if (isa<ConstantAggregateZero>(CV)) {
445 Out << "zeroinitializer";
Chris Lattner1e194682002-04-18 18:53:13 +0000446 } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
447 // As a special case, print the array as a string if it is an array of
448 // ubytes or an array of sbytes with positive values.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000449 //
Chris Lattner1e194682002-04-18 18:53:13 +0000450 const Type *ETy = CA->getType()->getElementType();
451 bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
452
453 if (ETy == Type::SByteTy)
454 for (unsigned i = 0; i < CA->getNumOperands(); ++i)
455 if (cast<ConstantSInt>(CA->getOperand(i))->getValue() < 0) {
456 isString = false;
457 break;
458 }
459
460 if (isString) {
461 Out << "c\"";
462 for (unsigned i = 0; i < CA->getNumOperands(); ++i) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000463 unsigned char C =
Chris Lattneraa6ff272004-06-04 23:53:20 +0000464 (unsigned char)cast<ConstantInt>(CA->getOperand(i))->getRawValue();
Misha Brukmanb1c93172005-04-21 23:48:37 +0000465
Chris Lattnere5fd3862002-07-31 23:56:44 +0000466 if (isprint(C) && C != '"' && C != '\\') {
Chris Lattner1e194682002-04-18 18:53:13 +0000467 Out << C;
468 } else {
469 Out << '\\'
470 << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'))
471 << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
472 }
473 }
474 Out << "\"";
475
476 } else { // Cannot output in string format...
Misha Brukman21bbdb92004-06-04 21:11:51 +0000477 Out << '[';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000478 if (CA->getNumOperands()) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000479 Out << ' ';
Chris Lattner1e194682002-04-18 18:53:13 +0000480 printTypeInt(Out, ETy, TypeTable);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000481 WriteAsOperandInternal(Out, CA->getOperand(0),
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000482 PrintName, TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000483 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
484 Out << ", ";
Chris Lattner1e194682002-04-18 18:53:13 +0000485 printTypeInt(Out, ETy, TypeTable);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000486 WriteAsOperandInternal(Out, CA->getOperand(i), PrintName,
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000487 TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000488 }
489 }
490 Out << " ]";
491 }
492 } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000493 Out << '{';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000494 if (CS->getNumOperands()) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000495 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000496 printTypeInt(Out, CS->getOperand(0)->getType(), TypeTable);
497
498 WriteAsOperandInternal(Out, CS->getOperand(0),
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000499 PrintName, TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000500
501 for (unsigned i = 1; i < CS->getNumOperands(); i++) {
502 Out << ", ";
503 printTypeInt(Out, CS->getOperand(i)->getType(), TypeTable);
504
505 WriteAsOperandInternal(Out, CS->getOperand(i),
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000506 PrintName, TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000507 }
508 }
509
510 Out << " }";
Brian Gaeke02209042004-08-20 06:00:58 +0000511 } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(CV)) {
512 const Type *ETy = CP->getType()->getElementType();
Misha Brukmanb1c93172005-04-21 23:48:37 +0000513 assert(CP->getNumOperands() > 0 &&
Brian Gaeke02209042004-08-20 06:00:58 +0000514 "Number of operands for a PackedConst must be > 0");
515 Out << '<';
516 Out << ' ';
517 printTypeInt(Out, ETy, TypeTable);
518 WriteAsOperandInternal(Out, CP->getOperand(0),
519 PrintName, TypeTable, Machine);
520 for (unsigned i = 1, e = CP->getNumOperands(); i != e; ++i) {
521 Out << ", ";
522 printTypeInt(Out, ETy, TypeTable);
523 WriteAsOperandInternal(Out, CP->getOperand(i), PrintName,
524 TypeTable, Machine);
525 }
526 Out << " >";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000527 } else if (isa<ConstantPointerNull>(CV)) {
528 Out << "null";
529
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000530 } else if (isa<UndefValue>(CV)) {
531 Out << "undef";
532
Chris Lattner3cd8c562002-07-30 18:54:25 +0000533 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Chris Lattnerb8257222003-03-06 23:23:32 +0000534 Out << CE->getOpcodeName() << " (";
Misha Brukmanb1c93172005-04-21 23:48:37 +0000535
Vikram S. Adveb952b542002-07-14 23:14:45 +0000536 for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
537 printTypeInt(Out, (*OI)->getType(), TypeTable);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000538 WriteAsOperandInternal(Out, *OI, PrintName, TypeTable, Machine);
Vikram S. Adveb952b542002-07-14 23:14:45 +0000539 if (OI+1 != CE->op_end())
Chris Lattner3cd8c562002-07-30 18:54:25 +0000540 Out << ", ";
Vikram S. Adveb952b542002-07-14 23:14:45 +0000541 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000542
Chris Lattnercfe8f532002-08-16 21:17:11 +0000543 if (CE->getOpcode() == Instruction::Cast) {
Chris Lattner83b396b2002-08-15 19:37:43 +0000544 Out << " to ";
545 printTypeInt(Out, CE->getType(), TypeTable);
546 }
Misha Brukman21bbdb92004-06-04 21:11:51 +0000547 Out << ')';
Chris Lattner83b396b2002-08-15 19:37:43 +0000548
Chris Lattnerd84bb632002-04-16 21:36:08 +0000549 } else {
Vikram S. Adveb952b542002-07-14 23:14:45 +0000550 Out << "<placeholder or erroneous Constant>";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000551 }
552}
553
554
Misha Brukmanc566ca362004-03-02 00:22:19 +0000555/// WriteAsOperand - Write the name of the specified value out to the specified
556/// ostream. This can be useful when you just want to print int %reg126, not
557/// the whole instruction that generated it.
558///
Misha Brukmanb1c93172005-04-21 23:48:37 +0000559static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000560 bool PrintName,
561 std::map<const Type*, std::string> &TypeTable,
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000562 SlotMachine *Machine) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000563 Out << ' ';
Reid Spenceraccd7c72004-07-17 23:47:01 +0000564 if ((PrintName || isa<GlobalValue>(V)) && V->hasName())
Chris Lattner1c343042003-08-22 05:40:38 +0000565 Out << getLLVMName(V->getName());
Reid Spenceraccd7c72004-07-17 23:47:01 +0000566 else {
567 const Constant *CV = dyn_cast<Constant>(V);
568 if (CV && !isa<GlobalValue>(CV))
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000569 WriteConstantInt(Out, CV, PrintName, TypeTable, Machine);
Reid Spenceraccd7c72004-07-17 23:47:01 +0000570 else {
Chris Lattnerd84bb632002-04-16 21:36:08 +0000571 int Slot;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000572 if (Machine) {
Reid Spencer56010e42004-05-26 21:56:09 +0000573 Slot = Machine->getSlot(V);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000574 } else {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000575 Machine = createSlotMachine(V);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000576 if (Machine == 0)
Chris Lattner757ee0b2004-06-09 19:41:19 +0000577 Slot = Machine->getSlot(V);
578 else
579 Slot = -1;
Reid Spencer56010e42004-05-26 21:56:09 +0000580 delete Machine;
Chris Lattnerd84bb632002-04-16 21:36:08 +0000581 }
Chris Lattner757ee0b2004-06-09 19:41:19 +0000582 if (Slot != -1)
583 Out << '%' << Slot;
584 else
585 Out << "<badref>";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000586 }
587 }
588}
589
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000590/// WriteAsOperand - Write the name of the specified value out to the specified
591/// ostream. This can be useful when you just want to print int %reg126, not
592/// the whole instruction that generated it.
593///
Chris Lattner189d19f2003-11-21 20:23:48 +0000594std::ostream &llvm::WriteAsOperand(std::ostream &Out, const Value *V,
Misha Brukmanb1c93172005-04-21 23:48:37 +0000595 bool PrintType, bool PrintName,
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000596 const Module *Context) {
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000597 std::map<const Type *, std::string> TypeNames;
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000598 if (Context == 0) Context = getModuleFromVal(V);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000599
Chris Lattner98cf1f52002-11-20 18:36:02 +0000600 if (Context)
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000601 fillTypeNameTable(Context, TypeNames);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000602
603 if (PrintType)
604 printTypeInt(Out, V->getType(), TypeNames);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000605
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000606 WriteAsOperandInternal(Out, V, PrintName, TypeNames, 0);
Chris Lattner5e5abe32001-07-20 19:15:21 +0000607 return Out;
608}
609
Misha Brukmanb1c93172005-04-21 23:48:37 +0000610/// WriteAsOperandInternal - Write the name of the specified value out to
611/// the specified ostream. This can be useful when you just want to print
Reid Spencer58d30f22004-07-04 11:50:43 +0000612/// int %reg126, not the whole instruction that generated it.
613///
Misha Brukmanb1c93172005-04-21 23:48:37 +0000614static void WriteAsOperandInternal(std::ostream &Out, const Type *T,
Reid Spencer58d30f22004-07-04 11:50:43 +0000615 bool PrintName,
616 std::map<const Type*, std::string> &TypeTable,
617 SlotMachine *Machine) {
618 Out << ' ';
619 int Slot;
620 if (Machine) {
621 Slot = Machine->getSlot(T);
622 if (Slot != -1)
623 Out << '%' << Slot;
624 else
625 Out << "<badref>";
626 } else {
627 Out << T->getDescription();
628 }
629}
630
631/// WriteAsOperand - Write the name of the specified value out to the specified
632/// ostream. This can be useful when you just want to print int %reg126, not
633/// the whole instruction that generated it.
634///
635std::ostream &llvm::WriteAsOperand(std::ostream &Out, const Type *Ty,
Misha Brukmanb1c93172005-04-21 23:48:37 +0000636 bool PrintType, bool PrintName,
Reid Spencer58d30f22004-07-04 11:50:43 +0000637 const Module *Context) {
638 std::map<const Type *, std::string> TypeNames;
639 assert(Context != 0 && "Can't write types as operand without module context");
640
641 fillTypeNameTable(Context, TypeNames);
642
643 // if (PrintType)
644 // printTypeInt(Out, V->getType(), TypeNames);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000645
Reid Spencer58d30f22004-07-04 11:50:43 +0000646 printTypeInt(Out, Ty, TypeNames);
647
648 WriteAsOperandInternal(Out, Ty, PrintName, TypeNames, 0);
649 return Out;
650}
651
Chris Lattner189d19f2003-11-21 20:23:48 +0000652namespace llvm {
Chris Lattner2e9fee42001-07-12 23:35:26 +0000653
Chris Lattnerfee714f2001-09-07 16:36:04 +0000654class AssemblyWriter {
Misha Brukmana6619a92004-06-21 21:53:56 +0000655 std::ostream &Out;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000656 SlotMachine &Machine;
Chris Lattner7bfee412001-10-29 16:05:51 +0000657 const Module *TheModule;
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000658 std::map<const Type *, std::string> TypeNames;
Chris Lattner8339f7d2003-10-30 23:41:03 +0000659 AssemblyAnnotationWriter *AnnotationWriter;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000660public:
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000661 inline AssemblyWriter(std::ostream &o, SlotMachine &Mac, const Module *M,
Chris Lattner8339f7d2003-10-30 23:41:03 +0000662 AssemblyAnnotationWriter *AAW)
Misha Brukmana6619a92004-06-21 21:53:56 +0000663 : Out(o), Machine(Mac), TheModule(M), AnnotationWriter(AAW) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000664
665 // If the module has a symbol table, take all global types and stuff their
666 // names into the TypeNames map.
667 //
Chris Lattnerb86620e2001-10-29 16:37:48 +0000668 fillTypeNameTable(M, TypeNames);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000669 }
670
Chris Lattner7bfee412001-10-29 16:05:51 +0000671 inline void write(const Module *M) { printModule(M); }
672 inline void write(const GlobalVariable *G) { printGlobal(G); }
Chris Lattner57698e22002-03-26 18:01:55 +0000673 inline void write(const Function *F) { printFunction(F); }
Chris Lattner7bfee412001-10-29 16:05:51 +0000674 inline void write(const BasicBlock *BB) { printBasicBlock(BB); }
Chris Lattner113f4f42002-06-25 16:13:24 +0000675 inline void write(const Instruction *I) { printInstruction(*I); }
Chris Lattner3462ae32001-12-03 22:26:30 +0000676 inline void write(const Constant *CPV) { printConstant(CPV); }
Chris Lattner7db79582001-11-07 04:21:57 +0000677 inline void write(const Type *Ty) { printType(Ty); }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000678
Chris Lattner1e194682002-04-18 18:53:13 +0000679 void writeOperand(const Value *Op, bool PrintType, bool PrintName = true);
680
Misha Brukman4685e262004-04-28 15:31:21 +0000681 const Module* getModule() { return TheModule; }
682
Misha Brukmand92f54a2004-11-15 19:30:05 +0000683private:
Chris Lattner7bfee412001-10-29 16:05:51 +0000684 void printModule(const Module *M);
685 void printSymbolTable(const SymbolTable &ST);
Chris Lattner3462ae32001-12-03 22:26:30 +0000686 void printConstant(const Constant *CPV);
Chris Lattner7bfee412001-10-29 16:05:51 +0000687 void printGlobal(const GlobalVariable *GV);
Chris Lattner57698e22002-03-26 18:01:55 +0000688 void printFunction(const Function *F);
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000689 void printArgument(const Argument *FA);
Chris Lattner7bfee412001-10-29 16:05:51 +0000690 void printBasicBlock(const BasicBlock *BB);
Chris Lattner113f4f42002-06-25 16:13:24 +0000691 void printInstruction(const Instruction &I);
Chris Lattnerd816b532002-04-13 20:53:41 +0000692
693 // printType - Go to extreme measures to attempt to print out a short,
694 // symbolic version of a type name.
695 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000696 std::ostream &printType(const Type *Ty) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000697 return printTypeInt(Out, Ty, TypeNames);
Chris Lattnerd816b532002-04-13 20:53:41 +0000698 }
699
700 // printTypeAtLeastOneLevel - Print out one level of the possibly complex type
701 // without considering any symbolic types that we may have equal to it.
702 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000703 std::ostream &printTypeAtLeastOneLevel(const Type *Ty);
Chris Lattner7bfee412001-10-29 16:05:51 +0000704
Chris Lattner862e3382001-10-13 06:42:36 +0000705 // printInfoComment - Print a little comment after the instruction indicating
706 // which slot it occupies.
Chris Lattner113f4f42002-06-25 16:13:24 +0000707 void printInfoComment(const Value &V);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000708};
Reid Spencerf43ac622004-05-27 22:04:46 +0000709} // end of llvm namespace
Chris Lattner2f7c9632001-06-06 20:29:01 +0000710
Misha Brukmanc566ca362004-03-02 00:22:19 +0000711/// printTypeAtLeastOneLevel - Print out one level of the possibly complex type
712/// without considering any symbolic types that we may have equal to it.
713///
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000714std::ostream &AssemblyWriter::printTypeAtLeastOneLevel(const Type *Ty) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000715 if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000716 printType(FTy->getReturnType()) << " (";
Chris Lattnerfa829be2004-02-09 04:14:01 +0000717 for (FunctionType::param_iterator I = FTy->param_begin(),
718 E = FTy->param_end(); I != E; ++I) {
719 if (I != FTy->param_begin())
Misha Brukmana6619a92004-06-21 21:53:56 +0000720 Out << ", ";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000721 printType(*I);
Chris Lattnerd816b532002-04-13 20:53:41 +0000722 }
723 if (FTy->isVarArg()) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000724 if (FTy->getNumParams()) Out << ", ";
725 Out << "...";
Chris Lattnerd816b532002-04-13 20:53:41 +0000726 }
Misha Brukmana6619a92004-06-21 21:53:56 +0000727 Out << ')';
Chris Lattner113f4f42002-06-25 16:13:24 +0000728 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000729 Out << "{ ";
Chris Lattnerac6db752004-02-09 04:37:31 +0000730 for (StructType::element_iterator I = STy->element_begin(),
731 E = STy->element_end(); I != E; ++I) {
732 if (I != STy->element_begin())
Misha Brukmana6619a92004-06-21 21:53:56 +0000733 Out << ", ";
Chris Lattnerd816b532002-04-13 20:53:41 +0000734 printType(*I);
735 }
Misha Brukmana6619a92004-06-21 21:53:56 +0000736 Out << " }";
Chris Lattner113f4f42002-06-25 16:13:24 +0000737 } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000738 printType(PTy->getElementType()) << '*';
Chris Lattner113f4f42002-06-25 16:13:24 +0000739 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000740 Out << '[' << ATy->getNumElements() << " x ";
Misha Brukman21bbdb92004-06-04 21:11:51 +0000741 printType(ATy->getElementType()) << ']';
Reid Spencere203d352004-08-20 15:37:30 +0000742 } else if (const PackedType *PTy = dyn_cast<PackedType>(Ty)) {
743 Out << '<' << PTy->getNumElements() << " x ";
744 printType(PTy->getElementType()) << '>';
745 }
746 else if (const OpaqueType *OTy = dyn_cast<OpaqueType>(Ty)) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000747 Out << "opaque";
Chris Lattnerd816b532002-04-13 20:53:41 +0000748 } else {
Vikram S. Adveb952b542002-07-14 23:14:45 +0000749 if (!Ty->isPrimitiveType())
Misha Brukmana6619a92004-06-21 21:53:56 +0000750 Out << "<unknown derived type>";
Chris Lattnerd816b532002-04-13 20:53:41 +0000751 printType(Ty);
752 }
Misha Brukmana6619a92004-06-21 21:53:56 +0000753 return Out;
Chris Lattnerd816b532002-04-13 20:53:41 +0000754}
755
756
Misha Brukmanb1c93172005-04-21 23:48:37 +0000757void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType,
Reid Spencer56010e42004-05-26 21:56:09 +0000758 bool PrintName) {
Chris Lattner08f7d0c2005-02-24 16:58:29 +0000759 if (Operand != 0) {
760 if (PrintType) { Out << ' '; printType(Operand->getType()); }
761 WriteAsOperandInternal(Out, Operand, PrintName, TypeNames, &Machine);
762 } else {
763 Out << "<null operand!>";
764 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000765}
766
Chris Lattner2f7c9632001-06-06 20:29:01 +0000767
Chris Lattner7bfee412001-10-29 16:05:51 +0000768void AssemblyWriter::printModule(const Module *M) {
Chris Lattner4d8689e2005-03-02 23:12:40 +0000769 if (!M->getModuleIdentifier().empty() &&
Misha Brukmanb1c93172005-04-21 23:48:37 +0000770 // Don't print the ID if it will start a new line (which would
Chris Lattner4d8689e2005-03-02 23:12:40 +0000771 // require a comment char before it).
772 M->getModuleIdentifier().find('\n') == std::string::npos)
773 Out << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
774
Chris Lattner8068e0c2003-08-24 13:48:48 +0000775 switch (M->getEndianness()) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000776 case Module::LittleEndian: Out << "target endian = little\n"; break;
777 case Module::BigEndian: Out << "target endian = big\n"; break;
Chris Lattner8068e0c2003-08-24 13:48:48 +0000778 case Module::AnyEndianness: break;
779 }
780 switch (M->getPointerSize()) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000781 case Module::Pointer32: Out << "target pointersize = 32\n"; break;
782 case Module::Pointer64: Out << "target pointersize = 64\n"; break;
Chris Lattner8068e0c2003-08-24 13:48:48 +0000783 case Module::AnyPointerSize: break;
784 }
Reid Spencer48f98c82004-07-25 21:44:54 +0000785 if (!M->getTargetTriple().empty())
Reid Spencerffec7df2004-07-25 21:29:43 +0000786 Out << "target triple = \"" << M->getTargetTriple() << "\"\n";
Misha Brukmanb1c93172005-04-21 23:48:37 +0000787
Chris Lattner2cdd49d2004-09-14 05:06:58 +0000788 // Loop over the dependent libraries and emit them.
Chris Lattner730cfe42004-09-14 04:51:44 +0000789 Module::lib_iterator LI = M->lib_begin();
790 Module::lib_iterator LE = M->lib_end();
Reid Spencer48f98c82004-07-25 21:44:54 +0000791 if (LI != LE) {
Chris Lattner730cfe42004-09-14 04:51:44 +0000792 Out << "deplibs = [ ";
793 while (LI != LE) {
Chris Lattner2cdd49d2004-09-14 05:06:58 +0000794 Out << '"' << *LI << '"';
Reid Spencerffec7df2004-07-25 21:29:43 +0000795 ++LI;
Chris Lattner730cfe42004-09-14 04:51:44 +0000796 if (LI != LE)
797 Out << ", ";
Reid Spencerffec7df2004-07-25 21:29:43 +0000798 }
799 Out << " ]\n";
Reid Spencercc5ff642004-07-25 18:08:18 +0000800 }
Reid Spencerb9e08772004-09-13 23:44:23 +0000801
Chris Lattner2cdd49d2004-09-14 05:06:58 +0000802 // Loop over the symbol table, emitting all named constants.
Chris Lattner98cf1f52002-11-20 18:36:02 +0000803 printSymbolTable(M->getSymbolTable());
Misha Brukmanb1c93172005-04-21 23:48:37 +0000804
Chris Lattner531f9e92005-03-15 04:54:21 +0000805 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end(); I != E; ++I)
Chris Lattner113f4f42002-06-25 16:13:24 +0000806 printGlobal(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000807
Misha Brukmana6619a92004-06-21 21:53:56 +0000808 Out << "\nimplementation ; Functions:\n";
Misha Brukmanb1c93172005-04-21 23:48:37 +0000809
Chris Lattner2cdd49d2004-09-14 05:06:58 +0000810 // Output all of the functions.
Chris Lattner113f4f42002-06-25 16:13:24 +0000811 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
812 printFunction(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000813}
814
Chris Lattner7bfee412001-10-29 16:05:51 +0000815void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000816 if (GV->hasName()) Out << getLLVMName(GV->getName()) << " = ";
Chris Lattner37798642001-09-18 04:01:05 +0000817
Misha Brukmanb1c93172005-04-21 23:48:37 +0000818 if (!GV->hasInitializer())
Misha Brukmana6619a92004-06-21 21:53:56 +0000819 Out << "external ";
Chris Lattner379a8d22003-04-16 20:28:45 +0000820 else
821 switch (GV->getLinkage()) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000822 case GlobalValue::InternalLinkage: Out << "internal "; break;
823 case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
824 case GlobalValue::WeakLinkage: Out << "weak "; break;
825 case GlobalValue::AppendingLinkage: Out << "appending "; break;
Chris Lattner379a8d22003-04-16 20:28:45 +0000826 case GlobalValue::ExternalLinkage: break;
Misha Brukman70457632004-11-14 21:04:34 +0000827 case GlobalValue::GhostLinkage:
828 std::cerr << "GhostLinkage not allowed in AsmWriter!\n";
829 abort();
Chris Lattner379a8d22003-04-16 20:28:45 +0000830 }
Chris Lattner37798642001-09-18 04:01:05 +0000831
Misha Brukmana6619a92004-06-21 21:53:56 +0000832 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattner2413b162001-12-04 00:03:30 +0000833 printType(GV->getType()->getElementType());
Chris Lattner37798642001-09-18 04:01:05 +0000834
Reid Spenceraccd7c72004-07-17 23:47:01 +0000835 if (GV->hasInitializer()) {
836 Constant* C = cast<Constant>(GV->getInitializer());
837 assert(C && "GlobalVar initializer isn't constant?");
Reid Spencerc9c90cf2004-07-18 01:04:19 +0000838 writeOperand(GV->getInitializer(), false, isa<GlobalValue>(C));
Reid Spenceraccd7c72004-07-17 23:47:01 +0000839 }
Chris Lattner37798642001-09-18 04:01:05 +0000840
Chris Lattner113f4f42002-06-25 16:13:24 +0000841 printInfoComment(*GV);
Misha Brukmana6619a92004-06-21 21:53:56 +0000842 Out << "\n";
Chris Lattnerda975502001-09-10 07:58:01 +0000843}
844
Chris Lattnerfee714f2001-09-07 16:36:04 +0000845
Reid Spencere7e96712004-05-25 08:53:40 +0000846// printSymbolTable - Run through symbol table looking for constants
847// and types. Emit their declarations.
Chris Lattner7bfee412001-10-29 16:05:51 +0000848void AssemblyWriter::printSymbolTable(const SymbolTable &ST) {
Reid Spencere7e96712004-05-25 08:53:40 +0000849
850 // Print the types.
851 for (SymbolTable::type_const_iterator TI = ST.type_begin();
852 TI != ST.type_end(); ++TI ) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000853 Out << "\t" << getLLVMName(TI->first) << " = type ";
Reid Spencere7e96712004-05-25 08:53:40 +0000854
855 // Make sure we print out at least one level of the type structure, so
856 // that we do not get %FILE = type %FILE
857 //
858 printTypeAtLeastOneLevel(TI->second) << "\n";
859 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000860
Reid Spencere7e96712004-05-25 08:53:40 +0000861 // Print the constants, in type plane order.
862 for (SymbolTable::plane_const_iterator PI = ST.plane_begin();
863 PI != ST.plane_end(); ++PI ) {
864 SymbolTable::value_const_iterator VI = ST.value_begin(PI->first);
865 SymbolTable::value_const_iterator VE = ST.value_end(PI->first);
866
867 for (; VI != VE; ++VI) {
Reid Spenceraccd7c72004-07-17 23:47:01 +0000868 const Value* V = VI->second;
869 const Constant *CPV = dyn_cast<Constant>(V) ;
870 if (CPV && !isa<GlobalValue>(V)) {
Reid Spencer56010e42004-05-26 21:56:09 +0000871 printConstant(CPV);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000872 }
873 }
Chris Lattnera7620d92001-07-15 06:35:59 +0000874 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000875}
876
877
Misha Brukmanc566ca362004-03-02 00:22:19 +0000878/// printConstant - Print out a constant pool entry...
879///
Chris Lattner3462ae32001-12-03 22:26:30 +0000880void AssemblyWriter::printConstant(const Constant *CPV) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000881 // Don't print out unnamed constants, they will be inlined
882 if (!CPV->hasName()) return;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000883
Chris Lattneree998be2001-07-26 16:29:38 +0000884 // Print out name...
Misha Brukmana6619a92004-06-21 21:53:56 +0000885 Out << "\t" << getLLVMName(CPV->getName()) << " =";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000886
887 // Write the value out now...
Chris Lattnerd84bb632002-04-16 21:36:08 +0000888 writeOperand(CPV, true, false);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000889
Chris Lattner113f4f42002-06-25 16:13:24 +0000890 printInfoComment(*CPV);
Misha Brukmana6619a92004-06-21 21:53:56 +0000891 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000892}
893
Misha Brukmanc566ca362004-03-02 00:22:19 +0000894/// printFunction - Print all aspects of a function.
895///
Chris Lattner113f4f42002-06-25 16:13:24 +0000896void AssemblyWriter::printFunction(const Function *F) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000897 // Print out the return type and name...
Misha Brukmana6619a92004-06-21 21:53:56 +0000898 Out << "\n";
Chris Lattner379a8d22003-04-16 20:28:45 +0000899
Chris Lattner97e36f22004-12-05 06:44:09 +0000900 // Ensure that no local symbols conflict with global symbols.
901 const_cast<Function*>(F)->renameLocalSymbols();
902
Misha Brukmana6619a92004-06-21 21:53:56 +0000903 if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +0000904
Chris Lattner379a8d22003-04-16 20:28:45 +0000905 if (F->isExternal())
Misha Brukmana6619a92004-06-21 21:53:56 +0000906 Out << "declare ";
Chris Lattner379a8d22003-04-16 20:28:45 +0000907 else
908 switch (F->getLinkage()) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000909 case GlobalValue::InternalLinkage: Out << "internal "; break;
910 case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
911 case GlobalValue::WeakLinkage: Out << "weak "; break;
912 case GlobalValue::AppendingLinkage: Out << "appending "; break;
Chris Lattner379a8d22003-04-16 20:28:45 +0000913 case GlobalValue::ExternalLinkage: break;
Misha Brukman70457632004-11-14 21:04:34 +0000914 case GlobalValue::GhostLinkage:
915 std::cerr << "GhostLinkage not allowed in AsmWriter!\n";
916 abort();
Chris Lattner379a8d22003-04-16 20:28:45 +0000917 }
918
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000919 // Print the calling convention.
920 switch (F->getCallingConv()) {
921 case CallingConv::C: break; // default
922 case CallingConv::Fast: Out << "fastcc "; break;
923 case CallingConv::Cold: Out << "coldcc "; break;
924 default: Out << "cc" << F->getCallingConv() << " "; break;
925 }
926
Misha Brukman21bbdb92004-06-04 21:11:51 +0000927 printType(F->getReturnType()) << ' ';
Chris Lattner5b337482003-10-18 05:57:43 +0000928 if (!F->getName().empty())
Misha Brukmana6619a92004-06-21 21:53:56 +0000929 Out << getLLVMName(F->getName());
Chris Lattner5b337482003-10-18 05:57:43 +0000930 else
Misha Brukmana6619a92004-06-21 21:53:56 +0000931 Out << "\"\"";
932 Out << '(';
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000933 Machine.incorporateFunction(F);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000934
Chris Lattner7bfee412001-10-29 16:05:51 +0000935 // Loop over the arguments, printing them...
Chris Lattner113f4f42002-06-25 16:13:24 +0000936 const FunctionType *FT = F->getFunctionType();
Chris Lattnerfee714f2001-09-07 16:36:04 +0000937
Chris Lattner531f9e92005-03-15 04:54:21 +0000938 for(Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I)
Chris Lattner149376d2002-10-13 20:57:00 +0000939 printArgument(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000940
941 // Finish printing arguments...
Chris Lattner113f4f42002-06-25 16:13:24 +0000942 if (FT->isVarArg()) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000943 if (FT->getNumParams()) Out << ", ";
944 Out << "..."; // Output varargs portion of signature!
Chris Lattnerfee714f2001-09-07 16:36:04 +0000945 }
Misha Brukmana6619a92004-06-21 21:53:56 +0000946 Out << ')';
Chris Lattnerfee714f2001-09-07 16:36:04 +0000947
Chris Lattner113f4f42002-06-25 16:13:24 +0000948 if (F->isExternal()) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000949 Out << "\n";
Chris Lattnerb2f02e52002-05-06 03:00:40 +0000950 } else {
Misha Brukmana6619a92004-06-21 21:53:56 +0000951 Out << " {";
Misha Brukmanb1c93172005-04-21 23:48:37 +0000952
Chris Lattner6915f8f2002-04-07 22:49:37 +0000953 // Output all of its basic blocks... for the function
Chris Lattner113f4f42002-06-25 16:13:24 +0000954 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
955 printBasicBlock(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000956
Misha Brukmana6619a92004-06-21 21:53:56 +0000957 Out << "}\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000958 }
959
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000960 Machine.purgeFunction();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000961}
962
Misha Brukmanc566ca362004-03-02 00:22:19 +0000963/// printArgument - This member is called for every argument that is passed into
964/// the function. Simply print it out
965///
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000966void AssemblyWriter::printArgument(const Argument *Arg) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000967 // Insert commas as we go... the first arg doesn't get a comma
Chris Lattnerfb972ad2005-03-15 05:03:36 +0000968 if (Arg != Arg->getParent()->arg_begin()) Out << ", ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000969
970 // Output type...
Chris Lattner7bfee412001-10-29 16:05:51 +0000971 printType(Arg->getType());
Misha Brukmanb1c93172005-04-21 23:48:37 +0000972
Chris Lattner2f7c9632001-06-06 20:29:01 +0000973 // Output name, if available...
974 if (Arg->hasName())
Misha Brukmana6619a92004-06-21 21:53:56 +0000975 Out << ' ' << getLLVMName(Arg->getName());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000976}
977
Misha Brukmanc566ca362004-03-02 00:22:19 +0000978/// printBasicBlock - This member is called for each basic block in a method.
979///
Chris Lattner7bfee412001-10-29 16:05:51 +0000980void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000981 if (BB->hasName()) { // Print out the label if it exists...
Alkis Evlogimenos991d6ad2004-12-10 05:41:10 +0000982 Out << "\n" << getLLVMName(BB->getName(), false) << ':';
Chris Lattner408dbdb2002-05-14 16:02:05 +0000983 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Misha Brukmana6619a92004-06-21 21:53:56 +0000984 Out << "\n; <label>:";
Chris Lattner757ee0b2004-06-09 19:41:19 +0000985 int Slot = Machine.getSlot(BB);
986 if (Slot != -1)
Misha Brukmana6619a92004-06-21 21:53:56 +0000987 Out << Slot;
Chris Lattner757ee0b2004-06-09 19:41:19 +0000988 else
Misha Brukmana6619a92004-06-21 21:53:56 +0000989 Out << "<badref>";
Chris Lattner58185f22002-10-02 19:38:55 +0000990 }
Chris Lattner2447ef52003-11-20 00:09:43 +0000991
992 if (BB->getParent() == 0)
Misha Brukmana6619a92004-06-21 21:53:56 +0000993 Out << "\t\t; Error: Block without parent!";
Chris Lattner2447ef52003-11-20 00:09:43 +0000994 else {
995 if (BB != &BB->getParent()->front()) { // Not the entry block?
996 // Output predecessors for the block...
Misha Brukmana6619a92004-06-21 21:53:56 +0000997 Out << "\t\t;";
Chris Lattner2447ef52003-11-20 00:09:43 +0000998 pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000999
Chris Lattner2447ef52003-11-20 00:09:43 +00001000 if (PI == PE) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001001 Out << " No predecessors!";
Chris Lattner2447ef52003-11-20 00:09:43 +00001002 } else {
Misha Brukmana6619a92004-06-21 21:53:56 +00001003 Out << " preds =";
Chris Lattner00211f12003-11-16 22:59:57 +00001004 writeOperand(*PI, false, true);
Chris Lattner2447ef52003-11-20 00:09:43 +00001005 for (++PI; PI != PE; ++PI) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001006 Out << ',';
Chris Lattner2447ef52003-11-20 00:09:43 +00001007 writeOperand(*PI, false, true);
1008 }
Chris Lattner00211f12003-11-16 22:59:57 +00001009 }
Chris Lattner58185f22002-10-02 19:38:55 +00001010 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001011 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001012
Misha Brukmana6619a92004-06-21 21:53:56 +00001013 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001014
Misha Brukmana6619a92004-06-21 21:53:56 +00001015 if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001016
Chris Lattnerfee714f2001-09-07 16:36:04 +00001017 // Output all of the instructions in the basic block...
Chris Lattner113f4f42002-06-25 16:13:24 +00001018 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
1019 printInstruction(*I);
Chris Lattner96cdd272004-03-08 18:51:45 +00001020
Misha Brukmana6619a92004-06-21 21:53:56 +00001021 if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001022}
1023
Chris Lattner862e3382001-10-13 06:42:36 +00001024
Misha Brukmanc566ca362004-03-02 00:22:19 +00001025/// printInfoComment - Print a little comment after the instruction indicating
1026/// which slot it occupies.
1027///
Chris Lattner113f4f42002-06-25 16:13:24 +00001028void AssemblyWriter::printInfoComment(const Value &V) {
1029 if (V.getType() != Type::VoidTy) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001030 Out << "\t\t; <";
Misha Brukman21bbdb92004-06-04 21:11:51 +00001031 printType(V.getType()) << '>';
Chris Lattner862e3382001-10-13 06:42:36 +00001032
Chris Lattner113f4f42002-06-25 16:13:24 +00001033 if (!V.hasName()) {
Chris Lattner757ee0b2004-06-09 19:41:19 +00001034 int SlotNum = Machine.getSlot(&V);
1035 if (SlotNum == -1)
Misha Brukmana6619a92004-06-21 21:53:56 +00001036 Out << ":<badref>";
Reid Spencer8beac692004-06-09 15:26:53 +00001037 else
Misha Brukmana6619a92004-06-21 21:53:56 +00001038 Out << ':' << SlotNum; // Print out the def slot taken.
Chris Lattner862e3382001-10-13 06:42:36 +00001039 }
Chris Lattnerb6c21db2005-02-01 01:24:01 +00001040 Out << " [#uses=" << V.getNumUses() << ']'; // Output # uses
Chris Lattner862e3382001-10-13 06:42:36 +00001041 }
1042}
1043
Reid Spencer8beac692004-06-09 15:26:53 +00001044/// printInstruction - This member is called for each Instruction in a function..
Misha Brukmanc566ca362004-03-02 00:22:19 +00001045///
Chris Lattner113f4f42002-06-25 16:13:24 +00001046void AssemblyWriter::printInstruction(const Instruction &I) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001047 if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001048
Misha Brukmana6619a92004-06-21 21:53:56 +00001049 Out << "\t";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001050
1051 // Print out name if it exists...
Chris Lattner113f4f42002-06-25 16:13:24 +00001052 if (I.hasName())
Misha Brukmana6619a92004-06-21 21:53:56 +00001053 Out << getLLVMName(I.getName()) << " = ";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001054
Chris Lattner06038452005-05-06 05:51:46 +00001055 // If this is a volatile load or store, print out the volatile marker.
Chris Lattner504f9242003-09-08 17:45:59 +00001056 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
Chris Lattner06038452005-05-06 05:51:46 +00001057 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile())) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001058 Out << "volatile ";
Chris Lattner06038452005-05-06 05:51:46 +00001059 } else if (isa<CallInst>(I) && cast<CallInst>(I).isTailCall()) {
1060 // If this is a call, check if it's a tail call.
1061 Out << "tail ";
1062 }
Chris Lattner504f9242003-09-08 17:45:59 +00001063
Chris Lattner2f7c9632001-06-06 20:29:01 +00001064 // Print out the opcode...
Misha Brukmana6619a92004-06-21 21:53:56 +00001065 Out << I.getOpcodeName();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001066
1067 // Print out the type of the operands...
Chris Lattner113f4f42002-06-25 16:13:24 +00001068 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001069
1070 // Special case conditional branches to swizzle the condition out to the front
Chris Lattner113f4f42002-06-25 16:13:24 +00001071 if (isa<BranchInst>(I) && I.getNumOperands() > 1) {
1072 writeOperand(I.getOperand(2), true);
Misha Brukmana6619a92004-06-21 21:53:56 +00001073 Out << ',';
Chris Lattner2f7c9632001-06-06 20:29:01 +00001074 writeOperand(Operand, true);
Misha Brukmana6619a92004-06-21 21:53:56 +00001075 Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001076 writeOperand(I.getOperand(1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001077
Chris Lattner8d48df22002-04-13 18:34:38 +00001078 } else if (isa<SwitchInst>(I)) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001079 // Special case switch statement to get formatting nice and correct...
Misha Brukmana6619a92004-06-21 21:53:56 +00001080 writeOperand(Operand , true); Out << ',';
1081 writeOperand(I.getOperand(1), true); Out << " [";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001082
Chris Lattner113f4f42002-06-25 16:13:24 +00001083 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001084 Out << "\n\t\t";
1085 writeOperand(I.getOperand(op ), true); Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001086 writeOperand(I.getOperand(op+1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001087 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001088 Out << "\n\t]";
Chris Lattnerda558102001-10-02 03:41:24 +00001089 } else if (isa<PHINode>(I)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001090 Out << ' ';
Chris Lattner113f4f42002-06-25 16:13:24 +00001091 printType(I.getType());
Misha Brukmana6619a92004-06-21 21:53:56 +00001092 Out << ' ';
Chris Lattner2f7c9632001-06-06 20:29:01 +00001093
Chris Lattner113f4f42002-06-25 16:13:24 +00001094 for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001095 if (op) Out << ", ";
Misha Brukmanb1c93172005-04-21 23:48:37 +00001096 Out << '[';
Misha Brukmana6619a92004-06-21 21:53:56 +00001097 writeOperand(I.getOperand(op ), false); Out << ',';
1098 writeOperand(I.getOperand(op+1), false); Out << " ]";
Chris Lattner931ef3b2001-06-11 15:04:20 +00001099 }
Chris Lattner862e3382001-10-13 06:42:36 +00001100 } else if (isa<ReturnInst>(I) && !Operand) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001101 Out << " void";
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001102 } else if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
1103 // Print the calling convention being used.
1104 switch (CI->getCallingConv()) {
1105 case CallingConv::C: break; // default
1106 case CallingConv::Fast: Out << " fastcc"; break;
1107 case CallingConv::Cold: Out << " coldcc"; break;
1108 default: Out << " cc" << CI->getCallingConv(); break;
1109 }
1110
Chris Lattner463d6a52003-08-05 15:34:45 +00001111 const PointerType *PTy = cast<PointerType>(Operand->getType());
1112 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1113 const Type *RetTy = FTy->getReturnType();
Chris Lattner2f2d9472001-11-06 21:28:12 +00001114
Chris Lattner463d6a52003-08-05 15:34:45 +00001115 // If possible, print out the short form of the call instruction. We can
Chris Lattner6915f8f2002-04-07 22:49:37 +00001116 // only do this if the first argument is a pointer to a nonvararg function,
Chris Lattner463d6a52003-08-05 15:34:45 +00001117 // and if the return type is not a pointer to a function.
Chris Lattner2f2d9472001-11-06 21:28:12 +00001118 //
Chris Lattner463d6a52003-08-05 15:34:45 +00001119 if (!FTy->isVarArg() &&
Misha Brukmanb1c93172005-04-21 23:48:37 +00001120 (!isa<PointerType>(RetTy) ||
Chris Lattnerd9a36a62002-07-25 20:58:51 +00001121 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001122 Out << ' '; printType(RetTy);
Chris Lattner2f2d9472001-11-06 21:28:12 +00001123 writeOperand(Operand, false);
1124 } else {
1125 writeOperand(Operand, true);
1126 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001127 Out << '(';
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001128 if (CI->getNumOperands() > 1) writeOperand(CI->getOperand(1), true);
Chris Lattner113f4f42002-06-25 16:13:24 +00001129 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; ++op) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001130 Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001131 writeOperand(I.getOperand(op), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001132 }
1133
Misha Brukmana6619a92004-06-21 21:53:56 +00001134 Out << " )";
Chris Lattner113f4f42002-06-25 16:13:24 +00001135 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Chris Lattner463d6a52003-08-05 15:34:45 +00001136 const PointerType *PTy = cast<PointerType>(Operand->getType());
1137 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1138 const Type *RetTy = FTy->getReturnType();
1139
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001140 // Print the calling convention being used.
1141 switch (II->getCallingConv()) {
1142 case CallingConv::C: break; // default
1143 case CallingConv::Fast: Out << " fastcc"; break;
1144 case CallingConv::Cold: Out << " coldcc"; break;
1145 default: Out << " cc" << II->getCallingConv(); break;
1146 }
1147
Chris Lattner463d6a52003-08-05 15:34:45 +00001148 // If possible, print out the short form of the invoke instruction. We can
1149 // only do this if the first argument is a pointer to a nonvararg function,
1150 // and if the return type is not a pointer to a function.
1151 //
1152 if (!FTy->isVarArg() &&
Misha Brukmanb1c93172005-04-21 23:48:37 +00001153 (!isa<PointerType>(RetTy) ||
Chris Lattner463d6a52003-08-05 15:34:45 +00001154 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001155 Out << ' '; printType(RetTy);
Chris Lattner463d6a52003-08-05 15:34:45 +00001156 writeOperand(Operand, false);
1157 } else {
1158 writeOperand(Operand, true);
1159 }
1160
Misha Brukmana6619a92004-06-21 21:53:56 +00001161 Out << '(';
Chris Lattner113f4f42002-06-25 16:13:24 +00001162 if (I.getNumOperands() > 3) writeOperand(I.getOperand(3), true);
1163 for (unsigned op = 4, Eop = I.getNumOperands(); op < Eop; ++op) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001164 Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001165 writeOperand(I.getOperand(op), true);
Chris Lattner862e3382001-10-13 06:42:36 +00001166 }
1167
Misha Brukmana6619a92004-06-21 21:53:56 +00001168 Out << " )\n\t\t\tto";
Chris Lattner862e3382001-10-13 06:42:36 +00001169 writeOperand(II->getNormalDest(), true);
Misha Brukmana6619a92004-06-21 21:53:56 +00001170 Out << " unwind";
Chris Lattnerfae8ab32004-02-08 21:44:31 +00001171 writeOperand(II->getUnwindDest(), true);
Chris Lattner862e3382001-10-13 06:42:36 +00001172
Chris Lattner113f4f42002-06-25 16:13:24 +00001173 } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(&I)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001174 Out << ' ';
Chris Lattner8d48df22002-04-13 18:34:38 +00001175 printType(AI->getType()->getElementType());
1176 if (AI->isArrayAllocation()) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001177 Out << ',';
Chris Lattner8d48df22002-04-13 18:34:38 +00001178 writeOperand(AI->getArraySize(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001179 }
Chris Lattner862e3382001-10-13 06:42:36 +00001180 } else if (isa<CastInst>(I)) {
Chris Lattnerc96e96b2003-11-17 01:17:04 +00001181 if (Operand) writeOperand(Operand, true); // Work with broken code
Misha Brukmana6619a92004-06-21 21:53:56 +00001182 Out << " to ";
Chris Lattner113f4f42002-06-25 16:13:24 +00001183 printType(I.getType());
Chris Lattner5b337482003-10-18 05:57:43 +00001184 } else if (isa<VAArgInst>(I)) {
Chris Lattnerc96e96b2003-11-17 01:17:04 +00001185 if (Operand) writeOperand(Operand, true); // Work with broken code
Misha Brukmana6619a92004-06-21 21:53:56 +00001186 Out << ", ";
Chris Lattnerf70da102003-05-08 02:44:12 +00001187 printType(I.getType());
Chris Lattner5b337482003-10-18 05:57:43 +00001188 } else if (const VANextInst *VAN = dyn_cast<VANextInst>(&I)) {
Chris Lattnerc96e96b2003-11-17 01:17:04 +00001189 if (Operand) writeOperand(Operand, true); // Work with broken code
Misha Brukmana6619a92004-06-21 21:53:56 +00001190 Out << ", ";
Chris Lattner5b337482003-10-18 05:57:43 +00001191 printType(VAN->getArgType());
Chris Lattner2f7c9632001-06-06 20:29:01 +00001192 } else if (Operand) { // Print the normal way...
1193
Misha Brukmanb1c93172005-04-21 23:48:37 +00001194 // PrintAllTypes - Instructions who have operands of all the same type
Chris Lattner2f7c9632001-06-06 20:29:01 +00001195 // omit the type from all but the first operand. If the instruction has
1196 // different type operands (for example br), then they are all printed.
1197 bool PrintAllTypes = false;
1198 const Type *TheType = Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001199
Chris Lattner52bd5cb92004-03-12 05:53:14 +00001200 // Shift Left & Right print both types even for Ubyte LHS, and select prints
1201 // types even if all operands are bools.
Chris Lattner159485f2005-02-09 17:45:03 +00001202 if (isa<ShiftInst>(I) || isa<SelectInst>(I) || isa<StoreInst>(I)) {
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00001203 PrintAllTypes = true;
1204 } else {
1205 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
1206 Operand = I.getOperand(i);
1207 if (Operand->getType() != TheType) {
1208 PrintAllTypes = true; // We have differing types! Print them all!
1209 break;
1210 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001211 }
1212 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001213
Chris Lattner7bfee412001-10-29 16:05:51 +00001214 if (!PrintAllTypes) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001215 Out << ' ';
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00001216 printType(TheType);
Chris Lattner7bfee412001-10-29 16:05:51 +00001217 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001218
Chris Lattner113f4f42002-06-25 16:13:24 +00001219 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001220 if (i) Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001221 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001222 }
1223 }
1224
Chris Lattner862e3382001-10-13 06:42:36 +00001225 printInfoComment(I);
Misha Brukmana6619a92004-06-21 21:53:56 +00001226 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001227}
1228
1229
1230//===----------------------------------------------------------------------===//
1231// External Interface declarations
1232//===----------------------------------------------------------------------===//
1233
Chris Lattner8339f7d2003-10-30 23:41:03 +00001234void Module::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001235 SlotMachine SlotTable(this);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001236 AssemblyWriter W(o, SlotTable, this, AAW);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001237 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001238}
1239
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001240void GlobalVariable::print(std::ostream &o) const {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001241 SlotMachine SlotTable(getParent());
Chris Lattner8339f7d2003-10-30 23:41:03 +00001242 AssemblyWriter W(o, SlotTable, getParent(), 0);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001243 W.write(this);
Chris Lattneradfe0d12001-09-10 20:08:19 +00001244}
1245
Chris Lattner8339f7d2003-10-30 23:41:03 +00001246void Function::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001247 SlotMachine SlotTable(getParent());
Chris Lattner8339f7d2003-10-30 23:41:03 +00001248 AssemblyWriter W(o, SlotTable, getParent(), AAW);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001249
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001250 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001251}
1252
Chris Lattner8339f7d2003-10-30 23:41:03 +00001253void BasicBlock::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001254 SlotMachine SlotTable(getParent());
Misha Brukmanb1c93172005-04-21 23:48:37 +00001255 AssemblyWriter W(o, SlotTable,
Chris Lattner8339f7d2003-10-30 23:41:03 +00001256 getParent() ? getParent()->getParent() : 0, AAW);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001257 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001258}
1259
Chris Lattner8339f7d2003-10-30 23:41:03 +00001260void Instruction::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001261 const Function *F = getParent() ? getParent()->getParent() : 0;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001262 SlotMachine SlotTable(F);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001263 AssemblyWriter W(o, SlotTable, F ? F->getParent() : 0, AAW);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001264
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001265 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001266}
Chris Lattner7db79582001-11-07 04:21:57 +00001267
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001268void Constant::print(std::ostream &o) const {
1269 if (this == 0) { o << "<null> constant value\n"; return; }
Chris Lattner7d734802002-09-10 15:53:49 +00001270
Misha Brukman21bbdb92004-06-04 21:11:51 +00001271 o << ' ' << getType()->getDescription() << ' ';
Chris Lattner1e194682002-04-18 18:53:13 +00001272
Chris Lattnerab7d1ab2003-05-08 02:08:14 +00001273 std::map<const Type *, std::string> TypeTable;
Chris Lattner1e194682002-04-18 18:53:13 +00001274 WriteConstantInt(o, this, false, TypeTable, 0);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001275}
1276
Misha Brukmanb1c93172005-04-21 23:48:37 +00001277void Type::print(std::ostream &o) const {
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001278 if (this == 0)
1279 o << "<null Type>";
1280 else
1281 o << getDescription();
1282}
1283
Chris Lattner2e9fa6d2002-04-09 19:48:49 +00001284void Argument::print(std::ostream &o) const {
Chris Lattnere52ed452004-07-13 23:14:34 +00001285 WriteAsOperand(o, this, true, true,
1286 getParent() ? getParent()->getParent() : 0);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001287}
1288
Reid Spencer52641832004-05-25 18:14:38 +00001289// Value::dump - allow easy printing of Values from the debugger.
1290// Located here because so much of the needed functionality is here.
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001291void Value::dump() const { print(std::cerr); }
Reid Spencer52641832004-05-25 18:14:38 +00001292
1293// Type::dump - allow easy printing of Values from the debugger.
1294// Located here because so much of the needed functionality is here.
Reid Spencere7e96712004-05-25 08:53:40 +00001295void Type::dump() const { print(std::cerr); }
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001296
1297//===----------------------------------------------------------------------===//
1298// CachedWriter Class Implementation
1299//===----------------------------------------------------------------------===//
1300
Chris Lattner7db79582001-11-07 04:21:57 +00001301void CachedWriter::setModule(const Module *M) {
1302 delete SC; delete AW;
1303 if (M) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001304 SC = new SlotMachine(M );
Misha Brukman21bbdb92004-06-04 21:11:51 +00001305 AW = new AssemblyWriter(Out, *SC, M, 0);
Chris Lattner7db79582001-11-07 04:21:57 +00001306 } else {
1307 SC = 0; AW = 0;
1308 }
1309}
1310
1311CachedWriter::~CachedWriter() {
1312 delete AW;
1313 delete SC;
1314}
1315
Chris Lattner60a7dd12004-07-15 02:51:31 +00001316CachedWriter &CachedWriter::operator<<(const Value &V) {
Chris Lattner7db79582001-11-07 04:21:57 +00001317 assert(AW && SC && "CachedWriter does not have a current module!");
Chris Lattner60a7dd12004-07-15 02:51:31 +00001318 if (const Instruction *I = dyn_cast<Instruction>(&V))
Chris Lattner80d2d532004-06-26 19:40:40 +00001319 AW->write(I);
Chris Lattner60a7dd12004-07-15 02:51:31 +00001320 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(&V))
Chris Lattner80d2d532004-06-26 19:40:40 +00001321 AW->write(BB);
Chris Lattner60a7dd12004-07-15 02:51:31 +00001322 else if (const Function *F = dyn_cast<Function>(&V))
Chris Lattner80d2d532004-06-26 19:40:40 +00001323 AW->write(F);
Chris Lattner60a7dd12004-07-15 02:51:31 +00001324 else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(&V))
Chris Lattner80d2d532004-06-26 19:40:40 +00001325 AW->write(GV);
Misha Brukmanb1c93172005-04-21 23:48:37 +00001326 else
Chris Lattner60a7dd12004-07-15 02:51:31 +00001327 AW->writeOperand(&V, true, true);
Chris Lattner7db79582001-11-07 04:21:57 +00001328 return *this;
1329}
Misha Brukman4685e262004-04-28 15:31:21 +00001330
Chris Lattner60a7dd12004-07-15 02:51:31 +00001331CachedWriter& CachedWriter::operator<<(const Type &Ty) {
Misha Brukman4685e262004-04-28 15:31:21 +00001332 if (SymbolicTypes) {
1333 const Module *M = AW->getModule();
Chris Lattner60a7dd12004-07-15 02:51:31 +00001334 if (M) WriteTypeSymbolic(Out, &Ty, M);
Reid Spencer58d30f22004-07-04 11:50:43 +00001335 } else {
Chris Lattner60a7dd12004-07-15 02:51:31 +00001336 AW->write(&Ty);
Reid Spencer58d30f22004-07-04 11:50:43 +00001337 }
1338 return *this;
Misha Brukman4685e262004-04-28 15:31:21 +00001339}
Misha Brukmanda546ea2004-04-28 19:24:28 +00001340
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001341//===----------------------------------------------------------------------===//
1342//===-- SlotMachine Implementation
1343//===----------------------------------------------------------------------===//
1344
1345#if 0
1346#define SC_DEBUG(X) std::cerr << X
1347#else
1348#define SC_DEBUG(X)
1349#endif
1350
1351// Module level constructor. Causes the contents of the Module (sans functions)
1352// to be added to the slot table.
Misha Brukmanb1c93172005-04-21 23:48:37 +00001353SlotMachine::SlotMachine(const Module *M)
Reid Spencer56010e42004-05-26 21:56:09 +00001354 : TheModule(M) ///< Saved for lazy initialization.
1355 , TheFunction(0)
Reid Spencerb0ac8c42004-08-16 07:46:33 +00001356 , FunctionProcessed(false)
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001357 , mMap()
Reid Spencer58d30f22004-07-04 11:50:43 +00001358 , mTypes()
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001359 , fMap()
Reid Spencer58d30f22004-07-04 11:50:43 +00001360 , fTypes()
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001361{
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001362}
1363
1364// Function level constructor. Causes the contents of the Module and the one
1365// function provided to be added to the slot table.
Misha Brukmanb1c93172005-04-21 23:48:37 +00001366SlotMachine::SlotMachine(const Function *F )
Reid Spencer56010e42004-05-26 21:56:09 +00001367 : TheModule( F ? F->getParent() : 0 ) ///< Saved for lazy initialization
1368 , TheFunction(F) ///< Saved for lazy initialization
Reid Spencerb0ac8c42004-08-16 07:46:33 +00001369 , FunctionProcessed(false)
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001370 , mMap()
Reid Spencer58d30f22004-07-04 11:50:43 +00001371 , mTypes()
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001372 , fMap()
Reid Spencer58d30f22004-07-04 11:50:43 +00001373 , fTypes()
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001374{
Reid Spencer56010e42004-05-26 21:56:09 +00001375}
1376
1377inline void SlotMachine::initialize(void) {
Misha Brukmanb1c93172005-04-21 23:48:37 +00001378 if ( TheModule) {
1379 processModule();
Reid Spencer56010e42004-05-26 21:56:09 +00001380 TheModule = 0; ///< Prevent re-processing next time we're called.
1381 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001382 if ( TheFunction && ! FunctionProcessed) {
1383 processFunction();
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001384 }
1385}
1386
1387// Iterate through all the global variables, functions, and global
Misha Brukmanb1c93172005-04-21 23:48:37 +00001388// variable initializers and create slots for them.
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001389void SlotMachine::processModule() {
1390 SC_DEBUG("begin processModule!\n");
1391
1392 // Add all of the global variables to the value table...
Chris Lattner531f9e92005-03-15 04:54:21 +00001393 for (Module::const_global_iterator I = TheModule->global_begin(), E = TheModule->global_end();
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001394 I != E; ++I)
1395 createSlot(I);
1396
1397 // Add all the functions to the table
1398 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
1399 I != E; ++I)
1400 createSlot(I);
1401
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001402 SC_DEBUG("end processModule!\n");
1403}
1404
1405
Reid Spencer56010e42004-05-26 21:56:09 +00001406// Process the arguments, basic blocks, and instructions of a function.
1407void SlotMachine::processFunction() {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001408 SC_DEBUG("begin processFunction!\n");
1409
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001410 // Add all the function arguments
Misha Brukmanb1c93172005-04-21 23:48:37 +00001411 for(Function::const_arg_iterator AI = TheFunction->arg_begin(),
Chris Lattner531f9e92005-03-15 04:54:21 +00001412 AE = TheFunction->arg_end(); AI != AE; ++AI)
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001413 createSlot(AI);
1414
1415 SC_DEBUG("Inserting Instructions:\n");
1416
1417 // Add all of the basic blocks and instructions
Misha Brukmanb1c93172005-04-21 23:48:37 +00001418 for (Function::const_iterator BB = TheFunction->begin(),
Reid Spencer56010e42004-05-26 21:56:09 +00001419 E = TheFunction->end(); BB != E; ++BB) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001420 createSlot(BB);
1421 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
1422 createSlot(I);
1423 }
1424 }
1425
Reid Spencerb0ac8c42004-08-16 07:46:33 +00001426 FunctionProcessed = true;
1427
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001428 SC_DEBUG("end processFunction!\n");
1429}
1430
1431// Clean up after incorporating a function. This is the only way
Reid Spencer56010e42004-05-26 21:56:09 +00001432// to get out of the function incorporation state that affects the
1433// getSlot/createSlot lock. Function incorporation state is indicated
1434// by TheFunction != 0.
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001435void SlotMachine::purgeFunction() {
1436 SC_DEBUG("begin purgeFunction!\n");
1437 fMap.clear(); // Simply discard the function level map
Reid Spencer58d30f22004-07-04 11:50:43 +00001438 fTypes.clear();
Reid Spencer56010e42004-05-26 21:56:09 +00001439 TheFunction = 0;
Reid Spencerb0ac8c42004-08-16 07:46:33 +00001440 FunctionProcessed = false;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001441 SC_DEBUG("end purgeFunction!\n");
1442}
1443
1444/// Get the slot number for a value. This function will assert if you
1445/// ask for a Value that hasn't previously been inserted with createSlot.
1446/// Types are forbidden because Type does not inherit from Value (any more).
Chris Lattner757ee0b2004-06-09 19:41:19 +00001447int SlotMachine::getSlot(const Value *V) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001448 assert( V && "Can't get slot for null Value" );
Misha Brukmanb1c93172005-04-21 23:48:37 +00001449 assert(!isa<Constant>(V) || isa<GlobalValue>(V) &&
1450 "Can't insert a non-GlobalValue Constant into SlotMachine");
Reid Spencer56010e42004-05-26 21:56:09 +00001451
1452 // Check for uninitialized state and do lazy initialization
1453 this->initialize();
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001454
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001455 // Get the type of the value
1456 const Type* VTy = V->getType();
1457
1458 // Find the type plane in the module map
1459 TypedPlanes::const_iterator MI = mMap.find(VTy);
1460
Reid Spencer56010e42004-05-26 21:56:09 +00001461 if ( TheFunction ) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001462 // Lookup the type in the function map too
1463 TypedPlanes::const_iterator FI = fMap.find(VTy);
1464 // If there is a corresponding type plane in the function map
1465 if ( FI != fMap.end() ) {
1466 // Lookup the Value in the function map
1467 ValueMap::const_iterator FVI = FI->second.map.find(V);
1468 // If the value doesn't exist in the function map
1469 if ( FVI == FI->second.map.end() ) {
Chris Lattner68a038e2004-06-09 22:22:10 +00001470 // Look up the value in the module map.
1471 if (MI == mMap.end()) return -1;
Reid Spencer56010e42004-05-26 21:56:09 +00001472 ValueMap::const_iterator MVI = MI->second.map.find(V);
1473 // If we didn't find it, it wasn't inserted
Chris Lattner757ee0b2004-06-09 19:41:19 +00001474 if (MVI == MI->second.map.end()) return -1;
Reid Spencer56010e42004-05-26 21:56:09 +00001475 assert( MVI != MI->second.map.end() && "Value not found");
1476 // We found it only at the module level
Misha Brukmanb1c93172005-04-21 23:48:37 +00001477 return MVI->second;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001478
1479 // else the value exists in the function map
1480 } else {
Reid Spencer56010e42004-05-26 21:56:09 +00001481 // Return the slot number as the module's contribution to
1482 // the type plane plus the index in the function's contribution
1483 // to the type plane.
Chris Lattnerb1f04782004-06-15 21:07:32 +00001484 if (MI != mMap.end())
1485 return MI->second.next_slot + FVI->second;
1486 else
1487 return FVI->second;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001488 }
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001489 }
1490 }
1491
Reid Spencer8beac692004-06-09 15:26:53 +00001492 // N.B. Can get here only if either !TheFunction or the function doesn't
1493 // have a corresponding type plane for the Value
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001494
1495 // Make sure the type plane exists
Chris Lattner757ee0b2004-06-09 19:41:19 +00001496 if (MI == mMap.end()) return -1;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001497 // Lookup the value in the module's map
1498 ValueMap::const_iterator MVI = MI->second.map.find(V);
1499 // Make sure we found it.
Chris Lattner757ee0b2004-06-09 19:41:19 +00001500 if (MVI == MI->second.map.end()) return -1;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001501 // Return it.
1502 return MVI->second;
1503}
1504
Reid Spencer58d30f22004-07-04 11:50:43 +00001505/// Get the slot number for a value. This function will assert if you
1506/// ask for a Value that hasn't previously been inserted with createSlot.
1507/// Types are forbidden because Type does not inherit from Value (any more).
1508int SlotMachine::getSlot(const Type *Ty) {
1509 assert( Ty && "Can't get slot for null Type" );
1510
1511 // Check for uninitialized state and do lazy initialization
1512 this->initialize();
1513
1514 if ( TheFunction ) {
1515 // Lookup the Type in the function map
1516 TypeMap::const_iterator FTI = fTypes.map.find(Ty);
1517 // If the Type doesn't exist in the function map
1518 if ( FTI == fTypes.map.end() ) {
1519 TypeMap::const_iterator MTI = mTypes.map.find(Ty);
1520 // If we didn't find it, it wasn't inserted
Misha Brukmanb1c93172005-04-21 23:48:37 +00001521 if (MTI == mTypes.map.end())
Reid Spencer58d30f22004-07-04 11:50:43 +00001522 return -1;
1523 // We found it only at the module level
Misha Brukmanb1c93172005-04-21 23:48:37 +00001524 return MTI->second;
Reid Spencer58d30f22004-07-04 11:50:43 +00001525
1526 // else the value exists in the function map
1527 } else {
1528 // Return the slot number as the module's contribution to
1529 // the type plane plus the index in the function's contribution
1530 // to the type plane.
1531 return mTypes.next_slot + FTI->second;
1532 }
1533 }
1534
1535 // N.B. Can get here only if either !TheFunction
1536
1537 // Lookup the value in the module's map
1538 TypeMap::const_iterator MTI = mTypes.map.find(Ty);
1539 // Make sure we found it.
1540 if (MTI == mTypes.map.end()) return -1;
1541 // Return it.
1542 return MTI->second;
1543}
1544
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001545// Create a new slot, or return the existing slot if it is already
1546// inserted. Note that the logic here parallels getSlot but instead
1547// of asserting when the Value* isn't found, it inserts the value.
1548unsigned SlotMachine::createSlot(const Value *V) {
1549 assert( V && "Can't insert a null Value to SlotMachine");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001550 assert(!isa<Constant>(V) || isa<GlobalValue>(V) &&
1551 "Can't insert a non-GlobalValue Constant into SlotMachine");
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001552
1553 const Type* VTy = V->getType();
1554
1555 // Just ignore void typed things
1556 if (VTy == Type::VoidTy) return 0; // FIXME: Wrong return value!
1557
1558 // Look up the type plane for the Value's type from the module map
1559 TypedPlanes::const_iterator MI = mMap.find(VTy);
1560
Reid Spencer56010e42004-05-26 21:56:09 +00001561 if ( TheFunction ) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001562 // Get the type plane for the Value's type from the function map
1563 TypedPlanes::const_iterator FI = fMap.find(VTy);
1564 // If there is a corresponding type plane in the function map
1565 if ( FI != fMap.end() ) {
1566 // Lookup the Value in the function map
1567 ValueMap::const_iterator FVI = FI->second.map.find(V);
1568 // If the value doesn't exist in the function map
1569 if ( FVI == FI->second.map.end() ) {
Reid Spencer56010e42004-05-26 21:56:09 +00001570 // If there is no corresponding type plane in the module map
1571 if ( MI == mMap.end() )
1572 return insertValue(V);
1573 // Look up the value in the module map
1574 ValueMap::const_iterator MVI = MI->second.map.find(V);
1575 // If we didn't find it, it wasn't inserted
1576 if ( MVI == MI->second.map.end() )
1577 return insertValue(V);
1578 else
1579 // We found it only at the module level
1580 return MVI->second;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001581
1582 // else the value exists in the function map
1583 } else {
Reid Spencer56010e42004-05-26 21:56:09 +00001584 if ( MI == mMap.end() )
1585 return FVI->second;
1586 else
1587 // Return the slot number as the module's contribution to
1588 // the type plane plus the index in the function's contribution
1589 // to the type plane.
1590 return MI->second.next_slot + FVI->second;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001591 }
1592
1593 // else there is not a corresponding type plane in the function map
1594 } else {
1595 // If the type plane doesn't exists at the module level
1596 if ( MI == mMap.end() ) {
Reid Spencer56010e42004-05-26 21:56:09 +00001597 return insertValue(V);
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001598 // else type plane exists at the module level, examine it
1599 } else {
Reid Spencer56010e42004-05-26 21:56:09 +00001600 // Look up the value in the module's map
1601 ValueMap::const_iterator MVI = MI->second.map.find(V);
1602 // If we didn't find it there either
1603 if ( MVI == MI->second.map.end() )
1604 // Return the slot number as the module's contribution to
1605 // the type plane plus the index of the function map insertion.
1606 return MI->second.next_slot + insertValue(V);
1607 else
1608 return MVI->second;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001609 }
1610 }
1611 }
1612
Reid Spencer56010e42004-05-26 21:56:09 +00001613 // N.B. Can only get here if !TheFunction
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001614
1615 // If the module map's type plane is not for the Value's type
1616 if ( MI != mMap.end() ) {
1617 // Lookup the value in the module's map
1618 ValueMap::const_iterator MVI = MI->second.map.find(V);
Misha Brukmanb1c93172005-04-21 23:48:37 +00001619 if ( MVI != MI->second.map.end() )
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001620 return MVI->second;
1621 }
1622
1623 return insertValue(V);
1624}
1625
Reid Spencer58d30f22004-07-04 11:50:43 +00001626// Create a new slot, or return the existing slot if it is already
1627// inserted. Note that the logic here parallels getSlot but instead
1628// of asserting when the Value* isn't found, it inserts the value.
1629unsigned SlotMachine::createSlot(const Type *Ty) {
1630 assert( Ty && "Can't insert a null Type to SlotMachine");
1631
1632 if ( TheFunction ) {
1633 // Lookup the Type in the function map
1634 TypeMap::const_iterator FTI = fTypes.map.find(Ty);
1635 // If the type doesn't exist in the function map
1636 if ( FTI == fTypes.map.end() ) {
1637 // Look up the type in the module map
1638 TypeMap::const_iterator MTI = mTypes.map.find(Ty);
1639 // If we didn't find it, it wasn't inserted
1640 if ( MTI == mTypes.map.end() )
1641 return insertValue(Ty);
1642 else
1643 // We found it only at the module level
1644 return MTI->second;
1645
1646 // else the value exists in the function map
1647 } else {
1648 // Return the slot number as the module's contribution to
1649 // the type plane plus the index in the function's contribution
1650 // to the type plane.
1651 return mTypes.next_slot + FTI->second;
1652 }
1653 }
1654
1655 // N.B. Can only get here if !TheFunction
1656
1657 // Lookup the type in the module's map
1658 TypeMap::const_iterator MTI = mTypes.map.find(Ty);
Misha Brukmanb1c93172005-04-21 23:48:37 +00001659 if ( MTI != mTypes.map.end() )
Reid Spencer58d30f22004-07-04 11:50:43 +00001660 return MTI->second;
1661
1662 return insertValue(Ty);
1663}
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001664
1665// Low level insert function. Minimal checking is done. This
1666// function is just for the convenience of createSlot (above).
1667unsigned SlotMachine::insertValue(const Value *V ) {
1668 assert(V && "Can't insert a null Value into SlotMachine!");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001669 assert(!isa<Constant>(V) || isa<GlobalValue>(V) &&
1670 "Can't insert a non-GlobalValue Constant into SlotMachine");
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001671
Reid Spencer56010e42004-05-26 21:56:09 +00001672 // If this value does not contribute to a plane (is void)
Misha Brukmanb1c93172005-04-21 23:48:37 +00001673 // or if the value already has a name then ignore it.
Reid Spencer56010e42004-05-26 21:56:09 +00001674 if (V->getType() == Type::VoidTy || V->hasName() ) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001675 SC_DEBUG("ignored value " << *V << "\n");
1676 return 0; // FIXME: Wrong return value
1677 }
1678
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001679 const Type *VTy = V->getType();
1680 unsigned DestSlot = 0;
1681
Reid Spencer56010e42004-05-26 21:56:09 +00001682 if ( TheFunction ) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001683 TypedPlanes::iterator I = fMap.find( VTy );
Misha Brukmanb1c93172005-04-21 23:48:37 +00001684 if ( I == fMap.end() )
Reid Spencer58d30f22004-07-04 11:50:43 +00001685 I = fMap.insert(std::make_pair(VTy,ValuePlane())).first;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001686 DestSlot = I->second.map[V] = I->second.next_slot++;
1687 } else {
1688 TypedPlanes::iterator I = mMap.find( VTy );
1689 if ( I == mMap.end() )
Reid Spencer58d30f22004-07-04 11:50:43 +00001690 I = mMap.insert(std::make_pair(VTy,ValuePlane())).first;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001691 DestSlot = I->second.map[V] = I->second.next_slot++;
1692 }
1693
Misha Brukmanb1c93172005-04-21 23:48:37 +00001694 SC_DEBUG(" Inserting value [" << VTy << "] = " << V << " slot=" <<
Reid Spencer56010e42004-05-26 21:56:09 +00001695 DestSlot << " [");
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001696 // G = Global, C = Constant, T = Type, F = Function, o = other
Misha Brukmanb1c93172005-04-21 23:48:37 +00001697 SC_DEBUG((isa<GlobalVariable>(V) ? 'G' : (isa<Function>(V) ? 'F' :
Reid Spenceraccd7c72004-07-17 23:47:01 +00001698 (isa<Constant>(V) ? 'C' : 'o'))));
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001699 SC_DEBUG("]\n");
1700 return DestSlot;
1701}
1702
Reid Spencer58d30f22004-07-04 11:50:43 +00001703// Low level insert function. Minimal checking is done. This
1704// function is just for the convenience of createSlot (above).
1705unsigned SlotMachine::insertValue(const Type *Ty ) {
1706 assert(Ty && "Can't insert a null Type into SlotMachine!");
1707
1708 unsigned DestSlot = 0;
1709
1710 if ( TheFunction ) {
1711 DestSlot = fTypes.map[Ty] = fTypes.next_slot++;
1712 } else {
1713 DestSlot = fTypes.map[Ty] = fTypes.next_slot++;
1714 }
1715 SC_DEBUG(" Inserting type [" << DestSlot << "] = " << Ty << "\n");
1716 return DestSlot;
1717}
1718
Reid Spencere7e96712004-05-25 08:53:40 +00001719// vim: sw=2