implement several trivial operand printers, reducing
[oota-llvm.git] / lib / Target / PowerPC / InstPrinter / PPCInstPrinter.cpp
1 //===-- PPCInstPrinter.cpp - Convert PPC MCInst to assembly syntax --------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This class prints an PPC MCInst to a .s file.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #define DEBUG_TYPE "asm-printer"
15 #include "PPCInstPrinter.h"
16 #include "llvm/MC/MCExpr.h"
17 #include "llvm/MC/MCInst.h"
18 //#include "llvm/MC/MCAsmInfo.h"
19 //#include "llvm/ADT/StringExtras.h"
20 #include "llvm/Support/raw_ostream.h"
21
22 #include "PPCGenRegisterNames.inc"
23 #include "PPCGenInstrNames.inc"
24 using namespace llvm;
25
26 #define GET_INSTRUCTION_NAME
27 #define PPCAsmPrinter PPCInstPrinter
28 #define MachineInstr MCInst
29 #include "PPCGenAsmWriter.inc"
30
31 StringRef PPCInstPrinter::getOpcodeName(unsigned Opcode) const {
32   return getInstructionName(Opcode);
33 }
34
35
36 void PPCInstPrinter::printInst(const MCInst *MI, raw_ostream &O) {
37   // TODO: pseudo ops.
38   
39   printInstruction(MI, O);
40 }
41
42 void PPCInstPrinter::printS5ImmOperand(const MCInst *MI, unsigned OpNo,
43                                        raw_ostream &O) {
44   char Value = MI->getOperand(OpNo).getImm();
45   Value = (Value << (32-5)) >> (32-5);
46   O << (int)Value;
47 }
48
49 void PPCInstPrinter::printU5ImmOperand(const MCInst *MI, unsigned OpNo,
50                                        raw_ostream &O) {
51   unsigned char Value = MI->getOperand(OpNo).getImm();
52   assert(Value <= 31 && "Invalid u5imm argument!");
53   O << (unsigned int)Value;
54 }
55
56 void PPCInstPrinter::printU6ImmOperand(const MCInst *MI, unsigned OpNo,
57                                        raw_ostream &O) {
58   unsigned char Value = MI->getOperand(OpNo).getImm();
59   assert(Value <= 63 && "Invalid u6imm argument!");
60   O << (unsigned int)Value;
61 }
62
63 void PPCInstPrinter::printS16ImmOperand(const MCInst *MI, unsigned OpNo,
64                                         raw_ostream &O) {
65   O << (short)MI->getOperand(OpNo).getImm();
66 }
67
68 void PPCInstPrinter::printU16ImmOperand(const MCInst *MI, unsigned OpNo,
69                                         raw_ostream &O) {
70   O << (unsigned short)MI->getOperand(OpNo).getImm();
71 }
72
73 void PPCInstPrinter::printS16X4ImmOperand(const MCInst *MI, unsigned OpNo,
74                                           raw_ostream &O) {
75   if (MI->getOperand(OpNo).isImm()) {
76     O << (short)(MI->getOperand(OpNo).getImm()*4);
77     return;
78   }
79   
80   assert(0 && "Unhandled operand");
81 #if 0
82   O << "lo16(";
83   printOp(MI->getOperand(OpNo), O);
84   if (TM.getRelocationModel() == Reloc::PIC_)
85     O << "-\"L" << getFunctionNumber() << "$pb\")";
86   else
87     O << ')';
88 #endif
89 }
90
91
92 /// stripRegisterPrefix - This method strips the character prefix from a
93 /// register name so that only the number is left.  Used by for linux asm.
94 const char *stripRegisterPrefix(const char *RegName) {
95   switch (RegName[0]) {
96   case 'r':
97   case 'f':
98   case 'v': return RegName + 1;
99   case 'c': if (RegName[1] == 'r') return RegName + 2;
100   }
101   
102   return RegName;
103 }
104
105 void PPCInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
106                                   raw_ostream &O) {
107   const MCOperand &Op = MI->getOperand(OpNo);
108   if (Op.isReg()) {
109     const char *RegName = getRegisterName(Op.getReg());
110     // The linux and AIX assembler does not take register prefixes.
111     if (!isDarwinSyntax())
112       RegName = stripRegisterPrefix(RegName);
113     
114     O << RegName;
115     return;
116   }
117   
118   if (Op.isImm()) {
119     O << Op.getImm();
120     return;
121   }
122   
123   assert(Op.isExpr() && "unknown operand kind in printOperand");
124   O << *Op.getExpr();
125 }
126