blob: a82132fc2bbfb3e2fa99f451c68085f9fa4d63cc [file] [log] [blame]
Chris Lattner8da78af2002-04-07 22:31:46 +00001//===-- AsmWriter.cpp - Printing LLVM as an assembly file -----------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
10// This library implements the functionality defined in llvm/Assembly/Writer.h
11//
Chris Lattner02b93992002-04-12 18:21:53 +000012// Note that these routines must be extremely tolerant of various errors in the
Chris Lattner8f77dae2003-05-08 02:44:12 +000013// LLVM code, because it can be used for debugging transformations.
Chris Lattner02b93992002-04-12 18:21:53 +000014//
Chris Lattner00950542001-06-06 20:29:01 +000015//===----------------------------------------------------------------------===//
16
Chris Lattnerda1fbcc2001-11-07 04:21:57 +000017#include "llvm/Assembly/CachedWriter.h"
Chris Lattner75cf7cf2002-04-08 22:03:40 +000018#include "llvm/Assembly/Writer.h"
Chris Lattnerf082b802002-07-23 18:07:49 +000019#include "llvm/Assembly/PrintModulePass.h"
Chris Lattner95e5a2c2003-10-30 23:41:03 +000020#include "llvm/Assembly/AsmAnnotationWriter.h"
Chris Lattnerf2d577b2004-01-20 19:50:34 +000021#include "llvm/Constants.h"
Chris Lattner3eb59c02002-04-29 18:46:50 +000022#include "llvm/DerivedTypes.h"
Vikram S. Adveb4dbb442002-07-14 23:14:45 +000023#include "llvm/Instruction.h"
Chris Lattner00950542001-06-06 20:29:01 +000024#include "llvm/iMemory.h"
Chris Lattnere02fa852001-10-13 06:42:36 +000025#include "llvm/iTerminators.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000026#include "llvm/iPHINode.h"
27#include "llvm/iOther.h"
Chris Lattnerf2d577b2004-01-20 19:50:34 +000028#include "llvm/Module.h"
Chris Lattner007377f2001-09-07 16:36:04 +000029#include "llvm/SymbolTable.h"
Misha Brukman5cf1acf2004-04-28 15:31:21 +000030#include "llvm/Assembly/Writer.h"
Chris Lattner061269b2002-10-02 19:38:55 +000031#include "llvm/Support/CFG.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000032#include "Support/StringExtras.h"
33#include "Support/STLExtras.h"
Chris Lattner007377f2001-09-07 16:36:04 +000034#include <algorithm>
Chris Lattner31f84992003-11-21 20:23:48 +000035using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000036
Reid Spencer0d1b77e2004-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*
Chris Lattner69566452004-06-09 19:41:19 +000077 int getSlot(const Value *V);
Reid Spencerfc621e22004-06-09 15:26:53 +000078
79 /// Determine if a Value has a slot or not
80 bool hasSlot(const Value* V);
Reid Spencer0d1b77e2004-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 Spencerb03de0c2004-05-26 21:56:09 +000088 void incorporateFunction(const Function *F) { TheFunction = F; }
Reid Spencer0d1b77e2004-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 Spencerb03de0c2004-05-26 21:56:09 +000099 /// This function does the actual initialization.
100 inline void initialize();
101
Reid Spencer0d1b77e2004-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 Spencerb03de0c2004-05-26 21:56:09 +0000116 /// Add all of the functions arguments, basic blocks, and instructions
117 void processFunction();
118
Reid Spencer0d1b77e2004-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 Spencerb03de0c2004-05-26 21:56:09 +0000128 const Module* TheModule;
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000129
Reid Spencerb03de0c2004-05-26 21:56:09 +0000130 /// @brief The function for which we are holding slot numbers
131 const Function* TheFunction;
Reid Spencer0d1b77e2004-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 Lattnera6275cc2002-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 Lattnerf082b802002-07-23 18:07:49 +0000149
Chris Lattner7b13f562003-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 Spencer0d1b77e2004-05-26 07:18:52 +0000153 SlotMachine *Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000154
Chris Lattner207b5bc2001-10-29 16:37:48 +0000155static const Module *getModuleFromVal(const Value *V) {
Chris Lattner949a3622003-07-23 15:30:06 +0000156 if (const Argument *MA = dyn_cast<Argument>(V))
Chris Lattner207b5bc2001-10-29 16:37:48 +0000157 return MA->getParent() ? MA->getParent()->getParent() : 0;
Chris Lattner949a3622003-07-23 15:30:06 +0000158 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
Chris Lattner207b5bc2001-10-29 16:37:48 +0000159 return BB->getParent() ? BB->getParent()->getParent() : 0;
Chris Lattner949a3622003-07-23 15:30:06 +0000160 else if (const Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000161 const Function *M = I->getParent() ? I->getParent()->getParent() : 0;
Chris Lattner207b5bc2001-10-29 16:37:48 +0000162 return M ? M->getParent() : 0;
Chris Lattner949a3622003-07-23 15:30:06 +0000163 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
Chris Lattner207b5bc2001-10-29 16:37:48 +0000164 return GV->getParent();
Chris Lattner207b5bc2001-10-29 16:37:48 +0000165 return 0;
166}
167
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000168static SlotMachine *createSlotMachine(const Value *V) {
Chris Lattnerc1824992001-10-29 16:05:51 +0000169 assert(!isa<Type>(V) && "Can't create an SC for a type!");
Chris Lattner949a3622003-07-23 15:30:06 +0000170 if (const Argument *FA = dyn_cast<Argument>(V)) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000171 return new SlotMachine(FA->getParent());
Chris Lattner949a3622003-07-23 15:30:06 +0000172 } else if (const Instruction *I = dyn_cast<Instruction>(V)) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000173 return new SlotMachine(I->getParent()->getParent());
Chris Lattner949a3622003-07-23 15:30:06 +0000174 } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000175 return new SlotMachine(BB->getParent());
Chris Lattner949a3622003-07-23 15:30:06 +0000176 } else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)){
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000177 return new SlotMachine(GV->getParent());
Chris Lattner949a3622003-07-23 15:30:06 +0000178 } else if (const Function *Func = dyn_cast<Function>(V)) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000179 return new SlotMachine(Func);
Chris Lattnerc1824992001-10-29 16:05:51 +0000180 }
181 return 0;
182}
Chris Lattner00950542001-06-06 20:29:01 +0000183
Chris Lattner24b8a5d2003-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 Lattner207b5bc2001-10-29 16:37:48 +0000207
Misha Brukmanab5c6002004-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 Lattner207b5bc2001-10-29 16:37:48 +0000211static void fillTypeNameTable(const Module *M,
Chris Lattner7b13f562003-05-08 02:08:14 +0000212 std::map<const Type *, std::string> &TypeNames) {
Chris Lattner6e6026b2002-11-20 18:36:02 +0000213 if (!M) return;
214 const SymbolTable &ST = M->getSymbolTable();
Reid Spencer9231ac82004-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 Spencerb03de0c2004-05-26 21:56:09 +0000222 !cast<PointerType>(Ty)->getElementType()->isPrimitiveType() ||
223 isa<OpaqueType>(cast<PointerType>(Ty)->getElementType()))
Reid Spencer9231ac82004-05-25 08:53:40 +0000224 TypeNames.insert(std::make_pair(Ty, getLLVMName(TI->first)));
Chris Lattner207b5bc2001-10-29 16:37:48 +0000225 }
226}
227
228
229
John Criswell4ff620a2004-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 Lattner207b5bc2001-10-29 16:37:48 +0000238
239 // Check to see if the type is named.
Chris Lattner7b13f562003-05-08 02:08:14 +0000240 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
John Criswell4ff620a2004-06-01 14:54:08 +0000241 if (I != TypeNames.end()) {
242 Result += I->second;
243 return;
244 }
Chris Lattner207b5bc2001-10-29 16:37:48 +0000245
John Criswell4ff620a2004-06-01 14:54:08 +0000246 if (isa<OpaqueType>(Ty)) {
247 Result += "opaque";
248 return;
249 }
Chris Lattner88c17382003-10-30 00:22:33 +0000250
Chris Lattner207b5bc2001-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 Criswell4ff620a2004-06-01 14:54:08 +0000258 if (Slot < CurSize) {
259 Result += "\\" + utostr(CurSize-Slot); // Here's the upreference
260 return;
261 }
Chris Lattner207b5bc2001-10-29 16:37:48 +0000262
263 TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
264
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000265 switch (Ty->getTypeID()) {
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000266 case Type::FunctionTyID: {
Chris Lattner949a3622003-07-23 15:30:06 +0000267 const FunctionType *FTy = cast<FunctionType>(Ty);
John Criswell4ff620a2004-06-01 14:54:08 +0000268 calcTypeName(FTy->getReturnType(), TypeStack, TypeNames, Result);
269 Result += " (";
Chris Lattnerd5d89962004-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 Lattner207b5bc2001-10-29 16:37:48 +0000273 Result += ", ";
John Criswell4ff620a2004-06-01 14:54:08 +0000274 calcTypeName(*I, TypeStack, TypeNames, Result);
Chris Lattner207b5bc2001-10-29 16:37:48 +0000275 }
Chris Lattner2761e9f2002-04-13 20:53:41 +0000276 if (FTy->isVarArg()) {
Chris Lattnerd5d89962004-02-09 04:14:01 +0000277 if (FTy->getNumParams()) Result += ", ";
Chris Lattner207b5bc2001-10-29 16:37:48 +0000278 Result += "...";
279 }
280 Result += ")";
281 break;
282 }
283 case Type::StructTyID: {
Chris Lattner949a3622003-07-23 15:30:06 +0000284 const StructType *STy = cast<StructType>(Ty);
John Criswell4ff620a2004-06-01 14:54:08 +0000285 Result += "{ ";
Chris Lattnerd21cd802004-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 Lattner207b5bc2001-10-29 16:37:48 +0000289 Result += ", ";
John Criswell4ff620a2004-06-01 14:54:08 +0000290 calcTypeName(*I, TypeStack, TypeNames, Result);
Chris Lattner207b5bc2001-10-29 16:37:48 +0000291 }
292 Result += " }";
293 break;
294 }
295 case Type::PointerTyID:
John Criswell4ff620a2004-06-01 14:54:08 +0000296 calcTypeName(cast<PointerType>(Ty)->getElementType(),
297 TypeStack, TypeNames, Result);
298 Result += "*";
Chris Lattner207b5bc2001-10-29 16:37:48 +0000299 break;
300 case Type::ArrayTyID: {
Chris Lattner949a3622003-07-23 15:30:06 +0000301 const ArrayType *ATy = cast<ArrayType>(Ty);
John Criswell4ff620a2004-06-01 14:54:08 +0000302 Result += "[" + utostr(ATy->getNumElements()) + " x ";
303 calcTypeName(ATy->getElementType(), TypeStack, TypeNames, Result);
304 Result += "]";
Chris Lattner207b5bc2001-10-29 16:37:48 +0000305 break;
306 }
Chris Lattner9e094c42003-05-14 17:50:47 +0000307 case Type::OpaqueTyID:
John Criswell4ff620a2004-06-01 14:54:08 +0000308 Result += "opaque";
Chris Lattner9e094c42003-05-14 17:50:47 +0000309 break;
Chris Lattner207b5bc2001-10-29 16:37:48 +0000310 default:
John Criswell4ff620a2004-06-01 14:54:08 +0000311 Result += "<unrecognized-type>";
Chris Lattner207b5bc2001-10-29 16:37:48 +0000312 }
313
314 TypeStack.pop_back(); // Remove self from stack...
John Criswell4ff620a2004-06-01 14:54:08 +0000315 return;
Chris Lattner207b5bc2001-10-29 16:37:48 +0000316}
317
318
Misha Brukman9d0802e2004-03-01 19:48:13 +0000319/// printTypeInt - The internal guts of printing out a type that has a
320/// potentially named portion.
321///
Chris Lattner7b13f562003-05-08 02:08:14 +0000322static std::ostream &printTypeInt(std::ostream &Out, const Type *Ty,
323 std::map<const Type *, std::string> &TypeNames) {
Chris Lattner207b5bc2001-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 Lattnerdaf2a492003-10-30 00:12:51 +0000327 if (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty))
328 return Out << Ty->getDescription();
Chris Lattner207b5bc2001-10-29 16:37:48 +0000329
330 // Check to see if the type is named.
Chris Lattner7b13f562003-05-08 02:08:14 +0000331 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
Chris Lattner207b5bc2001-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 Lattner7b13f562003-05-08 02:08:14 +0000338 std::vector<const Type *> TypeStack;
John Criswell4ff620a2004-06-01 14:54:08 +0000339 std::string TypeName;
340 calcTypeName(Ty, TypeStack, TypeNames, TypeName);
Chris Lattner697954c2002-01-20 22:54:45 +0000341 TypeNames.insert(std::make_pair(Ty, TypeName));//Cache type name for later use
John Criswell4ff620a2004-06-01 14:54:08 +0000342 return (Out << TypeName);
Chris Lattner207b5bc2001-10-29 16:37:48 +0000343}
344
Chris Lattnere51e03b2001-10-31 04:33:19 +0000345
Misha Brukman9d0802e2004-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 Lattner31f84992003-11-21 20:23:48 +0000350std::ostream &llvm::WriteTypeSymbolic(std::ostream &Out, const Type *Ty,
351 const Module *M) {
Misha Brukman40c732c2004-06-04 21:11:51 +0000352 Out << ' ';
Chris Lattner207b5bc2001-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 Lattner6e6026b2002-11-20 18:36:02 +0000356 if (M) {
Chris Lattner7b13f562003-05-08 02:08:14 +0000357 std::map<const Type *, std::string> TypeNames;
Chris Lattner207b5bc2001-10-29 16:37:48 +0000358 fillTypeNameTable(M, TypeNames);
359
Chris Lattner7b8660d2001-10-29 16:40:32 +0000360 return printTypeInt(Out, Ty, TypeNames);
Chris Lattner207b5bc2001-10-29 16:37:48 +0000361 } else {
Chris Lattner7b8660d2001-10-29 16:40:32 +0000362 return Out << Ty->getDescription();
Chris Lattner207b5bc2001-10-29 16:37:48 +0000363 }
364}
365
Chris Lattner7b13f562003-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 Spencer0d1b77e2004-05-26 07:18:52 +0000369 SlotMachine *Machine) {
Chris Lattner66e810b2002-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 Gaekeb471a232003-06-17 23:55:35 +0000390 (StrVal[1] >= '0' && StrVal[1] <= '9')))
Chris Lattner66e810b2002-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 Lattnerde512b52004-02-15 05:55:15 +0000408 } else if (isa<ConstantAggregateZero>(CV)) {
409 Out << "zeroinitializer";
Chris Lattner66e810b2002-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 Lattner54e3e8f2004-06-04 23:53:20 +0000427 unsigned char C =
428 (unsigned char)cast<ConstantInt>(CA->getOperand(i))->getRawValue();
Chris Lattner66e810b2002-04-18 18:53:13 +0000429
Chris Lattnerfc944462002-07-31 23:56:44 +0000430 if (isprint(C) && C != '"' && C != '\\') {
Chris Lattner66e810b2002-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 Brukman40c732c2004-06-04 21:11:51 +0000441 Out << '[';
Chris Lattner7a716ad2002-04-16 21:36:08 +0000442 if (CA->getNumOperands()) {
Misha Brukman40c732c2004-06-04 21:11:51 +0000443 Out << ' ';
Chris Lattner66e810b2002-04-18 18:53:13 +0000444 printTypeInt(Out, ETy, TypeTable);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000445 WriteAsOperandInternal(Out, CA->getOperand(0),
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000446 PrintName, TypeTable, Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000447 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
448 Out << ", ";
Chris Lattner66e810b2002-04-18 18:53:13 +0000449 printTypeInt(Out, ETy, TypeTable);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000450 WriteAsOperandInternal(Out, CA->getOperand(i), PrintName,
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000451 TypeTable, Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000452 }
453 }
454 Out << " ]";
455 }
456 } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Misha Brukman40c732c2004-06-04 21:11:51 +0000457 Out << '{';
Chris Lattner7a716ad2002-04-16 21:36:08 +0000458 if (CS->getNumOperands()) {
Misha Brukman40c732c2004-06-04 21:11:51 +0000459 Out << ' ';
Chris Lattner7a716ad2002-04-16 21:36:08 +0000460 printTypeInt(Out, CS->getOperand(0)->getType(), TypeTable);
461
462 WriteAsOperandInternal(Out, CS->getOperand(0),
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000463 PrintName, TypeTable, Machine);
Chris Lattner7a716ad2002-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 Spencer0d1b77e2004-05-26 07:18:52 +0000470 PrintName, TypeTable, Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000471 }
472 }
473
474 Out << " }";
475 } else if (isa<ConstantPointerNull>(CV)) {
476 Out << "null";
477
Chris Lattner7e708292002-06-25 16:13:24 +0000478 } else if (const ConstantPointerRef *PR = dyn_cast<ConstantPointerRef>(CV)) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000479 WriteAsOperandInternal(Out, PR->getValue(), true, TypeTable, Machine);
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000480
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000481 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Chris Lattner38d87732003-03-06 23:23:32 +0000482 Out << CE->getOpcodeName() << " (";
Vikram S. Adveb4dbb442002-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 Spencer0d1b77e2004-05-26 07:18:52 +0000486 WriteAsOperandInternal(Out, *OI, PrintName, TypeTable, Machine);
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000487 if (OI+1 != CE->op_end())
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000488 Out << ", ";
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000489 }
490
Chris Lattner1c93e5b2002-08-16 21:17:11 +0000491 if (CE->getOpcode() == Instruction::Cast) {
Chris Lattner95586b82002-08-15 19:37:43 +0000492 Out << " to ";
493 printTypeInt(Out, CE->getType(), TypeTable);
494 }
Misha Brukman40c732c2004-06-04 21:11:51 +0000495 Out << ')';
Chris Lattner95586b82002-08-15 19:37:43 +0000496
Chris Lattner7a716ad2002-04-16 21:36:08 +0000497 } else {
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000498 Out << "<placeholder or erroneous Constant>";
Chris Lattner7a716ad2002-04-16 21:36:08 +0000499 }
500}
501
502
Misha Brukmanab5c6002004-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 Lattner7b13f562003-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 Spencer0d1b77e2004-05-26 07:18:52 +0000510 SlotMachine *Machine) {
Misha Brukman40c732c2004-06-04 21:11:51 +0000511 Out << ' ';
Chris Lattner7a716ad2002-04-16 21:36:08 +0000512 if (PrintName && V->hasName()) {
Chris Lattner24b8a5d2003-08-22 05:40:38 +0000513 Out << getLLVMName(V->getName());
Chris Lattner7a716ad2002-04-16 21:36:08 +0000514 } else {
Chris Lattner949a3622003-07-23 15:30:06 +0000515 if (const Constant *CV = dyn_cast<Constant>(V)) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000516 WriteConstantInt(Out, CV, PrintName, TypeTable, Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000517 } else {
518 int Slot;
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000519 if (Machine) {
Reid Spencerb03de0c2004-05-26 21:56:09 +0000520 Slot = Machine->getSlot(V);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000521 } else {
Chris Lattner949a3622003-07-23 15:30:06 +0000522 if (const Type *Ty = dyn_cast<Type>(V)) {
Chris Lattner7a716ad2002-04-16 21:36:08 +0000523 Out << Ty->getDescription();
524 return;
525 }
526
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000527 Machine = createSlotMachine(V);
Chris Lattner69566452004-06-09 19:41:19 +0000528 if (Machine == 0)
529 Slot = Machine->getSlot(V);
530 else
531 Slot = -1;
Reid Spencerb03de0c2004-05-26 21:56:09 +0000532 delete Machine;
Chris Lattner7a716ad2002-04-16 21:36:08 +0000533 }
Chris Lattner69566452004-06-09 19:41:19 +0000534 if (Slot != -1)
535 Out << '%' << Slot;
536 else
537 Out << "<badref>";
Chris Lattner7a716ad2002-04-16 21:36:08 +0000538 }
539 }
540}
541
542
Misha Brukman9d0802e2004-03-01 19:48:13 +0000543/// WriteAsOperand - Write the name of the specified value out to the specified
544/// ostream. This can be useful when you just want to print int %reg126, not
545/// the whole instruction that generated it.
546///
Chris Lattner31f84992003-11-21 20:23:48 +0000547std::ostream &llvm::WriteAsOperand(std::ostream &Out, const Value *V,
Misha Brukman9d0802e2004-03-01 19:48:13 +0000548 bool PrintType, bool PrintName,
549 const Module *Context) {
Chris Lattner7b13f562003-05-08 02:08:14 +0000550 std::map<const Type *, std::string> TypeNames;
Chris Lattner607dc682002-07-10 16:48:17 +0000551 if (Context == 0) Context = getModuleFromVal(V);
Chris Lattner207b5bc2001-10-29 16:37:48 +0000552
Chris Lattner6e6026b2002-11-20 18:36:02 +0000553 if (Context)
Chris Lattner607dc682002-07-10 16:48:17 +0000554 fillTypeNameTable(Context, TypeNames);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000555
556 if (PrintType)
557 printTypeInt(Out, V->getType(), TypeNames);
558
Brian Gaekecd4a3982003-11-16 23:08:27 +0000559 if (const Type *Ty = dyn_cast<Type> (V))
560 printTypeInt(Out, Ty, TypeNames);
561
Chris Lattner607dc682002-07-10 16:48:17 +0000562 WriteAsOperandInternal(Out, V, PrintName, TypeNames, 0);
Chris Lattner622f7402001-07-20 19:15:21 +0000563 return Out;
564}
565
Chris Lattner31f84992003-11-21 20:23:48 +0000566namespace llvm {
Chris Lattnerd8c2e422001-07-12 23:35:26 +0000567
Chris Lattner007377f2001-09-07 16:36:04 +0000568class AssemblyWriter {
Misha Brukmane5242de2004-04-28 19:24:28 +0000569 std::ostream *Out;
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000570 SlotMachine &Machine;
Chris Lattnerc1824992001-10-29 16:05:51 +0000571 const Module *TheModule;
Chris Lattner7b13f562003-05-08 02:08:14 +0000572 std::map<const Type *, std::string> TypeNames;
Chris Lattner95e5a2c2003-10-30 23:41:03 +0000573 AssemblyAnnotationWriter *AnnotationWriter;
Chris Lattner00950542001-06-06 20:29:01 +0000574public:
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000575 inline AssemblyWriter(std::ostream &o, SlotMachine &Mac, const Module *M,
Chris Lattner95e5a2c2003-10-30 23:41:03 +0000576 AssemblyAnnotationWriter *AAW)
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000577 : Out(&o), Machine(Mac), TheModule(M), AnnotationWriter(AAW) {
Chris Lattnerc1824992001-10-29 16:05:51 +0000578
579 // If the module has a symbol table, take all global types and stuff their
580 // names into the TypeNames map.
581 //
Chris Lattner207b5bc2001-10-29 16:37:48 +0000582 fillTypeNameTable(M, TypeNames);
Chris Lattner00950542001-06-06 20:29:01 +0000583 }
584
Chris Lattnerc1824992001-10-29 16:05:51 +0000585 inline void write(const Module *M) { printModule(M); }
586 inline void write(const GlobalVariable *G) { printGlobal(G); }
Chris Lattner79df7c02002-03-26 18:01:55 +0000587 inline void write(const Function *F) { printFunction(F); }
Chris Lattnerc1824992001-10-29 16:05:51 +0000588 inline void write(const BasicBlock *BB) { printBasicBlock(BB); }
Chris Lattner7e708292002-06-25 16:13:24 +0000589 inline void write(const Instruction *I) { printInstruction(*I); }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000590 inline void write(const Constant *CPV) { printConstant(CPV); }
Chris Lattnerda1fbcc2001-11-07 04:21:57 +0000591 inline void write(const Type *Ty) { printType(Ty); }
Chris Lattner00950542001-06-06 20:29:01 +0000592
Chris Lattner66e810b2002-04-18 18:53:13 +0000593 void writeOperand(const Value *Op, bool PrintType, bool PrintName = true);
594
Misha Brukman5cf1acf2004-04-28 15:31:21 +0000595 const Module* getModule() { return TheModule; }
Misha Brukmane5242de2004-04-28 19:24:28 +0000596 void setStream(std::ostream &os) { Out = &os; }
Misha Brukman5cf1acf2004-04-28 15:31:21 +0000597
Chris Lattner00950542001-06-06 20:29:01 +0000598private :
Chris Lattnerc1824992001-10-29 16:05:51 +0000599 void printModule(const Module *M);
600 void printSymbolTable(const SymbolTable &ST);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000601 void printConstant(const Constant *CPV);
Chris Lattnerc1824992001-10-29 16:05:51 +0000602 void printGlobal(const GlobalVariable *GV);
Chris Lattner79df7c02002-03-26 18:01:55 +0000603 void printFunction(const Function *F);
Chris Lattner73e21422002-04-09 19:48:49 +0000604 void printArgument(const Argument *FA);
Chris Lattnerc1824992001-10-29 16:05:51 +0000605 void printBasicBlock(const BasicBlock *BB);
Chris Lattner7e708292002-06-25 16:13:24 +0000606 void printInstruction(const Instruction &I);
Chris Lattner2761e9f2002-04-13 20:53:41 +0000607
608 // printType - Go to extreme measures to attempt to print out a short,
609 // symbolic version of a type name.
610 //
Chris Lattner7b13f562003-05-08 02:08:14 +0000611 std::ostream &printType(const Type *Ty) {
Misha Brukmane5242de2004-04-28 19:24:28 +0000612 return printTypeInt(*Out, Ty, TypeNames);
Chris Lattner2761e9f2002-04-13 20:53:41 +0000613 }
614
615 // printTypeAtLeastOneLevel - Print out one level of the possibly complex type
616 // without considering any symbolic types that we may have equal to it.
617 //
Chris Lattner7b13f562003-05-08 02:08:14 +0000618 std::ostream &printTypeAtLeastOneLevel(const Type *Ty);
Chris Lattnerc1824992001-10-29 16:05:51 +0000619
Chris Lattnere02fa852001-10-13 06:42:36 +0000620 // printInfoComment - Print a little comment after the instruction indicating
621 // which slot it occupies.
Chris Lattner7e708292002-06-25 16:13:24 +0000622 void printInfoComment(const Value &V);
Chris Lattner00950542001-06-06 20:29:01 +0000623};
Reid Spencer73b74952004-05-27 22:04:46 +0000624} // end of llvm namespace
Chris Lattner00950542001-06-06 20:29:01 +0000625
Misha Brukmanab5c6002004-03-02 00:22:19 +0000626/// printTypeAtLeastOneLevel - Print out one level of the possibly complex type
627/// without considering any symbolic types that we may have equal to it.
628///
Chris Lattner7b13f562003-05-08 02:08:14 +0000629std::ostream &AssemblyWriter::printTypeAtLeastOneLevel(const Type *Ty) {
Chris Lattner7e708292002-06-25 16:13:24 +0000630 if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
Chris Lattner2761e9f2002-04-13 20:53:41 +0000631 printType(FTy->getReturnType()) << " (";
Chris Lattnerd5d89962004-02-09 04:14:01 +0000632 for (FunctionType::param_iterator I = FTy->param_begin(),
633 E = FTy->param_end(); I != E; ++I) {
634 if (I != FTy->param_begin())
Misha Brukmane5242de2004-04-28 19:24:28 +0000635 *Out << ", ";
Chris Lattner7a716ad2002-04-16 21:36:08 +0000636 printType(*I);
Chris Lattner2761e9f2002-04-13 20:53:41 +0000637 }
638 if (FTy->isVarArg()) {
Misha Brukmane5242de2004-04-28 19:24:28 +0000639 if (FTy->getNumParams()) *Out << ", ";
640 *Out << "...";
Chris Lattner2761e9f2002-04-13 20:53:41 +0000641 }
Misha Brukman40c732c2004-06-04 21:11:51 +0000642 *Out << ')';
Chris Lattner7e708292002-06-25 16:13:24 +0000643 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
Misha Brukmane5242de2004-04-28 19:24:28 +0000644 *Out << "{ ";
Chris Lattnerd21cd802004-02-09 04:37:31 +0000645 for (StructType::element_iterator I = STy->element_begin(),
646 E = STy->element_end(); I != E; ++I) {
647 if (I != STy->element_begin())
Misha Brukmane5242de2004-04-28 19:24:28 +0000648 *Out << ", ";
Chris Lattner2761e9f2002-04-13 20:53:41 +0000649 printType(*I);
650 }
Misha Brukmane5242de2004-04-28 19:24:28 +0000651 *Out << " }";
Chris Lattner7e708292002-06-25 16:13:24 +0000652 } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
Misha Brukman40c732c2004-06-04 21:11:51 +0000653 printType(PTy->getElementType()) << '*';
Chris Lattner7e708292002-06-25 16:13:24 +0000654 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Misha Brukman40c732c2004-06-04 21:11:51 +0000655 *Out << '[' << ATy->getNumElements() << " x ";
656 printType(ATy->getElementType()) << ']';
Chris Lattner7e708292002-06-25 16:13:24 +0000657 } else if (const OpaqueType *OTy = dyn_cast<OpaqueType>(Ty)) {
Misha Brukmane5242de2004-04-28 19:24:28 +0000658 *Out << "opaque";
Chris Lattner2761e9f2002-04-13 20:53:41 +0000659 } else {
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000660 if (!Ty->isPrimitiveType())
Misha Brukmane5242de2004-04-28 19:24:28 +0000661 *Out << "<unknown derived type>";
Chris Lattner2761e9f2002-04-13 20:53:41 +0000662 printType(Ty);
663 }
Misha Brukmane5242de2004-04-28 19:24:28 +0000664 return *Out;
Chris Lattner2761e9f2002-04-13 20:53:41 +0000665}
666
667
Chris Lattner007377f2001-09-07 16:36:04 +0000668void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType,
Reid Spencerb03de0c2004-05-26 21:56:09 +0000669 bool PrintName) {
Misha Brukman40c732c2004-06-04 21:11:51 +0000670 if (PrintType) { *Out << ' '; printType(Operand->getType()); }
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000671 WriteAsOperandInternal(*Out, Operand, PrintName, TypeNames, &Machine);
Chris Lattner00950542001-06-06 20:29:01 +0000672}
673
Chris Lattner00950542001-06-06 20:29:01 +0000674
Chris Lattnerc1824992001-10-29 16:05:51 +0000675void AssemblyWriter::printModule(const Module *M) {
Chris Lattnereb5d3a12003-08-24 13:48:48 +0000676 switch (M->getEndianness()) {
Misha Brukmane5242de2004-04-28 19:24:28 +0000677 case Module::LittleEndian: *Out << "target endian = little\n"; break;
678 case Module::BigEndian: *Out << "target endian = big\n"; break;
Chris Lattnereb5d3a12003-08-24 13:48:48 +0000679 case Module::AnyEndianness: break;
680 }
681 switch (M->getPointerSize()) {
Misha Brukmane5242de2004-04-28 19:24:28 +0000682 case Module::Pointer32: *Out << "target pointersize = 32\n"; break;
683 case Module::Pointer64: *Out << "target pointersize = 64\n"; break;
Chris Lattnereb5d3a12003-08-24 13:48:48 +0000684 case Module::AnyPointerSize: break;
685 }
686
Chris Lattner007377f2001-09-07 16:36:04 +0000687 // Loop over the symbol table, emitting all named constants...
Chris Lattner6e6026b2002-11-20 18:36:02 +0000688 printSymbolTable(M->getSymbolTable());
Chris Lattner70cc3392001-09-10 07:58:01 +0000689
Chris Lattner7e708292002-06-25 16:13:24 +0000690 for (Module::const_giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
691 printGlobal(I);
Chris Lattner007377f2001-09-07 16:36:04 +0000692
Misha Brukmane5242de2004-04-28 19:24:28 +0000693 *Out << "\nimplementation ; Functions:\n";
Vikram S. Adve5efa3cc2001-09-18 12:48:16 +0000694
Chris Lattnerb5794002002-04-07 22:49:37 +0000695 // Output all of the functions...
Chris Lattner7e708292002-06-25 16:13:24 +0000696 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
697 printFunction(I);
Chris Lattner007377f2001-09-07 16:36:04 +0000698}
699
Chris Lattnerc1824992001-10-29 16:05:51 +0000700void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Misha Brukmane5242de2004-04-28 19:24:28 +0000701 if (GV->hasName()) *Out << getLLVMName(GV->getName()) << " = ";
Chris Lattnerd70684f2001-09-18 04:01:05 +0000702
Chris Lattner4ad02e72003-04-16 20:28:45 +0000703 if (!GV->hasInitializer())
Misha Brukmane5242de2004-04-28 19:24:28 +0000704 *Out << "external ";
Chris Lattner4ad02e72003-04-16 20:28:45 +0000705 else
706 switch (GV->getLinkage()) {
Misha Brukmane5242de2004-04-28 19:24:28 +0000707 case GlobalValue::InternalLinkage: *Out << "internal "; break;
708 case GlobalValue::LinkOnceLinkage: *Out << "linkonce "; break;
709 case GlobalValue::WeakLinkage: *Out << "weak "; break;
710 case GlobalValue::AppendingLinkage: *Out << "appending "; break;
Chris Lattner4ad02e72003-04-16 20:28:45 +0000711 case GlobalValue::ExternalLinkage: break;
712 }
Chris Lattnerd70684f2001-09-18 04:01:05 +0000713
Misha Brukmane5242de2004-04-28 19:24:28 +0000714 *Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattner7a176752001-12-04 00:03:30 +0000715 printType(GV->getType()->getElementType());
Chris Lattnerd70684f2001-09-18 04:01:05 +0000716
717 if (GV->hasInitializer())
718 writeOperand(GV->getInitializer(), false, false);
719
Chris Lattner7e708292002-06-25 16:13:24 +0000720 printInfoComment(*GV);
Misha Brukmane5242de2004-04-28 19:24:28 +0000721 *Out << "\n";
Chris Lattner70cc3392001-09-10 07:58:01 +0000722}
723
Chris Lattner007377f2001-09-07 16:36:04 +0000724
Reid Spencer9231ac82004-05-25 08:53:40 +0000725// printSymbolTable - Run through symbol table looking for constants
726// and types. Emit their declarations.
Chris Lattnerc1824992001-10-29 16:05:51 +0000727void AssemblyWriter::printSymbolTable(const SymbolTable &ST) {
Reid Spencer9231ac82004-05-25 08:53:40 +0000728
729 // Print the types.
730 for (SymbolTable::type_const_iterator TI = ST.type_begin();
731 TI != ST.type_end(); ++TI ) {
732 *Out << "\t" << getLLVMName(TI->first) << " = type ";
733
734 // Make sure we print out at least one level of the type structure, so
735 // that we do not get %FILE = type %FILE
736 //
737 printTypeAtLeastOneLevel(TI->second) << "\n";
738 }
Chris Lattner007377f2001-09-07 16:36:04 +0000739
Reid Spencer9231ac82004-05-25 08:53:40 +0000740 // Print the constants, in type plane order.
741 for (SymbolTable::plane_const_iterator PI = ST.plane_begin();
742 PI != ST.plane_end(); ++PI ) {
743 SymbolTable::value_const_iterator VI = ST.value_begin(PI->first);
744 SymbolTable::value_const_iterator VE = ST.value_end(PI->first);
745
746 for (; VI != VE; ++VI) {
747 const Value *V = VI->second;
Chris Lattner949a3622003-07-23 15:30:06 +0000748 if (const Constant *CPV = dyn_cast<Constant>(V)) {
Reid Spencerb03de0c2004-05-26 21:56:09 +0000749 printConstant(CPV);
Chris Lattner007377f2001-09-07 16:36:04 +0000750 }
751 }
Chris Lattner739a56d2001-07-15 06:35:59 +0000752 }
Chris Lattner00950542001-06-06 20:29:01 +0000753}
754
755
Misha Brukmanab5c6002004-03-02 00:22:19 +0000756/// printConstant - Print out a constant pool entry...
757///
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000758void AssemblyWriter::printConstant(const Constant *CPV) {
Chris Lattner007377f2001-09-07 16:36:04 +0000759 // Don't print out unnamed constants, they will be inlined
760 if (!CPV->hasName()) return;
Chris Lattner00950542001-06-06 20:29:01 +0000761
Chris Lattner1333ed52001-07-26 16:29:38 +0000762 // Print out name...
Misha Brukmane5242de2004-04-28 19:24:28 +0000763 *Out << "\t" << getLLVMName(CPV->getName()) << " =";
Chris Lattner00950542001-06-06 20:29:01 +0000764
765 // Write the value out now...
Chris Lattner7a716ad2002-04-16 21:36:08 +0000766 writeOperand(CPV, true, false);
Chris Lattner00950542001-06-06 20:29:01 +0000767
Chris Lattner7e708292002-06-25 16:13:24 +0000768 printInfoComment(*CPV);
Misha Brukmane5242de2004-04-28 19:24:28 +0000769 *Out << "\n";
Chris Lattner00950542001-06-06 20:29:01 +0000770}
771
Misha Brukmanab5c6002004-03-02 00:22:19 +0000772/// printFunction - Print all aspects of a function.
773///
Chris Lattner7e708292002-06-25 16:13:24 +0000774void AssemblyWriter::printFunction(const Function *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000775 // Print out the return type and name...
Misha Brukmane5242de2004-04-28 19:24:28 +0000776 *Out << "\n";
Chris Lattner4ad02e72003-04-16 20:28:45 +0000777
Misha Brukmane5242de2004-04-28 19:24:28 +0000778 if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, *Out);
Chris Lattner95e5a2c2003-10-30 23:41:03 +0000779
Chris Lattner4ad02e72003-04-16 20:28:45 +0000780 if (F->isExternal())
Misha Brukmane5242de2004-04-28 19:24:28 +0000781 *Out << "declare ";
Chris Lattner4ad02e72003-04-16 20:28:45 +0000782 else
783 switch (F->getLinkage()) {
Misha Brukmane5242de2004-04-28 19:24:28 +0000784 case GlobalValue::InternalLinkage: *Out << "internal "; break;
785 case GlobalValue::LinkOnceLinkage: *Out << "linkonce "; break;
786 case GlobalValue::WeakLinkage: *Out << "weak "; break;
787 case GlobalValue::AppendingLinkage: *Out << "appending "; break;
Chris Lattner4ad02e72003-04-16 20:28:45 +0000788 case GlobalValue::ExternalLinkage: break;
789 }
790
Misha Brukman40c732c2004-06-04 21:11:51 +0000791 printType(F->getReturnType()) << ' ';
Chris Lattner4d45bd02003-10-18 05:57:43 +0000792 if (!F->getName().empty())
Misha Brukmane5242de2004-04-28 19:24:28 +0000793 *Out << getLLVMName(F->getName());
Chris Lattner4d45bd02003-10-18 05:57:43 +0000794 else
Misha Brukmane5242de2004-04-28 19:24:28 +0000795 *Out << "\"\"";
Misha Brukman40c732c2004-06-04 21:11:51 +0000796 *Out << '(';
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000797 Machine.incorporateFunction(F);
Chris Lattner007377f2001-09-07 16:36:04 +0000798
Chris Lattnerc1824992001-10-29 16:05:51 +0000799 // Loop over the arguments, printing them...
Chris Lattner7e708292002-06-25 16:13:24 +0000800 const FunctionType *FT = F->getFunctionType();
Chris Lattner007377f2001-09-07 16:36:04 +0000801
Chris Lattner69da5cf2002-10-13 20:57:00 +0000802 for(Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I)
803 printArgument(I);
Chris Lattner007377f2001-09-07 16:36:04 +0000804
805 // Finish printing arguments...
Chris Lattner7e708292002-06-25 16:13:24 +0000806 if (FT->isVarArg()) {
Misha Brukmane5242de2004-04-28 19:24:28 +0000807 if (FT->getNumParams()) *Out << ", ";
808 *Out << "..."; // Output varargs portion of signature!
Chris Lattner007377f2001-09-07 16:36:04 +0000809 }
Misha Brukman40c732c2004-06-04 21:11:51 +0000810 *Out << ')';
Chris Lattner007377f2001-09-07 16:36:04 +0000811
Chris Lattner7e708292002-06-25 16:13:24 +0000812 if (F->isExternal()) {
Misha Brukmane5242de2004-04-28 19:24:28 +0000813 *Out << "\n";
Chris Lattner03e2acb2002-05-06 03:00:40 +0000814 } else {
Misha Brukmane5242de2004-04-28 19:24:28 +0000815 *Out << " {";
Chris Lattner007377f2001-09-07 16:36:04 +0000816
Chris Lattnerb5794002002-04-07 22:49:37 +0000817 // Output all of its basic blocks... for the function
Chris Lattner7e708292002-06-25 16:13:24 +0000818 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
819 printBasicBlock(I);
Chris Lattner007377f2001-09-07 16:36:04 +0000820
Misha Brukmane5242de2004-04-28 19:24:28 +0000821 *Out << "}\n";
Chris Lattner007377f2001-09-07 16:36:04 +0000822 }
823
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000824 Machine.purgeFunction();
Chris Lattner00950542001-06-06 20:29:01 +0000825}
826
Misha Brukmanab5c6002004-03-02 00:22:19 +0000827/// printArgument - This member is called for every argument that is passed into
828/// the function. Simply print it out
829///
Chris Lattner73e21422002-04-09 19:48:49 +0000830void AssemblyWriter::printArgument(const Argument *Arg) {
Chris Lattner00950542001-06-06 20:29:01 +0000831 // Insert commas as we go... the first arg doesn't get a comma
Misha Brukmane5242de2004-04-28 19:24:28 +0000832 if (Arg != &Arg->getParent()->afront()) *Out << ", ";
Chris Lattner00950542001-06-06 20:29:01 +0000833
834 // Output type...
Chris Lattnerc1824992001-10-29 16:05:51 +0000835 printType(Arg->getType());
Chris Lattner00950542001-06-06 20:29:01 +0000836
837 // Output name, if available...
838 if (Arg->hasName())
Misha Brukman40c732c2004-06-04 21:11:51 +0000839 *Out << ' ' << getLLVMName(Arg->getName());
Chris Lattner00950542001-06-06 20:29:01 +0000840}
841
Misha Brukmanab5c6002004-03-02 00:22:19 +0000842/// printBasicBlock - This member is called for each basic block in a method.
843///
Chris Lattnerc1824992001-10-29 16:05:51 +0000844void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Chris Lattner00950542001-06-06 20:29:01 +0000845 if (BB->hasName()) { // Print out the label if it exists...
Misha Brukman40c732c2004-06-04 21:11:51 +0000846 *Out << "\n" << BB->getName() << ':';
Chris Lattnerafc38682002-05-14 16:02:05 +0000847 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Chris Lattner69566452004-06-09 19:41:19 +0000848 *Out << "\n; <label>:";
849 int Slot = Machine.getSlot(BB);
850 if (Slot != -1)
851 *Out << Slot;
852 else
853 *Out << "<badref>";
Chris Lattner061269b2002-10-02 19:38:55 +0000854 }
Chris Lattner4e4d8622003-11-20 00:09:43 +0000855
856 if (BB->getParent() == 0)
Misha Brukmane5242de2004-04-28 19:24:28 +0000857 *Out << "\t\t; Error: Block without parent!";
Chris Lattner4e4d8622003-11-20 00:09:43 +0000858 else {
859 if (BB != &BB->getParent()->front()) { // Not the entry block?
860 // Output predecessors for the block...
Misha Brukmane5242de2004-04-28 19:24:28 +0000861 *Out << "\t\t;";
Chris Lattner4e4d8622003-11-20 00:09:43 +0000862 pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
863
864 if (PI == PE) {
Misha Brukmane5242de2004-04-28 19:24:28 +0000865 *Out << " No predecessors!";
Chris Lattner4e4d8622003-11-20 00:09:43 +0000866 } else {
Misha Brukmane5242de2004-04-28 19:24:28 +0000867 *Out << " preds =";
Chris Lattner40efcec2003-11-16 22:59:57 +0000868 writeOperand(*PI, false, true);
Chris Lattner4e4d8622003-11-20 00:09:43 +0000869 for (++PI; PI != PE; ++PI) {
Misha Brukman40c732c2004-06-04 21:11:51 +0000870 *Out << ',';
Chris Lattner4e4d8622003-11-20 00:09:43 +0000871 writeOperand(*PI, false, true);
872 }
Chris Lattner40efcec2003-11-16 22:59:57 +0000873 }
Chris Lattner061269b2002-10-02 19:38:55 +0000874 }
Chris Lattner00950542001-06-06 20:29:01 +0000875 }
Chris Lattnerafc38682002-05-14 16:02:05 +0000876
Misha Brukmane5242de2004-04-28 19:24:28 +0000877 *Out << "\n";
Chris Lattner00950542001-06-06 20:29:01 +0000878
Misha Brukmane5242de2004-04-28 19:24:28 +0000879 if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, *Out);
Chris Lattner95e5a2c2003-10-30 23:41:03 +0000880
Chris Lattner007377f2001-09-07 16:36:04 +0000881 // Output all of the instructions in the basic block...
Chris Lattner7e708292002-06-25 16:13:24 +0000882 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
883 printInstruction(*I);
Chris Lattner9f717ef2004-03-08 18:51:45 +0000884
Misha Brukmane5242de2004-04-28 19:24:28 +0000885 if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, *Out);
Chris Lattner00950542001-06-06 20:29:01 +0000886}
887
Chris Lattnere02fa852001-10-13 06:42:36 +0000888
Misha Brukmanab5c6002004-03-02 00:22:19 +0000889/// printInfoComment - Print a little comment after the instruction indicating
890/// which slot it occupies.
891///
Chris Lattner7e708292002-06-25 16:13:24 +0000892void AssemblyWriter::printInfoComment(const Value &V) {
893 if (V.getType() != Type::VoidTy) {
Misha Brukmane5242de2004-04-28 19:24:28 +0000894 *Out << "\t\t; <";
Misha Brukman40c732c2004-06-04 21:11:51 +0000895 printType(V.getType()) << '>';
Chris Lattnere02fa852001-10-13 06:42:36 +0000896
Chris Lattner7e708292002-06-25 16:13:24 +0000897 if (!V.hasName()) {
Chris Lattner69566452004-06-09 19:41:19 +0000898 int SlotNum = Machine.getSlot(&V);
899 if (SlotNum == -1)
900 *Out << ":<badref>";
Reid Spencerfc621e22004-06-09 15:26:53 +0000901 else
Chris Lattner69566452004-06-09 19:41:19 +0000902 *Out << ':' << SlotNum; // Print out the def slot taken.
Chris Lattnere02fa852001-10-13 06:42:36 +0000903 }
Misha Brukman40c732c2004-06-04 21:11:51 +0000904 *Out << " [#uses=" << V.use_size() << ']'; // Output # uses
Chris Lattnere02fa852001-10-13 06:42:36 +0000905 }
906}
907
Reid Spencerfc621e22004-06-09 15:26:53 +0000908/// printInstruction - This member is called for each Instruction in a function..
Misha Brukmanab5c6002004-03-02 00:22:19 +0000909///
Chris Lattner7e708292002-06-25 16:13:24 +0000910void AssemblyWriter::printInstruction(const Instruction &I) {
Misha Brukmane5242de2004-04-28 19:24:28 +0000911 if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, *Out);
Chris Lattner95e5a2c2003-10-30 23:41:03 +0000912
Misha Brukmane5242de2004-04-28 19:24:28 +0000913 *Out << "\t";
Chris Lattner00950542001-06-06 20:29:01 +0000914
915 // Print out name if it exists...
Chris Lattner7e708292002-06-25 16:13:24 +0000916 if (I.hasName())
Misha Brukmane5242de2004-04-28 19:24:28 +0000917 *Out << getLLVMName(I.getName()) << " = ";
Chris Lattner00950542001-06-06 20:29:01 +0000918
Chris Lattnere5e475e2003-09-08 17:45:59 +0000919 // If this is a volatile load or store, print out the volatile marker
920 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
921 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile()))
Misha Brukmane5242de2004-04-28 19:24:28 +0000922 *Out << "volatile ";
Chris Lattnere5e475e2003-09-08 17:45:59 +0000923
Chris Lattner00950542001-06-06 20:29:01 +0000924 // Print out the opcode...
Misha Brukmane5242de2004-04-28 19:24:28 +0000925 *Out << I.getOpcodeName();
Chris Lattner00950542001-06-06 20:29:01 +0000926
927 // Print out the type of the operands...
Chris Lattner7e708292002-06-25 16:13:24 +0000928 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
Chris Lattner00950542001-06-06 20:29:01 +0000929
930 // Special case conditional branches to swizzle the condition out to the front
Chris Lattner7e708292002-06-25 16:13:24 +0000931 if (isa<BranchInst>(I) && I.getNumOperands() > 1) {
932 writeOperand(I.getOperand(2), true);
Misha Brukman40c732c2004-06-04 21:11:51 +0000933 *Out << ',';
Chris Lattner00950542001-06-06 20:29:01 +0000934 writeOperand(Operand, true);
Misha Brukman40c732c2004-06-04 21:11:51 +0000935 *Out << ',';
Chris Lattner7e708292002-06-25 16:13:24 +0000936 writeOperand(I.getOperand(1), true);
Chris Lattner00950542001-06-06 20:29:01 +0000937
Chris Lattner94dc1f22002-04-13 18:34:38 +0000938 } else if (isa<SwitchInst>(I)) {
Chris Lattner00950542001-06-06 20:29:01 +0000939 // Special case switch statement to get formatting nice and correct...
Misha Brukman40c732c2004-06-04 21:11:51 +0000940 writeOperand(Operand , true); *Out << ',';
Misha Brukmane5242de2004-04-28 19:24:28 +0000941 writeOperand(I.getOperand(1), true); *Out << " [";
Chris Lattner00950542001-06-06 20:29:01 +0000942
Chris Lattner7e708292002-06-25 16:13:24 +0000943 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukmane5242de2004-04-28 19:24:28 +0000944 *Out << "\n\t\t";
Misha Brukman40c732c2004-06-04 21:11:51 +0000945 writeOperand(I.getOperand(op ), true); *Out << ',';
Chris Lattner7e708292002-06-25 16:13:24 +0000946 writeOperand(I.getOperand(op+1), true);
Chris Lattner00950542001-06-06 20:29:01 +0000947 }
Misha Brukmane5242de2004-04-28 19:24:28 +0000948 *Out << "\n\t]";
Chris Lattnerb00c5822001-10-02 03:41:24 +0000949 } else if (isa<PHINode>(I)) {
Misha Brukman40c732c2004-06-04 21:11:51 +0000950 *Out << ' ';
Chris Lattner7e708292002-06-25 16:13:24 +0000951 printType(I.getType());
Misha Brukman40c732c2004-06-04 21:11:51 +0000952 *Out << ' ';
Chris Lattner00950542001-06-06 20:29:01 +0000953
Chris Lattner7e708292002-06-25 16:13:24 +0000954 for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukmane5242de2004-04-28 19:24:28 +0000955 if (op) *Out << ", ";
Misha Brukman40c732c2004-06-04 21:11:51 +0000956 *Out << '[';
957 writeOperand(I.getOperand(op ), false); *Out << ',';
Misha Brukmane5242de2004-04-28 19:24:28 +0000958 writeOperand(I.getOperand(op+1), false); *Out << " ]";
Chris Lattnerc24d2082001-06-11 15:04:20 +0000959 }
Chris Lattnere02fa852001-10-13 06:42:36 +0000960 } else if (isa<ReturnInst>(I) && !Operand) {
Misha Brukmane5242de2004-04-28 19:24:28 +0000961 *Out << " void";
Chris Lattnere02fa852001-10-13 06:42:36 +0000962 } else if (isa<CallInst>(I)) {
Chris Lattner7a012292003-08-05 15:34:45 +0000963 const PointerType *PTy = cast<PointerType>(Operand->getType());
964 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
965 const Type *RetTy = FTy->getReturnType();
Chris Lattner268de042001-11-06 21:28:12 +0000966
Chris Lattner7a012292003-08-05 15:34:45 +0000967 // If possible, print out the short form of the call instruction. We can
Chris Lattnerb5794002002-04-07 22:49:37 +0000968 // only do this if the first argument is a pointer to a nonvararg function,
Chris Lattner7a012292003-08-05 15:34:45 +0000969 // and if the return type is not a pointer to a function.
Chris Lattner268de042001-11-06 21:28:12 +0000970 //
Chris Lattner7a012292003-08-05 15:34:45 +0000971 if (!FTy->isVarArg() &&
Chris Lattner94dc1f22002-04-13 18:34:38 +0000972 (!isa<PointerType>(RetTy) ||
Chris Lattnerc1b27182002-07-25 20:58:51 +0000973 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Misha Brukman40c732c2004-06-04 21:11:51 +0000974 *Out << ' '; printType(RetTy);
Chris Lattner268de042001-11-06 21:28:12 +0000975 writeOperand(Operand, false);
976 } else {
977 writeOperand(Operand, true);
978 }
Misha Brukman40c732c2004-06-04 21:11:51 +0000979 *Out << '(';
Chris Lattner7e708292002-06-25 16:13:24 +0000980 if (I.getNumOperands() > 1) writeOperand(I.getOperand(1), true);
981 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; ++op) {
Misha Brukman40c732c2004-06-04 21:11:51 +0000982 *Out << ',';
Chris Lattner7e708292002-06-25 16:13:24 +0000983 writeOperand(I.getOperand(op), true);
Chris Lattner00950542001-06-06 20:29:01 +0000984 }
985
Misha Brukmane5242de2004-04-28 19:24:28 +0000986 *Out << " )";
Chris Lattner7e708292002-06-25 16:13:24 +0000987 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Chris Lattner7a012292003-08-05 15:34:45 +0000988 const PointerType *PTy = cast<PointerType>(Operand->getType());
989 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
990 const Type *RetTy = FTy->getReturnType();
991
992 // If possible, print out the short form of the invoke instruction. We can
993 // only do this if the first argument is a pointer to a nonvararg function,
994 // and if the return type is not a pointer to a function.
995 //
996 if (!FTy->isVarArg() &&
997 (!isa<PointerType>(RetTy) ||
998 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Misha Brukman40c732c2004-06-04 21:11:51 +0000999 *Out << ' '; printType(RetTy);
Chris Lattner7a012292003-08-05 15:34:45 +00001000 writeOperand(Operand, false);
1001 } else {
1002 writeOperand(Operand, true);
1003 }
1004
Misha Brukman40c732c2004-06-04 21:11:51 +00001005 *Out << '(';
Chris Lattner7e708292002-06-25 16:13:24 +00001006 if (I.getNumOperands() > 3) writeOperand(I.getOperand(3), true);
1007 for (unsigned op = 4, Eop = I.getNumOperands(); op < Eop; ++op) {
Misha Brukman40c732c2004-06-04 21:11:51 +00001008 *Out << ',';
Chris Lattner7e708292002-06-25 16:13:24 +00001009 writeOperand(I.getOperand(op), true);
Chris Lattnere02fa852001-10-13 06:42:36 +00001010 }
1011
Misha Brukmane5242de2004-04-28 19:24:28 +00001012 *Out << " )\n\t\t\tto";
Chris Lattnere02fa852001-10-13 06:42:36 +00001013 writeOperand(II->getNormalDest(), true);
Misha Brukmane5242de2004-04-28 19:24:28 +00001014 *Out << " unwind";
Chris Lattneraeb2a1d2004-02-08 21:44:31 +00001015 writeOperand(II->getUnwindDest(), true);
Chris Lattnere02fa852001-10-13 06:42:36 +00001016
Chris Lattner7e708292002-06-25 16:13:24 +00001017 } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(&I)) {
Misha Brukman40c732c2004-06-04 21:11:51 +00001018 *Out << ' ';
Chris Lattner94dc1f22002-04-13 18:34:38 +00001019 printType(AI->getType()->getElementType());
1020 if (AI->isArrayAllocation()) {
Misha Brukman40c732c2004-06-04 21:11:51 +00001021 *Out << ',';
Chris Lattner94dc1f22002-04-13 18:34:38 +00001022 writeOperand(AI->getArraySize(), true);
Chris Lattner00950542001-06-06 20:29:01 +00001023 }
Chris Lattnere02fa852001-10-13 06:42:36 +00001024 } else if (isa<CastInst>(I)) {
Chris Lattner41495a22003-11-17 01:17:04 +00001025 if (Operand) writeOperand(Operand, true); // Work with broken code
Misha Brukmane5242de2004-04-28 19:24:28 +00001026 *Out << " to ";
Chris Lattner7e708292002-06-25 16:13:24 +00001027 printType(I.getType());
Chris Lattner4d45bd02003-10-18 05:57:43 +00001028 } else if (isa<VAArgInst>(I)) {
Chris Lattner41495a22003-11-17 01:17:04 +00001029 if (Operand) writeOperand(Operand, true); // Work with broken code
Misha Brukmane5242de2004-04-28 19:24:28 +00001030 *Out << ", ";
Chris Lattner8f77dae2003-05-08 02:44:12 +00001031 printType(I.getType());
Chris Lattner4d45bd02003-10-18 05:57:43 +00001032 } else if (const VANextInst *VAN = dyn_cast<VANextInst>(&I)) {
Chris Lattner41495a22003-11-17 01:17:04 +00001033 if (Operand) writeOperand(Operand, true); // Work with broken code
Misha Brukmane5242de2004-04-28 19:24:28 +00001034 *Out << ", ";
Chris Lattner4d45bd02003-10-18 05:57:43 +00001035 printType(VAN->getArgType());
Chris Lattner00950542001-06-06 20:29:01 +00001036 } else if (Operand) { // Print the normal way...
1037
1038 // PrintAllTypes - Instructions who have operands of all the same type
1039 // omit the type from all but the first operand. If the instruction has
1040 // different type operands (for example br), then they are all printed.
1041 bool PrintAllTypes = false;
1042 const Type *TheType = Operand->getType();
Chris Lattner00950542001-06-06 20:29:01 +00001043
Chris Lattnercfdd1482004-03-12 05:53:14 +00001044 // Shift Left & Right print both types even for Ubyte LHS, and select prints
1045 // types even if all operands are bools.
1046 if (isa<ShiftInst>(I) || isa<SelectInst>(I)) {
Chris Lattnerffd9bf42003-04-16 20:20:02 +00001047 PrintAllTypes = true;
1048 } else {
1049 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
1050 Operand = I.getOperand(i);
1051 if (Operand->getType() != TheType) {
1052 PrintAllTypes = true; // We have differing types! Print them all!
1053 break;
1054 }
Chris Lattner00950542001-06-06 20:29:01 +00001055 }
1056 }
Chris Lattnerffd9bf42003-04-16 20:20:02 +00001057
Chris Lattnerc1824992001-10-29 16:05:51 +00001058 if (!PrintAllTypes) {
Misha Brukman40c732c2004-06-04 21:11:51 +00001059 *Out << ' ';
Chris Lattnerffd9bf42003-04-16 20:20:02 +00001060 printType(TheType);
Chris Lattnerc1824992001-10-29 16:05:51 +00001061 }
Chris Lattner00950542001-06-06 20:29:01 +00001062
Chris Lattner7e708292002-06-25 16:13:24 +00001063 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Misha Brukman40c732c2004-06-04 21:11:51 +00001064 if (i) *Out << ',';
Chris Lattner7e708292002-06-25 16:13:24 +00001065 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner00950542001-06-06 20:29:01 +00001066 }
1067 }
1068
Chris Lattnere02fa852001-10-13 06:42:36 +00001069 printInfoComment(I);
Misha Brukmane5242de2004-04-28 19:24:28 +00001070 *Out << "\n";
Chris Lattner00950542001-06-06 20:29:01 +00001071}
1072
1073
1074//===----------------------------------------------------------------------===//
1075// External Interface declarations
1076//===----------------------------------------------------------------------===//
1077
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001078void Module::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001079 SlotMachine SlotTable(this);
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001080 AssemblyWriter W(o, SlotTable, this, AAW);
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001081 W.write(this);
Chris Lattner00950542001-06-06 20:29:01 +00001082}
1083
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001084void GlobalVariable::print(std::ostream &o) const {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001085 SlotMachine SlotTable(getParent());
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001086 AssemblyWriter W(o, SlotTable, getParent(), 0);
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001087 W.write(this);
Chris Lattnerb0e45232001-09-10 20:08:19 +00001088}
1089
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001090void Function::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001091 SlotMachine SlotTable(getParent());
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001092 AssemblyWriter W(o, SlotTable, getParent(), AAW);
Chris Lattner00950542001-06-06 20:29:01 +00001093
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001094 W.write(this);
Chris Lattner00950542001-06-06 20:29:01 +00001095}
1096
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001097void BasicBlock::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001098 SlotMachine SlotTable(getParent());
Chris Lattnerc1824992001-10-29 16:05:51 +00001099 AssemblyWriter W(o, SlotTable,
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001100 getParent() ? getParent()->getParent() : 0, AAW);
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001101 W.write(this);
Chris Lattner00950542001-06-06 20:29:01 +00001102}
1103
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001104void Instruction::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001105 const Function *F = getParent() ? getParent()->getParent() : 0;
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001106 SlotMachine SlotTable(F);
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001107 AssemblyWriter W(o, SlotTable, F ? F->getParent() : 0, AAW);
Chris Lattner00950542001-06-06 20:29:01 +00001108
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001109 W.write(this);
Chris Lattner00950542001-06-06 20:29:01 +00001110}
Chris Lattnerda1fbcc2001-11-07 04:21:57 +00001111
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001112void Constant::print(std::ostream &o) const {
1113 if (this == 0) { o << "<null> constant value\n"; return; }
Chris Lattner3bc06b32002-09-10 15:53:49 +00001114
1115 // Handle CPR's special, because they have context information...
1116 if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(this)) {
1117 CPR->getValue()->print(o); // Print as a global value, with context info.
1118 return;
1119 }
1120
Misha Brukman40c732c2004-06-04 21:11:51 +00001121 o << ' ' << getType()->getDescription() << ' ';
Chris Lattner66e810b2002-04-18 18:53:13 +00001122
Chris Lattner7b13f562003-05-08 02:08:14 +00001123 std::map<const Type *, std::string> TypeTable;
Chris Lattner66e810b2002-04-18 18:53:13 +00001124 WriteConstantInt(o, this, false, TypeTable, 0);
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001125}
1126
1127void Type::print(std::ostream &o) const {
1128 if (this == 0)
1129 o << "<null Type>";
1130 else
1131 o << getDescription();
1132}
1133
Chris Lattner73e21422002-04-09 19:48:49 +00001134void Argument::print(std::ostream &o) const {
Chris Lattnera8abee22004-06-18 04:07:20 +00001135 WriteAsOperand(o, this, true, true, getParent()->getParent());
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001136}
1137
Reid Spencerfa452c02004-05-25 18:14:38 +00001138// Value::dump - allow easy printing of Values from the debugger.
1139// Located here because so much of the needed functionality is here.
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001140void Value::dump() const { print(std::cerr); }
Reid Spencerfa452c02004-05-25 18:14:38 +00001141
1142// Type::dump - allow easy printing of Values from the debugger.
1143// Located here because so much of the needed functionality is here.
Reid Spencer9231ac82004-05-25 08:53:40 +00001144void Type::dump() const { print(std::cerr); }
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001145
1146//===----------------------------------------------------------------------===//
1147// CachedWriter Class Implementation
1148//===----------------------------------------------------------------------===//
1149
Chris Lattnerda1fbcc2001-11-07 04:21:57 +00001150void CachedWriter::setModule(const Module *M) {
1151 delete SC; delete AW;
1152 if (M) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001153 SC = new SlotMachine(M );
Misha Brukman40c732c2004-06-04 21:11:51 +00001154 AW = new AssemblyWriter(Out, *SC, M, 0);
Chris Lattnerda1fbcc2001-11-07 04:21:57 +00001155 } else {
1156 SC = 0; AW = 0;
1157 }
1158}
1159
1160CachedWriter::~CachedWriter() {
1161 delete AW;
1162 delete SC;
1163}
1164
1165CachedWriter &CachedWriter::operator<<(const Value *V) {
1166 assert(AW && SC && "CachedWriter does not have a current module!");
1167 switch (V->getValueType()) {
1168 case Value::ConstantVal:
Chris Lattner66e810b2002-04-18 18:53:13 +00001169 case Value::ArgumentVal: AW->writeOperand(V, true, true); break;
Chris Lattner949a3622003-07-23 15:30:06 +00001170 case Value::TypeVal: AW->write(cast<Type>(V)); break;
Chris Lattnerda1fbcc2001-11-07 04:21:57 +00001171 case Value::InstructionVal: AW->write(cast<Instruction>(V)); break;
1172 case Value::BasicBlockVal: AW->write(cast<BasicBlock>(V)); break;
Chris Lattner79df7c02002-03-26 18:01:55 +00001173 case Value::FunctionVal: AW->write(cast<Function>(V)); break;
Chris Lattnerda1fbcc2001-11-07 04:21:57 +00001174 case Value::GlobalVariableVal: AW->write(cast<GlobalVariable>(V)); break;
Misha Brukman40c732c2004-06-04 21:11:51 +00001175 default: Out << "<unknown value type: " << V->getValueType() << '>'; break;
Chris Lattnerda1fbcc2001-11-07 04:21:57 +00001176 }
1177 return *this;
1178}
Misha Brukman5cf1acf2004-04-28 15:31:21 +00001179
1180CachedWriter& CachedWriter::operator<<(const Type *X) {
1181 if (SymbolicTypes) {
1182 const Module *M = AW->getModule();
Misha Brukman40c732c2004-06-04 21:11:51 +00001183 if (M) WriteTypeSymbolic(Out, X, M);
Misha Brukman5cf1acf2004-04-28 15:31:21 +00001184 return *this;
1185 } else
1186 return *this << (const Value*)X;
1187}
Misha Brukmane5242de2004-04-28 19:24:28 +00001188
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001189//===----------------------------------------------------------------------===//
1190//===-- SlotMachine Implementation
1191//===----------------------------------------------------------------------===//
1192
1193#if 0
1194#define SC_DEBUG(X) std::cerr << X
1195#else
1196#define SC_DEBUG(X)
1197#endif
1198
1199// Module level constructor. Causes the contents of the Module (sans functions)
1200// to be added to the slot table.
1201SlotMachine::SlotMachine(const Module *M)
Reid Spencerb03de0c2004-05-26 21:56:09 +00001202 : TheModule(M) ///< Saved for lazy initialization.
1203 , TheFunction(0)
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001204 , mMap()
1205 , fMap()
1206{
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001207}
1208
1209// Function level constructor. Causes the contents of the Module and the one
1210// function provided to be added to the slot table.
1211SlotMachine::SlotMachine(const Function *F )
Reid Spencerb03de0c2004-05-26 21:56:09 +00001212 : TheModule( F ? F->getParent() : 0 ) ///< Saved for lazy initialization
1213 , TheFunction(F) ///< Saved for lazy initialization
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001214 , mMap()
1215 , fMap()
1216{
Reid Spencerb03de0c2004-05-26 21:56:09 +00001217}
1218
1219inline void SlotMachine::initialize(void) {
1220 if ( TheModule) {
1221 processModule();
1222 TheModule = 0; ///< Prevent re-processing next time we're called.
1223 }
1224 if ( TheFunction ) {
1225 processFunction();
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001226 }
1227}
1228
1229// Iterate through all the global variables, functions, and global
1230// variable initializers and create slots for them.
1231void SlotMachine::processModule() {
1232 SC_DEBUG("begin processModule!\n");
1233
1234 // Add all of the global variables to the value table...
1235 for (Module::const_giterator I = TheModule->gbegin(), E = TheModule->gend();
1236 I != E; ++I)
1237 createSlot(I);
1238
1239 // Add all the functions to the table
1240 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
1241 I != E; ++I)
1242 createSlot(I);
1243
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001244 SC_DEBUG("end processModule!\n");
1245}
1246
1247
Reid Spencerb03de0c2004-05-26 21:56:09 +00001248// Process the arguments, basic blocks, and instructions of a function.
1249void SlotMachine::processFunction() {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001250 SC_DEBUG("begin processFunction!\n");
1251
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001252 // Add all the function arguments
Reid Spencerb03de0c2004-05-26 21:56:09 +00001253 for(Function::const_aiterator AI = TheFunction->abegin(),
1254 AE = TheFunction->aend(); AI != AE; ++AI)
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001255 createSlot(AI);
1256
1257 SC_DEBUG("Inserting Instructions:\n");
1258
1259 // Add all of the basic blocks and instructions
Reid Spencerb03de0c2004-05-26 21:56:09 +00001260 for (Function::const_iterator BB = TheFunction->begin(),
1261 E = TheFunction->end(); BB != E; ++BB) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001262 createSlot(BB);
1263 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
1264 createSlot(I);
1265 }
1266 }
1267
1268 SC_DEBUG("end processFunction!\n");
1269}
1270
1271// Clean up after incorporating a function. This is the only way
Reid Spencerb03de0c2004-05-26 21:56:09 +00001272// to get out of the function incorporation state that affects the
1273// getSlot/createSlot lock. Function incorporation state is indicated
1274// by TheFunction != 0.
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001275void SlotMachine::purgeFunction() {
1276 SC_DEBUG("begin purgeFunction!\n");
1277 fMap.clear(); // Simply discard the function level map
Reid Spencerb03de0c2004-05-26 21:56:09 +00001278 TheFunction = 0;
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001279 SC_DEBUG("end purgeFunction!\n");
1280}
1281
1282/// Get the slot number for a value. This function will assert if you
1283/// ask for a Value that hasn't previously been inserted with createSlot.
1284/// Types are forbidden because Type does not inherit from Value (any more).
Chris Lattner69566452004-06-09 19:41:19 +00001285int SlotMachine::getSlot(const Value *V) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001286 assert( V && "Can't get slot for null Value" );
1287 assert( !isa<Type>(V) && "Can't get slot for a type" );
Reid Spencerb03de0c2004-05-26 21:56:09 +00001288 assert(!isa<Constant>(V) || isa<GlobalValue>(V) &&
1289 "Can't insert a non-GlobalValue Constant into SlotMachine");
1290
1291 // Check for uninitialized state and do lazy initialization
1292 this->initialize();
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001293
1294 // Do not number CPR's at all. They are an abomination
1295 if ( const ConstantPointerRef* CPR = dyn_cast<ConstantPointerRef>(V) )
1296 V = CPR->getValue() ;
1297
1298 // Get the type of the value
1299 const Type* VTy = V->getType();
1300
1301 // Find the type plane in the module map
1302 TypedPlanes::const_iterator MI = mMap.find(VTy);
1303
Reid Spencerb03de0c2004-05-26 21:56:09 +00001304 if ( TheFunction ) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001305 // Lookup the type in the function map too
1306 TypedPlanes::const_iterator FI = fMap.find(VTy);
1307 // If there is a corresponding type plane in the function map
1308 if ( FI != fMap.end() ) {
1309 // Lookup the Value in the function map
1310 ValueMap::const_iterator FVI = FI->second.map.find(V);
1311 // If the value doesn't exist in the function map
1312 if ( FVI == FI->second.map.end() ) {
Chris Lattnere9e326e2004-06-09 22:22:10 +00001313 // Look up the value in the module map.
1314 if (MI == mMap.end()) return -1;
Reid Spencerb03de0c2004-05-26 21:56:09 +00001315 ValueMap::const_iterator MVI = MI->second.map.find(V);
1316 // If we didn't find it, it wasn't inserted
Chris Lattner69566452004-06-09 19:41:19 +00001317 if (MVI == MI->second.map.end()) return -1;
Reid Spencerb03de0c2004-05-26 21:56:09 +00001318 assert( MVI != MI->second.map.end() && "Value not found");
1319 // We found it only at the module level
1320 return MVI->second;
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001321
1322 // else the value exists in the function map
1323 } else {
Reid Spencerb03de0c2004-05-26 21:56:09 +00001324 // Return the slot number as the module's contribution to
1325 // the type plane plus the index in the function's contribution
1326 // to the type plane.
Chris Lattnerd1cd3282004-06-15 21:07:32 +00001327 if (MI != mMap.end())
1328 return MI->second.next_slot + FVI->second;
1329 else
1330 return FVI->second;
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001331 }
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001332 }
1333 }
1334
Reid Spencerfc621e22004-06-09 15:26:53 +00001335 // N.B. Can get here only if either !TheFunction or the function doesn't
1336 // have a corresponding type plane for the Value
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001337
1338 // Make sure the type plane exists
Chris Lattner69566452004-06-09 19:41:19 +00001339 if (MI == mMap.end()) return -1;
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001340 // Lookup the value in the module's map
1341 ValueMap::const_iterator MVI = MI->second.map.find(V);
1342 // Make sure we found it.
Chris Lattner69566452004-06-09 19:41:19 +00001343 if (MVI == MI->second.map.end()) return -1;
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001344 // Return it.
1345 return MVI->second;
1346}
1347
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001348// Create a new slot, or return the existing slot if it is already
1349// inserted. Note that the logic here parallels getSlot but instead
1350// of asserting when the Value* isn't found, it inserts the value.
1351unsigned SlotMachine::createSlot(const Value *V) {
1352 assert( V && "Can't insert a null Value to SlotMachine");
1353 assert( !isa<Type>(V) && "Can't insert a Type into SlotMachine");
Reid Spencerb03de0c2004-05-26 21:56:09 +00001354 assert(!isa<Constant>(V) || isa<GlobalValue>(V) &&
1355 "Can't insert a non-GlobalValue Constant into SlotMachine");
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001356
1357 const Type* VTy = V->getType();
1358
1359 // Just ignore void typed things
1360 if (VTy == Type::VoidTy) return 0; // FIXME: Wrong return value!
1361
1362 // Look up the type plane for the Value's type from the module map
1363 TypedPlanes::const_iterator MI = mMap.find(VTy);
1364
Reid Spencerb03de0c2004-05-26 21:56:09 +00001365 if ( TheFunction ) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001366 // Get the type plane for the Value's type from the function map
1367 TypedPlanes::const_iterator FI = fMap.find(VTy);
1368 // If there is a corresponding type plane in the function map
1369 if ( FI != fMap.end() ) {
1370 // Lookup the Value in the function map
1371 ValueMap::const_iterator FVI = FI->second.map.find(V);
1372 // If the value doesn't exist in the function map
1373 if ( FVI == FI->second.map.end() ) {
Reid Spencerb03de0c2004-05-26 21:56:09 +00001374 // If there is no corresponding type plane in the module map
1375 if ( MI == mMap.end() )
1376 return insertValue(V);
1377 // Look up the value in the module map
1378 ValueMap::const_iterator MVI = MI->second.map.find(V);
1379 // If we didn't find it, it wasn't inserted
1380 if ( MVI == MI->second.map.end() )
1381 return insertValue(V);
1382 else
1383 // We found it only at the module level
1384 return MVI->second;
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001385
1386 // else the value exists in the function map
1387 } else {
Reid Spencerb03de0c2004-05-26 21:56:09 +00001388 if ( MI == mMap.end() )
1389 return FVI->second;
1390 else
1391 // Return the slot number as the module's contribution to
1392 // the type plane plus the index in the function's contribution
1393 // to the type plane.
1394 return MI->second.next_slot + FVI->second;
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001395 }
1396
1397 // else there is not a corresponding type plane in the function map
1398 } else {
1399 // If the type plane doesn't exists at the module level
1400 if ( MI == mMap.end() ) {
Reid Spencerb03de0c2004-05-26 21:56:09 +00001401 return insertValue(V);
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001402 // else type plane exists at the module level, examine it
1403 } else {
Reid Spencerb03de0c2004-05-26 21:56:09 +00001404 // Look up the value in the module's map
1405 ValueMap::const_iterator MVI = MI->second.map.find(V);
1406 // If we didn't find it there either
1407 if ( MVI == MI->second.map.end() )
1408 // Return the slot number as the module's contribution to
1409 // the type plane plus the index of the function map insertion.
1410 return MI->second.next_slot + insertValue(V);
1411 else
1412 return MVI->second;
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001413 }
1414 }
1415 }
1416
Reid Spencerb03de0c2004-05-26 21:56:09 +00001417 // N.B. Can only get here if !TheFunction
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001418
1419 // If the module map's type plane is not for the Value's type
1420 if ( MI != mMap.end() ) {
1421 // Lookup the value in the module's map
1422 ValueMap::const_iterator MVI = MI->second.map.find(V);
1423 if ( MVI != MI->second.map.end() )
1424 return MVI->second;
1425 }
1426
1427 return insertValue(V);
1428}
1429
1430
1431// Low level insert function. Minimal checking is done. This
1432// function is just for the convenience of createSlot (above).
1433unsigned SlotMachine::insertValue(const Value *V ) {
1434 assert(V && "Can't insert a null Value into SlotMachine!");
1435 assert(!isa<Type>(V) && "Can't insert a Type into SlotMachine!");
Reid Spencerb03de0c2004-05-26 21:56:09 +00001436 assert(!isa<Constant>(V) || isa<GlobalValue>(V) &&
1437 "Can't insert a non-GlobalValue Constant into SlotMachine");
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001438
Reid Spencerb03de0c2004-05-26 21:56:09 +00001439 // If this value does not contribute to a plane (is void)
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001440 // or if the value already has a name then ignore it.
Reid Spencerb03de0c2004-05-26 21:56:09 +00001441 if (V->getType() == Type::VoidTy || V->hasName() ) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001442 SC_DEBUG("ignored value " << *V << "\n");
1443 return 0; // FIXME: Wrong return value
1444 }
1445
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001446 const Type *VTy = V->getType();
1447 unsigned DestSlot = 0;
1448
Reid Spencerb03de0c2004-05-26 21:56:09 +00001449 if ( TheFunction ) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001450 TypedPlanes::iterator I = fMap.find( VTy );
1451 if ( I == fMap.end() )
1452 I = fMap.insert(std::make_pair(VTy,Plane())).first;
1453 DestSlot = I->second.map[V] = I->second.next_slot++;
1454 } else {
1455 TypedPlanes::iterator I = mMap.find( VTy );
1456 if ( I == mMap.end() )
1457 I = mMap.insert(std::make_pair(VTy,Plane())).first;
1458 DestSlot = I->second.map[V] = I->second.next_slot++;
1459 }
1460
1461 SC_DEBUG(" Inserting value [" << VTy << "] = " << V << " slot=" <<
Reid Spencerb03de0c2004-05-26 21:56:09 +00001462 DestSlot << " [");
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001463 // G = Global, C = Constant, T = Type, F = Function, o = other
Misha Brukman40c732c2004-06-04 21:11:51 +00001464 SC_DEBUG((isa<GlobalVariable>(V) ? 'G' : (isa<Constant>(V) ? 'C' :
1465 (isa<Function>(V) ? 'F' : 'o'))));
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001466 SC_DEBUG("]\n");
1467 return DestSlot;
1468}
1469
Reid Spencer9231ac82004-05-25 08:53:40 +00001470// vim: sw=2