blob: 9a791b43171ae45f8e9e7e29dbd568bca3b55c2b [file] [log] [blame]
Chris Lattnerf7e79482002-04-07 22:31:46 +00001//===-- AsmWriter.cpp - Printing LLVM as an assembly file -----------------===//
John Criswell482202a2003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
10// This library implements the functionality defined in llvm/Assembly/Writer.h
11//
Chris Lattner189088e2002-04-12 18:21:53 +000012// Note that these routines must be extremely tolerant of various errors in the
Chris Lattnerf70da102003-05-08 02:44:12 +000013// LLVM code, because it can be used for debugging transformations.
Chris Lattner189088e2002-04-12 18:21:53 +000014//
Chris Lattner2f7c9632001-06-06 20:29:01 +000015//===----------------------------------------------------------------------===//
16
Chris Lattner7db79582001-11-07 04:21:57 +000017#include "llvm/Assembly/CachedWriter.h"
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +000018#include "llvm/Assembly/Writer.h"
Chris Lattner7f8845a2002-07-23 18:07:49 +000019#include "llvm/Assembly/PrintModulePass.h"
Chris Lattner8339f7d2003-10-30 23:41:03 +000020#include "llvm/Assembly/AsmAnnotationWriter.h"
Chris Lattnerc70b3f62004-01-20 19:50:34 +000021#include "llvm/Constants.h"
Chris Lattner913d18f2002-04-29 18:46:50 +000022#include "llvm/DerivedTypes.h"
Vikram S. Adveb952b542002-07-14 23:14:45 +000023#include "llvm/Instruction.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000024#include "llvm/iMemory.h"
Chris Lattner862e3382001-10-13 06:42:36 +000025#include "llvm/iTerminators.h"
Chris Lattnerfb5ae022001-12-03 18:02:31 +000026#include "llvm/iPHINode.h"
27#include "llvm/iOther.h"
Chris Lattnerc70b3f62004-01-20 19:50:34 +000028#include "llvm/Module.h"
Chris Lattnerfee714f2001-09-07 16:36:04 +000029#include "llvm/SymbolTable.h"
Misha Brukman4685e262004-04-28 15:31:21 +000030#include "llvm/Assembly/Writer.h"
Chris Lattner58185f22002-10-02 19:38:55 +000031#include "llvm/Support/CFG.h"
Chris Lattner5de22042001-11-27 00:03:19 +000032#include "Support/StringExtras.h"
33#include "Support/STLExtras.h"
Chris Lattnerfee714f2001-09-07 16:36:04 +000034#include <algorithm>
Chris Lattner189d19f2003-11-21 20:23:48 +000035using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000036
Reid Spencer16f2f7f2004-05-26 07:18:52 +000037namespace {
38
39/// This class provides computation of slot numbers for LLVM Assembly writing.
40/// @brief LLVM Assembly Writing Slot Computation.
41class SlotMachine {
42
43/// @name Types
44/// @{
45public:
46
47 /// @brief A mapping of Values to slot numbers
48 typedef std::map<const Value*, unsigned> ValueMap;
49
50 /// @brief A plane with next slot number and ValueMap
51 struct Plane {
52 unsigned next_slot; ///< The next slot number to use
53 ValueMap map; ///< The map of Value* -> unsigned
54 Plane() { next_slot = 0; } ///< Make sure we start at 0
55 };
56
57 /// @brief The map of planes by Type
58 typedef std::map<const Type*, Plane> TypedPlanes;
59
60/// @}
61/// @name Constructors
62/// @{
63public:
64 /// @brief Construct from a module
65 SlotMachine(const Module *M );
66
67 /// @brief Construct from a function, starting out in incorp state.
68 SlotMachine(const Function *F );
69
70/// @}
71/// @name Accessors
72/// @{
73public:
74 /// Return the slot number of the specified value in it's type
75 /// plane. Its an error to ask for something not in the SlotMachine.
76 /// Its an error to ask for a Type*
77 unsigned getSlot(const Value *V) const;
78
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.
85 void incorporateFunction(const Function *F);
86
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:
96 /// Values can be crammed into here at will. If they haven't
97 /// been inserted already, they get inserted, otherwise they are ignored.
98 /// Either way, the slot number for the Value* is returned.
99 unsigned createSlot(const Value *V);
100
101 /// Insert a value into the value table. Return the slot number
102 /// that it now occupies. BadThings(TM) will happen if you insert a
103 /// Value that's already been inserted.
104 unsigned insertValue( const Value *V );
105
106 /// Add all of the module level global variables (and their initializers)
107 /// and function declarations, but not the contents of those functions.
108 void processModule();
109
110 SlotMachine(const SlotMachine &); // DO NOT IMPLEMENT
111 void operator=(const SlotMachine &); // DO NOT IMPLEMENT
112
113/// @}
114/// @name Data
115/// @{
116public:
117
118 /// @brief The module for which we are holding slot numbers
119 const Module *TheModule;
120
121 /// @brief Whether or not we have a function incorporated
122 bool FunctionIncorporated;
123
124 /// @brief The TypePlanes map for the module level data
125 TypedPlanes mMap;
126
127 /// @brief The TypePlanes map for the function level data
128 TypedPlanes fMap;
129
130/// @}
131
132};
133
134}
135
Chris Lattnerc8b70922002-07-26 21:12:46 +0000136static RegisterPass<PrintModulePass>
137X("printm", "Print module to stderr",PassInfo::Analysis|PassInfo::Optimization);
138static RegisterPass<PrintFunctionPass>
139Y("print","Print function to stderr",PassInfo::Analysis|PassInfo::Optimization);
Chris Lattner7f8845a2002-07-23 18:07:49 +0000140
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000141static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
142 bool PrintName,
143 std::map<const Type *, std::string> &TypeTable,
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000144 SlotMachine *Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000145
Chris Lattnerb86620e2001-10-29 16:37:48 +0000146static const Module *getModuleFromVal(const Value *V) {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000147 if (const Argument *MA = dyn_cast<Argument>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +0000148 return MA->getParent() ? MA->getParent()->getParent() : 0;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000149 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +0000150 return BB->getParent() ? BB->getParent()->getParent() : 0;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000151 else if (const Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner57698e22002-03-26 18:01:55 +0000152 const Function *M = I->getParent() ? I->getParent()->getParent() : 0;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000153 return M ? M->getParent() : 0;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000154 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +0000155 return GV->getParent();
Chris Lattnerb86620e2001-10-29 16:37:48 +0000156 return 0;
157}
158
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000159static SlotMachine *createSlotMachine(const Value *V) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000160 assert(!isa<Type>(V) && "Can't create an SC for a type!");
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000161 if (const Argument *FA = dyn_cast<Argument>(V)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000162 return new SlotMachine(FA->getParent());
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000163 } else if (const Instruction *I = dyn_cast<Instruction>(V)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000164 return new SlotMachine(I->getParent()->getParent());
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000165 } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000166 return new SlotMachine(BB->getParent());
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000167 } else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)){
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000168 return new SlotMachine(GV->getParent());
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000169 } else if (const Function *Func = dyn_cast<Function>(V)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000170 return new SlotMachine(Func);
Chris Lattner7bfee412001-10-29 16:05:51 +0000171 }
172 return 0;
173}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000174
Chris Lattner1c343042003-08-22 05:40:38 +0000175// getLLVMName - Turn the specified string into an 'LLVM name', which is either
176// prefixed with % (if the string only contains simple characters) or is
177// surrounded with ""'s (if it has special chars in it).
178static std::string getLLVMName(const std::string &Name) {
179 assert(!Name.empty() && "Cannot get empty name!");
180
181 // First character cannot start with a number...
182 if (Name[0] >= '0' && Name[0] <= '9')
183 return "\"" + Name + "\"";
184
185 // Scan to see if we have any characters that are not on the "white list"
186 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
187 char C = Name[i];
188 assert(C != '"' && "Illegal character in LLVM value name!");
189 if ((C < 'a' || C > 'z') && (C < 'A' || C > 'Z') && (C < '0' || C > '9') &&
190 C != '-' && C != '.' && C != '_')
191 return "\"" + Name + "\"";
192 }
193
194 // If we get here, then the identifier is legal to use as a "VarID".
195 return "%"+Name;
196}
197
Chris Lattnerb86620e2001-10-29 16:37:48 +0000198
Misha Brukmanc566ca362004-03-02 00:22:19 +0000199/// fillTypeNameTable - If the module has a symbol table, take all global types
200/// and stuff their names into the TypeNames map.
201///
Chris Lattnerb86620e2001-10-29 16:37:48 +0000202static void fillTypeNameTable(const Module *M,
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000203 std::map<const Type *, std::string> &TypeNames) {
Chris Lattner98cf1f52002-11-20 18:36:02 +0000204 if (!M) return;
205 const SymbolTable &ST = M->getSymbolTable();
Reid Spencere7e96712004-05-25 08:53:40 +0000206 SymbolTable::type_const_iterator TI = ST.type_begin();
207 for (; TI != ST.type_end(); ++TI ) {
208 // As a heuristic, don't insert pointer to primitive types, because
209 // they are used too often to have a single useful name.
210 //
211 const Type *Ty = cast<Type>(TI->second);
212 if (!isa<PointerType>(Ty) ||
213 !cast<PointerType>(Ty)->getElementType()->isPrimitiveType() ||
214 isa<OpaqueType>(cast<PointerType>(Ty)->getElementType()))
215 TypeNames.insert(std::make_pair(Ty, getLLVMName(TI->first)));
Chris Lattnerb86620e2001-10-29 16:37:48 +0000216 }
217}
218
219
220
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000221static std::string calcTypeName(const Type *Ty,
222 std::vector<const Type *> &TypeStack,
223 std::map<const Type *, std::string> &TypeNames){
Chris Lattnerf14ead92003-10-30 00:22:33 +0000224 if (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty))
225 return Ty->getDescription(); // Base case
Chris Lattnerb86620e2001-10-29 16:37:48 +0000226
227 // Check to see if the type is named.
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000228 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000229 if (I != TypeNames.end()) return I->second;
230
Chris Lattnerf14ead92003-10-30 00:22:33 +0000231 if (isa<OpaqueType>(Ty))
232 return "opaque";
233
Chris Lattnerb86620e2001-10-29 16:37:48 +0000234 // Check to see if the Type is already on the stack...
235 unsigned Slot = 0, CurSize = TypeStack.size();
236 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
237
238 // This is another base case for the recursion. In this case, we know
239 // that we have looped back to a type that we have previously visited.
240 // Generate the appropriate upreference to handle this.
Chris Lattnerb86620e2001-10-29 16:37:48 +0000241 if (Slot < CurSize)
242 return "\\" + utostr(CurSize-Slot); // Here's the upreference
243
244 TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
245
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000246 std::string Result;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000247 switch (Ty->getPrimitiveID()) {
Chris Lattner91db5822002-03-29 03:44:36 +0000248 case Type::FunctionTyID: {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000249 const FunctionType *FTy = cast<FunctionType>(Ty);
Chris Lattnerd816b532002-04-13 20:53:41 +0000250 Result = calcTypeName(FTy->getReturnType(), TypeStack, TypeNames) + " (";
Chris Lattnerfa829be2004-02-09 04:14:01 +0000251 for (FunctionType::param_iterator I = FTy->param_begin(),
252 E = FTy->param_end(); I != E; ++I) {
253 if (I != FTy->param_begin())
Chris Lattnerb86620e2001-10-29 16:37:48 +0000254 Result += ", ";
255 Result += calcTypeName(*I, TypeStack, TypeNames);
256 }
Chris Lattnerd816b532002-04-13 20:53:41 +0000257 if (FTy->isVarArg()) {
Chris Lattnerfa829be2004-02-09 04:14:01 +0000258 if (FTy->getNumParams()) Result += ", ";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000259 Result += "...";
260 }
261 Result += ")";
262 break;
263 }
264 case Type::StructTyID: {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000265 const StructType *STy = cast<StructType>(Ty);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000266 Result = "{ ";
Chris Lattnerac6db752004-02-09 04:37:31 +0000267 for (StructType::element_iterator I = STy->element_begin(),
268 E = STy->element_end(); I != E; ++I) {
269 if (I != STy->element_begin())
Chris Lattnerb86620e2001-10-29 16:37:48 +0000270 Result += ", ";
271 Result += calcTypeName(*I, TypeStack, TypeNames);
272 }
273 Result += " }";
274 break;
275 }
276 case Type::PointerTyID:
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000277 Result = calcTypeName(cast<PointerType>(Ty)->getElementType(),
Chris Lattner189088e2002-04-12 18:21:53 +0000278 TypeStack, TypeNames) + "*";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000279 break;
280 case Type::ArrayTyID: {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000281 const ArrayType *ATy = cast<ArrayType>(Ty);
Chris Lattner8a939c92002-04-13 21:11:04 +0000282 Result = "[" + utostr(ATy->getNumElements()) + " x ";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000283 Result += calcTypeName(ATy->getElementType(), TypeStack, TypeNames) + "]";
284 break;
285 }
Chris Lattner15285ab2003-05-14 17:50:47 +0000286 case Type::OpaqueTyID:
287 Result = "opaque";
288 break;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000289 default:
Vikram S. Adveb952b542002-07-14 23:14:45 +0000290 Result = "<unrecognized-type>";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000291 }
292
293 TypeStack.pop_back(); // Remove self from stack...
294 return Result;
295}
296
297
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000298/// printTypeInt - The internal guts of printing out a type that has a
299/// potentially named portion.
300///
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000301static std::ostream &printTypeInt(std::ostream &Out, const Type *Ty,
302 std::map<const Type *, std::string> &TypeNames) {
Chris Lattnerb86620e2001-10-29 16:37:48 +0000303 // Primitive types always print out their description, regardless of whether
304 // they have been named or not.
305 //
Chris Lattner92d60532003-10-30 00:12:51 +0000306 if (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty))
307 return Out << Ty->getDescription();
Chris Lattnerb86620e2001-10-29 16:37:48 +0000308
309 // Check to see if the type is named.
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000310 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000311 if (I != TypeNames.end()) return Out << I->second;
312
313 // Otherwise we have a type that has not been named but is a derived type.
314 // Carefully recurse the type hierarchy to print out any contained symbolic
315 // names.
316 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000317 std::vector<const Type *> TypeStack;
318 std::string TypeName = calcTypeName(Ty, TypeStack, TypeNames);
Chris Lattner7f74a562002-01-20 22:54:45 +0000319 TypeNames.insert(std::make_pair(Ty, TypeName));//Cache type name for later use
Chris Lattnerb86620e2001-10-29 16:37:48 +0000320 return Out << TypeName;
321}
322
Chris Lattner34b95182001-10-31 04:33:19 +0000323
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000324/// WriteTypeSymbolic - This attempts to write the specified type as a symbolic
325/// type, iff there is an entry in the modules symbol table for the specified
326/// type or one of it's component types. This is slower than a simple x << Type
327///
Chris Lattner189d19f2003-11-21 20:23:48 +0000328std::ostream &llvm::WriteTypeSymbolic(std::ostream &Out, const Type *Ty,
329 const Module *M) {
Chris Lattnerb86620e2001-10-29 16:37:48 +0000330 Out << " ";
331
332 // If they want us to print out a type, attempt to make it symbolic if there
333 // is a symbol table in the module...
Chris Lattner98cf1f52002-11-20 18:36:02 +0000334 if (M) {
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000335 std::map<const Type *, std::string> TypeNames;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000336 fillTypeNameTable(M, TypeNames);
337
Chris Lattner72f866e2001-10-29 16:40:32 +0000338 return printTypeInt(Out, Ty, TypeNames);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000339 } else {
Chris Lattner72f866e2001-10-29 16:40:32 +0000340 return Out << Ty->getDescription();
Chris Lattnerb86620e2001-10-29 16:37:48 +0000341 }
342}
343
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000344static void WriteConstantInt(std::ostream &Out, const Constant *CV,
345 bool PrintName,
346 std::map<const Type *, std::string> &TypeTable,
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000347 SlotMachine *Machine) {
Chris Lattner1e194682002-04-18 18:53:13 +0000348 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
349 Out << (CB == ConstantBool::True ? "true" : "false");
350 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV)) {
351 Out << CI->getValue();
352 } else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV)) {
353 Out << CI->getValue();
354 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
355 // We would like to output the FP constant value in exponential notation,
356 // but we cannot do this if doing so will lose precision. Check here to
357 // make sure that we only output it in exponential format if we can parse
358 // the value back and get the same value.
359 //
360 std::string StrVal = ftostr(CFP->getValue());
361
362 // Check to make sure that the stringized number is not some string like
363 // "Inf" or NaN, that atof will accept, but the lexer will not. Check that
364 // the string matches the "[-+]?[0-9]" regex.
365 //
366 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
367 ((StrVal[0] == '-' || StrVal[0] == '+') &&
Brian Gaeke87b4f072003-06-17 23:55:35 +0000368 (StrVal[1] >= '0' && StrVal[1] <= '9')))
Chris Lattner1e194682002-04-18 18:53:13 +0000369 // Reparse stringized version!
370 if (atof(StrVal.c_str()) == CFP->getValue()) {
371 Out << StrVal; return;
372 }
373
374 // Otherwise we could not reparse it to exactly the same value, so we must
375 // output the string in hexadecimal format!
376 //
377 // Behave nicely in the face of C TBAA rules... see:
378 // http://www.nullstone.com/htmls/category/aliastyp.htm
379 //
380 double Val = CFP->getValue();
381 char *Ptr = (char*)&Val;
382 assert(sizeof(double) == sizeof(uint64_t) && sizeof(double) == 8 &&
383 "assuming that double is 64 bits!");
384 Out << "0x" << utohexstr(*(uint64_t*)Ptr);
385
Chris Lattner76b2ff42004-02-15 05:55:15 +0000386 } else if (isa<ConstantAggregateZero>(CV)) {
387 Out << "zeroinitializer";
Chris Lattner1e194682002-04-18 18:53:13 +0000388 } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
389 // As a special case, print the array as a string if it is an array of
390 // ubytes or an array of sbytes with positive values.
391 //
392 const Type *ETy = CA->getType()->getElementType();
393 bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
394
395 if (ETy == Type::SByteTy)
396 for (unsigned i = 0; i < CA->getNumOperands(); ++i)
397 if (cast<ConstantSInt>(CA->getOperand(i))->getValue() < 0) {
398 isString = false;
399 break;
400 }
401
402 if (isString) {
403 Out << "c\"";
404 for (unsigned i = 0; i < CA->getNumOperands(); ++i) {
Chris Lattner6077c312003-07-23 15:22:26 +0000405 unsigned char C = cast<ConstantInt>(CA->getOperand(i))->getRawValue();
Chris Lattner1e194682002-04-18 18:53:13 +0000406
Chris Lattnere5fd3862002-07-31 23:56:44 +0000407 if (isprint(C) && C != '"' && C != '\\') {
Chris Lattner1e194682002-04-18 18:53:13 +0000408 Out << C;
409 } else {
410 Out << '\\'
411 << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'))
412 << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
413 }
414 }
415 Out << "\"";
416
417 } else { // Cannot output in string format...
Chris Lattnerd84bb632002-04-16 21:36:08 +0000418 Out << "[";
419 if (CA->getNumOperands()) {
420 Out << " ";
Chris Lattner1e194682002-04-18 18:53:13 +0000421 printTypeInt(Out, ETy, TypeTable);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000422 WriteAsOperandInternal(Out, CA->getOperand(0),
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000423 PrintName, TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000424 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
425 Out << ", ";
Chris Lattner1e194682002-04-18 18:53:13 +0000426 printTypeInt(Out, ETy, TypeTable);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000427 WriteAsOperandInternal(Out, CA->getOperand(i), PrintName,
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000428 TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000429 }
430 }
431 Out << " ]";
432 }
433 } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
434 Out << "{";
435 if (CS->getNumOperands()) {
436 Out << " ";
437 printTypeInt(Out, CS->getOperand(0)->getType(), TypeTable);
438
439 WriteAsOperandInternal(Out, CS->getOperand(0),
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000440 PrintName, TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000441
442 for (unsigned i = 1; i < CS->getNumOperands(); i++) {
443 Out << ", ";
444 printTypeInt(Out, CS->getOperand(i)->getType(), TypeTable);
445
446 WriteAsOperandInternal(Out, CS->getOperand(i),
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000447 PrintName, TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000448 }
449 }
450
451 Out << " }";
452 } else if (isa<ConstantPointerNull>(CV)) {
453 Out << "null";
454
Chris Lattner113f4f42002-06-25 16:13:24 +0000455 } else if (const ConstantPointerRef *PR = dyn_cast<ConstantPointerRef>(CV)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000456 WriteAsOperandInternal(Out, PR->getValue(), true, TypeTable, Machine);
Vikram S. Adveb952b542002-07-14 23:14:45 +0000457
Chris Lattner3cd8c562002-07-30 18:54:25 +0000458 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Chris Lattnerb8257222003-03-06 23:23:32 +0000459 Out << CE->getOpcodeName() << " (";
Vikram S. Adveb952b542002-07-14 23:14:45 +0000460
461 for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
462 printTypeInt(Out, (*OI)->getType(), TypeTable);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000463 WriteAsOperandInternal(Out, *OI, PrintName, TypeTable, Machine);
Vikram S. Adveb952b542002-07-14 23:14:45 +0000464 if (OI+1 != CE->op_end())
Chris Lattner3cd8c562002-07-30 18:54:25 +0000465 Out << ", ";
Vikram S. Adveb952b542002-07-14 23:14:45 +0000466 }
467
Chris Lattnercfe8f532002-08-16 21:17:11 +0000468 if (CE->getOpcode() == Instruction::Cast) {
Chris Lattner83b396b2002-08-15 19:37:43 +0000469 Out << " to ";
470 printTypeInt(Out, CE->getType(), TypeTable);
471 }
Chris Lattnercfe8f532002-08-16 21:17:11 +0000472 Out << ")";
Chris Lattner83b396b2002-08-15 19:37:43 +0000473
Chris Lattnerd84bb632002-04-16 21:36:08 +0000474 } else {
Vikram S. Adveb952b542002-07-14 23:14:45 +0000475 Out << "<placeholder or erroneous Constant>";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000476 }
477}
478
479
Misha Brukmanc566ca362004-03-02 00:22:19 +0000480/// WriteAsOperand - Write the name of the specified value out to the specified
481/// ostream. This can be useful when you just want to print int %reg126, not
482/// the whole instruction that generated it.
483///
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000484static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
485 bool PrintName,
486 std::map<const Type*, std::string> &TypeTable,
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000487 SlotMachine *Machine) {
Chris Lattnerd84bb632002-04-16 21:36:08 +0000488 Out << " ";
489 if (PrintName && V->hasName()) {
Chris Lattner1c343042003-08-22 05:40:38 +0000490 Out << getLLVMName(V->getName());
Chris Lattnerd84bb632002-04-16 21:36:08 +0000491 } else {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000492 if (const Constant *CV = dyn_cast<Constant>(V)) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000493 WriteConstantInt(Out, CV, PrintName, TypeTable, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000494 } else {
495 int Slot;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000496 if (Machine) {
497 Slot = Machine->getSlot(V);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000498 } else {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000499 if (const Type *Ty = dyn_cast<Type>(V)) {
Chris Lattnerd84bb632002-04-16 21:36:08 +0000500 Out << Ty->getDescription();
501 return;
502 }
503
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000504 Machine = createSlotMachine(V);
505 if (Machine == 0) { Out << "BAD VALUE TYPE!"; return; }
Chris Lattnerd84bb632002-04-16 21:36:08 +0000506
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000507 Slot = Machine->getSlot(V);
508 delete Machine;
Chris Lattnerd84bb632002-04-16 21:36:08 +0000509 }
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000510 Out << "%" << Slot;
Chris Lattnerd84bb632002-04-16 21:36:08 +0000511 }
512 }
513}
514
515
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000516/// WriteAsOperand - Write the name of the specified value out to the specified
517/// ostream. This can be useful when you just want to print int %reg126, not
518/// the whole instruction that generated it.
519///
Chris Lattner189d19f2003-11-21 20:23:48 +0000520std::ostream &llvm::WriteAsOperand(std::ostream &Out, const Value *V,
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000521 bool PrintType, bool PrintName,
522 const Module *Context) {
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000523 std::map<const Type *, std::string> TypeNames;
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000524 if (Context == 0) Context = getModuleFromVal(V);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000525
Chris Lattner98cf1f52002-11-20 18:36:02 +0000526 if (Context)
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000527 fillTypeNameTable(Context, TypeNames);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000528
529 if (PrintType)
530 printTypeInt(Out, V->getType(), TypeNames);
531
Brian Gaekefda1f182003-11-16 23:08:27 +0000532 if (const Type *Ty = dyn_cast<Type> (V))
533 printTypeInt(Out, Ty, TypeNames);
534
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000535 WriteAsOperandInternal(Out, V, PrintName, TypeNames, 0);
Chris Lattner5e5abe32001-07-20 19:15:21 +0000536 return Out;
537}
538
Chris Lattner189d19f2003-11-21 20:23:48 +0000539namespace llvm {
Chris Lattner2e9fee42001-07-12 23:35:26 +0000540
Chris Lattnerfee714f2001-09-07 16:36:04 +0000541class AssemblyWriter {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000542 std::ostream *Out;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000543 SlotMachine &Machine;
Chris Lattner7bfee412001-10-29 16:05:51 +0000544 const Module *TheModule;
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000545 std::map<const Type *, std::string> TypeNames;
Chris Lattner8339f7d2003-10-30 23:41:03 +0000546 AssemblyAnnotationWriter *AnnotationWriter;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000547public:
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000548 inline AssemblyWriter(std::ostream &o, SlotMachine &Mac, const Module *M,
Chris Lattner8339f7d2003-10-30 23:41:03 +0000549 AssemblyAnnotationWriter *AAW)
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000550 : Out(&o), Machine(Mac), TheModule(M), AnnotationWriter(AAW) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000551
552 // If the module has a symbol table, take all global types and stuff their
553 // names into the TypeNames map.
554 //
Chris Lattnerb86620e2001-10-29 16:37:48 +0000555 fillTypeNameTable(M, TypeNames);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000556 }
557
Chris Lattner7bfee412001-10-29 16:05:51 +0000558 inline void write(const Module *M) { printModule(M); }
559 inline void write(const GlobalVariable *G) { printGlobal(G); }
Chris Lattner57698e22002-03-26 18:01:55 +0000560 inline void write(const Function *F) { printFunction(F); }
Chris Lattner7bfee412001-10-29 16:05:51 +0000561 inline void write(const BasicBlock *BB) { printBasicBlock(BB); }
Chris Lattner113f4f42002-06-25 16:13:24 +0000562 inline void write(const Instruction *I) { printInstruction(*I); }
Chris Lattner3462ae32001-12-03 22:26:30 +0000563 inline void write(const Constant *CPV) { printConstant(CPV); }
Chris Lattner7db79582001-11-07 04:21:57 +0000564 inline void write(const Type *Ty) { printType(Ty); }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000565
Chris Lattner1e194682002-04-18 18:53:13 +0000566 void writeOperand(const Value *Op, bool PrintType, bool PrintName = true);
567
Misha Brukman4685e262004-04-28 15:31:21 +0000568 const Module* getModule() { return TheModule; }
Misha Brukmanda546ea2004-04-28 19:24:28 +0000569 void setStream(std::ostream &os) { Out = &os; }
Misha Brukman4685e262004-04-28 15:31:21 +0000570
Chris Lattner2f7c9632001-06-06 20:29:01 +0000571private :
Chris Lattner7bfee412001-10-29 16:05:51 +0000572 void printModule(const Module *M);
573 void printSymbolTable(const SymbolTable &ST);
Chris Lattner3462ae32001-12-03 22:26:30 +0000574 void printConstant(const Constant *CPV);
Chris Lattner7bfee412001-10-29 16:05:51 +0000575 void printGlobal(const GlobalVariable *GV);
Chris Lattner57698e22002-03-26 18:01:55 +0000576 void printFunction(const Function *F);
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000577 void printArgument(const Argument *FA);
Chris Lattner7bfee412001-10-29 16:05:51 +0000578 void printBasicBlock(const BasicBlock *BB);
Chris Lattner113f4f42002-06-25 16:13:24 +0000579 void printInstruction(const Instruction &I);
Chris Lattnerd816b532002-04-13 20:53:41 +0000580
581 // printType - Go to extreme measures to attempt to print out a short,
582 // symbolic version of a type name.
583 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000584 std::ostream &printType(const Type *Ty) {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000585 return printTypeInt(*Out, Ty, TypeNames);
Chris Lattnerd816b532002-04-13 20:53:41 +0000586 }
587
588 // printTypeAtLeastOneLevel - Print out one level of the possibly complex type
589 // without considering any symbolic types that we may have equal to it.
590 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000591 std::ostream &printTypeAtLeastOneLevel(const Type *Ty);
Chris Lattner7bfee412001-10-29 16:05:51 +0000592
Chris Lattner862e3382001-10-13 06:42:36 +0000593 // printInfoComment - Print a little comment after the instruction indicating
594 // which slot it occupies.
Chris Lattner113f4f42002-06-25 16:13:24 +0000595 void printInfoComment(const Value &V);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000596};
Chris Lattner189d19f2003-11-21 20:23:48 +0000597} // end of anonymous namespace
Chris Lattner2f7c9632001-06-06 20:29:01 +0000598
Misha Brukmanc566ca362004-03-02 00:22:19 +0000599/// printTypeAtLeastOneLevel - Print out one level of the possibly complex type
600/// without considering any symbolic types that we may have equal to it.
601///
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000602std::ostream &AssemblyWriter::printTypeAtLeastOneLevel(const Type *Ty) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000603 if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000604 printType(FTy->getReturnType()) << " (";
Chris Lattnerfa829be2004-02-09 04:14:01 +0000605 for (FunctionType::param_iterator I = FTy->param_begin(),
606 E = FTy->param_end(); I != E; ++I) {
607 if (I != FTy->param_begin())
Misha Brukmanda546ea2004-04-28 19:24:28 +0000608 *Out << ", ";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000609 printType(*I);
Chris Lattnerd816b532002-04-13 20:53:41 +0000610 }
611 if (FTy->isVarArg()) {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000612 if (FTy->getNumParams()) *Out << ", ";
613 *Out << "...";
Chris Lattnerd816b532002-04-13 20:53:41 +0000614 }
Misha Brukmanda546ea2004-04-28 19:24:28 +0000615 *Out << ")";
Chris Lattner113f4f42002-06-25 16:13:24 +0000616 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000617 *Out << "{ ";
Chris Lattnerac6db752004-02-09 04:37:31 +0000618 for (StructType::element_iterator I = STy->element_begin(),
619 E = STy->element_end(); I != E; ++I) {
620 if (I != STy->element_begin())
Misha Brukmanda546ea2004-04-28 19:24:28 +0000621 *Out << ", ";
Chris Lattnerd816b532002-04-13 20:53:41 +0000622 printType(*I);
623 }
Misha Brukmanda546ea2004-04-28 19:24:28 +0000624 *Out << " }";
Chris Lattner113f4f42002-06-25 16:13:24 +0000625 } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000626 printType(PTy->getElementType()) << "*";
Chris Lattner113f4f42002-06-25 16:13:24 +0000627 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000628 *Out << "[" << ATy->getNumElements() << " x ";
Chris Lattnerd816b532002-04-13 20:53:41 +0000629 printType(ATy->getElementType()) << "]";
Chris Lattner113f4f42002-06-25 16:13:24 +0000630 } else if (const OpaqueType *OTy = dyn_cast<OpaqueType>(Ty)) {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000631 *Out << "opaque";
Chris Lattnerd816b532002-04-13 20:53:41 +0000632 } else {
Vikram S. Adveb952b542002-07-14 23:14:45 +0000633 if (!Ty->isPrimitiveType())
Misha Brukmanda546ea2004-04-28 19:24:28 +0000634 *Out << "<unknown derived type>";
Chris Lattnerd816b532002-04-13 20:53:41 +0000635 printType(Ty);
636 }
Misha Brukmanda546ea2004-04-28 19:24:28 +0000637 return *Out;
Chris Lattnerd816b532002-04-13 20:53:41 +0000638}
639
640
Chris Lattnerfee714f2001-09-07 16:36:04 +0000641void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType,
642 bool PrintName) {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000643 if (PrintType) { *Out << " "; printType(Operand->getType()); }
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000644 WriteAsOperandInternal(*Out, Operand, PrintName, TypeNames, &Machine);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000645}
646
Chris Lattner2f7c9632001-06-06 20:29:01 +0000647
Chris Lattner7bfee412001-10-29 16:05:51 +0000648void AssemblyWriter::printModule(const Module *M) {
Chris Lattner8068e0c2003-08-24 13:48:48 +0000649 switch (M->getEndianness()) {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000650 case Module::LittleEndian: *Out << "target endian = little\n"; break;
651 case Module::BigEndian: *Out << "target endian = big\n"; break;
Chris Lattner8068e0c2003-08-24 13:48:48 +0000652 case Module::AnyEndianness: break;
653 }
654 switch (M->getPointerSize()) {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000655 case Module::Pointer32: *Out << "target pointersize = 32\n"; break;
656 case Module::Pointer64: *Out << "target pointersize = 64\n"; break;
Chris Lattner8068e0c2003-08-24 13:48:48 +0000657 case Module::AnyPointerSize: break;
658 }
659
Chris Lattnerfee714f2001-09-07 16:36:04 +0000660 // Loop over the symbol table, emitting all named constants...
Chris Lattner98cf1f52002-11-20 18:36:02 +0000661 printSymbolTable(M->getSymbolTable());
Chris Lattnerda975502001-09-10 07:58:01 +0000662
Chris Lattner113f4f42002-06-25 16:13:24 +0000663 for (Module::const_giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
664 printGlobal(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000665
Misha Brukmanda546ea2004-04-28 19:24:28 +0000666 *Out << "\nimplementation ; Functions:\n";
Vikram S. Adve13ba19a2001-09-18 12:48:16 +0000667
Chris Lattner6915f8f2002-04-07 22:49:37 +0000668 // Output all of the functions...
Chris Lattner113f4f42002-06-25 16:13:24 +0000669 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
670 printFunction(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000671}
672
Chris Lattner7bfee412001-10-29 16:05:51 +0000673void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000674 if (GV->hasName()) *Out << getLLVMName(GV->getName()) << " = ";
Chris Lattner37798642001-09-18 04:01:05 +0000675
Chris Lattner379a8d22003-04-16 20:28:45 +0000676 if (!GV->hasInitializer())
Misha Brukmanda546ea2004-04-28 19:24:28 +0000677 *Out << "external ";
Chris Lattner379a8d22003-04-16 20:28:45 +0000678 else
679 switch (GV->getLinkage()) {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000680 case GlobalValue::InternalLinkage: *Out << "internal "; break;
681 case GlobalValue::LinkOnceLinkage: *Out << "linkonce "; break;
682 case GlobalValue::WeakLinkage: *Out << "weak "; break;
683 case GlobalValue::AppendingLinkage: *Out << "appending "; break;
Chris Lattner379a8d22003-04-16 20:28:45 +0000684 case GlobalValue::ExternalLinkage: break;
685 }
Chris Lattner37798642001-09-18 04:01:05 +0000686
Misha Brukmanda546ea2004-04-28 19:24:28 +0000687 *Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattner2413b162001-12-04 00:03:30 +0000688 printType(GV->getType()->getElementType());
Chris Lattner37798642001-09-18 04:01:05 +0000689
690 if (GV->hasInitializer())
691 writeOperand(GV->getInitializer(), false, false);
692
Chris Lattner113f4f42002-06-25 16:13:24 +0000693 printInfoComment(*GV);
Misha Brukmanda546ea2004-04-28 19:24:28 +0000694 *Out << "\n";
Chris Lattnerda975502001-09-10 07:58:01 +0000695}
696
Chris Lattnerfee714f2001-09-07 16:36:04 +0000697
Reid Spencere7e96712004-05-25 08:53:40 +0000698// printSymbolTable - Run through symbol table looking for constants
699// and types. Emit their declarations.
Chris Lattner7bfee412001-10-29 16:05:51 +0000700void AssemblyWriter::printSymbolTable(const SymbolTable &ST) {
Reid Spencere7e96712004-05-25 08:53:40 +0000701
702 // Print the types.
703 for (SymbolTable::type_const_iterator TI = ST.type_begin();
704 TI != ST.type_end(); ++TI ) {
705 *Out << "\t" << getLLVMName(TI->first) << " = type ";
706
707 // Make sure we print out at least one level of the type structure, so
708 // that we do not get %FILE = type %FILE
709 //
710 printTypeAtLeastOneLevel(TI->second) << "\n";
711 }
Chris Lattnerfee714f2001-09-07 16:36:04 +0000712
Reid Spencere7e96712004-05-25 08:53:40 +0000713 // Print the constants, in type plane order.
714 for (SymbolTable::plane_const_iterator PI = ST.plane_begin();
715 PI != ST.plane_end(); ++PI ) {
716 SymbolTable::value_const_iterator VI = ST.value_begin(PI->first);
717 SymbolTable::value_const_iterator VE = ST.value_end(PI->first);
718
719 for (; VI != VE; ++VI) {
720 const Value *V = VI->second;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000721 if (const Constant *CPV = dyn_cast<Constant>(V)) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000722 printConstant(CPV);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000723 }
724 }
Chris Lattnera7620d92001-07-15 06:35:59 +0000725 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000726}
727
728
Misha Brukmanc566ca362004-03-02 00:22:19 +0000729/// printConstant - Print out a constant pool entry...
730///
Chris Lattner3462ae32001-12-03 22:26:30 +0000731void AssemblyWriter::printConstant(const Constant *CPV) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000732 // Don't print out unnamed constants, they will be inlined
733 if (!CPV->hasName()) return;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000734
Chris Lattneree998be2001-07-26 16:29:38 +0000735 // Print out name...
Misha Brukmanda546ea2004-04-28 19:24:28 +0000736 *Out << "\t" << getLLVMName(CPV->getName()) << " =";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000737
738 // Write the value out now...
Chris Lattnerd84bb632002-04-16 21:36:08 +0000739 writeOperand(CPV, true, false);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000740
Chris Lattner113f4f42002-06-25 16:13:24 +0000741 printInfoComment(*CPV);
Misha Brukmanda546ea2004-04-28 19:24:28 +0000742 *Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000743}
744
Misha Brukmanc566ca362004-03-02 00:22:19 +0000745/// printFunction - Print all aspects of a function.
746///
Chris Lattner113f4f42002-06-25 16:13:24 +0000747void AssemblyWriter::printFunction(const Function *F) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000748 // Print out the return type and name...
Misha Brukmanda546ea2004-04-28 19:24:28 +0000749 *Out << "\n";
Chris Lattner379a8d22003-04-16 20:28:45 +0000750
Misha Brukmanda546ea2004-04-28 19:24:28 +0000751 if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, *Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +0000752
Chris Lattner379a8d22003-04-16 20:28:45 +0000753 if (F->isExternal())
Misha Brukmanda546ea2004-04-28 19:24:28 +0000754 *Out << "declare ";
Chris Lattner379a8d22003-04-16 20:28:45 +0000755 else
756 switch (F->getLinkage()) {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000757 case GlobalValue::InternalLinkage: *Out << "internal "; break;
758 case GlobalValue::LinkOnceLinkage: *Out << "linkonce "; break;
759 case GlobalValue::WeakLinkage: *Out << "weak "; break;
760 case GlobalValue::AppendingLinkage: *Out << "appending "; break;
Chris Lattner379a8d22003-04-16 20:28:45 +0000761 case GlobalValue::ExternalLinkage: break;
762 }
763
Chris Lattner13651f02003-09-03 17:56:43 +0000764 printType(F->getReturnType()) << " ";
Chris Lattner5b337482003-10-18 05:57:43 +0000765 if (!F->getName().empty())
Misha Brukmanda546ea2004-04-28 19:24:28 +0000766 *Out << getLLVMName(F->getName());
Chris Lattner5b337482003-10-18 05:57:43 +0000767 else
Misha Brukmanda546ea2004-04-28 19:24:28 +0000768 *Out << "\"\"";
769 *Out << "(";
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000770 Machine.incorporateFunction(F);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000771
Chris Lattner7bfee412001-10-29 16:05:51 +0000772 // Loop over the arguments, printing them...
Chris Lattner113f4f42002-06-25 16:13:24 +0000773 const FunctionType *FT = F->getFunctionType();
Chris Lattnerfee714f2001-09-07 16:36:04 +0000774
Chris Lattner149376d2002-10-13 20:57:00 +0000775 for(Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I)
776 printArgument(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000777
778 // Finish printing arguments...
Chris Lattner113f4f42002-06-25 16:13:24 +0000779 if (FT->isVarArg()) {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000780 if (FT->getNumParams()) *Out << ", ";
781 *Out << "..."; // Output varargs portion of signature!
Chris Lattnerfee714f2001-09-07 16:36:04 +0000782 }
Misha Brukmanda546ea2004-04-28 19:24:28 +0000783 *Out << ")";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000784
Chris Lattner113f4f42002-06-25 16:13:24 +0000785 if (F->isExternal()) {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000786 *Out << "\n";
Chris Lattnerb2f02e52002-05-06 03:00:40 +0000787 } else {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000788 *Out << " {";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000789
Chris Lattner6915f8f2002-04-07 22:49:37 +0000790 // Output all of its basic blocks... for the function
Chris Lattner113f4f42002-06-25 16:13:24 +0000791 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
792 printBasicBlock(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000793
Misha Brukmanda546ea2004-04-28 19:24:28 +0000794 *Out << "}\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000795 }
796
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000797 Machine.purgeFunction();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000798}
799
Misha Brukmanc566ca362004-03-02 00:22:19 +0000800/// printArgument - This member is called for every argument that is passed into
801/// the function. Simply print it out
802///
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000803void AssemblyWriter::printArgument(const Argument *Arg) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000804 // Insert commas as we go... the first arg doesn't get a comma
Misha Brukmanda546ea2004-04-28 19:24:28 +0000805 if (Arg != &Arg->getParent()->afront()) *Out << ", ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000806
807 // Output type...
Chris Lattner7bfee412001-10-29 16:05:51 +0000808 printType(Arg->getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000809
810 // Output name, if available...
811 if (Arg->hasName())
Misha Brukmanda546ea2004-04-28 19:24:28 +0000812 *Out << " " << getLLVMName(Arg->getName());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000813}
814
Misha Brukmanc566ca362004-03-02 00:22:19 +0000815/// printBasicBlock - This member is called for each basic block in a method.
816///
Chris Lattner7bfee412001-10-29 16:05:51 +0000817void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000818 if (BB->hasName()) { // Print out the label if it exists...
Misha Brukmanda546ea2004-04-28 19:24:28 +0000819 *Out << "\n" << BB->getName() << ":";
Chris Lattner408dbdb2002-05-14 16:02:05 +0000820 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000821 *Out << "\n; <label>:" << Machine.getSlot(BB);
Chris Lattner58185f22002-10-02 19:38:55 +0000822 }
Chris Lattner2447ef52003-11-20 00:09:43 +0000823
824 if (BB->getParent() == 0)
Misha Brukmanda546ea2004-04-28 19:24:28 +0000825 *Out << "\t\t; Error: Block without parent!";
Chris Lattner2447ef52003-11-20 00:09:43 +0000826 else {
827 if (BB != &BB->getParent()->front()) { // Not the entry block?
828 // Output predecessors for the block...
Misha Brukmanda546ea2004-04-28 19:24:28 +0000829 *Out << "\t\t;";
Chris Lattner2447ef52003-11-20 00:09:43 +0000830 pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
831
832 if (PI == PE) {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000833 *Out << " No predecessors!";
Chris Lattner2447ef52003-11-20 00:09:43 +0000834 } else {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000835 *Out << " preds =";
Chris Lattner00211f12003-11-16 22:59:57 +0000836 writeOperand(*PI, false, true);
Chris Lattner2447ef52003-11-20 00:09:43 +0000837 for (++PI; PI != PE; ++PI) {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000838 *Out << ",";
Chris Lattner2447ef52003-11-20 00:09:43 +0000839 writeOperand(*PI, false, true);
840 }
Chris Lattner00211f12003-11-16 22:59:57 +0000841 }
Chris Lattner58185f22002-10-02 19:38:55 +0000842 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000843 }
Chris Lattner408dbdb2002-05-14 16:02:05 +0000844
Misha Brukmanda546ea2004-04-28 19:24:28 +0000845 *Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000846
Misha Brukmanda546ea2004-04-28 19:24:28 +0000847 if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, *Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +0000848
Chris Lattnerfee714f2001-09-07 16:36:04 +0000849 // Output all of the instructions in the basic block...
Chris Lattner113f4f42002-06-25 16:13:24 +0000850 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
851 printInstruction(*I);
Chris Lattner96cdd272004-03-08 18:51:45 +0000852
Misha Brukmanda546ea2004-04-28 19:24:28 +0000853 if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, *Out);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000854}
855
Chris Lattner862e3382001-10-13 06:42:36 +0000856
Misha Brukmanc566ca362004-03-02 00:22:19 +0000857/// printInfoComment - Print a little comment after the instruction indicating
858/// which slot it occupies.
859///
Chris Lattner113f4f42002-06-25 16:13:24 +0000860void AssemblyWriter::printInfoComment(const Value &V) {
861 if (V.getType() != Type::VoidTy) {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000862 *Out << "\t\t; <";
Chris Lattner113f4f42002-06-25 16:13:24 +0000863 printType(V.getType()) << ">";
Chris Lattner862e3382001-10-13 06:42:36 +0000864
Chris Lattner113f4f42002-06-25 16:13:24 +0000865 if (!V.hasName()) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000866 *Out << ":" << Machine.getSlot(&V); // Print out the def slot taken.
Chris Lattner862e3382001-10-13 06:42:36 +0000867 }
Misha Brukmanda546ea2004-04-28 19:24:28 +0000868 *Out << " [#uses=" << V.use_size() << "]"; // Output # uses
Chris Lattner862e3382001-10-13 06:42:36 +0000869 }
870}
871
Misha Brukmanc566ca362004-03-02 00:22:19 +0000872/// printInstruction - This member is called for each Instruction in a method.
873///
Chris Lattner113f4f42002-06-25 16:13:24 +0000874void AssemblyWriter::printInstruction(const Instruction &I) {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000875 if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, *Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +0000876
Misha Brukmanda546ea2004-04-28 19:24:28 +0000877 *Out << "\t";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000878
879 // Print out name if it exists...
Chris Lattner113f4f42002-06-25 16:13:24 +0000880 if (I.hasName())
Misha Brukmanda546ea2004-04-28 19:24:28 +0000881 *Out << getLLVMName(I.getName()) << " = ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000882
Chris Lattner504f9242003-09-08 17:45:59 +0000883 // If this is a volatile load or store, print out the volatile marker
884 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
885 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile()))
Misha Brukmanda546ea2004-04-28 19:24:28 +0000886 *Out << "volatile ";
Chris Lattner504f9242003-09-08 17:45:59 +0000887
Chris Lattner2f7c9632001-06-06 20:29:01 +0000888 // Print out the opcode...
Misha Brukmanda546ea2004-04-28 19:24:28 +0000889 *Out << I.getOpcodeName();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000890
891 // Print out the type of the operands...
Chris Lattner113f4f42002-06-25 16:13:24 +0000892 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000893
894 // Special case conditional branches to swizzle the condition out to the front
Chris Lattner113f4f42002-06-25 16:13:24 +0000895 if (isa<BranchInst>(I) && I.getNumOperands() > 1) {
896 writeOperand(I.getOperand(2), true);
Misha Brukmanda546ea2004-04-28 19:24:28 +0000897 *Out << ",";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000898 writeOperand(Operand, true);
Misha Brukmanda546ea2004-04-28 19:24:28 +0000899 *Out << ",";
Chris Lattner113f4f42002-06-25 16:13:24 +0000900 writeOperand(I.getOperand(1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000901
Chris Lattner8d48df22002-04-13 18:34:38 +0000902 } else if (isa<SwitchInst>(I)) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000903 // Special case switch statement to get formatting nice and correct...
Misha Brukmanda546ea2004-04-28 19:24:28 +0000904 writeOperand(Operand , true); *Out << ",";
905 writeOperand(I.getOperand(1), true); *Out << " [";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000906
Chris Lattner113f4f42002-06-25 16:13:24 +0000907 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000908 *Out << "\n\t\t";
909 writeOperand(I.getOperand(op ), true); *Out << ",";
Chris Lattner113f4f42002-06-25 16:13:24 +0000910 writeOperand(I.getOperand(op+1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000911 }
Misha Brukmanda546ea2004-04-28 19:24:28 +0000912 *Out << "\n\t]";
Chris Lattnerda558102001-10-02 03:41:24 +0000913 } else if (isa<PHINode>(I)) {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000914 *Out << " ";
Chris Lattner113f4f42002-06-25 16:13:24 +0000915 printType(I.getType());
Misha Brukmanda546ea2004-04-28 19:24:28 +0000916 *Out << " ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000917
Chris Lattner113f4f42002-06-25 16:13:24 +0000918 for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000919 if (op) *Out << ", ";
920 *Out << "[";
921 writeOperand(I.getOperand(op ), false); *Out << ",";
922 writeOperand(I.getOperand(op+1), false); *Out << " ]";
Chris Lattner931ef3b2001-06-11 15:04:20 +0000923 }
Chris Lattner862e3382001-10-13 06:42:36 +0000924 } else if (isa<ReturnInst>(I) && !Operand) {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000925 *Out << " void";
Chris Lattner862e3382001-10-13 06:42:36 +0000926 } else if (isa<CallInst>(I)) {
Chris Lattner463d6a52003-08-05 15:34:45 +0000927 const PointerType *PTy = cast<PointerType>(Operand->getType());
928 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
929 const Type *RetTy = FTy->getReturnType();
Chris Lattner2f2d9472001-11-06 21:28:12 +0000930
Chris Lattner463d6a52003-08-05 15:34:45 +0000931 // If possible, print out the short form of the call instruction. We can
Chris Lattner6915f8f2002-04-07 22:49:37 +0000932 // only do this if the first argument is a pointer to a nonvararg function,
Chris Lattner463d6a52003-08-05 15:34:45 +0000933 // and if the return type is not a pointer to a function.
Chris Lattner2f2d9472001-11-06 21:28:12 +0000934 //
Chris Lattner463d6a52003-08-05 15:34:45 +0000935 if (!FTy->isVarArg() &&
Chris Lattner8d48df22002-04-13 18:34:38 +0000936 (!isa<PointerType>(RetTy) ||
Chris Lattnerd9a36a62002-07-25 20:58:51 +0000937 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000938 *Out << " "; printType(RetTy);
Chris Lattner2f2d9472001-11-06 21:28:12 +0000939 writeOperand(Operand, false);
940 } else {
941 writeOperand(Operand, true);
942 }
Misha Brukmanda546ea2004-04-28 19:24:28 +0000943 *Out << "(";
Chris Lattner113f4f42002-06-25 16:13:24 +0000944 if (I.getNumOperands() > 1) writeOperand(I.getOperand(1), true);
945 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; ++op) {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000946 *Out << ",";
Chris Lattner113f4f42002-06-25 16:13:24 +0000947 writeOperand(I.getOperand(op), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000948 }
949
Misha Brukmanda546ea2004-04-28 19:24:28 +0000950 *Out << " )";
Chris Lattner113f4f42002-06-25 16:13:24 +0000951 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Chris Lattner463d6a52003-08-05 15:34:45 +0000952 const PointerType *PTy = cast<PointerType>(Operand->getType());
953 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
954 const Type *RetTy = FTy->getReturnType();
955
956 // If possible, print out the short form of the invoke instruction. We can
957 // only do this if the first argument is a pointer to a nonvararg function,
958 // and if the return type is not a pointer to a function.
959 //
960 if (!FTy->isVarArg() &&
961 (!isa<PointerType>(RetTy) ||
962 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000963 *Out << " "; printType(RetTy);
Chris Lattner463d6a52003-08-05 15:34:45 +0000964 writeOperand(Operand, false);
965 } else {
966 writeOperand(Operand, true);
967 }
968
Misha Brukmanda546ea2004-04-28 19:24:28 +0000969 *Out << "(";
Chris Lattner113f4f42002-06-25 16:13:24 +0000970 if (I.getNumOperands() > 3) writeOperand(I.getOperand(3), true);
971 for (unsigned op = 4, Eop = I.getNumOperands(); op < Eop; ++op) {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000972 *Out << ",";
Chris Lattner113f4f42002-06-25 16:13:24 +0000973 writeOperand(I.getOperand(op), true);
Chris Lattner862e3382001-10-13 06:42:36 +0000974 }
975
Misha Brukmanda546ea2004-04-28 19:24:28 +0000976 *Out << " )\n\t\t\tto";
Chris Lattner862e3382001-10-13 06:42:36 +0000977 writeOperand(II->getNormalDest(), true);
Misha Brukmanda546ea2004-04-28 19:24:28 +0000978 *Out << " unwind";
Chris Lattnerfae8ab32004-02-08 21:44:31 +0000979 writeOperand(II->getUnwindDest(), true);
Chris Lattner862e3382001-10-13 06:42:36 +0000980
Chris Lattner113f4f42002-06-25 16:13:24 +0000981 } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(&I)) {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000982 *Out << " ";
Chris Lattner8d48df22002-04-13 18:34:38 +0000983 printType(AI->getType()->getElementType());
984 if (AI->isArrayAllocation()) {
Misha Brukmanda546ea2004-04-28 19:24:28 +0000985 *Out << ",";
Chris Lattner8d48df22002-04-13 18:34:38 +0000986 writeOperand(AI->getArraySize(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000987 }
Chris Lattner862e3382001-10-13 06:42:36 +0000988 } else if (isa<CastInst>(I)) {
Chris Lattnerc96e96b2003-11-17 01:17:04 +0000989 if (Operand) writeOperand(Operand, true); // Work with broken code
Misha Brukmanda546ea2004-04-28 19:24:28 +0000990 *Out << " to ";
Chris Lattner113f4f42002-06-25 16:13:24 +0000991 printType(I.getType());
Chris Lattner5b337482003-10-18 05:57:43 +0000992 } else if (isa<VAArgInst>(I)) {
Chris Lattnerc96e96b2003-11-17 01:17:04 +0000993 if (Operand) writeOperand(Operand, true); // Work with broken code
Misha Brukmanda546ea2004-04-28 19:24:28 +0000994 *Out << ", ";
Chris Lattnerf70da102003-05-08 02:44:12 +0000995 printType(I.getType());
Chris Lattner5b337482003-10-18 05:57:43 +0000996 } else if (const VANextInst *VAN = dyn_cast<VANextInst>(&I)) {
Chris Lattnerc96e96b2003-11-17 01:17:04 +0000997 if (Operand) writeOperand(Operand, true); // Work with broken code
Misha Brukmanda546ea2004-04-28 19:24:28 +0000998 *Out << ", ";
Chris Lattner5b337482003-10-18 05:57:43 +0000999 printType(VAN->getArgType());
Chris Lattner2f7c9632001-06-06 20:29:01 +00001000 } else if (Operand) { // Print the normal way...
1001
1002 // PrintAllTypes - Instructions who have operands of all the same type
1003 // omit the type from all but the first operand. If the instruction has
1004 // different type operands (for example br), then they are all printed.
1005 bool PrintAllTypes = false;
1006 const Type *TheType = Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001007
Chris Lattner52bd5cb92004-03-12 05:53:14 +00001008 // Shift Left & Right print both types even for Ubyte LHS, and select prints
1009 // types even if all operands are bools.
1010 if (isa<ShiftInst>(I) || isa<SelectInst>(I)) {
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00001011 PrintAllTypes = true;
1012 } else {
1013 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
1014 Operand = I.getOperand(i);
1015 if (Operand->getType() != TheType) {
1016 PrintAllTypes = true; // We have differing types! Print them all!
1017 break;
1018 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001019 }
1020 }
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00001021
Chris Lattner7bfee412001-10-29 16:05:51 +00001022 if (!PrintAllTypes) {
Misha Brukmanda546ea2004-04-28 19:24:28 +00001023 *Out << " ";
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00001024 printType(TheType);
Chris Lattner7bfee412001-10-29 16:05:51 +00001025 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001026
Chris Lattner113f4f42002-06-25 16:13:24 +00001027 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Misha Brukmanda546ea2004-04-28 19:24:28 +00001028 if (i) *Out << ",";
Chris Lattner113f4f42002-06-25 16:13:24 +00001029 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001030 }
1031 }
1032
Chris Lattner862e3382001-10-13 06:42:36 +00001033 printInfoComment(I);
Misha Brukmanda546ea2004-04-28 19:24:28 +00001034 *Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001035}
1036
1037
1038//===----------------------------------------------------------------------===//
1039// External Interface declarations
1040//===----------------------------------------------------------------------===//
1041
Chris Lattner8339f7d2003-10-30 23:41:03 +00001042void Module::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001043 SlotMachine SlotTable(this);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001044 AssemblyWriter W(o, SlotTable, this, AAW);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001045 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001046}
1047
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001048void GlobalVariable::print(std::ostream &o) const {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001049 SlotMachine SlotTable(getParent());
Chris Lattner8339f7d2003-10-30 23:41:03 +00001050 AssemblyWriter W(o, SlotTable, getParent(), 0);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001051 W.write(this);
Chris Lattneradfe0d12001-09-10 20:08:19 +00001052}
1053
Chris Lattner8339f7d2003-10-30 23:41:03 +00001054void Function::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001055 SlotMachine SlotTable(getParent());
Chris Lattner8339f7d2003-10-30 23:41:03 +00001056 AssemblyWriter W(o, SlotTable, getParent(), AAW);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001057
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001058 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001059}
1060
Chris Lattner8339f7d2003-10-30 23:41:03 +00001061void BasicBlock::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001062 SlotMachine SlotTable(getParent());
Chris Lattner7bfee412001-10-29 16:05:51 +00001063 AssemblyWriter W(o, SlotTable,
Chris Lattner8339f7d2003-10-30 23:41:03 +00001064 getParent() ? getParent()->getParent() : 0, AAW);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001065 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001066}
1067
Chris Lattner8339f7d2003-10-30 23:41:03 +00001068void Instruction::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001069 const Function *F = getParent() ? getParent()->getParent() : 0;
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001070 SlotMachine SlotTable(F);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001071 AssemblyWriter W(o, SlotTable, F ? F->getParent() : 0, AAW);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001072
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001073 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001074}
Chris Lattner7db79582001-11-07 04:21:57 +00001075
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001076void Constant::print(std::ostream &o) const {
1077 if (this == 0) { o << "<null> constant value\n"; return; }
Chris Lattner7d734802002-09-10 15:53:49 +00001078
1079 // Handle CPR's special, because they have context information...
1080 if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(this)) {
1081 CPR->getValue()->print(o); // Print as a global value, with context info.
1082 return;
1083 }
1084
Chris Lattner1e194682002-04-18 18:53:13 +00001085 o << " " << getType()->getDescription() << " ";
1086
Chris Lattnerab7d1ab2003-05-08 02:08:14 +00001087 std::map<const Type *, std::string> TypeTable;
Chris Lattner1e194682002-04-18 18:53:13 +00001088 WriteConstantInt(o, this, false, TypeTable, 0);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001089}
1090
1091void Type::print(std::ostream &o) const {
1092 if (this == 0)
1093 o << "<null Type>";
1094 else
1095 o << getDescription();
1096}
1097
Chris Lattner2e9fa6d2002-04-09 19:48:49 +00001098void Argument::print(std::ostream &o) const {
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001099 o << getType() << " " << getName();
1100}
1101
Reid Spencer52641832004-05-25 18:14:38 +00001102// Value::dump - allow easy printing of Values from the debugger.
1103// Located here because so much of the needed functionality is here.
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001104void Value::dump() const { print(std::cerr); }
Reid Spencer52641832004-05-25 18:14:38 +00001105
1106// Type::dump - allow easy printing of Values from the debugger.
1107// Located here because so much of the needed functionality is here.
Reid Spencere7e96712004-05-25 08:53:40 +00001108void Type::dump() const { print(std::cerr); }
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001109
1110//===----------------------------------------------------------------------===//
1111// CachedWriter Class Implementation
1112//===----------------------------------------------------------------------===//
1113
Chris Lattner7db79582001-11-07 04:21:57 +00001114void CachedWriter::setModule(const Module *M) {
1115 delete SC; delete AW;
1116 if (M) {
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001117 SC = new SlotMachine(M );
Misha Brukman4685e262004-04-28 15:31:21 +00001118 AW = new AssemblyWriter(*Out, *SC, M, 0);
Chris Lattner7db79582001-11-07 04:21:57 +00001119 } else {
1120 SC = 0; AW = 0;
1121 }
1122}
1123
1124CachedWriter::~CachedWriter() {
1125 delete AW;
1126 delete SC;
1127}
1128
1129CachedWriter &CachedWriter::operator<<(const Value *V) {
1130 assert(AW && SC && "CachedWriter does not have a current module!");
1131 switch (V->getValueType()) {
1132 case Value::ConstantVal:
Chris Lattner1e194682002-04-18 18:53:13 +00001133 case Value::ArgumentVal: AW->writeOperand(V, true, true); break;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +00001134 case Value::TypeVal: AW->write(cast<Type>(V)); break;
Chris Lattner7db79582001-11-07 04:21:57 +00001135 case Value::InstructionVal: AW->write(cast<Instruction>(V)); break;
1136 case Value::BasicBlockVal: AW->write(cast<BasicBlock>(V)); break;
Chris Lattner57698e22002-03-26 18:01:55 +00001137 case Value::FunctionVal: AW->write(cast<Function>(V)); break;
Chris Lattner7db79582001-11-07 04:21:57 +00001138 case Value::GlobalVariableVal: AW->write(cast<GlobalVariable>(V)); break;
Misha Brukman4685e262004-04-28 15:31:21 +00001139 default: *Out << "<unknown value type: " << V->getValueType() << ">"; break;
Chris Lattner7db79582001-11-07 04:21:57 +00001140 }
1141 return *this;
1142}
Misha Brukman4685e262004-04-28 15:31:21 +00001143
1144CachedWriter& CachedWriter::operator<<(const Type *X) {
1145 if (SymbolicTypes) {
1146 const Module *M = AW->getModule();
1147 if (M) WriteTypeSymbolic(*Out, X, M);
1148 return *this;
1149 } else
1150 return *this << (const Value*)X;
1151}
Misha Brukmanda546ea2004-04-28 19:24:28 +00001152
1153void CachedWriter::setStream(std::ostream &os) {
1154 Out = &os;
1155 if (AW) AW->setStream(os);
1156}
Reid Spencere7e96712004-05-25 08:53:40 +00001157
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001158//===----------------------------------------------------------------------===//
1159//===-- SlotMachine Implementation
1160//===----------------------------------------------------------------------===//
1161
1162#if 0
1163#define SC_DEBUG(X) std::cerr << X
1164#else
1165#define SC_DEBUG(X)
1166#endif
1167
1168// Module level constructor. Causes the contents of the Module (sans functions)
1169// to be added to the slot table.
1170SlotMachine::SlotMachine(const Module *M)
1171 : TheModule(M)
1172 , FunctionIncorporated(false)
1173 , mMap()
1174 , fMap()
1175{
1176 if ( M != 0 )
1177 processModule();
1178}
1179
1180// Function level constructor. Causes the contents of the Module and the one
1181// function provided to be added to the slot table.
1182SlotMachine::SlotMachine(const Function *F )
1183 : TheModule( F ? F->getParent() : 0 )
1184 , FunctionIncorporated(true)
1185 , mMap()
1186 , fMap()
1187{
1188 if ( TheModule ) {
1189 processModule(); // Process module level stuff
1190 incorporateFunction(F); // Start out in incorporated state
1191 }
1192}
1193
1194// Iterate through all the global variables, functions, and global
1195// variable initializers and create slots for them.
1196void SlotMachine::processModule() {
1197 SC_DEBUG("begin processModule!\n");
1198
1199 // Add all of the global variables to the value table...
1200 for (Module::const_giterator I = TheModule->gbegin(), E = TheModule->gend();
1201 I != E; ++I)
1202 createSlot(I);
1203
1204 // Add all the functions to the table
1205 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
1206 I != E; ++I)
1207 createSlot(I);
1208
1209 // Add all of the module level constants used as initializers
1210 for (Module::const_giterator I = TheModule->gbegin(), E = TheModule->gend();
1211 I != E; ++I)
1212 if (I->hasInitializer())
1213 createSlot(I->getInitializer());
1214
1215 SC_DEBUG("end processModule!\n");
1216}
1217
1218
1219// Incorporate the arguments, basic blocks, and instructions of a function.
1220// This is the *only* way to get the FunctionIncorporated flag set.
1221void SlotMachine::incorporateFunction(const Function *F) {
1222 SC_DEBUG("begin processFunction!\n");
1223
1224 FunctionIncorporated = true;
1225
1226 // Add all the function arguments
1227 for(Function::const_aiterator AI = F->abegin(),
1228 AE = F->aend(); AI != AE; ++AI)
1229 createSlot(AI);
1230
1231 SC_DEBUG("Inserting Instructions:\n");
1232
1233 // Add all of the basic blocks and instructions
1234 for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
1235 createSlot(BB);
1236 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
1237 createSlot(I);
1238 }
1239 }
1240
1241 SC_DEBUG("end processFunction!\n");
1242}
1243
1244// Clean up after incorporating a function. This is the only way
1245// (other than construction) to get the FunctionIncorporated flag cleared.
1246void SlotMachine::purgeFunction() {
1247 SC_DEBUG("begin purgeFunction!\n");
1248 fMap.clear(); // Simply discard the function level map
1249 FunctionIncorporated = false;
1250 SC_DEBUG("end purgeFunction!\n");
1251}
1252
1253/// Get the slot number for a value. This function will assert if you
1254/// ask for a Value that hasn't previously been inserted with createSlot.
1255/// Types are forbidden because Type does not inherit from Value (any more).
1256unsigned SlotMachine::getSlot(const Value *V) const {
1257 assert( V && "Can't get slot for null Value" );
1258 assert( !isa<Type>(V) && "Can't get slot for a type" );
1259
1260 // Do not number CPR's at all. They are an abomination
1261 if ( const ConstantPointerRef* CPR = dyn_cast<ConstantPointerRef>(V) )
1262 V = CPR->getValue() ;
1263
1264 // Get the type of the value
1265 const Type* VTy = V->getType();
1266
1267 // Find the type plane in the module map
1268 TypedPlanes::const_iterator MI = mMap.find(VTy);
1269
1270 if ( FunctionIncorporated ) {
1271 // Lookup the type in the function map too
1272 TypedPlanes::const_iterator FI = fMap.find(VTy);
1273 // If there is a corresponding type plane in the function map
1274 if ( FI != fMap.end() ) {
1275 // Lookup the Value in the function map
1276 ValueMap::const_iterator FVI = FI->second.map.find(V);
1277 // If the value doesn't exist in the function map
1278 if ( FVI == FI->second.map.end() ) {
1279 // Look up the value in the module map
1280 ValueMap::const_iterator MVI = MI->second.map.find(V);
1281 // If we didn't find it, it wasn't inserted
1282 assert( MVI != MI->second.map.end() && "Value not found");
1283 // We found it only at the module level
1284 return MVI->second;
1285
1286 // else the value exists in the function map
1287 } else {
1288 // Return the slot number as the module's contribution to
1289 // the type plane plus the index in the function's contribution
1290 // to the type plane.
1291 return MI->second.next_slot + FVI->second;
1292 }
1293
1294 // else there is not a corresponding type plane in the function map
1295 } else {
1296 assert( MI != mMap.end() && "No such type plane!" );
1297 // Look up the value in the module's 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 in the module level and function level
1302 // didn't even have a type plane.
1303 return MVI->second;
1304 }
1305 }
1306
1307 // N.B. Can only get here if !FunctionIncorporated
1308
1309 // Make sure the type plane exists
1310 assert( MI != mMap.end() && "No such type plane!" );
1311 // Lookup the value in the module's map
1312 ValueMap::const_iterator MVI = MI->second.map.find(V);
1313 // Make sure we found it.
1314 assert( MVI != MI->second.map.end() && "Value not found" );
1315 // Return it.
1316 return MVI->second;
1317}
1318
1319
1320// Create a new slot, or return the existing slot if it is already
1321// inserted. Note that the logic here parallels getSlot but instead
1322// of asserting when the Value* isn't found, it inserts the value.
1323unsigned SlotMachine::createSlot(const Value *V) {
1324 assert( V && "Can't insert a null Value to SlotMachine");
1325 assert( !isa<Type>(V) && "Can't insert a Type into SlotMachine");
1326
1327 const Type* VTy = V->getType();
1328
1329 // Just ignore void typed things
1330 if (VTy == Type::VoidTy) return 0; // FIXME: Wrong return value!
1331
1332 // Look up the type plane for the Value's type from the module map
1333 TypedPlanes::const_iterator MI = mMap.find(VTy);
1334
1335 if ( FunctionIncorporated ) {
1336 // Get the type plane for the Value's type from the function map
1337 TypedPlanes::const_iterator FI = fMap.find(VTy);
1338 // If there is a corresponding type plane in the function map
1339 if ( FI != fMap.end() ) {
1340 // Lookup the Value in the function map
1341 ValueMap::const_iterator FVI = FI->second.map.find(V);
1342 // If the value doesn't exist in the function map
1343 if ( FVI == FI->second.map.end() ) {
1344 // If there is no corresponding type plane in the module map
1345 if ( MI == mMap.end() )
1346 return insertValue(V);
1347 // Look up the value in the module map
1348 ValueMap::const_iterator MVI = MI->second.map.find(V);
1349 // If we didn't find it, it wasn't inserted
1350 if ( MVI == MI->second.map.end() )
1351 return insertValue(V);
1352 else
1353 // We found it only at the module level
1354 return MVI->second;
1355
1356 // else the value exists in the function map
1357 } else {
1358 if ( MI == mMap.end() )
1359 return FVI->second;
1360 else
1361 // Return the slot number as the module's contribution to
1362 // the type plane plus the index in the function's contribution
1363 // to the type plane.
1364 return MI->second.next_slot + FVI->second;
1365 }
1366
1367 // else there is not a corresponding type plane in the function map
1368 } else {
1369 // If the type plane doesn't exists at the module level
1370 if ( MI == mMap.end() ) {
1371 return insertValue(V);
1372 // else type plane exists at the module level, examine it
1373 } else {
1374 // Look up the value in the module's map
1375 ValueMap::const_iterator MVI = MI->second.map.find(V);
1376 // If we didn't find it there either
1377 if ( MVI == MI->second.map.end() )
1378 // Return the slot number as the module's contribution to
1379 // the type plane plus the index of the function map insertion.
1380 return MI->second.next_slot + insertValue(V);
1381 else
1382 return MVI->second;
1383 }
1384 }
1385 }
1386
1387 // N.B. Can only get here if !FunctionIncorporated
1388
1389 // If the module map's type plane is not for the Value's type
1390 if ( MI != mMap.end() ) {
1391 // Lookup the value in the module's map
1392 ValueMap::const_iterator MVI = MI->second.map.find(V);
1393 if ( MVI != MI->second.map.end() )
1394 return MVI->second;
1395 }
1396
1397 return insertValue(V);
1398}
1399
1400
1401// Low level insert function. Minimal checking is done. This
1402// function is just for the convenience of createSlot (above).
1403unsigned SlotMachine::insertValue(const Value *V ) {
1404 assert(V && "Can't insert a null Value into SlotMachine!");
1405 assert(!isa<Type>(V) && "Can't insert a Type into SlotMachine!");
1406
1407 // If this value does not contribute to a plane (is void or constant)
1408 // or if the value already has a name then ignore it.
1409 if (V->getType() == Type::VoidTy || // Ignore void type nodes
1410 (V->hasName() || isa<Constant>(V)) ) {
1411 SC_DEBUG("ignored value " << *V << "\n");
1412 return 0; // FIXME: Wrong return value
1413 }
1414
1415 if (!isa<GlobalValue>(V)) // Initializers for globals are handled explicitly
1416 if (const Constant *C = dyn_cast<Constant>(V)) {
1417 // This makes sure that if a constant has uses (for example an array of
1418 // const ints), that they are inserted also.
1419 for (User::const_op_iterator I = C->op_begin(), E = C->op_end();
1420 I != E; ++I)
1421 createSlot(*I);
1422 }
1423
1424 const Type *VTy = V->getType();
1425 unsigned DestSlot = 0;
1426
1427 if ( FunctionIncorporated ) {
1428 TypedPlanes::iterator I = fMap.find( VTy );
1429 if ( I == fMap.end() )
1430 I = fMap.insert(std::make_pair(VTy,Plane())).first;
1431 DestSlot = I->second.map[V] = I->second.next_slot++;
1432 } else {
1433 TypedPlanes::iterator I = mMap.find( VTy );
1434 if ( I == mMap.end() )
1435 I = mMap.insert(std::make_pair(VTy,Plane())).first;
1436 DestSlot = I->second.map[V] = I->second.next_slot++;
1437 }
1438
1439 SC_DEBUG(" Inserting value [" << VTy << "] = " << V << " slot=" <<
1440 DestSlot << " [");
1441 // G = Global, C = Constant, T = Type, F = Function, o = other
1442 SC_DEBUG((isa<GlobalVariable>(V) ? "G" : (isa<Constant>(V) ? "C" :
1443 (isa<Function>(V) ? "F" : "o"))));
1444 SC_DEBUG("]\n");
1445 return DestSlot;
1446}
1447
Reid Spencere7e96712004-05-25 08:53:40 +00001448// vim: sw=2