SparcAsmPrinter.cpp revision d3b31a73e8b58e837510fd429f0f4cb2b8c1a0c2
1//===-- SparcAsmPrinter.cpp - Sparc LLVM assembly writer ------------------===//
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 file contains a printer that converts from our internal representation
11// of machine-dependent LLVM code to GAS-format SPARC assembly language.
12//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "asm-printer"
16#include "Sparc.h"
17#include "SparcInstrInfo.h"
18#include "SparcTargetMachine.h"
19#include "llvm/Constants.h"
20#include "llvm/DerivedTypes.h"
21#include "llvm/Module.h"
22#include "llvm/CodeGen/AsmPrinter.h"
23#include "llvm/CodeGen/DwarfWriter.h"
24#include "llvm/CodeGen/MachineFunctionPass.h"
25#include "llvm/CodeGen/MachineConstantPool.h"
26#include "llvm/CodeGen/MachineInstr.h"
27#include "llvm/MC/MCAsmInfo.h"
28#include "llvm/MC/MCStreamer.h"
29#include "llvm/MC/MCSymbol.h"
30#include "llvm/Target/TargetLoweringObjectFile.h"
31#include "llvm/Target/TargetRegistry.h"
32#include "llvm/ADT/Statistic.h"
33#include "llvm/ADT/StringExtras.h"
34#include "llvm/Support/CommandLine.h"
35#include "llvm/Support/ErrorHandling.h"
36#include "llvm/Support/FormattedStream.h"
37#include "llvm/Support/MathExtras.h"
38#include <cctype>
39#include <cstring>
40#include <map>
41using namespace llvm;
42
43STATISTIC(EmittedInsts, "Number of machine instrs printed");
44
45namespace {
46  class SparcAsmPrinter : public AsmPrinter {
47  public:
48    explicit SparcAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
49                             const MCAsmInfo *T, bool V)
50      : AsmPrinter(O, TM, T, V) {}
51
52    virtual const char *getPassName() const {
53      return "Sparc Assembly Printer";
54    }
55
56    void printOperand(const MachineInstr *MI, int opNum);
57    void printMemOperand(const MachineInstr *MI, int opNum,
58                         const char *Modifier = 0);
59    void printCCOperand(const MachineInstr *MI, int opNum);
60
61    void printInstruction(const MachineInstr *MI);  // autogenerated.
62    static const char *getRegisterName(unsigned RegNo);
63
64    bool runOnMachineFunction(MachineFunction &F);
65    bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
66                       unsigned AsmVariant, const char *ExtraCode);
67    bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
68                             unsigned AsmVariant, const char *ExtraCode);
69
70    bool printGetPCX(const MachineInstr *MI, unsigned OpNo);
71  };
72} // end of anonymous namespace
73
74#include "SparcGenAsmWriter.inc"
75
76/// runOnMachineFunction - This uses the printInstruction()
77/// method to print assembly for each instruction.
78///
79bool SparcAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
80  SetupMachineFunction(MF);
81
82  EmitFunctionHeader();
83
84  // Print out code for the function.
85  for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
86       I != E; ++I) {
87    // Print a label for the basic block.
88    if (I != MF.begin())
89      EmitBasicBlockStart(I);
90
91    for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
92         II != E; ++II) {
93      // Print the assembly for the instruction.
94      processDebugLoc(II, true);
95      printInstruction(II);
96
97      if (VerboseAsm)
98        EmitComments(*II);
99      O << '\n';
100      processDebugLoc(II, false);
101      ++EmittedInsts;
102    }
103  }
104
105  // Emit post-function debug information.
106  DW->EndFunction(&MF);
107
108  // We didn't modify anything.
109  O << "\t.size\t" << *CurrentFnSym << ", .-" << *CurrentFnSym << '\n';
110  return false;
111}
112
113
114void SparcAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
115  const MachineOperand &MO = MI->getOperand (opNum);
116  bool CloseParen = false;
117  if (MI->getOpcode() == SP::SETHIi && !MO.isReg() && !MO.isImm()) {
118    O << "%hi(";
119    CloseParen = true;
120  } else if ((MI->getOpcode() == SP::ORri || MI->getOpcode() == SP::ADDri) &&
121             !MO.isReg() && !MO.isImm()) {
122    O << "%lo(";
123    CloseParen = true;
124  }
125  switch (MO.getType()) {
126  case MachineOperand::MO_Register:
127    O << "%" << LowercaseString(getRegisterName(MO.getReg()));
128    break;
129
130  case MachineOperand::MO_Immediate:
131    O << (int)MO.getImm();
132    break;
133  case MachineOperand::MO_MachineBasicBlock:
134    O << *MO.getMBB()->getSymbol(OutContext);
135    return;
136  case MachineOperand::MO_GlobalAddress:
137    O << *GetGlobalValueSymbol(MO.getGlobal());
138    break;
139  case MachineOperand::MO_ExternalSymbol:
140    O << MO.getSymbolName();
141    break;
142  case MachineOperand::MO_ConstantPoolIndex:
143    O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
144      << MO.getIndex();
145    break;
146  default:
147    llvm_unreachable("<unknown operand type>");
148  }
149  if (CloseParen) O << ")";
150}
151
152void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
153                                      const char *Modifier) {
154  printOperand(MI, opNum);
155
156  // If this is an ADD operand, emit it like normal operands.
157  if (Modifier && !strcmp(Modifier, "arith")) {
158    O << ", ";
159    printOperand(MI, opNum+1);
160    return;
161  }
162
163  if (MI->getOperand(opNum+1).isReg() &&
164      MI->getOperand(opNum+1).getReg() == SP::G0)
165    return;   // don't print "+%g0"
166  if (MI->getOperand(opNum+1).isImm() &&
167      MI->getOperand(opNum+1).getImm() == 0)
168    return;   // don't print "+0"
169
170  O << "+";
171  if (MI->getOperand(opNum+1).isGlobal() ||
172      MI->getOperand(opNum+1).isCPI()) {
173    O << "%lo(";
174    printOperand(MI, opNum+1);
175    O << ")";
176  } else {
177    printOperand(MI, opNum+1);
178  }
179}
180
181bool SparcAsmPrinter::printGetPCX(const MachineInstr *MI, unsigned opNum) {
182  std::string operand = "";
183  const MachineOperand &MO = MI->getOperand(opNum);
184  switch (MO.getType()) {
185  default: assert(0 && "Operand is not a register ");
186  case MachineOperand::MO_Register:
187    assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
188           "Operand is not a physical register ");
189    operand = "%" + LowercaseString(getRegisterName(MO.getReg()));
190    break;
191  }
192
193  unsigned bbNum = MI->getParent()->getNumber();
194
195  O << '\n' << ".LLGETPCH" << bbNum << ":\n";
196  O << "\tcall\t.LLGETPC" << bbNum << '\n' ;
197
198  O << "\t  sethi\t"
199    << "%hi(_GLOBAL_OFFSET_TABLE_+(.-.LLGETPCH" << bbNum << ")), "
200    << operand << '\n' ;
201
202  O << ".LLGETPC" << bbNum << ":\n" ;
203  O << "\tor\t" << operand
204    << ", %lo(_GLOBAL_OFFSET_TABLE_+(.-.LLGETPCH" << bbNum << ")), "
205    << operand << '\n';
206  O << "\tadd\t" << operand << ", %o7, " << operand << '\n';
207
208  return true;
209}
210
211void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
212  int CC = (int)MI->getOperand(opNum).getImm();
213  O << SPARCCondCodeToString((SPCC::CondCodes)CC);
214}
215
216/// PrintAsmOperand - Print out an operand for an inline asm expression.
217///
218bool SparcAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
219                                      unsigned AsmVariant,
220                                      const char *ExtraCode) {
221  if (ExtraCode && ExtraCode[0]) {
222    if (ExtraCode[1] != 0) return true; // Unknown modifier.
223
224    switch (ExtraCode[0]) {
225    default: return true;  // Unknown modifier.
226    case 'r':
227     break;
228    }
229  }
230
231  printOperand(MI, OpNo);
232
233  return false;
234}
235
236bool SparcAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
237                                            unsigned OpNo,
238                                            unsigned AsmVariant,
239                                            const char *ExtraCode) {
240  if (ExtraCode && ExtraCode[0])
241    return true;  // Unknown modifier
242
243  O << '[';
244  printMemOperand(MI, OpNo);
245  O << ']';
246
247  return false;
248}
249
250// Force static initialization.
251extern "C" void LLVMInitializeSparcAsmPrinter() {
252  RegisterAsmPrinter<SparcAsmPrinter> X(TheSparcTarget);
253}
254