blob: 3908b1eeeb43108a02c1696bb53355a54718f2e8 [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"
Jim Grosbach7ac16092010-10-01 22:39:28 +000020#include "InstPrinter/ARMInstPrinter.h"
Chris Lattner97f06932009-10-19 20:20:46 +000021#include "ARMMachineFunctionInfo.h"
22#include "ARMMCInstLower.h"
23#include "ARMTargetMachine.h"
Jason W Kim17b443d2010-10-11 23:01:44 +000024#include "ARMTargetObjectFile.h"
Dale Johannesen3f282aa2010-04-26 20:07:31 +000025#include "llvm/Analysis/DebugInfo.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000026#include "llvm/Constants.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000027#include "llvm/Module.h"
Benjamin Kramere55b15f2009-12-28 12:27:56 +000028#include "llvm/Type.h"
Dan Gohmancf20ac42009-08-13 01:36:44 +000029#include "llvm/Assembly/Writer.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000030#include "llvm/CodeGen/AsmPrinter.h"
Chris Lattnerb0f294c2009-10-19 18:38:33 +000031#include "llvm/CodeGen/MachineModuleInfoImpls.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000032#include "llvm/CodeGen/MachineFunctionPass.h"
Evan Chenga8e29892007-01-19 07:51:42 +000033#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattnerb0f294c2009-10-19 18:38:33 +000034#include "llvm/MC/MCAsmInfo.h"
Rafael Espindolacecbc3d2010-10-25 17:50:35 +000035#include "llvm/MC/MCAssembler.h"
Chris Lattnerb0f294c2009-10-19 18:38:33 +000036#include "llvm/MC/MCContext.h"
Bill Wendlingbecd83e2010-03-09 00:40:17 +000037#include "llvm/MC/MCExpr.h"
Chris Lattner97f06932009-10-19 20:20:46 +000038#include "llvm/MC/MCInst.h"
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000039#include "llvm/MC/MCSectionMachO.h"
Rafael Espindolacecbc3d2010-10-25 17:50:35 +000040#include "llvm/MC/MCObjectStreamer.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000041#include "llvm/MC/MCStreamer.h"
Chris Lattner325d3dc2009-09-13 17:14:04 +000042#include "llvm/MC/MCSymbol.h"
Chris Lattnerd62f1b42010-03-12 21:19:23 +000043#include "llvm/Target/Mangler.h"
Rafael Espindolab01c4bb2006-07-27 11:38:51 +000044#include "llvm/Target/TargetData.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000045#include "llvm/Target/TargetMachine.h"
Evan Cheng5be54b02007-01-19 19:25:36 +000046#include "llvm/Target/TargetOptions.h"
Daniel Dunbar51b198a2009-07-15 20:24:03 +000047#include "llvm/Target/TargetRegistry.h"
Evan Chengc324ecb2009-07-24 18:19:46 +000048#include "llvm/ADT/SmallPtrSet.h"
Jim Grosbachc40d9f92009-09-01 18:49:12 +000049#include "llvm/ADT/SmallString.h"
Bob Wilson54c78ef2009-11-06 23:33:28 +000050#include "llvm/ADT/StringExtras.h"
Chris Lattner97f06932009-10-19 20:20:46 +000051#include "llvm/Support/CommandLine.h"
Devang Patel59135f42010-08-04 22:39:39 +000052#include "llvm/Support/Debug.h"
Torok Edwin30464702009-07-08 20:55:50 +000053#include "llvm/Support/ErrorHandling.h"
Chris Lattnerb23569a2010-04-04 08:18:47 +000054#include "llvm/Support/raw_ostream.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000055#include <cctype>
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000056using namespace llvm;
57
Jim Grosbach91729002010-07-21 23:03:52 +000058namespace llvm {
59 namespace ARM {
60 enum DW_ISA {
61 DW_ISA_ARM_thumb = 1,
62 DW_ISA_ARM_arm = 2
63 };
64 }
65}
66
Chris Lattner95b2c7d2006-12-19 22:59:26 +000067namespace {
Rafael Espindolacecbc3d2010-10-25 17:50:35 +000068
69 // Per section and per symbol attributes are not supported.
70 // To implement them we would need the ability to delay this emission
71 // until the assembly file is fully parsed/generated as only then do we
72 // know the symbol and section numbers.
73 class AttributeEmitter {
74 public:
75 virtual void MaybeSwitchVendor(StringRef Vendor) = 0;
76 virtual void EmitAttribute(unsigned Attribute, unsigned Value) = 0;
77 virtual void Finish() = 0;
Rafael Espindola4921e232010-10-25 18:38:32 +000078 virtual ~AttributeEmitter() {}
Rafael Espindolacecbc3d2010-10-25 17:50:35 +000079 };
80
81 class AsmAttributeEmitter : public AttributeEmitter {
82 MCStreamer &Streamer;
83
84 public:
85 AsmAttributeEmitter(MCStreamer &Streamer_) : Streamer(Streamer_) {}
86 void MaybeSwitchVendor(StringRef Vendor) { }
87
88 void EmitAttribute(unsigned Attribute, unsigned Value) {
89 Streamer.EmitRawText("\t.eabi_attribute " +
90 Twine(Attribute) + ", " + Twine(Value));
91 }
92
93 void Finish() { }
94 };
95
96 class ObjectAttributeEmitter : public AttributeEmitter {
97 MCObjectStreamer &Streamer;
Rafael Espindolacecbc3d2010-10-25 17:50:35 +000098 StringRef CurrentVendor;
99 SmallString<64> Contents;
100
101 public:
102 ObjectAttributeEmitter(MCObjectStreamer &Streamer_) :
103 Streamer(Streamer_), CurrentVendor("") { }
104
105 void MaybeSwitchVendor(StringRef Vendor) {
106 assert(!Vendor.empty() && "Vendor cannot be empty.");
107
108 if (CurrentVendor.empty())
109 CurrentVendor = Vendor;
110 else if (CurrentVendor == Vendor)
111 return;
112 else
113 Finish();
114
115 CurrentVendor = Vendor;
116
Rafael Espindola33363842010-10-25 22:26:55 +0000117 assert(Contents.size() == 0);
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000118 }
119
120 void EmitAttribute(unsigned Attribute, unsigned Value) {
121 // FIXME: should be ULEB
122 Contents += Attribute;
123 Contents += Value;
124 }
125
126 void Finish() {
Rafael Espindola33363842010-10-25 22:26:55 +0000127 const size_t ContentsSize = Contents.size();
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000128
Rafael Espindola33363842010-10-25 22:26:55 +0000129 // Vendor size + Vendor name + '\0'
130 const size_t VendorHeaderSize = 4 + CurrentVendor.size() + 1;
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000131
Rafael Espindola33363842010-10-25 22:26:55 +0000132 // Tag + Tag Size
133 const size_t TagHeaderSize = 1 + 4;
134
135 Streamer.EmitIntValue(VendorHeaderSize + TagHeaderSize + ContentsSize, 4);
136 Streamer.EmitBytes(CurrentVendor, 0);
137 Streamer.EmitIntValue(0, 1); // '\0'
138
139 Streamer.EmitIntValue(ARMBuildAttrs::File, 1);
140 Streamer.EmitIntValue(TagHeaderSize + ContentsSize, 4);
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000141
142 Streamer.EmitBytes(Contents, 0);
Rafael Espindola33363842010-10-25 22:26:55 +0000143
144 Contents.clear();
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000145 }
146 };
147
Chris Lattner4a071d62009-10-19 17:59:19 +0000148 class ARMAsmPrinter : public AsmPrinter {
Evan Chenga8e29892007-01-19 07:51:42 +0000149
150 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
151 /// make the right decision when printing asm code for different targets.
152 const ARMSubtarget *Subtarget;
153
154 /// AFI - Keep a pointer to ARMFunctionInfo for the current
Evan Cheng6d63a722008-09-18 07:27:23 +0000155 /// MachineFunction.
Evan Chenga8e29892007-01-19 07:51:42 +0000156 ARMFunctionInfo *AFI;
157
Evan Cheng6d63a722008-09-18 07:27:23 +0000158 /// MCP - Keep a pointer to constantpool entries of the current
159 /// MachineFunction.
160 const MachineConstantPool *MCP;
161
Bill Wendling57f0db82009-02-24 08:30:20 +0000162 public:
Chris Lattnerb23569a2010-04-04 08:18:47 +0000163 explicit ARMAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
164 : AsmPrinter(TM, Streamer), AFI(NULL), MCP(NULL) {
Bill Wendling57f0db82009-02-24 08:30:20 +0000165 Subtarget = &TM.getSubtarget<ARMSubtarget>();
166 }
167
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000168 virtual const char *getPassName() const {
169 return "ARM Assembly Printer";
170 }
Jim Grosbachb0739b72010-09-02 01:02:06 +0000171
Chris Lattner35c33bd2010-04-04 04:47:45 +0000172 void printOperand(const MachineInstr *MI, int OpNum, raw_ostream &O,
Evan Chenga8e29892007-01-19 07:51:42 +0000173 const char *Modifier = 0);
Bob Wilson54c78ef2009-11-06 23:33:28 +0000174
Evan Cheng055b0312009-06-29 07:51:04 +0000175 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
Chris Lattnerc75c0282010-04-04 05:29:35 +0000176 unsigned AsmVariant, const char *ExtraCode,
177 raw_ostream &O);
Evan Cheng055b0312009-06-29 07:51:04 +0000178 virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNum,
Bob Wilson224c2442009-05-19 05:53:42 +0000179 unsigned AsmVariant,
Chris Lattnerc75c0282010-04-04 05:29:35 +0000180 const char *ExtraCode, raw_ostream &O);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000181
Jim Grosbach2317e402010-09-30 01:57:53 +0000182 void EmitJumpTable(const MachineInstr *MI);
183 void EmitJump2Table(const MachineInstr *MI);
Chris Lattnera786cea2010-01-28 01:10:34 +0000184 virtual void EmitInstruction(const MachineInstr *MI);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000185 bool runOnMachineFunction(MachineFunction &F);
Jim Grosbachb0739b72010-09-02 01:02:06 +0000186
Chris Lattnera2406192010-01-28 00:19:24 +0000187 virtual void EmitConstantPool() {} // we emit constant pools customly!
Chris Lattner953ebb72010-01-27 23:58:11 +0000188 virtual void EmitFunctionEntryLabel();
Bob Wilson812209a2009-09-30 22:06:26 +0000189 void EmitStartOfAsmFile(Module &M);
Chris Lattner4a071d62009-10-19 17:59:19 +0000190 void EmitEndOfAsmFile(Module &M);
Evan Chenga8e29892007-01-19 07:51:42 +0000191
Jason W Kimdef9ac42010-10-06 22:36:46 +0000192 private:
193 // Helpers for EmitStartOfAsmFile() and EmitEndOfAsmFile()
194 void emitAttributes();
Jason W Kimdef9ac42010-10-06 22:36:46 +0000195
Jason W Kim17b443d2010-10-11 23:01:44 +0000196 // Helper for ELF .o only
197 void emitARMAttributeSection();
198
Jason W Kimdef9ac42010-10-06 22:36:46 +0000199 public:
Jim Grosbach2d0f53b2010-09-28 17:05:56 +0000200 void PrintDebugValueComment(const MachineInstr *MI, raw_ostream &OS);
201
Devang Patel59135f42010-08-04 22:39:39 +0000202 MachineLocation getDebugValueLocation(const MachineInstr *MI) const {
203 MachineLocation Location;
204 assert (MI->getNumOperands() == 4 && "Invalid no. of machine operands!");
205 // Frame address. Currently handles register +- offset only.
206 if (MI->getOperand(0).isReg() && MI->getOperand(1).isImm())
207 Location.set(MI->getOperand(0).getReg(), MI->getOperand(1).getImm());
208 else {
209 DEBUG(dbgs() << "DBG_VALUE instruction ignored! " << *MI << "\n");
210 }
211 return Location;
212 }
213
Jim Grosbach91729002010-07-21 23:03:52 +0000214 virtual unsigned getISAEncoding() {
215 // ARM/Darwin adds ISA to the DWARF info for each function.
216 if (!Subtarget->isTargetDarwin())
217 return 0;
218 return Subtarget->isThumb() ?
219 llvm::ARM::DW_ISA_ARM_thumb : llvm::ARM::DW_ISA_ARM_arm;
220 }
221
Chris Lattner0890cf12010-01-25 19:51:38 +0000222 MCSymbol *GetARMSetPICJumpTableLabel2(unsigned uid, unsigned uid2,
223 const MachineBasicBlock *MBB) const;
224 MCSymbol *GetARMJTIPICJumpTableLabel2(unsigned uid, unsigned uid2) const;
Chris Lattnerbfcb0962010-01-25 19:39:52 +0000225
Jim Grosbach433a5782010-09-24 20:47:58 +0000226 MCSymbol *GetARMSJLJEHLabel(void) const;
227
Evan Cheng711b6dc2008-08-08 06:56:16 +0000228 /// EmitMachineConstantPoolValue - Print a machine constantpool value to
229 /// the .s file.
Evan Chenga8e29892007-01-19 07:51:42 +0000230 virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
Jim Grosbach8da0a572010-11-05 20:34:24 +0000231 int Size = TM.getTargetData()->getTypeAllocSize(MCPV->getType());
Evan Chenga8e29892007-01-19 07:51:42 +0000232
Evan Cheng711b6dc2008-08-08 06:56:16 +0000233 ARMConstantPoolValue *ACPV = static_cast<ARMConstantPoolValue*>(MCPV);
Jim Grosbach8da0a572010-11-05 20:34:24 +0000234 SmallString<128> Str;
235 raw_svector_ostream OS(Str);
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +0000236
237 if (ACPV->isLSDA()) {
Jim Grosbach8da0a572010-11-05 20:34:24 +0000238 OS << MAI->getPrivateGlobalPrefix() << "_LSDA_" << getFunctionNumber();
Bob Wilson28989a82009-11-02 16:59:06 +0000239 } else if (ACPV->isBlockAddress()) {
Jim Grosbach8da0a572010-11-05 20:34:24 +0000240 OS << *GetBlockAddressSymbol(ACPV->getBlockAddress());
Bob Wilson28989a82009-11-02 16:59:06 +0000241 } else if (ACPV->isGlobalValue()) {
Dan Gohman46510a72010-04-15 01:51:59 +0000242 const GlobalValue *GV = ACPV->getGV();
Evan Chenge4e4ed32009-08-28 23:18:09 +0000243 bool isIndirect = Subtarget->isTargetDarwin() &&
Evan Cheng63476a82009-09-03 07:04:02 +0000244 Subtarget->GVIsIndirectSymbol(GV, TM.getRelocationModel());
Evan Chenge4e4ed32009-08-28 23:18:09 +0000245 if (!isIndirect)
Jim Grosbach8da0a572010-11-05 20:34:24 +0000246 OS << *Mang->getSymbol(GV);
Evan Chenge4e4ed32009-08-28 23:18:09 +0000247 else {
248 // FIXME: Remove this when Darwin transition to @GOT like syntax.
Chris Lattner7a2ba942010-01-16 18:37:32 +0000249 MCSymbol *Sym = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
Jim Grosbach8da0a572010-11-05 20:34:24 +0000250 OS << *Sym;
Jim Grosbachb0739b72010-09-02 01:02:06 +0000251
Chris Lattnerb8f64a72009-10-19 18:49:14 +0000252 MachineModuleInfoMachO &MMIMachO =
253 MMI->getObjFileInfo<MachineModuleInfoMachO>();
Bill Wendlingcebae362010-03-10 22:34:10 +0000254 MachineModuleInfoImpl::StubValueTy &StubSym =
Chris Lattnerb8f64a72009-10-19 18:49:14 +0000255 GV->hasHiddenVisibility() ? MMIMachO.getHiddenGVStubEntry(Sym) :
256 MMIMachO.getGVStubEntry(Sym);
Bill Wendlingcebae362010-03-10 22:34:10 +0000257 if (StubSym.getPointer() == 0)
258 StubSym = MachineModuleInfoImpl::
Chris Lattnerd62f1b42010-03-12 21:19:23 +0000259 StubValueTy(Mang->getSymbol(GV), !GV->hasInternalLinkage());
Evan Chenge4e4ed32009-08-28 23:18:09 +0000260 }
Bob Wilson28989a82009-11-02 16:59:06 +0000261 } else {
262 assert(ACPV->isExtSymbol() && "unrecognized constant pool value");
Jim Grosbach8da0a572010-11-05 20:34:24 +0000263 OS << *GetExternalSymbolSymbol(ACPV->getSymbol());
Bob Wilson28989a82009-11-02 16:59:06 +0000264 }
Jim Grosbache9952212009-09-04 01:38:51 +0000265
Jim Grosbach8da0a572010-11-05 20:34:24 +0000266 // Create an MCSymbol for the reference.
267 MCSymbol *MCSym = OutContext.GetOrCreateSymbol(OS.str());
268 const MCExpr *Expr = MCSymbolRefExpr::Create(MCSym, OutContext);
269
270 // FIXME: Model the whole expression an an MCExpr and we can get rid
271 // of this hasRawTextSupport() clause and just do an EmitValue().
272 if (OutStreamer.hasRawTextSupport()) {
273 if (ACPV->hasModifier()) OS << "(" << ACPV->getModifier() << ")";
274 if (ACPV->getPCAdjustment() != 0) {
275 OS << "-(" << MAI->getPrivateGlobalPrefix() << "PC"
276 << getFunctionNumber() << "_" << ACPV->getLabelId()
277 << "+" << (unsigned)ACPV->getPCAdjustment();
278 if (ACPV->mustAddCurrentAddress())
279 OS << "-.";
280 OS << ')';
281 }
282 const char *DataDirective = 0;
283 switch (Size) {
284 case 1: DataDirective = MAI->getData8bitsDirective(0); break;
285 case 2: DataDirective = MAI->getData16bitsDirective(0); break;
286 case 4: DataDirective = MAI->getData32bitsDirective(0); break;
287 default: assert(0 && "Unknown CPV size");
288 }
289 Twine Text(DataDirective, OS.str());
290 OutStreamer.EmitRawText(Text);
291 } else {
292 assert(!ACPV->hasModifier() && ACPV->getPCAdjustment() == 0 &&
293 "ARM binary streamer of non-trivial constant pool value!");
294 OutStreamer.EmitValue(Expr, Size);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000295 }
Evan Chenga8e29892007-01-19 07:51:42 +0000296 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000297 };
298} // end of anonymous namespace
299
Chris Lattner953ebb72010-01-27 23:58:11 +0000300void ARMAsmPrinter::EmitFunctionEntryLabel() {
301 if (AFI->isThumbFunction()) {
Jim Grosbachce792992010-11-05 22:08:08 +0000302 OutStreamer.EmitAssemblerFlag(MCAF_Code16);
303 OutStreamer.EmitThumbFunc(Subtarget->isTargetDarwin()? CurrentFnSym : 0);
Chris Lattner953ebb72010-01-27 23:58:11 +0000304 }
Jim Grosbachb0739b72010-09-02 01:02:06 +0000305
Chris Lattner953ebb72010-01-27 23:58:11 +0000306 OutStreamer.EmitLabel(CurrentFnSym);
307}
308
Jim Grosbach2317e402010-09-30 01:57:53 +0000309/// runOnMachineFunction - This uses the EmitInstruction()
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000310/// method to print assembly for each instruction.
311///
312bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Evan Chenga8e29892007-01-19 07:51:42 +0000313 AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng6d63a722008-09-18 07:27:23 +0000314 MCP = MF.getConstantPool();
Rafael Espindola4b442b52006-05-23 02:48:20 +0000315
Chris Lattnerd49fe1b2010-01-28 01:28:58 +0000316 return AsmPrinter::runOnMachineFunction(MF);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000317}
318
Evan Cheng055b0312009-06-29 07:51:04 +0000319void ARMAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
Chris Lattner35c33bd2010-04-04 04:47:45 +0000320 raw_ostream &O, const char *Modifier) {
Evan Cheng055b0312009-06-29 07:51:04 +0000321 const MachineOperand &MO = MI->getOperand(OpNum);
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000322 unsigned TF = MO.getTargetFlags();
323
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000324 switch (MO.getType()) {
Chris Lattner8bc86cb2009-10-19 20:59:55 +0000325 default:
326 assert(0 && "<unknown operand type>");
Bob Wilson5bafff32009-06-22 23:27:02 +0000327 case MachineOperand::MO_Register: {
328 unsigned Reg = MO.getReg();
Chris Lattner8bc86cb2009-10-19 20:59:55 +0000329 assert(TargetRegisterInfo::isPhysicalRegister(Reg));
Jim Grosbach35636282010-10-06 21:22:32 +0000330 assert(!MO.getSubReg() && "Subregs should be eliminated!");
331 O << ARMInstPrinter::getRegisterName(Reg);
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000332 break;
Bob Wilson5bafff32009-06-22 23:27:02 +0000333 }
Evan Chenga8e29892007-01-19 07:51:42 +0000334 case MachineOperand::MO_Immediate: {
Evan Cheng5adb66a2009-09-28 09:14:39 +0000335 int64_t Imm = MO.getImm();
Anton Korobeynikov632606c2009-10-08 20:43:22 +0000336 O << '#';
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000337 if ((Modifier && strcmp(Modifier, "lo16") == 0) ||
Jim Grosbach4dea9412010-10-06 16:51:55 +0000338 (TF == ARMII::MO_LO16))
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000339 O << ":lower16:";
340 else if ((Modifier && strcmp(Modifier, "hi16") == 0) ||
Jim Grosbach4dea9412010-10-06 16:51:55 +0000341 (TF == ARMII::MO_HI16))
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000342 O << ":upper16:";
Anton Korobeynikov632606c2009-10-08 20:43:22 +0000343 O << Imm;
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000344 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000345 }
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000346 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner1b2eb0e2010-03-13 21:04:28 +0000347 O << *MO.getMBB()->getSymbol();
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000348 return;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000349 case MachineOperand::MO_GlobalAddress: {
Dan Gohman46510a72010-04-15 01:51:59 +0000350 const GlobalValue *GV = MO.getGlobal();
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000351 if ((Modifier && strcmp(Modifier, "lo16") == 0) ||
352 (TF & ARMII::MO_LO16))
353 O << ":lower16:";
354 else if ((Modifier && strcmp(Modifier, "hi16") == 0) ||
355 (TF & ARMII::MO_HI16))
356 O << ":upper16:";
Chris Lattnerd62f1b42010-03-12 21:19:23 +0000357 O << *Mang->getSymbol(GV);
Anton Korobeynikov7751ad92008-11-22 16:15:34 +0000358
Chris Lattner0c08d092010-04-03 22:28:33 +0000359 printOffset(MO.getOffset(), O);
Jim Grosbach1d6111c2010-10-06 21:36:43 +0000360 if (TF == ARMII::MO_PLT)
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000361 O << "(PLT)";
Evan Chenga8e29892007-01-19 07:51:42 +0000362 break;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000363 }
Evan Chenga8e29892007-01-19 07:51:42 +0000364 case MachineOperand::MO_ExternalSymbol: {
Chris Lattner10b318b2010-01-17 21:43:43 +0000365 O << *GetExternalSymbolSymbol(MO.getSymbolName());
Jim Grosbach1d6111c2010-10-06 21:36:43 +0000366 if (TF == ARMII::MO_PLT)
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000367 O << "(PLT)";
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000368 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000369 }
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000370 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner1b46f432010-01-23 07:00:21 +0000371 O << *GetCPISymbol(MO.getIndex());
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000372 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000373 case MachineOperand::MO_JumpTableIndex:
Chris Lattner1b46f432010-01-23 07:00:21 +0000374 O << *GetJTISymbol(MO.getIndex());
Evan Chenga8e29892007-01-19 07:51:42 +0000375 break;
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000376 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000377}
378
Evan Cheng055b0312009-06-29 07:51:04 +0000379//===--------------------------------------------------------------------===//
380
Chris Lattner0890cf12010-01-25 19:51:38 +0000381MCSymbol *ARMAsmPrinter::
382GetARMSetPICJumpTableLabel2(unsigned uid, unsigned uid2,
383 const MachineBasicBlock *MBB) const {
384 SmallString<60> Name;
385 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix()
Chris Lattnerbfcb0962010-01-25 19:39:52 +0000386 << getFunctionNumber() << '_' << uid << '_' << uid2
Chris Lattner0890cf12010-01-25 19:51:38 +0000387 << "_set_" << MBB->getNumber();
Chris Lattner9b97a732010-03-30 18:10:53 +0000388 return OutContext.GetOrCreateSymbol(Name.str());
Chris Lattner0890cf12010-01-25 19:51:38 +0000389}
390
391MCSymbol *ARMAsmPrinter::
392GetARMJTIPICJumpTableLabel2(unsigned uid, unsigned uid2) const {
393 SmallString<60> Name;
394 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix() << "JTI"
Chris Lattner281e7762010-01-25 23:28:03 +0000395 << getFunctionNumber() << '_' << uid << '_' << uid2;
Chris Lattner9b97a732010-03-30 18:10:53 +0000396 return OutContext.GetOrCreateSymbol(Name.str());
Chris Lattnerbfcb0962010-01-25 19:39:52 +0000397}
398
Jim Grosbach433a5782010-09-24 20:47:58 +0000399
400MCSymbol *ARMAsmPrinter::GetARMSJLJEHLabel(void) const {
401 SmallString<60> Name;
402 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix() << "SJLJEH"
403 << getFunctionNumber();
404 return OutContext.GetOrCreateSymbol(Name.str());
405}
406
Evan Cheng055b0312009-06-29 07:51:04 +0000407bool ARMAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
Chris Lattnerc75c0282010-04-04 05:29:35 +0000408 unsigned AsmVariant, const char *ExtraCode,
409 raw_ostream &O) {
Evan Chenga8e29892007-01-19 07:51:42 +0000410 // Does this asm operand have a single letter operand modifier?
411 if (ExtraCode && ExtraCode[0]) {
412 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov8e9ece72009-08-08 23:10:41 +0000413
Evan Chenga8e29892007-01-19 07:51:42 +0000414 switch (ExtraCode[0]) {
415 default: return true; // Unknown modifier.
Bob Wilson9b4b00a2009-07-09 23:54:51 +0000416 case 'a': // Print as a memory address.
417 if (MI->getOperand(OpNum).isReg()) {
Jim Grosbach2f24c4e2010-09-30 15:25:22 +0000418 O << "["
419 << ARMInstPrinter::getRegisterName(MI->getOperand(OpNum).getReg())
420 << "]";
Bob Wilson9b4b00a2009-07-09 23:54:51 +0000421 return false;
422 }
423 // Fallthrough
424 case 'c': // Don't print "#" before an immediate operand.
Bob Wilson4f38b382009-08-21 21:58:55 +0000425 if (!MI->getOperand(OpNum).isImm())
426 return true;
Jim Grosbach2317e402010-09-30 01:57:53 +0000427 O << MI->getOperand(OpNum).getImm();
Bob Wilson8f343462009-04-06 21:46:51 +0000428 return false;
Evan Chenge21e3962007-04-04 00:13:29 +0000429 case 'P': // Print a VFP double precision register.
Evan Chengd831cda2009-12-08 23:06:22 +0000430 case 'q': // Print a NEON quad precision register.
Chris Lattner35c33bd2010-04-04 04:47:45 +0000431 printOperand(MI, OpNum, O);
Evan Cheng23a95702007-03-08 22:42:46 +0000432 return false;
Evan Chenga8e29892007-01-19 07:51:42 +0000433 case 'Q':
Bob Wilsond984eb62010-05-27 20:23:42 +0000434 case 'R':
Bob Wilsond984eb62010-05-27 20:23:42 +0000435 case 'H':
Evan Cheng12616722010-05-27 23:45:31 +0000436 report_fatal_error("llvm does not support 'Q', 'R', and 'H' modifiers!");
Bob Wilsond984eb62010-05-27 20:23:42 +0000437 return true;
Evan Cheng84f60b72010-05-27 22:08:38 +0000438 }
Evan Chenga8e29892007-01-19 07:51:42 +0000439 }
Jim Grosbache9952212009-09-04 01:38:51 +0000440
Chris Lattner35c33bd2010-04-04 04:47:45 +0000441 printOperand(MI, OpNum, O);
Evan Chenga8e29892007-01-19 07:51:42 +0000442 return false;
443}
444
Bob Wilson224c2442009-05-19 05:53:42 +0000445bool ARMAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Evan Cheng055b0312009-06-29 07:51:04 +0000446 unsigned OpNum, unsigned AsmVariant,
Chris Lattnerc75c0282010-04-04 05:29:35 +0000447 const char *ExtraCode,
448 raw_ostream &O) {
Bob Wilson224c2442009-05-19 05:53:42 +0000449 if (ExtraCode && ExtraCode[0])
450 return true; // Unknown modifier.
Bob Wilson765cc0b2009-10-13 20:50:28 +0000451
452 const MachineOperand &MO = MI->getOperand(OpNum);
453 assert(MO.isReg() && "unexpected inline asm memory operand");
Jim Grosbach2317e402010-09-30 01:57:53 +0000454 O << "[" << ARMInstPrinter::getRegisterName(MO.getReg()) << "]";
Bob Wilson224c2442009-05-19 05:53:42 +0000455 return false;
456}
457
Bob Wilson812209a2009-09-30 22:06:26 +0000458void ARMAsmPrinter::EmitStartOfAsmFile(Module &M) {
Bob Wilson0fb34682009-09-30 00:23:42 +0000459 if (Subtarget->isTargetDarwin()) {
460 Reloc::Model RelocM = TM.getRelocationModel();
461 if (RelocM == Reloc::PIC_ || RelocM == Reloc::DynamicNoPIC) {
462 // Declare all the text sections up front (before the DWARF sections
463 // emitted by AsmPrinter::doInitialization) so the assembler will keep
464 // them together at the beginning of the object file. This helps
465 // avoid out-of-range branches that are due a fundamental limitation of
466 // the way symbol offsets are encoded with the current Darwin ARM
467 // relocations.
Jim Grosbachb0739b72010-09-02 01:02:06 +0000468 const TargetLoweringObjectFileMachO &TLOFMacho =
Dan Gohman0d805c32010-04-17 16:44:48 +0000469 static_cast<const TargetLoweringObjectFileMachO &>(
470 getObjFileLowering());
Bob Wilson29e06692009-09-30 22:25:37 +0000471 OutStreamer.SwitchSection(TLOFMacho.getTextSection());
472 OutStreamer.SwitchSection(TLOFMacho.getTextCoalSection());
473 OutStreamer.SwitchSection(TLOFMacho.getConstTextCoalSection());
474 if (RelocM == Reloc::DynamicNoPIC) {
475 const MCSection *sect =
Chris Lattner22772212010-04-08 20:40:11 +0000476 OutContext.getMachOSection("__TEXT", "__symbol_stub4",
477 MCSectionMachO::S_SYMBOL_STUBS,
478 12, SectionKind::getText());
Bob Wilson29e06692009-09-30 22:25:37 +0000479 OutStreamer.SwitchSection(sect);
480 } else {
481 const MCSection *sect =
Chris Lattner22772212010-04-08 20:40:11 +0000482 OutContext.getMachOSection("__TEXT", "__picsymbolstub4",
483 MCSectionMachO::S_SYMBOL_STUBS,
484 16, SectionKind::getText());
Bob Wilson29e06692009-09-30 22:25:37 +0000485 OutStreamer.SwitchSection(sect);
486 }
Bob Wilson63db5942010-07-30 19:55:47 +0000487 const MCSection *StaticInitSect =
488 OutContext.getMachOSection("__TEXT", "__StaticInit",
489 MCSectionMachO::S_REGULAR |
490 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
491 SectionKind::getText());
492 OutStreamer.SwitchSection(StaticInitSect);
Bob Wilson0fb34682009-09-30 00:23:42 +0000493 }
494 }
495
Jim Grosbache5165492009-11-09 00:11:35 +0000496 // Use unified assembler syntax.
Jason W Kimafd1cc22010-09-30 02:45:56 +0000497 OutStreamer.EmitAssemblerFlag(MCAF_SyntaxUnified);
Anton Korobeynikovd61eca52009-06-17 23:43:18 +0000498
Anton Korobeynikov88ce6672009-05-23 19:51:20 +0000499 // Emit ARM Build Attributes
500 if (Subtarget->isTargetELF()) {
Anton Korobeynikov88ce6672009-05-23 19:51:20 +0000501
Jason W Kimdef9ac42010-10-06 22:36:46 +0000502 emitAttributes();
Anton Korobeynikov88ce6672009-05-23 19:51:20 +0000503 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000504}
505
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +0000506
Chris Lattner4a071d62009-10-19 17:59:19 +0000507void ARMAsmPrinter::EmitEndOfAsmFile(Module &M) {
Evan Cheng5be54b02007-01-19 19:25:36 +0000508 if (Subtarget->isTargetDarwin()) {
Chris Lattnerf61159b2009-08-03 22:18:15 +0000509 // All darwin targets use mach-o.
Dan Gohman0d805c32010-04-17 16:44:48 +0000510 const TargetLoweringObjectFileMachO &TLOFMacho =
511 static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattnerb0f294c2009-10-19 18:38:33 +0000512 MachineModuleInfoMachO &MMIMacho =
513 MMI->getObjFileInfo<MachineModuleInfoMachO>();
Jim Grosbache9952212009-09-04 01:38:51 +0000514
Evan Chenga8e29892007-01-19 07:51:42 +0000515 // Output non-lazy-pointers for external and common global variables.
Chris Lattnerb0f294c2009-10-19 18:38:33 +0000516 MachineModuleInfoMachO::SymbolListTy Stubs = MMIMacho.GetGVStubList();
Bill Wendlingcebae362010-03-10 22:34:10 +0000517
Chris Lattnerb0f294c2009-10-19 18:38:33 +0000518 if (!Stubs.empty()) {
Chris Lattnerff4bc462009-08-10 01:39:42 +0000519 // Switch with ".non_lazy_symbol_pointer" directive.
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000520 OutStreamer.SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
Chris Lattnerc076a972009-08-10 18:01:34 +0000521 EmitAlignment(2);
Chris Lattnerb0f294c2009-10-19 18:38:33 +0000522 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Bill Wendlingbecd83e2010-03-09 00:40:17 +0000523 // L_foo$stub:
524 OutStreamer.EmitLabel(Stubs[i].first);
525 // .indirect_symbol _foo
Bill Wendling52a50e52010-03-11 01:18:13 +0000526 MachineModuleInfoImpl::StubValueTy &MCSym = Stubs[i].second;
527 OutStreamer.EmitSymbolAttribute(MCSym.getPointer(),MCSA_IndirectSymbol);
Bill Wendlingcf6f28d2010-03-09 00:43:34 +0000528
Bill Wendling52a50e52010-03-11 01:18:13 +0000529 if (MCSym.getInt())
Bill Wendlingcf6f28d2010-03-09 00:43:34 +0000530 // External to current translation unit.
531 OutStreamer.EmitIntValue(0, 4/*size*/, 0/*addrspace*/);
532 else
533 // Internal to current translation unit.
Bill Wendling5e1b55d2010-03-31 18:47:10 +0000534 //
Jim Grosbach1b935a32010-09-22 16:45:13 +0000535 // When we place the LSDA into the TEXT section, the type info
536 // pointers need to be indirect and pc-rel. We accomplish this by
537 // using NLPs; however, sometimes the types are local to the file.
538 // We need to fill in the value for the NLP in those cases.
Bill Wendling52a50e52010-03-11 01:18:13 +0000539 OutStreamer.EmitValue(MCSymbolRefExpr::Create(MCSym.getPointer(),
540 OutContext),
Bill Wendlingcf6f28d2010-03-09 00:43:34 +0000541 4/*size*/, 0/*addrspace*/);
Evan Chengae94e592008-12-05 01:06:39 +0000542 }
Bill Wendlingbecd83e2010-03-09 00:40:17 +0000543
544 Stubs.clear();
545 OutStreamer.AddBlankLine();
Evan Chenga8e29892007-01-19 07:51:42 +0000546 }
547
Chris Lattnere4d9ea82009-10-19 18:44:38 +0000548 Stubs = MMIMacho.GetHiddenGVStubList();
549 if (!Stubs.empty()) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000550 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
Chris Lattnerf3231de2009-08-10 18:02:16 +0000551 EmitAlignment(2);
Bill Wendlingbecd83e2010-03-09 00:40:17 +0000552 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
553 // L_foo$stub:
554 OutStreamer.EmitLabel(Stubs[i].first);
555 // .long _foo
Bill Wendlingcebae362010-03-10 22:34:10 +0000556 OutStreamer.EmitValue(MCSymbolRefExpr::
557 Create(Stubs[i].second.getPointer(),
558 OutContext),
Bill Wendlingbecd83e2010-03-09 00:40:17 +0000559 4/*size*/, 0/*addrspace*/);
560 }
Bill Wendlingcf6f28d2010-03-09 00:43:34 +0000561
562 Stubs.clear();
563 OutStreamer.AddBlankLine();
Evan Chengae94e592008-12-05 01:06:39 +0000564 }
565
Evan Chenga8e29892007-01-19 07:51:42 +0000566 // Funny Darwin hack: This flag tells the linker that no global symbols
567 // contain code that falls through to other global symbols (e.g. the obvious
568 // implementation of multiple entry points). If this doesn't occur, the
569 // linker can safely perform dead code stripping. Since LLVM never
570 // generates code that does this, it is always safe to set.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000571 OutStreamer.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000572 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000573}
Anton Korobeynikov0bd89712008-08-17 13:55:10 +0000574
Chris Lattner97f06932009-10-19 20:20:46 +0000575//===----------------------------------------------------------------------===//
Jason W Kimdef9ac42010-10-06 22:36:46 +0000576// Helper routines for EmitStartOfAsmFile() and EmitEndOfAsmFile()
577// FIXME:
578// The following seem like one-off assembler flags, but they actually need
Jim Grosbachfa7fb642010-10-06 22:46:47 +0000579// to appear in the .ARM.attributes section in ELF.
Jason W Kimdef9ac42010-10-06 22:36:46 +0000580// Instead of subclassing the MCELFStreamer, we do the work here.
581
582void ARMAsmPrinter::emitAttributes() {
Jim Grosbachfa7fb642010-10-06 22:46:47 +0000583
Jason W Kim17b443d2010-10-11 23:01:44 +0000584 emitARMAttributeSection();
585
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000586 AttributeEmitter *AttrEmitter;
587 if (OutStreamer.hasRawTextSupport())
588 AttrEmitter = new AsmAttributeEmitter(OutStreamer);
589 else {
590 MCObjectStreamer &O = static_cast<MCObjectStreamer&>(OutStreamer);
591 AttrEmitter = new ObjectAttributeEmitter(O);
592 }
593
594 AttrEmitter->MaybeSwitchVendor("aeabi");
595
Jason W Kimdef9ac42010-10-06 22:36:46 +0000596 std::string CPUString = Subtarget->getCPUString();
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000597 if (OutStreamer.hasRawTextSupport()) {
598 if (CPUString != "generic")
599 OutStreamer.EmitRawText(StringRef("\t.cpu ") + CPUString);
600 } else {
601 assert(CPUString == "generic" && "Unsupported .cpu attribute for ELF/.o");
602 // FIXME: Why these defaults?
603 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch, ARMBuildAttrs::v4T);
604 AttrEmitter->EmitAttribute(ARMBuildAttrs::ARM_ISA_use, 1);
605 AttrEmitter->EmitAttribute(ARMBuildAttrs::THUMB_ISA_use, 1);
606 }
Jason W Kimdef9ac42010-10-06 22:36:46 +0000607
608 // FIXME: Emit FPU type
609 if (Subtarget->hasVFP2())
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000610 AttrEmitter->EmitAttribute(ARMBuildAttrs::VFP_arch, 2);
Jason W Kimdef9ac42010-10-06 22:36:46 +0000611
612 // Signal various FP modes.
613 if (!UnsafeFPMath) {
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000614 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_FP_denormal, 1);
615 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_FP_exceptions, 1);
Jason W Kimdef9ac42010-10-06 22:36:46 +0000616 }
617
618 if (NoInfsFPMath && NoNaNsFPMath)
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000619 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_FP_number_model, 1);
Jason W Kimdef9ac42010-10-06 22:36:46 +0000620 else
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000621 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_FP_number_model, 3);
Jason W Kimdef9ac42010-10-06 22:36:46 +0000622
623 // 8-bytes alignment stuff.
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000624 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_align8_needed, 1);
625 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_align8_preserved, 1);
Jason W Kimdef9ac42010-10-06 22:36:46 +0000626
627 // Hard float. Use both S and D registers and conform to AAPCS-VFP.
628 if (Subtarget->isAAPCS_ABI() && FloatABIType == FloatABI::Hard) {
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000629 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_HardFP_use, 3);
630 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_VFP_args, 1);
Jason W Kimdef9ac42010-10-06 22:36:46 +0000631 }
632 // FIXME: Should we signal R9 usage?
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000633
634 AttrEmitter->EmitAttribute(ARMBuildAttrs::DIV_use, 1);
635
636 AttrEmitter->Finish();
637 delete AttrEmitter;
Jason W Kimdef9ac42010-10-06 22:36:46 +0000638}
639
Jason W Kim17b443d2010-10-11 23:01:44 +0000640void ARMAsmPrinter::emitARMAttributeSection() {
641 // <format-version>
642 // [ <section-length> "vendor-name"
643 // [ <file-tag> <size> <attribute>*
644 // | <section-tag> <size> <section-number>* 0 <attribute>*
645 // | <symbol-tag> <size> <symbol-number>* 0 <attribute>*
646 // ]+
647 // ]*
648
649 if (OutStreamer.hasRawTextSupport())
650 return;
651
652 const ARMElfTargetObjectFile &TLOFELF =
653 static_cast<const ARMElfTargetObjectFile &>
654 (getObjFileLowering());
655
656 OutStreamer.SwitchSection(TLOFELF.getAttributesSection());
Jason W Kim17b443d2010-10-11 23:01:44 +0000657
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000658 // Format version
659 OutStreamer.EmitIntValue(0x41, 1);
Jason W Kim17b443d2010-10-11 23:01:44 +0000660}
661
Jason W Kimdef9ac42010-10-06 22:36:46 +0000662//===----------------------------------------------------------------------===//
Chris Lattner97f06932009-10-19 20:20:46 +0000663
Jim Grosbach988ce092010-09-18 00:05:05 +0000664static MCSymbol *getPICLabel(const char *Prefix, unsigned FunctionNumber,
665 unsigned LabelId, MCContext &Ctx) {
666
667 MCSymbol *Label = Ctx.GetOrCreateSymbol(Twine(Prefix)
668 + "PC" + Twine(FunctionNumber) + "_" + Twine(LabelId));
669 return Label;
670}
671
Jim Grosbacha2244cb2010-09-22 17:39:48 +0000672void ARMAsmPrinter::EmitJumpTable(const MachineInstr *MI) {
673 unsigned Opcode = MI->getOpcode();
674 int OpNum = 1;
675 if (Opcode == ARM::BR_JTadd)
676 OpNum = 2;
677 else if (Opcode == ARM::BR_JTm)
678 OpNum = 3;
679
680 const MachineOperand &MO1 = MI->getOperand(OpNum);
681 const MachineOperand &MO2 = MI->getOperand(OpNum+1); // Unique Id
682 unsigned JTI = MO1.getIndex();
683
684 // Emit a label for the jump table.
685 MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel2(JTI, MO2.getImm());
686 OutStreamer.EmitLabel(JTISymbol);
687
688 // Emit each entry of the table.
689 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
690 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
691 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
692
693 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
694 MachineBasicBlock *MBB = JTBBs[i];
695 // Construct an MCExpr for the entry. We want a value of the form:
696 // (BasicBlockAddr - TableBeginAddr)
697 //
698 // For example, a table with entries jumping to basic blocks BB0 and BB1
699 // would look like:
700 // LJTI_0_0:
701 // .word (LBB0 - LJTI_0_0)
702 // .word (LBB1 - LJTI_0_0)
703 const MCExpr *Expr = MCSymbolRefExpr::Create(MBB->getSymbol(), OutContext);
704
705 if (TM.getRelocationModel() == Reloc::PIC_)
706 Expr = MCBinaryExpr::CreateSub(Expr, MCSymbolRefExpr::Create(JTISymbol,
707 OutContext),
708 OutContext);
709 OutStreamer.EmitValue(Expr, 4);
710 }
711}
712
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000713void ARMAsmPrinter::EmitJump2Table(const MachineInstr *MI) {
714 unsigned Opcode = MI->getOpcode();
715 int OpNum = (Opcode == ARM::t2BR_JT) ? 2 : 1;
716 const MachineOperand &MO1 = MI->getOperand(OpNum);
717 const MachineOperand &MO2 = MI->getOperand(OpNum+1); // Unique Id
718 unsigned JTI = MO1.getIndex();
719
720 // Emit a label for the jump table.
721 MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel2(JTI, MO2.getImm());
722 OutStreamer.EmitLabel(JTISymbol);
723
724 // Emit each entry of the table.
725 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
726 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
727 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000728 unsigned OffsetWidth = 4;
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000729 if (MI->getOpcode() == ARM::t2TBB)
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000730 OffsetWidth = 1;
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000731 else if (MI->getOpcode() == ARM::t2TBH)
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000732 OffsetWidth = 2;
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000733
734 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
735 MachineBasicBlock *MBB = JTBBs[i];
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000736 const MCExpr *MBBSymbolExpr = MCSymbolRefExpr::Create(MBB->getSymbol(),
737 OutContext);
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000738 // If this isn't a TBB or TBH, the entries are direct branch instructions.
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000739 if (OffsetWidth == 4) {
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000740 MCInst BrInst;
741 BrInst.setOpcode(ARM::t2B);
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000742 BrInst.addOperand(MCOperand::CreateExpr(MBBSymbolExpr));
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000743 OutStreamer.EmitInstruction(BrInst);
744 continue;
745 }
746 // Otherwise it's an offset from the dispatch instruction. Construct an
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000747 // MCExpr for the entry. We want a value of the form:
748 // (BasicBlockAddr - TableBeginAddr) / 2
749 //
750 // For example, a TBB table with entries jumping to basic blocks BB0 and BB1
751 // would look like:
752 // LJTI_0_0:
753 // .byte (LBB0 - LJTI_0_0) / 2
754 // .byte (LBB1 - LJTI_0_0) / 2
755 const MCExpr *Expr =
756 MCBinaryExpr::CreateSub(MBBSymbolExpr,
757 MCSymbolRefExpr::Create(JTISymbol, OutContext),
758 OutContext);
759 Expr = MCBinaryExpr::CreateDiv(Expr, MCConstantExpr::Create(2, OutContext),
760 OutContext);
761 OutStreamer.EmitValue(Expr, OffsetWidth);
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000762 }
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000763
764 // Make sure the instruction that follows TBB is 2-byte aligned.
765 // FIXME: Constant island pass should insert an "ALIGN" instruction instead.
766 if (MI->getOpcode() == ARM::t2TBB)
767 EmitAlignment(1);
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000768}
769
Jim Grosbach2d0f53b2010-09-28 17:05:56 +0000770void ARMAsmPrinter::PrintDebugValueComment(const MachineInstr *MI,
771 raw_ostream &OS) {
772 unsigned NOps = MI->getNumOperands();
773 assert(NOps==4);
774 OS << '\t' << MAI->getCommentString() << "DEBUG_VALUE: ";
775 // cast away const; DIetc do not take const operands for some reason.
776 DIVariable V(const_cast<MDNode *>(MI->getOperand(NOps-1).getMetadata()));
777 OS << V.getName();
778 OS << " <- ";
779 // Frame address. Currently handles register +- offset only.
780 assert(MI->getOperand(0).isReg() && MI->getOperand(1).isImm());
781 OS << '['; printOperand(MI, 0, OS); OS << '+'; printOperand(MI, 1, OS);
782 OS << ']';
783 OS << "+";
784 printOperand(MI, NOps-2, OS);
785}
786
Jim Grosbachb454cda2010-09-29 15:23:40 +0000787void ARMAsmPrinter::EmitInstruction(const MachineInstr *MI) {
Chris Lattner96bc2172009-10-20 00:52:47 +0000788 ARMMCInstLower MCInstLowering(OutContext, *Mang, *this);
Chris Lattner97f06932009-10-19 20:20:46 +0000789 switch (MI->getOpcode()) {
Chris Lattnerc6b8a992009-10-20 05:58:02 +0000790 case ARM::t2MOVi32imm:
791 assert(0 && "Should be lowered by thumb2it pass");
Chris Lattner4d152222009-10-19 22:23:04 +0000792 default: break;
Jim Grosbach2d0f53b2010-09-28 17:05:56 +0000793 case ARM::DBG_VALUE: {
794 if (isVerbose() && OutStreamer.hasRawTextSupport()) {
795 SmallString<128> TmpStr;
796 raw_svector_ostream OS(TmpStr);
797 PrintDebugValueComment(MI, OS);
798 OutStreamer.EmitRawText(StringRef(OS.str()));
799 }
800 return;
801 }
Jim Grosbachfbd18732010-09-17 23:41:53 +0000802 case ARM::tPICADD: {
803 // This is a pseudo op for a label + instruction sequence, which looks like:
804 // LPC0:
805 // add r0, pc
806 // This adds the address of LPC0 to r0.
807
808 // Emit the label.
Jim Grosbach988ce092010-09-18 00:05:05 +0000809 OutStreamer.EmitLabel(getPICLabel(MAI->getPrivateGlobalPrefix(),
810 getFunctionNumber(), MI->getOperand(2).getImm(),
811 OutContext));
Jim Grosbachfbd18732010-09-17 23:41:53 +0000812
813 // Form and emit the add.
814 MCInst AddInst;
815 AddInst.setOpcode(ARM::tADDhirr);
816 AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
817 AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
818 AddInst.addOperand(MCOperand::CreateReg(ARM::PC));
819 // Add predicate operands.
820 AddInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
821 AddInst.addOperand(MCOperand::CreateReg(0));
822 OutStreamer.EmitInstruction(AddInst);
823 return;
824 }
Jim Grosbacha3fbadf2010-09-30 19:53:58 +0000825 case ARM::PICADD: {
Chris Lattner4d152222009-10-19 22:23:04 +0000826 // This is a pseudo op for a label + instruction sequence, which looks like:
827 // LPC0:
828 // add r0, pc, r0
829 // This adds the address of LPC0 to r0.
Jim Grosbachb0739b72010-09-02 01:02:06 +0000830
Chris Lattner4d152222009-10-19 22:23:04 +0000831 // Emit the label.
Jim Grosbach988ce092010-09-18 00:05:05 +0000832 OutStreamer.EmitLabel(getPICLabel(MAI->getPrivateGlobalPrefix(),
833 getFunctionNumber(), MI->getOperand(2).getImm(),
834 OutContext));
Jim Grosbachb0739b72010-09-02 01:02:06 +0000835
Jim Grosbachf3f09522010-09-14 21:05:34 +0000836 // Form and emit the add.
Chris Lattner4d152222009-10-19 22:23:04 +0000837 MCInst AddInst;
838 AddInst.setOpcode(ARM::ADDrr);
839 AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
840 AddInst.addOperand(MCOperand::CreateReg(ARM::PC));
841 AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg()));
Jim Grosbach5b46d622010-09-14 21:28:17 +0000842 // Add predicate operands.
843 AddInst.addOperand(MCOperand::CreateImm(MI->getOperand(3).getImm()));
844 AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(4).getReg()));
845 // Add 's' bit operand (always reg0 for this)
846 AddInst.addOperand(MCOperand::CreateReg(0));
Chris Lattner850d2e22010-02-03 01:16:28 +0000847 OutStreamer.EmitInstruction(AddInst);
Chris Lattner4d152222009-10-19 22:23:04 +0000848 return;
849 }
Jim Grosbacha28abbe2010-09-17 16:25:52 +0000850 case ARM::PICSTR:
851 case ARM::PICSTRB:
852 case ARM::PICSTRH:
853 case ARM::PICLDR:
854 case ARM::PICLDRB:
855 case ARM::PICLDRH:
856 case ARM::PICLDRSB:
857 case ARM::PICLDRSH: {
Jim Grosbachb74ca9d2010-09-16 17:43:25 +0000858 // This is a pseudo op for a label + instruction sequence, which looks like:
859 // LPC0:
Jim Grosbacha28abbe2010-09-17 16:25:52 +0000860 // OP r0, [pc, r0]
Jim Grosbachb74ca9d2010-09-16 17:43:25 +0000861 // The LCP0 label is referenced by a constant pool entry in order to get
862 // a PC-relative address at the ldr instruction.
863
864 // Emit the label.
Jim Grosbach988ce092010-09-18 00:05:05 +0000865 OutStreamer.EmitLabel(getPICLabel(MAI->getPrivateGlobalPrefix(),
866 getFunctionNumber(), MI->getOperand(2).getImm(),
867 OutContext));
Jim Grosbachb74ca9d2010-09-16 17:43:25 +0000868
869 // Form and emit the load
Jim Grosbacha28abbe2010-09-17 16:25:52 +0000870 unsigned Opcode;
871 switch (MI->getOpcode()) {
872 default:
873 llvm_unreachable("Unexpected opcode!");
Jim Grosbach7e3383c2010-10-27 23:12:14 +0000874 case ARM::PICSTR: Opcode = ARM::STRrs; break;
875 case ARM::PICSTRB: Opcode = ARM::STRBrs; break;
Jim Grosbacha28abbe2010-09-17 16:25:52 +0000876 case ARM::PICSTRH: Opcode = ARM::STRH; break;
Jim Grosbach3e556122010-10-26 22:37:02 +0000877 case ARM::PICLDR: Opcode = ARM::LDRrs; break;
Jim Grosbachc1d30212010-10-27 00:19:44 +0000878 case ARM::PICLDRB: Opcode = ARM::LDRBrs; break;
Jim Grosbacha28abbe2010-09-17 16:25:52 +0000879 case ARM::PICLDRH: Opcode = ARM::LDRH; break;
880 case ARM::PICLDRSB: Opcode = ARM::LDRSB; break;
881 case ARM::PICLDRSH: Opcode = ARM::LDRSH; break;
882 }
883 MCInst LdStInst;
884 LdStInst.setOpcode(Opcode);
885 LdStInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
886 LdStInst.addOperand(MCOperand::CreateReg(ARM::PC));
887 LdStInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg()));
888 LdStInst.addOperand(MCOperand::CreateImm(0));
Jim Grosbachb74ca9d2010-09-16 17:43:25 +0000889 // Add predicate operands.
Jim Grosbacha28abbe2010-09-17 16:25:52 +0000890 LdStInst.addOperand(MCOperand::CreateImm(MI->getOperand(3).getImm()));
891 LdStInst.addOperand(MCOperand::CreateReg(MI->getOperand(4).getReg()));
892 OutStreamer.EmitInstruction(LdStInst);
Jim Grosbachb74ca9d2010-09-16 17:43:25 +0000893
894 return;
895 }
Jim Grosbacha3fbadf2010-09-30 19:53:58 +0000896 case ARM::CONSTPOOL_ENTRY: {
Chris Lattnera70e6442009-10-19 22:33:05 +0000897 /// CONSTPOOL_ENTRY - This instruction represents a floating constant pool
898 /// in the function. The first operand is the ID# for this instruction, the
899 /// second is the index into the MachineConstantPool that this is, the third
900 /// is the size in bytes of this constant pool entry.
901 unsigned LabelId = (unsigned)MI->getOperand(0).getImm();
902 unsigned CPIdx = (unsigned)MI->getOperand(1).getIndex();
903
904 EmitAlignment(2);
Chris Lattner1b46f432010-01-23 07:00:21 +0000905 OutStreamer.EmitLabel(GetCPISymbol(LabelId));
Chris Lattnera70e6442009-10-19 22:33:05 +0000906
907 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPIdx];
908 if (MCPE.isMachineConstantPoolEntry())
909 EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
910 else
911 EmitGlobalConstant(MCPE.Val.ConstVal);
Jim Grosbachb0739b72010-09-02 01:02:06 +0000912
Chris Lattnera70e6442009-10-19 22:33:05 +0000913 return;
914 }
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000915 case ARM::t2TBB:
916 case ARM::t2TBH:
917 case ARM::t2BR_JT: {
918 // Lower and emit the instruction itself, then the jump table following it.
919 MCInst TmpInst;
920 MCInstLowering.Lower(MI, TmpInst);
921 OutStreamer.EmitInstruction(TmpInst);
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000922 EmitJump2Table(MI);
923 return;
924 }
925 case ARM::tBR_JTr:
926 case ARM::BR_JTr:
927 case ARM::BR_JTm:
Jim Grosbacha2244cb2010-09-22 17:39:48 +0000928 case ARM::BR_JTadd: {
929 // Lower and emit the instruction itself, then the jump table following it.
930 MCInst TmpInst;
931 MCInstLowering.Lower(MI, TmpInst);
932 OutStreamer.EmitInstruction(TmpInst);
933 EmitJumpTable(MI);
934 return;
935 }
Jim Grosbach2e6ae132010-09-23 18:05:37 +0000936 case ARM::TRAP: {
937 // Non-Darwin binutils don't yet support the "trap" mnemonic.
938 // FIXME: Remove this special case when they do.
939 if (!Subtarget->isTargetDarwin()) {
Jim Grosbach78890f42010-10-01 23:21:38 +0000940 //.long 0xe7ffdefe @ trap
Jim Grosbachb2dda4b2010-09-23 19:42:17 +0000941 uint32_t Val = 0xe7ffdefeUL;
Jim Grosbach2e6ae132010-09-23 18:05:37 +0000942 OutStreamer.AddComment("trap");
943 OutStreamer.EmitIntValue(Val, 4);
944 return;
945 }
946 break;
947 }
948 case ARM::tTRAP: {
949 // Non-Darwin binutils don't yet support the "trap" mnemonic.
950 // FIXME: Remove this special case when they do.
951 if (!Subtarget->isTargetDarwin()) {
Jim Grosbach78890f42010-10-01 23:21:38 +0000952 //.short 57086 @ trap
Benjamin Kramerc8ab9eb2010-09-23 18:57:26 +0000953 uint16_t Val = 0xdefe;
Jim Grosbach2e6ae132010-09-23 18:05:37 +0000954 OutStreamer.AddComment("trap");
955 OutStreamer.EmitIntValue(Val, 2);
956 return;
957 }
958 break;
959 }
Jim Grosbach433a5782010-09-24 20:47:58 +0000960 case ARM::t2Int_eh_sjlj_setjmp:
961 case ARM::t2Int_eh_sjlj_setjmp_nofp:
Jim Grosbacha3fbadf2010-09-30 19:53:58 +0000962 case ARM::tInt_eh_sjlj_setjmp: {
Jim Grosbach433a5782010-09-24 20:47:58 +0000963 // Two incoming args: GPR:$src, GPR:$val
964 // mov $val, pc
965 // adds $val, #7
966 // str $val, [$src, #4]
967 // movs r0, #0
968 // b 1f
969 // movs r0, #1
970 // 1:
971 unsigned SrcReg = MI->getOperand(0).getReg();
972 unsigned ValReg = MI->getOperand(1).getReg();
973 MCSymbol *Label = GetARMSJLJEHLabel();
974 {
975 MCInst TmpInst;
976 TmpInst.setOpcode(ARM::tMOVgpr2tgpr);
977 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
978 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
979 // 's' bit operand
980 TmpInst.addOperand(MCOperand::CreateReg(ARM::CPSR));
981 OutStreamer.AddComment("eh_setjmp begin");
982 OutStreamer.EmitInstruction(TmpInst);
983 }
984 {
985 MCInst TmpInst;
986 TmpInst.setOpcode(ARM::tADDi3);
987 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
988 // 's' bit operand
989 TmpInst.addOperand(MCOperand::CreateReg(ARM::CPSR));
990 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
991 TmpInst.addOperand(MCOperand::CreateImm(7));
992 // Predicate.
993 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
994 TmpInst.addOperand(MCOperand::CreateReg(0));
995 OutStreamer.EmitInstruction(TmpInst);
996 }
997 {
998 MCInst TmpInst;
999 TmpInst.setOpcode(ARM::tSTR);
1000 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
1001 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
1002 // The offset immediate is #4. The operand value is scaled by 4 for the
1003 // tSTR instruction.
1004 TmpInst.addOperand(MCOperand::CreateImm(1));
1005 TmpInst.addOperand(MCOperand::CreateReg(0));
1006 // Predicate.
1007 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1008 TmpInst.addOperand(MCOperand::CreateReg(0));
1009 OutStreamer.EmitInstruction(TmpInst);
1010 }
1011 {
1012 MCInst TmpInst;
1013 TmpInst.setOpcode(ARM::tMOVi8);
1014 TmpInst.addOperand(MCOperand::CreateReg(ARM::R0));
1015 TmpInst.addOperand(MCOperand::CreateReg(ARM::CPSR));
1016 TmpInst.addOperand(MCOperand::CreateImm(0));
1017 // Predicate.
1018 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1019 TmpInst.addOperand(MCOperand::CreateReg(0));
1020 OutStreamer.EmitInstruction(TmpInst);
1021 }
1022 {
1023 const MCExpr *SymbolExpr = MCSymbolRefExpr::Create(Label, OutContext);
1024 MCInst TmpInst;
1025 TmpInst.setOpcode(ARM::tB);
1026 TmpInst.addOperand(MCOperand::CreateExpr(SymbolExpr));
1027 OutStreamer.EmitInstruction(TmpInst);
1028 }
1029 {
1030 MCInst TmpInst;
1031 TmpInst.setOpcode(ARM::tMOVi8);
1032 TmpInst.addOperand(MCOperand::CreateReg(ARM::R0));
1033 TmpInst.addOperand(MCOperand::CreateReg(ARM::CPSR));
1034 TmpInst.addOperand(MCOperand::CreateImm(1));
1035 // Predicate.
1036 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1037 TmpInst.addOperand(MCOperand::CreateReg(0));
1038 OutStreamer.AddComment("eh_setjmp end");
1039 OutStreamer.EmitInstruction(TmpInst);
1040 }
1041 OutStreamer.EmitLabel(Label);
1042 return;
1043 }
1044
Jim Grosbach45390082010-09-23 23:33:56 +00001045 case ARM::Int_eh_sjlj_setjmp_nofp:
Jim Grosbacha3fbadf2010-09-30 19:53:58 +00001046 case ARM::Int_eh_sjlj_setjmp: {
Jim Grosbach45390082010-09-23 23:33:56 +00001047 // Two incoming args: GPR:$src, GPR:$val
1048 // add $val, pc, #8
1049 // str $val, [$src, #+4]
1050 // mov r0, #0
1051 // add pc, pc, #0
1052 // mov r0, #1
1053 unsigned SrcReg = MI->getOperand(0).getReg();
1054 unsigned ValReg = MI->getOperand(1).getReg();
1055
1056 {
1057 MCInst TmpInst;
1058 TmpInst.setOpcode(ARM::ADDri);
1059 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
1060 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1061 TmpInst.addOperand(MCOperand::CreateImm(8));
1062 // Predicate.
1063 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1064 TmpInst.addOperand(MCOperand::CreateReg(0));
1065 // 's' bit operand (always reg0 for this).
1066 TmpInst.addOperand(MCOperand::CreateReg(0));
1067 OutStreamer.AddComment("eh_setjmp begin");
1068 OutStreamer.EmitInstruction(TmpInst);
1069 }
1070 {
1071 MCInst TmpInst;
Jim Grosbach7e3383c2010-10-27 23:12:14 +00001072 TmpInst.setOpcode(ARM::STRi12);
Jim Grosbach45390082010-09-23 23:33:56 +00001073 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
1074 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
Jim Grosbach45390082010-09-23 23:33:56 +00001075 TmpInst.addOperand(MCOperand::CreateImm(4));
1076 // Predicate.
1077 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1078 TmpInst.addOperand(MCOperand::CreateReg(0));
1079 OutStreamer.EmitInstruction(TmpInst);
1080 }
1081 {
1082 MCInst TmpInst;
1083 TmpInst.setOpcode(ARM::MOVi);
1084 TmpInst.addOperand(MCOperand::CreateReg(ARM::R0));
1085 TmpInst.addOperand(MCOperand::CreateImm(0));
1086 // Predicate.
1087 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1088 TmpInst.addOperand(MCOperand::CreateReg(0));
1089 // 's' bit operand (always reg0 for this).
1090 TmpInst.addOperand(MCOperand::CreateReg(0));
1091 OutStreamer.EmitInstruction(TmpInst);
1092 }
1093 {
1094 MCInst TmpInst;
1095 TmpInst.setOpcode(ARM::ADDri);
1096 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1097 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1098 TmpInst.addOperand(MCOperand::CreateImm(0));
1099 // Predicate.
1100 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1101 TmpInst.addOperand(MCOperand::CreateReg(0));
1102 // 's' bit operand (always reg0 for this).
1103 TmpInst.addOperand(MCOperand::CreateReg(0));
1104 OutStreamer.EmitInstruction(TmpInst);
1105 }
1106 {
1107 MCInst TmpInst;
1108 TmpInst.setOpcode(ARM::MOVi);
1109 TmpInst.addOperand(MCOperand::CreateReg(ARM::R0));
1110 TmpInst.addOperand(MCOperand::CreateImm(1));
1111 // Predicate.
1112 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1113 TmpInst.addOperand(MCOperand::CreateReg(0));
1114 // 's' bit operand (always reg0 for this).
1115 TmpInst.addOperand(MCOperand::CreateReg(0));
1116 OutStreamer.AddComment("eh_setjmp end");
1117 OutStreamer.EmitInstruction(TmpInst);
1118 }
1119 return;
1120 }
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001121 case ARM::Int_eh_sjlj_longjmp: {
1122 // ldr sp, [$src, #8]
1123 // ldr $scratch, [$src, #4]
1124 // ldr r7, [$src]
1125 // bx $scratch
1126 unsigned SrcReg = MI->getOperand(0).getReg();
1127 unsigned ScratchReg = MI->getOperand(1).getReg();
1128 {
1129 MCInst TmpInst;
Jim Grosbach3e556122010-10-26 22:37:02 +00001130 TmpInst.setOpcode(ARM::LDRi12);
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001131 TmpInst.addOperand(MCOperand::CreateReg(ARM::SP));
1132 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001133 TmpInst.addOperand(MCOperand::CreateImm(8));
1134 // Predicate.
1135 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1136 TmpInst.addOperand(MCOperand::CreateReg(0));
1137 OutStreamer.EmitInstruction(TmpInst);
1138 }
1139 {
1140 MCInst TmpInst;
Jim Grosbach3e556122010-10-26 22:37:02 +00001141 TmpInst.setOpcode(ARM::LDRi12);
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001142 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1143 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001144 TmpInst.addOperand(MCOperand::CreateImm(4));
1145 // Predicate.
1146 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1147 TmpInst.addOperand(MCOperand::CreateReg(0));
1148 OutStreamer.EmitInstruction(TmpInst);
1149 }
1150 {
1151 MCInst TmpInst;
Jim Grosbach3e556122010-10-26 22:37:02 +00001152 TmpInst.setOpcode(ARM::LDRi12);
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001153 TmpInst.addOperand(MCOperand::CreateReg(ARM::R7));
1154 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001155 TmpInst.addOperand(MCOperand::CreateImm(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::BRIND);
1164 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1165 // Predicate.
1166 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1167 TmpInst.addOperand(MCOperand::CreateReg(0));
1168 OutStreamer.EmitInstruction(TmpInst);
1169 }
1170 return;
1171 }
Jim Grosbach385cc5e2010-09-27 22:28:11 +00001172 case ARM::tInt_eh_sjlj_longjmp: {
1173 // ldr $scratch, [$src, #8]
1174 // mov sp, $scratch
1175 // ldr $scratch, [$src, #4]
1176 // ldr r7, [$src]
1177 // bx $scratch
1178 unsigned SrcReg = MI->getOperand(0).getReg();
1179 unsigned ScratchReg = MI->getOperand(1).getReg();
1180 {
1181 MCInst TmpInst;
1182 TmpInst.setOpcode(ARM::tLDR);
1183 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1184 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
1185 // The offset immediate is #8. The operand value is scaled by 4 for the
1186 // tSTR instruction.
1187 TmpInst.addOperand(MCOperand::CreateImm(2));
1188 TmpInst.addOperand(MCOperand::CreateReg(0));
1189 // Predicate.
1190 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1191 TmpInst.addOperand(MCOperand::CreateReg(0));
1192 OutStreamer.EmitInstruction(TmpInst);
1193 }
1194 {
1195 MCInst TmpInst;
1196 TmpInst.setOpcode(ARM::tMOVtgpr2gpr);
1197 TmpInst.addOperand(MCOperand::CreateReg(ARM::SP));
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 {
1205 MCInst TmpInst;
1206 TmpInst.setOpcode(ARM::tLDR);
1207 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1208 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
1209 TmpInst.addOperand(MCOperand::CreateImm(1));
1210 TmpInst.addOperand(MCOperand::CreateReg(0));
1211 // Predicate.
1212 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1213 TmpInst.addOperand(MCOperand::CreateReg(0));
1214 OutStreamer.EmitInstruction(TmpInst);
1215 }
1216 {
1217 MCInst TmpInst;
1218 TmpInst.setOpcode(ARM::tLDR);
1219 TmpInst.addOperand(MCOperand::CreateReg(ARM::R7));
1220 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
1221 TmpInst.addOperand(MCOperand::CreateImm(0));
1222 TmpInst.addOperand(MCOperand::CreateReg(0));
1223 // Predicate.
1224 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1225 TmpInst.addOperand(MCOperand::CreateReg(0));
1226 OutStreamer.EmitInstruction(TmpInst);
1227 }
1228 {
1229 MCInst TmpInst;
1230 TmpInst.setOpcode(ARM::tBX_RET_vararg);
1231 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1232 // Predicate.
1233 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1234 TmpInst.addOperand(MCOperand::CreateReg(0));
1235 OutStreamer.EmitInstruction(TmpInst);
1236 }
1237 return;
1238 }
Chris Lattner97f06932009-10-19 20:20:46 +00001239 }
Jim Grosbachb0739b72010-09-02 01:02:06 +00001240
Chris Lattner97f06932009-10-19 20:20:46 +00001241 MCInst TmpInst;
1242 MCInstLowering.Lower(MI, TmpInst);
Chris Lattner850d2e22010-02-03 01:16:28 +00001243 OutStreamer.EmitInstruction(TmpInst);
Chris Lattner97f06932009-10-19 20:20:46 +00001244}
Daniel Dunbar2685a292009-10-20 05:15:36 +00001245
1246//===----------------------------------------------------------------------===//
1247// Target Registry Stuff
1248//===----------------------------------------------------------------------===//
1249
1250static MCInstPrinter *createARMMCInstPrinter(const Target &T,
1251 unsigned SyntaxVariant,
Chris Lattnerd3740872010-04-04 05:04:31 +00001252 const MCAsmInfo &MAI) {
Daniel Dunbar2685a292009-10-20 05:15:36 +00001253 if (SyntaxVariant == 0)
Jim Grosbach74d7e6c2010-09-17 21:33:25 +00001254 return new ARMInstPrinter(MAI);
Daniel Dunbar2685a292009-10-20 05:15:36 +00001255 return 0;
1256}
1257
1258// Force static initialization.
1259extern "C" void LLVMInitializeARMAsmPrinter() {
1260 RegisterAsmPrinter<ARMAsmPrinter> X(TheARMTarget);
1261 RegisterAsmPrinter<ARMAsmPrinter> Y(TheThumbTarget);
1262
1263 TargetRegistry::RegisterMCInstPrinter(TheARMTarget, createARMMCInstPrinter);
1264 TargetRegistry::RegisterMCInstPrinter(TheThumbTarget, createARMMCInstPrinter);
1265}
1266