blob: 24c33489a8eca8e8a2947863c4eaa6a950395637 [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 Spencer8beac692004-06-09 15:26:53 +000077 unsigned getSlot(const Value *V, bool ignoreMissing = false ) ;
78
79 /// Determine if a Value has a slot or not
80 bool hasSlot(const Value* V);
Reid Spencer16f2f7f2004-05-26 07:18:52 +000081
82/// @}
83/// @name Mutators
84/// @{
85public:
86 /// If you'd like to deal with a function instead of just a module, use
87 /// this method to get its data into the SlotMachine.
Reid Spencer56010e42004-05-26 21:56:09 +000088 void incorporateFunction(const Function *F) { TheFunction = F; }
Reid Spencer16f2f7f2004-05-26 07:18:52 +000089
90 /// After calling incorporateFunction, use this method to remove the
91 /// most recently incorporated function from the SlotMachine. This
92 /// will reset the state of the machine back to just the module contents.
93 void purgeFunction();
94
95/// @}
96/// @name Implementation Details
97/// @{
98private:
Reid Spencer56010e42004-05-26 21:56:09 +000099 /// This function does the actual initialization.
100 inline void initialize();
101
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000102 /// Values can be crammed into here at will. If they haven't
103 /// been inserted already, they get inserted, otherwise they are ignored.
104 /// Either way, the slot number for the Value* is returned.
105 unsigned createSlot(const Value *V);
106
107 /// Insert a value into the value table. Return the slot number
108 /// that it now occupies. BadThings(TM) will happen if you insert a
109 /// Value that's already been inserted.
110 unsigned insertValue( const Value *V );
111
112 /// Add all of the module level global variables (and their initializers)
113 /// and function declarations, but not the contents of those functions.
114 void processModule();
115
Reid Spencer56010e42004-05-26 21:56:09 +0000116 /// Add all of the functions arguments, basic blocks, and instructions
117 void processFunction();
118
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000119 SlotMachine(const SlotMachine &); // DO NOT IMPLEMENT
120 void operator=(const SlotMachine &); // DO NOT IMPLEMENT
121
122/// @}
123/// @name Data
124/// @{
125public:
126
127 /// @brief The module for which we are holding slot numbers
Reid Spencer56010e42004-05-26 21:56:09 +0000128 const Module* TheModule;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000129
Reid Spencer56010e42004-05-26 21:56:09 +0000130 /// @brief The function for which we are holding slot numbers
131 const Function* TheFunction;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000132
133 /// @brief The TypePlanes map for the module level data
134 TypedPlanes mMap;
135
136 /// @brief The TypePlanes map for the function level data
137 TypedPlanes fMap;
138
139/// @}
140
141};
142
143}
144
Chris Lattnerc8b70922002-07-26 21:12:46 +0000145static RegisterPass<PrintModulePass>
146X("printm", "Print module to stderr",PassInfo::Analysis|PassInfo::Optimization);
147static RegisterPass<PrintFunctionPass>
148Y("print","Print function to stderr",PassInfo::Analysis|PassInfo::Optimization);
Chris Lattner7f8845a2002-07-23 18:07:49 +0000149
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000150static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
151 bool PrintName,
152 std::map<const Type *, std::string> &TypeTable,
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000153 SlotMachine *Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000154
Chris Lattnerb86620e2001-10-29 16:37:48 +0000155static const Module *getModuleFromVal(const Value *V) {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000156 if (const Argument *MA = dyn_cast<Argument>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +0000157 return MA->getParent() ? MA->getParent()->getParent() : 0;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000158 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +0000159 return BB->getParent() ? BB->getParent()->getParent() : 0;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000160 else if (const Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner57698e22002-03-26 18:01:55 +0000161 const Function *M = I->getParent() ? I->getParent()->getParent() : 0;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000162 return M ? M->getParent() : 0;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000163 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +0000164 return GV->getParent();
Chris Lattnerb86620e2001-10-29 16:37:48 +0000165 return 0;
166}
167
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000168static SlotMachine *createSlotMachine(const Value *V) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000169 assert(!isa<Type>(V) && "Can't create an SC for a type!");
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000170 if (const Argument *FA = dyn_cast<Argument>(V)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000171 return new SlotMachine(FA->getParent());
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000172 } else if (const Instruction *I = dyn_cast<Instruction>(V)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000173 return new SlotMachine(I->getParent()->getParent());
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000174 } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000175 return new SlotMachine(BB->getParent());
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000176 } else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)){
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000177 return new SlotMachine(GV->getParent());
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000178 } else if (const Function *Func = dyn_cast<Function>(V)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000179 return new SlotMachine(Func);
Chris Lattner7bfee412001-10-29 16:05:51 +0000180 }
181 return 0;
182}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000183
Chris Lattner1c343042003-08-22 05:40:38 +0000184// getLLVMName - Turn the specified string into an 'LLVM name', which is either
185// prefixed with % (if the string only contains simple characters) or is
186// surrounded with ""'s (if it has special chars in it).
187static std::string getLLVMName(const std::string &Name) {
188 assert(!Name.empty() && "Cannot get empty name!");
189
190 // First character cannot start with a number...
191 if (Name[0] >= '0' && Name[0] <= '9')
192 return "\"" + Name + "\"";
193
194 // Scan to see if we have any characters that are not on the "white list"
195 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
196 char C = Name[i];
197 assert(C != '"' && "Illegal character in LLVM value name!");
198 if ((C < 'a' || C > 'z') && (C < 'A' || C > 'Z') && (C < '0' || C > '9') &&
199 C != '-' && C != '.' && C != '_')
200 return "\"" + Name + "\"";
201 }
202
203 // If we get here, then the identifier is legal to use as a "VarID".
204 return "%"+Name;
205}
206
Chris Lattnerb86620e2001-10-29 16:37:48 +0000207
Misha Brukmanc566ca362004-03-02 00:22:19 +0000208/// fillTypeNameTable - If the module has a symbol table, take all global types
209/// and stuff their names into the TypeNames map.
210///
Chris Lattnerb86620e2001-10-29 16:37:48 +0000211static void fillTypeNameTable(const Module *M,
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000212 std::map<const Type *, std::string> &TypeNames) {
Chris Lattner98cf1f52002-11-20 18:36:02 +0000213 if (!M) return;
214 const SymbolTable &ST = M->getSymbolTable();
Reid Spencere7e96712004-05-25 08:53:40 +0000215 SymbolTable::type_const_iterator TI = ST.type_begin();
216 for (; TI != ST.type_end(); ++TI ) {
217 // As a heuristic, don't insert pointer to primitive types, because
218 // they are used too often to have a single useful name.
219 //
220 const Type *Ty = cast<Type>(TI->second);
221 if (!isa<PointerType>(Ty) ||
Reid Spencer56010e42004-05-26 21:56:09 +0000222 !cast<PointerType>(Ty)->getElementType()->isPrimitiveType() ||
223 isa<OpaqueType>(cast<PointerType>(Ty)->getElementType()))
Reid Spencere7e96712004-05-25 08:53:40 +0000224 TypeNames.insert(std::make_pair(Ty, getLLVMName(TI->first)));
Chris Lattnerb86620e2001-10-29 16:37:48 +0000225 }
226}
227
228
229
John Criswellcd116ba2004-06-01 14:54:08 +0000230static void calcTypeName(const Type *Ty,
231 std::vector<const Type *> &TypeStack,
232 std::map<const Type *, std::string> &TypeNames,
233 std::string & Result){
234 if (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty)) {
235 Result += Ty->getDescription(); // Base case
236 return;
237 }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000238
239 // Check to see if the type is named.
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000240 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
John Criswellcd116ba2004-06-01 14:54:08 +0000241 if (I != TypeNames.end()) {
242 Result += I->second;
243 return;
244 }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000245
John Criswellcd116ba2004-06-01 14:54:08 +0000246 if (isa<OpaqueType>(Ty)) {
247 Result += "opaque";
248 return;
249 }
Chris Lattnerf14ead92003-10-30 00:22:33 +0000250
Chris Lattnerb86620e2001-10-29 16:37:48 +0000251 // Check to see if the Type is already on the stack...
252 unsigned Slot = 0, CurSize = TypeStack.size();
253 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
254
255 // This is another base case for the recursion. In this case, we know
256 // that we have looped back to a type that we have previously visited.
257 // Generate the appropriate upreference to handle this.
John Criswellcd116ba2004-06-01 14:54:08 +0000258 if (Slot < CurSize) {
259 Result += "\\" + utostr(CurSize-Slot); // Here's the upreference
260 return;
261 }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000262
263 TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
264
Chris Lattnerb86620e2001-10-29 16:37:48 +0000265 switch (Ty->getPrimitiveID()) {
Chris Lattner91db5822002-03-29 03:44:36 +0000266 case Type::FunctionTyID: {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000267 const FunctionType *FTy = cast<FunctionType>(Ty);
John Criswellcd116ba2004-06-01 14:54:08 +0000268 calcTypeName(FTy->getReturnType(), TypeStack, TypeNames, Result);
269 Result += " (";
Chris Lattnerfa829be2004-02-09 04:14:01 +0000270 for (FunctionType::param_iterator I = FTy->param_begin(),
271 E = FTy->param_end(); I != E; ++I) {
272 if (I != FTy->param_begin())
Chris Lattnerb86620e2001-10-29 16:37:48 +0000273 Result += ", ";
John Criswellcd116ba2004-06-01 14:54:08 +0000274 calcTypeName(*I, TypeStack, TypeNames, Result);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000275 }
Chris Lattnerd816b532002-04-13 20:53:41 +0000276 if (FTy->isVarArg()) {
Chris Lattnerfa829be2004-02-09 04:14:01 +0000277 if (FTy->getNumParams()) Result += ", ";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000278 Result += "...";
279 }
280 Result += ")";
281 break;
282 }
283 case Type::StructTyID: {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000284 const StructType *STy = cast<StructType>(Ty);
John Criswellcd116ba2004-06-01 14:54:08 +0000285 Result += "{ ";
Chris Lattnerac6db752004-02-09 04:37:31 +0000286 for (StructType::element_iterator I = STy->element_begin(),
287 E = STy->element_end(); I != E; ++I) {
288 if (I != STy->element_begin())
Chris Lattnerb86620e2001-10-29 16:37:48 +0000289 Result += ", ";
John Criswellcd116ba2004-06-01 14:54:08 +0000290 calcTypeName(*I, TypeStack, TypeNames, Result);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000291 }
292 Result += " }";
293 break;
294 }
295 case Type::PointerTyID:
John Criswellcd116ba2004-06-01 14:54:08 +0000296 calcTypeName(cast<PointerType>(Ty)->getElementType(),
297 TypeStack, TypeNames, Result);
298 Result += "*";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000299 break;
300 case Type::ArrayTyID: {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000301 const ArrayType *ATy = cast<ArrayType>(Ty);
John Criswellcd116ba2004-06-01 14:54:08 +0000302 Result += "[" + utostr(ATy->getNumElements()) + " x ";
303 calcTypeName(ATy->getElementType(), TypeStack, TypeNames, Result);
304 Result += "]";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000305 break;
306 }
Chris Lattner15285ab2003-05-14 17:50:47 +0000307 case Type::OpaqueTyID:
John Criswellcd116ba2004-06-01 14:54:08 +0000308 Result += "opaque";
Chris Lattner15285ab2003-05-14 17:50:47 +0000309 break;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000310 default:
John Criswellcd116ba2004-06-01 14:54:08 +0000311 Result += "<unrecognized-type>";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000312 }
313
314 TypeStack.pop_back(); // Remove self from stack...
John Criswellcd116ba2004-06-01 14:54:08 +0000315 return;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000316}
317
318
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000319/// printTypeInt - The internal guts of printing out a type that has a
320/// potentially named portion.
321///
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000322static std::ostream &printTypeInt(std::ostream &Out, const Type *Ty,
323 std::map<const Type *, std::string> &TypeNames) {
Chris Lattnerb86620e2001-10-29 16:37:48 +0000324 // Primitive types always print out their description, regardless of whether
325 // they have been named or not.
326 //
Chris Lattner92d60532003-10-30 00:12:51 +0000327 if (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty))
328 return Out << Ty->getDescription();
Chris Lattnerb86620e2001-10-29 16:37:48 +0000329
330 // Check to see if the type is named.
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000331 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000332 if (I != TypeNames.end()) return Out << I->second;
333
334 // Otherwise we have a type that has not been named but is a derived type.
335 // Carefully recurse the type hierarchy to print out any contained symbolic
336 // names.
337 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000338 std::vector<const Type *> TypeStack;
John Criswellcd116ba2004-06-01 14:54:08 +0000339 std::string TypeName;
340 calcTypeName(Ty, TypeStack, TypeNames, TypeName);
Chris Lattner7f74a562002-01-20 22:54:45 +0000341 TypeNames.insert(std::make_pair(Ty, TypeName));//Cache type name for later use
John Criswellcd116ba2004-06-01 14:54:08 +0000342 return (Out << TypeName);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000343}
344
Chris Lattner34b95182001-10-31 04:33:19 +0000345
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000346/// WriteTypeSymbolic - This attempts to write the specified type as a symbolic
347/// type, iff there is an entry in the modules symbol table for the specified
348/// type or one of it's component types. This is slower than a simple x << Type
349///
Chris Lattner189d19f2003-11-21 20:23:48 +0000350std::ostream &llvm::WriteTypeSymbolic(std::ostream &Out, const Type *Ty,
351 const Module *M) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000352 Out << ' ';
Chris Lattnerb86620e2001-10-29 16:37:48 +0000353
354 // If they want us to print out a type, attempt to make it symbolic if there
355 // is a symbol table in the module...
Chris Lattner98cf1f52002-11-20 18:36:02 +0000356 if (M) {
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000357 std::map<const Type *, std::string> TypeNames;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000358 fillTypeNameTable(M, TypeNames);
359
Chris Lattner72f866e2001-10-29 16:40:32 +0000360 return printTypeInt(Out, Ty, TypeNames);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000361 } else {
Chris Lattner72f866e2001-10-29 16:40:32 +0000362 return Out << Ty->getDescription();
Chris Lattnerb86620e2001-10-29 16:37:48 +0000363 }
364}
365
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000366static void WriteConstantInt(std::ostream &Out, const Constant *CV,
367 bool PrintName,
368 std::map<const Type *, std::string> &TypeTable,
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000369 SlotMachine *Machine) {
Chris Lattner1e194682002-04-18 18:53:13 +0000370 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
371 Out << (CB == ConstantBool::True ? "true" : "false");
372 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV)) {
373 Out << CI->getValue();
374 } else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV)) {
375 Out << CI->getValue();
376 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
377 // We would like to output the FP constant value in exponential notation,
378 // but we cannot do this if doing so will lose precision. Check here to
379 // make sure that we only output it in exponential format if we can parse
380 // the value back and get the same value.
381 //
382 std::string StrVal = ftostr(CFP->getValue());
383
384 // Check to make sure that the stringized number is not some string like
385 // "Inf" or NaN, that atof will accept, but the lexer will not. Check that
386 // the string matches the "[-+]?[0-9]" regex.
387 //
388 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
389 ((StrVal[0] == '-' || StrVal[0] == '+') &&
Brian Gaeke87b4f072003-06-17 23:55:35 +0000390 (StrVal[1] >= '0' && StrVal[1] <= '9')))
Chris Lattner1e194682002-04-18 18:53:13 +0000391 // Reparse stringized version!
392 if (atof(StrVal.c_str()) == CFP->getValue()) {
393 Out << StrVal; return;
394 }
395
396 // Otherwise we could not reparse it to exactly the same value, so we must
397 // output the string in hexadecimal format!
398 //
399 // Behave nicely in the face of C TBAA rules... see:
400 // http://www.nullstone.com/htmls/category/aliastyp.htm
401 //
402 double Val = CFP->getValue();
403 char *Ptr = (char*)&Val;
404 assert(sizeof(double) == sizeof(uint64_t) && sizeof(double) == 8 &&
405 "assuming that double is 64 bits!");
406 Out << "0x" << utohexstr(*(uint64_t*)Ptr);
407
Chris Lattner76b2ff42004-02-15 05:55:15 +0000408 } else if (isa<ConstantAggregateZero>(CV)) {
409 Out << "zeroinitializer";
Chris Lattner1e194682002-04-18 18:53:13 +0000410 } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
411 // As a special case, print the array as a string if it is an array of
412 // ubytes or an array of sbytes with positive values.
413 //
414 const Type *ETy = CA->getType()->getElementType();
415 bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
416
417 if (ETy == Type::SByteTy)
418 for (unsigned i = 0; i < CA->getNumOperands(); ++i)
419 if (cast<ConstantSInt>(CA->getOperand(i))->getValue() < 0) {
420 isString = false;
421 break;
422 }
423
424 if (isString) {
425 Out << "c\"";
426 for (unsigned i = 0; i < CA->getNumOperands(); ++i) {
Chris Lattneraa6ff272004-06-04 23:53:20 +0000427 unsigned char C =
428 (unsigned char)cast<ConstantInt>(CA->getOperand(i))->getRawValue();
Chris Lattner1e194682002-04-18 18:53:13 +0000429
Chris Lattnere5fd3862002-07-31 23:56:44 +0000430 if (isprint(C) && C != '"' && C != '\\') {
Chris Lattner1e194682002-04-18 18:53:13 +0000431 Out << C;
432 } else {
433 Out << '\\'
434 << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'))
435 << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
436 }
437 }
438 Out << "\"";
439
440 } else { // Cannot output in string format...
Misha Brukman21bbdb92004-06-04 21:11:51 +0000441 Out << '[';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000442 if (CA->getNumOperands()) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000443 Out << ' ';
Chris Lattner1e194682002-04-18 18:53:13 +0000444 printTypeInt(Out, ETy, TypeTable);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000445 WriteAsOperandInternal(Out, CA->getOperand(0),
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000446 PrintName, TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000447 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
448 Out << ", ";
Chris Lattner1e194682002-04-18 18:53:13 +0000449 printTypeInt(Out, ETy, TypeTable);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000450 WriteAsOperandInternal(Out, CA->getOperand(i), PrintName,
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000451 TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000452 }
453 }
454 Out << " ]";
455 }
456 } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000457 Out << '{';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000458 if (CS->getNumOperands()) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000459 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000460 printTypeInt(Out, CS->getOperand(0)->getType(), TypeTable);
461
462 WriteAsOperandInternal(Out, CS->getOperand(0),
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000463 PrintName, TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000464
465 for (unsigned i = 1; i < CS->getNumOperands(); i++) {
466 Out << ", ";
467 printTypeInt(Out, CS->getOperand(i)->getType(), TypeTable);
468
469 WriteAsOperandInternal(Out, CS->getOperand(i),
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000470 PrintName, TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000471 }
472 }
473
474 Out << " }";
475 } else if (isa<ConstantPointerNull>(CV)) {
476 Out << "null";
477
Chris Lattner113f4f42002-06-25 16:13:24 +0000478 } else if (const ConstantPointerRef *PR = dyn_cast<ConstantPointerRef>(CV)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000479 WriteAsOperandInternal(Out, PR->getValue(), true, TypeTable, Machine);
Vikram S. Adveb952b542002-07-14 23:14:45 +0000480
Chris Lattner3cd8c562002-07-30 18:54:25 +0000481 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Chris Lattnerb8257222003-03-06 23:23:32 +0000482 Out << CE->getOpcodeName() << " (";
Vikram S. Adveb952b542002-07-14 23:14:45 +0000483
484 for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
485 printTypeInt(Out, (*OI)->getType(), TypeTable);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000486 WriteAsOperandInternal(Out, *OI, PrintName, TypeTable, Machine);
Vikram S. Adveb952b542002-07-14 23:14:45 +0000487 if (OI+1 != CE->op_end())
Chris Lattner3cd8c562002-07-30 18:54:25 +0000488 Out << ", ";
Vikram S. Adveb952b542002-07-14 23:14:45 +0000489 }
490
Chris Lattnercfe8f532002-08-16 21:17:11 +0000491 if (CE->getOpcode() == Instruction::Cast) {
Chris Lattner83b396b2002-08-15 19:37:43 +0000492 Out << " to ";
493 printTypeInt(Out, CE->getType(), TypeTable);
494 }
Misha Brukman21bbdb92004-06-04 21:11:51 +0000495 Out << ')';
Chris Lattner83b396b2002-08-15 19:37:43 +0000496
Chris Lattnerd84bb632002-04-16 21:36:08 +0000497 } else {
Vikram S. Adveb952b542002-07-14 23:14:45 +0000498 Out << "<placeholder or erroneous Constant>";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000499 }
500}
501
502
Misha Brukmanc566ca362004-03-02 00:22:19 +0000503/// WriteAsOperand - Write the name of the specified value out to the specified
504/// ostream. This can be useful when you just want to print int %reg126, not
505/// the whole instruction that generated it.
506///
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000507static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
508 bool PrintName,
509 std::map<const Type*, std::string> &TypeTable,
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000510 SlotMachine *Machine) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000511 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000512 if (PrintName && V->hasName()) {
Chris Lattner1c343042003-08-22 05:40:38 +0000513 Out << getLLVMName(V->getName());
Chris Lattnerd84bb632002-04-16 21:36:08 +0000514 } else {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000515 if (const Constant *CV = dyn_cast<Constant>(V)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000516 WriteConstantInt(Out, CV, PrintName, TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000517 } else {
518 int Slot;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000519 if (Machine) {
Reid Spencer56010e42004-05-26 21:56:09 +0000520 Slot = Machine->getSlot(V);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000521 } else {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000522 if (const Type *Ty = dyn_cast<Type>(V)) {
Chris Lattnerd84bb632002-04-16 21:36:08 +0000523 Out << Ty->getDescription();
524 return;
525 }
526
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000527 Machine = createSlotMachine(V);
528 if (Machine == 0) { Out << "BAD VALUE TYPE!"; return; }
Chris Lattnerd84bb632002-04-16 21:36:08 +0000529
Reid Spencer56010e42004-05-26 21:56:09 +0000530 Slot = Machine->getSlot(V);
531 delete Machine;
Chris Lattnerd84bb632002-04-16 21:36:08 +0000532 }
Misha Brukman21bbdb92004-06-04 21:11:51 +0000533 Out << '%' << Slot;
Chris Lattnerd84bb632002-04-16 21:36:08 +0000534 }
535 }
536}
537
538
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000539/// WriteAsOperand - Write the name of the specified value out to the specified
540/// ostream. This can be useful when you just want to print int %reg126, not
541/// the whole instruction that generated it.
542///
Chris Lattner189d19f2003-11-21 20:23:48 +0000543std::ostream &llvm::WriteAsOperand(std::ostream &Out, const Value *V,
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000544 bool PrintType, bool PrintName,
545 const Module *Context) {
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000546 std::map<const Type *, std::string> TypeNames;
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000547 if (Context == 0) Context = getModuleFromVal(V);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000548
Chris Lattner98cf1f52002-11-20 18:36:02 +0000549 if (Context)
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000550 fillTypeNameTable(Context, TypeNames);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000551
552 if (PrintType)
553 printTypeInt(Out, V->getType(), TypeNames);
554
Brian Gaekefda1f182003-11-16 23:08:27 +0000555 if (const Type *Ty = dyn_cast<Type> (V))
556 printTypeInt(Out, Ty, TypeNames);
557
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000558 WriteAsOperandInternal(Out, V, PrintName, TypeNames, 0);
Chris Lattner5e5abe32001-07-20 19:15:21 +0000559 return Out;
560}
561
Chris Lattner189d19f2003-11-21 20:23:48 +0000562namespace llvm {
Chris Lattner2e9fee42001-07-12 23:35:26 +0000563
Chris Lattnerfee714f2001-09-07 16:36:04 +0000564class AssemblyWriter {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000565 std::ostream *Out;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000566 SlotMachine &Machine;
Chris Lattner7bfee412001-10-29 16:05:51 +0000567 const Module *TheModule;
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000568 std::map<const Type *, std::string> TypeNames;
Chris Lattner8339f7d2003-10-30 23:41:03 +0000569 AssemblyAnnotationWriter *AnnotationWriter;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000570public:
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000571 inline AssemblyWriter(std::ostream &o, SlotMachine &Mac, const Module *M,
Chris Lattner8339f7d2003-10-30 23:41:03 +0000572 AssemblyAnnotationWriter *AAW)
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000573 : Out(&o), Machine(Mac), TheModule(M), AnnotationWriter(AAW) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000574
575 // If the module has a symbol table, take all global types and stuff their
576 // names into the TypeNames map.
577 //
Chris Lattnerb86620e2001-10-29 16:37:48 +0000578 fillTypeNameTable(M, TypeNames);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000579 }
580
Chris Lattner7bfee412001-10-29 16:05:51 +0000581 inline void write(const Module *M) { printModule(M); }
582 inline void write(const GlobalVariable *G) { printGlobal(G); }
Chris Lattner57698e22002-03-26 18:01:55 +0000583 inline void write(const Function *F) { printFunction(F); }
Chris Lattner7bfee412001-10-29 16:05:51 +0000584 inline void write(const BasicBlock *BB) { printBasicBlock(BB); }
Chris Lattner113f4f42002-06-25 16:13:24 +0000585 inline void write(const Instruction *I) { printInstruction(*I); }
Chris Lattner3462ae32001-12-03 22:26:30 +0000586 inline void write(const Constant *CPV) { printConstant(CPV); }
Chris Lattner7db79582001-11-07 04:21:57 +0000587 inline void write(const Type *Ty) { printType(Ty); }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000588
Chris Lattner1e194682002-04-18 18:53:13 +0000589 void writeOperand(const Value *Op, bool PrintType, bool PrintName = true);
590
Misha Brukman4685e262004-04-28 15:31:21 +0000591 const Module* getModule() { return TheModule; }
Misha Brukmanda546ea2004-04-28 19:24:28 +0000592 void setStream(std::ostream &os) { Out = &os; }
Misha Brukman4685e262004-04-28 15:31:21 +0000593
Chris Lattner2f7c9632001-06-06 20:29:01 +0000594private :
Chris Lattner7bfee412001-10-29 16:05:51 +0000595 void printModule(const Module *M);
596 void printSymbolTable(const SymbolTable &ST);
Chris Lattner3462ae32001-12-03 22:26:30 +0000597 void printConstant(const Constant *CPV);
Chris Lattner7bfee412001-10-29 16:05:51 +0000598 void printGlobal(const GlobalVariable *GV);
Chris Lattner57698e22002-03-26 18:01:55 +0000599 void printFunction(const Function *F);
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000600 void printArgument(const Argument *FA);
Chris Lattner7bfee412001-10-29 16:05:51 +0000601 void printBasicBlock(const BasicBlock *BB);
Chris Lattner113f4f42002-06-25 16:13:24 +0000602 void printInstruction(const Instruction &I);
Chris Lattnerd816b532002-04-13 20:53:41 +0000603
604 // printType - Go to extreme measures to attempt to print out a short,
605 // symbolic version of a type name.
606 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000607 std::ostream &printType(const Type *Ty) {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000608 return printTypeInt(*Out, Ty, TypeNames);
Chris Lattnerd816b532002-04-13 20:53:41 +0000609 }
610
611 // printTypeAtLeastOneLevel - Print out one level of the possibly complex type
612 // without considering any symbolic types that we may have equal to it.
613 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000614 std::ostream &printTypeAtLeastOneLevel(const Type *Ty);
Chris Lattner7bfee412001-10-29 16:05:51 +0000615
Chris Lattner862e3382001-10-13 06:42:36 +0000616 // printInfoComment - Print a little comment after the instruction indicating
617 // which slot it occupies.
Chris Lattner113f4f42002-06-25 16:13:24 +0000618 void printInfoComment(const Value &V);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000619};
Reid Spencerf43ac622004-05-27 22:04:46 +0000620} // end of llvm namespace
Chris Lattner2f7c9632001-06-06 20:29:01 +0000621
Misha Brukmanc566ca362004-03-02 00:22:19 +0000622/// printTypeAtLeastOneLevel - Print out one level of the possibly complex type
623/// without considering any symbolic types that we may have equal to it.
624///
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000625std::ostream &AssemblyWriter::printTypeAtLeastOneLevel(const Type *Ty) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000626 if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000627 printType(FTy->getReturnType()) << " (";
Chris Lattnerfa829be2004-02-09 04:14:01 +0000628 for (FunctionType::param_iterator I = FTy->param_begin(),
629 E = FTy->param_end(); I != E; ++I) {
630 if (I != FTy->param_begin())
Misha Brukmanda546ea2004-04-28 19:24:28 +0000631 *Out << ", ";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000632 printType(*I);
Chris Lattnerd816b532002-04-13 20:53:41 +0000633 }
634 if (FTy->isVarArg()) {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000635 if (FTy->getNumParams()) *Out << ", ";
636 *Out << "...";
Chris Lattnerd816b532002-04-13 20:53:41 +0000637 }
Misha Brukman21bbdb92004-06-04 21:11:51 +0000638 *Out << ')';
Chris Lattner113f4f42002-06-25 16:13:24 +0000639 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000640 *Out << "{ ";
Chris Lattnerac6db752004-02-09 04:37:31 +0000641 for (StructType::element_iterator I = STy->element_begin(),
642 E = STy->element_end(); I != E; ++I) {
643 if (I != STy->element_begin())
Misha Brukmanda546ea2004-04-28 19:24:28 +0000644 *Out << ", ";
Chris Lattnerd816b532002-04-13 20:53:41 +0000645 printType(*I);
646 }
Misha Brukmanda546ea2004-04-28 19:24:28 +0000647 *Out << " }";
Chris Lattner113f4f42002-06-25 16:13:24 +0000648 } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000649 printType(PTy->getElementType()) << '*';
Chris Lattner113f4f42002-06-25 16:13:24 +0000650 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000651 *Out << '[' << ATy->getNumElements() << " x ";
652 printType(ATy->getElementType()) << ']';
Chris Lattner113f4f42002-06-25 16:13:24 +0000653 } else if (const OpaqueType *OTy = dyn_cast<OpaqueType>(Ty)) {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000654 *Out << "opaque";
Chris Lattnerd816b532002-04-13 20:53:41 +0000655 } else {
Vikram S. Adveb952b542002-07-14 23:14:45 +0000656 if (!Ty->isPrimitiveType())
Misha Brukmanda546ea2004-04-28 19:24:28 +0000657 *Out << "<unknown derived type>";
Chris Lattnerd816b532002-04-13 20:53:41 +0000658 printType(Ty);
659 }
Misha Brukmanda546ea2004-04-28 19:24:28 +0000660 return *Out;
Chris Lattnerd816b532002-04-13 20:53:41 +0000661}
662
663
Chris Lattnerfee714f2001-09-07 16:36:04 +0000664void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType,
Reid Spencer56010e42004-05-26 21:56:09 +0000665 bool PrintName) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000666 if (PrintType) { *Out << ' '; printType(Operand->getType()); }
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000667 WriteAsOperandInternal(*Out, Operand, PrintName, TypeNames, &Machine);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000668}
669
Chris Lattner2f7c9632001-06-06 20:29:01 +0000670
Chris Lattner7bfee412001-10-29 16:05:51 +0000671void AssemblyWriter::printModule(const Module *M) {
Chris Lattner8068e0c2003-08-24 13:48:48 +0000672 switch (M->getEndianness()) {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000673 case Module::LittleEndian: *Out << "target endian = little\n"; break;
674 case Module::BigEndian: *Out << "target endian = big\n"; break;
Chris Lattner8068e0c2003-08-24 13:48:48 +0000675 case Module::AnyEndianness: break;
676 }
677 switch (M->getPointerSize()) {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000678 case Module::Pointer32: *Out << "target pointersize = 32\n"; break;
679 case Module::Pointer64: *Out << "target pointersize = 64\n"; break;
Chris Lattner8068e0c2003-08-24 13:48:48 +0000680 case Module::AnyPointerSize: break;
681 }
682
Chris Lattnerfee714f2001-09-07 16:36:04 +0000683 // Loop over the symbol table, emitting all named constants...
Chris Lattner98cf1f52002-11-20 18:36:02 +0000684 printSymbolTable(M->getSymbolTable());
Chris Lattnerda975502001-09-10 07:58:01 +0000685
Chris Lattner113f4f42002-06-25 16:13:24 +0000686 for (Module::const_giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
687 printGlobal(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000688
Misha Brukmanda546ea2004-04-28 19:24:28 +0000689 *Out << "\nimplementation ; Functions:\n";
Vikram S. Adve13ba19a2001-09-18 12:48:16 +0000690
Chris Lattner6915f8f2002-04-07 22:49:37 +0000691 // Output all of the functions...
Chris Lattner113f4f42002-06-25 16:13:24 +0000692 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
693 printFunction(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000694}
695
Chris Lattner7bfee412001-10-29 16:05:51 +0000696void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000697 if (GV->hasName()) *Out << getLLVMName(GV->getName()) << " = ";
Chris Lattner37798642001-09-18 04:01:05 +0000698
Chris Lattner379a8d22003-04-16 20:28:45 +0000699 if (!GV->hasInitializer())
Misha Brukmanda546ea2004-04-28 19:24:28 +0000700 *Out << "external ";
Chris Lattner379a8d22003-04-16 20:28:45 +0000701 else
702 switch (GV->getLinkage()) {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000703 case GlobalValue::InternalLinkage: *Out << "internal "; break;
704 case GlobalValue::LinkOnceLinkage: *Out << "linkonce "; break;
705 case GlobalValue::WeakLinkage: *Out << "weak "; break;
706 case GlobalValue::AppendingLinkage: *Out << "appending "; break;
Chris Lattner379a8d22003-04-16 20:28:45 +0000707 case GlobalValue::ExternalLinkage: break;
708 }
Chris Lattner37798642001-09-18 04:01:05 +0000709
Misha Brukmanda546ea2004-04-28 19:24:28 +0000710 *Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattner2413b162001-12-04 00:03:30 +0000711 printType(GV->getType()->getElementType());
Chris Lattner37798642001-09-18 04:01:05 +0000712
713 if (GV->hasInitializer())
714 writeOperand(GV->getInitializer(), false, false);
715
Chris Lattner113f4f42002-06-25 16:13:24 +0000716 printInfoComment(*GV);
Misha Brukmanda546ea2004-04-28 19:24:28 +0000717 *Out << "\n";
Chris Lattnerda975502001-09-10 07:58:01 +0000718}
719
Chris Lattnerfee714f2001-09-07 16:36:04 +0000720
Reid Spencere7e96712004-05-25 08:53:40 +0000721// printSymbolTable - Run through symbol table looking for constants
722// and types. Emit their declarations.
Chris Lattner7bfee412001-10-29 16:05:51 +0000723void AssemblyWriter::printSymbolTable(const SymbolTable &ST) {
Reid Spencere7e96712004-05-25 08:53:40 +0000724
725 // Print the types.
726 for (SymbolTable::type_const_iterator TI = ST.type_begin();
727 TI != ST.type_end(); ++TI ) {
728 *Out << "\t" << getLLVMName(TI->first) << " = type ";
729
730 // Make sure we print out at least one level of the type structure, so
731 // that we do not get %FILE = type %FILE
732 //
733 printTypeAtLeastOneLevel(TI->second) << "\n";
734 }
Chris Lattnerfee714f2001-09-07 16:36:04 +0000735
Reid Spencere7e96712004-05-25 08:53:40 +0000736 // Print the constants, in type plane order.
737 for (SymbolTable::plane_const_iterator PI = ST.plane_begin();
738 PI != ST.plane_end(); ++PI ) {
739 SymbolTable::value_const_iterator VI = ST.value_begin(PI->first);
740 SymbolTable::value_const_iterator VE = ST.value_end(PI->first);
741
742 for (; VI != VE; ++VI) {
743 const Value *V = VI->second;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000744 if (const Constant *CPV = dyn_cast<Constant>(V)) {
Reid Spencer56010e42004-05-26 21:56:09 +0000745 printConstant(CPV);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000746 }
747 }
Chris Lattnera7620d92001-07-15 06:35:59 +0000748 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000749}
750
751
Misha Brukmanc566ca362004-03-02 00:22:19 +0000752/// printConstant - Print out a constant pool entry...
753///
Chris Lattner3462ae32001-12-03 22:26:30 +0000754void AssemblyWriter::printConstant(const Constant *CPV) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000755 // Don't print out unnamed constants, they will be inlined
756 if (!CPV->hasName()) return;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000757
Chris Lattneree998be2001-07-26 16:29:38 +0000758 // Print out name...
Misha Brukmanda546ea2004-04-28 19:24:28 +0000759 *Out << "\t" << getLLVMName(CPV->getName()) << " =";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000760
761 // Write the value out now...
Chris Lattnerd84bb632002-04-16 21:36:08 +0000762 writeOperand(CPV, true, false);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000763
Chris Lattner113f4f42002-06-25 16:13:24 +0000764 printInfoComment(*CPV);
Misha Brukmanda546ea2004-04-28 19:24:28 +0000765 *Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000766}
767
Misha Brukmanc566ca362004-03-02 00:22:19 +0000768/// printFunction - Print all aspects of a function.
769///
Chris Lattner113f4f42002-06-25 16:13:24 +0000770void AssemblyWriter::printFunction(const Function *F) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000771 // Print out the return type and name...
Misha Brukmanda546ea2004-04-28 19:24:28 +0000772 *Out << "\n";
Chris Lattner379a8d22003-04-16 20:28:45 +0000773
Misha Brukmanda546ea2004-04-28 19:24:28 +0000774 if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, *Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +0000775
Chris Lattner379a8d22003-04-16 20:28:45 +0000776 if (F->isExternal())
Misha Brukmanda546ea2004-04-28 19:24:28 +0000777 *Out << "declare ";
Chris Lattner379a8d22003-04-16 20:28:45 +0000778 else
779 switch (F->getLinkage()) {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000780 case GlobalValue::InternalLinkage: *Out << "internal "; break;
781 case GlobalValue::LinkOnceLinkage: *Out << "linkonce "; break;
782 case GlobalValue::WeakLinkage: *Out << "weak "; break;
783 case GlobalValue::AppendingLinkage: *Out << "appending "; break;
Chris Lattner379a8d22003-04-16 20:28:45 +0000784 case GlobalValue::ExternalLinkage: break;
785 }
786
Misha Brukman21bbdb92004-06-04 21:11:51 +0000787 printType(F->getReturnType()) << ' ';
Chris Lattner5b337482003-10-18 05:57:43 +0000788 if (!F->getName().empty())
Misha Brukmanda546ea2004-04-28 19:24:28 +0000789 *Out << getLLVMName(F->getName());
Chris Lattner5b337482003-10-18 05:57:43 +0000790 else
Misha Brukmanda546ea2004-04-28 19:24:28 +0000791 *Out << "\"\"";
Misha Brukman21bbdb92004-06-04 21:11:51 +0000792 *Out << '(';
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000793 Machine.incorporateFunction(F);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000794
Chris Lattner7bfee412001-10-29 16:05:51 +0000795 // Loop over the arguments, printing them...
Chris Lattner113f4f42002-06-25 16:13:24 +0000796 const FunctionType *FT = F->getFunctionType();
Chris Lattnerfee714f2001-09-07 16:36:04 +0000797
Chris Lattner149376d2002-10-13 20:57:00 +0000798 for(Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I)
799 printArgument(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000800
801 // Finish printing arguments...
Chris Lattner113f4f42002-06-25 16:13:24 +0000802 if (FT->isVarArg()) {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000803 if (FT->getNumParams()) *Out << ", ";
804 *Out << "..."; // Output varargs portion of signature!
Chris Lattnerfee714f2001-09-07 16:36:04 +0000805 }
Misha Brukman21bbdb92004-06-04 21:11:51 +0000806 *Out << ')';
Chris Lattnerfee714f2001-09-07 16:36:04 +0000807
Chris Lattner113f4f42002-06-25 16:13:24 +0000808 if (F->isExternal()) {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000809 *Out << "\n";
Chris Lattnerb2f02e52002-05-06 03:00:40 +0000810 } else {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000811 *Out << " {";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000812
Chris Lattner6915f8f2002-04-07 22:49:37 +0000813 // Output all of its basic blocks... for the function
Chris Lattner113f4f42002-06-25 16:13:24 +0000814 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
815 printBasicBlock(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000816
Misha Brukmanda546ea2004-04-28 19:24:28 +0000817 *Out << "}\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000818 }
819
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000820 Machine.purgeFunction();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000821}
822
Misha Brukmanc566ca362004-03-02 00:22:19 +0000823/// printArgument - This member is called for every argument that is passed into
824/// the function. Simply print it out
825///
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000826void AssemblyWriter::printArgument(const Argument *Arg) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000827 // Insert commas as we go... the first arg doesn't get a comma
Misha Brukmanda546ea2004-04-28 19:24:28 +0000828 if (Arg != &Arg->getParent()->afront()) *Out << ", ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000829
830 // Output type...
Chris Lattner7bfee412001-10-29 16:05:51 +0000831 printType(Arg->getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000832
833 // Output name, if available...
834 if (Arg->hasName())
Misha Brukman21bbdb92004-06-04 21:11:51 +0000835 *Out << ' ' << getLLVMName(Arg->getName());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000836}
837
Misha Brukmanc566ca362004-03-02 00:22:19 +0000838/// printBasicBlock - This member is called for each basic block in a method.
839///
Chris Lattner7bfee412001-10-29 16:05:51 +0000840void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000841 if (BB->hasName()) { // Print out the label if it exists...
Misha Brukman21bbdb92004-06-04 21:11:51 +0000842 *Out << "\n" << BB->getName() << ':';
Chris Lattner408dbdb2002-05-14 16:02:05 +0000843 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000844 *Out << "\n; <label>:" << Machine.getSlot(BB);
Chris Lattner58185f22002-10-02 19:38:55 +0000845 }
Chris Lattner2447ef52003-11-20 00:09:43 +0000846
847 if (BB->getParent() == 0)
Misha Brukmanda546ea2004-04-28 19:24:28 +0000848 *Out << "\t\t; Error: Block without parent!";
Chris Lattner2447ef52003-11-20 00:09:43 +0000849 else {
850 if (BB != &BB->getParent()->front()) { // Not the entry block?
851 // Output predecessors for the block...
Misha Brukmanda546ea2004-04-28 19:24:28 +0000852 *Out << "\t\t;";
Chris Lattner2447ef52003-11-20 00:09:43 +0000853 pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
854
855 if (PI == PE) {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000856 *Out << " No predecessors!";
Chris Lattner2447ef52003-11-20 00:09:43 +0000857 } else {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000858 *Out << " preds =";
Chris Lattner00211f12003-11-16 22:59:57 +0000859 writeOperand(*PI, false, true);
Chris Lattner2447ef52003-11-20 00:09:43 +0000860 for (++PI; PI != PE; ++PI) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000861 *Out << ',';
Chris Lattner2447ef52003-11-20 00:09:43 +0000862 writeOperand(*PI, false, true);
863 }
Chris Lattner00211f12003-11-16 22:59:57 +0000864 }
Chris Lattner58185f22002-10-02 19:38:55 +0000865 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000866 }
Chris Lattner408dbdb2002-05-14 16:02:05 +0000867
Misha Brukmanda546ea2004-04-28 19:24:28 +0000868 *Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000869
Misha Brukmanda546ea2004-04-28 19:24:28 +0000870 if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, *Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +0000871
Chris Lattnerfee714f2001-09-07 16:36:04 +0000872 // Output all of the instructions in the basic block...
Chris Lattner113f4f42002-06-25 16:13:24 +0000873 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
874 printInstruction(*I);
Chris Lattner96cdd272004-03-08 18:51:45 +0000875
Misha Brukmanda546ea2004-04-28 19:24:28 +0000876 if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, *Out);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000877}
878
Chris Lattner862e3382001-10-13 06:42:36 +0000879
Misha Brukmanc566ca362004-03-02 00:22:19 +0000880/// printInfoComment - Print a little comment after the instruction indicating
881/// which slot it occupies.
882///
Chris Lattner113f4f42002-06-25 16:13:24 +0000883void AssemblyWriter::printInfoComment(const Value &V) {
884 if (V.getType() != Type::VoidTy) {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000885 *Out << "\t\t; <";
Misha Brukman21bbdb92004-06-04 21:11:51 +0000886 printType(V.getType()) << '>';
Chris Lattner862e3382001-10-13 06:42:36 +0000887
Chris Lattner113f4f42002-06-25 16:13:24 +0000888 if (!V.hasName()) {
Reid Spencer8beac692004-06-09 15:26:53 +0000889 unsigned SlotNum = Machine.getSlot(&V,true);
890 if ( unsigned(-1) == SlotNum )
891 *Out << ":??";
892 else
893 *Out << ':' << SlotNum; // Print out the def slot taken.
Chris Lattner862e3382001-10-13 06:42:36 +0000894 }
Misha Brukman21bbdb92004-06-04 21:11:51 +0000895 *Out << " [#uses=" << V.use_size() << ']'; // Output # uses
Chris Lattner862e3382001-10-13 06:42:36 +0000896 }
897}
898
Reid Spencer8beac692004-06-09 15:26:53 +0000899/// printInstruction - This member is called for each Instruction in a function..
Misha Brukmanc566ca362004-03-02 00:22:19 +0000900///
Chris Lattner113f4f42002-06-25 16:13:24 +0000901void AssemblyWriter::printInstruction(const Instruction &I) {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000902 if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, *Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +0000903
Misha Brukmanda546ea2004-04-28 19:24:28 +0000904 *Out << "\t";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000905
906 // Print out name if it exists...
Chris Lattner113f4f42002-06-25 16:13:24 +0000907 if (I.hasName())
Misha Brukmanda546ea2004-04-28 19:24:28 +0000908 *Out << getLLVMName(I.getName()) << " = ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000909
Chris Lattner504f9242003-09-08 17:45:59 +0000910 // If this is a volatile load or store, print out the volatile marker
911 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
912 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile()))
Misha Brukmanda546ea2004-04-28 19:24:28 +0000913 *Out << "volatile ";
Chris Lattner504f9242003-09-08 17:45:59 +0000914
Chris Lattner2f7c9632001-06-06 20:29:01 +0000915 // Print out the opcode...
Misha Brukmanda546ea2004-04-28 19:24:28 +0000916 *Out << I.getOpcodeName();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000917
918 // Print out the type of the operands...
Chris Lattner113f4f42002-06-25 16:13:24 +0000919 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000920
921 // Special case conditional branches to swizzle the condition out to the front
Chris Lattner113f4f42002-06-25 16:13:24 +0000922 if (isa<BranchInst>(I) && I.getNumOperands() > 1) {
923 writeOperand(I.getOperand(2), true);
Misha Brukman21bbdb92004-06-04 21:11:51 +0000924 *Out << ',';
Chris Lattner2f7c9632001-06-06 20:29:01 +0000925 writeOperand(Operand, true);
Misha Brukman21bbdb92004-06-04 21:11:51 +0000926 *Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +0000927 writeOperand(I.getOperand(1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000928
Chris Lattner8d48df22002-04-13 18:34:38 +0000929 } else if (isa<SwitchInst>(I)) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000930 // Special case switch statement to get formatting nice and correct...
Misha Brukman21bbdb92004-06-04 21:11:51 +0000931 writeOperand(Operand , true); *Out << ',';
Misha Brukmanda546ea2004-04-28 19:24:28 +0000932 writeOperand(I.getOperand(1), true); *Out << " [";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000933
Chris Lattner113f4f42002-06-25 16:13:24 +0000934 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000935 *Out << "\n\t\t";
Misha Brukman21bbdb92004-06-04 21:11:51 +0000936 writeOperand(I.getOperand(op ), true); *Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +0000937 writeOperand(I.getOperand(op+1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000938 }
Misha Brukmanda546ea2004-04-28 19:24:28 +0000939 *Out << "\n\t]";
Chris Lattnerda558102001-10-02 03:41:24 +0000940 } else if (isa<PHINode>(I)) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000941 *Out << ' ';
Chris Lattner113f4f42002-06-25 16:13:24 +0000942 printType(I.getType());
Misha Brukman21bbdb92004-06-04 21:11:51 +0000943 *Out << ' ';
Chris Lattner2f7c9632001-06-06 20:29:01 +0000944
Chris Lattner113f4f42002-06-25 16:13:24 +0000945 for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000946 if (op) *Out << ", ";
Misha Brukman21bbdb92004-06-04 21:11:51 +0000947 *Out << '[';
948 writeOperand(I.getOperand(op ), false); *Out << ',';
Misha Brukmanda546ea2004-04-28 19:24:28 +0000949 writeOperand(I.getOperand(op+1), false); *Out << " ]";
Chris Lattner931ef3b2001-06-11 15:04:20 +0000950 }
Chris Lattner862e3382001-10-13 06:42:36 +0000951 } else if (isa<ReturnInst>(I) && !Operand) {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000952 *Out << " void";
Chris Lattner862e3382001-10-13 06:42:36 +0000953 } else if (isa<CallInst>(I)) {
Chris Lattner463d6a52003-08-05 15:34:45 +0000954 const PointerType *PTy = cast<PointerType>(Operand->getType());
955 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
956 const Type *RetTy = FTy->getReturnType();
Chris Lattner2f2d9472001-11-06 21:28:12 +0000957
Chris Lattner463d6a52003-08-05 15:34:45 +0000958 // If possible, print out the short form of the call instruction. We can
Chris Lattner6915f8f2002-04-07 22:49:37 +0000959 // only do this if the first argument is a pointer to a nonvararg function,
Chris Lattner463d6a52003-08-05 15:34:45 +0000960 // and if the return type is not a pointer to a function.
Chris Lattner2f2d9472001-11-06 21:28:12 +0000961 //
Chris Lattner463d6a52003-08-05 15:34:45 +0000962 if (!FTy->isVarArg() &&
Chris Lattner8d48df22002-04-13 18:34:38 +0000963 (!isa<PointerType>(RetTy) ||
Chris Lattnerd9a36a62002-07-25 20:58:51 +0000964 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000965 *Out << ' '; printType(RetTy);
Chris Lattner2f2d9472001-11-06 21:28:12 +0000966 writeOperand(Operand, false);
967 } else {
968 writeOperand(Operand, true);
969 }
Misha Brukman21bbdb92004-06-04 21:11:51 +0000970 *Out << '(';
Chris Lattner113f4f42002-06-25 16:13:24 +0000971 if (I.getNumOperands() > 1) writeOperand(I.getOperand(1), true);
972 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; ++op) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000973 *Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +0000974 writeOperand(I.getOperand(op), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000975 }
976
Misha Brukmanda546ea2004-04-28 19:24:28 +0000977 *Out << " )";
Chris Lattner113f4f42002-06-25 16:13:24 +0000978 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Chris Lattner463d6a52003-08-05 15:34:45 +0000979 const PointerType *PTy = cast<PointerType>(Operand->getType());
980 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
981 const Type *RetTy = FTy->getReturnType();
982
983 // If possible, print out the short form of the invoke instruction. We can
984 // only do this if the first argument is a pointer to a nonvararg function,
985 // and if the return type is not a pointer to a function.
986 //
987 if (!FTy->isVarArg() &&
988 (!isa<PointerType>(RetTy) ||
989 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000990 *Out << ' '; printType(RetTy);
Chris Lattner463d6a52003-08-05 15:34:45 +0000991 writeOperand(Operand, false);
992 } else {
993 writeOperand(Operand, true);
994 }
995
Misha Brukman21bbdb92004-06-04 21:11:51 +0000996 *Out << '(';
Chris Lattner113f4f42002-06-25 16:13:24 +0000997 if (I.getNumOperands() > 3) writeOperand(I.getOperand(3), true);
998 for (unsigned op = 4, Eop = I.getNumOperands(); op < Eop; ++op) {
Misha Brukman21bbdb92004-06-04 21:11:51 +0000999 *Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001000 writeOperand(I.getOperand(op), true);
Chris Lattner862e3382001-10-13 06:42:36 +00001001 }
1002
Misha Brukmanda546ea2004-04-28 19:24:28 +00001003 *Out << " )\n\t\t\tto";
Chris Lattner862e3382001-10-13 06:42:36 +00001004 writeOperand(II->getNormalDest(), true);
Misha Brukmanda546ea2004-04-28 19:24:28 +00001005 *Out << " unwind";
Chris Lattnerfae8ab32004-02-08 21:44:31 +00001006 writeOperand(II->getUnwindDest(), true);
Chris Lattner862e3382001-10-13 06:42:36 +00001007
Chris Lattner113f4f42002-06-25 16:13:24 +00001008 } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(&I)) {
Misha Brukman21bbdb92004-06-04 21:11:51 +00001009 *Out << ' ';
Chris Lattner8d48df22002-04-13 18:34:38 +00001010 printType(AI->getType()->getElementType());
1011 if (AI->isArrayAllocation()) {
Misha Brukman21bbdb92004-06-04 21:11:51 +00001012 *Out << ',';
Chris Lattner8d48df22002-04-13 18:34:38 +00001013 writeOperand(AI->getArraySize(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001014 }
Chris Lattner862e3382001-10-13 06:42:36 +00001015 } else if (isa<CastInst>(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 << " to ";
Chris Lattner113f4f42002-06-25 16:13:24 +00001018 printType(I.getType());
Chris Lattner5b337482003-10-18 05:57:43 +00001019 } else if (isa<VAArgInst>(I)) {
Chris Lattnerc96e96b2003-11-17 01:17:04 +00001020 if (Operand) writeOperand(Operand, true); // Work with broken code
Misha Brukmanda546ea2004-04-28 19:24:28 +00001021 *Out << ", ";
Chris Lattnerf70da102003-05-08 02:44:12 +00001022 printType(I.getType());
Chris Lattner5b337482003-10-18 05:57:43 +00001023 } else if (const VANextInst *VAN = dyn_cast<VANextInst>(&I)) {
Chris Lattnerc96e96b2003-11-17 01:17:04 +00001024 if (Operand) writeOperand(Operand, true); // Work with broken code
Misha Brukmanda546ea2004-04-28 19:24:28 +00001025 *Out << ", ";
Chris Lattner5b337482003-10-18 05:57:43 +00001026 printType(VAN->getArgType());
Chris Lattner2f7c9632001-06-06 20:29:01 +00001027 } else if (Operand) { // Print the normal way...
1028
1029 // PrintAllTypes - Instructions who have operands of all the same type
1030 // omit the type from all but the first operand. If the instruction has
1031 // different type operands (for example br), then they are all printed.
1032 bool PrintAllTypes = false;
1033 const Type *TheType = Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001034
Chris Lattner52bd5cb92004-03-12 05:53:14 +00001035 // Shift Left & Right print both types even for Ubyte LHS, and select prints
1036 // types even if all operands are bools.
1037 if (isa<ShiftInst>(I) || isa<SelectInst>(I)) {
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00001038 PrintAllTypes = true;
1039 } else {
1040 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
1041 Operand = I.getOperand(i);
1042 if (Operand->getType() != TheType) {
1043 PrintAllTypes = true; // We have differing types! Print them all!
1044 break;
1045 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001046 }
1047 }
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00001048
Chris Lattner7bfee412001-10-29 16:05:51 +00001049 if (!PrintAllTypes) {
Misha Brukman21bbdb92004-06-04 21:11:51 +00001050 *Out << ' ';
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00001051 printType(TheType);
Chris Lattner7bfee412001-10-29 16:05:51 +00001052 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001053
Chris Lattner113f4f42002-06-25 16:13:24 +00001054 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Misha Brukman21bbdb92004-06-04 21:11:51 +00001055 if (i) *Out << ',';
Chris Lattner113f4f42002-06-25 16:13:24 +00001056 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001057 }
1058 }
1059
Chris Lattner862e3382001-10-13 06:42:36 +00001060 printInfoComment(I);
Misha Brukmanda546ea2004-04-28 19:24:28 +00001061 *Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001062}
1063
1064
1065//===----------------------------------------------------------------------===//
1066// External Interface declarations
1067//===----------------------------------------------------------------------===//
1068
Chris Lattner8339f7d2003-10-30 23:41:03 +00001069void Module::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001070 SlotMachine SlotTable(this);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001071 AssemblyWriter W(o, SlotTable, this, AAW);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001072 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001073}
1074
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001075void GlobalVariable::print(std::ostream &o) const {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001076 SlotMachine SlotTable(getParent());
Chris Lattner8339f7d2003-10-30 23:41:03 +00001077 AssemblyWriter W(o, SlotTable, getParent(), 0);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001078 W.write(this);
Chris Lattneradfe0d12001-09-10 20:08:19 +00001079}
1080
Chris Lattner8339f7d2003-10-30 23:41:03 +00001081void Function::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001082 SlotMachine SlotTable(getParent());
Chris Lattner8339f7d2003-10-30 23:41:03 +00001083 AssemblyWriter W(o, SlotTable, getParent(), AAW);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001084
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001085 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001086}
1087
Chris Lattner8339f7d2003-10-30 23:41:03 +00001088void BasicBlock::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001089 SlotMachine SlotTable(getParent());
Chris Lattner7bfee412001-10-29 16:05:51 +00001090 AssemblyWriter W(o, SlotTable,
Chris Lattner8339f7d2003-10-30 23:41:03 +00001091 getParent() ? getParent()->getParent() : 0, AAW);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001092 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001093}
1094
Chris Lattner8339f7d2003-10-30 23:41:03 +00001095void Instruction::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001096 const Function *F = getParent() ? getParent()->getParent() : 0;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001097 SlotMachine SlotTable(F);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001098 AssemblyWriter W(o, SlotTable, F ? F->getParent() : 0, AAW);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001099
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001100 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001101}
Chris Lattner7db79582001-11-07 04:21:57 +00001102
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001103void Constant::print(std::ostream &o) const {
1104 if (this == 0) { o << "<null> constant value\n"; return; }
Chris Lattner7d734802002-09-10 15:53:49 +00001105
1106 // Handle CPR's special, because they have context information...
1107 if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(this)) {
1108 CPR->getValue()->print(o); // Print as a global value, with context info.
1109 return;
1110 }
1111
Misha Brukman21bbdb92004-06-04 21:11:51 +00001112 o << ' ' << getType()->getDescription() << ' ';
Chris Lattner1e194682002-04-18 18:53:13 +00001113
Chris Lattnerab7d1ab2003-05-08 02:08:14 +00001114 std::map<const Type *, std::string> TypeTable;
Chris Lattner1e194682002-04-18 18:53:13 +00001115 WriteConstantInt(o, this, false, TypeTable, 0);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001116}
1117
1118void Type::print(std::ostream &o) const {
1119 if (this == 0)
1120 o << "<null Type>";
1121 else
1122 o << getDescription();
1123}
1124
Chris Lattner2e9fa6d2002-04-09 19:48:49 +00001125void Argument::print(std::ostream &o) const {
Misha Brukman21bbdb92004-06-04 21:11:51 +00001126 o << getType() << ' ' << getName();
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001127}
1128
Reid Spencer52641832004-05-25 18:14:38 +00001129// Value::dump - allow easy printing of Values from the debugger.
1130// Located here because so much of the needed functionality is here.
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001131void Value::dump() const { print(std::cerr); }
Reid Spencer52641832004-05-25 18:14:38 +00001132
1133// Type::dump - allow easy printing of Values from the debugger.
1134// Located here because so much of the needed functionality is here.
Reid Spencere7e96712004-05-25 08:53:40 +00001135void Type::dump() const { print(std::cerr); }
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001136
1137//===----------------------------------------------------------------------===//
1138// CachedWriter Class Implementation
1139//===----------------------------------------------------------------------===//
1140
Chris Lattner7db79582001-11-07 04:21:57 +00001141void CachedWriter::setModule(const Module *M) {
1142 delete SC; delete AW;
1143 if (M) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001144 SC = new SlotMachine(M );
Misha Brukman21bbdb92004-06-04 21:11:51 +00001145 AW = new AssemblyWriter(Out, *SC, M, 0);
Chris Lattner7db79582001-11-07 04:21:57 +00001146 } else {
1147 SC = 0; AW = 0;
1148 }
1149}
1150
1151CachedWriter::~CachedWriter() {
1152 delete AW;
1153 delete SC;
1154}
1155
1156CachedWriter &CachedWriter::operator<<(const Value *V) {
1157 assert(AW && SC && "CachedWriter does not have a current module!");
1158 switch (V->getValueType()) {
1159 case Value::ConstantVal:
Chris Lattner1e194682002-04-18 18:53:13 +00001160 case Value::ArgumentVal: AW->writeOperand(V, true, true); break;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +00001161 case Value::TypeVal: AW->write(cast<Type>(V)); break;
Chris Lattner7db79582001-11-07 04:21:57 +00001162 case Value::InstructionVal: AW->write(cast<Instruction>(V)); break;
1163 case Value::BasicBlockVal: AW->write(cast<BasicBlock>(V)); break;
Chris Lattner57698e22002-03-26 18:01:55 +00001164 case Value::FunctionVal: AW->write(cast<Function>(V)); break;
Chris Lattner7db79582001-11-07 04:21:57 +00001165 case Value::GlobalVariableVal: AW->write(cast<GlobalVariable>(V)); break;
Misha Brukman21bbdb92004-06-04 21:11:51 +00001166 default: Out << "<unknown value type: " << V->getValueType() << '>'; break;
Chris Lattner7db79582001-11-07 04:21:57 +00001167 }
1168 return *this;
1169}
Misha Brukman4685e262004-04-28 15:31:21 +00001170
1171CachedWriter& CachedWriter::operator<<(const Type *X) {
1172 if (SymbolicTypes) {
1173 const Module *M = AW->getModule();
Misha Brukman21bbdb92004-06-04 21:11:51 +00001174 if (M) WriteTypeSymbolic(Out, X, M);
Misha Brukman4685e262004-04-28 15:31:21 +00001175 return *this;
1176 } else
1177 return *this << (const Value*)X;
1178}
Misha Brukmanda546ea2004-04-28 19:24:28 +00001179
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001180//===----------------------------------------------------------------------===//
1181//===-- SlotMachine Implementation
1182//===----------------------------------------------------------------------===//
1183
1184#if 0
1185#define SC_DEBUG(X) std::cerr << X
1186#else
1187#define SC_DEBUG(X)
1188#endif
1189
1190// Module level constructor. Causes the contents of the Module (sans functions)
1191// to be added to the slot table.
1192SlotMachine::SlotMachine(const Module *M)
Reid Spencer56010e42004-05-26 21:56:09 +00001193 : TheModule(M) ///< Saved for lazy initialization.
1194 , TheFunction(0)
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001195 , mMap()
1196 , fMap()
1197{
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001198}
1199
1200// Function level constructor. Causes the contents of the Module and the one
1201// function provided to be added to the slot table.
1202SlotMachine::SlotMachine(const Function *F )
Reid Spencer56010e42004-05-26 21:56:09 +00001203 : TheModule( F ? F->getParent() : 0 ) ///< Saved for lazy initialization
1204 , TheFunction(F) ///< Saved for lazy initialization
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001205 , mMap()
1206 , fMap()
1207{
Reid Spencer56010e42004-05-26 21:56:09 +00001208}
1209
1210inline void SlotMachine::initialize(void) {
1211 if ( TheModule) {
1212 processModule();
1213 TheModule = 0; ///< Prevent re-processing next time we're called.
1214 }
1215 if ( TheFunction ) {
1216 processFunction();
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001217 }
1218}
1219
1220// Iterate through all the global variables, functions, and global
1221// variable initializers and create slots for them.
1222void SlotMachine::processModule() {
1223 SC_DEBUG("begin processModule!\n");
1224
1225 // Add all of the global variables to the value table...
1226 for (Module::const_giterator I = TheModule->gbegin(), E = TheModule->gend();
1227 I != E; ++I)
1228 createSlot(I);
1229
1230 // Add all the functions to the table
1231 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
1232 I != E; ++I)
1233 createSlot(I);
1234
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001235 SC_DEBUG("end processModule!\n");
1236}
1237
1238
Reid Spencer56010e42004-05-26 21:56:09 +00001239// Process the arguments, basic blocks, and instructions of a function.
1240void SlotMachine::processFunction() {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001241 SC_DEBUG("begin processFunction!\n");
1242
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001243 // Add all the function arguments
Reid Spencer56010e42004-05-26 21:56:09 +00001244 for(Function::const_aiterator AI = TheFunction->abegin(),
1245 AE = TheFunction->aend(); AI != AE; ++AI)
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001246 createSlot(AI);
1247
1248 SC_DEBUG("Inserting Instructions:\n");
1249
1250 // Add all of the basic blocks and instructions
Reid Spencer56010e42004-05-26 21:56:09 +00001251 for (Function::const_iterator BB = TheFunction->begin(),
1252 E = TheFunction->end(); BB != E; ++BB) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001253 createSlot(BB);
1254 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
1255 createSlot(I);
1256 }
1257 }
1258
1259 SC_DEBUG("end processFunction!\n");
1260}
1261
1262// Clean up after incorporating a function. This is the only way
Reid Spencer56010e42004-05-26 21:56:09 +00001263// to get out of the function incorporation state that affects the
1264// getSlot/createSlot lock. Function incorporation state is indicated
1265// by TheFunction != 0.
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001266void SlotMachine::purgeFunction() {
1267 SC_DEBUG("begin purgeFunction!\n");
1268 fMap.clear(); // Simply discard the function level map
Reid Spencer56010e42004-05-26 21:56:09 +00001269 TheFunction = 0;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001270 SC_DEBUG("end purgeFunction!\n");
1271}
1272
1273/// Get the slot number for a value. This function will assert if you
1274/// ask for a Value that hasn't previously been inserted with createSlot.
1275/// Types are forbidden because Type does not inherit from Value (any more).
Reid Spencer8beac692004-06-09 15:26:53 +00001276unsigned SlotMachine::getSlot(const Value *V, bool ignoreMissing ) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001277 assert( V && "Can't get slot for null Value" );
1278 assert( !isa<Type>(V) && "Can't get slot for a type" );
Reid Spencer56010e42004-05-26 21:56:09 +00001279 assert(!isa<Constant>(V) || isa<GlobalValue>(V) &&
1280 "Can't insert a non-GlobalValue Constant into SlotMachine");
1281
1282 // Check for uninitialized state and do lazy initialization
1283 this->initialize();
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001284
1285 // Do not number CPR's at all. They are an abomination
1286 if ( const ConstantPointerRef* CPR = dyn_cast<ConstantPointerRef>(V) )
1287 V = CPR->getValue() ;
1288
1289 // Get the type of the value
1290 const Type* VTy = V->getType();
1291
1292 // Find the type plane in the module map
1293 TypedPlanes::const_iterator MI = mMap.find(VTy);
1294
Reid Spencer56010e42004-05-26 21:56:09 +00001295 if ( TheFunction ) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001296 // Lookup the type in the function map too
1297 TypedPlanes::const_iterator FI = fMap.find(VTy);
1298 // If there is a corresponding type plane in the function map
1299 if ( FI != fMap.end() ) {
1300 // Lookup the Value in the function map
1301 ValueMap::const_iterator FVI = FI->second.map.find(V);
1302 // If the value doesn't exist in the function map
1303 if ( FVI == FI->second.map.end() ) {
Reid Spencer56010e42004-05-26 21:56:09 +00001304 // Look up the value in the module map
1305 ValueMap::const_iterator MVI = MI->second.map.find(V);
1306 // If we didn't find it, it wasn't inserted
Reid Spencer8beac692004-06-09 15:26:53 +00001307 if ( ignoreMissing && MVI == MI->second.map.end()) return unsigned(-1);
Reid Spencer56010e42004-05-26 21:56:09 +00001308 assert( MVI != MI->second.map.end() && "Value not found");
1309 // We found it only at the module level
1310 return MVI->second;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001311
1312 // else the value exists in the function map
1313 } else {
Reid Spencer56010e42004-05-26 21:56:09 +00001314 // Return the slot number as the module's contribution to
1315 // the type plane plus the index in the function's contribution
1316 // to the type plane.
1317 return MI->second.next_slot + FVI->second;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001318 }
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001319 }
1320 }
1321
Reid Spencer8beac692004-06-09 15:26:53 +00001322 // N.B. Can get here only if either !TheFunction or the function doesn't
1323 // have a corresponding type plane for the Value
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001324
1325 // Make sure the type plane exists
Reid Spencer8beac692004-06-09 15:26:53 +00001326 if ( ignoreMissing && MI == mMap.end()) return unsigned(-1);
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001327 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.
Reid Spencer8beac692004-06-09 15:26:53 +00001331 if ( ignoreMissing && MVI == MI->second.map.end()) return unsigned(-1);
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001332 assert( MVI != MI->second.map.end() && "Value not found" );
1333 // Return it.
1334 return MVI->second;
1335}
1336
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001337// 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