blob: 1647f51da96654590f344f9d3e8e54afab1943fb [file] [log] [blame]
Chris Lattnerf7e79482002-04-07 22:31:46 +00001//===-- AsmWriter.cpp - Printing LLVM as an assembly file -----------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
10// This library implements the functionality defined in llvm/Assembly/Writer.h
11//
Chris Lattner189088e2002-04-12 18:21:53 +000012// Note that these routines must be extremely tolerant of various errors in the
Chris Lattnerf70da102003-05-08 02:44:12 +000013// LLVM code, because it can be used for debugging transformations.
Chris Lattner189088e2002-04-12 18:21:53 +000014//
Chris Lattner2f7c9632001-06-06 20:29:01 +000015//===----------------------------------------------------------------------===//
16
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +000017#include "llvm/Assembly/Writer.h"
Chris Lattner7f8845a2002-07-23 18:07:49 +000018#include "llvm/Assembly/PrintModulePass.h"
Chris Lattner8339f7d2003-10-30 23:41:03 +000019#include "llvm/Assembly/AsmAnnotationWriter.h"
Chris Lattnerf7b6d312005-05-06 20:26:43 +000020#include "llvm/CallingConv.h"
Chris Lattnerc70b3f62004-01-20 19:50:34 +000021#include "llvm/Constants.h"
Chris Lattner913d18f2002-04-29 18:46:50 +000022#include "llvm/DerivedTypes.h"
Chris Lattner8bbcda22006-01-25 18:57:27 +000023#include "llvm/InlineAsm.h"
Vikram S. Adveb952b542002-07-14 23:14:45 +000024#include "llvm/Instruction.h"
Misha Brukman2d3fa9e2004-07-29 16:53:53 +000025#include "llvm/Instructions.h"
Chris Lattnerc70b3f62004-01-20 19:50:34 +000026#include "llvm/Module.h"
Chris Lattnerfee714f2001-09-07 16:36:04 +000027#include "llvm/SymbolTable.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000028#include "llvm/ADT/StringExtras.h"
29#include "llvm/ADT/STLExtras.h"
Bill Wendlingdfc91892006-11-28 02:09:03 +000030#include "llvm/Support/CFG.h"
Jim Laskeyb74c6662005-08-17 19:34:49 +000031#include "llvm/Support/MathExtras.h"
Bill Wendlingdfc91892006-11-28 02:09:03 +000032#include "llvm/Support/Streams.h"
Chris Lattnerfee714f2001-09-07 16:36:04 +000033#include <algorithm>
Chris Lattner189d19f2003-11-21 20:23:48 +000034using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000035
Chris Lattner60a7dd12004-07-15 02:51:31 +000036namespace llvm {
Reid Spencer16f2f7f2004-05-26 07:18:52 +000037
Reid Spencer294715b2005-05-15 16:13:11 +000038// Make virtual table appear in this compilation unit.
39AssemblyAnnotationWriter::~AssemblyAnnotationWriter() {}
40
Reid Spencer16f2f7f2004-05-26 07:18:52 +000041/// This class provides computation of slot numbers for LLVM Assembly writing.
42/// @brief LLVM Assembly Writing Slot Computation.
43class SlotMachine {
44
45/// @name Types
46/// @{
47public:
48
49 /// @brief A mapping of Values to slot numbers
50 typedef std::map<const Value*, unsigned> ValueMap;
51
52 /// @brief A plane with next slot number and ValueMap
Misha Brukmanb1c93172005-04-21 23:48:37 +000053 struct ValuePlane {
Reid Spencer16f2f7f2004-05-26 07:18:52 +000054 unsigned next_slot; ///< The next slot number to use
55 ValueMap map; ///< The map of Value* -> unsigned
Reid Spencer58d30f22004-07-04 11:50:43 +000056 ValuePlane() { next_slot = 0; } ///< Make sure we start at 0
57 };
58
Reid Spencer16f2f7f2004-05-26 07:18:52 +000059 /// @brief The map of planes by Type
Reid Spencer58d30f22004-07-04 11:50:43 +000060 typedef std::map<const Type*, ValuePlane> TypedPlanes;
Reid Spencer16f2f7f2004-05-26 07:18:52 +000061
62/// @}
63/// @name Constructors
64/// @{
65public:
66 /// @brief Construct from a module
Chris Lattner23e829a2006-12-06 05:12:21 +000067 SlotMachine(const Module *M);
Reid Spencer16f2f7f2004-05-26 07:18:52 +000068
69 /// @brief Construct from a function, starting out in incorp state.
Chris Lattner23e829a2006-12-06 05:12:21 +000070 SlotMachine(const Function *F);
Reid Spencer16f2f7f2004-05-26 07:18:52 +000071
72/// @}
73/// @name Accessors
74/// @{
75public:
76 /// Return the slot number of the specified value in it's type
77 /// plane. Its an error to ask for something not in the SlotMachine.
78 /// Its an error to ask for a Type*
Chris Lattner757ee0b2004-06-09 19:41:19 +000079 int getSlot(const Value *V);
Reid Spencer8beac692004-06-09 15:26:53 +000080
Reid Spencer16f2f7f2004-05-26 07:18:52 +000081/// @}
82/// @name Mutators
83/// @{
84public:
Misha Brukmanb1c93172005-04-21 23:48:37 +000085 /// If you'd like to deal with a function instead of just a module, use
Reid Spencer16f2f7f2004-05-26 07:18:52 +000086 /// this method to get its data into the SlotMachine.
Misha Brukmanb1c93172005-04-21 23:48:37 +000087 void incorporateFunction(const Function *F) {
88 TheFunction = F;
Reid Spencerb0ac8c42004-08-16 07:46:33 +000089 FunctionProcessed = false;
90 }
Reid Spencer16f2f7f2004-05-26 07:18:52 +000091
Misha Brukmanb1c93172005-04-21 23:48:37 +000092 /// After calling incorporateFunction, use this method to remove the
93 /// most recently incorporated function from the SlotMachine. This
Reid Spencer16f2f7f2004-05-26 07:18:52 +000094 /// will reset the state of the machine back to just the module contents.
95 void purgeFunction();
96
97/// @}
98/// @name Implementation Details
99/// @{
100private:
Reid Spencer56010e42004-05-26 21:56:09 +0000101 /// This function does the actual initialization.
102 inline void initialize();
103
Misha Brukmanb1c93172005-04-21 23:48:37 +0000104 /// Values can be crammed into here at will. If they haven't
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000105 /// been inserted already, they get inserted, otherwise they are ignored.
106 /// Either way, the slot number for the Value* is returned.
Chris Lattner193de902006-12-06 05:27:40 +0000107 unsigned getOrCreateSlot(const Value *V);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000108
109 /// Insert a value into the value table. Return the slot number
110 /// that it now occupies. BadThings(TM) will happen if you insert a
Misha Brukmanb1c93172005-04-21 23:48:37 +0000111 /// Value that's already been inserted.
Chris Lattner23e829a2006-12-06 05:12:21 +0000112 unsigned insertValue(const Value *V);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000113
114 /// Add all of the module level global variables (and their initializers)
115 /// and function declarations, but not the contents of those functions.
116 void processModule();
117
Reid Spencer56010e42004-05-26 21:56:09 +0000118 /// Add all of the functions arguments, basic blocks, and instructions
119 void processFunction();
120
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000121 SlotMachine(const SlotMachine &); // DO NOT IMPLEMENT
122 void operator=(const SlotMachine &); // DO NOT IMPLEMENT
123
124/// @}
125/// @name Data
126/// @{
127public:
128
129 /// @brief The module for which we are holding slot numbers
Reid Spencer56010e42004-05-26 21:56:09 +0000130 const Module* TheModule;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000131
Reid Spencer56010e42004-05-26 21:56:09 +0000132 /// @brief The function for which we are holding slot numbers
133 const Function* TheFunction;
Reid Spencerb0ac8c42004-08-16 07:46:33 +0000134 bool FunctionProcessed;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000135
136 /// @brief The TypePlanes map for the module level data
137 TypedPlanes mMap;
138
139 /// @brief The TypePlanes map for the function level data
140 TypedPlanes fMap;
141
142/// @}
143
144};
145
Chris Lattner60a7dd12004-07-15 02:51:31 +0000146} // end namespace llvm
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000147
Chris Lattnerc2d3d312006-08-27 22:42:52 +0000148static RegisterPass<PrintModulePass>
Chris Lattnere7134c52006-08-21 17:20:01 +0000149X("printm", "Print module to stderr");
Chris Lattnerc2d3d312006-08-27 22:42:52 +0000150static RegisterPass<PrintFunctionPass>
Chris Lattnere7134c52006-08-21 17:20:01 +0000151Y("print","Print function to stderr");
Chris Lattner7f8845a2002-07-23 18:07:49 +0000152
Misha Brukmanb1c93172005-04-21 23:48:37 +0000153static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
Chris Lattnera9f0a112006-12-06 05:50:41 +0000154 std::map<const Type *, std::string> &TypeTable,
Reid Spencer58d30f22004-07-04 11:50:43 +0000155 SlotMachine *Machine);
156
Chris Lattnerb86620e2001-10-29 16:37:48 +0000157static const Module *getModuleFromVal(const Value *V) {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000158 if (const Argument *MA = dyn_cast<Argument>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +0000159 return MA->getParent() ? MA->getParent()->getParent() : 0;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000160 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +0000161 return BB->getParent() ? BB->getParent()->getParent() : 0;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000162 else if (const Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner57698e22002-03-26 18:01:55 +0000163 const Function *M = I->getParent() ? I->getParent()->getParent() : 0;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000164 return M ? M->getParent() : 0;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000165 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +0000166 return GV->getParent();
Chris Lattnerb86620e2001-10-29 16:37:48 +0000167 return 0;
168}
169
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000170static SlotMachine *createSlotMachine(const Value *V) {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000171 if (const Argument *FA = dyn_cast<Argument>(V)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000172 return new SlotMachine(FA->getParent());
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000173 } else if (const Instruction *I = dyn_cast<Instruction>(V)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000174 return new SlotMachine(I->getParent()->getParent());
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000175 } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000176 return new SlotMachine(BB->getParent());
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000177 } else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)){
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000178 return new SlotMachine(GV->getParent());
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000179 } else if (const Function *Func = dyn_cast<Function>(V)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000180 return new SlotMachine(Func);
Chris Lattner7bfee412001-10-29 16:05:51 +0000181 }
182 return 0;
183}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000184
Chris Lattner1c343042003-08-22 05:40:38 +0000185// getLLVMName - Turn the specified string into an 'LLVM name', which is either
186// prefixed with % (if the string only contains simple characters) or is
187// surrounded with ""'s (if it has special chars in it).
Alkis Evlogimenos991d6ad2004-12-10 05:41:10 +0000188static std::string getLLVMName(const std::string &Name,
189 bool prefixName = true) {
Chris Lattner1c343042003-08-22 05:40:38 +0000190 assert(!Name.empty() && "Cannot get empty name!");
191
192 // First character cannot start with a number...
193 if (Name[0] >= '0' && Name[0] <= '9')
194 return "\"" + Name + "\"";
195
196 // Scan to see if we have any characters that are not on the "white list"
197 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
198 char C = Name[i];
199 assert(C != '"' && "Illegal character in LLVM value name!");
200 if ((C < 'a' || C > 'z') && (C < 'A' || C > 'Z') && (C < '0' || C > '9') &&
201 C != '-' && C != '.' && C != '_')
202 return "\"" + Name + "\"";
203 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000204
Chris Lattner1c343042003-08-22 05:40:38 +0000205 // If we get here, then the identifier is legal to use as a "VarID".
Alkis Evlogimenos991d6ad2004-12-10 05:41:10 +0000206 if (prefixName)
207 return "%"+Name;
208 else
209 return Name;
Chris Lattner1c343042003-08-22 05:40:38 +0000210}
211
Chris Lattnerb86620e2001-10-29 16:37:48 +0000212
Misha Brukmanc566ca362004-03-02 00:22:19 +0000213/// fillTypeNameTable - If the module has a symbol table, take all global types
214/// and stuff their names into the TypeNames map.
215///
Chris Lattnerb86620e2001-10-29 16:37:48 +0000216static void fillTypeNameTable(const Module *M,
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000217 std::map<const Type *, std::string> &TypeNames) {
Chris Lattner98cf1f52002-11-20 18:36:02 +0000218 if (!M) return;
219 const SymbolTable &ST = M->getSymbolTable();
Reid Spencere7e96712004-05-25 08:53:40 +0000220 SymbolTable::type_const_iterator TI = ST.type_begin();
Chris Lattner23e829a2006-12-06 05:12:21 +0000221 for (; TI != ST.type_end(); ++TI) {
Reid Spencere7e96712004-05-25 08:53:40 +0000222 // As a heuristic, don't insert pointer to primitive types, because
223 // they are used too often to have a single useful name.
224 //
225 const Type *Ty = cast<Type>(TI->second);
226 if (!isa<PointerType>(Ty) ||
Reid Spencer56010e42004-05-26 21:56:09 +0000227 !cast<PointerType>(Ty)->getElementType()->isPrimitiveType() ||
228 isa<OpaqueType>(cast<PointerType>(Ty)->getElementType()))
Reid Spencere7e96712004-05-25 08:53:40 +0000229 TypeNames.insert(std::make_pair(Ty, getLLVMName(TI->first)));
Chris Lattnerb86620e2001-10-29 16:37:48 +0000230 }
231}
232
233
234
Misha Brukmanb1c93172005-04-21 23:48:37 +0000235static void calcTypeName(const Type *Ty,
John Criswellcd116ba2004-06-01 14:54:08 +0000236 std::vector<const Type *> &TypeStack,
237 std::map<const Type *, std::string> &TypeNames,
238 std::string & Result){
239 if (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty)) {
240 Result += Ty->getDescription(); // Base case
241 return;
242 }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000243
244 // Check to see if the type is named.
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000245 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
John Criswellcd116ba2004-06-01 14:54:08 +0000246 if (I != TypeNames.end()) {
247 Result += I->second;
248 return;
249 }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000250
John Criswellcd116ba2004-06-01 14:54:08 +0000251 if (isa<OpaqueType>(Ty)) {
252 Result += "opaque";
253 return;
254 }
Chris Lattnerf14ead92003-10-30 00:22:33 +0000255
Chris Lattnerb86620e2001-10-29 16:37:48 +0000256 // Check to see if the Type is already on the stack...
257 unsigned Slot = 0, CurSize = TypeStack.size();
258 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
259
Misha Brukmanb1c93172005-04-21 23:48:37 +0000260 // This is another base case for the recursion. In this case, we know
Chris Lattnerb86620e2001-10-29 16:37:48 +0000261 // that we have looped back to a type that we have previously visited.
262 // Generate the appropriate upreference to handle this.
John Criswellcd116ba2004-06-01 14:54:08 +0000263 if (Slot < CurSize) {
264 Result += "\\" + utostr(CurSize-Slot); // Here's the upreference
265 return;
266 }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000267
268 TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
Misha Brukmanb1c93172005-04-21 23:48:37 +0000269
Chris Lattner6b727592004-06-17 18:19:28 +0000270 switch (Ty->getTypeID()) {
Chris Lattner91db5822002-03-29 03:44:36 +0000271 case Type::FunctionTyID: {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000272 const FunctionType *FTy = cast<FunctionType>(Ty);
John Criswellcd116ba2004-06-01 14:54:08 +0000273 calcTypeName(FTy->getReturnType(), TypeStack, TypeNames, Result);
274 Result += " (";
Chris Lattnerfa829be2004-02-09 04:14:01 +0000275 for (FunctionType::param_iterator I = FTy->param_begin(),
276 E = FTy->param_end(); I != E; ++I) {
277 if (I != FTy->param_begin())
Chris Lattnerb86620e2001-10-29 16:37:48 +0000278 Result += ", ";
John Criswellcd116ba2004-06-01 14:54:08 +0000279 calcTypeName(*I, TypeStack, TypeNames, Result);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000280 }
Chris Lattnerd816b532002-04-13 20:53:41 +0000281 if (FTy->isVarArg()) {
Chris Lattnerfa829be2004-02-09 04:14:01 +0000282 if (FTy->getNumParams()) Result += ", ";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000283 Result += "...";
284 }
285 Result += ")";
286 break;
287 }
288 case Type::StructTyID: {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000289 const StructType *STy = cast<StructType>(Ty);
Andrew Lenharthdcb3c972006-12-08 18:06:16 +0000290 if (STy->isPacked())
291 Result += '<';
John Criswellcd116ba2004-06-01 14:54:08 +0000292 Result += "{ ";
Chris Lattnerac6db752004-02-09 04:37:31 +0000293 for (StructType::element_iterator I = STy->element_begin(),
294 E = STy->element_end(); I != E; ++I) {
295 if (I != STy->element_begin())
Chris Lattnerb86620e2001-10-29 16:37:48 +0000296 Result += ", ";
John Criswellcd116ba2004-06-01 14:54:08 +0000297 calcTypeName(*I, TypeStack, TypeNames, Result);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000298 }
299 Result += " }";
Andrew Lenharthdcb3c972006-12-08 18:06:16 +0000300 if (STy->isPacked())
301 Result += '>';
Chris Lattnerb86620e2001-10-29 16:37:48 +0000302 break;
303 }
304 case Type::PointerTyID:
Misha Brukmanb1c93172005-04-21 23:48:37 +0000305 calcTypeName(cast<PointerType>(Ty)->getElementType(),
John Criswellcd116ba2004-06-01 14:54:08 +0000306 TypeStack, TypeNames, Result);
307 Result += "*";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000308 break;
309 case Type::ArrayTyID: {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000310 const ArrayType *ATy = cast<ArrayType>(Ty);
John Criswellcd116ba2004-06-01 14:54:08 +0000311 Result += "[" + utostr(ATy->getNumElements()) + " x ";
312 calcTypeName(ATy->getElementType(), TypeStack, TypeNames, Result);
313 Result += "]";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000314 break;
315 }
Brian Gaeke02209042004-08-20 06:00:58 +0000316 case Type::PackedTyID: {
317 const PackedType *PTy = cast<PackedType>(Ty);
318 Result += "<" + utostr(PTy->getNumElements()) + " x ";
319 calcTypeName(PTy->getElementType(), TypeStack, TypeNames, Result);
320 Result += ">";
321 break;
322 }
Chris Lattner15285ab2003-05-14 17:50:47 +0000323 case Type::OpaqueTyID:
John Criswellcd116ba2004-06-01 14:54:08 +0000324 Result += "opaque";
Chris Lattner15285ab2003-05-14 17:50:47 +0000325 break;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000326 default:
John Criswellcd116ba2004-06-01 14:54:08 +0000327 Result += "<unrecognized-type>";
Chris Lattnerfc9f1c92006-12-06 06:40:49 +0000328 break;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000329 }
330
331 TypeStack.pop_back(); // Remove self from stack...
Chris Lattnerb86620e2001-10-29 16:37:48 +0000332}
333
334
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000335/// printTypeInt - The internal guts of printing out a type that has a
336/// potentially named portion.
337///
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000338static std::ostream &printTypeInt(std::ostream &Out, const Type *Ty,
339 std::map<const Type *, std::string> &TypeNames) {
Chris Lattnerb86620e2001-10-29 16:37:48 +0000340 // Primitive types always print out their description, regardless of whether
341 // they have been named or not.
342 //
Chris Lattner92d60532003-10-30 00:12:51 +0000343 if (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty))
344 return Out << Ty->getDescription();
Chris Lattnerb86620e2001-10-29 16:37:48 +0000345
346 // Check to see if the type is named.
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000347 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000348 if (I != TypeNames.end()) return Out << I->second;
349
350 // Otherwise we have a type that has not been named but is a derived type.
351 // Carefully recurse the type hierarchy to print out any contained symbolic
352 // names.
353 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000354 std::vector<const Type *> TypeStack;
John Criswellcd116ba2004-06-01 14:54:08 +0000355 std::string TypeName;
356 calcTypeName(Ty, TypeStack, TypeNames, TypeName);
Chris Lattner7f74a562002-01-20 22:54:45 +0000357 TypeNames.insert(std::make_pair(Ty, TypeName));//Cache type name for later use
John Criswellcd116ba2004-06-01 14:54:08 +0000358 return (Out << TypeName);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000359}
360
Chris Lattner34b95182001-10-31 04:33:19 +0000361
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000362/// WriteTypeSymbolic - This attempts to write the specified type as a symbolic
363/// type, iff there is an entry in the modules symbol table for the specified
364/// type or one of it's component types. This is slower than a simple x << Type
365///
Chris Lattner189d19f2003-11-21 20:23:48 +0000366std::ostream &llvm::WriteTypeSymbolic(std::ostream &Out, const Type *Ty,
367 const Module *M) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000368 Out << ' ';
Chris Lattnerb86620e2001-10-29 16:37:48 +0000369
Chris Lattnerfc9f1c92006-12-06 06:40:49 +0000370 // If they want us to print out a type, but there is no context, we can't
371 // print it symbolically.
372 if (!M)
Chris Lattner72f866e2001-10-29 16:40:32 +0000373 return Out << Ty->getDescription();
Chris Lattnerfc9f1c92006-12-06 06:40:49 +0000374
375 std::map<const Type *, std::string> TypeNames;
376 fillTypeNameTable(M, TypeNames);
377 return printTypeInt(Out, Ty, TypeNames);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000378}
379
Chris Lattner6ed87bd2006-01-23 23:03:36 +0000380// PrintEscapedString - Print each character of the specified string, escaping
381// it if it is not printable or if it is an escape char.
382static void PrintEscapedString(const std::string &Str, std::ostream &Out) {
383 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
384 unsigned char C = Str[i];
385 if (isprint(C) && C != '"' && C != '\\') {
386 Out << C;
387 } else {
388 Out << '\\'
389 << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'))
390 << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
391 }
392 }
393}
394
Chris Lattnerfc9f1c92006-12-06 06:40:49 +0000395static const char *getPredicateText(unsigned predicate) {
Reid Spencer812a1be2006-12-04 05:19:18 +0000396 const char * pred = "unknown";
397 switch (predicate) {
398 case FCmpInst::FCMP_FALSE: pred = "false"; break;
399 case FCmpInst::FCMP_OEQ: pred = "oeq"; break;
400 case FCmpInst::FCMP_OGT: pred = "ogt"; break;
401 case FCmpInst::FCMP_OGE: pred = "oge"; break;
402 case FCmpInst::FCMP_OLT: pred = "olt"; break;
403 case FCmpInst::FCMP_OLE: pred = "ole"; break;
404 case FCmpInst::FCMP_ONE: pred = "one"; break;
405 case FCmpInst::FCMP_ORD: pred = "ord"; break;
406 case FCmpInst::FCMP_UNO: pred = "uno"; break;
407 case FCmpInst::FCMP_UEQ: pred = "ueq"; break;
408 case FCmpInst::FCMP_UGT: pred = "ugt"; break;
409 case FCmpInst::FCMP_UGE: pred = "uge"; break;
410 case FCmpInst::FCMP_ULT: pred = "ult"; break;
411 case FCmpInst::FCMP_ULE: pred = "ule"; break;
412 case FCmpInst::FCMP_UNE: pred = "une"; break;
413 case FCmpInst::FCMP_TRUE: pred = "true"; break;
414 case ICmpInst::ICMP_EQ: pred = "eq"; break;
415 case ICmpInst::ICMP_NE: pred = "ne"; break;
416 case ICmpInst::ICMP_SGT: pred = "sgt"; break;
417 case ICmpInst::ICMP_SGE: pred = "sge"; break;
418 case ICmpInst::ICMP_SLT: pred = "slt"; break;
419 case ICmpInst::ICMP_SLE: pred = "sle"; break;
420 case ICmpInst::ICMP_UGT: pred = "ugt"; break;
421 case ICmpInst::ICMP_UGE: pred = "uge"; break;
422 case ICmpInst::ICMP_ULT: pred = "ult"; break;
423 case ICmpInst::ICMP_ULE: pred = "ule"; break;
424 }
425 return pred;
426}
427
Misha Brukmanb1c93172005-04-21 23:48:37 +0000428/// @brief Internal constant writer.
429static void WriteConstantInt(std::ostream &Out, const Constant *CV,
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000430 std::map<const Type *, std::string> &TypeTable,
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000431 SlotMachine *Machine) {
Jim Laskey6be3d8e2006-02-27 10:33:53 +0000432 const int IndentSize = 4;
433 static std::string Indent = "\n";
Chris Lattner1e194682002-04-18 18:53:13 +0000434 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
Chris Lattner94e39cb2006-09-28 22:50:29 +0000435 Out << (CB->getValue() ? "true" : "false");
Reid Spencere0fc4df2006-10-20 07:07:24 +0000436 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
437 if (CI->getType()->isSigned())
438 Out << CI->getSExtValue();
439 else
440 Out << CI->getZExtValue();
Chris Lattner1e194682002-04-18 18:53:13 +0000441 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
442 // We would like to output the FP constant value in exponential notation,
443 // but we cannot do this if doing so will lose precision. Check here to
444 // make sure that we only output it in exponential format if we can parse
445 // the value back and get the same value.
446 //
447 std::string StrVal = ftostr(CFP->getValue());
448
449 // Check to make sure that the stringized number is not some string like
450 // "Inf" or NaN, that atof will accept, but the lexer will not. Check that
451 // the string matches the "[-+]?[0-9]" regex.
452 //
453 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
454 ((StrVal[0] == '-' || StrVal[0] == '+') &&
Brian Gaeke87b4f072003-06-17 23:55:35 +0000455 (StrVal[1] >= '0' && StrVal[1] <= '9')))
Chris Lattner1e194682002-04-18 18:53:13 +0000456 // Reparse stringized version!
457 if (atof(StrVal.c_str()) == CFP->getValue()) {
Chris Lattner472cc102005-01-04 01:56:57 +0000458 Out << StrVal;
459 return;
Chris Lattner1e194682002-04-18 18:53:13 +0000460 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000461
Chris Lattner1e194682002-04-18 18:53:13 +0000462 // Otherwise we could not reparse it to exactly the same value, so we must
463 // output the string in hexadecimal format!
Chris Lattner472cc102005-01-04 01:56:57 +0000464 assert(sizeof(double) == sizeof(uint64_t) &&
Chris Lattner1e194682002-04-18 18:53:13 +0000465 "assuming that double is 64 bits!");
Jim Laskeyb74c6662005-08-17 19:34:49 +0000466 Out << "0x" << utohexstr(DoubleToBits(CFP->getValue()));
Chris Lattner1e194682002-04-18 18:53:13 +0000467
Chris Lattner76b2ff42004-02-15 05:55:15 +0000468 } else if (isa<ConstantAggregateZero>(CV)) {
469 Out << "zeroinitializer";
Chris Lattner1e194682002-04-18 18:53:13 +0000470 } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
471 // As a special case, print the array as a string if it is an array of
472 // ubytes or an array of sbytes with positive values.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000473 //
Chris Lattner1e194682002-04-18 18:53:13 +0000474 const Type *ETy = CA->getType()->getElementType();
Chris Lattner6ed87bd2006-01-23 23:03:36 +0000475 if (CA->isString()) {
Chris Lattner1e194682002-04-18 18:53:13 +0000476 Out << "c\"";
Chris Lattner6ed87bd2006-01-23 23:03:36 +0000477 PrintEscapedString(CA->getAsString(), Out);
Chris Lattner1e194682002-04-18 18:53:13 +0000478 Out << "\"";
479
480 } else { // Cannot output in string format...
Misha Brukman21bbdb92004-06-04 21:11:51 +0000481 Out << '[';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000482 if (CA->getNumOperands()) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000483 Out << ' ';
Chris Lattner1e194682002-04-18 18:53:13 +0000484 printTypeInt(Out, ETy, TypeTable);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000485 WriteAsOperandInternal(Out, CA->getOperand(0),
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000486 TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000487 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
488 Out << ", ";
Chris Lattner1e194682002-04-18 18:53:13 +0000489 printTypeInt(Out, ETy, TypeTable);
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000490 WriteAsOperandInternal(Out, CA->getOperand(i), TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000491 }
492 }
493 Out << " ]";
494 }
495 } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000496 Out << '{';
Jim Laskey3bb78742006-02-25 12:27:03 +0000497 unsigned N = CS->getNumOperands();
498 if (N) {
Jim Laskey6be3d8e2006-02-27 10:33:53 +0000499 if (N > 2) {
500 Indent += std::string(IndentSize, ' ');
501 Out << Indent;
502 } else {
503 Out << ' ';
504 }
Chris Lattnerd84bb632002-04-16 21:36:08 +0000505 printTypeInt(Out, CS->getOperand(0)->getType(), TypeTable);
506
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000507 WriteAsOperandInternal(Out, CS->getOperand(0), TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000508
Jim Laskey3bb78742006-02-25 12:27:03 +0000509 for (unsigned i = 1; i < N; i++) {
Chris Lattnerd84bb632002-04-16 21:36:08 +0000510 Out << ", ";
Jim Laskey6be3d8e2006-02-27 10:33:53 +0000511 if (N > 2) Out << Indent;
Chris Lattnerd84bb632002-04-16 21:36:08 +0000512 printTypeInt(Out, CS->getOperand(i)->getType(), TypeTable);
513
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000514 WriteAsOperandInternal(Out, CS->getOperand(i), TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000515 }
Jim Laskey6be3d8e2006-02-27 10:33:53 +0000516 if (N > 2) Indent.resize(Indent.size() - IndentSize);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000517 }
Jim Laskey3bb78742006-02-25 12:27:03 +0000518
Chris Lattnerd84bb632002-04-16 21:36:08 +0000519 Out << " }";
Brian Gaeke02209042004-08-20 06:00:58 +0000520 } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(CV)) {
521 const Type *ETy = CP->getType()->getElementType();
Misha Brukmanb1c93172005-04-21 23:48:37 +0000522 assert(CP->getNumOperands() > 0 &&
Brian Gaeke02209042004-08-20 06:00:58 +0000523 "Number of operands for a PackedConst must be > 0");
524 Out << '<';
525 Out << ' ';
526 printTypeInt(Out, ETy, TypeTable);
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000527 WriteAsOperandInternal(Out, CP->getOperand(0), TypeTable, Machine);
Brian Gaeke02209042004-08-20 06:00:58 +0000528 for (unsigned i = 1, e = CP->getNumOperands(); i != e; ++i) {
529 Out << ", ";
530 printTypeInt(Out, ETy, TypeTable);
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000531 WriteAsOperandInternal(Out, CP->getOperand(i), TypeTable, Machine);
Brian Gaeke02209042004-08-20 06:00:58 +0000532 }
533 Out << " >";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000534 } else if (isa<ConstantPointerNull>(CV)) {
535 Out << "null";
536
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000537 } else if (isa<UndefValue>(CV)) {
538 Out << "undef";
539
Chris Lattner3cd8c562002-07-30 18:54:25 +0000540 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Reid Spencer812a1be2006-12-04 05:19:18 +0000541 Out << CE->getOpcodeName();
542 if (CE->isCompare())
543 Out << " " << getPredicateText(CE->getPredicate());
544 Out << " (";
Misha Brukmanb1c93172005-04-21 23:48:37 +0000545
Vikram S. Adveb952b542002-07-14 23:14:45 +0000546 for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
547 printTypeInt(Out, (*OI)->getType(), TypeTable);
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000548 WriteAsOperandInternal(Out, *OI, TypeTable, Machine);
Vikram S. Adveb952b542002-07-14 23:14:45 +0000549 if (OI+1 != CE->op_end())
Chris Lattner3cd8c562002-07-30 18:54:25 +0000550 Out << ", ";
Vikram S. Adveb952b542002-07-14 23:14:45 +0000551 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000552
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000553 if (CE->isCast()) {
Chris Lattner83b396b2002-08-15 19:37:43 +0000554 Out << " to ";
555 printTypeInt(Out, CE->getType(), TypeTable);
556 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000557
Misha Brukman21bbdb92004-06-04 21:11:51 +0000558 Out << ')';
Chris Lattner83b396b2002-08-15 19:37:43 +0000559
Chris Lattnerd84bb632002-04-16 21:36:08 +0000560 } else {
Vikram S. Adveb952b542002-07-14 23:14:45 +0000561 Out << "<placeholder or erroneous Constant>";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000562 }
563}
564
565
Misha Brukmanc566ca362004-03-02 00:22:19 +0000566/// WriteAsOperand - Write the name of the specified value out to the specified
567/// ostream. This can be useful when you just want to print int %reg126, not
568/// the whole instruction that generated it.
569///
Misha Brukmanb1c93172005-04-21 23:48:37 +0000570static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000571 std::map<const Type*, std::string> &TypeTable,
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000572 SlotMachine *Machine) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000573 Out << ' ';
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000574 if (V->hasName())
Chris Lattner1c343042003-08-22 05:40:38 +0000575 Out << getLLVMName(V->getName());
Reid Spenceraccd7c72004-07-17 23:47:01 +0000576 else {
577 const Constant *CV = dyn_cast<Constant>(V);
Chris Lattnera2d810d2006-01-25 22:26:05 +0000578 if (CV && !isa<GlobalValue>(CV)) {
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000579 WriteConstantInt(Out, CV, TypeTable, Machine);
Chris Lattnera2d810d2006-01-25 22:26:05 +0000580 } else if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
581 Out << "asm ";
582 if (IA->hasSideEffects())
583 Out << "sideeffect ";
584 Out << '"';
585 PrintEscapedString(IA->getAsmString(), Out);
586 Out << "\", \"";
587 PrintEscapedString(IA->getConstraintString(), Out);
588 Out << '"';
589 } else {
Chris Lattnerd84bb632002-04-16 21:36:08 +0000590 int Slot;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000591 if (Machine) {
Reid Spencer56010e42004-05-26 21:56:09 +0000592 Slot = Machine->getSlot(V);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000593 } else {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000594 Machine = createSlotMachine(V);
Chris Lattner96749c42006-05-14 18:46:52 +0000595 if (Machine)
Chris Lattner757ee0b2004-06-09 19:41:19 +0000596 Slot = Machine->getSlot(V);
597 else
598 Slot = -1;
Reid Spencer56010e42004-05-26 21:56:09 +0000599 delete Machine;
Chris Lattnerd84bb632002-04-16 21:36:08 +0000600 }
Chris Lattner757ee0b2004-06-09 19:41:19 +0000601 if (Slot != -1)
602 Out << '%' << Slot;
603 else
604 Out << "<badref>";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000605 }
606 }
607}
608
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000609/// WriteAsOperand - Write the name of the specified value out to the specified
610/// ostream. This can be useful when you just want to print int %reg126, not
611/// the whole instruction that generated it.
612///
Chris Lattner189d19f2003-11-21 20:23:48 +0000613std::ostream &llvm::WriteAsOperand(std::ostream &Out, const Value *V,
Chris Lattner0dda8612006-12-06 06:15:43 +0000614 bool PrintType, const Module *Context) {
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000615 std::map<const Type *, std::string> TypeNames;
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000616 if (Context == 0) Context = getModuleFromVal(V);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000617
Chris Lattner98cf1f52002-11-20 18:36:02 +0000618 if (Context)
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000619 fillTypeNameTable(Context, TypeNames);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000620
621 if (PrintType)
622 printTypeInt(Out, V->getType(), TypeNames);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000623
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000624 WriteAsOperandInternal(Out, V, TypeNames, 0);
Chris Lattner5e5abe32001-07-20 19:15:21 +0000625 return Out;
626}
627
Reid Spencer58d30f22004-07-04 11:50:43 +0000628
Chris Lattner189d19f2003-11-21 20:23:48 +0000629namespace llvm {
Chris Lattner2e9fee42001-07-12 23:35:26 +0000630
Chris Lattnerfee714f2001-09-07 16:36:04 +0000631class AssemblyWriter {
Misha Brukmana6619a92004-06-21 21:53:56 +0000632 std::ostream &Out;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000633 SlotMachine &Machine;
Chris Lattner7bfee412001-10-29 16:05:51 +0000634 const Module *TheModule;
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000635 std::map<const Type *, std::string> TypeNames;
Chris Lattner8339f7d2003-10-30 23:41:03 +0000636 AssemblyAnnotationWriter *AnnotationWriter;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000637public:
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000638 inline AssemblyWriter(std::ostream &o, SlotMachine &Mac, const Module *M,
Chris Lattner8339f7d2003-10-30 23:41:03 +0000639 AssemblyAnnotationWriter *AAW)
Misha Brukmana6619a92004-06-21 21:53:56 +0000640 : Out(o), Machine(Mac), TheModule(M), AnnotationWriter(AAW) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000641
642 // If the module has a symbol table, take all global types and stuff their
643 // names into the TypeNames map.
644 //
Chris Lattnerb86620e2001-10-29 16:37:48 +0000645 fillTypeNameTable(M, TypeNames);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000646 }
647
Chris Lattner7bfee412001-10-29 16:05:51 +0000648 inline void write(const Module *M) { printModule(M); }
649 inline void write(const GlobalVariable *G) { printGlobal(G); }
Chris Lattner57698e22002-03-26 18:01:55 +0000650 inline void write(const Function *F) { printFunction(F); }
Chris Lattner7bfee412001-10-29 16:05:51 +0000651 inline void write(const BasicBlock *BB) { printBasicBlock(BB); }
Chris Lattner113f4f42002-06-25 16:13:24 +0000652 inline void write(const Instruction *I) { printInstruction(*I); }
Chris Lattner3462ae32001-12-03 22:26:30 +0000653 inline void write(const Constant *CPV) { printConstant(CPV); }
Chris Lattner7db79582001-11-07 04:21:57 +0000654 inline void write(const Type *Ty) { printType(Ty); }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000655
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000656 void writeOperand(const Value *Op, bool PrintType);
Chris Lattner1e194682002-04-18 18:53:13 +0000657
Misha Brukman4685e262004-04-28 15:31:21 +0000658 const Module* getModule() { return TheModule; }
659
Misha Brukmand92f54a2004-11-15 19:30:05 +0000660private:
Chris Lattner7bfee412001-10-29 16:05:51 +0000661 void printModule(const Module *M);
662 void printSymbolTable(const SymbolTable &ST);
Chris Lattner3462ae32001-12-03 22:26:30 +0000663 void printConstant(const Constant *CPV);
Chris Lattner7bfee412001-10-29 16:05:51 +0000664 void printGlobal(const GlobalVariable *GV);
Chris Lattner57698e22002-03-26 18:01:55 +0000665 void printFunction(const Function *F);
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000666 void printArgument(const Argument *FA);
Chris Lattner7bfee412001-10-29 16:05:51 +0000667 void printBasicBlock(const BasicBlock *BB);
Chris Lattner113f4f42002-06-25 16:13:24 +0000668 void printInstruction(const Instruction &I);
Chris Lattnerd816b532002-04-13 20:53:41 +0000669
670 // printType - Go to extreme measures to attempt to print out a short,
671 // symbolic version of a type name.
672 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000673 std::ostream &printType(const Type *Ty) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000674 return printTypeInt(Out, Ty, TypeNames);
Chris Lattnerd816b532002-04-13 20:53:41 +0000675 }
676
677 // printTypeAtLeastOneLevel - Print out one level of the possibly complex type
678 // without considering any symbolic types that we may have equal to it.
679 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000680 std::ostream &printTypeAtLeastOneLevel(const Type *Ty);
Chris Lattner7bfee412001-10-29 16:05:51 +0000681
Chris Lattner862e3382001-10-13 06:42:36 +0000682 // printInfoComment - Print a little comment after the instruction indicating
683 // which slot it occupies.
Chris Lattner113f4f42002-06-25 16:13:24 +0000684 void printInfoComment(const Value &V);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000685};
Reid Spencerf43ac622004-05-27 22:04:46 +0000686} // end of llvm namespace
Chris Lattner2f7c9632001-06-06 20:29:01 +0000687
Misha Brukmanc566ca362004-03-02 00:22:19 +0000688/// printTypeAtLeastOneLevel - Print out one level of the possibly complex type
689/// without considering any symbolic types that we may have equal to it.
690///
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000691std::ostream &AssemblyWriter::printTypeAtLeastOneLevel(const Type *Ty) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000692 if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000693 printType(FTy->getReturnType()) << " (";
Chris Lattnerfa829be2004-02-09 04:14:01 +0000694 for (FunctionType::param_iterator I = FTy->param_begin(),
695 E = FTy->param_end(); I != E; ++I) {
696 if (I != FTy->param_begin())
Misha Brukmana6619a92004-06-21 21:53:56 +0000697 Out << ", ";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000698 printType(*I);
Chris Lattnerd816b532002-04-13 20:53:41 +0000699 }
700 if (FTy->isVarArg()) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000701 if (FTy->getNumParams()) Out << ", ";
702 Out << "...";
Chris Lattnerd816b532002-04-13 20:53:41 +0000703 }
Misha Brukmana6619a92004-06-21 21:53:56 +0000704 Out << ')';
Chris Lattner113f4f42002-06-25 16:13:24 +0000705 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
Andrew Lenharthdcb3c972006-12-08 18:06:16 +0000706 if (STy->isPacked())
707 Out << '<';
Misha Brukmana6619a92004-06-21 21:53:56 +0000708 Out << "{ ";
Chris Lattnerac6db752004-02-09 04:37:31 +0000709 for (StructType::element_iterator I = STy->element_begin(),
710 E = STy->element_end(); I != E; ++I) {
711 if (I != STy->element_begin())
Misha Brukmana6619a92004-06-21 21:53:56 +0000712 Out << ", ";
Chris Lattnerd816b532002-04-13 20:53:41 +0000713 printType(*I);
714 }
Misha Brukmana6619a92004-06-21 21:53:56 +0000715 Out << " }";
Andrew Lenharthdcb3c972006-12-08 18:06:16 +0000716 if (STy->isPacked())
717 Out << '>';
Chris Lattner113f4f42002-06-25 16:13:24 +0000718 } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000719 printType(PTy->getElementType()) << '*';
Chris Lattner113f4f42002-06-25 16:13:24 +0000720 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000721 Out << '[' << ATy->getNumElements() << " x ";
Misha Brukman21bbdb92004-06-04 21:11:51 +0000722 printType(ATy->getElementType()) << ']';
Reid Spencere203d352004-08-20 15:37:30 +0000723 } else if (const PackedType *PTy = dyn_cast<PackedType>(Ty)) {
724 Out << '<' << PTy->getNumElements() << " x ";
725 printType(PTy->getElementType()) << '>';
726 }
Reid Spencerde46e482006-11-02 20:25:50 +0000727 else if (isa<OpaqueType>(Ty)) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000728 Out << "opaque";
Chris Lattnerd816b532002-04-13 20:53:41 +0000729 } else {
Vikram S. Adveb952b542002-07-14 23:14:45 +0000730 if (!Ty->isPrimitiveType())
Misha Brukmana6619a92004-06-21 21:53:56 +0000731 Out << "<unknown derived type>";
Chris Lattnerd816b532002-04-13 20:53:41 +0000732 printType(Ty);
733 }
Misha Brukmana6619a92004-06-21 21:53:56 +0000734 return Out;
Chris Lattnerd816b532002-04-13 20:53:41 +0000735}
736
737
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000738void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType) {
739 if (Operand == 0) {
Chris Lattner08f7d0c2005-02-24 16:58:29 +0000740 Out << "<null operand!>";
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000741 } else {
742 if (PrintType) { Out << ' '; printType(Operand->getType()); }
743 WriteAsOperandInternal(Out, Operand, TypeNames, &Machine);
Chris Lattner08f7d0c2005-02-24 16:58:29 +0000744 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000745}
746
Chris Lattner2f7c9632001-06-06 20:29:01 +0000747
Chris Lattner7bfee412001-10-29 16:05:51 +0000748void AssemblyWriter::printModule(const Module *M) {
Chris Lattner4d8689e2005-03-02 23:12:40 +0000749 if (!M->getModuleIdentifier().empty() &&
Misha Brukmanb1c93172005-04-21 23:48:37 +0000750 // Don't print the ID if it will start a new line (which would
Chris Lattner4d8689e2005-03-02 23:12:40 +0000751 // require a comment char before it).
752 M->getModuleIdentifier().find('\n') == std::string::npos)
753 Out << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
754
Owen Andersone2237542006-10-18 02:21:12 +0000755 if (!M->getDataLayout().empty())
Chris Lattner04897162006-10-22 06:06:56 +0000756 Out << "target datalayout = \"" << M->getDataLayout() << "\"\n";
Owen Andersone2237542006-10-18 02:21:12 +0000757
Chris Lattner8068e0c2003-08-24 13:48:48 +0000758 switch (M->getEndianness()) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000759 case Module::LittleEndian: Out << "target endian = little\n"; break;
760 case Module::BigEndian: Out << "target endian = big\n"; break;
Chris Lattner8068e0c2003-08-24 13:48:48 +0000761 case Module::AnyEndianness: break;
762 }
763 switch (M->getPointerSize()) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000764 case Module::Pointer32: Out << "target pointersize = 32\n"; break;
765 case Module::Pointer64: Out << "target pointersize = 64\n"; break;
Chris Lattner8068e0c2003-08-24 13:48:48 +0000766 case Module::AnyPointerSize: break;
767 }
Reid Spencer48f98c82004-07-25 21:44:54 +0000768 if (!M->getTargetTriple().empty())
Reid Spencerffec7df2004-07-25 21:29:43 +0000769 Out << "target triple = \"" << M->getTargetTriple() << "\"\n";
Misha Brukmanb1c93172005-04-21 23:48:37 +0000770
Chris Lattnereef2fe72006-01-24 04:13:11 +0000771 if (!M->getModuleInlineAsm().empty()) {
Chris Lattnerefaf35d2006-01-24 00:45:30 +0000772 // Split the string into lines, to make it easier to read the .ll file.
Chris Lattnereef2fe72006-01-24 04:13:11 +0000773 std::string Asm = M->getModuleInlineAsm();
Chris Lattnerefaf35d2006-01-24 00:45:30 +0000774 size_t CurPos = 0;
775 size_t NewLine = Asm.find_first_of('\n', CurPos);
776 while (NewLine != std::string::npos) {
777 // We found a newline, print the portion of the asm string from the
778 // last newline up to this newline.
779 Out << "module asm \"";
780 PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.begin()+NewLine),
781 Out);
782 Out << "\"\n";
783 CurPos = NewLine+1;
784 NewLine = Asm.find_first_of('\n', CurPos);
785 }
Chris Lattner3acaf5c2006-01-24 00:40:17 +0000786 Out << "module asm \"";
Chris Lattnerefaf35d2006-01-24 00:45:30 +0000787 PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.end()), Out);
Chris Lattner6ed87bd2006-01-23 23:03:36 +0000788 Out << "\"\n";
789 }
790
Chris Lattner2cdd49d2004-09-14 05:06:58 +0000791 // Loop over the dependent libraries and emit them.
Chris Lattner730cfe42004-09-14 04:51:44 +0000792 Module::lib_iterator LI = M->lib_begin();
793 Module::lib_iterator LE = M->lib_end();
Reid Spencer48f98c82004-07-25 21:44:54 +0000794 if (LI != LE) {
Chris Lattner730cfe42004-09-14 04:51:44 +0000795 Out << "deplibs = [ ";
796 while (LI != LE) {
Chris Lattner2cdd49d2004-09-14 05:06:58 +0000797 Out << '"' << *LI << '"';
Reid Spencerffec7df2004-07-25 21:29:43 +0000798 ++LI;
Chris Lattner730cfe42004-09-14 04:51:44 +0000799 if (LI != LE)
800 Out << ", ";
Reid Spencerffec7df2004-07-25 21:29:43 +0000801 }
802 Out << " ]\n";
Reid Spencercc5ff642004-07-25 18:08:18 +0000803 }
Reid Spencerb9e08772004-09-13 23:44:23 +0000804
Chris Lattner2cdd49d2004-09-14 05:06:58 +0000805 // Loop over the symbol table, emitting all named constants.
Chris Lattner98cf1f52002-11-20 18:36:02 +0000806 printSymbolTable(M->getSymbolTable());
Misha Brukmanb1c93172005-04-21 23:48:37 +0000807
Chris Lattner54932b02006-12-06 04:41:52 +0000808 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
809 I != E; ++I)
Chris Lattner113f4f42002-06-25 16:13:24 +0000810 printGlobal(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000811
Misha Brukmana6619a92004-06-21 21:53:56 +0000812 Out << "\nimplementation ; Functions:\n";
Misha Brukmanb1c93172005-04-21 23:48:37 +0000813
Chris Lattner2cdd49d2004-09-14 05:06:58 +0000814 // Output all of the functions.
Chris Lattner113f4f42002-06-25 16:13:24 +0000815 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
816 printFunction(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000817}
818
Chris Lattner7bfee412001-10-29 16:05:51 +0000819void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000820 if (GV->hasName()) Out << getLLVMName(GV->getName()) << " = ";
Chris Lattner37798642001-09-18 04:01:05 +0000821
Misha Brukmanb1c93172005-04-21 23:48:37 +0000822 if (!GV->hasInitializer())
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000823 switch (GV->getLinkage()) {
824 case GlobalValue::DLLImportLinkage: Out << "dllimport "; break;
825 case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break;
826 default: Out << "external "; break;
827 }
Chris Lattner379a8d22003-04-16 20:28:45 +0000828 else
829 switch (GV->getLinkage()) {
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000830 case GlobalValue::InternalLinkage: Out << "internal "; break;
831 case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
832 case GlobalValue::WeakLinkage: Out << "weak "; break;
833 case GlobalValue::AppendingLinkage: Out << "appending "; break;
834 case GlobalValue::DLLImportLinkage: Out << "dllimport "; break;
835 case GlobalValue::DLLExportLinkage: Out << "dllexport "; break;
836 case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break;
837 case GlobalValue::ExternalLinkage: break;
Misha Brukman70457632004-11-14 21:04:34 +0000838 case GlobalValue::GhostLinkage:
Bill Wendlingf3baad32006-12-07 01:30:32 +0000839 cerr << "GhostLinkage not allowed in AsmWriter!\n";
Misha Brukman70457632004-11-14 21:04:34 +0000840 abort();
Chris Lattner379a8d22003-04-16 20:28:45 +0000841 }
Chris Lattner37798642001-09-18 04:01:05 +0000842
Misha Brukmana6619a92004-06-21 21:53:56 +0000843 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattner2413b162001-12-04 00:03:30 +0000844 printType(GV->getType()->getElementType());
Chris Lattner37798642001-09-18 04:01:05 +0000845
Reid Spenceraccd7c72004-07-17 23:47:01 +0000846 if (GV->hasInitializer()) {
847 Constant* C = cast<Constant>(GV->getInitializer());
848 assert(C && "GlobalVar initializer isn't constant?");
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000849 writeOperand(GV->getInitializer(), false);
Reid Spenceraccd7c72004-07-17 23:47:01 +0000850 }
Chris Lattnerf8a974d2005-11-06 06:48:53 +0000851
Chris Lattner4b96c542005-11-12 00:10:19 +0000852 if (GV->hasSection())
853 Out << ", section \"" << GV->getSection() << '"';
854 if (GV->getAlignment())
Chris Lattnerf8a974d2005-11-06 06:48:53 +0000855 Out << ", align " << GV->getAlignment();
Chris Lattner4b96c542005-11-12 00:10:19 +0000856
Chris Lattner113f4f42002-06-25 16:13:24 +0000857 printInfoComment(*GV);
Misha Brukmana6619a92004-06-21 21:53:56 +0000858 Out << "\n";
Chris Lattnerda975502001-09-10 07:58:01 +0000859}
860
Chris Lattnerfee714f2001-09-07 16:36:04 +0000861
Reid Spencere7e96712004-05-25 08:53:40 +0000862// printSymbolTable - Run through symbol table looking for constants
863// and types. Emit their declarations.
Chris Lattner7bfee412001-10-29 16:05:51 +0000864void AssemblyWriter::printSymbolTable(const SymbolTable &ST) {
Reid Spencere7e96712004-05-25 08:53:40 +0000865
866 // Print the types.
867 for (SymbolTable::type_const_iterator TI = ST.type_begin();
Chris Lattner23e829a2006-12-06 05:12:21 +0000868 TI != ST.type_end(); ++TI) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000869 Out << "\t" << getLLVMName(TI->first) << " = type ";
Reid Spencere7e96712004-05-25 08:53:40 +0000870
871 // Make sure we print out at least one level of the type structure, so
872 // that we do not get %FILE = type %FILE
873 //
874 printTypeAtLeastOneLevel(TI->second) << "\n";
875 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000876
Reid Spencere7e96712004-05-25 08:53:40 +0000877 // Print the constants, in type plane order.
878 for (SymbolTable::plane_const_iterator PI = ST.plane_begin();
Chris Lattner23e829a2006-12-06 05:12:21 +0000879 PI != ST.plane_end(); ++PI) {
Reid Spencere7e96712004-05-25 08:53:40 +0000880 SymbolTable::value_const_iterator VI = ST.value_begin(PI->first);
881 SymbolTable::value_const_iterator VE = ST.value_end(PI->first);
882
883 for (; VI != VE; ++VI) {
Reid Spenceraccd7c72004-07-17 23:47:01 +0000884 const Value* V = VI->second;
885 const Constant *CPV = dyn_cast<Constant>(V) ;
886 if (CPV && !isa<GlobalValue>(V)) {
Reid Spencer56010e42004-05-26 21:56:09 +0000887 printConstant(CPV);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000888 }
889 }
Chris Lattnera7620d92001-07-15 06:35:59 +0000890 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000891}
892
893
Misha Brukmanc566ca362004-03-02 00:22:19 +0000894/// printConstant - Print out a constant pool entry...
895///
Chris Lattner3462ae32001-12-03 22:26:30 +0000896void AssemblyWriter::printConstant(const Constant *CPV) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000897 // Don't print out unnamed constants, they will be inlined
898 if (!CPV->hasName()) return;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000899
Chris Lattneree998be2001-07-26 16:29:38 +0000900 // Print out name...
Misha Brukmana6619a92004-06-21 21:53:56 +0000901 Out << "\t" << getLLVMName(CPV->getName()) << " =";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000902
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000903 // Write the value out now.
904 writeOperand(CPV, true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000905
Chris Lattner113f4f42002-06-25 16:13:24 +0000906 printInfoComment(*CPV);
Misha Brukmana6619a92004-06-21 21:53:56 +0000907 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000908}
909
Misha Brukmanc566ca362004-03-02 00:22:19 +0000910/// printFunction - Print all aspects of a function.
911///
Chris Lattner113f4f42002-06-25 16:13:24 +0000912void AssemblyWriter::printFunction(const Function *F) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000913 // Print out the return type and name...
Misha Brukmana6619a92004-06-21 21:53:56 +0000914 Out << "\n";
Chris Lattner379a8d22003-04-16 20:28:45 +0000915
Chris Lattner97e36f22004-12-05 06:44:09 +0000916 // Ensure that no local symbols conflict with global symbols.
917 const_cast<Function*>(F)->renameLocalSymbols();
918
Misha Brukmana6619a92004-06-21 21:53:56 +0000919 if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +0000920
Chris Lattner379a8d22003-04-16 20:28:45 +0000921 if (F->isExternal())
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000922 switch (F->getLinkage()) {
923 case GlobalValue::DLLImportLinkage: Out << "declare dllimport "; break;
924 case GlobalValue::ExternalWeakLinkage: Out << "declare extern_weak "; break;
925 default: Out << "declare ";
926 }
Chris Lattner379a8d22003-04-16 20:28:45 +0000927 else
928 switch (F->getLinkage()) {
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000929 case GlobalValue::InternalLinkage: Out << "internal "; break;
930 case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
931 case GlobalValue::WeakLinkage: Out << "weak "; break;
932 case GlobalValue::AppendingLinkage: Out << "appending "; break;
933 case GlobalValue::DLLImportLinkage: Out << "dllimport "; break;
934 case GlobalValue::DLLExportLinkage: Out << "dllexport "; break;
935 case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break;
Chris Lattner379a8d22003-04-16 20:28:45 +0000936 case GlobalValue::ExternalLinkage: break;
Misha Brukman70457632004-11-14 21:04:34 +0000937 case GlobalValue::GhostLinkage:
Bill Wendlingf3baad32006-12-07 01:30:32 +0000938 cerr << "GhostLinkage not allowed in AsmWriter!\n";
Misha Brukman70457632004-11-14 21:04:34 +0000939 abort();
Chris Lattner379a8d22003-04-16 20:28:45 +0000940 }
941
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000942 // Print the calling convention.
943 switch (F->getCallingConv()) {
944 case CallingConv::C: break; // default
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +0000945 case CallingConv::CSRet: Out << "csretcc "; break;
946 case CallingConv::Fast: Out << "fastcc "; break;
947 case CallingConv::Cold: Out << "coldcc "; break;
948 case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break;
949 case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break;
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000950 default: Out << "cc" << F->getCallingConv() << " "; break;
951 }
952
Misha Brukman21bbdb92004-06-04 21:11:51 +0000953 printType(F->getReturnType()) << ' ';
Chris Lattner5b337482003-10-18 05:57:43 +0000954 if (!F->getName().empty())
Misha Brukmana6619a92004-06-21 21:53:56 +0000955 Out << getLLVMName(F->getName());
Chris Lattner5b337482003-10-18 05:57:43 +0000956 else
Misha Brukmana6619a92004-06-21 21:53:56 +0000957 Out << "\"\"";
958 Out << '(';
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000959 Machine.incorporateFunction(F);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000960
Chris Lattner7bfee412001-10-29 16:05:51 +0000961 // Loop over the arguments, printing them...
Chris Lattner113f4f42002-06-25 16:13:24 +0000962 const FunctionType *FT = F->getFunctionType();
Chris Lattnerfee714f2001-09-07 16:36:04 +0000963
Chris Lattner54932b02006-12-06 04:41:52 +0000964 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
965 I != E; ++I)
Chris Lattner149376d2002-10-13 20:57:00 +0000966 printArgument(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000967
968 // Finish printing arguments...
Chris Lattner113f4f42002-06-25 16:13:24 +0000969 if (FT->isVarArg()) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000970 if (FT->getNumParams()) Out << ", ";
971 Out << "..."; // Output varargs portion of signature!
Chris Lattnerfee714f2001-09-07 16:36:04 +0000972 }
Misha Brukmana6619a92004-06-21 21:53:56 +0000973 Out << ')';
Chris Lattnerfee714f2001-09-07 16:36:04 +0000974
Chris Lattner4b96c542005-11-12 00:10:19 +0000975 if (F->hasSection())
976 Out << " section \"" << F->getSection() << '"';
Chris Lattnerf8a974d2005-11-06 06:48:53 +0000977 if (F->getAlignment())
978 Out << " align " << F->getAlignment();
Chris Lattner4b96c542005-11-12 00:10:19 +0000979
Chris Lattner113f4f42002-06-25 16:13:24 +0000980 if (F->isExternal()) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000981 Out << "\n";
Chris Lattnerb2f02e52002-05-06 03:00:40 +0000982 } else {
Misha Brukmana6619a92004-06-21 21:53:56 +0000983 Out << " {";
Misha Brukmanb1c93172005-04-21 23:48:37 +0000984
Chris Lattner6915f8f2002-04-07 22:49:37 +0000985 // Output all of its basic blocks... for the function
Chris Lattner113f4f42002-06-25 16:13:24 +0000986 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
987 printBasicBlock(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000988
Misha Brukmana6619a92004-06-21 21:53:56 +0000989 Out << "}\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000990 }
991
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000992 Machine.purgeFunction();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000993}
994
Misha Brukmanc566ca362004-03-02 00:22:19 +0000995/// printArgument - This member is called for every argument that is passed into
996/// the function. Simply print it out
997///
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000998void AssemblyWriter::printArgument(const Argument *Arg) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000999 // Insert commas as we go... the first arg doesn't get a comma
Chris Lattnerfb972ad2005-03-15 05:03:36 +00001000 if (Arg != Arg->getParent()->arg_begin()) Out << ", ";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001001
1002 // Output type...
Chris Lattner7bfee412001-10-29 16:05:51 +00001003 printType(Arg->getType());
Misha Brukmanb1c93172005-04-21 23:48:37 +00001004
Chris Lattner2f7c9632001-06-06 20:29:01 +00001005 // Output name, if available...
1006 if (Arg->hasName())
Misha Brukmana6619a92004-06-21 21:53:56 +00001007 Out << ' ' << getLLVMName(Arg->getName());
Chris Lattner2f7c9632001-06-06 20:29:01 +00001008}
1009
Misha Brukmanc566ca362004-03-02 00:22:19 +00001010/// printBasicBlock - This member is called for each basic block in a method.
1011///
Chris Lattner7bfee412001-10-29 16:05:51 +00001012void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001013 if (BB->hasName()) { // Print out the label if it exists...
Alkis Evlogimenos991d6ad2004-12-10 05:41:10 +00001014 Out << "\n" << getLLVMName(BB->getName(), false) << ':';
Chris Lattner408dbdb2002-05-14 16:02:05 +00001015 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Misha Brukmana6619a92004-06-21 21:53:56 +00001016 Out << "\n; <label>:";
Chris Lattner757ee0b2004-06-09 19:41:19 +00001017 int Slot = Machine.getSlot(BB);
1018 if (Slot != -1)
Misha Brukmana6619a92004-06-21 21:53:56 +00001019 Out << Slot;
Chris Lattner757ee0b2004-06-09 19:41:19 +00001020 else
Misha Brukmana6619a92004-06-21 21:53:56 +00001021 Out << "<badref>";
Chris Lattner58185f22002-10-02 19:38:55 +00001022 }
Chris Lattner2447ef52003-11-20 00:09:43 +00001023
1024 if (BB->getParent() == 0)
Misha Brukmana6619a92004-06-21 21:53:56 +00001025 Out << "\t\t; Error: Block without parent!";
Chris Lattner2447ef52003-11-20 00:09:43 +00001026 else {
1027 if (BB != &BB->getParent()->front()) { // Not the entry block?
1028 // Output predecessors for the block...
Misha Brukmana6619a92004-06-21 21:53:56 +00001029 Out << "\t\t;";
Chris Lattner2447ef52003-11-20 00:09:43 +00001030 pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
Misha Brukmanb1c93172005-04-21 23:48:37 +00001031
Chris Lattner2447ef52003-11-20 00:09:43 +00001032 if (PI == PE) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001033 Out << " No predecessors!";
Chris Lattner2447ef52003-11-20 00:09:43 +00001034 } else {
Misha Brukmana6619a92004-06-21 21:53:56 +00001035 Out << " preds =";
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001036 writeOperand(*PI, false);
Chris Lattner2447ef52003-11-20 00:09:43 +00001037 for (++PI; PI != PE; ++PI) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001038 Out << ',';
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001039 writeOperand(*PI, false);
Chris Lattner2447ef52003-11-20 00:09:43 +00001040 }
Chris Lattner00211f12003-11-16 22:59:57 +00001041 }
Chris Lattner58185f22002-10-02 19:38:55 +00001042 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001043 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001044
Misha Brukmana6619a92004-06-21 21:53:56 +00001045 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001046
Misha Brukmana6619a92004-06-21 21:53:56 +00001047 if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001048
Chris Lattnerfee714f2001-09-07 16:36:04 +00001049 // Output all of the instructions in the basic block...
Chris Lattner113f4f42002-06-25 16:13:24 +00001050 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
1051 printInstruction(*I);
Chris Lattner96cdd272004-03-08 18:51:45 +00001052
Misha Brukmana6619a92004-06-21 21:53:56 +00001053 if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001054}
1055
Chris Lattner862e3382001-10-13 06:42:36 +00001056
Misha Brukmanc566ca362004-03-02 00:22:19 +00001057/// printInfoComment - Print a little comment after the instruction indicating
1058/// which slot it occupies.
1059///
Chris Lattner113f4f42002-06-25 16:13:24 +00001060void AssemblyWriter::printInfoComment(const Value &V) {
1061 if (V.getType() != Type::VoidTy) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001062 Out << "\t\t; <";
Misha Brukman21bbdb92004-06-04 21:11:51 +00001063 printType(V.getType()) << '>';
Chris Lattner862e3382001-10-13 06:42:36 +00001064
Chris Lattner113f4f42002-06-25 16:13:24 +00001065 if (!V.hasName()) {
Chris Lattner757ee0b2004-06-09 19:41:19 +00001066 int SlotNum = Machine.getSlot(&V);
1067 if (SlotNum == -1)
Misha Brukmana6619a92004-06-21 21:53:56 +00001068 Out << ":<badref>";
Reid Spencer8beac692004-06-09 15:26:53 +00001069 else
Misha Brukmana6619a92004-06-21 21:53:56 +00001070 Out << ':' << SlotNum; // Print out the def slot taken.
Chris Lattner862e3382001-10-13 06:42:36 +00001071 }
Chris Lattnerb6c21db2005-02-01 01:24:01 +00001072 Out << " [#uses=" << V.getNumUses() << ']'; // Output # uses
Chris Lattner862e3382001-10-13 06:42:36 +00001073 }
1074}
1075
Reid Spencere7141c82006-08-28 01:02:49 +00001076// This member is called for each Instruction in a function..
Chris Lattner113f4f42002-06-25 16:13:24 +00001077void AssemblyWriter::printInstruction(const Instruction &I) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001078 if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001079
Misha Brukmana6619a92004-06-21 21:53:56 +00001080 Out << "\t";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001081
1082 // Print out name if it exists...
Chris Lattner113f4f42002-06-25 16:13:24 +00001083 if (I.hasName())
Misha Brukmana6619a92004-06-21 21:53:56 +00001084 Out << getLLVMName(I.getName()) << " = ";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001085
Chris Lattner06038452005-05-06 05:51:46 +00001086 // If this is a volatile load or store, print out the volatile marker.
Chris Lattner504f9242003-09-08 17:45:59 +00001087 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
Chris Lattner06038452005-05-06 05:51:46 +00001088 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile())) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001089 Out << "volatile ";
Chris Lattner06038452005-05-06 05:51:46 +00001090 } else if (isa<CallInst>(I) && cast<CallInst>(I).isTailCall()) {
1091 // If this is a call, check if it's a tail call.
1092 Out << "tail ";
1093 }
Chris Lattner504f9242003-09-08 17:45:59 +00001094
Chris Lattner2f7c9632001-06-06 20:29:01 +00001095 // Print out the opcode...
Misha Brukmana6619a92004-06-21 21:53:56 +00001096 Out << I.getOpcodeName();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001097
Reid Spencer45e52392006-12-03 06:27:29 +00001098 // Print out the compare instruction predicates
1099 if (const FCmpInst *FCI = dyn_cast<FCmpInst>(&I)) {
Reid Spencer812a1be2006-12-04 05:19:18 +00001100 Out << " " << getPredicateText(FCI->getPredicate());
Reid Spencer45e52392006-12-03 06:27:29 +00001101 } else if (const ICmpInst *ICI = dyn_cast<ICmpInst>(&I)) {
Reid Spencer812a1be2006-12-04 05:19:18 +00001102 Out << " " << getPredicateText(ICI->getPredicate());
Reid Spencer45e52392006-12-03 06:27:29 +00001103 }
1104
Chris Lattner2f7c9632001-06-06 20:29:01 +00001105 // Print out the type of the operands...
Chris Lattner113f4f42002-06-25 16:13:24 +00001106 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001107
1108 // Special case conditional branches to swizzle the condition out to the front
Chris Lattner113f4f42002-06-25 16:13:24 +00001109 if (isa<BranchInst>(I) && I.getNumOperands() > 1) {
1110 writeOperand(I.getOperand(2), true);
Misha Brukmana6619a92004-06-21 21:53:56 +00001111 Out << ',';
Chris Lattner2f7c9632001-06-06 20:29:01 +00001112 writeOperand(Operand, true);
Misha Brukmana6619a92004-06-21 21:53:56 +00001113 Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001114 writeOperand(I.getOperand(1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001115
Chris Lattner8d48df22002-04-13 18:34:38 +00001116 } else if (isa<SwitchInst>(I)) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001117 // Special case switch statement to get formatting nice and correct...
Misha Brukmana6619a92004-06-21 21:53:56 +00001118 writeOperand(Operand , true); Out << ',';
1119 writeOperand(I.getOperand(1), true); Out << " [";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001120
Chris Lattner113f4f42002-06-25 16:13:24 +00001121 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001122 Out << "\n\t\t";
1123 writeOperand(I.getOperand(op ), true); Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001124 writeOperand(I.getOperand(op+1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001125 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001126 Out << "\n\t]";
Chris Lattnerda558102001-10-02 03:41:24 +00001127 } else if (isa<PHINode>(I)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001128 Out << ' ';
Chris Lattner113f4f42002-06-25 16:13:24 +00001129 printType(I.getType());
Misha Brukmana6619a92004-06-21 21:53:56 +00001130 Out << ' ';
Chris Lattner2f7c9632001-06-06 20:29:01 +00001131
Chris Lattner113f4f42002-06-25 16:13:24 +00001132 for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001133 if (op) Out << ", ";
Misha Brukmanb1c93172005-04-21 23:48:37 +00001134 Out << '[';
Misha Brukmana6619a92004-06-21 21:53:56 +00001135 writeOperand(I.getOperand(op ), false); Out << ',';
1136 writeOperand(I.getOperand(op+1), false); Out << " ]";
Chris Lattner931ef3b2001-06-11 15:04:20 +00001137 }
Chris Lattner862e3382001-10-13 06:42:36 +00001138 } else if (isa<ReturnInst>(I) && !Operand) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001139 Out << " void";
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001140 } else if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
1141 // Print the calling convention being used.
1142 switch (CI->getCallingConv()) {
1143 case CallingConv::C: break; // default
Chris Lattner29d20852006-05-19 21:58:52 +00001144 case CallingConv::CSRet: Out << " csretcc"; break;
1145 case CallingConv::Fast: Out << " fastcc"; break;
1146 case CallingConv::Cold: Out << " coldcc"; break;
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +00001147 case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break;
1148 case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break;
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001149 default: Out << " cc" << CI->getCallingConv(); break;
1150 }
1151
Chris Lattner463d6a52003-08-05 15:34:45 +00001152 const PointerType *PTy = cast<PointerType>(Operand->getType());
1153 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1154 const Type *RetTy = FTy->getReturnType();
Chris Lattner2f2d9472001-11-06 21:28:12 +00001155
Chris Lattner463d6a52003-08-05 15:34:45 +00001156 // If possible, print out the short form of the call instruction. We can
Chris Lattner6915f8f2002-04-07 22:49:37 +00001157 // only do this if the first argument is a pointer to a nonvararg function,
Chris Lattner463d6a52003-08-05 15:34:45 +00001158 // and if the return type is not a pointer to a function.
Chris Lattner2f2d9472001-11-06 21:28:12 +00001159 //
Chris Lattner463d6a52003-08-05 15:34:45 +00001160 if (!FTy->isVarArg() &&
Misha Brukmanb1c93172005-04-21 23:48:37 +00001161 (!isa<PointerType>(RetTy) ||
Chris Lattnerd9a36a62002-07-25 20:58:51 +00001162 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001163 Out << ' '; printType(RetTy);
Chris Lattner2f2d9472001-11-06 21:28:12 +00001164 writeOperand(Operand, false);
1165 } else {
1166 writeOperand(Operand, true);
1167 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001168 Out << '(';
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001169 if (CI->getNumOperands() > 1) writeOperand(CI->getOperand(1), true);
Chris Lattner113f4f42002-06-25 16:13:24 +00001170 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; ++op) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001171 Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001172 writeOperand(I.getOperand(op), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001173 }
1174
Misha Brukmana6619a92004-06-21 21:53:56 +00001175 Out << " )";
Chris Lattner113f4f42002-06-25 16:13:24 +00001176 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Chris Lattner463d6a52003-08-05 15:34:45 +00001177 const PointerType *PTy = cast<PointerType>(Operand->getType());
1178 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1179 const Type *RetTy = FTy->getReturnType();
1180
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001181 // Print the calling convention being used.
1182 switch (II->getCallingConv()) {
1183 case CallingConv::C: break; // default
Chris Lattner29d20852006-05-19 21:58:52 +00001184 case CallingConv::CSRet: Out << " csretcc"; break;
1185 case CallingConv::Fast: Out << " fastcc"; break;
1186 case CallingConv::Cold: Out << " coldcc"; break;
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +00001187 case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break;
1188 case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break;
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001189 default: Out << " cc" << II->getCallingConv(); break;
1190 }
1191
Chris Lattner463d6a52003-08-05 15:34:45 +00001192 // If possible, print out the short form of the invoke instruction. We can
1193 // only do this if the first argument is a pointer to a nonvararg function,
1194 // and if the return type is not a pointer to a function.
1195 //
1196 if (!FTy->isVarArg() &&
Misha Brukmanb1c93172005-04-21 23:48:37 +00001197 (!isa<PointerType>(RetTy) ||
Chris Lattner463d6a52003-08-05 15:34:45 +00001198 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001199 Out << ' '; printType(RetTy);
Chris Lattner463d6a52003-08-05 15:34:45 +00001200 writeOperand(Operand, false);
1201 } else {
1202 writeOperand(Operand, true);
1203 }
1204
Misha Brukmana6619a92004-06-21 21:53:56 +00001205 Out << '(';
Chris Lattner113f4f42002-06-25 16:13:24 +00001206 if (I.getNumOperands() > 3) writeOperand(I.getOperand(3), true);
1207 for (unsigned op = 4, Eop = I.getNumOperands(); op < Eop; ++op) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001208 Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001209 writeOperand(I.getOperand(op), true);
Chris Lattner862e3382001-10-13 06:42:36 +00001210 }
1211
Misha Brukmana6619a92004-06-21 21:53:56 +00001212 Out << " )\n\t\t\tto";
Chris Lattner862e3382001-10-13 06:42:36 +00001213 writeOperand(II->getNormalDest(), true);
Misha Brukmana6619a92004-06-21 21:53:56 +00001214 Out << " unwind";
Chris Lattnerfae8ab32004-02-08 21:44:31 +00001215 writeOperand(II->getUnwindDest(), true);
Chris Lattner862e3382001-10-13 06:42:36 +00001216
Chris Lattner113f4f42002-06-25 16:13:24 +00001217 } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(&I)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001218 Out << ' ';
Chris Lattner8d48df22002-04-13 18:34:38 +00001219 printType(AI->getType()->getElementType());
1220 if (AI->isArrayAllocation()) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001221 Out << ',';
Chris Lattner8d48df22002-04-13 18:34:38 +00001222 writeOperand(AI->getArraySize(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001223 }
Nate Begeman848622f2005-11-05 09:21:28 +00001224 if (AI->getAlignment()) {
Chris Lattner7aeee3a2005-11-05 21:20:34 +00001225 Out << ", align " << AI->getAlignment();
Nate Begeman848622f2005-11-05 09:21:28 +00001226 }
Chris Lattner862e3382001-10-13 06:42:36 +00001227 } else if (isa<CastInst>(I)) {
Chris Lattnerc96e96b2003-11-17 01:17:04 +00001228 if (Operand) writeOperand(Operand, true); // Work with broken code
Misha Brukmana6619a92004-06-21 21:53:56 +00001229 Out << " to ";
Chris Lattner113f4f42002-06-25 16:13:24 +00001230 printType(I.getType());
Chris Lattner5b337482003-10-18 05:57:43 +00001231 } else if (isa<VAArgInst>(I)) {
Chris Lattnerc96e96b2003-11-17 01:17:04 +00001232 if (Operand) writeOperand(Operand, true); // Work with broken code
Misha Brukmana6619a92004-06-21 21:53:56 +00001233 Out << ", ";
Chris Lattnerf70da102003-05-08 02:44:12 +00001234 printType(I.getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +00001235 } else if (Operand) { // Print the normal way...
1236
Misha Brukmanb1c93172005-04-21 23:48:37 +00001237 // PrintAllTypes - Instructions who have operands of all the same type
Chris Lattner2f7c9632001-06-06 20:29:01 +00001238 // omit the type from all but the first operand. If the instruction has
1239 // different type operands (for example br), then they are all printed.
1240 bool PrintAllTypes = false;
1241 const Type *TheType = Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001242
Chris Lattner52bd5cb92004-03-12 05:53:14 +00001243 // Shift Left & Right print both types even for Ubyte LHS, and select prints
1244 // types even if all operands are bools.
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001245 if (isa<ShiftInst>(I) || isa<SelectInst>(I) || isa<StoreInst>(I) ||
1246 isa<ShuffleVectorInst>(I)) {
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00001247 PrintAllTypes = true;
1248 } else {
1249 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
1250 Operand = I.getOperand(i);
1251 if (Operand->getType() != TheType) {
1252 PrintAllTypes = true; // We have differing types! Print them all!
1253 break;
1254 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001255 }
1256 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001257
Chris Lattner7bfee412001-10-29 16:05:51 +00001258 if (!PrintAllTypes) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001259 Out << ' ';
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00001260 printType(TheType);
Chris Lattner7bfee412001-10-29 16:05:51 +00001261 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001262
Chris Lattner113f4f42002-06-25 16:13:24 +00001263 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001264 if (i) Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001265 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001266 }
1267 }
1268
Chris Lattner862e3382001-10-13 06:42:36 +00001269 printInfoComment(I);
Misha Brukmana6619a92004-06-21 21:53:56 +00001270 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001271}
1272
1273
1274//===----------------------------------------------------------------------===//
1275// External Interface declarations
1276//===----------------------------------------------------------------------===//
1277
Chris Lattner8339f7d2003-10-30 23:41:03 +00001278void Module::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001279 SlotMachine SlotTable(this);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001280 AssemblyWriter W(o, SlotTable, this, AAW);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001281 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001282}
1283
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001284void GlobalVariable::print(std::ostream &o) const {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001285 SlotMachine SlotTable(getParent());
Chris Lattner8339f7d2003-10-30 23:41:03 +00001286 AssemblyWriter W(o, SlotTable, getParent(), 0);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001287 W.write(this);
Chris Lattneradfe0d12001-09-10 20:08:19 +00001288}
1289
Chris Lattner8339f7d2003-10-30 23:41:03 +00001290void Function::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001291 SlotMachine SlotTable(getParent());
Chris Lattner8339f7d2003-10-30 23:41:03 +00001292 AssemblyWriter W(o, SlotTable, getParent(), AAW);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001293
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001294 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001295}
1296
Chris Lattnereef2fe72006-01-24 04:13:11 +00001297void InlineAsm::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001298 WriteAsOperand(o, this, true, 0);
Chris Lattnereef2fe72006-01-24 04:13:11 +00001299}
1300
Chris Lattner8339f7d2003-10-30 23:41:03 +00001301void BasicBlock::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001302 SlotMachine SlotTable(getParent());
Misha Brukmanb1c93172005-04-21 23:48:37 +00001303 AssemblyWriter W(o, SlotTable,
Chris Lattner8339f7d2003-10-30 23:41:03 +00001304 getParent() ? getParent()->getParent() : 0, AAW);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001305 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001306}
1307
Chris Lattner8339f7d2003-10-30 23:41:03 +00001308void Instruction::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001309 const Function *F = getParent() ? getParent()->getParent() : 0;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001310 SlotMachine SlotTable(F);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001311 AssemblyWriter W(o, SlotTable, F ? F->getParent() : 0, AAW);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001312
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001313 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001314}
Chris Lattner7db79582001-11-07 04:21:57 +00001315
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001316void Constant::print(std::ostream &o) const {
1317 if (this == 0) { o << "<null> constant value\n"; return; }
Chris Lattner7d734802002-09-10 15:53:49 +00001318
Misha Brukman21bbdb92004-06-04 21:11:51 +00001319 o << ' ' << getType()->getDescription() << ' ';
Evan Cheng5b19a802006-03-01 22:17:00 +00001320
1321 std::map<const Type *, std::string> TypeTable;
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001322 WriteConstantInt(o, this, TypeTable, 0);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001323}
1324
Misha Brukmanb1c93172005-04-21 23:48:37 +00001325void Type::print(std::ostream &o) const {
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001326 if (this == 0)
1327 o << "<null Type>";
1328 else
1329 o << getDescription();
1330}
1331
Chris Lattner2e9fa6d2002-04-09 19:48:49 +00001332void Argument::print(std::ostream &o) const {
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001333 WriteAsOperand(o, this, true, getParent() ? getParent()->getParent() : 0);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001334}
1335
Reid Spencer52641832004-05-25 18:14:38 +00001336// Value::dump - allow easy printing of Values from the debugger.
1337// Located here because so much of the needed functionality is here.
Bill Wendling22e978a2006-12-07 20:04:42 +00001338void Value::dump() const { print(*cerr.stream()); cerr << '\n'; }
Reid Spencer52641832004-05-25 18:14:38 +00001339
1340// Type::dump - allow easy printing of Values from the debugger.
1341// Located here because so much of the needed functionality is here.
Bill Wendling22e978a2006-12-07 20:04:42 +00001342void Type::dump() const { print(*cerr.stream()); cerr << '\n'; }
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001343
1344//===----------------------------------------------------------------------===//
Chris Lattnerfc9f1c92006-12-06 06:40:49 +00001345// SlotMachine Implementation
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001346//===----------------------------------------------------------------------===//
1347
1348#if 0
Bill Wendlingf3baad32006-12-07 01:30:32 +00001349#define SC_DEBUG(X) cerr << X
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001350#else
1351#define SC_DEBUG(X)
1352#endif
1353
1354// Module level constructor. Causes the contents of the Module (sans functions)
1355// to be added to the slot table.
Misha Brukmanb1c93172005-04-21 23:48:37 +00001356SlotMachine::SlotMachine(const Module *M)
Reid Spencer56010e42004-05-26 21:56:09 +00001357 : TheModule(M) ///< Saved for lazy initialization.
1358 , TheFunction(0)
Reid Spencerb0ac8c42004-08-16 07:46:33 +00001359 , FunctionProcessed(false)
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001360{
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001361}
1362
1363// Function level constructor. Causes the contents of the Module and the one
1364// function provided to be added to the slot table.
Chris Lattner23e829a2006-12-06 05:12:21 +00001365SlotMachine::SlotMachine(const Function *F)
1366 : TheModule(F ? F->getParent() : 0) ///< Saved for lazy initialization
Reid Spencer56010e42004-05-26 21:56:09 +00001367 , TheFunction(F) ///< Saved for lazy initialization
Reid Spencerb0ac8c42004-08-16 07:46:33 +00001368 , FunctionProcessed(false)
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001369{
Reid Spencer56010e42004-05-26 21:56:09 +00001370}
1371
1372inline void SlotMachine::initialize(void) {
Chris Lattner23e829a2006-12-06 05:12:21 +00001373 if (TheModule) {
Misha Brukmanb1c93172005-04-21 23:48:37 +00001374 processModule();
Reid Spencer56010e42004-05-26 21:56:09 +00001375 TheModule = 0; ///< Prevent re-processing next time we're called.
1376 }
Chris Lattner193de902006-12-06 05:27:40 +00001377 if (TheFunction && !FunctionProcessed)
Misha Brukmanb1c93172005-04-21 23:48:37 +00001378 processFunction();
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001379}
1380
1381// Iterate through all the global variables, functions, and global
Misha Brukmanb1c93172005-04-21 23:48:37 +00001382// variable initializers and create slots for them.
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001383void SlotMachine::processModule() {
1384 SC_DEBUG("begin processModule!\n");
1385
Chris Lattner0dda8612006-12-06 06:15:43 +00001386 // Add all of the unnamed global variables to the value table.
Chris Lattner54932b02006-12-06 04:41:52 +00001387 for (Module::const_global_iterator I = TheModule->global_begin(),
1388 E = TheModule->global_end(); I != E; ++I)
Chris Lattner0dda8612006-12-06 06:15:43 +00001389 if (!I->hasName())
1390 getOrCreateSlot(I);
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001391
Chris Lattner0dda8612006-12-06 06:15:43 +00001392 // Add all the unnamed functions to the table.
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001393 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
1394 I != E; ++I)
Chris Lattner0dda8612006-12-06 06:15:43 +00001395 if (!I->hasName())
1396 getOrCreateSlot(I);
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001397
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001398 SC_DEBUG("end processModule!\n");
1399}
1400
1401
Reid Spencer56010e42004-05-26 21:56:09 +00001402// Process the arguments, basic blocks, and instructions of a function.
1403void SlotMachine::processFunction() {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001404 SC_DEBUG("begin processFunction!\n");
1405
Chris Lattner0dda8612006-12-06 06:15:43 +00001406 // Add all the function arguments with no names.
Misha Brukmanb1c93172005-04-21 23:48:37 +00001407 for(Function::const_arg_iterator AI = TheFunction->arg_begin(),
Chris Lattner531f9e92005-03-15 04:54:21 +00001408 AE = TheFunction->arg_end(); AI != AE; ++AI)
Chris Lattner0dda8612006-12-06 06:15:43 +00001409 if (!AI->hasName())
1410 getOrCreateSlot(AI);
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001411
1412 SC_DEBUG("Inserting Instructions:\n");
1413
Chris Lattner0dda8612006-12-06 06:15:43 +00001414 // Add all of the basic blocks and instructions with no names.
Misha Brukmanb1c93172005-04-21 23:48:37 +00001415 for (Function::const_iterator BB = TheFunction->begin(),
Reid Spencer56010e42004-05-26 21:56:09 +00001416 E = TheFunction->end(); BB != E; ++BB) {
Chris Lattner0dda8612006-12-06 06:15:43 +00001417 if (!BB->hasName())
1418 getOrCreateSlot(BB);
1419 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
1420 if (I->getType() != Type::VoidTy && !I->hasName())
1421 getOrCreateSlot(I);
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001422 }
1423
Reid Spencerb0ac8c42004-08-16 07:46:33 +00001424 FunctionProcessed = true;
1425
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001426 SC_DEBUG("end processFunction!\n");
1427}
1428
Chris Lattner193de902006-12-06 05:27:40 +00001429/// Clean up after incorporating a function. This is the only way to get out of
1430/// the function incorporation state that affects the
1431/// getSlot/getOrCreateSlot lock. Function incorporation state is indicated
1432/// by TheFunction != 0.
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001433void SlotMachine::purgeFunction() {
1434 SC_DEBUG("begin purgeFunction!\n");
1435 fMap.clear(); // Simply discard the function level map
Reid Spencer56010e42004-05-26 21:56:09 +00001436 TheFunction = 0;
Reid Spencerb0ac8c42004-08-16 07:46:33 +00001437 FunctionProcessed = false;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001438 SC_DEBUG("end purgeFunction!\n");
1439}
1440
1441/// Get the slot number for a value. This function will assert if you
Chris Lattner193de902006-12-06 05:27:40 +00001442/// ask for a Value that hasn't previously been inserted with getOrCreateSlot.
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001443/// Types are forbidden because Type does not inherit from Value (any more).
Chris Lattner757ee0b2004-06-09 19:41:19 +00001444int SlotMachine::getSlot(const Value *V) {
Chris Lattner23e829a2006-12-06 05:12:21 +00001445 assert(V && "Can't get slot for null Value");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001446 assert(!isa<Constant>(V) || isa<GlobalValue>(V) &&
1447 "Can't insert a non-GlobalValue Constant into SlotMachine");
Reid Spencer56010e42004-05-26 21:56:09 +00001448
1449 // Check for uninitialized state and do lazy initialization
1450 this->initialize();
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001451
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001452 // Get the type of the value
1453 const Type* VTy = V->getType();
1454
1455 // Find the type plane in the module map
1456 TypedPlanes::const_iterator MI = mMap.find(VTy);
1457
Chris Lattner23e829a2006-12-06 05:12:21 +00001458 if (TheFunction) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001459 // Lookup the type in the function map too
1460 TypedPlanes::const_iterator FI = fMap.find(VTy);
1461 // If there is a corresponding type plane in the function map
Chris Lattner23e829a2006-12-06 05:12:21 +00001462 if (FI != fMap.end()) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001463 // Lookup the Value in the function map
1464 ValueMap::const_iterator FVI = FI->second.map.find(V);
1465 // If the value doesn't exist in the function map
Chris Lattner23e829a2006-12-06 05:12:21 +00001466 if (FVI == FI->second.map.end()) {
Chris Lattner68a038e2004-06-09 22:22:10 +00001467 // Look up the value in the module map.
1468 if (MI == mMap.end()) return -1;
Reid Spencer56010e42004-05-26 21:56:09 +00001469 ValueMap::const_iterator MVI = MI->second.map.find(V);
1470 // If we didn't find it, it wasn't inserted
Chris Lattner757ee0b2004-06-09 19:41:19 +00001471 if (MVI == MI->second.map.end()) return -1;
Chris Lattner23e829a2006-12-06 05:12:21 +00001472 assert(MVI != MI->second.map.end() && "Value not found");
Reid Spencer56010e42004-05-26 21:56:09 +00001473 // We found it only at the module level
Misha Brukmanb1c93172005-04-21 23:48:37 +00001474 return MVI->second;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001475
1476 // else the value exists in the function map
1477 } else {
Reid Spencer56010e42004-05-26 21:56:09 +00001478 // Return the slot number as the module's contribution to
1479 // the type plane plus the index in the function's contribution
1480 // to the type plane.
Chris Lattnerb1f04782004-06-15 21:07:32 +00001481 if (MI != mMap.end())
1482 return MI->second.next_slot + FVI->second;
1483 else
1484 return FVI->second;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001485 }
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001486 }
1487 }
1488
Reid Spencer8beac692004-06-09 15:26:53 +00001489 // N.B. Can get here only if either !TheFunction or the function doesn't
1490 // have a corresponding type plane for the Value
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001491
1492 // Make sure the type plane exists
Chris Lattner757ee0b2004-06-09 19:41:19 +00001493 if (MI == mMap.end()) return -1;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001494 // Lookup the value in the module's map
1495 ValueMap::const_iterator MVI = MI->second.map.find(V);
1496 // Make sure we found it.
Chris Lattner757ee0b2004-06-09 19:41:19 +00001497 if (MVI == MI->second.map.end()) return -1;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001498 // Return it.
1499 return MVI->second;
1500}
1501
Reid Spencer58d30f22004-07-04 11:50:43 +00001502
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001503// Create a new slot, or return the existing slot if it is already
1504// inserted. Note that the logic here parallels getSlot but instead
1505// of asserting when the Value* isn't found, it inserts the value.
Chris Lattner193de902006-12-06 05:27:40 +00001506unsigned SlotMachine::getOrCreateSlot(const Value *V) {
Chris Lattner0dda8612006-12-06 06:15:43 +00001507 const Type* VTy = V->getType();
1508 assert(VTy != Type::VoidTy && !V->hasName() && "Doesn't need a slot!");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001509 assert(!isa<Constant>(V) || isa<GlobalValue>(V) &&
1510 "Can't insert a non-GlobalValue Constant into SlotMachine");
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001511
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001512 // Look up the type plane for the Value's type from the module map
1513 TypedPlanes::const_iterator MI = mMap.find(VTy);
1514
Chris Lattner23e829a2006-12-06 05:12:21 +00001515 if (TheFunction) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001516 // Get the type plane for the Value's type from the function map
1517 TypedPlanes::const_iterator FI = fMap.find(VTy);
1518 // If there is a corresponding type plane in the function map
Chris Lattner23e829a2006-12-06 05:12:21 +00001519 if (FI != fMap.end()) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001520 // Lookup the Value in the function map
1521 ValueMap::const_iterator FVI = FI->second.map.find(V);
1522 // If the value doesn't exist in the function map
Chris Lattner23e829a2006-12-06 05:12:21 +00001523 if (FVI == FI->second.map.end()) {
Reid Spencer56010e42004-05-26 21:56:09 +00001524 // If there is no corresponding type plane in the module map
Chris Lattner23e829a2006-12-06 05:12:21 +00001525 if (MI == mMap.end())
Reid Spencer56010e42004-05-26 21:56:09 +00001526 return insertValue(V);
1527 // Look up the value in the module map
1528 ValueMap::const_iterator MVI = MI->second.map.find(V);
1529 // If we didn't find it, it wasn't inserted
Chris Lattner23e829a2006-12-06 05:12:21 +00001530 if (MVI == MI->second.map.end())
Reid Spencer56010e42004-05-26 21:56:09 +00001531 return insertValue(V);
1532 else
1533 // We found it only at the module level
1534 return MVI->second;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001535
1536 // else the value exists in the function map
1537 } else {
Chris Lattner23e829a2006-12-06 05:12:21 +00001538 if (MI == mMap.end())
Reid Spencer56010e42004-05-26 21:56:09 +00001539 return FVI->second;
1540 else
1541 // Return the slot number as the module's contribution to
1542 // the type plane plus the index in the function's contribution
1543 // to the type plane.
1544 return MI->second.next_slot + FVI->second;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001545 }
1546
1547 // else there is not a corresponding type plane in the function map
1548 } else {
1549 // If the type plane doesn't exists at the module level
Chris Lattner23e829a2006-12-06 05:12:21 +00001550 if (MI == mMap.end()) {
Reid Spencer56010e42004-05-26 21:56:09 +00001551 return insertValue(V);
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001552 // else type plane exists at the module level, examine it
1553 } else {
Reid Spencer56010e42004-05-26 21:56:09 +00001554 // Look up the value in the module's map
1555 ValueMap::const_iterator MVI = MI->second.map.find(V);
1556 // If we didn't find it there either
Chris Lattner23e829a2006-12-06 05:12:21 +00001557 if (MVI == MI->second.map.end())
Reid Spencer56010e42004-05-26 21:56:09 +00001558 // Return the slot number as the module's contribution to
1559 // the type plane plus the index of the function map insertion.
1560 return MI->second.next_slot + insertValue(V);
1561 else
1562 return MVI->second;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001563 }
1564 }
1565 }
1566
Chris Lattner0dda8612006-12-06 06:15:43 +00001567 // N.B. Can only get here if TheFunction == 0
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001568
1569 // If the module map's type plane is not for the Value's type
Chris Lattner23e829a2006-12-06 05:12:21 +00001570 if (MI != mMap.end()) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001571 // Lookup the value in the module's map
1572 ValueMap::const_iterator MVI = MI->second.map.find(V);
Chris Lattner23e829a2006-12-06 05:12:21 +00001573 if (MVI != MI->second.map.end())
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001574 return MVI->second;
1575 }
1576
1577 return insertValue(V);
1578}
1579
1580
1581// Low level insert function. Minimal checking is done. This
Chris Lattner193de902006-12-06 05:27:40 +00001582// function is just for the convenience of getOrCreateSlot (above).
Chris Lattner23e829a2006-12-06 05:12:21 +00001583unsigned SlotMachine::insertValue(const Value *V) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001584 assert(V && "Can't insert a null Value into SlotMachine!");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001585 assert(!isa<Constant>(V) || isa<GlobalValue>(V) &&
Chris Lattner7e4e7662006-12-06 05:42:32 +00001586 "Can't insert a non-GlobalValue Constant into SlotMachine");
1587 assert(V->getType() != Type::VoidTy && !V->hasName());
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001588
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001589 const Type *VTy = V->getType();
1590 unsigned DestSlot = 0;
1591
Chris Lattner23e829a2006-12-06 05:12:21 +00001592 if (TheFunction) {
1593 TypedPlanes::iterator I = fMap.find(VTy);
1594 if (I == fMap.end())
Reid Spencer58d30f22004-07-04 11:50:43 +00001595 I = fMap.insert(std::make_pair(VTy,ValuePlane())).first;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001596 DestSlot = I->second.map[V] = I->second.next_slot++;
1597 } else {
Chris Lattner23e829a2006-12-06 05:12:21 +00001598 TypedPlanes::iterator I = mMap.find(VTy);
1599 if (I == mMap.end())
Reid Spencer58d30f22004-07-04 11:50:43 +00001600 I = mMap.insert(std::make_pair(VTy,ValuePlane())).first;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001601 DestSlot = I->second.map[V] = I->second.next_slot++;
1602 }
1603
Misha Brukmanb1c93172005-04-21 23:48:37 +00001604 SC_DEBUG(" Inserting value [" << VTy << "] = " << V << " slot=" <<
Reid Spencer56010e42004-05-26 21:56:09 +00001605 DestSlot << " [");
Chris Lattnerf8090cb2006-12-06 05:55:41 +00001606 // G = Global, F = Function, o = other
1607 SC_DEBUG((isa<GlobalVariable>(V) ? 'G' : (isa<Function>(V) ? 'F' : 'o')));
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001608 SC_DEBUG("]\n");
1609 return DestSlot;
1610}