blob: aec653e414963bfebb5a0d79e89a522011f3251d [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"
Chris Lattner2f7c9632001-06-06 20:29:01 +000024#include "llvm/iMemory.h"
Chris Lattner862e3382001-10-13 06:42:36 +000025#include "llvm/iTerminators.h"
Chris Lattnerfb5ae022001-12-03 18:02:31 +000026#include "llvm/iPHINode.h"
27#include "llvm/iOther.h"
Chris Lattnerc70b3f62004-01-20 19:50:34 +000028#include "llvm/Module.h"
Chris Lattnerfee714f2001-09-07 16:36:04 +000029#include "llvm/SymbolTable.h"
Misha Brukman4685e262004-04-28 15:31:21 +000030#include "llvm/Assembly/Writer.h"
Chris Lattner58185f22002-10-02 19:38:55 +000031#include "llvm/Support/CFG.h"
Chris Lattner5de22042001-11-27 00:03:19 +000032#include "Support/StringExtras.h"
33#include "Support/STLExtras.h"
Chris Lattnerfee714f2001-09-07 16:36:04 +000034#include <algorithm>
Chris Lattner189d19f2003-11-21 20:23:48 +000035using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000036
Reid Spencer16f2f7f2004-05-26 07:18:52 +000037namespace {
38
39/// This class provides computation of slot numbers for LLVM Assembly writing.
40/// @brief LLVM Assembly Writing Slot Computation.
41class SlotMachine {
42
43/// @name Types
44/// @{
45public:
46
47 /// @brief A mapping of Values to slot numbers
48 typedef std::map<const Value*, unsigned> ValueMap;
49
50 /// @brief A plane with next slot number and ValueMap
51 struct Plane {
52 unsigned next_slot; ///< The next slot number to use
53 ValueMap map; ///< The map of Value* -> unsigned
54 Plane() { next_slot = 0; } ///< Make sure we start at 0
55 };
56
57 /// @brief The map of planes by Type
58 typedef std::map<const Type*, Plane> TypedPlanes;
59
60/// @}
61/// @name Constructors
62/// @{
63public:
64 /// @brief Construct from a module
65 SlotMachine(const Module *M );
66
67 /// @brief Construct from a function, starting out in incorp state.
68 SlotMachine(const Function *F );
69
70/// @}
71/// @name Accessors
72/// @{
73public:
74 /// Return the slot number of the specified value in it's type
75 /// plane. Its an error to ask for something not in the SlotMachine.
76 /// Its an error to ask for a Type*
Reid Spencer56010e42004-05-26 21:56:09 +000077 unsigned getSlot(const Value *V) ;
Reid Spencer16f2f7f2004-05-26 07:18:52 +000078
79/// @}
80/// @name Mutators
81/// @{
82public:
83 /// If you'd like to deal with a function instead of just a module, use
84 /// this method to get its data into the SlotMachine.
Reid Spencer56010e42004-05-26 21:56:09 +000085 void incorporateFunction(const Function *F) { TheFunction = F; }
Reid Spencer16f2f7f2004-05-26 07:18:52 +000086
87 /// After calling incorporateFunction, use this method to remove the
88 /// most recently incorporated function from the SlotMachine. This
89 /// will reset the state of the machine back to just the module contents.
90 void purgeFunction();
91
92/// @}
93/// @name Implementation Details
94/// @{
95private:
Reid Spencer56010e42004-05-26 21:56:09 +000096 /// This function does the actual initialization.
97 inline void initialize();
98
Reid Spencer16f2f7f2004-05-26 07:18:52 +000099 /// Values can be crammed into here at will. If they haven't
100 /// been inserted already, they get inserted, otherwise they are ignored.
101 /// Either way, the slot number for the Value* is returned.
102 unsigned createSlot(const Value *V);
103
104 /// Insert a value into the value table. Return the slot number
105 /// that it now occupies. BadThings(TM) will happen if you insert a
106 /// Value that's already been inserted.
107 unsigned insertValue( const Value *V );
108
109 /// Add all of the module level global variables (and their initializers)
110 /// and function declarations, but not the contents of those functions.
111 void processModule();
112
Reid Spencer56010e42004-05-26 21:56:09 +0000113 /// Add all of the functions arguments, basic blocks, and instructions
114 void processFunction();
115
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000116 SlotMachine(const SlotMachine &); // DO NOT IMPLEMENT
117 void operator=(const SlotMachine &); // DO NOT IMPLEMENT
118
119/// @}
120/// @name Data
121/// @{
122public:
123
124 /// @brief The module for which we are holding slot numbers
Reid Spencer56010e42004-05-26 21:56:09 +0000125 const Module* TheModule;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000126
Reid Spencer56010e42004-05-26 21:56:09 +0000127 /// @brief The function for which we are holding slot numbers
128 const Function* TheFunction;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000129
130 /// @brief The TypePlanes map for the module level data
131 TypedPlanes mMap;
132
133 /// @brief The TypePlanes map for the function level data
134 TypedPlanes fMap;
135
136/// @}
137
138};
139
140}
141
Chris Lattnerc8b70922002-07-26 21:12:46 +0000142static RegisterPass<PrintModulePass>
143X("printm", "Print module to stderr",PassInfo::Analysis|PassInfo::Optimization);
144static RegisterPass<PrintFunctionPass>
145Y("print","Print function to stderr",PassInfo::Analysis|PassInfo::Optimization);
Chris Lattner7f8845a2002-07-23 18:07:49 +0000146
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000147static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
148 bool PrintName,
149 std::map<const Type *, std::string> &TypeTable,
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000150 SlotMachine *Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000151
Chris Lattnerb86620e2001-10-29 16:37:48 +0000152static const Module *getModuleFromVal(const Value *V) {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000153 if (const Argument *MA = dyn_cast<Argument>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +0000154 return MA->getParent() ? MA->getParent()->getParent() : 0;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000155 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +0000156 return BB->getParent() ? BB->getParent()->getParent() : 0;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000157 else if (const Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner57698e22002-03-26 18:01:55 +0000158 const Function *M = I->getParent() ? I->getParent()->getParent() : 0;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000159 return M ? M->getParent() : 0;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000160 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +0000161 return GV->getParent();
Chris Lattnerb86620e2001-10-29 16:37:48 +0000162 return 0;
163}
164
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000165static SlotMachine *createSlotMachine(const Value *V) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000166 assert(!isa<Type>(V) && "Can't create an SC for a type!");
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000167 if (const Argument *FA = dyn_cast<Argument>(V)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000168 return new SlotMachine(FA->getParent());
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000169 } else if (const Instruction *I = dyn_cast<Instruction>(V)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000170 return new SlotMachine(I->getParent()->getParent());
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000171 } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000172 return new SlotMachine(BB->getParent());
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000173 } else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)){
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000174 return new SlotMachine(GV->getParent());
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000175 } else if (const Function *Func = dyn_cast<Function>(V)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000176 return new SlotMachine(Func);
Chris Lattner7bfee412001-10-29 16:05:51 +0000177 }
178 return 0;
179}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000180
Chris Lattner1c343042003-08-22 05:40:38 +0000181// getLLVMName - Turn the specified string into an 'LLVM name', which is either
182// prefixed with % (if the string only contains simple characters) or is
183// surrounded with ""'s (if it has special chars in it).
184static std::string getLLVMName(const std::string &Name) {
185 assert(!Name.empty() && "Cannot get empty name!");
186
187 // First character cannot start with a number...
188 if (Name[0] >= '0' && Name[0] <= '9')
189 return "\"" + Name + "\"";
190
191 // Scan to see if we have any characters that are not on the "white list"
192 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
193 char C = Name[i];
194 assert(C != '"' && "Illegal character in LLVM value name!");
195 if ((C < 'a' || C > 'z') && (C < 'A' || C > 'Z') && (C < '0' || C > '9') &&
196 C != '-' && C != '.' && C != '_')
197 return "\"" + Name + "\"";
198 }
199
200 // If we get here, then the identifier is legal to use as a "VarID".
201 return "%"+Name;
202}
203
Chris Lattnerb86620e2001-10-29 16:37:48 +0000204
Misha Brukmanc566ca362004-03-02 00:22:19 +0000205/// fillTypeNameTable - If the module has a symbol table, take all global types
206/// and stuff their names into the TypeNames map.
207///
Chris Lattnerb86620e2001-10-29 16:37:48 +0000208static void fillTypeNameTable(const Module *M,
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000209 std::map<const Type *, std::string> &TypeNames) {
Chris Lattner98cf1f52002-11-20 18:36:02 +0000210 if (!M) return;
211 const SymbolTable &ST = M->getSymbolTable();
Reid Spencere7e96712004-05-25 08:53:40 +0000212 SymbolTable::type_const_iterator TI = ST.type_begin();
213 for (; TI != ST.type_end(); ++TI ) {
214 // As a heuristic, don't insert pointer to primitive types, because
215 // they are used too often to have a single useful name.
216 //
217 const Type *Ty = cast<Type>(TI->second);
218 if (!isa<PointerType>(Ty) ||
Reid Spencer56010e42004-05-26 21:56:09 +0000219 !cast<PointerType>(Ty)->getElementType()->isPrimitiveType() ||
220 isa<OpaqueType>(cast<PointerType>(Ty)->getElementType()))
Reid Spencere7e96712004-05-25 08:53:40 +0000221 TypeNames.insert(std::make_pair(Ty, getLLVMName(TI->first)));
Chris Lattnerb86620e2001-10-29 16:37:48 +0000222 }
223}
224
225
226
John Criswellcd116ba2004-06-01 14:54:08 +0000227static void calcTypeName(const Type *Ty,
228 std::vector<const Type *> &TypeStack,
229 std::map<const Type *, std::string> &TypeNames,
230 std::string & Result){
231 if (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty)) {
232 Result += Ty->getDescription(); // Base case
233 return;
234 }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000235
236 // Check to see if the type is named.
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000237 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
John Criswellcd116ba2004-06-01 14:54:08 +0000238 if (I != TypeNames.end()) {
239 Result += I->second;
240 return;
241 }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000242
John Criswellcd116ba2004-06-01 14:54:08 +0000243 if (isa<OpaqueType>(Ty)) {
244 Result += "opaque";
245 return;
246 }
Chris Lattnerf14ead92003-10-30 00:22:33 +0000247
Chris Lattnerb86620e2001-10-29 16:37:48 +0000248 // Check to see if the Type is already on the stack...
249 unsigned Slot = 0, CurSize = TypeStack.size();
250 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
251
252 // This is another base case for the recursion. In this case, we know
253 // that we have looped back to a type that we have previously visited.
254 // Generate the appropriate upreference to handle this.
John Criswellcd116ba2004-06-01 14:54:08 +0000255 if (Slot < CurSize) {
256 Result += "\\" + utostr(CurSize-Slot); // Here's the upreference
257 return;
258 }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000259
260 TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
261
Chris Lattnerb86620e2001-10-29 16:37:48 +0000262 switch (Ty->getPrimitiveID()) {
Chris Lattner91db5822002-03-29 03:44:36 +0000263 case Type::FunctionTyID: {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000264 const FunctionType *FTy = cast<FunctionType>(Ty);
John Criswellcd116ba2004-06-01 14:54:08 +0000265 calcTypeName(FTy->getReturnType(), TypeStack, TypeNames, Result);
266 Result += " (";
Chris Lattnerfa829be2004-02-09 04:14:01 +0000267 for (FunctionType::param_iterator I = FTy->param_begin(),
268 E = FTy->param_end(); I != E; ++I) {
269 if (I != FTy->param_begin())
Chris Lattnerb86620e2001-10-29 16:37:48 +0000270 Result += ", ";
John Criswellcd116ba2004-06-01 14:54:08 +0000271 calcTypeName(*I, TypeStack, TypeNames, Result);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000272 }
Chris Lattnerd816b532002-04-13 20:53:41 +0000273 if (FTy->isVarArg()) {
Chris Lattnerfa829be2004-02-09 04:14:01 +0000274 if (FTy->getNumParams()) Result += ", ";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000275 Result += "...";
276 }
277 Result += ")";
278 break;
279 }
280 case Type::StructTyID: {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000281 const StructType *STy = cast<StructType>(Ty);
John Criswellcd116ba2004-06-01 14:54:08 +0000282 Result += "{ ";
Chris Lattnerac6db752004-02-09 04:37:31 +0000283 for (StructType::element_iterator I = STy->element_begin(),
284 E = STy->element_end(); I != E; ++I) {
285 if (I != STy->element_begin())
Chris Lattnerb86620e2001-10-29 16:37:48 +0000286 Result += ", ";
John Criswellcd116ba2004-06-01 14:54:08 +0000287 calcTypeName(*I, TypeStack, TypeNames, Result);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000288 }
289 Result += " }";
290 break;
291 }
292 case Type::PointerTyID:
John Criswellcd116ba2004-06-01 14:54:08 +0000293 calcTypeName(cast<PointerType>(Ty)->getElementType(),
294 TypeStack, TypeNames, Result);
295 Result += "*";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000296 break;
297 case Type::ArrayTyID: {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000298 const ArrayType *ATy = cast<ArrayType>(Ty);
John Criswellcd116ba2004-06-01 14:54:08 +0000299 Result += "[" + utostr(ATy->getNumElements()) + " x ";
300 calcTypeName(ATy->getElementType(), TypeStack, TypeNames, Result);
301 Result += "]";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000302 break;
303 }
Chris Lattner15285ab2003-05-14 17:50:47 +0000304 case Type::OpaqueTyID:
John Criswellcd116ba2004-06-01 14:54:08 +0000305 Result += "opaque";
Chris Lattner15285ab2003-05-14 17:50:47 +0000306 break;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000307 default:
John Criswellcd116ba2004-06-01 14:54:08 +0000308 Result += "<unrecognized-type>";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000309 }
310
311 TypeStack.pop_back(); // Remove self from stack...
John Criswellcd116ba2004-06-01 14:54:08 +0000312 return;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000313}
314
315
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000316/// printTypeInt - The internal guts of printing out a type that has a
317/// potentially named portion.
318///
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000319static std::ostream &printTypeInt(std::ostream &Out, const Type *Ty,
320 std::map<const Type *, std::string> &TypeNames) {
Chris Lattnerb86620e2001-10-29 16:37:48 +0000321 // Primitive types always print out their description, regardless of whether
322 // they have been named or not.
323 //
Chris Lattner92d60532003-10-30 00:12:51 +0000324 if (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty))
325 return Out << Ty->getDescription();
Chris Lattnerb86620e2001-10-29 16:37:48 +0000326
327 // Check to see if the type is named.
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000328 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000329 if (I != TypeNames.end()) return Out << I->second;
330
331 // Otherwise we have a type that has not been named but is a derived type.
332 // Carefully recurse the type hierarchy to print out any contained symbolic
333 // names.
334 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000335 std::vector<const Type *> TypeStack;
John Criswellcd116ba2004-06-01 14:54:08 +0000336 std::string TypeName;
337 calcTypeName(Ty, TypeStack, TypeNames, TypeName);
Chris Lattner7f74a562002-01-20 22:54:45 +0000338 TypeNames.insert(std::make_pair(Ty, TypeName));//Cache type name for later use
John Criswellcd116ba2004-06-01 14:54:08 +0000339 return (Out << TypeName);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000340}
341
Chris Lattner34b95182001-10-31 04:33:19 +0000342
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000343/// WriteTypeSymbolic - This attempts to write the specified type as a symbolic
344/// type, iff there is an entry in the modules symbol table for the specified
345/// type or one of it's component types. This is slower than a simple x << Type
346///
Chris Lattner189d19f2003-11-21 20:23:48 +0000347std::ostream &llvm::WriteTypeSymbolic(std::ostream &Out, const Type *Ty,
348 const Module *M) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000349 Out << ' ';
Chris Lattnerb86620e2001-10-29 16:37:48 +0000350
351 // If they want us to print out a type, attempt to make it symbolic if there
352 // is a symbol table in the module...
Chris Lattner98cf1f52002-11-20 18:36:02 +0000353 if (M) {
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000354 std::map<const Type *, std::string> TypeNames;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000355 fillTypeNameTable(M, TypeNames);
356
Chris Lattner72f866e2001-10-29 16:40:32 +0000357 return printTypeInt(Out, Ty, TypeNames);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000358 } else {
Chris Lattner72f866e2001-10-29 16:40:32 +0000359 return Out << Ty->getDescription();
Chris Lattnerb86620e2001-10-29 16:37:48 +0000360 }
361}
362
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000363static void WriteConstantInt(std::ostream &Out, const Constant *CV,
364 bool PrintName,
365 std::map<const Type *, std::string> &TypeTable,
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000366 SlotMachine *Machine) {
Chris Lattner1e194682002-04-18 18:53:13 +0000367 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
368 Out << (CB == ConstantBool::True ? "true" : "false");
369 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV)) {
370 Out << CI->getValue();
371 } else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV)) {
372 Out << CI->getValue();
373 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
374 // We would like to output the FP constant value in exponential notation,
375 // but we cannot do this if doing so will lose precision. Check here to
376 // make sure that we only output it in exponential format if we can parse
377 // the value back and get the same value.
378 //
379 std::string StrVal = ftostr(CFP->getValue());
380
381 // Check to make sure that the stringized number is not some string like
382 // "Inf" or NaN, that atof will accept, but the lexer will not. Check that
383 // the string matches the "[-+]?[0-9]" regex.
384 //
385 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
386 ((StrVal[0] == '-' || StrVal[0] == '+') &&
Brian Gaeke87b4f072003-06-17 23:55:35 +0000387 (StrVal[1] >= '0' && StrVal[1] <= '9')))
Chris Lattner1e194682002-04-18 18:53:13 +0000388 // Reparse stringized version!
389 if (atof(StrVal.c_str()) == CFP->getValue()) {
390 Out << StrVal; return;
391 }
392
393 // Otherwise we could not reparse it to exactly the same value, so we must
394 // output the string in hexadecimal format!
395 //
396 // Behave nicely in the face of C TBAA rules... see:
397 // http://www.nullstone.com/htmls/category/aliastyp.htm
398 //
399 double Val = CFP->getValue();
400 char *Ptr = (char*)&Val;
401 assert(sizeof(double) == sizeof(uint64_t) && sizeof(double) == 8 &&
402 "assuming that double is 64 bits!");
403 Out << "0x" << utohexstr(*(uint64_t*)Ptr);
404
Chris Lattner76b2ff42004-02-15 05:55:15 +0000405 } else if (isa<ConstantAggregateZero>(CV)) {
406 Out << "zeroinitializer";
Chris Lattner1e194682002-04-18 18:53:13 +0000407 } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
408 // As a special case, print the array as a string if it is an array of
409 // ubytes or an array of sbytes with positive values.
410 //
411 const Type *ETy = CA->getType()->getElementType();
412 bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
413
414 if (ETy == Type::SByteTy)
415 for (unsigned i = 0; i < CA->getNumOperands(); ++i)
416 if (cast<ConstantSInt>(CA->getOperand(i))->getValue() < 0) {
417 isString = false;
418 break;
419 }
420
421 if (isString) {
422 Out << "c\"";
423 for (unsigned i = 0; i < CA->getNumOperands(); ++i) {
Chris Lattner6077c312003-07-23 15:22:26 +0000424 unsigned char C = cast<ConstantInt>(CA->getOperand(i))->getRawValue();
Chris Lattner1e194682002-04-18 18:53:13 +0000425
Chris Lattnere5fd3862002-07-31 23:56:44 +0000426 if (isprint(C) && C != '"' && C != '\\') {
Chris Lattner1e194682002-04-18 18:53:13 +0000427 Out << C;
428 } else {
429 Out << '\\'
430 << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'))
431 << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
432 }
433 }
434 Out << "\"";
435
436 } else { // Cannot output in string format...
Misha Brukman21bbdb92004-06-04 21:11:51 +0000437 Out << '[';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000438 if (CA->getNumOperands()) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000439 Out << ' ';
Chris Lattner1e194682002-04-18 18:53:13 +0000440 printTypeInt(Out, ETy, TypeTable);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000441 WriteAsOperandInternal(Out, CA->getOperand(0),
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000442 PrintName, TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000443 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
444 Out << ", ";
Chris Lattner1e194682002-04-18 18:53:13 +0000445 printTypeInt(Out, ETy, TypeTable);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000446 WriteAsOperandInternal(Out, CA->getOperand(i), PrintName,
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000447 TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000448 }
449 }
450 Out << " ]";
451 }
452 } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000453 Out << '{';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000454 if (CS->getNumOperands()) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000455 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000456 printTypeInt(Out, CS->getOperand(0)->getType(), TypeTable);
457
458 WriteAsOperandInternal(Out, CS->getOperand(0),
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000459 PrintName, TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000460
461 for (unsigned i = 1; i < CS->getNumOperands(); i++) {
462 Out << ", ";
463 printTypeInt(Out, CS->getOperand(i)->getType(), TypeTable);
464
465 WriteAsOperandInternal(Out, CS->getOperand(i),
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000466 PrintName, TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000467 }
468 }
469
470 Out << " }";
471 } else if (isa<ConstantPointerNull>(CV)) {
472 Out << "null";
473
Chris Lattner113f4f42002-06-25 16:13:24 +0000474 } else if (const ConstantPointerRef *PR = dyn_cast<ConstantPointerRef>(CV)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000475 WriteAsOperandInternal(Out, PR->getValue(), true, TypeTable, Machine);
Vikram S. Adveb952b542002-07-14 23:14:45 +0000476
Chris Lattner3cd8c562002-07-30 18:54:25 +0000477 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Chris Lattnerb8257222003-03-06 23:23:32 +0000478 Out << CE->getOpcodeName() << " (";
Vikram S. Adveb952b542002-07-14 23:14:45 +0000479
480 for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
481 printTypeInt(Out, (*OI)->getType(), TypeTable);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000482 WriteAsOperandInternal(Out, *OI, PrintName, TypeTable, Machine);
Vikram S. Adveb952b542002-07-14 23:14:45 +0000483 if (OI+1 != CE->op_end())
Chris Lattner3cd8c562002-07-30 18:54:25 +0000484 Out << ", ";
Vikram S. Adveb952b542002-07-14 23:14:45 +0000485 }
486
Chris Lattnercfe8f532002-08-16 21:17:11 +0000487 if (CE->getOpcode() == Instruction::Cast) {
Chris Lattner83b396b2002-08-15 19:37:43 +0000488 Out << " to ";
489 printTypeInt(Out, CE->getType(), TypeTable);
490 }
Misha Brukman21bbdb92004-06-04 21:11:51 +0000491 Out << ')';
Chris Lattner83b396b2002-08-15 19:37:43 +0000492
Chris Lattnerd84bb632002-04-16 21:36:08 +0000493 } else {
Vikram S. Adveb952b542002-07-14 23:14:45 +0000494 Out << "<placeholder or erroneous Constant>";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000495 }
496}
497
498
Misha Brukmanc566ca362004-03-02 00:22:19 +0000499/// WriteAsOperand - Write the name of the specified value out to the specified
500/// ostream. This can be useful when you just want to print int %reg126, not
501/// the whole instruction that generated it.
502///
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000503static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
504 bool PrintName,
505 std::map<const Type*, std::string> &TypeTable,
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000506 SlotMachine *Machine) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000507 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000508 if (PrintName && V->hasName()) {
Chris Lattner1c343042003-08-22 05:40:38 +0000509 Out << getLLVMName(V->getName());
Chris Lattnerd84bb632002-04-16 21:36:08 +0000510 } else {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000511 if (const Constant *CV = dyn_cast<Constant>(V)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000512 WriteConstantInt(Out, CV, PrintName, TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000513 } else {
514 int Slot;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000515 if (Machine) {
Reid Spencer56010e42004-05-26 21:56:09 +0000516 Slot = Machine->getSlot(V);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000517 } else {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000518 if (const Type *Ty = dyn_cast<Type>(V)) {
Chris Lattnerd84bb632002-04-16 21:36:08 +0000519 Out << Ty->getDescription();
520 return;
521 }
522
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000523 Machine = createSlotMachine(V);
524 if (Machine == 0) { Out << "BAD VALUE TYPE!"; return; }
Chris Lattnerd84bb632002-04-16 21:36:08 +0000525
Reid Spencer56010e42004-05-26 21:56:09 +0000526 Slot = Machine->getSlot(V);
527 delete Machine;
Chris Lattnerd84bb632002-04-16 21:36:08 +0000528 }
Misha Brukman21bbdb92004-06-04 21:11:51 +0000529 Out << '%' << Slot;
Chris Lattnerd84bb632002-04-16 21:36:08 +0000530 }
531 }
532}
533
534
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000535/// WriteAsOperand - Write the name of the specified value out to the specified
536/// ostream. This can be useful when you just want to print int %reg126, not
537/// the whole instruction that generated it.
538///
Chris Lattner189d19f2003-11-21 20:23:48 +0000539std::ostream &llvm::WriteAsOperand(std::ostream &Out, const Value *V,
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000540 bool PrintType, bool PrintName,
541 const Module *Context) {
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000542 std::map<const Type *, std::string> TypeNames;
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000543 if (Context == 0) Context = getModuleFromVal(V);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000544
Chris Lattner98cf1f52002-11-20 18:36:02 +0000545 if (Context)
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000546 fillTypeNameTable(Context, TypeNames);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000547
548 if (PrintType)
549 printTypeInt(Out, V->getType(), TypeNames);
550
Brian Gaekefda1f182003-11-16 23:08:27 +0000551 if (const Type *Ty = dyn_cast<Type> (V))
552 printTypeInt(Out, Ty, TypeNames);
553
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000554 WriteAsOperandInternal(Out, V, PrintName, TypeNames, 0);
Chris Lattner5e5abe32001-07-20 19:15:21 +0000555 return Out;
556}
557
Chris Lattner189d19f2003-11-21 20:23:48 +0000558namespace llvm {
Chris Lattner2e9fee42001-07-12 23:35:26 +0000559
Chris Lattnerfee714f2001-09-07 16:36:04 +0000560class AssemblyWriter {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000561 std::ostream *Out;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000562 SlotMachine &Machine;
Chris Lattner7bfee412001-10-29 16:05:51 +0000563 const Module *TheModule;
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000564 std::map<const Type *, std::string> TypeNames;
Chris Lattner8339f7d2003-10-30 23:41:03 +0000565 AssemblyAnnotationWriter *AnnotationWriter;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000566public:
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000567 inline AssemblyWriter(std::ostream &o, SlotMachine &Mac, const Module *M,
Chris Lattner8339f7d2003-10-30 23:41:03 +0000568 AssemblyAnnotationWriter *AAW)
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000569 : Out(&o), Machine(Mac), TheModule(M), AnnotationWriter(AAW) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000570
571 // If the module has a symbol table, take all global types and stuff their
572 // names into the TypeNames map.
573 //
Chris Lattnerb86620e2001-10-29 16:37:48 +0000574 fillTypeNameTable(M, TypeNames);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000575 }
576
Chris Lattner7bfee412001-10-29 16:05:51 +0000577 inline void write(const Module *M) { printModule(M); }
578 inline void write(const GlobalVariable *G) { printGlobal(G); }
Chris Lattner57698e22002-03-26 18:01:55 +0000579 inline void write(const Function *F) { printFunction(F); }
Chris Lattner7bfee412001-10-29 16:05:51 +0000580 inline void write(const BasicBlock *BB) { printBasicBlock(BB); }
Chris Lattner113f4f42002-06-25 16:13:24 +0000581 inline void write(const Instruction *I) { printInstruction(*I); }
Chris Lattner3462ae32001-12-03 22:26:30 +0000582 inline void write(const Constant *CPV) { printConstant(CPV); }
Chris Lattner7db79582001-11-07 04:21:57 +0000583 inline void write(const Type *Ty) { printType(Ty); }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000584
Chris Lattner1e194682002-04-18 18:53:13 +0000585 void writeOperand(const Value *Op, bool PrintType, bool PrintName = true);
586
Misha Brukman4685e262004-04-28 15:31:21 +0000587 const Module* getModule() { return TheModule; }
Misha Brukmanda546ea2004-04-28 19:24:28 +0000588 void setStream(std::ostream &os) { Out = &os; }
Misha Brukman4685e262004-04-28 15:31:21 +0000589
Chris Lattner2f7c9632001-06-06 20:29:01 +0000590private :
Chris Lattner7bfee412001-10-29 16:05:51 +0000591 void printModule(const Module *M);
592 void printSymbolTable(const SymbolTable &ST);
Chris Lattner3462ae32001-12-03 22:26:30 +0000593 void printConstant(const Constant *CPV);
Chris Lattner7bfee412001-10-29 16:05:51 +0000594 void printGlobal(const GlobalVariable *GV);
Chris Lattner57698e22002-03-26 18:01:55 +0000595 void printFunction(const Function *F);
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000596 void printArgument(const Argument *FA);
Chris Lattner7bfee412001-10-29 16:05:51 +0000597 void printBasicBlock(const BasicBlock *BB);
Chris Lattner113f4f42002-06-25 16:13:24 +0000598 void printInstruction(const Instruction &I);
Chris Lattnerd816b532002-04-13 20:53:41 +0000599
600 // printType - Go to extreme measures to attempt to print out a short,
601 // symbolic version of a type name.
602 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000603 std::ostream &printType(const Type *Ty) {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000604 return printTypeInt(*Out, Ty, TypeNames);
Chris Lattnerd816b532002-04-13 20:53:41 +0000605 }
606
607 // printTypeAtLeastOneLevel - Print out one level of the possibly complex type
608 // without considering any symbolic types that we may have equal to it.
609 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000610 std::ostream &printTypeAtLeastOneLevel(const Type *Ty);
Chris Lattner7bfee412001-10-29 16:05:51 +0000611
Chris Lattner862e3382001-10-13 06:42:36 +0000612 // printInfoComment - Print a little comment after the instruction indicating
613 // which slot it occupies.
Chris Lattner113f4f42002-06-25 16:13:24 +0000614 void printInfoComment(const Value &V);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000615};
Reid Spencerf43ac622004-05-27 22:04:46 +0000616} // end of llvm namespace
Chris Lattner2f7c9632001-06-06 20:29:01 +0000617
Misha Brukmanc566ca362004-03-02 00:22:19 +0000618/// printTypeAtLeastOneLevel - Print out one level of the possibly complex type
619/// without considering any symbolic types that we may have equal to it.
620///
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000621std::ostream &AssemblyWriter::printTypeAtLeastOneLevel(const Type *Ty) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000622 if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000623 printType(FTy->getReturnType()) << " (";
Chris Lattnerfa829be2004-02-09 04:14:01 +0000624 for (FunctionType::param_iterator I = FTy->param_begin(),
625 E = FTy->param_end(); I != E; ++I) {
626 if (I != FTy->param_begin())
Misha Brukmanda546ea2004-04-28 19:24:28 +0000627 *Out << ", ";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000628 printType(*I);
Chris Lattnerd816b532002-04-13 20:53:41 +0000629 }
630 if (FTy->isVarArg()) {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000631 if (FTy->getNumParams()) *Out << ", ";
632 *Out << "...";
Chris Lattnerd816b532002-04-13 20:53:41 +0000633 }
Misha Brukman21bbdb92004-06-04 21:11:51 +0000634 *Out << ')';
Chris Lattner113f4f42002-06-25 16:13:24 +0000635 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000636 *Out << "{ ";
Chris Lattnerac6db752004-02-09 04:37:31 +0000637 for (StructType::element_iterator I = STy->element_begin(),
638 E = STy->element_end(); I != E; ++I) {
639 if (I != STy->element_begin())
Misha Brukmanda546ea2004-04-28 19:24:28 +0000640 *Out << ", ";
Chris Lattnerd816b532002-04-13 20:53:41 +0000641 printType(*I);
642 }
Misha Brukmanda546ea2004-04-28 19:24:28 +0000643 *Out << " }";
Chris Lattner113f4f42002-06-25 16:13:24 +0000644 } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000645 printType(PTy->getElementType()) << '*';
Chris Lattner113f4f42002-06-25 16:13:24 +0000646 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000647 *Out << '[' << ATy->getNumElements() << " x ";
648 printType(ATy->getElementType()) << ']';
Chris Lattner113f4f42002-06-25 16:13:24 +0000649 } else if (const OpaqueType *OTy = dyn_cast<OpaqueType>(Ty)) {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000650 *Out << "opaque";
Chris Lattnerd816b532002-04-13 20:53:41 +0000651 } else {
Vikram S. Adveb952b542002-07-14 23:14:45 +0000652 if (!Ty->isPrimitiveType())
Misha Brukmanda546ea2004-04-28 19:24:28 +0000653 *Out << "<unknown derived type>";
Chris Lattnerd816b532002-04-13 20:53:41 +0000654 printType(Ty);
655 }
Misha Brukmanda546ea2004-04-28 19:24:28 +0000656 return *Out;
Chris Lattnerd816b532002-04-13 20:53:41 +0000657}
658
659
Chris Lattnerfee714f2001-09-07 16:36:04 +0000660void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType,
Reid Spencer56010e42004-05-26 21:56:09 +0000661 bool PrintName) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000662 if (PrintType) { *Out << ' '; printType(Operand->getType()); }
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000663 WriteAsOperandInternal(*Out, Operand, PrintName, TypeNames, &Machine);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000664}
665
Chris Lattner2f7c9632001-06-06 20:29:01 +0000666
Chris Lattner7bfee412001-10-29 16:05:51 +0000667void AssemblyWriter::printModule(const Module *M) {
Chris Lattner8068e0c2003-08-24 13:48:48 +0000668 switch (M->getEndianness()) {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000669 case Module::LittleEndian: *Out << "target endian = little\n"; break;
670 case Module::BigEndian: *Out << "target endian = big\n"; break;
Chris Lattner8068e0c2003-08-24 13:48:48 +0000671 case Module::AnyEndianness: break;
672 }
673 switch (M->getPointerSize()) {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000674 case Module::Pointer32: *Out << "target pointersize = 32\n"; break;
675 case Module::Pointer64: *Out << "target pointersize = 64\n"; break;
Chris Lattner8068e0c2003-08-24 13:48:48 +0000676 case Module::AnyPointerSize: break;
677 }
678
Chris Lattnerfee714f2001-09-07 16:36:04 +0000679 // Loop over the symbol table, emitting all named constants...
Chris Lattner98cf1f52002-11-20 18:36:02 +0000680 printSymbolTable(M->getSymbolTable());
Chris Lattnerda975502001-09-10 07:58:01 +0000681
Chris Lattner113f4f42002-06-25 16:13:24 +0000682 for (Module::const_giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
683 printGlobal(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000684
Misha Brukmanda546ea2004-04-28 19:24:28 +0000685 *Out << "\nimplementation ; Functions:\n";
Vikram S. Adve13ba19a2001-09-18 12:48:16 +0000686
Chris Lattner6915f8f2002-04-07 22:49:37 +0000687 // Output all of the functions...
Chris Lattner113f4f42002-06-25 16:13:24 +0000688 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
689 printFunction(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000690}
691
Chris Lattner7bfee412001-10-29 16:05:51 +0000692void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000693 if (GV->hasName()) *Out << getLLVMName(GV->getName()) << " = ";
Chris Lattner37798642001-09-18 04:01:05 +0000694
Chris Lattner379a8d22003-04-16 20:28:45 +0000695 if (!GV->hasInitializer())
Misha Brukmanda546ea2004-04-28 19:24:28 +0000696 *Out << "external ";
Chris Lattner379a8d22003-04-16 20:28:45 +0000697 else
698 switch (GV->getLinkage()) {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000699 case GlobalValue::InternalLinkage: *Out << "internal "; break;
700 case GlobalValue::LinkOnceLinkage: *Out << "linkonce "; break;
701 case GlobalValue::WeakLinkage: *Out << "weak "; break;
702 case GlobalValue::AppendingLinkage: *Out << "appending "; break;
Chris Lattner379a8d22003-04-16 20:28:45 +0000703 case GlobalValue::ExternalLinkage: break;
704 }
Chris Lattner37798642001-09-18 04:01:05 +0000705
Misha Brukmanda546ea2004-04-28 19:24:28 +0000706 *Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattner2413b162001-12-04 00:03:30 +0000707 printType(GV->getType()->getElementType());
Chris Lattner37798642001-09-18 04:01:05 +0000708
709 if (GV->hasInitializer())
710 writeOperand(GV->getInitializer(), false, false);
711
Chris Lattner113f4f42002-06-25 16:13:24 +0000712 printInfoComment(*GV);
Misha Brukmanda546ea2004-04-28 19:24:28 +0000713 *Out << "\n";
Chris Lattnerda975502001-09-10 07:58:01 +0000714}
715
Chris Lattnerfee714f2001-09-07 16:36:04 +0000716
Reid Spencere7e96712004-05-25 08:53:40 +0000717// printSymbolTable - Run through symbol table looking for constants
718// and types. Emit their declarations.
Chris Lattner7bfee412001-10-29 16:05:51 +0000719void AssemblyWriter::printSymbolTable(const SymbolTable &ST) {
Reid Spencere7e96712004-05-25 08:53:40 +0000720
721 // Print the types.
722 for (SymbolTable::type_const_iterator TI = ST.type_begin();
723 TI != ST.type_end(); ++TI ) {
724 *Out << "\t" << getLLVMName(TI->first) << " = type ";
725
726 // Make sure we print out at least one level of the type structure, so
727 // that we do not get %FILE = type %FILE
728 //
729 printTypeAtLeastOneLevel(TI->second) << "\n";
730 }
Chris Lattnerfee714f2001-09-07 16:36:04 +0000731
Reid Spencere7e96712004-05-25 08:53:40 +0000732 // Print the constants, in type plane order.
733 for (SymbolTable::plane_const_iterator PI = ST.plane_begin();
734 PI != ST.plane_end(); ++PI ) {
735 SymbolTable::value_const_iterator VI = ST.value_begin(PI->first);
736 SymbolTable::value_const_iterator VE = ST.value_end(PI->first);
737
738 for (; VI != VE; ++VI) {
739 const Value *V = VI->second;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000740 if (const Constant *CPV = dyn_cast<Constant>(V)) {
Reid Spencer56010e42004-05-26 21:56:09 +0000741 printConstant(CPV);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000742 }
743 }
Chris Lattnera7620d92001-07-15 06:35:59 +0000744 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000745}
746
747
Misha Brukmanc566ca362004-03-02 00:22:19 +0000748/// printConstant - Print out a constant pool entry...
749///
Chris Lattner3462ae32001-12-03 22:26:30 +0000750void AssemblyWriter::printConstant(const Constant *CPV) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000751 // Don't print out unnamed constants, they will be inlined
752 if (!CPV->hasName()) return;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000753
Chris Lattneree998be2001-07-26 16:29:38 +0000754 // Print out name...
Misha Brukmanda546ea2004-04-28 19:24:28 +0000755 *Out << "\t" << getLLVMName(CPV->getName()) << " =";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000756
757 // Write the value out now...
Chris Lattnerd84bb632002-04-16 21:36:08 +0000758 writeOperand(CPV, true, false);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000759
Chris Lattner113f4f42002-06-25 16:13:24 +0000760 printInfoComment(*CPV);
Misha Brukmanda546ea2004-04-28 19:24:28 +0000761 *Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000762}
763
Misha Brukmanc566ca362004-03-02 00:22:19 +0000764/// printFunction - Print all aspects of a function.
765///
Chris Lattner113f4f42002-06-25 16:13:24 +0000766void AssemblyWriter::printFunction(const Function *F) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000767 // Print out the return type and name...
Misha Brukmanda546ea2004-04-28 19:24:28 +0000768 *Out << "\n";
Chris Lattner379a8d22003-04-16 20:28:45 +0000769
Misha Brukmanda546ea2004-04-28 19:24:28 +0000770 if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, *Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +0000771
Chris Lattner379a8d22003-04-16 20:28:45 +0000772 if (F->isExternal())
Misha Brukmanda546ea2004-04-28 19:24:28 +0000773 *Out << "declare ";
Chris Lattner379a8d22003-04-16 20:28:45 +0000774 else
775 switch (F->getLinkage()) {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000776 case GlobalValue::InternalLinkage: *Out << "internal "; break;
777 case GlobalValue::LinkOnceLinkage: *Out << "linkonce "; break;
778 case GlobalValue::WeakLinkage: *Out << "weak "; break;
779 case GlobalValue::AppendingLinkage: *Out << "appending "; break;
Chris Lattner379a8d22003-04-16 20:28:45 +0000780 case GlobalValue::ExternalLinkage: break;
781 }
782
Misha Brukman21bbdb92004-06-04 21:11:51 +0000783 printType(F->getReturnType()) << ' ';
Chris Lattner5b337482003-10-18 05:57:43 +0000784 if (!F->getName().empty())
Misha Brukmanda546ea2004-04-28 19:24:28 +0000785 *Out << getLLVMName(F->getName());
Chris Lattner5b337482003-10-18 05:57:43 +0000786 else
Misha Brukmanda546ea2004-04-28 19:24:28 +0000787 *Out << "\"\"";
Misha Brukman21bbdb92004-06-04 21:11:51 +0000788 *Out << '(';
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000789 Machine.incorporateFunction(F);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000790
Chris Lattner7bfee412001-10-29 16:05:51 +0000791 // Loop over the arguments, printing them...
Chris Lattner113f4f42002-06-25 16:13:24 +0000792 const FunctionType *FT = F->getFunctionType();
Chris Lattnerfee714f2001-09-07 16:36:04 +0000793
Chris Lattner149376d2002-10-13 20:57:00 +0000794 for(Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I)
795 printArgument(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000796
797 // Finish printing arguments...
Chris Lattner113f4f42002-06-25 16:13:24 +0000798 if (FT->isVarArg()) {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000799 if (FT->getNumParams()) *Out << ", ";
800 *Out << "..."; // Output varargs portion of signature!
Chris Lattnerfee714f2001-09-07 16:36:04 +0000801 }
Misha Brukman21bbdb92004-06-04 21:11:51 +0000802 *Out << ')';
Chris Lattnerfee714f2001-09-07 16:36:04 +0000803
Chris Lattner113f4f42002-06-25 16:13:24 +0000804 if (F->isExternal()) {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000805 *Out << "\n";
Chris Lattnerb2f02e52002-05-06 03:00:40 +0000806 } else {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000807 *Out << " {";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000808
Chris Lattner6915f8f2002-04-07 22:49:37 +0000809 // Output all of its basic blocks... for the function
Chris Lattner113f4f42002-06-25 16:13:24 +0000810 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
811 printBasicBlock(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000812
Misha Brukmanda546ea2004-04-28 19:24:28 +0000813 *Out << "}\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000814 }
815
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000816 Machine.purgeFunction();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000817}
818
Misha Brukmanc566ca362004-03-02 00:22:19 +0000819/// printArgument - This member is called for every argument that is passed into
820/// the function. Simply print it out
821///
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000822void AssemblyWriter::printArgument(const Argument *Arg) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000823 // Insert commas as we go... the first arg doesn't get a comma
Misha Brukmanda546ea2004-04-28 19:24:28 +0000824 if (Arg != &Arg->getParent()->afront()) *Out << ", ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000825
826 // Output type...
Chris Lattner7bfee412001-10-29 16:05:51 +0000827 printType(Arg->getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000828
829 // Output name, if available...
830 if (Arg->hasName())
Misha Brukman21bbdb92004-06-04 21:11:51 +0000831 *Out << ' ' << getLLVMName(Arg->getName());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000832}
833
Misha Brukmanc566ca362004-03-02 00:22:19 +0000834/// printBasicBlock - This member is called for each basic block in a method.
835///
Chris Lattner7bfee412001-10-29 16:05:51 +0000836void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000837 if (BB->hasName()) { // Print out the label if it exists...
Misha Brukman21bbdb92004-06-04 21:11:51 +0000838 *Out << "\n" << BB->getName() << ':';
Chris Lattner408dbdb2002-05-14 16:02:05 +0000839 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000840 *Out << "\n; <label>:" << Machine.getSlot(BB);
Chris Lattner58185f22002-10-02 19:38:55 +0000841 }
Chris Lattner2447ef52003-11-20 00:09:43 +0000842
843 if (BB->getParent() == 0)
Misha Brukmanda546ea2004-04-28 19:24:28 +0000844 *Out << "\t\t; Error: Block without parent!";
Chris Lattner2447ef52003-11-20 00:09:43 +0000845 else {
846 if (BB != &BB->getParent()->front()) { // Not the entry block?
847 // Output predecessors for the block...
Misha Brukmanda546ea2004-04-28 19:24:28 +0000848 *Out << "\t\t;";
Chris Lattner2447ef52003-11-20 00:09:43 +0000849 pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
850
851 if (PI == PE) {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000852 *Out << " No predecessors!";
Chris Lattner2447ef52003-11-20 00:09:43 +0000853 } else {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000854 *Out << " preds =";
Chris Lattner00211f12003-11-16 22:59:57 +0000855 writeOperand(*PI, false, true);
Chris Lattner2447ef52003-11-20 00:09:43 +0000856 for (++PI; PI != PE; ++PI) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000857 *Out << ',';
Chris Lattner2447ef52003-11-20 00:09:43 +0000858 writeOperand(*PI, false, true);
859 }
Chris Lattner00211f12003-11-16 22:59:57 +0000860 }
Chris Lattner58185f22002-10-02 19:38:55 +0000861 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000862 }
Chris Lattner408dbdb2002-05-14 16:02:05 +0000863
Misha Brukmanda546ea2004-04-28 19:24:28 +0000864 *Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000865
Misha Brukmanda546ea2004-04-28 19:24:28 +0000866 if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, *Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +0000867
Chris Lattnerfee714f2001-09-07 16:36:04 +0000868 // Output all of the instructions in the basic block...
Chris Lattner113f4f42002-06-25 16:13:24 +0000869 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
870 printInstruction(*I);
Chris Lattner96cdd272004-03-08 18:51:45 +0000871
Misha Brukmanda546ea2004-04-28 19:24:28 +0000872 if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, *Out);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000873}
874
Chris Lattner862e3382001-10-13 06:42:36 +0000875
Misha Brukmanc566ca362004-03-02 00:22:19 +0000876/// printInfoComment - Print a little comment after the instruction indicating
877/// which slot it occupies.
878///
Chris Lattner113f4f42002-06-25 16:13:24 +0000879void AssemblyWriter::printInfoComment(const Value &V) {
880 if (V.getType() != Type::VoidTy) {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000881 *Out << "\t\t; <";
Misha Brukman21bbdb92004-06-04 21:11:51 +0000882 printType(V.getType()) << '>';
Chris Lattner862e3382001-10-13 06:42:36 +0000883
Chris Lattner113f4f42002-06-25 16:13:24 +0000884 if (!V.hasName()) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000885 *Out << ':' << Machine.getSlot(&V); // Print out the def slot taken.
Chris Lattner862e3382001-10-13 06:42:36 +0000886 }
Misha Brukman21bbdb92004-06-04 21:11:51 +0000887 *Out << " [#uses=" << V.use_size() << ']'; // Output # uses
Chris Lattner862e3382001-10-13 06:42:36 +0000888 }
889}
890
Misha Brukmanc566ca362004-03-02 00:22:19 +0000891/// printInstruction - This member is called for each Instruction in a method.
892///
Chris Lattner113f4f42002-06-25 16:13:24 +0000893void AssemblyWriter::printInstruction(const Instruction &I) {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000894 if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, *Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +0000895
Misha Brukmanda546ea2004-04-28 19:24:28 +0000896 *Out << "\t";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000897
898 // Print out name if it exists...
Chris Lattner113f4f42002-06-25 16:13:24 +0000899 if (I.hasName())
Misha Brukmanda546ea2004-04-28 19:24:28 +0000900 *Out << getLLVMName(I.getName()) << " = ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000901
Chris Lattner504f9242003-09-08 17:45:59 +0000902 // If this is a volatile load or store, print out the volatile marker
903 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
904 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile()))
Misha Brukmanda546ea2004-04-28 19:24:28 +0000905 *Out << "volatile ";
Chris Lattner504f9242003-09-08 17:45:59 +0000906
Chris Lattner2f7c9632001-06-06 20:29:01 +0000907 // Print out the opcode...
Misha Brukmanda546ea2004-04-28 19:24:28 +0000908 *Out << I.getOpcodeName();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000909
910 // Print out the type of the operands...
Chris Lattner113f4f42002-06-25 16:13:24 +0000911 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000912
913 // Special case conditional branches to swizzle the condition out to the front
Chris Lattner113f4f42002-06-25 16:13:24 +0000914 if (isa<BranchInst>(I) && I.getNumOperands() > 1) {
915 writeOperand(I.getOperand(2), true);
Misha Brukman21bbdb92004-06-04 21:11:51 +0000916 *Out << ',';
Chris Lattner2f7c9632001-06-06 20:29:01 +0000917 writeOperand(Operand, true);
Misha Brukman21bbdb92004-06-04 21:11:51 +0000918 *Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +0000919 writeOperand(I.getOperand(1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000920
Chris Lattner8d48df22002-04-13 18:34:38 +0000921 } else if (isa<SwitchInst>(I)) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000922 // Special case switch statement to get formatting nice and correct...
Misha Brukman21bbdb92004-06-04 21:11:51 +0000923 writeOperand(Operand , true); *Out << ',';
Misha Brukmanda546ea2004-04-28 19:24:28 +0000924 writeOperand(I.getOperand(1), true); *Out << " [";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000925
Chris Lattner113f4f42002-06-25 16:13:24 +0000926 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000927 *Out << "\n\t\t";
Misha Brukman21bbdb92004-06-04 21:11:51 +0000928 writeOperand(I.getOperand(op ), true); *Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +0000929 writeOperand(I.getOperand(op+1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000930 }
Misha Brukmanda546ea2004-04-28 19:24:28 +0000931 *Out << "\n\t]";
Chris Lattnerda558102001-10-02 03:41:24 +0000932 } else if (isa<PHINode>(I)) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000933 *Out << ' ';
Chris Lattner113f4f42002-06-25 16:13:24 +0000934 printType(I.getType());
Misha Brukman21bbdb92004-06-04 21:11:51 +0000935 *Out << ' ';
Chris Lattner2f7c9632001-06-06 20:29:01 +0000936
Chris Lattner113f4f42002-06-25 16:13:24 +0000937 for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000938 if (op) *Out << ", ";
Misha Brukman21bbdb92004-06-04 21:11:51 +0000939 *Out << '[';
940 writeOperand(I.getOperand(op ), false); *Out << ',';
Misha Brukmanda546ea2004-04-28 19:24:28 +0000941 writeOperand(I.getOperand(op+1), false); *Out << " ]";
Chris Lattner931ef3b2001-06-11 15:04:20 +0000942 }
Chris Lattner862e3382001-10-13 06:42:36 +0000943 } else if (isa<ReturnInst>(I) && !Operand) {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000944 *Out << " void";
Chris Lattner862e3382001-10-13 06:42:36 +0000945 } else if (isa<CallInst>(I)) {
Chris Lattner463d6a52003-08-05 15:34:45 +0000946 const PointerType *PTy = cast<PointerType>(Operand->getType());
947 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
948 const Type *RetTy = FTy->getReturnType();
Chris Lattner2f2d9472001-11-06 21:28:12 +0000949
Chris Lattner463d6a52003-08-05 15:34:45 +0000950 // If possible, print out the short form of the call instruction. We can
Chris Lattner6915f8f2002-04-07 22:49:37 +0000951 // only do this if the first argument is a pointer to a nonvararg function,
Chris Lattner463d6a52003-08-05 15:34:45 +0000952 // and if the return type is not a pointer to a function.
Chris Lattner2f2d9472001-11-06 21:28:12 +0000953 //
Chris Lattner463d6a52003-08-05 15:34:45 +0000954 if (!FTy->isVarArg() &&
Chris Lattner8d48df22002-04-13 18:34:38 +0000955 (!isa<PointerType>(RetTy) ||
Chris Lattnerd9a36a62002-07-25 20:58:51 +0000956 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000957 *Out << ' '; printType(RetTy);
Chris Lattner2f2d9472001-11-06 21:28:12 +0000958 writeOperand(Operand, false);
959 } else {
960 writeOperand(Operand, true);
961 }
Misha Brukman21bbdb92004-06-04 21:11:51 +0000962 *Out << '(';
Chris Lattner113f4f42002-06-25 16:13:24 +0000963 if (I.getNumOperands() > 1) writeOperand(I.getOperand(1), true);
964 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; ++op) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000965 *Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +0000966 writeOperand(I.getOperand(op), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000967 }
968
Misha Brukmanda546ea2004-04-28 19:24:28 +0000969 *Out << " )";
Chris Lattner113f4f42002-06-25 16:13:24 +0000970 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Chris Lattner463d6a52003-08-05 15:34:45 +0000971 const PointerType *PTy = cast<PointerType>(Operand->getType());
972 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
973 const Type *RetTy = FTy->getReturnType();
974
975 // If possible, print out the short form of the invoke instruction. We can
976 // only do this if the first argument is a pointer to a nonvararg function,
977 // and if the return type is not a pointer to a function.
978 //
979 if (!FTy->isVarArg() &&
980 (!isa<PointerType>(RetTy) ||
981 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000982 *Out << ' '; printType(RetTy);
Chris Lattner463d6a52003-08-05 15:34:45 +0000983 writeOperand(Operand, false);
984 } else {
985 writeOperand(Operand, true);
986 }
987
Misha Brukman21bbdb92004-06-04 21:11:51 +0000988 *Out << '(';
Chris Lattner113f4f42002-06-25 16:13:24 +0000989 if (I.getNumOperands() > 3) writeOperand(I.getOperand(3), true);
990 for (unsigned op = 4, Eop = I.getNumOperands(); op < Eop; ++op) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000991 *Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +0000992 writeOperand(I.getOperand(op), true);
Chris Lattner862e3382001-10-13 06:42:36 +0000993 }
994
Misha Brukmanda546ea2004-04-28 19:24:28 +0000995 *Out << " )\n\t\t\tto";
Chris Lattner862e3382001-10-13 06:42:36 +0000996 writeOperand(II->getNormalDest(), true);
Misha Brukmanda546ea2004-04-28 19:24:28 +0000997 *Out << " unwind";
Chris Lattnerfae8ab32004-02-08 21:44:31 +0000998 writeOperand(II->getUnwindDest(), true);
Chris Lattner862e3382001-10-13 06:42:36 +0000999
Chris Lattner113f4f42002-06-25 16:13:24 +00001000 } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(&I)) {
Misha Brukman21bbdb92004-06-04 21:11:51 +00001001 *Out << ' ';
Chris Lattner8d48df22002-04-13 18:34:38 +00001002 printType(AI->getType()->getElementType());
1003 if (AI->isArrayAllocation()) {
Misha Brukman21bbdb92004-06-04 21:11:51 +00001004 *Out << ',';
Chris Lattner8d48df22002-04-13 18:34:38 +00001005 writeOperand(AI->getArraySize(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001006 }
Chris Lattner862e3382001-10-13 06:42:36 +00001007 } else if (isa<CastInst>(I)) {
Chris Lattnerc96e96b2003-11-17 01:17:04 +00001008 if (Operand) writeOperand(Operand, true); // Work with broken code
Misha Brukmanda546ea2004-04-28 19:24:28 +00001009 *Out << " to ";
Chris Lattner113f4f42002-06-25 16:13:24 +00001010 printType(I.getType());
Chris Lattner5b337482003-10-18 05:57:43 +00001011 } else if (isa<VAArgInst>(I)) {
Chris Lattnerc96e96b2003-11-17 01:17:04 +00001012 if (Operand) writeOperand(Operand, true); // Work with broken code
Misha Brukmanda546ea2004-04-28 19:24:28 +00001013 *Out << ", ";
Chris Lattnerf70da102003-05-08 02:44:12 +00001014 printType(I.getType());
Chris Lattner5b337482003-10-18 05:57:43 +00001015 } else if (const VANextInst *VAN = dyn_cast<VANextInst>(&I)) {
Chris Lattnerc96e96b2003-11-17 01:17:04 +00001016 if (Operand) writeOperand(Operand, true); // Work with broken code
Misha Brukmanda546ea2004-04-28 19:24:28 +00001017 *Out << ", ";
Chris Lattner5b337482003-10-18 05:57:43 +00001018 printType(VAN->getArgType());
Chris Lattner2f7c9632001-06-06 20:29:01 +00001019 } else if (Operand) { // Print the normal way...
1020
1021 // PrintAllTypes - Instructions who have operands of all the same type
1022 // omit the type from all but the first operand. If the instruction has
1023 // different type operands (for example br), then they are all printed.
1024 bool PrintAllTypes = false;
1025 const Type *TheType = Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001026
Chris Lattner52bd5cb92004-03-12 05:53:14 +00001027 // Shift Left & Right print both types even for Ubyte LHS, and select prints
1028 // types even if all operands are bools.
1029 if (isa<ShiftInst>(I) || isa<SelectInst>(I)) {
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00001030 PrintAllTypes = true;
1031 } else {
1032 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
1033 Operand = I.getOperand(i);
1034 if (Operand->getType() != TheType) {
1035 PrintAllTypes = true; // We have differing types! Print them all!
1036 break;
1037 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001038 }
1039 }
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00001040
Chris Lattner7bfee412001-10-29 16:05:51 +00001041 if (!PrintAllTypes) {
Misha Brukman21bbdb92004-06-04 21:11:51 +00001042 *Out << ' ';
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00001043 printType(TheType);
Chris Lattner7bfee412001-10-29 16:05:51 +00001044 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001045
Chris Lattner113f4f42002-06-25 16:13:24 +00001046 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Misha Brukman21bbdb92004-06-04 21:11:51 +00001047 if (i) *Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001048 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001049 }
1050 }
1051
Chris Lattner862e3382001-10-13 06:42:36 +00001052 printInfoComment(I);
Misha Brukmanda546ea2004-04-28 19:24:28 +00001053 *Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001054}
1055
1056
1057//===----------------------------------------------------------------------===//
1058// External Interface declarations
1059//===----------------------------------------------------------------------===//
1060
Chris Lattner8339f7d2003-10-30 23:41:03 +00001061void Module::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001062 SlotMachine SlotTable(this);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001063 AssemblyWriter W(o, SlotTable, this, AAW);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001064 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001065}
1066
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001067void GlobalVariable::print(std::ostream &o) const {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001068 SlotMachine SlotTable(getParent());
Chris Lattner8339f7d2003-10-30 23:41:03 +00001069 AssemblyWriter W(o, SlotTable, getParent(), 0);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001070 W.write(this);
Chris Lattneradfe0d12001-09-10 20:08:19 +00001071}
1072
Chris Lattner8339f7d2003-10-30 23:41:03 +00001073void Function::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001074 SlotMachine SlotTable(getParent());
Chris Lattner8339f7d2003-10-30 23:41:03 +00001075 AssemblyWriter W(o, SlotTable, getParent(), AAW);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001076
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001077 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001078}
1079
Chris Lattner8339f7d2003-10-30 23:41:03 +00001080void BasicBlock::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001081 SlotMachine SlotTable(getParent());
Chris Lattner7bfee412001-10-29 16:05:51 +00001082 AssemblyWriter W(o, SlotTable,
Chris Lattner8339f7d2003-10-30 23:41:03 +00001083 getParent() ? getParent()->getParent() : 0, AAW);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001084 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001085}
1086
Chris Lattner8339f7d2003-10-30 23:41:03 +00001087void Instruction::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001088 const Function *F = getParent() ? getParent()->getParent() : 0;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001089 SlotMachine SlotTable(F);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001090 AssemblyWriter W(o, SlotTable, F ? F->getParent() : 0, AAW);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001091
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001092 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001093}
Chris Lattner7db79582001-11-07 04:21:57 +00001094
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001095void Constant::print(std::ostream &o) const {
1096 if (this == 0) { o << "<null> constant value\n"; return; }
Chris Lattner7d734802002-09-10 15:53:49 +00001097
1098 // Handle CPR's special, because they have context information...
1099 if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(this)) {
1100 CPR->getValue()->print(o); // Print as a global value, with context info.
1101 return;
1102 }
1103
Misha Brukman21bbdb92004-06-04 21:11:51 +00001104 o << ' ' << getType()->getDescription() << ' ';
Chris Lattner1e194682002-04-18 18:53:13 +00001105
Chris Lattnerab7d1ab2003-05-08 02:08:14 +00001106 std::map<const Type *, std::string> TypeTable;
Chris Lattner1e194682002-04-18 18:53:13 +00001107 WriteConstantInt(o, this, false, TypeTable, 0);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001108}
1109
1110void Type::print(std::ostream &o) const {
1111 if (this == 0)
1112 o << "<null Type>";
1113 else
1114 o << getDescription();
1115}
1116
Chris Lattner2e9fa6d2002-04-09 19:48:49 +00001117void Argument::print(std::ostream &o) const {
Misha Brukman21bbdb92004-06-04 21:11:51 +00001118 o << getType() << ' ' << getName();
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001119}
1120
Reid Spencer52641832004-05-25 18:14:38 +00001121// Value::dump - allow easy printing of Values from the debugger.
1122// Located here because so much of the needed functionality is here.
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001123void Value::dump() const { print(std::cerr); }
Reid Spencer52641832004-05-25 18:14:38 +00001124
1125// Type::dump - allow easy printing of Values from the debugger.
1126// Located here because so much of the needed functionality is here.
Reid Spencere7e96712004-05-25 08:53:40 +00001127void Type::dump() const { print(std::cerr); }
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001128
1129//===----------------------------------------------------------------------===//
1130// CachedWriter Class Implementation
1131//===----------------------------------------------------------------------===//
1132
Chris Lattner7db79582001-11-07 04:21:57 +00001133void CachedWriter::setModule(const Module *M) {
1134 delete SC; delete AW;
1135 if (M) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001136 SC = new SlotMachine(M );
Misha Brukman21bbdb92004-06-04 21:11:51 +00001137 AW = new AssemblyWriter(Out, *SC, M, 0);
Chris Lattner7db79582001-11-07 04:21:57 +00001138 } else {
1139 SC = 0; AW = 0;
1140 }
1141}
1142
1143CachedWriter::~CachedWriter() {
1144 delete AW;
1145 delete SC;
1146}
1147
1148CachedWriter &CachedWriter::operator<<(const Value *V) {
1149 assert(AW && SC && "CachedWriter does not have a current module!");
1150 switch (V->getValueType()) {
1151 case Value::ConstantVal:
Chris Lattner1e194682002-04-18 18:53:13 +00001152 case Value::ArgumentVal: AW->writeOperand(V, true, true); break;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +00001153 case Value::TypeVal: AW->write(cast<Type>(V)); break;
Chris Lattner7db79582001-11-07 04:21:57 +00001154 case Value::InstructionVal: AW->write(cast<Instruction>(V)); break;
1155 case Value::BasicBlockVal: AW->write(cast<BasicBlock>(V)); break;
Chris Lattner57698e22002-03-26 18:01:55 +00001156 case Value::FunctionVal: AW->write(cast<Function>(V)); break;
Chris Lattner7db79582001-11-07 04:21:57 +00001157 case Value::GlobalVariableVal: AW->write(cast<GlobalVariable>(V)); break;
Misha Brukman21bbdb92004-06-04 21:11:51 +00001158 default: Out << "<unknown value type: " << V->getValueType() << '>'; break;
Chris Lattner7db79582001-11-07 04:21:57 +00001159 }
1160 return *this;
1161}
Misha Brukman4685e262004-04-28 15:31:21 +00001162
1163CachedWriter& CachedWriter::operator<<(const Type *X) {
1164 if (SymbolicTypes) {
1165 const Module *M = AW->getModule();
Misha Brukman21bbdb92004-06-04 21:11:51 +00001166 if (M) WriteTypeSymbolic(Out, X, M);
Misha Brukman4685e262004-04-28 15:31:21 +00001167 return *this;
1168 } else
1169 return *this << (const Value*)X;
1170}
Misha Brukmanda546ea2004-04-28 19:24:28 +00001171
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001172//===----------------------------------------------------------------------===//
1173//===-- SlotMachine Implementation
1174//===----------------------------------------------------------------------===//
1175
1176#if 0
1177#define SC_DEBUG(X) std::cerr << X
1178#else
1179#define SC_DEBUG(X)
1180#endif
1181
1182// Module level constructor. Causes the contents of the Module (sans functions)
1183// to be added to the slot table.
1184SlotMachine::SlotMachine(const Module *M)
Reid Spencer56010e42004-05-26 21:56:09 +00001185 : TheModule(M) ///< Saved for lazy initialization.
1186 , TheFunction(0)
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001187 , mMap()
1188 , fMap()
1189{
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001190}
1191
1192// Function level constructor. Causes the contents of the Module and the one
1193// function provided to be added to the slot table.
1194SlotMachine::SlotMachine(const Function *F )
Reid Spencer56010e42004-05-26 21:56:09 +00001195 : TheModule( F ? F->getParent() : 0 ) ///< Saved for lazy initialization
1196 , TheFunction(F) ///< Saved for lazy initialization
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001197 , mMap()
1198 , fMap()
1199{
Reid Spencer56010e42004-05-26 21:56:09 +00001200}
1201
1202inline void SlotMachine::initialize(void) {
1203 if ( TheModule) {
1204 processModule();
1205 TheModule = 0; ///< Prevent re-processing next time we're called.
1206 }
1207 if ( TheFunction ) {
1208 processFunction();
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001209 }
1210}
1211
1212// Iterate through all the global variables, functions, and global
1213// variable initializers and create slots for them.
1214void SlotMachine::processModule() {
1215 SC_DEBUG("begin processModule!\n");
1216
1217 // Add all of the global variables to the value table...
1218 for (Module::const_giterator I = TheModule->gbegin(), E = TheModule->gend();
1219 I != E; ++I)
1220 createSlot(I);
1221
1222 // Add all the functions to the table
1223 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
1224 I != E; ++I)
1225 createSlot(I);
1226
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001227 SC_DEBUG("end processModule!\n");
1228}
1229
1230
Reid Spencer56010e42004-05-26 21:56:09 +00001231// Process the arguments, basic blocks, and instructions of a function.
1232void SlotMachine::processFunction() {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001233 SC_DEBUG("begin processFunction!\n");
1234
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001235 // Add all the function arguments
Reid Spencer56010e42004-05-26 21:56:09 +00001236 for(Function::const_aiterator AI = TheFunction->abegin(),
1237 AE = TheFunction->aend(); AI != AE; ++AI)
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001238 createSlot(AI);
1239
1240 SC_DEBUG("Inserting Instructions:\n");
1241
1242 // Add all of the basic blocks and instructions
Reid Spencer56010e42004-05-26 21:56:09 +00001243 for (Function::const_iterator BB = TheFunction->begin(),
1244 E = TheFunction->end(); BB != E; ++BB) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001245 createSlot(BB);
1246 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
1247 createSlot(I);
1248 }
1249 }
1250
1251 SC_DEBUG("end processFunction!\n");
1252}
1253
1254// Clean up after incorporating a function. This is the only way
Reid Spencer56010e42004-05-26 21:56:09 +00001255// to get out of the function incorporation state that affects the
1256// getSlot/createSlot lock. Function incorporation state is indicated
1257// by TheFunction != 0.
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001258void SlotMachine::purgeFunction() {
1259 SC_DEBUG("begin purgeFunction!\n");
1260 fMap.clear(); // Simply discard the function level map
Reid Spencer56010e42004-05-26 21:56:09 +00001261 TheFunction = 0;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001262 SC_DEBUG("end purgeFunction!\n");
1263}
1264
1265/// Get the slot number for a value. This function will assert if you
1266/// ask for a Value that hasn't previously been inserted with createSlot.
1267/// Types are forbidden because Type does not inherit from Value (any more).
Reid Spencer56010e42004-05-26 21:56:09 +00001268unsigned SlotMachine::getSlot(const Value *V) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001269 assert( V && "Can't get slot for null Value" );
1270 assert( !isa<Type>(V) && "Can't get slot for a type" );
Reid Spencer56010e42004-05-26 21:56:09 +00001271 assert(!isa<Constant>(V) || isa<GlobalValue>(V) &&
1272 "Can't insert a non-GlobalValue Constant into SlotMachine");
1273
1274 // Check for uninitialized state and do lazy initialization
1275 this->initialize();
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001276
1277 // Do not number CPR's at all. They are an abomination
1278 if ( const ConstantPointerRef* CPR = dyn_cast<ConstantPointerRef>(V) )
1279 V = CPR->getValue() ;
1280
1281 // Get the type of the value
1282 const Type* VTy = V->getType();
1283
1284 // Find the type plane in the module map
1285 TypedPlanes::const_iterator MI = mMap.find(VTy);
1286
Reid Spencer56010e42004-05-26 21:56:09 +00001287 if ( TheFunction ) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001288 // Lookup the type in the function map too
1289 TypedPlanes::const_iterator FI = fMap.find(VTy);
1290 // If there is a corresponding type plane in the function map
1291 if ( FI != fMap.end() ) {
1292 // Lookup the Value in the function map
1293 ValueMap::const_iterator FVI = FI->second.map.find(V);
1294 // If the value doesn't exist in the function map
1295 if ( FVI == FI->second.map.end() ) {
Reid Spencer56010e42004-05-26 21:56:09 +00001296 // Look up the value in the module map
1297 ValueMap::const_iterator MVI = MI->second.map.find(V);
1298 // If we didn't find it, it wasn't inserted
1299 assert( MVI != MI->second.map.end() && "Value not found");
1300 // We found it only at the module level
1301 return MVI->second;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001302
1303 // else the value exists in the function map
1304 } else {
Reid Spencer56010e42004-05-26 21:56:09 +00001305 // Return the slot number as the module's contribution to
1306 // the type plane plus the index in the function's contribution
1307 // to the type plane.
1308 return MI->second.next_slot + FVI->second;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001309 }
1310
1311 // else there is not a corresponding type plane in the function map
1312 } else {
1313 assert( MI != mMap.end() && "No such type plane!" );
1314 // Look up the value in the module's map
1315 ValueMap::const_iterator MVI = MI->second.map.find(V);
1316 // If we didn't find it, it wasn't inserted.
1317 assert( MVI != MI->second.map.end() && "Value not found");
1318 // We found it only in the module level and function level
1319 // didn't even have a type plane.
1320 return MVI->second;
1321 }
1322 }
1323
Reid Spencer56010e42004-05-26 21:56:09 +00001324 // N.B. Can only get here if !TheFunction
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001325
1326 // Make sure the type plane exists
1327 assert( MI != mMap.end() && "No such type plane!" );
1328 // Lookup the value in the module's map
1329 ValueMap::const_iterator MVI = MI->second.map.find(V);
1330 // Make sure we found it.
1331 assert( MVI != MI->second.map.end() && "Value not found" );
1332 // Return it.
1333 return MVI->second;
1334}
1335
1336
1337// Create a new slot, or return the existing slot if it is already
1338// inserted. Note that the logic here parallels getSlot but instead
1339// of asserting when the Value* isn't found, it inserts the value.
1340unsigned SlotMachine::createSlot(const Value *V) {
1341 assert( V && "Can't insert a null Value to SlotMachine");
1342 assert( !isa<Type>(V) && "Can't insert a Type into SlotMachine");
Reid Spencer56010e42004-05-26 21:56:09 +00001343 assert(!isa<Constant>(V) || isa<GlobalValue>(V) &&
1344 "Can't insert a non-GlobalValue Constant into SlotMachine");
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001345
1346 const Type* VTy = V->getType();
1347
1348 // Just ignore void typed things
1349 if (VTy == Type::VoidTy) return 0; // FIXME: Wrong return value!
1350
1351 // Look up the type plane for the Value's type from the module map
1352 TypedPlanes::const_iterator MI = mMap.find(VTy);
1353
Reid Spencer56010e42004-05-26 21:56:09 +00001354 if ( TheFunction ) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001355 // Get the type plane for the Value's type from the function map
1356 TypedPlanes::const_iterator FI = fMap.find(VTy);
1357 // If there is a corresponding type plane in the function map
1358 if ( FI != fMap.end() ) {
1359 // Lookup the Value in the function map
1360 ValueMap::const_iterator FVI = FI->second.map.find(V);
1361 // If the value doesn't exist in the function map
1362 if ( FVI == FI->second.map.end() ) {
Reid Spencer56010e42004-05-26 21:56:09 +00001363 // If there is no corresponding type plane in the module map
1364 if ( MI == mMap.end() )
1365 return insertValue(V);
1366 // Look up the value in the module map
1367 ValueMap::const_iterator MVI = MI->second.map.find(V);
1368 // If we didn't find it, it wasn't inserted
1369 if ( MVI == MI->second.map.end() )
1370 return insertValue(V);
1371 else
1372 // We found it only at the module level
1373 return MVI->second;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001374
1375 // else the value exists in the function map
1376 } else {
Reid Spencer56010e42004-05-26 21:56:09 +00001377 if ( MI == mMap.end() )
1378 return FVI->second;
1379 else
1380 // Return the slot number as the module's contribution to
1381 // the type plane plus the index in the function's contribution
1382 // to the type plane.
1383 return MI->second.next_slot + FVI->second;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001384 }
1385
1386 // else there is not a corresponding type plane in the function map
1387 } else {
1388 // If the type plane doesn't exists at the module level
1389 if ( MI == mMap.end() ) {
Reid Spencer56010e42004-05-26 21:56:09 +00001390 return insertValue(V);
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001391 // else type plane exists at the module level, examine it
1392 } else {
Reid Spencer56010e42004-05-26 21:56:09 +00001393 // Look up the value in the module's map
1394 ValueMap::const_iterator MVI = MI->second.map.find(V);
1395 // If we didn't find it there either
1396 if ( MVI == MI->second.map.end() )
1397 // Return the slot number as the module's contribution to
1398 // the type plane plus the index of the function map insertion.
1399 return MI->second.next_slot + insertValue(V);
1400 else
1401 return MVI->second;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001402 }
1403 }
1404 }
1405
Reid Spencer56010e42004-05-26 21:56:09 +00001406 // N.B. Can only get here if !TheFunction
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001407
1408 // If the module map's type plane is not for the Value's type
1409 if ( MI != mMap.end() ) {
1410 // Lookup the value in the module's map
1411 ValueMap::const_iterator MVI = MI->second.map.find(V);
1412 if ( MVI != MI->second.map.end() )
1413 return MVI->second;
1414 }
1415
1416 return insertValue(V);
1417}
1418
1419
1420// Low level insert function. Minimal checking is done. This
1421// function is just for the convenience of createSlot (above).
1422unsigned SlotMachine::insertValue(const Value *V ) {
1423 assert(V && "Can't insert a null Value into SlotMachine!");
1424 assert(!isa<Type>(V) && "Can't insert a Type into SlotMachine!");
Reid Spencer56010e42004-05-26 21:56:09 +00001425 assert(!isa<Constant>(V) || isa<GlobalValue>(V) &&
1426 "Can't insert a non-GlobalValue Constant into SlotMachine");
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001427
Reid Spencer56010e42004-05-26 21:56:09 +00001428 // If this value does not contribute to a plane (is void)
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001429 // or if the value already has a name then ignore it.
Reid Spencer56010e42004-05-26 21:56:09 +00001430 if (V->getType() == Type::VoidTy || V->hasName() ) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001431 SC_DEBUG("ignored value " << *V << "\n");
1432 return 0; // FIXME: Wrong return value
1433 }
1434
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001435 const Type *VTy = V->getType();
1436 unsigned DestSlot = 0;
1437
Reid Spencer56010e42004-05-26 21:56:09 +00001438 if ( TheFunction ) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001439 TypedPlanes::iterator I = fMap.find( VTy );
1440 if ( I == fMap.end() )
1441 I = fMap.insert(std::make_pair(VTy,Plane())).first;
1442 DestSlot = I->second.map[V] = I->second.next_slot++;
1443 } else {
1444 TypedPlanes::iterator I = mMap.find( VTy );
1445 if ( I == mMap.end() )
1446 I = mMap.insert(std::make_pair(VTy,Plane())).first;
1447 DestSlot = I->second.map[V] = I->second.next_slot++;
1448 }
1449
1450 SC_DEBUG(" Inserting value [" << VTy << "] = " << V << " slot=" <<
Reid Spencer56010e42004-05-26 21:56:09 +00001451 DestSlot << " [");
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001452 // G = Global, C = Constant, T = Type, F = Function, o = other
Misha Brukman21bbdb92004-06-04 21:11:51 +00001453 SC_DEBUG((isa<GlobalVariable>(V) ? 'G' : (isa<Constant>(V) ? 'C' :
1454 (isa<Function>(V) ? 'F' : 'o'))));
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001455 SC_DEBUG("]\n");
1456 return DestSlot;
1457}
1458
Reid Spencere7e96712004-05-25 08:53:40 +00001459// vim: sw=2