blob: e7928e25eb6a462a8b2b88f83829a422232df2a1 [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"
29#include "llvm/Analysis/SlotCalculator.h"
Chris Lattnerfee714f2001-09-07 16:36:04 +000030#include "llvm/SymbolTable.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
Chris Lattnerc8b70922002-07-26 21:12:46 +000037static RegisterPass<PrintModulePass>
38X("printm", "Print module to stderr",PassInfo::Analysis|PassInfo::Optimization);
39static RegisterPass<PrintFunctionPass>
40Y("print","Print function to stderr",PassInfo::Analysis|PassInfo::Optimization);
Chris Lattner7f8845a2002-07-23 18:07:49 +000041
Chris Lattnerab7d1ab2003-05-08 02:08:14 +000042static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
43 bool PrintName,
44 std::map<const Type *, std::string> &TypeTable,
Chris Lattnerd84bb632002-04-16 21:36:08 +000045 SlotCalculator *Table);
46
Chris Lattnerb86620e2001-10-29 16:37:48 +000047static const Module *getModuleFromVal(const Value *V) {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +000048 if (const Argument *MA = dyn_cast<Argument>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +000049 return MA->getParent() ? MA->getParent()->getParent() : 0;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +000050 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +000051 return BB->getParent() ? BB->getParent()->getParent() : 0;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +000052 else if (const Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner57698e22002-03-26 18:01:55 +000053 const Function *M = I->getParent() ? I->getParent()->getParent() : 0;
Chris Lattnerb86620e2001-10-29 16:37:48 +000054 return M ? M->getParent() : 0;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +000055 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +000056 return GV->getParent();
Chris Lattnerb86620e2001-10-29 16:37:48 +000057 return 0;
58}
59
Chris Lattner7bfee412001-10-29 16:05:51 +000060static SlotCalculator *createSlotCalculator(const Value *V) {
61 assert(!isa<Type>(V) && "Can't create an SC for a type!");
Chris Lattnerf26a8ee2003-07-23 15:30:06 +000062 if (const Argument *FA = dyn_cast<Argument>(V)) {
Chris Lattner6089bb12004-01-14 02:49:34 +000063 return new SlotCalculator(FA->getParent(), false);
Chris Lattnerf26a8ee2003-07-23 15:30:06 +000064 } else if (const Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner6089bb12004-01-14 02:49:34 +000065 return new SlotCalculator(I->getParent()->getParent(), false);
Chris Lattnerf26a8ee2003-07-23 15:30:06 +000066 } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
Chris Lattner6089bb12004-01-14 02:49:34 +000067 return new SlotCalculator(BB->getParent(), false);
Chris Lattnerf26a8ee2003-07-23 15:30:06 +000068 } else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)){
Chris Lattner6089bb12004-01-14 02:49:34 +000069 return new SlotCalculator(GV->getParent(), false);
Chris Lattnerf26a8ee2003-07-23 15:30:06 +000070 } else if (const Function *Func = dyn_cast<Function>(V)) {
Chris Lattner6089bb12004-01-14 02:49:34 +000071 return new SlotCalculator(Func, false);
Chris Lattner7bfee412001-10-29 16:05:51 +000072 }
73 return 0;
74}
Chris Lattner2f7c9632001-06-06 20:29:01 +000075
Chris Lattner1c343042003-08-22 05:40:38 +000076// getLLVMName - Turn the specified string into an 'LLVM name', which is either
77// prefixed with % (if the string only contains simple characters) or is
78// surrounded with ""'s (if it has special chars in it).
79static std::string getLLVMName(const std::string &Name) {
80 assert(!Name.empty() && "Cannot get empty name!");
81
82 // First character cannot start with a number...
83 if (Name[0] >= '0' && Name[0] <= '9')
84 return "\"" + Name + "\"";
85
86 // Scan to see if we have any characters that are not on the "white list"
87 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
88 char C = Name[i];
89 assert(C != '"' && "Illegal character in LLVM value name!");
90 if ((C < 'a' || C > 'z') && (C < 'A' || C > 'Z') && (C < '0' || C > '9') &&
91 C != '-' && C != '.' && C != '_')
92 return "\"" + Name + "\"";
93 }
94
95 // If we get here, then the identifier is legal to use as a "VarID".
96 return "%"+Name;
97}
98
Chris Lattnerb86620e2001-10-29 16:37:48 +000099
100// If the module has a symbol table, take all global types and stuff their
101// names into the TypeNames map.
102//
103static void fillTypeNameTable(const Module *M,
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000104 std::map<const Type *, std::string> &TypeNames) {
Chris Lattner98cf1f52002-11-20 18:36:02 +0000105 if (!M) return;
106 const SymbolTable &ST = M->getSymbolTable();
107 SymbolTable::const_iterator PI = ST.find(Type::TypeTy);
108 if (PI != ST.end()) {
109 SymbolTable::type_const_iterator I = PI->second.begin();
110 for (; I != PI->second.end(); ++I) {
111 // As a heuristic, don't insert pointer to primitive types, because
112 // they are used too often to have a single useful name.
113 //
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000114 const Type *Ty = cast<Type>(I->second);
Chris Lattner98cf1f52002-11-20 18:36:02 +0000115 if (!isa<PointerType>(Ty) ||
Chris Lattnerf14ead92003-10-30 00:22:33 +0000116 !cast<PointerType>(Ty)->getElementType()->isPrimitiveType() ||
117 isa<OpaqueType>(cast<PointerType>(Ty)->getElementType()))
Chris Lattner1c343042003-08-22 05:40:38 +0000118 TypeNames.insert(std::make_pair(Ty, getLLVMName(I->first)));
Chris Lattnerb86620e2001-10-29 16:37:48 +0000119 }
120 }
121}
122
123
124
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000125static std::string calcTypeName(const Type *Ty,
126 std::vector<const Type *> &TypeStack,
127 std::map<const Type *, std::string> &TypeNames){
Chris Lattnerf14ead92003-10-30 00:22:33 +0000128 if (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty))
129 return Ty->getDescription(); // Base case
Chris Lattnerb86620e2001-10-29 16:37:48 +0000130
131 // Check to see if the type is named.
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000132 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000133 if (I != TypeNames.end()) return I->second;
134
Chris Lattnerf14ead92003-10-30 00:22:33 +0000135 if (isa<OpaqueType>(Ty))
136 return "opaque";
137
Chris Lattnerb86620e2001-10-29 16:37:48 +0000138 // Check to see if the Type is already on the stack...
139 unsigned Slot = 0, CurSize = TypeStack.size();
140 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
141
142 // This is another base case for the recursion. In this case, we know
143 // that we have looped back to a type that we have previously visited.
144 // Generate the appropriate upreference to handle this.
145 //
146 if (Slot < CurSize)
147 return "\\" + utostr(CurSize-Slot); // Here's the upreference
148
149 TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
150
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000151 std::string Result;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000152 switch (Ty->getPrimitiveID()) {
Chris Lattner91db5822002-03-29 03:44:36 +0000153 case Type::FunctionTyID: {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000154 const FunctionType *FTy = cast<FunctionType>(Ty);
Chris Lattnerd816b532002-04-13 20:53:41 +0000155 Result = calcTypeName(FTy->getReturnType(), TypeStack, TypeNames) + " (";
Chris Lattner91db5822002-03-29 03:44:36 +0000156 for (FunctionType::ParamTypes::const_iterator
Chris Lattnerd816b532002-04-13 20:53:41 +0000157 I = FTy->getParamTypes().begin(),
158 E = FTy->getParamTypes().end(); I != E; ++I) {
159 if (I != FTy->getParamTypes().begin())
Chris Lattnerb86620e2001-10-29 16:37:48 +0000160 Result += ", ";
161 Result += calcTypeName(*I, TypeStack, TypeNames);
162 }
Chris Lattnerd816b532002-04-13 20:53:41 +0000163 if (FTy->isVarArg()) {
164 if (!FTy->getParamTypes().empty()) Result += ", ";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000165 Result += "...";
166 }
167 Result += ")";
168 break;
169 }
170 case Type::StructTyID: {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000171 const StructType *STy = cast<StructType>(Ty);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000172 Result = "{ ";
173 for (StructType::ElementTypes::const_iterator
174 I = STy->getElementTypes().begin(),
175 E = STy->getElementTypes().end(); I != E; ++I) {
176 if (I != STy->getElementTypes().begin())
177 Result += ", ";
178 Result += calcTypeName(*I, TypeStack, TypeNames);
179 }
180 Result += " }";
181 break;
182 }
183 case Type::PointerTyID:
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000184 Result = calcTypeName(cast<PointerType>(Ty)->getElementType(),
Chris Lattner189088e2002-04-12 18:21:53 +0000185 TypeStack, TypeNames) + "*";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000186 break;
187 case Type::ArrayTyID: {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000188 const ArrayType *ATy = cast<ArrayType>(Ty);
Chris Lattner8a939c92002-04-13 21:11:04 +0000189 Result = "[" + utostr(ATy->getNumElements()) + " x ";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000190 Result += calcTypeName(ATy->getElementType(), TypeStack, TypeNames) + "]";
191 break;
192 }
Chris Lattner15285ab2003-05-14 17:50:47 +0000193 case Type::OpaqueTyID:
194 Result = "opaque";
195 break;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000196 default:
Vikram S. Adveb952b542002-07-14 23:14:45 +0000197 Result = "<unrecognized-type>";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000198 }
199
200 TypeStack.pop_back(); // Remove self from stack...
201 return Result;
202}
203
204
205// printTypeInt - The internal guts of printing out a type that has a
206// potentially named portion.
207//
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000208static std::ostream &printTypeInt(std::ostream &Out, const Type *Ty,
209 std::map<const Type *, std::string> &TypeNames) {
Chris Lattnerb86620e2001-10-29 16:37:48 +0000210 // Primitive types always print out their description, regardless of whether
211 // they have been named or not.
212 //
Chris Lattner92d60532003-10-30 00:12:51 +0000213 if (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty))
214 return Out << Ty->getDescription();
Chris Lattnerb86620e2001-10-29 16:37:48 +0000215
216 // Check to see if the type is named.
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000217 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000218 if (I != TypeNames.end()) return Out << I->second;
219
220 // Otherwise we have a type that has not been named but is a derived type.
221 // Carefully recurse the type hierarchy to print out any contained symbolic
222 // names.
223 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000224 std::vector<const Type *> TypeStack;
225 std::string TypeName = calcTypeName(Ty, TypeStack, TypeNames);
Chris Lattner7f74a562002-01-20 22:54:45 +0000226 TypeNames.insert(std::make_pair(Ty, TypeName));//Cache type name for later use
Chris Lattnerb86620e2001-10-29 16:37:48 +0000227 return Out << TypeName;
228}
229
Chris Lattner34b95182001-10-31 04:33:19 +0000230
Chris Lattnerb86620e2001-10-29 16:37:48 +0000231// WriteTypeSymbolic - This attempts to write the specified type as a symbolic
232// type, iff there is an entry in the modules symbol table for the specified
233// type or one of it's component types. This is slower than a simple x << Type;
234//
Chris Lattner189d19f2003-11-21 20:23:48 +0000235std::ostream &llvm::WriteTypeSymbolic(std::ostream &Out, const Type *Ty,
236 const Module *M) {
Chris Lattnerb86620e2001-10-29 16:37:48 +0000237 Out << " ";
238
239 // If they want us to print out a type, attempt to make it symbolic if there
240 // is a symbol table in the module...
Chris Lattner98cf1f52002-11-20 18:36:02 +0000241 if (M) {
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000242 std::map<const Type *, std::string> TypeNames;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000243 fillTypeNameTable(M, TypeNames);
244
Chris Lattner72f866e2001-10-29 16:40:32 +0000245 return printTypeInt(Out, Ty, TypeNames);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000246 } else {
Chris Lattner72f866e2001-10-29 16:40:32 +0000247 return Out << Ty->getDescription();
Chris Lattnerb86620e2001-10-29 16:37:48 +0000248 }
249}
250
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000251static void WriteConstantInt(std::ostream &Out, const Constant *CV,
252 bool PrintName,
253 std::map<const Type *, std::string> &TypeTable,
Chris Lattnerd84bb632002-04-16 21:36:08 +0000254 SlotCalculator *Table) {
Chris Lattner1e194682002-04-18 18:53:13 +0000255 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
256 Out << (CB == ConstantBool::True ? "true" : "false");
257 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV)) {
258 Out << CI->getValue();
259 } else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV)) {
260 Out << CI->getValue();
261 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
262 // We would like to output the FP constant value in exponential notation,
263 // but we cannot do this if doing so will lose precision. Check here to
264 // make sure that we only output it in exponential format if we can parse
265 // the value back and get the same value.
266 //
267 std::string StrVal = ftostr(CFP->getValue());
268
269 // Check to make sure that the stringized number is not some string like
270 // "Inf" or NaN, that atof will accept, but the lexer will not. Check that
271 // the string matches the "[-+]?[0-9]" regex.
272 //
273 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
274 ((StrVal[0] == '-' || StrVal[0] == '+') &&
Brian Gaeke87b4f072003-06-17 23:55:35 +0000275 (StrVal[1] >= '0' && StrVal[1] <= '9')))
Chris Lattner1e194682002-04-18 18:53:13 +0000276 // Reparse stringized version!
277 if (atof(StrVal.c_str()) == CFP->getValue()) {
278 Out << StrVal; return;
279 }
280
281 // Otherwise we could not reparse it to exactly the same value, so we must
282 // output the string in hexadecimal format!
283 //
284 // Behave nicely in the face of C TBAA rules... see:
285 // http://www.nullstone.com/htmls/category/aliastyp.htm
286 //
287 double Val = CFP->getValue();
288 char *Ptr = (char*)&Val;
289 assert(sizeof(double) == sizeof(uint64_t) && sizeof(double) == 8 &&
290 "assuming that double is 64 bits!");
291 Out << "0x" << utohexstr(*(uint64_t*)Ptr);
292
293 } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
Chris Lattnerd401f392003-06-28 20:08:24 +0000294 if (CA->getNumOperands() > 5 && CA->isNullValue()) {
295 Out << "zeroinitializer";
296 return;
297 }
298
Chris Lattner1e194682002-04-18 18:53:13 +0000299 // As a special case, print the array as a string if it is an array of
300 // ubytes or an array of sbytes with positive values.
301 //
302 const Type *ETy = CA->getType()->getElementType();
303 bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
304
305 if (ETy == Type::SByteTy)
306 for (unsigned i = 0; i < CA->getNumOperands(); ++i)
307 if (cast<ConstantSInt>(CA->getOperand(i))->getValue() < 0) {
308 isString = false;
309 break;
310 }
311
312 if (isString) {
313 Out << "c\"";
314 for (unsigned i = 0; i < CA->getNumOperands(); ++i) {
Chris Lattner6077c312003-07-23 15:22:26 +0000315 unsigned char C = cast<ConstantInt>(CA->getOperand(i))->getRawValue();
Chris Lattner1e194682002-04-18 18:53:13 +0000316
Chris Lattnere5fd3862002-07-31 23:56:44 +0000317 if (isprint(C) && C != '"' && C != '\\') {
Chris Lattner1e194682002-04-18 18:53:13 +0000318 Out << C;
319 } else {
320 Out << '\\'
321 << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'))
322 << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
323 }
324 }
325 Out << "\"";
326
327 } else { // Cannot output in string format...
Chris Lattnerd84bb632002-04-16 21:36:08 +0000328 Out << "[";
329 if (CA->getNumOperands()) {
330 Out << " ";
Chris Lattner1e194682002-04-18 18:53:13 +0000331 printTypeInt(Out, ETy, TypeTable);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000332 WriteAsOperandInternal(Out, CA->getOperand(0),
333 PrintName, TypeTable, Table);
334 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
335 Out << ", ";
Chris Lattner1e194682002-04-18 18:53:13 +0000336 printTypeInt(Out, ETy, TypeTable);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000337 WriteAsOperandInternal(Out, CA->getOperand(i), PrintName,
338 TypeTable, Table);
339 }
340 }
341 Out << " ]";
342 }
343 } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Chris Lattnerd401f392003-06-28 20:08:24 +0000344 if (CS->getNumOperands() > 5 && CS->isNullValue()) {
345 Out << "zeroinitializer";
346 return;
347 }
348
Chris Lattnerd84bb632002-04-16 21:36:08 +0000349 Out << "{";
350 if (CS->getNumOperands()) {
351 Out << " ";
352 printTypeInt(Out, CS->getOperand(0)->getType(), TypeTable);
353
354 WriteAsOperandInternal(Out, CS->getOperand(0),
355 PrintName, TypeTable, Table);
356
357 for (unsigned i = 1; i < CS->getNumOperands(); i++) {
358 Out << ", ";
359 printTypeInt(Out, CS->getOperand(i)->getType(), TypeTable);
360
361 WriteAsOperandInternal(Out, CS->getOperand(i),
362 PrintName, TypeTable, Table);
363 }
364 }
365
366 Out << " }";
367 } else if (isa<ConstantPointerNull>(CV)) {
368 Out << "null";
369
Chris Lattner113f4f42002-06-25 16:13:24 +0000370 } else if (const ConstantPointerRef *PR = dyn_cast<ConstantPointerRef>(CV)) {
Chris Lattnerf78819d2004-01-18 21:03:06 +0000371 WriteAsOperandInternal(Out, PR->getValue(), true, TypeTable, Table);
Vikram S. Adveb952b542002-07-14 23:14:45 +0000372
Chris Lattner3cd8c562002-07-30 18:54:25 +0000373 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Chris Lattnerb8257222003-03-06 23:23:32 +0000374 Out << CE->getOpcodeName() << " (";
Vikram S. Adveb952b542002-07-14 23:14:45 +0000375
376 for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
377 printTypeInt(Out, (*OI)->getType(), TypeTable);
378 WriteAsOperandInternal(Out, *OI, PrintName, TypeTable, Table);
379 if (OI+1 != CE->op_end())
Chris Lattner3cd8c562002-07-30 18:54:25 +0000380 Out << ", ";
Vikram S. Adveb952b542002-07-14 23:14:45 +0000381 }
382
Chris Lattnercfe8f532002-08-16 21:17:11 +0000383 if (CE->getOpcode() == Instruction::Cast) {
Chris Lattner83b396b2002-08-15 19:37:43 +0000384 Out << " to ";
385 printTypeInt(Out, CE->getType(), TypeTable);
386 }
Chris Lattnercfe8f532002-08-16 21:17:11 +0000387 Out << ")";
Chris Lattner83b396b2002-08-15 19:37:43 +0000388
Chris Lattnerd84bb632002-04-16 21:36:08 +0000389 } else {
Vikram S. Adveb952b542002-07-14 23:14:45 +0000390 Out << "<placeholder or erroneous Constant>";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000391 }
392}
393
394
395// WriteAsOperand - Write the name of the specified value out to the specified
396// ostream. This can be useful when you just want to print int %reg126, not the
397// whole instruction that generated it.
398//
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000399static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
400 bool PrintName,
401 std::map<const Type*, std::string> &TypeTable,
Chris Lattnerd84bb632002-04-16 21:36:08 +0000402 SlotCalculator *Table) {
403 Out << " ";
404 if (PrintName && V->hasName()) {
Chris Lattner1c343042003-08-22 05:40:38 +0000405 Out << getLLVMName(V->getName());
Chris Lattnerd84bb632002-04-16 21:36:08 +0000406 } else {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000407 if (const Constant *CV = dyn_cast<Constant>(V)) {
Chris Lattnerd84bb632002-04-16 21:36:08 +0000408 WriteConstantInt(Out, CV, PrintName, TypeTable, Table);
409 } else {
410 int Slot;
411 if (Table) {
Alkis Evlogimenos8faf8d92003-10-17 02:02:40 +0000412 Slot = Table->getSlot(V);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000413 } else {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000414 if (const Type *Ty = dyn_cast<Type>(V)) {
Chris Lattnerd84bb632002-04-16 21:36:08 +0000415 Out << Ty->getDescription();
416 return;
417 }
418
419 Table = createSlotCalculator(V);
420 if (Table == 0) { Out << "BAD VALUE TYPE!"; return; }
421
Alkis Evlogimenos8faf8d92003-10-17 02:02:40 +0000422 Slot = Table->getSlot(V);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000423 delete Table;
424 }
425 if (Slot >= 0) Out << "%" << Slot;
426 else if (PrintName)
Misha Brukmanfa100532003-10-10 17:54:14 +0000427 Out << "<badref>"; // Not embedded into a location?
Chris Lattnerd84bb632002-04-16 21:36:08 +0000428 }
429 }
430}
431
432
Chris Lattnerb86620e2001-10-29 16:37:48 +0000433
434// WriteAsOperand - Write the name of the specified value out to the specified
435// ostream. This can be useful when you just want to print int %reg126, not the
436// whole instruction that generated it.
437//
Chris Lattner189d19f2003-11-21 20:23:48 +0000438std::ostream &llvm::WriteAsOperand(std::ostream &Out, const Value *V,
439 bool PrintType,
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000440 bool PrintName, const Module *Context) {
441 std::map<const Type *, std::string> TypeNames;
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000442 if (Context == 0) Context = getModuleFromVal(V);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000443
Chris Lattner98cf1f52002-11-20 18:36:02 +0000444 if (Context)
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000445 fillTypeNameTable(Context, TypeNames);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000446
447 if (PrintType)
448 printTypeInt(Out, V->getType(), TypeNames);
449
Brian Gaekefda1f182003-11-16 23:08:27 +0000450 if (const Type *Ty = dyn_cast<Type> (V))
451 printTypeInt(Out, Ty, TypeNames);
452
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000453 WriteAsOperandInternal(Out, V, PrintName, TypeNames, 0);
Chris Lattner5e5abe32001-07-20 19:15:21 +0000454 return Out;
455}
456
Chris Lattner189d19f2003-11-21 20:23:48 +0000457namespace llvm {
Chris Lattner2e9fee42001-07-12 23:35:26 +0000458
Chris Lattnerfee714f2001-09-07 16:36:04 +0000459class AssemblyWriter {
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000460 std::ostream &Out;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000461 SlotCalculator &Table;
Chris Lattner7bfee412001-10-29 16:05:51 +0000462 const Module *TheModule;
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000463 std::map<const Type *, std::string> TypeNames;
Chris Lattner8339f7d2003-10-30 23:41:03 +0000464 AssemblyAnnotationWriter *AnnotationWriter;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000465public:
Chris Lattner8339f7d2003-10-30 23:41:03 +0000466 inline AssemblyWriter(std::ostream &o, SlotCalculator &Tab, const Module *M,
467 AssemblyAnnotationWriter *AAW)
468 : Out(o), Table(Tab), TheModule(M), AnnotationWriter(AAW) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000469
470 // If the module has a symbol table, take all global types and stuff their
471 // names into the TypeNames map.
472 //
Chris Lattnerb86620e2001-10-29 16:37:48 +0000473 fillTypeNameTable(M, TypeNames);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000474 }
475
Chris Lattner7bfee412001-10-29 16:05:51 +0000476 inline void write(const Module *M) { printModule(M); }
477 inline void write(const GlobalVariable *G) { printGlobal(G); }
Chris Lattner57698e22002-03-26 18:01:55 +0000478 inline void write(const Function *F) { printFunction(F); }
Chris Lattner7bfee412001-10-29 16:05:51 +0000479 inline void write(const BasicBlock *BB) { printBasicBlock(BB); }
Chris Lattner113f4f42002-06-25 16:13:24 +0000480 inline void write(const Instruction *I) { printInstruction(*I); }
Chris Lattner3462ae32001-12-03 22:26:30 +0000481 inline void write(const Constant *CPV) { printConstant(CPV); }
Chris Lattner7db79582001-11-07 04:21:57 +0000482 inline void write(const Type *Ty) { printType(Ty); }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000483
Chris Lattner1e194682002-04-18 18:53:13 +0000484 void writeOperand(const Value *Op, bool PrintType, bool PrintName = true);
485
Chris Lattner2f7c9632001-06-06 20:29:01 +0000486private :
Chris Lattner7bfee412001-10-29 16:05:51 +0000487 void printModule(const Module *M);
488 void printSymbolTable(const SymbolTable &ST);
Chris Lattner3462ae32001-12-03 22:26:30 +0000489 void printConstant(const Constant *CPV);
Chris Lattner7bfee412001-10-29 16:05:51 +0000490 void printGlobal(const GlobalVariable *GV);
Chris Lattner57698e22002-03-26 18:01:55 +0000491 void printFunction(const Function *F);
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000492 void printArgument(const Argument *FA);
Chris Lattner7bfee412001-10-29 16:05:51 +0000493 void printBasicBlock(const BasicBlock *BB);
Chris Lattner113f4f42002-06-25 16:13:24 +0000494 void printInstruction(const Instruction &I);
Chris Lattnerd816b532002-04-13 20:53:41 +0000495
496 // printType - Go to extreme measures to attempt to print out a short,
497 // symbolic version of a type name.
498 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000499 std::ostream &printType(const Type *Ty) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000500 return printTypeInt(Out, Ty, TypeNames);
501 }
502
503 // printTypeAtLeastOneLevel - Print out one level of the possibly complex type
504 // without considering any symbolic types that we may have equal to it.
505 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000506 std::ostream &printTypeAtLeastOneLevel(const Type *Ty);
Chris Lattner7bfee412001-10-29 16:05:51 +0000507
Chris Lattner862e3382001-10-13 06:42:36 +0000508 // printInfoComment - Print a little comment after the instruction indicating
509 // which slot it occupies.
Chris Lattner113f4f42002-06-25 16:13:24 +0000510 void printInfoComment(const Value &V);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000511};
Chris Lattner189d19f2003-11-21 20:23:48 +0000512} // end of anonymous namespace
Chris Lattner2f7c9632001-06-06 20:29:01 +0000513
Chris Lattnerd816b532002-04-13 20:53:41 +0000514// printTypeAtLeastOneLevel - Print out one level of the possibly complex type
515// without considering any symbolic types that we may have equal to it.
516//
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000517std::ostream &AssemblyWriter::printTypeAtLeastOneLevel(const Type *Ty) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000518 if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000519 printType(FTy->getReturnType()) << " (";
520 for (FunctionType::ParamTypes::const_iterator
521 I = FTy->getParamTypes().begin(),
522 E = FTy->getParamTypes().end(); I != E; ++I) {
523 if (I != FTy->getParamTypes().begin())
524 Out << ", ";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000525 printType(*I);
Chris Lattnerd816b532002-04-13 20:53:41 +0000526 }
527 if (FTy->isVarArg()) {
528 if (!FTy->getParamTypes().empty()) Out << ", ";
529 Out << "...";
530 }
531 Out << ")";
Chris Lattner113f4f42002-06-25 16:13:24 +0000532 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000533 Out << "{ ";
534 for (StructType::ElementTypes::const_iterator
535 I = STy->getElementTypes().begin(),
536 E = STy->getElementTypes().end(); I != E; ++I) {
537 if (I != STy->getElementTypes().begin())
538 Out << ", ";
539 printType(*I);
540 }
541 Out << " }";
Chris Lattner113f4f42002-06-25 16:13:24 +0000542 } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000543 printType(PTy->getElementType()) << "*";
Chris Lattner113f4f42002-06-25 16:13:24 +0000544 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000545 Out << "[" << ATy->getNumElements() << " x ";
546 printType(ATy->getElementType()) << "]";
Chris Lattner113f4f42002-06-25 16:13:24 +0000547 } else if (const OpaqueType *OTy = dyn_cast<OpaqueType>(Ty)) {
Chris Lattner39adbfc2003-06-01 03:45:51 +0000548 Out << "opaque";
Chris Lattnerd816b532002-04-13 20:53:41 +0000549 } else {
Vikram S. Adveb952b542002-07-14 23:14:45 +0000550 if (!Ty->isPrimitiveType())
551 Out << "<unknown derived type>";
Chris Lattnerd816b532002-04-13 20:53:41 +0000552 printType(Ty);
553 }
554 return Out;
555}
556
557
Chris Lattnerfee714f2001-09-07 16:36:04 +0000558void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType,
559 bool PrintName) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000560 if (PrintType) { Out << " "; printType(Operand->getType()); }
Chris Lattnerd84bb632002-04-16 21:36:08 +0000561 WriteAsOperandInternal(Out, Operand, PrintName, TypeNames, &Table);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000562}
563
Chris Lattner2f7c9632001-06-06 20:29:01 +0000564
Chris Lattner7bfee412001-10-29 16:05:51 +0000565void AssemblyWriter::printModule(const Module *M) {
Chris Lattner8068e0c2003-08-24 13:48:48 +0000566 switch (M->getEndianness()) {
567 case Module::LittleEndian: Out << "target endian = little\n"; break;
568 case Module::BigEndian: Out << "target endian = big\n"; break;
569 case Module::AnyEndianness: break;
570 }
571 switch (M->getPointerSize()) {
572 case Module::Pointer32: Out << "target pointersize = 32\n"; break;
573 case Module::Pointer64: Out << "target pointersize = 64\n"; break;
574 case Module::AnyPointerSize: break;
575 }
576
Chris Lattnerfee714f2001-09-07 16:36:04 +0000577 // Loop over the symbol table, emitting all named constants...
Chris Lattner98cf1f52002-11-20 18:36:02 +0000578 printSymbolTable(M->getSymbolTable());
Chris Lattnerda975502001-09-10 07:58:01 +0000579
Chris Lattner113f4f42002-06-25 16:13:24 +0000580 for (Module::const_giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
581 printGlobal(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000582
Chris Lattnerb2f02e52002-05-06 03:00:40 +0000583 Out << "\nimplementation ; Functions:\n";
Vikram S. Adve13ba19a2001-09-18 12:48:16 +0000584
Chris Lattner6915f8f2002-04-07 22:49:37 +0000585 // Output all of the functions...
Chris Lattner113f4f42002-06-25 16:13:24 +0000586 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
587 printFunction(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000588}
589
Chris Lattner7bfee412001-10-29 16:05:51 +0000590void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Chris Lattner1c343042003-08-22 05:40:38 +0000591 if (GV->hasName()) Out << getLLVMName(GV->getName()) << " = ";
Chris Lattner37798642001-09-18 04:01:05 +0000592
Chris Lattner379a8d22003-04-16 20:28:45 +0000593 if (!GV->hasInitializer())
594 Out << "external ";
595 else
596 switch (GV->getLinkage()) {
Chris Lattner068ad842003-10-16 18:29:00 +0000597 case GlobalValue::InternalLinkage: Out << "internal "; break;
598 case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
599 case GlobalValue::WeakLinkage: Out << "weak "; break;
Chris Lattner379a8d22003-04-16 20:28:45 +0000600 case GlobalValue::AppendingLinkage: Out << "appending "; break;
601 case GlobalValue::ExternalLinkage: break;
602 }
Chris Lattner37798642001-09-18 04:01:05 +0000603
Chris Lattner7bfee412001-10-29 16:05:51 +0000604 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattner2413b162001-12-04 00:03:30 +0000605 printType(GV->getType()->getElementType());
Chris Lattner37798642001-09-18 04:01:05 +0000606
607 if (GV->hasInitializer())
608 writeOperand(GV->getInitializer(), false, false);
609
Chris Lattner113f4f42002-06-25 16:13:24 +0000610 printInfoComment(*GV);
Chris Lattner7f74a562002-01-20 22:54:45 +0000611 Out << "\n";
Chris Lattnerda975502001-09-10 07:58:01 +0000612}
613
Chris Lattnerfee714f2001-09-07 16:36:04 +0000614
Chris Lattner7bfee412001-10-29 16:05:51 +0000615// printSymbolTable - Run through symbol table looking for named constants
Chris Lattnerfee714f2001-09-07 16:36:04 +0000616// if a named constant is found, emit it's declaration...
617//
Chris Lattner7bfee412001-10-29 16:05:51 +0000618void AssemblyWriter::printSymbolTable(const SymbolTable &ST) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000619 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
620 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
621 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
622
623 for (; I != End; ++I) {
624 const Value *V = I->second;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000625 if (const Constant *CPV = dyn_cast<Constant>(V)) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000626 printConstant(CPV);
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000627 } else if (const Type *Ty = dyn_cast<Type>(V)) {
Chris Lattnerda8571b2003-11-09 15:51:07 +0000628 assert(Ty->getType() == Type::TypeTy && TI->first == Type::TypeTy);
Chris Lattner1c343042003-08-22 05:40:38 +0000629 Out << "\t" << getLLVMName(I->first) << " = type ";
Chris Lattnerd816b532002-04-13 20:53:41 +0000630
631 // Make sure we print out at least one level of the type structure, so
632 // that we do not get %FILE = type %FILE
633 //
634 printTypeAtLeastOneLevel(Ty) << "\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000635 }
636 }
Chris Lattnera7620d92001-07-15 06:35:59 +0000637 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000638}
639
640
Chris Lattner7bfee412001-10-29 16:05:51 +0000641// printConstant - Print out a constant pool entry...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000642//
Chris Lattner3462ae32001-12-03 22:26:30 +0000643void AssemblyWriter::printConstant(const Constant *CPV) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000644 // Don't print out unnamed constants, they will be inlined
645 if (!CPV->hasName()) return;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000646
Chris Lattneree998be2001-07-26 16:29:38 +0000647 // Print out name...
Chris Lattner1c343042003-08-22 05:40:38 +0000648 Out << "\t" << getLLVMName(CPV->getName()) << " =";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000649
650 // Write the value out now...
Chris Lattnerd84bb632002-04-16 21:36:08 +0000651 writeOperand(CPV, true, false);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000652
Chris Lattner113f4f42002-06-25 16:13:24 +0000653 printInfoComment(*CPV);
Chris Lattner7f74a562002-01-20 22:54:45 +0000654 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000655}
656
Chris Lattner6915f8f2002-04-07 22:49:37 +0000657// printFunction - Print all aspects of a function.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000658//
Chris Lattner113f4f42002-06-25 16:13:24 +0000659void AssemblyWriter::printFunction(const Function *F) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000660 // Print out the return type and name...
Chris Lattner379a8d22003-04-16 20:28:45 +0000661 Out << "\n";
662
Chris Lattner8339f7d2003-10-30 23:41:03 +0000663 if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out);
664
Chris Lattner379a8d22003-04-16 20:28:45 +0000665 if (F->isExternal())
666 Out << "declare ";
667 else
668 switch (F->getLinkage()) {
Chris Lattner068ad842003-10-16 18:29:00 +0000669 case GlobalValue::InternalLinkage: Out << "internal "; break;
670 case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
671 case GlobalValue::WeakLinkage: Out << "weak "; break;
Chris Lattner379a8d22003-04-16 20:28:45 +0000672 case GlobalValue::AppendingLinkage: Out << "appending "; break;
673 case GlobalValue::ExternalLinkage: break;
674 }
675
Chris Lattner13651f02003-09-03 17:56:43 +0000676 printType(F->getReturnType()) << " ";
Chris Lattner5b337482003-10-18 05:57:43 +0000677 if (!F->getName().empty())
678 Out << getLLVMName(F->getName());
679 else
680 Out << "\"\"";
Chris Lattner13651f02003-09-03 17:56:43 +0000681 Out << "(";
Chris Lattner113f4f42002-06-25 16:13:24 +0000682 Table.incorporateFunction(F);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000683
Chris Lattner7bfee412001-10-29 16:05:51 +0000684 // Loop over the arguments, printing them...
Chris Lattner113f4f42002-06-25 16:13:24 +0000685 const FunctionType *FT = F->getFunctionType();
Chris Lattnerfee714f2001-09-07 16:36:04 +0000686
Chris Lattner149376d2002-10-13 20:57:00 +0000687 for(Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I)
688 printArgument(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000689
690 // Finish printing arguments...
Chris Lattner113f4f42002-06-25 16:13:24 +0000691 if (FT->isVarArg()) {
692 if (FT->getParamTypes().size()) Out << ", ";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000693 Out << "..."; // Output varargs portion of signature!
694 }
Chris Lattnerb2f02e52002-05-06 03:00:40 +0000695 Out << ")";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000696
Chris Lattner113f4f42002-06-25 16:13:24 +0000697 if (F->isExternal()) {
Chris Lattnerb2f02e52002-05-06 03:00:40 +0000698 Out << "\n";
699 } else {
700 Out << " {";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000701
Chris Lattner6915f8f2002-04-07 22:49:37 +0000702 // Output all of its basic blocks... for the function
Chris Lattner113f4f42002-06-25 16:13:24 +0000703 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
704 printBasicBlock(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000705
Chris Lattnerb2f02e52002-05-06 03:00:40 +0000706 Out << "}\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000707 }
708
Chris Lattner6915f8f2002-04-07 22:49:37 +0000709 Table.purgeFunction();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000710}
711
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000712// printArgument - This member is called for every argument that
Chris Lattner6915f8f2002-04-07 22:49:37 +0000713// is passed into the function. Simply print it out
Chris Lattner2f7c9632001-06-06 20:29:01 +0000714//
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000715void AssemblyWriter::printArgument(const Argument *Arg) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000716 // Insert commas as we go... the first arg doesn't get a comma
Chris Lattner113f4f42002-06-25 16:13:24 +0000717 if (Arg != &Arg->getParent()->afront()) Out << ", ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000718
719 // Output type...
Chris Lattner7bfee412001-10-29 16:05:51 +0000720 printType(Arg->getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000721
722 // Output name, if available...
723 if (Arg->hasName())
Chris Lattner1c343042003-08-22 05:40:38 +0000724 Out << " " << getLLVMName(Arg->getName());
Alkis Evlogimenos8faf8d92003-10-17 02:02:40 +0000725 else if (Table.getSlot(Arg) < 0)
Chris Lattner2f7c9632001-06-06 20:29:01 +0000726 Out << "<badref>";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000727}
728
Misha Brukmanfa100532003-10-10 17:54:14 +0000729// printBasicBlock - This member is called for each basic block in a method.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000730//
Chris Lattner7bfee412001-10-29 16:05:51 +0000731void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000732 if (BB->hasName()) { // Print out the label if it exists...
Chris Lattner58185f22002-10-02 19:38:55 +0000733 Out << "\n" << BB->getName() << ":";
Chris Lattner408dbdb2002-05-14 16:02:05 +0000734 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Alkis Evlogimenos8faf8d92003-10-17 02:02:40 +0000735 int Slot = Table.getSlot(BB);
Chris Lattnera2f01872001-06-07 16:58:55 +0000736 Out << "\n; <label>:";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000737 if (Slot >= 0)
Misha Brukman7fdaab42003-07-14 17:20:40 +0000738 Out << Slot; // Extra newline separates out label's
Chris Lattner2f7c9632001-06-06 20:29:01 +0000739 else
Chris Lattnera2f01872001-06-07 16:58:55 +0000740 Out << "<badref>";
Chris Lattner58185f22002-10-02 19:38:55 +0000741 }
Chris Lattner2447ef52003-11-20 00:09:43 +0000742
743 if (BB->getParent() == 0)
744 Out << "\t\t; Error: Block without parent!";
745 else {
746 if (BB != &BB->getParent()->front()) { // Not the entry block?
747 // Output predecessors for the block...
748 Out << "\t\t;";
749 pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
750
751 if (PI == PE) {
752 Out << " No predecessors!";
753 } else {
754 Out << " preds =";
Chris Lattner00211f12003-11-16 22:59:57 +0000755 writeOperand(*PI, false, true);
Chris Lattner2447ef52003-11-20 00:09:43 +0000756 for (++PI; PI != PE; ++PI) {
757 Out << ",";
758 writeOperand(*PI, false, true);
759 }
Chris Lattner00211f12003-11-16 22:59:57 +0000760 }
Chris Lattner58185f22002-10-02 19:38:55 +0000761 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000762 }
Chris Lattner408dbdb2002-05-14 16:02:05 +0000763
764 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000765
Chris Lattner8339f7d2003-10-30 23:41:03 +0000766 if (AnnotationWriter) AnnotationWriter->emitBasicBlockAnnot(BB, Out);
767
Chris Lattnerfee714f2001-09-07 16:36:04 +0000768 // Output all of the instructions in the basic block...
Chris Lattner113f4f42002-06-25 16:13:24 +0000769 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
770 printInstruction(*I);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000771}
772
Chris Lattner862e3382001-10-13 06:42:36 +0000773
774// printInfoComment - Print a little comment after the instruction indicating
775// which slot it occupies.
776//
Chris Lattner113f4f42002-06-25 16:13:24 +0000777void AssemblyWriter::printInfoComment(const Value &V) {
778 if (V.getType() != Type::VoidTy) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000779 Out << "\t\t; <";
Chris Lattner113f4f42002-06-25 16:13:24 +0000780 printType(V.getType()) << ">";
Chris Lattner862e3382001-10-13 06:42:36 +0000781
Chris Lattner113f4f42002-06-25 16:13:24 +0000782 if (!V.hasName()) {
Alkis Evlogimenos8faf8d92003-10-17 02:02:40 +0000783 int Slot = Table.getSlot(&V); // Print out the def slot taken...
Chris Lattner862e3382001-10-13 06:42:36 +0000784 if (Slot >= 0) Out << ":" << Slot;
785 else Out << ":<badref>";
786 }
Chris Lattner113f4f42002-06-25 16:13:24 +0000787 Out << " [#uses=" << V.use_size() << "]"; // Output # uses
Chris Lattner862e3382001-10-13 06:42:36 +0000788 }
789}
790
Misha Brukmanfa100532003-10-10 17:54:14 +0000791// printInstruction - This member is called for each Instruction in a method.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000792//
Chris Lattner113f4f42002-06-25 16:13:24 +0000793void AssemblyWriter::printInstruction(const Instruction &I) {
Chris Lattner8339f7d2003-10-30 23:41:03 +0000794 if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out);
795
Chris Lattner2f7c9632001-06-06 20:29:01 +0000796 Out << "\t";
797
798 // Print out name if it exists...
Chris Lattner113f4f42002-06-25 16:13:24 +0000799 if (I.hasName())
Chris Lattner1c343042003-08-22 05:40:38 +0000800 Out << getLLVMName(I.getName()) << " = ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000801
Chris Lattner504f9242003-09-08 17:45:59 +0000802 // If this is a volatile load or store, print out the volatile marker
803 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
804 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile()))
805 Out << "volatile ";
806
Chris Lattner2f7c9632001-06-06 20:29:01 +0000807 // Print out the opcode...
Chris Lattner113f4f42002-06-25 16:13:24 +0000808 Out << I.getOpcodeName();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000809
810 // Print out the type of the operands...
Chris Lattner113f4f42002-06-25 16:13:24 +0000811 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000812
813 // Special case conditional branches to swizzle the condition out to the front
Chris Lattner113f4f42002-06-25 16:13:24 +0000814 if (isa<BranchInst>(I) && I.getNumOperands() > 1) {
815 writeOperand(I.getOperand(2), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000816 Out << ",";
817 writeOperand(Operand, true);
818 Out << ",";
Chris Lattner113f4f42002-06-25 16:13:24 +0000819 writeOperand(I.getOperand(1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000820
Chris Lattner8d48df22002-04-13 18:34:38 +0000821 } else if (isa<SwitchInst>(I)) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000822 // Special case switch statement to get formatting nice and correct...
Chris Lattner113f4f42002-06-25 16:13:24 +0000823 writeOperand(Operand , true); Out << ",";
824 writeOperand(I.getOperand(1), true); Out << " [";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000825
Chris Lattner113f4f42002-06-25 16:13:24 +0000826 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; op += 2) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000827 Out << "\n\t\t";
Chris Lattner113f4f42002-06-25 16:13:24 +0000828 writeOperand(I.getOperand(op ), true); Out << ",";
829 writeOperand(I.getOperand(op+1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000830 }
831 Out << "\n\t]";
Chris Lattnerda558102001-10-02 03:41:24 +0000832 } else if (isa<PHINode>(I)) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000833 Out << " ";
Chris Lattner113f4f42002-06-25 16:13:24 +0000834 printType(I.getType());
Chris Lattner9d3292d2001-11-06 08:33:46 +0000835 Out << " ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000836
Chris Lattner113f4f42002-06-25 16:13:24 +0000837 for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) {
Chris Lattner29644b32001-11-06 01:48:45 +0000838 if (op) Out << ", ";
839 Out << "[";
Chris Lattner113f4f42002-06-25 16:13:24 +0000840 writeOperand(I.getOperand(op ), false); Out << ",";
841 writeOperand(I.getOperand(op+1), false); Out << " ]";
Chris Lattner931ef3b2001-06-11 15:04:20 +0000842 }
Chris Lattner862e3382001-10-13 06:42:36 +0000843 } else if (isa<ReturnInst>(I) && !Operand) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000844 Out << " void";
Chris Lattner862e3382001-10-13 06:42:36 +0000845 } else if (isa<CallInst>(I)) {
Chris Lattner463d6a52003-08-05 15:34:45 +0000846 const PointerType *PTy = cast<PointerType>(Operand->getType());
847 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
848 const Type *RetTy = FTy->getReturnType();
Chris Lattner2f2d9472001-11-06 21:28:12 +0000849
Chris Lattner463d6a52003-08-05 15:34:45 +0000850 // If possible, print out the short form of the call instruction. We can
Chris Lattner6915f8f2002-04-07 22:49:37 +0000851 // only do this if the first argument is a pointer to a nonvararg function,
Chris Lattner463d6a52003-08-05 15:34:45 +0000852 // and if the return type is not a pointer to a function.
Chris Lattner2f2d9472001-11-06 21:28:12 +0000853 //
Chris Lattner463d6a52003-08-05 15:34:45 +0000854 if (!FTy->isVarArg() &&
Chris Lattner8d48df22002-04-13 18:34:38 +0000855 (!isa<PointerType>(RetTy) ||
Chris Lattnerd9a36a62002-07-25 20:58:51 +0000856 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Chris Lattner2f2d9472001-11-06 21:28:12 +0000857 Out << " "; printType(RetTy);
858 writeOperand(Operand, false);
859 } else {
860 writeOperand(Operand, true);
861 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000862 Out << "(";
Chris Lattner113f4f42002-06-25 16:13:24 +0000863 if (I.getNumOperands() > 1) writeOperand(I.getOperand(1), true);
864 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; ++op) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000865 Out << ",";
Chris Lattner113f4f42002-06-25 16:13:24 +0000866 writeOperand(I.getOperand(op), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000867 }
868
869 Out << " )";
Chris Lattner113f4f42002-06-25 16:13:24 +0000870 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Chris Lattner463d6a52003-08-05 15:34:45 +0000871 const PointerType *PTy = cast<PointerType>(Operand->getType());
872 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
873 const Type *RetTy = FTy->getReturnType();
874
875 // If possible, print out the short form of the invoke instruction. We can
876 // only do this if the first argument is a pointer to a nonvararg function,
877 // and if the return type is not a pointer to a function.
878 //
879 if (!FTy->isVarArg() &&
880 (!isa<PointerType>(RetTy) ||
881 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
882 Out << " "; printType(RetTy);
883 writeOperand(Operand, false);
884 } else {
885 writeOperand(Operand, true);
886 }
887
Chris Lattner862e3382001-10-13 06:42:36 +0000888 Out << "(";
Chris Lattner113f4f42002-06-25 16:13:24 +0000889 if (I.getNumOperands() > 3) writeOperand(I.getOperand(3), true);
890 for (unsigned op = 4, Eop = I.getNumOperands(); op < Eop; ++op) {
Chris Lattner862e3382001-10-13 06:42:36 +0000891 Out << ",";
Chris Lattner113f4f42002-06-25 16:13:24 +0000892 writeOperand(I.getOperand(op), true);
Chris Lattner862e3382001-10-13 06:42:36 +0000893 }
894
895 Out << " )\n\t\t\tto";
896 writeOperand(II->getNormalDest(), true);
897 Out << " except";
Chris Lattnerfae8ab32004-02-08 21:44:31 +0000898 writeOperand(II->getUnwindDest(), true);
Chris Lattner862e3382001-10-13 06:42:36 +0000899
Chris Lattner113f4f42002-06-25 16:13:24 +0000900 } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(&I)) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000901 Out << " ";
Chris Lattner8d48df22002-04-13 18:34:38 +0000902 printType(AI->getType()->getElementType());
903 if (AI->isArrayAllocation()) {
Chris Lattnera073acb2001-07-07 08:36:50 +0000904 Out << ",";
Chris Lattner8d48df22002-04-13 18:34:38 +0000905 writeOperand(AI->getArraySize(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000906 }
Chris Lattner862e3382001-10-13 06:42:36 +0000907 } else if (isa<CastInst>(I)) {
Chris Lattnerc96e96b2003-11-17 01:17:04 +0000908 if (Operand) writeOperand(Operand, true); // Work with broken code
Chris Lattner7bfee412001-10-29 16:05:51 +0000909 Out << " to ";
Chris Lattner113f4f42002-06-25 16:13:24 +0000910 printType(I.getType());
Chris Lattner5b337482003-10-18 05:57:43 +0000911 } else if (isa<VAArgInst>(I)) {
Chris Lattnerc96e96b2003-11-17 01:17:04 +0000912 if (Operand) writeOperand(Operand, true); // Work with broken code
Chris Lattnerf70da102003-05-08 02:44:12 +0000913 Out << ", ";
914 printType(I.getType());
Chris Lattner5b337482003-10-18 05:57:43 +0000915 } else if (const VANextInst *VAN = dyn_cast<VANextInst>(&I)) {
Chris Lattnerc96e96b2003-11-17 01:17:04 +0000916 if (Operand) writeOperand(Operand, true); // Work with broken code
Chris Lattner5b337482003-10-18 05:57:43 +0000917 Out << ", ";
918 printType(VAN->getArgType());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000919 } else if (Operand) { // Print the normal way...
920
921 // PrintAllTypes - Instructions who have operands of all the same type
922 // omit the type from all but the first operand. If the instruction has
923 // different type operands (for example br), then they are all printed.
924 bool PrintAllTypes = false;
925 const Type *TheType = Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000926
Chris Lattnerdeccfaf2003-04-16 20:20:02 +0000927 // Shift Left & Right print both types even for Ubyte LHS
928 if (isa<ShiftInst>(I)) {
929 PrintAllTypes = true;
930 } else {
931 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
932 Operand = I.getOperand(i);
933 if (Operand->getType() != TheType) {
934 PrintAllTypes = true; // We have differing types! Print them all!
935 break;
936 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000937 }
938 }
Chris Lattnerdeccfaf2003-04-16 20:20:02 +0000939
Chris Lattner7bfee412001-10-29 16:05:51 +0000940 if (!PrintAllTypes) {
941 Out << " ";
Chris Lattnerdeccfaf2003-04-16 20:20:02 +0000942 printType(TheType);
Chris Lattner7bfee412001-10-29 16:05:51 +0000943 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000944
Chris Lattner113f4f42002-06-25 16:13:24 +0000945 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000946 if (i) Out << ",";
Chris Lattner113f4f42002-06-25 16:13:24 +0000947 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000948 }
949 }
950
Chris Lattner862e3382001-10-13 06:42:36 +0000951 printInfoComment(I);
Chris Lattner7f74a562002-01-20 22:54:45 +0000952 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000953}
954
955
956//===----------------------------------------------------------------------===//
957// External Interface declarations
958//===----------------------------------------------------------------------===//
959
Chris Lattner8339f7d2003-10-30 23:41:03 +0000960void Module::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Chris Lattnerf78819d2004-01-18 21:03:06 +0000961 SlotCalculator SlotTable(this, false);
Chris Lattner8339f7d2003-10-30 23:41:03 +0000962 AssemblyWriter W(o, SlotTable, this, AAW);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000963 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000964}
965
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000966void GlobalVariable::print(std::ostream &o) const {
Chris Lattnerf78819d2004-01-18 21:03:06 +0000967 SlotCalculator SlotTable(getParent(), false);
Chris Lattner8339f7d2003-10-30 23:41:03 +0000968 AssemblyWriter W(o, SlotTable, getParent(), 0);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000969 W.write(this);
Chris Lattneradfe0d12001-09-10 20:08:19 +0000970}
971
Chris Lattner8339f7d2003-10-30 23:41:03 +0000972void Function::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Chris Lattnerf78819d2004-01-18 21:03:06 +0000973 SlotCalculator SlotTable(getParent(), false);
Chris Lattner8339f7d2003-10-30 23:41:03 +0000974 AssemblyWriter W(o, SlotTable, getParent(), AAW);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000975
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000976 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000977}
978
Chris Lattner8339f7d2003-10-30 23:41:03 +0000979void BasicBlock::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Chris Lattnerf78819d2004-01-18 21:03:06 +0000980 SlotCalculator SlotTable(getParent(), false);
Chris Lattner7bfee412001-10-29 16:05:51 +0000981 AssemblyWriter W(o, SlotTable,
Chris Lattner8339f7d2003-10-30 23:41:03 +0000982 getParent() ? getParent()->getParent() : 0, AAW);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000983 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000984}
985
Chris Lattner8339f7d2003-10-30 23:41:03 +0000986void Instruction::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000987 const Function *F = getParent() ? getParent()->getParent() : 0;
Chris Lattnerf78819d2004-01-18 21:03:06 +0000988 SlotCalculator SlotTable(F, false);
Chris Lattner8339f7d2003-10-30 23:41:03 +0000989 AssemblyWriter W(o, SlotTable, F ? F->getParent() : 0, AAW);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000990
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000991 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000992}
Chris Lattner7db79582001-11-07 04:21:57 +0000993
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000994void Constant::print(std::ostream &o) const {
995 if (this == 0) { o << "<null> constant value\n"; return; }
Chris Lattner7d734802002-09-10 15:53:49 +0000996
997 // Handle CPR's special, because they have context information...
998 if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(this)) {
999 CPR->getValue()->print(o); // Print as a global value, with context info.
1000 return;
1001 }
1002
Chris Lattner1e194682002-04-18 18:53:13 +00001003 o << " " << getType()->getDescription() << " ";
1004
Chris Lattnerab7d1ab2003-05-08 02:08:14 +00001005 std::map<const Type *, std::string> TypeTable;
Chris Lattner1e194682002-04-18 18:53:13 +00001006 WriteConstantInt(o, this, false, TypeTable, 0);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001007}
1008
1009void Type::print(std::ostream &o) const {
1010 if (this == 0)
1011 o << "<null Type>";
1012 else
1013 o << getDescription();
1014}
1015
Chris Lattner2e9fa6d2002-04-09 19:48:49 +00001016void Argument::print(std::ostream &o) const {
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001017 o << getType() << " " << getName();
1018}
1019
1020void Value::dump() const { print(std::cerr); }
1021
1022//===----------------------------------------------------------------------===//
1023// CachedWriter Class Implementation
1024//===----------------------------------------------------------------------===//
1025
Chris Lattner7db79582001-11-07 04:21:57 +00001026void CachedWriter::setModule(const Module *M) {
1027 delete SC; delete AW;
1028 if (M) {
Chris Lattner6089bb12004-01-14 02:49:34 +00001029 SC = new SlotCalculator(M, false);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001030 AW = new AssemblyWriter(Out, *SC, M, 0);
Chris Lattner7db79582001-11-07 04:21:57 +00001031 } else {
1032 SC = 0; AW = 0;
1033 }
1034}
1035
1036CachedWriter::~CachedWriter() {
1037 delete AW;
1038 delete SC;
1039}
1040
1041CachedWriter &CachedWriter::operator<<(const Value *V) {
1042 assert(AW && SC && "CachedWriter does not have a current module!");
1043 switch (V->getValueType()) {
1044 case Value::ConstantVal:
Chris Lattner1e194682002-04-18 18:53:13 +00001045 case Value::ArgumentVal: AW->writeOperand(V, true, true); break;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +00001046 case Value::TypeVal: AW->write(cast<Type>(V)); break;
Chris Lattner7db79582001-11-07 04:21:57 +00001047 case Value::InstructionVal: AW->write(cast<Instruction>(V)); break;
1048 case Value::BasicBlockVal: AW->write(cast<BasicBlock>(V)); break;
Chris Lattner57698e22002-03-26 18:01:55 +00001049 case Value::FunctionVal: AW->write(cast<Function>(V)); break;
Chris Lattner7db79582001-11-07 04:21:57 +00001050 case Value::GlobalVariableVal: AW->write(cast<GlobalVariable>(V)); break;
Chris Lattner7db79582001-11-07 04:21:57 +00001051 default: Out << "<unknown value type: " << V->getValueType() << ">"; break;
1052 }
1053 return *this;
1054}