blob: 0f76712a2b57f47e976e30589c76eae304c0eeb0 [file] [log] [blame]
Chris Lattner8da78af2002-04-07 22:31:46 +00001//===-- AsmWriter.cpp - Printing LLVM as an assembly file -----------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// 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.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
10// This library implements the functionality defined in llvm/Assembly/Writer.h
11//
Chris Lattner02b93992002-04-12 18:21:53 +000012// Note that these routines must be extremely tolerant of various errors in the
Chris Lattner8f77dae2003-05-08 02:44:12 +000013// LLVM code, because it can be used for debugging transformations.
Chris Lattner02b93992002-04-12 18:21:53 +000014//
Chris Lattner00950542001-06-06 20:29:01 +000015//===----------------------------------------------------------------------===//
16
Chris Lattnerda1fbcc2001-11-07 04:21:57 +000017#include "llvm/Assembly/CachedWriter.h"
Chris Lattner75cf7cf2002-04-08 22:03:40 +000018#include "llvm/Assembly/Writer.h"
Chris Lattnerf082b802002-07-23 18:07:49 +000019#include "llvm/Assembly/PrintModulePass.h"
Chris Lattner95e5a2c2003-10-30 23:41:03 +000020#include "llvm/Assembly/AsmAnnotationWriter.h"
Chris Lattnerf2d577b2004-01-20 19:50:34 +000021#include "llvm/Constants.h"
Chris Lattner3eb59c02002-04-29 18:46:50 +000022#include "llvm/DerivedTypes.h"
Vikram S. Adveb4dbb442002-07-14 23:14:45 +000023#include "llvm/Instruction.h"
Chris Lattner00950542001-06-06 20:29:01 +000024#include "llvm/iMemory.h"
Chris Lattnere02fa852001-10-13 06:42:36 +000025#include "llvm/iTerminators.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000026#include "llvm/iPHINode.h"
27#include "llvm/iOther.h"
Chris Lattnerf2d577b2004-01-20 19:50:34 +000028#include "llvm/Module.h"
Chris Lattner007377f2001-09-07 16:36:04 +000029#include "llvm/SymbolTable.h"
Misha Brukman5cf1acf2004-04-28 15:31:21 +000030#include "llvm/Assembly/Writer.h"
Chris Lattner061269b2002-10-02 19:38:55 +000031#include "llvm/Support/CFG.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000032#include "Support/StringExtras.h"
33#include "Support/STLExtras.h"
Chris Lattner007377f2001-09-07 16:36:04 +000034#include <algorithm>
Chris Lattner31f84992003-11-21 20:23:48 +000035using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000036
Chris Lattner0a8e8e12004-07-15 02:51:31 +000037namespace llvm {
Reid Spencer0d1b77e2004-05-26 07:18:52 +000038
39/// This class provides computation of slot numbers for LLVM Assembly writing.
40/// @brief LLVM Assembly Writing Slot Computation.
41class SlotMachine {
42
43/// @name Types
44/// @{
45public:
46
47 /// @brief A mapping of Values to slot numbers
48 typedef std::map<const Value*, unsigned> ValueMap;
Reid Spencer0e25e1c2004-07-04 11:50:43 +000049 typedef std::map<const Type*, unsigned> TypeMap;
Reid Spencer0d1b77e2004-05-26 07:18:52 +000050
51 /// @brief A plane with next slot number and ValueMap
Reid Spencer0e25e1c2004-07-04 11:50:43 +000052 struct ValuePlane {
Reid Spencer0d1b77e2004-05-26 07:18:52 +000053 unsigned next_slot; ///< The next slot number to use
54 ValueMap map; ///< The map of Value* -> unsigned
Reid Spencer0e25e1c2004-07-04 11:50:43 +000055 ValuePlane() { next_slot = 0; } ///< Make sure we start at 0
56 };
57
58 struct TypePlane {
59 unsigned next_slot;
60 TypeMap map;
61 TypePlane() { next_slot = 0; }
62 void clear() { map.clear(); next_slot = 0; }
Reid Spencer0d1b77e2004-05-26 07:18:52 +000063 };
64
65 /// @brief The map of planes by Type
Reid Spencer0e25e1c2004-07-04 11:50:43 +000066 typedef std::map<const Type*, ValuePlane> TypedPlanes;
Reid Spencer0d1b77e2004-05-26 07:18:52 +000067
68/// @}
69/// @name Constructors
70/// @{
71public:
72 /// @brief Construct from a module
73 SlotMachine(const Module *M );
74
75 /// @brief Construct from a function, starting out in incorp state.
76 SlotMachine(const Function *F );
77
78/// @}
79/// @name Accessors
80/// @{
81public:
82 /// Return the slot number of the specified value in it's type
83 /// plane. Its an error to ask for something not in the SlotMachine.
84 /// Its an error to ask for a Type*
Chris Lattner69566452004-06-09 19:41:19 +000085 int getSlot(const Value *V);
Reid Spencer0e25e1c2004-07-04 11:50:43 +000086 int getSlot(const Type*Ty);
Reid Spencerfc621e22004-06-09 15:26:53 +000087
88 /// Determine if a Value has a slot or not
89 bool hasSlot(const Value* V);
Reid Spencer0e25e1c2004-07-04 11:50:43 +000090 bool hasSlot(const Type* Ty);
Reid Spencer0d1b77e2004-05-26 07:18:52 +000091
92/// @}
93/// @name Mutators
94/// @{
95public:
96 /// If you'd like to deal with a function instead of just a module, use
97 /// this method to get its data into the SlotMachine.
Reid Spencerb03de0c2004-05-26 21:56:09 +000098 void incorporateFunction(const Function *F) { TheFunction = F; }
Reid Spencer0d1b77e2004-05-26 07:18:52 +000099
100 /// After calling incorporateFunction, use this method to remove the
101 /// most recently incorporated function from the SlotMachine. This
102 /// will reset the state of the machine back to just the module contents.
103 void purgeFunction();
104
105/// @}
106/// @name Implementation Details
107/// @{
108private:
Reid Spencerb03de0c2004-05-26 21:56:09 +0000109 /// This function does the actual initialization.
110 inline void initialize();
111
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000112 /// Values can be crammed into here at will. If they haven't
113 /// been inserted already, they get inserted, otherwise they are ignored.
114 /// Either way, the slot number for the Value* is returned.
115 unsigned createSlot(const Value *V);
Reid Spencer0e25e1c2004-07-04 11:50:43 +0000116 unsigned createSlot(const Type* Ty);
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000117
118 /// Insert a value into the value table. Return the slot number
119 /// that it now occupies. BadThings(TM) will happen if you insert a
120 /// Value that's already been inserted.
121 unsigned insertValue( const Value *V );
Reid Spencer0e25e1c2004-07-04 11:50:43 +0000122 unsigned insertValue( const Type* Ty);
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000123
124 /// Add all of the module level global variables (and their initializers)
125 /// and function declarations, but not the contents of those functions.
126 void processModule();
127
Reid Spencerb03de0c2004-05-26 21:56:09 +0000128 /// Add all of the functions arguments, basic blocks, and instructions
129 void processFunction();
130
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000131 SlotMachine(const SlotMachine &); // DO NOT IMPLEMENT
132 void operator=(const SlotMachine &); // DO NOT IMPLEMENT
133
134/// @}
135/// @name Data
136/// @{
137public:
138
139 /// @brief The module for which we are holding slot numbers
Reid Spencerb03de0c2004-05-26 21:56:09 +0000140 const Module* TheModule;
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000141
Reid Spencerb03de0c2004-05-26 21:56:09 +0000142 /// @brief The function for which we are holding slot numbers
143 const Function* TheFunction;
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000144
145 /// @brief The TypePlanes map for the module level data
146 TypedPlanes mMap;
Reid Spencer0e25e1c2004-07-04 11:50:43 +0000147 TypePlane mTypes;
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000148
149 /// @brief The TypePlanes map for the function level data
150 TypedPlanes fMap;
Reid Spencer0e25e1c2004-07-04 11:50:43 +0000151 TypePlane fTypes;
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000152
153/// @}
154
155};
156
Chris Lattner0a8e8e12004-07-15 02:51:31 +0000157} // end namespace llvm
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000158
Chris Lattnera6275cc2002-07-26 21:12:46 +0000159static RegisterPass<PrintModulePass>
160X("printm", "Print module to stderr",PassInfo::Analysis|PassInfo::Optimization);
161static RegisterPass<PrintFunctionPass>
162Y("print","Print function to stderr",PassInfo::Analysis|PassInfo::Optimization);
Chris Lattnerf082b802002-07-23 18:07:49 +0000163
Chris Lattner7b13f562003-05-08 02:08:14 +0000164static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
165 bool PrintName,
166 std::map<const Type *, std::string> &TypeTable,
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000167 SlotMachine *Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000168
Reid Spencer0e25e1c2004-07-04 11:50:43 +0000169static void WriteAsOperandInternal(std::ostream &Out, const Type *T,
170 bool PrintName,
171 std::map<const Type *, std::string> &TypeTable,
172 SlotMachine *Machine);
173
Chris Lattner207b5bc2001-10-29 16:37:48 +0000174static const Module *getModuleFromVal(const Value *V) {
Chris Lattner949a3622003-07-23 15:30:06 +0000175 if (const Argument *MA = dyn_cast<Argument>(V))
Chris Lattner207b5bc2001-10-29 16:37:48 +0000176 return MA->getParent() ? MA->getParent()->getParent() : 0;
Chris Lattner949a3622003-07-23 15:30:06 +0000177 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
Chris Lattner207b5bc2001-10-29 16:37:48 +0000178 return BB->getParent() ? BB->getParent()->getParent() : 0;
Chris Lattner949a3622003-07-23 15:30:06 +0000179 else if (const Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000180 const Function *M = I->getParent() ? I->getParent()->getParent() : 0;
Chris Lattner207b5bc2001-10-29 16:37:48 +0000181 return M ? M->getParent() : 0;
Chris Lattner949a3622003-07-23 15:30:06 +0000182 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
Chris Lattner207b5bc2001-10-29 16:37:48 +0000183 return GV->getParent();
Chris Lattner207b5bc2001-10-29 16:37:48 +0000184 return 0;
185}
186
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000187static SlotMachine *createSlotMachine(const Value *V) {
Chris Lattner949a3622003-07-23 15:30:06 +0000188 if (const Argument *FA = dyn_cast<Argument>(V)) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000189 return new SlotMachine(FA->getParent());
Chris Lattner949a3622003-07-23 15:30:06 +0000190 } else if (const Instruction *I = dyn_cast<Instruction>(V)) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000191 return new SlotMachine(I->getParent()->getParent());
Chris Lattner949a3622003-07-23 15:30:06 +0000192 } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000193 return new SlotMachine(BB->getParent());
Chris Lattner949a3622003-07-23 15:30:06 +0000194 } else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)){
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000195 return new SlotMachine(GV->getParent());
Chris Lattner949a3622003-07-23 15:30:06 +0000196 } else if (const Function *Func = dyn_cast<Function>(V)) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000197 return new SlotMachine(Func);
Chris Lattnerc1824992001-10-29 16:05:51 +0000198 }
199 return 0;
200}
Chris Lattner00950542001-06-06 20:29:01 +0000201
Chris Lattner24b8a5d2003-08-22 05:40:38 +0000202// getLLVMName - Turn the specified string into an 'LLVM name', which is either
203// prefixed with % (if the string only contains simple characters) or is
204// surrounded with ""'s (if it has special chars in it).
205static std::string getLLVMName(const std::string &Name) {
206 assert(!Name.empty() && "Cannot get empty name!");
207
208 // First character cannot start with a number...
209 if (Name[0] >= '0' && Name[0] <= '9')
210 return "\"" + Name + "\"";
211
212 // Scan to see if we have any characters that are not on the "white list"
213 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
214 char C = Name[i];
215 assert(C != '"' && "Illegal character in LLVM value name!");
216 if ((C < 'a' || C > 'z') && (C < 'A' || C > 'Z') && (C < '0' || C > '9') &&
217 C != '-' && C != '.' && C != '_')
218 return "\"" + Name + "\"";
219 }
220
221 // If we get here, then the identifier is legal to use as a "VarID".
222 return "%"+Name;
223}
224
Chris Lattner207b5bc2001-10-29 16:37:48 +0000225
Misha Brukmanab5c6002004-03-02 00:22:19 +0000226/// fillTypeNameTable - If the module has a symbol table, take all global types
227/// and stuff their names into the TypeNames map.
228///
Chris Lattner207b5bc2001-10-29 16:37:48 +0000229static void fillTypeNameTable(const Module *M,
Chris Lattner7b13f562003-05-08 02:08:14 +0000230 std::map<const Type *, std::string> &TypeNames) {
Chris Lattner6e6026b2002-11-20 18:36:02 +0000231 if (!M) return;
232 const SymbolTable &ST = M->getSymbolTable();
Reid Spencer9231ac82004-05-25 08:53:40 +0000233 SymbolTable::type_const_iterator TI = ST.type_begin();
234 for (; TI != ST.type_end(); ++TI ) {
235 // As a heuristic, don't insert pointer to primitive types, because
236 // they are used too often to have a single useful name.
237 //
238 const Type *Ty = cast<Type>(TI->second);
239 if (!isa<PointerType>(Ty) ||
Reid Spencerb03de0c2004-05-26 21:56:09 +0000240 !cast<PointerType>(Ty)->getElementType()->isPrimitiveType() ||
241 isa<OpaqueType>(cast<PointerType>(Ty)->getElementType()))
Reid Spencer9231ac82004-05-25 08:53:40 +0000242 TypeNames.insert(std::make_pair(Ty, getLLVMName(TI->first)));
Chris Lattner207b5bc2001-10-29 16:37:48 +0000243 }
244}
245
246
247
John Criswell4ff620a2004-06-01 14:54:08 +0000248static void calcTypeName(const Type *Ty,
249 std::vector<const Type *> &TypeStack,
250 std::map<const Type *, std::string> &TypeNames,
251 std::string & Result){
252 if (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty)) {
253 Result += Ty->getDescription(); // Base case
254 return;
255 }
Chris Lattner207b5bc2001-10-29 16:37:48 +0000256
257 // Check to see if the type is named.
Chris Lattner7b13f562003-05-08 02:08:14 +0000258 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
John Criswell4ff620a2004-06-01 14:54:08 +0000259 if (I != TypeNames.end()) {
260 Result += I->second;
261 return;
262 }
Chris Lattner207b5bc2001-10-29 16:37:48 +0000263
John Criswell4ff620a2004-06-01 14:54:08 +0000264 if (isa<OpaqueType>(Ty)) {
265 Result += "opaque";
266 return;
267 }
Chris Lattner88c17382003-10-30 00:22:33 +0000268
Chris Lattner207b5bc2001-10-29 16:37:48 +0000269 // Check to see if the Type is already on the stack...
270 unsigned Slot = 0, CurSize = TypeStack.size();
271 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
272
273 // This is another base case for the recursion. In this case, we know
274 // that we have looped back to a type that we have previously visited.
275 // Generate the appropriate upreference to handle this.
John Criswell4ff620a2004-06-01 14:54:08 +0000276 if (Slot < CurSize) {
277 Result += "\\" + utostr(CurSize-Slot); // Here's the upreference
278 return;
279 }
Chris Lattner207b5bc2001-10-29 16:37:48 +0000280
281 TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
282
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000283 switch (Ty->getTypeID()) {
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000284 case Type::FunctionTyID: {
Chris Lattner949a3622003-07-23 15:30:06 +0000285 const FunctionType *FTy = cast<FunctionType>(Ty);
John Criswell4ff620a2004-06-01 14:54:08 +0000286 calcTypeName(FTy->getReturnType(), TypeStack, TypeNames, Result);
287 Result += " (";
Chris Lattnerd5d89962004-02-09 04:14:01 +0000288 for (FunctionType::param_iterator I = FTy->param_begin(),
289 E = FTy->param_end(); I != E; ++I) {
290 if (I != FTy->param_begin())
Chris Lattner207b5bc2001-10-29 16:37:48 +0000291 Result += ", ";
John Criswell4ff620a2004-06-01 14:54:08 +0000292 calcTypeName(*I, TypeStack, TypeNames, Result);
Chris Lattner207b5bc2001-10-29 16:37:48 +0000293 }
Chris Lattner2761e9f2002-04-13 20:53:41 +0000294 if (FTy->isVarArg()) {
Chris Lattnerd5d89962004-02-09 04:14:01 +0000295 if (FTy->getNumParams()) Result += ", ";
Chris Lattner207b5bc2001-10-29 16:37:48 +0000296 Result += "...";
297 }
298 Result += ")";
299 break;
300 }
301 case Type::StructTyID: {
Chris Lattner949a3622003-07-23 15:30:06 +0000302 const StructType *STy = cast<StructType>(Ty);
John Criswell4ff620a2004-06-01 14:54:08 +0000303 Result += "{ ";
Chris Lattnerd21cd802004-02-09 04:37:31 +0000304 for (StructType::element_iterator I = STy->element_begin(),
305 E = STy->element_end(); I != E; ++I) {
306 if (I != STy->element_begin())
Chris Lattner207b5bc2001-10-29 16:37:48 +0000307 Result += ", ";
John Criswell4ff620a2004-06-01 14:54:08 +0000308 calcTypeName(*I, TypeStack, TypeNames, Result);
Chris Lattner207b5bc2001-10-29 16:37:48 +0000309 }
310 Result += " }";
311 break;
312 }
313 case Type::PointerTyID:
John Criswell4ff620a2004-06-01 14:54:08 +0000314 calcTypeName(cast<PointerType>(Ty)->getElementType(),
315 TypeStack, TypeNames, Result);
316 Result += "*";
Chris Lattner207b5bc2001-10-29 16:37:48 +0000317 break;
318 case Type::ArrayTyID: {
Chris Lattner949a3622003-07-23 15:30:06 +0000319 const ArrayType *ATy = cast<ArrayType>(Ty);
John Criswell4ff620a2004-06-01 14:54:08 +0000320 Result += "[" + utostr(ATy->getNumElements()) + " x ";
321 calcTypeName(ATy->getElementType(), TypeStack, TypeNames, Result);
322 Result += "]";
Chris Lattner207b5bc2001-10-29 16:37:48 +0000323 break;
324 }
Chris Lattner9e094c42003-05-14 17:50:47 +0000325 case Type::OpaqueTyID:
John Criswell4ff620a2004-06-01 14:54:08 +0000326 Result += "opaque";
Chris Lattner9e094c42003-05-14 17:50:47 +0000327 break;
Chris Lattner207b5bc2001-10-29 16:37:48 +0000328 default:
John Criswell4ff620a2004-06-01 14:54:08 +0000329 Result += "<unrecognized-type>";
Chris Lattner207b5bc2001-10-29 16:37:48 +0000330 }
331
332 TypeStack.pop_back(); // Remove self from stack...
John Criswell4ff620a2004-06-01 14:54:08 +0000333 return;
Chris Lattner207b5bc2001-10-29 16:37:48 +0000334}
335
336
Misha Brukman9d0802e2004-03-01 19:48:13 +0000337/// printTypeInt - The internal guts of printing out a type that has a
338/// potentially named portion.
339///
Chris Lattner7b13f562003-05-08 02:08:14 +0000340static std::ostream &printTypeInt(std::ostream &Out, const Type *Ty,
341 std::map<const Type *, std::string> &TypeNames) {
Chris Lattner207b5bc2001-10-29 16:37:48 +0000342 // Primitive types always print out their description, regardless of whether
343 // they have been named or not.
344 //
Chris Lattnerdaf2a492003-10-30 00:12:51 +0000345 if (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty))
346 return Out << Ty->getDescription();
Chris Lattner207b5bc2001-10-29 16:37:48 +0000347
348 // Check to see if the type is named.
Chris Lattner7b13f562003-05-08 02:08:14 +0000349 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
Chris Lattner207b5bc2001-10-29 16:37:48 +0000350 if (I != TypeNames.end()) return Out << I->second;
351
352 // Otherwise we have a type that has not been named but is a derived type.
353 // Carefully recurse the type hierarchy to print out any contained symbolic
354 // names.
355 //
Chris Lattner7b13f562003-05-08 02:08:14 +0000356 std::vector<const Type *> TypeStack;
John Criswell4ff620a2004-06-01 14:54:08 +0000357 std::string TypeName;
358 calcTypeName(Ty, TypeStack, TypeNames, TypeName);
Chris Lattner697954c2002-01-20 22:54:45 +0000359 TypeNames.insert(std::make_pair(Ty, TypeName));//Cache type name for later use
John Criswell4ff620a2004-06-01 14:54:08 +0000360 return (Out << TypeName);
Chris Lattner207b5bc2001-10-29 16:37:48 +0000361}
362
Chris Lattnere51e03b2001-10-31 04:33:19 +0000363
Misha Brukman9d0802e2004-03-01 19:48:13 +0000364/// WriteTypeSymbolic - This attempts to write the specified type as a symbolic
365/// type, iff there is an entry in the modules symbol table for the specified
366/// type or one of it's component types. This is slower than a simple x << Type
367///
Chris Lattner31f84992003-11-21 20:23:48 +0000368std::ostream &llvm::WriteTypeSymbolic(std::ostream &Out, const Type *Ty,
369 const Module *M) {
Misha Brukman40c732c2004-06-04 21:11:51 +0000370 Out << ' ';
Chris Lattner207b5bc2001-10-29 16:37:48 +0000371
372 // If they want us to print out a type, attempt to make it symbolic if there
373 // is a symbol table in the module...
Chris Lattner6e6026b2002-11-20 18:36:02 +0000374 if (M) {
Chris Lattner7b13f562003-05-08 02:08:14 +0000375 std::map<const Type *, std::string> TypeNames;
Chris Lattner207b5bc2001-10-29 16:37:48 +0000376 fillTypeNameTable(M, TypeNames);
377
Chris Lattner7b8660d2001-10-29 16:40:32 +0000378 return printTypeInt(Out, Ty, TypeNames);
Chris Lattner207b5bc2001-10-29 16:37:48 +0000379 } else {
Chris Lattner7b8660d2001-10-29 16:40:32 +0000380 return Out << Ty->getDescription();
Chris Lattner207b5bc2001-10-29 16:37:48 +0000381 }
382}
383
Chris Lattner7b13f562003-05-08 02:08:14 +0000384static void WriteConstantInt(std::ostream &Out, const Constant *CV,
385 bool PrintName,
386 std::map<const Type *, std::string> &TypeTable,
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000387 SlotMachine *Machine) {
Chris Lattner66e810b2002-04-18 18:53:13 +0000388 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
389 Out << (CB == ConstantBool::True ? "true" : "false");
390 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV)) {
391 Out << CI->getValue();
392 } else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV)) {
393 Out << CI->getValue();
394 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
395 // We would like to output the FP constant value in exponential notation,
396 // but we cannot do this if doing so will lose precision. Check here to
397 // make sure that we only output it in exponential format if we can parse
398 // the value back and get the same value.
399 //
400 std::string StrVal = ftostr(CFP->getValue());
401
402 // Check to make sure that the stringized number is not some string like
403 // "Inf" or NaN, that atof will accept, but the lexer will not. Check that
404 // the string matches the "[-+]?[0-9]" regex.
405 //
406 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
407 ((StrVal[0] == '-' || StrVal[0] == '+') &&
Brian Gaekeb471a232003-06-17 23:55:35 +0000408 (StrVal[1] >= '0' && StrVal[1] <= '9')))
Chris Lattner66e810b2002-04-18 18:53:13 +0000409 // Reparse stringized version!
410 if (atof(StrVal.c_str()) == CFP->getValue()) {
411 Out << StrVal; return;
412 }
413
414 // Otherwise we could not reparse it to exactly the same value, so we must
415 // output the string in hexadecimal format!
416 //
417 // Behave nicely in the face of C TBAA rules... see:
418 // http://www.nullstone.com/htmls/category/aliastyp.htm
419 //
420 double Val = CFP->getValue();
421 char *Ptr = (char*)&Val;
422 assert(sizeof(double) == sizeof(uint64_t) && sizeof(double) == 8 &&
423 "assuming that double is 64 bits!");
424 Out << "0x" << utohexstr(*(uint64_t*)Ptr);
425
Chris Lattnerde512b52004-02-15 05:55:15 +0000426 } else if (isa<ConstantAggregateZero>(CV)) {
427 Out << "zeroinitializer";
Chris Lattner66e810b2002-04-18 18:53:13 +0000428 } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
429 // As a special case, print the array as a string if it is an array of
430 // ubytes or an array of sbytes with positive values.
431 //
432 const Type *ETy = CA->getType()->getElementType();
433 bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
434
435 if (ETy == Type::SByteTy)
436 for (unsigned i = 0; i < CA->getNumOperands(); ++i)
437 if (cast<ConstantSInt>(CA->getOperand(i))->getValue() < 0) {
438 isString = false;
439 break;
440 }
441
442 if (isString) {
443 Out << "c\"";
444 for (unsigned i = 0; i < CA->getNumOperands(); ++i) {
Chris Lattner54e3e8f2004-06-04 23:53:20 +0000445 unsigned char C =
446 (unsigned char)cast<ConstantInt>(CA->getOperand(i))->getRawValue();
Chris Lattner66e810b2002-04-18 18:53:13 +0000447
Chris Lattnerfc944462002-07-31 23:56:44 +0000448 if (isprint(C) && C != '"' && C != '\\') {
Chris Lattner66e810b2002-04-18 18:53:13 +0000449 Out << C;
450 } else {
451 Out << '\\'
452 << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'))
453 << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
454 }
455 }
456 Out << "\"";
457
458 } else { // Cannot output in string format...
Misha Brukman40c732c2004-06-04 21:11:51 +0000459 Out << '[';
Chris Lattner7a716ad2002-04-16 21:36:08 +0000460 if (CA->getNumOperands()) {
Misha Brukman40c732c2004-06-04 21:11:51 +0000461 Out << ' ';
Chris Lattner66e810b2002-04-18 18:53:13 +0000462 printTypeInt(Out, ETy, TypeTable);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000463 WriteAsOperandInternal(Out, CA->getOperand(0),
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000464 PrintName, TypeTable, Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000465 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
466 Out << ", ";
Chris Lattner66e810b2002-04-18 18:53:13 +0000467 printTypeInt(Out, ETy, TypeTable);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000468 WriteAsOperandInternal(Out, CA->getOperand(i), PrintName,
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000469 TypeTable, Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000470 }
471 }
472 Out << " ]";
473 }
474 } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Misha Brukman40c732c2004-06-04 21:11:51 +0000475 Out << '{';
Chris Lattner7a716ad2002-04-16 21:36:08 +0000476 if (CS->getNumOperands()) {
Misha Brukman40c732c2004-06-04 21:11:51 +0000477 Out << ' ';
Chris Lattner7a716ad2002-04-16 21:36:08 +0000478 printTypeInt(Out, CS->getOperand(0)->getType(), TypeTable);
479
480 WriteAsOperandInternal(Out, CS->getOperand(0),
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000481 PrintName, TypeTable, Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000482
483 for (unsigned i = 1; i < CS->getNumOperands(); i++) {
484 Out << ", ";
485 printTypeInt(Out, CS->getOperand(i)->getType(), TypeTable);
486
487 WriteAsOperandInternal(Out, CS->getOperand(i),
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000488 PrintName, TypeTable, Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000489 }
490 }
491
492 Out << " }";
493 } else if (isa<ConstantPointerNull>(CV)) {
494 Out << "null";
495
Chris Lattner7e708292002-06-25 16:13:24 +0000496 } else if (const ConstantPointerRef *PR = dyn_cast<ConstantPointerRef>(CV)) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000497 WriteAsOperandInternal(Out, PR->getValue(), true, TypeTable, Machine);
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000498
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000499 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Chris Lattner38d87732003-03-06 23:23:32 +0000500 Out << CE->getOpcodeName() << " (";
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000501
502 for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
503 printTypeInt(Out, (*OI)->getType(), TypeTable);
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000504 WriteAsOperandInternal(Out, *OI, PrintName, TypeTable, Machine);
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000505 if (OI+1 != CE->op_end())
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000506 Out << ", ";
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000507 }
508
Chris Lattner1c93e5b2002-08-16 21:17:11 +0000509 if (CE->getOpcode() == Instruction::Cast) {
Chris Lattner95586b82002-08-15 19:37:43 +0000510 Out << " to ";
511 printTypeInt(Out, CE->getType(), TypeTable);
512 }
Misha Brukman40c732c2004-06-04 21:11:51 +0000513 Out << ')';
Chris Lattner95586b82002-08-15 19:37:43 +0000514
Chris Lattner7a716ad2002-04-16 21:36:08 +0000515 } else {
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000516 Out << "<placeholder or erroneous Constant>";
Chris Lattner7a716ad2002-04-16 21:36:08 +0000517 }
518}
519
520
Misha Brukmanab5c6002004-03-02 00:22:19 +0000521/// WriteAsOperand - Write the name of the specified value out to the specified
522/// ostream. This can be useful when you just want to print int %reg126, not
523/// the whole instruction that generated it.
524///
Chris Lattner7b13f562003-05-08 02:08:14 +0000525static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
526 bool PrintName,
527 std::map<const Type*, std::string> &TypeTable,
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000528 SlotMachine *Machine) {
Misha Brukman40c732c2004-06-04 21:11:51 +0000529 Out << ' ';
Chris Lattner7a716ad2002-04-16 21:36:08 +0000530 if (PrintName && V->hasName()) {
Chris Lattner24b8a5d2003-08-22 05:40:38 +0000531 Out << getLLVMName(V->getName());
Chris Lattner7a716ad2002-04-16 21:36:08 +0000532 } else {
Chris Lattner949a3622003-07-23 15:30:06 +0000533 if (const Constant *CV = dyn_cast<Constant>(V)) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000534 WriteConstantInt(Out, CV, PrintName, TypeTable, Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000535 } else {
536 int Slot;
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000537 if (Machine) {
Reid Spencerb03de0c2004-05-26 21:56:09 +0000538 Slot = Machine->getSlot(V);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000539 } else {
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000540 Machine = createSlotMachine(V);
Chris Lattner69566452004-06-09 19:41:19 +0000541 if (Machine == 0)
542 Slot = Machine->getSlot(V);
543 else
544 Slot = -1;
Reid Spencerb03de0c2004-05-26 21:56:09 +0000545 delete Machine;
Chris Lattner7a716ad2002-04-16 21:36:08 +0000546 }
Chris Lattner69566452004-06-09 19:41:19 +0000547 if (Slot != -1)
548 Out << '%' << Slot;
549 else
550 Out << "<badref>";
Chris Lattner7a716ad2002-04-16 21:36:08 +0000551 }
552 }
553}
554
Misha Brukman9d0802e2004-03-01 19:48:13 +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///
Chris Lattner31f84992003-11-21 20:23:48 +0000559std::ostream &llvm::WriteAsOperand(std::ostream &Out, const Value *V,
Misha Brukman9d0802e2004-03-01 19:48:13 +0000560 bool PrintType, bool PrintName,
561 const Module *Context) {
Chris Lattner7b13f562003-05-08 02:08:14 +0000562 std::map<const Type *, std::string> TypeNames;
Chris Lattner607dc682002-07-10 16:48:17 +0000563 if (Context == 0) Context = getModuleFromVal(V);
Chris Lattner207b5bc2001-10-29 16:37:48 +0000564
Chris Lattner6e6026b2002-11-20 18:36:02 +0000565 if (Context)
Chris Lattner607dc682002-07-10 16:48:17 +0000566 fillTypeNameTable(Context, TypeNames);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000567
568 if (PrintType)
569 printTypeInt(Out, V->getType(), TypeNames);
570
Chris Lattner607dc682002-07-10 16:48:17 +0000571 WriteAsOperandInternal(Out, V, PrintName, TypeNames, 0);
Chris Lattner622f7402001-07-20 19:15:21 +0000572 return Out;
573}
574
Reid Spencer0e25e1c2004-07-04 11:50:43 +0000575/// WriteAsOperandInternal - Write the name of the specified value out to
576/// the specified ostream. This can be useful when you just want to print
577/// int %reg126, not the whole instruction that generated it.
578///
579static void WriteAsOperandInternal(std::ostream &Out, const Type *T,
580 bool PrintName,
581 std::map<const Type*, std::string> &TypeTable,
582 SlotMachine *Machine) {
583 Out << ' ';
584 int Slot;
585 if (Machine) {
586 Slot = Machine->getSlot(T);
587 if (Slot != -1)
588 Out << '%' << Slot;
589 else
590 Out << "<badref>";
591 } else {
592 Out << T->getDescription();
593 }
594}
595
596/// WriteAsOperand - Write the name of the specified value out to the specified
597/// ostream. This can be useful when you just want to print int %reg126, not
598/// the whole instruction that generated it.
599///
600std::ostream &llvm::WriteAsOperand(std::ostream &Out, const Type *Ty,
601 bool PrintType, bool PrintName,
602 const Module *Context) {
603 std::map<const Type *, std::string> TypeNames;
604 assert(Context != 0 && "Can't write types as operand without module context");
605
606 fillTypeNameTable(Context, TypeNames);
607
608 // if (PrintType)
609 // printTypeInt(Out, V->getType(), TypeNames);
610
611 printTypeInt(Out, Ty, TypeNames);
612
613 WriteAsOperandInternal(Out, Ty, PrintName, TypeNames, 0);
614 return Out;
615}
616
Chris Lattner31f84992003-11-21 20:23:48 +0000617namespace llvm {
Chris Lattnerd8c2e422001-07-12 23:35:26 +0000618
Chris Lattner007377f2001-09-07 16:36:04 +0000619class AssemblyWriter {
Misha Brukman0313e0b2004-06-21 21:53:56 +0000620 std::ostream &Out;
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000621 SlotMachine &Machine;
Chris Lattnerc1824992001-10-29 16:05:51 +0000622 const Module *TheModule;
Chris Lattner7b13f562003-05-08 02:08:14 +0000623 std::map<const Type *, std::string> TypeNames;
Chris Lattner95e5a2c2003-10-30 23:41:03 +0000624 AssemblyAnnotationWriter *AnnotationWriter;
Chris Lattner00950542001-06-06 20:29:01 +0000625public:
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000626 inline AssemblyWriter(std::ostream &o, SlotMachine &Mac, const Module *M,
Chris Lattner95e5a2c2003-10-30 23:41:03 +0000627 AssemblyAnnotationWriter *AAW)
Misha Brukman0313e0b2004-06-21 21:53:56 +0000628 : Out(o), Machine(Mac), TheModule(M), AnnotationWriter(AAW) {
Chris Lattnerc1824992001-10-29 16:05:51 +0000629
630 // If the module has a symbol table, take all global types and stuff their
631 // names into the TypeNames map.
632 //
Chris Lattner207b5bc2001-10-29 16:37:48 +0000633 fillTypeNameTable(M, TypeNames);
Chris Lattner00950542001-06-06 20:29:01 +0000634 }
635
Chris Lattnerc1824992001-10-29 16:05:51 +0000636 inline void write(const Module *M) { printModule(M); }
637 inline void write(const GlobalVariable *G) { printGlobal(G); }
Chris Lattner79df7c02002-03-26 18:01:55 +0000638 inline void write(const Function *F) { printFunction(F); }
Chris Lattnerc1824992001-10-29 16:05:51 +0000639 inline void write(const BasicBlock *BB) { printBasicBlock(BB); }
Chris Lattner7e708292002-06-25 16:13:24 +0000640 inline void write(const Instruction *I) { printInstruction(*I); }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000641 inline void write(const Constant *CPV) { printConstant(CPV); }
Chris Lattnerda1fbcc2001-11-07 04:21:57 +0000642 inline void write(const Type *Ty) { printType(Ty); }
Chris Lattner00950542001-06-06 20:29:01 +0000643
Chris Lattner66e810b2002-04-18 18:53:13 +0000644 void writeOperand(const Value *Op, bool PrintType, bool PrintName = true);
645
Misha Brukman5cf1acf2004-04-28 15:31:21 +0000646 const Module* getModule() { return TheModule; }
647
Chris Lattner00950542001-06-06 20:29:01 +0000648private :
Chris Lattnerc1824992001-10-29 16:05:51 +0000649 void printModule(const Module *M);
650 void printSymbolTable(const SymbolTable &ST);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000651 void printConstant(const Constant *CPV);
Chris Lattnerc1824992001-10-29 16:05:51 +0000652 void printGlobal(const GlobalVariable *GV);
Chris Lattner79df7c02002-03-26 18:01:55 +0000653 void printFunction(const Function *F);
Chris Lattner73e21422002-04-09 19:48:49 +0000654 void printArgument(const Argument *FA);
Chris Lattnerc1824992001-10-29 16:05:51 +0000655 void printBasicBlock(const BasicBlock *BB);
Chris Lattner7e708292002-06-25 16:13:24 +0000656 void printInstruction(const Instruction &I);
Chris Lattner2761e9f2002-04-13 20:53:41 +0000657
658 // printType - Go to extreme measures to attempt to print out a short,
659 // symbolic version of a type name.
660 //
Chris Lattner7b13f562003-05-08 02:08:14 +0000661 std::ostream &printType(const Type *Ty) {
Misha Brukman0313e0b2004-06-21 21:53:56 +0000662 return printTypeInt(Out, Ty, TypeNames);
Chris Lattner2761e9f2002-04-13 20:53:41 +0000663 }
664
665 // printTypeAtLeastOneLevel - Print out one level of the possibly complex type
666 // without considering any symbolic types that we may have equal to it.
667 //
Chris Lattner7b13f562003-05-08 02:08:14 +0000668 std::ostream &printTypeAtLeastOneLevel(const Type *Ty);
Chris Lattnerc1824992001-10-29 16:05:51 +0000669
Chris Lattnere02fa852001-10-13 06:42:36 +0000670 // printInfoComment - Print a little comment after the instruction indicating
671 // which slot it occupies.
Chris Lattner7e708292002-06-25 16:13:24 +0000672 void printInfoComment(const Value &V);
Chris Lattner00950542001-06-06 20:29:01 +0000673};
Reid Spencer73b74952004-05-27 22:04:46 +0000674} // end of llvm namespace
Chris Lattner00950542001-06-06 20:29:01 +0000675
Misha Brukmanab5c6002004-03-02 00:22:19 +0000676/// printTypeAtLeastOneLevel - Print out one level of the possibly complex type
677/// without considering any symbolic types that we may have equal to it.
678///
Chris Lattner7b13f562003-05-08 02:08:14 +0000679std::ostream &AssemblyWriter::printTypeAtLeastOneLevel(const Type *Ty) {
Chris Lattner7e708292002-06-25 16:13:24 +0000680 if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
Chris Lattner2761e9f2002-04-13 20:53:41 +0000681 printType(FTy->getReturnType()) << " (";
Chris Lattnerd5d89962004-02-09 04:14:01 +0000682 for (FunctionType::param_iterator I = FTy->param_begin(),
683 E = FTy->param_end(); I != E; ++I) {
684 if (I != FTy->param_begin())
Misha Brukman0313e0b2004-06-21 21:53:56 +0000685 Out << ", ";
Chris Lattner7a716ad2002-04-16 21:36:08 +0000686 printType(*I);
Chris Lattner2761e9f2002-04-13 20:53:41 +0000687 }
688 if (FTy->isVarArg()) {
Misha Brukman0313e0b2004-06-21 21:53:56 +0000689 if (FTy->getNumParams()) Out << ", ";
690 Out << "...";
Chris Lattner2761e9f2002-04-13 20:53:41 +0000691 }
Misha Brukman0313e0b2004-06-21 21:53:56 +0000692 Out << ')';
Chris Lattner7e708292002-06-25 16:13:24 +0000693 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
Misha Brukman0313e0b2004-06-21 21:53:56 +0000694 Out << "{ ";
Chris Lattnerd21cd802004-02-09 04:37:31 +0000695 for (StructType::element_iterator I = STy->element_begin(),
696 E = STy->element_end(); I != E; ++I) {
697 if (I != STy->element_begin())
Misha Brukman0313e0b2004-06-21 21:53:56 +0000698 Out << ", ";
Chris Lattner2761e9f2002-04-13 20:53:41 +0000699 printType(*I);
700 }
Misha Brukman0313e0b2004-06-21 21:53:56 +0000701 Out << " }";
Chris Lattner7e708292002-06-25 16:13:24 +0000702 } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
Misha Brukman40c732c2004-06-04 21:11:51 +0000703 printType(PTy->getElementType()) << '*';
Chris Lattner7e708292002-06-25 16:13:24 +0000704 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Misha Brukman0313e0b2004-06-21 21:53:56 +0000705 Out << '[' << ATy->getNumElements() << " x ";
Misha Brukman40c732c2004-06-04 21:11:51 +0000706 printType(ATy->getElementType()) << ']';
Chris Lattner7e708292002-06-25 16:13:24 +0000707 } else if (const OpaqueType *OTy = dyn_cast<OpaqueType>(Ty)) {
Misha Brukman0313e0b2004-06-21 21:53:56 +0000708 Out << "opaque";
Chris Lattner2761e9f2002-04-13 20:53:41 +0000709 } else {
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000710 if (!Ty->isPrimitiveType())
Misha Brukman0313e0b2004-06-21 21:53:56 +0000711 Out << "<unknown derived type>";
Chris Lattner2761e9f2002-04-13 20:53:41 +0000712 printType(Ty);
713 }
Misha Brukman0313e0b2004-06-21 21:53:56 +0000714 return Out;
Chris Lattner2761e9f2002-04-13 20:53:41 +0000715}
716
717
Chris Lattner007377f2001-09-07 16:36:04 +0000718void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType,
Reid Spencerb03de0c2004-05-26 21:56:09 +0000719 bool PrintName) {
Misha Brukman0313e0b2004-06-21 21:53:56 +0000720 if (PrintType) { Out << ' '; printType(Operand->getType()); }
721 WriteAsOperandInternal(Out, Operand, PrintName, TypeNames, &Machine);
Chris Lattner00950542001-06-06 20:29:01 +0000722}
723
Chris Lattner00950542001-06-06 20:29:01 +0000724
Chris Lattnerc1824992001-10-29 16:05:51 +0000725void AssemblyWriter::printModule(const Module *M) {
Chris Lattnereb5d3a12003-08-24 13:48:48 +0000726 switch (M->getEndianness()) {
Misha Brukman0313e0b2004-06-21 21:53:56 +0000727 case Module::LittleEndian: Out << "target endian = little\n"; break;
728 case Module::BigEndian: Out << "target endian = big\n"; break;
Chris Lattnereb5d3a12003-08-24 13:48:48 +0000729 case Module::AnyEndianness: break;
730 }
731 switch (M->getPointerSize()) {
Misha Brukman0313e0b2004-06-21 21:53:56 +0000732 case Module::Pointer32: Out << "target pointersize = 32\n"; break;
733 case Module::Pointer64: Out << "target pointersize = 64\n"; break;
Chris Lattnereb5d3a12003-08-24 13:48:48 +0000734 case Module::AnyPointerSize: break;
735 }
736
Chris Lattner007377f2001-09-07 16:36:04 +0000737 // Loop over the symbol table, emitting all named constants...
Chris Lattner6e6026b2002-11-20 18:36:02 +0000738 printSymbolTable(M->getSymbolTable());
Chris Lattner70cc3392001-09-10 07:58:01 +0000739
Chris Lattner7e708292002-06-25 16:13:24 +0000740 for (Module::const_giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
741 printGlobal(I);
Chris Lattner007377f2001-09-07 16:36:04 +0000742
Misha Brukman0313e0b2004-06-21 21:53:56 +0000743 Out << "\nimplementation ; Functions:\n";
Vikram S. Adve5efa3cc2001-09-18 12:48:16 +0000744
Chris Lattnerb5794002002-04-07 22:49:37 +0000745 // Output all of the functions...
Chris Lattner7e708292002-06-25 16:13:24 +0000746 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
747 printFunction(I);
Chris Lattner007377f2001-09-07 16:36:04 +0000748}
749
Chris Lattnerc1824992001-10-29 16:05:51 +0000750void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Misha Brukman0313e0b2004-06-21 21:53:56 +0000751 if (GV->hasName()) Out << getLLVMName(GV->getName()) << " = ";
Chris Lattnerd70684f2001-09-18 04:01:05 +0000752
Chris Lattner4ad02e72003-04-16 20:28:45 +0000753 if (!GV->hasInitializer())
Misha Brukman0313e0b2004-06-21 21:53:56 +0000754 Out << "external ";
Chris Lattner4ad02e72003-04-16 20:28:45 +0000755 else
756 switch (GV->getLinkage()) {
Misha Brukman0313e0b2004-06-21 21:53:56 +0000757 case GlobalValue::InternalLinkage: Out << "internal "; break;
758 case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
759 case GlobalValue::WeakLinkage: Out << "weak "; break;
760 case GlobalValue::AppendingLinkage: Out << "appending "; break;
Chris Lattner4ad02e72003-04-16 20:28:45 +0000761 case GlobalValue::ExternalLinkage: break;
762 }
Chris Lattnerd70684f2001-09-18 04:01:05 +0000763
Misha Brukman0313e0b2004-06-21 21:53:56 +0000764 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattner7a176752001-12-04 00:03:30 +0000765 printType(GV->getType()->getElementType());
Chris Lattnerd70684f2001-09-18 04:01:05 +0000766
767 if (GV->hasInitializer())
768 writeOperand(GV->getInitializer(), false, false);
769
Chris Lattner7e708292002-06-25 16:13:24 +0000770 printInfoComment(*GV);
Misha Brukman0313e0b2004-06-21 21:53:56 +0000771 Out << "\n";
Chris Lattner70cc3392001-09-10 07:58:01 +0000772}
773
Chris Lattner007377f2001-09-07 16:36:04 +0000774
Reid Spencer9231ac82004-05-25 08:53:40 +0000775// printSymbolTable - Run through symbol table looking for constants
776// and types. Emit their declarations.
Chris Lattnerc1824992001-10-29 16:05:51 +0000777void AssemblyWriter::printSymbolTable(const SymbolTable &ST) {
Reid Spencer9231ac82004-05-25 08:53:40 +0000778
779 // Print the types.
780 for (SymbolTable::type_const_iterator TI = ST.type_begin();
781 TI != ST.type_end(); ++TI ) {
Misha Brukman0313e0b2004-06-21 21:53:56 +0000782 Out << "\t" << getLLVMName(TI->first) << " = type ";
Reid Spencer9231ac82004-05-25 08:53:40 +0000783
784 // Make sure we print out at least one level of the type structure, so
785 // that we do not get %FILE = type %FILE
786 //
787 printTypeAtLeastOneLevel(TI->second) << "\n";
788 }
Chris Lattner007377f2001-09-07 16:36:04 +0000789
Reid Spencer9231ac82004-05-25 08:53:40 +0000790 // Print the constants, in type plane order.
791 for (SymbolTable::plane_const_iterator PI = ST.plane_begin();
792 PI != ST.plane_end(); ++PI ) {
793 SymbolTable::value_const_iterator VI = ST.value_begin(PI->first);
794 SymbolTable::value_const_iterator VE = ST.value_end(PI->first);
795
796 for (; VI != VE; ++VI) {
797 const Value *V = VI->second;
Chris Lattner949a3622003-07-23 15:30:06 +0000798 if (const Constant *CPV = dyn_cast<Constant>(V)) {
Reid Spencerb03de0c2004-05-26 21:56:09 +0000799 printConstant(CPV);
Chris Lattner007377f2001-09-07 16:36:04 +0000800 }
801 }
Chris Lattner739a56d2001-07-15 06:35:59 +0000802 }
Chris Lattner00950542001-06-06 20:29:01 +0000803}
804
805
Misha Brukmanab5c6002004-03-02 00:22:19 +0000806/// printConstant - Print out a constant pool entry...
807///
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000808void AssemblyWriter::printConstant(const Constant *CPV) {
Chris Lattner007377f2001-09-07 16:36:04 +0000809 // Don't print out unnamed constants, they will be inlined
810 if (!CPV->hasName()) return;
Chris Lattner00950542001-06-06 20:29:01 +0000811
Chris Lattner1333ed52001-07-26 16:29:38 +0000812 // Print out name...
Misha Brukman0313e0b2004-06-21 21:53:56 +0000813 Out << "\t" << getLLVMName(CPV->getName()) << " =";
Chris Lattner00950542001-06-06 20:29:01 +0000814
815 // Write the value out now...
Chris Lattner7a716ad2002-04-16 21:36:08 +0000816 writeOperand(CPV, true, false);
Chris Lattner00950542001-06-06 20:29:01 +0000817
Chris Lattner7e708292002-06-25 16:13:24 +0000818 printInfoComment(*CPV);
Misha Brukman0313e0b2004-06-21 21:53:56 +0000819 Out << "\n";
Chris Lattner00950542001-06-06 20:29:01 +0000820}
821
Misha Brukmanab5c6002004-03-02 00:22:19 +0000822/// printFunction - Print all aspects of a function.
823///
Chris Lattner7e708292002-06-25 16:13:24 +0000824void AssemblyWriter::printFunction(const Function *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000825 // Print out the return type and name...
Misha Brukman0313e0b2004-06-21 21:53:56 +0000826 Out << "\n";
Chris Lattner4ad02e72003-04-16 20:28:45 +0000827
Misha Brukman0313e0b2004-06-21 21:53:56 +0000828 if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out);
Chris Lattner95e5a2c2003-10-30 23:41:03 +0000829
Chris Lattner4ad02e72003-04-16 20:28:45 +0000830 if (F->isExternal())
Misha Brukman0313e0b2004-06-21 21:53:56 +0000831 Out << "declare ";
Chris Lattner4ad02e72003-04-16 20:28:45 +0000832 else
833 switch (F->getLinkage()) {
Misha Brukman0313e0b2004-06-21 21:53:56 +0000834 case GlobalValue::InternalLinkage: Out << "internal "; break;
835 case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
836 case GlobalValue::WeakLinkage: Out << "weak "; break;
837 case GlobalValue::AppendingLinkage: Out << "appending "; break;
Chris Lattner4ad02e72003-04-16 20:28:45 +0000838 case GlobalValue::ExternalLinkage: break;
839 }
840
Misha Brukman40c732c2004-06-04 21:11:51 +0000841 printType(F->getReturnType()) << ' ';
Chris Lattner4d45bd02003-10-18 05:57:43 +0000842 if (!F->getName().empty())
Misha Brukman0313e0b2004-06-21 21:53:56 +0000843 Out << getLLVMName(F->getName());
Chris Lattner4d45bd02003-10-18 05:57:43 +0000844 else
Misha Brukman0313e0b2004-06-21 21:53:56 +0000845 Out << "\"\"";
846 Out << '(';
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000847 Machine.incorporateFunction(F);
Chris Lattner007377f2001-09-07 16:36:04 +0000848
Chris Lattnerc1824992001-10-29 16:05:51 +0000849 // Loop over the arguments, printing them...
Chris Lattner7e708292002-06-25 16:13:24 +0000850 const FunctionType *FT = F->getFunctionType();
Chris Lattner007377f2001-09-07 16:36:04 +0000851
Chris Lattner69da5cf2002-10-13 20:57:00 +0000852 for(Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I)
853 printArgument(I);
Chris Lattner007377f2001-09-07 16:36:04 +0000854
855 // Finish printing arguments...
Chris Lattner7e708292002-06-25 16:13:24 +0000856 if (FT->isVarArg()) {
Misha Brukman0313e0b2004-06-21 21:53:56 +0000857 if (FT->getNumParams()) Out << ", ";
858 Out << "..."; // Output varargs portion of signature!
Chris Lattner007377f2001-09-07 16:36:04 +0000859 }
Misha Brukman0313e0b2004-06-21 21:53:56 +0000860 Out << ')';
Chris Lattner007377f2001-09-07 16:36:04 +0000861
Chris Lattner7e708292002-06-25 16:13:24 +0000862 if (F->isExternal()) {
Misha Brukman0313e0b2004-06-21 21:53:56 +0000863 Out << "\n";
Chris Lattner03e2acb2002-05-06 03:00:40 +0000864 } else {
Misha Brukman0313e0b2004-06-21 21:53:56 +0000865 Out << " {";
Chris Lattner007377f2001-09-07 16:36:04 +0000866
Chris Lattnerb5794002002-04-07 22:49:37 +0000867 // Output all of its basic blocks... for the function
Chris Lattner7e708292002-06-25 16:13:24 +0000868 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
869 printBasicBlock(I);
Chris Lattner007377f2001-09-07 16:36:04 +0000870
Misha Brukman0313e0b2004-06-21 21:53:56 +0000871 Out << "}\n";
Chris Lattner007377f2001-09-07 16:36:04 +0000872 }
873
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000874 Machine.purgeFunction();
Chris Lattner00950542001-06-06 20:29:01 +0000875}
876
Misha Brukmanab5c6002004-03-02 00:22:19 +0000877/// printArgument - This member is called for every argument that is passed into
878/// the function. Simply print it out
879///
Chris Lattner73e21422002-04-09 19:48:49 +0000880void AssemblyWriter::printArgument(const Argument *Arg) {
Chris Lattner00950542001-06-06 20:29:01 +0000881 // Insert commas as we go... the first arg doesn't get a comma
Misha Brukman0313e0b2004-06-21 21:53:56 +0000882 if (Arg != &Arg->getParent()->afront()) Out << ", ";
Chris Lattner00950542001-06-06 20:29:01 +0000883
884 // Output type...
Chris Lattnerc1824992001-10-29 16:05:51 +0000885 printType(Arg->getType());
Chris Lattner00950542001-06-06 20:29:01 +0000886
887 // Output name, if available...
888 if (Arg->hasName())
Misha Brukman0313e0b2004-06-21 21:53:56 +0000889 Out << ' ' << getLLVMName(Arg->getName());
Chris Lattner00950542001-06-06 20:29:01 +0000890}
891
Misha Brukmanab5c6002004-03-02 00:22:19 +0000892/// printBasicBlock - This member is called for each basic block in a method.
893///
Chris Lattnerc1824992001-10-29 16:05:51 +0000894void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Chris Lattner00950542001-06-06 20:29:01 +0000895 if (BB->hasName()) { // Print out the label if it exists...
Misha Brukman0313e0b2004-06-21 21:53:56 +0000896 Out << "\n" << BB->getName() << ':';
Chris Lattnerafc38682002-05-14 16:02:05 +0000897 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Misha Brukman0313e0b2004-06-21 21:53:56 +0000898 Out << "\n; <label>:";
Chris Lattner69566452004-06-09 19:41:19 +0000899 int Slot = Machine.getSlot(BB);
900 if (Slot != -1)
Misha Brukman0313e0b2004-06-21 21:53:56 +0000901 Out << Slot;
Chris Lattner69566452004-06-09 19:41:19 +0000902 else
Misha Brukman0313e0b2004-06-21 21:53:56 +0000903 Out << "<badref>";
Chris Lattner061269b2002-10-02 19:38:55 +0000904 }
Chris Lattner4e4d8622003-11-20 00:09:43 +0000905
906 if (BB->getParent() == 0)
Misha Brukman0313e0b2004-06-21 21:53:56 +0000907 Out << "\t\t; Error: Block without parent!";
Chris Lattner4e4d8622003-11-20 00:09:43 +0000908 else {
909 if (BB != &BB->getParent()->front()) { // Not the entry block?
910 // Output predecessors for the block...
Misha Brukman0313e0b2004-06-21 21:53:56 +0000911 Out << "\t\t;";
Chris Lattner4e4d8622003-11-20 00:09:43 +0000912 pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
913
914 if (PI == PE) {
Misha Brukman0313e0b2004-06-21 21:53:56 +0000915 Out << " No predecessors!";
Chris Lattner4e4d8622003-11-20 00:09:43 +0000916 } else {
Misha Brukman0313e0b2004-06-21 21:53:56 +0000917 Out << " preds =";
Chris Lattner40efcec2003-11-16 22:59:57 +0000918 writeOperand(*PI, false, true);
Chris Lattner4e4d8622003-11-20 00:09:43 +0000919 for (++PI; PI != PE; ++PI) {
Misha Brukman0313e0b2004-06-21 21:53:56 +0000920 Out << ',';
Chris Lattner4e4d8622003-11-20 00:09:43 +0000921 writeOperand(*PI, false, true);
922 }
Chris Lattner40efcec2003-11-16 22:59:57 +0000923 }
Chris Lattner061269b2002-10-02 19:38:55 +0000924 }
Chris Lattner00950542001-06-06 20:29:01 +0000925 }
Chris Lattnerafc38682002-05-14 16:02:05 +0000926
Misha Brukman0313e0b2004-06-21 21:53:56 +0000927 Out << "\n";
Chris Lattner00950542001-06-06 20:29:01 +0000928
Misha Brukman0313e0b2004-06-21 21:53:56 +0000929 if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out);
Chris Lattner95e5a2c2003-10-30 23:41:03 +0000930
Chris Lattner007377f2001-09-07 16:36:04 +0000931 // Output all of the instructions in the basic block...
Chris Lattner7e708292002-06-25 16:13:24 +0000932 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
933 printInstruction(*I);
Chris Lattner9f717ef2004-03-08 18:51:45 +0000934
Misha Brukman0313e0b2004-06-21 21:53:56 +0000935 if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000936}
937
Chris Lattnere02fa852001-10-13 06:42:36 +0000938
Misha Brukmanab5c6002004-03-02 00:22:19 +0000939/// printInfoComment - Print a little comment after the instruction indicating
940/// which slot it occupies.
941///
Chris Lattner7e708292002-06-25 16:13:24 +0000942void AssemblyWriter::printInfoComment(const Value &V) {
943 if (V.getType() != Type::VoidTy) {
Misha Brukman0313e0b2004-06-21 21:53:56 +0000944 Out << "\t\t; <";
Misha Brukman40c732c2004-06-04 21:11:51 +0000945 printType(V.getType()) << '>';
Chris Lattnere02fa852001-10-13 06:42:36 +0000946
Chris Lattner7e708292002-06-25 16:13:24 +0000947 if (!V.hasName()) {
Chris Lattner69566452004-06-09 19:41:19 +0000948 int SlotNum = Machine.getSlot(&V);
949 if (SlotNum == -1)
Misha Brukman0313e0b2004-06-21 21:53:56 +0000950 Out << ":<badref>";
Reid Spencerfc621e22004-06-09 15:26:53 +0000951 else
Misha Brukman0313e0b2004-06-21 21:53:56 +0000952 Out << ':' << SlotNum; // Print out the def slot taken.
Chris Lattnere02fa852001-10-13 06:42:36 +0000953 }
Misha Brukman0313e0b2004-06-21 21:53:56 +0000954 Out << " [#uses=" << V.use_size() << ']'; // Output # uses
Chris Lattnere02fa852001-10-13 06:42:36 +0000955 }
956}
957
Reid Spencerfc621e22004-06-09 15:26:53 +0000958/// printInstruction - This member is called for each Instruction in a function..
Misha Brukmanab5c6002004-03-02 00:22:19 +0000959///
Chris Lattner7e708292002-06-25 16:13:24 +0000960void AssemblyWriter::printInstruction(const Instruction &I) {
Misha Brukman0313e0b2004-06-21 21:53:56 +0000961 if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out);
Chris Lattner95e5a2c2003-10-30 23:41:03 +0000962
Misha Brukman0313e0b2004-06-21 21:53:56 +0000963 Out << "\t";
Chris Lattner00950542001-06-06 20:29:01 +0000964
965 // Print out name if it exists...
Chris Lattner7e708292002-06-25 16:13:24 +0000966 if (I.hasName())
Misha Brukman0313e0b2004-06-21 21:53:56 +0000967 Out << getLLVMName(I.getName()) << " = ";
Chris Lattner00950542001-06-06 20:29:01 +0000968
Chris Lattnere5e475e2003-09-08 17:45:59 +0000969 // If this is a volatile load or store, print out the volatile marker
970 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
971 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile()))
Misha Brukman0313e0b2004-06-21 21:53:56 +0000972 Out << "volatile ";
Chris Lattnere5e475e2003-09-08 17:45:59 +0000973
Chris Lattner00950542001-06-06 20:29:01 +0000974 // Print out the opcode...
Misha Brukman0313e0b2004-06-21 21:53:56 +0000975 Out << I.getOpcodeName();
Chris Lattner00950542001-06-06 20:29:01 +0000976
977 // Print out the type of the operands...
Chris Lattner7e708292002-06-25 16:13:24 +0000978 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
Chris Lattner00950542001-06-06 20:29:01 +0000979
980 // Special case conditional branches to swizzle the condition out to the front
Chris Lattner7e708292002-06-25 16:13:24 +0000981 if (isa<BranchInst>(I) && I.getNumOperands() > 1) {
982 writeOperand(I.getOperand(2), true);
Misha Brukman0313e0b2004-06-21 21:53:56 +0000983 Out << ',';
Chris Lattner00950542001-06-06 20:29:01 +0000984 writeOperand(Operand, true);
Misha Brukman0313e0b2004-06-21 21:53:56 +0000985 Out << ',';
Chris Lattner7e708292002-06-25 16:13:24 +0000986 writeOperand(I.getOperand(1), true);
Chris Lattner00950542001-06-06 20:29:01 +0000987
Chris Lattner94dc1f22002-04-13 18:34:38 +0000988 } else if (isa<SwitchInst>(I)) {
Chris Lattner00950542001-06-06 20:29:01 +0000989 // Special case switch statement to get formatting nice and correct...
Misha Brukman0313e0b2004-06-21 21:53:56 +0000990 writeOperand(Operand , true); Out << ',';
991 writeOperand(I.getOperand(1), true); Out << " [";
Chris Lattner00950542001-06-06 20:29:01 +0000992
Chris Lattner7e708292002-06-25 16:13:24 +0000993 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukman0313e0b2004-06-21 21:53:56 +0000994 Out << "\n\t\t";
995 writeOperand(I.getOperand(op ), true); Out << ',';
Chris Lattner7e708292002-06-25 16:13:24 +0000996 writeOperand(I.getOperand(op+1), true);
Chris Lattner00950542001-06-06 20:29:01 +0000997 }
Misha Brukman0313e0b2004-06-21 21:53:56 +0000998 Out << "\n\t]";
Chris Lattnerb00c5822001-10-02 03:41:24 +0000999 } else if (isa<PHINode>(I)) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001000 Out << ' ';
Chris Lattner7e708292002-06-25 16:13:24 +00001001 printType(I.getType());
Misha Brukman0313e0b2004-06-21 21:53:56 +00001002 Out << ' ';
Chris Lattner00950542001-06-06 20:29:01 +00001003
Chris Lattner7e708292002-06-25 16:13:24 +00001004 for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001005 if (op) Out << ", ";
1006 Out << '[';
1007 writeOperand(I.getOperand(op ), false); Out << ',';
1008 writeOperand(I.getOperand(op+1), false); Out << " ]";
Chris Lattnerc24d2082001-06-11 15:04:20 +00001009 }
Chris Lattnere02fa852001-10-13 06:42:36 +00001010 } else if (isa<ReturnInst>(I) && !Operand) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001011 Out << " void";
Chris Lattnere02fa852001-10-13 06:42:36 +00001012 } else if (isa<CallInst>(I)) {
Chris Lattner7a012292003-08-05 15:34:45 +00001013 const PointerType *PTy = cast<PointerType>(Operand->getType());
1014 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1015 const Type *RetTy = FTy->getReturnType();
Chris Lattner268de042001-11-06 21:28:12 +00001016
Chris Lattner7a012292003-08-05 15:34:45 +00001017 // If possible, print out the short form of the call instruction. We can
Chris Lattnerb5794002002-04-07 22:49:37 +00001018 // only do this if the first argument is a pointer to a nonvararg function,
Chris Lattner7a012292003-08-05 15:34:45 +00001019 // and if the return type is not a pointer to a function.
Chris Lattner268de042001-11-06 21:28:12 +00001020 //
Chris Lattner7a012292003-08-05 15:34:45 +00001021 if (!FTy->isVarArg() &&
Chris Lattner94dc1f22002-04-13 18:34:38 +00001022 (!isa<PointerType>(RetTy) ||
Chris Lattnerc1b27182002-07-25 20:58:51 +00001023 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001024 Out << ' '; printType(RetTy);
Chris Lattner268de042001-11-06 21:28:12 +00001025 writeOperand(Operand, false);
1026 } else {
1027 writeOperand(Operand, true);
1028 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00001029 Out << '(';
Chris Lattner7e708292002-06-25 16:13:24 +00001030 if (I.getNumOperands() > 1) writeOperand(I.getOperand(1), true);
1031 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; ++op) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001032 Out << ',';
Chris Lattner7e708292002-06-25 16:13:24 +00001033 writeOperand(I.getOperand(op), true);
Chris Lattner00950542001-06-06 20:29:01 +00001034 }
1035
Misha Brukman0313e0b2004-06-21 21:53:56 +00001036 Out << " )";
Chris Lattner7e708292002-06-25 16:13:24 +00001037 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Chris Lattner7a012292003-08-05 15:34:45 +00001038 const PointerType *PTy = cast<PointerType>(Operand->getType());
1039 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1040 const Type *RetTy = FTy->getReturnType();
1041
1042 // If possible, print out the short form of the invoke instruction. We can
1043 // only do this if the first argument is a pointer to a nonvararg function,
1044 // and if the return type is not a pointer to a function.
1045 //
1046 if (!FTy->isVarArg() &&
1047 (!isa<PointerType>(RetTy) ||
1048 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001049 Out << ' '; printType(RetTy);
Chris Lattner7a012292003-08-05 15:34:45 +00001050 writeOperand(Operand, false);
1051 } else {
1052 writeOperand(Operand, true);
1053 }
1054
Misha Brukman0313e0b2004-06-21 21:53:56 +00001055 Out << '(';
Chris Lattner7e708292002-06-25 16:13:24 +00001056 if (I.getNumOperands() > 3) writeOperand(I.getOperand(3), true);
1057 for (unsigned op = 4, Eop = I.getNumOperands(); op < Eop; ++op) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001058 Out << ',';
Chris Lattner7e708292002-06-25 16:13:24 +00001059 writeOperand(I.getOperand(op), true);
Chris Lattnere02fa852001-10-13 06:42:36 +00001060 }
1061
Misha Brukman0313e0b2004-06-21 21:53:56 +00001062 Out << " )\n\t\t\tto";
Chris Lattnere02fa852001-10-13 06:42:36 +00001063 writeOperand(II->getNormalDest(), true);
Misha Brukman0313e0b2004-06-21 21:53:56 +00001064 Out << " unwind";
Chris Lattneraeb2a1d2004-02-08 21:44:31 +00001065 writeOperand(II->getUnwindDest(), true);
Chris Lattnere02fa852001-10-13 06:42:36 +00001066
Chris Lattner7e708292002-06-25 16:13:24 +00001067 } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(&I)) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001068 Out << ' ';
Chris Lattner94dc1f22002-04-13 18:34:38 +00001069 printType(AI->getType()->getElementType());
1070 if (AI->isArrayAllocation()) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001071 Out << ',';
Chris Lattner94dc1f22002-04-13 18:34:38 +00001072 writeOperand(AI->getArraySize(), true);
Chris Lattner00950542001-06-06 20:29:01 +00001073 }
Chris Lattnere02fa852001-10-13 06:42:36 +00001074 } else if (isa<CastInst>(I)) {
Chris Lattner41495a22003-11-17 01:17:04 +00001075 if (Operand) writeOperand(Operand, true); // Work with broken code
Misha Brukman0313e0b2004-06-21 21:53:56 +00001076 Out << " to ";
Chris Lattner7e708292002-06-25 16:13:24 +00001077 printType(I.getType());
Chris Lattner4d45bd02003-10-18 05:57:43 +00001078 } else if (isa<VAArgInst>(I)) {
Chris Lattner41495a22003-11-17 01:17:04 +00001079 if (Operand) writeOperand(Operand, true); // Work with broken code
Misha Brukman0313e0b2004-06-21 21:53:56 +00001080 Out << ", ";
Chris Lattner8f77dae2003-05-08 02:44:12 +00001081 printType(I.getType());
Chris Lattner4d45bd02003-10-18 05:57:43 +00001082 } else if (const VANextInst *VAN = dyn_cast<VANextInst>(&I)) {
Chris Lattner41495a22003-11-17 01:17:04 +00001083 if (Operand) writeOperand(Operand, true); // Work with broken code
Misha Brukman0313e0b2004-06-21 21:53:56 +00001084 Out << ", ";
Chris Lattner4d45bd02003-10-18 05:57:43 +00001085 printType(VAN->getArgType());
Chris Lattner00950542001-06-06 20:29:01 +00001086 } else if (Operand) { // Print the normal way...
1087
1088 // PrintAllTypes - Instructions who have operands of all the same type
1089 // omit the type from all but the first operand. If the instruction has
1090 // different type operands (for example br), then they are all printed.
1091 bool PrintAllTypes = false;
1092 const Type *TheType = Operand->getType();
Chris Lattner00950542001-06-06 20:29:01 +00001093
Chris Lattnercfdd1482004-03-12 05:53:14 +00001094 // Shift Left & Right print both types even for Ubyte LHS, and select prints
1095 // types even if all operands are bools.
1096 if (isa<ShiftInst>(I) || isa<SelectInst>(I)) {
Chris Lattnerffd9bf42003-04-16 20:20:02 +00001097 PrintAllTypes = true;
1098 } else {
1099 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
1100 Operand = I.getOperand(i);
1101 if (Operand->getType() != TheType) {
1102 PrintAllTypes = true; // We have differing types! Print them all!
1103 break;
1104 }
Chris Lattner00950542001-06-06 20:29:01 +00001105 }
1106 }
Chris Lattnerffd9bf42003-04-16 20:20:02 +00001107
Chris Lattnerc1824992001-10-29 16:05:51 +00001108 if (!PrintAllTypes) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001109 Out << ' ';
Chris Lattnerffd9bf42003-04-16 20:20:02 +00001110 printType(TheType);
Chris Lattnerc1824992001-10-29 16:05:51 +00001111 }
Chris Lattner00950542001-06-06 20:29:01 +00001112
Chris Lattner7e708292002-06-25 16:13:24 +00001113 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001114 if (i) Out << ',';
Chris Lattner7e708292002-06-25 16:13:24 +00001115 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner00950542001-06-06 20:29:01 +00001116 }
1117 }
1118
Chris Lattnere02fa852001-10-13 06:42:36 +00001119 printInfoComment(I);
Misha Brukman0313e0b2004-06-21 21:53:56 +00001120 Out << "\n";
Chris Lattner00950542001-06-06 20:29:01 +00001121}
1122
1123
1124//===----------------------------------------------------------------------===//
1125// External Interface declarations
1126//===----------------------------------------------------------------------===//
1127
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001128void Module::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001129 SlotMachine SlotTable(this);
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001130 AssemblyWriter W(o, SlotTable, this, AAW);
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001131 W.write(this);
Chris Lattner00950542001-06-06 20:29:01 +00001132}
1133
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001134void GlobalVariable::print(std::ostream &o) const {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001135 SlotMachine SlotTable(getParent());
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001136 AssemblyWriter W(o, SlotTable, getParent(), 0);
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001137 W.write(this);
Chris Lattnerb0e45232001-09-10 20:08:19 +00001138}
1139
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001140void Function::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001141 SlotMachine SlotTable(getParent());
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001142 AssemblyWriter W(o, SlotTable, getParent(), AAW);
Chris Lattner00950542001-06-06 20:29:01 +00001143
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001144 W.write(this);
Chris Lattner00950542001-06-06 20:29:01 +00001145}
1146
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001147void BasicBlock::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001148 SlotMachine SlotTable(getParent());
Chris Lattnerc1824992001-10-29 16:05:51 +00001149 AssemblyWriter W(o, SlotTable,
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001150 getParent() ? getParent()->getParent() : 0, AAW);
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001151 W.write(this);
Chris Lattner00950542001-06-06 20:29:01 +00001152}
1153
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001154void Instruction::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001155 const Function *F = getParent() ? getParent()->getParent() : 0;
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001156 SlotMachine SlotTable(F);
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001157 AssemblyWriter W(o, SlotTable, F ? F->getParent() : 0, AAW);
Chris Lattner00950542001-06-06 20:29:01 +00001158
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001159 W.write(this);
Chris Lattner00950542001-06-06 20:29:01 +00001160}
Chris Lattnerda1fbcc2001-11-07 04:21:57 +00001161
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001162void Constant::print(std::ostream &o) const {
1163 if (this == 0) { o << "<null> constant value\n"; return; }
Chris Lattner3bc06b32002-09-10 15:53:49 +00001164
1165 // Handle CPR's special, because they have context information...
1166 if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(this)) {
1167 CPR->getValue()->print(o); // Print as a global value, with context info.
1168 return;
1169 }
1170
Misha Brukman40c732c2004-06-04 21:11:51 +00001171 o << ' ' << getType()->getDescription() << ' ';
Chris Lattner66e810b2002-04-18 18:53:13 +00001172
Chris Lattner7b13f562003-05-08 02:08:14 +00001173 std::map<const Type *, std::string> TypeTable;
Chris Lattner66e810b2002-04-18 18:53:13 +00001174 WriteConstantInt(o, this, false, TypeTable, 0);
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001175}
1176
1177void Type::print(std::ostream &o) const {
1178 if (this == 0)
1179 o << "<null Type>";
1180 else
1181 o << getDescription();
1182}
1183
Chris Lattner73e21422002-04-09 19:48:49 +00001184void Argument::print(std::ostream &o) const {
Chris Lattner144d9ba2004-07-13 23:14:34 +00001185 WriteAsOperand(o, this, true, true,
1186 getParent() ? getParent()->getParent() : 0);
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001187}
1188
Reid Spencerfa452c02004-05-25 18:14:38 +00001189// Value::dump - allow easy printing of Values from the debugger.
1190// Located here because so much of the needed functionality is here.
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001191void Value::dump() const { print(std::cerr); }
Reid Spencerfa452c02004-05-25 18:14:38 +00001192
1193// Type::dump - allow easy printing of Values from the debugger.
1194// Located here because so much of the needed functionality is here.
Reid Spencer9231ac82004-05-25 08:53:40 +00001195void Type::dump() const { print(std::cerr); }
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001196
1197//===----------------------------------------------------------------------===//
1198// CachedWriter Class Implementation
1199//===----------------------------------------------------------------------===//
1200
Chris Lattnerda1fbcc2001-11-07 04:21:57 +00001201void CachedWriter::setModule(const Module *M) {
1202 delete SC; delete AW;
1203 if (M) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001204 SC = new SlotMachine(M );
Misha Brukman40c732c2004-06-04 21:11:51 +00001205 AW = new AssemblyWriter(Out, *SC, M, 0);
Chris Lattnerda1fbcc2001-11-07 04:21:57 +00001206 } else {
1207 SC = 0; AW = 0;
1208 }
1209}
1210
1211CachedWriter::~CachedWriter() {
1212 delete AW;
1213 delete SC;
1214}
1215
Chris Lattner0a8e8e12004-07-15 02:51:31 +00001216CachedWriter &CachedWriter::operator<<(const Value &V) {
Chris Lattnerda1fbcc2001-11-07 04:21:57 +00001217 assert(AW && SC && "CachedWriter does not have a current module!");
Chris Lattner0a8e8e12004-07-15 02:51:31 +00001218 if (const Instruction *I = dyn_cast<Instruction>(&V))
Chris Lattnerfae098a2004-06-26 19:40:40 +00001219 AW->write(I);
Chris Lattner0a8e8e12004-07-15 02:51:31 +00001220 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(&V))
Chris Lattnerfae098a2004-06-26 19:40:40 +00001221 AW->write(BB);
Chris Lattner0a8e8e12004-07-15 02:51:31 +00001222 else if (const Function *F = dyn_cast<Function>(&V))
Chris Lattnerfae098a2004-06-26 19:40:40 +00001223 AW->write(F);
Chris Lattner0a8e8e12004-07-15 02:51:31 +00001224 else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(&V))
Chris Lattnerfae098a2004-06-26 19:40:40 +00001225 AW->write(GV);
Chris Lattnerfae098a2004-06-26 19:40:40 +00001226 else
Chris Lattner0a8e8e12004-07-15 02:51:31 +00001227 AW->writeOperand(&V, true, true);
Chris Lattnerda1fbcc2001-11-07 04:21:57 +00001228 return *this;
1229}
Misha Brukman5cf1acf2004-04-28 15:31:21 +00001230
Chris Lattner0a8e8e12004-07-15 02:51:31 +00001231CachedWriter& CachedWriter::operator<<(const Type &Ty) {
Misha Brukman5cf1acf2004-04-28 15:31:21 +00001232 if (SymbolicTypes) {
1233 const Module *M = AW->getModule();
Chris Lattner0a8e8e12004-07-15 02:51:31 +00001234 if (M) WriteTypeSymbolic(Out, &Ty, M);
Reid Spencer0e25e1c2004-07-04 11:50:43 +00001235 } else {
Chris Lattner0a8e8e12004-07-15 02:51:31 +00001236 AW->write(&Ty);
Reid Spencer0e25e1c2004-07-04 11:50:43 +00001237 }
1238 return *this;
Misha Brukman5cf1acf2004-04-28 15:31:21 +00001239}
Misha Brukmane5242de2004-04-28 19:24:28 +00001240
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001241//===----------------------------------------------------------------------===//
1242//===-- SlotMachine Implementation
1243//===----------------------------------------------------------------------===//
1244
1245#if 0
1246#define SC_DEBUG(X) std::cerr << X
1247#else
1248#define SC_DEBUG(X)
1249#endif
1250
1251// Module level constructor. Causes the contents of the Module (sans functions)
1252// to be added to the slot table.
1253SlotMachine::SlotMachine(const Module *M)
Reid Spencerb03de0c2004-05-26 21:56:09 +00001254 : TheModule(M) ///< Saved for lazy initialization.
1255 , TheFunction(0)
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001256 , mMap()
Reid Spencer0e25e1c2004-07-04 11:50:43 +00001257 , mTypes()
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001258 , fMap()
Reid Spencer0e25e1c2004-07-04 11:50:43 +00001259 , fTypes()
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001260{
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001261}
1262
1263// Function level constructor. Causes the contents of the Module and the one
1264// function provided to be added to the slot table.
1265SlotMachine::SlotMachine(const Function *F )
Reid Spencerb03de0c2004-05-26 21:56:09 +00001266 : TheModule( F ? F->getParent() : 0 ) ///< Saved for lazy initialization
1267 , TheFunction(F) ///< Saved for lazy initialization
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001268 , mMap()
Reid Spencer0e25e1c2004-07-04 11:50:43 +00001269 , mTypes()
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001270 , fMap()
Reid Spencer0e25e1c2004-07-04 11:50:43 +00001271 , fTypes()
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001272{
Reid Spencerb03de0c2004-05-26 21:56:09 +00001273}
1274
1275inline void SlotMachine::initialize(void) {
1276 if ( TheModule) {
1277 processModule();
1278 TheModule = 0; ///< Prevent re-processing next time we're called.
1279 }
1280 if ( TheFunction ) {
1281 processFunction();
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001282 }
1283}
1284
1285// Iterate through all the global variables, functions, and global
1286// variable initializers and create slots for them.
1287void SlotMachine::processModule() {
1288 SC_DEBUG("begin processModule!\n");
1289
1290 // Add all of the global variables to the value table...
1291 for (Module::const_giterator I = TheModule->gbegin(), E = TheModule->gend();
1292 I != E; ++I)
1293 createSlot(I);
1294
1295 // Add all the functions to the table
1296 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
1297 I != E; ++I)
1298 createSlot(I);
1299
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001300 SC_DEBUG("end processModule!\n");
1301}
1302
1303
Reid Spencerb03de0c2004-05-26 21:56:09 +00001304// Process the arguments, basic blocks, and instructions of a function.
1305void SlotMachine::processFunction() {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001306 SC_DEBUG("begin processFunction!\n");
1307
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001308 // Add all the function arguments
Reid Spencerb03de0c2004-05-26 21:56:09 +00001309 for(Function::const_aiterator AI = TheFunction->abegin(),
1310 AE = TheFunction->aend(); AI != AE; ++AI)
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001311 createSlot(AI);
1312
1313 SC_DEBUG("Inserting Instructions:\n");
1314
1315 // Add all of the basic blocks and instructions
Reid Spencerb03de0c2004-05-26 21:56:09 +00001316 for (Function::const_iterator BB = TheFunction->begin(),
1317 E = TheFunction->end(); BB != E; ++BB) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001318 createSlot(BB);
1319 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
1320 createSlot(I);
1321 }
1322 }
1323
1324 SC_DEBUG("end processFunction!\n");
1325}
1326
1327// Clean up after incorporating a function. This is the only way
Reid Spencerb03de0c2004-05-26 21:56:09 +00001328// to get out of the function incorporation state that affects the
1329// getSlot/createSlot lock. Function incorporation state is indicated
1330// by TheFunction != 0.
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001331void SlotMachine::purgeFunction() {
1332 SC_DEBUG("begin purgeFunction!\n");
1333 fMap.clear(); // Simply discard the function level map
Reid Spencer0e25e1c2004-07-04 11:50:43 +00001334 fTypes.clear();
Reid Spencerb03de0c2004-05-26 21:56:09 +00001335 TheFunction = 0;
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001336 SC_DEBUG("end purgeFunction!\n");
1337}
1338
1339/// Get the slot number for a value. This function will assert if you
1340/// ask for a Value that hasn't previously been inserted with createSlot.
1341/// Types are forbidden because Type does not inherit from Value (any more).
Chris Lattner69566452004-06-09 19:41:19 +00001342int SlotMachine::getSlot(const Value *V) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001343 assert( V && "Can't get slot for null Value" );
Reid Spencerb03de0c2004-05-26 21:56:09 +00001344 assert(!isa<Constant>(V) || isa<GlobalValue>(V) &&
1345 "Can't insert a non-GlobalValue Constant into SlotMachine");
1346
1347 // Check for uninitialized state and do lazy initialization
1348 this->initialize();
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001349
1350 // Do not number CPR's at all. They are an abomination
1351 if ( const ConstantPointerRef* CPR = dyn_cast<ConstantPointerRef>(V) )
1352 V = CPR->getValue() ;
1353
1354 // Get the type of the value
1355 const Type* VTy = V->getType();
1356
1357 // Find the type plane in the module map
1358 TypedPlanes::const_iterator MI = mMap.find(VTy);
1359
Reid Spencerb03de0c2004-05-26 21:56:09 +00001360 if ( TheFunction ) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001361 // Lookup the type in the function map too
1362 TypedPlanes::const_iterator FI = fMap.find(VTy);
1363 // If there is a corresponding type plane in the function map
1364 if ( FI != fMap.end() ) {
1365 // Lookup the Value in the function map
1366 ValueMap::const_iterator FVI = FI->second.map.find(V);
1367 // If the value doesn't exist in the function map
1368 if ( FVI == FI->second.map.end() ) {
Chris Lattnere9e326e2004-06-09 22:22:10 +00001369 // Look up the value in the module map.
1370 if (MI == mMap.end()) return -1;
Reid Spencerb03de0c2004-05-26 21:56:09 +00001371 ValueMap::const_iterator MVI = MI->second.map.find(V);
1372 // If we didn't find it, it wasn't inserted
Chris Lattner69566452004-06-09 19:41:19 +00001373 if (MVI == MI->second.map.end()) return -1;
Reid Spencerb03de0c2004-05-26 21:56:09 +00001374 assert( MVI != MI->second.map.end() && "Value not found");
1375 // We found it only at the module level
1376 return MVI->second;
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001377
1378 // else the value exists in the function map
1379 } else {
Reid Spencerb03de0c2004-05-26 21:56:09 +00001380 // Return the slot number as the module's contribution to
1381 // the type plane plus the index in the function's contribution
1382 // to the type plane.
Chris Lattnerd1cd3282004-06-15 21:07:32 +00001383 if (MI != mMap.end())
1384 return MI->second.next_slot + FVI->second;
1385 else
1386 return FVI->second;
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001387 }
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001388 }
1389 }
1390
Reid Spencerfc621e22004-06-09 15:26:53 +00001391 // N.B. Can get here only if either !TheFunction or the function doesn't
1392 // have a corresponding type plane for the Value
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001393
1394 // Make sure the type plane exists
Chris Lattner69566452004-06-09 19:41:19 +00001395 if (MI == mMap.end()) return -1;
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001396 // Lookup the value in the module's map
1397 ValueMap::const_iterator MVI = MI->second.map.find(V);
1398 // Make sure we found it.
Chris Lattner69566452004-06-09 19:41:19 +00001399 if (MVI == MI->second.map.end()) return -1;
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001400 // Return it.
1401 return MVI->second;
1402}
1403
Reid Spencer0e25e1c2004-07-04 11:50:43 +00001404/// Get the slot number for a value. This function will assert if you
1405/// ask for a Value that hasn't previously been inserted with createSlot.
1406/// Types are forbidden because Type does not inherit from Value (any more).
1407int SlotMachine::getSlot(const Type *Ty) {
1408 assert( Ty && "Can't get slot for null Type" );
1409
1410 // Check for uninitialized state and do lazy initialization
1411 this->initialize();
1412
1413 if ( TheFunction ) {
1414 // Lookup the Type in the function map
1415 TypeMap::const_iterator FTI = fTypes.map.find(Ty);
1416 // If the Type doesn't exist in the function map
1417 if ( FTI == fTypes.map.end() ) {
1418 TypeMap::const_iterator MTI = mTypes.map.find(Ty);
1419 // If we didn't find it, it wasn't inserted
1420 if (MTI == mTypes.map.end())
1421 return -1;
1422 // We found it only at the module level
1423 return MTI->second;
1424
1425 // else the value exists in the function map
1426 } else {
1427 // Return the slot number as the module's contribution to
1428 // the type plane plus the index in the function's contribution
1429 // to the type plane.
1430 return mTypes.next_slot + FTI->second;
1431 }
1432 }
1433
1434 // N.B. Can get here only if either !TheFunction
1435
1436 // Lookup the value in the module's map
1437 TypeMap::const_iterator MTI = mTypes.map.find(Ty);
1438 // Make sure we found it.
1439 if (MTI == mTypes.map.end()) return -1;
1440 // Return it.
1441 return MTI->second;
1442}
1443
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001444// Create a new slot, or return the existing slot if it is already
1445// inserted. Note that the logic here parallels getSlot but instead
1446// of asserting when the Value* isn't found, it inserts the value.
1447unsigned SlotMachine::createSlot(const Value *V) {
1448 assert( V && "Can't insert a null Value to SlotMachine");
Reid Spencerb03de0c2004-05-26 21:56:09 +00001449 assert(!isa<Constant>(V) || isa<GlobalValue>(V) &&
1450 "Can't insert a non-GlobalValue Constant into SlotMachine");
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001451
1452 const Type* VTy = V->getType();
1453
1454 // Just ignore void typed things
1455 if (VTy == Type::VoidTy) return 0; // FIXME: Wrong return value!
1456
1457 // Look up the type plane for the Value's type from the module map
1458 TypedPlanes::const_iterator MI = mMap.find(VTy);
1459
Reid Spencerb03de0c2004-05-26 21:56:09 +00001460 if ( TheFunction ) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001461 // Get the type plane for the Value's type from the function map
1462 TypedPlanes::const_iterator FI = fMap.find(VTy);
1463 // If there is a corresponding type plane in the function map
1464 if ( FI != fMap.end() ) {
1465 // Lookup the Value in the function map
1466 ValueMap::const_iterator FVI = FI->second.map.find(V);
1467 // If the value doesn't exist in the function map
1468 if ( FVI == FI->second.map.end() ) {
Reid Spencerb03de0c2004-05-26 21:56:09 +00001469 // If there is no corresponding type plane in the module map
1470 if ( MI == mMap.end() )
1471 return insertValue(V);
1472 // Look up the value in the module map
1473 ValueMap::const_iterator MVI = MI->second.map.find(V);
1474 // If we didn't find it, it wasn't inserted
1475 if ( MVI == MI->second.map.end() )
1476 return insertValue(V);
1477 else
1478 // We found it only at the module level
1479 return MVI->second;
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001480
1481 // else the value exists in the function map
1482 } else {
Reid Spencerb03de0c2004-05-26 21:56:09 +00001483 if ( MI == mMap.end() )
1484 return FVI->second;
1485 else
1486 // Return the slot number as the module's contribution to
1487 // the type plane plus the index in the function's contribution
1488 // to the type plane.
1489 return MI->second.next_slot + FVI->second;
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001490 }
1491
1492 // else there is not a corresponding type plane in the function map
1493 } else {
1494 // If the type plane doesn't exists at the module level
1495 if ( MI == mMap.end() ) {
Reid Spencerb03de0c2004-05-26 21:56:09 +00001496 return insertValue(V);
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001497 // else type plane exists at the module level, examine it
1498 } else {
Reid Spencerb03de0c2004-05-26 21:56:09 +00001499 // Look up the value in the module's map
1500 ValueMap::const_iterator MVI = MI->second.map.find(V);
1501 // If we didn't find it there either
1502 if ( MVI == MI->second.map.end() )
1503 // Return the slot number as the module's contribution to
1504 // the type plane plus the index of the function map insertion.
1505 return MI->second.next_slot + insertValue(V);
1506 else
1507 return MVI->second;
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001508 }
1509 }
1510 }
1511
Reid Spencerb03de0c2004-05-26 21:56:09 +00001512 // N.B. Can only get here if !TheFunction
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001513
1514 // If the module map's type plane is not for the Value's type
1515 if ( MI != mMap.end() ) {
1516 // Lookup the value in the module's map
1517 ValueMap::const_iterator MVI = MI->second.map.find(V);
1518 if ( MVI != MI->second.map.end() )
1519 return MVI->second;
1520 }
1521
1522 return insertValue(V);
1523}
1524
Reid Spencer0e25e1c2004-07-04 11:50:43 +00001525// Create a new slot, or return the existing slot if it is already
1526// inserted. Note that the logic here parallels getSlot but instead
1527// of asserting when the Value* isn't found, it inserts the value.
1528unsigned SlotMachine::createSlot(const Type *Ty) {
1529 assert( Ty && "Can't insert a null Type to SlotMachine");
1530
1531 if ( TheFunction ) {
1532 // Lookup the Type in the function map
1533 TypeMap::const_iterator FTI = fTypes.map.find(Ty);
1534 // If the type doesn't exist in the function map
1535 if ( FTI == fTypes.map.end() ) {
1536 // Look up the type in the module map
1537 TypeMap::const_iterator MTI = mTypes.map.find(Ty);
1538 // If we didn't find it, it wasn't inserted
1539 if ( MTI == mTypes.map.end() )
1540 return insertValue(Ty);
1541 else
1542 // We found it only at the module level
1543 return MTI->second;
1544
1545 // else the value exists in the function map
1546 } else {
1547 // Return the slot number as the module's contribution to
1548 // the type plane plus the index in the function's contribution
1549 // to the type plane.
1550 return mTypes.next_slot + FTI->second;
1551 }
1552 }
1553
1554 // N.B. Can only get here if !TheFunction
1555
1556 // Lookup the type in the module's map
1557 TypeMap::const_iterator MTI = mTypes.map.find(Ty);
1558 if ( MTI != mTypes.map.end() )
1559 return MTI->second;
1560
1561 return insertValue(Ty);
1562}
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001563
1564// Low level insert function. Minimal checking is done. This
1565// function is just for the convenience of createSlot (above).
1566unsigned SlotMachine::insertValue(const Value *V ) {
1567 assert(V && "Can't insert a null Value into SlotMachine!");
Reid Spencerb03de0c2004-05-26 21:56:09 +00001568 assert(!isa<Constant>(V) || isa<GlobalValue>(V) &&
1569 "Can't insert a non-GlobalValue Constant into SlotMachine");
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001570
Reid Spencerb03de0c2004-05-26 21:56:09 +00001571 // If this value does not contribute to a plane (is void)
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001572 // or if the value already has a name then ignore it.
Reid Spencerb03de0c2004-05-26 21:56:09 +00001573 if (V->getType() == Type::VoidTy || V->hasName() ) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001574 SC_DEBUG("ignored value " << *V << "\n");
1575 return 0; // FIXME: Wrong return value
1576 }
1577
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001578 const Type *VTy = V->getType();
1579 unsigned DestSlot = 0;
1580
Reid Spencerb03de0c2004-05-26 21:56:09 +00001581 if ( TheFunction ) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001582 TypedPlanes::iterator I = fMap.find( VTy );
1583 if ( I == fMap.end() )
Reid Spencer0e25e1c2004-07-04 11:50:43 +00001584 I = fMap.insert(std::make_pair(VTy,ValuePlane())).first;
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001585 DestSlot = I->second.map[V] = I->second.next_slot++;
1586 } else {
1587 TypedPlanes::iterator I = mMap.find( VTy );
1588 if ( I == mMap.end() )
Reid Spencer0e25e1c2004-07-04 11:50:43 +00001589 I = mMap.insert(std::make_pair(VTy,ValuePlane())).first;
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001590 DestSlot = I->second.map[V] = I->second.next_slot++;
1591 }
1592
1593 SC_DEBUG(" Inserting value [" << VTy << "] = " << V << " slot=" <<
Reid Spencerb03de0c2004-05-26 21:56:09 +00001594 DestSlot << " [");
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001595 // G = Global, C = Constant, T = Type, F = Function, o = other
Misha Brukman40c732c2004-06-04 21:11:51 +00001596 SC_DEBUG((isa<GlobalVariable>(V) ? 'G' : (isa<Constant>(V) ? 'C' :
1597 (isa<Function>(V) ? 'F' : 'o'))));
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001598 SC_DEBUG("]\n");
1599 return DestSlot;
1600}
1601
Reid Spencer0e25e1c2004-07-04 11:50:43 +00001602// Low level insert function. Minimal checking is done. This
1603// function is just for the convenience of createSlot (above).
1604unsigned SlotMachine::insertValue(const Type *Ty ) {
1605 assert(Ty && "Can't insert a null Type into SlotMachine!");
1606
1607 unsigned DestSlot = 0;
1608
1609 if ( TheFunction ) {
1610 DestSlot = fTypes.map[Ty] = fTypes.next_slot++;
1611 } else {
1612 DestSlot = fTypes.map[Ty] = fTypes.next_slot++;
1613 }
1614 SC_DEBUG(" Inserting type [" << DestSlot << "] = " << Ty << "\n");
1615 return DestSlot;
1616}
1617
Reid Spencer9231ac82004-05-25 08:53:40 +00001618// vim: sw=2