blob: b8469a15be7f1fb9c72f046f4021e90865c8c6aa [file] [log] [blame]
Chris Lattner97f06932009-10-19 20:20:46 +00001//===-- ARMAsmPrinter.cpp - Print machine code to an ARM .s file ----------===//
2//
Rafael Espindola7bc59bc2006-05-14 22:18:28 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
Rafael Espindola7bc59bc2006-05-14 22:18:28 +00006// 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 ARM assembly language.
12//
13//===----------------------------------------------------------------------===//
14
Chris Lattner95b2c7d2006-12-19 22:59:26 +000015#define DEBUG_TYPE "asm-printer"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000016#include "ARM.h"
Anton Korobeynikov88ce6672009-05-23 19:51:20 +000017#include "ARMBuildAttrs.h"
Evan Chenga8e29892007-01-19 07:51:42 +000018#include "ARMAddressingModes.h"
19#include "ARMConstantPoolValue.h"
Chris Lattnerf447a5f2010-07-19 23:44:46 +000020#include "AsmPrinter/ARMInstPrinter.h"
Chris Lattner97f06932009-10-19 20:20:46 +000021#include "ARMMachineFunctionInfo.h"
22#include "ARMMCInstLower.h"
23#include "ARMTargetMachine.h"
Dale Johannesen3f282aa2010-04-26 20:07:31 +000024#include "llvm/Analysis/DebugInfo.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000025#include "llvm/Constants.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000026#include "llvm/Module.h"
Benjamin Kramere55b15f2009-12-28 12:27:56 +000027#include "llvm/Type.h"
Dan Gohmancf20ac42009-08-13 01:36:44 +000028#include "llvm/Assembly/Writer.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000029#include "llvm/CodeGen/AsmPrinter.h"
Chris Lattnerb0f294c2009-10-19 18:38:33 +000030#include "llvm/CodeGen/MachineModuleInfoImpls.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000031#include "llvm/CodeGen/MachineFunctionPass.h"
Evan Chenga8e29892007-01-19 07:51:42 +000032#include "llvm/CodeGen/MachineJumpTableInfo.h"
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000033#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Chris Lattnerb0f294c2009-10-19 18:38:33 +000034#include "llvm/MC/MCAsmInfo.h"
35#include "llvm/MC/MCContext.h"
Bill Wendlingbecd83e2010-03-09 00:40:17 +000036#include "llvm/MC/MCExpr.h"
Chris Lattner97f06932009-10-19 20:20:46 +000037#include "llvm/MC/MCInst.h"
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000038#include "llvm/MC/MCSectionMachO.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000039#include "llvm/MC/MCStreamer.h"
Chris Lattner325d3dc2009-09-13 17:14:04 +000040#include "llvm/MC/MCSymbol.h"
Chris Lattnerd62f1b42010-03-12 21:19:23 +000041#include "llvm/Target/Mangler.h"
Rafael Espindolab01c4bb2006-07-27 11:38:51 +000042#include "llvm/Target/TargetData.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000043#include "llvm/Target/TargetMachine.h"
Evan Cheng5be54b02007-01-19 19:25:36 +000044#include "llvm/Target/TargetOptions.h"
Daniel Dunbar51b198a2009-07-15 20:24:03 +000045#include "llvm/Target/TargetRegistry.h"
Evan Chengc324ecb2009-07-24 18:19:46 +000046#include "llvm/ADT/SmallPtrSet.h"
Jim Grosbachc40d9f92009-09-01 18:49:12 +000047#include "llvm/ADT/SmallString.h"
Bob Wilson54c78ef2009-11-06 23:33:28 +000048#include "llvm/ADT/StringExtras.h"
Chris Lattner97f06932009-10-19 20:20:46 +000049#include "llvm/Support/CommandLine.h"
Devang Patel59135f42010-08-04 22:39:39 +000050#include "llvm/Support/Debug.h"
Torok Edwin30464702009-07-08 20:55:50 +000051#include "llvm/Support/ErrorHandling.h"
Chris Lattnerb23569a2010-04-04 08:18:47 +000052#include "llvm/Support/raw_ostream.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000053#include <cctype>
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000054using namespace llvm;
55
Jim Grosbach91729002010-07-21 23:03:52 +000056namespace llvm {
57 namespace ARM {
58 enum DW_ISA {
59 DW_ISA_ARM_thumb = 1,
60 DW_ISA_ARM_arm = 2
61 };
62 }
63}
64
Chris Lattner95b2c7d2006-12-19 22:59:26 +000065namespace {
Chris Lattner4a071d62009-10-19 17:59:19 +000066 class ARMAsmPrinter : public AsmPrinter {
Evan Chenga8e29892007-01-19 07:51:42 +000067
68 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
69 /// make the right decision when printing asm code for different targets.
70 const ARMSubtarget *Subtarget;
71
72 /// AFI - Keep a pointer to ARMFunctionInfo for the current
Evan Cheng6d63a722008-09-18 07:27:23 +000073 /// MachineFunction.
Evan Chenga8e29892007-01-19 07:51:42 +000074 ARMFunctionInfo *AFI;
75
Evan Cheng6d63a722008-09-18 07:27:23 +000076 /// MCP - Keep a pointer to constantpool entries of the current
77 /// MachineFunction.
78 const MachineConstantPool *MCP;
79
Bill Wendling57f0db82009-02-24 08:30:20 +000080 public:
Chris Lattnerb23569a2010-04-04 08:18:47 +000081 explicit ARMAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
82 : AsmPrinter(TM, Streamer), AFI(NULL), MCP(NULL) {
Bill Wendling57f0db82009-02-24 08:30:20 +000083 Subtarget = &TM.getSubtarget<ARMSubtarget>();
84 }
85
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000086 virtual const char *getPassName() const {
87 return "ARM Assembly Printer";
88 }
Jim Grosbachb0739b72010-09-02 01:02:06 +000089
Chris Lattner35c33bd2010-04-04 04:47:45 +000090 void printOperand(const MachineInstr *MI, int OpNum, raw_ostream &O,
Evan Chenga8e29892007-01-19 07:51:42 +000091 const char *Modifier = 0);
Bob Wilson54c78ef2009-11-06 23:33:28 +000092
Evan Cheng055b0312009-06-29 07:51:04 +000093 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
Chris Lattnerc75c0282010-04-04 05:29:35 +000094 unsigned AsmVariant, const char *ExtraCode,
95 raw_ostream &O);
Evan Cheng055b0312009-06-29 07:51:04 +000096 virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNum,
Bob Wilson224c2442009-05-19 05:53:42 +000097 unsigned AsmVariant,
Chris Lattnerc75c0282010-04-04 05:29:35 +000098 const char *ExtraCode, raw_ostream &O);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000099
Jim Grosbach2317e402010-09-30 01:57:53 +0000100 void EmitJumpTable(const MachineInstr *MI);
101 void EmitJump2Table(const MachineInstr *MI);
Chris Lattnera786cea2010-01-28 01:10:34 +0000102 virtual void EmitInstruction(const MachineInstr *MI);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000103 bool runOnMachineFunction(MachineFunction &F);
Jim Grosbachb0739b72010-09-02 01:02:06 +0000104
Chris Lattnera2406192010-01-28 00:19:24 +0000105 virtual void EmitConstantPool() {} // we emit constant pools customly!
Chris Lattner953ebb72010-01-27 23:58:11 +0000106 virtual void EmitFunctionEntryLabel();
Bob Wilson812209a2009-09-30 22:06:26 +0000107 void EmitStartOfAsmFile(Module &M);
Chris Lattner4a071d62009-10-19 17:59:19 +0000108 void EmitEndOfAsmFile(Module &M);
Evan Chenga8e29892007-01-19 07:51:42 +0000109
Jim Grosbach2d0f53b2010-09-28 17:05:56 +0000110 void PrintDebugValueComment(const MachineInstr *MI, raw_ostream &OS);
111
Devang Patel59135f42010-08-04 22:39:39 +0000112 MachineLocation getDebugValueLocation(const MachineInstr *MI) const {
113 MachineLocation Location;
114 assert (MI->getNumOperands() == 4 && "Invalid no. of machine operands!");
115 // Frame address. Currently handles register +- offset only.
116 if (MI->getOperand(0).isReg() && MI->getOperand(1).isImm())
117 Location.set(MI->getOperand(0).getReg(), MI->getOperand(1).getImm());
118 else {
119 DEBUG(dbgs() << "DBG_VALUE instruction ignored! " << *MI << "\n");
120 }
121 return Location;
122 }
123
Jim Grosbach91729002010-07-21 23:03:52 +0000124 virtual unsigned getISAEncoding() {
125 // ARM/Darwin adds ISA to the DWARF info for each function.
126 if (!Subtarget->isTargetDarwin())
127 return 0;
128 return Subtarget->isThumb() ?
129 llvm::ARM::DW_ISA_ARM_thumb : llvm::ARM::DW_ISA_ARM_arm;
130 }
131
Chris Lattner0890cf12010-01-25 19:51:38 +0000132 MCSymbol *GetARMSetPICJumpTableLabel2(unsigned uid, unsigned uid2,
133 const MachineBasicBlock *MBB) const;
134 MCSymbol *GetARMJTIPICJumpTableLabel2(unsigned uid, unsigned uid2) const;
Chris Lattnerbfcb0962010-01-25 19:39:52 +0000135
Jim Grosbach433a5782010-09-24 20:47:58 +0000136 MCSymbol *GetARMSJLJEHLabel(void) const;
137
Evan Cheng711b6dc2008-08-08 06:56:16 +0000138 /// EmitMachineConstantPoolValue - Print a machine constantpool value to
139 /// the .s file.
Evan Chenga8e29892007-01-19 07:51:42 +0000140 virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
Chris Lattner9d7efd32010-04-04 07:05:53 +0000141 SmallString<128> Str;
142 raw_svector_ostream OS(Str);
143 EmitMachineConstantPoolValue(MCPV, OS);
144 OutStreamer.EmitRawText(OS.str());
145 }
Jim Grosbachb0739b72010-09-02 01:02:06 +0000146
Chris Lattner9d7efd32010-04-04 07:05:53 +0000147 void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV,
148 raw_ostream &O) {
Chris Lattnerea3cb402010-01-20 07:33:29 +0000149 switch (TM.getTargetData()->getTypeAllocSize(MCPV->getType())) {
150 case 1: O << MAI->getData8bitsDirective(0); break;
151 case 2: O << MAI->getData16bitsDirective(0); break;
152 case 4: O << MAI->getData32bitsDirective(0); break;
153 default: assert(0 && "Unknown CPV size");
154 }
Evan Chenga8e29892007-01-19 07:51:42 +0000155
Evan Cheng711b6dc2008-08-08 06:56:16 +0000156 ARMConstantPoolValue *ACPV = static_cast<ARMConstantPoolValue*>(MCPV);
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +0000157
158 if (ACPV->isLSDA()) {
Chris Lattner9d7efd32010-04-04 07:05:53 +0000159 O << MAI->getPrivateGlobalPrefix() << "_LSDA_" << getFunctionNumber();
Bob Wilson28989a82009-11-02 16:59:06 +0000160 } else if (ACPV->isBlockAddress()) {
Chris Lattner0752cda2010-04-05 16:32:14 +0000161 O << *GetBlockAddressSymbol(ACPV->getBlockAddress());
Bob Wilson28989a82009-11-02 16:59:06 +0000162 } else if (ACPV->isGlobalValue()) {
Dan Gohman46510a72010-04-15 01:51:59 +0000163 const GlobalValue *GV = ACPV->getGV();
Evan Chenge4e4ed32009-08-28 23:18:09 +0000164 bool isIndirect = Subtarget->isTargetDarwin() &&
Evan Cheng63476a82009-09-03 07:04:02 +0000165 Subtarget->GVIsIndirectSymbol(GV, TM.getRelocationModel());
Evan Chenge4e4ed32009-08-28 23:18:09 +0000166 if (!isIndirect)
Chris Lattnerd62f1b42010-03-12 21:19:23 +0000167 O << *Mang->getSymbol(GV);
Evan Chenge4e4ed32009-08-28 23:18:09 +0000168 else {
169 // FIXME: Remove this when Darwin transition to @GOT like syntax.
Chris Lattner7a2ba942010-01-16 18:37:32 +0000170 MCSymbol *Sym = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
Chris Lattner10b318b2010-01-17 21:43:43 +0000171 O << *Sym;
Jim Grosbachb0739b72010-09-02 01:02:06 +0000172
Chris Lattnerb8f64a72009-10-19 18:49:14 +0000173 MachineModuleInfoMachO &MMIMachO =
174 MMI->getObjFileInfo<MachineModuleInfoMachO>();
Bill Wendlingcebae362010-03-10 22:34:10 +0000175 MachineModuleInfoImpl::StubValueTy &StubSym =
Chris Lattnerb8f64a72009-10-19 18:49:14 +0000176 GV->hasHiddenVisibility() ? MMIMachO.getHiddenGVStubEntry(Sym) :
177 MMIMachO.getGVStubEntry(Sym);
Bill Wendlingcebae362010-03-10 22:34:10 +0000178 if (StubSym.getPointer() == 0)
179 StubSym = MachineModuleInfoImpl::
Chris Lattnerd62f1b42010-03-12 21:19:23 +0000180 StubValueTy(Mang->getSymbol(GV), !GV->hasInternalLinkage());
Evan Chenge4e4ed32009-08-28 23:18:09 +0000181 }
Bob Wilson28989a82009-11-02 16:59:06 +0000182 } else {
183 assert(ACPV->isExtSymbol() && "unrecognized constant pool value");
Chris Lattner10b318b2010-01-17 21:43:43 +0000184 O << *GetExternalSymbolSymbol(ACPV->getSymbol());
Bob Wilson28989a82009-11-02 16:59:06 +0000185 }
Jim Grosbache9952212009-09-04 01:38:51 +0000186
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000187 if (ACPV->hasModifier()) O << "(" << ACPV->getModifier() << ")";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000188 if (ACPV->getPCAdjustment() != 0) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000189 O << "-(" << MAI->getPrivateGlobalPrefix() << "PC"
Evan Chenge7e0d622009-11-06 22:24:13 +0000190 << getFunctionNumber() << "_" << ACPV->getLabelId()
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000191 << "+" << (unsigned)ACPV->getPCAdjustment();
192 if (ACPV->mustAddCurrentAddress())
193 O << "-.";
Chris Lattner8b378752010-01-15 23:26:49 +0000194 O << ')';
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000195 }
Evan Chenga8e29892007-01-19 07:51:42 +0000196 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000197 };
198} // end of anonymous namespace
199
Chris Lattner953ebb72010-01-27 23:58:11 +0000200void ARMAsmPrinter::EmitFunctionEntryLabel() {
201 if (AFI->isThumbFunction()) {
Chris Lattner9d7efd32010-04-04 07:05:53 +0000202 OutStreamer.EmitRawText(StringRef("\t.code\t16"));
Chris Lattner0752cda2010-04-05 16:32:14 +0000203 if (!Subtarget->isTargetDarwin())
Chris Lattner9d7efd32010-04-04 07:05:53 +0000204 OutStreamer.EmitRawText(StringRef("\t.thumb_func"));
Chris Lattner0752cda2010-04-05 16:32:14 +0000205 else {
206 // This needs to emit to a temporary string to get properly quoted
207 // MCSymbols when they have spaces in them.
208 SmallString<128> Tmp;
209 raw_svector_ostream OS(Tmp);
210 OS << "\t.thumb_func\t" << *CurrentFnSym;
211 OutStreamer.EmitRawText(OS.str());
212 }
Chris Lattner953ebb72010-01-27 23:58:11 +0000213 }
Jim Grosbachb0739b72010-09-02 01:02:06 +0000214
Chris Lattner953ebb72010-01-27 23:58:11 +0000215 OutStreamer.EmitLabel(CurrentFnSym);
216}
217
Jim Grosbach2317e402010-09-30 01:57:53 +0000218/// runOnMachineFunction - This uses the EmitInstruction()
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000219/// method to print assembly for each instruction.
220///
221bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Evan Chenga8e29892007-01-19 07:51:42 +0000222 AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng6d63a722008-09-18 07:27:23 +0000223 MCP = MF.getConstantPool();
Rafael Espindola4b442b52006-05-23 02:48:20 +0000224
Chris Lattnerd49fe1b2010-01-28 01:28:58 +0000225 return AsmPrinter::runOnMachineFunction(MF);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000226}
227
Evan Cheng055b0312009-06-29 07:51:04 +0000228void ARMAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
Chris Lattner35c33bd2010-04-04 04:47:45 +0000229 raw_ostream &O, const char *Modifier) {
Evan Cheng055b0312009-06-29 07:51:04 +0000230 const MachineOperand &MO = MI->getOperand(OpNum);
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000231 unsigned TF = MO.getTargetFlags();
232
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000233 switch (MO.getType()) {
Chris Lattner8bc86cb2009-10-19 20:59:55 +0000234 default:
235 assert(0 && "<unknown operand type>");
Bob Wilson5bafff32009-06-22 23:27:02 +0000236 case MachineOperand::MO_Register: {
237 unsigned Reg = MO.getReg();
Chris Lattner8bc86cb2009-10-19 20:59:55 +0000238 assert(TargetRegisterInfo::isPhysicalRegister(Reg));
Bob Wilsonde0ae8f2010-09-16 04:55:00 +0000239 if (Modifier && strcmp(Modifier, "lane") == 0) {
Jim Grosbacha4c3c8f2010-09-15 20:26:25 +0000240 unsigned RegNum = getARMRegisterNumbering(Reg);
Chris Lattner9d1c1ad2010-04-04 18:06:11 +0000241 unsigned DReg =
Jakob Stoklund Olesene00fa642010-05-25 00:15:15 +0000242 TM.getRegisterInfo()->getMatchingSuperReg(Reg,
243 RegNum & 1 ? ARM::ssub_1 : ARM::ssub_0, &ARM::DPR_VFP2RegClass);
Jim Grosbach2317e402010-09-30 01:57:53 +0000244 O << ARMInstPrinter::getRegisterName(DReg) << '[' << (RegNum & 1) << ']';
Chris Lattner8bc86cb2009-10-19 20:59:55 +0000245 } else {
Anton Korobeynikove8ea0112009-11-07 15:20:32 +0000246 assert(!MO.getSubReg() && "Subregs should be eliminated!");
Jim Grosbach2317e402010-09-30 01:57:53 +0000247 O << ARMInstPrinter::getRegisterName(Reg);
Chris Lattner8bc86cb2009-10-19 20:59:55 +0000248 }
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000249 break;
Bob Wilson5bafff32009-06-22 23:27:02 +0000250 }
Evan Chenga8e29892007-01-19 07:51:42 +0000251 case MachineOperand::MO_Immediate: {
Evan Cheng5adb66a2009-09-28 09:14:39 +0000252 int64_t Imm = MO.getImm();
Anton Korobeynikov632606c2009-10-08 20:43:22 +0000253 O << '#';
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000254 if ((Modifier && strcmp(Modifier, "lo16") == 0) ||
255 (TF & ARMII::MO_LO16))
256 O << ":lower16:";
257 else if ((Modifier && strcmp(Modifier, "hi16") == 0) ||
258 (TF & ARMII::MO_HI16))
259 O << ":upper16:";
Anton Korobeynikov632606c2009-10-08 20:43:22 +0000260 O << Imm;
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000261 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000262 }
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000263 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner1b2eb0e2010-03-13 21:04:28 +0000264 O << *MO.getMBB()->getSymbol();
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000265 return;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000266 case MachineOperand::MO_GlobalAddress: {
Evan Chenga8e29892007-01-19 07:51:42 +0000267 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Dan Gohman46510a72010-04-15 01:51:59 +0000268 const GlobalValue *GV = MO.getGlobal();
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000269
270 if ((Modifier && strcmp(Modifier, "lo16") == 0) ||
271 (TF & ARMII::MO_LO16))
272 O << ":lower16:";
273 else if ((Modifier && strcmp(Modifier, "hi16") == 0) ||
274 (TF & ARMII::MO_HI16))
275 O << ":upper16:";
Chris Lattnerd62f1b42010-03-12 21:19:23 +0000276 O << *Mang->getSymbol(GV);
Anton Korobeynikov7751ad92008-11-22 16:15:34 +0000277
Chris Lattner0c08d092010-04-03 22:28:33 +0000278 printOffset(MO.getOffset(), O);
Anton Korobeynikov7751ad92008-11-22 16:15:34 +0000279
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000280 if (isCallOp && Subtarget->isTargetELF() &&
281 TM.getRelocationModel() == Reloc::PIC_)
282 O << "(PLT)";
Evan Chenga8e29892007-01-19 07:51:42 +0000283 break;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000284 }
Evan Chenga8e29892007-01-19 07:51:42 +0000285 case MachineOperand::MO_ExternalSymbol: {
286 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Chris Lattner10b318b2010-01-17 21:43:43 +0000287 O << *GetExternalSymbolSymbol(MO.getSymbolName());
Jim Grosbachb0739b72010-09-02 01:02:06 +0000288
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000289 if (isCallOp && Subtarget->isTargetELF() &&
290 TM.getRelocationModel() == Reloc::PIC_)
291 O << "(PLT)";
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000292 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000293 }
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000294 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner1b46f432010-01-23 07:00:21 +0000295 O << *GetCPISymbol(MO.getIndex());
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000296 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000297 case MachineOperand::MO_JumpTableIndex:
Chris Lattner1b46f432010-01-23 07:00:21 +0000298 O << *GetJTISymbol(MO.getIndex());
Evan Chenga8e29892007-01-19 07:51:42 +0000299 break;
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000300 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000301}
302
Evan Cheng055b0312009-06-29 07:51:04 +0000303//===--------------------------------------------------------------------===//
304
Chris Lattner0890cf12010-01-25 19:51:38 +0000305MCSymbol *ARMAsmPrinter::
306GetARMSetPICJumpTableLabel2(unsigned uid, unsigned uid2,
307 const MachineBasicBlock *MBB) const {
308 SmallString<60> Name;
309 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix()
Chris Lattnerbfcb0962010-01-25 19:39:52 +0000310 << getFunctionNumber() << '_' << uid << '_' << uid2
Chris Lattner0890cf12010-01-25 19:51:38 +0000311 << "_set_" << MBB->getNumber();
Chris Lattner9b97a732010-03-30 18:10:53 +0000312 return OutContext.GetOrCreateSymbol(Name.str());
Chris Lattner0890cf12010-01-25 19:51:38 +0000313}
314
315MCSymbol *ARMAsmPrinter::
316GetARMJTIPICJumpTableLabel2(unsigned uid, unsigned uid2) const {
317 SmallString<60> Name;
318 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix() << "JTI"
Chris Lattner281e7762010-01-25 23:28:03 +0000319 << getFunctionNumber() << '_' << uid << '_' << uid2;
Chris Lattner9b97a732010-03-30 18:10:53 +0000320 return OutContext.GetOrCreateSymbol(Name.str());
Chris Lattnerbfcb0962010-01-25 19:39:52 +0000321}
322
Jim Grosbach433a5782010-09-24 20:47:58 +0000323
324MCSymbol *ARMAsmPrinter::GetARMSJLJEHLabel(void) const {
325 SmallString<60> Name;
326 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix() << "SJLJEH"
327 << getFunctionNumber();
328 return OutContext.GetOrCreateSymbol(Name.str());
329}
330
Evan Cheng055b0312009-06-29 07:51:04 +0000331bool ARMAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
Chris Lattnerc75c0282010-04-04 05:29:35 +0000332 unsigned AsmVariant, const char *ExtraCode,
333 raw_ostream &O) {
Evan Chenga8e29892007-01-19 07:51:42 +0000334 // Does this asm operand have a single letter operand modifier?
335 if (ExtraCode && ExtraCode[0]) {
336 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov8e9ece72009-08-08 23:10:41 +0000337
Evan Chenga8e29892007-01-19 07:51:42 +0000338 switch (ExtraCode[0]) {
339 default: return true; // Unknown modifier.
Bob Wilson9b4b00a2009-07-09 23:54:51 +0000340 case 'a': // Print as a memory address.
341 if (MI->getOperand(OpNum).isReg()) {
Jim Grosbach2317e402010-09-30 01:57:53 +0000342 O << "[" << ARMInstPrinter::getRegisterName(MI->getOperand(OpNum).getReg()) << "]";
Bob Wilson9b4b00a2009-07-09 23:54:51 +0000343 return false;
344 }
345 // Fallthrough
346 case 'c': // Don't print "#" before an immediate operand.
Bob Wilson4f38b382009-08-21 21:58:55 +0000347 if (!MI->getOperand(OpNum).isImm())
348 return true;
Jim Grosbach2317e402010-09-30 01:57:53 +0000349 O << MI->getOperand(OpNum).getImm();
Bob Wilson8f343462009-04-06 21:46:51 +0000350 return false;
Evan Chenge21e3962007-04-04 00:13:29 +0000351 case 'P': // Print a VFP double precision register.
Evan Chengd831cda2009-12-08 23:06:22 +0000352 case 'q': // Print a NEON quad precision register.
Chris Lattner35c33bd2010-04-04 04:47:45 +0000353 printOperand(MI, OpNum, O);
Evan Cheng23a95702007-03-08 22:42:46 +0000354 return false;
Evan Chenga8e29892007-01-19 07:51:42 +0000355 case 'Q':
Bob Wilsond984eb62010-05-27 20:23:42 +0000356 case 'R':
Bob Wilsond984eb62010-05-27 20:23:42 +0000357 case 'H':
Evan Cheng12616722010-05-27 23:45:31 +0000358 report_fatal_error("llvm does not support 'Q', 'R', and 'H' modifiers!");
Bob Wilsond984eb62010-05-27 20:23:42 +0000359 return true;
Evan Cheng84f60b72010-05-27 22:08:38 +0000360 }
Evan Chenga8e29892007-01-19 07:51:42 +0000361 }
Jim Grosbache9952212009-09-04 01:38:51 +0000362
Chris Lattner35c33bd2010-04-04 04:47:45 +0000363 printOperand(MI, OpNum, O);
Evan Chenga8e29892007-01-19 07:51:42 +0000364 return false;
365}
366
Bob Wilson224c2442009-05-19 05:53:42 +0000367bool ARMAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Evan Cheng055b0312009-06-29 07:51:04 +0000368 unsigned OpNum, unsigned AsmVariant,
Chris Lattnerc75c0282010-04-04 05:29:35 +0000369 const char *ExtraCode,
370 raw_ostream &O) {
Bob Wilson224c2442009-05-19 05:53:42 +0000371 if (ExtraCode && ExtraCode[0])
372 return true; // Unknown modifier.
Bob Wilson765cc0b2009-10-13 20:50:28 +0000373
374 const MachineOperand &MO = MI->getOperand(OpNum);
375 assert(MO.isReg() && "unexpected inline asm memory operand");
Jim Grosbach2317e402010-09-30 01:57:53 +0000376 O << "[" << ARMInstPrinter::getRegisterName(MO.getReg()) << "]";
Bob Wilson224c2442009-05-19 05:53:42 +0000377 return false;
378}
379
Bob Wilson812209a2009-09-30 22:06:26 +0000380void ARMAsmPrinter::EmitStartOfAsmFile(Module &M) {
Bob Wilson0fb34682009-09-30 00:23:42 +0000381 if (Subtarget->isTargetDarwin()) {
382 Reloc::Model RelocM = TM.getRelocationModel();
383 if (RelocM == Reloc::PIC_ || RelocM == Reloc::DynamicNoPIC) {
384 // Declare all the text sections up front (before the DWARF sections
385 // emitted by AsmPrinter::doInitialization) so the assembler will keep
386 // them together at the beginning of the object file. This helps
387 // avoid out-of-range branches that are due a fundamental limitation of
388 // the way symbol offsets are encoded with the current Darwin ARM
389 // relocations.
Jim Grosbachb0739b72010-09-02 01:02:06 +0000390 const TargetLoweringObjectFileMachO &TLOFMacho =
Dan Gohman0d805c32010-04-17 16:44:48 +0000391 static_cast<const TargetLoweringObjectFileMachO &>(
392 getObjFileLowering());
Bob Wilson29e06692009-09-30 22:25:37 +0000393 OutStreamer.SwitchSection(TLOFMacho.getTextSection());
394 OutStreamer.SwitchSection(TLOFMacho.getTextCoalSection());
395 OutStreamer.SwitchSection(TLOFMacho.getConstTextCoalSection());
396 if (RelocM == Reloc::DynamicNoPIC) {
397 const MCSection *sect =
Chris Lattner22772212010-04-08 20:40:11 +0000398 OutContext.getMachOSection("__TEXT", "__symbol_stub4",
399 MCSectionMachO::S_SYMBOL_STUBS,
400 12, SectionKind::getText());
Bob Wilson29e06692009-09-30 22:25:37 +0000401 OutStreamer.SwitchSection(sect);
402 } else {
403 const MCSection *sect =
Chris Lattner22772212010-04-08 20:40:11 +0000404 OutContext.getMachOSection("__TEXT", "__picsymbolstub4",
405 MCSectionMachO::S_SYMBOL_STUBS,
406 16, SectionKind::getText());
Bob Wilson29e06692009-09-30 22:25:37 +0000407 OutStreamer.SwitchSection(sect);
408 }
Bob Wilson63db5942010-07-30 19:55:47 +0000409 const MCSection *StaticInitSect =
410 OutContext.getMachOSection("__TEXT", "__StaticInit",
411 MCSectionMachO::S_REGULAR |
412 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
413 SectionKind::getText());
414 OutStreamer.SwitchSection(StaticInitSect);
Bob Wilson0fb34682009-09-30 00:23:42 +0000415 }
416 }
417
Jim Grosbache5165492009-11-09 00:11:35 +0000418 // Use unified assembler syntax.
Jason W Kimafd1cc22010-09-30 02:45:56 +0000419 OutStreamer.EmitAssemblerFlag(MCAF_SyntaxUnified);
Anton Korobeynikovd61eca52009-06-17 23:43:18 +0000420
Anton Korobeynikov88ce6672009-05-23 19:51:20 +0000421 // Emit ARM Build Attributes
422 if (Subtarget->isTargetELF()) {
423 // CPU Type
Anton Korobeynikovd260c242009-06-01 19:03:17 +0000424 std::string CPUString = Subtarget->getCPUString();
425 if (CPUString != "generic")
Chris Lattner9d7efd32010-04-04 07:05:53 +0000426 OutStreamer.EmitRawText("\t.cpu " + Twine(CPUString));
Anton Korobeynikov88ce6672009-05-23 19:51:20 +0000427
428 // FIXME: Emit FPU type
429 if (Subtarget->hasVFP2())
Chris Lattner9d7efd32010-04-04 07:05:53 +0000430 OutStreamer.EmitRawText("\t.eabi_attribute " +
431 Twine(ARMBuildAttrs::VFP_arch) + ", 2");
Anton Korobeynikov88ce6672009-05-23 19:51:20 +0000432
433 // Signal various FP modes.
Chris Lattner9d7efd32010-04-04 07:05:53 +0000434 if (!UnsafeFPMath) {
435 OutStreamer.EmitRawText("\t.eabi_attribute " +
436 Twine(ARMBuildAttrs::ABI_FP_denormal) + ", 1");
437 OutStreamer.EmitRawText("\t.eabi_attribute " +
438 Twine(ARMBuildAttrs::ABI_FP_exceptions) + ", 1");
439 }
Jim Grosbachb0739b72010-09-02 01:02:06 +0000440
Evan Cheng60108e92010-07-15 22:07:12 +0000441 if (NoInfsFPMath && NoNaNsFPMath)
Chris Lattner9d7efd32010-04-04 07:05:53 +0000442 OutStreamer.EmitRawText("\t.eabi_attribute " +
443 Twine(ARMBuildAttrs::ABI_FP_number_model)+ ", 1");
Anton Korobeynikov88ce6672009-05-23 19:51:20 +0000444 else
Chris Lattner9d7efd32010-04-04 07:05:53 +0000445 OutStreamer.EmitRawText("\t.eabi_attribute " +
446 Twine(ARMBuildAttrs::ABI_FP_number_model)+ ", 3");
Anton Korobeynikov88ce6672009-05-23 19:51:20 +0000447
448 // 8-bytes alignment stuff.
Chris Lattner9d7efd32010-04-04 07:05:53 +0000449 OutStreamer.EmitRawText("\t.eabi_attribute " +
450 Twine(ARMBuildAttrs::ABI_align8_needed) + ", 1");
451 OutStreamer.EmitRawText("\t.eabi_attribute " +
452 Twine(ARMBuildAttrs::ABI_align8_preserved) + ", 1");
Anton Korobeynikov88ce6672009-05-23 19:51:20 +0000453
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000454 // Hard float. Use both S and D registers and conform to AAPCS-VFP.
Chris Lattner9d7efd32010-04-04 07:05:53 +0000455 if (Subtarget->isAAPCS_ABI() && FloatABIType == FloatABI::Hard) {
456 OutStreamer.EmitRawText("\t.eabi_attribute " +
457 Twine(ARMBuildAttrs::ABI_HardFP_use) + ", 3");
458 OutStreamer.EmitRawText("\t.eabi_attribute " +
459 Twine(ARMBuildAttrs::ABI_VFP_args) + ", 1");
460 }
Anton Korobeynikov88ce6672009-05-23 19:51:20 +0000461 // FIXME: Should we signal R9 usage?
462 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000463}
464
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +0000465
Chris Lattner4a071d62009-10-19 17:59:19 +0000466void ARMAsmPrinter::EmitEndOfAsmFile(Module &M) {
Evan Cheng5be54b02007-01-19 19:25:36 +0000467 if (Subtarget->isTargetDarwin()) {
Chris Lattnerf61159b2009-08-03 22:18:15 +0000468 // All darwin targets use mach-o.
Dan Gohman0d805c32010-04-17 16:44:48 +0000469 const TargetLoweringObjectFileMachO &TLOFMacho =
470 static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattnerb0f294c2009-10-19 18:38:33 +0000471 MachineModuleInfoMachO &MMIMacho =
472 MMI->getObjFileInfo<MachineModuleInfoMachO>();
Jim Grosbache9952212009-09-04 01:38:51 +0000473
Evan Chenga8e29892007-01-19 07:51:42 +0000474 // Output non-lazy-pointers for external and common global variables.
Chris Lattnerb0f294c2009-10-19 18:38:33 +0000475 MachineModuleInfoMachO::SymbolListTy Stubs = MMIMacho.GetGVStubList();
Bill Wendlingcebae362010-03-10 22:34:10 +0000476
Chris Lattnerb0f294c2009-10-19 18:38:33 +0000477 if (!Stubs.empty()) {
Chris Lattnerff4bc462009-08-10 01:39:42 +0000478 // Switch with ".non_lazy_symbol_pointer" directive.
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000479 OutStreamer.SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
Chris Lattnerc076a972009-08-10 18:01:34 +0000480 EmitAlignment(2);
Chris Lattnerb0f294c2009-10-19 18:38:33 +0000481 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Bill Wendlingbecd83e2010-03-09 00:40:17 +0000482 // L_foo$stub:
483 OutStreamer.EmitLabel(Stubs[i].first);
484 // .indirect_symbol _foo
Bill Wendling52a50e52010-03-11 01:18:13 +0000485 MachineModuleInfoImpl::StubValueTy &MCSym = Stubs[i].second;
486 OutStreamer.EmitSymbolAttribute(MCSym.getPointer(),MCSA_IndirectSymbol);
Bill Wendlingcf6f28d2010-03-09 00:43:34 +0000487
Bill Wendling52a50e52010-03-11 01:18:13 +0000488 if (MCSym.getInt())
Bill Wendlingcf6f28d2010-03-09 00:43:34 +0000489 // External to current translation unit.
490 OutStreamer.EmitIntValue(0, 4/*size*/, 0/*addrspace*/);
491 else
492 // Internal to current translation unit.
Bill Wendling5e1b55d2010-03-31 18:47:10 +0000493 //
Jim Grosbach1b935a32010-09-22 16:45:13 +0000494 // When we place the LSDA into the TEXT section, the type info
495 // pointers need to be indirect and pc-rel. We accomplish this by
496 // using NLPs; however, sometimes the types are local to the file.
497 // We need to fill in the value for the NLP in those cases.
Bill Wendling52a50e52010-03-11 01:18:13 +0000498 OutStreamer.EmitValue(MCSymbolRefExpr::Create(MCSym.getPointer(),
499 OutContext),
Bill Wendlingcf6f28d2010-03-09 00:43:34 +0000500 4/*size*/, 0/*addrspace*/);
Evan Chengae94e592008-12-05 01:06:39 +0000501 }
Bill Wendlingbecd83e2010-03-09 00:40:17 +0000502
503 Stubs.clear();
504 OutStreamer.AddBlankLine();
Evan Chenga8e29892007-01-19 07:51:42 +0000505 }
506
Chris Lattnere4d9ea82009-10-19 18:44:38 +0000507 Stubs = MMIMacho.GetHiddenGVStubList();
508 if (!Stubs.empty()) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000509 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
Chris Lattnerf3231de2009-08-10 18:02:16 +0000510 EmitAlignment(2);
Bill Wendlingbecd83e2010-03-09 00:40:17 +0000511 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
512 // L_foo$stub:
513 OutStreamer.EmitLabel(Stubs[i].first);
514 // .long _foo
Bill Wendlingcebae362010-03-10 22:34:10 +0000515 OutStreamer.EmitValue(MCSymbolRefExpr::
516 Create(Stubs[i].second.getPointer(),
517 OutContext),
Bill Wendlingbecd83e2010-03-09 00:40:17 +0000518 4/*size*/, 0/*addrspace*/);
519 }
Bill Wendlingcf6f28d2010-03-09 00:43:34 +0000520
521 Stubs.clear();
522 OutStreamer.AddBlankLine();
Evan Chengae94e592008-12-05 01:06:39 +0000523 }
524
Evan Chenga8e29892007-01-19 07:51:42 +0000525 // Funny Darwin hack: This flag tells the linker that no global symbols
526 // contain code that falls through to other global symbols (e.g. the obvious
527 // implementation of multiple entry points). If this doesn't occur, the
528 // linker can safely perform dead code stripping. Since LLVM never
529 // generates code that does this, it is always safe to set.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000530 OutStreamer.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000531 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000532}
Anton Korobeynikov0bd89712008-08-17 13:55:10 +0000533
Chris Lattner97f06932009-10-19 20:20:46 +0000534//===----------------------------------------------------------------------===//
535
Jim Grosbach988ce092010-09-18 00:05:05 +0000536static MCSymbol *getPICLabel(const char *Prefix, unsigned FunctionNumber,
537 unsigned LabelId, MCContext &Ctx) {
538
539 MCSymbol *Label = Ctx.GetOrCreateSymbol(Twine(Prefix)
540 + "PC" + Twine(FunctionNumber) + "_" + Twine(LabelId));
541 return Label;
542}
543
Jim Grosbacha2244cb2010-09-22 17:39:48 +0000544void ARMAsmPrinter::EmitJumpTable(const MachineInstr *MI) {
545 unsigned Opcode = MI->getOpcode();
546 int OpNum = 1;
547 if (Opcode == ARM::BR_JTadd)
548 OpNum = 2;
549 else if (Opcode == ARM::BR_JTm)
550 OpNum = 3;
551
552 const MachineOperand &MO1 = MI->getOperand(OpNum);
553 const MachineOperand &MO2 = MI->getOperand(OpNum+1); // Unique Id
554 unsigned JTI = MO1.getIndex();
555
556 // Emit a label for the jump table.
557 MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel2(JTI, MO2.getImm());
558 OutStreamer.EmitLabel(JTISymbol);
559
560 // Emit each entry of the table.
561 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
562 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
563 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
564
565 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
566 MachineBasicBlock *MBB = JTBBs[i];
567 // Construct an MCExpr for the entry. We want a value of the form:
568 // (BasicBlockAddr - TableBeginAddr)
569 //
570 // For example, a table with entries jumping to basic blocks BB0 and BB1
571 // would look like:
572 // LJTI_0_0:
573 // .word (LBB0 - LJTI_0_0)
574 // .word (LBB1 - LJTI_0_0)
575 const MCExpr *Expr = MCSymbolRefExpr::Create(MBB->getSymbol(), OutContext);
576
577 if (TM.getRelocationModel() == Reloc::PIC_)
578 Expr = MCBinaryExpr::CreateSub(Expr, MCSymbolRefExpr::Create(JTISymbol,
579 OutContext),
580 OutContext);
581 OutStreamer.EmitValue(Expr, 4);
582 }
583}
584
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000585void ARMAsmPrinter::EmitJump2Table(const MachineInstr *MI) {
586 unsigned Opcode = MI->getOpcode();
587 int OpNum = (Opcode == ARM::t2BR_JT) ? 2 : 1;
588 const MachineOperand &MO1 = MI->getOperand(OpNum);
589 const MachineOperand &MO2 = MI->getOperand(OpNum+1); // Unique Id
590 unsigned JTI = MO1.getIndex();
591
592 // Emit a label for the jump table.
593 MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel2(JTI, MO2.getImm());
594 OutStreamer.EmitLabel(JTISymbol);
595
596 // Emit each entry of the table.
597 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
598 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
599 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000600 unsigned OffsetWidth = 4;
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000601 if (MI->getOpcode() == ARM::t2TBB)
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000602 OffsetWidth = 1;
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000603 else if (MI->getOpcode() == ARM::t2TBH)
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000604 OffsetWidth = 2;
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000605
606 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
607 MachineBasicBlock *MBB = JTBBs[i];
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000608 const MCExpr *MBBSymbolExpr = MCSymbolRefExpr::Create(MBB->getSymbol(),
609 OutContext);
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000610 // If this isn't a TBB or TBH, the entries are direct branch instructions.
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000611 if (OffsetWidth == 4) {
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000612 MCInst BrInst;
613 BrInst.setOpcode(ARM::t2B);
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000614 BrInst.addOperand(MCOperand::CreateExpr(MBBSymbolExpr));
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000615 OutStreamer.EmitInstruction(BrInst);
616 continue;
617 }
618 // Otherwise it's an offset from the dispatch instruction. Construct an
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000619 // MCExpr for the entry. We want a value of the form:
620 // (BasicBlockAddr - TableBeginAddr) / 2
621 //
622 // For example, a TBB table with entries jumping to basic blocks BB0 and BB1
623 // would look like:
624 // LJTI_0_0:
625 // .byte (LBB0 - LJTI_0_0) / 2
626 // .byte (LBB1 - LJTI_0_0) / 2
627 const MCExpr *Expr =
628 MCBinaryExpr::CreateSub(MBBSymbolExpr,
629 MCSymbolRefExpr::Create(JTISymbol, OutContext),
630 OutContext);
631 Expr = MCBinaryExpr::CreateDiv(Expr, MCConstantExpr::Create(2, OutContext),
632 OutContext);
633 OutStreamer.EmitValue(Expr, OffsetWidth);
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000634 }
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000635
636 // Make sure the instruction that follows TBB is 2-byte aligned.
637 // FIXME: Constant island pass should insert an "ALIGN" instruction instead.
638 if (MI->getOpcode() == ARM::t2TBB)
639 EmitAlignment(1);
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000640}
641
Jim Grosbach2d0f53b2010-09-28 17:05:56 +0000642void ARMAsmPrinter::PrintDebugValueComment(const MachineInstr *MI,
643 raw_ostream &OS) {
644 unsigned NOps = MI->getNumOperands();
645 assert(NOps==4);
646 OS << '\t' << MAI->getCommentString() << "DEBUG_VALUE: ";
647 // cast away const; DIetc do not take const operands for some reason.
648 DIVariable V(const_cast<MDNode *>(MI->getOperand(NOps-1).getMetadata()));
649 OS << V.getName();
650 OS << " <- ";
651 // Frame address. Currently handles register +- offset only.
652 assert(MI->getOperand(0).isReg() && MI->getOperand(1).isImm());
653 OS << '['; printOperand(MI, 0, OS); OS << '+'; printOperand(MI, 1, OS);
654 OS << ']';
655 OS << "+";
656 printOperand(MI, NOps-2, OS);
657}
658
Jim Grosbachb454cda2010-09-29 15:23:40 +0000659void ARMAsmPrinter::EmitInstruction(const MachineInstr *MI) {
Chris Lattner96bc2172009-10-20 00:52:47 +0000660 ARMMCInstLower MCInstLowering(OutContext, *Mang, *this);
Chris Lattner97f06932009-10-19 20:20:46 +0000661 switch (MI->getOpcode()) {
Chris Lattnerc6b8a992009-10-20 05:58:02 +0000662 case ARM::t2MOVi32imm:
663 assert(0 && "Should be lowered by thumb2it pass");
Chris Lattner4d152222009-10-19 22:23:04 +0000664 default: break;
Jim Grosbach2d0f53b2010-09-28 17:05:56 +0000665 case ARM::DBG_VALUE: {
666 if (isVerbose() && OutStreamer.hasRawTextSupport()) {
667 SmallString<128> TmpStr;
668 raw_svector_ostream OS(TmpStr);
669 PrintDebugValueComment(MI, OS);
670 OutStreamer.EmitRawText(StringRef(OS.str()));
671 }
672 return;
673 }
Jim Grosbachfbd18732010-09-17 23:41:53 +0000674 case ARM::tPICADD: {
675 // This is a pseudo op for a label + instruction sequence, which looks like:
676 // LPC0:
677 // add r0, pc
678 // This adds the address of LPC0 to r0.
679
680 // Emit the label.
Jim Grosbach988ce092010-09-18 00:05:05 +0000681 OutStreamer.EmitLabel(getPICLabel(MAI->getPrivateGlobalPrefix(),
682 getFunctionNumber(), MI->getOperand(2).getImm(),
683 OutContext));
Jim Grosbachfbd18732010-09-17 23:41:53 +0000684
685 // Form and emit the add.
686 MCInst AddInst;
687 AddInst.setOpcode(ARM::tADDhirr);
688 AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
689 AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
690 AddInst.addOperand(MCOperand::CreateReg(ARM::PC));
691 // Add predicate operands.
692 AddInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
693 AddInst.addOperand(MCOperand::CreateReg(0));
694 OutStreamer.EmitInstruction(AddInst);
695 return;
696 }
Chris Lattner4d152222009-10-19 22:23:04 +0000697 case ARM::PICADD: { // FIXME: Remove asm string from td file.
698 // This is a pseudo op for a label + instruction sequence, which looks like:
699 // LPC0:
700 // add r0, pc, r0
701 // This adds the address of LPC0 to r0.
Jim Grosbachb0739b72010-09-02 01:02:06 +0000702
Chris Lattner4d152222009-10-19 22:23:04 +0000703 // Emit the label.
Jim Grosbach988ce092010-09-18 00:05:05 +0000704 OutStreamer.EmitLabel(getPICLabel(MAI->getPrivateGlobalPrefix(),
705 getFunctionNumber(), MI->getOperand(2).getImm(),
706 OutContext));
Jim Grosbachb0739b72010-09-02 01:02:06 +0000707
Jim Grosbachf3f09522010-09-14 21:05:34 +0000708 // Form and emit the add.
Chris Lattner4d152222009-10-19 22:23:04 +0000709 MCInst AddInst;
710 AddInst.setOpcode(ARM::ADDrr);
711 AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
712 AddInst.addOperand(MCOperand::CreateReg(ARM::PC));
713 AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg()));
Jim Grosbach5b46d622010-09-14 21:28:17 +0000714 // Add predicate operands.
715 AddInst.addOperand(MCOperand::CreateImm(MI->getOperand(3).getImm()));
716 AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(4).getReg()));
717 // Add 's' bit operand (always reg0 for this)
718 AddInst.addOperand(MCOperand::CreateReg(0));
Chris Lattner850d2e22010-02-03 01:16:28 +0000719 OutStreamer.EmitInstruction(AddInst);
Chris Lattner4d152222009-10-19 22:23:04 +0000720 return;
721 }
Jim Grosbacha28abbe2010-09-17 16:25:52 +0000722 case ARM::PICSTR:
723 case ARM::PICSTRB:
724 case ARM::PICSTRH:
725 case ARM::PICLDR:
726 case ARM::PICLDRB:
727 case ARM::PICLDRH:
728 case ARM::PICLDRSB:
729 case ARM::PICLDRSH: {
Jim Grosbachb74ca9d2010-09-16 17:43:25 +0000730 // This is a pseudo op for a label + instruction sequence, which looks like:
731 // LPC0:
Jim Grosbacha28abbe2010-09-17 16:25:52 +0000732 // OP r0, [pc, r0]
Jim Grosbachb74ca9d2010-09-16 17:43:25 +0000733 // The LCP0 label is referenced by a constant pool entry in order to get
734 // a PC-relative address at the ldr instruction.
735
736 // Emit the label.
Jim Grosbach988ce092010-09-18 00:05:05 +0000737 OutStreamer.EmitLabel(getPICLabel(MAI->getPrivateGlobalPrefix(),
738 getFunctionNumber(), MI->getOperand(2).getImm(),
739 OutContext));
Jim Grosbachb74ca9d2010-09-16 17:43:25 +0000740
741 // Form and emit the load
Jim Grosbacha28abbe2010-09-17 16:25:52 +0000742 unsigned Opcode;
743 switch (MI->getOpcode()) {
744 default:
745 llvm_unreachable("Unexpected opcode!");
746 case ARM::PICSTR: Opcode = ARM::STR; break;
747 case ARM::PICSTRB: Opcode = ARM::STRB; break;
748 case ARM::PICSTRH: Opcode = ARM::STRH; break;
749 case ARM::PICLDR: Opcode = ARM::LDR; break;
750 case ARM::PICLDRB: Opcode = ARM::LDRB; break;
751 case ARM::PICLDRH: Opcode = ARM::LDRH; break;
752 case ARM::PICLDRSB: Opcode = ARM::LDRSB; break;
753 case ARM::PICLDRSH: Opcode = ARM::LDRSH; break;
754 }
755 MCInst LdStInst;
756 LdStInst.setOpcode(Opcode);
757 LdStInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
758 LdStInst.addOperand(MCOperand::CreateReg(ARM::PC));
759 LdStInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg()));
760 LdStInst.addOperand(MCOperand::CreateImm(0));
Jim Grosbachb74ca9d2010-09-16 17:43:25 +0000761 // Add predicate operands.
Jim Grosbacha28abbe2010-09-17 16:25:52 +0000762 LdStInst.addOperand(MCOperand::CreateImm(MI->getOperand(3).getImm()));
763 LdStInst.addOperand(MCOperand::CreateReg(MI->getOperand(4).getReg()));
764 OutStreamer.EmitInstruction(LdStInst);
Jim Grosbachb74ca9d2010-09-16 17:43:25 +0000765
766 return;
767 }
Chris Lattnera70e6442009-10-19 22:33:05 +0000768 case ARM::CONSTPOOL_ENTRY: { // FIXME: Remove asm string from td file.
769 /// CONSTPOOL_ENTRY - This instruction represents a floating constant pool
770 /// in the function. The first operand is the ID# for this instruction, the
771 /// second is the index into the MachineConstantPool that this is, the third
772 /// is the size in bytes of this constant pool entry.
773 unsigned LabelId = (unsigned)MI->getOperand(0).getImm();
774 unsigned CPIdx = (unsigned)MI->getOperand(1).getIndex();
775
776 EmitAlignment(2);
Chris Lattner1b46f432010-01-23 07:00:21 +0000777 OutStreamer.EmitLabel(GetCPISymbol(LabelId));
Chris Lattnera70e6442009-10-19 22:33:05 +0000778
779 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPIdx];
780 if (MCPE.isMachineConstantPoolEntry())
781 EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
782 else
783 EmitGlobalConstant(MCPE.Val.ConstVal);
Jim Grosbachb0739b72010-09-02 01:02:06 +0000784
Chris Lattnera70e6442009-10-19 22:33:05 +0000785 return;
786 }
Chris Lattner017d9472009-10-20 00:40:56 +0000787 case ARM::MOVi2pieces: { // FIXME: Remove asmstring from td file.
788 // This is a hack that lowers as a two instruction sequence.
789 unsigned DstReg = MI->getOperand(0).getReg();
790 unsigned ImmVal = (unsigned)MI->getOperand(1).getImm();
791
792 unsigned SOImmValV1 = ARM_AM::getSOImmTwoPartFirst(ImmVal);
793 unsigned SOImmValV2 = ARM_AM::getSOImmTwoPartSecond(ImmVal);
Jim Grosbachb0739b72010-09-02 01:02:06 +0000794
Chris Lattner017d9472009-10-20 00:40:56 +0000795 {
796 MCInst TmpInst;
797 TmpInst.setOpcode(ARM::MOVi);
798 TmpInst.addOperand(MCOperand::CreateReg(DstReg));
799 TmpInst.addOperand(MCOperand::CreateImm(SOImmValV1));
Jim Grosbachb0739b72010-09-02 01:02:06 +0000800
Chris Lattner017d9472009-10-20 00:40:56 +0000801 // Predicate.
802 TmpInst.addOperand(MCOperand::CreateImm(MI->getOperand(2).getImm()));
803 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(3).getReg()));
Chris Lattner233917c2009-10-20 00:46:11 +0000804
805 TmpInst.addOperand(MCOperand::CreateReg(0)); // cc_out
Chris Lattner850d2e22010-02-03 01:16:28 +0000806 OutStreamer.EmitInstruction(TmpInst);
Chris Lattner017d9472009-10-20 00:40:56 +0000807 }
808
809 {
810 MCInst TmpInst;
811 TmpInst.setOpcode(ARM::ORRri);
812 TmpInst.addOperand(MCOperand::CreateReg(DstReg)); // dstreg
813 TmpInst.addOperand(MCOperand::CreateReg(DstReg)); // inreg
814 TmpInst.addOperand(MCOperand::CreateImm(SOImmValV2)); // so_imm
815 // Predicate.
816 TmpInst.addOperand(MCOperand::CreateImm(MI->getOperand(2).getImm()));
817 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(3).getReg()));
Jim Grosbachb0739b72010-09-02 01:02:06 +0000818
Chris Lattner017d9472009-10-20 00:40:56 +0000819 TmpInst.addOperand(MCOperand::CreateReg(0)); // cc_out
Chris Lattner850d2e22010-02-03 01:16:28 +0000820 OutStreamer.EmitInstruction(TmpInst);
Chris Lattner017d9472009-10-20 00:40:56 +0000821 }
Jim Grosbachb0739b72010-09-02 01:02:06 +0000822 return;
Chris Lattner017d9472009-10-20 00:40:56 +0000823 }
Chris Lattner161dcbf2009-10-20 01:11:37 +0000824 case ARM::MOVi32imm: { // FIXME: Remove asmstring from td file.
825 // This is a hack that lowers as a two instruction sequence.
826 unsigned DstReg = MI->getOperand(0).getReg();
Rafael Espindola18c10212010-05-12 05:16:34 +0000827 const MachineOperand &MO = MI->getOperand(1);
828 MCOperand V1, V2;
829 if (MO.isImm()) {
830 unsigned ImmVal = (unsigned)MI->getOperand(1).getImm();
831 V1 = MCOperand::CreateImm(ImmVal & 65535);
832 V2 = MCOperand::CreateImm(ImmVal >> 16);
833 } else if (MO.isGlobal()) {
Jim Grosbachc686e332010-09-17 18:25:25 +0000834 MCSymbol *Symbol = MCInstLowering.GetGlobalAddressSymbol(MO.getGlobal());
Rafael Espindola18c10212010-05-12 05:16:34 +0000835 const MCSymbolRefExpr *SymRef1 =
Duncan Sands34727662010-07-12 08:16:59 +0000836 MCSymbolRefExpr::Create(Symbol,
837 MCSymbolRefExpr::VK_ARM_LO16, OutContext);
Rafael Espindola18c10212010-05-12 05:16:34 +0000838 const MCSymbolRefExpr *SymRef2 =
Duncan Sands34727662010-07-12 08:16:59 +0000839 MCSymbolRefExpr::Create(Symbol,
840 MCSymbolRefExpr::VK_ARM_HI16, OutContext);
Rafael Espindola18c10212010-05-12 05:16:34 +0000841 V1 = MCOperand::CreateExpr(SymRef1);
842 V2 = MCOperand::CreateExpr(SymRef2);
843 } else {
Jim Grosbachf0633e42010-09-22 20:55:15 +0000844 // FIXME: External symbol?
Rafael Espindola18c10212010-05-12 05:16:34 +0000845 MI->dump();
846 llvm_unreachable("cannot handle this operand");
847 }
848
Chris Lattner161dcbf2009-10-20 01:11:37 +0000849 {
850 MCInst TmpInst;
851 TmpInst.setOpcode(ARM::MOVi16);
852 TmpInst.addOperand(MCOperand::CreateReg(DstReg)); // dstreg
Rafael Espindola18c10212010-05-12 05:16:34 +0000853 TmpInst.addOperand(V1); // lower16(imm)
Jim Grosbachb0739b72010-09-02 01:02:06 +0000854
Chris Lattner161dcbf2009-10-20 01:11:37 +0000855 // Predicate.
856 TmpInst.addOperand(MCOperand::CreateImm(MI->getOperand(2).getImm()));
857 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(3).getReg()));
Jim Grosbachb0739b72010-09-02 01:02:06 +0000858
Chris Lattner850d2e22010-02-03 01:16:28 +0000859 OutStreamer.EmitInstruction(TmpInst);
Chris Lattner161dcbf2009-10-20 01:11:37 +0000860 }
Jim Grosbachb0739b72010-09-02 01:02:06 +0000861
Chris Lattner161dcbf2009-10-20 01:11:37 +0000862 {
863 MCInst TmpInst;
864 TmpInst.setOpcode(ARM::MOVTi16);
865 TmpInst.addOperand(MCOperand::CreateReg(DstReg)); // dstreg
866 TmpInst.addOperand(MCOperand::CreateReg(DstReg)); // srcreg
Rafael Espindola18c10212010-05-12 05:16:34 +0000867 TmpInst.addOperand(V2); // upper16(imm)
Jim Grosbachb0739b72010-09-02 01:02:06 +0000868
Chris Lattner161dcbf2009-10-20 01:11:37 +0000869 // Predicate.
870 TmpInst.addOperand(MCOperand::CreateImm(MI->getOperand(2).getImm()));
871 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(3).getReg()));
Jim Grosbachb0739b72010-09-02 01:02:06 +0000872
Chris Lattner850d2e22010-02-03 01:16:28 +0000873 OutStreamer.EmitInstruction(TmpInst);
Chris Lattner161dcbf2009-10-20 01:11:37 +0000874 }
Jim Grosbachb0739b72010-09-02 01:02:06 +0000875
Chris Lattner161dcbf2009-10-20 01:11:37 +0000876 return;
877 }
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000878 case ARM::t2TBB:
879 case ARM::t2TBH:
880 case ARM::t2BR_JT: {
881 // Lower and emit the instruction itself, then the jump table following it.
882 MCInst TmpInst;
883 MCInstLowering.Lower(MI, TmpInst);
884 OutStreamer.EmitInstruction(TmpInst);
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000885 EmitJump2Table(MI);
886 return;
887 }
888 case ARM::tBR_JTr:
889 case ARM::BR_JTr:
890 case ARM::BR_JTm:
Jim Grosbacha2244cb2010-09-22 17:39:48 +0000891 case ARM::BR_JTadd: {
892 // Lower and emit the instruction itself, then the jump table following it.
893 MCInst TmpInst;
894 MCInstLowering.Lower(MI, TmpInst);
895 OutStreamer.EmitInstruction(TmpInst);
896 EmitJumpTable(MI);
897 return;
898 }
Jim Grosbach2e6ae132010-09-23 18:05:37 +0000899 case ARM::TRAP: {
900 // Non-Darwin binutils don't yet support the "trap" mnemonic.
901 // FIXME: Remove this special case when they do.
902 if (!Subtarget->isTargetDarwin()) {
903 //.long 0xe7ffdefe ${:comment} trap
Jim Grosbachb2dda4b2010-09-23 19:42:17 +0000904 uint32_t Val = 0xe7ffdefeUL;
Jim Grosbach2e6ae132010-09-23 18:05:37 +0000905 OutStreamer.AddComment("trap");
906 OutStreamer.EmitIntValue(Val, 4);
907 return;
908 }
909 break;
910 }
911 case ARM::tTRAP: {
912 // Non-Darwin binutils don't yet support the "trap" mnemonic.
913 // FIXME: Remove this special case when they do.
914 if (!Subtarget->isTargetDarwin()) {
Benjamin Kramerc8ab9eb2010-09-23 18:57:26 +0000915 //.short 57086 ${:comment} trap
916 uint16_t Val = 0xdefe;
Jim Grosbach2e6ae132010-09-23 18:05:37 +0000917 OutStreamer.AddComment("trap");
918 OutStreamer.EmitIntValue(Val, 2);
919 return;
920 }
921 break;
922 }
Jim Grosbach433a5782010-09-24 20:47:58 +0000923 case ARM::t2Int_eh_sjlj_setjmp:
924 case ARM::t2Int_eh_sjlj_setjmp_nofp:
925 case ARM::tInt_eh_sjlj_setjmp: { // FIXME: Remove asmstring from td file.
926 // Two incoming args: GPR:$src, GPR:$val
927 // mov $val, pc
928 // adds $val, #7
929 // str $val, [$src, #4]
930 // movs r0, #0
931 // b 1f
932 // movs r0, #1
933 // 1:
934 unsigned SrcReg = MI->getOperand(0).getReg();
935 unsigned ValReg = MI->getOperand(1).getReg();
936 MCSymbol *Label = GetARMSJLJEHLabel();
937 {
938 MCInst TmpInst;
939 TmpInst.setOpcode(ARM::tMOVgpr2tgpr);
940 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
941 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
942 // 's' bit operand
943 TmpInst.addOperand(MCOperand::CreateReg(ARM::CPSR));
944 OutStreamer.AddComment("eh_setjmp begin");
945 OutStreamer.EmitInstruction(TmpInst);
946 }
947 {
948 MCInst TmpInst;
949 TmpInst.setOpcode(ARM::tADDi3);
950 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
951 // 's' bit operand
952 TmpInst.addOperand(MCOperand::CreateReg(ARM::CPSR));
953 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
954 TmpInst.addOperand(MCOperand::CreateImm(7));
955 // Predicate.
956 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
957 TmpInst.addOperand(MCOperand::CreateReg(0));
958 OutStreamer.EmitInstruction(TmpInst);
959 }
960 {
961 MCInst TmpInst;
962 TmpInst.setOpcode(ARM::tSTR);
963 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
964 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
965 // The offset immediate is #4. The operand value is scaled by 4 for the
966 // tSTR instruction.
967 TmpInst.addOperand(MCOperand::CreateImm(1));
968 TmpInst.addOperand(MCOperand::CreateReg(0));
969 // Predicate.
970 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
971 TmpInst.addOperand(MCOperand::CreateReg(0));
972 OutStreamer.EmitInstruction(TmpInst);
973 }
974 {
975 MCInst TmpInst;
976 TmpInst.setOpcode(ARM::tMOVi8);
977 TmpInst.addOperand(MCOperand::CreateReg(ARM::R0));
978 TmpInst.addOperand(MCOperand::CreateReg(ARM::CPSR));
979 TmpInst.addOperand(MCOperand::CreateImm(0));
980 // Predicate.
981 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
982 TmpInst.addOperand(MCOperand::CreateReg(0));
983 OutStreamer.EmitInstruction(TmpInst);
984 }
985 {
986 const MCExpr *SymbolExpr = MCSymbolRefExpr::Create(Label, OutContext);
987 MCInst TmpInst;
988 TmpInst.setOpcode(ARM::tB);
989 TmpInst.addOperand(MCOperand::CreateExpr(SymbolExpr));
990 OutStreamer.EmitInstruction(TmpInst);
991 }
992 {
993 MCInst TmpInst;
994 TmpInst.setOpcode(ARM::tMOVi8);
995 TmpInst.addOperand(MCOperand::CreateReg(ARM::R0));
996 TmpInst.addOperand(MCOperand::CreateReg(ARM::CPSR));
997 TmpInst.addOperand(MCOperand::CreateImm(1));
998 // Predicate.
999 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1000 TmpInst.addOperand(MCOperand::CreateReg(0));
1001 OutStreamer.AddComment("eh_setjmp end");
1002 OutStreamer.EmitInstruction(TmpInst);
1003 }
1004 OutStreamer.EmitLabel(Label);
1005 return;
1006 }
1007
Jim Grosbach45390082010-09-23 23:33:56 +00001008 case ARM::Int_eh_sjlj_setjmp_nofp:
1009 case ARM::Int_eh_sjlj_setjmp: { // FIXME: Remove asmstring from td file.
1010 // Two incoming args: GPR:$src, GPR:$val
1011 // add $val, pc, #8
1012 // str $val, [$src, #+4]
1013 // mov r0, #0
1014 // add pc, pc, #0
1015 // mov r0, #1
1016 unsigned SrcReg = MI->getOperand(0).getReg();
1017 unsigned ValReg = MI->getOperand(1).getReg();
1018
1019 {
1020 MCInst TmpInst;
1021 TmpInst.setOpcode(ARM::ADDri);
1022 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
1023 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1024 TmpInst.addOperand(MCOperand::CreateImm(8));
1025 // Predicate.
1026 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1027 TmpInst.addOperand(MCOperand::CreateReg(0));
1028 // 's' bit operand (always reg0 for this).
1029 TmpInst.addOperand(MCOperand::CreateReg(0));
1030 OutStreamer.AddComment("eh_setjmp begin");
1031 OutStreamer.EmitInstruction(TmpInst);
1032 }
1033 {
1034 MCInst TmpInst;
1035 TmpInst.setOpcode(ARM::STR);
1036 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
1037 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
1038 TmpInst.addOperand(MCOperand::CreateReg(0));
1039 TmpInst.addOperand(MCOperand::CreateImm(4));
1040 // Predicate.
1041 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1042 TmpInst.addOperand(MCOperand::CreateReg(0));
1043 OutStreamer.EmitInstruction(TmpInst);
1044 }
1045 {
1046 MCInst TmpInst;
1047 TmpInst.setOpcode(ARM::MOVi);
1048 TmpInst.addOperand(MCOperand::CreateReg(ARM::R0));
1049 TmpInst.addOperand(MCOperand::CreateImm(0));
1050 // Predicate.
1051 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1052 TmpInst.addOperand(MCOperand::CreateReg(0));
1053 // 's' bit operand (always reg0 for this).
1054 TmpInst.addOperand(MCOperand::CreateReg(0));
1055 OutStreamer.EmitInstruction(TmpInst);
1056 }
1057 {
1058 MCInst TmpInst;
1059 TmpInst.setOpcode(ARM::ADDri);
1060 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1061 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1062 TmpInst.addOperand(MCOperand::CreateImm(0));
1063 // Predicate.
1064 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1065 TmpInst.addOperand(MCOperand::CreateReg(0));
1066 // 's' bit operand (always reg0 for this).
1067 TmpInst.addOperand(MCOperand::CreateReg(0));
1068 OutStreamer.EmitInstruction(TmpInst);
1069 }
1070 {
1071 MCInst TmpInst;
1072 TmpInst.setOpcode(ARM::MOVi);
1073 TmpInst.addOperand(MCOperand::CreateReg(ARM::R0));
1074 TmpInst.addOperand(MCOperand::CreateImm(1));
1075 // Predicate.
1076 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1077 TmpInst.addOperand(MCOperand::CreateReg(0));
1078 // 's' bit operand (always reg0 for this).
1079 TmpInst.addOperand(MCOperand::CreateReg(0));
1080 OutStreamer.AddComment("eh_setjmp end");
1081 OutStreamer.EmitInstruction(TmpInst);
1082 }
1083 return;
1084 }
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001085 case ARM::Int_eh_sjlj_longjmp: {
1086 // ldr sp, [$src, #8]
1087 // ldr $scratch, [$src, #4]
1088 // ldr r7, [$src]
1089 // bx $scratch
1090 unsigned SrcReg = MI->getOperand(0).getReg();
1091 unsigned ScratchReg = MI->getOperand(1).getReg();
1092 {
1093 MCInst TmpInst;
1094 TmpInst.setOpcode(ARM::LDR);
1095 TmpInst.addOperand(MCOperand::CreateReg(ARM::SP));
1096 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
1097 TmpInst.addOperand(MCOperand::CreateReg(0));
1098 TmpInst.addOperand(MCOperand::CreateImm(8));
1099 // Predicate.
1100 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1101 TmpInst.addOperand(MCOperand::CreateReg(0));
1102 OutStreamer.EmitInstruction(TmpInst);
1103 }
1104 {
1105 MCInst TmpInst;
1106 TmpInst.setOpcode(ARM::LDR);
1107 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1108 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
1109 TmpInst.addOperand(MCOperand::CreateReg(0));
1110 TmpInst.addOperand(MCOperand::CreateImm(4));
1111 // Predicate.
1112 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1113 TmpInst.addOperand(MCOperand::CreateReg(0));
1114 OutStreamer.EmitInstruction(TmpInst);
1115 }
1116 {
1117 MCInst TmpInst;
1118 TmpInst.setOpcode(ARM::LDR);
1119 TmpInst.addOperand(MCOperand::CreateReg(ARM::R7));
1120 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
1121 TmpInst.addOperand(MCOperand::CreateReg(0));
1122 TmpInst.addOperand(MCOperand::CreateImm(0));
1123 // Predicate.
1124 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1125 TmpInst.addOperand(MCOperand::CreateReg(0));
1126 OutStreamer.EmitInstruction(TmpInst);
1127 }
1128 {
1129 MCInst TmpInst;
1130 TmpInst.setOpcode(ARM::BRIND);
1131 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1132 // Predicate.
1133 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1134 TmpInst.addOperand(MCOperand::CreateReg(0));
1135 OutStreamer.EmitInstruction(TmpInst);
1136 }
1137 return;
1138 }
Jim Grosbach385cc5e2010-09-27 22:28:11 +00001139 case ARM::tInt_eh_sjlj_longjmp: {
1140 // ldr $scratch, [$src, #8]
1141 // mov sp, $scratch
1142 // ldr $scratch, [$src, #4]
1143 // ldr r7, [$src]
1144 // bx $scratch
1145 unsigned SrcReg = MI->getOperand(0).getReg();
1146 unsigned ScratchReg = MI->getOperand(1).getReg();
1147 {
1148 MCInst TmpInst;
1149 TmpInst.setOpcode(ARM::tLDR);
1150 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1151 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
1152 // The offset immediate is #8. The operand value is scaled by 4 for the
1153 // tSTR instruction.
1154 TmpInst.addOperand(MCOperand::CreateImm(2));
1155 TmpInst.addOperand(MCOperand::CreateReg(0));
1156 // Predicate.
1157 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1158 TmpInst.addOperand(MCOperand::CreateReg(0));
1159 OutStreamer.EmitInstruction(TmpInst);
1160 }
1161 {
1162 MCInst TmpInst;
1163 TmpInst.setOpcode(ARM::tMOVtgpr2gpr);
1164 TmpInst.addOperand(MCOperand::CreateReg(ARM::SP));
1165 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1166 // Predicate.
1167 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1168 TmpInst.addOperand(MCOperand::CreateReg(0));
1169 OutStreamer.EmitInstruction(TmpInst);
1170 }
1171 {
1172 MCInst TmpInst;
1173 TmpInst.setOpcode(ARM::tLDR);
1174 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1175 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
1176 TmpInst.addOperand(MCOperand::CreateImm(1));
1177 TmpInst.addOperand(MCOperand::CreateReg(0));
1178 // Predicate.
1179 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1180 TmpInst.addOperand(MCOperand::CreateReg(0));
1181 OutStreamer.EmitInstruction(TmpInst);
1182 }
1183 {
1184 MCInst TmpInst;
1185 TmpInst.setOpcode(ARM::tLDR);
1186 TmpInst.addOperand(MCOperand::CreateReg(ARM::R7));
1187 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
1188 TmpInst.addOperand(MCOperand::CreateImm(0));
1189 TmpInst.addOperand(MCOperand::CreateReg(0));
1190 // Predicate.
1191 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1192 TmpInst.addOperand(MCOperand::CreateReg(0));
1193 OutStreamer.EmitInstruction(TmpInst);
1194 }
1195 {
1196 MCInst TmpInst;
1197 TmpInst.setOpcode(ARM::tBX_RET_vararg);
1198 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1199 // Predicate.
1200 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1201 TmpInst.addOperand(MCOperand::CreateReg(0));
1202 OutStreamer.EmitInstruction(TmpInst);
1203 }
1204 return;
1205 }
Chris Lattner97f06932009-10-19 20:20:46 +00001206 }
Jim Grosbachb0739b72010-09-02 01:02:06 +00001207
Chris Lattner97f06932009-10-19 20:20:46 +00001208 MCInst TmpInst;
1209 MCInstLowering.Lower(MI, TmpInst);
Chris Lattner850d2e22010-02-03 01:16:28 +00001210 OutStreamer.EmitInstruction(TmpInst);
Chris Lattner97f06932009-10-19 20:20:46 +00001211}
Daniel Dunbar2685a292009-10-20 05:15:36 +00001212
1213//===----------------------------------------------------------------------===//
1214// Target Registry Stuff
1215//===----------------------------------------------------------------------===//
1216
1217static MCInstPrinter *createARMMCInstPrinter(const Target &T,
1218 unsigned SyntaxVariant,
Chris Lattnerd3740872010-04-04 05:04:31 +00001219 const MCAsmInfo &MAI) {
Daniel Dunbar2685a292009-10-20 05:15:36 +00001220 if (SyntaxVariant == 0)
Jim Grosbach74d7e6c2010-09-17 21:33:25 +00001221 return new ARMInstPrinter(MAI);
Daniel Dunbar2685a292009-10-20 05:15:36 +00001222 return 0;
1223}
1224
1225// Force static initialization.
1226extern "C" void LLVMInitializeARMAsmPrinter() {
1227 RegisterAsmPrinter<ARMAsmPrinter> X(TheARMTarget);
1228 RegisterAsmPrinter<ARMAsmPrinter> Y(TheThumbTarget);
1229
1230 TargetRegistry::RegisterMCInstPrinter(TheARMTarget, createARMMCInstPrinter);
1231 TargetRegistry::RegisterMCInstPrinter(TheThumbTarget, createARMMCInstPrinter);
1232}
1233