blob: 3f72cfbb900fc402778befb918e3e7303d419082 [file] [log] [blame]
Chris Lattner8da78af2002-04-07 22:31:46 +00001//===-- AsmWriter.cpp - Printing LLVM as an assembly file -----------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// 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.
7//
8//===----------------------------------------------------------------------===//
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 Lattnerda1fbcc2001-11-07 04:21:57 +000017#include "llvm/Assembly/CachedWriter.h"
Chris Lattner75cf7cf2002-04-08 22:03:40 +000018#include "llvm/Assembly/Writer.h"
Chris Lattnerf082b802002-07-23 18:07:49 +000019#include "llvm/Assembly/PrintModulePass.h"
Chris Lattner95e5a2c2003-10-30 23:41:03 +000020#include "llvm/Assembly/AsmAnnotationWriter.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"
Vikram S. Adveb4dbb442002-07-14 23:14:45 +000023#include "llvm/Instruction.h"
Misha Brukman44336292004-07-29 16:53:53 +000024#include "llvm/Instructions.h"
Chris Lattnerf2d577b2004-01-20 19:50:34 +000025#include "llvm/Module.h"
Chris Lattner007377f2001-09-07 16:36:04 +000026#include "llvm/SymbolTable.h"
Misha Brukman5cf1acf2004-04-28 15:31:21 +000027#include "llvm/Assembly/Writer.h"
Chris Lattner061269b2002-10-02 19:38:55 +000028#include "llvm/Support/CFG.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000029#include "llvm/ADT/StringExtras.h"
30#include "llvm/ADT/STLExtras.h"
Chris Lattner007377f2001-09-07 16:36:04 +000031#include <algorithm>
Chris Lattner31f84992003-11-21 20:23:48 +000032using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000033
Chris Lattner0a8e8e12004-07-15 02:51:31 +000034namespace llvm {
Reid Spencer0d1b77e2004-05-26 07:18:52 +000035
36/// This class provides computation of slot numbers for LLVM Assembly writing.
37/// @brief LLVM Assembly Writing Slot Computation.
38class SlotMachine {
39
40/// @name Types
41/// @{
42public:
43
44 /// @brief A mapping of Values to slot numbers
45 typedef std::map<const Value*, unsigned> ValueMap;
Reid Spencer0e25e1c2004-07-04 11:50:43 +000046 typedef std::map<const Type*, unsigned> TypeMap;
Reid Spencer0d1b77e2004-05-26 07:18:52 +000047
48 /// @brief A plane with next slot number and ValueMap
Reid Spencer0e25e1c2004-07-04 11:50:43 +000049 struct ValuePlane {
Reid Spencer0d1b77e2004-05-26 07:18:52 +000050 unsigned next_slot; ///< The next slot number to use
51 ValueMap map; ///< The map of Value* -> unsigned
Reid Spencer0e25e1c2004-07-04 11:50:43 +000052 ValuePlane() { next_slot = 0; } ///< Make sure we start at 0
53 };
54
55 struct TypePlane {
56 unsigned next_slot;
57 TypeMap map;
58 TypePlane() { next_slot = 0; }
59 void clear() { map.clear(); next_slot = 0; }
Reid Spencer0d1b77e2004-05-26 07:18:52 +000060 };
61
62 /// @brief The map of planes by Type
Reid Spencer0e25e1c2004-07-04 11:50:43 +000063 typedef std::map<const Type*, ValuePlane> TypedPlanes;
Reid Spencer0d1b77e2004-05-26 07:18:52 +000064
65/// @}
66/// @name Constructors
67/// @{
68public:
69 /// @brief Construct from a module
70 SlotMachine(const Module *M );
71
72 /// @brief Construct from a function, starting out in incorp state.
73 SlotMachine(const Function *F );
74
75/// @}
76/// @name Accessors
77/// @{
78public:
79 /// Return the slot number of the specified value in it's type
80 /// plane. Its an error to ask for something not in the SlotMachine.
81 /// Its an error to ask for a Type*
Chris Lattner69566452004-06-09 19:41:19 +000082 int getSlot(const Value *V);
Reid Spencer0e25e1c2004-07-04 11:50:43 +000083 int getSlot(const Type*Ty);
Reid Spencerfc621e22004-06-09 15:26:53 +000084
85 /// Determine if a Value has a slot or not
86 bool hasSlot(const Value* V);
Reid Spencer0e25e1c2004-07-04 11:50:43 +000087 bool hasSlot(const Type* Ty);
Reid Spencer0d1b77e2004-05-26 07:18:52 +000088
89/// @}
90/// @name Mutators
91/// @{
92public:
93 /// If you'd like to deal with a function instead of just a module, use
94 /// this method to get its data into the SlotMachine.
Reid Spencer28531c72004-08-16 07:46:33 +000095 void incorporateFunction(const Function *F) {
96 TheFunction = F;
97 FunctionProcessed = false;
98 }
Reid Spencer0d1b77e2004-05-26 07:18:52 +000099
100 /// After calling incorporateFunction, use this method to remove the
101 /// most recently incorporated function from the SlotMachine. This
102 /// will reset the state of the machine back to just the module contents.
103 void purgeFunction();
104
105/// @}
106/// @name Implementation Details
107/// @{
108private:
Reid Spencerb03de0c2004-05-26 21:56:09 +0000109 /// This function does the actual initialization.
110 inline void initialize();
111
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000112 /// Values can be crammed into here at will. If they haven't
113 /// been inserted already, they get inserted, otherwise they are ignored.
114 /// Either way, the slot number for the Value* is returned.
115 unsigned createSlot(const Value *V);
Reid Spencer0e25e1c2004-07-04 11:50:43 +0000116 unsigned createSlot(const Type* Ty);
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000117
118 /// Insert a value into the value table. Return the slot number
119 /// that it now occupies. BadThings(TM) will happen if you insert a
120 /// Value that's already been inserted.
121 unsigned insertValue( const Value *V );
Reid Spencer0e25e1c2004-07-04 11:50:43 +0000122 unsigned insertValue( const Type* Ty);
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000123
124 /// Add all of the module level global variables (and their initializers)
125 /// and function declarations, but not the contents of those functions.
126 void processModule();
127
Reid Spencerb03de0c2004-05-26 21:56:09 +0000128 /// Add all of the functions arguments, basic blocks, and instructions
129 void processFunction();
130
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000131 SlotMachine(const SlotMachine &); // DO NOT IMPLEMENT
132 void operator=(const SlotMachine &); // DO NOT IMPLEMENT
133
134/// @}
135/// @name Data
136/// @{
137public:
138
139 /// @brief The module for which we are holding slot numbers
Reid Spencerb03de0c2004-05-26 21:56:09 +0000140 const Module* TheModule;
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000141
Reid Spencerb03de0c2004-05-26 21:56:09 +0000142 /// @brief The function for which we are holding slot numbers
143 const Function* TheFunction;
Reid Spencer28531c72004-08-16 07:46:33 +0000144 bool FunctionProcessed;
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000145
146 /// @brief The TypePlanes map for the module level data
147 TypedPlanes mMap;
Reid Spencer0e25e1c2004-07-04 11:50:43 +0000148 TypePlane mTypes;
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000149
150 /// @brief The TypePlanes map for the function level data
151 TypedPlanes fMap;
Reid Spencer0e25e1c2004-07-04 11:50:43 +0000152 TypePlane fTypes;
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000153
154/// @}
155
156};
157
Chris Lattner0a8e8e12004-07-15 02:51:31 +0000158} // end namespace llvm
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000159
Chris Lattnera6275cc2002-07-26 21:12:46 +0000160static RegisterPass<PrintModulePass>
161X("printm", "Print module to stderr",PassInfo::Analysis|PassInfo::Optimization);
162static RegisterPass<PrintFunctionPass>
163Y("print","Print function to stderr",PassInfo::Analysis|PassInfo::Optimization);
Chris Lattnerf082b802002-07-23 18:07:49 +0000164
Chris Lattner7b13f562003-05-08 02:08:14 +0000165static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
166 bool PrintName,
167 std::map<const Type *, std::string> &TypeTable,
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000168 SlotMachine *Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000169
Reid Spencer0e25e1c2004-07-04 11:50:43 +0000170static void WriteAsOperandInternal(std::ostream &Out, const Type *T,
171 bool PrintName,
172 std::map<const Type *, std::string> &TypeTable,
173 SlotMachine *Machine);
174
Chris Lattner207b5bc2001-10-29 16:37:48 +0000175static const Module *getModuleFromVal(const Value *V) {
Chris Lattner949a3622003-07-23 15:30:06 +0000176 if (const Argument *MA = dyn_cast<Argument>(V))
Chris Lattner207b5bc2001-10-29 16:37:48 +0000177 return MA->getParent() ? MA->getParent()->getParent() : 0;
Chris Lattner949a3622003-07-23 15:30:06 +0000178 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
Chris Lattner207b5bc2001-10-29 16:37:48 +0000179 return BB->getParent() ? BB->getParent()->getParent() : 0;
Chris Lattner949a3622003-07-23 15:30:06 +0000180 else if (const Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000181 const Function *M = I->getParent() ? I->getParent()->getParent() : 0;
Chris Lattner207b5bc2001-10-29 16:37:48 +0000182 return M ? M->getParent() : 0;
Chris Lattner949a3622003-07-23 15:30:06 +0000183 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
Chris Lattner207b5bc2001-10-29 16:37:48 +0000184 return GV->getParent();
Chris Lattner207b5bc2001-10-29 16:37:48 +0000185 return 0;
186}
187
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000188static SlotMachine *createSlotMachine(const Value *V) {
Chris Lattner949a3622003-07-23 15:30:06 +0000189 if (const Argument *FA = dyn_cast<Argument>(V)) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000190 return new SlotMachine(FA->getParent());
Chris Lattner949a3622003-07-23 15:30:06 +0000191 } else if (const Instruction *I = dyn_cast<Instruction>(V)) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000192 return new SlotMachine(I->getParent()->getParent());
Chris Lattner949a3622003-07-23 15:30:06 +0000193 } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000194 return new SlotMachine(BB->getParent());
Chris Lattner949a3622003-07-23 15:30:06 +0000195 } else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)){
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000196 return new SlotMachine(GV->getParent());
Chris Lattner949a3622003-07-23 15:30:06 +0000197 } else if (const Function *Func = dyn_cast<Function>(V)) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000198 return new SlotMachine(Func);
Chris Lattnerc1824992001-10-29 16:05:51 +0000199 }
200 return 0;
201}
Chris Lattner00950542001-06-06 20:29:01 +0000202
Chris Lattner24b8a5d2003-08-22 05:40:38 +0000203// getLLVMName - Turn the specified string into an 'LLVM name', which is either
204// prefixed with % (if the string only contains simple characters) or is
205// surrounded with ""'s (if it has special chars in it).
Alkis Evlogimenos9913e592004-12-10 05:41:10 +0000206static std::string getLLVMName(const std::string &Name,
207 bool prefixName = true) {
Chris Lattner24b8a5d2003-08-22 05:40:38 +0000208 assert(!Name.empty() && "Cannot get empty name!");
209
210 // First character cannot start with a number...
211 if (Name[0] >= '0' && Name[0] <= '9')
212 return "\"" + Name + "\"";
213
214 // Scan to see if we have any characters that are not on the "white list"
215 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
216 char C = Name[i];
217 assert(C != '"' && "Illegal character in LLVM value name!");
218 if ((C < 'a' || C > 'z') && (C < 'A' || C > 'Z') && (C < '0' || C > '9') &&
219 C != '-' && C != '.' && C != '_')
220 return "\"" + Name + "\"";
221 }
222
223 // If we get here, then the identifier is legal to use as a "VarID".
Alkis Evlogimenos9913e592004-12-10 05:41:10 +0000224 if (prefixName)
225 return "%"+Name;
226 else
227 return Name;
Chris Lattner24b8a5d2003-08-22 05:40:38 +0000228}
229
Chris Lattner207b5bc2001-10-29 16:37:48 +0000230
Misha Brukmanab5c6002004-03-02 00:22:19 +0000231/// fillTypeNameTable - If the module has a symbol table, take all global types
232/// and stuff their names into the TypeNames map.
233///
Chris Lattner207b5bc2001-10-29 16:37:48 +0000234static void fillTypeNameTable(const Module *M,
Chris Lattner7b13f562003-05-08 02:08:14 +0000235 std::map<const Type *, std::string> &TypeNames) {
Chris Lattner6e6026b2002-11-20 18:36:02 +0000236 if (!M) return;
237 const SymbolTable &ST = M->getSymbolTable();
Reid Spencer9231ac82004-05-25 08:53:40 +0000238 SymbolTable::type_const_iterator TI = ST.type_begin();
239 for (; TI != ST.type_end(); ++TI ) {
240 // As a heuristic, don't insert pointer to primitive types, because
241 // they are used too often to have a single useful name.
242 //
243 const Type *Ty = cast<Type>(TI->second);
244 if (!isa<PointerType>(Ty) ||
Reid Spencerb03de0c2004-05-26 21:56:09 +0000245 !cast<PointerType>(Ty)->getElementType()->isPrimitiveType() ||
246 isa<OpaqueType>(cast<PointerType>(Ty)->getElementType()))
Reid Spencer9231ac82004-05-25 08:53:40 +0000247 TypeNames.insert(std::make_pair(Ty, getLLVMName(TI->first)));
Chris Lattner207b5bc2001-10-29 16:37:48 +0000248 }
249}
250
251
252
John Criswell4ff620a2004-06-01 14:54:08 +0000253static void calcTypeName(const Type *Ty,
254 std::vector<const Type *> &TypeStack,
255 std::map<const Type *, std::string> &TypeNames,
256 std::string & Result){
257 if (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty)) {
258 Result += Ty->getDescription(); // Base case
259 return;
260 }
Chris Lattner207b5bc2001-10-29 16:37:48 +0000261
262 // Check to see if the type is named.
Chris Lattner7b13f562003-05-08 02:08:14 +0000263 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
John Criswell4ff620a2004-06-01 14:54:08 +0000264 if (I != TypeNames.end()) {
265 Result += I->second;
266 return;
267 }
Chris Lattner207b5bc2001-10-29 16:37:48 +0000268
John Criswell4ff620a2004-06-01 14:54:08 +0000269 if (isa<OpaqueType>(Ty)) {
270 Result += "opaque";
271 return;
272 }
Chris Lattner88c17382003-10-30 00:22:33 +0000273
Chris Lattner207b5bc2001-10-29 16:37:48 +0000274 // Check to see if the Type is already on the stack...
275 unsigned Slot = 0, CurSize = TypeStack.size();
276 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
277
278 // This is another base case for the recursion. In this case, we know
279 // that we have looped back to a type that we have previously visited.
280 // Generate the appropriate upreference to handle this.
John Criswell4ff620a2004-06-01 14:54:08 +0000281 if (Slot < CurSize) {
282 Result += "\\" + utostr(CurSize-Slot); // Here's the upreference
283 return;
284 }
Chris Lattner207b5bc2001-10-29 16:37:48 +0000285
286 TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
287
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000288 switch (Ty->getTypeID()) {
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000289 case Type::FunctionTyID: {
Chris Lattner949a3622003-07-23 15:30:06 +0000290 const FunctionType *FTy = cast<FunctionType>(Ty);
John Criswell4ff620a2004-06-01 14:54:08 +0000291 calcTypeName(FTy->getReturnType(), TypeStack, TypeNames, Result);
292 Result += " (";
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);
Chris Lattner207b5bc2001-10-29 16:37:48 +0000298 }
Chris Lattner2761e9f2002-04-13 20:53:41 +0000299 if (FTy->isVarArg()) {
Chris Lattnerd5d89962004-02-09 04:14:01 +0000300 if (FTy->getNumParams()) Result += ", ";
Chris Lattner207b5bc2001-10-29 16:37:48 +0000301 Result += "...";
302 }
303 Result += ")";
304 break;
305 }
306 case Type::StructTyID: {
Chris Lattner949a3622003-07-23 15:30:06 +0000307 const StructType *STy = cast<StructType>(Ty);
John Criswell4ff620a2004-06-01 14:54:08 +0000308 Result += "{ ";
Chris Lattnerd21cd802004-02-09 04:37:31 +0000309 for (StructType::element_iterator I = STy->element_begin(),
310 E = STy->element_end(); I != E; ++I) {
311 if (I != STy->element_begin())
Chris Lattner207b5bc2001-10-29 16:37:48 +0000312 Result += ", ";
John Criswell4ff620a2004-06-01 14:54:08 +0000313 calcTypeName(*I, TypeStack, TypeNames, Result);
Chris Lattner207b5bc2001-10-29 16:37:48 +0000314 }
315 Result += " }";
316 break;
317 }
318 case Type::PointerTyID:
John Criswell4ff620a2004-06-01 14:54:08 +0000319 calcTypeName(cast<PointerType>(Ty)->getElementType(),
320 TypeStack, TypeNames, Result);
321 Result += "*";
Chris Lattner207b5bc2001-10-29 16:37:48 +0000322 break;
323 case Type::ArrayTyID: {
Chris Lattner949a3622003-07-23 15:30:06 +0000324 const ArrayType *ATy = cast<ArrayType>(Ty);
John Criswell4ff620a2004-06-01 14:54:08 +0000325 Result += "[" + utostr(ATy->getNumElements()) + " x ";
326 calcTypeName(ATy->getElementType(), TypeStack, TypeNames, Result);
327 Result += "]";
Chris Lattner207b5bc2001-10-29 16:37:48 +0000328 break;
329 }
Brian Gaeke715c90b2004-08-20 06:00:58 +0000330 case Type::PackedTyID: {
331 const PackedType *PTy = cast<PackedType>(Ty);
332 Result += "<" + utostr(PTy->getNumElements()) + " x ";
333 calcTypeName(PTy->getElementType(), TypeStack, TypeNames, Result);
334 Result += ">";
335 break;
336 }
Chris Lattner9e094c42003-05-14 17:50:47 +0000337 case Type::OpaqueTyID:
John Criswell4ff620a2004-06-01 14:54:08 +0000338 Result += "opaque";
Chris Lattner9e094c42003-05-14 17:50:47 +0000339 break;
Chris Lattner207b5bc2001-10-29 16:37:48 +0000340 default:
John Criswell4ff620a2004-06-01 14:54:08 +0000341 Result += "<unrecognized-type>";
Chris Lattner207b5bc2001-10-29 16:37:48 +0000342 }
343
344 TypeStack.pop_back(); // Remove self from stack...
John Criswell4ff620a2004-06-01 14:54:08 +0000345 return;
Chris Lattner207b5bc2001-10-29 16:37:48 +0000346}
347
348
Misha Brukman9d0802e2004-03-01 19:48:13 +0000349/// printTypeInt - The internal guts of printing out a type that has a
350/// potentially named portion.
351///
Chris Lattner7b13f562003-05-08 02:08:14 +0000352static std::ostream &printTypeInt(std::ostream &Out, const Type *Ty,
353 std::map<const Type *, std::string> &TypeNames) {
Chris Lattner207b5bc2001-10-29 16:37:48 +0000354 // Primitive types always print out their description, regardless of whether
355 // they have been named or not.
356 //
Chris Lattnerdaf2a492003-10-30 00:12:51 +0000357 if (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty))
358 return Out << Ty->getDescription();
Chris Lattner207b5bc2001-10-29 16:37:48 +0000359
360 // Check to see if the type is named.
Chris Lattner7b13f562003-05-08 02:08:14 +0000361 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
Chris Lattner207b5bc2001-10-29 16:37:48 +0000362 if (I != TypeNames.end()) return Out << I->second;
363
364 // Otherwise we have a type that has not been named but is a derived type.
365 // Carefully recurse the type hierarchy to print out any contained symbolic
366 // names.
367 //
Chris Lattner7b13f562003-05-08 02:08:14 +0000368 std::vector<const Type *> TypeStack;
John Criswell4ff620a2004-06-01 14:54:08 +0000369 std::string TypeName;
370 calcTypeName(Ty, TypeStack, TypeNames, TypeName);
Chris Lattner697954c2002-01-20 22:54:45 +0000371 TypeNames.insert(std::make_pair(Ty, TypeName));//Cache type name for later use
John Criswell4ff620a2004-06-01 14:54:08 +0000372 return (Out << TypeName);
Chris Lattner207b5bc2001-10-29 16:37:48 +0000373}
374
Chris Lattnere51e03b2001-10-31 04:33:19 +0000375
Misha Brukman9d0802e2004-03-01 19:48:13 +0000376/// WriteTypeSymbolic - This attempts to write the specified type as a symbolic
377/// type, iff there is an entry in the modules symbol table for the specified
378/// type or one of it's component types. This is slower than a simple x << Type
379///
Chris Lattner31f84992003-11-21 20:23:48 +0000380std::ostream &llvm::WriteTypeSymbolic(std::ostream &Out, const Type *Ty,
381 const Module *M) {
Misha Brukman40c732c2004-06-04 21:11:51 +0000382 Out << ' ';
Chris Lattner207b5bc2001-10-29 16:37:48 +0000383
384 // If they want us to print out a type, attempt to make it symbolic if there
385 // is a symbol table in the module...
Chris Lattner6e6026b2002-11-20 18:36:02 +0000386 if (M) {
Chris Lattner7b13f562003-05-08 02:08:14 +0000387 std::map<const Type *, std::string> TypeNames;
Chris Lattner207b5bc2001-10-29 16:37:48 +0000388 fillTypeNameTable(M, TypeNames);
389
Chris Lattner7b8660d2001-10-29 16:40:32 +0000390 return printTypeInt(Out, Ty, TypeNames);
Chris Lattner207b5bc2001-10-29 16:37:48 +0000391 } else {
Chris Lattner7b8660d2001-10-29 16:40:32 +0000392 return Out << Ty->getDescription();
Chris Lattner207b5bc2001-10-29 16:37:48 +0000393 }
394}
395
Reid Spencer79703962004-07-17 23:47:01 +0000396/// @brief Internal constant writer.
Chris Lattner7b13f562003-05-08 02:08:14 +0000397static void WriteConstantInt(std::ostream &Out, const Constant *CV,
398 bool PrintName,
399 std::map<const Type *, std::string> &TypeTable,
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000400 SlotMachine *Machine) {
Chris Lattner66e810b2002-04-18 18:53:13 +0000401 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
402 Out << (CB == ConstantBool::True ? "true" : "false");
403 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV)) {
404 Out << CI->getValue();
405 } else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV)) {
406 Out << CI->getValue();
407 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
408 // We would like to output the FP constant value in exponential notation,
409 // but we cannot do this if doing so will lose precision. Check here to
410 // make sure that we only output it in exponential format if we can parse
411 // the value back and get the same value.
412 //
413 std::string StrVal = ftostr(CFP->getValue());
414
415 // Check to make sure that the stringized number is not some string like
416 // "Inf" or NaN, that atof will accept, but the lexer will not. Check that
417 // the string matches the "[-+]?[0-9]" regex.
418 //
419 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
420 ((StrVal[0] == '-' || StrVal[0] == '+') &&
Brian Gaekeb471a232003-06-17 23:55:35 +0000421 (StrVal[1] >= '0' && StrVal[1] <= '9')))
Chris Lattner66e810b2002-04-18 18:53:13 +0000422 // Reparse stringized version!
423 if (atof(StrVal.c_str()) == CFP->getValue()) {
Chris Lattner71d94d12005-01-04 01:56:57 +0000424 Out << StrVal;
425 return;
Chris Lattner66e810b2002-04-18 18:53:13 +0000426 }
427
428 // Otherwise we could not reparse it to exactly the same value, so we must
429 // output the string in hexadecimal format!
430 //
431 // Behave nicely in the face of C TBAA rules... see:
432 // http://www.nullstone.com/htmls/category/aliastyp.htm
433 //
Chris Lattner71d94d12005-01-04 01:56:57 +0000434 union {
435 double D;
436 uint64_t U;
437 } V;
438 V.D = CFP->getValue();
439 assert(sizeof(double) == sizeof(uint64_t) &&
Chris Lattner66e810b2002-04-18 18:53:13 +0000440 "assuming that double is 64 bits!");
Chris Lattner71d94d12005-01-04 01:56:57 +0000441 Out << "0x" << utohexstr(V.U);
Chris Lattner66e810b2002-04-18 18:53:13 +0000442
Chris Lattnerde512b52004-02-15 05:55:15 +0000443 } else if (isa<ConstantAggregateZero>(CV)) {
444 Out << "zeroinitializer";
Chris Lattner66e810b2002-04-18 18:53:13 +0000445 } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
446 // As a special case, print the array as a string if it is an array of
447 // ubytes or an array of sbytes with positive values.
448 //
449 const Type *ETy = CA->getType()->getElementType();
450 bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
451
452 if (ETy == Type::SByteTy)
453 for (unsigned i = 0; i < CA->getNumOperands(); ++i)
454 if (cast<ConstantSInt>(CA->getOperand(i))->getValue() < 0) {
455 isString = false;
456 break;
457 }
458
459 if (isString) {
460 Out << "c\"";
461 for (unsigned i = 0; i < CA->getNumOperands(); ++i) {
Chris Lattner54e3e8f2004-06-04 23:53:20 +0000462 unsigned char C =
463 (unsigned char)cast<ConstantInt>(CA->getOperand(i))->getRawValue();
Chris Lattner66e810b2002-04-18 18:53:13 +0000464
Chris Lattnerfc944462002-07-31 23:56:44 +0000465 if (isprint(C) && C != '"' && C != '\\') {
Chris Lattner66e810b2002-04-18 18:53:13 +0000466 Out << C;
467 } else {
468 Out << '\\'
469 << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'))
470 << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
471 }
472 }
473 Out << "\"";
474
475 } else { // Cannot output in string format...
Misha Brukman40c732c2004-06-04 21:11:51 +0000476 Out << '[';
Chris Lattner7a716ad2002-04-16 21:36:08 +0000477 if (CA->getNumOperands()) {
Misha Brukman40c732c2004-06-04 21:11:51 +0000478 Out << ' ';
Chris Lattner66e810b2002-04-18 18:53:13 +0000479 printTypeInt(Out, ETy, TypeTable);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000480 WriteAsOperandInternal(Out, CA->getOperand(0),
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000481 PrintName, TypeTable, Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000482 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
483 Out << ", ";
Chris Lattner66e810b2002-04-18 18:53:13 +0000484 printTypeInt(Out, ETy, TypeTable);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000485 WriteAsOperandInternal(Out, CA->getOperand(i), PrintName,
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000486 TypeTable, Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000487 }
488 }
489 Out << " ]";
490 }
491 } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Misha Brukman40c732c2004-06-04 21:11:51 +0000492 Out << '{';
Chris Lattner7a716ad2002-04-16 21:36:08 +0000493 if (CS->getNumOperands()) {
Misha Brukman40c732c2004-06-04 21:11:51 +0000494 Out << ' ';
Chris Lattner7a716ad2002-04-16 21:36:08 +0000495 printTypeInt(Out, CS->getOperand(0)->getType(), TypeTable);
496
497 WriteAsOperandInternal(Out, CS->getOperand(0),
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000498 PrintName, TypeTable, Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000499
500 for (unsigned i = 1; i < CS->getNumOperands(); i++) {
501 Out << ", ";
502 printTypeInt(Out, CS->getOperand(i)->getType(), TypeTable);
503
504 WriteAsOperandInternal(Out, CS->getOperand(i),
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000505 PrintName, TypeTable, Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000506 }
507 }
508
509 Out << " }";
Brian Gaeke715c90b2004-08-20 06:00:58 +0000510 } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(CV)) {
511 const Type *ETy = CP->getType()->getElementType();
512 assert(CP->getNumOperands() > 0 &&
513 "Number of operands for a PackedConst must be > 0");
514 Out << '<';
515 Out << ' ';
516 printTypeInt(Out, ETy, TypeTable);
517 WriteAsOperandInternal(Out, CP->getOperand(0),
518 PrintName, TypeTable, Machine);
519 for (unsigned i = 1, e = CP->getNumOperands(); i != e; ++i) {
520 Out << ", ";
521 printTypeInt(Out, ETy, TypeTable);
522 WriteAsOperandInternal(Out, CP->getOperand(i), PrintName,
523 TypeTable, Machine);
524 }
525 Out << " >";
Chris Lattner7a716ad2002-04-16 21:36:08 +0000526 } else if (isa<ConstantPointerNull>(CV)) {
527 Out << "null";
528
Chris Lattnerb976e662004-10-16 18:08:06 +0000529 } else if (isa<UndefValue>(CV)) {
530 Out << "undef";
531
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000532 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Chris Lattner38d87732003-03-06 23:23:32 +0000533 Out << CE->getOpcodeName() << " (";
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000534
535 for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
536 printTypeInt(Out, (*OI)->getType(), TypeTable);
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000537 WriteAsOperandInternal(Out, *OI, PrintName, TypeTable, Machine);
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000538 if (OI+1 != CE->op_end())
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000539 Out << ", ";
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000540 }
541
Chris Lattner1c93e5b2002-08-16 21:17:11 +0000542 if (CE->getOpcode() == Instruction::Cast) {
Chris Lattner95586b82002-08-15 19:37:43 +0000543 Out << " to ";
544 printTypeInt(Out, CE->getType(), TypeTable);
545 }
Misha Brukman40c732c2004-06-04 21:11:51 +0000546 Out << ')';
Chris Lattner95586b82002-08-15 19:37:43 +0000547
Chris Lattner7a716ad2002-04-16 21:36:08 +0000548 } else {
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000549 Out << "<placeholder or erroneous Constant>";
Chris Lattner7a716ad2002-04-16 21:36:08 +0000550 }
551}
552
553
Misha Brukmanab5c6002004-03-02 00:22:19 +0000554/// WriteAsOperand - Write the name of the specified value out to the specified
555/// ostream. This can be useful when you just want to print int %reg126, not
556/// the whole instruction that generated it.
557///
Chris Lattner7b13f562003-05-08 02:08:14 +0000558static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
559 bool PrintName,
560 std::map<const Type*, std::string> &TypeTable,
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000561 SlotMachine *Machine) {
Misha Brukman40c732c2004-06-04 21:11:51 +0000562 Out << ' ';
Reid Spencer79703962004-07-17 23:47:01 +0000563 if ((PrintName || isa<GlobalValue>(V)) && V->hasName())
Chris Lattner24b8a5d2003-08-22 05:40:38 +0000564 Out << getLLVMName(V->getName());
Reid Spencer79703962004-07-17 23:47:01 +0000565 else {
566 const Constant *CV = dyn_cast<Constant>(V);
567 if (CV && !isa<GlobalValue>(CV))
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000568 WriteConstantInt(Out, CV, PrintName, TypeTable, Machine);
Reid Spencer79703962004-07-17 23:47:01 +0000569 else {
Chris Lattner7a716ad2002-04-16 21:36:08 +0000570 int Slot;
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000571 if (Machine) {
Reid Spencerb03de0c2004-05-26 21:56:09 +0000572 Slot = Machine->getSlot(V);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000573 } else {
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000574 Machine = createSlotMachine(V);
Chris Lattner69566452004-06-09 19:41:19 +0000575 if (Machine == 0)
576 Slot = Machine->getSlot(V);
577 else
578 Slot = -1;
Reid Spencerb03de0c2004-05-26 21:56:09 +0000579 delete Machine;
Chris Lattner7a716ad2002-04-16 21:36:08 +0000580 }
Chris Lattner69566452004-06-09 19:41:19 +0000581 if (Slot != -1)
582 Out << '%' << Slot;
583 else
584 Out << "<badref>";
Chris Lattner7a716ad2002-04-16 21:36:08 +0000585 }
586 }
587}
588
Misha Brukman9d0802e2004-03-01 19:48:13 +0000589/// WriteAsOperand - Write the name of the specified value out to the specified
590/// ostream. This can be useful when you just want to print int %reg126, not
591/// the whole instruction that generated it.
592///
Chris Lattner31f84992003-11-21 20:23:48 +0000593std::ostream &llvm::WriteAsOperand(std::ostream &Out, const Value *V,
Misha Brukman9d0802e2004-03-01 19:48:13 +0000594 bool PrintType, bool PrintName,
595 const Module *Context) {
Chris Lattner7b13f562003-05-08 02:08:14 +0000596 std::map<const Type *, std::string> TypeNames;
Chris Lattner607dc682002-07-10 16:48:17 +0000597 if (Context == 0) Context = getModuleFromVal(V);
Chris Lattner207b5bc2001-10-29 16:37:48 +0000598
Chris Lattner6e6026b2002-11-20 18:36:02 +0000599 if (Context)
Chris Lattner607dc682002-07-10 16:48:17 +0000600 fillTypeNameTable(Context, TypeNames);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000601
602 if (PrintType)
603 printTypeInt(Out, V->getType(), TypeNames);
604
Chris Lattner607dc682002-07-10 16:48:17 +0000605 WriteAsOperandInternal(Out, V, PrintName, TypeNames, 0);
Chris Lattner622f7402001-07-20 19:15:21 +0000606 return Out;
607}
608
Reid Spencer0e25e1c2004-07-04 11:50:43 +0000609/// WriteAsOperandInternal - Write the name of the specified value out to
610/// the specified ostream. This can be useful when you just want to print
611/// int %reg126, not the whole instruction that generated it.
612///
613static void WriteAsOperandInternal(std::ostream &Out, const Type *T,
614 bool PrintName,
615 std::map<const Type*, std::string> &TypeTable,
616 SlotMachine *Machine) {
617 Out << ' ';
618 int Slot;
619 if (Machine) {
620 Slot = Machine->getSlot(T);
621 if (Slot != -1)
622 Out << '%' << Slot;
623 else
624 Out << "<badref>";
625 } else {
626 Out << T->getDescription();
627 }
628}
629
630/// WriteAsOperand - Write the name of the specified value out to the specified
631/// ostream. This can be useful when you just want to print int %reg126, not
632/// the whole instruction that generated it.
633///
634std::ostream &llvm::WriteAsOperand(std::ostream &Out, const Type *Ty,
635 bool PrintType, bool PrintName,
636 const Module *Context) {
637 std::map<const Type *, std::string> TypeNames;
638 assert(Context != 0 && "Can't write types as operand without module context");
639
640 fillTypeNameTable(Context, TypeNames);
641
642 // if (PrintType)
643 // printTypeInt(Out, V->getType(), TypeNames);
644
645 printTypeInt(Out, Ty, TypeNames);
646
647 WriteAsOperandInternal(Out, Ty, PrintName, TypeNames, 0);
648 return Out;
649}
650
Chris Lattner31f84992003-11-21 20:23:48 +0000651namespace llvm {
Chris Lattnerd8c2e422001-07-12 23:35:26 +0000652
Chris Lattner007377f2001-09-07 16:36:04 +0000653class AssemblyWriter {
Misha Brukman0313e0b2004-06-21 21:53:56 +0000654 std::ostream &Out;
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000655 SlotMachine &Machine;
Chris Lattnerc1824992001-10-29 16:05:51 +0000656 const Module *TheModule;
Chris Lattner7b13f562003-05-08 02:08:14 +0000657 std::map<const Type *, std::string> TypeNames;
Chris Lattner95e5a2c2003-10-30 23:41:03 +0000658 AssemblyAnnotationWriter *AnnotationWriter;
Chris Lattner00950542001-06-06 20:29:01 +0000659public:
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000660 inline AssemblyWriter(std::ostream &o, SlotMachine &Mac, const Module *M,
Chris Lattner95e5a2c2003-10-30 23:41:03 +0000661 AssemblyAnnotationWriter *AAW)
Misha Brukman0313e0b2004-06-21 21:53:56 +0000662 : Out(o), Machine(Mac), TheModule(M), AnnotationWriter(AAW) {
Chris Lattnerc1824992001-10-29 16:05:51 +0000663
664 // If the module has a symbol table, take all global types and stuff their
665 // names into the TypeNames map.
666 //
Chris Lattner207b5bc2001-10-29 16:37:48 +0000667 fillTypeNameTable(M, TypeNames);
Chris Lattner00950542001-06-06 20:29:01 +0000668 }
669
Chris Lattnerc1824992001-10-29 16:05:51 +0000670 inline void write(const Module *M) { printModule(M); }
671 inline void write(const GlobalVariable *G) { printGlobal(G); }
Chris Lattner79df7c02002-03-26 18:01:55 +0000672 inline void write(const Function *F) { printFunction(F); }
Chris Lattnerc1824992001-10-29 16:05:51 +0000673 inline void write(const BasicBlock *BB) { printBasicBlock(BB); }
Chris Lattner7e708292002-06-25 16:13:24 +0000674 inline void write(const Instruction *I) { printInstruction(*I); }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000675 inline void write(const Constant *CPV) { printConstant(CPV); }
Chris Lattnerda1fbcc2001-11-07 04:21:57 +0000676 inline void write(const Type *Ty) { printType(Ty); }
Chris Lattner00950542001-06-06 20:29:01 +0000677
Chris Lattner66e810b2002-04-18 18:53:13 +0000678 void writeOperand(const Value *Op, bool PrintType, bool PrintName = true);
679
Misha Brukman5cf1acf2004-04-28 15:31:21 +0000680 const Module* getModule() { return TheModule; }
681
Misha Brukmanf771bea2004-11-15 19:30:05 +0000682private:
Chris Lattnerc1824992001-10-29 16:05:51 +0000683 void printModule(const Module *M);
684 void printSymbolTable(const SymbolTable &ST);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000685 void printConstant(const Constant *CPV);
Chris Lattnerc1824992001-10-29 16:05:51 +0000686 void printGlobal(const GlobalVariable *GV);
Chris Lattner79df7c02002-03-26 18:01:55 +0000687 void printFunction(const Function *F);
Chris Lattner73e21422002-04-09 19:48:49 +0000688 void printArgument(const Argument *FA);
Chris Lattnerc1824992001-10-29 16:05:51 +0000689 void printBasicBlock(const BasicBlock *BB);
Chris Lattner7e708292002-06-25 16:13:24 +0000690 void printInstruction(const Instruction &I);
Chris Lattner2761e9f2002-04-13 20:53:41 +0000691
692 // printType - Go to extreme measures to attempt to print out a short,
693 // symbolic version of a type name.
694 //
Chris Lattner7b13f562003-05-08 02:08:14 +0000695 std::ostream &printType(const Type *Ty) {
Misha Brukman0313e0b2004-06-21 21:53:56 +0000696 return printTypeInt(Out, Ty, TypeNames);
Chris Lattner2761e9f2002-04-13 20:53:41 +0000697 }
698
699 // printTypeAtLeastOneLevel - Print out one level of the possibly complex type
700 // without considering any symbolic types that we may have equal to it.
701 //
Chris Lattner7b13f562003-05-08 02:08:14 +0000702 std::ostream &printTypeAtLeastOneLevel(const Type *Ty);
Chris Lattnerc1824992001-10-29 16:05:51 +0000703
Chris Lattnere02fa852001-10-13 06:42:36 +0000704 // printInfoComment - Print a little comment after the instruction indicating
705 // which slot it occupies.
Chris Lattner7e708292002-06-25 16:13:24 +0000706 void printInfoComment(const Value &V);
Chris Lattner00950542001-06-06 20:29:01 +0000707};
Reid Spencer73b74952004-05-27 22:04:46 +0000708} // end of llvm namespace
Chris Lattner00950542001-06-06 20:29:01 +0000709
Misha Brukmanab5c6002004-03-02 00:22:19 +0000710/// printTypeAtLeastOneLevel - Print out one level of the possibly complex type
711/// without considering any symbolic types that we may have equal to it.
712///
Chris Lattner7b13f562003-05-08 02:08:14 +0000713std::ostream &AssemblyWriter::printTypeAtLeastOneLevel(const Type *Ty) {
Chris Lattner7e708292002-06-25 16:13:24 +0000714 if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
Chris Lattner2761e9f2002-04-13 20:53:41 +0000715 printType(FTy->getReturnType()) << " (";
Chris Lattnerd5d89962004-02-09 04:14:01 +0000716 for (FunctionType::param_iterator I = FTy->param_begin(),
717 E = FTy->param_end(); I != E; ++I) {
718 if (I != FTy->param_begin())
Misha Brukman0313e0b2004-06-21 21:53:56 +0000719 Out << ", ";
Chris Lattner7a716ad2002-04-16 21:36:08 +0000720 printType(*I);
Chris Lattner2761e9f2002-04-13 20:53:41 +0000721 }
722 if (FTy->isVarArg()) {
Misha Brukman0313e0b2004-06-21 21:53:56 +0000723 if (FTy->getNumParams()) Out << ", ";
724 Out << "...";
Chris Lattner2761e9f2002-04-13 20:53:41 +0000725 }
Misha Brukman0313e0b2004-06-21 21:53:56 +0000726 Out << ')';
Chris Lattner7e708292002-06-25 16:13:24 +0000727 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
Misha Brukman0313e0b2004-06-21 21:53:56 +0000728 Out << "{ ";
Chris Lattnerd21cd802004-02-09 04:37:31 +0000729 for (StructType::element_iterator I = STy->element_begin(),
730 E = STy->element_end(); I != E; ++I) {
731 if (I != STy->element_begin())
Misha Brukman0313e0b2004-06-21 21:53:56 +0000732 Out << ", ";
Chris Lattner2761e9f2002-04-13 20:53:41 +0000733 printType(*I);
734 }
Misha Brukman0313e0b2004-06-21 21:53:56 +0000735 Out << " }";
Chris Lattner7e708292002-06-25 16:13:24 +0000736 } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
Misha Brukman40c732c2004-06-04 21:11:51 +0000737 printType(PTy->getElementType()) << '*';
Chris Lattner7e708292002-06-25 16:13:24 +0000738 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Misha Brukman0313e0b2004-06-21 21:53:56 +0000739 Out << '[' << ATy->getNumElements() << " x ";
Misha Brukman40c732c2004-06-04 21:11:51 +0000740 printType(ATy->getElementType()) << ']';
Reid Spencer5527c0b2004-08-20 15:37:30 +0000741 } else if (const PackedType *PTy = dyn_cast<PackedType>(Ty)) {
742 Out << '<' << PTy->getNumElements() << " x ";
743 printType(PTy->getElementType()) << '>';
744 }
745 else if (const OpaqueType *OTy = dyn_cast<OpaqueType>(Ty)) {
Misha Brukman0313e0b2004-06-21 21:53:56 +0000746 Out << "opaque";
Chris Lattner2761e9f2002-04-13 20:53:41 +0000747 } else {
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000748 if (!Ty->isPrimitiveType())
Misha Brukman0313e0b2004-06-21 21:53:56 +0000749 Out << "<unknown derived type>";
Chris Lattner2761e9f2002-04-13 20:53:41 +0000750 printType(Ty);
751 }
Misha Brukman0313e0b2004-06-21 21:53:56 +0000752 return Out;
Chris Lattner2761e9f2002-04-13 20:53:41 +0000753}
754
755
Chris Lattner007377f2001-09-07 16:36:04 +0000756void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType,
Reid Spencerb03de0c2004-05-26 21:56:09 +0000757 bool PrintName) {
Chris Lattneraab18202005-02-24 16:58:29 +0000758 if (Operand != 0) {
759 if (PrintType) { Out << ' '; printType(Operand->getType()); }
760 WriteAsOperandInternal(Out, Operand, PrintName, TypeNames, &Machine);
761 } else {
762 Out << "<null operand!>";
763 }
Chris Lattner00950542001-06-06 20:29:01 +0000764}
765
Chris Lattner00950542001-06-06 20:29:01 +0000766
Chris Lattnerc1824992001-10-29 16:05:51 +0000767void AssemblyWriter::printModule(const Module *M) {
Chris Lattner31ab1b32005-03-02 23:12:40 +0000768 if (!M->getModuleIdentifier().empty() &&
Misha Brukman0fbd9142005-03-02 23:17:31 +0000769 // Don't print the ID if it will start a new line (which would
Chris Lattner31ab1b32005-03-02 23:12:40 +0000770 // require a comment char before it).
771 M->getModuleIdentifier().find('\n') == std::string::npos)
772 Out << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
773
Chris Lattnereb5d3a12003-08-24 13:48:48 +0000774 switch (M->getEndianness()) {
Misha Brukman0313e0b2004-06-21 21:53:56 +0000775 case Module::LittleEndian: Out << "target endian = little\n"; break;
776 case Module::BigEndian: Out << "target endian = big\n"; break;
Chris Lattnereb5d3a12003-08-24 13:48:48 +0000777 case Module::AnyEndianness: break;
778 }
779 switch (M->getPointerSize()) {
Misha Brukman0313e0b2004-06-21 21:53:56 +0000780 case Module::Pointer32: Out << "target pointersize = 32\n"; break;
781 case Module::Pointer64: Out << "target pointersize = 64\n"; break;
Chris Lattnereb5d3a12003-08-24 13:48:48 +0000782 case Module::AnyPointerSize: break;
783 }
Reid Spencercddc86f2004-07-25 21:44:54 +0000784 if (!M->getTargetTriple().empty())
Reid Spencerc9a1f0d2004-07-25 21:29:43 +0000785 Out << "target triple = \"" << M->getTargetTriple() << "\"\n";
Reid Spencer83f6a772004-07-25 18:08:18 +0000786
Chris Lattner44da7d72004-09-14 05:06:58 +0000787 // Loop over the dependent libraries and emit them.
Chris Lattnercfe97b72004-09-14 04:51:44 +0000788 Module::lib_iterator LI = M->lib_begin();
789 Module::lib_iterator LE = M->lib_end();
Reid Spencercddc86f2004-07-25 21:44:54 +0000790 if (LI != LE) {
Chris Lattnercfe97b72004-09-14 04:51:44 +0000791 Out << "deplibs = [ ";
792 while (LI != LE) {
Chris Lattner44da7d72004-09-14 05:06:58 +0000793 Out << '"' << *LI << '"';
Reid Spencerc9a1f0d2004-07-25 21:29:43 +0000794 ++LI;
Chris Lattnercfe97b72004-09-14 04:51:44 +0000795 if (LI != LE)
796 Out << ", ";
Reid Spencerc9a1f0d2004-07-25 21:29:43 +0000797 }
798 Out << " ]\n";
Reid Spencer83f6a772004-07-25 18:08:18 +0000799 }
Reid Spencere59eaf42004-09-13 23:44:23 +0000800
Chris Lattner44da7d72004-09-14 05:06:58 +0000801 // Loop over the symbol table, emitting all named constants.
Chris Lattner6e6026b2002-11-20 18:36:02 +0000802 printSymbolTable(M->getSymbolTable());
Chris Lattner70cc3392001-09-10 07:58:01 +0000803
Chris Lattnere4d5c442005-03-15 04:54:21 +0000804 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end(); I != E; ++I)
Chris Lattner7e708292002-06-25 16:13:24 +0000805 printGlobal(I);
Chris Lattner007377f2001-09-07 16:36:04 +0000806
Misha Brukman0313e0b2004-06-21 21:53:56 +0000807 Out << "\nimplementation ; Functions:\n";
Vikram S. Adve5efa3cc2001-09-18 12:48:16 +0000808
Chris Lattner44da7d72004-09-14 05:06:58 +0000809 // Output all of the functions.
Chris Lattner7e708292002-06-25 16:13:24 +0000810 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
811 printFunction(I);
Chris Lattner007377f2001-09-07 16:36:04 +0000812}
813
Chris Lattnerc1824992001-10-29 16:05:51 +0000814void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Misha Brukman0313e0b2004-06-21 21:53:56 +0000815 if (GV->hasName()) Out << getLLVMName(GV->getName()) << " = ";
Chris Lattnerd70684f2001-09-18 04:01:05 +0000816
Chris Lattner4ad02e72003-04-16 20:28:45 +0000817 if (!GV->hasInitializer())
Misha Brukman0313e0b2004-06-21 21:53:56 +0000818 Out << "external ";
Chris Lattner4ad02e72003-04-16 20:28:45 +0000819 else
820 switch (GV->getLinkage()) {
Misha Brukman0313e0b2004-06-21 21:53:56 +0000821 case GlobalValue::InternalLinkage: Out << "internal "; break;
822 case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
823 case GlobalValue::WeakLinkage: Out << "weak "; break;
824 case GlobalValue::AppendingLinkage: Out << "appending "; break;
Chris Lattner4ad02e72003-04-16 20:28:45 +0000825 case GlobalValue::ExternalLinkage: break;
Misha Brukman3c8f2c62004-11-14 21:04:34 +0000826 case GlobalValue::GhostLinkage:
827 std::cerr << "GhostLinkage not allowed in AsmWriter!\n";
828 abort();
Chris Lattner4ad02e72003-04-16 20:28:45 +0000829 }
Chris Lattnerd70684f2001-09-18 04:01:05 +0000830
Misha Brukman0313e0b2004-06-21 21:53:56 +0000831 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattner7a176752001-12-04 00:03:30 +0000832 printType(GV->getType()->getElementType());
Chris Lattnerd70684f2001-09-18 04:01:05 +0000833
Reid Spencer79703962004-07-17 23:47:01 +0000834 if (GV->hasInitializer()) {
835 Constant* C = cast<Constant>(GV->getInitializer());
836 assert(C && "GlobalVar initializer isn't constant?");
Reid Spenceracc92802004-07-18 01:04:19 +0000837 writeOperand(GV->getInitializer(), false, isa<GlobalValue>(C));
Reid Spencer79703962004-07-17 23:47:01 +0000838 }
Chris Lattnerd70684f2001-09-18 04:01:05 +0000839
Chris Lattner7e708292002-06-25 16:13:24 +0000840 printInfoComment(*GV);
Misha Brukman0313e0b2004-06-21 21:53:56 +0000841 Out << "\n";
Chris Lattner70cc3392001-09-10 07:58:01 +0000842}
843
Chris Lattner007377f2001-09-07 16:36:04 +0000844
Reid Spencer9231ac82004-05-25 08:53:40 +0000845// printSymbolTable - Run through symbol table looking for constants
846// and types. Emit their declarations.
Chris Lattnerc1824992001-10-29 16:05:51 +0000847void AssemblyWriter::printSymbolTable(const SymbolTable &ST) {
Reid Spencer9231ac82004-05-25 08:53:40 +0000848
849 // Print the types.
850 for (SymbolTable::type_const_iterator TI = ST.type_begin();
851 TI != ST.type_end(); ++TI ) {
Misha Brukman0313e0b2004-06-21 21:53:56 +0000852 Out << "\t" << getLLVMName(TI->first) << " = type ";
Reid Spencer9231ac82004-05-25 08:53:40 +0000853
854 // Make sure we print out at least one level of the type structure, so
855 // that we do not get %FILE = type %FILE
856 //
857 printTypeAtLeastOneLevel(TI->second) << "\n";
858 }
Chris Lattner007377f2001-09-07 16:36:04 +0000859
Reid Spencer9231ac82004-05-25 08:53:40 +0000860 // Print the constants, in type plane order.
861 for (SymbolTable::plane_const_iterator PI = ST.plane_begin();
862 PI != ST.plane_end(); ++PI ) {
863 SymbolTable::value_const_iterator VI = ST.value_begin(PI->first);
864 SymbolTable::value_const_iterator VE = ST.value_end(PI->first);
865
866 for (; VI != VE; ++VI) {
Reid Spencer79703962004-07-17 23:47:01 +0000867 const Value* V = VI->second;
868 const Constant *CPV = dyn_cast<Constant>(V) ;
869 if (CPV && !isa<GlobalValue>(V)) {
Reid Spencerb03de0c2004-05-26 21:56:09 +0000870 printConstant(CPV);
Chris Lattner007377f2001-09-07 16:36:04 +0000871 }
872 }
Chris Lattner739a56d2001-07-15 06:35:59 +0000873 }
Chris Lattner00950542001-06-06 20:29:01 +0000874}
875
876
Misha Brukmanab5c6002004-03-02 00:22:19 +0000877/// printConstant - Print out a constant pool entry...
878///
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000879void AssemblyWriter::printConstant(const Constant *CPV) {
Chris Lattner007377f2001-09-07 16:36:04 +0000880 // Don't print out unnamed constants, they will be inlined
881 if (!CPV->hasName()) return;
Chris Lattner00950542001-06-06 20:29:01 +0000882
Chris Lattner1333ed52001-07-26 16:29:38 +0000883 // Print out name...
Misha Brukman0313e0b2004-06-21 21:53:56 +0000884 Out << "\t" << getLLVMName(CPV->getName()) << " =";
Chris Lattner00950542001-06-06 20:29:01 +0000885
886 // Write the value out now...
Chris Lattner7a716ad2002-04-16 21:36:08 +0000887 writeOperand(CPV, true, false);
Chris Lattner00950542001-06-06 20:29:01 +0000888
Chris Lattner7e708292002-06-25 16:13:24 +0000889 printInfoComment(*CPV);
Misha Brukman0313e0b2004-06-21 21:53:56 +0000890 Out << "\n";
Chris Lattner00950542001-06-06 20:29:01 +0000891}
892
Misha Brukmanab5c6002004-03-02 00:22:19 +0000893/// printFunction - Print all aspects of a function.
894///
Chris Lattner7e708292002-06-25 16:13:24 +0000895void AssemblyWriter::printFunction(const Function *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000896 // Print out the return type and name...
Misha Brukman0313e0b2004-06-21 21:53:56 +0000897 Out << "\n";
Chris Lattner4ad02e72003-04-16 20:28:45 +0000898
Chris Lattner333529e2004-12-05 06:44:09 +0000899 // Ensure that no local symbols conflict with global symbols.
900 const_cast<Function*>(F)->renameLocalSymbols();
901
Misha Brukman0313e0b2004-06-21 21:53:56 +0000902 if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out);
Chris Lattner95e5a2c2003-10-30 23:41:03 +0000903
Chris Lattner4ad02e72003-04-16 20:28:45 +0000904 if (F->isExternal())
Misha Brukman0313e0b2004-06-21 21:53:56 +0000905 Out << "declare ";
Chris Lattner4ad02e72003-04-16 20:28:45 +0000906 else
907 switch (F->getLinkage()) {
Misha Brukman0313e0b2004-06-21 21:53:56 +0000908 case GlobalValue::InternalLinkage: Out << "internal "; break;
909 case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
910 case GlobalValue::WeakLinkage: Out << "weak "; break;
911 case GlobalValue::AppendingLinkage: Out << "appending "; break;
Chris Lattner4ad02e72003-04-16 20:28:45 +0000912 case GlobalValue::ExternalLinkage: break;
Misha Brukman3c8f2c62004-11-14 21:04:34 +0000913 case GlobalValue::GhostLinkage:
914 std::cerr << "GhostLinkage not allowed in AsmWriter!\n";
915 abort();
Chris Lattner4ad02e72003-04-16 20:28:45 +0000916 }
917
Misha Brukman40c732c2004-06-04 21:11:51 +0000918 printType(F->getReturnType()) << ' ';
Chris Lattner4d45bd02003-10-18 05:57:43 +0000919 if (!F->getName().empty())
Misha Brukman0313e0b2004-06-21 21:53:56 +0000920 Out << getLLVMName(F->getName());
Chris Lattner4d45bd02003-10-18 05:57:43 +0000921 else
Misha Brukman0313e0b2004-06-21 21:53:56 +0000922 Out << "\"\"";
923 Out << '(';
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000924 Machine.incorporateFunction(F);
Chris Lattner007377f2001-09-07 16:36:04 +0000925
Chris Lattnerc1824992001-10-29 16:05:51 +0000926 // Loop over the arguments, printing them...
Chris Lattner7e708292002-06-25 16:13:24 +0000927 const FunctionType *FT = F->getFunctionType();
Chris Lattner007377f2001-09-07 16:36:04 +0000928
Chris Lattnere4d5c442005-03-15 04:54:21 +0000929 for(Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I)
Chris Lattner69da5cf2002-10-13 20:57:00 +0000930 printArgument(I);
Chris Lattner007377f2001-09-07 16:36:04 +0000931
932 // Finish printing arguments...
Chris Lattner7e708292002-06-25 16:13:24 +0000933 if (FT->isVarArg()) {
Misha Brukman0313e0b2004-06-21 21:53:56 +0000934 if (FT->getNumParams()) Out << ", ";
935 Out << "..."; // Output varargs portion of signature!
Chris Lattner007377f2001-09-07 16:36:04 +0000936 }
Misha Brukman0313e0b2004-06-21 21:53:56 +0000937 Out << ')';
Chris Lattner007377f2001-09-07 16:36:04 +0000938
Chris Lattner7e708292002-06-25 16:13:24 +0000939 if (F->isExternal()) {
Misha Brukman0313e0b2004-06-21 21:53:56 +0000940 Out << "\n";
Chris Lattner03e2acb2002-05-06 03:00:40 +0000941 } else {
Misha Brukman0313e0b2004-06-21 21:53:56 +0000942 Out << " {";
Chris Lattner007377f2001-09-07 16:36:04 +0000943
Chris Lattnerb5794002002-04-07 22:49:37 +0000944 // Output all of its basic blocks... for the function
Chris Lattner7e708292002-06-25 16:13:24 +0000945 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
946 printBasicBlock(I);
Chris Lattner007377f2001-09-07 16:36:04 +0000947
Misha Brukman0313e0b2004-06-21 21:53:56 +0000948 Out << "}\n";
Chris Lattner007377f2001-09-07 16:36:04 +0000949 }
950
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000951 Machine.purgeFunction();
Chris Lattner00950542001-06-06 20:29:01 +0000952}
953
Misha Brukmanab5c6002004-03-02 00:22:19 +0000954/// printArgument - This member is called for every argument that is passed into
955/// the function. Simply print it out
956///
Chris Lattner73e21422002-04-09 19:48:49 +0000957void AssemblyWriter::printArgument(const Argument *Arg) {
Chris Lattner00950542001-06-06 20:29:01 +0000958 // Insert commas as we go... the first arg doesn't get a comma
Chris Lattnere4d5c442005-03-15 04:54:21 +0000959 if (Arg != &Arg->getParent()->arg_front()) Out << ", ";
Chris Lattner00950542001-06-06 20:29:01 +0000960
961 // Output type...
Chris Lattnerc1824992001-10-29 16:05:51 +0000962 printType(Arg->getType());
Chris Lattner00950542001-06-06 20:29:01 +0000963
964 // Output name, if available...
965 if (Arg->hasName())
Misha Brukman0313e0b2004-06-21 21:53:56 +0000966 Out << ' ' << getLLVMName(Arg->getName());
Chris Lattner00950542001-06-06 20:29:01 +0000967}
968
Misha Brukmanab5c6002004-03-02 00:22:19 +0000969/// printBasicBlock - This member is called for each basic block in a method.
970///
Chris Lattnerc1824992001-10-29 16:05:51 +0000971void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Chris Lattner00950542001-06-06 20:29:01 +0000972 if (BB->hasName()) { // Print out the label if it exists...
Alkis Evlogimenos9913e592004-12-10 05:41:10 +0000973 Out << "\n" << getLLVMName(BB->getName(), false) << ':';
Chris Lattnerafc38682002-05-14 16:02:05 +0000974 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Misha Brukman0313e0b2004-06-21 21:53:56 +0000975 Out << "\n; <label>:";
Chris Lattner69566452004-06-09 19:41:19 +0000976 int Slot = Machine.getSlot(BB);
977 if (Slot != -1)
Misha Brukman0313e0b2004-06-21 21:53:56 +0000978 Out << Slot;
Chris Lattner69566452004-06-09 19:41:19 +0000979 else
Misha Brukman0313e0b2004-06-21 21:53:56 +0000980 Out << "<badref>";
Chris Lattner061269b2002-10-02 19:38:55 +0000981 }
Chris Lattner4e4d8622003-11-20 00:09:43 +0000982
983 if (BB->getParent() == 0)
Misha Brukman0313e0b2004-06-21 21:53:56 +0000984 Out << "\t\t; Error: Block without parent!";
Chris Lattner4e4d8622003-11-20 00:09:43 +0000985 else {
986 if (BB != &BB->getParent()->front()) { // Not the entry block?
987 // Output predecessors for the block...
Misha Brukman0313e0b2004-06-21 21:53:56 +0000988 Out << "\t\t;";
Chris Lattner4e4d8622003-11-20 00:09:43 +0000989 pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
990
991 if (PI == PE) {
Misha Brukman0313e0b2004-06-21 21:53:56 +0000992 Out << " No predecessors!";
Chris Lattner4e4d8622003-11-20 00:09:43 +0000993 } else {
Misha Brukman0313e0b2004-06-21 21:53:56 +0000994 Out << " preds =";
Chris Lattner40efcec2003-11-16 22:59:57 +0000995 writeOperand(*PI, false, true);
Chris Lattner4e4d8622003-11-20 00:09:43 +0000996 for (++PI; PI != PE; ++PI) {
Misha Brukman0313e0b2004-06-21 21:53:56 +0000997 Out << ',';
Chris Lattner4e4d8622003-11-20 00:09:43 +0000998 writeOperand(*PI, false, true);
999 }
Chris Lattner40efcec2003-11-16 22:59:57 +00001000 }
Chris Lattner061269b2002-10-02 19:38:55 +00001001 }
Chris Lattner00950542001-06-06 20:29:01 +00001002 }
Chris Lattnerafc38682002-05-14 16:02:05 +00001003
Misha Brukman0313e0b2004-06-21 21:53:56 +00001004 Out << "\n";
Chris Lattner00950542001-06-06 20:29:01 +00001005
Misha Brukman0313e0b2004-06-21 21:53:56 +00001006 if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out);
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001007
Chris Lattner007377f2001-09-07 16:36:04 +00001008 // Output all of the instructions in the basic block...
Chris Lattner7e708292002-06-25 16:13:24 +00001009 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
1010 printInstruction(*I);
Chris Lattner9f717ef2004-03-08 18:51:45 +00001011
Misha Brukman0313e0b2004-06-21 21:53:56 +00001012 if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out);
Chris Lattner00950542001-06-06 20:29:01 +00001013}
1014
Chris Lattnere02fa852001-10-13 06:42:36 +00001015
Misha Brukmanab5c6002004-03-02 00:22:19 +00001016/// printInfoComment - Print a little comment after the instruction indicating
1017/// which slot it occupies.
1018///
Chris Lattner7e708292002-06-25 16:13:24 +00001019void AssemblyWriter::printInfoComment(const Value &V) {
1020 if (V.getType() != Type::VoidTy) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001021 Out << "\t\t; <";
Misha Brukman40c732c2004-06-04 21:11:51 +00001022 printType(V.getType()) << '>';
Chris Lattnere02fa852001-10-13 06:42:36 +00001023
Chris Lattner7e708292002-06-25 16:13:24 +00001024 if (!V.hasName()) {
Chris Lattner69566452004-06-09 19:41:19 +00001025 int SlotNum = Machine.getSlot(&V);
1026 if (SlotNum == -1)
Misha Brukman0313e0b2004-06-21 21:53:56 +00001027 Out << ":<badref>";
Reid Spencerfc621e22004-06-09 15:26:53 +00001028 else
Misha Brukman0313e0b2004-06-21 21:53:56 +00001029 Out << ':' << SlotNum; // Print out the def slot taken.
Chris Lattnere02fa852001-10-13 06:42:36 +00001030 }
Chris Lattner5c461402005-02-01 01:24:01 +00001031 Out << " [#uses=" << V.getNumUses() << ']'; // Output # uses
Chris Lattnere02fa852001-10-13 06:42:36 +00001032 }
1033}
1034
Reid Spencerfc621e22004-06-09 15:26:53 +00001035/// printInstruction - This member is called for each Instruction in a function..
Misha Brukmanab5c6002004-03-02 00:22:19 +00001036///
Chris Lattner7e708292002-06-25 16:13:24 +00001037void AssemblyWriter::printInstruction(const Instruction &I) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001038 if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out);
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001039
Misha Brukman0313e0b2004-06-21 21:53:56 +00001040 Out << "\t";
Chris Lattner00950542001-06-06 20:29:01 +00001041
1042 // Print out name if it exists...
Chris Lattner7e708292002-06-25 16:13:24 +00001043 if (I.hasName())
Misha Brukman0313e0b2004-06-21 21:53:56 +00001044 Out << getLLVMName(I.getName()) << " = ";
Chris Lattner00950542001-06-06 20:29:01 +00001045
Chris Lattnere5e475e2003-09-08 17:45:59 +00001046 // If this is a volatile load or store, print out the volatile marker
1047 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
1048 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile()))
Misha Brukman0313e0b2004-06-21 21:53:56 +00001049 Out << "volatile ";
Chris Lattnere5e475e2003-09-08 17:45:59 +00001050
Chris Lattner00950542001-06-06 20:29:01 +00001051 // Print out the opcode...
Misha Brukman0313e0b2004-06-21 21:53:56 +00001052 Out << I.getOpcodeName();
Chris Lattner00950542001-06-06 20:29:01 +00001053
1054 // Print out the type of the operands...
Chris Lattner7e708292002-06-25 16:13:24 +00001055 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
Chris Lattner00950542001-06-06 20:29:01 +00001056
1057 // Special case conditional branches to swizzle the condition out to the front
Chris Lattner7e708292002-06-25 16:13:24 +00001058 if (isa<BranchInst>(I) && I.getNumOperands() > 1) {
1059 writeOperand(I.getOperand(2), true);
Misha Brukman0313e0b2004-06-21 21:53:56 +00001060 Out << ',';
Chris Lattner00950542001-06-06 20:29:01 +00001061 writeOperand(Operand, true);
Misha Brukman0313e0b2004-06-21 21:53:56 +00001062 Out << ',';
Chris Lattner7e708292002-06-25 16:13:24 +00001063 writeOperand(I.getOperand(1), true);
Chris Lattner00950542001-06-06 20:29:01 +00001064
Chris Lattner94dc1f22002-04-13 18:34:38 +00001065 } else if (isa<SwitchInst>(I)) {
Chris Lattner00950542001-06-06 20:29:01 +00001066 // Special case switch statement to get formatting nice and correct...
Misha Brukman0313e0b2004-06-21 21:53:56 +00001067 writeOperand(Operand , true); Out << ',';
1068 writeOperand(I.getOperand(1), true); Out << " [";
Chris Lattner00950542001-06-06 20:29:01 +00001069
Chris Lattner7e708292002-06-25 16:13:24 +00001070 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001071 Out << "\n\t\t";
1072 writeOperand(I.getOperand(op ), true); Out << ',';
Chris Lattner7e708292002-06-25 16:13:24 +00001073 writeOperand(I.getOperand(op+1), true);
Chris Lattner00950542001-06-06 20:29:01 +00001074 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00001075 Out << "\n\t]";
Chris Lattnerb00c5822001-10-02 03:41:24 +00001076 } else if (isa<PHINode>(I)) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001077 Out << ' ';
Chris Lattner7e708292002-06-25 16:13:24 +00001078 printType(I.getType());
Misha Brukman0313e0b2004-06-21 21:53:56 +00001079 Out << ' ';
Chris Lattner00950542001-06-06 20:29:01 +00001080
Chris Lattner7e708292002-06-25 16:13:24 +00001081 for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001082 if (op) Out << ", ";
1083 Out << '[';
1084 writeOperand(I.getOperand(op ), false); Out << ',';
1085 writeOperand(I.getOperand(op+1), false); Out << " ]";
Chris Lattnerc24d2082001-06-11 15:04:20 +00001086 }
Chris Lattnere02fa852001-10-13 06:42:36 +00001087 } else if (isa<ReturnInst>(I) && !Operand) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001088 Out << " void";
Chris Lattnere02fa852001-10-13 06:42:36 +00001089 } else if (isa<CallInst>(I)) {
Chris Lattner7a012292003-08-05 15:34:45 +00001090 const PointerType *PTy = cast<PointerType>(Operand->getType());
1091 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1092 const Type *RetTy = FTy->getReturnType();
Chris Lattner268de042001-11-06 21:28:12 +00001093
Chris Lattner7a012292003-08-05 15:34:45 +00001094 // If possible, print out the short form of the call instruction. We can
Chris Lattnerb5794002002-04-07 22:49:37 +00001095 // only do this if the first argument is a pointer to a nonvararg function,
Chris Lattner7a012292003-08-05 15:34:45 +00001096 // and if the return type is not a pointer to a function.
Chris Lattner268de042001-11-06 21:28:12 +00001097 //
Chris Lattner7a012292003-08-05 15:34:45 +00001098 if (!FTy->isVarArg() &&
Chris Lattner94dc1f22002-04-13 18:34:38 +00001099 (!isa<PointerType>(RetTy) ||
Chris Lattnerc1b27182002-07-25 20:58:51 +00001100 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001101 Out << ' '; printType(RetTy);
Chris Lattner268de042001-11-06 21:28:12 +00001102 writeOperand(Operand, false);
1103 } else {
1104 writeOperand(Operand, true);
1105 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00001106 Out << '(';
Chris Lattner7e708292002-06-25 16:13:24 +00001107 if (I.getNumOperands() > 1) writeOperand(I.getOperand(1), true);
1108 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; ++op) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001109 Out << ',';
Chris Lattner7e708292002-06-25 16:13:24 +00001110 writeOperand(I.getOperand(op), true);
Chris Lattner00950542001-06-06 20:29:01 +00001111 }
1112
Misha Brukman0313e0b2004-06-21 21:53:56 +00001113 Out << " )";
Chris Lattner7e708292002-06-25 16:13:24 +00001114 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Chris Lattner7a012292003-08-05 15:34:45 +00001115 const PointerType *PTy = cast<PointerType>(Operand->getType());
1116 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1117 const Type *RetTy = FTy->getReturnType();
1118
1119 // If possible, print out the short form of the invoke instruction. We can
1120 // only do this if the first argument is a pointer to a nonvararg function,
1121 // and if the return type is not a pointer to a function.
1122 //
1123 if (!FTy->isVarArg() &&
1124 (!isa<PointerType>(RetTy) ||
1125 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001126 Out << ' '; printType(RetTy);
Chris Lattner7a012292003-08-05 15:34:45 +00001127 writeOperand(Operand, false);
1128 } else {
1129 writeOperand(Operand, true);
1130 }
1131
Misha Brukman0313e0b2004-06-21 21:53:56 +00001132 Out << '(';
Chris Lattner7e708292002-06-25 16:13:24 +00001133 if (I.getNumOperands() > 3) writeOperand(I.getOperand(3), true);
1134 for (unsigned op = 4, Eop = I.getNumOperands(); op < Eop; ++op) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001135 Out << ',';
Chris Lattner7e708292002-06-25 16:13:24 +00001136 writeOperand(I.getOperand(op), true);
Chris Lattnere02fa852001-10-13 06:42:36 +00001137 }
1138
Misha Brukman0313e0b2004-06-21 21:53:56 +00001139 Out << " )\n\t\t\tto";
Chris Lattnere02fa852001-10-13 06:42:36 +00001140 writeOperand(II->getNormalDest(), true);
Misha Brukman0313e0b2004-06-21 21:53:56 +00001141 Out << " unwind";
Chris Lattneraeb2a1d2004-02-08 21:44:31 +00001142 writeOperand(II->getUnwindDest(), true);
Chris Lattnere02fa852001-10-13 06:42:36 +00001143
Chris Lattner7e708292002-06-25 16:13:24 +00001144 } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(&I)) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001145 Out << ' ';
Chris Lattner94dc1f22002-04-13 18:34:38 +00001146 printType(AI->getType()->getElementType());
1147 if (AI->isArrayAllocation()) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001148 Out << ',';
Chris Lattner94dc1f22002-04-13 18:34:38 +00001149 writeOperand(AI->getArraySize(), true);
Chris Lattner00950542001-06-06 20:29:01 +00001150 }
Chris Lattnere02fa852001-10-13 06:42:36 +00001151 } else if (isa<CastInst>(I)) {
Chris Lattner41495a22003-11-17 01:17:04 +00001152 if (Operand) writeOperand(Operand, true); // Work with broken code
Misha Brukman0313e0b2004-06-21 21:53:56 +00001153 Out << " to ";
Chris Lattner7e708292002-06-25 16:13:24 +00001154 printType(I.getType());
Chris Lattner4d45bd02003-10-18 05:57:43 +00001155 } else if (isa<VAArgInst>(I)) {
Chris Lattner41495a22003-11-17 01:17:04 +00001156 if (Operand) writeOperand(Operand, true); // Work with broken code
Misha Brukman0313e0b2004-06-21 21:53:56 +00001157 Out << ", ";
Chris Lattner8f77dae2003-05-08 02:44:12 +00001158 printType(I.getType());
Chris Lattner4d45bd02003-10-18 05:57:43 +00001159 } else if (const VANextInst *VAN = dyn_cast<VANextInst>(&I)) {
Chris Lattner41495a22003-11-17 01:17:04 +00001160 if (Operand) writeOperand(Operand, true); // Work with broken code
Misha Brukman0313e0b2004-06-21 21:53:56 +00001161 Out << ", ";
Chris Lattner4d45bd02003-10-18 05:57:43 +00001162 printType(VAN->getArgType());
Chris Lattner00950542001-06-06 20:29:01 +00001163 } else if (Operand) { // Print the normal way...
1164
1165 // PrintAllTypes - Instructions who have operands of all the same type
1166 // omit the type from all but the first operand. If the instruction has
1167 // different type operands (for example br), then they are all printed.
1168 bool PrintAllTypes = false;
1169 const Type *TheType = Operand->getType();
Chris Lattner00950542001-06-06 20:29:01 +00001170
Chris Lattnercfdd1482004-03-12 05:53:14 +00001171 // Shift Left & Right print both types even for Ubyte LHS, and select prints
1172 // types even if all operands are bools.
Chris Lattnerfe0343a2005-02-09 17:45:03 +00001173 if (isa<ShiftInst>(I) || isa<SelectInst>(I) || isa<StoreInst>(I)) {
Chris Lattnerffd9bf42003-04-16 20:20:02 +00001174 PrintAllTypes = true;
1175 } else {
1176 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
1177 Operand = I.getOperand(i);
1178 if (Operand->getType() != TheType) {
1179 PrintAllTypes = true; // We have differing types! Print them all!
1180 break;
1181 }
Chris Lattner00950542001-06-06 20:29:01 +00001182 }
1183 }
Chris Lattnerffd9bf42003-04-16 20:20:02 +00001184
Chris Lattnerc1824992001-10-29 16:05:51 +00001185 if (!PrintAllTypes) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001186 Out << ' ';
Chris Lattnerffd9bf42003-04-16 20:20:02 +00001187 printType(TheType);
Chris Lattnerc1824992001-10-29 16:05:51 +00001188 }
Chris Lattner00950542001-06-06 20:29:01 +00001189
Chris Lattner7e708292002-06-25 16:13:24 +00001190 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001191 if (i) Out << ',';
Chris Lattner7e708292002-06-25 16:13:24 +00001192 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner00950542001-06-06 20:29:01 +00001193 }
1194 }
1195
Chris Lattnere02fa852001-10-13 06:42:36 +00001196 printInfoComment(I);
Misha Brukman0313e0b2004-06-21 21:53:56 +00001197 Out << "\n";
Chris Lattner00950542001-06-06 20:29:01 +00001198}
1199
1200
1201//===----------------------------------------------------------------------===//
1202// External Interface declarations
1203//===----------------------------------------------------------------------===//
1204
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001205void Module::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001206 SlotMachine SlotTable(this);
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001207 AssemblyWriter W(o, SlotTable, this, AAW);
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001208 W.write(this);
Chris Lattner00950542001-06-06 20:29:01 +00001209}
1210
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001211void GlobalVariable::print(std::ostream &o) const {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001212 SlotMachine SlotTable(getParent());
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001213 AssemblyWriter W(o, SlotTable, getParent(), 0);
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001214 W.write(this);
Chris Lattnerb0e45232001-09-10 20:08:19 +00001215}
1216
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001217void Function::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001218 SlotMachine SlotTable(getParent());
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001219 AssemblyWriter W(o, SlotTable, getParent(), AAW);
Chris Lattner00950542001-06-06 20:29:01 +00001220
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001221 W.write(this);
Chris Lattner00950542001-06-06 20:29:01 +00001222}
1223
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001224void BasicBlock::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001225 SlotMachine SlotTable(getParent());
Chris Lattnerc1824992001-10-29 16:05:51 +00001226 AssemblyWriter W(o, SlotTable,
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001227 getParent() ? getParent()->getParent() : 0, AAW);
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001228 W.write(this);
Chris Lattner00950542001-06-06 20:29:01 +00001229}
1230
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001231void Instruction::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001232 const Function *F = getParent() ? getParent()->getParent() : 0;
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001233 SlotMachine SlotTable(F);
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001234 AssemblyWriter W(o, SlotTable, F ? F->getParent() : 0, AAW);
Chris Lattner00950542001-06-06 20:29:01 +00001235
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001236 W.write(this);
Chris Lattner00950542001-06-06 20:29:01 +00001237}
Chris Lattnerda1fbcc2001-11-07 04:21:57 +00001238
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001239void Constant::print(std::ostream &o) const {
1240 if (this == 0) { o << "<null> constant value\n"; return; }
Chris Lattner3bc06b32002-09-10 15:53:49 +00001241
Misha Brukman40c732c2004-06-04 21:11:51 +00001242 o << ' ' << getType()->getDescription() << ' ';
Chris Lattner66e810b2002-04-18 18:53:13 +00001243
Chris Lattner7b13f562003-05-08 02:08:14 +00001244 std::map<const Type *, std::string> TypeTable;
Chris Lattner66e810b2002-04-18 18:53:13 +00001245 WriteConstantInt(o, this, false, TypeTable, 0);
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001246}
1247
1248void Type::print(std::ostream &o) const {
1249 if (this == 0)
1250 o << "<null Type>";
1251 else
1252 o << getDescription();
1253}
1254
Chris Lattner73e21422002-04-09 19:48:49 +00001255void Argument::print(std::ostream &o) const {
Chris Lattner144d9ba2004-07-13 23:14:34 +00001256 WriteAsOperand(o, this, true, true,
1257 getParent() ? getParent()->getParent() : 0);
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001258}
1259
Reid Spencerfa452c02004-05-25 18:14:38 +00001260// Value::dump - allow easy printing of Values from the debugger.
1261// Located here because so much of the needed functionality is here.
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001262void Value::dump() const { print(std::cerr); }
Reid Spencerfa452c02004-05-25 18:14:38 +00001263
1264// Type::dump - allow easy printing of Values from the debugger.
1265// Located here because so much of the needed functionality is here.
Reid Spencer9231ac82004-05-25 08:53:40 +00001266void Type::dump() const { print(std::cerr); }
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001267
1268//===----------------------------------------------------------------------===//
1269// CachedWriter Class Implementation
1270//===----------------------------------------------------------------------===//
1271
Chris Lattnerda1fbcc2001-11-07 04:21:57 +00001272void CachedWriter::setModule(const Module *M) {
1273 delete SC; delete AW;
1274 if (M) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001275 SC = new SlotMachine(M );
Misha Brukman40c732c2004-06-04 21:11:51 +00001276 AW = new AssemblyWriter(Out, *SC, M, 0);
Chris Lattnerda1fbcc2001-11-07 04:21:57 +00001277 } else {
1278 SC = 0; AW = 0;
1279 }
1280}
1281
1282CachedWriter::~CachedWriter() {
1283 delete AW;
1284 delete SC;
1285}
1286
Chris Lattner0a8e8e12004-07-15 02:51:31 +00001287CachedWriter &CachedWriter::operator<<(const Value &V) {
Chris Lattnerda1fbcc2001-11-07 04:21:57 +00001288 assert(AW && SC && "CachedWriter does not have a current module!");
Chris Lattner0a8e8e12004-07-15 02:51:31 +00001289 if (const Instruction *I = dyn_cast<Instruction>(&V))
Chris Lattnerfae098a2004-06-26 19:40:40 +00001290 AW->write(I);
Chris Lattner0a8e8e12004-07-15 02:51:31 +00001291 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(&V))
Chris Lattnerfae098a2004-06-26 19:40:40 +00001292 AW->write(BB);
Chris Lattner0a8e8e12004-07-15 02:51:31 +00001293 else if (const Function *F = dyn_cast<Function>(&V))
Chris Lattnerfae098a2004-06-26 19:40:40 +00001294 AW->write(F);
Chris Lattner0a8e8e12004-07-15 02:51:31 +00001295 else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(&V))
Chris Lattnerfae098a2004-06-26 19:40:40 +00001296 AW->write(GV);
Chris Lattnerfae098a2004-06-26 19:40:40 +00001297 else
Chris Lattner0a8e8e12004-07-15 02:51:31 +00001298 AW->writeOperand(&V, true, true);
Chris Lattnerda1fbcc2001-11-07 04:21:57 +00001299 return *this;
1300}
Misha Brukman5cf1acf2004-04-28 15:31:21 +00001301
Chris Lattner0a8e8e12004-07-15 02:51:31 +00001302CachedWriter& CachedWriter::operator<<(const Type &Ty) {
Misha Brukman5cf1acf2004-04-28 15:31:21 +00001303 if (SymbolicTypes) {
1304 const Module *M = AW->getModule();
Chris Lattner0a8e8e12004-07-15 02:51:31 +00001305 if (M) WriteTypeSymbolic(Out, &Ty, M);
Reid Spencer0e25e1c2004-07-04 11:50:43 +00001306 } else {
Chris Lattner0a8e8e12004-07-15 02:51:31 +00001307 AW->write(&Ty);
Reid Spencer0e25e1c2004-07-04 11:50:43 +00001308 }
1309 return *this;
Misha Brukman5cf1acf2004-04-28 15:31:21 +00001310}
Misha Brukmane5242de2004-04-28 19:24:28 +00001311
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001312//===----------------------------------------------------------------------===//
1313//===-- SlotMachine Implementation
1314//===----------------------------------------------------------------------===//
1315
1316#if 0
1317#define SC_DEBUG(X) std::cerr << X
1318#else
1319#define SC_DEBUG(X)
1320#endif
1321
1322// Module level constructor. Causes the contents of the Module (sans functions)
1323// to be added to the slot table.
1324SlotMachine::SlotMachine(const Module *M)
Reid Spencerb03de0c2004-05-26 21:56:09 +00001325 : TheModule(M) ///< Saved for lazy initialization.
1326 , TheFunction(0)
Reid Spencer28531c72004-08-16 07:46:33 +00001327 , FunctionProcessed(false)
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001328 , mMap()
Reid Spencer0e25e1c2004-07-04 11:50:43 +00001329 , mTypes()
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001330 , fMap()
Reid Spencer0e25e1c2004-07-04 11:50:43 +00001331 , fTypes()
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001332{
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001333}
1334
1335// Function level constructor. Causes the contents of the Module and the one
1336// function provided to be added to the slot table.
1337SlotMachine::SlotMachine(const Function *F )
Reid Spencerb03de0c2004-05-26 21:56:09 +00001338 : TheModule( F ? F->getParent() : 0 ) ///< Saved for lazy initialization
1339 , TheFunction(F) ///< Saved for lazy initialization
Reid Spencer28531c72004-08-16 07:46:33 +00001340 , FunctionProcessed(false)
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001341 , mMap()
Reid Spencer0e25e1c2004-07-04 11:50:43 +00001342 , mTypes()
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001343 , fMap()
Reid Spencer0e25e1c2004-07-04 11:50:43 +00001344 , fTypes()
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001345{
Reid Spencerb03de0c2004-05-26 21:56:09 +00001346}
1347
1348inline void SlotMachine::initialize(void) {
1349 if ( TheModule) {
1350 processModule();
1351 TheModule = 0; ///< Prevent re-processing next time we're called.
1352 }
Reid Spencer28531c72004-08-16 07:46:33 +00001353 if ( TheFunction && ! FunctionProcessed) {
Reid Spencerb03de0c2004-05-26 21:56:09 +00001354 processFunction();
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001355 }
1356}
1357
1358// Iterate through all the global variables, functions, and global
1359// variable initializers and create slots for them.
1360void SlotMachine::processModule() {
1361 SC_DEBUG("begin processModule!\n");
1362
1363 // Add all of the global variables to the value table...
Chris Lattnere4d5c442005-03-15 04:54:21 +00001364 for (Module::const_global_iterator I = TheModule->global_begin(), E = TheModule->global_end();
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001365 I != E; ++I)
1366 createSlot(I);
1367
1368 // Add all the functions to the table
1369 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
1370 I != E; ++I)
1371 createSlot(I);
1372
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001373 SC_DEBUG("end processModule!\n");
1374}
1375
1376
Reid Spencerb03de0c2004-05-26 21:56:09 +00001377// Process the arguments, basic blocks, and instructions of a function.
1378void SlotMachine::processFunction() {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001379 SC_DEBUG("begin processFunction!\n");
1380
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001381 // Add all the function arguments
Chris Lattnere4d5c442005-03-15 04:54:21 +00001382 for(Function::const_arg_iterator AI = TheFunction->arg_begin(),
1383 AE = TheFunction->arg_end(); AI != AE; ++AI)
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001384 createSlot(AI);
1385
1386 SC_DEBUG("Inserting Instructions:\n");
1387
1388 // Add all of the basic blocks and instructions
Reid Spencerb03de0c2004-05-26 21:56:09 +00001389 for (Function::const_iterator BB = TheFunction->begin(),
1390 E = TheFunction->end(); BB != E; ++BB) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001391 createSlot(BB);
1392 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
1393 createSlot(I);
1394 }
1395 }
1396
Reid Spencer28531c72004-08-16 07:46:33 +00001397 FunctionProcessed = true;
1398
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001399 SC_DEBUG("end processFunction!\n");
1400}
1401
1402// Clean up after incorporating a function. This is the only way
Reid Spencerb03de0c2004-05-26 21:56:09 +00001403// to get out of the function incorporation state that affects the
1404// getSlot/createSlot lock. Function incorporation state is indicated
1405// by TheFunction != 0.
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001406void SlotMachine::purgeFunction() {
1407 SC_DEBUG("begin purgeFunction!\n");
1408 fMap.clear(); // Simply discard the function level map
Reid Spencer0e25e1c2004-07-04 11:50:43 +00001409 fTypes.clear();
Reid Spencerb03de0c2004-05-26 21:56:09 +00001410 TheFunction = 0;
Reid Spencer28531c72004-08-16 07:46:33 +00001411 FunctionProcessed = false;
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001412 SC_DEBUG("end purgeFunction!\n");
1413}
1414
1415/// Get the slot number for a value. This function will assert if you
1416/// ask for a Value that hasn't previously been inserted with createSlot.
1417/// Types are forbidden because Type does not inherit from Value (any more).
Chris Lattner69566452004-06-09 19:41:19 +00001418int SlotMachine::getSlot(const Value *V) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001419 assert( V && "Can't get slot for null Value" );
Reid Spencerb03de0c2004-05-26 21:56:09 +00001420 assert(!isa<Constant>(V) || isa<GlobalValue>(V) &&
1421 "Can't insert a non-GlobalValue Constant into SlotMachine");
1422
1423 // Check for uninitialized state and do lazy initialization
1424 this->initialize();
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001425
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001426 // Get the type of the value
1427 const Type* VTy = V->getType();
1428
1429 // Find the type plane in the module map
1430 TypedPlanes::const_iterator MI = mMap.find(VTy);
1431
Reid Spencerb03de0c2004-05-26 21:56:09 +00001432 if ( TheFunction ) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001433 // Lookup the type in the function map too
1434 TypedPlanes::const_iterator FI = fMap.find(VTy);
1435 // If there is a corresponding type plane in the function map
1436 if ( FI != fMap.end() ) {
1437 // Lookup the Value in the function map
1438 ValueMap::const_iterator FVI = FI->second.map.find(V);
1439 // If the value doesn't exist in the function map
1440 if ( FVI == FI->second.map.end() ) {
Chris Lattnere9e326e2004-06-09 22:22:10 +00001441 // Look up the value in the module map.
1442 if (MI == mMap.end()) return -1;
Reid Spencerb03de0c2004-05-26 21:56:09 +00001443 ValueMap::const_iterator MVI = MI->second.map.find(V);
1444 // If we didn't find it, it wasn't inserted
Chris Lattner69566452004-06-09 19:41:19 +00001445 if (MVI == MI->second.map.end()) return -1;
Reid Spencerb03de0c2004-05-26 21:56:09 +00001446 assert( MVI != MI->second.map.end() && "Value not found");
1447 // We found it only at the module level
1448 return MVI->second;
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001449
1450 // else the value exists in the function map
1451 } else {
Reid Spencerb03de0c2004-05-26 21:56:09 +00001452 // Return the slot number as the module's contribution to
1453 // the type plane plus the index in the function's contribution
1454 // to the type plane.
Chris Lattnerd1cd3282004-06-15 21:07:32 +00001455 if (MI != mMap.end())
1456 return MI->second.next_slot + FVI->second;
1457 else
1458 return FVI->second;
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001459 }
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001460 }
1461 }
1462
Reid Spencerfc621e22004-06-09 15:26:53 +00001463 // N.B. Can get here only if either !TheFunction or the function doesn't
1464 // have a corresponding type plane for the Value
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001465
1466 // Make sure the type plane exists
Chris Lattner69566452004-06-09 19:41:19 +00001467 if (MI == mMap.end()) return -1;
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001468 // Lookup the value in the module's map
1469 ValueMap::const_iterator MVI = MI->second.map.find(V);
1470 // Make sure we found it.
Chris Lattner69566452004-06-09 19:41:19 +00001471 if (MVI == MI->second.map.end()) return -1;
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001472 // Return it.
1473 return MVI->second;
1474}
1475
Reid Spencer0e25e1c2004-07-04 11:50:43 +00001476/// Get the slot number for a value. This function will assert if you
1477/// ask for a Value that hasn't previously been inserted with createSlot.
1478/// Types are forbidden because Type does not inherit from Value (any more).
1479int SlotMachine::getSlot(const Type *Ty) {
1480 assert( Ty && "Can't get slot for null Type" );
1481
1482 // Check for uninitialized state and do lazy initialization
1483 this->initialize();
1484
1485 if ( TheFunction ) {
1486 // Lookup the Type in the function map
1487 TypeMap::const_iterator FTI = fTypes.map.find(Ty);
1488 // If the Type doesn't exist in the function map
1489 if ( FTI == fTypes.map.end() ) {
1490 TypeMap::const_iterator MTI = mTypes.map.find(Ty);
1491 // If we didn't find it, it wasn't inserted
1492 if (MTI == mTypes.map.end())
1493 return -1;
1494 // We found it only at the module level
1495 return MTI->second;
1496
1497 // else the value exists in the function map
1498 } else {
1499 // Return the slot number as the module's contribution to
1500 // the type plane plus the index in the function's contribution
1501 // to the type plane.
1502 return mTypes.next_slot + FTI->second;
1503 }
1504 }
1505
1506 // N.B. Can get here only if either !TheFunction
1507
1508 // Lookup the value in the module's map
1509 TypeMap::const_iterator MTI = mTypes.map.find(Ty);
1510 // Make sure we found it.
1511 if (MTI == mTypes.map.end()) return -1;
1512 // Return it.
1513 return MTI->second;
1514}
1515
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001516// Create a new slot, or return the existing slot if it is already
1517// inserted. Note that the logic here parallels getSlot but instead
1518// of asserting when the Value* isn't found, it inserts the value.
1519unsigned SlotMachine::createSlot(const Value *V) {
1520 assert( V && "Can't insert a null Value to SlotMachine");
Reid Spencerb03de0c2004-05-26 21:56:09 +00001521 assert(!isa<Constant>(V) || isa<GlobalValue>(V) &&
1522 "Can't insert a non-GlobalValue Constant into SlotMachine");
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001523
1524 const Type* VTy = V->getType();
1525
1526 // Just ignore void typed things
1527 if (VTy == Type::VoidTy) return 0; // FIXME: Wrong return value!
1528
1529 // Look up the type plane for the Value's type from the module map
1530 TypedPlanes::const_iterator MI = mMap.find(VTy);
1531
Reid Spencerb03de0c2004-05-26 21:56:09 +00001532 if ( TheFunction ) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001533 // Get the type plane for the Value's type from the function map
1534 TypedPlanes::const_iterator FI = fMap.find(VTy);
1535 // If there is a corresponding type plane in the function map
1536 if ( FI != fMap.end() ) {
1537 // Lookup the Value in the function map
1538 ValueMap::const_iterator FVI = FI->second.map.find(V);
1539 // If the value doesn't exist in the function map
1540 if ( FVI == FI->second.map.end() ) {
Reid Spencerb03de0c2004-05-26 21:56:09 +00001541 // If there is no corresponding type plane in the module map
1542 if ( MI == mMap.end() )
1543 return insertValue(V);
1544 // Look up the value in the module map
1545 ValueMap::const_iterator MVI = MI->second.map.find(V);
1546 // If we didn't find it, it wasn't inserted
1547 if ( MVI == MI->second.map.end() )
1548 return insertValue(V);
1549 else
1550 // We found it only at the module level
1551 return MVI->second;
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001552
1553 // else the value exists in the function map
1554 } else {
Reid Spencerb03de0c2004-05-26 21:56:09 +00001555 if ( MI == mMap.end() )
1556 return FVI->second;
1557 else
1558 // Return the slot number as the module's contribution to
1559 // the type plane plus the index in the function's contribution
1560 // to the type plane.
1561 return MI->second.next_slot + FVI->second;
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001562 }
1563
1564 // else there is not a corresponding type plane in the function map
1565 } else {
1566 // If the type plane doesn't exists at the module level
1567 if ( MI == mMap.end() ) {
Reid Spencerb03de0c2004-05-26 21:56:09 +00001568 return insertValue(V);
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001569 // else type plane exists at the module level, examine it
1570 } else {
Reid Spencerb03de0c2004-05-26 21:56:09 +00001571 // Look up the value in the module's map
1572 ValueMap::const_iterator MVI = MI->second.map.find(V);
1573 // If we didn't find it there either
1574 if ( MVI == MI->second.map.end() )
1575 // Return the slot number as the module's contribution to
1576 // the type plane plus the index of the function map insertion.
1577 return MI->second.next_slot + insertValue(V);
1578 else
1579 return MVI->second;
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001580 }
1581 }
1582 }
1583
Reid Spencerb03de0c2004-05-26 21:56:09 +00001584 // N.B. Can only get here if !TheFunction
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001585
1586 // If the module map's type plane is not for the Value's type
1587 if ( MI != mMap.end() ) {
1588 // Lookup the value in the module's map
1589 ValueMap::const_iterator MVI = MI->second.map.find(V);
1590 if ( MVI != MI->second.map.end() )
1591 return MVI->second;
1592 }
1593
1594 return insertValue(V);
1595}
1596
Reid Spencer0e25e1c2004-07-04 11:50:43 +00001597// Create a new slot, or return the existing slot if it is already
1598// inserted. Note that the logic here parallels getSlot but instead
1599// of asserting when the Value* isn't found, it inserts the value.
1600unsigned SlotMachine::createSlot(const Type *Ty) {
1601 assert( Ty && "Can't insert a null Type to SlotMachine");
1602
1603 if ( TheFunction ) {
1604 // Lookup the Type in the function map
1605 TypeMap::const_iterator FTI = fTypes.map.find(Ty);
1606 // If the type doesn't exist in the function map
1607 if ( FTI == fTypes.map.end() ) {
1608 // Look up the type in the module map
1609 TypeMap::const_iterator MTI = mTypes.map.find(Ty);
1610 // If we didn't find it, it wasn't inserted
1611 if ( MTI == mTypes.map.end() )
1612 return insertValue(Ty);
1613 else
1614 // We found it only at the module level
1615 return MTI->second;
1616
1617 // else the value exists in the function map
1618 } else {
1619 // Return the slot number as the module's contribution to
1620 // the type plane plus the index in the function's contribution
1621 // to the type plane.
1622 return mTypes.next_slot + FTI->second;
1623 }
1624 }
1625
1626 // N.B. Can only get here if !TheFunction
1627
1628 // Lookup the type in the module's map
1629 TypeMap::const_iterator MTI = mTypes.map.find(Ty);
1630 if ( MTI != mTypes.map.end() )
1631 return MTI->second;
1632
1633 return insertValue(Ty);
1634}
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001635
1636// Low level insert function. Minimal checking is done. This
1637// function is just for the convenience of createSlot (above).
1638unsigned SlotMachine::insertValue(const Value *V ) {
1639 assert(V && "Can't insert a null Value into SlotMachine!");
Reid Spencerb03de0c2004-05-26 21:56:09 +00001640 assert(!isa<Constant>(V) || isa<GlobalValue>(V) &&
1641 "Can't insert a non-GlobalValue Constant into SlotMachine");
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001642
Reid Spencerb03de0c2004-05-26 21:56:09 +00001643 // If this value does not contribute to a plane (is void)
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001644 // or if the value already has a name then ignore it.
Reid Spencerb03de0c2004-05-26 21:56:09 +00001645 if (V->getType() == Type::VoidTy || V->hasName() ) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001646 SC_DEBUG("ignored value " << *V << "\n");
1647 return 0; // FIXME: Wrong return value
1648 }
1649
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001650 const Type *VTy = V->getType();
1651 unsigned DestSlot = 0;
1652
Reid Spencerb03de0c2004-05-26 21:56:09 +00001653 if ( TheFunction ) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001654 TypedPlanes::iterator I = fMap.find( VTy );
1655 if ( I == fMap.end() )
Reid Spencer0e25e1c2004-07-04 11:50:43 +00001656 I = fMap.insert(std::make_pair(VTy,ValuePlane())).first;
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001657 DestSlot = I->second.map[V] = I->second.next_slot++;
1658 } else {
1659 TypedPlanes::iterator I = mMap.find( VTy );
1660 if ( I == mMap.end() )
Reid Spencer0e25e1c2004-07-04 11:50:43 +00001661 I = mMap.insert(std::make_pair(VTy,ValuePlane())).first;
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001662 DestSlot = I->second.map[V] = I->second.next_slot++;
1663 }
1664
1665 SC_DEBUG(" Inserting value [" << VTy << "] = " << V << " slot=" <<
Reid Spencerb03de0c2004-05-26 21:56:09 +00001666 DestSlot << " [");
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001667 // G = Global, C = Constant, T = Type, F = Function, o = other
Reid Spencer79703962004-07-17 23:47:01 +00001668 SC_DEBUG((isa<GlobalVariable>(V) ? 'G' : (isa<Function>(V) ? 'F' :
1669 (isa<Constant>(V) ? 'C' : 'o'))));
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001670 SC_DEBUG("]\n");
1671 return DestSlot;
1672}
1673
Reid Spencer0e25e1c2004-07-04 11:50:43 +00001674// Low level insert function. Minimal checking is done. This
1675// function is just for the convenience of createSlot (above).
1676unsigned SlotMachine::insertValue(const Type *Ty ) {
1677 assert(Ty && "Can't insert a null Type into SlotMachine!");
1678
1679 unsigned DestSlot = 0;
1680
1681 if ( TheFunction ) {
1682 DestSlot = fTypes.map[Ty] = fTypes.next_slot++;
1683 } else {
1684 DestSlot = fTypes.map[Ty] = fTypes.next_slot++;
1685 }
1686 SC_DEBUG(" Inserting type [" << DestSlot << "] = " << Ty << "\n");
1687 return DestSlot;
1688}
1689
Reid Spencer9231ac82004-05-25 08:53:40 +00001690// vim: sw=2