blob: 487d7e6a05d0c2a5e6f3f04a392d6a893f4079d8 [file] [log] [blame]
Chris Lattner8da78af2002-04-07 22:31:46 +00001//===-- AsmWriter.cpp - Printing LLVM as an assembly file -----------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
10// This library implements the functionality defined in llvm/Assembly/Writer.h
11//
Chris Lattner02b93992002-04-12 18:21:53 +000012// Note that these routines must be extremely tolerant of various errors in the
Chris Lattner8f77dae2003-05-08 02:44:12 +000013// LLVM code, because it can be used for debugging transformations.
Chris Lattner02b93992002-04-12 18:21:53 +000014//
Chris Lattner00950542001-06-06 20:29:01 +000015//===----------------------------------------------------------------------===//
16
Chris Lattner75cf7cf2002-04-08 22:03:40 +000017#include "llvm/Assembly/Writer.h"
Chris Lattnerf082b802002-07-23 18:07:49 +000018#include "llvm/Assembly/PrintModulePass.h"
Chris Lattner95e5a2c2003-10-30 23:41:03 +000019#include "llvm/Assembly/AsmAnnotationWriter.h"
Chris Lattnerd5118982005-05-06 20:26:43 +000020#include "llvm/CallingConv.h"
Chris Lattnerf2d577b2004-01-20 19:50:34 +000021#include "llvm/Constants.h"
Chris Lattner3eb59c02002-04-29 18:46:50 +000022#include "llvm/DerivedTypes.h"
Reid Spencerb138a062007-04-09 06:10:42 +000023#include "llvm/ParameterAttributes.h"
Chris Lattner863517a2006-01-25 18:57:27 +000024#include "llvm/InlineAsm.h"
Vikram S. Adveb4dbb442002-07-14 23:14:45 +000025#include "llvm/Instruction.h"
Misha Brukman44336292004-07-29 16:53:53 +000026#include "llvm/Instructions.h"
Chris Lattnerf2d577b2004-01-20 19:50:34 +000027#include "llvm/Module.h"
Reid Spenceref9b9a72007-02-05 20:47:22 +000028#include "llvm/ValueSymbolTable.h"
Reid Spencer78d033e2007-01-06 07:24:44 +000029#include "llvm/TypeSymbolTable.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000030#include "llvm/ADT/StringExtras.h"
31#include "llvm/ADT/STLExtras.h"
Bill Wendling8f487662006-11-28 02:09:03 +000032#include "llvm/Support/CFG.h"
Jim Laskeycb6682f2005-08-17 19:34:49 +000033#include "llvm/Support/MathExtras.h"
Bill Wendling8f487662006-11-28 02:09:03 +000034#include "llvm/Support/Streams.h"
Chris Lattner007377f2001-09-07 16:36:04 +000035#include <algorithm>
Chris Lattner31f84992003-11-21 20:23:48 +000036using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000037
Chris Lattner0a8e8e12004-07-15 02:51:31 +000038namespace llvm {
Reid Spencer0d1b77e2004-05-26 07:18:52 +000039
Reid Spenceredd5d9e2005-05-15 16:13:11 +000040// Make virtual table appear in this compilation unit.
41AssemblyAnnotationWriter::~AssemblyAnnotationWriter() {}
42
Reid Spencer0d1b77e2004-05-26 07:18:52 +000043/// This class provides computation of slot numbers for LLVM Assembly writing.
44/// @brief LLVM Assembly Writing Slot Computation.
45class SlotMachine {
46
47/// @name Types
48/// @{
49public:
50
51 /// @brief A mapping of Values to slot numbers
Reid Spencer590b3c52007-03-19 18:32:53 +000052 typedef std::map<const Value*,unsigned> ValueMap;
Reid Spencer0d1b77e2004-05-26 07:18:52 +000053
54/// @}
55/// @name Constructors
56/// @{
57public:
58 /// @brief Construct from a module
Chris Lattnerc96ce892006-12-06 05:12:21 +000059 SlotMachine(const Module *M);
Reid Spencer0d1b77e2004-05-26 07:18:52 +000060
61 /// @brief Construct from a function, starting out in incorp state.
Chris Lattnerc96ce892006-12-06 05:12:21 +000062 SlotMachine(const Function *F);
Reid Spencer0d1b77e2004-05-26 07:18:52 +000063
64/// @}
65/// @name Accessors
66/// @{
67public:
68 /// Return the slot number of the specified value in it's type
Chris Lattner22379bc2007-01-11 03:54:27 +000069 /// plane. If something is not in the SlotMachine, return -1.
70 int getLocalSlot(const Value *V);
71 int getGlobalSlot(const GlobalValue *V);
Reid Spencerfc621e22004-06-09 15:26:53 +000072
Reid Spencer0d1b77e2004-05-26 07:18:52 +000073/// @}
74/// @name Mutators
75/// @{
76public:
Misha Brukmanfd939082005-04-21 23:48:37 +000077 /// If you'd like to deal with a function instead of just a module, use
Reid Spencer0d1b77e2004-05-26 07:18:52 +000078 /// this method to get its data into the SlotMachine.
Misha Brukmanfd939082005-04-21 23:48:37 +000079 void incorporateFunction(const Function *F) {
80 TheFunction = F;
Reid Spencer28531c72004-08-16 07:46:33 +000081 FunctionProcessed = false;
82 }
Reid Spencer0d1b77e2004-05-26 07:18:52 +000083
Misha Brukmanfd939082005-04-21 23:48:37 +000084 /// After calling incorporateFunction, use this method to remove the
85 /// most recently incorporated function from the SlotMachine. This
Reid Spencer0d1b77e2004-05-26 07:18:52 +000086 /// will reset the state of the machine back to just the module contents.
87 void purgeFunction();
88
89/// @}
90/// @name Implementation Details
91/// @{
92private:
Reid Spencerb03de0c2004-05-26 21:56:09 +000093 /// This function does the actual initialization.
94 inline void initialize();
95
Chris Lattner9446bbe2007-01-09 07:55:49 +000096 /// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
97 void CreateModuleSlot(const GlobalValue *V);
98
99 /// CreateFunctionSlot - Insert the specified Value* into the slot table.
100 void CreateFunctionSlot(const Value *V);
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000101
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000102 /// Add all of the module level global variables (and their initializers)
103 /// and function declarations, but not the contents of those functions.
104 void processModule();
105
Reid Spencerb03de0c2004-05-26 21:56:09 +0000106 /// Add all of the functions arguments, basic blocks, and instructions
107 void processFunction();
108
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000109 SlotMachine(const SlotMachine &); // DO NOT IMPLEMENT
110 void operator=(const SlotMachine &); // DO NOT IMPLEMENT
111
112/// @}
113/// @name Data
114/// @{
115public:
116
117 /// @brief The module for which we are holding slot numbers
Reid Spencerb03de0c2004-05-26 21:56:09 +0000118 const Module* TheModule;
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000119
Reid Spencerb03de0c2004-05-26 21:56:09 +0000120 /// @brief The function for which we are holding slot numbers
121 const Function* TheFunction;
Reid Spencer28531c72004-08-16 07:46:33 +0000122 bool FunctionProcessed;
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000123
124 /// @brief The TypePlanes map for the module level data
Reid Spencer590b3c52007-03-19 18:32:53 +0000125 ValueMap mMap;
126 unsigned mNext;
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000127
128 /// @brief The TypePlanes map for the function level data
Reid Spencer590b3c52007-03-19 18:32:53 +0000129 ValueMap fMap;
130 unsigned fNext;
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000131
132/// @}
133
134};
135
Chris Lattner0a8e8e12004-07-15 02:51:31 +0000136} // end namespace llvm
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000137
Chris Lattner7f8897f2006-08-27 22:42:52 +0000138static RegisterPass<PrintModulePass>
Chris Lattner3dd965c2006-08-21 17:20:01 +0000139X("printm", "Print module to stderr");
Chris Lattner7f8897f2006-08-27 22:42:52 +0000140static RegisterPass<PrintFunctionPass>
Chris Lattner3dd965c2006-08-21 17:20:01 +0000141Y("print","Print function to stderr");
Chris Lattnerf082b802002-07-23 18:07:49 +0000142
Misha Brukmanfd939082005-04-21 23:48:37 +0000143static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
Chris Lattner919e70c2006-12-06 05:50:41 +0000144 std::map<const Type *, std::string> &TypeTable,
Reid Spencer0e25e1c2004-07-04 11:50:43 +0000145 SlotMachine *Machine);
146
Chris Lattner207b5bc2001-10-29 16:37:48 +0000147static const Module *getModuleFromVal(const Value *V) {
Chris Lattner949a3622003-07-23 15:30:06 +0000148 if (const Argument *MA = dyn_cast<Argument>(V))
Chris Lattner207b5bc2001-10-29 16:37:48 +0000149 return MA->getParent() ? MA->getParent()->getParent() : 0;
Chris Lattner949a3622003-07-23 15:30:06 +0000150 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
Chris Lattner207b5bc2001-10-29 16:37:48 +0000151 return BB->getParent() ? BB->getParent()->getParent() : 0;
Chris Lattner949a3622003-07-23 15:30:06 +0000152 else if (const Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000153 const Function *M = I->getParent() ? I->getParent()->getParent() : 0;
Chris Lattner207b5bc2001-10-29 16:37:48 +0000154 return M ? M->getParent() : 0;
Chris Lattner949a3622003-07-23 15:30:06 +0000155 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
Chris Lattner207b5bc2001-10-29 16:37:48 +0000156 return GV->getParent();
Chris Lattner207b5bc2001-10-29 16:37:48 +0000157 return 0;
158}
159
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000160static SlotMachine *createSlotMachine(const Value *V) {
Chris Lattner949a3622003-07-23 15:30:06 +0000161 if (const Argument *FA = dyn_cast<Argument>(V)) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000162 return new SlotMachine(FA->getParent());
Chris Lattner949a3622003-07-23 15:30:06 +0000163 } else if (const Instruction *I = dyn_cast<Instruction>(V)) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000164 return new SlotMachine(I->getParent()->getParent());
Chris Lattner949a3622003-07-23 15:30:06 +0000165 } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000166 return new SlotMachine(BB->getParent());
Chris Lattner949a3622003-07-23 15:30:06 +0000167 } else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)){
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000168 return new SlotMachine(GV->getParent());
Chris Lattner949a3622003-07-23 15:30:06 +0000169 } else if (const Function *Func = dyn_cast<Function>(V)) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000170 return new SlotMachine(Func);
Chris Lattnerc1824992001-10-29 16:05:51 +0000171 }
172 return 0;
173}
Chris Lattner00950542001-06-06 20:29:01 +0000174
Reid Spencer3702d262007-01-26 08:02:52 +0000175/// NameNeedsQuotes - Return true if the specified llvm name should be wrapped
176/// with ""'s.
177static bool NameNeedsQuotes(const std::string &Name) {
178 if (Name[0] >= '0' && Name[0] <= '9') return true;
Chris Lattner24b8a5d2003-08-22 05:40:38 +0000179 // Scan to see if we have any characters that are not on the "white list"
180 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
181 char C = Name[i];
182 assert(C != '"' && "Illegal character in LLVM value name!");
183 if ((C < 'a' || C > 'z') && (C < 'A' || C > 'Z') && (C < '0' || C > '9') &&
184 C != '-' && C != '.' && C != '_')
Reid Spencer3702d262007-01-26 08:02:52 +0000185 return true;
186 }
187 return false;
188}
189
190enum PrefixType {
191 GlobalPrefix,
192 LabelPrefix,
193 LocalPrefix
194};
195
196/// getLLVMName - Turn the specified string into an 'LLVM name', which is either
197/// prefixed with % (if the string only contains simple characters) or is
198/// surrounded with ""'s (if it has special chars in it).
199static std::string getLLVMName(const std::string &Name, PrefixType Prefix) {
200 assert(!Name.empty() && "Cannot get empty name!");
201
202 // First character cannot start with a number...
203 if (NameNeedsQuotes(Name)) {
204 if (Prefix == GlobalPrefix)
205 return "@\"" + Name + "\"";
206 return "\"" + Name + "\"";
Chris Lattner24b8a5d2003-08-22 05:40:38 +0000207 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000208
Chris Lattner24b8a5d2003-08-22 05:40:38 +0000209 // If we get here, then the identifier is legal to use as a "VarID".
Reid Spencer3702d262007-01-26 08:02:52 +0000210 switch (Prefix) {
211 default: assert(0 && "Bad prefix!");
212 case GlobalPrefix: return '@' + Name;
213 case LabelPrefix: return Name;
214 case LocalPrefix: return '%' + Name;
215 }
Chris Lattner24b8a5d2003-08-22 05:40:38 +0000216}
217
Chris Lattner207b5bc2001-10-29 16:37:48 +0000218
Misha Brukmanab5c6002004-03-02 00:22:19 +0000219/// fillTypeNameTable - If the module has a symbol table, take all global types
220/// and stuff their names into the TypeNames map.
221///
Chris Lattner207b5bc2001-10-29 16:37:48 +0000222static void fillTypeNameTable(const Module *M,
Chris Lattner7b13f562003-05-08 02:08:14 +0000223 std::map<const Type *, std::string> &TypeNames) {
Chris Lattner6e6026b2002-11-20 18:36:02 +0000224 if (!M) return;
Reid Spencer78d033e2007-01-06 07:24:44 +0000225 const TypeSymbolTable &ST = M->getTypeSymbolTable();
226 TypeSymbolTable::const_iterator TI = ST.begin();
227 for (; TI != ST.end(); ++TI) {
Reid Spencer9231ac82004-05-25 08:53:40 +0000228 // As a heuristic, don't insert pointer to primitive types, because
229 // they are used too often to have a single useful name.
230 //
231 const Type *Ty = cast<Type>(TI->second);
232 if (!isa<PointerType>(Ty) ||
Reid Spencerb03de0c2004-05-26 21:56:09 +0000233 !cast<PointerType>(Ty)->getElementType()->isPrimitiveType() ||
Chris Lattner42a75512007-01-15 02:27:26 +0000234 !cast<PointerType>(Ty)->getElementType()->isInteger() ||
Reid Spencerb03de0c2004-05-26 21:56:09 +0000235 isa<OpaqueType>(cast<PointerType>(Ty)->getElementType()))
Reid Spencer3702d262007-01-26 08:02:52 +0000236 TypeNames.insert(std::make_pair(Ty, getLLVMName(TI->first, LocalPrefix)));
Chris Lattner207b5bc2001-10-29 16:37:48 +0000237 }
238}
239
240
241
Misha Brukmanfd939082005-04-21 23:48:37 +0000242static void calcTypeName(const Type *Ty,
John Criswell4ff620a2004-06-01 14:54:08 +0000243 std::vector<const Type *> &TypeStack,
244 std::map<const Type *, std::string> &TypeNames,
245 std::string & Result){
Chris Lattner42a75512007-01-15 02:27:26 +0000246 if (Ty->isInteger() || (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty))) {
John Criswell4ff620a2004-06-01 14:54:08 +0000247 Result += Ty->getDescription(); // Base case
248 return;
249 }
Chris Lattner207b5bc2001-10-29 16:37:48 +0000250
251 // Check to see if the type is named.
Chris Lattner7b13f562003-05-08 02:08:14 +0000252 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
John Criswell4ff620a2004-06-01 14:54:08 +0000253 if (I != TypeNames.end()) {
254 Result += I->second;
255 return;
256 }
Chris Lattner207b5bc2001-10-29 16:37:48 +0000257
John Criswell4ff620a2004-06-01 14:54:08 +0000258 if (isa<OpaqueType>(Ty)) {
259 Result += "opaque";
260 return;
261 }
Chris Lattner88c17382003-10-30 00:22:33 +0000262
Chris Lattner207b5bc2001-10-29 16:37:48 +0000263 // Check to see if the Type is already on the stack...
264 unsigned Slot = 0, CurSize = TypeStack.size();
265 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
266
Misha Brukmanfd939082005-04-21 23:48:37 +0000267 // This is another base case for the recursion. In this case, we know
Chris Lattner207b5bc2001-10-29 16:37:48 +0000268 // that we have looped back to a type that we have previously visited.
269 // Generate the appropriate upreference to handle this.
John Criswell4ff620a2004-06-01 14:54:08 +0000270 if (Slot < CurSize) {
271 Result += "\\" + utostr(CurSize-Slot); // Here's the upreference
272 return;
273 }
Chris Lattner207b5bc2001-10-29 16:37:48 +0000274
275 TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
Misha Brukmanfd939082005-04-21 23:48:37 +0000276
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000277 switch (Ty->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000278 case Type::IntegerTyID: {
279 unsigned BitWidth = cast<IntegerType>(Ty)->getBitWidth();
Reid Spencer15ee9352007-01-12 07:25:20 +0000280 Result += "i" + utostr(BitWidth);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000281 break;
282 }
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000283 case Type::FunctionTyID: {
Chris Lattner949a3622003-07-23 15:30:06 +0000284 const FunctionType *FTy = cast<FunctionType>(Ty);
John Criswell4ff620a2004-06-01 14:54:08 +0000285 calcTypeName(FTy->getReturnType(), TypeStack, TypeNames, Result);
286 Result += " (";
Reid Spencerbd5db8e2006-12-31 05:24:50 +0000287 unsigned Idx = 1;
Reid Spencerb138a062007-04-09 06:10:42 +0000288 const ParamAttrsList *Attrs = FTy->getParamAttrs();
Chris Lattnerd5d89962004-02-09 04:14:01 +0000289 for (FunctionType::param_iterator I = FTy->param_begin(),
290 E = FTy->param_end(); I != E; ++I) {
291 if (I != FTy->param_begin())
Chris Lattner207b5bc2001-10-29 16:37:48 +0000292 Result += ", ";
John Criswell4ff620a2004-06-01 14:54:08 +0000293 calcTypeName(*I, TypeStack, TypeNames, Result);
Reid Spencer18da0722007-04-11 02:44:20 +0000294 if (Attrs && Attrs->getParamAttrs(Idx) != ParamAttr::None) {
Reid Spencerbd5db8e2006-12-31 05:24:50 +0000295 Result += + " ";
Reid Spencerb138a062007-04-09 06:10:42 +0000296 Result += Attrs->getParamAttrsTextByIndex(Idx);
Reid Spencerbd5db8e2006-12-31 05:24:50 +0000297 }
298 Idx++;
Chris Lattner207b5bc2001-10-29 16:37:48 +0000299 }
Chris Lattner2761e9f2002-04-13 20:53:41 +0000300 if (FTy->isVarArg()) {
Chris Lattnerd5d89962004-02-09 04:14:01 +0000301 if (FTy->getNumParams()) Result += ", ";
Chris Lattner207b5bc2001-10-29 16:37:48 +0000302 Result += "...";
303 }
304 Result += ")";
Reid Spencer18da0722007-04-11 02:44:20 +0000305 if (Attrs && Attrs->getParamAttrs(0) != ParamAttr::None) {
Reid Spencer2c261782007-01-05 17:06:19 +0000306 Result += " ";
Reid Spencerb138a062007-04-09 06:10:42 +0000307 Result += Attrs->getParamAttrsTextByIndex(0);
Reid Spencer2c261782007-01-05 17:06:19 +0000308 }
Chris Lattner207b5bc2001-10-29 16:37:48 +0000309 break;
310 }
311 case Type::StructTyID: {
Chris Lattner949a3622003-07-23 15:30:06 +0000312 const StructType *STy = cast<StructType>(Ty);
Andrew Lenharth38ecbf12006-12-08 18:06:16 +0000313 if (STy->isPacked())
314 Result += '<';
John Criswell4ff620a2004-06-01 14:54:08 +0000315 Result += "{ ";
Chris Lattnerd21cd802004-02-09 04:37:31 +0000316 for (StructType::element_iterator I = STy->element_begin(),
317 E = STy->element_end(); I != E; ++I) {
318 if (I != STy->element_begin())
Chris Lattner207b5bc2001-10-29 16:37:48 +0000319 Result += ", ";
John Criswell4ff620a2004-06-01 14:54:08 +0000320 calcTypeName(*I, TypeStack, TypeNames, Result);
Chris Lattner207b5bc2001-10-29 16:37:48 +0000321 }
322 Result += " }";
Andrew Lenharth38ecbf12006-12-08 18:06:16 +0000323 if (STy->isPacked())
324 Result += '>';
Chris Lattner207b5bc2001-10-29 16:37:48 +0000325 break;
326 }
327 case Type::PointerTyID:
Misha Brukmanfd939082005-04-21 23:48:37 +0000328 calcTypeName(cast<PointerType>(Ty)->getElementType(),
John Criswell4ff620a2004-06-01 14:54:08 +0000329 TypeStack, TypeNames, Result);
330 Result += "*";
Chris Lattner207b5bc2001-10-29 16:37:48 +0000331 break;
332 case Type::ArrayTyID: {
Chris Lattner949a3622003-07-23 15:30:06 +0000333 const ArrayType *ATy = cast<ArrayType>(Ty);
John Criswell4ff620a2004-06-01 14:54:08 +0000334 Result += "[" + utostr(ATy->getNumElements()) + " x ";
335 calcTypeName(ATy->getElementType(), TypeStack, TypeNames, Result);
336 Result += "]";
Chris Lattner207b5bc2001-10-29 16:37:48 +0000337 break;
338 }
Reid Spencer9d6565a2007-02-15 02:26:10 +0000339 case Type::VectorTyID: {
340 const VectorType *PTy = cast<VectorType>(Ty);
Brian Gaeke715c90b2004-08-20 06:00:58 +0000341 Result += "<" + utostr(PTy->getNumElements()) + " x ";
342 calcTypeName(PTy->getElementType(), TypeStack, TypeNames, Result);
343 Result += ">";
344 break;
345 }
Chris Lattner9e094c42003-05-14 17:50:47 +0000346 case Type::OpaqueTyID:
John Criswell4ff620a2004-06-01 14:54:08 +0000347 Result += "opaque";
Chris Lattner9e094c42003-05-14 17:50:47 +0000348 break;
Chris Lattner207b5bc2001-10-29 16:37:48 +0000349 default:
John Criswell4ff620a2004-06-01 14:54:08 +0000350 Result += "<unrecognized-type>";
Chris Lattner82c4bc72006-12-06 06:40:49 +0000351 break;
Chris Lattner207b5bc2001-10-29 16:37:48 +0000352 }
353
354 TypeStack.pop_back(); // Remove self from stack...
Chris Lattner207b5bc2001-10-29 16:37:48 +0000355}
356
357
Misha Brukman9d0802e2004-03-01 19:48:13 +0000358/// printTypeInt - The internal guts of printing out a type that has a
359/// potentially named portion.
360///
Chris Lattner7b13f562003-05-08 02:08:14 +0000361static std::ostream &printTypeInt(std::ostream &Out, const Type *Ty,
362 std::map<const Type *, std::string> &TypeNames) {
Chris Lattner207b5bc2001-10-29 16:37:48 +0000363 // Primitive types always print out their description, regardless of whether
364 // they have been named or not.
365 //
Chris Lattner42a75512007-01-15 02:27:26 +0000366 if (Ty->isInteger() || (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty)))
Chris Lattnerdaf2a492003-10-30 00:12:51 +0000367 return Out << Ty->getDescription();
Chris Lattner207b5bc2001-10-29 16:37:48 +0000368
369 // Check to see if the type is named.
Chris Lattner7b13f562003-05-08 02:08:14 +0000370 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
Chris Lattner207b5bc2001-10-29 16:37:48 +0000371 if (I != TypeNames.end()) return Out << I->second;
372
373 // Otherwise we have a type that has not been named but is a derived type.
374 // Carefully recurse the type hierarchy to print out any contained symbolic
375 // names.
376 //
Chris Lattner7b13f562003-05-08 02:08:14 +0000377 std::vector<const Type *> TypeStack;
John Criswell4ff620a2004-06-01 14:54:08 +0000378 std::string TypeName;
379 calcTypeName(Ty, TypeStack, TypeNames, TypeName);
Chris Lattner697954c2002-01-20 22:54:45 +0000380 TypeNames.insert(std::make_pair(Ty, TypeName));//Cache type name for later use
John Criswell4ff620a2004-06-01 14:54:08 +0000381 return (Out << TypeName);
Chris Lattner207b5bc2001-10-29 16:37:48 +0000382}
383
Chris Lattnere51e03b2001-10-31 04:33:19 +0000384
Misha Brukman9d0802e2004-03-01 19:48:13 +0000385/// WriteTypeSymbolic - This attempts to write the specified type as a symbolic
386/// type, iff there is an entry in the modules symbol table for the specified
387/// type or one of it's component types. This is slower than a simple x << Type
388///
Chris Lattner31f84992003-11-21 20:23:48 +0000389std::ostream &llvm::WriteTypeSymbolic(std::ostream &Out, const Type *Ty,
390 const Module *M) {
Misha Brukmanfd939082005-04-21 23:48:37 +0000391 Out << ' ';
Chris Lattner207b5bc2001-10-29 16:37:48 +0000392
Chris Lattner82c4bc72006-12-06 06:40:49 +0000393 // If they want us to print out a type, but there is no context, we can't
394 // print it symbolically.
395 if (!M)
Chris Lattner7b8660d2001-10-29 16:40:32 +0000396 return Out << Ty->getDescription();
Chris Lattner82c4bc72006-12-06 06:40:49 +0000397
398 std::map<const Type *, std::string> TypeNames;
399 fillTypeNameTable(M, TypeNames);
400 return printTypeInt(Out, Ty, TypeNames);
Chris Lattner207b5bc2001-10-29 16:37:48 +0000401}
402
Chris Lattner18365502006-01-23 23:03:36 +0000403// PrintEscapedString - Print each character of the specified string, escaping
404// it if it is not printable or if it is an escape char.
405static void PrintEscapedString(const std::string &Str, std::ostream &Out) {
406 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
407 unsigned char C = Str[i];
408 if (isprint(C) && C != '"' && C != '\\') {
409 Out << C;
410 } else {
411 Out << '\\'
412 << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'))
413 << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
414 }
415 }
416}
417
Chris Lattner82c4bc72006-12-06 06:40:49 +0000418static const char *getPredicateText(unsigned predicate) {
Reid Spencer81dfeb32006-12-04 05:19:18 +0000419 const char * pred = "unknown";
420 switch (predicate) {
421 case FCmpInst::FCMP_FALSE: pred = "false"; break;
422 case FCmpInst::FCMP_OEQ: pred = "oeq"; break;
423 case FCmpInst::FCMP_OGT: pred = "ogt"; break;
424 case FCmpInst::FCMP_OGE: pred = "oge"; break;
425 case FCmpInst::FCMP_OLT: pred = "olt"; break;
426 case FCmpInst::FCMP_OLE: pred = "ole"; break;
427 case FCmpInst::FCMP_ONE: pred = "one"; break;
428 case FCmpInst::FCMP_ORD: pred = "ord"; break;
429 case FCmpInst::FCMP_UNO: pred = "uno"; break;
430 case FCmpInst::FCMP_UEQ: pred = "ueq"; break;
431 case FCmpInst::FCMP_UGT: pred = "ugt"; break;
432 case FCmpInst::FCMP_UGE: pred = "uge"; break;
433 case FCmpInst::FCMP_ULT: pred = "ult"; break;
434 case FCmpInst::FCMP_ULE: pred = "ule"; break;
435 case FCmpInst::FCMP_UNE: pred = "une"; break;
436 case FCmpInst::FCMP_TRUE: pred = "true"; break;
437 case ICmpInst::ICMP_EQ: pred = "eq"; break;
438 case ICmpInst::ICMP_NE: pred = "ne"; break;
439 case ICmpInst::ICMP_SGT: pred = "sgt"; break;
440 case ICmpInst::ICMP_SGE: pred = "sge"; break;
441 case ICmpInst::ICMP_SLT: pred = "slt"; break;
442 case ICmpInst::ICMP_SLE: pred = "sle"; break;
443 case ICmpInst::ICMP_UGT: pred = "ugt"; break;
444 case ICmpInst::ICMP_UGE: pred = "uge"; break;
445 case ICmpInst::ICMP_ULT: pred = "ult"; break;
446 case ICmpInst::ICMP_ULE: pred = "ule"; break;
447 }
448 return pred;
449}
450
Misha Brukmanfd939082005-04-21 23:48:37 +0000451/// @brief Internal constant writer.
452static void WriteConstantInt(std::ostream &Out, const Constant *CV,
Chris Lattner7b13f562003-05-08 02:08:14 +0000453 std::map<const Type *, std::string> &TypeTable,
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000454 SlotMachine *Machine) {
Jim Laskey38a409c2006-02-27 10:33:53 +0000455 const int IndentSize = 4;
456 static std::string Indent = "\n";
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000457 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Reid Spencer4fe16d62007-01-11 18:21:29 +0000458 if (CI->getType() == Type::Int1Ty)
Reid Spencer579dca12007-01-12 04:24:46 +0000459 Out << (CI->getZExtValue() ? "true" : "false");
460 else
Reid Spencer914c3bc2007-02-27 20:25:25 +0000461 Out << CI->getValue().toStringSigned(10);
Chris Lattner66e810b2002-04-18 18:53:13 +0000462 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
463 // We would like to output the FP constant value in exponential notation,
464 // but we cannot do this if doing so will lose precision. Check here to
465 // make sure that we only output it in exponential format if we can parse
466 // the value back and get the same value.
467 //
468 std::string StrVal = ftostr(CFP->getValue());
469
470 // Check to make sure that the stringized number is not some string like
471 // "Inf" or NaN, that atof will accept, but the lexer will not. Check that
472 // the string matches the "[-+]?[0-9]" regex.
473 //
474 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
475 ((StrVal[0] == '-' || StrVal[0] == '+') &&
Brian Gaekeb471a232003-06-17 23:55:35 +0000476 (StrVal[1] >= '0' && StrVal[1] <= '9')))
Chris Lattner66e810b2002-04-18 18:53:13 +0000477 // Reparse stringized version!
478 if (atof(StrVal.c_str()) == CFP->getValue()) {
Chris Lattner71d94d12005-01-04 01:56:57 +0000479 Out << StrVal;
480 return;
Chris Lattner66e810b2002-04-18 18:53:13 +0000481 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000482
Chris Lattner66e810b2002-04-18 18:53:13 +0000483 // Otherwise we could not reparse it to exactly the same value, so we must
484 // output the string in hexadecimal format!
Chris Lattner71d94d12005-01-04 01:56:57 +0000485 assert(sizeof(double) == sizeof(uint64_t) &&
Chris Lattner66e810b2002-04-18 18:53:13 +0000486 "assuming that double is 64 bits!");
Jim Laskeycb6682f2005-08-17 19:34:49 +0000487 Out << "0x" << utohexstr(DoubleToBits(CFP->getValue()));
Chris Lattner66e810b2002-04-18 18:53:13 +0000488
Chris Lattnerde512b52004-02-15 05:55:15 +0000489 } else if (isa<ConstantAggregateZero>(CV)) {
490 Out << "zeroinitializer";
Chris Lattner66e810b2002-04-18 18:53:13 +0000491 } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
492 // As a special case, print the array as a string if it is an array of
493 // ubytes or an array of sbytes with positive values.
Misha Brukmanfd939082005-04-21 23:48:37 +0000494 //
Chris Lattner66e810b2002-04-18 18:53:13 +0000495 const Type *ETy = CA->getType()->getElementType();
Chris Lattner18365502006-01-23 23:03:36 +0000496 if (CA->isString()) {
Chris Lattner66e810b2002-04-18 18:53:13 +0000497 Out << "c\"";
Chris Lattner18365502006-01-23 23:03:36 +0000498 PrintEscapedString(CA->getAsString(), Out);
Chris Lattner66e810b2002-04-18 18:53:13 +0000499 Out << "\"";
500
501 } else { // Cannot output in string format...
Misha Brukman40c732c2004-06-04 21:11:51 +0000502 Out << '[';
Chris Lattner7a716ad2002-04-16 21:36:08 +0000503 if (CA->getNumOperands()) {
Misha Brukman40c732c2004-06-04 21:11:51 +0000504 Out << ' ';
Chris Lattner66e810b2002-04-18 18:53:13 +0000505 printTypeInt(Out, ETy, TypeTable);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000506 WriteAsOperandInternal(Out, CA->getOperand(0),
Chris Lattner2fcfdb72006-12-06 06:24:27 +0000507 TypeTable, Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000508 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
509 Out << ", ";
Chris Lattner66e810b2002-04-18 18:53:13 +0000510 printTypeInt(Out, ETy, TypeTable);
Chris Lattner2fcfdb72006-12-06 06:24:27 +0000511 WriteAsOperandInternal(Out, CA->getOperand(i), TypeTable, Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000512 }
513 }
514 Out << " ]";
515 }
516 } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Andrew Lenharth43f344a2007-01-08 18:21:30 +0000517 if (CS->getType()->isPacked())
518 Out << '<';
Misha Brukman40c732c2004-06-04 21:11:51 +0000519 Out << '{';
Jim Laskeya3f332b2006-02-25 12:27:03 +0000520 unsigned N = CS->getNumOperands();
521 if (N) {
Jim Laskey38a409c2006-02-27 10:33:53 +0000522 if (N > 2) {
523 Indent += std::string(IndentSize, ' ');
524 Out << Indent;
525 } else {
526 Out << ' ';
527 }
Chris Lattner7a716ad2002-04-16 21:36:08 +0000528 printTypeInt(Out, CS->getOperand(0)->getType(), TypeTable);
529
Chris Lattner2fcfdb72006-12-06 06:24:27 +0000530 WriteAsOperandInternal(Out, CS->getOperand(0), TypeTable, Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000531
Jim Laskeya3f332b2006-02-25 12:27:03 +0000532 for (unsigned i = 1; i < N; i++) {
Chris Lattner7a716ad2002-04-16 21:36:08 +0000533 Out << ", ";
Jim Laskey38a409c2006-02-27 10:33:53 +0000534 if (N > 2) Out << Indent;
Chris Lattner7a716ad2002-04-16 21:36:08 +0000535 printTypeInt(Out, CS->getOperand(i)->getType(), TypeTable);
536
Chris Lattner2fcfdb72006-12-06 06:24:27 +0000537 WriteAsOperandInternal(Out, CS->getOperand(i), TypeTable, Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000538 }
Jim Laskey38a409c2006-02-27 10:33:53 +0000539 if (N > 2) Indent.resize(Indent.size() - IndentSize);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000540 }
Jim Laskeya3f332b2006-02-25 12:27:03 +0000541
Chris Lattner7a716ad2002-04-16 21:36:08 +0000542 Out << " }";
Andrew Lenharth43f344a2007-01-08 18:21:30 +0000543 if (CS->getType()->isPacked())
544 Out << '>';
Reid Spencer9d6565a2007-02-15 02:26:10 +0000545 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
Brian Gaeke715c90b2004-08-20 06:00:58 +0000546 const Type *ETy = CP->getType()->getElementType();
Misha Brukmanfd939082005-04-21 23:48:37 +0000547 assert(CP->getNumOperands() > 0 &&
Brian Gaeke715c90b2004-08-20 06:00:58 +0000548 "Number of operands for a PackedConst must be > 0");
549 Out << '<';
550 Out << ' ';
551 printTypeInt(Out, ETy, TypeTable);
Chris Lattner2fcfdb72006-12-06 06:24:27 +0000552 WriteAsOperandInternal(Out, CP->getOperand(0), TypeTable, Machine);
Brian Gaeke715c90b2004-08-20 06:00:58 +0000553 for (unsigned i = 1, e = CP->getNumOperands(); i != e; ++i) {
554 Out << ", ";
555 printTypeInt(Out, ETy, TypeTable);
Chris Lattner2fcfdb72006-12-06 06:24:27 +0000556 WriteAsOperandInternal(Out, CP->getOperand(i), TypeTable, Machine);
Brian Gaeke715c90b2004-08-20 06:00:58 +0000557 }
558 Out << " >";
Chris Lattner7a716ad2002-04-16 21:36:08 +0000559 } else if (isa<ConstantPointerNull>(CV)) {
560 Out << "null";
561
Chris Lattnerb976e662004-10-16 18:08:06 +0000562 } else if (isa<UndefValue>(CV)) {
563 Out << "undef";
564
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000565 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Reid Spencer81dfeb32006-12-04 05:19:18 +0000566 Out << CE->getOpcodeName();
567 if (CE->isCompare())
568 Out << " " << getPredicateText(CE->getPredicate());
569 Out << " (";
Misha Brukmanfd939082005-04-21 23:48:37 +0000570
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000571 for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
572 printTypeInt(Out, (*OI)->getType(), TypeTable);
Chris Lattner2fcfdb72006-12-06 06:24:27 +0000573 WriteAsOperandInternal(Out, *OI, TypeTable, Machine);
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000574 if (OI+1 != CE->op_end())
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000575 Out << ", ";
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000576 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000577
Reid Spencer3da59db2006-11-27 01:05:10 +0000578 if (CE->isCast()) {
Chris Lattner95586b82002-08-15 19:37:43 +0000579 Out << " to ";
580 printTypeInt(Out, CE->getType(), TypeTable);
581 }
Reid Spencer3da59db2006-11-27 01:05:10 +0000582
Misha Brukman40c732c2004-06-04 21:11:51 +0000583 Out << ')';
Chris Lattner95586b82002-08-15 19:37:43 +0000584
Chris Lattner7a716ad2002-04-16 21:36:08 +0000585 } else {
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000586 Out << "<placeholder or erroneous Constant>";
Chris Lattner7a716ad2002-04-16 21:36:08 +0000587 }
588}
589
590
Misha Brukmanab5c6002004-03-02 00:22:19 +0000591/// WriteAsOperand - Write the name of the specified value out to the specified
592/// ostream. This can be useful when you just want to print int %reg126, not
593/// the whole instruction that generated it.
594///
Misha Brukmanfd939082005-04-21 23:48:37 +0000595static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
Chris Lattner7b13f562003-05-08 02:08:14 +0000596 std::map<const Type*, std::string> &TypeTable,
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000597 SlotMachine *Machine) {
Misha Brukman40c732c2004-06-04 21:11:51 +0000598 Out << ' ';
Chris Lattner2fcfdb72006-12-06 06:24:27 +0000599 if (V->hasName())
Reid Spencer3702d262007-01-26 08:02:52 +0000600 Out << getLLVMName(V->getName(),
601 isa<GlobalValue>(V) ? GlobalPrefix : LocalPrefix);
Reid Spencer79703962004-07-17 23:47:01 +0000602 else {
603 const Constant *CV = dyn_cast<Constant>(V);
Chris Lattner80cd1152006-01-25 22:26:05 +0000604 if (CV && !isa<GlobalValue>(CV)) {
Chris Lattner2fcfdb72006-12-06 06:24:27 +0000605 WriteConstantInt(Out, CV, TypeTable, Machine);
Chris Lattner80cd1152006-01-25 22:26:05 +0000606 } else if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
607 Out << "asm ";
608 if (IA->hasSideEffects())
609 Out << "sideeffect ";
610 Out << '"';
611 PrintEscapedString(IA->getAsmString(), Out);
612 Out << "\", \"";
613 PrintEscapedString(IA->getConstraintString(), Out);
614 Out << '"';
615 } else {
Reid Spencer3702d262007-01-26 08:02:52 +0000616 char Prefix = '%';
Chris Lattner7a716ad2002-04-16 21:36:08 +0000617 int Slot;
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000618 if (Machine) {
Reid Spencer3702d262007-01-26 08:02:52 +0000619 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattner22379bc2007-01-11 03:54:27 +0000620 Slot = Machine->getGlobalSlot(GV);
Reid Spencer3702d262007-01-26 08:02:52 +0000621 Prefix = '@';
622 } else {
Chris Lattner22379bc2007-01-11 03:54:27 +0000623 Slot = Machine->getLocalSlot(V);
Reid Spencer3702d262007-01-26 08:02:52 +0000624 }
Chris Lattner7a716ad2002-04-16 21:36:08 +0000625 } else {
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000626 Machine = createSlotMachine(V);
Chris Lattner22379bc2007-01-11 03:54:27 +0000627 if (Machine) {
Reid Spencer3702d262007-01-26 08:02:52 +0000628 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattner22379bc2007-01-11 03:54:27 +0000629 Slot = Machine->getGlobalSlot(GV);
Reid Spencer3702d262007-01-26 08:02:52 +0000630 Prefix = '@';
631 } else {
Chris Lattner22379bc2007-01-11 03:54:27 +0000632 Slot = Machine->getLocalSlot(V);
Reid Spencer3702d262007-01-26 08:02:52 +0000633 }
Chris Lattner22379bc2007-01-11 03:54:27 +0000634 } else {
Chris Lattner69566452004-06-09 19:41:19 +0000635 Slot = -1;
Chris Lattner22379bc2007-01-11 03:54:27 +0000636 }
Reid Spencerb03de0c2004-05-26 21:56:09 +0000637 delete Machine;
Chris Lattner7a716ad2002-04-16 21:36:08 +0000638 }
Chris Lattner69566452004-06-09 19:41:19 +0000639 if (Slot != -1)
Reid Spencer3702d262007-01-26 08:02:52 +0000640 Out << Prefix << Slot;
Chris Lattner69566452004-06-09 19:41:19 +0000641 else
642 Out << "<badref>";
Chris Lattner7a716ad2002-04-16 21:36:08 +0000643 }
644 }
645}
646
Misha Brukman9d0802e2004-03-01 19:48:13 +0000647/// WriteAsOperand - Write the name of the specified value out to the specified
648/// ostream. This can be useful when you just want to print int %reg126, not
649/// the whole instruction that generated it.
650///
Chris Lattner31f84992003-11-21 20:23:48 +0000651std::ostream &llvm::WriteAsOperand(std::ostream &Out, const Value *V,
Chris Lattnera6b1ffc2006-12-06 06:15:43 +0000652 bool PrintType, const Module *Context) {
Chris Lattner7b13f562003-05-08 02:08:14 +0000653 std::map<const Type *, std::string> TypeNames;
Chris Lattner607dc682002-07-10 16:48:17 +0000654 if (Context == 0) Context = getModuleFromVal(V);
Chris Lattner207b5bc2001-10-29 16:37:48 +0000655
Chris Lattner6e6026b2002-11-20 18:36:02 +0000656 if (Context)
Chris Lattner607dc682002-07-10 16:48:17 +0000657 fillTypeNameTable(Context, TypeNames);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000658
659 if (PrintType)
660 printTypeInt(Out, V->getType(), TypeNames);
Misha Brukmanfd939082005-04-21 23:48:37 +0000661
Chris Lattner2fcfdb72006-12-06 06:24:27 +0000662 WriteAsOperandInternal(Out, V, TypeNames, 0);
Chris Lattner622f7402001-07-20 19:15:21 +0000663 return Out;
664}
665
Reid Spencer0e25e1c2004-07-04 11:50:43 +0000666
Chris Lattner31f84992003-11-21 20:23:48 +0000667namespace llvm {
Chris Lattnerd8c2e422001-07-12 23:35:26 +0000668
Chris Lattner007377f2001-09-07 16:36:04 +0000669class AssemblyWriter {
Misha Brukman0313e0b2004-06-21 21:53:56 +0000670 std::ostream &Out;
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000671 SlotMachine &Machine;
Chris Lattnerc1824992001-10-29 16:05:51 +0000672 const Module *TheModule;
Chris Lattner7b13f562003-05-08 02:08:14 +0000673 std::map<const Type *, std::string> TypeNames;
Chris Lattner95e5a2c2003-10-30 23:41:03 +0000674 AssemblyAnnotationWriter *AnnotationWriter;
Chris Lattner00950542001-06-06 20:29:01 +0000675public:
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000676 inline AssemblyWriter(std::ostream &o, SlotMachine &Mac, const Module *M,
Chris Lattner95e5a2c2003-10-30 23:41:03 +0000677 AssemblyAnnotationWriter *AAW)
Misha Brukman0313e0b2004-06-21 21:53:56 +0000678 : Out(o), Machine(Mac), TheModule(M), AnnotationWriter(AAW) {
Chris Lattnerc1824992001-10-29 16:05:51 +0000679
680 // If the module has a symbol table, take all global types and stuff their
681 // names into the TypeNames map.
682 //
Chris Lattner207b5bc2001-10-29 16:37:48 +0000683 fillTypeNameTable(M, TypeNames);
Chris Lattner00950542001-06-06 20:29:01 +0000684 }
685
Chris Lattnerc1824992001-10-29 16:05:51 +0000686 inline void write(const Module *M) { printModule(M); }
687 inline void write(const GlobalVariable *G) { printGlobal(G); }
Chris Lattner79df7c02002-03-26 18:01:55 +0000688 inline void write(const Function *F) { printFunction(F); }
Chris Lattnerc1824992001-10-29 16:05:51 +0000689 inline void write(const BasicBlock *BB) { printBasicBlock(BB); }
Chris Lattner7e708292002-06-25 16:13:24 +0000690 inline void write(const Instruction *I) { printInstruction(*I); }
Chris Lattnerda1fbcc2001-11-07 04:21:57 +0000691 inline void write(const Type *Ty) { printType(Ty); }
Chris Lattner00950542001-06-06 20:29:01 +0000692
Chris Lattner2fcfdb72006-12-06 06:24:27 +0000693 void writeOperand(const Value *Op, bool PrintType);
Chris Lattner66e810b2002-04-18 18:53:13 +0000694
Misha Brukman5cf1acf2004-04-28 15:31:21 +0000695 const Module* getModule() { return TheModule; }
696
Misha Brukmanf771bea2004-11-15 19:30:05 +0000697private:
Chris Lattnerc1824992001-10-29 16:05:51 +0000698 void printModule(const Module *M);
Reid Spencer78d033e2007-01-06 07:24:44 +0000699 void printTypeSymbolTable(const TypeSymbolTable &ST);
Chris Lattnerc1824992001-10-29 16:05:51 +0000700 void printGlobal(const GlobalVariable *GV);
Chris Lattner79df7c02002-03-26 18:01:55 +0000701 void printFunction(const Function *F);
Reid Spencerb138a062007-04-09 06:10:42 +0000702 void printArgument(const Argument *FA, uint16_t ParamAttrs);
Chris Lattnerc1824992001-10-29 16:05:51 +0000703 void printBasicBlock(const BasicBlock *BB);
Chris Lattner7e708292002-06-25 16:13:24 +0000704 void printInstruction(const Instruction &I);
Chris Lattner2761e9f2002-04-13 20:53:41 +0000705
706 // printType - Go to extreme measures to attempt to print out a short,
707 // symbolic version of a type name.
708 //
Chris Lattner7b13f562003-05-08 02:08:14 +0000709 std::ostream &printType(const Type *Ty) {
Misha Brukman0313e0b2004-06-21 21:53:56 +0000710 return printTypeInt(Out, Ty, TypeNames);
Chris Lattner2761e9f2002-04-13 20:53:41 +0000711 }
712
713 // printTypeAtLeastOneLevel - Print out one level of the possibly complex type
714 // without considering any symbolic types that we may have equal to it.
715 //
Chris Lattner7b13f562003-05-08 02:08:14 +0000716 std::ostream &printTypeAtLeastOneLevel(const Type *Ty);
Chris Lattnerc1824992001-10-29 16:05:51 +0000717
Chris Lattnere02fa852001-10-13 06:42:36 +0000718 // printInfoComment - Print a little comment after the instruction indicating
719 // which slot it occupies.
Chris Lattner7e708292002-06-25 16:13:24 +0000720 void printInfoComment(const Value &V);
Chris Lattner00950542001-06-06 20:29:01 +0000721};
Reid Spencer73b74952004-05-27 22:04:46 +0000722} // end of llvm namespace
Chris Lattner00950542001-06-06 20:29:01 +0000723
Misha Brukmanab5c6002004-03-02 00:22:19 +0000724/// printTypeAtLeastOneLevel - Print out one level of the possibly complex type
725/// without considering any symbolic types that we may have equal to it.
726///
Chris Lattner7b13f562003-05-08 02:08:14 +0000727std::ostream &AssemblyWriter::printTypeAtLeastOneLevel(const Type *Ty) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000728 if (const IntegerType *ITy = dyn_cast<IntegerType>(Ty))
729 Out << "i" << utostr(ITy->getBitWidth());
730 else if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
Reid Spencerbd5db8e2006-12-31 05:24:50 +0000731 printType(FTy->getReturnType());
Reid Spencerbd5db8e2006-12-31 05:24:50 +0000732 Out << " (";
733 unsigned Idx = 1;
Reid Spencerb138a062007-04-09 06:10:42 +0000734 const ParamAttrsList *Attrs = FTy->getParamAttrs();
Chris Lattnerd5d89962004-02-09 04:14:01 +0000735 for (FunctionType::param_iterator I = FTy->param_begin(),
736 E = FTy->param_end(); I != E; ++I) {
737 if (I != FTy->param_begin())
Misha Brukman0313e0b2004-06-21 21:53:56 +0000738 Out << ", ";
Chris Lattner7a716ad2002-04-16 21:36:08 +0000739 printType(*I);
Reid Spencer18da0722007-04-11 02:44:20 +0000740 if (Attrs && Attrs->getParamAttrs(Idx) != ParamAttr::None) {
Reid Spencerb138a062007-04-09 06:10:42 +0000741 Out << " " << Attrs->getParamAttrsTextByIndex(Idx);
Reid Spencerbd5db8e2006-12-31 05:24:50 +0000742 }
743 Idx++;
Chris Lattner2761e9f2002-04-13 20:53:41 +0000744 }
745 if (FTy->isVarArg()) {
Misha Brukman0313e0b2004-06-21 21:53:56 +0000746 if (FTy->getNumParams()) Out << ", ";
747 Out << "...";
Chris Lattner2761e9f2002-04-13 20:53:41 +0000748 }
Misha Brukman0313e0b2004-06-21 21:53:56 +0000749 Out << ')';
Reid Spencer18da0722007-04-11 02:44:20 +0000750 if (Attrs && Attrs->getParamAttrs(0) != ParamAttr::None)
Reid Spencerb138a062007-04-09 06:10:42 +0000751 Out << ' ' << Attrs->getParamAttrsTextByIndex(0);
Chris Lattner7e708292002-06-25 16:13:24 +0000752 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
Andrew Lenharth38ecbf12006-12-08 18:06:16 +0000753 if (STy->isPacked())
754 Out << '<';
Misha Brukman0313e0b2004-06-21 21:53:56 +0000755 Out << "{ ";
Chris Lattnerd21cd802004-02-09 04:37:31 +0000756 for (StructType::element_iterator I = STy->element_begin(),
757 E = STy->element_end(); I != E; ++I) {
758 if (I != STy->element_begin())
Misha Brukman0313e0b2004-06-21 21:53:56 +0000759 Out << ", ";
Chris Lattner2761e9f2002-04-13 20:53:41 +0000760 printType(*I);
761 }
Misha Brukman0313e0b2004-06-21 21:53:56 +0000762 Out << " }";
Andrew Lenharth38ecbf12006-12-08 18:06:16 +0000763 if (STy->isPacked())
764 Out << '>';
Chris Lattner7e708292002-06-25 16:13:24 +0000765 } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
Misha Brukman40c732c2004-06-04 21:11:51 +0000766 printType(PTy->getElementType()) << '*';
Chris Lattner7e708292002-06-25 16:13:24 +0000767 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Misha Brukman0313e0b2004-06-21 21:53:56 +0000768 Out << '[' << ATy->getNumElements() << " x ";
Misha Brukman40c732c2004-06-04 21:11:51 +0000769 printType(ATy->getElementType()) << ']';
Reid Spencer9d6565a2007-02-15 02:26:10 +0000770 } else if (const VectorType *PTy = dyn_cast<VectorType>(Ty)) {
Reid Spencer5527c0b2004-08-20 15:37:30 +0000771 Out << '<' << PTy->getNumElements() << " x ";
772 printType(PTy->getElementType()) << '>';
773 }
Reid Spencer3ed469c2006-11-02 20:25:50 +0000774 else if (isa<OpaqueType>(Ty)) {
Misha Brukman0313e0b2004-06-21 21:53:56 +0000775 Out << "opaque";
Chris Lattner2761e9f2002-04-13 20:53:41 +0000776 } else {
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000777 if (!Ty->isPrimitiveType())
Misha Brukman0313e0b2004-06-21 21:53:56 +0000778 Out << "<unknown derived type>";
Chris Lattner2761e9f2002-04-13 20:53:41 +0000779 printType(Ty);
780 }
Misha Brukman0313e0b2004-06-21 21:53:56 +0000781 return Out;
Chris Lattner2761e9f2002-04-13 20:53:41 +0000782}
783
784
Chris Lattner2fcfdb72006-12-06 06:24:27 +0000785void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType) {
786 if (Operand == 0) {
Chris Lattneraab18202005-02-24 16:58:29 +0000787 Out << "<null operand!>";
Chris Lattner2fcfdb72006-12-06 06:24:27 +0000788 } else {
789 if (PrintType) { Out << ' '; printType(Operand->getType()); }
790 WriteAsOperandInternal(Out, Operand, TypeNames, &Machine);
Chris Lattneraab18202005-02-24 16:58:29 +0000791 }
Chris Lattner00950542001-06-06 20:29:01 +0000792}
793
Chris Lattner00950542001-06-06 20:29:01 +0000794
Chris Lattnerc1824992001-10-29 16:05:51 +0000795void AssemblyWriter::printModule(const Module *M) {
Chris Lattner31ab1b32005-03-02 23:12:40 +0000796 if (!M->getModuleIdentifier().empty() &&
Misha Brukmanfd939082005-04-21 23:48:37 +0000797 // Don't print the ID if it will start a new line (which would
Chris Lattner31ab1b32005-03-02 23:12:40 +0000798 // require a comment char before it).
799 M->getModuleIdentifier().find('\n') == std::string::npos)
800 Out << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
801
Owen Andersoncf7ff2b2006-10-18 02:21:12 +0000802 if (!M->getDataLayout().empty())
Chris Lattnerd2f9e602006-10-22 06:06:56 +0000803 Out << "target datalayout = \"" << M->getDataLayout() << "\"\n";
Reid Spencercddc86f2004-07-25 21:44:54 +0000804 if (!M->getTargetTriple().empty())
Reid Spencerc9a1f0d2004-07-25 21:29:43 +0000805 Out << "target triple = \"" << M->getTargetTriple() << "\"\n";
Misha Brukmanfd939082005-04-21 23:48:37 +0000806
Chris Lattnercc041ba2006-01-24 04:13:11 +0000807 if (!M->getModuleInlineAsm().empty()) {
Chris Lattner42a162e2006-01-24 00:45:30 +0000808 // Split the string into lines, to make it easier to read the .ll file.
Chris Lattnercc041ba2006-01-24 04:13:11 +0000809 std::string Asm = M->getModuleInlineAsm();
Chris Lattner42a162e2006-01-24 00:45:30 +0000810 size_t CurPos = 0;
811 size_t NewLine = Asm.find_first_of('\n', CurPos);
812 while (NewLine != std::string::npos) {
813 // We found a newline, print the portion of the asm string from the
814 // last newline up to this newline.
815 Out << "module asm \"";
816 PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.begin()+NewLine),
817 Out);
818 Out << "\"\n";
819 CurPos = NewLine+1;
820 NewLine = Asm.find_first_of('\n', CurPos);
821 }
Chris Lattner71cdba32006-01-24 00:40:17 +0000822 Out << "module asm \"";
Chris Lattner42a162e2006-01-24 00:45:30 +0000823 PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.end()), Out);
Chris Lattner18365502006-01-23 23:03:36 +0000824 Out << "\"\n";
825 }
826
Chris Lattner44da7d72004-09-14 05:06:58 +0000827 // Loop over the dependent libraries and emit them.
Chris Lattnercfe97b72004-09-14 04:51:44 +0000828 Module::lib_iterator LI = M->lib_begin();
829 Module::lib_iterator LE = M->lib_end();
Reid Spencercddc86f2004-07-25 21:44:54 +0000830 if (LI != LE) {
Chris Lattnercfe97b72004-09-14 04:51:44 +0000831 Out << "deplibs = [ ";
832 while (LI != LE) {
Chris Lattner44da7d72004-09-14 05:06:58 +0000833 Out << '"' << *LI << '"';
Reid Spencerc9a1f0d2004-07-25 21:29:43 +0000834 ++LI;
Chris Lattnercfe97b72004-09-14 04:51:44 +0000835 if (LI != LE)
836 Out << ", ";
Reid Spencerc9a1f0d2004-07-25 21:29:43 +0000837 }
838 Out << " ]\n";
Reid Spencer83f6a772004-07-25 18:08:18 +0000839 }
Reid Spencere59eaf42004-09-13 23:44:23 +0000840
Chris Lattner44da7d72004-09-14 05:06:58 +0000841 // Loop over the symbol table, emitting all named constants.
Reid Spencer78d033e2007-01-06 07:24:44 +0000842 printTypeSymbolTable(M->getTypeSymbolTable());
Misha Brukmanfd939082005-04-21 23:48:37 +0000843
Chris Lattnerd6d826c2006-12-06 04:41:52 +0000844 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
845 I != E; ++I)
Chris Lattner7e708292002-06-25 16:13:24 +0000846 printGlobal(I);
Chris Lattner007377f2001-09-07 16:36:04 +0000847
Chris Lattner44da7d72004-09-14 05:06:58 +0000848 // Output all of the functions.
Chris Lattner7e708292002-06-25 16:13:24 +0000849 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
850 printFunction(I);
Chris Lattner007377f2001-09-07 16:36:04 +0000851}
852
Chris Lattnerc1824992001-10-29 16:05:51 +0000853void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Reid Spencer3702d262007-01-26 08:02:52 +0000854 if (GV->hasName()) Out << getLLVMName(GV->getName(), GlobalPrefix) << " = ";
Chris Lattnerd70684f2001-09-18 04:01:05 +0000855
Misha Brukmanfd939082005-04-21 23:48:37 +0000856 if (!GV->hasInitializer())
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000857 switch (GV->getLinkage()) {
858 case GlobalValue::DLLImportLinkage: Out << "dllimport "; break;
859 case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break;
860 default: Out << "external "; break;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000861 } else {
Chris Lattner4ad02e72003-04-16 20:28:45 +0000862 switch (GV->getLinkage()) {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000863 case GlobalValue::InternalLinkage: Out << "internal "; break;
864 case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
865 case GlobalValue::WeakLinkage: Out << "weak "; break;
866 case GlobalValue::AppendingLinkage: Out << "appending "; break;
867 case GlobalValue::DLLImportLinkage: Out << "dllimport "; break;
868 case GlobalValue::DLLExportLinkage: Out << "dllexport "; break;
869 case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break;
870 case GlobalValue::ExternalLinkage: break;
Misha Brukman3c8f2c62004-11-14 21:04:34 +0000871 case GlobalValue::GhostLinkage:
Bill Wendlinge8156192006-12-07 01:30:32 +0000872 cerr << "GhostLinkage not allowed in AsmWriter!\n";
Misha Brukman3c8f2c62004-11-14 21:04:34 +0000873 abort();
Chris Lattner4ad02e72003-04-16 20:28:45 +0000874 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000875 switch (GV->getVisibility()) {
Chris Lattner9a40c022007-01-15 18:28:18 +0000876 default: assert(0 && "Invalid visibility style!");
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000877 case GlobalValue::DefaultVisibility: break;
878 case GlobalValue::HiddenVisibility: Out << "hidden "; break;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000879 }
880 }
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +0000881
882 if (GV->isThreadLocal()) Out << "thread_local ";
Misha Brukman0313e0b2004-06-21 21:53:56 +0000883 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattner7a176752001-12-04 00:03:30 +0000884 printType(GV->getType()->getElementType());
Chris Lattnerd70684f2001-09-18 04:01:05 +0000885
Reid Spencer79703962004-07-17 23:47:01 +0000886 if (GV->hasInitializer()) {
887 Constant* C = cast<Constant>(GV->getInitializer());
888 assert(C && "GlobalVar initializer isn't constant?");
Chris Lattner2fcfdb72006-12-06 06:24:27 +0000889 writeOperand(GV->getInitializer(), false);
Reid Spencer79703962004-07-17 23:47:01 +0000890 }
Chris Lattner30caa282005-11-06 06:48:53 +0000891
Chris Lattner60962db2005-11-12 00:10:19 +0000892 if (GV->hasSection())
893 Out << ", section \"" << GV->getSection() << '"';
894 if (GV->getAlignment())
Chris Lattner30caa282005-11-06 06:48:53 +0000895 Out << ", align " << GV->getAlignment();
Chris Lattner60962db2005-11-12 00:10:19 +0000896
Chris Lattner7e708292002-06-25 16:13:24 +0000897 printInfoComment(*GV);
Misha Brukman0313e0b2004-06-21 21:53:56 +0000898 Out << "\n";
Chris Lattner70cc3392001-09-10 07:58:01 +0000899}
900
Reid Spencer78d033e2007-01-06 07:24:44 +0000901void AssemblyWriter::printTypeSymbolTable(const TypeSymbolTable &ST) {
Reid Spencer9231ac82004-05-25 08:53:40 +0000902 // Print the types.
Reid Spencer78d033e2007-01-06 07:24:44 +0000903 for (TypeSymbolTable::const_iterator TI = ST.begin(), TE = ST.end();
904 TI != TE; ++TI) {
Reid Spencer3702d262007-01-26 08:02:52 +0000905 Out << "\t" << getLLVMName(TI->first, LocalPrefix) << " = type ";
Reid Spencer9231ac82004-05-25 08:53:40 +0000906
907 // Make sure we print out at least one level of the type structure, so
908 // that we do not get %FILE = type %FILE
909 //
910 printTypeAtLeastOneLevel(TI->second) << "\n";
911 }
Reid Spencer78d033e2007-01-06 07:24:44 +0000912}
913
Misha Brukmanab5c6002004-03-02 00:22:19 +0000914/// printFunction - Print all aspects of a function.
915///
Chris Lattner7e708292002-06-25 16:13:24 +0000916void AssemblyWriter::printFunction(const Function *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000917 // Print out the return type and name...
Misha Brukman0313e0b2004-06-21 21:53:56 +0000918 Out << "\n";
Chris Lattner4ad02e72003-04-16 20:28:45 +0000919
Misha Brukman0313e0b2004-06-21 21:53:56 +0000920 if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out);
Chris Lattner95e5a2c2003-10-30 23:41:03 +0000921
Reid Spencer5cbf9852007-01-30 20:08:39 +0000922 if (F->isDeclaration())
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000923 switch (F->getLinkage()) {
924 case GlobalValue::DLLImportLinkage: Out << "declare dllimport "; break;
925 case GlobalValue::ExternalWeakLinkage: Out << "declare extern_weak "; break;
926 default: Out << "declare ";
927 }
Reid Spencerb951bc02006-12-29 20:29:48 +0000928 else {
929 Out << "define ";
Chris Lattner4ad02e72003-04-16 20:28:45 +0000930 switch (F->getLinkage()) {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000931 case GlobalValue::InternalLinkage: Out << "internal "; break;
932 case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
933 case GlobalValue::WeakLinkage: Out << "weak "; break;
934 case GlobalValue::AppendingLinkage: Out << "appending "; break;
935 case GlobalValue::DLLImportLinkage: Out << "dllimport "; break;
936 case GlobalValue::DLLExportLinkage: Out << "dllexport "; break;
937 case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break;
Chris Lattner4ad02e72003-04-16 20:28:45 +0000938 case GlobalValue::ExternalLinkage: break;
Misha Brukman3c8f2c62004-11-14 21:04:34 +0000939 case GlobalValue::GhostLinkage:
Bill Wendlinge8156192006-12-07 01:30:32 +0000940 cerr << "GhostLinkage not allowed in AsmWriter!\n";
Misha Brukman3c8f2c62004-11-14 21:04:34 +0000941 abort();
Chris Lattner4ad02e72003-04-16 20:28:45 +0000942 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000943 switch (F->getVisibility()) {
Chris Lattner9a40c022007-01-15 18:28:18 +0000944 default: assert(0 && "Invalid visibility style!");
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000945 case GlobalValue::DefaultVisibility: break;
946 case GlobalValue::HiddenVisibility: Out << "hidden "; break;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000947 }
Reid Spencerb951bc02006-12-29 20:29:48 +0000948 }
Chris Lattner4ad02e72003-04-16 20:28:45 +0000949
Chris Lattnerd5118982005-05-06 20:26:43 +0000950 // Print the calling convention.
951 switch (F->getCallingConv()) {
952 case CallingConv::C: break; // default
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000953 case CallingConv::Fast: Out << "fastcc "; break;
954 case CallingConv::Cold: Out << "coldcc "; break;
955 case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break;
956 case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break;
Chris Lattnerd5118982005-05-06 20:26:43 +0000957 default: Out << "cc" << F->getCallingConv() << " "; break;
958 }
959
Reid Spencerbd5db8e2006-12-31 05:24:50 +0000960 const FunctionType *FT = F->getFunctionType();
Reid Spencerb138a062007-04-09 06:10:42 +0000961 const ParamAttrsList *Attrs = FT->getParamAttrs();
Misha Brukman40c732c2004-06-04 21:11:51 +0000962 printType(F->getReturnType()) << ' ';
Chris Lattner4d45bd02003-10-18 05:57:43 +0000963 if (!F->getName().empty())
Reid Spencer3702d262007-01-26 08:02:52 +0000964 Out << getLLVMName(F->getName(), GlobalPrefix);
Chris Lattner4d45bd02003-10-18 05:57:43 +0000965 else
Reid Spencer3702d262007-01-26 08:02:52 +0000966 Out << "@\"\"";
Misha Brukman0313e0b2004-06-21 21:53:56 +0000967 Out << '(';
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000968 Machine.incorporateFunction(F);
Chris Lattner007377f2001-09-07 16:36:04 +0000969
Chris Lattnerc1824992001-10-29 16:05:51 +0000970 // Loop over the arguments, printing them...
Chris Lattner007377f2001-09-07 16:36:04 +0000971
Reid Spencerbd5db8e2006-12-31 05:24:50 +0000972 unsigned Idx = 1;
Chris Lattnerd6d826c2006-12-06 04:41:52 +0000973 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
Reid Spencerbd5db8e2006-12-31 05:24:50 +0000974 I != E; ++I) {
975 // Insert commas as we go... the first arg doesn't get a comma
976 if (I != F->arg_begin()) Out << ", ";
Reid Spencerb138a062007-04-09 06:10:42 +0000977 printArgument(I, (Attrs ? Attrs->getParamAttrs(Idx)
Reid Spencer18da0722007-04-11 02:44:20 +0000978 : uint16_t(ParamAttr::None)));
Reid Spencerbd5db8e2006-12-31 05:24:50 +0000979 Idx++;
980 }
Chris Lattner007377f2001-09-07 16:36:04 +0000981
982 // Finish printing arguments...
Chris Lattner7e708292002-06-25 16:13:24 +0000983 if (FT->isVarArg()) {
Misha Brukman0313e0b2004-06-21 21:53:56 +0000984 if (FT->getNumParams()) Out << ", ";
985 Out << "..."; // Output varargs portion of signature!
Chris Lattner007377f2001-09-07 16:36:04 +0000986 }
Misha Brukman0313e0b2004-06-21 21:53:56 +0000987 Out << ')';
Reid Spencer18da0722007-04-11 02:44:20 +0000988 if (Attrs && Attrs->getParamAttrs(0) != ParamAttr::None)
Reid Spencerb138a062007-04-09 06:10:42 +0000989 Out << ' ' << Attrs->getParamAttrsTextByIndex(0);
Chris Lattner60962db2005-11-12 00:10:19 +0000990 if (F->hasSection())
991 Out << " section \"" << F->getSection() << '"';
Chris Lattner30caa282005-11-06 06:48:53 +0000992 if (F->getAlignment())
993 Out << " align " << F->getAlignment();
Chris Lattner60962db2005-11-12 00:10:19 +0000994
Reid Spencer5cbf9852007-01-30 20:08:39 +0000995 if (F->isDeclaration()) {
Misha Brukman0313e0b2004-06-21 21:53:56 +0000996 Out << "\n";
Chris Lattner03e2acb2002-05-06 03:00:40 +0000997 } else {
Misha Brukman0313e0b2004-06-21 21:53:56 +0000998 Out << " {";
Misha Brukmanfd939082005-04-21 23:48:37 +0000999
Chris Lattnerb5794002002-04-07 22:49:37 +00001000 // Output all of its basic blocks... for the function
Chris Lattner7e708292002-06-25 16:13:24 +00001001 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
1002 printBasicBlock(I);
Chris Lattner007377f2001-09-07 16:36:04 +00001003
Misha Brukman0313e0b2004-06-21 21:53:56 +00001004 Out << "}\n";
Chris Lattner007377f2001-09-07 16:36:04 +00001005 }
1006
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001007 Machine.purgeFunction();
Chris Lattner00950542001-06-06 20:29:01 +00001008}
1009
Misha Brukmanab5c6002004-03-02 00:22:19 +00001010/// printArgument - This member is called for every argument that is passed into
1011/// the function. Simply print it out
1012///
Reid Spencerb138a062007-04-09 06:10:42 +00001013void AssemblyWriter::printArgument(const Argument *Arg, uint16_t Attrs) {
Chris Lattner00950542001-06-06 20:29:01 +00001014 // Output type...
Chris Lattnerc1824992001-10-29 16:05:51 +00001015 printType(Arg->getType());
Misha Brukmanfd939082005-04-21 23:48:37 +00001016
Reid Spencer18da0722007-04-11 02:44:20 +00001017 if (Attrs != ParamAttr::None)
Reid Spencerb138a062007-04-09 06:10:42 +00001018 Out << ' ' << ParamAttrsList::getParamAttrsText(Attrs);
Reid Spencerbd5db8e2006-12-31 05:24:50 +00001019
Chris Lattner00950542001-06-06 20:29:01 +00001020 // Output name, if available...
1021 if (Arg->hasName())
Reid Spencer3702d262007-01-26 08:02:52 +00001022 Out << ' ' << getLLVMName(Arg->getName(), LocalPrefix);
Chris Lattner00950542001-06-06 20:29:01 +00001023}
1024
Misha Brukmanab5c6002004-03-02 00:22:19 +00001025/// printBasicBlock - This member is called for each basic block in a method.
1026///
Chris Lattnerc1824992001-10-29 16:05:51 +00001027void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Chris Lattner00950542001-06-06 20:29:01 +00001028 if (BB->hasName()) { // Print out the label if it exists...
Reid Spencer3702d262007-01-26 08:02:52 +00001029 Out << "\n" << getLLVMName(BB->getName(), LabelPrefix) << ':';
Chris Lattnerafc38682002-05-14 16:02:05 +00001030 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Misha Brukman0313e0b2004-06-21 21:53:56 +00001031 Out << "\n; <label>:";
Chris Lattner22379bc2007-01-11 03:54:27 +00001032 int Slot = Machine.getLocalSlot(BB);
Chris Lattner69566452004-06-09 19:41:19 +00001033 if (Slot != -1)
Misha Brukman0313e0b2004-06-21 21:53:56 +00001034 Out << Slot;
Chris Lattner69566452004-06-09 19:41:19 +00001035 else
Misha Brukman0313e0b2004-06-21 21:53:56 +00001036 Out << "<badref>";
Chris Lattner061269b2002-10-02 19:38:55 +00001037 }
Chris Lattner4e4d8622003-11-20 00:09:43 +00001038
1039 if (BB->getParent() == 0)
Misha Brukman0313e0b2004-06-21 21:53:56 +00001040 Out << "\t\t; Error: Block without parent!";
Chris Lattner4e4d8622003-11-20 00:09:43 +00001041 else {
Dan Gohmanecb7a772007-03-22 16:38:57 +00001042 if (BB != &BB->getParent()->getEntryBlock()) { // Not the entry block?
Chris Lattner4e4d8622003-11-20 00:09:43 +00001043 // Output predecessors for the block...
Misha Brukman0313e0b2004-06-21 21:53:56 +00001044 Out << "\t\t;";
Chris Lattner4e4d8622003-11-20 00:09:43 +00001045 pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
Misha Brukmanfd939082005-04-21 23:48:37 +00001046
Chris Lattner4e4d8622003-11-20 00:09:43 +00001047 if (PI == PE) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001048 Out << " No predecessors!";
Chris Lattner4e4d8622003-11-20 00:09:43 +00001049 } else {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001050 Out << " preds =";
Chris Lattner2fcfdb72006-12-06 06:24:27 +00001051 writeOperand(*PI, false);
Chris Lattner4e4d8622003-11-20 00:09:43 +00001052 for (++PI; PI != PE; ++PI) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001053 Out << ',';
Chris Lattner2fcfdb72006-12-06 06:24:27 +00001054 writeOperand(*PI, false);
Chris Lattner4e4d8622003-11-20 00:09:43 +00001055 }
Chris Lattner40efcec2003-11-16 22:59:57 +00001056 }
Chris Lattner061269b2002-10-02 19:38:55 +00001057 }
Chris Lattner00950542001-06-06 20:29:01 +00001058 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001059
Misha Brukman0313e0b2004-06-21 21:53:56 +00001060 Out << "\n";
Chris Lattner00950542001-06-06 20:29:01 +00001061
Misha Brukman0313e0b2004-06-21 21:53:56 +00001062 if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out);
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001063
Chris Lattner007377f2001-09-07 16:36:04 +00001064 // Output all of the instructions in the basic block...
Chris Lattner7e708292002-06-25 16:13:24 +00001065 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
1066 printInstruction(*I);
Chris Lattner9f717ef2004-03-08 18:51:45 +00001067
Misha Brukman0313e0b2004-06-21 21:53:56 +00001068 if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out);
Chris Lattner00950542001-06-06 20:29:01 +00001069}
1070
Chris Lattnere02fa852001-10-13 06:42:36 +00001071
Misha Brukmanab5c6002004-03-02 00:22:19 +00001072/// printInfoComment - Print a little comment after the instruction indicating
1073/// which slot it occupies.
1074///
Chris Lattner7e708292002-06-25 16:13:24 +00001075void AssemblyWriter::printInfoComment(const Value &V) {
1076 if (V.getType() != Type::VoidTy) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001077 Out << "\t\t; <";
Misha Brukman40c732c2004-06-04 21:11:51 +00001078 printType(V.getType()) << '>';
Chris Lattnere02fa852001-10-13 06:42:36 +00001079
Chris Lattner7e708292002-06-25 16:13:24 +00001080 if (!V.hasName()) {
Chris Lattner22379bc2007-01-11 03:54:27 +00001081 int SlotNum;
1082 if (const GlobalValue *GV = dyn_cast<GlobalValue>(&V))
1083 SlotNum = Machine.getGlobalSlot(GV);
1084 else
1085 SlotNum = Machine.getLocalSlot(&V);
Chris Lattner69566452004-06-09 19:41:19 +00001086 if (SlotNum == -1)
Misha Brukman0313e0b2004-06-21 21:53:56 +00001087 Out << ":<badref>";
Reid Spencerfc621e22004-06-09 15:26:53 +00001088 else
Misha Brukman0313e0b2004-06-21 21:53:56 +00001089 Out << ':' << SlotNum; // Print out the def slot taken.
Chris Lattnere02fa852001-10-13 06:42:36 +00001090 }
Chris Lattner5c461402005-02-01 01:24:01 +00001091 Out << " [#uses=" << V.getNumUses() << ']'; // Output # uses
Chris Lattnere02fa852001-10-13 06:42:36 +00001092 }
1093}
1094
Reid Spencer3a9ec242006-08-28 01:02:49 +00001095// This member is called for each Instruction in a function..
Chris Lattner7e708292002-06-25 16:13:24 +00001096void AssemblyWriter::printInstruction(const Instruction &I) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001097 if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out);
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001098
Misha Brukman0313e0b2004-06-21 21:53:56 +00001099 Out << "\t";
Chris Lattner00950542001-06-06 20:29:01 +00001100
1101 // Print out name if it exists...
Chris Lattner7e708292002-06-25 16:13:24 +00001102 if (I.hasName())
Reid Spencer3702d262007-01-26 08:02:52 +00001103 Out << getLLVMName(I.getName(), LocalPrefix) << " = ";
Chris Lattner00950542001-06-06 20:29:01 +00001104
Chris Lattnerddb6db42005-05-06 05:51:46 +00001105 // If this is a volatile load or store, print out the volatile marker.
Chris Lattnere5e475e2003-09-08 17:45:59 +00001106 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
Chris Lattnerddb6db42005-05-06 05:51:46 +00001107 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile())) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001108 Out << "volatile ";
Chris Lattnerddb6db42005-05-06 05:51:46 +00001109 } else if (isa<CallInst>(I) && cast<CallInst>(I).isTailCall()) {
1110 // If this is a call, check if it's a tail call.
1111 Out << "tail ";
1112 }
Chris Lattnere5e475e2003-09-08 17:45:59 +00001113
Chris Lattner00950542001-06-06 20:29:01 +00001114 // Print out the opcode...
Misha Brukman0313e0b2004-06-21 21:53:56 +00001115 Out << I.getOpcodeName();
Chris Lattner00950542001-06-06 20:29:01 +00001116
Reid Spencer74f16422006-12-03 06:27:29 +00001117 // Print out the compare instruction predicates
1118 if (const FCmpInst *FCI = dyn_cast<FCmpInst>(&I)) {
Reid Spencer81dfeb32006-12-04 05:19:18 +00001119 Out << " " << getPredicateText(FCI->getPredicate());
Reid Spencer74f16422006-12-03 06:27:29 +00001120 } else if (const ICmpInst *ICI = dyn_cast<ICmpInst>(&I)) {
Reid Spencer81dfeb32006-12-04 05:19:18 +00001121 Out << " " << getPredicateText(ICI->getPredicate());
Reid Spencer74f16422006-12-03 06:27:29 +00001122 }
1123
Chris Lattner00950542001-06-06 20:29:01 +00001124 // Print out the type of the operands...
Chris Lattner7e708292002-06-25 16:13:24 +00001125 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
Chris Lattner00950542001-06-06 20:29:01 +00001126
1127 // Special case conditional branches to swizzle the condition out to the front
Chris Lattner7e708292002-06-25 16:13:24 +00001128 if (isa<BranchInst>(I) && I.getNumOperands() > 1) {
1129 writeOperand(I.getOperand(2), true);
Misha Brukman0313e0b2004-06-21 21:53:56 +00001130 Out << ',';
Chris Lattner00950542001-06-06 20:29:01 +00001131 writeOperand(Operand, true);
Misha Brukman0313e0b2004-06-21 21:53:56 +00001132 Out << ',';
Chris Lattner7e708292002-06-25 16:13:24 +00001133 writeOperand(I.getOperand(1), true);
Chris Lattner00950542001-06-06 20:29:01 +00001134
Chris Lattner94dc1f22002-04-13 18:34:38 +00001135 } else if (isa<SwitchInst>(I)) {
Chris Lattner00950542001-06-06 20:29:01 +00001136 // Special case switch statement to get formatting nice and correct...
Misha Brukman0313e0b2004-06-21 21:53:56 +00001137 writeOperand(Operand , true); Out << ',';
1138 writeOperand(I.getOperand(1), true); Out << " [";
Chris Lattner00950542001-06-06 20:29:01 +00001139
Chris Lattner7e708292002-06-25 16:13:24 +00001140 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001141 Out << "\n\t\t";
1142 writeOperand(I.getOperand(op ), true); Out << ',';
Chris Lattner7e708292002-06-25 16:13:24 +00001143 writeOperand(I.getOperand(op+1), true);
Chris Lattner00950542001-06-06 20:29:01 +00001144 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00001145 Out << "\n\t]";
Chris Lattnerb00c5822001-10-02 03:41:24 +00001146 } else if (isa<PHINode>(I)) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001147 Out << ' ';
Chris Lattner7e708292002-06-25 16:13:24 +00001148 printType(I.getType());
Misha Brukman0313e0b2004-06-21 21:53:56 +00001149 Out << ' ';
Chris Lattner00950542001-06-06 20:29:01 +00001150
Chris Lattner7e708292002-06-25 16:13:24 +00001151 for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001152 if (op) Out << ", ";
Misha Brukmanfd939082005-04-21 23:48:37 +00001153 Out << '[';
Misha Brukman0313e0b2004-06-21 21:53:56 +00001154 writeOperand(I.getOperand(op ), false); Out << ',';
1155 writeOperand(I.getOperand(op+1), false); Out << " ]";
Chris Lattnerc24d2082001-06-11 15:04:20 +00001156 }
Chris Lattnere02fa852001-10-13 06:42:36 +00001157 } else if (isa<ReturnInst>(I) && !Operand) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001158 Out << " void";
Chris Lattnerd5118982005-05-06 20:26:43 +00001159 } else if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
1160 // Print the calling convention being used.
1161 switch (CI->getCallingConv()) {
1162 case CallingConv::C: break; // default
Chris Lattner0deaab82006-05-19 21:58:52 +00001163 case CallingConv::Fast: Out << " fastcc"; break;
1164 case CallingConv::Cold: Out << " coldcc"; break;
Anton Korobeynikovf8248682006-09-20 22:03:51 +00001165 case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break;
1166 case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break;
Chris Lattnerd5118982005-05-06 20:26:43 +00001167 default: Out << " cc" << CI->getCallingConv(); break;
1168 }
1169
Reid Spencerb138a062007-04-09 06:10:42 +00001170 const PointerType *PTy = cast<PointerType>(Operand->getType());
1171 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1172 const Type *RetTy = FTy->getReturnType();
1173 const ParamAttrsList *PAL = FTy->getParamAttrs();
Chris Lattner268de042001-11-06 21:28:12 +00001174
Chris Lattner7a012292003-08-05 15:34:45 +00001175 // If possible, print out the short form of the call instruction. We can
Chris Lattnerb5794002002-04-07 22:49:37 +00001176 // only do this if the first argument is a pointer to a nonvararg function,
Chris Lattner7a012292003-08-05 15:34:45 +00001177 // and if the return type is not a pointer to a function.
Chris Lattner268de042001-11-06 21:28:12 +00001178 //
Chris Lattner7a012292003-08-05 15:34:45 +00001179 if (!FTy->isVarArg() &&
Misha Brukmanfd939082005-04-21 23:48:37 +00001180 (!isa<PointerType>(RetTy) ||
Chris Lattnerc1b27182002-07-25 20:58:51 +00001181 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001182 Out << ' '; printType(RetTy);
Chris Lattner268de042001-11-06 21:28:12 +00001183 writeOperand(Operand, false);
1184 } else {
1185 writeOperand(Operand, true);
1186 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00001187 Out << '(';
Reid Spencerbd5db8e2006-12-31 05:24:50 +00001188 for (unsigned op = 1, Eop = I.getNumOperands(); op < Eop; ++op) {
1189 if (op > 1)
1190 Out << ',';
Chris Lattner7e708292002-06-25 16:13:24 +00001191 writeOperand(I.getOperand(op), true);
Reid Spencer18da0722007-04-11 02:44:20 +00001192 if (PAL && PAL->getParamAttrs(op) != ParamAttr::None)
Reid Spencerb138a062007-04-09 06:10:42 +00001193 Out << " " << PAL->getParamAttrsTextByIndex(op);
Chris Lattner00950542001-06-06 20:29:01 +00001194 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00001195 Out << " )";
Reid Spencer18da0722007-04-11 02:44:20 +00001196 if (PAL && PAL->getParamAttrs(0) != ParamAttr::None)
Reid Spencerb138a062007-04-09 06:10:42 +00001197 Out << ' ' << PAL->getParamAttrsTextByIndex(0);
Chris Lattner7e708292002-06-25 16:13:24 +00001198 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Reid Spencerb138a062007-04-09 06:10:42 +00001199 const PointerType *PTy = cast<PointerType>(Operand->getType());
1200 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1201 const Type *RetTy = FTy->getReturnType();
1202 const ParamAttrsList *PAL = FTy->getParamAttrs();
Chris Lattner7a012292003-08-05 15:34:45 +00001203
Chris Lattnerd5118982005-05-06 20:26:43 +00001204 // Print the calling convention being used.
1205 switch (II->getCallingConv()) {
1206 case CallingConv::C: break; // default
Chris Lattner0deaab82006-05-19 21:58:52 +00001207 case CallingConv::Fast: Out << " fastcc"; break;
1208 case CallingConv::Cold: Out << " coldcc"; break;
Anton Korobeynikovf8248682006-09-20 22:03:51 +00001209 case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break;
1210 case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break;
Chris Lattnerd5118982005-05-06 20:26:43 +00001211 default: Out << " cc" << II->getCallingConv(); break;
1212 }
1213
Chris Lattner7a012292003-08-05 15:34:45 +00001214 // If possible, print out the short form of the invoke instruction. We can
1215 // only do this if the first argument is a pointer to a nonvararg function,
1216 // and if the return type is not a pointer to a function.
1217 //
1218 if (!FTy->isVarArg() &&
Misha Brukmanfd939082005-04-21 23:48:37 +00001219 (!isa<PointerType>(RetTy) ||
Chris Lattner7a012292003-08-05 15:34:45 +00001220 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001221 Out << ' '; printType(RetTy);
Chris Lattner7a012292003-08-05 15:34:45 +00001222 writeOperand(Operand, false);
1223 } else {
1224 writeOperand(Operand, true);
1225 }
1226
Misha Brukman0313e0b2004-06-21 21:53:56 +00001227 Out << '(';
Reid Spencerbd5db8e2006-12-31 05:24:50 +00001228 for (unsigned op = 3, Eop = I.getNumOperands(); op < Eop; ++op) {
1229 if (op > 3)
1230 Out << ',';
Chris Lattner7e708292002-06-25 16:13:24 +00001231 writeOperand(I.getOperand(op), true);
Reid Spencer18da0722007-04-11 02:44:20 +00001232 if (PAL && PAL->getParamAttrs(op-2) != ParamAttr::None)
Reid Spencerb138a062007-04-09 06:10:42 +00001233 Out << " " << PAL->getParamAttrsTextByIndex(op-2);
Chris Lattnere02fa852001-10-13 06:42:36 +00001234 }
1235
Reid Spencer2c261782007-01-05 17:06:19 +00001236 Out << " )";
Reid Spencer18da0722007-04-11 02:44:20 +00001237 if (PAL && PAL->getParamAttrs(0) != ParamAttr::None)
Reid Spencerb138a062007-04-09 06:10:42 +00001238 Out << " " << PAL->getParamAttrsTextByIndex(0);
Reid Spencer2c261782007-01-05 17:06:19 +00001239 Out << "\n\t\t\tto";
Chris Lattnere02fa852001-10-13 06:42:36 +00001240 writeOperand(II->getNormalDest(), true);
Misha Brukman0313e0b2004-06-21 21:53:56 +00001241 Out << " unwind";
Chris Lattneraeb2a1d2004-02-08 21:44:31 +00001242 writeOperand(II->getUnwindDest(), true);
Chris Lattnere02fa852001-10-13 06:42:36 +00001243
Chris Lattner7e708292002-06-25 16:13:24 +00001244 } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(&I)) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001245 Out << ' ';
Chris Lattner94dc1f22002-04-13 18:34:38 +00001246 printType(AI->getType()->getElementType());
1247 if (AI->isArrayAllocation()) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001248 Out << ',';
Chris Lattner94dc1f22002-04-13 18:34:38 +00001249 writeOperand(AI->getArraySize(), true);
Chris Lattner00950542001-06-06 20:29:01 +00001250 }
Nate Begeman14b05292005-11-05 09:21:28 +00001251 if (AI->getAlignment()) {
Chris Lattner9fad0b92005-11-05 21:20:34 +00001252 Out << ", align " << AI->getAlignment();
Nate Begeman14b05292005-11-05 09:21:28 +00001253 }
Chris Lattnere02fa852001-10-13 06:42:36 +00001254 } else if (isa<CastInst>(I)) {
Chris Lattner41495a22003-11-17 01:17:04 +00001255 if (Operand) writeOperand(Operand, true); // Work with broken code
Misha Brukman0313e0b2004-06-21 21:53:56 +00001256 Out << " to ";
Chris Lattner7e708292002-06-25 16:13:24 +00001257 printType(I.getType());
Chris Lattner4d45bd02003-10-18 05:57:43 +00001258 } else if (isa<VAArgInst>(I)) {
Chris Lattner41495a22003-11-17 01:17:04 +00001259 if (Operand) writeOperand(Operand, true); // Work with broken code
Misha Brukman0313e0b2004-06-21 21:53:56 +00001260 Out << ", ";
Chris Lattner8f77dae2003-05-08 02:44:12 +00001261 printType(I.getType());
Chris Lattner00950542001-06-06 20:29:01 +00001262 } else if (Operand) { // Print the normal way...
1263
Misha Brukmanfd939082005-04-21 23:48:37 +00001264 // PrintAllTypes - Instructions who have operands of all the same type
Chris Lattner00950542001-06-06 20:29:01 +00001265 // omit the type from all but the first operand. If the instruction has
1266 // different type operands (for example br), then they are all printed.
1267 bool PrintAllTypes = false;
1268 const Type *TheType = Operand->getType();
Chris Lattner00950542001-06-06 20:29:01 +00001269
Reid Spencerebe57e32007-02-02 13:54:55 +00001270 // Select, Store and ShuffleVector always print all types.
Reid Spencer832254e2007-02-02 02:16:23 +00001271 if (isa<SelectInst>(I) || isa<StoreInst>(I) || isa<ShuffleVectorInst>(I)) {
Chris Lattnerffd9bf42003-04-16 20:20:02 +00001272 PrintAllTypes = true;
1273 } else {
1274 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
1275 Operand = I.getOperand(i);
1276 if (Operand->getType() != TheType) {
1277 PrintAllTypes = true; // We have differing types! Print them all!
1278 break;
1279 }
Chris Lattner00950542001-06-06 20:29:01 +00001280 }
1281 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001282
Chris Lattnerc1824992001-10-29 16:05:51 +00001283 if (!PrintAllTypes) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001284 Out << ' ';
Chris Lattnerffd9bf42003-04-16 20:20:02 +00001285 printType(TheType);
Chris Lattnerc1824992001-10-29 16:05:51 +00001286 }
Chris Lattner00950542001-06-06 20:29:01 +00001287
Chris Lattner7e708292002-06-25 16:13:24 +00001288 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001289 if (i) Out << ',';
Chris Lattner7e708292002-06-25 16:13:24 +00001290 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner00950542001-06-06 20:29:01 +00001291 }
1292 }
1293
Chris Lattnere02fa852001-10-13 06:42:36 +00001294 printInfoComment(I);
Misha Brukman0313e0b2004-06-21 21:53:56 +00001295 Out << "\n";
Chris Lattner00950542001-06-06 20:29:01 +00001296}
1297
1298
1299//===----------------------------------------------------------------------===//
1300// External Interface declarations
1301//===----------------------------------------------------------------------===//
1302
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001303void Module::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001304 SlotMachine SlotTable(this);
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001305 AssemblyWriter W(o, SlotTable, this, AAW);
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001306 W.write(this);
Chris Lattner00950542001-06-06 20:29:01 +00001307}
1308
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001309void GlobalVariable::print(std::ostream &o) const {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001310 SlotMachine SlotTable(getParent());
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001311 AssemblyWriter W(o, SlotTable, getParent(), 0);
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001312 W.write(this);
Chris Lattnerb0e45232001-09-10 20:08:19 +00001313}
1314
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001315void Function::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001316 SlotMachine SlotTable(getParent());
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001317 AssemblyWriter W(o, SlotTable, getParent(), AAW);
Chris Lattner00950542001-06-06 20:29:01 +00001318
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001319 W.write(this);
Chris Lattner00950542001-06-06 20:29:01 +00001320}
1321
Chris Lattnercc041ba2006-01-24 04:13:11 +00001322void InlineAsm::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Chris Lattner2fcfdb72006-12-06 06:24:27 +00001323 WriteAsOperand(o, this, true, 0);
Chris Lattnercc041ba2006-01-24 04:13:11 +00001324}
1325
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001326void BasicBlock::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001327 SlotMachine SlotTable(getParent());
Misha Brukmanfd939082005-04-21 23:48:37 +00001328 AssemblyWriter W(o, SlotTable,
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001329 getParent() ? getParent()->getParent() : 0, AAW);
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001330 W.write(this);
Chris Lattner00950542001-06-06 20:29:01 +00001331}
1332
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001333void Instruction::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001334 const Function *F = getParent() ? getParent()->getParent() : 0;
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001335 SlotMachine SlotTable(F);
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001336 AssemblyWriter W(o, SlotTable, F ? F->getParent() : 0, AAW);
Chris Lattner00950542001-06-06 20:29:01 +00001337
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001338 W.write(this);
Chris Lattner00950542001-06-06 20:29:01 +00001339}
Chris Lattnerda1fbcc2001-11-07 04:21:57 +00001340
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001341void Constant::print(std::ostream &o) const {
1342 if (this == 0) { o << "<null> constant value\n"; return; }
Chris Lattner3bc06b32002-09-10 15:53:49 +00001343
Misha Brukman40c732c2004-06-04 21:11:51 +00001344 o << ' ' << getType()->getDescription() << ' ';
Evan Chenga4ffcc22006-03-01 22:17:00 +00001345
1346 std::map<const Type *, std::string> TypeTable;
Chris Lattner2fcfdb72006-12-06 06:24:27 +00001347 WriteConstantInt(o, this, TypeTable, 0);
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001348}
1349
Misha Brukmanfd939082005-04-21 23:48:37 +00001350void Type::print(std::ostream &o) const {
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001351 if (this == 0)
1352 o << "<null Type>";
1353 else
1354 o << getDescription();
1355}
1356
Chris Lattner73e21422002-04-09 19:48:49 +00001357void Argument::print(std::ostream &o) const {
Chris Lattner2fcfdb72006-12-06 06:24:27 +00001358 WriteAsOperand(o, this, true, getParent() ? getParent()->getParent() : 0);
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001359}
1360
Reid Spencerfa452c02004-05-25 18:14:38 +00001361// Value::dump - allow easy printing of Values from the debugger.
1362// Located here because so much of the needed functionality is here.
Bill Wendling832171c2006-12-07 20:04:42 +00001363void Value::dump() const { print(*cerr.stream()); cerr << '\n'; }
Reid Spencerfa452c02004-05-25 18:14:38 +00001364
1365// Type::dump - allow easy printing of Values from the debugger.
1366// Located here because so much of the needed functionality is here.
Bill Wendling832171c2006-12-07 20:04:42 +00001367void Type::dump() const { print(*cerr.stream()); cerr << '\n'; }
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001368
1369//===----------------------------------------------------------------------===//
Chris Lattner82c4bc72006-12-06 06:40:49 +00001370// SlotMachine Implementation
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001371//===----------------------------------------------------------------------===//
1372
1373#if 0
Bill Wendlinge8156192006-12-07 01:30:32 +00001374#define SC_DEBUG(X) cerr << X
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001375#else
1376#define SC_DEBUG(X)
1377#endif
1378
1379// Module level constructor. Causes the contents of the Module (sans functions)
1380// to be added to the slot table.
Misha Brukmanfd939082005-04-21 23:48:37 +00001381SlotMachine::SlotMachine(const Module *M)
Reid Spencerb03de0c2004-05-26 21:56:09 +00001382 : TheModule(M) ///< Saved for lazy initialization.
1383 , TheFunction(0)
Reid Spencer28531c72004-08-16 07:46:33 +00001384 , FunctionProcessed(false)
Reid Spencer590b3c52007-03-19 18:32:53 +00001385 , mMap(), mNext(0), fMap(), fNext(0)
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001386{
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001387}
1388
1389// Function level constructor. Causes the contents of the Module and the one
1390// function provided to be added to the slot table.
Chris Lattnerc96ce892006-12-06 05:12:21 +00001391SlotMachine::SlotMachine(const Function *F)
1392 : TheModule(F ? F->getParent() : 0) ///< Saved for lazy initialization
Reid Spencerb03de0c2004-05-26 21:56:09 +00001393 , TheFunction(F) ///< Saved for lazy initialization
Reid Spencer28531c72004-08-16 07:46:33 +00001394 , FunctionProcessed(false)
Reid Spencer590b3c52007-03-19 18:32:53 +00001395 , mMap(), mNext(0), fMap(), fNext(0)
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001396{
Reid Spencerb03de0c2004-05-26 21:56:09 +00001397}
1398
Chris Lattner22379bc2007-01-11 03:54:27 +00001399inline void SlotMachine::initialize() {
Chris Lattnerc96ce892006-12-06 05:12:21 +00001400 if (TheModule) {
Misha Brukmanfd939082005-04-21 23:48:37 +00001401 processModule();
Reid Spencerb03de0c2004-05-26 21:56:09 +00001402 TheModule = 0; ///< Prevent re-processing next time we're called.
1403 }
Chris Lattnerde891a62006-12-06 05:27:40 +00001404 if (TheFunction && !FunctionProcessed)
Misha Brukmanfd939082005-04-21 23:48:37 +00001405 processFunction();
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001406}
1407
1408// Iterate through all the global variables, functions, and global
Misha Brukmanfd939082005-04-21 23:48:37 +00001409// variable initializers and create slots for them.
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001410void SlotMachine::processModule() {
1411 SC_DEBUG("begin processModule!\n");
1412
Chris Lattnera6b1ffc2006-12-06 06:15:43 +00001413 // Add all of the unnamed global variables to the value table.
Chris Lattnerd6d826c2006-12-06 04:41:52 +00001414 for (Module::const_global_iterator I = TheModule->global_begin(),
1415 E = TheModule->global_end(); I != E; ++I)
Chris Lattnera6b1ffc2006-12-06 06:15:43 +00001416 if (!I->hasName())
Chris Lattner9446bbe2007-01-09 07:55:49 +00001417 CreateModuleSlot(I);
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001418
Chris Lattnera6b1ffc2006-12-06 06:15:43 +00001419 // Add all the unnamed functions to the table.
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001420 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
1421 I != E; ++I)
Chris Lattnera6b1ffc2006-12-06 06:15:43 +00001422 if (!I->hasName())
Chris Lattner9446bbe2007-01-09 07:55:49 +00001423 CreateModuleSlot(I);
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001424
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001425 SC_DEBUG("end processModule!\n");
1426}
1427
1428
Reid Spencerb03de0c2004-05-26 21:56:09 +00001429// Process the arguments, basic blocks, and instructions of a function.
1430void SlotMachine::processFunction() {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001431 SC_DEBUG("begin processFunction!\n");
Reid Spencer590b3c52007-03-19 18:32:53 +00001432 fNext = 0;
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001433
Chris Lattnera6b1ffc2006-12-06 06:15:43 +00001434 // Add all the function arguments with no names.
Misha Brukmanfd939082005-04-21 23:48:37 +00001435 for(Function::const_arg_iterator AI = TheFunction->arg_begin(),
Chris Lattnere4d5c442005-03-15 04:54:21 +00001436 AE = TheFunction->arg_end(); AI != AE; ++AI)
Chris Lattnera6b1ffc2006-12-06 06:15:43 +00001437 if (!AI->hasName())
Chris Lattner9446bbe2007-01-09 07:55:49 +00001438 CreateFunctionSlot(AI);
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001439
1440 SC_DEBUG("Inserting Instructions:\n");
1441
Chris Lattnera6b1ffc2006-12-06 06:15:43 +00001442 // Add all of the basic blocks and instructions with no names.
Misha Brukmanfd939082005-04-21 23:48:37 +00001443 for (Function::const_iterator BB = TheFunction->begin(),
Reid Spencerb03de0c2004-05-26 21:56:09 +00001444 E = TheFunction->end(); BB != E; ++BB) {
Chris Lattnera6b1ffc2006-12-06 06:15:43 +00001445 if (!BB->hasName())
Chris Lattner9446bbe2007-01-09 07:55:49 +00001446 CreateFunctionSlot(BB);
Chris Lattnera6b1ffc2006-12-06 06:15:43 +00001447 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
1448 if (I->getType() != Type::VoidTy && !I->hasName())
Chris Lattner9446bbe2007-01-09 07:55:49 +00001449 CreateFunctionSlot(I);
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001450 }
1451
Reid Spencer28531c72004-08-16 07:46:33 +00001452 FunctionProcessed = true;
1453
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001454 SC_DEBUG("end processFunction!\n");
1455}
1456
Chris Lattnerde891a62006-12-06 05:27:40 +00001457/// Clean up after incorporating a function. This is the only way to get out of
Chris Lattner22379bc2007-01-11 03:54:27 +00001458/// the function incorporation state that affects get*Slot/Create*Slot. Function
Chris Lattner3a4621c2007-01-09 07:46:21 +00001459/// incorporation state is indicated by TheFunction != 0.
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001460void SlotMachine::purgeFunction() {
1461 SC_DEBUG("begin purgeFunction!\n");
1462 fMap.clear(); // Simply discard the function level map
Reid Spencerb03de0c2004-05-26 21:56:09 +00001463 TheFunction = 0;
Reid Spencer28531c72004-08-16 07:46:33 +00001464 FunctionProcessed = false;
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001465 SC_DEBUG("end purgeFunction!\n");
1466}
1467
Chris Lattner22379bc2007-01-11 03:54:27 +00001468/// getGlobalSlot - Get the slot number of a global value.
1469int SlotMachine::getGlobalSlot(const GlobalValue *V) {
1470 // Check for uninitialized state and do lazy initialization.
1471 initialize();
1472
1473 // Find the type plane in the module map
Reid Spencer590b3c52007-03-19 18:32:53 +00001474 ValueMap::const_iterator MI = mMap.find(V);
Chris Lattner22379bc2007-01-11 03:54:27 +00001475 if (MI == mMap.end()) return -1;
Reid Spencer590b3c52007-03-19 18:32:53 +00001476
1477 return MI->second;
Chris Lattner22379bc2007-01-11 03:54:27 +00001478}
Reid Spencerb03de0c2004-05-26 21:56:09 +00001479
Chris Lattner22379bc2007-01-11 03:54:27 +00001480
1481/// getLocalSlot - Get the slot number for a value that is local to a function.
1482int SlotMachine::getLocalSlot(const Value *V) {
1483 assert(!isa<Constant>(V) && "Can't get a constant or global slot with this!");
1484
1485 // Check for uninitialized state and do lazy initialization.
1486 initialize();
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001487
Reid Spencer590b3c52007-03-19 18:32:53 +00001488 ValueMap::const_iterator FI = fMap.find(V);
Chris Lattner22379bc2007-01-11 03:54:27 +00001489 if (FI == fMap.end()) return -1;
1490
Reid Spencer590b3c52007-03-19 18:32:53 +00001491 return FI->second;
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001492}
1493
Reid Spencer0e25e1c2004-07-04 11:50:43 +00001494
Chris Lattner9446bbe2007-01-09 07:55:49 +00001495/// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
1496void SlotMachine::CreateModuleSlot(const GlobalValue *V) {
Chris Lattnercf8790a2007-01-09 08:04:59 +00001497 assert(V && "Can't insert a null Value into SlotMachine!");
Reid Spencer590b3c52007-03-19 18:32:53 +00001498 assert(V->getType() != Type::VoidTy && "Doesn't need a slot!");
1499 assert(!V->hasName() && "Doesn't need a slot!");
Chris Lattnercf8790a2007-01-09 08:04:59 +00001500
Reid Spencer590b3c52007-03-19 18:32:53 +00001501 unsigned DestSlot = mNext++;
1502 mMap[V] = DestSlot;
Chris Lattnercf8790a2007-01-09 08:04:59 +00001503
Reid Spencer590b3c52007-03-19 18:32:53 +00001504 SC_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
Chris Lattnercf8790a2007-01-09 08:04:59 +00001505 DestSlot << " [");
1506 // G = Global, F = Function, o = other
1507 SC_DEBUG((isa<GlobalVariable>(V) ? 'G' : 'F') << "]\n");
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001508}
1509
1510
Chris Lattner9446bbe2007-01-09 07:55:49 +00001511/// CreateSlot - Create a new slot for the specified value if it has no name.
1512void SlotMachine::CreateFunctionSlot(const Value *V) {
1513 const Type *VTy = V->getType();
1514 assert(VTy != Type::VoidTy && !V->hasName() && "Doesn't need a slot!");
Chris Lattnercf8790a2007-01-09 08:04:59 +00001515
Reid Spencer590b3c52007-03-19 18:32:53 +00001516 unsigned DestSlot = fNext++;
1517 fMap[V] = DestSlot;
Chris Lattnercf8790a2007-01-09 08:04:59 +00001518
Chris Lattner4932a5a2006-12-06 05:55:41 +00001519 // G = Global, F = Function, o = other
Chris Lattnercf8790a2007-01-09 08:04:59 +00001520 SC_DEBUG(" Inserting value [" << VTy << "] = " << V << " slot=" <<
1521 DestSlot << " [o]\n");
1522}