//===-- Writer.cpp - Library for Printing VM assembly files ------*- C++ -*--=// // // This library implements the functionality defined in llvm/Assembly/Writer.h // // This library uses the Analysis library to figure out offsets for // variables in the method tables... // // TODO: print out the type name instead of the full type if a particular type // is in the symbol table... // //===----------------------------------------------------------------------===// #include "llvm/Assembly/Writer.h" #include "llvm/Analysis/SlotCalculator.h" #include "llvm/Module.h" #include "llvm/Method.h" #include "llvm/BasicBlock.h" #include "llvm/ConstPoolVals.h" #include "llvm/iOther.h" #include "llvm/iMemory.h" void DebugValue(const Value *V) { cerr << V << endl; } // WriteAsOperand - Write the name of the specified value out to the specified // ostream. This can be useful when you just want to print int %reg126, not the // whole instruction that generated it. // ostream &WriteAsOperand(ostream &Out, const Value *V, bool PrintType, bool PrintName, SlotCalculator *Table) { if (PrintType) Out << " " << V->getType(); if (V->hasName() && PrintName) { Out << " %" << V->getName(); } else { if (const ConstPoolVal *CPV = V->castConstant()) { Out << " " << CPV->getStrValue(); } else { int Slot; if (Table) { Slot = Table->getValSlot(V); } else { if (const Type *Ty = V->castType()) { return Out << " " << Ty; } else if (const MethodArgument *MA = V->castMethodArgument()) { Table = new SlotCalculator(MA->getParent(), true); } else if (const Instruction *I = V->castInstruction()) { Table = new SlotCalculator(I->getParent()->getParent(), true); } else if (const BasicBlock *BB = V->castBasicBlock()) { Table = new SlotCalculator(BB->getParent(), true); } else if (const Method *Meth = V->castMethod()) { Table = new SlotCalculator(Meth, true); } else if (const Module *Mod = V->castModule()) { Table = new SlotCalculator(Mod, true); } else { return Out << "BAD VALUE TYPE!"; } Slot = Table->getValSlot(V); delete Table; } if (Slot >= 0) Out << " %" << Slot; else if (PrintName) Out << ""; // Not embeded into a location? } } return Out; } class AssemblyWriter : public ModuleAnalyzer { ostream &Out; SlotCalculator &Table; public: inline AssemblyWriter(ostream &o, SlotCalculator &Tab) : Out(o), Table(Tab) { } inline void write(const Module *M) { processModule(M); } inline void write(const Method *M) { processMethod(M); } inline void write(const BasicBlock *BB) { processBasicBlock(BB); } inline void write(const Instruction *I) { processInstruction(I); } inline void write(const ConstPoolVal *CPV) { processConstant(CPV); } protected: virtual bool visitMethod(const Method *M); virtual bool processConstPool(const ConstantPool &CP, bool isMethod); virtual bool processConstant(const ConstPoolVal *CPV); virtual bool processMethod(const Method *M); virtual bool processMethodArgument(const MethodArgument *MA); virtual bool processBasicBlock(const BasicBlock *BB); virtual bool processInstruction(const Instruction *I); private : void writeOperand(const Value *Op, bool PrintType, bool PrintName = true); }; // visitMethod - This member is called after the above two steps, visting each // method, because they are effectively values that go into the constant pool. // bool AssemblyWriter::visitMethod(const Method *M) { return false; } bool AssemblyWriter::processConstPool(const ConstantPool &CP, bool isMethod) { // Done printing arguments... if (isMethod) Out << ")\n"; ModuleAnalyzer::processConstPool(CP, isMethod); if (isMethod) { if (!CP.getParentV()->castMethodAsserting()->isExternal()) Out << "begin"; } else { Out << "implementation\n"; } return false; } // processConstant - Print out a constant pool entry... // bool AssemblyWriter::processConstant(const ConstPoolVal *CPV) { Out << "\t"; // Print out name if it exists... if (CPV->hasName()) Out << "%" << CPV->getName() << " = "; // Print out the opcode... Out << CPV->getType(); // Write the value out now... writeOperand(CPV, false, false); if (!CPV->hasName() && CPV->getType() != Type::VoidTy) { int Slot = Table.getValSlot(CPV); // Print out the def slot taken... Out << "\t\t; <" << CPV->getType() << ">:"; if (Slot >= 0) Out << Slot; else Out << ""; } Out << endl; return false; } // processMethod - Process all aspects of a method. // bool AssemblyWriter::processMethod(const Method *M) { // Print out the return type and name... Out << "\n" << (M->isExternal() ? "declare " : "") << M->getReturnType() << " \"" << M->getName() << "\"("; Table.incorporateMethod(M); ModuleAnalyzer::processMethod(M); Table.purgeMethod(); if (!M->isExternal()) Out << "end\n"; return false; } // processMethodArgument - This member is called for every argument that // is passed into the method. Simply print it out // bool AssemblyWriter::processMethodArgument(const MethodArgument *Arg) { // Insert commas as we go... the first arg doesn't get a comma if (Arg != Arg->getParent()->getArgumentList().front()) Out << ", "; // Output type... Out << Arg->getType(); // Output name, if available... if (Arg->hasName()) Out << " %" << Arg->getName(); else if (Table.getValSlot(Arg) < 0) Out << ""; return false; } // processBasicBlock - This member is called for each basic block in a methd. // bool AssemblyWriter::processBasicBlock(const BasicBlock *BB) { if (BB->hasName()) { // Print out the label if it exists... Out << "\n" << BB->getName() << ":"; } else { int Slot = Table.getValSlot(BB); Out << "\n;