blob: e53d29bb86e151fa28a54d6afead8827733cb2bc [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*
Reid Spencerb03de0c2004-05-26 21:56:09 +000077 unsigned getSlot(const Value *V) ;
Reid Spencer0d1b77e2004-05-26 07:18:52 +000078
79/// @}
80/// @name Mutators
81/// @{
82public:
83 /// If you'd like to deal with a function instead of just a module, use
84 /// this method to get its data into the SlotMachine.
Reid Spencerb03de0c2004-05-26 21:56:09 +000085 void incorporateFunction(const Function *F) { TheFunction = F; }
Reid Spencer0d1b77e2004-05-26 07:18:52 +000086
87 /// After calling incorporateFunction, use this method to remove the
88 /// most recently incorporated function from the SlotMachine. This
89 /// will reset the state of the machine back to just the module contents.
90 void purgeFunction();
91
92/// @}
93/// @name Implementation Details
94/// @{
95private:
Reid Spencerb03de0c2004-05-26 21:56:09 +000096 /// This function does the actual initialization.
97 inline void initialize();
98
Reid Spencer0d1b77e2004-05-26 07:18:52 +000099 /// Values can be crammed into here at will. If they haven't
100 /// been inserted already, they get inserted, otherwise they are ignored.
101 /// Either way, the slot number for the Value* is returned.
102 unsigned createSlot(const Value *V);
103
104 /// Insert a value into the value table. Return the slot number
105 /// that it now occupies. BadThings(TM) will happen if you insert a
106 /// Value that's already been inserted.
107 unsigned insertValue( const Value *V );
108
109 /// Add all of the module level global variables (and their initializers)
110 /// and function declarations, but not the contents of those functions.
111 void processModule();
112
Reid Spencerb03de0c2004-05-26 21:56:09 +0000113 /// Add all of the functions arguments, basic blocks, and instructions
114 void processFunction();
115
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000116 SlotMachine(const SlotMachine &); // DO NOT IMPLEMENT
117 void operator=(const SlotMachine &); // DO NOT IMPLEMENT
118
119/// @}
120/// @name Data
121/// @{
122public:
123
124 /// @brief The module for which we are holding slot numbers
Reid Spencerb03de0c2004-05-26 21:56:09 +0000125 const Module* TheModule;
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000126
Reid Spencerb03de0c2004-05-26 21:56:09 +0000127 /// @brief The function for which we are holding slot numbers
128 const Function* TheFunction;
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000129
130 /// @brief The TypePlanes map for the module level data
131 TypedPlanes mMap;
132
133 /// @brief The TypePlanes map for the function level data
134 TypedPlanes fMap;
135
136/// @}
137
138};
139
140}
141
Chris Lattnera6275cc2002-07-26 21:12:46 +0000142static RegisterPass<PrintModulePass>
143X("printm", "Print module to stderr",PassInfo::Analysis|PassInfo::Optimization);
144static RegisterPass<PrintFunctionPass>
145Y("print","Print function to stderr",PassInfo::Analysis|PassInfo::Optimization);
Chris Lattnerf082b802002-07-23 18:07:49 +0000146
Chris Lattner7b13f562003-05-08 02:08:14 +0000147static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
148 bool PrintName,
149 std::map<const Type *, std::string> &TypeTable,
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000150 SlotMachine *Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000151
Chris Lattner207b5bc2001-10-29 16:37:48 +0000152static const Module *getModuleFromVal(const Value *V) {
Chris Lattner949a3622003-07-23 15:30:06 +0000153 if (const Argument *MA = dyn_cast<Argument>(V))
Chris Lattner207b5bc2001-10-29 16:37:48 +0000154 return MA->getParent() ? MA->getParent()->getParent() : 0;
Chris Lattner949a3622003-07-23 15:30:06 +0000155 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
Chris Lattner207b5bc2001-10-29 16:37:48 +0000156 return BB->getParent() ? BB->getParent()->getParent() : 0;
Chris Lattner949a3622003-07-23 15:30:06 +0000157 else if (const Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000158 const Function *M = I->getParent() ? I->getParent()->getParent() : 0;
Chris Lattner207b5bc2001-10-29 16:37:48 +0000159 return M ? M->getParent() : 0;
Chris Lattner949a3622003-07-23 15:30:06 +0000160 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
Chris Lattner207b5bc2001-10-29 16:37:48 +0000161 return GV->getParent();
Chris Lattner207b5bc2001-10-29 16:37:48 +0000162 return 0;
163}
164
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000165static SlotMachine *createSlotMachine(const Value *V) {
Chris Lattnerc1824992001-10-29 16:05:51 +0000166 assert(!isa<Type>(V) && "Can't create an SC for a type!");
Chris Lattner949a3622003-07-23 15:30:06 +0000167 if (const Argument *FA = dyn_cast<Argument>(V)) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000168 return new SlotMachine(FA->getParent());
Chris Lattner949a3622003-07-23 15:30:06 +0000169 } else if (const Instruction *I = dyn_cast<Instruction>(V)) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000170 return new SlotMachine(I->getParent()->getParent());
Chris Lattner949a3622003-07-23 15:30:06 +0000171 } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000172 return new SlotMachine(BB->getParent());
Chris Lattner949a3622003-07-23 15:30:06 +0000173 } else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)){
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000174 return new SlotMachine(GV->getParent());
Chris Lattner949a3622003-07-23 15:30:06 +0000175 } else if (const Function *Func = dyn_cast<Function>(V)) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000176 return new SlotMachine(Func);
Chris Lattnerc1824992001-10-29 16:05:51 +0000177 }
178 return 0;
179}
Chris Lattner00950542001-06-06 20:29:01 +0000180
Chris Lattner24b8a5d2003-08-22 05:40:38 +0000181// getLLVMName - Turn the specified string into an 'LLVM name', which is either
182// prefixed with % (if the string only contains simple characters) or is
183// surrounded with ""'s (if it has special chars in it).
184static std::string getLLVMName(const std::string &Name) {
185 assert(!Name.empty() && "Cannot get empty name!");
186
187 // First character cannot start with a number...
188 if (Name[0] >= '0' && Name[0] <= '9')
189 return "\"" + Name + "\"";
190
191 // Scan to see if we have any characters that are not on the "white list"
192 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
193 char C = Name[i];
194 assert(C != '"' && "Illegal character in LLVM value name!");
195 if ((C < 'a' || C > 'z') && (C < 'A' || C > 'Z') && (C < '0' || C > '9') &&
196 C != '-' && C != '.' && C != '_')
197 return "\"" + Name + "\"";
198 }
199
200 // If we get here, then the identifier is legal to use as a "VarID".
201 return "%"+Name;
202}
203
Chris Lattner207b5bc2001-10-29 16:37:48 +0000204
Misha Brukmanab5c6002004-03-02 00:22:19 +0000205/// fillTypeNameTable - If the module has a symbol table, take all global types
206/// and stuff their names into the TypeNames map.
207///
Chris Lattner207b5bc2001-10-29 16:37:48 +0000208static void fillTypeNameTable(const Module *M,
Chris Lattner7b13f562003-05-08 02:08:14 +0000209 std::map<const Type *, std::string> &TypeNames) {
Chris Lattner6e6026b2002-11-20 18:36:02 +0000210 if (!M) return;
211 const SymbolTable &ST = M->getSymbolTable();
Reid Spencer9231ac82004-05-25 08:53:40 +0000212 SymbolTable::type_const_iterator TI = ST.type_begin();
213 for (; TI != ST.type_end(); ++TI ) {
214 // As a heuristic, don't insert pointer to primitive types, because
215 // they are used too often to have a single useful name.
216 //
217 const Type *Ty = cast<Type>(TI->second);
218 if (!isa<PointerType>(Ty) ||
Reid Spencerb03de0c2004-05-26 21:56:09 +0000219 !cast<PointerType>(Ty)->getElementType()->isPrimitiveType() ||
220 isa<OpaqueType>(cast<PointerType>(Ty)->getElementType()))
Reid Spencer9231ac82004-05-25 08:53:40 +0000221 TypeNames.insert(std::make_pair(Ty, getLLVMName(TI->first)));
Chris Lattner207b5bc2001-10-29 16:37:48 +0000222 }
223}
224
225
226
John Criswell4ff620a2004-06-01 14:54:08 +0000227static void calcTypeName(const Type *Ty,
228 std::vector<const Type *> &TypeStack,
229 std::map<const Type *, std::string> &TypeNames,
230 std::string & Result){
231 if (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty)) {
232 Result += Ty->getDescription(); // Base case
233 return;
234 }
Chris Lattner207b5bc2001-10-29 16:37:48 +0000235
236 // Check to see if the type is named.
Chris Lattner7b13f562003-05-08 02:08:14 +0000237 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
John Criswell4ff620a2004-06-01 14:54:08 +0000238 if (I != TypeNames.end()) {
239 Result += I->second;
240 return;
241 }
Chris Lattner207b5bc2001-10-29 16:37:48 +0000242
John Criswell4ff620a2004-06-01 14:54:08 +0000243 if (isa<OpaqueType>(Ty)) {
244 Result += "opaque";
245 return;
246 }
Chris Lattner88c17382003-10-30 00:22:33 +0000247
Chris Lattner207b5bc2001-10-29 16:37:48 +0000248 // Check to see if the Type is already on the stack...
249 unsigned Slot = 0, CurSize = TypeStack.size();
250 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
251
252 // This is another base case for the recursion. In this case, we know
253 // that we have looped back to a type that we have previously visited.
254 // Generate the appropriate upreference to handle this.
John Criswell4ff620a2004-06-01 14:54:08 +0000255 if (Slot < CurSize) {
256 Result += "\\" + utostr(CurSize-Slot); // Here's the upreference
257 return;
258 }
Chris Lattner207b5bc2001-10-29 16:37:48 +0000259
260 TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
261
Chris Lattner207b5bc2001-10-29 16:37:48 +0000262 switch (Ty->getPrimitiveID()) {
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000263 case Type::FunctionTyID: {
Chris Lattner949a3622003-07-23 15:30:06 +0000264 const FunctionType *FTy = cast<FunctionType>(Ty);
John Criswell4ff620a2004-06-01 14:54:08 +0000265 calcTypeName(FTy->getReturnType(), TypeStack, TypeNames, Result);
266 Result += " (";
Chris Lattnerd5d89962004-02-09 04:14:01 +0000267 for (FunctionType::param_iterator I = FTy->param_begin(),
268 E = FTy->param_end(); I != E; ++I) {
269 if (I != FTy->param_begin())
Chris Lattner207b5bc2001-10-29 16:37:48 +0000270 Result += ", ";
John Criswell4ff620a2004-06-01 14:54:08 +0000271 calcTypeName(*I, TypeStack, TypeNames, Result);
Chris Lattner207b5bc2001-10-29 16:37:48 +0000272 }
Chris Lattner2761e9f2002-04-13 20:53:41 +0000273 if (FTy->isVarArg()) {
Chris Lattnerd5d89962004-02-09 04:14:01 +0000274 if (FTy->getNumParams()) Result += ", ";
Chris Lattner207b5bc2001-10-29 16:37:48 +0000275 Result += "...";
276 }
277 Result += ")";
278 break;
279 }
280 case Type::StructTyID: {
Chris Lattner949a3622003-07-23 15:30:06 +0000281 const StructType *STy = cast<StructType>(Ty);
John Criswell4ff620a2004-06-01 14:54:08 +0000282 Result += "{ ";
Chris Lattnerd21cd802004-02-09 04:37:31 +0000283 for (StructType::element_iterator I = STy->element_begin(),
284 E = STy->element_end(); I != E; ++I) {
285 if (I != STy->element_begin())
Chris Lattner207b5bc2001-10-29 16:37:48 +0000286 Result += ", ";
John Criswell4ff620a2004-06-01 14:54:08 +0000287 calcTypeName(*I, TypeStack, TypeNames, Result);
Chris Lattner207b5bc2001-10-29 16:37:48 +0000288 }
289 Result += " }";
290 break;
291 }
292 case Type::PointerTyID:
John Criswell4ff620a2004-06-01 14:54:08 +0000293 calcTypeName(cast<PointerType>(Ty)->getElementType(),
294 TypeStack, TypeNames, Result);
295 Result += "*";
Chris Lattner207b5bc2001-10-29 16:37:48 +0000296 break;
297 case Type::ArrayTyID: {
Chris Lattner949a3622003-07-23 15:30:06 +0000298 const ArrayType *ATy = cast<ArrayType>(Ty);
John Criswell4ff620a2004-06-01 14:54:08 +0000299 Result += "[" + utostr(ATy->getNumElements()) + " x ";
300 calcTypeName(ATy->getElementType(), TypeStack, TypeNames, Result);
301 Result += "]";
Chris Lattner207b5bc2001-10-29 16:37:48 +0000302 break;
303 }
Chris Lattner9e094c42003-05-14 17:50:47 +0000304 case Type::OpaqueTyID:
John Criswell4ff620a2004-06-01 14:54:08 +0000305 Result += "opaque";
Chris Lattner9e094c42003-05-14 17:50:47 +0000306 break;
Chris Lattner207b5bc2001-10-29 16:37:48 +0000307 default:
John Criswell4ff620a2004-06-01 14:54:08 +0000308 Result += "<unrecognized-type>";
Chris Lattner207b5bc2001-10-29 16:37:48 +0000309 }
310
311 TypeStack.pop_back(); // Remove self from stack...
John Criswell4ff620a2004-06-01 14:54:08 +0000312 return;
Chris Lattner207b5bc2001-10-29 16:37:48 +0000313}
314
315
Misha Brukman9d0802e2004-03-01 19:48:13 +0000316/// printTypeInt - The internal guts of printing out a type that has a
317/// potentially named portion.
318///
Chris Lattner7b13f562003-05-08 02:08:14 +0000319static std::ostream &printTypeInt(std::ostream &Out, const Type *Ty,
320 std::map<const Type *, std::string> &TypeNames) {
Chris Lattner207b5bc2001-10-29 16:37:48 +0000321 // Primitive types always print out their description, regardless of whether
322 // they have been named or not.
323 //
Chris Lattnerdaf2a492003-10-30 00:12:51 +0000324 if (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty))
325 return Out << Ty->getDescription();
Chris Lattner207b5bc2001-10-29 16:37:48 +0000326
327 // Check to see if the type is named.
Chris Lattner7b13f562003-05-08 02:08:14 +0000328 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
Chris Lattner207b5bc2001-10-29 16:37:48 +0000329 if (I != TypeNames.end()) return Out << I->second;
330
331 // Otherwise we have a type that has not been named but is a derived type.
332 // Carefully recurse the type hierarchy to print out any contained symbolic
333 // names.
334 //
Chris Lattner7b13f562003-05-08 02:08:14 +0000335 std::vector<const Type *> TypeStack;
John Criswell4ff620a2004-06-01 14:54:08 +0000336 std::string TypeName;
337 calcTypeName(Ty, TypeStack, TypeNames, TypeName);
Chris Lattner697954c2002-01-20 22:54:45 +0000338 TypeNames.insert(std::make_pair(Ty, TypeName));//Cache type name for later use
John Criswell4ff620a2004-06-01 14:54:08 +0000339 return (Out << TypeName);
Chris Lattner207b5bc2001-10-29 16:37:48 +0000340}
341
Chris Lattnere51e03b2001-10-31 04:33:19 +0000342
Misha Brukman9d0802e2004-03-01 19:48:13 +0000343/// WriteTypeSymbolic - This attempts to write the specified type as a symbolic
344/// type, iff there is an entry in the modules symbol table for the specified
345/// type or one of it's component types. This is slower than a simple x << Type
346///
Chris Lattner31f84992003-11-21 20:23:48 +0000347std::ostream &llvm::WriteTypeSymbolic(std::ostream &Out, const Type *Ty,
348 const Module *M) {
Misha Brukman40c732c2004-06-04 21:11:51 +0000349 Out << ' ';
Chris Lattner207b5bc2001-10-29 16:37:48 +0000350
351 // If they want us to print out a type, attempt to make it symbolic if there
352 // is a symbol table in the module...
Chris Lattner6e6026b2002-11-20 18:36:02 +0000353 if (M) {
Chris Lattner7b13f562003-05-08 02:08:14 +0000354 std::map<const Type *, std::string> TypeNames;
Chris Lattner207b5bc2001-10-29 16:37:48 +0000355 fillTypeNameTable(M, TypeNames);
356
Chris Lattner7b8660d2001-10-29 16:40:32 +0000357 return printTypeInt(Out, Ty, TypeNames);
Chris Lattner207b5bc2001-10-29 16:37:48 +0000358 } else {
Chris Lattner7b8660d2001-10-29 16:40:32 +0000359 return Out << Ty->getDescription();
Chris Lattner207b5bc2001-10-29 16:37:48 +0000360 }
361}
362
Chris Lattner7b13f562003-05-08 02:08:14 +0000363static void WriteConstantInt(std::ostream &Out, const Constant *CV,
364 bool PrintName,
365 std::map<const Type *, std::string> &TypeTable,
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000366 SlotMachine *Machine) {
Chris Lattner66e810b2002-04-18 18:53:13 +0000367 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
368 Out << (CB == ConstantBool::True ? "true" : "false");
369 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV)) {
370 Out << CI->getValue();
371 } else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV)) {
372 Out << CI->getValue();
373 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
374 // We would like to output the FP constant value in exponential notation,
375 // but we cannot do this if doing so will lose precision. Check here to
376 // make sure that we only output it in exponential format if we can parse
377 // the value back and get the same value.
378 //
379 std::string StrVal = ftostr(CFP->getValue());
380
381 // Check to make sure that the stringized number is not some string like
382 // "Inf" or NaN, that atof will accept, but the lexer will not. Check that
383 // the string matches the "[-+]?[0-9]" regex.
384 //
385 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
386 ((StrVal[0] == '-' || StrVal[0] == '+') &&
Brian Gaekeb471a232003-06-17 23:55:35 +0000387 (StrVal[1] >= '0' && StrVal[1] <= '9')))
Chris Lattner66e810b2002-04-18 18:53:13 +0000388 // Reparse stringized version!
389 if (atof(StrVal.c_str()) == CFP->getValue()) {
390 Out << StrVal; return;
391 }
392
393 // Otherwise we could not reparse it to exactly the same value, so we must
394 // output the string in hexadecimal format!
395 //
396 // Behave nicely in the face of C TBAA rules... see:
397 // http://www.nullstone.com/htmls/category/aliastyp.htm
398 //
399 double Val = CFP->getValue();
400 char *Ptr = (char*)&Val;
401 assert(sizeof(double) == sizeof(uint64_t) && sizeof(double) == 8 &&
402 "assuming that double is 64 bits!");
403 Out << "0x" << utohexstr(*(uint64_t*)Ptr);
404
Chris Lattnerde512b52004-02-15 05:55:15 +0000405 } else if (isa<ConstantAggregateZero>(CV)) {
406 Out << "zeroinitializer";
Chris Lattner66e810b2002-04-18 18:53:13 +0000407 } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
408 // As a special case, print the array as a string if it is an array of
409 // ubytes or an array of sbytes with positive values.
410 //
411 const Type *ETy = CA->getType()->getElementType();
412 bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
413
414 if (ETy == Type::SByteTy)
415 for (unsigned i = 0; i < CA->getNumOperands(); ++i)
416 if (cast<ConstantSInt>(CA->getOperand(i))->getValue() < 0) {
417 isString = false;
418 break;
419 }
420
421 if (isString) {
422 Out << "c\"";
423 for (unsigned i = 0; i < CA->getNumOperands(); ++i) {
Chris Lattner54e3e8f2004-06-04 23:53:20 +0000424 unsigned char C =
425 (unsigned char)cast<ConstantInt>(CA->getOperand(i))->getRawValue();
Chris Lattner66e810b2002-04-18 18:53:13 +0000426
Chris Lattnerfc944462002-07-31 23:56:44 +0000427 if (isprint(C) && C != '"' && C != '\\') {
Chris Lattner66e810b2002-04-18 18:53:13 +0000428 Out << C;
429 } else {
430 Out << '\\'
431 << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'))
432 << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
433 }
434 }
435 Out << "\"";
436
437 } else { // Cannot output in string format...
Misha Brukman40c732c2004-06-04 21:11:51 +0000438 Out << '[';
Chris Lattner7a716ad2002-04-16 21:36:08 +0000439 if (CA->getNumOperands()) {
Misha Brukman40c732c2004-06-04 21:11:51 +0000440 Out << ' ';
Chris Lattner66e810b2002-04-18 18:53:13 +0000441 printTypeInt(Out, ETy, TypeTable);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000442 WriteAsOperandInternal(Out, CA->getOperand(0),
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000443 PrintName, TypeTable, Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000444 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
445 Out << ", ";
Chris Lattner66e810b2002-04-18 18:53:13 +0000446 printTypeInt(Out, ETy, TypeTable);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000447 WriteAsOperandInternal(Out, CA->getOperand(i), PrintName,
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000448 TypeTable, Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000449 }
450 }
451 Out << " ]";
452 }
453 } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Misha Brukman40c732c2004-06-04 21:11:51 +0000454 Out << '{';
Chris Lattner7a716ad2002-04-16 21:36:08 +0000455 if (CS->getNumOperands()) {
Misha Brukman40c732c2004-06-04 21:11:51 +0000456 Out << ' ';
Chris Lattner7a716ad2002-04-16 21:36:08 +0000457 printTypeInt(Out, CS->getOperand(0)->getType(), TypeTable);
458
459 WriteAsOperandInternal(Out, CS->getOperand(0),
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000460 PrintName, TypeTable, Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000461
462 for (unsigned i = 1; i < CS->getNumOperands(); i++) {
463 Out << ", ";
464 printTypeInt(Out, CS->getOperand(i)->getType(), TypeTable);
465
466 WriteAsOperandInternal(Out, CS->getOperand(i),
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000467 PrintName, TypeTable, Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000468 }
469 }
470
471 Out << " }";
472 } else if (isa<ConstantPointerNull>(CV)) {
473 Out << "null";
474
Chris Lattner7e708292002-06-25 16:13:24 +0000475 } else if (const ConstantPointerRef *PR = dyn_cast<ConstantPointerRef>(CV)) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000476 WriteAsOperandInternal(Out, PR->getValue(), true, TypeTable, Machine);
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000477
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000478 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Chris Lattner38d87732003-03-06 23:23:32 +0000479 Out << CE->getOpcodeName() << " (";
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000480
481 for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
482 printTypeInt(Out, (*OI)->getType(), TypeTable);
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000483 WriteAsOperandInternal(Out, *OI, PrintName, TypeTable, Machine);
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000484 if (OI+1 != CE->op_end())
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000485 Out << ", ";
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000486 }
487
Chris Lattner1c93e5b2002-08-16 21:17:11 +0000488 if (CE->getOpcode() == Instruction::Cast) {
Chris Lattner95586b82002-08-15 19:37:43 +0000489 Out << " to ";
490 printTypeInt(Out, CE->getType(), TypeTable);
491 }
Misha Brukman40c732c2004-06-04 21:11:51 +0000492 Out << ')';
Chris Lattner95586b82002-08-15 19:37:43 +0000493
Chris Lattner7a716ad2002-04-16 21:36:08 +0000494 } else {
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000495 Out << "<placeholder or erroneous Constant>";
Chris Lattner7a716ad2002-04-16 21:36:08 +0000496 }
497}
498
499
Misha Brukmanab5c6002004-03-02 00:22:19 +0000500/// WriteAsOperand - Write the name of the specified value out to the specified
501/// ostream. This can be useful when you just want to print int %reg126, not
502/// the whole instruction that generated it.
503///
Chris Lattner7b13f562003-05-08 02:08:14 +0000504static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
505 bool PrintName,
506 std::map<const Type*, std::string> &TypeTable,
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000507 SlotMachine *Machine) {
Misha Brukman40c732c2004-06-04 21:11:51 +0000508 Out << ' ';
Chris Lattner7a716ad2002-04-16 21:36:08 +0000509 if (PrintName && V->hasName()) {
Chris Lattner24b8a5d2003-08-22 05:40:38 +0000510 Out << getLLVMName(V->getName());
Chris Lattner7a716ad2002-04-16 21:36:08 +0000511 } else {
Chris Lattner949a3622003-07-23 15:30:06 +0000512 if (const Constant *CV = dyn_cast<Constant>(V)) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000513 WriteConstantInt(Out, CV, PrintName, TypeTable, Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000514 } else {
515 int Slot;
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000516 if (Machine) {
Reid Spencerb03de0c2004-05-26 21:56:09 +0000517 Slot = Machine->getSlot(V);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000518 } else {
Chris Lattner949a3622003-07-23 15:30:06 +0000519 if (const Type *Ty = dyn_cast<Type>(V)) {
Chris Lattner7a716ad2002-04-16 21:36:08 +0000520 Out << Ty->getDescription();
521 return;
522 }
523
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000524 Machine = createSlotMachine(V);
525 if (Machine == 0) { Out << "BAD VALUE TYPE!"; return; }
Chris Lattner7a716ad2002-04-16 21:36:08 +0000526
Reid Spencerb03de0c2004-05-26 21:56:09 +0000527 Slot = Machine->getSlot(V);
528 delete Machine;
Chris Lattner7a716ad2002-04-16 21:36:08 +0000529 }
Misha Brukman40c732c2004-06-04 21:11:51 +0000530 Out << '%' << Slot;
Chris Lattner7a716ad2002-04-16 21:36:08 +0000531 }
532 }
533}
534
535
Misha Brukman9d0802e2004-03-01 19:48:13 +0000536/// WriteAsOperand - Write the name of the specified value out to the specified
537/// ostream. This can be useful when you just want to print int %reg126, not
538/// the whole instruction that generated it.
539///
Chris Lattner31f84992003-11-21 20:23:48 +0000540std::ostream &llvm::WriteAsOperand(std::ostream &Out, const Value *V,
Misha Brukman9d0802e2004-03-01 19:48:13 +0000541 bool PrintType, bool PrintName,
542 const Module *Context) {
Chris Lattner7b13f562003-05-08 02:08:14 +0000543 std::map<const Type *, std::string> TypeNames;
Chris Lattner607dc682002-07-10 16:48:17 +0000544 if (Context == 0) Context = getModuleFromVal(V);
Chris Lattner207b5bc2001-10-29 16:37:48 +0000545
Chris Lattner6e6026b2002-11-20 18:36:02 +0000546 if (Context)
Chris Lattner607dc682002-07-10 16:48:17 +0000547 fillTypeNameTable(Context, TypeNames);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000548
549 if (PrintType)
550 printTypeInt(Out, V->getType(), TypeNames);
551
Brian Gaekecd4a3982003-11-16 23:08:27 +0000552 if (const Type *Ty = dyn_cast<Type> (V))
553 printTypeInt(Out, Ty, TypeNames);
554
Chris Lattner607dc682002-07-10 16:48:17 +0000555 WriteAsOperandInternal(Out, V, PrintName, TypeNames, 0);
Chris Lattner622f7402001-07-20 19:15:21 +0000556 return Out;
557}
558
Chris Lattner31f84992003-11-21 20:23:48 +0000559namespace llvm {
Chris Lattnerd8c2e422001-07-12 23:35:26 +0000560
Chris Lattner007377f2001-09-07 16:36:04 +0000561class AssemblyWriter {
Misha Brukmane5242de2004-04-28 19:24:28 +0000562 std::ostream *Out;
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000563 SlotMachine &Machine;
Chris Lattnerc1824992001-10-29 16:05:51 +0000564 const Module *TheModule;
Chris Lattner7b13f562003-05-08 02:08:14 +0000565 std::map<const Type *, std::string> TypeNames;
Chris Lattner95e5a2c2003-10-30 23:41:03 +0000566 AssemblyAnnotationWriter *AnnotationWriter;
Chris Lattner00950542001-06-06 20:29:01 +0000567public:
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000568 inline AssemblyWriter(std::ostream &o, SlotMachine &Mac, const Module *M,
Chris Lattner95e5a2c2003-10-30 23:41:03 +0000569 AssemblyAnnotationWriter *AAW)
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000570 : Out(&o), Machine(Mac), TheModule(M), AnnotationWriter(AAW) {
Chris Lattnerc1824992001-10-29 16:05:51 +0000571
572 // If the module has a symbol table, take all global types and stuff their
573 // names into the TypeNames map.
574 //
Chris Lattner207b5bc2001-10-29 16:37:48 +0000575 fillTypeNameTable(M, TypeNames);
Chris Lattner00950542001-06-06 20:29:01 +0000576 }
577
Chris Lattnerc1824992001-10-29 16:05:51 +0000578 inline void write(const Module *M) { printModule(M); }
579 inline void write(const GlobalVariable *G) { printGlobal(G); }
Chris Lattner79df7c02002-03-26 18:01:55 +0000580 inline void write(const Function *F) { printFunction(F); }
Chris Lattnerc1824992001-10-29 16:05:51 +0000581 inline void write(const BasicBlock *BB) { printBasicBlock(BB); }
Chris Lattner7e708292002-06-25 16:13:24 +0000582 inline void write(const Instruction *I) { printInstruction(*I); }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000583 inline void write(const Constant *CPV) { printConstant(CPV); }
Chris Lattnerda1fbcc2001-11-07 04:21:57 +0000584 inline void write(const Type *Ty) { printType(Ty); }
Chris Lattner00950542001-06-06 20:29:01 +0000585
Chris Lattner66e810b2002-04-18 18:53:13 +0000586 void writeOperand(const Value *Op, bool PrintType, bool PrintName = true);
587
Misha Brukman5cf1acf2004-04-28 15:31:21 +0000588 const Module* getModule() { return TheModule; }
Misha Brukmane5242de2004-04-28 19:24:28 +0000589 void setStream(std::ostream &os) { Out = &os; }
Misha Brukman5cf1acf2004-04-28 15:31:21 +0000590
Chris Lattner00950542001-06-06 20:29:01 +0000591private :
Chris Lattnerc1824992001-10-29 16:05:51 +0000592 void printModule(const Module *M);
593 void printSymbolTable(const SymbolTable &ST);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000594 void printConstant(const Constant *CPV);
Chris Lattnerc1824992001-10-29 16:05:51 +0000595 void printGlobal(const GlobalVariable *GV);
Chris Lattner79df7c02002-03-26 18:01:55 +0000596 void printFunction(const Function *F);
Chris Lattner73e21422002-04-09 19:48:49 +0000597 void printArgument(const Argument *FA);
Chris Lattnerc1824992001-10-29 16:05:51 +0000598 void printBasicBlock(const BasicBlock *BB);
Chris Lattner7e708292002-06-25 16:13:24 +0000599 void printInstruction(const Instruction &I);
Chris Lattner2761e9f2002-04-13 20:53:41 +0000600
601 // printType - Go to extreme measures to attempt to print out a short,
602 // symbolic version of a type name.
603 //
Chris Lattner7b13f562003-05-08 02:08:14 +0000604 std::ostream &printType(const Type *Ty) {
Misha Brukmane5242de2004-04-28 19:24:28 +0000605 return printTypeInt(*Out, Ty, TypeNames);
Chris Lattner2761e9f2002-04-13 20:53:41 +0000606 }
607
608 // printTypeAtLeastOneLevel - Print out one level of the possibly complex type
609 // without considering any symbolic types that we may have equal to it.
610 //
Chris Lattner7b13f562003-05-08 02:08:14 +0000611 std::ostream &printTypeAtLeastOneLevel(const Type *Ty);
Chris Lattnerc1824992001-10-29 16:05:51 +0000612
Chris Lattnere02fa852001-10-13 06:42:36 +0000613 // printInfoComment - Print a little comment after the instruction indicating
614 // which slot it occupies.
Chris Lattner7e708292002-06-25 16:13:24 +0000615 void printInfoComment(const Value &V);
Chris Lattner00950542001-06-06 20:29:01 +0000616};
Reid Spencer73b74952004-05-27 22:04:46 +0000617} // end of llvm namespace
Chris Lattner00950542001-06-06 20:29:01 +0000618
Misha Brukmanab5c6002004-03-02 00:22:19 +0000619/// printTypeAtLeastOneLevel - Print out one level of the possibly complex type
620/// without considering any symbolic types that we may have equal to it.
621///
Chris Lattner7b13f562003-05-08 02:08:14 +0000622std::ostream &AssemblyWriter::printTypeAtLeastOneLevel(const Type *Ty) {
Chris Lattner7e708292002-06-25 16:13:24 +0000623 if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
Chris Lattner2761e9f2002-04-13 20:53:41 +0000624 printType(FTy->getReturnType()) << " (";
Chris Lattnerd5d89962004-02-09 04:14:01 +0000625 for (FunctionType::param_iterator I = FTy->param_begin(),
626 E = FTy->param_end(); I != E; ++I) {
627 if (I != FTy->param_begin())
Misha Brukmane5242de2004-04-28 19:24:28 +0000628 *Out << ", ";
Chris Lattner7a716ad2002-04-16 21:36:08 +0000629 printType(*I);
Chris Lattner2761e9f2002-04-13 20:53:41 +0000630 }
631 if (FTy->isVarArg()) {
Misha Brukmane5242de2004-04-28 19:24:28 +0000632 if (FTy->getNumParams()) *Out << ", ";
633 *Out << "...";
Chris Lattner2761e9f2002-04-13 20:53:41 +0000634 }
Misha Brukman40c732c2004-06-04 21:11:51 +0000635 *Out << ')';
Chris Lattner7e708292002-06-25 16:13:24 +0000636 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
Misha Brukmane5242de2004-04-28 19:24:28 +0000637 *Out << "{ ";
Chris Lattnerd21cd802004-02-09 04:37:31 +0000638 for (StructType::element_iterator I = STy->element_begin(),
639 E = STy->element_end(); I != E; ++I) {
640 if (I != STy->element_begin())
Misha Brukmane5242de2004-04-28 19:24:28 +0000641 *Out << ", ";
Chris Lattner2761e9f2002-04-13 20:53:41 +0000642 printType(*I);
643 }
Misha Brukmane5242de2004-04-28 19:24:28 +0000644 *Out << " }";
Chris Lattner7e708292002-06-25 16:13:24 +0000645 } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
Misha Brukman40c732c2004-06-04 21:11:51 +0000646 printType(PTy->getElementType()) << '*';
Chris Lattner7e708292002-06-25 16:13:24 +0000647 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Misha Brukman40c732c2004-06-04 21:11:51 +0000648 *Out << '[' << ATy->getNumElements() << " x ";
649 printType(ATy->getElementType()) << ']';
Chris Lattner7e708292002-06-25 16:13:24 +0000650 } else if (const OpaqueType *OTy = dyn_cast<OpaqueType>(Ty)) {
Misha Brukmane5242de2004-04-28 19:24:28 +0000651 *Out << "opaque";
Chris Lattner2761e9f2002-04-13 20:53:41 +0000652 } else {
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000653 if (!Ty->isPrimitiveType())
Misha Brukmane5242de2004-04-28 19:24:28 +0000654 *Out << "<unknown derived type>";
Chris Lattner2761e9f2002-04-13 20:53:41 +0000655 printType(Ty);
656 }
Misha Brukmane5242de2004-04-28 19:24:28 +0000657 return *Out;
Chris Lattner2761e9f2002-04-13 20:53:41 +0000658}
659
660
Chris Lattner007377f2001-09-07 16:36:04 +0000661void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType,
Reid Spencerb03de0c2004-05-26 21:56:09 +0000662 bool PrintName) {
Misha Brukman40c732c2004-06-04 21:11:51 +0000663 if (PrintType) { *Out << ' '; printType(Operand->getType()); }
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000664 WriteAsOperandInternal(*Out, Operand, PrintName, TypeNames, &Machine);
Chris Lattner00950542001-06-06 20:29:01 +0000665}
666
Chris Lattner00950542001-06-06 20:29:01 +0000667
Chris Lattnerc1824992001-10-29 16:05:51 +0000668void AssemblyWriter::printModule(const Module *M) {
Chris Lattnereb5d3a12003-08-24 13:48:48 +0000669 switch (M->getEndianness()) {
Misha Brukmane5242de2004-04-28 19:24:28 +0000670 case Module::LittleEndian: *Out << "target endian = little\n"; break;
671 case Module::BigEndian: *Out << "target endian = big\n"; break;
Chris Lattnereb5d3a12003-08-24 13:48:48 +0000672 case Module::AnyEndianness: break;
673 }
674 switch (M->getPointerSize()) {
Misha Brukmane5242de2004-04-28 19:24:28 +0000675 case Module::Pointer32: *Out << "target pointersize = 32\n"; break;
676 case Module::Pointer64: *Out << "target pointersize = 64\n"; break;
Chris Lattnereb5d3a12003-08-24 13:48:48 +0000677 case Module::AnyPointerSize: break;
678 }
679
Chris Lattner007377f2001-09-07 16:36:04 +0000680 // Loop over the symbol table, emitting all named constants...
Chris Lattner6e6026b2002-11-20 18:36:02 +0000681 printSymbolTable(M->getSymbolTable());
Chris Lattner70cc3392001-09-10 07:58:01 +0000682
Chris Lattner7e708292002-06-25 16:13:24 +0000683 for (Module::const_giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
684 printGlobal(I);
Chris Lattner007377f2001-09-07 16:36:04 +0000685
Misha Brukmane5242de2004-04-28 19:24:28 +0000686 *Out << "\nimplementation ; Functions:\n";
Vikram S. Adve5efa3cc2001-09-18 12:48:16 +0000687
Chris Lattnerb5794002002-04-07 22:49:37 +0000688 // Output all of the functions...
Chris Lattner7e708292002-06-25 16:13:24 +0000689 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
690 printFunction(I);
Chris Lattner007377f2001-09-07 16:36:04 +0000691}
692
Chris Lattnerc1824992001-10-29 16:05:51 +0000693void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Misha Brukmane5242de2004-04-28 19:24:28 +0000694 if (GV->hasName()) *Out << getLLVMName(GV->getName()) << " = ";
Chris Lattnerd70684f2001-09-18 04:01:05 +0000695
Chris Lattner4ad02e72003-04-16 20:28:45 +0000696 if (!GV->hasInitializer())
Misha Brukmane5242de2004-04-28 19:24:28 +0000697 *Out << "external ";
Chris Lattner4ad02e72003-04-16 20:28:45 +0000698 else
699 switch (GV->getLinkage()) {
Misha Brukmane5242de2004-04-28 19:24:28 +0000700 case GlobalValue::InternalLinkage: *Out << "internal "; break;
701 case GlobalValue::LinkOnceLinkage: *Out << "linkonce "; break;
702 case GlobalValue::WeakLinkage: *Out << "weak "; break;
703 case GlobalValue::AppendingLinkage: *Out << "appending "; break;
Chris Lattner4ad02e72003-04-16 20:28:45 +0000704 case GlobalValue::ExternalLinkage: break;
705 }
Chris Lattnerd70684f2001-09-18 04:01:05 +0000706
Misha Brukmane5242de2004-04-28 19:24:28 +0000707 *Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattner7a176752001-12-04 00:03:30 +0000708 printType(GV->getType()->getElementType());
Chris Lattnerd70684f2001-09-18 04:01:05 +0000709
710 if (GV->hasInitializer())
711 writeOperand(GV->getInitializer(), false, false);
712
Chris Lattner7e708292002-06-25 16:13:24 +0000713 printInfoComment(*GV);
Misha Brukmane5242de2004-04-28 19:24:28 +0000714 *Out << "\n";
Chris Lattner70cc3392001-09-10 07:58:01 +0000715}
716
Chris Lattner007377f2001-09-07 16:36:04 +0000717
Reid Spencer9231ac82004-05-25 08:53:40 +0000718// printSymbolTable - Run through symbol table looking for constants
719// and types. Emit their declarations.
Chris Lattnerc1824992001-10-29 16:05:51 +0000720void AssemblyWriter::printSymbolTable(const SymbolTable &ST) {
Reid Spencer9231ac82004-05-25 08:53:40 +0000721
722 // Print the types.
723 for (SymbolTable::type_const_iterator TI = ST.type_begin();
724 TI != ST.type_end(); ++TI ) {
725 *Out << "\t" << getLLVMName(TI->first) << " = type ";
726
727 // Make sure we print out at least one level of the type structure, so
728 // that we do not get %FILE = type %FILE
729 //
730 printTypeAtLeastOneLevel(TI->second) << "\n";
731 }
Chris Lattner007377f2001-09-07 16:36:04 +0000732
Reid Spencer9231ac82004-05-25 08:53:40 +0000733 // Print the constants, in type plane order.
734 for (SymbolTable::plane_const_iterator PI = ST.plane_begin();
735 PI != ST.plane_end(); ++PI ) {
736 SymbolTable::value_const_iterator VI = ST.value_begin(PI->first);
737 SymbolTable::value_const_iterator VE = ST.value_end(PI->first);
738
739 for (; VI != VE; ++VI) {
740 const Value *V = VI->second;
Chris Lattner949a3622003-07-23 15:30:06 +0000741 if (const Constant *CPV = dyn_cast<Constant>(V)) {
Reid Spencerb03de0c2004-05-26 21:56:09 +0000742 printConstant(CPV);
Chris Lattner007377f2001-09-07 16:36:04 +0000743 }
744 }
Chris Lattner739a56d2001-07-15 06:35:59 +0000745 }
Chris Lattner00950542001-06-06 20:29:01 +0000746}
747
748
Misha Brukmanab5c6002004-03-02 00:22:19 +0000749/// printConstant - Print out a constant pool entry...
750///
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000751void AssemblyWriter::printConstant(const Constant *CPV) {
Chris Lattner007377f2001-09-07 16:36:04 +0000752 // Don't print out unnamed constants, they will be inlined
753 if (!CPV->hasName()) return;
Chris Lattner00950542001-06-06 20:29:01 +0000754
Chris Lattner1333ed52001-07-26 16:29:38 +0000755 // Print out name...
Misha Brukmane5242de2004-04-28 19:24:28 +0000756 *Out << "\t" << getLLVMName(CPV->getName()) << " =";
Chris Lattner00950542001-06-06 20:29:01 +0000757
758 // Write the value out now...
Chris Lattner7a716ad2002-04-16 21:36:08 +0000759 writeOperand(CPV, true, false);
Chris Lattner00950542001-06-06 20:29:01 +0000760
Chris Lattner7e708292002-06-25 16:13:24 +0000761 printInfoComment(*CPV);
Misha Brukmane5242de2004-04-28 19:24:28 +0000762 *Out << "\n";
Chris Lattner00950542001-06-06 20:29:01 +0000763}
764
Misha Brukmanab5c6002004-03-02 00:22:19 +0000765/// printFunction - Print all aspects of a function.
766///
Chris Lattner7e708292002-06-25 16:13:24 +0000767void AssemblyWriter::printFunction(const Function *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000768 // Print out the return type and name...
Misha Brukmane5242de2004-04-28 19:24:28 +0000769 *Out << "\n";
Chris Lattner4ad02e72003-04-16 20:28:45 +0000770
Misha Brukmane5242de2004-04-28 19:24:28 +0000771 if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, *Out);
Chris Lattner95e5a2c2003-10-30 23:41:03 +0000772
Chris Lattner4ad02e72003-04-16 20:28:45 +0000773 if (F->isExternal())
Misha Brukmane5242de2004-04-28 19:24:28 +0000774 *Out << "declare ";
Chris Lattner4ad02e72003-04-16 20:28:45 +0000775 else
776 switch (F->getLinkage()) {
Misha Brukmane5242de2004-04-28 19:24:28 +0000777 case GlobalValue::InternalLinkage: *Out << "internal "; break;
778 case GlobalValue::LinkOnceLinkage: *Out << "linkonce "; break;
779 case GlobalValue::WeakLinkage: *Out << "weak "; break;
780 case GlobalValue::AppendingLinkage: *Out << "appending "; break;
Chris Lattner4ad02e72003-04-16 20:28:45 +0000781 case GlobalValue::ExternalLinkage: break;
782 }
783
Misha Brukman40c732c2004-06-04 21:11:51 +0000784 printType(F->getReturnType()) << ' ';
Chris Lattner4d45bd02003-10-18 05:57:43 +0000785 if (!F->getName().empty())
Misha Brukmane5242de2004-04-28 19:24:28 +0000786 *Out << getLLVMName(F->getName());
Chris Lattner4d45bd02003-10-18 05:57:43 +0000787 else
Misha Brukmane5242de2004-04-28 19:24:28 +0000788 *Out << "\"\"";
Misha Brukman40c732c2004-06-04 21:11:51 +0000789 *Out << '(';
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000790 Machine.incorporateFunction(F);
Chris Lattner007377f2001-09-07 16:36:04 +0000791
Chris Lattnerc1824992001-10-29 16:05:51 +0000792 // Loop over the arguments, printing them...
Chris Lattner7e708292002-06-25 16:13:24 +0000793 const FunctionType *FT = F->getFunctionType();
Chris Lattner007377f2001-09-07 16:36:04 +0000794
Chris Lattner69da5cf2002-10-13 20:57:00 +0000795 for(Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I)
796 printArgument(I);
Chris Lattner007377f2001-09-07 16:36:04 +0000797
798 // Finish printing arguments...
Chris Lattner7e708292002-06-25 16:13:24 +0000799 if (FT->isVarArg()) {
Misha Brukmane5242de2004-04-28 19:24:28 +0000800 if (FT->getNumParams()) *Out << ", ";
801 *Out << "..."; // Output varargs portion of signature!
Chris Lattner007377f2001-09-07 16:36:04 +0000802 }
Misha Brukman40c732c2004-06-04 21:11:51 +0000803 *Out << ')';
Chris Lattner007377f2001-09-07 16:36:04 +0000804
Chris Lattner7e708292002-06-25 16:13:24 +0000805 if (F->isExternal()) {
Misha Brukmane5242de2004-04-28 19:24:28 +0000806 *Out << "\n";
Chris Lattner03e2acb2002-05-06 03:00:40 +0000807 } else {
Misha Brukmane5242de2004-04-28 19:24:28 +0000808 *Out << " {";
Chris Lattner007377f2001-09-07 16:36:04 +0000809
Chris Lattnerb5794002002-04-07 22:49:37 +0000810 // Output all of its basic blocks... for the function
Chris Lattner7e708292002-06-25 16:13:24 +0000811 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
812 printBasicBlock(I);
Chris Lattner007377f2001-09-07 16:36:04 +0000813
Misha Brukmane5242de2004-04-28 19:24:28 +0000814 *Out << "}\n";
Chris Lattner007377f2001-09-07 16:36:04 +0000815 }
816
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000817 Machine.purgeFunction();
Chris Lattner00950542001-06-06 20:29:01 +0000818}
819
Misha Brukmanab5c6002004-03-02 00:22:19 +0000820/// printArgument - This member is called for every argument that is passed into
821/// the function. Simply print it out
822///
Chris Lattner73e21422002-04-09 19:48:49 +0000823void AssemblyWriter::printArgument(const Argument *Arg) {
Chris Lattner00950542001-06-06 20:29:01 +0000824 // Insert commas as we go... the first arg doesn't get a comma
Misha Brukmane5242de2004-04-28 19:24:28 +0000825 if (Arg != &Arg->getParent()->afront()) *Out << ", ";
Chris Lattner00950542001-06-06 20:29:01 +0000826
827 // Output type...
Chris Lattnerc1824992001-10-29 16:05:51 +0000828 printType(Arg->getType());
Chris Lattner00950542001-06-06 20:29:01 +0000829
830 // Output name, if available...
831 if (Arg->hasName())
Misha Brukman40c732c2004-06-04 21:11:51 +0000832 *Out << ' ' << getLLVMName(Arg->getName());
Chris Lattner00950542001-06-06 20:29:01 +0000833}
834
Misha Brukmanab5c6002004-03-02 00:22:19 +0000835/// printBasicBlock - This member is called for each basic block in a method.
836///
Chris Lattnerc1824992001-10-29 16:05:51 +0000837void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Chris Lattner00950542001-06-06 20:29:01 +0000838 if (BB->hasName()) { // Print out the label if it exists...
Misha Brukman40c732c2004-06-04 21:11:51 +0000839 *Out << "\n" << BB->getName() << ':';
Chris Lattnerafc38682002-05-14 16:02:05 +0000840 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000841 *Out << "\n; <label>:" << Machine.getSlot(BB);
Chris Lattner061269b2002-10-02 19:38:55 +0000842 }
Chris Lattner4e4d8622003-11-20 00:09:43 +0000843
844 if (BB->getParent() == 0)
Misha Brukmane5242de2004-04-28 19:24:28 +0000845 *Out << "\t\t; Error: Block without parent!";
Chris Lattner4e4d8622003-11-20 00:09:43 +0000846 else {
847 if (BB != &BB->getParent()->front()) { // Not the entry block?
848 // Output predecessors for the block...
Misha Brukmane5242de2004-04-28 19:24:28 +0000849 *Out << "\t\t;";
Chris Lattner4e4d8622003-11-20 00:09:43 +0000850 pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
851
852 if (PI == PE) {
Misha Brukmane5242de2004-04-28 19:24:28 +0000853 *Out << " No predecessors!";
Chris Lattner4e4d8622003-11-20 00:09:43 +0000854 } else {
Misha Brukmane5242de2004-04-28 19:24:28 +0000855 *Out << " preds =";
Chris Lattner40efcec2003-11-16 22:59:57 +0000856 writeOperand(*PI, false, true);
Chris Lattner4e4d8622003-11-20 00:09:43 +0000857 for (++PI; PI != PE; ++PI) {
Misha Brukman40c732c2004-06-04 21:11:51 +0000858 *Out << ',';
Chris Lattner4e4d8622003-11-20 00:09:43 +0000859 writeOperand(*PI, false, true);
860 }
Chris Lattner40efcec2003-11-16 22:59:57 +0000861 }
Chris Lattner061269b2002-10-02 19:38:55 +0000862 }
Chris Lattner00950542001-06-06 20:29:01 +0000863 }
Chris Lattnerafc38682002-05-14 16:02:05 +0000864
Misha Brukmane5242de2004-04-28 19:24:28 +0000865 *Out << "\n";
Chris Lattner00950542001-06-06 20:29:01 +0000866
Misha Brukmane5242de2004-04-28 19:24:28 +0000867 if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, *Out);
Chris Lattner95e5a2c2003-10-30 23:41:03 +0000868
Chris Lattner007377f2001-09-07 16:36:04 +0000869 // Output all of the instructions in the basic block...
Chris Lattner7e708292002-06-25 16:13:24 +0000870 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
871 printInstruction(*I);
Chris Lattner9f717ef2004-03-08 18:51:45 +0000872
Misha Brukmane5242de2004-04-28 19:24:28 +0000873 if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, *Out);
Chris Lattner00950542001-06-06 20:29:01 +0000874}
875
Chris Lattnere02fa852001-10-13 06:42:36 +0000876
Misha Brukmanab5c6002004-03-02 00:22:19 +0000877/// printInfoComment - Print a little comment after the instruction indicating
878/// which slot it occupies.
879///
Chris Lattner7e708292002-06-25 16:13:24 +0000880void AssemblyWriter::printInfoComment(const Value &V) {
881 if (V.getType() != Type::VoidTy) {
Misha Brukmane5242de2004-04-28 19:24:28 +0000882 *Out << "\t\t; <";
Misha Brukman40c732c2004-06-04 21:11:51 +0000883 printType(V.getType()) << '>';
Chris Lattnere02fa852001-10-13 06:42:36 +0000884
Chris Lattner7e708292002-06-25 16:13:24 +0000885 if (!V.hasName()) {
Misha Brukman40c732c2004-06-04 21:11:51 +0000886 *Out << ':' << Machine.getSlot(&V); // Print out the def slot taken.
Chris Lattnere02fa852001-10-13 06:42:36 +0000887 }
Misha Brukman40c732c2004-06-04 21:11:51 +0000888 *Out << " [#uses=" << V.use_size() << ']'; // Output # uses
Chris Lattnere02fa852001-10-13 06:42:36 +0000889 }
890}
891
Misha Brukmanab5c6002004-03-02 00:22:19 +0000892/// printInstruction - This member is called for each Instruction in a method.
893///
Chris Lattner7e708292002-06-25 16:13:24 +0000894void AssemblyWriter::printInstruction(const Instruction &I) {
Misha Brukmane5242de2004-04-28 19:24:28 +0000895 if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, *Out);
Chris Lattner95e5a2c2003-10-30 23:41:03 +0000896
Misha Brukmane5242de2004-04-28 19:24:28 +0000897 *Out << "\t";
Chris Lattner00950542001-06-06 20:29:01 +0000898
899 // Print out name if it exists...
Chris Lattner7e708292002-06-25 16:13:24 +0000900 if (I.hasName())
Misha Brukmane5242de2004-04-28 19:24:28 +0000901 *Out << getLLVMName(I.getName()) << " = ";
Chris Lattner00950542001-06-06 20:29:01 +0000902
Chris Lattnere5e475e2003-09-08 17:45:59 +0000903 // If this is a volatile load or store, print out the volatile marker
904 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
905 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile()))
Misha Brukmane5242de2004-04-28 19:24:28 +0000906 *Out << "volatile ";
Chris Lattnere5e475e2003-09-08 17:45:59 +0000907
Chris Lattner00950542001-06-06 20:29:01 +0000908 // Print out the opcode...
Misha Brukmane5242de2004-04-28 19:24:28 +0000909 *Out << I.getOpcodeName();
Chris Lattner00950542001-06-06 20:29:01 +0000910
911 // Print out the type of the operands...
Chris Lattner7e708292002-06-25 16:13:24 +0000912 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
Chris Lattner00950542001-06-06 20:29:01 +0000913
914 // Special case conditional branches to swizzle the condition out to the front
Chris Lattner7e708292002-06-25 16:13:24 +0000915 if (isa<BranchInst>(I) && I.getNumOperands() > 1) {
916 writeOperand(I.getOperand(2), true);
Misha Brukman40c732c2004-06-04 21:11:51 +0000917 *Out << ',';
Chris Lattner00950542001-06-06 20:29:01 +0000918 writeOperand(Operand, true);
Misha Brukman40c732c2004-06-04 21:11:51 +0000919 *Out << ',';
Chris Lattner7e708292002-06-25 16:13:24 +0000920 writeOperand(I.getOperand(1), true);
Chris Lattner00950542001-06-06 20:29:01 +0000921
Chris Lattner94dc1f22002-04-13 18:34:38 +0000922 } else if (isa<SwitchInst>(I)) {
Chris Lattner00950542001-06-06 20:29:01 +0000923 // Special case switch statement to get formatting nice and correct...
Misha Brukman40c732c2004-06-04 21:11:51 +0000924 writeOperand(Operand , true); *Out << ',';
Misha Brukmane5242de2004-04-28 19:24:28 +0000925 writeOperand(I.getOperand(1), true); *Out << " [";
Chris Lattner00950542001-06-06 20:29:01 +0000926
Chris Lattner7e708292002-06-25 16:13:24 +0000927 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukmane5242de2004-04-28 19:24:28 +0000928 *Out << "\n\t\t";
Misha Brukman40c732c2004-06-04 21:11:51 +0000929 writeOperand(I.getOperand(op ), true); *Out << ',';
Chris Lattner7e708292002-06-25 16:13:24 +0000930 writeOperand(I.getOperand(op+1), true);
Chris Lattner00950542001-06-06 20:29:01 +0000931 }
Misha Brukmane5242de2004-04-28 19:24:28 +0000932 *Out << "\n\t]";
Chris Lattnerb00c5822001-10-02 03:41:24 +0000933 } else if (isa<PHINode>(I)) {
Misha Brukman40c732c2004-06-04 21:11:51 +0000934 *Out << ' ';
Chris Lattner7e708292002-06-25 16:13:24 +0000935 printType(I.getType());
Misha Brukman40c732c2004-06-04 21:11:51 +0000936 *Out << ' ';
Chris Lattner00950542001-06-06 20:29:01 +0000937
Chris Lattner7e708292002-06-25 16:13:24 +0000938 for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukmane5242de2004-04-28 19:24:28 +0000939 if (op) *Out << ", ";
Misha Brukman40c732c2004-06-04 21:11:51 +0000940 *Out << '[';
941 writeOperand(I.getOperand(op ), false); *Out << ',';
Misha Brukmane5242de2004-04-28 19:24:28 +0000942 writeOperand(I.getOperand(op+1), false); *Out << " ]";
Chris Lattnerc24d2082001-06-11 15:04:20 +0000943 }
Chris Lattnere02fa852001-10-13 06:42:36 +0000944 } else if (isa<ReturnInst>(I) && !Operand) {
Misha Brukmane5242de2004-04-28 19:24:28 +0000945 *Out << " void";
Chris Lattnere02fa852001-10-13 06:42:36 +0000946 } else if (isa<CallInst>(I)) {
Chris Lattner7a012292003-08-05 15:34:45 +0000947 const PointerType *PTy = cast<PointerType>(Operand->getType());
948 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
949 const Type *RetTy = FTy->getReturnType();
Chris Lattner268de042001-11-06 21:28:12 +0000950
Chris Lattner7a012292003-08-05 15:34:45 +0000951 // If possible, print out the short form of the call instruction. We can
Chris Lattnerb5794002002-04-07 22:49:37 +0000952 // only do this if the first argument is a pointer to a nonvararg function,
Chris Lattner7a012292003-08-05 15:34:45 +0000953 // and if the return type is not a pointer to a function.
Chris Lattner268de042001-11-06 21:28:12 +0000954 //
Chris Lattner7a012292003-08-05 15:34:45 +0000955 if (!FTy->isVarArg() &&
Chris Lattner94dc1f22002-04-13 18:34:38 +0000956 (!isa<PointerType>(RetTy) ||
Chris Lattnerc1b27182002-07-25 20:58:51 +0000957 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Misha Brukman40c732c2004-06-04 21:11:51 +0000958 *Out << ' '; printType(RetTy);
Chris Lattner268de042001-11-06 21:28:12 +0000959 writeOperand(Operand, false);
960 } else {
961 writeOperand(Operand, true);
962 }
Misha Brukman40c732c2004-06-04 21:11:51 +0000963 *Out << '(';
Chris Lattner7e708292002-06-25 16:13:24 +0000964 if (I.getNumOperands() > 1) writeOperand(I.getOperand(1), true);
965 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; ++op) {
Misha Brukman40c732c2004-06-04 21:11:51 +0000966 *Out << ',';
Chris Lattner7e708292002-06-25 16:13:24 +0000967 writeOperand(I.getOperand(op), true);
Chris Lattner00950542001-06-06 20:29:01 +0000968 }
969
Misha Brukmane5242de2004-04-28 19:24:28 +0000970 *Out << " )";
Chris Lattner7e708292002-06-25 16:13:24 +0000971 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Chris Lattner7a012292003-08-05 15:34:45 +0000972 const PointerType *PTy = cast<PointerType>(Operand->getType());
973 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
974 const Type *RetTy = FTy->getReturnType();
975
976 // If possible, print out the short form of the invoke instruction. We can
977 // only do this if the first argument is a pointer to a nonvararg function,
978 // and if the return type is not a pointer to a function.
979 //
980 if (!FTy->isVarArg() &&
981 (!isa<PointerType>(RetTy) ||
982 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Misha Brukman40c732c2004-06-04 21:11:51 +0000983 *Out << ' '; printType(RetTy);
Chris Lattner7a012292003-08-05 15:34:45 +0000984 writeOperand(Operand, false);
985 } else {
986 writeOperand(Operand, true);
987 }
988
Misha Brukman40c732c2004-06-04 21:11:51 +0000989 *Out << '(';
Chris Lattner7e708292002-06-25 16:13:24 +0000990 if (I.getNumOperands() > 3) writeOperand(I.getOperand(3), true);
991 for (unsigned op = 4, Eop = I.getNumOperands(); op < Eop; ++op) {
Misha Brukman40c732c2004-06-04 21:11:51 +0000992 *Out << ',';
Chris Lattner7e708292002-06-25 16:13:24 +0000993 writeOperand(I.getOperand(op), true);
Chris Lattnere02fa852001-10-13 06:42:36 +0000994 }
995
Misha Brukmane5242de2004-04-28 19:24:28 +0000996 *Out << " )\n\t\t\tto";
Chris Lattnere02fa852001-10-13 06:42:36 +0000997 writeOperand(II->getNormalDest(), true);
Misha Brukmane5242de2004-04-28 19:24:28 +0000998 *Out << " unwind";
Chris Lattneraeb2a1d2004-02-08 21:44:31 +0000999 writeOperand(II->getUnwindDest(), true);
Chris Lattnere02fa852001-10-13 06:42:36 +00001000
Chris Lattner7e708292002-06-25 16:13:24 +00001001 } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(&I)) {
Misha Brukman40c732c2004-06-04 21:11:51 +00001002 *Out << ' ';
Chris Lattner94dc1f22002-04-13 18:34:38 +00001003 printType(AI->getType()->getElementType());
1004 if (AI->isArrayAllocation()) {
Misha Brukman40c732c2004-06-04 21:11:51 +00001005 *Out << ',';
Chris Lattner94dc1f22002-04-13 18:34:38 +00001006 writeOperand(AI->getArraySize(), true);
Chris Lattner00950542001-06-06 20:29:01 +00001007 }
Chris Lattnere02fa852001-10-13 06:42:36 +00001008 } else if (isa<CastInst>(I)) {
Chris Lattner41495a22003-11-17 01:17:04 +00001009 if (Operand) writeOperand(Operand, true); // Work with broken code
Misha Brukmane5242de2004-04-28 19:24:28 +00001010 *Out << " to ";
Chris Lattner7e708292002-06-25 16:13:24 +00001011 printType(I.getType());
Chris Lattner4d45bd02003-10-18 05:57:43 +00001012 } else if (isa<VAArgInst>(I)) {
Chris Lattner41495a22003-11-17 01:17:04 +00001013 if (Operand) writeOperand(Operand, true); // Work with broken code
Misha Brukmane5242de2004-04-28 19:24:28 +00001014 *Out << ", ";
Chris Lattner8f77dae2003-05-08 02:44:12 +00001015 printType(I.getType());
Chris Lattner4d45bd02003-10-18 05:57:43 +00001016 } else if (const VANextInst *VAN = dyn_cast<VANextInst>(&I)) {
Chris Lattner41495a22003-11-17 01:17:04 +00001017 if (Operand) writeOperand(Operand, true); // Work with broken code
Misha Brukmane5242de2004-04-28 19:24:28 +00001018 *Out << ", ";
Chris Lattner4d45bd02003-10-18 05:57:43 +00001019 printType(VAN->getArgType());
Chris Lattner00950542001-06-06 20:29:01 +00001020 } else if (Operand) { // Print the normal way...
1021
1022 // PrintAllTypes - Instructions who have operands of all the same type
1023 // omit the type from all but the first operand. If the instruction has
1024 // different type operands (for example br), then they are all printed.
1025 bool PrintAllTypes = false;
1026 const Type *TheType = Operand->getType();
Chris Lattner00950542001-06-06 20:29:01 +00001027
Chris Lattnercfdd1482004-03-12 05:53:14 +00001028 // Shift Left & Right print both types even for Ubyte LHS, and select prints
1029 // types even if all operands are bools.
1030 if (isa<ShiftInst>(I) || isa<SelectInst>(I)) {
Chris Lattnerffd9bf42003-04-16 20:20:02 +00001031 PrintAllTypes = true;
1032 } else {
1033 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
1034 Operand = I.getOperand(i);
1035 if (Operand->getType() != TheType) {
1036 PrintAllTypes = true; // We have differing types! Print them all!
1037 break;
1038 }
Chris Lattner00950542001-06-06 20:29:01 +00001039 }
1040 }
Chris Lattnerffd9bf42003-04-16 20:20:02 +00001041
Chris Lattnerc1824992001-10-29 16:05:51 +00001042 if (!PrintAllTypes) {
Misha Brukman40c732c2004-06-04 21:11:51 +00001043 *Out << ' ';
Chris Lattnerffd9bf42003-04-16 20:20:02 +00001044 printType(TheType);
Chris Lattnerc1824992001-10-29 16:05:51 +00001045 }
Chris Lattner00950542001-06-06 20:29:01 +00001046
Chris Lattner7e708292002-06-25 16:13:24 +00001047 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Misha Brukman40c732c2004-06-04 21:11:51 +00001048 if (i) *Out << ',';
Chris Lattner7e708292002-06-25 16:13:24 +00001049 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner00950542001-06-06 20:29:01 +00001050 }
1051 }
1052
Chris Lattnere02fa852001-10-13 06:42:36 +00001053 printInfoComment(I);
Misha Brukmane5242de2004-04-28 19:24:28 +00001054 *Out << "\n";
Chris Lattner00950542001-06-06 20:29:01 +00001055}
1056
1057
1058//===----------------------------------------------------------------------===//
1059// External Interface declarations
1060//===----------------------------------------------------------------------===//
1061
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001062void Module::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001063 SlotMachine SlotTable(this);
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001064 AssemblyWriter W(o, SlotTable, this, AAW);
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001065 W.write(this);
Chris Lattner00950542001-06-06 20:29:01 +00001066}
1067
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001068void GlobalVariable::print(std::ostream &o) const {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001069 SlotMachine SlotTable(getParent());
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001070 AssemblyWriter W(o, SlotTable, getParent(), 0);
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001071 W.write(this);
Chris Lattnerb0e45232001-09-10 20:08:19 +00001072}
1073
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001074void Function::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001075 SlotMachine SlotTable(getParent());
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001076 AssemblyWriter W(o, SlotTable, getParent(), AAW);
Chris Lattner00950542001-06-06 20:29:01 +00001077
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001078 W.write(this);
Chris Lattner00950542001-06-06 20:29:01 +00001079}
1080
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001081void BasicBlock::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001082 SlotMachine SlotTable(getParent());
Chris Lattnerc1824992001-10-29 16:05:51 +00001083 AssemblyWriter W(o, SlotTable,
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001084 getParent() ? getParent()->getParent() : 0, AAW);
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001085 W.write(this);
Chris Lattner00950542001-06-06 20:29:01 +00001086}
1087
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001088void Instruction::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001089 const Function *F = getParent() ? getParent()->getParent() : 0;
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001090 SlotMachine SlotTable(F);
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001091 AssemblyWriter W(o, SlotTable, F ? F->getParent() : 0, AAW);
Chris Lattner00950542001-06-06 20:29:01 +00001092
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001093 W.write(this);
Chris Lattner00950542001-06-06 20:29:01 +00001094}
Chris Lattnerda1fbcc2001-11-07 04:21:57 +00001095
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001096void Constant::print(std::ostream &o) const {
1097 if (this == 0) { o << "<null> constant value\n"; return; }
Chris Lattner3bc06b32002-09-10 15:53:49 +00001098
1099 // Handle CPR's special, because they have context information...
1100 if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(this)) {
1101 CPR->getValue()->print(o); // Print as a global value, with context info.
1102 return;
1103 }
1104
Misha Brukman40c732c2004-06-04 21:11:51 +00001105 o << ' ' << getType()->getDescription() << ' ';
Chris Lattner66e810b2002-04-18 18:53:13 +00001106
Chris Lattner7b13f562003-05-08 02:08:14 +00001107 std::map<const Type *, std::string> TypeTable;
Chris Lattner66e810b2002-04-18 18:53:13 +00001108 WriteConstantInt(o, this, false, TypeTable, 0);
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001109}
1110
1111void Type::print(std::ostream &o) const {
1112 if (this == 0)
1113 o << "<null Type>";
1114 else
1115 o << getDescription();
1116}
1117
Chris Lattner73e21422002-04-09 19:48:49 +00001118void Argument::print(std::ostream &o) const {
Misha Brukman40c732c2004-06-04 21:11:51 +00001119 o << getType() << ' ' << getName();
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001120}
1121
Reid Spencerfa452c02004-05-25 18:14:38 +00001122// Value::dump - allow easy printing of Values from the debugger.
1123// Located here because so much of the needed functionality is here.
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001124void Value::dump() const { print(std::cerr); }
Reid Spencerfa452c02004-05-25 18:14:38 +00001125
1126// Type::dump - allow easy printing of Values from the debugger.
1127// Located here because so much of the needed functionality is here.
Reid Spencer9231ac82004-05-25 08:53:40 +00001128void Type::dump() const { print(std::cerr); }
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001129
1130//===----------------------------------------------------------------------===//
1131// CachedWriter Class Implementation
1132//===----------------------------------------------------------------------===//
1133
Chris Lattnerda1fbcc2001-11-07 04:21:57 +00001134void CachedWriter::setModule(const Module *M) {
1135 delete SC; delete AW;
1136 if (M) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001137 SC = new SlotMachine(M );
Misha Brukman40c732c2004-06-04 21:11:51 +00001138 AW = new AssemblyWriter(Out, *SC, M, 0);
Chris Lattnerda1fbcc2001-11-07 04:21:57 +00001139 } else {
1140 SC = 0; AW = 0;
1141 }
1142}
1143
1144CachedWriter::~CachedWriter() {
1145 delete AW;
1146 delete SC;
1147}
1148
1149CachedWriter &CachedWriter::operator<<(const Value *V) {
1150 assert(AW && SC && "CachedWriter does not have a current module!");
1151 switch (V->getValueType()) {
1152 case Value::ConstantVal:
Chris Lattner66e810b2002-04-18 18:53:13 +00001153 case Value::ArgumentVal: AW->writeOperand(V, true, true); break;
Chris Lattner949a3622003-07-23 15:30:06 +00001154 case Value::TypeVal: AW->write(cast<Type>(V)); break;
Chris Lattnerda1fbcc2001-11-07 04:21:57 +00001155 case Value::InstructionVal: AW->write(cast<Instruction>(V)); break;
1156 case Value::BasicBlockVal: AW->write(cast<BasicBlock>(V)); break;
Chris Lattner79df7c02002-03-26 18:01:55 +00001157 case Value::FunctionVal: AW->write(cast<Function>(V)); break;
Chris Lattnerda1fbcc2001-11-07 04:21:57 +00001158 case Value::GlobalVariableVal: AW->write(cast<GlobalVariable>(V)); break;
Misha Brukman40c732c2004-06-04 21:11:51 +00001159 default: Out << "<unknown value type: " << V->getValueType() << '>'; break;
Chris Lattnerda1fbcc2001-11-07 04:21:57 +00001160 }
1161 return *this;
1162}
Misha Brukman5cf1acf2004-04-28 15:31:21 +00001163
1164CachedWriter& CachedWriter::operator<<(const Type *X) {
1165 if (SymbolicTypes) {
1166 const Module *M = AW->getModule();
Misha Brukman40c732c2004-06-04 21:11:51 +00001167 if (M) WriteTypeSymbolic(Out, X, M);
Misha Brukman5cf1acf2004-04-28 15:31:21 +00001168 return *this;
1169 } else
1170 return *this << (const Value*)X;
1171}
Misha Brukmane5242de2004-04-28 19:24:28 +00001172
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001173//===----------------------------------------------------------------------===//
1174//===-- SlotMachine Implementation
1175//===----------------------------------------------------------------------===//
1176
1177#if 0
1178#define SC_DEBUG(X) std::cerr << X
1179#else
1180#define SC_DEBUG(X)
1181#endif
1182
1183// Module level constructor. Causes the contents of the Module (sans functions)
1184// to be added to the slot table.
1185SlotMachine::SlotMachine(const Module *M)
Reid Spencerb03de0c2004-05-26 21:56:09 +00001186 : TheModule(M) ///< Saved for lazy initialization.
1187 , TheFunction(0)
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001188 , mMap()
1189 , fMap()
1190{
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001191}
1192
1193// Function level constructor. Causes the contents of the Module and the one
1194// function provided to be added to the slot table.
1195SlotMachine::SlotMachine(const Function *F )
Reid Spencerb03de0c2004-05-26 21:56:09 +00001196 : TheModule( F ? F->getParent() : 0 ) ///< Saved for lazy initialization
1197 , TheFunction(F) ///< Saved for lazy initialization
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001198 , mMap()
1199 , fMap()
1200{
Reid Spencerb03de0c2004-05-26 21:56:09 +00001201}
1202
1203inline void SlotMachine::initialize(void) {
1204 if ( TheModule) {
1205 processModule();
1206 TheModule = 0; ///< Prevent re-processing next time we're called.
1207 }
1208 if ( TheFunction ) {
1209 processFunction();
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001210 }
1211}
1212
1213// Iterate through all the global variables, functions, and global
1214// variable initializers and create slots for them.
1215void SlotMachine::processModule() {
1216 SC_DEBUG("begin processModule!\n");
1217
1218 // Add all of the global variables to the value table...
1219 for (Module::const_giterator I = TheModule->gbegin(), E = TheModule->gend();
1220 I != E; ++I)
1221 createSlot(I);
1222
1223 // Add all the functions to the table
1224 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
1225 I != E; ++I)
1226 createSlot(I);
1227
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001228 SC_DEBUG("end processModule!\n");
1229}
1230
1231
Reid Spencerb03de0c2004-05-26 21:56:09 +00001232// Process the arguments, basic blocks, and instructions of a function.
1233void SlotMachine::processFunction() {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001234 SC_DEBUG("begin processFunction!\n");
1235
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001236 // Add all the function arguments
Reid Spencerb03de0c2004-05-26 21:56:09 +00001237 for(Function::const_aiterator AI = TheFunction->abegin(),
1238 AE = TheFunction->aend(); AI != AE; ++AI)
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001239 createSlot(AI);
1240
1241 SC_DEBUG("Inserting Instructions:\n");
1242
1243 // Add all of the basic blocks and instructions
Reid Spencerb03de0c2004-05-26 21:56:09 +00001244 for (Function::const_iterator BB = TheFunction->begin(),
1245 E = TheFunction->end(); BB != E; ++BB) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001246 createSlot(BB);
1247 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
1248 createSlot(I);
1249 }
1250 }
1251
1252 SC_DEBUG("end processFunction!\n");
1253}
1254
1255// Clean up after incorporating a function. This is the only way
Reid Spencerb03de0c2004-05-26 21:56:09 +00001256// to get out of the function incorporation state that affects the
1257// getSlot/createSlot lock. Function incorporation state is indicated
1258// by TheFunction != 0.
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001259void SlotMachine::purgeFunction() {
1260 SC_DEBUG("begin purgeFunction!\n");
1261 fMap.clear(); // Simply discard the function level map
Reid Spencerb03de0c2004-05-26 21:56:09 +00001262 TheFunction = 0;
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001263 SC_DEBUG("end purgeFunction!\n");
1264}
1265
1266/// Get the slot number for a value. This function will assert if you
1267/// ask for a Value that hasn't previously been inserted with createSlot.
1268/// Types are forbidden because Type does not inherit from Value (any more).
Reid Spencerb03de0c2004-05-26 21:56:09 +00001269unsigned SlotMachine::getSlot(const Value *V) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001270 assert( V && "Can't get slot for null Value" );
1271 assert( !isa<Type>(V) && "Can't get slot for a type" );
Reid Spencerb03de0c2004-05-26 21:56:09 +00001272 assert(!isa<Constant>(V) || isa<GlobalValue>(V) &&
1273 "Can't insert a non-GlobalValue Constant into SlotMachine");
1274
1275 // Check for uninitialized state and do lazy initialization
1276 this->initialize();
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001277
1278 // Do not number CPR's at all. They are an abomination
1279 if ( const ConstantPointerRef* CPR = dyn_cast<ConstantPointerRef>(V) )
1280 V = CPR->getValue() ;
1281
1282 // Get the type of the value
1283 const Type* VTy = V->getType();
1284
1285 // Find the type plane in the module map
1286 TypedPlanes::const_iterator MI = mMap.find(VTy);
1287
Reid Spencerb03de0c2004-05-26 21:56:09 +00001288 if ( TheFunction ) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001289 // Lookup the type in the function map too
1290 TypedPlanes::const_iterator FI = fMap.find(VTy);
1291 // If there is a corresponding type plane in the function map
1292 if ( FI != fMap.end() ) {
1293 // Lookup the Value in the function map
1294 ValueMap::const_iterator FVI = FI->second.map.find(V);
1295 // If the value doesn't exist in the function map
1296 if ( FVI == FI->second.map.end() ) {
Reid Spencerb03de0c2004-05-26 21:56:09 +00001297 // Look up the value in the module map
1298 ValueMap::const_iterator MVI = MI->second.map.find(V);
1299 // If we didn't find it, it wasn't inserted
1300 assert( MVI != MI->second.map.end() && "Value not found");
1301 // We found it only at the module level
1302 return MVI->second;
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001303
1304 // else the value exists in the function map
1305 } else {
Reid Spencerb03de0c2004-05-26 21:56:09 +00001306 // Return the slot number as the module's contribution to
1307 // the type plane plus the index in the function's contribution
1308 // to the type plane.
1309 return MI->second.next_slot + FVI->second;
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001310 }
1311
1312 // else there is not a corresponding type plane in the function map
1313 } else {
1314 assert( MI != mMap.end() && "No such type plane!" );
1315 // Look up the value in the module's map
1316 ValueMap::const_iterator MVI = MI->second.map.find(V);
1317 // If we didn't find it, it wasn't inserted.
1318 assert( MVI != MI->second.map.end() && "Value not found");
1319 // We found it only in the module level and function level
1320 // didn't even have a type plane.
1321 return MVI->second;
1322 }
1323 }
1324
Reid Spencerb03de0c2004-05-26 21:56:09 +00001325 // N.B. Can only get here if !TheFunction
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001326
1327 // Make sure the type plane exists
1328 assert( MI != mMap.end() && "No such type plane!" );
1329 // Lookup the value in the module's map
1330 ValueMap::const_iterator MVI = MI->second.map.find(V);
1331 // Make sure we found it.
1332 assert( MVI != MI->second.map.end() && "Value not found" );
1333 // Return it.
1334 return MVI->second;
1335}
1336
1337
1338// Create a new slot, or return the existing slot if it is already
1339// inserted. Note that the logic here parallels getSlot but instead
1340// of asserting when the Value* isn't found, it inserts the value.
1341unsigned SlotMachine::createSlot(const Value *V) {
1342 assert( V && "Can't insert a null Value to SlotMachine");
1343 assert( !isa<Type>(V) && "Can't insert a Type into SlotMachine");
Reid Spencerb03de0c2004-05-26 21:56:09 +00001344 assert(!isa<Constant>(V) || isa<GlobalValue>(V) &&
1345 "Can't insert a non-GlobalValue Constant into SlotMachine");
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001346
1347 const Type* VTy = V->getType();
1348
1349 // Just ignore void typed things
1350 if (VTy == Type::VoidTy) return 0; // FIXME: Wrong return value!
1351
1352 // Look up the type plane for the Value's type from the module map
1353 TypedPlanes::const_iterator MI = mMap.find(VTy);
1354
Reid Spencerb03de0c2004-05-26 21:56:09 +00001355 if ( TheFunction ) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001356 // Get the type plane for the Value's type from the function map
1357 TypedPlanes::const_iterator FI = fMap.find(VTy);
1358 // If there is a corresponding type plane in the function map
1359 if ( FI != fMap.end() ) {
1360 // Lookup the Value in the function map
1361 ValueMap::const_iterator FVI = FI->second.map.find(V);
1362 // If the value doesn't exist in the function map
1363 if ( FVI == FI->second.map.end() ) {
Reid Spencerb03de0c2004-05-26 21:56:09 +00001364 // If there is no corresponding type plane in the module map
1365 if ( MI == mMap.end() )
1366 return insertValue(V);
1367 // Look up the value in the module map
1368 ValueMap::const_iterator MVI = MI->second.map.find(V);
1369 // If we didn't find it, it wasn't inserted
1370 if ( MVI == MI->second.map.end() )
1371 return insertValue(V);
1372 else
1373 // We found it only at the module level
1374 return MVI->second;
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001375
1376 // else the value exists in the function map
1377 } else {
Reid Spencerb03de0c2004-05-26 21:56:09 +00001378 if ( MI == mMap.end() )
1379 return FVI->second;
1380 else
1381 // Return the slot number as the module's contribution to
1382 // the type plane plus the index in the function's contribution
1383 // to the type plane.
1384 return MI->second.next_slot + FVI->second;
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001385 }
1386
1387 // else there is not a corresponding type plane in the function map
1388 } else {
1389 // If the type plane doesn't exists at the module level
1390 if ( MI == mMap.end() ) {
Reid Spencerb03de0c2004-05-26 21:56:09 +00001391 return insertValue(V);
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001392 // else type plane exists at the module level, examine it
1393 } else {
Reid Spencerb03de0c2004-05-26 21:56:09 +00001394 // Look up the value in the module's map
1395 ValueMap::const_iterator MVI = MI->second.map.find(V);
1396 // If we didn't find it there either
1397 if ( MVI == MI->second.map.end() )
1398 // Return the slot number as the module's contribution to
1399 // the type plane plus the index of the function map insertion.
1400 return MI->second.next_slot + insertValue(V);
1401 else
1402 return MVI->second;
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001403 }
1404 }
1405 }
1406
Reid Spencerb03de0c2004-05-26 21:56:09 +00001407 // N.B. Can only get here if !TheFunction
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001408
1409 // If the module map's type plane is not for the Value's type
1410 if ( MI != mMap.end() ) {
1411 // Lookup the value in the module's map
1412 ValueMap::const_iterator MVI = MI->second.map.find(V);
1413 if ( MVI != MI->second.map.end() )
1414 return MVI->second;
1415 }
1416
1417 return insertValue(V);
1418}
1419
1420
1421// Low level insert function. Minimal checking is done. This
1422// function is just for the convenience of createSlot (above).
1423unsigned SlotMachine::insertValue(const Value *V ) {
1424 assert(V && "Can't insert a null Value into SlotMachine!");
1425 assert(!isa<Type>(V) && "Can't insert a Type into SlotMachine!");
Reid Spencerb03de0c2004-05-26 21:56:09 +00001426 assert(!isa<Constant>(V) || isa<GlobalValue>(V) &&
1427 "Can't insert a non-GlobalValue Constant into SlotMachine");
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001428
Reid Spencerb03de0c2004-05-26 21:56:09 +00001429 // If this value does not contribute to a plane (is void)
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001430 // or if the value already has a name then ignore it.
Reid Spencerb03de0c2004-05-26 21:56:09 +00001431 if (V->getType() == Type::VoidTy || V->hasName() ) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001432 SC_DEBUG("ignored value " << *V << "\n");
1433 return 0; // FIXME: Wrong return value
1434 }
1435
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001436 const Type *VTy = V->getType();
1437 unsigned DestSlot = 0;
1438
Reid Spencerb03de0c2004-05-26 21:56:09 +00001439 if ( TheFunction ) {
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001440 TypedPlanes::iterator I = fMap.find( VTy );
1441 if ( I == fMap.end() )
1442 I = fMap.insert(std::make_pair(VTy,Plane())).first;
1443 DestSlot = I->second.map[V] = I->second.next_slot++;
1444 } else {
1445 TypedPlanes::iterator I = mMap.find( VTy );
1446 if ( I == mMap.end() )
1447 I = mMap.insert(std::make_pair(VTy,Plane())).first;
1448 DestSlot = I->second.map[V] = I->second.next_slot++;
1449 }
1450
1451 SC_DEBUG(" Inserting value [" << VTy << "] = " << V << " slot=" <<
Reid Spencerb03de0c2004-05-26 21:56:09 +00001452 DestSlot << " [");
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001453 // G = Global, C = Constant, T = Type, F = Function, o = other
Misha Brukman40c732c2004-06-04 21:11:51 +00001454 SC_DEBUG((isa<GlobalVariable>(V) ? 'G' : (isa<Constant>(V) ? 'C' :
1455 (isa<Function>(V) ? 'F' : 'o'))));
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001456 SC_DEBUG("]\n");
1457 return DestSlot;
1458}
1459
Reid Spencer9231ac82004-05-25 08:53:40 +00001460// vim: sw=2