blob: 20c052d746967c513a4f7557a6db46a7d1ddead3 [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 Lattnerdbb61c62002-11-17 22:53:13 +000014#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner72614082002-10-25 22:55:53 +000015
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
Chris Lattnerdbb61c62002-11-17 22:53:13 +000027/// createX86CodePrinterPass - Print out the specified machine code function to
28/// the specified stream. This function should work regardless of whether or
29/// not the function is in SSA form or not.
30///
31Pass *createX86CodePrinterPass(TargetMachine &TM, std::ostream &O) {
32 return new Printer(TM, O);
33}
34
35
Brian Gaeke6559bb92002-11-14 22:32:30 +000036/// runOnFunction - This uses the X86InstructionInfo::print method
37/// to print assembly for each instruction.
38bool Printer::runOnFunction (Function & F)
39{
40 static unsigned bbnumber = 0;
41 MachineFunction & MF = MachineFunction::get (&F);
42 const MachineInstrInfo & MII = TM.getInstrInfo ();
Brian Gaeke6559bb92002-11-14 22:32:30 +000043
Chris Lattner927dd092002-11-17 23:20:37 +000044 O << "; x86 printing only sorta implemented so far!\n";
Brian Gaeke6559bb92002-11-14 22:32:30 +000045
46 // Print out labels for the function.
47 O << "\t.globl\t" << F.getName () << "\n";
48 O << "\t.type\t" << F.getName () << ", @function\n";
49 O << F.getName () << ":\n";
50
51 // Print out code for the function.
52 for (MachineFunction::const_iterator bb_i = MF.begin (), bb_e = MF.end ();
53 bb_i != bb_e; ++bb_i)
54 {
55 // Print a label for the basic block.
56 O << ".BB" << bbnumber++ << ":\n";
57 for (MachineBasicBlock::const_iterator i_i = bb_i->begin (), i_e =
58 bb_i->end (); i_i != i_e; ++i_i)
59 {
60 // Print the assembly for the instruction.
61 O << "\t";
Chris Lattner927dd092002-11-17 23:20:37 +000062 MII.print(*i_i, O, TM);
Brian Gaeke6559bb92002-11-14 22:32:30 +000063 }
64 }
65
66 // We didn't modify anything.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000067 return false;
68}
69
Chris Lattnerf9f60882002-11-18 06:56:51 +000070static void printOp(std::ostream &O, const MachineOperand &MO,
71 const MRegisterInfo &RI) {
72 switch (MO.getType()) {
73 case MachineOperand::MO_VirtualRegister:
74 if (MO.getReg() < MRegisterInfo::FirstVirtualRegister)
75 O << RI.get(MO.getReg()).Name;
76 else
77 O << "%reg" << MO.getReg();
78 return;
79
80 default:
81 O << "<unknown op ty>"; return;
82 }
83}
84
85static inline void toHexDigit(std::ostream &O, unsigned char V) {
86 if (V >= 10)
87 O << (char)('A'+V-10);
88 else
89 O << (char)('0'+V);
90}
91
92static std::ostream &toHex(std::ostream &O, unsigned char V) {
93 toHexDigit(O, V >> 4);
94 toHexDigit(O, V & 0xF);
95 return O;
96}
97
Chris Lattnerdbb61c62002-11-17 22:53:13 +000098
99// print - Print out an x86 instruction in intel syntax
Chris Lattner927dd092002-11-17 23:20:37 +0000100void X86InstrInfo::print(const MachineInstr *MI, std::ostream &O,
101 const TargetMachine &TM) const {
Chris Lattnerf9f60882002-11-18 06:56:51 +0000102 unsigned Opcode = MI->getOpcode();
103 const MachineInstrDescriptor &Desc = get(Opcode);
104
105 if (Desc.TSFlags & X86II::TB)
106 O << "0F ";
107
108 switch (Desc.TSFlags & X86II::FormMask) {
109 case X86II::OtherFrm:
110 O << "\t";
111 O << "-"; MI->print(O, TM);
112 break;
113 case X86II::RawFrm:
114 toHex(O, getBaseOpcodeFor(Opcode)) << "\t";
115 O << getName(MI->getOpCode()) << " ";
116
117 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
118 if (i) O << ", ";
119 printOp(O, MI->getOperand(i), RI);
120 }
121 O << "\n";
122 return;
123
124
125 case X86II::AddRegFrm:
126 O << "\t-"; MI->print(O, TM); break;
127
128 case X86II::MRMDestReg:
129 // There are two acceptable forms of MRMDestReg instructions, those with 3
130 // and 2 operands:
131 //
132 // 3 Operands: in this form, the first two registers (the destination, and
133 // the first operand) should be the same, post register allocation. The 3rd
134 // operand is an additional input. This should be for things like add
135 // instructions.
136 //
137 // 2 Operands: this is for things like mov that do not read a second input
138 //
139 assert(((MI->getNumOperands() == 3 &&
140 (MI->getOperand(0).getType()==MachineOperand::MO_VirtualRegister&&
141 MI->getOperand(1).getType()==MachineOperand::MO_VirtualRegister))||
142 (MI->getNumOperands() == 2 &&
143 (MI->getOperand(0).getType()==MachineOperand::MO_VirtualRegister)))
144 && MI->getOperand(MI->getNumOperands()-1).getType() ==
145 MachineOperand::MO_VirtualRegister &&
146 "Bad format for MRMDestReg!");
147
148 if (MI->getNumOperands() == 3 &&
149 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
150 O << "**";
151
152 O << "\t";
153 O << getName(MI->getOpCode()) << " ";
154 printOp(O, MI->getOperand(0), RI);
155 O << ", ";
156 printOp(O, MI->getOperand(MI->getNumOperands()-1), RI);
157 O << "\n";
158 return;
159 case X86II::MRMDestMem:
160 case X86II::MRMSrcReg:
161 case X86II::MRMSrcMem:
162 default:
163 O << "\t-"; MI->print(O, TM); break;
164 }
Chris Lattner72614082002-10-25 22:55:53 +0000165}