1 //===-- Writer.cpp - Library for Printing VM assembly files ------*- C++ -*--=//
3 // This library implements the functionality defined in llvm/Assembly/Writer.h
5 // This library uses the Analysis library to figure out offsets for
6 // variables in the method tables...
8 // TODO: print out the type name instead of the full type if a particular type
9 // is in the symbol table...
11 //===----------------------------------------------------------------------===//
13 #include "llvm/Assembly/CachedWriter.h"
14 #include "llvm/Analysis/SlotCalculator.h"
15 #include "llvm/Module.h"
16 #include "llvm/Function.h"
17 #include "llvm/GlobalVariable.h"
18 #include "llvm/BasicBlock.h"
19 #include "llvm/ConstantVals.h"
20 #include "llvm/iMemory.h"
21 #include "llvm/iTerminators.h"
22 #include "llvm/iPHINode.h"
23 #include "llvm/iOther.h"
24 #include "llvm/SymbolTable.h"
25 #include "Support/StringExtras.h"
26 #include "Support/STLExtras.h"
34 static const Module *getModuleFromVal(const Value *V) {
35 if (const FunctionArgument *MA = dyn_cast<const FunctionArgument>(V))
36 return MA->getParent() ? MA->getParent()->getParent() : 0;
37 else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(V))
38 return BB->getParent() ? BB->getParent()->getParent() : 0;
39 else if (const Instruction *I = dyn_cast<const Instruction>(V)) {
40 const Function *M = I->getParent() ? I->getParent()->getParent() : 0;
41 return M ? M->getParent() : 0;
42 } else if (const GlobalValue *GV = dyn_cast<const GlobalValue>(V))
43 return GV->getParent();
44 else if (const Module *Mod = dyn_cast<const Module>(V))
49 static SlotCalculator *createSlotCalculator(const Value *V) {
50 assert(!isa<Type>(V) && "Can't create an SC for a type!");
51 if (const FunctionArgument *FA = dyn_cast<const FunctionArgument>(V)) {
52 return new SlotCalculator(FA->getParent(), true);
53 } else if (const Instruction *I = dyn_cast<const Instruction>(V)) {
54 return new SlotCalculator(I->getParent()->getParent(), true);
55 } else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(V)) {
56 return new SlotCalculator(BB->getParent(), true);
57 } else if (const GlobalVariable *GV = dyn_cast<const GlobalVariable>(V)){
58 return new SlotCalculator(GV->getParent(), true);
59 } else if (const Function *Func = dyn_cast<const Function>(V)) {
60 return new SlotCalculator(Func, true);
61 } else if (const Module *Mod = dyn_cast<const Module>(V)) {
62 return new SlotCalculator(Mod, true);
67 // WriteAsOperand - Write the name of the specified value out to the specified
68 // ostream. This can be useful when you just want to print int %reg126, not the
69 // whole instruction that generated it.
71 static void WriteAsOperandInternal(ostream &Out, const Value *V, bool PrintName,
72 SlotCalculator *Table) {
73 if (PrintName && V->hasName()) {
74 Out << " %" << V->getName();
76 if (const Constant *CPV = dyn_cast<const Constant>(V)) {
77 Out << " " << CPV->getStrValue();
81 Slot = Table->getValSlot(V);
83 if (const Type *Ty = dyn_cast<const Type>(V)) {
84 Out << " " << Ty->getDescription();
88 Table = createSlotCalculator(V);
89 if (Table == 0) { Out << "BAD VALUE TYPE!"; return; }
91 Slot = Table->getValSlot(V);
94 if (Slot >= 0) Out << " %" << Slot;
96 Out << "<badref>"; // Not embeded into a location?
102 // If the module has a symbol table, take all global types and stuff their
103 // names into the TypeNames map.
105 static void fillTypeNameTable(const Module *M,
106 map<const Type *, string> &TypeNames) {
107 if (M && M->hasSymbolTable()) {
108 const SymbolTable *ST = M->getSymbolTable();
109 SymbolTable::const_iterator PI = ST->find(Type::TypeTy);
110 if (PI != ST->end()) {
111 SymbolTable::type_const_iterator I = PI->second.begin();
112 for (; I != PI->second.end(); ++I) {
113 // As a heuristic, don't insert pointer to primitive types, because
114 // they are used too often to have a single useful name.
116 const Type *Ty = cast<const Type>(I->second);
117 if (!isa<PointerType>(Ty) ||
118 !cast<PointerType>(Ty)->getElementType()->isPrimitiveType())
119 TypeNames.insert(std::make_pair(Ty, "%"+I->first));
127 static string calcTypeName(const Type *Ty, vector<const Type *> &TypeStack,
128 map<const Type *, string> &TypeNames) {
129 if (Ty->isPrimitiveType()) return Ty->getDescription(); // Base case
131 // Check to see if the type is named.
132 map<const Type *, string>::iterator I = TypeNames.find(Ty);
133 if (I != TypeNames.end()) return I->second;
135 // Check to see if the Type is already on the stack...
136 unsigned Slot = 0, CurSize = TypeStack.size();
137 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
139 // This is another base case for the recursion. In this case, we know
140 // that we have looped back to a type that we have previously visited.
141 // Generate the appropriate upreference to handle this.
144 return "\\" + utostr(CurSize-Slot); // Here's the upreference
146 TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
149 switch (Ty->getPrimitiveID()) {
150 case Type::FunctionTyID: {
151 const FunctionType *MTy = cast<const FunctionType>(Ty);
152 Result = calcTypeName(MTy->getReturnType(), TypeStack, TypeNames) + " (";
153 for (FunctionType::ParamTypes::const_iterator
154 I = MTy->getParamTypes().begin(),
155 E = MTy->getParamTypes().end(); I != E; ++I) {
156 if (I != MTy->getParamTypes().begin())
158 Result += calcTypeName(*I, TypeStack, TypeNames);
160 if (MTy->isVarArg()) {
161 if (!MTy->getParamTypes().empty()) Result += ", ";
167 case Type::StructTyID: {
168 const StructType *STy = cast<const StructType>(Ty);
170 for (StructType::ElementTypes::const_iterator
171 I = STy->getElementTypes().begin(),
172 E = STy->getElementTypes().end(); I != E; ++I) {
173 if (I != STy->getElementTypes().begin())
175 Result += calcTypeName(*I, TypeStack, TypeNames);
180 case Type::PointerTyID:
181 Result = calcTypeName(cast<const PointerType>(Ty)->getElementType(),
182 TypeStack, TypeNames) + " *";
184 case Type::ArrayTyID: {
185 const ArrayType *ATy = cast<const ArrayType>(Ty);
186 int NumElements = ATy->getNumElements();
188 if (NumElements != -1) Result += itostr(NumElements) + " x ";
189 Result += calcTypeName(ATy->getElementType(), TypeStack, TypeNames) + "]";
193 assert(0 && "Unhandled case in getTypeProps!");
197 TypeStack.pop_back(); // Remove self from stack...
202 // printTypeInt - The internal guts of printing out a type that has a
203 // potentially named portion.
205 static ostream &printTypeInt(ostream &Out, const Type *Ty,
206 map<const Type *, string> &TypeNames) {
207 // Primitive types always print out their description, regardless of whether
208 // they have been named or not.
210 if (Ty->isPrimitiveType()) return Out << Ty->getDescription();
212 // Check to see if the type is named.
213 map<const Type *, string>::iterator I = TypeNames.find(Ty);
214 if (I != TypeNames.end()) return Out << I->second;
216 // Otherwise we have a type that has not been named but is a derived type.
217 // Carefully recurse the type hierarchy to print out any contained symbolic
220 vector<const Type *> TypeStack;
221 string TypeName = calcTypeName(Ty, TypeStack, TypeNames);
222 TypeNames.insert(std::make_pair(Ty, TypeName));//Cache type name for later use
223 return Out << TypeName;
227 // WriteTypeSymbolic - This attempts to write the specified type as a symbolic
228 // type, iff there is an entry in the modules symbol table for the specified
229 // type or one of it's component types. This is slower than a simple x << Type;
231 ostream &WriteTypeSymbolic(ostream &Out, const Type *Ty, const Module *M) {
234 // If they want us to print out a type, attempt to make it symbolic if there
235 // is a symbol table in the module...
236 if (M && M->hasSymbolTable()) {
237 map<const Type *, string> TypeNames;
238 fillTypeNameTable(M, TypeNames);
240 return printTypeInt(Out, Ty, TypeNames);
242 return Out << Ty->getDescription();
247 // WriteAsOperand - Write the name of the specified value out to the specified
248 // ostream. This can be useful when you just want to print int %reg126, not the
249 // whole instruction that generated it.
251 ostream &WriteAsOperand(ostream &Out, const Value *V, bool PrintType,
252 bool PrintName, SlotCalculator *Table) {
254 WriteTypeSymbolic(Out, V->getType(), getModuleFromVal(V));
256 WriteAsOperandInternal(Out, V, PrintName, Table);
262 class AssemblyWriter {
264 SlotCalculator &Table;
265 const Module *TheModule;
266 map<const Type *, string> TypeNames;
268 inline AssemblyWriter(ostream &o, SlotCalculator &Tab, const Module *M)
269 : Out(o), Table(Tab), TheModule(M) {
271 // If the module has a symbol table, take all global types and stuff their
272 // names into the TypeNames map.
274 fillTypeNameTable(M, TypeNames);
277 inline void write(const Module *M) { printModule(M); }
278 inline void write(const GlobalVariable *G) { printGlobal(G); }
279 inline void write(const Function *F) { printFunction(F); }
280 inline void write(const BasicBlock *BB) { printBasicBlock(BB); }
281 inline void write(const Instruction *I) { printInstruction(I); }
282 inline void write(const Constant *CPV) { printConstant(CPV); }
283 inline void write(const Type *Ty) { printType(Ty); }
286 void printModule(const Module *M);
287 void printSymbolTable(const SymbolTable &ST);
288 void printConstant(const Constant *CPV);
289 void printGlobal(const GlobalVariable *GV);
290 void printFunction(const Function *F);
291 void printFunctionArgument(const FunctionArgument *FA);
292 void printBasicBlock(const BasicBlock *BB);
293 void printInstruction(const Instruction *I);
294 ostream &printType(const Type *Ty);
296 void writeOperand(const Value *Op, bool PrintType, bool PrintName = true);
298 // printInfoComment - Print a little comment after the instruction indicating
299 // which slot it occupies.
300 void printInfoComment(const Value *V);
304 void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType,
306 if (PrintType) { Out << " "; printType(Operand->getType()); }
307 WriteAsOperandInternal(Out, Operand, PrintName, &Table);
311 void AssemblyWriter::printModule(const Module *M) {
312 // Loop over the symbol table, emitting all named constants...
313 if (M->hasSymbolTable())
314 printSymbolTable(*M->getSymbolTable());
316 for_each(M->gbegin(), M->gend(),
317 bind_obj(this, &AssemblyWriter::printGlobal));
319 Out << "implementation\n";
321 // Output all of the methods...
322 for_each(M->begin(), M->end(), bind_obj(this,&AssemblyWriter::printFunction));
325 void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
326 if (GV->hasName()) Out << "%" << GV->getName() << " = ";
328 if (GV->hasInternalLinkage()) Out << "internal ";
329 if (!GV->hasInitializer()) Out << "uninitialized ";
331 Out << (GV->isConstant() ? "constant " : "global ");
332 printType(GV->getType()->getElementType());
334 if (GV->hasInitializer())
335 writeOperand(GV->getInitializer(), false, false);
337 printInfoComment(GV);
342 // printSymbolTable - Run through symbol table looking for named constants
343 // if a named constant is found, emit it's declaration...
345 void AssemblyWriter::printSymbolTable(const SymbolTable &ST) {
346 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
347 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
348 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
350 for (; I != End; ++I) {
351 const Value *V = I->second;
352 if (const Constant *CPV = dyn_cast<const Constant>(V)) {
354 } else if (const Type *Ty = dyn_cast<const Type>(V)) {
355 Out << "\t%" << I->first << " = type " << Ty->getDescription() << "\n";
362 // printConstant - Print out a constant pool entry...
364 void AssemblyWriter::printConstant(const Constant *CPV) {
365 // Don't print out unnamed constants, they will be inlined
366 if (!CPV->hasName()) return;
369 Out << "\t%" << CPV->getName() << " = ";
371 // Print out the constant type...
372 printType(CPV->getType());
374 // Write the value out now...
375 writeOperand(CPV, false, false);
377 if (!CPV->hasName() && CPV->getType() != Type::VoidTy) {
378 int Slot = Table.getValSlot(CPV); // Print out the def slot taken...
380 printType(CPV->getType()) << ">:";
381 if (Slot >= 0) Out << Slot;
382 else Out << "<badref>";
388 // printFunction - Print all aspects of a method.
390 void AssemblyWriter::printFunction(const Function *M) {
391 // Print out the return type and name...
392 Out << "\n" << (M->isExternal() ? "declare " : "")
393 << (M->hasInternalLinkage() ? "internal " : "");
394 printType(M->getReturnType()) << " \"" << M->getName() << "\"(";
395 Table.incorporateMethod(M);
397 // Loop over the arguments, printing them...
398 const FunctionType *MT = M->getFunctionType();
400 if (!M->isExternal()) {
401 for_each(M->getArgumentList().begin(), M->getArgumentList().end(),
402 bind_obj(this, &AssemblyWriter::printFunctionArgument));
404 // Loop over the arguments, printing them...
405 const FunctionType *MT = M->getFunctionType();
406 for (FunctionType::ParamTypes::const_iterator I = MT->getParamTypes().begin(),
407 E = MT->getParamTypes().end(); I != E; ++I) {
408 if (I != MT->getParamTypes().begin()) Out << ", ";
413 // Finish printing arguments...
414 if (MT->isVarArg()) {
415 if (MT->getParamTypes().size()) Out << ", ";
416 Out << "..."; // Output varargs portion of signature!
420 if (!M->isExternal()) {
421 // Loop over the symbol table, emitting all named constants...
422 if (M->hasSymbolTable())
423 printSymbolTable(*M->getSymbolTable());
427 // Output all of its basic blocks... for the method
428 for_each(M->begin(), M->end(),
429 bind_obj(this, &AssemblyWriter::printBasicBlock));
437 // printFunctionArgument - This member is called for every argument that
438 // is passed into the method. Simply print it out
440 void AssemblyWriter::printFunctionArgument(const FunctionArgument *Arg) {
441 // Insert commas as we go... the first arg doesn't get a comma
442 if (Arg != Arg->getParent()->getArgumentList().front()) Out << ", ";
445 printType(Arg->getType());
447 // Output name, if available...
449 Out << " %" << Arg->getName();
450 else if (Table.getValSlot(Arg) < 0)
454 // printBasicBlock - This member is called for each basic block in a methd.
456 void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
457 if (BB->hasName()) { // Print out the label if it exists...
458 Out << "\n" << BB->getName() << ":";
460 int Slot = Table.getValSlot(BB);
461 Out << "\n; <label>:";
463 Out << Slot; // Extra newline seperates out label's
467 Out << "\t\t\t\t\t;[#uses=" << BB->use_size() << "]\n"; // Output # uses
469 // Output all of the instructions in the basic block...
470 for_each(BB->begin(), BB->end(),
471 bind_obj(this, &AssemblyWriter::printInstruction));
475 // printInfoComment - Print a little comment after the instruction indicating
476 // which slot it occupies.
478 void AssemblyWriter::printInfoComment(const Value *V) {
479 if (V->getType() != Type::VoidTy) {
481 printType(V->getType()) << ">";
484 int Slot = Table.getValSlot(V); // Print out the def slot taken...
485 if (Slot >= 0) Out << ":" << Slot;
486 else Out << ":<badref>";
488 Out << " [#uses=" << V->use_size() << "]"; // Output # uses
492 // printInstruction - This member is called for each Instruction in a methd.
494 void AssemblyWriter::printInstruction(const Instruction *I) {
497 // Print out name if it exists...
498 if (I && I->hasName())
499 Out << "%" << I->getName() << " = ";
501 // Print out the opcode...
502 Out << I->getOpcodeName();
504 // Print out the type of the operands...
505 const Value *Operand = I->getNumOperands() ? I->getOperand(0) : 0;
507 // Special case conditional branches to swizzle the condition out to the front
508 if (I->getOpcode() == Instruction::Br && I->getNumOperands() > 1) {
509 writeOperand(I->getOperand(2), true);
511 writeOperand(Operand, true);
513 writeOperand(I->getOperand(1), true);
515 } else if (I->getOpcode() == Instruction::Switch) {
516 // Special case switch statement to get formatting nice and correct...
517 writeOperand(Operand , true); Out << ",";
518 writeOperand(I->getOperand(1), true); Out << " [";
520 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; op += 2) {
522 writeOperand(I->getOperand(op ), true); Out << ",";
523 writeOperand(I->getOperand(op+1), true);
526 } else if (isa<PHINode>(I)) {
528 printType(I->getType());
531 for (unsigned op = 0, Eop = I->getNumOperands(); op < Eop; op += 2) {
534 writeOperand(I->getOperand(op ), false); Out << ",";
535 writeOperand(I->getOperand(op+1), false); Out << " ]";
537 } else if (isa<ReturnInst>(I) && !Operand) {
539 } else if (isa<CallInst>(I)) {
540 const PointerType *PTy = dyn_cast<PointerType>(Operand->getType());
541 const FunctionType*MTy = PTy ? dyn_cast<FunctionType>(PTy->getElementType()):0;
542 const Type *RetTy = MTy ? MTy->getReturnType() : 0;
544 // If possible, print out the short form of the call instruction, but we can
545 // only do this if the first argument is a pointer to a nonvararg method,
546 // and if the value returned is not a pointer to a method.
548 if (RetTy && !MTy->isVarArg() &&
549 (!isa<PointerType>(RetTy)||!isa<FunctionType>(cast<PointerType>(RetTy)))){
550 Out << " "; printType(RetTy);
551 writeOperand(Operand, false);
553 writeOperand(Operand, true);
556 if (I->getNumOperands() > 1) writeOperand(I->getOperand(1), true);
557 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; ++op) {
559 writeOperand(I->getOperand(op), true);
563 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(I)) {
564 // TODO: Should try to print out short form of the Invoke instruction
565 writeOperand(Operand, true);
567 if (I->getNumOperands() > 3) writeOperand(I->getOperand(3), true);
568 for (unsigned op = 4, Eop = I->getNumOperands(); op < Eop; ++op) {
570 writeOperand(I->getOperand(op), true);
573 Out << " )\n\t\t\tto";
574 writeOperand(II->getNormalDest(), true);
576 writeOperand(II->getExceptionalDest(), true);
578 } else if (I->getOpcode() == Instruction::Malloc ||
579 I->getOpcode() == Instruction::Alloca) {
581 printType(cast<const PointerType>(I->getType())->getElementType());
582 if (I->getNumOperands()) {
584 writeOperand(I->getOperand(0), true);
586 } else if (isa<CastInst>(I)) {
587 writeOperand(Operand, true);
589 printType(I->getType());
590 } else if (Operand) { // Print the normal way...
592 // PrintAllTypes - Instructions who have operands of all the same type
593 // omit the type from all but the first operand. If the instruction has
594 // different type operands (for example br), then they are all printed.
595 bool PrintAllTypes = false;
596 const Type *TheType = Operand->getType();
598 for (unsigned i = 1, E = I->getNumOperands(); i != E; ++i) {
599 Operand = I->getOperand(i);
600 if (Operand->getType() != TheType) {
601 PrintAllTypes = true; // We have differing types! Print them all!
606 // Shift Left & Right print both types even for Ubyte LHS
607 if (isa<ShiftInst>(I)) PrintAllTypes = true;
609 if (!PrintAllTypes) {
611 printType(I->getOperand(0)->getType());
614 for (unsigned i = 0, E = I->getNumOperands(); i != E; ++i) {
616 writeOperand(I->getOperand(i), PrintAllTypes);
625 // printType - Go to extreme measures to attempt to print out a short, symbolic
626 // version of a type name.
628 ostream &AssemblyWriter::printType(const Type *Ty) {
629 return printTypeInt(Out, Ty, TypeNames);
633 //===----------------------------------------------------------------------===//
634 // External Interface declarations
635 //===----------------------------------------------------------------------===//
639 void WriteToAssembly(const Module *M, ostream &o) {
640 if (M == 0) { o << "<null> module\n"; return; }
641 SlotCalculator SlotTable(M, true);
642 AssemblyWriter W(o, SlotTable, M);
647 void WriteToAssembly(const GlobalVariable *G, ostream &o) {
648 if (G == 0) { o << "<null> global variable\n"; return; }
649 SlotCalculator SlotTable(G->getParent(), true);
650 AssemblyWriter W(o, SlotTable, G->getParent());
654 void WriteToAssembly(const Function *F, ostream &o) {
655 if (F == 0) { o << "<null> function\n"; return; }
656 SlotCalculator SlotTable(F->getParent(), true);
657 AssemblyWriter W(o, SlotTable, F->getParent());
663 void WriteToAssembly(const BasicBlock *BB, ostream &o) {
664 if (BB == 0) { o << "<null> basic block\n"; return; }
666 SlotCalculator SlotTable(BB->getParent(), true);
667 AssemblyWriter W(o, SlotTable,
668 BB->getParent() ? BB->getParent()->getParent() : 0);
673 void WriteToAssembly(const Constant *CPV, ostream &o) {
674 if (CPV == 0) { o << "<null> constant pool value\n"; return; }
675 o << " " << CPV->getType()->getDescription() << " " << CPV->getStrValue();
678 void WriteToAssembly(const Instruction *I, ostream &o) {
679 if (I == 0) { o << "<null> instruction\n"; return; }
681 const Function *F = I->getParent() ? I->getParent()->getParent() : 0;
682 SlotCalculator SlotTable(F, true);
683 AssemblyWriter W(o, SlotTable, F ? F->getParent() : 0);
688 void CachedWriter::setModule(const Module *M) {
689 delete SC; delete AW;
691 SC = new SlotCalculator(M, true);
692 AW = new AssemblyWriter(Out, *SC, M);
698 CachedWriter::~CachedWriter() {
703 CachedWriter &CachedWriter::operator<<(const Value *V) {
704 assert(AW && SC && "CachedWriter does not have a current module!");
705 switch (V->getValueType()) {
706 case Value::ConstantVal:
707 Out << " "; AW->write(V->getType());
708 Out << " " << cast<Constant>(V)->getStrValue(); break;
709 case Value::FunctionArgumentVal:
710 AW->write(V->getType()); Out << " " << V->getName(); break;
711 case Value::TypeVal: AW->write(cast<const Type>(V)); break;
712 case Value::InstructionVal: AW->write(cast<Instruction>(V)); break;
713 case Value::BasicBlockVal: AW->write(cast<BasicBlock>(V)); break;
714 case Value::FunctionVal: AW->write(cast<Function>(V)); break;
715 case Value::GlobalVariableVal: AW->write(cast<GlobalVariable>(V)); break;
716 case Value::ModuleVal: AW->write(cast<Module>(V)); break;
717 default: Out << "<unknown value type: " << V->getValueType() << ">"; break;