blob: e0cafcca7fe8f473858bb7e2db622d346148984f [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 Lattnerb0d44c32010-02-02 23:37:42 +000032 MCContext &Ctx, MCStreamer &Streamer,
33 const MCAsmInfo *T)
34 : AsmPrinter(O, TM, Ctx, Streamer, T) {}
Chris Lattner158e1f52006-02-05 05:50:24 +000035
36 virtual const char *getPassName() const {
37 return "Sparc Assembly Printer";
38 }
39
40 void printOperand(const MachineInstr *MI, int opNum);
Chris Lattnerfcb8a3a2006-02-10 07:35:42 +000041 void printMemOperand(const MachineInstr *MI, int opNum,
42 const char *Modifier = 0);
Chris Lattner158e1f52006-02-05 05:50:24 +000043 void printCCOperand(const MachineInstr *MI, int opNum);
44
Chris Lattnerfd97a332010-01-28 01:48:52 +000045 virtual void EmitInstruction(const MachineInstr *MI) {
46 printInstruction(MI);
Chris Lattner996ec842010-02-03 01:09:55 +000047 O << '\n';
Chris Lattnerfd97a332010-01-28 01:48:52 +000048 }
Chris Lattnerb94284b2009-08-08 01:32:19 +000049 void printInstruction(const MachineInstr *MI); // autogenerated.
Chris Lattnerad10b3b2009-09-13 20:19:22 +000050 static const char *getRegisterName(unsigned RegNo);
Chris Lattner06c5eed2009-09-13 20:08:00 +000051
Anton Korobeynikov3db21732008-10-10 10:15:03 +000052 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
53 unsigned AsmVariant, const char *ExtraCode);
54 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
55 unsigned AsmVariant, const char *ExtraCode);
Chris Lattner840c7002009-09-15 17:46:24 +000056
Chris Lattner840c7002009-09-15 17:46:24 +000057 bool printGetPCX(const MachineInstr *MI, unsigned OpNo);
Chris Lattner158e1f52006-02-05 05:50:24 +000058 };
59} // end of anonymous namespace
60
61#include "SparcGenAsmWriter.inc"
62
Chris Lattner158e1f52006-02-05 05:50:24 +000063void SparcAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
64 const MachineOperand &MO = MI->getOperand (opNum);
Chris Lattner158e1f52006-02-05 05:50:24 +000065 bool CloseParen = false;
Dan Gohman0d1e9a82008-10-03 15:45:36 +000066 if (MI->getOpcode() == SP::SETHIi && !MO.isReg() && !MO.isImm()) {
Chris Lattner158e1f52006-02-05 05:50:24 +000067 O << "%hi(";
68 CloseParen = true;
Dan Gohman0d1e9a82008-10-03 15:45:36 +000069 } else if ((MI->getOpcode() == SP::ORri || MI->getOpcode() == SP::ADDri) &&
70 !MO.isReg() && !MO.isImm()) {
Chris Lattner158e1f52006-02-05 05:50:24 +000071 O << "%lo(";
72 CloseParen = true;
73 }
74 switch (MO.getType()) {
Chris Lattner10b71c02006-05-04 18:05:43 +000075 case MachineOperand::MO_Register:
Chris Lattnereb2cc682009-09-13 20:31:40 +000076 O << "%" << LowercaseString(getRegisterName(MO.getReg()));
Chris Lattner158e1f52006-02-05 05:50:24 +000077 break;
78
Chris Lattnerfef7a2d2006-05-04 17:21:20 +000079 case MachineOperand::MO_Immediate:
Chris Lattner5c463782007-12-30 20:49:49 +000080 O << (int)MO.getImm();
Chris Lattner158e1f52006-02-05 05:50:24 +000081 break;
Nate Begeman4ca2ea52006-04-22 18:53:45 +000082 case MachineOperand::MO_MachineBasicBlock:
Chris Lattnerd051af72010-01-26 04:55:51 +000083 O << *MO.getMBB()->getSymbol(OutContext);
Chris Lattner158e1f52006-02-05 05:50:24 +000084 return;
Chris Lattner158e1f52006-02-05 05:50:24 +000085 case MachineOperand::MO_GlobalAddress:
Chris Lattner8b5d55e2010-01-17 21:43:43 +000086 O << *GetGlobalValueSymbol(MO.getGlobal());
Chris Lattner158e1f52006-02-05 05:50:24 +000087 break;
88 case MachineOperand::MO_ExternalSymbol:
89 O << MO.getSymbolName();
90 break;
91 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattnere9a75a62009-08-22 21:43:10 +000092 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Chris Lattnera5bb3702007-12-30 23:10:15 +000093 << MO.getIndex();
Chris Lattner158e1f52006-02-05 05:50:24 +000094 break;
95 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +000096 llvm_unreachable("<unknown operand type>");
Chris Lattner158e1f52006-02-05 05:50:24 +000097 }
98 if (CloseParen) O << ")";
99}
100
Chris Lattnerfcb8a3a2006-02-10 07:35:42 +0000101void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
102 const char *Modifier) {
Chris Lattner158e1f52006-02-05 05:50:24 +0000103 printOperand(MI, opNum);
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000104
Chris Lattnerfcb8a3a2006-02-10 07:35:42 +0000105 // If this is an ADD operand, emit it like normal operands.
106 if (Modifier && !strcmp(Modifier, "arith")) {
107 O << ", ";
108 printOperand(MI, opNum+1);
109 return;
110 }
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000111
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000112 if (MI->getOperand(opNum+1).isReg() &&
Chris Lattner158e1f52006-02-05 05:50:24 +0000113 MI->getOperand(opNum+1).getReg() == SP::G0)
114 return; // don't print "+%g0"
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000115 if (MI->getOperand(opNum+1).isImm() &&
Chris Lattner5c463782007-12-30 20:49:49 +0000116 MI->getOperand(opNum+1).getImm() == 0)
Chris Lattner158e1f52006-02-05 05:50:24 +0000117 return; // don't print "+0"
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000118
Chris Lattner158e1f52006-02-05 05:50:24 +0000119 O << "+";
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000120 if (MI->getOperand(opNum+1).isGlobal() ||
121 MI->getOperand(opNum+1).isCPI()) {
Chris Lattner158e1f52006-02-05 05:50:24 +0000122 O << "%lo(";
123 printOperand(MI, opNum+1);
124 O << ")";
125 } else {
126 printOperand(MI, opNum+1);
127 }
128}
129
Chris Lattner840c7002009-09-15 17:46:24 +0000130bool SparcAsmPrinter::printGetPCX(const MachineInstr *MI, unsigned opNum) {
131 std::string operand = "";
132 const MachineOperand &MO = MI->getOperand(opNum);
133 switch (MO.getType()) {
134 default: assert(0 && "Operand is not a register ");
135 case MachineOperand::MO_Register:
136 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
137 "Operand is not a physical register ");
138 operand = "%" + LowercaseString(getRegisterName(MO.getReg()));
139 break;
140 }
141
Chris Lattner2ab11002010-01-27 00:20:02 +0000142 unsigned bbNum = MI->getParent()->getNumber();
Chris Lattner840c7002009-09-15 17:46:24 +0000143
144 O << '\n' << ".LLGETPCH" << bbNum << ":\n";
145 O << "\tcall\t.LLGETPC" << bbNum << '\n' ;
146
147 O << "\t sethi\t"
148 << "%hi(_GLOBAL_OFFSET_TABLE_+(.-.LLGETPCH" << bbNum << ")), "
149 << operand << '\n' ;
150
151 O << ".LLGETPC" << bbNum << ":\n" ;
152 O << "\tor\t" << operand
153 << ", %lo(_GLOBAL_OFFSET_TABLE_+(.-.LLGETPCH" << bbNum << ")), "
154 << operand << '\n';
155 O << "\tadd\t" << operand << ", %o7, " << operand << '\n';
156
157 return true;
158}
159
Chris Lattner158e1f52006-02-05 05:50:24 +0000160void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
Chris Lattner5c463782007-12-30 20:49:49 +0000161 int CC = (int)MI->getOperand(opNum).getImm();
Chris Lattner158e1f52006-02-05 05:50:24 +0000162 O << SPARCCondCodeToString((SPCC::CondCodes)CC);
163}
164
Anton Korobeynikov3db21732008-10-10 10:15:03 +0000165/// PrintAsmOperand - Print out an operand for an inline asm expression.
166///
167bool SparcAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
168 unsigned AsmVariant,
169 const char *ExtraCode) {
Anton Korobeynikovb80b4852008-10-10 20:29:50 +0000170 if (ExtraCode && ExtraCode[0]) {
171 if (ExtraCode[1] != 0) return true; // Unknown modifier.
172
173 switch (ExtraCode[0]) {
174 default: return true; // Unknown modifier.
175 case 'r':
176 break;
177 }
178 }
Anton Korobeynikov3db21732008-10-10 10:15:03 +0000179
180 printOperand(MI, OpNo);
181
182 return false;
183}
184
185bool SparcAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
186 unsigned OpNo,
187 unsigned AsmVariant,
188 const char *ExtraCode) {
189 if (ExtraCode && ExtraCode[0])
190 return true; // Unknown modifier
191
192 O << '[';
193 printMemOperand(MI, OpNo);
194 O << ']';
195
196 return false;
197}
Douglas Gregor1b731d52009-06-16 20:12:29 +0000198
Bob Wilson5a495fe2009-06-23 23:59:40 +0000199// Force static initialization.
Daniel Dunbare8338102009-07-15 20:24:03 +0000200extern "C" void LLVMInitializeSparcAsmPrinter() {
Daniel Dunbar5680b4f2009-07-25 06:49:55 +0000201 RegisterAsmPrinter<SparcAsmPrinter> X(TheSparcTarget);
Chris Lattner8228b112010-02-04 06:34:01 +0000202 RegisterAsmPrinter<SparcAsmPrinter> Y(TheSparcV9Target);
Daniel Dunbare8338102009-07-15 20:24:03 +0000203}