blob: df5cabc75697289c429dd6a398ed3a7cf047bf3e [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 Spencer48f98c82004-07-25 21:44:54 +0000735 if (!M->getTargetTriple().empty())
Reid Spencerffec7df2004-07-25 21:29:43 +0000736 Out << "target triple = \"" << M->getTargetTriple() << "\"\n";
Reid Spencercc5ff642004-07-25 18:08:18 +0000737
738 // Loop over the dependent libraries and emit them
Reid Spencer48f98c82004-07-25 21:44:54 +0000739 Module::lib_iterator LI= M->lib_begin();
740 Module::lib_iterator LE= M->lib_end();
741 if (LI != LE) {
Reid Spencerffec7df2004-07-25 21:29:43 +0000742 Out << "deplibs = [\n";
Reid Spencer48f98c82004-07-25 21:44:54 +0000743 while ( LI != LE ) {
Reid Spencerffec7df2004-07-25 21:29:43 +0000744 Out << "\"" << *LI << "\"";
745 ++LI;
746 if ( LI != LE )
747 Out << ",\n";
748 }
749 Out << " ]\n";
Reid Spencercc5ff642004-07-25 18:08:18 +0000750 }
Chris Lattner8068e0c2003-08-24 13:48:48 +0000751
Chris Lattnerfee714f2001-09-07 16:36:04 +0000752 // Loop over the symbol table, emitting all named constants...
Chris Lattner98cf1f52002-11-20 18:36:02 +0000753 printSymbolTable(M->getSymbolTable());
Chris Lattnerda975502001-09-10 07:58:01 +0000754
Chris Lattner113f4f42002-06-25 16:13:24 +0000755 for (Module::const_giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
756 printGlobal(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000757
Misha Brukmana6619a92004-06-21 21:53:56 +0000758 Out << "\nimplementation ; Functions:\n";
Vikram S. Adve13ba19a2001-09-18 12:48:16 +0000759
Chris Lattner6915f8f2002-04-07 22:49:37 +0000760 // Output all of the functions...
Chris Lattner113f4f42002-06-25 16:13:24 +0000761 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
762 printFunction(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000763}
764
Chris Lattner7bfee412001-10-29 16:05:51 +0000765void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000766 if (GV->hasName()) Out << getLLVMName(GV->getName()) << " = ";
Chris Lattner37798642001-09-18 04:01:05 +0000767
Chris Lattner379a8d22003-04-16 20:28:45 +0000768 if (!GV->hasInitializer())
Misha Brukmana6619a92004-06-21 21:53:56 +0000769 Out << "external ";
Chris Lattner379a8d22003-04-16 20:28:45 +0000770 else
771 switch (GV->getLinkage()) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000772 case GlobalValue::InternalLinkage: Out << "internal "; break;
773 case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
774 case GlobalValue::WeakLinkage: Out << "weak "; break;
775 case GlobalValue::AppendingLinkage: Out << "appending "; break;
Chris Lattner379a8d22003-04-16 20:28:45 +0000776 case GlobalValue::ExternalLinkage: break;
777 }
Chris Lattner37798642001-09-18 04:01:05 +0000778
Misha Brukmana6619a92004-06-21 21:53:56 +0000779 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattner2413b162001-12-04 00:03:30 +0000780 printType(GV->getType()->getElementType());
Chris Lattner37798642001-09-18 04:01:05 +0000781
Reid Spenceraccd7c72004-07-17 23:47:01 +0000782 if (GV->hasInitializer()) {
783 Constant* C = cast<Constant>(GV->getInitializer());
784 assert(C && "GlobalVar initializer isn't constant?");
Reid Spencerc9c90cf2004-07-18 01:04:19 +0000785 writeOperand(GV->getInitializer(), false, isa<GlobalValue>(C));
Reid Spenceraccd7c72004-07-17 23:47:01 +0000786 }
Chris Lattner37798642001-09-18 04:01:05 +0000787
Chris Lattner113f4f42002-06-25 16:13:24 +0000788 printInfoComment(*GV);
Misha Brukmana6619a92004-06-21 21:53:56 +0000789 Out << "\n";
Chris Lattnerda975502001-09-10 07:58:01 +0000790}
791
Chris Lattnerfee714f2001-09-07 16:36:04 +0000792
Reid Spencere7e96712004-05-25 08:53:40 +0000793// printSymbolTable - Run through symbol table looking for constants
794// and types. Emit their declarations.
Chris Lattner7bfee412001-10-29 16:05:51 +0000795void AssemblyWriter::printSymbolTable(const SymbolTable &ST) {
Reid Spencere7e96712004-05-25 08:53:40 +0000796
797 // Print the types.
798 for (SymbolTable::type_const_iterator TI = ST.type_begin();
799 TI != ST.type_end(); ++TI ) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000800 Out << "\t" << getLLVMName(TI->first) << " = type ";
Reid Spencere7e96712004-05-25 08:53:40 +0000801
802 // Make sure we print out at least one level of the type structure, so
803 // that we do not get %FILE = type %FILE
804 //
805 printTypeAtLeastOneLevel(TI->second) << "\n";
806 }
Chris Lattnerfee714f2001-09-07 16:36:04 +0000807
Reid Spencere7e96712004-05-25 08:53:40 +0000808 // Print the constants, in type plane order.
809 for (SymbolTable::plane_const_iterator PI = ST.plane_begin();
810 PI != ST.plane_end(); ++PI ) {
811 SymbolTable::value_const_iterator VI = ST.value_begin(PI->first);
812 SymbolTable::value_const_iterator VE = ST.value_end(PI->first);
813
814 for (; VI != VE; ++VI) {
Reid Spenceraccd7c72004-07-17 23:47:01 +0000815 const Value* V = VI->second;
816 const Constant *CPV = dyn_cast<Constant>(V) ;
817 if (CPV && !isa<GlobalValue>(V)) {
Reid Spencer56010e42004-05-26 21:56:09 +0000818 printConstant(CPV);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000819 }
820 }
Chris Lattnera7620d92001-07-15 06:35:59 +0000821 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000822}
823
824
Misha Brukmanc566ca362004-03-02 00:22:19 +0000825/// printConstant - Print out a constant pool entry...
826///
Chris Lattner3462ae32001-12-03 22:26:30 +0000827void AssemblyWriter::printConstant(const Constant *CPV) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000828 // Don't print out unnamed constants, they will be inlined
829 if (!CPV->hasName()) return;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000830
Chris Lattneree998be2001-07-26 16:29:38 +0000831 // Print out name...
Misha Brukmana6619a92004-06-21 21:53:56 +0000832 Out << "\t" << getLLVMName(CPV->getName()) << " =";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000833
834 // Write the value out now...
Chris Lattnerd84bb632002-04-16 21:36:08 +0000835 writeOperand(CPV, true, false);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000836
Chris Lattner113f4f42002-06-25 16:13:24 +0000837 printInfoComment(*CPV);
Misha Brukmana6619a92004-06-21 21:53:56 +0000838 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000839}
840
Misha Brukmanc566ca362004-03-02 00:22:19 +0000841/// printFunction - Print all aspects of a function.
842///
Chris Lattner113f4f42002-06-25 16:13:24 +0000843void AssemblyWriter::printFunction(const Function *F) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000844 // Print out the return type and name...
Misha Brukmana6619a92004-06-21 21:53:56 +0000845 Out << "\n";
Chris Lattner379a8d22003-04-16 20:28:45 +0000846
Misha Brukmana6619a92004-06-21 21:53:56 +0000847 if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +0000848
Chris Lattner379a8d22003-04-16 20:28:45 +0000849 if (F->isExternal())
Misha Brukmana6619a92004-06-21 21:53:56 +0000850 Out << "declare ";
Chris Lattner379a8d22003-04-16 20:28:45 +0000851 else
852 switch (F->getLinkage()) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000853 case GlobalValue::InternalLinkage: Out << "internal "; break;
854 case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
855 case GlobalValue::WeakLinkage: Out << "weak "; break;
856 case GlobalValue::AppendingLinkage: Out << "appending "; break;
Chris Lattner379a8d22003-04-16 20:28:45 +0000857 case GlobalValue::ExternalLinkage: break;
858 }
859
Misha Brukman21bbdb92004-06-04 21:11:51 +0000860 printType(F->getReturnType()) << ' ';
Chris Lattner5b337482003-10-18 05:57:43 +0000861 if (!F->getName().empty())
Misha Brukmana6619a92004-06-21 21:53:56 +0000862 Out << getLLVMName(F->getName());
Chris Lattner5b337482003-10-18 05:57:43 +0000863 else
Misha Brukmana6619a92004-06-21 21:53:56 +0000864 Out << "\"\"";
865 Out << '(';
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000866 Machine.incorporateFunction(F);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000867
Chris Lattner7bfee412001-10-29 16:05:51 +0000868 // Loop over the arguments, printing them...
Chris Lattner113f4f42002-06-25 16:13:24 +0000869 const FunctionType *FT = F->getFunctionType();
Chris Lattnerfee714f2001-09-07 16:36:04 +0000870
Chris Lattner149376d2002-10-13 20:57:00 +0000871 for(Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I)
872 printArgument(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000873
874 // Finish printing arguments...
Chris Lattner113f4f42002-06-25 16:13:24 +0000875 if (FT->isVarArg()) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000876 if (FT->getNumParams()) Out << ", ";
877 Out << "..."; // Output varargs portion of signature!
Chris Lattnerfee714f2001-09-07 16:36:04 +0000878 }
Misha Brukmana6619a92004-06-21 21:53:56 +0000879 Out << ')';
Chris Lattnerfee714f2001-09-07 16:36:04 +0000880
Chris Lattner113f4f42002-06-25 16:13:24 +0000881 if (F->isExternal()) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000882 Out << "\n";
Chris Lattnerb2f02e52002-05-06 03:00:40 +0000883 } else {
Misha Brukmana6619a92004-06-21 21:53:56 +0000884 Out << " {";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000885
Chris Lattner6915f8f2002-04-07 22:49:37 +0000886 // Output all of its basic blocks... for the function
Chris Lattner113f4f42002-06-25 16:13:24 +0000887 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
888 printBasicBlock(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000889
Misha Brukmana6619a92004-06-21 21:53:56 +0000890 Out << "}\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000891 }
892
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000893 Machine.purgeFunction();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000894}
895
Misha Brukmanc566ca362004-03-02 00:22:19 +0000896/// printArgument - This member is called for every argument that is passed into
897/// the function. Simply print it out
898///
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000899void AssemblyWriter::printArgument(const Argument *Arg) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000900 // Insert commas as we go... the first arg doesn't get a comma
Misha Brukmana6619a92004-06-21 21:53:56 +0000901 if (Arg != &Arg->getParent()->afront()) Out << ", ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000902
903 // Output type...
Chris Lattner7bfee412001-10-29 16:05:51 +0000904 printType(Arg->getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000905
906 // Output name, if available...
907 if (Arg->hasName())
Misha Brukmana6619a92004-06-21 21:53:56 +0000908 Out << ' ' << getLLVMName(Arg->getName());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000909}
910
Misha Brukmanc566ca362004-03-02 00:22:19 +0000911/// printBasicBlock - This member is called for each basic block in a method.
912///
Chris Lattner7bfee412001-10-29 16:05:51 +0000913void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000914 if (BB->hasName()) { // Print out the label if it exists...
Misha Brukmana6619a92004-06-21 21:53:56 +0000915 Out << "\n" << BB->getName() << ':';
Chris Lattner408dbdb2002-05-14 16:02:05 +0000916 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Misha Brukmana6619a92004-06-21 21:53:56 +0000917 Out << "\n; <label>:";
Chris Lattner757ee0b2004-06-09 19:41:19 +0000918 int Slot = Machine.getSlot(BB);
919 if (Slot != -1)
Misha Brukmana6619a92004-06-21 21:53:56 +0000920 Out << Slot;
Chris Lattner757ee0b2004-06-09 19:41:19 +0000921 else
Misha Brukmana6619a92004-06-21 21:53:56 +0000922 Out << "<badref>";
Chris Lattner58185f22002-10-02 19:38:55 +0000923 }
Chris Lattner2447ef52003-11-20 00:09:43 +0000924
925 if (BB->getParent() == 0)
Misha Brukmana6619a92004-06-21 21:53:56 +0000926 Out << "\t\t; Error: Block without parent!";
Chris Lattner2447ef52003-11-20 00:09:43 +0000927 else {
928 if (BB != &BB->getParent()->front()) { // Not the entry block?
929 // Output predecessors for the block...
Misha Brukmana6619a92004-06-21 21:53:56 +0000930 Out << "\t\t;";
Chris Lattner2447ef52003-11-20 00:09:43 +0000931 pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
932
933 if (PI == PE) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000934 Out << " No predecessors!";
Chris Lattner2447ef52003-11-20 00:09:43 +0000935 } else {
Misha Brukmana6619a92004-06-21 21:53:56 +0000936 Out << " preds =";
Chris Lattner00211f12003-11-16 22:59:57 +0000937 writeOperand(*PI, false, true);
Chris Lattner2447ef52003-11-20 00:09:43 +0000938 for (++PI; PI != PE; ++PI) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000939 Out << ',';
Chris Lattner2447ef52003-11-20 00:09:43 +0000940 writeOperand(*PI, false, true);
941 }
Chris Lattner00211f12003-11-16 22:59:57 +0000942 }
Chris Lattner58185f22002-10-02 19:38:55 +0000943 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000944 }
Chris Lattner408dbdb2002-05-14 16:02:05 +0000945
Misha Brukmana6619a92004-06-21 21:53:56 +0000946 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000947
Misha Brukmana6619a92004-06-21 21:53:56 +0000948 if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +0000949
Chris Lattnerfee714f2001-09-07 16:36:04 +0000950 // Output all of the instructions in the basic block...
Chris Lattner113f4f42002-06-25 16:13:24 +0000951 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
952 printInstruction(*I);
Chris Lattner96cdd272004-03-08 18:51:45 +0000953
Misha Brukmana6619a92004-06-21 21:53:56 +0000954 if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000955}
956
Chris Lattner862e3382001-10-13 06:42:36 +0000957
Misha Brukmanc566ca362004-03-02 00:22:19 +0000958/// printInfoComment - Print a little comment after the instruction indicating
959/// which slot it occupies.
960///
Chris Lattner113f4f42002-06-25 16:13:24 +0000961void AssemblyWriter::printInfoComment(const Value &V) {
962 if (V.getType() != Type::VoidTy) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000963 Out << "\t\t; <";
Misha Brukman21bbdb92004-06-04 21:11:51 +0000964 printType(V.getType()) << '>';
Chris Lattner862e3382001-10-13 06:42:36 +0000965
Chris Lattner113f4f42002-06-25 16:13:24 +0000966 if (!V.hasName()) {
Chris Lattner757ee0b2004-06-09 19:41:19 +0000967 int SlotNum = Machine.getSlot(&V);
968 if (SlotNum == -1)
Misha Brukmana6619a92004-06-21 21:53:56 +0000969 Out << ":<badref>";
Reid Spencer8beac692004-06-09 15:26:53 +0000970 else
Misha Brukmana6619a92004-06-21 21:53:56 +0000971 Out << ':' << SlotNum; // Print out the def slot taken.
Chris Lattner862e3382001-10-13 06:42:36 +0000972 }
Misha Brukmana6619a92004-06-21 21:53:56 +0000973 Out << " [#uses=" << V.use_size() << ']'; // Output # uses
Chris Lattner862e3382001-10-13 06:42:36 +0000974 }
975}
976
Reid Spencer8beac692004-06-09 15:26:53 +0000977/// printInstruction - This member is called for each Instruction in a function..
Misha Brukmanc566ca362004-03-02 00:22:19 +0000978///
Chris Lattner113f4f42002-06-25 16:13:24 +0000979void AssemblyWriter::printInstruction(const Instruction &I) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000980 if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +0000981
Misha Brukmana6619a92004-06-21 21:53:56 +0000982 Out << "\t";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000983
984 // Print out name if it exists...
Chris Lattner113f4f42002-06-25 16:13:24 +0000985 if (I.hasName())
Misha Brukmana6619a92004-06-21 21:53:56 +0000986 Out << getLLVMName(I.getName()) << " = ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000987
Chris Lattner504f9242003-09-08 17:45:59 +0000988 // If this is a volatile load or store, print out the volatile marker
989 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
990 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile()))
Misha Brukmana6619a92004-06-21 21:53:56 +0000991 Out << "volatile ";
Chris Lattner504f9242003-09-08 17:45:59 +0000992
Chris Lattner2f7c9632001-06-06 20:29:01 +0000993 // Print out the opcode...
Misha Brukmana6619a92004-06-21 21:53:56 +0000994 Out << I.getOpcodeName();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000995
996 // Print out the type of the operands...
Chris Lattner113f4f42002-06-25 16:13:24 +0000997 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000998
999 // Special case conditional branches to swizzle the condition out to the front
Chris Lattner113f4f42002-06-25 16:13:24 +00001000 if (isa<BranchInst>(I) && I.getNumOperands() > 1) {
1001 writeOperand(I.getOperand(2), true);
Misha Brukmana6619a92004-06-21 21:53:56 +00001002 Out << ',';
Chris Lattner2f7c9632001-06-06 20:29:01 +00001003 writeOperand(Operand, true);
Misha Brukmana6619a92004-06-21 21:53:56 +00001004 Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001005 writeOperand(I.getOperand(1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001006
Chris Lattner8d48df22002-04-13 18:34:38 +00001007 } else if (isa<SwitchInst>(I)) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001008 // Special case switch statement to get formatting nice and correct...
Misha Brukmana6619a92004-06-21 21:53:56 +00001009 writeOperand(Operand , true); Out << ',';
1010 writeOperand(I.getOperand(1), true); Out << " [";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001011
Chris Lattner113f4f42002-06-25 16:13:24 +00001012 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001013 Out << "\n\t\t";
1014 writeOperand(I.getOperand(op ), true); Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001015 writeOperand(I.getOperand(op+1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001016 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001017 Out << "\n\t]";
Chris Lattnerda558102001-10-02 03:41:24 +00001018 } else if (isa<PHINode>(I)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001019 Out << ' ';
Chris Lattner113f4f42002-06-25 16:13:24 +00001020 printType(I.getType());
Misha Brukmana6619a92004-06-21 21:53:56 +00001021 Out << ' ';
Chris Lattner2f7c9632001-06-06 20:29:01 +00001022
Chris Lattner113f4f42002-06-25 16:13:24 +00001023 for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001024 if (op) Out << ", ";
1025 Out << '[';
1026 writeOperand(I.getOperand(op ), false); Out << ',';
1027 writeOperand(I.getOperand(op+1), false); Out << " ]";
Chris Lattner931ef3b2001-06-11 15:04:20 +00001028 }
Chris Lattner862e3382001-10-13 06:42:36 +00001029 } else if (isa<ReturnInst>(I) && !Operand) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001030 Out << " void";
Chris Lattner862e3382001-10-13 06:42:36 +00001031 } else if (isa<CallInst>(I)) {
Chris Lattner463d6a52003-08-05 15:34:45 +00001032 const PointerType *PTy = cast<PointerType>(Operand->getType());
1033 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1034 const Type *RetTy = FTy->getReturnType();
Chris Lattner2f2d9472001-11-06 21:28:12 +00001035
Chris Lattner463d6a52003-08-05 15:34:45 +00001036 // If possible, print out the short form of the call instruction. We can
Chris Lattner6915f8f2002-04-07 22:49:37 +00001037 // only do this if the first argument is a pointer to a nonvararg function,
Chris Lattner463d6a52003-08-05 15:34:45 +00001038 // and if the return type is not a pointer to a function.
Chris Lattner2f2d9472001-11-06 21:28:12 +00001039 //
Chris Lattner463d6a52003-08-05 15:34:45 +00001040 if (!FTy->isVarArg() &&
Chris Lattner8d48df22002-04-13 18:34:38 +00001041 (!isa<PointerType>(RetTy) ||
Chris Lattnerd9a36a62002-07-25 20:58:51 +00001042 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001043 Out << ' '; printType(RetTy);
Chris Lattner2f2d9472001-11-06 21:28:12 +00001044 writeOperand(Operand, false);
1045 } else {
1046 writeOperand(Operand, true);
1047 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001048 Out << '(';
Chris Lattner113f4f42002-06-25 16:13:24 +00001049 if (I.getNumOperands() > 1) writeOperand(I.getOperand(1), true);
1050 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; ++op) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001051 Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001052 writeOperand(I.getOperand(op), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001053 }
1054
Misha Brukmana6619a92004-06-21 21:53:56 +00001055 Out << " )";
Chris Lattner113f4f42002-06-25 16:13:24 +00001056 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Chris Lattner463d6a52003-08-05 15:34:45 +00001057 const PointerType *PTy = cast<PointerType>(Operand->getType());
1058 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1059 const Type *RetTy = FTy->getReturnType();
1060
1061 // If possible, print out the short form of the invoke instruction. We can
1062 // only do this if the first argument is a pointer to a nonvararg function,
1063 // and if the return type is not a pointer to a function.
1064 //
1065 if (!FTy->isVarArg() &&
1066 (!isa<PointerType>(RetTy) ||
1067 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001068 Out << ' '; printType(RetTy);
Chris Lattner463d6a52003-08-05 15:34:45 +00001069 writeOperand(Operand, false);
1070 } else {
1071 writeOperand(Operand, true);
1072 }
1073
Misha Brukmana6619a92004-06-21 21:53:56 +00001074 Out << '(';
Chris Lattner113f4f42002-06-25 16:13:24 +00001075 if (I.getNumOperands() > 3) writeOperand(I.getOperand(3), true);
1076 for (unsigned op = 4, Eop = I.getNumOperands(); op < Eop; ++op) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001077 Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001078 writeOperand(I.getOperand(op), true);
Chris Lattner862e3382001-10-13 06:42:36 +00001079 }
1080
Misha Brukmana6619a92004-06-21 21:53:56 +00001081 Out << " )\n\t\t\tto";
Chris Lattner862e3382001-10-13 06:42:36 +00001082 writeOperand(II->getNormalDest(), true);
Misha Brukmana6619a92004-06-21 21:53:56 +00001083 Out << " unwind";
Chris Lattnerfae8ab32004-02-08 21:44:31 +00001084 writeOperand(II->getUnwindDest(), true);
Chris Lattner862e3382001-10-13 06:42:36 +00001085
Chris Lattner113f4f42002-06-25 16:13:24 +00001086 } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(&I)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001087 Out << ' ';
Chris Lattner8d48df22002-04-13 18:34:38 +00001088 printType(AI->getType()->getElementType());
1089 if (AI->isArrayAllocation()) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001090 Out << ',';
Chris Lattner8d48df22002-04-13 18:34:38 +00001091 writeOperand(AI->getArraySize(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001092 }
Chris Lattner862e3382001-10-13 06:42:36 +00001093 } else if (isa<CastInst>(I)) {
Chris Lattnerc96e96b2003-11-17 01:17:04 +00001094 if (Operand) writeOperand(Operand, true); // Work with broken code
Misha Brukmana6619a92004-06-21 21:53:56 +00001095 Out << " to ";
Chris Lattner113f4f42002-06-25 16:13:24 +00001096 printType(I.getType());
Chris Lattner5b337482003-10-18 05:57:43 +00001097 } else if (isa<VAArgInst>(I)) {
Chris Lattnerc96e96b2003-11-17 01:17:04 +00001098 if (Operand) writeOperand(Operand, true); // Work with broken code
Misha Brukmana6619a92004-06-21 21:53:56 +00001099 Out << ", ";
Chris Lattnerf70da102003-05-08 02:44:12 +00001100 printType(I.getType());
Chris Lattner5b337482003-10-18 05:57:43 +00001101 } else if (const VANextInst *VAN = dyn_cast<VANextInst>(&I)) {
Chris Lattnerc96e96b2003-11-17 01:17:04 +00001102 if (Operand) writeOperand(Operand, true); // Work with broken code
Misha Brukmana6619a92004-06-21 21:53:56 +00001103 Out << ", ";
Chris Lattner5b337482003-10-18 05:57:43 +00001104 printType(VAN->getArgType());
Chris Lattner2f7c9632001-06-06 20:29:01 +00001105 } else if (Operand) { // Print the normal way...
1106
1107 // PrintAllTypes - Instructions who have operands of all the same type
1108 // omit the type from all but the first operand. If the instruction has
1109 // different type operands (for example br), then they are all printed.
1110 bool PrintAllTypes = false;
1111 const Type *TheType = Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001112
Chris Lattner52bd5cb92004-03-12 05:53:14 +00001113 // Shift Left & Right print both types even for Ubyte LHS, and select prints
1114 // types even if all operands are bools.
1115 if (isa<ShiftInst>(I) || isa<SelectInst>(I)) {
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00001116 PrintAllTypes = true;
1117 } else {
1118 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
1119 Operand = I.getOperand(i);
1120 if (Operand->getType() != TheType) {
1121 PrintAllTypes = true; // We have differing types! Print them all!
1122 break;
1123 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001124 }
1125 }
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00001126
Chris Lattner7bfee412001-10-29 16:05:51 +00001127 if (!PrintAllTypes) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001128 Out << ' ';
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00001129 printType(TheType);
Chris Lattner7bfee412001-10-29 16:05:51 +00001130 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001131
Chris Lattner113f4f42002-06-25 16:13:24 +00001132 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001133 if (i) Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001134 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001135 }
1136 }
1137
Chris Lattner862e3382001-10-13 06:42:36 +00001138 printInfoComment(I);
Misha Brukmana6619a92004-06-21 21:53:56 +00001139 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001140}
1141
1142
1143//===----------------------------------------------------------------------===//
1144// External Interface declarations
1145//===----------------------------------------------------------------------===//
1146
Chris Lattner8339f7d2003-10-30 23:41:03 +00001147void Module::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001148 SlotMachine SlotTable(this);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001149 AssemblyWriter W(o, SlotTable, this, AAW);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001150 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001151}
1152
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001153void GlobalVariable::print(std::ostream &o) const {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001154 SlotMachine SlotTable(getParent());
Chris Lattner8339f7d2003-10-30 23:41:03 +00001155 AssemblyWriter W(o, SlotTable, getParent(), 0);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001156 W.write(this);
Chris Lattneradfe0d12001-09-10 20:08:19 +00001157}
1158
Chris Lattner8339f7d2003-10-30 23:41:03 +00001159void Function::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001160 SlotMachine SlotTable(getParent());
Chris Lattner8339f7d2003-10-30 23:41:03 +00001161 AssemblyWriter W(o, SlotTable, getParent(), AAW);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001162
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001163 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001164}
1165
Chris Lattner8339f7d2003-10-30 23:41:03 +00001166void BasicBlock::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001167 SlotMachine SlotTable(getParent());
Chris Lattner7bfee412001-10-29 16:05:51 +00001168 AssemblyWriter W(o, SlotTable,
Chris Lattner8339f7d2003-10-30 23:41:03 +00001169 getParent() ? getParent()->getParent() : 0, AAW);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001170 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001171}
1172
Chris Lattner8339f7d2003-10-30 23:41:03 +00001173void Instruction::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001174 const Function *F = getParent() ? getParent()->getParent() : 0;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001175 SlotMachine SlotTable(F);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001176 AssemblyWriter W(o, SlotTable, F ? F->getParent() : 0, AAW);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001177
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001178 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001179}
Chris Lattner7db79582001-11-07 04:21:57 +00001180
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001181void Constant::print(std::ostream &o) const {
1182 if (this == 0) { o << "<null> constant value\n"; return; }
Chris Lattner7d734802002-09-10 15:53:49 +00001183
Misha Brukman21bbdb92004-06-04 21:11:51 +00001184 o << ' ' << getType()->getDescription() << ' ';
Chris Lattner1e194682002-04-18 18:53:13 +00001185
Chris Lattnerab7d1ab2003-05-08 02:08:14 +00001186 std::map<const Type *, std::string> TypeTable;
Chris Lattner1e194682002-04-18 18:53:13 +00001187 WriteConstantInt(o, this, false, TypeTable, 0);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001188}
1189
1190void Type::print(std::ostream &o) const {
1191 if (this == 0)
1192 o << "<null Type>";
1193 else
1194 o << getDescription();
1195}
1196
Chris Lattner2e9fa6d2002-04-09 19:48:49 +00001197void Argument::print(std::ostream &o) const {
Chris Lattnere52ed452004-07-13 23:14:34 +00001198 WriteAsOperand(o, this, true, true,
1199 getParent() ? getParent()->getParent() : 0);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001200}
1201
Reid Spencer52641832004-05-25 18:14:38 +00001202// Value::dump - allow easy printing of Values from the debugger.
1203// Located here because so much of the needed functionality is here.
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001204void Value::dump() const { print(std::cerr); }
Reid Spencer52641832004-05-25 18:14:38 +00001205
1206// Type::dump - allow easy printing of Values from the debugger.
1207// Located here because so much of the needed functionality is here.
Reid Spencere7e96712004-05-25 08:53:40 +00001208void Type::dump() const { print(std::cerr); }
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001209
1210//===----------------------------------------------------------------------===//
1211// CachedWriter Class Implementation
1212//===----------------------------------------------------------------------===//
1213
Chris Lattner7db79582001-11-07 04:21:57 +00001214void CachedWriter::setModule(const Module *M) {
1215 delete SC; delete AW;
1216 if (M) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001217 SC = new SlotMachine(M );
Misha Brukman21bbdb92004-06-04 21:11:51 +00001218 AW = new AssemblyWriter(Out, *SC, M, 0);
Chris Lattner7db79582001-11-07 04:21:57 +00001219 } else {
1220 SC = 0; AW = 0;
1221 }
1222}
1223
1224CachedWriter::~CachedWriter() {
1225 delete AW;
1226 delete SC;
1227}
1228
Chris Lattner60a7dd12004-07-15 02:51:31 +00001229CachedWriter &CachedWriter::operator<<(const Value &V) {
Chris Lattner7db79582001-11-07 04:21:57 +00001230 assert(AW && SC && "CachedWriter does not have a current module!");
Chris Lattner60a7dd12004-07-15 02:51:31 +00001231 if (const Instruction *I = dyn_cast<Instruction>(&V))
Chris Lattner80d2d532004-06-26 19:40:40 +00001232 AW->write(I);
Chris Lattner60a7dd12004-07-15 02:51:31 +00001233 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(&V))
Chris Lattner80d2d532004-06-26 19:40:40 +00001234 AW->write(BB);
Chris Lattner60a7dd12004-07-15 02:51:31 +00001235 else if (const Function *F = dyn_cast<Function>(&V))
Chris Lattner80d2d532004-06-26 19:40:40 +00001236 AW->write(F);
Chris Lattner60a7dd12004-07-15 02:51:31 +00001237 else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(&V))
Chris Lattner80d2d532004-06-26 19:40:40 +00001238 AW->write(GV);
Chris Lattner80d2d532004-06-26 19:40:40 +00001239 else
Chris Lattner60a7dd12004-07-15 02:51:31 +00001240 AW->writeOperand(&V, true, true);
Chris Lattner7db79582001-11-07 04:21:57 +00001241 return *this;
1242}
Misha Brukman4685e262004-04-28 15:31:21 +00001243
Chris Lattner60a7dd12004-07-15 02:51:31 +00001244CachedWriter& CachedWriter::operator<<(const Type &Ty) {
Misha Brukman4685e262004-04-28 15:31:21 +00001245 if (SymbolicTypes) {
1246 const Module *M = AW->getModule();
Chris Lattner60a7dd12004-07-15 02:51:31 +00001247 if (M) WriteTypeSymbolic(Out, &Ty, M);
Reid Spencer58d30f22004-07-04 11:50:43 +00001248 } else {
Chris Lattner60a7dd12004-07-15 02:51:31 +00001249 AW->write(&Ty);
Reid Spencer58d30f22004-07-04 11:50:43 +00001250 }
1251 return *this;
Misha Brukman4685e262004-04-28 15:31:21 +00001252}
Misha Brukmanda546ea2004-04-28 19:24:28 +00001253
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001254//===----------------------------------------------------------------------===//
1255//===-- SlotMachine Implementation
1256//===----------------------------------------------------------------------===//
1257
1258#if 0
1259#define SC_DEBUG(X) std::cerr << X
1260#else
1261#define SC_DEBUG(X)
1262#endif
1263
1264// Module level constructor. Causes the contents of the Module (sans functions)
1265// to be added to the slot table.
1266SlotMachine::SlotMachine(const Module *M)
Reid Spencer56010e42004-05-26 21:56:09 +00001267 : TheModule(M) ///< Saved for lazy initialization.
1268 , TheFunction(0)
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001269 , mMap()
Reid Spencer58d30f22004-07-04 11:50:43 +00001270 , mTypes()
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001271 , fMap()
Reid Spencer58d30f22004-07-04 11:50:43 +00001272 , fTypes()
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001273{
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001274}
1275
1276// Function level constructor. Causes the contents of the Module and the one
1277// function provided to be added to the slot table.
1278SlotMachine::SlotMachine(const Function *F )
Reid Spencer56010e42004-05-26 21:56:09 +00001279 : TheModule( F ? F->getParent() : 0 ) ///< Saved for lazy initialization
1280 , TheFunction(F) ///< Saved for lazy initialization
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001281 , mMap()
Reid Spencer58d30f22004-07-04 11:50:43 +00001282 , mTypes()
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001283 , fMap()
Reid Spencer58d30f22004-07-04 11:50:43 +00001284 , fTypes()
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001285{
Reid Spencer56010e42004-05-26 21:56:09 +00001286}
1287
1288inline void SlotMachine::initialize(void) {
1289 if ( TheModule) {
1290 processModule();
1291 TheModule = 0; ///< Prevent re-processing next time we're called.
1292 }
1293 if ( TheFunction ) {
1294 processFunction();
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001295 }
1296}
1297
1298// Iterate through all the global variables, functions, and global
1299// variable initializers and create slots for them.
1300void SlotMachine::processModule() {
1301 SC_DEBUG("begin processModule!\n");
1302
1303 // Add all of the global variables to the value table...
1304 for (Module::const_giterator I = TheModule->gbegin(), E = TheModule->gend();
1305 I != E; ++I)
1306 createSlot(I);
1307
1308 // Add all the functions to the table
1309 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
1310 I != E; ++I)
1311 createSlot(I);
1312
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001313 SC_DEBUG("end processModule!\n");
1314}
1315
1316
Reid Spencer56010e42004-05-26 21:56:09 +00001317// Process the arguments, basic blocks, and instructions of a function.
1318void SlotMachine::processFunction() {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001319 SC_DEBUG("begin processFunction!\n");
1320
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001321 // Add all the function arguments
Reid Spencer56010e42004-05-26 21:56:09 +00001322 for(Function::const_aiterator AI = TheFunction->abegin(),
1323 AE = TheFunction->aend(); AI != AE; ++AI)
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001324 createSlot(AI);
1325
1326 SC_DEBUG("Inserting Instructions:\n");
1327
1328 // Add all of the basic blocks and instructions
Reid Spencer56010e42004-05-26 21:56:09 +00001329 for (Function::const_iterator BB = TheFunction->begin(),
1330 E = TheFunction->end(); BB != E; ++BB) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001331 createSlot(BB);
1332 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
1333 createSlot(I);
1334 }
1335 }
1336
1337 SC_DEBUG("end processFunction!\n");
1338}
1339
1340// Clean up after incorporating a function. This is the only way
Reid Spencer56010e42004-05-26 21:56:09 +00001341// to get out of the function incorporation state that affects the
1342// getSlot/createSlot lock. Function incorporation state is indicated
1343// by TheFunction != 0.
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001344void SlotMachine::purgeFunction() {
1345 SC_DEBUG("begin purgeFunction!\n");
1346 fMap.clear(); // Simply discard the function level map
Reid Spencer58d30f22004-07-04 11:50:43 +00001347 fTypes.clear();
Reid Spencer56010e42004-05-26 21:56:09 +00001348 TheFunction = 0;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001349 SC_DEBUG("end purgeFunction!\n");
1350}
1351
1352/// Get the slot number for a value. This function will assert if you
1353/// ask for a Value that hasn't previously been inserted with createSlot.
1354/// Types are forbidden because Type does not inherit from Value (any more).
Chris Lattner757ee0b2004-06-09 19:41:19 +00001355int SlotMachine::getSlot(const Value *V) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001356 assert( V && "Can't get slot for null Value" );
Reid Spencer56010e42004-05-26 21:56:09 +00001357 assert(!isa<Constant>(V) || isa<GlobalValue>(V) &&
1358 "Can't insert a non-GlobalValue Constant into SlotMachine");
1359
1360 // Check for uninitialized state and do lazy initialization
1361 this->initialize();
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001362
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001363 // Get the type of the value
1364 const Type* VTy = V->getType();
1365
1366 // Find the type plane in the module map
1367 TypedPlanes::const_iterator MI = mMap.find(VTy);
1368
Reid Spencer56010e42004-05-26 21:56:09 +00001369 if ( TheFunction ) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001370 // Lookup the type in the function map too
1371 TypedPlanes::const_iterator FI = fMap.find(VTy);
1372 // If there is a corresponding type plane in the function map
1373 if ( FI != fMap.end() ) {
1374 // Lookup the Value in the function map
1375 ValueMap::const_iterator FVI = FI->second.map.find(V);
1376 // If the value doesn't exist in the function map
1377 if ( FVI == FI->second.map.end() ) {
Chris Lattner68a038e2004-06-09 22:22:10 +00001378 // Look up the value in the module map.
1379 if (MI == mMap.end()) return -1;
Reid Spencer56010e42004-05-26 21:56:09 +00001380 ValueMap::const_iterator MVI = MI->second.map.find(V);
1381 // If we didn't find it, it wasn't inserted
Chris Lattner757ee0b2004-06-09 19:41:19 +00001382 if (MVI == MI->second.map.end()) return -1;
Reid Spencer56010e42004-05-26 21:56:09 +00001383 assert( MVI != MI->second.map.end() && "Value not found");
1384 // We found it only at the module level
1385 return MVI->second;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001386
1387 // else the value exists in the function map
1388 } else {
Reid Spencer56010e42004-05-26 21:56:09 +00001389 // Return the slot number as the module's contribution to
1390 // the type plane plus the index in the function's contribution
1391 // to the type plane.
Chris Lattnerb1f04782004-06-15 21:07:32 +00001392 if (MI != mMap.end())
1393 return MI->second.next_slot + FVI->second;
1394 else
1395 return FVI->second;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001396 }
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001397 }
1398 }
1399
Reid Spencer8beac692004-06-09 15:26:53 +00001400 // N.B. Can get here only if either !TheFunction or the function doesn't
1401 // have a corresponding type plane for the Value
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001402
1403 // Make sure the type plane exists
Chris Lattner757ee0b2004-06-09 19:41:19 +00001404 if (MI == mMap.end()) return -1;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001405 // Lookup the value in the module's map
1406 ValueMap::const_iterator MVI = MI->second.map.find(V);
1407 // Make sure we found it.
Chris Lattner757ee0b2004-06-09 19:41:19 +00001408 if (MVI == MI->second.map.end()) return -1;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001409 // Return it.
1410 return MVI->second;
1411}
1412
Reid Spencer58d30f22004-07-04 11:50:43 +00001413/// Get the slot number for a value. This function will assert if you
1414/// ask for a Value that hasn't previously been inserted with createSlot.
1415/// Types are forbidden because Type does not inherit from Value (any more).
1416int SlotMachine::getSlot(const Type *Ty) {
1417 assert( Ty && "Can't get slot for null Type" );
1418
1419 // Check for uninitialized state and do lazy initialization
1420 this->initialize();
1421
1422 if ( TheFunction ) {
1423 // Lookup the Type in the function map
1424 TypeMap::const_iterator FTI = fTypes.map.find(Ty);
1425 // If the Type doesn't exist in the function map
1426 if ( FTI == fTypes.map.end() ) {
1427 TypeMap::const_iterator MTI = mTypes.map.find(Ty);
1428 // If we didn't find it, it wasn't inserted
1429 if (MTI == mTypes.map.end())
1430 return -1;
1431 // We found it only at the module level
1432 return MTI->second;
1433
1434 // else the value exists in the function map
1435 } else {
1436 // Return the slot number as the module's contribution to
1437 // the type plane plus the index in the function's contribution
1438 // to the type plane.
1439 return mTypes.next_slot + FTI->second;
1440 }
1441 }
1442
1443 // N.B. Can get here only if either !TheFunction
1444
1445 // Lookup the value in the module's map
1446 TypeMap::const_iterator MTI = mTypes.map.find(Ty);
1447 // Make sure we found it.
1448 if (MTI == mTypes.map.end()) return -1;
1449 // Return it.
1450 return MTI->second;
1451}
1452
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001453// Create a new slot, or return the existing slot if it is already
1454// inserted. Note that the logic here parallels getSlot but instead
1455// of asserting when the Value* isn't found, it inserts the value.
1456unsigned SlotMachine::createSlot(const Value *V) {
1457 assert( V && "Can't insert a null Value to SlotMachine");
Reid Spencer56010e42004-05-26 21:56:09 +00001458 assert(!isa<Constant>(V) || isa<GlobalValue>(V) &&
1459 "Can't insert a non-GlobalValue Constant into SlotMachine");
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001460
1461 const Type* VTy = V->getType();
1462
1463 // Just ignore void typed things
1464 if (VTy == Type::VoidTy) return 0; // FIXME: Wrong return value!
1465
1466 // Look up the type plane for the Value's type from the module map
1467 TypedPlanes::const_iterator MI = mMap.find(VTy);
1468
Reid Spencer56010e42004-05-26 21:56:09 +00001469 if ( TheFunction ) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001470 // Get the type plane for the Value's type from the function map
1471 TypedPlanes::const_iterator FI = fMap.find(VTy);
1472 // If there is a corresponding type plane in the function map
1473 if ( FI != fMap.end() ) {
1474 // Lookup the Value in the function map
1475 ValueMap::const_iterator FVI = FI->second.map.find(V);
1476 // If the value doesn't exist in the function map
1477 if ( FVI == FI->second.map.end() ) {
Reid Spencer56010e42004-05-26 21:56:09 +00001478 // If there is no corresponding type plane in the module map
1479 if ( MI == mMap.end() )
1480 return insertValue(V);
1481 // Look up the value in the module map
1482 ValueMap::const_iterator MVI = MI->second.map.find(V);
1483 // If we didn't find it, it wasn't inserted
1484 if ( MVI == MI->second.map.end() )
1485 return insertValue(V);
1486 else
1487 // We found it only at the module level
1488 return MVI->second;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001489
1490 // else the value exists in the function map
1491 } else {
Reid Spencer56010e42004-05-26 21:56:09 +00001492 if ( MI == mMap.end() )
1493 return FVI->second;
1494 else
1495 // Return the slot number as the module's contribution to
1496 // the type plane plus the index in the function's contribution
1497 // to the type plane.
1498 return MI->second.next_slot + FVI->second;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001499 }
1500
1501 // else there is not a corresponding type plane in the function map
1502 } else {
1503 // If the type plane doesn't exists at the module level
1504 if ( MI == mMap.end() ) {
Reid Spencer56010e42004-05-26 21:56:09 +00001505 return insertValue(V);
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001506 // else type plane exists at the module level, examine it
1507 } else {
Reid Spencer56010e42004-05-26 21:56:09 +00001508 // Look up the value in the module's map
1509 ValueMap::const_iterator MVI = MI->second.map.find(V);
1510 // If we didn't find it there either
1511 if ( MVI == MI->second.map.end() )
1512 // Return the slot number as the module's contribution to
1513 // the type plane plus the index of the function map insertion.
1514 return MI->second.next_slot + insertValue(V);
1515 else
1516 return MVI->second;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001517 }
1518 }
1519 }
1520
Reid Spencer56010e42004-05-26 21:56:09 +00001521 // N.B. Can only get here if !TheFunction
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001522
1523 // If the module map's type plane is not for the Value's type
1524 if ( MI != mMap.end() ) {
1525 // Lookup the value in the module's map
1526 ValueMap::const_iterator MVI = MI->second.map.find(V);
1527 if ( MVI != MI->second.map.end() )
1528 return MVI->second;
1529 }
1530
1531 return insertValue(V);
1532}
1533
Reid Spencer58d30f22004-07-04 11:50:43 +00001534// Create a new slot, or return the existing slot if it is already
1535// inserted. Note that the logic here parallels getSlot but instead
1536// of asserting when the Value* isn't found, it inserts the value.
1537unsigned SlotMachine::createSlot(const Type *Ty) {
1538 assert( Ty && "Can't insert a null Type to SlotMachine");
1539
1540 if ( TheFunction ) {
1541 // Lookup the Type in the function map
1542 TypeMap::const_iterator FTI = fTypes.map.find(Ty);
1543 // If the type doesn't exist in the function map
1544 if ( FTI == fTypes.map.end() ) {
1545 // Look up the type in the module map
1546 TypeMap::const_iterator MTI = mTypes.map.find(Ty);
1547 // If we didn't find it, it wasn't inserted
1548 if ( MTI == mTypes.map.end() )
1549 return insertValue(Ty);
1550 else
1551 // We found it only at the module level
1552 return MTI->second;
1553
1554 // else the value exists in the function map
1555 } else {
1556 // Return the slot number as the module's contribution to
1557 // the type plane plus the index in the function's contribution
1558 // to the type plane.
1559 return mTypes.next_slot + FTI->second;
1560 }
1561 }
1562
1563 // N.B. Can only get here if !TheFunction
1564
1565 // Lookup the type in the module's map
1566 TypeMap::const_iterator MTI = mTypes.map.find(Ty);
1567 if ( MTI != mTypes.map.end() )
1568 return MTI->second;
1569
1570 return insertValue(Ty);
1571}
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001572
1573// Low level insert function. Minimal checking is done. This
1574// function is just for the convenience of createSlot (above).
1575unsigned SlotMachine::insertValue(const Value *V ) {
1576 assert(V && "Can't insert a null Value into SlotMachine!");
Reid Spencer56010e42004-05-26 21:56:09 +00001577 assert(!isa<Constant>(V) || isa<GlobalValue>(V) &&
1578 "Can't insert a non-GlobalValue Constant into SlotMachine");
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001579
Reid Spencer56010e42004-05-26 21:56:09 +00001580 // If this value does not contribute to a plane (is void)
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001581 // or if the value already has a name then ignore it.
Reid Spencer56010e42004-05-26 21:56:09 +00001582 if (V->getType() == Type::VoidTy || V->hasName() ) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001583 SC_DEBUG("ignored value " << *V << "\n");
1584 return 0; // FIXME: Wrong return value
1585 }
1586
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001587 const Type *VTy = V->getType();
1588 unsigned DestSlot = 0;
1589
Reid Spencer56010e42004-05-26 21:56:09 +00001590 if ( TheFunction ) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001591 TypedPlanes::iterator I = fMap.find( VTy );
1592 if ( I == fMap.end() )
Reid Spencer58d30f22004-07-04 11:50:43 +00001593 I = fMap.insert(std::make_pair(VTy,ValuePlane())).first;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001594 DestSlot = I->second.map[V] = I->second.next_slot++;
1595 } else {
1596 TypedPlanes::iterator I = mMap.find( VTy );
1597 if ( I == mMap.end() )
Reid Spencer58d30f22004-07-04 11:50:43 +00001598 I = mMap.insert(std::make_pair(VTy,ValuePlane())).first;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001599 DestSlot = I->second.map[V] = I->second.next_slot++;
1600 }
1601
1602 SC_DEBUG(" Inserting value [" << VTy << "] = " << V << " slot=" <<
Reid Spencer56010e42004-05-26 21:56:09 +00001603 DestSlot << " [");
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001604 // G = Global, C = Constant, T = Type, F = Function, o = other
Reid Spenceraccd7c72004-07-17 23:47:01 +00001605 SC_DEBUG((isa<GlobalVariable>(V) ? 'G' : (isa<Function>(V) ? 'F' :
1606 (isa<Constant>(V) ? 'C' : 'o'))));
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001607 SC_DEBUG("]\n");
1608 return DestSlot;
1609}
1610
Reid Spencer58d30f22004-07-04 11:50:43 +00001611// Low level insert function. Minimal checking is done. This
1612// function is just for the convenience of createSlot (above).
1613unsigned SlotMachine::insertValue(const Type *Ty ) {
1614 assert(Ty && "Can't insert a null Type into SlotMachine!");
1615
1616 unsigned DestSlot = 0;
1617
1618 if ( TheFunction ) {
1619 DestSlot = fTypes.map[Ty] = fTypes.next_slot++;
1620 } else {
1621 DestSlot = fTypes.map[Ty] = fTypes.next_slot++;
1622 }
1623 SC_DEBUG(" Inserting type [" << DestSlot << "] = " << Ty << "\n");
1624 return DestSlot;
1625}
1626
Reid Spencere7e96712004-05-25 08:53:40 +00001627// vim: sw=2