blob: 536b541dfa195c9bea77fb481339ff5b2b44abd2 [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
Devang Patel19974732007-05-03 01:11:54 +0000138char PrintModulePass::ID = 0;
Chris Lattner7f8897f2006-08-27 22:42:52 +0000139static RegisterPass<PrintModulePass>
Chris Lattner3dd965c2006-08-21 17:20:01 +0000140X("printm", "Print module to stderr");
Devang Patel19974732007-05-03 01:11:54 +0000141char PrintFunctionPass::ID = 0;
Chris Lattner7f8897f2006-08-27 22:42:52 +0000142static RegisterPass<PrintFunctionPass>
Chris Lattner3dd965c2006-08-21 17:20:01 +0000143Y("print","Print function to stderr");
Chris Lattnerf082b802002-07-23 18:07:49 +0000144
Misha Brukmanfd939082005-04-21 23:48:37 +0000145static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
Chris Lattner919e70c2006-12-06 05:50:41 +0000146 std::map<const Type *, std::string> &TypeTable,
Reid Spencer0e25e1c2004-07-04 11:50:43 +0000147 SlotMachine *Machine);
148
Chris Lattner207b5bc2001-10-29 16:37:48 +0000149static const Module *getModuleFromVal(const Value *V) {
Chris Lattner949a3622003-07-23 15:30:06 +0000150 if (const Argument *MA = dyn_cast<Argument>(V))
Chris Lattner207b5bc2001-10-29 16:37:48 +0000151 return MA->getParent() ? MA->getParent()->getParent() : 0;
Chris Lattner949a3622003-07-23 15:30:06 +0000152 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
Chris Lattner207b5bc2001-10-29 16:37:48 +0000153 return BB->getParent() ? BB->getParent()->getParent() : 0;
Chris Lattner949a3622003-07-23 15:30:06 +0000154 else if (const Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000155 const Function *M = I->getParent() ? I->getParent()->getParent() : 0;
Chris Lattner207b5bc2001-10-29 16:37:48 +0000156 return M ? M->getParent() : 0;
Chris Lattner949a3622003-07-23 15:30:06 +0000157 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
Chris Lattner207b5bc2001-10-29 16:37:48 +0000158 return GV->getParent();
Chris Lattner207b5bc2001-10-29 16:37:48 +0000159 return 0;
160}
161
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000162static SlotMachine *createSlotMachine(const Value *V) {
Chris Lattner949a3622003-07-23 15:30:06 +0000163 if (const Argument *FA = dyn_cast<Argument>(V)) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000164 return new SlotMachine(FA->getParent());
Chris Lattner949a3622003-07-23 15:30:06 +0000165 } else if (const Instruction *I = dyn_cast<Instruction>(V)) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000166 return new SlotMachine(I->getParent()->getParent());
Chris Lattner949a3622003-07-23 15:30:06 +0000167 } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000168 return new SlotMachine(BB->getParent());
Chris Lattner949a3622003-07-23 15:30:06 +0000169 } else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)){
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000170 return new SlotMachine(GV->getParent());
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000171 } else if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V)){
172 return new SlotMachine(GA->getParent());
Chris Lattner949a3622003-07-23 15:30:06 +0000173 } else if (const Function *Func = dyn_cast<Function>(V)) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000174 return new SlotMachine(Func);
Chris Lattnerc1824992001-10-29 16:05:51 +0000175 }
176 return 0;
177}
Chris Lattner00950542001-06-06 20:29:01 +0000178
Reid Spencer3702d262007-01-26 08:02:52 +0000179/// NameNeedsQuotes - Return true if the specified llvm name should be wrapped
180/// with ""'s.
181static bool NameNeedsQuotes(const std::string &Name) {
182 if (Name[0] >= '0' && Name[0] <= '9') return true;
Chris Lattner24b8a5d2003-08-22 05:40:38 +0000183 // Scan to see if we have any characters that are not on the "white list"
184 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
185 char C = Name[i];
186 assert(C != '"' && "Illegal character in LLVM value name!");
187 if ((C < 'a' || C > 'z') && (C < 'A' || C > 'Z') && (C < '0' || C > '9') &&
188 C != '-' && C != '.' && C != '_')
Reid Spencer3702d262007-01-26 08:02:52 +0000189 return true;
190 }
191 return false;
192}
193
194enum PrefixType {
195 GlobalPrefix,
196 LabelPrefix,
197 LocalPrefix
198};
199
200/// getLLVMName - Turn the specified string into an 'LLVM name', which is either
201/// prefixed with % (if the string only contains simple characters) or is
202/// surrounded with ""'s (if it has special chars in it).
203static std::string getLLVMName(const std::string &Name, PrefixType Prefix) {
204 assert(!Name.empty() && "Cannot get empty name!");
205
206 // First character cannot start with a number...
207 if (NameNeedsQuotes(Name)) {
208 if (Prefix == GlobalPrefix)
209 return "@\"" + Name + "\"";
210 return "\"" + Name + "\"";
Chris Lattner24b8a5d2003-08-22 05:40:38 +0000211 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000212
Chris Lattner24b8a5d2003-08-22 05:40:38 +0000213 // If we get here, then the identifier is legal to use as a "VarID".
Reid Spencer3702d262007-01-26 08:02:52 +0000214 switch (Prefix) {
215 default: assert(0 && "Bad prefix!");
216 case GlobalPrefix: return '@' + Name;
217 case LabelPrefix: return Name;
218 case LocalPrefix: return '%' + Name;
219 }
Chris Lattner24b8a5d2003-08-22 05:40:38 +0000220}
221
Chris Lattner207b5bc2001-10-29 16:37:48 +0000222
Misha Brukmanab5c6002004-03-02 00:22:19 +0000223/// fillTypeNameTable - If the module has a symbol table, take all global types
224/// and stuff their names into the TypeNames map.
225///
Chris Lattner207b5bc2001-10-29 16:37:48 +0000226static void fillTypeNameTable(const Module *M,
Chris Lattner7b13f562003-05-08 02:08:14 +0000227 std::map<const Type *, std::string> &TypeNames) {
Chris Lattner6e6026b2002-11-20 18:36:02 +0000228 if (!M) return;
Reid Spencer78d033e2007-01-06 07:24:44 +0000229 const TypeSymbolTable &ST = M->getTypeSymbolTable();
230 TypeSymbolTable::const_iterator TI = ST.begin();
231 for (; TI != ST.end(); ++TI) {
Reid Spencer9231ac82004-05-25 08:53:40 +0000232 // As a heuristic, don't insert pointer to primitive types, because
233 // they are used too often to have a single useful name.
234 //
235 const Type *Ty = cast<Type>(TI->second);
236 if (!isa<PointerType>(Ty) ||
Reid Spencerb03de0c2004-05-26 21:56:09 +0000237 !cast<PointerType>(Ty)->getElementType()->isPrimitiveType() ||
Chris Lattner42a75512007-01-15 02:27:26 +0000238 !cast<PointerType>(Ty)->getElementType()->isInteger() ||
Reid Spencerb03de0c2004-05-26 21:56:09 +0000239 isa<OpaqueType>(cast<PointerType>(Ty)->getElementType()))
Reid Spencer3702d262007-01-26 08:02:52 +0000240 TypeNames.insert(std::make_pair(Ty, getLLVMName(TI->first, LocalPrefix)));
Chris Lattner207b5bc2001-10-29 16:37:48 +0000241 }
242}
243
244
245
Misha Brukmanfd939082005-04-21 23:48:37 +0000246static void calcTypeName(const Type *Ty,
John Criswell4ff620a2004-06-01 14:54:08 +0000247 std::vector<const Type *> &TypeStack,
248 std::map<const Type *, std::string> &TypeNames,
249 std::string & Result){
Chris Lattner42a75512007-01-15 02:27:26 +0000250 if (Ty->isInteger() || (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty))) {
John Criswell4ff620a2004-06-01 14:54:08 +0000251 Result += Ty->getDescription(); // Base case
252 return;
253 }
Chris Lattner207b5bc2001-10-29 16:37:48 +0000254
255 // Check to see if the type is named.
Chris Lattner7b13f562003-05-08 02:08:14 +0000256 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
John Criswell4ff620a2004-06-01 14:54:08 +0000257 if (I != TypeNames.end()) {
258 Result += I->second;
259 return;
260 }
Chris Lattner207b5bc2001-10-29 16:37:48 +0000261
John Criswell4ff620a2004-06-01 14:54:08 +0000262 if (isa<OpaqueType>(Ty)) {
263 Result += "opaque";
264 return;
265 }
Chris Lattner88c17382003-10-30 00:22:33 +0000266
Chris Lattner207b5bc2001-10-29 16:37:48 +0000267 // Check to see if the Type is already on the stack...
268 unsigned Slot = 0, CurSize = TypeStack.size();
269 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
270
Misha Brukmanfd939082005-04-21 23:48:37 +0000271 // This is another base case for the recursion. In this case, we know
Chris Lattner207b5bc2001-10-29 16:37:48 +0000272 // that we have looped back to a type that we have previously visited.
273 // Generate the appropriate upreference to handle this.
John Criswell4ff620a2004-06-01 14:54:08 +0000274 if (Slot < CurSize) {
275 Result += "\\" + utostr(CurSize-Slot); // Here's the upreference
276 return;
277 }
Chris Lattner207b5bc2001-10-29 16:37:48 +0000278
279 TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
Misha Brukmanfd939082005-04-21 23:48:37 +0000280
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000281 switch (Ty->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000282 case Type::IntegerTyID: {
283 unsigned BitWidth = cast<IntegerType>(Ty)->getBitWidth();
Reid Spencer15ee9352007-01-12 07:25:20 +0000284 Result += "i" + utostr(BitWidth);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000285 break;
286 }
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000287 case Type::FunctionTyID: {
Chris Lattner949a3622003-07-23 15:30:06 +0000288 const FunctionType *FTy = cast<FunctionType>(Ty);
John Criswell4ff620a2004-06-01 14:54:08 +0000289 calcTypeName(FTy->getReturnType(), TypeStack, TypeNames, Result);
290 Result += " (";
Reid Spencerbd5db8e2006-12-31 05:24:50 +0000291 unsigned Idx = 1;
Reid Spencerb138a062007-04-09 06:10:42 +0000292 const ParamAttrsList *Attrs = FTy->getParamAttrs();
Chris Lattnerd5d89962004-02-09 04:14:01 +0000293 for (FunctionType::param_iterator I = FTy->param_begin(),
294 E = FTy->param_end(); I != E; ++I) {
295 if (I != FTy->param_begin())
Chris Lattner207b5bc2001-10-29 16:37:48 +0000296 Result += ", ";
John Criswell4ff620a2004-06-01 14:54:08 +0000297 calcTypeName(*I, TypeStack, TypeNames, Result);
Reid Spencer18da0722007-04-11 02:44:20 +0000298 if (Attrs && Attrs->getParamAttrs(Idx) != ParamAttr::None) {
Reid Spencerbd5db8e2006-12-31 05:24:50 +0000299 Result += + " ";
Reid Spencerb138a062007-04-09 06:10:42 +0000300 Result += Attrs->getParamAttrsTextByIndex(Idx);
Reid Spencerbd5db8e2006-12-31 05:24:50 +0000301 }
302 Idx++;
Chris Lattner207b5bc2001-10-29 16:37:48 +0000303 }
Chris Lattner2761e9f2002-04-13 20:53:41 +0000304 if (FTy->isVarArg()) {
Chris Lattnerd5d89962004-02-09 04:14:01 +0000305 if (FTy->getNumParams()) Result += ", ";
Chris Lattner207b5bc2001-10-29 16:37:48 +0000306 Result += "...";
307 }
308 Result += ")";
Reid Spencer18da0722007-04-11 02:44:20 +0000309 if (Attrs && Attrs->getParamAttrs(0) != ParamAttr::None) {
Reid Spencer2c261782007-01-05 17:06:19 +0000310 Result += " ";
Reid Spencerb138a062007-04-09 06:10:42 +0000311 Result += Attrs->getParamAttrsTextByIndex(0);
Reid Spencer2c261782007-01-05 17:06:19 +0000312 }
Chris Lattner207b5bc2001-10-29 16:37:48 +0000313 break;
314 }
315 case Type::StructTyID: {
Chris Lattner949a3622003-07-23 15:30:06 +0000316 const StructType *STy = cast<StructType>(Ty);
Andrew Lenharth38ecbf12006-12-08 18:06:16 +0000317 if (STy->isPacked())
318 Result += '<';
John Criswell4ff620a2004-06-01 14:54:08 +0000319 Result += "{ ";
Chris Lattnerd21cd802004-02-09 04:37:31 +0000320 for (StructType::element_iterator I = STy->element_begin(),
321 E = STy->element_end(); I != E; ++I) {
322 if (I != STy->element_begin())
Chris Lattner207b5bc2001-10-29 16:37:48 +0000323 Result += ", ";
John Criswell4ff620a2004-06-01 14:54:08 +0000324 calcTypeName(*I, TypeStack, TypeNames, Result);
Chris Lattner207b5bc2001-10-29 16:37:48 +0000325 }
326 Result += " }";
Andrew Lenharth38ecbf12006-12-08 18:06:16 +0000327 if (STy->isPacked())
328 Result += '>';
Chris Lattner207b5bc2001-10-29 16:37:48 +0000329 break;
330 }
331 case Type::PointerTyID:
Misha Brukmanfd939082005-04-21 23:48:37 +0000332 calcTypeName(cast<PointerType>(Ty)->getElementType(),
John Criswell4ff620a2004-06-01 14:54:08 +0000333 TypeStack, TypeNames, Result);
334 Result += "*";
Chris Lattner207b5bc2001-10-29 16:37:48 +0000335 break;
336 case Type::ArrayTyID: {
Chris Lattner949a3622003-07-23 15:30:06 +0000337 const ArrayType *ATy = cast<ArrayType>(Ty);
John Criswell4ff620a2004-06-01 14:54:08 +0000338 Result += "[" + utostr(ATy->getNumElements()) + " x ";
339 calcTypeName(ATy->getElementType(), TypeStack, TypeNames, Result);
340 Result += "]";
Chris Lattner207b5bc2001-10-29 16:37:48 +0000341 break;
342 }
Reid Spencer9d6565a2007-02-15 02:26:10 +0000343 case Type::VectorTyID: {
344 const VectorType *PTy = cast<VectorType>(Ty);
Brian Gaeke715c90b2004-08-20 06:00:58 +0000345 Result += "<" + utostr(PTy->getNumElements()) + " x ";
346 calcTypeName(PTy->getElementType(), TypeStack, TypeNames, Result);
347 Result += ">";
348 break;
349 }
Chris Lattner9e094c42003-05-14 17:50:47 +0000350 case Type::OpaqueTyID:
John Criswell4ff620a2004-06-01 14:54:08 +0000351 Result += "opaque";
Chris Lattner9e094c42003-05-14 17:50:47 +0000352 break;
Chris Lattner207b5bc2001-10-29 16:37:48 +0000353 default:
John Criswell4ff620a2004-06-01 14:54:08 +0000354 Result += "<unrecognized-type>";
Chris Lattner82c4bc72006-12-06 06:40:49 +0000355 break;
Chris Lattner207b5bc2001-10-29 16:37:48 +0000356 }
357
358 TypeStack.pop_back(); // Remove self from stack...
Chris Lattner207b5bc2001-10-29 16:37:48 +0000359}
360
361
Misha Brukman9d0802e2004-03-01 19:48:13 +0000362/// printTypeInt - The internal guts of printing out a type that has a
363/// potentially named portion.
364///
Chris Lattner7b13f562003-05-08 02:08:14 +0000365static std::ostream &printTypeInt(std::ostream &Out, const Type *Ty,
366 std::map<const Type *, std::string> &TypeNames) {
Chris Lattner207b5bc2001-10-29 16:37:48 +0000367 // Primitive types always print out their description, regardless of whether
368 // they have been named or not.
369 //
Chris Lattner42a75512007-01-15 02:27:26 +0000370 if (Ty->isInteger() || (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty)))
Chris Lattnerdaf2a492003-10-30 00:12:51 +0000371 return Out << Ty->getDescription();
Chris Lattner207b5bc2001-10-29 16:37:48 +0000372
373 // Check to see if the type is named.
Chris Lattner7b13f562003-05-08 02:08:14 +0000374 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
Chris Lattner207b5bc2001-10-29 16:37:48 +0000375 if (I != TypeNames.end()) return Out << I->second;
376
377 // Otherwise we have a type that has not been named but is a derived type.
378 // Carefully recurse the type hierarchy to print out any contained symbolic
379 // names.
380 //
Chris Lattner7b13f562003-05-08 02:08:14 +0000381 std::vector<const Type *> TypeStack;
John Criswell4ff620a2004-06-01 14:54:08 +0000382 std::string TypeName;
383 calcTypeName(Ty, TypeStack, TypeNames, TypeName);
Chris Lattner697954c2002-01-20 22:54:45 +0000384 TypeNames.insert(std::make_pair(Ty, TypeName));//Cache type name for later use
John Criswell4ff620a2004-06-01 14:54:08 +0000385 return (Out << TypeName);
Chris Lattner207b5bc2001-10-29 16:37:48 +0000386}
387
Chris Lattnere51e03b2001-10-31 04:33:19 +0000388
Misha Brukman9d0802e2004-03-01 19:48:13 +0000389/// WriteTypeSymbolic - This attempts to write the specified type as a symbolic
390/// type, iff there is an entry in the modules symbol table for the specified
391/// type or one of it's component types. This is slower than a simple x << Type
392///
Chris Lattner31f84992003-11-21 20:23:48 +0000393std::ostream &llvm::WriteTypeSymbolic(std::ostream &Out, const Type *Ty,
394 const Module *M) {
Misha Brukmanfd939082005-04-21 23:48:37 +0000395 Out << ' ';
Chris Lattner207b5bc2001-10-29 16:37:48 +0000396
Chris Lattner82c4bc72006-12-06 06:40:49 +0000397 // If they want us to print out a type, but there is no context, we can't
398 // print it symbolically.
399 if (!M)
Chris Lattner7b8660d2001-10-29 16:40:32 +0000400 return Out << Ty->getDescription();
Chris Lattner82c4bc72006-12-06 06:40:49 +0000401
402 std::map<const Type *, std::string> TypeNames;
403 fillTypeNameTable(M, TypeNames);
404 return printTypeInt(Out, Ty, TypeNames);
Chris Lattner207b5bc2001-10-29 16:37:48 +0000405}
406
Chris Lattner18365502006-01-23 23:03:36 +0000407// PrintEscapedString - Print each character of the specified string, escaping
408// it if it is not printable or if it is an escape char.
409static void PrintEscapedString(const std::string &Str, std::ostream &Out) {
410 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
411 unsigned char C = Str[i];
412 if (isprint(C) && C != '"' && C != '\\') {
413 Out << C;
414 } else {
415 Out << '\\'
416 << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'))
417 << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
418 }
419 }
420}
421
Chris Lattner82c4bc72006-12-06 06:40:49 +0000422static const char *getPredicateText(unsigned predicate) {
Reid Spencer81dfeb32006-12-04 05:19:18 +0000423 const char * pred = "unknown";
424 switch (predicate) {
425 case FCmpInst::FCMP_FALSE: pred = "false"; break;
426 case FCmpInst::FCMP_OEQ: pred = "oeq"; break;
427 case FCmpInst::FCMP_OGT: pred = "ogt"; break;
428 case FCmpInst::FCMP_OGE: pred = "oge"; break;
429 case FCmpInst::FCMP_OLT: pred = "olt"; break;
430 case FCmpInst::FCMP_OLE: pred = "ole"; break;
431 case FCmpInst::FCMP_ONE: pred = "one"; break;
432 case FCmpInst::FCMP_ORD: pred = "ord"; break;
433 case FCmpInst::FCMP_UNO: pred = "uno"; break;
434 case FCmpInst::FCMP_UEQ: pred = "ueq"; break;
435 case FCmpInst::FCMP_UGT: pred = "ugt"; break;
436 case FCmpInst::FCMP_UGE: pred = "uge"; break;
437 case FCmpInst::FCMP_ULT: pred = "ult"; break;
438 case FCmpInst::FCMP_ULE: pred = "ule"; break;
439 case FCmpInst::FCMP_UNE: pred = "une"; break;
440 case FCmpInst::FCMP_TRUE: pred = "true"; break;
441 case ICmpInst::ICMP_EQ: pred = "eq"; break;
442 case ICmpInst::ICMP_NE: pred = "ne"; break;
443 case ICmpInst::ICMP_SGT: pred = "sgt"; break;
444 case ICmpInst::ICMP_SGE: pred = "sge"; break;
445 case ICmpInst::ICMP_SLT: pred = "slt"; break;
446 case ICmpInst::ICMP_SLE: pred = "sle"; break;
447 case ICmpInst::ICMP_UGT: pred = "ugt"; break;
448 case ICmpInst::ICMP_UGE: pred = "uge"; break;
449 case ICmpInst::ICMP_ULT: pred = "ult"; break;
450 case ICmpInst::ICMP_ULE: pred = "ule"; break;
451 }
452 return pred;
453}
454
Misha Brukmanfd939082005-04-21 23:48:37 +0000455/// @brief Internal constant writer.
456static void WriteConstantInt(std::ostream &Out, const Constant *CV,
Chris Lattner7b13f562003-05-08 02:08:14 +0000457 std::map<const Type *, std::string> &TypeTable,
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000458 SlotMachine *Machine) {
Jim Laskey38a409c2006-02-27 10:33:53 +0000459 const int IndentSize = 4;
460 static std::string Indent = "\n";
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000461 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Reid Spencer4fe16d62007-01-11 18:21:29 +0000462 if (CI->getType() == Type::Int1Ty)
Reid Spencer579dca12007-01-12 04:24:46 +0000463 Out << (CI->getZExtValue() ? "true" : "false");
464 else
Reid Spencer914c3bc2007-02-27 20:25:25 +0000465 Out << CI->getValue().toStringSigned(10);
Chris Lattner66e810b2002-04-18 18:53:13 +0000466 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
467 // We would like to output the FP constant value in exponential notation,
468 // but we cannot do this if doing so will lose precision. Check here to
469 // make sure that we only output it in exponential format if we can parse
470 // the value back and get the same value.
471 //
472 std::string StrVal = ftostr(CFP->getValue());
473
474 // Check to make sure that the stringized number is not some string like
475 // "Inf" or NaN, that atof will accept, but the lexer will not. Check that
476 // the string matches the "[-+]?[0-9]" regex.
477 //
478 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
479 ((StrVal[0] == '-' || StrVal[0] == '+') &&
Brian Gaekeb471a232003-06-17 23:55:35 +0000480 (StrVal[1] >= '0' && StrVal[1] <= '9')))
Chris Lattner66e810b2002-04-18 18:53:13 +0000481 // Reparse stringized version!
482 if (atof(StrVal.c_str()) == CFP->getValue()) {
Chris Lattner71d94d12005-01-04 01:56:57 +0000483 Out << StrVal;
484 return;
Chris Lattner66e810b2002-04-18 18:53:13 +0000485 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000486
Chris Lattner66e810b2002-04-18 18:53:13 +0000487 // Otherwise we could not reparse it to exactly the same value, so we must
488 // output the string in hexadecimal format!
Chris Lattner71d94d12005-01-04 01:56:57 +0000489 assert(sizeof(double) == sizeof(uint64_t) &&
Chris Lattner66e810b2002-04-18 18:53:13 +0000490 "assuming that double is 64 bits!");
Jim Laskeycb6682f2005-08-17 19:34:49 +0000491 Out << "0x" << utohexstr(DoubleToBits(CFP->getValue()));
Chris Lattner66e810b2002-04-18 18:53:13 +0000492
Chris Lattnerde512b52004-02-15 05:55:15 +0000493 } else if (isa<ConstantAggregateZero>(CV)) {
494 Out << "zeroinitializer";
Chris Lattner66e810b2002-04-18 18:53:13 +0000495 } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
496 // As a special case, print the array as a string if it is an array of
497 // ubytes or an array of sbytes with positive values.
Misha Brukmanfd939082005-04-21 23:48:37 +0000498 //
Chris Lattner66e810b2002-04-18 18:53:13 +0000499 const Type *ETy = CA->getType()->getElementType();
Chris Lattner18365502006-01-23 23:03:36 +0000500 if (CA->isString()) {
Chris Lattner66e810b2002-04-18 18:53:13 +0000501 Out << "c\"";
Chris Lattner18365502006-01-23 23:03:36 +0000502 PrintEscapedString(CA->getAsString(), Out);
Chris Lattner66e810b2002-04-18 18:53:13 +0000503 Out << "\"";
504
505 } else { // Cannot output in string format...
Misha Brukman40c732c2004-06-04 21:11:51 +0000506 Out << '[';
Chris Lattner7a716ad2002-04-16 21:36:08 +0000507 if (CA->getNumOperands()) {
Misha Brukman40c732c2004-06-04 21:11:51 +0000508 Out << ' ';
Chris Lattner66e810b2002-04-18 18:53:13 +0000509 printTypeInt(Out, ETy, TypeTable);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000510 WriteAsOperandInternal(Out, CA->getOperand(0),
Chris Lattner2fcfdb72006-12-06 06:24:27 +0000511 TypeTable, Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000512 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
513 Out << ", ";
Chris Lattner66e810b2002-04-18 18:53:13 +0000514 printTypeInt(Out, ETy, TypeTable);
Chris Lattner2fcfdb72006-12-06 06:24:27 +0000515 WriteAsOperandInternal(Out, CA->getOperand(i), TypeTable, Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000516 }
517 }
518 Out << " ]";
519 }
520 } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Andrew Lenharth43f344a2007-01-08 18:21:30 +0000521 if (CS->getType()->isPacked())
522 Out << '<';
Misha Brukman40c732c2004-06-04 21:11:51 +0000523 Out << '{';
Jim Laskeya3f332b2006-02-25 12:27:03 +0000524 unsigned N = CS->getNumOperands();
525 if (N) {
Jim Laskey38a409c2006-02-27 10:33:53 +0000526 if (N > 2) {
527 Indent += std::string(IndentSize, ' ');
528 Out << Indent;
529 } else {
530 Out << ' ';
531 }
Chris Lattner7a716ad2002-04-16 21:36:08 +0000532 printTypeInt(Out, CS->getOperand(0)->getType(), TypeTable);
533
Chris Lattner2fcfdb72006-12-06 06:24:27 +0000534 WriteAsOperandInternal(Out, CS->getOperand(0), TypeTable, Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000535
Jim Laskeya3f332b2006-02-25 12:27:03 +0000536 for (unsigned i = 1; i < N; i++) {
Chris Lattner7a716ad2002-04-16 21:36:08 +0000537 Out << ", ";
Jim Laskey38a409c2006-02-27 10:33:53 +0000538 if (N > 2) Out << Indent;
Chris Lattner7a716ad2002-04-16 21:36:08 +0000539 printTypeInt(Out, CS->getOperand(i)->getType(), TypeTable);
540
Chris Lattner2fcfdb72006-12-06 06:24:27 +0000541 WriteAsOperandInternal(Out, CS->getOperand(i), TypeTable, Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000542 }
Jim Laskey38a409c2006-02-27 10:33:53 +0000543 if (N > 2) Indent.resize(Indent.size() - IndentSize);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000544 }
Jim Laskeya3f332b2006-02-25 12:27:03 +0000545
Chris Lattner7a716ad2002-04-16 21:36:08 +0000546 Out << " }";
Andrew Lenharth43f344a2007-01-08 18:21:30 +0000547 if (CS->getType()->isPacked())
548 Out << '>';
Reid Spencer9d6565a2007-02-15 02:26:10 +0000549 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
Brian Gaeke715c90b2004-08-20 06:00:58 +0000550 const Type *ETy = CP->getType()->getElementType();
Misha Brukmanfd939082005-04-21 23:48:37 +0000551 assert(CP->getNumOperands() > 0 &&
Brian Gaeke715c90b2004-08-20 06:00:58 +0000552 "Number of operands for a PackedConst must be > 0");
553 Out << '<';
554 Out << ' ';
555 printTypeInt(Out, ETy, TypeTable);
Chris Lattner2fcfdb72006-12-06 06:24:27 +0000556 WriteAsOperandInternal(Out, CP->getOperand(0), TypeTable, Machine);
Brian Gaeke715c90b2004-08-20 06:00:58 +0000557 for (unsigned i = 1, e = CP->getNumOperands(); i != e; ++i) {
558 Out << ", ";
559 printTypeInt(Out, ETy, TypeTable);
Chris Lattner2fcfdb72006-12-06 06:24:27 +0000560 WriteAsOperandInternal(Out, CP->getOperand(i), TypeTable, Machine);
Brian Gaeke715c90b2004-08-20 06:00:58 +0000561 }
562 Out << " >";
Chris Lattner7a716ad2002-04-16 21:36:08 +0000563 } else if (isa<ConstantPointerNull>(CV)) {
564 Out << "null";
565
Chris Lattnerb976e662004-10-16 18:08:06 +0000566 } else if (isa<UndefValue>(CV)) {
567 Out << "undef";
568
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000569 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Reid Spencer81dfeb32006-12-04 05:19:18 +0000570 Out << CE->getOpcodeName();
571 if (CE->isCompare())
572 Out << " " << getPredicateText(CE->getPredicate());
573 Out << " (";
Misha Brukmanfd939082005-04-21 23:48:37 +0000574
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000575 for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
576 printTypeInt(Out, (*OI)->getType(), TypeTable);
Chris Lattner2fcfdb72006-12-06 06:24:27 +0000577 WriteAsOperandInternal(Out, *OI, TypeTable, Machine);
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000578 if (OI+1 != CE->op_end())
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000579 Out << ", ";
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000580 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000581
Reid Spencer3da59db2006-11-27 01:05:10 +0000582 if (CE->isCast()) {
Chris Lattner95586b82002-08-15 19:37:43 +0000583 Out << " to ";
584 printTypeInt(Out, CE->getType(), TypeTable);
585 }
Reid Spencer3da59db2006-11-27 01:05:10 +0000586
Misha Brukman40c732c2004-06-04 21:11:51 +0000587 Out << ')';
Chris Lattner95586b82002-08-15 19:37:43 +0000588
Chris Lattner7a716ad2002-04-16 21:36:08 +0000589 } else {
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000590 Out << "<placeholder or erroneous Constant>";
Chris Lattner7a716ad2002-04-16 21:36:08 +0000591 }
592}
593
594
Misha Brukmanab5c6002004-03-02 00:22:19 +0000595/// WriteAsOperand - Write the name of the specified value out to the specified
596/// ostream. This can be useful when you just want to print int %reg126, not
597/// the whole instruction that generated it.
598///
Misha Brukmanfd939082005-04-21 23:48:37 +0000599static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
Chris Lattner7b13f562003-05-08 02:08:14 +0000600 std::map<const Type*, std::string> &TypeTable,
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000601 SlotMachine *Machine) {
Misha Brukman40c732c2004-06-04 21:11:51 +0000602 Out << ' ';
Chris Lattner2fcfdb72006-12-06 06:24:27 +0000603 if (V->hasName())
Reid Spencer3702d262007-01-26 08:02:52 +0000604 Out << getLLVMName(V->getName(),
605 isa<GlobalValue>(V) ? GlobalPrefix : LocalPrefix);
Reid Spencer79703962004-07-17 23:47:01 +0000606 else {
607 const Constant *CV = dyn_cast<Constant>(V);
Chris Lattner80cd1152006-01-25 22:26:05 +0000608 if (CV && !isa<GlobalValue>(CV)) {
Chris Lattner2fcfdb72006-12-06 06:24:27 +0000609 WriteConstantInt(Out, CV, TypeTable, Machine);
Chris Lattner80cd1152006-01-25 22:26:05 +0000610 } else if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
611 Out << "asm ";
612 if (IA->hasSideEffects())
613 Out << "sideeffect ";
614 Out << '"';
615 PrintEscapedString(IA->getAsmString(), Out);
616 Out << "\", \"";
617 PrintEscapedString(IA->getConstraintString(), Out);
618 Out << '"';
619 } else {
Reid Spencer3702d262007-01-26 08:02:52 +0000620 char Prefix = '%';
Chris Lattner7a716ad2002-04-16 21:36:08 +0000621 int Slot;
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000622 if (Machine) {
Reid Spencer3702d262007-01-26 08:02:52 +0000623 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattner22379bc2007-01-11 03:54:27 +0000624 Slot = Machine->getGlobalSlot(GV);
Reid Spencer3702d262007-01-26 08:02:52 +0000625 Prefix = '@';
626 } else {
Chris Lattner22379bc2007-01-11 03:54:27 +0000627 Slot = Machine->getLocalSlot(V);
Reid Spencer3702d262007-01-26 08:02:52 +0000628 }
Chris Lattner7a716ad2002-04-16 21:36:08 +0000629 } else {
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000630 Machine = createSlotMachine(V);
Chris Lattner22379bc2007-01-11 03:54:27 +0000631 if (Machine) {
Reid Spencer3702d262007-01-26 08:02:52 +0000632 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattner22379bc2007-01-11 03:54:27 +0000633 Slot = Machine->getGlobalSlot(GV);
Reid Spencer3702d262007-01-26 08:02:52 +0000634 Prefix = '@';
635 } else {
Chris Lattner22379bc2007-01-11 03:54:27 +0000636 Slot = Machine->getLocalSlot(V);
Reid Spencer3702d262007-01-26 08:02:52 +0000637 }
Chris Lattner22379bc2007-01-11 03:54:27 +0000638 } else {
Chris Lattner69566452004-06-09 19:41:19 +0000639 Slot = -1;
Chris Lattner22379bc2007-01-11 03:54:27 +0000640 }
Reid Spencerb03de0c2004-05-26 21:56:09 +0000641 delete Machine;
Chris Lattner7a716ad2002-04-16 21:36:08 +0000642 }
Chris Lattner69566452004-06-09 19:41:19 +0000643 if (Slot != -1)
Reid Spencer3702d262007-01-26 08:02:52 +0000644 Out << Prefix << Slot;
Chris Lattner69566452004-06-09 19:41:19 +0000645 else
646 Out << "<badref>";
Chris Lattner7a716ad2002-04-16 21:36:08 +0000647 }
648 }
649}
650
Misha Brukman9d0802e2004-03-01 19:48:13 +0000651/// WriteAsOperand - Write the name of the specified value out to the specified
652/// ostream. This can be useful when you just want to print int %reg126, not
653/// the whole instruction that generated it.
654///
Chris Lattner31f84992003-11-21 20:23:48 +0000655std::ostream &llvm::WriteAsOperand(std::ostream &Out, const Value *V,
Chris Lattnera6b1ffc2006-12-06 06:15:43 +0000656 bool PrintType, const Module *Context) {
Chris Lattner7b13f562003-05-08 02:08:14 +0000657 std::map<const Type *, std::string> TypeNames;
Chris Lattner607dc682002-07-10 16:48:17 +0000658 if (Context == 0) Context = getModuleFromVal(V);
Chris Lattner207b5bc2001-10-29 16:37:48 +0000659
Chris Lattner6e6026b2002-11-20 18:36:02 +0000660 if (Context)
Chris Lattner607dc682002-07-10 16:48:17 +0000661 fillTypeNameTable(Context, TypeNames);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000662
663 if (PrintType)
664 printTypeInt(Out, V->getType(), TypeNames);
Misha Brukmanfd939082005-04-21 23:48:37 +0000665
Chris Lattner2fcfdb72006-12-06 06:24:27 +0000666 WriteAsOperandInternal(Out, V, TypeNames, 0);
Chris Lattner622f7402001-07-20 19:15:21 +0000667 return Out;
668}
669
Reid Spencer0e25e1c2004-07-04 11:50:43 +0000670
Chris Lattner31f84992003-11-21 20:23:48 +0000671namespace llvm {
Chris Lattnerd8c2e422001-07-12 23:35:26 +0000672
Chris Lattner007377f2001-09-07 16:36:04 +0000673class AssemblyWriter {
Misha Brukman0313e0b2004-06-21 21:53:56 +0000674 std::ostream &Out;
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000675 SlotMachine &Machine;
Chris Lattnerc1824992001-10-29 16:05:51 +0000676 const Module *TheModule;
Chris Lattner7b13f562003-05-08 02:08:14 +0000677 std::map<const Type *, std::string> TypeNames;
Chris Lattner95e5a2c2003-10-30 23:41:03 +0000678 AssemblyAnnotationWriter *AnnotationWriter;
Chris Lattner00950542001-06-06 20:29:01 +0000679public:
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000680 inline AssemblyWriter(std::ostream &o, SlotMachine &Mac, const Module *M,
Chris Lattner95e5a2c2003-10-30 23:41:03 +0000681 AssemblyAnnotationWriter *AAW)
Misha Brukman0313e0b2004-06-21 21:53:56 +0000682 : Out(o), Machine(Mac), TheModule(M), AnnotationWriter(AAW) {
Chris Lattnerc1824992001-10-29 16:05:51 +0000683
684 // If the module has a symbol table, take all global types and stuff their
685 // names into the TypeNames map.
686 //
Chris Lattner207b5bc2001-10-29 16:37:48 +0000687 fillTypeNameTable(M, TypeNames);
Chris Lattner00950542001-06-06 20:29:01 +0000688 }
689
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000690 inline void write(const Module *M) { printModule(M); }
691 inline void write(const GlobalVariable *G) { printGlobal(G); }
692 inline void write(const GlobalAlias *G) { printAlias(G); }
693 inline void write(const Function *F) { printFunction(F); }
694 inline void write(const BasicBlock *BB) { printBasicBlock(BB); }
Chris Lattner7e708292002-06-25 16:13:24 +0000695 inline void write(const Instruction *I) { printInstruction(*I); }
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000696 inline void write(const Type *Ty) { printType(Ty); }
Chris Lattner00950542001-06-06 20:29:01 +0000697
Chris Lattner2fcfdb72006-12-06 06:24:27 +0000698 void writeOperand(const Value *Op, bool PrintType);
Chris Lattner66e810b2002-04-18 18:53:13 +0000699
Misha Brukman5cf1acf2004-04-28 15:31:21 +0000700 const Module* getModule() { return TheModule; }
701
Misha Brukmanf771bea2004-11-15 19:30:05 +0000702private:
Chris Lattnerc1824992001-10-29 16:05:51 +0000703 void printModule(const Module *M);
Reid Spencer78d033e2007-01-06 07:24:44 +0000704 void printTypeSymbolTable(const TypeSymbolTable &ST);
Chris Lattnerc1824992001-10-29 16:05:51 +0000705 void printGlobal(const GlobalVariable *GV);
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000706 void printAlias(const GlobalAlias *GV);
Chris Lattner79df7c02002-03-26 18:01:55 +0000707 void printFunction(const Function *F);
Reid Spencerb138a062007-04-09 06:10:42 +0000708 void printArgument(const Argument *FA, uint16_t ParamAttrs);
Chris Lattnerc1824992001-10-29 16:05:51 +0000709 void printBasicBlock(const BasicBlock *BB);
Chris Lattner7e708292002-06-25 16:13:24 +0000710 void printInstruction(const Instruction &I);
Chris Lattner2761e9f2002-04-13 20:53:41 +0000711
712 // printType - Go to extreme measures to attempt to print out a short,
713 // symbolic version of a type name.
714 //
Chris Lattner7b13f562003-05-08 02:08:14 +0000715 std::ostream &printType(const Type *Ty) {
Misha Brukman0313e0b2004-06-21 21:53:56 +0000716 return printTypeInt(Out, Ty, TypeNames);
Chris Lattner2761e9f2002-04-13 20:53:41 +0000717 }
718
719 // printTypeAtLeastOneLevel - Print out one level of the possibly complex type
720 // without considering any symbolic types that we may have equal to it.
721 //
Chris Lattner7b13f562003-05-08 02:08:14 +0000722 std::ostream &printTypeAtLeastOneLevel(const Type *Ty);
Chris Lattnerc1824992001-10-29 16:05:51 +0000723
Chris Lattnere02fa852001-10-13 06:42:36 +0000724 // printInfoComment - Print a little comment after the instruction indicating
725 // which slot it occupies.
Chris Lattner7e708292002-06-25 16:13:24 +0000726 void printInfoComment(const Value &V);
Chris Lattner00950542001-06-06 20:29:01 +0000727};
Reid Spencer73b74952004-05-27 22:04:46 +0000728} // end of llvm namespace
Chris Lattner00950542001-06-06 20:29:01 +0000729
Misha Brukmanab5c6002004-03-02 00:22:19 +0000730/// printTypeAtLeastOneLevel - Print out one level of the possibly complex type
731/// without considering any symbolic types that we may have equal to it.
732///
Chris Lattner7b13f562003-05-08 02:08:14 +0000733std::ostream &AssemblyWriter::printTypeAtLeastOneLevel(const Type *Ty) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000734 if (const IntegerType *ITy = dyn_cast<IntegerType>(Ty))
735 Out << "i" << utostr(ITy->getBitWidth());
736 else if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
Reid Spencerbd5db8e2006-12-31 05:24:50 +0000737 printType(FTy->getReturnType());
Reid Spencerbd5db8e2006-12-31 05:24:50 +0000738 Out << " (";
739 unsigned Idx = 1;
Reid Spencerb138a062007-04-09 06:10:42 +0000740 const ParamAttrsList *Attrs = FTy->getParamAttrs();
Chris Lattnerd5d89962004-02-09 04:14:01 +0000741 for (FunctionType::param_iterator I = FTy->param_begin(),
742 E = FTy->param_end(); I != E; ++I) {
743 if (I != FTy->param_begin())
Misha Brukman0313e0b2004-06-21 21:53:56 +0000744 Out << ", ";
Chris Lattner7a716ad2002-04-16 21:36:08 +0000745 printType(*I);
Reid Spencer18da0722007-04-11 02:44:20 +0000746 if (Attrs && Attrs->getParamAttrs(Idx) != ParamAttr::None) {
Reid Spencerb138a062007-04-09 06:10:42 +0000747 Out << " " << Attrs->getParamAttrsTextByIndex(Idx);
Reid Spencerbd5db8e2006-12-31 05:24:50 +0000748 }
749 Idx++;
Chris Lattner2761e9f2002-04-13 20:53:41 +0000750 }
751 if (FTy->isVarArg()) {
Misha Brukman0313e0b2004-06-21 21:53:56 +0000752 if (FTy->getNumParams()) Out << ", ";
753 Out << "...";
Chris Lattner2761e9f2002-04-13 20:53:41 +0000754 }
Misha Brukman0313e0b2004-06-21 21:53:56 +0000755 Out << ')';
Reid Spencer18da0722007-04-11 02:44:20 +0000756 if (Attrs && Attrs->getParamAttrs(0) != ParamAttr::None)
Reid Spencerb138a062007-04-09 06:10:42 +0000757 Out << ' ' << Attrs->getParamAttrsTextByIndex(0);
Chris Lattner7e708292002-06-25 16:13:24 +0000758 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
Andrew Lenharth38ecbf12006-12-08 18:06:16 +0000759 if (STy->isPacked())
760 Out << '<';
Misha Brukman0313e0b2004-06-21 21:53:56 +0000761 Out << "{ ";
Chris Lattnerd21cd802004-02-09 04:37:31 +0000762 for (StructType::element_iterator I = STy->element_begin(),
763 E = STy->element_end(); I != E; ++I) {
764 if (I != STy->element_begin())
Misha Brukman0313e0b2004-06-21 21:53:56 +0000765 Out << ", ";
Chris Lattner2761e9f2002-04-13 20:53:41 +0000766 printType(*I);
767 }
Misha Brukman0313e0b2004-06-21 21:53:56 +0000768 Out << " }";
Andrew Lenharth38ecbf12006-12-08 18:06:16 +0000769 if (STy->isPacked())
770 Out << '>';
Chris Lattner7e708292002-06-25 16:13:24 +0000771 } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
Misha Brukman40c732c2004-06-04 21:11:51 +0000772 printType(PTy->getElementType()) << '*';
Chris Lattner7e708292002-06-25 16:13:24 +0000773 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Misha Brukman0313e0b2004-06-21 21:53:56 +0000774 Out << '[' << ATy->getNumElements() << " x ";
Misha Brukman40c732c2004-06-04 21:11:51 +0000775 printType(ATy->getElementType()) << ']';
Reid Spencer9d6565a2007-02-15 02:26:10 +0000776 } else if (const VectorType *PTy = dyn_cast<VectorType>(Ty)) {
Reid Spencer5527c0b2004-08-20 15:37:30 +0000777 Out << '<' << PTy->getNumElements() << " x ";
778 printType(PTy->getElementType()) << '>';
779 }
Reid Spencer3ed469c2006-11-02 20:25:50 +0000780 else if (isa<OpaqueType>(Ty)) {
Misha Brukman0313e0b2004-06-21 21:53:56 +0000781 Out << "opaque";
Chris Lattner2761e9f2002-04-13 20:53:41 +0000782 } else {
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000783 if (!Ty->isPrimitiveType())
Misha Brukman0313e0b2004-06-21 21:53:56 +0000784 Out << "<unknown derived type>";
Chris Lattner2761e9f2002-04-13 20:53:41 +0000785 printType(Ty);
786 }
Misha Brukman0313e0b2004-06-21 21:53:56 +0000787 return Out;
Chris Lattner2761e9f2002-04-13 20:53:41 +0000788}
789
790
Chris Lattner2fcfdb72006-12-06 06:24:27 +0000791void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType) {
792 if (Operand == 0) {
Chris Lattneraab18202005-02-24 16:58:29 +0000793 Out << "<null operand!>";
Chris Lattner2fcfdb72006-12-06 06:24:27 +0000794 } else {
795 if (PrintType) { Out << ' '; printType(Operand->getType()); }
796 WriteAsOperandInternal(Out, Operand, TypeNames, &Machine);
Chris Lattneraab18202005-02-24 16:58:29 +0000797 }
Chris Lattner00950542001-06-06 20:29:01 +0000798}
799
Chris Lattner00950542001-06-06 20:29:01 +0000800
Chris Lattnerc1824992001-10-29 16:05:51 +0000801void AssemblyWriter::printModule(const Module *M) {
Chris Lattner31ab1b32005-03-02 23:12:40 +0000802 if (!M->getModuleIdentifier().empty() &&
Misha Brukmanfd939082005-04-21 23:48:37 +0000803 // Don't print the ID if it will start a new line (which would
Chris Lattner31ab1b32005-03-02 23:12:40 +0000804 // require a comment char before it).
805 M->getModuleIdentifier().find('\n') == std::string::npos)
806 Out << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
807
Owen Andersoncf7ff2b2006-10-18 02:21:12 +0000808 if (!M->getDataLayout().empty())
Chris Lattnerd2f9e602006-10-22 06:06:56 +0000809 Out << "target datalayout = \"" << M->getDataLayout() << "\"\n";
Reid Spencercddc86f2004-07-25 21:44:54 +0000810 if (!M->getTargetTriple().empty())
Reid Spencerc9a1f0d2004-07-25 21:29:43 +0000811 Out << "target triple = \"" << M->getTargetTriple() << "\"\n";
Misha Brukmanfd939082005-04-21 23:48:37 +0000812
Chris Lattnercc041ba2006-01-24 04:13:11 +0000813 if (!M->getModuleInlineAsm().empty()) {
Chris Lattner42a162e2006-01-24 00:45:30 +0000814 // Split the string into lines, to make it easier to read the .ll file.
Chris Lattnercc041ba2006-01-24 04:13:11 +0000815 std::string Asm = M->getModuleInlineAsm();
Chris Lattner42a162e2006-01-24 00:45:30 +0000816 size_t CurPos = 0;
817 size_t NewLine = Asm.find_first_of('\n', CurPos);
818 while (NewLine != std::string::npos) {
819 // We found a newline, print the portion of the asm string from the
820 // last newline up to this newline.
821 Out << "module asm \"";
822 PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.begin()+NewLine),
823 Out);
824 Out << "\"\n";
825 CurPos = NewLine+1;
826 NewLine = Asm.find_first_of('\n', CurPos);
827 }
Chris Lattner71cdba32006-01-24 00:40:17 +0000828 Out << "module asm \"";
Chris Lattner42a162e2006-01-24 00:45:30 +0000829 PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.end()), Out);
Chris Lattner18365502006-01-23 23:03:36 +0000830 Out << "\"\n";
831 }
832
Chris Lattner44da7d72004-09-14 05:06:58 +0000833 // Loop over the dependent libraries and emit them.
Chris Lattnercfe97b72004-09-14 04:51:44 +0000834 Module::lib_iterator LI = M->lib_begin();
835 Module::lib_iterator LE = M->lib_end();
Reid Spencercddc86f2004-07-25 21:44:54 +0000836 if (LI != LE) {
Chris Lattnercfe97b72004-09-14 04:51:44 +0000837 Out << "deplibs = [ ";
838 while (LI != LE) {
Chris Lattner44da7d72004-09-14 05:06:58 +0000839 Out << '"' << *LI << '"';
Reid Spencerc9a1f0d2004-07-25 21:29:43 +0000840 ++LI;
Chris Lattnercfe97b72004-09-14 04:51:44 +0000841 if (LI != LE)
842 Out << ", ";
Reid Spencerc9a1f0d2004-07-25 21:29:43 +0000843 }
844 Out << " ]\n";
Reid Spencer83f6a772004-07-25 18:08:18 +0000845 }
Reid Spencere59eaf42004-09-13 23:44:23 +0000846
Chris Lattner44da7d72004-09-14 05:06:58 +0000847 // Loop over the symbol table, emitting all named constants.
Reid Spencer78d033e2007-01-06 07:24:44 +0000848 printTypeSymbolTable(M->getTypeSymbolTable());
Misha Brukmanfd939082005-04-21 23:48:37 +0000849
Chris Lattnerd6d826c2006-12-06 04:41:52 +0000850 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
851 I != E; ++I)
Chris Lattner7e708292002-06-25 16:13:24 +0000852 printGlobal(I);
Chris Lattner69dacfc2007-04-26 02:24:10 +0000853
854 // Output all aliases.
855 if (!M->alias_empty()) Out << "\n";
856 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
857 I != E; ++I)
858 printAlias(I);
Chris Lattner007377f2001-09-07 16:36:04 +0000859
Chris Lattner44da7d72004-09-14 05:06:58 +0000860 // Output all of the functions.
Chris Lattner7e708292002-06-25 16:13:24 +0000861 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
862 printFunction(I);
Chris Lattner007377f2001-09-07 16:36:04 +0000863}
864
Chris Lattnerc1824992001-10-29 16:05:51 +0000865void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Reid Spencer3702d262007-01-26 08:02:52 +0000866 if (GV->hasName()) Out << getLLVMName(GV->getName(), GlobalPrefix) << " = ";
Chris Lattnerd70684f2001-09-18 04:01:05 +0000867
Misha Brukmanfd939082005-04-21 23:48:37 +0000868 if (!GV->hasInitializer())
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000869 switch (GV->getLinkage()) {
870 case GlobalValue::DLLImportLinkage: Out << "dllimport "; break;
871 case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break;
872 default: Out << "external "; break;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000873 } else {
Chris Lattner4ad02e72003-04-16 20:28:45 +0000874 switch (GV->getLinkage()) {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000875 case GlobalValue::InternalLinkage: Out << "internal "; break;
876 case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
877 case GlobalValue::WeakLinkage: Out << "weak "; break;
878 case GlobalValue::AppendingLinkage: Out << "appending "; break;
879 case GlobalValue::DLLImportLinkage: Out << "dllimport "; break;
880 case GlobalValue::DLLExportLinkage: Out << "dllexport "; break;
881 case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break;
882 case GlobalValue::ExternalLinkage: break;
Misha Brukman3c8f2c62004-11-14 21:04:34 +0000883 case GlobalValue::GhostLinkage:
Bill Wendlinge8156192006-12-07 01:30:32 +0000884 cerr << "GhostLinkage not allowed in AsmWriter!\n";
Misha Brukman3c8f2c62004-11-14 21:04:34 +0000885 abort();
Chris Lattner4ad02e72003-04-16 20:28:45 +0000886 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000887 switch (GV->getVisibility()) {
Chris Lattner9a40c022007-01-15 18:28:18 +0000888 default: assert(0 && "Invalid visibility style!");
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000889 case GlobalValue::DefaultVisibility: break;
890 case GlobalValue::HiddenVisibility: Out << "hidden "; break;
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +0000891 case GlobalValue::ProtectedVisibility: Out << "protected "; break;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000892 }
893 }
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +0000894
895 if (GV->isThreadLocal()) Out << "thread_local ";
Misha Brukman0313e0b2004-06-21 21:53:56 +0000896 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattner7a176752001-12-04 00:03:30 +0000897 printType(GV->getType()->getElementType());
Chris Lattnerd70684f2001-09-18 04:01:05 +0000898
Reid Spencer79703962004-07-17 23:47:01 +0000899 if (GV->hasInitializer()) {
900 Constant* C = cast<Constant>(GV->getInitializer());
901 assert(C && "GlobalVar initializer isn't constant?");
Chris Lattner2fcfdb72006-12-06 06:24:27 +0000902 writeOperand(GV->getInitializer(), false);
Reid Spencer79703962004-07-17 23:47:01 +0000903 }
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000904
Chris Lattner60962db2005-11-12 00:10:19 +0000905 if (GV->hasSection())
906 Out << ", section \"" << GV->getSection() << '"';
907 if (GV->getAlignment())
Chris Lattner30caa282005-11-06 06:48:53 +0000908 Out << ", align " << GV->getAlignment();
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000909
Chris Lattner7e708292002-06-25 16:13:24 +0000910 printInfoComment(*GV);
Misha Brukman0313e0b2004-06-21 21:53:56 +0000911 Out << "\n";
Chris Lattner70cc3392001-09-10 07:58:01 +0000912}
913
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000914void AssemblyWriter::printAlias(const GlobalAlias *GA) {
915 Out << getLLVMName(GA->getName(), GlobalPrefix) << " = ";
916 switch (GA->getVisibility()) {
917 default: assert(0 && "Invalid visibility style!");
918 case GlobalValue::DefaultVisibility: break;
919 case GlobalValue::HiddenVisibility: Out << "hidden "; break;
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +0000920 case GlobalValue::ProtectedVisibility: Out << "protected "; break;
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000921 }
922
923 Out << "alias ";
924
925 switch (GA->getLinkage()) {
926 case GlobalValue::WeakLinkage: Out << "weak "; break;
927 case GlobalValue::InternalLinkage: Out << "internal "; break;
928 case GlobalValue::ExternalLinkage: break;
929 default:
930 assert(0 && "Invalid alias linkage");
931 }
932
Anton Korobeynikovc6c98af2007-04-29 18:02:48 +0000933 const Constant *Aliasee = GA->getAliasee();
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000934
935 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Aliasee)) {
936 printType(GV->getType());
937 Out << " " << getLLVMName(GV->getName(), GlobalPrefix);
938 } else if (const Function *F = dyn_cast<Function>(Aliasee)) {
939 printType(F->getFunctionType());
940 Out << "* ";
941
942 if (!F->getName().empty())
943 Out << getLLVMName(F->getName(), GlobalPrefix);
944 else
945 Out << "@\"\"";
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000946 } else {
947 const ConstantExpr *CE = 0;
948 if ((CE = dyn_cast<ConstantExpr>(Aliasee)) &&
949 (CE->getOpcode() == Instruction::BitCast)) {
950 writeOperand(CE, false);
951 } else
952 assert(0 && "Unsupported aliasee");
953 }
954
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000955 printInfoComment(*GA);
956 Out << "\n";
957}
958
Reid Spencer78d033e2007-01-06 07:24:44 +0000959void AssemblyWriter::printTypeSymbolTable(const TypeSymbolTable &ST) {
Reid Spencer9231ac82004-05-25 08:53:40 +0000960 // Print the types.
Reid Spencer78d033e2007-01-06 07:24:44 +0000961 for (TypeSymbolTable::const_iterator TI = ST.begin(), TE = ST.end();
962 TI != TE; ++TI) {
Reid Spencer3702d262007-01-26 08:02:52 +0000963 Out << "\t" << getLLVMName(TI->first, LocalPrefix) << " = type ";
Reid Spencer9231ac82004-05-25 08:53:40 +0000964
965 // Make sure we print out at least one level of the type structure, so
966 // that we do not get %FILE = type %FILE
967 //
968 printTypeAtLeastOneLevel(TI->second) << "\n";
969 }
Reid Spencer78d033e2007-01-06 07:24:44 +0000970}
971
Misha Brukmanab5c6002004-03-02 00:22:19 +0000972/// printFunction - Print all aspects of a function.
973///
Chris Lattner7e708292002-06-25 16:13:24 +0000974void AssemblyWriter::printFunction(const Function *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000975 // Print out the return type and name...
Misha Brukman0313e0b2004-06-21 21:53:56 +0000976 Out << "\n";
Chris Lattner4ad02e72003-04-16 20:28:45 +0000977
Misha Brukman0313e0b2004-06-21 21:53:56 +0000978 if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out);
Chris Lattner95e5a2c2003-10-30 23:41:03 +0000979
Reid Spencer5cbf9852007-01-30 20:08:39 +0000980 if (F->isDeclaration())
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000981 switch (F->getLinkage()) {
982 case GlobalValue::DLLImportLinkage: Out << "declare dllimport "; break;
983 case GlobalValue::ExternalWeakLinkage: Out << "declare extern_weak "; break;
984 default: Out << "declare ";
985 }
Reid Spencerb951bc02006-12-29 20:29:48 +0000986 else {
987 Out << "define ";
Chris Lattner4ad02e72003-04-16 20:28:45 +0000988 switch (F->getLinkage()) {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000989 case GlobalValue::InternalLinkage: Out << "internal "; break;
990 case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
991 case GlobalValue::WeakLinkage: Out << "weak "; break;
992 case GlobalValue::AppendingLinkage: Out << "appending "; break;
993 case GlobalValue::DLLImportLinkage: Out << "dllimport "; break;
994 case GlobalValue::DLLExportLinkage: Out << "dllexport "; break;
995 case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break;
Chris Lattner4ad02e72003-04-16 20:28:45 +0000996 case GlobalValue::ExternalLinkage: break;
Misha Brukman3c8f2c62004-11-14 21:04:34 +0000997 case GlobalValue::GhostLinkage:
Bill Wendlinge8156192006-12-07 01:30:32 +0000998 cerr << "GhostLinkage not allowed in AsmWriter!\n";
Misha Brukman3c8f2c62004-11-14 21:04:34 +0000999 abort();
Chris Lattner4ad02e72003-04-16 20:28:45 +00001000 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001001 switch (F->getVisibility()) {
Chris Lattner9a40c022007-01-15 18:28:18 +00001002 default: assert(0 && "Invalid visibility style!");
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001003 case GlobalValue::DefaultVisibility: break;
1004 case GlobalValue::HiddenVisibility: Out << "hidden "; break;
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +00001005 case GlobalValue::ProtectedVisibility: Out << "protected "; break;
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001006 }
Reid Spencerb951bc02006-12-29 20:29:48 +00001007 }
Chris Lattner4ad02e72003-04-16 20:28:45 +00001008
Chris Lattnerd5118982005-05-06 20:26:43 +00001009 // Print the calling convention.
1010 switch (F->getCallingConv()) {
1011 case CallingConv::C: break; // default
Anton Korobeynikovf8248682006-09-20 22:03:51 +00001012 case CallingConv::Fast: Out << "fastcc "; break;
1013 case CallingConv::Cold: Out << "coldcc "; break;
1014 case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break;
1015 case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break;
Chris Lattnerd5118982005-05-06 20:26:43 +00001016 default: Out << "cc" << F->getCallingConv() << " "; break;
1017 }
1018
Reid Spencerbd5db8e2006-12-31 05:24:50 +00001019 const FunctionType *FT = F->getFunctionType();
Reid Spencerb138a062007-04-09 06:10:42 +00001020 const ParamAttrsList *Attrs = FT->getParamAttrs();
Misha Brukman40c732c2004-06-04 21:11:51 +00001021 printType(F->getReturnType()) << ' ';
Chris Lattner4d45bd02003-10-18 05:57:43 +00001022 if (!F->getName().empty())
Reid Spencer3702d262007-01-26 08:02:52 +00001023 Out << getLLVMName(F->getName(), GlobalPrefix);
Chris Lattner4d45bd02003-10-18 05:57:43 +00001024 else
Reid Spencer3702d262007-01-26 08:02:52 +00001025 Out << "@\"\"";
Misha Brukman0313e0b2004-06-21 21:53:56 +00001026 Out << '(';
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001027 Machine.incorporateFunction(F);
Chris Lattner007377f2001-09-07 16:36:04 +00001028
Chris Lattnerc1824992001-10-29 16:05:51 +00001029 // Loop over the arguments, printing them...
Chris Lattner007377f2001-09-07 16:36:04 +00001030
Reid Spencerbd5db8e2006-12-31 05:24:50 +00001031 unsigned Idx = 1;
Chris Lattner8dcd2f12007-04-18 00:57:22 +00001032 if (!F->isDeclaration()) {
1033 // If this isn't a declaration, print the argument names as well.
1034 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
1035 I != E; ++I) {
1036 // Insert commas as we go... the first arg doesn't get a comma
1037 if (I != F->arg_begin()) Out << ", ";
1038 printArgument(I, (Attrs ? Attrs->getParamAttrs(Idx)
1039 : uint16_t(ParamAttr::None)));
1040 Idx++;
1041 }
1042 } else {
1043 // Otherwise, print the types from the function type.
1044 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
1045 // Insert commas as we go... the first arg doesn't get a comma
1046 if (i) Out << ", ";
1047
1048 // Output type...
1049 printType(FT->getParamType(i));
1050
1051 unsigned ArgAttrs = ParamAttr::None;
1052 if (Attrs) ArgAttrs = Attrs->getParamAttrs(i+1);
1053 if (ArgAttrs != ParamAttr::None)
1054 Out << ' ' << ParamAttrsList::getParamAttrsText(ArgAttrs);
1055 }
Reid Spencerbd5db8e2006-12-31 05:24:50 +00001056 }
Chris Lattner007377f2001-09-07 16:36:04 +00001057
1058 // Finish printing arguments...
Chris Lattner7e708292002-06-25 16:13:24 +00001059 if (FT->isVarArg()) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001060 if (FT->getNumParams()) Out << ", ";
1061 Out << "..."; // Output varargs portion of signature!
Chris Lattner007377f2001-09-07 16:36:04 +00001062 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00001063 Out << ')';
Reid Spencer18da0722007-04-11 02:44:20 +00001064 if (Attrs && Attrs->getParamAttrs(0) != ParamAttr::None)
Reid Spencerb138a062007-04-09 06:10:42 +00001065 Out << ' ' << Attrs->getParamAttrsTextByIndex(0);
Chris Lattner60962db2005-11-12 00:10:19 +00001066 if (F->hasSection())
1067 Out << " section \"" << F->getSection() << '"';
Chris Lattner30caa282005-11-06 06:48:53 +00001068 if (F->getAlignment())
1069 Out << " align " << F->getAlignment();
Chris Lattner60962db2005-11-12 00:10:19 +00001070
Reid Spencer5cbf9852007-01-30 20:08:39 +00001071 if (F->isDeclaration()) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001072 Out << "\n";
Chris Lattner03e2acb2002-05-06 03:00:40 +00001073 } else {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001074 Out << " {";
Misha Brukmanfd939082005-04-21 23:48:37 +00001075
Chris Lattnerb5794002002-04-07 22:49:37 +00001076 // Output all of its basic blocks... for the function
Chris Lattner7e708292002-06-25 16:13:24 +00001077 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
1078 printBasicBlock(I);
Chris Lattner007377f2001-09-07 16:36:04 +00001079
Misha Brukman0313e0b2004-06-21 21:53:56 +00001080 Out << "}\n";
Chris Lattner007377f2001-09-07 16:36:04 +00001081 }
1082
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001083 Machine.purgeFunction();
Chris Lattner00950542001-06-06 20:29:01 +00001084}
1085
Misha Brukmanab5c6002004-03-02 00:22:19 +00001086/// printArgument - This member is called for every argument that is passed into
1087/// the function. Simply print it out
1088///
Reid Spencerb138a062007-04-09 06:10:42 +00001089void AssemblyWriter::printArgument(const Argument *Arg, uint16_t Attrs) {
Chris Lattner00950542001-06-06 20:29:01 +00001090 // Output type...
Chris Lattnerc1824992001-10-29 16:05:51 +00001091 printType(Arg->getType());
Misha Brukmanfd939082005-04-21 23:48:37 +00001092
Reid Spencer18da0722007-04-11 02:44:20 +00001093 if (Attrs != ParamAttr::None)
Reid Spencerb138a062007-04-09 06:10:42 +00001094 Out << ' ' << ParamAttrsList::getParamAttrsText(Attrs);
Reid Spencerbd5db8e2006-12-31 05:24:50 +00001095
Chris Lattner00950542001-06-06 20:29:01 +00001096 // Output name, if available...
1097 if (Arg->hasName())
Reid Spencer3702d262007-01-26 08:02:52 +00001098 Out << ' ' << getLLVMName(Arg->getName(), LocalPrefix);
Chris Lattner00950542001-06-06 20:29:01 +00001099}
1100
Misha Brukmanab5c6002004-03-02 00:22:19 +00001101/// printBasicBlock - This member is called for each basic block in a method.
1102///
Chris Lattnerc1824992001-10-29 16:05:51 +00001103void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Chris Lattner00950542001-06-06 20:29:01 +00001104 if (BB->hasName()) { // Print out the label if it exists...
Reid Spencer3702d262007-01-26 08:02:52 +00001105 Out << "\n" << getLLVMName(BB->getName(), LabelPrefix) << ':';
Chris Lattnerafc38682002-05-14 16:02:05 +00001106 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Misha Brukman0313e0b2004-06-21 21:53:56 +00001107 Out << "\n; <label>:";
Chris Lattner22379bc2007-01-11 03:54:27 +00001108 int Slot = Machine.getLocalSlot(BB);
Chris Lattner69566452004-06-09 19:41:19 +00001109 if (Slot != -1)
Misha Brukman0313e0b2004-06-21 21:53:56 +00001110 Out << Slot;
Chris Lattner69566452004-06-09 19:41:19 +00001111 else
Misha Brukman0313e0b2004-06-21 21:53:56 +00001112 Out << "<badref>";
Chris Lattner061269b2002-10-02 19:38:55 +00001113 }
Chris Lattner4e4d8622003-11-20 00:09:43 +00001114
1115 if (BB->getParent() == 0)
Misha Brukman0313e0b2004-06-21 21:53:56 +00001116 Out << "\t\t; Error: Block without parent!";
Chris Lattner4e4d8622003-11-20 00:09:43 +00001117 else {
Dan Gohmanecb7a772007-03-22 16:38:57 +00001118 if (BB != &BB->getParent()->getEntryBlock()) { // Not the entry block?
Chris Lattner4e4d8622003-11-20 00:09:43 +00001119 // Output predecessors for the block...
Misha Brukman0313e0b2004-06-21 21:53:56 +00001120 Out << "\t\t;";
Chris Lattner4e4d8622003-11-20 00:09:43 +00001121 pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
Misha Brukmanfd939082005-04-21 23:48:37 +00001122
Chris Lattner4e4d8622003-11-20 00:09:43 +00001123 if (PI == PE) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001124 Out << " No predecessors!";
Chris Lattner4e4d8622003-11-20 00:09:43 +00001125 } else {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001126 Out << " preds =";
Chris Lattner2fcfdb72006-12-06 06:24:27 +00001127 writeOperand(*PI, false);
Chris Lattner4e4d8622003-11-20 00:09:43 +00001128 for (++PI; PI != PE; ++PI) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001129 Out << ',';
Chris Lattner2fcfdb72006-12-06 06:24:27 +00001130 writeOperand(*PI, false);
Chris Lattner4e4d8622003-11-20 00:09:43 +00001131 }
Chris Lattner40efcec2003-11-16 22:59:57 +00001132 }
Chris Lattner061269b2002-10-02 19:38:55 +00001133 }
Chris Lattner00950542001-06-06 20:29:01 +00001134 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001135
Misha Brukman0313e0b2004-06-21 21:53:56 +00001136 Out << "\n";
Chris Lattner00950542001-06-06 20:29:01 +00001137
Misha Brukman0313e0b2004-06-21 21:53:56 +00001138 if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out);
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001139
Chris Lattner007377f2001-09-07 16:36:04 +00001140 // Output all of the instructions in the basic block...
Chris Lattner7e708292002-06-25 16:13:24 +00001141 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
1142 printInstruction(*I);
Chris Lattner9f717ef2004-03-08 18:51:45 +00001143
Misha Brukman0313e0b2004-06-21 21:53:56 +00001144 if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out);
Chris Lattner00950542001-06-06 20:29:01 +00001145}
1146
Chris Lattnere02fa852001-10-13 06:42:36 +00001147
Misha Brukmanab5c6002004-03-02 00:22:19 +00001148/// printInfoComment - Print a little comment after the instruction indicating
1149/// which slot it occupies.
1150///
Chris Lattner7e708292002-06-25 16:13:24 +00001151void AssemblyWriter::printInfoComment(const Value &V) {
1152 if (V.getType() != Type::VoidTy) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001153 Out << "\t\t; <";
Misha Brukman40c732c2004-06-04 21:11:51 +00001154 printType(V.getType()) << '>';
Chris Lattnere02fa852001-10-13 06:42:36 +00001155
Chris Lattner7e708292002-06-25 16:13:24 +00001156 if (!V.hasName()) {
Chris Lattner22379bc2007-01-11 03:54:27 +00001157 int SlotNum;
1158 if (const GlobalValue *GV = dyn_cast<GlobalValue>(&V))
1159 SlotNum = Machine.getGlobalSlot(GV);
1160 else
1161 SlotNum = Machine.getLocalSlot(&V);
Chris Lattner69566452004-06-09 19:41:19 +00001162 if (SlotNum == -1)
Misha Brukman0313e0b2004-06-21 21:53:56 +00001163 Out << ":<badref>";
Reid Spencerfc621e22004-06-09 15:26:53 +00001164 else
Misha Brukman0313e0b2004-06-21 21:53:56 +00001165 Out << ':' << SlotNum; // Print out the def slot taken.
Chris Lattnere02fa852001-10-13 06:42:36 +00001166 }
Chris Lattner5c461402005-02-01 01:24:01 +00001167 Out << " [#uses=" << V.getNumUses() << ']'; // Output # uses
Chris Lattnere02fa852001-10-13 06:42:36 +00001168 }
1169}
1170
Reid Spencer3a9ec242006-08-28 01:02:49 +00001171// This member is called for each Instruction in a function..
Chris Lattner7e708292002-06-25 16:13:24 +00001172void AssemblyWriter::printInstruction(const Instruction &I) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001173 if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out);
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001174
Misha Brukman0313e0b2004-06-21 21:53:56 +00001175 Out << "\t";
Chris Lattner00950542001-06-06 20:29:01 +00001176
1177 // Print out name if it exists...
Chris Lattner7e708292002-06-25 16:13:24 +00001178 if (I.hasName())
Reid Spencer3702d262007-01-26 08:02:52 +00001179 Out << getLLVMName(I.getName(), LocalPrefix) << " = ";
Chris Lattner00950542001-06-06 20:29:01 +00001180
Chris Lattnerddb6db42005-05-06 05:51:46 +00001181 // If this is a volatile load or store, print out the volatile marker.
Chris Lattnere5e475e2003-09-08 17:45:59 +00001182 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
Chris Lattnerddb6db42005-05-06 05:51:46 +00001183 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile())) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001184 Out << "volatile ";
Chris Lattnerddb6db42005-05-06 05:51:46 +00001185 } else if (isa<CallInst>(I) && cast<CallInst>(I).isTailCall()) {
1186 // If this is a call, check if it's a tail call.
1187 Out << "tail ";
1188 }
Chris Lattnere5e475e2003-09-08 17:45:59 +00001189
Chris Lattner00950542001-06-06 20:29:01 +00001190 // Print out the opcode...
Misha Brukman0313e0b2004-06-21 21:53:56 +00001191 Out << I.getOpcodeName();
Chris Lattner00950542001-06-06 20:29:01 +00001192
Reid Spencer74f16422006-12-03 06:27:29 +00001193 // Print out the compare instruction predicates
1194 if (const FCmpInst *FCI = dyn_cast<FCmpInst>(&I)) {
Reid Spencer81dfeb32006-12-04 05:19:18 +00001195 Out << " " << getPredicateText(FCI->getPredicate());
Reid Spencer74f16422006-12-03 06:27:29 +00001196 } else if (const ICmpInst *ICI = dyn_cast<ICmpInst>(&I)) {
Reid Spencer81dfeb32006-12-04 05:19:18 +00001197 Out << " " << getPredicateText(ICI->getPredicate());
Reid Spencer74f16422006-12-03 06:27:29 +00001198 }
1199
Chris Lattner00950542001-06-06 20:29:01 +00001200 // Print out the type of the operands...
Chris Lattner7e708292002-06-25 16:13:24 +00001201 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
Chris Lattner00950542001-06-06 20:29:01 +00001202
1203 // Special case conditional branches to swizzle the condition out to the front
Chris Lattner7e708292002-06-25 16:13:24 +00001204 if (isa<BranchInst>(I) && I.getNumOperands() > 1) {
1205 writeOperand(I.getOperand(2), true);
Misha Brukman0313e0b2004-06-21 21:53:56 +00001206 Out << ',';
Chris Lattner00950542001-06-06 20:29:01 +00001207 writeOperand(Operand, true);
Misha Brukman0313e0b2004-06-21 21:53:56 +00001208 Out << ',';
Chris Lattner7e708292002-06-25 16:13:24 +00001209 writeOperand(I.getOperand(1), true);
Chris Lattner00950542001-06-06 20:29:01 +00001210
Chris Lattner94dc1f22002-04-13 18:34:38 +00001211 } else if (isa<SwitchInst>(I)) {
Chris Lattner00950542001-06-06 20:29:01 +00001212 // Special case switch statement to get formatting nice and correct...
Misha Brukman0313e0b2004-06-21 21:53:56 +00001213 writeOperand(Operand , true); Out << ',';
1214 writeOperand(I.getOperand(1), true); Out << " [";
Chris Lattner00950542001-06-06 20:29:01 +00001215
Chris Lattner7e708292002-06-25 16:13:24 +00001216 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001217 Out << "\n\t\t";
1218 writeOperand(I.getOperand(op ), true); Out << ',';
Chris Lattner7e708292002-06-25 16:13:24 +00001219 writeOperand(I.getOperand(op+1), true);
Chris Lattner00950542001-06-06 20:29:01 +00001220 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00001221 Out << "\n\t]";
Chris Lattnerb00c5822001-10-02 03:41:24 +00001222 } else if (isa<PHINode>(I)) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001223 Out << ' ';
Chris Lattner7e708292002-06-25 16:13:24 +00001224 printType(I.getType());
Misha Brukman0313e0b2004-06-21 21:53:56 +00001225 Out << ' ';
Chris Lattner00950542001-06-06 20:29:01 +00001226
Chris Lattner7e708292002-06-25 16:13:24 +00001227 for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001228 if (op) Out << ", ";
Misha Brukmanfd939082005-04-21 23:48:37 +00001229 Out << '[';
Misha Brukman0313e0b2004-06-21 21:53:56 +00001230 writeOperand(I.getOperand(op ), false); Out << ',';
1231 writeOperand(I.getOperand(op+1), false); Out << " ]";
Chris Lattnerc24d2082001-06-11 15:04:20 +00001232 }
Chris Lattnere02fa852001-10-13 06:42:36 +00001233 } else if (isa<ReturnInst>(I) && !Operand) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001234 Out << " void";
Chris Lattnerd5118982005-05-06 20:26:43 +00001235 } else if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
1236 // Print the calling convention being used.
1237 switch (CI->getCallingConv()) {
1238 case CallingConv::C: break; // default
Chris Lattner0deaab82006-05-19 21:58:52 +00001239 case CallingConv::Fast: Out << " fastcc"; break;
1240 case CallingConv::Cold: Out << " coldcc"; break;
Anton Korobeynikovf8248682006-09-20 22:03:51 +00001241 case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break;
1242 case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break;
Chris Lattnerd5118982005-05-06 20:26:43 +00001243 default: Out << " cc" << CI->getCallingConv(); break;
1244 }
1245
Reid Spencerb138a062007-04-09 06:10:42 +00001246 const PointerType *PTy = cast<PointerType>(Operand->getType());
1247 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1248 const Type *RetTy = FTy->getReturnType();
1249 const ParamAttrsList *PAL = FTy->getParamAttrs();
Chris Lattner268de042001-11-06 21:28:12 +00001250
Chris Lattner7a012292003-08-05 15:34:45 +00001251 // If possible, print out the short form of the call instruction. We can
Chris Lattnerb5794002002-04-07 22:49:37 +00001252 // only do this if the first argument is a pointer to a nonvararg function,
Chris Lattner7a012292003-08-05 15:34:45 +00001253 // and if the return type is not a pointer to a function.
Chris Lattner268de042001-11-06 21:28:12 +00001254 //
Chris Lattner7a012292003-08-05 15:34:45 +00001255 if (!FTy->isVarArg() &&
Misha Brukmanfd939082005-04-21 23:48:37 +00001256 (!isa<PointerType>(RetTy) ||
Chris Lattnerc1b27182002-07-25 20:58:51 +00001257 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001258 Out << ' '; printType(RetTy);
Chris Lattner268de042001-11-06 21:28:12 +00001259 writeOperand(Operand, false);
1260 } else {
1261 writeOperand(Operand, true);
1262 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00001263 Out << '(';
Reid Spencerbd5db8e2006-12-31 05:24:50 +00001264 for (unsigned op = 1, Eop = I.getNumOperands(); op < Eop; ++op) {
1265 if (op > 1)
1266 Out << ',';
Chris Lattner7e708292002-06-25 16:13:24 +00001267 writeOperand(I.getOperand(op), true);
Reid Spencer18da0722007-04-11 02:44:20 +00001268 if (PAL && PAL->getParamAttrs(op) != ParamAttr::None)
Reid Spencerb138a062007-04-09 06:10:42 +00001269 Out << " " << PAL->getParamAttrsTextByIndex(op);
Chris Lattner00950542001-06-06 20:29:01 +00001270 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00001271 Out << " )";
Reid Spencer18da0722007-04-11 02:44:20 +00001272 if (PAL && PAL->getParamAttrs(0) != ParamAttr::None)
Reid Spencerb138a062007-04-09 06:10:42 +00001273 Out << ' ' << PAL->getParamAttrsTextByIndex(0);
Chris Lattner7e708292002-06-25 16:13:24 +00001274 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Reid Spencerb138a062007-04-09 06:10:42 +00001275 const PointerType *PTy = cast<PointerType>(Operand->getType());
1276 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1277 const Type *RetTy = FTy->getReturnType();
1278 const ParamAttrsList *PAL = FTy->getParamAttrs();
Chris Lattner7a012292003-08-05 15:34:45 +00001279
Chris Lattnerd5118982005-05-06 20:26:43 +00001280 // Print the calling convention being used.
1281 switch (II->getCallingConv()) {
1282 case CallingConv::C: break; // default
Chris Lattner0deaab82006-05-19 21:58:52 +00001283 case CallingConv::Fast: Out << " fastcc"; break;
1284 case CallingConv::Cold: Out << " coldcc"; break;
Anton Korobeynikovf8248682006-09-20 22:03:51 +00001285 case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break;
1286 case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break;
Chris Lattnerd5118982005-05-06 20:26:43 +00001287 default: Out << " cc" << II->getCallingConv(); break;
1288 }
1289
Chris Lattner7a012292003-08-05 15:34:45 +00001290 // If possible, print out the short form of the invoke instruction. We can
1291 // only do this if the first argument is a pointer to a nonvararg function,
1292 // and if the return type is not a pointer to a function.
1293 //
1294 if (!FTy->isVarArg() &&
Misha Brukmanfd939082005-04-21 23:48:37 +00001295 (!isa<PointerType>(RetTy) ||
Chris Lattner7a012292003-08-05 15:34:45 +00001296 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001297 Out << ' '; printType(RetTy);
Chris Lattner7a012292003-08-05 15:34:45 +00001298 writeOperand(Operand, false);
1299 } else {
1300 writeOperand(Operand, true);
1301 }
1302
Misha Brukman0313e0b2004-06-21 21:53:56 +00001303 Out << '(';
Reid Spencerbd5db8e2006-12-31 05:24:50 +00001304 for (unsigned op = 3, Eop = I.getNumOperands(); op < Eop; ++op) {
1305 if (op > 3)
1306 Out << ',';
Chris Lattner7e708292002-06-25 16:13:24 +00001307 writeOperand(I.getOperand(op), true);
Reid Spencer18da0722007-04-11 02:44:20 +00001308 if (PAL && PAL->getParamAttrs(op-2) != ParamAttr::None)
Reid Spencerb138a062007-04-09 06:10:42 +00001309 Out << " " << PAL->getParamAttrsTextByIndex(op-2);
Chris Lattnere02fa852001-10-13 06:42:36 +00001310 }
1311
Reid Spencer2c261782007-01-05 17:06:19 +00001312 Out << " )";
Reid Spencer18da0722007-04-11 02:44:20 +00001313 if (PAL && PAL->getParamAttrs(0) != ParamAttr::None)
Reid Spencerb138a062007-04-09 06:10:42 +00001314 Out << " " << PAL->getParamAttrsTextByIndex(0);
Reid Spencer2c261782007-01-05 17:06:19 +00001315 Out << "\n\t\t\tto";
Chris Lattnere02fa852001-10-13 06:42:36 +00001316 writeOperand(II->getNormalDest(), true);
Misha Brukman0313e0b2004-06-21 21:53:56 +00001317 Out << " unwind";
Chris Lattneraeb2a1d2004-02-08 21:44:31 +00001318 writeOperand(II->getUnwindDest(), true);
Chris Lattnere02fa852001-10-13 06:42:36 +00001319
Chris Lattner7e708292002-06-25 16:13:24 +00001320 } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(&I)) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001321 Out << ' ';
Chris Lattner94dc1f22002-04-13 18:34:38 +00001322 printType(AI->getType()->getElementType());
1323 if (AI->isArrayAllocation()) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001324 Out << ',';
Chris Lattner94dc1f22002-04-13 18:34:38 +00001325 writeOperand(AI->getArraySize(), true);
Chris Lattner00950542001-06-06 20:29:01 +00001326 }
Nate Begeman14b05292005-11-05 09:21:28 +00001327 if (AI->getAlignment()) {
Chris Lattner9fad0b92005-11-05 21:20:34 +00001328 Out << ", align " << AI->getAlignment();
Nate Begeman14b05292005-11-05 09:21:28 +00001329 }
Chris Lattnere02fa852001-10-13 06:42:36 +00001330 } else if (isa<CastInst>(I)) {
Chris Lattner41495a22003-11-17 01:17:04 +00001331 if (Operand) writeOperand(Operand, true); // Work with broken code
Misha Brukman0313e0b2004-06-21 21:53:56 +00001332 Out << " to ";
Chris Lattner7e708292002-06-25 16:13:24 +00001333 printType(I.getType());
Chris Lattner4d45bd02003-10-18 05:57:43 +00001334 } else if (isa<VAArgInst>(I)) {
Chris Lattner41495a22003-11-17 01:17:04 +00001335 if (Operand) writeOperand(Operand, true); // Work with broken code
Misha Brukman0313e0b2004-06-21 21:53:56 +00001336 Out << ", ";
Chris Lattner8f77dae2003-05-08 02:44:12 +00001337 printType(I.getType());
Chris Lattner00950542001-06-06 20:29:01 +00001338 } else if (Operand) { // Print the normal way...
1339
Misha Brukmanfd939082005-04-21 23:48:37 +00001340 // PrintAllTypes - Instructions who have operands of all the same type
Chris Lattner00950542001-06-06 20:29:01 +00001341 // omit the type from all but the first operand. If the instruction has
1342 // different type operands (for example br), then they are all printed.
1343 bool PrintAllTypes = false;
1344 const Type *TheType = Operand->getType();
Chris Lattner00950542001-06-06 20:29:01 +00001345
Reid Spencerebe57e32007-02-02 13:54:55 +00001346 // Select, Store and ShuffleVector always print all types.
Reid Spencer832254e2007-02-02 02:16:23 +00001347 if (isa<SelectInst>(I) || isa<StoreInst>(I) || isa<ShuffleVectorInst>(I)) {
Chris Lattnerffd9bf42003-04-16 20:20:02 +00001348 PrintAllTypes = true;
1349 } else {
1350 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
1351 Operand = I.getOperand(i);
1352 if (Operand->getType() != TheType) {
1353 PrintAllTypes = true; // We have differing types! Print them all!
1354 break;
1355 }
Chris Lattner00950542001-06-06 20:29:01 +00001356 }
1357 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001358
Chris Lattnerc1824992001-10-29 16:05:51 +00001359 if (!PrintAllTypes) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001360 Out << ' ';
Chris Lattnerffd9bf42003-04-16 20:20:02 +00001361 printType(TheType);
Chris Lattnerc1824992001-10-29 16:05:51 +00001362 }
Chris Lattner00950542001-06-06 20:29:01 +00001363
Chris Lattner7e708292002-06-25 16:13:24 +00001364 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001365 if (i) Out << ',';
Chris Lattner7e708292002-06-25 16:13:24 +00001366 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner00950542001-06-06 20:29:01 +00001367 }
1368 }
Christopher Lamb43c7f372007-04-22 19:24:39 +00001369
1370 // Print post operand alignment for load/store
1371 if (isa<LoadInst>(I) && cast<LoadInst>(I).getAlignment()) {
1372 Out << ", align " << cast<LoadInst>(I).getAlignment();
1373 } else if (isa<StoreInst>(I) && cast<StoreInst>(I).getAlignment()) {
1374 Out << ", align " << cast<StoreInst>(I).getAlignment();
1375 }
Chris Lattner00950542001-06-06 20:29:01 +00001376
Chris Lattnere02fa852001-10-13 06:42:36 +00001377 printInfoComment(I);
Misha Brukman0313e0b2004-06-21 21:53:56 +00001378 Out << "\n";
Chris Lattner00950542001-06-06 20:29:01 +00001379}
1380
1381
1382//===----------------------------------------------------------------------===//
1383// External Interface declarations
1384//===----------------------------------------------------------------------===//
1385
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001386void Module::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001387 SlotMachine SlotTable(this);
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001388 AssemblyWriter W(o, SlotTable, this, AAW);
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001389 W.write(this);
Chris Lattner00950542001-06-06 20:29:01 +00001390}
1391
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001392void GlobalVariable::print(std::ostream &o) const {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001393 SlotMachine SlotTable(getParent());
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001394 AssemblyWriter W(o, SlotTable, getParent(), 0);
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001395 W.write(this);
Chris Lattnerb0e45232001-09-10 20:08:19 +00001396}
1397
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001398void GlobalAlias::print(std::ostream &o) const {
1399 SlotMachine SlotTable(getParent());
1400 AssemblyWriter W(o, SlotTable, getParent(), 0);
1401 W.write(this);
1402}
1403
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001404void Function::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001405 SlotMachine SlotTable(getParent());
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001406 AssemblyWriter W(o, SlotTable, getParent(), AAW);
Chris Lattner00950542001-06-06 20:29:01 +00001407
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001408 W.write(this);
Chris Lattner00950542001-06-06 20:29:01 +00001409}
1410
Chris Lattnercc041ba2006-01-24 04:13:11 +00001411void InlineAsm::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Chris Lattner2fcfdb72006-12-06 06:24:27 +00001412 WriteAsOperand(o, this, true, 0);
Chris Lattnercc041ba2006-01-24 04:13:11 +00001413}
1414
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001415void BasicBlock::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001416 SlotMachine SlotTable(getParent());
Misha Brukmanfd939082005-04-21 23:48:37 +00001417 AssemblyWriter W(o, SlotTable,
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001418 getParent() ? getParent()->getParent() : 0, AAW);
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001419 W.write(this);
Chris Lattner00950542001-06-06 20:29:01 +00001420}
1421
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001422void Instruction::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001423 const Function *F = getParent() ? getParent()->getParent() : 0;
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001424 SlotMachine SlotTable(F);
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001425 AssemblyWriter W(o, SlotTable, F ? F->getParent() : 0, AAW);
Chris Lattner00950542001-06-06 20:29:01 +00001426
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001427 W.write(this);
Chris Lattner00950542001-06-06 20:29:01 +00001428}
Chris Lattnerda1fbcc2001-11-07 04:21:57 +00001429
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001430void Constant::print(std::ostream &o) const {
1431 if (this == 0) { o << "<null> constant value\n"; return; }
Chris Lattner3bc06b32002-09-10 15:53:49 +00001432
Misha Brukman40c732c2004-06-04 21:11:51 +00001433 o << ' ' << getType()->getDescription() << ' ';
Evan Chenga4ffcc22006-03-01 22:17:00 +00001434
1435 std::map<const Type *, std::string> TypeTable;
Chris Lattner2fcfdb72006-12-06 06:24:27 +00001436 WriteConstantInt(o, this, TypeTable, 0);
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001437}
1438
Misha Brukmanfd939082005-04-21 23:48:37 +00001439void Type::print(std::ostream &o) const {
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001440 if (this == 0)
1441 o << "<null Type>";
1442 else
1443 o << getDescription();
1444}
1445
Chris Lattner73e21422002-04-09 19:48:49 +00001446void Argument::print(std::ostream &o) const {
Chris Lattner2fcfdb72006-12-06 06:24:27 +00001447 WriteAsOperand(o, this, true, getParent() ? getParent()->getParent() : 0);
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001448}
1449
Reid Spencerfa452c02004-05-25 18:14:38 +00001450// Value::dump - allow easy printing of Values from the debugger.
1451// Located here because so much of the needed functionality is here.
Bill Wendling832171c2006-12-07 20:04:42 +00001452void Value::dump() const { print(*cerr.stream()); cerr << '\n'; }
Reid Spencerfa452c02004-05-25 18:14:38 +00001453
1454// Type::dump - allow easy printing of Values from the debugger.
1455// Located here because so much of the needed functionality is here.
Bill Wendling832171c2006-12-07 20:04:42 +00001456void Type::dump() const { print(*cerr.stream()); cerr << '\n'; }
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001457
Reid Spencer4f859aa2007-04-22 05:46:44 +00001458void
1459ParamAttrsList::dump() const {
1460 cerr << "PAL[ ";
1461 for (unsigned i = 0; i < attrs.size(); ++i) {
1462 uint16_t index = getParamIndex(i);
1463 uint16_t attrs = getParamAttrs(index);
1464 cerr << "{" << index << "," << attrs << "} ";
1465 }
1466 cerr << "]\n";
1467}
1468
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001469//===----------------------------------------------------------------------===//
Chris Lattner82c4bc72006-12-06 06:40:49 +00001470// SlotMachine Implementation
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001471//===----------------------------------------------------------------------===//
1472
1473#if 0
Bill Wendlinge8156192006-12-07 01:30:32 +00001474#define SC_DEBUG(X) cerr << X
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001475#else
1476#define SC_DEBUG(X)
1477#endif
1478
1479// Module level constructor. Causes the contents of the Module (sans functions)
1480// to be added to the slot table.
Misha Brukmanfd939082005-04-21 23:48:37 +00001481SlotMachine::SlotMachine(const Module *M)
Reid Spencerb03de0c2004-05-26 21:56:09 +00001482 : TheModule(M) ///< Saved for lazy initialization.
1483 , TheFunction(0)
Reid Spencer28531c72004-08-16 07:46:33 +00001484 , FunctionProcessed(false)
Reid Spencer590b3c52007-03-19 18:32:53 +00001485 , mMap(), mNext(0), fMap(), fNext(0)
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001486{
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001487}
1488
1489// Function level constructor. Causes the contents of the Module and the one
1490// function provided to be added to the slot table.
Chris Lattnerc96ce892006-12-06 05:12:21 +00001491SlotMachine::SlotMachine(const Function *F)
1492 : TheModule(F ? F->getParent() : 0) ///< Saved for lazy initialization
Reid Spencerb03de0c2004-05-26 21:56:09 +00001493 , TheFunction(F) ///< Saved for lazy initialization
Reid Spencer28531c72004-08-16 07:46:33 +00001494 , FunctionProcessed(false)
Reid Spencer590b3c52007-03-19 18:32:53 +00001495 , mMap(), mNext(0), fMap(), fNext(0)
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001496{
Reid Spencerb03de0c2004-05-26 21:56:09 +00001497}
1498
Chris Lattner22379bc2007-01-11 03:54:27 +00001499inline void SlotMachine::initialize() {
Chris Lattnerc96ce892006-12-06 05:12:21 +00001500 if (TheModule) {
Misha Brukmanfd939082005-04-21 23:48:37 +00001501 processModule();
Reid Spencerb03de0c2004-05-26 21:56:09 +00001502 TheModule = 0; ///< Prevent re-processing next time we're called.
1503 }
Chris Lattnerde891a62006-12-06 05:27:40 +00001504 if (TheFunction && !FunctionProcessed)
Misha Brukmanfd939082005-04-21 23:48:37 +00001505 processFunction();
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001506}
1507
1508// Iterate through all the global variables, functions, and global
Misha Brukmanfd939082005-04-21 23:48:37 +00001509// variable initializers and create slots for them.
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001510void SlotMachine::processModule() {
1511 SC_DEBUG("begin processModule!\n");
1512
Chris Lattnera6b1ffc2006-12-06 06:15:43 +00001513 // Add all of the unnamed global variables to the value table.
Chris Lattnerd6d826c2006-12-06 04:41:52 +00001514 for (Module::const_global_iterator I = TheModule->global_begin(),
1515 E = TheModule->global_end(); I != E; ++I)
Chris Lattnera6b1ffc2006-12-06 06:15:43 +00001516 if (!I->hasName())
Chris Lattner9446bbe2007-01-09 07:55:49 +00001517 CreateModuleSlot(I);
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001518
Chris Lattnera6b1ffc2006-12-06 06:15:43 +00001519 // Add all the unnamed functions to the table.
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001520 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
1521 I != E; ++I)
Chris Lattnera6b1ffc2006-12-06 06:15:43 +00001522 if (!I->hasName())
Chris Lattner9446bbe2007-01-09 07:55:49 +00001523 CreateModuleSlot(I);
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001524
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001525 SC_DEBUG("end processModule!\n");
1526}
1527
1528
Reid Spencerb03de0c2004-05-26 21:56:09 +00001529// Process the arguments, basic blocks, and instructions of a function.
1530void SlotMachine::processFunction() {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001531 SC_DEBUG("begin processFunction!\n");
Reid Spencer590b3c52007-03-19 18:32:53 +00001532 fNext = 0;
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001533
Chris Lattnera6b1ffc2006-12-06 06:15:43 +00001534 // Add all the function arguments with no names.
Misha Brukmanfd939082005-04-21 23:48:37 +00001535 for(Function::const_arg_iterator AI = TheFunction->arg_begin(),
Chris Lattnere4d5c442005-03-15 04:54:21 +00001536 AE = TheFunction->arg_end(); AI != AE; ++AI)
Chris Lattnera6b1ffc2006-12-06 06:15:43 +00001537 if (!AI->hasName())
Chris Lattner9446bbe2007-01-09 07:55:49 +00001538 CreateFunctionSlot(AI);
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001539
1540 SC_DEBUG("Inserting Instructions:\n");
1541
Chris Lattnera6b1ffc2006-12-06 06:15:43 +00001542 // Add all of the basic blocks and instructions with no names.
Misha Brukmanfd939082005-04-21 23:48:37 +00001543 for (Function::const_iterator BB = TheFunction->begin(),
Reid Spencerb03de0c2004-05-26 21:56:09 +00001544 E = TheFunction->end(); BB != E; ++BB) {
Chris Lattnera6b1ffc2006-12-06 06:15:43 +00001545 if (!BB->hasName())
Chris Lattner9446bbe2007-01-09 07:55:49 +00001546 CreateFunctionSlot(BB);
Chris Lattnera6b1ffc2006-12-06 06:15:43 +00001547 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
1548 if (I->getType() != Type::VoidTy && !I->hasName())
Chris Lattner9446bbe2007-01-09 07:55:49 +00001549 CreateFunctionSlot(I);
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001550 }
1551
Reid Spencer28531c72004-08-16 07:46:33 +00001552 FunctionProcessed = true;
1553
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001554 SC_DEBUG("end processFunction!\n");
1555}
1556
Chris Lattnerde891a62006-12-06 05:27:40 +00001557/// Clean up after incorporating a function. This is the only way to get out of
Chris Lattner22379bc2007-01-11 03:54:27 +00001558/// the function incorporation state that affects get*Slot/Create*Slot. Function
Chris Lattner3a4621c2007-01-09 07:46:21 +00001559/// incorporation state is indicated by TheFunction != 0.
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001560void SlotMachine::purgeFunction() {
1561 SC_DEBUG("begin purgeFunction!\n");
1562 fMap.clear(); // Simply discard the function level map
Reid Spencerb03de0c2004-05-26 21:56:09 +00001563 TheFunction = 0;
Reid Spencer28531c72004-08-16 07:46:33 +00001564 FunctionProcessed = false;
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001565 SC_DEBUG("end purgeFunction!\n");
1566}
1567
Chris Lattner22379bc2007-01-11 03:54:27 +00001568/// getGlobalSlot - Get the slot number of a global value.
1569int SlotMachine::getGlobalSlot(const GlobalValue *V) {
1570 // Check for uninitialized state and do lazy initialization.
1571 initialize();
1572
1573 // Find the type plane in the module map
Reid Spencer590b3c52007-03-19 18:32:53 +00001574 ValueMap::const_iterator MI = mMap.find(V);
Chris Lattner22379bc2007-01-11 03:54:27 +00001575 if (MI == mMap.end()) return -1;
Reid Spencer590b3c52007-03-19 18:32:53 +00001576
1577 return MI->second;
Chris Lattner22379bc2007-01-11 03:54:27 +00001578}
Reid Spencerb03de0c2004-05-26 21:56:09 +00001579
Chris Lattner22379bc2007-01-11 03:54:27 +00001580
1581/// getLocalSlot - Get the slot number for a value that is local to a function.
1582int SlotMachine::getLocalSlot(const Value *V) {
1583 assert(!isa<Constant>(V) && "Can't get a constant or global slot with this!");
1584
1585 // Check for uninitialized state and do lazy initialization.
1586 initialize();
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001587
Reid Spencer590b3c52007-03-19 18:32:53 +00001588 ValueMap::const_iterator FI = fMap.find(V);
Chris Lattner22379bc2007-01-11 03:54:27 +00001589 if (FI == fMap.end()) return -1;
1590
Reid Spencer590b3c52007-03-19 18:32:53 +00001591 return FI->second;
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001592}
1593
Reid Spencer0e25e1c2004-07-04 11:50:43 +00001594
Chris Lattner9446bbe2007-01-09 07:55:49 +00001595/// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
1596void SlotMachine::CreateModuleSlot(const GlobalValue *V) {
Chris Lattnercf8790a2007-01-09 08:04:59 +00001597 assert(V && "Can't insert a null Value into SlotMachine!");
Reid Spencer590b3c52007-03-19 18:32:53 +00001598 assert(V->getType() != Type::VoidTy && "Doesn't need a slot!");
1599 assert(!V->hasName() && "Doesn't need a slot!");
Chris Lattnercf8790a2007-01-09 08:04:59 +00001600
Reid Spencer590b3c52007-03-19 18:32:53 +00001601 unsigned DestSlot = mNext++;
1602 mMap[V] = DestSlot;
Chris Lattnercf8790a2007-01-09 08:04:59 +00001603
Reid Spencer590b3c52007-03-19 18:32:53 +00001604 SC_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
Chris Lattnercf8790a2007-01-09 08:04:59 +00001605 DestSlot << " [");
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001606 // G = Global, F = Function, A = Alias, o = other
1607 SC_DEBUG((isa<GlobalVariable>(V) ? 'G' :
1608 (isa<Function> ? 'F' :
1609 (isa<GlobalAlias> ? 'A' : 'o'))) << "]\n");
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001610}
1611
1612
Chris Lattner9446bbe2007-01-09 07:55:49 +00001613/// CreateSlot - Create a new slot for the specified value if it has no name.
1614void SlotMachine::CreateFunctionSlot(const Value *V) {
1615 const Type *VTy = V->getType();
1616 assert(VTy != Type::VoidTy && !V->hasName() && "Doesn't need a slot!");
Chris Lattnercf8790a2007-01-09 08:04:59 +00001617
Reid Spencer590b3c52007-03-19 18:32:53 +00001618 unsigned DestSlot = fNext++;
1619 fMap[V] = DestSlot;
Chris Lattnercf8790a2007-01-09 08:04:59 +00001620
Chris Lattner4932a5a2006-12-06 05:55:41 +00001621 // G = Global, F = Function, o = other
Chris Lattnercf8790a2007-01-09 08:04:59 +00001622 SC_DEBUG(" Inserting value [" << VTy << "] = " << V << " slot=" <<
1623 DestSlot << " [o]\n");
1624}