blob: 072fc51d22608cf442e2e7d4742757e335d8b74e [file] [log] [blame]
Chris Lattnerf7e79482002-04-07 22:31:46 +00001//===-- AsmWriter.cpp - Printing LLVM as an assembly file -----------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
10// This library implements the functionality defined in llvm/Assembly/Writer.h
11//
Chris Lattner189088e2002-04-12 18:21:53 +000012// Note that these routines must be extremely tolerant of various errors in the
Chris Lattnerf70da102003-05-08 02:44:12 +000013// LLVM code, because it can be used for debugging transformations.
Chris Lattner189088e2002-04-12 18:21:53 +000014//
Chris Lattner2f7c9632001-06-06 20:29:01 +000015//===----------------------------------------------------------------------===//
16
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +000017#include "llvm/Assembly/Writer.h"
Chris Lattner7f8845a2002-07-23 18:07:49 +000018#include "llvm/Assembly/PrintModulePass.h"
Chris Lattner8339f7d2003-10-30 23:41:03 +000019#include "llvm/Assembly/AsmAnnotationWriter.h"
Chris Lattnerf7b6d312005-05-06 20:26:43 +000020#include "llvm/CallingConv.h"
Chris Lattnerc70b3f62004-01-20 19:50:34 +000021#include "llvm/Constants.h"
Chris Lattner913d18f2002-04-29 18:46:50 +000022#include "llvm/DerivedTypes.h"
Chris Lattner8bbcda22006-01-25 18:57:27 +000023#include "llvm/InlineAsm.h"
Vikram S. Adveb952b542002-07-14 23:14:45 +000024#include "llvm/Instruction.h"
Misha Brukman2d3fa9e2004-07-29 16:53:53 +000025#include "llvm/Instructions.h"
Chris Lattnerc70b3f62004-01-20 19:50:34 +000026#include "llvm/Module.h"
Chris Lattnerfee714f2001-09-07 16:36:04 +000027#include "llvm/SymbolTable.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000028#include "llvm/ADT/StringExtras.h"
29#include "llvm/ADT/STLExtras.h"
Bill Wendlingdfc91892006-11-28 02:09:03 +000030#include "llvm/Support/CFG.h"
Jim Laskeyb74c6662005-08-17 19:34:49 +000031#include "llvm/Support/MathExtras.h"
Bill Wendlingdfc91892006-11-28 02:09:03 +000032#include "llvm/Support/Streams.h"
Chris Lattnerfee714f2001-09-07 16:36:04 +000033#include <algorithm>
Chris Lattnerfc9f1c92006-12-06 06:40:49 +000034#include <iostream>
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
78 /// plane. Its an error to ask for something not in the SlotMachine.
79 /// Its an error to ask for a Type*
Chris Lattner757ee0b2004-06-09 19:41:19 +000080 int getSlot(const Value *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
Misha Brukmanb1c93172005-04-21 23:48:37 +0000105 /// Values can be crammed into here at will. If they haven't
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000106 /// been inserted already, they get inserted, otherwise they are ignored.
107 /// Either way, the slot number for the Value* is returned.
Chris Lattner193de902006-12-06 05:27:40 +0000108 unsigned getOrCreateSlot(const Value *V);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000109
110 /// Insert a value into the value table. Return the slot number
111 /// that it now occupies. BadThings(TM) will happen if you insert a
Misha Brukmanb1c93172005-04-21 23:48:37 +0000112 /// Value that's already been inserted.
Chris Lattner23e829a2006-12-06 05:12:21 +0000113 unsigned insertValue(const Value *V);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000114
115 /// Add all of the module level global variables (and their initializers)
116 /// and function declarations, but not the contents of those functions.
117 void processModule();
118
Reid Spencer56010e42004-05-26 21:56:09 +0000119 /// Add all of the functions arguments, basic blocks, and instructions
120 void processFunction();
121
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000122 SlotMachine(const SlotMachine &); // DO NOT IMPLEMENT
123 void operator=(const SlotMachine &); // DO NOT IMPLEMENT
124
125/// @}
126/// @name Data
127/// @{
128public:
129
130 /// @brief The module for which we are holding slot numbers
Reid Spencer56010e42004-05-26 21:56:09 +0000131 const Module* TheModule;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000132
Reid Spencer56010e42004-05-26 21:56:09 +0000133 /// @brief The function for which we are holding slot numbers
134 const Function* TheFunction;
Reid Spencerb0ac8c42004-08-16 07:46:33 +0000135 bool FunctionProcessed;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000136
137 /// @brief The TypePlanes map for the module level data
138 TypedPlanes mMap;
139
140 /// @brief The TypePlanes map for the function level data
141 TypedPlanes fMap;
142
143/// @}
144
145};
146
Chris Lattner60a7dd12004-07-15 02:51:31 +0000147} // end namespace llvm
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000148
Chris Lattnerc2d3d312006-08-27 22:42:52 +0000149static RegisterPass<PrintModulePass>
Chris Lattnere7134c52006-08-21 17:20:01 +0000150X("printm", "Print module to stderr");
Chris Lattnerc2d3d312006-08-27 22:42:52 +0000151static RegisterPass<PrintFunctionPass>
Chris Lattnere7134c52006-08-21 17:20:01 +0000152Y("print","Print function to stderr");
Chris Lattner7f8845a2002-07-23 18:07:49 +0000153
Misha Brukmanb1c93172005-04-21 23:48:37 +0000154static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
Chris Lattnera9f0a112006-12-06 05:50:41 +0000155 std::map<const Type *, std::string> &TypeTable,
Reid Spencer58d30f22004-07-04 11:50:43 +0000156 SlotMachine *Machine);
157
Chris Lattnerb86620e2001-10-29 16:37:48 +0000158static const Module *getModuleFromVal(const Value *V) {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000159 if (const Argument *MA = dyn_cast<Argument>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +0000160 return MA->getParent() ? MA->getParent()->getParent() : 0;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000161 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +0000162 return BB->getParent() ? BB->getParent()->getParent() : 0;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000163 else if (const Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner57698e22002-03-26 18:01:55 +0000164 const Function *M = I->getParent() ? I->getParent()->getParent() : 0;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000165 return M ? M->getParent() : 0;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000166 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +0000167 return GV->getParent();
Chris Lattnerb86620e2001-10-29 16:37:48 +0000168 return 0;
169}
170
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000171static SlotMachine *createSlotMachine(const Value *V) {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000172 if (const Argument *FA = dyn_cast<Argument>(V)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000173 return new SlotMachine(FA->getParent());
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000174 } else if (const Instruction *I = dyn_cast<Instruction>(V)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000175 return new SlotMachine(I->getParent()->getParent());
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000176 } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000177 return new SlotMachine(BB->getParent());
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000178 } else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)){
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000179 return new SlotMachine(GV->getParent());
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000180 } else if (const Function *Func = dyn_cast<Function>(V)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000181 return new SlotMachine(Func);
Chris Lattner7bfee412001-10-29 16:05:51 +0000182 }
183 return 0;
184}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000185
Chris Lattner1c343042003-08-22 05:40:38 +0000186// getLLVMName - Turn the specified string into an 'LLVM name', which is either
187// prefixed with % (if the string only contains simple characters) or is
188// surrounded with ""'s (if it has special chars in it).
Alkis Evlogimenos991d6ad2004-12-10 05:41:10 +0000189static std::string getLLVMName(const std::string &Name,
190 bool prefixName = true) {
Chris Lattner1c343042003-08-22 05:40:38 +0000191 assert(!Name.empty() && "Cannot get empty name!");
192
193 // First character cannot start with a number...
194 if (Name[0] >= '0' && Name[0] <= '9')
195 return "\"" + Name + "\"";
196
197 // Scan to see if we have any characters that are not on the "white list"
198 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
199 char C = Name[i];
200 assert(C != '"' && "Illegal character in LLVM value name!");
201 if ((C < 'a' || C > 'z') && (C < 'A' || C > 'Z') && (C < '0' || C > '9') &&
202 C != '-' && C != '.' && C != '_')
203 return "\"" + Name + "\"";
204 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000205
Chris Lattner1c343042003-08-22 05:40:38 +0000206 // If we get here, then the identifier is legal to use as a "VarID".
Alkis Evlogimenos991d6ad2004-12-10 05:41:10 +0000207 if (prefixName)
208 return "%"+Name;
209 else
210 return Name;
Chris Lattner1c343042003-08-22 05:40:38 +0000211}
212
Chris Lattnerb86620e2001-10-29 16:37:48 +0000213
Misha Brukmanc566ca362004-03-02 00:22:19 +0000214/// fillTypeNameTable - If the module has a symbol table, take all global types
215/// and stuff their names into the TypeNames map.
216///
Chris Lattnerb86620e2001-10-29 16:37:48 +0000217static void fillTypeNameTable(const Module *M,
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000218 std::map<const Type *, std::string> &TypeNames) {
Chris Lattner98cf1f52002-11-20 18:36:02 +0000219 if (!M) return;
220 const SymbolTable &ST = M->getSymbolTable();
Reid Spencere7e96712004-05-25 08:53:40 +0000221 SymbolTable::type_const_iterator TI = ST.type_begin();
Chris Lattner23e829a2006-12-06 05:12:21 +0000222 for (; TI != ST.type_end(); ++TI) {
Reid Spencere7e96712004-05-25 08:53:40 +0000223 // As a heuristic, don't insert pointer to primitive types, because
224 // they are used too often to have a single useful name.
225 //
226 const Type *Ty = cast<Type>(TI->second);
227 if (!isa<PointerType>(Ty) ||
Reid Spencer56010e42004-05-26 21:56:09 +0000228 !cast<PointerType>(Ty)->getElementType()->isPrimitiveType() ||
229 isa<OpaqueType>(cast<PointerType>(Ty)->getElementType()))
Reid Spencere7e96712004-05-25 08:53:40 +0000230 TypeNames.insert(std::make_pair(Ty, getLLVMName(TI->first)));
Chris Lattnerb86620e2001-10-29 16:37:48 +0000231 }
232}
233
234
235
Misha Brukmanb1c93172005-04-21 23:48:37 +0000236static void calcTypeName(const Type *Ty,
John Criswellcd116ba2004-06-01 14:54:08 +0000237 std::vector<const Type *> &TypeStack,
238 std::map<const Type *, std::string> &TypeNames,
239 std::string & Result){
240 if (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty)) {
241 Result += Ty->getDescription(); // Base case
242 return;
243 }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000244
245 // Check to see if the type is named.
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000246 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
John Criswellcd116ba2004-06-01 14:54:08 +0000247 if (I != TypeNames.end()) {
248 Result += I->second;
249 return;
250 }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000251
John Criswellcd116ba2004-06-01 14:54:08 +0000252 if (isa<OpaqueType>(Ty)) {
253 Result += "opaque";
254 return;
255 }
Chris Lattnerf14ead92003-10-30 00:22:33 +0000256
Chris Lattnerb86620e2001-10-29 16:37:48 +0000257 // Check to see if the Type is already on the stack...
258 unsigned Slot = 0, CurSize = TypeStack.size();
259 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
260
Misha Brukmanb1c93172005-04-21 23:48:37 +0000261 // This is another base case for the recursion. In this case, we know
Chris Lattnerb86620e2001-10-29 16:37:48 +0000262 // that we have looped back to a type that we have previously visited.
263 // Generate the appropriate upreference to handle this.
John Criswellcd116ba2004-06-01 14:54:08 +0000264 if (Slot < CurSize) {
265 Result += "\\" + utostr(CurSize-Slot); // Here's the upreference
266 return;
267 }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000268
269 TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
Misha Brukmanb1c93172005-04-21 23:48:37 +0000270
Chris Lattner6b727592004-06-17 18:19:28 +0000271 switch (Ty->getTypeID()) {
Chris Lattner91db5822002-03-29 03:44:36 +0000272 case Type::FunctionTyID: {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000273 const FunctionType *FTy = cast<FunctionType>(Ty);
John Criswellcd116ba2004-06-01 14:54:08 +0000274 calcTypeName(FTy->getReturnType(), TypeStack, TypeNames, Result);
275 Result += " (";
Chris Lattnerfa829be2004-02-09 04:14:01 +0000276 for (FunctionType::param_iterator I = FTy->param_begin(),
277 E = FTy->param_end(); I != E; ++I) {
278 if (I != FTy->param_begin())
Chris Lattnerb86620e2001-10-29 16:37:48 +0000279 Result += ", ";
John Criswellcd116ba2004-06-01 14:54:08 +0000280 calcTypeName(*I, TypeStack, TypeNames, Result);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000281 }
Chris Lattnerd816b532002-04-13 20:53:41 +0000282 if (FTy->isVarArg()) {
Chris Lattnerfa829be2004-02-09 04:14:01 +0000283 if (FTy->getNumParams()) Result += ", ";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000284 Result += "...";
285 }
286 Result += ")";
287 break;
288 }
289 case Type::StructTyID: {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000290 const StructType *STy = cast<StructType>(Ty);
John Criswellcd116ba2004-06-01 14:54:08 +0000291 Result += "{ ";
Chris Lattnerac6db752004-02-09 04:37:31 +0000292 for (StructType::element_iterator I = STy->element_begin(),
293 E = STy->element_end(); I != E; ++I) {
294 if (I != STy->element_begin())
Chris Lattnerb86620e2001-10-29 16:37:48 +0000295 Result += ", ";
John Criswellcd116ba2004-06-01 14:54:08 +0000296 calcTypeName(*I, TypeStack, TypeNames, Result);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000297 }
298 Result += " }";
299 break;
300 }
301 case Type::PointerTyID:
Misha Brukmanb1c93172005-04-21 23:48:37 +0000302 calcTypeName(cast<PointerType>(Ty)->getElementType(),
John Criswellcd116ba2004-06-01 14:54:08 +0000303 TypeStack, TypeNames, Result);
304 Result += "*";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000305 break;
306 case Type::ArrayTyID: {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000307 const ArrayType *ATy = cast<ArrayType>(Ty);
John Criswellcd116ba2004-06-01 14:54:08 +0000308 Result += "[" + utostr(ATy->getNumElements()) + " x ";
309 calcTypeName(ATy->getElementType(), TypeStack, TypeNames, Result);
310 Result += "]";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000311 break;
312 }
Brian Gaeke02209042004-08-20 06:00:58 +0000313 case Type::PackedTyID: {
314 const PackedType *PTy = cast<PackedType>(Ty);
315 Result += "<" + utostr(PTy->getNumElements()) + " x ";
316 calcTypeName(PTy->getElementType(), TypeStack, TypeNames, Result);
317 Result += ">";
318 break;
319 }
Chris Lattner15285ab2003-05-14 17:50:47 +0000320 case Type::OpaqueTyID:
John Criswellcd116ba2004-06-01 14:54:08 +0000321 Result += "opaque";
Chris Lattner15285ab2003-05-14 17:50:47 +0000322 break;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000323 default:
John Criswellcd116ba2004-06-01 14:54:08 +0000324 Result += "<unrecognized-type>";
Chris Lattnerfc9f1c92006-12-06 06:40:49 +0000325 break;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000326 }
327
328 TypeStack.pop_back(); // Remove self from stack...
Chris Lattnerb86620e2001-10-29 16:37:48 +0000329}
330
331
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000332/// printTypeInt - The internal guts of printing out a type that has a
333/// potentially named portion.
334///
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000335static std::ostream &printTypeInt(std::ostream &Out, const Type *Ty,
336 std::map<const Type *, std::string> &TypeNames) {
Chris Lattnerb86620e2001-10-29 16:37:48 +0000337 // Primitive types always print out their description, regardless of whether
338 // they have been named or not.
339 //
Chris Lattner92d60532003-10-30 00:12:51 +0000340 if (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty))
341 return Out << Ty->getDescription();
Chris Lattnerb86620e2001-10-29 16:37:48 +0000342
343 // Check to see if the type is named.
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000344 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000345 if (I != TypeNames.end()) return Out << I->second;
346
347 // Otherwise we have a type that has not been named but is a derived type.
348 // Carefully recurse the type hierarchy to print out any contained symbolic
349 // names.
350 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000351 std::vector<const Type *> TypeStack;
John Criswellcd116ba2004-06-01 14:54:08 +0000352 std::string TypeName;
353 calcTypeName(Ty, TypeStack, TypeNames, TypeName);
Chris Lattner7f74a562002-01-20 22:54:45 +0000354 TypeNames.insert(std::make_pair(Ty, TypeName));//Cache type name for later use
John Criswellcd116ba2004-06-01 14:54:08 +0000355 return (Out << TypeName);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000356}
357
Chris Lattner34b95182001-10-31 04:33:19 +0000358
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000359/// WriteTypeSymbolic - This attempts to write the specified type as a symbolic
360/// type, iff there is an entry in the modules symbol table for the specified
361/// type or one of it's component types. This is slower than a simple x << Type
362///
Chris Lattner189d19f2003-11-21 20:23:48 +0000363std::ostream &llvm::WriteTypeSymbolic(std::ostream &Out, const Type *Ty,
364 const Module *M) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000365 Out << ' ';
Chris Lattnerb86620e2001-10-29 16:37:48 +0000366
Chris Lattnerfc9f1c92006-12-06 06:40:49 +0000367 // If they want us to print out a type, but there is no context, we can't
368 // print it symbolically.
369 if (!M)
Chris Lattner72f866e2001-10-29 16:40:32 +0000370 return Out << Ty->getDescription();
Chris Lattnerfc9f1c92006-12-06 06:40:49 +0000371
372 std::map<const Type *, std::string> TypeNames;
373 fillTypeNameTable(M, TypeNames);
374 return printTypeInt(Out, Ty, TypeNames);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000375}
376
Chris Lattner6ed87bd2006-01-23 23:03:36 +0000377// PrintEscapedString - Print each character of the specified string, escaping
378// it if it is not printable or if it is an escape char.
379static void PrintEscapedString(const std::string &Str, std::ostream &Out) {
380 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
381 unsigned char C = Str[i];
382 if (isprint(C) && C != '"' && C != '\\') {
383 Out << C;
384 } else {
385 Out << '\\'
386 << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'))
387 << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
388 }
389 }
390}
391
Chris Lattnerfc9f1c92006-12-06 06:40:49 +0000392static const char *getPredicateText(unsigned predicate) {
Reid Spencer812a1be2006-12-04 05:19:18 +0000393 const char * pred = "unknown";
394 switch (predicate) {
395 case FCmpInst::FCMP_FALSE: pred = "false"; break;
396 case FCmpInst::FCMP_OEQ: pred = "oeq"; break;
397 case FCmpInst::FCMP_OGT: pred = "ogt"; break;
398 case FCmpInst::FCMP_OGE: pred = "oge"; break;
399 case FCmpInst::FCMP_OLT: pred = "olt"; break;
400 case FCmpInst::FCMP_OLE: pred = "ole"; break;
401 case FCmpInst::FCMP_ONE: pred = "one"; break;
402 case FCmpInst::FCMP_ORD: pred = "ord"; break;
403 case FCmpInst::FCMP_UNO: pred = "uno"; break;
404 case FCmpInst::FCMP_UEQ: pred = "ueq"; break;
405 case FCmpInst::FCMP_UGT: pred = "ugt"; break;
406 case FCmpInst::FCMP_UGE: pred = "uge"; break;
407 case FCmpInst::FCMP_ULT: pred = "ult"; break;
408 case FCmpInst::FCMP_ULE: pred = "ule"; break;
409 case FCmpInst::FCMP_UNE: pred = "une"; break;
410 case FCmpInst::FCMP_TRUE: pred = "true"; break;
411 case ICmpInst::ICMP_EQ: pred = "eq"; break;
412 case ICmpInst::ICMP_NE: pred = "ne"; break;
413 case ICmpInst::ICMP_SGT: pred = "sgt"; break;
414 case ICmpInst::ICMP_SGE: pred = "sge"; break;
415 case ICmpInst::ICMP_SLT: pred = "slt"; break;
416 case ICmpInst::ICMP_SLE: pred = "sle"; break;
417 case ICmpInst::ICMP_UGT: pred = "ugt"; break;
418 case ICmpInst::ICMP_UGE: pred = "uge"; break;
419 case ICmpInst::ICMP_ULT: pred = "ult"; break;
420 case ICmpInst::ICMP_ULE: pred = "ule"; break;
421 }
422 return pred;
423}
424
Misha Brukmanb1c93172005-04-21 23:48:37 +0000425/// @brief Internal constant writer.
426static void WriteConstantInt(std::ostream &Out, const Constant *CV,
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000427 std::map<const Type *, std::string> &TypeTable,
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000428 SlotMachine *Machine) {
Jim Laskey6be3d8e2006-02-27 10:33:53 +0000429 const int IndentSize = 4;
430 static std::string Indent = "\n";
Chris Lattner1e194682002-04-18 18:53:13 +0000431 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
Chris Lattner94e39cb2006-09-28 22:50:29 +0000432 Out << (CB->getValue() ? "true" : "false");
Reid Spencere0fc4df2006-10-20 07:07:24 +0000433 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
434 if (CI->getType()->isSigned())
435 Out << CI->getSExtValue();
436 else
437 Out << CI->getZExtValue();
Chris Lattner1e194682002-04-18 18:53:13 +0000438 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
439 // We would like to output the FP constant value in exponential notation,
440 // but we cannot do this if doing so will lose precision. Check here to
441 // make sure that we only output it in exponential format if we can parse
442 // the value back and get the same value.
443 //
444 std::string StrVal = ftostr(CFP->getValue());
445
446 // Check to make sure that the stringized number is not some string like
447 // "Inf" or NaN, that atof will accept, but the lexer will not. Check that
448 // the string matches the "[-+]?[0-9]" regex.
449 //
450 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
451 ((StrVal[0] == '-' || StrVal[0] == '+') &&
Brian Gaeke87b4f072003-06-17 23:55:35 +0000452 (StrVal[1] >= '0' && StrVal[1] <= '9')))
Chris Lattner1e194682002-04-18 18:53:13 +0000453 // Reparse stringized version!
454 if (atof(StrVal.c_str()) == CFP->getValue()) {
Chris Lattner472cc102005-01-04 01:56:57 +0000455 Out << StrVal;
456 return;
Chris Lattner1e194682002-04-18 18:53:13 +0000457 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000458
Chris Lattner1e194682002-04-18 18:53:13 +0000459 // Otherwise we could not reparse it to exactly the same value, so we must
460 // output the string in hexadecimal format!
Chris Lattner472cc102005-01-04 01:56:57 +0000461 assert(sizeof(double) == sizeof(uint64_t) &&
Chris Lattner1e194682002-04-18 18:53:13 +0000462 "assuming that double is 64 bits!");
Jim Laskeyb74c6662005-08-17 19:34:49 +0000463 Out << "0x" << utohexstr(DoubleToBits(CFP->getValue()));
Chris Lattner1e194682002-04-18 18:53:13 +0000464
Chris Lattner76b2ff42004-02-15 05:55:15 +0000465 } else if (isa<ConstantAggregateZero>(CV)) {
466 Out << "zeroinitializer";
Chris Lattner1e194682002-04-18 18:53:13 +0000467 } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
468 // As a special case, print the array as a string if it is an array of
469 // ubytes or an array of sbytes with positive values.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000470 //
Chris Lattner1e194682002-04-18 18:53:13 +0000471 const Type *ETy = CA->getType()->getElementType();
Chris Lattner6ed87bd2006-01-23 23:03:36 +0000472 if (CA->isString()) {
Chris Lattner1e194682002-04-18 18:53:13 +0000473 Out << "c\"";
Chris Lattner6ed87bd2006-01-23 23:03:36 +0000474 PrintEscapedString(CA->getAsString(), Out);
Chris Lattner1e194682002-04-18 18:53:13 +0000475 Out << "\"";
476
477 } else { // Cannot output in string format...
Misha Brukman21bbdb92004-06-04 21:11:51 +0000478 Out << '[';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000479 if (CA->getNumOperands()) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000480 Out << ' ';
Chris Lattner1e194682002-04-18 18:53:13 +0000481 printTypeInt(Out, ETy, TypeTable);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000482 WriteAsOperandInternal(Out, CA->getOperand(0),
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000483 TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000484 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
485 Out << ", ";
Chris Lattner1e194682002-04-18 18:53:13 +0000486 printTypeInt(Out, ETy, TypeTable);
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000487 WriteAsOperandInternal(Out, CA->getOperand(i), TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000488 }
489 }
490 Out << " ]";
491 }
492 } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000493 Out << '{';
Jim Laskey3bb78742006-02-25 12:27:03 +0000494 unsigned N = CS->getNumOperands();
495 if (N) {
Jim Laskey6be3d8e2006-02-27 10:33:53 +0000496 if (N > 2) {
497 Indent += std::string(IndentSize, ' ');
498 Out << Indent;
499 } else {
500 Out << ' ';
501 }
Chris Lattnerd84bb632002-04-16 21:36:08 +0000502 printTypeInt(Out, CS->getOperand(0)->getType(), TypeTable);
503
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000504 WriteAsOperandInternal(Out, CS->getOperand(0), TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000505
Jim Laskey3bb78742006-02-25 12:27:03 +0000506 for (unsigned i = 1; i < N; i++) {
Chris Lattnerd84bb632002-04-16 21:36:08 +0000507 Out << ", ";
Jim Laskey6be3d8e2006-02-27 10:33:53 +0000508 if (N > 2) Out << Indent;
Chris Lattnerd84bb632002-04-16 21:36:08 +0000509 printTypeInt(Out, CS->getOperand(i)->getType(), TypeTable);
510
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000511 WriteAsOperandInternal(Out, CS->getOperand(i), TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000512 }
Jim Laskey6be3d8e2006-02-27 10:33:53 +0000513 if (N > 2) Indent.resize(Indent.size() - IndentSize);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000514 }
Jim Laskey3bb78742006-02-25 12:27:03 +0000515
Chris Lattnerd84bb632002-04-16 21:36:08 +0000516 Out << " }";
Brian Gaeke02209042004-08-20 06:00:58 +0000517 } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(CV)) {
518 const Type *ETy = CP->getType()->getElementType();
Misha Brukmanb1c93172005-04-21 23:48:37 +0000519 assert(CP->getNumOperands() > 0 &&
Brian Gaeke02209042004-08-20 06:00:58 +0000520 "Number of operands for a PackedConst must be > 0");
521 Out << '<';
522 Out << ' ';
523 printTypeInt(Out, ETy, TypeTable);
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000524 WriteAsOperandInternal(Out, CP->getOperand(0), TypeTable, Machine);
Brian Gaeke02209042004-08-20 06:00:58 +0000525 for (unsigned i = 1, e = CP->getNumOperands(); i != e; ++i) {
526 Out << ", ";
527 printTypeInt(Out, ETy, TypeTable);
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000528 WriteAsOperandInternal(Out, CP->getOperand(i), TypeTable, Machine);
Brian Gaeke02209042004-08-20 06:00:58 +0000529 }
530 Out << " >";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000531 } else if (isa<ConstantPointerNull>(CV)) {
532 Out << "null";
533
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000534 } else if (isa<UndefValue>(CV)) {
535 Out << "undef";
536
Chris Lattner3cd8c562002-07-30 18:54:25 +0000537 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Reid Spencer812a1be2006-12-04 05:19:18 +0000538 Out << CE->getOpcodeName();
539 if (CE->isCompare())
540 Out << " " << getPredicateText(CE->getPredicate());
541 Out << " (";
Misha Brukmanb1c93172005-04-21 23:48:37 +0000542
Vikram S. Adveb952b542002-07-14 23:14:45 +0000543 for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
544 printTypeInt(Out, (*OI)->getType(), TypeTable);
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000545 WriteAsOperandInternal(Out, *OI, TypeTable, Machine);
Vikram S. Adveb952b542002-07-14 23:14:45 +0000546 if (OI+1 != CE->op_end())
Chris Lattner3cd8c562002-07-30 18:54:25 +0000547 Out << ", ";
Vikram S. Adveb952b542002-07-14 23:14:45 +0000548 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000549
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000550 if (CE->isCast()) {
Chris Lattner83b396b2002-08-15 19:37:43 +0000551 Out << " to ";
552 printTypeInt(Out, CE->getType(), TypeTable);
553 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000554
Misha Brukman21bbdb92004-06-04 21:11:51 +0000555 Out << ')';
Chris Lattner83b396b2002-08-15 19:37:43 +0000556
Chris Lattnerd84bb632002-04-16 21:36:08 +0000557 } else {
Vikram S. Adveb952b542002-07-14 23:14:45 +0000558 Out << "<placeholder or erroneous Constant>";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000559 }
560}
561
562
Misha Brukmanc566ca362004-03-02 00:22:19 +0000563/// WriteAsOperand - Write the name of the specified value out to the specified
564/// ostream. This can be useful when you just want to print int %reg126, not
565/// the whole instruction that generated it.
566///
Misha Brukmanb1c93172005-04-21 23:48:37 +0000567static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000568 std::map<const Type*, std::string> &TypeTable,
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000569 SlotMachine *Machine) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000570 Out << ' ';
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000571 if (V->hasName())
Chris Lattner1c343042003-08-22 05:40:38 +0000572 Out << getLLVMName(V->getName());
Reid Spenceraccd7c72004-07-17 23:47:01 +0000573 else {
574 const Constant *CV = dyn_cast<Constant>(V);
Chris Lattnera2d810d2006-01-25 22:26:05 +0000575 if (CV && !isa<GlobalValue>(CV)) {
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000576 WriteConstantInt(Out, CV, TypeTable, Machine);
Chris Lattnera2d810d2006-01-25 22:26:05 +0000577 } else if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
578 Out << "asm ";
579 if (IA->hasSideEffects())
580 Out << "sideeffect ";
581 Out << '"';
582 PrintEscapedString(IA->getAsmString(), Out);
583 Out << "\", \"";
584 PrintEscapedString(IA->getConstraintString(), Out);
585 Out << '"';
586 } else {
Chris Lattnerd84bb632002-04-16 21:36:08 +0000587 int Slot;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000588 if (Machine) {
Reid Spencer56010e42004-05-26 21:56:09 +0000589 Slot = Machine->getSlot(V);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000590 } else {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000591 Machine = createSlotMachine(V);
Chris Lattner96749c42006-05-14 18:46:52 +0000592 if (Machine)
Chris Lattner757ee0b2004-06-09 19:41:19 +0000593 Slot = Machine->getSlot(V);
594 else
595 Slot = -1;
Reid Spencer56010e42004-05-26 21:56:09 +0000596 delete Machine;
Chris Lattnerd84bb632002-04-16 21:36:08 +0000597 }
Chris Lattner757ee0b2004-06-09 19:41:19 +0000598 if (Slot != -1)
599 Out << '%' << Slot;
600 else
601 Out << "<badref>";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000602 }
603 }
604}
605
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000606/// WriteAsOperand - Write the name of the specified value out to the specified
607/// ostream. This can be useful when you just want to print int %reg126, not
608/// the whole instruction that generated it.
609///
Chris Lattner189d19f2003-11-21 20:23:48 +0000610std::ostream &llvm::WriteAsOperand(std::ostream &Out, const Value *V,
Chris Lattner0dda8612006-12-06 06:15:43 +0000611 bool PrintType, const Module *Context) {
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000612 std::map<const Type *, std::string> TypeNames;
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000613 if (Context == 0) Context = getModuleFromVal(V);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000614
Chris Lattner98cf1f52002-11-20 18:36:02 +0000615 if (Context)
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000616 fillTypeNameTable(Context, TypeNames);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000617
618 if (PrintType)
619 printTypeInt(Out, V->getType(), TypeNames);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000620
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000621 WriteAsOperandInternal(Out, V, TypeNames, 0);
Chris Lattner5e5abe32001-07-20 19:15:21 +0000622 return Out;
623}
624
Reid Spencer58d30f22004-07-04 11:50:43 +0000625
Chris Lattner189d19f2003-11-21 20:23:48 +0000626namespace llvm {
Chris Lattner2e9fee42001-07-12 23:35:26 +0000627
Chris Lattnerfee714f2001-09-07 16:36:04 +0000628class AssemblyWriter {
Misha Brukmana6619a92004-06-21 21:53:56 +0000629 std::ostream &Out;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000630 SlotMachine &Machine;
Chris Lattner7bfee412001-10-29 16:05:51 +0000631 const Module *TheModule;
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000632 std::map<const Type *, std::string> TypeNames;
Chris Lattner8339f7d2003-10-30 23:41:03 +0000633 AssemblyAnnotationWriter *AnnotationWriter;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000634public:
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000635 inline AssemblyWriter(std::ostream &o, SlotMachine &Mac, const Module *M,
Chris Lattner8339f7d2003-10-30 23:41:03 +0000636 AssemblyAnnotationWriter *AAW)
Misha Brukmana6619a92004-06-21 21:53:56 +0000637 : Out(o), Machine(Mac), TheModule(M), AnnotationWriter(AAW) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000638
639 // If the module has a symbol table, take all global types and stuff their
640 // names into the TypeNames map.
641 //
Chris Lattnerb86620e2001-10-29 16:37:48 +0000642 fillTypeNameTable(M, TypeNames);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000643 }
644
Chris Lattner7bfee412001-10-29 16:05:51 +0000645 inline void write(const Module *M) { printModule(M); }
646 inline void write(const GlobalVariable *G) { printGlobal(G); }
Chris Lattner57698e22002-03-26 18:01:55 +0000647 inline void write(const Function *F) { printFunction(F); }
Chris Lattner7bfee412001-10-29 16:05:51 +0000648 inline void write(const BasicBlock *BB) { printBasicBlock(BB); }
Chris Lattner113f4f42002-06-25 16:13:24 +0000649 inline void write(const Instruction *I) { printInstruction(*I); }
Chris Lattner3462ae32001-12-03 22:26:30 +0000650 inline void write(const Constant *CPV) { printConstant(CPV); }
Chris Lattner7db79582001-11-07 04:21:57 +0000651 inline void write(const Type *Ty) { printType(Ty); }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000652
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000653 void writeOperand(const Value *Op, bool PrintType);
Chris Lattner1e194682002-04-18 18:53:13 +0000654
Misha Brukman4685e262004-04-28 15:31:21 +0000655 const Module* getModule() { return TheModule; }
656
Misha Brukmand92f54a2004-11-15 19:30:05 +0000657private:
Chris Lattner7bfee412001-10-29 16:05:51 +0000658 void printModule(const Module *M);
659 void printSymbolTable(const SymbolTable &ST);
Chris Lattner3462ae32001-12-03 22:26:30 +0000660 void printConstant(const Constant *CPV);
Chris Lattner7bfee412001-10-29 16:05:51 +0000661 void printGlobal(const GlobalVariable *GV);
Chris Lattner57698e22002-03-26 18:01:55 +0000662 void printFunction(const Function *F);
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000663 void printArgument(const Argument *FA);
Chris Lattner7bfee412001-10-29 16:05:51 +0000664 void printBasicBlock(const BasicBlock *BB);
Chris Lattner113f4f42002-06-25 16:13:24 +0000665 void printInstruction(const Instruction &I);
Chris Lattnerd816b532002-04-13 20:53:41 +0000666
667 // printType - Go to extreme measures to attempt to print out a short,
668 // symbolic version of a type name.
669 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000670 std::ostream &printType(const Type *Ty) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000671 return printTypeInt(Out, Ty, TypeNames);
Chris Lattnerd816b532002-04-13 20:53:41 +0000672 }
673
674 // printTypeAtLeastOneLevel - Print out one level of the possibly complex type
675 // without considering any symbolic types that we may have equal to it.
676 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000677 std::ostream &printTypeAtLeastOneLevel(const Type *Ty);
Chris Lattner7bfee412001-10-29 16:05:51 +0000678
Chris Lattner862e3382001-10-13 06:42:36 +0000679 // printInfoComment - Print a little comment after the instruction indicating
680 // which slot it occupies.
Chris Lattner113f4f42002-06-25 16:13:24 +0000681 void printInfoComment(const Value &V);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000682};
Reid Spencerf43ac622004-05-27 22:04:46 +0000683} // end of llvm namespace
Chris Lattner2f7c9632001-06-06 20:29:01 +0000684
Misha Brukmanc566ca362004-03-02 00:22:19 +0000685/// printTypeAtLeastOneLevel - Print out one level of the possibly complex type
686/// without considering any symbolic types that we may have equal to it.
687///
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000688std::ostream &AssemblyWriter::printTypeAtLeastOneLevel(const Type *Ty) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000689 if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000690 printType(FTy->getReturnType()) << " (";
Chris Lattnerfa829be2004-02-09 04:14:01 +0000691 for (FunctionType::param_iterator I = FTy->param_begin(),
692 E = FTy->param_end(); I != E; ++I) {
693 if (I != FTy->param_begin())
Misha Brukmana6619a92004-06-21 21:53:56 +0000694 Out << ", ";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000695 printType(*I);
Chris Lattnerd816b532002-04-13 20:53:41 +0000696 }
697 if (FTy->isVarArg()) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000698 if (FTy->getNumParams()) Out << ", ";
699 Out << "...";
Chris Lattnerd816b532002-04-13 20:53:41 +0000700 }
Misha Brukmana6619a92004-06-21 21:53:56 +0000701 Out << ')';
Chris Lattner113f4f42002-06-25 16:13:24 +0000702 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000703 Out << "{ ";
Chris Lattnerac6db752004-02-09 04:37:31 +0000704 for (StructType::element_iterator I = STy->element_begin(),
705 E = STy->element_end(); I != E; ++I) {
706 if (I != STy->element_begin())
Misha Brukmana6619a92004-06-21 21:53:56 +0000707 Out << ", ";
Chris Lattnerd816b532002-04-13 20:53:41 +0000708 printType(*I);
709 }
Misha Brukmana6619a92004-06-21 21:53:56 +0000710 Out << " }";
Chris Lattner113f4f42002-06-25 16:13:24 +0000711 } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000712 printType(PTy->getElementType()) << '*';
Chris Lattner113f4f42002-06-25 16:13:24 +0000713 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000714 Out << '[' << ATy->getNumElements() << " x ";
Misha Brukman21bbdb92004-06-04 21:11:51 +0000715 printType(ATy->getElementType()) << ']';
Reid Spencere203d352004-08-20 15:37:30 +0000716 } else if (const PackedType *PTy = dyn_cast<PackedType>(Ty)) {
717 Out << '<' << PTy->getNumElements() << " x ";
718 printType(PTy->getElementType()) << '>';
719 }
Reid Spencerde46e482006-11-02 20:25:50 +0000720 else if (isa<OpaqueType>(Ty)) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000721 Out << "opaque";
Chris Lattnerd816b532002-04-13 20:53:41 +0000722 } else {
Vikram S. Adveb952b542002-07-14 23:14:45 +0000723 if (!Ty->isPrimitiveType())
Misha Brukmana6619a92004-06-21 21:53:56 +0000724 Out << "<unknown derived type>";
Chris Lattnerd816b532002-04-13 20:53:41 +0000725 printType(Ty);
726 }
Misha Brukmana6619a92004-06-21 21:53:56 +0000727 return Out;
Chris Lattnerd816b532002-04-13 20:53:41 +0000728}
729
730
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000731void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType) {
732 if (Operand == 0) {
Chris Lattner08f7d0c2005-02-24 16:58:29 +0000733 Out << "<null operand!>";
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000734 } else {
735 if (PrintType) { Out << ' '; printType(Operand->getType()); }
736 WriteAsOperandInternal(Out, Operand, TypeNames, &Machine);
Chris Lattner08f7d0c2005-02-24 16:58:29 +0000737 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000738}
739
Chris Lattner2f7c9632001-06-06 20:29:01 +0000740
Chris Lattner7bfee412001-10-29 16:05:51 +0000741void AssemblyWriter::printModule(const Module *M) {
Chris Lattner4d8689e2005-03-02 23:12:40 +0000742 if (!M->getModuleIdentifier().empty() &&
Misha Brukmanb1c93172005-04-21 23:48:37 +0000743 // Don't print the ID if it will start a new line (which would
Chris Lattner4d8689e2005-03-02 23:12:40 +0000744 // require a comment char before it).
745 M->getModuleIdentifier().find('\n') == std::string::npos)
746 Out << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
747
Owen Andersone2237542006-10-18 02:21:12 +0000748 if (!M->getDataLayout().empty())
Chris Lattner04897162006-10-22 06:06:56 +0000749 Out << "target datalayout = \"" << M->getDataLayout() << "\"\n";
Owen Andersone2237542006-10-18 02:21:12 +0000750
Chris Lattner8068e0c2003-08-24 13:48:48 +0000751 switch (M->getEndianness()) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000752 case Module::LittleEndian: Out << "target endian = little\n"; break;
753 case Module::BigEndian: Out << "target endian = big\n"; break;
Chris Lattner8068e0c2003-08-24 13:48:48 +0000754 case Module::AnyEndianness: break;
755 }
756 switch (M->getPointerSize()) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000757 case Module::Pointer32: Out << "target pointersize = 32\n"; break;
758 case Module::Pointer64: Out << "target pointersize = 64\n"; break;
Chris Lattner8068e0c2003-08-24 13:48:48 +0000759 case Module::AnyPointerSize: break;
760 }
Reid Spencer48f98c82004-07-25 21:44:54 +0000761 if (!M->getTargetTriple().empty())
Reid Spencerffec7df2004-07-25 21:29:43 +0000762 Out << "target triple = \"" << M->getTargetTriple() << "\"\n";
Misha Brukmanb1c93172005-04-21 23:48:37 +0000763
Chris Lattnereef2fe72006-01-24 04:13:11 +0000764 if (!M->getModuleInlineAsm().empty()) {
Chris Lattnerefaf35d2006-01-24 00:45:30 +0000765 // Split the string into lines, to make it easier to read the .ll file.
Chris Lattnereef2fe72006-01-24 04:13:11 +0000766 std::string Asm = M->getModuleInlineAsm();
Chris Lattnerefaf35d2006-01-24 00:45:30 +0000767 size_t CurPos = 0;
768 size_t NewLine = Asm.find_first_of('\n', CurPos);
769 while (NewLine != std::string::npos) {
770 // We found a newline, print the portion of the asm string from the
771 // last newline up to this newline.
772 Out << "module asm \"";
773 PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.begin()+NewLine),
774 Out);
775 Out << "\"\n";
776 CurPos = NewLine+1;
777 NewLine = Asm.find_first_of('\n', CurPos);
778 }
Chris Lattner3acaf5c2006-01-24 00:40:17 +0000779 Out << "module asm \"";
Chris Lattnerefaf35d2006-01-24 00:45:30 +0000780 PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.end()), Out);
Chris Lattner6ed87bd2006-01-23 23:03:36 +0000781 Out << "\"\n";
782 }
783
Chris Lattner2cdd49d2004-09-14 05:06:58 +0000784 // Loop over the dependent libraries and emit them.
Chris Lattner730cfe42004-09-14 04:51:44 +0000785 Module::lib_iterator LI = M->lib_begin();
786 Module::lib_iterator LE = M->lib_end();
Reid Spencer48f98c82004-07-25 21:44:54 +0000787 if (LI != LE) {
Chris Lattner730cfe42004-09-14 04:51:44 +0000788 Out << "deplibs = [ ";
789 while (LI != LE) {
Chris Lattner2cdd49d2004-09-14 05:06:58 +0000790 Out << '"' << *LI << '"';
Reid Spencerffec7df2004-07-25 21:29:43 +0000791 ++LI;
Chris Lattner730cfe42004-09-14 04:51:44 +0000792 if (LI != LE)
793 Out << ", ";
Reid Spencerffec7df2004-07-25 21:29:43 +0000794 }
795 Out << " ]\n";
Reid Spencercc5ff642004-07-25 18:08:18 +0000796 }
Reid Spencerb9e08772004-09-13 23:44:23 +0000797
Chris Lattner2cdd49d2004-09-14 05:06:58 +0000798 // Loop over the symbol table, emitting all named constants.
Chris Lattner98cf1f52002-11-20 18:36:02 +0000799 printSymbolTable(M->getSymbolTable());
Misha Brukmanb1c93172005-04-21 23:48:37 +0000800
Chris Lattner54932b02006-12-06 04:41:52 +0000801 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
802 I != E; ++I)
Chris Lattner113f4f42002-06-25 16:13:24 +0000803 printGlobal(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000804
Misha Brukmana6619a92004-06-21 21:53:56 +0000805 Out << "\nimplementation ; Functions:\n";
Misha Brukmanb1c93172005-04-21 23:48:37 +0000806
Chris Lattner2cdd49d2004-09-14 05:06:58 +0000807 // Output all of the functions.
Chris Lattner113f4f42002-06-25 16:13:24 +0000808 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
809 printFunction(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000810}
811
Chris Lattner7bfee412001-10-29 16:05:51 +0000812void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000813 if (GV->hasName()) Out << getLLVMName(GV->getName()) << " = ";
Chris Lattner37798642001-09-18 04:01:05 +0000814
Misha Brukmanb1c93172005-04-21 23:48:37 +0000815 if (!GV->hasInitializer())
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000816 switch (GV->getLinkage()) {
817 case GlobalValue::DLLImportLinkage: Out << "dllimport "; break;
818 case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break;
819 default: Out << "external "; break;
820 }
Chris Lattner379a8d22003-04-16 20:28:45 +0000821 else
822 switch (GV->getLinkage()) {
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000823 case GlobalValue::InternalLinkage: Out << "internal "; break;
824 case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
825 case GlobalValue::WeakLinkage: Out << "weak "; break;
826 case GlobalValue::AppendingLinkage: Out << "appending "; break;
827 case GlobalValue::DLLImportLinkage: Out << "dllimport "; break;
828 case GlobalValue::DLLExportLinkage: Out << "dllexport "; break;
829 case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break;
830 case GlobalValue::ExternalLinkage: break;
Misha Brukman70457632004-11-14 21:04:34 +0000831 case GlobalValue::GhostLinkage:
Bill Wendlingf3baad32006-12-07 01:30:32 +0000832 cerr << "GhostLinkage not allowed in AsmWriter!\n";
Misha Brukman70457632004-11-14 21:04:34 +0000833 abort();
Chris Lattner379a8d22003-04-16 20:28:45 +0000834 }
Chris Lattner37798642001-09-18 04:01:05 +0000835
Misha Brukmana6619a92004-06-21 21:53:56 +0000836 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattner2413b162001-12-04 00:03:30 +0000837 printType(GV->getType()->getElementType());
Chris Lattner37798642001-09-18 04:01:05 +0000838
Reid Spenceraccd7c72004-07-17 23:47:01 +0000839 if (GV->hasInitializer()) {
840 Constant* C = cast<Constant>(GV->getInitializer());
841 assert(C && "GlobalVar initializer isn't constant?");
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000842 writeOperand(GV->getInitializer(), false);
Reid Spenceraccd7c72004-07-17 23:47:01 +0000843 }
Chris Lattnerf8a974d2005-11-06 06:48:53 +0000844
Chris Lattner4b96c542005-11-12 00:10:19 +0000845 if (GV->hasSection())
846 Out << ", section \"" << GV->getSection() << '"';
847 if (GV->getAlignment())
Chris Lattnerf8a974d2005-11-06 06:48:53 +0000848 Out << ", align " << GV->getAlignment();
Chris Lattner4b96c542005-11-12 00:10:19 +0000849
Chris Lattner113f4f42002-06-25 16:13:24 +0000850 printInfoComment(*GV);
Misha Brukmana6619a92004-06-21 21:53:56 +0000851 Out << "\n";
Chris Lattnerda975502001-09-10 07:58:01 +0000852}
853
Chris Lattnerfee714f2001-09-07 16:36:04 +0000854
Reid Spencere7e96712004-05-25 08:53:40 +0000855// printSymbolTable - Run through symbol table looking for constants
856// and types. Emit their declarations.
Chris Lattner7bfee412001-10-29 16:05:51 +0000857void AssemblyWriter::printSymbolTable(const SymbolTable &ST) {
Reid Spencere7e96712004-05-25 08:53:40 +0000858
859 // Print the types.
860 for (SymbolTable::type_const_iterator TI = ST.type_begin();
Chris Lattner23e829a2006-12-06 05:12:21 +0000861 TI != ST.type_end(); ++TI) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000862 Out << "\t" << getLLVMName(TI->first) << " = type ";
Reid Spencere7e96712004-05-25 08:53:40 +0000863
864 // Make sure we print out at least one level of the type structure, so
865 // that we do not get %FILE = type %FILE
866 //
867 printTypeAtLeastOneLevel(TI->second) << "\n";
868 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000869
Reid Spencere7e96712004-05-25 08:53:40 +0000870 // Print the constants, in type plane order.
871 for (SymbolTable::plane_const_iterator PI = ST.plane_begin();
Chris Lattner23e829a2006-12-06 05:12:21 +0000872 PI != ST.plane_end(); ++PI) {
Reid Spencere7e96712004-05-25 08:53:40 +0000873 SymbolTable::value_const_iterator VI = ST.value_begin(PI->first);
874 SymbolTable::value_const_iterator VE = ST.value_end(PI->first);
875
876 for (; VI != VE; ++VI) {
Reid Spenceraccd7c72004-07-17 23:47:01 +0000877 const Value* V = VI->second;
878 const Constant *CPV = dyn_cast<Constant>(V) ;
879 if (CPV && !isa<GlobalValue>(V)) {
Reid Spencer56010e42004-05-26 21:56:09 +0000880 printConstant(CPV);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000881 }
882 }
Chris Lattnera7620d92001-07-15 06:35:59 +0000883 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000884}
885
886
Misha Brukmanc566ca362004-03-02 00:22:19 +0000887/// printConstant - Print out a constant pool entry...
888///
Chris Lattner3462ae32001-12-03 22:26:30 +0000889void AssemblyWriter::printConstant(const Constant *CPV) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000890 // Don't print out unnamed constants, they will be inlined
891 if (!CPV->hasName()) return;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000892
Chris Lattneree998be2001-07-26 16:29:38 +0000893 // Print out name...
Misha Brukmana6619a92004-06-21 21:53:56 +0000894 Out << "\t" << getLLVMName(CPV->getName()) << " =";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000895
Chris Lattner78e2e8b2006-12-06 06:24:27 +0000896 // Write the value out now.
897 writeOperand(CPV, true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000898
Chris Lattner113f4f42002-06-25 16:13:24 +0000899 printInfoComment(*CPV);
Misha Brukmana6619a92004-06-21 21:53:56 +0000900 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000901}
902
Misha Brukmanc566ca362004-03-02 00:22:19 +0000903/// printFunction - Print all aspects of a function.
904///
Chris Lattner113f4f42002-06-25 16:13:24 +0000905void AssemblyWriter::printFunction(const Function *F) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000906 // Print out the return type and name...
Misha Brukmana6619a92004-06-21 21:53:56 +0000907 Out << "\n";
Chris Lattner379a8d22003-04-16 20:28:45 +0000908
Chris Lattner97e36f22004-12-05 06:44:09 +0000909 // Ensure that no local symbols conflict with global symbols.
910 const_cast<Function*>(F)->renameLocalSymbols();
911
Misha Brukmana6619a92004-06-21 21:53:56 +0000912 if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +0000913
Chris Lattner379a8d22003-04-16 20:28:45 +0000914 if (F->isExternal())
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000915 switch (F->getLinkage()) {
916 case GlobalValue::DLLImportLinkage: Out << "declare dllimport "; break;
917 case GlobalValue::ExternalWeakLinkage: Out << "declare extern_weak "; break;
918 default: Out << "declare ";
919 }
Chris Lattner379a8d22003-04-16 20:28:45 +0000920 else
921 switch (F->getLinkage()) {
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000922 case GlobalValue::InternalLinkage: Out << "internal "; break;
923 case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
924 case GlobalValue::WeakLinkage: Out << "weak "; break;
925 case GlobalValue::AppendingLinkage: Out << "appending "; break;
926 case GlobalValue::DLLImportLinkage: Out << "dllimport "; break;
927 case GlobalValue::DLLExportLinkage: Out << "dllexport "; break;
928 case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break;
Chris Lattner379a8d22003-04-16 20:28:45 +0000929 case GlobalValue::ExternalLinkage: break;
Misha Brukman70457632004-11-14 21:04:34 +0000930 case GlobalValue::GhostLinkage:
Bill Wendlingf3baad32006-12-07 01:30:32 +0000931 cerr << "GhostLinkage not allowed in AsmWriter!\n";
Misha Brukman70457632004-11-14 21:04:34 +0000932 abort();
Chris Lattner379a8d22003-04-16 20:28:45 +0000933 }
934
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000935 // Print the calling convention.
936 switch (F->getCallingConv()) {
937 case CallingConv::C: break; // default
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +0000938 case CallingConv::CSRet: Out << "csretcc "; break;
939 case CallingConv::Fast: Out << "fastcc "; break;
940 case CallingConv::Cold: Out << "coldcc "; break;
941 case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break;
942 case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break;
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000943 default: Out << "cc" << F->getCallingConv() << " "; break;
944 }
945
Misha Brukman21bbdb92004-06-04 21:11:51 +0000946 printType(F->getReturnType()) << ' ';
Chris Lattner5b337482003-10-18 05:57:43 +0000947 if (!F->getName().empty())
Misha Brukmana6619a92004-06-21 21:53:56 +0000948 Out << getLLVMName(F->getName());
Chris Lattner5b337482003-10-18 05:57:43 +0000949 else
Misha Brukmana6619a92004-06-21 21:53:56 +0000950 Out << "\"\"";
951 Out << '(';
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000952 Machine.incorporateFunction(F);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000953
Chris Lattner7bfee412001-10-29 16:05:51 +0000954 // Loop over the arguments, printing them...
Chris Lattner113f4f42002-06-25 16:13:24 +0000955 const FunctionType *FT = F->getFunctionType();
Chris Lattnerfee714f2001-09-07 16:36:04 +0000956
Chris Lattner54932b02006-12-06 04:41:52 +0000957 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
958 I != E; ++I)
Chris Lattner149376d2002-10-13 20:57:00 +0000959 printArgument(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000960
961 // Finish printing arguments...
Chris Lattner113f4f42002-06-25 16:13:24 +0000962 if (FT->isVarArg()) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000963 if (FT->getNumParams()) Out << ", ";
964 Out << "..."; // Output varargs portion of signature!
Chris Lattnerfee714f2001-09-07 16:36:04 +0000965 }
Misha Brukmana6619a92004-06-21 21:53:56 +0000966 Out << ')';
Chris Lattnerfee714f2001-09-07 16:36:04 +0000967
Chris Lattner4b96c542005-11-12 00:10:19 +0000968 if (F->hasSection())
969 Out << " section \"" << F->getSection() << '"';
Chris Lattnerf8a974d2005-11-06 06:48:53 +0000970 if (F->getAlignment())
971 Out << " align " << F->getAlignment();
Chris Lattner4b96c542005-11-12 00:10:19 +0000972
Chris Lattner113f4f42002-06-25 16:13:24 +0000973 if (F->isExternal()) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000974 Out << "\n";
Chris Lattnerb2f02e52002-05-06 03:00:40 +0000975 } else {
Misha Brukmana6619a92004-06-21 21:53:56 +0000976 Out << " {";
Misha Brukmanb1c93172005-04-21 23:48:37 +0000977
Chris Lattner6915f8f2002-04-07 22:49:37 +0000978 // Output all of its basic blocks... for the function
Chris Lattner113f4f42002-06-25 16:13:24 +0000979 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
980 printBasicBlock(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000981
Misha Brukmana6619a92004-06-21 21:53:56 +0000982 Out << "}\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000983 }
984
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000985 Machine.purgeFunction();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000986}
987
Misha Brukmanc566ca362004-03-02 00:22:19 +0000988/// printArgument - This member is called for every argument that is passed into
989/// the function. Simply print it out
990///
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000991void AssemblyWriter::printArgument(const Argument *Arg) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000992 // Insert commas as we go... the first arg doesn't get a comma
Chris Lattnerfb972ad2005-03-15 05:03:36 +0000993 if (Arg != Arg->getParent()->arg_begin()) Out << ", ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000994
995 // Output type...
Chris Lattner7bfee412001-10-29 16:05:51 +0000996 printType(Arg->getType());
Misha Brukmanb1c93172005-04-21 23:48:37 +0000997
Chris Lattner2f7c9632001-06-06 20:29:01 +0000998 // Output name, if available...
999 if (Arg->hasName())
Misha Brukmana6619a92004-06-21 21:53:56 +00001000 Out << ' ' << getLLVMName(Arg->getName());
Chris Lattner2f7c9632001-06-06 20:29:01 +00001001}
1002
Misha Brukmanc566ca362004-03-02 00:22:19 +00001003/// printBasicBlock - This member is called for each basic block in a method.
1004///
Chris Lattner7bfee412001-10-29 16:05:51 +00001005void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001006 if (BB->hasName()) { // Print out the label if it exists...
Alkis Evlogimenos991d6ad2004-12-10 05:41:10 +00001007 Out << "\n" << getLLVMName(BB->getName(), false) << ':';
Chris Lattner408dbdb2002-05-14 16:02:05 +00001008 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Misha Brukmana6619a92004-06-21 21:53:56 +00001009 Out << "\n; <label>:";
Chris Lattner757ee0b2004-06-09 19:41:19 +00001010 int Slot = Machine.getSlot(BB);
1011 if (Slot != -1)
Misha Brukmana6619a92004-06-21 21:53:56 +00001012 Out << Slot;
Chris Lattner757ee0b2004-06-09 19:41:19 +00001013 else
Misha Brukmana6619a92004-06-21 21:53:56 +00001014 Out << "<badref>";
Chris Lattner58185f22002-10-02 19:38:55 +00001015 }
Chris Lattner2447ef52003-11-20 00:09:43 +00001016
1017 if (BB->getParent() == 0)
Misha Brukmana6619a92004-06-21 21:53:56 +00001018 Out << "\t\t; Error: Block without parent!";
Chris Lattner2447ef52003-11-20 00:09:43 +00001019 else {
1020 if (BB != &BB->getParent()->front()) { // Not the entry block?
1021 // Output predecessors for the block...
Misha Brukmana6619a92004-06-21 21:53:56 +00001022 Out << "\t\t;";
Chris Lattner2447ef52003-11-20 00:09:43 +00001023 pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
Misha Brukmanb1c93172005-04-21 23:48:37 +00001024
Chris Lattner2447ef52003-11-20 00:09:43 +00001025 if (PI == PE) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001026 Out << " No predecessors!";
Chris Lattner2447ef52003-11-20 00:09:43 +00001027 } else {
Misha Brukmana6619a92004-06-21 21:53:56 +00001028 Out << " preds =";
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001029 writeOperand(*PI, false);
Chris Lattner2447ef52003-11-20 00:09:43 +00001030 for (++PI; PI != PE; ++PI) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001031 Out << ',';
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001032 writeOperand(*PI, false);
Chris Lattner2447ef52003-11-20 00:09:43 +00001033 }
Chris Lattner00211f12003-11-16 22:59:57 +00001034 }
Chris Lattner58185f22002-10-02 19:38:55 +00001035 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001036 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001037
Misha Brukmana6619a92004-06-21 21:53:56 +00001038 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001039
Misha Brukmana6619a92004-06-21 21:53:56 +00001040 if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001041
Chris Lattnerfee714f2001-09-07 16:36:04 +00001042 // Output all of the instructions in the basic block...
Chris Lattner113f4f42002-06-25 16:13:24 +00001043 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
1044 printInstruction(*I);
Chris Lattner96cdd272004-03-08 18:51:45 +00001045
Misha Brukmana6619a92004-06-21 21:53:56 +00001046 if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001047}
1048
Chris Lattner862e3382001-10-13 06:42:36 +00001049
Misha Brukmanc566ca362004-03-02 00:22:19 +00001050/// printInfoComment - Print a little comment after the instruction indicating
1051/// which slot it occupies.
1052///
Chris Lattner113f4f42002-06-25 16:13:24 +00001053void AssemblyWriter::printInfoComment(const Value &V) {
1054 if (V.getType() != Type::VoidTy) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001055 Out << "\t\t; <";
Misha Brukman21bbdb92004-06-04 21:11:51 +00001056 printType(V.getType()) << '>';
Chris Lattner862e3382001-10-13 06:42:36 +00001057
Chris Lattner113f4f42002-06-25 16:13:24 +00001058 if (!V.hasName()) {
Chris Lattner757ee0b2004-06-09 19:41:19 +00001059 int SlotNum = Machine.getSlot(&V);
1060 if (SlotNum == -1)
Misha Brukmana6619a92004-06-21 21:53:56 +00001061 Out << ":<badref>";
Reid Spencer8beac692004-06-09 15:26:53 +00001062 else
Misha Brukmana6619a92004-06-21 21:53:56 +00001063 Out << ':' << SlotNum; // Print out the def slot taken.
Chris Lattner862e3382001-10-13 06:42:36 +00001064 }
Chris Lattnerb6c21db2005-02-01 01:24:01 +00001065 Out << " [#uses=" << V.getNumUses() << ']'; // Output # uses
Chris Lattner862e3382001-10-13 06:42:36 +00001066 }
1067}
1068
Reid Spencere7141c82006-08-28 01:02:49 +00001069// This member is called for each Instruction in a function..
Chris Lattner113f4f42002-06-25 16:13:24 +00001070void AssemblyWriter::printInstruction(const Instruction &I) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001071 if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001072
Misha Brukmana6619a92004-06-21 21:53:56 +00001073 Out << "\t";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001074
1075 // Print out name if it exists...
Chris Lattner113f4f42002-06-25 16:13:24 +00001076 if (I.hasName())
Misha Brukmana6619a92004-06-21 21:53:56 +00001077 Out << getLLVMName(I.getName()) << " = ";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001078
Chris Lattner06038452005-05-06 05:51:46 +00001079 // If this is a volatile load or store, print out the volatile marker.
Chris Lattner504f9242003-09-08 17:45:59 +00001080 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
Chris Lattner06038452005-05-06 05:51:46 +00001081 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile())) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001082 Out << "volatile ";
Chris Lattner06038452005-05-06 05:51:46 +00001083 } else if (isa<CallInst>(I) && cast<CallInst>(I).isTailCall()) {
1084 // If this is a call, check if it's a tail call.
1085 Out << "tail ";
1086 }
Chris Lattner504f9242003-09-08 17:45:59 +00001087
Chris Lattner2f7c9632001-06-06 20:29:01 +00001088 // Print out the opcode...
Misha Brukmana6619a92004-06-21 21:53:56 +00001089 Out << I.getOpcodeName();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001090
Reid Spencer45e52392006-12-03 06:27:29 +00001091 // Print out the compare instruction predicates
1092 if (const FCmpInst *FCI = dyn_cast<FCmpInst>(&I)) {
Reid Spencer812a1be2006-12-04 05:19:18 +00001093 Out << " " << getPredicateText(FCI->getPredicate());
Reid Spencer45e52392006-12-03 06:27:29 +00001094 } else if (const ICmpInst *ICI = dyn_cast<ICmpInst>(&I)) {
Reid Spencer812a1be2006-12-04 05:19:18 +00001095 Out << " " << getPredicateText(ICI->getPredicate());
Reid Spencer45e52392006-12-03 06:27:29 +00001096 }
1097
Chris Lattner2f7c9632001-06-06 20:29:01 +00001098 // Print out the type of the operands...
Chris Lattner113f4f42002-06-25 16:13:24 +00001099 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001100
1101 // Special case conditional branches to swizzle the condition out to the front
Chris Lattner113f4f42002-06-25 16:13:24 +00001102 if (isa<BranchInst>(I) && I.getNumOperands() > 1) {
1103 writeOperand(I.getOperand(2), true);
Misha Brukmana6619a92004-06-21 21:53:56 +00001104 Out << ',';
Chris Lattner2f7c9632001-06-06 20:29:01 +00001105 writeOperand(Operand, true);
Misha Brukmana6619a92004-06-21 21:53:56 +00001106 Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001107 writeOperand(I.getOperand(1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001108
Chris Lattner8d48df22002-04-13 18:34:38 +00001109 } else if (isa<SwitchInst>(I)) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001110 // Special case switch statement to get formatting nice and correct...
Misha Brukmana6619a92004-06-21 21:53:56 +00001111 writeOperand(Operand , true); Out << ',';
1112 writeOperand(I.getOperand(1), true); Out << " [";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001113
Chris Lattner113f4f42002-06-25 16:13:24 +00001114 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001115 Out << "\n\t\t";
1116 writeOperand(I.getOperand(op ), true); Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001117 writeOperand(I.getOperand(op+1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001118 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001119 Out << "\n\t]";
Chris Lattnerda558102001-10-02 03:41:24 +00001120 } else if (isa<PHINode>(I)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001121 Out << ' ';
Chris Lattner113f4f42002-06-25 16:13:24 +00001122 printType(I.getType());
Misha Brukmana6619a92004-06-21 21:53:56 +00001123 Out << ' ';
Chris Lattner2f7c9632001-06-06 20:29:01 +00001124
Chris Lattner113f4f42002-06-25 16:13:24 +00001125 for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001126 if (op) Out << ", ";
Misha Brukmanb1c93172005-04-21 23:48:37 +00001127 Out << '[';
Misha Brukmana6619a92004-06-21 21:53:56 +00001128 writeOperand(I.getOperand(op ), false); Out << ',';
1129 writeOperand(I.getOperand(op+1), false); Out << " ]";
Chris Lattner931ef3b2001-06-11 15:04:20 +00001130 }
Chris Lattner862e3382001-10-13 06:42:36 +00001131 } else if (isa<ReturnInst>(I) && !Operand) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001132 Out << " void";
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001133 } else if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
1134 // Print the calling convention being used.
1135 switch (CI->getCallingConv()) {
1136 case CallingConv::C: break; // default
Chris Lattner29d20852006-05-19 21:58:52 +00001137 case CallingConv::CSRet: Out << " csretcc"; break;
1138 case CallingConv::Fast: Out << " fastcc"; break;
1139 case CallingConv::Cold: Out << " coldcc"; break;
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +00001140 case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break;
1141 case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break;
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001142 default: Out << " cc" << CI->getCallingConv(); break;
1143 }
1144
Chris Lattner463d6a52003-08-05 15:34:45 +00001145 const PointerType *PTy = cast<PointerType>(Operand->getType());
1146 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1147 const Type *RetTy = FTy->getReturnType();
Chris Lattner2f2d9472001-11-06 21:28:12 +00001148
Chris Lattner463d6a52003-08-05 15:34:45 +00001149 // If possible, print out the short form of the call instruction. We can
Chris Lattner6915f8f2002-04-07 22:49:37 +00001150 // only do this if the first argument is a pointer to a nonvararg function,
Chris Lattner463d6a52003-08-05 15:34:45 +00001151 // and if the return type is not a pointer to a function.
Chris Lattner2f2d9472001-11-06 21:28:12 +00001152 //
Chris Lattner463d6a52003-08-05 15:34:45 +00001153 if (!FTy->isVarArg() &&
Misha Brukmanb1c93172005-04-21 23:48:37 +00001154 (!isa<PointerType>(RetTy) ||
Chris Lattnerd9a36a62002-07-25 20:58:51 +00001155 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001156 Out << ' '; printType(RetTy);
Chris Lattner2f2d9472001-11-06 21:28:12 +00001157 writeOperand(Operand, false);
1158 } else {
1159 writeOperand(Operand, true);
1160 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001161 Out << '(';
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001162 if (CI->getNumOperands() > 1) writeOperand(CI->getOperand(1), true);
Chris Lattner113f4f42002-06-25 16:13:24 +00001163 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; ++op) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001164 Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001165 writeOperand(I.getOperand(op), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001166 }
1167
Misha Brukmana6619a92004-06-21 21:53:56 +00001168 Out << " )";
Chris Lattner113f4f42002-06-25 16:13:24 +00001169 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Chris Lattner463d6a52003-08-05 15:34:45 +00001170 const PointerType *PTy = cast<PointerType>(Operand->getType());
1171 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1172 const Type *RetTy = FTy->getReturnType();
1173
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001174 // Print the calling convention being used.
1175 switch (II->getCallingConv()) {
1176 case CallingConv::C: break; // default
Chris Lattner29d20852006-05-19 21:58:52 +00001177 case CallingConv::CSRet: Out << " csretcc"; break;
1178 case CallingConv::Fast: Out << " fastcc"; break;
1179 case CallingConv::Cold: Out << " coldcc"; break;
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +00001180 case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break;
1181 case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break;
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001182 default: Out << " cc" << II->getCallingConv(); break;
1183 }
1184
Chris Lattner463d6a52003-08-05 15:34:45 +00001185 // If possible, print out the short form of the invoke instruction. We can
1186 // only do this if the first argument is a pointer to a nonvararg function,
1187 // and if the return type is not a pointer to a function.
1188 //
1189 if (!FTy->isVarArg() &&
Misha Brukmanb1c93172005-04-21 23:48:37 +00001190 (!isa<PointerType>(RetTy) ||
Chris Lattner463d6a52003-08-05 15:34:45 +00001191 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001192 Out << ' '; printType(RetTy);
Chris Lattner463d6a52003-08-05 15:34:45 +00001193 writeOperand(Operand, false);
1194 } else {
1195 writeOperand(Operand, true);
1196 }
1197
Misha Brukmana6619a92004-06-21 21:53:56 +00001198 Out << '(';
Chris Lattner113f4f42002-06-25 16:13:24 +00001199 if (I.getNumOperands() > 3) writeOperand(I.getOperand(3), true);
1200 for (unsigned op = 4, Eop = I.getNumOperands(); op < Eop; ++op) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001201 Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001202 writeOperand(I.getOperand(op), true);
Chris Lattner862e3382001-10-13 06:42:36 +00001203 }
1204
Misha Brukmana6619a92004-06-21 21:53:56 +00001205 Out << " )\n\t\t\tto";
Chris Lattner862e3382001-10-13 06:42:36 +00001206 writeOperand(II->getNormalDest(), true);
Misha Brukmana6619a92004-06-21 21:53:56 +00001207 Out << " unwind";
Chris Lattnerfae8ab32004-02-08 21:44:31 +00001208 writeOperand(II->getUnwindDest(), true);
Chris Lattner862e3382001-10-13 06:42:36 +00001209
Chris Lattner113f4f42002-06-25 16:13:24 +00001210 } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(&I)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001211 Out << ' ';
Chris Lattner8d48df22002-04-13 18:34:38 +00001212 printType(AI->getType()->getElementType());
1213 if (AI->isArrayAllocation()) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001214 Out << ',';
Chris Lattner8d48df22002-04-13 18:34:38 +00001215 writeOperand(AI->getArraySize(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001216 }
Nate Begeman848622f2005-11-05 09:21:28 +00001217 if (AI->getAlignment()) {
Chris Lattner7aeee3a2005-11-05 21:20:34 +00001218 Out << ", align " << AI->getAlignment();
Nate Begeman848622f2005-11-05 09:21:28 +00001219 }
Chris Lattner862e3382001-10-13 06:42:36 +00001220 } else if (isa<CastInst>(I)) {
Chris Lattnerc96e96b2003-11-17 01:17:04 +00001221 if (Operand) writeOperand(Operand, true); // Work with broken code
Misha Brukmana6619a92004-06-21 21:53:56 +00001222 Out << " to ";
Chris Lattner113f4f42002-06-25 16:13:24 +00001223 printType(I.getType());
Chris Lattner5b337482003-10-18 05:57:43 +00001224 } else if (isa<VAArgInst>(I)) {
Chris Lattnerc96e96b2003-11-17 01:17:04 +00001225 if (Operand) writeOperand(Operand, true); // Work with broken code
Misha Brukmana6619a92004-06-21 21:53:56 +00001226 Out << ", ";
Chris Lattnerf70da102003-05-08 02:44:12 +00001227 printType(I.getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +00001228 } else if (Operand) { // Print the normal way...
1229
Misha Brukmanb1c93172005-04-21 23:48:37 +00001230 // PrintAllTypes - Instructions who have operands of all the same type
Chris Lattner2f7c9632001-06-06 20:29:01 +00001231 // omit the type from all but the first operand. If the instruction has
1232 // different type operands (for example br), then they are all printed.
1233 bool PrintAllTypes = false;
1234 const Type *TheType = Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001235
Chris Lattner52bd5cb92004-03-12 05:53:14 +00001236 // Shift Left & Right print both types even for Ubyte LHS, and select prints
1237 // types even if all operands are bools.
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001238 if (isa<ShiftInst>(I) || isa<SelectInst>(I) || isa<StoreInst>(I) ||
1239 isa<ShuffleVectorInst>(I)) {
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00001240 PrintAllTypes = true;
1241 } else {
1242 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
1243 Operand = I.getOperand(i);
1244 if (Operand->getType() != TheType) {
1245 PrintAllTypes = true; // We have differing types! Print them all!
1246 break;
1247 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001248 }
1249 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001250
Chris Lattner7bfee412001-10-29 16:05:51 +00001251 if (!PrintAllTypes) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001252 Out << ' ';
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00001253 printType(TheType);
Chris Lattner7bfee412001-10-29 16:05:51 +00001254 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001255
Chris Lattner113f4f42002-06-25 16:13:24 +00001256 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001257 if (i) Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001258 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001259 }
1260 }
1261
Chris Lattner862e3382001-10-13 06:42:36 +00001262 printInfoComment(I);
Misha Brukmana6619a92004-06-21 21:53:56 +00001263 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001264}
1265
1266
1267//===----------------------------------------------------------------------===//
1268// External Interface declarations
1269//===----------------------------------------------------------------------===//
1270
Chris Lattner8339f7d2003-10-30 23:41:03 +00001271void Module::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001272 SlotMachine SlotTable(this);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001273 AssemblyWriter W(o, SlotTable, this, AAW);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001274 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001275}
1276
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001277void GlobalVariable::print(std::ostream &o) const {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001278 SlotMachine SlotTable(getParent());
Chris Lattner8339f7d2003-10-30 23:41:03 +00001279 AssemblyWriter W(o, SlotTable, getParent(), 0);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001280 W.write(this);
Chris Lattneradfe0d12001-09-10 20:08:19 +00001281}
1282
Chris Lattner8339f7d2003-10-30 23:41:03 +00001283void Function::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001284 SlotMachine SlotTable(getParent());
Chris Lattner8339f7d2003-10-30 23:41:03 +00001285 AssemblyWriter W(o, SlotTable, getParent(), AAW);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001286
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001287 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001288}
1289
Chris Lattnereef2fe72006-01-24 04:13:11 +00001290void InlineAsm::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001291 WriteAsOperand(o, this, true, 0);
Chris Lattnereef2fe72006-01-24 04:13:11 +00001292}
1293
Chris Lattner8339f7d2003-10-30 23:41:03 +00001294void BasicBlock::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001295 SlotMachine SlotTable(getParent());
Misha Brukmanb1c93172005-04-21 23:48:37 +00001296 AssemblyWriter W(o, SlotTable,
Chris Lattner8339f7d2003-10-30 23:41:03 +00001297 getParent() ? getParent()->getParent() : 0, AAW);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001298 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001299}
1300
Chris Lattner8339f7d2003-10-30 23:41:03 +00001301void Instruction::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001302 const Function *F = getParent() ? getParent()->getParent() : 0;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001303 SlotMachine SlotTable(F);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001304 AssemblyWriter W(o, SlotTable, F ? F->getParent() : 0, AAW);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001305
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001306 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001307}
Chris Lattner7db79582001-11-07 04:21:57 +00001308
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001309void Constant::print(std::ostream &o) const {
1310 if (this == 0) { o << "<null> constant value\n"; return; }
Chris Lattner7d734802002-09-10 15:53:49 +00001311
Misha Brukman21bbdb92004-06-04 21:11:51 +00001312 o << ' ' << getType()->getDescription() << ' ';
Evan Cheng5b19a802006-03-01 22:17:00 +00001313
1314 std::map<const Type *, std::string> TypeTable;
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001315 WriteConstantInt(o, this, TypeTable, 0);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001316}
1317
Misha Brukmanb1c93172005-04-21 23:48:37 +00001318void Type::print(std::ostream &o) const {
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001319 if (this == 0)
1320 o << "<null Type>";
1321 else
1322 o << getDescription();
1323}
1324
Chris Lattner2e9fa6d2002-04-09 19:48:49 +00001325void Argument::print(std::ostream &o) const {
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001326 WriteAsOperand(o, this, true, getParent() ? getParent()->getParent() : 0);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001327}
1328
Reid Spencer52641832004-05-25 18:14:38 +00001329// Value::dump - allow easy printing of Values from the debugger.
1330// Located here because so much of the needed functionality is here.
Bill Wendlingf3baad32006-12-07 01:30:32 +00001331void Value::dump() const { print(std::cerr); cerr << '\n'; }
Reid Spencer52641832004-05-25 18:14:38 +00001332
1333// Type::dump - allow easy printing of Values from the debugger.
1334// Located here because so much of the needed functionality is here.
Bill Wendlingf3baad32006-12-07 01:30:32 +00001335void Type::dump() const { print(std::cerr); cerr << '\n'; }
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001336
1337//===----------------------------------------------------------------------===//
Chris Lattnerfc9f1c92006-12-06 06:40:49 +00001338// SlotMachine Implementation
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001339//===----------------------------------------------------------------------===//
1340
1341#if 0
Bill Wendlingf3baad32006-12-07 01:30:32 +00001342#define SC_DEBUG(X) cerr << X
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001343#else
1344#define SC_DEBUG(X)
1345#endif
1346
1347// Module level constructor. Causes the contents of the Module (sans functions)
1348// to be added to the slot table.
Misha Brukmanb1c93172005-04-21 23:48:37 +00001349SlotMachine::SlotMachine(const Module *M)
Reid Spencer56010e42004-05-26 21:56:09 +00001350 : TheModule(M) ///< Saved for lazy initialization.
1351 , TheFunction(0)
Reid Spencerb0ac8c42004-08-16 07:46:33 +00001352 , FunctionProcessed(false)
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001353{
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001354}
1355
1356// Function level constructor. Causes the contents of the Module and the one
1357// function provided to be added to the slot table.
Chris Lattner23e829a2006-12-06 05:12:21 +00001358SlotMachine::SlotMachine(const Function *F)
1359 : TheModule(F ? F->getParent() : 0) ///< Saved for lazy initialization
Reid Spencer56010e42004-05-26 21:56:09 +00001360 , TheFunction(F) ///< Saved for lazy initialization
Reid Spencerb0ac8c42004-08-16 07:46:33 +00001361 , FunctionProcessed(false)
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001362{
Reid Spencer56010e42004-05-26 21:56:09 +00001363}
1364
1365inline void SlotMachine::initialize(void) {
Chris Lattner23e829a2006-12-06 05:12:21 +00001366 if (TheModule) {
Misha Brukmanb1c93172005-04-21 23:48:37 +00001367 processModule();
Reid Spencer56010e42004-05-26 21:56:09 +00001368 TheModule = 0; ///< Prevent re-processing next time we're called.
1369 }
Chris Lattner193de902006-12-06 05:27:40 +00001370 if (TheFunction && !FunctionProcessed)
Misha Brukmanb1c93172005-04-21 23:48:37 +00001371 processFunction();
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001372}
1373
1374// Iterate through all the global variables, functions, and global
Misha Brukmanb1c93172005-04-21 23:48:37 +00001375// variable initializers and create slots for them.
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001376void SlotMachine::processModule() {
1377 SC_DEBUG("begin processModule!\n");
1378
Chris Lattner0dda8612006-12-06 06:15:43 +00001379 // Add all of the unnamed global variables to the value table.
Chris Lattner54932b02006-12-06 04:41:52 +00001380 for (Module::const_global_iterator I = TheModule->global_begin(),
1381 E = TheModule->global_end(); I != E; ++I)
Chris Lattner0dda8612006-12-06 06:15:43 +00001382 if (!I->hasName())
1383 getOrCreateSlot(I);
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001384
Chris Lattner0dda8612006-12-06 06:15:43 +00001385 // Add all the unnamed functions to the table.
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001386 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
1387 I != E; ++I)
Chris Lattner0dda8612006-12-06 06:15:43 +00001388 if (!I->hasName())
1389 getOrCreateSlot(I);
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001390
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001391 SC_DEBUG("end processModule!\n");
1392}
1393
1394
Reid Spencer56010e42004-05-26 21:56:09 +00001395// Process the arguments, basic blocks, and instructions of a function.
1396void SlotMachine::processFunction() {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001397 SC_DEBUG("begin processFunction!\n");
1398
Chris Lattner0dda8612006-12-06 06:15:43 +00001399 // Add all the function arguments with no names.
Misha Brukmanb1c93172005-04-21 23:48:37 +00001400 for(Function::const_arg_iterator AI = TheFunction->arg_begin(),
Chris Lattner531f9e92005-03-15 04:54:21 +00001401 AE = TheFunction->arg_end(); AI != AE; ++AI)
Chris Lattner0dda8612006-12-06 06:15:43 +00001402 if (!AI->hasName())
1403 getOrCreateSlot(AI);
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001404
1405 SC_DEBUG("Inserting Instructions:\n");
1406
Chris Lattner0dda8612006-12-06 06:15:43 +00001407 // Add all of the basic blocks and instructions with no names.
Misha Brukmanb1c93172005-04-21 23:48:37 +00001408 for (Function::const_iterator BB = TheFunction->begin(),
Reid Spencer56010e42004-05-26 21:56:09 +00001409 E = TheFunction->end(); BB != E; ++BB) {
Chris Lattner0dda8612006-12-06 06:15:43 +00001410 if (!BB->hasName())
1411 getOrCreateSlot(BB);
1412 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
1413 if (I->getType() != Type::VoidTy && !I->hasName())
1414 getOrCreateSlot(I);
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001415 }
1416
Reid Spencerb0ac8c42004-08-16 07:46:33 +00001417 FunctionProcessed = true;
1418
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001419 SC_DEBUG("end processFunction!\n");
1420}
1421
Chris Lattner193de902006-12-06 05:27:40 +00001422/// Clean up after incorporating a function. This is the only way to get out of
1423/// the function incorporation state that affects the
1424/// getSlot/getOrCreateSlot lock. Function incorporation state is indicated
1425/// by TheFunction != 0.
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001426void SlotMachine::purgeFunction() {
1427 SC_DEBUG("begin purgeFunction!\n");
1428 fMap.clear(); // Simply discard the function level map
Reid Spencer56010e42004-05-26 21:56:09 +00001429 TheFunction = 0;
Reid Spencerb0ac8c42004-08-16 07:46:33 +00001430 FunctionProcessed = false;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001431 SC_DEBUG("end purgeFunction!\n");
1432}
1433
1434/// Get the slot number for a value. This function will assert if you
Chris Lattner193de902006-12-06 05:27:40 +00001435/// ask for a Value that hasn't previously been inserted with getOrCreateSlot.
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001436/// Types are forbidden because Type does not inherit from Value (any more).
Chris Lattner757ee0b2004-06-09 19:41:19 +00001437int SlotMachine::getSlot(const Value *V) {
Chris Lattner23e829a2006-12-06 05:12:21 +00001438 assert(V && "Can't get slot for null Value");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001439 assert(!isa<Constant>(V) || isa<GlobalValue>(V) &&
1440 "Can't insert a non-GlobalValue Constant into SlotMachine");
Reid Spencer56010e42004-05-26 21:56:09 +00001441
1442 // Check for uninitialized state and do lazy initialization
1443 this->initialize();
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001444
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001445 // Get the type of the value
1446 const Type* VTy = V->getType();
1447
1448 // Find the type plane in the module map
1449 TypedPlanes::const_iterator MI = mMap.find(VTy);
1450
Chris Lattner23e829a2006-12-06 05:12:21 +00001451 if (TheFunction) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001452 // Lookup the type in the function map too
1453 TypedPlanes::const_iterator FI = fMap.find(VTy);
1454 // If there is a corresponding type plane in the function map
Chris Lattner23e829a2006-12-06 05:12:21 +00001455 if (FI != fMap.end()) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001456 // Lookup the Value in the function map
1457 ValueMap::const_iterator FVI = FI->second.map.find(V);
1458 // If the value doesn't exist in the function map
Chris Lattner23e829a2006-12-06 05:12:21 +00001459 if (FVI == FI->second.map.end()) {
Chris Lattner68a038e2004-06-09 22:22:10 +00001460 // Look up the value in the module map.
1461 if (MI == mMap.end()) return -1;
Reid Spencer56010e42004-05-26 21:56:09 +00001462 ValueMap::const_iterator MVI = MI->second.map.find(V);
1463 // If we didn't find it, it wasn't inserted
Chris Lattner757ee0b2004-06-09 19:41:19 +00001464 if (MVI == MI->second.map.end()) return -1;
Chris Lattner23e829a2006-12-06 05:12:21 +00001465 assert(MVI != MI->second.map.end() && "Value not found");
Reid Spencer56010e42004-05-26 21:56:09 +00001466 // We found it only at the module level
Misha Brukmanb1c93172005-04-21 23:48:37 +00001467 return MVI->second;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001468
1469 // else the value exists in the function map
1470 } else {
Reid Spencer56010e42004-05-26 21:56:09 +00001471 // Return the slot number as the module's contribution to
1472 // the type plane plus the index in the function's contribution
1473 // to the type plane.
Chris Lattnerb1f04782004-06-15 21:07:32 +00001474 if (MI != mMap.end())
1475 return MI->second.next_slot + FVI->second;
1476 else
1477 return FVI->second;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001478 }
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001479 }
1480 }
1481
Reid Spencer8beac692004-06-09 15:26:53 +00001482 // N.B. Can get here only if either !TheFunction or the function doesn't
1483 // have a corresponding type plane for the Value
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001484
1485 // Make sure the type plane exists
Chris Lattner757ee0b2004-06-09 19:41:19 +00001486 if (MI == mMap.end()) return -1;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001487 // Lookup the value in the module's map
1488 ValueMap::const_iterator MVI = MI->second.map.find(V);
1489 // Make sure we found it.
Chris Lattner757ee0b2004-06-09 19:41:19 +00001490 if (MVI == MI->second.map.end()) return -1;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001491 // Return it.
1492 return MVI->second;
1493}
1494
Reid Spencer58d30f22004-07-04 11:50:43 +00001495
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001496// Create a new slot, or return the existing slot if it is already
1497// inserted. Note that the logic here parallels getSlot but instead
1498// of asserting when the Value* isn't found, it inserts the value.
Chris Lattner193de902006-12-06 05:27:40 +00001499unsigned SlotMachine::getOrCreateSlot(const Value *V) {
Chris Lattner0dda8612006-12-06 06:15:43 +00001500 const Type* VTy = V->getType();
1501 assert(VTy != Type::VoidTy && !V->hasName() && "Doesn't need a slot!");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001502 assert(!isa<Constant>(V) || isa<GlobalValue>(V) &&
1503 "Can't insert a non-GlobalValue Constant into SlotMachine");
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001504
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001505 // Look up the type plane for the Value's type from the module map
1506 TypedPlanes::const_iterator MI = mMap.find(VTy);
1507
Chris Lattner23e829a2006-12-06 05:12:21 +00001508 if (TheFunction) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001509 // Get the type plane for the Value's type from the function map
1510 TypedPlanes::const_iterator FI = fMap.find(VTy);
1511 // If there is a corresponding type plane in the function map
Chris Lattner23e829a2006-12-06 05:12:21 +00001512 if (FI != fMap.end()) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001513 // Lookup the Value in the function map
1514 ValueMap::const_iterator FVI = FI->second.map.find(V);
1515 // If the value doesn't exist in the function map
Chris Lattner23e829a2006-12-06 05:12:21 +00001516 if (FVI == FI->second.map.end()) {
Reid Spencer56010e42004-05-26 21:56:09 +00001517 // If there is no corresponding type plane in the module map
Chris Lattner23e829a2006-12-06 05:12:21 +00001518 if (MI == mMap.end())
Reid Spencer56010e42004-05-26 21:56:09 +00001519 return insertValue(V);
1520 // Look up the value in the module map
1521 ValueMap::const_iterator MVI = MI->second.map.find(V);
1522 // If we didn't find it, it wasn't inserted
Chris Lattner23e829a2006-12-06 05:12:21 +00001523 if (MVI == MI->second.map.end())
Reid Spencer56010e42004-05-26 21:56:09 +00001524 return insertValue(V);
1525 else
1526 // We found it only at the module level
1527 return MVI->second;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001528
1529 // else the value exists in the function map
1530 } else {
Chris Lattner23e829a2006-12-06 05:12:21 +00001531 if (MI == mMap.end())
Reid Spencer56010e42004-05-26 21:56:09 +00001532 return FVI->second;
1533 else
1534 // Return the slot number as the module's contribution to
1535 // the type plane plus the index in the function's contribution
1536 // to the type plane.
1537 return MI->second.next_slot + FVI->second;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001538 }
1539
1540 // else there is not a corresponding type plane in the function map
1541 } else {
1542 // If the type plane doesn't exists at the module level
Chris Lattner23e829a2006-12-06 05:12:21 +00001543 if (MI == mMap.end()) {
Reid Spencer56010e42004-05-26 21:56:09 +00001544 return insertValue(V);
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001545 // else type plane exists at the module level, examine it
1546 } else {
Reid Spencer56010e42004-05-26 21:56:09 +00001547 // Look up the value in the module's map
1548 ValueMap::const_iterator MVI = MI->second.map.find(V);
1549 // If we didn't find it there either
Chris Lattner23e829a2006-12-06 05:12:21 +00001550 if (MVI == MI->second.map.end())
Reid Spencer56010e42004-05-26 21:56:09 +00001551 // Return the slot number as the module's contribution to
1552 // the type plane plus the index of the function map insertion.
1553 return MI->second.next_slot + insertValue(V);
1554 else
1555 return MVI->second;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001556 }
1557 }
1558 }
1559
Chris Lattner0dda8612006-12-06 06:15:43 +00001560 // N.B. Can only get here if TheFunction == 0
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001561
1562 // If the module map's type plane is not for the Value's type
Chris Lattner23e829a2006-12-06 05:12:21 +00001563 if (MI != mMap.end()) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001564 // Lookup the value in the module's map
1565 ValueMap::const_iterator MVI = MI->second.map.find(V);
Chris Lattner23e829a2006-12-06 05:12:21 +00001566 if (MVI != MI->second.map.end())
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001567 return MVI->second;
1568 }
1569
1570 return insertValue(V);
1571}
1572
1573
1574// Low level insert function. Minimal checking is done. This
Chris Lattner193de902006-12-06 05:27:40 +00001575// function is just for the convenience of getOrCreateSlot (above).
Chris Lattner23e829a2006-12-06 05:12:21 +00001576unsigned SlotMachine::insertValue(const Value *V) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001577 assert(V && "Can't insert a null Value into SlotMachine!");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001578 assert(!isa<Constant>(V) || isa<GlobalValue>(V) &&
Chris Lattner7e4e7662006-12-06 05:42:32 +00001579 "Can't insert a non-GlobalValue Constant into SlotMachine");
1580 assert(V->getType() != Type::VoidTy && !V->hasName());
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001581
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001582 const Type *VTy = V->getType();
1583 unsigned DestSlot = 0;
1584
Chris Lattner23e829a2006-12-06 05:12:21 +00001585 if (TheFunction) {
1586 TypedPlanes::iterator I = fMap.find(VTy);
1587 if (I == fMap.end())
Reid Spencer58d30f22004-07-04 11:50:43 +00001588 I = fMap.insert(std::make_pair(VTy,ValuePlane())).first;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001589 DestSlot = I->second.map[V] = I->second.next_slot++;
1590 } else {
Chris Lattner23e829a2006-12-06 05:12:21 +00001591 TypedPlanes::iterator I = mMap.find(VTy);
1592 if (I == mMap.end())
Reid Spencer58d30f22004-07-04 11:50:43 +00001593 I = mMap.insert(std::make_pair(VTy,ValuePlane())).first;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001594 DestSlot = I->second.map[V] = I->second.next_slot++;
1595 }
1596
Misha Brukmanb1c93172005-04-21 23:48:37 +00001597 SC_DEBUG(" Inserting value [" << VTy << "] = " << V << " slot=" <<
Reid Spencer56010e42004-05-26 21:56:09 +00001598 DestSlot << " [");
Chris Lattnerf8090cb2006-12-06 05:55:41 +00001599 // G = Global, F = Function, o = other
1600 SC_DEBUG((isa<GlobalVariable>(V) ? 'G' : (isa<Function>(V) ? 'F' : 'o')));
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001601 SC_DEBUG("]\n");
1602 return DestSlot;
1603}