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