blob: fdb444e54eef44a82a667d96df5776ccabadd136 [file] [log] [blame]
Chris Lattner72614082002-10-25 22:55:53 +00001//===-- X86/Printer.cpp - Convert X86 code to human readable rep. ---------===//
2//
3// This file contains a printer that converts from our internal representation
4// of LLVM code to a nice human readable form that is suitable for debuggging.
5//
6//===----------------------------------------------------------------------===//
7
8#include "X86.h"
Brian Gaeke6559bb92002-11-14 22:32:30 +00009#include "X86InstrInfo.h"
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000010#include "llvm/Pass.h"
Brian Gaeke6559bb92002-11-14 22:32:30 +000011#include "llvm/Function.h"
12#include "llvm/Target/TargetMachine.h"
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000013#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner72614082002-10-25 22:55:53 +000014#include <iostream>
15
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000016namespace {
17 struct Printer : public FunctionPass {
18 TargetMachine &TM;
19 std::ostream &O;
Chris Lattner72614082002-10-25 22:55:53 +000020
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000021 Printer(TargetMachine &tm, std::ostream &o) : TM(tm), O(o) {}
22
23 bool runOnFunction(Function &F);
24 };
25}
26
Brian Gaeke6559bb92002-11-14 22:32:30 +000027/// runOnFunction - This uses the X86InstructionInfo::print method
28/// to print assembly for each instruction.
29bool Printer::runOnFunction (Function & F)
30{
31 static unsigned bbnumber = 0;
32 MachineFunction & MF = MachineFunction::get (&F);
33 const MachineInstrInfo & MII = TM.getInstrInfo ();
34 const X86InstrInfo & x86ii = dynamic_cast <const X86InstrInfo &> (MII);
35
36 O << "# x86 printing not implemented yet!\n";
37
38 // Print out labels for the function.
39 O << "\t.globl\t" << F.getName () << "\n";
40 O << "\t.type\t" << F.getName () << ", @function\n";
41 O << F.getName () << ":\n";
42
43 // Print out code for the function.
44 for (MachineFunction::const_iterator bb_i = MF.begin (), bb_e = MF.end ();
45 bb_i != bb_e; ++bb_i)
46 {
47 // Print a label for the basic block.
48 O << ".BB" << bbnumber++ << ":\n";
49 for (MachineBasicBlock::const_iterator i_i = bb_i->begin (), i_e =
50 bb_i->end (); i_i != i_e; ++i_i)
51 {
52 // Print the assembly for the instruction.
53 O << "\t";
54 x86ii.print (*i_i, O);
55 }
56 }
57
58 // We didn't modify anything.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000059 return false;
60}
61
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000062/// createX86CodePrinterPass - Print out the specified machine code function to
63/// the specified stream. This function should work regardless of whether or
64/// not the function is in SSA form or not.
65///
66Pass *createX86CodePrinterPass(TargetMachine &TM, std::ostream &O) {
67 return new Printer(TM, O);
Chris Lattner72614082002-10-25 22:55:53 +000068}