blob: 6f7c4953d270c90c65129c26966751e49cda0d87 [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 Spencer32af9e82007-01-06 07:24:44 +000028#include "llvm/TypeSymbolTable.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000029#include "llvm/ADT/StringExtras.h"
30#include "llvm/ADT/STLExtras.h"
Bill Wendlingdfc91892006-11-28 02:09:03 +000031#include "llvm/Support/CFG.h"
Jim Laskeyb74c6662005-08-17 19:34:49 +000032#include "llvm/Support/MathExtras.h"
Bill Wendlingdfc91892006-11-28 02:09:03 +000033#include "llvm/Support/Streams.h"
Chris Lattnerfee714f2001-09-07 16:36:04 +000034#include <algorithm>
Chris Lattner189d19f2003-11-21 20:23:48 +000035using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000036
Chris Lattner60a7dd12004-07-15 02:51:31 +000037namespace llvm {
Reid Spencer16f2f7f2004-05-26 07:18:52 +000038
Reid Spencer294715b2005-05-15 16:13:11 +000039// Make virtual table appear in this compilation unit.
40AssemblyAnnotationWriter::~AssemblyAnnotationWriter() {}
41
Reid Spencer16f2f7f2004-05-26 07:18:52 +000042/// This class provides computation of slot numbers for LLVM Assembly writing.
43/// @brief LLVM Assembly Writing Slot Computation.
44class SlotMachine {
45
46/// @name Types
47/// @{
48public:
49
50 /// @brief A mapping of Values to slot numbers
51 typedef std::map<const Value*, unsigned> ValueMap;
52
53 /// @brief A plane with next slot number and ValueMap
Misha Brukmanb1c93172005-04-21 23:48:37 +000054 struct ValuePlane {
Reid Spencer16f2f7f2004-05-26 07:18:52 +000055 unsigned next_slot; ///< The next slot number to use
56 ValueMap map; ///< The map of Value* -> unsigned
Reid Spencer58d30f22004-07-04 11:50:43 +000057 ValuePlane() { next_slot = 0; } ///< Make sure we start at 0
58 };
59
Reid Spencer16f2f7f2004-05-26 07:18:52 +000060 /// @brief The map of planes by Type
Reid Spencer58d30f22004-07-04 11:50:43 +000061 typedef std::map<const Type*, ValuePlane> TypedPlanes;
Reid Spencer16f2f7f2004-05-26 07:18:52 +000062
63/// @}
64/// @name Constructors
65/// @{
66public:
67 /// @brief Construct from a module
Chris Lattner23e829a2006-12-06 05:12:21 +000068 SlotMachine(const Module *M);
Reid Spencer16f2f7f2004-05-26 07:18:52 +000069
70 /// @brief Construct from a function, starting out in incorp state.
Chris Lattner23e829a2006-12-06 05:12:21 +000071 SlotMachine(const Function *F);
Reid Spencer16f2f7f2004-05-26 07:18:52 +000072
73/// @}
74/// @name Accessors
75/// @{
76public:
77 /// Return the slot number of the specified value in it's type
Chris Lattner5e043322007-01-11 03:54:27 +000078 /// plane. If something is not in the SlotMachine, return -1.
79 int getLocalSlot(const Value *V);
80 int getGlobalSlot(const GlobalValue *V);
Reid Spencer8beac692004-06-09 15:26:53 +000081
Reid Spencer16f2f7f2004-05-26 07:18:52 +000082/// @}
83/// @name Mutators
84/// @{
85public:
Misha Brukmanb1c93172005-04-21 23:48:37 +000086 /// If you'd like to deal with a function instead of just a module, use
Reid Spencer16f2f7f2004-05-26 07:18:52 +000087 /// this method to get its data into the SlotMachine.
Misha Brukmanb1c93172005-04-21 23:48:37 +000088 void incorporateFunction(const Function *F) {
89 TheFunction = F;
Reid Spencerb0ac8c42004-08-16 07:46:33 +000090 FunctionProcessed = false;
91 }
Reid Spencer16f2f7f2004-05-26 07:18:52 +000092
Misha Brukmanb1c93172005-04-21 23:48:37 +000093 /// After calling incorporateFunction, use this method to remove the
94 /// most recently incorporated function from the SlotMachine. This
Reid Spencer16f2f7f2004-05-26 07:18:52 +000095 /// will reset the state of the machine back to just the module contents.
96 void purgeFunction();
97
98/// @}
99/// @name Implementation Details
100/// @{
101private:
Reid Spencer56010e42004-05-26 21:56:09 +0000102 /// This function does the actual initialization.
103 inline void initialize();
104
Chris Lattnerea862a32007-01-09 07:55:49 +0000105 /// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
106 void CreateModuleSlot(const GlobalValue *V);
107
108 /// CreateFunctionSlot - Insert the specified Value* into the slot table.
109 void CreateFunctionSlot(const Value *V);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000110
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000111 /// Add all of the module level global variables (and their initializers)
112 /// and function declarations, but not the contents of those functions.
113 void processModule();
114
Reid Spencer56010e42004-05-26 21:56:09 +0000115 /// Add all of the functions arguments, basic blocks, and instructions
116 void processFunction();
117
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000118 SlotMachine(const SlotMachine &); // DO NOT IMPLEMENT
119 void operator=(const SlotMachine &); // DO NOT IMPLEMENT
120
121/// @}
122/// @name Data
123/// @{
124public:
125
126 /// @brief The module for which we are holding slot numbers
Reid Spencer56010e42004-05-26 21:56:09 +0000127 const Module* TheModule;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000128
Reid Spencer56010e42004-05-26 21:56:09 +0000129 /// @brief The function for which we are holding slot numbers
130 const Function* TheFunction;
Reid Spencerb0ac8c42004-08-16 07:46:33 +0000131 bool FunctionProcessed;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000132
133 /// @brief The TypePlanes map for the module level data
134 TypedPlanes mMap;
135
136 /// @brief The TypePlanes map for the function level data
137 TypedPlanes fMap;
138
139/// @}
140
141};
142
Chris Lattner60a7dd12004-07-15 02:51:31 +0000143} // end namespace llvm
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000144
Chris Lattnerc2d3d312006-08-27 22:42:52 +0000145static RegisterPass<PrintModulePass>
Chris Lattnere7134c52006-08-21 17:20:01 +0000146X("printm", "Print module to stderr");
Chris Lattnerc2d3d312006-08-27 22:42:52 +0000147static RegisterPass<PrintFunctionPass>
Chris Lattnere7134c52006-08-21 17:20:01 +0000148Y("print","Print function to stderr");
Chris Lattner7f8845a2002-07-23 18:07:49 +0000149
Misha Brukmanb1c93172005-04-21 23:48:37 +0000150static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
Chris Lattnera9f0a112006-12-06 05:50:41 +0000151 std::map<const Type *, std::string> &TypeTable,
Reid Spencer58d30f22004-07-04 11:50:43 +0000152 SlotMachine *Machine);
153
Chris Lattnerb86620e2001-10-29 16:37:48 +0000154static const Module *getModuleFromVal(const Value *V) {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000155 if (const Argument *MA = dyn_cast<Argument>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +0000156 return MA->getParent() ? MA->getParent()->getParent() : 0;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000157 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +0000158 return BB->getParent() ? BB->getParent()->getParent() : 0;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000159 else if (const Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner57698e22002-03-26 18:01:55 +0000160 const Function *M = I->getParent() ? I->getParent()->getParent() : 0;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000161 return M ? M->getParent() : 0;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000162 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +0000163 return GV->getParent();
Chris Lattnerb86620e2001-10-29 16:37:48 +0000164 return 0;
165}
166
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000167static SlotMachine *createSlotMachine(const Value *V) {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000168 if (const Argument *FA = dyn_cast<Argument>(V)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000169 return new SlotMachine(FA->getParent());
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000170 } else if (const Instruction *I = dyn_cast<Instruction>(V)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000171 return new SlotMachine(I->getParent()->getParent());
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000172 } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000173 return new SlotMachine(BB->getParent());
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000174 } else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)){
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000175 return new SlotMachine(GV->getParent());
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000176 } else if (const Function *Func = dyn_cast<Function>(V)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000177 return new SlotMachine(Func);
Chris Lattner7bfee412001-10-29 16:05:51 +0000178 }
179 return 0;
180}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000181
Chris Lattner1c343042003-08-22 05:40:38 +0000182// getLLVMName - Turn the specified string into an 'LLVM name', which is either
183// prefixed with % (if the string only contains simple characters) or is
184// surrounded with ""'s (if it has special chars in it).
Alkis Evlogimenos991d6ad2004-12-10 05:41:10 +0000185static std::string getLLVMName(const std::string &Name,
186 bool prefixName = true) {
Chris Lattner1c343042003-08-22 05:40:38 +0000187 assert(!Name.empty() && "Cannot get empty name!");
188
189 // First character cannot start with a number...
190 if (Name[0] >= '0' && Name[0] <= '9')
191 return "\"" + Name + "\"";
192
193 // Scan to see if we have any characters that are not on the "white list"
194 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
195 char C = Name[i];
196 assert(C != '"' && "Illegal character in LLVM value name!");
197 if ((C < 'a' || C > 'z') && (C < 'A' || C > 'Z') && (C < '0' || C > '9') &&
198 C != '-' && C != '.' && C != '_')
199 return "\"" + Name + "\"";
200 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000201
Chris Lattner1c343042003-08-22 05:40:38 +0000202 // If we get here, then the identifier is legal to use as a "VarID".
Alkis Evlogimenos991d6ad2004-12-10 05:41:10 +0000203 if (prefixName)
204 return "%"+Name;
205 else
206 return Name;
Chris Lattner1c343042003-08-22 05:40:38 +0000207}
208
Chris Lattnerb86620e2001-10-29 16:37:48 +0000209
Misha Brukmanc566ca362004-03-02 00:22:19 +0000210/// fillTypeNameTable - If the module has a symbol table, take all global types
211/// and stuff their names into the TypeNames map.
212///
Chris Lattnerb86620e2001-10-29 16:37:48 +0000213static void fillTypeNameTable(const Module *M,
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000214 std::map<const Type *, std::string> &TypeNames) {
Chris Lattner98cf1f52002-11-20 18:36:02 +0000215 if (!M) return;
Reid Spencer32af9e82007-01-06 07:24:44 +0000216 const TypeSymbolTable &ST = M->getTypeSymbolTable();
217 TypeSymbolTable::const_iterator TI = ST.begin();
218 for (; TI != ST.end(); ++TI) {
Reid Spencere7e96712004-05-25 08:53:40 +0000219 // As a heuristic, don't insert pointer to primitive types, because
220 // they are used too often to have a single useful name.
221 //
222 const Type *Ty = cast<Type>(TI->second);
223 if (!isa<PointerType>(Ty) ||
Reid Spencer56010e42004-05-26 21:56:09 +0000224 !cast<PointerType>(Ty)->getElementType()->isPrimitiveType() ||
Chris Lattner03c49532007-01-15 02:27:26 +0000225 !cast<PointerType>(Ty)->getElementType()->isInteger() ||
Reid Spencer56010e42004-05-26 21:56:09 +0000226 isa<OpaqueType>(cast<PointerType>(Ty)->getElementType()))
Reid Spencere7e96712004-05-25 08:53:40 +0000227 TypeNames.insert(std::make_pair(Ty, getLLVMName(TI->first)));
Chris Lattnerb86620e2001-10-29 16:37:48 +0000228 }
229}
230
231
232
Misha Brukmanb1c93172005-04-21 23:48:37 +0000233static void calcTypeName(const Type *Ty,
John Criswellcd116ba2004-06-01 14:54:08 +0000234 std::vector<const Type *> &TypeStack,
235 std::map<const Type *, std::string> &TypeNames,
236 std::string & Result){
Chris Lattner03c49532007-01-15 02:27:26 +0000237 if (Ty->isInteger() || (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty))) {
John Criswellcd116ba2004-06-01 14:54:08 +0000238 Result += Ty->getDescription(); // Base case
239 return;
240 }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000241
242 // Check to see if the type is named.
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000243 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
John Criswellcd116ba2004-06-01 14:54:08 +0000244 if (I != TypeNames.end()) {
245 Result += I->second;
246 return;
247 }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000248
John Criswellcd116ba2004-06-01 14:54:08 +0000249 if (isa<OpaqueType>(Ty)) {
250 Result += "opaque";
251 return;
252 }
Chris Lattnerf14ead92003-10-30 00:22:33 +0000253
Chris Lattnerb86620e2001-10-29 16:37:48 +0000254 // Check to see if the Type is already on the stack...
255 unsigned Slot = 0, CurSize = TypeStack.size();
256 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
257
Misha Brukmanb1c93172005-04-21 23:48:37 +0000258 // This is another base case for the recursion. In this case, we know
Chris Lattnerb86620e2001-10-29 16:37:48 +0000259 // that we have looped back to a type that we have previously visited.
260 // Generate the appropriate upreference to handle this.
John Criswellcd116ba2004-06-01 14:54:08 +0000261 if (Slot < CurSize) {
262 Result += "\\" + utostr(CurSize-Slot); // Here's the upreference
263 return;
264 }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000265
266 TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
Misha Brukmanb1c93172005-04-21 23:48:37 +0000267
Chris Lattner6b727592004-06-17 18:19:28 +0000268 switch (Ty->getTypeID()) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000269 case Type::IntegerTyID: {
270 unsigned BitWidth = cast<IntegerType>(Ty)->getBitWidth();
Reid Spencerc8721592007-01-12 07:25:20 +0000271 Result += "i" + utostr(BitWidth);
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000272 break;
273 }
Chris Lattner91db5822002-03-29 03:44:36 +0000274 case Type::FunctionTyID: {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000275 const FunctionType *FTy = cast<FunctionType>(Ty);
John Criswellcd116ba2004-06-01 14:54:08 +0000276 calcTypeName(FTy->getReturnType(), TypeStack, TypeNames, Result);
277 Result += " (";
Reid Spencer8c4914c2006-12-31 05:24:50 +0000278 unsigned Idx = 1;
Chris Lattnerfa829be2004-02-09 04:14:01 +0000279 for (FunctionType::param_iterator I = FTy->param_begin(),
280 E = FTy->param_end(); I != E; ++I) {
281 if (I != FTy->param_begin())
Chris Lattnerb86620e2001-10-29 16:37:48 +0000282 Result += ", ";
John Criswellcd116ba2004-06-01 14:54:08 +0000283 calcTypeName(*I, TypeStack, TypeNames, Result);
Reid Spencer8c4914c2006-12-31 05:24:50 +0000284 if (FTy->getParamAttrs(Idx)) {
285 Result += + " ";
286 Result += FunctionType::getParamAttrsText(FTy->getParamAttrs(Idx));
287 }
288 Idx++;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000289 }
Chris Lattnerd816b532002-04-13 20:53:41 +0000290 if (FTy->isVarArg()) {
Chris Lattnerfa829be2004-02-09 04:14:01 +0000291 if (FTy->getNumParams()) Result += ", ";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000292 Result += "...";
293 }
294 Result += ")";
Reid Spencer136a91c2007-01-05 17:06:19 +0000295 if (FTy->getParamAttrs(0)) {
296 Result += " ";
297 Result += FunctionType::getParamAttrsText(FTy->getParamAttrs(0));
298 }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000299 break;
300 }
301 case Type::StructTyID: {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000302 const StructType *STy = cast<StructType>(Ty);
Andrew Lenharthdcb3c972006-12-08 18:06:16 +0000303 if (STy->isPacked())
304 Result += '<';
John Criswellcd116ba2004-06-01 14:54:08 +0000305 Result += "{ ";
Chris Lattnerac6db752004-02-09 04:37:31 +0000306 for (StructType::element_iterator I = STy->element_begin(),
307 E = STy->element_end(); I != E; ++I) {
308 if (I != STy->element_begin())
Chris Lattnerb86620e2001-10-29 16:37:48 +0000309 Result += ", ";
John Criswellcd116ba2004-06-01 14:54:08 +0000310 calcTypeName(*I, TypeStack, TypeNames, Result);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000311 }
312 Result += " }";
Andrew Lenharthdcb3c972006-12-08 18:06:16 +0000313 if (STy->isPacked())
314 Result += '>';
Chris Lattnerb86620e2001-10-29 16:37:48 +0000315 break;
316 }
317 case Type::PointerTyID:
Misha Brukmanb1c93172005-04-21 23:48:37 +0000318 calcTypeName(cast<PointerType>(Ty)->getElementType(),
John Criswellcd116ba2004-06-01 14:54:08 +0000319 TypeStack, TypeNames, Result);
320 Result += "*";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000321 break;
322 case Type::ArrayTyID: {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000323 const ArrayType *ATy = cast<ArrayType>(Ty);
John Criswellcd116ba2004-06-01 14:54:08 +0000324 Result += "[" + utostr(ATy->getNumElements()) + " x ";
325 calcTypeName(ATy->getElementType(), TypeStack, TypeNames, Result);
326 Result += "]";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000327 break;
328 }
Brian Gaeke02209042004-08-20 06:00:58 +0000329 case Type::PackedTyID: {
330 const PackedType *PTy = cast<PackedType>(Ty);
331 Result += "<" + utostr(PTy->getNumElements()) + " x ";
332 calcTypeName(PTy->getElementType(), TypeStack, TypeNames, Result);
333 Result += ">";
334 break;
335 }
Chris Lattner15285ab2003-05-14 17:50:47 +0000336 case Type::OpaqueTyID:
John Criswellcd116ba2004-06-01 14:54:08 +0000337 Result += "opaque";
Chris Lattner15285ab2003-05-14 17:50:47 +0000338 break;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000339 default:
John Criswellcd116ba2004-06-01 14:54:08 +0000340 Result += "<unrecognized-type>";
Chris Lattnerfc9f1c92006-12-06 06:40:49 +0000341 break;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000342 }
343
344 TypeStack.pop_back(); // Remove self from stack...
Chris Lattnerb86620e2001-10-29 16:37:48 +0000345}
346
347
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000348/// printTypeInt - The internal guts of printing out a type that has a
349/// potentially named portion.
350///
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000351static std::ostream &printTypeInt(std::ostream &Out, const Type *Ty,
352 std::map<const Type *, std::string> &TypeNames) {
Chris Lattnerb86620e2001-10-29 16:37:48 +0000353 // Primitive types always print out their description, regardless of whether
354 // they have been named or not.
355 //
Chris Lattner03c49532007-01-15 02:27:26 +0000356 if (Ty->isInteger() || (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty)))
Chris Lattner92d60532003-10-30 00:12:51 +0000357 return Out << Ty->getDescription();
Chris Lattnerb86620e2001-10-29 16:37:48 +0000358
359 // Check to see if the type is named.
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000360 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000361 if (I != TypeNames.end()) return Out << I->second;
362
363 // Otherwise we have a type that has not been named but is a derived type.
364 // Carefully recurse the type hierarchy to print out any contained symbolic
365 // names.
366 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000367 std::vector<const Type *> TypeStack;
John Criswellcd116ba2004-06-01 14:54:08 +0000368 std::string TypeName;
369 calcTypeName(Ty, TypeStack, TypeNames, TypeName);
Chris Lattner7f74a562002-01-20 22:54:45 +0000370 TypeNames.insert(std::make_pair(Ty, TypeName));//Cache type name for later use
John Criswellcd116ba2004-06-01 14:54:08 +0000371 return (Out << TypeName);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000372}
373
Chris Lattner34b95182001-10-31 04:33:19 +0000374
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000375/// WriteTypeSymbolic - This attempts to write the specified type as a symbolic
376/// type, iff there is an entry in the modules symbol table for the specified
377/// type or one of it's component types. This is slower than a simple x << Type
378///
Chris Lattner189d19f2003-11-21 20:23:48 +0000379std::ostream &llvm::WriteTypeSymbolic(std::ostream &Out, const Type *Ty,
380 const Module *M) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000381 Out << ' ';
Chris Lattnerb86620e2001-10-29 16:37:48 +0000382
Chris Lattnerfc9f1c92006-12-06 06:40:49 +0000383 // If they want us to print out a type, but there is no context, we can't
384 // print it symbolically.
385 if (!M)
Chris Lattner72f866e2001-10-29 16:40:32 +0000386 return Out << Ty->getDescription();
Chris Lattnerfc9f1c92006-12-06 06:40:49 +0000387
388 std::map<const Type *, std::string> TypeNames;
389 fillTypeNameTable(M, TypeNames);
390 return printTypeInt(Out, Ty, TypeNames);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000391}
392
Chris Lattner6ed87bd2006-01-23 23:03:36 +0000393// PrintEscapedString - Print each character of the specified string, escaping
394// it if it is not printable or if it is an escape char.
395static void PrintEscapedString(const std::string &Str, std::ostream &Out) {
396 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
397 unsigned char C = Str[i];
398 if (isprint(C) && C != '"' && C != '\\') {
399 Out << C;
400 } else {
401 Out << '\\'
402 << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'))
403 << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
404 }
405 }
406}
407
Chris Lattnerfc9f1c92006-12-06 06:40:49 +0000408static const char *getPredicateText(unsigned predicate) {
Reid Spencer812a1be2006-12-04 05:19:18 +0000409 const char * pred = "unknown";
410 switch (predicate) {
411 case FCmpInst::FCMP_FALSE: pred = "false"; break;
412 case FCmpInst::FCMP_OEQ: pred = "oeq"; break;
413 case FCmpInst::FCMP_OGT: pred = "ogt"; break;
414 case FCmpInst::FCMP_OGE: pred = "oge"; break;
415 case FCmpInst::FCMP_OLT: pred = "olt"; break;
416 case FCmpInst::FCMP_OLE: pred = "ole"; break;
417 case FCmpInst::FCMP_ONE: pred = "one"; break;
418 case FCmpInst::FCMP_ORD: pred = "ord"; break;
419 case FCmpInst::FCMP_UNO: pred = "uno"; break;
420 case FCmpInst::FCMP_UEQ: pred = "ueq"; break;
421 case FCmpInst::FCMP_UGT: pred = "ugt"; break;
422 case FCmpInst::FCMP_UGE: pred = "uge"; break;
423 case FCmpInst::FCMP_ULT: pred = "ult"; break;
424 case FCmpInst::FCMP_ULE: pred = "ule"; break;
425 case FCmpInst::FCMP_UNE: pred = "une"; break;
426 case FCmpInst::FCMP_TRUE: pred = "true"; break;
427 case ICmpInst::ICMP_EQ: pred = "eq"; break;
428 case ICmpInst::ICMP_NE: pred = "ne"; break;
429 case ICmpInst::ICMP_SGT: pred = "sgt"; break;
430 case ICmpInst::ICMP_SGE: pred = "sge"; break;
431 case ICmpInst::ICMP_SLT: pred = "slt"; break;
432 case ICmpInst::ICMP_SLE: pred = "sle"; break;
433 case ICmpInst::ICMP_UGT: pred = "ugt"; break;
434 case ICmpInst::ICMP_UGE: pred = "uge"; break;
435 case ICmpInst::ICMP_ULT: pred = "ult"; break;
436 case ICmpInst::ICMP_ULE: pred = "ule"; break;
437 }
438 return pred;
439}
440
Misha Brukmanb1c93172005-04-21 23:48:37 +0000441/// @brief Internal constant writer.
442static void WriteConstantInt(std::ostream &Out, const Constant *CV,
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000443 std::map<const Type *, std::string> &TypeTable,
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000444 SlotMachine *Machine) {
Jim Laskey6be3d8e2006-02-27 10:33:53 +0000445 const int IndentSize = 4;
446 static std::string Indent = "\n";
Zhou Sheng75b871f2007-01-11 12:24:14 +0000447 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Reid Spencer542964f2007-01-11 18:21:29 +0000448 if (CI->getType() == Type::Int1Ty)
Reid Spencercddc9df2007-01-12 04:24:46 +0000449 Out << (CI->getZExtValue() ? "true" : "false");
450 else
451 Out << CI->getSExtValue();
Chris Lattner1e194682002-04-18 18:53:13 +0000452 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
453 // We would like to output the FP constant value in exponential notation,
454 // but we cannot do this if doing so will lose precision. Check here to
455 // make sure that we only output it in exponential format if we can parse
456 // the value back and get the same value.
457 //
458 std::string StrVal = ftostr(CFP->getValue());
459
460 // Check to make sure that the stringized number is not some string like
461 // "Inf" or NaN, that atof will accept, but the lexer will not. Check that
462 // the string matches the "[-+]?[0-9]" regex.
463 //
464 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
465 ((StrVal[0] == '-' || StrVal[0] == '+') &&
Brian Gaeke87b4f072003-06-17 23:55:35 +0000466 (StrVal[1] >= '0' && StrVal[1] <= '9')))
Chris Lattner1e194682002-04-18 18:53:13 +0000467 // Reparse stringized version!
468 if (atof(StrVal.c_str()) == CFP->getValue()) {
Chris Lattner472cc102005-01-04 01:56:57 +0000469 Out << StrVal;
470 return;
Chris Lattner1e194682002-04-18 18:53:13 +0000471 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000472
Chris Lattner1e194682002-04-18 18:53:13 +0000473 // Otherwise we could not reparse it to exactly the same value, so we must
474 // output the string in hexadecimal format!
Chris Lattner472cc102005-01-04 01:56:57 +0000475 assert(sizeof(double) == sizeof(uint64_t) &&
Chris Lattner1e194682002-04-18 18:53:13 +0000476 "assuming that double is 64 bits!");
Jim Laskeyb74c6662005-08-17 19:34:49 +0000477 Out << "0x" << utohexstr(DoubleToBits(CFP->getValue()));
Chris Lattner1e194682002-04-18 18:53:13 +0000478
Chris Lattner76b2ff42004-02-15 05:55:15 +0000479 } else if (isa<ConstantAggregateZero>(CV)) {
480 Out << "zeroinitializer";
Chris Lattner1e194682002-04-18 18:53:13 +0000481 } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
482 // As a special case, print the array as a string if it is an array of
483 // ubytes or an array of sbytes with positive values.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000484 //
Chris Lattner1e194682002-04-18 18:53:13 +0000485 const Type *ETy = CA->getType()->getElementType();
Chris Lattner6ed87bd2006-01-23 23:03:36 +0000486 if (CA->isString()) {
Chris Lattner1e194682002-04-18 18:53:13 +0000487 Out << "c\"";
Chris Lattner6ed87bd2006-01-23 23:03:36 +0000488 PrintEscapedString(CA->getAsString(), Out);
Chris Lattner1e194682002-04-18 18:53:13 +0000489 Out << "\"";
490
491 } else { // Cannot output in string format...
Misha Brukman21bbdb92004-06-04 21:11:51 +0000492 Out << '[';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000493 if (CA->getNumOperands()) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000494 Out << ' ';
Chris Lattner1e194682002-04-18 18:53:13 +0000495 printTypeInt(Out, ETy, TypeTable);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000496 WriteAsOperandInternal(Out, CA->getOperand(0),
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000497 TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000498 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
499 Out << ", ";
Chris Lattner1e194682002-04-18 18:53:13 +0000500 printTypeInt(Out, ETy, TypeTable);
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000501 WriteAsOperandInternal(Out, CA->getOperand(i), TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000502 }
503 }
504 Out << " ]";
505 }
506 } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Andrew Lenharth0d124b82007-01-08 18:21:30 +0000507 if (CS->getType()->isPacked())
508 Out << '<';
Misha Brukman21bbdb92004-06-04 21:11:51 +0000509 Out << '{';
Jim Laskey3bb78742006-02-25 12:27:03 +0000510 unsigned N = CS->getNumOperands();
511 if (N) {
Jim Laskey6be3d8e2006-02-27 10:33:53 +0000512 if (N > 2) {
513 Indent += std::string(IndentSize, ' ');
514 Out << Indent;
515 } else {
516 Out << ' ';
517 }
Chris Lattnerd84bb632002-04-16 21:36:08 +0000518 printTypeInt(Out, CS->getOperand(0)->getType(), TypeTable);
519
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000520 WriteAsOperandInternal(Out, CS->getOperand(0), TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000521
Jim Laskey3bb78742006-02-25 12:27:03 +0000522 for (unsigned i = 1; i < N; i++) {
Chris Lattnerd84bb632002-04-16 21:36:08 +0000523 Out << ", ";
Jim Laskey6be3d8e2006-02-27 10:33:53 +0000524 if (N > 2) Out << Indent;
Chris Lattnerd84bb632002-04-16 21:36:08 +0000525 printTypeInt(Out, CS->getOperand(i)->getType(), TypeTable);
526
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000527 WriteAsOperandInternal(Out, CS->getOperand(i), TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000528 }
Jim Laskey6be3d8e2006-02-27 10:33:53 +0000529 if (N > 2) Indent.resize(Indent.size() - IndentSize);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000530 }
Jim Laskey3bb78742006-02-25 12:27:03 +0000531
Chris Lattnerd84bb632002-04-16 21:36:08 +0000532 Out << " }";
Andrew Lenharth0d124b82007-01-08 18:21:30 +0000533 if (CS->getType()->isPacked())
534 Out << '>';
Brian Gaeke02209042004-08-20 06:00:58 +0000535 } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(CV)) {
536 const Type *ETy = CP->getType()->getElementType();
Misha Brukmanb1c93172005-04-21 23:48:37 +0000537 assert(CP->getNumOperands() > 0 &&
Brian Gaeke02209042004-08-20 06:00:58 +0000538 "Number of operands for a PackedConst must be > 0");
539 Out << '<';
540 Out << ' ';
541 printTypeInt(Out, ETy, TypeTable);
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000542 WriteAsOperandInternal(Out, CP->getOperand(0), TypeTable, Machine);
Brian Gaeke02209042004-08-20 06:00:58 +0000543 for (unsigned i = 1, e = CP->getNumOperands(); i != e; ++i) {
544 Out << ", ";
545 printTypeInt(Out, ETy, TypeTable);
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000546 WriteAsOperandInternal(Out, CP->getOperand(i), TypeTable, Machine);
Brian Gaeke02209042004-08-20 06:00:58 +0000547 }
548 Out << " >";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000549 } else if (isa<ConstantPointerNull>(CV)) {
550 Out << "null";
551
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000552 } else if (isa<UndefValue>(CV)) {
553 Out << "undef";
554
Chris Lattner3cd8c562002-07-30 18:54:25 +0000555 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Reid Spencer812a1be2006-12-04 05:19:18 +0000556 Out << CE->getOpcodeName();
557 if (CE->isCompare())
558 Out << " " << getPredicateText(CE->getPredicate());
559 Out << " (";
Misha Brukmanb1c93172005-04-21 23:48:37 +0000560
Vikram S. Adveb952b542002-07-14 23:14:45 +0000561 for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
562 printTypeInt(Out, (*OI)->getType(), TypeTable);
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000563 WriteAsOperandInternal(Out, *OI, TypeTable, Machine);
Vikram S. Adveb952b542002-07-14 23:14:45 +0000564 if (OI+1 != CE->op_end())
Chris Lattner3cd8c562002-07-30 18:54:25 +0000565 Out << ", ";
Vikram S. Adveb952b542002-07-14 23:14:45 +0000566 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000567
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000568 if (CE->isCast()) {
Chris Lattner83b396b2002-08-15 19:37:43 +0000569 Out << " to ";
570 printTypeInt(Out, CE->getType(), TypeTable);
571 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000572
Misha Brukman21bbdb92004-06-04 21:11:51 +0000573 Out << ')';
Chris Lattner83b396b2002-08-15 19:37:43 +0000574
Chris Lattnerd84bb632002-04-16 21:36:08 +0000575 } else {
Vikram S. Adveb952b542002-07-14 23:14:45 +0000576 Out << "<placeholder or erroneous Constant>";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000577 }
578}
579
580
Misha Brukmanc566ca362004-03-02 00:22:19 +0000581/// WriteAsOperand - Write the name of the specified value out to the specified
582/// ostream. This can be useful when you just want to print int %reg126, not
583/// the whole instruction that generated it.
584///
Misha Brukmanb1c93172005-04-21 23:48:37 +0000585static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000586 std::map<const Type*, std::string> &TypeTable,
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000587 SlotMachine *Machine) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000588 Out << ' ';
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000589 if (V->hasName())
Chris Lattner1c343042003-08-22 05:40:38 +0000590 Out << getLLVMName(V->getName());
Reid Spenceraccd7c72004-07-17 23:47:01 +0000591 else {
592 const Constant *CV = dyn_cast<Constant>(V);
Chris Lattnera2d810d2006-01-25 22:26:05 +0000593 if (CV && !isa<GlobalValue>(CV)) {
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000594 WriteConstantInt(Out, CV, TypeTable, Machine);
Chris Lattnera2d810d2006-01-25 22:26:05 +0000595 } else if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
596 Out << "asm ";
597 if (IA->hasSideEffects())
598 Out << "sideeffect ";
599 Out << '"';
600 PrintEscapedString(IA->getAsmString(), Out);
601 Out << "\", \"";
602 PrintEscapedString(IA->getConstraintString(), Out);
603 Out << '"';
604 } else {
Chris Lattnerd84bb632002-04-16 21:36:08 +0000605 int Slot;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000606 if (Machine) {
Chris Lattner5e043322007-01-11 03:54:27 +0000607 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
608 Slot = Machine->getGlobalSlot(GV);
609 else
610 Slot = Machine->getLocalSlot(V);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000611 } else {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000612 Machine = createSlotMachine(V);
Chris Lattner5e043322007-01-11 03:54:27 +0000613 if (Machine) {
614 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
615 Slot = Machine->getGlobalSlot(GV);
616 else
617 Slot = Machine->getLocalSlot(V);
618 } else {
Chris Lattner757ee0b2004-06-09 19:41:19 +0000619 Slot = -1;
Chris Lattner5e043322007-01-11 03:54:27 +0000620 }
Reid Spencer56010e42004-05-26 21:56:09 +0000621 delete Machine;
Chris Lattnerd84bb632002-04-16 21:36:08 +0000622 }
Chris Lattner757ee0b2004-06-09 19:41:19 +0000623 if (Slot != -1)
624 Out << '%' << Slot;
625 else
626 Out << "<badref>";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000627 }
628 }
629}
630
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000631/// WriteAsOperand - Write the name of the specified value out to the specified
632/// ostream. This can be useful when you just want to print int %reg126, not
633/// the whole instruction that generated it.
634///
Chris Lattner189d19f2003-11-21 20:23:48 +0000635std::ostream &llvm::WriteAsOperand(std::ostream &Out, const Value *V,
Chris Lattner0dda8612006-12-06 06:15:43 +0000636 bool PrintType, const Module *Context) {
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000637 std::map<const Type *, std::string> TypeNames;
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000638 if (Context == 0) Context = getModuleFromVal(V);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000639
Chris Lattner98cf1f52002-11-20 18:36:02 +0000640 if (Context)
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000641 fillTypeNameTable(Context, TypeNames);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000642
643 if (PrintType)
644 printTypeInt(Out, V->getType(), TypeNames);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000645
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000646 WriteAsOperandInternal(Out, V, TypeNames, 0);
Chris Lattner5e5abe32001-07-20 19:15:21 +0000647 return Out;
648}
649
Reid Spencer58d30f22004-07-04 11:50:43 +0000650
Chris Lattner189d19f2003-11-21 20:23:48 +0000651namespace llvm {
Chris Lattner2e9fee42001-07-12 23:35:26 +0000652
Chris Lattnerfee714f2001-09-07 16:36:04 +0000653class AssemblyWriter {
Misha Brukmana6619a92004-06-21 21:53:56 +0000654 std::ostream &Out;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000655 SlotMachine &Machine;
Chris Lattner7bfee412001-10-29 16:05:51 +0000656 const Module *TheModule;
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000657 std::map<const Type *, std::string> TypeNames;
Chris Lattner8339f7d2003-10-30 23:41:03 +0000658 AssemblyAnnotationWriter *AnnotationWriter;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000659public:
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000660 inline AssemblyWriter(std::ostream &o, SlotMachine &Mac, const Module *M,
Chris Lattner8339f7d2003-10-30 23:41:03 +0000661 AssemblyAnnotationWriter *AAW)
Misha Brukmana6619a92004-06-21 21:53:56 +0000662 : Out(o), Machine(Mac), TheModule(M), AnnotationWriter(AAW) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000663
664 // If the module has a symbol table, take all global types and stuff their
665 // names into the TypeNames map.
666 //
Chris Lattnerb86620e2001-10-29 16:37:48 +0000667 fillTypeNameTable(M, TypeNames);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000668 }
669
Chris Lattner7bfee412001-10-29 16:05:51 +0000670 inline void write(const Module *M) { printModule(M); }
671 inline void write(const GlobalVariable *G) { printGlobal(G); }
Chris Lattner57698e22002-03-26 18:01:55 +0000672 inline void write(const Function *F) { printFunction(F); }
Chris Lattner7bfee412001-10-29 16:05:51 +0000673 inline void write(const BasicBlock *BB) { printBasicBlock(BB); }
Chris Lattner113f4f42002-06-25 16:13:24 +0000674 inline void write(const Instruction *I) { printInstruction(*I); }
Chris Lattner3462ae32001-12-03 22:26:30 +0000675 inline void write(const Constant *CPV) { printConstant(CPV); }
Chris Lattner7db79582001-11-07 04:21:57 +0000676 inline void write(const Type *Ty) { printType(Ty); }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000677
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000678 void writeOperand(const Value *Op, bool PrintType);
Chris Lattner1e194682002-04-18 18:53:13 +0000679
Misha Brukman4685e262004-04-28 15:31:21 +0000680 const Module* getModule() { return TheModule; }
681
Misha Brukmand92f54a2004-11-15 19:30:05 +0000682private:
Chris Lattner7bfee412001-10-29 16:05:51 +0000683 void printModule(const Module *M);
Reid Spencer32af9e82007-01-06 07:24:44 +0000684 void printTypeSymbolTable(const TypeSymbolTable &ST);
685 void printValueSymbolTable(const SymbolTable &ST);
Chris Lattner3462ae32001-12-03 22:26:30 +0000686 void printConstant(const Constant *CPV);
Chris Lattner7bfee412001-10-29 16:05:51 +0000687 void printGlobal(const GlobalVariable *GV);
Chris Lattner57698e22002-03-26 18:01:55 +0000688 void printFunction(const Function *F);
Reid Spencer8c4914c2006-12-31 05:24:50 +0000689 void printArgument(const Argument *FA, FunctionType::ParameterAttributes A);
Chris Lattner7bfee412001-10-29 16:05:51 +0000690 void printBasicBlock(const BasicBlock *BB);
Chris Lattner113f4f42002-06-25 16:13:24 +0000691 void printInstruction(const Instruction &I);
Chris Lattnerd816b532002-04-13 20:53:41 +0000692
693 // printType - Go to extreme measures to attempt to print out a short,
694 // symbolic version of a type name.
695 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000696 std::ostream &printType(const Type *Ty) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000697 return printTypeInt(Out, Ty, TypeNames);
Chris Lattnerd816b532002-04-13 20:53:41 +0000698 }
699
700 // printTypeAtLeastOneLevel - Print out one level of the possibly complex type
701 // without considering any symbolic types that we may have equal to it.
702 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000703 std::ostream &printTypeAtLeastOneLevel(const Type *Ty);
Chris Lattner7bfee412001-10-29 16:05:51 +0000704
Chris Lattner862e3382001-10-13 06:42:36 +0000705 // printInfoComment - Print a little comment after the instruction indicating
706 // which slot it occupies.
Chris Lattner113f4f42002-06-25 16:13:24 +0000707 void printInfoComment(const Value &V);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000708};
Reid Spencerf43ac622004-05-27 22:04:46 +0000709} // end of llvm namespace
Chris Lattner2f7c9632001-06-06 20:29:01 +0000710
Misha Brukmanc566ca362004-03-02 00:22:19 +0000711/// printTypeAtLeastOneLevel - Print out one level of the possibly complex type
712/// without considering any symbolic types that we may have equal to it.
713///
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000714std::ostream &AssemblyWriter::printTypeAtLeastOneLevel(const Type *Ty) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000715 if (const IntegerType *ITy = dyn_cast<IntegerType>(Ty))
716 Out << "i" << utostr(ITy->getBitWidth());
717 else if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
Reid Spencer8c4914c2006-12-31 05:24:50 +0000718 printType(FTy->getReturnType());
Reid Spencer8c4914c2006-12-31 05:24:50 +0000719 Out << " (";
720 unsigned Idx = 1;
Chris Lattnerfa829be2004-02-09 04:14:01 +0000721 for (FunctionType::param_iterator I = FTy->param_begin(),
722 E = FTy->param_end(); I != E; ++I) {
723 if (I != FTy->param_begin())
Misha Brukmana6619a92004-06-21 21:53:56 +0000724 Out << ", ";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000725 printType(*I);
Reid Spencer8c4914c2006-12-31 05:24:50 +0000726 if (FTy->getParamAttrs(Idx)) {
727 Out << " " << FunctionType::getParamAttrsText(FTy->getParamAttrs(Idx));
728 }
729 Idx++;
Chris Lattnerd816b532002-04-13 20:53:41 +0000730 }
731 if (FTy->isVarArg()) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000732 if (FTy->getNumParams()) Out << ", ";
733 Out << "...";
Chris Lattnerd816b532002-04-13 20:53:41 +0000734 }
Misha Brukmana6619a92004-06-21 21:53:56 +0000735 Out << ')';
Reid Spencer136a91c2007-01-05 17:06:19 +0000736 if (FTy->getParamAttrs(0))
737 Out << ' ' << FunctionType::getParamAttrsText(FTy->getParamAttrs(0));
Chris Lattner113f4f42002-06-25 16:13:24 +0000738 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
Andrew Lenharthdcb3c972006-12-08 18:06:16 +0000739 if (STy->isPacked())
740 Out << '<';
Misha Brukmana6619a92004-06-21 21:53:56 +0000741 Out << "{ ";
Chris Lattnerac6db752004-02-09 04:37:31 +0000742 for (StructType::element_iterator I = STy->element_begin(),
743 E = STy->element_end(); I != E; ++I) {
744 if (I != STy->element_begin())
Misha Brukmana6619a92004-06-21 21:53:56 +0000745 Out << ", ";
Chris Lattnerd816b532002-04-13 20:53:41 +0000746 printType(*I);
747 }
Misha Brukmana6619a92004-06-21 21:53:56 +0000748 Out << " }";
Andrew Lenharthdcb3c972006-12-08 18:06:16 +0000749 if (STy->isPacked())
750 Out << '>';
Chris Lattner113f4f42002-06-25 16:13:24 +0000751 } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000752 printType(PTy->getElementType()) << '*';
Chris Lattner113f4f42002-06-25 16:13:24 +0000753 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000754 Out << '[' << ATy->getNumElements() << " x ";
Misha Brukman21bbdb92004-06-04 21:11:51 +0000755 printType(ATy->getElementType()) << ']';
Reid Spencere203d352004-08-20 15:37:30 +0000756 } else if (const PackedType *PTy = dyn_cast<PackedType>(Ty)) {
757 Out << '<' << PTy->getNumElements() << " x ";
758 printType(PTy->getElementType()) << '>';
759 }
Reid Spencerde46e482006-11-02 20:25:50 +0000760 else if (isa<OpaqueType>(Ty)) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000761 Out << "opaque";
Chris Lattnerd816b532002-04-13 20:53:41 +0000762 } else {
Vikram S. Adveb952b542002-07-14 23:14:45 +0000763 if (!Ty->isPrimitiveType())
Misha Brukmana6619a92004-06-21 21:53:56 +0000764 Out << "<unknown derived type>";
Chris Lattnerd816b532002-04-13 20:53:41 +0000765 printType(Ty);
766 }
Misha Brukmana6619a92004-06-21 21:53:56 +0000767 return Out;
Chris Lattnerd816b532002-04-13 20:53:41 +0000768}
769
770
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000771void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType) {
772 if (Operand == 0) {
Chris Lattner08f7d0c2005-02-24 16:58:29 +0000773 Out << "<null operand!>";
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000774 } else {
775 if (PrintType) { Out << ' '; printType(Operand->getType()); }
776 WriteAsOperandInternal(Out, Operand, TypeNames, &Machine);
Chris Lattner08f7d0c2005-02-24 16:58:29 +0000777 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000778}
779
Chris Lattner2f7c9632001-06-06 20:29:01 +0000780
Chris Lattner7bfee412001-10-29 16:05:51 +0000781void AssemblyWriter::printModule(const Module *M) {
Chris Lattner4d8689e2005-03-02 23:12:40 +0000782 if (!M->getModuleIdentifier().empty() &&
Misha Brukmanb1c93172005-04-21 23:48:37 +0000783 // Don't print the ID if it will start a new line (which would
Chris Lattner4d8689e2005-03-02 23:12:40 +0000784 // require a comment char before it).
785 M->getModuleIdentifier().find('\n') == std::string::npos)
786 Out << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
787
Owen Andersone2237542006-10-18 02:21:12 +0000788 if (!M->getDataLayout().empty())
Chris Lattner04897162006-10-22 06:06:56 +0000789 Out << "target datalayout = \"" << M->getDataLayout() << "\"\n";
Owen Andersone2237542006-10-18 02:21:12 +0000790
Chris Lattner8068e0c2003-08-24 13:48:48 +0000791 switch (M->getEndianness()) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000792 case Module::LittleEndian: Out << "target endian = little\n"; break;
793 case Module::BigEndian: Out << "target endian = big\n"; break;
Chris Lattner8068e0c2003-08-24 13:48:48 +0000794 case Module::AnyEndianness: break;
795 }
796 switch (M->getPointerSize()) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000797 case Module::Pointer32: Out << "target pointersize = 32\n"; break;
798 case Module::Pointer64: Out << "target pointersize = 64\n"; break;
Chris Lattner8068e0c2003-08-24 13:48:48 +0000799 case Module::AnyPointerSize: break;
800 }
Reid Spencer48f98c82004-07-25 21:44:54 +0000801 if (!M->getTargetTriple().empty())
Reid Spencerffec7df2004-07-25 21:29:43 +0000802 Out << "target triple = \"" << M->getTargetTriple() << "\"\n";
Misha Brukmanb1c93172005-04-21 23:48:37 +0000803
Chris Lattnereef2fe72006-01-24 04:13:11 +0000804 if (!M->getModuleInlineAsm().empty()) {
Chris Lattnerefaf35d2006-01-24 00:45:30 +0000805 // Split the string into lines, to make it easier to read the .ll file.
Chris Lattnereef2fe72006-01-24 04:13:11 +0000806 std::string Asm = M->getModuleInlineAsm();
Chris Lattnerefaf35d2006-01-24 00:45:30 +0000807 size_t CurPos = 0;
808 size_t NewLine = Asm.find_first_of('\n', CurPos);
809 while (NewLine != std::string::npos) {
810 // We found a newline, print the portion of the asm string from the
811 // last newline up to this newline.
812 Out << "module asm \"";
813 PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.begin()+NewLine),
814 Out);
815 Out << "\"\n";
816 CurPos = NewLine+1;
817 NewLine = Asm.find_first_of('\n', CurPos);
818 }
Chris Lattner3acaf5c2006-01-24 00:40:17 +0000819 Out << "module asm \"";
Chris Lattnerefaf35d2006-01-24 00:45:30 +0000820 PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.end()), Out);
Chris Lattner6ed87bd2006-01-23 23:03:36 +0000821 Out << "\"\n";
822 }
823
Chris Lattner2cdd49d2004-09-14 05:06:58 +0000824 // Loop over the dependent libraries and emit them.
Chris Lattner730cfe42004-09-14 04:51:44 +0000825 Module::lib_iterator LI = M->lib_begin();
826 Module::lib_iterator LE = M->lib_end();
Reid Spencer48f98c82004-07-25 21:44:54 +0000827 if (LI != LE) {
Chris Lattner730cfe42004-09-14 04:51:44 +0000828 Out << "deplibs = [ ";
829 while (LI != LE) {
Chris Lattner2cdd49d2004-09-14 05:06:58 +0000830 Out << '"' << *LI << '"';
Reid Spencerffec7df2004-07-25 21:29:43 +0000831 ++LI;
Chris Lattner730cfe42004-09-14 04:51:44 +0000832 if (LI != LE)
833 Out << ", ";
Reid Spencerffec7df2004-07-25 21:29:43 +0000834 }
835 Out << " ]\n";
Reid Spencercc5ff642004-07-25 18:08:18 +0000836 }
Reid Spencerb9e08772004-09-13 23:44:23 +0000837
Chris Lattner2cdd49d2004-09-14 05:06:58 +0000838 // Loop over the symbol table, emitting all named constants.
Reid Spencer32af9e82007-01-06 07:24:44 +0000839 printTypeSymbolTable(M->getTypeSymbolTable());
840 printValueSymbolTable(M->getValueSymbolTable());
Misha Brukmanb1c93172005-04-21 23:48:37 +0000841
Chris Lattner54932b02006-12-06 04:41:52 +0000842 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
843 I != E; ++I)
Chris Lattner113f4f42002-06-25 16:13:24 +0000844 printGlobal(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000845
Misha Brukmana6619a92004-06-21 21:53:56 +0000846 Out << "\nimplementation ; Functions:\n";
Misha Brukmanb1c93172005-04-21 23:48:37 +0000847
Chris Lattner2cdd49d2004-09-14 05:06:58 +0000848 // Output all of the functions.
Chris Lattner113f4f42002-06-25 16:13:24 +0000849 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
850 printFunction(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000851}
852
Chris Lattner7bfee412001-10-29 16:05:51 +0000853void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000854 if (GV->hasName()) Out << getLLVMName(GV->getName()) << " = ";
Chris Lattner37798642001-09-18 04:01:05 +0000855
Misha Brukmanb1c93172005-04-21 23:48:37 +0000856 if (!GV->hasInitializer())
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000857 switch (GV->getLinkage()) {
858 case GlobalValue::DLLImportLinkage: Out << "dllimport "; break;
859 case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break;
860 default: Out << "external "; break;
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000861 } else {
Chris Lattner379a8d22003-04-16 20:28:45 +0000862 switch (GV->getLinkage()) {
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000863 case GlobalValue::InternalLinkage: Out << "internal "; break;
864 case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
865 case GlobalValue::WeakLinkage: Out << "weak "; break;
866 case GlobalValue::AppendingLinkage: Out << "appending "; break;
867 case GlobalValue::DLLImportLinkage: Out << "dllimport "; break;
868 case GlobalValue::DLLExportLinkage: Out << "dllexport "; break;
869 case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break;
870 case GlobalValue::ExternalLinkage: break;
Misha Brukman70457632004-11-14 21:04:34 +0000871 case GlobalValue::GhostLinkage:
Bill Wendlingf3baad32006-12-07 01:30:32 +0000872 cerr << "GhostLinkage not allowed in AsmWriter!\n";
Misha Brukman70457632004-11-14 21:04:34 +0000873 abort();
Chris Lattner379a8d22003-04-16 20:28:45 +0000874 }
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000875 switch (GV->getVisibility()) {
876 case GlobalValue::DefaultVisibility: break;
877 case GlobalValue::HiddenVisibility: Out << "hidden "; break;
878 default:
879 cerr << "Invalid visibility style!\n";
880 abort();
881 }
882 }
883
Misha Brukmana6619a92004-06-21 21:53:56 +0000884 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattner2413b162001-12-04 00:03:30 +0000885 printType(GV->getType()->getElementType());
Chris Lattner37798642001-09-18 04:01:05 +0000886
Reid Spenceraccd7c72004-07-17 23:47:01 +0000887 if (GV->hasInitializer()) {
888 Constant* C = cast<Constant>(GV->getInitializer());
889 assert(C && "GlobalVar initializer isn't constant?");
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000890 writeOperand(GV->getInitializer(), false);
Reid Spenceraccd7c72004-07-17 23:47:01 +0000891 }
Chris Lattnerf8a974d2005-11-06 06:48:53 +0000892
Chris Lattner4b96c542005-11-12 00:10:19 +0000893 if (GV->hasSection())
894 Out << ", section \"" << GV->getSection() << '"';
895 if (GV->getAlignment())
Chris Lattnerf8a974d2005-11-06 06:48:53 +0000896 Out << ", align " << GV->getAlignment();
Chris Lattner4b96c542005-11-12 00:10:19 +0000897
Chris Lattner113f4f42002-06-25 16:13:24 +0000898 printInfoComment(*GV);
Misha Brukmana6619a92004-06-21 21:53:56 +0000899 Out << "\n";
Chris Lattnerda975502001-09-10 07:58:01 +0000900}
901
Reid Spencer32af9e82007-01-06 07:24:44 +0000902void AssemblyWriter::printTypeSymbolTable(const TypeSymbolTable &ST) {
Reid Spencere7e96712004-05-25 08:53:40 +0000903 // Print the types.
Reid Spencer32af9e82007-01-06 07:24:44 +0000904 for (TypeSymbolTable::const_iterator TI = ST.begin(), TE = ST.end();
905 TI != TE; ++TI) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000906 Out << "\t" << getLLVMName(TI->first) << " = type ";
Reid Spencere7e96712004-05-25 08:53:40 +0000907
908 // Make sure we print out at least one level of the type structure, so
909 // that we do not get %FILE = type %FILE
910 //
911 printTypeAtLeastOneLevel(TI->second) << "\n";
912 }
Reid Spencer32af9e82007-01-06 07:24:44 +0000913}
914
915// printSymbolTable - Run through symbol table looking for constants
916// and types. Emit their declarations.
917void AssemblyWriter::printValueSymbolTable(const SymbolTable &ST) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000918
Reid Spencere7e96712004-05-25 08:53:40 +0000919 // Print the constants, in type plane order.
920 for (SymbolTable::plane_const_iterator PI = ST.plane_begin();
Chris Lattner23e829a2006-12-06 05:12:21 +0000921 PI != ST.plane_end(); ++PI) {
Reid Spencere7e96712004-05-25 08:53:40 +0000922 SymbolTable::value_const_iterator VI = ST.value_begin(PI->first);
923 SymbolTable::value_const_iterator VE = ST.value_end(PI->first);
924
925 for (; VI != VE; ++VI) {
Reid Spenceraccd7c72004-07-17 23:47:01 +0000926 const Value* V = VI->second;
927 const Constant *CPV = dyn_cast<Constant>(V) ;
928 if (CPV && !isa<GlobalValue>(V)) {
Reid Spencer56010e42004-05-26 21:56:09 +0000929 printConstant(CPV);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000930 }
931 }
Chris Lattnera7620d92001-07-15 06:35:59 +0000932 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000933}
934
935
Misha Brukmanc566ca362004-03-02 00:22:19 +0000936/// printConstant - Print out a constant pool entry...
937///
Chris Lattner3462ae32001-12-03 22:26:30 +0000938void AssemblyWriter::printConstant(const Constant *CPV) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000939 // Don't print out unnamed constants, they will be inlined
940 if (!CPV->hasName()) return;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000941
Chris Lattneree998be2001-07-26 16:29:38 +0000942 // Print out name...
Misha Brukmana6619a92004-06-21 21:53:56 +0000943 Out << "\t" << getLLVMName(CPV->getName()) << " =";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000944
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000945 // Write the value out now.
946 writeOperand(CPV, true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000947
Chris Lattner113f4f42002-06-25 16:13:24 +0000948 printInfoComment(*CPV);
Misha Brukmana6619a92004-06-21 21:53:56 +0000949 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000950}
951
Misha Brukmanc566ca362004-03-02 00:22:19 +0000952/// printFunction - Print all aspects of a function.
953///
Chris Lattner113f4f42002-06-25 16:13:24 +0000954void AssemblyWriter::printFunction(const Function *F) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000955 // Print out the return type and name...
Misha Brukmana6619a92004-06-21 21:53:56 +0000956 Out << "\n";
Chris Lattner379a8d22003-04-16 20:28:45 +0000957
Chris Lattner97e36f22004-12-05 06:44:09 +0000958 // Ensure that no local symbols conflict with global symbols.
959 const_cast<Function*>(F)->renameLocalSymbols();
960
Misha Brukmana6619a92004-06-21 21:53:56 +0000961 if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +0000962
Chris Lattner379a8d22003-04-16 20:28:45 +0000963 if (F->isExternal())
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000964 switch (F->getLinkage()) {
965 case GlobalValue::DLLImportLinkage: Out << "declare dllimport "; break;
966 case GlobalValue::ExternalWeakLinkage: Out << "declare extern_weak "; break;
967 default: Out << "declare ";
968 }
Reid Spencer7ce2d2a2006-12-29 20:29:48 +0000969 else {
970 Out << "define ";
Chris Lattner379a8d22003-04-16 20:28:45 +0000971 switch (F->getLinkage()) {
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000972 case GlobalValue::InternalLinkage: Out << "internal "; break;
973 case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
974 case GlobalValue::WeakLinkage: Out << "weak "; break;
975 case GlobalValue::AppendingLinkage: Out << "appending "; break;
976 case GlobalValue::DLLImportLinkage: Out << "dllimport "; break;
977 case GlobalValue::DLLExportLinkage: Out << "dllexport "; break;
978 case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break;
Chris Lattner379a8d22003-04-16 20:28:45 +0000979 case GlobalValue::ExternalLinkage: break;
Misha Brukman70457632004-11-14 21:04:34 +0000980 case GlobalValue::GhostLinkage:
Bill Wendlingf3baad32006-12-07 01:30:32 +0000981 cerr << "GhostLinkage not allowed in AsmWriter!\n";
Misha Brukman70457632004-11-14 21:04:34 +0000982 abort();
Chris Lattner379a8d22003-04-16 20:28:45 +0000983 }
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000984 switch (F->getVisibility()) {
985 case GlobalValue::DefaultVisibility: break;
986 case GlobalValue::HiddenVisibility: Out << "hidden "; break;
987 default:
988 cerr << "Invalid visibility style!\n";
989 abort();
990 }
Reid Spencer7ce2d2a2006-12-29 20:29:48 +0000991 }
Chris Lattner379a8d22003-04-16 20:28:45 +0000992
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000993 // Print the calling convention.
994 switch (F->getCallingConv()) {
995 case CallingConv::C: break; // default
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +0000996 case CallingConv::CSRet: Out << "csretcc "; break;
997 case CallingConv::Fast: Out << "fastcc "; break;
998 case CallingConv::Cold: Out << "coldcc "; break;
999 case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break;
1000 case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break;
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001001 default: Out << "cc" << F->getCallingConv() << " "; break;
1002 }
1003
Reid Spencer8c4914c2006-12-31 05:24:50 +00001004 const FunctionType *FT = F->getFunctionType();
Misha Brukman21bbdb92004-06-04 21:11:51 +00001005 printType(F->getReturnType()) << ' ';
Chris Lattner5b337482003-10-18 05:57:43 +00001006 if (!F->getName().empty())
Misha Brukmana6619a92004-06-21 21:53:56 +00001007 Out << getLLVMName(F->getName());
Chris Lattner5b337482003-10-18 05:57:43 +00001008 else
Misha Brukmana6619a92004-06-21 21:53:56 +00001009 Out << "\"\"";
1010 Out << '(';
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001011 Machine.incorporateFunction(F);
Chris Lattnerfee714f2001-09-07 16:36:04 +00001012
Chris Lattner7bfee412001-10-29 16:05:51 +00001013 // Loop over the arguments, printing them...
Chris Lattnerfee714f2001-09-07 16:36:04 +00001014
Reid Spencer8c4914c2006-12-31 05:24:50 +00001015 unsigned Idx = 1;
Chris Lattner54932b02006-12-06 04:41:52 +00001016 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
Reid Spencer8c4914c2006-12-31 05:24:50 +00001017 I != E; ++I) {
1018 // Insert commas as we go... the first arg doesn't get a comma
1019 if (I != F->arg_begin()) Out << ", ";
1020 printArgument(I, FT->getParamAttrs(Idx));
1021 Idx++;
1022 }
Chris Lattnerfee714f2001-09-07 16:36:04 +00001023
1024 // Finish printing arguments...
Chris Lattner113f4f42002-06-25 16:13:24 +00001025 if (FT->isVarArg()) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001026 if (FT->getNumParams()) Out << ", ";
1027 Out << "..."; // Output varargs portion of signature!
Chris Lattnerfee714f2001-09-07 16:36:04 +00001028 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001029 Out << ')';
Reid Spencer136a91c2007-01-05 17:06:19 +00001030 if (FT->getParamAttrs(0))
1031 Out << ' ' << FunctionType::getParamAttrsText(FT->getParamAttrs(0));
Chris Lattner4b96c542005-11-12 00:10:19 +00001032 if (F->hasSection())
1033 Out << " section \"" << F->getSection() << '"';
Chris Lattnerf8a974d2005-11-06 06:48:53 +00001034 if (F->getAlignment())
1035 Out << " align " << F->getAlignment();
Chris Lattner4b96c542005-11-12 00:10:19 +00001036
Chris Lattner113f4f42002-06-25 16:13:24 +00001037 if (F->isExternal()) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001038 Out << "\n";
Chris Lattnerb2f02e52002-05-06 03:00:40 +00001039 } else {
Misha Brukmana6619a92004-06-21 21:53:56 +00001040 Out << " {";
Misha Brukmanb1c93172005-04-21 23:48:37 +00001041
Chris Lattner6915f8f2002-04-07 22:49:37 +00001042 // Output all of its basic blocks... for the function
Chris Lattner113f4f42002-06-25 16:13:24 +00001043 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
1044 printBasicBlock(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +00001045
Misha Brukmana6619a92004-06-21 21:53:56 +00001046 Out << "}\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +00001047 }
1048
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001049 Machine.purgeFunction();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001050}
1051
Misha Brukmanc566ca362004-03-02 00:22:19 +00001052/// printArgument - This member is called for every argument that is passed into
1053/// the function. Simply print it out
1054///
Reid Spencer8c4914c2006-12-31 05:24:50 +00001055void AssemblyWriter::printArgument(const Argument *Arg,
1056 FunctionType::ParameterAttributes attrs) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001057 // Output type...
Chris Lattner7bfee412001-10-29 16:05:51 +00001058 printType(Arg->getType());
Misha Brukmanb1c93172005-04-21 23:48:37 +00001059
Reid Spencer8c4914c2006-12-31 05:24:50 +00001060 if (attrs != FunctionType::NoAttributeSet)
1061 Out << ' ' << FunctionType::getParamAttrsText(attrs);
1062
Chris Lattner2f7c9632001-06-06 20:29:01 +00001063 // Output name, if available...
1064 if (Arg->hasName())
Misha Brukmana6619a92004-06-21 21:53:56 +00001065 Out << ' ' << getLLVMName(Arg->getName());
Chris Lattner2f7c9632001-06-06 20:29:01 +00001066}
1067
Misha Brukmanc566ca362004-03-02 00:22:19 +00001068/// printBasicBlock - This member is called for each basic block in a method.
1069///
Chris Lattner7bfee412001-10-29 16:05:51 +00001070void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001071 if (BB->hasName()) { // Print out the label if it exists...
Alkis Evlogimenos991d6ad2004-12-10 05:41:10 +00001072 Out << "\n" << getLLVMName(BB->getName(), false) << ':';
Chris Lattner408dbdb2002-05-14 16:02:05 +00001073 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Misha Brukmana6619a92004-06-21 21:53:56 +00001074 Out << "\n; <label>:";
Chris Lattner5e043322007-01-11 03:54:27 +00001075 int Slot = Machine.getLocalSlot(BB);
Chris Lattner757ee0b2004-06-09 19:41:19 +00001076 if (Slot != -1)
Misha Brukmana6619a92004-06-21 21:53:56 +00001077 Out << Slot;
Chris Lattner757ee0b2004-06-09 19:41:19 +00001078 else
Misha Brukmana6619a92004-06-21 21:53:56 +00001079 Out << "<badref>";
Chris Lattner58185f22002-10-02 19:38:55 +00001080 }
Chris Lattner2447ef52003-11-20 00:09:43 +00001081
1082 if (BB->getParent() == 0)
Misha Brukmana6619a92004-06-21 21:53:56 +00001083 Out << "\t\t; Error: Block without parent!";
Chris Lattner2447ef52003-11-20 00:09:43 +00001084 else {
1085 if (BB != &BB->getParent()->front()) { // Not the entry block?
1086 // Output predecessors for the block...
Misha Brukmana6619a92004-06-21 21:53:56 +00001087 Out << "\t\t;";
Chris Lattner2447ef52003-11-20 00:09:43 +00001088 pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
Misha Brukmanb1c93172005-04-21 23:48:37 +00001089
Chris Lattner2447ef52003-11-20 00:09:43 +00001090 if (PI == PE) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001091 Out << " No predecessors!";
Chris Lattner2447ef52003-11-20 00:09:43 +00001092 } else {
Misha Brukmana6619a92004-06-21 21:53:56 +00001093 Out << " preds =";
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001094 writeOperand(*PI, false);
Chris Lattner2447ef52003-11-20 00:09:43 +00001095 for (++PI; PI != PE; ++PI) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001096 Out << ',';
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001097 writeOperand(*PI, false);
Chris Lattner2447ef52003-11-20 00:09:43 +00001098 }
Chris Lattner00211f12003-11-16 22:59:57 +00001099 }
Chris Lattner58185f22002-10-02 19:38:55 +00001100 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001101 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001102
Misha Brukmana6619a92004-06-21 21:53:56 +00001103 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001104
Misha Brukmana6619a92004-06-21 21:53:56 +00001105 if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001106
Chris Lattnerfee714f2001-09-07 16:36:04 +00001107 // Output all of the instructions in the basic block...
Chris Lattner113f4f42002-06-25 16:13:24 +00001108 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
1109 printInstruction(*I);
Chris Lattner96cdd272004-03-08 18:51:45 +00001110
Misha Brukmana6619a92004-06-21 21:53:56 +00001111 if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001112}
1113
Chris Lattner862e3382001-10-13 06:42:36 +00001114
Misha Brukmanc566ca362004-03-02 00:22:19 +00001115/// printInfoComment - Print a little comment after the instruction indicating
1116/// which slot it occupies.
1117///
Chris Lattner113f4f42002-06-25 16:13:24 +00001118void AssemblyWriter::printInfoComment(const Value &V) {
1119 if (V.getType() != Type::VoidTy) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001120 Out << "\t\t; <";
Misha Brukman21bbdb92004-06-04 21:11:51 +00001121 printType(V.getType()) << '>';
Chris Lattner862e3382001-10-13 06:42:36 +00001122
Chris Lattner113f4f42002-06-25 16:13:24 +00001123 if (!V.hasName()) {
Chris Lattner5e043322007-01-11 03:54:27 +00001124 int SlotNum;
1125 if (const GlobalValue *GV = dyn_cast<GlobalValue>(&V))
1126 SlotNum = Machine.getGlobalSlot(GV);
1127 else
1128 SlotNum = Machine.getLocalSlot(&V);
Chris Lattner757ee0b2004-06-09 19:41:19 +00001129 if (SlotNum == -1)
Misha Brukmana6619a92004-06-21 21:53:56 +00001130 Out << ":<badref>";
Reid Spencer8beac692004-06-09 15:26:53 +00001131 else
Misha Brukmana6619a92004-06-21 21:53:56 +00001132 Out << ':' << SlotNum; // Print out the def slot taken.
Chris Lattner862e3382001-10-13 06:42:36 +00001133 }
Chris Lattnerb6c21db2005-02-01 01:24:01 +00001134 Out << " [#uses=" << V.getNumUses() << ']'; // Output # uses
Chris Lattner862e3382001-10-13 06:42:36 +00001135 }
1136}
1137
Reid Spencere7141c82006-08-28 01:02:49 +00001138// This member is called for each Instruction in a function..
Chris Lattner113f4f42002-06-25 16:13:24 +00001139void AssemblyWriter::printInstruction(const Instruction &I) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001140 if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001141
Misha Brukmana6619a92004-06-21 21:53:56 +00001142 Out << "\t";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001143
1144 // Print out name if it exists...
Chris Lattner113f4f42002-06-25 16:13:24 +00001145 if (I.hasName())
Misha Brukmana6619a92004-06-21 21:53:56 +00001146 Out << getLLVMName(I.getName()) << " = ";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001147
Chris Lattner06038452005-05-06 05:51:46 +00001148 // If this is a volatile load or store, print out the volatile marker.
Chris Lattner504f9242003-09-08 17:45:59 +00001149 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
Chris Lattner06038452005-05-06 05:51:46 +00001150 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile())) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001151 Out << "volatile ";
Chris Lattner06038452005-05-06 05:51:46 +00001152 } else if (isa<CallInst>(I) && cast<CallInst>(I).isTailCall()) {
1153 // If this is a call, check if it's a tail call.
1154 Out << "tail ";
1155 }
Chris Lattner504f9242003-09-08 17:45:59 +00001156
Chris Lattner2f7c9632001-06-06 20:29:01 +00001157 // Print out the opcode...
Misha Brukmana6619a92004-06-21 21:53:56 +00001158 Out << I.getOpcodeName();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001159
Reid Spencer45e52392006-12-03 06:27:29 +00001160 // Print out the compare instruction predicates
1161 if (const FCmpInst *FCI = dyn_cast<FCmpInst>(&I)) {
Reid Spencer812a1be2006-12-04 05:19:18 +00001162 Out << " " << getPredicateText(FCI->getPredicate());
Reid Spencer45e52392006-12-03 06:27:29 +00001163 } else if (const ICmpInst *ICI = dyn_cast<ICmpInst>(&I)) {
Reid Spencer812a1be2006-12-04 05:19:18 +00001164 Out << " " << getPredicateText(ICI->getPredicate());
Reid Spencer45e52392006-12-03 06:27:29 +00001165 }
1166
Chris Lattner2f7c9632001-06-06 20:29:01 +00001167 // Print out the type of the operands...
Chris Lattner113f4f42002-06-25 16:13:24 +00001168 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001169
1170 // Special case conditional branches to swizzle the condition out to the front
Chris Lattner113f4f42002-06-25 16:13:24 +00001171 if (isa<BranchInst>(I) && I.getNumOperands() > 1) {
1172 writeOperand(I.getOperand(2), true);
Misha Brukmana6619a92004-06-21 21:53:56 +00001173 Out << ',';
Chris Lattner2f7c9632001-06-06 20:29:01 +00001174 writeOperand(Operand, true);
Misha Brukmana6619a92004-06-21 21:53:56 +00001175 Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001176 writeOperand(I.getOperand(1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001177
Chris Lattner8d48df22002-04-13 18:34:38 +00001178 } else if (isa<SwitchInst>(I)) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001179 // Special case switch statement to get formatting nice and correct...
Misha Brukmana6619a92004-06-21 21:53:56 +00001180 writeOperand(Operand , true); Out << ',';
1181 writeOperand(I.getOperand(1), true); Out << " [";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001182
Chris Lattner113f4f42002-06-25 16:13:24 +00001183 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001184 Out << "\n\t\t";
1185 writeOperand(I.getOperand(op ), true); Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001186 writeOperand(I.getOperand(op+1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001187 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001188 Out << "\n\t]";
Chris Lattnerda558102001-10-02 03:41:24 +00001189 } else if (isa<PHINode>(I)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001190 Out << ' ';
Chris Lattner113f4f42002-06-25 16:13:24 +00001191 printType(I.getType());
Misha Brukmana6619a92004-06-21 21:53:56 +00001192 Out << ' ';
Chris Lattner2f7c9632001-06-06 20:29:01 +00001193
Chris Lattner113f4f42002-06-25 16:13:24 +00001194 for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001195 if (op) Out << ", ";
Misha Brukmanb1c93172005-04-21 23:48:37 +00001196 Out << '[';
Misha Brukmana6619a92004-06-21 21:53:56 +00001197 writeOperand(I.getOperand(op ), false); Out << ',';
1198 writeOperand(I.getOperand(op+1), false); Out << " ]";
Chris Lattner931ef3b2001-06-11 15:04:20 +00001199 }
Chris Lattner862e3382001-10-13 06:42:36 +00001200 } else if (isa<ReturnInst>(I) && !Operand) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001201 Out << " void";
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001202 } else if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
1203 // Print the calling convention being used.
1204 switch (CI->getCallingConv()) {
1205 case CallingConv::C: break; // default
Chris Lattner29d20852006-05-19 21:58:52 +00001206 case CallingConv::CSRet: Out << " csretcc"; break;
1207 case CallingConv::Fast: Out << " fastcc"; break;
1208 case CallingConv::Cold: Out << " coldcc"; break;
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +00001209 case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break;
1210 case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break;
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001211 default: Out << " cc" << CI->getCallingConv(); break;
1212 }
1213
Chris Lattner463d6a52003-08-05 15:34:45 +00001214 const PointerType *PTy = cast<PointerType>(Operand->getType());
1215 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1216 const Type *RetTy = FTy->getReturnType();
Chris Lattner2f2d9472001-11-06 21:28:12 +00001217
Chris Lattner463d6a52003-08-05 15:34:45 +00001218 // If possible, print out the short form of the call instruction. We can
Chris Lattner6915f8f2002-04-07 22:49:37 +00001219 // only do this if the first argument is a pointer to a nonvararg function,
Chris Lattner463d6a52003-08-05 15:34:45 +00001220 // and if the return type is not a pointer to a function.
Chris Lattner2f2d9472001-11-06 21:28:12 +00001221 //
Chris Lattner463d6a52003-08-05 15:34:45 +00001222 if (!FTy->isVarArg() &&
Misha Brukmanb1c93172005-04-21 23:48:37 +00001223 (!isa<PointerType>(RetTy) ||
Chris Lattnerd9a36a62002-07-25 20:58:51 +00001224 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001225 Out << ' '; printType(RetTy);
Chris Lattner2f2d9472001-11-06 21:28:12 +00001226 writeOperand(Operand, false);
1227 } else {
1228 writeOperand(Operand, true);
1229 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001230 Out << '(';
Reid Spencer8c4914c2006-12-31 05:24:50 +00001231 for (unsigned op = 1, Eop = I.getNumOperands(); op < Eop; ++op) {
1232 if (op > 1)
1233 Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001234 writeOperand(I.getOperand(op), true);
Reid Spencer8c4914c2006-12-31 05:24:50 +00001235 if (FTy->getParamAttrs(op) != FunctionType::NoAttributeSet)
1236 Out << " " << FTy->getParamAttrsText(FTy->getParamAttrs(op));
Chris Lattner2f7c9632001-06-06 20:29:01 +00001237 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001238 Out << " )";
Reid Spencer136a91c2007-01-05 17:06:19 +00001239 if (FTy->getParamAttrs(0) != FunctionType::NoAttributeSet)
1240 Out << ' ' << FTy->getParamAttrsText(FTy->getParamAttrs(0));
Chris Lattner113f4f42002-06-25 16:13:24 +00001241 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Chris Lattner463d6a52003-08-05 15:34:45 +00001242 const PointerType *PTy = cast<PointerType>(Operand->getType());
1243 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1244 const Type *RetTy = FTy->getReturnType();
1245
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001246 // Print the calling convention being used.
1247 switch (II->getCallingConv()) {
1248 case CallingConv::C: break; // default
Chris Lattner29d20852006-05-19 21:58:52 +00001249 case CallingConv::CSRet: Out << " csretcc"; break;
1250 case CallingConv::Fast: Out << " fastcc"; break;
1251 case CallingConv::Cold: Out << " coldcc"; break;
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +00001252 case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break;
1253 case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break;
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001254 default: Out << " cc" << II->getCallingConv(); break;
1255 }
1256
Chris Lattner463d6a52003-08-05 15:34:45 +00001257 // If possible, print out the short form of the invoke instruction. We can
1258 // only do this if the first argument is a pointer to a nonvararg function,
1259 // and if the return type is not a pointer to a function.
1260 //
1261 if (!FTy->isVarArg() &&
Misha Brukmanb1c93172005-04-21 23:48:37 +00001262 (!isa<PointerType>(RetTy) ||
Chris Lattner463d6a52003-08-05 15:34:45 +00001263 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001264 Out << ' '; printType(RetTy);
Chris Lattner463d6a52003-08-05 15:34:45 +00001265 writeOperand(Operand, false);
1266 } else {
1267 writeOperand(Operand, true);
1268 }
1269
Misha Brukmana6619a92004-06-21 21:53:56 +00001270 Out << '(';
Reid Spencer8c4914c2006-12-31 05:24:50 +00001271 for (unsigned op = 3, Eop = I.getNumOperands(); op < Eop; ++op) {
1272 if (op > 3)
1273 Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001274 writeOperand(I.getOperand(op), true);
Reid Spencer9cfa4e82006-12-31 22:17:01 +00001275 if (FTy->getParamAttrs(op-2) != FunctionType::NoAttributeSet)
1276 Out << " " << FTy->getParamAttrsText(FTy->getParamAttrs(op-2));
Chris Lattner862e3382001-10-13 06:42:36 +00001277 }
1278
Reid Spencer136a91c2007-01-05 17:06:19 +00001279 Out << " )";
1280 if (FTy->getParamAttrs(0) != FunctionType::NoAttributeSet)
1281 Out << " " << FTy->getParamAttrsText(FTy->getParamAttrs(0));
1282 Out << "\n\t\t\tto";
Chris Lattner862e3382001-10-13 06:42:36 +00001283 writeOperand(II->getNormalDest(), true);
Misha Brukmana6619a92004-06-21 21:53:56 +00001284 Out << " unwind";
Chris Lattnerfae8ab32004-02-08 21:44:31 +00001285 writeOperand(II->getUnwindDest(), true);
Chris Lattner862e3382001-10-13 06:42:36 +00001286
Chris Lattner113f4f42002-06-25 16:13:24 +00001287 } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(&I)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001288 Out << ' ';
Chris Lattner8d48df22002-04-13 18:34:38 +00001289 printType(AI->getType()->getElementType());
1290 if (AI->isArrayAllocation()) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001291 Out << ',';
Chris Lattner8d48df22002-04-13 18:34:38 +00001292 writeOperand(AI->getArraySize(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001293 }
Nate Begeman848622f2005-11-05 09:21:28 +00001294 if (AI->getAlignment()) {
Chris Lattner7aeee3a2005-11-05 21:20:34 +00001295 Out << ", align " << AI->getAlignment();
Nate Begeman848622f2005-11-05 09:21:28 +00001296 }
Chris Lattner862e3382001-10-13 06:42:36 +00001297 } else if (isa<CastInst>(I)) {
Chris Lattnerc96e96b2003-11-17 01:17:04 +00001298 if (Operand) writeOperand(Operand, true); // Work with broken code
Misha Brukmana6619a92004-06-21 21:53:56 +00001299 Out << " to ";
Chris Lattner113f4f42002-06-25 16:13:24 +00001300 printType(I.getType());
Chris Lattner5b337482003-10-18 05:57:43 +00001301 } else if (isa<VAArgInst>(I)) {
Chris Lattnerc96e96b2003-11-17 01:17:04 +00001302 if (Operand) writeOperand(Operand, true); // Work with broken code
Misha Brukmana6619a92004-06-21 21:53:56 +00001303 Out << ", ";
Chris Lattnerf70da102003-05-08 02:44:12 +00001304 printType(I.getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +00001305 } else if (Operand) { // Print the normal way...
1306
Misha Brukmanb1c93172005-04-21 23:48:37 +00001307 // PrintAllTypes - Instructions who have operands of all the same type
Chris Lattner2f7c9632001-06-06 20:29:01 +00001308 // omit the type from all but the first operand. If the instruction has
1309 // different type operands (for example br), then they are all printed.
1310 bool PrintAllTypes = false;
1311 const Type *TheType = Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001312
Chris Lattner52bd5cb92004-03-12 05:53:14 +00001313 // Shift Left & Right print both types even for Ubyte LHS, and select prints
1314 // types even if all operands are bools.
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001315 if (isa<ShiftInst>(I) || isa<SelectInst>(I) || isa<StoreInst>(I) ||
1316 isa<ShuffleVectorInst>(I)) {
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00001317 PrintAllTypes = true;
1318 } else {
1319 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
1320 Operand = I.getOperand(i);
1321 if (Operand->getType() != TheType) {
1322 PrintAllTypes = true; // We have differing types! Print them all!
1323 break;
1324 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001325 }
1326 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001327
Chris Lattner7bfee412001-10-29 16:05:51 +00001328 if (!PrintAllTypes) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001329 Out << ' ';
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00001330 printType(TheType);
Chris Lattner7bfee412001-10-29 16:05:51 +00001331 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001332
Chris Lattner113f4f42002-06-25 16:13:24 +00001333 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001334 if (i) Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001335 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001336 }
1337 }
1338
Chris Lattner862e3382001-10-13 06:42:36 +00001339 printInfoComment(I);
Misha Brukmana6619a92004-06-21 21:53:56 +00001340 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001341}
1342
1343
1344//===----------------------------------------------------------------------===//
1345// External Interface declarations
1346//===----------------------------------------------------------------------===//
1347
Chris Lattner8339f7d2003-10-30 23:41:03 +00001348void Module::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001349 SlotMachine SlotTable(this);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001350 AssemblyWriter W(o, SlotTable, this, AAW);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001351 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001352}
1353
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001354void GlobalVariable::print(std::ostream &o) const {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001355 SlotMachine SlotTable(getParent());
Chris Lattner8339f7d2003-10-30 23:41:03 +00001356 AssemblyWriter W(o, SlotTable, getParent(), 0);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001357 W.write(this);
Chris Lattneradfe0d12001-09-10 20:08:19 +00001358}
1359
Chris Lattner8339f7d2003-10-30 23:41:03 +00001360void Function::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001361 SlotMachine SlotTable(getParent());
Chris Lattner8339f7d2003-10-30 23:41:03 +00001362 AssemblyWriter W(o, SlotTable, getParent(), AAW);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001363
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001364 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001365}
1366
Chris Lattnereef2fe72006-01-24 04:13:11 +00001367void InlineAsm::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001368 WriteAsOperand(o, this, true, 0);
Chris Lattnereef2fe72006-01-24 04:13:11 +00001369}
1370
Chris Lattner8339f7d2003-10-30 23:41:03 +00001371void BasicBlock::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001372 SlotMachine SlotTable(getParent());
Misha Brukmanb1c93172005-04-21 23:48:37 +00001373 AssemblyWriter W(o, SlotTable,
Chris Lattner8339f7d2003-10-30 23:41:03 +00001374 getParent() ? getParent()->getParent() : 0, AAW);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001375 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001376}
1377
Chris Lattner8339f7d2003-10-30 23:41:03 +00001378void Instruction::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001379 const Function *F = getParent() ? getParent()->getParent() : 0;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001380 SlotMachine SlotTable(F);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001381 AssemblyWriter W(o, SlotTable, F ? F->getParent() : 0, AAW);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001382
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001383 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001384}
Chris Lattner7db79582001-11-07 04:21:57 +00001385
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001386void Constant::print(std::ostream &o) const {
1387 if (this == 0) { o << "<null> constant value\n"; return; }
Chris Lattner7d734802002-09-10 15:53:49 +00001388
Misha Brukman21bbdb92004-06-04 21:11:51 +00001389 o << ' ' << getType()->getDescription() << ' ';
Evan Cheng5b19a802006-03-01 22:17:00 +00001390
1391 std::map<const Type *, std::string> TypeTable;
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001392 WriteConstantInt(o, this, TypeTable, 0);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001393}
1394
Misha Brukmanb1c93172005-04-21 23:48:37 +00001395void Type::print(std::ostream &o) const {
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001396 if (this == 0)
1397 o << "<null Type>";
1398 else
1399 o << getDescription();
1400}
1401
Chris Lattner2e9fa6d2002-04-09 19:48:49 +00001402void Argument::print(std::ostream &o) const {
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001403 WriteAsOperand(o, this, true, getParent() ? getParent()->getParent() : 0);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001404}
1405
Reid Spencer52641832004-05-25 18:14:38 +00001406// Value::dump - allow easy printing of Values from the debugger.
1407// Located here because so much of the needed functionality is here.
Bill Wendling22e978a2006-12-07 20:04:42 +00001408void Value::dump() const { print(*cerr.stream()); cerr << '\n'; }
Reid Spencer52641832004-05-25 18:14:38 +00001409
1410// Type::dump - allow easy printing of Values from the debugger.
1411// Located here because so much of the needed functionality is here.
Bill Wendling22e978a2006-12-07 20:04:42 +00001412void Type::dump() const { print(*cerr.stream()); cerr << '\n'; }
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001413
1414//===----------------------------------------------------------------------===//
Chris Lattnerfc9f1c92006-12-06 06:40:49 +00001415// SlotMachine Implementation
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001416//===----------------------------------------------------------------------===//
1417
1418#if 0
Bill Wendlingf3baad32006-12-07 01:30:32 +00001419#define SC_DEBUG(X) cerr << X
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001420#else
1421#define SC_DEBUG(X)
1422#endif
1423
1424// Module level constructor. Causes the contents of the Module (sans functions)
1425// to be added to the slot table.
Misha Brukmanb1c93172005-04-21 23:48:37 +00001426SlotMachine::SlotMachine(const Module *M)
Reid Spencer56010e42004-05-26 21:56:09 +00001427 : TheModule(M) ///< Saved for lazy initialization.
1428 , TheFunction(0)
Reid Spencerb0ac8c42004-08-16 07:46:33 +00001429 , FunctionProcessed(false)
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001430{
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001431}
1432
1433// Function level constructor. Causes the contents of the Module and the one
1434// function provided to be added to the slot table.
Chris Lattner23e829a2006-12-06 05:12:21 +00001435SlotMachine::SlotMachine(const Function *F)
1436 : TheModule(F ? F->getParent() : 0) ///< Saved for lazy initialization
Reid Spencer56010e42004-05-26 21:56:09 +00001437 , TheFunction(F) ///< Saved for lazy initialization
Reid Spencerb0ac8c42004-08-16 07:46:33 +00001438 , FunctionProcessed(false)
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001439{
Reid Spencer56010e42004-05-26 21:56:09 +00001440}
1441
Chris Lattner5e043322007-01-11 03:54:27 +00001442inline void SlotMachine::initialize() {
Chris Lattner23e829a2006-12-06 05:12:21 +00001443 if (TheModule) {
Misha Brukmanb1c93172005-04-21 23:48:37 +00001444 processModule();
Reid Spencer56010e42004-05-26 21:56:09 +00001445 TheModule = 0; ///< Prevent re-processing next time we're called.
1446 }
Chris Lattner193de902006-12-06 05:27:40 +00001447 if (TheFunction && !FunctionProcessed)
Misha Brukmanb1c93172005-04-21 23:48:37 +00001448 processFunction();
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001449}
1450
1451// Iterate through all the global variables, functions, and global
Misha Brukmanb1c93172005-04-21 23:48:37 +00001452// variable initializers and create slots for them.
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001453void SlotMachine::processModule() {
1454 SC_DEBUG("begin processModule!\n");
1455
Chris Lattner0dda8612006-12-06 06:15:43 +00001456 // Add all of the unnamed global variables to the value table.
Chris Lattner54932b02006-12-06 04:41:52 +00001457 for (Module::const_global_iterator I = TheModule->global_begin(),
1458 E = TheModule->global_end(); I != E; ++I)
Chris Lattner0dda8612006-12-06 06:15:43 +00001459 if (!I->hasName())
Chris Lattnerea862a32007-01-09 07:55:49 +00001460 CreateModuleSlot(I);
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001461
Chris Lattner0dda8612006-12-06 06:15:43 +00001462 // Add all the unnamed functions to the table.
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001463 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
1464 I != E; ++I)
Chris Lattner0dda8612006-12-06 06:15:43 +00001465 if (!I->hasName())
Chris Lattnerea862a32007-01-09 07:55:49 +00001466 CreateModuleSlot(I);
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001467
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001468 SC_DEBUG("end processModule!\n");
1469}
1470
1471
Reid Spencer56010e42004-05-26 21:56:09 +00001472// Process the arguments, basic blocks, and instructions of a function.
1473void SlotMachine::processFunction() {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001474 SC_DEBUG("begin processFunction!\n");
1475
Chris Lattner0dda8612006-12-06 06:15:43 +00001476 // Add all the function arguments with no names.
Misha Brukmanb1c93172005-04-21 23:48:37 +00001477 for(Function::const_arg_iterator AI = TheFunction->arg_begin(),
Chris Lattner531f9e92005-03-15 04:54:21 +00001478 AE = TheFunction->arg_end(); AI != AE; ++AI)
Chris Lattner0dda8612006-12-06 06:15:43 +00001479 if (!AI->hasName())
Chris Lattnerea862a32007-01-09 07:55:49 +00001480 CreateFunctionSlot(AI);
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001481
1482 SC_DEBUG("Inserting Instructions:\n");
1483
Chris Lattner0dda8612006-12-06 06:15:43 +00001484 // Add all of the basic blocks and instructions with no names.
Misha Brukmanb1c93172005-04-21 23:48:37 +00001485 for (Function::const_iterator BB = TheFunction->begin(),
Reid Spencer56010e42004-05-26 21:56:09 +00001486 E = TheFunction->end(); BB != E; ++BB) {
Chris Lattner0dda8612006-12-06 06:15:43 +00001487 if (!BB->hasName())
Chris Lattnerea862a32007-01-09 07:55:49 +00001488 CreateFunctionSlot(BB);
Chris Lattner0dda8612006-12-06 06:15:43 +00001489 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
1490 if (I->getType() != Type::VoidTy && !I->hasName())
Chris Lattnerea862a32007-01-09 07:55:49 +00001491 CreateFunctionSlot(I);
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001492 }
1493
Reid Spencerb0ac8c42004-08-16 07:46:33 +00001494 FunctionProcessed = true;
1495
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001496 SC_DEBUG("end processFunction!\n");
1497}
1498
Chris Lattner193de902006-12-06 05:27:40 +00001499/// Clean up after incorporating a function. This is the only way to get out of
Chris Lattner5e043322007-01-11 03:54:27 +00001500/// the function incorporation state that affects get*Slot/Create*Slot. Function
Chris Lattner89e774e2007-01-09 07:46:21 +00001501/// incorporation state is indicated by TheFunction != 0.
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001502void SlotMachine::purgeFunction() {
1503 SC_DEBUG("begin purgeFunction!\n");
1504 fMap.clear(); // Simply discard the function level map
Reid Spencer56010e42004-05-26 21:56:09 +00001505 TheFunction = 0;
Reid Spencerb0ac8c42004-08-16 07:46:33 +00001506 FunctionProcessed = false;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001507 SC_DEBUG("end purgeFunction!\n");
1508}
1509
Chris Lattner5e043322007-01-11 03:54:27 +00001510/// getGlobalSlot - Get the slot number of a global value.
1511int SlotMachine::getGlobalSlot(const GlobalValue *V) {
1512 // Check for uninitialized state and do lazy initialization.
1513 initialize();
1514
1515 // Find the type plane in the module map
1516 TypedPlanes::const_iterator MI = mMap.find(V->getType());
1517 if (MI == mMap.end()) return -1;
1518
1519 // Lookup the value in the module plane's map.
1520 ValueMap::const_iterator MVI = MI->second.map.find(V);
Reid Spencer5656cad2007-01-11 07:58:19 +00001521 return MVI != MI->second.map.end() ? int(MVI->second) : -1;
Chris Lattner5e043322007-01-11 03:54:27 +00001522}
Reid Spencer56010e42004-05-26 21:56:09 +00001523
Chris Lattner5e043322007-01-11 03:54:27 +00001524
1525/// getLocalSlot - Get the slot number for a value that is local to a function.
1526int SlotMachine::getLocalSlot(const Value *V) {
1527 assert(!isa<Constant>(V) && "Can't get a constant or global slot with this!");
1528
1529 // Check for uninitialized state and do lazy initialization.
1530 initialize();
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001531
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001532 // Get the type of the value
Chris Lattner5e043322007-01-11 03:54:27 +00001533 const Type *VTy = V->getType();
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001534
Chris Lattner5e043322007-01-11 03:54:27 +00001535 TypedPlanes::const_iterator FI = fMap.find(VTy);
1536 if (FI == fMap.end()) return -1;
1537
1538 // Lookup the Value in the function and module maps.
1539 ValueMap::const_iterator FVI = FI->second.map.find(V);
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001540 TypedPlanes::const_iterator MI = mMap.find(VTy);
Chris Lattner5e043322007-01-11 03:54:27 +00001541
Chris Lattner550a8f32007-01-11 04:30:21 +00001542 // If the value doesn't exist in the function map, it is a <badref>
1543 if (FVI == FI->second.map.end()) return -1;
Chris Lattner5e043322007-01-11 03:54:27 +00001544
1545 // Return the slot number as the module's contribution to
1546 // the type plane plus the index in the function's contribution
1547 // to the type plane.
1548 if (MI != mMap.end())
1549 return MI->second.next_slot + FVI->second;
1550 else
1551 return FVI->second;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001552}
1553
Reid Spencer58d30f22004-07-04 11:50:43 +00001554
Chris Lattnerea862a32007-01-09 07:55:49 +00001555/// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
1556void SlotMachine::CreateModuleSlot(const GlobalValue *V) {
Chris Lattner04398e82007-01-09 08:04:59 +00001557 assert(V && "Can't insert a null Value into SlotMachine!");
1558
1559 unsigned DestSlot = 0;
1560 const Type *VTy = V->getType();
1561
Chris Lattner2cf85c72007-01-10 06:43:26 +00001562 ValuePlane &PlaneMap = mMap[VTy];
1563 DestSlot = PlaneMap.map[V] = PlaneMap.next_slot++;
Chris Lattner04398e82007-01-09 08:04:59 +00001564
1565 SC_DEBUG(" Inserting value [" << VTy << "] = " << V << " slot=" <<
1566 DestSlot << " [");
1567 // G = Global, F = Function, o = other
1568 SC_DEBUG((isa<GlobalVariable>(V) ? 'G' : 'F') << "]\n");
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001569}
1570
1571
Chris Lattnerea862a32007-01-09 07:55:49 +00001572/// CreateSlot - Create a new slot for the specified value if it has no name.
1573void SlotMachine::CreateFunctionSlot(const Value *V) {
1574 const Type *VTy = V->getType();
1575 assert(VTy != Type::VoidTy && !V->hasName() && "Doesn't need a slot!");
Chris Lattner04398e82007-01-09 08:04:59 +00001576
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001577 unsigned DestSlot = 0;
Chris Lattner04398e82007-01-09 08:04:59 +00001578
Chris Lattner2cf85c72007-01-10 06:43:26 +00001579 ValuePlane &PlaneMap = fMap[VTy];
1580 DestSlot = PlaneMap.map[V] = PlaneMap.next_slot++;
Chris Lattner04398e82007-01-09 08:04:59 +00001581
Chris Lattnerf8090cb2006-12-06 05:55:41 +00001582 // G = Global, F = Function, o = other
Chris Lattner04398e82007-01-09 08:04:59 +00001583 SC_DEBUG(" Inserting value [" << VTy << "] = " << V << " slot=" <<
1584 DestSlot << " [o]\n");
1585}