blob: 8f12f13baa4a7ba0fc0bc9a046b0c2be724de3f0 [file] [log] [blame]
Chris Lattnerf7e79482002-04-07 22:31:46 +00001//===-- AsmWriter.cpp - Printing LLVM as an assembly file -----------------===//
John Criswell482202a2003-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 Lattner2f7c9632001-06-06 20:29:01 +00009//
10// This library implements the functionality defined in llvm/Assembly/Writer.h
11//
Chris Lattner189088e2002-04-12 18:21:53 +000012// Note that these routines must be extremely tolerant of various errors in the
Chris Lattnerf70da102003-05-08 02:44:12 +000013// LLVM code, because it can be used for debugging transformations.
Chris Lattner189088e2002-04-12 18:21:53 +000014//
Chris Lattner2f7c9632001-06-06 20:29:01 +000015//===----------------------------------------------------------------------===//
16
Chris Lattner7db79582001-11-07 04:21:57 +000017#include "llvm/Assembly/CachedWriter.h"
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +000018#include "llvm/Assembly/Writer.h"
Chris Lattner7f8845a2002-07-23 18:07:49 +000019#include "llvm/Assembly/PrintModulePass.h"
Chris Lattner8339f7d2003-10-30 23:41:03 +000020#include "llvm/Assembly/AsmAnnotationWriter.h"
Chris Lattnerc70b3f62004-01-20 19:50:34 +000021#include "llvm/Constants.h"
Chris Lattner913d18f2002-04-29 18:46:50 +000022#include "llvm/DerivedTypes.h"
Vikram S. Adveb952b542002-07-14 23:14:45 +000023#include "llvm/Instruction.h"
Misha Brukman2d3fa9e2004-07-29 16:53:53 +000024#include "llvm/Instructions.h"
Chris Lattnerc70b3f62004-01-20 19:50:34 +000025#include "llvm/Module.h"
Chris Lattnerfee714f2001-09-07 16:36:04 +000026#include "llvm/SymbolTable.h"
Misha Brukman4685e262004-04-28 15:31:21 +000027#include "llvm/Assembly/Writer.h"
Chris Lattner58185f22002-10-02 19:38:55 +000028#include "llvm/Support/CFG.h"
Chris Lattner5de22042001-11-27 00:03:19 +000029#include "Support/StringExtras.h"
30#include "Support/STLExtras.h"
Chris Lattnerfee714f2001-09-07 16:36:04 +000031#include <algorithm>
Chris Lattner189d19f2003-11-21 20:23:48 +000032using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000033
Chris Lattner60a7dd12004-07-15 02:51:31 +000034namespace llvm {
Reid Spencer16f2f7f2004-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 Spencer58d30f22004-07-04 11:50:43 +000046 typedef std::map<const Type*, unsigned> TypeMap;
Reid Spencer16f2f7f2004-05-26 07:18:52 +000047
48 /// @brief A plane with next slot number and ValueMap
Reid Spencer58d30f22004-07-04 11:50:43 +000049 struct ValuePlane {
Reid Spencer16f2f7f2004-05-26 07:18:52 +000050 unsigned next_slot; ///< The next slot number to use
51 ValueMap map; ///< The map of Value* -> unsigned
Reid Spencer58d30f22004-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 Spencer16f2f7f2004-05-26 07:18:52 +000060 };
61
62 /// @brief The map of planes by Type
Reid Spencer58d30f22004-07-04 11:50:43 +000063 typedef std::map<const Type*, ValuePlane> TypedPlanes;
Reid Spencer16f2f7f2004-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 Lattner757ee0b2004-06-09 19:41:19 +000082 int getSlot(const Value *V);
Reid Spencer58d30f22004-07-04 11:50:43 +000083 int getSlot(const Type*Ty);
Reid Spencer8beac692004-06-09 15:26:53 +000084
85 /// Determine if a Value has a slot or not
86 bool hasSlot(const Value* V);
Reid Spencer58d30f22004-07-04 11:50:43 +000087 bool hasSlot(const Type* Ty);
Reid Spencer16f2f7f2004-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 Spencer56010e42004-05-26 21:56:09 +000095 void incorporateFunction(const Function *F) { TheFunction = F; }
Reid Spencer16f2f7f2004-05-26 07:18:52 +000096
97 /// After calling incorporateFunction, use this method to remove the
98 /// most recently incorporated function from the SlotMachine. This
99 /// will reset the state of the machine back to just the module contents.
100 void purgeFunction();
101
102/// @}
103/// @name Implementation Details
104/// @{
105private:
Reid Spencer56010e42004-05-26 21:56:09 +0000106 /// This function does the actual initialization.
107 inline void initialize();
108
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000109 /// Values can be crammed into here at will. If they haven't
110 /// been inserted already, they get inserted, otherwise they are ignored.
111 /// Either way, the slot number for the Value* is returned.
112 unsigned createSlot(const Value *V);
Reid Spencer58d30f22004-07-04 11:50:43 +0000113 unsigned createSlot(const Type* Ty);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000114
115 /// Insert a value into the value table. Return the slot number
116 /// that it now occupies. BadThings(TM) will happen if you insert a
117 /// Value that's already been inserted.
118 unsigned insertValue( const Value *V );
Reid Spencer58d30f22004-07-04 11:50:43 +0000119 unsigned insertValue( const Type* Ty);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000120
121 /// Add all of the module level global variables (and their initializers)
122 /// and function declarations, but not the contents of those functions.
123 void processModule();
124
Reid Spencer56010e42004-05-26 21:56:09 +0000125 /// Add all of the functions arguments, basic blocks, and instructions
126 void processFunction();
127
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000128 SlotMachine(const SlotMachine &); // DO NOT IMPLEMENT
129 void operator=(const SlotMachine &); // DO NOT IMPLEMENT
130
131/// @}
132/// @name Data
133/// @{
134public:
135
136 /// @brief The module for which we are holding slot numbers
Reid Spencer56010e42004-05-26 21:56:09 +0000137 const Module* TheModule;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000138
Reid Spencer56010e42004-05-26 21:56:09 +0000139 /// @brief The function for which we are holding slot numbers
140 const Function* TheFunction;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000141
142 /// @brief The TypePlanes map for the module level data
143 TypedPlanes mMap;
Reid Spencer58d30f22004-07-04 11:50:43 +0000144 TypePlane mTypes;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000145
146 /// @brief The TypePlanes map for the function level data
147 TypedPlanes fMap;
Reid Spencer58d30f22004-07-04 11:50:43 +0000148 TypePlane fTypes;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000149
150/// @}
151
152};
153
Chris Lattner60a7dd12004-07-15 02:51:31 +0000154} // end namespace llvm
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000155
Chris Lattnerc8b70922002-07-26 21:12:46 +0000156static RegisterPass<PrintModulePass>
157X("printm", "Print module to stderr",PassInfo::Analysis|PassInfo::Optimization);
158static RegisterPass<PrintFunctionPass>
159Y("print","Print function to stderr",PassInfo::Analysis|PassInfo::Optimization);
Chris Lattner7f8845a2002-07-23 18:07:49 +0000160
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000161static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
162 bool PrintName,
163 std::map<const Type *, std::string> &TypeTable,
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000164 SlotMachine *Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000165
Reid Spencer58d30f22004-07-04 11:50:43 +0000166static void WriteAsOperandInternal(std::ostream &Out, const Type *T,
167 bool PrintName,
168 std::map<const Type *, std::string> &TypeTable,
169 SlotMachine *Machine);
170
Chris Lattnerb86620e2001-10-29 16:37:48 +0000171static const Module *getModuleFromVal(const Value *V) {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000172 if (const Argument *MA = dyn_cast<Argument>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +0000173 return MA->getParent() ? MA->getParent()->getParent() : 0;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000174 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +0000175 return BB->getParent() ? BB->getParent()->getParent() : 0;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000176 else if (const Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner57698e22002-03-26 18:01:55 +0000177 const Function *M = I->getParent() ? I->getParent()->getParent() : 0;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000178 return M ? M->getParent() : 0;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000179 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +0000180 return GV->getParent();
Chris Lattnerb86620e2001-10-29 16:37:48 +0000181 return 0;
182}
183
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000184static SlotMachine *createSlotMachine(const Value *V) {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000185 if (const Argument *FA = dyn_cast<Argument>(V)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000186 return new SlotMachine(FA->getParent());
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000187 } else if (const Instruction *I = dyn_cast<Instruction>(V)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000188 return new SlotMachine(I->getParent()->getParent());
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000189 } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000190 return new SlotMachine(BB->getParent());
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000191 } else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)){
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000192 return new SlotMachine(GV->getParent());
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000193 } else if (const Function *Func = dyn_cast<Function>(V)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000194 return new SlotMachine(Func);
Chris Lattner7bfee412001-10-29 16:05:51 +0000195 }
196 return 0;
197}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000198
Chris Lattner1c343042003-08-22 05:40:38 +0000199// getLLVMName - Turn the specified string into an 'LLVM name', which is either
200// prefixed with % (if the string only contains simple characters) or is
201// surrounded with ""'s (if it has special chars in it).
202static std::string getLLVMName(const std::string &Name) {
203 assert(!Name.empty() && "Cannot get empty name!");
204
205 // First character cannot start with a number...
206 if (Name[0] >= '0' && Name[0] <= '9')
207 return "\"" + Name + "\"";
208
209 // Scan to see if we have any characters that are not on the "white list"
210 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
211 char C = Name[i];
212 assert(C != '"' && "Illegal character in LLVM value name!");
213 if ((C < 'a' || C > 'z') && (C < 'A' || C > 'Z') && (C < '0' || C > '9') &&
214 C != '-' && C != '.' && C != '_')
215 return "\"" + Name + "\"";
216 }
217
218 // If we get here, then the identifier is legal to use as a "VarID".
219 return "%"+Name;
220}
221
Chris Lattnerb86620e2001-10-29 16:37:48 +0000222
Misha Brukmanc566ca362004-03-02 00:22:19 +0000223/// fillTypeNameTable - If the module has a symbol table, take all global types
224/// and stuff their names into the TypeNames map.
225///
Chris Lattnerb86620e2001-10-29 16:37:48 +0000226static void fillTypeNameTable(const Module *M,
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000227 std::map<const Type *, std::string> &TypeNames) {
Chris Lattner98cf1f52002-11-20 18:36:02 +0000228 if (!M) return;
229 const SymbolTable &ST = M->getSymbolTable();
Reid Spencere7e96712004-05-25 08:53:40 +0000230 SymbolTable::type_const_iterator TI = ST.type_begin();
231 for (; TI != ST.type_end(); ++TI ) {
232 // As a heuristic, don't insert pointer to primitive types, because
233 // they are used too often to have a single useful name.
234 //
235 const Type *Ty = cast<Type>(TI->second);
236 if (!isa<PointerType>(Ty) ||
Reid Spencer56010e42004-05-26 21:56:09 +0000237 !cast<PointerType>(Ty)->getElementType()->isPrimitiveType() ||
238 isa<OpaqueType>(cast<PointerType>(Ty)->getElementType()))
Reid Spencere7e96712004-05-25 08:53:40 +0000239 TypeNames.insert(std::make_pair(Ty, getLLVMName(TI->first)));
Chris Lattnerb86620e2001-10-29 16:37:48 +0000240 }
241}
242
243
244
John Criswellcd116ba2004-06-01 14:54:08 +0000245static void calcTypeName(const Type *Ty,
246 std::vector<const Type *> &TypeStack,
247 std::map<const Type *, std::string> &TypeNames,
248 std::string & Result){
249 if (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty)) {
250 Result += Ty->getDescription(); // Base case
251 return;
252 }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000253
254 // Check to see if the type is named.
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000255 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
John Criswellcd116ba2004-06-01 14:54:08 +0000256 if (I != TypeNames.end()) {
257 Result += I->second;
258 return;
259 }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000260
John Criswellcd116ba2004-06-01 14:54:08 +0000261 if (isa<OpaqueType>(Ty)) {
262 Result += "opaque";
263 return;
264 }
Chris Lattnerf14ead92003-10-30 00:22:33 +0000265
Chris Lattnerb86620e2001-10-29 16:37:48 +0000266 // Check to see if the Type is already on the stack...
267 unsigned Slot = 0, CurSize = TypeStack.size();
268 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
269
270 // This is another base case for the recursion. In this case, we know
271 // that we have looped back to a type that we have previously visited.
272 // Generate the appropriate upreference to handle this.
John Criswellcd116ba2004-06-01 14:54:08 +0000273 if (Slot < CurSize) {
274 Result += "\\" + utostr(CurSize-Slot); // Here's the upreference
275 return;
276 }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000277
278 TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
279
Chris Lattner6b727592004-06-17 18:19:28 +0000280 switch (Ty->getTypeID()) {
Chris Lattner91db5822002-03-29 03:44:36 +0000281 case Type::FunctionTyID: {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000282 const FunctionType *FTy = cast<FunctionType>(Ty);
John Criswellcd116ba2004-06-01 14:54:08 +0000283 calcTypeName(FTy->getReturnType(), TypeStack, TypeNames, Result);
284 Result += " (";
Chris Lattnerfa829be2004-02-09 04:14:01 +0000285 for (FunctionType::param_iterator I = FTy->param_begin(),
286 E = FTy->param_end(); I != E; ++I) {
287 if (I != FTy->param_begin())
Chris Lattnerb86620e2001-10-29 16:37:48 +0000288 Result += ", ";
John Criswellcd116ba2004-06-01 14:54:08 +0000289 calcTypeName(*I, TypeStack, TypeNames, Result);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000290 }
Chris Lattnerd816b532002-04-13 20:53:41 +0000291 if (FTy->isVarArg()) {
Chris Lattnerfa829be2004-02-09 04:14:01 +0000292 if (FTy->getNumParams()) Result += ", ";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000293 Result += "...";
294 }
295 Result += ")";
296 break;
297 }
298 case Type::StructTyID: {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000299 const StructType *STy = cast<StructType>(Ty);
John Criswellcd116ba2004-06-01 14:54:08 +0000300 Result += "{ ";
Chris Lattnerac6db752004-02-09 04:37:31 +0000301 for (StructType::element_iterator I = STy->element_begin(),
302 E = STy->element_end(); I != E; ++I) {
303 if (I != STy->element_begin())
Chris Lattnerb86620e2001-10-29 16:37:48 +0000304 Result += ", ";
John Criswellcd116ba2004-06-01 14:54:08 +0000305 calcTypeName(*I, TypeStack, TypeNames, Result);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000306 }
307 Result += " }";
308 break;
309 }
310 case Type::PointerTyID:
John Criswellcd116ba2004-06-01 14:54:08 +0000311 calcTypeName(cast<PointerType>(Ty)->getElementType(),
312 TypeStack, TypeNames, Result);
313 Result += "*";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000314 break;
315 case Type::ArrayTyID: {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000316 const ArrayType *ATy = cast<ArrayType>(Ty);
John Criswellcd116ba2004-06-01 14:54:08 +0000317 Result += "[" + utostr(ATy->getNumElements()) + " x ";
318 calcTypeName(ATy->getElementType(), TypeStack, TypeNames, Result);
319 Result += "]";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000320 break;
321 }
Chris Lattner15285ab2003-05-14 17:50:47 +0000322 case Type::OpaqueTyID:
John Criswellcd116ba2004-06-01 14:54:08 +0000323 Result += "opaque";
Chris Lattner15285ab2003-05-14 17:50:47 +0000324 break;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000325 default:
John Criswellcd116ba2004-06-01 14:54:08 +0000326 Result += "<unrecognized-type>";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000327 }
328
329 TypeStack.pop_back(); // Remove self from stack...
John Criswellcd116ba2004-06-01 14:54:08 +0000330 return;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000331}
332
333
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000334/// printTypeInt - The internal guts of printing out a type that has a
335/// potentially named portion.
336///
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000337static std::ostream &printTypeInt(std::ostream &Out, const Type *Ty,
338 std::map<const Type *, std::string> &TypeNames) {
Chris Lattnerb86620e2001-10-29 16:37:48 +0000339 // Primitive types always print out their description, regardless of whether
340 // they have been named or not.
341 //
Chris Lattner92d60532003-10-30 00:12:51 +0000342 if (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty))
343 return Out << Ty->getDescription();
Chris Lattnerb86620e2001-10-29 16:37:48 +0000344
345 // Check to see if the type is named.
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000346 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000347 if (I != TypeNames.end()) return Out << I->second;
348
349 // Otherwise we have a type that has not been named but is a derived type.
350 // Carefully recurse the type hierarchy to print out any contained symbolic
351 // names.
352 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000353 std::vector<const Type *> TypeStack;
John Criswellcd116ba2004-06-01 14:54:08 +0000354 std::string TypeName;
355 calcTypeName(Ty, TypeStack, TypeNames, TypeName);
Chris Lattner7f74a562002-01-20 22:54:45 +0000356 TypeNames.insert(std::make_pair(Ty, TypeName));//Cache type name for later use
John Criswellcd116ba2004-06-01 14:54:08 +0000357 return (Out << TypeName);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000358}
359
Chris Lattner34b95182001-10-31 04:33:19 +0000360
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000361/// WriteTypeSymbolic - This attempts to write the specified type as a symbolic
362/// type, iff there is an entry in the modules symbol table for the specified
363/// type or one of it's component types. This is slower than a simple x << Type
364///
Chris Lattner189d19f2003-11-21 20:23:48 +0000365std::ostream &llvm::WriteTypeSymbolic(std::ostream &Out, const Type *Ty,
366 const Module *M) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000367 Out << ' ';
Chris Lattnerb86620e2001-10-29 16:37:48 +0000368
369 // If they want us to print out a type, attempt to make it symbolic if there
370 // is a symbol table in the module...
Chris Lattner98cf1f52002-11-20 18:36:02 +0000371 if (M) {
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000372 std::map<const Type *, std::string> TypeNames;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000373 fillTypeNameTable(M, TypeNames);
374
Chris Lattner72f866e2001-10-29 16:40:32 +0000375 return printTypeInt(Out, Ty, TypeNames);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000376 } else {
Chris Lattner72f866e2001-10-29 16:40:32 +0000377 return Out << Ty->getDescription();
Chris Lattnerb86620e2001-10-29 16:37:48 +0000378 }
379}
380
Reid Spenceraccd7c72004-07-17 23:47:01 +0000381/// @brief Internal constant writer.
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000382static void WriteConstantInt(std::ostream &Out, const Constant *CV,
383 bool PrintName,
384 std::map<const Type *, std::string> &TypeTable,
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000385 SlotMachine *Machine) {
Chris Lattner1e194682002-04-18 18:53:13 +0000386 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
387 Out << (CB == ConstantBool::True ? "true" : "false");
388 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV)) {
389 Out << CI->getValue();
390 } else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV)) {
391 Out << CI->getValue();
392 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
393 // We would like to output the FP constant value in exponential notation,
394 // but we cannot do this if doing so will lose precision. Check here to
395 // make sure that we only output it in exponential format if we can parse
396 // the value back and get the same value.
397 //
398 std::string StrVal = ftostr(CFP->getValue());
399
400 // Check to make sure that the stringized number is not some string like
401 // "Inf" or NaN, that atof will accept, but the lexer will not. Check that
402 // the string matches the "[-+]?[0-9]" regex.
403 //
404 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
405 ((StrVal[0] == '-' || StrVal[0] == '+') &&
Brian Gaeke87b4f072003-06-17 23:55:35 +0000406 (StrVal[1] >= '0' && StrVal[1] <= '9')))
Chris Lattner1e194682002-04-18 18:53:13 +0000407 // Reparse stringized version!
408 if (atof(StrVal.c_str()) == CFP->getValue()) {
409 Out << StrVal; return;
410 }
411
412 // Otherwise we could not reparse it to exactly the same value, so we must
413 // output the string in hexadecimal format!
414 //
415 // Behave nicely in the face of C TBAA rules... see:
416 // http://www.nullstone.com/htmls/category/aliastyp.htm
417 //
418 double Val = CFP->getValue();
419 char *Ptr = (char*)&Val;
420 assert(sizeof(double) == sizeof(uint64_t) && sizeof(double) == 8 &&
421 "assuming that double is 64 bits!");
422 Out << "0x" << utohexstr(*(uint64_t*)Ptr);
423
Chris Lattner76b2ff42004-02-15 05:55:15 +0000424 } else if (isa<ConstantAggregateZero>(CV)) {
425 Out << "zeroinitializer";
Chris Lattner1e194682002-04-18 18:53:13 +0000426 } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
427 // As a special case, print the array as a string if it is an array of
428 // ubytes or an array of sbytes with positive values.
429 //
430 const Type *ETy = CA->getType()->getElementType();
431 bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
432
433 if (ETy == Type::SByteTy)
434 for (unsigned i = 0; i < CA->getNumOperands(); ++i)
435 if (cast<ConstantSInt>(CA->getOperand(i))->getValue() < 0) {
436 isString = false;
437 break;
438 }
439
440 if (isString) {
441 Out << "c\"";
442 for (unsigned i = 0; i < CA->getNumOperands(); ++i) {
Chris Lattneraa6ff272004-06-04 23:53:20 +0000443 unsigned char C =
444 (unsigned char)cast<ConstantInt>(CA->getOperand(i))->getRawValue();
Chris Lattner1e194682002-04-18 18:53:13 +0000445
Chris Lattnere5fd3862002-07-31 23:56:44 +0000446 if (isprint(C) && C != '"' && C != '\\') {
Chris Lattner1e194682002-04-18 18:53:13 +0000447 Out << C;
448 } else {
449 Out << '\\'
450 << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'))
451 << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
452 }
453 }
454 Out << "\"";
455
456 } else { // Cannot output in string format...
Misha Brukman21bbdb92004-06-04 21:11:51 +0000457 Out << '[';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000458 if (CA->getNumOperands()) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000459 Out << ' ';
Chris Lattner1e194682002-04-18 18:53:13 +0000460 printTypeInt(Out, ETy, TypeTable);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000461 WriteAsOperandInternal(Out, CA->getOperand(0),
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000462 PrintName, TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000463 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
464 Out << ", ";
Chris Lattner1e194682002-04-18 18:53:13 +0000465 printTypeInt(Out, ETy, TypeTable);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000466 WriteAsOperandInternal(Out, CA->getOperand(i), PrintName,
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000467 TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000468 }
469 }
470 Out << " ]";
471 }
472 } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000473 Out << '{';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000474 if (CS->getNumOperands()) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000475 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000476 printTypeInt(Out, CS->getOperand(0)->getType(), TypeTable);
477
478 WriteAsOperandInternal(Out, CS->getOperand(0),
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000479 PrintName, TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000480
481 for (unsigned i = 1; i < CS->getNumOperands(); i++) {
482 Out << ", ";
483 printTypeInt(Out, CS->getOperand(i)->getType(), TypeTable);
484
485 WriteAsOperandInternal(Out, CS->getOperand(i),
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000486 PrintName, TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000487 }
488 }
489
490 Out << " }";
491 } else if (isa<ConstantPointerNull>(CV)) {
492 Out << "null";
493
Chris Lattner3cd8c562002-07-30 18:54:25 +0000494 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Chris Lattnerb8257222003-03-06 23:23:32 +0000495 Out << CE->getOpcodeName() << " (";
Vikram S. Adveb952b542002-07-14 23:14:45 +0000496
497 for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
498 printTypeInt(Out, (*OI)->getType(), TypeTable);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000499 WriteAsOperandInternal(Out, *OI, PrintName, TypeTable, Machine);
Vikram S. Adveb952b542002-07-14 23:14:45 +0000500 if (OI+1 != CE->op_end())
Chris Lattner3cd8c562002-07-30 18:54:25 +0000501 Out << ", ";
Vikram S. Adveb952b542002-07-14 23:14:45 +0000502 }
503
Chris Lattnercfe8f532002-08-16 21:17:11 +0000504 if (CE->getOpcode() == Instruction::Cast) {
Chris Lattner83b396b2002-08-15 19:37:43 +0000505 Out << " to ";
506 printTypeInt(Out, CE->getType(), TypeTable);
507 }
Misha Brukman21bbdb92004-06-04 21:11:51 +0000508 Out << ')';
Chris Lattner83b396b2002-08-15 19:37:43 +0000509
Chris Lattnerd84bb632002-04-16 21:36:08 +0000510 } else {
Vikram S. Adveb952b542002-07-14 23:14:45 +0000511 Out << "<placeholder or erroneous Constant>";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000512 }
513}
514
515
Misha Brukmanc566ca362004-03-02 00:22:19 +0000516/// WriteAsOperand - Write the name of the specified value out to the specified
517/// ostream. This can be useful when you just want to print int %reg126, not
518/// the whole instruction that generated it.
519///
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000520static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
521 bool PrintName,
522 std::map<const Type*, std::string> &TypeTable,
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000523 SlotMachine *Machine) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000524 Out << ' ';
Reid Spenceraccd7c72004-07-17 23:47:01 +0000525 if ((PrintName || isa<GlobalValue>(V)) && V->hasName())
Chris Lattner1c343042003-08-22 05:40:38 +0000526 Out << getLLVMName(V->getName());
Reid Spenceraccd7c72004-07-17 23:47:01 +0000527 else {
528 const Constant *CV = dyn_cast<Constant>(V);
529 if (CV && !isa<GlobalValue>(CV))
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000530 WriteConstantInt(Out, CV, PrintName, TypeTable, Machine);
Reid Spenceraccd7c72004-07-17 23:47:01 +0000531 else {
Chris Lattnerd84bb632002-04-16 21:36:08 +0000532 int Slot;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000533 if (Machine) {
Reid Spencer56010e42004-05-26 21:56:09 +0000534 Slot = Machine->getSlot(V);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000535 } else {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000536 Machine = createSlotMachine(V);
Chris Lattner757ee0b2004-06-09 19:41:19 +0000537 if (Machine == 0)
538 Slot = Machine->getSlot(V);
539 else
540 Slot = -1;
Reid Spencer56010e42004-05-26 21:56:09 +0000541 delete Machine;
Chris Lattnerd84bb632002-04-16 21:36:08 +0000542 }
Chris Lattner757ee0b2004-06-09 19:41:19 +0000543 if (Slot != -1)
544 Out << '%' << Slot;
545 else
546 Out << "<badref>";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000547 }
548 }
549}
550
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000551/// WriteAsOperand - Write the name of the specified value out to the specified
552/// ostream. This can be useful when you just want to print int %reg126, not
553/// the whole instruction that generated it.
554///
Chris Lattner189d19f2003-11-21 20:23:48 +0000555std::ostream &llvm::WriteAsOperand(std::ostream &Out, const Value *V,
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000556 bool PrintType, bool PrintName,
557 const Module *Context) {
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000558 std::map<const Type *, std::string> TypeNames;
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000559 if (Context == 0) Context = getModuleFromVal(V);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000560
Chris Lattner98cf1f52002-11-20 18:36:02 +0000561 if (Context)
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000562 fillTypeNameTable(Context, TypeNames);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000563
564 if (PrintType)
565 printTypeInt(Out, V->getType(), TypeNames);
566
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000567 WriteAsOperandInternal(Out, V, PrintName, TypeNames, 0);
Chris Lattner5e5abe32001-07-20 19:15:21 +0000568 return Out;
569}
570
Reid Spencer58d30f22004-07-04 11:50:43 +0000571/// WriteAsOperandInternal - Write the name of the specified value out to
572/// the specified ostream. This can be useful when you just want to print
573/// int %reg126, not the whole instruction that generated it.
574///
575static void WriteAsOperandInternal(std::ostream &Out, const Type *T,
576 bool PrintName,
577 std::map<const Type*, std::string> &TypeTable,
578 SlotMachine *Machine) {
579 Out << ' ';
580 int Slot;
581 if (Machine) {
582 Slot = Machine->getSlot(T);
583 if (Slot != -1)
584 Out << '%' << Slot;
585 else
586 Out << "<badref>";
587 } else {
588 Out << T->getDescription();
589 }
590}
591
592/// WriteAsOperand - Write the name of the specified value out to the specified
593/// ostream. This can be useful when you just want to print int %reg126, not
594/// the whole instruction that generated it.
595///
596std::ostream &llvm::WriteAsOperand(std::ostream &Out, const Type *Ty,
597 bool PrintType, bool PrintName,
598 const Module *Context) {
599 std::map<const Type *, std::string> TypeNames;
600 assert(Context != 0 && "Can't write types as operand without module context");
601
602 fillTypeNameTable(Context, TypeNames);
603
604 // if (PrintType)
605 // printTypeInt(Out, V->getType(), TypeNames);
606
607 printTypeInt(Out, Ty, TypeNames);
608
609 WriteAsOperandInternal(Out, Ty, PrintName, TypeNames, 0);
610 return Out;
611}
612
Chris Lattner189d19f2003-11-21 20:23:48 +0000613namespace llvm {
Chris Lattner2e9fee42001-07-12 23:35:26 +0000614
Chris Lattnerfee714f2001-09-07 16:36:04 +0000615class AssemblyWriter {
Misha Brukmana6619a92004-06-21 21:53:56 +0000616 std::ostream &Out;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000617 SlotMachine &Machine;
Chris Lattner7bfee412001-10-29 16:05:51 +0000618 const Module *TheModule;
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000619 std::map<const Type *, std::string> TypeNames;
Chris Lattner8339f7d2003-10-30 23:41:03 +0000620 AssemblyAnnotationWriter *AnnotationWriter;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000621public:
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000622 inline AssemblyWriter(std::ostream &o, SlotMachine &Mac, const Module *M,
Chris Lattner8339f7d2003-10-30 23:41:03 +0000623 AssemblyAnnotationWriter *AAW)
Misha Brukmana6619a92004-06-21 21:53:56 +0000624 : Out(o), Machine(Mac), TheModule(M), AnnotationWriter(AAW) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000625
626 // If the module has a symbol table, take all global types and stuff their
627 // names into the TypeNames map.
628 //
Chris Lattnerb86620e2001-10-29 16:37:48 +0000629 fillTypeNameTable(M, TypeNames);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000630 }
631
Chris Lattner7bfee412001-10-29 16:05:51 +0000632 inline void write(const Module *M) { printModule(M); }
633 inline void write(const GlobalVariable *G) { printGlobal(G); }
Chris Lattner57698e22002-03-26 18:01:55 +0000634 inline void write(const Function *F) { printFunction(F); }
Chris Lattner7bfee412001-10-29 16:05:51 +0000635 inline void write(const BasicBlock *BB) { printBasicBlock(BB); }
Chris Lattner113f4f42002-06-25 16:13:24 +0000636 inline void write(const Instruction *I) { printInstruction(*I); }
Chris Lattner3462ae32001-12-03 22:26:30 +0000637 inline void write(const Constant *CPV) { printConstant(CPV); }
Chris Lattner7db79582001-11-07 04:21:57 +0000638 inline void write(const Type *Ty) { printType(Ty); }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000639
Chris Lattner1e194682002-04-18 18:53:13 +0000640 void writeOperand(const Value *Op, bool PrintType, bool PrintName = true);
641
Misha Brukman4685e262004-04-28 15:31:21 +0000642 const Module* getModule() { return TheModule; }
643
Chris Lattner2f7c9632001-06-06 20:29:01 +0000644private :
Chris Lattner7bfee412001-10-29 16:05:51 +0000645 void printModule(const Module *M);
646 void printSymbolTable(const SymbolTable &ST);
Chris Lattner3462ae32001-12-03 22:26:30 +0000647 void printConstant(const Constant *CPV);
Chris Lattner7bfee412001-10-29 16:05:51 +0000648 void printGlobal(const GlobalVariable *GV);
Chris Lattner57698e22002-03-26 18:01:55 +0000649 void printFunction(const Function *F);
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000650 void printArgument(const Argument *FA);
Chris Lattner7bfee412001-10-29 16:05:51 +0000651 void printBasicBlock(const BasicBlock *BB);
Chris Lattner113f4f42002-06-25 16:13:24 +0000652 void printInstruction(const Instruction &I);
Chris Lattnerd816b532002-04-13 20:53:41 +0000653
654 // printType - Go to extreme measures to attempt to print out a short,
655 // symbolic version of a type name.
656 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000657 std::ostream &printType(const Type *Ty) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000658 return printTypeInt(Out, Ty, TypeNames);
Chris Lattnerd816b532002-04-13 20:53:41 +0000659 }
660
661 // printTypeAtLeastOneLevel - Print out one level of the possibly complex type
662 // without considering any symbolic types that we may have equal to it.
663 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000664 std::ostream &printTypeAtLeastOneLevel(const Type *Ty);
Chris Lattner7bfee412001-10-29 16:05:51 +0000665
Chris Lattner862e3382001-10-13 06:42:36 +0000666 // printInfoComment - Print a little comment after the instruction indicating
667 // which slot it occupies.
Chris Lattner113f4f42002-06-25 16:13:24 +0000668 void printInfoComment(const Value &V);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000669};
Reid Spencerf43ac622004-05-27 22:04:46 +0000670} // end of llvm namespace
Chris Lattner2f7c9632001-06-06 20:29:01 +0000671
Misha Brukmanc566ca362004-03-02 00:22:19 +0000672/// printTypeAtLeastOneLevel - Print out one level of the possibly complex type
673/// without considering any symbolic types that we may have equal to it.
674///
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000675std::ostream &AssemblyWriter::printTypeAtLeastOneLevel(const Type *Ty) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000676 if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000677 printType(FTy->getReturnType()) << " (";
Chris Lattnerfa829be2004-02-09 04:14:01 +0000678 for (FunctionType::param_iterator I = FTy->param_begin(),
679 E = FTy->param_end(); I != E; ++I) {
680 if (I != FTy->param_begin())
Misha Brukmana6619a92004-06-21 21:53:56 +0000681 Out << ", ";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000682 printType(*I);
Chris Lattnerd816b532002-04-13 20:53:41 +0000683 }
684 if (FTy->isVarArg()) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000685 if (FTy->getNumParams()) Out << ", ";
686 Out << "...";
Chris Lattnerd816b532002-04-13 20:53:41 +0000687 }
Misha Brukmana6619a92004-06-21 21:53:56 +0000688 Out << ')';
Chris Lattner113f4f42002-06-25 16:13:24 +0000689 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000690 Out << "{ ";
Chris Lattnerac6db752004-02-09 04:37:31 +0000691 for (StructType::element_iterator I = STy->element_begin(),
692 E = STy->element_end(); I != E; ++I) {
693 if (I != STy->element_begin())
Misha Brukmana6619a92004-06-21 21:53:56 +0000694 Out << ", ";
Chris Lattnerd816b532002-04-13 20:53:41 +0000695 printType(*I);
696 }
Misha Brukmana6619a92004-06-21 21:53:56 +0000697 Out << " }";
Chris Lattner113f4f42002-06-25 16:13:24 +0000698 } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000699 printType(PTy->getElementType()) << '*';
Chris Lattner113f4f42002-06-25 16:13:24 +0000700 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000701 Out << '[' << ATy->getNumElements() << " x ";
Misha Brukman21bbdb92004-06-04 21:11:51 +0000702 printType(ATy->getElementType()) << ']';
Chris Lattner113f4f42002-06-25 16:13:24 +0000703 } else if (const OpaqueType *OTy = dyn_cast<OpaqueType>(Ty)) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000704 Out << "opaque";
Chris Lattnerd816b532002-04-13 20:53:41 +0000705 } else {
Vikram S. Adveb952b542002-07-14 23:14:45 +0000706 if (!Ty->isPrimitiveType())
Misha Brukmana6619a92004-06-21 21:53:56 +0000707 Out << "<unknown derived type>";
Chris Lattnerd816b532002-04-13 20:53:41 +0000708 printType(Ty);
709 }
Misha Brukmana6619a92004-06-21 21:53:56 +0000710 return Out;
Chris Lattnerd816b532002-04-13 20:53:41 +0000711}
712
713
Chris Lattnerfee714f2001-09-07 16:36:04 +0000714void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType,
Reid Spencer56010e42004-05-26 21:56:09 +0000715 bool PrintName) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000716 if (PrintType) { Out << ' '; printType(Operand->getType()); }
717 WriteAsOperandInternal(Out, Operand, PrintName, TypeNames, &Machine);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000718}
719
Chris Lattner2f7c9632001-06-06 20:29:01 +0000720
Chris Lattner7bfee412001-10-29 16:05:51 +0000721void AssemblyWriter::printModule(const Module *M) {
Chris Lattner8068e0c2003-08-24 13:48:48 +0000722 switch (M->getEndianness()) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000723 case Module::LittleEndian: Out << "target endian = little\n"; break;
724 case Module::BigEndian: Out << "target endian = big\n"; break;
Chris Lattner8068e0c2003-08-24 13:48:48 +0000725 case Module::AnyEndianness: break;
726 }
727 switch (M->getPointerSize()) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000728 case Module::Pointer32: Out << "target pointersize = 32\n"; break;
729 case Module::Pointer64: Out << "target pointersize = 64\n"; break;
Chris Lattner8068e0c2003-08-24 13:48:48 +0000730 case Module::AnyPointerSize: break;
731 }
Reid Spencer48f98c82004-07-25 21:44:54 +0000732 if (!M->getTargetTriple().empty())
Reid Spencerffec7df2004-07-25 21:29:43 +0000733 Out << "target triple = \"" << M->getTargetTriple() << "\"\n";
Reid Spencercc5ff642004-07-25 18:08:18 +0000734
735 // Loop over the dependent libraries and emit them
Reid Spencer48f98c82004-07-25 21:44:54 +0000736 Module::lib_iterator LI= M->lib_begin();
737 Module::lib_iterator LE= M->lib_end();
738 if (LI != LE) {
Reid Spencerffec7df2004-07-25 21:29:43 +0000739 Out << "deplibs = [\n";
Reid Spencer48f98c82004-07-25 21:44:54 +0000740 while ( LI != LE ) {
Reid Spencerffec7df2004-07-25 21:29:43 +0000741 Out << "\"" << *LI << "\"";
742 ++LI;
743 if ( LI != LE )
744 Out << ",\n";
745 }
746 Out << " ]\n";
Reid Spencercc5ff642004-07-25 18:08:18 +0000747 }
Chris Lattner8068e0c2003-08-24 13:48:48 +0000748
Chris Lattnerfee714f2001-09-07 16:36:04 +0000749 // Loop over the symbol table, emitting all named constants...
Chris Lattner98cf1f52002-11-20 18:36:02 +0000750 printSymbolTable(M->getSymbolTable());
Chris Lattnerda975502001-09-10 07:58:01 +0000751
Chris Lattner113f4f42002-06-25 16:13:24 +0000752 for (Module::const_giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
753 printGlobal(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000754
Misha Brukmana6619a92004-06-21 21:53:56 +0000755 Out << "\nimplementation ; Functions:\n";
Vikram S. Adve13ba19a2001-09-18 12:48:16 +0000756
Chris Lattner6915f8f2002-04-07 22:49:37 +0000757 // Output all of the functions...
Chris Lattner113f4f42002-06-25 16:13:24 +0000758 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
759 printFunction(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000760}
761
Chris Lattner7bfee412001-10-29 16:05:51 +0000762void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000763 if (GV->hasName()) Out << getLLVMName(GV->getName()) << " = ";
Chris Lattner37798642001-09-18 04:01:05 +0000764
Chris Lattner379a8d22003-04-16 20:28:45 +0000765 if (!GV->hasInitializer())
Misha Brukmana6619a92004-06-21 21:53:56 +0000766 Out << "external ";
Chris Lattner379a8d22003-04-16 20:28:45 +0000767 else
768 switch (GV->getLinkage()) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000769 case GlobalValue::InternalLinkage: Out << "internal "; break;
770 case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
771 case GlobalValue::WeakLinkage: Out << "weak "; break;
772 case GlobalValue::AppendingLinkage: Out << "appending "; break;
Chris Lattner379a8d22003-04-16 20:28:45 +0000773 case GlobalValue::ExternalLinkage: break;
774 }
Chris Lattner37798642001-09-18 04:01:05 +0000775
Misha Brukmana6619a92004-06-21 21:53:56 +0000776 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattner2413b162001-12-04 00:03:30 +0000777 printType(GV->getType()->getElementType());
Chris Lattner37798642001-09-18 04:01:05 +0000778
Reid Spenceraccd7c72004-07-17 23:47:01 +0000779 if (GV->hasInitializer()) {
780 Constant* C = cast<Constant>(GV->getInitializer());
781 assert(C && "GlobalVar initializer isn't constant?");
Reid Spencerc9c90cf2004-07-18 01:04:19 +0000782 writeOperand(GV->getInitializer(), false, isa<GlobalValue>(C));
Reid Spenceraccd7c72004-07-17 23:47:01 +0000783 }
Chris Lattner37798642001-09-18 04:01:05 +0000784
Chris Lattner113f4f42002-06-25 16:13:24 +0000785 printInfoComment(*GV);
Misha Brukmana6619a92004-06-21 21:53:56 +0000786 Out << "\n";
Chris Lattnerda975502001-09-10 07:58:01 +0000787}
788
Chris Lattnerfee714f2001-09-07 16:36:04 +0000789
Reid Spencere7e96712004-05-25 08:53:40 +0000790// printSymbolTable - Run through symbol table looking for constants
791// and types. Emit their declarations.
Chris Lattner7bfee412001-10-29 16:05:51 +0000792void AssemblyWriter::printSymbolTable(const SymbolTable &ST) {
Reid Spencere7e96712004-05-25 08:53:40 +0000793
794 // Print the types.
795 for (SymbolTable::type_const_iterator TI = ST.type_begin();
796 TI != ST.type_end(); ++TI ) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000797 Out << "\t" << getLLVMName(TI->first) << " = type ";
Reid Spencere7e96712004-05-25 08:53:40 +0000798
799 // Make sure we print out at least one level of the type structure, so
800 // that we do not get %FILE = type %FILE
801 //
802 printTypeAtLeastOneLevel(TI->second) << "\n";
803 }
Chris Lattnerfee714f2001-09-07 16:36:04 +0000804
Reid Spencere7e96712004-05-25 08:53:40 +0000805 // Print the constants, in type plane order.
806 for (SymbolTable::plane_const_iterator PI = ST.plane_begin();
807 PI != ST.plane_end(); ++PI ) {
808 SymbolTable::value_const_iterator VI = ST.value_begin(PI->first);
809 SymbolTable::value_const_iterator VE = ST.value_end(PI->first);
810
811 for (; VI != VE; ++VI) {
Reid Spenceraccd7c72004-07-17 23:47:01 +0000812 const Value* V = VI->second;
813 const Constant *CPV = dyn_cast<Constant>(V) ;
814 if (CPV && !isa<GlobalValue>(V)) {
Reid Spencer56010e42004-05-26 21:56:09 +0000815 printConstant(CPV);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000816 }
817 }
Chris Lattnera7620d92001-07-15 06:35:59 +0000818 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000819}
820
821
Misha Brukmanc566ca362004-03-02 00:22:19 +0000822/// printConstant - Print out a constant pool entry...
823///
Chris Lattner3462ae32001-12-03 22:26:30 +0000824void AssemblyWriter::printConstant(const Constant *CPV) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000825 // Don't print out unnamed constants, they will be inlined
826 if (!CPV->hasName()) return;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000827
Chris Lattneree998be2001-07-26 16:29:38 +0000828 // Print out name...
Misha Brukmana6619a92004-06-21 21:53:56 +0000829 Out << "\t" << getLLVMName(CPV->getName()) << " =";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000830
831 // Write the value out now...
Chris Lattnerd84bb632002-04-16 21:36:08 +0000832 writeOperand(CPV, true, false);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000833
Chris Lattner113f4f42002-06-25 16:13:24 +0000834 printInfoComment(*CPV);
Misha Brukmana6619a92004-06-21 21:53:56 +0000835 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000836}
837
Misha Brukmanc566ca362004-03-02 00:22:19 +0000838/// printFunction - Print all aspects of a function.
839///
Chris Lattner113f4f42002-06-25 16:13:24 +0000840void AssemblyWriter::printFunction(const Function *F) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000841 // Print out the return type and name...
Misha Brukmana6619a92004-06-21 21:53:56 +0000842 Out << "\n";
Chris Lattner379a8d22003-04-16 20:28:45 +0000843
Misha Brukmana6619a92004-06-21 21:53:56 +0000844 if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +0000845
Chris Lattner379a8d22003-04-16 20:28:45 +0000846 if (F->isExternal())
Misha Brukmana6619a92004-06-21 21:53:56 +0000847 Out << "declare ";
Chris Lattner379a8d22003-04-16 20:28:45 +0000848 else
849 switch (F->getLinkage()) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000850 case GlobalValue::InternalLinkage: Out << "internal "; break;
851 case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
852 case GlobalValue::WeakLinkage: Out << "weak "; break;
853 case GlobalValue::AppendingLinkage: Out << "appending "; break;
Chris Lattner379a8d22003-04-16 20:28:45 +0000854 case GlobalValue::ExternalLinkage: break;
855 }
856
Misha Brukman21bbdb92004-06-04 21:11:51 +0000857 printType(F->getReturnType()) << ' ';
Chris Lattner5b337482003-10-18 05:57:43 +0000858 if (!F->getName().empty())
Misha Brukmana6619a92004-06-21 21:53:56 +0000859 Out << getLLVMName(F->getName());
Chris Lattner5b337482003-10-18 05:57:43 +0000860 else
Misha Brukmana6619a92004-06-21 21:53:56 +0000861 Out << "\"\"";
862 Out << '(';
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000863 Machine.incorporateFunction(F);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000864
Chris Lattner7bfee412001-10-29 16:05:51 +0000865 // Loop over the arguments, printing them...
Chris Lattner113f4f42002-06-25 16:13:24 +0000866 const FunctionType *FT = F->getFunctionType();
Chris Lattnerfee714f2001-09-07 16:36:04 +0000867
Chris Lattner149376d2002-10-13 20:57:00 +0000868 for(Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I)
869 printArgument(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000870
871 // Finish printing arguments...
Chris Lattner113f4f42002-06-25 16:13:24 +0000872 if (FT->isVarArg()) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000873 if (FT->getNumParams()) Out << ", ";
874 Out << "..."; // Output varargs portion of signature!
Chris Lattnerfee714f2001-09-07 16:36:04 +0000875 }
Misha Brukmana6619a92004-06-21 21:53:56 +0000876 Out << ')';
Chris Lattnerfee714f2001-09-07 16:36:04 +0000877
Chris Lattner113f4f42002-06-25 16:13:24 +0000878 if (F->isExternal()) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000879 Out << "\n";
Chris Lattnerb2f02e52002-05-06 03:00:40 +0000880 } else {
Misha Brukmana6619a92004-06-21 21:53:56 +0000881 Out << " {";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000882
Chris Lattner6915f8f2002-04-07 22:49:37 +0000883 // Output all of its basic blocks... for the function
Chris Lattner113f4f42002-06-25 16:13:24 +0000884 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
885 printBasicBlock(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000886
Misha Brukmana6619a92004-06-21 21:53:56 +0000887 Out << "}\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000888 }
889
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000890 Machine.purgeFunction();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000891}
892
Misha Brukmanc566ca362004-03-02 00:22:19 +0000893/// printArgument - This member is called for every argument that is passed into
894/// the function. Simply print it out
895///
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000896void AssemblyWriter::printArgument(const Argument *Arg) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000897 // Insert commas as we go... the first arg doesn't get a comma
Misha Brukmana6619a92004-06-21 21:53:56 +0000898 if (Arg != &Arg->getParent()->afront()) Out << ", ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000899
900 // Output type...
Chris Lattner7bfee412001-10-29 16:05:51 +0000901 printType(Arg->getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000902
903 // Output name, if available...
904 if (Arg->hasName())
Misha Brukmana6619a92004-06-21 21:53:56 +0000905 Out << ' ' << getLLVMName(Arg->getName());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000906}
907
Misha Brukmanc566ca362004-03-02 00:22:19 +0000908/// printBasicBlock - This member is called for each basic block in a method.
909///
Chris Lattner7bfee412001-10-29 16:05:51 +0000910void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000911 if (BB->hasName()) { // Print out the label if it exists...
Misha Brukmana6619a92004-06-21 21:53:56 +0000912 Out << "\n" << BB->getName() << ':';
Chris Lattner408dbdb2002-05-14 16:02:05 +0000913 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Misha Brukmana6619a92004-06-21 21:53:56 +0000914 Out << "\n; <label>:";
Chris Lattner757ee0b2004-06-09 19:41:19 +0000915 int Slot = Machine.getSlot(BB);
916 if (Slot != -1)
Misha Brukmana6619a92004-06-21 21:53:56 +0000917 Out << Slot;
Chris Lattner757ee0b2004-06-09 19:41:19 +0000918 else
Misha Brukmana6619a92004-06-21 21:53:56 +0000919 Out << "<badref>";
Chris Lattner58185f22002-10-02 19:38:55 +0000920 }
Chris Lattner2447ef52003-11-20 00:09:43 +0000921
922 if (BB->getParent() == 0)
Misha Brukmana6619a92004-06-21 21:53:56 +0000923 Out << "\t\t; Error: Block without parent!";
Chris Lattner2447ef52003-11-20 00:09:43 +0000924 else {
925 if (BB != &BB->getParent()->front()) { // Not the entry block?
926 // Output predecessors for the block...
Misha Brukmana6619a92004-06-21 21:53:56 +0000927 Out << "\t\t;";
Chris Lattner2447ef52003-11-20 00:09:43 +0000928 pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
929
930 if (PI == PE) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000931 Out << " No predecessors!";
Chris Lattner2447ef52003-11-20 00:09:43 +0000932 } else {
Misha Brukmana6619a92004-06-21 21:53:56 +0000933 Out << " preds =";
Chris Lattner00211f12003-11-16 22:59:57 +0000934 writeOperand(*PI, false, true);
Chris Lattner2447ef52003-11-20 00:09:43 +0000935 for (++PI; PI != PE; ++PI) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000936 Out << ',';
Chris Lattner2447ef52003-11-20 00:09:43 +0000937 writeOperand(*PI, false, true);
938 }
Chris Lattner00211f12003-11-16 22:59:57 +0000939 }
Chris Lattner58185f22002-10-02 19:38:55 +0000940 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000941 }
Chris Lattner408dbdb2002-05-14 16:02:05 +0000942
Misha Brukmana6619a92004-06-21 21:53:56 +0000943 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000944
Misha Brukmana6619a92004-06-21 21:53:56 +0000945 if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +0000946
Chris Lattnerfee714f2001-09-07 16:36:04 +0000947 // Output all of the instructions in the basic block...
Chris Lattner113f4f42002-06-25 16:13:24 +0000948 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
949 printInstruction(*I);
Chris Lattner96cdd272004-03-08 18:51:45 +0000950
Misha Brukmana6619a92004-06-21 21:53:56 +0000951 if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000952}
953
Chris Lattner862e3382001-10-13 06:42:36 +0000954
Misha Brukmanc566ca362004-03-02 00:22:19 +0000955/// printInfoComment - Print a little comment after the instruction indicating
956/// which slot it occupies.
957///
Chris Lattner113f4f42002-06-25 16:13:24 +0000958void AssemblyWriter::printInfoComment(const Value &V) {
959 if (V.getType() != Type::VoidTy) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000960 Out << "\t\t; <";
Misha Brukman21bbdb92004-06-04 21:11:51 +0000961 printType(V.getType()) << '>';
Chris Lattner862e3382001-10-13 06:42:36 +0000962
Chris Lattner113f4f42002-06-25 16:13:24 +0000963 if (!V.hasName()) {
Chris Lattner757ee0b2004-06-09 19:41:19 +0000964 int SlotNum = Machine.getSlot(&V);
965 if (SlotNum == -1)
Misha Brukmana6619a92004-06-21 21:53:56 +0000966 Out << ":<badref>";
Reid Spencer8beac692004-06-09 15:26:53 +0000967 else
Misha Brukmana6619a92004-06-21 21:53:56 +0000968 Out << ':' << SlotNum; // Print out the def slot taken.
Chris Lattner862e3382001-10-13 06:42:36 +0000969 }
Misha Brukmana6619a92004-06-21 21:53:56 +0000970 Out << " [#uses=" << V.use_size() << ']'; // Output # uses
Chris Lattner862e3382001-10-13 06:42:36 +0000971 }
972}
973
Reid Spencer8beac692004-06-09 15:26:53 +0000974/// printInstruction - This member is called for each Instruction in a function..
Misha Brukmanc566ca362004-03-02 00:22:19 +0000975///
Chris Lattner113f4f42002-06-25 16:13:24 +0000976void AssemblyWriter::printInstruction(const Instruction &I) {
Misha Brukmana6619a92004-06-21 21:53:56 +0000977 if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +0000978
Misha Brukmana6619a92004-06-21 21:53:56 +0000979 Out << "\t";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000980
981 // Print out name if it exists...
Chris Lattner113f4f42002-06-25 16:13:24 +0000982 if (I.hasName())
Misha Brukmana6619a92004-06-21 21:53:56 +0000983 Out << getLLVMName(I.getName()) << " = ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000984
Chris Lattner504f9242003-09-08 17:45:59 +0000985 // If this is a volatile load or store, print out the volatile marker
986 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
987 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile()))
Misha Brukmana6619a92004-06-21 21:53:56 +0000988 Out << "volatile ";
Chris Lattner504f9242003-09-08 17:45:59 +0000989
Chris Lattner2f7c9632001-06-06 20:29:01 +0000990 // Print out the opcode...
Misha Brukmana6619a92004-06-21 21:53:56 +0000991 Out << I.getOpcodeName();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000992
993 // Print out the type of the operands...
Chris Lattner113f4f42002-06-25 16:13:24 +0000994 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000995
996 // Special case conditional branches to swizzle the condition out to the front
Chris Lattner113f4f42002-06-25 16:13:24 +0000997 if (isa<BranchInst>(I) && I.getNumOperands() > 1) {
998 writeOperand(I.getOperand(2), true);
Misha Brukmana6619a92004-06-21 21:53:56 +0000999 Out << ',';
Chris Lattner2f7c9632001-06-06 20:29:01 +00001000 writeOperand(Operand, true);
Misha Brukmana6619a92004-06-21 21:53:56 +00001001 Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001002 writeOperand(I.getOperand(1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001003
Chris Lattner8d48df22002-04-13 18:34:38 +00001004 } else if (isa<SwitchInst>(I)) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001005 // Special case switch statement to get formatting nice and correct...
Misha Brukmana6619a92004-06-21 21:53:56 +00001006 writeOperand(Operand , true); Out << ',';
1007 writeOperand(I.getOperand(1), true); Out << " [";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001008
Chris Lattner113f4f42002-06-25 16:13:24 +00001009 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001010 Out << "\n\t\t";
1011 writeOperand(I.getOperand(op ), true); Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001012 writeOperand(I.getOperand(op+1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001013 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001014 Out << "\n\t]";
Chris Lattnerda558102001-10-02 03:41:24 +00001015 } else if (isa<PHINode>(I)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001016 Out << ' ';
Chris Lattner113f4f42002-06-25 16:13:24 +00001017 printType(I.getType());
Misha Brukmana6619a92004-06-21 21:53:56 +00001018 Out << ' ';
Chris Lattner2f7c9632001-06-06 20:29:01 +00001019
Chris Lattner113f4f42002-06-25 16:13:24 +00001020 for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001021 if (op) Out << ", ";
1022 Out << '[';
1023 writeOperand(I.getOperand(op ), false); Out << ',';
1024 writeOperand(I.getOperand(op+1), false); Out << " ]";
Chris Lattner931ef3b2001-06-11 15:04:20 +00001025 }
Chris Lattner862e3382001-10-13 06:42:36 +00001026 } else if (isa<ReturnInst>(I) && !Operand) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001027 Out << " void";
Chris Lattner862e3382001-10-13 06:42:36 +00001028 } else if (isa<CallInst>(I)) {
Chris Lattner463d6a52003-08-05 15:34:45 +00001029 const PointerType *PTy = cast<PointerType>(Operand->getType());
1030 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1031 const Type *RetTy = FTy->getReturnType();
Chris Lattner2f2d9472001-11-06 21:28:12 +00001032
Chris Lattner463d6a52003-08-05 15:34:45 +00001033 // If possible, print out the short form of the call instruction. We can
Chris Lattner6915f8f2002-04-07 22:49:37 +00001034 // only do this if the first argument is a pointer to a nonvararg function,
Chris Lattner463d6a52003-08-05 15:34:45 +00001035 // and if the return type is not a pointer to a function.
Chris Lattner2f2d9472001-11-06 21:28:12 +00001036 //
Chris Lattner463d6a52003-08-05 15:34:45 +00001037 if (!FTy->isVarArg() &&
Chris Lattner8d48df22002-04-13 18:34:38 +00001038 (!isa<PointerType>(RetTy) ||
Chris Lattnerd9a36a62002-07-25 20:58:51 +00001039 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001040 Out << ' '; printType(RetTy);
Chris Lattner2f2d9472001-11-06 21:28:12 +00001041 writeOperand(Operand, false);
1042 } else {
1043 writeOperand(Operand, true);
1044 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001045 Out << '(';
Chris Lattner113f4f42002-06-25 16:13:24 +00001046 if (I.getNumOperands() > 1) writeOperand(I.getOperand(1), true);
1047 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; ++op) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001048 Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001049 writeOperand(I.getOperand(op), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001050 }
1051
Misha Brukmana6619a92004-06-21 21:53:56 +00001052 Out << " )";
Chris Lattner113f4f42002-06-25 16:13:24 +00001053 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Chris Lattner463d6a52003-08-05 15:34:45 +00001054 const PointerType *PTy = cast<PointerType>(Operand->getType());
1055 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1056 const Type *RetTy = FTy->getReturnType();
1057
1058 // If possible, print out the short form of the invoke instruction. We can
1059 // only do this if the first argument is a pointer to a nonvararg function,
1060 // and if the return type is not a pointer to a function.
1061 //
1062 if (!FTy->isVarArg() &&
1063 (!isa<PointerType>(RetTy) ||
1064 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001065 Out << ' '; printType(RetTy);
Chris Lattner463d6a52003-08-05 15:34:45 +00001066 writeOperand(Operand, false);
1067 } else {
1068 writeOperand(Operand, true);
1069 }
1070
Misha Brukmana6619a92004-06-21 21:53:56 +00001071 Out << '(';
Chris Lattner113f4f42002-06-25 16:13:24 +00001072 if (I.getNumOperands() > 3) writeOperand(I.getOperand(3), true);
1073 for (unsigned op = 4, Eop = I.getNumOperands(); op < Eop; ++op) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001074 Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001075 writeOperand(I.getOperand(op), true);
Chris Lattner862e3382001-10-13 06:42:36 +00001076 }
1077
Misha Brukmana6619a92004-06-21 21:53:56 +00001078 Out << " )\n\t\t\tto";
Chris Lattner862e3382001-10-13 06:42:36 +00001079 writeOperand(II->getNormalDest(), true);
Misha Brukmana6619a92004-06-21 21:53:56 +00001080 Out << " unwind";
Chris Lattnerfae8ab32004-02-08 21:44:31 +00001081 writeOperand(II->getUnwindDest(), true);
Chris Lattner862e3382001-10-13 06:42:36 +00001082
Chris Lattner113f4f42002-06-25 16:13:24 +00001083 } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(&I)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001084 Out << ' ';
Chris Lattner8d48df22002-04-13 18:34:38 +00001085 printType(AI->getType()->getElementType());
1086 if (AI->isArrayAllocation()) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001087 Out << ',';
Chris Lattner8d48df22002-04-13 18:34:38 +00001088 writeOperand(AI->getArraySize(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001089 }
Chris Lattner862e3382001-10-13 06:42:36 +00001090 } else if (isa<CastInst>(I)) {
Chris Lattnerc96e96b2003-11-17 01:17:04 +00001091 if (Operand) writeOperand(Operand, true); // Work with broken code
Misha Brukmana6619a92004-06-21 21:53:56 +00001092 Out << " to ";
Chris Lattner113f4f42002-06-25 16:13:24 +00001093 printType(I.getType());
Chris Lattner5b337482003-10-18 05:57:43 +00001094 } else if (isa<VAArgInst>(I)) {
Chris Lattnerc96e96b2003-11-17 01:17:04 +00001095 if (Operand) writeOperand(Operand, true); // Work with broken code
Misha Brukmana6619a92004-06-21 21:53:56 +00001096 Out << ", ";
Chris Lattnerf70da102003-05-08 02:44:12 +00001097 printType(I.getType());
Chris Lattner5b337482003-10-18 05:57:43 +00001098 } else if (const VANextInst *VAN = dyn_cast<VANextInst>(&I)) {
Chris Lattnerc96e96b2003-11-17 01:17:04 +00001099 if (Operand) writeOperand(Operand, true); // Work with broken code
Misha Brukmana6619a92004-06-21 21:53:56 +00001100 Out << ", ";
Chris Lattner5b337482003-10-18 05:57:43 +00001101 printType(VAN->getArgType());
Chris Lattner2f7c9632001-06-06 20:29:01 +00001102 } else if (Operand) { // Print the normal way...
1103
1104 // PrintAllTypes - Instructions who have operands of all the same type
1105 // omit the type from all but the first operand. If the instruction has
1106 // different type operands (for example br), then they are all printed.
1107 bool PrintAllTypes = false;
1108 const Type *TheType = Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001109
Chris Lattner52bd5cb92004-03-12 05:53:14 +00001110 // Shift Left & Right print both types even for Ubyte LHS, and select prints
1111 // types even if all operands are bools.
1112 if (isa<ShiftInst>(I) || isa<SelectInst>(I)) {
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00001113 PrintAllTypes = true;
1114 } else {
1115 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
1116 Operand = I.getOperand(i);
1117 if (Operand->getType() != TheType) {
1118 PrintAllTypes = true; // We have differing types! Print them all!
1119 break;
1120 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001121 }
1122 }
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00001123
Chris Lattner7bfee412001-10-29 16:05:51 +00001124 if (!PrintAllTypes) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001125 Out << ' ';
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00001126 printType(TheType);
Chris Lattner7bfee412001-10-29 16:05:51 +00001127 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001128
Chris Lattner113f4f42002-06-25 16:13:24 +00001129 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001130 if (i) Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001131 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001132 }
1133 }
1134
Chris Lattner862e3382001-10-13 06:42:36 +00001135 printInfoComment(I);
Misha Brukmana6619a92004-06-21 21:53:56 +00001136 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001137}
1138
1139
1140//===----------------------------------------------------------------------===//
1141// External Interface declarations
1142//===----------------------------------------------------------------------===//
1143
Chris Lattner8339f7d2003-10-30 23:41:03 +00001144void Module::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001145 SlotMachine SlotTable(this);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001146 AssemblyWriter W(o, SlotTable, this, AAW);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001147 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001148}
1149
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001150void GlobalVariable::print(std::ostream &o) const {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001151 SlotMachine SlotTable(getParent());
Chris Lattner8339f7d2003-10-30 23:41:03 +00001152 AssemblyWriter W(o, SlotTable, getParent(), 0);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001153 W.write(this);
Chris Lattneradfe0d12001-09-10 20:08:19 +00001154}
1155
Chris Lattner8339f7d2003-10-30 23:41:03 +00001156void Function::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001157 SlotMachine SlotTable(getParent());
Chris Lattner8339f7d2003-10-30 23:41:03 +00001158 AssemblyWriter W(o, SlotTable, getParent(), AAW);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001159
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001160 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001161}
1162
Chris Lattner8339f7d2003-10-30 23:41:03 +00001163void BasicBlock::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001164 SlotMachine SlotTable(getParent());
Chris Lattner7bfee412001-10-29 16:05:51 +00001165 AssemblyWriter W(o, SlotTable,
Chris Lattner8339f7d2003-10-30 23:41:03 +00001166 getParent() ? getParent()->getParent() : 0, AAW);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001167 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001168}
1169
Chris Lattner8339f7d2003-10-30 23:41:03 +00001170void Instruction::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001171 const Function *F = getParent() ? getParent()->getParent() : 0;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001172 SlotMachine SlotTable(F);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001173 AssemblyWriter W(o, SlotTable, F ? F->getParent() : 0, AAW);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001174
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001175 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001176}
Chris Lattner7db79582001-11-07 04:21:57 +00001177
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001178void Constant::print(std::ostream &o) const {
1179 if (this == 0) { o << "<null> constant value\n"; return; }
Chris Lattner7d734802002-09-10 15:53:49 +00001180
Misha Brukman21bbdb92004-06-04 21:11:51 +00001181 o << ' ' << getType()->getDescription() << ' ';
Chris Lattner1e194682002-04-18 18:53:13 +00001182
Chris Lattnerab7d1ab2003-05-08 02:08:14 +00001183 std::map<const Type *, std::string> TypeTable;
Chris Lattner1e194682002-04-18 18:53:13 +00001184 WriteConstantInt(o, this, false, TypeTable, 0);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001185}
1186
1187void Type::print(std::ostream &o) const {
1188 if (this == 0)
1189 o << "<null Type>";
1190 else
1191 o << getDescription();
1192}
1193
Chris Lattner2e9fa6d2002-04-09 19:48:49 +00001194void Argument::print(std::ostream &o) const {
Chris Lattnere52ed452004-07-13 23:14:34 +00001195 WriteAsOperand(o, this, true, true,
1196 getParent() ? getParent()->getParent() : 0);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001197}
1198
Reid Spencer52641832004-05-25 18:14:38 +00001199// Value::dump - allow easy printing of Values from the debugger.
1200// Located here because so much of the needed functionality is here.
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001201void Value::dump() const { print(std::cerr); }
Reid Spencer52641832004-05-25 18:14:38 +00001202
1203// Type::dump - allow easy printing of Values from the debugger.
1204// Located here because so much of the needed functionality is here.
Reid Spencere7e96712004-05-25 08:53:40 +00001205void Type::dump() const { print(std::cerr); }
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001206
1207//===----------------------------------------------------------------------===//
1208// CachedWriter Class Implementation
1209//===----------------------------------------------------------------------===//
1210
Chris Lattner7db79582001-11-07 04:21:57 +00001211void CachedWriter::setModule(const Module *M) {
1212 delete SC; delete AW;
1213 if (M) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001214 SC = new SlotMachine(M );
Misha Brukman21bbdb92004-06-04 21:11:51 +00001215 AW = new AssemblyWriter(Out, *SC, M, 0);
Chris Lattner7db79582001-11-07 04:21:57 +00001216 } else {
1217 SC = 0; AW = 0;
1218 }
1219}
1220
1221CachedWriter::~CachedWriter() {
1222 delete AW;
1223 delete SC;
1224}
1225
Chris Lattner60a7dd12004-07-15 02:51:31 +00001226CachedWriter &CachedWriter::operator<<(const Value &V) {
Chris Lattner7db79582001-11-07 04:21:57 +00001227 assert(AW && SC && "CachedWriter does not have a current module!");
Chris Lattner60a7dd12004-07-15 02:51:31 +00001228 if (const Instruction *I = dyn_cast<Instruction>(&V))
Chris Lattner80d2d532004-06-26 19:40:40 +00001229 AW->write(I);
Chris Lattner60a7dd12004-07-15 02:51:31 +00001230 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(&V))
Chris Lattner80d2d532004-06-26 19:40:40 +00001231 AW->write(BB);
Chris Lattner60a7dd12004-07-15 02:51:31 +00001232 else if (const Function *F = dyn_cast<Function>(&V))
Chris Lattner80d2d532004-06-26 19:40:40 +00001233 AW->write(F);
Chris Lattner60a7dd12004-07-15 02:51:31 +00001234 else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(&V))
Chris Lattner80d2d532004-06-26 19:40:40 +00001235 AW->write(GV);
Chris Lattner80d2d532004-06-26 19:40:40 +00001236 else
Chris Lattner60a7dd12004-07-15 02:51:31 +00001237 AW->writeOperand(&V, true, true);
Chris Lattner7db79582001-11-07 04:21:57 +00001238 return *this;
1239}
Misha Brukman4685e262004-04-28 15:31:21 +00001240
Chris Lattner60a7dd12004-07-15 02:51:31 +00001241CachedWriter& CachedWriter::operator<<(const Type &Ty) {
Misha Brukman4685e262004-04-28 15:31:21 +00001242 if (SymbolicTypes) {
1243 const Module *M = AW->getModule();
Chris Lattner60a7dd12004-07-15 02:51:31 +00001244 if (M) WriteTypeSymbolic(Out, &Ty, M);
Reid Spencer58d30f22004-07-04 11:50:43 +00001245 } else {
Chris Lattner60a7dd12004-07-15 02:51:31 +00001246 AW->write(&Ty);
Reid Spencer58d30f22004-07-04 11:50:43 +00001247 }
1248 return *this;
Misha Brukman4685e262004-04-28 15:31:21 +00001249}
Misha Brukmanda546ea2004-04-28 19:24:28 +00001250
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001251//===----------------------------------------------------------------------===//
1252//===-- SlotMachine Implementation
1253//===----------------------------------------------------------------------===//
1254
1255#if 0
1256#define SC_DEBUG(X) std::cerr << X
1257#else
1258#define SC_DEBUG(X)
1259#endif
1260
1261// Module level constructor. Causes the contents of the Module (sans functions)
1262// to be added to the slot table.
1263SlotMachine::SlotMachine(const Module *M)
Reid Spencer56010e42004-05-26 21:56:09 +00001264 : TheModule(M) ///< Saved for lazy initialization.
1265 , TheFunction(0)
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001266 , mMap()
Reid Spencer58d30f22004-07-04 11:50:43 +00001267 , mTypes()
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001268 , fMap()
Reid Spencer58d30f22004-07-04 11:50:43 +00001269 , fTypes()
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001270{
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001271}
1272
1273// Function level constructor. Causes the contents of the Module and the one
1274// function provided to be added to the slot table.
1275SlotMachine::SlotMachine(const Function *F )
Reid Spencer56010e42004-05-26 21:56:09 +00001276 : TheModule( F ? F->getParent() : 0 ) ///< Saved for lazy initialization
1277 , TheFunction(F) ///< Saved for lazy initialization
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001278 , mMap()
Reid Spencer58d30f22004-07-04 11:50:43 +00001279 , mTypes()
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001280 , fMap()
Reid Spencer58d30f22004-07-04 11:50:43 +00001281 , fTypes()
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001282{
Reid Spencer56010e42004-05-26 21:56:09 +00001283}
1284
1285inline void SlotMachine::initialize(void) {
1286 if ( TheModule) {
1287 processModule();
1288 TheModule = 0; ///< Prevent re-processing next time we're called.
1289 }
1290 if ( TheFunction ) {
1291 processFunction();
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001292 }
1293}
1294
1295// Iterate through all the global variables, functions, and global
1296// variable initializers and create slots for them.
1297void SlotMachine::processModule() {
1298 SC_DEBUG("begin processModule!\n");
1299
1300 // Add all of the global variables to the value table...
1301 for (Module::const_giterator I = TheModule->gbegin(), E = TheModule->gend();
1302 I != E; ++I)
1303 createSlot(I);
1304
1305 // Add all the functions to the table
1306 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
1307 I != E; ++I)
1308 createSlot(I);
1309
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001310 SC_DEBUG("end processModule!\n");
1311}
1312
1313
Reid Spencer56010e42004-05-26 21:56:09 +00001314// Process the arguments, basic blocks, and instructions of a function.
1315void SlotMachine::processFunction() {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001316 SC_DEBUG("begin processFunction!\n");
1317
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001318 // Add all the function arguments
Reid Spencer56010e42004-05-26 21:56:09 +00001319 for(Function::const_aiterator AI = TheFunction->abegin(),
1320 AE = TheFunction->aend(); AI != AE; ++AI)
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001321 createSlot(AI);
1322
1323 SC_DEBUG("Inserting Instructions:\n");
1324
1325 // Add all of the basic blocks and instructions
Reid Spencer56010e42004-05-26 21:56:09 +00001326 for (Function::const_iterator BB = TheFunction->begin(),
1327 E = TheFunction->end(); BB != E; ++BB) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001328 createSlot(BB);
1329 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
1330 createSlot(I);
1331 }
1332 }
1333
1334 SC_DEBUG("end processFunction!\n");
1335}
1336
1337// Clean up after incorporating a function. This is the only way
Reid Spencer56010e42004-05-26 21:56:09 +00001338// to get out of the function incorporation state that affects the
1339// getSlot/createSlot lock. Function incorporation state is indicated
1340// by TheFunction != 0.
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001341void SlotMachine::purgeFunction() {
1342 SC_DEBUG("begin purgeFunction!\n");
1343 fMap.clear(); // Simply discard the function level map
Reid Spencer58d30f22004-07-04 11:50:43 +00001344 fTypes.clear();
Reid Spencer56010e42004-05-26 21:56:09 +00001345 TheFunction = 0;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001346 SC_DEBUG("end purgeFunction!\n");
1347}
1348
1349/// Get the slot number for a value. This function will assert if you
1350/// ask for a Value that hasn't previously been inserted with createSlot.
1351/// Types are forbidden because Type does not inherit from Value (any more).
Chris Lattner757ee0b2004-06-09 19:41:19 +00001352int SlotMachine::getSlot(const Value *V) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001353 assert( V && "Can't get slot for null Value" );
Reid Spencer56010e42004-05-26 21:56:09 +00001354 assert(!isa<Constant>(V) || isa<GlobalValue>(V) &&
1355 "Can't insert a non-GlobalValue Constant into SlotMachine");
1356
1357 // Check for uninitialized state and do lazy initialization
1358 this->initialize();
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001359
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001360 // Get the type of the value
1361 const Type* VTy = V->getType();
1362
1363 // Find the type plane in the module map
1364 TypedPlanes::const_iterator MI = mMap.find(VTy);
1365
Reid Spencer56010e42004-05-26 21:56:09 +00001366 if ( TheFunction ) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001367 // Lookup the type in the function map too
1368 TypedPlanes::const_iterator FI = fMap.find(VTy);
1369 // If there is a corresponding type plane in the function map
1370 if ( FI != fMap.end() ) {
1371 // Lookup the Value in the function map
1372 ValueMap::const_iterator FVI = FI->second.map.find(V);
1373 // If the value doesn't exist in the function map
1374 if ( FVI == FI->second.map.end() ) {
Chris Lattner68a038e2004-06-09 22:22:10 +00001375 // Look up the value in the module map.
1376 if (MI == mMap.end()) return -1;
Reid Spencer56010e42004-05-26 21:56:09 +00001377 ValueMap::const_iterator MVI = MI->second.map.find(V);
1378 // If we didn't find it, it wasn't inserted
Chris Lattner757ee0b2004-06-09 19:41:19 +00001379 if (MVI == MI->second.map.end()) return -1;
Reid Spencer56010e42004-05-26 21:56:09 +00001380 assert( MVI != MI->second.map.end() && "Value not found");
1381 // We found it only at the module level
1382 return MVI->second;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001383
1384 // else the value exists in the function map
1385 } else {
Reid Spencer56010e42004-05-26 21:56:09 +00001386 // Return the slot number as the module's contribution to
1387 // the type plane plus the index in the function's contribution
1388 // to the type plane.
Chris Lattnerb1f04782004-06-15 21:07:32 +00001389 if (MI != mMap.end())
1390 return MI->second.next_slot + FVI->second;
1391 else
1392 return FVI->second;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001393 }
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001394 }
1395 }
1396
Reid Spencer8beac692004-06-09 15:26:53 +00001397 // N.B. Can get here only if either !TheFunction or the function doesn't
1398 // have a corresponding type plane for the Value
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001399
1400 // Make sure the type plane exists
Chris Lattner757ee0b2004-06-09 19:41:19 +00001401 if (MI == mMap.end()) return -1;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001402 // Lookup the value in the module's map
1403 ValueMap::const_iterator MVI = MI->second.map.find(V);
1404 // Make sure we found it.
Chris Lattner757ee0b2004-06-09 19:41:19 +00001405 if (MVI == MI->second.map.end()) return -1;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001406 // Return it.
1407 return MVI->second;
1408}
1409
Reid Spencer58d30f22004-07-04 11:50:43 +00001410/// Get the slot number for a value. This function will assert if you
1411/// ask for a Value that hasn't previously been inserted with createSlot.
1412/// Types are forbidden because Type does not inherit from Value (any more).
1413int SlotMachine::getSlot(const Type *Ty) {
1414 assert( Ty && "Can't get slot for null Type" );
1415
1416 // Check for uninitialized state and do lazy initialization
1417 this->initialize();
1418
1419 if ( TheFunction ) {
1420 // Lookup the Type in the function map
1421 TypeMap::const_iterator FTI = fTypes.map.find(Ty);
1422 // If the Type doesn't exist in the function map
1423 if ( FTI == fTypes.map.end() ) {
1424 TypeMap::const_iterator MTI = mTypes.map.find(Ty);
1425 // If we didn't find it, it wasn't inserted
1426 if (MTI == mTypes.map.end())
1427 return -1;
1428 // We found it only at the module level
1429 return MTI->second;
1430
1431 // else the value exists in the function map
1432 } else {
1433 // Return the slot number as the module's contribution to
1434 // the type plane plus the index in the function's contribution
1435 // to the type plane.
1436 return mTypes.next_slot + FTI->second;
1437 }
1438 }
1439
1440 // N.B. Can get here only if either !TheFunction
1441
1442 // Lookup the value in the module's map
1443 TypeMap::const_iterator MTI = mTypes.map.find(Ty);
1444 // Make sure we found it.
1445 if (MTI == mTypes.map.end()) return -1;
1446 // Return it.
1447 return MTI->second;
1448}
1449
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001450// Create a new slot, or return the existing slot if it is already
1451// inserted. Note that the logic here parallels getSlot but instead
1452// of asserting when the Value* isn't found, it inserts the value.
1453unsigned SlotMachine::createSlot(const Value *V) {
1454 assert( V && "Can't insert a null Value to SlotMachine");
Reid Spencer56010e42004-05-26 21:56:09 +00001455 assert(!isa<Constant>(V) || isa<GlobalValue>(V) &&
1456 "Can't insert a non-GlobalValue Constant into SlotMachine");
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001457
1458 const Type* VTy = V->getType();
1459
1460 // Just ignore void typed things
1461 if (VTy == Type::VoidTy) return 0; // FIXME: Wrong return value!
1462
1463 // Look up the type plane for the Value's type from the module map
1464 TypedPlanes::const_iterator MI = mMap.find(VTy);
1465
Reid Spencer56010e42004-05-26 21:56:09 +00001466 if ( TheFunction ) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001467 // Get the type plane for the Value's type from the function map
1468 TypedPlanes::const_iterator FI = fMap.find(VTy);
1469 // If there is a corresponding type plane in the function map
1470 if ( FI != fMap.end() ) {
1471 // Lookup the Value in the function map
1472 ValueMap::const_iterator FVI = FI->second.map.find(V);
1473 // If the value doesn't exist in the function map
1474 if ( FVI == FI->second.map.end() ) {
Reid Spencer56010e42004-05-26 21:56:09 +00001475 // If there is no corresponding type plane in the module map
1476 if ( MI == mMap.end() )
1477 return insertValue(V);
1478 // Look up the value in the module map
1479 ValueMap::const_iterator MVI = MI->second.map.find(V);
1480 // If we didn't find it, it wasn't inserted
1481 if ( MVI == MI->second.map.end() )
1482 return insertValue(V);
1483 else
1484 // We found it only at the module level
1485 return MVI->second;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001486
1487 // else the value exists in the function map
1488 } else {
Reid Spencer56010e42004-05-26 21:56:09 +00001489 if ( MI == mMap.end() )
1490 return FVI->second;
1491 else
1492 // Return the slot number as the module's contribution to
1493 // the type plane plus the index in the function's contribution
1494 // to the type plane.
1495 return MI->second.next_slot + FVI->second;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001496 }
1497
1498 // else there is not a corresponding type plane in the function map
1499 } else {
1500 // If the type plane doesn't exists at the module level
1501 if ( MI == mMap.end() ) {
Reid Spencer56010e42004-05-26 21:56:09 +00001502 return insertValue(V);
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001503 // else type plane exists at the module level, examine it
1504 } else {
Reid Spencer56010e42004-05-26 21:56:09 +00001505 // Look up the value in the module's map
1506 ValueMap::const_iterator MVI = MI->second.map.find(V);
1507 // If we didn't find it there either
1508 if ( MVI == MI->second.map.end() )
1509 // Return the slot number as the module's contribution to
1510 // the type plane plus the index of the function map insertion.
1511 return MI->second.next_slot + insertValue(V);
1512 else
1513 return MVI->second;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001514 }
1515 }
1516 }
1517
Reid Spencer56010e42004-05-26 21:56:09 +00001518 // N.B. Can only get here if !TheFunction
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001519
1520 // If the module map's type plane is not for the Value's type
1521 if ( MI != mMap.end() ) {
1522 // Lookup the value in the module's map
1523 ValueMap::const_iterator MVI = MI->second.map.find(V);
1524 if ( MVI != MI->second.map.end() )
1525 return MVI->second;
1526 }
1527
1528 return insertValue(V);
1529}
1530
Reid Spencer58d30f22004-07-04 11:50:43 +00001531// Create a new slot, or return the existing slot if it is already
1532// inserted. Note that the logic here parallels getSlot but instead
1533// of asserting when the Value* isn't found, it inserts the value.
1534unsigned SlotMachine::createSlot(const Type *Ty) {
1535 assert( Ty && "Can't insert a null Type to SlotMachine");
1536
1537 if ( TheFunction ) {
1538 // Lookup the Type in the function map
1539 TypeMap::const_iterator FTI = fTypes.map.find(Ty);
1540 // If the type doesn't exist in the function map
1541 if ( FTI == fTypes.map.end() ) {
1542 // Look up the type in the module map
1543 TypeMap::const_iterator MTI = mTypes.map.find(Ty);
1544 // If we didn't find it, it wasn't inserted
1545 if ( MTI == mTypes.map.end() )
1546 return insertValue(Ty);
1547 else
1548 // We found it only at the module level
1549 return MTI->second;
1550
1551 // else the value exists in the function map
1552 } else {
1553 // Return the slot number as the module's contribution to
1554 // the type plane plus the index in the function's contribution
1555 // to the type plane.
1556 return mTypes.next_slot + FTI->second;
1557 }
1558 }
1559
1560 // N.B. Can only get here if !TheFunction
1561
1562 // Lookup the type in the module's map
1563 TypeMap::const_iterator MTI = mTypes.map.find(Ty);
1564 if ( MTI != mTypes.map.end() )
1565 return MTI->second;
1566
1567 return insertValue(Ty);
1568}
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001569
1570// Low level insert function. Minimal checking is done. This
1571// function is just for the convenience of createSlot (above).
1572unsigned SlotMachine::insertValue(const Value *V ) {
1573 assert(V && "Can't insert a null Value into SlotMachine!");
Reid Spencer56010e42004-05-26 21:56:09 +00001574 assert(!isa<Constant>(V) || isa<GlobalValue>(V) &&
1575 "Can't insert a non-GlobalValue Constant into SlotMachine");
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001576
Reid Spencer56010e42004-05-26 21:56:09 +00001577 // If this value does not contribute to a plane (is void)
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001578 // or if the value already has a name then ignore it.
Reid Spencer56010e42004-05-26 21:56:09 +00001579 if (V->getType() == Type::VoidTy || V->hasName() ) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001580 SC_DEBUG("ignored value " << *V << "\n");
1581 return 0; // FIXME: Wrong return value
1582 }
1583
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001584 const Type *VTy = V->getType();
1585 unsigned DestSlot = 0;
1586
Reid Spencer56010e42004-05-26 21:56:09 +00001587 if ( TheFunction ) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001588 TypedPlanes::iterator I = fMap.find( VTy );
1589 if ( I == fMap.end() )
Reid Spencer58d30f22004-07-04 11:50:43 +00001590 I = fMap.insert(std::make_pair(VTy,ValuePlane())).first;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001591 DestSlot = I->second.map[V] = I->second.next_slot++;
1592 } else {
1593 TypedPlanes::iterator I = mMap.find( VTy );
1594 if ( I == mMap.end() )
Reid Spencer58d30f22004-07-04 11:50:43 +00001595 I = mMap.insert(std::make_pair(VTy,ValuePlane())).first;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001596 DestSlot = I->second.map[V] = I->second.next_slot++;
1597 }
1598
1599 SC_DEBUG(" Inserting value [" << VTy << "] = " << V << " slot=" <<
Reid Spencer56010e42004-05-26 21:56:09 +00001600 DestSlot << " [");
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001601 // G = Global, C = Constant, T = Type, F = Function, o = other
Reid Spenceraccd7c72004-07-17 23:47:01 +00001602 SC_DEBUG((isa<GlobalVariable>(V) ? 'G' : (isa<Function>(V) ? 'F' :
1603 (isa<Constant>(V) ? 'C' : 'o'))));
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001604 SC_DEBUG("]\n");
1605 return DestSlot;
1606}
1607
Reid Spencer58d30f22004-07-04 11:50:43 +00001608// Low level insert function. Minimal checking is done. This
1609// function is just for the convenience of createSlot (above).
1610unsigned SlotMachine::insertValue(const Type *Ty ) {
1611 assert(Ty && "Can't insert a null Type into SlotMachine!");
1612
1613 unsigned DestSlot = 0;
1614
1615 if ( TheFunction ) {
1616 DestSlot = fTypes.map[Ty] = fTypes.next_slot++;
1617 } else {
1618 DestSlot = fTypes.map[Ty] = fTypes.next_slot++;
1619 }
1620 SC_DEBUG(" Inserting type [" << DestSlot << "] = " << Ty << "\n");
1621 return DestSlot;
1622}
1623
Reid Spencere7e96712004-05-25 08:53:40 +00001624// vim: sw=2