blob: 22f437ccf7f86e23dcbaba08edec1243085d032b [file] [log] [blame]
Chris Lattner158e1f52006-02-05 05:50:24 +00001//===-- SparcAsmPrinter.cpp - Sparc LLVM assembly writer ------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner158e1f52006-02-05 05:50:24 +00007//
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
Chris Lattner1ef9cd42006-12-19 22:59:26 +000015#define DEBUG_TYPE "asm-printer"
Chris Lattner158e1f52006-02-05 05:50:24 +000016#include "Sparc.h"
17#include "SparcInstrInfo.h"
Chris Lattner3773afe2009-06-19 15:48:10 +000018#include "SparcTargetMachine.h"
Chris Lattner158e1f52006-02-05 05:50:24 +000019#include "llvm/CodeGen/AsmPrinter.h"
Chris Lattner158e1f52006-02-05 05:50:24 +000020#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner7b26fce2009-08-22 20:48:53 +000021#include "llvm/MC/MCAsmInfo.h"
Chris Lattner4cd44982009-09-13 17:14:04 +000022#include "llvm/MC/MCSymbol.h"
Daniel Dunbare8338102009-07-15 20:24:03 +000023#include "llvm/Target/TargetRegistry.h"
Chris Lattner158e1f52006-02-05 05:50:24 +000024#include "llvm/ADT/StringExtras.h"
Chris Lattner4b7dadb2009-08-19 05:49:37 +000025#include "llvm/Support/FormattedStream.h"
Chris Lattner158e1f52006-02-05 05:50:24 +000026using namespace llvm;
27
Chris Lattner1ef9cd42006-12-19 22:59:26 +000028namespace {
Nick Lewycky02d5f772009-10-25 06:33:48 +000029 class SparcAsmPrinter : public AsmPrinter {
Bill Wendlingc5437ea2009-02-24 08:30:20 +000030 public:
David Greenea31f96c2009-07-14 20:18:05 +000031 explicit SparcAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattner7b26fce2009-08-22 20:48:53 +000032 const MCAsmInfo *T, bool V)
Chris Lattner2ab11002010-01-27 00:20:02 +000033 : AsmPrinter(O, TM, T, V) {}
Chris Lattner158e1f52006-02-05 05:50:24 +000034
35 virtual const char *getPassName() const {
36 return "Sparc Assembly Printer";
37 }
38
39 void printOperand(const MachineInstr *MI, int opNum);
Chris Lattnerfcb8a3a2006-02-10 07:35:42 +000040 void printMemOperand(const MachineInstr *MI, int opNum,
41 const char *Modifier = 0);
Chris Lattner158e1f52006-02-05 05:50:24 +000042 void printCCOperand(const MachineInstr *MI, int opNum);
43
Chris Lattnerfd97a332010-01-28 01:48:52 +000044 virtual void EmitInstruction(const MachineInstr *MI) {
45 printInstruction(MI);
46 }
Chris Lattnerb94284b2009-08-08 01:32:19 +000047 void printInstruction(const MachineInstr *MI); // autogenerated.
Chris Lattnerad10b3b2009-09-13 20:19:22 +000048 static const char *getRegisterName(unsigned RegNo);
Chris Lattner06c5eed2009-09-13 20:08:00 +000049
Anton Korobeynikov3db21732008-10-10 10:15:03 +000050 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
51 unsigned AsmVariant, const char *ExtraCode);
52 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
53 unsigned AsmVariant, const char *ExtraCode);
Chris Lattner840c7002009-09-15 17:46:24 +000054
Chris Lattner840c7002009-09-15 17:46:24 +000055 bool printGetPCX(const MachineInstr *MI, unsigned OpNo);
Chris Lattner158e1f52006-02-05 05:50:24 +000056 };
57} // end of anonymous namespace
58
59#include "SparcGenAsmWriter.inc"
60
Chris Lattner158e1f52006-02-05 05:50:24 +000061void SparcAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
62 const MachineOperand &MO = MI->getOperand (opNum);
Chris Lattner158e1f52006-02-05 05:50:24 +000063 bool CloseParen = false;
Dan Gohman0d1e9a82008-10-03 15:45:36 +000064 if (MI->getOpcode() == SP::SETHIi && !MO.isReg() && !MO.isImm()) {
Chris Lattner158e1f52006-02-05 05:50:24 +000065 O << "%hi(";
66 CloseParen = true;
Dan Gohman0d1e9a82008-10-03 15:45:36 +000067 } else if ((MI->getOpcode() == SP::ORri || MI->getOpcode() == SP::ADDri) &&
68 !MO.isReg() && !MO.isImm()) {
Chris Lattner158e1f52006-02-05 05:50:24 +000069 O << "%lo(";
70 CloseParen = true;
71 }
72 switch (MO.getType()) {
Chris Lattner10b71c02006-05-04 18:05:43 +000073 case MachineOperand::MO_Register:
Chris Lattnereb2cc682009-09-13 20:31:40 +000074 O << "%" << LowercaseString(getRegisterName(MO.getReg()));
Chris Lattner158e1f52006-02-05 05:50:24 +000075 break;
76
Chris Lattnerfef7a2d2006-05-04 17:21:20 +000077 case MachineOperand::MO_Immediate:
Chris Lattner5c463782007-12-30 20:49:49 +000078 O << (int)MO.getImm();
Chris Lattner158e1f52006-02-05 05:50:24 +000079 break;
Nate Begeman4ca2ea52006-04-22 18:53:45 +000080 case MachineOperand::MO_MachineBasicBlock:
Chris Lattnerd051af72010-01-26 04:55:51 +000081 O << *MO.getMBB()->getSymbol(OutContext);
Chris Lattner158e1f52006-02-05 05:50:24 +000082 return;
Chris Lattner158e1f52006-02-05 05:50:24 +000083 case MachineOperand::MO_GlobalAddress:
Chris Lattner8b5d55e2010-01-17 21:43:43 +000084 O << *GetGlobalValueSymbol(MO.getGlobal());
Chris Lattner158e1f52006-02-05 05:50:24 +000085 break;
86 case MachineOperand::MO_ExternalSymbol:
87 O << MO.getSymbolName();
88 break;
89 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattnere9a75a62009-08-22 21:43:10 +000090 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Chris Lattnera5bb3702007-12-30 23:10:15 +000091 << MO.getIndex();
Chris Lattner158e1f52006-02-05 05:50:24 +000092 break;
93 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +000094 llvm_unreachable("<unknown operand type>");
Chris Lattner158e1f52006-02-05 05:50:24 +000095 }
96 if (CloseParen) O << ")";
97}
98
Chris Lattnerfcb8a3a2006-02-10 07:35:42 +000099void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
100 const char *Modifier) {
Chris Lattner158e1f52006-02-05 05:50:24 +0000101 printOperand(MI, opNum);
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000102
Chris Lattnerfcb8a3a2006-02-10 07:35:42 +0000103 // If this is an ADD operand, emit it like normal operands.
104 if (Modifier && !strcmp(Modifier, "arith")) {
105 O << ", ";
106 printOperand(MI, opNum+1);
107 return;
108 }
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000109
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000110 if (MI->getOperand(opNum+1).isReg() &&
Chris Lattner158e1f52006-02-05 05:50:24 +0000111 MI->getOperand(opNum+1).getReg() == SP::G0)
112 return; // don't print "+%g0"
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000113 if (MI->getOperand(opNum+1).isImm() &&
Chris Lattner5c463782007-12-30 20:49:49 +0000114 MI->getOperand(opNum+1).getImm() == 0)
Chris Lattner158e1f52006-02-05 05:50:24 +0000115 return; // don't print "+0"
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000116
Chris Lattner158e1f52006-02-05 05:50:24 +0000117 O << "+";
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000118 if (MI->getOperand(opNum+1).isGlobal() ||
119 MI->getOperand(opNum+1).isCPI()) {
Chris Lattner158e1f52006-02-05 05:50:24 +0000120 O << "%lo(";
121 printOperand(MI, opNum+1);
122 O << ")";
123 } else {
124 printOperand(MI, opNum+1);
125 }
126}
127
Chris Lattner840c7002009-09-15 17:46:24 +0000128bool SparcAsmPrinter::printGetPCX(const MachineInstr *MI, unsigned opNum) {
129 std::string operand = "";
130 const MachineOperand &MO = MI->getOperand(opNum);
131 switch (MO.getType()) {
132 default: assert(0 && "Operand is not a register ");
133 case MachineOperand::MO_Register:
134 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
135 "Operand is not a physical register ");
136 operand = "%" + LowercaseString(getRegisterName(MO.getReg()));
137 break;
138 }
139
Chris Lattner2ab11002010-01-27 00:20:02 +0000140 unsigned bbNum = MI->getParent()->getNumber();
Chris Lattner840c7002009-09-15 17:46:24 +0000141
142 O << '\n' << ".LLGETPCH" << bbNum << ":\n";
143 O << "\tcall\t.LLGETPC" << bbNum << '\n' ;
144
145 O << "\t sethi\t"
146 << "%hi(_GLOBAL_OFFSET_TABLE_+(.-.LLGETPCH" << bbNum << ")), "
147 << operand << '\n' ;
148
149 O << ".LLGETPC" << bbNum << ":\n" ;
150 O << "\tor\t" << operand
151 << ", %lo(_GLOBAL_OFFSET_TABLE_+(.-.LLGETPCH" << bbNum << ")), "
152 << operand << '\n';
153 O << "\tadd\t" << operand << ", %o7, " << operand << '\n';
154
155 return true;
156}
157
Chris Lattner158e1f52006-02-05 05:50:24 +0000158void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
Chris Lattner5c463782007-12-30 20:49:49 +0000159 int CC = (int)MI->getOperand(opNum).getImm();
Chris Lattner158e1f52006-02-05 05:50:24 +0000160 O << SPARCCondCodeToString((SPCC::CondCodes)CC);
161}
162
Anton Korobeynikov3db21732008-10-10 10:15:03 +0000163/// PrintAsmOperand - Print out an operand for an inline asm expression.
164///
165bool SparcAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
166 unsigned AsmVariant,
167 const char *ExtraCode) {
Anton Korobeynikovb80b4852008-10-10 20:29:50 +0000168 if (ExtraCode && ExtraCode[0]) {
169 if (ExtraCode[1] != 0) return true; // Unknown modifier.
170
171 switch (ExtraCode[0]) {
172 default: return true; // Unknown modifier.
173 case 'r':
174 break;
175 }
176 }
Anton Korobeynikov3db21732008-10-10 10:15:03 +0000177
178 printOperand(MI, OpNo);
179
180 return false;
181}
182
183bool SparcAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
184 unsigned OpNo,
185 unsigned AsmVariant,
186 const char *ExtraCode) {
187 if (ExtraCode && ExtraCode[0])
188 return true; // Unknown modifier
189
190 O << '[';
191 printMemOperand(MI, OpNo);
192 O << ']';
193
194 return false;
195}
Douglas Gregor1b731d52009-06-16 20:12:29 +0000196
Bob Wilson5a495fe2009-06-23 23:59:40 +0000197// Force static initialization.
Daniel Dunbare8338102009-07-15 20:24:03 +0000198extern "C" void LLVMInitializeSparcAsmPrinter() {
Daniel Dunbar5680b4f2009-07-25 06:49:55 +0000199 RegisterAsmPrinter<SparcAsmPrinter> X(TheSparcTarget);
Daniel Dunbare8338102009-07-15 20:24:03 +0000200}