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/Method.h"
17 #include "llvm/GlobalVariable.h"
18 #include "llvm/BasicBlock.h"
19 #include "llvm/ConstPoolVals.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"
30 static const Module *getModuleFromVal(const Value *V) {
31 if (const MethodArgument *MA =dyn_cast<const MethodArgument>(V))
32 return MA->getParent() ? MA->getParent()->getParent() : 0;
33 else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(V))
34 return BB->getParent() ? BB->getParent()->getParent() : 0;
35 else if (const Instruction *I = dyn_cast<const Instruction>(V)) {
36 const Method *M = I->getParent() ? I->getParent()->getParent() : 0;
37 return M ? M->getParent() : 0;
38 } else if (const GlobalValue *GV =dyn_cast<const GlobalValue>(V))
39 return GV->getParent();
40 else if (const Module *Mod = dyn_cast<const Module>(V))
45 static SlotCalculator *createSlotCalculator(const Value *V) {
46 assert(!isa<Type>(V) && "Can't create an SC for a type!");
47 if (const MethodArgument *MA =dyn_cast<const MethodArgument>(V)){
48 return new SlotCalculator(MA->getParent(), true);
49 } else if (const Instruction *I = dyn_cast<const Instruction>(V)) {
50 return new SlotCalculator(I->getParent()->getParent(), true);
51 } else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(V)) {
52 return new SlotCalculator(BB->getParent(), true);
53 } else if (const GlobalVariable *GV =dyn_cast<const GlobalVariable>(V)){
54 return new SlotCalculator(GV->getParent(), true);
55 } else if (const Method *Meth = dyn_cast<const Method>(V)) {
56 return new SlotCalculator(Meth, true);
57 } else if (const Module *Mod = dyn_cast<const Module>(V)) {
58 return new SlotCalculator(Mod, true);
63 // WriteAsOperand - Write the name of the specified value out to the specified
64 // ostream. This can be useful when you just want to print int %reg126, not the
65 // whole instruction that generated it.
67 static void WriteAsOperandInternal(ostream &Out, const Value *V, bool PrintName,
68 SlotCalculator *Table) {
69 if (PrintName && V->hasName()) {
70 Out << " %" << V->getName();
72 if (const ConstPoolVal *CPV = dyn_cast<const ConstPoolVal>(V)) {
73 Out << " " << CPV->getStrValue();
77 Slot = Table->getValSlot(V);
79 if (const Type *Ty = dyn_cast<const Type>(V)) {
80 Out << " " << Ty->getDescription();
84 Table = createSlotCalculator(V);
85 if (Table == 0) { Out << "BAD VALUE TYPE!"; return; }
87 Slot = Table->getValSlot(V);
90 if (Slot >= 0) Out << " %" << Slot;
92 Out << "<badref>"; // Not embeded into a location?
98 // If the module has a symbol table, take all global types and stuff their
99 // names into the TypeNames map.
101 static void fillTypeNameTable(const Module *M,
102 map<const Type *, string> &TypeNames) {
103 if (M && M->hasSymbolTable()) {
104 const SymbolTable *ST = M->getSymbolTable();
105 SymbolTable::const_iterator PI = ST->find(Type::TypeTy);
106 if (PI != ST->end()) {
107 SymbolTable::type_const_iterator I = PI->second.begin();
108 for (; I != PI->second.end(); ++I) {
109 // As a heuristic, don't insert pointer to primitive types, because
110 // they are used too often to have a single useful name.
112 const Type *Ty = cast<const Type>(I->second);
113 if (!isa<PointerType>(Ty) ||
114 !cast<PointerType>(Ty)->getValueType()->isPrimitiveType())
115 TypeNames.insert(make_pair(Ty, "%"+I->first));
123 static string calcTypeName(const Type *Ty, vector<const Type *> &TypeStack,
124 map<const Type *, string> &TypeNames) {
125 if (Ty->isPrimitiveType()) return Ty->getDescription(); // Base case
127 // Check to see if the type is named.
128 map<const Type *, string>::iterator I = TypeNames.find(Ty);
129 if (I != TypeNames.end()) return I->second;
131 // Check to see if the Type is already on the stack...
132 unsigned Slot = 0, CurSize = TypeStack.size();
133 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
135 // This is another base case for the recursion. In this case, we know
136 // that we have looped back to a type that we have previously visited.
137 // Generate the appropriate upreference to handle this.
140 return "\\" + utostr(CurSize-Slot); // Here's the upreference
142 TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
145 switch (Ty->getPrimitiveID()) {
146 case Type::MethodTyID: {
147 const MethodType *MTy = cast<const MethodType>(Ty);
148 Result = calcTypeName(MTy->getReturnType(), TypeStack, TypeNames) + " (";
149 for (MethodType::ParamTypes::const_iterator
150 I = MTy->getParamTypes().begin(),
151 E = MTy->getParamTypes().end(); I != E; ++I) {
152 if (I != MTy->getParamTypes().begin())
154 Result += calcTypeName(*I, TypeStack, TypeNames);
156 if (MTy->isVarArg()) {
157 if (!MTy->getParamTypes().empty()) Result += ", ";
163 case Type::StructTyID: {
164 const StructType *STy = cast<const StructType>(Ty);
166 for (StructType::ElementTypes::const_iterator
167 I = STy->getElementTypes().begin(),
168 E = STy->getElementTypes().end(); I != E; ++I) {
169 if (I != STy->getElementTypes().begin())
171 Result += calcTypeName(*I, TypeStack, TypeNames);
176 case Type::PointerTyID:
177 Result = calcTypeName(cast<const PointerType>(Ty)->getValueType(),
178 TypeStack, TypeNames) + " *";
180 case Type::ArrayTyID: {
181 const ArrayType *ATy = cast<const ArrayType>(Ty);
182 int NumElements = ATy->getNumElements();
184 if (NumElements != -1) Result += itostr(NumElements) + " x ";
185 Result += calcTypeName(ATy->getElementType(), TypeStack, TypeNames) + "]";
189 assert(0 && "Unhandled case in getTypeProps!");
193 TypeStack.pop_back(); // Remove self from stack...
198 // printTypeInt - The internal guts of printing out a type that has a
199 // potentially named portion.
201 static ostream &printTypeInt(ostream &Out, const Type *Ty,
202 map<const Type *, string> &TypeNames) {
203 // Primitive types always print out their description, regardless of whether
204 // they have been named or not.
206 if (Ty->isPrimitiveType()) return Out << Ty->getDescription();
208 // Check to see if the type is named.
209 map<const Type *, string>::iterator I = TypeNames.find(Ty);
210 if (I != TypeNames.end()) return Out << I->second;
212 // Otherwise we have a type that has not been named but is a derived type.
213 // Carefully recurse the type hierarchy to print out any contained symbolic
216 vector<const Type *> TypeStack;
217 string TypeName = calcTypeName(Ty, TypeStack, TypeNames);
218 TypeNames.insert(make_pair(Ty, TypeName)); // Cache type name for later use
219 return Out << TypeName;
223 // WriteTypeSymbolic - This attempts to write the specified type as a symbolic
224 // type, iff there is an entry in the modules symbol table for the specified
225 // type or one of it's component types. This is slower than a simple x << Type;
227 ostream &WriteTypeSymbolic(ostream &Out, const Type *Ty, const Module *M) {
230 // If they want us to print out a type, attempt to make it symbolic if there
231 // is a symbol table in the module...
232 if (M && M->hasSymbolTable()) {
233 map<const Type *, string> TypeNames;
234 fillTypeNameTable(M, TypeNames);
236 return printTypeInt(Out, Ty, TypeNames);
238 return Out << Ty->getDescription();
243 // WriteAsOperand - Write the name of the specified value out to the specified
244 // ostream. This can be useful when you just want to print int %reg126, not the
245 // whole instruction that generated it.
247 ostream &WriteAsOperand(ostream &Out, const Value *V, bool PrintType,
248 bool PrintName, SlotCalculator *Table) {
250 WriteTypeSymbolic(Out, V->getType(), getModuleFromVal(V));
252 WriteAsOperandInternal(Out, V, PrintName, Table);
258 class AssemblyWriter {
260 SlotCalculator &Table;
261 const Module *TheModule;
262 map<const Type *, string> TypeNames;
264 inline AssemblyWriter(ostream &o, SlotCalculator &Tab, const Module *M)
265 : Out(o), Table(Tab), TheModule(M) {
267 // If the module has a symbol table, take all global types and stuff their
268 // names into the TypeNames map.
270 fillTypeNameTable(M, TypeNames);
273 inline void write(const Module *M) { printModule(M); }
274 inline void write(const GlobalVariable *G) { printGlobal(G); }
275 inline void write(const Method *M) { printMethod(M); }
276 inline void write(const BasicBlock *BB) { printBasicBlock(BB); }
277 inline void write(const Instruction *I) { printInstruction(I); }
278 inline void write(const ConstPoolVal *CPV) { printConstant(CPV); }
279 inline void write(const Type *Ty) { printType(Ty); }
282 void printModule(const Module *M);
283 void printSymbolTable(const SymbolTable &ST);
284 void printConstant(const ConstPoolVal *CPV);
285 void printGlobal(const GlobalVariable *GV);
286 void printMethod(const Method *M);
287 void printMethodArgument(const MethodArgument *MA);
288 void printBasicBlock(const BasicBlock *BB);
289 void printInstruction(const Instruction *I);
290 ostream &printType(const Type *Ty);
292 void writeOperand(const Value *Op, bool PrintType, bool PrintName = true);
294 // printInfoComment - Print a little comment after the instruction indicating
295 // which slot it occupies.
296 void printInfoComment(const Value *V);
300 void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType,
302 if (PrintType) { Out << " "; printType(Operand->getType()); }
303 WriteAsOperandInternal(Out, Operand, PrintName, &Table);
307 void AssemblyWriter::printModule(const Module *M) {
308 // Loop over the symbol table, emitting all named constants...
309 if (M->hasSymbolTable())
310 printSymbolTable(*M->getSymbolTable());
312 for_each(M->gbegin(), M->gend(),
313 bind_obj(this, &AssemblyWriter::printGlobal));
315 Out << "implementation\n";
317 // Output all of the methods...
318 for_each(M->begin(), M->end(), bind_obj(this,&AssemblyWriter::printMethod));
321 void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
322 if (GV->hasName()) Out << "%" << GV->getName() << " = ";
324 if (GV->hasInternalLinkage()) Out << "internal ";
325 if (!GV->hasInitializer()) Out << "uninitialized ";
327 Out << (GV->isConstant() ? "constant " : "global ");
328 printType(GV->getType()->getValueType());
330 if (GV->hasInitializer())
331 writeOperand(GV->getInitializer(), false, false);
333 printInfoComment(GV);
338 // printSymbolTable - Run through symbol table looking for named constants
339 // if a named constant is found, emit it's declaration...
341 void AssemblyWriter::printSymbolTable(const SymbolTable &ST) {
342 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
343 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
344 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
346 for (; I != End; ++I) {
347 const Value *V = I->second;
348 if (const ConstPoolVal *CPV = dyn_cast<const ConstPoolVal>(V)) {
350 } else if (const Type *Ty = dyn_cast<const Type>(V)) {
351 Out << "\t%" << I->first << " = type " << Ty->getDescription() << endl;
358 // printConstant - Print out a constant pool entry...
360 void AssemblyWriter::printConstant(const ConstPoolVal *CPV) {
361 // Don't print out unnamed constants, they will be inlined
362 if (!CPV->hasName()) return;
365 Out << "\t%" << CPV->getName() << " = ";
367 // Print out the constant type...
368 printType(CPV->getType());
370 // Write the value out now...
371 writeOperand(CPV, false, false);
373 if (!CPV->hasName() && CPV->getType() != Type::VoidTy) {
374 int Slot = Table.getValSlot(CPV); // Print out the def slot taken...
376 printType(CPV->getType()) << ">:";
377 if (Slot >= 0) Out << Slot;
378 else Out << "<badref>";
384 // printMethod - Print all aspects of a method.
386 void AssemblyWriter::printMethod(const Method *M) {
387 // Print out the return type and name...
388 Out << "\n" << (M->isExternal() ? "declare " : "")
389 << (M->hasInternalLinkage() ? "internal " : "");
390 printType(M->getReturnType()) << " \"" << M->getName() << "\"(";
391 Table.incorporateMethod(M);
393 // Loop over the arguments, printing them...
394 const MethodType *MT = cast<const MethodType>(M->getMethodType());
396 if (!M->isExternal()) {
397 for_each(M->getArgumentList().begin(), M->getArgumentList().end(),
398 bind_obj(this, &AssemblyWriter::printMethodArgument));
400 // Loop over the arguments, printing them...
401 const MethodType *MT = cast<const MethodType>(M->getMethodType());
402 for (MethodType::ParamTypes::const_iterator I = MT->getParamTypes().begin(),
403 E = MT->getParamTypes().end(); I != E; ++I) {
404 if (I != MT->getParamTypes().begin()) Out << ", ";
409 // Finish printing arguments...
410 if (MT->isVarArg()) {
411 if (MT->getParamTypes().size()) Out << ", ";
412 Out << "..."; // Output varargs portion of signature!
416 if (!M->isExternal()) {
417 // Loop over the symbol table, emitting all named constants...
418 if (M->hasSymbolTable())
419 printSymbolTable(*M->getSymbolTable());
423 // Output all of its basic blocks... for the method
424 for_each(M->begin(), M->end(),
425 bind_obj(this, &AssemblyWriter::printBasicBlock));
433 // printMethodArgument - This member is called for every argument that
434 // is passed into the method. Simply print it out
436 void AssemblyWriter::printMethodArgument(const MethodArgument *Arg) {
437 // Insert commas as we go... the first arg doesn't get a comma
438 if (Arg != Arg->getParent()->getArgumentList().front()) Out << ", ";
441 printType(Arg->getType());
443 // Output name, if available...
445 Out << " %" << Arg->getName();
446 else if (Table.getValSlot(Arg) < 0)
450 // printBasicBlock - This member is called for each basic block in a methd.
452 void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
453 if (BB->hasName()) { // Print out the label if it exists...
454 Out << "\n" << BB->getName() << ":";
456 int Slot = Table.getValSlot(BB);
457 Out << "\n; <label>:";
459 Out << Slot; // Extra newline seperates out label's
463 Out << "\t\t\t\t\t;[#uses=" << BB->use_size() << "]\n"; // Output # uses
465 // Output all of the instructions in the basic block...
466 for_each(BB->begin(), BB->end(),
467 bind_obj(this, &AssemblyWriter::printInstruction));
471 // printInfoComment - Print a little comment after the instruction indicating
472 // which slot it occupies.
474 void AssemblyWriter::printInfoComment(const Value *V) {
475 if (V->getType() != Type::VoidTy) {
477 printType(V->getType()) << ">";
480 int Slot = Table.getValSlot(V); // Print out the def slot taken...
481 if (Slot >= 0) Out << ":" << Slot;
482 else Out << ":<badref>";
484 Out << "\t[#uses=" << V->use_size() << "]"; // Output # uses
488 // printInstruction - This member is called for each Instruction in a methd.
490 void AssemblyWriter::printInstruction(const Instruction *I) {
493 // Print out name if it exists...
494 if (I && I->hasName())
495 Out << "%" << I->getName() << " = ";
497 // Print out the opcode...
498 Out << I->getOpcodeName();
500 // Print out the type of the operands...
501 const Value *Operand = I->getNumOperands() ? I->getOperand(0) : 0;
503 // Special case conditional branches to swizzle the condition out to the front
504 if (I->getOpcode() == Instruction::Br && I->getNumOperands() > 1) {
505 writeOperand(I->getOperand(2), true);
507 writeOperand(Operand, true);
509 writeOperand(I->getOperand(1), true);
511 } else if (I->getOpcode() == Instruction::Switch) {
512 // Special case switch statement to get formatting nice and correct...
513 writeOperand(Operand , true); Out << ",";
514 writeOperand(I->getOperand(1), true); Out << " [";
516 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; op += 2) {
518 writeOperand(I->getOperand(op ), true); Out << ",";
519 writeOperand(I->getOperand(op+1), true);
522 } else if (isa<PHINode>(I)) {
524 printType(I->getType());
527 for (unsigned op = 0, Eop = I->getNumOperands(); op < Eop; op += 2) {
530 writeOperand(I->getOperand(op ), false); Out << ",";
531 writeOperand(I->getOperand(op+1), false); Out << " ]";
533 } else if (isa<ReturnInst>(I) && !Operand) {
535 } else if (isa<CallInst>(I)) {
536 const PointerType *PTy = dyn_cast<PointerType>(Operand->getType());
537 const MethodType *MTy = PTy ? dyn_cast<MethodType>(PTy->getValueType()) :0;
538 const Type *RetTy = MTy ? MTy->getReturnType() : 0;
540 // If possible, print out the short form of the call instruction, but we can
541 // only do this if the first argument is a pointer to a nonvararg method,
542 // and if the value returned is not a pointer to a method.
544 if (RetTy && !MTy->isVarArg() &&
545 (!isa<PointerType>(RetTy)||!isa<MethodType>(cast<PointerType>(RetTy)))){
546 Out << " "; printType(RetTy);
547 writeOperand(Operand, false);
549 writeOperand(Operand, true);
552 if (I->getNumOperands() > 1) writeOperand(I->getOperand(1), true);
553 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; ++op) {
555 writeOperand(I->getOperand(op), true);
559 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(I)) {
560 // TODO: Should try to print out short form of the Invoke instruction
561 writeOperand(Operand, true);
563 if (I->getNumOperands() > 3) writeOperand(I->getOperand(3), true);
564 for (unsigned op = 4, Eop = I->getNumOperands(); op < Eop; ++op) {
566 writeOperand(I->getOperand(op), true);
569 Out << " )\n\t\t\tto";
570 writeOperand(II->getNormalDest(), true);
572 writeOperand(II->getExceptionalDest(), true);
574 } else if (I->getOpcode() == Instruction::Malloc ||
575 I->getOpcode() == Instruction::Alloca) {
577 printType(cast<const PointerType>(I->getType())->getValueType());
578 if (I->getNumOperands()) {
580 writeOperand(I->getOperand(0), true);
582 } else if (isa<CastInst>(I)) {
583 writeOperand(Operand, true);
585 printType(I->getType());
586 } else if (Operand) { // Print the normal way...
588 // PrintAllTypes - Instructions who have operands of all the same type
589 // omit the type from all but the first operand. If the instruction has
590 // different type operands (for example br), then they are all printed.
591 bool PrintAllTypes = false;
592 const Type *TheType = Operand->getType();
594 for (unsigned i = 1, E = I->getNumOperands(); i != E; ++i) {
595 Operand = I->getOperand(i);
596 if (Operand->getType() != TheType) {
597 PrintAllTypes = true; // We have differing types! Print them all!
602 // Shift Left & Right print both types even for Ubyte LHS
603 if (isa<ShiftInst>(I)) PrintAllTypes = true;
605 if (!PrintAllTypes) {
607 printType(I->getOperand(0)->getType());
610 for (unsigned i = 0, E = I->getNumOperands(); i != E; ++i) {
612 writeOperand(I->getOperand(i), PrintAllTypes);
621 // printType - Go to extreme measures to attempt to print out a short, symbolic
622 // version of a type name.
624 ostream &AssemblyWriter::printType(const Type *Ty) {
625 return printTypeInt(Out, Ty, TypeNames);
629 //===----------------------------------------------------------------------===//
630 // External Interface declarations
631 //===----------------------------------------------------------------------===//
635 void WriteToAssembly(const Module *M, ostream &o) {
636 if (M == 0) { o << "<null> module\n"; return; }
637 SlotCalculator SlotTable(M, true);
638 AssemblyWriter W(o, SlotTable, M);
643 void WriteToAssembly(const GlobalVariable *G, ostream &o) {
644 if (G == 0) { o << "<null> global variable\n"; return; }
645 SlotCalculator SlotTable(G->getParent(), true);
646 AssemblyWriter W(o, SlotTable, G->getParent());
650 void WriteToAssembly(const Method *M, ostream &o) {
651 if (M == 0) { o << "<null> method\n"; return; }
652 SlotCalculator SlotTable(M->getParent(), true);
653 AssemblyWriter W(o, SlotTable, M->getParent());
659 void WriteToAssembly(const BasicBlock *BB, ostream &o) {
660 if (BB == 0) { o << "<null> basic block\n"; return; }
662 SlotCalculator SlotTable(BB->getParent(), true);
663 AssemblyWriter W(o, SlotTable,
664 BB->getParent() ? BB->getParent()->getParent() : 0);
669 void WriteToAssembly(const ConstPoolVal *CPV, ostream &o) {
670 if (CPV == 0) { o << "<null> constant pool value\n"; return; }
671 o << " " << CPV->getType()->getDescription() << " " << CPV->getStrValue();
674 void WriteToAssembly(const Instruction *I, ostream &o) {
675 if (I == 0) { o << "<null> instruction\n"; return; }
677 const Method *M = I->getParent() ? I->getParent()->getParent() : 0;
678 SlotCalculator SlotTable(M, true);
679 AssemblyWriter W(o, SlotTable, M ? M->getParent() : 0);
684 void CachedWriter::setModule(const Module *M) {
685 delete SC; delete AW;
687 SC = new SlotCalculator(M, true);
688 AW = new AssemblyWriter(Out, *SC, M);
694 CachedWriter::~CachedWriter() {
699 CachedWriter &CachedWriter::operator<<(const Value *V) {
700 assert(AW && SC && "CachedWriter does not have a current module!");
701 switch (V->getValueType()) {
702 case Value::ConstantVal:
703 Out << " "; AW->write(V->getType());
704 Out << " " << cast<ConstPoolVal>(V)->getStrValue(); break;
705 case Value::MethodArgumentVal:
706 AW->write(V->getType()); Out << " " << V->getName(); break;
707 case Value::TypeVal: AW->write(cast<const Type>(V)); break;
708 case Value::InstructionVal: AW->write(cast<Instruction>(V)); break;
709 case Value::BasicBlockVal: AW->write(cast<BasicBlock>(V)); break;
710 case Value::MethodVal: AW->write(cast<Method>(V)); break;
711 case Value::GlobalVariableVal: AW->write(cast<GlobalVariable>(V)); break;
712 case Value::ModuleVal: AW->write(cast<Module>(V)); break;
713 default: Out << "<unknown value type: " << V->getValueType() << ">"; break;