blob: 4d055e786ab2434fb6df578cc196f6c9d2937053 [file] [log] [blame]
Chris Lattnerf7e79482002-04-07 22:31:46 +00001//===-- AsmWriter.cpp - Printing LLVM as an assembly file -----------------===//
John Criswell482202a2003-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 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 Lattnerc70b3f62004-01-20 19:50:34 +000021#include "llvm/Constants.h"
Chris Lattner913d18f2002-04-29 18:46:50 +000022#include "llvm/DerivedTypes.h"
Vikram S. Adveb952b542002-07-14 23:14:45 +000023#include "llvm/Instruction.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000024#include "llvm/iMemory.h"
Chris Lattner862e3382001-10-13 06:42:36 +000025#include "llvm/iTerminators.h"
Chris Lattnerfb5ae022001-12-03 18:02:31 +000026#include "llvm/iPHINode.h"
27#include "llvm/iOther.h"
Chris Lattnerc70b3f62004-01-20 19:50:34 +000028#include "llvm/Module.h"
Chris Lattnerfee714f2001-09-07 16:36:04 +000029#include "llvm/SymbolTable.h"
Misha Brukman4685e262004-04-28 15:31:21 +000030#include "llvm/Assembly/Writer.h"
Chris Lattner58185f22002-10-02 19:38:55 +000031#include "llvm/Support/CFG.h"
Chris Lattner5de22042001-11-27 00:03:19 +000032#include "Support/StringExtras.h"
33#include "Support/STLExtras.h"
Chris Lattnerfee714f2001-09-07 16:36:04 +000034#include <algorithm>
Chris Lattner189d19f2003-11-21 20:23:48 +000035using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000036
Chris Lattner60a7dd12004-07-15 02:51:31 +000037namespace llvm {
Reid Spencer16f2f7f2004-05-26 07:18:52 +000038
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 Spencer58d30f22004-07-04 11:50:43 +000049 typedef std::map<const Type*, unsigned> TypeMap;
Reid Spencer16f2f7f2004-05-26 07:18:52 +000050
51 /// @brief A plane with next slot number and ValueMap
Reid Spencer58d30f22004-07-04 11:50:43 +000052 struct ValuePlane {
Reid Spencer16f2f7f2004-05-26 07:18:52 +000053 unsigned next_slot; ///< The next slot number to use
54 ValueMap map; ///< The map of Value* -> unsigned
Reid Spencer58d30f22004-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 Spencer16f2f7f2004-05-26 07:18:52 +000063 };
64
65 /// @brief The map of planes by Type
Reid Spencer58d30f22004-07-04 11:50:43 +000066 typedef std::map<const Type*, ValuePlane> TypedPlanes;
Reid Spencer16f2f7f2004-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 Lattner757ee0b2004-06-09 19:41:19 +000085 int getSlot(const Value *V);
Reid Spencer58d30f22004-07-04 11:50:43 +000086 int getSlot(const Type*Ty);
Reid Spencer8beac692004-06-09 15:26:53 +000087
88 /// Determine if a Value has a slot or not
89 bool hasSlot(const Value* V);
Reid Spencer58d30f22004-07-04 11:50:43 +000090 bool hasSlot(const Type* Ty);
Reid Spencer16f2f7f2004-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 Spencer56010e42004-05-26 21:56:09 +000098 void incorporateFunction(const Function *F) { TheFunction = F; }
Reid Spencer16f2f7f2004-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 Spencer56010e42004-05-26 21:56:09 +0000109 /// This function does the actual initialization.
110 inline void initialize();
111
Reid Spencer16f2f7f2004-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 Spencer58d30f22004-07-04 11:50:43 +0000116 unsigned createSlot(const Type* Ty);
Reid Spencer16f2f7f2004-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 Spencer58d30f22004-07-04 11:50:43 +0000122 unsigned insertValue( const Type* Ty);
Reid Spencer16f2f7f2004-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 Spencer56010e42004-05-26 21:56:09 +0000128 /// Add all of the functions arguments, basic blocks, and instructions
129 void processFunction();
130
Reid Spencer16f2f7f2004-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 Spencer56010e42004-05-26 21:56:09 +0000140 const Module* TheModule;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000141
Reid Spencer56010e42004-05-26 21:56:09 +0000142 /// @brief The function for which we are holding slot numbers
143 const Function* TheFunction;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000144
145 /// @brief The TypePlanes map for the module level data
146 TypedPlanes mMap;
Reid Spencer58d30f22004-07-04 11:50:43 +0000147 TypePlane mTypes;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000148
149 /// @brief The TypePlanes map for the function level data
150 TypedPlanes fMap;
Reid Spencer58d30f22004-07-04 11:50:43 +0000151 TypePlane fTypes;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000152
153/// @}
154
155};
156
Chris Lattner60a7dd12004-07-15 02:51:31 +0000157} // end namespace llvm
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000158
Chris Lattnerc8b70922002-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 Lattner7f8845a2002-07-23 18:07:49 +0000163
Chris Lattnerab7d1ab2003-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 Spencer16f2f7f2004-05-26 07:18:52 +0000167 SlotMachine *Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000168
Reid Spencer58d30f22004-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 Lattnerb86620e2001-10-29 16:37:48 +0000174static const Module *getModuleFromVal(const Value *V) {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000175 if (const Argument *MA = dyn_cast<Argument>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +0000176 return MA->getParent() ? MA->getParent()->getParent() : 0;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000177 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +0000178 return BB->getParent() ? BB->getParent()->getParent() : 0;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000179 else if (const Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner57698e22002-03-26 18:01:55 +0000180 const Function *M = I->getParent() ? I->getParent()->getParent() : 0;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000181 return M ? M->getParent() : 0;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000182 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +0000183 return GV->getParent();
Chris Lattnerb86620e2001-10-29 16:37:48 +0000184 return 0;
185}
186
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000187static SlotMachine *createSlotMachine(const Value *V) {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000188 if (const Argument *FA = dyn_cast<Argument>(V)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000189 return new SlotMachine(FA->getParent());
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000190 } else if (const Instruction *I = dyn_cast<Instruction>(V)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000191 return new SlotMachine(I->getParent()->getParent());
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000192 } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000193 return new SlotMachine(BB->getParent());
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000194 } else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)){
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000195 return new SlotMachine(GV->getParent());
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000196 } else if (const Function *Func = dyn_cast<Function>(V)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000197 return new SlotMachine(Func);
Chris Lattner7bfee412001-10-29 16:05:51 +0000198 }
199 return 0;
200}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000201
Chris Lattner1c343042003-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 Lattnerb86620e2001-10-29 16:37:48 +0000225
Misha Brukmanc566ca362004-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 Lattnerb86620e2001-10-29 16:37:48 +0000229static void fillTypeNameTable(const Module *M,
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000230 std::map<const Type *, std::string> &TypeNames) {
Chris Lattner98cf1f52002-11-20 18:36:02 +0000231 if (!M) return;
232 const SymbolTable &ST = M->getSymbolTable();
Reid Spencere7e96712004-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 Spencer56010e42004-05-26 21:56:09 +0000240 !cast<PointerType>(Ty)->getElementType()->isPrimitiveType() ||
241 isa<OpaqueType>(cast<PointerType>(Ty)->getElementType()))
Reid Spencere7e96712004-05-25 08:53:40 +0000242 TypeNames.insert(std::make_pair(Ty, getLLVMName(TI->first)));
Chris Lattnerb86620e2001-10-29 16:37:48 +0000243 }
244}
245
246
247
John Criswellcd116ba2004-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 Lattnerb86620e2001-10-29 16:37:48 +0000256
257 // Check to see if the type is named.
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000258 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
John Criswellcd116ba2004-06-01 14:54:08 +0000259 if (I != TypeNames.end()) {
260 Result += I->second;
261 return;
262 }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000263
John Criswellcd116ba2004-06-01 14:54:08 +0000264 if (isa<OpaqueType>(Ty)) {
265 Result += "opaque";
266 return;
267 }
Chris Lattnerf14ead92003-10-30 00:22:33 +0000268
Chris Lattnerb86620e2001-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 Criswellcd116ba2004-06-01 14:54:08 +0000276 if (Slot < CurSize) {
277 Result += "\\" + utostr(CurSize-Slot); // Here's the upreference
278 return;
279 }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000280
281 TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
282
Chris Lattner6b727592004-06-17 18:19:28 +0000283 switch (Ty->getTypeID()) {
Chris Lattner91db5822002-03-29 03:44:36 +0000284 case Type::FunctionTyID: {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000285 const FunctionType *FTy = cast<FunctionType>(Ty);
John Criswellcd116ba2004-06-01 14:54:08 +0000286 calcTypeName(FTy->getReturnType(), TypeStack, TypeNames, Result);
287 Result += " (";
Chris Lattnerfa829be2004-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 Lattnerb86620e2001-10-29 16:37:48 +0000291 Result += ", ";
John Criswellcd116ba2004-06-01 14:54:08 +0000292 calcTypeName(*I, TypeStack, TypeNames, Result);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000293 }
Chris Lattnerd816b532002-04-13 20:53:41 +0000294 if (FTy->isVarArg()) {
Chris Lattnerfa829be2004-02-09 04:14:01 +0000295 if (FTy->getNumParams()) Result += ", ";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000296 Result += "...";
297 }
298 Result += ")";
299 break;
300 }
301 case Type::StructTyID: {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000302 const StructType *STy = cast<StructType>(Ty);
John Criswellcd116ba2004-06-01 14:54:08 +0000303 Result += "{ ";
Chris Lattnerac6db752004-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 Lattnerb86620e2001-10-29 16:37:48 +0000307 Result += ", ";
John Criswellcd116ba2004-06-01 14:54:08 +0000308 calcTypeName(*I, TypeStack, TypeNames, Result);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000309 }
310 Result += " }";
311 break;
312 }
313 case Type::PointerTyID:
John Criswellcd116ba2004-06-01 14:54:08 +0000314 calcTypeName(cast<PointerType>(Ty)->getElementType(),
315 TypeStack, TypeNames, Result);
316 Result += "*";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000317 break;
318 case Type::ArrayTyID: {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000319 const ArrayType *ATy = cast<ArrayType>(Ty);
John Criswellcd116ba2004-06-01 14:54:08 +0000320 Result += "[" + utostr(ATy->getNumElements()) + " x ";
321 calcTypeName(ATy->getElementType(), TypeStack, TypeNames, Result);
322 Result += "]";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000323 break;
324 }
Chris Lattner15285ab2003-05-14 17:50:47 +0000325 case Type::OpaqueTyID:
John Criswellcd116ba2004-06-01 14:54:08 +0000326 Result += "opaque";
Chris Lattner15285ab2003-05-14 17:50:47 +0000327 break;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000328 default:
John Criswellcd116ba2004-06-01 14:54:08 +0000329 Result += "<unrecognized-type>";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000330 }
331
332 TypeStack.pop_back(); // Remove self from stack...
John Criswellcd116ba2004-06-01 14:54:08 +0000333 return;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000334}
335
336
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000337/// printTypeInt - The internal guts of printing out a type that has a
338/// potentially named portion.
339///
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000340static std::ostream &printTypeInt(std::ostream &Out, const Type *Ty,
341 std::map<const Type *, std::string> &TypeNames) {
Chris Lattnerb86620e2001-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 Lattner92d60532003-10-30 00:12:51 +0000345 if (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty))
346 return Out << Ty->getDescription();
Chris Lattnerb86620e2001-10-29 16:37:48 +0000347
348 // Check to see if the type is named.
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000349 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
Chris Lattnerb86620e2001-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 Lattnerab7d1ab2003-05-08 02:08:14 +0000356 std::vector<const Type *> TypeStack;
John Criswellcd116ba2004-06-01 14:54:08 +0000357 std::string TypeName;
358 calcTypeName(Ty, TypeStack, TypeNames, TypeName);
Chris Lattner7f74a562002-01-20 22:54:45 +0000359 TypeNames.insert(std::make_pair(Ty, TypeName));//Cache type name for later use
John Criswellcd116ba2004-06-01 14:54:08 +0000360 return (Out << TypeName);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000361}
362
Chris Lattner34b95182001-10-31 04:33:19 +0000363
Misha Brukmanb22d09c2004-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 Lattner189d19f2003-11-21 20:23:48 +0000368std::ostream &llvm::WriteTypeSymbolic(std::ostream &Out, const Type *Ty,
369 const Module *M) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000370 Out << ' ';
Chris Lattnerb86620e2001-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 Lattner98cf1f52002-11-20 18:36:02 +0000374 if (M) {
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000375 std::map<const Type *, std::string> TypeNames;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000376 fillTypeNameTable(M, TypeNames);
377
Chris Lattner72f866e2001-10-29 16:40:32 +0000378 return printTypeInt(Out, Ty, TypeNames);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000379 } else {
Chris Lattner72f866e2001-10-29 16:40:32 +0000380 return Out << Ty->getDescription();
Chris Lattnerb86620e2001-10-29 16:37:48 +0000381 }
382}
383
Reid Spenceraccd7c72004-07-17 23:47:01 +0000384/// @brief Internal constant writer.
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000385static void WriteConstantInt(std::ostream &Out, const Constant *CV,
386 bool PrintName,
387 std::map<const Type *, std::string> &TypeTable,
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000388 SlotMachine *Machine) {
Chris Lattner1e194682002-04-18 18:53:13 +0000389 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
390 Out << (CB == ConstantBool::True ? "true" : "false");
391 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV)) {
392 Out << CI->getValue();
393 } else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV)) {
394 Out << CI->getValue();
395 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
396 // We would like to output the FP constant value in exponential notation,
397 // but we cannot do this if doing so will lose precision. Check here to
398 // make sure that we only output it in exponential format if we can parse
399 // the value back and get the same value.
400 //
401 std::string StrVal = ftostr(CFP->getValue());
402
403 // Check to make sure that the stringized number is not some string like
404 // "Inf" or NaN, that atof will accept, but the lexer will not. Check that
405 // the string matches the "[-+]?[0-9]" regex.
406 //
407 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
408 ((StrVal[0] == '-' || StrVal[0] == '+') &&
Brian Gaeke87b4f072003-06-17 23:55:35 +0000409 (StrVal[1] >= '0' && StrVal[1] <= '9')))
Chris Lattner1e194682002-04-18 18:53:13 +0000410 // Reparse stringized version!
411 if (atof(StrVal.c_str()) == CFP->getValue()) {
412 Out << StrVal; return;
413 }
414
415 // Otherwise we could not reparse it to exactly the same value, so we must
416 // output the string in hexadecimal format!
417 //
418 // Behave nicely in the face of C TBAA rules... see:
419 // http://www.nullstone.com/htmls/category/aliastyp.htm
420 //
421 double Val = CFP->getValue();
422 char *Ptr = (char*)&Val;
423 assert(sizeof(double) == sizeof(uint64_t) && sizeof(double) == 8 &&
424 "assuming that double is 64 bits!");
425 Out << "0x" << utohexstr(*(uint64_t*)Ptr);
426
Chris Lattner76b2ff42004-02-15 05:55:15 +0000427 } else if (isa<ConstantAggregateZero>(CV)) {
428 Out << "zeroinitializer";
Chris Lattner1e194682002-04-18 18:53:13 +0000429 } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
430 // As a special case, print the array as a string if it is an array of
431 // ubytes or an array of sbytes with positive values.
432 //
433 const Type *ETy = CA->getType()->getElementType();
434 bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
435
436 if (ETy == Type::SByteTy)
437 for (unsigned i = 0; i < CA->getNumOperands(); ++i)
438 if (cast<ConstantSInt>(CA->getOperand(i))->getValue() < 0) {
439 isString = false;
440 break;
441 }
442
443 if (isString) {
444 Out << "c\"";
445 for (unsigned i = 0; i < CA->getNumOperands(); ++i) {
Chris Lattneraa6ff272004-06-04 23:53:20 +0000446 unsigned char C =
447 (unsigned char)cast<ConstantInt>(CA->getOperand(i))->getRawValue();
Chris Lattner1e194682002-04-18 18:53:13 +0000448
Chris Lattnere5fd3862002-07-31 23:56:44 +0000449 if (isprint(C) && C != '"' && C != '\\') {
Chris Lattner1e194682002-04-18 18:53:13 +0000450 Out << C;
451 } else {
452 Out << '\\'
453 << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'))
454 << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
455 }
456 }
457 Out << "\"";
458
459 } else { // Cannot output in string format...
Misha Brukman21bbdb92004-06-04 21:11:51 +0000460 Out << '[';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000461 if (CA->getNumOperands()) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000462 Out << ' ';
Chris Lattner1e194682002-04-18 18:53:13 +0000463 printTypeInt(Out, ETy, TypeTable);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000464 WriteAsOperandInternal(Out, CA->getOperand(0),
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000465 PrintName, TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000466 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
467 Out << ", ";
Chris Lattner1e194682002-04-18 18:53:13 +0000468 printTypeInt(Out, ETy, TypeTable);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000469 WriteAsOperandInternal(Out, CA->getOperand(i), PrintName,
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000470 TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000471 }
472 }
473 Out << " ]";
474 }
475 } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000476 Out << '{';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000477 if (CS->getNumOperands()) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000478 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000479 printTypeInt(Out, CS->getOperand(0)->getType(), TypeTable);
480
481 WriteAsOperandInternal(Out, CS->getOperand(0),
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000482 PrintName, TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000483
484 for (unsigned i = 1; i < CS->getNumOperands(); i++) {
485 Out << ", ";
486 printTypeInt(Out, CS->getOperand(i)->getType(), TypeTable);
487
488 WriteAsOperandInternal(Out, CS->getOperand(i),
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000489 PrintName, TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000490 }
491 }
492
493 Out << " }";
494 } else if (isa<ConstantPointerNull>(CV)) {
495 Out << "null";
496
Chris Lattner3cd8c562002-07-30 18:54:25 +0000497 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Chris Lattnerb8257222003-03-06 23:23:32 +0000498 Out << CE->getOpcodeName() << " (";
Vikram S. Adveb952b542002-07-14 23:14:45 +0000499
500 for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
501 printTypeInt(Out, (*OI)->getType(), TypeTable);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000502 WriteAsOperandInternal(Out, *OI, PrintName, TypeTable, Machine);
Vikram S. Adveb952b542002-07-14 23:14:45 +0000503 if (OI+1 != CE->op_end())
Chris Lattner3cd8c562002-07-30 18:54:25 +0000504 Out << ", ";
Vikram S. Adveb952b542002-07-14 23:14:45 +0000505 }
506
Chris Lattnercfe8f532002-08-16 21:17:11 +0000507 if (CE->getOpcode() == Instruction::Cast) {
Chris Lattner83b396b2002-08-15 19:37:43 +0000508 Out << " to ";
509 printTypeInt(Out, CE->getType(), TypeTable);
510 }
Misha Brukman21bbdb92004-06-04 21:11:51 +0000511 Out << ')';
Chris Lattner83b396b2002-08-15 19:37:43 +0000512
Chris Lattnerd84bb632002-04-16 21:36:08 +0000513 } else {
Vikram S. Adveb952b542002-07-14 23:14:45 +0000514 Out << "<placeholder or erroneous Constant>";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000515 }
516}
517
518
Misha Brukmanc566ca362004-03-02 00:22:19 +0000519/// WriteAsOperand - Write the name of the specified value out to the specified
520/// ostream. This can be useful when you just want to print int %reg126, not
521/// the whole instruction that generated it.
522///
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000523static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
524 bool PrintName,
525 std::map<const Type*, std::string> &TypeTable,
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000526 SlotMachine *Machine) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000527 Out << ' ';
Reid Spenceraccd7c72004-07-17 23:47:01 +0000528 if ((PrintName || isa<GlobalValue>(V)) && V->hasName())
Chris Lattner1c343042003-08-22 05:40:38 +0000529 Out << getLLVMName(V->getName());
Reid Spenceraccd7c72004-07-17 23:47:01 +0000530 else {
531 const Constant *CV = dyn_cast<Constant>(V);
532 if (CV && !isa<GlobalValue>(CV))
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000533 WriteConstantInt(Out, CV, PrintName, TypeTable, Machine);
Reid Spenceraccd7c72004-07-17 23:47:01 +0000534 else {
Chris Lattnerd84bb632002-04-16 21:36:08 +0000535 int Slot;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000536 if (Machine) {
Reid Spencer56010e42004-05-26 21:56:09 +0000537 Slot = Machine->getSlot(V);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000538 } else {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000539 Machine = createSlotMachine(V);
Chris Lattner757ee0b2004-06-09 19:41:19 +0000540 if (Machine == 0)
541 Slot = Machine->getSlot(V);
542 else
543 Slot = -1;
Reid Spencer56010e42004-05-26 21:56:09 +0000544 delete Machine;
Chris Lattnerd84bb632002-04-16 21:36:08 +0000545 }
Chris Lattner757ee0b2004-06-09 19:41:19 +0000546 if (Slot != -1)
547 Out << '%' << Slot;
548 else
549 Out << "<badref>";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000550 }
551 }
552}
553
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000554/// WriteAsOperand - Write the name of the specified value out to the specified
555/// ostream. This can be useful when you just want to print int %reg126, not
556/// the whole instruction that generated it.
557///
Chris Lattner189d19f2003-11-21 20:23:48 +0000558std::ostream &llvm::WriteAsOperand(std::ostream &Out, const Value *V,
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000559 bool PrintType, bool PrintName,
560 const Module *Context) {
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000561 std::map<const Type *, std::string> TypeNames;
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000562 if (Context == 0) Context = getModuleFromVal(V);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000563
Chris Lattner98cf1f52002-11-20 18:36:02 +0000564 if (Context)
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000565 fillTypeNameTable(Context, TypeNames);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000566
567 if (PrintType)
568 printTypeInt(Out, V->getType(), TypeNames);
569
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000570 WriteAsOperandInternal(Out, V, PrintName, TypeNames, 0);
Chris Lattner5e5abe32001-07-20 19:15:21 +0000571 return Out;
572}
573
Reid Spencer58d30f22004-07-04 11:50:43 +0000574/// WriteAsOperandInternal - Write the name of the specified value out to
575/// the specified ostream. This can be useful when you just want to print
576/// int %reg126, not the whole instruction that generated it.
577///
578static void WriteAsOperandInternal(std::ostream &Out, const Type *T,
579 bool PrintName,
580 std::map<const Type*, std::string> &TypeTable,
581 SlotMachine *Machine) {
582 Out << ' ';
583 int Slot;
584 if (Machine) {
585 Slot = Machine->getSlot(T);
586 if (Slot != -1)
587 Out << '%' << Slot;
588 else
589 Out << "<badref>";
590 } else {
591 Out << T->getDescription();
592 }
593}
594
595/// WriteAsOperand - Write the name of the specified value out to the specified
596/// ostream. This can be useful when you just want to print int %reg126, not
597/// the whole instruction that generated it.
598///
599std::ostream &llvm::WriteAsOperand(std::ostream &Out, const Type *Ty,
600 bool PrintType, bool PrintName,
601 const Module *Context) {
602 std::map<const Type *, std::string> TypeNames;
603 assert(Context != 0 && "Can't write types as operand without module context");
604
605 fillTypeNameTable(Context, TypeNames);
606
607 // if (PrintType)
608 // printTypeInt(Out, V->getType(), TypeNames);
609
610 printTypeInt(Out, Ty, TypeNames);
611
612 WriteAsOperandInternal(Out, Ty, PrintName, TypeNames, 0);
613 return Out;
614}
615
Chris Lattner189d19f2003-11-21 20:23:48 +0000616namespace llvm {
Chris Lattner2e9fee42001-07-12 23:35:26 +0000617
Chris Lattnerfee714f2001-09-07 16:36:04 +0000618class AssemblyWriter {
Misha Brukmana6619a92004-06-21 21:53:56 +0000619 std::ostream &Out;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000620 SlotMachine &Machine;
Chris Lattner7bfee412001-10-29 16:05:51 +0000621 const Module *TheModule;
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000622 std::map<const Type *, std::string> TypeNames;
Chris Lattner8339f7d2003-10-30 23:41:03 +0000623 AssemblyAnnotationWriter *AnnotationWriter;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000624public:
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000625 inline AssemblyWriter(std::ostream &o, SlotMachine &Mac, const Module *M,
Chris Lattner8339f7d2003-10-30 23:41:03 +0000626 AssemblyAnnotationWriter *AAW)
Misha Brukmana6619a92004-06-21 21:53:56 +0000627 : Out(o), Machine(Mac), TheModule(M), AnnotationWriter(AAW) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000628
629 // If the module has a symbol table, take all global types and stuff their
630 // names into the TypeNames map.
631 //
Chris Lattnerb86620e2001-10-29 16:37:48 +0000632 fillTypeNameTable(M, TypeNames);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000633 }
634
Chris Lattner7bfee412001-10-29 16:05:51 +0000635 inline void write(const Module *M) { printModule(M); }
636 inline void write(const GlobalVariable *G) { printGlobal(G); }
Chris Lattner57698e22002-03-26 18:01:55 +0000637 inline void write(const Function *F) { printFunction(F); }
Chris Lattner7bfee412001-10-29 16:05:51 +0000638 inline void write(const BasicBlock *BB) { printBasicBlock(BB); }
Chris Lattner113f4f42002-06-25 16:13:24 +0000639 inline void write(const Instruction *I) { printInstruction(*I); }
Chris Lattner3462ae32001-12-03 22:26:30 +0000640 inline void write(const Constant *CPV) { printConstant(CPV); }
Chris Lattner7db79582001-11-07 04:21:57 +0000641 inline void write(const Type *Ty) { printType(Ty); }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000642
Chris Lattner1e194682002-04-18 18:53:13 +0000643 void writeOperand(const Value *Op, bool PrintType, bool PrintName = true);
644
Misha Brukman4685e262004-04-28 15:31:21 +0000645 const Module* getModule() { return TheModule; }
646
Chris Lattner2f7c9632001-06-06 20:29:01 +0000647private :
Chris Lattner7bfee412001-10-29 16:05:51 +0000648 void printModule(const Module *M);
649 void printSymbolTable(const SymbolTable &ST);
Chris Lattner3462ae32001-12-03 22:26:30 +0000650 void printConstant(const Constant *CPV);
Chris Lattner7bfee412001-10-29 16:05:51 +0000651 void printGlobal(const GlobalVariable *GV);
Chris Lattner57698e22002-03-26 18:01:55 +0000652 void printFunction(const Function *F);
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000653 void printArgument(const Argument *FA);
Chris Lattner7bfee412001-10-29 16:05:51 +0000654 void printBasicBlock(const BasicBlock *BB);
Chris Lattner113f4f42002-06-25 16:13:24 +0000655 void printInstruction(const Instruction &I);
Chris Lattnerd816b532002-04-13 20:53:41 +0000656
657 // printType - Go to extreme measures to attempt to print out a short,
658 // symbolic version of a type name.
659 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000660 std::ostream &printType(const Type *Ty) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000661 return printTypeInt(Out, Ty, TypeNames);
Chris Lattnerd816b532002-04-13 20:53:41 +0000662 }
663
664 // printTypeAtLeastOneLevel - Print out one level of the possibly complex type
665 // without considering any symbolic types that we may have equal to it.
666 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000667 std::ostream &printTypeAtLeastOneLevel(const Type *Ty);
Chris Lattner7bfee412001-10-29 16:05:51 +0000668
Chris Lattner862e3382001-10-13 06:42:36 +0000669 // printInfoComment - Print a little comment after the instruction indicating
670 // which slot it occupies.
Chris Lattner113f4f42002-06-25 16:13:24 +0000671 void printInfoComment(const Value &V);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000672};
Reid Spencerf43ac622004-05-27 22:04:46 +0000673} // end of llvm namespace
Chris Lattner2f7c9632001-06-06 20:29:01 +0000674
Misha Brukmanc566ca362004-03-02 00:22:19 +0000675/// printTypeAtLeastOneLevel - Print out one level of the possibly complex type
676/// without considering any symbolic types that we may have equal to it.
677///
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000678std::ostream &AssemblyWriter::printTypeAtLeastOneLevel(const Type *Ty) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000679 if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000680 printType(FTy->getReturnType()) << " (";
Chris Lattnerfa829be2004-02-09 04:14:01 +0000681 for (FunctionType::param_iterator I = FTy->param_begin(),
682 E = FTy->param_end(); I != E; ++I) {
683 if (I != FTy->param_begin())
Misha Brukmana6619a92004-06-21 21:53:56 +0000684 Out << ", ";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000685 printType(*I);
Chris Lattnerd816b532002-04-13 20:53:41 +0000686 }
687 if (FTy->isVarArg()) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000688 if (FTy->getNumParams()) Out << ", ";
689 Out << "...";
Chris Lattnerd816b532002-04-13 20:53:41 +0000690 }
Misha Brukmana6619a92004-06-21 21:53:56 +0000691 Out << ')';
Chris Lattner113f4f42002-06-25 16:13:24 +0000692 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000693 Out << "{ ";
Chris Lattnerac6db752004-02-09 04:37:31 +0000694 for (StructType::element_iterator I = STy->element_begin(),
695 E = STy->element_end(); I != E; ++I) {
696 if (I != STy->element_begin())
Misha Brukmana6619a92004-06-21 21:53:56 +0000697 Out << ", ";
Chris Lattnerd816b532002-04-13 20:53:41 +0000698 printType(*I);
699 }
Misha Brukmana6619a92004-06-21 21:53:56 +0000700 Out << " }";
Chris Lattner113f4f42002-06-25 16:13:24 +0000701 } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000702 printType(PTy->getElementType()) << '*';
Chris Lattner113f4f42002-06-25 16:13:24 +0000703 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000704 Out << '[' << ATy->getNumElements() << " x ";
Misha Brukman21bbdb92004-06-04 21:11:51 +0000705 printType(ATy->getElementType()) << ']';
Chris Lattner113f4f42002-06-25 16:13:24 +0000706 } else if (const OpaqueType *OTy = dyn_cast<OpaqueType>(Ty)) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000707 Out << "opaque";
Chris Lattnerd816b532002-04-13 20:53:41 +0000708 } else {
Vikram S. Adveb952b542002-07-14 23:14:45 +0000709 if (!Ty->isPrimitiveType())
Misha Brukmana6619a92004-06-21 21:53:56 +0000710 Out << "<unknown derived type>";
Chris Lattnerd816b532002-04-13 20:53:41 +0000711 printType(Ty);
712 }
Misha Brukmana6619a92004-06-21 21:53:56 +0000713 return Out;
Chris Lattnerd816b532002-04-13 20:53:41 +0000714}
715
716
Chris Lattnerfee714f2001-09-07 16:36:04 +0000717void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType,
Reid Spencer56010e42004-05-26 21:56:09 +0000718 bool PrintName) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000719 if (PrintType) { Out << ' '; printType(Operand->getType()); }
720 WriteAsOperandInternal(Out, Operand, PrintName, TypeNames, &Machine);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000721}
722
Chris Lattner2f7c9632001-06-06 20:29:01 +0000723
Chris Lattner7bfee412001-10-29 16:05:51 +0000724void AssemblyWriter::printModule(const Module *M) {
Chris Lattner8068e0c2003-08-24 13:48:48 +0000725 switch (M->getEndianness()) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000726 case Module::LittleEndian: Out << "target endian = little\n"; break;
727 case Module::BigEndian: Out << "target endian = big\n"; break;
Chris Lattner8068e0c2003-08-24 13:48:48 +0000728 case Module::AnyEndianness: break;
729 }
730 switch (M->getPointerSize()) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000731 case Module::Pointer32: Out << "target pointersize = 32\n"; break;
732 case Module::Pointer64: Out << "target pointersize = 64\n"; break;
Chris Lattner8068e0c2003-08-24 13:48:48 +0000733 case Module::AnyPointerSize: break;
734 }
Reid Spencerffec7df2004-07-25 21:29:43 +0000735 if (M->getTargetTriple().size() > 0)
736 Out << "target triple = \"" << M->getTargetTriple() << "\"\n";
Reid Spencercc5ff642004-07-25 18:08:18 +0000737
738 // Loop over the dependent libraries and emit them
Reid Spencerffec7df2004-07-25 21:29:43 +0000739 if (M->lib_size() > 0) {
740 Out << "deplibs = [\n";
741 for (Module::lib_iterator LI = M->lib_begin(), LE = M->lib_end();
742 LI != LE; ) {
743 Out << "\"" << *LI << "\"";
744 ++LI;
745 if ( LI != LE )
746 Out << ",\n";
747 }
748 Out << " ]\n";
Reid Spencercc5ff642004-07-25 18:08:18 +0000749 }
Chris Lattner8068e0c2003-08-24 13:48:48 +0000750
Chris Lattnerfee714f2001-09-07 16:36:04 +0000751 // Loop over the symbol table, emitting all named constants...
Chris Lattner98cf1f52002-11-20 18:36:02 +0000752 printSymbolTable(M->getSymbolTable());
Chris Lattnerda975502001-09-10 07:58:01 +0000753
Chris Lattner113f4f42002-06-25 16:13:24 +0000754 for (Module::const_giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
755 printGlobal(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000756
Misha Brukmana6619a92004-06-21 21:53:56 +0000757 Out << "\nimplementation ; Functions:\n";
Vikram S. Adve13ba19a2001-09-18 12:48:16 +0000758
Chris Lattner6915f8f2002-04-07 22:49:37 +0000759 // Output all of the functions...
Chris Lattner113f4f42002-06-25 16:13:24 +0000760 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
761 printFunction(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000762}
763
Chris Lattner7bfee412001-10-29 16:05:51 +0000764void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000765 if (GV->hasName()) Out << getLLVMName(GV->getName()) << " = ";
Chris Lattner37798642001-09-18 04:01:05 +0000766
Chris Lattner379a8d22003-04-16 20:28:45 +0000767 if (!GV->hasInitializer())
Misha Brukmana6619a92004-06-21 21:53:56 +0000768 Out << "external ";
Chris Lattner379a8d22003-04-16 20:28:45 +0000769 else
770 switch (GV->getLinkage()) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000771 case GlobalValue::InternalLinkage: Out << "internal "; break;
772 case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
773 case GlobalValue::WeakLinkage: Out << "weak "; break;
774 case GlobalValue::AppendingLinkage: Out << "appending "; break;
Chris Lattner379a8d22003-04-16 20:28:45 +0000775 case GlobalValue::ExternalLinkage: break;
776 }
Chris Lattner37798642001-09-18 04:01:05 +0000777
Misha Brukmana6619a92004-06-21 21:53:56 +0000778 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattner2413b162001-12-04 00:03:30 +0000779 printType(GV->getType()->getElementType());
Chris Lattner37798642001-09-18 04:01:05 +0000780
Reid Spenceraccd7c72004-07-17 23:47:01 +0000781 if (GV->hasInitializer()) {
782 Constant* C = cast<Constant>(GV->getInitializer());
783 assert(C && "GlobalVar initializer isn't constant?");
Reid Spencerc9c90cf2004-07-18 01:04:19 +0000784 writeOperand(GV->getInitializer(), false, isa<GlobalValue>(C));
Reid Spenceraccd7c72004-07-17 23:47:01 +0000785 }
Chris Lattner37798642001-09-18 04:01:05 +0000786
Chris Lattner113f4f42002-06-25 16:13:24 +0000787 printInfoComment(*GV);
Misha Brukmana6619a92004-06-21 21:53:56 +0000788 Out << "\n";
Chris Lattnerda975502001-09-10 07:58:01 +0000789}
790
Chris Lattnerfee714f2001-09-07 16:36:04 +0000791
Reid Spencere7e96712004-05-25 08:53:40 +0000792// printSymbolTable - Run through symbol table looking for constants
793// and types. Emit their declarations.
Chris Lattner7bfee412001-10-29 16:05:51 +0000794void AssemblyWriter::printSymbolTable(const SymbolTable &ST) {
Reid Spencere7e96712004-05-25 08:53:40 +0000795
796 // Print the types.
797 for (SymbolTable::type_const_iterator TI = ST.type_begin();
798 TI != ST.type_end(); ++TI ) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000799 Out << "\t" << getLLVMName(TI->first) << " = type ";
Reid Spencere7e96712004-05-25 08:53:40 +0000800
801 // Make sure we print out at least one level of the type structure, so
802 // that we do not get %FILE = type %FILE
803 //
804 printTypeAtLeastOneLevel(TI->second) << "\n";
805 }
Chris Lattnerfee714f2001-09-07 16:36:04 +0000806
Reid Spencere7e96712004-05-25 08:53:40 +0000807 // Print the constants, in type plane order.
808 for (SymbolTable::plane_const_iterator PI = ST.plane_begin();
809 PI != ST.plane_end(); ++PI ) {
810 SymbolTable::value_const_iterator VI = ST.value_begin(PI->first);
811 SymbolTable::value_const_iterator VE = ST.value_end(PI->first);
812
813 for (; VI != VE; ++VI) {
Reid Spenceraccd7c72004-07-17 23:47:01 +0000814 const Value* V = VI->second;
815 const Constant *CPV = dyn_cast<Constant>(V) ;
816 if (CPV && !isa<GlobalValue>(V)) {
Reid Spencer56010e42004-05-26 21:56:09 +0000817 printConstant(CPV);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000818 }
819 }
Chris Lattnera7620d92001-07-15 06:35:59 +0000820 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000821}
822
823
Misha Brukmanc566ca362004-03-02 00:22:19 +0000824/// printConstant - Print out a constant pool entry...
825///
Chris Lattner3462ae32001-12-03 22:26:30 +0000826void AssemblyWriter::printConstant(const Constant *CPV) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000827 // Don't print out unnamed constants, they will be inlined
828 if (!CPV->hasName()) return;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000829
Chris Lattneree998be2001-07-26 16:29:38 +0000830 // Print out name...
Misha Brukmana6619a92004-06-21 21:53:56 +0000831 Out << "\t" << getLLVMName(CPV->getName()) << " =";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000832
833 // Write the value out now...
Chris Lattnerd84bb632002-04-16 21:36:08 +0000834 writeOperand(CPV, true, false);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000835
Chris Lattner113f4f42002-06-25 16:13:24 +0000836 printInfoComment(*CPV);
Misha Brukmana6619a92004-06-21 21:53:56 +0000837 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000838}
839
Misha Brukmanc566ca362004-03-02 00:22:19 +0000840/// printFunction - Print all aspects of a function.
841///
Chris Lattner113f4f42002-06-25 16:13:24 +0000842void AssemblyWriter::printFunction(const Function *F) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000843 // Print out the return type and name...
Misha Brukmana6619a92004-06-21 21:53:56 +0000844 Out << "\n";
Chris Lattner379a8d22003-04-16 20:28:45 +0000845
Misha Brukmana6619a92004-06-21 21:53:56 +0000846 if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +0000847
Chris Lattner379a8d22003-04-16 20:28:45 +0000848 if (F->isExternal())
Misha Brukmana6619a92004-06-21 21:53:56 +0000849 Out << "declare ";
Chris Lattner379a8d22003-04-16 20:28:45 +0000850 else
851 switch (F->getLinkage()) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000852 case GlobalValue::InternalLinkage: Out << "internal "; break;
853 case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
854 case GlobalValue::WeakLinkage: Out << "weak "; break;
855 case GlobalValue::AppendingLinkage: Out << "appending "; break;
Chris Lattner379a8d22003-04-16 20:28:45 +0000856 case GlobalValue::ExternalLinkage: break;
857 }
858
Misha Brukman21bbdb92004-06-04 21:11:51 +0000859 printType(F->getReturnType()) << ' ';
Chris Lattner5b337482003-10-18 05:57:43 +0000860 if (!F->getName().empty())
Misha Brukmana6619a92004-06-21 21:53:56 +0000861 Out << getLLVMName(F->getName());
Chris Lattner5b337482003-10-18 05:57:43 +0000862 else
Misha Brukmana6619a92004-06-21 21:53:56 +0000863 Out << "\"\"";
864 Out << '(';
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000865 Machine.incorporateFunction(F);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000866
Chris Lattner7bfee412001-10-29 16:05:51 +0000867 // Loop over the arguments, printing them...
Chris Lattner113f4f42002-06-25 16:13:24 +0000868 const FunctionType *FT = F->getFunctionType();
Chris Lattnerfee714f2001-09-07 16:36:04 +0000869
Chris Lattner149376d2002-10-13 20:57:00 +0000870 for(Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I)
871 printArgument(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000872
873 // Finish printing arguments...
Chris Lattner113f4f42002-06-25 16:13:24 +0000874 if (FT->isVarArg()) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000875 if (FT->getNumParams()) Out << ", ";
876 Out << "..."; // Output varargs portion of signature!
Chris Lattnerfee714f2001-09-07 16:36:04 +0000877 }
Misha Brukmana6619a92004-06-21 21:53:56 +0000878 Out << ')';
Chris Lattnerfee714f2001-09-07 16:36:04 +0000879
Chris Lattner113f4f42002-06-25 16:13:24 +0000880 if (F->isExternal()) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000881 Out << "\n";
Chris Lattnerb2f02e52002-05-06 03:00:40 +0000882 } else {
Misha Brukmana6619a92004-06-21 21:53:56 +0000883 Out << " {";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000884
Chris Lattner6915f8f2002-04-07 22:49:37 +0000885 // Output all of its basic blocks... for the function
Chris Lattner113f4f42002-06-25 16:13:24 +0000886 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
887 printBasicBlock(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000888
Misha Brukmana6619a92004-06-21 21:53:56 +0000889 Out << "}\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000890 }
891
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000892 Machine.purgeFunction();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000893}
894
Misha Brukmanc566ca362004-03-02 00:22:19 +0000895/// printArgument - This member is called for every argument that is passed into
896/// the function. Simply print it out
897///
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000898void AssemblyWriter::printArgument(const Argument *Arg) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000899 // Insert commas as we go... the first arg doesn't get a comma
Misha Brukmana6619a92004-06-21 21:53:56 +0000900 if (Arg != &Arg->getParent()->afront()) Out << ", ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000901
902 // Output type...
Chris Lattner7bfee412001-10-29 16:05:51 +0000903 printType(Arg->getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000904
905 // Output name, if available...
906 if (Arg->hasName())
Misha Brukmana6619a92004-06-21 21:53:56 +0000907 Out << ' ' << getLLVMName(Arg->getName());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000908}
909
Misha Brukmanc566ca362004-03-02 00:22:19 +0000910/// printBasicBlock - This member is called for each basic block in a method.
911///
Chris Lattner7bfee412001-10-29 16:05:51 +0000912void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000913 if (BB->hasName()) { // Print out the label if it exists...
Misha Brukmana6619a92004-06-21 21:53:56 +0000914 Out << "\n" << BB->getName() << ':';
Chris Lattner408dbdb2002-05-14 16:02:05 +0000915 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Misha Brukmana6619a92004-06-21 21:53:56 +0000916 Out << "\n; <label>:";
Chris Lattner757ee0b2004-06-09 19:41:19 +0000917 int Slot = Machine.getSlot(BB);
918 if (Slot != -1)
Misha Brukmana6619a92004-06-21 21:53:56 +0000919 Out << Slot;
Chris Lattner757ee0b2004-06-09 19:41:19 +0000920 else
Misha Brukmana6619a92004-06-21 21:53:56 +0000921 Out << "<badref>";
Chris Lattner58185f22002-10-02 19:38:55 +0000922 }
Chris Lattner2447ef52003-11-20 00:09:43 +0000923
924 if (BB->getParent() == 0)
Misha Brukmana6619a92004-06-21 21:53:56 +0000925 Out << "\t\t; Error: Block without parent!";
Chris Lattner2447ef52003-11-20 00:09:43 +0000926 else {
927 if (BB != &BB->getParent()->front()) { // Not the entry block?
928 // Output predecessors for the block...
Misha Brukmana6619a92004-06-21 21:53:56 +0000929 Out << "\t\t;";
Chris Lattner2447ef52003-11-20 00:09:43 +0000930 pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
931
932 if (PI == PE) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000933 Out << " No predecessors!";
Chris Lattner2447ef52003-11-20 00:09:43 +0000934 } else {
Misha Brukmana6619a92004-06-21 21:53:56 +0000935 Out << " preds =";
Chris Lattner00211f12003-11-16 22:59:57 +0000936 writeOperand(*PI, false, true);
Chris Lattner2447ef52003-11-20 00:09:43 +0000937 for (++PI; PI != PE; ++PI) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000938 Out << ',';
Chris Lattner2447ef52003-11-20 00:09:43 +0000939 writeOperand(*PI, false, true);
940 }
Chris Lattner00211f12003-11-16 22:59:57 +0000941 }
Chris Lattner58185f22002-10-02 19:38:55 +0000942 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000943 }
Chris Lattner408dbdb2002-05-14 16:02:05 +0000944
Misha Brukmana6619a92004-06-21 21:53:56 +0000945 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000946
Misha Brukmana6619a92004-06-21 21:53:56 +0000947 if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +0000948
Chris Lattnerfee714f2001-09-07 16:36:04 +0000949 // Output all of the instructions in the basic block...
Chris Lattner113f4f42002-06-25 16:13:24 +0000950 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
951 printInstruction(*I);
Chris Lattner96cdd272004-03-08 18:51:45 +0000952
Misha Brukmana6619a92004-06-21 21:53:56 +0000953 if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000954}
955
Chris Lattner862e3382001-10-13 06:42:36 +0000956
Misha Brukmanc566ca362004-03-02 00:22:19 +0000957/// printInfoComment - Print a little comment after the instruction indicating
958/// which slot it occupies.
959///
Chris Lattner113f4f42002-06-25 16:13:24 +0000960void AssemblyWriter::printInfoComment(const Value &V) {
961 if (V.getType() != Type::VoidTy) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000962 Out << "\t\t; <";
Misha Brukman21bbdb92004-06-04 21:11:51 +0000963 printType(V.getType()) << '>';
Chris Lattner862e3382001-10-13 06:42:36 +0000964
Chris Lattner113f4f42002-06-25 16:13:24 +0000965 if (!V.hasName()) {
Chris Lattner757ee0b2004-06-09 19:41:19 +0000966 int SlotNum = Machine.getSlot(&V);
967 if (SlotNum == -1)
Misha Brukmana6619a92004-06-21 21:53:56 +0000968 Out << ":<badref>";
Reid Spencer8beac692004-06-09 15:26:53 +0000969 else
Misha Brukmana6619a92004-06-21 21:53:56 +0000970 Out << ':' << SlotNum; // Print out the def slot taken.
Chris Lattner862e3382001-10-13 06:42:36 +0000971 }
Misha Brukmana6619a92004-06-21 21:53:56 +0000972 Out << " [#uses=" << V.use_size() << ']'; // Output # uses
Chris Lattner862e3382001-10-13 06:42:36 +0000973 }
974}
975
Reid Spencer8beac692004-06-09 15:26:53 +0000976/// printInstruction - This member is called for each Instruction in a function..
Misha Brukmanc566ca362004-03-02 00:22:19 +0000977///
Chris Lattner113f4f42002-06-25 16:13:24 +0000978void AssemblyWriter::printInstruction(const Instruction &I) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000979 if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +0000980
Misha Brukmana6619a92004-06-21 21:53:56 +0000981 Out << "\t";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000982
983 // Print out name if it exists...
Chris Lattner113f4f42002-06-25 16:13:24 +0000984 if (I.hasName())
Misha Brukmana6619a92004-06-21 21:53:56 +0000985 Out << getLLVMName(I.getName()) << " = ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000986
Chris Lattner504f9242003-09-08 17:45:59 +0000987 // If this is a volatile load or store, print out the volatile marker
988 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
989 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile()))
Misha Brukmana6619a92004-06-21 21:53:56 +0000990 Out << "volatile ";
Chris Lattner504f9242003-09-08 17:45:59 +0000991
Chris Lattner2f7c9632001-06-06 20:29:01 +0000992 // Print out the opcode...
Misha Brukmana6619a92004-06-21 21:53:56 +0000993 Out << I.getOpcodeName();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000994
995 // Print out the type of the operands...
Chris Lattner113f4f42002-06-25 16:13:24 +0000996 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000997
998 // Special case conditional branches to swizzle the condition out to the front
Chris Lattner113f4f42002-06-25 16:13:24 +0000999 if (isa<BranchInst>(I) && I.getNumOperands() > 1) {
1000 writeOperand(I.getOperand(2), true);
Misha Brukmana6619a92004-06-21 21:53:56 +00001001 Out << ',';
Chris Lattner2f7c9632001-06-06 20:29:01 +00001002 writeOperand(Operand, true);
Misha Brukmana6619a92004-06-21 21:53:56 +00001003 Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001004 writeOperand(I.getOperand(1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001005
Chris Lattner8d48df22002-04-13 18:34:38 +00001006 } else if (isa<SwitchInst>(I)) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001007 // Special case switch statement to get formatting nice and correct...
Misha Brukmana6619a92004-06-21 21:53:56 +00001008 writeOperand(Operand , true); Out << ',';
1009 writeOperand(I.getOperand(1), true); Out << " [";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001010
Chris Lattner113f4f42002-06-25 16:13:24 +00001011 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001012 Out << "\n\t\t";
1013 writeOperand(I.getOperand(op ), true); Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001014 writeOperand(I.getOperand(op+1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001015 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001016 Out << "\n\t]";
Chris Lattnerda558102001-10-02 03:41:24 +00001017 } else if (isa<PHINode>(I)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001018 Out << ' ';
Chris Lattner113f4f42002-06-25 16:13:24 +00001019 printType(I.getType());
Misha Brukmana6619a92004-06-21 21:53:56 +00001020 Out << ' ';
Chris Lattner2f7c9632001-06-06 20:29:01 +00001021
Chris Lattner113f4f42002-06-25 16:13:24 +00001022 for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001023 if (op) Out << ", ";
1024 Out << '[';
1025 writeOperand(I.getOperand(op ), false); Out << ',';
1026 writeOperand(I.getOperand(op+1), false); Out << " ]";
Chris Lattner931ef3b2001-06-11 15:04:20 +00001027 }
Chris Lattner862e3382001-10-13 06:42:36 +00001028 } else if (isa<ReturnInst>(I) && !Operand) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001029 Out << " void";
Chris Lattner862e3382001-10-13 06:42:36 +00001030 } else if (isa<CallInst>(I)) {
Chris Lattner463d6a52003-08-05 15:34:45 +00001031 const PointerType *PTy = cast<PointerType>(Operand->getType());
1032 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1033 const Type *RetTy = FTy->getReturnType();
Chris Lattner2f2d9472001-11-06 21:28:12 +00001034
Chris Lattner463d6a52003-08-05 15:34:45 +00001035 // If possible, print out the short form of the call instruction. We can
Chris Lattner6915f8f2002-04-07 22:49:37 +00001036 // only do this if the first argument is a pointer to a nonvararg function,
Chris Lattner463d6a52003-08-05 15:34:45 +00001037 // and if the return type is not a pointer to a function.
Chris Lattner2f2d9472001-11-06 21:28:12 +00001038 //
Chris Lattner463d6a52003-08-05 15:34:45 +00001039 if (!FTy->isVarArg() &&
Chris Lattner8d48df22002-04-13 18:34:38 +00001040 (!isa<PointerType>(RetTy) ||
Chris Lattnerd9a36a62002-07-25 20:58:51 +00001041 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001042 Out << ' '; printType(RetTy);
Chris Lattner2f2d9472001-11-06 21:28:12 +00001043 writeOperand(Operand, false);
1044 } else {
1045 writeOperand(Operand, true);
1046 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001047 Out << '(';
Chris Lattner113f4f42002-06-25 16:13:24 +00001048 if (I.getNumOperands() > 1) writeOperand(I.getOperand(1), true);
1049 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; ++op) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001050 Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001051 writeOperand(I.getOperand(op), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001052 }
1053
Misha Brukmana6619a92004-06-21 21:53:56 +00001054 Out << " )";
Chris Lattner113f4f42002-06-25 16:13:24 +00001055 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Chris Lattner463d6a52003-08-05 15:34:45 +00001056 const PointerType *PTy = cast<PointerType>(Operand->getType());
1057 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1058 const Type *RetTy = FTy->getReturnType();
1059
1060 // If possible, print out the short form of the invoke instruction. We can
1061 // only do this if the first argument is a pointer to a nonvararg function,
1062 // and if the return type is not a pointer to a function.
1063 //
1064 if (!FTy->isVarArg() &&
1065 (!isa<PointerType>(RetTy) ||
1066 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001067 Out << ' '; printType(RetTy);
Chris Lattner463d6a52003-08-05 15:34:45 +00001068 writeOperand(Operand, false);
1069 } else {
1070 writeOperand(Operand, true);
1071 }
1072
Misha Brukmana6619a92004-06-21 21:53:56 +00001073 Out << '(';
Chris Lattner113f4f42002-06-25 16:13:24 +00001074 if (I.getNumOperands() > 3) writeOperand(I.getOperand(3), true);
1075 for (unsigned op = 4, Eop = I.getNumOperands(); op < Eop; ++op) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001076 Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001077 writeOperand(I.getOperand(op), true);
Chris Lattner862e3382001-10-13 06:42:36 +00001078 }
1079
Misha Brukmana6619a92004-06-21 21:53:56 +00001080 Out << " )\n\t\t\tto";
Chris Lattner862e3382001-10-13 06:42:36 +00001081 writeOperand(II->getNormalDest(), true);
Misha Brukmana6619a92004-06-21 21:53:56 +00001082 Out << " unwind";
Chris Lattnerfae8ab32004-02-08 21:44:31 +00001083 writeOperand(II->getUnwindDest(), true);
Chris Lattner862e3382001-10-13 06:42:36 +00001084
Chris Lattner113f4f42002-06-25 16:13:24 +00001085 } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(&I)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001086 Out << ' ';
Chris Lattner8d48df22002-04-13 18:34:38 +00001087 printType(AI->getType()->getElementType());
1088 if (AI->isArrayAllocation()) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001089 Out << ',';
Chris Lattner8d48df22002-04-13 18:34:38 +00001090 writeOperand(AI->getArraySize(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001091 }
Chris Lattner862e3382001-10-13 06:42:36 +00001092 } else if (isa<CastInst>(I)) {
Chris Lattnerc96e96b2003-11-17 01:17:04 +00001093 if (Operand) writeOperand(Operand, true); // Work with broken code
Misha Brukmana6619a92004-06-21 21:53:56 +00001094 Out << " to ";
Chris Lattner113f4f42002-06-25 16:13:24 +00001095 printType(I.getType());
Chris Lattner5b337482003-10-18 05:57:43 +00001096 } else if (isa<VAArgInst>(I)) {
Chris Lattnerc96e96b2003-11-17 01:17:04 +00001097 if (Operand) writeOperand(Operand, true); // Work with broken code
Misha Brukmana6619a92004-06-21 21:53:56 +00001098 Out << ", ";
Chris Lattnerf70da102003-05-08 02:44:12 +00001099 printType(I.getType());
Chris Lattner5b337482003-10-18 05:57:43 +00001100 } else if (const VANextInst *VAN = dyn_cast<VANextInst>(&I)) {
Chris Lattnerc96e96b2003-11-17 01:17:04 +00001101 if (Operand) writeOperand(Operand, true); // Work with broken code
Misha Brukmana6619a92004-06-21 21:53:56 +00001102 Out << ", ";
Chris Lattner5b337482003-10-18 05:57:43 +00001103 printType(VAN->getArgType());
Chris Lattner2f7c9632001-06-06 20:29:01 +00001104 } else if (Operand) { // Print the normal way...
1105
1106 // PrintAllTypes - Instructions who have operands of all the same type
1107 // omit the type from all but the first operand. If the instruction has
1108 // different type operands (for example br), then they are all printed.
1109 bool PrintAllTypes = false;
1110 const Type *TheType = Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001111
Chris Lattner52bd5cb92004-03-12 05:53:14 +00001112 // Shift Left & Right print both types even for Ubyte LHS, and select prints
1113 // types even if all operands are bools.
1114 if (isa<ShiftInst>(I) || isa<SelectInst>(I)) {
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00001115 PrintAllTypes = true;
1116 } else {
1117 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
1118 Operand = I.getOperand(i);
1119 if (Operand->getType() != TheType) {
1120 PrintAllTypes = true; // We have differing types! Print them all!
1121 break;
1122 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001123 }
1124 }
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00001125
Chris Lattner7bfee412001-10-29 16:05:51 +00001126 if (!PrintAllTypes) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001127 Out << ' ';
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00001128 printType(TheType);
Chris Lattner7bfee412001-10-29 16:05:51 +00001129 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001130
Chris Lattner113f4f42002-06-25 16:13:24 +00001131 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001132 if (i) Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001133 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001134 }
1135 }
1136
Chris Lattner862e3382001-10-13 06:42:36 +00001137 printInfoComment(I);
Misha Brukmana6619a92004-06-21 21:53:56 +00001138 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001139}
1140
1141
1142//===----------------------------------------------------------------------===//
1143// External Interface declarations
1144//===----------------------------------------------------------------------===//
1145
Chris Lattner8339f7d2003-10-30 23:41:03 +00001146void Module::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001147 SlotMachine SlotTable(this);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001148 AssemblyWriter W(o, SlotTable, this, AAW);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001149 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001150}
1151
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001152void GlobalVariable::print(std::ostream &o) const {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001153 SlotMachine SlotTable(getParent());
Chris Lattner8339f7d2003-10-30 23:41:03 +00001154 AssemblyWriter W(o, SlotTable, getParent(), 0);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001155 W.write(this);
Chris Lattneradfe0d12001-09-10 20:08:19 +00001156}
1157
Chris Lattner8339f7d2003-10-30 23:41:03 +00001158void Function::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001159 SlotMachine SlotTable(getParent());
Chris Lattner8339f7d2003-10-30 23:41:03 +00001160 AssemblyWriter W(o, SlotTable, getParent(), AAW);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001161
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001162 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001163}
1164
Chris Lattner8339f7d2003-10-30 23:41:03 +00001165void BasicBlock::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001166 SlotMachine SlotTable(getParent());
Chris Lattner7bfee412001-10-29 16:05:51 +00001167 AssemblyWriter W(o, SlotTable,
Chris Lattner8339f7d2003-10-30 23:41:03 +00001168 getParent() ? getParent()->getParent() : 0, AAW);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001169 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001170}
1171
Chris Lattner8339f7d2003-10-30 23:41:03 +00001172void Instruction::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001173 const Function *F = getParent() ? getParent()->getParent() : 0;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001174 SlotMachine SlotTable(F);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001175 AssemblyWriter W(o, SlotTable, F ? F->getParent() : 0, AAW);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001176
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001177 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001178}
Chris Lattner7db79582001-11-07 04:21:57 +00001179
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001180void Constant::print(std::ostream &o) const {
1181 if (this == 0) { o << "<null> constant value\n"; return; }
Chris Lattner7d734802002-09-10 15:53:49 +00001182
Misha Brukman21bbdb92004-06-04 21:11:51 +00001183 o << ' ' << getType()->getDescription() << ' ';
Chris Lattner1e194682002-04-18 18:53:13 +00001184
Chris Lattnerab7d1ab2003-05-08 02:08:14 +00001185 std::map<const Type *, std::string> TypeTable;
Chris Lattner1e194682002-04-18 18:53:13 +00001186 WriteConstantInt(o, this, false, TypeTable, 0);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001187}
1188
1189void Type::print(std::ostream &o) const {
1190 if (this == 0)
1191 o << "<null Type>";
1192 else
1193 o << getDescription();
1194}
1195
Chris Lattner2e9fa6d2002-04-09 19:48:49 +00001196void Argument::print(std::ostream &o) const {
Chris Lattnere52ed452004-07-13 23:14:34 +00001197 WriteAsOperand(o, this, true, true,
1198 getParent() ? getParent()->getParent() : 0);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001199}
1200
Reid Spencer52641832004-05-25 18:14:38 +00001201// Value::dump - allow easy printing of Values from the debugger.
1202// Located here because so much of the needed functionality is here.
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001203void Value::dump() const { print(std::cerr); }
Reid Spencer52641832004-05-25 18:14:38 +00001204
1205// Type::dump - allow easy printing of Values from the debugger.
1206// Located here because so much of the needed functionality is here.
Reid Spencere7e96712004-05-25 08:53:40 +00001207void Type::dump() const { print(std::cerr); }
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001208
1209//===----------------------------------------------------------------------===//
1210// CachedWriter Class Implementation
1211//===----------------------------------------------------------------------===//
1212
Chris Lattner7db79582001-11-07 04:21:57 +00001213void CachedWriter::setModule(const Module *M) {
1214 delete SC; delete AW;
1215 if (M) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001216 SC = new SlotMachine(M );
Misha Brukman21bbdb92004-06-04 21:11:51 +00001217 AW = new AssemblyWriter(Out, *SC, M, 0);
Chris Lattner7db79582001-11-07 04:21:57 +00001218 } else {
1219 SC = 0; AW = 0;
1220 }
1221}
1222
1223CachedWriter::~CachedWriter() {
1224 delete AW;
1225 delete SC;
1226}
1227
Chris Lattner60a7dd12004-07-15 02:51:31 +00001228CachedWriter &CachedWriter::operator<<(const Value &V) {
Chris Lattner7db79582001-11-07 04:21:57 +00001229 assert(AW && SC && "CachedWriter does not have a current module!");
Chris Lattner60a7dd12004-07-15 02:51:31 +00001230 if (const Instruction *I = dyn_cast<Instruction>(&V))
Chris Lattner80d2d532004-06-26 19:40:40 +00001231 AW->write(I);
Chris Lattner60a7dd12004-07-15 02:51:31 +00001232 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(&V))
Chris Lattner80d2d532004-06-26 19:40:40 +00001233 AW->write(BB);
Chris Lattner60a7dd12004-07-15 02:51:31 +00001234 else if (const Function *F = dyn_cast<Function>(&V))
Chris Lattner80d2d532004-06-26 19:40:40 +00001235 AW->write(F);
Chris Lattner60a7dd12004-07-15 02:51:31 +00001236 else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(&V))
Chris Lattner80d2d532004-06-26 19:40:40 +00001237 AW->write(GV);
Chris Lattner80d2d532004-06-26 19:40:40 +00001238 else
Chris Lattner60a7dd12004-07-15 02:51:31 +00001239 AW->writeOperand(&V, true, true);
Chris Lattner7db79582001-11-07 04:21:57 +00001240 return *this;
1241}
Misha Brukman4685e262004-04-28 15:31:21 +00001242
Chris Lattner60a7dd12004-07-15 02:51:31 +00001243CachedWriter& CachedWriter::operator<<(const Type &Ty) {
Misha Brukman4685e262004-04-28 15:31:21 +00001244 if (SymbolicTypes) {
1245 const Module *M = AW->getModule();
Chris Lattner60a7dd12004-07-15 02:51:31 +00001246 if (M) WriteTypeSymbolic(Out, &Ty, M);
Reid Spencer58d30f22004-07-04 11:50:43 +00001247 } else {
Chris Lattner60a7dd12004-07-15 02:51:31 +00001248 AW->write(&Ty);
Reid Spencer58d30f22004-07-04 11:50:43 +00001249 }
1250 return *this;
Misha Brukman4685e262004-04-28 15:31:21 +00001251}
Misha Brukmanda546ea2004-04-28 19:24:28 +00001252
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001253//===----------------------------------------------------------------------===//
1254//===-- SlotMachine Implementation
1255//===----------------------------------------------------------------------===//
1256
1257#if 0
1258#define SC_DEBUG(X) std::cerr << X
1259#else
1260#define SC_DEBUG(X)
1261#endif
1262
1263// Module level constructor. Causes the contents of the Module (sans functions)
1264// to be added to the slot table.
1265SlotMachine::SlotMachine(const Module *M)
Reid Spencer56010e42004-05-26 21:56:09 +00001266 : TheModule(M) ///< Saved for lazy initialization.
1267 , TheFunction(0)
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001268 , mMap()
Reid Spencer58d30f22004-07-04 11:50:43 +00001269 , mTypes()
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001270 , fMap()
Reid Spencer58d30f22004-07-04 11:50:43 +00001271 , fTypes()
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001272{
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001273}
1274
1275// Function level constructor. Causes the contents of the Module and the one
1276// function provided to be added to the slot table.
1277SlotMachine::SlotMachine(const Function *F )
Reid Spencer56010e42004-05-26 21:56:09 +00001278 : TheModule( F ? F->getParent() : 0 ) ///< Saved for lazy initialization
1279 , TheFunction(F) ///< Saved for lazy initialization
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001280 , mMap()
Reid Spencer58d30f22004-07-04 11:50:43 +00001281 , mTypes()
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001282 , fMap()
Reid Spencer58d30f22004-07-04 11:50:43 +00001283 , fTypes()
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001284{
Reid Spencer56010e42004-05-26 21:56:09 +00001285}
1286
1287inline void SlotMachine::initialize(void) {
1288 if ( TheModule) {
1289 processModule();
1290 TheModule = 0; ///< Prevent re-processing next time we're called.
1291 }
1292 if ( TheFunction ) {
1293 processFunction();
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001294 }
1295}
1296
1297// Iterate through all the global variables, functions, and global
1298// variable initializers and create slots for them.
1299void SlotMachine::processModule() {
1300 SC_DEBUG("begin processModule!\n");
1301
1302 // Add all of the global variables to the value table...
1303 for (Module::const_giterator I = TheModule->gbegin(), E = TheModule->gend();
1304 I != E; ++I)
1305 createSlot(I);
1306
1307 // Add all the functions to the table
1308 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
1309 I != E; ++I)
1310 createSlot(I);
1311
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001312 SC_DEBUG("end processModule!\n");
1313}
1314
1315
Reid Spencer56010e42004-05-26 21:56:09 +00001316// Process the arguments, basic blocks, and instructions of a function.
1317void SlotMachine::processFunction() {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001318 SC_DEBUG("begin processFunction!\n");
1319
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001320 // Add all the function arguments
Reid Spencer56010e42004-05-26 21:56:09 +00001321 for(Function::const_aiterator AI = TheFunction->abegin(),
1322 AE = TheFunction->aend(); AI != AE; ++AI)
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001323 createSlot(AI);
1324
1325 SC_DEBUG("Inserting Instructions:\n");
1326
1327 // Add all of the basic blocks and instructions
Reid Spencer56010e42004-05-26 21:56:09 +00001328 for (Function::const_iterator BB = TheFunction->begin(),
1329 E = TheFunction->end(); BB != E; ++BB) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001330 createSlot(BB);
1331 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
1332 createSlot(I);
1333 }
1334 }
1335
1336 SC_DEBUG("end processFunction!\n");
1337}
1338
1339// Clean up after incorporating a function. This is the only way
Reid Spencer56010e42004-05-26 21:56:09 +00001340// to get out of the function incorporation state that affects the
1341// getSlot/createSlot lock. Function incorporation state is indicated
1342// by TheFunction != 0.
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001343void SlotMachine::purgeFunction() {
1344 SC_DEBUG("begin purgeFunction!\n");
1345 fMap.clear(); // Simply discard the function level map
Reid Spencer58d30f22004-07-04 11:50:43 +00001346 fTypes.clear();
Reid Spencer56010e42004-05-26 21:56:09 +00001347 TheFunction = 0;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001348 SC_DEBUG("end purgeFunction!\n");
1349}
1350
1351/// Get the slot number for a value. This function will assert if you
1352/// ask for a Value that hasn't previously been inserted with createSlot.
1353/// Types are forbidden because Type does not inherit from Value (any more).
Chris Lattner757ee0b2004-06-09 19:41:19 +00001354int SlotMachine::getSlot(const Value *V) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001355 assert( V && "Can't get slot for null Value" );
Reid Spencer56010e42004-05-26 21:56:09 +00001356 assert(!isa<Constant>(V) || isa<GlobalValue>(V) &&
1357 "Can't insert a non-GlobalValue Constant into SlotMachine");
1358
1359 // Check for uninitialized state and do lazy initialization
1360 this->initialize();
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001361
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001362 // Get the type of the value
1363 const Type* VTy = V->getType();
1364
1365 // Find the type plane in the module map
1366 TypedPlanes::const_iterator MI = mMap.find(VTy);
1367
Reid Spencer56010e42004-05-26 21:56:09 +00001368 if ( TheFunction ) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001369 // Lookup the type in the function map too
1370 TypedPlanes::const_iterator FI = fMap.find(VTy);
1371 // If there is a corresponding type plane in the function map
1372 if ( FI != fMap.end() ) {
1373 // Lookup the Value in the function map
1374 ValueMap::const_iterator FVI = FI->second.map.find(V);
1375 // If the value doesn't exist in the function map
1376 if ( FVI == FI->second.map.end() ) {
Chris Lattner68a038e2004-06-09 22:22:10 +00001377 // Look up the value in the module map.
1378 if (MI == mMap.end()) return -1;
Reid Spencer56010e42004-05-26 21:56:09 +00001379 ValueMap::const_iterator MVI = MI->second.map.find(V);
1380 // If we didn't find it, it wasn't inserted
Chris Lattner757ee0b2004-06-09 19:41:19 +00001381 if (MVI == MI->second.map.end()) return -1;
Reid Spencer56010e42004-05-26 21:56:09 +00001382 assert( MVI != MI->second.map.end() && "Value not found");
1383 // We found it only at the module level
1384 return MVI->second;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001385
1386 // else the value exists in the function map
1387 } else {
Reid Spencer56010e42004-05-26 21:56:09 +00001388 // Return the slot number as the module's contribution to
1389 // the type plane plus the index in the function's contribution
1390 // to the type plane.
Chris Lattnerb1f04782004-06-15 21:07:32 +00001391 if (MI != mMap.end())
1392 return MI->second.next_slot + FVI->second;
1393 else
1394 return FVI->second;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001395 }
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001396 }
1397 }
1398
Reid Spencer8beac692004-06-09 15:26:53 +00001399 // N.B. Can get here only if either !TheFunction or the function doesn't
1400 // have a corresponding type plane for the Value
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001401
1402 // Make sure the type plane exists
Chris Lattner757ee0b2004-06-09 19:41:19 +00001403 if (MI == mMap.end()) return -1;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001404 // Lookup the value in the module's map
1405 ValueMap::const_iterator MVI = MI->second.map.find(V);
1406 // Make sure we found it.
Chris Lattner757ee0b2004-06-09 19:41:19 +00001407 if (MVI == MI->second.map.end()) return -1;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001408 // Return it.
1409 return MVI->second;
1410}
1411
Reid Spencer58d30f22004-07-04 11:50:43 +00001412/// Get the slot number for a value. This function will assert if you
1413/// ask for a Value that hasn't previously been inserted with createSlot.
1414/// Types are forbidden because Type does not inherit from Value (any more).
1415int SlotMachine::getSlot(const Type *Ty) {
1416 assert( Ty && "Can't get slot for null Type" );
1417
1418 // Check for uninitialized state and do lazy initialization
1419 this->initialize();
1420
1421 if ( TheFunction ) {
1422 // Lookup the Type in the function map
1423 TypeMap::const_iterator FTI = fTypes.map.find(Ty);
1424 // If the Type doesn't exist in the function map
1425 if ( FTI == fTypes.map.end() ) {
1426 TypeMap::const_iterator MTI = mTypes.map.find(Ty);
1427 // If we didn't find it, it wasn't inserted
1428 if (MTI == mTypes.map.end())
1429 return -1;
1430 // We found it only at the module level
1431 return MTI->second;
1432
1433 // else the value exists in the function map
1434 } else {
1435 // Return the slot number as the module's contribution to
1436 // the type plane plus the index in the function's contribution
1437 // to the type plane.
1438 return mTypes.next_slot + FTI->second;
1439 }
1440 }
1441
1442 // N.B. Can get here only if either !TheFunction
1443
1444 // Lookup the value in the module's map
1445 TypeMap::const_iterator MTI = mTypes.map.find(Ty);
1446 // Make sure we found it.
1447 if (MTI == mTypes.map.end()) return -1;
1448 // Return it.
1449 return MTI->second;
1450}
1451
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001452// Create a new slot, or return the existing slot if it is already
1453// inserted. Note that the logic here parallels getSlot but instead
1454// of asserting when the Value* isn't found, it inserts the value.
1455unsigned SlotMachine::createSlot(const Value *V) {
1456 assert( V && "Can't insert a null Value to SlotMachine");
Reid Spencer56010e42004-05-26 21:56:09 +00001457 assert(!isa<Constant>(V) || isa<GlobalValue>(V) &&
1458 "Can't insert a non-GlobalValue Constant into SlotMachine");
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001459
1460 const Type* VTy = V->getType();
1461
1462 // Just ignore void typed things
1463 if (VTy == Type::VoidTy) return 0; // FIXME: Wrong return value!
1464
1465 // Look up the type plane for the Value's type from the module map
1466 TypedPlanes::const_iterator MI = mMap.find(VTy);
1467
Reid Spencer56010e42004-05-26 21:56:09 +00001468 if ( TheFunction ) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001469 // Get the type plane for the Value's type from the function map
1470 TypedPlanes::const_iterator FI = fMap.find(VTy);
1471 // If there is a corresponding type plane in the function map
1472 if ( FI != fMap.end() ) {
1473 // Lookup the Value in the function map
1474 ValueMap::const_iterator FVI = FI->second.map.find(V);
1475 // If the value doesn't exist in the function map
1476 if ( FVI == FI->second.map.end() ) {
Reid Spencer56010e42004-05-26 21:56:09 +00001477 // If there is no corresponding type plane in the module map
1478 if ( MI == mMap.end() )
1479 return insertValue(V);
1480 // Look up the value in the module map
1481 ValueMap::const_iterator MVI = MI->second.map.find(V);
1482 // If we didn't find it, it wasn't inserted
1483 if ( MVI == MI->second.map.end() )
1484 return insertValue(V);
1485 else
1486 // We found it only at the module level
1487 return MVI->second;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001488
1489 // else the value exists in the function map
1490 } else {
Reid Spencer56010e42004-05-26 21:56:09 +00001491 if ( MI == mMap.end() )
1492 return FVI->second;
1493 else
1494 // Return the slot number as the module's contribution to
1495 // the type plane plus the index in the function's contribution
1496 // to the type plane.
1497 return MI->second.next_slot + FVI->second;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001498 }
1499
1500 // else there is not a corresponding type plane in the function map
1501 } else {
1502 // If the type plane doesn't exists at the module level
1503 if ( MI == mMap.end() ) {
Reid Spencer56010e42004-05-26 21:56:09 +00001504 return insertValue(V);
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001505 // else type plane exists at the module level, examine it
1506 } else {
Reid Spencer56010e42004-05-26 21:56:09 +00001507 // Look up the value in the module's map
1508 ValueMap::const_iterator MVI = MI->second.map.find(V);
1509 // If we didn't find it there either
1510 if ( MVI == MI->second.map.end() )
1511 // Return the slot number as the module's contribution to
1512 // the type plane plus the index of the function map insertion.
1513 return MI->second.next_slot + insertValue(V);
1514 else
1515 return MVI->second;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001516 }
1517 }
1518 }
1519
Reid Spencer56010e42004-05-26 21:56:09 +00001520 // N.B. Can only get here if !TheFunction
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001521
1522 // If the module map's type plane is not for the Value's type
1523 if ( MI != mMap.end() ) {
1524 // Lookup the value in the module's map
1525 ValueMap::const_iterator MVI = MI->second.map.find(V);
1526 if ( MVI != MI->second.map.end() )
1527 return MVI->second;
1528 }
1529
1530 return insertValue(V);
1531}
1532
Reid Spencer58d30f22004-07-04 11:50:43 +00001533// Create a new slot, or return the existing slot if it is already
1534// inserted. Note that the logic here parallels getSlot but instead
1535// of asserting when the Value* isn't found, it inserts the value.
1536unsigned SlotMachine::createSlot(const Type *Ty) {
1537 assert( Ty && "Can't insert a null Type to SlotMachine");
1538
1539 if ( TheFunction ) {
1540 // Lookup the Type in the function map
1541 TypeMap::const_iterator FTI = fTypes.map.find(Ty);
1542 // If the type doesn't exist in the function map
1543 if ( FTI == fTypes.map.end() ) {
1544 // Look up the type in the module map
1545 TypeMap::const_iterator MTI = mTypes.map.find(Ty);
1546 // If we didn't find it, it wasn't inserted
1547 if ( MTI == mTypes.map.end() )
1548 return insertValue(Ty);
1549 else
1550 // We found it only at the module level
1551 return MTI->second;
1552
1553 // else the value exists in the function map
1554 } else {
1555 // Return the slot number as the module's contribution to
1556 // the type plane plus the index in the function's contribution
1557 // to the type plane.
1558 return mTypes.next_slot + FTI->second;
1559 }
1560 }
1561
1562 // N.B. Can only get here if !TheFunction
1563
1564 // Lookup the type in the module's map
1565 TypeMap::const_iterator MTI = mTypes.map.find(Ty);
1566 if ( MTI != mTypes.map.end() )
1567 return MTI->second;
1568
1569 return insertValue(Ty);
1570}
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001571
1572// Low level insert function. Minimal checking is done. This
1573// function is just for the convenience of createSlot (above).
1574unsigned SlotMachine::insertValue(const Value *V ) {
1575 assert(V && "Can't insert a null Value into SlotMachine!");
Reid Spencer56010e42004-05-26 21:56:09 +00001576 assert(!isa<Constant>(V) || isa<GlobalValue>(V) &&
1577 "Can't insert a non-GlobalValue Constant into SlotMachine");
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001578
Reid Spencer56010e42004-05-26 21:56:09 +00001579 // If this value does not contribute to a plane (is void)
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001580 // or if the value already has a name then ignore it.
Reid Spencer56010e42004-05-26 21:56:09 +00001581 if (V->getType() == Type::VoidTy || V->hasName() ) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001582 SC_DEBUG("ignored value " << *V << "\n");
1583 return 0; // FIXME: Wrong return value
1584 }
1585
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001586 const Type *VTy = V->getType();
1587 unsigned DestSlot = 0;
1588
Reid Spencer56010e42004-05-26 21:56:09 +00001589 if ( TheFunction ) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001590 TypedPlanes::iterator I = fMap.find( VTy );
1591 if ( I == fMap.end() )
Reid Spencer58d30f22004-07-04 11:50:43 +00001592 I = fMap.insert(std::make_pair(VTy,ValuePlane())).first;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001593 DestSlot = I->second.map[V] = I->second.next_slot++;
1594 } else {
1595 TypedPlanes::iterator I = mMap.find( VTy );
1596 if ( I == mMap.end() )
Reid Spencer58d30f22004-07-04 11:50:43 +00001597 I = mMap.insert(std::make_pair(VTy,ValuePlane())).first;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001598 DestSlot = I->second.map[V] = I->second.next_slot++;
1599 }
1600
1601 SC_DEBUG(" Inserting value [" << VTy << "] = " << V << " slot=" <<
Reid Spencer56010e42004-05-26 21:56:09 +00001602 DestSlot << " [");
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001603 // G = Global, C = Constant, T = Type, F = Function, o = other
Reid Spenceraccd7c72004-07-17 23:47:01 +00001604 SC_DEBUG((isa<GlobalVariable>(V) ? 'G' : (isa<Function>(V) ? 'F' :
1605 (isa<Constant>(V) ? 'C' : 'o'))));
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001606 SC_DEBUG("]\n");
1607 return DestSlot;
1608}
1609
Reid Spencer58d30f22004-07-04 11:50:43 +00001610// Low level insert function. Minimal checking is done. This
1611// function is just for the convenience of createSlot (above).
1612unsigned SlotMachine::insertValue(const Type *Ty ) {
1613 assert(Ty && "Can't insert a null Type into SlotMachine!");
1614
1615 unsigned DestSlot = 0;
1616
1617 if ( TheFunction ) {
1618 DestSlot = fTypes.map[Ty] = fTypes.next_slot++;
1619 } else {
1620 DestSlot = fTypes.map[Ty] = fTypes.next_slot++;
1621 }
1622 SC_DEBUG(" Inserting type [" << DestSlot << "] = " << Ty << "\n");
1623 return DestSlot;
1624}
1625
Reid Spencere7e96712004-05-25 08:53:40 +00001626// vim: sw=2